ctl.c revision 288820
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 288820 2015-10-05 11:48:14Z 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 bzero(&msg.mode, sizeof(msg.mode)); 933 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 934 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 935 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 936 msg.hdr.nexus.targ_lun = lun->lun; 937 msg.hdr.nexus.targ_mapped_lun = lun->lun; 938 msg.mode.page_code = page; 939 msg.mode.subpage = subpage; 940 msg.mode.page_len = lun->mode_pages.index[i].page_len; 941 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 942 msg.mode.page_len); 943 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 944 M_WAITOK); 945} 946 947static void 948ctl_isc_ha_link_up(struct ctl_softc *softc) 949{ 950 struct ctl_port *port; 951 struct ctl_lun *lun; 952 union ctl_ha_msg msg; 953 int i; 954 955 /* Announce this node parameters to peer for validation. */ 956 msg.login.msg_type = CTL_MSG_LOGIN; 957 msg.login.version = CTL_HA_VERSION; 958 msg.login.ha_mode = softc->ha_mode; 959 msg.login.ha_id = softc->ha_id; 960 msg.login.max_luns = CTL_MAX_LUNS; 961 msg.login.max_ports = CTL_MAX_PORTS; 962 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 963 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 964 M_WAITOK); 965 966 STAILQ_FOREACH(port, &softc->port_list, links) { 967 ctl_isc_announce_port(port); 968 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 969 if (port->wwpn_iid[i].in_use) 970 ctl_isc_announce_iid(port, i); 971 } 972 } 973 STAILQ_FOREACH(lun, &softc->lun_list, links) 974 ctl_isc_announce_lun(lun); 975} 976 977static void 978ctl_isc_ha_link_down(struct ctl_softc *softc) 979{ 980 struct ctl_port *port; 981 struct ctl_lun *lun; 982 union ctl_io *io; 983 int i; 984 985 mtx_lock(&softc->ctl_lock); 986 STAILQ_FOREACH(lun, &softc->lun_list, links) { 987 mtx_lock(&lun->lun_lock); 988 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 989 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 990 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 991 } 992 mtx_unlock(&lun->lun_lock); 993 994 mtx_unlock(&softc->ctl_lock); 995 io = ctl_alloc_io(softc->othersc_pool); 996 mtx_lock(&softc->ctl_lock); 997 ctl_zero_io(io); 998 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 999 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1000 ctl_enqueue_isc(io); 1001 } 1002 1003 STAILQ_FOREACH(port, &softc->port_list, links) { 1004 if (port->targ_port >= softc->port_min && 1005 port->targ_port < softc->port_max) 1006 continue; 1007 port->status &= ~CTL_PORT_STATUS_ONLINE; 1008 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1009 port->wwpn_iid[i].in_use = 0; 1010 free(port->wwpn_iid[i].name, M_CTL); 1011 port->wwpn_iid[i].name = NULL; 1012 } 1013 } 1014 mtx_unlock(&softc->ctl_lock); 1015} 1016 1017static void 1018ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1019{ 1020 struct ctl_lun *lun; 1021 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1022 1023 mtx_lock(&softc->ctl_lock); 1024 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 1025 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 1026 mtx_lock(&lun->lun_lock); 1027 mtx_unlock(&softc->ctl_lock); 1028 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 1029 msg->ua.ua_set) 1030 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1031 if (msg->ua.ua_all) { 1032 if (msg->ua.ua_set) 1033 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1034 else 1035 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1036 } else { 1037 if (msg->ua.ua_set) 1038 ctl_est_ua(lun, iid, msg->ua.ua_type); 1039 else 1040 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1041 } 1042 mtx_unlock(&lun->lun_lock); 1043 } else 1044 mtx_unlock(&softc->ctl_lock); 1045} 1046 1047static void 1048ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1049{ 1050 struct ctl_lun *lun; 1051 struct ctl_ha_msg_lun_pr_key pr_key; 1052 int i, k; 1053 ctl_lun_flags oflags; 1054 uint32_t targ_lun; 1055 1056 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1057 mtx_lock(&softc->ctl_lock); 1058 if ((targ_lun >= CTL_MAX_LUNS) || 1059 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1060 mtx_unlock(&softc->ctl_lock); 1061 return; 1062 } 1063 mtx_lock(&lun->lun_lock); 1064 mtx_unlock(&softc->ctl_lock); 1065 if (lun->flags & CTL_LUN_DISABLED) { 1066 mtx_unlock(&lun->lun_lock); 1067 return; 1068 } 1069 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1070 if (msg->lun.lun_devid_len != i || (i > 0 && 1071 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1072 mtx_unlock(&lun->lun_lock); 1073 printf("%s: Received conflicting HA LUN %d\n", 1074 __func__, msg->hdr.nexus.targ_lun); 1075 return; 1076 } else { 1077 /* Record whether peer is primary. */ 1078 oflags = lun->flags; 1079 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1080 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1081 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1082 else 1083 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1084 if (oflags != lun->flags) 1085 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1086 1087 /* If peer is primary and we are not -- use data */ 1088 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1089 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1090 lun->pr_generation = msg->lun.pr_generation; 1091 lun->pr_res_idx = msg->lun.pr_res_idx; 1092 lun->pr_res_type = msg->lun.pr_res_type; 1093 lun->pr_key_count = msg->lun.pr_key_count; 1094 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1095 ctl_clr_prkey(lun, k); 1096 for (k = 0; k < msg->lun.pr_key_count; k++) { 1097 memcpy(&pr_key, &msg->lun.data[i], 1098 sizeof(pr_key)); 1099 ctl_alloc_prkey(lun, pr_key.pr_iid); 1100 ctl_set_prkey(lun, pr_key.pr_iid, 1101 pr_key.pr_key); 1102 i += sizeof(pr_key); 1103 } 1104 } 1105 1106 mtx_unlock(&lun->lun_lock); 1107 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1108 __func__, msg->hdr.nexus.targ_lun, 1109 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1110 "primary" : "secondary")); 1111 1112 /* If we are primary but peer doesn't know -- notify */ 1113 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1114 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1115 ctl_isc_announce_lun(lun); 1116 } 1117} 1118 1119static void 1120ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1121{ 1122 struct ctl_port *port; 1123 struct ctl_lun *lun; 1124 int i, new; 1125 1126 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1127 if (port == NULL) { 1128 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1129 msg->hdr.nexus.targ_port)); 1130 new = 1; 1131 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1132 port->frontend = &ha_frontend; 1133 port->targ_port = msg->hdr.nexus.targ_port; 1134 port->fe_datamove = ctl_ha_datamove; 1135 port->fe_done = ctl_ha_done; 1136 } else if (port->frontend == &ha_frontend) { 1137 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1138 msg->hdr.nexus.targ_port)); 1139 new = 0; 1140 } else { 1141 printf("%s: Received conflicting HA port %d\n", 1142 __func__, msg->hdr.nexus.targ_port); 1143 return; 1144 } 1145 port->port_type = msg->port.port_type; 1146 port->physical_port = msg->port.physical_port; 1147 port->virtual_port = msg->port.virtual_port; 1148 port->status = msg->port.status; 1149 i = 0; 1150 free(port->port_name, M_CTL); 1151 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1152 M_CTL); 1153 i += msg->port.name_len; 1154 if (msg->port.lun_map_len != 0) { 1155 if (port->lun_map == NULL) 1156 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 1157 M_CTL, M_WAITOK); 1158 memcpy(port->lun_map, &msg->port.data[i], 1159 sizeof(uint32_t) * CTL_MAX_LUNS); 1160 i += msg->port.lun_map_len; 1161 } else { 1162 free(port->lun_map, M_CTL); 1163 port->lun_map = NULL; 1164 } 1165 if (msg->port.port_devid_len != 0) { 1166 if (port->port_devid == NULL || 1167 port->port_devid->len != msg->port.port_devid_len) { 1168 free(port->port_devid, M_CTL); 1169 port->port_devid = malloc(sizeof(struct ctl_devid) + 1170 msg->port.port_devid_len, M_CTL, M_WAITOK); 1171 } 1172 memcpy(port->port_devid->data, &msg->port.data[i], 1173 msg->port.port_devid_len); 1174 port->port_devid->len = msg->port.port_devid_len; 1175 i += msg->port.port_devid_len; 1176 } else { 1177 free(port->port_devid, M_CTL); 1178 port->port_devid = NULL; 1179 } 1180 if (msg->port.target_devid_len != 0) { 1181 if (port->target_devid == NULL || 1182 port->target_devid->len != msg->port.target_devid_len) { 1183 free(port->target_devid, M_CTL); 1184 port->target_devid = malloc(sizeof(struct ctl_devid) + 1185 msg->port.target_devid_len, M_CTL, M_WAITOK); 1186 } 1187 memcpy(port->target_devid->data, &msg->port.data[i], 1188 msg->port.target_devid_len); 1189 port->target_devid->len = msg->port.target_devid_len; 1190 i += msg->port.target_devid_len; 1191 } else { 1192 free(port->target_devid, M_CTL); 1193 port->target_devid = NULL; 1194 } 1195 if (msg->port.init_devid_len != 0) { 1196 if (port->init_devid == NULL || 1197 port->init_devid->len != msg->port.init_devid_len) { 1198 free(port->init_devid, M_CTL); 1199 port->init_devid = malloc(sizeof(struct ctl_devid) + 1200 msg->port.init_devid_len, M_CTL, M_WAITOK); 1201 } 1202 memcpy(port->init_devid->data, &msg->port.data[i], 1203 msg->port.init_devid_len); 1204 port->init_devid->len = msg->port.init_devid_len; 1205 i += msg->port.init_devid_len; 1206 } else { 1207 free(port->init_devid, M_CTL); 1208 port->init_devid = NULL; 1209 } 1210 if (new) { 1211 if (ctl_port_register(port) != 0) { 1212 printf("%s: ctl_port_register() failed with error\n", 1213 __func__); 1214 } 1215 } 1216 mtx_lock(&softc->ctl_lock); 1217 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1218 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 1219 continue; 1220 mtx_lock(&lun->lun_lock); 1221 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1222 mtx_unlock(&lun->lun_lock); 1223 } 1224 mtx_unlock(&softc->ctl_lock); 1225} 1226 1227static void 1228ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1229{ 1230 struct ctl_port *port; 1231 int iid; 1232 1233 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1234 if (port == NULL) { 1235 printf("%s: Received IID for unknown port %d\n", 1236 __func__, msg->hdr.nexus.targ_port); 1237 return; 1238 } 1239 iid = msg->hdr.nexus.initid; 1240 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1241 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1242 free(port->wwpn_iid[iid].name, M_CTL); 1243 if (msg->iid.name_len) { 1244 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1245 msg->iid.name_len, M_CTL); 1246 } else 1247 port->wwpn_iid[iid].name = NULL; 1248} 1249 1250static void 1251ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1252{ 1253 1254 if (msg->login.version != CTL_HA_VERSION) { 1255 printf("CTL HA peers have different versions %d != %d\n", 1256 msg->login.version, CTL_HA_VERSION); 1257 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1258 return; 1259 } 1260 if (msg->login.ha_mode != softc->ha_mode) { 1261 printf("CTL HA peers have different ha_mode %d != %d\n", 1262 msg->login.ha_mode, softc->ha_mode); 1263 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1264 return; 1265 } 1266 if (msg->login.ha_id == softc->ha_id) { 1267 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1268 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1269 return; 1270 } 1271 if (msg->login.max_luns != CTL_MAX_LUNS || 1272 msg->login.max_ports != CTL_MAX_PORTS || 1273 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1274 printf("CTL HA peers have different limits\n"); 1275 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1276 return; 1277 } 1278} 1279 1280static void 1281ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1282{ 1283 struct ctl_lun *lun; 1284 int i; 1285 uint32_t initidx, targ_lun; 1286 1287 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1288 mtx_lock(&softc->ctl_lock); 1289 if ((targ_lun >= CTL_MAX_LUNS) || 1290 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1291 mtx_unlock(&softc->ctl_lock); 1292 return; 1293 } 1294 mtx_lock(&lun->lun_lock); 1295 mtx_unlock(&softc->ctl_lock); 1296 if (lun->flags & CTL_LUN_DISABLED) { 1297 mtx_unlock(&lun->lun_lock); 1298 return; 1299 } 1300 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1301 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1302 msg->mode.page_code && 1303 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1304 break; 1305 } 1306 if (i == CTL_NUM_MODE_PAGES) { 1307 mtx_unlock(&lun->lun_lock); 1308 return; 1309 } 1310 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1311 lun->mode_pages.index[i].page_len); 1312 initidx = ctl_get_initindex(&msg->hdr.nexus); 1313 if (initidx != -1) 1314 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1315 mtx_unlock(&lun->lun_lock); 1316} 1317 1318/* 1319 * ISC (Inter Shelf Communication) event handler. Events from the HA 1320 * subsystem come in here. 1321 */ 1322static void 1323ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1324{ 1325 struct ctl_softc *softc = control_softc; 1326 union ctl_io *io; 1327 struct ctl_prio *presio; 1328 ctl_ha_status isc_status; 1329 1330 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1331 if (event == CTL_HA_EVT_MSG_RECV) { 1332 union ctl_ha_msg *msg, msgbuf; 1333 1334 if (param > sizeof(msgbuf)) 1335 msg = malloc(param, M_CTL, M_WAITOK); 1336 else 1337 msg = &msgbuf; 1338 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1339 M_WAITOK); 1340 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1341 printf("%s: Error receiving message: %d\n", 1342 __func__, isc_status); 1343 if (msg != &msgbuf) 1344 free(msg, M_CTL); 1345 return; 1346 } 1347 1348 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1349 switch (msg->hdr.msg_type) { 1350 case CTL_MSG_SERIALIZE: 1351 io = ctl_alloc_io(softc->othersc_pool); 1352 ctl_zero_io(io); 1353 // populate ctsio from msg 1354 io->io_hdr.io_type = CTL_IO_SCSI; 1355 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1356 io->io_hdr.original_sc = msg->hdr.original_sc; 1357 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1358 CTL_FLAG_IO_ACTIVE; 1359 /* 1360 * If we're in serialization-only mode, we don't 1361 * want to go through full done processing. Thus 1362 * the COPY flag. 1363 * 1364 * XXX KDM add another flag that is more specific. 1365 */ 1366 if (softc->ha_mode != CTL_HA_MODE_XFER) 1367 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1368 io->io_hdr.nexus = msg->hdr.nexus; 1369#if 0 1370 printf("port %u, iid %u, lun %u\n", 1371 io->io_hdr.nexus.targ_port, 1372 io->io_hdr.nexus.initid, 1373 io->io_hdr.nexus.targ_lun); 1374#endif 1375 io->scsiio.tag_num = msg->scsi.tag_num; 1376 io->scsiio.tag_type = msg->scsi.tag_type; 1377#ifdef CTL_TIME_IO 1378 io->io_hdr.start_time = time_uptime; 1379 getbinuptime(&io->io_hdr.start_bt); 1380#endif /* CTL_TIME_IO */ 1381 io->scsiio.cdb_len = msg->scsi.cdb_len; 1382 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1383 CTL_MAX_CDBLEN); 1384 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1385 const struct ctl_cmd_entry *entry; 1386 1387 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1388 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1389 io->io_hdr.flags |= 1390 entry->flags & CTL_FLAG_DATA_MASK; 1391 } 1392 ctl_enqueue_isc(io); 1393 break; 1394 1395 /* Performed on the Originating SC, XFER mode only */ 1396 case CTL_MSG_DATAMOVE: { 1397 struct ctl_sg_entry *sgl; 1398 int i, j; 1399 1400 io = msg->hdr.original_sc; 1401 if (io == NULL) { 1402 printf("%s: original_sc == NULL!\n", __func__); 1403 /* XXX KDM do something here */ 1404 break; 1405 } 1406 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1407 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1408 /* 1409 * Keep track of this, we need to send it back over 1410 * when the datamove is complete. 1411 */ 1412 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1413 if (msg->hdr.status == CTL_SUCCESS) 1414 io->io_hdr.status = msg->hdr.status; 1415 1416 if (msg->dt.sg_sequence == 0) { 1417#ifdef CTL_TIME_IO 1418 getbinuptime(&io->io_hdr.dma_start_bt); 1419#endif 1420 i = msg->dt.kern_sg_entries + 1421 msg->dt.kern_data_len / 1422 CTL_HA_DATAMOVE_SEGMENT + 1; 1423 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1424 M_WAITOK | M_ZERO); 1425 io->io_hdr.remote_sglist = sgl; 1426 io->io_hdr.local_sglist = 1427 &sgl[msg->dt.kern_sg_entries]; 1428 1429 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1430 1431 io->scsiio.kern_sg_entries = 1432 msg->dt.kern_sg_entries; 1433 io->scsiio.rem_sg_entries = 1434 msg->dt.kern_sg_entries; 1435 io->scsiio.kern_data_len = 1436 msg->dt.kern_data_len; 1437 io->scsiio.kern_total_len = 1438 msg->dt.kern_total_len; 1439 io->scsiio.kern_data_resid = 1440 msg->dt.kern_data_resid; 1441 io->scsiio.kern_rel_offset = 1442 msg->dt.kern_rel_offset; 1443 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1444 io->io_hdr.flags |= msg->dt.flags & 1445 CTL_FLAG_BUS_ADDR; 1446 } else 1447 sgl = (struct ctl_sg_entry *) 1448 io->scsiio.kern_data_ptr; 1449 1450 for (i = msg->dt.sent_sg_entries, j = 0; 1451 i < (msg->dt.sent_sg_entries + 1452 msg->dt.cur_sg_entries); i++, j++) { 1453 sgl[i].addr = msg->dt.sg_list[j].addr; 1454 sgl[i].len = msg->dt.sg_list[j].len; 1455 1456#if 0 1457 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1458 __func__, sgl[i].addr, sgl[i].len, j, i); 1459#endif 1460 } 1461 1462 /* 1463 * If this is the last piece of the I/O, we've got 1464 * the full S/G list. Queue processing in the thread. 1465 * Otherwise wait for the next piece. 1466 */ 1467 if (msg->dt.sg_last != 0) 1468 ctl_enqueue_isc(io); 1469 break; 1470 } 1471 /* Performed on the Serializing (primary) SC, XFER mode only */ 1472 case CTL_MSG_DATAMOVE_DONE: { 1473 if (msg->hdr.serializing_sc == NULL) { 1474 printf("%s: serializing_sc == NULL!\n", 1475 __func__); 1476 /* XXX KDM now what? */ 1477 break; 1478 } 1479 /* 1480 * We grab the sense information here in case 1481 * there was a failure, so we can return status 1482 * back to the initiator. 1483 */ 1484 io = msg->hdr.serializing_sc; 1485 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1486 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1487 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1488 io->io_hdr.port_status = msg->scsi.fetd_status; 1489 io->scsiio.residual = msg->scsi.residual; 1490 if (msg->hdr.status != CTL_STATUS_NONE) { 1491 io->io_hdr.status = msg->hdr.status; 1492 io->scsiio.scsi_status = msg->scsi.scsi_status; 1493 io->scsiio.sense_len = msg->scsi.sense_len; 1494 io->scsiio.sense_residual =msg->scsi.sense_residual; 1495 memcpy(&io->scsiio.sense_data, 1496 &msg->scsi.sense_data, 1497 msg->scsi.sense_len); 1498 if (msg->hdr.status == CTL_SUCCESS) 1499 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1500 } 1501 ctl_enqueue_isc(io); 1502 break; 1503 } 1504 1505 /* Preformed on Originating SC, SER_ONLY mode */ 1506 case CTL_MSG_R2R: 1507 io = msg->hdr.original_sc; 1508 if (io == NULL) { 1509 printf("%s: original_sc == NULL!\n", 1510 __func__); 1511 break; 1512 } 1513 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1514 io->io_hdr.msg_type = CTL_MSG_R2R; 1515 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1516 ctl_enqueue_isc(io); 1517 break; 1518 1519 /* 1520 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1521 * mode. 1522 * Performed on the Originating (i.e. secondary) SC in XFER 1523 * mode 1524 */ 1525 case CTL_MSG_FINISH_IO: 1526 if (softc->ha_mode == CTL_HA_MODE_XFER) 1527 ctl_isc_handler_finish_xfer(softc, msg); 1528 else 1529 ctl_isc_handler_finish_ser_only(softc, msg); 1530 break; 1531 1532 /* Preformed on Originating SC */ 1533 case CTL_MSG_BAD_JUJU: 1534 io = msg->hdr.original_sc; 1535 if (io == NULL) { 1536 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1537 __func__); 1538 break; 1539 } 1540 ctl_copy_sense_data(msg, io); 1541 /* 1542 * IO should have already been cleaned up on other 1543 * SC so clear this flag so we won't send a message 1544 * back to finish the IO there. 1545 */ 1546 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1547 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1548 1549 /* io = msg->hdr.serializing_sc; */ 1550 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1551 ctl_enqueue_isc(io); 1552 break; 1553 1554 /* Handle resets sent from the other side */ 1555 case CTL_MSG_MANAGE_TASKS: { 1556 struct ctl_taskio *taskio; 1557 taskio = (struct ctl_taskio *)ctl_alloc_io( 1558 softc->othersc_pool); 1559 ctl_zero_io((union ctl_io *)taskio); 1560 taskio->io_hdr.io_type = CTL_IO_TASK; 1561 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1562 taskio->io_hdr.nexus = msg->hdr.nexus; 1563 taskio->task_action = msg->task.task_action; 1564 taskio->tag_num = msg->task.tag_num; 1565 taskio->tag_type = msg->task.tag_type; 1566#ifdef CTL_TIME_IO 1567 taskio->io_hdr.start_time = time_uptime; 1568 getbinuptime(&taskio->io_hdr.start_bt); 1569#endif /* CTL_TIME_IO */ 1570 ctl_run_task((union ctl_io *)taskio); 1571 break; 1572 } 1573 /* Persistent Reserve action which needs attention */ 1574 case CTL_MSG_PERS_ACTION: 1575 presio = (struct ctl_prio *)ctl_alloc_io( 1576 softc->othersc_pool); 1577 ctl_zero_io((union ctl_io *)presio); 1578 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1579 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1580 presio->io_hdr.nexus = msg->hdr.nexus; 1581 presio->pr_msg = msg->pr; 1582 ctl_enqueue_isc((union ctl_io *)presio); 1583 break; 1584 case CTL_MSG_UA: 1585 ctl_isc_ua(softc, msg, param); 1586 break; 1587 case CTL_MSG_PORT_SYNC: 1588 ctl_isc_port_sync(softc, msg, param); 1589 break; 1590 case CTL_MSG_LUN_SYNC: 1591 ctl_isc_lun_sync(softc, msg, param); 1592 break; 1593 case CTL_MSG_IID_SYNC: 1594 ctl_isc_iid_sync(softc, msg, param); 1595 break; 1596 case CTL_MSG_LOGIN: 1597 ctl_isc_login(softc, msg, param); 1598 break; 1599 case CTL_MSG_MODE_SYNC: 1600 ctl_isc_mode_sync(softc, msg, param); 1601 break; 1602 default: 1603 printf("Received HA message of unknown type %d\n", 1604 msg->hdr.msg_type); 1605 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1606 break; 1607 } 1608 if (msg != &msgbuf) 1609 free(msg, M_CTL); 1610 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1611 printf("CTL: HA link status changed from %d to %d\n", 1612 softc->ha_link, param); 1613 if (param == softc->ha_link) 1614 return; 1615 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1616 softc->ha_link = param; 1617 ctl_isc_ha_link_down(softc); 1618 } else { 1619 softc->ha_link = param; 1620 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1621 ctl_isc_ha_link_up(softc); 1622 } 1623 return; 1624 } else { 1625 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1626 return; 1627 } 1628} 1629 1630static void 1631ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1632{ 1633 1634 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1635 src->scsi.sense_len); 1636 dest->scsiio.scsi_status = src->scsi.scsi_status; 1637 dest->scsiio.sense_len = src->scsi.sense_len; 1638 dest->io_hdr.status = src->hdr.status; 1639} 1640 1641static void 1642ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1643{ 1644 1645 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1646 src->scsiio.sense_len); 1647 dest->scsi.scsi_status = src->scsiio.scsi_status; 1648 dest->scsi.sense_len = src->scsiio.sense_len; 1649 dest->hdr.status = src->io_hdr.status; 1650} 1651 1652void 1653ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1654{ 1655 struct ctl_softc *softc = lun->ctl_softc; 1656 ctl_ua_type *pu; 1657 1658 if (initidx < softc->init_min || initidx >= softc->init_max) 1659 return; 1660 mtx_assert(&lun->lun_lock, MA_OWNED); 1661 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1662 if (pu == NULL) 1663 return; 1664 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1665} 1666 1667void 1668ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1669{ 1670 int i; 1671 1672 mtx_assert(&lun->lun_lock, MA_OWNED); 1673 if (lun->pending_ua[port] == NULL) 1674 return; 1675 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1676 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1677 continue; 1678 lun->pending_ua[port][i] |= ua; 1679 } 1680} 1681 1682void 1683ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1684{ 1685 struct ctl_softc *softc = lun->ctl_softc; 1686 int i; 1687 1688 mtx_assert(&lun->lun_lock, MA_OWNED); 1689 for (i = softc->port_min; i < softc->port_max; i++) 1690 ctl_est_ua_port(lun, i, except, ua); 1691} 1692 1693void 1694ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1695{ 1696 struct ctl_softc *softc = lun->ctl_softc; 1697 ctl_ua_type *pu; 1698 1699 if (initidx < softc->init_min || initidx >= softc->init_max) 1700 return; 1701 mtx_assert(&lun->lun_lock, MA_OWNED); 1702 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1703 if (pu == NULL) 1704 return; 1705 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1706} 1707 1708void 1709ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1710{ 1711 struct ctl_softc *softc = lun->ctl_softc; 1712 int i, j; 1713 1714 mtx_assert(&lun->lun_lock, MA_OWNED); 1715 for (i = softc->port_min; i < softc->port_max; i++) { 1716 if (lun->pending_ua[i] == NULL) 1717 continue; 1718 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1719 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1720 continue; 1721 lun->pending_ua[i][j] &= ~ua; 1722 } 1723 } 1724} 1725 1726void 1727ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1728 ctl_ua_type ua_type) 1729{ 1730 struct ctl_lun *lun; 1731 1732 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1733 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1734 mtx_lock(&lun->lun_lock); 1735 ctl_clr_ua(lun, initidx, ua_type); 1736 mtx_unlock(&lun->lun_lock); 1737 } 1738} 1739 1740static int 1741ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1742{ 1743 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1744 struct ctl_lun *lun; 1745 struct ctl_lun_req ireq; 1746 int error, value; 1747 1748 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1749 error = sysctl_handle_int(oidp, &value, 0, req); 1750 if ((error != 0) || (req->newptr == NULL)) 1751 return (error); 1752 1753 mtx_lock(&softc->ctl_lock); 1754 if (value == 0) 1755 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1756 else 1757 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1758 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1759 mtx_unlock(&softc->ctl_lock); 1760 bzero(&ireq, sizeof(ireq)); 1761 ireq.reqtype = CTL_LUNREQ_MODIFY; 1762 ireq.reqdata.modify.lun_id = lun->lun; 1763 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1764 curthread); 1765 if (ireq.status != CTL_LUN_OK) { 1766 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1767 __func__, ireq.status, ireq.error_str); 1768 } 1769 mtx_lock(&softc->ctl_lock); 1770 } 1771 mtx_unlock(&softc->ctl_lock); 1772 return (0); 1773} 1774 1775static int 1776ctl_init(void) 1777{ 1778 struct ctl_softc *softc; 1779 void *other_pool; 1780 int i, error; 1781 1782 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1783 M_WAITOK | M_ZERO); 1784 1785 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1786 "cam/ctl"); 1787 softc->dev->si_drv1 = softc; 1788 1789 sysctl_ctx_init(&softc->sysctl_ctx); 1790 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1791 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1792 CTLFLAG_RD, 0, "CAM Target Layer"); 1793 1794 if (softc->sysctl_tree == NULL) { 1795 printf("%s: unable to allocate sysctl tree\n", __func__); 1796 destroy_dev(softc->dev); 1797 free(control_softc, M_DEVBUF); 1798 control_softc = NULL; 1799 return (ENOMEM); 1800 } 1801 1802 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1803 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1804 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1805 softc->flags = 0; 1806 1807 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1808 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1809 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1810 1811 /* 1812 * In Copan's HA scheme, the "master" and "slave" roles are 1813 * figured out through the slot the controller is in. Although it 1814 * is an active/active system, someone has to be in charge. 1815 */ 1816 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1817 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1818 "HA head ID (0 - no HA)"); 1819 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1820 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1821 softc->is_single = 1; 1822 softc->port_cnt = CTL_MAX_PORTS; 1823 softc->port_min = 0; 1824 } else { 1825 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1826 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1827 } 1828 softc->port_max = softc->port_min + softc->port_cnt; 1829 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1830 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1831 1832 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1833 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1834 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1835 1836 STAILQ_INIT(&softc->lun_list); 1837 STAILQ_INIT(&softc->pending_lun_queue); 1838 STAILQ_INIT(&softc->fe_list); 1839 STAILQ_INIT(&softc->port_list); 1840 STAILQ_INIT(&softc->be_list); 1841 ctl_tpc_init(softc); 1842 1843 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1844 &other_pool) != 0) 1845 { 1846 printf("ctl: can't allocate %d entry other SC pool, " 1847 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1848 return (ENOMEM); 1849 } 1850 softc->othersc_pool = other_pool; 1851 1852 if (worker_threads <= 0) 1853 worker_threads = max(1, mp_ncpus / 4); 1854 if (worker_threads > CTL_MAX_THREADS) 1855 worker_threads = CTL_MAX_THREADS; 1856 1857 for (i = 0; i < worker_threads; i++) { 1858 struct ctl_thread *thr = &softc->threads[i]; 1859 1860 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1861 thr->ctl_softc = softc; 1862 STAILQ_INIT(&thr->incoming_queue); 1863 STAILQ_INIT(&thr->rtr_queue); 1864 STAILQ_INIT(&thr->done_queue); 1865 STAILQ_INIT(&thr->isc_queue); 1866 1867 error = kproc_kthread_add(ctl_work_thread, thr, 1868 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1869 if (error != 0) { 1870 printf("error creating CTL work thread!\n"); 1871 ctl_pool_free(other_pool); 1872 return (error); 1873 } 1874 } 1875 error = kproc_kthread_add(ctl_lun_thread, softc, 1876 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1877 if (error != 0) { 1878 printf("error creating CTL lun thread!\n"); 1879 ctl_pool_free(other_pool); 1880 return (error); 1881 } 1882 error = kproc_kthread_add(ctl_thresh_thread, softc, 1883 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1884 if (error != 0) { 1885 printf("error creating CTL threshold thread!\n"); 1886 ctl_pool_free(other_pool); 1887 return (error); 1888 } 1889 1890 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1891 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1892 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1893 1894 if (softc->is_single == 0) { 1895 ctl_frontend_register(&ha_frontend); 1896 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1897 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1898 softc->is_single = 1; 1899 } else 1900 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1901 != CTL_HA_STATUS_SUCCESS) { 1902 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1903 softc->is_single = 1; 1904 } 1905 } 1906 return (0); 1907} 1908 1909void 1910ctl_shutdown(void) 1911{ 1912 struct ctl_softc *softc = control_softc; 1913 struct ctl_lun *lun, *next_lun; 1914 1915 if (softc->is_single == 0) { 1916 ctl_ha_msg_shutdown(softc); 1917 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1918 != CTL_HA_STATUS_SUCCESS) 1919 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1920 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1921 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1922 ctl_frontend_deregister(&ha_frontend); 1923 } 1924 1925 mtx_lock(&softc->ctl_lock); 1926 1927 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1928 ctl_free_lun(lun); 1929 1930 mtx_unlock(&softc->ctl_lock); 1931 1932#if 0 1933 ctl_shutdown_thread(softc->work_thread); 1934 mtx_destroy(&softc->queue_lock); 1935#endif 1936 1937 ctl_tpc_shutdown(softc); 1938 uma_zdestroy(softc->io_zone); 1939 mtx_destroy(&softc->ctl_lock); 1940 1941 destroy_dev(softc->dev); 1942 1943 sysctl_ctx_free(&softc->sysctl_ctx); 1944 1945 free(control_softc, M_DEVBUF); 1946 control_softc = NULL; 1947} 1948 1949static int 1950ctl_module_event_handler(module_t mod, int what, void *arg) 1951{ 1952 1953 switch (what) { 1954 case MOD_LOAD: 1955 return (ctl_init()); 1956 case MOD_UNLOAD: 1957 return (EBUSY); 1958 default: 1959 return (EOPNOTSUPP); 1960 } 1961} 1962 1963/* 1964 * XXX KDM should we do some access checks here? Bump a reference count to 1965 * prevent a CTL module from being unloaded while someone has it open? 1966 */ 1967static int 1968ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1969{ 1970 return (0); 1971} 1972 1973static int 1974ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1975{ 1976 return (0); 1977} 1978 1979/* 1980 * Remove an initiator by port number and initiator ID. 1981 * Returns 0 for success, -1 for failure. 1982 */ 1983int 1984ctl_remove_initiator(struct ctl_port *port, int iid) 1985{ 1986 struct ctl_softc *softc = port->ctl_softc; 1987 1988 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1989 1990 if (iid > CTL_MAX_INIT_PER_PORT) { 1991 printf("%s: initiator ID %u > maximun %u!\n", 1992 __func__, iid, CTL_MAX_INIT_PER_PORT); 1993 return (-1); 1994 } 1995 1996 mtx_lock(&softc->ctl_lock); 1997 port->wwpn_iid[iid].in_use--; 1998 port->wwpn_iid[iid].last_use = time_uptime; 1999 mtx_unlock(&softc->ctl_lock); 2000 ctl_isc_announce_iid(port, iid); 2001 2002 return (0); 2003} 2004 2005/* 2006 * Add an initiator to the initiator map. 2007 * Returns iid for success, < 0 for failure. 2008 */ 2009int 2010ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2011{ 2012 struct ctl_softc *softc = port->ctl_softc; 2013 time_t best_time; 2014 int i, best; 2015 2016 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2017 2018 if (iid >= CTL_MAX_INIT_PER_PORT) { 2019 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2020 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2021 free(name, M_CTL); 2022 return (-1); 2023 } 2024 2025 mtx_lock(&softc->ctl_lock); 2026 2027 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2028 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2029 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2030 iid = i; 2031 break; 2032 } 2033 if (name != NULL && port->wwpn_iid[i].name != NULL && 2034 strcmp(name, port->wwpn_iid[i].name) == 0) { 2035 iid = i; 2036 break; 2037 } 2038 } 2039 } 2040 2041 if (iid < 0) { 2042 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2043 if (port->wwpn_iid[i].in_use == 0 && 2044 port->wwpn_iid[i].wwpn == 0 && 2045 port->wwpn_iid[i].name == NULL) { 2046 iid = i; 2047 break; 2048 } 2049 } 2050 } 2051 2052 if (iid < 0) { 2053 best = -1; 2054 best_time = INT32_MAX; 2055 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2056 if (port->wwpn_iid[i].in_use == 0) { 2057 if (port->wwpn_iid[i].last_use < best_time) { 2058 best = i; 2059 best_time = port->wwpn_iid[i].last_use; 2060 } 2061 } 2062 } 2063 iid = best; 2064 } 2065 2066 if (iid < 0) { 2067 mtx_unlock(&softc->ctl_lock); 2068 free(name, M_CTL); 2069 return (-2); 2070 } 2071 2072 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2073 /* 2074 * This is not an error yet. 2075 */ 2076 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2077#if 0 2078 printf("%s: port %d iid %u WWPN %#jx arrived" 2079 " again\n", __func__, port->targ_port, 2080 iid, (uintmax_t)wwpn); 2081#endif 2082 goto take; 2083 } 2084 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2085 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2086#if 0 2087 printf("%s: port %d iid %u name '%s' arrived" 2088 " again\n", __func__, port->targ_port, 2089 iid, name); 2090#endif 2091 goto take; 2092 } 2093 2094 /* 2095 * This is an error, but what do we do about it? The 2096 * driver is telling us we have a new WWPN for this 2097 * initiator ID, so we pretty much need to use it. 2098 */ 2099 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2100 " but WWPN %#jx '%s' is still at that address\n", 2101 __func__, port->targ_port, iid, wwpn, name, 2102 (uintmax_t)port->wwpn_iid[iid].wwpn, 2103 port->wwpn_iid[iid].name); 2104 2105 /* 2106 * XXX KDM clear have_ca and ua_pending on each LUN for 2107 * this initiator. 2108 */ 2109 } 2110take: 2111 free(port->wwpn_iid[iid].name, M_CTL); 2112 port->wwpn_iid[iid].name = name; 2113 port->wwpn_iid[iid].wwpn = wwpn; 2114 port->wwpn_iid[iid].in_use++; 2115 mtx_unlock(&softc->ctl_lock); 2116 ctl_isc_announce_iid(port, iid); 2117 2118 return (iid); 2119} 2120 2121static int 2122ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2123{ 2124 int len; 2125 2126 switch (port->port_type) { 2127 case CTL_PORT_FC: 2128 { 2129 struct scsi_transportid_fcp *id = 2130 (struct scsi_transportid_fcp *)buf; 2131 if (port->wwpn_iid[iid].wwpn == 0) 2132 return (0); 2133 memset(id, 0, sizeof(*id)); 2134 id->format_protocol = SCSI_PROTO_FC; 2135 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2136 return (sizeof(*id)); 2137 } 2138 case CTL_PORT_ISCSI: 2139 { 2140 struct scsi_transportid_iscsi_port *id = 2141 (struct scsi_transportid_iscsi_port *)buf; 2142 if (port->wwpn_iid[iid].name == NULL) 2143 return (0); 2144 memset(id, 0, 256); 2145 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2146 SCSI_PROTO_ISCSI; 2147 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2148 len = roundup2(min(len, 252), 4); 2149 scsi_ulto2b(len, id->additional_length); 2150 return (sizeof(*id) + len); 2151 } 2152 case CTL_PORT_SAS: 2153 { 2154 struct scsi_transportid_sas *id = 2155 (struct scsi_transportid_sas *)buf; 2156 if (port->wwpn_iid[iid].wwpn == 0) 2157 return (0); 2158 memset(id, 0, sizeof(*id)); 2159 id->format_protocol = SCSI_PROTO_SAS; 2160 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2161 return (sizeof(*id)); 2162 } 2163 default: 2164 { 2165 struct scsi_transportid_spi *id = 2166 (struct scsi_transportid_spi *)buf; 2167 memset(id, 0, sizeof(*id)); 2168 id->format_protocol = SCSI_PROTO_SPI; 2169 scsi_ulto2b(iid, id->scsi_addr); 2170 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2171 return (sizeof(*id)); 2172 } 2173 } 2174} 2175 2176/* 2177 * Serialize a command that went down the "wrong" side, and so was sent to 2178 * this controller for execution. The logic is a little different than the 2179 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2180 * sent back to the other side, but in the success case, we execute the 2181 * command on this side (XFER mode) or tell the other side to execute it 2182 * (SER_ONLY mode). 2183 */ 2184static int 2185ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2186{ 2187 struct ctl_softc *softc = control_softc; 2188 union ctl_ha_msg msg_info; 2189 struct ctl_port *port; 2190 struct ctl_lun *lun; 2191 const struct ctl_cmd_entry *entry; 2192 int retval = 0; 2193 uint32_t targ_lun; 2194 2195 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2196 mtx_lock(&softc->ctl_lock); 2197 2198 /* Make sure that we know about this port. */ 2199 port = ctl_io_port(&ctsio->io_hdr); 2200 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2201 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2202 /*retry_count*/ 1); 2203 goto badjuju; 2204 } 2205 2206 /* Make sure that we know about this LUN. */ 2207 if ((targ_lun < CTL_MAX_LUNS) && 2208 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 2209 mtx_lock(&lun->lun_lock); 2210 mtx_unlock(&softc->ctl_lock); 2211 /* 2212 * If the LUN is invalid, pretend that it doesn't exist. 2213 * It will go away as soon as all pending I/O has been 2214 * completed. 2215 */ 2216 if (lun->flags & CTL_LUN_DISABLED) { 2217 mtx_unlock(&lun->lun_lock); 2218 lun = NULL; 2219 } 2220 } else { 2221 mtx_unlock(&softc->ctl_lock); 2222 lun = NULL; 2223 } 2224 if (lun == NULL) { 2225 /* 2226 * The other node would not send this request to us unless 2227 * received announce that we are primary node for this LUN. 2228 * If this LUN does not exist now, it is probably result of 2229 * a race, so respond to initiator in the most opaque way. 2230 */ 2231 ctl_set_busy(ctsio); 2232 goto badjuju; 2233 } 2234 2235 entry = ctl_get_cmd_entry(ctsio, NULL); 2236 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2237 mtx_unlock(&lun->lun_lock); 2238 goto badjuju; 2239 } 2240 2241 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2242 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2243 2244 /* 2245 * Every I/O goes into the OOA queue for a 2246 * particular LUN, and stays there until completion. 2247 */ 2248#ifdef CTL_TIME_IO 2249 if (TAILQ_EMPTY(&lun->ooa_queue)) 2250 lun->idle_time += getsbinuptime() - lun->last_busy; 2251#endif 2252 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2253 2254 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2255 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2256 ooa_links))) { 2257 case CTL_ACTION_BLOCK: 2258 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2259 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2260 blocked_links); 2261 mtx_unlock(&lun->lun_lock); 2262 break; 2263 case CTL_ACTION_PASS: 2264 case CTL_ACTION_SKIP: 2265 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2266 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2267 ctl_enqueue_rtr((union ctl_io *)ctsio); 2268 mtx_unlock(&lun->lun_lock); 2269 } else { 2270 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2271 mtx_unlock(&lun->lun_lock); 2272 2273 /* send msg back to other side */ 2274 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2275 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2276 msg_info.hdr.msg_type = CTL_MSG_R2R; 2277 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2278 sizeof(msg_info.hdr), M_WAITOK); 2279 } 2280 break; 2281 case CTL_ACTION_OVERLAP: 2282 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2283 mtx_unlock(&lun->lun_lock); 2284 ctl_set_overlapped_cmd(ctsio); 2285 goto badjuju; 2286 case CTL_ACTION_OVERLAP_TAG: 2287 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2288 mtx_unlock(&lun->lun_lock); 2289 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2290 goto badjuju; 2291 case CTL_ACTION_ERROR: 2292 default: 2293 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2294 mtx_unlock(&lun->lun_lock); 2295 2296 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2297 /*retry_count*/ 0); 2298badjuju: 2299 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2300 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2301 msg_info.hdr.serializing_sc = NULL; 2302 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2303 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2304 sizeof(msg_info.scsi), M_WAITOK); 2305 retval = 1; 2306 break; 2307 } 2308 return (retval); 2309} 2310 2311/* 2312 * Returns 0 for success, errno for failure. 2313 */ 2314static void 2315ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2316 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2317{ 2318 union ctl_io *io; 2319 2320 mtx_lock(&lun->lun_lock); 2321 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2322 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2323 ooa_links)) { 2324 struct ctl_ooa_entry *entry; 2325 2326 /* 2327 * If we've got more than we can fit, just count the 2328 * remaining entries. 2329 */ 2330 if (*cur_fill_num >= ooa_hdr->alloc_num) 2331 continue; 2332 2333 entry = &kern_entries[*cur_fill_num]; 2334 2335 entry->tag_num = io->scsiio.tag_num; 2336 entry->lun_num = lun->lun; 2337#ifdef CTL_TIME_IO 2338 entry->start_bt = io->io_hdr.start_bt; 2339#endif 2340 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2341 entry->cdb_len = io->scsiio.cdb_len; 2342 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2343 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2344 2345 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2346 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2347 2348 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2349 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2350 2351 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2352 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2353 2354 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2355 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2356 } 2357 mtx_unlock(&lun->lun_lock); 2358} 2359 2360static void * 2361ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2362 size_t error_str_len) 2363{ 2364 void *kptr; 2365 2366 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2367 2368 if (copyin(user_addr, kptr, len) != 0) { 2369 snprintf(error_str, error_str_len, "Error copying %d bytes " 2370 "from user address %p to kernel address %p", len, 2371 user_addr, kptr); 2372 free(kptr, M_CTL); 2373 return (NULL); 2374 } 2375 2376 return (kptr); 2377} 2378 2379static void 2380ctl_free_args(int num_args, struct ctl_be_arg *args) 2381{ 2382 int i; 2383 2384 if (args == NULL) 2385 return; 2386 2387 for (i = 0; i < num_args; i++) { 2388 free(args[i].kname, M_CTL); 2389 free(args[i].kvalue, M_CTL); 2390 } 2391 2392 free(args, M_CTL); 2393} 2394 2395static struct ctl_be_arg * 2396ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2397 char *error_str, size_t error_str_len) 2398{ 2399 struct ctl_be_arg *args; 2400 int i; 2401 2402 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2403 error_str, error_str_len); 2404 2405 if (args == NULL) 2406 goto bailout; 2407 2408 for (i = 0; i < num_args; i++) { 2409 args[i].kname = NULL; 2410 args[i].kvalue = NULL; 2411 } 2412 2413 for (i = 0; i < num_args; i++) { 2414 uint8_t *tmpptr; 2415 2416 args[i].kname = ctl_copyin_alloc(args[i].name, 2417 args[i].namelen, error_str, error_str_len); 2418 if (args[i].kname == NULL) 2419 goto bailout; 2420 2421 if (args[i].kname[args[i].namelen - 1] != '\0') { 2422 snprintf(error_str, error_str_len, "Argument %d " 2423 "name is not NUL-terminated", i); 2424 goto bailout; 2425 } 2426 2427 if (args[i].flags & CTL_BEARG_RD) { 2428 tmpptr = ctl_copyin_alloc(args[i].value, 2429 args[i].vallen, error_str, error_str_len); 2430 if (tmpptr == NULL) 2431 goto bailout; 2432 if ((args[i].flags & CTL_BEARG_ASCII) 2433 && (tmpptr[args[i].vallen - 1] != '\0')) { 2434 snprintf(error_str, error_str_len, "Argument " 2435 "%d value is not NUL-terminated", i); 2436 goto bailout; 2437 } 2438 args[i].kvalue = tmpptr; 2439 } else { 2440 args[i].kvalue = malloc(args[i].vallen, 2441 M_CTL, M_WAITOK | M_ZERO); 2442 } 2443 } 2444 2445 return (args); 2446bailout: 2447 2448 ctl_free_args(num_args, args); 2449 2450 return (NULL); 2451} 2452 2453static void 2454ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2455{ 2456 int i; 2457 2458 for (i = 0; i < num_args; i++) { 2459 if (args[i].flags & CTL_BEARG_WR) 2460 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2461 } 2462} 2463 2464/* 2465 * Escape characters that are illegal or not recommended in XML. 2466 */ 2467int 2468ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2469{ 2470 char *end = str + size; 2471 int retval; 2472 2473 retval = 0; 2474 2475 for (; *str && str < end; str++) { 2476 switch (*str) { 2477 case '&': 2478 retval = sbuf_printf(sb, "&"); 2479 break; 2480 case '>': 2481 retval = sbuf_printf(sb, ">"); 2482 break; 2483 case '<': 2484 retval = sbuf_printf(sb, "<"); 2485 break; 2486 default: 2487 retval = sbuf_putc(sb, *str); 2488 break; 2489 } 2490 2491 if (retval != 0) 2492 break; 2493 2494 } 2495 2496 return (retval); 2497} 2498 2499static void 2500ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2501{ 2502 struct scsi_vpd_id_descriptor *desc; 2503 int i; 2504 2505 if (id == NULL || id->len < 4) 2506 return; 2507 desc = (struct scsi_vpd_id_descriptor *)id->data; 2508 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2509 case SVPD_ID_TYPE_T10: 2510 sbuf_printf(sb, "t10."); 2511 break; 2512 case SVPD_ID_TYPE_EUI64: 2513 sbuf_printf(sb, "eui."); 2514 break; 2515 case SVPD_ID_TYPE_NAA: 2516 sbuf_printf(sb, "naa."); 2517 break; 2518 case SVPD_ID_TYPE_SCSI_NAME: 2519 break; 2520 } 2521 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2522 case SVPD_ID_CODESET_BINARY: 2523 for (i = 0; i < desc->length; i++) 2524 sbuf_printf(sb, "%02x", desc->identifier[i]); 2525 break; 2526 case SVPD_ID_CODESET_ASCII: 2527 sbuf_printf(sb, "%.*s", (int)desc->length, 2528 (char *)desc->identifier); 2529 break; 2530 case SVPD_ID_CODESET_UTF8: 2531 sbuf_printf(sb, "%s", (char *)desc->identifier); 2532 break; 2533 } 2534} 2535 2536static int 2537ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2538 struct thread *td) 2539{ 2540 struct ctl_softc *softc = dev->si_drv1; 2541 struct ctl_lun *lun; 2542 int retval; 2543 2544 retval = 0; 2545 2546 switch (cmd) { 2547 case CTL_IO: 2548 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2549 break; 2550 case CTL_ENABLE_PORT: 2551 case CTL_DISABLE_PORT: 2552 case CTL_SET_PORT_WWNS: { 2553 struct ctl_port *port; 2554 struct ctl_port_entry *entry; 2555 2556 entry = (struct ctl_port_entry *)addr; 2557 2558 mtx_lock(&softc->ctl_lock); 2559 STAILQ_FOREACH(port, &softc->port_list, links) { 2560 int action, done; 2561 2562 if (port->targ_port < softc->port_min || 2563 port->targ_port >= softc->port_max) 2564 continue; 2565 2566 action = 0; 2567 done = 0; 2568 if ((entry->port_type == CTL_PORT_NONE) 2569 && (entry->targ_port == port->targ_port)) { 2570 /* 2571 * If the user only wants to enable or 2572 * disable or set WWNs on a specific port, 2573 * do the operation and we're done. 2574 */ 2575 action = 1; 2576 done = 1; 2577 } else if (entry->port_type & port->port_type) { 2578 /* 2579 * Compare the user's type mask with the 2580 * particular frontend type to see if we 2581 * have a match. 2582 */ 2583 action = 1; 2584 done = 0; 2585 2586 /* 2587 * Make sure the user isn't trying to set 2588 * WWNs on multiple ports at the same time. 2589 */ 2590 if (cmd == CTL_SET_PORT_WWNS) { 2591 printf("%s: Can't set WWNs on " 2592 "multiple ports\n", __func__); 2593 retval = EINVAL; 2594 break; 2595 } 2596 } 2597 if (action == 0) 2598 continue; 2599 2600 /* 2601 * XXX KDM we have to drop the lock here, because 2602 * the online/offline operations can potentially 2603 * block. We need to reference count the frontends 2604 * so they can't go away, 2605 */ 2606 if (cmd == CTL_ENABLE_PORT) { 2607 mtx_unlock(&softc->ctl_lock); 2608 ctl_port_online(port); 2609 mtx_lock(&softc->ctl_lock); 2610 } else if (cmd == CTL_DISABLE_PORT) { 2611 mtx_unlock(&softc->ctl_lock); 2612 ctl_port_offline(port); 2613 mtx_lock(&softc->ctl_lock); 2614 } else if (cmd == CTL_SET_PORT_WWNS) { 2615 ctl_port_set_wwns(port, 2616 (entry->flags & CTL_PORT_WWNN_VALID) ? 2617 1 : 0, entry->wwnn, 2618 (entry->flags & CTL_PORT_WWPN_VALID) ? 2619 1 : 0, entry->wwpn); 2620 } 2621 if (done != 0) 2622 break; 2623 } 2624 mtx_unlock(&softc->ctl_lock); 2625 break; 2626 } 2627 case CTL_GET_OOA: { 2628 struct ctl_ooa *ooa_hdr; 2629 struct ctl_ooa_entry *entries; 2630 uint32_t cur_fill_num; 2631 2632 ooa_hdr = (struct ctl_ooa *)addr; 2633 2634 if ((ooa_hdr->alloc_len == 0) 2635 || (ooa_hdr->alloc_num == 0)) { 2636 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2637 "must be non-zero\n", __func__, 2638 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2639 retval = EINVAL; 2640 break; 2641 } 2642 2643 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2644 sizeof(struct ctl_ooa_entry))) { 2645 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2646 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2647 __func__, ooa_hdr->alloc_len, 2648 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2649 retval = EINVAL; 2650 break; 2651 } 2652 2653 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2654 if (entries == NULL) { 2655 printf("%s: could not allocate %d bytes for OOA " 2656 "dump\n", __func__, ooa_hdr->alloc_len); 2657 retval = ENOMEM; 2658 break; 2659 } 2660 2661 mtx_lock(&softc->ctl_lock); 2662 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2663 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2664 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2665 mtx_unlock(&softc->ctl_lock); 2666 free(entries, M_CTL); 2667 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2668 __func__, (uintmax_t)ooa_hdr->lun_num); 2669 retval = EINVAL; 2670 break; 2671 } 2672 2673 cur_fill_num = 0; 2674 2675 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2676 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2677 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2678 ooa_hdr, entries); 2679 } 2680 } else { 2681 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2682 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2683 entries); 2684 } 2685 mtx_unlock(&softc->ctl_lock); 2686 2687 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2688 ooa_hdr->fill_len = ooa_hdr->fill_num * 2689 sizeof(struct ctl_ooa_entry); 2690 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2691 if (retval != 0) { 2692 printf("%s: error copying out %d bytes for OOA dump\n", 2693 __func__, ooa_hdr->fill_len); 2694 } 2695 2696 getbinuptime(&ooa_hdr->cur_bt); 2697 2698 if (cur_fill_num > ooa_hdr->alloc_num) { 2699 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2700 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2701 } else { 2702 ooa_hdr->dropped_num = 0; 2703 ooa_hdr->status = CTL_OOA_OK; 2704 } 2705 2706 free(entries, M_CTL); 2707 break; 2708 } 2709 case CTL_DELAY_IO: { 2710 struct ctl_io_delay_info *delay_info; 2711 2712 delay_info = (struct ctl_io_delay_info *)addr; 2713 2714#ifdef CTL_IO_DELAY 2715 mtx_lock(&softc->ctl_lock); 2716 2717 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2718 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2719 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2720 } else { 2721 lun = softc->ctl_luns[delay_info->lun_id]; 2722 mtx_lock(&lun->lun_lock); 2723 2724 delay_info->status = CTL_DELAY_STATUS_OK; 2725 2726 switch (delay_info->delay_type) { 2727 case CTL_DELAY_TYPE_CONT: 2728 break; 2729 case CTL_DELAY_TYPE_ONESHOT: 2730 break; 2731 default: 2732 delay_info->status = 2733 CTL_DELAY_STATUS_INVALID_TYPE; 2734 break; 2735 } 2736 2737 switch (delay_info->delay_loc) { 2738 case CTL_DELAY_LOC_DATAMOVE: 2739 lun->delay_info.datamove_type = 2740 delay_info->delay_type; 2741 lun->delay_info.datamove_delay = 2742 delay_info->delay_secs; 2743 break; 2744 case CTL_DELAY_LOC_DONE: 2745 lun->delay_info.done_type = 2746 delay_info->delay_type; 2747 lun->delay_info.done_delay = 2748 delay_info->delay_secs; 2749 break; 2750 default: 2751 delay_info->status = 2752 CTL_DELAY_STATUS_INVALID_LOC; 2753 break; 2754 } 2755 mtx_unlock(&lun->lun_lock); 2756 } 2757 2758 mtx_unlock(&softc->ctl_lock); 2759#else 2760 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2761#endif /* CTL_IO_DELAY */ 2762 break; 2763 } 2764 case CTL_GETSTATS: { 2765 struct ctl_stats *stats; 2766 int i; 2767 2768 stats = (struct ctl_stats *)addr; 2769 2770 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2771 stats->alloc_len) { 2772 stats->status = CTL_SS_NEED_MORE_SPACE; 2773 stats->num_luns = softc->num_luns; 2774 break; 2775 } 2776 /* 2777 * XXX KDM no locking here. If the LUN list changes, 2778 * things can blow up. 2779 */ 2780 i = 0; 2781 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2782 retval = copyout(&lun->stats, &stats->lun_stats[i++], 2783 sizeof(lun->stats)); 2784 if (retval != 0) 2785 break; 2786 } 2787 stats->num_luns = softc->num_luns; 2788 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2789 softc->num_luns; 2790 stats->status = CTL_SS_OK; 2791#ifdef CTL_TIME_IO 2792 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2793#else 2794 stats->flags = CTL_STATS_FLAG_NONE; 2795#endif 2796 getnanouptime(&stats->timestamp); 2797 break; 2798 } 2799 case CTL_ERROR_INJECT: { 2800 struct ctl_error_desc *err_desc, *new_err_desc; 2801 2802 err_desc = (struct ctl_error_desc *)addr; 2803 2804 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2805 M_WAITOK | M_ZERO); 2806 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2807 2808 mtx_lock(&softc->ctl_lock); 2809 lun = softc->ctl_luns[err_desc->lun_id]; 2810 if (lun == NULL) { 2811 mtx_unlock(&softc->ctl_lock); 2812 free(new_err_desc, M_CTL); 2813 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2814 __func__, (uintmax_t)err_desc->lun_id); 2815 retval = EINVAL; 2816 break; 2817 } 2818 mtx_lock(&lun->lun_lock); 2819 mtx_unlock(&softc->ctl_lock); 2820 2821 /* 2822 * We could do some checking here to verify the validity 2823 * of the request, but given the complexity of error 2824 * injection requests, the checking logic would be fairly 2825 * complex. 2826 * 2827 * For now, if the request is invalid, it just won't get 2828 * executed and might get deleted. 2829 */ 2830 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2831 2832 /* 2833 * XXX KDM check to make sure the serial number is unique, 2834 * in case we somehow manage to wrap. That shouldn't 2835 * happen for a very long time, but it's the right thing to 2836 * do. 2837 */ 2838 new_err_desc->serial = lun->error_serial; 2839 err_desc->serial = lun->error_serial; 2840 lun->error_serial++; 2841 2842 mtx_unlock(&lun->lun_lock); 2843 break; 2844 } 2845 case CTL_ERROR_INJECT_DELETE: { 2846 struct ctl_error_desc *delete_desc, *desc, *desc2; 2847 int delete_done; 2848 2849 delete_desc = (struct ctl_error_desc *)addr; 2850 delete_done = 0; 2851 2852 mtx_lock(&softc->ctl_lock); 2853 lun = softc->ctl_luns[delete_desc->lun_id]; 2854 if (lun == NULL) { 2855 mtx_unlock(&softc->ctl_lock); 2856 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2857 __func__, (uintmax_t)delete_desc->lun_id); 2858 retval = EINVAL; 2859 break; 2860 } 2861 mtx_lock(&lun->lun_lock); 2862 mtx_unlock(&softc->ctl_lock); 2863 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2864 if (desc->serial != delete_desc->serial) 2865 continue; 2866 2867 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2868 links); 2869 free(desc, M_CTL); 2870 delete_done = 1; 2871 } 2872 mtx_unlock(&lun->lun_lock); 2873 if (delete_done == 0) { 2874 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2875 "error serial %ju on LUN %u\n", __func__, 2876 delete_desc->serial, delete_desc->lun_id); 2877 retval = EINVAL; 2878 break; 2879 } 2880 break; 2881 } 2882 case CTL_DUMP_STRUCTS: { 2883 int i, j, k; 2884 struct ctl_port *port; 2885 struct ctl_frontend *fe; 2886 2887 mtx_lock(&softc->ctl_lock); 2888 printf("CTL Persistent Reservation information start:\n"); 2889 for (i = 0; i < CTL_MAX_LUNS; i++) { 2890 lun = softc->ctl_luns[i]; 2891 2892 if ((lun == NULL) 2893 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2894 continue; 2895 2896 for (j = 0; j < CTL_MAX_PORTS; j++) { 2897 if (lun->pr_keys[j] == NULL) 2898 continue; 2899 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2900 if (lun->pr_keys[j][k] == 0) 2901 continue; 2902 printf(" LUN %d port %d iid %d key " 2903 "%#jx\n", i, j, k, 2904 (uintmax_t)lun->pr_keys[j][k]); 2905 } 2906 } 2907 } 2908 printf("CTL Persistent Reservation information end\n"); 2909 printf("CTL Ports:\n"); 2910 STAILQ_FOREACH(port, &softc->port_list, links) { 2911 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2912 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2913 port->frontend->name, port->port_type, 2914 port->physical_port, port->virtual_port, 2915 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2916 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2917 if (port->wwpn_iid[j].in_use == 0 && 2918 port->wwpn_iid[j].wwpn == 0 && 2919 port->wwpn_iid[j].name == NULL) 2920 continue; 2921 2922 printf(" iid %u use %d WWPN %#jx '%s'\n", 2923 j, port->wwpn_iid[j].in_use, 2924 (uintmax_t)port->wwpn_iid[j].wwpn, 2925 port->wwpn_iid[j].name); 2926 } 2927 } 2928 printf("CTL Port information end\n"); 2929 mtx_unlock(&softc->ctl_lock); 2930 /* 2931 * XXX KDM calling this without a lock. We'd likely want 2932 * to drop the lock before calling the frontend's dump 2933 * routine anyway. 2934 */ 2935 printf("CTL Frontends:\n"); 2936 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2937 printf(" Frontend '%s'\n", fe->name); 2938 if (fe->fe_dump != NULL) 2939 fe->fe_dump(); 2940 } 2941 printf("CTL Frontend information end\n"); 2942 break; 2943 } 2944 case CTL_LUN_REQ: { 2945 struct ctl_lun_req *lun_req; 2946 struct ctl_backend_driver *backend; 2947 2948 lun_req = (struct ctl_lun_req *)addr; 2949 2950 backend = ctl_backend_find(lun_req->backend); 2951 if (backend == NULL) { 2952 lun_req->status = CTL_LUN_ERROR; 2953 snprintf(lun_req->error_str, 2954 sizeof(lun_req->error_str), 2955 "Backend \"%s\" not found.", 2956 lun_req->backend); 2957 break; 2958 } 2959 if (lun_req->num_be_args > 0) { 2960 lun_req->kern_be_args = ctl_copyin_args( 2961 lun_req->num_be_args, 2962 lun_req->be_args, 2963 lun_req->error_str, 2964 sizeof(lun_req->error_str)); 2965 if (lun_req->kern_be_args == NULL) { 2966 lun_req->status = CTL_LUN_ERROR; 2967 break; 2968 } 2969 } 2970 2971 retval = backend->ioctl(dev, cmd, addr, flag, td); 2972 2973 if (lun_req->num_be_args > 0) { 2974 ctl_copyout_args(lun_req->num_be_args, 2975 lun_req->kern_be_args); 2976 ctl_free_args(lun_req->num_be_args, 2977 lun_req->kern_be_args); 2978 } 2979 break; 2980 } 2981 case CTL_LUN_LIST: { 2982 struct sbuf *sb; 2983 struct ctl_lun_list *list; 2984 struct ctl_option *opt; 2985 2986 list = (struct ctl_lun_list *)addr; 2987 2988 /* 2989 * Allocate a fixed length sbuf here, based on the length 2990 * of the user's buffer. We could allocate an auto-extending 2991 * buffer, and then tell the user how much larger our 2992 * amount of data is than his buffer, but that presents 2993 * some problems: 2994 * 2995 * 1. The sbuf(9) routines use a blocking malloc, and so 2996 * we can't hold a lock while calling them with an 2997 * auto-extending buffer. 2998 * 2999 * 2. There is not currently a LUN reference counting 3000 * mechanism, outside of outstanding transactions on 3001 * the LUN's OOA queue. So a LUN could go away on us 3002 * while we're getting the LUN number, backend-specific 3003 * information, etc. Thus, given the way things 3004 * currently work, we need to hold the CTL lock while 3005 * grabbing LUN information. 3006 * 3007 * So, from the user's standpoint, the best thing to do is 3008 * allocate what he thinks is a reasonable buffer length, 3009 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3010 * double the buffer length and try again. (And repeat 3011 * that until he succeeds.) 3012 */ 3013 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3014 if (sb == NULL) { 3015 list->status = CTL_LUN_LIST_ERROR; 3016 snprintf(list->error_str, sizeof(list->error_str), 3017 "Unable to allocate %d bytes for LUN list", 3018 list->alloc_len); 3019 break; 3020 } 3021 3022 sbuf_printf(sb, "<ctllunlist>\n"); 3023 3024 mtx_lock(&softc->ctl_lock); 3025 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3026 mtx_lock(&lun->lun_lock); 3027 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3028 (uintmax_t)lun->lun); 3029 3030 /* 3031 * Bail out as soon as we see that we've overfilled 3032 * the buffer. 3033 */ 3034 if (retval != 0) 3035 break; 3036 3037 retval = sbuf_printf(sb, "\t<backend_type>%s" 3038 "</backend_type>\n", 3039 (lun->backend == NULL) ? "none" : 3040 lun->backend->name); 3041 3042 if (retval != 0) 3043 break; 3044 3045 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3046 lun->be_lun->lun_type); 3047 3048 if (retval != 0) 3049 break; 3050 3051 if (lun->backend == NULL) { 3052 retval = sbuf_printf(sb, "</lun>\n"); 3053 if (retval != 0) 3054 break; 3055 continue; 3056 } 3057 3058 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3059 (lun->be_lun->maxlba > 0) ? 3060 lun->be_lun->maxlba + 1 : 0); 3061 3062 if (retval != 0) 3063 break; 3064 3065 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3066 lun->be_lun->blocksize); 3067 3068 if (retval != 0) 3069 break; 3070 3071 retval = sbuf_printf(sb, "\t<serial_number>"); 3072 3073 if (retval != 0) 3074 break; 3075 3076 retval = ctl_sbuf_printf_esc(sb, 3077 lun->be_lun->serial_num, 3078 sizeof(lun->be_lun->serial_num)); 3079 3080 if (retval != 0) 3081 break; 3082 3083 retval = sbuf_printf(sb, "</serial_number>\n"); 3084 3085 if (retval != 0) 3086 break; 3087 3088 retval = sbuf_printf(sb, "\t<device_id>"); 3089 3090 if (retval != 0) 3091 break; 3092 3093 retval = ctl_sbuf_printf_esc(sb, 3094 lun->be_lun->device_id, 3095 sizeof(lun->be_lun->device_id)); 3096 3097 if (retval != 0) 3098 break; 3099 3100 retval = sbuf_printf(sb, "</device_id>\n"); 3101 3102 if (retval != 0) 3103 break; 3104 3105 if (lun->backend->lun_info != NULL) { 3106 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3107 if (retval != 0) 3108 break; 3109 } 3110 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3111 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3112 opt->name, opt->value, opt->name); 3113 if (retval != 0) 3114 break; 3115 } 3116 3117 retval = sbuf_printf(sb, "</lun>\n"); 3118 3119 if (retval != 0) 3120 break; 3121 mtx_unlock(&lun->lun_lock); 3122 } 3123 if (lun != NULL) 3124 mtx_unlock(&lun->lun_lock); 3125 mtx_unlock(&softc->ctl_lock); 3126 3127 if ((retval != 0) 3128 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3129 retval = 0; 3130 sbuf_delete(sb); 3131 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3132 snprintf(list->error_str, sizeof(list->error_str), 3133 "Out of space, %d bytes is too small", 3134 list->alloc_len); 3135 break; 3136 } 3137 3138 sbuf_finish(sb); 3139 3140 retval = copyout(sbuf_data(sb), list->lun_xml, 3141 sbuf_len(sb) + 1); 3142 3143 list->fill_len = sbuf_len(sb) + 1; 3144 list->status = CTL_LUN_LIST_OK; 3145 sbuf_delete(sb); 3146 break; 3147 } 3148 case CTL_ISCSI: { 3149 struct ctl_iscsi *ci; 3150 struct ctl_frontend *fe; 3151 3152 ci = (struct ctl_iscsi *)addr; 3153 3154 fe = ctl_frontend_find("iscsi"); 3155 if (fe == NULL) { 3156 ci->status = CTL_ISCSI_ERROR; 3157 snprintf(ci->error_str, sizeof(ci->error_str), 3158 "Frontend \"iscsi\" not found."); 3159 break; 3160 } 3161 3162 retval = fe->ioctl(dev, cmd, addr, flag, td); 3163 break; 3164 } 3165 case CTL_PORT_REQ: { 3166 struct ctl_req *req; 3167 struct ctl_frontend *fe; 3168 3169 req = (struct ctl_req *)addr; 3170 3171 fe = ctl_frontend_find(req->driver); 3172 if (fe == NULL) { 3173 req->status = CTL_LUN_ERROR; 3174 snprintf(req->error_str, sizeof(req->error_str), 3175 "Frontend \"%s\" not found.", req->driver); 3176 break; 3177 } 3178 if (req->num_args > 0) { 3179 req->kern_args = ctl_copyin_args(req->num_args, 3180 req->args, req->error_str, sizeof(req->error_str)); 3181 if (req->kern_args == NULL) { 3182 req->status = CTL_LUN_ERROR; 3183 break; 3184 } 3185 } 3186 3187 if (fe->ioctl) 3188 retval = fe->ioctl(dev, cmd, addr, flag, td); 3189 else 3190 retval = ENODEV; 3191 3192 if (req->num_args > 0) { 3193 ctl_copyout_args(req->num_args, req->kern_args); 3194 ctl_free_args(req->num_args, req->kern_args); 3195 } 3196 break; 3197 } 3198 case CTL_PORT_LIST: { 3199 struct sbuf *sb; 3200 struct ctl_port *port; 3201 struct ctl_lun_list *list; 3202 struct ctl_option *opt; 3203 int j; 3204 uint32_t plun; 3205 3206 list = (struct ctl_lun_list *)addr; 3207 3208 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3209 if (sb == NULL) { 3210 list->status = CTL_LUN_LIST_ERROR; 3211 snprintf(list->error_str, sizeof(list->error_str), 3212 "Unable to allocate %d bytes for LUN list", 3213 list->alloc_len); 3214 break; 3215 } 3216 3217 sbuf_printf(sb, "<ctlportlist>\n"); 3218 3219 mtx_lock(&softc->ctl_lock); 3220 STAILQ_FOREACH(port, &softc->port_list, links) { 3221 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3222 (uintmax_t)port->targ_port); 3223 3224 /* 3225 * Bail out as soon as we see that we've overfilled 3226 * the buffer. 3227 */ 3228 if (retval != 0) 3229 break; 3230 3231 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3232 "</frontend_type>\n", port->frontend->name); 3233 if (retval != 0) 3234 break; 3235 3236 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3237 port->port_type); 3238 if (retval != 0) 3239 break; 3240 3241 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3242 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3243 if (retval != 0) 3244 break; 3245 3246 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3247 port->port_name); 3248 if (retval != 0) 3249 break; 3250 3251 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3252 port->physical_port); 3253 if (retval != 0) 3254 break; 3255 3256 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3257 port->virtual_port); 3258 if (retval != 0) 3259 break; 3260 3261 if (port->target_devid != NULL) { 3262 sbuf_printf(sb, "\t<target>"); 3263 ctl_id_sbuf(port->target_devid, sb); 3264 sbuf_printf(sb, "</target>\n"); 3265 } 3266 3267 if (port->port_devid != NULL) { 3268 sbuf_printf(sb, "\t<port>"); 3269 ctl_id_sbuf(port->port_devid, sb); 3270 sbuf_printf(sb, "</port>\n"); 3271 } 3272 3273 if (port->port_info != NULL) { 3274 retval = port->port_info(port->onoff_arg, sb); 3275 if (retval != 0) 3276 break; 3277 } 3278 STAILQ_FOREACH(opt, &port->options, links) { 3279 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3280 opt->name, opt->value, opt->name); 3281 if (retval != 0) 3282 break; 3283 } 3284 3285 if (port->lun_map != NULL) { 3286 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3287 for (j = 0; j < CTL_MAX_LUNS; j++) { 3288 plun = ctl_lun_map_from_port(port, j); 3289 if (plun >= CTL_MAX_LUNS) 3290 continue; 3291 sbuf_printf(sb, 3292 "\t<lun id=\"%u\">%u</lun>\n", 3293 j, plun); 3294 } 3295 } 3296 3297 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3298 if (port->wwpn_iid[j].in_use == 0 || 3299 (port->wwpn_iid[j].wwpn == 0 && 3300 port->wwpn_iid[j].name == NULL)) 3301 continue; 3302 3303 if (port->wwpn_iid[j].name != NULL) 3304 retval = sbuf_printf(sb, 3305 "\t<initiator id=\"%u\">%s</initiator>\n", 3306 j, port->wwpn_iid[j].name); 3307 else 3308 retval = sbuf_printf(sb, 3309 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3310 j, port->wwpn_iid[j].wwpn); 3311 if (retval != 0) 3312 break; 3313 } 3314 if (retval != 0) 3315 break; 3316 3317 retval = sbuf_printf(sb, "</targ_port>\n"); 3318 if (retval != 0) 3319 break; 3320 } 3321 mtx_unlock(&softc->ctl_lock); 3322 3323 if ((retval != 0) 3324 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3325 retval = 0; 3326 sbuf_delete(sb); 3327 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3328 snprintf(list->error_str, sizeof(list->error_str), 3329 "Out of space, %d bytes is too small", 3330 list->alloc_len); 3331 break; 3332 } 3333 3334 sbuf_finish(sb); 3335 3336 retval = copyout(sbuf_data(sb), list->lun_xml, 3337 sbuf_len(sb) + 1); 3338 3339 list->fill_len = sbuf_len(sb) + 1; 3340 list->status = CTL_LUN_LIST_OK; 3341 sbuf_delete(sb); 3342 break; 3343 } 3344 case CTL_LUN_MAP: { 3345 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3346 struct ctl_port *port; 3347 3348 mtx_lock(&softc->ctl_lock); 3349 if (lm->port < softc->port_min || 3350 lm->port >= softc->port_max || 3351 (port = softc->ctl_ports[lm->port]) == NULL) { 3352 mtx_unlock(&softc->ctl_lock); 3353 return (ENXIO); 3354 } 3355 if (port->status & CTL_PORT_STATUS_ONLINE) { 3356 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3357 if (ctl_lun_map_to_port(port, lun->lun) >= 3358 CTL_MAX_LUNS) 3359 continue; 3360 mtx_lock(&lun->lun_lock); 3361 ctl_est_ua_port(lun, lm->port, -1, 3362 CTL_UA_LUN_CHANGE); 3363 mtx_unlock(&lun->lun_lock); 3364 } 3365 } 3366 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3367 if (lm->plun < CTL_MAX_LUNS) { 3368 if (lm->lun == UINT32_MAX) 3369 retval = ctl_lun_map_unset(port, lm->plun); 3370 else if (lm->lun < CTL_MAX_LUNS && 3371 softc->ctl_luns[lm->lun] != NULL) 3372 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3373 else 3374 return (ENXIO); 3375 } else if (lm->plun == UINT32_MAX) { 3376 if (lm->lun == UINT32_MAX) 3377 retval = ctl_lun_map_deinit(port); 3378 else 3379 retval = ctl_lun_map_init(port); 3380 } else 3381 return (ENXIO); 3382 if (port->status & CTL_PORT_STATUS_ONLINE) 3383 ctl_isc_announce_port(port); 3384 break; 3385 } 3386 default: { 3387 /* XXX KDM should we fix this? */ 3388#if 0 3389 struct ctl_backend_driver *backend; 3390 unsigned int type; 3391 int found; 3392 3393 found = 0; 3394 3395 /* 3396 * We encode the backend type as the ioctl type for backend 3397 * ioctls. So parse it out here, and then search for a 3398 * backend of this type. 3399 */ 3400 type = _IOC_TYPE(cmd); 3401 3402 STAILQ_FOREACH(backend, &softc->be_list, links) { 3403 if (backend->type == type) { 3404 found = 1; 3405 break; 3406 } 3407 } 3408 if (found == 0) { 3409 printf("ctl: unknown ioctl command %#lx or backend " 3410 "%d\n", cmd, type); 3411 retval = EINVAL; 3412 break; 3413 } 3414 retval = backend->ioctl(dev, cmd, addr, flag, td); 3415#endif 3416 retval = ENOTTY; 3417 break; 3418 } 3419 } 3420 return (retval); 3421} 3422 3423uint32_t 3424ctl_get_initindex(struct ctl_nexus *nexus) 3425{ 3426 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3427} 3428 3429int 3430ctl_lun_map_init(struct ctl_port *port) 3431{ 3432 struct ctl_softc *softc = port->ctl_softc; 3433 struct ctl_lun *lun; 3434 uint32_t i; 3435 3436 if (port->lun_map == NULL) 3437 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3438 M_CTL, M_NOWAIT); 3439 if (port->lun_map == NULL) 3440 return (ENOMEM); 3441 for (i = 0; i < CTL_MAX_LUNS; i++) 3442 port->lun_map[i] = UINT32_MAX; 3443 if (port->status & CTL_PORT_STATUS_ONLINE) { 3444 if (port->lun_disable != NULL) { 3445 STAILQ_FOREACH(lun, &softc->lun_list, links) 3446 port->lun_disable(port->targ_lun_arg, lun->lun); 3447 } 3448 ctl_isc_announce_port(port); 3449 } 3450 return (0); 3451} 3452 3453int 3454ctl_lun_map_deinit(struct ctl_port *port) 3455{ 3456 struct ctl_softc *softc = port->ctl_softc; 3457 struct ctl_lun *lun; 3458 3459 if (port->lun_map == NULL) 3460 return (0); 3461 free(port->lun_map, M_CTL); 3462 port->lun_map = NULL; 3463 if (port->status & CTL_PORT_STATUS_ONLINE) { 3464 if (port->lun_enable != NULL) { 3465 STAILQ_FOREACH(lun, &softc->lun_list, links) 3466 port->lun_enable(port->targ_lun_arg, lun->lun); 3467 } 3468 ctl_isc_announce_port(port); 3469 } 3470 return (0); 3471} 3472 3473int 3474ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3475{ 3476 int status; 3477 uint32_t old; 3478 3479 if (port->lun_map == NULL) { 3480 status = ctl_lun_map_init(port); 3481 if (status != 0) 3482 return (status); 3483 } 3484 old = port->lun_map[plun]; 3485 port->lun_map[plun] = glun; 3486 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3487 if (port->lun_enable != NULL) 3488 port->lun_enable(port->targ_lun_arg, plun); 3489 ctl_isc_announce_port(port); 3490 } 3491 return (0); 3492} 3493 3494int 3495ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3496{ 3497 uint32_t old; 3498 3499 if (port->lun_map == NULL) 3500 return (0); 3501 old = port->lun_map[plun]; 3502 port->lun_map[plun] = UINT32_MAX; 3503 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3504 if (port->lun_disable != NULL) 3505 port->lun_disable(port->targ_lun_arg, plun); 3506 ctl_isc_announce_port(port); 3507 } 3508 return (0); 3509} 3510 3511uint32_t 3512ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3513{ 3514 3515 if (port == NULL) 3516 return (UINT32_MAX); 3517 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3518 return (lun_id); 3519 return (port->lun_map[lun_id]); 3520} 3521 3522uint32_t 3523ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3524{ 3525 uint32_t i; 3526 3527 if (port == NULL) 3528 return (UINT32_MAX); 3529 if (port->lun_map == NULL) 3530 return (lun_id); 3531 for (i = 0; i < CTL_MAX_LUNS; i++) { 3532 if (port->lun_map[i] == lun_id) 3533 return (i); 3534 } 3535 return (UINT32_MAX); 3536} 3537 3538static struct ctl_port * 3539ctl_io_port(struct ctl_io_hdr *io_hdr) 3540{ 3541 3542 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3543} 3544 3545int 3546ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3547{ 3548 int i; 3549 3550 for (i = first; i < last; i++) { 3551 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3552 return (i); 3553 } 3554 return (-1); 3555} 3556 3557int 3558ctl_set_mask(uint32_t *mask, uint32_t bit) 3559{ 3560 uint32_t chunk, piece; 3561 3562 chunk = bit >> 5; 3563 piece = bit % (sizeof(uint32_t) * 8); 3564 3565 if ((mask[chunk] & (1 << piece)) != 0) 3566 return (-1); 3567 else 3568 mask[chunk] |= (1 << piece); 3569 3570 return (0); 3571} 3572 3573int 3574ctl_clear_mask(uint32_t *mask, uint32_t bit) 3575{ 3576 uint32_t chunk, piece; 3577 3578 chunk = bit >> 5; 3579 piece = bit % (sizeof(uint32_t) * 8); 3580 3581 if ((mask[chunk] & (1 << piece)) == 0) 3582 return (-1); 3583 else 3584 mask[chunk] &= ~(1 << piece); 3585 3586 return (0); 3587} 3588 3589int 3590ctl_is_set(uint32_t *mask, uint32_t bit) 3591{ 3592 uint32_t chunk, piece; 3593 3594 chunk = bit >> 5; 3595 piece = bit % (sizeof(uint32_t) * 8); 3596 3597 if ((mask[chunk] & (1 << piece)) == 0) 3598 return (0); 3599 else 3600 return (1); 3601} 3602 3603static uint64_t 3604ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3605{ 3606 uint64_t *t; 3607 3608 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3609 if (t == NULL) 3610 return (0); 3611 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3612} 3613 3614static void 3615ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3616{ 3617 uint64_t *t; 3618 3619 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3620 if (t == NULL) 3621 return; 3622 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3623} 3624 3625static void 3626ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3627{ 3628 uint64_t *p; 3629 u_int i; 3630 3631 i = residx/CTL_MAX_INIT_PER_PORT; 3632 if (lun->pr_keys[i] != NULL) 3633 return; 3634 mtx_unlock(&lun->lun_lock); 3635 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3636 M_WAITOK | M_ZERO); 3637 mtx_lock(&lun->lun_lock); 3638 if (lun->pr_keys[i] == NULL) 3639 lun->pr_keys[i] = p; 3640 else 3641 free(p, M_CTL); 3642} 3643 3644static void 3645ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3646{ 3647 uint64_t *t; 3648 3649 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3650 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3651 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3652} 3653 3654/* 3655 * ctl_softc, pool_name, total_ctl_io are passed in. 3656 * npool is passed out. 3657 */ 3658int 3659ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3660 uint32_t total_ctl_io, void **npool) 3661{ 3662#ifdef IO_POOLS 3663 struct ctl_io_pool *pool; 3664 3665 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3666 M_NOWAIT | M_ZERO); 3667 if (pool == NULL) 3668 return (ENOMEM); 3669 3670 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3671 pool->ctl_softc = ctl_softc; 3672 pool->zone = uma_zsecond_create(pool->name, NULL, 3673 NULL, NULL, NULL, ctl_softc->io_zone); 3674 /* uma_prealloc(pool->zone, total_ctl_io); */ 3675 3676 *npool = pool; 3677#else 3678 *npool = ctl_softc->io_zone; 3679#endif 3680 return (0); 3681} 3682 3683void 3684ctl_pool_free(struct ctl_io_pool *pool) 3685{ 3686 3687 if (pool == NULL) 3688 return; 3689 3690#ifdef IO_POOLS 3691 uma_zdestroy(pool->zone); 3692 free(pool, M_CTL); 3693#endif 3694} 3695 3696union ctl_io * 3697ctl_alloc_io(void *pool_ref) 3698{ 3699 union ctl_io *io; 3700#ifdef IO_POOLS 3701 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3702 3703 io = uma_zalloc(pool->zone, M_WAITOK); 3704#else 3705 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3706#endif 3707 if (io != NULL) 3708 io->io_hdr.pool = pool_ref; 3709 return (io); 3710} 3711 3712union ctl_io * 3713ctl_alloc_io_nowait(void *pool_ref) 3714{ 3715 union ctl_io *io; 3716#ifdef IO_POOLS 3717 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3718 3719 io = uma_zalloc(pool->zone, M_NOWAIT); 3720#else 3721 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3722#endif 3723 if (io != NULL) 3724 io->io_hdr.pool = pool_ref; 3725 return (io); 3726} 3727 3728void 3729ctl_free_io(union ctl_io *io) 3730{ 3731#ifdef IO_POOLS 3732 struct ctl_io_pool *pool; 3733#endif 3734 3735 if (io == NULL) 3736 return; 3737 3738#ifdef IO_POOLS 3739 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3740 uma_zfree(pool->zone, io); 3741#else 3742 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3743#endif 3744} 3745 3746void 3747ctl_zero_io(union ctl_io *io) 3748{ 3749 void *pool_ref; 3750 3751 if (io == NULL) 3752 return; 3753 3754 /* 3755 * May need to preserve linked list pointers at some point too. 3756 */ 3757 pool_ref = io->io_hdr.pool; 3758 memset(io, 0, sizeof(*io)); 3759 io->io_hdr.pool = pool_ref; 3760} 3761 3762int 3763ctl_expand_number(const char *buf, uint64_t *num) 3764{ 3765 char *endptr; 3766 uint64_t number; 3767 unsigned shift; 3768 3769 number = strtoq(buf, &endptr, 0); 3770 3771 switch (tolower((unsigned char)*endptr)) { 3772 case 'e': 3773 shift = 60; 3774 break; 3775 case 'p': 3776 shift = 50; 3777 break; 3778 case 't': 3779 shift = 40; 3780 break; 3781 case 'g': 3782 shift = 30; 3783 break; 3784 case 'm': 3785 shift = 20; 3786 break; 3787 case 'k': 3788 shift = 10; 3789 break; 3790 case 'b': 3791 case '\0': /* No unit. */ 3792 *num = number; 3793 return (0); 3794 default: 3795 /* Unrecognized unit. */ 3796 return (-1); 3797 } 3798 3799 if ((number << shift) >> shift != number) { 3800 /* Overflow */ 3801 return (-1); 3802 } 3803 *num = number << shift; 3804 return (0); 3805} 3806 3807 3808/* 3809 * This routine could be used in the future to load default and/or saved 3810 * mode page parameters for a particuar lun. 3811 */ 3812static int 3813ctl_init_page_index(struct ctl_lun *lun) 3814{ 3815 int i; 3816 struct ctl_page_index *page_index; 3817 const char *value; 3818 uint64_t ival; 3819 3820 memcpy(&lun->mode_pages.index, page_index_template, 3821 sizeof(page_index_template)); 3822 3823 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3824 3825 page_index = &lun->mode_pages.index[i]; 3826 if (lun->be_lun->lun_type == T_DIRECT && 3827 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3828 continue; 3829 if (lun->be_lun->lun_type == T_PROCESSOR && 3830 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3831 continue; 3832 if (lun->be_lun->lun_type == T_CDROM && 3833 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3834 continue; 3835 3836 switch (page_index->page_code & SMPH_PC_MASK) { 3837 case SMS_RW_ERROR_RECOVERY_PAGE: { 3838 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3839 panic("subpage is incorrect!"); 3840 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3841 &rw_er_page_default, 3842 sizeof(rw_er_page_default)); 3843 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3844 &rw_er_page_changeable, 3845 sizeof(rw_er_page_changeable)); 3846 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3847 &rw_er_page_default, 3848 sizeof(rw_er_page_default)); 3849 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3850 &rw_er_page_default, 3851 sizeof(rw_er_page_default)); 3852 page_index->page_data = 3853 (uint8_t *)lun->mode_pages.rw_er_page; 3854 break; 3855 } 3856 case SMS_FORMAT_DEVICE_PAGE: { 3857 struct scsi_format_page *format_page; 3858 3859 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3860 panic("subpage is incorrect!"); 3861 3862 /* 3863 * Sectors per track are set above. Bytes per 3864 * sector need to be set here on a per-LUN basis. 3865 */ 3866 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3867 &format_page_default, 3868 sizeof(format_page_default)); 3869 memcpy(&lun->mode_pages.format_page[ 3870 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3871 sizeof(format_page_changeable)); 3872 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3873 &format_page_default, 3874 sizeof(format_page_default)); 3875 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3876 &format_page_default, 3877 sizeof(format_page_default)); 3878 3879 format_page = &lun->mode_pages.format_page[ 3880 CTL_PAGE_CURRENT]; 3881 scsi_ulto2b(lun->be_lun->blocksize, 3882 format_page->bytes_per_sector); 3883 3884 format_page = &lun->mode_pages.format_page[ 3885 CTL_PAGE_DEFAULT]; 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_SAVED]; 3891 scsi_ulto2b(lun->be_lun->blocksize, 3892 format_page->bytes_per_sector); 3893 3894 page_index->page_data = 3895 (uint8_t *)lun->mode_pages.format_page; 3896 break; 3897 } 3898 case SMS_RIGID_DISK_PAGE: { 3899 struct scsi_rigid_disk_page *rigid_disk_page; 3900 uint32_t sectors_per_cylinder; 3901 uint64_t cylinders; 3902#ifndef __XSCALE__ 3903 int shift; 3904#endif /* !__XSCALE__ */ 3905 3906 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3907 panic("invalid subpage value %d", 3908 page_index->subpage); 3909 3910 /* 3911 * Rotation rate and sectors per track are set 3912 * above. We calculate the cylinders here based on 3913 * capacity. Due to the number of heads and 3914 * sectors per track we're using, smaller arrays 3915 * may turn out to have 0 cylinders. Linux and 3916 * FreeBSD don't pay attention to these mode pages 3917 * to figure out capacity, but Solaris does. It 3918 * seems to deal with 0 cylinders just fine, and 3919 * works out a fake geometry based on the capacity. 3920 */ 3921 memcpy(&lun->mode_pages.rigid_disk_page[ 3922 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3923 sizeof(rigid_disk_page_default)); 3924 memcpy(&lun->mode_pages.rigid_disk_page[ 3925 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3926 sizeof(rigid_disk_page_changeable)); 3927 3928 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3929 CTL_DEFAULT_HEADS; 3930 3931 /* 3932 * The divide method here will be more accurate, 3933 * probably, but results in floating point being 3934 * used in the kernel on i386 (__udivdi3()). On the 3935 * XScale, though, __udivdi3() is implemented in 3936 * software. 3937 * 3938 * The shift method for cylinder calculation is 3939 * accurate if sectors_per_cylinder is a power of 3940 * 2. Otherwise it might be slightly off -- you 3941 * might have a bit of a truncation problem. 3942 */ 3943#ifdef __XSCALE__ 3944 cylinders = (lun->be_lun->maxlba + 1) / 3945 sectors_per_cylinder; 3946#else 3947 for (shift = 31; shift > 0; shift--) { 3948 if (sectors_per_cylinder & (1 << shift)) 3949 break; 3950 } 3951 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3952#endif 3953 3954 /* 3955 * We've basically got 3 bytes, or 24 bits for the 3956 * cylinder size in the mode page. If we're over, 3957 * just round down to 2^24. 3958 */ 3959 if (cylinders > 0xffffff) 3960 cylinders = 0xffffff; 3961 3962 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3963 CTL_PAGE_DEFAULT]; 3964 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3965 3966 if ((value = ctl_get_opt(&lun->be_lun->options, 3967 "rpm")) != NULL) { 3968 scsi_ulto2b(strtol(value, NULL, 0), 3969 rigid_disk_page->rotation_rate); 3970 } 3971 3972 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3973 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3974 sizeof(rigid_disk_page_default)); 3975 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3976 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3977 sizeof(rigid_disk_page_default)); 3978 3979 page_index->page_data = 3980 (uint8_t *)lun->mode_pages.rigid_disk_page; 3981 break; 3982 } 3983 case SMS_CACHING_PAGE: { 3984 struct scsi_caching_page *caching_page; 3985 3986 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3987 panic("invalid subpage value %d", 3988 page_index->subpage); 3989 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3990 &caching_page_default, 3991 sizeof(caching_page_default)); 3992 memcpy(&lun->mode_pages.caching_page[ 3993 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3994 sizeof(caching_page_changeable)); 3995 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3996 &caching_page_default, 3997 sizeof(caching_page_default)); 3998 caching_page = &lun->mode_pages.caching_page[ 3999 CTL_PAGE_SAVED]; 4000 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4001 if (value != NULL && strcmp(value, "off") == 0) 4002 caching_page->flags1 &= ~SCP_WCE; 4003 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4004 if (value != NULL && strcmp(value, "off") == 0) 4005 caching_page->flags1 |= SCP_RCD; 4006 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4007 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4008 sizeof(caching_page_default)); 4009 page_index->page_data = 4010 (uint8_t *)lun->mode_pages.caching_page; 4011 break; 4012 } 4013 case SMS_CONTROL_MODE_PAGE: { 4014 switch (page_index->subpage) { 4015 case SMS_SUBPAGE_PAGE_0: { 4016 struct scsi_control_page *control_page; 4017 4018 memcpy(&lun->mode_pages.control_page[ 4019 CTL_PAGE_DEFAULT], 4020 &control_page_default, 4021 sizeof(control_page_default)); 4022 memcpy(&lun->mode_pages.control_page[ 4023 CTL_PAGE_CHANGEABLE], 4024 &control_page_changeable, 4025 sizeof(control_page_changeable)); 4026 memcpy(&lun->mode_pages.control_page[ 4027 CTL_PAGE_SAVED], 4028 &control_page_default, 4029 sizeof(control_page_default)); 4030 control_page = &lun->mode_pages.control_page[ 4031 CTL_PAGE_SAVED]; 4032 value = ctl_get_opt(&lun->be_lun->options, 4033 "reordering"); 4034 if (value != NULL && 4035 strcmp(value, "unrestricted") == 0) { 4036 control_page->queue_flags &= 4037 ~SCP_QUEUE_ALG_MASK; 4038 control_page->queue_flags |= 4039 SCP_QUEUE_ALG_UNRESTRICTED; 4040 } 4041 memcpy(&lun->mode_pages.control_page[ 4042 CTL_PAGE_CURRENT], 4043 &lun->mode_pages.control_page[ 4044 CTL_PAGE_SAVED], 4045 sizeof(control_page_default)); 4046 page_index->page_data = 4047 (uint8_t *)lun->mode_pages.control_page; 4048 break; 4049 } 4050 case 0x01: 4051 memcpy(&lun->mode_pages.control_ext_page[ 4052 CTL_PAGE_DEFAULT], 4053 &control_ext_page_default, 4054 sizeof(control_ext_page_default)); 4055 memcpy(&lun->mode_pages.control_ext_page[ 4056 CTL_PAGE_CHANGEABLE], 4057 &control_ext_page_changeable, 4058 sizeof(control_ext_page_changeable)); 4059 memcpy(&lun->mode_pages.control_ext_page[ 4060 CTL_PAGE_SAVED], 4061 &control_ext_page_default, 4062 sizeof(control_ext_page_default)); 4063 memcpy(&lun->mode_pages.control_ext_page[ 4064 CTL_PAGE_CURRENT], 4065 &lun->mode_pages.control_ext_page[ 4066 CTL_PAGE_SAVED], 4067 sizeof(control_ext_page_default)); 4068 page_index->page_data = 4069 (uint8_t *)lun->mode_pages.control_ext_page; 4070 break; 4071 } 4072 break; 4073 } 4074 case SMS_INFO_EXCEPTIONS_PAGE: { 4075 switch (page_index->subpage) { 4076 case SMS_SUBPAGE_PAGE_0: 4077 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4078 &ie_page_default, 4079 sizeof(ie_page_default)); 4080 memcpy(&lun->mode_pages.ie_page[ 4081 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4082 sizeof(ie_page_changeable)); 4083 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4084 &ie_page_default, 4085 sizeof(ie_page_default)); 4086 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4087 &ie_page_default, 4088 sizeof(ie_page_default)); 4089 page_index->page_data = 4090 (uint8_t *)lun->mode_pages.ie_page; 4091 break; 4092 case 0x02: { 4093 struct ctl_logical_block_provisioning_page *page; 4094 4095 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4096 &lbp_page_default, 4097 sizeof(lbp_page_default)); 4098 memcpy(&lun->mode_pages.lbp_page[ 4099 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4100 sizeof(lbp_page_changeable)); 4101 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4102 &lbp_page_default, 4103 sizeof(lbp_page_default)); 4104 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4105 value = ctl_get_opt(&lun->be_lun->options, 4106 "avail-threshold"); 4107 if (value != NULL && 4108 ctl_expand_number(value, &ival) == 0) { 4109 page->descr[0].flags |= SLBPPD_ENABLED | 4110 SLBPPD_ARMING_DEC; 4111 if (lun->be_lun->blocksize) 4112 ival /= lun->be_lun->blocksize; 4113 else 4114 ival /= 512; 4115 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4116 page->descr[0].count); 4117 } 4118 value = ctl_get_opt(&lun->be_lun->options, 4119 "used-threshold"); 4120 if (value != NULL && 4121 ctl_expand_number(value, &ival) == 0) { 4122 page->descr[1].flags |= SLBPPD_ENABLED | 4123 SLBPPD_ARMING_INC; 4124 if (lun->be_lun->blocksize) 4125 ival /= lun->be_lun->blocksize; 4126 else 4127 ival /= 512; 4128 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4129 page->descr[1].count); 4130 } 4131 value = ctl_get_opt(&lun->be_lun->options, 4132 "pool-avail-threshold"); 4133 if (value != NULL && 4134 ctl_expand_number(value, &ival) == 0) { 4135 page->descr[2].flags |= SLBPPD_ENABLED | 4136 SLBPPD_ARMING_DEC; 4137 if (lun->be_lun->blocksize) 4138 ival /= lun->be_lun->blocksize; 4139 else 4140 ival /= 512; 4141 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4142 page->descr[2].count); 4143 } 4144 value = ctl_get_opt(&lun->be_lun->options, 4145 "pool-used-threshold"); 4146 if (value != NULL && 4147 ctl_expand_number(value, &ival) == 0) { 4148 page->descr[3].flags |= SLBPPD_ENABLED | 4149 SLBPPD_ARMING_INC; 4150 if (lun->be_lun->blocksize) 4151 ival /= lun->be_lun->blocksize; 4152 else 4153 ival /= 512; 4154 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4155 page->descr[3].count); 4156 } 4157 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4158 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4159 sizeof(lbp_page_default)); 4160 page_index->page_data = 4161 (uint8_t *)lun->mode_pages.lbp_page; 4162 }} 4163 break; 4164 } 4165 case SMS_CDDVD_CAPS_PAGE:{ 4166 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4167 &cddvd_page_default, 4168 sizeof(cddvd_page_default)); 4169 memcpy(&lun->mode_pages.cddvd_page[ 4170 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4171 sizeof(cddvd_page_changeable)); 4172 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4173 &cddvd_page_default, 4174 sizeof(cddvd_page_default)); 4175 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4176 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4177 sizeof(cddvd_page_default)); 4178 page_index->page_data = 4179 (uint8_t *)lun->mode_pages.cddvd_page; 4180 break; 4181 } 4182 case SMS_VENDOR_SPECIFIC_PAGE:{ 4183 switch (page_index->subpage) { 4184 case DBGCNF_SUBPAGE_CODE: { 4185 memcpy(&lun->mode_pages.debugconf_subpage[ 4186 CTL_PAGE_CURRENT], 4187 &debugconf_page_default, 4188 sizeof(debugconf_page_default)); 4189 memcpy(&lun->mode_pages.debugconf_subpage[ 4190 CTL_PAGE_CHANGEABLE], 4191 &debugconf_page_changeable, 4192 sizeof(debugconf_page_changeable)); 4193 memcpy(&lun->mode_pages.debugconf_subpage[ 4194 CTL_PAGE_DEFAULT], 4195 &debugconf_page_default, 4196 sizeof(debugconf_page_default)); 4197 memcpy(&lun->mode_pages.debugconf_subpage[ 4198 CTL_PAGE_SAVED], 4199 &debugconf_page_default, 4200 sizeof(debugconf_page_default)); 4201 page_index->page_data = 4202 (uint8_t *)lun->mode_pages.debugconf_subpage; 4203 break; 4204 } 4205 default: 4206 panic("invalid subpage value %d", 4207 page_index->subpage); 4208 break; 4209 } 4210 break; 4211 } 4212 default: 4213 panic("invalid page value %d", 4214 page_index->page_code & SMPH_PC_MASK); 4215 break; 4216 } 4217 } 4218 4219 return (CTL_RETVAL_COMPLETE); 4220} 4221 4222static int 4223ctl_init_log_page_index(struct ctl_lun *lun) 4224{ 4225 struct ctl_page_index *page_index; 4226 int i, j, k, prev; 4227 4228 memcpy(&lun->log_pages.index, log_page_index_template, 4229 sizeof(log_page_index_template)); 4230 4231 prev = -1; 4232 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4233 4234 page_index = &lun->log_pages.index[i]; 4235 if (lun->be_lun->lun_type == T_DIRECT && 4236 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4237 continue; 4238 if (lun->be_lun->lun_type == T_PROCESSOR && 4239 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4240 continue; 4241 if (lun->be_lun->lun_type == T_CDROM && 4242 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4243 continue; 4244 4245 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4246 lun->backend->lun_attr == NULL) 4247 continue; 4248 4249 if (page_index->page_code != prev) { 4250 lun->log_pages.pages_page[j] = page_index->page_code; 4251 prev = page_index->page_code; 4252 j++; 4253 } 4254 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4255 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4256 k++; 4257 } 4258 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4259 lun->log_pages.index[0].page_len = j; 4260 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4261 lun->log_pages.index[1].page_len = k * 2; 4262 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4263 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4264 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4265 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4266 4267 return (CTL_RETVAL_COMPLETE); 4268} 4269 4270static int 4271hex2bin(const char *str, uint8_t *buf, int buf_size) 4272{ 4273 int i; 4274 u_char c; 4275 4276 memset(buf, 0, buf_size); 4277 while (isspace(str[0])) 4278 str++; 4279 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4280 str += 2; 4281 buf_size *= 2; 4282 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4283 c = str[i]; 4284 if (isdigit(c)) 4285 c -= '0'; 4286 else if (isalpha(c)) 4287 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4288 else 4289 break; 4290 if (c >= 16) 4291 break; 4292 if ((i & 1) == 0) 4293 buf[i / 2] |= (c << 4); 4294 else 4295 buf[i / 2] |= c; 4296 } 4297 return ((i + 1) / 2); 4298} 4299 4300/* 4301 * LUN allocation. 4302 * 4303 * Requirements: 4304 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4305 * wants us to allocate the LUN and he can block. 4306 * - ctl_softc is always set 4307 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4308 * 4309 * Returns 0 for success, non-zero (errno) for failure. 4310 */ 4311static int 4312ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4313 struct ctl_be_lun *const be_lun) 4314{ 4315 struct ctl_lun *nlun, *lun; 4316 struct scsi_vpd_id_descriptor *desc; 4317 struct scsi_vpd_id_t10 *t10id; 4318 const char *eui, *naa, *scsiname, *vendor, *value; 4319 int lun_number, i, lun_malloced; 4320 int devidlen, idlen1, idlen2 = 0, len; 4321 4322 if (be_lun == NULL) 4323 return (EINVAL); 4324 4325 /* 4326 * We currently only support Direct Access or Processor LUN types. 4327 */ 4328 switch (be_lun->lun_type) { 4329 case T_DIRECT: 4330 case T_PROCESSOR: 4331 case T_CDROM: 4332 break; 4333 case T_SEQUENTIAL: 4334 case T_CHANGER: 4335 default: 4336 be_lun->lun_config_status(be_lun->be_lun, 4337 CTL_LUN_CONFIG_FAILURE); 4338 break; 4339 } 4340 if (ctl_lun == NULL) { 4341 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4342 lun_malloced = 1; 4343 } else { 4344 lun_malloced = 0; 4345 lun = ctl_lun; 4346 } 4347 4348 memset(lun, 0, sizeof(*lun)); 4349 if (lun_malloced) 4350 lun->flags = CTL_LUN_MALLOCED; 4351 4352 /* Generate LUN ID. */ 4353 devidlen = max(CTL_DEVID_MIN_LEN, 4354 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4355 idlen1 = sizeof(*t10id) + devidlen; 4356 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4357 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4358 if (scsiname != NULL) { 4359 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4360 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4361 } 4362 eui = ctl_get_opt(&be_lun->options, "eui"); 4363 if (eui != NULL) { 4364 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4365 } 4366 naa = ctl_get_opt(&be_lun->options, "naa"); 4367 if (naa != NULL) { 4368 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4369 } 4370 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4371 M_CTL, M_WAITOK | M_ZERO); 4372 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4373 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4374 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4375 desc->length = idlen1; 4376 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4377 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4378 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4379 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4380 } else { 4381 strncpy(t10id->vendor, vendor, 4382 min(sizeof(t10id->vendor), strlen(vendor))); 4383 } 4384 strncpy((char *)t10id->vendor_spec_id, 4385 (char *)be_lun->device_id, devidlen); 4386 if (scsiname != NULL) { 4387 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4388 desc->length); 4389 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4390 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4391 SVPD_ID_TYPE_SCSI_NAME; 4392 desc->length = idlen2; 4393 strlcpy(desc->identifier, scsiname, idlen2); 4394 } 4395 if (eui != NULL) { 4396 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4397 desc->length); 4398 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4399 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4400 SVPD_ID_TYPE_EUI64; 4401 desc->length = hex2bin(eui, desc->identifier, 16); 4402 desc->length = desc->length > 12 ? 16 : 4403 (desc->length > 8 ? 12 : 8); 4404 len -= 16 - desc->length; 4405 } 4406 if (naa != NULL) { 4407 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4408 desc->length); 4409 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4410 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4411 SVPD_ID_TYPE_NAA; 4412 desc->length = hex2bin(naa, desc->identifier, 16); 4413 desc->length = desc->length > 8 ? 16 : 8; 4414 len -= 16 - desc->length; 4415 } 4416 lun->lun_devid->len = len; 4417 4418 mtx_lock(&ctl_softc->ctl_lock); 4419 /* 4420 * See if the caller requested a particular LUN number. If so, see 4421 * if it is available. Otherwise, allocate the first available LUN. 4422 */ 4423 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4424 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4425 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4426 mtx_unlock(&ctl_softc->ctl_lock); 4427 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4428 printf("ctl: requested LUN ID %d is higher " 4429 "than CTL_MAX_LUNS - 1 (%d)\n", 4430 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4431 } else { 4432 /* 4433 * XXX KDM return an error, or just assign 4434 * another LUN ID in this case?? 4435 */ 4436 printf("ctl: requested LUN ID %d is already " 4437 "in use\n", be_lun->req_lun_id); 4438 } 4439 if (lun->flags & CTL_LUN_MALLOCED) 4440 free(lun, M_CTL); 4441 be_lun->lun_config_status(be_lun->be_lun, 4442 CTL_LUN_CONFIG_FAILURE); 4443 return (ENOSPC); 4444 } 4445 lun_number = be_lun->req_lun_id; 4446 } else { 4447 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4448 if (lun_number == -1) { 4449 mtx_unlock(&ctl_softc->ctl_lock); 4450 printf("ctl: can't allocate LUN, out of LUNs\n"); 4451 if (lun->flags & CTL_LUN_MALLOCED) 4452 free(lun, M_CTL); 4453 be_lun->lun_config_status(be_lun->be_lun, 4454 CTL_LUN_CONFIG_FAILURE); 4455 return (ENOSPC); 4456 } 4457 } 4458 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4459 4460 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4461 lun->lun = lun_number; 4462 lun->be_lun = be_lun; 4463 /* 4464 * The processor LUN is always enabled. Disk LUNs come on line 4465 * disabled, and must be enabled by the backend. 4466 */ 4467 lun->flags |= CTL_LUN_DISABLED; 4468 lun->backend = be_lun->be; 4469 be_lun->ctl_lun = lun; 4470 be_lun->lun_id = lun_number; 4471 atomic_add_int(&be_lun->be->num_luns, 1); 4472 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4473 lun->flags |= CTL_LUN_EJECTED; 4474 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4475 lun->flags |= CTL_LUN_NO_MEDIA; 4476 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4477 lun->flags |= CTL_LUN_STOPPED; 4478 4479 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4480 lun->flags |= CTL_LUN_PRIMARY_SC; 4481 4482 value = ctl_get_opt(&be_lun->options, "removable"); 4483 if (value != NULL) { 4484 if (strcmp(value, "on") == 0) 4485 lun->flags |= CTL_LUN_REMOVABLE; 4486 } else if (be_lun->lun_type == T_CDROM) 4487 lun->flags |= CTL_LUN_REMOVABLE; 4488 4489 lun->ctl_softc = ctl_softc; 4490#ifdef CTL_TIME_IO 4491 lun->last_busy = getsbinuptime(); 4492#endif 4493 TAILQ_INIT(&lun->ooa_queue); 4494 TAILQ_INIT(&lun->blocked_queue); 4495 STAILQ_INIT(&lun->error_list); 4496 ctl_tpc_lun_init(lun); 4497 4498 /* 4499 * Initialize the mode and log page index. 4500 */ 4501 ctl_init_page_index(lun); 4502 ctl_init_log_page_index(lun); 4503 4504 /* 4505 * Now, before we insert this lun on the lun list, set the lun 4506 * inventory changed UA for all other luns. 4507 */ 4508 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4509 mtx_lock(&nlun->lun_lock); 4510 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4511 mtx_unlock(&nlun->lun_lock); 4512 } 4513 4514 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4515 4516 ctl_softc->ctl_luns[lun_number] = lun; 4517 4518 ctl_softc->num_luns++; 4519 4520 /* Setup statistics gathering */ 4521 lun->stats.device_type = be_lun->lun_type; 4522 lun->stats.lun_number = lun_number; 4523 lun->stats.blocksize = be_lun->blocksize; 4524 if (be_lun->blocksize == 0) 4525 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4526 for (i = 0;i < CTL_MAX_PORTS;i++) 4527 lun->stats.ports[i].targ_port = i; 4528 4529 mtx_unlock(&ctl_softc->ctl_lock); 4530 4531 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4532 return (0); 4533} 4534 4535/* 4536 * Delete a LUN. 4537 * Assumptions: 4538 * - LUN has already been marked invalid and any pending I/O has been taken 4539 * care of. 4540 */ 4541static int 4542ctl_free_lun(struct ctl_lun *lun) 4543{ 4544 struct ctl_softc *softc; 4545 struct ctl_lun *nlun; 4546 int i; 4547 4548 softc = lun->ctl_softc; 4549 4550 mtx_assert(&softc->ctl_lock, MA_OWNED); 4551 4552 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4553 4554 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4555 4556 softc->ctl_luns[lun->lun] = NULL; 4557 4558 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4559 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4560 4561 softc->num_luns--; 4562 4563 /* 4564 * Tell the backend to free resources, if this LUN has a backend. 4565 */ 4566 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4567 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4568 4569 ctl_tpc_lun_shutdown(lun); 4570 mtx_destroy(&lun->lun_lock); 4571 free(lun->lun_devid, M_CTL); 4572 for (i = 0; i < CTL_MAX_PORTS; i++) 4573 free(lun->pending_ua[i], M_CTL); 4574 for (i = 0; i < CTL_MAX_PORTS; i++) 4575 free(lun->pr_keys[i], M_CTL); 4576 free(lun->write_buffer, M_CTL); 4577 if (lun->flags & CTL_LUN_MALLOCED) 4578 free(lun, M_CTL); 4579 4580 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4581 mtx_lock(&nlun->lun_lock); 4582 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4583 mtx_unlock(&nlun->lun_lock); 4584 } 4585 4586 return (0); 4587} 4588 4589static void 4590ctl_create_lun(struct ctl_be_lun *be_lun) 4591{ 4592 4593 /* 4594 * ctl_alloc_lun() should handle all potential failure cases. 4595 */ 4596 ctl_alloc_lun(control_softc, NULL, be_lun); 4597} 4598 4599int 4600ctl_add_lun(struct ctl_be_lun *be_lun) 4601{ 4602 struct ctl_softc *softc = control_softc; 4603 4604 mtx_lock(&softc->ctl_lock); 4605 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4606 mtx_unlock(&softc->ctl_lock); 4607 wakeup(&softc->pending_lun_queue); 4608 4609 return (0); 4610} 4611 4612int 4613ctl_enable_lun(struct ctl_be_lun *be_lun) 4614{ 4615 struct ctl_softc *softc; 4616 struct ctl_port *port, *nport; 4617 struct ctl_lun *lun; 4618 int retval; 4619 4620 lun = (struct ctl_lun *)be_lun->ctl_lun; 4621 softc = lun->ctl_softc; 4622 4623 mtx_lock(&softc->ctl_lock); 4624 mtx_lock(&lun->lun_lock); 4625 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4626 /* 4627 * eh? Why did we get called if the LUN is already 4628 * enabled? 4629 */ 4630 mtx_unlock(&lun->lun_lock); 4631 mtx_unlock(&softc->ctl_lock); 4632 return (0); 4633 } 4634 lun->flags &= ~CTL_LUN_DISABLED; 4635 mtx_unlock(&lun->lun_lock); 4636 4637 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4638 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4639 port->lun_map != NULL || port->lun_enable == NULL) 4640 continue; 4641 4642 /* 4643 * Drop the lock while we call the FETD's enable routine. 4644 * This can lead to a callback into CTL (at least in the 4645 * case of the internal initiator frontend. 4646 */ 4647 mtx_unlock(&softc->ctl_lock); 4648 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4649 mtx_lock(&softc->ctl_lock); 4650 if (retval != 0) { 4651 printf("%s: FETD %s port %d returned error " 4652 "%d for lun_enable on lun %jd\n", 4653 __func__, port->port_name, port->targ_port, 4654 retval, (intmax_t)lun->lun); 4655 } 4656 } 4657 4658 mtx_unlock(&softc->ctl_lock); 4659 ctl_isc_announce_lun(lun); 4660 4661 return (0); 4662} 4663 4664int 4665ctl_disable_lun(struct ctl_be_lun *be_lun) 4666{ 4667 struct ctl_softc *softc; 4668 struct ctl_port *port; 4669 struct ctl_lun *lun; 4670 int retval; 4671 4672 lun = (struct ctl_lun *)be_lun->ctl_lun; 4673 softc = lun->ctl_softc; 4674 4675 mtx_lock(&softc->ctl_lock); 4676 mtx_lock(&lun->lun_lock); 4677 if (lun->flags & CTL_LUN_DISABLED) { 4678 mtx_unlock(&lun->lun_lock); 4679 mtx_unlock(&softc->ctl_lock); 4680 return (0); 4681 } 4682 lun->flags |= CTL_LUN_DISABLED; 4683 mtx_unlock(&lun->lun_lock); 4684 4685 STAILQ_FOREACH(port, &softc->port_list, links) { 4686 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4687 port->lun_map != NULL || port->lun_disable == NULL) 4688 continue; 4689 4690 /* 4691 * Drop the lock before we call the frontend's disable 4692 * routine, to avoid lock order reversals. 4693 * 4694 * XXX KDM what happens if the frontend list changes while 4695 * we're traversing it? It's unlikely, but should be handled. 4696 */ 4697 mtx_unlock(&softc->ctl_lock); 4698 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4699 mtx_lock(&softc->ctl_lock); 4700 if (retval != 0) { 4701 printf("%s: FETD %s port %d returned error " 4702 "%d for lun_disable on lun %jd\n", 4703 __func__, port->port_name, port->targ_port, 4704 retval, (intmax_t)lun->lun); 4705 } 4706 } 4707 4708 mtx_unlock(&softc->ctl_lock); 4709 ctl_isc_announce_lun(lun); 4710 4711 return (0); 4712} 4713 4714int 4715ctl_start_lun(struct ctl_be_lun *be_lun) 4716{ 4717 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4718 4719 mtx_lock(&lun->lun_lock); 4720 lun->flags &= ~CTL_LUN_STOPPED; 4721 mtx_unlock(&lun->lun_lock); 4722 return (0); 4723} 4724 4725int 4726ctl_stop_lun(struct ctl_be_lun *be_lun) 4727{ 4728 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4729 4730 mtx_lock(&lun->lun_lock); 4731 lun->flags |= CTL_LUN_STOPPED; 4732 mtx_unlock(&lun->lun_lock); 4733 return (0); 4734} 4735 4736int 4737ctl_lun_no_media(struct ctl_be_lun *be_lun) 4738{ 4739 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4740 4741 mtx_lock(&lun->lun_lock); 4742 lun->flags |= CTL_LUN_NO_MEDIA; 4743 mtx_unlock(&lun->lun_lock); 4744 return (0); 4745} 4746 4747int 4748ctl_lun_has_media(struct ctl_be_lun *be_lun) 4749{ 4750 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4751 union ctl_ha_msg msg; 4752 4753 mtx_lock(&lun->lun_lock); 4754 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4755 if (lun->flags & CTL_LUN_REMOVABLE) 4756 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4757 mtx_unlock(&lun->lun_lock); 4758 if ((lun->flags & CTL_LUN_REMOVABLE) && 4759 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4760 bzero(&msg.ua, sizeof(msg.ua)); 4761 msg.hdr.msg_type = CTL_MSG_UA; 4762 msg.hdr.nexus.initid = -1; 4763 msg.hdr.nexus.targ_port = -1; 4764 msg.hdr.nexus.targ_lun = lun->lun; 4765 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4766 msg.ua.ua_all = 1; 4767 msg.ua.ua_set = 1; 4768 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4769 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4770 M_WAITOK); 4771 } 4772 return (0); 4773} 4774 4775int 4776ctl_lun_ejected(struct ctl_be_lun *be_lun) 4777{ 4778 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4779 4780 mtx_lock(&lun->lun_lock); 4781 lun->flags |= CTL_LUN_EJECTED; 4782 mtx_unlock(&lun->lun_lock); 4783 return (0); 4784} 4785 4786int 4787ctl_lun_primary(struct ctl_be_lun *be_lun) 4788{ 4789 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4790 4791 mtx_lock(&lun->lun_lock); 4792 lun->flags |= CTL_LUN_PRIMARY_SC; 4793 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4794 mtx_unlock(&lun->lun_lock); 4795 ctl_isc_announce_lun(lun); 4796 return (0); 4797} 4798 4799int 4800ctl_lun_secondary(struct ctl_be_lun *be_lun) 4801{ 4802 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4803 4804 mtx_lock(&lun->lun_lock); 4805 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4806 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4807 mtx_unlock(&lun->lun_lock); 4808 ctl_isc_announce_lun(lun); 4809 return (0); 4810} 4811 4812int 4813ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4814{ 4815 struct ctl_softc *softc; 4816 struct ctl_lun *lun; 4817 4818 lun = (struct ctl_lun *)be_lun->ctl_lun; 4819 softc = lun->ctl_softc; 4820 4821 mtx_lock(&lun->lun_lock); 4822 4823 /* 4824 * The LUN needs to be disabled before it can be marked invalid. 4825 */ 4826 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4827 mtx_unlock(&lun->lun_lock); 4828 return (-1); 4829 } 4830 /* 4831 * Mark the LUN invalid. 4832 */ 4833 lun->flags |= CTL_LUN_INVALID; 4834 4835 /* 4836 * If there is nothing in the OOA queue, go ahead and free the LUN. 4837 * If we have something in the OOA queue, we'll free it when the 4838 * last I/O completes. 4839 */ 4840 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4841 mtx_unlock(&lun->lun_lock); 4842 mtx_lock(&softc->ctl_lock); 4843 ctl_free_lun(lun); 4844 mtx_unlock(&softc->ctl_lock); 4845 } else 4846 mtx_unlock(&lun->lun_lock); 4847 4848 return (0); 4849} 4850 4851void 4852ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4853{ 4854 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4855 union ctl_ha_msg msg; 4856 4857 mtx_lock(&lun->lun_lock); 4858 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4859 mtx_unlock(&lun->lun_lock); 4860 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4861 /* Send msg to other side. */ 4862 bzero(&msg.ua, sizeof(msg.ua)); 4863 msg.hdr.msg_type = CTL_MSG_UA; 4864 msg.hdr.nexus.initid = -1; 4865 msg.hdr.nexus.targ_port = -1; 4866 msg.hdr.nexus.targ_lun = lun->lun; 4867 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4868 msg.ua.ua_all = 1; 4869 msg.ua.ua_set = 1; 4870 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4871 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4872 M_WAITOK); 4873 } 4874} 4875 4876/* 4877 * Backend "memory move is complete" callback for requests that never 4878 * make it down to say RAIDCore's configuration code. 4879 */ 4880int 4881ctl_config_move_done(union ctl_io *io) 4882{ 4883 int retval; 4884 4885 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4886 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4887 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4888 4889 if ((io->io_hdr.port_status != 0) && 4890 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4891 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4892 /* 4893 * For hardware error sense keys, the sense key 4894 * specific value is defined to be a retry count, 4895 * but we use it to pass back an internal FETD 4896 * error code. XXX KDM Hopefully the FETD is only 4897 * using 16 bits for an error code, since that's 4898 * all the space we have in the sks field. 4899 */ 4900 ctl_set_internal_failure(&io->scsiio, 4901 /*sks_valid*/ 1, 4902 /*retry_count*/ 4903 io->io_hdr.port_status); 4904 } 4905 4906 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4907 ctl_data_print(io); 4908 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4909 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4910 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4911 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4912 /* 4913 * XXX KDM just assuming a single pointer here, and not a 4914 * S/G list. If we start using S/G lists for config data, 4915 * we'll need to know how to clean them up here as well. 4916 */ 4917 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4918 free(io->scsiio.kern_data_ptr, M_CTL); 4919 ctl_done(io); 4920 retval = CTL_RETVAL_COMPLETE; 4921 } else { 4922 /* 4923 * XXX KDM now we need to continue data movement. Some 4924 * options: 4925 * - call ctl_scsiio() again? We don't do this for data 4926 * writes, because for those at least we know ahead of 4927 * time where the write will go and how long it is. For 4928 * config writes, though, that information is largely 4929 * contained within the write itself, thus we need to 4930 * parse out the data again. 4931 * 4932 * - Call some other function once the data is in? 4933 */ 4934 4935 /* 4936 * XXX KDM call ctl_scsiio() again for now, and check flag 4937 * bits to see whether we're allocated or not. 4938 */ 4939 retval = ctl_scsiio(&io->scsiio); 4940 } 4941 return (retval); 4942} 4943 4944/* 4945 * This gets called by a backend driver when it is done with a 4946 * data_submit method. 4947 */ 4948void 4949ctl_data_submit_done(union ctl_io *io) 4950{ 4951 /* 4952 * If the IO_CONT flag is set, we need to call the supplied 4953 * function to continue processing the I/O, instead of completing 4954 * the I/O just yet. 4955 * 4956 * If there is an error, though, we don't want to keep processing. 4957 * Instead, just send status back to the initiator. 4958 */ 4959 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4960 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4961 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4962 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4963 io->scsiio.io_cont(io); 4964 return; 4965 } 4966 ctl_done(io); 4967} 4968 4969/* 4970 * This gets called by a backend driver when it is done with a 4971 * configuration write. 4972 */ 4973void 4974ctl_config_write_done(union ctl_io *io) 4975{ 4976 uint8_t *buf; 4977 4978 /* 4979 * If the IO_CONT flag is set, we need to call the supplied 4980 * function to continue processing the I/O, instead of completing 4981 * the I/O just yet. 4982 * 4983 * If there is an error, though, we don't want to keep processing. 4984 * Instead, just send status back to the initiator. 4985 */ 4986 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4987 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4988 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4989 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4990 io->scsiio.io_cont(io); 4991 return; 4992 } 4993 /* 4994 * Since a configuration write can be done for commands that actually 4995 * have data allocated, like write buffer, and commands that have 4996 * no data, like start/stop unit, we need to check here. 4997 */ 4998 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4999 buf = io->scsiio.kern_data_ptr; 5000 else 5001 buf = NULL; 5002 ctl_done(io); 5003 if (buf) 5004 free(buf, M_CTL); 5005} 5006 5007void 5008ctl_config_read_done(union ctl_io *io) 5009{ 5010 uint8_t *buf; 5011 5012 /* 5013 * If there is some error -- we are done, skip data transfer. 5014 */ 5015 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5016 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5017 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5018 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5019 buf = io->scsiio.kern_data_ptr; 5020 else 5021 buf = NULL; 5022 ctl_done(io); 5023 if (buf) 5024 free(buf, M_CTL); 5025 return; 5026 } 5027 5028 /* 5029 * If the IO_CONT flag is set, we need to call the supplied 5030 * function to continue processing the I/O, instead of completing 5031 * the I/O just yet. 5032 */ 5033 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5034 io->scsiio.io_cont(io); 5035 return; 5036 } 5037 5038 ctl_datamove(io); 5039} 5040 5041/* 5042 * SCSI release command. 5043 */ 5044int 5045ctl_scsi_release(struct ctl_scsiio *ctsio) 5046{ 5047 struct ctl_lun *lun; 5048 uint32_t residx; 5049 5050 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5051 5052 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5053 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5054 5055 /* 5056 * XXX KDM right now, we only support LUN reservation. We don't 5057 * support 3rd party reservations, or extent reservations, which 5058 * might actually need the parameter list. If we've gotten this 5059 * far, we've got a LUN reservation. Anything else got kicked out 5060 * above. So, according to SPC, ignore the length. 5061 */ 5062 5063 mtx_lock(&lun->lun_lock); 5064 5065 /* 5066 * According to SPC, it is not an error for an intiator to attempt 5067 * to release a reservation on a LUN that isn't reserved, or that 5068 * is reserved by another initiator. The reservation can only be 5069 * released, though, by the initiator who made it or by one of 5070 * several reset type events. 5071 */ 5072 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5073 lun->flags &= ~CTL_LUN_RESERVED; 5074 5075 mtx_unlock(&lun->lun_lock); 5076 5077 ctl_set_success(ctsio); 5078 ctl_done((union ctl_io *)ctsio); 5079 return (CTL_RETVAL_COMPLETE); 5080} 5081 5082int 5083ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5084{ 5085 struct ctl_lun *lun; 5086 uint32_t residx; 5087 5088 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5089 5090 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5091 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5092 5093 /* 5094 * XXX KDM right now, we only support LUN reservation. We don't 5095 * support 3rd party reservations, or extent reservations, which 5096 * might actually need the parameter list. If we've gotten this 5097 * far, we've got a LUN reservation. Anything else got kicked out 5098 * above. So, according to SPC, ignore the length. 5099 */ 5100 5101 mtx_lock(&lun->lun_lock); 5102 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5103 ctl_set_reservation_conflict(ctsio); 5104 goto bailout; 5105 } 5106 5107 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5108 if (lun->flags & CTL_LUN_PR_RESERVED) { 5109 ctl_set_success(ctsio); 5110 goto bailout; 5111 } 5112 5113 lun->flags |= CTL_LUN_RESERVED; 5114 lun->res_idx = residx; 5115 ctl_set_success(ctsio); 5116 5117bailout: 5118 mtx_unlock(&lun->lun_lock); 5119 ctl_done((union ctl_io *)ctsio); 5120 return (CTL_RETVAL_COMPLETE); 5121} 5122 5123int 5124ctl_start_stop(struct ctl_scsiio *ctsio) 5125{ 5126 struct scsi_start_stop_unit *cdb; 5127 struct ctl_lun *lun; 5128 int retval; 5129 5130 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5131 5132 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5133 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5134 5135 if ((cdb->how & SSS_PC_MASK) == 0) { 5136 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5137 (cdb->how & SSS_START) == 0) { 5138 uint32_t residx; 5139 5140 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5141 if (ctl_get_prkey(lun, residx) == 0 || 5142 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5143 5144 ctl_set_reservation_conflict(ctsio); 5145 ctl_done((union ctl_io *)ctsio); 5146 return (CTL_RETVAL_COMPLETE); 5147 } 5148 } 5149 5150 if ((cdb->how & SSS_LOEJ) && 5151 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5152 ctl_set_invalid_field(ctsio, 5153 /*sks_valid*/ 1, 5154 /*command*/ 1, 5155 /*field*/ 4, 5156 /*bit_valid*/ 1, 5157 /*bit*/ 1); 5158 ctl_done((union ctl_io *)ctsio); 5159 return (CTL_RETVAL_COMPLETE); 5160 } 5161 5162 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5163 lun->prevent_count > 0) { 5164 /* "Medium removal prevented" */ 5165 ctl_set_sense(ctsio, /*current_error*/ 1, 5166 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5167 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5168 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5169 ctl_done((union ctl_io *)ctsio); 5170 return (CTL_RETVAL_COMPLETE); 5171 } 5172 } 5173 5174 retval = lun->backend->config_write((union ctl_io *)ctsio); 5175 return (retval); 5176} 5177 5178int 5179ctl_prevent_allow(struct ctl_scsiio *ctsio) 5180{ 5181 struct ctl_lun *lun; 5182 struct scsi_prevent *cdb; 5183 int retval; 5184 uint32_t initidx; 5185 5186 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5187 5188 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5189 cdb = (struct scsi_prevent *)ctsio->cdb; 5190 5191 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5192 ctl_set_invalid_opcode(ctsio); 5193 ctl_done((union ctl_io *)ctsio); 5194 return (CTL_RETVAL_COMPLETE); 5195 } 5196 5197 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5198 mtx_lock(&lun->lun_lock); 5199 if ((cdb->how & PR_PREVENT) && 5200 ctl_is_set(lun->prevent, initidx) == 0) { 5201 ctl_set_mask(lun->prevent, initidx); 5202 lun->prevent_count++; 5203 } else if ((cdb->how & PR_PREVENT) == 0 && 5204 ctl_is_set(lun->prevent, initidx)) { 5205 ctl_clear_mask(lun->prevent, initidx); 5206 lun->prevent_count--; 5207 } 5208 mtx_unlock(&lun->lun_lock); 5209 retval = lun->backend->config_write((union ctl_io *)ctsio); 5210 return (retval); 5211} 5212 5213/* 5214 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5215 * we don't really do anything with the LBA and length fields if the user 5216 * passes them in. Instead we'll just flush out the cache for the entire 5217 * LUN. 5218 */ 5219int 5220ctl_sync_cache(struct ctl_scsiio *ctsio) 5221{ 5222 struct ctl_lun *lun; 5223 struct ctl_softc *softc; 5224 struct ctl_lba_len_flags *lbalen; 5225 uint64_t starting_lba; 5226 uint32_t block_count; 5227 int retval; 5228 uint8_t byte2; 5229 5230 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5231 5232 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5233 softc = lun->ctl_softc; 5234 retval = 0; 5235 5236 switch (ctsio->cdb[0]) { 5237 case SYNCHRONIZE_CACHE: { 5238 struct scsi_sync_cache *cdb; 5239 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5240 5241 starting_lba = scsi_4btoul(cdb->begin_lba); 5242 block_count = scsi_2btoul(cdb->lb_count); 5243 byte2 = cdb->byte2; 5244 break; 5245 } 5246 case SYNCHRONIZE_CACHE_16: { 5247 struct scsi_sync_cache_16 *cdb; 5248 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5249 5250 starting_lba = scsi_8btou64(cdb->begin_lba); 5251 block_count = scsi_4btoul(cdb->lb_count); 5252 byte2 = cdb->byte2; 5253 break; 5254 } 5255 default: 5256 ctl_set_invalid_opcode(ctsio); 5257 ctl_done((union ctl_io *)ctsio); 5258 goto bailout; 5259 break; /* NOTREACHED */ 5260 } 5261 5262 /* 5263 * We check the LBA and length, but don't do anything with them. 5264 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5265 * get flushed. This check will just help satisfy anyone who wants 5266 * to see an error for an out of range LBA. 5267 */ 5268 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5269 ctl_set_lba_out_of_range(ctsio); 5270 ctl_done((union ctl_io *)ctsio); 5271 goto bailout; 5272 } 5273 5274 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5275 lbalen->lba = starting_lba; 5276 lbalen->len = block_count; 5277 lbalen->flags = byte2; 5278 retval = lun->backend->config_write((union ctl_io *)ctsio); 5279 5280bailout: 5281 return (retval); 5282} 5283 5284int 5285ctl_format(struct ctl_scsiio *ctsio) 5286{ 5287 struct scsi_format *cdb; 5288 struct ctl_lun *lun; 5289 int length, defect_list_len; 5290 5291 CTL_DEBUG_PRINT(("ctl_format\n")); 5292 5293 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5294 5295 cdb = (struct scsi_format *)ctsio->cdb; 5296 5297 length = 0; 5298 if (cdb->byte2 & SF_FMTDATA) { 5299 if (cdb->byte2 & SF_LONGLIST) 5300 length = sizeof(struct scsi_format_header_long); 5301 else 5302 length = sizeof(struct scsi_format_header_short); 5303 } 5304 5305 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5306 && (length > 0)) { 5307 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5308 ctsio->kern_data_len = length; 5309 ctsio->kern_total_len = length; 5310 ctsio->kern_data_resid = 0; 5311 ctsio->kern_rel_offset = 0; 5312 ctsio->kern_sg_entries = 0; 5313 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5314 ctsio->be_move_done = ctl_config_move_done; 5315 ctl_datamove((union ctl_io *)ctsio); 5316 5317 return (CTL_RETVAL_COMPLETE); 5318 } 5319 5320 defect_list_len = 0; 5321 5322 if (cdb->byte2 & SF_FMTDATA) { 5323 if (cdb->byte2 & SF_LONGLIST) { 5324 struct scsi_format_header_long *header; 5325 5326 header = (struct scsi_format_header_long *) 5327 ctsio->kern_data_ptr; 5328 5329 defect_list_len = scsi_4btoul(header->defect_list_len); 5330 if (defect_list_len != 0) { 5331 ctl_set_invalid_field(ctsio, 5332 /*sks_valid*/ 1, 5333 /*command*/ 0, 5334 /*field*/ 2, 5335 /*bit_valid*/ 0, 5336 /*bit*/ 0); 5337 goto bailout; 5338 } 5339 } else { 5340 struct scsi_format_header_short *header; 5341 5342 header = (struct scsi_format_header_short *) 5343 ctsio->kern_data_ptr; 5344 5345 defect_list_len = scsi_2btoul(header->defect_list_len); 5346 if (defect_list_len != 0) { 5347 ctl_set_invalid_field(ctsio, 5348 /*sks_valid*/ 1, 5349 /*command*/ 0, 5350 /*field*/ 2, 5351 /*bit_valid*/ 0, 5352 /*bit*/ 0); 5353 goto bailout; 5354 } 5355 } 5356 } 5357 5358 ctl_set_success(ctsio); 5359bailout: 5360 5361 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5362 free(ctsio->kern_data_ptr, M_CTL); 5363 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5364 } 5365 5366 ctl_done((union ctl_io *)ctsio); 5367 return (CTL_RETVAL_COMPLETE); 5368} 5369 5370int 5371ctl_read_buffer(struct ctl_scsiio *ctsio) 5372{ 5373 struct ctl_lun *lun; 5374 uint64_t buffer_offset; 5375 uint32_t len; 5376 uint8_t byte2; 5377 static uint8_t descr[4]; 5378 static uint8_t echo_descr[4] = { 0 }; 5379 5380 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5381 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5382 switch (ctsio->cdb[0]) { 5383 case READ_BUFFER: { 5384 struct scsi_read_buffer *cdb; 5385 5386 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5387 buffer_offset = scsi_3btoul(cdb->offset); 5388 len = scsi_3btoul(cdb->length); 5389 byte2 = cdb->byte2; 5390 break; 5391 } 5392 case READ_BUFFER_16: { 5393 struct scsi_read_buffer_16 *cdb; 5394 5395 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5396 buffer_offset = scsi_8btou64(cdb->offset); 5397 len = scsi_4btoul(cdb->length); 5398 byte2 = cdb->byte2; 5399 break; 5400 } 5401 default: /* This shouldn't happen. */ 5402 ctl_set_invalid_opcode(ctsio); 5403 ctl_done((union ctl_io *)ctsio); 5404 return (CTL_RETVAL_COMPLETE); 5405 } 5406 5407 if ((byte2 & RWB_MODE) != RWB_MODE_DATA && 5408 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5409 (byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5410 ctl_set_invalid_field(ctsio, 5411 /*sks_valid*/ 1, 5412 /*command*/ 1, 5413 /*field*/ 1, 5414 /*bit_valid*/ 1, 5415 /*bit*/ 4); 5416 ctl_done((union ctl_io *)ctsio); 5417 return (CTL_RETVAL_COMPLETE); 5418 } 5419 5420 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5421 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5422 ctl_set_invalid_field(ctsio, 5423 /*sks_valid*/ 1, 5424 /*command*/ 1, 5425 /*field*/ 6, 5426 /*bit_valid*/ 0, 5427 /*bit*/ 0); 5428 ctl_done((union ctl_io *)ctsio); 5429 return (CTL_RETVAL_COMPLETE); 5430 } 5431 5432 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5433 descr[0] = 0; 5434 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5435 ctsio->kern_data_ptr = descr; 5436 len = min(len, sizeof(descr)); 5437 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5438 ctsio->kern_data_ptr = echo_descr; 5439 len = min(len, sizeof(echo_descr)); 5440 } else { 5441 if (lun->write_buffer == NULL) { 5442 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5443 M_CTL, M_WAITOK); 5444 } 5445 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5446 } 5447 ctsio->kern_data_len = len; 5448 ctsio->kern_total_len = len; 5449 ctsio->kern_data_resid = 0; 5450 ctsio->kern_rel_offset = 0; 5451 ctsio->kern_sg_entries = 0; 5452 ctl_set_success(ctsio); 5453 ctsio->be_move_done = ctl_config_move_done; 5454 ctl_datamove((union ctl_io *)ctsio); 5455 return (CTL_RETVAL_COMPLETE); 5456} 5457 5458int 5459ctl_write_buffer(struct ctl_scsiio *ctsio) 5460{ 5461 struct scsi_write_buffer *cdb; 5462 struct ctl_lun *lun; 5463 int buffer_offset, len; 5464 5465 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5466 5467 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5468 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5469 5470 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5471 ctl_set_invalid_field(ctsio, 5472 /*sks_valid*/ 1, 5473 /*command*/ 1, 5474 /*field*/ 1, 5475 /*bit_valid*/ 1, 5476 /*bit*/ 4); 5477 ctl_done((union ctl_io *)ctsio); 5478 return (CTL_RETVAL_COMPLETE); 5479 } 5480 5481 len = scsi_3btoul(cdb->length); 5482 buffer_offset = scsi_3btoul(cdb->offset); 5483 5484 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5485 ctl_set_invalid_field(ctsio, 5486 /*sks_valid*/ 1, 5487 /*command*/ 1, 5488 /*field*/ 6, 5489 /*bit_valid*/ 0, 5490 /*bit*/ 0); 5491 ctl_done((union ctl_io *)ctsio); 5492 return (CTL_RETVAL_COMPLETE); 5493 } 5494 5495 /* 5496 * If we've got a kernel request that hasn't been malloced yet, 5497 * malloc it and tell the caller the data buffer is here. 5498 */ 5499 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5500 if (lun->write_buffer == NULL) { 5501 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5502 M_CTL, M_WAITOK); 5503 } 5504 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5505 ctsio->kern_data_len = len; 5506 ctsio->kern_total_len = len; 5507 ctsio->kern_data_resid = 0; 5508 ctsio->kern_rel_offset = 0; 5509 ctsio->kern_sg_entries = 0; 5510 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5511 ctsio->be_move_done = ctl_config_move_done; 5512 ctl_datamove((union ctl_io *)ctsio); 5513 5514 return (CTL_RETVAL_COMPLETE); 5515 } 5516 5517 ctl_set_success(ctsio); 5518 ctl_done((union ctl_io *)ctsio); 5519 return (CTL_RETVAL_COMPLETE); 5520} 5521 5522int 5523ctl_write_same(struct ctl_scsiio *ctsio) 5524{ 5525 struct ctl_lun *lun; 5526 struct ctl_lba_len_flags *lbalen; 5527 uint64_t lba; 5528 uint32_t num_blocks; 5529 int len, retval; 5530 uint8_t byte2; 5531 5532 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5533 5534 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5535 5536 switch (ctsio->cdb[0]) { 5537 case WRITE_SAME_10: { 5538 struct scsi_write_same_10 *cdb; 5539 5540 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5541 5542 lba = scsi_4btoul(cdb->addr); 5543 num_blocks = scsi_2btoul(cdb->length); 5544 byte2 = cdb->byte2; 5545 break; 5546 } 5547 case WRITE_SAME_16: { 5548 struct scsi_write_same_16 *cdb; 5549 5550 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5551 5552 lba = scsi_8btou64(cdb->addr); 5553 num_blocks = scsi_4btoul(cdb->length); 5554 byte2 = cdb->byte2; 5555 break; 5556 } 5557 default: 5558 /* 5559 * We got a command we don't support. This shouldn't 5560 * happen, commands should be filtered out above us. 5561 */ 5562 ctl_set_invalid_opcode(ctsio); 5563 ctl_done((union ctl_io *)ctsio); 5564 5565 return (CTL_RETVAL_COMPLETE); 5566 break; /* NOTREACHED */ 5567 } 5568 5569 /* ANCHOR flag can be used only together with UNMAP */ 5570 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5571 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5572 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5573 ctl_done((union ctl_io *)ctsio); 5574 return (CTL_RETVAL_COMPLETE); 5575 } 5576 5577 /* 5578 * The first check is to make sure we're in bounds, the second 5579 * check is to catch wrap-around problems. If the lba + num blocks 5580 * is less than the lba, then we've wrapped around and the block 5581 * range is invalid anyway. 5582 */ 5583 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5584 || ((lba + num_blocks) < lba)) { 5585 ctl_set_lba_out_of_range(ctsio); 5586 ctl_done((union ctl_io *)ctsio); 5587 return (CTL_RETVAL_COMPLETE); 5588 } 5589 5590 /* Zero number of blocks means "to the last logical block" */ 5591 if (num_blocks == 0) { 5592 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5593 ctl_set_invalid_field(ctsio, 5594 /*sks_valid*/ 0, 5595 /*command*/ 1, 5596 /*field*/ 0, 5597 /*bit_valid*/ 0, 5598 /*bit*/ 0); 5599 ctl_done((union ctl_io *)ctsio); 5600 return (CTL_RETVAL_COMPLETE); 5601 } 5602 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5603 } 5604 5605 len = lun->be_lun->blocksize; 5606 5607 /* 5608 * If we've got a kernel request that hasn't been malloced yet, 5609 * malloc it and tell the caller the data buffer is here. 5610 */ 5611 if ((byte2 & SWS_NDOB) == 0 && 5612 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5613 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5614 ctsio->kern_data_len = len; 5615 ctsio->kern_total_len = len; 5616 ctsio->kern_data_resid = 0; 5617 ctsio->kern_rel_offset = 0; 5618 ctsio->kern_sg_entries = 0; 5619 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5620 ctsio->be_move_done = ctl_config_move_done; 5621 ctl_datamove((union ctl_io *)ctsio); 5622 5623 return (CTL_RETVAL_COMPLETE); 5624 } 5625 5626 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5627 lbalen->lba = lba; 5628 lbalen->len = num_blocks; 5629 lbalen->flags = byte2; 5630 retval = lun->backend->config_write((union ctl_io *)ctsio); 5631 5632 return (retval); 5633} 5634 5635int 5636ctl_unmap(struct ctl_scsiio *ctsio) 5637{ 5638 struct ctl_lun *lun; 5639 struct scsi_unmap *cdb; 5640 struct ctl_ptr_len_flags *ptrlen; 5641 struct scsi_unmap_header *hdr; 5642 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5643 uint64_t lba; 5644 uint32_t num_blocks; 5645 int len, retval; 5646 uint8_t byte2; 5647 5648 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5649 5650 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5651 cdb = (struct scsi_unmap *)ctsio->cdb; 5652 5653 len = scsi_2btoul(cdb->length); 5654 byte2 = cdb->byte2; 5655 5656 /* 5657 * If we've got a kernel request that hasn't been malloced yet, 5658 * malloc it and tell the caller the data buffer is here. 5659 */ 5660 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5661 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5662 ctsio->kern_data_len = len; 5663 ctsio->kern_total_len = len; 5664 ctsio->kern_data_resid = 0; 5665 ctsio->kern_rel_offset = 0; 5666 ctsio->kern_sg_entries = 0; 5667 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5668 ctsio->be_move_done = ctl_config_move_done; 5669 ctl_datamove((union ctl_io *)ctsio); 5670 5671 return (CTL_RETVAL_COMPLETE); 5672 } 5673 5674 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5675 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5676 if (len < sizeof (*hdr) || 5677 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5678 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5679 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5680 ctl_set_invalid_field(ctsio, 5681 /*sks_valid*/ 0, 5682 /*command*/ 0, 5683 /*field*/ 0, 5684 /*bit_valid*/ 0, 5685 /*bit*/ 0); 5686 goto done; 5687 } 5688 len = scsi_2btoul(hdr->desc_length); 5689 buf = (struct scsi_unmap_desc *)(hdr + 1); 5690 end = buf + len / sizeof(*buf); 5691 5692 endnz = buf; 5693 for (range = buf; range < end; range++) { 5694 lba = scsi_8btou64(range->lba); 5695 num_blocks = scsi_4btoul(range->length); 5696 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5697 || ((lba + num_blocks) < lba)) { 5698 ctl_set_lba_out_of_range(ctsio); 5699 ctl_done((union ctl_io *)ctsio); 5700 return (CTL_RETVAL_COMPLETE); 5701 } 5702 if (num_blocks != 0) 5703 endnz = range + 1; 5704 } 5705 5706 /* 5707 * Block backend can not handle zero last range. 5708 * Filter it out and return if there is nothing left. 5709 */ 5710 len = (uint8_t *)endnz - (uint8_t *)buf; 5711 if (len == 0) { 5712 ctl_set_success(ctsio); 5713 goto done; 5714 } 5715 5716 mtx_lock(&lun->lun_lock); 5717 ptrlen = (struct ctl_ptr_len_flags *) 5718 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5719 ptrlen->ptr = (void *)buf; 5720 ptrlen->len = len; 5721 ptrlen->flags = byte2; 5722 ctl_check_blocked(lun); 5723 mtx_unlock(&lun->lun_lock); 5724 5725 retval = lun->backend->config_write((union ctl_io *)ctsio); 5726 return (retval); 5727 5728done: 5729 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5730 free(ctsio->kern_data_ptr, M_CTL); 5731 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5732 } 5733 ctl_done((union ctl_io *)ctsio); 5734 return (CTL_RETVAL_COMPLETE); 5735} 5736 5737/* 5738 * Note that this function currently doesn't actually do anything inside 5739 * CTL to enforce things if the DQue bit is turned on. 5740 * 5741 * Also note that this function can't be used in the default case, because 5742 * the DQue bit isn't set in the changeable mask for the control mode page 5743 * anyway. This is just here as an example for how to implement a page 5744 * handler, and a placeholder in case we want to allow the user to turn 5745 * tagged queueing on and off. 5746 * 5747 * The D_SENSE bit handling is functional, however, and will turn 5748 * descriptor sense on and off for a given LUN. 5749 */ 5750int 5751ctl_control_page_handler(struct ctl_scsiio *ctsio, 5752 struct ctl_page_index *page_index, uint8_t *page_ptr) 5753{ 5754 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5755 struct ctl_lun *lun; 5756 int set_ua; 5757 uint32_t initidx; 5758 5759 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5760 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5761 set_ua = 0; 5762 5763 user_cp = (struct scsi_control_page *)page_ptr; 5764 current_cp = (struct scsi_control_page *) 5765 (page_index->page_data + (page_index->page_len * 5766 CTL_PAGE_CURRENT)); 5767 saved_cp = (struct scsi_control_page *) 5768 (page_index->page_data + (page_index->page_len * 5769 CTL_PAGE_SAVED)); 5770 5771 mtx_lock(&lun->lun_lock); 5772 if (((current_cp->rlec & SCP_DSENSE) == 0) 5773 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5774 /* 5775 * Descriptor sense is currently turned off and the user 5776 * wants to turn it on. 5777 */ 5778 current_cp->rlec |= SCP_DSENSE; 5779 saved_cp->rlec |= SCP_DSENSE; 5780 lun->flags |= CTL_LUN_SENSE_DESC; 5781 set_ua = 1; 5782 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5783 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5784 /* 5785 * Descriptor sense is currently turned on, and the user 5786 * wants to turn it off. 5787 */ 5788 current_cp->rlec &= ~SCP_DSENSE; 5789 saved_cp->rlec &= ~SCP_DSENSE; 5790 lun->flags &= ~CTL_LUN_SENSE_DESC; 5791 set_ua = 1; 5792 } 5793 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5794 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5795 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5796 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5797 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5798 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5799 set_ua = 1; 5800 } 5801 if ((current_cp->eca_and_aen & SCP_SWP) != 5802 (user_cp->eca_and_aen & SCP_SWP)) { 5803 current_cp->eca_and_aen &= ~SCP_SWP; 5804 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5805 saved_cp->eca_and_aen &= ~SCP_SWP; 5806 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5807 set_ua = 1; 5808 } 5809 if (set_ua != 0) 5810 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5811 mtx_unlock(&lun->lun_lock); 5812 if (set_ua) { 5813 ctl_isc_announce_mode(lun, 5814 ctl_get_initindex(&ctsio->io_hdr.nexus), 5815 page_index->page_code, page_index->subpage); 5816 } 5817 return (0); 5818} 5819 5820int 5821ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5822 struct ctl_page_index *page_index, uint8_t *page_ptr) 5823{ 5824 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5825 struct ctl_lun *lun; 5826 int set_ua; 5827 uint32_t initidx; 5828 5829 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5830 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5831 set_ua = 0; 5832 5833 user_cp = (struct scsi_caching_page *)page_ptr; 5834 current_cp = (struct scsi_caching_page *) 5835 (page_index->page_data + (page_index->page_len * 5836 CTL_PAGE_CURRENT)); 5837 saved_cp = (struct scsi_caching_page *) 5838 (page_index->page_data + (page_index->page_len * 5839 CTL_PAGE_SAVED)); 5840 5841 mtx_lock(&lun->lun_lock); 5842 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5843 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5844 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5845 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5846 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5847 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5848 set_ua = 1; 5849 } 5850 if (set_ua != 0) 5851 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5852 mtx_unlock(&lun->lun_lock); 5853 if (set_ua) { 5854 ctl_isc_announce_mode(lun, 5855 ctl_get_initindex(&ctsio->io_hdr.nexus), 5856 page_index->page_code, page_index->subpage); 5857 } 5858 return (0); 5859} 5860 5861int 5862ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5863 struct ctl_page_index *page_index, 5864 uint8_t *page_ptr) 5865{ 5866 uint8_t *c; 5867 int i; 5868 5869 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5870 ctl_time_io_secs = 5871 (c[0] << 8) | 5872 (c[1] << 0) | 5873 0; 5874 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5875 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5876 printf("page data:"); 5877 for (i=0; i<8; i++) 5878 printf(" %.2x",page_ptr[i]); 5879 printf("\n"); 5880 return (0); 5881} 5882 5883int 5884ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5885 struct ctl_page_index *page_index, 5886 int pc) 5887{ 5888 struct copan_debugconf_subpage *page; 5889 5890 page = (struct copan_debugconf_subpage *)page_index->page_data + 5891 (page_index->page_len * pc); 5892 5893 switch (pc) { 5894 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5895 case SMS_PAGE_CTRL_DEFAULT >> 6: 5896 case SMS_PAGE_CTRL_SAVED >> 6: 5897 /* 5898 * We don't update the changable or default bits for this page. 5899 */ 5900 break; 5901 case SMS_PAGE_CTRL_CURRENT >> 6: 5902 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5903 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5904 break; 5905 default: 5906 break; 5907 } 5908 return (0); 5909} 5910 5911 5912static int 5913ctl_do_mode_select(union ctl_io *io) 5914{ 5915 struct scsi_mode_page_header *page_header; 5916 struct ctl_page_index *page_index; 5917 struct ctl_scsiio *ctsio; 5918 int page_len, page_len_offset, page_len_size; 5919 union ctl_modepage_info *modepage_info; 5920 struct ctl_lun *lun; 5921 int *len_left, *len_used; 5922 int retval, i; 5923 5924 ctsio = &io->scsiio; 5925 page_index = NULL; 5926 page_len = 0; 5927 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5928 5929 modepage_info = (union ctl_modepage_info *) 5930 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5931 len_left = &modepage_info->header.len_left; 5932 len_used = &modepage_info->header.len_used; 5933 5934do_next_page: 5935 5936 page_header = (struct scsi_mode_page_header *) 5937 (ctsio->kern_data_ptr + *len_used); 5938 5939 if (*len_left == 0) { 5940 free(ctsio->kern_data_ptr, M_CTL); 5941 ctl_set_success(ctsio); 5942 ctl_done((union ctl_io *)ctsio); 5943 return (CTL_RETVAL_COMPLETE); 5944 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5945 5946 free(ctsio->kern_data_ptr, M_CTL); 5947 ctl_set_param_len_error(ctsio); 5948 ctl_done((union ctl_io *)ctsio); 5949 return (CTL_RETVAL_COMPLETE); 5950 5951 } else if ((page_header->page_code & SMPH_SPF) 5952 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5953 5954 free(ctsio->kern_data_ptr, M_CTL); 5955 ctl_set_param_len_error(ctsio); 5956 ctl_done((union ctl_io *)ctsio); 5957 return (CTL_RETVAL_COMPLETE); 5958 } 5959 5960 5961 /* 5962 * XXX KDM should we do something with the block descriptor? 5963 */ 5964 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5965 page_index = &lun->mode_pages.index[i]; 5966 if (lun->be_lun->lun_type == T_DIRECT && 5967 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5968 continue; 5969 if (lun->be_lun->lun_type == T_PROCESSOR && 5970 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5971 continue; 5972 if (lun->be_lun->lun_type == T_CDROM && 5973 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5974 continue; 5975 5976 if ((page_index->page_code & SMPH_PC_MASK) != 5977 (page_header->page_code & SMPH_PC_MASK)) 5978 continue; 5979 5980 /* 5981 * If neither page has a subpage code, then we've got a 5982 * match. 5983 */ 5984 if (((page_index->page_code & SMPH_SPF) == 0) 5985 && ((page_header->page_code & SMPH_SPF) == 0)) { 5986 page_len = page_header->page_length; 5987 break; 5988 } 5989 5990 /* 5991 * If both pages have subpages, then the subpage numbers 5992 * have to match. 5993 */ 5994 if ((page_index->page_code & SMPH_SPF) 5995 && (page_header->page_code & SMPH_SPF)) { 5996 struct scsi_mode_page_header_sp *sph; 5997 5998 sph = (struct scsi_mode_page_header_sp *)page_header; 5999 if (page_index->subpage == sph->subpage) { 6000 page_len = scsi_2btoul(sph->page_length); 6001 break; 6002 } 6003 } 6004 } 6005 6006 /* 6007 * If we couldn't find the page, or if we don't have a mode select 6008 * handler for it, send back an error to the user. 6009 */ 6010 if ((i >= CTL_NUM_MODE_PAGES) 6011 || (page_index->select_handler == NULL)) { 6012 ctl_set_invalid_field(ctsio, 6013 /*sks_valid*/ 1, 6014 /*command*/ 0, 6015 /*field*/ *len_used, 6016 /*bit_valid*/ 0, 6017 /*bit*/ 0); 6018 free(ctsio->kern_data_ptr, M_CTL); 6019 ctl_done((union ctl_io *)ctsio); 6020 return (CTL_RETVAL_COMPLETE); 6021 } 6022 6023 if (page_index->page_code & SMPH_SPF) { 6024 page_len_offset = 2; 6025 page_len_size = 2; 6026 } else { 6027 page_len_size = 1; 6028 page_len_offset = 1; 6029 } 6030 6031 /* 6032 * If the length the initiator gives us isn't the one we specify in 6033 * the mode page header, or if they didn't specify enough data in 6034 * the CDB to avoid truncating this page, kick out the request. 6035 */ 6036 if ((page_len != (page_index->page_len - page_len_offset - 6037 page_len_size)) 6038 || (*len_left < page_index->page_len)) { 6039 6040 6041 ctl_set_invalid_field(ctsio, 6042 /*sks_valid*/ 1, 6043 /*command*/ 0, 6044 /*field*/ *len_used + page_len_offset, 6045 /*bit_valid*/ 0, 6046 /*bit*/ 0); 6047 free(ctsio->kern_data_ptr, M_CTL); 6048 ctl_done((union ctl_io *)ctsio); 6049 return (CTL_RETVAL_COMPLETE); 6050 } 6051 6052 /* 6053 * Run through the mode page, checking to make sure that the bits 6054 * the user changed are actually legal for him to change. 6055 */ 6056 for (i = 0; i < page_index->page_len; i++) { 6057 uint8_t *user_byte, *change_mask, *current_byte; 6058 int bad_bit; 6059 int j; 6060 6061 user_byte = (uint8_t *)page_header + i; 6062 change_mask = page_index->page_data + 6063 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6064 current_byte = page_index->page_data + 6065 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6066 6067 /* 6068 * Check to see whether the user set any bits in this byte 6069 * that he is not allowed to set. 6070 */ 6071 if ((*user_byte & ~(*change_mask)) == 6072 (*current_byte & ~(*change_mask))) 6073 continue; 6074 6075 /* 6076 * Go through bit by bit to determine which one is illegal. 6077 */ 6078 bad_bit = 0; 6079 for (j = 7; j >= 0; j--) { 6080 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6081 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6082 bad_bit = i; 6083 break; 6084 } 6085 } 6086 ctl_set_invalid_field(ctsio, 6087 /*sks_valid*/ 1, 6088 /*command*/ 0, 6089 /*field*/ *len_used + i, 6090 /*bit_valid*/ 1, 6091 /*bit*/ bad_bit); 6092 free(ctsio->kern_data_ptr, M_CTL); 6093 ctl_done((union ctl_io *)ctsio); 6094 return (CTL_RETVAL_COMPLETE); 6095 } 6096 6097 /* 6098 * Decrement these before we call the page handler, since we may 6099 * end up getting called back one way or another before the handler 6100 * returns to this context. 6101 */ 6102 *len_left -= page_index->page_len; 6103 *len_used += page_index->page_len; 6104 6105 retval = page_index->select_handler(ctsio, page_index, 6106 (uint8_t *)page_header); 6107 6108 /* 6109 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6110 * wait until this queued command completes to finish processing 6111 * the mode page. If it returns anything other than 6112 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6113 * already set the sense information, freed the data pointer, and 6114 * completed the io for us. 6115 */ 6116 if (retval != CTL_RETVAL_COMPLETE) 6117 goto bailout_no_done; 6118 6119 /* 6120 * If the initiator sent us more than one page, parse the next one. 6121 */ 6122 if (*len_left > 0) 6123 goto do_next_page; 6124 6125 ctl_set_success(ctsio); 6126 free(ctsio->kern_data_ptr, M_CTL); 6127 ctl_done((union ctl_io *)ctsio); 6128 6129bailout_no_done: 6130 6131 return (CTL_RETVAL_COMPLETE); 6132 6133} 6134 6135int 6136ctl_mode_select(struct ctl_scsiio *ctsio) 6137{ 6138 int param_len, pf, sp; 6139 int header_size, bd_len; 6140 union ctl_modepage_info *modepage_info; 6141 6142 switch (ctsio->cdb[0]) { 6143 case MODE_SELECT_6: { 6144 struct scsi_mode_select_6 *cdb; 6145 6146 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6147 6148 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6149 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6150 param_len = cdb->length; 6151 header_size = sizeof(struct scsi_mode_header_6); 6152 break; 6153 } 6154 case MODE_SELECT_10: { 6155 struct scsi_mode_select_10 *cdb; 6156 6157 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6158 6159 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6160 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6161 param_len = scsi_2btoul(cdb->length); 6162 header_size = sizeof(struct scsi_mode_header_10); 6163 break; 6164 } 6165 default: 6166 ctl_set_invalid_opcode(ctsio); 6167 ctl_done((union ctl_io *)ctsio); 6168 return (CTL_RETVAL_COMPLETE); 6169 } 6170 6171 /* 6172 * From SPC-3: 6173 * "A parameter list length of zero indicates that the Data-Out Buffer 6174 * shall be empty. This condition shall not be considered as an error." 6175 */ 6176 if (param_len == 0) { 6177 ctl_set_success(ctsio); 6178 ctl_done((union ctl_io *)ctsio); 6179 return (CTL_RETVAL_COMPLETE); 6180 } 6181 6182 /* 6183 * Since we'll hit this the first time through, prior to 6184 * allocation, we don't need to free a data buffer here. 6185 */ 6186 if (param_len < header_size) { 6187 ctl_set_param_len_error(ctsio); 6188 ctl_done((union ctl_io *)ctsio); 6189 return (CTL_RETVAL_COMPLETE); 6190 } 6191 6192 /* 6193 * Allocate the data buffer and grab the user's data. In theory, 6194 * we shouldn't have to sanity check the parameter list length here 6195 * because the maximum size is 64K. We should be able to malloc 6196 * that much without too many problems. 6197 */ 6198 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6199 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6200 ctsio->kern_data_len = param_len; 6201 ctsio->kern_total_len = param_len; 6202 ctsio->kern_data_resid = 0; 6203 ctsio->kern_rel_offset = 0; 6204 ctsio->kern_sg_entries = 0; 6205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6206 ctsio->be_move_done = ctl_config_move_done; 6207 ctl_datamove((union ctl_io *)ctsio); 6208 6209 return (CTL_RETVAL_COMPLETE); 6210 } 6211 6212 switch (ctsio->cdb[0]) { 6213 case MODE_SELECT_6: { 6214 struct scsi_mode_header_6 *mh6; 6215 6216 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6217 bd_len = mh6->blk_desc_len; 6218 break; 6219 } 6220 case MODE_SELECT_10: { 6221 struct scsi_mode_header_10 *mh10; 6222 6223 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6224 bd_len = scsi_2btoul(mh10->blk_desc_len); 6225 break; 6226 } 6227 default: 6228 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6229 break; 6230 } 6231 6232 if (param_len < (header_size + bd_len)) { 6233 free(ctsio->kern_data_ptr, M_CTL); 6234 ctl_set_param_len_error(ctsio); 6235 ctl_done((union ctl_io *)ctsio); 6236 return (CTL_RETVAL_COMPLETE); 6237 } 6238 6239 /* 6240 * Set the IO_CONT flag, so that if this I/O gets passed to 6241 * ctl_config_write_done(), it'll get passed back to 6242 * ctl_do_mode_select() for further processing, or completion if 6243 * we're all done. 6244 */ 6245 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6246 ctsio->io_cont = ctl_do_mode_select; 6247 6248 modepage_info = (union ctl_modepage_info *) 6249 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6250 memset(modepage_info, 0, sizeof(*modepage_info)); 6251 modepage_info->header.len_left = param_len - header_size - bd_len; 6252 modepage_info->header.len_used = header_size + bd_len; 6253 6254 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6255} 6256 6257int 6258ctl_mode_sense(struct ctl_scsiio *ctsio) 6259{ 6260 struct ctl_lun *lun; 6261 int pc, page_code, dbd, llba, subpage; 6262 int alloc_len, page_len, header_len, total_len; 6263 struct scsi_mode_block_descr *block_desc; 6264 struct ctl_page_index *page_index; 6265 6266 dbd = 0; 6267 llba = 0; 6268 block_desc = NULL; 6269 6270 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6271 6272 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6273 switch (ctsio->cdb[0]) { 6274 case MODE_SENSE_6: { 6275 struct scsi_mode_sense_6 *cdb; 6276 6277 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6278 6279 header_len = sizeof(struct scsi_mode_hdr_6); 6280 if (cdb->byte2 & SMS_DBD) 6281 dbd = 1; 6282 else 6283 header_len += sizeof(struct scsi_mode_block_descr); 6284 6285 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6286 page_code = cdb->page & SMS_PAGE_CODE; 6287 subpage = cdb->subpage; 6288 alloc_len = cdb->length; 6289 break; 6290 } 6291 case MODE_SENSE_10: { 6292 struct scsi_mode_sense_10 *cdb; 6293 6294 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6295 6296 header_len = sizeof(struct scsi_mode_hdr_10); 6297 6298 if (cdb->byte2 & SMS_DBD) 6299 dbd = 1; 6300 else 6301 header_len += sizeof(struct scsi_mode_block_descr); 6302 if (cdb->byte2 & SMS10_LLBAA) 6303 llba = 1; 6304 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6305 page_code = cdb->page & SMS_PAGE_CODE; 6306 subpage = cdb->subpage; 6307 alloc_len = scsi_2btoul(cdb->length); 6308 break; 6309 } 6310 default: 6311 ctl_set_invalid_opcode(ctsio); 6312 ctl_done((union ctl_io *)ctsio); 6313 return (CTL_RETVAL_COMPLETE); 6314 break; /* NOTREACHED */ 6315 } 6316 6317 /* 6318 * We have to make a first pass through to calculate the size of 6319 * the pages that match the user's query. Then we allocate enough 6320 * memory to hold it, and actually copy the data into the buffer. 6321 */ 6322 switch (page_code) { 6323 case SMS_ALL_PAGES_PAGE: { 6324 int i; 6325 6326 page_len = 0; 6327 6328 /* 6329 * At the moment, values other than 0 and 0xff here are 6330 * reserved according to SPC-3. 6331 */ 6332 if ((subpage != SMS_SUBPAGE_PAGE_0) 6333 && (subpage != SMS_SUBPAGE_ALL)) { 6334 ctl_set_invalid_field(ctsio, 6335 /*sks_valid*/ 1, 6336 /*command*/ 1, 6337 /*field*/ 3, 6338 /*bit_valid*/ 0, 6339 /*bit*/ 0); 6340 ctl_done((union ctl_io *)ctsio); 6341 return (CTL_RETVAL_COMPLETE); 6342 } 6343 6344 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6345 page_index = &lun->mode_pages.index[i]; 6346 6347 /* Make sure the page is supported for this dev type */ 6348 if (lun->be_lun->lun_type == T_DIRECT && 6349 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6350 continue; 6351 if (lun->be_lun->lun_type == T_PROCESSOR && 6352 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6353 continue; 6354 if (lun->be_lun->lun_type == T_CDROM && 6355 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6356 continue; 6357 6358 /* 6359 * We don't use this subpage if the user didn't 6360 * request all subpages. 6361 */ 6362 if ((page_index->subpage != 0) 6363 && (subpage == SMS_SUBPAGE_PAGE_0)) 6364 continue; 6365 6366#if 0 6367 printf("found page %#x len %d\n", 6368 page_index->page_code & SMPH_PC_MASK, 6369 page_index->page_len); 6370#endif 6371 page_len += page_index->page_len; 6372 } 6373 break; 6374 } 6375 default: { 6376 int i; 6377 6378 page_len = 0; 6379 6380 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6381 page_index = &lun->mode_pages.index[i]; 6382 6383 /* Make sure the page is supported for this dev type */ 6384 if (lun->be_lun->lun_type == T_DIRECT && 6385 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6386 continue; 6387 if (lun->be_lun->lun_type == T_PROCESSOR && 6388 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6389 continue; 6390 if (lun->be_lun->lun_type == T_CDROM && 6391 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6392 continue; 6393 6394 /* Look for the right page code */ 6395 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6396 continue; 6397 6398 /* Look for the right subpage or the subpage wildcard*/ 6399 if ((page_index->subpage != subpage) 6400 && (subpage != SMS_SUBPAGE_ALL)) 6401 continue; 6402 6403#if 0 6404 printf("found page %#x len %d\n", 6405 page_index->page_code & SMPH_PC_MASK, 6406 page_index->page_len); 6407#endif 6408 6409 page_len += page_index->page_len; 6410 } 6411 6412 if (page_len == 0) { 6413 ctl_set_invalid_field(ctsio, 6414 /*sks_valid*/ 1, 6415 /*command*/ 1, 6416 /*field*/ 2, 6417 /*bit_valid*/ 1, 6418 /*bit*/ 5); 6419 ctl_done((union ctl_io *)ctsio); 6420 return (CTL_RETVAL_COMPLETE); 6421 } 6422 break; 6423 } 6424 } 6425 6426 total_len = header_len + page_len; 6427#if 0 6428 printf("header_len = %d, page_len = %d, total_len = %d\n", 6429 header_len, page_len, total_len); 6430#endif 6431 6432 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6433 ctsio->kern_sg_entries = 0; 6434 ctsio->kern_data_resid = 0; 6435 ctsio->kern_rel_offset = 0; 6436 if (total_len < alloc_len) { 6437 ctsio->residual = alloc_len - total_len; 6438 ctsio->kern_data_len = total_len; 6439 ctsio->kern_total_len = total_len; 6440 } else { 6441 ctsio->residual = 0; 6442 ctsio->kern_data_len = alloc_len; 6443 ctsio->kern_total_len = alloc_len; 6444 } 6445 6446 switch (ctsio->cdb[0]) { 6447 case MODE_SENSE_6: { 6448 struct scsi_mode_hdr_6 *header; 6449 6450 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6451 6452 header->datalen = MIN(total_len - 1, 254); 6453 if (lun->be_lun->lun_type == T_DIRECT) { 6454 header->dev_specific = 0x10; /* DPOFUA */ 6455 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6456 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6457 .eca_and_aen & SCP_SWP) != 0) 6458 header->dev_specific |= 0x80; /* WP */ 6459 } 6460 if (dbd) 6461 header->block_descr_len = 0; 6462 else 6463 header->block_descr_len = 6464 sizeof(struct scsi_mode_block_descr); 6465 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6466 break; 6467 } 6468 case MODE_SENSE_10: { 6469 struct scsi_mode_hdr_10 *header; 6470 int datalen; 6471 6472 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6473 6474 datalen = MIN(total_len - 2, 65533); 6475 scsi_ulto2b(datalen, header->datalen); 6476 if (lun->be_lun->lun_type == T_DIRECT) { 6477 header->dev_specific = 0x10; /* DPOFUA */ 6478 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6479 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6480 .eca_and_aen & SCP_SWP) != 0) 6481 header->dev_specific |= 0x80; /* WP */ 6482 } 6483 if (dbd) 6484 scsi_ulto2b(0, header->block_descr_len); 6485 else 6486 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6487 header->block_descr_len); 6488 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6489 break; 6490 } 6491 default: 6492 panic("invalid CDB type %#x", ctsio->cdb[0]); 6493 break; /* NOTREACHED */ 6494 } 6495 6496 /* 6497 * If we've got a disk, use its blocksize in the block 6498 * descriptor. Otherwise, just set it to 0. 6499 */ 6500 if (dbd == 0) { 6501 if (lun->be_lun->lun_type == T_DIRECT) 6502 scsi_ulto3b(lun->be_lun->blocksize, 6503 block_desc->block_len); 6504 else 6505 scsi_ulto3b(0, block_desc->block_len); 6506 } 6507 6508 switch (page_code) { 6509 case SMS_ALL_PAGES_PAGE: { 6510 int i, data_used; 6511 6512 data_used = header_len; 6513 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6514 struct ctl_page_index *page_index; 6515 6516 page_index = &lun->mode_pages.index[i]; 6517 if (lun->be_lun->lun_type == T_DIRECT && 6518 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6519 continue; 6520 if (lun->be_lun->lun_type == T_PROCESSOR && 6521 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6522 continue; 6523 if (lun->be_lun->lun_type == T_CDROM && 6524 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6525 continue; 6526 6527 /* 6528 * We don't use this subpage if the user didn't 6529 * request all subpages. We already checked (above) 6530 * to make sure the user only specified a subpage 6531 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6532 */ 6533 if ((page_index->subpage != 0) 6534 && (subpage == SMS_SUBPAGE_PAGE_0)) 6535 continue; 6536 6537 /* 6538 * Call the handler, if it exists, to update the 6539 * page to the latest values. 6540 */ 6541 if (page_index->sense_handler != NULL) 6542 page_index->sense_handler(ctsio, page_index,pc); 6543 6544 memcpy(ctsio->kern_data_ptr + data_used, 6545 page_index->page_data + 6546 (page_index->page_len * pc), 6547 page_index->page_len); 6548 data_used += page_index->page_len; 6549 } 6550 break; 6551 } 6552 default: { 6553 int i, data_used; 6554 6555 data_used = header_len; 6556 6557 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6558 struct ctl_page_index *page_index; 6559 6560 page_index = &lun->mode_pages.index[i]; 6561 6562 /* Look for the right page code */ 6563 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6564 continue; 6565 6566 /* Look for the right subpage or the subpage wildcard*/ 6567 if ((page_index->subpage != subpage) 6568 && (subpage != SMS_SUBPAGE_ALL)) 6569 continue; 6570 6571 /* Make sure the page is supported for this dev type */ 6572 if (lun->be_lun->lun_type == T_DIRECT && 6573 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6574 continue; 6575 if (lun->be_lun->lun_type == T_PROCESSOR && 6576 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6577 continue; 6578 if (lun->be_lun->lun_type == T_CDROM && 6579 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6580 continue; 6581 6582 /* 6583 * Call the handler, if it exists, to update the 6584 * page to the latest values. 6585 */ 6586 if (page_index->sense_handler != NULL) 6587 page_index->sense_handler(ctsio, page_index,pc); 6588 6589 memcpy(ctsio->kern_data_ptr + data_used, 6590 page_index->page_data + 6591 (page_index->page_len * pc), 6592 page_index->page_len); 6593 data_used += page_index->page_len; 6594 } 6595 break; 6596 } 6597 } 6598 6599 ctl_set_success(ctsio); 6600 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6601 ctsio->be_move_done = ctl_config_move_done; 6602 ctl_datamove((union ctl_io *)ctsio); 6603 return (CTL_RETVAL_COMPLETE); 6604} 6605 6606int 6607ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6608 struct ctl_page_index *page_index, 6609 int pc) 6610{ 6611 struct ctl_lun *lun; 6612 struct scsi_log_param_header *phdr; 6613 uint8_t *data; 6614 uint64_t val; 6615 6616 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6617 data = page_index->page_data; 6618 6619 if (lun->backend->lun_attr != NULL && 6620 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6621 != UINT64_MAX) { 6622 phdr = (struct scsi_log_param_header *)data; 6623 scsi_ulto2b(0x0001, phdr->param_code); 6624 phdr->param_control = SLP_LBIN | SLP_LP; 6625 phdr->param_len = 8; 6626 data = (uint8_t *)(phdr + 1); 6627 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6628 data[4] = 0x02; /* per-pool */ 6629 data += phdr->param_len; 6630 } 6631 6632 if (lun->backend->lun_attr != NULL && 6633 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6634 != UINT64_MAX) { 6635 phdr = (struct scsi_log_param_header *)data; 6636 scsi_ulto2b(0x0002, phdr->param_code); 6637 phdr->param_control = SLP_LBIN | SLP_LP; 6638 phdr->param_len = 8; 6639 data = (uint8_t *)(phdr + 1); 6640 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6641 data[4] = 0x01; /* per-LUN */ 6642 data += phdr->param_len; 6643 } 6644 6645 if (lun->backend->lun_attr != NULL && 6646 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6647 != UINT64_MAX) { 6648 phdr = (struct scsi_log_param_header *)data; 6649 scsi_ulto2b(0x00f1, phdr->param_code); 6650 phdr->param_control = SLP_LBIN | SLP_LP; 6651 phdr->param_len = 8; 6652 data = (uint8_t *)(phdr + 1); 6653 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6654 data[4] = 0x02; /* per-pool */ 6655 data += phdr->param_len; 6656 } 6657 6658 if (lun->backend->lun_attr != NULL && 6659 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6660 != UINT64_MAX) { 6661 phdr = (struct scsi_log_param_header *)data; 6662 scsi_ulto2b(0x00f2, phdr->param_code); 6663 phdr->param_control = SLP_LBIN | SLP_LP; 6664 phdr->param_len = 8; 6665 data = (uint8_t *)(phdr + 1); 6666 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6667 data[4] = 0x02; /* per-pool */ 6668 data += phdr->param_len; 6669 } 6670 6671 page_index->page_len = data - page_index->page_data; 6672 return (0); 6673} 6674 6675int 6676ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6677 struct ctl_page_index *page_index, 6678 int pc) 6679{ 6680 struct ctl_lun *lun; 6681 struct stat_page *data; 6682 uint64_t rn, wn, rb, wb; 6683 struct bintime rt, wt; 6684 int i; 6685 6686 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6687 data = (struct stat_page *)page_index->page_data; 6688 6689 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6690 data->sap.hdr.param_control = SLP_LBIN; 6691 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6692 sizeof(struct scsi_log_param_header); 6693 rn = wn = rb = wb = 0; 6694 bintime_clear(&rt); 6695 bintime_clear(&wt); 6696 for (i = 0; i < CTL_MAX_PORTS; i++) { 6697 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6698 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6699 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6700 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6701 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6702 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6703 } 6704 scsi_u64to8b(rn, data->sap.read_num); 6705 scsi_u64to8b(wn, data->sap.write_num); 6706 if (lun->stats.blocksize > 0) { 6707 scsi_u64to8b(wb / lun->stats.blocksize, 6708 data->sap.recvieved_lba); 6709 scsi_u64to8b(rb / lun->stats.blocksize, 6710 data->sap.transmitted_lba); 6711 } 6712 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6713 data->sap.read_int); 6714 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6715 data->sap.write_int); 6716 scsi_u64to8b(0, data->sap.weighted_num); 6717 scsi_u64to8b(0, data->sap.weighted_int); 6718 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6719 data->it.hdr.param_control = SLP_LBIN; 6720 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6721 sizeof(struct scsi_log_param_header); 6722#ifdef CTL_TIME_IO 6723 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6724#endif 6725 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6726 data->it.hdr.param_control = SLP_LBIN; 6727 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6728 sizeof(struct scsi_log_param_header); 6729 scsi_ulto4b(3, data->ti.exponent); 6730 scsi_ulto4b(1, data->ti.integer); 6731 6732 page_index->page_len = sizeof(*data); 6733 return (0); 6734} 6735 6736int 6737ctl_log_sense(struct ctl_scsiio *ctsio) 6738{ 6739 struct ctl_lun *lun; 6740 int i, pc, page_code, subpage; 6741 int alloc_len, total_len; 6742 struct ctl_page_index *page_index; 6743 struct scsi_log_sense *cdb; 6744 struct scsi_log_header *header; 6745 6746 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6747 6748 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6749 cdb = (struct scsi_log_sense *)ctsio->cdb; 6750 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6751 page_code = cdb->page & SLS_PAGE_CODE; 6752 subpage = cdb->subpage; 6753 alloc_len = scsi_2btoul(cdb->length); 6754 6755 page_index = NULL; 6756 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6757 page_index = &lun->log_pages.index[i]; 6758 6759 /* Look for the right page code */ 6760 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6761 continue; 6762 6763 /* Look for the right subpage or the subpage wildcard*/ 6764 if (page_index->subpage != subpage) 6765 continue; 6766 6767 break; 6768 } 6769 if (i >= CTL_NUM_LOG_PAGES) { 6770 ctl_set_invalid_field(ctsio, 6771 /*sks_valid*/ 1, 6772 /*command*/ 1, 6773 /*field*/ 2, 6774 /*bit_valid*/ 0, 6775 /*bit*/ 0); 6776 ctl_done((union ctl_io *)ctsio); 6777 return (CTL_RETVAL_COMPLETE); 6778 } 6779 6780 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6781 6782 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6783 ctsio->kern_sg_entries = 0; 6784 ctsio->kern_data_resid = 0; 6785 ctsio->kern_rel_offset = 0; 6786 if (total_len < alloc_len) { 6787 ctsio->residual = alloc_len - total_len; 6788 ctsio->kern_data_len = total_len; 6789 ctsio->kern_total_len = total_len; 6790 } else { 6791 ctsio->residual = 0; 6792 ctsio->kern_data_len = alloc_len; 6793 ctsio->kern_total_len = alloc_len; 6794 } 6795 6796 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6797 header->page = page_index->page_code; 6798 if (page_index->subpage) { 6799 header->page |= SL_SPF; 6800 header->subpage = page_index->subpage; 6801 } 6802 scsi_ulto2b(page_index->page_len, header->datalen); 6803 6804 /* 6805 * Call the handler, if it exists, to update the 6806 * page to the latest values. 6807 */ 6808 if (page_index->sense_handler != NULL) 6809 page_index->sense_handler(ctsio, page_index, pc); 6810 6811 memcpy(header + 1, page_index->page_data, page_index->page_len); 6812 6813 ctl_set_success(ctsio); 6814 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6815 ctsio->be_move_done = ctl_config_move_done; 6816 ctl_datamove((union ctl_io *)ctsio); 6817 return (CTL_RETVAL_COMPLETE); 6818} 6819 6820int 6821ctl_read_capacity(struct ctl_scsiio *ctsio) 6822{ 6823 struct scsi_read_capacity *cdb; 6824 struct scsi_read_capacity_data *data; 6825 struct ctl_lun *lun; 6826 uint32_t lba; 6827 6828 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6829 6830 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6831 6832 lba = scsi_4btoul(cdb->addr); 6833 if (((cdb->pmi & SRC_PMI) == 0) 6834 && (lba != 0)) { 6835 ctl_set_invalid_field(/*ctsio*/ ctsio, 6836 /*sks_valid*/ 1, 6837 /*command*/ 1, 6838 /*field*/ 2, 6839 /*bit_valid*/ 0, 6840 /*bit*/ 0); 6841 ctl_done((union ctl_io *)ctsio); 6842 return (CTL_RETVAL_COMPLETE); 6843 } 6844 6845 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6846 6847 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6848 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6849 ctsio->residual = 0; 6850 ctsio->kern_data_len = sizeof(*data); 6851 ctsio->kern_total_len = sizeof(*data); 6852 ctsio->kern_data_resid = 0; 6853 ctsio->kern_rel_offset = 0; 6854 ctsio->kern_sg_entries = 0; 6855 6856 /* 6857 * If the maximum LBA is greater than 0xfffffffe, the user must 6858 * issue a SERVICE ACTION IN (16) command, with the read capacity 6859 * serivce action set. 6860 */ 6861 if (lun->be_lun->maxlba > 0xfffffffe) 6862 scsi_ulto4b(0xffffffff, data->addr); 6863 else 6864 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6865 6866 /* 6867 * XXX KDM this may not be 512 bytes... 6868 */ 6869 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6870 6871 ctl_set_success(ctsio); 6872 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6873 ctsio->be_move_done = ctl_config_move_done; 6874 ctl_datamove((union ctl_io *)ctsio); 6875 return (CTL_RETVAL_COMPLETE); 6876} 6877 6878int 6879ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6880{ 6881 struct scsi_read_capacity_16 *cdb; 6882 struct scsi_read_capacity_data_long *data; 6883 struct ctl_lun *lun; 6884 uint64_t lba; 6885 uint32_t alloc_len; 6886 6887 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6888 6889 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6890 6891 alloc_len = scsi_4btoul(cdb->alloc_len); 6892 lba = scsi_8btou64(cdb->addr); 6893 6894 if ((cdb->reladr & SRC16_PMI) 6895 && (lba != 0)) { 6896 ctl_set_invalid_field(/*ctsio*/ ctsio, 6897 /*sks_valid*/ 1, 6898 /*command*/ 1, 6899 /*field*/ 2, 6900 /*bit_valid*/ 0, 6901 /*bit*/ 0); 6902 ctl_done((union ctl_io *)ctsio); 6903 return (CTL_RETVAL_COMPLETE); 6904 } 6905 6906 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6907 6908 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6909 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6910 6911 if (sizeof(*data) < alloc_len) { 6912 ctsio->residual = alloc_len - sizeof(*data); 6913 ctsio->kern_data_len = sizeof(*data); 6914 ctsio->kern_total_len = sizeof(*data); 6915 } else { 6916 ctsio->residual = 0; 6917 ctsio->kern_data_len = alloc_len; 6918 ctsio->kern_total_len = alloc_len; 6919 } 6920 ctsio->kern_data_resid = 0; 6921 ctsio->kern_rel_offset = 0; 6922 ctsio->kern_sg_entries = 0; 6923 6924 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6925 /* XXX KDM this may not be 512 bytes... */ 6926 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6927 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6928 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6929 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6930 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6931 6932 ctl_set_success(ctsio); 6933 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6934 ctsio->be_move_done = ctl_config_move_done; 6935 ctl_datamove((union ctl_io *)ctsio); 6936 return (CTL_RETVAL_COMPLETE); 6937} 6938 6939int 6940ctl_get_lba_status(struct ctl_scsiio *ctsio) 6941{ 6942 struct scsi_get_lba_status *cdb; 6943 struct scsi_get_lba_status_data *data; 6944 struct ctl_lun *lun; 6945 struct ctl_lba_len_flags *lbalen; 6946 uint64_t lba; 6947 uint32_t alloc_len, total_len; 6948 int retval; 6949 6950 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6951 6952 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6953 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6954 lba = scsi_8btou64(cdb->addr); 6955 alloc_len = scsi_4btoul(cdb->alloc_len); 6956 6957 if (lba > lun->be_lun->maxlba) { 6958 ctl_set_lba_out_of_range(ctsio); 6959 ctl_done((union ctl_io *)ctsio); 6960 return (CTL_RETVAL_COMPLETE); 6961 } 6962 6963 total_len = sizeof(*data) + sizeof(data->descr[0]); 6964 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6965 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6966 6967 if (total_len < alloc_len) { 6968 ctsio->residual = alloc_len - total_len; 6969 ctsio->kern_data_len = total_len; 6970 ctsio->kern_total_len = total_len; 6971 } else { 6972 ctsio->residual = 0; 6973 ctsio->kern_data_len = alloc_len; 6974 ctsio->kern_total_len = alloc_len; 6975 } 6976 ctsio->kern_data_resid = 0; 6977 ctsio->kern_rel_offset = 0; 6978 ctsio->kern_sg_entries = 0; 6979 6980 /* Fill dummy data in case backend can't tell anything. */ 6981 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6982 scsi_u64to8b(lba, data->descr[0].addr); 6983 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6984 data->descr[0].length); 6985 data->descr[0].status = 0; /* Mapped or unknown. */ 6986 6987 ctl_set_success(ctsio); 6988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6989 ctsio->be_move_done = ctl_config_move_done; 6990 6991 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6992 lbalen->lba = lba; 6993 lbalen->len = total_len; 6994 lbalen->flags = 0; 6995 retval = lun->backend->config_read((union ctl_io *)ctsio); 6996 return (CTL_RETVAL_COMPLETE); 6997} 6998 6999int 7000ctl_read_defect(struct ctl_scsiio *ctsio) 7001{ 7002 struct scsi_read_defect_data_10 *ccb10; 7003 struct scsi_read_defect_data_12 *ccb12; 7004 struct scsi_read_defect_data_hdr_10 *data10; 7005 struct scsi_read_defect_data_hdr_12 *data12; 7006 uint32_t alloc_len, data_len; 7007 uint8_t format; 7008 7009 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7010 7011 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7012 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7013 format = ccb10->format; 7014 alloc_len = scsi_2btoul(ccb10->alloc_length); 7015 data_len = sizeof(*data10); 7016 } else { 7017 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7018 format = ccb12->format; 7019 alloc_len = scsi_4btoul(ccb12->alloc_length); 7020 data_len = sizeof(*data12); 7021 } 7022 if (alloc_len == 0) { 7023 ctl_set_success(ctsio); 7024 ctl_done((union ctl_io *)ctsio); 7025 return (CTL_RETVAL_COMPLETE); 7026 } 7027 7028 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7029 if (data_len < alloc_len) { 7030 ctsio->residual = alloc_len - data_len; 7031 ctsio->kern_data_len = data_len; 7032 ctsio->kern_total_len = data_len; 7033 } else { 7034 ctsio->residual = 0; 7035 ctsio->kern_data_len = alloc_len; 7036 ctsio->kern_total_len = alloc_len; 7037 } 7038 ctsio->kern_data_resid = 0; 7039 ctsio->kern_rel_offset = 0; 7040 ctsio->kern_sg_entries = 0; 7041 7042 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7043 data10 = (struct scsi_read_defect_data_hdr_10 *) 7044 ctsio->kern_data_ptr; 7045 data10->format = format; 7046 scsi_ulto2b(0, data10->length); 7047 } else { 7048 data12 = (struct scsi_read_defect_data_hdr_12 *) 7049 ctsio->kern_data_ptr; 7050 data12->format = format; 7051 scsi_ulto2b(0, data12->generation); 7052 scsi_ulto4b(0, data12->length); 7053 } 7054 7055 ctl_set_success(ctsio); 7056 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7057 ctsio->be_move_done = ctl_config_move_done; 7058 ctl_datamove((union ctl_io *)ctsio); 7059 return (CTL_RETVAL_COMPLETE); 7060} 7061 7062int 7063ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7064{ 7065 struct scsi_maintenance_in *cdb; 7066 int retval; 7067 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7068 int num_target_port_groups, num_target_ports; 7069 struct ctl_lun *lun; 7070 struct ctl_softc *softc; 7071 struct ctl_port *port; 7072 struct scsi_target_group_data *rtg_ptr; 7073 struct scsi_target_group_data_extended *rtg_ext_ptr; 7074 struct scsi_target_port_group_descriptor *tpg_desc; 7075 7076 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7077 7078 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7080 softc = lun->ctl_softc; 7081 7082 retval = CTL_RETVAL_COMPLETE; 7083 7084 switch (cdb->byte2 & STG_PDF_MASK) { 7085 case STG_PDF_LENGTH: 7086 ext = 0; 7087 break; 7088 case STG_PDF_EXTENDED: 7089 ext = 1; 7090 break; 7091 default: 7092 ctl_set_invalid_field(/*ctsio*/ ctsio, 7093 /*sks_valid*/ 1, 7094 /*command*/ 1, 7095 /*field*/ 2, 7096 /*bit_valid*/ 1, 7097 /*bit*/ 5); 7098 ctl_done((union ctl_io *)ctsio); 7099 return(retval); 7100 } 7101 7102 if (softc->is_single) 7103 num_target_port_groups = 1; 7104 else 7105 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7106 num_target_ports = 0; 7107 mtx_lock(&softc->ctl_lock); 7108 STAILQ_FOREACH(port, &softc->port_list, links) { 7109 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7110 continue; 7111 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7112 continue; 7113 num_target_ports++; 7114 } 7115 mtx_unlock(&softc->ctl_lock); 7116 7117 if (ext) 7118 total_len = sizeof(struct scsi_target_group_data_extended); 7119 else 7120 total_len = sizeof(struct scsi_target_group_data); 7121 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7122 num_target_port_groups + 7123 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7124 7125 alloc_len = scsi_4btoul(cdb->length); 7126 7127 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7128 7129 ctsio->kern_sg_entries = 0; 7130 7131 if (total_len < alloc_len) { 7132 ctsio->residual = alloc_len - total_len; 7133 ctsio->kern_data_len = total_len; 7134 ctsio->kern_total_len = total_len; 7135 } else { 7136 ctsio->residual = 0; 7137 ctsio->kern_data_len = alloc_len; 7138 ctsio->kern_total_len = alloc_len; 7139 } 7140 ctsio->kern_data_resid = 0; 7141 ctsio->kern_rel_offset = 0; 7142 7143 if (ext) { 7144 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7145 ctsio->kern_data_ptr; 7146 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7147 rtg_ext_ptr->format_type = 0x10; 7148 rtg_ext_ptr->implicit_transition_time = 0; 7149 tpg_desc = &rtg_ext_ptr->groups[0]; 7150 } else { 7151 rtg_ptr = (struct scsi_target_group_data *) 7152 ctsio->kern_data_ptr; 7153 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7154 tpg_desc = &rtg_ptr->groups[0]; 7155 } 7156 7157 mtx_lock(&softc->ctl_lock); 7158 pg = softc->port_min / softc->port_cnt; 7159 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7160 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7161 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7162 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7163 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7164 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7165 else 7166 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7167 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7168 os = gs; 7169 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7170 } else 7171 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7172 for (g = 0; g < num_target_port_groups; g++) { 7173 tpg_desc->pref_state = (g == pg) ? gs : os; 7174 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7175 TPG_U_SUP | TPG_T_SUP; 7176 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7177 tpg_desc->status = TPG_IMPLICIT; 7178 pc = 0; 7179 STAILQ_FOREACH(port, &softc->port_list, links) { 7180 if (port->targ_port < g * softc->port_cnt || 7181 port->targ_port >= (g + 1) * softc->port_cnt) 7182 continue; 7183 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7184 continue; 7185 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7186 continue; 7187 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7188 relative_target_port_identifier); 7189 pc++; 7190 } 7191 tpg_desc->target_port_count = pc; 7192 tpg_desc = (struct scsi_target_port_group_descriptor *) 7193 &tpg_desc->descriptors[pc]; 7194 } 7195 mtx_unlock(&softc->ctl_lock); 7196 7197 ctl_set_success(ctsio); 7198 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7199 ctsio->be_move_done = ctl_config_move_done; 7200 ctl_datamove((union ctl_io *)ctsio); 7201 return(retval); 7202} 7203 7204int 7205ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7206{ 7207 struct ctl_lun *lun; 7208 struct scsi_report_supported_opcodes *cdb; 7209 const struct ctl_cmd_entry *entry, *sentry; 7210 struct scsi_report_supported_opcodes_all *all; 7211 struct scsi_report_supported_opcodes_descr *descr; 7212 struct scsi_report_supported_opcodes_one *one; 7213 int retval; 7214 int alloc_len, total_len; 7215 int opcode, service_action, i, j, num; 7216 7217 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7218 7219 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7220 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7221 7222 retval = CTL_RETVAL_COMPLETE; 7223 7224 opcode = cdb->requested_opcode; 7225 service_action = scsi_2btoul(cdb->requested_service_action); 7226 switch (cdb->options & RSO_OPTIONS_MASK) { 7227 case RSO_OPTIONS_ALL: 7228 num = 0; 7229 for (i = 0; i < 256; i++) { 7230 entry = &ctl_cmd_table[i]; 7231 if (entry->flags & CTL_CMD_FLAG_SA5) { 7232 for (j = 0; j < 32; j++) { 7233 sentry = &((const struct ctl_cmd_entry *) 7234 entry->execute)[j]; 7235 if (ctl_cmd_applicable( 7236 lun->be_lun->lun_type, sentry)) 7237 num++; 7238 } 7239 } else { 7240 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7241 entry)) 7242 num++; 7243 } 7244 } 7245 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7246 num * sizeof(struct scsi_report_supported_opcodes_descr); 7247 break; 7248 case RSO_OPTIONS_OC: 7249 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7250 ctl_set_invalid_field(/*ctsio*/ ctsio, 7251 /*sks_valid*/ 1, 7252 /*command*/ 1, 7253 /*field*/ 2, 7254 /*bit_valid*/ 1, 7255 /*bit*/ 2); 7256 ctl_done((union ctl_io *)ctsio); 7257 return (CTL_RETVAL_COMPLETE); 7258 } 7259 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7260 break; 7261 case RSO_OPTIONS_OC_SA: 7262 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7263 service_action >= 32) { 7264 ctl_set_invalid_field(/*ctsio*/ ctsio, 7265 /*sks_valid*/ 1, 7266 /*command*/ 1, 7267 /*field*/ 2, 7268 /*bit_valid*/ 1, 7269 /*bit*/ 2); 7270 ctl_done((union ctl_io *)ctsio); 7271 return (CTL_RETVAL_COMPLETE); 7272 } 7273 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7274 break; 7275 default: 7276 ctl_set_invalid_field(/*ctsio*/ ctsio, 7277 /*sks_valid*/ 1, 7278 /*command*/ 1, 7279 /*field*/ 2, 7280 /*bit_valid*/ 1, 7281 /*bit*/ 2); 7282 ctl_done((union ctl_io *)ctsio); 7283 return (CTL_RETVAL_COMPLETE); 7284 } 7285 7286 alloc_len = scsi_4btoul(cdb->length); 7287 7288 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7289 7290 ctsio->kern_sg_entries = 0; 7291 7292 if (total_len < alloc_len) { 7293 ctsio->residual = alloc_len - total_len; 7294 ctsio->kern_data_len = total_len; 7295 ctsio->kern_total_len = total_len; 7296 } else { 7297 ctsio->residual = 0; 7298 ctsio->kern_data_len = alloc_len; 7299 ctsio->kern_total_len = alloc_len; 7300 } 7301 ctsio->kern_data_resid = 0; 7302 ctsio->kern_rel_offset = 0; 7303 7304 switch (cdb->options & RSO_OPTIONS_MASK) { 7305 case RSO_OPTIONS_ALL: 7306 all = (struct scsi_report_supported_opcodes_all *) 7307 ctsio->kern_data_ptr; 7308 num = 0; 7309 for (i = 0; i < 256; i++) { 7310 entry = &ctl_cmd_table[i]; 7311 if (entry->flags & CTL_CMD_FLAG_SA5) { 7312 for (j = 0; j < 32; j++) { 7313 sentry = &((const struct ctl_cmd_entry *) 7314 entry->execute)[j]; 7315 if (!ctl_cmd_applicable( 7316 lun->be_lun->lun_type, sentry)) 7317 continue; 7318 descr = &all->descr[num++]; 7319 descr->opcode = i; 7320 scsi_ulto2b(j, descr->service_action); 7321 descr->flags = RSO_SERVACTV; 7322 scsi_ulto2b(sentry->length, 7323 descr->cdb_length); 7324 } 7325 } else { 7326 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7327 entry)) 7328 continue; 7329 descr = &all->descr[num++]; 7330 descr->opcode = i; 7331 scsi_ulto2b(0, descr->service_action); 7332 descr->flags = 0; 7333 scsi_ulto2b(entry->length, descr->cdb_length); 7334 } 7335 } 7336 scsi_ulto4b( 7337 num * sizeof(struct scsi_report_supported_opcodes_descr), 7338 all->length); 7339 break; 7340 case RSO_OPTIONS_OC: 7341 one = (struct scsi_report_supported_opcodes_one *) 7342 ctsio->kern_data_ptr; 7343 entry = &ctl_cmd_table[opcode]; 7344 goto fill_one; 7345 case RSO_OPTIONS_OC_SA: 7346 one = (struct scsi_report_supported_opcodes_one *) 7347 ctsio->kern_data_ptr; 7348 entry = &ctl_cmd_table[opcode]; 7349 entry = &((const struct ctl_cmd_entry *) 7350 entry->execute)[service_action]; 7351fill_one: 7352 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7353 one->support = 3; 7354 scsi_ulto2b(entry->length, one->cdb_length); 7355 one->cdb_usage[0] = opcode; 7356 memcpy(&one->cdb_usage[1], entry->usage, 7357 entry->length - 1); 7358 } else 7359 one->support = 1; 7360 break; 7361 } 7362 7363 ctl_set_success(ctsio); 7364 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7365 ctsio->be_move_done = ctl_config_move_done; 7366 ctl_datamove((union ctl_io *)ctsio); 7367 return(retval); 7368} 7369 7370int 7371ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7372{ 7373 struct scsi_report_supported_tmf *cdb; 7374 struct scsi_report_supported_tmf_data *data; 7375 int retval; 7376 int alloc_len, total_len; 7377 7378 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7379 7380 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7381 7382 retval = CTL_RETVAL_COMPLETE; 7383 7384 total_len = sizeof(struct scsi_report_supported_tmf_data); 7385 alloc_len = scsi_4btoul(cdb->length); 7386 7387 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7388 7389 ctsio->kern_sg_entries = 0; 7390 7391 if (total_len < alloc_len) { 7392 ctsio->residual = alloc_len - total_len; 7393 ctsio->kern_data_len = total_len; 7394 ctsio->kern_total_len = total_len; 7395 } else { 7396 ctsio->residual = 0; 7397 ctsio->kern_data_len = alloc_len; 7398 ctsio->kern_total_len = alloc_len; 7399 } 7400 ctsio->kern_data_resid = 0; 7401 ctsio->kern_rel_offset = 0; 7402 7403 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7404 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7405 RST_TRS; 7406 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7407 7408 ctl_set_success(ctsio); 7409 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7410 ctsio->be_move_done = ctl_config_move_done; 7411 ctl_datamove((union ctl_io *)ctsio); 7412 return (retval); 7413} 7414 7415int 7416ctl_report_timestamp(struct ctl_scsiio *ctsio) 7417{ 7418 struct scsi_report_timestamp *cdb; 7419 struct scsi_report_timestamp_data *data; 7420 struct timeval tv; 7421 int64_t timestamp; 7422 int retval; 7423 int alloc_len, total_len; 7424 7425 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7426 7427 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7428 7429 retval = CTL_RETVAL_COMPLETE; 7430 7431 total_len = sizeof(struct scsi_report_timestamp_data); 7432 alloc_len = scsi_4btoul(cdb->length); 7433 7434 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7435 7436 ctsio->kern_sg_entries = 0; 7437 7438 if (total_len < alloc_len) { 7439 ctsio->residual = alloc_len - total_len; 7440 ctsio->kern_data_len = total_len; 7441 ctsio->kern_total_len = total_len; 7442 } else { 7443 ctsio->residual = 0; 7444 ctsio->kern_data_len = alloc_len; 7445 ctsio->kern_total_len = alloc_len; 7446 } 7447 ctsio->kern_data_resid = 0; 7448 ctsio->kern_rel_offset = 0; 7449 7450 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7451 scsi_ulto2b(sizeof(*data) - 2, data->length); 7452 data->origin = RTS_ORIG_OUTSIDE; 7453 getmicrotime(&tv); 7454 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7455 scsi_ulto4b(timestamp >> 16, data->timestamp); 7456 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7457 7458 ctl_set_success(ctsio); 7459 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7460 ctsio->be_move_done = ctl_config_move_done; 7461 ctl_datamove((union ctl_io *)ctsio); 7462 return (retval); 7463} 7464 7465int 7466ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7467{ 7468 struct scsi_per_res_in *cdb; 7469 int alloc_len, total_len = 0; 7470 /* struct scsi_per_res_in_rsrv in_data; */ 7471 struct ctl_lun *lun; 7472 struct ctl_softc *softc; 7473 uint64_t key; 7474 7475 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7476 7477 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7478 7479 alloc_len = scsi_2btoul(cdb->length); 7480 7481 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7482 softc = lun->ctl_softc; 7483 7484retry: 7485 mtx_lock(&lun->lun_lock); 7486 switch (cdb->action) { 7487 case SPRI_RK: /* read keys */ 7488 total_len = sizeof(struct scsi_per_res_in_keys) + 7489 lun->pr_key_count * 7490 sizeof(struct scsi_per_res_key); 7491 break; 7492 case SPRI_RR: /* read reservation */ 7493 if (lun->flags & CTL_LUN_PR_RESERVED) 7494 total_len = sizeof(struct scsi_per_res_in_rsrv); 7495 else 7496 total_len = sizeof(struct scsi_per_res_in_header); 7497 break; 7498 case SPRI_RC: /* report capabilities */ 7499 total_len = sizeof(struct scsi_per_res_cap); 7500 break; 7501 case SPRI_RS: /* read full status */ 7502 total_len = sizeof(struct scsi_per_res_in_header) + 7503 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7504 lun->pr_key_count; 7505 break; 7506 default: 7507 panic("Invalid PR type %x", cdb->action); 7508 } 7509 mtx_unlock(&lun->lun_lock); 7510 7511 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7512 7513 if (total_len < alloc_len) { 7514 ctsio->residual = alloc_len - total_len; 7515 ctsio->kern_data_len = total_len; 7516 ctsio->kern_total_len = total_len; 7517 } else { 7518 ctsio->residual = 0; 7519 ctsio->kern_data_len = alloc_len; 7520 ctsio->kern_total_len = alloc_len; 7521 } 7522 7523 ctsio->kern_data_resid = 0; 7524 ctsio->kern_rel_offset = 0; 7525 ctsio->kern_sg_entries = 0; 7526 7527 mtx_lock(&lun->lun_lock); 7528 switch (cdb->action) { 7529 case SPRI_RK: { // read keys 7530 struct scsi_per_res_in_keys *res_keys; 7531 int i, key_count; 7532 7533 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7534 7535 /* 7536 * We had to drop the lock to allocate our buffer, which 7537 * leaves time for someone to come in with another 7538 * persistent reservation. (That is unlikely, though, 7539 * since this should be the only persistent reservation 7540 * command active right now.) 7541 */ 7542 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7543 (lun->pr_key_count * 7544 sizeof(struct scsi_per_res_key)))){ 7545 mtx_unlock(&lun->lun_lock); 7546 free(ctsio->kern_data_ptr, M_CTL); 7547 printf("%s: reservation length changed, retrying\n", 7548 __func__); 7549 goto retry; 7550 } 7551 7552 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7553 7554 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7555 lun->pr_key_count, res_keys->header.length); 7556 7557 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7558 if ((key = ctl_get_prkey(lun, i)) == 0) 7559 continue; 7560 7561 /* 7562 * We used lun->pr_key_count to calculate the 7563 * size to allocate. If it turns out the number of 7564 * initiators with the registered flag set is 7565 * larger than that (i.e. they haven't been kept in 7566 * sync), we've got a problem. 7567 */ 7568 if (key_count >= lun->pr_key_count) { 7569 key_count++; 7570 continue; 7571 } 7572 scsi_u64to8b(key, res_keys->keys[key_count].key); 7573 key_count++; 7574 } 7575 break; 7576 } 7577 case SPRI_RR: { // read reservation 7578 struct scsi_per_res_in_rsrv *res; 7579 int tmp_len, header_only; 7580 7581 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7582 7583 scsi_ulto4b(lun->pr_generation, res->header.generation); 7584 7585 if (lun->flags & CTL_LUN_PR_RESERVED) 7586 { 7587 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7588 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7589 res->header.length); 7590 header_only = 0; 7591 } else { 7592 tmp_len = sizeof(struct scsi_per_res_in_header); 7593 scsi_ulto4b(0, res->header.length); 7594 header_only = 1; 7595 } 7596 7597 /* 7598 * We had to drop the lock to allocate our buffer, which 7599 * leaves time for someone to come in with another 7600 * persistent reservation. (That is unlikely, though, 7601 * since this should be the only persistent reservation 7602 * command active right now.) 7603 */ 7604 if (tmp_len != total_len) { 7605 mtx_unlock(&lun->lun_lock); 7606 free(ctsio->kern_data_ptr, M_CTL); 7607 printf("%s: reservation status changed, retrying\n", 7608 __func__); 7609 goto retry; 7610 } 7611 7612 /* 7613 * No reservation held, so we're done. 7614 */ 7615 if (header_only != 0) 7616 break; 7617 7618 /* 7619 * If the registration is an All Registrants type, the key 7620 * is 0, since it doesn't really matter. 7621 */ 7622 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7623 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7624 res->data.reservation); 7625 } 7626 res->data.scopetype = lun->pr_res_type; 7627 break; 7628 } 7629 case SPRI_RC: //report capabilities 7630 { 7631 struct scsi_per_res_cap *res_cap; 7632 uint16_t type_mask; 7633 7634 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7635 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7636 res_cap->flags1 = SPRI_CRH; 7637 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7638 type_mask = SPRI_TM_WR_EX_AR | 7639 SPRI_TM_EX_AC_RO | 7640 SPRI_TM_WR_EX_RO | 7641 SPRI_TM_EX_AC | 7642 SPRI_TM_WR_EX | 7643 SPRI_TM_EX_AC_AR; 7644 scsi_ulto2b(type_mask, res_cap->type_mask); 7645 break; 7646 } 7647 case SPRI_RS: { // read full status 7648 struct scsi_per_res_in_full *res_status; 7649 struct scsi_per_res_in_full_desc *res_desc; 7650 struct ctl_port *port; 7651 int i, len; 7652 7653 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7654 7655 /* 7656 * We had to drop the lock to allocate our buffer, which 7657 * leaves time for someone to come in with another 7658 * persistent reservation. (That is unlikely, though, 7659 * since this should be the only persistent reservation 7660 * command active right now.) 7661 */ 7662 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7663 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7664 lun->pr_key_count)){ 7665 mtx_unlock(&lun->lun_lock); 7666 free(ctsio->kern_data_ptr, M_CTL); 7667 printf("%s: reservation length changed, retrying\n", 7668 __func__); 7669 goto retry; 7670 } 7671 7672 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7673 7674 res_desc = &res_status->desc[0]; 7675 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7676 if ((key = ctl_get_prkey(lun, i)) == 0) 7677 continue; 7678 7679 scsi_u64to8b(key, res_desc->res_key.key); 7680 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7681 (lun->pr_res_idx == i || 7682 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7683 res_desc->flags = SPRI_FULL_R_HOLDER; 7684 res_desc->scopetype = lun->pr_res_type; 7685 } 7686 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7687 res_desc->rel_trgt_port_id); 7688 len = 0; 7689 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7690 if (port != NULL) 7691 len = ctl_create_iid(port, 7692 i % CTL_MAX_INIT_PER_PORT, 7693 res_desc->transport_id); 7694 scsi_ulto4b(len, res_desc->additional_length); 7695 res_desc = (struct scsi_per_res_in_full_desc *) 7696 &res_desc->transport_id[len]; 7697 } 7698 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7699 res_status->header.length); 7700 break; 7701 } 7702 default: 7703 /* 7704 * This is a bug, because we just checked for this above, 7705 * and should have returned an error. 7706 */ 7707 panic("Invalid PR type %x", cdb->action); 7708 break; /* NOTREACHED */ 7709 } 7710 mtx_unlock(&lun->lun_lock); 7711 7712 ctl_set_success(ctsio); 7713 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7714 ctsio->be_move_done = ctl_config_move_done; 7715 ctl_datamove((union ctl_io *)ctsio); 7716 return (CTL_RETVAL_COMPLETE); 7717} 7718 7719/* 7720 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7721 * it should return. 7722 */ 7723static int 7724ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7725 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7726 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7727 struct scsi_per_res_out_parms* param) 7728{ 7729 union ctl_ha_msg persis_io; 7730 int i; 7731 7732 mtx_lock(&lun->lun_lock); 7733 if (sa_res_key == 0) { 7734 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7735 /* validate scope and type */ 7736 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7737 SPR_LU_SCOPE) { 7738 mtx_unlock(&lun->lun_lock); 7739 ctl_set_invalid_field(/*ctsio*/ ctsio, 7740 /*sks_valid*/ 1, 7741 /*command*/ 1, 7742 /*field*/ 2, 7743 /*bit_valid*/ 1, 7744 /*bit*/ 4); 7745 ctl_done((union ctl_io *)ctsio); 7746 return (1); 7747 } 7748 7749 if (type>8 || type==2 || type==4 || type==0) { 7750 mtx_unlock(&lun->lun_lock); 7751 ctl_set_invalid_field(/*ctsio*/ ctsio, 7752 /*sks_valid*/ 1, 7753 /*command*/ 1, 7754 /*field*/ 2, 7755 /*bit_valid*/ 1, 7756 /*bit*/ 0); 7757 ctl_done((union ctl_io *)ctsio); 7758 return (1); 7759 } 7760 7761 /* 7762 * Unregister everybody else and build UA for 7763 * them 7764 */ 7765 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7766 if (i == residx || ctl_get_prkey(lun, i) == 0) 7767 continue; 7768 7769 ctl_clr_prkey(lun, i); 7770 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7771 } 7772 lun->pr_key_count = 1; 7773 lun->pr_res_type = type; 7774 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7775 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7776 lun->pr_res_idx = residx; 7777 lun->pr_generation++; 7778 mtx_unlock(&lun->lun_lock); 7779 7780 /* send msg to other side */ 7781 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7782 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7783 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7784 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7785 persis_io.pr.pr_info.res_type = type; 7786 memcpy(persis_io.pr.pr_info.sa_res_key, 7787 param->serv_act_res_key, 7788 sizeof(param->serv_act_res_key)); 7789 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7790 sizeof(persis_io.pr), M_WAITOK); 7791 } else { 7792 /* not all registrants */ 7793 mtx_unlock(&lun->lun_lock); 7794 free(ctsio->kern_data_ptr, M_CTL); 7795 ctl_set_invalid_field(ctsio, 7796 /*sks_valid*/ 1, 7797 /*command*/ 0, 7798 /*field*/ 8, 7799 /*bit_valid*/ 0, 7800 /*bit*/ 0); 7801 ctl_done((union ctl_io *)ctsio); 7802 return (1); 7803 } 7804 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7805 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7806 int found = 0; 7807 7808 if (res_key == sa_res_key) { 7809 /* special case */ 7810 /* 7811 * The spec implies this is not good but doesn't 7812 * say what to do. There are two choices either 7813 * generate a res conflict or check condition 7814 * with illegal field in parameter data. Since 7815 * that is what is done when the sa_res_key is 7816 * zero I'll take that approach since this has 7817 * to do with the sa_res_key. 7818 */ 7819 mtx_unlock(&lun->lun_lock); 7820 free(ctsio->kern_data_ptr, M_CTL); 7821 ctl_set_invalid_field(ctsio, 7822 /*sks_valid*/ 1, 7823 /*command*/ 0, 7824 /*field*/ 8, 7825 /*bit_valid*/ 0, 7826 /*bit*/ 0); 7827 ctl_done((union ctl_io *)ctsio); 7828 return (1); 7829 } 7830 7831 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7832 if (ctl_get_prkey(lun, i) != sa_res_key) 7833 continue; 7834 7835 found = 1; 7836 ctl_clr_prkey(lun, i); 7837 lun->pr_key_count--; 7838 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7839 } 7840 if (!found) { 7841 mtx_unlock(&lun->lun_lock); 7842 free(ctsio->kern_data_ptr, M_CTL); 7843 ctl_set_reservation_conflict(ctsio); 7844 ctl_done((union ctl_io *)ctsio); 7845 return (CTL_RETVAL_COMPLETE); 7846 } 7847 lun->pr_generation++; 7848 mtx_unlock(&lun->lun_lock); 7849 7850 /* send msg to other side */ 7851 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7852 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7853 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7854 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7855 persis_io.pr.pr_info.res_type = type; 7856 memcpy(persis_io.pr.pr_info.sa_res_key, 7857 param->serv_act_res_key, 7858 sizeof(param->serv_act_res_key)); 7859 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7860 sizeof(persis_io.pr), M_WAITOK); 7861 } else { 7862 /* Reserved but not all registrants */ 7863 /* sa_res_key is res holder */ 7864 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7865 /* validate scope and type */ 7866 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7867 SPR_LU_SCOPE) { 7868 mtx_unlock(&lun->lun_lock); 7869 ctl_set_invalid_field(/*ctsio*/ ctsio, 7870 /*sks_valid*/ 1, 7871 /*command*/ 1, 7872 /*field*/ 2, 7873 /*bit_valid*/ 1, 7874 /*bit*/ 4); 7875 ctl_done((union ctl_io *)ctsio); 7876 return (1); 7877 } 7878 7879 if (type>8 || type==2 || type==4 || type==0) { 7880 mtx_unlock(&lun->lun_lock); 7881 ctl_set_invalid_field(/*ctsio*/ ctsio, 7882 /*sks_valid*/ 1, 7883 /*command*/ 1, 7884 /*field*/ 2, 7885 /*bit_valid*/ 1, 7886 /*bit*/ 0); 7887 ctl_done((union ctl_io *)ctsio); 7888 return (1); 7889 } 7890 7891 /* 7892 * Do the following: 7893 * if sa_res_key != res_key remove all 7894 * registrants w/sa_res_key and generate UA 7895 * for these registrants(Registrations 7896 * Preempted) if it wasn't an exclusive 7897 * reservation generate UA(Reservations 7898 * Preempted) for all other registered nexuses 7899 * if the type has changed. Establish the new 7900 * reservation and holder. If res_key and 7901 * sa_res_key are the same do the above 7902 * except don't unregister the res holder. 7903 */ 7904 7905 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7906 if (i == residx || ctl_get_prkey(lun, i) == 0) 7907 continue; 7908 7909 if (sa_res_key == ctl_get_prkey(lun, i)) { 7910 ctl_clr_prkey(lun, i); 7911 lun->pr_key_count--; 7912 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7913 } else if (type != lun->pr_res_type && 7914 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 7915 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 7916 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7917 } 7918 } 7919 lun->pr_res_type = type; 7920 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7921 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7922 lun->pr_res_idx = residx; 7923 else 7924 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7925 lun->pr_generation++; 7926 mtx_unlock(&lun->lun_lock); 7927 7928 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7929 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7930 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7931 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7932 persis_io.pr.pr_info.res_type = type; 7933 memcpy(persis_io.pr.pr_info.sa_res_key, 7934 param->serv_act_res_key, 7935 sizeof(param->serv_act_res_key)); 7936 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7937 sizeof(persis_io.pr), M_WAITOK); 7938 } else { 7939 /* 7940 * sa_res_key is not the res holder just 7941 * remove registrants 7942 */ 7943 int found=0; 7944 7945 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7946 if (sa_res_key != ctl_get_prkey(lun, i)) 7947 continue; 7948 7949 found = 1; 7950 ctl_clr_prkey(lun, i); 7951 lun->pr_key_count--; 7952 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7953 } 7954 7955 if (!found) { 7956 mtx_unlock(&lun->lun_lock); 7957 free(ctsio->kern_data_ptr, M_CTL); 7958 ctl_set_reservation_conflict(ctsio); 7959 ctl_done((union ctl_io *)ctsio); 7960 return (1); 7961 } 7962 lun->pr_generation++; 7963 mtx_unlock(&lun->lun_lock); 7964 7965 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7966 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7967 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7968 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7969 persis_io.pr.pr_info.res_type = type; 7970 memcpy(persis_io.pr.pr_info.sa_res_key, 7971 param->serv_act_res_key, 7972 sizeof(param->serv_act_res_key)); 7973 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7974 sizeof(persis_io.pr), M_WAITOK); 7975 } 7976 } 7977 return (0); 7978} 7979 7980static void 7981ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7982{ 7983 uint64_t sa_res_key; 7984 int i; 7985 7986 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7987 7988 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7989 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7990 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7991 if (sa_res_key == 0) { 7992 /* 7993 * Unregister everybody else and build UA for 7994 * them 7995 */ 7996 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7997 if (i == msg->pr.pr_info.residx || 7998 ctl_get_prkey(lun, i) == 0) 7999 continue; 8000 8001 ctl_clr_prkey(lun, i); 8002 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8003 } 8004 8005 lun->pr_key_count = 1; 8006 lun->pr_res_type = msg->pr.pr_info.res_type; 8007 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8008 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8009 lun->pr_res_idx = msg->pr.pr_info.residx; 8010 } else { 8011 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8012 if (sa_res_key == ctl_get_prkey(lun, i)) 8013 continue; 8014 8015 ctl_clr_prkey(lun, i); 8016 lun->pr_key_count--; 8017 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8018 } 8019 } 8020 } else { 8021 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8022 if (i == msg->pr.pr_info.residx || 8023 ctl_get_prkey(lun, i) == 0) 8024 continue; 8025 8026 if (sa_res_key == ctl_get_prkey(lun, i)) { 8027 ctl_clr_prkey(lun, i); 8028 lun->pr_key_count--; 8029 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8030 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8031 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8032 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8033 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8034 } 8035 } 8036 lun->pr_res_type = msg->pr.pr_info.res_type; 8037 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8038 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8039 lun->pr_res_idx = msg->pr.pr_info.residx; 8040 else 8041 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8042 } 8043 lun->pr_generation++; 8044 8045} 8046 8047 8048int 8049ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8050{ 8051 int retval; 8052 u_int32_t param_len; 8053 struct scsi_per_res_out *cdb; 8054 struct ctl_lun *lun; 8055 struct scsi_per_res_out_parms* param; 8056 struct ctl_softc *softc; 8057 uint32_t residx; 8058 uint64_t res_key, sa_res_key, key; 8059 uint8_t type; 8060 union ctl_ha_msg persis_io; 8061 int i; 8062 8063 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8064 8065 retval = CTL_RETVAL_COMPLETE; 8066 8067 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8068 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8069 softc = lun->ctl_softc; 8070 8071 /* 8072 * We only support whole-LUN scope. The scope & type are ignored for 8073 * register, register and ignore existing key and clear. 8074 * We sometimes ignore scope and type on preempts too!! 8075 * Verify reservation type here as well. 8076 */ 8077 type = cdb->scope_type & SPR_TYPE_MASK; 8078 if ((cdb->action == SPRO_RESERVE) 8079 || (cdb->action == SPRO_RELEASE)) { 8080 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8081 ctl_set_invalid_field(/*ctsio*/ ctsio, 8082 /*sks_valid*/ 1, 8083 /*command*/ 1, 8084 /*field*/ 2, 8085 /*bit_valid*/ 1, 8086 /*bit*/ 4); 8087 ctl_done((union ctl_io *)ctsio); 8088 return (CTL_RETVAL_COMPLETE); 8089 } 8090 8091 if (type>8 || type==2 || type==4 || type==0) { 8092 ctl_set_invalid_field(/*ctsio*/ ctsio, 8093 /*sks_valid*/ 1, 8094 /*command*/ 1, 8095 /*field*/ 2, 8096 /*bit_valid*/ 1, 8097 /*bit*/ 0); 8098 ctl_done((union ctl_io *)ctsio); 8099 return (CTL_RETVAL_COMPLETE); 8100 } 8101 } 8102 8103 param_len = scsi_4btoul(cdb->length); 8104 8105 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8106 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8107 ctsio->kern_data_len = param_len; 8108 ctsio->kern_total_len = param_len; 8109 ctsio->kern_data_resid = 0; 8110 ctsio->kern_rel_offset = 0; 8111 ctsio->kern_sg_entries = 0; 8112 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8113 ctsio->be_move_done = ctl_config_move_done; 8114 ctl_datamove((union ctl_io *)ctsio); 8115 8116 return (CTL_RETVAL_COMPLETE); 8117 } 8118 8119 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8120 8121 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8122 res_key = scsi_8btou64(param->res_key.key); 8123 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8124 8125 /* 8126 * Validate the reservation key here except for SPRO_REG_IGNO 8127 * This must be done for all other service actions 8128 */ 8129 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8130 mtx_lock(&lun->lun_lock); 8131 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8132 if (res_key != key) { 8133 /* 8134 * The current key passed in doesn't match 8135 * the one the initiator previously 8136 * registered. 8137 */ 8138 mtx_unlock(&lun->lun_lock); 8139 free(ctsio->kern_data_ptr, M_CTL); 8140 ctl_set_reservation_conflict(ctsio); 8141 ctl_done((union ctl_io *)ctsio); 8142 return (CTL_RETVAL_COMPLETE); 8143 } 8144 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8145 /* 8146 * We are not registered 8147 */ 8148 mtx_unlock(&lun->lun_lock); 8149 free(ctsio->kern_data_ptr, M_CTL); 8150 ctl_set_reservation_conflict(ctsio); 8151 ctl_done((union ctl_io *)ctsio); 8152 return (CTL_RETVAL_COMPLETE); 8153 } else if (res_key != 0) { 8154 /* 8155 * We are not registered and trying to register but 8156 * the register key isn't zero. 8157 */ 8158 mtx_unlock(&lun->lun_lock); 8159 free(ctsio->kern_data_ptr, M_CTL); 8160 ctl_set_reservation_conflict(ctsio); 8161 ctl_done((union ctl_io *)ctsio); 8162 return (CTL_RETVAL_COMPLETE); 8163 } 8164 mtx_unlock(&lun->lun_lock); 8165 } 8166 8167 switch (cdb->action & SPRO_ACTION_MASK) { 8168 case SPRO_REGISTER: 8169 case SPRO_REG_IGNO: { 8170 8171#if 0 8172 printf("Registration received\n"); 8173#endif 8174 8175 /* 8176 * We don't support any of these options, as we report in 8177 * the read capabilities request (see 8178 * ctl_persistent_reserve_in(), above). 8179 */ 8180 if ((param->flags & SPR_SPEC_I_PT) 8181 || (param->flags & SPR_ALL_TG_PT) 8182 || (param->flags & SPR_APTPL)) { 8183 int bit_ptr; 8184 8185 if (param->flags & SPR_APTPL) 8186 bit_ptr = 0; 8187 else if (param->flags & SPR_ALL_TG_PT) 8188 bit_ptr = 2; 8189 else /* SPR_SPEC_I_PT */ 8190 bit_ptr = 3; 8191 8192 free(ctsio->kern_data_ptr, M_CTL); 8193 ctl_set_invalid_field(ctsio, 8194 /*sks_valid*/ 1, 8195 /*command*/ 0, 8196 /*field*/ 20, 8197 /*bit_valid*/ 1, 8198 /*bit*/ bit_ptr); 8199 ctl_done((union ctl_io *)ctsio); 8200 return (CTL_RETVAL_COMPLETE); 8201 } 8202 8203 mtx_lock(&lun->lun_lock); 8204 8205 /* 8206 * The initiator wants to clear the 8207 * key/unregister. 8208 */ 8209 if (sa_res_key == 0) { 8210 if ((res_key == 0 8211 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8212 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8213 && ctl_get_prkey(lun, residx) == 0)) { 8214 mtx_unlock(&lun->lun_lock); 8215 goto done; 8216 } 8217 8218 ctl_clr_prkey(lun, residx); 8219 lun->pr_key_count--; 8220 8221 if (residx == lun->pr_res_idx) { 8222 lun->flags &= ~CTL_LUN_PR_RESERVED; 8223 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8224 8225 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8226 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8227 lun->pr_key_count) { 8228 /* 8229 * If the reservation is a registrants 8230 * only type we need to generate a UA 8231 * for other registered inits. The 8232 * sense code should be RESERVATIONS 8233 * RELEASED 8234 */ 8235 8236 for (i = softc->init_min; i < softc->init_max; i++){ 8237 if (ctl_get_prkey(lun, i) == 0) 8238 continue; 8239 ctl_est_ua(lun, i, 8240 CTL_UA_RES_RELEASE); 8241 } 8242 } 8243 lun->pr_res_type = 0; 8244 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8245 if (lun->pr_key_count==0) { 8246 lun->flags &= ~CTL_LUN_PR_RESERVED; 8247 lun->pr_res_type = 0; 8248 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8249 } 8250 } 8251 lun->pr_generation++; 8252 mtx_unlock(&lun->lun_lock); 8253 8254 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8255 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8256 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8257 persis_io.pr.pr_info.residx = residx; 8258 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8259 sizeof(persis_io.pr), M_WAITOK); 8260 } else /* sa_res_key != 0 */ { 8261 8262 /* 8263 * If we aren't registered currently then increment 8264 * the key count and set the registered flag. 8265 */ 8266 ctl_alloc_prkey(lun, residx); 8267 if (ctl_get_prkey(lun, residx) == 0) 8268 lun->pr_key_count++; 8269 ctl_set_prkey(lun, residx, sa_res_key); 8270 lun->pr_generation++; 8271 mtx_unlock(&lun->lun_lock); 8272 8273 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8274 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8275 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8276 persis_io.pr.pr_info.residx = residx; 8277 memcpy(persis_io.pr.pr_info.sa_res_key, 8278 param->serv_act_res_key, 8279 sizeof(param->serv_act_res_key)); 8280 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8281 sizeof(persis_io.pr), M_WAITOK); 8282 } 8283 8284 break; 8285 } 8286 case SPRO_RESERVE: 8287#if 0 8288 printf("Reserve executed type %d\n", type); 8289#endif 8290 mtx_lock(&lun->lun_lock); 8291 if (lun->flags & CTL_LUN_PR_RESERVED) { 8292 /* 8293 * if this isn't the reservation holder and it's 8294 * not a "all registrants" type or if the type is 8295 * different then we have a conflict 8296 */ 8297 if ((lun->pr_res_idx != residx 8298 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8299 || lun->pr_res_type != type) { 8300 mtx_unlock(&lun->lun_lock); 8301 free(ctsio->kern_data_ptr, M_CTL); 8302 ctl_set_reservation_conflict(ctsio); 8303 ctl_done((union ctl_io *)ctsio); 8304 return (CTL_RETVAL_COMPLETE); 8305 } 8306 mtx_unlock(&lun->lun_lock); 8307 } else /* create a reservation */ { 8308 /* 8309 * If it's not an "all registrants" type record 8310 * reservation holder 8311 */ 8312 if (type != SPR_TYPE_WR_EX_AR 8313 && type != SPR_TYPE_EX_AC_AR) 8314 lun->pr_res_idx = residx; /* Res holder */ 8315 else 8316 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8317 8318 lun->flags |= CTL_LUN_PR_RESERVED; 8319 lun->pr_res_type = type; 8320 8321 mtx_unlock(&lun->lun_lock); 8322 8323 /* send msg to other side */ 8324 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8325 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8326 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8327 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8328 persis_io.pr.pr_info.res_type = type; 8329 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8330 sizeof(persis_io.pr), M_WAITOK); 8331 } 8332 break; 8333 8334 case SPRO_RELEASE: 8335 mtx_lock(&lun->lun_lock); 8336 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8337 /* No reservation exists return good status */ 8338 mtx_unlock(&lun->lun_lock); 8339 goto done; 8340 } 8341 /* 8342 * Is this nexus a reservation holder? 8343 */ 8344 if (lun->pr_res_idx != residx 8345 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8346 /* 8347 * not a res holder return good status but 8348 * do nothing 8349 */ 8350 mtx_unlock(&lun->lun_lock); 8351 goto done; 8352 } 8353 8354 if (lun->pr_res_type != type) { 8355 mtx_unlock(&lun->lun_lock); 8356 free(ctsio->kern_data_ptr, M_CTL); 8357 ctl_set_illegal_pr_release(ctsio); 8358 ctl_done((union ctl_io *)ctsio); 8359 return (CTL_RETVAL_COMPLETE); 8360 } 8361 8362 /* okay to release */ 8363 lun->flags &= ~CTL_LUN_PR_RESERVED; 8364 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8365 lun->pr_res_type = 0; 8366 8367 /* 8368 * if this isn't an exclusive access 8369 * res generate UA for all other 8370 * registrants. 8371 */ 8372 if (type != SPR_TYPE_EX_AC 8373 && type != SPR_TYPE_WR_EX) { 8374 for (i = softc->init_min; i < softc->init_max; i++) { 8375 if (i == residx || ctl_get_prkey(lun, i) == 0) 8376 continue; 8377 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8378 } 8379 } 8380 mtx_unlock(&lun->lun_lock); 8381 8382 /* Send msg to other side */ 8383 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8384 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8385 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8386 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8387 sizeof(persis_io.pr), M_WAITOK); 8388 break; 8389 8390 case SPRO_CLEAR: 8391 /* send msg to other side */ 8392 8393 mtx_lock(&lun->lun_lock); 8394 lun->flags &= ~CTL_LUN_PR_RESERVED; 8395 lun->pr_res_type = 0; 8396 lun->pr_key_count = 0; 8397 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8398 8399 ctl_clr_prkey(lun, residx); 8400 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8401 if (ctl_get_prkey(lun, i) != 0) { 8402 ctl_clr_prkey(lun, i); 8403 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8404 } 8405 lun->pr_generation++; 8406 mtx_unlock(&lun->lun_lock); 8407 8408 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8409 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8410 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8411 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8412 sizeof(persis_io.pr), M_WAITOK); 8413 break; 8414 8415 case SPRO_PREEMPT: 8416 case SPRO_PRE_ABO: { 8417 int nretval; 8418 8419 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8420 residx, ctsio, cdb, param); 8421 if (nretval != 0) 8422 return (CTL_RETVAL_COMPLETE); 8423 break; 8424 } 8425 default: 8426 panic("Invalid PR type %x", cdb->action); 8427 } 8428 8429done: 8430 free(ctsio->kern_data_ptr, M_CTL); 8431 ctl_set_success(ctsio); 8432 ctl_done((union ctl_io *)ctsio); 8433 8434 return (retval); 8435} 8436 8437/* 8438 * This routine is for handling a message from the other SC pertaining to 8439 * persistent reserve out. All the error checking will have been done 8440 * so only perorming the action need be done here to keep the two 8441 * in sync. 8442 */ 8443static void 8444ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8445{ 8446 struct ctl_softc *softc = control_softc; 8447 struct ctl_lun *lun; 8448 int i; 8449 uint32_t residx, targ_lun; 8450 8451 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8452 mtx_lock(&softc->ctl_lock); 8453 if ((targ_lun >= CTL_MAX_LUNS) || 8454 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8455 mtx_unlock(&softc->ctl_lock); 8456 return; 8457 } 8458 mtx_lock(&lun->lun_lock); 8459 mtx_unlock(&softc->ctl_lock); 8460 if (lun->flags & CTL_LUN_DISABLED) { 8461 mtx_unlock(&lun->lun_lock); 8462 return; 8463 } 8464 residx = ctl_get_initindex(&msg->hdr.nexus); 8465 switch(msg->pr.pr_info.action) { 8466 case CTL_PR_REG_KEY: 8467 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8468 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8469 lun->pr_key_count++; 8470 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8471 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8472 lun->pr_generation++; 8473 break; 8474 8475 case CTL_PR_UNREG_KEY: 8476 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8477 lun->pr_key_count--; 8478 8479 /* XXX Need to see if the reservation has been released */ 8480 /* if so do we need to generate UA? */ 8481 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8482 lun->flags &= ~CTL_LUN_PR_RESERVED; 8483 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8484 8485 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8486 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8487 lun->pr_key_count) { 8488 /* 8489 * If the reservation is a registrants 8490 * only type we need to generate a UA 8491 * for other registered inits. The 8492 * sense code should be RESERVATIONS 8493 * RELEASED 8494 */ 8495 8496 for (i = softc->init_min; i < softc->init_max; i++) { 8497 if (ctl_get_prkey(lun, i) == 0) 8498 continue; 8499 8500 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8501 } 8502 } 8503 lun->pr_res_type = 0; 8504 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8505 if (lun->pr_key_count==0) { 8506 lun->flags &= ~CTL_LUN_PR_RESERVED; 8507 lun->pr_res_type = 0; 8508 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8509 } 8510 } 8511 lun->pr_generation++; 8512 break; 8513 8514 case CTL_PR_RESERVE: 8515 lun->flags |= CTL_LUN_PR_RESERVED; 8516 lun->pr_res_type = msg->pr.pr_info.res_type; 8517 lun->pr_res_idx = msg->pr.pr_info.residx; 8518 8519 break; 8520 8521 case CTL_PR_RELEASE: 8522 /* 8523 * if this isn't an exclusive access res generate UA for all 8524 * other registrants. 8525 */ 8526 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8527 lun->pr_res_type != SPR_TYPE_WR_EX) { 8528 for (i = softc->init_min; i < softc->init_max; i++) 8529 if (i == residx || ctl_get_prkey(lun, i) == 0) 8530 continue; 8531 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8532 } 8533 8534 lun->flags &= ~CTL_LUN_PR_RESERVED; 8535 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8536 lun->pr_res_type = 0; 8537 break; 8538 8539 case CTL_PR_PREEMPT: 8540 ctl_pro_preempt_other(lun, msg); 8541 break; 8542 case CTL_PR_CLEAR: 8543 lun->flags &= ~CTL_LUN_PR_RESERVED; 8544 lun->pr_res_type = 0; 8545 lun->pr_key_count = 0; 8546 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8547 8548 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8549 if (ctl_get_prkey(lun, i) == 0) 8550 continue; 8551 ctl_clr_prkey(lun, i); 8552 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8553 } 8554 lun->pr_generation++; 8555 break; 8556 } 8557 8558 mtx_unlock(&lun->lun_lock); 8559} 8560 8561int 8562ctl_read_write(struct ctl_scsiio *ctsio) 8563{ 8564 struct ctl_lun *lun; 8565 struct ctl_lba_len_flags *lbalen; 8566 uint64_t lba; 8567 uint32_t num_blocks; 8568 int flags, retval; 8569 int isread; 8570 8571 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8572 8573 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8574 8575 flags = 0; 8576 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8577 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8578 switch (ctsio->cdb[0]) { 8579 case READ_6: 8580 case WRITE_6: { 8581 struct scsi_rw_6 *cdb; 8582 8583 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8584 8585 lba = scsi_3btoul(cdb->addr); 8586 /* only 5 bits are valid in the most significant address byte */ 8587 lba &= 0x1fffff; 8588 num_blocks = cdb->length; 8589 /* 8590 * This is correct according to SBC-2. 8591 */ 8592 if (num_blocks == 0) 8593 num_blocks = 256; 8594 break; 8595 } 8596 case READ_10: 8597 case WRITE_10: { 8598 struct scsi_rw_10 *cdb; 8599 8600 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8601 if (cdb->byte2 & SRW10_FUA) 8602 flags |= CTL_LLF_FUA; 8603 if (cdb->byte2 & SRW10_DPO) 8604 flags |= CTL_LLF_DPO; 8605 lba = scsi_4btoul(cdb->addr); 8606 num_blocks = scsi_2btoul(cdb->length); 8607 break; 8608 } 8609 case WRITE_VERIFY_10: { 8610 struct scsi_write_verify_10 *cdb; 8611 8612 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8613 flags |= CTL_LLF_FUA; 8614 if (cdb->byte2 & SWV_DPO) 8615 flags |= CTL_LLF_DPO; 8616 lba = scsi_4btoul(cdb->addr); 8617 num_blocks = scsi_2btoul(cdb->length); 8618 break; 8619 } 8620 case READ_12: 8621 case WRITE_12: { 8622 struct scsi_rw_12 *cdb; 8623 8624 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8625 if (cdb->byte2 & SRW12_FUA) 8626 flags |= CTL_LLF_FUA; 8627 if (cdb->byte2 & SRW12_DPO) 8628 flags |= CTL_LLF_DPO; 8629 lba = scsi_4btoul(cdb->addr); 8630 num_blocks = scsi_4btoul(cdb->length); 8631 break; 8632 } 8633 case WRITE_VERIFY_12: { 8634 struct scsi_write_verify_12 *cdb; 8635 8636 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8637 flags |= CTL_LLF_FUA; 8638 if (cdb->byte2 & SWV_DPO) 8639 flags |= CTL_LLF_DPO; 8640 lba = scsi_4btoul(cdb->addr); 8641 num_blocks = scsi_4btoul(cdb->length); 8642 break; 8643 } 8644 case READ_16: 8645 case WRITE_16: { 8646 struct scsi_rw_16 *cdb; 8647 8648 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8649 if (cdb->byte2 & SRW12_FUA) 8650 flags |= CTL_LLF_FUA; 8651 if (cdb->byte2 & SRW12_DPO) 8652 flags |= CTL_LLF_DPO; 8653 lba = scsi_8btou64(cdb->addr); 8654 num_blocks = scsi_4btoul(cdb->length); 8655 break; 8656 } 8657 case WRITE_ATOMIC_16: { 8658 struct scsi_write_atomic_16 *cdb; 8659 8660 if (lun->be_lun->atomicblock == 0) { 8661 ctl_set_invalid_opcode(ctsio); 8662 ctl_done((union ctl_io *)ctsio); 8663 return (CTL_RETVAL_COMPLETE); 8664 } 8665 8666 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8667 if (cdb->byte2 & SRW12_FUA) 8668 flags |= CTL_LLF_FUA; 8669 if (cdb->byte2 & SRW12_DPO) 8670 flags |= CTL_LLF_DPO; 8671 lba = scsi_8btou64(cdb->addr); 8672 num_blocks = scsi_2btoul(cdb->length); 8673 if (num_blocks > lun->be_lun->atomicblock) { 8674 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8675 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8676 /*bit*/ 0); 8677 ctl_done((union ctl_io *)ctsio); 8678 return (CTL_RETVAL_COMPLETE); 8679 } 8680 break; 8681 } 8682 case WRITE_VERIFY_16: { 8683 struct scsi_write_verify_16 *cdb; 8684 8685 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8686 flags |= CTL_LLF_FUA; 8687 if (cdb->byte2 & SWV_DPO) 8688 flags |= CTL_LLF_DPO; 8689 lba = scsi_8btou64(cdb->addr); 8690 num_blocks = scsi_4btoul(cdb->length); 8691 break; 8692 } 8693 default: 8694 /* 8695 * We got a command we don't support. This shouldn't 8696 * happen, commands should be filtered out above us. 8697 */ 8698 ctl_set_invalid_opcode(ctsio); 8699 ctl_done((union ctl_io *)ctsio); 8700 8701 return (CTL_RETVAL_COMPLETE); 8702 break; /* NOTREACHED */ 8703 } 8704 8705 /* 8706 * The first check is to make sure we're in bounds, the second 8707 * check is to catch wrap-around problems. If the lba + num blocks 8708 * is less than the lba, then we've wrapped around and the block 8709 * range is invalid anyway. 8710 */ 8711 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8712 || ((lba + num_blocks) < lba)) { 8713 ctl_set_lba_out_of_range(ctsio); 8714 ctl_done((union ctl_io *)ctsio); 8715 return (CTL_RETVAL_COMPLETE); 8716 } 8717 8718 /* 8719 * According to SBC-3, a transfer length of 0 is not an error. 8720 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8721 * translates to 256 blocks for those commands. 8722 */ 8723 if (num_blocks == 0) { 8724 ctl_set_success(ctsio); 8725 ctl_done((union ctl_io *)ctsio); 8726 return (CTL_RETVAL_COMPLETE); 8727 } 8728 8729 /* Set FUA and/or DPO if caches are disabled. */ 8730 if (isread) { 8731 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8732 SCP_RCD) != 0) 8733 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8734 } else { 8735 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8736 SCP_WCE) == 0) 8737 flags |= CTL_LLF_FUA; 8738 } 8739 8740 lbalen = (struct ctl_lba_len_flags *) 8741 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8742 lbalen->lba = lba; 8743 lbalen->len = num_blocks; 8744 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8745 8746 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8747 ctsio->kern_rel_offset = 0; 8748 8749 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8750 8751 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8752 return (retval); 8753} 8754 8755static int 8756ctl_cnw_cont(union ctl_io *io) 8757{ 8758 struct ctl_scsiio *ctsio; 8759 struct ctl_lun *lun; 8760 struct ctl_lba_len_flags *lbalen; 8761 int retval; 8762 8763 ctsio = &io->scsiio; 8764 ctsio->io_hdr.status = CTL_STATUS_NONE; 8765 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8766 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8767 lbalen = (struct ctl_lba_len_flags *) 8768 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8769 lbalen->flags &= ~CTL_LLF_COMPARE; 8770 lbalen->flags |= CTL_LLF_WRITE; 8771 8772 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8773 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8774 return (retval); 8775} 8776 8777int 8778ctl_cnw(struct ctl_scsiio *ctsio) 8779{ 8780 struct ctl_lun *lun; 8781 struct ctl_lba_len_flags *lbalen; 8782 uint64_t lba; 8783 uint32_t num_blocks; 8784 int flags, retval; 8785 8786 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8787 8788 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8789 8790 flags = 0; 8791 switch (ctsio->cdb[0]) { 8792 case COMPARE_AND_WRITE: { 8793 struct scsi_compare_and_write *cdb; 8794 8795 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8796 if (cdb->byte2 & SRW10_FUA) 8797 flags |= CTL_LLF_FUA; 8798 if (cdb->byte2 & SRW10_DPO) 8799 flags |= CTL_LLF_DPO; 8800 lba = scsi_8btou64(cdb->addr); 8801 num_blocks = cdb->length; 8802 break; 8803 } 8804 default: 8805 /* 8806 * We got a command we don't support. This shouldn't 8807 * happen, commands should be filtered out above us. 8808 */ 8809 ctl_set_invalid_opcode(ctsio); 8810 ctl_done((union ctl_io *)ctsio); 8811 8812 return (CTL_RETVAL_COMPLETE); 8813 break; /* NOTREACHED */ 8814 } 8815 8816 /* 8817 * The first check is to make sure we're in bounds, the second 8818 * check is to catch wrap-around problems. If the lba + num blocks 8819 * is less than the lba, then we've wrapped around and the block 8820 * range is invalid anyway. 8821 */ 8822 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8823 || ((lba + num_blocks) < lba)) { 8824 ctl_set_lba_out_of_range(ctsio); 8825 ctl_done((union ctl_io *)ctsio); 8826 return (CTL_RETVAL_COMPLETE); 8827 } 8828 8829 /* 8830 * According to SBC-3, a transfer length of 0 is not an error. 8831 */ 8832 if (num_blocks == 0) { 8833 ctl_set_success(ctsio); 8834 ctl_done((union ctl_io *)ctsio); 8835 return (CTL_RETVAL_COMPLETE); 8836 } 8837 8838 /* Set FUA if write cache is disabled. */ 8839 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8840 SCP_WCE) == 0) 8841 flags |= CTL_LLF_FUA; 8842 8843 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8844 ctsio->kern_rel_offset = 0; 8845 8846 /* 8847 * Set the IO_CONT flag, so that if this I/O gets passed to 8848 * ctl_data_submit_done(), it'll get passed back to 8849 * ctl_ctl_cnw_cont() for further processing. 8850 */ 8851 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8852 ctsio->io_cont = ctl_cnw_cont; 8853 8854 lbalen = (struct ctl_lba_len_flags *) 8855 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8856 lbalen->lba = lba; 8857 lbalen->len = num_blocks; 8858 lbalen->flags = CTL_LLF_COMPARE | flags; 8859 8860 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8861 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8862 return (retval); 8863} 8864 8865int 8866ctl_verify(struct ctl_scsiio *ctsio) 8867{ 8868 struct ctl_lun *lun; 8869 struct ctl_lba_len_flags *lbalen; 8870 uint64_t lba; 8871 uint32_t num_blocks; 8872 int bytchk, flags; 8873 int retval; 8874 8875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8876 8877 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8878 8879 bytchk = 0; 8880 flags = CTL_LLF_FUA; 8881 switch (ctsio->cdb[0]) { 8882 case VERIFY_10: { 8883 struct scsi_verify_10 *cdb; 8884 8885 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8886 if (cdb->byte2 & SVFY_BYTCHK) 8887 bytchk = 1; 8888 if (cdb->byte2 & SVFY_DPO) 8889 flags |= CTL_LLF_DPO; 8890 lba = scsi_4btoul(cdb->addr); 8891 num_blocks = scsi_2btoul(cdb->length); 8892 break; 8893 } 8894 case VERIFY_12: { 8895 struct scsi_verify_12 *cdb; 8896 8897 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8898 if (cdb->byte2 & SVFY_BYTCHK) 8899 bytchk = 1; 8900 if (cdb->byte2 & SVFY_DPO) 8901 flags |= CTL_LLF_DPO; 8902 lba = scsi_4btoul(cdb->addr); 8903 num_blocks = scsi_4btoul(cdb->length); 8904 break; 8905 } 8906 case VERIFY_16: { 8907 struct scsi_rw_16 *cdb; 8908 8909 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8910 if (cdb->byte2 & SVFY_BYTCHK) 8911 bytchk = 1; 8912 if (cdb->byte2 & SVFY_DPO) 8913 flags |= CTL_LLF_DPO; 8914 lba = scsi_8btou64(cdb->addr); 8915 num_blocks = scsi_4btoul(cdb->length); 8916 break; 8917 } 8918 default: 8919 /* 8920 * We got a command we don't support. This shouldn't 8921 * happen, commands should be filtered out above us. 8922 */ 8923 ctl_set_invalid_opcode(ctsio); 8924 ctl_done((union ctl_io *)ctsio); 8925 return (CTL_RETVAL_COMPLETE); 8926 } 8927 8928 /* 8929 * The first check is to make sure we're in bounds, the second 8930 * check is to catch wrap-around problems. If the lba + num blocks 8931 * is less than the lba, then we've wrapped around and the block 8932 * range is invalid anyway. 8933 */ 8934 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8935 || ((lba + num_blocks) < lba)) { 8936 ctl_set_lba_out_of_range(ctsio); 8937 ctl_done((union ctl_io *)ctsio); 8938 return (CTL_RETVAL_COMPLETE); 8939 } 8940 8941 /* 8942 * According to SBC-3, a transfer length of 0 is not an error. 8943 */ 8944 if (num_blocks == 0) { 8945 ctl_set_success(ctsio); 8946 ctl_done((union ctl_io *)ctsio); 8947 return (CTL_RETVAL_COMPLETE); 8948 } 8949 8950 lbalen = (struct ctl_lba_len_flags *) 8951 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8952 lbalen->lba = lba; 8953 lbalen->len = num_blocks; 8954 if (bytchk) { 8955 lbalen->flags = CTL_LLF_COMPARE | flags; 8956 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8957 } else { 8958 lbalen->flags = CTL_LLF_VERIFY | flags; 8959 ctsio->kern_total_len = 0; 8960 } 8961 ctsio->kern_rel_offset = 0; 8962 8963 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8964 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8965 return (retval); 8966} 8967 8968int 8969ctl_report_luns(struct ctl_scsiio *ctsio) 8970{ 8971 struct ctl_softc *softc; 8972 struct scsi_report_luns *cdb; 8973 struct scsi_report_luns_data *lun_data; 8974 struct ctl_lun *lun, *request_lun; 8975 struct ctl_port *port; 8976 int num_luns, retval; 8977 uint32_t alloc_len, lun_datalen; 8978 int num_filled; 8979 uint32_t initidx, targ_lun_id, lun_id; 8980 8981 retval = CTL_RETVAL_COMPLETE; 8982 cdb = (struct scsi_report_luns *)ctsio->cdb; 8983 port = ctl_io_port(&ctsio->io_hdr); 8984 softc = port->ctl_softc; 8985 8986 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8987 8988 mtx_lock(&softc->ctl_lock); 8989 num_luns = 0; 8990 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 8991 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 8992 num_luns++; 8993 } 8994 mtx_unlock(&softc->ctl_lock); 8995 8996 switch (cdb->select_report) { 8997 case RPL_REPORT_DEFAULT: 8998 case RPL_REPORT_ALL: 8999 case RPL_REPORT_NONSUBSID: 9000 break; 9001 case RPL_REPORT_WELLKNOWN: 9002 case RPL_REPORT_ADMIN: 9003 case RPL_REPORT_CONGLOM: 9004 num_luns = 0; 9005 break; 9006 default: 9007 ctl_set_invalid_field(ctsio, 9008 /*sks_valid*/ 1, 9009 /*command*/ 1, 9010 /*field*/ 2, 9011 /*bit_valid*/ 0, 9012 /*bit*/ 0); 9013 ctl_done((union ctl_io *)ctsio); 9014 return (retval); 9015 break; /* NOTREACHED */ 9016 } 9017 9018 alloc_len = scsi_4btoul(cdb->length); 9019 /* 9020 * The initiator has to allocate at least 16 bytes for this request, 9021 * so he can at least get the header and the first LUN. Otherwise 9022 * we reject the request (per SPC-3 rev 14, section 6.21). 9023 */ 9024 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9025 sizeof(struct scsi_report_luns_lundata))) { 9026 ctl_set_invalid_field(ctsio, 9027 /*sks_valid*/ 1, 9028 /*command*/ 1, 9029 /*field*/ 6, 9030 /*bit_valid*/ 0, 9031 /*bit*/ 0); 9032 ctl_done((union ctl_io *)ctsio); 9033 return (retval); 9034 } 9035 9036 request_lun = (struct ctl_lun *) 9037 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9038 9039 lun_datalen = sizeof(*lun_data) + 9040 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9041 9042 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9043 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9044 ctsio->kern_sg_entries = 0; 9045 9046 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9047 9048 mtx_lock(&softc->ctl_lock); 9049 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9050 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9051 if (lun_id >= CTL_MAX_LUNS) 9052 continue; 9053 lun = softc->ctl_luns[lun_id]; 9054 if (lun == NULL) 9055 continue; 9056 9057 if (targ_lun_id <= 0xff) { 9058 /* 9059 * Peripheral addressing method, bus number 0. 9060 */ 9061 lun_data->luns[num_filled].lundata[0] = 9062 RPL_LUNDATA_ATYP_PERIPH; 9063 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9064 num_filled++; 9065 } else if (targ_lun_id <= 0x3fff) { 9066 /* 9067 * Flat addressing method. 9068 */ 9069 lun_data->luns[num_filled].lundata[0] = 9070 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9071 lun_data->luns[num_filled].lundata[1] = 9072 (targ_lun_id & 0xff); 9073 num_filled++; 9074 } else if (targ_lun_id <= 0xffffff) { 9075 /* 9076 * Extended flat addressing method. 9077 */ 9078 lun_data->luns[num_filled].lundata[0] = 9079 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9080 scsi_ulto3b(targ_lun_id, 9081 &lun_data->luns[num_filled].lundata[1]); 9082 num_filled++; 9083 } else { 9084 printf("ctl_report_luns: bogus LUN number %jd, " 9085 "skipping\n", (intmax_t)targ_lun_id); 9086 } 9087 /* 9088 * According to SPC-3, rev 14 section 6.21: 9089 * 9090 * "The execution of a REPORT LUNS command to any valid and 9091 * installed logical unit shall clear the REPORTED LUNS DATA 9092 * HAS CHANGED unit attention condition for all logical 9093 * units of that target with respect to the requesting 9094 * initiator. A valid and installed logical unit is one 9095 * having a PERIPHERAL QUALIFIER of 000b in the standard 9096 * INQUIRY data (see 6.4.2)." 9097 * 9098 * If request_lun is NULL, the LUN this report luns command 9099 * was issued to is either disabled or doesn't exist. In that 9100 * case, we shouldn't clear any pending lun change unit 9101 * attention. 9102 */ 9103 if (request_lun != NULL) { 9104 mtx_lock(&lun->lun_lock); 9105 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9106 mtx_unlock(&lun->lun_lock); 9107 } 9108 } 9109 mtx_unlock(&softc->ctl_lock); 9110 9111 /* 9112 * It's quite possible that we've returned fewer LUNs than we allocated 9113 * space for. Trim it. 9114 */ 9115 lun_datalen = sizeof(*lun_data) + 9116 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9117 9118 if (lun_datalen < alloc_len) { 9119 ctsio->residual = alloc_len - lun_datalen; 9120 ctsio->kern_data_len = lun_datalen; 9121 ctsio->kern_total_len = lun_datalen; 9122 } else { 9123 ctsio->residual = 0; 9124 ctsio->kern_data_len = alloc_len; 9125 ctsio->kern_total_len = alloc_len; 9126 } 9127 ctsio->kern_data_resid = 0; 9128 ctsio->kern_rel_offset = 0; 9129 ctsio->kern_sg_entries = 0; 9130 9131 /* 9132 * We set this to the actual data length, regardless of how much 9133 * space we actually have to return results. If the user looks at 9134 * this value, he'll know whether or not he allocated enough space 9135 * and reissue the command if necessary. We don't support well 9136 * known logical units, so if the user asks for that, return none. 9137 */ 9138 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9139 9140 /* 9141 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9142 * this request. 9143 */ 9144 ctl_set_success(ctsio); 9145 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9146 ctsio->be_move_done = ctl_config_move_done; 9147 ctl_datamove((union ctl_io *)ctsio); 9148 return (retval); 9149} 9150 9151int 9152ctl_request_sense(struct ctl_scsiio *ctsio) 9153{ 9154 struct scsi_request_sense *cdb; 9155 struct scsi_sense_data *sense_ptr; 9156 struct ctl_softc *ctl_softc; 9157 struct ctl_lun *lun; 9158 uint32_t initidx; 9159 int have_error; 9160 scsi_sense_data_type sense_format; 9161 ctl_ua_type ua_type; 9162 9163 cdb = (struct scsi_request_sense *)ctsio->cdb; 9164 9165 ctl_softc = control_softc; 9166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9167 9168 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9169 9170 /* 9171 * Determine which sense format the user wants. 9172 */ 9173 if (cdb->byte2 & SRS_DESC) 9174 sense_format = SSD_TYPE_DESC; 9175 else 9176 sense_format = SSD_TYPE_FIXED; 9177 9178 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9179 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9180 ctsio->kern_sg_entries = 0; 9181 9182 /* 9183 * struct scsi_sense_data, which is currently set to 256 bytes, is 9184 * larger than the largest allowed value for the length field in the 9185 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9186 */ 9187 ctsio->residual = 0; 9188 ctsio->kern_data_len = cdb->length; 9189 ctsio->kern_total_len = cdb->length; 9190 9191 ctsio->kern_data_resid = 0; 9192 ctsio->kern_rel_offset = 0; 9193 ctsio->kern_sg_entries = 0; 9194 9195 /* 9196 * If we don't have a LUN, we don't have any pending sense. 9197 */ 9198 if (lun == NULL) 9199 goto no_sense; 9200 9201 have_error = 0; 9202 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9203 /* 9204 * Check for pending sense, and then for pending unit attentions. 9205 * Pending sense gets returned first, then pending unit attentions. 9206 */ 9207 mtx_lock(&lun->lun_lock); 9208#ifdef CTL_WITH_CA 9209 if (ctl_is_set(lun->have_ca, initidx)) { 9210 scsi_sense_data_type stored_format; 9211 9212 /* 9213 * Check to see which sense format was used for the stored 9214 * sense data. 9215 */ 9216 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9217 9218 /* 9219 * If the user requested a different sense format than the 9220 * one we stored, then we need to convert it to the other 9221 * format. If we're going from descriptor to fixed format 9222 * sense data, we may lose things in translation, depending 9223 * on what options were used. 9224 * 9225 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9226 * for some reason we'll just copy it out as-is. 9227 */ 9228 if ((stored_format == SSD_TYPE_FIXED) 9229 && (sense_format == SSD_TYPE_DESC)) 9230 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9231 &lun->pending_sense[initidx], 9232 (struct scsi_sense_data_desc *)sense_ptr); 9233 else if ((stored_format == SSD_TYPE_DESC) 9234 && (sense_format == SSD_TYPE_FIXED)) 9235 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9236 &lun->pending_sense[initidx], 9237 (struct scsi_sense_data_fixed *)sense_ptr); 9238 else 9239 memcpy(sense_ptr, &lun->pending_sense[initidx], 9240 MIN(sizeof(*sense_ptr), 9241 sizeof(lun->pending_sense[initidx]))); 9242 9243 ctl_clear_mask(lun->have_ca, initidx); 9244 have_error = 1; 9245 } else 9246#endif 9247 { 9248 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9249 if (ua_type != CTL_UA_NONE) 9250 have_error = 1; 9251 if (ua_type == CTL_UA_LUN_CHANGE) { 9252 mtx_unlock(&lun->lun_lock); 9253 mtx_lock(&ctl_softc->ctl_lock); 9254 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9255 mtx_unlock(&ctl_softc->ctl_lock); 9256 mtx_lock(&lun->lun_lock); 9257 } 9258 9259 } 9260 mtx_unlock(&lun->lun_lock); 9261 9262 /* 9263 * We already have a pending error, return it. 9264 */ 9265 if (have_error != 0) { 9266 /* 9267 * We report the SCSI status as OK, since the status of the 9268 * request sense command itself is OK. 9269 * We report 0 for the sense length, because we aren't doing 9270 * autosense in this case. We're reporting sense as 9271 * parameter data. 9272 */ 9273 ctl_set_success(ctsio); 9274 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9275 ctsio->be_move_done = ctl_config_move_done; 9276 ctl_datamove((union ctl_io *)ctsio); 9277 return (CTL_RETVAL_COMPLETE); 9278 } 9279 9280no_sense: 9281 9282 /* 9283 * No sense information to report, so we report that everything is 9284 * okay. 9285 */ 9286 ctl_set_sense_data(sense_ptr, 9287 lun, 9288 sense_format, 9289 /*current_error*/ 1, 9290 /*sense_key*/ SSD_KEY_NO_SENSE, 9291 /*asc*/ 0x00, 9292 /*ascq*/ 0x00, 9293 SSD_ELEM_NONE); 9294 9295 /* 9296 * We report 0 for the sense length, because we aren't doing 9297 * autosense in this case. We're reporting sense as parameter data. 9298 */ 9299 ctl_set_success(ctsio); 9300 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9301 ctsio->be_move_done = ctl_config_move_done; 9302 ctl_datamove((union ctl_io *)ctsio); 9303 return (CTL_RETVAL_COMPLETE); 9304} 9305 9306int 9307ctl_tur(struct ctl_scsiio *ctsio) 9308{ 9309 9310 CTL_DEBUG_PRINT(("ctl_tur\n")); 9311 9312 ctl_set_success(ctsio); 9313 ctl_done((union ctl_io *)ctsio); 9314 9315 return (CTL_RETVAL_COMPLETE); 9316} 9317 9318/* 9319 * SCSI VPD page 0x00, the Supported VPD Pages page. 9320 */ 9321static int 9322ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9323{ 9324 struct scsi_vpd_supported_pages *pages; 9325 int sup_page_size; 9326 struct ctl_lun *lun; 9327 int p; 9328 9329 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9330 9331 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9332 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9333 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9334 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9335 ctsio->kern_sg_entries = 0; 9336 9337 if (sup_page_size < alloc_len) { 9338 ctsio->residual = alloc_len - sup_page_size; 9339 ctsio->kern_data_len = sup_page_size; 9340 ctsio->kern_total_len = sup_page_size; 9341 } else { 9342 ctsio->residual = 0; 9343 ctsio->kern_data_len = alloc_len; 9344 ctsio->kern_total_len = alloc_len; 9345 } 9346 ctsio->kern_data_resid = 0; 9347 ctsio->kern_rel_offset = 0; 9348 ctsio->kern_sg_entries = 0; 9349 9350 /* 9351 * The control device is always connected. The disk device, on the 9352 * other hand, may not be online all the time. Need to change this 9353 * to figure out whether the disk device is actually online or not. 9354 */ 9355 if (lun != NULL) 9356 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9357 lun->be_lun->lun_type; 9358 else 9359 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9360 9361 p = 0; 9362 /* Supported VPD pages */ 9363 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9364 /* Serial Number */ 9365 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9366 /* Device Identification */ 9367 pages->page_list[p++] = SVPD_DEVICE_ID; 9368 /* Extended INQUIRY Data */ 9369 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9370 /* Mode Page Policy */ 9371 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9372 /* SCSI Ports */ 9373 pages->page_list[p++] = SVPD_SCSI_PORTS; 9374 /* Third-party Copy */ 9375 pages->page_list[p++] = SVPD_SCSI_TPC; 9376 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9377 /* Block limits */ 9378 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9379 /* Block Device Characteristics */ 9380 pages->page_list[p++] = SVPD_BDC; 9381 /* Logical Block Provisioning */ 9382 pages->page_list[p++] = SVPD_LBP; 9383 } 9384 pages->length = p; 9385 9386 ctl_set_success(ctsio); 9387 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9388 ctsio->be_move_done = ctl_config_move_done; 9389 ctl_datamove((union ctl_io *)ctsio); 9390 return (CTL_RETVAL_COMPLETE); 9391} 9392 9393/* 9394 * SCSI VPD page 0x80, the Unit Serial Number page. 9395 */ 9396static int 9397ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9398{ 9399 struct scsi_vpd_unit_serial_number *sn_ptr; 9400 struct ctl_lun *lun; 9401 int data_len; 9402 9403 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9404 9405 data_len = 4 + CTL_SN_LEN; 9406 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9407 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9408 if (data_len < alloc_len) { 9409 ctsio->residual = alloc_len - data_len; 9410 ctsio->kern_data_len = data_len; 9411 ctsio->kern_total_len = data_len; 9412 } else { 9413 ctsio->residual = 0; 9414 ctsio->kern_data_len = alloc_len; 9415 ctsio->kern_total_len = alloc_len; 9416 } 9417 ctsio->kern_data_resid = 0; 9418 ctsio->kern_rel_offset = 0; 9419 ctsio->kern_sg_entries = 0; 9420 9421 /* 9422 * The control device is always connected. The disk device, on the 9423 * other hand, may not be online all the time. Need to change this 9424 * to figure out whether the disk device is actually online or not. 9425 */ 9426 if (lun != NULL) 9427 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9428 lun->be_lun->lun_type; 9429 else 9430 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9431 9432 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9433 sn_ptr->length = CTL_SN_LEN; 9434 /* 9435 * If we don't have a LUN, we just leave the serial number as 9436 * all spaces. 9437 */ 9438 if (lun != NULL) { 9439 strncpy((char *)sn_ptr->serial_num, 9440 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9441 } else 9442 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9443 9444 ctl_set_success(ctsio); 9445 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9446 ctsio->be_move_done = ctl_config_move_done; 9447 ctl_datamove((union ctl_io *)ctsio); 9448 return (CTL_RETVAL_COMPLETE); 9449} 9450 9451 9452/* 9453 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9454 */ 9455static int 9456ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9457{ 9458 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9459 struct ctl_lun *lun; 9460 int data_len; 9461 9462 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9463 9464 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9465 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9466 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9467 ctsio->kern_sg_entries = 0; 9468 9469 if (data_len < alloc_len) { 9470 ctsio->residual = alloc_len - data_len; 9471 ctsio->kern_data_len = data_len; 9472 ctsio->kern_total_len = data_len; 9473 } else { 9474 ctsio->residual = 0; 9475 ctsio->kern_data_len = alloc_len; 9476 ctsio->kern_total_len = alloc_len; 9477 } 9478 ctsio->kern_data_resid = 0; 9479 ctsio->kern_rel_offset = 0; 9480 ctsio->kern_sg_entries = 0; 9481 9482 /* 9483 * The control device is always connected. The disk device, on the 9484 * other hand, may not be online all the time. 9485 */ 9486 if (lun != NULL) 9487 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9488 lun->be_lun->lun_type; 9489 else 9490 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9491 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9492 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9493 /* 9494 * We support head of queue, ordered and simple tags. 9495 */ 9496 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9497 /* 9498 * Volatile cache supported. 9499 */ 9500 eid_ptr->flags3 = SVPD_EID_V_SUP; 9501 9502 /* 9503 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9504 * attention for a particular IT nexus on all LUNs once we report 9505 * it to that nexus once. This bit is required as of SPC-4. 9506 */ 9507 eid_ptr->flags4 = SVPD_EID_LUICLT; 9508 9509 /* 9510 * XXX KDM in order to correctly answer this, we would need 9511 * information from the SIM to determine how much sense data it 9512 * can send. So this would really be a path inquiry field, most 9513 * likely. This can be set to a maximum of 252 according to SPC-4, 9514 * but the hardware may or may not be able to support that much. 9515 * 0 just means that the maximum sense data length is not reported. 9516 */ 9517 eid_ptr->max_sense_length = 0; 9518 9519 ctl_set_success(ctsio); 9520 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9521 ctsio->be_move_done = ctl_config_move_done; 9522 ctl_datamove((union ctl_io *)ctsio); 9523 return (CTL_RETVAL_COMPLETE); 9524} 9525 9526static int 9527ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9528{ 9529 struct scsi_vpd_mode_page_policy *mpp_ptr; 9530 struct ctl_lun *lun; 9531 int data_len; 9532 9533 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9534 9535 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9536 sizeof(struct scsi_vpd_mode_page_policy_descr); 9537 9538 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9539 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9540 ctsio->kern_sg_entries = 0; 9541 9542 if (data_len < alloc_len) { 9543 ctsio->residual = alloc_len - data_len; 9544 ctsio->kern_data_len = data_len; 9545 ctsio->kern_total_len = data_len; 9546 } else { 9547 ctsio->residual = 0; 9548 ctsio->kern_data_len = alloc_len; 9549 ctsio->kern_total_len = alloc_len; 9550 } 9551 ctsio->kern_data_resid = 0; 9552 ctsio->kern_rel_offset = 0; 9553 ctsio->kern_sg_entries = 0; 9554 9555 /* 9556 * The control device is always connected. The disk device, on the 9557 * other hand, may not be online all the time. 9558 */ 9559 if (lun != NULL) 9560 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9561 lun->be_lun->lun_type; 9562 else 9563 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9564 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9565 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9566 mpp_ptr->descr[0].page_code = 0x3f; 9567 mpp_ptr->descr[0].subpage_code = 0xff; 9568 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9569 9570 ctl_set_success(ctsio); 9571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9572 ctsio->be_move_done = ctl_config_move_done; 9573 ctl_datamove((union ctl_io *)ctsio); 9574 return (CTL_RETVAL_COMPLETE); 9575} 9576 9577/* 9578 * SCSI VPD page 0x83, the Device Identification page. 9579 */ 9580static int 9581ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9582{ 9583 struct scsi_vpd_device_id *devid_ptr; 9584 struct scsi_vpd_id_descriptor *desc; 9585 struct ctl_softc *softc; 9586 struct ctl_lun *lun; 9587 struct ctl_port *port; 9588 int data_len; 9589 uint8_t proto; 9590 9591 softc = control_softc; 9592 9593 port = ctl_io_port(&ctsio->io_hdr); 9594 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9595 9596 data_len = sizeof(struct scsi_vpd_device_id) + 9597 sizeof(struct scsi_vpd_id_descriptor) + 9598 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9599 sizeof(struct scsi_vpd_id_descriptor) + 9600 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9601 if (lun && lun->lun_devid) 9602 data_len += lun->lun_devid->len; 9603 if (port && port->port_devid) 9604 data_len += port->port_devid->len; 9605 if (port && port->target_devid) 9606 data_len += port->target_devid->len; 9607 9608 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9609 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9610 ctsio->kern_sg_entries = 0; 9611 9612 if (data_len < alloc_len) { 9613 ctsio->residual = alloc_len - data_len; 9614 ctsio->kern_data_len = data_len; 9615 ctsio->kern_total_len = data_len; 9616 } else { 9617 ctsio->residual = 0; 9618 ctsio->kern_data_len = alloc_len; 9619 ctsio->kern_total_len = alloc_len; 9620 } 9621 ctsio->kern_data_resid = 0; 9622 ctsio->kern_rel_offset = 0; 9623 ctsio->kern_sg_entries = 0; 9624 9625 /* 9626 * The control device is always connected. The disk device, on the 9627 * other hand, may not be online all the time. 9628 */ 9629 if (lun != NULL) 9630 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9631 lun->be_lun->lun_type; 9632 else 9633 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9634 devid_ptr->page_code = SVPD_DEVICE_ID; 9635 scsi_ulto2b(data_len - 4, devid_ptr->length); 9636 9637 if (port && port->port_type == CTL_PORT_FC) 9638 proto = SCSI_PROTO_FC << 4; 9639 else if (port && port->port_type == CTL_PORT_ISCSI) 9640 proto = SCSI_PROTO_ISCSI << 4; 9641 else 9642 proto = SCSI_PROTO_SPI << 4; 9643 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9644 9645 /* 9646 * We're using a LUN association here. i.e., this device ID is a 9647 * per-LUN identifier. 9648 */ 9649 if (lun && lun->lun_devid) { 9650 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9651 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9652 lun->lun_devid->len); 9653 } 9654 9655 /* 9656 * This is for the WWPN which is a port association. 9657 */ 9658 if (port && port->port_devid) { 9659 memcpy(desc, port->port_devid->data, port->port_devid->len); 9660 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9661 port->port_devid->len); 9662 } 9663 9664 /* 9665 * This is for the Relative Target Port(type 4h) identifier 9666 */ 9667 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9668 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9669 SVPD_ID_TYPE_RELTARG; 9670 desc->length = 4; 9671 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9672 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9673 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9674 9675 /* 9676 * This is for the Target Port Group(type 5h) identifier 9677 */ 9678 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9679 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9680 SVPD_ID_TYPE_TPORTGRP; 9681 desc->length = 4; 9682 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9683 &desc->identifier[2]); 9684 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9685 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9686 9687 /* 9688 * This is for the Target identifier 9689 */ 9690 if (port && port->target_devid) { 9691 memcpy(desc, port->target_devid->data, port->target_devid->len); 9692 } 9693 9694 ctl_set_success(ctsio); 9695 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9696 ctsio->be_move_done = ctl_config_move_done; 9697 ctl_datamove((union ctl_io *)ctsio); 9698 return (CTL_RETVAL_COMPLETE); 9699} 9700 9701static int 9702ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9703{ 9704 struct ctl_softc *softc = control_softc; 9705 struct scsi_vpd_scsi_ports *sp; 9706 struct scsi_vpd_port_designation *pd; 9707 struct scsi_vpd_port_designation_cont *pdc; 9708 struct ctl_lun *lun; 9709 struct ctl_port *port; 9710 int data_len, num_target_ports, iid_len, id_len; 9711 9712 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9713 9714 num_target_ports = 0; 9715 iid_len = 0; 9716 id_len = 0; 9717 mtx_lock(&softc->ctl_lock); 9718 STAILQ_FOREACH(port, &softc->port_list, links) { 9719 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9720 continue; 9721 if (lun != NULL && 9722 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9723 continue; 9724 num_target_ports++; 9725 if (port->init_devid) 9726 iid_len += port->init_devid->len; 9727 if (port->port_devid) 9728 id_len += port->port_devid->len; 9729 } 9730 mtx_unlock(&softc->ctl_lock); 9731 9732 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9733 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9734 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9735 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9736 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9737 ctsio->kern_sg_entries = 0; 9738 9739 if (data_len < alloc_len) { 9740 ctsio->residual = alloc_len - data_len; 9741 ctsio->kern_data_len = data_len; 9742 ctsio->kern_total_len = data_len; 9743 } else { 9744 ctsio->residual = 0; 9745 ctsio->kern_data_len = alloc_len; 9746 ctsio->kern_total_len = alloc_len; 9747 } 9748 ctsio->kern_data_resid = 0; 9749 ctsio->kern_rel_offset = 0; 9750 ctsio->kern_sg_entries = 0; 9751 9752 /* 9753 * The control device is always connected. The disk device, on the 9754 * other hand, may not be online all the time. Need to change this 9755 * to figure out whether the disk device is actually online or not. 9756 */ 9757 if (lun != NULL) 9758 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9759 lun->be_lun->lun_type; 9760 else 9761 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9762 9763 sp->page_code = SVPD_SCSI_PORTS; 9764 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9765 sp->page_length); 9766 pd = &sp->design[0]; 9767 9768 mtx_lock(&softc->ctl_lock); 9769 STAILQ_FOREACH(port, &softc->port_list, links) { 9770 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9771 continue; 9772 if (lun != NULL && 9773 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9774 continue; 9775 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9776 if (port->init_devid) { 9777 iid_len = port->init_devid->len; 9778 memcpy(pd->initiator_transportid, 9779 port->init_devid->data, port->init_devid->len); 9780 } else 9781 iid_len = 0; 9782 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9783 pdc = (struct scsi_vpd_port_designation_cont *) 9784 (&pd->initiator_transportid[iid_len]); 9785 if (port->port_devid) { 9786 id_len = port->port_devid->len; 9787 memcpy(pdc->target_port_descriptors, 9788 port->port_devid->data, port->port_devid->len); 9789 } else 9790 id_len = 0; 9791 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9792 pd = (struct scsi_vpd_port_designation *) 9793 ((uint8_t *)pdc->target_port_descriptors + id_len); 9794 } 9795 mtx_unlock(&softc->ctl_lock); 9796 9797 ctl_set_success(ctsio); 9798 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9799 ctsio->be_move_done = ctl_config_move_done; 9800 ctl_datamove((union ctl_io *)ctsio); 9801 return (CTL_RETVAL_COMPLETE); 9802} 9803 9804static int 9805ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9806{ 9807 struct scsi_vpd_block_limits *bl_ptr; 9808 struct ctl_lun *lun; 9809 9810 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9811 9812 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9813 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9814 ctsio->kern_sg_entries = 0; 9815 9816 if (sizeof(*bl_ptr) < alloc_len) { 9817 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9818 ctsio->kern_data_len = sizeof(*bl_ptr); 9819 ctsio->kern_total_len = sizeof(*bl_ptr); 9820 } else { 9821 ctsio->residual = 0; 9822 ctsio->kern_data_len = alloc_len; 9823 ctsio->kern_total_len = alloc_len; 9824 } 9825 ctsio->kern_data_resid = 0; 9826 ctsio->kern_rel_offset = 0; 9827 ctsio->kern_sg_entries = 0; 9828 9829 /* 9830 * The control device is always connected. The disk device, on the 9831 * other hand, may not be online all the time. Need to change this 9832 * to figure out whether the disk device is actually online or not. 9833 */ 9834 if (lun != NULL) 9835 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9836 lun->be_lun->lun_type; 9837 else 9838 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9839 9840 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9841 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9842 bl_ptr->max_cmp_write_len = 0xff; 9843 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9844 if (lun != NULL) { 9845 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9846 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9847 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9848 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9849 if (lun->be_lun->ublockexp != 0) { 9850 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9851 bl_ptr->opt_unmap_grain); 9852 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9853 bl_ptr->unmap_grain_align); 9854 } 9855 } 9856 scsi_ulto4b(lun->be_lun->atomicblock, 9857 bl_ptr->max_atomic_transfer_length); 9858 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9859 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9860 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9861 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9862 } 9863 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9864 9865 ctl_set_success(ctsio); 9866 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9867 ctsio->be_move_done = ctl_config_move_done; 9868 ctl_datamove((union ctl_io *)ctsio); 9869 return (CTL_RETVAL_COMPLETE); 9870} 9871 9872static int 9873ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9874{ 9875 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9876 struct ctl_lun *lun; 9877 const char *value; 9878 u_int i; 9879 9880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9881 9882 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9883 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9884 ctsio->kern_sg_entries = 0; 9885 9886 if (sizeof(*bdc_ptr) < alloc_len) { 9887 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9888 ctsio->kern_data_len = sizeof(*bdc_ptr); 9889 ctsio->kern_total_len = sizeof(*bdc_ptr); 9890 } else { 9891 ctsio->residual = 0; 9892 ctsio->kern_data_len = alloc_len; 9893 ctsio->kern_total_len = alloc_len; 9894 } 9895 ctsio->kern_data_resid = 0; 9896 ctsio->kern_rel_offset = 0; 9897 ctsio->kern_sg_entries = 0; 9898 9899 /* 9900 * The control device is always connected. The disk device, on the 9901 * other hand, may not be online all the time. Need to change this 9902 * to figure out whether the disk device is actually online or not. 9903 */ 9904 if (lun != NULL) 9905 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9906 lun->be_lun->lun_type; 9907 else 9908 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9909 bdc_ptr->page_code = SVPD_BDC; 9910 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9911 if (lun != NULL && 9912 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9913 i = strtol(value, NULL, 0); 9914 else 9915 i = CTL_DEFAULT_ROTATION_RATE; 9916 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9917 if (lun != NULL && 9918 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9919 i = strtol(value, NULL, 0); 9920 else 9921 i = 0; 9922 bdc_ptr->wab_wac_ff = (i & 0x0f); 9923 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9924 9925 ctl_set_success(ctsio); 9926 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9927 ctsio->be_move_done = ctl_config_move_done; 9928 ctl_datamove((union ctl_io *)ctsio); 9929 return (CTL_RETVAL_COMPLETE); 9930} 9931 9932static int 9933ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9934{ 9935 struct scsi_vpd_logical_block_prov *lbp_ptr; 9936 struct ctl_lun *lun; 9937 9938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9939 9940 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9941 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9942 ctsio->kern_sg_entries = 0; 9943 9944 if (sizeof(*lbp_ptr) < alloc_len) { 9945 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9946 ctsio->kern_data_len = sizeof(*lbp_ptr); 9947 ctsio->kern_total_len = sizeof(*lbp_ptr); 9948 } else { 9949 ctsio->residual = 0; 9950 ctsio->kern_data_len = alloc_len; 9951 ctsio->kern_total_len = alloc_len; 9952 } 9953 ctsio->kern_data_resid = 0; 9954 ctsio->kern_rel_offset = 0; 9955 ctsio->kern_sg_entries = 0; 9956 9957 /* 9958 * The control device is always connected. The disk device, on the 9959 * other hand, may not be online all the time. Need to change this 9960 * to figure out whether the disk device is actually online or not. 9961 */ 9962 if (lun != NULL) 9963 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9964 lun->be_lun->lun_type; 9965 else 9966 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9967 9968 lbp_ptr->page_code = SVPD_LBP; 9969 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9970 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9971 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9972 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9973 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9974 lbp_ptr->prov_type = SVPD_LBP_THIN; 9975 } 9976 9977 ctl_set_success(ctsio); 9978 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9979 ctsio->be_move_done = ctl_config_move_done; 9980 ctl_datamove((union ctl_io *)ctsio); 9981 return (CTL_RETVAL_COMPLETE); 9982} 9983 9984/* 9985 * INQUIRY with the EVPD bit set. 9986 */ 9987static int 9988ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9989{ 9990 struct ctl_lun *lun; 9991 struct scsi_inquiry *cdb; 9992 int alloc_len, retval; 9993 9994 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9995 cdb = (struct scsi_inquiry *)ctsio->cdb; 9996 alloc_len = scsi_2btoul(cdb->length); 9997 9998 switch (cdb->page_code) { 9999 case SVPD_SUPPORTED_PAGES: 10000 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10001 break; 10002 case SVPD_UNIT_SERIAL_NUMBER: 10003 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10004 break; 10005 case SVPD_DEVICE_ID: 10006 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10007 break; 10008 case SVPD_EXTENDED_INQUIRY_DATA: 10009 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10010 break; 10011 case SVPD_MODE_PAGE_POLICY: 10012 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10013 break; 10014 case SVPD_SCSI_PORTS: 10015 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10016 break; 10017 case SVPD_SCSI_TPC: 10018 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10019 break; 10020 case SVPD_BLOCK_LIMITS: 10021 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10022 goto err; 10023 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10024 break; 10025 case SVPD_BDC: 10026 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10027 goto err; 10028 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10029 break; 10030 case SVPD_LBP: 10031 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10032 goto err; 10033 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10034 break; 10035 default: 10036err: 10037 ctl_set_invalid_field(ctsio, 10038 /*sks_valid*/ 1, 10039 /*command*/ 1, 10040 /*field*/ 2, 10041 /*bit_valid*/ 0, 10042 /*bit*/ 0); 10043 ctl_done((union ctl_io *)ctsio); 10044 retval = CTL_RETVAL_COMPLETE; 10045 break; 10046 } 10047 10048 return (retval); 10049} 10050 10051/* 10052 * Standard INQUIRY data. 10053 */ 10054static int 10055ctl_inquiry_std(struct ctl_scsiio *ctsio) 10056{ 10057 struct scsi_inquiry_data *inq_ptr; 10058 struct scsi_inquiry *cdb; 10059 struct ctl_softc *softc = control_softc; 10060 struct ctl_port *port; 10061 struct ctl_lun *lun; 10062 char *val; 10063 uint32_t alloc_len, data_len; 10064 ctl_port_type port_type; 10065 10066 port = ctl_io_port(&ctsio->io_hdr); 10067 port_type = port->port_type; 10068 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10069 port_type = CTL_PORT_SCSI; 10070 10071 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10072 cdb = (struct scsi_inquiry *)ctsio->cdb; 10073 alloc_len = scsi_2btoul(cdb->length); 10074 10075 /* 10076 * We malloc the full inquiry data size here and fill it 10077 * in. If the user only asks for less, we'll give him 10078 * that much. 10079 */ 10080 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10081 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10082 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10083 ctsio->kern_sg_entries = 0; 10084 ctsio->kern_data_resid = 0; 10085 ctsio->kern_rel_offset = 0; 10086 10087 if (data_len < alloc_len) { 10088 ctsio->residual = alloc_len - data_len; 10089 ctsio->kern_data_len = data_len; 10090 ctsio->kern_total_len = data_len; 10091 } else { 10092 ctsio->residual = 0; 10093 ctsio->kern_data_len = alloc_len; 10094 ctsio->kern_total_len = alloc_len; 10095 } 10096 10097 if (lun != NULL) { 10098 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10099 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10100 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10101 lun->be_lun->lun_type; 10102 } else { 10103 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10104 lun->be_lun->lun_type; 10105 } 10106 if (lun->flags & CTL_LUN_REMOVABLE) 10107 inq_ptr->dev_qual2 |= SID_RMB; 10108 } else 10109 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10110 10111 /* RMB in byte 2 is 0 */ 10112 inq_ptr->version = SCSI_REV_SPC4; 10113 10114 /* 10115 * According to SAM-3, even if a device only supports a single 10116 * level of LUN addressing, it should still set the HISUP bit: 10117 * 10118 * 4.9.1 Logical unit numbers overview 10119 * 10120 * All logical unit number formats described in this standard are 10121 * hierarchical in structure even when only a single level in that 10122 * hierarchy is used. The HISUP bit shall be set to one in the 10123 * standard INQUIRY data (see SPC-2) when any logical unit number 10124 * format described in this standard is used. Non-hierarchical 10125 * formats are outside the scope of this standard. 10126 * 10127 * Therefore we set the HiSup bit here. 10128 * 10129 * The reponse format is 2, per SPC-3. 10130 */ 10131 inq_ptr->response_format = SID_HiSup | 2; 10132 10133 inq_ptr->additional_length = data_len - 10134 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10135 CTL_DEBUG_PRINT(("additional_length = %d\n", 10136 inq_ptr->additional_length)); 10137 10138 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10139 if (port_type == CTL_PORT_SCSI) 10140 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10141 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10142 inq_ptr->flags = SID_CmdQue; 10143 if (port_type == CTL_PORT_SCSI) 10144 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10145 10146 /* 10147 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10148 * We have 8 bytes for the vendor name, and 16 bytes for the device 10149 * name and 4 bytes for the revision. 10150 */ 10151 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10152 "vendor")) == NULL) { 10153 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10154 } else { 10155 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10156 strncpy(inq_ptr->vendor, val, 10157 min(sizeof(inq_ptr->vendor), strlen(val))); 10158 } 10159 if (lun == NULL) { 10160 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10161 sizeof(inq_ptr->product)); 10162 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10163 switch (lun->be_lun->lun_type) { 10164 case T_DIRECT: 10165 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10166 sizeof(inq_ptr->product)); 10167 break; 10168 case T_PROCESSOR: 10169 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10170 sizeof(inq_ptr->product)); 10171 break; 10172 case T_CDROM: 10173 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10174 sizeof(inq_ptr->product)); 10175 break; 10176 default: 10177 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10178 sizeof(inq_ptr->product)); 10179 break; 10180 } 10181 } else { 10182 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10183 strncpy(inq_ptr->product, val, 10184 min(sizeof(inq_ptr->product), strlen(val))); 10185 } 10186 10187 /* 10188 * XXX make this a macro somewhere so it automatically gets 10189 * incremented when we make changes. 10190 */ 10191 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10192 "revision")) == NULL) { 10193 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10194 } else { 10195 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10196 strncpy(inq_ptr->revision, val, 10197 min(sizeof(inq_ptr->revision), strlen(val))); 10198 } 10199 10200 /* 10201 * For parallel SCSI, we support double transition and single 10202 * transition clocking. We also support QAS (Quick Arbitration 10203 * and Selection) and Information Unit transfers on both the 10204 * control and array devices. 10205 */ 10206 if (port_type == CTL_PORT_SCSI) 10207 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10208 SID_SPI_IUS; 10209 10210 /* SAM-5 (no version claimed) */ 10211 scsi_ulto2b(0x00A0, inq_ptr->version1); 10212 /* SPC-4 (no version claimed) */ 10213 scsi_ulto2b(0x0460, inq_ptr->version2); 10214 if (port_type == CTL_PORT_FC) { 10215 /* FCP-2 ANSI INCITS.350:2003 */ 10216 scsi_ulto2b(0x0917, inq_ptr->version3); 10217 } else if (port_type == CTL_PORT_SCSI) { 10218 /* SPI-4 ANSI INCITS.362:200x */ 10219 scsi_ulto2b(0x0B56, inq_ptr->version3); 10220 } else if (port_type == CTL_PORT_ISCSI) { 10221 /* iSCSI (no version claimed) */ 10222 scsi_ulto2b(0x0960, inq_ptr->version3); 10223 } else if (port_type == CTL_PORT_SAS) { 10224 /* SAS (no version claimed) */ 10225 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10226 } 10227 10228 if (lun == NULL) { 10229 /* SBC-4 (no version claimed) */ 10230 scsi_ulto2b(0x0600, inq_ptr->version4); 10231 } else { 10232 switch (lun->be_lun->lun_type) { 10233 case T_DIRECT: 10234 /* SBC-4 (no version claimed) */ 10235 scsi_ulto2b(0x0600, inq_ptr->version4); 10236 break; 10237 case T_PROCESSOR: 10238 break; 10239 case T_CDROM: 10240 /* MMC-6 (no version claimed) */ 10241 scsi_ulto2b(0x04E0, inq_ptr->version4); 10242 break; 10243 default: 10244 break; 10245 } 10246 } 10247 10248 ctl_set_success(ctsio); 10249 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10250 ctsio->be_move_done = ctl_config_move_done; 10251 ctl_datamove((union ctl_io *)ctsio); 10252 return (CTL_RETVAL_COMPLETE); 10253} 10254 10255int 10256ctl_inquiry(struct ctl_scsiio *ctsio) 10257{ 10258 struct scsi_inquiry *cdb; 10259 int retval; 10260 10261 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10262 10263 cdb = (struct scsi_inquiry *)ctsio->cdb; 10264 if (cdb->byte2 & SI_EVPD) 10265 retval = ctl_inquiry_evpd(ctsio); 10266 else if (cdb->page_code == 0) 10267 retval = ctl_inquiry_std(ctsio); 10268 else { 10269 ctl_set_invalid_field(ctsio, 10270 /*sks_valid*/ 1, 10271 /*command*/ 1, 10272 /*field*/ 2, 10273 /*bit_valid*/ 0, 10274 /*bit*/ 0); 10275 ctl_done((union ctl_io *)ctsio); 10276 return (CTL_RETVAL_COMPLETE); 10277 } 10278 10279 return (retval); 10280} 10281 10282int 10283ctl_get_config(struct ctl_scsiio *ctsio) 10284{ 10285 struct scsi_get_config_header *hdr; 10286 struct scsi_get_config_feature *feature; 10287 struct scsi_get_config *cdb; 10288 struct ctl_lun *lun; 10289 uint32_t alloc_len, data_len; 10290 int rt, starting; 10291 10292 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10293 cdb = (struct scsi_get_config *)ctsio->cdb; 10294 rt = (cdb->rt & SGC_RT_MASK); 10295 starting = scsi_2btoul(cdb->starting_feature); 10296 alloc_len = scsi_2btoul(cdb->length); 10297 10298 data_len = sizeof(struct scsi_get_config_header) + 10299 sizeof(struct scsi_get_config_feature) + 8 + 10300 sizeof(struct scsi_get_config_feature) + 8 + 10301 sizeof(struct scsi_get_config_feature) + 4 + 10302 sizeof(struct scsi_get_config_feature) + 4 + 10303 sizeof(struct scsi_get_config_feature) + 8 + 10304 sizeof(struct scsi_get_config_feature) + 10305 sizeof(struct scsi_get_config_feature) + 4 + 10306 sizeof(struct scsi_get_config_feature) + 4 + 10307 sizeof(struct scsi_get_config_feature) + 4 + 10308 sizeof(struct scsi_get_config_feature) + 4 + 10309 sizeof(struct scsi_get_config_feature) + 4 + 10310 sizeof(struct scsi_get_config_feature) + 4; 10311 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10312 ctsio->kern_sg_entries = 0; 10313 ctsio->kern_data_resid = 0; 10314 ctsio->kern_rel_offset = 0; 10315 10316 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10317 if (lun->flags & CTL_LUN_NO_MEDIA) 10318 scsi_ulto2b(0x0000, hdr->current_profile); 10319 else 10320 scsi_ulto2b(0x0010, hdr->current_profile); 10321 feature = (struct scsi_get_config_feature *)(hdr + 1); 10322 10323 if (starting > 0x003b) 10324 goto done; 10325 if (starting > 0x003a) 10326 goto f3b; 10327 if (starting > 0x002b) 10328 goto f3a; 10329 if (starting > 0x002a) 10330 goto f2b; 10331 if (starting > 0x001f) 10332 goto f2a; 10333 if (starting > 0x001e) 10334 goto f1f; 10335 if (starting > 0x001d) 10336 goto f1e; 10337 if (starting > 0x0010) 10338 goto f1d; 10339 if (starting > 0x0003) 10340 goto f10; 10341 if (starting > 0x0002) 10342 goto f3; 10343 if (starting > 0x0001) 10344 goto f2; 10345 if (starting > 0x0000) 10346 goto f1; 10347 10348 /* Profile List */ 10349 scsi_ulto2b(0x0000, feature->feature_code); 10350 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10351 feature->add_length = 8; 10352 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10353 feature->feature_data[2] = 0x00; 10354 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10355 feature->feature_data[6] = 0x01; 10356 feature = (struct scsi_get_config_feature *) 10357 &feature->feature_data[feature->add_length]; 10358 10359f1: /* Core */ 10360 scsi_ulto2b(0x0001, feature->feature_code); 10361 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10362 feature->add_length = 8; 10363 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10364 feature->feature_data[4] = 0x03; 10365 feature = (struct scsi_get_config_feature *) 10366 &feature->feature_data[feature->add_length]; 10367 10368f2: /* Morphing */ 10369 scsi_ulto2b(0x0002, feature->feature_code); 10370 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10371 feature->add_length = 4; 10372 feature->feature_data[0] = 0x02; 10373 feature = (struct scsi_get_config_feature *) 10374 &feature->feature_data[feature->add_length]; 10375 10376f3: /* Removable Medium */ 10377 scsi_ulto2b(0x0003, feature->feature_code); 10378 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10379 feature->add_length = 4; 10380 feature->feature_data[0] = 0x39; 10381 feature = (struct scsi_get_config_feature *) 10382 &feature->feature_data[feature->add_length]; 10383 10384 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10385 goto done; 10386 10387f10: /* Random Read */ 10388 scsi_ulto2b(0x0010, feature->feature_code); 10389 feature->flags = 0x00; 10390 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10391 feature->flags |= SGC_F_CURRENT; 10392 feature->add_length = 8; 10393 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10394 scsi_ulto2b(1, &feature->feature_data[4]); 10395 feature->feature_data[6] = 0x00; 10396 feature = (struct scsi_get_config_feature *) 10397 &feature->feature_data[feature->add_length]; 10398 10399f1d: /* Multi-Read */ 10400 scsi_ulto2b(0x001D, feature->feature_code); 10401 feature->flags = 0x00; 10402 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10403 feature->flags |= SGC_F_CURRENT; 10404 feature->add_length = 0; 10405 feature = (struct scsi_get_config_feature *) 10406 &feature->feature_data[feature->add_length]; 10407 10408f1e: /* CD Read */ 10409 scsi_ulto2b(0x001E, feature->feature_code); 10410 feature->flags = 0x00; 10411 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10412 feature->flags |= SGC_F_CURRENT; 10413 feature->add_length = 4; 10414 feature->feature_data[0] = 0x00; 10415 feature = (struct scsi_get_config_feature *) 10416 &feature->feature_data[feature->add_length]; 10417 10418f1f: /* DVD Read */ 10419 scsi_ulto2b(0x001F, feature->feature_code); 10420 feature->flags = 0x08; 10421 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10422 feature->flags |= SGC_F_CURRENT; 10423 feature->add_length = 4; 10424 feature->feature_data[0] = 0x01; 10425 feature->feature_data[2] = 0x03; 10426 feature = (struct scsi_get_config_feature *) 10427 &feature->feature_data[feature->add_length]; 10428 10429f2a: /* DVD+RW */ 10430 scsi_ulto2b(0x002A, feature->feature_code); 10431 feature->flags = 0x04; 10432 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10433 feature->flags |= SGC_F_CURRENT; 10434 feature->add_length = 4; 10435 feature->feature_data[0] = 0x00; 10436 feature->feature_data[1] = 0x00; 10437 feature = (struct scsi_get_config_feature *) 10438 &feature->feature_data[feature->add_length]; 10439 10440f2b: /* DVD+R */ 10441 scsi_ulto2b(0x002B, feature->feature_code); 10442 feature->flags = 0x00; 10443 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10444 feature->flags |= SGC_F_CURRENT; 10445 feature->add_length = 4; 10446 feature->feature_data[0] = 0x00; 10447 feature = (struct scsi_get_config_feature *) 10448 &feature->feature_data[feature->add_length]; 10449 10450f3a: /* DVD+RW Dual Layer */ 10451 scsi_ulto2b(0x003A, feature->feature_code); 10452 feature->flags = 0x00; 10453 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10454 feature->flags |= SGC_F_CURRENT; 10455 feature->add_length = 4; 10456 feature->feature_data[0] = 0x00; 10457 feature->feature_data[1] = 0x00; 10458 feature = (struct scsi_get_config_feature *) 10459 &feature->feature_data[feature->add_length]; 10460 10461f3b: /* DVD+R Dual Layer */ 10462 scsi_ulto2b(0x003B, feature->feature_code); 10463 feature->flags = 0x00; 10464 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10465 feature->flags |= SGC_F_CURRENT; 10466 feature->add_length = 4; 10467 feature->feature_data[0] = 0x00; 10468 feature = (struct scsi_get_config_feature *) 10469 &feature->feature_data[feature->add_length]; 10470 10471done: 10472 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10473 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10474 feature = (struct scsi_get_config_feature *)(hdr + 1); 10475 if (scsi_2btoul(feature->feature_code) == starting) 10476 feature = (struct scsi_get_config_feature *) 10477 &feature->feature_data[feature->add_length]; 10478 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10479 } 10480 scsi_ulto4b(data_len - 4, hdr->data_length); 10481 if (data_len < alloc_len) { 10482 ctsio->residual = alloc_len - data_len; 10483 ctsio->kern_data_len = data_len; 10484 ctsio->kern_total_len = data_len; 10485 } else { 10486 ctsio->residual = 0; 10487 ctsio->kern_data_len = alloc_len; 10488 ctsio->kern_total_len = alloc_len; 10489 } 10490 10491 ctl_set_success(ctsio); 10492 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10493 ctsio->be_move_done = ctl_config_move_done; 10494 ctl_datamove((union ctl_io *)ctsio); 10495 return (CTL_RETVAL_COMPLETE); 10496} 10497 10498int 10499ctl_get_event_status(struct ctl_scsiio *ctsio) 10500{ 10501 struct scsi_get_event_status_header *hdr; 10502 struct scsi_get_event_status *cdb; 10503 struct ctl_lun *lun; 10504 uint32_t alloc_len, data_len; 10505 int notif_class; 10506 10507 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10508 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10509 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10510 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10511 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10512 ctl_done((union ctl_io *)ctsio); 10513 return (CTL_RETVAL_COMPLETE); 10514 } 10515 notif_class = cdb->notif_class; 10516 alloc_len = scsi_2btoul(cdb->length); 10517 10518 data_len = sizeof(struct scsi_get_event_status_header); 10519 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10520 ctsio->kern_sg_entries = 0; 10521 ctsio->kern_data_resid = 0; 10522 ctsio->kern_rel_offset = 0; 10523 10524 if (data_len < alloc_len) { 10525 ctsio->residual = alloc_len - data_len; 10526 ctsio->kern_data_len = data_len; 10527 ctsio->kern_total_len = data_len; 10528 } else { 10529 ctsio->residual = 0; 10530 ctsio->kern_data_len = alloc_len; 10531 ctsio->kern_total_len = alloc_len; 10532 } 10533 10534 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10535 scsi_ulto2b(0, hdr->descr_length); 10536 hdr->nea_class = SGESN_NEA; 10537 hdr->supported_class = 0; 10538 10539 ctl_set_success(ctsio); 10540 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10541 ctsio->be_move_done = ctl_config_move_done; 10542 ctl_datamove((union ctl_io *)ctsio); 10543 return (CTL_RETVAL_COMPLETE); 10544} 10545 10546int 10547ctl_mechanism_status(struct ctl_scsiio *ctsio) 10548{ 10549 struct scsi_mechanism_status_header *hdr; 10550 struct scsi_mechanism_status *cdb; 10551 struct ctl_lun *lun; 10552 uint32_t alloc_len, data_len; 10553 10554 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10555 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10556 alloc_len = scsi_2btoul(cdb->length); 10557 10558 data_len = sizeof(struct scsi_mechanism_status_header); 10559 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10560 ctsio->kern_sg_entries = 0; 10561 ctsio->kern_data_resid = 0; 10562 ctsio->kern_rel_offset = 0; 10563 10564 if (data_len < alloc_len) { 10565 ctsio->residual = alloc_len - data_len; 10566 ctsio->kern_data_len = data_len; 10567 ctsio->kern_total_len = data_len; 10568 } else { 10569 ctsio->residual = 0; 10570 ctsio->kern_data_len = alloc_len; 10571 ctsio->kern_total_len = alloc_len; 10572 } 10573 10574 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10575 hdr->state1 = 0x00; 10576 hdr->state2 = 0xe0; 10577 scsi_ulto3b(0, hdr->lba); 10578 hdr->slots_num = 0; 10579 scsi_ulto2b(0, hdr->slots_length); 10580 10581 ctl_set_success(ctsio); 10582 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10583 ctsio->be_move_done = ctl_config_move_done; 10584 ctl_datamove((union ctl_io *)ctsio); 10585 return (CTL_RETVAL_COMPLETE); 10586} 10587 10588static void 10589ctl_ultomsf(uint32_t lba, uint8_t *buf) 10590{ 10591 10592 lba += 150; 10593 buf[0] = 0; 10594 buf[1] = bin2bcd((lba / 75) / 60); 10595 buf[2] = bin2bcd((lba / 75) % 60); 10596 buf[3] = bin2bcd(lba % 75); 10597} 10598 10599int 10600ctl_read_toc(struct ctl_scsiio *ctsio) 10601{ 10602 struct scsi_read_toc_hdr *hdr; 10603 struct scsi_read_toc_type01_descr *descr; 10604 struct scsi_read_toc *cdb; 10605 struct ctl_lun *lun; 10606 uint32_t alloc_len, data_len; 10607 int format, msf; 10608 10609 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10610 cdb = (struct scsi_read_toc *)ctsio->cdb; 10611 msf = (cdb->byte2 & CD_MSF) != 0; 10612 format = cdb->format; 10613 alloc_len = scsi_2btoul(cdb->data_len); 10614 10615 data_len = sizeof(struct scsi_read_toc_hdr); 10616 if (format == 0) 10617 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10618 else 10619 data_len += sizeof(struct scsi_read_toc_type01_descr); 10620 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10621 ctsio->kern_sg_entries = 0; 10622 ctsio->kern_data_resid = 0; 10623 ctsio->kern_rel_offset = 0; 10624 10625 if (data_len < alloc_len) { 10626 ctsio->residual = alloc_len - data_len; 10627 ctsio->kern_data_len = data_len; 10628 ctsio->kern_total_len = data_len; 10629 } else { 10630 ctsio->residual = 0; 10631 ctsio->kern_data_len = alloc_len; 10632 ctsio->kern_total_len = alloc_len; 10633 } 10634 10635 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10636 if (format == 0) { 10637 scsi_ulto2b(0x12, hdr->data_length); 10638 hdr->first = 1; 10639 hdr->last = 1; 10640 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10641 descr->addr_ctl = 0x14; 10642 descr->track_number = 1; 10643 if (msf) 10644 ctl_ultomsf(0, descr->track_start); 10645 else 10646 scsi_ulto4b(0, descr->track_start); 10647 descr++; 10648 descr->addr_ctl = 0x14; 10649 descr->track_number = 0xaa; 10650 if (msf) 10651 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10652 else 10653 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10654 } else { 10655 scsi_ulto2b(0x0a, hdr->data_length); 10656 hdr->first = 1; 10657 hdr->last = 1; 10658 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10659 descr->addr_ctl = 0x14; 10660 descr->track_number = 1; 10661 if (msf) 10662 ctl_ultomsf(0, descr->track_start); 10663 else 10664 scsi_ulto4b(0, descr->track_start); 10665 } 10666 10667 ctl_set_success(ctsio); 10668 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10669 ctsio->be_move_done = ctl_config_move_done; 10670 ctl_datamove((union ctl_io *)ctsio); 10671 return (CTL_RETVAL_COMPLETE); 10672} 10673 10674/* 10675 * For known CDB types, parse the LBA and length. 10676 */ 10677static int 10678ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10679{ 10680 if (io->io_hdr.io_type != CTL_IO_SCSI) 10681 return (1); 10682 10683 switch (io->scsiio.cdb[0]) { 10684 case COMPARE_AND_WRITE: { 10685 struct scsi_compare_and_write *cdb; 10686 10687 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10688 10689 *lba = scsi_8btou64(cdb->addr); 10690 *len = cdb->length; 10691 break; 10692 } 10693 case READ_6: 10694 case WRITE_6: { 10695 struct scsi_rw_6 *cdb; 10696 10697 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10698 10699 *lba = scsi_3btoul(cdb->addr); 10700 /* only 5 bits are valid in the most significant address byte */ 10701 *lba &= 0x1fffff; 10702 *len = cdb->length; 10703 break; 10704 } 10705 case READ_10: 10706 case WRITE_10: { 10707 struct scsi_rw_10 *cdb; 10708 10709 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10710 10711 *lba = scsi_4btoul(cdb->addr); 10712 *len = scsi_2btoul(cdb->length); 10713 break; 10714 } 10715 case WRITE_VERIFY_10: { 10716 struct scsi_write_verify_10 *cdb; 10717 10718 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10719 10720 *lba = scsi_4btoul(cdb->addr); 10721 *len = scsi_2btoul(cdb->length); 10722 break; 10723 } 10724 case READ_12: 10725 case WRITE_12: { 10726 struct scsi_rw_12 *cdb; 10727 10728 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10729 10730 *lba = scsi_4btoul(cdb->addr); 10731 *len = scsi_4btoul(cdb->length); 10732 break; 10733 } 10734 case WRITE_VERIFY_12: { 10735 struct scsi_write_verify_12 *cdb; 10736 10737 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10738 10739 *lba = scsi_4btoul(cdb->addr); 10740 *len = scsi_4btoul(cdb->length); 10741 break; 10742 } 10743 case READ_16: 10744 case WRITE_16: { 10745 struct scsi_rw_16 *cdb; 10746 10747 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10748 10749 *lba = scsi_8btou64(cdb->addr); 10750 *len = scsi_4btoul(cdb->length); 10751 break; 10752 } 10753 case WRITE_ATOMIC_16: { 10754 struct scsi_write_atomic_16 *cdb; 10755 10756 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10757 10758 *lba = scsi_8btou64(cdb->addr); 10759 *len = scsi_2btoul(cdb->length); 10760 break; 10761 } 10762 case WRITE_VERIFY_16: { 10763 struct scsi_write_verify_16 *cdb; 10764 10765 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10766 10767 *lba = scsi_8btou64(cdb->addr); 10768 *len = scsi_4btoul(cdb->length); 10769 break; 10770 } 10771 case WRITE_SAME_10: { 10772 struct scsi_write_same_10 *cdb; 10773 10774 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10775 10776 *lba = scsi_4btoul(cdb->addr); 10777 *len = scsi_2btoul(cdb->length); 10778 break; 10779 } 10780 case WRITE_SAME_16: { 10781 struct scsi_write_same_16 *cdb; 10782 10783 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10784 10785 *lba = scsi_8btou64(cdb->addr); 10786 *len = scsi_4btoul(cdb->length); 10787 break; 10788 } 10789 case VERIFY_10: { 10790 struct scsi_verify_10 *cdb; 10791 10792 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10793 10794 *lba = scsi_4btoul(cdb->addr); 10795 *len = scsi_2btoul(cdb->length); 10796 break; 10797 } 10798 case VERIFY_12: { 10799 struct scsi_verify_12 *cdb; 10800 10801 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10802 10803 *lba = scsi_4btoul(cdb->addr); 10804 *len = scsi_4btoul(cdb->length); 10805 break; 10806 } 10807 case VERIFY_16: { 10808 struct scsi_verify_16 *cdb; 10809 10810 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10811 10812 *lba = scsi_8btou64(cdb->addr); 10813 *len = scsi_4btoul(cdb->length); 10814 break; 10815 } 10816 case UNMAP: { 10817 *lba = 0; 10818 *len = UINT64_MAX; 10819 break; 10820 } 10821 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10822 struct scsi_get_lba_status *cdb; 10823 10824 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10825 *lba = scsi_8btou64(cdb->addr); 10826 *len = UINT32_MAX; 10827 break; 10828 } 10829 default: 10830 return (1); 10831 break; /* NOTREACHED */ 10832 } 10833 10834 return (0); 10835} 10836 10837static ctl_action 10838ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10839 bool seq) 10840{ 10841 uint64_t endlba1, endlba2; 10842 10843 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10844 endlba2 = lba2 + len2 - 1; 10845 10846 if ((endlba1 < lba2) || (endlba2 < lba1)) 10847 return (CTL_ACTION_PASS); 10848 else 10849 return (CTL_ACTION_BLOCK); 10850} 10851 10852static int 10853ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10854{ 10855 struct ctl_ptr_len_flags *ptrlen; 10856 struct scsi_unmap_desc *buf, *end, *range; 10857 uint64_t lba; 10858 uint32_t len; 10859 10860 /* If not UNMAP -- go other way. */ 10861 if (io->io_hdr.io_type != CTL_IO_SCSI || 10862 io->scsiio.cdb[0] != UNMAP) 10863 return (CTL_ACTION_ERROR); 10864 10865 /* If UNMAP without data -- block and wait for data. */ 10866 ptrlen = (struct ctl_ptr_len_flags *) 10867 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10868 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10869 ptrlen->ptr == NULL) 10870 return (CTL_ACTION_BLOCK); 10871 10872 /* UNMAP with data -- check for collision. */ 10873 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10874 end = buf + ptrlen->len / sizeof(*buf); 10875 for (range = buf; range < end; range++) { 10876 lba = scsi_8btou64(range->lba); 10877 len = scsi_4btoul(range->length); 10878 if ((lba < lba2 + len2) && (lba + len > lba2)) 10879 return (CTL_ACTION_BLOCK); 10880 } 10881 return (CTL_ACTION_PASS); 10882} 10883 10884static ctl_action 10885ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10886{ 10887 uint64_t lba1, lba2; 10888 uint64_t len1, len2; 10889 int retval; 10890 10891 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10892 return (CTL_ACTION_ERROR); 10893 10894 retval = ctl_extent_check_unmap(io1, lba2, len2); 10895 if (retval != CTL_ACTION_ERROR) 10896 return (retval); 10897 10898 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10899 return (CTL_ACTION_ERROR); 10900 10901 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10902 seq = FALSE; 10903 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10904} 10905 10906static ctl_action 10907ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10908{ 10909 uint64_t lba1, lba2; 10910 uint64_t len1, len2; 10911 10912 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10913 return (CTL_ACTION_PASS); 10914 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10915 return (CTL_ACTION_ERROR); 10916 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10917 return (CTL_ACTION_ERROR); 10918 10919 if (lba1 + len1 == lba2) 10920 return (CTL_ACTION_BLOCK); 10921 return (CTL_ACTION_PASS); 10922} 10923 10924static ctl_action 10925ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10926 union ctl_io *ooa_io) 10927{ 10928 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10929 const ctl_serialize_action *serialize_row; 10930 10931 /* 10932 * The initiator attempted multiple untagged commands at the same 10933 * time. Can't do that. 10934 */ 10935 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10936 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10937 && ((pending_io->io_hdr.nexus.targ_port == 10938 ooa_io->io_hdr.nexus.targ_port) 10939 && (pending_io->io_hdr.nexus.initid == 10940 ooa_io->io_hdr.nexus.initid)) 10941 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10942 CTL_FLAG_STATUS_SENT)) == 0)) 10943 return (CTL_ACTION_OVERLAP); 10944 10945 /* 10946 * The initiator attempted to send multiple tagged commands with 10947 * the same ID. (It's fine if different initiators have the same 10948 * tag ID.) 10949 * 10950 * Even if all of those conditions are true, we don't kill the I/O 10951 * if the command ahead of us has been aborted. We won't end up 10952 * sending it to the FETD, and it's perfectly legal to resend a 10953 * command with the same tag number as long as the previous 10954 * instance of this tag number has been aborted somehow. 10955 */ 10956 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10957 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10958 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10959 && ((pending_io->io_hdr.nexus.targ_port == 10960 ooa_io->io_hdr.nexus.targ_port) 10961 && (pending_io->io_hdr.nexus.initid == 10962 ooa_io->io_hdr.nexus.initid)) 10963 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10964 CTL_FLAG_STATUS_SENT)) == 0)) 10965 return (CTL_ACTION_OVERLAP_TAG); 10966 10967 /* 10968 * If we get a head of queue tag, SAM-3 says that we should 10969 * immediately execute it. 10970 * 10971 * What happens if this command would normally block for some other 10972 * reason? e.g. a request sense with a head of queue tag 10973 * immediately after a write. Normally that would block, but this 10974 * will result in its getting executed immediately... 10975 * 10976 * We currently return "pass" instead of "skip", so we'll end up 10977 * going through the rest of the queue to check for overlapped tags. 10978 * 10979 * XXX KDM check for other types of blockage first?? 10980 */ 10981 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10982 return (CTL_ACTION_PASS); 10983 10984 /* 10985 * Ordered tags have to block until all items ahead of them 10986 * have completed. If we get called with an ordered tag, we always 10987 * block, if something else is ahead of us in the queue. 10988 */ 10989 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10990 return (CTL_ACTION_BLOCK); 10991 10992 /* 10993 * Simple tags get blocked until all head of queue and ordered tags 10994 * ahead of them have completed. I'm lumping untagged commands in 10995 * with simple tags here. XXX KDM is that the right thing to do? 10996 */ 10997 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10998 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10999 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11000 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11001 return (CTL_ACTION_BLOCK); 11002 11003 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11004 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11005 11006 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11007 11008 switch (serialize_row[pending_entry->seridx]) { 11009 case CTL_SER_BLOCK: 11010 return (CTL_ACTION_BLOCK); 11011 case CTL_SER_EXTENT: 11012 return (ctl_extent_check(ooa_io, pending_io, 11013 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11014 case CTL_SER_EXTENTOPT: 11015 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11016 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11017 return (ctl_extent_check(ooa_io, pending_io, 11018 (lun->be_lun && 11019 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11020 return (CTL_ACTION_PASS); 11021 case CTL_SER_EXTENTSEQ: 11022 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11023 return (ctl_extent_check_seq(ooa_io, pending_io)); 11024 return (CTL_ACTION_PASS); 11025 case CTL_SER_PASS: 11026 return (CTL_ACTION_PASS); 11027 case CTL_SER_BLOCKOPT: 11028 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11029 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11030 return (CTL_ACTION_BLOCK); 11031 return (CTL_ACTION_PASS); 11032 case CTL_SER_SKIP: 11033 return (CTL_ACTION_SKIP); 11034 default: 11035 panic("invalid serialization value %d", 11036 serialize_row[pending_entry->seridx]); 11037 } 11038 11039 return (CTL_ACTION_ERROR); 11040} 11041 11042/* 11043 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11044 * Assumptions: 11045 * - pending_io is generally either incoming, or on the blocked queue 11046 * - starting I/O is the I/O we want to start the check with. 11047 */ 11048static ctl_action 11049ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11050 union ctl_io *starting_io) 11051{ 11052 union ctl_io *ooa_io; 11053 ctl_action action; 11054 11055 mtx_assert(&lun->lun_lock, MA_OWNED); 11056 11057 /* 11058 * Run back along the OOA queue, starting with the current 11059 * blocked I/O and going through every I/O before it on the 11060 * queue. If starting_io is NULL, we'll just end up returning 11061 * CTL_ACTION_PASS. 11062 */ 11063 for (ooa_io = starting_io; ooa_io != NULL; 11064 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11065 ooa_links)){ 11066 11067 /* 11068 * This routine just checks to see whether 11069 * cur_blocked is blocked by ooa_io, which is ahead 11070 * of it in the queue. It doesn't queue/dequeue 11071 * cur_blocked. 11072 */ 11073 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11074 switch (action) { 11075 case CTL_ACTION_BLOCK: 11076 case CTL_ACTION_OVERLAP: 11077 case CTL_ACTION_OVERLAP_TAG: 11078 case CTL_ACTION_SKIP: 11079 case CTL_ACTION_ERROR: 11080 return (action); 11081 break; /* NOTREACHED */ 11082 case CTL_ACTION_PASS: 11083 break; 11084 default: 11085 panic("invalid action %d", action); 11086 break; /* NOTREACHED */ 11087 } 11088 } 11089 11090 return (CTL_ACTION_PASS); 11091} 11092 11093/* 11094 * Assumptions: 11095 * - An I/O has just completed, and has been removed from the per-LUN OOA 11096 * queue, so some items on the blocked queue may now be unblocked. 11097 */ 11098static int 11099ctl_check_blocked(struct ctl_lun *lun) 11100{ 11101 struct ctl_softc *softc = lun->ctl_softc; 11102 union ctl_io *cur_blocked, *next_blocked; 11103 11104 mtx_assert(&lun->lun_lock, MA_OWNED); 11105 11106 /* 11107 * Run forward from the head of the blocked queue, checking each 11108 * entry against the I/Os prior to it on the OOA queue to see if 11109 * there is still any blockage. 11110 * 11111 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11112 * with our removing a variable on it while it is traversing the 11113 * list. 11114 */ 11115 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11116 cur_blocked != NULL; cur_blocked = next_blocked) { 11117 union ctl_io *prev_ooa; 11118 ctl_action action; 11119 11120 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11121 blocked_links); 11122 11123 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11124 ctl_ooaq, ooa_links); 11125 11126 /* 11127 * If cur_blocked happens to be the first item in the OOA 11128 * queue now, prev_ooa will be NULL, and the action 11129 * returned will just be CTL_ACTION_PASS. 11130 */ 11131 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11132 11133 switch (action) { 11134 case CTL_ACTION_BLOCK: 11135 /* Nothing to do here, still blocked */ 11136 break; 11137 case CTL_ACTION_OVERLAP: 11138 case CTL_ACTION_OVERLAP_TAG: 11139 /* 11140 * This shouldn't happen! In theory we've already 11141 * checked this command for overlap... 11142 */ 11143 break; 11144 case CTL_ACTION_PASS: 11145 case CTL_ACTION_SKIP: { 11146 const struct ctl_cmd_entry *entry; 11147 11148 /* 11149 * The skip case shouldn't happen, this transaction 11150 * should have never made it onto the blocked queue. 11151 */ 11152 /* 11153 * This I/O is no longer blocked, we can remove it 11154 * from the blocked queue. Since this is a TAILQ 11155 * (doubly linked list), we can do O(1) removals 11156 * from any place on the list. 11157 */ 11158 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11159 blocked_links); 11160 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11161 11162 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11163 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11164 /* 11165 * Need to send IO back to original side to 11166 * run 11167 */ 11168 union ctl_ha_msg msg_info; 11169 11170 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11171 msg_info.hdr.original_sc = 11172 cur_blocked->io_hdr.original_sc; 11173 msg_info.hdr.serializing_sc = cur_blocked; 11174 msg_info.hdr.msg_type = CTL_MSG_R2R; 11175 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11176 sizeof(msg_info.hdr), M_NOWAIT); 11177 break; 11178 } 11179 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11180 11181 /* 11182 * Check this I/O for LUN state changes that may 11183 * have happened while this command was blocked. 11184 * The LUN state may have been changed by a command 11185 * ahead of us in the queue, so we need to re-check 11186 * for any states that can be caused by SCSI 11187 * commands. 11188 */ 11189 if (ctl_scsiio_lun_check(lun, entry, 11190 &cur_blocked->scsiio) == 0) { 11191 cur_blocked->io_hdr.flags |= 11192 CTL_FLAG_IS_WAS_ON_RTR; 11193 ctl_enqueue_rtr(cur_blocked); 11194 } else 11195 ctl_done(cur_blocked); 11196 break; 11197 } 11198 default: 11199 /* 11200 * This probably shouldn't happen -- we shouldn't 11201 * get CTL_ACTION_ERROR, or anything else. 11202 */ 11203 break; 11204 } 11205 } 11206 11207 return (CTL_RETVAL_COMPLETE); 11208} 11209 11210/* 11211 * This routine (with one exception) checks LUN flags that can be set by 11212 * commands ahead of us in the OOA queue. These flags have to be checked 11213 * when a command initially comes in, and when we pull a command off the 11214 * blocked queue and are preparing to execute it. The reason we have to 11215 * check these flags for commands on the blocked queue is that the LUN 11216 * state may have been changed by a command ahead of us while we're on the 11217 * blocked queue. 11218 * 11219 * Ordering is somewhat important with these checks, so please pay 11220 * careful attention to the placement of any new checks. 11221 */ 11222static int 11223ctl_scsiio_lun_check(struct ctl_lun *lun, 11224 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11225{ 11226 struct ctl_softc *softc = lun->ctl_softc; 11227 int retval; 11228 uint32_t residx; 11229 11230 retval = 0; 11231 11232 mtx_assert(&lun->lun_lock, MA_OWNED); 11233 11234 /* 11235 * If this shelf is a secondary shelf controller, we may have to 11236 * reject some commands disallowed by HA mode and link state. 11237 */ 11238 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11239 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11240 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11241 ctl_set_lun_unavail(ctsio); 11242 retval = 1; 11243 goto bailout; 11244 } 11245 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11246 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11247 ctl_set_lun_transit(ctsio); 11248 retval = 1; 11249 goto bailout; 11250 } 11251 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11252 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11253 ctl_set_lun_standby(ctsio); 11254 retval = 1; 11255 goto bailout; 11256 } 11257 11258 /* The rest of checks are only done on executing side */ 11259 if (softc->ha_mode == CTL_HA_MODE_XFER) 11260 goto bailout; 11261 } 11262 11263 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11264 if (lun->be_lun && 11265 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11266 ctl_set_hw_write_protected(ctsio); 11267 retval = 1; 11268 goto bailout; 11269 } 11270 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11271 .eca_and_aen & SCP_SWP) != 0) { 11272 ctl_set_sense(ctsio, /*current_error*/ 1, 11273 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11274 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11275 retval = 1; 11276 goto bailout; 11277 } 11278 } 11279 11280 /* 11281 * Check for a reservation conflict. If this command isn't allowed 11282 * even on reserved LUNs, and if this initiator isn't the one who 11283 * reserved us, reject the command with a reservation conflict. 11284 */ 11285 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11286 if ((lun->flags & CTL_LUN_RESERVED) 11287 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11288 if (lun->res_idx != residx) { 11289 ctl_set_reservation_conflict(ctsio); 11290 retval = 1; 11291 goto bailout; 11292 } 11293 } 11294 11295 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11296 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11297 /* No reservation or command is allowed. */; 11298 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11299 (lun->pr_res_type == SPR_TYPE_WR_EX || 11300 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11301 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11302 /* The command is allowed for Write Exclusive resv. */; 11303 } else { 11304 /* 11305 * if we aren't registered or it's a res holder type 11306 * reservation and this isn't the res holder then set a 11307 * conflict. 11308 */ 11309 if (ctl_get_prkey(lun, residx) == 0 || 11310 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11311 ctl_set_reservation_conflict(ctsio); 11312 retval = 1; 11313 goto bailout; 11314 } 11315 } 11316 11317 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11318 if (lun->flags & CTL_LUN_EJECTED) 11319 ctl_set_lun_ejected(ctsio); 11320 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11321 if (lun->flags & CTL_LUN_REMOVABLE) 11322 ctl_set_lun_no_media(ctsio); 11323 else 11324 ctl_set_lun_int_reqd(ctsio); 11325 } else if (lun->flags & CTL_LUN_STOPPED) 11326 ctl_set_lun_stopped(ctsio); 11327 else 11328 goto bailout; 11329 retval = 1; 11330 goto bailout; 11331 } 11332 11333bailout: 11334 return (retval); 11335} 11336 11337static void 11338ctl_failover_io(union ctl_io *io, int have_lock) 11339{ 11340 ctl_set_busy(&io->scsiio); 11341 ctl_done(io); 11342} 11343 11344static void 11345ctl_failover_lun(union ctl_io *rio) 11346{ 11347 struct ctl_softc *softc = control_softc; 11348 struct ctl_lun *lun; 11349 struct ctl_io_hdr *io, *next_io; 11350 uint32_t targ_lun; 11351 11352 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11353 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11354 11355 /* Find and lock the LUN. */ 11356 mtx_lock(&softc->ctl_lock); 11357 if ((targ_lun < CTL_MAX_LUNS) && 11358 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11359 mtx_lock(&lun->lun_lock); 11360 mtx_unlock(&softc->ctl_lock); 11361 if (lun->flags & CTL_LUN_DISABLED) { 11362 mtx_unlock(&lun->lun_lock); 11363 return; 11364 } 11365 } else { 11366 mtx_unlock(&softc->ctl_lock); 11367 return; 11368 } 11369 11370 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11371 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11372 /* We are master */ 11373 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11374 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11375 io->flags |= CTL_FLAG_ABORT; 11376 io->flags |= CTL_FLAG_FAILOVER; 11377 } else { /* This can be only due to DATAMOVE */ 11378 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11379 io->flags &= ~CTL_FLAG_DMA_INPROG; 11380 io->flags |= CTL_FLAG_IO_ACTIVE; 11381 io->port_status = 31340; 11382 ctl_enqueue_isc((union ctl_io *)io); 11383 } 11384 } 11385 /* We are slave */ 11386 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11387 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11388 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11389 io->flags |= CTL_FLAG_FAILOVER; 11390 } else { 11391 ctl_set_busy(&((union ctl_io *)io)-> 11392 scsiio); 11393 ctl_done((union ctl_io *)io); 11394 } 11395 } 11396 } 11397 } else { /* SERIALIZE modes */ 11398 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11399 next_io) { 11400 /* We are master */ 11401 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11402 TAILQ_REMOVE(&lun->blocked_queue, io, 11403 blocked_links); 11404 io->flags &= ~CTL_FLAG_BLOCKED; 11405 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11406 ctl_free_io((union ctl_io *)io); 11407 } 11408 } 11409 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11410 /* We are master */ 11411 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11412 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11413 ctl_free_io((union ctl_io *)io); 11414 } 11415 /* We are slave */ 11416 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11417 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11418 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11419 ctl_set_busy(&((union ctl_io *)io)-> 11420 scsiio); 11421 ctl_done((union ctl_io *)io); 11422 } 11423 } 11424 } 11425 ctl_check_blocked(lun); 11426 } 11427 mtx_unlock(&lun->lun_lock); 11428} 11429 11430static int 11431ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11432{ 11433 struct ctl_lun *lun; 11434 const struct ctl_cmd_entry *entry; 11435 uint32_t initidx, targ_lun; 11436 int retval; 11437 11438 retval = 0; 11439 11440 lun = NULL; 11441 11442 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11443 if ((targ_lun < CTL_MAX_LUNS) 11444 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11445 /* 11446 * If the LUN is invalid, pretend that it doesn't exist. 11447 * It will go away as soon as all pending I/O has been 11448 * completed. 11449 */ 11450 mtx_lock(&lun->lun_lock); 11451 if (lun->flags & CTL_LUN_DISABLED) { 11452 mtx_unlock(&lun->lun_lock); 11453 lun = NULL; 11454 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11455 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11456 } else { 11457 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11458 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11459 lun->be_lun; 11460 11461 /* 11462 * Every I/O goes into the OOA queue for a 11463 * particular LUN, and stays there until completion. 11464 */ 11465#ifdef CTL_TIME_IO 11466 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11467 lun->idle_time += getsbinuptime() - 11468 lun->last_busy; 11469 } 11470#endif 11471 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11472 ooa_links); 11473 } 11474 } else { 11475 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11476 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11477 } 11478 11479 /* Get command entry and return error if it is unsuppotyed. */ 11480 entry = ctl_validate_command(ctsio); 11481 if (entry == NULL) { 11482 if (lun) 11483 mtx_unlock(&lun->lun_lock); 11484 return (retval); 11485 } 11486 11487 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11488 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11489 11490 /* 11491 * Check to see whether we can send this command to LUNs that don't 11492 * exist. This should pretty much only be the case for inquiry 11493 * and request sense. Further checks, below, really require having 11494 * a LUN, so we can't really check the command anymore. Just put 11495 * it on the rtr queue. 11496 */ 11497 if (lun == NULL) { 11498 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11499 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11500 ctl_enqueue_rtr((union ctl_io *)ctsio); 11501 return (retval); 11502 } 11503 11504 ctl_set_unsupported_lun(ctsio); 11505 ctl_done((union ctl_io *)ctsio); 11506 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11507 return (retval); 11508 } else { 11509 /* 11510 * Make sure we support this particular command on this LUN. 11511 * e.g., we don't support writes to the control LUN. 11512 */ 11513 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11514 mtx_unlock(&lun->lun_lock); 11515 ctl_set_invalid_opcode(ctsio); 11516 ctl_done((union ctl_io *)ctsio); 11517 return (retval); 11518 } 11519 } 11520 11521 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11522 11523#ifdef CTL_WITH_CA 11524 /* 11525 * If we've got a request sense, it'll clear the contingent 11526 * allegiance condition. Otherwise, if we have a CA condition for 11527 * this initiator, clear it, because it sent down a command other 11528 * than request sense. 11529 */ 11530 if ((ctsio->cdb[0] != REQUEST_SENSE) 11531 && (ctl_is_set(lun->have_ca, initidx))) 11532 ctl_clear_mask(lun->have_ca, initidx); 11533#endif 11534 11535 /* 11536 * If the command has this flag set, it handles its own unit 11537 * attention reporting, we shouldn't do anything. Otherwise we 11538 * check for any pending unit attentions, and send them back to the 11539 * initiator. We only do this when a command initially comes in, 11540 * not when we pull it off the blocked queue. 11541 * 11542 * According to SAM-3, section 5.3.2, the order that things get 11543 * presented back to the host is basically unit attentions caused 11544 * by some sort of reset event, busy status, reservation conflicts 11545 * or task set full, and finally any other status. 11546 * 11547 * One issue here is that some of the unit attentions we report 11548 * don't fall into the "reset" category (e.g. "reported luns data 11549 * has changed"). So reporting it here, before the reservation 11550 * check, may be technically wrong. I guess the only thing to do 11551 * would be to check for and report the reset events here, and then 11552 * check for the other unit attention types after we check for a 11553 * reservation conflict. 11554 * 11555 * XXX KDM need to fix this 11556 */ 11557 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11558 ctl_ua_type ua_type; 11559 11560 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11561 SSD_TYPE_NONE); 11562 if (ua_type != CTL_UA_NONE) { 11563 mtx_unlock(&lun->lun_lock); 11564 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11565 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11566 ctsio->sense_len = SSD_FULL_SIZE; 11567 ctl_done((union ctl_io *)ctsio); 11568 return (retval); 11569 } 11570 } 11571 11572 11573 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11574 mtx_unlock(&lun->lun_lock); 11575 ctl_done((union ctl_io *)ctsio); 11576 return (retval); 11577 } 11578 11579 /* 11580 * XXX CHD this is where we want to send IO to other side if 11581 * this LUN is secondary on this SC. We will need to make a copy 11582 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11583 * the copy we send as FROM_OTHER. 11584 * We also need to stuff the address of the original IO so we can 11585 * find it easily. Something similar will need be done on the other 11586 * side so when we are done we can find the copy. 11587 */ 11588 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11589 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11590 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11591 union ctl_ha_msg msg_info; 11592 int isc_retval; 11593 11594 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11595 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11596 mtx_unlock(&lun->lun_lock); 11597 11598 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11599 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11600 msg_info.hdr.serializing_sc = NULL; 11601 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11602 msg_info.scsi.tag_num = ctsio->tag_num; 11603 msg_info.scsi.tag_type = ctsio->tag_type; 11604 msg_info.scsi.cdb_len = ctsio->cdb_len; 11605 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11606 11607 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11608 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11609 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11610 ctl_set_busy(ctsio); 11611 ctl_done((union ctl_io *)ctsio); 11612 return (retval); 11613 } 11614 return (retval); 11615 } 11616 11617 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11618 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11619 ctl_ooaq, ooa_links))) { 11620 case CTL_ACTION_BLOCK: 11621 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11622 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11623 blocked_links); 11624 mtx_unlock(&lun->lun_lock); 11625 return (retval); 11626 case CTL_ACTION_PASS: 11627 case CTL_ACTION_SKIP: 11628 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11629 mtx_unlock(&lun->lun_lock); 11630 ctl_enqueue_rtr((union ctl_io *)ctsio); 11631 break; 11632 case CTL_ACTION_OVERLAP: 11633 mtx_unlock(&lun->lun_lock); 11634 ctl_set_overlapped_cmd(ctsio); 11635 ctl_done((union ctl_io *)ctsio); 11636 break; 11637 case CTL_ACTION_OVERLAP_TAG: 11638 mtx_unlock(&lun->lun_lock); 11639 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11640 ctl_done((union ctl_io *)ctsio); 11641 break; 11642 case CTL_ACTION_ERROR: 11643 default: 11644 mtx_unlock(&lun->lun_lock); 11645 ctl_set_internal_failure(ctsio, 11646 /*sks_valid*/ 0, 11647 /*retry_count*/ 0); 11648 ctl_done((union ctl_io *)ctsio); 11649 break; 11650 } 11651 return (retval); 11652} 11653 11654const struct ctl_cmd_entry * 11655ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11656{ 11657 const struct ctl_cmd_entry *entry; 11658 int service_action; 11659 11660 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11661 if (sa) 11662 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11663 if (entry->flags & CTL_CMD_FLAG_SA5) { 11664 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11665 entry = &((const struct ctl_cmd_entry *) 11666 entry->execute)[service_action]; 11667 } 11668 return (entry); 11669} 11670 11671const struct ctl_cmd_entry * 11672ctl_validate_command(struct ctl_scsiio *ctsio) 11673{ 11674 const struct ctl_cmd_entry *entry; 11675 int i, sa; 11676 uint8_t diff; 11677 11678 entry = ctl_get_cmd_entry(ctsio, &sa); 11679 if (entry->execute == NULL) { 11680 if (sa) 11681 ctl_set_invalid_field(ctsio, 11682 /*sks_valid*/ 1, 11683 /*command*/ 1, 11684 /*field*/ 1, 11685 /*bit_valid*/ 1, 11686 /*bit*/ 4); 11687 else 11688 ctl_set_invalid_opcode(ctsio); 11689 ctl_done((union ctl_io *)ctsio); 11690 return (NULL); 11691 } 11692 KASSERT(entry->length > 0, 11693 ("Not defined length for command 0x%02x/0x%02x", 11694 ctsio->cdb[0], ctsio->cdb[1])); 11695 for (i = 1; i < entry->length; i++) { 11696 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11697 if (diff == 0) 11698 continue; 11699 ctl_set_invalid_field(ctsio, 11700 /*sks_valid*/ 1, 11701 /*command*/ 1, 11702 /*field*/ i, 11703 /*bit_valid*/ 1, 11704 /*bit*/ fls(diff) - 1); 11705 ctl_done((union ctl_io *)ctsio); 11706 return (NULL); 11707 } 11708 return (entry); 11709} 11710 11711static int 11712ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11713{ 11714 11715 switch (lun_type) { 11716 case T_DIRECT: 11717 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11718 return (0); 11719 break; 11720 case T_PROCESSOR: 11721 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11722 return (0); 11723 break; 11724 case T_CDROM: 11725 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11726 return (0); 11727 break; 11728 default: 11729 return (0); 11730 } 11731 return (1); 11732} 11733 11734static int 11735ctl_scsiio(struct ctl_scsiio *ctsio) 11736{ 11737 int retval; 11738 const struct ctl_cmd_entry *entry; 11739 11740 retval = CTL_RETVAL_COMPLETE; 11741 11742 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11743 11744 entry = ctl_get_cmd_entry(ctsio, NULL); 11745 11746 /* 11747 * If this I/O has been aborted, just send it straight to 11748 * ctl_done() without executing it. 11749 */ 11750 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11751 ctl_done((union ctl_io *)ctsio); 11752 goto bailout; 11753 } 11754 11755 /* 11756 * All the checks should have been handled by ctl_scsiio_precheck(). 11757 * We should be clear now to just execute the I/O. 11758 */ 11759 retval = entry->execute(ctsio); 11760 11761bailout: 11762 return (retval); 11763} 11764 11765/* 11766 * Since we only implement one target right now, a bus reset simply resets 11767 * our single target. 11768 */ 11769static int 11770ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11771{ 11772 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11773} 11774 11775static int 11776ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11777 ctl_ua_type ua_type) 11778{ 11779 struct ctl_port *port; 11780 struct ctl_lun *lun; 11781 int retval; 11782 11783 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11784 union ctl_ha_msg msg_info; 11785 11786 msg_info.hdr.nexus = io->io_hdr.nexus; 11787 if (ua_type==CTL_UA_TARG_RESET) 11788 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11789 else 11790 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11791 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11792 msg_info.hdr.original_sc = NULL; 11793 msg_info.hdr.serializing_sc = NULL; 11794 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11795 sizeof(msg_info.task), M_WAITOK); 11796 } 11797 retval = 0; 11798 11799 mtx_lock(&softc->ctl_lock); 11800 port = ctl_io_port(&io->io_hdr); 11801 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11802 if (port != NULL && 11803 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11804 continue; 11805 retval += ctl_do_lun_reset(lun, io, ua_type); 11806 } 11807 mtx_unlock(&softc->ctl_lock); 11808 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11809 return (retval); 11810} 11811 11812/* 11813 * The LUN should always be set. The I/O is optional, and is used to 11814 * distinguish between I/Os sent by this initiator, and by other 11815 * initiators. We set unit attention for initiators other than this one. 11816 * SAM-3 is vague on this point. It does say that a unit attention should 11817 * be established for other initiators when a LUN is reset (see section 11818 * 5.7.3), but it doesn't specifically say that the unit attention should 11819 * be established for this particular initiator when a LUN is reset. Here 11820 * is the relevant text, from SAM-3 rev 8: 11821 * 11822 * 5.7.2 When a SCSI initiator port aborts its own tasks 11823 * 11824 * When a SCSI initiator port causes its own task(s) to be aborted, no 11825 * notification that the task(s) have been aborted shall be returned to 11826 * the SCSI initiator port other than the completion response for the 11827 * command or task management function action that caused the task(s) to 11828 * be aborted and notification(s) associated with related effects of the 11829 * action (e.g., a reset unit attention condition). 11830 * 11831 * XXX KDM for now, we're setting unit attention for all initiators. 11832 */ 11833static int 11834ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11835{ 11836 union ctl_io *xio; 11837#if 0 11838 uint32_t initidx; 11839#endif 11840 int i; 11841 11842 mtx_lock(&lun->lun_lock); 11843 /* 11844 * Run through the OOA queue and abort each I/O. 11845 */ 11846 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11847 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11848 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11849 } 11850 11851 /* 11852 * This version sets unit attention for every 11853 */ 11854#if 0 11855 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11856 ctl_est_ua_all(lun, initidx, ua_type); 11857#else 11858 ctl_est_ua_all(lun, -1, ua_type); 11859#endif 11860 11861 /* 11862 * A reset (any kind, really) clears reservations established with 11863 * RESERVE/RELEASE. It does not clear reservations established 11864 * with PERSISTENT RESERVE OUT, but we don't support that at the 11865 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11866 * reservations made with the RESERVE/RELEASE commands, because 11867 * those commands are obsolete in SPC-3. 11868 */ 11869 lun->flags &= ~CTL_LUN_RESERVED; 11870 11871#ifdef CTL_WITH_CA 11872 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11873 ctl_clear_mask(lun->have_ca, i); 11874#endif 11875 lun->prevent_count = 0; 11876 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11877 ctl_clear_mask(lun->prevent, i); 11878 mtx_unlock(&lun->lun_lock); 11879 11880 return (0); 11881} 11882 11883static int 11884ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11885{ 11886 struct ctl_lun *lun; 11887 uint32_t targ_lun; 11888 int retval; 11889 11890 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11891 mtx_lock(&softc->ctl_lock); 11892 if ((targ_lun >= CTL_MAX_LUNS) || 11893 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11894 mtx_unlock(&softc->ctl_lock); 11895 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11896 return (1); 11897 } 11898 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11899 mtx_unlock(&softc->ctl_lock); 11900 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11901 11902 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11903 union ctl_ha_msg msg_info; 11904 11905 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11906 msg_info.hdr.nexus = io->io_hdr.nexus; 11907 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11908 msg_info.hdr.original_sc = NULL; 11909 msg_info.hdr.serializing_sc = NULL; 11910 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11911 sizeof(msg_info.task), M_WAITOK); 11912 } 11913 return (retval); 11914} 11915 11916static void 11917ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11918 int other_sc) 11919{ 11920 union ctl_io *xio; 11921 11922 mtx_assert(&lun->lun_lock, MA_OWNED); 11923 11924 /* 11925 * Run through the OOA queue and attempt to find the given I/O. 11926 * The target port, initiator ID, tag type and tag number have to 11927 * match the values that we got from the initiator. If we have an 11928 * untagged command to abort, simply abort the first untagged command 11929 * we come to. We only allow one untagged command at a time of course. 11930 */ 11931 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11932 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11933 11934 if ((targ_port == UINT32_MAX || 11935 targ_port == xio->io_hdr.nexus.targ_port) && 11936 (init_id == UINT32_MAX || 11937 init_id == xio->io_hdr.nexus.initid)) { 11938 if (targ_port != xio->io_hdr.nexus.targ_port || 11939 init_id != xio->io_hdr.nexus.initid) 11940 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11941 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11942 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11943 union ctl_ha_msg msg_info; 11944 11945 msg_info.hdr.nexus = xio->io_hdr.nexus; 11946 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11947 msg_info.task.tag_num = xio->scsiio.tag_num; 11948 msg_info.task.tag_type = xio->scsiio.tag_type; 11949 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11950 msg_info.hdr.original_sc = NULL; 11951 msg_info.hdr.serializing_sc = NULL; 11952 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11953 sizeof(msg_info.task), M_NOWAIT); 11954 } 11955 } 11956 } 11957} 11958 11959static int 11960ctl_abort_task_set(union ctl_io *io) 11961{ 11962 struct ctl_softc *softc = control_softc; 11963 struct ctl_lun *lun; 11964 uint32_t targ_lun; 11965 11966 /* 11967 * Look up the LUN. 11968 */ 11969 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11970 mtx_lock(&softc->ctl_lock); 11971 if ((targ_lun >= CTL_MAX_LUNS) || 11972 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11973 mtx_unlock(&softc->ctl_lock); 11974 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11975 return (1); 11976 } 11977 11978 mtx_lock(&lun->lun_lock); 11979 mtx_unlock(&softc->ctl_lock); 11980 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11981 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11982 io->io_hdr.nexus.initid, 11983 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11984 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11985 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11986 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11987 } 11988 mtx_unlock(&lun->lun_lock); 11989 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11990 return (0); 11991} 11992 11993static int 11994ctl_i_t_nexus_reset(union ctl_io *io) 11995{ 11996 struct ctl_softc *softc = control_softc; 11997 struct ctl_lun *lun; 11998 uint32_t initidx; 11999 12000 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12001 union ctl_ha_msg msg_info; 12002 12003 msg_info.hdr.nexus = io->io_hdr.nexus; 12004 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12005 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12006 msg_info.hdr.original_sc = NULL; 12007 msg_info.hdr.serializing_sc = NULL; 12008 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12009 sizeof(msg_info.task), M_WAITOK); 12010 } 12011 12012 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12013 mtx_lock(&softc->ctl_lock); 12014 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12015 mtx_lock(&lun->lun_lock); 12016 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12017 io->io_hdr.nexus.initid, 1); 12018#ifdef CTL_WITH_CA 12019 ctl_clear_mask(lun->have_ca, initidx); 12020#endif 12021 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12022 lun->flags &= ~CTL_LUN_RESERVED; 12023 if (ctl_is_set(lun->prevent, initidx)) { 12024 ctl_clear_mask(lun->prevent, initidx); 12025 lun->prevent_count--; 12026 } 12027 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12028 mtx_unlock(&lun->lun_lock); 12029 } 12030 mtx_unlock(&softc->ctl_lock); 12031 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12032 return (0); 12033} 12034 12035static int 12036ctl_abort_task(union ctl_io *io) 12037{ 12038 union ctl_io *xio; 12039 struct ctl_lun *lun; 12040 struct ctl_softc *softc; 12041#if 0 12042 struct sbuf sb; 12043 char printbuf[128]; 12044#endif 12045 int found; 12046 uint32_t targ_lun; 12047 12048 softc = control_softc; 12049 found = 0; 12050 12051 /* 12052 * Look up the LUN. 12053 */ 12054 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12055 mtx_lock(&softc->ctl_lock); 12056 if ((targ_lun >= CTL_MAX_LUNS) || 12057 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12058 mtx_unlock(&softc->ctl_lock); 12059 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12060 return (1); 12061 } 12062 12063#if 0 12064 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12065 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12066#endif 12067 12068 mtx_lock(&lun->lun_lock); 12069 mtx_unlock(&softc->ctl_lock); 12070 /* 12071 * Run through the OOA queue and attempt to find the given I/O. 12072 * The target port, initiator ID, tag type and tag number have to 12073 * match the values that we got from the initiator. If we have an 12074 * untagged command to abort, simply abort the first untagged command 12075 * we come to. We only allow one untagged command at a time of course. 12076 */ 12077 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12078 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12079#if 0 12080 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12081 12082 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12083 lun->lun, xio->scsiio.tag_num, 12084 xio->scsiio.tag_type, 12085 (xio->io_hdr.blocked_links.tqe_prev 12086 == NULL) ? "" : " BLOCKED", 12087 (xio->io_hdr.flags & 12088 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12089 (xio->io_hdr.flags & 12090 CTL_FLAG_ABORT) ? " ABORT" : "", 12091 (xio->io_hdr.flags & 12092 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12093 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12094 sbuf_finish(&sb); 12095 printf("%s\n", sbuf_data(&sb)); 12096#endif 12097 12098 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12099 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12100 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12101 continue; 12102 12103 /* 12104 * If the abort says that the task is untagged, the 12105 * task in the queue must be untagged. Otherwise, 12106 * we just check to see whether the tag numbers 12107 * match. This is because the QLogic firmware 12108 * doesn't pass back the tag type in an abort 12109 * request. 12110 */ 12111#if 0 12112 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12113 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12114 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12115#endif 12116 /* 12117 * XXX KDM we've got problems with FC, because it 12118 * doesn't send down a tag type with aborts. So we 12119 * can only really go by the tag number... 12120 * This may cause problems with parallel SCSI. 12121 * Need to figure that out!! 12122 */ 12123 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12124 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12125 found = 1; 12126 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12127 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12128 union ctl_ha_msg msg_info; 12129 12130 msg_info.hdr.nexus = io->io_hdr.nexus; 12131 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12132 msg_info.task.tag_num = io->taskio.tag_num; 12133 msg_info.task.tag_type = io->taskio.tag_type; 12134 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12135 msg_info.hdr.original_sc = NULL; 12136 msg_info.hdr.serializing_sc = NULL; 12137#if 0 12138 printf("Sent Abort to other side\n"); 12139#endif 12140 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12141 sizeof(msg_info.task), M_NOWAIT); 12142 } 12143#if 0 12144 printf("ctl_abort_task: found I/O to abort\n"); 12145#endif 12146 } 12147 } 12148 mtx_unlock(&lun->lun_lock); 12149 12150 if (found == 0) { 12151 /* 12152 * This isn't really an error. It's entirely possible for 12153 * the abort and command completion to cross on the wire. 12154 * This is more of an informative/diagnostic error. 12155 */ 12156#if 0 12157 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12158 "%u:%u:%u tag %d type %d\n", 12159 io->io_hdr.nexus.initid, 12160 io->io_hdr.nexus.targ_port, 12161 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12162 io->taskio.tag_type); 12163#endif 12164 } 12165 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12166 return (0); 12167} 12168 12169static int 12170ctl_query_task(union ctl_io *io, int task_set) 12171{ 12172 union ctl_io *xio; 12173 struct ctl_lun *lun; 12174 struct ctl_softc *softc; 12175 int found = 0; 12176 uint32_t targ_lun; 12177 12178 softc = control_softc; 12179 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12180 mtx_lock(&softc->ctl_lock); 12181 if ((targ_lun >= CTL_MAX_LUNS) || 12182 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12183 mtx_unlock(&softc->ctl_lock); 12184 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12185 return (1); 12186 } 12187 mtx_lock(&lun->lun_lock); 12188 mtx_unlock(&softc->ctl_lock); 12189 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12190 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12191 12192 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12193 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12194 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12195 continue; 12196 12197 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12198 found = 1; 12199 break; 12200 } 12201 } 12202 mtx_unlock(&lun->lun_lock); 12203 if (found) 12204 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12205 else 12206 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12207 return (0); 12208} 12209 12210static int 12211ctl_query_async_event(union ctl_io *io) 12212{ 12213 struct ctl_lun *lun; 12214 struct ctl_softc *softc; 12215 ctl_ua_type ua; 12216 uint32_t targ_lun, initidx; 12217 12218 softc = control_softc; 12219 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12220 mtx_lock(&softc->ctl_lock); 12221 if ((targ_lun >= CTL_MAX_LUNS) || 12222 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12223 mtx_unlock(&softc->ctl_lock); 12224 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12225 return (1); 12226 } 12227 mtx_lock(&lun->lun_lock); 12228 mtx_unlock(&softc->ctl_lock); 12229 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12230 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12231 mtx_unlock(&lun->lun_lock); 12232 if (ua != CTL_UA_NONE) 12233 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12234 else 12235 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12236 return (0); 12237} 12238 12239static void 12240ctl_run_task(union ctl_io *io) 12241{ 12242 struct ctl_softc *softc = control_softc; 12243 int retval = 1; 12244 12245 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12246 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12247 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12248 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12249 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12250 switch (io->taskio.task_action) { 12251 case CTL_TASK_ABORT_TASK: 12252 retval = ctl_abort_task(io); 12253 break; 12254 case CTL_TASK_ABORT_TASK_SET: 12255 case CTL_TASK_CLEAR_TASK_SET: 12256 retval = ctl_abort_task_set(io); 12257 break; 12258 case CTL_TASK_CLEAR_ACA: 12259 break; 12260 case CTL_TASK_I_T_NEXUS_RESET: 12261 retval = ctl_i_t_nexus_reset(io); 12262 break; 12263 case CTL_TASK_LUN_RESET: 12264 retval = ctl_lun_reset(softc, io); 12265 break; 12266 case CTL_TASK_TARGET_RESET: 12267 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12268 break; 12269 case CTL_TASK_BUS_RESET: 12270 retval = ctl_bus_reset(softc, io); 12271 break; 12272 case CTL_TASK_PORT_LOGIN: 12273 break; 12274 case CTL_TASK_PORT_LOGOUT: 12275 break; 12276 case CTL_TASK_QUERY_TASK: 12277 retval = ctl_query_task(io, 0); 12278 break; 12279 case CTL_TASK_QUERY_TASK_SET: 12280 retval = ctl_query_task(io, 1); 12281 break; 12282 case CTL_TASK_QUERY_ASYNC_EVENT: 12283 retval = ctl_query_async_event(io); 12284 break; 12285 default: 12286 printf("%s: got unknown task management event %d\n", 12287 __func__, io->taskio.task_action); 12288 break; 12289 } 12290 if (retval == 0) 12291 io->io_hdr.status = CTL_SUCCESS; 12292 else 12293 io->io_hdr.status = CTL_ERROR; 12294 ctl_done(io); 12295} 12296 12297/* 12298 * For HA operation. Handle commands that come in from the other 12299 * controller. 12300 */ 12301static void 12302ctl_handle_isc(union ctl_io *io) 12303{ 12304 int free_io; 12305 struct ctl_lun *lun; 12306 struct ctl_softc *softc = control_softc; 12307 uint32_t targ_lun; 12308 12309 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12310 lun = softc->ctl_luns[targ_lun]; 12311 12312 switch (io->io_hdr.msg_type) { 12313 case CTL_MSG_SERIALIZE: 12314 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12315 break; 12316 case CTL_MSG_R2R: { 12317 const struct ctl_cmd_entry *entry; 12318 12319 /* 12320 * This is only used in SER_ONLY mode. 12321 */ 12322 free_io = 0; 12323 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12324 mtx_lock(&lun->lun_lock); 12325 if (ctl_scsiio_lun_check(lun, 12326 entry, (struct ctl_scsiio *)io) != 0) { 12327 mtx_unlock(&lun->lun_lock); 12328 ctl_done(io); 12329 break; 12330 } 12331 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12332 mtx_unlock(&lun->lun_lock); 12333 ctl_enqueue_rtr(io); 12334 break; 12335 } 12336 case CTL_MSG_FINISH_IO: 12337 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12338 free_io = 0; 12339 ctl_done(io); 12340 } else { 12341 free_io = 1; 12342 mtx_lock(&lun->lun_lock); 12343 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12344 ooa_links); 12345 ctl_check_blocked(lun); 12346 mtx_unlock(&lun->lun_lock); 12347 } 12348 break; 12349 case CTL_MSG_PERS_ACTION: 12350 ctl_hndl_per_res_out_on_other_sc( 12351 (union ctl_ha_msg *)&io->presio.pr_msg); 12352 free_io = 1; 12353 break; 12354 case CTL_MSG_BAD_JUJU: 12355 free_io = 0; 12356 ctl_done(io); 12357 break; 12358 case CTL_MSG_DATAMOVE: 12359 /* Only used in XFER mode */ 12360 free_io = 0; 12361 ctl_datamove_remote(io); 12362 break; 12363 case CTL_MSG_DATAMOVE_DONE: 12364 /* Only used in XFER mode */ 12365 free_io = 0; 12366 io->scsiio.be_move_done(io); 12367 break; 12368 case CTL_MSG_FAILOVER: 12369 ctl_failover_lun(io); 12370 free_io = 1; 12371 break; 12372 default: 12373 free_io = 1; 12374 printf("%s: Invalid message type %d\n", 12375 __func__, io->io_hdr.msg_type); 12376 break; 12377 } 12378 if (free_io) 12379 ctl_free_io(io); 12380 12381} 12382 12383 12384/* 12385 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12386 * there is no match. 12387 */ 12388static ctl_lun_error_pattern 12389ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12390{ 12391 const struct ctl_cmd_entry *entry; 12392 ctl_lun_error_pattern filtered_pattern, pattern; 12393 12394 pattern = desc->error_pattern; 12395 12396 /* 12397 * XXX KDM we need more data passed into this function to match a 12398 * custom pattern, and we actually need to implement custom pattern 12399 * matching. 12400 */ 12401 if (pattern & CTL_LUN_PAT_CMD) 12402 return (CTL_LUN_PAT_CMD); 12403 12404 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12405 return (CTL_LUN_PAT_ANY); 12406 12407 entry = ctl_get_cmd_entry(ctsio, NULL); 12408 12409 filtered_pattern = entry->pattern & pattern; 12410 12411 /* 12412 * If the user requested specific flags in the pattern (e.g. 12413 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12414 * flags. 12415 * 12416 * If the user did not specify any flags, it doesn't matter whether 12417 * or not the command supports the flags. 12418 */ 12419 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12420 (pattern & ~CTL_LUN_PAT_MASK)) 12421 return (CTL_LUN_PAT_NONE); 12422 12423 /* 12424 * If the user asked for a range check, see if the requested LBA 12425 * range overlaps with this command's LBA range. 12426 */ 12427 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12428 uint64_t lba1; 12429 uint64_t len1; 12430 ctl_action action; 12431 int retval; 12432 12433 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12434 if (retval != 0) 12435 return (CTL_LUN_PAT_NONE); 12436 12437 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12438 desc->lba_range.len, FALSE); 12439 /* 12440 * A "pass" means that the LBA ranges don't overlap, so 12441 * this doesn't match the user's range criteria. 12442 */ 12443 if (action == CTL_ACTION_PASS) 12444 return (CTL_LUN_PAT_NONE); 12445 } 12446 12447 return (filtered_pattern); 12448} 12449 12450static void 12451ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12452{ 12453 struct ctl_error_desc *desc, *desc2; 12454 12455 mtx_assert(&lun->lun_lock, MA_OWNED); 12456 12457 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12458 ctl_lun_error_pattern pattern; 12459 /* 12460 * Check to see whether this particular command matches 12461 * the pattern in the descriptor. 12462 */ 12463 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12464 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12465 continue; 12466 12467 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12468 case CTL_LUN_INJ_ABORTED: 12469 ctl_set_aborted(&io->scsiio); 12470 break; 12471 case CTL_LUN_INJ_MEDIUM_ERR: 12472 ctl_set_medium_error(&io->scsiio, 12473 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12474 CTL_FLAG_DATA_OUT); 12475 break; 12476 case CTL_LUN_INJ_UA: 12477 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12478 * OCCURRED */ 12479 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12480 break; 12481 case CTL_LUN_INJ_CUSTOM: 12482 /* 12483 * We're assuming the user knows what he is doing. 12484 * Just copy the sense information without doing 12485 * checks. 12486 */ 12487 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12488 MIN(sizeof(desc->custom_sense), 12489 sizeof(io->scsiio.sense_data))); 12490 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12491 io->scsiio.sense_len = SSD_FULL_SIZE; 12492 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12493 break; 12494 case CTL_LUN_INJ_NONE: 12495 default: 12496 /* 12497 * If this is an error injection type we don't know 12498 * about, clear the continuous flag (if it is set) 12499 * so it will get deleted below. 12500 */ 12501 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12502 break; 12503 } 12504 /* 12505 * By default, each error injection action is a one-shot 12506 */ 12507 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12508 continue; 12509 12510 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12511 12512 free(desc, M_CTL); 12513 } 12514} 12515 12516#ifdef CTL_IO_DELAY 12517static void 12518ctl_datamove_timer_wakeup(void *arg) 12519{ 12520 union ctl_io *io; 12521 12522 io = (union ctl_io *)arg; 12523 12524 ctl_datamove(io); 12525} 12526#endif /* CTL_IO_DELAY */ 12527 12528void 12529ctl_datamove(union ctl_io *io) 12530{ 12531 struct ctl_lun *lun; 12532 void (*fe_datamove)(union ctl_io *io); 12533 12534 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12535 12536 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12537 12538 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12539#ifdef CTL_TIME_IO 12540 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12541 char str[256]; 12542 char path_str[64]; 12543 struct sbuf sb; 12544 12545 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12546 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12547 12548 sbuf_cat(&sb, path_str); 12549 switch (io->io_hdr.io_type) { 12550 case CTL_IO_SCSI: 12551 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12552 sbuf_printf(&sb, "\n"); 12553 sbuf_cat(&sb, path_str); 12554 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12555 io->scsiio.tag_num, io->scsiio.tag_type); 12556 break; 12557 case CTL_IO_TASK: 12558 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12559 "Tag Type: %d\n", io->taskio.task_action, 12560 io->taskio.tag_num, io->taskio.tag_type); 12561 break; 12562 default: 12563 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12564 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12565 break; 12566 } 12567 sbuf_cat(&sb, path_str); 12568 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12569 (intmax_t)time_uptime - io->io_hdr.start_time); 12570 sbuf_finish(&sb); 12571 printf("%s", sbuf_data(&sb)); 12572 } 12573#endif /* CTL_TIME_IO */ 12574 12575#ifdef CTL_IO_DELAY 12576 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12577 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12578 } else { 12579 if ((lun != NULL) 12580 && (lun->delay_info.datamove_delay > 0)) { 12581 12582 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12583 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12584 callout_reset(&io->io_hdr.delay_callout, 12585 lun->delay_info.datamove_delay * hz, 12586 ctl_datamove_timer_wakeup, io); 12587 if (lun->delay_info.datamove_type == 12588 CTL_DELAY_TYPE_ONESHOT) 12589 lun->delay_info.datamove_delay = 0; 12590 return; 12591 } 12592 } 12593#endif 12594 12595 /* 12596 * This command has been aborted. Set the port status, so we fail 12597 * the data move. 12598 */ 12599 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12600 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12601 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12602 io->io_hdr.nexus.targ_port, 12603 io->io_hdr.nexus.targ_lun); 12604 io->io_hdr.port_status = 31337; 12605 /* 12606 * Note that the backend, in this case, will get the 12607 * callback in its context. In other cases it may get 12608 * called in the frontend's interrupt thread context. 12609 */ 12610 io->scsiio.be_move_done(io); 12611 return; 12612 } 12613 12614 /* Don't confuse frontend with zero length data move. */ 12615 if (io->scsiio.kern_data_len == 0) { 12616 io->scsiio.be_move_done(io); 12617 return; 12618 } 12619 12620 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12621 fe_datamove(io); 12622} 12623 12624static void 12625ctl_send_datamove_done(union ctl_io *io, int have_lock) 12626{ 12627 union ctl_ha_msg msg; 12628#ifdef CTL_TIME_IO 12629 struct bintime cur_bt; 12630#endif 12631 12632 memset(&msg, 0, sizeof(msg)); 12633 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12634 msg.hdr.original_sc = io; 12635 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12636 msg.hdr.nexus = io->io_hdr.nexus; 12637 msg.hdr.status = io->io_hdr.status; 12638 msg.scsi.tag_num = io->scsiio.tag_num; 12639 msg.scsi.tag_type = io->scsiio.tag_type; 12640 msg.scsi.scsi_status = io->scsiio.scsi_status; 12641 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12642 io->scsiio.sense_len); 12643 msg.scsi.sense_len = io->scsiio.sense_len; 12644 msg.scsi.sense_residual = io->scsiio.sense_residual; 12645 msg.scsi.fetd_status = io->io_hdr.port_status; 12646 msg.scsi.residual = io->scsiio.residual; 12647 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12648 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12649 ctl_failover_io(io, /*have_lock*/ have_lock); 12650 return; 12651 } 12652 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12653 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12654 msg.scsi.sense_len, M_WAITOK); 12655 12656#ifdef CTL_TIME_IO 12657 getbinuptime(&cur_bt); 12658 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12659 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12660#endif 12661 io->io_hdr.num_dmas++; 12662} 12663 12664/* 12665 * The DMA to the remote side is done, now we need to tell the other side 12666 * we're done so it can continue with its data movement. 12667 */ 12668static void 12669ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12670{ 12671 union ctl_io *io; 12672 int i; 12673 12674 io = rq->context; 12675 12676 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12677 printf("%s: ISC DMA write failed with error %d", __func__, 12678 rq->ret); 12679 ctl_set_internal_failure(&io->scsiio, 12680 /*sks_valid*/ 1, 12681 /*retry_count*/ rq->ret); 12682 } 12683 12684 ctl_dt_req_free(rq); 12685 12686 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12687 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12688 free(io->io_hdr.remote_sglist, M_CTL); 12689 io->io_hdr.remote_sglist = NULL; 12690 io->io_hdr.local_sglist = NULL; 12691 12692 /* 12693 * The data is in local and remote memory, so now we need to send 12694 * status (good or back) back to the other side. 12695 */ 12696 ctl_send_datamove_done(io, /*have_lock*/ 0); 12697} 12698 12699/* 12700 * We've moved the data from the host/controller into local memory. Now we 12701 * need to push it over to the remote controller's memory. 12702 */ 12703static int 12704ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12705{ 12706 int retval; 12707 12708 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12709 ctl_datamove_remote_write_cb); 12710 return (retval); 12711} 12712 12713static void 12714ctl_datamove_remote_write(union ctl_io *io) 12715{ 12716 int retval; 12717 void (*fe_datamove)(union ctl_io *io); 12718 12719 /* 12720 * - Get the data from the host/HBA into local memory. 12721 * - DMA memory from the local controller to the remote controller. 12722 * - Send status back to the remote controller. 12723 */ 12724 12725 retval = ctl_datamove_remote_sgl_setup(io); 12726 if (retval != 0) 12727 return; 12728 12729 /* Switch the pointer over so the FETD knows what to do */ 12730 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12731 12732 /* 12733 * Use a custom move done callback, since we need to send completion 12734 * back to the other controller, not to the backend on this side. 12735 */ 12736 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12737 12738 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12739 fe_datamove(io); 12740} 12741 12742static int 12743ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12744{ 12745#if 0 12746 char str[256]; 12747 char path_str[64]; 12748 struct sbuf sb; 12749#endif 12750 int i; 12751 12752 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12753 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12754 free(io->io_hdr.remote_sglist, M_CTL); 12755 io->io_hdr.remote_sglist = NULL; 12756 io->io_hdr.local_sglist = NULL; 12757 12758#if 0 12759 scsi_path_string(io, path_str, sizeof(path_str)); 12760 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12761 sbuf_cat(&sb, path_str); 12762 scsi_command_string(&io->scsiio, NULL, &sb); 12763 sbuf_printf(&sb, "\n"); 12764 sbuf_cat(&sb, path_str); 12765 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12766 io->scsiio.tag_num, io->scsiio.tag_type); 12767 sbuf_cat(&sb, path_str); 12768 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12769 io->io_hdr.flags, io->io_hdr.status); 12770 sbuf_finish(&sb); 12771 printk("%s", sbuf_data(&sb)); 12772#endif 12773 12774 12775 /* 12776 * The read is done, now we need to send status (good or bad) back 12777 * to the other side. 12778 */ 12779 ctl_send_datamove_done(io, /*have_lock*/ 0); 12780 12781 return (0); 12782} 12783 12784static void 12785ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12786{ 12787 union ctl_io *io; 12788 void (*fe_datamove)(union ctl_io *io); 12789 12790 io = rq->context; 12791 12792 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12793 printf("%s: ISC DMA read failed with error %d\n", __func__, 12794 rq->ret); 12795 ctl_set_internal_failure(&io->scsiio, 12796 /*sks_valid*/ 1, 12797 /*retry_count*/ rq->ret); 12798 } 12799 12800 ctl_dt_req_free(rq); 12801 12802 /* Switch the pointer over so the FETD knows what to do */ 12803 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12804 12805 /* 12806 * Use a custom move done callback, since we need to send completion 12807 * back to the other controller, not to the backend on this side. 12808 */ 12809 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12810 12811 /* XXX KDM add checks like the ones in ctl_datamove? */ 12812 12813 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12814 fe_datamove(io); 12815} 12816 12817static int 12818ctl_datamove_remote_sgl_setup(union ctl_io *io) 12819{ 12820 struct ctl_sg_entry *local_sglist; 12821 uint32_t len_to_go; 12822 int retval; 12823 int i; 12824 12825 retval = 0; 12826 local_sglist = io->io_hdr.local_sglist; 12827 len_to_go = io->scsiio.kern_data_len; 12828 12829 /* 12830 * The difficult thing here is that the size of the various 12831 * S/G segments may be different than the size from the 12832 * remote controller. That'll make it harder when DMAing 12833 * the data back to the other side. 12834 */ 12835 for (i = 0; len_to_go > 0; i++) { 12836 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12837 local_sglist[i].addr = 12838 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12839 12840 len_to_go -= local_sglist[i].len; 12841 } 12842 /* 12843 * Reset the number of S/G entries accordingly. The original 12844 * number of S/G entries is available in rem_sg_entries. 12845 */ 12846 io->scsiio.kern_sg_entries = i; 12847 12848#if 0 12849 printf("%s: kern_sg_entries = %d\n", __func__, 12850 io->scsiio.kern_sg_entries); 12851 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12852 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12853 local_sglist[i].addr, local_sglist[i].len); 12854#endif 12855 12856 return (retval); 12857} 12858 12859static int 12860ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12861 ctl_ha_dt_cb callback) 12862{ 12863 struct ctl_ha_dt_req *rq; 12864 struct ctl_sg_entry *remote_sglist, *local_sglist; 12865 uint32_t local_used, remote_used, total_used; 12866 int i, j, isc_ret; 12867 12868 rq = ctl_dt_req_alloc(); 12869 12870 /* 12871 * If we failed to allocate the request, and if the DMA didn't fail 12872 * anyway, set busy status. This is just a resource allocation 12873 * failure. 12874 */ 12875 if ((rq == NULL) 12876 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12877 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12878 ctl_set_busy(&io->scsiio); 12879 12880 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12881 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12882 12883 if (rq != NULL) 12884 ctl_dt_req_free(rq); 12885 12886 /* 12887 * The data move failed. We need to return status back 12888 * to the other controller. No point in trying to DMA 12889 * data to the remote controller. 12890 */ 12891 12892 ctl_send_datamove_done(io, /*have_lock*/ 0); 12893 12894 return (1); 12895 } 12896 12897 local_sglist = io->io_hdr.local_sglist; 12898 remote_sglist = io->io_hdr.remote_sglist; 12899 local_used = 0; 12900 remote_used = 0; 12901 total_used = 0; 12902 12903 /* 12904 * Pull/push the data over the wire from/to the other controller. 12905 * This takes into account the possibility that the local and 12906 * remote sglists may not be identical in terms of the size of 12907 * the elements and the number of elements. 12908 * 12909 * One fundamental assumption here is that the length allocated for 12910 * both the local and remote sglists is identical. Otherwise, we've 12911 * essentially got a coding error of some sort. 12912 */ 12913 isc_ret = CTL_HA_STATUS_SUCCESS; 12914 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12915 uint32_t cur_len; 12916 uint8_t *tmp_ptr; 12917 12918 rq->command = command; 12919 rq->context = io; 12920 12921 /* 12922 * Both pointers should be aligned. But it is possible 12923 * that the allocation length is not. They should both 12924 * also have enough slack left over at the end, though, 12925 * to round up to the next 8 byte boundary. 12926 */ 12927 cur_len = MIN(local_sglist[i].len - local_used, 12928 remote_sglist[j].len - remote_used); 12929 rq->size = cur_len; 12930 12931 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12932 tmp_ptr += local_used; 12933 12934#if 0 12935 /* Use physical addresses when talking to ISC hardware */ 12936 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12937 /* XXX KDM use busdma */ 12938 rq->local = vtophys(tmp_ptr); 12939 } else 12940 rq->local = tmp_ptr; 12941#else 12942 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12943 ("HA does not support BUS_ADDR")); 12944 rq->local = tmp_ptr; 12945#endif 12946 12947 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12948 tmp_ptr += remote_used; 12949 rq->remote = tmp_ptr; 12950 12951 rq->callback = NULL; 12952 12953 local_used += cur_len; 12954 if (local_used >= local_sglist[i].len) { 12955 i++; 12956 local_used = 0; 12957 } 12958 12959 remote_used += cur_len; 12960 if (remote_used >= remote_sglist[j].len) { 12961 j++; 12962 remote_used = 0; 12963 } 12964 total_used += cur_len; 12965 12966 if (total_used >= io->scsiio.kern_data_len) 12967 rq->callback = callback; 12968 12969#if 0 12970 printf("%s: %s: local %p remote %p size %d\n", __func__, 12971 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12972 rq->local, rq->remote, rq->size); 12973#endif 12974 12975 isc_ret = ctl_dt_single(rq); 12976 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12977 break; 12978 } 12979 if (isc_ret != CTL_HA_STATUS_WAIT) { 12980 rq->ret = isc_ret; 12981 callback(rq); 12982 } 12983 12984 return (0); 12985} 12986 12987static void 12988ctl_datamove_remote_read(union ctl_io *io) 12989{ 12990 int retval; 12991 int i; 12992 12993 /* 12994 * This will send an error to the other controller in the case of a 12995 * failure. 12996 */ 12997 retval = ctl_datamove_remote_sgl_setup(io); 12998 if (retval != 0) 12999 return; 13000 13001 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13002 ctl_datamove_remote_read_cb); 13003 if (retval != 0) { 13004 /* 13005 * Make sure we free memory if there was an error.. The 13006 * ctl_datamove_remote_xfer() function will send the 13007 * datamove done message, or call the callback with an 13008 * error if there is a problem. 13009 */ 13010 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13011 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13012 free(io->io_hdr.remote_sglist, M_CTL); 13013 io->io_hdr.remote_sglist = NULL; 13014 io->io_hdr.local_sglist = NULL; 13015 } 13016} 13017 13018/* 13019 * Process a datamove request from the other controller. This is used for 13020 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13021 * first. Once that is complete, the data gets DMAed into the remote 13022 * controller's memory. For reads, we DMA from the remote controller's 13023 * memory into our memory first, and then move it out to the FETD. 13024 */ 13025static void 13026ctl_datamove_remote(union ctl_io *io) 13027{ 13028 13029 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13030 13031 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13032 ctl_failover_io(io, /*have_lock*/ 0); 13033 return; 13034 } 13035 13036 /* 13037 * Note that we look for an aborted I/O here, but don't do some of 13038 * the other checks that ctl_datamove() normally does. 13039 * We don't need to run the datamove delay code, since that should 13040 * have been done if need be on the other controller. 13041 */ 13042 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13043 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13044 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13045 io->io_hdr.nexus.targ_port, 13046 io->io_hdr.nexus.targ_lun); 13047 io->io_hdr.port_status = 31338; 13048 ctl_send_datamove_done(io, /*have_lock*/ 0); 13049 return; 13050 } 13051 13052 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13053 ctl_datamove_remote_write(io); 13054 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13055 ctl_datamove_remote_read(io); 13056 else { 13057 io->io_hdr.port_status = 31339; 13058 ctl_send_datamove_done(io, /*have_lock*/ 0); 13059 } 13060} 13061 13062static void 13063ctl_process_done(union ctl_io *io) 13064{ 13065 struct ctl_lun *lun; 13066 struct ctl_softc *softc = control_softc; 13067 void (*fe_done)(union ctl_io *io); 13068 union ctl_ha_msg msg; 13069 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13070 13071 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13072 fe_done = softc->ctl_ports[targ_port]->fe_done; 13073 13074#ifdef CTL_TIME_IO 13075 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13076 char str[256]; 13077 char path_str[64]; 13078 struct sbuf sb; 13079 13080 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13081 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13082 13083 sbuf_cat(&sb, path_str); 13084 switch (io->io_hdr.io_type) { 13085 case CTL_IO_SCSI: 13086 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13087 sbuf_printf(&sb, "\n"); 13088 sbuf_cat(&sb, path_str); 13089 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13090 io->scsiio.tag_num, io->scsiio.tag_type); 13091 break; 13092 case CTL_IO_TASK: 13093 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13094 "Tag Type: %d\n", io->taskio.task_action, 13095 io->taskio.tag_num, io->taskio.tag_type); 13096 break; 13097 default: 13098 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13099 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13100 break; 13101 } 13102 sbuf_cat(&sb, path_str); 13103 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13104 (intmax_t)time_uptime - io->io_hdr.start_time); 13105 sbuf_finish(&sb); 13106 printf("%s", sbuf_data(&sb)); 13107 } 13108#endif /* CTL_TIME_IO */ 13109 13110 switch (io->io_hdr.io_type) { 13111 case CTL_IO_SCSI: 13112 break; 13113 case CTL_IO_TASK: 13114 if (ctl_debug & CTL_DEBUG_INFO) 13115 ctl_io_error_print(io, NULL); 13116 fe_done(io); 13117 return; 13118 default: 13119 panic("ctl_process_done: invalid io type %d\n", 13120 io->io_hdr.io_type); 13121 break; /* NOTREACHED */ 13122 } 13123 13124 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13125 if (lun == NULL) { 13126 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13127 io->io_hdr.nexus.targ_mapped_lun)); 13128 goto bailout; 13129 } 13130 13131 mtx_lock(&lun->lun_lock); 13132 13133 /* 13134 * Check to see if we have any errors to inject here. We only 13135 * inject errors for commands that don't already have errors set. 13136 */ 13137 if (!STAILQ_EMPTY(&lun->error_list) && 13138 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13139 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13140 ctl_inject_error(lun, io); 13141 13142 /* 13143 * XXX KDM how do we treat commands that aren't completed 13144 * successfully? 13145 * 13146 * XXX KDM should we also track I/O latency? 13147 */ 13148 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13149 io->io_hdr.io_type == CTL_IO_SCSI) { 13150#ifdef CTL_TIME_IO 13151 struct bintime cur_bt; 13152#endif 13153 int type; 13154 13155 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13156 CTL_FLAG_DATA_IN) 13157 type = CTL_STATS_READ; 13158 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13159 CTL_FLAG_DATA_OUT) 13160 type = CTL_STATS_WRITE; 13161 else 13162 type = CTL_STATS_NO_IO; 13163 13164 lun->stats.ports[targ_port].bytes[type] += 13165 io->scsiio.kern_total_len; 13166 lun->stats.ports[targ_port].operations[type]++; 13167#ifdef CTL_TIME_IO 13168 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13169 &io->io_hdr.dma_bt); 13170 getbinuptime(&cur_bt); 13171 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13172 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13173#endif 13174 lun->stats.ports[targ_port].num_dmas[type] += 13175 io->io_hdr.num_dmas; 13176 } 13177 13178 /* 13179 * Remove this from the OOA queue. 13180 */ 13181 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13182#ifdef CTL_TIME_IO 13183 if (TAILQ_EMPTY(&lun->ooa_queue)) 13184 lun->last_busy = getsbinuptime(); 13185#endif 13186 13187 /* 13188 * Run through the blocked queue on this LUN and see if anything 13189 * has become unblocked, now that this transaction is done. 13190 */ 13191 ctl_check_blocked(lun); 13192 13193 /* 13194 * If the LUN has been invalidated, free it if there is nothing 13195 * left on its OOA queue. 13196 */ 13197 if ((lun->flags & CTL_LUN_INVALID) 13198 && TAILQ_EMPTY(&lun->ooa_queue)) { 13199 mtx_unlock(&lun->lun_lock); 13200 mtx_lock(&softc->ctl_lock); 13201 ctl_free_lun(lun); 13202 mtx_unlock(&softc->ctl_lock); 13203 } else 13204 mtx_unlock(&lun->lun_lock); 13205 13206bailout: 13207 13208 /* 13209 * If this command has been aborted, make sure we set the status 13210 * properly. The FETD is responsible for freeing the I/O and doing 13211 * whatever it needs to do to clean up its state. 13212 */ 13213 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13214 ctl_set_task_aborted(&io->scsiio); 13215 13216 /* 13217 * If enabled, print command error status. 13218 */ 13219 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13220 (ctl_debug & CTL_DEBUG_INFO) != 0) 13221 ctl_io_error_print(io, NULL); 13222 13223 /* 13224 * Tell the FETD or the other shelf controller we're done with this 13225 * command. Note that only SCSI commands get to this point. Task 13226 * management commands are completed above. 13227 */ 13228 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13229 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13230 memset(&msg, 0, sizeof(msg)); 13231 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13232 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13233 msg.hdr.nexus = io->io_hdr.nexus; 13234 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13235 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13236 M_WAITOK); 13237 } 13238 13239 fe_done(io); 13240} 13241 13242#ifdef CTL_WITH_CA 13243/* 13244 * Front end should call this if it doesn't do autosense. When the request 13245 * sense comes back in from the initiator, we'll dequeue this and send it. 13246 */ 13247int 13248ctl_queue_sense(union ctl_io *io) 13249{ 13250 struct ctl_lun *lun; 13251 struct ctl_port *port; 13252 struct ctl_softc *softc; 13253 uint32_t initidx, targ_lun; 13254 13255 softc = control_softc; 13256 13257 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13258 13259 /* 13260 * LUN lookup will likely move to the ctl_work_thread() once we 13261 * have our new queueing infrastructure (that doesn't put things on 13262 * a per-LUN queue initially). That is so that we can handle 13263 * things like an INQUIRY to a LUN that we don't have enabled. We 13264 * can't deal with that right now. 13265 */ 13266 mtx_lock(&softc->ctl_lock); 13267 13268 /* 13269 * If we don't have a LUN for this, just toss the sense 13270 * information. 13271 */ 13272 port = ctl_io_port(&ctsio->io_hdr); 13273 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13274 if ((targ_lun < CTL_MAX_LUNS) 13275 && (softc->ctl_luns[targ_lun] != NULL)) 13276 lun = softc->ctl_luns[targ_lun]; 13277 else 13278 goto bailout; 13279 13280 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13281 13282 mtx_lock(&lun->lun_lock); 13283 /* 13284 * Already have CA set for this LUN...toss the sense information. 13285 */ 13286 if (ctl_is_set(lun->have_ca, initidx)) { 13287 mtx_unlock(&lun->lun_lock); 13288 goto bailout; 13289 } 13290 13291 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13292 MIN(sizeof(lun->pending_sense[initidx]), 13293 sizeof(io->scsiio.sense_data))); 13294 ctl_set_mask(lun->have_ca, initidx); 13295 mtx_unlock(&lun->lun_lock); 13296 13297bailout: 13298 mtx_unlock(&softc->ctl_lock); 13299 13300 ctl_free_io(io); 13301 13302 return (CTL_RETVAL_COMPLETE); 13303} 13304#endif 13305 13306/* 13307 * Primary command inlet from frontend ports. All SCSI and task I/O 13308 * requests must go through this function. 13309 */ 13310int 13311ctl_queue(union ctl_io *io) 13312{ 13313 struct ctl_port *port; 13314 13315 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13316 13317#ifdef CTL_TIME_IO 13318 io->io_hdr.start_time = time_uptime; 13319 getbinuptime(&io->io_hdr.start_bt); 13320#endif /* CTL_TIME_IO */ 13321 13322 /* Map FE-specific LUN ID into global one. */ 13323 port = ctl_io_port(&io->io_hdr); 13324 io->io_hdr.nexus.targ_mapped_lun = 13325 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13326 13327 switch (io->io_hdr.io_type) { 13328 case CTL_IO_SCSI: 13329 case CTL_IO_TASK: 13330 if (ctl_debug & CTL_DEBUG_CDB) 13331 ctl_io_print(io); 13332 ctl_enqueue_incoming(io); 13333 break; 13334 default: 13335 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13336 return (EINVAL); 13337 } 13338 13339 return (CTL_RETVAL_COMPLETE); 13340} 13341 13342#ifdef CTL_IO_DELAY 13343static void 13344ctl_done_timer_wakeup(void *arg) 13345{ 13346 union ctl_io *io; 13347 13348 io = (union ctl_io *)arg; 13349 ctl_done(io); 13350} 13351#endif /* CTL_IO_DELAY */ 13352 13353void 13354ctl_serseq_done(union ctl_io *io) 13355{ 13356 struct ctl_lun *lun; 13357 13358 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13359 if (lun->be_lun == NULL || 13360 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13361 return; 13362 mtx_lock(&lun->lun_lock); 13363 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13364 ctl_check_blocked(lun); 13365 mtx_unlock(&lun->lun_lock); 13366} 13367 13368void 13369ctl_done(union ctl_io *io) 13370{ 13371 13372 /* 13373 * Enable this to catch duplicate completion issues. 13374 */ 13375#if 0 13376 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13377 printf("%s: type %d msg %d cdb %x iptl: " 13378 "%u:%u:%u tag 0x%04x " 13379 "flag %#x status %x\n", 13380 __func__, 13381 io->io_hdr.io_type, 13382 io->io_hdr.msg_type, 13383 io->scsiio.cdb[0], 13384 io->io_hdr.nexus.initid, 13385 io->io_hdr.nexus.targ_port, 13386 io->io_hdr.nexus.targ_lun, 13387 (io->io_hdr.io_type == 13388 CTL_IO_TASK) ? 13389 io->taskio.tag_num : 13390 io->scsiio.tag_num, 13391 io->io_hdr.flags, 13392 io->io_hdr.status); 13393 } else 13394 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13395#endif 13396 13397 /* 13398 * This is an internal copy of an I/O, and should not go through 13399 * the normal done processing logic. 13400 */ 13401 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13402 return; 13403 13404#ifdef CTL_IO_DELAY 13405 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13406 struct ctl_lun *lun; 13407 13408 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13409 13410 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13411 } else { 13412 struct ctl_lun *lun; 13413 13414 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13415 13416 if ((lun != NULL) 13417 && (lun->delay_info.done_delay > 0)) { 13418 13419 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13420 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13421 callout_reset(&io->io_hdr.delay_callout, 13422 lun->delay_info.done_delay * hz, 13423 ctl_done_timer_wakeup, io); 13424 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13425 lun->delay_info.done_delay = 0; 13426 return; 13427 } 13428 } 13429#endif /* CTL_IO_DELAY */ 13430 13431 ctl_enqueue_done(io); 13432} 13433 13434static void 13435ctl_work_thread(void *arg) 13436{ 13437 struct ctl_thread *thr = (struct ctl_thread *)arg; 13438 struct ctl_softc *softc = thr->ctl_softc; 13439 union ctl_io *io; 13440 int retval; 13441 13442 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13443 13444 for (;;) { 13445 /* 13446 * We handle the queues in this order: 13447 * - ISC 13448 * - done queue (to free up resources, unblock other commands) 13449 * - RtR queue 13450 * - incoming queue 13451 * 13452 * If those queues are empty, we break out of the loop and 13453 * go to sleep. 13454 */ 13455 mtx_lock(&thr->queue_lock); 13456 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13457 if (io != NULL) { 13458 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13459 mtx_unlock(&thr->queue_lock); 13460 ctl_handle_isc(io); 13461 continue; 13462 } 13463 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13464 if (io != NULL) { 13465 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13466 /* clear any blocked commands, call fe_done */ 13467 mtx_unlock(&thr->queue_lock); 13468 ctl_process_done(io); 13469 continue; 13470 } 13471 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13472 if (io != NULL) { 13473 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13474 mtx_unlock(&thr->queue_lock); 13475 if (io->io_hdr.io_type == CTL_IO_TASK) 13476 ctl_run_task(io); 13477 else 13478 ctl_scsiio_precheck(softc, &io->scsiio); 13479 continue; 13480 } 13481 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13482 if (io != NULL) { 13483 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13484 mtx_unlock(&thr->queue_lock); 13485 retval = ctl_scsiio(&io->scsiio); 13486 if (retval != CTL_RETVAL_COMPLETE) 13487 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13488 continue; 13489 } 13490 13491 /* Sleep until we have something to do. */ 13492 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13493 } 13494} 13495 13496static void 13497ctl_lun_thread(void *arg) 13498{ 13499 struct ctl_softc *softc = (struct ctl_softc *)arg; 13500 struct ctl_be_lun *be_lun; 13501 13502 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13503 13504 for (;;) { 13505 mtx_lock(&softc->ctl_lock); 13506 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13507 if (be_lun != NULL) { 13508 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13509 mtx_unlock(&softc->ctl_lock); 13510 ctl_create_lun(be_lun); 13511 continue; 13512 } 13513 13514 /* Sleep until we have something to do. */ 13515 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13516 PDROP | PRIBIO, "-", 0); 13517 } 13518} 13519 13520static void 13521ctl_thresh_thread(void *arg) 13522{ 13523 struct ctl_softc *softc = (struct ctl_softc *)arg; 13524 struct ctl_lun *lun; 13525 struct scsi_da_rw_recovery_page *rwpage; 13526 struct ctl_logical_block_provisioning_page *page; 13527 const char *attr; 13528 union ctl_ha_msg msg; 13529 uint64_t thres, val; 13530 int i, e, set; 13531 13532 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13533 13534 for (;;) { 13535 mtx_lock(&softc->ctl_lock); 13536 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13537 if ((lun->flags & CTL_LUN_DISABLED) || 13538 (lun->flags & CTL_LUN_NO_MEDIA) || 13539 lun->backend->lun_attr == NULL) 13540 continue; 13541 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13542 softc->ha_mode == CTL_HA_MODE_XFER) 13543 continue; 13544 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13545 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13546 continue; 13547 e = 0; 13548 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13549 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13550 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13551 continue; 13552 thres = scsi_4btoul(page->descr[i].count); 13553 thres <<= CTL_LBP_EXPONENT; 13554 switch (page->descr[i].resource) { 13555 case 0x01: 13556 attr = "blocksavail"; 13557 break; 13558 case 0x02: 13559 attr = "blocksused"; 13560 break; 13561 case 0xf1: 13562 attr = "poolblocksavail"; 13563 break; 13564 case 0xf2: 13565 attr = "poolblocksused"; 13566 break; 13567 default: 13568 continue; 13569 } 13570 mtx_unlock(&softc->ctl_lock); // XXX 13571 val = lun->backend->lun_attr( 13572 lun->be_lun->be_lun, attr); 13573 mtx_lock(&softc->ctl_lock); 13574 if (val == UINT64_MAX) 13575 continue; 13576 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13577 == SLBPPD_ARMING_INC) 13578 e = (val >= thres); 13579 else 13580 e = (val <= thres); 13581 if (e) 13582 break; 13583 } 13584 mtx_lock(&lun->lun_lock); 13585 if (e) { 13586 scsi_u64to8b((uint8_t *)&page->descr[i] - 13587 (uint8_t *)page, lun->ua_tpt_info); 13588 if (lun->lasttpt == 0 || 13589 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13590 lun->lasttpt = time_uptime; 13591 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13592 set = 1; 13593 } else 13594 set = 0; 13595 } else { 13596 lun->lasttpt = 0; 13597 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13598 set = -1; 13599 } 13600 mtx_unlock(&lun->lun_lock); 13601 if (set != 0 && 13602 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13603 /* Send msg to other side. */ 13604 bzero(&msg.ua, sizeof(msg.ua)); 13605 msg.hdr.msg_type = CTL_MSG_UA; 13606 msg.hdr.nexus.initid = -1; 13607 msg.hdr.nexus.targ_port = -1; 13608 msg.hdr.nexus.targ_lun = lun->lun; 13609 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13610 msg.ua.ua_all = 1; 13611 msg.ua.ua_set = (set > 0); 13612 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13613 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13614 mtx_unlock(&softc->ctl_lock); // XXX 13615 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13616 sizeof(msg.ua), M_WAITOK); 13617 mtx_lock(&softc->ctl_lock); 13618 } 13619 } 13620 mtx_unlock(&softc->ctl_lock); 13621 pause("-", CTL_LBP_PERIOD * hz); 13622 } 13623} 13624 13625static void 13626ctl_enqueue_incoming(union ctl_io *io) 13627{ 13628 struct ctl_softc *softc = control_softc; 13629 struct ctl_thread *thr; 13630 u_int idx; 13631 13632 idx = (io->io_hdr.nexus.targ_port * 127 + 13633 io->io_hdr.nexus.initid) % worker_threads; 13634 thr = &softc->threads[idx]; 13635 mtx_lock(&thr->queue_lock); 13636 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13637 mtx_unlock(&thr->queue_lock); 13638 wakeup(thr); 13639} 13640 13641static void 13642ctl_enqueue_rtr(union ctl_io *io) 13643{ 13644 struct ctl_softc *softc = control_softc; 13645 struct ctl_thread *thr; 13646 13647 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13648 mtx_lock(&thr->queue_lock); 13649 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13650 mtx_unlock(&thr->queue_lock); 13651 wakeup(thr); 13652} 13653 13654static void 13655ctl_enqueue_done(union ctl_io *io) 13656{ 13657 struct ctl_softc *softc = control_softc; 13658 struct ctl_thread *thr; 13659 13660 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13661 mtx_lock(&thr->queue_lock); 13662 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13663 mtx_unlock(&thr->queue_lock); 13664 wakeup(thr); 13665} 13666 13667static void 13668ctl_enqueue_isc(union ctl_io *io) 13669{ 13670 struct ctl_softc *softc = control_softc; 13671 struct ctl_thread *thr; 13672 13673 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13674 mtx_lock(&thr->queue_lock); 13675 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13676 mtx_unlock(&thr->queue_lock); 13677 wakeup(thr); 13678} 13679 13680/* 13681 * vim: ts=8 13682 */ 13683