1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 5 * Copyright (c) 2012 The FreeBSD Foundation 6 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org> 7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org> 8 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org> 9 * All rights reserved. 10 * 11 * Portions of this software were developed by Edward Tomasz Napierala 12 * under sponsorship from the FreeBSD Foundation. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions, and the following disclaimer, 19 * without modification. 20 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 21 * substantially similar to the "NO WARRANTY" disclaimer below 22 * ("Disclaimer") and any redistribution must be conditioned upon 23 * including a substantially similar Disclaimer requirement for further 24 * binary redistribution. 25 * 26 * NO WARRANTY 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGES. 38 * 39 * $Id$ 40 */ 41 /* 42 * CAM Target Layer, a SCSI device emulation subsystem. 43 * 44 * Author: Ken Merry <ken@FreeBSD.org> 45 */ 46 47 #include <sys/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/ctype.h> 53 #include <sys/kernel.h> 54 #include <sys/types.h> 55 #include <sys/kthread.h> 56 #include <sys/bio.h> 57 #include <sys/fcntl.h> 58 #include <sys/lock.h> 59 #include <sys/module.h> 60 #include <sys/mutex.h> 61 #include <sys/condvar.h> 62 #include <sys/malloc.h> 63 #include <sys/conf.h> 64 #include <sys/ioccom.h> 65 #include <sys/queue.h> 66 #include <sys/sbuf.h> 67 #include <sys/smp.h> 68 #include <sys/endian.h> 69 #include <sys/sysctl.h> 70 #include <sys/nv.h> 71 #include <sys/dnv.h> 72 #include <vm/uma.h> 73 74 #include <cam/cam.h> 75 #include <cam/scsi/scsi_all.h> 76 #include <cam/scsi/scsi_cd.h> 77 #include <cam/scsi/scsi_da.h> 78 #include <cam/ctl/ctl_io.h> 79 #include <cam/ctl/ctl.h> 80 #include <cam/ctl/ctl_frontend.h> 81 #include <cam/ctl/ctl_util.h> 82 #include <cam/ctl/ctl_backend.h> 83 #include <cam/ctl/ctl_ioctl.h> 84 #include <cam/ctl/ctl_ha.h> 85 #include <cam/ctl/ctl_private.h> 86 #include <cam/ctl/ctl_debug.h> 87 #include <cam/ctl/ctl_scsi_all.h> 88 #include <cam/ctl/ctl_error.h> 89 90 struct ctl_softc *control_softc = NULL; 91 92 /* 93 * Template mode pages. 94 */ 95 96 /* 97 * Note that these are default values only. The actual values will be 98 * filled in when the user does a mode sense. 99 */ 100 const static struct scsi_da_rw_recovery_page rw_er_page_default = { 101 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 102 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 103 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 104 /*read_retry_count*/0, 105 /*correction_span*/0, 106 /*head_offset_count*/0, 107 /*data_strobe_offset_cnt*/0, 108 /*byte8*/SMS_RWER_LBPERE, 109 /*write_retry_count*/0, 110 /*reserved2*/0, 111 /*recovery_time_limit*/{0, 0}, 112 }; 113 114 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 117 /*byte3*/SMS_RWER_PER, 118 /*read_retry_count*/0, 119 /*correction_span*/0, 120 /*head_offset_count*/0, 121 /*data_strobe_offset_cnt*/0, 122 /*byte8*/SMS_RWER_LBPERE, 123 /*write_retry_count*/0, 124 /*reserved2*/0, 125 /*recovery_time_limit*/{0, 0}, 126 }; 127 128 const static struct scsi_format_page format_page_default = { 129 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 130 /*page_length*/sizeof(struct scsi_format_page) - 2, 131 /*tracks_per_zone*/ {0, 0}, 132 /*alt_sectors_per_zone*/ {0, 0}, 133 /*alt_tracks_per_zone*/ {0, 0}, 134 /*alt_tracks_per_lun*/ {0, 0}, 135 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 136 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 137 /*bytes_per_sector*/ {0, 0}, 138 /*interleave*/ {0, 0}, 139 /*track_skew*/ {0, 0}, 140 /*cylinder_skew*/ {0, 0}, 141 /*flags*/ SFP_HSEC, 142 /*reserved*/ {0, 0, 0} 143 }; 144 145 const static struct scsi_format_page format_page_changeable = { 146 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 147 /*page_length*/sizeof(struct scsi_format_page) - 2, 148 /*tracks_per_zone*/ {0, 0}, 149 /*alt_sectors_per_zone*/ {0, 0}, 150 /*alt_tracks_per_zone*/ {0, 0}, 151 /*alt_tracks_per_lun*/ {0, 0}, 152 /*sectors_per_track*/ {0, 0}, 153 /*bytes_per_sector*/ {0, 0}, 154 /*interleave*/ {0, 0}, 155 /*track_skew*/ {0, 0}, 156 /*cylinder_skew*/ {0, 0}, 157 /*flags*/ 0, 158 /*reserved*/ {0, 0, 0} 159 }; 160 161 const static struct scsi_rigid_disk_page rigid_disk_page_default = { 162 /*page_code*/SMS_RIGID_DISK_PAGE, 163 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 164 /*cylinders*/ {0, 0, 0}, 165 /*heads*/ CTL_DEFAULT_HEADS, 166 /*start_write_precomp*/ {0, 0, 0}, 167 /*start_reduced_current*/ {0, 0, 0}, 168 /*step_rate*/ {0, 0}, 169 /*landing_zone_cylinder*/ {0, 0, 0}, 170 /*rpl*/ SRDP_RPL_DISABLED, 171 /*rotational_offset*/ 0, 172 /*reserved1*/ 0, 173 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 174 CTL_DEFAULT_ROTATION_RATE & 0xff}, 175 /*reserved2*/ {0, 0} 176 }; 177 178 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 179 /*page_code*/SMS_RIGID_DISK_PAGE, 180 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 181 /*cylinders*/ {0, 0, 0}, 182 /*heads*/ 0, 183 /*start_write_precomp*/ {0, 0, 0}, 184 /*start_reduced_current*/ {0, 0, 0}, 185 /*step_rate*/ {0, 0}, 186 /*landing_zone_cylinder*/ {0, 0, 0}, 187 /*rpl*/ 0, 188 /*rotational_offset*/ 0, 189 /*reserved1*/ 0, 190 /*rotation_rate*/ {0, 0}, 191 /*reserved2*/ {0, 0} 192 }; 193 194 const static struct scsi_da_verify_recovery_page verify_er_page_default = { 195 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 196 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 197 /*byte3*/0, 198 /*read_retry_count*/0, 199 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 200 /*recovery_time_limit*/{0, 0}, 201 }; 202 203 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 204 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 205 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 206 /*byte3*/SMS_VER_PER, 207 /*read_retry_count*/0, 208 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 209 /*recovery_time_limit*/{0, 0}, 210 }; 211 212 const static struct scsi_caching_page caching_page_default = { 213 /*page_code*/SMS_CACHING_PAGE, 214 /*page_length*/sizeof(struct scsi_caching_page) - 2, 215 /*flags1*/ SCP_DISC | SCP_WCE, 216 /*ret_priority*/ 0, 217 /*disable_pf_transfer_len*/ {0xff, 0xff}, 218 /*min_prefetch*/ {0, 0}, 219 /*max_prefetch*/ {0xff, 0xff}, 220 /*max_pf_ceiling*/ {0xff, 0xff}, 221 /*flags2*/ 0, 222 /*cache_segments*/ 0, 223 /*cache_seg_size*/ {0, 0}, 224 /*reserved*/ 0, 225 /*non_cache_seg_size*/ {0, 0, 0} 226 }; 227 228 const static struct scsi_caching_page caching_page_changeable = { 229 /*page_code*/SMS_CACHING_PAGE, 230 /*page_length*/sizeof(struct scsi_caching_page) - 2, 231 /*flags1*/ SCP_WCE | SCP_RCD, 232 /*ret_priority*/ 0, 233 /*disable_pf_transfer_len*/ {0, 0}, 234 /*min_prefetch*/ {0, 0}, 235 /*max_prefetch*/ {0, 0}, 236 /*max_pf_ceiling*/ {0, 0}, 237 /*flags2*/ 0, 238 /*cache_segments*/ 0, 239 /*cache_seg_size*/ {0, 0}, 240 /*reserved*/ 0, 241 /*non_cache_seg_size*/ {0, 0, 0} 242 }; 243 244 const static struct scsi_control_page control_page_default = { 245 /*page_code*/SMS_CONTROL_MODE_PAGE, 246 /*page_length*/sizeof(struct scsi_control_page) - 2, 247 /*rlec*/0, 248 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 249 /*eca_and_aen*/0, 250 /*flags4*/SCP_TAS, 251 /*aen_holdoff_period*/{0, 0}, 252 /*busy_timeout_period*/{0, 0}, 253 /*extended_selftest_completion_time*/{0, 0} 254 }; 255 256 const static struct scsi_control_page control_page_changeable = { 257 /*page_code*/SMS_CONTROL_MODE_PAGE, 258 /*page_length*/sizeof(struct scsi_control_page) - 2, 259 /*rlec*/SCP_DSENSE, 260 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 261 /*eca_and_aen*/SCP_SWP, 262 /*flags4*/0, 263 /*aen_holdoff_period*/{0, 0}, 264 /*busy_timeout_period*/{0, 0}, 265 /*extended_selftest_completion_time*/{0, 0} 266 }; 267 268 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 269 270 const static struct scsi_control_ext_page control_ext_page_default = { 271 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 272 /*subpage_code*/0x01, 273 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 274 /*flags*/0, 275 /*prio*/0, 276 /*max_sense*/0 277 }; 278 279 const static struct scsi_control_ext_page control_ext_page_changeable = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 281 /*subpage_code*/0x01, 282 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 283 /*flags*/0, 284 /*prio*/0, 285 /*max_sense*/0xff 286 }; 287 288 const static struct scsi_info_exceptions_page ie_page_default = { 289 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 290 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 291 /*info_flags*/SIEP_FLAGS_EWASC, 292 /*mrie*/SIEP_MRIE_NO, 293 /*interval_timer*/{0, 0, 0, 0}, 294 /*report_count*/{0, 0, 0, 1} 295 }; 296 297 const static struct scsi_info_exceptions_page ie_page_changeable = { 298 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 299 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 300 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 301 SIEP_FLAGS_LOGERR, 302 /*mrie*/0x0f, 303 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 304 /*report_count*/{0xff, 0xff, 0xff, 0xff} 305 }; 306 307 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 308 309 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 310 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 311 /*subpage_code*/0x02, 312 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 313 /*flags*/0, 314 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 315 /*descr*/{}}, 316 {{/*flags*/0, 317 /*resource*/0x01, 318 /*reserved*/{0, 0}, 319 /*count*/{0, 0, 0, 0}}, 320 {/*flags*/0, 321 /*resource*/0x02, 322 /*reserved*/{0, 0}, 323 /*count*/{0, 0, 0, 0}}, 324 {/*flags*/0, 325 /*resource*/0xf1, 326 /*reserved*/{0, 0}, 327 /*count*/{0, 0, 0, 0}}, 328 {/*flags*/0, 329 /*resource*/0xf2, 330 /*reserved*/{0, 0}, 331 /*count*/{0, 0, 0, 0}} 332 } 333 }; 334 335 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 336 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 337 /*subpage_code*/0x02, 338 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 339 /*flags*/SLBPP_SITUA, 340 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 341 /*descr*/{}}, 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 {/*flags*/0, 355 /*resource*/0, 356 /*reserved*/{0, 0}, 357 /*count*/{0, 0, 0, 0}} 358 } 359 }; 360 361 const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 362 /*page_code*/SMS_CDDVD_CAPS_PAGE, 363 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 364 /*caps1*/0x3f, 365 /*caps2*/0x00, 366 /*caps3*/0xf0, 367 /*caps4*/0x00, 368 /*caps5*/0x29, 369 /*caps6*/0x00, 370 /*obsolete*/{0, 0}, 371 /*nvol_levels*/{0, 0}, 372 /*buffer_size*/{8, 0}, 373 /*obsolete2*/{0, 0}, 374 /*reserved*/0, 375 /*digital*/0, 376 /*obsolete3*/0, 377 /*copy_management*/0, 378 /*reserved2*/0, 379 /*rotation_control*/0, 380 /*cur_write_speed*/0, 381 /*num_speed_descr*/0, 382 }; 383 384 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 385 /*page_code*/SMS_CDDVD_CAPS_PAGE, 386 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 387 /*caps1*/0, 388 /*caps2*/0, 389 /*caps3*/0, 390 /*caps4*/0, 391 /*caps5*/0, 392 /*caps6*/0, 393 /*obsolete*/{0, 0}, 394 /*nvol_levels*/{0, 0}, 395 /*buffer_size*/{0, 0}, 396 /*obsolete2*/{0, 0}, 397 /*reserved*/0, 398 /*digital*/0, 399 /*obsolete3*/0, 400 /*copy_management*/0, 401 /*reserved2*/0, 402 /*rotation_control*/0, 403 /*cur_write_speed*/0, 404 /*num_speed_descr*/0, 405 }; 406 407 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 408 static int worker_threads = -1; 409 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 410 &worker_threads, 1, "Number of worker threads"); 411 static int ctl_debug = CTL_DEBUG_NONE; 412 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 413 &ctl_debug, 0, "Enabled debug flags"); 414 static int ctl_lun_map_size = 1024; 415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 416 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 417 #ifdef CTL_TIME_IO 418 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS; 419 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN, 420 &ctl_time_io_secs, 0, "Log requests taking more seconds"); 421 #endif 422 423 /* 424 * Maximum number of LUNs we support. MUST be a power of 2. 425 */ 426 #define CTL_DEFAULT_MAX_LUNS 1024 427 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 428 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns); 429 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN, 430 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs"); 431 432 /* 433 * Maximum number of ports registered at one time. 434 */ 435 #define CTL_DEFAULT_MAX_PORTS 256 436 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 437 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports); 438 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN, 439 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports"); 440 441 /* 442 * Maximum number of initiators we support. 443 */ 444 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports) 445 446 /* 447 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 448 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 449 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 450 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 451 */ 452 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 453 454 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 455 int param); 456 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 457 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 458 static int ctl_init(void); 459 static int ctl_shutdown(void); 460 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 461 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 462 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 463 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 464 struct ctl_ooa *ooa_hdr, 465 struct ctl_ooa_entry *kern_entries); 466 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 467 struct thread *td); 468 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 469 struct ctl_be_lun *be_lun); 470 static int ctl_free_lun(struct ctl_lun *lun); 471 static void ctl_create_lun(struct ctl_be_lun *be_lun); 472 473 static int ctl_do_mode_select(union ctl_io *io); 474 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 475 uint64_t res_key, uint64_t sa_res_key, 476 uint8_t type, uint32_t residx, 477 struct ctl_scsiio *ctsio, 478 struct scsi_per_res_out *cdb, 479 struct scsi_per_res_out_parms* param); 480 static void ctl_pro_preempt_other(struct ctl_lun *lun, 481 union ctl_ha_msg *msg); 482 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 483 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 484 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 485 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 486 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 487 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 488 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 489 int alloc_len); 490 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 491 int alloc_len); 492 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 493 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 494 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 495 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 496 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 497 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 498 bool seq); 499 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 500 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 501 union ctl_io *pending_io, union ctl_io *ooa_io); 502 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 503 union ctl_io *starting_io); 504 static int ctl_check_blocked(struct ctl_lun *lun); 505 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 506 const struct ctl_cmd_entry *entry, 507 struct ctl_scsiio *ctsio); 508 static void ctl_failover_lun(union ctl_io *io); 509 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 510 struct ctl_scsiio *ctsio); 511 static int ctl_scsiio(struct ctl_scsiio *ctsio); 512 513 static int ctl_target_reset(union ctl_io *io); 514 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, 515 ctl_ua_type ua_type); 516 static int ctl_lun_reset(union ctl_io *io); 517 static int ctl_abort_task(union ctl_io *io); 518 static int ctl_abort_task_set(union ctl_io *io); 519 static int ctl_query_task(union ctl_io *io, int task_set); 520 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 521 ctl_ua_type ua_type); 522 static int ctl_i_t_nexus_reset(union ctl_io *io); 523 static int ctl_query_async_event(union ctl_io *io); 524 static void ctl_run_task(union ctl_io *io); 525 #ifdef CTL_IO_DELAY 526 static void ctl_datamove_timer_wakeup(void *arg); 527 static void ctl_done_timer_wakeup(void *arg); 528 #endif /* CTL_IO_DELAY */ 529 530 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 531 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 532 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 533 static void ctl_datamove_remote_write(union ctl_io *io); 534 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 535 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 536 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 537 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 538 ctl_ha_dt_cb callback); 539 static void ctl_datamove_remote_read(union ctl_io *io); 540 static void ctl_datamove_remote(union ctl_io *io); 541 static void ctl_process_done(union ctl_io *io); 542 static void ctl_lun_thread(void *arg); 543 static void ctl_thresh_thread(void *arg); 544 static void ctl_work_thread(void *arg); 545 static void ctl_enqueue_incoming(union ctl_io *io); 546 static void ctl_enqueue_rtr(union ctl_io *io); 547 static void ctl_enqueue_done(union ctl_io *io); 548 static void ctl_enqueue_isc(union ctl_io *io); 549 static const struct ctl_cmd_entry * 550 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 551 static const struct ctl_cmd_entry * 552 ctl_validate_command(struct ctl_scsiio *ctsio); 553 static int ctl_cmd_applicable(uint8_t lun_type, 554 const struct ctl_cmd_entry *entry); 555 static int ctl_ha_init(void); 556 static int ctl_ha_shutdown(void); 557 558 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 559 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 560 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 561 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 562 563 /* 564 * Load the serialization table. This isn't very pretty, but is probably 565 * the easiest way to do it. 566 */ 567 #include "ctl_ser_table.c" 568 569 /* 570 * We only need to define open, close and ioctl routines for this driver. 571 */ 572 static struct cdevsw ctl_cdevsw = { 573 .d_version = D_VERSION, 574 .d_flags = 0, 575 .d_open = ctl_open, 576 .d_close = ctl_close, 577 .d_ioctl = ctl_ioctl, 578 .d_name = "ctl", 579 }; 580 581 582 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 583 584 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 585 586 static moduledata_t ctl_moduledata = { 587 "ctl", 588 ctl_module_event_handler, 589 NULL 590 }; 591 592 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 593 MODULE_VERSION(ctl, 1); 594 595 static struct ctl_frontend ha_frontend = 596 { 597 .name = "ha", 598 .init = ctl_ha_init, 599 .shutdown = ctl_ha_shutdown, 600 }; 601 602 static int 603 ctl_ha_init(void) 604 { 605 struct ctl_softc *softc = control_softc; 606 607 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 608 &softc->othersc_pool) != 0) 609 return (ENOMEM); 610 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 611 ctl_pool_free(softc->othersc_pool); 612 return (EIO); 613 } 614 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 615 != CTL_HA_STATUS_SUCCESS) { 616 ctl_ha_msg_destroy(softc); 617 ctl_pool_free(softc->othersc_pool); 618 return (EIO); 619 } 620 return (0); 621 }; 622 623 static int 624 ctl_ha_shutdown(void) 625 { 626 struct ctl_softc *softc = control_softc; 627 struct ctl_port *port; 628 629 ctl_ha_msg_shutdown(softc); 630 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 631 return (EIO); 632 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 633 return (EIO); 634 ctl_pool_free(softc->othersc_pool); 635 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 636 ctl_port_deregister(port); 637 free(port->port_name, M_CTL); 638 free(port, M_CTL); 639 } 640 return (0); 641 }; 642 643 static void 644 ctl_ha_datamove(union ctl_io *io) 645 { 646 struct ctl_lun *lun = CTL_LUN(io); 647 struct ctl_sg_entry *sgl; 648 union ctl_ha_msg msg; 649 uint32_t sg_entries_sent; 650 int do_sg_copy, i, j; 651 652 memset(&msg.dt, 0, sizeof(msg.dt)); 653 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 654 msg.hdr.original_sc = io->io_hdr.original_sc; 655 msg.hdr.serializing_sc = io; 656 msg.hdr.nexus = io->io_hdr.nexus; 657 msg.hdr.status = io->io_hdr.status; 658 msg.dt.flags = io->io_hdr.flags; 659 660 /* 661 * We convert everything into a S/G list here. We can't 662 * pass by reference, only by value between controllers. 663 * So we can't pass a pointer to the S/G list, only as many 664 * S/G entries as we can fit in here. If it's possible for 665 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 666 * then we need to break this up into multiple transfers. 667 */ 668 if (io->scsiio.kern_sg_entries == 0) { 669 msg.dt.kern_sg_entries = 1; 670 #if 0 671 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 672 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 673 } else { 674 /* XXX KDM use busdma here! */ 675 msg.dt.sg_list[0].addr = 676 (void *)vtophys(io->scsiio.kern_data_ptr); 677 } 678 #else 679 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 680 ("HA does not support BUS_ADDR")); 681 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 682 #endif 683 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 684 do_sg_copy = 0; 685 } else { 686 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 687 do_sg_copy = 1; 688 } 689 690 msg.dt.kern_data_len = io->scsiio.kern_data_len; 691 msg.dt.kern_total_len = io->scsiio.kern_total_len; 692 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 693 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 694 msg.dt.sg_sequence = 0; 695 696 /* 697 * Loop until we've sent all of the S/G entries. On the 698 * other end, we'll recompose these S/G entries into one 699 * contiguous list before processing. 700 */ 701 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 702 msg.dt.sg_sequence++) { 703 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 704 sizeof(msg.dt.sg_list[0])), 705 msg.dt.kern_sg_entries - sg_entries_sent); 706 if (do_sg_copy != 0) { 707 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 708 for (i = sg_entries_sent, j = 0; 709 i < msg.dt.cur_sg_entries; i++, j++) { 710 #if 0 711 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 712 msg.dt.sg_list[j].addr = sgl[i].addr; 713 } else { 714 /* XXX KDM use busdma here! */ 715 msg.dt.sg_list[j].addr = 716 (void *)vtophys(sgl[i].addr); 717 } 718 #else 719 KASSERT((io->io_hdr.flags & 720 CTL_FLAG_BUS_ADDR) == 0, 721 ("HA does not support BUS_ADDR")); 722 msg.dt.sg_list[j].addr = sgl[i].addr; 723 #endif 724 msg.dt.sg_list[j].len = sgl[i].len; 725 } 726 } 727 728 sg_entries_sent += msg.dt.cur_sg_entries; 729 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 730 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 731 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 732 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 733 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 734 io->io_hdr.port_status = 31341; 735 io->scsiio.be_move_done(io); 736 return; 737 } 738 msg.dt.sent_sg_entries = sg_entries_sent; 739 } 740 741 /* 742 * Officially handover the request from us to peer. 743 * If failover has just happened, then we must return error. 744 * If failover happen just after, then it is not our problem. 745 */ 746 if (lun) 747 mtx_lock(&lun->lun_lock); 748 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 749 if (lun) 750 mtx_unlock(&lun->lun_lock); 751 io->io_hdr.port_status = 31342; 752 io->scsiio.be_move_done(io); 753 return; 754 } 755 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 756 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 757 if (lun) 758 mtx_unlock(&lun->lun_lock); 759 } 760 761 static void 762 ctl_ha_done(union ctl_io *io) 763 { 764 union ctl_ha_msg msg; 765 766 if (io->io_hdr.io_type == CTL_IO_SCSI) { 767 memset(&msg, 0, sizeof(msg)); 768 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 769 msg.hdr.original_sc = io->io_hdr.original_sc; 770 msg.hdr.nexus = io->io_hdr.nexus; 771 msg.hdr.status = io->io_hdr.status; 772 msg.scsi.scsi_status = io->scsiio.scsi_status; 773 msg.scsi.tag_num = io->scsiio.tag_num; 774 msg.scsi.tag_type = io->scsiio.tag_type; 775 msg.scsi.sense_len = io->scsiio.sense_len; 776 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 777 io->scsiio.sense_len); 778 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 779 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 780 msg.scsi.sense_len, M_WAITOK); 781 } 782 ctl_free_io(io); 783 } 784 785 static void 786 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 787 union ctl_ha_msg *msg_info) 788 { 789 struct ctl_scsiio *ctsio; 790 791 if (msg_info->hdr.original_sc == NULL) { 792 printf("%s: original_sc == NULL!\n", __func__); 793 /* XXX KDM now what? */ 794 return; 795 } 796 797 ctsio = &msg_info->hdr.original_sc->scsiio; 798 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 799 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 800 ctsio->io_hdr.status = msg_info->hdr.status; 801 ctsio->scsi_status = msg_info->scsi.scsi_status; 802 ctsio->sense_len = msg_info->scsi.sense_len; 803 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 804 msg_info->scsi.sense_len); 805 ctl_enqueue_isc((union ctl_io *)ctsio); 806 } 807 808 static void 809 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 810 union ctl_ha_msg *msg_info) 811 { 812 struct ctl_scsiio *ctsio; 813 814 if (msg_info->hdr.serializing_sc == NULL) { 815 printf("%s: serializing_sc == NULL!\n", __func__); 816 /* XXX KDM now what? */ 817 return; 818 } 819 820 ctsio = &msg_info->hdr.serializing_sc->scsiio; 821 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 822 ctl_enqueue_isc((union ctl_io *)ctsio); 823 } 824 825 void 826 ctl_isc_announce_lun(struct ctl_lun *lun) 827 { 828 struct ctl_softc *softc = lun->ctl_softc; 829 union ctl_ha_msg *msg; 830 struct ctl_ha_msg_lun_pr_key pr_key; 831 int i, k; 832 833 if (softc->ha_link != CTL_HA_LINK_ONLINE) 834 return; 835 mtx_lock(&lun->lun_lock); 836 i = sizeof(msg->lun); 837 if (lun->lun_devid) 838 i += lun->lun_devid->len; 839 i += sizeof(pr_key) * lun->pr_key_count; 840 alloc: 841 mtx_unlock(&lun->lun_lock); 842 msg = malloc(i, M_CTL, M_WAITOK); 843 mtx_lock(&lun->lun_lock); 844 k = sizeof(msg->lun); 845 if (lun->lun_devid) 846 k += lun->lun_devid->len; 847 k += sizeof(pr_key) * lun->pr_key_count; 848 if (i < k) { 849 free(msg, M_CTL); 850 i = k; 851 goto alloc; 852 } 853 bzero(&msg->lun, sizeof(msg->lun)); 854 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 855 msg->hdr.nexus.targ_lun = lun->lun; 856 msg->hdr.nexus.targ_mapped_lun = lun->lun; 857 msg->lun.flags = lun->flags; 858 msg->lun.pr_generation = lun->pr_generation; 859 msg->lun.pr_res_idx = lun->pr_res_idx; 860 msg->lun.pr_res_type = lun->pr_res_type; 861 msg->lun.pr_key_count = lun->pr_key_count; 862 i = 0; 863 if (lun->lun_devid) { 864 msg->lun.lun_devid_len = lun->lun_devid->len; 865 memcpy(&msg->lun.data[i], lun->lun_devid->data, 866 msg->lun.lun_devid_len); 867 i += msg->lun.lun_devid_len; 868 } 869 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 870 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 871 continue; 872 pr_key.pr_iid = k; 873 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 874 i += sizeof(pr_key); 875 } 876 mtx_unlock(&lun->lun_lock); 877 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 878 M_WAITOK); 879 free(msg, M_CTL); 880 881 if (lun->flags & CTL_LUN_PRIMARY_SC) { 882 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 883 ctl_isc_announce_mode(lun, -1, 884 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 885 lun->mode_pages.index[i].subpage); 886 } 887 } 888 } 889 890 void 891 ctl_isc_announce_port(struct ctl_port *port) 892 { 893 struct ctl_softc *softc = port->ctl_softc; 894 union ctl_ha_msg *msg; 895 int i; 896 897 if (port->targ_port < softc->port_min || 898 port->targ_port >= softc->port_max || 899 softc->ha_link != CTL_HA_LINK_ONLINE) 900 return; 901 i = sizeof(msg->port) + strlen(port->port_name) + 1; 902 if (port->lun_map) 903 i += port->lun_map_size * sizeof(uint32_t); 904 if (port->port_devid) 905 i += port->port_devid->len; 906 if (port->target_devid) 907 i += port->target_devid->len; 908 if (port->init_devid) 909 i += port->init_devid->len; 910 msg = malloc(i, M_CTL, M_WAITOK); 911 bzero(&msg->port, sizeof(msg->port)); 912 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 913 msg->hdr.nexus.targ_port = port->targ_port; 914 msg->port.port_type = port->port_type; 915 msg->port.physical_port = port->physical_port; 916 msg->port.virtual_port = port->virtual_port; 917 msg->port.status = port->status; 918 i = 0; 919 msg->port.name_len = sprintf(&msg->port.data[i], 920 "%d:%s", softc->ha_id, port->port_name) + 1; 921 i += msg->port.name_len; 922 if (port->lun_map) { 923 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 924 memcpy(&msg->port.data[i], port->lun_map, 925 msg->port.lun_map_len); 926 i += msg->port.lun_map_len; 927 } 928 if (port->port_devid) { 929 msg->port.port_devid_len = port->port_devid->len; 930 memcpy(&msg->port.data[i], port->port_devid->data, 931 msg->port.port_devid_len); 932 i += msg->port.port_devid_len; 933 } 934 if (port->target_devid) { 935 msg->port.target_devid_len = port->target_devid->len; 936 memcpy(&msg->port.data[i], port->target_devid->data, 937 msg->port.target_devid_len); 938 i += msg->port.target_devid_len; 939 } 940 if (port->init_devid) { 941 msg->port.init_devid_len = port->init_devid->len; 942 memcpy(&msg->port.data[i], port->init_devid->data, 943 msg->port.init_devid_len); 944 i += msg->port.init_devid_len; 945 } 946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 947 M_WAITOK); 948 free(msg, M_CTL); 949 } 950 951 void 952 ctl_isc_announce_iid(struct ctl_port *port, int iid) 953 { 954 struct ctl_softc *softc = port->ctl_softc; 955 union ctl_ha_msg *msg; 956 int i, l; 957 958 if (port->targ_port < softc->port_min || 959 port->targ_port >= softc->port_max || 960 softc->ha_link != CTL_HA_LINK_ONLINE) 961 return; 962 mtx_lock(&softc->ctl_lock); 963 i = sizeof(msg->iid); 964 l = 0; 965 if (port->wwpn_iid[iid].name) 966 l = strlen(port->wwpn_iid[iid].name) + 1; 967 i += l; 968 msg = malloc(i, M_CTL, M_NOWAIT); 969 if (msg == NULL) { 970 mtx_unlock(&softc->ctl_lock); 971 return; 972 } 973 bzero(&msg->iid, sizeof(msg->iid)); 974 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 975 msg->hdr.nexus.targ_port = port->targ_port; 976 msg->hdr.nexus.initid = iid; 977 msg->iid.in_use = port->wwpn_iid[iid].in_use; 978 msg->iid.name_len = l; 979 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 980 if (port->wwpn_iid[iid].name) 981 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 982 mtx_unlock(&softc->ctl_lock); 983 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 984 free(msg, M_CTL); 985 } 986 987 void 988 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 989 uint8_t page, uint8_t subpage) 990 { 991 struct ctl_softc *softc = lun->ctl_softc; 992 union ctl_ha_msg msg; 993 u_int i; 994 995 if (softc->ha_link != CTL_HA_LINK_ONLINE) 996 return; 997 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 998 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 999 page && lun->mode_pages.index[i].subpage == subpage) 1000 break; 1001 } 1002 if (i == CTL_NUM_MODE_PAGES) 1003 return; 1004 1005 /* Don't try to replicate pages not present on this device. */ 1006 if (lun->mode_pages.index[i].page_data == NULL) 1007 return; 1008 1009 bzero(&msg.mode, sizeof(msg.mode)); 1010 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 1011 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 1012 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 1013 msg.hdr.nexus.targ_lun = lun->lun; 1014 msg.hdr.nexus.targ_mapped_lun = lun->lun; 1015 msg.mode.page_code = page; 1016 msg.mode.subpage = subpage; 1017 msg.mode.page_len = lun->mode_pages.index[i].page_len; 1018 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 1019 msg.mode.page_len); 1020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 1021 M_WAITOK); 1022 } 1023 1024 static void 1025 ctl_isc_ha_link_up(struct ctl_softc *softc) 1026 { 1027 struct ctl_port *port; 1028 struct ctl_lun *lun; 1029 union ctl_ha_msg msg; 1030 int i; 1031 1032 /* Announce this node parameters to peer for validation. */ 1033 msg.login.msg_type = CTL_MSG_LOGIN; 1034 msg.login.version = CTL_HA_VERSION; 1035 msg.login.ha_mode = softc->ha_mode; 1036 msg.login.ha_id = softc->ha_id; 1037 msg.login.max_luns = ctl_max_luns; 1038 msg.login.max_ports = ctl_max_ports; 1039 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1040 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1041 M_WAITOK); 1042 1043 STAILQ_FOREACH(port, &softc->port_list, links) { 1044 ctl_isc_announce_port(port); 1045 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1046 if (port->wwpn_iid[i].in_use) 1047 ctl_isc_announce_iid(port, i); 1048 } 1049 } 1050 STAILQ_FOREACH(lun, &softc->lun_list, links) 1051 ctl_isc_announce_lun(lun); 1052 } 1053 1054 static void 1055 ctl_isc_ha_link_down(struct ctl_softc *softc) 1056 { 1057 struct ctl_port *port; 1058 struct ctl_lun *lun; 1059 union ctl_io *io; 1060 int i; 1061 1062 mtx_lock(&softc->ctl_lock); 1063 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1064 mtx_lock(&lun->lun_lock); 1065 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1066 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1067 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1068 } 1069 mtx_unlock(&lun->lun_lock); 1070 1071 mtx_unlock(&softc->ctl_lock); 1072 io = ctl_alloc_io(softc->othersc_pool); 1073 mtx_lock(&softc->ctl_lock); 1074 ctl_zero_io(io); 1075 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1076 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1077 ctl_enqueue_isc(io); 1078 } 1079 1080 STAILQ_FOREACH(port, &softc->port_list, links) { 1081 if (port->targ_port >= softc->port_min && 1082 port->targ_port < softc->port_max) 1083 continue; 1084 port->status &= ~CTL_PORT_STATUS_ONLINE; 1085 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1086 port->wwpn_iid[i].in_use = 0; 1087 free(port->wwpn_iid[i].name, M_CTL); 1088 port->wwpn_iid[i].name = NULL; 1089 } 1090 } 1091 mtx_unlock(&softc->ctl_lock); 1092 } 1093 1094 static void 1095 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1096 { 1097 struct ctl_lun *lun; 1098 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1099 1100 mtx_lock(&softc->ctl_lock); 1101 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1102 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1103 mtx_unlock(&softc->ctl_lock); 1104 return; 1105 } 1106 mtx_lock(&lun->lun_lock); 1107 mtx_unlock(&softc->ctl_lock); 1108 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1109 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1110 if (msg->ua.ua_all) { 1111 if (msg->ua.ua_set) 1112 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1113 else 1114 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1115 } else { 1116 if (msg->ua.ua_set) 1117 ctl_est_ua(lun, iid, msg->ua.ua_type); 1118 else 1119 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1120 } 1121 mtx_unlock(&lun->lun_lock); 1122 } 1123 1124 static void 1125 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1126 { 1127 struct ctl_lun *lun; 1128 struct ctl_ha_msg_lun_pr_key pr_key; 1129 int i, k; 1130 ctl_lun_flags oflags; 1131 uint32_t targ_lun; 1132 1133 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1134 mtx_lock(&softc->ctl_lock); 1135 if (targ_lun >= ctl_max_luns || 1136 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1137 mtx_unlock(&softc->ctl_lock); 1138 return; 1139 } 1140 mtx_lock(&lun->lun_lock); 1141 mtx_unlock(&softc->ctl_lock); 1142 if (lun->flags & CTL_LUN_DISABLED) { 1143 mtx_unlock(&lun->lun_lock); 1144 return; 1145 } 1146 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1147 if (msg->lun.lun_devid_len != i || (i > 0 && 1148 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1149 mtx_unlock(&lun->lun_lock); 1150 printf("%s: Received conflicting HA LUN %d\n", 1151 __func__, targ_lun); 1152 return; 1153 } else { 1154 /* Record whether peer is primary. */ 1155 oflags = lun->flags; 1156 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1157 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1158 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1159 else 1160 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1161 if (oflags != lun->flags) 1162 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1163 1164 /* If peer is primary and we are not -- use data */ 1165 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1166 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1167 lun->pr_generation = msg->lun.pr_generation; 1168 lun->pr_res_idx = msg->lun.pr_res_idx; 1169 lun->pr_res_type = msg->lun.pr_res_type; 1170 lun->pr_key_count = msg->lun.pr_key_count; 1171 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1172 ctl_clr_prkey(lun, k); 1173 for (k = 0; k < msg->lun.pr_key_count; k++) { 1174 memcpy(&pr_key, &msg->lun.data[i], 1175 sizeof(pr_key)); 1176 ctl_alloc_prkey(lun, pr_key.pr_iid); 1177 ctl_set_prkey(lun, pr_key.pr_iid, 1178 pr_key.pr_key); 1179 i += sizeof(pr_key); 1180 } 1181 } 1182 1183 mtx_unlock(&lun->lun_lock); 1184 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1185 __func__, targ_lun, 1186 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1187 "primary" : "secondary")); 1188 1189 /* If we are primary but peer doesn't know -- notify */ 1190 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1191 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1192 ctl_isc_announce_lun(lun); 1193 } 1194 } 1195 1196 static void 1197 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1198 { 1199 struct ctl_port *port; 1200 struct ctl_lun *lun; 1201 int i, new; 1202 1203 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1204 if (port == NULL) { 1205 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1206 msg->hdr.nexus.targ_port)); 1207 new = 1; 1208 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1209 port->frontend = &ha_frontend; 1210 port->targ_port = msg->hdr.nexus.targ_port; 1211 port->fe_datamove = ctl_ha_datamove; 1212 port->fe_done = ctl_ha_done; 1213 } else if (port->frontend == &ha_frontend) { 1214 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1215 msg->hdr.nexus.targ_port)); 1216 new = 0; 1217 } else { 1218 printf("%s: Received conflicting HA port %d\n", 1219 __func__, msg->hdr.nexus.targ_port); 1220 return; 1221 } 1222 port->port_type = msg->port.port_type; 1223 port->physical_port = msg->port.physical_port; 1224 port->virtual_port = msg->port.virtual_port; 1225 port->status = msg->port.status; 1226 i = 0; 1227 free(port->port_name, M_CTL); 1228 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1229 M_CTL); 1230 i += msg->port.name_len; 1231 if (msg->port.lun_map_len != 0) { 1232 if (port->lun_map == NULL || 1233 port->lun_map_size * sizeof(uint32_t) < 1234 msg->port.lun_map_len) { 1235 port->lun_map_size = 0; 1236 free(port->lun_map, M_CTL); 1237 port->lun_map = malloc(msg->port.lun_map_len, 1238 M_CTL, M_WAITOK); 1239 } 1240 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1241 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1242 i += msg->port.lun_map_len; 1243 } else { 1244 port->lun_map_size = 0; 1245 free(port->lun_map, M_CTL); 1246 port->lun_map = NULL; 1247 } 1248 if (msg->port.port_devid_len != 0) { 1249 if (port->port_devid == NULL || 1250 port->port_devid->len < msg->port.port_devid_len) { 1251 free(port->port_devid, M_CTL); 1252 port->port_devid = malloc(sizeof(struct ctl_devid) + 1253 msg->port.port_devid_len, M_CTL, M_WAITOK); 1254 } 1255 memcpy(port->port_devid->data, &msg->port.data[i], 1256 msg->port.port_devid_len); 1257 port->port_devid->len = msg->port.port_devid_len; 1258 i += msg->port.port_devid_len; 1259 } else { 1260 free(port->port_devid, M_CTL); 1261 port->port_devid = NULL; 1262 } 1263 if (msg->port.target_devid_len != 0) { 1264 if (port->target_devid == NULL || 1265 port->target_devid->len < msg->port.target_devid_len) { 1266 free(port->target_devid, M_CTL); 1267 port->target_devid = malloc(sizeof(struct ctl_devid) + 1268 msg->port.target_devid_len, M_CTL, M_WAITOK); 1269 } 1270 memcpy(port->target_devid->data, &msg->port.data[i], 1271 msg->port.target_devid_len); 1272 port->target_devid->len = msg->port.target_devid_len; 1273 i += msg->port.target_devid_len; 1274 } else { 1275 free(port->target_devid, M_CTL); 1276 port->target_devid = NULL; 1277 } 1278 if (msg->port.init_devid_len != 0) { 1279 if (port->init_devid == NULL || 1280 port->init_devid->len < msg->port.init_devid_len) { 1281 free(port->init_devid, M_CTL); 1282 port->init_devid = malloc(sizeof(struct ctl_devid) + 1283 msg->port.init_devid_len, M_CTL, M_WAITOK); 1284 } 1285 memcpy(port->init_devid->data, &msg->port.data[i], 1286 msg->port.init_devid_len); 1287 port->init_devid->len = msg->port.init_devid_len; 1288 i += msg->port.init_devid_len; 1289 } else { 1290 free(port->init_devid, M_CTL); 1291 port->init_devid = NULL; 1292 } 1293 if (new) { 1294 if (ctl_port_register(port) != 0) { 1295 printf("%s: ctl_port_register() failed with error\n", 1296 __func__); 1297 } 1298 } 1299 mtx_lock(&softc->ctl_lock); 1300 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1301 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1302 continue; 1303 mtx_lock(&lun->lun_lock); 1304 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1305 mtx_unlock(&lun->lun_lock); 1306 } 1307 mtx_unlock(&softc->ctl_lock); 1308 } 1309 1310 static void 1311 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1312 { 1313 struct ctl_port *port; 1314 int iid; 1315 1316 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1317 if (port == NULL) { 1318 printf("%s: Received IID for unknown port %d\n", 1319 __func__, msg->hdr.nexus.targ_port); 1320 return; 1321 } 1322 iid = msg->hdr.nexus.initid; 1323 if (port->wwpn_iid[iid].in_use != 0 && 1324 msg->iid.in_use == 0) 1325 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1326 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1327 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1328 free(port->wwpn_iid[iid].name, M_CTL); 1329 if (msg->iid.name_len) { 1330 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1331 msg->iid.name_len, M_CTL); 1332 } else 1333 port->wwpn_iid[iid].name = NULL; 1334 } 1335 1336 static void 1337 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1338 { 1339 1340 if (msg->login.version != CTL_HA_VERSION) { 1341 printf("CTL HA peers have different versions %d != %d\n", 1342 msg->login.version, CTL_HA_VERSION); 1343 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1344 return; 1345 } 1346 if (msg->login.ha_mode != softc->ha_mode) { 1347 printf("CTL HA peers have different ha_mode %d != %d\n", 1348 msg->login.ha_mode, softc->ha_mode); 1349 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1350 return; 1351 } 1352 if (msg->login.ha_id == softc->ha_id) { 1353 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1354 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1355 return; 1356 } 1357 if (msg->login.max_luns != ctl_max_luns || 1358 msg->login.max_ports != ctl_max_ports || 1359 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1360 printf("CTL HA peers have different limits\n"); 1361 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1362 return; 1363 } 1364 } 1365 1366 static void 1367 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1368 { 1369 struct ctl_lun *lun; 1370 u_int i; 1371 uint32_t initidx, targ_lun; 1372 1373 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1374 mtx_lock(&softc->ctl_lock); 1375 if (targ_lun >= ctl_max_luns || 1376 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1377 mtx_unlock(&softc->ctl_lock); 1378 return; 1379 } 1380 mtx_lock(&lun->lun_lock); 1381 mtx_unlock(&softc->ctl_lock); 1382 if (lun->flags & CTL_LUN_DISABLED) { 1383 mtx_unlock(&lun->lun_lock); 1384 return; 1385 } 1386 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1387 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1388 msg->mode.page_code && 1389 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1390 break; 1391 } 1392 if (i == CTL_NUM_MODE_PAGES) { 1393 mtx_unlock(&lun->lun_lock); 1394 return; 1395 } 1396 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1397 lun->mode_pages.index[i].page_len); 1398 initidx = ctl_get_initindex(&msg->hdr.nexus); 1399 if (initidx != -1) 1400 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1401 mtx_unlock(&lun->lun_lock); 1402 } 1403 1404 /* 1405 * ISC (Inter Shelf Communication) event handler. Events from the HA 1406 * subsystem come in here. 1407 */ 1408 static void 1409 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1410 { 1411 struct ctl_softc *softc = control_softc; 1412 union ctl_io *io; 1413 struct ctl_prio *presio; 1414 ctl_ha_status isc_status; 1415 1416 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1417 if (event == CTL_HA_EVT_MSG_RECV) { 1418 union ctl_ha_msg *msg, msgbuf; 1419 1420 if (param > sizeof(msgbuf)) 1421 msg = malloc(param, M_CTL, M_WAITOK); 1422 else 1423 msg = &msgbuf; 1424 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1425 M_WAITOK); 1426 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1427 printf("%s: Error receiving message: %d\n", 1428 __func__, isc_status); 1429 if (msg != &msgbuf) 1430 free(msg, M_CTL); 1431 return; 1432 } 1433 1434 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1435 switch (msg->hdr.msg_type) { 1436 case CTL_MSG_SERIALIZE: 1437 io = ctl_alloc_io(softc->othersc_pool); 1438 ctl_zero_io(io); 1439 // populate ctsio from msg 1440 io->io_hdr.io_type = CTL_IO_SCSI; 1441 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1442 io->io_hdr.original_sc = msg->hdr.original_sc; 1443 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1444 CTL_FLAG_IO_ACTIVE; 1445 /* 1446 * If we're in serialization-only mode, we don't 1447 * want to go through full done processing. Thus 1448 * the COPY flag. 1449 * 1450 * XXX KDM add another flag that is more specific. 1451 */ 1452 if (softc->ha_mode != CTL_HA_MODE_XFER) 1453 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1454 io->io_hdr.nexus = msg->hdr.nexus; 1455 #if 0 1456 printf("port %u, iid %u, lun %u\n", 1457 io->io_hdr.nexus.targ_port, 1458 io->io_hdr.nexus.initid, 1459 io->io_hdr.nexus.targ_lun); 1460 #endif 1461 io->scsiio.tag_num = msg->scsi.tag_num; 1462 io->scsiio.tag_type = msg->scsi.tag_type; 1463 #ifdef CTL_TIME_IO 1464 io->io_hdr.start_time = time_uptime; 1465 getbinuptime(&io->io_hdr.start_bt); 1466 #endif /* CTL_TIME_IO */ 1467 io->scsiio.cdb_len = msg->scsi.cdb_len; 1468 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1469 CTL_MAX_CDBLEN); 1470 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1471 const struct ctl_cmd_entry *entry; 1472 1473 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1474 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1475 io->io_hdr.flags |= 1476 entry->flags & CTL_FLAG_DATA_MASK; 1477 } 1478 ctl_enqueue_isc(io); 1479 break; 1480 1481 /* Performed on the Originating SC, XFER mode only */ 1482 case CTL_MSG_DATAMOVE: { 1483 struct ctl_sg_entry *sgl; 1484 int i, j; 1485 1486 io = msg->hdr.original_sc; 1487 if (io == NULL) { 1488 printf("%s: original_sc == NULL!\n", __func__); 1489 /* XXX KDM do something here */ 1490 break; 1491 } 1492 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1493 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1494 /* 1495 * Keep track of this, we need to send it back over 1496 * when the datamove is complete. 1497 */ 1498 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1499 if (msg->hdr.status == CTL_SUCCESS) 1500 io->io_hdr.status = msg->hdr.status; 1501 1502 if (msg->dt.sg_sequence == 0) { 1503 #ifdef CTL_TIME_IO 1504 getbinuptime(&io->io_hdr.dma_start_bt); 1505 #endif 1506 i = msg->dt.kern_sg_entries + 1507 msg->dt.kern_data_len / 1508 CTL_HA_DATAMOVE_SEGMENT + 1; 1509 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1510 M_WAITOK | M_ZERO); 1511 io->io_hdr.remote_sglist = sgl; 1512 io->io_hdr.local_sglist = 1513 &sgl[msg->dt.kern_sg_entries]; 1514 1515 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1516 1517 io->scsiio.kern_sg_entries = 1518 msg->dt.kern_sg_entries; 1519 io->scsiio.rem_sg_entries = 1520 msg->dt.kern_sg_entries; 1521 io->scsiio.kern_data_len = 1522 msg->dt.kern_data_len; 1523 io->scsiio.kern_total_len = 1524 msg->dt.kern_total_len; 1525 io->scsiio.kern_data_resid = 1526 msg->dt.kern_data_resid; 1527 io->scsiio.kern_rel_offset = 1528 msg->dt.kern_rel_offset; 1529 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1530 io->io_hdr.flags |= msg->dt.flags & 1531 CTL_FLAG_BUS_ADDR; 1532 } else 1533 sgl = (struct ctl_sg_entry *) 1534 io->scsiio.kern_data_ptr; 1535 1536 for (i = msg->dt.sent_sg_entries, j = 0; 1537 i < (msg->dt.sent_sg_entries + 1538 msg->dt.cur_sg_entries); i++, j++) { 1539 sgl[i].addr = msg->dt.sg_list[j].addr; 1540 sgl[i].len = msg->dt.sg_list[j].len; 1541 1542 #if 0 1543 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1544 __func__, sgl[i].addr, sgl[i].len, j, i); 1545 #endif 1546 } 1547 1548 /* 1549 * If this is the last piece of the I/O, we've got 1550 * the full S/G list. Queue processing in the thread. 1551 * Otherwise wait for the next piece. 1552 */ 1553 if (msg->dt.sg_last != 0) 1554 ctl_enqueue_isc(io); 1555 break; 1556 } 1557 /* Performed on the Serializing (primary) SC, XFER mode only */ 1558 case CTL_MSG_DATAMOVE_DONE: { 1559 if (msg->hdr.serializing_sc == NULL) { 1560 printf("%s: serializing_sc == NULL!\n", 1561 __func__); 1562 /* XXX KDM now what? */ 1563 break; 1564 } 1565 /* 1566 * We grab the sense information here in case 1567 * there was a failure, so we can return status 1568 * back to the initiator. 1569 */ 1570 io = msg->hdr.serializing_sc; 1571 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1572 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1573 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1574 io->io_hdr.port_status = msg->scsi.port_status; 1575 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1576 if (msg->hdr.status != CTL_STATUS_NONE) { 1577 io->io_hdr.status = msg->hdr.status; 1578 io->scsiio.scsi_status = msg->scsi.scsi_status; 1579 io->scsiio.sense_len = msg->scsi.sense_len; 1580 memcpy(&io->scsiio.sense_data, 1581 &msg->scsi.sense_data, 1582 msg->scsi.sense_len); 1583 if (msg->hdr.status == CTL_SUCCESS) 1584 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1585 } 1586 ctl_enqueue_isc(io); 1587 break; 1588 } 1589 1590 /* Preformed on Originating SC, SER_ONLY mode */ 1591 case CTL_MSG_R2R: 1592 io = msg->hdr.original_sc; 1593 if (io == NULL) { 1594 printf("%s: original_sc == NULL!\n", 1595 __func__); 1596 break; 1597 } 1598 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1599 io->io_hdr.msg_type = CTL_MSG_R2R; 1600 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1601 ctl_enqueue_isc(io); 1602 break; 1603 1604 /* 1605 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1606 * mode. 1607 * Performed on the Originating (i.e. secondary) SC in XFER 1608 * mode 1609 */ 1610 case CTL_MSG_FINISH_IO: 1611 if (softc->ha_mode == CTL_HA_MODE_XFER) 1612 ctl_isc_handler_finish_xfer(softc, msg); 1613 else 1614 ctl_isc_handler_finish_ser_only(softc, msg); 1615 break; 1616 1617 /* Preformed on Originating SC */ 1618 case CTL_MSG_BAD_JUJU: 1619 io = msg->hdr.original_sc; 1620 if (io == NULL) { 1621 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1622 __func__); 1623 break; 1624 } 1625 ctl_copy_sense_data(msg, io); 1626 /* 1627 * IO should have already been cleaned up on other 1628 * SC so clear this flag so we won't send a message 1629 * back to finish the IO there. 1630 */ 1631 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1632 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1633 1634 /* io = msg->hdr.serializing_sc; */ 1635 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1636 ctl_enqueue_isc(io); 1637 break; 1638 1639 /* Handle resets sent from the other side */ 1640 case CTL_MSG_MANAGE_TASKS: { 1641 struct ctl_taskio *taskio; 1642 taskio = (struct ctl_taskio *)ctl_alloc_io( 1643 softc->othersc_pool); 1644 ctl_zero_io((union ctl_io *)taskio); 1645 taskio->io_hdr.io_type = CTL_IO_TASK; 1646 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1647 taskio->io_hdr.nexus = msg->hdr.nexus; 1648 taskio->task_action = msg->task.task_action; 1649 taskio->tag_num = msg->task.tag_num; 1650 taskio->tag_type = msg->task.tag_type; 1651 #ifdef CTL_TIME_IO 1652 taskio->io_hdr.start_time = time_uptime; 1653 getbinuptime(&taskio->io_hdr.start_bt); 1654 #endif /* CTL_TIME_IO */ 1655 ctl_run_task((union ctl_io *)taskio); 1656 break; 1657 } 1658 /* Persistent Reserve action which needs attention */ 1659 case CTL_MSG_PERS_ACTION: 1660 presio = (struct ctl_prio *)ctl_alloc_io( 1661 softc->othersc_pool); 1662 ctl_zero_io((union ctl_io *)presio); 1663 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1664 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1665 presio->io_hdr.nexus = msg->hdr.nexus; 1666 presio->pr_msg = msg->pr; 1667 ctl_enqueue_isc((union ctl_io *)presio); 1668 break; 1669 case CTL_MSG_UA: 1670 ctl_isc_ua(softc, msg, param); 1671 break; 1672 case CTL_MSG_PORT_SYNC: 1673 ctl_isc_port_sync(softc, msg, param); 1674 break; 1675 case CTL_MSG_LUN_SYNC: 1676 ctl_isc_lun_sync(softc, msg, param); 1677 break; 1678 case CTL_MSG_IID_SYNC: 1679 ctl_isc_iid_sync(softc, msg, param); 1680 break; 1681 case CTL_MSG_LOGIN: 1682 ctl_isc_login(softc, msg, param); 1683 break; 1684 case CTL_MSG_MODE_SYNC: 1685 ctl_isc_mode_sync(softc, msg, param); 1686 break; 1687 default: 1688 printf("Received HA message of unknown type %d\n", 1689 msg->hdr.msg_type); 1690 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1691 break; 1692 } 1693 if (msg != &msgbuf) 1694 free(msg, M_CTL); 1695 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1696 printf("CTL: HA link status changed from %d to %d\n", 1697 softc->ha_link, param); 1698 if (param == softc->ha_link) 1699 return; 1700 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1701 softc->ha_link = param; 1702 ctl_isc_ha_link_down(softc); 1703 } else { 1704 softc->ha_link = param; 1705 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1706 ctl_isc_ha_link_up(softc); 1707 } 1708 return; 1709 } else { 1710 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1711 return; 1712 } 1713 } 1714 1715 static void 1716 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1717 { 1718 1719 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1720 src->scsi.sense_len); 1721 dest->scsiio.scsi_status = src->scsi.scsi_status; 1722 dest->scsiio.sense_len = src->scsi.sense_len; 1723 dest->io_hdr.status = src->hdr.status; 1724 } 1725 1726 static void 1727 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1728 { 1729 1730 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1731 src->scsiio.sense_len); 1732 dest->scsi.scsi_status = src->scsiio.scsi_status; 1733 dest->scsi.sense_len = src->scsiio.sense_len; 1734 dest->hdr.status = src->io_hdr.status; 1735 } 1736 1737 void 1738 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1739 { 1740 struct ctl_softc *softc = lun->ctl_softc; 1741 ctl_ua_type *pu; 1742 1743 if (initidx < softc->init_min || initidx >= softc->init_max) 1744 return; 1745 mtx_assert(&lun->lun_lock, MA_OWNED); 1746 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1747 if (pu == NULL) 1748 return; 1749 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1750 } 1751 1752 void 1753 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1754 { 1755 int i; 1756 1757 mtx_assert(&lun->lun_lock, MA_OWNED); 1758 if (lun->pending_ua[port] == NULL) 1759 return; 1760 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1761 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1762 continue; 1763 lun->pending_ua[port][i] |= ua; 1764 } 1765 } 1766 1767 void 1768 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1769 { 1770 struct ctl_softc *softc = lun->ctl_softc; 1771 int i; 1772 1773 mtx_assert(&lun->lun_lock, MA_OWNED); 1774 for (i = softc->port_min; i < softc->port_max; i++) 1775 ctl_est_ua_port(lun, i, except, ua); 1776 } 1777 1778 void 1779 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1780 { 1781 struct ctl_softc *softc = lun->ctl_softc; 1782 ctl_ua_type *pu; 1783 1784 if (initidx < softc->init_min || initidx >= softc->init_max) 1785 return; 1786 mtx_assert(&lun->lun_lock, MA_OWNED); 1787 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1788 if (pu == NULL) 1789 return; 1790 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1791 } 1792 1793 void 1794 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1795 { 1796 struct ctl_softc *softc = lun->ctl_softc; 1797 int i, j; 1798 1799 mtx_assert(&lun->lun_lock, MA_OWNED); 1800 for (i = softc->port_min; i < softc->port_max; i++) { 1801 if (lun->pending_ua[i] == NULL) 1802 continue; 1803 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1804 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1805 continue; 1806 lun->pending_ua[i][j] &= ~ua; 1807 } 1808 } 1809 } 1810 1811 void 1812 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1813 ctl_ua_type ua_type) 1814 { 1815 struct ctl_lun *lun; 1816 1817 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1818 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1819 mtx_lock(&lun->lun_lock); 1820 ctl_clr_ua(lun, initidx, ua_type); 1821 mtx_unlock(&lun->lun_lock); 1822 } 1823 } 1824 1825 static int 1826 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1827 { 1828 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1829 struct ctl_lun *lun; 1830 struct ctl_lun_req ireq; 1831 int error, value; 1832 1833 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1834 error = sysctl_handle_int(oidp, &value, 0, req); 1835 if ((error != 0) || (req->newptr == NULL)) 1836 return (error); 1837 1838 mtx_lock(&softc->ctl_lock); 1839 if (value == 0) 1840 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1841 else 1842 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1843 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1844 mtx_unlock(&softc->ctl_lock); 1845 bzero(&ireq, sizeof(ireq)); 1846 ireq.reqtype = CTL_LUNREQ_MODIFY; 1847 ireq.reqdata.modify.lun_id = lun->lun; 1848 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1849 curthread); 1850 if (ireq.status != CTL_LUN_OK) { 1851 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1852 __func__, ireq.status, ireq.error_str); 1853 } 1854 mtx_lock(&softc->ctl_lock); 1855 } 1856 mtx_unlock(&softc->ctl_lock); 1857 return (0); 1858 } 1859 1860 static int 1861 ctl_init(void) 1862 { 1863 struct make_dev_args args; 1864 struct ctl_softc *softc; 1865 int i, error; 1866 1867 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1868 M_WAITOK | M_ZERO); 1869 1870 make_dev_args_init(&args); 1871 args.mda_devsw = &ctl_cdevsw; 1872 args.mda_uid = UID_ROOT; 1873 args.mda_gid = GID_OPERATOR; 1874 args.mda_mode = 0600; 1875 args.mda_si_drv1 = softc; 1876 args.mda_si_drv2 = NULL; 1877 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1878 if (error != 0) { 1879 free(softc, M_DEVBUF); 1880 control_softc = NULL; 1881 return (error); 1882 } 1883 1884 sysctl_ctx_init(&softc->sysctl_ctx); 1885 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1886 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1887 CTLFLAG_RD, 0, "CAM Target Layer"); 1888 1889 if (softc->sysctl_tree == NULL) { 1890 printf("%s: unable to allocate sysctl tree\n", __func__); 1891 destroy_dev(softc->dev); 1892 free(softc, M_DEVBUF); 1893 control_softc = NULL; 1894 return (ENOMEM); 1895 } 1896 1897 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1898 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1899 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1900 softc->flags = 0; 1901 1902 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1903 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1904 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1905 1906 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) { 1907 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n", 1908 ctl_max_luns, CTL_DEFAULT_MAX_LUNS); 1909 ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 1910 } 1911 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns, 1912 M_DEVBUF, M_WAITOK | M_ZERO); 1913 softc->ctl_lun_mask = malloc(sizeof(uint32_t) * 1914 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1915 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) { 1916 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n", 1917 ctl_max_ports, CTL_DEFAULT_MAX_PORTS); 1918 ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 1919 } 1920 softc->ctl_port_mask = malloc(sizeof(uint32_t) * 1921 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1922 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports, 1923 M_DEVBUF, M_WAITOK | M_ZERO); 1924 1925 1926 /* 1927 * In Copan's HA scheme, the "master" and "slave" roles are 1928 * figured out through the slot the controller is in. Although it 1929 * is an active/active system, someone has to be in charge. 1930 */ 1931 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1932 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1933 "HA head ID (0 - no HA)"); 1934 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1935 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1936 softc->is_single = 1; 1937 softc->port_cnt = ctl_max_ports; 1938 softc->port_min = 0; 1939 } else { 1940 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 1941 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1942 } 1943 softc->port_max = softc->port_min + softc->port_cnt; 1944 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1945 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1946 1947 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1948 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1949 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1950 1951 STAILQ_INIT(&softc->lun_list); 1952 STAILQ_INIT(&softc->pending_lun_queue); 1953 STAILQ_INIT(&softc->fe_list); 1954 STAILQ_INIT(&softc->port_list); 1955 STAILQ_INIT(&softc->be_list); 1956 ctl_tpc_init(softc); 1957 1958 if (worker_threads <= 0) 1959 worker_threads = max(1, mp_ncpus / 4); 1960 if (worker_threads > CTL_MAX_THREADS) 1961 worker_threads = CTL_MAX_THREADS; 1962 1963 for (i = 0; i < worker_threads; i++) { 1964 struct ctl_thread *thr = &softc->threads[i]; 1965 1966 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1967 thr->ctl_softc = softc; 1968 STAILQ_INIT(&thr->incoming_queue); 1969 STAILQ_INIT(&thr->rtr_queue); 1970 STAILQ_INIT(&thr->done_queue); 1971 STAILQ_INIT(&thr->isc_queue); 1972 1973 error = kproc_kthread_add(ctl_work_thread, thr, 1974 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1975 if (error != 0) { 1976 printf("error creating CTL work thread!\n"); 1977 return (error); 1978 } 1979 } 1980 error = kproc_kthread_add(ctl_lun_thread, softc, 1981 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun"); 1982 if (error != 0) { 1983 printf("error creating CTL lun thread!\n"); 1984 return (error); 1985 } 1986 error = kproc_kthread_add(ctl_thresh_thread, softc, 1987 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 1988 if (error != 0) { 1989 printf("error creating CTL threshold thread!\n"); 1990 return (error); 1991 } 1992 1993 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1994 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1995 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1996 1997 if (softc->is_single == 0) { 1998 if (ctl_frontend_register(&ha_frontend) != 0) 1999 softc->is_single = 1; 2000 } 2001 return (0); 2002 } 2003 2004 static int 2005 ctl_shutdown(void) 2006 { 2007 struct ctl_softc *softc = control_softc; 2008 int i; 2009 2010 if (softc->is_single == 0) 2011 ctl_frontend_deregister(&ha_frontend); 2012 2013 destroy_dev(softc->dev); 2014 2015 /* Shutdown CTL threads. */ 2016 softc->shutdown = 1; 2017 for (i = 0; i < worker_threads; i++) { 2018 struct ctl_thread *thr = &softc->threads[i]; 2019 while (thr->thread != NULL) { 2020 wakeup(thr); 2021 if (thr->thread != NULL) 2022 pause("CTL thr shutdown", 1); 2023 } 2024 mtx_destroy(&thr->queue_lock); 2025 } 2026 while (softc->lun_thread != NULL) { 2027 wakeup(&softc->pending_lun_queue); 2028 if (softc->lun_thread != NULL) 2029 pause("CTL thr shutdown", 1); 2030 } 2031 while (softc->thresh_thread != NULL) { 2032 wakeup(softc->thresh_thread); 2033 if (softc->thresh_thread != NULL) 2034 pause("CTL thr shutdown", 1); 2035 } 2036 2037 ctl_tpc_shutdown(softc); 2038 uma_zdestroy(softc->io_zone); 2039 mtx_destroy(&softc->ctl_lock); 2040 2041 free(softc->ctl_luns, M_DEVBUF); 2042 free(softc->ctl_lun_mask, M_DEVBUF); 2043 free(softc->ctl_port_mask, M_DEVBUF); 2044 free(softc->ctl_ports, M_DEVBUF); 2045 2046 sysctl_ctx_free(&softc->sysctl_ctx); 2047 2048 free(softc, M_DEVBUF); 2049 control_softc = NULL; 2050 return (0); 2051 } 2052 2053 static int 2054 ctl_module_event_handler(module_t mod, int what, void *arg) 2055 { 2056 2057 switch (what) { 2058 case MOD_LOAD: 2059 return (ctl_init()); 2060 case MOD_UNLOAD: 2061 return (ctl_shutdown()); 2062 default: 2063 return (EOPNOTSUPP); 2064 } 2065 } 2066 2067 /* 2068 * XXX KDM should we do some access checks here? Bump a reference count to 2069 * prevent a CTL module from being unloaded while someone has it open? 2070 */ 2071 static int 2072 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2073 { 2074 return (0); 2075 } 2076 2077 static int 2078 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2079 { 2080 return (0); 2081 } 2082 2083 /* 2084 * Remove an initiator by port number and initiator ID. 2085 * Returns 0 for success, -1 for failure. 2086 */ 2087 int 2088 ctl_remove_initiator(struct ctl_port *port, int iid) 2089 { 2090 struct ctl_softc *softc = port->ctl_softc; 2091 int last; 2092 2093 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2094 2095 if (iid > CTL_MAX_INIT_PER_PORT) { 2096 printf("%s: initiator ID %u > maximun %u!\n", 2097 __func__, iid, CTL_MAX_INIT_PER_PORT); 2098 return (-1); 2099 } 2100 2101 mtx_lock(&softc->ctl_lock); 2102 last = (--port->wwpn_iid[iid].in_use == 0); 2103 port->wwpn_iid[iid].last_use = time_uptime; 2104 mtx_unlock(&softc->ctl_lock); 2105 if (last) 2106 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2107 ctl_isc_announce_iid(port, iid); 2108 2109 return (0); 2110 } 2111 2112 /* 2113 * Add an initiator to the initiator map. 2114 * Returns iid for success, < 0 for failure. 2115 */ 2116 int 2117 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2118 { 2119 struct ctl_softc *softc = port->ctl_softc; 2120 time_t best_time; 2121 int i, best; 2122 2123 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2124 2125 if (iid >= CTL_MAX_INIT_PER_PORT) { 2126 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2127 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2128 free(name, M_CTL); 2129 return (-1); 2130 } 2131 2132 mtx_lock(&softc->ctl_lock); 2133 2134 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2135 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2136 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2137 iid = i; 2138 break; 2139 } 2140 if (name != NULL && port->wwpn_iid[i].name != NULL && 2141 strcmp(name, port->wwpn_iid[i].name) == 0) { 2142 iid = i; 2143 break; 2144 } 2145 } 2146 } 2147 2148 if (iid < 0) { 2149 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2150 if (port->wwpn_iid[i].in_use == 0 && 2151 port->wwpn_iid[i].wwpn == 0 && 2152 port->wwpn_iid[i].name == NULL) { 2153 iid = i; 2154 break; 2155 } 2156 } 2157 } 2158 2159 if (iid < 0) { 2160 best = -1; 2161 best_time = INT32_MAX; 2162 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2163 if (port->wwpn_iid[i].in_use == 0) { 2164 if (port->wwpn_iid[i].last_use < best_time) { 2165 best = i; 2166 best_time = port->wwpn_iid[i].last_use; 2167 } 2168 } 2169 } 2170 iid = best; 2171 } 2172 2173 if (iid < 0) { 2174 mtx_unlock(&softc->ctl_lock); 2175 free(name, M_CTL); 2176 return (-2); 2177 } 2178 2179 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2180 /* 2181 * This is not an error yet. 2182 */ 2183 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2184 #if 0 2185 printf("%s: port %d iid %u WWPN %#jx arrived" 2186 " again\n", __func__, port->targ_port, 2187 iid, (uintmax_t)wwpn); 2188 #endif 2189 goto take; 2190 } 2191 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2192 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2193 #if 0 2194 printf("%s: port %d iid %u name '%s' arrived" 2195 " again\n", __func__, port->targ_port, 2196 iid, name); 2197 #endif 2198 goto take; 2199 } 2200 2201 /* 2202 * This is an error, but what do we do about it? The 2203 * driver is telling us we have a new WWPN for this 2204 * initiator ID, so we pretty much need to use it. 2205 */ 2206 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2207 " but WWPN %#jx '%s' is still at that address\n", 2208 __func__, port->targ_port, iid, wwpn, name, 2209 (uintmax_t)port->wwpn_iid[iid].wwpn, 2210 port->wwpn_iid[iid].name); 2211 } 2212 take: 2213 free(port->wwpn_iid[iid].name, M_CTL); 2214 port->wwpn_iid[iid].name = name; 2215 port->wwpn_iid[iid].wwpn = wwpn; 2216 port->wwpn_iid[iid].in_use++; 2217 mtx_unlock(&softc->ctl_lock); 2218 ctl_isc_announce_iid(port, iid); 2219 2220 return (iid); 2221 } 2222 2223 static int 2224 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2225 { 2226 int len; 2227 2228 switch (port->port_type) { 2229 case CTL_PORT_FC: 2230 { 2231 struct scsi_transportid_fcp *id = 2232 (struct scsi_transportid_fcp *)buf; 2233 if (port->wwpn_iid[iid].wwpn == 0) 2234 return (0); 2235 memset(id, 0, sizeof(*id)); 2236 id->format_protocol = SCSI_PROTO_FC; 2237 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2238 return (sizeof(*id)); 2239 } 2240 case CTL_PORT_ISCSI: 2241 { 2242 struct scsi_transportid_iscsi_port *id = 2243 (struct scsi_transportid_iscsi_port *)buf; 2244 if (port->wwpn_iid[iid].name == NULL) 2245 return (0); 2246 memset(id, 0, 256); 2247 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2248 SCSI_PROTO_ISCSI; 2249 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2250 len = roundup2(min(len, 252), 4); 2251 scsi_ulto2b(len, id->additional_length); 2252 return (sizeof(*id) + len); 2253 } 2254 case CTL_PORT_SAS: 2255 { 2256 struct scsi_transportid_sas *id = 2257 (struct scsi_transportid_sas *)buf; 2258 if (port->wwpn_iid[iid].wwpn == 0) 2259 return (0); 2260 memset(id, 0, sizeof(*id)); 2261 id->format_protocol = SCSI_PROTO_SAS; 2262 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2263 return (sizeof(*id)); 2264 } 2265 default: 2266 { 2267 struct scsi_transportid_spi *id = 2268 (struct scsi_transportid_spi *)buf; 2269 memset(id, 0, sizeof(*id)); 2270 id->format_protocol = SCSI_PROTO_SPI; 2271 scsi_ulto2b(iid, id->scsi_addr); 2272 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2273 return (sizeof(*id)); 2274 } 2275 } 2276 } 2277 2278 /* 2279 * Serialize a command that went down the "wrong" side, and so was sent to 2280 * this controller for execution. The logic is a little different than the 2281 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2282 * sent back to the other side, but in the success case, we execute the 2283 * command on this side (XFER mode) or tell the other side to execute it 2284 * (SER_ONLY mode). 2285 */ 2286 static void 2287 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2288 { 2289 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2290 struct ctl_port *port = CTL_PORT(ctsio); 2291 union ctl_ha_msg msg_info; 2292 struct ctl_lun *lun; 2293 const struct ctl_cmd_entry *entry; 2294 uint32_t targ_lun; 2295 2296 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2297 2298 /* Make sure that we know about this port. */ 2299 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2300 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2301 /*retry_count*/ 1); 2302 goto badjuju; 2303 } 2304 2305 /* Make sure that we know about this LUN. */ 2306 mtx_lock(&softc->ctl_lock); 2307 if (targ_lun >= ctl_max_luns || 2308 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2309 mtx_unlock(&softc->ctl_lock); 2310 2311 /* 2312 * The other node would not send this request to us unless 2313 * received announce that we are primary node for this LUN. 2314 * If this LUN does not exist now, it is probably result of 2315 * a race, so respond to initiator in the most opaque way. 2316 */ 2317 ctl_set_busy(ctsio); 2318 goto badjuju; 2319 } 2320 mtx_lock(&lun->lun_lock); 2321 mtx_unlock(&softc->ctl_lock); 2322 2323 /* 2324 * If the LUN is invalid, pretend that it doesn't exist. 2325 * It will go away as soon as all pending I/Os completed. 2326 */ 2327 if (lun->flags & CTL_LUN_DISABLED) { 2328 mtx_unlock(&lun->lun_lock); 2329 ctl_set_busy(ctsio); 2330 goto badjuju; 2331 } 2332 2333 entry = ctl_get_cmd_entry(ctsio, NULL); 2334 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2335 mtx_unlock(&lun->lun_lock); 2336 goto badjuju; 2337 } 2338 2339 CTL_LUN(ctsio) = lun; 2340 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2341 2342 /* 2343 * Every I/O goes into the OOA queue for a 2344 * particular LUN, and stays there until completion. 2345 */ 2346 #ifdef CTL_TIME_IO 2347 if (TAILQ_EMPTY(&lun->ooa_queue)) 2348 lun->idle_time += getsbinuptime() - lun->last_busy; 2349 #endif 2350 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2351 2352 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2353 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2354 ooa_links))) { 2355 case CTL_ACTION_BLOCK: 2356 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2357 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2358 blocked_links); 2359 mtx_unlock(&lun->lun_lock); 2360 break; 2361 case CTL_ACTION_PASS: 2362 case CTL_ACTION_SKIP: 2363 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2364 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2365 ctl_enqueue_rtr((union ctl_io *)ctsio); 2366 mtx_unlock(&lun->lun_lock); 2367 } else { 2368 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2369 mtx_unlock(&lun->lun_lock); 2370 2371 /* send msg back to other side */ 2372 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2373 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2374 msg_info.hdr.msg_type = CTL_MSG_R2R; 2375 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2376 sizeof(msg_info.hdr), M_WAITOK); 2377 } 2378 break; 2379 case CTL_ACTION_OVERLAP: 2380 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2381 mtx_unlock(&lun->lun_lock); 2382 ctl_set_overlapped_cmd(ctsio); 2383 goto badjuju; 2384 case CTL_ACTION_OVERLAP_TAG: 2385 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2386 mtx_unlock(&lun->lun_lock); 2387 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2388 goto badjuju; 2389 case CTL_ACTION_ERROR: 2390 default: 2391 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2392 mtx_unlock(&lun->lun_lock); 2393 2394 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2395 /*retry_count*/ 0); 2396 badjuju: 2397 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2398 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2399 msg_info.hdr.serializing_sc = NULL; 2400 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2401 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2402 sizeof(msg_info.scsi), M_WAITOK); 2403 ctl_free_io((union ctl_io *)ctsio); 2404 break; 2405 } 2406 } 2407 2408 /* 2409 * Returns 0 for success, errno for failure. 2410 */ 2411 static void 2412 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2413 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2414 { 2415 union ctl_io *io; 2416 2417 mtx_lock(&lun->lun_lock); 2418 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2419 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2420 ooa_links)) { 2421 struct ctl_ooa_entry *entry; 2422 2423 /* 2424 * If we've got more than we can fit, just count the 2425 * remaining entries. 2426 */ 2427 if (*cur_fill_num >= ooa_hdr->alloc_num) 2428 continue; 2429 2430 entry = &kern_entries[*cur_fill_num]; 2431 2432 entry->tag_num = io->scsiio.tag_num; 2433 entry->lun_num = lun->lun; 2434 #ifdef CTL_TIME_IO 2435 entry->start_bt = io->io_hdr.start_bt; 2436 #endif 2437 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2438 entry->cdb_len = io->scsiio.cdb_len; 2439 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2440 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2441 2442 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2443 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2444 2445 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2446 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2447 2448 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2449 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2450 2451 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2452 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2453 } 2454 mtx_unlock(&lun->lun_lock); 2455 } 2456 2457 /* 2458 * Escape characters that are illegal or not recommended in XML. 2459 */ 2460 int 2461 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2462 { 2463 char *end = str + size; 2464 int retval; 2465 2466 retval = 0; 2467 2468 for (; *str && str < end; str++) { 2469 switch (*str) { 2470 case '&': 2471 retval = sbuf_printf(sb, "&"); 2472 break; 2473 case '>': 2474 retval = sbuf_printf(sb, ">"); 2475 break; 2476 case '<': 2477 retval = sbuf_printf(sb, "<"); 2478 break; 2479 default: 2480 retval = sbuf_putc(sb, *str); 2481 break; 2482 } 2483 2484 if (retval != 0) 2485 break; 2486 2487 } 2488 2489 return (retval); 2490 } 2491 2492 static void 2493 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2494 { 2495 struct scsi_vpd_id_descriptor *desc; 2496 int i; 2497 2498 if (id == NULL || id->len < 4) 2499 return; 2500 desc = (struct scsi_vpd_id_descriptor *)id->data; 2501 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2502 case SVPD_ID_TYPE_T10: 2503 sbuf_printf(sb, "t10."); 2504 break; 2505 case SVPD_ID_TYPE_EUI64: 2506 sbuf_printf(sb, "eui."); 2507 break; 2508 case SVPD_ID_TYPE_NAA: 2509 sbuf_printf(sb, "naa."); 2510 break; 2511 case SVPD_ID_TYPE_SCSI_NAME: 2512 break; 2513 } 2514 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2515 case SVPD_ID_CODESET_BINARY: 2516 for (i = 0; i < desc->length; i++) 2517 sbuf_printf(sb, "%02x", desc->identifier[i]); 2518 break; 2519 case SVPD_ID_CODESET_ASCII: 2520 sbuf_printf(sb, "%.*s", (int)desc->length, 2521 (char *)desc->identifier); 2522 break; 2523 case SVPD_ID_CODESET_UTF8: 2524 sbuf_printf(sb, "%s", (char *)desc->identifier); 2525 break; 2526 } 2527 } 2528 2529 static int 2530 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2531 struct thread *td) 2532 { 2533 struct ctl_softc *softc = dev->si_drv1; 2534 struct ctl_port *port; 2535 struct ctl_lun *lun; 2536 int retval; 2537 2538 retval = 0; 2539 2540 switch (cmd) { 2541 case CTL_IO: 2542 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2543 break; 2544 case CTL_ENABLE_PORT: 2545 case CTL_DISABLE_PORT: 2546 case CTL_SET_PORT_WWNS: { 2547 struct ctl_port *port; 2548 struct ctl_port_entry *entry; 2549 2550 entry = (struct ctl_port_entry *)addr; 2551 2552 mtx_lock(&softc->ctl_lock); 2553 STAILQ_FOREACH(port, &softc->port_list, links) { 2554 int action, done; 2555 2556 if (port->targ_port < softc->port_min || 2557 port->targ_port >= softc->port_max) 2558 continue; 2559 2560 action = 0; 2561 done = 0; 2562 if ((entry->port_type == CTL_PORT_NONE) 2563 && (entry->targ_port == port->targ_port)) { 2564 /* 2565 * If the user only wants to enable or 2566 * disable or set WWNs on a specific port, 2567 * do the operation and we're done. 2568 */ 2569 action = 1; 2570 done = 1; 2571 } else if (entry->port_type & port->port_type) { 2572 /* 2573 * Compare the user's type mask with the 2574 * particular frontend type to see if we 2575 * have a match. 2576 */ 2577 action = 1; 2578 done = 0; 2579 2580 /* 2581 * Make sure the user isn't trying to set 2582 * WWNs on multiple ports at the same time. 2583 */ 2584 if (cmd == CTL_SET_PORT_WWNS) { 2585 printf("%s: Can't set WWNs on " 2586 "multiple ports\n", __func__); 2587 retval = EINVAL; 2588 break; 2589 } 2590 } 2591 if (action == 0) 2592 continue; 2593 2594 /* 2595 * XXX KDM we have to drop the lock here, because 2596 * the online/offline operations can potentially 2597 * block. We need to reference count the frontends 2598 * so they can't go away, 2599 */ 2600 if (cmd == CTL_ENABLE_PORT) { 2601 mtx_unlock(&softc->ctl_lock); 2602 ctl_port_online(port); 2603 mtx_lock(&softc->ctl_lock); 2604 } else if (cmd == CTL_DISABLE_PORT) { 2605 mtx_unlock(&softc->ctl_lock); 2606 ctl_port_offline(port); 2607 mtx_lock(&softc->ctl_lock); 2608 } else if (cmd == CTL_SET_PORT_WWNS) { 2609 ctl_port_set_wwns(port, 2610 (entry->flags & CTL_PORT_WWNN_VALID) ? 2611 1 : 0, entry->wwnn, 2612 (entry->flags & CTL_PORT_WWPN_VALID) ? 2613 1 : 0, entry->wwpn); 2614 } 2615 if (done != 0) 2616 break; 2617 } 2618 mtx_unlock(&softc->ctl_lock); 2619 break; 2620 } 2621 case CTL_GET_OOA: { 2622 struct ctl_ooa *ooa_hdr; 2623 struct ctl_ooa_entry *entries; 2624 uint32_t cur_fill_num; 2625 2626 ooa_hdr = (struct ctl_ooa *)addr; 2627 2628 if ((ooa_hdr->alloc_len == 0) 2629 || (ooa_hdr->alloc_num == 0)) { 2630 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2631 "must be non-zero\n", __func__, 2632 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2633 retval = EINVAL; 2634 break; 2635 } 2636 2637 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2638 sizeof(struct ctl_ooa_entry))) { 2639 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2640 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2641 __func__, ooa_hdr->alloc_len, 2642 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2643 retval = EINVAL; 2644 break; 2645 } 2646 2647 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2648 if (entries == NULL) { 2649 printf("%s: could not allocate %d bytes for OOA " 2650 "dump\n", __func__, ooa_hdr->alloc_len); 2651 retval = ENOMEM; 2652 break; 2653 } 2654 2655 mtx_lock(&softc->ctl_lock); 2656 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2657 (ooa_hdr->lun_num >= ctl_max_luns || 2658 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2659 mtx_unlock(&softc->ctl_lock); 2660 free(entries, M_CTL); 2661 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2662 __func__, (uintmax_t)ooa_hdr->lun_num); 2663 retval = EINVAL; 2664 break; 2665 } 2666 2667 cur_fill_num = 0; 2668 2669 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2670 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2671 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2672 ooa_hdr, entries); 2673 } 2674 } else { 2675 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2676 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2677 entries); 2678 } 2679 mtx_unlock(&softc->ctl_lock); 2680 2681 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2682 ooa_hdr->fill_len = ooa_hdr->fill_num * 2683 sizeof(struct ctl_ooa_entry); 2684 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2685 if (retval != 0) { 2686 printf("%s: error copying out %d bytes for OOA dump\n", 2687 __func__, ooa_hdr->fill_len); 2688 } 2689 2690 getbinuptime(&ooa_hdr->cur_bt); 2691 2692 if (cur_fill_num > ooa_hdr->alloc_num) { 2693 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2694 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2695 } else { 2696 ooa_hdr->dropped_num = 0; 2697 ooa_hdr->status = CTL_OOA_OK; 2698 } 2699 2700 free(entries, M_CTL); 2701 break; 2702 } 2703 case CTL_DELAY_IO: { 2704 struct ctl_io_delay_info *delay_info; 2705 2706 delay_info = (struct ctl_io_delay_info *)addr; 2707 2708 #ifdef CTL_IO_DELAY 2709 mtx_lock(&softc->ctl_lock); 2710 if (delay_info->lun_id >= ctl_max_luns || 2711 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2712 mtx_unlock(&softc->ctl_lock); 2713 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2714 break; 2715 } 2716 mtx_lock(&lun->lun_lock); 2717 mtx_unlock(&softc->ctl_lock); 2718 delay_info->status = CTL_DELAY_STATUS_OK; 2719 switch (delay_info->delay_type) { 2720 case CTL_DELAY_TYPE_CONT: 2721 case CTL_DELAY_TYPE_ONESHOT: 2722 break; 2723 default: 2724 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2725 break; 2726 } 2727 switch (delay_info->delay_loc) { 2728 case CTL_DELAY_LOC_DATAMOVE: 2729 lun->delay_info.datamove_type = delay_info->delay_type; 2730 lun->delay_info.datamove_delay = delay_info->delay_secs; 2731 break; 2732 case CTL_DELAY_LOC_DONE: 2733 lun->delay_info.done_type = delay_info->delay_type; 2734 lun->delay_info.done_delay = delay_info->delay_secs; 2735 break; 2736 default: 2737 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2738 break; 2739 } 2740 mtx_unlock(&lun->lun_lock); 2741 #else 2742 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2743 #endif /* CTL_IO_DELAY */ 2744 break; 2745 } 2746 #ifdef CTL_LEGACY_STATS 2747 case CTL_GETSTATS: { 2748 struct ctl_stats *stats = (struct ctl_stats *)addr; 2749 int i; 2750 2751 /* 2752 * XXX KDM no locking here. If the LUN list changes, 2753 * things can blow up. 2754 */ 2755 i = 0; 2756 stats->status = CTL_SS_OK; 2757 stats->fill_len = 0; 2758 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2759 if (stats->fill_len + sizeof(lun->legacy_stats) > 2760 stats->alloc_len) { 2761 stats->status = CTL_SS_NEED_MORE_SPACE; 2762 break; 2763 } 2764 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++], 2765 sizeof(lun->legacy_stats)); 2766 if (retval != 0) 2767 break; 2768 stats->fill_len += sizeof(lun->legacy_stats); 2769 } 2770 stats->num_luns = softc->num_luns; 2771 stats->flags = CTL_STATS_FLAG_NONE; 2772 #ifdef CTL_TIME_IO 2773 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 2774 #endif 2775 getnanouptime(&stats->timestamp); 2776 break; 2777 } 2778 #endif /* CTL_LEGACY_STATS */ 2779 case CTL_ERROR_INJECT: { 2780 struct ctl_error_desc *err_desc, *new_err_desc; 2781 2782 err_desc = (struct ctl_error_desc *)addr; 2783 2784 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2785 M_WAITOK | M_ZERO); 2786 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2787 2788 mtx_lock(&softc->ctl_lock); 2789 if (err_desc->lun_id >= ctl_max_luns || 2790 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2791 mtx_unlock(&softc->ctl_lock); 2792 free(new_err_desc, M_CTL); 2793 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2794 __func__, (uintmax_t)err_desc->lun_id); 2795 retval = EINVAL; 2796 break; 2797 } 2798 mtx_lock(&lun->lun_lock); 2799 mtx_unlock(&softc->ctl_lock); 2800 2801 /* 2802 * We could do some checking here to verify the validity 2803 * of the request, but given the complexity of error 2804 * injection requests, the checking logic would be fairly 2805 * complex. 2806 * 2807 * For now, if the request is invalid, it just won't get 2808 * executed and might get deleted. 2809 */ 2810 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2811 2812 /* 2813 * XXX KDM check to make sure the serial number is unique, 2814 * in case we somehow manage to wrap. That shouldn't 2815 * happen for a very long time, but it's the right thing to 2816 * do. 2817 */ 2818 new_err_desc->serial = lun->error_serial; 2819 err_desc->serial = lun->error_serial; 2820 lun->error_serial++; 2821 2822 mtx_unlock(&lun->lun_lock); 2823 break; 2824 } 2825 case CTL_ERROR_INJECT_DELETE: { 2826 struct ctl_error_desc *delete_desc, *desc, *desc2; 2827 int delete_done; 2828 2829 delete_desc = (struct ctl_error_desc *)addr; 2830 delete_done = 0; 2831 2832 mtx_lock(&softc->ctl_lock); 2833 if (delete_desc->lun_id >= ctl_max_luns || 2834 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2835 mtx_unlock(&softc->ctl_lock); 2836 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2837 __func__, (uintmax_t)delete_desc->lun_id); 2838 retval = EINVAL; 2839 break; 2840 } 2841 mtx_lock(&lun->lun_lock); 2842 mtx_unlock(&softc->ctl_lock); 2843 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2844 if (desc->serial != delete_desc->serial) 2845 continue; 2846 2847 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2848 links); 2849 free(desc, M_CTL); 2850 delete_done = 1; 2851 } 2852 mtx_unlock(&lun->lun_lock); 2853 if (delete_done == 0) { 2854 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2855 "error serial %ju on LUN %u\n", __func__, 2856 delete_desc->serial, delete_desc->lun_id); 2857 retval = EINVAL; 2858 break; 2859 } 2860 break; 2861 } 2862 case CTL_DUMP_STRUCTS: { 2863 int j, k; 2864 struct ctl_port *port; 2865 struct ctl_frontend *fe; 2866 2867 mtx_lock(&softc->ctl_lock); 2868 printf("CTL Persistent Reservation information start:\n"); 2869 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2870 mtx_lock(&lun->lun_lock); 2871 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2872 mtx_unlock(&lun->lun_lock); 2873 continue; 2874 } 2875 2876 for (j = 0; j < ctl_max_ports; j++) { 2877 if (lun->pr_keys[j] == NULL) 2878 continue; 2879 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2880 if (lun->pr_keys[j][k] == 0) 2881 continue; 2882 printf(" LUN %ju port %d iid %d key " 2883 "%#jx\n", lun->lun, j, k, 2884 (uintmax_t)lun->pr_keys[j][k]); 2885 } 2886 } 2887 mtx_unlock(&lun->lun_lock); 2888 } 2889 printf("CTL Persistent Reservation information end\n"); 2890 printf("CTL Ports:\n"); 2891 STAILQ_FOREACH(port, &softc->port_list, links) { 2892 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2893 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2894 port->frontend->name, port->port_type, 2895 port->physical_port, port->virtual_port, 2896 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2897 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2898 if (port->wwpn_iid[j].in_use == 0 && 2899 port->wwpn_iid[j].wwpn == 0 && 2900 port->wwpn_iid[j].name == NULL) 2901 continue; 2902 2903 printf(" iid %u use %d WWPN %#jx '%s'\n", 2904 j, port->wwpn_iid[j].in_use, 2905 (uintmax_t)port->wwpn_iid[j].wwpn, 2906 port->wwpn_iid[j].name); 2907 } 2908 } 2909 printf("CTL Port information end\n"); 2910 mtx_unlock(&softc->ctl_lock); 2911 /* 2912 * XXX KDM calling this without a lock. We'd likely want 2913 * to drop the lock before calling the frontend's dump 2914 * routine anyway. 2915 */ 2916 printf("CTL Frontends:\n"); 2917 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2918 printf(" Frontend '%s'\n", fe->name); 2919 if (fe->fe_dump != NULL) 2920 fe->fe_dump(); 2921 } 2922 printf("CTL Frontend information end\n"); 2923 break; 2924 } 2925 case CTL_LUN_REQ: { 2926 struct ctl_lun_req *lun_req; 2927 struct ctl_backend_driver *backend; 2928 void *packed; 2929 nvlist_t *tmp_args_nvl; 2930 size_t packed_len; 2931 2932 lun_req = (struct ctl_lun_req *)addr; 2933 tmp_args_nvl = lun_req->args_nvl; 2934 2935 backend = ctl_backend_find(lun_req->backend); 2936 if (backend == NULL) { 2937 lun_req->status = CTL_LUN_ERROR; 2938 snprintf(lun_req->error_str, 2939 sizeof(lun_req->error_str), 2940 "Backend \"%s\" not found.", 2941 lun_req->backend); 2942 break; 2943 } 2944 2945 if (lun_req->args != NULL) { 2946 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2947 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2948 free(packed, M_CTL); 2949 lun_req->status = CTL_LUN_ERROR; 2950 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2951 "Cannot copyin args."); 2952 break; 2953 } 2954 lun_req->args_nvl = nvlist_unpack(packed, 2955 lun_req->args_len, 0); 2956 free(packed, M_CTL); 2957 2958 if (lun_req->args_nvl == NULL) { 2959 lun_req->status = CTL_LUN_ERROR; 2960 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2961 "Cannot unpack args nvlist."); 2962 break; 2963 } 2964 } else 2965 lun_req->args_nvl = nvlist_create(0); 2966 2967 retval = backend->ioctl(dev, cmd, addr, flag, td); 2968 nvlist_destroy(lun_req->args_nvl); 2969 lun_req->args_nvl = tmp_args_nvl; 2970 2971 if (lun_req->result_nvl != NULL) { 2972 if (lun_req->result != NULL) { 2973 packed = nvlist_pack(lun_req->result_nvl, 2974 &packed_len); 2975 if (packed == NULL) { 2976 lun_req->status = CTL_LUN_ERROR; 2977 snprintf(lun_req->error_str, 2978 sizeof(lun_req->error_str), 2979 "Cannot pack result nvlist."); 2980 break; 2981 } 2982 2983 if (packed_len > lun_req->result_len) { 2984 lun_req->status = CTL_LUN_ERROR; 2985 snprintf(lun_req->error_str, 2986 sizeof(lun_req->error_str), 2987 "Result nvlist too large."); 2988 free(packed, M_NVLIST); 2989 break; 2990 } 2991 2992 if (copyout(packed, lun_req->result, packed_len)) { 2993 lun_req->status = CTL_LUN_ERROR; 2994 snprintf(lun_req->error_str, 2995 sizeof(lun_req->error_str), 2996 "Cannot copyout() the result."); 2997 free(packed, M_NVLIST); 2998 break; 2999 } 3000 3001 lun_req->result_len = packed_len; 3002 free(packed, M_NVLIST); 3003 } 3004 3005 nvlist_destroy(lun_req->result_nvl); 3006 } 3007 break; 3008 } 3009 case CTL_LUN_LIST: { 3010 struct sbuf *sb; 3011 struct ctl_lun_list *list; 3012 const char *name, *value; 3013 void *cookie; 3014 int type; 3015 3016 list = (struct ctl_lun_list *)addr; 3017 3018 /* 3019 * Allocate a fixed length sbuf here, based on the length 3020 * of the user's buffer. We could allocate an auto-extending 3021 * buffer, and then tell the user how much larger our 3022 * amount of data is than his buffer, but that presents 3023 * some problems: 3024 * 3025 * 1. The sbuf(9) routines use a blocking malloc, and so 3026 * we can't hold a lock while calling them with an 3027 * auto-extending buffer. 3028 * 3029 * 2. There is not currently a LUN reference counting 3030 * mechanism, outside of outstanding transactions on 3031 * the LUN's OOA queue. So a LUN could go away on us 3032 * while we're getting the LUN number, backend-specific 3033 * information, etc. Thus, given the way things 3034 * currently work, we need to hold the CTL lock while 3035 * grabbing LUN information. 3036 * 3037 * So, from the user's standpoint, the best thing to do is 3038 * allocate what he thinks is a reasonable buffer length, 3039 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3040 * double the buffer length and try again. (And repeat 3041 * that until he succeeds.) 3042 */ 3043 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3044 if (sb == NULL) { 3045 list->status = CTL_LUN_LIST_ERROR; 3046 snprintf(list->error_str, sizeof(list->error_str), 3047 "Unable to allocate %d bytes for LUN list", 3048 list->alloc_len); 3049 break; 3050 } 3051 3052 sbuf_printf(sb, "<ctllunlist>\n"); 3053 3054 mtx_lock(&softc->ctl_lock); 3055 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3056 mtx_lock(&lun->lun_lock); 3057 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3058 (uintmax_t)lun->lun); 3059 3060 /* 3061 * Bail out as soon as we see that we've overfilled 3062 * the buffer. 3063 */ 3064 if (retval != 0) 3065 break; 3066 3067 retval = sbuf_printf(sb, "\t<backend_type>%s" 3068 "</backend_type>\n", 3069 (lun->backend == NULL) ? "none" : 3070 lun->backend->name); 3071 3072 if (retval != 0) 3073 break; 3074 3075 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3076 lun->be_lun->lun_type); 3077 3078 if (retval != 0) 3079 break; 3080 3081 if (lun->backend == NULL) { 3082 retval = sbuf_printf(sb, "</lun>\n"); 3083 if (retval != 0) 3084 break; 3085 continue; 3086 } 3087 3088 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3089 (lun->be_lun->maxlba > 0) ? 3090 lun->be_lun->maxlba + 1 : 0); 3091 3092 if (retval != 0) 3093 break; 3094 3095 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3096 lun->be_lun->blocksize); 3097 3098 if (retval != 0) 3099 break; 3100 3101 retval = sbuf_printf(sb, "\t<serial_number>"); 3102 3103 if (retval != 0) 3104 break; 3105 3106 retval = ctl_sbuf_printf_esc(sb, 3107 lun->be_lun->serial_num, 3108 sizeof(lun->be_lun->serial_num)); 3109 3110 if (retval != 0) 3111 break; 3112 3113 retval = sbuf_printf(sb, "</serial_number>\n"); 3114 3115 if (retval != 0) 3116 break; 3117 3118 retval = sbuf_printf(sb, "\t<device_id>"); 3119 3120 if (retval != 0) 3121 break; 3122 3123 retval = ctl_sbuf_printf_esc(sb, 3124 lun->be_lun->device_id, 3125 sizeof(lun->be_lun->device_id)); 3126 3127 if (retval != 0) 3128 break; 3129 3130 retval = sbuf_printf(sb, "</device_id>\n"); 3131 3132 if (retval != 0) 3133 break; 3134 3135 if (lun->backend->lun_info != NULL) { 3136 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3137 if (retval != 0) 3138 break; 3139 } 3140 3141 cookie = NULL; 3142 while ((name = nvlist_next(lun->be_lun->options, &type, 3143 &cookie)) != NULL) { 3144 sbuf_printf(sb, "\t<%s>", name); 3145 3146 if (type == NV_TYPE_STRING) { 3147 value = dnvlist_get_string( 3148 lun->be_lun->options, name, NULL); 3149 if (value != NULL) 3150 sbuf_printf(sb, "%s", value); 3151 } 3152 3153 sbuf_printf(sb, "</%s>\n", name); 3154 } 3155 3156 retval = sbuf_printf(sb, "</lun>\n"); 3157 3158 if (retval != 0) 3159 break; 3160 mtx_unlock(&lun->lun_lock); 3161 } 3162 if (lun != NULL) 3163 mtx_unlock(&lun->lun_lock); 3164 mtx_unlock(&softc->ctl_lock); 3165 3166 if ((retval != 0) 3167 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3168 retval = 0; 3169 sbuf_delete(sb); 3170 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3171 snprintf(list->error_str, sizeof(list->error_str), 3172 "Out of space, %d bytes is too small", 3173 list->alloc_len); 3174 break; 3175 } 3176 3177 sbuf_finish(sb); 3178 3179 retval = copyout(sbuf_data(sb), list->lun_xml, 3180 sbuf_len(sb) + 1); 3181 3182 list->fill_len = sbuf_len(sb) + 1; 3183 list->status = CTL_LUN_LIST_OK; 3184 sbuf_delete(sb); 3185 break; 3186 } 3187 case CTL_ISCSI: { 3188 struct ctl_iscsi *ci; 3189 struct ctl_frontend *fe; 3190 3191 ci = (struct ctl_iscsi *)addr; 3192 3193 fe = ctl_frontend_find("iscsi"); 3194 if (fe == NULL) { 3195 ci->status = CTL_ISCSI_ERROR; 3196 snprintf(ci->error_str, sizeof(ci->error_str), 3197 "Frontend \"iscsi\" not found."); 3198 break; 3199 } 3200 3201 retval = fe->ioctl(dev, cmd, addr, flag, td); 3202 break; 3203 } 3204 case CTL_PORT_REQ: { 3205 struct ctl_req *req; 3206 struct ctl_frontend *fe; 3207 void *packed; 3208 nvlist_t *tmp_args_nvl; 3209 size_t packed_len; 3210 3211 req = (struct ctl_req *)addr; 3212 tmp_args_nvl = req->args_nvl; 3213 3214 fe = ctl_frontend_find(req->driver); 3215 if (fe == NULL) { 3216 req->status = CTL_LUN_ERROR; 3217 snprintf(req->error_str, sizeof(req->error_str), 3218 "Frontend \"%s\" not found.", req->driver); 3219 break; 3220 } 3221 3222 if (req->args != NULL) { 3223 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3224 if (copyin(req->args, packed, req->args_len) != 0) { 3225 free(packed, M_CTL); 3226 req->status = CTL_LUN_ERROR; 3227 snprintf(req->error_str, sizeof(req->error_str), 3228 "Cannot copyin args."); 3229 break; 3230 } 3231 req->args_nvl = nvlist_unpack(packed, 3232 req->args_len, 0); 3233 free(packed, M_CTL); 3234 3235 if (req->args_nvl == NULL) { 3236 req->status = CTL_LUN_ERROR; 3237 snprintf(req->error_str, sizeof(req->error_str), 3238 "Cannot unpack args nvlist."); 3239 break; 3240 } 3241 } else 3242 req->args_nvl = nvlist_create(0); 3243 3244 if (fe->ioctl) 3245 retval = fe->ioctl(dev, cmd, addr, flag, td); 3246 else 3247 retval = ENODEV; 3248 3249 nvlist_destroy(req->args_nvl); 3250 req->args_nvl = tmp_args_nvl; 3251 3252 if (req->result_nvl != NULL) { 3253 if (req->result != NULL) { 3254 packed = nvlist_pack(req->result_nvl, 3255 &packed_len); 3256 if (packed == NULL) { 3257 req->status = CTL_LUN_ERROR; 3258 snprintf(req->error_str, 3259 sizeof(req->error_str), 3260 "Cannot pack result nvlist."); 3261 break; 3262 } 3263 3264 if (packed_len > req->result_len) { 3265 req->status = CTL_LUN_ERROR; 3266 snprintf(req->error_str, 3267 sizeof(req->error_str), 3268 "Result nvlist too large."); 3269 free(packed, M_NVLIST); 3270 break; 3271 } 3272 3273 if (copyout(packed, req->result, packed_len)) { 3274 req->status = CTL_LUN_ERROR; 3275 snprintf(req->error_str, 3276 sizeof(req->error_str), 3277 "Cannot copyout() the result."); 3278 free(packed, M_NVLIST); 3279 break; 3280 } 3281 3282 req->result_len = packed_len; 3283 free(packed, M_NVLIST); 3284 } 3285 3286 nvlist_destroy(req->result_nvl); 3287 } 3288 break; 3289 } 3290 case CTL_PORT_LIST: { 3291 struct sbuf *sb; 3292 struct ctl_port *port; 3293 struct ctl_lun_list *list; 3294 const char *name, *value; 3295 void *cookie; 3296 int j, type; 3297 uint32_t plun; 3298 3299 list = (struct ctl_lun_list *)addr; 3300 3301 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3302 if (sb == NULL) { 3303 list->status = CTL_LUN_LIST_ERROR; 3304 snprintf(list->error_str, sizeof(list->error_str), 3305 "Unable to allocate %d bytes for LUN list", 3306 list->alloc_len); 3307 break; 3308 } 3309 3310 sbuf_printf(sb, "<ctlportlist>\n"); 3311 3312 mtx_lock(&softc->ctl_lock); 3313 STAILQ_FOREACH(port, &softc->port_list, links) { 3314 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3315 (uintmax_t)port->targ_port); 3316 3317 /* 3318 * Bail out as soon as we see that we've overfilled 3319 * the buffer. 3320 */ 3321 if (retval != 0) 3322 break; 3323 3324 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3325 "</frontend_type>\n", port->frontend->name); 3326 if (retval != 0) 3327 break; 3328 3329 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3330 port->port_type); 3331 if (retval != 0) 3332 break; 3333 3334 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3335 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3336 if (retval != 0) 3337 break; 3338 3339 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3340 port->port_name); 3341 if (retval != 0) 3342 break; 3343 3344 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3345 port->physical_port); 3346 if (retval != 0) 3347 break; 3348 3349 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3350 port->virtual_port); 3351 if (retval != 0) 3352 break; 3353 3354 if (port->target_devid != NULL) { 3355 sbuf_printf(sb, "\t<target>"); 3356 ctl_id_sbuf(port->target_devid, sb); 3357 sbuf_printf(sb, "</target>\n"); 3358 } 3359 3360 if (port->port_devid != NULL) { 3361 sbuf_printf(sb, "\t<port>"); 3362 ctl_id_sbuf(port->port_devid, sb); 3363 sbuf_printf(sb, "</port>\n"); 3364 } 3365 3366 if (port->port_info != NULL) { 3367 retval = port->port_info(port->onoff_arg, sb); 3368 if (retval != 0) 3369 break; 3370 } 3371 3372 cookie = NULL; 3373 while ((name = nvlist_next(port->options, &type, 3374 &cookie)) != NULL) { 3375 sbuf_printf(sb, "\t<%s>", name); 3376 3377 if (type == NV_TYPE_STRING) { 3378 value = dnvlist_get_string(port->options, 3379 name, NULL); 3380 if (value != NULL) 3381 sbuf_printf(sb, "%s", value); 3382 } 3383 3384 sbuf_printf(sb, "</%s>\n", name); 3385 } 3386 3387 if (port->lun_map != NULL) { 3388 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3389 for (j = 0; j < port->lun_map_size; j++) { 3390 plun = ctl_lun_map_from_port(port, j); 3391 if (plun == UINT32_MAX) 3392 continue; 3393 sbuf_printf(sb, 3394 "\t<lun id=\"%u\">%u</lun>\n", 3395 j, plun); 3396 } 3397 } 3398 3399 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3400 if (port->wwpn_iid[j].in_use == 0 || 3401 (port->wwpn_iid[j].wwpn == 0 && 3402 port->wwpn_iid[j].name == NULL)) 3403 continue; 3404 3405 if (port->wwpn_iid[j].name != NULL) 3406 retval = sbuf_printf(sb, 3407 "\t<initiator id=\"%u\">%s</initiator>\n", 3408 j, port->wwpn_iid[j].name); 3409 else 3410 retval = sbuf_printf(sb, 3411 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3412 j, port->wwpn_iid[j].wwpn); 3413 if (retval != 0) 3414 break; 3415 } 3416 if (retval != 0) 3417 break; 3418 3419 retval = sbuf_printf(sb, "</targ_port>\n"); 3420 if (retval != 0) 3421 break; 3422 } 3423 mtx_unlock(&softc->ctl_lock); 3424 3425 if ((retval != 0) 3426 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3427 retval = 0; 3428 sbuf_delete(sb); 3429 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3430 snprintf(list->error_str, sizeof(list->error_str), 3431 "Out of space, %d bytes is too small", 3432 list->alloc_len); 3433 break; 3434 } 3435 3436 sbuf_finish(sb); 3437 3438 retval = copyout(sbuf_data(sb), list->lun_xml, 3439 sbuf_len(sb) + 1); 3440 3441 list->fill_len = sbuf_len(sb) + 1; 3442 list->status = CTL_LUN_LIST_OK; 3443 sbuf_delete(sb); 3444 break; 3445 } 3446 case CTL_LUN_MAP: { 3447 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3448 struct ctl_port *port; 3449 3450 mtx_lock(&softc->ctl_lock); 3451 if (lm->port < softc->port_min || 3452 lm->port >= softc->port_max || 3453 (port = softc->ctl_ports[lm->port]) == NULL) { 3454 mtx_unlock(&softc->ctl_lock); 3455 return (ENXIO); 3456 } 3457 if (port->status & CTL_PORT_STATUS_ONLINE) { 3458 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3459 if (ctl_lun_map_to_port(port, lun->lun) == 3460 UINT32_MAX) 3461 continue; 3462 mtx_lock(&lun->lun_lock); 3463 ctl_est_ua_port(lun, lm->port, -1, 3464 CTL_UA_LUN_CHANGE); 3465 mtx_unlock(&lun->lun_lock); 3466 } 3467 } 3468 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3469 if (lm->plun != UINT32_MAX) { 3470 if (lm->lun == UINT32_MAX) 3471 retval = ctl_lun_map_unset(port, lm->plun); 3472 else if (lm->lun < ctl_max_luns && 3473 softc->ctl_luns[lm->lun] != NULL) 3474 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3475 else 3476 return (ENXIO); 3477 } else { 3478 if (lm->lun == UINT32_MAX) 3479 retval = ctl_lun_map_deinit(port); 3480 else 3481 retval = ctl_lun_map_init(port); 3482 } 3483 if (port->status & CTL_PORT_STATUS_ONLINE) 3484 ctl_isc_announce_port(port); 3485 break; 3486 } 3487 case CTL_GET_LUN_STATS: { 3488 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3489 int i; 3490 3491 /* 3492 * XXX KDM no locking here. If the LUN list changes, 3493 * things can blow up. 3494 */ 3495 i = 0; 3496 stats->status = CTL_SS_OK; 3497 stats->fill_len = 0; 3498 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3499 if (lun->lun < stats->first_item) 3500 continue; 3501 if (stats->fill_len + sizeof(lun->stats) > 3502 stats->alloc_len) { 3503 stats->status = CTL_SS_NEED_MORE_SPACE; 3504 break; 3505 } 3506 retval = copyout(&lun->stats, &stats->stats[i++], 3507 sizeof(lun->stats)); 3508 if (retval != 0) 3509 break; 3510 stats->fill_len += sizeof(lun->stats); 3511 } 3512 stats->num_items = softc->num_luns; 3513 stats->flags = CTL_STATS_FLAG_NONE; 3514 #ifdef CTL_TIME_IO 3515 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3516 #endif 3517 getnanouptime(&stats->timestamp); 3518 break; 3519 } 3520 case CTL_GET_PORT_STATS: { 3521 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3522 int i; 3523 3524 /* 3525 * XXX KDM no locking here. If the LUN list changes, 3526 * things can blow up. 3527 */ 3528 i = 0; 3529 stats->status = CTL_SS_OK; 3530 stats->fill_len = 0; 3531 STAILQ_FOREACH(port, &softc->port_list, links) { 3532 if (port->targ_port < stats->first_item) 3533 continue; 3534 if (stats->fill_len + sizeof(port->stats) > 3535 stats->alloc_len) { 3536 stats->status = CTL_SS_NEED_MORE_SPACE; 3537 break; 3538 } 3539 retval = copyout(&port->stats, &stats->stats[i++], 3540 sizeof(port->stats)); 3541 if (retval != 0) 3542 break; 3543 stats->fill_len += sizeof(port->stats); 3544 } 3545 stats->num_items = softc->num_ports; 3546 stats->flags = CTL_STATS_FLAG_NONE; 3547 #ifdef CTL_TIME_IO 3548 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3549 #endif 3550 getnanouptime(&stats->timestamp); 3551 break; 3552 } 3553 default: { 3554 /* XXX KDM should we fix this? */ 3555 #if 0 3556 struct ctl_backend_driver *backend; 3557 unsigned int type; 3558 int found; 3559 3560 found = 0; 3561 3562 /* 3563 * We encode the backend type as the ioctl type for backend 3564 * ioctls. So parse it out here, and then search for a 3565 * backend of this type. 3566 */ 3567 type = _IOC_TYPE(cmd); 3568 3569 STAILQ_FOREACH(backend, &softc->be_list, links) { 3570 if (backend->type == type) { 3571 found = 1; 3572 break; 3573 } 3574 } 3575 if (found == 0) { 3576 printf("ctl: unknown ioctl command %#lx or backend " 3577 "%d\n", cmd, type); 3578 retval = EINVAL; 3579 break; 3580 } 3581 retval = backend->ioctl(dev, cmd, addr, flag, td); 3582 #endif 3583 retval = ENOTTY; 3584 break; 3585 } 3586 } 3587 return (retval); 3588 } 3589 3590 uint32_t 3591 ctl_get_initindex(struct ctl_nexus *nexus) 3592 { 3593 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3594 } 3595 3596 int 3597 ctl_lun_map_init(struct ctl_port *port) 3598 { 3599 struct ctl_softc *softc = port->ctl_softc; 3600 struct ctl_lun *lun; 3601 int size = ctl_lun_map_size; 3602 uint32_t i; 3603 3604 if (port->lun_map == NULL || port->lun_map_size < size) { 3605 port->lun_map_size = 0; 3606 free(port->lun_map, M_CTL); 3607 port->lun_map = malloc(size * sizeof(uint32_t), 3608 M_CTL, M_NOWAIT); 3609 } 3610 if (port->lun_map == NULL) 3611 return (ENOMEM); 3612 for (i = 0; i < size; i++) 3613 port->lun_map[i] = UINT32_MAX; 3614 port->lun_map_size = size; 3615 if (port->status & CTL_PORT_STATUS_ONLINE) { 3616 if (port->lun_disable != NULL) { 3617 STAILQ_FOREACH(lun, &softc->lun_list, links) 3618 port->lun_disable(port->targ_lun_arg, lun->lun); 3619 } 3620 ctl_isc_announce_port(port); 3621 } 3622 return (0); 3623 } 3624 3625 int 3626 ctl_lun_map_deinit(struct ctl_port *port) 3627 { 3628 struct ctl_softc *softc = port->ctl_softc; 3629 struct ctl_lun *lun; 3630 3631 if (port->lun_map == NULL) 3632 return (0); 3633 port->lun_map_size = 0; 3634 free(port->lun_map, M_CTL); 3635 port->lun_map = NULL; 3636 if (port->status & CTL_PORT_STATUS_ONLINE) { 3637 if (port->lun_enable != NULL) { 3638 STAILQ_FOREACH(lun, &softc->lun_list, links) 3639 port->lun_enable(port->targ_lun_arg, lun->lun); 3640 } 3641 ctl_isc_announce_port(port); 3642 } 3643 return (0); 3644 } 3645 3646 int 3647 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3648 { 3649 int status; 3650 uint32_t old; 3651 3652 if (port->lun_map == NULL) { 3653 status = ctl_lun_map_init(port); 3654 if (status != 0) 3655 return (status); 3656 } 3657 if (plun >= port->lun_map_size) 3658 return (EINVAL); 3659 old = port->lun_map[plun]; 3660 port->lun_map[plun] = glun; 3661 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3662 if (port->lun_enable != NULL) 3663 port->lun_enable(port->targ_lun_arg, plun); 3664 ctl_isc_announce_port(port); 3665 } 3666 return (0); 3667 } 3668 3669 int 3670 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3671 { 3672 uint32_t old; 3673 3674 if (port->lun_map == NULL || plun >= port->lun_map_size) 3675 return (0); 3676 old = port->lun_map[plun]; 3677 port->lun_map[plun] = UINT32_MAX; 3678 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3679 if (port->lun_disable != NULL) 3680 port->lun_disable(port->targ_lun_arg, plun); 3681 ctl_isc_announce_port(port); 3682 } 3683 return (0); 3684 } 3685 3686 uint32_t 3687 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3688 { 3689 3690 if (port == NULL) 3691 return (UINT32_MAX); 3692 if (port->lun_map == NULL) 3693 return (lun_id); 3694 if (lun_id > port->lun_map_size) 3695 return (UINT32_MAX); 3696 return (port->lun_map[lun_id]); 3697 } 3698 3699 uint32_t 3700 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3701 { 3702 uint32_t i; 3703 3704 if (port == NULL) 3705 return (UINT32_MAX); 3706 if (port->lun_map == NULL) 3707 return (lun_id); 3708 for (i = 0; i < port->lun_map_size; i++) { 3709 if (port->lun_map[i] == lun_id) 3710 return (i); 3711 } 3712 return (UINT32_MAX); 3713 } 3714 3715 uint32_t 3716 ctl_decode_lun(uint64_t encoded) 3717 { 3718 uint8_t lun[8]; 3719 uint32_t result = 0xffffffff; 3720 3721 be64enc(lun, encoded); 3722 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3723 case RPL_LUNDATA_ATYP_PERIPH: 3724 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3725 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3726 result = lun[1]; 3727 break; 3728 case RPL_LUNDATA_ATYP_FLAT: 3729 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3730 lun[6] == 0 && lun[7] == 0) 3731 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3732 break; 3733 case RPL_LUNDATA_ATYP_EXTLUN: 3734 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3735 case 0x02: 3736 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3737 case 0x00: 3738 result = lun[1]; 3739 break; 3740 case 0x10: 3741 result = (lun[1] << 16) + (lun[2] << 8) + 3742 lun[3]; 3743 break; 3744 case 0x20: 3745 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3746 result = (lun[2] << 24) + 3747 (lun[3] << 16) + (lun[4] << 8) + 3748 lun[5]; 3749 break; 3750 } 3751 break; 3752 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3753 result = 0xffffffff; 3754 break; 3755 } 3756 break; 3757 } 3758 return (result); 3759 } 3760 3761 uint64_t 3762 ctl_encode_lun(uint32_t decoded) 3763 { 3764 uint64_t l = decoded; 3765 3766 if (l <= 0xff) 3767 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3768 if (l <= 0x3fff) 3769 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3770 if (l <= 0xffffff) 3771 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3772 (l << 32)); 3773 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3774 } 3775 3776 int 3777 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3778 { 3779 int i; 3780 3781 for (i = first; i < last; i++) { 3782 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3783 return (i); 3784 } 3785 return (-1); 3786 } 3787 3788 int 3789 ctl_set_mask(uint32_t *mask, uint32_t bit) 3790 { 3791 uint32_t chunk, piece; 3792 3793 chunk = bit >> 5; 3794 piece = bit % (sizeof(uint32_t) * 8); 3795 3796 if ((mask[chunk] & (1 << piece)) != 0) 3797 return (-1); 3798 else 3799 mask[chunk] |= (1 << piece); 3800 3801 return (0); 3802 } 3803 3804 int 3805 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3806 { 3807 uint32_t chunk, piece; 3808 3809 chunk = bit >> 5; 3810 piece = bit % (sizeof(uint32_t) * 8); 3811 3812 if ((mask[chunk] & (1 << piece)) == 0) 3813 return (-1); 3814 else 3815 mask[chunk] &= ~(1 << piece); 3816 3817 return (0); 3818 } 3819 3820 int 3821 ctl_is_set(uint32_t *mask, uint32_t bit) 3822 { 3823 uint32_t chunk, piece; 3824 3825 chunk = bit >> 5; 3826 piece = bit % (sizeof(uint32_t) * 8); 3827 3828 if ((mask[chunk] & (1 << piece)) == 0) 3829 return (0); 3830 else 3831 return (1); 3832 } 3833 3834 static uint64_t 3835 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3836 { 3837 uint64_t *t; 3838 3839 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3840 if (t == NULL) 3841 return (0); 3842 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3843 } 3844 3845 static void 3846 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3847 { 3848 uint64_t *t; 3849 3850 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3851 if (t == NULL) 3852 return; 3853 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3854 } 3855 3856 static void 3857 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3858 { 3859 uint64_t *p; 3860 u_int i; 3861 3862 i = residx/CTL_MAX_INIT_PER_PORT; 3863 if (lun->pr_keys[i] != NULL) 3864 return; 3865 mtx_unlock(&lun->lun_lock); 3866 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3867 M_WAITOK | M_ZERO); 3868 mtx_lock(&lun->lun_lock); 3869 if (lun->pr_keys[i] == NULL) 3870 lun->pr_keys[i] = p; 3871 else 3872 free(p, M_CTL); 3873 } 3874 3875 static void 3876 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3877 { 3878 uint64_t *t; 3879 3880 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3881 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3882 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3883 } 3884 3885 /* 3886 * ctl_softc, pool_name, total_ctl_io are passed in. 3887 * npool is passed out. 3888 */ 3889 int 3890 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3891 uint32_t total_ctl_io, void **npool) 3892 { 3893 struct ctl_io_pool *pool; 3894 3895 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3896 M_NOWAIT | M_ZERO); 3897 if (pool == NULL) 3898 return (ENOMEM); 3899 3900 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3901 pool->ctl_softc = ctl_softc; 3902 #ifdef IO_POOLS 3903 pool->zone = uma_zsecond_create(pool->name, NULL, 3904 NULL, NULL, NULL, ctl_softc->io_zone); 3905 /* uma_prealloc(pool->zone, total_ctl_io); */ 3906 #else 3907 pool->zone = ctl_softc->io_zone; 3908 #endif 3909 3910 *npool = pool; 3911 return (0); 3912 } 3913 3914 void 3915 ctl_pool_free(struct ctl_io_pool *pool) 3916 { 3917 3918 if (pool == NULL) 3919 return; 3920 3921 #ifdef IO_POOLS 3922 uma_zdestroy(pool->zone); 3923 #endif 3924 free(pool, M_CTL); 3925 } 3926 3927 union ctl_io * 3928 ctl_alloc_io(void *pool_ref) 3929 { 3930 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3931 union ctl_io *io; 3932 3933 io = uma_zalloc(pool->zone, M_WAITOK); 3934 if (io != NULL) { 3935 io->io_hdr.pool = pool_ref; 3936 CTL_SOFTC(io) = pool->ctl_softc; 3937 } 3938 return (io); 3939 } 3940 3941 union ctl_io * 3942 ctl_alloc_io_nowait(void *pool_ref) 3943 { 3944 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3945 union ctl_io *io; 3946 3947 io = uma_zalloc(pool->zone, M_NOWAIT); 3948 if (io != NULL) { 3949 io->io_hdr.pool = pool_ref; 3950 CTL_SOFTC(io) = pool->ctl_softc; 3951 } 3952 return (io); 3953 } 3954 3955 void 3956 ctl_free_io(union ctl_io *io) 3957 { 3958 struct ctl_io_pool *pool; 3959 3960 if (io == NULL) 3961 return; 3962 3963 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3964 uma_zfree(pool->zone, io); 3965 } 3966 3967 void 3968 ctl_zero_io(union ctl_io *io) 3969 { 3970 struct ctl_io_pool *pool; 3971 3972 if (io == NULL) 3973 return; 3974 3975 /* 3976 * May need to preserve linked list pointers at some point too. 3977 */ 3978 pool = io->io_hdr.pool; 3979 memset(io, 0, sizeof(*io)); 3980 io->io_hdr.pool = pool; 3981 CTL_SOFTC(io) = pool->ctl_softc; 3982 } 3983 3984 int 3985 ctl_expand_number(const char *buf, uint64_t *num) 3986 { 3987 char *endptr; 3988 uint64_t number; 3989 unsigned shift; 3990 3991 number = strtoq(buf, &endptr, 0); 3992 3993 switch (tolower((unsigned char)*endptr)) { 3994 case 'e': 3995 shift = 60; 3996 break; 3997 case 'p': 3998 shift = 50; 3999 break; 4000 case 't': 4001 shift = 40; 4002 break; 4003 case 'g': 4004 shift = 30; 4005 break; 4006 case 'm': 4007 shift = 20; 4008 break; 4009 case 'k': 4010 shift = 10; 4011 break; 4012 case 'b': 4013 case '\0': /* No unit. */ 4014 *num = number; 4015 return (0); 4016 default: 4017 /* Unrecognized unit. */ 4018 return (-1); 4019 } 4020 4021 if ((number << shift) >> shift != number) { 4022 /* Overflow */ 4023 return (-1); 4024 } 4025 *num = number << shift; 4026 return (0); 4027 } 4028 4029 4030 /* 4031 * This routine could be used in the future to load default and/or saved 4032 * mode page parameters for a particuar lun. 4033 */ 4034 static int 4035 ctl_init_page_index(struct ctl_lun *lun) 4036 { 4037 int i, page_code; 4038 struct ctl_page_index *page_index; 4039 const char *value; 4040 uint64_t ival; 4041 4042 memcpy(&lun->mode_pages.index, page_index_template, 4043 sizeof(page_index_template)); 4044 4045 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4046 4047 page_index = &lun->mode_pages.index[i]; 4048 if (lun->be_lun->lun_type == T_DIRECT && 4049 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4050 continue; 4051 if (lun->be_lun->lun_type == T_PROCESSOR && 4052 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4053 continue; 4054 if (lun->be_lun->lun_type == T_CDROM && 4055 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4056 continue; 4057 4058 page_code = page_index->page_code & SMPH_PC_MASK; 4059 switch (page_code) { 4060 case SMS_RW_ERROR_RECOVERY_PAGE: { 4061 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4062 ("subpage %#x for page %#x is incorrect!", 4063 page_index->subpage, page_code)); 4064 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4065 &rw_er_page_default, 4066 sizeof(rw_er_page_default)); 4067 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4068 &rw_er_page_changeable, 4069 sizeof(rw_er_page_changeable)); 4070 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4071 &rw_er_page_default, 4072 sizeof(rw_er_page_default)); 4073 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4074 &rw_er_page_default, 4075 sizeof(rw_er_page_default)); 4076 page_index->page_data = 4077 (uint8_t *)lun->mode_pages.rw_er_page; 4078 break; 4079 } 4080 case SMS_FORMAT_DEVICE_PAGE: { 4081 struct scsi_format_page *format_page; 4082 4083 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4084 ("subpage %#x for page %#x is incorrect!", 4085 page_index->subpage, page_code)); 4086 4087 /* 4088 * Sectors per track are set above. Bytes per 4089 * sector need to be set here on a per-LUN basis. 4090 */ 4091 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4092 &format_page_default, 4093 sizeof(format_page_default)); 4094 memcpy(&lun->mode_pages.format_page[ 4095 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4096 sizeof(format_page_changeable)); 4097 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4098 &format_page_default, 4099 sizeof(format_page_default)); 4100 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4101 &format_page_default, 4102 sizeof(format_page_default)); 4103 4104 format_page = &lun->mode_pages.format_page[ 4105 CTL_PAGE_CURRENT]; 4106 scsi_ulto2b(lun->be_lun->blocksize, 4107 format_page->bytes_per_sector); 4108 4109 format_page = &lun->mode_pages.format_page[ 4110 CTL_PAGE_DEFAULT]; 4111 scsi_ulto2b(lun->be_lun->blocksize, 4112 format_page->bytes_per_sector); 4113 4114 format_page = &lun->mode_pages.format_page[ 4115 CTL_PAGE_SAVED]; 4116 scsi_ulto2b(lun->be_lun->blocksize, 4117 format_page->bytes_per_sector); 4118 4119 page_index->page_data = 4120 (uint8_t *)lun->mode_pages.format_page; 4121 break; 4122 } 4123 case SMS_RIGID_DISK_PAGE: { 4124 struct scsi_rigid_disk_page *rigid_disk_page; 4125 uint32_t sectors_per_cylinder; 4126 uint64_t cylinders; 4127 #ifndef __XSCALE__ 4128 int shift; 4129 #endif /* !__XSCALE__ */ 4130 4131 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4132 ("subpage %#x for page %#x is incorrect!", 4133 page_index->subpage, page_code)); 4134 4135 /* 4136 * Rotation rate and sectors per track are set 4137 * above. We calculate the cylinders here based on 4138 * capacity. Due to the number of heads and 4139 * sectors per track we're using, smaller arrays 4140 * may turn out to have 0 cylinders. Linux and 4141 * FreeBSD don't pay attention to these mode pages 4142 * to figure out capacity, but Solaris does. It 4143 * seems to deal with 0 cylinders just fine, and 4144 * works out a fake geometry based on the capacity. 4145 */ 4146 memcpy(&lun->mode_pages.rigid_disk_page[ 4147 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4148 sizeof(rigid_disk_page_default)); 4149 memcpy(&lun->mode_pages.rigid_disk_page[ 4150 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4151 sizeof(rigid_disk_page_changeable)); 4152 4153 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4154 CTL_DEFAULT_HEADS; 4155 4156 /* 4157 * The divide method here will be more accurate, 4158 * probably, but results in floating point being 4159 * used in the kernel on i386 (__udivdi3()). On the 4160 * XScale, though, __udivdi3() is implemented in 4161 * software. 4162 * 4163 * The shift method for cylinder calculation is 4164 * accurate if sectors_per_cylinder is a power of 4165 * 2. Otherwise it might be slightly off -- you 4166 * might have a bit of a truncation problem. 4167 */ 4168 #ifdef __XSCALE__ 4169 cylinders = (lun->be_lun->maxlba + 1) / 4170 sectors_per_cylinder; 4171 #else 4172 for (shift = 31; shift > 0; shift--) { 4173 if (sectors_per_cylinder & (1 << shift)) 4174 break; 4175 } 4176 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4177 #endif 4178 4179 /* 4180 * We've basically got 3 bytes, or 24 bits for the 4181 * cylinder size in the mode page. If we're over, 4182 * just round down to 2^24. 4183 */ 4184 if (cylinders > 0xffffff) 4185 cylinders = 0xffffff; 4186 4187 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4188 CTL_PAGE_DEFAULT]; 4189 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4190 4191 if ((value = dnvlist_get_string(lun->be_lun->options, 4192 "rpm", NULL)) != NULL) { 4193 scsi_ulto2b(strtol(value, NULL, 0), 4194 rigid_disk_page->rotation_rate); 4195 } 4196 4197 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4198 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4199 sizeof(rigid_disk_page_default)); 4200 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4201 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4202 sizeof(rigid_disk_page_default)); 4203 4204 page_index->page_data = 4205 (uint8_t *)lun->mode_pages.rigid_disk_page; 4206 break; 4207 } 4208 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4209 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4210 ("subpage %#x for page %#x is incorrect!", 4211 page_index->subpage, page_code)); 4212 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4213 &verify_er_page_default, 4214 sizeof(verify_er_page_default)); 4215 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4216 &verify_er_page_changeable, 4217 sizeof(verify_er_page_changeable)); 4218 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4219 &verify_er_page_default, 4220 sizeof(verify_er_page_default)); 4221 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4222 &verify_er_page_default, 4223 sizeof(verify_er_page_default)); 4224 page_index->page_data = 4225 (uint8_t *)lun->mode_pages.verify_er_page; 4226 break; 4227 } 4228 case SMS_CACHING_PAGE: { 4229 struct scsi_caching_page *caching_page; 4230 4231 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4232 ("subpage %#x for page %#x is incorrect!", 4233 page_index->subpage, page_code)); 4234 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4235 &caching_page_default, 4236 sizeof(caching_page_default)); 4237 memcpy(&lun->mode_pages.caching_page[ 4238 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4239 sizeof(caching_page_changeable)); 4240 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4241 &caching_page_default, 4242 sizeof(caching_page_default)); 4243 caching_page = &lun->mode_pages.caching_page[ 4244 CTL_PAGE_SAVED]; 4245 value = dnvlist_get_string(lun->be_lun->options, 4246 "writecache", NULL); 4247 if (value != NULL && strcmp(value, "off") == 0) 4248 caching_page->flags1 &= ~SCP_WCE; 4249 value = dnvlist_get_string(lun->be_lun->options, 4250 "readcache", NULL); 4251 if (value != NULL && strcmp(value, "off") == 0) 4252 caching_page->flags1 |= SCP_RCD; 4253 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4254 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4255 sizeof(caching_page_default)); 4256 page_index->page_data = 4257 (uint8_t *)lun->mode_pages.caching_page; 4258 break; 4259 } 4260 case SMS_CONTROL_MODE_PAGE: { 4261 switch (page_index->subpage) { 4262 case SMS_SUBPAGE_PAGE_0: { 4263 struct scsi_control_page *control_page; 4264 4265 memcpy(&lun->mode_pages.control_page[ 4266 CTL_PAGE_DEFAULT], 4267 &control_page_default, 4268 sizeof(control_page_default)); 4269 memcpy(&lun->mode_pages.control_page[ 4270 CTL_PAGE_CHANGEABLE], 4271 &control_page_changeable, 4272 sizeof(control_page_changeable)); 4273 memcpy(&lun->mode_pages.control_page[ 4274 CTL_PAGE_SAVED], 4275 &control_page_default, 4276 sizeof(control_page_default)); 4277 control_page = &lun->mode_pages.control_page[ 4278 CTL_PAGE_SAVED]; 4279 value = dnvlist_get_string(lun->be_lun->options, 4280 "reordering", NULL); 4281 if (value != NULL && 4282 strcmp(value, "unrestricted") == 0) { 4283 control_page->queue_flags &= 4284 ~SCP_QUEUE_ALG_MASK; 4285 control_page->queue_flags |= 4286 SCP_QUEUE_ALG_UNRESTRICTED; 4287 } 4288 memcpy(&lun->mode_pages.control_page[ 4289 CTL_PAGE_CURRENT], 4290 &lun->mode_pages.control_page[ 4291 CTL_PAGE_SAVED], 4292 sizeof(control_page_default)); 4293 page_index->page_data = 4294 (uint8_t *)lun->mode_pages.control_page; 4295 break; 4296 } 4297 case 0x01: 4298 memcpy(&lun->mode_pages.control_ext_page[ 4299 CTL_PAGE_DEFAULT], 4300 &control_ext_page_default, 4301 sizeof(control_ext_page_default)); 4302 memcpy(&lun->mode_pages.control_ext_page[ 4303 CTL_PAGE_CHANGEABLE], 4304 &control_ext_page_changeable, 4305 sizeof(control_ext_page_changeable)); 4306 memcpy(&lun->mode_pages.control_ext_page[ 4307 CTL_PAGE_SAVED], 4308 &control_ext_page_default, 4309 sizeof(control_ext_page_default)); 4310 memcpy(&lun->mode_pages.control_ext_page[ 4311 CTL_PAGE_CURRENT], 4312 &lun->mode_pages.control_ext_page[ 4313 CTL_PAGE_SAVED], 4314 sizeof(control_ext_page_default)); 4315 page_index->page_data = 4316 (uint8_t *)lun->mode_pages.control_ext_page; 4317 break; 4318 default: 4319 panic("subpage %#x for page %#x is incorrect!", 4320 page_index->subpage, page_code); 4321 } 4322 break; 4323 } 4324 case SMS_INFO_EXCEPTIONS_PAGE: { 4325 switch (page_index->subpage) { 4326 case SMS_SUBPAGE_PAGE_0: 4327 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4328 &ie_page_default, 4329 sizeof(ie_page_default)); 4330 memcpy(&lun->mode_pages.ie_page[ 4331 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4332 sizeof(ie_page_changeable)); 4333 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4334 &ie_page_default, 4335 sizeof(ie_page_default)); 4336 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4337 &ie_page_default, 4338 sizeof(ie_page_default)); 4339 page_index->page_data = 4340 (uint8_t *)lun->mode_pages.ie_page; 4341 break; 4342 case 0x02: { 4343 struct ctl_logical_block_provisioning_page *page; 4344 4345 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4346 &lbp_page_default, 4347 sizeof(lbp_page_default)); 4348 memcpy(&lun->mode_pages.lbp_page[ 4349 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4350 sizeof(lbp_page_changeable)); 4351 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4352 &lbp_page_default, 4353 sizeof(lbp_page_default)); 4354 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4355 value = dnvlist_get_string(lun->be_lun->options, 4356 "avail-threshold", NULL); 4357 if (value != NULL && 4358 ctl_expand_number(value, &ival) == 0) { 4359 page->descr[0].flags |= SLBPPD_ENABLED | 4360 SLBPPD_ARMING_DEC; 4361 if (lun->be_lun->blocksize) 4362 ival /= lun->be_lun->blocksize; 4363 else 4364 ival /= 512; 4365 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4366 page->descr[0].count); 4367 } 4368 value = dnvlist_get_string(lun->be_lun->options, 4369 "used-threshold", NULL); 4370 if (value != NULL && 4371 ctl_expand_number(value, &ival) == 0) { 4372 page->descr[1].flags |= SLBPPD_ENABLED | 4373 SLBPPD_ARMING_INC; 4374 if (lun->be_lun->blocksize) 4375 ival /= lun->be_lun->blocksize; 4376 else 4377 ival /= 512; 4378 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4379 page->descr[1].count); 4380 } 4381 value = dnvlist_get_string(lun->be_lun->options, 4382 "pool-avail-threshold", NULL); 4383 if (value != NULL && 4384 ctl_expand_number(value, &ival) == 0) { 4385 page->descr[2].flags |= SLBPPD_ENABLED | 4386 SLBPPD_ARMING_DEC; 4387 if (lun->be_lun->blocksize) 4388 ival /= lun->be_lun->blocksize; 4389 else 4390 ival /= 512; 4391 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4392 page->descr[2].count); 4393 } 4394 value = dnvlist_get_string(lun->be_lun->options, 4395 "pool-used-threshold", NULL); 4396 if (value != NULL && 4397 ctl_expand_number(value, &ival) == 0) { 4398 page->descr[3].flags |= SLBPPD_ENABLED | 4399 SLBPPD_ARMING_INC; 4400 if (lun->be_lun->blocksize) 4401 ival /= lun->be_lun->blocksize; 4402 else 4403 ival /= 512; 4404 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4405 page->descr[3].count); 4406 } 4407 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4408 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4409 sizeof(lbp_page_default)); 4410 page_index->page_data = 4411 (uint8_t *)lun->mode_pages.lbp_page; 4412 break; 4413 } 4414 default: 4415 panic("subpage %#x for page %#x is incorrect!", 4416 page_index->subpage, page_code); 4417 } 4418 break; 4419 } 4420 case SMS_CDDVD_CAPS_PAGE:{ 4421 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4422 ("subpage %#x for page %#x is incorrect!", 4423 page_index->subpage, page_code)); 4424 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4425 &cddvd_page_default, 4426 sizeof(cddvd_page_default)); 4427 memcpy(&lun->mode_pages.cddvd_page[ 4428 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4429 sizeof(cddvd_page_changeable)); 4430 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4431 &cddvd_page_default, 4432 sizeof(cddvd_page_default)); 4433 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4434 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4435 sizeof(cddvd_page_default)); 4436 page_index->page_data = 4437 (uint8_t *)lun->mode_pages.cddvd_page; 4438 break; 4439 } 4440 default: 4441 panic("invalid page code value %#x", page_code); 4442 } 4443 } 4444 4445 return (CTL_RETVAL_COMPLETE); 4446 } 4447 4448 static int 4449 ctl_init_log_page_index(struct ctl_lun *lun) 4450 { 4451 struct ctl_page_index *page_index; 4452 int i, j, k, prev; 4453 4454 memcpy(&lun->log_pages.index, log_page_index_template, 4455 sizeof(log_page_index_template)); 4456 4457 prev = -1; 4458 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4459 4460 page_index = &lun->log_pages.index[i]; 4461 if (lun->be_lun->lun_type == T_DIRECT && 4462 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4463 continue; 4464 if (lun->be_lun->lun_type == T_PROCESSOR && 4465 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4466 continue; 4467 if (lun->be_lun->lun_type == T_CDROM && 4468 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4469 continue; 4470 4471 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4472 lun->backend->lun_attr == NULL) 4473 continue; 4474 4475 if (page_index->page_code != prev) { 4476 lun->log_pages.pages_page[j] = page_index->page_code; 4477 prev = page_index->page_code; 4478 j++; 4479 } 4480 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4481 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4482 k++; 4483 } 4484 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4485 lun->log_pages.index[0].page_len = j; 4486 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4487 lun->log_pages.index[1].page_len = k * 2; 4488 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4489 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4490 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4491 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4492 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4493 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4494 4495 return (CTL_RETVAL_COMPLETE); 4496 } 4497 4498 static int 4499 hex2bin(const char *str, uint8_t *buf, int buf_size) 4500 { 4501 int i; 4502 u_char c; 4503 4504 memset(buf, 0, buf_size); 4505 while (isspace(str[0])) 4506 str++; 4507 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4508 str += 2; 4509 buf_size *= 2; 4510 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4511 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4512 str++; 4513 c = str[i]; 4514 if (isdigit(c)) 4515 c -= '0'; 4516 else if (isalpha(c)) 4517 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4518 else 4519 break; 4520 if (c >= 16) 4521 break; 4522 if ((i & 1) == 0) 4523 buf[i / 2] |= (c << 4); 4524 else 4525 buf[i / 2] |= c; 4526 } 4527 return ((i + 1) / 2); 4528 } 4529 4530 /* 4531 * LUN allocation. 4532 * 4533 * Requirements: 4534 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4535 * wants us to allocate the LUN and he can block. 4536 * - ctl_softc is always set 4537 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4538 * 4539 * Returns 0 for success, non-zero (errno) for failure. 4540 */ 4541 static int 4542 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4543 struct ctl_be_lun *const be_lun) 4544 { 4545 struct ctl_lun *nlun, *lun; 4546 struct scsi_vpd_id_descriptor *desc; 4547 struct scsi_vpd_id_t10 *t10id; 4548 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4549 int lun_number, lun_malloced; 4550 int devidlen, idlen1, idlen2 = 0, len; 4551 4552 if (be_lun == NULL) 4553 return (EINVAL); 4554 4555 /* 4556 * We currently only support Direct Access or Processor LUN types. 4557 */ 4558 switch (be_lun->lun_type) { 4559 case T_DIRECT: 4560 case T_PROCESSOR: 4561 case T_CDROM: 4562 break; 4563 case T_SEQUENTIAL: 4564 case T_CHANGER: 4565 default: 4566 be_lun->lun_config_status(be_lun->be_lun, 4567 CTL_LUN_CONFIG_FAILURE); 4568 break; 4569 } 4570 if (ctl_lun == NULL) { 4571 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4572 lun_malloced = 1; 4573 } else { 4574 lun_malloced = 0; 4575 lun = ctl_lun; 4576 } 4577 4578 memset(lun, 0, sizeof(*lun)); 4579 if (lun_malloced) 4580 lun->flags = CTL_LUN_MALLOCED; 4581 4582 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4583 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4584 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4585 M_DEVBUF, M_WAITOK | M_ZERO); 4586 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4587 M_DEVBUF, M_WAITOK | M_ZERO); 4588 4589 /* Generate LUN ID. */ 4590 devidlen = max(CTL_DEVID_MIN_LEN, 4591 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4592 idlen1 = sizeof(*t10id) + devidlen; 4593 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4594 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4595 if (scsiname != NULL) { 4596 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4597 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4598 } 4599 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4600 if (eui != NULL) { 4601 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4602 } 4603 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4604 if (naa != NULL) { 4605 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4606 } 4607 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4608 if (uuid != NULL) { 4609 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4610 } 4611 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4612 M_CTL, M_WAITOK | M_ZERO); 4613 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4614 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4615 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4616 desc->length = idlen1; 4617 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4618 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4619 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4620 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4621 } else { 4622 strncpy(t10id->vendor, vendor, 4623 min(sizeof(t10id->vendor), strlen(vendor))); 4624 } 4625 strncpy((char *)t10id->vendor_spec_id, 4626 (char *)be_lun->device_id, devidlen); 4627 if (scsiname != NULL) { 4628 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4629 desc->length); 4630 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4631 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4632 SVPD_ID_TYPE_SCSI_NAME; 4633 desc->length = idlen2; 4634 strlcpy(desc->identifier, scsiname, idlen2); 4635 } 4636 if (eui != NULL) { 4637 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4638 desc->length); 4639 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4640 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4641 SVPD_ID_TYPE_EUI64; 4642 desc->length = hex2bin(eui, desc->identifier, 16); 4643 desc->length = desc->length > 12 ? 16 : 4644 (desc->length > 8 ? 12 : 8); 4645 len -= 16 - desc->length; 4646 } 4647 if (naa != NULL) { 4648 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4649 desc->length); 4650 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4651 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4652 SVPD_ID_TYPE_NAA; 4653 desc->length = hex2bin(naa, desc->identifier, 16); 4654 desc->length = desc->length > 8 ? 16 : 8; 4655 len -= 16 - desc->length; 4656 } 4657 if (uuid != NULL) { 4658 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4659 desc->length); 4660 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4661 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4662 SVPD_ID_TYPE_UUID; 4663 desc->identifier[0] = 0x10; 4664 hex2bin(uuid, &desc->identifier[2], 16); 4665 desc->length = 18; 4666 } 4667 lun->lun_devid->len = len; 4668 4669 mtx_lock(&ctl_softc->ctl_lock); 4670 /* 4671 * See if the caller requested a particular LUN number. If so, see 4672 * if it is available. Otherwise, allocate the first available LUN. 4673 */ 4674 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4675 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4676 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4677 mtx_unlock(&ctl_softc->ctl_lock); 4678 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4679 printf("ctl: requested LUN ID %d is higher " 4680 "than ctl_max_luns - 1 (%d)\n", 4681 be_lun->req_lun_id, ctl_max_luns - 1); 4682 } else { 4683 /* 4684 * XXX KDM return an error, or just assign 4685 * another LUN ID in this case?? 4686 */ 4687 printf("ctl: requested LUN ID %d is already " 4688 "in use\n", be_lun->req_lun_id); 4689 } 4690 fail: 4691 free(lun->lun_devid, M_CTL); 4692 if (lun->flags & CTL_LUN_MALLOCED) 4693 free(lun, M_CTL); 4694 be_lun->lun_config_status(be_lun->be_lun, 4695 CTL_LUN_CONFIG_FAILURE); 4696 return (ENOSPC); 4697 } 4698 lun_number = be_lun->req_lun_id; 4699 } else { 4700 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4701 if (lun_number == -1) { 4702 mtx_unlock(&ctl_softc->ctl_lock); 4703 printf("ctl: can't allocate LUN, out of LUNs\n"); 4704 goto fail; 4705 } 4706 } 4707 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4708 mtx_unlock(&ctl_softc->ctl_lock); 4709 4710 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4711 lun->lun = lun_number; 4712 lun->be_lun = be_lun; 4713 /* 4714 * The processor LUN is always enabled. Disk LUNs come on line 4715 * disabled, and must be enabled by the backend. 4716 */ 4717 lun->flags |= CTL_LUN_DISABLED; 4718 lun->backend = be_lun->be; 4719 be_lun->ctl_lun = lun; 4720 be_lun->lun_id = lun_number; 4721 atomic_add_int(&be_lun->be->num_luns, 1); 4722 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4723 lun->flags |= CTL_LUN_EJECTED; 4724 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4725 lun->flags |= CTL_LUN_NO_MEDIA; 4726 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4727 lun->flags |= CTL_LUN_STOPPED; 4728 4729 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4730 lun->flags |= CTL_LUN_PRIMARY_SC; 4731 4732 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4733 if (value != NULL) { 4734 if (strcmp(value, "on") == 0) 4735 lun->flags |= CTL_LUN_REMOVABLE; 4736 } else if (be_lun->lun_type == T_CDROM) 4737 lun->flags |= CTL_LUN_REMOVABLE; 4738 4739 lun->ctl_softc = ctl_softc; 4740 #ifdef CTL_TIME_IO 4741 lun->last_busy = getsbinuptime(); 4742 #endif 4743 TAILQ_INIT(&lun->ooa_queue); 4744 TAILQ_INIT(&lun->blocked_queue); 4745 STAILQ_INIT(&lun->error_list); 4746 lun->ie_reported = 1; 4747 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4748 ctl_tpc_lun_init(lun); 4749 if (lun->flags & CTL_LUN_REMOVABLE) { 4750 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4751 M_CTL, M_WAITOK); 4752 } 4753 4754 /* 4755 * Initialize the mode and log page index. 4756 */ 4757 ctl_init_page_index(lun); 4758 ctl_init_log_page_index(lun); 4759 4760 /* Setup statistics gathering */ 4761 #ifdef CTL_LEGACY_STATS 4762 lun->legacy_stats.device_type = be_lun->lun_type; 4763 lun->legacy_stats.lun_number = lun_number; 4764 lun->legacy_stats.blocksize = be_lun->blocksize; 4765 if (be_lun->blocksize == 0) 4766 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4767 lun->legacy_stats.ports = malloc(sizeof(struct ctl_lun_io_port_stats) * 4768 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4769 for (len = 0; len < ctl_max_ports; len++) 4770 lun->legacy_stats.ports[len].targ_port = len; 4771 #endif /* CTL_LEGACY_STATS */ 4772 lun->stats.item = lun_number; 4773 4774 /* 4775 * Now, before we insert this lun on the lun list, set the lun 4776 * inventory changed UA for all other luns. 4777 */ 4778 mtx_lock(&ctl_softc->ctl_lock); 4779 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4780 mtx_lock(&nlun->lun_lock); 4781 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4782 mtx_unlock(&nlun->lun_lock); 4783 } 4784 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4785 ctl_softc->ctl_luns[lun_number] = lun; 4786 ctl_softc->num_luns++; 4787 mtx_unlock(&ctl_softc->ctl_lock); 4788 4789 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4790 return (0); 4791 } 4792 4793 /* 4794 * Delete a LUN. 4795 * Assumptions: 4796 * - LUN has already been marked invalid and any pending I/O has been taken 4797 * care of. 4798 */ 4799 static int 4800 ctl_free_lun(struct ctl_lun *lun) 4801 { 4802 struct ctl_softc *softc = lun->ctl_softc; 4803 struct ctl_lun *nlun; 4804 int i; 4805 4806 KASSERT(TAILQ_EMPTY(&lun->ooa_queue), 4807 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4808 4809 mtx_lock(&softc->ctl_lock); 4810 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4811 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4812 softc->ctl_luns[lun->lun] = NULL; 4813 softc->num_luns--; 4814 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4815 mtx_lock(&nlun->lun_lock); 4816 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4817 mtx_unlock(&nlun->lun_lock); 4818 } 4819 mtx_unlock(&softc->ctl_lock); 4820 4821 /* 4822 * Tell the backend to free resources, if this LUN has a backend. 4823 */ 4824 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4825 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4826 4827 lun->ie_reportcnt = UINT32_MAX; 4828 callout_drain(&lun->ie_callout); 4829 ctl_tpc_lun_shutdown(lun); 4830 mtx_destroy(&lun->lun_lock); 4831 free(lun->lun_devid, M_CTL); 4832 for (i = 0; i < ctl_max_ports; i++) 4833 free(lun->pending_ua[i], M_CTL); 4834 free(lun->pending_ua, M_DEVBUF); 4835 for (i = 0; i < ctl_max_ports; i++) 4836 free(lun->pr_keys[i], M_CTL); 4837 free(lun->pr_keys, M_DEVBUF); 4838 free(lun->write_buffer, M_CTL); 4839 free(lun->prevent, M_CTL); 4840 if (lun->flags & CTL_LUN_MALLOCED) 4841 free(lun, M_CTL); 4842 4843 return (0); 4844 } 4845 4846 static void 4847 ctl_create_lun(struct ctl_be_lun *be_lun) 4848 { 4849 4850 /* 4851 * ctl_alloc_lun() should handle all potential failure cases. 4852 */ 4853 ctl_alloc_lun(control_softc, NULL, be_lun); 4854 } 4855 4856 int 4857 ctl_add_lun(struct ctl_be_lun *be_lun) 4858 { 4859 struct ctl_softc *softc = control_softc; 4860 4861 mtx_lock(&softc->ctl_lock); 4862 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4863 mtx_unlock(&softc->ctl_lock); 4864 wakeup(&softc->pending_lun_queue); 4865 4866 return (0); 4867 } 4868 4869 int 4870 ctl_enable_lun(struct ctl_be_lun *be_lun) 4871 { 4872 struct ctl_softc *softc; 4873 struct ctl_port *port, *nport; 4874 struct ctl_lun *lun; 4875 int retval; 4876 4877 lun = (struct ctl_lun *)be_lun->ctl_lun; 4878 softc = lun->ctl_softc; 4879 4880 mtx_lock(&softc->ctl_lock); 4881 mtx_lock(&lun->lun_lock); 4882 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4883 /* 4884 * eh? Why did we get called if the LUN is already 4885 * enabled? 4886 */ 4887 mtx_unlock(&lun->lun_lock); 4888 mtx_unlock(&softc->ctl_lock); 4889 return (0); 4890 } 4891 lun->flags &= ~CTL_LUN_DISABLED; 4892 mtx_unlock(&lun->lun_lock); 4893 4894 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4895 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4896 port->lun_map != NULL || port->lun_enable == NULL) 4897 continue; 4898 4899 /* 4900 * Drop the lock while we call the FETD's enable routine. 4901 * This can lead to a callback into CTL (at least in the 4902 * case of the internal initiator frontend. 4903 */ 4904 mtx_unlock(&softc->ctl_lock); 4905 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4906 mtx_lock(&softc->ctl_lock); 4907 if (retval != 0) { 4908 printf("%s: FETD %s port %d returned error " 4909 "%d for lun_enable on lun %jd\n", 4910 __func__, port->port_name, port->targ_port, 4911 retval, (intmax_t)lun->lun); 4912 } 4913 } 4914 4915 mtx_unlock(&softc->ctl_lock); 4916 ctl_isc_announce_lun(lun); 4917 4918 return (0); 4919 } 4920 4921 int 4922 ctl_disable_lun(struct ctl_be_lun *be_lun) 4923 { 4924 struct ctl_softc *softc; 4925 struct ctl_port *port; 4926 struct ctl_lun *lun; 4927 int retval; 4928 4929 lun = (struct ctl_lun *)be_lun->ctl_lun; 4930 softc = lun->ctl_softc; 4931 4932 mtx_lock(&softc->ctl_lock); 4933 mtx_lock(&lun->lun_lock); 4934 if (lun->flags & CTL_LUN_DISABLED) { 4935 mtx_unlock(&lun->lun_lock); 4936 mtx_unlock(&softc->ctl_lock); 4937 return (0); 4938 } 4939 lun->flags |= CTL_LUN_DISABLED; 4940 mtx_unlock(&lun->lun_lock); 4941 4942 STAILQ_FOREACH(port, &softc->port_list, links) { 4943 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4944 port->lun_map != NULL || port->lun_disable == NULL) 4945 continue; 4946 4947 /* 4948 * Drop the lock before we call the frontend's disable 4949 * routine, to avoid lock order reversals. 4950 * 4951 * XXX KDM what happens if the frontend list changes while 4952 * we're traversing it? It's unlikely, but should be handled. 4953 */ 4954 mtx_unlock(&softc->ctl_lock); 4955 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4956 mtx_lock(&softc->ctl_lock); 4957 if (retval != 0) { 4958 printf("%s: FETD %s port %d returned error " 4959 "%d for lun_disable on lun %jd\n", 4960 __func__, port->port_name, port->targ_port, 4961 retval, (intmax_t)lun->lun); 4962 } 4963 } 4964 4965 mtx_unlock(&softc->ctl_lock); 4966 ctl_isc_announce_lun(lun); 4967 4968 return (0); 4969 } 4970 4971 int 4972 ctl_start_lun(struct ctl_be_lun *be_lun) 4973 { 4974 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4975 4976 mtx_lock(&lun->lun_lock); 4977 lun->flags &= ~CTL_LUN_STOPPED; 4978 mtx_unlock(&lun->lun_lock); 4979 return (0); 4980 } 4981 4982 int 4983 ctl_stop_lun(struct ctl_be_lun *be_lun) 4984 { 4985 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4986 4987 mtx_lock(&lun->lun_lock); 4988 lun->flags |= CTL_LUN_STOPPED; 4989 mtx_unlock(&lun->lun_lock); 4990 return (0); 4991 } 4992 4993 int 4994 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4995 { 4996 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4997 4998 mtx_lock(&lun->lun_lock); 4999 lun->flags |= CTL_LUN_NO_MEDIA; 5000 mtx_unlock(&lun->lun_lock); 5001 return (0); 5002 } 5003 5004 int 5005 ctl_lun_has_media(struct ctl_be_lun *be_lun) 5006 { 5007 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5008 union ctl_ha_msg msg; 5009 5010 mtx_lock(&lun->lun_lock); 5011 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 5012 if (lun->flags & CTL_LUN_REMOVABLE) 5013 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 5014 mtx_unlock(&lun->lun_lock); 5015 if ((lun->flags & CTL_LUN_REMOVABLE) && 5016 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5017 bzero(&msg.ua, sizeof(msg.ua)); 5018 msg.hdr.msg_type = CTL_MSG_UA; 5019 msg.hdr.nexus.initid = -1; 5020 msg.hdr.nexus.targ_port = -1; 5021 msg.hdr.nexus.targ_lun = lun->lun; 5022 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5023 msg.ua.ua_all = 1; 5024 msg.ua.ua_set = 1; 5025 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 5026 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5027 M_WAITOK); 5028 } 5029 return (0); 5030 } 5031 5032 int 5033 ctl_lun_ejected(struct ctl_be_lun *be_lun) 5034 { 5035 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5036 5037 mtx_lock(&lun->lun_lock); 5038 lun->flags |= CTL_LUN_EJECTED; 5039 mtx_unlock(&lun->lun_lock); 5040 return (0); 5041 } 5042 5043 int 5044 ctl_lun_primary(struct ctl_be_lun *be_lun) 5045 { 5046 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5047 5048 mtx_lock(&lun->lun_lock); 5049 lun->flags |= CTL_LUN_PRIMARY_SC; 5050 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5051 mtx_unlock(&lun->lun_lock); 5052 ctl_isc_announce_lun(lun); 5053 return (0); 5054 } 5055 5056 int 5057 ctl_lun_secondary(struct ctl_be_lun *be_lun) 5058 { 5059 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5060 5061 mtx_lock(&lun->lun_lock); 5062 lun->flags &= ~CTL_LUN_PRIMARY_SC; 5063 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5064 mtx_unlock(&lun->lun_lock); 5065 ctl_isc_announce_lun(lun); 5066 return (0); 5067 } 5068 5069 int 5070 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 5071 { 5072 struct ctl_lun *lun; 5073 5074 lun = (struct ctl_lun *)be_lun->ctl_lun; 5075 5076 mtx_lock(&lun->lun_lock); 5077 5078 /* 5079 * The LUN needs to be disabled before it can be marked invalid. 5080 */ 5081 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5082 mtx_unlock(&lun->lun_lock); 5083 return (-1); 5084 } 5085 /* 5086 * Mark the LUN invalid. 5087 */ 5088 lun->flags |= CTL_LUN_INVALID; 5089 5090 /* 5091 * If there is nothing in the OOA queue, go ahead and free the LUN. 5092 * If we have something in the OOA queue, we'll free it when the 5093 * last I/O completes. 5094 */ 5095 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5096 mtx_unlock(&lun->lun_lock); 5097 ctl_free_lun(lun); 5098 } else 5099 mtx_unlock(&lun->lun_lock); 5100 5101 return (0); 5102 } 5103 5104 void 5105 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5106 { 5107 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5108 union ctl_ha_msg msg; 5109 5110 mtx_lock(&lun->lun_lock); 5111 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5112 mtx_unlock(&lun->lun_lock); 5113 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5114 /* Send msg to other side. */ 5115 bzero(&msg.ua, sizeof(msg.ua)); 5116 msg.hdr.msg_type = CTL_MSG_UA; 5117 msg.hdr.nexus.initid = -1; 5118 msg.hdr.nexus.targ_port = -1; 5119 msg.hdr.nexus.targ_lun = lun->lun; 5120 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5121 msg.ua.ua_all = 1; 5122 msg.ua.ua_set = 1; 5123 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5124 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5125 M_WAITOK); 5126 } 5127 } 5128 5129 /* 5130 * Backend "memory move is complete" callback for requests that never 5131 * make it down to say RAIDCore's configuration code. 5132 */ 5133 int 5134 ctl_config_move_done(union ctl_io *io) 5135 { 5136 int retval; 5137 5138 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5139 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5140 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5141 5142 if ((io->io_hdr.port_status != 0) && 5143 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5144 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5145 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 5146 /*retry_count*/ io->io_hdr.port_status); 5147 } else if (io->scsiio.kern_data_resid != 0 && 5148 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 5149 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5150 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5151 ctl_set_invalid_field_ciu(&io->scsiio); 5152 } 5153 5154 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5155 ctl_data_print(io); 5156 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5157 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5158 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5159 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5160 /* 5161 * XXX KDM just assuming a single pointer here, and not a 5162 * S/G list. If we start using S/G lists for config data, 5163 * we'll need to know how to clean them up here as well. 5164 */ 5165 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5166 free(io->scsiio.kern_data_ptr, M_CTL); 5167 ctl_done(io); 5168 retval = CTL_RETVAL_COMPLETE; 5169 } else { 5170 /* 5171 * XXX KDM now we need to continue data movement. Some 5172 * options: 5173 * - call ctl_scsiio() again? We don't do this for data 5174 * writes, because for those at least we know ahead of 5175 * time where the write will go and how long it is. For 5176 * config writes, though, that information is largely 5177 * contained within the write itself, thus we need to 5178 * parse out the data again. 5179 * 5180 * - Call some other function once the data is in? 5181 */ 5182 5183 /* 5184 * XXX KDM call ctl_scsiio() again for now, and check flag 5185 * bits to see whether we're allocated or not. 5186 */ 5187 retval = ctl_scsiio(&io->scsiio); 5188 } 5189 return (retval); 5190 } 5191 5192 /* 5193 * This gets called by a backend driver when it is done with a 5194 * data_submit method. 5195 */ 5196 void 5197 ctl_data_submit_done(union ctl_io *io) 5198 { 5199 /* 5200 * If the IO_CONT flag is set, we need to call the supplied 5201 * function to continue processing the I/O, instead of completing 5202 * the I/O just yet. 5203 * 5204 * If there is an error, though, we don't want to keep processing. 5205 * Instead, just send status back to the initiator. 5206 */ 5207 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5208 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5209 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5210 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5211 io->scsiio.io_cont(io); 5212 return; 5213 } 5214 ctl_done(io); 5215 } 5216 5217 /* 5218 * This gets called by a backend driver when it is done with a 5219 * configuration write. 5220 */ 5221 void 5222 ctl_config_write_done(union ctl_io *io) 5223 { 5224 uint8_t *buf; 5225 5226 /* 5227 * If the IO_CONT flag is set, we need to call the supplied 5228 * function to continue processing the I/O, instead of completing 5229 * the I/O just yet. 5230 * 5231 * If there is an error, though, we don't want to keep processing. 5232 * Instead, just send status back to the initiator. 5233 */ 5234 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5235 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5236 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5237 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5238 io->scsiio.io_cont(io); 5239 return; 5240 } 5241 /* 5242 * Since a configuration write can be done for commands that actually 5243 * have data allocated, like write buffer, and commands that have 5244 * no data, like start/stop unit, we need to check here. 5245 */ 5246 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5247 buf = io->scsiio.kern_data_ptr; 5248 else 5249 buf = NULL; 5250 ctl_done(io); 5251 if (buf) 5252 free(buf, M_CTL); 5253 } 5254 5255 void 5256 ctl_config_read_done(union ctl_io *io) 5257 { 5258 uint8_t *buf; 5259 5260 /* 5261 * If there is some error -- we are done, skip data transfer. 5262 */ 5263 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5264 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5265 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5266 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5267 buf = io->scsiio.kern_data_ptr; 5268 else 5269 buf = NULL; 5270 ctl_done(io); 5271 if (buf) 5272 free(buf, M_CTL); 5273 return; 5274 } 5275 5276 /* 5277 * If the IO_CONT flag is set, we need to call the supplied 5278 * function to continue processing the I/O, instead of completing 5279 * the I/O just yet. 5280 */ 5281 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5282 io->scsiio.io_cont(io); 5283 return; 5284 } 5285 5286 ctl_datamove(io); 5287 } 5288 5289 /* 5290 * SCSI release command. 5291 */ 5292 int 5293 ctl_scsi_release(struct ctl_scsiio *ctsio) 5294 { 5295 struct ctl_lun *lun = CTL_LUN(ctsio); 5296 uint32_t residx; 5297 5298 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5299 5300 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5301 5302 /* 5303 * XXX KDM right now, we only support LUN reservation. We don't 5304 * support 3rd party reservations, or extent reservations, which 5305 * might actually need the parameter list. If we've gotten this 5306 * far, we've got a LUN reservation. Anything else got kicked out 5307 * above. So, according to SPC, ignore the length. 5308 */ 5309 5310 mtx_lock(&lun->lun_lock); 5311 5312 /* 5313 * According to SPC, it is not an error for an intiator to attempt 5314 * to release a reservation on a LUN that isn't reserved, or that 5315 * is reserved by another initiator. The reservation can only be 5316 * released, though, by the initiator who made it or by one of 5317 * several reset type events. 5318 */ 5319 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5320 lun->flags &= ~CTL_LUN_RESERVED; 5321 5322 mtx_unlock(&lun->lun_lock); 5323 5324 ctl_set_success(ctsio); 5325 ctl_done((union ctl_io *)ctsio); 5326 return (CTL_RETVAL_COMPLETE); 5327 } 5328 5329 int 5330 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5331 { 5332 struct ctl_lun *lun = CTL_LUN(ctsio); 5333 uint32_t residx; 5334 5335 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5336 5337 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5338 5339 /* 5340 * XXX KDM right now, we only support LUN reservation. We don't 5341 * support 3rd party reservations, or extent reservations, which 5342 * might actually need the parameter list. If we've gotten this 5343 * far, we've got a LUN reservation. Anything else got kicked out 5344 * above. So, according to SPC, ignore the length. 5345 */ 5346 5347 mtx_lock(&lun->lun_lock); 5348 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5349 ctl_set_reservation_conflict(ctsio); 5350 goto bailout; 5351 } 5352 5353 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5354 if (lun->flags & CTL_LUN_PR_RESERVED) { 5355 ctl_set_success(ctsio); 5356 goto bailout; 5357 } 5358 5359 lun->flags |= CTL_LUN_RESERVED; 5360 lun->res_idx = residx; 5361 ctl_set_success(ctsio); 5362 5363 bailout: 5364 mtx_unlock(&lun->lun_lock); 5365 ctl_done((union ctl_io *)ctsio); 5366 return (CTL_RETVAL_COMPLETE); 5367 } 5368 5369 int 5370 ctl_start_stop(struct ctl_scsiio *ctsio) 5371 { 5372 struct ctl_lun *lun = CTL_LUN(ctsio); 5373 struct scsi_start_stop_unit *cdb; 5374 int retval; 5375 5376 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5377 5378 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5379 5380 if ((cdb->how & SSS_PC_MASK) == 0) { 5381 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5382 (cdb->how & SSS_START) == 0) { 5383 uint32_t residx; 5384 5385 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5386 if (ctl_get_prkey(lun, residx) == 0 || 5387 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5388 5389 ctl_set_reservation_conflict(ctsio); 5390 ctl_done((union ctl_io *)ctsio); 5391 return (CTL_RETVAL_COMPLETE); 5392 } 5393 } 5394 5395 if ((cdb->how & SSS_LOEJ) && 5396 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5397 ctl_set_invalid_field(ctsio, 5398 /*sks_valid*/ 1, 5399 /*command*/ 1, 5400 /*field*/ 4, 5401 /*bit_valid*/ 1, 5402 /*bit*/ 1); 5403 ctl_done((union ctl_io *)ctsio); 5404 return (CTL_RETVAL_COMPLETE); 5405 } 5406 5407 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5408 lun->prevent_count > 0) { 5409 /* "Medium removal prevented" */ 5410 ctl_set_sense(ctsio, /*current_error*/ 1, 5411 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5412 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5413 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5414 ctl_done((union ctl_io *)ctsio); 5415 return (CTL_RETVAL_COMPLETE); 5416 } 5417 } 5418 5419 retval = lun->backend->config_write((union ctl_io *)ctsio); 5420 return (retval); 5421 } 5422 5423 int 5424 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5425 { 5426 struct ctl_lun *lun = CTL_LUN(ctsio); 5427 struct scsi_prevent *cdb; 5428 int retval; 5429 uint32_t initidx; 5430 5431 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5432 5433 cdb = (struct scsi_prevent *)ctsio->cdb; 5434 5435 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5436 ctl_set_invalid_opcode(ctsio); 5437 ctl_done((union ctl_io *)ctsio); 5438 return (CTL_RETVAL_COMPLETE); 5439 } 5440 5441 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5442 mtx_lock(&lun->lun_lock); 5443 if ((cdb->how & PR_PREVENT) && 5444 ctl_is_set(lun->prevent, initidx) == 0) { 5445 ctl_set_mask(lun->prevent, initidx); 5446 lun->prevent_count++; 5447 } else if ((cdb->how & PR_PREVENT) == 0 && 5448 ctl_is_set(lun->prevent, initidx)) { 5449 ctl_clear_mask(lun->prevent, initidx); 5450 lun->prevent_count--; 5451 } 5452 mtx_unlock(&lun->lun_lock); 5453 retval = lun->backend->config_write((union ctl_io *)ctsio); 5454 return (retval); 5455 } 5456 5457 /* 5458 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5459 * we don't really do anything with the LBA and length fields if the user 5460 * passes them in. Instead we'll just flush out the cache for the entire 5461 * LUN. 5462 */ 5463 int 5464 ctl_sync_cache(struct ctl_scsiio *ctsio) 5465 { 5466 struct ctl_lun *lun = CTL_LUN(ctsio); 5467 struct ctl_lba_len_flags *lbalen; 5468 uint64_t starting_lba; 5469 uint32_t block_count; 5470 int retval; 5471 uint8_t byte2; 5472 5473 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5474 5475 retval = 0; 5476 5477 switch (ctsio->cdb[0]) { 5478 case SYNCHRONIZE_CACHE: { 5479 struct scsi_sync_cache *cdb; 5480 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5481 5482 starting_lba = scsi_4btoul(cdb->begin_lba); 5483 block_count = scsi_2btoul(cdb->lb_count); 5484 byte2 = cdb->byte2; 5485 break; 5486 } 5487 case SYNCHRONIZE_CACHE_16: { 5488 struct scsi_sync_cache_16 *cdb; 5489 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5490 5491 starting_lba = scsi_8btou64(cdb->begin_lba); 5492 block_count = scsi_4btoul(cdb->lb_count); 5493 byte2 = cdb->byte2; 5494 break; 5495 } 5496 default: 5497 ctl_set_invalid_opcode(ctsio); 5498 ctl_done((union ctl_io *)ctsio); 5499 goto bailout; 5500 break; /* NOTREACHED */ 5501 } 5502 5503 /* 5504 * We check the LBA and length, but don't do anything with them. 5505 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5506 * get flushed. This check will just help satisfy anyone who wants 5507 * to see an error for an out of range LBA. 5508 */ 5509 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5510 ctl_set_lba_out_of_range(ctsio, 5511 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5512 ctl_done((union ctl_io *)ctsio); 5513 goto bailout; 5514 } 5515 5516 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5517 lbalen->lba = starting_lba; 5518 lbalen->len = block_count; 5519 lbalen->flags = byte2; 5520 retval = lun->backend->config_write((union ctl_io *)ctsio); 5521 5522 bailout: 5523 return (retval); 5524 } 5525 5526 int 5527 ctl_format(struct ctl_scsiio *ctsio) 5528 { 5529 struct scsi_format *cdb; 5530 int length, defect_list_len; 5531 5532 CTL_DEBUG_PRINT(("ctl_format\n")); 5533 5534 cdb = (struct scsi_format *)ctsio->cdb; 5535 5536 length = 0; 5537 if (cdb->byte2 & SF_FMTDATA) { 5538 if (cdb->byte2 & SF_LONGLIST) 5539 length = sizeof(struct scsi_format_header_long); 5540 else 5541 length = sizeof(struct scsi_format_header_short); 5542 } 5543 5544 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5545 && (length > 0)) { 5546 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5547 ctsio->kern_data_len = length; 5548 ctsio->kern_total_len = length; 5549 ctsio->kern_rel_offset = 0; 5550 ctsio->kern_sg_entries = 0; 5551 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5552 ctsio->be_move_done = ctl_config_move_done; 5553 ctl_datamove((union ctl_io *)ctsio); 5554 5555 return (CTL_RETVAL_COMPLETE); 5556 } 5557 5558 defect_list_len = 0; 5559 5560 if (cdb->byte2 & SF_FMTDATA) { 5561 if (cdb->byte2 & SF_LONGLIST) { 5562 struct scsi_format_header_long *header; 5563 5564 header = (struct scsi_format_header_long *) 5565 ctsio->kern_data_ptr; 5566 5567 defect_list_len = scsi_4btoul(header->defect_list_len); 5568 if (defect_list_len != 0) { 5569 ctl_set_invalid_field(ctsio, 5570 /*sks_valid*/ 1, 5571 /*command*/ 0, 5572 /*field*/ 2, 5573 /*bit_valid*/ 0, 5574 /*bit*/ 0); 5575 goto bailout; 5576 } 5577 } else { 5578 struct scsi_format_header_short *header; 5579 5580 header = (struct scsi_format_header_short *) 5581 ctsio->kern_data_ptr; 5582 5583 defect_list_len = scsi_2btoul(header->defect_list_len); 5584 if (defect_list_len != 0) { 5585 ctl_set_invalid_field(ctsio, 5586 /*sks_valid*/ 1, 5587 /*command*/ 0, 5588 /*field*/ 2, 5589 /*bit_valid*/ 0, 5590 /*bit*/ 0); 5591 goto bailout; 5592 } 5593 } 5594 } 5595 5596 ctl_set_success(ctsio); 5597 bailout: 5598 5599 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5600 free(ctsio->kern_data_ptr, M_CTL); 5601 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5602 } 5603 5604 ctl_done((union ctl_io *)ctsio); 5605 return (CTL_RETVAL_COMPLETE); 5606 } 5607 5608 int 5609 ctl_read_buffer(struct ctl_scsiio *ctsio) 5610 { 5611 struct ctl_lun *lun = CTL_LUN(ctsio); 5612 uint64_t buffer_offset; 5613 uint32_t len; 5614 uint8_t byte2; 5615 static uint8_t descr[4]; 5616 static uint8_t echo_descr[4] = { 0 }; 5617 5618 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5619 5620 switch (ctsio->cdb[0]) { 5621 case READ_BUFFER: { 5622 struct scsi_read_buffer *cdb; 5623 5624 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5625 buffer_offset = scsi_3btoul(cdb->offset); 5626 len = scsi_3btoul(cdb->length); 5627 byte2 = cdb->byte2; 5628 break; 5629 } 5630 case READ_BUFFER_16: { 5631 struct scsi_read_buffer_16 *cdb; 5632 5633 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5634 buffer_offset = scsi_8btou64(cdb->offset); 5635 len = scsi_4btoul(cdb->length); 5636 byte2 = cdb->byte2; 5637 break; 5638 } 5639 default: /* This shouldn't happen. */ 5640 ctl_set_invalid_opcode(ctsio); 5641 ctl_done((union ctl_io *)ctsio); 5642 return (CTL_RETVAL_COMPLETE); 5643 } 5644 5645 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5646 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5647 ctl_set_invalid_field(ctsio, 5648 /*sks_valid*/ 1, 5649 /*command*/ 1, 5650 /*field*/ 6, 5651 /*bit_valid*/ 0, 5652 /*bit*/ 0); 5653 ctl_done((union ctl_io *)ctsio); 5654 return (CTL_RETVAL_COMPLETE); 5655 } 5656 5657 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5658 descr[0] = 0; 5659 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5660 ctsio->kern_data_ptr = descr; 5661 len = min(len, sizeof(descr)); 5662 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5663 ctsio->kern_data_ptr = echo_descr; 5664 len = min(len, sizeof(echo_descr)); 5665 } else { 5666 if (lun->write_buffer == NULL) { 5667 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5668 M_CTL, M_WAITOK); 5669 } 5670 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5671 } 5672 ctsio->kern_data_len = len; 5673 ctsio->kern_total_len = len; 5674 ctsio->kern_rel_offset = 0; 5675 ctsio->kern_sg_entries = 0; 5676 ctl_set_success(ctsio); 5677 ctsio->be_move_done = ctl_config_move_done; 5678 ctl_datamove((union ctl_io *)ctsio); 5679 return (CTL_RETVAL_COMPLETE); 5680 } 5681 5682 int 5683 ctl_write_buffer(struct ctl_scsiio *ctsio) 5684 { 5685 struct ctl_lun *lun = CTL_LUN(ctsio); 5686 struct scsi_write_buffer *cdb; 5687 int buffer_offset, len; 5688 5689 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5690 5691 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5692 5693 len = scsi_3btoul(cdb->length); 5694 buffer_offset = scsi_3btoul(cdb->offset); 5695 5696 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5697 ctl_set_invalid_field(ctsio, 5698 /*sks_valid*/ 1, 5699 /*command*/ 1, 5700 /*field*/ 6, 5701 /*bit_valid*/ 0, 5702 /*bit*/ 0); 5703 ctl_done((union ctl_io *)ctsio); 5704 return (CTL_RETVAL_COMPLETE); 5705 } 5706 5707 /* 5708 * If we've got a kernel request that hasn't been malloced yet, 5709 * malloc it and tell the caller the data buffer is here. 5710 */ 5711 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5712 if (lun->write_buffer == NULL) { 5713 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5714 M_CTL, M_WAITOK); 5715 } 5716 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5717 ctsio->kern_data_len = len; 5718 ctsio->kern_total_len = len; 5719 ctsio->kern_rel_offset = 0; 5720 ctsio->kern_sg_entries = 0; 5721 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5722 ctsio->be_move_done = ctl_config_move_done; 5723 ctl_datamove((union ctl_io *)ctsio); 5724 5725 return (CTL_RETVAL_COMPLETE); 5726 } 5727 5728 ctl_set_success(ctsio); 5729 ctl_done((union ctl_io *)ctsio); 5730 return (CTL_RETVAL_COMPLETE); 5731 } 5732 5733 int 5734 ctl_write_same(struct ctl_scsiio *ctsio) 5735 { 5736 struct ctl_lun *lun = CTL_LUN(ctsio); 5737 struct ctl_lba_len_flags *lbalen; 5738 uint64_t lba; 5739 uint32_t num_blocks; 5740 int len, retval; 5741 uint8_t byte2; 5742 5743 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5744 5745 switch (ctsio->cdb[0]) { 5746 case WRITE_SAME_10: { 5747 struct scsi_write_same_10 *cdb; 5748 5749 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5750 5751 lba = scsi_4btoul(cdb->addr); 5752 num_blocks = scsi_2btoul(cdb->length); 5753 byte2 = cdb->byte2; 5754 break; 5755 } 5756 case WRITE_SAME_16: { 5757 struct scsi_write_same_16 *cdb; 5758 5759 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5760 5761 lba = scsi_8btou64(cdb->addr); 5762 num_blocks = scsi_4btoul(cdb->length); 5763 byte2 = cdb->byte2; 5764 break; 5765 } 5766 default: 5767 /* 5768 * We got a command we don't support. This shouldn't 5769 * happen, commands should be filtered out above us. 5770 */ 5771 ctl_set_invalid_opcode(ctsio); 5772 ctl_done((union ctl_io *)ctsio); 5773 5774 return (CTL_RETVAL_COMPLETE); 5775 break; /* NOTREACHED */ 5776 } 5777 5778 /* ANCHOR flag can be used only together with UNMAP */ 5779 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5780 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5781 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5782 ctl_done((union ctl_io *)ctsio); 5783 return (CTL_RETVAL_COMPLETE); 5784 } 5785 5786 /* 5787 * The first check is to make sure we're in bounds, the second 5788 * check is to catch wrap-around problems. If the lba + num blocks 5789 * is less than the lba, then we've wrapped around and the block 5790 * range is invalid anyway. 5791 */ 5792 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5793 || ((lba + num_blocks) < lba)) { 5794 ctl_set_lba_out_of_range(ctsio, 5795 MAX(lba, lun->be_lun->maxlba + 1)); 5796 ctl_done((union ctl_io *)ctsio); 5797 return (CTL_RETVAL_COMPLETE); 5798 } 5799 5800 /* Zero number of blocks means "to the last logical block" */ 5801 if (num_blocks == 0) { 5802 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5803 ctl_set_invalid_field(ctsio, 5804 /*sks_valid*/ 0, 5805 /*command*/ 1, 5806 /*field*/ 0, 5807 /*bit_valid*/ 0, 5808 /*bit*/ 0); 5809 ctl_done((union ctl_io *)ctsio); 5810 return (CTL_RETVAL_COMPLETE); 5811 } 5812 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5813 } 5814 5815 len = lun->be_lun->blocksize; 5816 5817 /* 5818 * If we've got a kernel request that hasn't been malloced yet, 5819 * malloc it and tell the caller the data buffer is here. 5820 */ 5821 if ((byte2 & SWS_NDOB) == 0 && 5822 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5823 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5824 ctsio->kern_data_len = len; 5825 ctsio->kern_total_len = len; 5826 ctsio->kern_rel_offset = 0; 5827 ctsio->kern_sg_entries = 0; 5828 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5829 ctsio->be_move_done = ctl_config_move_done; 5830 ctl_datamove((union ctl_io *)ctsio); 5831 5832 return (CTL_RETVAL_COMPLETE); 5833 } 5834 5835 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5836 lbalen->lba = lba; 5837 lbalen->len = num_blocks; 5838 lbalen->flags = byte2; 5839 retval = lun->backend->config_write((union ctl_io *)ctsio); 5840 5841 return (retval); 5842 } 5843 5844 int 5845 ctl_unmap(struct ctl_scsiio *ctsio) 5846 { 5847 struct ctl_lun *lun = CTL_LUN(ctsio); 5848 struct scsi_unmap *cdb; 5849 struct ctl_ptr_len_flags *ptrlen; 5850 struct scsi_unmap_header *hdr; 5851 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5852 uint64_t lba; 5853 uint32_t num_blocks; 5854 int len, retval; 5855 uint8_t byte2; 5856 5857 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5858 5859 cdb = (struct scsi_unmap *)ctsio->cdb; 5860 len = scsi_2btoul(cdb->length); 5861 byte2 = cdb->byte2; 5862 5863 /* 5864 * If we've got a kernel request that hasn't been malloced yet, 5865 * malloc it and tell the caller the data buffer is here. 5866 */ 5867 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5868 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5869 ctsio->kern_data_len = len; 5870 ctsio->kern_total_len = len; 5871 ctsio->kern_rel_offset = 0; 5872 ctsio->kern_sg_entries = 0; 5873 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5874 ctsio->be_move_done = ctl_config_move_done; 5875 ctl_datamove((union ctl_io *)ctsio); 5876 5877 return (CTL_RETVAL_COMPLETE); 5878 } 5879 5880 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5881 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5882 if (len < sizeof (*hdr) || 5883 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5884 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5885 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5886 ctl_set_invalid_field(ctsio, 5887 /*sks_valid*/ 0, 5888 /*command*/ 0, 5889 /*field*/ 0, 5890 /*bit_valid*/ 0, 5891 /*bit*/ 0); 5892 goto done; 5893 } 5894 len = scsi_2btoul(hdr->desc_length); 5895 buf = (struct scsi_unmap_desc *)(hdr + 1); 5896 end = buf + len / sizeof(*buf); 5897 5898 endnz = buf; 5899 for (range = buf; range < end; range++) { 5900 lba = scsi_8btou64(range->lba); 5901 num_blocks = scsi_4btoul(range->length); 5902 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5903 || ((lba + num_blocks) < lba)) { 5904 ctl_set_lba_out_of_range(ctsio, 5905 MAX(lba, lun->be_lun->maxlba + 1)); 5906 ctl_done((union ctl_io *)ctsio); 5907 return (CTL_RETVAL_COMPLETE); 5908 } 5909 if (num_blocks != 0) 5910 endnz = range + 1; 5911 } 5912 5913 /* 5914 * Block backend can not handle zero last range. 5915 * Filter it out and return if there is nothing left. 5916 */ 5917 len = (uint8_t *)endnz - (uint8_t *)buf; 5918 if (len == 0) { 5919 ctl_set_success(ctsio); 5920 goto done; 5921 } 5922 5923 mtx_lock(&lun->lun_lock); 5924 ptrlen = (struct ctl_ptr_len_flags *) 5925 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5926 ptrlen->ptr = (void *)buf; 5927 ptrlen->len = len; 5928 ptrlen->flags = byte2; 5929 ctl_check_blocked(lun); 5930 mtx_unlock(&lun->lun_lock); 5931 5932 retval = lun->backend->config_write((union ctl_io *)ctsio); 5933 return (retval); 5934 5935 done: 5936 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5937 free(ctsio->kern_data_ptr, M_CTL); 5938 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5939 } 5940 ctl_done((union ctl_io *)ctsio); 5941 return (CTL_RETVAL_COMPLETE); 5942 } 5943 5944 int 5945 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5946 struct ctl_page_index *page_index, uint8_t *page_ptr) 5947 { 5948 struct ctl_lun *lun = CTL_LUN(ctsio); 5949 uint8_t *current_cp; 5950 int set_ua; 5951 uint32_t initidx; 5952 5953 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5954 set_ua = 0; 5955 5956 current_cp = (page_index->page_data + (page_index->page_len * 5957 CTL_PAGE_CURRENT)); 5958 5959 mtx_lock(&lun->lun_lock); 5960 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5961 memcpy(current_cp, page_ptr, page_index->page_len); 5962 set_ua = 1; 5963 } 5964 if (set_ua != 0) 5965 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5966 mtx_unlock(&lun->lun_lock); 5967 if (set_ua) { 5968 ctl_isc_announce_mode(lun, 5969 ctl_get_initindex(&ctsio->io_hdr.nexus), 5970 page_index->page_code, page_index->subpage); 5971 } 5972 return (CTL_RETVAL_COMPLETE); 5973 } 5974 5975 static void 5976 ctl_ie_timer(void *arg) 5977 { 5978 struct ctl_lun *lun = arg; 5979 uint64_t t; 5980 5981 if (lun->ie_asc == 0) 5982 return; 5983 5984 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5985 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5986 else 5987 lun->ie_reported = 0; 5988 5989 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5990 lun->ie_reportcnt++; 5991 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5992 if (t == 0 || t == UINT32_MAX) 5993 t = 3000; /* 5 min */ 5994 callout_schedule(&lun->ie_callout, t * hz / 10); 5995 } 5996 } 5997 5998 int 5999 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 6000 struct ctl_page_index *page_index, uint8_t *page_ptr) 6001 { 6002 struct ctl_lun *lun = CTL_LUN(ctsio); 6003 struct scsi_info_exceptions_page *pg; 6004 uint64_t t; 6005 6006 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 6007 6008 pg = (struct scsi_info_exceptions_page *)page_ptr; 6009 mtx_lock(&lun->lun_lock); 6010 if (pg->info_flags & SIEP_FLAGS_TEST) { 6011 lun->ie_asc = 0x5d; 6012 lun->ie_ascq = 0xff; 6013 if (pg->mrie == SIEP_MRIE_UA) { 6014 ctl_est_ua_all(lun, -1, CTL_UA_IE); 6015 lun->ie_reported = 1; 6016 } else { 6017 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 6018 lun->ie_reported = -1; 6019 } 6020 lun->ie_reportcnt = 1; 6021 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 6022 lun->ie_reportcnt++; 6023 t = scsi_4btoul(pg->interval_timer); 6024 if (t == 0 || t == UINT32_MAX) 6025 t = 3000; /* 5 min */ 6026 callout_reset(&lun->ie_callout, t * hz / 10, 6027 ctl_ie_timer, lun); 6028 } 6029 } else { 6030 lun->ie_asc = 0; 6031 lun->ie_ascq = 0; 6032 lun->ie_reported = 1; 6033 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 6034 lun->ie_reportcnt = UINT32_MAX; 6035 callout_stop(&lun->ie_callout); 6036 } 6037 mtx_unlock(&lun->lun_lock); 6038 return (CTL_RETVAL_COMPLETE); 6039 } 6040 6041 static int 6042 ctl_do_mode_select(union ctl_io *io) 6043 { 6044 struct ctl_lun *lun = CTL_LUN(io); 6045 struct scsi_mode_page_header *page_header; 6046 struct ctl_page_index *page_index; 6047 struct ctl_scsiio *ctsio; 6048 int page_len, page_len_offset, page_len_size; 6049 union ctl_modepage_info *modepage_info; 6050 uint16_t *len_left, *len_used; 6051 int retval, i; 6052 6053 ctsio = &io->scsiio; 6054 page_index = NULL; 6055 page_len = 0; 6056 6057 modepage_info = (union ctl_modepage_info *) 6058 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6059 len_left = &modepage_info->header.len_left; 6060 len_used = &modepage_info->header.len_used; 6061 6062 do_next_page: 6063 6064 page_header = (struct scsi_mode_page_header *) 6065 (ctsio->kern_data_ptr + *len_used); 6066 6067 if (*len_left == 0) { 6068 free(ctsio->kern_data_ptr, M_CTL); 6069 ctl_set_success(ctsio); 6070 ctl_done((union ctl_io *)ctsio); 6071 return (CTL_RETVAL_COMPLETE); 6072 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6073 6074 free(ctsio->kern_data_ptr, M_CTL); 6075 ctl_set_param_len_error(ctsio); 6076 ctl_done((union ctl_io *)ctsio); 6077 return (CTL_RETVAL_COMPLETE); 6078 6079 } else if ((page_header->page_code & SMPH_SPF) 6080 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6081 6082 free(ctsio->kern_data_ptr, M_CTL); 6083 ctl_set_param_len_error(ctsio); 6084 ctl_done((union ctl_io *)ctsio); 6085 return (CTL_RETVAL_COMPLETE); 6086 } 6087 6088 6089 /* 6090 * XXX KDM should we do something with the block descriptor? 6091 */ 6092 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6093 page_index = &lun->mode_pages.index[i]; 6094 if (lun->be_lun->lun_type == T_DIRECT && 6095 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6096 continue; 6097 if (lun->be_lun->lun_type == T_PROCESSOR && 6098 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6099 continue; 6100 if (lun->be_lun->lun_type == T_CDROM && 6101 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6102 continue; 6103 6104 if ((page_index->page_code & SMPH_PC_MASK) != 6105 (page_header->page_code & SMPH_PC_MASK)) 6106 continue; 6107 6108 /* 6109 * If neither page has a subpage code, then we've got a 6110 * match. 6111 */ 6112 if (((page_index->page_code & SMPH_SPF) == 0) 6113 && ((page_header->page_code & SMPH_SPF) == 0)) { 6114 page_len = page_header->page_length; 6115 break; 6116 } 6117 6118 /* 6119 * If both pages have subpages, then the subpage numbers 6120 * have to match. 6121 */ 6122 if ((page_index->page_code & SMPH_SPF) 6123 && (page_header->page_code & SMPH_SPF)) { 6124 struct scsi_mode_page_header_sp *sph; 6125 6126 sph = (struct scsi_mode_page_header_sp *)page_header; 6127 if (page_index->subpage == sph->subpage) { 6128 page_len = scsi_2btoul(sph->page_length); 6129 break; 6130 } 6131 } 6132 } 6133 6134 /* 6135 * If we couldn't find the page, or if we don't have a mode select 6136 * handler for it, send back an error to the user. 6137 */ 6138 if ((i >= CTL_NUM_MODE_PAGES) 6139 || (page_index->select_handler == NULL)) { 6140 ctl_set_invalid_field(ctsio, 6141 /*sks_valid*/ 1, 6142 /*command*/ 0, 6143 /*field*/ *len_used, 6144 /*bit_valid*/ 0, 6145 /*bit*/ 0); 6146 free(ctsio->kern_data_ptr, M_CTL); 6147 ctl_done((union ctl_io *)ctsio); 6148 return (CTL_RETVAL_COMPLETE); 6149 } 6150 6151 if (page_index->page_code & SMPH_SPF) { 6152 page_len_offset = 2; 6153 page_len_size = 2; 6154 } else { 6155 page_len_size = 1; 6156 page_len_offset = 1; 6157 } 6158 6159 /* 6160 * If the length the initiator gives us isn't the one we specify in 6161 * the mode page header, or if they didn't specify enough data in 6162 * the CDB to avoid truncating this page, kick out the request. 6163 */ 6164 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6165 ctl_set_invalid_field(ctsio, 6166 /*sks_valid*/ 1, 6167 /*command*/ 0, 6168 /*field*/ *len_used + page_len_offset, 6169 /*bit_valid*/ 0, 6170 /*bit*/ 0); 6171 free(ctsio->kern_data_ptr, M_CTL); 6172 ctl_done((union ctl_io *)ctsio); 6173 return (CTL_RETVAL_COMPLETE); 6174 } 6175 if (*len_left < page_index->page_len) { 6176 free(ctsio->kern_data_ptr, M_CTL); 6177 ctl_set_param_len_error(ctsio); 6178 ctl_done((union ctl_io *)ctsio); 6179 return (CTL_RETVAL_COMPLETE); 6180 } 6181 6182 /* 6183 * Run through the mode page, checking to make sure that the bits 6184 * the user changed are actually legal for him to change. 6185 */ 6186 for (i = 0; i < page_index->page_len; i++) { 6187 uint8_t *user_byte, *change_mask, *current_byte; 6188 int bad_bit; 6189 int j; 6190 6191 user_byte = (uint8_t *)page_header + i; 6192 change_mask = page_index->page_data + 6193 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6194 current_byte = page_index->page_data + 6195 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6196 6197 /* 6198 * Check to see whether the user set any bits in this byte 6199 * that he is not allowed to set. 6200 */ 6201 if ((*user_byte & ~(*change_mask)) == 6202 (*current_byte & ~(*change_mask))) 6203 continue; 6204 6205 /* 6206 * Go through bit by bit to determine which one is illegal. 6207 */ 6208 bad_bit = 0; 6209 for (j = 7; j >= 0; j--) { 6210 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6211 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6212 bad_bit = i; 6213 break; 6214 } 6215 } 6216 ctl_set_invalid_field(ctsio, 6217 /*sks_valid*/ 1, 6218 /*command*/ 0, 6219 /*field*/ *len_used + i, 6220 /*bit_valid*/ 1, 6221 /*bit*/ bad_bit); 6222 free(ctsio->kern_data_ptr, M_CTL); 6223 ctl_done((union ctl_io *)ctsio); 6224 return (CTL_RETVAL_COMPLETE); 6225 } 6226 6227 /* 6228 * Decrement these before we call the page handler, since we may 6229 * end up getting called back one way or another before the handler 6230 * returns to this context. 6231 */ 6232 *len_left -= page_index->page_len; 6233 *len_used += page_index->page_len; 6234 6235 retval = page_index->select_handler(ctsio, page_index, 6236 (uint8_t *)page_header); 6237 6238 /* 6239 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6240 * wait until this queued command completes to finish processing 6241 * the mode page. If it returns anything other than 6242 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6243 * already set the sense information, freed the data pointer, and 6244 * completed the io for us. 6245 */ 6246 if (retval != CTL_RETVAL_COMPLETE) 6247 goto bailout_no_done; 6248 6249 /* 6250 * If the initiator sent us more than one page, parse the next one. 6251 */ 6252 if (*len_left > 0) 6253 goto do_next_page; 6254 6255 ctl_set_success(ctsio); 6256 free(ctsio->kern_data_ptr, M_CTL); 6257 ctl_done((union ctl_io *)ctsio); 6258 6259 bailout_no_done: 6260 6261 return (CTL_RETVAL_COMPLETE); 6262 6263 } 6264 6265 int 6266 ctl_mode_select(struct ctl_scsiio *ctsio) 6267 { 6268 struct ctl_lun *lun = CTL_LUN(ctsio); 6269 union ctl_modepage_info *modepage_info; 6270 int bd_len, i, header_size, param_len, rtd; 6271 uint32_t initidx; 6272 6273 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6274 switch (ctsio->cdb[0]) { 6275 case MODE_SELECT_6: { 6276 struct scsi_mode_select_6 *cdb; 6277 6278 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6279 6280 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6281 param_len = cdb->length; 6282 header_size = sizeof(struct scsi_mode_header_6); 6283 break; 6284 } 6285 case MODE_SELECT_10: { 6286 struct scsi_mode_select_10 *cdb; 6287 6288 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6289 6290 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6291 param_len = scsi_2btoul(cdb->length); 6292 header_size = sizeof(struct scsi_mode_header_10); 6293 break; 6294 } 6295 default: 6296 ctl_set_invalid_opcode(ctsio); 6297 ctl_done((union ctl_io *)ctsio); 6298 return (CTL_RETVAL_COMPLETE); 6299 } 6300 6301 if (rtd) { 6302 if (param_len != 0) { 6303 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6304 /*command*/ 1, /*field*/ 0, 6305 /*bit_valid*/ 0, /*bit*/ 0); 6306 ctl_done((union ctl_io *)ctsio); 6307 return (CTL_RETVAL_COMPLETE); 6308 } 6309 6310 /* Revert to defaults. */ 6311 ctl_init_page_index(lun); 6312 mtx_lock(&lun->lun_lock); 6313 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6314 mtx_unlock(&lun->lun_lock); 6315 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6316 ctl_isc_announce_mode(lun, -1, 6317 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6318 lun->mode_pages.index[i].subpage); 6319 } 6320 ctl_set_success(ctsio); 6321 ctl_done((union ctl_io *)ctsio); 6322 return (CTL_RETVAL_COMPLETE); 6323 } 6324 6325 /* 6326 * From SPC-3: 6327 * "A parameter list length of zero indicates that the Data-Out Buffer 6328 * shall be empty. This condition shall not be considered as an error." 6329 */ 6330 if (param_len == 0) { 6331 ctl_set_success(ctsio); 6332 ctl_done((union ctl_io *)ctsio); 6333 return (CTL_RETVAL_COMPLETE); 6334 } 6335 6336 /* 6337 * Since we'll hit this the first time through, prior to 6338 * allocation, we don't need to free a data buffer here. 6339 */ 6340 if (param_len < header_size) { 6341 ctl_set_param_len_error(ctsio); 6342 ctl_done((union ctl_io *)ctsio); 6343 return (CTL_RETVAL_COMPLETE); 6344 } 6345 6346 /* 6347 * Allocate the data buffer and grab the user's data. In theory, 6348 * we shouldn't have to sanity check the parameter list length here 6349 * because the maximum size is 64K. We should be able to malloc 6350 * that much without too many problems. 6351 */ 6352 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6353 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6354 ctsio->kern_data_len = param_len; 6355 ctsio->kern_total_len = param_len; 6356 ctsio->kern_rel_offset = 0; 6357 ctsio->kern_sg_entries = 0; 6358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6359 ctsio->be_move_done = ctl_config_move_done; 6360 ctl_datamove((union ctl_io *)ctsio); 6361 6362 return (CTL_RETVAL_COMPLETE); 6363 } 6364 6365 switch (ctsio->cdb[0]) { 6366 case MODE_SELECT_6: { 6367 struct scsi_mode_header_6 *mh6; 6368 6369 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6370 bd_len = mh6->blk_desc_len; 6371 break; 6372 } 6373 case MODE_SELECT_10: { 6374 struct scsi_mode_header_10 *mh10; 6375 6376 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6377 bd_len = scsi_2btoul(mh10->blk_desc_len); 6378 break; 6379 } 6380 default: 6381 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6382 } 6383 6384 if (param_len < (header_size + bd_len)) { 6385 free(ctsio->kern_data_ptr, M_CTL); 6386 ctl_set_param_len_error(ctsio); 6387 ctl_done((union ctl_io *)ctsio); 6388 return (CTL_RETVAL_COMPLETE); 6389 } 6390 6391 /* 6392 * Set the IO_CONT flag, so that if this I/O gets passed to 6393 * ctl_config_write_done(), it'll get passed back to 6394 * ctl_do_mode_select() for further processing, or completion if 6395 * we're all done. 6396 */ 6397 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6398 ctsio->io_cont = ctl_do_mode_select; 6399 6400 modepage_info = (union ctl_modepage_info *) 6401 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6402 memset(modepage_info, 0, sizeof(*modepage_info)); 6403 modepage_info->header.len_left = param_len - header_size - bd_len; 6404 modepage_info->header.len_used = header_size + bd_len; 6405 6406 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6407 } 6408 6409 int 6410 ctl_mode_sense(struct ctl_scsiio *ctsio) 6411 { 6412 struct ctl_lun *lun = CTL_LUN(ctsio); 6413 int pc, page_code, dbd, subpage; 6414 int alloc_len, page_len, header_len, total_len; 6415 struct scsi_mode_block_descr *block_desc; 6416 struct ctl_page_index *page_index; 6417 6418 dbd = 0; 6419 block_desc = NULL; 6420 6421 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6422 6423 switch (ctsio->cdb[0]) { 6424 case MODE_SENSE_6: { 6425 struct scsi_mode_sense_6 *cdb; 6426 6427 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6428 6429 header_len = sizeof(struct scsi_mode_hdr_6); 6430 if (cdb->byte2 & SMS_DBD) 6431 dbd = 1; 6432 else 6433 header_len += sizeof(struct scsi_mode_block_descr); 6434 6435 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6436 page_code = cdb->page & SMS_PAGE_CODE; 6437 subpage = cdb->subpage; 6438 alloc_len = cdb->length; 6439 break; 6440 } 6441 case MODE_SENSE_10: { 6442 struct scsi_mode_sense_10 *cdb; 6443 6444 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6445 6446 header_len = sizeof(struct scsi_mode_hdr_10); 6447 6448 if (cdb->byte2 & SMS_DBD) 6449 dbd = 1; 6450 else 6451 header_len += sizeof(struct scsi_mode_block_descr); 6452 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6453 page_code = cdb->page & SMS_PAGE_CODE; 6454 subpage = cdb->subpage; 6455 alloc_len = scsi_2btoul(cdb->length); 6456 break; 6457 } 6458 default: 6459 ctl_set_invalid_opcode(ctsio); 6460 ctl_done((union ctl_io *)ctsio); 6461 return (CTL_RETVAL_COMPLETE); 6462 break; /* NOTREACHED */ 6463 } 6464 6465 /* 6466 * We have to make a first pass through to calculate the size of 6467 * the pages that match the user's query. Then we allocate enough 6468 * memory to hold it, and actually copy the data into the buffer. 6469 */ 6470 switch (page_code) { 6471 case SMS_ALL_PAGES_PAGE: { 6472 u_int i; 6473 6474 page_len = 0; 6475 6476 /* 6477 * At the moment, values other than 0 and 0xff here are 6478 * reserved according to SPC-3. 6479 */ 6480 if ((subpage != SMS_SUBPAGE_PAGE_0) 6481 && (subpage != SMS_SUBPAGE_ALL)) { 6482 ctl_set_invalid_field(ctsio, 6483 /*sks_valid*/ 1, 6484 /*command*/ 1, 6485 /*field*/ 3, 6486 /*bit_valid*/ 0, 6487 /*bit*/ 0); 6488 ctl_done((union ctl_io *)ctsio); 6489 return (CTL_RETVAL_COMPLETE); 6490 } 6491 6492 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6493 page_index = &lun->mode_pages.index[i]; 6494 6495 /* Make sure the page is supported for this dev type */ 6496 if (lun->be_lun->lun_type == T_DIRECT && 6497 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6498 continue; 6499 if (lun->be_lun->lun_type == T_PROCESSOR && 6500 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6501 continue; 6502 if (lun->be_lun->lun_type == T_CDROM && 6503 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6504 continue; 6505 6506 /* 6507 * We don't use this subpage if the user didn't 6508 * request all subpages. 6509 */ 6510 if ((page_index->subpage != 0) 6511 && (subpage == SMS_SUBPAGE_PAGE_0)) 6512 continue; 6513 6514 #if 0 6515 printf("found page %#x len %d\n", 6516 page_index->page_code & SMPH_PC_MASK, 6517 page_index->page_len); 6518 #endif 6519 page_len += page_index->page_len; 6520 } 6521 break; 6522 } 6523 default: { 6524 u_int i; 6525 6526 page_len = 0; 6527 6528 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6529 page_index = &lun->mode_pages.index[i]; 6530 6531 /* Make sure the page is supported for this dev type */ 6532 if (lun->be_lun->lun_type == T_DIRECT && 6533 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6534 continue; 6535 if (lun->be_lun->lun_type == T_PROCESSOR && 6536 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6537 continue; 6538 if (lun->be_lun->lun_type == T_CDROM && 6539 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6540 continue; 6541 6542 /* Look for the right page code */ 6543 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6544 continue; 6545 6546 /* Look for the right subpage or the subpage wildcard*/ 6547 if ((page_index->subpage != subpage) 6548 && (subpage != SMS_SUBPAGE_ALL)) 6549 continue; 6550 6551 #if 0 6552 printf("found page %#x len %d\n", 6553 page_index->page_code & SMPH_PC_MASK, 6554 page_index->page_len); 6555 #endif 6556 6557 page_len += page_index->page_len; 6558 } 6559 6560 if (page_len == 0) { 6561 ctl_set_invalid_field(ctsio, 6562 /*sks_valid*/ 1, 6563 /*command*/ 1, 6564 /*field*/ 2, 6565 /*bit_valid*/ 1, 6566 /*bit*/ 5); 6567 ctl_done((union ctl_io *)ctsio); 6568 return (CTL_RETVAL_COMPLETE); 6569 } 6570 break; 6571 } 6572 } 6573 6574 total_len = header_len + page_len; 6575 #if 0 6576 printf("header_len = %d, page_len = %d, total_len = %d\n", 6577 header_len, page_len, total_len); 6578 #endif 6579 6580 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6581 ctsio->kern_sg_entries = 0; 6582 ctsio->kern_rel_offset = 0; 6583 ctsio->kern_data_len = min(total_len, alloc_len); 6584 ctsio->kern_total_len = ctsio->kern_data_len; 6585 6586 switch (ctsio->cdb[0]) { 6587 case MODE_SENSE_6: { 6588 struct scsi_mode_hdr_6 *header; 6589 6590 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6591 6592 header->datalen = MIN(total_len - 1, 254); 6593 if (lun->be_lun->lun_type == T_DIRECT) { 6594 header->dev_specific = 0x10; /* DPOFUA */ 6595 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6596 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6597 header->dev_specific |= 0x80; /* WP */ 6598 } 6599 if (dbd) 6600 header->block_descr_len = 0; 6601 else 6602 header->block_descr_len = 6603 sizeof(struct scsi_mode_block_descr); 6604 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6605 break; 6606 } 6607 case MODE_SENSE_10: { 6608 struct scsi_mode_hdr_10 *header; 6609 int datalen; 6610 6611 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6612 6613 datalen = MIN(total_len - 2, 65533); 6614 scsi_ulto2b(datalen, header->datalen); 6615 if (lun->be_lun->lun_type == T_DIRECT) { 6616 header->dev_specific = 0x10; /* DPOFUA */ 6617 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6618 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6619 header->dev_specific |= 0x80; /* WP */ 6620 } 6621 if (dbd) 6622 scsi_ulto2b(0, header->block_descr_len); 6623 else 6624 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6625 header->block_descr_len); 6626 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6627 break; 6628 } 6629 default: 6630 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6631 } 6632 6633 /* 6634 * If we've got a disk, use its blocksize in the block 6635 * descriptor. Otherwise, just set it to 0. 6636 */ 6637 if (dbd == 0) { 6638 if (lun->be_lun->lun_type == T_DIRECT) 6639 scsi_ulto3b(lun->be_lun->blocksize, 6640 block_desc->block_len); 6641 else 6642 scsi_ulto3b(0, block_desc->block_len); 6643 } 6644 6645 switch (page_code) { 6646 case SMS_ALL_PAGES_PAGE: { 6647 int i, data_used; 6648 6649 data_used = header_len; 6650 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6651 struct ctl_page_index *page_index; 6652 6653 page_index = &lun->mode_pages.index[i]; 6654 if (lun->be_lun->lun_type == T_DIRECT && 6655 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6656 continue; 6657 if (lun->be_lun->lun_type == T_PROCESSOR && 6658 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6659 continue; 6660 if (lun->be_lun->lun_type == T_CDROM && 6661 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6662 continue; 6663 6664 /* 6665 * We don't use this subpage if the user didn't 6666 * request all subpages. We already checked (above) 6667 * to make sure the user only specified a subpage 6668 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6669 */ 6670 if ((page_index->subpage != 0) 6671 && (subpage == SMS_SUBPAGE_PAGE_0)) 6672 continue; 6673 6674 /* 6675 * Call the handler, if it exists, to update the 6676 * page to the latest values. 6677 */ 6678 if (page_index->sense_handler != NULL) 6679 page_index->sense_handler(ctsio, page_index,pc); 6680 6681 memcpy(ctsio->kern_data_ptr + data_used, 6682 page_index->page_data + 6683 (page_index->page_len * pc), 6684 page_index->page_len); 6685 data_used += page_index->page_len; 6686 } 6687 break; 6688 } 6689 default: { 6690 int i, data_used; 6691 6692 data_used = header_len; 6693 6694 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6695 struct ctl_page_index *page_index; 6696 6697 page_index = &lun->mode_pages.index[i]; 6698 6699 /* Look for the right page code */ 6700 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6701 continue; 6702 6703 /* Look for the right subpage or the subpage wildcard*/ 6704 if ((page_index->subpage != subpage) 6705 && (subpage != SMS_SUBPAGE_ALL)) 6706 continue; 6707 6708 /* Make sure the page is supported for this dev type */ 6709 if (lun->be_lun->lun_type == T_DIRECT && 6710 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6711 continue; 6712 if (lun->be_lun->lun_type == T_PROCESSOR && 6713 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6714 continue; 6715 if (lun->be_lun->lun_type == T_CDROM && 6716 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6717 continue; 6718 6719 /* 6720 * Call the handler, if it exists, to update the 6721 * page to the latest values. 6722 */ 6723 if (page_index->sense_handler != NULL) 6724 page_index->sense_handler(ctsio, page_index,pc); 6725 6726 memcpy(ctsio->kern_data_ptr + data_used, 6727 page_index->page_data + 6728 (page_index->page_len * pc), 6729 page_index->page_len); 6730 data_used += page_index->page_len; 6731 } 6732 break; 6733 } 6734 } 6735 6736 ctl_set_success(ctsio); 6737 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6738 ctsio->be_move_done = ctl_config_move_done; 6739 ctl_datamove((union ctl_io *)ctsio); 6740 return (CTL_RETVAL_COMPLETE); 6741 } 6742 6743 int 6744 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6745 struct ctl_page_index *page_index, 6746 int pc) 6747 { 6748 struct ctl_lun *lun = CTL_LUN(ctsio); 6749 struct scsi_log_param_header *phdr; 6750 uint8_t *data; 6751 uint64_t val; 6752 6753 data = page_index->page_data; 6754 6755 if (lun->backend->lun_attr != NULL && 6756 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6757 != UINT64_MAX) { 6758 phdr = (struct scsi_log_param_header *)data; 6759 scsi_ulto2b(0x0001, phdr->param_code); 6760 phdr->param_control = SLP_LBIN | SLP_LP; 6761 phdr->param_len = 8; 6762 data = (uint8_t *)(phdr + 1); 6763 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6764 data[4] = 0x02; /* per-pool */ 6765 data += phdr->param_len; 6766 } 6767 6768 if (lun->backend->lun_attr != NULL && 6769 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6770 != UINT64_MAX) { 6771 phdr = (struct scsi_log_param_header *)data; 6772 scsi_ulto2b(0x0002, phdr->param_code); 6773 phdr->param_control = SLP_LBIN | SLP_LP; 6774 phdr->param_len = 8; 6775 data = (uint8_t *)(phdr + 1); 6776 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6777 data[4] = 0x01; /* per-LUN */ 6778 data += phdr->param_len; 6779 } 6780 6781 if (lun->backend->lun_attr != NULL && 6782 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6783 != UINT64_MAX) { 6784 phdr = (struct scsi_log_param_header *)data; 6785 scsi_ulto2b(0x00f1, phdr->param_code); 6786 phdr->param_control = SLP_LBIN | SLP_LP; 6787 phdr->param_len = 8; 6788 data = (uint8_t *)(phdr + 1); 6789 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6790 data[4] = 0x02; /* per-pool */ 6791 data += phdr->param_len; 6792 } 6793 6794 if (lun->backend->lun_attr != NULL && 6795 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6796 != UINT64_MAX) { 6797 phdr = (struct scsi_log_param_header *)data; 6798 scsi_ulto2b(0x00f2, phdr->param_code); 6799 phdr->param_control = SLP_LBIN | SLP_LP; 6800 phdr->param_len = 8; 6801 data = (uint8_t *)(phdr + 1); 6802 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6803 data[4] = 0x02; /* per-pool */ 6804 data += phdr->param_len; 6805 } 6806 6807 page_index->page_len = data - page_index->page_data; 6808 return (0); 6809 } 6810 6811 int 6812 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6813 struct ctl_page_index *page_index, 6814 int pc) 6815 { 6816 struct ctl_lun *lun = CTL_LUN(ctsio); 6817 struct stat_page *data; 6818 struct bintime *t; 6819 6820 data = (struct stat_page *)page_index->page_data; 6821 6822 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6823 data->sap.hdr.param_control = SLP_LBIN; 6824 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6825 sizeof(struct scsi_log_param_header); 6826 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6827 data->sap.read_num); 6828 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6829 data->sap.write_num); 6830 if (lun->be_lun->blocksize > 0) { 6831 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6832 lun->be_lun->blocksize, data->sap.recvieved_lba); 6833 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6834 lun->be_lun->blocksize, data->sap.transmitted_lba); 6835 } 6836 t = &lun->stats.time[CTL_STATS_READ]; 6837 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6838 data->sap.read_int); 6839 t = &lun->stats.time[CTL_STATS_WRITE]; 6840 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6841 data->sap.write_int); 6842 scsi_u64to8b(0, data->sap.weighted_num); 6843 scsi_u64to8b(0, data->sap.weighted_int); 6844 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6845 data->it.hdr.param_control = SLP_LBIN; 6846 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6847 sizeof(struct scsi_log_param_header); 6848 #ifdef CTL_TIME_IO 6849 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6850 #endif 6851 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6852 data->it.hdr.param_control = SLP_LBIN; 6853 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6854 sizeof(struct scsi_log_param_header); 6855 scsi_ulto4b(3, data->ti.exponent); 6856 scsi_ulto4b(1, data->ti.integer); 6857 return (0); 6858 } 6859 6860 int 6861 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6862 struct ctl_page_index *page_index, 6863 int pc) 6864 { 6865 struct ctl_lun *lun = CTL_LUN(ctsio); 6866 struct scsi_log_informational_exceptions *data; 6867 6868 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6869 6870 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6871 data->hdr.param_control = SLP_LBIN; 6872 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6873 sizeof(struct scsi_log_param_header); 6874 data->ie_asc = lun->ie_asc; 6875 data->ie_ascq = lun->ie_ascq; 6876 data->temperature = 0xff; 6877 return (0); 6878 } 6879 6880 int 6881 ctl_log_sense(struct ctl_scsiio *ctsio) 6882 { 6883 struct ctl_lun *lun = CTL_LUN(ctsio); 6884 int i, pc, page_code, subpage; 6885 int alloc_len, total_len; 6886 struct ctl_page_index *page_index; 6887 struct scsi_log_sense *cdb; 6888 struct scsi_log_header *header; 6889 6890 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6891 6892 cdb = (struct scsi_log_sense *)ctsio->cdb; 6893 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6894 page_code = cdb->page & SLS_PAGE_CODE; 6895 subpage = cdb->subpage; 6896 alloc_len = scsi_2btoul(cdb->length); 6897 6898 page_index = NULL; 6899 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6900 page_index = &lun->log_pages.index[i]; 6901 6902 /* Look for the right page code */ 6903 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6904 continue; 6905 6906 /* Look for the right subpage or the subpage wildcard*/ 6907 if (page_index->subpage != subpage) 6908 continue; 6909 6910 break; 6911 } 6912 if (i >= CTL_NUM_LOG_PAGES) { 6913 ctl_set_invalid_field(ctsio, 6914 /*sks_valid*/ 1, 6915 /*command*/ 1, 6916 /*field*/ 2, 6917 /*bit_valid*/ 0, 6918 /*bit*/ 0); 6919 ctl_done((union ctl_io *)ctsio); 6920 return (CTL_RETVAL_COMPLETE); 6921 } 6922 6923 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6924 6925 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6926 ctsio->kern_sg_entries = 0; 6927 ctsio->kern_rel_offset = 0; 6928 ctsio->kern_data_len = min(total_len, alloc_len); 6929 ctsio->kern_total_len = ctsio->kern_data_len; 6930 6931 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6932 header->page = page_index->page_code; 6933 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6934 header->page |= SL_DS; 6935 if (page_index->subpage) { 6936 header->page |= SL_SPF; 6937 header->subpage = page_index->subpage; 6938 } 6939 scsi_ulto2b(page_index->page_len, header->datalen); 6940 6941 /* 6942 * Call the handler, if it exists, to update the 6943 * page to the latest values. 6944 */ 6945 if (page_index->sense_handler != NULL) 6946 page_index->sense_handler(ctsio, page_index, pc); 6947 6948 memcpy(header + 1, page_index->page_data, page_index->page_len); 6949 6950 ctl_set_success(ctsio); 6951 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6952 ctsio->be_move_done = ctl_config_move_done; 6953 ctl_datamove((union ctl_io *)ctsio); 6954 return (CTL_RETVAL_COMPLETE); 6955 } 6956 6957 int 6958 ctl_read_capacity(struct ctl_scsiio *ctsio) 6959 { 6960 struct ctl_lun *lun = CTL_LUN(ctsio); 6961 struct scsi_read_capacity *cdb; 6962 struct scsi_read_capacity_data *data; 6963 uint32_t lba; 6964 6965 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6966 6967 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6968 6969 lba = scsi_4btoul(cdb->addr); 6970 if (((cdb->pmi & SRC_PMI) == 0) 6971 && (lba != 0)) { 6972 ctl_set_invalid_field(/*ctsio*/ ctsio, 6973 /*sks_valid*/ 1, 6974 /*command*/ 1, 6975 /*field*/ 2, 6976 /*bit_valid*/ 0, 6977 /*bit*/ 0); 6978 ctl_done((union ctl_io *)ctsio); 6979 return (CTL_RETVAL_COMPLETE); 6980 } 6981 6982 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6983 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6984 ctsio->kern_data_len = sizeof(*data); 6985 ctsio->kern_total_len = sizeof(*data); 6986 ctsio->kern_rel_offset = 0; 6987 ctsio->kern_sg_entries = 0; 6988 6989 /* 6990 * If the maximum LBA is greater than 0xfffffffe, the user must 6991 * issue a SERVICE ACTION IN (16) command, with the read capacity 6992 * serivce action set. 6993 */ 6994 if (lun->be_lun->maxlba > 0xfffffffe) 6995 scsi_ulto4b(0xffffffff, data->addr); 6996 else 6997 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6998 6999 /* 7000 * XXX KDM this may not be 512 bytes... 7001 */ 7002 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7003 7004 ctl_set_success(ctsio); 7005 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7006 ctsio->be_move_done = ctl_config_move_done; 7007 ctl_datamove((union ctl_io *)ctsio); 7008 return (CTL_RETVAL_COMPLETE); 7009 } 7010 7011 int 7012 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7013 { 7014 struct ctl_lun *lun = CTL_LUN(ctsio); 7015 struct scsi_read_capacity_16 *cdb; 7016 struct scsi_read_capacity_data_long *data; 7017 uint64_t lba; 7018 uint32_t alloc_len; 7019 7020 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7021 7022 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7023 7024 alloc_len = scsi_4btoul(cdb->alloc_len); 7025 lba = scsi_8btou64(cdb->addr); 7026 7027 if ((cdb->reladr & SRC16_PMI) 7028 && (lba != 0)) { 7029 ctl_set_invalid_field(/*ctsio*/ ctsio, 7030 /*sks_valid*/ 1, 7031 /*command*/ 1, 7032 /*field*/ 2, 7033 /*bit_valid*/ 0, 7034 /*bit*/ 0); 7035 ctl_done((union ctl_io *)ctsio); 7036 return (CTL_RETVAL_COMPLETE); 7037 } 7038 7039 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7040 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7041 ctsio->kern_rel_offset = 0; 7042 ctsio->kern_sg_entries = 0; 7043 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 7044 ctsio->kern_total_len = ctsio->kern_data_len; 7045 7046 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7047 /* XXX KDM this may not be 512 bytes... */ 7048 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7049 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7050 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7051 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7052 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7053 7054 ctl_set_success(ctsio); 7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7056 ctsio->be_move_done = ctl_config_move_done; 7057 ctl_datamove((union ctl_io *)ctsio); 7058 return (CTL_RETVAL_COMPLETE); 7059 } 7060 7061 int 7062 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7063 { 7064 struct ctl_lun *lun = CTL_LUN(ctsio); 7065 struct scsi_get_lba_status *cdb; 7066 struct scsi_get_lba_status_data *data; 7067 struct ctl_lba_len_flags *lbalen; 7068 uint64_t lba; 7069 uint32_t alloc_len, total_len; 7070 int retval; 7071 7072 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7073 7074 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7075 lba = scsi_8btou64(cdb->addr); 7076 alloc_len = scsi_4btoul(cdb->alloc_len); 7077 7078 if (lba > lun->be_lun->maxlba) { 7079 ctl_set_lba_out_of_range(ctsio, lba); 7080 ctl_done((union ctl_io *)ctsio); 7081 return (CTL_RETVAL_COMPLETE); 7082 } 7083 7084 total_len = sizeof(*data) + sizeof(data->descr[0]); 7085 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7086 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7087 ctsio->kern_rel_offset = 0; 7088 ctsio->kern_sg_entries = 0; 7089 ctsio->kern_data_len = min(total_len, alloc_len); 7090 ctsio->kern_total_len = ctsio->kern_data_len; 7091 7092 /* Fill dummy data in case backend can't tell anything. */ 7093 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7094 scsi_u64to8b(lba, data->descr[0].addr); 7095 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7096 data->descr[0].length); 7097 data->descr[0].status = 0; /* Mapped or unknown. */ 7098 7099 ctl_set_success(ctsio); 7100 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7101 ctsio->be_move_done = ctl_config_move_done; 7102 7103 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7104 lbalen->lba = lba; 7105 lbalen->len = total_len; 7106 lbalen->flags = 0; 7107 retval = lun->backend->config_read((union ctl_io *)ctsio); 7108 return (retval); 7109 } 7110 7111 int 7112 ctl_read_defect(struct ctl_scsiio *ctsio) 7113 { 7114 struct scsi_read_defect_data_10 *ccb10; 7115 struct scsi_read_defect_data_12 *ccb12; 7116 struct scsi_read_defect_data_hdr_10 *data10; 7117 struct scsi_read_defect_data_hdr_12 *data12; 7118 uint32_t alloc_len, data_len; 7119 uint8_t format; 7120 7121 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7122 7123 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7124 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7125 format = ccb10->format; 7126 alloc_len = scsi_2btoul(ccb10->alloc_length); 7127 data_len = sizeof(*data10); 7128 } else { 7129 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7130 format = ccb12->format; 7131 alloc_len = scsi_4btoul(ccb12->alloc_length); 7132 data_len = sizeof(*data12); 7133 } 7134 if (alloc_len == 0) { 7135 ctl_set_success(ctsio); 7136 ctl_done((union ctl_io *)ctsio); 7137 return (CTL_RETVAL_COMPLETE); 7138 } 7139 7140 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7141 ctsio->kern_rel_offset = 0; 7142 ctsio->kern_sg_entries = 0; 7143 ctsio->kern_data_len = min(data_len, alloc_len); 7144 ctsio->kern_total_len = ctsio->kern_data_len; 7145 7146 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7147 data10 = (struct scsi_read_defect_data_hdr_10 *) 7148 ctsio->kern_data_ptr; 7149 data10->format = format; 7150 scsi_ulto2b(0, data10->length); 7151 } else { 7152 data12 = (struct scsi_read_defect_data_hdr_12 *) 7153 ctsio->kern_data_ptr; 7154 data12->format = format; 7155 scsi_ulto2b(0, data12->generation); 7156 scsi_ulto4b(0, data12->length); 7157 } 7158 7159 ctl_set_success(ctsio); 7160 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7161 ctsio->be_move_done = ctl_config_move_done; 7162 ctl_datamove((union ctl_io *)ctsio); 7163 return (CTL_RETVAL_COMPLETE); 7164 } 7165 7166 int 7167 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7168 { 7169 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7170 struct ctl_lun *lun = CTL_LUN(ctsio); 7171 struct scsi_maintenance_in *cdb; 7172 int retval; 7173 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7174 int num_ha_groups, num_target_ports, shared_group; 7175 struct ctl_port *port; 7176 struct scsi_target_group_data *rtg_ptr; 7177 struct scsi_target_group_data_extended *rtg_ext_ptr; 7178 struct scsi_target_port_group_descriptor *tpg_desc; 7179 7180 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7181 7182 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7183 retval = CTL_RETVAL_COMPLETE; 7184 7185 switch (cdb->byte2 & STG_PDF_MASK) { 7186 case STG_PDF_LENGTH: 7187 ext = 0; 7188 break; 7189 case STG_PDF_EXTENDED: 7190 ext = 1; 7191 break; 7192 default: 7193 ctl_set_invalid_field(/*ctsio*/ ctsio, 7194 /*sks_valid*/ 1, 7195 /*command*/ 1, 7196 /*field*/ 2, 7197 /*bit_valid*/ 1, 7198 /*bit*/ 5); 7199 ctl_done((union ctl_io *)ctsio); 7200 return(retval); 7201 } 7202 7203 num_target_ports = 0; 7204 shared_group = (softc->is_single != 0); 7205 mtx_lock(&softc->ctl_lock); 7206 STAILQ_FOREACH(port, &softc->port_list, links) { 7207 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7208 continue; 7209 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7210 continue; 7211 num_target_ports++; 7212 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7213 shared_group = 1; 7214 } 7215 mtx_unlock(&softc->ctl_lock); 7216 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7217 7218 if (ext) 7219 total_len = sizeof(struct scsi_target_group_data_extended); 7220 else 7221 total_len = sizeof(struct scsi_target_group_data); 7222 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7223 (shared_group + num_ha_groups) + 7224 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7225 7226 alloc_len = scsi_4btoul(cdb->length); 7227 7228 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7229 ctsio->kern_sg_entries = 0; 7230 ctsio->kern_rel_offset = 0; 7231 ctsio->kern_data_len = min(total_len, alloc_len); 7232 ctsio->kern_total_len = ctsio->kern_data_len; 7233 7234 if (ext) { 7235 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7236 ctsio->kern_data_ptr; 7237 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7238 rtg_ext_ptr->format_type = 0x10; 7239 rtg_ext_ptr->implicit_transition_time = 0; 7240 tpg_desc = &rtg_ext_ptr->groups[0]; 7241 } else { 7242 rtg_ptr = (struct scsi_target_group_data *) 7243 ctsio->kern_data_ptr; 7244 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7245 tpg_desc = &rtg_ptr->groups[0]; 7246 } 7247 7248 mtx_lock(&softc->ctl_lock); 7249 pg = softc->port_min / softc->port_cnt; 7250 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7251 /* Some shelf is known to be primary. */ 7252 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7253 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7254 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7255 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7256 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7257 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7258 else 7259 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7260 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7261 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7262 } else { 7263 ts = os; 7264 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7265 } 7266 } else { 7267 /* No known primary shelf. */ 7268 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7269 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7270 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7271 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7272 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7273 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7274 } else { 7275 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7276 } 7277 } 7278 if (shared_group) { 7279 tpg_desc->pref_state = ts; 7280 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7281 TPG_U_SUP | TPG_T_SUP; 7282 scsi_ulto2b(1, tpg_desc->target_port_group); 7283 tpg_desc->status = TPG_IMPLICIT; 7284 pc = 0; 7285 STAILQ_FOREACH(port, &softc->port_list, links) { 7286 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7287 continue; 7288 if (!softc->is_single && 7289 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7290 continue; 7291 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7292 continue; 7293 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7294 relative_target_port_identifier); 7295 pc++; 7296 } 7297 tpg_desc->target_port_count = pc; 7298 tpg_desc = (struct scsi_target_port_group_descriptor *) 7299 &tpg_desc->descriptors[pc]; 7300 } 7301 for (g = 0; g < num_ha_groups; g++) { 7302 tpg_desc->pref_state = (g == pg) ? ts : os; 7303 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7304 TPG_U_SUP | TPG_T_SUP; 7305 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7306 tpg_desc->status = TPG_IMPLICIT; 7307 pc = 0; 7308 STAILQ_FOREACH(port, &softc->port_list, links) { 7309 if (port->targ_port < g * softc->port_cnt || 7310 port->targ_port >= (g + 1) * softc->port_cnt) 7311 continue; 7312 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7313 continue; 7314 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7315 continue; 7316 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7317 continue; 7318 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7319 relative_target_port_identifier); 7320 pc++; 7321 } 7322 tpg_desc->target_port_count = pc; 7323 tpg_desc = (struct scsi_target_port_group_descriptor *) 7324 &tpg_desc->descriptors[pc]; 7325 } 7326 mtx_unlock(&softc->ctl_lock); 7327 7328 ctl_set_success(ctsio); 7329 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7330 ctsio->be_move_done = ctl_config_move_done; 7331 ctl_datamove((union ctl_io *)ctsio); 7332 return(retval); 7333 } 7334 7335 int 7336 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7337 { 7338 struct ctl_lun *lun = CTL_LUN(ctsio); 7339 struct scsi_report_supported_opcodes *cdb; 7340 const struct ctl_cmd_entry *entry, *sentry; 7341 struct scsi_report_supported_opcodes_all *all; 7342 struct scsi_report_supported_opcodes_descr *descr; 7343 struct scsi_report_supported_opcodes_one *one; 7344 int retval; 7345 int alloc_len, total_len; 7346 int opcode, service_action, i, j, num; 7347 7348 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7349 7350 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7351 retval = CTL_RETVAL_COMPLETE; 7352 7353 opcode = cdb->requested_opcode; 7354 service_action = scsi_2btoul(cdb->requested_service_action); 7355 switch (cdb->options & RSO_OPTIONS_MASK) { 7356 case RSO_OPTIONS_ALL: 7357 num = 0; 7358 for (i = 0; i < 256; i++) { 7359 entry = &ctl_cmd_table[i]; 7360 if (entry->flags & CTL_CMD_FLAG_SA5) { 7361 for (j = 0; j < 32; j++) { 7362 sentry = &((const struct ctl_cmd_entry *) 7363 entry->execute)[j]; 7364 if (ctl_cmd_applicable( 7365 lun->be_lun->lun_type, sentry)) 7366 num++; 7367 } 7368 } else { 7369 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7370 entry)) 7371 num++; 7372 } 7373 } 7374 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7375 num * sizeof(struct scsi_report_supported_opcodes_descr); 7376 break; 7377 case RSO_OPTIONS_OC: 7378 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7379 ctl_set_invalid_field(/*ctsio*/ ctsio, 7380 /*sks_valid*/ 1, 7381 /*command*/ 1, 7382 /*field*/ 2, 7383 /*bit_valid*/ 1, 7384 /*bit*/ 2); 7385 ctl_done((union ctl_io *)ctsio); 7386 return (CTL_RETVAL_COMPLETE); 7387 } 7388 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7389 break; 7390 case RSO_OPTIONS_OC_SA: 7391 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7392 service_action >= 32) { 7393 ctl_set_invalid_field(/*ctsio*/ ctsio, 7394 /*sks_valid*/ 1, 7395 /*command*/ 1, 7396 /*field*/ 2, 7397 /*bit_valid*/ 1, 7398 /*bit*/ 2); 7399 ctl_done((union ctl_io *)ctsio); 7400 return (CTL_RETVAL_COMPLETE); 7401 } 7402 /* FALLTHROUGH */ 7403 case RSO_OPTIONS_OC_ASA: 7404 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7405 break; 7406 default: 7407 ctl_set_invalid_field(/*ctsio*/ ctsio, 7408 /*sks_valid*/ 1, 7409 /*command*/ 1, 7410 /*field*/ 2, 7411 /*bit_valid*/ 1, 7412 /*bit*/ 2); 7413 ctl_done((union ctl_io *)ctsio); 7414 return (CTL_RETVAL_COMPLETE); 7415 } 7416 7417 alloc_len = scsi_4btoul(cdb->length); 7418 7419 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7420 ctsio->kern_sg_entries = 0; 7421 ctsio->kern_rel_offset = 0; 7422 ctsio->kern_data_len = min(total_len, alloc_len); 7423 ctsio->kern_total_len = ctsio->kern_data_len; 7424 7425 switch (cdb->options & RSO_OPTIONS_MASK) { 7426 case RSO_OPTIONS_ALL: 7427 all = (struct scsi_report_supported_opcodes_all *) 7428 ctsio->kern_data_ptr; 7429 num = 0; 7430 for (i = 0; i < 256; i++) { 7431 entry = &ctl_cmd_table[i]; 7432 if (entry->flags & CTL_CMD_FLAG_SA5) { 7433 for (j = 0; j < 32; j++) { 7434 sentry = &((const struct ctl_cmd_entry *) 7435 entry->execute)[j]; 7436 if (!ctl_cmd_applicable( 7437 lun->be_lun->lun_type, sentry)) 7438 continue; 7439 descr = &all->descr[num++]; 7440 descr->opcode = i; 7441 scsi_ulto2b(j, descr->service_action); 7442 descr->flags = RSO_SERVACTV; 7443 scsi_ulto2b(sentry->length, 7444 descr->cdb_length); 7445 } 7446 } else { 7447 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7448 entry)) 7449 continue; 7450 descr = &all->descr[num++]; 7451 descr->opcode = i; 7452 scsi_ulto2b(0, descr->service_action); 7453 descr->flags = 0; 7454 scsi_ulto2b(entry->length, descr->cdb_length); 7455 } 7456 } 7457 scsi_ulto4b( 7458 num * sizeof(struct scsi_report_supported_opcodes_descr), 7459 all->length); 7460 break; 7461 case RSO_OPTIONS_OC: 7462 one = (struct scsi_report_supported_opcodes_one *) 7463 ctsio->kern_data_ptr; 7464 entry = &ctl_cmd_table[opcode]; 7465 goto fill_one; 7466 case RSO_OPTIONS_OC_SA: 7467 one = (struct scsi_report_supported_opcodes_one *) 7468 ctsio->kern_data_ptr; 7469 entry = &ctl_cmd_table[opcode]; 7470 entry = &((const struct ctl_cmd_entry *) 7471 entry->execute)[service_action]; 7472 fill_one: 7473 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7474 one->support = 3; 7475 scsi_ulto2b(entry->length, one->cdb_length); 7476 one->cdb_usage[0] = opcode; 7477 memcpy(&one->cdb_usage[1], entry->usage, 7478 entry->length - 1); 7479 } else 7480 one->support = 1; 7481 break; 7482 case RSO_OPTIONS_OC_ASA: 7483 one = (struct scsi_report_supported_opcodes_one *) 7484 ctsio->kern_data_ptr; 7485 entry = &ctl_cmd_table[opcode]; 7486 if (entry->flags & CTL_CMD_FLAG_SA5) { 7487 entry = &((const struct ctl_cmd_entry *) 7488 entry->execute)[service_action]; 7489 } else if (service_action != 0) { 7490 one->support = 1; 7491 break; 7492 } 7493 goto fill_one; 7494 } 7495 7496 ctl_set_success(ctsio); 7497 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7498 ctsio->be_move_done = ctl_config_move_done; 7499 ctl_datamove((union ctl_io *)ctsio); 7500 return(retval); 7501 } 7502 7503 int 7504 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7505 { 7506 struct scsi_report_supported_tmf *cdb; 7507 struct scsi_report_supported_tmf_ext_data *data; 7508 int retval; 7509 int alloc_len, total_len; 7510 7511 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7512 7513 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7514 7515 retval = CTL_RETVAL_COMPLETE; 7516 7517 if (cdb->options & RST_REPD) 7518 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7519 else 7520 total_len = sizeof(struct scsi_report_supported_tmf_data); 7521 alloc_len = scsi_4btoul(cdb->length); 7522 7523 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7524 ctsio->kern_sg_entries = 0; 7525 ctsio->kern_rel_offset = 0; 7526 ctsio->kern_data_len = min(total_len, alloc_len); 7527 ctsio->kern_total_len = ctsio->kern_data_len; 7528 7529 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7530 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7531 RST_TRS; 7532 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7533 data->length = total_len - 4; 7534 7535 ctl_set_success(ctsio); 7536 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7537 ctsio->be_move_done = ctl_config_move_done; 7538 ctl_datamove((union ctl_io *)ctsio); 7539 return (retval); 7540 } 7541 7542 int 7543 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7544 { 7545 struct scsi_report_timestamp *cdb; 7546 struct scsi_report_timestamp_data *data; 7547 struct timeval tv; 7548 int64_t timestamp; 7549 int retval; 7550 int alloc_len, total_len; 7551 7552 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7553 7554 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7555 7556 retval = CTL_RETVAL_COMPLETE; 7557 7558 total_len = sizeof(struct scsi_report_timestamp_data); 7559 alloc_len = scsi_4btoul(cdb->length); 7560 7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7562 ctsio->kern_sg_entries = 0; 7563 ctsio->kern_rel_offset = 0; 7564 ctsio->kern_data_len = min(total_len, alloc_len); 7565 ctsio->kern_total_len = ctsio->kern_data_len; 7566 7567 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7568 scsi_ulto2b(sizeof(*data) - 2, data->length); 7569 data->origin = RTS_ORIG_OUTSIDE; 7570 getmicrotime(&tv); 7571 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7572 scsi_ulto4b(timestamp >> 16, data->timestamp); 7573 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7574 7575 ctl_set_success(ctsio); 7576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7577 ctsio->be_move_done = ctl_config_move_done; 7578 ctl_datamove((union ctl_io *)ctsio); 7579 return (retval); 7580 } 7581 7582 int 7583 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7584 { 7585 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7586 struct ctl_lun *lun = CTL_LUN(ctsio); 7587 struct scsi_per_res_in *cdb; 7588 int alloc_len, total_len = 0; 7589 /* struct scsi_per_res_in_rsrv in_data; */ 7590 uint64_t key; 7591 7592 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7593 7594 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7595 7596 alloc_len = scsi_2btoul(cdb->length); 7597 7598 retry: 7599 mtx_lock(&lun->lun_lock); 7600 switch (cdb->action) { 7601 case SPRI_RK: /* read keys */ 7602 total_len = sizeof(struct scsi_per_res_in_keys) + 7603 lun->pr_key_count * 7604 sizeof(struct scsi_per_res_key); 7605 break; 7606 case SPRI_RR: /* read reservation */ 7607 if (lun->flags & CTL_LUN_PR_RESERVED) 7608 total_len = sizeof(struct scsi_per_res_in_rsrv); 7609 else 7610 total_len = sizeof(struct scsi_per_res_in_header); 7611 break; 7612 case SPRI_RC: /* report capabilities */ 7613 total_len = sizeof(struct scsi_per_res_cap); 7614 break; 7615 case SPRI_RS: /* read full status */ 7616 total_len = sizeof(struct scsi_per_res_in_header) + 7617 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7618 lun->pr_key_count; 7619 break; 7620 default: 7621 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7622 } 7623 mtx_unlock(&lun->lun_lock); 7624 7625 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7626 ctsio->kern_rel_offset = 0; 7627 ctsio->kern_sg_entries = 0; 7628 ctsio->kern_data_len = min(total_len, alloc_len); 7629 ctsio->kern_total_len = ctsio->kern_data_len; 7630 7631 mtx_lock(&lun->lun_lock); 7632 switch (cdb->action) { 7633 case SPRI_RK: { // read keys 7634 struct scsi_per_res_in_keys *res_keys; 7635 int i, key_count; 7636 7637 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7638 7639 /* 7640 * We had to drop the lock to allocate our buffer, which 7641 * leaves time for someone to come in with another 7642 * persistent reservation. (That is unlikely, though, 7643 * since this should be the only persistent reservation 7644 * command active right now.) 7645 */ 7646 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7647 (lun->pr_key_count * 7648 sizeof(struct scsi_per_res_key)))){ 7649 mtx_unlock(&lun->lun_lock); 7650 free(ctsio->kern_data_ptr, M_CTL); 7651 printf("%s: reservation length changed, retrying\n", 7652 __func__); 7653 goto retry; 7654 } 7655 7656 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7657 7658 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7659 lun->pr_key_count, res_keys->header.length); 7660 7661 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7662 if ((key = ctl_get_prkey(lun, i)) == 0) 7663 continue; 7664 7665 /* 7666 * We used lun->pr_key_count to calculate the 7667 * size to allocate. If it turns out the number of 7668 * initiators with the registered flag set is 7669 * larger than that (i.e. they haven't been kept in 7670 * sync), we've got a problem. 7671 */ 7672 if (key_count >= lun->pr_key_count) { 7673 key_count++; 7674 continue; 7675 } 7676 scsi_u64to8b(key, res_keys->keys[key_count].key); 7677 key_count++; 7678 } 7679 break; 7680 } 7681 case SPRI_RR: { // read reservation 7682 struct scsi_per_res_in_rsrv *res; 7683 int tmp_len, header_only; 7684 7685 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7686 7687 scsi_ulto4b(lun->pr_generation, res->header.generation); 7688 7689 if (lun->flags & CTL_LUN_PR_RESERVED) 7690 { 7691 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7692 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7693 res->header.length); 7694 header_only = 0; 7695 } else { 7696 tmp_len = sizeof(struct scsi_per_res_in_header); 7697 scsi_ulto4b(0, res->header.length); 7698 header_only = 1; 7699 } 7700 7701 /* 7702 * We had to drop the lock to allocate our buffer, which 7703 * leaves time for someone to come in with another 7704 * persistent reservation. (That is unlikely, though, 7705 * since this should be the only persistent reservation 7706 * command active right now.) 7707 */ 7708 if (tmp_len != total_len) { 7709 mtx_unlock(&lun->lun_lock); 7710 free(ctsio->kern_data_ptr, M_CTL); 7711 printf("%s: reservation status changed, retrying\n", 7712 __func__); 7713 goto retry; 7714 } 7715 7716 /* 7717 * No reservation held, so we're done. 7718 */ 7719 if (header_only != 0) 7720 break; 7721 7722 /* 7723 * If the registration is an All Registrants type, the key 7724 * is 0, since it doesn't really matter. 7725 */ 7726 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7727 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7728 res->data.reservation); 7729 } 7730 res->data.scopetype = lun->pr_res_type; 7731 break; 7732 } 7733 case SPRI_RC: //report capabilities 7734 { 7735 struct scsi_per_res_cap *res_cap; 7736 uint16_t type_mask; 7737 7738 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7739 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7740 res_cap->flags1 = SPRI_CRH; 7741 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7742 type_mask = SPRI_TM_WR_EX_AR | 7743 SPRI_TM_EX_AC_RO | 7744 SPRI_TM_WR_EX_RO | 7745 SPRI_TM_EX_AC | 7746 SPRI_TM_WR_EX | 7747 SPRI_TM_EX_AC_AR; 7748 scsi_ulto2b(type_mask, res_cap->type_mask); 7749 break; 7750 } 7751 case SPRI_RS: { // read full status 7752 struct scsi_per_res_in_full *res_status; 7753 struct scsi_per_res_in_full_desc *res_desc; 7754 struct ctl_port *port; 7755 int i, len; 7756 7757 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7758 7759 /* 7760 * We had to drop the lock to allocate our buffer, which 7761 * leaves time for someone to come in with another 7762 * persistent reservation. (That is unlikely, though, 7763 * since this should be the only persistent reservation 7764 * command active right now.) 7765 */ 7766 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7767 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7768 lun->pr_key_count)){ 7769 mtx_unlock(&lun->lun_lock); 7770 free(ctsio->kern_data_ptr, M_CTL); 7771 printf("%s: reservation length changed, retrying\n", 7772 __func__); 7773 goto retry; 7774 } 7775 7776 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7777 7778 res_desc = &res_status->desc[0]; 7779 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7780 if ((key = ctl_get_prkey(lun, i)) == 0) 7781 continue; 7782 7783 scsi_u64to8b(key, res_desc->res_key.key); 7784 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7785 (lun->pr_res_idx == i || 7786 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7787 res_desc->flags = SPRI_FULL_R_HOLDER; 7788 res_desc->scopetype = lun->pr_res_type; 7789 } 7790 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7791 res_desc->rel_trgt_port_id); 7792 len = 0; 7793 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7794 if (port != NULL) 7795 len = ctl_create_iid(port, 7796 i % CTL_MAX_INIT_PER_PORT, 7797 res_desc->transport_id); 7798 scsi_ulto4b(len, res_desc->additional_length); 7799 res_desc = (struct scsi_per_res_in_full_desc *) 7800 &res_desc->transport_id[len]; 7801 } 7802 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7803 res_status->header.length); 7804 break; 7805 } 7806 default: 7807 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7808 } 7809 mtx_unlock(&lun->lun_lock); 7810 7811 ctl_set_success(ctsio); 7812 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7813 ctsio->be_move_done = ctl_config_move_done; 7814 ctl_datamove((union ctl_io *)ctsio); 7815 return (CTL_RETVAL_COMPLETE); 7816 } 7817 7818 /* 7819 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7820 * it should return. 7821 */ 7822 static int 7823 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7824 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7825 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7826 struct scsi_per_res_out_parms* param) 7827 { 7828 union ctl_ha_msg persis_io; 7829 int i; 7830 7831 mtx_lock(&lun->lun_lock); 7832 if (sa_res_key == 0) { 7833 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7834 /* validate scope and type */ 7835 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7836 SPR_LU_SCOPE) { 7837 mtx_unlock(&lun->lun_lock); 7838 ctl_set_invalid_field(/*ctsio*/ ctsio, 7839 /*sks_valid*/ 1, 7840 /*command*/ 1, 7841 /*field*/ 2, 7842 /*bit_valid*/ 1, 7843 /*bit*/ 4); 7844 ctl_done((union ctl_io *)ctsio); 7845 return (1); 7846 } 7847 7848 if (type>8 || type==2 || type==4 || type==0) { 7849 mtx_unlock(&lun->lun_lock); 7850 ctl_set_invalid_field(/*ctsio*/ ctsio, 7851 /*sks_valid*/ 1, 7852 /*command*/ 1, 7853 /*field*/ 2, 7854 /*bit_valid*/ 1, 7855 /*bit*/ 0); 7856 ctl_done((union ctl_io *)ctsio); 7857 return (1); 7858 } 7859 7860 /* 7861 * Unregister everybody else and build UA for 7862 * them 7863 */ 7864 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7865 if (i == residx || ctl_get_prkey(lun, i) == 0) 7866 continue; 7867 7868 ctl_clr_prkey(lun, i); 7869 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7870 } 7871 lun->pr_key_count = 1; 7872 lun->pr_res_type = type; 7873 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7874 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7875 lun->pr_res_idx = residx; 7876 lun->pr_generation++; 7877 mtx_unlock(&lun->lun_lock); 7878 7879 /* send msg to other side */ 7880 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7881 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7882 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7883 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7884 persis_io.pr.pr_info.res_type = type; 7885 memcpy(persis_io.pr.pr_info.sa_res_key, 7886 param->serv_act_res_key, 7887 sizeof(param->serv_act_res_key)); 7888 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7889 sizeof(persis_io.pr), M_WAITOK); 7890 } else { 7891 /* not all registrants */ 7892 mtx_unlock(&lun->lun_lock); 7893 free(ctsio->kern_data_ptr, M_CTL); 7894 ctl_set_invalid_field(ctsio, 7895 /*sks_valid*/ 1, 7896 /*command*/ 0, 7897 /*field*/ 8, 7898 /*bit_valid*/ 0, 7899 /*bit*/ 0); 7900 ctl_done((union ctl_io *)ctsio); 7901 return (1); 7902 } 7903 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7904 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7905 int found = 0; 7906 7907 if (res_key == sa_res_key) { 7908 /* special case */ 7909 /* 7910 * The spec implies this is not good but doesn't 7911 * say what to do. There are two choices either 7912 * generate a res conflict or check condition 7913 * with illegal field in parameter data. Since 7914 * that is what is done when the sa_res_key is 7915 * zero I'll take that approach since this has 7916 * to do with the sa_res_key. 7917 */ 7918 mtx_unlock(&lun->lun_lock); 7919 free(ctsio->kern_data_ptr, M_CTL); 7920 ctl_set_invalid_field(ctsio, 7921 /*sks_valid*/ 1, 7922 /*command*/ 0, 7923 /*field*/ 8, 7924 /*bit_valid*/ 0, 7925 /*bit*/ 0); 7926 ctl_done((union ctl_io *)ctsio); 7927 return (1); 7928 } 7929 7930 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7931 if (ctl_get_prkey(lun, i) != sa_res_key) 7932 continue; 7933 7934 found = 1; 7935 ctl_clr_prkey(lun, i); 7936 lun->pr_key_count--; 7937 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7938 } 7939 if (!found) { 7940 mtx_unlock(&lun->lun_lock); 7941 free(ctsio->kern_data_ptr, M_CTL); 7942 ctl_set_reservation_conflict(ctsio); 7943 ctl_done((union ctl_io *)ctsio); 7944 return (CTL_RETVAL_COMPLETE); 7945 } 7946 lun->pr_generation++; 7947 mtx_unlock(&lun->lun_lock); 7948 7949 /* send msg to other side */ 7950 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7951 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7952 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7953 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7954 persis_io.pr.pr_info.res_type = type; 7955 memcpy(persis_io.pr.pr_info.sa_res_key, 7956 param->serv_act_res_key, 7957 sizeof(param->serv_act_res_key)); 7958 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7959 sizeof(persis_io.pr), M_WAITOK); 7960 } else { 7961 /* Reserved but not all registrants */ 7962 /* sa_res_key is res holder */ 7963 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7964 /* validate scope and type */ 7965 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7966 SPR_LU_SCOPE) { 7967 mtx_unlock(&lun->lun_lock); 7968 ctl_set_invalid_field(/*ctsio*/ ctsio, 7969 /*sks_valid*/ 1, 7970 /*command*/ 1, 7971 /*field*/ 2, 7972 /*bit_valid*/ 1, 7973 /*bit*/ 4); 7974 ctl_done((union ctl_io *)ctsio); 7975 return (1); 7976 } 7977 7978 if (type>8 || type==2 || type==4 || type==0) { 7979 mtx_unlock(&lun->lun_lock); 7980 ctl_set_invalid_field(/*ctsio*/ ctsio, 7981 /*sks_valid*/ 1, 7982 /*command*/ 1, 7983 /*field*/ 2, 7984 /*bit_valid*/ 1, 7985 /*bit*/ 0); 7986 ctl_done((union ctl_io *)ctsio); 7987 return (1); 7988 } 7989 7990 /* 7991 * Do the following: 7992 * if sa_res_key != res_key remove all 7993 * registrants w/sa_res_key and generate UA 7994 * for these registrants(Registrations 7995 * Preempted) if it wasn't an exclusive 7996 * reservation generate UA(Reservations 7997 * Preempted) for all other registered nexuses 7998 * if the type has changed. Establish the new 7999 * reservation and holder. If res_key and 8000 * sa_res_key are the same do the above 8001 * except don't unregister the res holder. 8002 */ 8003 8004 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8005 if (i == residx || ctl_get_prkey(lun, i) == 0) 8006 continue; 8007 8008 if (sa_res_key == ctl_get_prkey(lun, i)) { 8009 ctl_clr_prkey(lun, i); 8010 lun->pr_key_count--; 8011 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8012 } else if (type != lun->pr_res_type && 8013 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8014 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8015 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8016 } 8017 } 8018 lun->pr_res_type = type; 8019 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8020 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8021 lun->pr_res_idx = residx; 8022 else 8023 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8024 lun->pr_generation++; 8025 mtx_unlock(&lun->lun_lock); 8026 8027 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8028 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8029 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8030 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8031 persis_io.pr.pr_info.res_type = type; 8032 memcpy(persis_io.pr.pr_info.sa_res_key, 8033 param->serv_act_res_key, 8034 sizeof(param->serv_act_res_key)); 8035 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8036 sizeof(persis_io.pr), M_WAITOK); 8037 } else { 8038 /* 8039 * sa_res_key is not the res holder just 8040 * remove registrants 8041 */ 8042 int found=0; 8043 8044 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8045 if (sa_res_key != ctl_get_prkey(lun, i)) 8046 continue; 8047 8048 found = 1; 8049 ctl_clr_prkey(lun, i); 8050 lun->pr_key_count--; 8051 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8052 } 8053 8054 if (!found) { 8055 mtx_unlock(&lun->lun_lock); 8056 free(ctsio->kern_data_ptr, M_CTL); 8057 ctl_set_reservation_conflict(ctsio); 8058 ctl_done((union ctl_io *)ctsio); 8059 return (1); 8060 } 8061 lun->pr_generation++; 8062 mtx_unlock(&lun->lun_lock); 8063 8064 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8065 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8066 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8067 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8068 persis_io.pr.pr_info.res_type = type; 8069 memcpy(persis_io.pr.pr_info.sa_res_key, 8070 param->serv_act_res_key, 8071 sizeof(param->serv_act_res_key)); 8072 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8073 sizeof(persis_io.pr), M_WAITOK); 8074 } 8075 } 8076 return (0); 8077 } 8078 8079 static void 8080 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8081 { 8082 uint64_t sa_res_key; 8083 int i; 8084 8085 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8086 8087 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8088 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8089 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8090 if (sa_res_key == 0) { 8091 /* 8092 * Unregister everybody else and build UA for 8093 * them 8094 */ 8095 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8096 if (i == msg->pr.pr_info.residx || 8097 ctl_get_prkey(lun, i) == 0) 8098 continue; 8099 8100 ctl_clr_prkey(lun, i); 8101 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8102 } 8103 8104 lun->pr_key_count = 1; 8105 lun->pr_res_type = msg->pr.pr_info.res_type; 8106 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8107 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8108 lun->pr_res_idx = msg->pr.pr_info.residx; 8109 } else { 8110 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8111 if (sa_res_key == ctl_get_prkey(lun, i)) 8112 continue; 8113 8114 ctl_clr_prkey(lun, i); 8115 lun->pr_key_count--; 8116 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8117 } 8118 } 8119 } else { 8120 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8121 if (i == msg->pr.pr_info.residx || 8122 ctl_get_prkey(lun, i) == 0) 8123 continue; 8124 8125 if (sa_res_key == ctl_get_prkey(lun, i)) { 8126 ctl_clr_prkey(lun, i); 8127 lun->pr_key_count--; 8128 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8129 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8130 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8131 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8132 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8133 } 8134 } 8135 lun->pr_res_type = msg->pr.pr_info.res_type; 8136 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8137 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8138 lun->pr_res_idx = msg->pr.pr_info.residx; 8139 else 8140 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8141 } 8142 lun->pr_generation++; 8143 8144 } 8145 8146 8147 int 8148 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8149 { 8150 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8151 struct ctl_lun *lun = CTL_LUN(ctsio); 8152 int retval; 8153 u_int32_t param_len; 8154 struct scsi_per_res_out *cdb; 8155 struct scsi_per_res_out_parms* param; 8156 uint32_t residx; 8157 uint64_t res_key, sa_res_key, key; 8158 uint8_t type; 8159 union ctl_ha_msg persis_io; 8160 int i; 8161 8162 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8163 8164 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8165 retval = CTL_RETVAL_COMPLETE; 8166 8167 /* 8168 * We only support whole-LUN scope. The scope & type are ignored for 8169 * register, register and ignore existing key and clear. 8170 * We sometimes ignore scope and type on preempts too!! 8171 * Verify reservation type here as well. 8172 */ 8173 type = cdb->scope_type & SPR_TYPE_MASK; 8174 if ((cdb->action == SPRO_RESERVE) 8175 || (cdb->action == SPRO_RELEASE)) { 8176 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8177 ctl_set_invalid_field(/*ctsio*/ ctsio, 8178 /*sks_valid*/ 1, 8179 /*command*/ 1, 8180 /*field*/ 2, 8181 /*bit_valid*/ 1, 8182 /*bit*/ 4); 8183 ctl_done((union ctl_io *)ctsio); 8184 return (CTL_RETVAL_COMPLETE); 8185 } 8186 8187 if (type>8 || type==2 || type==4 || type==0) { 8188 ctl_set_invalid_field(/*ctsio*/ ctsio, 8189 /*sks_valid*/ 1, 8190 /*command*/ 1, 8191 /*field*/ 2, 8192 /*bit_valid*/ 1, 8193 /*bit*/ 0); 8194 ctl_done((union ctl_io *)ctsio); 8195 return (CTL_RETVAL_COMPLETE); 8196 } 8197 } 8198 8199 param_len = scsi_4btoul(cdb->length); 8200 8201 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8202 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8203 ctsio->kern_data_len = param_len; 8204 ctsio->kern_total_len = param_len; 8205 ctsio->kern_rel_offset = 0; 8206 ctsio->kern_sg_entries = 0; 8207 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8208 ctsio->be_move_done = ctl_config_move_done; 8209 ctl_datamove((union ctl_io *)ctsio); 8210 8211 return (CTL_RETVAL_COMPLETE); 8212 } 8213 8214 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8215 8216 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8217 res_key = scsi_8btou64(param->res_key.key); 8218 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8219 8220 /* 8221 * Validate the reservation key here except for SPRO_REG_IGNO 8222 * This must be done for all other service actions 8223 */ 8224 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8225 mtx_lock(&lun->lun_lock); 8226 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8227 if (res_key != key) { 8228 /* 8229 * The current key passed in doesn't match 8230 * the one the initiator previously 8231 * registered. 8232 */ 8233 mtx_unlock(&lun->lun_lock); 8234 free(ctsio->kern_data_ptr, M_CTL); 8235 ctl_set_reservation_conflict(ctsio); 8236 ctl_done((union ctl_io *)ctsio); 8237 return (CTL_RETVAL_COMPLETE); 8238 } 8239 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8240 /* 8241 * We are not registered 8242 */ 8243 mtx_unlock(&lun->lun_lock); 8244 free(ctsio->kern_data_ptr, M_CTL); 8245 ctl_set_reservation_conflict(ctsio); 8246 ctl_done((union ctl_io *)ctsio); 8247 return (CTL_RETVAL_COMPLETE); 8248 } else if (res_key != 0) { 8249 /* 8250 * We are not registered and trying to register but 8251 * the register key isn't zero. 8252 */ 8253 mtx_unlock(&lun->lun_lock); 8254 free(ctsio->kern_data_ptr, M_CTL); 8255 ctl_set_reservation_conflict(ctsio); 8256 ctl_done((union ctl_io *)ctsio); 8257 return (CTL_RETVAL_COMPLETE); 8258 } 8259 mtx_unlock(&lun->lun_lock); 8260 } 8261 8262 switch (cdb->action & SPRO_ACTION_MASK) { 8263 case SPRO_REGISTER: 8264 case SPRO_REG_IGNO: { 8265 8266 #if 0 8267 printf("Registration received\n"); 8268 #endif 8269 8270 /* 8271 * We don't support any of these options, as we report in 8272 * the read capabilities request (see 8273 * ctl_persistent_reserve_in(), above). 8274 */ 8275 if ((param->flags & SPR_SPEC_I_PT) 8276 || (param->flags & SPR_ALL_TG_PT) 8277 || (param->flags & SPR_APTPL)) { 8278 int bit_ptr; 8279 8280 if (param->flags & SPR_APTPL) 8281 bit_ptr = 0; 8282 else if (param->flags & SPR_ALL_TG_PT) 8283 bit_ptr = 2; 8284 else /* SPR_SPEC_I_PT */ 8285 bit_ptr = 3; 8286 8287 free(ctsio->kern_data_ptr, M_CTL); 8288 ctl_set_invalid_field(ctsio, 8289 /*sks_valid*/ 1, 8290 /*command*/ 0, 8291 /*field*/ 20, 8292 /*bit_valid*/ 1, 8293 /*bit*/ bit_ptr); 8294 ctl_done((union ctl_io *)ctsio); 8295 return (CTL_RETVAL_COMPLETE); 8296 } 8297 8298 mtx_lock(&lun->lun_lock); 8299 8300 /* 8301 * The initiator wants to clear the 8302 * key/unregister. 8303 */ 8304 if (sa_res_key == 0) { 8305 if ((res_key == 0 8306 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8307 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8308 && ctl_get_prkey(lun, residx) == 0)) { 8309 mtx_unlock(&lun->lun_lock); 8310 goto done; 8311 } 8312 8313 ctl_clr_prkey(lun, residx); 8314 lun->pr_key_count--; 8315 8316 if (residx == lun->pr_res_idx) { 8317 lun->flags &= ~CTL_LUN_PR_RESERVED; 8318 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8319 8320 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8321 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8322 lun->pr_key_count) { 8323 /* 8324 * If the reservation is a registrants 8325 * only type we need to generate a UA 8326 * for other registered inits. The 8327 * sense code should be RESERVATIONS 8328 * RELEASED 8329 */ 8330 8331 for (i = softc->init_min; i < softc->init_max; i++){ 8332 if (ctl_get_prkey(lun, i) == 0) 8333 continue; 8334 ctl_est_ua(lun, i, 8335 CTL_UA_RES_RELEASE); 8336 } 8337 } 8338 lun->pr_res_type = 0; 8339 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8340 if (lun->pr_key_count==0) { 8341 lun->flags &= ~CTL_LUN_PR_RESERVED; 8342 lun->pr_res_type = 0; 8343 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8344 } 8345 } 8346 lun->pr_generation++; 8347 mtx_unlock(&lun->lun_lock); 8348 8349 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8350 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8351 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8352 persis_io.pr.pr_info.residx = residx; 8353 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8354 sizeof(persis_io.pr), M_WAITOK); 8355 } else /* sa_res_key != 0 */ { 8356 8357 /* 8358 * If we aren't registered currently then increment 8359 * the key count and set the registered flag. 8360 */ 8361 ctl_alloc_prkey(lun, residx); 8362 if (ctl_get_prkey(lun, residx) == 0) 8363 lun->pr_key_count++; 8364 ctl_set_prkey(lun, residx, sa_res_key); 8365 lun->pr_generation++; 8366 mtx_unlock(&lun->lun_lock); 8367 8368 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8369 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8370 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8371 persis_io.pr.pr_info.residx = residx; 8372 memcpy(persis_io.pr.pr_info.sa_res_key, 8373 param->serv_act_res_key, 8374 sizeof(param->serv_act_res_key)); 8375 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8376 sizeof(persis_io.pr), M_WAITOK); 8377 } 8378 8379 break; 8380 } 8381 case SPRO_RESERVE: 8382 #if 0 8383 printf("Reserve executed type %d\n", type); 8384 #endif 8385 mtx_lock(&lun->lun_lock); 8386 if (lun->flags & CTL_LUN_PR_RESERVED) { 8387 /* 8388 * if this isn't the reservation holder and it's 8389 * not a "all registrants" type or if the type is 8390 * different then we have a conflict 8391 */ 8392 if ((lun->pr_res_idx != residx 8393 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8394 || lun->pr_res_type != type) { 8395 mtx_unlock(&lun->lun_lock); 8396 free(ctsio->kern_data_ptr, M_CTL); 8397 ctl_set_reservation_conflict(ctsio); 8398 ctl_done((union ctl_io *)ctsio); 8399 return (CTL_RETVAL_COMPLETE); 8400 } 8401 mtx_unlock(&lun->lun_lock); 8402 } else /* create a reservation */ { 8403 /* 8404 * If it's not an "all registrants" type record 8405 * reservation holder 8406 */ 8407 if (type != SPR_TYPE_WR_EX_AR 8408 && type != SPR_TYPE_EX_AC_AR) 8409 lun->pr_res_idx = residx; /* Res holder */ 8410 else 8411 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8412 8413 lun->flags |= CTL_LUN_PR_RESERVED; 8414 lun->pr_res_type = type; 8415 8416 mtx_unlock(&lun->lun_lock); 8417 8418 /* send msg to other side */ 8419 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8420 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8421 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8422 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8423 persis_io.pr.pr_info.res_type = type; 8424 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8425 sizeof(persis_io.pr), M_WAITOK); 8426 } 8427 break; 8428 8429 case SPRO_RELEASE: 8430 mtx_lock(&lun->lun_lock); 8431 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8432 /* No reservation exists return good status */ 8433 mtx_unlock(&lun->lun_lock); 8434 goto done; 8435 } 8436 /* 8437 * Is this nexus a reservation holder? 8438 */ 8439 if (lun->pr_res_idx != residx 8440 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8441 /* 8442 * not a res holder return good status but 8443 * do nothing 8444 */ 8445 mtx_unlock(&lun->lun_lock); 8446 goto done; 8447 } 8448 8449 if (lun->pr_res_type != type) { 8450 mtx_unlock(&lun->lun_lock); 8451 free(ctsio->kern_data_ptr, M_CTL); 8452 ctl_set_illegal_pr_release(ctsio); 8453 ctl_done((union ctl_io *)ctsio); 8454 return (CTL_RETVAL_COMPLETE); 8455 } 8456 8457 /* okay to release */ 8458 lun->flags &= ~CTL_LUN_PR_RESERVED; 8459 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8460 lun->pr_res_type = 0; 8461 8462 /* 8463 * If this isn't an exclusive access reservation and NUAR 8464 * is not set, generate UA for all other registrants. 8465 */ 8466 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8467 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8468 for (i = softc->init_min; i < softc->init_max; i++) { 8469 if (i == residx || ctl_get_prkey(lun, i) == 0) 8470 continue; 8471 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8472 } 8473 } 8474 mtx_unlock(&lun->lun_lock); 8475 8476 /* Send msg to other side */ 8477 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8478 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8479 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8480 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8481 sizeof(persis_io.pr), M_WAITOK); 8482 break; 8483 8484 case SPRO_CLEAR: 8485 /* send msg to other side */ 8486 8487 mtx_lock(&lun->lun_lock); 8488 lun->flags &= ~CTL_LUN_PR_RESERVED; 8489 lun->pr_res_type = 0; 8490 lun->pr_key_count = 0; 8491 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8492 8493 ctl_clr_prkey(lun, residx); 8494 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8495 if (ctl_get_prkey(lun, i) != 0) { 8496 ctl_clr_prkey(lun, i); 8497 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8498 } 8499 lun->pr_generation++; 8500 mtx_unlock(&lun->lun_lock); 8501 8502 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8503 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8504 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8505 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8506 sizeof(persis_io.pr), M_WAITOK); 8507 break; 8508 8509 case SPRO_PREEMPT: 8510 case SPRO_PRE_ABO: { 8511 int nretval; 8512 8513 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8514 residx, ctsio, cdb, param); 8515 if (nretval != 0) 8516 return (CTL_RETVAL_COMPLETE); 8517 break; 8518 } 8519 default: 8520 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8521 } 8522 8523 done: 8524 free(ctsio->kern_data_ptr, M_CTL); 8525 ctl_set_success(ctsio); 8526 ctl_done((union ctl_io *)ctsio); 8527 8528 return (retval); 8529 } 8530 8531 /* 8532 * This routine is for handling a message from the other SC pertaining to 8533 * persistent reserve out. All the error checking will have been done 8534 * so only perorming the action need be done here to keep the two 8535 * in sync. 8536 */ 8537 static void 8538 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8539 { 8540 struct ctl_softc *softc = CTL_SOFTC(io); 8541 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8542 struct ctl_lun *lun; 8543 int i; 8544 uint32_t residx, targ_lun; 8545 8546 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8547 mtx_lock(&softc->ctl_lock); 8548 if (targ_lun >= ctl_max_luns || 8549 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8550 mtx_unlock(&softc->ctl_lock); 8551 return; 8552 } 8553 mtx_lock(&lun->lun_lock); 8554 mtx_unlock(&softc->ctl_lock); 8555 if (lun->flags & CTL_LUN_DISABLED) { 8556 mtx_unlock(&lun->lun_lock); 8557 return; 8558 } 8559 residx = ctl_get_initindex(&msg->hdr.nexus); 8560 switch(msg->pr.pr_info.action) { 8561 case CTL_PR_REG_KEY: 8562 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8563 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8564 lun->pr_key_count++; 8565 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8566 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8567 lun->pr_generation++; 8568 break; 8569 8570 case CTL_PR_UNREG_KEY: 8571 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8572 lun->pr_key_count--; 8573 8574 /* XXX Need to see if the reservation has been released */ 8575 /* if so do we need to generate UA? */ 8576 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8577 lun->flags &= ~CTL_LUN_PR_RESERVED; 8578 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8579 8580 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8581 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8582 lun->pr_key_count) { 8583 /* 8584 * If the reservation is a registrants 8585 * only type we need to generate a UA 8586 * for other registered inits. The 8587 * sense code should be RESERVATIONS 8588 * RELEASED 8589 */ 8590 8591 for (i = softc->init_min; i < softc->init_max; i++) { 8592 if (ctl_get_prkey(lun, i) == 0) 8593 continue; 8594 8595 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8596 } 8597 } 8598 lun->pr_res_type = 0; 8599 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8600 if (lun->pr_key_count==0) { 8601 lun->flags &= ~CTL_LUN_PR_RESERVED; 8602 lun->pr_res_type = 0; 8603 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8604 } 8605 } 8606 lun->pr_generation++; 8607 break; 8608 8609 case CTL_PR_RESERVE: 8610 lun->flags |= CTL_LUN_PR_RESERVED; 8611 lun->pr_res_type = msg->pr.pr_info.res_type; 8612 lun->pr_res_idx = msg->pr.pr_info.residx; 8613 8614 break; 8615 8616 case CTL_PR_RELEASE: 8617 /* 8618 * If this isn't an exclusive access reservation and NUAR 8619 * is not set, generate UA for all other registrants. 8620 */ 8621 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8622 lun->pr_res_type != SPR_TYPE_WR_EX && 8623 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8624 for (i = softc->init_min; i < softc->init_max; i++) { 8625 if (i == residx || ctl_get_prkey(lun, i) == 0) 8626 continue; 8627 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8628 } 8629 } 8630 8631 lun->flags &= ~CTL_LUN_PR_RESERVED; 8632 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8633 lun->pr_res_type = 0; 8634 break; 8635 8636 case CTL_PR_PREEMPT: 8637 ctl_pro_preempt_other(lun, msg); 8638 break; 8639 case CTL_PR_CLEAR: 8640 lun->flags &= ~CTL_LUN_PR_RESERVED; 8641 lun->pr_res_type = 0; 8642 lun->pr_key_count = 0; 8643 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8644 8645 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8646 if (ctl_get_prkey(lun, i) == 0) 8647 continue; 8648 ctl_clr_prkey(lun, i); 8649 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8650 } 8651 lun->pr_generation++; 8652 break; 8653 } 8654 8655 mtx_unlock(&lun->lun_lock); 8656 } 8657 8658 int 8659 ctl_read_write(struct ctl_scsiio *ctsio) 8660 { 8661 struct ctl_lun *lun = CTL_LUN(ctsio); 8662 struct ctl_lba_len_flags *lbalen; 8663 uint64_t lba; 8664 uint32_t num_blocks; 8665 int flags, retval; 8666 int isread; 8667 8668 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8669 8670 flags = 0; 8671 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8672 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8673 switch (ctsio->cdb[0]) { 8674 case READ_6: 8675 case WRITE_6: { 8676 struct scsi_rw_6 *cdb; 8677 8678 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8679 8680 lba = scsi_3btoul(cdb->addr); 8681 /* only 5 bits are valid in the most significant address byte */ 8682 lba &= 0x1fffff; 8683 num_blocks = cdb->length; 8684 /* 8685 * This is correct according to SBC-2. 8686 */ 8687 if (num_blocks == 0) 8688 num_blocks = 256; 8689 break; 8690 } 8691 case READ_10: 8692 case WRITE_10: { 8693 struct scsi_rw_10 *cdb; 8694 8695 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8696 if (cdb->byte2 & SRW10_FUA) 8697 flags |= CTL_LLF_FUA; 8698 if (cdb->byte2 & SRW10_DPO) 8699 flags |= CTL_LLF_DPO; 8700 lba = scsi_4btoul(cdb->addr); 8701 num_blocks = scsi_2btoul(cdb->length); 8702 break; 8703 } 8704 case WRITE_VERIFY_10: { 8705 struct scsi_write_verify_10 *cdb; 8706 8707 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8708 flags |= CTL_LLF_FUA; 8709 if (cdb->byte2 & SWV_DPO) 8710 flags |= CTL_LLF_DPO; 8711 lba = scsi_4btoul(cdb->addr); 8712 num_blocks = scsi_2btoul(cdb->length); 8713 break; 8714 } 8715 case READ_12: 8716 case WRITE_12: { 8717 struct scsi_rw_12 *cdb; 8718 8719 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8720 if (cdb->byte2 & SRW12_FUA) 8721 flags |= CTL_LLF_FUA; 8722 if (cdb->byte2 & SRW12_DPO) 8723 flags |= CTL_LLF_DPO; 8724 lba = scsi_4btoul(cdb->addr); 8725 num_blocks = scsi_4btoul(cdb->length); 8726 break; 8727 } 8728 case WRITE_VERIFY_12: { 8729 struct scsi_write_verify_12 *cdb; 8730 8731 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8732 flags |= CTL_LLF_FUA; 8733 if (cdb->byte2 & SWV_DPO) 8734 flags |= CTL_LLF_DPO; 8735 lba = scsi_4btoul(cdb->addr); 8736 num_blocks = scsi_4btoul(cdb->length); 8737 break; 8738 } 8739 case READ_16: 8740 case WRITE_16: { 8741 struct scsi_rw_16 *cdb; 8742 8743 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8744 if (cdb->byte2 & SRW12_FUA) 8745 flags |= CTL_LLF_FUA; 8746 if (cdb->byte2 & SRW12_DPO) 8747 flags |= CTL_LLF_DPO; 8748 lba = scsi_8btou64(cdb->addr); 8749 num_blocks = scsi_4btoul(cdb->length); 8750 break; 8751 } 8752 case WRITE_ATOMIC_16: { 8753 struct scsi_write_atomic_16 *cdb; 8754 8755 if (lun->be_lun->atomicblock == 0) { 8756 ctl_set_invalid_opcode(ctsio); 8757 ctl_done((union ctl_io *)ctsio); 8758 return (CTL_RETVAL_COMPLETE); 8759 } 8760 8761 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8762 if (cdb->byte2 & SRW12_FUA) 8763 flags |= CTL_LLF_FUA; 8764 if (cdb->byte2 & SRW12_DPO) 8765 flags |= CTL_LLF_DPO; 8766 lba = scsi_8btou64(cdb->addr); 8767 num_blocks = scsi_2btoul(cdb->length); 8768 if (num_blocks > lun->be_lun->atomicblock) { 8769 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8770 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8771 /*bit*/ 0); 8772 ctl_done((union ctl_io *)ctsio); 8773 return (CTL_RETVAL_COMPLETE); 8774 } 8775 break; 8776 } 8777 case WRITE_VERIFY_16: { 8778 struct scsi_write_verify_16 *cdb; 8779 8780 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8781 flags |= CTL_LLF_FUA; 8782 if (cdb->byte2 & SWV_DPO) 8783 flags |= CTL_LLF_DPO; 8784 lba = scsi_8btou64(cdb->addr); 8785 num_blocks = scsi_4btoul(cdb->length); 8786 break; 8787 } 8788 default: 8789 /* 8790 * We got a command we don't support. This shouldn't 8791 * happen, commands should be filtered out above us. 8792 */ 8793 ctl_set_invalid_opcode(ctsio); 8794 ctl_done((union ctl_io *)ctsio); 8795 8796 return (CTL_RETVAL_COMPLETE); 8797 break; /* NOTREACHED */ 8798 } 8799 8800 /* 8801 * The first check is to make sure we're in bounds, the second 8802 * check is to catch wrap-around problems. If the lba + num blocks 8803 * is less than the lba, then we've wrapped around and the block 8804 * range is invalid anyway. 8805 */ 8806 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8807 || ((lba + num_blocks) < lba)) { 8808 ctl_set_lba_out_of_range(ctsio, 8809 MAX(lba, lun->be_lun->maxlba + 1)); 8810 ctl_done((union ctl_io *)ctsio); 8811 return (CTL_RETVAL_COMPLETE); 8812 } 8813 8814 /* 8815 * According to SBC-3, a transfer length of 0 is not an error. 8816 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8817 * translates to 256 blocks for those commands. 8818 */ 8819 if (num_blocks == 0) { 8820 ctl_set_success(ctsio); 8821 ctl_done((union ctl_io *)ctsio); 8822 return (CTL_RETVAL_COMPLETE); 8823 } 8824 8825 /* Set FUA and/or DPO if caches are disabled. */ 8826 if (isread) { 8827 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8828 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8829 } else { 8830 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8831 flags |= CTL_LLF_FUA; 8832 } 8833 8834 lbalen = (struct ctl_lba_len_flags *) 8835 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8836 lbalen->lba = lba; 8837 lbalen->len = num_blocks; 8838 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8839 8840 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8841 ctsio->kern_rel_offset = 0; 8842 8843 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8844 8845 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8846 return (retval); 8847 } 8848 8849 static int 8850 ctl_cnw_cont(union ctl_io *io) 8851 { 8852 struct ctl_lun *lun = CTL_LUN(io); 8853 struct ctl_scsiio *ctsio; 8854 struct ctl_lba_len_flags *lbalen; 8855 int retval; 8856 8857 ctsio = &io->scsiio; 8858 ctsio->io_hdr.status = CTL_STATUS_NONE; 8859 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8860 lbalen = (struct ctl_lba_len_flags *) 8861 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8862 lbalen->flags &= ~CTL_LLF_COMPARE; 8863 lbalen->flags |= CTL_LLF_WRITE; 8864 8865 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8866 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8867 return (retval); 8868 } 8869 8870 int 8871 ctl_cnw(struct ctl_scsiio *ctsio) 8872 { 8873 struct ctl_lun *lun = CTL_LUN(ctsio); 8874 struct ctl_lba_len_flags *lbalen; 8875 uint64_t lba; 8876 uint32_t num_blocks; 8877 int flags, retval; 8878 8879 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8880 8881 flags = 0; 8882 switch (ctsio->cdb[0]) { 8883 case COMPARE_AND_WRITE: { 8884 struct scsi_compare_and_write *cdb; 8885 8886 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8887 if (cdb->byte2 & SRW10_FUA) 8888 flags |= CTL_LLF_FUA; 8889 if (cdb->byte2 & SRW10_DPO) 8890 flags |= CTL_LLF_DPO; 8891 lba = scsi_8btou64(cdb->addr); 8892 num_blocks = cdb->length; 8893 break; 8894 } 8895 default: 8896 /* 8897 * We got a command we don't support. This shouldn't 8898 * happen, commands should be filtered out above us. 8899 */ 8900 ctl_set_invalid_opcode(ctsio); 8901 ctl_done((union ctl_io *)ctsio); 8902 8903 return (CTL_RETVAL_COMPLETE); 8904 break; /* NOTREACHED */ 8905 } 8906 8907 /* 8908 * The first check is to make sure we're in bounds, the second 8909 * check is to catch wrap-around problems. If the lba + num blocks 8910 * is less than the lba, then we've wrapped around and the block 8911 * range is invalid anyway. 8912 */ 8913 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8914 || ((lba + num_blocks) < lba)) { 8915 ctl_set_lba_out_of_range(ctsio, 8916 MAX(lba, lun->be_lun->maxlba + 1)); 8917 ctl_done((union ctl_io *)ctsio); 8918 return (CTL_RETVAL_COMPLETE); 8919 } 8920 8921 /* 8922 * According to SBC-3, a transfer length of 0 is not an error. 8923 */ 8924 if (num_blocks == 0) { 8925 ctl_set_success(ctsio); 8926 ctl_done((union ctl_io *)ctsio); 8927 return (CTL_RETVAL_COMPLETE); 8928 } 8929 8930 /* Set FUA if write cache is disabled. */ 8931 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8932 flags |= CTL_LLF_FUA; 8933 8934 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8935 ctsio->kern_rel_offset = 0; 8936 8937 /* 8938 * Set the IO_CONT flag, so that if this I/O gets passed to 8939 * ctl_data_submit_done(), it'll get passed back to 8940 * ctl_ctl_cnw_cont() for further processing. 8941 */ 8942 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8943 ctsio->io_cont = ctl_cnw_cont; 8944 8945 lbalen = (struct ctl_lba_len_flags *) 8946 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8947 lbalen->lba = lba; 8948 lbalen->len = num_blocks; 8949 lbalen->flags = CTL_LLF_COMPARE | flags; 8950 8951 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8952 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8953 return (retval); 8954 } 8955 8956 int 8957 ctl_verify(struct ctl_scsiio *ctsio) 8958 { 8959 struct ctl_lun *lun = CTL_LUN(ctsio); 8960 struct ctl_lba_len_flags *lbalen; 8961 uint64_t lba; 8962 uint32_t num_blocks; 8963 int bytchk, flags; 8964 int retval; 8965 8966 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8967 8968 bytchk = 0; 8969 flags = CTL_LLF_FUA; 8970 switch (ctsio->cdb[0]) { 8971 case VERIFY_10: { 8972 struct scsi_verify_10 *cdb; 8973 8974 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8975 if (cdb->byte2 & SVFY_BYTCHK) 8976 bytchk = 1; 8977 if (cdb->byte2 & SVFY_DPO) 8978 flags |= CTL_LLF_DPO; 8979 lba = scsi_4btoul(cdb->addr); 8980 num_blocks = scsi_2btoul(cdb->length); 8981 break; 8982 } 8983 case VERIFY_12: { 8984 struct scsi_verify_12 *cdb; 8985 8986 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8987 if (cdb->byte2 & SVFY_BYTCHK) 8988 bytchk = 1; 8989 if (cdb->byte2 & SVFY_DPO) 8990 flags |= CTL_LLF_DPO; 8991 lba = scsi_4btoul(cdb->addr); 8992 num_blocks = scsi_4btoul(cdb->length); 8993 break; 8994 } 8995 case VERIFY_16: { 8996 struct scsi_rw_16 *cdb; 8997 8998 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8999 if (cdb->byte2 & SVFY_BYTCHK) 9000 bytchk = 1; 9001 if (cdb->byte2 & SVFY_DPO) 9002 flags |= CTL_LLF_DPO; 9003 lba = scsi_8btou64(cdb->addr); 9004 num_blocks = scsi_4btoul(cdb->length); 9005 break; 9006 } 9007 default: 9008 /* 9009 * We got a command we don't support. This shouldn't 9010 * happen, commands should be filtered out above us. 9011 */ 9012 ctl_set_invalid_opcode(ctsio); 9013 ctl_done((union ctl_io *)ctsio); 9014 return (CTL_RETVAL_COMPLETE); 9015 } 9016 9017 /* 9018 * The first check is to make sure we're in bounds, the second 9019 * check is to catch wrap-around problems. If the lba + num blocks 9020 * is less than the lba, then we've wrapped around and the block 9021 * range is invalid anyway. 9022 */ 9023 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9024 || ((lba + num_blocks) < lba)) { 9025 ctl_set_lba_out_of_range(ctsio, 9026 MAX(lba, lun->be_lun->maxlba + 1)); 9027 ctl_done((union ctl_io *)ctsio); 9028 return (CTL_RETVAL_COMPLETE); 9029 } 9030 9031 /* 9032 * According to SBC-3, a transfer length of 0 is not an error. 9033 */ 9034 if (num_blocks == 0) { 9035 ctl_set_success(ctsio); 9036 ctl_done((union ctl_io *)ctsio); 9037 return (CTL_RETVAL_COMPLETE); 9038 } 9039 9040 lbalen = (struct ctl_lba_len_flags *) 9041 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9042 lbalen->lba = lba; 9043 lbalen->len = num_blocks; 9044 if (bytchk) { 9045 lbalen->flags = CTL_LLF_COMPARE | flags; 9046 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9047 } else { 9048 lbalen->flags = CTL_LLF_VERIFY | flags; 9049 ctsio->kern_total_len = 0; 9050 } 9051 ctsio->kern_rel_offset = 0; 9052 9053 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9054 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9055 return (retval); 9056 } 9057 9058 int 9059 ctl_report_luns(struct ctl_scsiio *ctsio) 9060 { 9061 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9062 struct ctl_port *port = CTL_PORT(ctsio); 9063 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9064 struct scsi_report_luns *cdb; 9065 struct scsi_report_luns_data *lun_data; 9066 int num_filled, num_luns, num_port_luns, retval; 9067 uint32_t alloc_len, lun_datalen; 9068 uint32_t initidx, targ_lun_id, lun_id; 9069 9070 retval = CTL_RETVAL_COMPLETE; 9071 cdb = (struct scsi_report_luns *)ctsio->cdb; 9072 9073 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9074 9075 num_luns = 0; 9076 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9077 mtx_lock(&softc->ctl_lock); 9078 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9079 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9080 num_luns++; 9081 } 9082 mtx_unlock(&softc->ctl_lock); 9083 9084 switch (cdb->select_report) { 9085 case RPL_REPORT_DEFAULT: 9086 case RPL_REPORT_ALL: 9087 case RPL_REPORT_NONSUBSID: 9088 break; 9089 case RPL_REPORT_WELLKNOWN: 9090 case RPL_REPORT_ADMIN: 9091 case RPL_REPORT_CONGLOM: 9092 num_luns = 0; 9093 break; 9094 default: 9095 ctl_set_invalid_field(ctsio, 9096 /*sks_valid*/ 1, 9097 /*command*/ 1, 9098 /*field*/ 2, 9099 /*bit_valid*/ 0, 9100 /*bit*/ 0); 9101 ctl_done((union ctl_io *)ctsio); 9102 return (retval); 9103 break; /* NOTREACHED */ 9104 } 9105 9106 alloc_len = scsi_4btoul(cdb->length); 9107 /* 9108 * The initiator has to allocate at least 16 bytes for this request, 9109 * so he can at least get the header and the first LUN. Otherwise 9110 * we reject the request (per SPC-3 rev 14, section 6.21). 9111 */ 9112 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9113 sizeof(struct scsi_report_luns_lundata))) { 9114 ctl_set_invalid_field(ctsio, 9115 /*sks_valid*/ 1, 9116 /*command*/ 1, 9117 /*field*/ 6, 9118 /*bit_valid*/ 0, 9119 /*bit*/ 0); 9120 ctl_done((union ctl_io *)ctsio); 9121 return (retval); 9122 } 9123 9124 lun_datalen = sizeof(*lun_data) + 9125 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9126 9127 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9128 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9129 ctsio->kern_sg_entries = 0; 9130 9131 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9132 9133 mtx_lock(&softc->ctl_lock); 9134 for (targ_lun_id = 0, num_filled = 0; 9135 targ_lun_id < num_port_luns && num_filled < num_luns; 9136 targ_lun_id++) { 9137 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9138 if (lun_id == UINT32_MAX) 9139 continue; 9140 lun = softc->ctl_luns[lun_id]; 9141 if (lun == NULL) 9142 continue; 9143 9144 be64enc(lun_data->luns[num_filled++].lundata, 9145 ctl_encode_lun(targ_lun_id)); 9146 9147 /* 9148 * According to SPC-3, rev 14 section 6.21: 9149 * 9150 * "The execution of a REPORT LUNS command to any valid and 9151 * installed logical unit shall clear the REPORTED LUNS DATA 9152 * HAS CHANGED unit attention condition for all logical 9153 * units of that target with respect to the requesting 9154 * initiator. A valid and installed logical unit is one 9155 * having a PERIPHERAL QUALIFIER of 000b in the standard 9156 * INQUIRY data (see 6.4.2)." 9157 * 9158 * If request_lun is NULL, the LUN this report luns command 9159 * was issued to is either disabled or doesn't exist. In that 9160 * case, we shouldn't clear any pending lun change unit 9161 * attention. 9162 */ 9163 if (request_lun != NULL) { 9164 mtx_lock(&lun->lun_lock); 9165 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9166 mtx_unlock(&lun->lun_lock); 9167 } 9168 } 9169 mtx_unlock(&softc->ctl_lock); 9170 9171 /* 9172 * It's quite possible that we've returned fewer LUNs than we allocated 9173 * space for. Trim it. 9174 */ 9175 lun_datalen = sizeof(*lun_data) + 9176 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9177 ctsio->kern_rel_offset = 0; 9178 ctsio->kern_sg_entries = 0; 9179 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9180 ctsio->kern_total_len = ctsio->kern_data_len; 9181 9182 /* 9183 * We set this to the actual data length, regardless of how much 9184 * space we actually have to return results. If the user looks at 9185 * this value, he'll know whether or not he allocated enough space 9186 * and reissue the command if necessary. We don't support well 9187 * known logical units, so if the user asks for that, return none. 9188 */ 9189 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9190 9191 /* 9192 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9193 * this request. 9194 */ 9195 ctl_set_success(ctsio); 9196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9197 ctsio->be_move_done = ctl_config_move_done; 9198 ctl_datamove((union ctl_io *)ctsio); 9199 return (retval); 9200 } 9201 9202 int 9203 ctl_request_sense(struct ctl_scsiio *ctsio) 9204 { 9205 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9206 struct ctl_lun *lun = CTL_LUN(ctsio); 9207 struct scsi_request_sense *cdb; 9208 struct scsi_sense_data *sense_ptr, *ps; 9209 uint32_t initidx; 9210 int have_error; 9211 u_int sense_len = SSD_FULL_SIZE; 9212 scsi_sense_data_type sense_format; 9213 ctl_ua_type ua_type; 9214 uint8_t asc = 0, ascq = 0; 9215 9216 cdb = (struct scsi_request_sense *)ctsio->cdb; 9217 9218 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9219 9220 /* 9221 * Determine which sense format the user wants. 9222 */ 9223 if (cdb->byte2 & SRS_DESC) 9224 sense_format = SSD_TYPE_DESC; 9225 else 9226 sense_format = SSD_TYPE_FIXED; 9227 9228 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9229 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9230 ctsio->kern_sg_entries = 0; 9231 ctsio->kern_rel_offset = 0; 9232 9233 /* 9234 * struct scsi_sense_data, which is currently set to 256 bytes, is 9235 * larger than the largest allowed value for the length field in the 9236 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9237 */ 9238 ctsio->kern_data_len = cdb->length; 9239 ctsio->kern_total_len = cdb->length; 9240 9241 /* 9242 * If we don't have a LUN, we don't have any pending sense. 9243 */ 9244 if (lun == NULL || 9245 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9246 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9247 /* "Logical unit not supported" */ 9248 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9249 /*current_error*/ 1, 9250 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9251 /*asc*/ 0x25, 9252 /*ascq*/ 0x00, 9253 SSD_ELEM_NONE); 9254 goto send; 9255 } 9256 9257 have_error = 0; 9258 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9259 /* 9260 * Check for pending sense, and then for pending unit attentions. 9261 * Pending sense gets returned first, then pending unit attentions. 9262 */ 9263 mtx_lock(&lun->lun_lock); 9264 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9265 if (ps != NULL) 9266 ps += initidx % CTL_MAX_INIT_PER_PORT; 9267 if (ps != NULL && ps->error_code != 0) { 9268 scsi_sense_data_type stored_format; 9269 9270 /* 9271 * Check to see which sense format was used for the stored 9272 * sense data. 9273 */ 9274 stored_format = scsi_sense_type(ps); 9275 9276 /* 9277 * If the user requested a different sense format than the 9278 * one we stored, then we need to convert it to the other 9279 * format. If we're going from descriptor to fixed format 9280 * sense data, we may lose things in translation, depending 9281 * on what options were used. 9282 * 9283 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9284 * for some reason we'll just copy it out as-is. 9285 */ 9286 if ((stored_format == SSD_TYPE_FIXED) 9287 && (sense_format == SSD_TYPE_DESC)) 9288 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9289 ps, (struct scsi_sense_data_desc *)sense_ptr); 9290 else if ((stored_format == SSD_TYPE_DESC) 9291 && (sense_format == SSD_TYPE_FIXED)) 9292 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9293 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9294 else 9295 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9296 9297 ps->error_code = 0; 9298 have_error = 1; 9299 } else { 9300 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9301 sense_format); 9302 if (ua_type != CTL_UA_NONE) 9303 have_error = 1; 9304 } 9305 if (have_error == 0) { 9306 /* 9307 * Report informational exception if have one and allowed. 9308 */ 9309 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9310 asc = lun->ie_asc; 9311 ascq = lun->ie_ascq; 9312 } 9313 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9314 /*current_error*/ 1, 9315 /*sense_key*/ SSD_KEY_NO_SENSE, 9316 /*asc*/ asc, 9317 /*ascq*/ ascq, 9318 SSD_ELEM_NONE); 9319 } 9320 mtx_unlock(&lun->lun_lock); 9321 9322 send: 9323 /* 9324 * We report the SCSI status as OK, since the status of the command 9325 * itself is OK. We're reporting sense as parameter data. 9326 */ 9327 ctl_set_success(ctsio); 9328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9329 ctsio->be_move_done = ctl_config_move_done; 9330 ctl_datamove((union ctl_io *)ctsio); 9331 return (CTL_RETVAL_COMPLETE); 9332 } 9333 9334 int 9335 ctl_tur(struct ctl_scsiio *ctsio) 9336 { 9337 9338 CTL_DEBUG_PRINT(("ctl_tur\n")); 9339 9340 ctl_set_success(ctsio); 9341 ctl_done((union ctl_io *)ctsio); 9342 9343 return (CTL_RETVAL_COMPLETE); 9344 } 9345 9346 /* 9347 * SCSI VPD page 0x00, the Supported VPD Pages page. 9348 */ 9349 static int 9350 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9351 { 9352 struct ctl_lun *lun = CTL_LUN(ctsio); 9353 struct scsi_vpd_supported_pages *pages; 9354 int sup_page_size; 9355 int p; 9356 9357 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9358 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9359 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9360 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9361 ctsio->kern_rel_offset = 0; 9362 ctsio->kern_sg_entries = 0; 9363 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9364 ctsio->kern_total_len = ctsio->kern_data_len; 9365 9366 /* 9367 * The control device is always connected. The disk device, on the 9368 * other hand, may not be online all the time. Need to change this 9369 * to figure out whether the disk device is actually online or not. 9370 */ 9371 if (lun != NULL) 9372 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9373 lun->be_lun->lun_type; 9374 else 9375 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9376 9377 p = 0; 9378 /* Supported VPD pages */ 9379 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9380 /* Serial Number */ 9381 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9382 /* Device Identification */ 9383 pages->page_list[p++] = SVPD_DEVICE_ID; 9384 /* Extended INQUIRY Data */ 9385 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9386 /* Mode Page Policy */ 9387 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9388 /* SCSI Ports */ 9389 pages->page_list[p++] = SVPD_SCSI_PORTS; 9390 /* Third-party Copy */ 9391 pages->page_list[p++] = SVPD_SCSI_TPC; 9392 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9393 /* Block limits */ 9394 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9395 /* Block Device Characteristics */ 9396 pages->page_list[p++] = SVPD_BDC; 9397 /* Logical Block Provisioning */ 9398 pages->page_list[p++] = SVPD_LBP; 9399 } 9400 pages->length = p; 9401 9402 ctl_set_success(ctsio); 9403 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9404 ctsio->be_move_done = ctl_config_move_done; 9405 ctl_datamove((union ctl_io *)ctsio); 9406 return (CTL_RETVAL_COMPLETE); 9407 } 9408 9409 /* 9410 * SCSI VPD page 0x80, the Unit Serial Number page. 9411 */ 9412 static int 9413 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9414 { 9415 struct ctl_lun *lun = CTL_LUN(ctsio); 9416 struct scsi_vpd_unit_serial_number *sn_ptr; 9417 int data_len; 9418 9419 data_len = 4 + CTL_SN_LEN; 9420 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9421 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9422 ctsio->kern_rel_offset = 0; 9423 ctsio->kern_sg_entries = 0; 9424 ctsio->kern_data_len = min(data_len, alloc_len); 9425 ctsio->kern_total_len = ctsio->kern_data_len; 9426 9427 /* 9428 * The control device is always connected. The disk device, on the 9429 * other hand, may not be online all the time. Need to change this 9430 * to figure out whether the disk device is actually online or not. 9431 */ 9432 if (lun != NULL) 9433 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9434 lun->be_lun->lun_type; 9435 else 9436 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9437 9438 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9439 sn_ptr->length = CTL_SN_LEN; 9440 /* 9441 * If we don't have a LUN, we just leave the serial number as 9442 * all spaces. 9443 */ 9444 if (lun != NULL) { 9445 strncpy((char *)sn_ptr->serial_num, 9446 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9447 } else 9448 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9449 9450 ctl_set_success(ctsio); 9451 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9452 ctsio->be_move_done = ctl_config_move_done; 9453 ctl_datamove((union ctl_io *)ctsio); 9454 return (CTL_RETVAL_COMPLETE); 9455 } 9456 9457 9458 /* 9459 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9460 */ 9461 static int 9462 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9463 { 9464 struct ctl_lun *lun = CTL_LUN(ctsio); 9465 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9466 int data_len; 9467 9468 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9469 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9470 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9471 ctsio->kern_sg_entries = 0; 9472 ctsio->kern_rel_offset = 0; 9473 ctsio->kern_data_len = min(data_len, alloc_len); 9474 ctsio->kern_total_len = ctsio->kern_data_len; 9475 9476 /* 9477 * The control device is always connected. The disk device, on the 9478 * other hand, may not be online all the time. 9479 */ 9480 if (lun != NULL) 9481 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9482 lun->be_lun->lun_type; 9483 else 9484 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9485 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9486 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9487 /* 9488 * We support head of queue, ordered and simple tags. 9489 */ 9490 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9491 /* 9492 * Volatile cache supported. 9493 */ 9494 eid_ptr->flags3 = SVPD_EID_V_SUP; 9495 9496 /* 9497 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9498 * attention for a particular IT nexus on all LUNs once we report 9499 * it to that nexus once. This bit is required as of SPC-4. 9500 */ 9501 eid_ptr->flags4 = SVPD_EID_LUICLR; 9502 9503 /* 9504 * We support revert to defaults (RTD) bit in MODE SELECT. 9505 */ 9506 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9507 9508 /* 9509 * XXX KDM in order to correctly answer this, we would need 9510 * information from the SIM to determine how much sense data it 9511 * can send. So this would really be a path inquiry field, most 9512 * likely. This can be set to a maximum of 252 according to SPC-4, 9513 * but the hardware may or may not be able to support that much. 9514 * 0 just means that the maximum sense data length is not reported. 9515 */ 9516 eid_ptr->max_sense_length = 0; 9517 9518 ctl_set_success(ctsio); 9519 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9520 ctsio->be_move_done = ctl_config_move_done; 9521 ctl_datamove((union ctl_io *)ctsio); 9522 return (CTL_RETVAL_COMPLETE); 9523 } 9524 9525 static int 9526 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9527 { 9528 struct ctl_lun *lun = CTL_LUN(ctsio); 9529 struct scsi_vpd_mode_page_policy *mpp_ptr; 9530 int data_len; 9531 9532 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9533 sizeof(struct scsi_vpd_mode_page_policy_descr); 9534 9535 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9536 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9537 ctsio->kern_rel_offset = 0; 9538 ctsio->kern_sg_entries = 0; 9539 ctsio->kern_data_len = min(data_len, alloc_len); 9540 ctsio->kern_total_len = ctsio->kern_data_len; 9541 9542 /* 9543 * The control device is always connected. The disk device, on the 9544 * other hand, may not be online all the time. 9545 */ 9546 if (lun != NULL) 9547 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9548 lun->be_lun->lun_type; 9549 else 9550 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9551 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9552 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9553 mpp_ptr->descr[0].page_code = 0x3f; 9554 mpp_ptr->descr[0].subpage_code = 0xff; 9555 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9556 9557 ctl_set_success(ctsio); 9558 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9559 ctsio->be_move_done = ctl_config_move_done; 9560 ctl_datamove((union ctl_io *)ctsio); 9561 return (CTL_RETVAL_COMPLETE); 9562 } 9563 9564 /* 9565 * SCSI VPD page 0x83, the Device Identification page. 9566 */ 9567 static int 9568 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9569 { 9570 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9571 struct ctl_port *port = CTL_PORT(ctsio); 9572 struct ctl_lun *lun = CTL_LUN(ctsio); 9573 struct scsi_vpd_device_id *devid_ptr; 9574 struct scsi_vpd_id_descriptor *desc; 9575 int data_len, g; 9576 uint8_t proto; 9577 9578 data_len = sizeof(struct scsi_vpd_device_id) + 9579 sizeof(struct scsi_vpd_id_descriptor) + 9580 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9581 sizeof(struct scsi_vpd_id_descriptor) + 9582 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9583 if (lun && lun->lun_devid) 9584 data_len += lun->lun_devid->len; 9585 if (port && port->port_devid) 9586 data_len += port->port_devid->len; 9587 if (port && port->target_devid) 9588 data_len += port->target_devid->len; 9589 9590 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9591 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9592 ctsio->kern_sg_entries = 0; 9593 ctsio->kern_rel_offset = 0; 9594 ctsio->kern_sg_entries = 0; 9595 ctsio->kern_data_len = min(data_len, alloc_len); 9596 ctsio->kern_total_len = ctsio->kern_data_len; 9597 9598 /* 9599 * The control device is always connected. The disk device, on the 9600 * other hand, may not be online all the time. 9601 */ 9602 if (lun != NULL) 9603 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9604 lun->be_lun->lun_type; 9605 else 9606 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9607 devid_ptr->page_code = SVPD_DEVICE_ID; 9608 scsi_ulto2b(data_len - 4, devid_ptr->length); 9609 9610 if (port && port->port_type == CTL_PORT_FC) 9611 proto = SCSI_PROTO_FC << 4; 9612 else if (port && port->port_type == CTL_PORT_SAS) 9613 proto = SCSI_PROTO_SAS << 4; 9614 else if (port && port->port_type == CTL_PORT_ISCSI) 9615 proto = SCSI_PROTO_ISCSI << 4; 9616 else 9617 proto = SCSI_PROTO_SPI << 4; 9618 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9619 9620 /* 9621 * We're using a LUN association here. i.e., this device ID is a 9622 * per-LUN identifier. 9623 */ 9624 if (lun && lun->lun_devid) { 9625 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9626 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9627 lun->lun_devid->len); 9628 } 9629 9630 /* 9631 * This is for the WWPN which is a port association. 9632 */ 9633 if (port && port->port_devid) { 9634 memcpy(desc, port->port_devid->data, port->port_devid->len); 9635 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9636 port->port_devid->len); 9637 } 9638 9639 /* 9640 * This is for the Relative Target Port(type 4h) identifier 9641 */ 9642 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9643 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9644 SVPD_ID_TYPE_RELTARG; 9645 desc->length = 4; 9646 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9647 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9648 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9649 9650 /* 9651 * This is for the Target Port Group(type 5h) identifier 9652 */ 9653 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9654 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9655 SVPD_ID_TYPE_TPORTGRP; 9656 desc->length = 4; 9657 if (softc->is_single || 9658 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9659 g = 1; 9660 else 9661 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9662 scsi_ulto2b(g, &desc->identifier[2]); 9663 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9664 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9665 9666 /* 9667 * This is for the Target identifier 9668 */ 9669 if (port && port->target_devid) { 9670 memcpy(desc, port->target_devid->data, port->target_devid->len); 9671 } 9672 9673 ctl_set_success(ctsio); 9674 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9675 ctsio->be_move_done = ctl_config_move_done; 9676 ctl_datamove((union ctl_io *)ctsio); 9677 return (CTL_RETVAL_COMPLETE); 9678 } 9679 9680 static int 9681 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9682 { 9683 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9684 struct ctl_lun *lun = CTL_LUN(ctsio); 9685 struct scsi_vpd_scsi_ports *sp; 9686 struct scsi_vpd_port_designation *pd; 9687 struct scsi_vpd_port_designation_cont *pdc; 9688 struct ctl_port *port; 9689 int data_len, num_target_ports, iid_len, id_len; 9690 9691 num_target_ports = 0; 9692 iid_len = 0; 9693 id_len = 0; 9694 mtx_lock(&softc->ctl_lock); 9695 STAILQ_FOREACH(port, &softc->port_list, links) { 9696 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9697 continue; 9698 if (lun != NULL && 9699 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9700 continue; 9701 num_target_ports++; 9702 if (port->init_devid) 9703 iid_len += port->init_devid->len; 9704 if (port->port_devid) 9705 id_len += port->port_devid->len; 9706 } 9707 mtx_unlock(&softc->ctl_lock); 9708 9709 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9710 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9711 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9712 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9713 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9714 ctsio->kern_sg_entries = 0; 9715 ctsio->kern_rel_offset = 0; 9716 ctsio->kern_sg_entries = 0; 9717 ctsio->kern_data_len = min(data_len, alloc_len); 9718 ctsio->kern_total_len = ctsio->kern_data_len; 9719 9720 /* 9721 * The control device is always connected. The disk device, on the 9722 * other hand, may not be online all the time. Need to change this 9723 * to figure out whether the disk device is actually online or not. 9724 */ 9725 if (lun != NULL) 9726 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9727 lun->be_lun->lun_type; 9728 else 9729 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9730 9731 sp->page_code = SVPD_SCSI_PORTS; 9732 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9733 sp->page_length); 9734 pd = &sp->design[0]; 9735 9736 mtx_lock(&softc->ctl_lock); 9737 STAILQ_FOREACH(port, &softc->port_list, links) { 9738 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9739 continue; 9740 if (lun != NULL && 9741 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9742 continue; 9743 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9744 if (port->init_devid) { 9745 iid_len = port->init_devid->len; 9746 memcpy(pd->initiator_transportid, 9747 port->init_devid->data, port->init_devid->len); 9748 } else 9749 iid_len = 0; 9750 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9751 pdc = (struct scsi_vpd_port_designation_cont *) 9752 (&pd->initiator_transportid[iid_len]); 9753 if (port->port_devid) { 9754 id_len = port->port_devid->len; 9755 memcpy(pdc->target_port_descriptors, 9756 port->port_devid->data, port->port_devid->len); 9757 } else 9758 id_len = 0; 9759 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9760 pd = (struct scsi_vpd_port_designation *) 9761 ((uint8_t *)pdc->target_port_descriptors + id_len); 9762 } 9763 mtx_unlock(&softc->ctl_lock); 9764 9765 ctl_set_success(ctsio); 9766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9767 ctsio->be_move_done = ctl_config_move_done; 9768 ctl_datamove((union ctl_io *)ctsio); 9769 return (CTL_RETVAL_COMPLETE); 9770 } 9771 9772 static int 9773 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9774 { 9775 struct ctl_lun *lun = CTL_LUN(ctsio); 9776 struct scsi_vpd_block_limits *bl_ptr; 9777 const char *val; 9778 uint64_t ival; 9779 9780 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9781 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9782 ctsio->kern_sg_entries = 0; 9783 ctsio->kern_rel_offset = 0; 9784 ctsio->kern_sg_entries = 0; 9785 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9786 ctsio->kern_total_len = ctsio->kern_data_len; 9787 9788 /* 9789 * The control device is always connected. The disk device, on the 9790 * other hand, may not be online all the time. Need to change this 9791 * to figure out whether the disk device is actually online or not. 9792 */ 9793 if (lun != NULL) 9794 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9795 lun->be_lun->lun_type; 9796 else 9797 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9798 9799 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9800 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9801 bl_ptr->max_cmp_write_len = 0xff; 9802 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9803 if (lun != NULL) { 9804 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9805 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9806 ival = 0xffffffff; 9807 val = dnvlist_get_string(lun->be_lun->options, 9808 "unmap_max_lba", NULL); 9809 if (val != NULL) 9810 ctl_expand_number(val, &ival); 9811 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9812 ival = 0xffffffff; 9813 val = dnvlist_get_string(lun->be_lun->options, 9814 "unmap_max_descr", NULL); 9815 if (val != NULL) 9816 ctl_expand_number(val, &ival); 9817 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9818 if (lun->be_lun->ublockexp != 0) { 9819 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9820 bl_ptr->opt_unmap_grain); 9821 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9822 bl_ptr->unmap_grain_align); 9823 } 9824 } 9825 scsi_ulto4b(lun->be_lun->atomicblock, 9826 bl_ptr->max_atomic_transfer_length); 9827 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9828 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9829 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9830 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9831 ival = UINT64_MAX; 9832 val = dnvlist_get_string(lun->be_lun->options, 9833 "write_same_max_lba", NULL); 9834 if (val != NULL) 9835 ctl_expand_number(val, &ival); 9836 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9837 } 9838 9839 ctl_set_success(ctsio); 9840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9841 ctsio->be_move_done = ctl_config_move_done; 9842 ctl_datamove((union ctl_io *)ctsio); 9843 return (CTL_RETVAL_COMPLETE); 9844 } 9845 9846 static int 9847 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9848 { 9849 struct ctl_lun *lun = CTL_LUN(ctsio); 9850 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9851 const char *value; 9852 u_int i; 9853 9854 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9855 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9856 ctsio->kern_sg_entries = 0; 9857 ctsio->kern_rel_offset = 0; 9858 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 9859 ctsio->kern_total_len = ctsio->kern_data_len; 9860 9861 /* 9862 * The control device is always connected. The disk device, on the 9863 * other hand, may not be online all the time. Need to change this 9864 * to figure out whether the disk device is actually online or not. 9865 */ 9866 if (lun != NULL) 9867 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9868 lun->be_lun->lun_type; 9869 else 9870 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9871 bdc_ptr->page_code = SVPD_BDC; 9872 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9873 if (lun != NULL && 9874 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 9875 i = strtol(value, NULL, 0); 9876 else 9877 i = CTL_DEFAULT_ROTATION_RATE; 9878 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9879 if (lun != NULL && 9880 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 9881 i = strtol(value, NULL, 0); 9882 else 9883 i = 0; 9884 bdc_ptr->wab_wac_ff = (i & 0x0f); 9885 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9886 9887 ctl_set_success(ctsio); 9888 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9889 ctsio->be_move_done = ctl_config_move_done; 9890 ctl_datamove((union ctl_io *)ctsio); 9891 return (CTL_RETVAL_COMPLETE); 9892 } 9893 9894 static int 9895 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9896 { 9897 struct ctl_lun *lun = CTL_LUN(ctsio); 9898 struct scsi_vpd_logical_block_prov *lbp_ptr; 9899 const char *value; 9900 9901 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9902 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9903 ctsio->kern_sg_entries = 0; 9904 ctsio->kern_rel_offset = 0; 9905 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 9906 ctsio->kern_total_len = ctsio->kern_data_len; 9907 9908 /* 9909 * The control device is always connected. The disk device, on the 9910 * other hand, may not be online all the time. Need to change this 9911 * to figure out whether the disk device is actually online or not. 9912 */ 9913 if (lun != NULL) 9914 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9915 lun->be_lun->lun_type; 9916 else 9917 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9918 9919 lbp_ptr->page_code = SVPD_LBP; 9920 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9921 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9922 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9923 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9924 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9925 value = dnvlist_get_string(lun->be_lun->options, 9926 "provisioning_type", NULL); 9927 if (value != NULL) { 9928 if (strcmp(value, "resource") == 0) 9929 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 9930 else if (strcmp(value, "thin") == 0) 9931 lbp_ptr->prov_type = SVPD_LBP_THIN; 9932 } else 9933 lbp_ptr->prov_type = SVPD_LBP_THIN; 9934 } 9935 9936 ctl_set_success(ctsio); 9937 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9938 ctsio->be_move_done = ctl_config_move_done; 9939 ctl_datamove((union ctl_io *)ctsio); 9940 return (CTL_RETVAL_COMPLETE); 9941 } 9942 9943 /* 9944 * INQUIRY with the EVPD bit set. 9945 */ 9946 static int 9947 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9948 { 9949 struct ctl_lun *lun = CTL_LUN(ctsio); 9950 struct scsi_inquiry *cdb; 9951 int alloc_len, retval; 9952 9953 cdb = (struct scsi_inquiry *)ctsio->cdb; 9954 alloc_len = scsi_2btoul(cdb->length); 9955 9956 switch (cdb->page_code) { 9957 case SVPD_SUPPORTED_PAGES: 9958 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9959 break; 9960 case SVPD_UNIT_SERIAL_NUMBER: 9961 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9962 break; 9963 case SVPD_DEVICE_ID: 9964 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9965 break; 9966 case SVPD_EXTENDED_INQUIRY_DATA: 9967 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9968 break; 9969 case SVPD_MODE_PAGE_POLICY: 9970 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 9971 break; 9972 case SVPD_SCSI_PORTS: 9973 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 9974 break; 9975 case SVPD_SCSI_TPC: 9976 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 9977 break; 9978 case SVPD_BLOCK_LIMITS: 9979 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9980 goto err; 9981 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9982 break; 9983 case SVPD_BDC: 9984 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9985 goto err; 9986 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 9987 break; 9988 case SVPD_LBP: 9989 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9990 goto err; 9991 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9992 break; 9993 default: 9994 err: 9995 ctl_set_invalid_field(ctsio, 9996 /*sks_valid*/ 1, 9997 /*command*/ 1, 9998 /*field*/ 2, 9999 /*bit_valid*/ 0, 10000 /*bit*/ 0); 10001 ctl_done((union ctl_io *)ctsio); 10002 retval = CTL_RETVAL_COMPLETE; 10003 break; 10004 } 10005 10006 return (retval); 10007 } 10008 10009 /* 10010 * Standard INQUIRY data. 10011 */ 10012 static int 10013 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10014 { 10015 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10016 struct ctl_port *port = CTL_PORT(ctsio); 10017 struct ctl_lun *lun = CTL_LUN(ctsio); 10018 struct scsi_inquiry_data *inq_ptr; 10019 struct scsi_inquiry *cdb; 10020 const char *val; 10021 uint32_t alloc_len, data_len; 10022 ctl_port_type port_type; 10023 10024 port_type = port->port_type; 10025 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10026 port_type = CTL_PORT_SCSI; 10027 10028 cdb = (struct scsi_inquiry *)ctsio->cdb; 10029 alloc_len = scsi_2btoul(cdb->length); 10030 10031 /* 10032 * We malloc the full inquiry data size here and fill it 10033 * in. If the user only asks for less, we'll give him 10034 * that much. 10035 */ 10036 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10037 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10038 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10039 ctsio->kern_sg_entries = 0; 10040 ctsio->kern_rel_offset = 0; 10041 ctsio->kern_data_len = min(data_len, alloc_len); 10042 ctsio->kern_total_len = ctsio->kern_data_len; 10043 10044 if (lun != NULL) { 10045 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10046 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10047 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10048 lun->be_lun->lun_type; 10049 } else { 10050 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10051 lun->be_lun->lun_type; 10052 } 10053 if (lun->flags & CTL_LUN_REMOVABLE) 10054 inq_ptr->dev_qual2 |= SID_RMB; 10055 } else 10056 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10057 10058 /* RMB in byte 2 is 0 */ 10059 inq_ptr->version = SCSI_REV_SPC5; 10060 10061 /* 10062 * According to SAM-3, even if a device only supports a single 10063 * level of LUN addressing, it should still set the HISUP bit: 10064 * 10065 * 4.9.1 Logical unit numbers overview 10066 * 10067 * All logical unit number formats described in this standard are 10068 * hierarchical in structure even when only a single level in that 10069 * hierarchy is used. The HISUP bit shall be set to one in the 10070 * standard INQUIRY data (see SPC-2) when any logical unit number 10071 * format described in this standard is used. Non-hierarchical 10072 * formats are outside the scope of this standard. 10073 * 10074 * Therefore we set the HiSup bit here. 10075 * 10076 * The response format is 2, per SPC-3. 10077 */ 10078 inq_ptr->response_format = SID_HiSup | 2; 10079 10080 inq_ptr->additional_length = data_len - 10081 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10082 CTL_DEBUG_PRINT(("additional_length = %d\n", 10083 inq_ptr->additional_length)); 10084 10085 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10086 if (port_type == CTL_PORT_SCSI) 10087 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10088 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10089 inq_ptr->flags = SID_CmdQue; 10090 if (port_type == CTL_PORT_SCSI) 10091 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10092 10093 /* 10094 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10095 * We have 8 bytes for the vendor name, and 16 bytes for the device 10096 * name and 4 bytes for the revision. 10097 */ 10098 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10099 "vendor", NULL)) == NULL) { 10100 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10101 } else { 10102 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10103 strncpy(inq_ptr->vendor, val, 10104 min(sizeof(inq_ptr->vendor), strlen(val))); 10105 } 10106 if (lun == NULL) { 10107 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10108 sizeof(inq_ptr->product)); 10109 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10110 NULL)) == NULL) { 10111 switch (lun->be_lun->lun_type) { 10112 case T_DIRECT: 10113 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10114 sizeof(inq_ptr->product)); 10115 break; 10116 case T_PROCESSOR: 10117 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10118 sizeof(inq_ptr->product)); 10119 break; 10120 case T_CDROM: 10121 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10122 sizeof(inq_ptr->product)); 10123 break; 10124 default: 10125 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10126 sizeof(inq_ptr->product)); 10127 break; 10128 } 10129 } else { 10130 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10131 strncpy(inq_ptr->product, val, 10132 min(sizeof(inq_ptr->product), strlen(val))); 10133 } 10134 10135 /* 10136 * XXX make this a macro somewhere so it automatically gets 10137 * incremented when we make changes. 10138 */ 10139 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10140 "revision", NULL)) == NULL) { 10141 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10142 } else { 10143 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10144 strncpy(inq_ptr->revision, val, 10145 min(sizeof(inq_ptr->revision), strlen(val))); 10146 } 10147 10148 /* 10149 * For parallel SCSI, we support double transition and single 10150 * transition clocking. We also support QAS (Quick Arbitration 10151 * and Selection) and Information Unit transfers on both the 10152 * control and array devices. 10153 */ 10154 if (port_type == CTL_PORT_SCSI) 10155 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10156 SID_SPI_IUS; 10157 10158 /* SAM-6 (no version claimed) */ 10159 scsi_ulto2b(0x00C0, inq_ptr->version1); 10160 /* SPC-5 (no version claimed) */ 10161 scsi_ulto2b(0x05C0, inq_ptr->version2); 10162 if (port_type == CTL_PORT_FC) { 10163 /* FCP-2 ANSI INCITS.350:2003 */ 10164 scsi_ulto2b(0x0917, inq_ptr->version3); 10165 } else if (port_type == CTL_PORT_SCSI) { 10166 /* SPI-4 ANSI INCITS.362:200x */ 10167 scsi_ulto2b(0x0B56, inq_ptr->version3); 10168 } else if (port_type == CTL_PORT_ISCSI) { 10169 /* iSCSI (no version claimed) */ 10170 scsi_ulto2b(0x0960, inq_ptr->version3); 10171 } else if (port_type == CTL_PORT_SAS) { 10172 /* SAS (no version claimed) */ 10173 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10174 } else if (port_type == CTL_PORT_UMASS) { 10175 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10176 scsi_ulto2b(0x1730, inq_ptr->version3); 10177 } 10178 10179 if (lun == NULL) { 10180 /* SBC-4 (no version claimed) */ 10181 scsi_ulto2b(0x0600, inq_ptr->version4); 10182 } else { 10183 switch (lun->be_lun->lun_type) { 10184 case T_DIRECT: 10185 /* SBC-4 (no version claimed) */ 10186 scsi_ulto2b(0x0600, inq_ptr->version4); 10187 break; 10188 case T_PROCESSOR: 10189 break; 10190 case T_CDROM: 10191 /* MMC-6 (no version claimed) */ 10192 scsi_ulto2b(0x04E0, inq_ptr->version4); 10193 break; 10194 default: 10195 break; 10196 } 10197 } 10198 10199 ctl_set_success(ctsio); 10200 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10201 ctsio->be_move_done = ctl_config_move_done; 10202 ctl_datamove((union ctl_io *)ctsio); 10203 return (CTL_RETVAL_COMPLETE); 10204 } 10205 10206 int 10207 ctl_inquiry(struct ctl_scsiio *ctsio) 10208 { 10209 struct scsi_inquiry *cdb; 10210 int retval; 10211 10212 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10213 10214 cdb = (struct scsi_inquiry *)ctsio->cdb; 10215 if (cdb->byte2 & SI_EVPD) 10216 retval = ctl_inquiry_evpd(ctsio); 10217 else if (cdb->page_code == 0) 10218 retval = ctl_inquiry_std(ctsio); 10219 else { 10220 ctl_set_invalid_field(ctsio, 10221 /*sks_valid*/ 1, 10222 /*command*/ 1, 10223 /*field*/ 2, 10224 /*bit_valid*/ 0, 10225 /*bit*/ 0); 10226 ctl_done((union ctl_io *)ctsio); 10227 return (CTL_RETVAL_COMPLETE); 10228 } 10229 10230 return (retval); 10231 } 10232 10233 int 10234 ctl_get_config(struct ctl_scsiio *ctsio) 10235 { 10236 struct ctl_lun *lun = CTL_LUN(ctsio); 10237 struct scsi_get_config_header *hdr; 10238 struct scsi_get_config_feature *feature; 10239 struct scsi_get_config *cdb; 10240 uint32_t alloc_len, data_len; 10241 int rt, starting; 10242 10243 cdb = (struct scsi_get_config *)ctsio->cdb; 10244 rt = (cdb->rt & SGC_RT_MASK); 10245 starting = scsi_2btoul(cdb->starting_feature); 10246 alloc_len = scsi_2btoul(cdb->length); 10247 10248 data_len = sizeof(struct scsi_get_config_header) + 10249 sizeof(struct scsi_get_config_feature) + 8 + 10250 sizeof(struct scsi_get_config_feature) + 8 + 10251 sizeof(struct scsi_get_config_feature) + 4 + 10252 sizeof(struct scsi_get_config_feature) + 4 + 10253 sizeof(struct scsi_get_config_feature) + 8 + 10254 sizeof(struct scsi_get_config_feature) + 10255 sizeof(struct scsi_get_config_feature) + 4 + 10256 sizeof(struct scsi_get_config_feature) + 4 + 10257 sizeof(struct scsi_get_config_feature) + 4 + 10258 sizeof(struct scsi_get_config_feature) + 4 + 10259 sizeof(struct scsi_get_config_feature) + 4 + 10260 sizeof(struct scsi_get_config_feature) + 4; 10261 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10262 ctsio->kern_sg_entries = 0; 10263 ctsio->kern_rel_offset = 0; 10264 10265 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10266 if (lun->flags & CTL_LUN_NO_MEDIA) 10267 scsi_ulto2b(0x0000, hdr->current_profile); 10268 else 10269 scsi_ulto2b(0x0010, hdr->current_profile); 10270 feature = (struct scsi_get_config_feature *)(hdr + 1); 10271 10272 if (starting > 0x003b) 10273 goto done; 10274 if (starting > 0x003a) 10275 goto f3b; 10276 if (starting > 0x002b) 10277 goto f3a; 10278 if (starting > 0x002a) 10279 goto f2b; 10280 if (starting > 0x001f) 10281 goto f2a; 10282 if (starting > 0x001e) 10283 goto f1f; 10284 if (starting > 0x001d) 10285 goto f1e; 10286 if (starting > 0x0010) 10287 goto f1d; 10288 if (starting > 0x0003) 10289 goto f10; 10290 if (starting > 0x0002) 10291 goto f3; 10292 if (starting > 0x0001) 10293 goto f2; 10294 if (starting > 0x0000) 10295 goto f1; 10296 10297 /* Profile List */ 10298 scsi_ulto2b(0x0000, feature->feature_code); 10299 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10300 feature->add_length = 8; 10301 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10302 feature->feature_data[2] = 0x00; 10303 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10304 feature->feature_data[6] = 0x01; 10305 feature = (struct scsi_get_config_feature *) 10306 &feature->feature_data[feature->add_length]; 10307 10308 f1: /* Core */ 10309 scsi_ulto2b(0x0001, feature->feature_code); 10310 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10311 feature->add_length = 8; 10312 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10313 feature->feature_data[4] = 0x03; 10314 feature = (struct scsi_get_config_feature *) 10315 &feature->feature_data[feature->add_length]; 10316 10317 f2: /* Morphing */ 10318 scsi_ulto2b(0x0002, feature->feature_code); 10319 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10320 feature->add_length = 4; 10321 feature->feature_data[0] = 0x02; 10322 feature = (struct scsi_get_config_feature *) 10323 &feature->feature_data[feature->add_length]; 10324 10325 f3: /* Removable Medium */ 10326 scsi_ulto2b(0x0003, feature->feature_code); 10327 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10328 feature->add_length = 4; 10329 feature->feature_data[0] = 0x39; 10330 feature = (struct scsi_get_config_feature *) 10331 &feature->feature_data[feature->add_length]; 10332 10333 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10334 goto done; 10335 10336 f10: /* Random Read */ 10337 scsi_ulto2b(0x0010, feature->feature_code); 10338 feature->flags = 0x00; 10339 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10340 feature->flags |= SGC_F_CURRENT; 10341 feature->add_length = 8; 10342 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10343 scsi_ulto2b(1, &feature->feature_data[4]); 10344 feature->feature_data[6] = 0x00; 10345 feature = (struct scsi_get_config_feature *) 10346 &feature->feature_data[feature->add_length]; 10347 10348 f1d: /* Multi-Read */ 10349 scsi_ulto2b(0x001D, feature->feature_code); 10350 feature->flags = 0x00; 10351 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10352 feature->flags |= SGC_F_CURRENT; 10353 feature->add_length = 0; 10354 feature = (struct scsi_get_config_feature *) 10355 &feature->feature_data[feature->add_length]; 10356 10357 f1e: /* CD Read */ 10358 scsi_ulto2b(0x001E, feature->feature_code); 10359 feature->flags = 0x00; 10360 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10361 feature->flags |= SGC_F_CURRENT; 10362 feature->add_length = 4; 10363 feature->feature_data[0] = 0x00; 10364 feature = (struct scsi_get_config_feature *) 10365 &feature->feature_data[feature->add_length]; 10366 10367 f1f: /* DVD Read */ 10368 scsi_ulto2b(0x001F, feature->feature_code); 10369 feature->flags = 0x08; 10370 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10371 feature->flags |= SGC_F_CURRENT; 10372 feature->add_length = 4; 10373 feature->feature_data[0] = 0x01; 10374 feature->feature_data[2] = 0x03; 10375 feature = (struct scsi_get_config_feature *) 10376 &feature->feature_data[feature->add_length]; 10377 10378 f2a: /* DVD+RW */ 10379 scsi_ulto2b(0x002A, feature->feature_code); 10380 feature->flags = 0x04; 10381 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10382 feature->flags |= SGC_F_CURRENT; 10383 feature->add_length = 4; 10384 feature->feature_data[0] = 0x00; 10385 feature->feature_data[1] = 0x00; 10386 feature = (struct scsi_get_config_feature *) 10387 &feature->feature_data[feature->add_length]; 10388 10389 f2b: /* DVD+R */ 10390 scsi_ulto2b(0x002B, feature->feature_code); 10391 feature->flags = 0x00; 10392 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10393 feature->flags |= SGC_F_CURRENT; 10394 feature->add_length = 4; 10395 feature->feature_data[0] = 0x00; 10396 feature = (struct scsi_get_config_feature *) 10397 &feature->feature_data[feature->add_length]; 10398 10399 f3a: /* DVD+RW Dual Layer */ 10400 scsi_ulto2b(0x003A, 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 = 4; 10405 feature->feature_data[0] = 0x00; 10406 feature->feature_data[1] = 0x00; 10407 feature = (struct scsi_get_config_feature *) 10408 &feature->feature_data[feature->add_length]; 10409 10410 f3b: /* DVD+R Dual Layer */ 10411 scsi_ulto2b(0x003B, feature->feature_code); 10412 feature->flags = 0x00; 10413 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10414 feature->flags |= SGC_F_CURRENT; 10415 feature->add_length = 4; 10416 feature->feature_data[0] = 0x00; 10417 feature = (struct scsi_get_config_feature *) 10418 &feature->feature_data[feature->add_length]; 10419 10420 done: 10421 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10422 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10423 feature = (struct scsi_get_config_feature *)(hdr + 1); 10424 if (scsi_2btoul(feature->feature_code) == starting) 10425 feature = (struct scsi_get_config_feature *) 10426 &feature->feature_data[feature->add_length]; 10427 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10428 } 10429 scsi_ulto4b(data_len - 4, hdr->data_length); 10430 ctsio->kern_data_len = min(data_len, alloc_len); 10431 ctsio->kern_total_len = ctsio->kern_data_len; 10432 10433 ctl_set_success(ctsio); 10434 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10435 ctsio->be_move_done = ctl_config_move_done; 10436 ctl_datamove((union ctl_io *)ctsio); 10437 return (CTL_RETVAL_COMPLETE); 10438 } 10439 10440 int 10441 ctl_get_event_status(struct ctl_scsiio *ctsio) 10442 { 10443 struct scsi_get_event_status_header *hdr; 10444 struct scsi_get_event_status *cdb; 10445 uint32_t alloc_len, data_len; 10446 10447 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10448 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10449 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10450 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10451 ctl_done((union ctl_io *)ctsio); 10452 return (CTL_RETVAL_COMPLETE); 10453 } 10454 alloc_len = scsi_2btoul(cdb->length); 10455 10456 data_len = sizeof(struct scsi_get_event_status_header); 10457 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10458 ctsio->kern_sg_entries = 0; 10459 ctsio->kern_rel_offset = 0; 10460 ctsio->kern_data_len = min(data_len, alloc_len); 10461 ctsio->kern_total_len = ctsio->kern_data_len; 10462 10463 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10464 scsi_ulto2b(0, hdr->descr_length); 10465 hdr->nea_class = SGESN_NEA; 10466 hdr->supported_class = 0; 10467 10468 ctl_set_success(ctsio); 10469 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10470 ctsio->be_move_done = ctl_config_move_done; 10471 ctl_datamove((union ctl_io *)ctsio); 10472 return (CTL_RETVAL_COMPLETE); 10473 } 10474 10475 int 10476 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10477 { 10478 struct scsi_mechanism_status_header *hdr; 10479 struct scsi_mechanism_status *cdb; 10480 uint32_t alloc_len, data_len; 10481 10482 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10483 alloc_len = scsi_2btoul(cdb->length); 10484 10485 data_len = sizeof(struct scsi_mechanism_status_header); 10486 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10487 ctsio->kern_sg_entries = 0; 10488 ctsio->kern_rel_offset = 0; 10489 ctsio->kern_data_len = min(data_len, alloc_len); 10490 ctsio->kern_total_len = ctsio->kern_data_len; 10491 10492 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10493 hdr->state1 = 0x00; 10494 hdr->state2 = 0xe0; 10495 scsi_ulto3b(0, hdr->lba); 10496 hdr->slots_num = 0; 10497 scsi_ulto2b(0, hdr->slots_length); 10498 10499 ctl_set_success(ctsio); 10500 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10501 ctsio->be_move_done = ctl_config_move_done; 10502 ctl_datamove((union ctl_io *)ctsio); 10503 return (CTL_RETVAL_COMPLETE); 10504 } 10505 10506 static void 10507 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10508 { 10509 10510 lba += 150; 10511 buf[0] = 0; 10512 buf[1] = bin2bcd((lba / 75) / 60); 10513 buf[2] = bin2bcd((lba / 75) % 60); 10514 buf[3] = bin2bcd(lba % 75); 10515 } 10516 10517 int 10518 ctl_read_toc(struct ctl_scsiio *ctsio) 10519 { 10520 struct ctl_lun *lun = CTL_LUN(ctsio); 10521 struct scsi_read_toc_hdr *hdr; 10522 struct scsi_read_toc_type01_descr *descr; 10523 struct scsi_read_toc *cdb; 10524 uint32_t alloc_len, data_len; 10525 int format, msf; 10526 10527 cdb = (struct scsi_read_toc *)ctsio->cdb; 10528 msf = (cdb->byte2 & CD_MSF) != 0; 10529 format = cdb->format; 10530 alloc_len = scsi_2btoul(cdb->data_len); 10531 10532 data_len = sizeof(struct scsi_read_toc_hdr); 10533 if (format == 0) 10534 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10535 else 10536 data_len += sizeof(struct scsi_read_toc_type01_descr); 10537 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10538 ctsio->kern_sg_entries = 0; 10539 ctsio->kern_rel_offset = 0; 10540 ctsio->kern_data_len = min(data_len, alloc_len); 10541 ctsio->kern_total_len = ctsio->kern_data_len; 10542 10543 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10544 if (format == 0) { 10545 scsi_ulto2b(0x12, hdr->data_length); 10546 hdr->first = 1; 10547 hdr->last = 1; 10548 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10549 descr->addr_ctl = 0x14; 10550 descr->track_number = 1; 10551 if (msf) 10552 ctl_ultomsf(0, descr->track_start); 10553 else 10554 scsi_ulto4b(0, descr->track_start); 10555 descr++; 10556 descr->addr_ctl = 0x14; 10557 descr->track_number = 0xaa; 10558 if (msf) 10559 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10560 else 10561 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10562 } else { 10563 scsi_ulto2b(0x0a, hdr->data_length); 10564 hdr->first = 1; 10565 hdr->last = 1; 10566 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10567 descr->addr_ctl = 0x14; 10568 descr->track_number = 1; 10569 if (msf) 10570 ctl_ultomsf(0, descr->track_start); 10571 else 10572 scsi_ulto4b(0, descr->track_start); 10573 } 10574 10575 ctl_set_success(ctsio); 10576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10577 ctsio->be_move_done = ctl_config_move_done; 10578 ctl_datamove((union ctl_io *)ctsio); 10579 return (CTL_RETVAL_COMPLETE); 10580 } 10581 10582 /* 10583 * For known CDB types, parse the LBA and length. 10584 */ 10585 static int 10586 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10587 { 10588 if (io->io_hdr.io_type != CTL_IO_SCSI) 10589 return (1); 10590 10591 switch (io->scsiio.cdb[0]) { 10592 case COMPARE_AND_WRITE: { 10593 struct scsi_compare_and_write *cdb; 10594 10595 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10596 10597 *lba = scsi_8btou64(cdb->addr); 10598 *len = cdb->length; 10599 break; 10600 } 10601 case READ_6: 10602 case WRITE_6: { 10603 struct scsi_rw_6 *cdb; 10604 10605 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10606 10607 *lba = scsi_3btoul(cdb->addr); 10608 /* only 5 bits are valid in the most significant address byte */ 10609 *lba &= 0x1fffff; 10610 *len = cdb->length; 10611 break; 10612 } 10613 case READ_10: 10614 case WRITE_10: { 10615 struct scsi_rw_10 *cdb; 10616 10617 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10618 10619 *lba = scsi_4btoul(cdb->addr); 10620 *len = scsi_2btoul(cdb->length); 10621 break; 10622 } 10623 case WRITE_VERIFY_10: { 10624 struct scsi_write_verify_10 *cdb; 10625 10626 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10627 10628 *lba = scsi_4btoul(cdb->addr); 10629 *len = scsi_2btoul(cdb->length); 10630 break; 10631 } 10632 case READ_12: 10633 case WRITE_12: { 10634 struct scsi_rw_12 *cdb; 10635 10636 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10637 10638 *lba = scsi_4btoul(cdb->addr); 10639 *len = scsi_4btoul(cdb->length); 10640 break; 10641 } 10642 case WRITE_VERIFY_12: { 10643 struct scsi_write_verify_12 *cdb; 10644 10645 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10646 10647 *lba = scsi_4btoul(cdb->addr); 10648 *len = scsi_4btoul(cdb->length); 10649 break; 10650 } 10651 case READ_16: 10652 case WRITE_16: { 10653 struct scsi_rw_16 *cdb; 10654 10655 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10656 10657 *lba = scsi_8btou64(cdb->addr); 10658 *len = scsi_4btoul(cdb->length); 10659 break; 10660 } 10661 case WRITE_ATOMIC_16: { 10662 struct scsi_write_atomic_16 *cdb; 10663 10664 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10665 10666 *lba = scsi_8btou64(cdb->addr); 10667 *len = scsi_2btoul(cdb->length); 10668 break; 10669 } 10670 case WRITE_VERIFY_16: { 10671 struct scsi_write_verify_16 *cdb; 10672 10673 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10674 10675 *lba = scsi_8btou64(cdb->addr); 10676 *len = scsi_4btoul(cdb->length); 10677 break; 10678 } 10679 case WRITE_SAME_10: { 10680 struct scsi_write_same_10 *cdb; 10681 10682 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10683 10684 *lba = scsi_4btoul(cdb->addr); 10685 *len = scsi_2btoul(cdb->length); 10686 break; 10687 } 10688 case WRITE_SAME_16: { 10689 struct scsi_write_same_16 *cdb; 10690 10691 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10692 10693 *lba = scsi_8btou64(cdb->addr); 10694 *len = scsi_4btoul(cdb->length); 10695 break; 10696 } 10697 case VERIFY_10: { 10698 struct scsi_verify_10 *cdb; 10699 10700 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10701 10702 *lba = scsi_4btoul(cdb->addr); 10703 *len = scsi_2btoul(cdb->length); 10704 break; 10705 } 10706 case VERIFY_12: { 10707 struct scsi_verify_12 *cdb; 10708 10709 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10710 10711 *lba = scsi_4btoul(cdb->addr); 10712 *len = scsi_4btoul(cdb->length); 10713 break; 10714 } 10715 case VERIFY_16: { 10716 struct scsi_verify_16 *cdb; 10717 10718 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10719 10720 *lba = scsi_8btou64(cdb->addr); 10721 *len = scsi_4btoul(cdb->length); 10722 break; 10723 } 10724 case UNMAP: { 10725 *lba = 0; 10726 *len = UINT64_MAX; 10727 break; 10728 } 10729 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10730 struct scsi_get_lba_status *cdb; 10731 10732 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10733 *lba = scsi_8btou64(cdb->addr); 10734 *len = UINT32_MAX; 10735 break; 10736 } 10737 default: 10738 return (1); 10739 break; /* NOTREACHED */ 10740 } 10741 10742 return (0); 10743 } 10744 10745 static ctl_action 10746 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10747 bool seq) 10748 { 10749 uint64_t endlba1, endlba2; 10750 10751 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10752 endlba2 = lba2 + len2 - 1; 10753 10754 if ((endlba1 < lba2) || (endlba2 < lba1)) 10755 return (CTL_ACTION_PASS); 10756 else 10757 return (CTL_ACTION_BLOCK); 10758 } 10759 10760 static int 10761 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10762 { 10763 struct ctl_ptr_len_flags *ptrlen; 10764 struct scsi_unmap_desc *buf, *end, *range; 10765 uint64_t lba; 10766 uint32_t len; 10767 10768 /* If not UNMAP -- go other way. */ 10769 if (io->io_hdr.io_type != CTL_IO_SCSI || 10770 io->scsiio.cdb[0] != UNMAP) 10771 return (CTL_ACTION_ERROR); 10772 10773 /* If UNMAP without data -- block and wait for data. */ 10774 ptrlen = (struct ctl_ptr_len_flags *) 10775 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10776 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10777 ptrlen->ptr == NULL) 10778 return (CTL_ACTION_BLOCK); 10779 10780 /* UNMAP with data -- check for collision. */ 10781 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10782 end = buf + ptrlen->len / sizeof(*buf); 10783 for (range = buf; range < end; range++) { 10784 lba = scsi_8btou64(range->lba); 10785 len = scsi_4btoul(range->length); 10786 if ((lba < lba2 + len2) && (lba + len > lba2)) 10787 return (CTL_ACTION_BLOCK); 10788 } 10789 return (CTL_ACTION_PASS); 10790 } 10791 10792 static ctl_action 10793 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10794 { 10795 uint64_t lba1, lba2; 10796 uint64_t len1, len2; 10797 int retval; 10798 10799 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10800 return (CTL_ACTION_ERROR); 10801 10802 retval = ctl_extent_check_unmap(io1, lba2, len2); 10803 if (retval != CTL_ACTION_ERROR) 10804 return (retval); 10805 10806 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10807 return (CTL_ACTION_ERROR); 10808 10809 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10810 seq = FALSE; 10811 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10812 } 10813 10814 static ctl_action 10815 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10816 { 10817 uint64_t lba1, lba2; 10818 uint64_t len1, len2; 10819 10820 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10821 return (CTL_ACTION_PASS); 10822 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10823 return (CTL_ACTION_ERROR); 10824 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10825 return (CTL_ACTION_ERROR); 10826 10827 if (lba1 + len1 == lba2) 10828 return (CTL_ACTION_BLOCK); 10829 return (CTL_ACTION_PASS); 10830 } 10831 10832 static ctl_action 10833 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10834 union ctl_io *ooa_io) 10835 { 10836 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10837 const ctl_serialize_action *serialize_row; 10838 10839 /* 10840 * The initiator attempted multiple untagged commands at the same 10841 * time. Can't do that. 10842 */ 10843 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10844 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10845 && ((pending_io->io_hdr.nexus.targ_port == 10846 ooa_io->io_hdr.nexus.targ_port) 10847 && (pending_io->io_hdr.nexus.initid == 10848 ooa_io->io_hdr.nexus.initid)) 10849 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10850 CTL_FLAG_STATUS_SENT)) == 0)) 10851 return (CTL_ACTION_OVERLAP); 10852 10853 /* 10854 * The initiator attempted to send multiple tagged commands with 10855 * the same ID. (It's fine if different initiators have the same 10856 * tag ID.) 10857 * 10858 * Even if all of those conditions are true, we don't kill the I/O 10859 * if the command ahead of us has been aborted. We won't end up 10860 * sending it to the FETD, and it's perfectly legal to resend a 10861 * command with the same tag number as long as the previous 10862 * instance of this tag number has been aborted somehow. 10863 */ 10864 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10865 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10866 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10867 && ((pending_io->io_hdr.nexus.targ_port == 10868 ooa_io->io_hdr.nexus.targ_port) 10869 && (pending_io->io_hdr.nexus.initid == 10870 ooa_io->io_hdr.nexus.initid)) 10871 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10872 CTL_FLAG_STATUS_SENT)) == 0)) 10873 return (CTL_ACTION_OVERLAP_TAG); 10874 10875 /* 10876 * If we get a head of queue tag, SAM-3 says that we should 10877 * immediately execute it. 10878 * 10879 * What happens if this command would normally block for some other 10880 * reason? e.g. a request sense with a head of queue tag 10881 * immediately after a write. Normally that would block, but this 10882 * will result in its getting executed immediately... 10883 * 10884 * We currently return "pass" instead of "skip", so we'll end up 10885 * going through the rest of the queue to check for overlapped tags. 10886 * 10887 * XXX KDM check for other types of blockage first?? 10888 */ 10889 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10890 return (CTL_ACTION_PASS); 10891 10892 /* 10893 * Ordered tags have to block until all items ahead of them 10894 * have completed. If we get called with an ordered tag, we always 10895 * block, if something else is ahead of us in the queue. 10896 */ 10897 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10898 return (CTL_ACTION_BLOCK); 10899 10900 /* 10901 * Simple tags get blocked until all head of queue and ordered tags 10902 * ahead of them have completed. I'm lumping untagged commands in 10903 * with simple tags here. XXX KDM is that the right thing to do? 10904 */ 10905 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10906 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10907 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10908 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10909 return (CTL_ACTION_BLOCK); 10910 10911 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10912 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 10913 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 10914 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 10915 pending_io->scsiio.cdb[1], pending_io)); 10916 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10917 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 10918 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 10919 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 10920 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 10921 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 10922 ooa_io->scsiio.cdb[1], ooa_io)); 10923 10924 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10925 10926 switch (serialize_row[pending_entry->seridx]) { 10927 case CTL_SER_BLOCK: 10928 return (CTL_ACTION_BLOCK); 10929 case CTL_SER_EXTENT: 10930 return (ctl_extent_check(ooa_io, pending_io, 10931 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10932 case CTL_SER_EXTENTOPT: 10933 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10934 SCP_QUEUE_ALG_UNRESTRICTED) 10935 return (ctl_extent_check(ooa_io, pending_io, 10936 (lun->be_lun && 10937 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10938 return (CTL_ACTION_PASS); 10939 case CTL_SER_EXTENTSEQ: 10940 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10941 return (ctl_extent_check_seq(ooa_io, pending_io)); 10942 return (CTL_ACTION_PASS); 10943 case CTL_SER_PASS: 10944 return (CTL_ACTION_PASS); 10945 case CTL_SER_BLOCKOPT: 10946 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10947 SCP_QUEUE_ALG_UNRESTRICTED) 10948 return (CTL_ACTION_BLOCK); 10949 return (CTL_ACTION_PASS); 10950 case CTL_SER_SKIP: 10951 return (CTL_ACTION_SKIP); 10952 default: 10953 panic("%s: Invalid serialization value %d for %d => %d", 10954 __func__, serialize_row[pending_entry->seridx], 10955 pending_entry->seridx, ooa_entry->seridx); 10956 } 10957 10958 return (CTL_ACTION_ERROR); 10959 } 10960 10961 /* 10962 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10963 * Assumptions: 10964 * - pending_io is generally either incoming, or on the blocked queue 10965 * - starting I/O is the I/O we want to start the check with. 10966 */ 10967 static ctl_action 10968 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10969 union ctl_io *starting_io) 10970 { 10971 union ctl_io *ooa_io; 10972 ctl_action action; 10973 10974 mtx_assert(&lun->lun_lock, MA_OWNED); 10975 10976 /* 10977 * Run back along the OOA queue, starting with the current 10978 * blocked I/O and going through every I/O before it on the 10979 * queue. If starting_io is NULL, we'll just end up returning 10980 * CTL_ACTION_PASS. 10981 */ 10982 for (ooa_io = starting_io; ooa_io != NULL; 10983 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10984 ooa_links)){ 10985 10986 /* 10987 * This routine just checks to see whether 10988 * cur_blocked is blocked by ooa_io, which is ahead 10989 * of it in the queue. It doesn't queue/dequeue 10990 * cur_blocked. 10991 */ 10992 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10993 switch (action) { 10994 case CTL_ACTION_BLOCK: 10995 case CTL_ACTION_OVERLAP: 10996 case CTL_ACTION_OVERLAP_TAG: 10997 case CTL_ACTION_SKIP: 10998 case CTL_ACTION_ERROR: 10999 return (action); 11000 break; /* NOTREACHED */ 11001 case CTL_ACTION_PASS: 11002 break; 11003 default: 11004 panic("%s: Invalid action %d\n", __func__, action); 11005 } 11006 } 11007 11008 return (CTL_ACTION_PASS); 11009 } 11010 11011 /* 11012 * Assumptions: 11013 * - An I/O has just completed, and has been removed from the per-LUN OOA 11014 * queue, so some items on the blocked queue may now be unblocked. 11015 */ 11016 static int 11017 ctl_check_blocked(struct ctl_lun *lun) 11018 { 11019 struct ctl_softc *softc = lun->ctl_softc; 11020 union ctl_io *cur_blocked, *next_blocked; 11021 11022 mtx_assert(&lun->lun_lock, MA_OWNED); 11023 11024 /* 11025 * Run forward from the head of the blocked queue, checking each 11026 * entry against the I/Os prior to it on the OOA queue to see if 11027 * there is still any blockage. 11028 * 11029 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11030 * with our removing a variable on it while it is traversing the 11031 * list. 11032 */ 11033 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11034 cur_blocked != NULL; cur_blocked = next_blocked) { 11035 union ctl_io *prev_ooa; 11036 ctl_action action; 11037 11038 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11039 blocked_links); 11040 11041 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11042 ctl_ooaq, ooa_links); 11043 11044 /* 11045 * If cur_blocked happens to be the first item in the OOA 11046 * queue now, prev_ooa will be NULL, and the action 11047 * returned will just be CTL_ACTION_PASS. 11048 */ 11049 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11050 11051 switch (action) { 11052 case CTL_ACTION_BLOCK: 11053 /* Nothing to do here, still blocked */ 11054 break; 11055 case CTL_ACTION_OVERLAP: 11056 case CTL_ACTION_OVERLAP_TAG: 11057 /* 11058 * This shouldn't happen! In theory we've already 11059 * checked this command for overlap... 11060 */ 11061 break; 11062 case CTL_ACTION_PASS: 11063 case CTL_ACTION_SKIP: { 11064 const struct ctl_cmd_entry *entry; 11065 11066 /* 11067 * The skip case shouldn't happen, this transaction 11068 * should have never made it onto the blocked queue. 11069 */ 11070 /* 11071 * This I/O is no longer blocked, we can remove it 11072 * from the blocked queue. Since this is a TAILQ 11073 * (doubly linked list), we can do O(1) removals 11074 * from any place on the list. 11075 */ 11076 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11077 blocked_links); 11078 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11079 11080 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11081 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11082 /* 11083 * Need to send IO back to original side to 11084 * run 11085 */ 11086 union ctl_ha_msg msg_info; 11087 11088 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11089 msg_info.hdr.original_sc = 11090 cur_blocked->io_hdr.original_sc; 11091 msg_info.hdr.serializing_sc = cur_blocked; 11092 msg_info.hdr.msg_type = CTL_MSG_R2R; 11093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11094 sizeof(msg_info.hdr), M_NOWAIT); 11095 break; 11096 } 11097 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11098 11099 /* 11100 * Check this I/O for LUN state changes that may 11101 * have happened while this command was blocked. 11102 * The LUN state may have been changed by a command 11103 * ahead of us in the queue, so we need to re-check 11104 * for any states that can be caused by SCSI 11105 * commands. 11106 */ 11107 if (ctl_scsiio_lun_check(lun, entry, 11108 &cur_blocked->scsiio) == 0) { 11109 cur_blocked->io_hdr.flags |= 11110 CTL_FLAG_IS_WAS_ON_RTR; 11111 ctl_enqueue_rtr(cur_blocked); 11112 } else 11113 ctl_done(cur_blocked); 11114 break; 11115 } 11116 default: 11117 /* 11118 * This probably shouldn't happen -- we shouldn't 11119 * get CTL_ACTION_ERROR, or anything else. 11120 */ 11121 break; 11122 } 11123 } 11124 11125 return (CTL_RETVAL_COMPLETE); 11126 } 11127 11128 /* 11129 * This routine (with one exception) checks LUN flags that can be set by 11130 * commands ahead of us in the OOA queue. These flags have to be checked 11131 * when a command initially comes in, and when we pull a command off the 11132 * blocked queue and are preparing to execute it. The reason we have to 11133 * check these flags for commands on the blocked queue is that the LUN 11134 * state may have been changed by a command ahead of us while we're on the 11135 * blocked queue. 11136 * 11137 * Ordering is somewhat important with these checks, so please pay 11138 * careful attention to the placement of any new checks. 11139 */ 11140 static int 11141 ctl_scsiio_lun_check(struct ctl_lun *lun, 11142 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11143 { 11144 struct ctl_softc *softc = lun->ctl_softc; 11145 int retval; 11146 uint32_t residx; 11147 11148 retval = 0; 11149 11150 mtx_assert(&lun->lun_lock, MA_OWNED); 11151 11152 /* 11153 * If this shelf is a secondary shelf controller, we may have to 11154 * reject some commands disallowed by HA mode and link state. 11155 */ 11156 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11157 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11158 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11159 ctl_set_lun_unavail(ctsio); 11160 retval = 1; 11161 goto bailout; 11162 } 11163 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11164 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11165 ctl_set_lun_transit(ctsio); 11166 retval = 1; 11167 goto bailout; 11168 } 11169 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11170 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11171 ctl_set_lun_standby(ctsio); 11172 retval = 1; 11173 goto bailout; 11174 } 11175 11176 /* The rest of checks are only done on executing side */ 11177 if (softc->ha_mode == CTL_HA_MODE_XFER) 11178 goto bailout; 11179 } 11180 11181 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11182 if (lun->be_lun && 11183 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11184 ctl_set_hw_write_protected(ctsio); 11185 retval = 1; 11186 goto bailout; 11187 } 11188 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11189 ctl_set_sense(ctsio, /*current_error*/ 1, 11190 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11191 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11192 retval = 1; 11193 goto bailout; 11194 } 11195 } 11196 11197 /* 11198 * Check for a reservation conflict. If this command isn't allowed 11199 * even on reserved LUNs, and if this initiator isn't the one who 11200 * reserved us, reject the command with a reservation conflict. 11201 */ 11202 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11203 if ((lun->flags & CTL_LUN_RESERVED) 11204 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11205 if (lun->res_idx != residx) { 11206 ctl_set_reservation_conflict(ctsio); 11207 retval = 1; 11208 goto bailout; 11209 } 11210 } 11211 11212 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11213 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11214 /* No reservation or command is allowed. */; 11215 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11216 (lun->pr_res_type == SPR_TYPE_WR_EX || 11217 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11218 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11219 /* The command is allowed for Write Exclusive resv. */; 11220 } else { 11221 /* 11222 * if we aren't registered or it's a res holder type 11223 * reservation and this isn't the res holder then set a 11224 * conflict. 11225 */ 11226 if (ctl_get_prkey(lun, residx) == 0 || 11227 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11228 ctl_set_reservation_conflict(ctsio); 11229 retval = 1; 11230 goto bailout; 11231 } 11232 } 11233 11234 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11235 if (lun->flags & CTL_LUN_EJECTED) 11236 ctl_set_lun_ejected(ctsio); 11237 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11238 if (lun->flags & CTL_LUN_REMOVABLE) 11239 ctl_set_lun_no_media(ctsio); 11240 else 11241 ctl_set_lun_int_reqd(ctsio); 11242 } else if (lun->flags & CTL_LUN_STOPPED) 11243 ctl_set_lun_stopped(ctsio); 11244 else 11245 goto bailout; 11246 retval = 1; 11247 goto bailout; 11248 } 11249 11250 bailout: 11251 return (retval); 11252 } 11253 11254 static void 11255 ctl_failover_io(union ctl_io *io, int have_lock) 11256 { 11257 ctl_set_busy(&io->scsiio); 11258 ctl_done(io); 11259 } 11260 11261 static void 11262 ctl_failover_lun(union ctl_io *rio) 11263 { 11264 struct ctl_softc *softc = CTL_SOFTC(rio); 11265 struct ctl_lun *lun; 11266 struct ctl_io_hdr *io, *next_io; 11267 uint32_t targ_lun; 11268 11269 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11270 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11271 11272 /* Find and lock the LUN. */ 11273 mtx_lock(&softc->ctl_lock); 11274 if (targ_lun > ctl_max_luns || 11275 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11276 mtx_unlock(&softc->ctl_lock); 11277 return; 11278 } 11279 mtx_lock(&lun->lun_lock); 11280 mtx_unlock(&softc->ctl_lock); 11281 if (lun->flags & CTL_LUN_DISABLED) { 11282 mtx_unlock(&lun->lun_lock); 11283 return; 11284 } 11285 11286 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11287 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11288 /* We are master */ 11289 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11290 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11291 io->flags |= CTL_FLAG_ABORT; 11292 io->flags |= CTL_FLAG_FAILOVER; 11293 } else { /* This can be only due to DATAMOVE */ 11294 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11295 io->flags &= ~CTL_FLAG_DMA_INPROG; 11296 io->flags |= CTL_FLAG_IO_ACTIVE; 11297 io->port_status = 31340; 11298 ctl_enqueue_isc((union ctl_io *)io); 11299 } 11300 } 11301 /* We are slave */ 11302 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11303 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11304 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11305 io->flags |= CTL_FLAG_FAILOVER; 11306 } else { 11307 ctl_set_busy(&((union ctl_io *)io)-> 11308 scsiio); 11309 ctl_done((union ctl_io *)io); 11310 } 11311 } 11312 } 11313 } else { /* SERIALIZE modes */ 11314 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11315 next_io) { 11316 /* We are master */ 11317 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11318 TAILQ_REMOVE(&lun->blocked_queue, io, 11319 blocked_links); 11320 io->flags &= ~CTL_FLAG_BLOCKED; 11321 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11322 ctl_free_io((union ctl_io *)io); 11323 } 11324 } 11325 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11326 /* We are master */ 11327 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11328 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11329 ctl_free_io((union ctl_io *)io); 11330 } 11331 /* We are slave */ 11332 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11333 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11334 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11335 ctl_set_busy(&((union ctl_io *)io)-> 11336 scsiio); 11337 ctl_done((union ctl_io *)io); 11338 } 11339 } 11340 } 11341 ctl_check_blocked(lun); 11342 } 11343 mtx_unlock(&lun->lun_lock); 11344 } 11345 11346 static int 11347 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11348 { 11349 struct ctl_lun *lun; 11350 const struct ctl_cmd_entry *entry; 11351 uint32_t initidx, targ_lun; 11352 int retval = 0; 11353 11354 lun = NULL; 11355 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11356 if (targ_lun < ctl_max_luns) 11357 lun = softc->ctl_luns[targ_lun]; 11358 if (lun) { 11359 /* 11360 * If the LUN is invalid, pretend that it doesn't exist. 11361 * It will go away as soon as all pending I/O has been 11362 * completed. 11363 */ 11364 mtx_lock(&lun->lun_lock); 11365 if (lun->flags & CTL_LUN_DISABLED) { 11366 mtx_unlock(&lun->lun_lock); 11367 lun = NULL; 11368 } 11369 } 11370 CTL_LUN(ctsio) = lun; 11371 if (lun) { 11372 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11373 11374 /* 11375 * Every I/O goes into the OOA queue for a particular LUN, 11376 * and stays there until completion. 11377 */ 11378 #ifdef CTL_TIME_IO 11379 if (TAILQ_EMPTY(&lun->ooa_queue)) 11380 lun->idle_time += getsbinuptime() - lun->last_busy; 11381 #endif 11382 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11383 } 11384 11385 /* Get command entry and return error if it is unsuppotyed. */ 11386 entry = ctl_validate_command(ctsio); 11387 if (entry == NULL) { 11388 if (lun) 11389 mtx_unlock(&lun->lun_lock); 11390 return (retval); 11391 } 11392 11393 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11394 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11395 11396 /* 11397 * Check to see whether we can send this command to LUNs that don't 11398 * exist. This should pretty much only be the case for inquiry 11399 * and request sense. Further checks, below, really require having 11400 * a LUN, so we can't really check the command anymore. Just put 11401 * it on the rtr queue. 11402 */ 11403 if (lun == NULL) { 11404 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11405 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11406 ctl_enqueue_rtr((union ctl_io *)ctsio); 11407 return (retval); 11408 } 11409 11410 ctl_set_unsupported_lun(ctsio); 11411 ctl_done((union ctl_io *)ctsio); 11412 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11413 return (retval); 11414 } else { 11415 /* 11416 * Make sure we support this particular command on this LUN. 11417 * e.g., we don't support writes to the control LUN. 11418 */ 11419 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11420 mtx_unlock(&lun->lun_lock); 11421 ctl_set_invalid_opcode(ctsio); 11422 ctl_done((union ctl_io *)ctsio); 11423 return (retval); 11424 } 11425 } 11426 11427 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11428 11429 /* 11430 * If we've got a request sense, it'll clear the contingent 11431 * allegiance condition. Otherwise, if we have a CA condition for 11432 * this initiator, clear it, because it sent down a command other 11433 * than request sense. 11434 */ 11435 if (ctsio->cdb[0] != REQUEST_SENSE) { 11436 struct scsi_sense_data *ps; 11437 11438 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11439 if (ps != NULL) 11440 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11441 } 11442 11443 /* 11444 * If the command has this flag set, it handles its own unit 11445 * attention reporting, we shouldn't do anything. Otherwise we 11446 * check for any pending unit attentions, and send them back to the 11447 * initiator. We only do this when a command initially comes in, 11448 * not when we pull it off the blocked queue. 11449 * 11450 * According to SAM-3, section 5.3.2, the order that things get 11451 * presented back to the host is basically unit attentions caused 11452 * by some sort of reset event, busy status, reservation conflicts 11453 * or task set full, and finally any other status. 11454 * 11455 * One issue here is that some of the unit attentions we report 11456 * don't fall into the "reset" category (e.g. "reported luns data 11457 * has changed"). So reporting it here, before the reservation 11458 * check, may be technically wrong. I guess the only thing to do 11459 * would be to check for and report the reset events here, and then 11460 * check for the other unit attention types after we check for a 11461 * reservation conflict. 11462 * 11463 * XXX KDM need to fix this 11464 */ 11465 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11466 ctl_ua_type ua_type; 11467 u_int sense_len = 0; 11468 11469 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11470 &sense_len, SSD_TYPE_NONE); 11471 if (ua_type != CTL_UA_NONE) { 11472 mtx_unlock(&lun->lun_lock); 11473 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11474 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11475 ctsio->sense_len = sense_len; 11476 ctl_done((union ctl_io *)ctsio); 11477 return (retval); 11478 } 11479 } 11480 11481 11482 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11483 mtx_unlock(&lun->lun_lock); 11484 ctl_done((union ctl_io *)ctsio); 11485 return (retval); 11486 } 11487 11488 /* 11489 * XXX CHD this is where we want to send IO to other side if 11490 * this LUN is secondary on this SC. We will need to make a copy 11491 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11492 * the copy we send as FROM_OTHER. 11493 * We also need to stuff the address of the original IO so we can 11494 * find it easily. Something similar will need be done on the other 11495 * side so when we are done we can find the copy. 11496 */ 11497 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11498 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11499 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11500 union ctl_ha_msg msg_info; 11501 int isc_retval; 11502 11503 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11504 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11505 mtx_unlock(&lun->lun_lock); 11506 11507 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11508 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11509 msg_info.hdr.serializing_sc = NULL; 11510 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11511 msg_info.scsi.tag_num = ctsio->tag_num; 11512 msg_info.scsi.tag_type = ctsio->tag_type; 11513 msg_info.scsi.cdb_len = ctsio->cdb_len; 11514 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11515 11516 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11517 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11518 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11519 ctl_set_busy(ctsio); 11520 ctl_done((union ctl_io *)ctsio); 11521 return (retval); 11522 } 11523 return (retval); 11524 } 11525 11526 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11527 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11528 ctl_ooaq, ooa_links))) { 11529 case CTL_ACTION_BLOCK: 11530 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11531 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11532 blocked_links); 11533 mtx_unlock(&lun->lun_lock); 11534 return (retval); 11535 case CTL_ACTION_PASS: 11536 case CTL_ACTION_SKIP: 11537 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11538 mtx_unlock(&lun->lun_lock); 11539 ctl_enqueue_rtr((union ctl_io *)ctsio); 11540 break; 11541 case CTL_ACTION_OVERLAP: 11542 mtx_unlock(&lun->lun_lock); 11543 ctl_set_overlapped_cmd(ctsio); 11544 ctl_done((union ctl_io *)ctsio); 11545 break; 11546 case CTL_ACTION_OVERLAP_TAG: 11547 mtx_unlock(&lun->lun_lock); 11548 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11549 ctl_done((union ctl_io *)ctsio); 11550 break; 11551 case CTL_ACTION_ERROR: 11552 default: 11553 mtx_unlock(&lun->lun_lock); 11554 ctl_set_internal_failure(ctsio, 11555 /*sks_valid*/ 0, 11556 /*retry_count*/ 0); 11557 ctl_done((union ctl_io *)ctsio); 11558 break; 11559 } 11560 return (retval); 11561 } 11562 11563 const struct ctl_cmd_entry * 11564 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11565 { 11566 const struct ctl_cmd_entry *entry; 11567 int service_action; 11568 11569 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11570 if (sa) 11571 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11572 if (entry->flags & CTL_CMD_FLAG_SA5) { 11573 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11574 entry = &((const struct ctl_cmd_entry *) 11575 entry->execute)[service_action]; 11576 } 11577 return (entry); 11578 } 11579 11580 const struct ctl_cmd_entry * 11581 ctl_validate_command(struct ctl_scsiio *ctsio) 11582 { 11583 const struct ctl_cmd_entry *entry; 11584 int i, sa; 11585 uint8_t diff; 11586 11587 entry = ctl_get_cmd_entry(ctsio, &sa); 11588 if (entry->execute == NULL) { 11589 if (sa) 11590 ctl_set_invalid_field(ctsio, 11591 /*sks_valid*/ 1, 11592 /*command*/ 1, 11593 /*field*/ 1, 11594 /*bit_valid*/ 1, 11595 /*bit*/ 4); 11596 else 11597 ctl_set_invalid_opcode(ctsio); 11598 ctl_done((union ctl_io *)ctsio); 11599 return (NULL); 11600 } 11601 KASSERT(entry->length > 0, 11602 ("Not defined length for command 0x%02x/0x%02x", 11603 ctsio->cdb[0], ctsio->cdb[1])); 11604 for (i = 1; i < entry->length; i++) { 11605 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11606 if (diff == 0) 11607 continue; 11608 ctl_set_invalid_field(ctsio, 11609 /*sks_valid*/ 1, 11610 /*command*/ 1, 11611 /*field*/ i, 11612 /*bit_valid*/ 1, 11613 /*bit*/ fls(diff) - 1); 11614 ctl_done((union ctl_io *)ctsio); 11615 return (NULL); 11616 } 11617 return (entry); 11618 } 11619 11620 static int 11621 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11622 { 11623 11624 switch (lun_type) { 11625 case T_DIRECT: 11626 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11627 return (0); 11628 break; 11629 case T_PROCESSOR: 11630 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11631 return (0); 11632 break; 11633 case T_CDROM: 11634 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11635 return (0); 11636 break; 11637 default: 11638 return (0); 11639 } 11640 return (1); 11641 } 11642 11643 static int 11644 ctl_scsiio(struct ctl_scsiio *ctsio) 11645 { 11646 int retval; 11647 const struct ctl_cmd_entry *entry; 11648 11649 retval = CTL_RETVAL_COMPLETE; 11650 11651 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11652 11653 entry = ctl_get_cmd_entry(ctsio, NULL); 11654 11655 /* 11656 * If this I/O has been aborted, just send it straight to 11657 * ctl_done() without executing it. 11658 */ 11659 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11660 ctl_done((union ctl_io *)ctsio); 11661 goto bailout; 11662 } 11663 11664 /* 11665 * All the checks should have been handled by ctl_scsiio_precheck(). 11666 * We should be clear now to just execute the I/O. 11667 */ 11668 retval = entry->execute(ctsio); 11669 11670 bailout: 11671 return (retval); 11672 } 11673 11674 static int 11675 ctl_target_reset(union ctl_io *io) 11676 { 11677 struct ctl_softc *softc = CTL_SOFTC(io); 11678 struct ctl_port *port = CTL_PORT(io); 11679 struct ctl_lun *lun; 11680 uint32_t initidx; 11681 ctl_ua_type ua_type; 11682 11683 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11684 union ctl_ha_msg msg_info; 11685 11686 msg_info.hdr.nexus = io->io_hdr.nexus; 11687 msg_info.task.task_action = io->taskio.task_action; 11688 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11689 msg_info.hdr.original_sc = NULL; 11690 msg_info.hdr.serializing_sc = NULL; 11691 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11692 sizeof(msg_info.task), M_WAITOK); 11693 } 11694 11695 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11696 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11697 ua_type = CTL_UA_TARG_RESET; 11698 else 11699 ua_type = CTL_UA_BUS_RESET; 11700 mtx_lock(&softc->ctl_lock); 11701 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11702 if (port != NULL && 11703 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11704 continue; 11705 ctl_do_lun_reset(lun, initidx, ua_type); 11706 } 11707 mtx_unlock(&softc->ctl_lock); 11708 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11709 return (0); 11710 } 11711 11712 /* 11713 * The LUN should always be set. The I/O is optional, and is used to 11714 * distinguish between I/Os sent by this initiator, and by other 11715 * initiators. We set unit attention for initiators other than this one. 11716 * SAM-3 is vague on this point. It does say that a unit attention should 11717 * be established for other initiators when a LUN is reset (see section 11718 * 5.7.3), but it doesn't specifically say that the unit attention should 11719 * be established for this particular initiator when a LUN is reset. Here 11720 * is the relevant text, from SAM-3 rev 8: 11721 * 11722 * 5.7.2 When a SCSI initiator port aborts its own tasks 11723 * 11724 * When a SCSI initiator port causes its own task(s) to be aborted, no 11725 * notification that the task(s) have been aborted shall be returned to 11726 * the SCSI initiator port other than the completion response for the 11727 * command or task management function action that caused the task(s) to 11728 * be aborted and notification(s) associated with related effects of the 11729 * action (e.g., a reset unit attention condition). 11730 * 11731 * XXX KDM for now, we're setting unit attention for all initiators. 11732 */ 11733 static void 11734 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11735 { 11736 union ctl_io *xio; 11737 int i; 11738 11739 mtx_lock(&lun->lun_lock); 11740 /* Abort tasks. */ 11741 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11742 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11743 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11744 } 11745 /* Clear CA. */ 11746 for (i = 0; i < ctl_max_ports; i++) { 11747 free(lun->pending_sense[i], M_CTL); 11748 lun->pending_sense[i] = NULL; 11749 } 11750 /* Clear reservation. */ 11751 lun->flags &= ~CTL_LUN_RESERVED; 11752 /* Clear prevent media removal. */ 11753 if (lun->prevent) { 11754 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11755 ctl_clear_mask(lun->prevent, i); 11756 lun->prevent_count = 0; 11757 } 11758 /* Clear TPC status */ 11759 ctl_tpc_lun_clear(lun, -1); 11760 /* Establish UA. */ 11761 #if 0 11762 ctl_est_ua_all(lun, initidx, ua_type); 11763 #else 11764 ctl_est_ua_all(lun, -1, ua_type); 11765 #endif 11766 mtx_unlock(&lun->lun_lock); 11767 } 11768 11769 static int 11770 ctl_lun_reset(union ctl_io *io) 11771 { 11772 struct ctl_softc *softc = CTL_SOFTC(io); 11773 struct ctl_lun *lun; 11774 uint32_t targ_lun, initidx; 11775 11776 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11777 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11778 mtx_lock(&softc->ctl_lock); 11779 if (targ_lun >= ctl_max_luns || 11780 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11781 mtx_unlock(&softc->ctl_lock); 11782 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11783 return (1); 11784 } 11785 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11786 mtx_unlock(&softc->ctl_lock); 11787 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11788 11789 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11790 union ctl_ha_msg msg_info; 11791 11792 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11793 msg_info.hdr.nexus = io->io_hdr.nexus; 11794 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11795 msg_info.hdr.original_sc = NULL; 11796 msg_info.hdr.serializing_sc = NULL; 11797 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11798 sizeof(msg_info.task), M_WAITOK); 11799 } 11800 return (0); 11801 } 11802 11803 static void 11804 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11805 int other_sc) 11806 { 11807 union ctl_io *xio; 11808 11809 mtx_assert(&lun->lun_lock, MA_OWNED); 11810 11811 /* 11812 * Run through the OOA queue and attempt to find the given I/O. 11813 * The target port, initiator ID, tag type and tag number have to 11814 * match the values that we got from the initiator. If we have an 11815 * untagged command to abort, simply abort the first untagged command 11816 * we come to. We only allow one untagged command at a time of course. 11817 */ 11818 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11819 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11820 11821 if ((targ_port == UINT32_MAX || 11822 targ_port == xio->io_hdr.nexus.targ_port) && 11823 (init_id == UINT32_MAX || 11824 init_id == xio->io_hdr.nexus.initid)) { 11825 if (targ_port != xio->io_hdr.nexus.targ_port || 11826 init_id != xio->io_hdr.nexus.initid) 11827 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11828 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11829 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11830 union ctl_ha_msg msg_info; 11831 11832 msg_info.hdr.nexus = xio->io_hdr.nexus; 11833 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11834 msg_info.task.tag_num = xio->scsiio.tag_num; 11835 msg_info.task.tag_type = xio->scsiio.tag_type; 11836 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11837 msg_info.hdr.original_sc = NULL; 11838 msg_info.hdr.serializing_sc = NULL; 11839 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11840 sizeof(msg_info.task), M_NOWAIT); 11841 } 11842 } 11843 } 11844 } 11845 11846 static int 11847 ctl_abort_task_set(union ctl_io *io) 11848 { 11849 struct ctl_softc *softc = CTL_SOFTC(io); 11850 struct ctl_lun *lun; 11851 uint32_t targ_lun; 11852 11853 /* 11854 * Look up the LUN. 11855 */ 11856 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11857 mtx_lock(&softc->ctl_lock); 11858 if (targ_lun >= ctl_max_luns || 11859 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11860 mtx_unlock(&softc->ctl_lock); 11861 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11862 return (1); 11863 } 11864 11865 mtx_lock(&lun->lun_lock); 11866 mtx_unlock(&softc->ctl_lock); 11867 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11868 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11869 io->io_hdr.nexus.initid, 11870 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11871 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11872 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11873 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11874 } 11875 mtx_unlock(&lun->lun_lock); 11876 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11877 return (0); 11878 } 11879 11880 static void 11881 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 11882 ctl_ua_type ua_type) 11883 { 11884 struct ctl_lun *lun; 11885 struct scsi_sense_data *ps; 11886 uint32_t p, i; 11887 11888 p = initidx / CTL_MAX_INIT_PER_PORT; 11889 i = initidx % CTL_MAX_INIT_PER_PORT; 11890 mtx_lock(&softc->ctl_lock); 11891 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11892 mtx_lock(&lun->lun_lock); 11893 /* Abort tasks. */ 11894 ctl_abort_tasks_lun(lun, p, i, 1); 11895 /* Clear CA. */ 11896 ps = lun->pending_sense[p]; 11897 if (ps != NULL) 11898 ps[i].error_code = 0; 11899 /* Clear reservation. */ 11900 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11901 lun->flags &= ~CTL_LUN_RESERVED; 11902 /* Clear prevent media removal. */ 11903 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 11904 ctl_clear_mask(lun->prevent, initidx); 11905 lun->prevent_count--; 11906 } 11907 /* Clear TPC status */ 11908 ctl_tpc_lun_clear(lun, initidx); 11909 /* Establish UA. */ 11910 ctl_est_ua(lun, initidx, ua_type); 11911 mtx_unlock(&lun->lun_lock); 11912 } 11913 mtx_unlock(&softc->ctl_lock); 11914 } 11915 11916 static int 11917 ctl_i_t_nexus_reset(union ctl_io *io) 11918 { 11919 struct ctl_softc *softc = CTL_SOFTC(io); 11920 uint32_t initidx; 11921 11922 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11923 union ctl_ha_msg msg_info; 11924 11925 msg_info.hdr.nexus = io->io_hdr.nexus; 11926 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11927 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11928 msg_info.hdr.original_sc = NULL; 11929 msg_info.hdr.serializing_sc = NULL; 11930 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11931 sizeof(msg_info.task), M_WAITOK); 11932 } 11933 11934 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11935 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 11936 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11937 return (0); 11938 } 11939 11940 static int 11941 ctl_abort_task(union ctl_io *io) 11942 { 11943 struct ctl_softc *softc = CTL_SOFTC(io); 11944 union ctl_io *xio; 11945 struct ctl_lun *lun; 11946 #if 0 11947 struct sbuf sb; 11948 char printbuf[128]; 11949 #endif 11950 int found; 11951 uint32_t targ_lun; 11952 11953 found = 0; 11954 11955 /* 11956 * Look up the LUN. 11957 */ 11958 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11959 mtx_lock(&softc->ctl_lock); 11960 if (targ_lun >= ctl_max_luns || 11961 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11962 mtx_unlock(&softc->ctl_lock); 11963 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11964 return (1); 11965 } 11966 11967 #if 0 11968 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11969 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11970 #endif 11971 11972 mtx_lock(&lun->lun_lock); 11973 mtx_unlock(&softc->ctl_lock); 11974 /* 11975 * Run through the OOA queue and attempt to find the given I/O. 11976 * The target port, initiator ID, tag type and tag number have to 11977 * match the values that we got from the initiator. If we have an 11978 * untagged command to abort, simply abort the first untagged command 11979 * we come to. We only allow one untagged command at a time of course. 11980 */ 11981 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11982 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11983 #if 0 11984 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11985 11986 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11987 lun->lun, xio->scsiio.tag_num, 11988 xio->scsiio.tag_type, 11989 (xio->io_hdr.blocked_links.tqe_prev 11990 == NULL) ? "" : " BLOCKED", 11991 (xio->io_hdr.flags & 11992 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11993 (xio->io_hdr.flags & 11994 CTL_FLAG_ABORT) ? " ABORT" : "", 11995 (xio->io_hdr.flags & 11996 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11997 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11998 sbuf_finish(&sb); 11999 printf("%s\n", sbuf_data(&sb)); 12000 #endif 12001 12002 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12003 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12004 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12005 continue; 12006 12007 /* 12008 * If the abort says that the task is untagged, the 12009 * task in the queue must be untagged. Otherwise, 12010 * we just check to see whether the tag numbers 12011 * match. This is because the QLogic firmware 12012 * doesn't pass back the tag type in an abort 12013 * request. 12014 */ 12015 #if 0 12016 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12017 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12018 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12019 #endif 12020 /* 12021 * XXX KDM we've got problems with FC, because it 12022 * doesn't send down a tag type with aborts. So we 12023 * can only really go by the tag number... 12024 * This may cause problems with parallel SCSI. 12025 * Need to figure that out!! 12026 */ 12027 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12028 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12029 found = 1; 12030 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12031 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12032 union ctl_ha_msg msg_info; 12033 12034 msg_info.hdr.nexus = io->io_hdr.nexus; 12035 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12036 msg_info.task.tag_num = io->taskio.tag_num; 12037 msg_info.task.tag_type = io->taskio.tag_type; 12038 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12039 msg_info.hdr.original_sc = NULL; 12040 msg_info.hdr.serializing_sc = NULL; 12041 #if 0 12042 printf("Sent Abort to other side\n"); 12043 #endif 12044 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12045 sizeof(msg_info.task), M_NOWAIT); 12046 } 12047 #if 0 12048 printf("ctl_abort_task: found I/O to abort\n"); 12049 #endif 12050 } 12051 } 12052 mtx_unlock(&lun->lun_lock); 12053 12054 if (found == 0) { 12055 /* 12056 * This isn't really an error. It's entirely possible for 12057 * the abort and command completion to cross on the wire. 12058 * This is more of an informative/diagnostic error. 12059 */ 12060 #if 0 12061 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12062 "%u:%u:%u tag %d type %d\n", 12063 io->io_hdr.nexus.initid, 12064 io->io_hdr.nexus.targ_port, 12065 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12066 io->taskio.tag_type); 12067 #endif 12068 } 12069 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12070 return (0); 12071 } 12072 12073 static int 12074 ctl_query_task(union ctl_io *io, int task_set) 12075 { 12076 struct ctl_softc *softc = CTL_SOFTC(io); 12077 union ctl_io *xio; 12078 struct ctl_lun *lun; 12079 int found = 0; 12080 uint32_t targ_lun; 12081 12082 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12083 mtx_lock(&softc->ctl_lock); 12084 if (targ_lun >= ctl_max_luns || 12085 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12086 mtx_unlock(&softc->ctl_lock); 12087 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12088 return (1); 12089 } 12090 mtx_lock(&lun->lun_lock); 12091 mtx_unlock(&softc->ctl_lock); 12092 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12093 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12094 12095 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12096 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12097 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12098 continue; 12099 12100 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12101 found = 1; 12102 break; 12103 } 12104 } 12105 mtx_unlock(&lun->lun_lock); 12106 if (found) 12107 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12108 else 12109 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12110 return (0); 12111 } 12112 12113 static int 12114 ctl_query_async_event(union ctl_io *io) 12115 { 12116 struct ctl_softc *softc = CTL_SOFTC(io); 12117 struct ctl_lun *lun; 12118 ctl_ua_type ua; 12119 uint32_t targ_lun, initidx; 12120 12121 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12122 mtx_lock(&softc->ctl_lock); 12123 if (targ_lun >= ctl_max_luns || 12124 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12125 mtx_unlock(&softc->ctl_lock); 12126 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12127 return (1); 12128 } 12129 mtx_lock(&lun->lun_lock); 12130 mtx_unlock(&softc->ctl_lock); 12131 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12132 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12133 mtx_unlock(&lun->lun_lock); 12134 if (ua != CTL_UA_NONE) 12135 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12136 else 12137 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12138 return (0); 12139 } 12140 12141 static void 12142 ctl_run_task(union ctl_io *io) 12143 { 12144 int retval = 1; 12145 12146 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12147 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12148 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12149 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12150 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12151 switch (io->taskio.task_action) { 12152 case CTL_TASK_ABORT_TASK: 12153 retval = ctl_abort_task(io); 12154 break; 12155 case CTL_TASK_ABORT_TASK_SET: 12156 case CTL_TASK_CLEAR_TASK_SET: 12157 retval = ctl_abort_task_set(io); 12158 break; 12159 case CTL_TASK_CLEAR_ACA: 12160 break; 12161 case CTL_TASK_I_T_NEXUS_RESET: 12162 retval = ctl_i_t_nexus_reset(io); 12163 break; 12164 case CTL_TASK_LUN_RESET: 12165 retval = ctl_lun_reset(io); 12166 break; 12167 case CTL_TASK_TARGET_RESET: 12168 case CTL_TASK_BUS_RESET: 12169 retval = ctl_target_reset(io); 12170 break; 12171 case CTL_TASK_PORT_LOGIN: 12172 break; 12173 case CTL_TASK_PORT_LOGOUT: 12174 break; 12175 case CTL_TASK_QUERY_TASK: 12176 retval = ctl_query_task(io, 0); 12177 break; 12178 case CTL_TASK_QUERY_TASK_SET: 12179 retval = ctl_query_task(io, 1); 12180 break; 12181 case CTL_TASK_QUERY_ASYNC_EVENT: 12182 retval = ctl_query_async_event(io); 12183 break; 12184 default: 12185 printf("%s: got unknown task management event %d\n", 12186 __func__, io->taskio.task_action); 12187 break; 12188 } 12189 if (retval == 0) 12190 io->io_hdr.status = CTL_SUCCESS; 12191 else 12192 io->io_hdr.status = CTL_ERROR; 12193 ctl_done(io); 12194 } 12195 12196 /* 12197 * For HA operation. Handle commands that come in from the other 12198 * controller. 12199 */ 12200 static void 12201 ctl_handle_isc(union ctl_io *io) 12202 { 12203 struct ctl_softc *softc = CTL_SOFTC(io); 12204 struct ctl_lun *lun; 12205 const struct ctl_cmd_entry *entry; 12206 uint32_t targ_lun; 12207 12208 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12209 switch (io->io_hdr.msg_type) { 12210 case CTL_MSG_SERIALIZE: 12211 ctl_serialize_other_sc_cmd(&io->scsiio); 12212 break; 12213 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12214 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12215 if (targ_lun >= ctl_max_luns || 12216 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12217 ctl_done(io); 12218 break; 12219 } 12220 mtx_lock(&lun->lun_lock); 12221 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12222 mtx_unlock(&lun->lun_lock); 12223 ctl_done(io); 12224 break; 12225 } 12226 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12227 mtx_unlock(&lun->lun_lock); 12228 ctl_enqueue_rtr(io); 12229 break; 12230 case CTL_MSG_FINISH_IO: 12231 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12232 ctl_done(io); 12233 break; 12234 } 12235 if (targ_lun >= ctl_max_luns || 12236 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12237 ctl_free_io(io); 12238 break; 12239 } 12240 mtx_lock(&lun->lun_lock); 12241 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12242 ctl_check_blocked(lun); 12243 mtx_unlock(&lun->lun_lock); 12244 ctl_free_io(io); 12245 break; 12246 case CTL_MSG_PERS_ACTION: 12247 ctl_hndl_per_res_out_on_other_sc(io); 12248 ctl_free_io(io); 12249 break; 12250 case CTL_MSG_BAD_JUJU: 12251 ctl_done(io); 12252 break; 12253 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12254 ctl_datamove_remote(io); 12255 break; 12256 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12257 io->scsiio.be_move_done(io); 12258 break; 12259 case CTL_MSG_FAILOVER: 12260 ctl_failover_lun(io); 12261 ctl_free_io(io); 12262 break; 12263 default: 12264 printf("%s: Invalid message type %d\n", 12265 __func__, io->io_hdr.msg_type); 12266 ctl_free_io(io); 12267 break; 12268 } 12269 12270 } 12271 12272 12273 /* 12274 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12275 * there is no match. 12276 */ 12277 static ctl_lun_error_pattern 12278 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12279 { 12280 const struct ctl_cmd_entry *entry; 12281 ctl_lun_error_pattern filtered_pattern, pattern; 12282 12283 pattern = desc->error_pattern; 12284 12285 /* 12286 * XXX KDM we need more data passed into this function to match a 12287 * custom pattern, and we actually need to implement custom pattern 12288 * matching. 12289 */ 12290 if (pattern & CTL_LUN_PAT_CMD) 12291 return (CTL_LUN_PAT_CMD); 12292 12293 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12294 return (CTL_LUN_PAT_ANY); 12295 12296 entry = ctl_get_cmd_entry(ctsio, NULL); 12297 12298 filtered_pattern = entry->pattern & pattern; 12299 12300 /* 12301 * If the user requested specific flags in the pattern (e.g. 12302 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12303 * flags. 12304 * 12305 * If the user did not specify any flags, it doesn't matter whether 12306 * or not the command supports the flags. 12307 */ 12308 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12309 (pattern & ~CTL_LUN_PAT_MASK)) 12310 return (CTL_LUN_PAT_NONE); 12311 12312 /* 12313 * If the user asked for a range check, see if the requested LBA 12314 * range overlaps with this command's LBA range. 12315 */ 12316 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12317 uint64_t lba1; 12318 uint64_t len1; 12319 ctl_action action; 12320 int retval; 12321 12322 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12323 if (retval != 0) 12324 return (CTL_LUN_PAT_NONE); 12325 12326 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12327 desc->lba_range.len, FALSE); 12328 /* 12329 * A "pass" means that the LBA ranges don't overlap, so 12330 * this doesn't match the user's range criteria. 12331 */ 12332 if (action == CTL_ACTION_PASS) 12333 return (CTL_LUN_PAT_NONE); 12334 } 12335 12336 return (filtered_pattern); 12337 } 12338 12339 static void 12340 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12341 { 12342 struct ctl_error_desc *desc, *desc2; 12343 12344 mtx_assert(&lun->lun_lock, MA_OWNED); 12345 12346 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12347 ctl_lun_error_pattern pattern; 12348 /* 12349 * Check to see whether this particular command matches 12350 * the pattern in the descriptor. 12351 */ 12352 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12353 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12354 continue; 12355 12356 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12357 case CTL_LUN_INJ_ABORTED: 12358 ctl_set_aborted(&io->scsiio); 12359 break; 12360 case CTL_LUN_INJ_MEDIUM_ERR: 12361 ctl_set_medium_error(&io->scsiio, 12362 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12363 CTL_FLAG_DATA_OUT); 12364 break; 12365 case CTL_LUN_INJ_UA: 12366 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12367 * OCCURRED */ 12368 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12369 break; 12370 case CTL_LUN_INJ_CUSTOM: 12371 /* 12372 * We're assuming the user knows what he is doing. 12373 * Just copy the sense information without doing 12374 * checks. 12375 */ 12376 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12377 MIN(sizeof(desc->custom_sense), 12378 sizeof(io->scsiio.sense_data))); 12379 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12380 io->scsiio.sense_len = SSD_FULL_SIZE; 12381 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12382 break; 12383 case CTL_LUN_INJ_NONE: 12384 default: 12385 /* 12386 * If this is an error injection type we don't know 12387 * about, clear the continuous flag (if it is set) 12388 * so it will get deleted below. 12389 */ 12390 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12391 break; 12392 } 12393 /* 12394 * By default, each error injection action is a one-shot 12395 */ 12396 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12397 continue; 12398 12399 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12400 12401 free(desc, M_CTL); 12402 } 12403 } 12404 12405 #ifdef CTL_IO_DELAY 12406 static void 12407 ctl_datamove_timer_wakeup(void *arg) 12408 { 12409 union ctl_io *io; 12410 12411 io = (union ctl_io *)arg; 12412 12413 ctl_datamove(io); 12414 } 12415 #endif /* CTL_IO_DELAY */ 12416 12417 void 12418 ctl_datamove(union ctl_io *io) 12419 { 12420 void (*fe_datamove)(union ctl_io *io); 12421 12422 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12423 12424 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12425 12426 /* No data transferred yet. Frontend must update this when done. */ 12427 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12428 12429 #ifdef CTL_TIME_IO 12430 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12431 char str[256]; 12432 char path_str[64]; 12433 struct sbuf sb; 12434 12435 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12436 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12437 12438 sbuf_cat(&sb, path_str); 12439 switch (io->io_hdr.io_type) { 12440 case CTL_IO_SCSI: 12441 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12442 sbuf_printf(&sb, "\n"); 12443 sbuf_cat(&sb, path_str); 12444 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12445 io->scsiio.tag_num, io->scsiio.tag_type); 12446 break; 12447 case CTL_IO_TASK: 12448 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12449 "Tag Type: %d\n", io->taskio.task_action, 12450 io->taskio.tag_num, io->taskio.tag_type); 12451 break; 12452 default: 12453 panic("%s: Invalid CTL I/O type %d\n", 12454 __func__, io->io_hdr.io_type); 12455 } 12456 sbuf_cat(&sb, path_str); 12457 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12458 (intmax_t)time_uptime - io->io_hdr.start_time); 12459 sbuf_finish(&sb); 12460 printf("%s", sbuf_data(&sb)); 12461 } 12462 #endif /* CTL_TIME_IO */ 12463 12464 #ifdef CTL_IO_DELAY 12465 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12466 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12467 } else { 12468 struct ctl_lun *lun; 12469 12470 lun = CTL_LUN(io); 12471 if ((lun != NULL) 12472 && (lun->delay_info.datamove_delay > 0)) { 12473 12474 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12475 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12476 callout_reset(&io->io_hdr.delay_callout, 12477 lun->delay_info.datamove_delay * hz, 12478 ctl_datamove_timer_wakeup, io); 12479 if (lun->delay_info.datamove_type == 12480 CTL_DELAY_TYPE_ONESHOT) 12481 lun->delay_info.datamove_delay = 0; 12482 return; 12483 } 12484 } 12485 #endif 12486 12487 /* 12488 * This command has been aborted. Set the port status, so we fail 12489 * the data move. 12490 */ 12491 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12492 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12493 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12494 io->io_hdr.nexus.targ_port, 12495 io->io_hdr.nexus.targ_lun); 12496 io->io_hdr.port_status = 31337; 12497 /* 12498 * Note that the backend, in this case, will get the 12499 * callback in its context. In other cases it may get 12500 * called in the frontend's interrupt thread context. 12501 */ 12502 io->scsiio.be_move_done(io); 12503 return; 12504 } 12505 12506 /* Don't confuse frontend with zero length data move. */ 12507 if (io->scsiio.kern_data_len == 0) { 12508 io->scsiio.be_move_done(io); 12509 return; 12510 } 12511 12512 fe_datamove = CTL_PORT(io)->fe_datamove; 12513 fe_datamove(io); 12514 } 12515 12516 static void 12517 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12518 { 12519 union ctl_ha_msg msg; 12520 #ifdef CTL_TIME_IO 12521 struct bintime cur_bt; 12522 #endif 12523 12524 memset(&msg, 0, sizeof(msg)); 12525 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12526 msg.hdr.original_sc = io; 12527 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12528 msg.hdr.nexus = io->io_hdr.nexus; 12529 msg.hdr.status = io->io_hdr.status; 12530 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12531 msg.scsi.tag_num = io->scsiio.tag_num; 12532 msg.scsi.tag_type = io->scsiio.tag_type; 12533 msg.scsi.scsi_status = io->scsiio.scsi_status; 12534 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12535 io->scsiio.sense_len); 12536 msg.scsi.sense_len = io->scsiio.sense_len; 12537 msg.scsi.port_status = io->io_hdr.port_status; 12538 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12539 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12540 ctl_failover_io(io, /*have_lock*/ have_lock); 12541 return; 12542 } 12543 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12544 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12545 msg.scsi.sense_len, M_WAITOK); 12546 12547 #ifdef CTL_TIME_IO 12548 getbinuptime(&cur_bt); 12549 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12550 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12551 #endif 12552 io->io_hdr.num_dmas++; 12553 } 12554 12555 /* 12556 * The DMA to the remote side is done, now we need to tell the other side 12557 * we're done so it can continue with its data movement. 12558 */ 12559 static void 12560 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12561 { 12562 union ctl_io *io; 12563 uint32_t i; 12564 12565 io = rq->context; 12566 12567 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12568 printf("%s: ISC DMA write failed with error %d", __func__, 12569 rq->ret); 12570 ctl_set_internal_failure(&io->scsiio, 12571 /*sks_valid*/ 1, 12572 /*retry_count*/ rq->ret); 12573 } 12574 12575 ctl_dt_req_free(rq); 12576 12577 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12578 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12579 free(io->io_hdr.remote_sglist, M_CTL); 12580 io->io_hdr.remote_sglist = NULL; 12581 io->io_hdr.local_sglist = NULL; 12582 12583 /* 12584 * The data is in local and remote memory, so now we need to send 12585 * status (good or back) back to the other side. 12586 */ 12587 ctl_send_datamove_done(io, /*have_lock*/ 0); 12588 } 12589 12590 /* 12591 * We've moved the data from the host/controller into local memory. Now we 12592 * need to push it over to the remote controller's memory. 12593 */ 12594 static int 12595 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12596 { 12597 int retval; 12598 12599 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12600 ctl_datamove_remote_write_cb); 12601 return (retval); 12602 } 12603 12604 static void 12605 ctl_datamove_remote_write(union ctl_io *io) 12606 { 12607 int retval; 12608 void (*fe_datamove)(union ctl_io *io); 12609 12610 /* 12611 * - Get the data from the host/HBA into local memory. 12612 * - DMA memory from the local controller to the remote controller. 12613 * - Send status back to the remote controller. 12614 */ 12615 12616 retval = ctl_datamove_remote_sgl_setup(io); 12617 if (retval != 0) 12618 return; 12619 12620 /* Switch the pointer over so the FETD knows what to do */ 12621 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12622 12623 /* 12624 * Use a custom move done callback, since we need to send completion 12625 * back to the other controller, not to the backend on this side. 12626 */ 12627 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12628 12629 fe_datamove = CTL_PORT(io)->fe_datamove; 12630 fe_datamove(io); 12631 } 12632 12633 static int 12634 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12635 { 12636 #if 0 12637 char str[256]; 12638 char path_str[64]; 12639 struct sbuf sb; 12640 #endif 12641 uint32_t i; 12642 12643 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12644 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12645 free(io->io_hdr.remote_sglist, M_CTL); 12646 io->io_hdr.remote_sglist = NULL; 12647 io->io_hdr.local_sglist = NULL; 12648 12649 #if 0 12650 scsi_path_string(io, path_str, sizeof(path_str)); 12651 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12652 sbuf_cat(&sb, path_str); 12653 scsi_command_string(&io->scsiio, NULL, &sb); 12654 sbuf_printf(&sb, "\n"); 12655 sbuf_cat(&sb, path_str); 12656 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12657 io->scsiio.tag_num, io->scsiio.tag_type); 12658 sbuf_cat(&sb, path_str); 12659 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12660 io->io_hdr.flags, io->io_hdr.status); 12661 sbuf_finish(&sb); 12662 printk("%s", sbuf_data(&sb)); 12663 #endif 12664 12665 12666 /* 12667 * The read is done, now we need to send status (good or bad) back 12668 * to the other side. 12669 */ 12670 ctl_send_datamove_done(io, /*have_lock*/ 0); 12671 12672 return (0); 12673 } 12674 12675 static void 12676 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12677 { 12678 union ctl_io *io; 12679 void (*fe_datamove)(union ctl_io *io); 12680 12681 io = rq->context; 12682 12683 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12684 printf("%s: ISC DMA read failed with error %d\n", __func__, 12685 rq->ret); 12686 ctl_set_internal_failure(&io->scsiio, 12687 /*sks_valid*/ 1, 12688 /*retry_count*/ rq->ret); 12689 } 12690 12691 ctl_dt_req_free(rq); 12692 12693 /* Switch the pointer over so the FETD knows what to do */ 12694 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12695 12696 /* 12697 * Use a custom move done callback, since we need to send completion 12698 * back to the other controller, not to the backend on this side. 12699 */ 12700 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12701 12702 /* XXX KDM add checks like the ones in ctl_datamove? */ 12703 12704 fe_datamove = CTL_PORT(io)->fe_datamove; 12705 fe_datamove(io); 12706 } 12707 12708 static int 12709 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12710 { 12711 struct ctl_sg_entry *local_sglist; 12712 uint32_t len_to_go; 12713 int retval; 12714 int i; 12715 12716 retval = 0; 12717 local_sglist = io->io_hdr.local_sglist; 12718 len_to_go = io->scsiio.kern_data_len; 12719 12720 /* 12721 * The difficult thing here is that the size of the various 12722 * S/G segments may be different than the size from the 12723 * remote controller. That'll make it harder when DMAing 12724 * the data back to the other side. 12725 */ 12726 for (i = 0; len_to_go > 0; i++) { 12727 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12728 local_sglist[i].addr = 12729 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12730 12731 len_to_go -= local_sglist[i].len; 12732 } 12733 /* 12734 * Reset the number of S/G entries accordingly. The original 12735 * number of S/G entries is available in rem_sg_entries. 12736 */ 12737 io->scsiio.kern_sg_entries = i; 12738 12739 #if 0 12740 printf("%s: kern_sg_entries = %d\n", __func__, 12741 io->scsiio.kern_sg_entries); 12742 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12743 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12744 local_sglist[i].addr, local_sglist[i].len); 12745 #endif 12746 12747 return (retval); 12748 } 12749 12750 static int 12751 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12752 ctl_ha_dt_cb callback) 12753 { 12754 struct ctl_ha_dt_req *rq; 12755 struct ctl_sg_entry *remote_sglist, *local_sglist; 12756 uint32_t local_used, remote_used, total_used; 12757 int i, j, isc_ret; 12758 12759 rq = ctl_dt_req_alloc(); 12760 12761 /* 12762 * If we failed to allocate the request, and if the DMA didn't fail 12763 * anyway, set busy status. This is just a resource allocation 12764 * failure. 12765 */ 12766 if ((rq == NULL) 12767 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12768 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12769 ctl_set_busy(&io->scsiio); 12770 12771 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12772 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12773 12774 if (rq != NULL) 12775 ctl_dt_req_free(rq); 12776 12777 /* 12778 * The data move failed. We need to return status back 12779 * to the other controller. No point in trying to DMA 12780 * data to the remote controller. 12781 */ 12782 12783 ctl_send_datamove_done(io, /*have_lock*/ 0); 12784 12785 return (1); 12786 } 12787 12788 local_sglist = io->io_hdr.local_sglist; 12789 remote_sglist = io->io_hdr.remote_sglist; 12790 local_used = 0; 12791 remote_used = 0; 12792 total_used = 0; 12793 12794 /* 12795 * Pull/push the data over the wire from/to the other controller. 12796 * This takes into account the possibility that the local and 12797 * remote sglists may not be identical in terms of the size of 12798 * the elements and the number of elements. 12799 * 12800 * One fundamental assumption here is that the length allocated for 12801 * both the local and remote sglists is identical. Otherwise, we've 12802 * essentially got a coding error of some sort. 12803 */ 12804 isc_ret = CTL_HA_STATUS_SUCCESS; 12805 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12806 uint32_t cur_len; 12807 uint8_t *tmp_ptr; 12808 12809 rq->command = command; 12810 rq->context = io; 12811 12812 /* 12813 * Both pointers should be aligned. But it is possible 12814 * that the allocation length is not. They should both 12815 * also have enough slack left over at the end, though, 12816 * to round up to the next 8 byte boundary. 12817 */ 12818 cur_len = MIN(local_sglist[i].len - local_used, 12819 remote_sglist[j].len - remote_used); 12820 rq->size = cur_len; 12821 12822 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12823 tmp_ptr += local_used; 12824 12825 #if 0 12826 /* Use physical addresses when talking to ISC hardware */ 12827 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12828 /* XXX KDM use busdma */ 12829 rq->local = vtophys(tmp_ptr); 12830 } else 12831 rq->local = tmp_ptr; 12832 #else 12833 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12834 ("HA does not support BUS_ADDR")); 12835 rq->local = tmp_ptr; 12836 #endif 12837 12838 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12839 tmp_ptr += remote_used; 12840 rq->remote = tmp_ptr; 12841 12842 rq->callback = NULL; 12843 12844 local_used += cur_len; 12845 if (local_used >= local_sglist[i].len) { 12846 i++; 12847 local_used = 0; 12848 } 12849 12850 remote_used += cur_len; 12851 if (remote_used >= remote_sglist[j].len) { 12852 j++; 12853 remote_used = 0; 12854 } 12855 total_used += cur_len; 12856 12857 if (total_used >= io->scsiio.kern_data_len) 12858 rq->callback = callback; 12859 12860 #if 0 12861 printf("%s: %s: local %p remote %p size %d\n", __func__, 12862 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12863 rq->local, rq->remote, rq->size); 12864 #endif 12865 12866 isc_ret = ctl_dt_single(rq); 12867 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12868 break; 12869 } 12870 if (isc_ret != CTL_HA_STATUS_WAIT) { 12871 rq->ret = isc_ret; 12872 callback(rq); 12873 } 12874 12875 return (0); 12876 } 12877 12878 static void 12879 ctl_datamove_remote_read(union ctl_io *io) 12880 { 12881 int retval; 12882 uint32_t i; 12883 12884 /* 12885 * This will send an error to the other controller in the case of a 12886 * failure. 12887 */ 12888 retval = ctl_datamove_remote_sgl_setup(io); 12889 if (retval != 0) 12890 return; 12891 12892 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12893 ctl_datamove_remote_read_cb); 12894 if (retval != 0) { 12895 /* 12896 * Make sure we free memory if there was an error.. The 12897 * ctl_datamove_remote_xfer() function will send the 12898 * datamove done message, or call the callback with an 12899 * error if there is a problem. 12900 */ 12901 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12902 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12903 free(io->io_hdr.remote_sglist, M_CTL); 12904 io->io_hdr.remote_sglist = NULL; 12905 io->io_hdr.local_sglist = NULL; 12906 } 12907 } 12908 12909 /* 12910 * Process a datamove request from the other controller. This is used for 12911 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12912 * first. Once that is complete, the data gets DMAed into the remote 12913 * controller's memory. For reads, we DMA from the remote controller's 12914 * memory into our memory first, and then move it out to the FETD. 12915 */ 12916 static void 12917 ctl_datamove_remote(union ctl_io *io) 12918 { 12919 12920 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12921 12922 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12923 ctl_failover_io(io, /*have_lock*/ 0); 12924 return; 12925 } 12926 12927 /* 12928 * Note that we look for an aborted I/O here, but don't do some of 12929 * the other checks that ctl_datamove() normally does. 12930 * We don't need to run the datamove delay code, since that should 12931 * have been done if need be on the other controller. 12932 */ 12933 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12934 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12935 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12936 io->io_hdr.nexus.targ_port, 12937 io->io_hdr.nexus.targ_lun); 12938 io->io_hdr.port_status = 31338; 12939 ctl_send_datamove_done(io, /*have_lock*/ 0); 12940 return; 12941 } 12942 12943 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12944 ctl_datamove_remote_write(io); 12945 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12946 ctl_datamove_remote_read(io); 12947 else { 12948 io->io_hdr.port_status = 31339; 12949 ctl_send_datamove_done(io, /*have_lock*/ 0); 12950 } 12951 } 12952 12953 static void 12954 ctl_process_done(union ctl_io *io) 12955 { 12956 struct ctl_softc *softc = CTL_SOFTC(io); 12957 struct ctl_port *port = CTL_PORT(io); 12958 struct ctl_lun *lun = CTL_LUN(io); 12959 void (*fe_done)(union ctl_io *io); 12960 union ctl_ha_msg msg; 12961 12962 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12963 fe_done = port->fe_done; 12964 12965 #ifdef CTL_TIME_IO 12966 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12967 char str[256]; 12968 char path_str[64]; 12969 struct sbuf sb; 12970 12971 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12972 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12973 12974 sbuf_cat(&sb, path_str); 12975 switch (io->io_hdr.io_type) { 12976 case CTL_IO_SCSI: 12977 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12978 sbuf_printf(&sb, "\n"); 12979 sbuf_cat(&sb, path_str); 12980 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12981 io->scsiio.tag_num, io->scsiio.tag_type); 12982 break; 12983 case CTL_IO_TASK: 12984 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12985 "Tag Type: %d\n", io->taskio.task_action, 12986 io->taskio.tag_num, io->taskio.tag_type); 12987 break; 12988 default: 12989 panic("%s: Invalid CTL I/O type %d\n", 12990 __func__, io->io_hdr.io_type); 12991 } 12992 sbuf_cat(&sb, path_str); 12993 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12994 (intmax_t)time_uptime - io->io_hdr.start_time); 12995 sbuf_finish(&sb); 12996 printf("%s", sbuf_data(&sb)); 12997 } 12998 #endif /* CTL_TIME_IO */ 12999 13000 switch (io->io_hdr.io_type) { 13001 case CTL_IO_SCSI: 13002 break; 13003 case CTL_IO_TASK: 13004 if (ctl_debug & CTL_DEBUG_INFO) 13005 ctl_io_error_print(io, NULL); 13006 fe_done(io); 13007 return; 13008 default: 13009 panic("%s: Invalid CTL I/O type %d\n", 13010 __func__, io->io_hdr.io_type); 13011 } 13012 13013 if (lun == NULL) { 13014 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13015 io->io_hdr.nexus.targ_mapped_lun)); 13016 goto bailout; 13017 } 13018 13019 mtx_lock(&lun->lun_lock); 13020 13021 /* 13022 * Check to see if we have any informational exception and status 13023 * of this command can be modified to report it in form of either 13024 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13025 */ 13026 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13027 io->io_hdr.status == CTL_SUCCESS && 13028 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13029 uint8_t mrie = lun->MODE_IE.mrie; 13030 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13031 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13032 if (((mrie == SIEP_MRIE_REC_COND && per) || 13033 mrie == SIEP_MRIE_REC_UNCOND || 13034 mrie == SIEP_MRIE_NO_SENSE) && 13035 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13036 CTL_CMD_FLAG_NO_SENSE) == 0) { 13037 ctl_set_sense(&io->scsiio, 13038 /*current_error*/ 1, 13039 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13040 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13041 /*asc*/ lun->ie_asc, 13042 /*ascq*/ lun->ie_ascq, 13043 SSD_ELEM_NONE); 13044 lun->ie_reported = 1; 13045 } 13046 } else if (lun->ie_reported < 0) 13047 lun->ie_reported = 0; 13048 13049 /* 13050 * Check to see if we have any errors to inject here. We only 13051 * inject errors for commands that don't already have errors set. 13052 */ 13053 if (!STAILQ_EMPTY(&lun->error_list) && 13054 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13055 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13056 ctl_inject_error(lun, io); 13057 13058 /* 13059 * XXX KDM how do we treat commands that aren't completed 13060 * successfully? 13061 * 13062 * XXX KDM should we also track I/O latency? 13063 */ 13064 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13065 io->io_hdr.io_type == CTL_IO_SCSI) { 13066 int type; 13067 #ifdef CTL_TIME_IO 13068 struct bintime bt; 13069 13070 getbinuptime(&bt); 13071 bintime_sub(&bt, &io->io_hdr.start_bt); 13072 #endif 13073 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13074 CTL_FLAG_DATA_IN) 13075 type = CTL_STATS_READ; 13076 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13077 CTL_FLAG_DATA_OUT) 13078 type = CTL_STATS_WRITE; 13079 else 13080 type = CTL_STATS_NO_IO; 13081 13082 #ifdef CTL_LEGACY_STATS 13083 uint32_t targ_port = port->targ_port; 13084 lun->legacy_stats.ports[targ_port].bytes[type] += 13085 io->scsiio.kern_total_len; 13086 lun->legacy_stats.ports[targ_port].operations[type] ++; 13087 lun->legacy_stats.ports[targ_port].num_dmas[type] += 13088 io->io_hdr.num_dmas; 13089 #ifdef CTL_TIME_IO 13090 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type], 13091 &io->io_hdr.dma_bt); 13092 bintime_add(&lun->legacy_stats.ports[targ_port].time[type], 13093 &bt); 13094 #endif 13095 #endif /* CTL_LEGACY_STATS */ 13096 13097 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13098 lun->stats.operations[type] ++; 13099 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13100 #ifdef CTL_TIME_IO 13101 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13102 bintime_add(&lun->stats.time[type], &bt); 13103 #endif 13104 13105 mtx_lock(&port->port_lock); 13106 port->stats.bytes[type] += io->scsiio.kern_total_len; 13107 port->stats.operations[type] ++; 13108 port->stats.dmas[type] += io->io_hdr.num_dmas; 13109 #ifdef CTL_TIME_IO 13110 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13111 bintime_add(&port->stats.time[type], &bt); 13112 #endif 13113 mtx_unlock(&port->port_lock); 13114 } 13115 13116 /* 13117 * Remove this from the OOA queue. 13118 */ 13119 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13120 #ifdef CTL_TIME_IO 13121 if (TAILQ_EMPTY(&lun->ooa_queue)) 13122 lun->last_busy = getsbinuptime(); 13123 #endif 13124 13125 /* 13126 * Run through the blocked queue on this LUN and see if anything 13127 * has become unblocked, now that this transaction is done. 13128 */ 13129 ctl_check_blocked(lun); 13130 13131 /* 13132 * If the LUN has been invalidated, free it if there is nothing 13133 * left on its OOA queue. 13134 */ 13135 if ((lun->flags & CTL_LUN_INVALID) 13136 && TAILQ_EMPTY(&lun->ooa_queue)) { 13137 mtx_unlock(&lun->lun_lock); 13138 ctl_free_lun(lun); 13139 } else 13140 mtx_unlock(&lun->lun_lock); 13141 13142 bailout: 13143 13144 /* 13145 * If this command has been aborted, make sure we set the status 13146 * properly. The FETD is responsible for freeing the I/O and doing 13147 * whatever it needs to do to clean up its state. 13148 */ 13149 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13150 ctl_set_task_aborted(&io->scsiio); 13151 13152 /* 13153 * If enabled, print command error status. 13154 */ 13155 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13156 (ctl_debug & CTL_DEBUG_INFO) != 0) 13157 ctl_io_error_print(io, NULL); 13158 13159 /* 13160 * Tell the FETD or the other shelf controller we're done with this 13161 * command. Note that only SCSI commands get to this point. Task 13162 * management commands are completed above. 13163 */ 13164 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13165 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13166 memset(&msg, 0, sizeof(msg)); 13167 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13168 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13169 msg.hdr.nexus = io->io_hdr.nexus; 13170 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13171 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13172 M_WAITOK); 13173 } 13174 13175 fe_done(io); 13176 } 13177 13178 /* 13179 * Front end should call this if it doesn't do autosense. When the request 13180 * sense comes back in from the initiator, we'll dequeue this and send it. 13181 */ 13182 int 13183 ctl_queue_sense(union ctl_io *io) 13184 { 13185 struct ctl_softc *softc = CTL_SOFTC(io); 13186 struct ctl_port *port = CTL_PORT(io); 13187 struct ctl_lun *lun; 13188 struct scsi_sense_data *ps; 13189 uint32_t initidx, p, targ_lun; 13190 13191 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13192 13193 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13194 13195 /* 13196 * LUN lookup will likely move to the ctl_work_thread() once we 13197 * have our new queueing infrastructure (that doesn't put things on 13198 * a per-LUN queue initially). That is so that we can handle 13199 * things like an INQUIRY to a LUN that we don't have enabled. We 13200 * can't deal with that right now. 13201 * If we don't have a LUN for this, just toss the sense information. 13202 */ 13203 mtx_lock(&softc->ctl_lock); 13204 if (targ_lun >= ctl_max_luns || 13205 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13206 mtx_unlock(&softc->ctl_lock); 13207 goto bailout; 13208 } 13209 mtx_lock(&lun->lun_lock); 13210 mtx_unlock(&softc->ctl_lock); 13211 13212 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13213 p = initidx / CTL_MAX_INIT_PER_PORT; 13214 if (lun->pending_sense[p] == NULL) { 13215 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13216 M_CTL, M_NOWAIT | M_ZERO); 13217 } 13218 if ((ps = lun->pending_sense[p]) != NULL) { 13219 ps += initidx % CTL_MAX_INIT_PER_PORT; 13220 memset(ps, 0, sizeof(*ps)); 13221 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13222 } 13223 mtx_unlock(&lun->lun_lock); 13224 13225 bailout: 13226 ctl_free_io(io); 13227 return (CTL_RETVAL_COMPLETE); 13228 } 13229 13230 /* 13231 * Primary command inlet from frontend ports. All SCSI and task I/O 13232 * requests must go through this function. 13233 */ 13234 int 13235 ctl_queue(union ctl_io *io) 13236 { 13237 struct ctl_port *port = CTL_PORT(io); 13238 13239 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13240 13241 #ifdef CTL_TIME_IO 13242 io->io_hdr.start_time = time_uptime; 13243 getbinuptime(&io->io_hdr.start_bt); 13244 #endif /* CTL_TIME_IO */ 13245 13246 /* Map FE-specific LUN ID into global one. */ 13247 io->io_hdr.nexus.targ_mapped_lun = 13248 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13249 13250 switch (io->io_hdr.io_type) { 13251 case CTL_IO_SCSI: 13252 case CTL_IO_TASK: 13253 if (ctl_debug & CTL_DEBUG_CDB) 13254 ctl_io_print(io); 13255 ctl_enqueue_incoming(io); 13256 break; 13257 default: 13258 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13259 return (EINVAL); 13260 } 13261 13262 return (CTL_RETVAL_COMPLETE); 13263 } 13264 13265 #ifdef CTL_IO_DELAY 13266 static void 13267 ctl_done_timer_wakeup(void *arg) 13268 { 13269 union ctl_io *io; 13270 13271 io = (union ctl_io *)arg; 13272 ctl_done(io); 13273 } 13274 #endif /* CTL_IO_DELAY */ 13275 13276 void 13277 ctl_serseq_done(union ctl_io *io) 13278 { 13279 struct ctl_lun *lun = CTL_LUN(io);; 13280 13281 if (lun->be_lun == NULL || 13282 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13283 return; 13284 mtx_lock(&lun->lun_lock); 13285 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13286 ctl_check_blocked(lun); 13287 mtx_unlock(&lun->lun_lock); 13288 } 13289 13290 void 13291 ctl_done(union ctl_io *io) 13292 { 13293 13294 /* 13295 * Enable this to catch duplicate completion issues. 13296 */ 13297 #if 0 13298 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13299 printf("%s: type %d msg %d cdb %x iptl: " 13300 "%u:%u:%u tag 0x%04x " 13301 "flag %#x status %x\n", 13302 __func__, 13303 io->io_hdr.io_type, 13304 io->io_hdr.msg_type, 13305 io->scsiio.cdb[0], 13306 io->io_hdr.nexus.initid, 13307 io->io_hdr.nexus.targ_port, 13308 io->io_hdr.nexus.targ_lun, 13309 (io->io_hdr.io_type == 13310 CTL_IO_TASK) ? 13311 io->taskio.tag_num : 13312 io->scsiio.tag_num, 13313 io->io_hdr.flags, 13314 io->io_hdr.status); 13315 } else 13316 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13317 #endif 13318 13319 /* 13320 * This is an internal copy of an I/O, and should not go through 13321 * the normal done processing logic. 13322 */ 13323 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13324 return; 13325 13326 #ifdef CTL_IO_DELAY 13327 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13328 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13329 } else { 13330 struct ctl_lun *lun = CTL_LUN(io); 13331 13332 if ((lun != NULL) 13333 && (lun->delay_info.done_delay > 0)) { 13334 13335 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13336 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13337 callout_reset(&io->io_hdr.delay_callout, 13338 lun->delay_info.done_delay * hz, 13339 ctl_done_timer_wakeup, io); 13340 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13341 lun->delay_info.done_delay = 0; 13342 return; 13343 } 13344 } 13345 #endif /* CTL_IO_DELAY */ 13346 13347 ctl_enqueue_done(io); 13348 } 13349 13350 static void 13351 ctl_work_thread(void *arg) 13352 { 13353 struct ctl_thread *thr = (struct ctl_thread *)arg; 13354 struct ctl_softc *softc = thr->ctl_softc; 13355 union ctl_io *io; 13356 int retval; 13357 13358 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13359 13360 while (!softc->shutdown) { 13361 /* 13362 * We handle the queues in this order: 13363 * - ISC 13364 * - done queue (to free up resources, unblock other commands) 13365 * - incoming queue 13366 * - RtR queue 13367 * 13368 * If those queues are empty, we break out of the loop and 13369 * go to sleep. 13370 */ 13371 mtx_lock(&thr->queue_lock); 13372 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13373 if (io != NULL) { 13374 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13375 mtx_unlock(&thr->queue_lock); 13376 ctl_handle_isc(io); 13377 continue; 13378 } 13379 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13380 if (io != NULL) { 13381 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13382 /* clear any blocked commands, call fe_done */ 13383 mtx_unlock(&thr->queue_lock); 13384 ctl_process_done(io); 13385 continue; 13386 } 13387 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13388 if (io != NULL) { 13389 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13390 mtx_unlock(&thr->queue_lock); 13391 if (io->io_hdr.io_type == CTL_IO_TASK) 13392 ctl_run_task(io); 13393 else 13394 ctl_scsiio_precheck(softc, &io->scsiio); 13395 continue; 13396 } 13397 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13398 if (io != NULL) { 13399 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13400 mtx_unlock(&thr->queue_lock); 13401 retval = ctl_scsiio(&io->scsiio); 13402 if (retval != CTL_RETVAL_COMPLETE) 13403 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13404 continue; 13405 } 13406 13407 /* Sleep until we have something to do. */ 13408 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13409 } 13410 thr->thread = NULL; 13411 kthread_exit(); 13412 } 13413 13414 static void 13415 ctl_lun_thread(void *arg) 13416 { 13417 struct ctl_softc *softc = (struct ctl_softc *)arg; 13418 struct ctl_be_lun *be_lun; 13419 13420 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13421 13422 while (!softc->shutdown) { 13423 mtx_lock(&softc->ctl_lock); 13424 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13425 if (be_lun != NULL) { 13426 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13427 mtx_unlock(&softc->ctl_lock); 13428 ctl_create_lun(be_lun); 13429 continue; 13430 } 13431 13432 /* Sleep until we have something to do. */ 13433 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13434 PDROP | PRIBIO, "-", 0); 13435 } 13436 softc->lun_thread = NULL; 13437 kthread_exit(); 13438 } 13439 13440 static void 13441 ctl_thresh_thread(void *arg) 13442 { 13443 struct ctl_softc *softc = (struct ctl_softc *)arg; 13444 struct ctl_lun *lun; 13445 struct ctl_logical_block_provisioning_page *page; 13446 const char *attr; 13447 union ctl_ha_msg msg; 13448 uint64_t thres, val; 13449 int i, e, set; 13450 13451 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13452 13453 while (!softc->shutdown) { 13454 mtx_lock(&softc->ctl_lock); 13455 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13456 if ((lun->flags & CTL_LUN_DISABLED) || 13457 (lun->flags & CTL_LUN_NO_MEDIA) || 13458 lun->backend->lun_attr == NULL) 13459 continue; 13460 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13461 softc->ha_mode == CTL_HA_MODE_XFER) 13462 continue; 13463 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13464 continue; 13465 e = 0; 13466 page = &lun->MODE_LBP; 13467 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13468 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13469 continue; 13470 thres = scsi_4btoul(page->descr[i].count); 13471 thres <<= CTL_LBP_EXPONENT; 13472 switch (page->descr[i].resource) { 13473 case 0x01: 13474 attr = "blocksavail"; 13475 break; 13476 case 0x02: 13477 attr = "blocksused"; 13478 break; 13479 case 0xf1: 13480 attr = "poolblocksavail"; 13481 break; 13482 case 0xf2: 13483 attr = "poolblocksused"; 13484 break; 13485 default: 13486 continue; 13487 } 13488 mtx_unlock(&softc->ctl_lock); // XXX 13489 val = lun->backend->lun_attr( 13490 lun->be_lun->be_lun, attr); 13491 mtx_lock(&softc->ctl_lock); 13492 if (val == UINT64_MAX) 13493 continue; 13494 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13495 == SLBPPD_ARMING_INC) 13496 e = (val >= thres); 13497 else 13498 e = (val <= thres); 13499 if (e) 13500 break; 13501 } 13502 mtx_lock(&lun->lun_lock); 13503 if (e) { 13504 scsi_u64to8b((uint8_t *)&page->descr[i] - 13505 (uint8_t *)page, lun->ua_tpt_info); 13506 if (lun->lasttpt == 0 || 13507 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13508 lun->lasttpt = time_uptime; 13509 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13510 set = 1; 13511 } else 13512 set = 0; 13513 } else { 13514 lun->lasttpt = 0; 13515 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13516 set = -1; 13517 } 13518 mtx_unlock(&lun->lun_lock); 13519 if (set != 0 && 13520 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13521 /* Send msg to other side. */ 13522 bzero(&msg.ua, sizeof(msg.ua)); 13523 msg.hdr.msg_type = CTL_MSG_UA; 13524 msg.hdr.nexus.initid = -1; 13525 msg.hdr.nexus.targ_port = -1; 13526 msg.hdr.nexus.targ_lun = lun->lun; 13527 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13528 msg.ua.ua_all = 1; 13529 msg.ua.ua_set = (set > 0); 13530 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13531 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13532 mtx_unlock(&softc->ctl_lock); // XXX 13533 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13534 sizeof(msg.ua), M_WAITOK); 13535 mtx_lock(&softc->ctl_lock); 13536 } 13537 } 13538 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13539 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz); 13540 } 13541 softc->thresh_thread = NULL; 13542 kthread_exit(); 13543 } 13544 13545 static void 13546 ctl_enqueue_incoming(union ctl_io *io) 13547 { 13548 struct ctl_softc *softc = CTL_SOFTC(io); 13549 struct ctl_thread *thr; 13550 u_int idx; 13551 13552 idx = (io->io_hdr.nexus.targ_port * 127 + 13553 io->io_hdr.nexus.initid) % worker_threads; 13554 thr = &softc->threads[idx]; 13555 mtx_lock(&thr->queue_lock); 13556 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13557 mtx_unlock(&thr->queue_lock); 13558 wakeup(thr); 13559 } 13560 13561 static void 13562 ctl_enqueue_rtr(union ctl_io *io) 13563 { 13564 struct ctl_softc *softc = CTL_SOFTC(io); 13565 struct ctl_thread *thr; 13566 13567 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13568 mtx_lock(&thr->queue_lock); 13569 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13570 mtx_unlock(&thr->queue_lock); 13571 wakeup(thr); 13572 } 13573 13574 static void 13575 ctl_enqueue_done(union ctl_io *io) 13576 { 13577 struct ctl_softc *softc = CTL_SOFTC(io); 13578 struct ctl_thread *thr; 13579 13580 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13581 mtx_lock(&thr->queue_lock); 13582 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13583 mtx_unlock(&thr->queue_lock); 13584 wakeup(thr); 13585 } 13586 13587 static void 13588 ctl_enqueue_isc(union ctl_io *io) 13589 { 13590 struct ctl_softc *softc = CTL_SOFTC(io); 13591 struct ctl_thread *thr; 13592 13593 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13594 mtx_lock(&thr->queue_lock); 13595 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13596 mtx_unlock(&thr->queue_lock); 13597 wakeup(thr); 13598 } 13599 13600 /* 13601 * vim: ts=8 13602 */ 13603