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/proc.h> 70 #include <sys/sched.h> 71 #include <sys/sysctl.h> 72 #include <sys/nv.h> 73 #include <sys/dnv.h> 74 #include <vm/uma.h> 75 76 #include <cam/cam.h> 77 #include <cam/scsi/scsi_all.h> 78 #include <cam/scsi/scsi_cd.h> 79 #include <cam/scsi/scsi_da.h> 80 #include <cam/ctl/ctl_io.h> 81 #include <cam/ctl/ctl.h> 82 #include <cam/ctl/ctl_frontend.h> 83 #include <cam/ctl/ctl_util.h> 84 #include <cam/ctl/ctl_backend.h> 85 #include <cam/ctl/ctl_ioctl.h> 86 #include <cam/ctl/ctl_ha.h> 87 #include <cam/ctl/ctl_private.h> 88 #include <cam/ctl/ctl_debug.h> 89 #include <cam/ctl/ctl_scsi_all.h> 90 #include <cam/ctl/ctl_error.h> 91 92 struct ctl_softc *control_softc = NULL; 93 94 /* 95 * Template mode pages. 96 */ 97 98 /* 99 * Note that these are default values only. The actual values will be 100 * filled in when the user does a mode sense. 101 */ 102 const static struct scsi_da_rw_recovery_page rw_er_page_default = { 103 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 104 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 105 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 106 /*read_retry_count*/0, 107 /*correction_span*/0, 108 /*head_offset_count*/0, 109 /*data_strobe_offset_cnt*/0, 110 /*byte8*/SMS_RWER_LBPERE, 111 /*write_retry_count*/0, 112 /*reserved2*/0, 113 /*recovery_time_limit*/{0, 0}, 114 }; 115 116 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 117 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 118 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 119 /*byte3*/SMS_RWER_PER, 120 /*read_retry_count*/0, 121 /*correction_span*/0, 122 /*head_offset_count*/0, 123 /*data_strobe_offset_cnt*/0, 124 /*byte8*/SMS_RWER_LBPERE, 125 /*write_retry_count*/0, 126 /*reserved2*/0, 127 /*recovery_time_limit*/{0, 0}, 128 }; 129 130 const static struct scsi_format_page format_page_default = { 131 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 132 /*page_length*/sizeof(struct scsi_format_page) - 2, 133 /*tracks_per_zone*/ {0, 0}, 134 /*alt_sectors_per_zone*/ {0, 0}, 135 /*alt_tracks_per_zone*/ {0, 0}, 136 /*alt_tracks_per_lun*/ {0, 0}, 137 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 138 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 139 /*bytes_per_sector*/ {0, 0}, 140 /*interleave*/ {0, 0}, 141 /*track_skew*/ {0, 0}, 142 /*cylinder_skew*/ {0, 0}, 143 /*flags*/ SFP_HSEC, 144 /*reserved*/ {0, 0, 0} 145 }; 146 147 const static struct scsi_format_page format_page_changeable = { 148 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 149 /*page_length*/sizeof(struct scsi_format_page) - 2, 150 /*tracks_per_zone*/ {0, 0}, 151 /*alt_sectors_per_zone*/ {0, 0}, 152 /*alt_tracks_per_zone*/ {0, 0}, 153 /*alt_tracks_per_lun*/ {0, 0}, 154 /*sectors_per_track*/ {0, 0}, 155 /*bytes_per_sector*/ {0, 0}, 156 /*interleave*/ {0, 0}, 157 /*track_skew*/ {0, 0}, 158 /*cylinder_skew*/ {0, 0}, 159 /*flags*/ 0, 160 /*reserved*/ {0, 0, 0} 161 }; 162 163 const static struct scsi_rigid_disk_page rigid_disk_page_default = { 164 /*page_code*/SMS_RIGID_DISK_PAGE, 165 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 166 /*cylinders*/ {0, 0, 0}, 167 /*heads*/ CTL_DEFAULT_HEADS, 168 /*start_write_precomp*/ {0, 0, 0}, 169 /*start_reduced_current*/ {0, 0, 0}, 170 /*step_rate*/ {0, 0}, 171 /*landing_zone_cylinder*/ {0, 0, 0}, 172 /*rpl*/ SRDP_RPL_DISABLED, 173 /*rotational_offset*/ 0, 174 /*reserved1*/ 0, 175 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 176 CTL_DEFAULT_ROTATION_RATE & 0xff}, 177 /*reserved2*/ {0, 0} 178 }; 179 180 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 181 /*page_code*/SMS_RIGID_DISK_PAGE, 182 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 183 /*cylinders*/ {0, 0, 0}, 184 /*heads*/ 0, 185 /*start_write_precomp*/ {0, 0, 0}, 186 /*start_reduced_current*/ {0, 0, 0}, 187 /*step_rate*/ {0, 0}, 188 /*landing_zone_cylinder*/ {0, 0, 0}, 189 /*rpl*/ 0, 190 /*rotational_offset*/ 0, 191 /*reserved1*/ 0, 192 /*rotation_rate*/ {0, 0}, 193 /*reserved2*/ {0, 0} 194 }; 195 196 const static struct scsi_da_verify_recovery_page verify_er_page_default = { 197 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 198 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 199 /*byte3*/0, 200 /*read_retry_count*/0, 201 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 202 /*recovery_time_limit*/{0, 0}, 203 }; 204 205 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 206 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 207 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 208 /*byte3*/SMS_VER_PER, 209 /*read_retry_count*/0, 210 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 211 /*recovery_time_limit*/{0, 0}, 212 }; 213 214 const static struct scsi_caching_page caching_page_default = { 215 /*page_code*/SMS_CACHING_PAGE, 216 /*page_length*/sizeof(struct scsi_caching_page) - 2, 217 /*flags1*/ SCP_DISC | SCP_WCE, 218 /*ret_priority*/ 0, 219 /*disable_pf_transfer_len*/ {0xff, 0xff}, 220 /*min_prefetch*/ {0, 0}, 221 /*max_prefetch*/ {0xff, 0xff}, 222 /*max_pf_ceiling*/ {0xff, 0xff}, 223 /*flags2*/ 0, 224 /*cache_segments*/ 0, 225 /*cache_seg_size*/ {0, 0}, 226 /*reserved*/ 0, 227 /*non_cache_seg_size*/ {0, 0, 0} 228 }; 229 230 const static struct scsi_caching_page caching_page_changeable = { 231 /*page_code*/SMS_CACHING_PAGE, 232 /*page_length*/sizeof(struct scsi_caching_page) - 2, 233 /*flags1*/ SCP_WCE | SCP_RCD, 234 /*ret_priority*/ 0, 235 /*disable_pf_transfer_len*/ {0, 0}, 236 /*min_prefetch*/ {0, 0}, 237 /*max_prefetch*/ {0, 0}, 238 /*max_pf_ceiling*/ {0, 0}, 239 /*flags2*/ 0, 240 /*cache_segments*/ 0, 241 /*cache_seg_size*/ {0, 0}, 242 /*reserved*/ 0, 243 /*non_cache_seg_size*/ {0, 0, 0} 244 }; 245 246 const static struct scsi_control_page control_page_default = { 247 /*page_code*/SMS_CONTROL_MODE_PAGE, 248 /*page_length*/sizeof(struct scsi_control_page) - 2, 249 /*rlec*/0, 250 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 251 /*eca_and_aen*/0, 252 /*flags4*/SCP_TAS, 253 /*aen_holdoff_period*/{0, 0}, 254 /*busy_timeout_period*/{0, 0}, 255 /*extended_selftest_completion_time*/{0, 0} 256 }; 257 258 const static struct scsi_control_page control_page_changeable = { 259 /*page_code*/SMS_CONTROL_MODE_PAGE, 260 /*page_length*/sizeof(struct scsi_control_page) - 2, 261 /*rlec*/SCP_DSENSE, 262 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 263 /*eca_and_aen*/SCP_SWP, 264 /*flags4*/0, 265 /*aen_holdoff_period*/{0, 0}, 266 /*busy_timeout_period*/{0, 0}, 267 /*extended_selftest_completion_time*/{0, 0} 268 }; 269 270 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 271 272 const static struct scsi_control_ext_page control_ext_page_default = { 273 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 274 /*subpage_code*/0x01, 275 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 276 /*flags*/0, 277 /*prio*/0, 278 /*max_sense*/0 279 }; 280 281 const static struct scsi_control_ext_page control_ext_page_changeable = { 282 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 283 /*subpage_code*/0x01, 284 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 285 /*flags*/0, 286 /*prio*/0, 287 /*max_sense*/0xff 288 }; 289 290 const static struct scsi_info_exceptions_page ie_page_default = { 291 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 292 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 293 /*info_flags*/SIEP_FLAGS_EWASC, 294 /*mrie*/SIEP_MRIE_NO, 295 /*interval_timer*/{0, 0, 0, 0}, 296 /*report_count*/{0, 0, 0, 1} 297 }; 298 299 const static struct scsi_info_exceptions_page ie_page_changeable = { 300 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 301 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 302 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 303 SIEP_FLAGS_LOGERR, 304 /*mrie*/0x0f, 305 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 306 /*report_count*/{0xff, 0xff, 0xff, 0xff} 307 }; 308 309 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 310 311 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 312 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 313 /*subpage_code*/0x02, 314 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 315 /*flags*/0, 316 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 317 /*descr*/{}}, 318 {{/*flags*/0, 319 /*resource*/0x01, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0x02, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}}, 326 {/*flags*/0, 327 /*resource*/0xf1, 328 /*reserved*/{0, 0}, 329 /*count*/{0, 0, 0, 0}}, 330 {/*flags*/0, 331 /*resource*/0xf2, 332 /*reserved*/{0, 0}, 333 /*count*/{0, 0, 0, 0}} 334 } 335 }; 336 337 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 338 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 339 /*subpage_code*/0x02, 340 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 341 /*flags*/SLBPP_SITUA, 342 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 343 /*descr*/{}}, 344 {{/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}}, 352 {/*flags*/0, 353 /*resource*/0, 354 /*reserved*/{0, 0}, 355 /*count*/{0, 0, 0, 0}}, 356 {/*flags*/0, 357 /*resource*/0, 358 /*reserved*/{0, 0}, 359 /*count*/{0, 0, 0, 0}} 360 } 361 }; 362 363 const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 364 /*page_code*/SMS_CDDVD_CAPS_PAGE, 365 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 366 /*caps1*/0x3f, 367 /*caps2*/0x00, 368 /*caps3*/0xf0, 369 /*caps4*/0x00, 370 /*caps5*/0x29, 371 /*caps6*/0x00, 372 /*obsolete*/{0, 0}, 373 /*nvol_levels*/{0, 0}, 374 /*buffer_size*/{8, 0}, 375 /*obsolete2*/{0, 0}, 376 /*reserved*/0, 377 /*digital*/0, 378 /*obsolete3*/0, 379 /*copy_management*/0, 380 /*reserved2*/0, 381 /*rotation_control*/0, 382 /*cur_write_speed*/0, 383 /*num_speed_descr*/0, 384 }; 385 386 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 387 /*page_code*/SMS_CDDVD_CAPS_PAGE, 388 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 389 /*caps1*/0, 390 /*caps2*/0, 391 /*caps3*/0, 392 /*caps4*/0, 393 /*caps5*/0, 394 /*caps6*/0, 395 /*obsolete*/{0, 0}, 396 /*nvol_levels*/{0, 0}, 397 /*buffer_size*/{0, 0}, 398 /*obsolete2*/{0, 0}, 399 /*reserved*/0, 400 /*digital*/0, 401 /*obsolete3*/0, 402 /*copy_management*/0, 403 /*reserved2*/0, 404 /*rotation_control*/0, 405 /*cur_write_speed*/0, 406 /*num_speed_descr*/0, 407 }; 408 409 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 410 "CAM Target Layer"); 411 static int worker_threads = -1; 412 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 413 &worker_threads, 1, "Number of worker threads"); 414 static int ctl_debug = CTL_DEBUG_NONE; 415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 416 &ctl_debug, 0, "Enabled debug flags"); 417 static int ctl_lun_map_size = 1024; 418 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 419 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 420 #ifdef CTL_TIME_IO 421 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS; 422 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN, 423 &ctl_time_io_secs, 0, "Log requests taking more seconds"); 424 #endif 425 426 /* 427 * Maximum number of LUNs we support. MUST be a power of 2. 428 */ 429 #define CTL_DEFAULT_MAX_LUNS 1024 430 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 431 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns); 432 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN, 433 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs"); 434 435 /* 436 * Maximum number of ports registered at one time. 437 */ 438 #define CTL_DEFAULT_MAX_PORTS 256 439 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 440 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports); 441 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN, 442 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports"); 443 444 /* 445 * Maximum number of initiators we support. 446 */ 447 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports) 448 449 /* 450 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 451 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 452 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92), 453 * Block limits (0xB0), Block Device Characteristics (0xB1) and 454 * Logical Block Provisioning (0xB2) 455 */ 456 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11 457 458 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 459 int param); 460 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 461 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 462 static int ctl_init(void); 463 static int ctl_shutdown(void); 464 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 465 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 466 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 467 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 468 struct ctl_ooa *ooa_hdr, 469 struct ctl_ooa_entry *kern_entries); 470 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 471 struct thread *td); 472 static int ctl_enable_lun(struct ctl_lun *lun); 473 static int ctl_disable_lun(struct ctl_lun *lun); 474 static int ctl_free_lun(struct ctl_lun *lun); 475 476 static int ctl_do_mode_select(union ctl_io *io); 477 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 478 uint64_t res_key, uint64_t sa_res_key, 479 uint8_t type, uint32_t residx, 480 struct ctl_scsiio *ctsio, 481 struct scsi_per_res_out *cdb, 482 struct scsi_per_res_out_parms* param); 483 static void ctl_pro_preempt_other(struct ctl_lun *lun, 484 union ctl_ha_msg *msg); 485 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 486 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 487 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 488 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 489 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 490 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 491 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 492 int alloc_len); 493 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len); 494 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 495 int alloc_len); 496 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 497 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 498 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 499 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 500 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 501 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 502 bool seq); 503 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 504 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 505 union ctl_io *pending_io, union ctl_io *ooa_io); 506 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 507 union ctl_io **starting_io); 508 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, 509 bool skip); 510 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io, 511 bool skip); 512 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 513 const struct ctl_cmd_entry *entry, 514 struct ctl_scsiio *ctsio); 515 static void ctl_failover_lun(union ctl_io *io); 516 static void ctl_scsiio_precheck(struct ctl_scsiio *ctsio); 517 static int ctl_scsiio(struct ctl_scsiio *ctsio); 518 519 static int ctl_target_reset(union ctl_io *io); 520 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, 521 ctl_ua_type ua_type); 522 static int ctl_lun_reset(union ctl_io *io); 523 static int ctl_abort_task(union ctl_io *io); 524 static int ctl_abort_task_set(union ctl_io *io); 525 static int ctl_query_task(union ctl_io *io, int task_set); 526 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 527 ctl_ua_type ua_type); 528 static int ctl_i_t_nexus_reset(union ctl_io *io); 529 static int ctl_query_async_event(union ctl_io *io); 530 static void ctl_run_task(union ctl_io *io); 531 #ifdef CTL_IO_DELAY 532 static void ctl_datamove_timer_wakeup(void *arg); 533 static void ctl_done_timer_wakeup(void *arg); 534 #endif /* CTL_IO_DELAY */ 535 536 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 537 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 538 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr); 539 static void ctl_datamove_remote_write(union ctl_io *io); 540 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr); 541 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 542 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 543 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 544 ctl_ha_dt_cb callback); 545 static void ctl_datamove_remote_read(union ctl_io *io); 546 static void ctl_datamove_remote(union ctl_io *io); 547 static void ctl_process_done(union ctl_io *io); 548 static void ctl_thresh_thread(void *arg); 549 static void ctl_work_thread(void *arg); 550 static void ctl_enqueue_incoming(union ctl_io *io); 551 static void ctl_enqueue_rtr(union ctl_io *io); 552 static void ctl_enqueue_done(union ctl_io *io); 553 static void ctl_enqueue_isc(union ctl_io *io); 554 static const struct ctl_cmd_entry * 555 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 556 static const struct ctl_cmd_entry * 557 ctl_validate_command(struct ctl_scsiio *ctsio); 558 static int ctl_cmd_applicable(uint8_t lun_type, 559 const struct ctl_cmd_entry *entry); 560 static int ctl_ha_init(void); 561 static int ctl_ha_shutdown(void); 562 563 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 564 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 565 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 566 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 567 568 /* 569 * Load the serialization table. This isn't very pretty, but is probably 570 * the easiest way to do it. 571 */ 572 #include "ctl_ser_table.c" 573 574 /* 575 * We only need to define open, close and ioctl routines for this driver. 576 */ 577 static struct cdevsw ctl_cdevsw = { 578 .d_version = D_VERSION, 579 .d_flags = 0, 580 .d_open = ctl_open, 581 .d_close = ctl_close, 582 .d_ioctl = ctl_ioctl, 583 .d_name = "ctl", 584 }; 585 586 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 587 588 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 589 590 static moduledata_t ctl_moduledata = { 591 "ctl", 592 ctl_module_event_handler, 593 NULL 594 }; 595 596 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 597 MODULE_VERSION(ctl, 1); 598 599 static struct ctl_frontend ha_frontend = 600 { 601 .name = "ha", 602 .init = ctl_ha_init, 603 .shutdown = ctl_ha_shutdown, 604 }; 605 606 static int 607 ctl_ha_init(void) 608 { 609 struct ctl_softc *softc = control_softc; 610 611 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 612 &softc->othersc_pool) != 0) 613 return (ENOMEM); 614 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 615 ctl_pool_free(softc->othersc_pool); 616 return (EIO); 617 } 618 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 619 != CTL_HA_STATUS_SUCCESS) { 620 ctl_ha_msg_destroy(softc); 621 ctl_pool_free(softc->othersc_pool); 622 return (EIO); 623 } 624 return (0); 625 }; 626 627 static int 628 ctl_ha_shutdown(void) 629 { 630 struct ctl_softc *softc = control_softc; 631 struct ctl_port *port; 632 633 ctl_ha_msg_shutdown(softc); 634 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 635 return (EIO); 636 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 637 return (EIO); 638 ctl_pool_free(softc->othersc_pool); 639 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 640 ctl_port_deregister(port); 641 free(port->port_name, M_CTL); 642 free(port, M_CTL); 643 } 644 return (0); 645 }; 646 647 static void 648 ctl_ha_datamove(union ctl_io *io) 649 { 650 struct ctl_lun *lun = CTL_LUN(io); 651 struct ctl_sg_entry *sgl; 652 union ctl_ha_msg msg; 653 uint32_t sg_entries_sent; 654 int do_sg_copy, i, j; 655 656 memset(&msg.dt, 0, sizeof(msg.dt)); 657 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 658 msg.hdr.original_sc = io->io_hdr.remote_io; 659 msg.hdr.serializing_sc = io; 660 msg.hdr.nexus = io->io_hdr.nexus; 661 msg.hdr.status = io->io_hdr.status; 662 msg.dt.flags = io->io_hdr.flags; 663 664 /* 665 * We convert everything into a S/G list here. We can't 666 * pass by reference, only by value between controllers. 667 * So we can't pass a pointer to the S/G list, only as many 668 * S/G entries as we can fit in here. If it's possible for 669 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 670 * then we need to break this up into multiple transfers. 671 */ 672 if (io->scsiio.kern_sg_entries == 0) { 673 msg.dt.kern_sg_entries = 1; 674 #if 0 675 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 676 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 677 } else { 678 /* XXX KDM use busdma here! */ 679 msg.dt.sg_list[0].addr = 680 (void *)vtophys(io->scsiio.kern_data_ptr); 681 } 682 #else 683 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 684 ("HA does not support BUS_ADDR")); 685 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 686 #endif 687 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 688 do_sg_copy = 0; 689 } else { 690 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 691 do_sg_copy = 1; 692 } 693 694 msg.dt.kern_data_len = io->scsiio.kern_data_len; 695 msg.dt.kern_total_len = io->scsiio.kern_total_len; 696 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 697 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 698 msg.dt.sg_sequence = 0; 699 700 /* 701 * Loop until we've sent all of the S/G entries. On the 702 * other end, we'll recompose these S/G entries into one 703 * contiguous list before processing. 704 */ 705 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 706 msg.dt.sg_sequence++) { 707 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 708 sizeof(msg.dt.sg_list[0])), 709 msg.dt.kern_sg_entries - sg_entries_sent); 710 if (do_sg_copy != 0) { 711 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 712 for (i = sg_entries_sent, j = 0; 713 i < msg.dt.cur_sg_entries; i++, j++) { 714 #if 0 715 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 716 msg.dt.sg_list[j].addr = sgl[i].addr; 717 } else { 718 /* XXX KDM use busdma here! */ 719 msg.dt.sg_list[j].addr = 720 (void *)vtophys(sgl[i].addr); 721 } 722 #else 723 KASSERT((io->io_hdr.flags & 724 CTL_FLAG_BUS_ADDR) == 0, 725 ("HA does not support BUS_ADDR")); 726 msg.dt.sg_list[j].addr = sgl[i].addr; 727 #endif 728 msg.dt.sg_list[j].len = sgl[i].len; 729 } 730 } 731 732 sg_entries_sent += msg.dt.cur_sg_entries; 733 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 734 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 735 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 736 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 737 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 738 io->io_hdr.port_status = 31341; 739 ctl_datamove_done(io, true); 740 return; 741 } 742 msg.dt.sent_sg_entries = sg_entries_sent; 743 } 744 745 /* 746 * Officially handover the request from us to peer. 747 * If failover has just happened, then we must return error. 748 * If failover happen just after, then it is not our problem. 749 */ 750 if (lun) 751 mtx_lock(&lun->lun_lock); 752 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 753 if (lun) 754 mtx_unlock(&lun->lun_lock); 755 io->io_hdr.port_status = 31342; 756 ctl_datamove_done(io, true); 757 return; 758 } 759 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 760 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 761 if (lun) 762 mtx_unlock(&lun->lun_lock); 763 } 764 765 static void 766 ctl_ha_done(union ctl_io *io) 767 { 768 union ctl_ha_msg msg; 769 770 if (io->io_hdr.io_type == CTL_IO_SCSI) { 771 memset(&msg, 0, sizeof(msg)); 772 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 773 msg.hdr.original_sc = io->io_hdr.remote_io; 774 msg.hdr.nexus = io->io_hdr.nexus; 775 msg.hdr.status = io->io_hdr.status; 776 msg.scsi.scsi_status = io->scsiio.scsi_status; 777 msg.scsi.tag_num = io->scsiio.tag_num; 778 msg.scsi.tag_type = io->scsiio.tag_type; 779 msg.scsi.sense_len = io->scsiio.sense_len; 780 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 781 io->scsiio.sense_len); 782 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 783 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 784 msg.scsi.sense_len, M_WAITOK); 785 } 786 ctl_free_io(io); 787 } 788 789 static void 790 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 791 union ctl_ha_msg *msg_info) 792 { 793 struct ctl_scsiio *ctsio; 794 795 if (msg_info->hdr.original_sc == NULL) { 796 printf("%s: original_sc == NULL!\n", __func__); 797 /* XXX KDM now what? */ 798 return; 799 } 800 801 ctsio = &msg_info->hdr.original_sc->scsiio; 802 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 803 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 804 ctsio->io_hdr.status = msg_info->hdr.status; 805 ctsio->scsi_status = msg_info->scsi.scsi_status; 806 ctsio->sense_len = msg_info->scsi.sense_len; 807 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 808 msg_info->scsi.sense_len); 809 ctl_enqueue_isc((union ctl_io *)ctsio); 810 } 811 812 static void 813 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 814 union ctl_ha_msg *msg_info) 815 { 816 struct ctl_scsiio *ctsio; 817 818 if (msg_info->hdr.serializing_sc == NULL) { 819 printf("%s: serializing_sc == NULL!\n", __func__); 820 /* XXX KDM now what? */ 821 return; 822 } 823 824 ctsio = &msg_info->hdr.serializing_sc->scsiio; 825 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 826 ctl_enqueue_isc((union ctl_io *)ctsio); 827 } 828 829 void 830 ctl_isc_announce_lun(struct ctl_lun *lun) 831 { 832 struct ctl_softc *softc = lun->ctl_softc; 833 union ctl_ha_msg *msg; 834 struct ctl_ha_msg_lun_pr_key pr_key; 835 int i, k; 836 837 if (softc->ha_link != CTL_HA_LINK_ONLINE) 838 return; 839 mtx_lock(&lun->lun_lock); 840 i = sizeof(msg->lun); 841 if (lun->lun_devid) 842 i += lun->lun_devid->len; 843 i += sizeof(pr_key) * lun->pr_key_count; 844 alloc: 845 mtx_unlock(&lun->lun_lock); 846 msg = malloc(i, M_CTL, M_WAITOK); 847 mtx_lock(&lun->lun_lock); 848 k = sizeof(msg->lun); 849 if (lun->lun_devid) 850 k += lun->lun_devid->len; 851 k += sizeof(pr_key) * lun->pr_key_count; 852 if (i < k) { 853 free(msg, M_CTL); 854 i = k; 855 goto alloc; 856 } 857 bzero(&msg->lun, sizeof(msg->lun)); 858 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 859 msg->hdr.nexus.targ_lun = lun->lun; 860 msg->hdr.nexus.targ_mapped_lun = lun->lun; 861 msg->lun.flags = lun->flags; 862 msg->lun.pr_generation = lun->pr_generation; 863 msg->lun.pr_res_idx = lun->pr_res_idx; 864 msg->lun.pr_res_type = lun->pr_res_type; 865 msg->lun.pr_key_count = lun->pr_key_count; 866 i = 0; 867 if (lun->lun_devid) { 868 msg->lun.lun_devid_len = lun->lun_devid->len; 869 memcpy(&msg->lun.data[i], lun->lun_devid->data, 870 msg->lun.lun_devid_len); 871 i += msg->lun.lun_devid_len; 872 } 873 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 874 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 875 continue; 876 pr_key.pr_iid = k; 877 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 878 i += sizeof(pr_key); 879 } 880 mtx_unlock(&lun->lun_lock); 881 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 882 M_WAITOK); 883 free(msg, M_CTL); 884 885 if (lun->flags & CTL_LUN_PRIMARY_SC) { 886 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 887 ctl_isc_announce_mode(lun, -1, 888 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 889 lun->mode_pages.index[i].subpage); 890 } 891 } 892 } 893 894 void 895 ctl_isc_announce_port(struct ctl_port *port) 896 { 897 struct ctl_softc *softc = port->ctl_softc; 898 union ctl_ha_msg *msg; 899 int i; 900 901 if (port->targ_port < softc->port_min || 902 port->targ_port >= softc->port_max || 903 softc->ha_link != CTL_HA_LINK_ONLINE) 904 return; 905 i = sizeof(msg->port) + strlen(port->port_name) + 1; 906 if (port->lun_map) 907 i += port->lun_map_size * sizeof(uint32_t); 908 if (port->port_devid) 909 i += port->port_devid->len; 910 if (port->target_devid) 911 i += port->target_devid->len; 912 if (port->init_devid) 913 i += port->init_devid->len; 914 msg = malloc(i, M_CTL, M_WAITOK); 915 bzero(&msg->port, sizeof(msg->port)); 916 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 917 msg->hdr.nexus.targ_port = port->targ_port; 918 msg->port.port_type = port->port_type; 919 msg->port.physical_port = port->physical_port; 920 msg->port.virtual_port = port->virtual_port; 921 msg->port.status = port->status; 922 i = 0; 923 msg->port.name_len = sprintf(&msg->port.data[i], 924 "%d:%s", softc->ha_id, port->port_name) + 1; 925 i += msg->port.name_len; 926 if (port->lun_map) { 927 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 928 memcpy(&msg->port.data[i], port->lun_map, 929 msg->port.lun_map_len); 930 i += msg->port.lun_map_len; 931 } 932 if (port->port_devid) { 933 msg->port.port_devid_len = port->port_devid->len; 934 memcpy(&msg->port.data[i], port->port_devid->data, 935 msg->port.port_devid_len); 936 i += msg->port.port_devid_len; 937 } 938 if (port->target_devid) { 939 msg->port.target_devid_len = port->target_devid->len; 940 memcpy(&msg->port.data[i], port->target_devid->data, 941 msg->port.target_devid_len); 942 i += msg->port.target_devid_len; 943 } 944 if (port->init_devid) { 945 msg->port.init_devid_len = port->init_devid->len; 946 memcpy(&msg->port.data[i], port->init_devid->data, 947 msg->port.init_devid_len); 948 i += msg->port.init_devid_len; 949 } 950 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 951 M_WAITOK); 952 free(msg, M_CTL); 953 } 954 955 void 956 ctl_isc_announce_iid(struct ctl_port *port, int iid) 957 { 958 struct ctl_softc *softc = port->ctl_softc; 959 union ctl_ha_msg *msg; 960 int i, l; 961 962 if (port->targ_port < softc->port_min || 963 port->targ_port >= softc->port_max || 964 softc->ha_link != CTL_HA_LINK_ONLINE) 965 return; 966 mtx_lock(&softc->ctl_lock); 967 i = sizeof(msg->iid); 968 l = 0; 969 if (port->wwpn_iid[iid].name) 970 l = strlen(port->wwpn_iid[iid].name) + 1; 971 i += l; 972 msg = malloc(i, M_CTL, M_NOWAIT); 973 if (msg == NULL) { 974 mtx_unlock(&softc->ctl_lock); 975 return; 976 } 977 bzero(&msg->iid, sizeof(msg->iid)); 978 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 979 msg->hdr.nexus.targ_port = port->targ_port; 980 msg->hdr.nexus.initid = iid; 981 msg->iid.in_use = port->wwpn_iid[iid].in_use; 982 msg->iid.name_len = l; 983 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 984 if (port->wwpn_iid[iid].name) 985 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 986 mtx_unlock(&softc->ctl_lock); 987 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 988 free(msg, M_CTL); 989 } 990 991 void 992 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 993 uint8_t page, uint8_t subpage) 994 { 995 struct ctl_softc *softc = lun->ctl_softc; 996 union ctl_ha_msg msg; 997 u_int i; 998 999 if (softc->ha_link != CTL_HA_LINK_ONLINE) 1000 return; 1001 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1002 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1003 page && lun->mode_pages.index[i].subpage == subpage) 1004 break; 1005 } 1006 if (i == CTL_NUM_MODE_PAGES) 1007 return; 1008 1009 /* Don't try to replicate pages not present on this device. */ 1010 if (lun->mode_pages.index[i].page_data == NULL) 1011 return; 1012 1013 bzero(&msg.mode, sizeof(msg.mode)); 1014 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 1015 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 1016 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 1017 msg.hdr.nexus.targ_lun = lun->lun; 1018 msg.hdr.nexus.targ_mapped_lun = lun->lun; 1019 msg.mode.page_code = page; 1020 msg.mode.subpage = subpage; 1021 msg.mode.page_len = lun->mode_pages.index[i].page_len; 1022 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 1023 msg.mode.page_len); 1024 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 1025 M_WAITOK); 1026 } 1027 1028 static void 1029 ctl_isc_ha_link_up(struct ctl_softc *softc) 1030 { 1031 struct ctl_port *port; 1032 struct ctl_lun *lun; 1033 union ctl_ha_msg msg; 1034 int i; 1035 1036 /* Announce this node parameters to peer for validation. */ 1037 msg.login.msg_type = CTL_MSG_LOGIN; 1038 msg.login.version = CTL_HA_VERSION; 1039 msg.login.ha_mode = softc->ha_mode; 1040 msg.login.ha_id = softc->ha_id; 1041 msg.login.max_luns = ctl_max_luns; 1042 msg.login.max_ports = ctl_max_ports; 1043 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1044 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1045 M_WAITOK); 1046 1047 STAILQ_FOREACH(port, &softc->port_list, links) { 1048 ctl_isc_announce_port(port); 1049 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1050 if (port->wwpn_iid[i].in_use) 1051 ctl_isc_announce_iid(port, i); 1052 } 1053 } 1054 STAILQ_FOREACH(lun, &softc->lun_list, links) 1055 ctl_isc_announce_lun(lun); 1056 } 1057 1058 static void 1059 ctl_isc_ha_link_down(struct ctl_softc *softc) 1060 { 1061 struct ctl_port *port; 1062 struct ctl_lun *lun; 1063 union ctl_io *io; 1064 int i; 1065 1066 mtx_lock(&softc->ctl_lock); 1067 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1068 mtx_lock(&lun->lun_lock); 1069 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1070 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1071 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1072 } 1073 mtx_unlock(&lun->lun_lock); 1074 1075 mtx_unlock(&softc->ctl_lock); 1076 io = ctl_alloc_io(softc->othersc_pool); 1077 mtx_lock(&softc->ctl_lock); 1078 ctl_zero_io(io); 1079 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1080 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1081 ctl_enqueue_isc(io); 1082 } 1083 1084 STAILQ_FOREACH(port, &softc->port_list, links) { 1085 if (port->targ_port >= softc->port_min && 1086 port->targ_port < softc->port_max) 1087 continue; 1088 port->status &= ~CTL_PORT_STATUS_ONLINE; 1089 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1090 port->wwpn_iid[i].in_use = 0; 1091 free(port->wwpn_iid[i].name, M_CTL); 1092 port->wwpn_iid[i].name = NULL; 1093 } 1094 } 1095 mtx_unlock(&softc->ctl_lock); 1096 } 1097 1098 static void 1099 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1100 { 1101 struct ctl_lun *lun; 1102 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1103 1104 mtx_lock(&softc->ctl_lock); 1105 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1106 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1107 mtx_unlock(&softc->ctl_lock); 1108 return; 1109 } 1110 mtx_lock(&lun->lun_lock); 1111 mtx_unlock(&softc->ctl_lock); 1112 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1113 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1114 if (msg->ua.ua_all) { 1115 if (msg->ua.ua_set) 1116 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1117 else 1118 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1119 } else { 1120 if (msg->ua.ua_set) 1121 ctl_est_ua(lun, iid, msg->ua.ua_type); 1122 else 1123 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1124 } 1125 mtx_unlock(&lun->lun_lock); 1126 } 1127 1128 static void 1129 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1130 { 1131 struct ctl_lun *lun; 1132 struct ctl_ha_msg_lun_pr_key pr_key; 1133 int i, k; 1134 ctl_lun_flags oflags; 1135 uint32_t targ_lun; 1136 1137 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1138 mtx_lock(&softc->ctl_lock); 1139 if (targ_lun >= ctl_max_luns || 1140 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1141 mtx_unlock(&softc->ctl_lock); 1142 return; 1143 } 1144 mtx_lock(&lun->lun_lock); 1145 mtx_unlock(&softc->ctl_lock); 1146 if (lun->flags & CTL_LUN_DISABLED) { 1147 mtx_unlock(&lun->lun_lock); 1148 return; 1149 } 1150 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1151 if (msg->lun.lun_devid_len != i || (i > 0 && 1152 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1153 mtx_unlock(&lun->lun_lock); 1154 printf("%s: Received conflicting HA LUN %d\n", 1155 __func__, targ_lun); 1156 return; 1157 } else { 1158 /* Record whether peer is primary. */ 1159 oflags = lun->flags; 1160 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1161 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1162 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1163 else 1164 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1165 if (oflags != lun->flags) 1166 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1167 1168 /* If peer is primary and we are not -- use data */ 1169 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1170 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1171 lun->pr_generation = msg->lun.pr_generation; 1172 lun->pr_res_idx = msg->lun.pr_res_idx; 1173 lun->pr_res_type = msg->lun.pr_res_type; 1174 lun->pr_key_count = msg->lun.pr_key_count; 1175 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1176 ctl_clr_prkey(lun, k); 1177 for (k = 0; k < msg->lun.pr_key_count; k++) { 1178 memcpy(&pr_key, &msg->lun.data[i], 1179 sizeof(pr_key)); 1180 ctl_alloc_prkey(lun, pr_key.pr_iid); 1181 ctl_set_prkey(lun, pr_key.pr_iid, 1182 pr_key.pr_key); 1183 i += sizeof(pr_key); 1184 } 1185 } 1186 1187 mtx_unlock(&lun->lun_lock); 1188 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1189 __func__, targ_lun, 1190 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1191 "primary" : "secondary")); 1192 1193 /* If we are primary but peer doesn't know -- notify */ 1194 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1195 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1196 ctl_isc_announce_lun(lun); 1197 } 1198 } 1199 1200 static void 1201 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1202 { 1203 struct ctl_port *port; 1204 struct ctl_lun *lun; 1205 int i, new; 1206 1207 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1208 if (port == NULL) { 1209 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1210 msg->hdr.nexus.targ_port)); 1211 new = 1; 1212 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1213 port->frontend = &ha_frontend; 1214 port->targ_port = msg->hdr.nexus.targ_port; 1215 port->fe_datamove = ctl_ha_datamove; 1216 port->fe_done = ctl_ha_done; 1217 } else if (port->frontend == &ha_frontend) { 1218 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1219 msg->hdr.nexus.targ_port)); 1220 new = 0; 1221 } else { 1222 printf("%s: Received conflicting HA port %d\n", 1223 __func__, msg->hdr.nexus.targ_port); 1224 return; 1225 } 1226 port->port_type = msg->port.port_type; 1227 port->physical_port = msg->port.physical_port; 1228 port->virtual_port = msg->port.virtual_port; 1229 port->status = msg->port.status; 1230 i = 0; 1231 free(port->port_name, M_CTL); 1232 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1233 M_CTL); 1234 i += msg->port.name_len; 1235 if (msg->port.lun_map_len != 0) { 1236 if (port->lun_map == NULL || 1237 port->lun_map_size * sizeof(uint32_t) < 1238 msg->port.lun_map_len) { 1239 port->lun_map_size = 0; 1240 free(port->lun_map, M_CTL); 1241 port->lun_map = malloc(msg->port.lun_map_len, 1242 M_CTL, M_WAITOK); 1243 } 1244 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1245 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1246 i += msg->port.lun_map_len; 1247 } else { 1248 port->lun_map_size = 0; 1249 free(port->lun_map, M_CTL); 1250 port->lun_map = NULL; 1251 } 1252 if (msg->port.port_devid_len != 0) { 1253 if (port->port_devid == NULL || 1254 port->port_devid->len < msg->port.port_devid_len) { 1255 free(port->port_devid, M_CTL); 1256 port->port_devid = malloc(sizeof(struct ctl_devid) + 1257 msg->port.port_devid_len, M_CTL, M_WAITOK); 1258 } 1259 memcpy(port->port_devid->data, &msg->port.data[i], 1260 msg->port.port_devid_len); 1261 port->port_devid->len = msg->port.port_devid_len; 1262 i += msg->port.port_devid_len; 1263 } else { 1264 free(port->port_devid, M_CTL); 1265 port->port_devid = NULL; 1266 } 1267 if (msg->port.target_devid_len != 0) { 1268 if (port->target_devid == NULL || 1269 port->target_devid->len < msg->port.target_devid_len) { 1270 free(port->target_devid, M_CTL); 1271 port->target_devid = malloc(sizeof(struct ctl_devid) + 1272 msg->port.target_devid_len, M_CTL, M_WAITOK); 1273 } 1274 memcpy(port->target_devid->data, &msg->port.data[i], 1275 msg->port.target_devid_len); 1276 port->target_devid->len = msg->port.target_devid_len; 1277 i += msg->port.target_devid_len; 1278 } else { 1279 free(port->target_devid, M_CTL); 1280 port->target_devid = NULL; 1281 } 1282 if (msg->port.init_devid_len != 0) { 1283 if (port->init_devid == NULL || 1284 port->init_devid->len < msg->port.init_devid_len) { 1285 free(port->init_devid, M_CTL); 1286 port->init_devid = malloc(sizeof(struct ctl_devid) + 1287 msg->port.init_devid_len, M_CTL, M_WAITOK); 1288 } 1289 memcpy(port->init_devid->data, &msg->port.data[i], 1290 msg->port.init_devid_len); 1291 port->init_devid->len = msg->port.init_devid_len; 1292 i += msg->port.init_devid_len; 1293 } else { 1294 free(port->init_devid, M_CTL); 1295 port->init_devid = NULL; 1296 } 1297 if (new) { 1298 if (ctl_port_register(port) != 0) { 1299 printf("%s: ctl_port_register() failed with error\n", 1300 __func__); 1301 } 1302 } 1303 mtx_lock(&softc->ctl_lock); 1304 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1305 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1306 continue; 1307 mtx_lock(&lun->lun_lock); 1308 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1309 mtx_unlock(&lun->lun_lock); 1310 } 1311 mtx_unlock(&softc->ctl_lock); 1312 } 1313 1314 static void 1315 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1316 { 1317 struct ctl_port *port; 1318 int iid; 1319 1320 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1321 if (port == NULL) { 1322 printf("%s: Received IID for unknown port %d\n", 1323 __func__, msg->hdr.nexus.targ_port); 1324 return; 1325 } 1326 iid = msg->hdr.nexus.initid; 1327 if (port->wwpn_iid[iid].in_use != 0 && 1328 msg->iid.in_use == 0) 1329 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1330 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1331 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1332 free(port->wwpn_iid[iid].name, M_CTL); 1333 if (msg->iid.name_len) { 1334 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1335 msg->iid.name_len, M_CTL); 1336 } else 1337 port->wwpn_iid[iid].name = NULL; 1338 } 1339 1340 static void 1341 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1342 { 1343 1344 if (msg->login.version != CTL_HA_VERSION) { 1345 printf("CTL HA peers have different versions %d != %d\n", 1346 msg->login.version, CTL_HA_VERSION); 1347 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1348 return; 1349 } 1350 if (msg->login.ha_mode != softc->ha_mode) { 1351 printf("CTL HA peers have different ha_mode %d != %d\n", 1352 msg->login.ha_mode, softc->ha_mode); 1353 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1354 return; 1355 } 1356 if (msg->login.ha_id == softc->ha_id) { 1357 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1358 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1359 return; 1360 } 1361 if (msg->login.max_luns != ctl_max_luns || 1362 msg->login.max_ports != ctl_max_ports || 1363 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1364 printf("CTL HA peers have different limits\n"); 1365 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1366 return; 1367 } 1368 } 1369 1370 static void 1371 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1372 { 1373 struct ctl_lun *lun; 1374 u_int i; 1375 uint32_t initidx, targ_lun; 1376 1377 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1378 mtx_lock(&softc->ctl_lock); 1379 if (targ_lun >= ctl_max_luns || 1380 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1381 mtx_unlock(&softc->ctl_lock); 1382 return; 1383 } 1384 mtx_lock(&lun->lun_lock); 1385 mtx_unlock(&softc->ctl_lock); 1386 if (lun->flags & CTL_LUN_DISABLED) { 1387 mtx_unlock(&lun->lun_lock); 1388 return; 1389 } 1390 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1391 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1392 msg->mode.page_code && 1393 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1394 break; 1395 } 1396 if (i == CTL_NUM_MODE_PAGES) { 1397 mtx_unlock(&lun->lun_lock); 1398 return; 1399 } 1400 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1401 lun->mode_pages.index[i].page_len); 1402 initidx = ctl_get_initindex(&msg->hdr.nexus); 1403 if (initidx != -1) 1404 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1405 mtx_unlock(&lun->lun_lock); 1406 } 1407 1408 /* 1409 * ISC (Inter Shelf Communication) event handler. Events from the HA 1410 * subsystem come in here. 1411 */ 1412 static void 1413 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1414 { 1415 struct ctl_softc *softc = control_softc; 1416 union ctl_io *io; 1417 struct ctl_prio *presio; 1418 ctl_ha_status isc_status; 1419 1420 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1421 if (event == CTL_HA_EVT_MSG_RECV) { 1422 union ctl_ha_msg *msg, msgbuf; 1423 1424 if (param > sizeof(msgbuf)) 1425 msg = malloc(param, M_CTL, M_WAITOK); 1426 else 1427 msg = &msgbuf; 1428 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1429 M_WAITOK); 1430 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1431 printf("%s: Error receiving message: %d\n", 1432 __func__, isc_status); 1433 if (msg != &msgbuf) 1434 free(msg, M_CTL); 1435 return; 1436 } 1437 1438 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->hdr.msg_type)); 1439 switch (msg->hdr.msg_type) { 1440 case CTL_MSG_SERIALIZE: 1441 io = ctl_alloc_io(softc->othersc_pool); 1442 ctl_zero_io(io); 1443 // populate ctsio from msg 1444 io->io_hdr.io_type = CTL_IO_SCSI; 1445 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1446 io->io_hdr.remote_io = msg->hdr.original_sc; 1447 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1448 CTL_FLAG_IO_ACTIVE; 1449 /* 1450 * If we're in serialization-only mode, we don't 1451 * want to go through full done processing. Thus 1452 * the COPY flag. 1453 * 1454 * XXX KDM add another flag that is more specific. 1455 */ 1456 if (softc->ha_mode != CTL_HA_MODE_XFER) 1457 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1458 io->io_hdr.nexus = msg->hdr.nexus; 1459 io->scsiio.priority = msg->scsi.priority; 1460 io->scsiio.tag_num = msg->scsi.tag_num; 1461 io->scsiio.tag_type = msg->scsi.tag_type; 1462 #ifdef CTL_TIME_IO 1463 io->io_hdr.start_time = time_uptime; 1464 getbinuptime(&io->io_hdr.start_bt); 1465 #endif /* CTL_TIME_IO */ 1466 io->scsiio.cdb_len = msg->scsi.cdb_len; 1467 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1468 CTL_MAX_CDBLEN); 1469 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1470 const struct ctl_cmd_entry *entry; 1471 1472 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1473 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1474 io->io_hdr.flags |= 1475 entry->flags & CTL_FLAG_DATA_MASK; 1476 } 1477 ctl_enqueue_isc(io); 1478 break; 1479 1480 /* Performed on the Originating SC, XFER mode only */ 1481 case CTL_MSG_DATAMOVE: { 1482 struct ctl_sg_entry *sgl; 1483 int i, j; 1484 1485 io = msg->hdr.original_sc; 1486 if (io == NULL) { 1487 printf("%s: original_sc == NULL!\n", __func__); 1488 /* XXX KDM do something here */ 1489 break; 1490 } 1491 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1492 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1493 /* 1494 * Keep track of this, we need to send it back over 1495 * when the datamove is complete. 1496 */ 1497 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1498 if (msg->hdr.status == CTL_SUCCESS) 1499 io->io_hdr.status = msg->hdr.status; 1500 1501 if (msg->dt.sg_sequence == 0) { 1502 #ifdef CTL_TIME_IO 1503 getbinuptime(&io->io_hdr.dma_start_bt); 1504 #endif 1505 i = msg->dt.kern_sg_entries + 1506 msg->dt.kern_data_len / 1507 CTL_HA_DATAMOVE_SEGMENT + 1; 1508 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1509 M_WAITOK | M_ZERO); 1510 CTL_RSGL(io) = sgl; 1511 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries]; 1512 1513 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1514 1515 io->scsiio.kern_sg_entries = 1516 msg->dt.kern_sg_entries; 1517 io->scsiio.rem_sg_entries = 1518 msg->dt.kern_sg_entries; 1519 io->scsiio.kern_data_len = 1520 msg->dt.kern_data_len; 1521 io->scsiio.kern_total_len = 1522 msg->dt.kern_total_len; 1523 io->scsiio.kern_data_resid = 1524 msg->dt.kern_data_resid; 1525 io->scsiio.kern_rel_offset = 1526 msg->dt.kern_rel_offset; 1527 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1528 io->io_hdr.flags |= msg->dt.flags & 1529 CTL_FLAG_BUS_ADDR; 1530 } else 1531 sgl = (struct ctl_sg_entry *) 1532 io->scsiio.kern_data_ptr; 1533 1534 for (i = msg->dt.sent_sg_entries, j = 0; 1535 i < (msg->dt.sent_sg_entries + 1536 msg->dt.cur_sg_entries); i++, j++) { 1537 sgl[i].addr = msg->dt.sg_list[j].addr; 1538 sgl[i].len = msg->dt.sg_list[j].len; 1539 } 1540 1541 /* 1542 * If this is the last piece of the I/O, we've got 1543 * the full S/G list. Queue processing in the thread. 1544 * Otherwise wait for the next piece. 1545 */ 1546 if (msg->dt.sg_last != 0) 1547 ctl_enqueue_isc(io); 1548 break; 1549 } 1550 /* Performed on the Serializing (primary) SC, XFER mode only */ 1551 case CTL_MSG_DATAMOVE_DONE: { 1552 if (msg->hdr.serializing_sc == NULL) { 1553 printf("%s: serializing_sc == NULL!\n", 1554 __func__); 1555 /* XXX KDM now what? */ 1556 break; 1557 } 1558 /* 1559 * We grab the sense information here in case 1560 * there was a failure, so we can return status 1561 * back to the initiator. 1562 */ 1563 io = msg->hdr.serializing_sc; 1564 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1565 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1566 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1567 io->io_hdr.port_status = msg->scsi.port_status; 1568 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1569 if (msg->hdr.status != CTL_STATUS_NONE) { 1570 io->io_hdr.status = msg->hdr.status; 1571 io->scsiio.scsi_status = msg->scsi.scsi_status; 1572 io->scsiio.sense_len = msg->scsi.sense_len; 1573 memcpy(&io->scsiio.sense_data, 1574 &msg->scsi.sense_data, 1575 msg->scsi.sense_len); 1576 if (msg->hdr.status == CTL_SUCCESS) 1577 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1578 } 1579 ctl_enqueue_isc(io); 1580 break; 1581 } 1582 1583 /* Preformed on Originating SC, SER_ONLY mode */ 1584 case CTL_MSG_R2R: 1585 io = msg->hdr.original_sc; 1586 if (io == NULL) { 1587 printf("%s: original_sc == NULL!\n", 1588 __func__); 1589 break; 1590 } 1591 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1592 io->io_hdr.msg_type = CTL_MSG_R2R; 1593 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1594 ctl_enqueue_isc(io); 1595 break; 1596 1597 /* 1598 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1599 * mode. 1600 * Performed on the Originating (i.e. secondary) SC in XFER 1601 * mode 1602 */ 1603 case CTL_MSG_FINISH_IO: 1604 if (softc->ha_mode == CTL_HA_MODE_XFER) 1605 ctl_isc_handler_finish_xfer(softc, msg); 1606 else 1607 ctl_isc_handler_finish_ser_only(softc, msg); 1608 break; 1609 1610 /* Preformed on Originating SC */ 1611 case CTL_MSG_BAD_JUJU: 1612 io = msg->hdr.original_sc; 1613 if (io == NULL) { 1614 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1615 __func__); 1616 break; 1617 } 1618 ctl_copy_sense_data(msg, io); 1619 /* 1620 * IO should have already been cleaned up on other 1621 * SC so clear this flag so we won't send a message 1622 * back to finish the IO there. 1623 */ 1624 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1625 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1626 1627 /* io = msg->hdr.serializing_sc; */ 1628 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1629 ctl_enqueue_isc(io); 1630 break; 1631 1632 /* Handle resets sent from the other side */ 1633 case CTL_MSG_MANAGE_TASKS: { 1634 struct ctl_taskio *taskio; 1635 taskio = (struct ctl_taskio *)ctl_alloc_io( 1636 softc->othersc_pool); 1637 ctl_zero_io((union ctl_io *)taskio); 1638 taskio->io_hdr.io_type = CTL_IO_TASK; 1639 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1640 taskio->io_hdr.nexus = msg->hdr.nexus; 1641 taskio->task_action = msg->task.task_action; 1642 taskio->tag_num = msg->task.tag_num; 1643 taskio->tag_type = msg->task.tag_type; 1644 #ifdef CTL_TIME_IO 1645 taskio->io_hdr.start_time = time_uptime; 1646 getbinuptime(&taskio->io_hdr.start_bt); 1647 #endif /* CTL_TIME_IO */ 1648 ctl_run_task((union ctl_io *)taskio); 1649 break; 1650 } 1651 /* Persistent Reserve action which needs attention */ 1652 case CTL_MSG_PERS_ACTION: 1653 presio = (struct ctl_prio *)ctl_alloc_io( 1654 softc->othersc_pool); 1655 ctl_zero_io((union ctl_io *)presio); 1656 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1657 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1658 presio->io_hdr.nexus = msg->hdr.nexus; 1659 presio->pr_msg = msg->pr; 1660 ctl_enqueue_isc((union ctl_io *)presio); 1661 break; 1662 case CTL_MSG_UA: 1663 ctl_isc_ua(softc, msg, param); 1664 break; 1665 case CTL_MSG_PORT_SYNC: 1666 ctl_isc_port_sync(softc, msg, param); 1667 break; 1668 case CTL_MSG_LUN_SYNC: 1669 ctl_isc_lun_sync(softc, msg, param); 1670 break; 1671 case CTL_MSG_IID_SYNC: 1672 ctl_isc_iid_sync(softc, msg, param); 1673 break; 1674 case CTL_MSG_LOGIN: 1675 ctl_isc_login(softc, msg, param); 1676 break; 1677 case CTL_MSG_MODE_SYNC: 1678 ctl_isc_mode_sync(softc, msg, param); 1679 break; 1680 default: 1681 printf("Received HA message of unknown type %d\n", 1682 msg->hdr.msg_type); 1683 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1684 break; 1685 } 1686 if (msg != &msgbuf) 1687 free(msg, M_CTL); 1688 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1689 printf("CTL: HA link status changed from %d to %d\n", 1690 softc->ha_link, param); 1691 if (param == softc->ha_link) 1692 return; 1693 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1694 softc->ha_link = param; 1695 ctl_isc_ha_link_down(softc); 1696 } else { 1697 softc->ha_link = param; 1698 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1699 ctl_isc_ha_link_up(softc); 1700 } 1701 return; 1702 } else { 1703 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1704 return; 1705 } 1706 } 1707 1708 static void 1709 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1710 { 1711 1712 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1713 src->scsi.sense_len); 1714 dest->scsiio.scsi_status = src->scsi.scsi_status; 1715 dest->scsiio.sense_len = src->scsi.sense_len; 1716 dest->io_hdr.status = src->hdr.status; 1717 } 1718 1719 static void 1720 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1721 { 1722 1723 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1724 src->scsiio.sense_len); 1725 dest->scsi.scsi_status = src->scsiio.scsi_status; 1726 dest->scsi.sense_len = src->scsiio.sense_len; 1727 dest->hdr.status = src->io_hdr.status; 1728 } 1729 1730 void 1731 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1732 { 1733 struct ctl_softc *softc = lun->ctl_softc; 1734 ctl_ua_type *pu; 1735 1736 if (initidx < softc->init_min || initidx >= softc->init_max) 1737 return; 1738 mtx_assert(&lun->lun_lock, MA_OWNED); 1739 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1740 if (pu == NULL) 1741 return; 1742 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1743 } 1744 1745 void 1746 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1747 { 1748 int i; 1749 1750 mtx_assert(&lun->lun_lock, MA_OWNED); 1751 if (lun->pending_ua[port] == NULL) 1752 return; 1753 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1754 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1755 continue; 1756 lun->pending_ua[port][i] |= ua; 1757 } 1758 } 1759 1760 void 1761 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1762 { 1763 struct ctl_softc *softc = lun->ctl_softc; 1764 int i; 1765 1766 mtx_assert(&lun->lun_lock, MA_OWNED); 1767 for (i = softc->port_min; i < softc->port_max; i++) 1768 ctl_est_ua_port(lun, i, except, ua); 1769 } 1770 1771 void 1772 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1773 { 1774 struct ctl_softc *softc = lun->ctl_softc; 1775 ctl_ua_type *pu; 1776 1777 if (initidx < softc->init_min || initidx >= softc->init_max) 1778 return; 1779 mtx_assert(&lun->lun_lock, MA_OWNED); 1780 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1781 if (pu == NULL) 1782 return; 1783 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1784 } 1785 1786 void 1787 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1788 { 1789 struct ctl_softc *softc = lun->ctl_softc; 1790 int i, j; 1791 1792 mtx_assert(&lun->lun_lock, MA_OWNED); 1793 for (i = softc->port_min; i < softc->port_max; i++) { 1794 if (lun->pending_ua[i] == NULL) 1795 continue; 1796 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1797 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1798 continue; 1799 lun->pending_ua[i][j] &= ~ua; 1800 } 1801 } 1802 } 1803 1804 void 1805 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1806 ctl_ua_type ua_type) 1807 { 1808 struct ctl_lun *lun; 1809 1810 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1811 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1812 mtx_lock(&lun->lun_lock); 1813 ctl_clr_ua(lun, initidx, ua_type); 1814 mtx_unlock(&lun->lun_lock); 1815 } 1816 } 1817 1818 static int 1819 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1820 { 1821 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1822 struct ctl_lun *lun; 1823 struct ctl_lun_req ireq; 1824 int error, value; 1825 1826 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1827 error = sysctl_handle_int(oidp, &value, 0, req); 1828 if ((error != 0) || (req->newptr == NULL)) 1829 return (error); 1830 1831 mtx_lock(&softc->ctl_lock); 1832 if (value == 0) 1833 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1834 else 1835 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1836 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1837 mtx_unlock(&softc->ctl_lock); 1838 bzero(&ireq, sizeof(ireq)); 1839 ireq.reqtype = CTL_LUNREQ_MODIFY; 1840 ireq.reqdata.modify.lun_id = lun->lun; 1841 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1842 curthread); 1843 if (ireq.status != CTL_LUN_OK) { 1844 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1845 __func__, ireq.status, ireq.error_str); 1846 } 1847 mtx_lock(&softc->ctl_lock); 1848 } 1849 mtx_unlock(&softc->ctl_lock); 1850 return (0); 1851 } 1852 1853 static int 1854 ctl_init(void) 1855 { 1856 struct make_dev_args args; 1857 struct ctl_softc *softc; 1858 int i, error; 1859 1860 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1861 M_WAITOK | M_ZERO); 1862 1863 make_dev_args_init(&args); 1864 args.mda_devsw = &ctl_cdevsw; 1865 args.mda_uid = UID_ROOT; 1866 args.mda_gid = GID_OPERATOR; 1867 args.mda_mode = 0600; 1868 args.mda_si_drv1 = softc; 1869 args.mda_si_drv2 = NULL; 1870 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1871 if (error != 0) { 1872 free(softc, M_DEVBUF); 1873 control_softc = NULL; 1874 return (error); 1875 } 1876 1877 sysctl_ctx_init(&softc->sysctl_ctx); 1878 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1879 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1880 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer"); 1881 1882 if (softc->sysctl_tree == NULL) { 1883 printf("%s: unable to allocate sysctl tree\n", __func__); 1884 destroy_dev(softc->dev); 1885 free(softc, M_DEVBUF); 1886 control_softc = NULL; 1887 return (ENOMEM); 1888 } 1889 1890 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1891 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1892 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1893 softc->flags = 0; 1894 1895 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1896 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1897 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1898 1899 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) { 1900 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n", 1901 ctl_max_luns, CTL_DEFAULT_MAX_LUNS); 1902 ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 1903 } 1904 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns, 1905 M_DEVBUF, M_WAITOK | M_ZERO); 1906 softc->ctl_lun_mask = malloc(sizeof(uint32_t) * 1907 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1908 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) { 1909 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n", 1910 ctl_max_ports, CTL_DEFAULT_MAX_PORTS); 1911 ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 1912 } 1913 softc->ctl_port_mask = malloc(sizeof(uint32_t) * 1914 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1915 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports, 1916 M_DEVBUF, M_WAITOK | M_ZERO); 1917 1918 /* 1919 * In Copan's HA scheme, the "master" and "slave" roles are 1920 * figured out through the slot the controller is in. Although it 1921 * is an active/active system, someone has to be in charge. 1922 */ 1923 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1924 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1925 "HA head ID (0 - no HA)"); 1926 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1927 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1928 softc->is_single = 1; 1929 softc->port_cnt = ctl_max_ports; 1930 softc->port_min = 0; 1931 } else { 1932 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 1933 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1934 } 1935 softc->port_max = softc->port_min + softc->port_cnt; 1936 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1937 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1938 1939 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1940 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1941 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1942 1943 STAILQ_INIT(&softc->lun_list); 1944 STAILQ_INIT(&softc->fe_list); 1945 STAILQ_INIT(&softc->port_list); 1946 STAILQ_INIT(&softc->be_list); 1947 ctl_tpc_init(softc); 1948 1949 if (worker_threads <= 0) 1950 worker_threads = max(1, mp_ncpus / 4); 1951 if (worker_threads > CTL_MAX_THREADS) 1952 worker_threads = CTL_MAX_THREADS; 1953 1954 for (i = 0; i < worker_threads; i++) { 1955 struct ctl_thread *thr = &softc->threads[i]; 1956 1957 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1958 thr->ctl_softc = softc; 1959 STAILQ_INIT(&thr->incoming_queue); 1960 STAILQ_INIT(&thr->rtr_queue); 1961 STAILQ_INIT(&thr->done_queue); 1962 STAILQ_INIT(&thr->isc_queue); 1963 1964 error = kproc_kthread_add(ctl_work_thread, thr, 1965 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1966 if (error != 0) { 1967 printf("error creating CTL work thread!\n"); 1968 return (error); 1969 } 1970 } 1971 error = kproc_kthread_add(ctl_thresh_thread, softc, 1972 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 1973 if (error != 0) { 1974 printf("error creating CTL threshold thread!\n"); 1975 return (error); 1976 } 1977 1978 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1979 OID_AUTO, "ha_role", 1980 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 1981 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1982 1983 if (softc->is_single == 0) { 1984 if (ctl_frontend_register(&ha_frontend) != 0) 1985 softc->is_single = 1; 1986 } 1987 return (0); 1988 } 1989 1990 static int 1991 ctl_shutdown(void) 1992 { 1993 struct ctl_softc *softc = control_softc; 1994 int i; 1995 1996 if (softc->is_single == 0) 1997 ctl_frontend_deregister(&ha_frontend); 1998 1999 destroy_dev(softc->dev); 2000 2001 /* Shutdown CTL threads. */ 2002 softc->shutdown = 1; 2003 for (i = 0; i < worker_threads; i++) { 2004 struct ctl_thread *thr = &softc->threads[i]; 2005 while (thr->thread != NULL) { 2006 wakeup(thr); 2007 if (thr->thread != NULL) 2008 pause("CTL thr shutdown", 1); 2009 } 2010 mtx_destroy(&thr->queue_lock); 2011 } 2012 while (softc->thresh_thread != NULL) { 2013 wakeup(softc->thresh_thread); 2014 if (softc->thresh_thread != NULL) 2015 pause("CTL thr shutdown", 1); 2016 } 2017 2018 ctl_tpc_shutdown(softc); 2019 uma_zdestroy(softc->io_zone); 2020 mtx_destroy(&softc->ctl_lock); 2021 2022 free(softc->ctl_luns, M_DEVBUF); 2023 free(softc->ctl_lun_mask, M_DEVBUF); 2024 free(softc->ctl_port_mask, M_DEVBUF); 2025 free(softc->ctl_ports, M_DEVBUF); 2026 2027 sysctl_ctx_free(&softc->sysctl_ctx); 2028 2029 free(softc, M_DEVBUF); 2030 control_softc = NULL; 2031 return (0); 2032 } 2033 2034 static int 2035 ctl_module_event_handler(module_t mod, int what, void *arg) 2036 { 2037 2038 switch (what) { 2039 case MOD_LOAD: 2040 return (ctl_init()); 2041 case MOD_UNLOAD: 2042 return (ctl_shutdown()); 2043 default: 2044 return (EOPNOTSUPP); 2045 } 2046 } 2047 2048 /* 2049 * XXX KDM should we do some access checks here? Bump a reference count to 2050 * prevent a CTL module from being unloaded while someone has it open? 2051 */ 2052 static int 2053 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2054 { 2055 return (0); 2056 } 2057 2058 static int 2059 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2060 { 2061 return (0); 2062 } 2063 2064 /* 2065 * Remove an initiator by port number and initiator ID. 2066 * Returns 0 for success, -1 for failure. 2067 */ 2068 int 2069 ctl_remove_initiator(struct ctl_port *port, int iid) 2070 { 2071 struct ctl_softc *softc = port->ctl_softc; 2072 int last; 2073 2074 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2075 2076 if (iid > CTL_MAX_INIT_PER_PORT) { 2077 printf("%s: initiator ID %u > maximun %u!\n", 2078 __func__, iid, CTL_MAX_INIT_PER_PORT); 2079 return (-1); 2080 } 2081 2082 mtx_lock(&softc->ctl_lock); 2083 last = (--port->wwpn_iid[iid].in_use == 0); 2084 port->wwpn_iid[iid].last_use = time_uptime; 2085 mtx_unlock(&softc->ctl_lock); 2086 if (last) 2087 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2088 ctl_isc_announce_iid(port, iid); 2089 2090 return (0); 2091 } 2092 2093 /* 2094 * Add an initiator to the initiator map. 2095 * Returns iid for success, < 0 for failure. 2096 */ 2097 int 2098 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2099 { 2100 struct ctl_softc *softc = port->ctl_softc; 2101 time_t best_time; 2102 int i, best; 2103 2104 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2105 2106 if (iid >= CTL_MAX_INIT_PER_PORT) { 2107 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2108 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2109 free(name, M_CTL); 2110 return (-1); 2111 } 2112 2113 mtx_lock(&softc->ctl_lock); 2114 2115 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2116 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2117 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2118 iid = i; 2119 break; 2120 } 2121 if (name != NULL && port->wwpn_iid[i].name != NULL && 2122 strcmp(name, port->wwpn_iid[i].name) == 0) { 2123 iid = i; 2124 break; 2125 } 2126 } 2127 } 2128 2129 if (iid < 0) { 2130 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2131 if (port->wwpn_iid[i].in_use == 0 && 2132 port->wwpn_iid[i].wwpn == 0 && 2133 port->wwpn_iid[i].name == NULL) { 2134 iid = i; 2135 break; 2136 } 2137 } 2138 } 2139 2140 if (iid < 0) { 2141 best = -1; 2142 best_time = INT32_MAX; 2143 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2144 if (port->wwpn_iid[i].in_use == 0) { 2145 if (port->wwpn_iid[i].last_use < best_time) { 2146 best = i; 2147 best_time = port->wwpn_iid[i].last_use; 2148 } 2149 } 2150 } 2151 iid = best; 2152 } 2153 2154 if (iid < 0) { 2155 mtx_unlock(&softc->ctl_lock); 2156 free(name, M_CTL); 2157 return (-2); 2158 } 2159 2160 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2161 /* 2162 * This is not an error yet. 2163 */ 2164 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2165 #if 0 2166 printf("%s: port %d iid %u WWPN %#jx arrived" 2167 " again\n", __func__, port->targ_port, 2168 iid, (uintmax_t)wwpn); 2169 #endif 2170 goto take; 2171 } 2172 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2173 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2174 #if 0 2175 printf("%s: port %d iid %u name '%s' arrived" 2176 " again\n", __func__, port->targ_port, 2177 iid, name); 2178 #endif 2179 goto take; 2180 } 2181 2182 /* 2183 * This is an error, but what do we do about it? The 2184 * driver is telling us we have a new WWPN for this 2185 * initiator ID, so we pretty much need to use it. 2186 */ 2187 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2188 " but WWPN %#jx '%s' is still at that address\n", 2189 __func__, port->targ_port, iid, wwpn, name, 2190 (uintmax_t)port->wwpn_iid[iid].wwpn, 2191 port->wwpn_iid[iid].name); 2192 } 2193 take: 2194 free(port->wwpn_iid[iid].name, M_CTL); 2195 port->wwpn_iid[iid].name = name; 2196 port->wwpn_iid[iid].wwpn = wwpn; 2197 port->wwpn_iid[iid].in_use++; 2198 mtx_unlock(&softc->ctl_lock); 2199 ctl_isc_announce_iid(port, iid); 2200 2201 return (iid); 2202 } 2203 2204 static int 2205 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2206 { 2207 int len; 2208 2209 switch (port->port_type) { 2210 case CTL_PORT_FC: 2211 { 2212 struct scsi_transportid_fcp *id = 2213 (struct scsi_transportid_fcp *)buf; 2214 if (port->wwpn_iid[iid].wwpn == 0) 2215 return (0); 2216 memset(id, 0, sizeof(*id)); 2217 id->format_protocol = SCSI_PROTO_FC; 2218 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2219 return (sizeof(*id)); 2220 } 2221 case CTL_PORT_ISCSI: 2222 { 2223 struct scsi_transportid_iscsi_port *id = 2224 (struct scsi_transportid_iscsi_port *)buf; 2225 if (port->wwpn_iid[iid].name == NULL) 2226 return (0); 2227 memset(id, 0, 256); 2228 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2229 SCSI_PROTO_ISCSI; 2230 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2231 len = roundup2(min(len, 252), 4); 2232 scsi_ulto2b(len, id->additional_length); 2233 return (sizeof(*id) + len); 2234 } 2235 case CTL_PORT_SAS: 2236 { 2237 struct scsi_transportid_sas *id = 2238 (struct scsi_transportid_sas *)buf; 2239 if (port->wwpn_iid[iid].wwpn == 0) 2240 return (0); 2241 memset(id, 0, sizeof(*id)); 2242 id->format_protocol = SCSI_PROTO_SAS; 2243 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2244 return (sizeof(*id)); 2245 } 2246 default: 2247 { 2248 struct scsi_transportid_spi *id = 2249 (struct scsi_transportid_spi *)buf; 2250 memset(id, 0, sizeof(*id)); 2251 id->format_protocol = SCSI_PROTO_SPI; 2252 scsi_ulto2b(iid, id->scsi_addr); 2253 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2254 return (sizeof(*id)); 2255 } 2256 } 2257 } 2258 2259 /* 2260 * Serialize a command that went down the "wrong" side, and so was sent to 2261 * this controller for execution. The logic is a little different than the 2262 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2263 * sent back to the other side, but in the success case, we execute the 2264 * command on this side (XFER mode) or tell the other side to execute it 2265 * (SER_ONLY mode). 2266 */ 2267 static void 2268 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2269 { 2270 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2271 struct ctl_port *port = CTL_PORT(ctsio); 2272 union ctl_ha_msg msg_info; 2273 struct ctl_lun *lun; 2274 const struct ctl_cmd_entry *entry; 2275 union ctl_io *bio; 2276 uint32_t targ_lun; 2277 2278 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2279 2280 /* Make sure that we know about this port. */ 2281 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2282 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2283 /*retry_count*/ 1); 2284 goto badjuju; 2285 } 2286 2287 /* Make sure that we know about this LUN. */ 2288 mtx_lock(&softc->ctl_lock); 2289 if (targ_lun >= ctl_max_luns || 2290 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2291 mtx_unlock(&softc->ctl_lock); 2292 2293 /* 2294 * The other node would not send this request to us unless 2295 * received announce that we are primary node for this LUN. 2296 * If this LUN does not exist now, it is probably result of 2297 * a race, so respond to initiator in the most opaque way. 2298 */ 2299 ctl_set_busy(ctsio); 2300 goto badjuju; 2301 } 2302 mtx_lock(&lun->lun_lock); 2303 mtx_unlock(&softc->ctl_lock); 2304 2305 /* 2306 * If the LUN is invalid, pretend that it doesn't exist. 2307 * It will go away as soon as all pending I/Os completed. 2308 */ 2309 if (lun->flags & CTL_LUN_DISABLED) { 2310 mtx_unlock(&lun->lun_lock); 2311 ctl_set_busy(ctsio); 2312 goto badjuju; 2313 } 2314 2315 entry = ctl_get_cmd_entry(ctsio, NULL); 2316 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2317 mtx_unlock(&lun->lun_lock); 2318 goto badjuju; 2319 } 2320 2321 CTL_LUN(ctsio) = lun; 2322 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2323 2324 /* 2325 * Every I/O goes into the OOA queue for a 2326 * particular LUN, and stays there until completion. 2327 */ 2328 #ifdef CTL_TIME_IO 2329 if (LIST_EMPTY(&lun->ooa_queue)) 2330 lun->idle_time += getsbinuptime() - lun->last_busy; 2331 #endif 2332 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2333 2334 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 2335 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 2336 case CTL_ACTION_BLOCK: 2337 ctsio->io_hdr.blocker = bio; 2338 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 2339 blocked_links); 2340 mtx_unlock(&lun->lun_lock); 2341 break; 2342 case CTL_ACTION_PASS: 2343 case CTL_ACTION_SKIP: 2344 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2345 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2346 ctl_enqueue_rtr((union ctl_io *)ctsio); 2347 mtx_unlock(&lun->lun_lock); 2348 } else { 2349 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2350 mtx_unlock(&lun->lun_lock); 2351 2352 /* send msg back to other side */ 2353 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2354 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2355 msg_info.hdr.msg_type = CTL_MSG_R2R; 2356 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2357 sizeof(msg_info.hdr), M_WAITOK); 2358 } 2359 break; 2360 case CTL_ACTION_OVERLAP: 2361 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2362 mtx_unlock(&lun->lun_lock); 2363 ctl_set_overlapped_cmd(ctsio); 2364 goto badjuju; 2365 case CTL_ACTION_OVERLAP_TAG: 2366 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2367 mtx_unlock(&lun->lun_lock); 2368 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2369 goto badjuju; 2370 case CTL_ACTION_ERROR: 2371 default: 2372 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2373 mtx_unlock(&lun->lun_lock); 2374 2375 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2376 /*retry_count*/ 0); 2377 badjuju: 2378 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2379 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2380 msg_info.hdr.serializing_sc = NULL; 2381 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2382 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2383 sizeof(msg_info.scsi), M_WAITOK); 2384 ctl_free_io((union ctl_io *)ctsio); 2385 break; 2386 } 2387 } 2388 2389 /* 2390 * Returns 0 for success, errno for failure. 2391 */ 2392 static void 2393 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2394 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2395 { 2396 struct ctl_io_hdr *ioh; 2397 2398 mtx_lock(&lun->lun_lock); 2399 ioh = LIST_FIRST(&lun->ooa_queue); 2400 if (ioh == NULL) { 2401 mtx_unlock(&lun->lun_lock); 2402 return; 2403 } 2404 while (LIST_NEXT(ioh, ooa_links) != NULL) 2405 ioh = LIST_NEXT(ioh, ooa_links); 2406 for ( ; ioh; ioh = LIST_PREV(ioh, &lun->ooa_queue, ctl_io_hdr, ooa_links)) { 2407 union ctl_io *io = (union ctl_io *)ioh; 2408 struct ctl_ooa_entry *entry; 2409 2410 /* 2411 * If we've got more than we can fit, just count the 2412 * remaining entries. 2413 */ 2414 if (*cur_fill_num >= ooa_hdr->alloc_num) { 2415 (*cur_fill_num)++; 2416 continue; 2417 } 2418 2419 entry = &kern_entries[*cur_fill_num]; 2420 2421 entry->tag_num = io->scsiio.tag_num; 2422 entry->lun_num = lun->lun; 2423 #ifdef CTL_TIME_IO 2424 entry->start_bt = io->io_hdr.start_bt; 2425 #endif 2426 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2427 entry->cdb_len = io->scsiio.cdb_len; 2428 if (io->io_hdr.blocker != NULL) 2429 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2430 2431 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2432 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2433 2434 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2435 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2436 2437 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2438 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2439 2440 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2441 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2442 2443 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) 2444 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED; 2445 2446 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) 2447 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT; 2448 (*cur_fill_num)++; 2449 } 2450 mtx_unlock(&lun->lun_lock); 2451 } 2452 2453 /* 2454 * Escape characters that are illegal or not recommended in XML. 2455 */ 2456 int 2457 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2458 { 2459 char *end = str + size; 2460 int retval; 2461 2462 retval = 0; 2463 2464 for (; *str && str < end; str++) { 2465 switch (*str) { 2466 case '&': 2467 retval = sbuf_printf(sb, "&"); 2468 break; 2469 case '>': 2470 retval = sbuf_printf(sb, ">"); 2471 break; 2472 case '<': 2473 retval = sbuf_printf(sb, "<"); 2474 break; 2475 default: 2476 retval = sbuf_putc(sb, *str); 2477 break; 2478 } 2479 2480 if (retval != 0) 2481 break; 2482 } 2483 2484 return (retval); 2485 } 2486 2487 static void 2488 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2489 { 2490 struct scsi_vpd_id_descriptor *desc; 2491 int i; 2492 2493 if (id == NULL || id->len < 4) 2494 return; 2495 desc = (struct scsi_vpd_id_descriptor *)id->data; 2496 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2497 case SVPD_ID_TYPE_T10: 2498 sbuf_printf(sb, "t10."); 2499 break; 2500 case SVPD_ID_TYPE_EUI64: 2501 sbuf_printf(sb, "eui."); 2502 break; 2503 case SVPD_ID_TYPE_NAA: 2504 sbuf_printf(sb, "naa."); 2505 break; 2506 case SVPD_ID_TYPE_SCSI_NAME: 2507 break; 2508 } 2509 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2510 case SVPD_ID_CODESET_BINARY: 2511 for (i = 0; i < desc->length; i++) 2512 sbuf_printf(sb, "%02x", desc->identifier[i]); 2513 break; 2514 case SVPD_ID_CODESET_ASCII: 2515 sbuf_printf(sb, "%.*s", (int)desc->length, 2516 (char *)desc->identifier); 2517 break; 2518 case SVPD_ID_CODESET_UTF8: 2519 sbuf_printf(sb, "%s", (char *)desc->identifier); 2520 break; 2521 } 2522 } 2523 2524 static int 2525 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2526 struct thread *td) 2527 { 2528 struct ctl_softc *softc = dev->si_drv1; 2529 struct ctl_port *port; 2530 struct ctl_lun *lun; 2531 int retval; 2532 2533 retval = 0; 2534 2535 switch (cmd) { 2536 case CTL_IO: 2537 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2538 break; 2539 case CTL_ENABLE_PORT: 2540 case CTL_DISABLE_PORT: 2541 case CTL_SET_PORT_WWNS: { 2542 struct ctl_port *port; 2543 struct ctl_port_entry *entry; 2544 2545 entry = (struct ctl_port_entry *)addr; 2546 2547 mtx_lock(&softc->ctl_lock); 2548 STAILQ_FOREACH(port, &softc->port_list, links) { 2549 int action, done; 2550 2551 if (port->targ_port < softc->port_min || 2552 port->targ_port >= softc->port_max) 2553 continue; 2554 2555 action = 0; 2556 done = 0; 2557 if ((entry->port_type == CTL_PORT_NONE) 2558 && (entry->targ_port == port->targ_port)) { 2559 /* 2560 * If the user only wants to enable or 2561 * disable or set WWNs on a specific port, 2562 * do the operation and we're done. 2563 */ 2564 action = 1; 2565 done = 1; 2566 } else if (entry->port_type & port->port_type) { 2567 /* 2568 * Compare the user's type mask with the 2569 * particular frontend type to see if we 2570 * have a match. 2571 */ 2572 action = 1; 2573 done = 0; 2574 2575 /* 2576 * Make sure the user isn't trying to set 2577 * WWNs on multiple ports at the same time. 2578 */ 2579 if (cmd == CTL_SET_PORT_WWNS) { 2580 printf("%s: Can't set WWNs on " 2581 "multiple ports\n", __func__); 2582 retval = EINVAL; 2583 break; 2584 } 2585 } 2586 if (action == 0) 2587 continue; 2588 2589 /* 2590 * XXX KDM we have to drop the lock here, because 2591 * the online/offline operations can potentially 2592 * block. We need to reference count the frontends 2593 * so they can't go away, 2594 */ 2595 if (cmd == CTL_ENABLE_PORT) { 2596 mtx_unlock(&softc->ctl_lock); 2597 ctl_port_online(port); 2598 mtx_lock(&softc->ctl_lock); 2599 } else if (cmd == CTL_DISABLE_PORT) { 2600 mtx_unlock(&softc->ctl_lock); 2601 ctl_port_offline(port); 2602 mtx_lock(&softc->ctl_lock); 2603 } else if (cmd == CTL_SET_PORT_WWNS) { 2604 ctl_port_set_wwns(port, 2605 (entry->flags & CTL_PORT_WWNN_VALID) ? 2606 1 : 0, entry->wwnn, 2607 (entry->flags & CTL_PORT_WWPN_VALID) ? 2608 1 : 0, entry->wwpn); 2609 } 2610 if (done != 0) 2611 break; 2612 } 2613 mtx_unlock(&softc->ctl_lock); 2614 break; 2615 } 2616 case CTL_GET_OOA: { 2617 struct ctl_ooa *ooa_hdr; 2618 struct ctl_ooa_entry *entries; 2619 uint32_t cur_fill_num; 2620 2621 ooa_hdr = (struct ctl_ooa *)addr; 2622 2623 if ((ooa_hdr->alloc_len == 0) 2624 || (ooa_hdr->alloc_num == 0)) { 2625 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2626 "must be non-zero\n", __func__, 2627 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2628 retval = EINVAL; 2629 break; 2630 } 2631 2632 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2633 sizeof(struct ctl_ooa_entry))) { 2634 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2635 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2636 __func__, ooa_hdr->alloc_len, 2637 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2638 retval = EINVAL; 2639 break; 2640 } 2641 2642 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2643 if (entries == NULL) { 2644 printf("%s: could not allocate %d bytes for OOA " 2645 "dump\n", __func__, ooa_hdr->alloc_len); 2646 retval = ENOMEM; 2647 break; 2648 } 2649 2650 mtx_lock(&softc->ctl_lock); 2651 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2652 (ooa_hdr->lun_num >= ctl_max_luns || 2653 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2654 mtx_unlock(&softc->ctl_lock); 2655 free(entries, M_CTL); 2656 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2657 __func__, (uintmax_t)ooa_hdr->lun_num); 2658 retval = EINVAL; 2659 break; 2660 } 2661 2662 cur_fill_num = 0; 2663 2664 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2665 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2666 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2667 ooa_hdr, entries); 2668 } 2669 } else { 2670 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2671 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2672 entries); 2673 } 2674 mtx_unlock(&softc->ctl_lock); 2675 2676 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2677 ooa_hdr->fill_len = ooa_hdr->fill_num * 2678 sizeof(struct ctl_ooa_entry); 2679 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2680 if (retval != 0) { 2681 printf("%s: error copying out %d bytes for OOA dump\n", 2682 __func__, ooa_hdr->fill_len); 2683 } 2684 2685 getbinuptime(&ooa_hdr->cur_bt); 2686 2687 if (cur_fill_num > ooa_hdr->alloc_num) { 2688 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2689 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2690 } else { 2691 ooa_hdr->dropped_num = 0; 2692 ooa_hdr->status = CTL_OOA_OK; 2693 } 2694 2695 free(entries, M_CTL); 2696 break; 2697 } 2698 case CTL_DELAY_IO: { 2699 struct ctl_io_delay_info *delay_info; 2700 2701 delay_info = (struct ctl_io_delay_info *)addr; 2702 2703 #ifdef CTL_IO_DELAY 2704 mtx_lock(&softc->ctl_lock); 2705 if (delay_info->lun_id >= ctl_max_luns || 2706 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2707 mtx_unlock(&softc->ctl_lock); 2708 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2709 break; 2710 } 2711 mtx_lock(&lun->lun_lock); 2712 mtx_unlock(&softc->ctl_lock); 2713 delay_info->status = CTL_DELAY_STATUS_OK; 2714 switch (delay_info->delay_type) { 2715 case CTL_DELAY_TYPE_CONT: 2716 case CTL_DELAY_TYPE_ONESHOT: 2717 break; 2718 default: 2719 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2720 break; 2721 } 2722 switch (delay_info->delay_loc) { 2723 case CTL_DELAY_LOC_DATAMOVE: 2724 lun->delay_info.datamove_type = delay_info->delay_type; 2725 lun->delay_info.datamove_delay = delay_info->delay_secs; 2726 break; 2727 case CTL_DELAY_LOC_DONE: 2728 lun->delay_info.done_type = delay_info->delay_type; 2729 lun->delay_info.done_delay = delay_info->delay_secs; 2730 break; 2731 default: 2732 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2733 break; 2734 } 2735 mtx_unlock(&lun->lun_lock); 2736 #else 2737 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2738 #endif /* CTL_IO_DELAY */ 2739 break; 2740 } 2741 case CTL_ERROR_INJECT: { 2742 struct ctl_error_desc *err_desc, *new_err_desc; 2743 2744 err_desc = (struct ctl_error_desc *)addr; 2745 2746 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2747 M_WAITOK | M_ZERO); 2748 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2749 2750 mtx_lock(&softc->ctl_lock); 2751 if (err_desc->lun_id >= ctl_max_luns || 2752 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2753 mtx_unlock(&softc->ctl_lock); 2754 free(new_err_desc, M_CTL); 2755 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2756 __func__, (uintmax_t)err_desc->lun_id); 2757 retval = EINVAL; 2758 break; 2759 } 2760 mtx_lock(&lun->lun_lock); 2761 mtx_unlock(&softc->ctl_lock); 2762 2763 /* 2764 * We could do some checking here to verify the validity 2765 * of the request, but given the complexity of error 2766 * injection requests, the checking logic would be fairly 2767 * complex. 2768 * 2769 * For now, if the request is invalid, it just won't get 2770 * executed and might get deleted. 2771 */ 2772 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2773 2774 /* 2775 * XXX KDM check to make sure the serial number is unique, 2776 * in case we somehow manage to wrap. That shouldn't 2777 * happen for a very long time, but it's the right thing to 2778 * do. 2779 */ 2780 new_err_desc->serial = lun->error_serial; 2781 err_desc->serial = lun->error_serial; 2782 lun->error_serial++; 2783 2784 mtx_unlock(&lun->lun_lock); 2785 break; 2786 } 2787 case CTL_ERROR_INJECT_DELETE: { 2788 struct ctl_error_desc *delete_desc, *desc, *desc2; 2789 int delete_done; 2790 2791 delete_desc = (struct ctl_error_desc *)addr; 2792 delete_done = 0; 2793 2794 mtx_lock(&softc->ctl_lock); 2795 if (delete_desc->lun_id >= ctl_max_luns || 2796 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2797 mtx_unlock(&softc->ctl_lock); 2798 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2799 __func__, (uintmax_t)delete_desc->lun_id); 2800 retval = EINVAL; 2801 break; 2802 } 2803 mtx_lock(&lun->lun_lock); 2804 mtx_unlock(&softc->ctl_lock); 2805 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2806 if (desc->serial != delete_desc->serial) 2807 continue; 2808 2809 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2810 links); 2811 free(desc, M_CTL); 2812 delete_done = 1; 2813 } 2814 mtx_unlock(&lun->lun_lock); 2815 if (delete_done == 0) { 2816 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2817 "error serial %ju on LUN %u\n", __func__, 2818 delete_desc->serial, delete_desc->lun_id); 2819 retval = EINVAL; 2820 break; 2821 } 2822 break; 2823 } 2824 case CTL_DUMP_STRUCTS: { 2825 int j, k; 2826 struct ctl_port *port; 2827 struct ctl_frontend *fe; 2828 2829 mtx_lock(&softc->ctl_lock); 2830 printf("CTL Persistent Reservation information start:\n"); 2831 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2832 mtx_lock(&lun->lun_lock); 2833 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2834 mtx_unlock(&lun->lun_lock); 2835 continue; 2836 } 2837 2838 for (j = 0; j < ctl_max_ports; j++) { 2839 if (lun->pr_keys[j] == NULL) 2840 continue; 2841 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2842 if (lun->pr_keys[j][k] == 0) 2843 continue; 2844 printf(" LUN %ju port %d iid %d key " 2845 "%#jx\n", lun->lun, j, k, 2846 (uintmax_t)lun->pr_keys[j][k]); 2847 } 2848 } 2849 mtx_unlock(&lun->lun_lock); 2850 } 2851 printf("CTL Persistent Reservation information end\n"); 2852 printf("CTL Ports:\n"); 2853 STAILQ_FOREACH(port, &softc->port_list, links) { 2854 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2855 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2856 port->frontend->name, port->port_type, 2857 port->physical_port, port->virtual_port, 2858 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2859 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2860 if (port->wwpn_iid[j].in_use == 0 && 2861 port->wwpn_iid[j].wwpn == 0 && 2862 port->wwpn_iid[j].name == NULL) 2863 continue; 2864 2865 printf(" iid %u use %d WWPN %#jx '%s'\n", 2866 j, port->wwpn_iid[j].in_use, 2867 (uintmax_t)port->wwpn_iid[j].wwpn, 2868 port->wwpn_iid[j].name); 2869 } 2870 } 2871 printf("CTL Port information end\n"); 2872 mtx_unlock(&softc->ctl_lock); 2873 /* 2874 * XXX KDM calling this without a lock. We'd likely want 2875 * to drop the lock before calling the frontend's dump 2876 * routine anyway. 2877 */ 2878 printf("CTL Frontends:\n"); 2879 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2880 printf(" Frontend '%s'\n", fe->name); 2881 if (fe->fe_dump != NULL) 2882 fe->fe_dump(); 2883 } 2884 printf("CTL Frontend information end\n"); 2885 break; 2886 } 2887 case CTL_LUN_REQ: { 2888 struct ctl_lun_req *lun_req; 2889 struct ctl_backend_driver *backend; 2890 void *packed; 2891 nvlist_t *tmp_args_nvl; 2892 size_t packed_len; 2893 2894 lun_req = (struct ctl_lun_req *)addr; 2895 tmp_args_nvl = lun_req->args_nvl; 2896 2897 backend = ctl_backend_find(lun_req->backend); 2898 if (backend == NULL) { 2899 lun_req->status = CTL_LUN_ERROR; 2900 snprintf(lun_req->error_str, 2901 sizeof(lun_req->error_str), 2902 "Backend \"%s\" not found.", 2903 lun_req->backend); 2904 break; 2905 } 2906 2907 if (lun_req->args != NULL) { 2908 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2909 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2910 free(packed, M_CTL); 2911 lun_req->status = CTL_LUN_ERROR; 2912 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2913 "Cannot copyin args."); 2914 break; 2915 } 2916 lun_req->args_nvl = nvlist_unpack(packed, 2917 lun_req->args_len, 0); 2918 free(packed, M_CTL); 2919 2920 if (lun_req->args_nvl == NULL) { 2921 lun_req->status = CTL_LUN_ERROR; 2922 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2923 "Cannot unpack args nvlist."); 2924 break; 2925 } 2926 } else 2927 lun_req->args_nvl = nvlist_create(0); 2928 2929 retval = backend->ioctl(dev, cmd, addr, flag, td); 2930 nvlist_destroy(lun_req->args_nvl); 2931 lun_req->args_nvl = tmp_args_nvl; 2932 2933 if (lun_req->result_nvl != NULL) { 2934 if (lun_req->result != NULL) { 2935 packed = nvlist_pack(lun_req->result_nvl, 2936 &packed_len); 2937 if (packed == NULL) { 2938 lun_req->status = CTL_LUN_ERROR; 2939 snprintf(lun_req->error_str, 2940 sizeof(lun_req->error_str), 2941 "Cannot pack result nvlist."); 2942 break; 2943 } 2944 2945 if (packed_len > lun_req->result_len) { 2946 lun_req->status = CTL_LUN_ERROR; 2947 snprintf(lun_req->error_str, 2948 sizeof(lun_req->error_str), 2949 "Result nvlist too large."); 2950 free(packed, M_NVLIST); 2951 break; 2952 } 2953 2954 if (copyout(packed, lun_req->result, packed_len)) { 2955 lun_req->status = CTL_LUN_ERROR; 2956 snprintf(lun_req->error_str, 2957 sizeof(lun_req->error_str), 2958 "Cannot copyout() the result."); 2959 free(packed, M_NVLIST); 2960 break; 2961 } 2962 2963 lun_req->result_len = packed_len; 2964 free(packed, M_NVLIST); 2965 } 2966 2967 nvlist_destroy(lun_req->result_nvl); 2968 } 2969 break; 2970 } 2971 case CTL_LUN_LIST: { 2972 struct sbuf *sb; 2973 struct ctl_lun_list *list; 2974 const char *name, *value; 2975 void *cookie; 2976 int type; 2977 2978 list = (struct ctl_lun_list *)addr; 2979 2980 /* 2981 * Allocate a fixed length sbuf here, based on the length 2982 * of the user's buffer. We could allocate an auto-extending 2983 * buffer, and then tell the user how much larger our 2984 * amount of data is than his buffer, but that presents 2985 * some problems: 2986 * 2987 * 1. The sbuf(9) routines use a blocking malloc, and so 2988 * we can't hold a lock while calling them with an 2989 * auto-extending buffer. 2990 * 2991 * 2. There is not currently a LUN reference counting 2992 * mechanism, outside of outstanding transactions on 2993 * the LUN's OOA queue. So a LUN could go away on us 2994 * while we're getting the LUN number, backend-specific 2995 * information, etc. Thus, given the way things 2996 * currently work, we need to hold the CTL lock while 2997 * grabbing LUN information. 2998 * 2999 * So, from the user's standpoint, the best thing to do is 3000 * allocate what he thinks is a reasonable buffer length, 3001 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3002 * double the buffer length and try again. (And repeat 3003 * that until he succeeds.) 3004 */ 3005 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3006 if (sb == NULL) { 3007 list->status = CTL_LUN_LIST_ERROR; 3008 snprintf(list->error_str, sizeof(list->error_str), 3009 "Unable to allocate %d bytes for LUN list", 3010 list->alloc_len); 3011 break; 3012 } 3013 3014 sbuf_printf(sb, "<ctllunlist>\n"); 3015 3016 mtx_lock(&softc->ctl_lock); 3017 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3018 mtx_lock(&lun->lun_lock); 3019 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3020 (uintmax_t)lun->lun); 3021 3022 /* 3023 * Bail out as soon as we see that we've overfilled 3024 * the buffer. 3025 */ 3026 if (retval != 0) 3027 break; 3028 3029 retval = sbuf_printf(sb, "\t<backend_type>%s" 3030 "</backend_type>\n", 3031 (lun->backend == NULL) ? "none" : 3032 lun->backend->name); 3033 3034 if (retval != 0) 3035 break; 3036 3037 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3038 lun->be_lun->lun_type); 3039 3040 if (retval != 0) 3041 break; 3042 3043 if (lun->backend == NULL) { 3044 retval = sbuf_printf(sb, "</lun>\n"); 3045 if (retval != 0) 3046 break; 3047 continue; 3048 } 3049 3050 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3051 (lun->be_lun->maxlba > 0) ? 3052 lun->be_lun->maxlba + 1 : 0); 3053 3054 if (retval != 0) 3055 break; 3056 3057 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3058 lun->be_lun->blocksize); 3059 3060 if (retval != 0) 3061 break; 3062 3063 retval = sbuf_printf(sb, "\t<serial_number>"); 3064 3065 if (retval != 0) 3066 break; 3067 3068 retval = ctl_sbuf_printf_esc(sb, 3069 lun->be_lun->serial_num, 3070 sizeof(lun->be_lun->serial_num)); 3071 3072 if (retval != 0) 3073 break; 3074 3075 retval = sbuf_printf(sb, "</serial_number>\n"); 3076 3077 if (retval != 0) 3078 break; 3079 3080 retval = sbuf_printf(sb, "\t<device_id>"); 3081 3082 if (retval != 0) 3083 break; 3084 3085 retval = ctl_sbuf_printf_esc(sb, 3086 lun->be_lun->device_id, 3087 sizeof(lun->be_lun->device_id)); 3088 3089 if (retval != 0) 3090 break; 3091 3092 retval = sbuf_printf(sb, "</device_id>\n"); 3093 3094 if (retval != 0) 3095 break; 3096 3097 if (lun->backend->lun_info != NULL) { 3098 retval = lun->backend->lun_info(lun->be_lun, sb); 3099 if (retval != 0) 3100 break; 3101 } 3102 3103 cookie = NULL; 3104 while ((name = nvlist_next(lun->be_lun->options, &type, 3105 &cookie)) != NULL) { 3106 sbuf_printf(sb, "\t<%s>", name); 3107 3108 if (type == NV_TYPE_STRING) { 3109 value = dnvlist_get_string( 3110 lun->be_lun->options, name, NULL); 3111 if (value != NULL) 3112 sbuf_printf(sb, "%s", value); 3113 } 3114 3115 sbuf_printf(sb, "</%s>\n", name); 3116 } 3117 3118 retval = sbuf_printf(sb, "</lun>\n"); 3119 3120 if (retval != 0) 3121 break; 3122 mtx_unlock(&lun->lun_lock); 3123 } 3124 if (lun != NULL) 3125 mtx_unlock(&lun->lun_lock); 3126 mtx_unlock(&softc->ctl_lock); 3127 3128 if ((retval != 0) 3129 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3130 retval = 0; 3131 sbuf_delete(sb); 3132 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3133 snprintf(list->error_str, sizeof(list->error_str), 3134 "Out of space, %d bytes is too small", 3135 list->alloc_len); 3136 break; 3137 } 3138 3139 sbuf_finish(sb); 3140 3141 retval = copyout(sbuf_data(sb), list->lun_xml, 3142 sbuf_len(sb) + 1); 3143 3144 list->fill_len = sbuf_len(sb) + 1; 3145 list->status = CTL_LUN_LIST_OK; 3146 sbuf_delete(sb); 3147 break; 3148 } 3149 case CTL_ISCSI: { 3150 struct ctl_iscsi *ci; 3151 struct ctl_frontend *fe; 3152 3153 ci = (struct ctl_iscsi *)addr; 3154 3155 fe = ctl_frontend_find("iscsi"); 3156 if (fe == NULL) { 3157 ci->status = CTL_ISCSI_ERROR; 3158 snprintf(ci->error_str, sizeof(ci->error_str), 3159 "Frontend \"iscsi\" not found."); 3160 break; 3161 } 3162 3163 retval = fe->ioctl(dev, cmd, addr, flag, td); 3164 break; 3165 } 3166 case CTL_PORT_REQ: { 3167 struct ctl_req *req; 3168 struct ctl_frontend *fe; 3169 void *packed; 3170 nvlist_t *tmp_args_nvl; 3171 size_t packed_len; 3172 3173 req = (struct ctl_req *)addr; 3174 tmp_args_nvl = req->args_nvl; 3175 3176 fe = ctl_frontend_find(req->driver); 3177 if (fe == NULL) { 3178 req->status = CTL_LUN_ERROR; 3179 snprintf(req->error_str, sizeof(req->error_str), 3180 "Frontend \"%s\" not found.", req->driver); 3181 break; 3182 } 3183 3184 if (req->args != NULL) { 3185 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3186 if (copyin(req->args, packed, req->args_len) != 0) { 3187 free(packed, M_CTL); 3188 req->status = CTL_LUN_ERROR; 3189 snprintf(req->error_str, sizeof(req->error_str), 3190 "Cannot copyin args."); 3191 break; 3192 } 3193 req->args_nvl = nvlist_unpack(packed, 3194 req->args_len, 0); 3195 free(packed, M_CTL); 3196 3197 if (req->args_nvl == NULL) { 3198 req->status = CTL_LUN_ERROR; 3199 snprintf(req->error_str, sizeof(req->error_str), 3200 "Cannot unpack args nvlist."); 3201 break; 3202 } 3203 } else 3204 req->args_nvl = nvlist_create(0); 3205 3206 if (fe->ioctl) 3207 retval = fe->ioctl(dev, cmd, addr, flag, td); 3208 else 3209 retval = ENODEV; 3210 3211 nvlist_destroy(req->args_nvl); 3212 req->args_nvl = tmp_args_nvl; 3213 3214 if (req->result_nvl != NULL) { 3215 if (req->result != NULL) { 3216 packed = nvlist_pack(req->result_nvl, 3217 &packed_len); 3218 if (packed == NULL) { 3219 req->status = CTL_LUN_ERROR; 3220 snprintf(req->error_str, 3221 sizeof(req->error_str), 3222 "Cannot pack result nvlist."); 3223 break; 3224 } 3225 3226 if (packed_len > req->result_len) { 3227 req->status = CTL_LUN_ERROR; 3228 snprintf(req->error_str, 3229 sizeof(req->error_str), 3230 "Result nvlist too large."); 3231 free(packed, M_NVLIST); 3232 break; 3233 } 3234 3235 if (copyout(packed, req->result, packed_len)) { 3236 req->status = CTL_LUN_ERROR; 3237 snprintf(req->error_str, 3238 sizeof(req->error_str), 3239 "Cannot copyout() the result."); 3240 free(packed, M_NVLIST); 3241 break; 3242 } 3243 3244 req->result_len = packed_len; 3245 free(packed, M_NVLIST); 3246 } 3247 3248 nvlist_destroy(req->result_nvl); 3249 } 3250 break; 3251 } 3252 case CTL_PORT_LIST: { 3253 struct sbuf *sb; 3254 struct ctl_port *port; 3255 struct ctl_lun_list *list; 3256 const char *name, *value; 3257 void *cookie; 3258 int j, type; 3259 uint32_t plun; 3260 3261 list = (struct ctl_lun_list *)addr; 3262 3263 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3264 if (sb == NULL) { 3265 list->status = CTL_LUN_LIST_ERROR; 3266 snprintf(list->error_str, sizeof(list->error_str), 3267 "Unable to allocate %d bytes for LUN list", 3268 list->alloc_len); 3269 break; 3270 } 3271 3272 sbuf_printf(sb, "<ctlportlist>\n"); 3273 3274 mtx_lock(&softc->ctl_lock); 3275 STAILQ_FOREACH(port, &softc->port_list, links) { 3276 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3277 (uintmax_t)port->targ_port); 3278 3279 /* 3280 * Bail out as soon as we see that we've overfilled 3281 * the buffer. 3282 */ 3283 if (retval != 0) 3284 break; 3285 3286 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3287 "</frontend_type>\n", port->frontend->name); 3288 if (retval != 0) 3289 break; 3290 3291 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3292 port->port_type); 3293 if (retval != 0) 3294 break; 3295 3296 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3297 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3298 if (retval != 0) 3299 break; 3300 3301 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3302 port->port_name); 3303 if (retval != 0) 3304 break; 3305 3306 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3307 port->physical_port); 3308 if (retval != 0) 3309 break; 3310 3311 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3312 port->virtual_port); 3313 if (retval != 0) 3314 break; 3315 3316 if (port->target_devid != NULL) { 3317 sbuf_printf(sb, "\t<target>"); 3318 ctl_id_sbuf(port->target_devid, sb); 3319 sbuf_printf(sb, "</target>\n"); 3320 } 3321 3322 if (port->port_devid != NULL) { 3323 sbuf_printf(sb, "\t<port>"); 3324 ctl_id_sbuf(port->port_devid, sb); 3325 sbuf_printf(sb, "</port>\n"); 3326 } 3327 3328 if (port->port_info != NULL) { 3329 retval = port->port_info(port->onoff_arg, sb); 3330 if (retval != 0) 3331 break; 3332 } 3333 3334 cookie = NULL; 3335 while ((name = nvlist_next(port->options, &type, 3336 &cookie)) != NULL) { 3337 sbuf_printf(sb, "\t<%s>", name); 3338 3339 if (type == NV_TYPE_STRING) { 3340 value = dnvlist_get_string(port->options, 3341 name, NULL); 3342 if (value != NULL) 3343 sbuf_printf(sb, "%s", value); 3344 } 3345 3346 sbuf_printf(sb, "</%s>\n", name); 3347 } 3348 3349 if (port->lun_map != NULL) { 3350 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3351 for (j = 0; j < port->lun_map_size; j++) { 3352 plun = ctl_lun_map_from_port(port, j); 3353 if (plun == UINT32_MAX) 3354 continue; 3355 sbuf_printf(sb, 3356 "\t<lun id=\"%u\">%u</lun>\n", 3357 j, plun); 3358 } 3359 } 3360 3361 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3362 if (port->wwpn_iid[j].in_use == 0 || 3363 (port->wwpn_iid[j].wwpn == 0 && 3364 port->wwpn_iid[j].name == NULL)) 3365 continue; 3366 3367 if (port->wwpn_iid[j].name != NULL) 3368 retval = sbuf_printf(sb, 3369 "\t<initiator id=\"%u\">%s</initiator>\n", 3370 j, port->wwpn_iid[j].name); 3371 else 3372 retval = sbuf_printf(sb, 3373 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3374 j, port->wwpn_iid[j].wwpn); 3375 if (retval != 0) 3376 break; 3377 } 3378 if (retval != 0) 3379 break; 3380 3381 retval = sbuf_printf(sb, "</targ_port>\n"); 3382 if (retval != 0) 3383 break; 3384 } 3385 mtx_unlock(&softc->ctl_lock); 3386 3387 if ((retval != 0) 3388 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3389 retval = 0; 3390 sbuf_delete(sb); 3391 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3392 snprintf(list->error_str, sizeof(list->error_str), 3393 "Out of space, %d bytes is too small", 3394 list->alloc_len); 3395 break; 3396 } 3397 3398 sbuf_finish(sb); 3399 3400 retval = copyout(sbuf_data(sb), list->lun_xml, 3401 sbuf_len(sb) + 1); 3402 3403 list->fill_len = sbuf_len(sb) + 1; 3404 list->status = CTL_LUN_LIST_OK; 3405 sbuf_delete(sb); 3406 break; 3407 } 3408 case CTL_LUN_MAP: { 3409 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3410 struct ctl_port *port; 3411 3412 mtx_lock(&softc->ctl_lock); 3413 if (lm->port < softc->port_min || 3414 lm->port >= softc->port_max || 3415 (port = softc->ctl_ports[lm->port]) == NULL) { 3416 mtx_unlock(&softc->ctl_lock); 3417 return (ENXIO); 3418 } 3419 if (port->status & CTL_PORT_STATUS_ONLINE) { 3420 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3421 if (ctl_lun_map_to_port(port, lun->lun) == 3422 UINT32_MAX) 3423 continue; 3424 mtx_lock(&lun->lun_lock); 3425 ctl_est_ua_port(lun, lm->port, -1, 3426 CTL_UA_LUN_CHANGE); 3427 mtx_unlock(&lun->lun_lock); 3428 } 3429 } 3430 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3431 if (lm->plun != UINT32_MAX) { 3432 if (lm->lun == UINT32_MAX) 3433 retval = ctl_lun_map_unset(port, lm->plun); 3434 else if (lm->lun < ctl_max_luns && 3435 softc->ctl_luns[lm->lun] != NULL) 3436 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3437 else 3438 return (ENXIO); 3439 } else { 3440 if (lm->lun == UINT32_MAX) 3441 retval = ctl_lun_map_deinit(port); 3442 else 3443 retval = ctl_lun_map_init(port); 3444 } 3445 if (port->status & CTL_PORT_STATUS_ONLINE) 3446 ctl_isc_announce_port(port); 3447 break; 3448 } 3449 case CTL_GET_LUN_STATS: { 3450 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3451 int i; 3452 3453 /* 3454 * XXX KDM no locking here. If the LUN list changes, 3455 * things can blow up. 3456 */ 3457 i = 0; 3458 stats->status = CTL_SS_OK; 3459 stats->fill_len = 0; 3460 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3461 if (lun->lun < stats->first_item) 3462 continue; 3463 if (stats->fill_len + sizeof(lun->stats) > 3464 stats->alloc_len) { 3465 stats->status = CTL_SS_NEED_MORE_SPACE; 3466 break; 3467 } 3468 retval = copyout(&lun->stats, &stats->stats[i++], 3469 sizeof(lun->stats)); 3470 if (retval != 0) 3471 break; 3472 stats->fill_len += sizeof(lun->stats); 3473 } 3474 stats->num_items = softc->num_luns; 3475 stats->flags = CTL_STATS_FLAG_NONE; 3476 #ifdef CTL_TIME_IO 3477 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3478 #endif 3479 getnanouptime(&stats->timestamp); 3480 break; 3481 } 3482 case CTL_GET_PORT_STATS: { 3483 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3484 int i; 3485 3486 /* 3487 * XXX KDM no locking here. If the LUN list changes, 3488 * things can blow up. 3489 */ 3490 i = 0; 3491 stats->status = CTL_SS_OK; 3492 stats->fill_len = 0; 3493 STAILQ_FOREACH(port, &softc->port_list, links) { 3494 if (port->targ_port < stats->first_item) 3495 continue; 3496 if (stats->fill_len + sizeof(port->stats) > 3497 stats->alloc_len) { 3498 stats->status = CTL_SS_NEED_MORE_SPACE; 3499 break; 3500 } 3501 retval = copyout(&port->stats, &stats->stats[i++], 3502 sizeof(port->stats)); 3503 if (retval != 0) 3504 break; 3505 stats->fill_len += sizeof(port->stats); 3506 } 3507 stats->num_items = softc->num_ports; 3508 stats->flags = CTL_STATS_FLAG_NONE; 3509 #ifdef CTL_TIME_IO 3510 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3511 #endif 3512 getnanouptime(&stats->timestamp); 3513 break; 3514 } 3515 default: { 3516 /* XXX KDM should we fix this? */ 3517 #if 0 3518 struct ctl_backend_driver *backend; 3519 unsigned int type; 3520 int found; 3521 3522 found = 0; 3523 3524 /* 3525 * We encode the backend type as the ioctl type for backend 3526 * ioctls. So parse it out here, and then search for a 3527 * backend of this type. 3528 */ 3529 type = _IOC_TYPE(cmd); 3530 3531 STAILQ_FOREACH(backend, &softc->be_list, links) { 3532 if (backend->type == type) { 3533 found = 1; 3534 break; 3535 } 3536 } 3537 if (found == 0) { 3538 printf("ctl: unknown ioctl command %#lx or backend " 3539 "%d\n", cmd, type); 3540 retval = EINVAL; 3541 break; 3542 } 3543 retval = backend->ioctl(dev, cmd, addr, flag, td); 3544 #endif 3545 retval = ENOTTY; 3546 break; 3547 } 3548 } 3549 return (retval); 3550 } 3551 3552 uint32_t 3553 ctl_get_initindex(struct ctl_nexus *nexus) 3554 { 3555 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3556 } 3557 3558 int 3559 ctl_lun_map_init(struct ctl_port *port) 3560 { 3561 struct ctl_softc *softc = port->ctl_softc; 3562 struct ctl_lun *lun; 3563 int size = ctl_lun_map_size; 3564 uint32_t i; 3565 3566 if (port->lun_map == NULL || port->lun_map_size < size) { 3567 port->lun_map_size = 0; 3568 free(port->lun_map, M_CTL); 3569 port->lun_map = malloc(size * sizeof(uint32_t), 3570 M_CTL, M_NOWAIT); 3571 } 3572 if (port->lun_map == NULL) 3573 return (ENOMEM); 3574 for (i = 0; i < size; i++) 3575 port->lun_map[i] = UINT32_MAX; 3576 port->lun_map_size = size; 3577 if (port->status & CTL_PORT_STATUS_ONLINE) { 3578 if (port->lun_disable != NULL) { 3579 STAILQ_FOREACH(lun, &softc->lun_list, links) 3580 port->lun_disable(port->targ_lun_arg, lun->lun); 3581 } 3582 ctl_isc_announce_port(port); 3583 } 3584 return (0); 3585 } 3586 3587 int 3588 ctl_lun_map_deinit(struct ctl_port *port) 3589 { 3590 struct ctl_softc *softc = port->ctl_softc; 3591 struct ctl_lun *lun; 3592 3593 if (port->lun_map == NULL) 3594 return (0); 3595 port->lun_map_size = 0; 3596 free(port->lun_map, M_CTL); 3597 port->lun_map = NULL; 3598 if (port->status & CTL_PORT_STATUS_ONLINE) { 3599 if (port->lun_enable != NULL) { 3600 STAILQ_FOREACH(lun, &softc->lun_list, links) 3601 port->lun_enable(port->targ_lun_arg, lun->lun); 3602 } 3603 ctl_isc_announce_port(port); 3604 } 3605 return (0); 3606 } 3607 3608 int 3609 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3610 { 3611 int status; 3612 uint32_t old; 3613 3614 if (port->lun_map == NULL) { 3615 status = ctl_lun_map_init(port); 3616 if (status != 0) 3617 return (status); 3618 } 3619 if (plun >= port->lun_map_size) 3620 return (EINVAL); 3621 old = port->lun_map[plun]; 3622 port->lun_map[plun] = glun; 3623 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3624 if (port->lun_enable != NULL) 3625 port->lun_enable(port->targ_lun_arg, plun); 3626 ctl_isc_announce_port(port); 3627 } 3628 return (0); 3629 } 3630 3631 int 3632 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3633 { 3634 uint32_t old; 3635 3636 if (port->lun_map == NULL || plun >= port->lun_map_size) 3637 return (0); 3638 old = port->lun_map[plun]; 3639 port->lun_map[plun] = UINT32_MAX; 3640 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3641 if (port->lun_disable != NULL) 3642 port->lun_disable(port->targ_lun_arg, plun); 3643 ctl_isc_announce_port(port); 3644 } 3645 return (0); 3646 } 3647 3648 uint32_t 3649 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3650 { 3651 3652 if (port == NULL) 3653 return (UINT32_MAX); 3654 if (port->lun_map == NULL) 3655 return (lun_id); 3656 if (lun_id > port->lun_map_size) 3657 return (UINT32_MAX); 3658 return (port->lun_map[lun_id]); 3659 } 3660 3661 uint32_t 3662 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3663 { 3664 uint32_t i; 3665 3666 if (port == NULL) 3667 return (UINT32_MAX); 3668 if (port->lun_map == NULL) 3669 return (lun_id); 3670 for (i = 0; i < port->lun_map_size; i++) { 3671 if (port->lun_map[i] == lun_id) 3672 return (i); 3673 } 3674 return (UINT32_MAX); 3675 } 3676 3677 uint32_t 3678 ctl_decode_lun(uint64_t encoded) 3679 { 3680 uint8_t lun[8]; 3681 uint32_t result = 0xffffffff; 3682 3683 be64enc(lun, encoded); 3684 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3685 case RPL_LUNDATA_ATYP_PERIPH: 3686 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3687 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3688 result = lun[1]; 3689 break; 3690 case RPL_LUNDATA_ATYP_FLAT: 3691 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3692 lun[6] == 0 && lun[7] == 0) 3693 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3694 break; 3695 case RPL_LUNDATA_ATYP_EXTLUN: 3696 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3697 case 0x02: 3698 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3699 case 0x00: 3700 result = lun[1]; 3701 break; 3702 case 0x10: 3703 result = (lun[1] << 16) + (lun[2] << 8) + 3704 lun[3]; 3705 break; 3706 case 0x20: 3707 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3708 result = (lun[2] << 24) + 3709 (lun[3] << 16) + (lun[4] << 8) + 3710 lun[5]; 3711 break; 3712 } 3713 break; 3714 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3715 result = 0xffffffff; 3716 break; 3717 } 3718 break; 3719 } 3720 return (result); 3721 } 3722 3723 uint64_t 3724 ctl_encode_lun(uint32_t decoded) 3725 { 3726 uint64_t l = decoded; 3727 3728 if (l <= 0xff) 3729 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3730 if (l <= 0x3fff) 3731 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3732 if (l <= 0xffffff) 3733 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3734 (l << 32)); 3735 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3736 } 3737 3738 int 3739 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3740 { 3741 int i; 3742 3743 for (i = first; i < last; i++) { 3744 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3745 return (i); 3746 } 3747 return (-1); 3748 } 3749 3750 int 3751 ctl_set_mask(uint32_t *mask, uint32_t bit) 3752 { 3753 uint32_t chunk, piece; 3754 3755 chunk = bit >> 5; 3756 piece = bit % (sizeof(uint32_t) * 8); 3757 3758 if ((mask[chunk] & (1 << piece)) != 0) 3759 return (-1); 3760 else 3761 mask[chunk] |= (1 << piece); 3762 3763 return (0); 3764 } 3765 3766 int 3767 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3768 { 3769 uint32_t chunk, piece; 3770 3771 chunk = bit >> 5; 3772 piece = bit % (sizeof(uint32_t) * 8); 3773 3774 if ((mask[chunk] & (1 << piece)) == 0) 3775 return (-1); 3776 else 3777 mask[chunk] &= ~(1 << piece); 3778 3779 return (0); 3780 } 3781 3782 int 3783 ctl_is_set(uint32_t *mask, uint32_t bit) 3784 { 3785 uint32_t chunk, piece; 3786 3787 chunk = bit >> 5; 3788 piece = bit % (sizeof(uint32_t) * 8); 3789 3790 if ((mask[chunk] & (1 << piece)) == 0) 3791 return (0); 3792 else 3793 return (1); 3794 } 3795 3796 static uint64_t 3797 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3798 { 3799 uint64_t *t; 3800 3801 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3802 if (t == NULL) 3803 return (0); 3804 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3805 } 3806 3807 static void 3808 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3809 { 3810 uint64_t *t; 3811 3812 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3813 if (t == NULL) 3814 return; 3815 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3816 } 3817 3818 static void 3819 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3820 { 3821 uint64_t *p; 3822 u_int i; 3823 3824 i = residx/CTL_MAX_INIT_PER_PORT; 3825 if (lun->pr_keys[i] != NULL) 3826 return; 3827 mtx_unlock(&lun->lun_lock); 3828 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3829 M_WAITOK | M_ZERO); 3830 mtx_lock(&lun->lun_lock); 3831 if (lun->pr_keys[i] == NULL) 3832 lun->pr_keys[i] = p; 3833 else 3834 free(p, M_CTL); 3835 } 3836 3837 static void 3838 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3839 { 3840 uint64_t *t; 3841 3842 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3843 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3844 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3845 } 3846 3847 /* 3848 * ctl_softc, pool_name, total_ctl_io are passed in. 3849 * npool is passed out. 3850 */ 3851 int 3852 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3853 uint32_t total_ctl_io, void **npool) 3854 { 3855 struct ctl_io_pool *pool; 3856 3857 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3858 M_NOWAIT | M_ZERO); 3859 if (pool == NULL) 3860 return (ENOMEM); 3861 3862 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3863 pool->ctl_softc = ctl_softc; 3864 #ifdef IO_POOLS 3865 pool->zone = uma_zsecond_create(pool->name, NULL, 3866 NULL, NULL, NULL, ctl_softc->io_zone); 3867 /* uma_prealloc(pool->zone, total_ctl_io); */ 3868 #else 3869 pool->zone = ctl_softc->io_zone; 3870 #endif 3871 3872 *npool = pool; 3873 return (0); 3874 } 3875 3876 void 3877 ctl_pool_free(struct ctl_io_pool *pool) 3878 { 3879 3880 if (pool == NULL) 3881 return; 3882 3883 #ifdef IO_POOLS 3884 uma_zdestroy(pool->zone); 3885 #endif 3886 free(pool, M_CTL); 3887 } 3888 3889 union ctl_io * 3890 ctl_alloc_io(void *pool_ref) 3891 { 3892 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3893 union ctl_io *io; 3894 3895 io = uma_zalloc(pool->zone, M_WAITOK); 3896 if (io != NULL) { 3897 io->io_hdr.pool = pool_ref; 3898 CTL_SOFTC(io) = pool->ctl_softc; 3899 TAILQ_INIT(&io->io_hdr.blocked_queue); 3900 } 3901 return (io); 3902 } 3903 3904 union ctl_io * 3905 ctl_alloc_io_nowait(void *pool_ref) 3906 { 3907 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3908 union ctl_io *io; 3909 3910 io = uma_zalloc(pool->zone, M_NOWAIT); 3911 if (io != NULL) { 3912 io->io_hdr.pool = pool_ref; 3913 CTL_SOFTC(io) = pool->ctl_softc; 3914 TAILQ_INIT(&io->io_hdr.blocked_queue); 3915 } 3916 return (io); 3917 } 3918 3919 void 3920 ctl_free_io(union ctl_io *io) 3921 { 3922 struct ctl_io_pool *pool; 3923 3924 if (io == NULL) 3925 return; 3926 3927 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3928 uma_zfree(pool->zone, io); 3929 } 3930 3931 void 3932 ctl_zero_io(union ctl_io *io) 3933 { 3934 struct ctl_io_pool *pool; 3935 3936 if (io == NULL) 3937 return; 3938 3939 /* 3940 * May need to preserve linked list pointers at some point too. 3941 */ 3942 pool = io->io_hdr.pool; 3943 memset(io, 0, sizeof(*io)); 3944 io->io_hdr.pool = pool; 3945 CTL_SOFTC(io) = pool->ctl_softc; 3946 TAILQ_INIT(&io->io_hdr.blocked_queue); 3947 } 3948 3949 int 3950 ctl_expand_number(const char *buf, uint64_t *num) 3951 { 3952 char *endptr; 3953 uint64_t number; 3954 unsigned shift; 3955 3956 number = strtoq(buf, &endptr, 0); 3957 3958 switch (tolower((unsigned char)*endptr)) { 3959 case 'e': 3960 shift = 60; 3961 break; 3962 case 'p': 3963 shift = 50; 3964 break; 3965 case 't': 3966 shift = 40; 3967 break; 3968 case 'g': 3969 shift = 30; 3970 break; 3971 case 'm': 3972 shift = 20; 3973 break; 3974 case 'k': 3975 shift = 10; 3976 break; 3977 case 'b': 3978 case '\0': /* No unit. */ 3979 *num = number; 3980 return (0); 3981 default: 3982 /* Unrecognized unit. */ 3983 return (-1); 3984 } 3985 3986 if ((number << shift) >> shift != number) { 3987 /* Overflow */ 3988 return (-1); 3989 } 3990 *num = number << shift; 3991 return (0); 3992 } 3993 3994 /* 3995 * This routine could be used in the future to load default and/or saved 3996 * mode page parameters for a particuar lun. 3997 */ 3998 static int 3999 ctl_init_page_index(struct ctl_lun *lun) 4000 { 4001 int i, page_code; 4002 struct ctl_page_index *page_index; 4003 const char *value; 4004 uint64_t ival; 4005 4006 memcpy(&lun->mode_pages.index, page_index_template, 4007 sizeof(page_index_template)); 4008 4009 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4010 page_index = &lun->mode_pages.index[i]; 4011 if (lun->be_lun->lun_type == T_DIRECT && 4012 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4013 continue; 4014 if (lun->be_lun->lun_type == T_PROCESSOR && 4015 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4016 continue; 4017 if (lun->be_lun->lun_type == T_CDROM && 4018 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4019 continue; 4020 4021 page_code = page_index->page_code & SMPH_PC_MASK; 4022 switch (page_code) { 4023 case SMS_RW_ERROR_RECOVERY_PAGE: { 4024 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4025 ("subpage %#x for page %#x is incorrect!", 4026 page_index->subpage, page_code)); 4027 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4028 &rw_er_page_default, 4029 sizeof(rw_er_page_default)); 4030 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4031 &rw_er_page_changeable, 4032 sizeof(rw_er_page_changeable)); 4033 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4034 &rw_er_page_default, 4035 sizeof(rw_er_page_default)); 4036 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4037 &rw_er_page_default, 4038 sizeof(rw_er_page_default)); 4039 page_index->page_data = 4040 (uint8_t *)lun->mode_pages.rw_er_page; 4041 break; 4042 } 4043 case SMS_FORMAT_DEVICE_PAGE: { 4044 struct scsi_format_page *format_page; 4045 4046 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4047 ("subpage %#x for page %#x is incorrect!", 4048 page_index->subpage, page_code)); 4049 4050 /* 4051 * Sectors per track are set above. Bytes per 4052 * sector need to be set here on a per-LUN basis. 4053 */ 4054 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4055 &format_page_default, 4056 sizeof(format_page_default)); 4057 memcpy(&lun->mode_pages.format_page[ 4058 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4059 sizeof(format_page_changeable)); 4060 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4061 &format_page_default, 4062 sizeof(format_page_default)); 4063 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4064 &format_page_default, 4065 sizeof(format_page_default)); 4066 4067 format_page = &lun->mode_pages.format_page[ 4068 CTL_PAGE_CURRENT]; 4069 scsi_ulto2b(lun->be_lun->blocksize, 4070 format_page->bytes_per_sector); 4071 4072 format_page = &lun->mode_pages.format_page[ 4073 CTL_PAGE_DEFAULT]; 4074 scsi_ulto2b(lun->be_lun->blocksize, 4075 format_page->bytes_per_sector); 4076 4077 format_page = &lun->mode_pages.format_page[ 4078 CTL_PAGE_SAVED]; 4079 scsi_ulto2b(lun->be_lun->blocksize, 4080 format_page->bytes_per_sector); 4081 4082 page_index->page_data = 4083 (uint8_t *)lun->mode_pages.format_page; 4084 break; 4085 } 4086 case SMS_RIGID_DISK_PAGE: { 4087 struct scsi_rigid_disk_page *rigid_disk_page; 4088 uint32_t sectors_per_cylinder; 4089 uint64_t cylinders; 4090 #ifndef __XSCALE__ 4091 int shift; 4092 #endif /* !__XSCALE__ */ 4093 4094 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4095 ("subpage %#x for page %#x is incorrect!", 4096 page_index->subpage, page_code)); 4097 4098 /* 4099 * Rotation rate and sectors per track are set 4100 * above. We calculate the cylinders here based on 4101 * capacity. Due to the number of heads and 4102 * sectors per track we're using, smaller arrays 4103 * may turn out to have 0 cylinders. Linux and 4104 * FreeBSD don't pay attention to these mode pages 4105 * to figure out capacity, but Solaris does. It 4106 * seems to deal with 0 cylinders just fine, and 4107 * works out a fake geometry based on the capacity. 4108 */ 4109 memcpy(&lun->mode_pages.rigid_disk_page[ 4110 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4111 sizeof(rigid_disk_page_default)); 4112 memcpy(&lun->mode_pages.rigid_disk_page[ 4113 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4114 sizeof(rigid_disk_page_changeable)); 4115 4116 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4117 CTL_DEFAULT_HEADS; 4118 4119 /* 4120 * The divide method here will be more accurate, 4121 * probably, but results in floating point being 4122 * used in the kernel on i386 (__udivdi3()). On the 4123 * XScale, though, __udivdi3() is implemented in 4124 * software. 4125 * 4126 * The shift method for cylinder calculation is 4127 * accurate if sectors_per_cylinder is a power of 4128 * 2. Otherwise it might be slightly off -- you 4129 * might have a bit of a truncation problem. 4130 */ 4131 #ifdef __XSCALE__ 4132 cylinders = (lun->be_lun->maxlba + 1) / 4133 sectors_per_cylinder; 4134 #else 4135 for (shift = 31; shift > 0; shift--) { 4136 if (sectors_per_cylinder & (1 << shift)) 4137 break; 4138 } 4139 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4140 #endif 4141 4142 /* 4143 * We've basically got 3 bytes, or 24 bits for the 4144 * cylinder size in the mode page. If we're over, 4145 * just round down to 2^24. 4146 */ 4147 if (cylinders > 0xffffff) 4148 cylinders = 0xffffff; 4149 4150 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4151 CTL_PAGE_DEFAULT]; 4152 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4153 4154 if ((value = dnvlist_get_string(lun->be_lun->options, 4155 "rpm", NULL)) != NULL) { 4156 scsi_ulto2b(strtol(value, NULL, 0), 4157 rigid_disk_page->rotation_rate); 4158 } 4159 4160 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4161 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4162 sizeof(rigid_disk_page_default)); 4163 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4164 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4165 sizeof(rigid_disk_page_default)); 4166 4167 page_index->page_data = 4168 (uint8_t *)lun->mode_pages.rigid_disk_page; 4169 break; 4170 } 4171 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4172 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4173 ("subpage %#x for page %#x is incorrect!", 4174 page_index->subpage, page_code)); 4175 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4176 &verify_er_page_default, 4177 sizeof(verify_er_page_default)); 4178 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4179 &verify_er_page_changeable, 4180 sizeof(verify_er_page_changeable)); 4181 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4182 &verify_er_page_default, 4183 sizeof(verify_er_page_default)); 4184 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4185 &verify_er_page_default, 4186 sizeof(verify_er_page_default)); 4187 page_index->page_data = 4188 (uint8_t *)lun->mode_pages.verify_er_page; 4189 break; 4190 } 4191 case SMS_CACHING_PAGE: { 4192 struct scsi_caching_page *caching_page; 4193 4194 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4195 ("subpage %#x for page %#x is incorrect!", 4196 page_index->subpage, page_code)); 4197 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4198 &caching_page_default, 4199 sizeof(caching_page_default)); 4200 memcpy(&lun->mode_pages.caching_page[ 4201 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4202 sizeof(caching_page_changeable)); 4203 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4204 &caching_page_default, 4205 sizeof(caching_page_default)); 4206 caching_page = &lun->mode_pages.caching_page[ 4207 CTL_PAGE_SAVED]; 4208 value = dnvlist_get_string(lun->be_lun->options, 4209 "writecache", NULL); 4210 if (value != NULL && strcmp(value, "off") == 0) 4211 caching_page->flags1 &= ~SCP_WCE; 4212 value = dnvlist_get_string(lun->be_lun->options, 4213 "readcache", NULL); 4214 if (value != NULL && strcmp(value, "off") == 0) 4215 caching_page->flags1 |= SCP_RCD; 4216 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4217 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4218 sizeof(caching_page_default)); 4219 page_index->page_data = 4220 (uint8_t *)lun->mode_pages.caching_page; 4221 break; 4222 } 4223 case SMS_CONTROL_MODE_PAGE: { 4224 switch (page_index->subpage) { 4225 case SMS_SUBPAGE_PAGE_0: { 4226 struct scsi_control_page *control_page; 4227 4228 memcpy(&lun->mode_pages.control_page[ 4229 CTL_PAGE_DEFAULT], 4230 &control_page_default, 4231 sizeof(control_page_default)); 4232 memcpy(&lun->mode_pages.control_page[ 4233 CTL_PAGE_CHANGEABLE], 4234 &control_page_changeable, 4235 sizeof(control_page_changeable)); 4236 memcpy(&lun->mode_pages.control_page[ 4237 CTL_PAGE_SAVED], 4238 &control_page_default, 4239 sizeof(control_page_default)); 4240 control_page = &lun->mode_pages.control_page[ 4241 CTL_PAGE_SAVED]; 4242 value = dnvlist_get_string(lun->be_lun->options, 4243 "reordering", NULL); 4244 if (value != NULL && 4245 strcmp(value, "unrestricted") == 0) { 4246 control_page->queue_flags &= 4247 ~SCP_QUEUE_ALG_MASK; 4248 control_page->queue_flags |= 4249 SCP_QUEUE_ALG_UNRESTRICTED; 4250 } 4251 memcpy(&lun->mode_pages.control_page[ 4252 CTL_PAGE_CURRENT], 4253 &lun->mode_pages.control_page[ 4254 CTL_PAGE_SAVED], 4255 sizeof(control_page_default)); 4256 page_index->page_data = 4257 (uint8_t *)lun->mode_pages.control_page; 4258 break; 4259 } 4260 case 0x01: 4261 memcpy(&lun->mode_pages.control_ext_page[ 4262 CTL_PAGE_DEFAULT], 4263 &control_ext_page_default, 4264 sizeof(control_ext_page_default)); 4265 memcpy(&lun->mode_pages.control_ext_page[ 4266 CTL_PAGE_CHANGEABLE], 4267 &control_ext_page_changeable, 4268 sizeof(control_ext_page_changeable)); 4269 memcpy(&lun->mode_pages.control_ext_page[ 4270 CTL_PAGE_SAVED], 4271 &control_ext_page_default, 4272 sizeof(control_ext_page_default)); 4273 memcpy(&lun->mode_pages.control_ext_page[ 4274 CTL_PAGE_CURRENT], 4275 &lun->mode_pages.control_ext_page[ 4276 CTL_PAGE_SAVED], 4277 sizeof(control_ext_page_default)); 4278 page_index->page_data = 4279 (uint8_t *)lun->mode_pages.control_ext_page; 4280 break; 4281 default: 4282 panic("subpage %#x for page %#x is incorrect!", 4283 page_index->subpage, page_code); 4284 } 4285 break; 4286 } 4287 case SMS_INFO_EXCEPTIONS_PAGE: { 4288 switch (page_index->subpage) { 4289 case SMS_SUBPAGE_PAGE_0: 4290 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4291 &ie_page_default, 4292 sizeof(ie_page_default)); 4293 memcpy(&lun->mode_pages.ie_page[ 4294 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4295 sizeof(ie_page_changeable)); 4296 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4297 &ie_page_default, 4298 sizeof(ie_page_default)); 4299 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4300 &ie_page_default, 4301 sizeof(ie_page_default)); 4302 page_index->page_data = 4303 (uint8_t *)lun->mode_pages.ie_page; 4304 break; 4305 case 0x02: { 4306 struct ctl_logical_block_provisioning_page *page; 4307 4308 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4309 &lbp_page_default, 4310 sizeof(lbp_page_default)); 4311 memcpy(&lun->mode_pages.lbp_page[ 4312 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4313 sizeof(lbp_page_changeable)); 4314 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4315 &lbp_page_default, 4316 sizeof(lbp_page_default)); 4317 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4318 value = dnvlist_get_string(lun->be_lun->options, 4319 "avail-threshold", NULL); 4320 if (value != NULL && 4321 ctl_expand_number(value, &ival) == 0) { 4322 page->descr[0].flags |= SLBPPD_ENABLED | 4323 SLBPPD_ARMING_DEC; 4324 if (lun->be_lun->blocksize) 4325 ival /= lun->be_lun->blocksize; 4326 else 4327 ival /= 512; 4328 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4329 page->descr[0].count); 4330 } 4331 value = dnvlist_get_string(lun->be_lun->options, 4332 "used-threshold", NULL); 4333 if (value != NULL && 4334 ctl_expand_number(value, &ival) == 0) { 4335 page->descr[1].flags |= SLBPPD_ENABLED | 4336 SLBPPD_ARMING_INC; 4337 if (lun->be_lun->blocksize) 4338 ival /= lun->be_lun->blocksize; 4339 else 4340 ival /= 512; 4341 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4342 page->descr[1].count); 4343 } 4344 value = dnvlist_get_string(lun->be_lun->options, 4345 "pool-avail-threshold", NULL); 4346 if (value != NULL && 4347 ctl_expand_number(value, &ival) == 0) { 4348 page->descr[2].flags |= SLBPPD_ENABLED | 4349 SLBPPD_ARMING_DEC; 4350 if (lun->be_lun->blocksize) 4351 ival /= lun->be_lun->blocksize; 4352 else 4353 ival /= 512; 4354 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4355 page->descr[2].count); 4356 } 4357 value = dnvlist_get_string(lun->be_lun->options, 4358 "pool-used-threshold", NULL); 4359 if (value != NULL && 4360 ctl_expand_number(value, &ival) == 0) { 4361 page->descr[3].flags |= SLBPPD_ENABLED | 4362 SLBPPD_ARMING_INC; 4363 if (lun->be_lun->blocksize) 4364 ival /= lun->be_lun->blocksize; 4365 else 4366 ival /= 512; 4367 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4368 page->descr[3].count); 4369 } 4370 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4371 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4372 sizeof(lbp_page_default)); 4373 page_index->page_data = 4374 (uint8_t *)lun->mode_pages.lbp_page; 4375 break; 4376 } 4377 default: 4378 panic("subpage %#x for page %#x is incorrect!", 4379 page_index->subpage, page_code); 4380 } 4381 break; 4382 } 4383 case SMS_CDDVD_CAPS_PAGE:{ 4384 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4385 ("subpage %#x for page %#x is incorrect!", 4386 page_index->subpage, page_code)); 4387 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4388 &cddvd_page_default, 4389 sizeof(cddvd_page_default)); 4390 memcpy(&lun->mode_pages.cddvd_page[ 4391 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4392 sizeof(cddvd_page_changeable)); 4393 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4394 &cddvd_page_default, 4395 sizeof(cddvd_page_default)); 4396 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4397 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4398 sizeof(cddvd_page_default)); 4399 page_index->page_data = 4400 (uint8_t *)lun->mode_pages.cddvd_page; 4401 break; 4402 } 4403 default: 4404 panic("invalid page code value %#x", page_code); 4405 } 4406 } 4407 4408 return (CTL_RETVAL_COMPLETE); 4409 } 4410 4411 static int 4412 ctl_init_log_page_index(struct ctl_lun *lun) 4413 { 4414 struct ctl_page_index *page_index; 4415 int i, j, k, prev; 4416 4417 memcpy(&lun->log_pages.index, log_page_index_template, 4418 sizeof(log_page_index_template)); 4419 4420 prev = -1; 4421 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4422 page_index = &lun->log_pages.index[i]; 4423 if (lun->be_lun->lun_type == T_DIRECT && 4424 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4425 continue; 4426 if (lun->be_lun->lun_type == T_PROCESSOR && 4427 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4428 continue; 4429 if (lun->be_lun->lun_type == T_CDROM && 4430 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4431 continue; 4432 4433 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4434 lun->backend->lun_attr == NULL) 4435 continue; 4436 4437 if (page_index->page_code != prev) { 4438 lun->log_pages.pages_page[j] = page_index->page_code; 4439 prev = page_index->page_code; 4440 j++; 4441 } 4442 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4443 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4444 k++; 4445 } 4446 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4447 lun->log_pages.index[0].page_len = j; 4448 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4449 lun->log_pages.index[1].page_len = k * 2; 4450 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page; 4451 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page); 4452 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0]; 4453 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS; 4454 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page; 4455 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page); 4456 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page; 4457 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page); 4458 4459 return (CTL_RETVAL_COMPLETE); 4460 } 4461 4462 static int 4463 hex2bin(const char *str, uint8_t *buf, int buf_size) 4464 { 4465 int i; 4466 u_char c; 4467 4468 memset(buf, 0, buf_size); 4469 while (isspace(str[0])) 4470 str++; 4471 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4472 str += 2; 4473 buf_size *= 2; 4474 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4475 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4476 str++; 4477 c = str[i]; 4478 if (isdigit(c)) 4479 c -= '0'; 4480 else if (isalpha(c)) 4481 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4482 else 4483 break; 4484 if (c >= 16) 4485 break; 4486 if ((i & 1) == 0) 4487 buf[i / 2] |= (c << 4); 4488 else 4489 buf[i / 2] |= c; 4490 } 4491 return ((i + 1) / 2); 4492 } 4493 4494 /* 4495 * Add LUN. 4496 * 4497 * Returns 0 for success, non-zero (errno) for failure. 4498 */ 4499 int 4500 ctl_add_lun(struct ctl_be_lun *be_lun) 4501 { 4502 struct ctl_softc *ctl_softc = control_softc; 4503 struct ctl_lun *nlun, *lun; 4504 struct scsi_vpd_id_descriptor *desc; 4505 struct scsi_vpd_id_t10 *t10id; 4506 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4507 int lun_number; 4508 int devidlen, idlen1, idlen2 = 0, len; 4509 4510 /* 4511 * We support only Direct Access, CD-ROM or Processor LUN types. 4512 */ 4513 switch (be_lun->lun_type) { 4514 case T_DIRECT: 4515 case T_PROCESSOR: 4516 case T_CDROM: 4517 break; 4518 case T_SEQUENTIAL: 4519 case T_CHANGER: 4520 default: 4521 return (EINVAL); 4522 } 4523 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO); 4524 4525 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4526 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4527 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4528 M_DEVBUF, M_WAITOK | M_ZERO); 4529 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4530 M_DEVBUF, M_WAITOK | M_ZERO); 4531 4532 /* Generate LUN ID. */ 4533 devidlen = max(CTL_DEVID_MIN_LEN, 4534 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4535 idlen1 = sizeof(*t10id) + devidlen; 4536 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4537 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4538 if (scsiname != NULL) { 4539 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4540 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4541 } 4542 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4543 if (eui != NULL) { 4544 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4545 } 4546 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4547 if (naa != NULL) { 4548 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4549 } 4550 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4551 if (uuid != NULL) { 4552 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4553 } 4554 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4555 M_CTL, M_WAITOK | M_ZERO); 4556 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4557 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4558 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4559 desc->length = idlen1; 4560 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4561 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4562 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4563 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4564 } else { 4565 strncpy(t10id->vendor, vendor, 4566 min(sizeof(t10id->vendor), strlen(vendor))); 4567 } 4568 strncpy((char *)t10id->vendor_spec_id, 4569 (char *)be_lun->device_id, devidlen); 4570 if (scsiname != NULL) { 4571 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4572 desc->length); 4573 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4574 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4575 SVPD_ID_TYPE_SCSI_NAME; 4576 desc->length = idlen2; 4577 strlcpy(desc->identifier, scsiname, idlen2); 4578 } 4579 if (eui != NULL) { 4580 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4581 desc->length); 4582 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4583 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4584 SVPD_ID_TYPE_EUI64; 4585 desc->length = hex2bin(eui, desc->identifier, 16); 4586 desc->length = desc->length > 12 ? 16 : 4587 (desc->length > 8 ? 12 : 8); 4588 len -= 16 - desc->length; 4589 } 4590 if (naa != NULL) { 4591 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4592 desc->length); 4593 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4594 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4595 SVPD_ID_TYPE_NAA; 4596 desc->length = hex2bin(naa, desc->identifier, 16); 4597 desc->length = desc->length > 8 ? 16 : 8; 4598 len -= 16 - desc->length; 4599 } 4600 if (uuid != NULL) { 4601 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4602 desc->length); 4603 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4604 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4605 SVPD_ID_TYPE_UUID; 4606 desc->identifier[0] = 0x10; 4607 hex2bin(uuid, &desc->identifier[2], 16); 4608 desc->length = 18; 4609 } 4610 lun->lun_devid->len = len; 4611 4612 mtx_lock(&ctl_softc->ctl_lock); 4613 /* 4614 * See if the caller requested a particular LUN number. If so, see 4615 * if it is available. Otherwise, allocate the first available LUN. 4616 */ 4617 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4618 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4619 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4620 mtx_unlock(&ctl_softc->ctl_lock); 4621 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4622 printf("ctl: requested LUN ID %d is higher " 4623 "than ctl_max_luns - 1 (%d)\n", 4624 be_lun->req_lun_id, ctl_max_luns - 1); 4625 } else { 4626 /* 4627 * XXX KDM return an error, or just assign 4628 * another LUN ID in this case?? 4629 */ 4630 printf("ctl: requested LUN ID %d is already " 4631 "in use\n", be_lun->req_lun_id); 4632 } 4633 fail: 4634 free(lun->lun_devid, M_CTL); 4635 free(lun, M_CTL); 4636 return (ENOSPC); 4637 } 4638 lun_number = be_lun->req_lun_id; 4639 } else { 4640 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4641 if (lun_number == -1) { 4642 mtx_unlock(&ctl_softc->ctl_lock); 4643 printf("ctl: can't allocate LUN, out of LUNs\n"); 4644 goto fail; 4645 } 4646 } 4647 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4648 mtx_unlock(&ctl_softc->ctl_lock); 4649 4650 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4651 lun->lun = lun_number; 4652 lun->be_lun = be_lun; 4653 /* 4654 * The processor LUN is always enabled. Disk LUNs come on line 4655 * disabled, and must be enabled by the backend. 4656 */ 4657 lun->flags |= CTL_LUN_DISABLED; 4658 lun->backend = be_lun->be; 4659 be_lun->ctl_lun = lun; 4660 be_lun->lun_id = lun_number; 4661 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4662 lun->flags |= CTL_LUN_EJECTED; 4663 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4664 lun->flags |= CTL_LUN_NO_MEDIA; 4665 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4666 lun->flags |= CTL_LUN_STOPPED; 4667 4668 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4669 lun->flags |= CTL_LUN_PRIMARY_SC; 4670 4671 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4672 if (value != NULL) { 4673 if (strcmp(value, "on") == 0) 4674 lun->flags |= CTL_LUN_REMOVABLE; 4675 } else if (be_lun->lun_type == T_CDROM) 4676 lun->flags |= CTL_LUN_REMOVABLE; 4677 4678 lun->ctl_softc = ctl_softc; 4679 #ifdef CTL_TIME_IO 4680 lun->last_busy = getsbinuptime(); 4681 #endif 4682 LIST_INIT(&lun->ooa_queue); 4683 STAILQ_INIT(&lun->error_list); 4684 lun->ie_reported = 1; 4685 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4686 ctl_tpc_lun_init(lun); 4687 if (lun->flags & CTL_LUN_REMOVABLE) { 4688 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4689 M_CTL, M_WAITOK); 4690 } 4691 4692 /* 4693 * Initialize the mode and log page index. 4694 */ 4695 ctl_init_page_index(lun); 4696 ctl_init_log_page_index(lun); 4697 4698 /* Setup statistics gathering */ 4699 lun->stats.item = lun_number; 4700 4701 /* 4702 * Now, before we insert this lun on the lun list, set the lun 4703 * inventory changed UA for all other luns. 4704 */ 4705 mtx_lock(&ctl_softc->ctl_lock); 4706 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4707 mtx_lock(&nlun->lun_lock); 4708 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4709 mtx_unlock(&nlun->lun_lock); 4710 } 4711 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4712 ctl_softc->ctl_luns[lun_number] = lun; 4713 ctl_softc->num_luns++; 4714 mtx_unlock(&ctl_softc->ctl_lock); 4715 4716 /* 4717 * We successfully added the LUN, attempt to enable it. 4718 */ 4719 if (ctl_enable_lun(lun) != 0) { 4720 printf("%s: ctl_enable_lun() failed!\n", __func__); 4721 mtx_lock(&ctl_softc->ctl_lock); 4722 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links); 4723 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number); 4724 ctl_softc->ctl_luns[lun_number] = NULL; 4725 ctl_softc->num_luns--; 4726 mtx_unlock(&ctl_softc->ctl_lock); 4727 free(lun->lun_devid, M_CTL); 4728 free(lun, M_CTL); 4729 return (EIO); 4730 } 4731 4732 return (0); 4733 } 4734 4735 /* 4736 * Free LUN that has no active requests. 4737 */ 4738 static int 4739 ctl_free_lun(struct ctl_lun *lun) 4740 { 4741 struct ctl_softc *softc = lun->ctl_softc; 4742 struct ctl_lun *nlun; 4743 int i; 4744 4745 KASSERT(LIST_EMPTY(&lun->ooa_queue), 4746 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4747 4748 mtx_lock(&softc->ctl_lock); 4749 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4750 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4751 softc->ctl_luns[lun->lun] = NULL; 4752 softc->num_luns--; 4753 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4754 mtx_lock(&nlun->lun_lock); 4755 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4756 mtx_unlock(&nlun->lun_lock); 4757 } 4758 mtx_unlock(&softc->ctl_lock); 4759 4760 /* 4761 * Tell the backend to free resources, if this LUN has a backend. 4762 */ 4763 lun->be_lun->lun_shutdown(lun->be_lun); 4764 4765 lun->ie_reportcnt = UINT32_MAX; 4766 callout_drain(&lun->ie_callout); 4767 ctl_tpc_lun_shutdown(lun); 4768 mtx_destroy(&lun->lun_lock); 4769 free(lun->lun_devid, M_CTL); 4770 for (i = 0; i < ctl_max_ports; i++) 4771 free(lun->pending_ua[i], M_CTL); 4772 free(lun->pending_ua, M_DEVBUF); 4773 for (i = 0; i < ctl_max_ports; i++) 4774 free(lun->pr_keys[i], M_CTL); 4775 free(lun->pr_keys, M_DEVBUF); 4776 free(lun->write_buffer, M_CTL); 4777 free(lun->prevent, M_CTL); 4778 free(lun, M_CTL); 4779 4780 return (0); 4781 } 4782 4783 static int 4784 ctl_enable_lun(struct ctl_lun *lun) 4785 { 4786 struct ctl_softc *softc; 4787 struct ctl_port *port, *nport; 4788 int retval; 4789 4790 softc = lun->ctl_softc; 4791 4792 mtx_lock(&softc->ctl_lock); 4793 mtx_lock(&lun->lun_lock); 4794 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0, 4795 ("%s: LUN not disabled", __func__)); 4796 lun->flags &= ~CTL_LUN_DISABLED; 4797 mtx_unlock(&lun->lun_lock); 4798 4799 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4800 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4801 port->lun_map != NULL || port->lun_enable == NULL) 4802 continue; 4803 4804 /* 4805 * Drop the lock while we call the FETD's enable routine. 4806 * This can lead to a callback into CTL (at least in the 4807 * case of the internal initiator frontend. 4808 */ 4809 mtx_unlock(&softc->ctl_lock); 4810 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4811 mtx_lock(&softc->ctl_lock); 4812 if (retval != 0) { 4813 printf("%s: FETD %s port %d returned error " 4814 "%d for lun_enable on lun %jd\n", 4815 __func__, port->port_name, port->targ_port, 4816 retval, (intmax_t)lun->lun); 4817 } 4818 } 4819 4820 mtx_unlock(&softc->ctl_lock); 4821 ctl_isc_announce_lun(lun); 4822 4823 return (0); 4824 } 4825 4826 static int 4827 ctl_disable_lun(struct ctl_lun *lun) 4828 { 4829 struct ctl_softc *softc; 4830 struct ctl_port *port; 4831 int retval; 4832 4833 softc = lun->ctl_softc; 4834 4835 mtx_lock(&softc->ctl_lock); 4836 mtx_lock(&lun->lun_lock); 4837 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0, 4838 ("%s: LUN not enabled", __func__)); 4839 lun->flags |= CTL_LUN_DISABLED; 4840 mtx_unlock(&lun->lun_lock); 4841 4842 STAILQ_FOREACH(port, &softc->port_list, links) { 4843 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4844 port->lun_map != NULL || port->lun_disable == NULL) 4845 continue; 4846 4847 /* 4848 * Drop the lock before we call the frontend's disable 4849 * routine, to avoid lock order reversals. 4850 * 4851 * XXX KDM what happens if the frontend list changes while 4852 * we're traversing it? It's unlikely, but should be handled. 4853 */ 4854 mtx_unlock(&softc->ctl_lock); 4855 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4856 mtx_lock(&softc->ctl_lock); 4857 if (retval != 0) { 4858 printf("%s: FETD %s port %d returned error " 4859 "%d for lun_disable on lun %jd\n", 4860 __func__, port->port_name, port->targ_port, 4861 retval, (intmax_t)lun->lun); 4862 } 4863 } 4864 4865 mtx_unlock(&softc->ctl_lock); 4866 ctl_isc_announce_lun(lun); 4867 4868 return (0); 4869 } 4870 4871 int 4872 ctl_start_lun(struct ctl_be_lun *be_lun) 4873 { 4874 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4875 4876 mtx_lock(&lun->lun_lock); 4877 lun->flags &= ~CTL_LUN_STOPPED; 4878 mtx_unlock(&lun->lun_lock); 4879 return (0); 4880 } 4881 4882 int 4883 ctl_stop_lun(struct ctl_be_lun *be_lun) 4884 { 4885 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4886 4887 mtx_lock(&lun->lun_lock); 4888 lun->flags |= CTL_LUN_STOPPED; 4889 mtx_unlock(&lun->lun_lock); 4890 return (0); 4891 } 4892 4893 int 4894 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4895 { 4896 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4897 4898 mtx_lock(&lun->lun_lock); 4899 lun->flags |= CTL_LUN_NO_MEDIA; 4900 mtx_unlock(&lun->lun_lock); 4901 return (0); 4902 } 4903 4904 int 4905 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4906 { 4907 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4908 union ctl_ha_msg msg; 4909 4910 mtx_lock(&lun->lun_lock); 4911 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4912 if (lun->flags & CTL_LUN_REMOVABLE) 4913 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4914 mtx_unlock(&lun->lun_lock); 4915 if ((lun->flags & CTL_LUN_REMOVABLE) && 4916 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4917 bzero(&msg.ua, sizeof(msg.ua)); 4918 msg.hdr.msg_type = CTL_MSG_UA; 4919 msg.hdr.nexus.initid = -1; 4920 msg.hdr.nexus.targ_port = -1; 4921 msg.hdr.nexus.targ_lun = lun->lun; 4922 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4923 msg.ua.ua_all = 1; 4924 msg.ua.ua_set = 1; 4925 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4926 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4927 M_WAITOK); 4928 } 4929 return (0); 4930 } 4931 4932 int 4933 ctl_lun_ejected(struct ctl_be_lun *be_lun) 4934 { 4935 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4936 4937 mtx_lock(&lun->lun_lock); 4938 lun->flags |= CTL_LUN_EJECTED; 4939 mtx_unlock(&lun->lun_lock); 4940 return (0); 4941 } 4942 4943 int 4944 ctl_lun_primary(struct ctl_be_lun *be_lun) 4945 { 4946 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4947 4948 mtx_lock(&lun->lun_lock); 4949 lun->flags |= CTL_LUN_PRIMARY_SC; 4950 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4951 mtx_unlock(&lun->lun_lock); 4952 ctl_isc_announce_lun(lun); 4953 return (0); 4954 } 4955 4956 int 4957 ctl_lun_secondary(struct ctl_be_lun *be_lun) 4958 { 4959 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4960 4961 mtx_lock(&lun->lun_lock); 4962 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4963 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4964 mtx_unlock(&lun->lun_lock); 4965 ctl_isc_announce_lun(lun); 4966 return (0); 4967 } 4968 4969 /* 4970 * Remove LUN. If there are active requests, wait for completion. 4971 * 4972 * Returns 0 for success, non-zero (errno) for failure. 4973 * Completion is reported to backed via the lun_shutdown() method. 4974 */ 4975 int 4976 ctl_remove_lun(struct ctl_be_lun *be_lun) 4977 { 4978 struct ctl_lun *lun; 4979 4980 lun = (struct ctl_lun *)be_lun->ctl_lun; 4981 4982 ctl_disable_lun(lun); 4983 4984 mtx_lock(&lun->lun_lock); 4985 lun->flags |= CTL_LUN_INVALID; 4986 4987 /* 4988 * If there is nothing in the OOA queue, go ahead and free the LUN. 4989 * If we have something in the OOA queue, we'll free it when the 4990 * last I/O completes. 4991 */ 4992 if (LIST_EMPTY(&lun->ooa_queue)) { 4993 mtx_unlock(&lun->lun_lock); 4994 ctl_free_lun(lun); 4995 } else 4996 mtx_unlock(&lun->lun_lock); 4997 4998 return (0); 4999 } 5000 5001 void 5002 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5003 { 5004 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5005 union ctl_ha_msg msg; 5006 5007 mtx_lock(&lun->lun_lock); 5008 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5009 mtx_unlock(&lun->lun_lock); 5010 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5011 /* Send msg to other side. */ 5012 bzero(&msg.ua, sizeof(msg.ua)); 5013 msg.hdr.msg_type = CTL_MSG_UA; 5014 msg.hdr.nexus.initid = -1; 5015 msg.hdr.nexus.targ_port = -1; 5016 msg.hdr.nexus.targ_lun = lun->lun; 5017 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5018 msg.ua.ua_all = 1; 5019 msg.ua.ua_set = 1; 5020 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5021 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5022 M_WAITOK); 5023 } 5024 } 5025 5026 /* 5027 * Backend "memory move is complete" callback for requests that never 5028 * make it down to say RAIDCore's configuration code. 5029 */ 5030 int 5031 ctl_config_move_done(union ctl_io *io, bool samethr) 5032 { 5033 int retval; 5034 5035 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5036 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5037 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 5038 5039 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5040 ctl_data_print(io); 5041 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5042 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5043 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5044 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5045 /* 5046 * XXX KDM just assuming a single pointer here, and not a 5047 * S/G list. If we start using S/G lists for config data, 5048 * we'll need to know how to clean them up here as well. 5049 */ 5050 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5051 free(io->scsiio.kern_data_ptr, M_CTL); 5052 ctl_done(io); 5053 retval = CTL_RETVAL_COMPLETE; 5054 } else { 5055 /* 5056 * XXX KDM now we need to continue data movement. Some 5057 * options: 5058 * - call ctl_scsiio() again? We don't do this for data 5059 * writes, because for those at least we know ahead of 5060 * time where the write will go and how long it is. For 5061 * config writes, though, that information is largely 5062 * contained within the write itself, thus we need to 5063 * parse out the data again. 5064 * 5065 * - Call some other function once the data is in? 5066 */ 5067 5068 /* 5069 * XXX KDM call ctl_scsiio() again for now, and check flag 5070 * bits to see whether we're allocated or not. 5071 */ 5072 retval = ctl_scsiio(&io->scsiio); 5073 } 5074 return (retval); 5075 } 5076 5077 /* 5078 * This gets called by a backend driver when it is done with a 5079 * data_submit method. 5080 */ 5081 void 5082 ctl_data_submit_done(union ctl_io *io) 5083 { 5084 /* 5085 * If the IO_CONT flag is set, we need to call the supplied 5086 * function to continue processing the I/O, instead of completing 5087 * the I/O just yet. 5088 * 5089 * If there is an error, though, we don't want to keep processing. 5090 * Instead, just send status back to the initiator. 5091 */ 5092 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5093 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5094 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5095 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5096 io->scsiio.io_cont(io); 5097 return; 5098 } 5099 ctl_done(io); 5100 } 5101 5102 /* 5103 * This gets called by a backend driver when it is done with a 5104 * configuration write. 5105 */ 5106 void 5107 ctl_config_write_done(union ctl_io *io) 5108 { 5109 uint8_t *buf; 5110 5111 /* 5112 * If the IO_CONT flag is set, we need to call the supplied 5113 * function to continue processing the I/O, instead of completing 5114 * the I/O just yet. 5115 * 5116 * If there is an error, though, we don't want to keep processing. 5117 * Instead, just send status back to the initiator. 5118 */ 5119 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5120 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5121 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5122 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5123 io->scsiio.io_cont(io); 5124 return; 5125 } 5126 /* 5127 * Since a configuration write can be done for commands that actually 5128 * have data allocated, like write buffer, and commands that have 5129 * no data, like start/stop unit, we need to check here. 5130 */ 5131 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5132 buf = io->scsiio.kern_data_ptr; 5133 else 5134 buf = NULL; 5135 ctl_done(io); 5136 if (buf) 5137 free(buf, M_CTL); 5138 } 5139 5140 void 5141 ctl_config_read_done(union ctl_io *io) 5142 { 5143 uint8_t *buf; 5144 5145 /* 5146 * If there is some error -- we are done, skip data transfer. 5147 */ 5148 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5149 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5150 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5151 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5152 buf = io->scsiio.kern_data_ptr; 5153 else 5154 buf = NULL; 5155 ctl_done(io); 5156 if (buf) 5157 free(buf, M_CTL); 5158 return; 5159 } 5160 5161 /* 5162 * If the IO_CONT flag is set, we need to call the supplied 5163 * function to continue processing the I/O, instead of completing 5164 * the I/O just yet. 5165 */ 5166 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5167 io->scsiio.io_cont(io); 5168 return; 5169 } 5170 5171 ctl_datamove(io); 5172 } 5173 5174 /* 5175 * SCSI release command. 5176 */ 5177 int 5178 ctl_scsi_release(struct ctl_scsiio *ctsio) 5179 { 5180 struct ctl_lun *lun = CTL_LUN(ctsio); 5181 uint32_t residx; 5182 5183 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5184 5185 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5186 5187 /* 5188 * XXX KDM right now, we only support LUN reservation. We don't 5189 * support 3rd party reservations, or extent reservations, which 5190 * might actually need the parameter list. If we've gotten this 5191 * far, we've got a LUN reservation. Anything else got kicked out 5192 * above. So, according to SPC, ignore the length. 5193 */ 5194 5195 mtx_lock(&lun->lun_lock); 5196 5197 /* 5198 * According to SPC, it is not an error for an intiator to attempt 5199 * to release a reservation on a LUN that isn't reserved, or that 5200 * is reserved by another initiator. The reservation can only be 5201 * released, though, by the initiator who made it or by one of 5202 * several reset type events. 5203 */ 5204 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5205 lun->flags &= ~CTL_LUN_RESERVED; 5206 5207 mtx_unlock(&lun->lun_lock); 5208 5209 ctl_set_success(ctsio); 5210 ctl_done((union ctl_io *)ctsio); 5211 return (CTL_RETVAL_COMPLETE); 5212 } 5213 5214 int 5215 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5216 { 5217 struct ctl_lun *lun = CTL_LUN(ctsio); 5218 uint32_t residx; 5219 5220 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5221 5222 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5223 5224 /* 5225 * XXX KDM right now, we only support LUN reservation. We don't 5226 * support 3rd party reservations, or extent reservations, which 5227 * might actually need the parameter list. If we've gotten this 5228 * far, we've got a LUN reservation. Anything else got kicked out 5229 * above. So, according to SPC, ignore the length. 5230 */ 5231 5232 mtx_lock(&lun->lun_lock); 5233 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5234 ctl_set_reservation_conflict(ctsio); 5235 goto bailout; 5236 } 5237 5238 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5239 if (lun->flags & CTL_LUN_PR_RESERVED) { 5240 ctl_set_success(ctsio); 5241 goto bailout; 5242 } 5243 5244 lun->flags |= CTL_LUN_RESERVED; 5245 lun->res_idx = residx; 5246 ctl_set_success(ctsio); 5247 5248 bailout: 5249 mtx_unlock(&lun->lun_lock); 5250 ctl_done((union ctl_io *)ctsio); 5251 return (CTL_RETVAL_COMPLETE); 5252 } 5253 5254 int 5255 ctl_start_stop(struct ctl_scsiio *ctsio) 5256 { 5257 struct ctl_lun *lun = CTL_LUN(ctsio); 5258 struct scsi_start_stop_unit *cdb; 5259 int retval; 5260 5261 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5262 5263 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5264 5265 if ((cdb->how & SSS_PC_MASK) == 0) { 5266 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5267 (cdb->how & SSS_START) == 0) { 5268 uint32_t residx; 5269 5270 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5271 if (ctl_get_prkey(lun, residx) == 0 || 5272 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5273 ctl_set_reservation_conflict(ctsio); 5274 ctl_done((union ctl_io *)ctsio); 5275 return (CTL_RETVAL_COMPLETE); 5276 } 5277 } 5278 5279 if ((cdb->how & SSS_LOEJ) && 5280 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5281 ctl_set_invalid_field(ctsio, 5282 /*sks_valid*/ 1, 5283 /*command*/ 1, 5284 /*field*/ 4, 5285 /*bit_valid*/ 1, 5286 /*bit*/ 1); 5287 ctl_done((union ctl_io *)ctsio); 5288 return (CTL_RETVAL_COMPLETE); 5289 } 5290 5291 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5292 lun->prevent_count > 0) { 5293 /* "Medium removal prevented" */ 5294 ctl_set_sense(ctsio, /*current_error*/ 1, 5295 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5296 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5297 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5298 ctl_done((union ctl_io *)ctsio); 5299 return (CTL_RETVAL_COMPLETE); 5300 } 5301 } 5302 5303 retval = lun->backend->config_write((union ctl_io *)ctsio); 5304 return (retval); 5305 } 5306 5307 int 5308 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5309 { 5310 struct ctl_lun *lun = CTL_LUN(ctsio); 5311 struct scsi_prevent *cdb; 5312 int retval; 5313 uint32_t initidx; 5314 5315 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5316 5317 cdb = (struct scsi_prevent *)ctsio->cdb; 5318 5319 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5320 ctl_set_invalid_opcode(ctsio); 5321 ctl_done((union ctl_io *)ctsio); 5322 return (CTL_RETVAL_COMPLETE); 5323 } 5324 5325 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5326 mtx_lock(&lun->lun_lock); 5327 if ((cdb->how & PR_PREVENT) && 5328 ctl_is_set(lun->prevent, initidx) == 0) { 5329 ctl_set_mask(lun->prevent, initidx); 5330 lun->prevent_count++; 5331 } else if ((cdb->how & PR_PREVENT) == 0 && 5332 ctl_is_set(lun->prevent, initidx)) { 5333 ctl_clear_mask(lun->prevent, initidx); 5334 lun->prevent_count--; 5335 } 5336 mtx_unlock(&lun->lun_lock); 5337 retval = lun->backend->config_write((union ctl_io *)ctsio); 5338 return (retval); 5339 } 5340 5341 /* 5342 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5343 * we don't really do anything with the LBA and length fields if the user 5344 * passes them in. Instead we'll just flush out the cache for the entire 5345 * LUN. 5346 */ 5347 int 5348 ctl_sync_cache(struct ctl_scsiio *ctsio) 5349 { 5350 struct ctl_lun *lun = CTL_LUN(ctsio); 5351 struct ctl_lba_len_flags *lbalen; 5352 uint64_t starting_lba; 5353 uint32_t block_count; 5354 int retval; 5355 uint8_t byte2; 5356 5357 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5358 5359 retval = 0; 5360 5361 switch (ctsio->cdb[0]) { 5362 case SYNCHRONIZE_CACHE: { 5363 struct scsi_sync_cache *cdb; 5364 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5365 5366 starting_lba = scsi_4btoul(cdb->begin_lba); 5367 block_count = scsi_2btoul(cdb->lb_count); 5368 byte2 = cdb->byte2; 5369 break; 5370 } 5371 case SYNCHRONIZE_CACHE_16: { 5372 struct scsi_sync_cache_16 *cdb; 5373 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5374 5375 starting_lba = scsi_8btou64(cdb->begin_lba); 5376 block_count = scsi_4btoul(cdb->lb_count); 5377 byte2 = cdb->byte2; 5378 break; 5379 } 5380 default: 5381 ctl_set_invalid_opcode(ctsio); 5382 ctl_done((union ctl_io *)ctsio); 5383 goto bailout; 5384 break; /* NOTREACHED */ 5385 } 5386 5387 /* 5388 * We check the LBA and length, but don't do anything with them. 5389 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5390 * get flushed. This check will just help satisfy anyone who wants 5391 * to see an error for an out of range LBA. 5392 */ 5393 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5394 ctl_set_lba_out_of_range(ctsio, 5395 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5396 ctl_done((union ctl_io *)ctsio); 5397 goto bailout; 5398 } 5399 5400 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5401 lbalen->lba = starting_lba; 5402 lbalen->len = block_count; 5403 lbalen->flags = byte2; 5404 retval = lun->backend->config_write((union ctl_io *)ctsio); 5405 5406 bailout: 5407 return (retval); 5408 } 5409 5410 int 5411 ctl_format(struct ctl_scsiio *ctsio) 5412 { 5413 struct scsi_format *cdb; 5414 int length, defect_list_len; 5415 5416 CTL_DEBUG_PRINT(("ctl_format\n")); 5417 5418 cdb = (struct scsi_format *)ctsio->cdb; 5419 5420 length = 0; 5421 if (cdb->byte2 & SF_FMTDATA) { 5422 if (cdb->byte2 & SF_LONGLIST) 5423 length = sizeof(struct scsi_format_header_long); 5424 else 5425 length = sizeof(struct scsi_format_header_short); 5426 } 5427 5428 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5429 && (length > 0)) { 5430 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5431 ctsio->kern_data_len = length; 5432 ctsio->kern_total_len = length; 5433 ctsio->kern_rel_offset = 0; 5434 ctsio->kern_sg_entries = 0; 5435 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5436 ctsio->be_move_done = ctl_config_move_done; 5437 ctl_datamove((union ctl_io *)ctsio); 5438 5439 return (CTL_RETVAL_COMPLETE); 5440 } 5441 5442 defect_list_len = 0; 5443 5444 if (cdb->byte2 & SF_FMTDATA) { 5445 if (cdb->byte2 & SF_LONGLIST) { 5446 struct scsi_format_header_long *header; 5447 5448 header = (struct scsi_format_header_long *) 5449 ctsio->kern_data_ptr; 5450 5451 defect_list_len = scsi_4btoul(header->defect_list_len); 5452 if (defect_list_len != 0) { 5453 ctl_set_invalid_field(ctsio, 5454 /*sks_valid*/ 1, 5455 /*command*/ 0, 5456 /*field*/ 2, 5457 /*bit_valid*/ 0, 5458 /*bit*/ 0); 5459 goto bailout; 5460 } 5461 } else { 5462 struct scsi_format_header_short *header; 5463 5464 header = (struct scsi_format_header_short *) 5465 ctsio->kern_data_ptr; 5466 5467 defect_list_len = scsi_2btoul(header->defect_list_len); 5468 if (defect_list_len != 0) { 5469 ctl_set_invalid_field(ctsio, 5470 /*sks_valid*/ 1, 5471 /*command*/ 0, 5472 /*field*/ 2, 5473 /*bit_valid*/ 0, 5474 /*bit*/ 0); 5475 goto bailout; 5476 } 5477 } 5478 } 5479 5480 ctl_set_success(ctsio); 5481 bailout: 5482 5483 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5484 free(ctsio->kern_data_ptr, M_CTL); 5485 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5486 } 5487 5488 ctl_done((union ctl_io *)ctsio); 5489 return (CTL_RETVAL_COMPLETE); 5490 } 5491 5492 int 5493 ctl_read_buffer(struct ctl_scsiio *ctsio) 5494 { 5495 struct ctl_lun *lun = CTL_LUN(ctsio); 5496 uint64_t buffer_offset; 5497 uint32_t len; 5498 uint8_t byte2; 5499 static uint8_t descr[4]; 5500 static uint8_t echo_descr[4] = { 0 }; 5501 5502 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5503 5504 switch (ctsio->cdb[0]) { 5505 case READ_BUFFER: { 5506 struct scsi_read_buffer *cdb; 5507 5508 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5509 buffer_offset = scsi_3btoul(cdb->offset); 5510 len = scsi_3btoul(cdb->length); 5511 byte2 = cdb->byte2; 5512 break; 5513 } 5514 case READ_BUFFER_16: { 5515 struct scsi_read_buffer_16 *cdb; 5516 5517 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5518 buffer_offset = scsi_8btou64(cdb->offset); 5519 len = scsi_4btoul(cdb->length); 5520 byte2 = cdb->byte2; 5521 break; 5522 } 5523 default: /* This shouldn't happen. */ 5524 ctl_set_invalid_opcode(ctsio); 5525 ctl_done((union ctl_io *)ctsio); 5526 return (CTL_RETVAL_COMPLETE); 5527 } 5528 5529 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5530 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5531 ctl_set_invalid_field(ctsio, 5532 /*sks_valid*/ 1, 5533 /*command*/ 1, 5534 /*field*/ 6, 5535 /*bit_valid*/ 0, 5536 /*bit*/ 0); 5537 ctl_done((union ctl_io *)ctsio); 5538 return (CTL_RETVAL_COMPLETE); 5539 } 5540 5541 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5542 descr[0] = 0; 5543 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5544 ctsio->kern_data_ptr = descr; 5545 len = min(len, sizeof(descr)); 5546 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5547 ctsio->kern_data_ptr = echo_descr; 5548 len = min(len, sizeof(echo_descr)); 5549 } else { 5550 if (lun->write_buffer == NULL) { 5551 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5552 M_CTL, M_WAITOK); 5553 } 5554 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5555 } 5556 ctsio->kern_data_len = len; 5557 ctsio->kern_total_len = len; 5558 ctsio->kern_rel_offset = 0; 5559 ctsio->kern_sg_entries = 0; 5560 ctl_set_success(ctsio); 5561 ctsio->be_move_done = ctl_config_move_done; 5562 ctl_datamove((union ctl_io *)ctsio); 5563 return (CTL_RETVAL_COMPLETE); 5564 } 5565 5566 int 5567 ctl_write_buffer(struct ctl_scsiio *ctsio) 5568 { 5569 struct ctl_lun *lun = CTL_LUN(ctsio); 5570 struct scsi_write_buffer *cdb; 5571 int buffer_offset, len; 5572 5573 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5574 5575 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5576 5577 len = scsi_3btoul(cdb->length); 5578 buffer_offset = scsi_3btoul(cdb->offset); 5579 5580 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5581 ctl_set_invalid_field(ctsio, 5582 /*sks_valid*/ 1, 5583 /*command*/ 1, 5584 /*field*/ 6, 5585 /*bit_valid*/ 0, 5586 /*bit*/ 0); 5587 ctl_done((union ctl_io *)ctsio); 5588 return (CTL_RETVAL_COMPLETE); 5589 } 5590 5591 /* 5592 * If we've got a kernel request that hasn't been malloced yet, 5593 * malloc it and tell the caller the data buffer is here. 5594 */ 5595 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5596 if (lun->write_buffer == NULL) { 5597 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5598 M_CTL, M_WAITOK); 5599 } 5600 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5601 ctsio->kern_data_len = len; 5602 ctsio->kern_total_len = len; 5603 ctsio->kern_rel_offset = 0; 5604 ctsio->kern_sg_entries = 0; 5605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5606 ctsio->be_move_done = ctl_config_move_done; 5607 ctl_datamove((union ctl_io *)ctsio); 5608 5609 return (CTL_RETVAL_COMPLETE); 5610 } 5611 5612 ctl_set_success(ctsio); 5613 ctl_done((union ctl_io *)ctsio); 5614 return (CTL_RETVAL_COMPLETE); 5615 } 5616 5617 static int 5618 ctl_write_same_cont(union ctl_io *io) 5619 { 5620 struct ctl_lun *lun = CTL_LUN(io); 5621 struct ctl_scsiio *ctsio; 5622 struct ctl_lba_len_flags *lbalen; 5623 int retval; 5624 5625 ctsio = &io->scsiio; 5626 ctsio->io_hdr.status = CTL_STATUS_NONE; 5627 lbalen = (struct ctl_lba_len_flags *) 5628 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5629 lbalen->lba += lbalen->len; 5630 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) { 5631 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 5632 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba; 5633 } 5634 5635 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n")); 5636 retval = lun->backend->config_write((union ctl_io *)ctsio); 5637 return (retval); 5638 } 5639 5640 int 5641 ctl_write_same(struct ctl_scsiio *ctsio) 5642 { 5643 struct ctl_lun *lun = CTL_LUN(ctsio); 5644 struct ctl_lba_len_flags *lbalen; 5645 const char *val; 5646 uint64_t lba, ival; 5647 uint32_t num_blocks; 5648 int len, retval; 5649 uint8_t byte2; 5650 5651 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5652 5653 switch (ctsio->cdb[0]) { 5654 case WRITE_SAME_10: { 5655 struct scsi_write_same_10 *cdb; 5656 5657 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5658 5659 lba = scsi_4btoul(cdb->addr); 5660 num_blocks = scsi_2btoul(cdb->length); 5661 byte2 = cdb->byte2; 5662 break; 5663 } 5664 case WRITE_SAME_16: { 5665 struct scsi_write_same_16 *cdb; 5666 5667 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5668 5669 lba = scsi_8btou64(cdb->addr); 5670 num_blocks = scsi_4btoul(cdb->length); 5671 byte2 = cdb->byte2; 5672 break; 5673 } 5674 default: 5675 /* 5676 * We got a command we don't support. This shouldn't 5677 * happen, commands should be filtered out above us. 5678 */ 5679 ctl_set_invalid_opcode(ctsio); 5680 ctl_done((union ctl_io *)ctsio); 5681 5682 return (CTL_RETVAL_COMPLETE); 5683 break; /* NOTREACHED */ 5684 } 5685 5686 /* ANCHOR flag can be used only together with UNMAP */ 5687 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5688 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5689 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5690 ctl_done((union ctl_io *)ctsio); 5691 return (CTL_RETVAL_COMPLETE); 5692 } 5693 5694 /* 5695 * The first check is to make sure we're in bounds, the second 5696 * check is to catch wrap-around problems. If the lba + num blocks 5697 * is less than the lba, then we've wrapped around and the block 5698 * range is invalid anyway. 5699 */ 5700 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5701 || ((lba + num_blocks) < lba)) { 5702 ctl_set_lba_out_of_range(ctsio, 5703 MAX(lba, lun->be_lun->maxlba + 1)); 5704 ctl_done((union ctl_io *)ctsio); 5705 return (CTL_RETVAL_COMPLETE); 5706 } 5707 5708 /* Zero number of blocks means "to the last logical block" */ 5709 if (num_blocks == 0) { 5710 ival = UINT64_MAX; 5711 val = dnvlist_get_string(lun->be_lun->options, 5712 "write_same_max_lba", NULL); 5713 if (val != NULL) 5714 ctl_expand_number(val, &ival); 5715 if ((lun->be_lun->maxlba + 1) - lba > ival) { 5716 ctl_set_invalid_field(ctsio, 5717 /*sks_valid*/ 1, /*command*/ 1, 5718 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10, 5719 /*bit_valid*/ 0, /*bit*/ 0); 5720 ctl_done((union ctl_io *)ctsio); 5721 return (CTL_RETVAL_COMPLETE); 5722 } 5723 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5724 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 5725 ctsio->io_cont = ctl_write_same_cont; 5726 num_blocks = 1 << 31; 5727 } else 5728 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5729 } 5730 5731 len = lun->be_lun->blocksize; 5732 5733 /* 5734 * If we've got a kernel request that hasn't been malloced yet, 5735 * malloc it and tell the caller the data buffer is here. 5736 */ 5737 if ((byte2 & SWS_NDOB) == 0 && 5738 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5739 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5740 ctsio->kern_data_len = len; 5741 ctsio->kern_total_len = len; 5742 ctsio->kern_rel_offset = 0; 5743 ctsio->kern_sg_entries = 0; 5744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5745 ctsio->be_move_done = ctl_config_move_done; 5746 ctl_datamove((union ctl_io *)ctsio); 5747 5748 return (CTL_RETVAL_COMPLETE); 5749 } 5750 5751 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5752 lbalen->lba = lba; 5753 lbalen->len = num_blocks; 5754 lbalen->flags = byte2; 5755 retval = lun->backend->config_write((union ctl_io *)ctsio); 5756 5757 return (retval); 5758 } 5759 5760 int 5761 ctl_unmap(struct ctl_scsiio *ctsio) 5762 { 5763 struct ctl_lun *lun = CTL_LUN(ctsio); 5764 struct scsi_unmap *cdb; 5765 struct ctl_ptr_len_flags *ptrlen; 5766 struct scsi_unmap_header *hdr; 5767 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5768 uint64_t lba; 5769 uint32_t num_blocks; 5770 int len, retval; 5771 uint8_t byte2; 5772 5773 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5774 5775 cdb = (struct scsi_unmap *)ctsio->cdb; 5776 len = scsi_2btoul(cdb->length); 5777 byte2 = cdb->byte2; 5778 5779 /* 5780 * If we've got a kernel request that hasn't been malloced yet, 5781 * malloc it and tell the caller the data buffer is here. 5782 */ 5783 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5784 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5785 ctsio->kern_data_len = len; 5786 ctsio->kern_total_len = len; 5787 ctsio->kern_rel_offset = 0; 5788 ctsio->kern_sg_entries = 0; 5789 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5790 ctsio->be_move_done = ctl_config_move_done; 5791 ctl_datamove((union ctl_io *)ctsio); 5792 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 5796 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5797 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5798 if (len < sizeof (*hdr) || 5799 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5800 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5801 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5802 ctl_set_invalid_field(ctsio, 5803 /*sks_valid*/ 0, 5804 /*command*/ 0, 5805 /*field*/ 0, 5806 /*bit_valid*/ 0, 5807 /*bit*/ 0); 5808 goto done; 5809 } 5810 len = scsi_2btoul(hdr->desc_length); 5811 buf = (struct scsi_unmap_desc *)(hdr + 1); 5812 end = buf + len / sizeof(*buf); 5813 5814 endnz = buf; 5815 for (range = buf; range < end; range++) { 5816 lba = scsi_8btou64(range->lba); 5817 num_blocks = scsi_4btoul(range->length); 5818 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5819 || ((lba + num_blocks) < lba)) { 5820 ctl_set_lba_out_of_range(ctsio, 5821 MAX(lba, lun->be_lun->maxlba + 1)); 5822 ctl_done((union ctl_io *)ctsio); 5823 return (CTL_RETVAL_COMPLETE); 5824 } 5825 if (num_blocks != 0) 5826 endnz = range + 1; 5827 } 5828 5829 /* 5830 * Block backend can not handle zero last range. 5831 * Filter it out and return if there is nothing left. 5832 */ 5833 len = (uint8_t *)endnz - (uint8_t *)buf; 5834 if (len == 0) { 5835 ctl_set_success(ctsio); 5836 goto done; 5837 } 5838 5839 mtx_lock(&lun->lun_lock); 5840 ptrlen = (struct ctl_ptr_len_flags *) 5841 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5842 ptrlen->ptr = (void *)buf; 5843 ptrlen->len = len; 5844 ptrlen->flags = byte2; 5845 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE); 5846 mtx_unlock(&lun->lun_lock); 5847 5848 retval = lun->backend->config_write((union ctl_io *)ctsio); 5849 return (retval); 5850 5851 done: 5852 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5853 free(ctsio->kern_data_ptr, M_CTL); 5854 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5855 } 5856 ctl_done((union ctl_io *)ctsio); 5857 return (CTL_RETVAL_COMPLETE); 5858 } 5859 5860 int 5861 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5862 struct ctl_page_index *page_index, uint8_t *page_ptr) 5863 { 5864 struct ctl_lun *lun = CTL_LUN(ctsio); 5865 uint8_t *current_cp; 5866 int set_ua; 5867 uint32_t initidx; 5868 5869 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5870 set_ua = 0; 5871 5872 current_cp = (page_index->page_data + (page_index->page_len * 5873 CTL_PAGE_CURRENT)); 5874 5875 mtx_lock(&lun->lun_lock); 5876 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5877 memcpy(current_cp, page_ptr, page_index->page_len); 5878 set_ua = 1; 5879 } 5880 if (set_ua != 0) 5881 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5882 mtx_unlock(&lun->lun_lock); 5883 if (set_ua) { 5884 ctl_isc_announce_mode(lun, 5885 ctl_get_initindex(&ctsio->io_hdr.nexus), 5886 page_index->page_code, page_index->subpage); 5887 } 5888 return (CTL_RETVAL_COMPLETE); 5889 } 5890 5891 static void 5892 ctl_ie_timer(void *arg) 5893 { 5894 struct ctl_lun *lun = arg; 5895 uint64_t t; 5896 5897 if (lun->ie_asc == 0) 5898 return; 5899 5900 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5901 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5902 else 5903 lun->ie_reported = 0; 5904 5905 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5906 lun->ie_reportcnt++; 5907 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5908 if (t == 0 || t == UINT32_MAX) 5909 t = 3000; /* 5 min */ 5910 callout_schedule(&lun->ie_callout, t * hz / 10); 5911 } 5912 } 5913 5914 int 5915 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5916 struct ctl_page_index *page_index, uint8_t *page_ptr) 5917 { 5918 struct ctl_lun *lun = CTL_LUN(ctsio); 5919 struct scsi_info_exceptions_page *pg; 5920 uint64_t t; 5921 5922 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5923 5924 pg = (struct scsi_info_exceptions_page *)page_ptr; 5925 mtx_lock(&lun->lun_lock); 5926 if (pg->info_flags & SIEP_FLAGS_TEST) { 5927 lun->ie_asc = 0x5d; 5928 lun->ie_ascq = 0xff; 5929 if (pg->mrie == SIEP_MRIE_UA) { 5930 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5931 lun->ie_reported = 1; 5932 } else { 5933 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5934 lun->ie_reported = -1; 5935 } 5936 lun->ie_reportcnt = 1; 5937 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5938 lun->ie_reportcnt++; 5939 t = scsi_4btoul(pg->interval_timer); 5940 if (t == 0 || t == UINT32_MAX) 5941 t = 3000; /* 5 min */ 5942 callout_reset(&lun->ie_callout, t * hz / 10, 5943 ctl_ie_timer, lun); 5944 } 5945 } else { 5946 lun->ie_asc = 0; 5947 lun->ie_ascq = 0; 5948 lun->ie_reported = 1; 5949 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5950 lun->ie_reportcnt = UINT32_MAX; 5951 callout_stop(&lun->ie_callout); 5952 } 5953 mtx_unlock(&lun->lun_lock); 5954 return (CTL_RETVAL_COMPLETE); 5955 } 5956 5957 static int 5958 ctl_do_mode_select(union ctl_io *io) 5959 { 5960 struct ctl_lun *lun = CTL_LUN(io); 5961 struct scsi_mode_page_header *page_header; 5962 struct ctl_page_index *page_index; 5963 struct ctl_scsiio *ctsio; 5964 int page_len, page_len_offset, page_len_size; 5965 union ctl_modepage_info *modepage_info; 5966 uint16_t *len_left, *len_used; 5967 int retval, i; 5968 5969 ctsio = &io->scsiio; 5970 page_index = NULL; 5971 page_len = 0; 5972 5973 modepage_info = (union ctl_modepage_info *) 5974 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5975 len_left = &modepage_info->header.len_left; 5976 len_used = &modepage_info->header.len_used; 5977 5978 do_next_page: 5979 5980 page_header = (struct scsi_mode_page_header *) 5981 (ctsio->kern_data_ptr + *len_used); 5982 5983 if (*len_left == 0) { 5984 free(ctsio->kern_data_ptr, M_CTL); 5985 ctl_set_success(ctsio); 5986 ctl_done((union ctl_io *)ctsio); 5987 return (CTL_RETVAL_COMPLETE); 5988 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5989 free(ctsio->kern_data_ptr, M_CTL); 5990 ctl_set_param_len_error(ctsio); 5991 ctl_done((union ctl_io *)ctsio); 5992 return (CTL_RETVAL_COMPLETE); 5993 5994 } else if ((page_header->page_code & SMPH_SPF) 5995 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5996 free(ctsio->kern_data_ptr, M_CTL); 5997 ctl_set_param_len_error(ctsio); 5998 ctl_done((union ctl_io *)ctsio); 5999 return (CTL_RETVAL_COMPLETE); 6000 } 6001 6002 /* 6003 * XXX KDM should we do something with the block descriptor? 6004 */ 6005 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6006 page_index = &lun->mode_pages.index[i]; 6007 if (lun->be_lun->lun_type == T_DIRECT && 6008 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6009 continue; 6010 if (lun->be_lun->lun_type == T_PROCESSOR && 6011 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6012 continue; 6013 if (lun->be_lun->lun_type == T_CDROM && 6014 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6015 continue; 6016 6017 if ((page_index->page_code & SMPH_PC_MASK) != 6018 (page_header->page_code & SMPH_PC_MASK)) 6019 continue; 6020 6021 /* 6022 * If neither page has a subpage code, then we've got a 6023 * match. 6024 */ 6025 if (((page_index->page_code & SMPH_SPF) == 0) 6026 && ((page_header->page_code & SMPH_SPF) == 0)) { 6027 page_len = page_header->page_length; 6028 break; 6029 } 6030 6031 /* 6032 * If both pages have subpages, then the subpage numbers 6033 * have to match. 6034 */ 6035 if ((page_index->page_code & SMPH_SPF) 6036 && (page_header->page_code & SMPH_SPF)) { 6037 struct scsi_mode_page_header_sp *sph; 6038 6039 sph = (struct scsi_mode_page_header_sp *)page_header; 6040 if (page_index->subpage == sph->subpage) { 6041 page_len = scsi_2btoul(sph->page_length); 6042 break; 6043 } 6044 } 6045 } 6046 6047 /* 6048 * If we couldn't find the page, or if we don't have a mode select 6049 * handler for it, send back an error to the user. 6050 */ 6051 if ((i >= CTL_NUM_MODE_PAGES) 6052 || (page_index->select_handler == NULL)) { 6053 ctl_set_invalid_field(ctsio, 6054 /*sks_valid*/ 1, 6055 /*command*/ 0, 6056 /*field*/ *len_used, 6057 /*bit_valid*/ 0, 6058 /*bit*/ 0); 6059 free(ctsio->kern_data_ptr, M_CTL); 6060 ctl_done((union ctl_io *)ctsio); 6061 return (CTL_RETVAL_COMPLETE); 6062 } 6063 6064 if (page_index->page_code & SMPH_SPF) { 6065 page_len_offset = 2; 6066 page_len_size = 2; 6067 } else { 6068 page_len_size = 1; 6069 page_len_offset = 1; 6070 } 6071 6072 /* 6073 * If the length the initiator gives us isn't the one we specify in 6074 * the mode page header, or if they didn't specify enough data in 6075 * the CDB to avoid truncating this page, kick out the request. 6076 */ 6077 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6078 ctl_set_invalid_field(ctsio, 6079 /*sks_valid*/ 1, 6080 /*command*/ 0, 6081 /*field*/ *len_used + page_len_offset, 6082 /*bit_valid*/ 0, 6083 /*bit*/ 0); 6084 free(ctsio->kern_data_ptr, M_CTL); 6085 ctl_done((union ctl_io *)ctsio); 6086 return (CTL_RETVAL_COMPLETE); 6087 } 6088 if (*len_left < page_index->page_len) { 6089 free(ctsio->kern_data_ptr, M_CTL); 6090 ctl_set_param_len_error(ctsio); 6091 ctl_done((union ctl_io *)ctsio); 6092 return (CTL_RETVAL_COMPLETE); 6093 } 6094 6095 /* 6096 * Run through the mode page, checking to make sure that the bits 6097 * the user changed are actually legal for him to change. 6098 */ 6099 for (i = 0; i < page_index->page_len; i++) { 6100 uint8_t *user_byte, *change_mask, *current_byte; 6101 int bad_bit; 6102 int j; 6103 6104 user_byte = (uint8_t *)page_header + i; 6105 change_mask = page_index->page_data + 6106 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6107 current_byte = page_index->page_data + 6108 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6109 6110 /* 6111 * Check to see whether the user set any bits in this byte 6112 * that he is not allowed to set. 6113 */ 6114 if ((*user_byte & ~(*change_mask)) == 6115 (*current_byte & ~(*change_mask))) 6116 continue; 6117 6118 /* 6119 * Go through bit by bit to determine which one is illegal. 6120 */ 6121 bad_bit = 0; 6122 for (j = 7; j >= 0; j--) { 6123 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6124 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6125 bad_bit = i; 6126 break; 6127 } 6128 } 6129 ctl_set_invalid_field(ctsio, 6130 /*sks_valid*/ 1, 6131 /*command*/ 0, 6132 /*field*/ *len_used + i, 6133 /*bit_valid*/ 1, 6134 /*bit*/ bad_bit); 6135 free(ctsio->kern_data_ptr, M_CTL); 6136 ctl_done((union ctl_io *)ctsio); 6137 return (CTL_RETVAL_COMPLETE); 6138 } 6139 6140 /* 6141 * Decrement these before we call the page handler, since we may 6142 * end up getting called back one way or another before the handler 6143 * returns to this context. 6144 */ 6145 *len_left -= page_index->page_len; 6146 *len_used += page_index->page_len; 6147 6148 retval = page_index->select_handler(ctsio, page_index, 6149 (uint8_t *)page_header); 6150 6151 /* 6152 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6153 * wait until this queued command completes to finish processing 6154 * the mode page. If it returns anything other than 6155 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6156 * already set the sense information, freed the data pointer, and 6157 * completed the io for us. 6158 */ 6159 if (retval != CTL_RETVAL_COMPLETE) 6160 goto bailout_no_done; 6161 6162 /* 6163 * If the initiator sent us more than one page, parse the next one. 6164 */ 6165 if (*len_left > 0) 6166 goto do_next_page; 6167 6168 ctl_set_success(ctsio); 6169 free(ctsio->kern_data_ptr, M_CTL); 6170 ctl_done((union ctl_io *)ctsio); 6171 6172 bailout_no_done: 6173 6174 return (CTL_RETVAL_COMPLETE); 6175 6176 } 6177 6178 int 6179 ctl_mode_select(struct ctl_scsiio *ctsio) 6180 { 6181 struct ctl_lun *lun = CTL_LUN(ctsio); 6182 union ctl_modepage_info *modepage_info; 6183 int bd_len, i, header_size, param_len, rtd; 6184 uint32_t initidx; 6185 6186 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6187 switch (ctsio->cdb[0]) { 6188 case MODE_SELECT_6: { 6189 struct scsi_mode_select_6 *cdb; 6190 6191 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6192 6193 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6194 param_len = cdb->length; 6195 header_size = sizeof(struct scsi_mode_header_6); 6196 break; 6197 } 6198 case MODE_SELECT_10: { 6199 struct scsi_mode_select_10 *cdb; 6200 6201 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6202 6203 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6204 param_len = scsi_2btoul(cdb->length); 6205 header_size = sizeof(struct scsi_mode_header_10); 6206 break; 6207 } 6208 default: 6209 ctl_set_invalid_opcode(ctsio); 6210 ctl_done((union ctl_io *)ctsio); 6211 return (CTL_RETVAL_COMPLETE); 6212 } 6213 6214 if (rtd) { 6215 if (param_len != 0) { 6216 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6217 /*command*/ 1, /*field*/ 0, 6218 /*bit_valid*/ 0, /*bit*/ 0); 6219 ctl_done((union ctl_io *)ctsio); 6220 return (CTL_RETVAL_COMPLETE); 6221 } 6222 6223 /* Revert to defaults. */ 6224 ctl_init_page_index(lun); 6225 mtx_lock(&lun->lun_lock); 6226 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6227 mtx_unlock(&lun->lun_lock); 6228 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6229 ctl_isc_announce_mode(lun, -1, 6230 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6231 lun->mode_pages.index[i].subpage); 6232 } 6233 ctl_set_success(ctsio); 6234 ctl_done((union ctl_io *)ctsio); 6235 return (CTL_RETVAL_COMPLETE); 6236 } 6237 6238 /* 6239 * From SPC-3: 6240 * "A parameter list length of zero indicates that the Data-Out Buffer 6241 * shall be empty. This condition shall not be considered as an error." 6242 */ 6243 if (param_len == 0) { 6244 ctl_set_success(ctsio); 6245 ctl_done((union ctl_io *)ctsio); 6246 return (CTL_RETVAL_COMPLETE); 6247 } 6248 6249 /* 6250 * Since we'll hit this the first time through, prior to 6251 * allocation, we don't need to free a data buffer here. 6252 */ 6253 if (param_len < header_size) { 6254 ctl_set_param_len_error(ctsio); 6255 ctl_done((union ctl_io *)ctsio); 6256 return (CTL_RETVAL_COMPLETE); 6257 } 6258 6259 /* 6260 * Allocate the data buffer and grab the user's data. In theory, 6261 * we shouldn't have to sanity check the parameter list length here 6262 * because the maximum size is 64K. We should be able to malloc 6263 * that much without too many problems. 6264 */ 6265 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6266 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6267 ctsio->kern_data_len = param_len; 6268 ctsio->kern_total_len = param_len; 6269 ctsio->kern_rel_offset = 0; 6270 ctsio->kern_sg_entries = 0; 6271 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6272 ctsio->be_move_done = ctl_config_move_done; 6273 ctl_datamove((union ctl_io *)ctsio); 6274 6275 return (CTL_RETVAL_COMPLETE); 6276 } 6277 6278 switch (ctsio->cdb[0]) { 6279 case MODE_SELECT_6: { 6280 struct scsi_mode_header_6 *mh6; 6281 6282 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6283 bd_len = mh6->blk_desc_len; 6284 break; 6285 } 6286 case MODE_SELECT_10: { 6287 struct scsi_mode_header_10 *mh10; 6288 6289 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6290 bd_len = scsi_2btoul(mh10->blk_desc_len); 6291 break; 6292 } 6293 default: 6294 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6295 } 6296 6297 if (param_len < (header_size + bd_len)) { 6298 free(ctsio->kern_data_ptr, M_CTL); 6299 ctl_set_param_len_error(ctsio); 6300 ctl_done((union ctl_io *)ctsio); 6301 return (CTL_RETVAL_COMPLETE); 6302 } 6303 6304 /* 6305 * Set the IO_CONT flag, so that if this I/O gets passed to 6306 * ctl_config_write_done(), it'll get passed back to 6307 * ctl_do_mode_select() for further processing, or completion if 6308 * we're all done. 6309 */ 6310 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6311 ctsio->io_cont = ctl_do_mode_select; 6312 6313 modepage_info = (union ctl_modepage_info *) 6314 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6315 memset(modepage_info, 0, sizeof(*modepage_info)); 6316 modepage_info->header.len_left = param_len - header_size - bd_len; 6317 modepage_info->header.len_used = header_size + bd_len; 6318 6319 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6320 } 6321 6322 int 6323 ctl_mode_sense(struct ctl_scsiio *ctsio) 6324 { 6325 struct ctl_lun *lun = CTL_LUN(ctsio); 6326 int pc, page_code, llba, subpage; 6327 int alloc_len, page_len, header_len, bd_len, total_len; 6328 void *block_desc; 6329 struct ctl_page_index *page_index; 6330 6331 llba = 0; 6332 6333 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6334 6335 switch (ctsio->cdb[0]) { 6336 case MODE_SENSE_6: { 6337 struct scsi_mode_sense_6 *cdb; 6338 6339 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6340 6341 header_len = sizeof(struct scsi_mode_hdr_6); 6342 if (cdb->byte2 & SMS_DBD) 6343 bd_len = 0; 6344 else 6345 bd_len = sizeof(struct scsi_mode_block_descr); 6346 header_len += bd_len; 6347 6348 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6349 page_code = cdb->page & SMS_PAGE_CODE; 6350 subpage = cdb->subpage; 6351 alloc_len = cdb->length; 6352 break; 6353 } 6354 case MODE_SENSE_10: { 6355 struct scsi_mode_sense_10 *cdb; 6356 6357 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6358 6359 header_len = sizeof(struct scsi_mode_hdr_10); 6360 if (cdb->byte2 & SMS_DBD) { 6361 bd_len = 0; 6362 } else if (lun->be_lun->lun_type == T_DIRECT) { 6363 if (cdb->byte2 & SMS10_LLBAA) { 6364 llba = 1; 6365 bd_len = sizeof(struct scsi_mode_block_descr_dlong); 6366 } else 6367 bd_len = sizeof(struct scsi_mode_block_descr_dshort); 6368 } else 6369 bd_len = sizeof(struct scsi_mode_block_descr); 6370 header_len += bd_len; 6371 6372 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6373 page_code = cdb->page & SMS_PAGE_CODE; 6374 subpage = cdb->subpage; 6375 alloc_len = scsi_2btoul(cdb->length); 6376 break; 6377 } 6378 default: 6379 ctl_set_invalid_opcode(ctsio); 6380 ctl_done((union ctl_io *)ctsio); 6381 return (CTL_RETVAL_COMPLETE); 6382 break; /* NOTREACHED */ 6383 } 6384 6385 /* 6386 * We have to make a first pass through to calculate the size of 6387 * the pages that match the user's query. Then we allocate enough 6388 * memory to hold it, and actually copy the data into the buffer. 6389 */ 6390 switch (page_code) { 6391 case SMS_ALL_PAGES_PAGE: { 6392 u_int i; 6393 6394 page_len = 0; 6395 6396 /* 6397 * At the moment, values other than 0 and 0xff here are 6398 * reserved according to SPC-3. 6399 */ 6400 if ((subpage != SMS_SUBPAGE_PAGE_0) 6401 && (subpage != SMS_SUBPAGE_ALL)) { 6402 ctl_set_invalid_field(ctsio, 6403 /*sks_valid*/ 1, 6404 /*command*/ 1, 6405 /*field*/ 3, 6406 /*bit_valid*/ 0, 6407 /*bit*/ 0); 6408 ctl_done((union ctl_io *)ctsio); 6409 return (CTL_RETVAL_COMPLETE); 6410 } 6411 6412 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6413 page_index = &lun->mode_pages.index[i]; 6414 6415 /* Make sure the page is supported for this dev type */ 6416 if (lun->be_lun->lun_type == T_DIRECT && 6417 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6418 continue; 6419 if (lun->be_lun->lun_type == T_PROCESSOR && 6420 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6421 continue; 6422 if (lun->be_lun->lun_type == T_CDROM && 6423 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6424 continue; 6425 6426 /* 6427 * We don't use this subpage if the user didn't 6428 * request all subpages. 6429 */ 6430 if ((page_index->subpage != 0) 6431 && (subpage == SMS_SUBPAGE_PAGE_0)) 6432 continue; 6433 6434 page_len += page_index->page_len; 6435 } 6436 break; 6437 } 6438 default: { 6439 u_int i; 6440 6441 page_len = 0; 6442 6443 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6444 page_index = &lun->mode_pages.index[i]; 6445 6446 /* Make sure the page is supported for this dev type */ 6447 if (lun->be_lun->lun_type == T_DIRECT && 6448 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6449 continue; 6450 if (lun->be_lun->lun_type == T_PROCESSOR && 6451 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6452 continue; 6453 if (lun->be_lun->lun_type == T_CDROM && 6454 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6455 continue; 6456 6457 /* Look for the right page code */ 6458 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6459 continue; 6460 6461 /* Look for the right subpage or the subpage wildcard*/ 6462 if ((page_index->subpage != subpage) 6463 && (subpage != SMS_SUBPAGE_ALL)) 6464 continue; 6465 6466 page_len += page_index->page_len; 6467 } 6468 6469 if (page_len == 0) { 6470 ctl_set_invalid_field(ctsio, 6471 /*sks_valid*/ 1, 6472 /*command*/ 1, 6473 /*field*/ 2, 6474 /*bit_valid*/ 1, 6475 /*bit*/ 5); 6476 ctl_done((union ctl_io *)ctsio); 6477 return (CTL_RETVAL_COMPLETE); 6478 } 6479 break; 6480 } 6481 } 6482 6483 total_len = header_len + page_len; 6484 6485 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6486 ctsio->kern_sg_entries = 0; 6487 ctsio->kern_rel_offset = 0; 6488 ctsio->kern_data_len = min(total_len, alloc_len); 6489 ctsio->kern_total_len = ctsio->kern_data_len; 6490 6491 switch (ctsio->cdb[0]) { 6492 case MODE_SENSE_6: { 6493 struct scsi_mode_hdr_6 *header; 6494 6495 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6496 6497 header->datalen = MIN(total_len - 1, 254); 6498 if (lun->be_lun->lun_type == T_DIRECT) { 6499 header->dev_specific = 0x10; /* DPOFUA */ 6500 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6501 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6502 header->dev_specific |= 0x80; /* WP */ 6503 } 6504 header->block_descr_len = bd_len; 6505 block_desc = &header[1]; 6506 break; 6507 } 6508 case MODE_SENSE_10: { 6509 struct scsi_mode_hdr_10 *header; 6510 int datalen; 6511 6512 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6513 6514 datalen = MIN(total_len - 2, 65533); 6515 scsi_ulto2b(datalen, header->datalen); 6516 if (lun->be_lun->lun_type == T_DIRECT) { 6517 header->dev_specific = 0x10; /* DPOFUA */ 6518 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6519 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6520 header->dev_specific |= 0x80; /* WP */ 6521 } 6522 if (llba) 6523 header->flags |= SMH_LONGLBA; 6524 scsi_ulto2b(bd_len, header->block_descr_len); 6525 block_desc = &header[1]; 6526 break; 6527 } 6528 default: 6529 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6530 } 6531 6532 /* 6533 * If we've got a disk, use its blocksize in the block 6534 * descriptor. Otherwise, just set it to 0. 6535 */ 6536 if (bd_len > 0) { 6537 if (lun->be_lun->lun_type == T_DIRECT) { 6538 if (llba) { 6539 struct scsi_mode_block_descr_dlong *bd = block_desc; 6540 if (lun->be_lun->maxlba != 0) 6541 scsi_u64to8b(lun->be_lun->maxlba + 1, 6542 bd->num_blocks); 6543 scsi_ulto4b(lun->be_lun->blocksize, 6544 bd->block_len); 6545 } else { 6546 struct scsi_mode_block_descr_dshort *bd = block_desc; 6547 if (lun->be_lun->maxlba != 0) 6548 scsi_ulto4b(MIN(lun->be_lun->maxlba+1, 6549 UINT32_MAX), bd->num_blocks); 6550 scsi_ulto3b(lun->be_lun->blocksize, 6551 bd->block_len); 6552 } 6553 } else { 6554 struct scsi_mode_block_descr *bd = block_desc; 6555 scsi_ulto3b(0, bd->block_len); 6556 } 6557 } 6558 6559 switch (page_code) { 6560 case SMS_ALL_PAGES_PAGE: { 6561 int i, data_used; 6562 6563 data_used = header_len; 6564 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6565 struct ctl_page_index *page_index; 6566 6567 page_index = &lun->mode_pages.index[i]; 6568 if (lun->be_lun->lun_type == T_DIRECT && 6569 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6570 continue; 6571 if (lun->be_lun->lun_type == T_PROCESSOR && 6572 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6573 continue; 6574 if (lun->be_lun->lun_type == T_CDROM && 6575 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6576 continue; 6577 6578 /* 6579 * We don't use this subpage if the user didn't 6580 * request all subpages. We already checked (above) 6581 * to make sure the user only specified a subpage 6582 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6583 */ 6584 if ((page_index->subpage != 0) 6585 && (subpage == SMS_SUBPAGE_PAGE_0)) 6586 continue; 6587 6588 /* 6589 * Call the handler, if it exists, to update the 6590 * page to the latest values. 6591 */ 6592 if (page_index->sense_handler != NULL) 6593 page_index->sense_handler(ctsio, page_index,pc); 6594 6595 memcpy(ctsio->kern_data_ptr + data_used, 6596 page_index->page_data + 6597 (page_index->page_len * pc), 6598 page_index->page_len); 6599 data_used += page_index->page_len; 6600 } 6601 break; 6602 } 6603 default: { 6604 int i, data_used; 6605 6606 data_used = header_len; 6607 6608 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6609 struct ctl_page_index *page_index; 6610 6611 page_index = &lun->mode_pages.index[i]; 6612 6613 /* Look for the right page code */ 6614 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6615 continue; 6616 6617 /* Look for the right subpage or the subpage wildcard*/ 6618 if ((page_index->subpage != subpage) 6619 && (subpage != SMS_SUBPAGE_ALL)) 6620 continue; 6621 6622 /* Make sure the page is supported for this dev type */ 6623 if (lun->be_lun->lun_type == T_DIRECT && 6624 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6625 continue; 6626 if (lun->be_lun->lun_type == T_PROCESSOR && 6627 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6628 continue; 6629 if (lun->be_lun->lun_type == T_CDROM && 6630 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6631 continue; 6632 6633 /* 6634 * Call the handler, if it exists, to update the 6635 * page to the latest values. 6636 */ 6637 if (page_index->sense_handler != NULL) 6638 page_index->sense_handler(ctsio, page_index,pc); 6639 6640 memcpy(ctsio->kern_data_ptr + data_used, 6641 page_index->page_data + 6642 (page_index->page_len * pc), 6643 page_index->page_len); 6644 data_used += page_index->page_len; 6645 } 6646 break; 6647 } 6648 } 6649 6650 ctl_set_success(ctsio); 6651 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6652 ctsio->be_move_done = ctl_config_move_done; 6653 ctl_datamove((union ctl_io *)ctsio); 6654 return (CTL_RETVAL_COMPLETE); 6655 } 6656 6657 int 6658 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio, 6659 struct ctl_page_index *page_index, 6660 int pc) 6661 { 6662 struct ctl_lun *lun = CTL_LUN(ctsio); 6663 struct scsi_log_temperature *data; 6664 const char *value; 6665 6666 data = (struct scsi_log_temperature *)page_index->page_data; 6667 6668 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code); 6669 data->hdr.param_control = SLP_LBIN; 6670 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6671 sizeof(struct scsi_log_param_header); 6672 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6673 NULL)) != NULL) 6674 data->temperature = strtol(value, NULL, 0); 6675 else 6676 data->temperature = 0xff; 6677 data++; 6678 6679 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code); 6680 data->hdr.param_control = SLP_LBIN; 6681 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6682 sizeof(struct scsi_log_param_header); 6683 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature", 6684 NULL)) != NULL) 6685 data->temperature = strtol(value, NULL, 0); 6686 else 6687 data->temperature = 0xff; 6688 return (0); 6689 } 6690 6691 int 6692 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6693 struct ctl_page_index *page_index, 6694 int pc) 6695 { 6696 struct ctl_lun *lun = CTL_LUN(ctsio); 6697 struct scsi_log_param_header *phdr; 6698 uint8_t *data; 6699 uint64_t val; 6700 6701 data = page_index->page_data; 6702 6703 if (lun->backend->lun_attr != NULL && 6704 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail")) 6705 != UINT64_MAX) { 6706 phdr = (struct scsi_log_param_header *)data; 6707 scsi_ulto2b(0x0001, phdr->param_code); 6708 phdr->param_control = SLP_LBIN | SLP_LP; 6709 phdr->param_len = 8; 6710 data = (uint8_t *)(phdr + 1); 6711 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6712 data[4] = 0x02; /* per-pool */ 6713 data += phdr->param_len; 6714 } 6715 6716 if (lun->backend->lun_attr != NULL && 6717 (val = lun->backend->lun_attr(lun->be_lun, "blocksused")) 6718 != UINT64_MAX) { 6719 phdr = (struct scsi_log_param_header *)data; 6720 scsi_ulto2b(0x0002, phdr->param_code); 6721 phdr->param_control = SLP_LBIN | SLP_LP; 6722 phdr->param_len = 8; 6723 data = (uint8_t *)(phdr + 1); 6724 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6725 data[4] = 0x01; /* per-LUN */ 6726 data += phdr->param_len; 6727 } 6728 6729 if (lun->backend->lun_attr != NULL && 6730 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail")) 6731 != UINT64_MAX) { 6732 phdr = (struct scsi_log_param_header *)data; 6733 scsi_ulto2b(0x00f1, phdr->param_code); 6734 phdr->param_control = SLP_LBIN | SLP_LP; 6735 phdr->param_len = 8; 6736 data = (uint8_t *)(phdr + 1); 6737 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6738 data[4] = 0x02; /* per-pool */ 6739 data += phdr->param_len; 6740 } 6741 6742 if (lun->backend->lun_attr != NULL && 6743 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused")) 6744 != UINT64_MAX) { 6745 phdr = (struct scsi_log_param_header *)data; 6746 scsi_ulto2b(0x00f2, phdr->param_code); 6747 phdr->param_control = SLP_LBIN | SLP_LP; 6748 phdr->param_len = 8; 6749 data = (uint8_t *)(phdr + 1); 6750 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6751 data[4] = 0x02; /* per-pool */ 6752 data += phdr->param_len; 6753 } 6754 6755 page_index->page_len = data - page_index->page_data; 6756 return (0); 6757 } 6758 6759 int 6760 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6761 struct ctl_page_index *page_index, 6762 int pc) 6763 { 6764 struct ctl_lun *lun = CTL_LUN(ctsio); 6765 struct stat_page *data; 6766 struct bintime *t; 6767 6768 data = (struct stat_page *)page_index->page_data; 6769 6770 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6771 data->sap.hdr.param_control = SLP_LBIN; 6772 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6773 sizeof(struct scsi_log_param_header); 6774 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6775 data->sap.read_num); 6776 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6777 data->sap.write_num); 6778 if (lun->be_lun->blocksize > 0) { 6779 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6780 lun->be_lun->blocksize, data->sap.recvieved_lba); 6781 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6782 lun->be_lun->blocksize, data->sap.transmitted_lba); 6783 } 6784 t = &lun->stats.time[CTL_STATS_READ]; 6785 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6786 data->sap.read_int); 6787 t = &lun->stats.time[CTL_STATS_WRITE]; 6788 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6789 data->sap.write_int); 6790 scsi_u64to8b(0, data->sap.weighted_num); 6791 scsi_u64to8b(0, data->sap.weighted_int); 6792 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6793 data->it.hdr.param_control = SLP_LBIN; 6794 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6795 sizeof(struct scsi_log_param_header); 6796 #ifdef CTL_TIME_IO 6797 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6798 #endif 6799 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6800 data->it.hdr.param_control = SLP_LBIN; 6801 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6802 sizeof(struct scsi_log_param_header); 6803 scsi_ulto4b(3, data->ti.exponent); 6804 scsi_ulto4b(1, data->ti.integer); 6805 return (0); 6806 } 6807 6808 int 6809 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6810 struct ctl_page_index *page_index, 6811 int pc) 6812 { 6813 struct ctl_lun *lun = CTL_LUN(ctsio); 6814 struct scsi_log_informational_exceptions *data; 6815 const char *value; 6816 6817 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6818 6819 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6820 data->hdr.param_control = SLP_LBIN; 6821 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6822 sizeof(struct scsi_log_param_header); 6823 data->ie_asc = lun->ie_asc; 6824 data->ie_ascq = lun->ie_ascq; 6825 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6826 NULL)) != NULL) 6827 data->temperature = strtol(value, NULL, 0); 6828 else 6829 data->temperature = 0xff; 6830 return (0); 6831 } 6832 6833 int 6834 ctl_log_sense(struct ctl_scsiio *ctsio) 6835 { 6836 struct ctl_lun *lun = CTL_LUN(ctsio); 6837 int i, pc, page_code, subpage; 6838 int alloc_len, total_len; 6839 struct ctl_page_index *page_index; 6840 struct scsi_log_sense *cdb; 6841 struct scsi_log_header *header; 6842 6843 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6844 6845 cdb = (struct scsi_log_sense *)ctsio->cdb; 6846 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6847 page_code = cdb->page & SLS_PAGE_CODE; 6848 subpage = cdb->subpage; 6849 alloc_len = scsi_2btoul(cdb->length); 6850 6851 page_index = NULL; 6852 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6853 page_index = &lun->log_pages.index[i]; 6854 6855 /* Look for the right page code */ 6856 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6857 continue; 6858 6859 /* Look for the right subpage or the subpage wildcard*/ 6860 if (page_index->subpage != subpage) 6861 continue; 6862 6863 break; 6864 } 6865 if (i >= CTL_NUM_LOG_PAGES) { 6866 ctl_set_invalid_field(ctsio, 6867 /*sks_valid*/ 1, 6868 /*command*/ 1, 6869 /*field*/ 2, 6870 /*bit_valid*/ 0, 6871 /*bit*/ 0); 6872 ctl_done((union ctl_io *)ctsio); 6873 return (CTL_RETVAL_COMPLETE); 6874 } 6875 6876 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6877 6878 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6879 ctsio->kern_sg_entries = 0; 6880 ctsio->kern_rel_offset = 0; 6881 ctsio->kern_data_len = min(total_len, alloc_len); 6882 ctsio->kern_total_len = ctsio->kern_data_len; 6883 6884 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6885 header->page = page_index->page_code; 6886 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6887 header->page |= SL_DS; 6888 if (page_index->subpage) { 6889 header->page |= SL_SPF; 6890 header->subpage = page_index->subpage; 6891 } 6892 scsi_ulto2b(page_index->page_len, header->datalen); 6893 6894 /* 6895 * Call the handler, if it exists, to update the 6896 * page to the latest values. 6897 */ 6898 if (page_index->sense_handler != NULL) 6899 page_index->sense_handler(ctsio, page_index, pc); 6900 6901 memcpy(header + 1, page_index->page_data, page_index->page_len); 6902 6903 ctl_set_success(ctsio); 6904 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6905 ctsio->be_move_done = ctl_config_move_done; 6906 ctl_datamove((union ctl_io *)ctsio); 6907 return (CTL_RETVAL_COMPLETE); 6908 } 6909 6910 int 6911 ctl_read_capacity(struct ctl_scsiio *ctsio) 6912 { 6913 struct ctl_lun *lun = CTL_LUN(ctsio); 6914 struct scsi_read_capacity *cdb; 6915 struct scsi_read_capacity_data *data; 6916 uint32_t lba; 6917 6918 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6919 6920 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6921 6922 lba = scsi_4btoul(cdb->addr); 6923 if (((cdb->pmi & SRC_PMI) == 0) 6924 && (lba != 0)) { 6925 ctl_set_invalid_field(/*ctsio*/ ctsio, 6926 /*sks_valid*/ 1, 6927 /*command*/ 1, 6928 /*field*/ 2, 6929 /*bit_valid*/ 0, 6930 /*bit*/ 0); 6931 ctl_done((union ctl_io *)ctsio); 6932 return (CTL_RETVAL_COMPLETE); 6933 } 6934 6935 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6936 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6937 ctsio->kern_data_len = sizeof(*data); 6938 ctsio->kern_total_len = sizeof(*data); 6939 ctsio->kern_rel_offset = 0; 6940 ctsio->kern_sg_entries = 0; 6941 6942 /* 6943 * If the maximum LBA is greater than 0xfffffffe, the user must 6944 * issue a SERVICE ACTION IN (16) command, with the read capacity 6945 * serivce action set. 6946 */ 6947 if (lun->be_lun->maxlba > 0xfffffffe) 6948 scsi_ulto4b(0xffffffff, data->addr); 6949 else 6950 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6951 6952 /* 6953 * XXX KDM this may not be 512 bytes... 6954 */ 6955 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6956 6957 ctl_set_success(ctsio); 6958 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6959 ctsio->be_move_done = ctl_config_move_done; 6960 ctl_datamove((union ctl_io *)ctsio); 6961 return (CTL_RETVAL_COMPLETE); 6962 } 6963 6964 int 6965 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6966 { 6967 struct ctl_lun *lun = CTL_LUN(ctsio); 6968 struct scsi_read_capacity_16 *cdb; 6969 struct scsi_read_capacity_data_long *data; 6970 uint64_t lba; 6971 uint32_t alloc_len; 6972 6973 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6974 6975 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6976 6977 alloc_len = scsi_4btoul(cdb->alloc_len); 6978 lba = scsi_8btou64(cdb->addr); 6979 6980 if ((cdb->reladr & SRC16_PMI) 6981 && (lba != 0)) { 6982 ctl_set_invalid_field(/*ctsio*/ ctsio, 6983 /*sks_valid*/ 1, 6984 /*command*/ 1, 6985 /*field*/ 2, 6986 /*bit_valid*/ 0, 6987 /*bit*/ 0); 6988 ctl_done((union ctl_io *)ctsio); 6989 return (CTL_RETVAL_COMPLETE); 6990 } 6991 6992 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6993 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6994 ctsio->kern_rel_offset = 0; 6995 ctsio->kern_sg_entries = 0; 6996 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 6997 ctsio->kern_total_len = ctsio->kern_data_len; 6998 6999 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7000 /* XXX KDM this may not be 512 bytes... */ 7001 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7002 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7003 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7004 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7005 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7006 7007 ctl_set_success(ctsio); 7008 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7009 ctsio->be_move_done = ctl_config_move_done; 7010 ctl_datamove((union ctl_io *)ctsio); 7011 return (CTL_RETVAL_COMPLETE); 7012 } 7013 7014 int 7015 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7016 { 7017 struct ctl_lun *lun = CTL_LUN(ctsio); 7018 struct scsi_get_lba_status *cdb; 7019 struct scsi_get_lba_status_data *data; 7020 struct ctl_lba_len_flags *lbalen; 7021 uint64_t lba; 7022 uint32_t alloc_len, total_len; 7023 int retval; 7024 7025 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7026 7027 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7028 lba = scsi_8btou64(cdb->addr); 7029 alloc_len = scsi_4btoul(cdb->alloc_len); 7030 7031 if (lba > lun->be_lun->maxlba) { 7032 ctl_set_lba_out_of_range(ctsio, lba); 7033 ctl_done((union ctl_io *)ctsio); 7034 return (CTL_RETVAL_COMPLETE); 7035 } 7036 7037 total_len = sizeof(*data) + sizeof(data->descr[0]); 7038 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7039 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7040 ctsio->kern_rel_offset = 0; 7041 ctsio->kern_sg_entries = 0; 7042 ctsio->kern_data_len = min(total_len, alloc_len); 7043 ctsio->kern_total_len = ctsio->kern_data_len; 7044 7045 /* Fill dummy data in case backend can't tell anything. */ 7046 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7047 scsi_u64to8b(lba, data->descr[0].addr); 7048 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7049 data->descr[0].length); 7050 data->descr[0].status = 0; /* Mapped or unknown. */ 7051 7052 ctl_set_success(ctsio); 7053 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7054 ctsio->be_move_done = ctl_config_move_done; 7055 7056 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7057 lbalen->lba = lba; 7058 lbalen->len = total_len; 7059 lbalen->flags = 0; 7060 retval = lun->backend->config_read((union ctl_io *)ctsio); 7061 return (retval); 7062 } 7063 7064 int 7065 ctl_read_defect(struct ctl_scsiio *ctsio) 7066 { 7067 struct scsi_read_defect_data_10 *ccb10; 7068 struct scsi_read_defect_data_12 *ccb12; 7069 struct scsi_read_defect_data_hdr_10 *data10; 7070 struct scsi_read_defect_data_hdr_12 *data12; 7071 uint32_t alloc_len, data_len; 7072 uint8_t format; 7073 7074 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7075 7076 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7077 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7078 format = ccb10->format; 7079 alloc_len = scsi_2btoul(ccb10->alloc_length); 7080 data_len = sizeof(*data10); 7081 } else { 7082 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7083 format = ccb12->format; 7084 alloc_len = scsi_4btoul(ccb12->alloc_length); 7085 data_len = sizeof(*data12); 7086 } 7087 if (alloc_len == 0) { 7088 ctl_set_success(ctsio); 7089 ctl_done((union ctl_io *)ctsio); 7090 return (CTL_RETVAL_COMPLETE); 7091 } 7092 7093 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7094 ctsio->kern_rel_offset = 0; 7095 ctsio->kern_sg_entries = 0; 7096 ctsio->kern_data_len = min(data_len, alloc_len); 7097 ctsio->kern_total_len = ctsio->kern_data_len; 7098 7099 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7100 data10 = (struct scsi_read_defect_data_hdr_10 *) 7101 ctsio->kern_data_ptr; 7102 data10->format = format; 7103 scsi_ulto2b(0, data10->length); 7104 } else { 7105 data12 = (struct scsi_read_defect_data_hdr_12 *) 7106 ctsio->kern_data_ptr; 7107 data12->format = format; 7108 scsi_ulto2b(0, data12->generation); 7109 scsi_ulto4b(0, data12->length); 7110 } 7111 7112 ctl_set_success(ctsio); 7113 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7114 ctsio->be_move_done = ctl_config_move_done; 7115 ctl_datamove((union ctl_io *)ctsio); 7116 return (CTL_RETVAL_COMPLETE); 7117 } 7118 7119 int 7120 ctl_report_ident_info(struct ctl_scsiio *ctsio) 7121 { 7122 struct ctl_lun *lun = CTL_LUN(ctsio); 7123 struct scsi_report_ident_info *cdb; 7124 struct scsi_report_ident_info_data *rii_ptr; 7125 struct scsi_report_ident_info_descr *riid_ptr; 7126 const char *oii, *otii; 7127 int retval, alloc_len, total_len = 0, len = 0; 7128 7129 CTL_DEBUG_PRINT(("ctl_report_ident_info\n")); 7130 7131 cdb = (struct scsi_report_ident_info *)ctsio->cdb; 7132 retval = CTL_RETVAL_COMPLETE; 7133 7134 total_len = sizeof(struct scsi_report_ident_info_data); 7135 switch (cdb->type) { 7136 case RII_LUII: 7137 oii = dnvlist_get_string(lun->be_lun->options, 7138 "ident_info", NULL); 7139 if (oii) 7140 len = strlen(oii); /* Approximately */ 7141 break; 7142 case RII_LUTII: 7143 otii = dnvlist_get_string(lun->be_lun->options, 7144 "text_ident_info", NULL); 7145 if (otii) 7146 len = strlen(otii) + 1; /* NULL-terminated */ 7147 break; 7148 case RII_IIS: 7149 len = 2 * sizeof(struct scsi_report_ident_info_descr); 7150 break; 7151 default: 7152 ctl_set_invalid_field(/*ctsio*/ ctsio, 7153 /*sks_valid*/ 1, 7154 /*command*/ 1, 7155 /*field*/ 11, 7156 /*bit_valid*/ 1, 7157 /*bit*/ 2); 7158 ctl_done((union ctl_io *)ctsio); 7159 return(retval); 7160 } 7161 total_len += len; 7162 alloc_len = scsi_4btoul(cdb->length); 7163 7164 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7165 ctsio->kern_sg_entries = 0; 7166 ctsio->kern_rel_offset = 0; 7167 ctsio->kern_data_len = min(total_len, alloc_len); 7168 ctsio->kern_total_len = ctsio->kern_data_len; 7169 7170 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr; 7171 switch (cdb->type) { 7172 case RII_LUII: 7173 if (oii) { 7174 if (oii[0] == '0' && oii[1] == 'x') 7175 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len); 7176 else 7177 strncpy((uint8_t *)(rii_ptr + 1), oii, len); 7178 } 7179 break; 7180 case RII_LUTII: 7181 if (otii) 7182 strlcpy((uint8_t *)(rii_ptr + 1), otii, len); 7183 break; 7184 case RII_IIS: 7185 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1); 7186 riid_ptr->type = RII_LUII; 7187 scsi_ulto2b(0xffff, riid_ptr->length); 7188 riid_ptr++; 7189 riid_ptr->type = RII_LUTII; 7190 scsi_ulto2b(0xffff, riid_ptr->length); 7191 } 7192 scsi_ulto2b(len, rii_ptr->length); 7193 7194 ctl_set_success(ctsio); 7195 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7196 ctsio->be_move_done = ctl_config_move_done; 7197 ctl_datamove((union ctl_io *)ctsio); 7198 return(retval); 7199 } 7200 7201 int 7202 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7203 { 7204 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7205 struct ctl_lun *lun = CTL_LUN(ctsio); 7206 struct scsi_maintenance_in *cdb; 7207 int retval; 7208 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7209 int num_ha_groups, num_target_ports, shared_group; 7210 struct ctl_port *port; 7211 struct scsi_target_group_data *rtg_ptr; 7212 struct scsi_target_group_data_extended *rtg_ext_ptr; 7213 struct scsi_target_port_group_descriptor *tpg_desc; 7214 7215 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7216 7217 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7218 retval = CTL_RETVAL_COMPLETE; 7219 7220 switch (cdb->byte2 & STG_PDF_MASK) { 7221 case STG_PDF_LENGTH: 7222 ext = 0; 7223 break; 7224 case STG_PDF_EXTENDED: 7225 ext = 1; 7226 break; 7227 default: 7228 ctl_set_invalid_field(/*ctsio*/ ctsio, 7229 /*sks_valid*/ 1, 7230 /*command*/ 1, 7231 /*field*/ 2, 7232 /*bit_valid*/ 1, 7233 /*bit*/ 5); 7234 ctl_done((union ctl_io *)ctsio); 7235 return(retval); 7236 } 7237 7238 num_target_ports = 0; 7239 shared_group = (softc->is_single != 0); 7240 mtx_lock(&softc->ctl_lock); 7241 STAILQ_FOREACH(port, &softc->port_list, links) { 7242 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7243 continue; 7244 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7245 continue; 7246 num_target_ports++; 7247 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7248 shared_group = 1; 7249 } 7250 mtx_unlock(&softc->ctl_lock); 7251 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7252 7253 if (ext) 7254 total_len = sizeof(struct scsi_target_group_data_extended); 7255 else 7256 total_len = sizeof(struct scsi_target_group_data); 7257 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7258 (shared_group + num_ha_groups) + 7259 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7260 7261 alloc_len = scsi_4btoul(cdb->length); 7262 7263 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7264 ctsio->kern_sg_entries = 0; 7265 ctsio->kern_rel_offset = 0; 7266 ctsio->kern_data_len = min(total_len, alloc_len); 7267 ctsio->kern_total_len = ctsio->kern_data_len; 7268 7269 if (ext) { 7270 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7271 ctsio->kern_data_ptr; 7272 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7273 rtg_ext_ptr->format_type = 0x10; 7274 rtg_ext_ptr->implicit_transition_time = 0; 7275 tpg_desc = &rtg_ext_ptr->groups[0]; 7276 } else { 7277 rtg_ptr = (struct scsi_target_group_data *) 7278 ctsio->kern_data_ptr; 7279 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7280 tpg_desc = &rtg_ptr->groups[0]; 7281 } 7282 7283 mtx_lock(&softc->ctl_lock); 7284 pg = softc->port_min / softc->port_cnt; 7285 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7286 /* Some shelf is known to be primary. */ 7287 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7288 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7289 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7290 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7291 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7292 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7293 else 7294 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7295 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7296 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7297 } else { 7298 ts = os; 7299 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7300 } 7301 } else { 7302 /* No known primary shelf. */ 7303 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7304 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7305 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7306 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7307 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7308 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7309 } else { 7310 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7311 } 7312 } 7313 if (shared_group) { 7314 tpg_desc->pref_state = ts; 7315 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7316 TPG_U_SUP | TPG_T_SUP; 7317 scsi_ulto2b(1, tpg_desc->target_port_group); 7318 tpg_desc->status = TPG_IMPLICIT; 7319 pc = 0; 7320 STAILQ_FOREACH(port, &softc->port_list, links) { 7321 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7322 continue; 7323 if (!softc->is_single && 7324 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7325 continue; 7326 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7327 continue; 7328 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7329 relative_target_port_identifier); 7330 pc++; 7331 } 7332 tpg_desc->target_port_count = pc; 7333 tpg_desc = (struct scsi_target_port_group_descriptor *) 7334 &tpg_desc->descriptors[pc]; 7335 } 7336 for (g = 0; g < num_ha_groups; g++) { 7337 tpg_desc->pref_state = (g == pg) ? ts : os; 7338 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7339 TPG_U_SUP | TPG_T_SUP; 7340 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7341 tpg_desc->status = TPG_IMPLICIT; 7342 pc = 0; 7343 STAILQ_FOREACH(port, &softc->port_list, links) { 7344 if (port->targ_port < g * softc->port_cnt || 7345 port->targ_port >= (g + 1) * softc->port_cnt) 7346 continue; 7347 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7348 continue; 7349 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7350 continue; 7351 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7352 continue; 7353 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7354 relative_target_port_identifier); 7355 pc++; 7356 } 7357 tpg_desc->target_port_count = pc; 7358 tpg_desc = (struct scsi_target_port_group_descriptor *) 7359 &tpg_desc->descriptors[pc]; 7360 } 7361 mtx_unlock(&softc->ctl_lock); 7362 7363 ctl_set_success(ctsio); 7364 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7365 ctsio->be_move_done = ctl_config_move_done; 7366 ctl_datamove((union ctl_io *)ctsio); 7367 return(retval); 7368 } 7369 7370 int 7371 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7372 { 7373 struct ctl_lun *lun = CTL_LUN(ctsio); 7374 struct scsi_report_supported_opcodes *cdb; 7375 const struct ctl_cmd_entry *entry, *sentry; 7376 struct scsi_report_supported_opcodes_all *all; 7377 struct scsi_report_supported_opcodes_descr *descr; 7378 struct scsi_report_supported_opcodes_one *one; 7379 int retval; 7380 int alloc_len, total_len; 7381 int opcode, service_action, i, j, num; 7382 7383 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7384 7385 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7386 retval = CTL_RETVAL_COMPLETE; 7387 7388 opcode = cdb->requested_opcode; 7389 service_action = scsi_2btoul(cdb->requested_service_action); 7390 switch (cdb->options & RSO_OPTIONS_MASK) { 7391 case RSO_OPTIONS_ALL: 7392 num = 0; 7393 for (i = 0; i < 256; i++) { 7394 entry = &ctl_cmd_table[i]; 7395 if (entry->flags & CTL_CMD_FLAG_SA5) { 7396 for (j = 0; j < 32; j++) { 7397 sentry = &((const struct ctl_cmd_entry *) 7398 entry->execute)[j]; 7399 if (ctl_cmd_applicable( 7400 lun->be_lun->lun_type, sentry)) 7401 num++; 7402 } 7403 } else { 7404 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7405 entry)) 7406 num++; 7407 } 7408 } 7409 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7410 num * sizeof(struct scsi_report_supported_opcodes_descr); 7411 break; 7412 case RSO_OPTIONS_OC: 7413 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7414 ctl_set_invalid_field(/*ctsio*/ ctsio, 7415 /*sks_valid*/ 1, 7416 /*command*/ 1, 7417 /*field*/ 2, 7418 /*bit_valid*/ 1, 7419 /*bit*/ 2); 7420 ctl_done((union ctl_io *)ctsio); 7421 return (CTL_RETVAL_COMPLETE); 7422 } 7423 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7424 break; 7425 case RSO_OPTIONS_OC_SA: 7426 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7427 service_action >= 32) { 7428 ctl_set_invalid_field(/*ctsio*/ ctsio, 7429 /*sks_valid*/ 1, 7430 /*command*/ 1, 7431 /*field*/ 2, 7432 /*bit_valid*/ 1, 7433 /*bit*/ 2); 7434 ctl_done((union ctl_io *)ctsio); 7435 return (CTL_RETVAL_COMPLETE); 7436 } 7437 /* FALLTHROUGH */ 7438 case RSO_OPTIONS_OC_ASA: 7439 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7440 break; 7441 default: 7442 ctl_set_invalid_field(/*ctsio*/ ctsio, 7443 /*sks_valid*/ 1, 7444 /*command*/ 1, 7445 /*field*/ 2, 7446 /*bit_valid*/ 1, 7447 /*bit*/ 2); 7448 ctl_done((union ctl_io *)ctsio); 7449 return (CTL_RETVAL_COMPLETE); 7450 } 7451 7452 alloc_len = scsi_4btoul(cdb->length); 7453 7454 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7455 ctsio->kern_sg_entries = 0; 7456 ctsio->kern_rel_offset = 0; 7457 ctsio->kern_data_len = min(total_len, alloc_len); 7458 ctsio->kern_total_len = ctsio->kern_data_len; 7459 7460 switch (cdb->options & RSO_OPTIONS_MASK) { 7461 case RSO_OPTIONS_ALL: 7462 all = (struct scsi_report_supported_opcodes_all *) 7463 ctsio->kern_data_ptr; 7464 num = 0; 7465 for (i = 0; i < 256; i++) { 7466 entry = &ctl_cmd_table[i]; 7467 if (entry->flags & CTL_CMD_FLAG_SA5) { 7468 for (j = 0; j < 32; j++) { 7469 sentry = &((const struct ctl_cmd_entry *) 7470 entry->execute)[j]; 7471 if (!ctl_cmd_applicable( 7472 lun->be_lun->lun_type, sentry)) 7473 continue; 7474 descr = &all->descr[num++]; 7475 descr->opcode = i; 7476 scsi_ulto2b(j, descr->service_action); 7477 descr->flags = RSO_SERVACTV; 7478 scsi_ulto2b(sentry->length, 7479 descr->cdb_length); 7480 } 7481 } else { 7482 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7483 entry)) 7484 continue; 7485 descr = &all->descr[num++]; 7486 descr->opcode = i; 7487 scsi_ulto2b(0, descr->service_action); 7488 descr->flags = 0; 7489 scsi_ulto2b(entry->length, descr->cdb_length); 7490 } 7491 } 7492 scsi_ulto4b( 7493 num * sizeof(struct scsi_report_supported_opcodes_descr), 7494 all->length); 7495 break; 7496 case RSO_OPTIONS_OC: 7497 one = (struct scsi_report_supported_opcodes_one *) 7498 ctsio->kern_data_ptr; 7499 entry = &ctl_cmd_table[opcode]; 7500 goto fill_one; 7501 case RSO_OPTIONS_OC_SA: 7502 one = (struct scsi_report_supported_opcodes_one *) 7503 ctsio->kern_data_ptr; 7504 entry = &ctl_cmd_table[opcode]; 7505 entry = &((const struct ctl_cmd_entry *) 7506 entry->execute)[service_action]; 7507 fill_one: 7508 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7509 one->support = 3; 7510 scsi_ulto2b(entry->length, one->cdb_length); 7511 one->cdb_usage[0] = opcode; 7512 memcpy(&one->cdb_usage[1], entry->usage, 7513 entry->length - 1); 7514 } else 7515 one->support = 1; 7516 break; 7517 case RSO_OPTIONS_OC_ASA: 7518 one = (struct scsi_report_supported_opcodes_one *) 7519 ctsio->kern_data_ptr; 7520 entry = &ctl_cmd_table[opcode]; 7521 if (entry->flags & CTL_CMD_FLAG_SA5) { 7522 entry = &((const struct ctl_cmd_entry *) 7523 entry->execute)[service_action]; 7524 } else if (service_action != 0) { 7525 one->support = 1; 7526 break; 7527 } 7528 goto fill_one; 7529 } 7530 7531 ctl_set_success(ctsio); 7532 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7533 ctsio->be_move_done = ctl_config_move_done; 7534 ctl_datamove((union ctl_io *)ctsio); 7535 return(retval); 7536 } 7537 7538 int 7539 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7540 { 7541 struct scsi_report_supported_tmf *cdb; 7542 struct scsi_report_supported_tmf_ext_data *data; 7543 int retval; 7544 int alloc_len, total_len; 7545 7546 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7547 7548 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7549 7550 retval = CTL_RETVAL_COMPLETE; 7551 7552 if (cdb->options & RST_REPD) 7553 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7554 else 7555 total_len = sizeof(struct scsi_report_supported_tmf_data); 7556 alloc_len = scsi_4btoul(cdb->length); 7557 7558 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7559 ctsio->kern_sg_entries = 0; 7560 ctsio->kern_rel_offset = 0; 7561 ctsio->kern_data_len = min(total_len, alloc_len); 7562 ctsio->kern_total_len = ctsio->kern_data_len; 7563 7564 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7565 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7566 RST_TRS; 7567 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7568 data->length = total_len - 4; 7569 7570 ctl_set_success(ctsio); 7571 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7572 ctsio->be_move_done = ctl_config_move_done; 7573 ctl_datamove((union ctl_io *)ctsio); 7574 return (retval); 7575 } 7576 7577 int 7578 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7579 { 7580 struct scsi_report_timestamp *cdb; 7581 struct scsi_report_timestamp_data *data; 7582 struct timeval tv; 7583 int64_t timestamp; 7584 int retval; 7585 int alloc_len, total_len; 7586 7587 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7588 7589 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7590 7591 retval = CTL_RETVAL_COMPLETE; 7592 7593 total_len = sizeof(struct scsi_report_timestamp_data); 7594 alloc_len = scsi_4btoul(cdb->length); 7595 7596 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7597 ctsio->kern_sg_entries = 0; 7598 ctsio->kern_rel_offset = 0; 7599 ctsio->kern_data_len = min(total_len, alloc_len); 7600 ctsio->kern_total_len = ctsio->kern_data_len; 7601 7602 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7603 scsi_ulto2b(sizeof(*data) - 2, data->length); 7604 data->origin = RTS_ORIG_OUTSIDE; 7605 getmicrotime(&tv); 7606 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7607 scsi_ulto4b(timestamp >> 16, data->timestamp); 7608 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7609 7610 ctl_set_success(ctsio); 7611 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7612 ctsio->be_move_done = ctl_config_move_done; 7613 ctl_datamove((union ctl_io *)ctsio); 7614 return (retval); 7615 } 7616 7617 int 7618 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7619 { 7620 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7621 struct ctl_lun *lun = CTL_LUN(ctsio); 7622 struct scsi_per_res_in *cdb; 7623 int alloc_len, total_len = 0; 7624 /* struct scsi_per_res_in_rsrv in_data; */ 7625 uint64_t key; 7626 7627 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7628 7629 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7630 7631 alloc_len = scsi_2btoul(cdb->length); 7632 7633 retry: 7634 mtx_lock(&lun->lun_lock); 7635 switch (cdb->action) { 7636 case SPRI_RK: /* read keys */ 7637 total_len = sizeof(struct scsi_per_res_in_keys) + 7638 lun->pr_key_count * 7639 sizeof(struct scsi_per_res_key); 7640 break; 7641 case SPRI_RR: /* read reservation */ 7642 if (lun->flags & CTL_LUN_PR_RESERVED) 7643 total_len = sizeof(struct scsi_per_res_in_rsrv); 7644 else 7645 total_len = sizeof(struct scsi_per_res_in_header); 7646 break; 7647 case SPRI_RC: /* report capabilities */ 7648 total_len = sizeof(struct scsi_per_res_cap); 7649 break; 7650 case SPRI_RS: /* read full status */ 7651 total_len = sizeof(struct scsi_per_res_in_header) + 7652 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7653 lun->pr_key_count; 7654 break; 7655 default: 7656 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7657 } 7658 mtx_unlock(&lun->lun_lock); 7659 7660 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7661 ctsio->kern_rel_offset = 0; 7662 ctsio->kern_sg_entries = 0; 7663 ctsio->kern_data_len = min(total_len, alloc_len); 7664 ctsio->kern_total_len = ctsio->kern_data_len; 7665 7666 mtx_lock(&lun->lun_lock); 7667 switch (cdb->action) { 7668 case SPRI_RK: { // read keys 7669 struct scsi_per_res_in_keys *res_keys; 7670 int i, key_count; 7671 7672 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7673 7674 /* 7675 * We had to drop the lock to allocate our buffer, which 7676 * leaves time for someone to come in with another 7677 * persistent reservation. (That is unlikely, though, 7678 * since this should be the only persistent reservation 7679 * command active right now.) 7680 */ 7681 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7682 (lun->pr_key_count * 7683 sizeof(struct scsi_per_res_key)))){ 7684 mtx_unlock(&lun->lun_lock); 7685 free(ctsio->kern_data_ptr, M_CTL); 7686 printf("%s: reservation length changed, retrying\n", 7687 __func__); 7688 goto retry; 7689 } 7690 7691 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7692 7693 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7694 lun->pr_key_count, res_keys->header.length); 7695 7696 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7697 if ((key = ctl_get_prkey(lun, i)) == 0) 7698 continue; 7699 7700 /* 7701 * We used lun->pr_key_count to calculate the 7702 * size to allocate. If it turns out the number of 7703 * initiators with the registered flag set is 7704 * larger than that (i.e. they haven't been kept in 7705 * sync), we've got a problem. 7706 */ 7707 if (key_count >= lun->pr_key_count) { 7708 key_count++; 7709 continue; 7710 } 7711 scsi_u64to8b(key, res_keys->keys[key_count].key); 7712 key_count++; 7713 } 7714 break; 7715 } 7716 case SPRI_RR: { // read reservation 7717 struct scsi_per_res_in_rsrv *res; 7718 int tmp_len, header_only; 7719 7720 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7721 7722 scsi_ulto4b(lun->pr_generation, res->header.generation); 7723 7724 if (lun->flags & CTL_LUN_PR_RESERVED) 7725 { 7726 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7727 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7728 res->header.length); 7729 header_only = 0; 7730 } else { 7731 tmp_len = sizeof(struct scsi_per_res_in_header); 7732 scsi_ulto4b(0, res->header.length); 7733 header_only = 1; 7734 } 7735 7736 /* 7737 * We had to drop the lock to allocate our buffer, which 7738 * leaves time for someone to come in with another 7739 * persistent reservation. (That is unlikely, though, 7740 * since this should be the only persistent reservation 7741 * command active right now.) 7742 */ 7743 if (tmp_len != total_len) { 7744 mtx_unlock(&lun->lun_lock); 7745 free(ctsio->kern_data_ptr, M_CTL); 7746 printf("%s: reservation status changed, retrying\n", 7747 __func__); 7748 goto retry; 7749 } 7750 7751 /* 7752 * No reservation held, so we're done. 7753 */ 7754 if (header_only != 0) 7755 break; 7756 7757 /* 7758 * If the registration is an All Registrants type, the key 7759 * is 0, since it doesn't really matter. 7760 */ 7761 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7762 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7763 res->data.reservation); 7764 } 7765 res->data.scopetype = lun->pr_res_type; 7766 break; 7767 } 7768 case SPRI_RC: //report capabilities 7769 { 7770 struct scsi_per_res_cap *res_cap; 7771 uint16_t type_mask; 7772 7773 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7774 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7775 res_cap->flags1 = SPRI_CRH; 7776 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7777 type_mask = SPRI_TM_WR_EX_AR | 7778 SPRI_TM_EX_AC_RO | 7779 SPRI_TM_WR_EX_RO | 7780 SPRI_TM_EX_AC | 7781 SPRI_TM_WR_EX | 7782 SPRI_TM_EX_AC_AR; 7783 scsi_ulto2b(type_mask, res_cap->type_mask); 7784 break; 7785 } 7786 case SPRI_RS: { // read full status 7787 struct scsi_per_res_in_full *res_status; 7788 struct scsi_per_res_in_full_desc *res_desc; 7789 struct ctl_port *port; 7790 int i, len; 7791 7792 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7793 7794 /* 7795 * We had to drop the lock to allocate our buffer, which 7796 * leaves time for someone to come in with another 7797 * persistent reservation. (That is unlikely, though, 7798 * since this should be the only persistent reservation 7799 * command active right now.) 7800 */ 7801 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7802 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7803 lun->pr_key_count)){ 7804 mtx_unlock(&lun->lun_lock); 7805 free(ctsio->kern_data_ptr, M_CTL); 7806 printf("%s: reservation length changed, retrying\n", 7807 __func__); 7808 goto retry; 7809 } 7810 7811 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7812 7813 res_desc = &res_status->desc[0]; 7814 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7815 if ((key = ctl_get_prkey(lun, i)) == 0) 7816 continue; 7817 7818 scsi_u64to8b(key, res_desc->res_key.key); 7819 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7820 (lun->pr_res_idx == i || 7821 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7822 res_desc->flags = SPRI_FULL_R_HOLDER; 7823 res_desc->scopetype = lun->pr_res_type; 7824 } 7825 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7826 res_desc->rel_trgt_port_id); 7827 len = 0; 7828 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7829 if (port != NULL) 7830 len = ctl_create_iid(port, 7831 i % CTL_MAX_INIT_PER_PORT, 7832 res_desc->transport_id); 7833 scsi_ulto4b(len, res_desc->additional_length); 7834 res_desc = (struct scsi_per_res_in_full_desc *) 7835 &res_desc->transport_id[len]; 7836 } 7837 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7838 res_status->header.length); 7839 break; 7840 } 7841 default: 7842 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7843 } 7844 mtx_unlock(&lun->lun_lock); 7845 7846 ctl_set_success(ctsio); 7847 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7848 ctsio->be_move_done = ctl_config_move_done; 7849 ctl_datamove((union ctl_io *)ctsio); 7850 return (CTL_RETVAL_COMPLETE); 7851 } 7852 7853 /* 7854 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7855 * it should return. 7856 */ 7857 static int 7858 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7859 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7860 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7861 struct scsi_per_res_out_parms* param) 7862 { 7863 union ctl_ha_msg persis_io; 7864 int i; 7865 7866 mtx_lock(&lun->lun_lock); 7867 if (sa_res_key == 0) { 7868 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7869 /* validate scope and type */ 7870 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7871 SPR_LU_SCOPE) { 7872 mtx_unlock(&lun->lun_lock); 7873 ctl_set_invalid_field(/*ctsio*/ ctsio, 7874 /*sks_valid*/ 1, 7875 /*command*/ 1, 7876 /*field*/ 2, 7877 /*bit_valid*/ 1, 7878 /*bit*/ 4); 7879 ctl_done((union ctl_io *)ctsio); 7880 return (1); 7881 } 7882 7883 if (type>8 || type==2 || type==4 || type==0) { 7884 mtx_unlock(&lun->lun_lock); 7885 ctl_set_invalid_field(/*ctsio*/ ctsio, 7886 /*sks_valid*/ 1, 7887 /*command*/ 1, 7888 /*field*/ 2, 7889 /*bit_valid*/ 1, 7890 /*bit*/ 0); 7891 ctl_done((union ctl_io *)ctsio); 7892 return (1); 7893 } 7894 7895 /* 7896 * Unregister everybody else and build UA for 7897 * them 7898 */ 7899 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7900 if (i == residx || ctl_get_prkey(lun, i) == 0) 7901 continue; 7902 7903 ctl_clr_prkey(lun, i); 7904 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7905 } 7906 lun->pr_key_count = 1; 7907 lun->pr_res_type = type; 7908 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7909 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7910 lun->pr_res_idx = residx; 7911 lun->pr_generation++; 7912 mtx_unlock(&lun->lun_lock); 7913 7914 /* send msg to other side */ 7915 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7916 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7917 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7918 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7919 persis_io.pr.pr_info.res_type = type; 7920 memcpy(persis_io.pr.pr_info.sa_res_key, 7921 param->serv_act_res_key, 7922 sizeof(param->serv_act_res_key)); 7923 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7924 sizeof(persis_io.pr), M_WAITOK); 7925 } else { 7926 /* not all registrants */ 7927 mtx_unlock(&lun->lun_lock); 7928 free(ctsio->kern_data_ptr, M_CTL); 7929 ctl_set_invalid_field(ctsio, 7930 /*sks_valid*/ 1, 7931 /*command*/ 0, 7932 /*field*/ 8, 7933 /*bit_valid*/ 0, 7934 /*bit*/ 0); 7935 ctl_done((union ctl_io *)ctsio); 7936 return (1); 7937 } 7938 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7939 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7940 int found = 0; 7941 7942 if (res_key == sa_res_key) { 7943 /* special case */ 7944 /* 7945 * The spec implies this is not good but doesn't 7946 * say what to do. There are two choices either 7947 * generate a res conflict or check condition 7948 * with illegal field in parameter data. Since 7949 * that is what is done when the sa_res_key is 7950 * zero I'll take that approach since this has 7951 * to do with the sa_res_key. 7952 */ 7953 mtx_unlock(&lun->lun_lock); 7954 free(ctsio->kern_data_ptr, M_CTL); 7955 ctl_set_invalid_field(ctsio, 7956 /*sks_valid*/ 1, 7957 /*command*/ 0, 7958 /*field*/ 8, 7959 /*bit_valid*/ 0, 7960 /*bit*/ 0); 7961 ctl_done((union ctl_io *)ctsio); 7962 return (1); 7963 } 7964 7965 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7966 if (ctl_get_prkey(lun, i) != sa_res_key) 7967 continue; 7968 7969 found = 1; 7970 ctl_clr_prkey(lun, i); 7971 lun->pr_key_count--; 7972 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7973 } 7974 if (!found) { 7975 mtx_unlock(&lun->lun_lock); 7976 free(ctsio->kern_data_ptr, M_CTL); 7977 ctl_set_reservation_conflict(ctsio); 7978 ctl_done((union ctl_io *)ctsio); 7979 return (CTL_RETVAL_COMPLETE); 7980 } 7981 lun->pr_generation++; 7982 mtx_unlock(&lun->lun_lock); 7983 7984 /* send msg to other side */ 7985 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7986 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7987 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7988 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7989 persis_io.pr.pr_info.res_type = type; 7990 memcpy(persis_io.pr.pr_info.sa_res_key, 7991 param->serv_act_res_key, 7992 sizeof(param->serv_act_res_key)); 7993 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7994 sizeof(persis_io.pr), M_WAITOK); 7995 } else { 7996 /* Reserved but not all registrants */ 7997 /* sa_res_key is res holder */ 7998 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7999 /* validate scope and type */ 8000 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8001 SPR_LU_SCOPE) { 8002 mtx_unlock(&lun->lun_lock); 8003 ctl_set_invalid_field(/*ctsio*/ ctsio, 8004 /*sks_valid*/ 1, 8005 /*command*/ 1, 8006 /*field*/ 2, 8007 /*bit_valid*/ 1, 8008 /*bit*/ 4); 8009 ctl_done((union ctl_io *)ctsio); 8010 return (1); 8011 } 8012 8013 if (type>8 || type==2 || type==4 || type==0) { 8014 mtx_unlock(&lun->lun_lock); 8015 ctl_set_invalid_field(/*ctsio*/ ctsio, 8016 /*sks_valid*/ 1, 8017 /*command*/ 1, 8018 /*field*/ 2, 8019 /*bit_valid*/ 1, 8020 /*bit*/ 0); 8021 ctl_done((union ctl_io *)ctsio); 8022 return (1); 8023 } 8024 8025 /* 8026 * Do the following: 8027 * if sa_res_key != res_key remove all 8028 * registrants w/sa_res_key and generate UA 8029 * for these registrants(Registrations 8030 * Preempted) if it wasn't an exclusive 8031 * reservation generate UA(Reservations 8032 * Preempted) for all other registered nexuses 8033 * if the type has changed. Establish the new 8034 * reservation and holder. If res_key and 8035 * sa_res_key are the same do the above 8036 * except don't unregister the res holder. 8037 */ 8038 8039 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8040 if (i == residx || ctl_get_prkey(lun, i) == 0) 8041 continue; 8042 8043 if (sa_res_key == ctl_get_prkey(lun, i)) { 8044 ctl_clr_prkey(lun, i); 8045 lun->pr_key_count--; 8046 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8047 } else if (type != lun->pr_res_type && 8048 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8049 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8050 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8051 } 8052 } 8053 lun->pr_res_type = type; 8054 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8055 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8056 lun->pr_res_idx = residx; 8057 else 8058 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8059 lun->pr_generation++; 8060 mtx_unlock(&lun->lun_lock); 8061 8062 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8063 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8064 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8065 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8066 persis_io.pr.pr_info.res_type = type; 8067 memcpy(persis_io.pr.pr_info.sa_res_key, 8068 param->serv_act_res_key, 8069 sizeof(param->serv_act_res_key)); 8070 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8071 sizeof(persis_io.pr), M_WAITOK); 8072 } else { 8073 /* 8074 * sa_res_key is not the res holder just 8075 * remove registrants 8076 */ 8077 int found=0; 8078 8079 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8080 if (sa_res_key != ctl_get_prkey(lun, i)) 8081 continue; 8082 8083 found = 1; 8084 ctl_clr_prkey(lun, i); 8085 lun->pr_key_count--; 8086 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8087 } 8088 8089 if (!found) { 8090 mtx_unlock(&lun->lun_lock); 8091 free(ctsio->kern_data_ptr, M_CTL); 8092 ctl_set_reservation_conflict(ctsio); 8093 ctl_done((union ctl_io *)ctsio); 8094 return (1); 8095 } 8096 lun->pr_generation++; 8097 mtx_unlock(&lun->lun_lock); 8098 8099 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8100 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8101 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8102 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8103 persis_io.pr.pr_info.res_type = type; 8104 memcpy(persis_io.pr.pr_info.sa_res_key, 8105 param->serv_act_res_key, 8106 sizeof(param->serv_act_res_key)); 8107 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8108 sizeof(persis_io.pr), M_WAITOK); 8109 } 8110 } 8111 return (0); 8112 } 8113 8114 static void 8115 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8116 { 8117 uint64_t sa_res_key; 8118 int i; 8119 8120 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8121 8122 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8123 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8124 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8125 if (sa_res_key == 0) { 8126 /* 8127 * Unregister everybody else and build UA for 8128 * them 8129 */ 8130 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8131 if (i == msg->pr.pr_info.residx || 8132 ctl_get_prkey(lun, i) == 0) 8133 continue; 8134 8135 ctl_clr_prkey(lun, i); 8136 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8137 } 8138 8139 lun->pr_key_count = 1; 8140 lun->pr_res_type = msg->pr.pr_info.res_type; 8141 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8142 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8143 lun->pr_res_idx = msg->pr.pr_info.residx; 8144 } else { 8145 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8146 if (sa_res_key == ctl_get_prkey(lun, i)) 8147 continue; 8148 8149 ctl_clr_prkey(lun, i); 8150 lun->pr_key_count--; 8151 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8152 } 8153 } 8154 } else { 8155 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8156 if (i == msg->pr.pr_info.residx || 8157 ctl_get_prkey(lun, i) == 0) 8158 continue; 8159 8160 if (sa_res_key == ctl_get_prkey(lun, i)) { 8161 ctl_clr_prkey(lun, i); 8162 lun->pr_key_count--; 8163 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8164 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8165 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8166 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8167 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8168 } 8169 } 8170 lun->pr_res_type = msg->pr.pr_info.res_type; 8171 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8172 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8173 lun->pr_res_idx = msg->pr.pr_info.residx; 8174 else 8175 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8176 } 8177 lun->pr_generation++; 8178 8179 } 8180 8181 int 8182 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8183 { 8184 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8185 struct ctl_lun *lun = CTL_LUN(ctsio); 8186 int retval; 8187 u_int32_t param_len; 8188 struct scsi_per_res_out *cdb; 8189 struct scsi_per_res_out_parms* param; 8190 uint32_t residx; 8191 uint64_t res_key, sa_res_key, key; 8192 uint8_t type; 8193 union ctl_ha_msg persis_io; 8194 int i; 8195 8196 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8197 8198 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8199 retval = CTL_RETVAL_COMPLETE; 8200 8201 /* 8202 * We only support whole-LUN scope. The scope & type are ignored for 8203 * register, register and ignore existing key and clear. 8204 * We sometimes ignore scope and type on preempts too!! 8205 * Verify reservation type here as well. 8206 */ 8207 type = cdb->scope_type & SPR_TYPE_MASK; 8208 if ((cdb->action == SPRO_RESERVE) 8209 || (cdb->action == SPRO_RELEASE)) { 8210 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8211 ctl_set_invalid_field(/*ctsio*/ ctsio, 8212 /*sks_valid*/ 1, 8213 /*command*/ 1, 8214 /*field*/ 2, 8215 /*bit_valid*/ 1, 8216 /*bit*/ 4); 8217 ctl_done((union ctl_io *)ctsio); 8218 return (CTL_RETVAL_COMPLETE); 8219 } 8220 8221 if (type>8 || type==2 || type==4 || type==0) { 8222 ctl_set_invalid_field(/*ctsio*/ ctsio, 8223 /*sks_valid*/ 1, 8224 /*command*/ 1, 8225 /*field*/ 2, 8226 /*bit_valid*/ 1, 8227 /*bit*/ 0); 8228 ctl_done((union ctl_io *)ctsio); 8229 return (CTL_RETVAL_COMPLETE); 8230 } 8231 } 8232 8233 param_len = scsi_4btoul(cdb->length); 8234 8235 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8236 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8237 ctsio->kern_data_len = param_len; 8238 ctsio->kern_total_len = param_len; 8239 ctsio->kern_rel_offset = 0; 8240 ctsio->kern_sg_entries = 0; 8241 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8242 ctsio->be_move_done = ctl_config_move_done; 8243 ctl_datamove((union ctl_io *)ctsio); 8244 8245 return (CTL_RETVAL_COMPLETE); 8246 } 8247 8248 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8249 8250 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8251 res_key = scsi_8btou64(param->res_key.key); 8252 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8253 8254 /* 8255 * Validate the reservation key here except for SPRO_REG_IGNO 8256 * This must be done for all other service actions 8257 */ 8258 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8259 mtx_lock(&lun->lun_lock); 8260 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8261 if (res_key != key) { 8262 /* 8263 * The current key passed in doesn't match 8264 * the one the initiator previously 8265 * registered. 8266 */ 8267 mtx_unlock(&lun->lun_lock); 8268 free(ctsio->kern_data_ptr, M_CTL); 8269 ctl_set_reservation_conflict(ctsio); 8270 ctl_done((union ctl_io *)ctsio); 8271 return (CTL_RETVAL_COMPLETE); 8272 } 8273 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8274 /* 8275 * We are not registered 8276 */ 8277 mtx_unlock(&lun->lun_lock); 8278 free(ctsio->kern_data_ptr, M_CTL); 8279 ctl_set_reservation_conflict(ctsio); 8280 ctl_done((union ctl_io *)ctsio); 8281 return (CTL_RETVAL_COMPLETE); 8282 } else if (res_key != 0) { 8283 /* 8284 * We are not registered and trying to register but 8285 * the register key isn't zero. 8286 */ 8287 mtx_unlock(&lun->lun_lock); 8288 free(ctsio->kern_data_ptr, M_CTL); 8289 ctl_set_reservation_conflict(ctsio); 8290 ctl_done((union ctl_io *)ctsio); 8291 return (CTL_RETVAL_COMPLETE); 8292 } 8293 mtx_unlock(&lun->lun_lock); 8294 } 8295 8296 switch (cdb->action & SPRO_ACTION_MASK) { 8297 case SPRO_REGISTER: 8298 case SPRO_REG_IGNO: { 8299 /* 8300 * We don't support any of these options, as we report in 8301 * the read capabilities request (see 8302 * ctl_persistent_reserve_in(), above). 8303 */ 8304 if ((param->flags & SPR_SPEC_I_PT) 8305 || (param->flags & SPR_ALL_TG_PT) 8306 || (param->flags & SPR_APTPL)) { 8307 int bit_ptr; 8308 8309 if (param->flags & SPR_APTPL) 8310 bit_ptr = 0; 8311 else if (param->flags & SPR_ALL_TG_PT) 8312 bit_ptr = 2; 8313 else /* SPR_SPEC_I_PT */ 8314 bit_ptr = 3; 8315 8316 free(ctsio->kern_data_ptr, M_CTL); 8317 ctl_set_invalid_field(ctsio, 8318 /*sks_valid*/ 1, 8319 /*command*/ 0, 8320 /*field*/ 20, 8321 /*bit_valid*/ 1, 8322 /*bit*/ bit_ptr); 8323 ctl_done((union ctl_io *)ctsio); 8324 return (CTL_RETVAL_COMPLETE); 8325 } 8326 8327 mtx_lock(&lun->lun_lock); 8328 8329 /* 8330 * The initiator wants to clear the 8331 * key/unregister. 8332 */ 8333 if (sa_res_key == 0) { 8334 if ((res_key == 0 8335 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8336 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8337 && ctl_get_prkey(lun, residx) == 0)) { 8338 mtx_unlock(&lun->lun_lock); 8339 goto done; 8340 } 8341 8342 ctl_clr_prkey(lun, residx); 8343 lun->pr_key_count--; 8344 8345 if (residx == lun->pr_res_idx) { 8346 lun->flags &= ~CTL_LUN_PR_RESERVED; 8347 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8348 8349 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8350 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8351 lun->pr_key_count) { 8352 /* 8353 * If the reservation is a registrants 8354 * only type we need to generate a UA 8355 * for other registered inits. The 8356 * sense code should be RESERVATIONS 8357 * RELEASED 8358 */ 8359 8360 for (i = softc->init_min; i < softc->init_max; i++){ 8361 if (ctl_get_prkey(lun, i) == 0) 8362 continue; 8363 ctl_est_ua(lun, i, 8364 CTL_UA_RES_RELEASE); 8365 } 8366 } 8367 lun->pr_res_type = 0; 8368 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8369 if (lun->pr_key_count==0) { 8370 lun->flags &= ~CTL_LUN_PR_RESERVED; 8371 lun->pr_res_type = 0; 8372 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8373 } 8374 } 8375 lun->pr_generation++; 8376 mtx_unlock(&lun->lun_lock); 8377 8378 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8379 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8380 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8381 persis_io.pr.pr_info.residx = residx; 8382 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8383 sizeof(persis_io.pr), M_WAITOK); 8384 } else /* sa_res_key != 0 */ { 8385 /* 8386 * If we aren't registered currently then increment 8387 * the key count and set the registered flag. 8388 */ 8389 ctl_alloc_prkey(lun, residx); 8390 if (ctl_get_prkey(lun, residx) == 0) 8391 lun->pr_key_count++; 8392 ctl_set_prkey(lun, residx, sa_res_key); 8393 lun->pr_generation++; 8394 mtx_unlock(&lun->lun_lock); 8395 8396 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8397 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8398 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8399 persis_io.pr.pr_info.residx = residx; 8400 memcpy(persis_io.pr.pr_info.sa_res_key, 8401 param->serv_act_res_key, 8402 sizeof(param->serv_act_res_key)); 8403 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8404 sizeof(persis_io.pr), M_WAITOK); 8405 } 8406 8407 break; 8408 } 8409 case SPRO_RESERVE: 8410 mtx_lock(&lun->lun_lock); 8411 if (lun->flags & CTL_LUN_PR_RESERVED) { 8412 /* 8413 * if this isn't the reservation holder and it's 8414 * not a "all registrants" type or if the type is 8415 * different then we have a conflict 8416 */ 8417 if ((lun->pr_res_idx != residx 8418 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8419 || lun->pr_res_type != type) { 8420 mtx_unlock(&lun->lun_lock); 8421 free(ctsio->kern_data_ptr, M_CTL); 8422 ctl_set_reservation_conflict(ctsio); 8423 ctl_done((union ctl_io *)ctsio); 8424 return (CTL_RETVAL_COMPLETE); 8425 } 8426 mtx_unlock(&lun->lun_lock); 8427 } else /* create a reservation */ { 8428 /* 8429 * If it's not an "all registrants" type record 8430 * reservation holder 8431 */ 8432 if (type != SPR_TYPE_WR_EX_AR 8433 && type != SPR_TYPE_EX_AC_AR) 8434 lun->pr_res_idx = residx; /* Res holder */ 8435 else 8436 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8437 8438 lun->flags |= CTL_LUN_PR_RESERVED; 8439 lun->pr_res_type = type; 8440 8441 mtx_unlock(&lun->lun_lock); 8442 8443 /* send msg to other side */ 8444 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8445 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8446 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8447 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8448 persis_io.pr.pr_info.res_type = type; 8449 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8450 sizeof(persis_io.pr), M_WAITOK); 8451 } 8452 break; 8453 8454 case SPRO_RELEASE: 8455 mtx_lock(&lun->lun_lock); 8456 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8457 /* No reservation exists return good status */ 8458 mtx_unlock(&lun->lun_lock); 8459 goto done; 8460 } 8461 /* 8462 * Is this nexus a reservation holder? 8463 */ 8464 if (lun->pr_res_idx != residx 8465 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8466 /* 8467 * not a res holder return good status but 8468 * do nothing 8469 */ 8470 mtx_unlock(&lun->lun_lock); 8471 goto done; 8472 } 8473 8474 if (lun->pr_res_type != type) { 8475 mtx_unlock(&lun->lun_lock); 8476 free(ctsio->kern_data_ptr, M_CTL); 8477 ctl_set_illegal_pr_release(ctsio); 8478 ctl_done((union ctl_io *)ctsio); 8479 return (CTL_RETVAL_COMPLETE); 8480 } 8481 8482 /* okay to release */ 8483 lun->flags &= ~CTL_LUN_PR_RESERVED; 8484 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8485 lun->pr_res_type = 0; 8486 8487 /* 8488 * If this isn't an exclusive access reservation and NUAR 8489 * is not set, generate UA for all other registrants. 8490 */ 8491 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8492 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8493 for (i = softc->init_min; i < softc->init_max; i++) { 8494 if (i == residx || ctl_get_prkey(lun, i) == 0) 8495 continue; 8496 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8497 } 8498 } 8499 mtx_unlock(&lun->lun_lock); 8500 8501 /* Send msg to other side */ 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_RELEASE; 8505 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8506 sizeof(persis_io.pr), M_WAITOK); 8507 break; 8508 8509 case SPRO_CLEAR: 8510 /* send msg to other side */ 8511 8512 mtx_lock(&lun->lun_lock); 8513 lun->flags &= ~CTL_LUN_PR_RESERVED; 8514 lun->pr_res_type = 0; 8515 lun->pr_key_count = 0; 8516 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8517 8518 ctl_clr_prkey(lun, residx); 8519 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8520 if (ctl_get_prkey(lun, i) != 0) { 8521 ctl_clr_prkey(lun, i); 8522 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8523 } 8524 lun->pr_generation++; 8525 mtx_unlock(&lun->lun_lock); 8526 8527 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8528 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8529 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8530 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8531 sizeof(persis_io.pr), M_WAITOK); 8532 break; 8533 8534 case SPRO_PREEMPT: 8535 case SPRO_PRE_ABO: { 8536 int nretval; 8537 8538 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8539 residx, ctsio, cdb, param); 8540 if (nretval != 0) 8541 return (CTL_RETVAL_COMPLETE); 8542 break; 8543 } 8544 default: 8545 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8546 } 8547 8548 done: 8549 free(ctsio->kern_data_ptr, M_CTL); 8550 ctl_set_success(ctsio); 8551 ctl_done((union ctl_io *)ctsio); 8552 8553 return (retval); 8554 } 8555 8556 /* 8557 * This routine is for handling a message from the other SC pertaining to 8558 * persistent reserve out. All the error checking will have been done 8559 * so only perorming the action need be done here to keep the two 8560 * in sync. 8561 */ 8562 static void 8563 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8564 { 8565 struct ctl_softc *softc = CTL_SOFTC(io); 8566 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8567 struct ctl_lun *lun; 8568 int i; 8569 uint32_t residx, targ_lun; 8570 8571 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8572 mtx_lock(&softc->ctl_lock); 8573 if (targ_lun >= ctl_max_luns || 8574 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8575 mtx_unlock(&softc->ctl_lock); 8576 return; 8577 } 8578 mtx_lock(&lun->lun_lock); 8579 mtx_unlock(&softc->ctl_lock); 8580 if (lun->flags & CTL_LUN_DISABLED) { 8581 mtx_unlock(&lun->lun_lock); 8582 return; 8583 } 8584 residx = ctl_get_initindex(&msg->hdr.nexus); 8585 switch(msg->pr.pr_info.action) { 8586 case CTL_PR_REG_KEY: 8587 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8588 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8589 lun->pr_key_count++; 8590 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8591 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8592 lun->pr_generation++; 8593 break; 8594 8595 case CTL_PR_UNREG_KEY: 8596 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8597 lun->pr_key_count--; 8598 8599 /* XXX Need to see if the reservation has been released */ 8600 /* if so do we need to generate UA? */ 8601 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8602 lun->flags &= ~CTL_LUN_PR_RESERVED; 8603 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8604 8605 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8606 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8607 lun->pr_key_count) { 8608 /* 8609 * If the reservation is a registrants 8610 * only type we need to generate a UA 8611 * for other registered inits. The 8612 * sense code should be RESERVATIONS 8613 * RELEASED 8614 */ 8615 8616 for (i = softc->init_min; i < softc->init_max; i++) { 8617 if (ctl_get_prkey(lun, i) == 0) 8618 continue; 8619 8620 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8621 } 8622 } 8623 lun->pr_res_type = 0; 8624 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8625 if (lun->pr_key_count==0) { 8626 lun->flags &= ~CTL_LUN_PR_RESERVED; 8627 lun->pr_res_type = 0; 8628 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8629 } 8630 } 8631 lun->pr_generation++; 8632 break; 8633 8634 case CTL_PR_RESERVE: 8635 lun->flags |= CTL_LUN_PR_RESERVED; 8636 lun->pr_res_type = msg->pr.pr_info.res_type; 8637 lun->pr_res_idx = msg->pr.pr_info.residx; 8638 8639 break; 8640 8641 case CTL_PR_RELEASE: 8642 /* 8643 * If this isn't an exclusive access reservation and NUAR 8644 * is not set, generate UA for all other registrants. 8645 */ 8646 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8647 lun->pr_res_type != SPR_TYPE_WR_EX && 8648 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8649 for (i = softc->init_min; i < softc->init_max; i++) { 8650 if (i == residx || ctl_get_prkey(lun, i) == 0) 8651 continue; 8652 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8653 } 8654 } 8655 8656 lun->flags &= ~CTL_LUN_PR_RESERVED; 8657 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8658 lun->pr_res_type = 0; 8659 break; 8660 8661 case CTL_PR_PREEMPT: 8662 ctl_pro_preempt_other(lun, msg); 8663 break; 8664 case CTL_PR_CLEAR: 8665 lun->flags &= ~CTL_LUN_PR_RESERVED; 8666 lun->pr_res_type = 0; 8667 lun->pr_key_count = 0; 8668 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8669 8670 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8671 if (ctl_get_prkey(lun, i) == 0) 8672 continue; 8673 ctl_clr_prkey(lun, i); 8674 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8675 } 8676 lun->pr_generation++; 8677 break; 8678 } 8679 8680 mtx_unlock(&lun->lun_lock); 8681 } 8682 8683 int 8684 ctl_read_write(struct ctl_scsiio *ctsio) 8685 { 8686 struct ctl_lun *lun = CTL_LUN(ctsio); 8687 struct ctl_lba_len_flags *lbalen; 8688 uint64_t lba; 8689 uint32_t num_blocks; 8690 int flags, retval; 8691 int isread; 8692 8693 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8694 8695 flags = 0; 8696 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8697 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8698 switch (ctsio->cdb[0]) { 8699 case READ_6: 8700 case WRITE_6: { 8701 struct scsi_rw_6 *cdb; 8702 8703 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8704 8705 lba = scsi_3btoul(cdb->addr); 8706 /* only 5 bits are valid in the most significant address byte */ 8707 lba &= 0x1fffff; 8708 num_blocks = cdb->length; 8709 /* 8710 * This is correct according to SBC-2. 8711 */ 8712 if (num_blocks == 0) 8713 num_blocks = 256; 8714 break; 8715 } 8716 case READ_10: 8717 case WRITE_10: { 8718 struct scsi_rw_10 *cdb; 8719 8720 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8721 if (cdb->byte2 & SRW10_FUA) 8722 flags |= CTL_LLF_FUA; 8723 if (cdb->byte2 & SRW10_DPO) 8724 flags |= CTL_LLF_DPO; 8725 lba = scsi_4btoul(cdb->addr); 8726 num_blocks = scsi_2btoul(cdb->length); 8727 break; 8728 } 8729 case WRITE_VERIFY_10: { 8730 struct scsi_write_verify_10 *cdb; 8731 8732 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8733 flags |= CTL_LLF_FUA; 8734 if (cdb->byte2 & SWV_DPO) 8735 flags |= CTL_LLF_DPO; 8736 lba = scsi_4btoul(cdb->addr); 8737 num_blocks = scsi_2btoul(cdb->length); 8738 break; 8739 } 8740 case READ_12: 8741 case WRITE_12: { 8742 struct scsi_rw_12 *cdb; 8743 8744 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8745 if (cdb->byte2 & SRW12_FUA) 8746 flags |= CTL_LLF_FUA; 8747 if (cdb->byte2 & SRW12_DPO) 8748 flags |= CTL_LLF_DPO; 8749 lba = scsi_4btoul(cdb->addr); 8750 num_blocks = scsi_4btoul(cdb->length); 8751 break; 8752 } 8753 case WRITE_VERIFY_12: { 8754 struct scsi_write_verify_12 *cdb; 8755 8756 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8757 flags |= CTL_LLF_FUA; 8758 if (cdb->byte2 & SWV_DPO) 8759 flags |= CTL_LLF_DPO; 8760 lba = scsi_4btoul(cdb->addr); 8761 num_blocks = scsi_4btoul(cdb->length); 8762 break; 8763 } 8764 case READ_16: 8765 case WRITE_16: { 8766 struct scsi_rw_16 *cdb; 8767 8768 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8769 if (cdb->byte2 & SRW12_FUA) 8770 flags |= CTL_LLF_FUA; 8771 if (cdb->byte2 & SRW12_DPO) 8772 flags |= CTL_LLF_DPO; 8773 lba = scsi_8btou64(cdb->addr); 8774 num_blocks = scsi_4btoul(cdb->length); 8775 break; 8776 } 8777 case WRITE_ATOMIC_16: { 8778 struct scsi_write_atomic_16 *cdb; 8779 8780 if (lun->be_lun->atomicblock == 0) { 8781 ctl_set_invalid_opcode(ctsio); 8782 ctl_done((union ctl_io *)ctsio); 8783 return (CTL_RETVAL_COMPLETE); 8784 } 8785 8786 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8787 if (cdb->byte2 & SRW12_FUA) 8788 flags |= CTL_LLF_FUA; 8789 if (cdb->byte2 & SRW12_DPO) 8790 flags |= CTL_LLF_DPO; 8791 lba = scsi_8btou64(cdb->addr); 8792 num_blocks = scsi_2btoul(cdb->length); 8793 if (num_blocks > lun->be_lun->atomicblock) { 8794 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8795 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8796 /*bit*/ 0); 8797 ctl_done((union ctl_io *)ctsio); 8798 return (CTL_RETVAL_COMPLETE); 8799 } 8800 break; 8801 } 8802 case WRITE_VERIFY_16: { 8803 struct scsi_write_verify_16 *cdb; 8804 8805 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8806 flags |= CTL_LLF_FUA; 8807 if (cdb->byte2 & SWV_DPO) 8808 flags |= CTL_LLF_DPO; 8809 lba = scsi_8btou64(cdb->addr); 8810 num_blocks = scsi_4btoul(cdb->length); 8811 break; 8812 } 8813 default: 8814 /* 8815 * We got a command we don't support. This shouldn't 8816 * happen, commands should be filtered out above us. 8817 */ 8818 ctl_set_invalid_opcode(ctsio); 8819 ctl_done((union ctl_io *)ctsio); 8820 8821 return (CTL_RETVAL_COMPLETE); 8822 break; /* NOTREACHED */ 8823 } 8824 8825 /* 8826 * The first check is to make sure we're in bounds, the second 8827 * check is to catch wrap-around problems. If the lba + num blocks 8828 * is less than the lba, then we've wrapped around and the block 8829 * range is invalid anyway. 8830 */ 8831 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8832 || ((lba + num_blocks) < lba)) { 8833 ctl_set_lba_out_of_range(ctsio, 8834 MAX(lba, lun->be_lun->maxlba + 1)); 8835 ctl_done((union ctl_io *)ctsio); 8836 return (CTL_RETVAL_COMPLETE); 8837 } 8838 8839 /* 8840 * According to SBC-3, a transfer length of 0 is not an error. 8841 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8842 * translates to 256 blocks for those commands. 8843 */ 8844 if (num_blocks == 0) { 8845 ctl_set_success(ctsio); 8846 ctl_done((union ctl_io *)ctsio); 8847 return (CTL_RETVAL_COMPLETE); 8848 } 8849 8850 /* Set FUA and/or DPO if caches are disabled. */ 8851 if (isread) { 8852 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8853 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8854 } else { 8855 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8856 flags |= CTL_LLF_FUA; 8857 } 8858 8859 lbalen = (struct ctl_lba_len_flags *) 8860 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8861 lbalen->lba = lba; 8862 lbalen->len = num_blocks; 8863 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8864 8865 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8866 ctsio->kern_rel_offset = 0; 8867 8868 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8869 8870 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8871 return (retval); 8872 } 8873 8874 static int 8875 ctl_cnw_cont(union ctl_io *io) 8876 { 8877 struct ctl_lun *lun = CTL_LUN(io); 8878 struct ctl_scsiio *ctsio; 8879 struct ctl_lba_len_flags *lbalen; 8880 int retval; 8881 8882 ctsio = &io->scsiio; 8883 ctsio->io_hdr.status = CTL_STATUS_NONE; 8884 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8885 lbalen = (struct ctl_lba_len_flags *) 8886 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8887 lbalen->flags &= ~CTL_LLF_COMPARE; 8888 lbalen->flags |= CTL_LLF_WRITE; 8889 8890 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8891 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8892 return (retval); 8893 } 8894 8895 int 8896 ctl_cnw(struct ctl_scsiio *ctsio) 8897 { 8898 struct ctl_lun *lun = CTL_LUN(ctsio); 8899 struct ctl_lba_len_flags *lbalen; 8900 uint64_t lba; 8901 uint32_t num_blocks; 8902 int flags, retval; 8903 8904 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8905 8906 flags = 0; 8907 switch (ctsio->cdb[0]) { 8908 case COMPARE_AND_WRITE: { 8909 struct scsi_compare_and_write *cdb; 8910 8911 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8912 if (cdb->byte2 & SRW10_FUA) 8913 flags |= CTL_LLF_FUA; 8914 if (cdb->byte2 & SRW10_DPO) 8915 flags |= CTL_LLF_DPO; 8916 lba = scsi_8btou64(cdb->addr); 8917 num_blocks = cdb->length; 8918 break; 8919 } 8920 default: 8921 /* 8922 * We got a command we don't support. This shouldn't 8923 * happen, commands should be filtered out above us. 8924 */ 8925 ctl_set_invalid_opcode(ctsio); 8926 ctl_done((union ctl_io *)ctsio); 8927 8928 return (CTL_RETVAL_COMPLETE); 8929 break; /* NOTREACHED */ 8930 } 8931 8932 /* 8933 * The first check is to make sure we're in bounds, the second 8934 * check is to catch wrap-around problems. If the lba + num blocks 8935 * is less than the lba, then we've wrapped around and the block 8936 * range is invalid anyway. 8937 */ 8938 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8939 || ((lba + num_blocks) < lba)) { 8940 ctl_set_lba_out_of_range(ctsio, 8941 MAX(lba, lun->be_lun->maxlba + 1)); 8942 ctl_done((union ctl_io *)ctsio); 8943 return (CTL_RETVAL_COMPLETE); 8944 } 8945 8946 /* 8947 * According to SBC-3, a transfer length of 0 is not an error. 8948 */ 8949 if (num_blocks == 0) { 8950 ctl_set_success(ctsio); 8951 ctl_done((union ctl_io *)ctsio); 8952 return (CTL_RETVAL_COMPLETE); 8953 } 8954 8955 /* Set FUA if write cache is disabled. */ 8956 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8957 flags |= CTL_LLF_FUA; 8958 8959 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8960 ctsio->kern_rel_offset = 0; 8961 8962 /* 8963 * Set the IO_CONT flag, so that if this I/O gets passed to 8964 * ctl_data_submit_done(), it'll get passed back to 8965 * ctl_ctl_cnw_cont() for further processing. 8966 */ 8967 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8968 ctsio->io_cont = ctl_cnw_cont; 8969 8970 lbalen = (struct ctl_lba_len_flags *) 8971 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8972 lbalen->lba = lba; 8973 lbalen->len = num_blocks; 8974 lbalen->flags = CTL_LLF_COMPARE | flags; 8975 8976 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8977 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8978 return (retval); 8979 } 8980 8981 int 8982 ctl_verify(struct ctl_scsiio *ctsio) 8983 { 8984 struct ctl_lun *lun = CTL_LUN(ctsio); 8985 struct ctl_lba_len_flags *lbalen; 8986 uint64_t lba; 8987 uint32_t num_blocks; 8988 int bytchk, flags; 8989 int retval; 8990 8991 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8992 8993 bytchk = 0; 8994 flags = CTL_LLF_FUA; 8995 switch (ctsio->cdb[0]) { 8996 case VERIFY_10: { 8997 struct scsi_verify_10 *cdb; 8998 8999 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9000 if (cdb->byte2 & SVFY_BYTCHK) 9001 bytchk = 1; 9002 if (cdb->byte2 & SVFY_DPO) 9003 flags |= CTL_LLF_DPO; 9004 lba = scsi_4btoul(cdb->addr); 9005 num_blocks = scsi_2btoul(cdb->length); 9006 break; 9007 } 9008 case VERIFY_12: { 9009 struct scsi_verify_12 *cdb; 9010 9011 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9012 if (cdb->byte2 & SVFY_BYTCHK) 9013 bytchk = 1; 9014 if (cdb->byte2 & SVFY_DPO) 9015 flags |= CTL_LLF_DPO; 9016 lba = scsi_4btoul(cdb->addr); 9017 num_blocks = scsi_4btoul(cdb->length); 9018 break; 9019 } 9020 case VERIFY_16: { 9021 struct scsi_rw_16 *cdb; 9022 9023 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9024 if (cdb->byte2 & SVFY_BYTCHK) 9025 bytchk = 1; 9026 if (cdb->byte2 & SVFY_DPO) 9027 flags |= CTL_LLF_DPO; 9028 lba = scsi_8btou64(cdb->addr); 9029 num_blocks = scsi_4btoul(cdb->length); 9030 break; 9031 } 9032 default: 9033 /* 9034 * We got a command we don't support. This shouldn't 9035 * happen, commands should be filtered out above us. 9036 */ 9037 ctl_set_invalid_opcode(ctsio); 9038 ctl_done((union ctl_io *)ctsio); 9039 return (CTL_RETVAL_COMPLETE); 9040 } 9041 9042 /* 9043 * The first check is to make sure we're in bounds, the second 9044 * check is to catch wrap-around problems. If the lba + num blocks 9045 * is less than the lba, then we've wrapped around and the block 9046 * range is invalid anyway. 9047 */ 9048 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9049 || ((lba + num_blocks) < lba)) { 9050 ctl_set_lba_out_of_range(ctsio, 9051 MAX(lba, lun->be_lun->maxlba + 1)); 9052 ctl_done((union ctl_io *)ctsio); 9053 return (CTL_RETVAL_COMPLETE); 9054 } 9055 9056 /* 9057 * According to SBC-3, a transfer length of 0 is not an error. 9058 */ 9059 if (num_blocks == 0) { 9060 ctl_set_success(ctsio); 9061 ctl_done((union ctl_io *)ctsio); 9062 return (CTL_RETVAL_COMPLETE); 9063 } 9064 9065 lbalen = (struct ctl_lba_len_flags *) 9066 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9067 lbalen->lba = lba; 9068 lbalen->len = num_blocks; 9069 if (bytchk) { 9070 lbalen->flags = CTL_LLF_COMPARE | flags; 9071 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9072 } else { 9073 lbalen->flags = CTL_LLF_VERIFY | flags; 9074 ctsio->kern_total_len = 0; 9075 } 9076 ctsio->kern_rel_offset = 0; 9077 9078 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9079 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9080 return (retval); 9081 } 9082 9083 int 9084 ctl_report_luns(struct ctl_scsiio *ctsio) 9085 { 9086 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9087 struct ctl_port *port = CTL_PORT(ctsio); 9088 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9089 struct scsi_report_luns *cdb; 9090 struct scsi_report_luns_data *lun_data; 9091 int num_filled, num_luns, num_port_luns, retval; 9092 uint32_t alloc_len, lun_datalen; 9093 uint32_t initidx, targ_lun_id, lun_id; 9094 9095 retval = CTL_RETVAL_COMPLETE; 9096 cdb = (struct scsi_report_luns *)ctsio->cdb; 9097 9098 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9099 9100 num_luns = 0; 9101 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9102 mtx_lock(&softc->ctl_lock); 9103 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9104 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9105 num_luns++; 9106 } 9107 mtx_unlock(&softc->ctl_lock); 9108 9109 switch (cdb->select_report) { 9110 case RPL_REPORT_DEFAULT: 9111 case RPL_REPORT_ALL: 9112 case RPL_REPORT_NONSUBSID: 9113 break; 9114 case RPL_REPORT_WELLKNOWN: 9115 case RPL_REPORT_ADMIN: 9116 case RPL_REPORT_CONGLOM: 9117 num_luns = 0; 9118 break; 9119 default: 9120 ctl_set_invalid_field(ctsio, 9121 /*sks_valid*/ 1, 9122 /*command*/ 1, 9123 /*field*/ 2, 9124 /*bit_valid*/ 0, 9125 /*bit*/ 0); 9126 ctl_done((union ctl_io *)ctsio); 9127 return (retval); 9128 break; /* NOTREACHED */ 9129 } 9130 9131 alloc_len = scsi_4btoul(cdb->length); 9132 /* 9133 * The initiator has to allocate at least 16 bytes for this request, 9134 * so he can at least get the header and the first LUN. Otherwise 9135 * we reject the request (per SPC-3 rev 14, section 6.21). 9136 */ 9137 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9138 sizeof(struct scsi_report_luns_lundata))) { 9139 ctl_set_invalid_field(ctsio, 9140 /*sks_valid*/ 1, 9141 /*command*/ 1, 9142 /*field*/ 6, 9143 /*bit_valid*/ 0, 9144 /*bit*/ 0); 9145 ctl_done((union ctl_io *)ctsio); 9146 return (retval); 9147 } 9148 9149 lun_datalen = sizeof(*lun_data) + 9150 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9151 9152 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9153 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9154 ctsio->kern_sg_entries = 0; 9155 9156 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9157 9158 mtx_lock(&softc->ctl_lock); 9159 for (targ_lun_id = 0, num_filled = 0; 9160 targ_lun_id < num_port_luns && num_filled < num_luns; 9161 targ_lun_id++) { 9162 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9163 if (lun_id == UINT32_MAX) 9164 continue; 9165 lun = softc->ctl_luns[lun_id]; 9166 if (lun == NULL) 9167 continue; 9168 9169 be64enc(lun_data->luns[num_filled++].lundata, 9170 ctl_encode_lun(targ_lun_id)); 9171 9172 /* 9173 * According to SPC-3, rev 14 section 6.21: 9174 * 9175 * "The execution of a REPORT LUNS command to any valid and 9176 * installed logical unit shall clear the REPORTED LUNS DATA 9177 * HAS CHANGED unit attention condition for all logical 9178 * units of that target with respect to the requesting 9179 * initiator. A valid and installed logical unit is one 9180 * having a PERIPHERAL QUALIFIER of 000b in the standard 9181 * INQUIRY data (see 6.4.2)." 9182 * 9183 * If request_lun is NULL, the LUN this report luns command 9184 * was issued to is either disabled or doesn't exist. In that 9185 * case, we shouldn't clear any pending lun change unit 9186 * attention. 9187 */ 9188 if (request_lun != NULL) { 9189 mtx_lock(&lun->lun_lock); 9190 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9191 mtx_unlock(&lun->lun_lock); 9192 } 9193 } 9194 mtx_unlock(&softc->ctl_lock); 9195 9196 /* 9197 * It's quite possible that we've returned fewer LUNs than we allocated 9198 * space for. Trim it. 9199 */ 9200 lun_datalen = sizeof(*lun_data) + 9201 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9202 ctsio->kern_rel_offset = 0; 9203 ctsio->kern_sg_entries = 0; 9204 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9205 ctsio->kern_total_len = ctsio->kern_data_len; 9206 9207 /* 9208 * We set this to the actual data length, regardless of how much 9209 * space we actually have to return results. If the user looks at 9210 * this value, he'll know whether or not he allocated enough space 9211 * and reissue the command if necessary. We don't support well 9212 * known logical units, so if the user asks for that, return none. 9213 */ 9214 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9215 9216 /* 9217 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9218 * this request. 9219 */ 9220 ctl_set_success(ctsio); 9221 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9222 ctsio->be_move_done = ctl_config_move_done; 9223 ctl_datamove((union ctl_io *)ctsio); 9224 return (retval); 9225 } 9226 9227 int 9228 ctl_request_sense(struct ctl_scsiio *ctsio) 9229 { 9230 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9231 struct ctl_lun *lun = CTL_LUN(ctsio); 9232 struct scsi_request_sense *cdb; 9233 struct scsi_sense_data *sense_ptr, *ps; 9234 uint32_t initidx; 9235 int have_error; 9236 u_int sense_len = SSD_FULL_SIZE; 9237 scsi_sense_data_type sense_format; 9238 ctl_ua_type ua_type; 9239 uint8_t asc = 0, ascq = 0; 9240 9241 cdb = (struct scsi_request_sense *)ctsio->cdb; 9242 9243 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9244 9245 /* 9246 * Determine which sense format the user wants. 9247 */ 9248 if (cdb->byte2 & SRS_DESC) 9249 sense_format = SSD_TYPE_DESC; 9250 else 9251 sense_format = SSD_TYPE_FIXED; 9252 9253 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9254 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9255 ctsio->kern_sg_entries = 0; 9256 ctsio->kern_rel_offset = 0; 9257 9258 /* 9259 * struct scsi_sense_data, which is currently set to 256 bytes, is 9260 * larger than the largest allowed value for the length field in the 9261 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9262 */ 9263 ctsio->kern_data_len = cdb->length; 9264 ctsio->kern_total_len = cdb->length; 9265 9266 /* 9267 * If we don't have a LUN, we don't have any pending sense. 9268 */ 9269 if (lun == NULL || 9270 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9271 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9272 /* "Logical unit not supported" */ 9273 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9274 /*current_error*/ 1, 9275 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9276 /*asc*/ 0x25, 9277 /*ascq*/ 0x00, 9278 SSD_ELEM_NONE); 9279 goto send; 9280 } 9281 9282 have_error = 0; 9283 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9284 /* 9285 * Check for pending sense, and then for pending unit attentions. 9286 * Pending sense gets returned first, then pending unit attentions. 9287 */ 9288 mtx_lock(&lun->lun_lock); 9289 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9290 if (ps != NULL) 9291 ps += initidx % CTL_MAX_INIT_PER_PORT; 9292 if (ps != NULL && ps->error_code != 0) { 9293 scsi_sense_data_type stored_format; 9294 9295 /* 9296 * Check to see which sense format was used for the stored 9297 * sense data. 9298 */ 9299 stored_format = scsi_sense_type(ps); 9300 9301 /* 9302 * If the user requested a different sense format than the 9303 * one we stored, then we need to convert it to the other 9304 * format. If we're going from descriptor to fixed format 9305 * sense data, we may lose things in translation, depending 9306 * on what options were used. 9307 * 9308 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9309 * for some reason we'll just copy it out as-is. 9310 */ 9311 if ((stored_format == SSD_TYPE_FIXED) 9312 && (sense_format == SSD_TYPE_DESC)) 9313 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9314 ps, (struct scsi_sense_data_desc *)sense_ptr); 9315 else if ((stored_format == SSD_TYPE_DESC) 9316 && (sense_format == SSD_TYPE_FIXED)) 9317 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9318 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9319 else 9320 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9321 9322 ps->error_code = 0; 9323 have_error = 1; 9324 } else { 9325 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9326 sense_format); 9327 if (ua_type != CTL_UA_NONE) 9328 have_error = 1; 9329 } 9330 if (have_error == 0) { 9331 /* 9332 * Report informational exception if have one and allowed. 9333 */ 9334 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9335 asc = lun->ie_asc; 9336 ascq = lun->ie_ascq; 9337 } 9338 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9339 /*current_error*/ 1, 9340 /*sense_key*/ SSD_KEY_NO_SENSE, 9341 /*asc*/ asc, 9342 /*ascq*/ ascq, 9343 SSD_ELEM_NONE); 9344 } 9345 mtx_unlock(&lun->lun_lock); 9346 9347 send: 9348 /* 9349 * We report the SCSI status as OK, since the status of the command 9350 * itself is OK. We're reporting sense as parameter data. 9351 */ 9352 ctl_set_success(ctsio); 9353 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9354 ctsio->be_move_done = ctl_config_move_done; 9355 ctl_datamove((union ctl_io *)ctsio); 9356 return (CTL_RETVAL_COMPLETE); 9357 } 9358 9359 int 9360 ctl_tur(struct ctl_scsiio *ctsio) 9361 { 9362 9363 CTL_DEBUG_PRINT(("ctl_tur\n")); 9364 9365 ctl_set_success(ctsio); 9366 ctl_done((union ctl_io *)ctsio); 9367 9368 return (CTL_RETVAL_COMPLETE); 9369 } 9370 9371 /* 9372 * SCSI VPD page 0x00, the Supported VPD Pages page. 9373 */ 9374 static int 9375 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9376 { 9377 struct ctl_lun *lun = CTL_LUN(ctsio); 9378 struct scsi_vpd_supported_pages *pages; 9379 int sup_page_size; 9380 int p; 9381 9382 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9383 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9384 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9385 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9386 ctsio->kern_rel_offset = 0; 9387 ctsio->kern_sg_entries = 0; 9388 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9389 ctsio->kern_total_len = ctsio->kern_data_len; 9390 9391 /* 9392 * The control device is always connected. The disk device, on the 9393 * other hand, may not be online all the time. Need to change this 9394 * to figure out whether the disk device is actually online or not. 9395 */ 9396 if (lun != NULL) 9397 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9398 lun->be_lun->lun_type; 9399 else 9400 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9401 9402 p = 0; 9403 /* Supported VPD pages */ 9404 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9405 /* Serial Number */ 9406 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9407 /* Device Identification */ 9408 pages->page_list[p++] = SVPD_DEVICE_ID; 9409 /* Extended INQUIRY Data */ 9410 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9411 /* Mode Page Policy */ 9412 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9413 /* SCSI Ports */ 9414 pages->page_list[p++] = SVPD_SCSI_PORTS; 9415 /* Third-party Copy */ 9416 pages->page_list[p++] = SVPD_SCSI_TPC; 9417 /* SCSI Feature Sets */ 9418 pages->page_list[p++] = SVPD_SCSI_SFS; 9419 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9420 /* Block limits */ 9421 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9422 /* Block Device Characteristics */ 9423 pages->page_list[p++] = SVPD_BDC; 9424 /* Logical Block Provisioning */ 9425 pages->page_list[p++] = SVPD_LBP; 9426 } 9427 pages->length = p; 9428 9429 ctl_set_success(ctsio); 9430 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9431 ctsio->be_move_done = ctl_config_move_done; 9432 ctl_datamove((union ctl_io *)ctsio); 9433 return (CTL_RETVAL_COMPLETE); 9434 } 9435 9436 /* 9437 * SCSI VPD page 0x80, the Unit Serial Number page. 9438 */ 9439 static int 9440 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9441 { 9442 struct ctl_lun *lun = CTL_LUN(ctsio); 9443 struct scsi_vpd_unit_serial_number *sn_ptr; 9444 int data_len; 9445 9446 data_len = 4 + CTL_SN_LEN; 9447 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9448 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9449 ctsio->kern_rel_offset = 0; 9450 ctsio->kern_sg_entries = 0; 9451 ctsio->kern_data_len = min(data_len, alloc_len); 9452 ctsio->kern_total_len = ctsio->kern_data_len; 9453 9454 /* 9455 * The control device is always connected. The disk device, on the 9456 * other hand, may not be online all the time. Need to change this 9457 * to figure out whether the disk device is actually online or not. 9458 */ 9459 if (lun != NULL) 9460 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9461 lun->be_lun->lun_type; 9462 else 9463 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9464 9465 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9466 sn_ptr->length = CTL_SN_LEN; 9467 /* 9468 * If we don't have a LUN, we just leave the serial number as 9469 * all spaces. 9470 */ 9471 if (lun != NULL) { 9472 strncpy((char *)sn_ptr->serial_num, 9473 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9474 } else 9475 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9476 9477 ctl_set_success(ctsio); 9478 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9479 ctsio->be_move_done = ctl_config_move_done; 9480 ctl_datamove((union ctl_io *)ctsio); 9481 return (CTL_RETVAL_COMPLETE); 9482 } 9483 9484 /* 9485 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9486 */ 9487 static int 9488 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9489 { 9490 struct ctl_lun *lun = CTL_LUN(ctsio); 9491 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9492 int data_len; 9493 9494 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9495 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9496 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9497 ctsio->kern_sg_entries = 0; 9498 ctsio->kern_rel_offset = 0; 9499 ctsio->kern_data_len = min(data_len, alloc_len); 9500 ctsio->kern_total_len = ctsio->kern_data_len; 9501 9502 /* 9503 * The control device is always connected. The disk device, on the 9504 * other hand, may not be online all the time. 9505 */ 9506 if (lun != NULL) 9507 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9508 lun->be_lun->lun_type; 9509 else 9510 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9511 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9512 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9513 /* 9514 * We support head of queue, ordered and simple tags. 9515 */ 9516 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9517 /* 9518 * Volatile cache supported. 9519 */ 9520 eid_ptr->flags3 = SVPD_EID_V_SUP; 9521 9522 /* 9523 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9524 * attention for a particular IT nexus on all LUNs once we report 9525 * it to that nexus once. This bit is required as of SPC-4. 9526 */ 9527 eid_ptr->flags4 = SVPD_EID_LUICLR; 9528 9529 /* 9530 * We support revert to defaults (RTD) bit in MODE SELECT. 9531 */ 9532 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9533 9534 /* 9535 * XXX KDM in order to correctly answer this, we would need 9536 * information from the SIM to determine how much sense data it 9537 * can send. So this would really be a path inquiry field, most 9538 * likely. This can be set to a maximum of 252 according to SPC-4, 9539 * but the hardware may or may not be able to support that much. 9540 * 0 just means that the maximum sense data length is not reported. 9541 */ 9542 eid_ptr->max_sense_length = 0; 9543 9544 ctl_set_success(ctsio); 9545 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9546 ctsio->be_move_done = ctl_config_move_done; 9547 ctl_datamove((union ctl_io *)ctsio); 9548 return (CTL_RETVAL_COMPLETE); 9549 } 9550 9551 static int 9552 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9553 { 9554 struct ctl_lun *lun = CTL_LUN(ctsio); 9555 struct scsi_vpd_mode_page_policy *mpp_ptr; 9556 int data_len; 9557 9558 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9559 sizeof(struct scsi_vpd_mode_page_policy_descr); 9560 9561 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9562 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9563 ctsio->kern_rel_offset = 0; 9564 ctsio->kern_sg_entries = 0; 9565 ctsio->kern_data_len = min(data_len, alloc_len); 9566 ctsio->kern_total_len = ctsio->kern_data_len; 9567 9568 /* 9569 * The control device is always connected. The disk device, on the 9570 * other hand, may not be online all the time. 9571 */ 9572 if (lun != NULL) 9573 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9574 lun->be_lun->lun_type; 9575 else 9576 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9577 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9578 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9579 mpp_ptr->descr[0].page_code = 0x3f; 9580 mpp_ptr->descr[0].subpage_code = 0xff; 9581 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9582 9583 ctl_set_success(ctsio); 9584 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9585 ctsio->be_move_done = ctl_config_move_done; 9586 ctl_datamove((union ctl_io *)ctsio); 9587 return (CTL_RETVAL_COMPLETE); 9588 } 9589 9590 /* 9591 * SCSI VPD page 0x83, the Device Identification page. 9592 */ 9593 static int 9594 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9595 { 9596 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9597 struct ctl_port *port = CTL_PORT(ctsio); 9598 struct ctl_lun *lun = CTL_LUN(ctsio); 9599 struct scsi_vpd_device_id *devid_ptr; 9600 struct scsi_vpd_id_descriptor *desc; 9601 int data_len, g; 9602 uint8_t proto; 9603 9604 data_len = sizeof(struct scsi_vpd_device_id) + 9605 sizeof(struct scsi_vpd_id_descriptor) + 9606 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9607 sizeof(struct scsi_vpd_id_descriptor) + 9608 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9609 if (lun && lun->lun_devid) 9610 data_len += lun->lun_devid->len; 9611 if (port && port->port_devid) 9612 data_len += port->port_devid->len; 9613 if (port && port->target_devid) 9614 data_len += port->target_devid->len; 9615 9616 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9617 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9618 ctsio->kern_sg_entries = 0; 9619 ctsio->kern_rel_offset = 0; 9620 ctsio->kern_sg_entries = 0; 9621 ctsio->kern_data_len = min(data_len, alloc_len); 9622 ctsio->kern_total_len = ctsio->kern_data_len; 9623 9624 /* 9625 * The control device is always connected. The disk device, on the 9626 * other hand, may not be online all the time. 9627 */ 9628 if (lun != NULL) 9629 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9630 lun->be_lun->lun_type; 9631 else 9632 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9633 devid_ptr->page_code = SVPD_DEVICE_ID; 9634 scsi_ulto2b(data_len - 4, devid_ptr->length); 9635 9636 if (port && port->port_type == CTL_PORT_FC) 9637 proto = SCSI_PROTO_FC << 4; 9638 else if (port && port->port_type == CTL_PORT_SAS) 9639 proto = SCSI_PROTO_SAS << 4; 9640 else if (port && port->port_type == CTL_PORT_ISCSI) 9641 proto = SCSI_PROTO_ISCSI << 4; 9642 else 9643 proto = SCSI_PROTO_SPI << 4; 9644 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9645 9646 /* 9647 * We're using a LUN association here. i.e., this device ID is a 9648 * per-LUN identifier. 9649 */ 9650 if (lun && lun->lun_devid) { 9651 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9652 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9653 lun->lun_devid->len); 9654 } 9655 9656 /* 9657 * This is for the WWPN which is a port association. 9658 */ 9659 if (port && port->port_devid) { 9660 memcpy(desc, port->port_devid->data, port->port_devid->len); 9661 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9662 port->port_devid->len); 9663 } 9664 9665 /* 9666 * This is for the Relative Target Port(type 4h) identifier 9667 */ 9668 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9669 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9670 SVPD_ID_TYPE_RELTARG; 9671 desc->length = 4; 9672 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9673 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9674 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9675 9676 /* 9677 * This is for the Target Port Group(type 5h) identifier 9678 */ 9679 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9680 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9681 SVPD_ID_TYPE_TPORTGRP; 9682 desc->length = 4; 9683 if (softc->is_single || 9684 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9685 g = 1; 9686 else 9687 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9688 scsi_ulto2b(g, &desc->identifier[2]); 9689 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9690 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9691 9692 /* 9693 * This is for the Target identifier 9694 */ 9695 if (port && port->target_devid) { 9696 memcpy(desc, port->target_devid->data, port->target_devid->len); 9697 } 9698 9699 ctl_set_success(ctsio); 9700 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9701 ctsio->be_move_done = ctl_config_move_done; 9702 ctl_datamove((union ctl_io *)ctsio); 9703 return (CTL_RETVAL_COMPLETE); 9704 } 9705 9706 static int 9707 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9708 { 9709 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9710 struct ctl_lun *lun = CTL_LUN(ctsio); 9711 struct scsi_vpd_scsi_ports *sp; 9712 struct scsi_vpd_port_designation *pd; 9713 struct scsi_vpd_port_designation_cont *pdc; 9714 struct ctl_port *port; 9715 int data_len, num_target_ports, iid_len, id_len; 9716 9717 num_target_ports = 0; 9718 iid_len = 0; 9719 id_len = 0; 9720 mtx_lock(&softc->ctl_lock); 9721 STAILQ_FOREACH(port, &softc->port_list, links) { 9722 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9723 continue; 9724 if (lun != NULL && 9725 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9726 continue; 9727 num_target_ports++; 9728 if (port->init_devid) 9729 iid_len += port->init_devid->len; 9730 if (port->port_devid) 9731 id_len += port->port_devid->len; 9732 } 9733 mtx_unlock(&softc->ctl_lock); 9734 9735 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9736 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9737 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9738 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9739 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9740 ctsio->kern_sg_entries = 0; 9741 ctsio->kern_rel_offset = 0; 9742 ctsio->kern_sg_entries = 0; 9743 ctsio->kern_data_len = min(data_len, alloc_len); 9744 ctsio->kern_total_len = ctsio->kern_data_len; 9745 9746 /* 9747 * The control device is always connected. The disk device, on the 9748 * other hand, may not be online all the time. Need to change this 9749 * to figure out whether the disk device is actually online or not. 9750 */ 9751 if (lun != NULL) 9752 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9753 lun->be_lun->lun_type; 9754 else 9755 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9756 9757 sp->page_code = SVPD_SCSI_PORTS; 9758 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9759 sp->page_length); 9760 pd = &sp->design[0]; 9761 9762 mtx_lock(&softc->ctl_lock); 9763 STAILQ_FOREACH(port, &softc->port_list, links) { 9764 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9765 continue; 9766 if (lun != NULL && 9767 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9768 continue; 9769 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9770 if (port->init_devid) { 9771 iid_len = port->init_devid->len; 9772 memcpy(pd->initiator_transportid, 9773 port->init_devid->data, port->init_devid->len); 9774 } else 9775 iid_len = 0; 9776 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9777 pdc = (struct scsi_vpd_port_designation_cont *) 9778 (&pd->initiator_transportid[iid_len]); 9779 if (port->port_devid) { 9780 id_len = port->port_devid->len; 9781 memcpy(pdc->target_port_descriptors, 9782 port->port_devid->data, port->port_devid->len); 9783 } else 9784 id_len = 0; 9785 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9786 pd = (struct scsi_vpd_port_designation *) 9787 ((uint8_t *)pdc->target_port_descriptors + id_len); 9788 } 9789 mtx_unlock(&softc->ctl_lock); 9790 9791 ctl_set_success(ctsio); 9792 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9793 ctsio->be_move_done = ctl_config_move_done; 9794 ctl_datamove((union ctl_io *)ctsio); 9795 return (CTL_RETVAL_COMPLETE); 9796 } 9797 9798 static int 9799 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len) 9800 { 9801 struct ctl_lun *lun = CTL_LUN(ctsio); 9802 struct scsi_vpd_sfs *sfs_ptr; 9803 int sfs_page_size, n; 9804 9805 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2; 9806 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO); 9807 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr; 9808 ctsio->kern_sg_entries = 0; 9809 ctsio->kern_rel_offset = 0; 9810 ctsio->kern_sg_entries = 0; 9811 ctsio->kern_data_len = min(sfs_page_size, alloc_len); 9812 ctsio->kern_total_len = ctsio->kern_data_len; 9813 9814 /* 9815 * The control device is always connected. The disk device, on the 9816 * other hand, may not be online all the time. Need to change this 9817 * to figure out whether the disk device is actually online or not. 9818 */ 9819 if (lun != NULL) 9820 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9821 lun->be_lun->lun_type; 9822 else 9823 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9824 9825 sfs_ptr->page_code = SVPD_SCSI_SFS; 9826 n = 0; 9827 /* Discovery 2016 */ 9828 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]); 9829 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9830 /* SBC Base 2016 */ 9831 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]); 9832 /* SBC Base 2010 */ 9833 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]); 9834 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9835 /* Basic Provisioning 2016 */ 9836 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]); 9837 } 9838 /* Drive Maintenance 2016 */ 9839 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]); 9840 } 9841 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length); 9842 9843 ctl_set_success(ctsio); 9844 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9845 ctsio->be_move_done = ctl_config_move_done; 9846 ctl_datamove((union ctl_io *)ctsio); 9847 return (CTL_RETVAL_COMPLETE); 9848 } 9849 9850 static int 9851 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9852 { 9853 struct ctl_lun *lun = CTL_LUN(ctsio); 9854 struct scsi_vpd_block_limits *bl_ptr; 9855 const char *val; 9856 uint64_t ival; 9857 9858 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9859 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9860 ctsio->kern_sg_entries = 0; 9861 ctsio->kern_rel_offset = 0; 9862 ctsio->kern_sg_entries = 0; 9863 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9864 ctsio->kern_total_len = ctsio->kern_data_len; 9865 9866 /* 9867 * The control device is always connected. The disk device, on the 9868 * other hand, may not be online all the time. Need to change this 9869 * to figure out whether the disk device is actually online or not. 9870 */ 9871 if (lun != NULL) 9872 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9873 lun->be_lun->lun_type; 9874 else 9875 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9876 9877 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9878 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9879 bl_ptr->max_cmp_write_len = 0xff; 9880 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9881 if (lun != NULL) { 9882 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9883 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9884 ival = 0xffffffff; 9885 val = dnvlist_get_string(lun->be_lun->options, 9886 "unmap_max_lba", NULL); 9887 if (val != NULL) 9888 ctl_expand_number(val, &ival); 9889 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9890 ival = 0xffffffff; 9891 val = dnvlist_get_string(lun->be_lun->options, 9892 "unmap_max_descr", NULL); 9893 if (val != NULL) 9894 ctl_expand_number(val, &ival); 9895 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9896 if (lun->be_lun->ublockexp != 0) { 9897 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9898 bl_ptr->opt_unmap_grain); 9899 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9900 bl_ptr->unmap_grain_align); 9901 } 9902 } 9903 scsi_ulto4b(lun->be_lun->atomicblock, 9904 bl_ptr->max_atomic_transfer_length); 9905 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9906 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9907 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9908 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9909 ival = UINT64_MAX; 9910 val = dnvlist_get_string(lun->be_lun->options, 9911 "write_same_max_lba", NULL); 9912 if (val != NULL) 9913 ctl_expand_number(val, &ival); 9914 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9915 if (lun->be_lun->maxlba + 1 > ival) 9916 bl_ptr->flags |= SVPD_BL_WSNZ; 9917 } 9918 9919 ctl_set_success(ctsio); 9920 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9921 ctsio->be_move_done = ctl_config_move_done; 9922 ctl_datamove((union ctl_io *)ctsio); 9923 return (CTL_RETVAL_COMPLETE); 9924 } 9925 9926 static int 9927 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9928 { 9929 struct ctl_lun *lun = CTL_LUN(ctsio); 9930 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9931 const char *value; 9932 u_int i; 9933 9934 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9935 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9936 ctsio->kern_sg_entries = 0; 9937 ctsio->kern_rel_offset = 0; 9938 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 9939 ctsio->kern_total_len = ctsio->kern_data_len; 9940 9941 /* 9942 * The control device is always connected. The disk device, on the 9943 * other hand, may not be online all the time. Need to change this 9944 * to figure out whether the disk device is actually online or not. 9945 */ 9946 if (lun != NULL) 9947 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9948 lun->be_lun->lun_type; 9949 else 9950 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9951 bdc_ptr->page_code = SVPD_BDC; 9952 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9953 if (lun != NULL && 9954 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 9955 i = strtol(value, NULL, 0); 9956 else 9957 i = CTL_DEFAULT_ROTATION_RATE; 9958 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9959 if (lun != NULL && 9960 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 9961 i = strtol(value, NULL, 0); 9962 else 9963 i = 0; 9964 bdc_ptr->wab_wac_ff = (i & 0x0f); 9965 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS; 9966 9967 ctl_set_success(ctsio); 9968 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9969 ctsio->be_move_done = ctl_config_move_done; 9970 ctl_datamove((union ctl_io *)ctsio); 9971 return (CTL_RETVAL_COMPLETE); 9972 } 9973 9974 static int 9975 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9976 { 9977 struct ctl_lun *lun = CTL_LUN(ctsio); 9978 struct scsi_vpd_logical_block_prov *lbp_ptr; 9979 const char *value; 9980 9981 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9982 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9983 ctsio->kern_sg_entries = 0; 9984 ctsio->kern_rel_offset = 0; 9985 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 9986 ctsio->kern_total_len = ctsio->kern_data_len; 9987 9988 /* 9989 * The control device is always connected. The disk device, on the 9990 * other hand, may not be online all the time. Need to change this 9991 * to figure out whether the disk device is actually online or not. 9992 */ 9993 if (lun != NULL) 9994 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9995 lun->be_lun->lun_type; 9996 else 9997 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9998 9999 lbp_ptr->page_code = SVPD_LBP; 10000 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10001 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10002 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10003 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10004 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10005 value = dnvlist_get_string(lun->be_lun->options, 10006 "provisioning_type", NULL); 10007 if (value != NULL) { 10008 if (strcmp(value, "resource") == 0) 10009 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10010 else if (strcmp(value, "thin") == 0) 10011 lbp_ptr->prov_type = SVPD_LBP_THIN; 10012 } else 10013 lbp_ptr->prov_type = SVPD_LBP_THIN; 10014 } 10015 10016 ctl_set_success(ctsio); 10017 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10018 ctsio->be_move_done = ctl_config_move_done; 10019 ctl_datamove((union ctl_io *)ctsio); 10020 return (CTL_RETVAL_COMPLETE); 10021 } 10022 10023 /* 10024 * INQUIRY with the EVPD bit set. 10025 */ 10026 static int 10027 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10028 { 10029 struct ctl_lun *lun = CTL_LUN(ctsio); 10030 struct scsi_inquiry *cdb; 10031 int alloc_len, retval; 10032 10033 cdb = (struct scsi_inquiry *)ctsio->cdb; 10034 alloc_len = scsi_2btoul(cdb->length); 10035 10036 switch (cdb->page_code) { 10037 case SVPD_SUPPORTED_PAGES: 10038 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10039 break; 10040 case SVPD_UNIT_SERIAL_NUMBER: 10041 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10042 break; 10043 case SVPD_DEVICE_ID: 10044 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10045 break; 10046 case SVPD_EXTENDED_INQUIRY_DATA: 10047 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10048 break; 10049 case SVPD_MODE_PAGE_POLICY: 10050 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10051 break; 10052 case SVPD_SCSI_PORTS: 10053 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10054 break; 10055 case SVPD_SCSI_TPC: 10056 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10057 break; 10058 case SVPD_SCSI_SFS: 10059 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len); 10060 break; 10061 case SVPD_BLOCK_LIMITS: 10062 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10063 goto err; 10064 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10065 break; 10066 case SVPD_BDC: 10067 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10068 goto err; 10069 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10070 break; 10071 case SVPD_LBP: 10072 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10073 goto err; 10074 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10075 break; 10076 default: 10077 err: 10078 ctl_set_invalid_field(ctsio, 10079 /*sks_valid*/ 1, 10080 /*command*/ 1, 10081 /*field*/ 2, 10082 /*bit_valid*/ 0, 10083 /*bit*/ 0); 10084 ctl_done((union ctl_io *)ctsio); 10085 retval = CTL_RETVAL_COMPLETE; 10086 break; 10087 } 10088 10089 return (retval); 10090 } 10091 10092 /* 10093 * Standard INQUIRY data. 10094 */ 10095 static int 10096 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10097 { 10098 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10099 struct ctl_port *port = CTL_PORT(ctsio); 10100 struct ctl_lun *lun = CTL_LUN(ctsio); 10101 struct scsi_inquiry_data *inq_ptr; 10102 struct scsi_inquiry *cdb; 10103 const char *val; 10104 uint32_t alloc_len, data_len; 10105 ctl_port_type port_type; 10106 10107 port_type = port->port_type; 10108 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10109 port_type = CTL_PORT_SCSI; 10110 10111 cdb = (struct scsi_inquiry *)ctsio->cdb; 10112 alloc_len = scsi_2btoul(cdb->length); 10113 10114 /* 10115 * We malloc the full inquiry data size here and fill it 10116 * in. If the user only asks for less, we'll give him 10117 * that much. 10118 */ 10119 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10120 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10121 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10122 ctsio->kern_sg_entries = 0; 10123 ctsio->kern_rel_offset = 0; 10124 ctsio->kern_data_len = min(data_len, alloc_len); 10125 ctsio->kern_total_len = ctsio->kern_data_len; 10126 10127 if (lun != NULL) { 10128 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10129 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10130 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10131 lun->be_lun->lun_type; 10132 } else { 10133 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10134 lun->be_lun->lun_type; 10135 } 10136 if (lun->flags & CTL_LUN_REMOVABLE) 10137 inq_ptr->dev_qual2 |= SID_RMB; 10138 } else 10139 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10140 10141 /* RMB in byte 2 is 0 */ 10142 inq_ptr->version = SCSI_REV_SPC5; 10143 10144 /* 10145 * According to SAM-3, even if a device only supports a single 10146 * level of LUN addressing, it should still set the HISUP bit: 10147 * 10148 * 4.9.1 Logical unit numbers overview 10149 * 10150 * All logical unit number formats described in this standard are 10151 * hierarchical in structure even when only a single level in that 10152 * hierarchy is used. The HISUP bit shall be set to one in the 10153 * standard INQUIRY data (see SPC-2) when any logical unit number 10154 * format described in this standard is used. Non-hierarchical 10155 * formats are outside the scope of this standard. 10156 * 10157 * Therefore we set the HiSup bit here. 10158 * 10159 * The response format is 2, per SPC-3. 10160 */ 10161 inq_ptr->response_format = SID_HiSup | 2; 10162 10163 inq_ptr->additional_length = data_len - 10164 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10165 CTL_DEBUG_PRINT(("additional_length = %d\n", 10166 inq_ptr->additional_length)); 10167 10168 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10169 if (port_type == CTL_PORT_SCSI) 10170 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10171 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10172 inq_ptr->flags = SID_CmdQue; 10173 if (port_type == CTL_PORT_SCSI) 10174 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10175 10176 /* 10177 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10178 * We have 8 bytes for the vendor name, and 16 bytes for the device 10179 * name and 4 bytes for the revision. 10180 */ 10181 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10182 "vendor", NULL)) == NULL) { 10183 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10184 } else { 10185 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10186 strncpy(inq_ptr->vendor, val, 10187 min(sizeof(inq_ptr->vendor), strlen(val))); 10188 } 10189 if (lun == NULL) { 10190 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10191 sizeof(inq_ptr->product)); 10192 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10193 NULL)) == NULL) { 10194 switch (lun->be_lun->lun_type) { 10195 case T_DIRECT: 10196 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10197 sizeof(inq_ptr->product)); 10198 break; 10199 case T_PROCESSOR: 10200 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10201 sizeof(inq_ptr->product)); 10202 break; 10203 case T_CDROM: 10204 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10205 sizeof(inq_ptr->product)); 10206 break; 10207 default: 10208 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10209 sizeof(inq_ptr->product)); 10210 break; 10211 } 10212 } else { 10213 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10214 strncpy(inq_ptr->product, val, 10215 min(sizeof(inq_ptr->product), strlen(val))); 10216 } 10217 10218 /* 10219 * XXX make this a macro somewhere so it automatically gets 10220 * incremented when we make changes. 10221 */ 10222 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10223 "revision", NULL)) == NULL) { 10224 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10225 } else { 10226 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10227 strncpy(inq_ptr->revision, val, 10228 min(sizeof(inq_ptr->revision), strlen(val))); 10229 } 10230 10231 /* 10232 * For parallel SCSI, we support double transition and single 10233 * transition clocking. We also support QAS (Quick Arbitration 10234 * and Selection) and Information Unit transfers on both the 10235 * control and array devices. 10236 */ 10237 if (port_type == CTL_PORT_SCSI) 10238 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10239 SID_SPI_IUS; 10240 10241 /* SAM-6 (no version claimed) */ 10242 scsi_ulto2b(0x00C0, inq_ptr->version1); 10243 /* SPC-5 (no version claimed) */ 10244 scsi_ulto2b(0x05C0, inq_ptr->version2); 10245 if (port_type == CTL_PORT_FC) { 10246 /* FCP-2 ANSI INCITS.350:2003 */ 10247 scsi_ulto2b(0x0917, inq_ptr->version3); 10248 } else if (port_type == CTL_PORT_SCSI) { 10249 /* SPI-4 ANSI INCITS.362:200x */ 10250 scsi_ulto2b(0x0B56, inq_ptr->version3); 10251 } else if (port_type == CTL_PORT_ISCSI) { 10252 /* iSCSI (no version claimed) */ 10253 scsi_ulto2b(0x0960, inq_ptr->version3); 10254 } else if (port_type == CTL_PORT_SAS) { 10255 /* SAS (no version claimed) */ 10256 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10257 } else if (port_type == CTL_PORT_UMASS) { 10258 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10259 scsi_ulto2b(0x1730, inq_ptr->version3); 10260 } 10261 10262 if (lun == NULL) { 10263 /* SBC-4 (no version claimed) */ 10264 scsi_ulto2b(0x0600, inq_ptr->version4); 10265 } else { 10266 switch (lun->be_lun->lun_type) { 10267 case T_DIRECT: 10268 /* SBC-4 (no version claimed) */ 10269 scsi_ulto2b(0x0600, inq_ptr->version4); 10270 break; 10271 case T_PROCESSOR: 10272 break; 10273 case T_CDROM: 10274 /* MMC-6 (no version claimed) */ 10275 scsi_ulto2b(0x04E0, inq_ptr->version4); 10276 break; 10277 default: 10278 break; 10279 } 10280 } 10281 10282 ctl_set_success(ctsio); 10283 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10284 ctsio->be_move_done = ctl_config_move_done; 10285 ctl_datamove((union ctl_io *)ctsio); 10286 return (CTL_RETVAL_COMPLETE); 10287 } 10288 10289 int 10290 ctl_inquiry(struct ctl_scsiio *ctsio) 10291 { 10292 struct scsi_inquiry *cdb; 10293 int retval; 10294 10295 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10296 10297 cdb = (struct scsi_inquiry *)ctsio->cdb; 10298 if (cdb->byte2 & SI_EVPD) 10299 retval = ctl_inquiry_evpd(ctsio); 10300 else if (cdb->page_code == 0) 10301 retval = ctl_inquiry_std(ctsio); 10302 else { 10303 ctl_set_invalid_field(ctsio, 10304 /*sks_valid*/ 1, 10305 /*command*/ 1, 10306 /*field*/ 2, 10307 /*bit_valid*/ 0, 10308 /*bit*/ 0); 10309 ctl_done((union ctl_io *)ctsio); 10310 return (CTL_RETVAL_COMPLETE); 10311 } 10312 10313 return (retval); 10314 } 10315 10316 int 10317 ctl_get_config(struct ctl_scsiio *ctsio) 10318 { 10319 struct ctl_lun *lun = CTL_LUN(ctsio); 10320 struct scsi_get_config_header *hdr; 10321 struct scsi_get_config_feature *feature; 10322 struct scsi_get_config *cdb; 10323 uint32_t alloc_len, data_len; 10324 int rt, starting; 10325 10326 cdb = (struct scsi_get_config *)ctsio->cdb; 10327 rt = (cdb->rt & SGC_RT_MASK); 10328 starting = scsi_2btoul(cdb->starting_feature); 10329 alloc_len = scsi_2btoul(cdb->length); 10330 10331 data_len = sizeof(struct scsi_get_config_header) + 10332 sizeof(struct scsi_get_config_feature) + 8 + 10333 sizeof(struct scsi_get_config_feature) + 8 + 10334 sizeof(struct scsi_get_config_feature) + 4 + 10335 sizeof(struct scsi_get_config_feature) + 4 + 10336 sizeof(struct scsi_get_config_feature) + 8 + 10337 sizeof(struct scsi_get_config_feature) + 10338 sizeof(struct scsi_get_config_feature) + 4 + 10339 sizeof(struct scsi_get_config_feature) + 4 + 10340 sizeof(struct scsi_get_config_feature) + 4 + 10341 sizeof(struct scsi_get_config_feature) + 4 + 10342 sizeof(struct scsi_get_config_feature) + 4 + 10343 sizeof(struct scsi_get_config_feature) + 4; 10344 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10345 ctsio->kern_sg_entries = 0; 10346 ctsio->kern_rel_offset = 0; 10347 10348 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10349 if (lun->flags & CTL_LUN_NO_MEDIA) 10350 scsi_ulto2b(0x0000, hdr->current_profile); 10351 else 10352 scsi_ulto2b(0x0010, hdr->current_profile); 10353 feature = (struct scsi_get_config_feature *)(hdr + 1); 10354 10355 if (starting > 0x003b) 10356 goto done; 10357 if (starting > 0x003a) 10358 goto f3b; 10359 if (starting > 0x002b) 10360 goto f3a; 10361 if (starting > 0x002a) 10362 goto f2b; 10363 if (starting > 0x001f) 10364 goto f2a; 10365 if (starting > 0x001e) 10366 goto f1f; 10367 if (starting > 0x001d) 10368 goto f1e; 10369 if (starting > 0x0010) 10370 goto f1d; 10371 if (starting > 0x0003) 10372 goto f10; 10373 if (starting > 0x0002) 10374 goto f3; 10375 if (starting > 0x0001) 10376 goto f2; 10377 if (starting > 0x0000) 10378 goto f1; 10379 10380 /* Profile List */ 10381 scsi_ulto2b(0x0000, feature->feature_code); 10382 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10383 feature->add_length = 8; 10384 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10385 feature->feature_data[2] = 0x00; 10386 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10387 feature->feature_data[6] = 0x01; 10388 feature = (struct scsi_get_config_feature *) 10389 &feature->feature_data[feature->add_length]; 10390 10391 f1: /* Core */ 10392 scsi_ulto2b(0x0001, feature->feature_code); 10393 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10394 feature->add_length = 8; 10395 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10396 feature->feature_data[4] = 0x03; 10397 feature = (struct scsi_get_config_feature *) 10398 &feature->feature_data[feature->add_length]; 10399 10400 f2: /* Morphing */ 10401 scsi_ulto2b(0x0002, feature->feature_code); 10402 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10403 feature->add_length = 4; 10404 feature->feature_data[0] = 0x02; 10405 feature = (struct scsi_get_config_feature *) 10406 &feature->feature_data[feature->add_length]; 10407 10408 f3: /* Removable Medium */ 10409 scsi_ulto2b(0x0003, feature->feature_code); 10410 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10411 feature->add_length = 4; 10412 feature->feature_data[0] = 0x39; 10413 feature = (struct scsi_get_config_feature *) 10414 &feature->feature_data[feature->add_length]; 10415 10416 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10417 goto done; 10418 10419 f10: /* Random Read */ 10420 scsi_ulto2b(0x0010, feature->feature_code); 10421 feature->flags = 0x00; 10422 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10423 feature->flags |= SGC_F_CURRENT; 10424 feature->add_length = 8; 10425 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10426 scsi_ulto2b(1, &feature->feature_data[4]); 10427 feature->feature_data[6] = 0x00; 10428 feature = (struct scsi_get_config_feature *) 10429 &feature->feature_data[feature->add_length]; 10430 10431 f1d: /* Multi-Read */ 10432 scsi_ulto2b(0x001D, feature->feature_code); 10433 feature->flags = 0x00; 10434 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10435 feature->flags |= SGC_F_CURRENT; 10436 feature->add_length = 0; 10437 feature = (struct scsi_get_config_feature *) 10438 &feature->feature_data[feature->add_length]; 10439 10440 f1e: /* CD Read */ 10441 scsi_ulto2b(0x001E, feature->feature_code); 10442 feature->flags = 0x00; 10443 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10444 feature->flags |= SGC_F_CURRENT; 10445 feature->add_length = 4; 10446 feature->feature_data[0] = 0x00; 10447 feature = (struct scsi_get_config_feature *) 10448 &feature->feature_data[feature->add_length]; 10449 10450 f1f: /* DVD Read */ 10451 scsi_ulto2b(0x001F, feature->feature_code); 10452 feature->flags = 0x08; 10453 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10454 feature->flags |= SGC_F_CURRENT; 10455 feature->add_length = 4; 10456 feature->feature_data[0] = 0x01; 10457 feature->feature_data[2] = 0x03; 10458 feature = (struct scsi_get_config_feature *) 10459 &feature->feature_data[feature->add_length]; 10460 10461 f2a: /* DVD+RW */ 10462 scsi_ulto2b(0x002A, feature->feature_code); 10463 feature->flags = 0x04; 10464 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10465 feature->flags |= SGC_F_CURRENT; 10466 feature->add_length = 4; 10467 feature->feature_data[0] = 0x00; 10468 feature->feature_data[1] = 0x00; 10469 feature = (struct scsi_get_config_feature *) 10470 &feature->feature_data[feature->add_length]; 10471 10472 f2b: /* DVD+R */ 10473 scsi_ulto2b(0x002B, feature->feature_code); 10474 feature->flags = 0x00; 10475 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10476 feature->flags |= SGC_F_CURRENT; 10477 feature->add_length = 4; 10478 feature->feature_data[0] = 0x00; 10479 feature = (struct scsi_get_config_feature *) 10480 &feature->feature_data[feature->add_length]; 10481 10482 f3a: /* DVD+RW Dual Layer */ 10483 scsi_ulto2b(0x003A, feature->feature_code); 10484 feature->flags = 0x00; 10485 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10486 feature->flags |= SGC_F_CURRENT; 10487 feature->add_length = 4; 10488 feature->feature_data[0] = 0x00; 10489 feature->feature_data[1] = 0x00; 10490 feature = (struct scsi_get_config_feature *) 10491 &feature->feature_data[feature->add_length]; 10492 10493 f3b: /* DVD+R Dual Layer */ 10494 scsi_ulto2b(0x003B, feature->feature_code); 10495 feature->flags = 0x00; 10496 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10497 feature->flags |= SGC_F_CURRENT; 10498 feature->add_length = 4; 10499 feature->feature_data[0] = 0x00; 10500 feature = (struct scsi_get_config_feature *) 10501 &feature->feature_data[feature->add_length]; 10502 10503 done: 10504 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10505 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10506 feature = (struct scsi_get_config_feature *)(hdr + 1); 10507 if (scsi_2btoul(feature->feature_code) == starting) 10508 feature = (struct scsi_get_config_feature *) 10509 &feature->feature_data[feature->add_length]; 10510 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10511 } 10512 scsi_ulto4b(data_len - 4, hdr->data_length); 10513 ctsio->kern_data_len = min(data_len, alloc_len); 10514 ctsio->kern_total_len = ctsio->kern_data_len; 10515 10516 ctl_set_success(ctsio); 10517 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10518 ctsio->be_move_done = ctl_config_move_done; 10519 ctl_datamove((union ctl_io *)ctsio); 10520 return (CTL_RETVAL_COMPLETE); 10521 } 10522 10523 int 10524 ctl_get_event_status(struct ctl_scsiio *ctsio) 10525 { 10526 struct scsi_get_event_status_header *hdr; 10527 struct scsi_get_event_status *cdb; 10528 uint32_t alloc_len, data_len; 10529 10530 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10531 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10532 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10533 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10534 ctl_done((union ctl_io *)ctsio); 10535 return (CTL_RETVAL_COMPLETE); 10536 } 10537 alloc_len = scsi_2btoul(cdb->length); 10538 10539 data_len = sizeof(struct scsi_get_event_status_header); 10540 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10541 ctsio->kern_sg_entries = 0; 10542 ctsio->kern_rel_offset = 0; 10543 ctsio->kern_data_len = min(data_len, alloc_len); 10544 ctsio->kern_total_len = ctsio->kern_data_len; 10545 10546 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10547 scsi_ulto2b(0, hdr->descr_length); 10548 hdr->nea_class = SGESN_NEA; 10549 hdr->supported_class = 0; 10550 10551 ctl_set_success(ctsio); 10552 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10553 ctsio->be_move_done = ctl_config_move_done; 10554 ctl_datamove((union ctl_io *)ctsio); 10555 return (CTL_RETVAL_COMPLETE); 10556 } 10557 10558 int 10559 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10560 { 10561 struct scsi_mechanism_status_header *hdr; 10562 struct scsi_mechanism_status *cdb; 10563 uint32_t alloc_len, data_len; 10564 10565 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10566 alloc_len = scsi_2btoul(cdb->length); 10567 10568 data_len = sizeof(struct scsi_mechanism_status_header); 10569 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10570 ctsio->kern_sg_entries = 0; 10571 ctsio->kern_rel_offset = 0; 10572 ctsio->kern_data_len = min(data_len, alloc_len); 10573 ctsio->kern_total_len = ctsio->kern_data_len; 10574 10575 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10576 hdr->state1 = 0x00; 10577 hdr->state2 = 0xe0; 10578 scsi_ulto3b(0, hdr->lba); 10579 hdr->slots_num = 0; 10580 scsi_ulto2b(0, hdr->slots_length); 10581 10582 ctl_set_success(ctsio); 10583 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10584 ctsio->be_move_done = ctl_config_move_done; 10585 ctl_datamove((union ctl_io *)ctsio); 10586 return (CTL_RETVAL_COMPLETE); 10587 } 10588 10589 static void 10590 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10591 { 10592 10593 lba += 150; 10594 buf[0] = 0; 10595 buf[1] = bin2bcd((lba / 75) / 60); 10596 buf[2] = bin2bcd((lba / 75) % 60); 10597 buf[3] = bin2bcd(lba % 75); 10598 } 10599 10600 int 10601 ctl_read_toc(struct ctl_scsiio *ctsio) 10602 { 10603 struct ctl_lun *lun = CTL_LUN(ctsio); 10604 struct scsi_read_toc_hdr *hdr; 10605 struct scsi_read_toc_type01_descr *descr; 10606 struct scsi_read_toc *cdb; 10607 uint32_t alloc_len, data_len; 10608 int format, msf; 10609 10610 cdb = (struct scsi_read_toc *)ctsio->cdb; 10611 msf = (cdb->byte2 & CD_MSF) != 0; 10612 format = cdb->format; 10613 alloc_len = scsi_2btoul(cdb->data_len); 10614 10615 data_len = sizeof(struct scsi_read_toc_hdr); 10616 if (format == 0) 10617 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10618 else 10619 data_len += sizeof(struct scsi_read_toc_type01_descr); 10620 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10621 ctsio->kern_sg_entries = 0; 10622 ctsio->kern_rel_offset = 0; 10623 ctsio->kern_data_len = min(data_len, alloc_len); 10624 ctsio->kern_total_len = ctsio->kern_data_len; 10625 10626 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10627 if (format == 0) { 10628 scsi_ulto2b(0x12, hdr->data_length); 10629 hdr->first = 1; 10630 hdr->last = 1; 10631 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10632 descr->addr_ctl = 0x14; 10633 descr->track_number = 1; 10634 if (msf) 10635 ctl_ultomsf(0, descr->track_start); 10636 else 10637 scsi_ulto4b(0, descr->track_start); 10638 descr++; 10639 descr->addr_ctl = 0x14; 10640 descr->track_number = 0xaa; 10641 if (msf) 10642 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10643 else 10644 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10645 } else { 10646 scsi_ulto2b(0x0a, hdr->data_length); 10647 hdr->first = 1; 10648 hdr->last = 1; 10649 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10650 descr->addr_ctl = 0x14; 10651 descr->track_number = 1; 10652 if (msf) 10653 ctl_ultomsf(0, descr->track_start); 10654 else 10655 scsi_ulto4b(0, descr->track_start); 10656 } 10657 10658 ctl_set_success(ctsio); 10659 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10660 ctsio->be_move_done = ctl_config_move_done; 10661 ctl_datamove((union ctl_io *)ctsio); 10662 return (CTL_RETVAL_COMPLETE); 10663 } 10664 10665 /* 10666 * For known CDB types, parse the LBA and length. 10667 */ 10668 static int 10669 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10670 { 10671 10672 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10673 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10674 10675 switch (io->scsiio.cdb[0]) { 10676 case COMPARE_AND_WRITE: { 10677 struct scsi_compare_and_write *cdb; 10678 10679 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10680 10681 *lba = scsi_8btou64(cdb->addr); 10682 *len = cdb->length; 10683 break; 10684 } 10685 case READ_6: 10686 case WRITE_6: { 10687 struct scsi_rw_6 *cdb; 10688 10689 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10690 10691 *lba = scsi_3btoul(cdb->addr); 10692 /* only 5 bits are valid in the most significant address byte */ 10693 *lba &= 0x1fffff; 10694 *len = cdb->length; 10695 break; 10696 } 10697 case READ_10: 10698 case WRITE_10: { 10699 struct scsi_rw_10 *cdb; 10700 10701 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10702 10703 *lba = scsi_4btoul(cdb->addr); 10704 *len = scsi_2btoul(cdb->length); 10705 break; 10706 } 10707 case WRITE_VERIFY_10: { 10708 struct scsi_write_verify_10 *cdb; 10709 10710 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10711 10712 *lba = scsi_4btoul(cdb->addr); 10713 *len = scsi_2btoul(cdb->length); 10714 break; 10715 } 10716 case READ_12: 10717 case WRITE_12: { 10718 struct scsi_rw_12 *cdb; 10719 10720 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10721 10722 *lba = scsi_4btoul(cdb->addr); 10723 *len = scsi_4btoul(cdb->length); 10724 break; 10725 } 10726 case WRITE_VERIFY_12: { 10727 struct scsi_write_verify_12 *cdb; 10728 10729 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10730 10731 *lba = scsi_4btoul(cdb->addr); 10732 *len = scsi_4btoul(cdb->length); 10733 break; 10734 } 10735 case READ_16: 10736 case WRITE_16: { 10737 struct scsi_rw_16 *cdb; 10738 10739 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10740 10741 *lba = scsi_8btou64(cdb->addr); 10742 *len = scsi_4btoul(cdb->length); 10743 break; 10744 } 10745 case WRITE_ATOMIC_16: { 10746 struct scsi_write_atomic_16 *cdb; 10747 10748 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10749 10750 *lba = scsi_8btou64(cdb->addr); 10751 *len = scsi_2btoul(cdb->length); 10752 break; 10753 } 10754 case WRITE_VERIFY_16: { 10755 struct scsi_write_verify_16 *cdb; 10756 10757 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10758 10759 *lba = scsi_8btou64(cdb->addr); 10760 *len = scsi_4btoul(cdb->length); 10761 break; 10762 } 10763 case WRITE_SAME_10: { 10764 struct scsi_write_same_10 *cdb; 10765 10766 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10767 10768 *lba = scsi_4btoul(cdb->addr); 10769 *len = scsi_2btoul(cdb->length); 10770 break; 10771 } 10772 case WRITE_SAME_16: { 10773 struct scsi_write_same_16 *cdb; 10774 10775 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10776 10777 *lba = scsi_8btou64(cdb->addr); 10778 *len = scsi_4btoul(cdb->length); 10779 break; 10780 } 10781 case VERIFY_10: { 10782 struct scsi_verify_10 *cdb; 10783 10784 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10785 10786 *lba = scsi_4btoul(cdb->addr); 10787 *len = scsi_2btoul(cdb->length); 10788 break; 10789 } 10790 case VERIFY_12: { 10791 struct scsi_verify_12 *cdb; 10792 10793 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10794 10795 *lba = scsi_4btoul(cdb->addr); 10796 *len = scsi_4btoul(cdb->length); 10797 break; 10798 } 10799 case VERIFY_16: { 10800 struct scsi_verify_16 *cdb; 10801 10802 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10803 10804 *lba = scsi_8btou64(cdb->addr); 10805 *len = scsi_4btoul(cdb->length); 10806 break; 10807 } 10808 case UNMAP: { 10809 *lba = 0; 10810 *len = UINT64_MAX; 10811 break; 10812 } 10813 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10814 struct scsi_get_lba_status *cdb; 10815 10816 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10817 *lba = scsi_8btou64(cdb->addr); 10818 *len = UINT32_MAX; 10819 break; 10820 } 10821 default: 10822 return (1); 10823 break; /* NOTREACHED */ 10824 } 10825 10826 return (0); 10827 } 10828 10829 static ctl_action 10830 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10831 bool seq) 10832 { 10833 uint64_t endlba1, endlba2; 10834 10835 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10836 endlba2 = lba2 + len2 - 1; 10837 10838 if ((endlba1 < lba2) || (endlba2 < lba1)) 10839 return (CTL_ACTION_PASS); 10840 else 10841 return (CTL_ACTION_BLOCK); 10842 } 10843 10844 static int 10845 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10846 { 10847 struct ctl_ptr_len_flags *ptrlen; 10848 struct scsi_unmap_desc *buf, *end, *range; 10849 uint64_t lba; 10850 uint32_t len; 10851 10852 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10853 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10854 10855 /* If not UNMAP -- go other way. */ 10856 if (io->scsiio.cdb[0] != UNMAP) 10857 return (CTL_ACTION_ERROR); 10858 10859 /* If UNMAP without data -- block and wait for data. */ 10860 ptrlen = (struct ctl_ptr_len_flags *) 10861 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10862 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10863 ptrlen->ptr == NULL) 10864 return (CTL_ACTION_BLOCK); 10865 10866 /* UNMAP with data -- check for collision. */ 10867 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10868 end = buf + ptrlen->len / sizeof(*buf); 10869 for (range = buf; range < end; range++) { 10870 lba = scsi_8btou64(range->lba); 10871 len = scsi_4btoul(range->length); 10872 if ((lba < lba2 + len2) && (lba + len > lba2)) 10873 return (CTL_ACTION_BLOCK); 10874 } 10875 return (CTL_ACTION_PASS); 10876 } 10877 10878 static ctl_action 10879 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10880 { 10881 uint64_t lba1, lba2; 10882 uint64_t len1, len2; 10883 int retval; 10884 10885 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10886 return (CTL_ACTION_ERROR); 10887 10888 retval = ctl_extent_check_unmap(io1, lba2, len2); 10889 if (retval != CTL_ACTION_ERROR) 10890 return (retval); 10891 10892 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10893 return (CTL_ACTION_ERROR); 10894 10895 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10896 seq = FALSE; 10897 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10898 } 10899 10900 static ctl_action 10901 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10902 { 10903 uint64_t lba1, lba2; 10904 uint64_t len1, len2; 10905 10906 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10907 return (CTL_ACTION_PASS); 10908 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10909 return (CTL_ACTION_ERROR); 10910 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10911 return (CTL_ACTION_ERROR); 10912 10913 if (lba1 + len1 == lba2) 10914 return (CTL_ACTION_BLOCK); 10915 return (CTL_ACTION_PASS); 10916 } 10917 10918 static ctl_action 10919 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10920 union ctl_io *ooa_io) 10921 { 10922 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10923 const ctl_serialize_action *serialize_row; 10924 10925 /* 10926 * Aborted commands are not going to be executed and may even 10927 * not report completion, so we don't care about their order. 10928 * Let them complete ASAP to clean the OOA queue. 10929 */ 10930 if (pending_io->io_hdr.flags & CTL_FLAG_ABORT) 10931 return (CTL_ACTION_SKIP); 10932 10933 /* 10934 * The initiator attempted multiple untagged commands at the same 10935 * time. Can't do that. 10936 */ 10937 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10938 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10939 && ((pending_io->io_hdr.nexus.targ_port == 10940 ooa_io->io_hdr.nexus.targ_port) 10941 && (pending_io->io_hdr.nexus.initid == 10942 ooa_io->io_hdr.nexus.initid)) 10943 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10944 CTL_FLAG_STATUS_SENT)) == 0)) 10945 return (CTL_ACTION_OVERLAP); 10946 10947 /* 10948 * The initiator attempted to send multiple tagged commands with 10949 * the same ID. (It's fine if different initiators have the same 10950 * tag ID.) 10951 * 10952 * Even if all of those conditions are true, we don't kill the I/O 10953 * if the command ahead of us has been aborted. We won't end up 10954 * sending it to the FETD, and it's perfectly legal to resend a 10955 * command with the same tag number as long as the previous 10956 * instance of this tag number has been aborted somehow. 10957 */ 10958 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10959 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10960 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10961 && ((pending_io->io_hdr.nexus.targ_port == 10962 ooa_io->io_hdr.nexus.targ_port) 10963 && (pending_io->io_hdr.nexus.initid == 10964 ooa_io->io_hdr.nexus.initid)) 10965 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10966 CTL_FLAG_STATUS_SENT)) == 0)) 10967 return (CTL_ACTION_OVERLAP_TAG); 10968 10969 /* 10970 * If we get a head of queue tag, SAM-3 says that we should 10971 * immediately execute it. 10972 * 10973 * What happens if this command would normally block for some other 10974 * reason? e.g. a request sense with a head of queue tag 10975 * immediately after a write. Normally that would block, but this 10976 * will result in its getting executed immediately... 10977 * 10978 * We currently return "pass" instead of "skip", so we'll end up 10979 * going through the rest of the queue to check for overlapped tags. 10980 * 10981 * XXX KDM check for other types of blockage first?? 10982 */ 10983 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10984 return (CTL_ACTION_PASS); 10985 10986 /* 10987 * Ordered tags have to block until all items ahead of them 10988 * have completed. If we get called with an ordered tag, we always 10989 * block, if something else is ahead of us in the queue. 10990 */ 10991 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10992 return (CTL_ACTION_BLOCK); 10993 10994 /* 10995 * Simple tags get blocked until all head of queue and ordered tags 10996 * ahead of them have completed. I'm lumping untagged commands in 10997 * with simple tags here. XXX KDM is that the right thing to do? 10998 */ 10999 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11000 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11001 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11002 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11003 return (CTL_ACTION_BLOCK); 11004 11005 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11006 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 11007 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 11008 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 11009 pending_io->scsiio.cdb[1], pending_io)); 11010 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11011 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 11012 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 11013 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 11014 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 11015 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 11016 ooa_io->scsiio.cdb[1], ooa_io)); 11017 11018 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11019 11020 switch (serialize_row[pending_entry->seridx]) { 11021 case CTL_SER_BLOCK: 11022 return (CTL_ACTION_BLOCK); 11023 case CTL_SER_EXTENT: 11024 return (ctl_extent_check(ooa_io, pending_io, 11025 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11026 case CTL_SER_EXTENTOPT: 11027 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11028 SCP_QUEUE_ALG_UNRESTRICTED) 11029 return (ctl_extent_check(ooa_io, pending_io, 11030 (lun->be_lun && 11031 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11032 return (CTL_ACTION_PASS); 11033 case CTL_SER_EXTENTSEQ: 11034 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11035 return (ctl_extent_check_seq(ooa_io, pending_io)); 11036 return (CTL_ACTION_PASS); 11037 case CTL_SER_PASS: 11038 return (CTL_ACTION_PASS); 11039 case CTL_SER_BLOCKOPT: 11040 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11041 SCP_QUEUE_ALG_UNRESTRICTED) 11042 return (CTL_ACTION_BLOCK); 11043 return (CTL_ACTION_PASS); 11044 case CTL_SER_SKIP: 11045 return (CTL_ACTION_SKIP); 11046 default: 11047 panic("%s: Invalid serialization value %d for %d => %d", 11048 __func__, serialize_row[pending_entry->seridx], 11049 pending_entry->seridx, ooa_entry->seridx); 11050 } 11051 11052 return (CTL_ACTION_ERROR); 11053 } 11054 11055 /* 11056 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11057 * Assumptions: 11058 * - pending_io is generally either incoming, or on the blocked queue 11059 * - starting I/O is the I/O we want to start the check with. 11060 */ 11061 static ctl_action 11062 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11063 union ctl_io **starting_io) 11064 { 11065 union ctl_io *ooa_io; 11066 ctl_action action; 11067 11068 mtx_assert(&lun->lun_lock, MA_OWNED); 11069 11070 /* 11071 * Run back along the OOA queue, starting with the current 11072 * blocked I/O and going through every I/O before it on the 11073 * queue. If starting_io is NULL, we'll just end up returning 11074 * CTL_ACTION_PASS. 11075 */ 11076 for (ooa_io = *starting_io; ooa_io != NULL; 11077 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) { 11078 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11079 if (action != CTL_ACTION_PASS) { 11080 *starting_io = ooa_io; 11081 return (action); 11082 } 11083 } 11084 11085 *starting_io = NULL; 11086 return (CTL_ACTION_PASS); 11087 } 11088 11089 /* 11090 * Try to unblock the specified I/O. 11091 * 11092 * skip parameter allows explicitly skip present blocker of the I/O, 11093 * starting from the previous one on OOA queue. It can be used when 11094 * we know for sure that the blocker I/O does no longer count. 11095 */ 11096 static void 11097 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11098 { 11099 struct ctl_softc *softc = lun->ctl_softc; 11100 union ctl_io *bio, *obio; 11101 const struct ctl_cmd_entry *entry; 11102 union ctl_ha_msg msg_info; 11103 ctl_action action; 11104 11105 mtx_assert(&lun->lun_lock, MA_OWNED); 11106 11107 if (io->io_hdr.blocker == NULL) 11108 return; 11109 11110 obio = bio = io->io_hdr.blocker; 11111 if (skip) 11112 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links); 11113 action = ctl_check_ooa(lun, io, &bio); 11114 if (action == CTL_ACTION_BLOCK) { 11115 /* Still blocked, but may be by different I/O now. */ 11116 if (bio != obio) { 11117 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, 11118 &io->io_hdr, blocked_links); 11119 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, 11120 &io->io_hdr, blocked_links); 11121 io->io_hdr.blocker = bio; 11122 } 11123 return; 11124 } 11125 11126 /* No longer blocked, one way or another. */ 11127 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links); 11128 io->io_hdr.blocker = NULL; 11129 11130 switch (action) { 11131 case CTL_ACTION_OVERLAP: 11132 ctl_set_overlapped_cmd(&io->scsiio); 11133 goto error; 11134 case CTL_ACTION_OVERLAP_TAG: 11135 ctl_set_overlapped_tag(&io->scsiio, 11136 io->scsiio.tag_num & 0xff); 11137 goto error; 11138 case CTL_ACTION_PASS: 11139 case CTL_ACTION_SKIP: 11140 11141 /* Serializing commands from the other SC retire there. */ 11142 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11143 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11144 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11145 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11146 msg_info.hdr.serializing_sc = io; 11147 msg_info.hdr.msg_type = CTL_MSG_R2R; 11148 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11149 sizeof(msg_info.hdr), M_NOWAIT); 11150 break; 11151 } 11152 11153 /* 11154 * Check this I/O for LUN state changes that may have happened 11155 * while this command was blocked. The LUN state may have been 11156 * changed by a command ahead of us in the queue. 11157 */ 11158 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11159 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 11160 ctl_done(io); 11161 break; 11162 } 11163 11164 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11165 ctl_enqueue_rtr(io); 11166 break; 11167 case CTL_ACTION_ERROR: 11168 default: 11169 ctl_set_internal_failure(&io->scsiio, 11170 /*sks_valid*/ 0, 11171 /*retry_count*/ 0); 11172 11173 error: 11174 /* Serializing commands from the other SC are done here. */ 11175 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11176 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11177 ctl_try_unblock_others(lun, io, TRUE); 11178 LIST_REMOVE(&io->io_hdr, ooa_links); 11179 11180 ctl_copy_sense_data_back(io, &msg_info); 11181 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11182 msg_info.hdr.serializing_sc = NULL; 11183 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 11184 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11185 sizeof(msg_info.scsi), M_WAITOK); 11186 ctl_free_io(io); 11187 break; 11188 } 11189 11190 ctl_done(io); 11191 break; 11192 } 11193 } 11194 11195 /* 11196 * Try to unblock I/Os blocked by the specified I/O. 11197 * 11198 * skip parameter allows explicitly skip the specified I/O as blocker, 11199 * starting from the previous one on the OOA queue. It can be used when 11200 * we know for sure that the specified I/O does no longer count (done). 11201 * It has to be still on OOA queue though so that we know where to start. 11202 */ 11203 static void 11204 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip) 11205 { 11206 union ctl_io *io, *next_io; 11207 11208 mtx_assert(&lun->lun_lock, MA_OWNED); 11209 11210 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue); 11211 io != NULL; io = next_io) { 11212 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links); 11213 11214 KASSERT(io->io_hdr.blocker != NULL, 11215 ("I/O %p on blocked list without blocker", io)); 11216 ctl_try_unblock_io(lun, io, skip); 11217 } 11218 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue), 11219 ("blocked_queue is not empty after skipping %p", bio)); 11220 } 11221 11222 /* 11223 * This routine (with one exception) checks LUN flags that can be set by 11224 * commands ahead of us in the OOA queue. These flags have to be checked 11225 * when a command initially comes in, and when we pull a command off the 11226 * blocked queue and are preparing to execute it. The reason we have to 11227 * check these flags for commands on the blocked queue is that the LUN 11228 * state may have been changed by a command ahead of us while we're on the 11229 * blocked queue. 11230 * 11231 * Ordering is somewhat important with these checks, so please pay 11232 * careful attention to the placement of any new checks. 11233 */ 11234 static int 11235 ctl_scsiio_lun_check(struct ctl_lun *lun, 11236 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11237 { 11238 struct ctl_softc *softc = lun->ctl_softc; 11239 int retval; 11240 uint32_t residx; 11241 11242 retval = 0; 11243 11244 mtx_assert(&lun->lun_lock, MA_OWNED); 11245 11246 /* 11247 * If this shelf is a secondary shelf controller, we may have to 11248 * reject some commands disallowed by HA mode and link state. 11249 */ 11250 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11251 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11252 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11253 ctl_set_lun_unavail(ctsio); 11254 retval = 1; 11255 goto bailout; 11256 } 11257 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11258 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11259 ctl_set_lun_transit(ctsio); 11260 retval = 1; 11261 goto bailout; 11262 } 11263 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11264 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11265 ctl_set_lun_standby(ctsio); 11266 retval = 1; 11267 goto bailout; 11268 } 11269 11270 /* The rest of checks are only done on executing side */ 11271 if (softc->ha_mode == CTL_HA_MODE_XFER) 11272 goto bailout; 11273 } 11274 11275 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11276 if (lun->be_lun && 11277 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11278 ctl_set_hw_write_protected(ctsio); 11279 retval = 1; 11280 goto bailout; 11281 } 11282 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11283 ctl_set_sense(ctsio, /*current_error*/ 1, 11284 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11285 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11286 retval = 1; 11287 goto bailout; 11288 } 11289 } 11290 11291 /* 11292 * Check for a reservation conflict. If this command isn't allowed 11293 * even on reserved LUNs, and if this initiator isn't the one who 11294 * reserved us, reject the command with a reservation conflict. 11295 */ 11296 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11297 if ((lun->flags & CTL_LUN_RESERVED) 11298 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11299 if (lun->res_idx != residx) { 11300 ctl_set_reservation_conflict(ctsio); 11301 retval = 1; 11302 goto bailout; 11303 } 11304 } 11305 11306 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11307 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11308 /* No reservation or command is allowed. */; 11309 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11310 (lun->pr_res_type == SPR_TYPE_WR_EX || 11311 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11312 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11313 /* The command is allowed for Write Exclusive resv. */; 11314 } else { 11315 /* 11316 * if we aren't registered or it's a res holder type 11317 * reservation and this isn't the res holder then set a 11318 * conflict. 11319 */ 11320 if (ctl_get_prkey(lun, residx) == 0 || 11321 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11322 ctl_set_reservation_conflict(ctsio); 11323 retval = 1; 11324 goto bailout; 11325 } 11326 } 11327 11328 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11329 if (lun->flags & CTL_LUN_EJECTED) 11330 ctl_set_lun_ejected(ctsio); 11331 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11332 if (lun->flags & CTL_LUN_REMOVABLE) 11333 ctl_set_lun_no_media(ctsio); 11334 else 11335 ctl_set_lun_int_reqd(ctsio); 11336 } else if (lun->flags & CTL_LUN_STOPPED) 11337 ctl_set_lun_stopped(ctsio); 11338 else 11339 goto bailout; 11340 retval = 1; 11341 goto bailout; 11342 } 11343 11344 bailout: 11345 return (retval); 11346 } 11347 11348 static void 11349 ctl_failover_io(union ctl_io *io, int have_lock) 11350 { 11351 ctl_set_busy(&io->scsiio); 11352 ctl_done(io); 11353 } 11354 11355 static void 11356 ctl_failover_lun(union ctl_io *rio) 11357 { 11358 struct ctl_softc *softc = CTL_SOFTC(rio); 11359 struct ctl_lun *lun; 11360 struct ctl_io_hdr *io, *next_io; 11361 uint32_t targ_lun; 11362 11363 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11364 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun)); 11365 11366 /* Find and lock the LUN. */ 11367 mtx_lock(&softc->ctl_lock); 11368 if (targ_lun > ctl_max_luns || 11369 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11370 mtx_unlock(&softc->ctl_lock); 11371 return; 11372 } 11373 mtx_lock(&lun->lun_lock); 11374 mtx_unlock(&softc->ctl_lock); 11375 if (lun->flags & CTL_LUN_DISABLED) { 11376 mtx_unlock(&lun->lun_lock); 11377 return; 11378 } 11379 11380 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11381 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11382 /* We are master */ 11383 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11384 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11385 io->flags |= CTL_FLAG_ABORT; 11386 io->flags |= CTL_FLAG_FAILOVER; 11387 ctl_try_unblock_io(lun, 11388 (union ctl_io *)io, FALSE); 11389 } else { /* This can be only due to DATAMOVE */ 11390 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11391 io->flags &= ~CTL_FLAG_DMA_INPROG; 11392 io->flags |= CTL_FLAG_IO_ACTIVE; 11393 io->port_status = 31340; 11394 ctl_enqueue_isc((union ctl_io *)io); 11395 } 11396 } else 11397 /* We are slave */ 11398 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11399 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11400 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11401 io->flags |= CTL_FLAG_FAILOVER; 11402 } else { 11403 ctl_set_busy(&((union ctl_io *)io)-> 11404 scsiio); 11405 ctl_done((union ctl_io *)io); 11406 } 11407 } 11408 } 11409 } else { /* SERIALIZE modes */ 11410 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11411 /* We are master */ 11412 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11413 if (io->blocker != NULL) { 11414 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue, 11415 io, blocked_links); 11416 io->blocker = NULL; 11417 } 11418 ctl_try_unblock_others(lun, (union ctl_io *)io, 11419 TRUE); 11420 LIST_REMOVE(io, ooa_links); 11421 ctl_free_io((union ctl_io *)io); 11422 } else 11423 /* We are slave */ 11424 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11425 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11426 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11427 ctl_set_busy(&((union ctl_io *)io)-> 11428 scsiio); 11429 ctl_done((union ctl_io *)io); 11430 } 11431 } 11432 } 11433 } 11434 mtx_unlock(&lun->lun_lock); 11435 } 11436 11437 static void 11438 ctl_scsiio_precheck(struct ctl_scsiio *ctsio) 11439 { 11440 struct ctl_softc *softc = CTL_SOFTC(ctsio); 11441 struct ctl_lun *lun; 11442 const struct ctl_cmd_entry *entry; 11443 union ctl_io *bio; 11444 uint32_t initidx, targ_lun; 11445 11446 lun = NULL; 11447 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11448 if (targ_lun < ctl_max_luns) 11449 lun = softc->ctl_luns[targ_lun]; 11450 if (lun) { 11451 /* 11452 * If the LUN is invalid, pretend that it doesn't exist. 11453 * It will go away as soon as all pending I/O has been 11454 * completed. 11455 */ 11456 mtx_lock(&lun->lun_lock); 11457 if (lun->flags & CTL_LUN_DISABLED) { 11458 mtx_unlock(&lun->lun_lock); 11459 lun = NULL; 11460 } 11461 } 11462 CTL_LUN(ctsio) = lun; 11463 if (lun) { 11464 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11465 11466 /* 11467 * Every I/O goes into the OOA queue for a particular LUN, 11468 * and stays there until completion. 11469 */ 11470 #ifdef CTL_TIME_IO 11471 if (LIST_EMPTY(&lun->ooa_queue)) 11472 lun->idle_time += getsbinuptime() - lun->last_busy; 11473 #endif 11474 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11475 } 11476 11477 /* Get command entry and return error if it is unsuppotyed. */ 11478 entry = ctl_validate_command(ctsio); 11479 if (entry == NULL) { 11480 if (lun) 11481 mtx_unlock(&lun->lun_lock); 11482 return; 11483 } 11484 11485 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11486 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11487 11488 /* 11489 * Check to see whether we can send this command to LUNs that don't 11490 * exist. This should pretty much only be the case for inquiry 11491 * and request sense. Further checks, below, really require having 11492 * a LUN, so we can't really check the command anymore. Just put 11493 * it on the rtr queue. 11494 */ 11495 if (lun == NULL) { 11496 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11497 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11498 ctl_enqueue_rtr((union ctl_io *)ctsio); 11499 return; 11500 } 11501 11502 ctl_set_unsupported_lun(ctsio); 11503 ctl_done((union ctl_io *)ctsio); 11504 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11505 return; 11506 } else { 11507 /* 11508 * Make sure we support this particular command on this LUN. 11509 * e.g., we don't support writes to the control LUN. 11510 */ 11511 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11512 mtx_unlock(&lun->lun_lock); 11513 ctl_set_invalid_opcode(ctsio); 11514 ctl_done((union ctl_io *)ctsio); 11515 return; 11516 } 11517 } 11518 11519 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11520 11521 /* 11522 * If we've got a request sense, it'll clear the contingent 11523 * allegiance condition. Otherwise, if we have a CA condition for 11524 * this initiator, clear it, because it sent down a command other 11525 * than request sense. 11526 */ 11527 if (ctsio->cdb[0] != REQUEST_SENSE) { 11528 struct scsi_sense_data *ps; 11529 11530 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11531 if (ps != NULL) 11532 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11533 } 11534 11535 /* 11536 * If the command has this flag set, it handles its own unit 11537 * attention reporting, we shouldn't do anything. Otherwise we 11538 * check for any pending unit attentions, and send them back to the 11539 * initiator. We only do this when a command initially comes in, 11540 * not when we pull it off the blocked queue. 11541 * 11542 * According to SAM-3, section 5.3.2, the order that things get 11543 * presented back to the host is basically unit attentions caused 11544 * by some sort of reset event, busy status, reservation conflicts 11545 * or task set full, and finally any other status. 11546 * 11547 * One issue here is that some of the unit attentions we report 11548 * don't fall into the "reset" category (e.g. "reported luns data 11549 * has changed"). So reporting it here, before the reservation 11550 * check, may be technically wrong. I guess the only thing to do 11551 * would be to check for and report the reset events here, and then 11552 * check for the other unit attention types after we check for a 11553 * reservation conflict. 11554 * 11555 * XXX KDM need to fix this 11556 */ 11557 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11558 ctl_ua_type ua_type; 11559 u_int sense_len = 0; 11560 11561 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11562 &sense_len, SSD_TYPE_NONE); 11563 if (ua_type != CTL_UA_NONE) { 11564 mtx_unlock(&lun->lun_lock); 11565 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11566 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11567 ctsio->sense_len = sense_len; 11568 ctl_done((union ctl_io *)ctsio); 11569 return; 11570 } 11571 } 11572 11573 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11574 mtx_unlock(&lun->lun_lock); 11575 ctl_done((union ctl_io *)ctsio); 11576 return; 11577 } 11578 11579 /* 11580 * XXX CHD this is where we want to send IO to other side if 11581 * this LUN is secondary on this SC. We will need to make a copy 11582 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11583 * the copy we send as FROM_OTHER. 11584 * We also need to stuff the address of the original IO so we can 11585 * find it easily. Something similar will need be done on the other 11586 * side so when we are done we can find the copy. 11587 */ 11588 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11589 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11590 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11591 union ctl_ha_msg msg_info; 11592 int isc_retval; 11593 11594 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11595 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11596 mtx_unlock(&lun->lun_lock); 11597 11598 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11599 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11600 msg_info.hdr.serializing_sc = NULL; 11601 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11602 msg_info.scsi.tag_num = ctsio->tag_num; 11603 msg_info.scsi.tag_type = ctsio->tag_type; 11604 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11605 msg_info.scsi.cdb_len = ctsio->cdb_len; 11606 msg_info.scsi.priority = ctsio->priority; 11607 11608 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11609 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11610 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11611 ctl_set_busy(ctsio); 11612 ctl_done((union ctl_io *)ctsio); 11613 return; 11614 } 11615 return; 11616 } 11617 11618 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 11619 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 11620 case CTL_ACTION_BLOCK: 11621 ctsio->io_hdr.blocker = bio; 11622 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 11623 blocked_links); 11624 mtx_unlock(&lun->lun_lock); 11625 break; 11626 case CTL_ACTION_PASS: 11627 case CTL_ACTION_SKIP: 11628 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11629 mtx_unlock(&lun->lun_lock); 11630 ctl_enqueue_rtr((union ctl_io *)ctsio); 11631 break; 11632 case CTL_ACTION_OVERLAP: 11633 mtx_unlock(&lun->lun_lock); 11634 ctl_set_overlapped_cmd(ctsio); 11635 ctl_done((union ctl_io *)ctsio); 11636 break; 11637 case CTL_ACTION_OVERLAP_TAG: 11638 mtx_unlock(&lun->lun_lock); 11639 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11640 ctl_done((union ctl_io *)ctsio); 11641 break; 11642 case CTL_ACTION_ERROR: 11643 default: 11644 mtx_unlock(&lun->lun_lock); 11645 ctl_set_internal_failure(ctsio, 11646 /*sks_valid*/ 0, 11647 /*retry_count*/ 0); 11648 ctl_done((union ctl_io *)ctsio); 11649 break; 11650 } 11651 } 11652 11653 const struct ctl_cmd_entry * 11654 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11655 { 11656 const struct ctl_cmd_entry *entry; 11657 int service_action; 11658 11659 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11660 if (sa) 11661 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11662 if (entry->flags & CTL_CMD_FLAG_SA5) { 11663 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11664 entry = &((const struct ctl_cmd_entry *) 11665 entry->execute)[service_action]; 11666 } 11667 return (entry); 11668 } 11669 11670 const struct ctl_cmd_entry * 11671 ctl_validate_command(struct ctl_scsiio *ctsio) 11672 { 11673 const struct ctl_cmd_entry *entry; 11674 int i, sa; 11675 uint8_t diff; 11676 11677 entry = ctl_get_cmd_entry(ctsio, &sa); 11678 if (entry->execute == NULL) { 11679 if (sa) 11680 ctl_set_invalid_field(ctsio, 11681 /*sks_valid*/ 1, 11682 /*command*/ 1, 11683 /*field*/ 1, 11684 /*bit_valid*/ 1, 11685 /*bit*/ 4); 11686 else 11687 ctl_set_invalid_opcode(ctsio); 11688 ctl_done((union ctl_io *)ctsio); 11689 return (NULL); 11690 } 11691 KASSERT(entry->length > 0, 11692 ("Not defined length for command 0x%02x/0x%02x", 11693 ctsio->cdb[0], ctsio->cdb[1])); 11694 for (i = 1; i < entry->length; i++) { 11695 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11696 if (diff == 0) 11697 continue; 11698 ctl_set_invalid_field(ctsio, 11699 /*sks_valid*/ 1, 11700 /*command*/ 1, 11701 /*field*/ i, 11702 /*bit_valid*/ 1, 11703 /*bit*/ fls(diff) - 1); 11704 ctl_done((union ctl_io *)ctsio); 11705 return (NULL); 11706 } 11707 return (entry); 11708 } 11709 11710 static int 11711 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11712 { 11713 11714 switch (lun_type) { 11715 case T_DIRECT: 11716 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11717 return (0); 11718 break; 11719 case T_PROCESSOR: 11720 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11721 return (0); 11722 break; 11723 case T_CDROM: 11724 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11725 return (0); 11726 break; 11727 default: 11728 return (0); 11729 } 11730 return (1); 11731 } 11732 11733 static int 11734 ctl_scsiio(struct ctl_scsiio *ctsio) 11735 { 11736 int retval; 11737 const struct ctl_cmd_entry *entry; 11738 11739 retval = CTL_RETVAL_COMPLETE; 11740 11741 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11742 11743 entry = ctl_get_cmd_entry(ctsio, NULL); 11744 11745 /* 11746 * If this I/O has been aborted, just send it straight to 11747 * ctl_done() without executing it. 11748 */ 11749 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11750 ctl_done((union ctl_io *)ctsio); 11751 goto bailout; 11752 } 11753 11754 /* 11755 * All the checks should have been handled by ctl_scsiio_precheck(). 11756 * We should be clear now to just execute the I/O. 11757 */ 11758 retval = entry->execute(ctsio); 11759 11760 bailout: 11761 return (retval); 11762 } 11763 11764 static int 11765 ctl_target_reset(union ctl_io *io) 11766 { 11767 struct ctl_softc *softc = CTL_SOFTC(io); 11768 struct ctl_port *port = CTL_PORT(io); 11769 struct ctl_lun *lun; 11770 uint32_t initidx; 11771 ctl_ua_type ua_type; 11772 11773 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11774 union ctl_ha_msg msg_info; 11775 11776 msg_info.hdr.nexus = io->io_hdr.nexus; 11777 msg_info.task.task_action = io->taskio.task_action; 11778 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11779 msg_info.hdr.original_sc = NULL; 11780 msg_info.hdr.serializing_sc = NULL; 11781 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11782 sizeof(msg_info.task), M_WAITOK); 11783 } 11784 11785 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11786 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11787 ua_type = CTL_UA_TARG_RESET; 11788 else 11789 ua_type = CTL_UA_BUS_RESET; 11790 mtx_lock(&softc->ctl_lock); 11791 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11792 if (port != NULL && 11793 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11794 continue; 11795 ctl_do_lun_reset(lun, initidx, ua_type); 11796 } 11797 mtx_unlock(&softc->ctl_lock); 11798 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11799 return (0); 11800 } 11801 11802 /* 11803 * The LUN should always be set. The I/O is optional, and is used to 11804 * distinguish between I/Os sent by this initiator, and by other 11805 * initiators. We set unit attention for initiators other than this one. 11806 * SAM-3 is vague on this point. It does say that a unit attention should 11807 * be established for other initiators when a LUN is reset (see section 11808 * 5.7.3), but it doesn't specifically say that the unit attention should 11809 * be established for this particular initiator when a LUN is reset. Here 11810 * is the relevant text, from SAM-3 rev 8: 11811 * 11812 * 5.7.2 When a SCSI initiator port aborts its own tasks 11813 * 11814 * When a SCSI initiator port causes its own task(s) to be aborted, no 11815 * notification that the task(s) have been aborted shall be returned to 11816 * the SCSI initiator port other than the completion response for the 11817 * command or task management function action that caused the task(s) to 11818 * be aborted and notification(s) associated with related effects of the 11819 * action (e.g., a reset unit attention condition). 11820 * 11821 * XXX KDM for now, we're setting unit attention for all initiators. 11822 */ 11823 static void 11824 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11825 { 11826 struct ctl_io_hdr *xioh; 11827 int i; 11828 11829 mtx_lock(&lun->lun_lock); 11830 /* Abort tasks. */ 11831 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11832 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11833 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE); 11834 } 11835 /* Clear CA. */ 11836 for (i = 0; i < ctl_max_ports; i++) { 11837 free(lun->pending_sense[i], M_CTL); 11838 lun->pending_sense[i] = NULL; 11839 } 11840 /* Clear reservation. */ 11841 lun->flags &= ~CTL_LUN_RESERVED; 11842 /* Clear prevent media removal. */ 11843 if (lun->prevent) { 11844 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11845 ctl_clear_mask(lun->prevent, i); 11846 lun->prevent_count = 0; 11847 } 11848 /* Clear TPC status */ 11849 ctl_tpc_lun_clear(lun, -1); 11850 /* Establish UA. */ 11851 #if 0 11852 ctl_est_ua_all(lun, initidx, ua_type); 11853 #else 11854 ctl_est_ua_all(lun, -1, ua_type); 11855 #endif 11856 mtx_unlock(&lun->lun_lock); 11857 } 11858 11859 static int 11860 ctl_lun_reset(union ctl_io *io) 11861 { 11862 struct ctl_softc *softc = CTL_SOFTC(io); 11863 struct ctl_lun *lun; 11864 uint32_t targ_lun, initidx; 11865 11866 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11867 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11868 mtx_lock(&softc->ctl_lock); 11869 if (targ_lun >= ctl_max_luns || 11870 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11871 mtx_unlock(&softc->ctl_lock); 11872 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11873 return (1); 11874 } 11875 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11876 mtx_unlock(&softc->ctl_lock); 11877 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11878 11879 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11880 union ctl_ha_msg msg_info; 11881 11882 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11883 msg_info.hdr.nexus = io->io_hdr.nexus; 11884 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11885 msg_info.hdr.original_sc = NULL; 11886 msg_info.hdr.serializing_sc = NULL; 11887 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11888 sizeof(msg_info.task), M_WAITOK); 11889 } 11890 return (0); 11891 } 11892 11893 static void 11894 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11895 int other_sc) 11896 { 11897 struct ctl_io_hdr *xioh; 11898 11899 mtx_assert(&lun->lun_lock, MA_OWNED); 11900 11901 /* 11902 * Run through the OOA queue and attempt to find the given I/O. 11903 * The target port, initiator ID, tag type and tag number have to 11904 * match the values that we got from the initiator. If we have an 11905 * untagged command to abort, simply abort the first untagged command 11906 * we come to. We only allow one untagged command at a time of course. 11907 */ 11908 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11909 union ctl_io *xio = (union ctl_io *)xioh; 11910 if ((targ_port == UINT32_MAX || 11911 targ_port == xioh->nexus.targ_port) && 11912 (init_id == UINT32_MAX || 11913 init_id == xioh->nexus.initid)) { 11914 if (targ_port != xioh->nexus.targ_port || 11915 init_id != xioh->nexus.initid) 11916 xioh->flags |= CTL_FLAG_ABORT_STATUS; 11917 xioh->flags |= CTL_FLAG_ABORT; 11918 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11919 union ctl_ha_msg msg_info; 11920 11921 msg_info.hdr.nexus = xioh->nexus; 11922 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11923 msg_info.task.tag_num = xio->scsiio.tag_num; 11924 msg_info.task.tag_type = xio->scsiio.tag_type; 11925 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11926 msg_info.hdr.original_sc = NULL; 11927 msg_info.hdr.serializing_sc = NULL; 11928 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11929 sizeof(msg_info.task), M_NOWAIT); 11930 } 11931 ctl_try_unblock_io(lun, xio, FALSE); 11932 } 11933 } 11934 } 11935 11936 static int 11937 ctl_abort_task_set(union ctl_io *io) 11938 { 11939 struct ctl_softc *softc = CTL_SOFTC(io); 11940 struct ctl_lun *lun; 11941 uint32_t targ_lun; 11942 11943 /* 11944 * Look up the LUN. 11945 */ 11946 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11947 mtx_lock(&softc->ctl_lock); 11948 if (targ_lun >= ctl_max_luns || 11949 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11950 mtx_unlock(&softc->ctl_lock); 11951 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11952 return (1); 11953 } 11954 11955 mtx_lock(&lun->lun_lock); 11956 mtx_unlock(&softc->ctl_lock); 11957 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11958 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11959 io->io_hdr.nexus.initid, 11960 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11961 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11962 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11963 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11964 } 11965 mtx_unlock(&lun->lun_lock); 11966 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11967 return (0); 11968 } 11969 11970 static void 11971 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 11972 ctl_ua_type ua_type) 11973 { 11974 struct ctl_lun *lun; 11975 struct scsi_sense_data *ps; 11976 uint32_t p, i; 11977 11978 p = initidx / CTL_MAX_INIT_PER_PORT; 11979 i = initidx % CTL_MAX_INIT_PER_PORT; 11980 mtx_lock(&softc->ctl_lock); 11981 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11982 mtx_lock(&lun->lun_lock); 11983 /* Abort tasks. */ 11984 ctl_abort_tasks_lun(lun, p, i, 1); 11985 /* Clear CA. */ 11986 ps = lun->pending_sense[p]; 11987 if (ps != NULL) 11988 ps[i].error_code = 0; 11989 /* Clear reservation. */ 11990 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11991 lun->flags &= ~CTL_LUN_RESERVED; 11992 /* Clear prevent media removal. */ 11993 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 11994 ctl_clear_mask(lun->prevent, initidx); 11995 lun->prevent_count--; 11996 } 11997 /* Clear TPC status */ 11998 ctl_tpc_lun_clear(lun, initidx); 11999 /* Establish UA. */ 12000 ctl_est_ua(lun, initidx, ua_type); 12001 mtx_unlock(&lun->lun_lock); 12002 } 12003 mtx_unlock(&softc->ctl_lock); 12004 } 12005 12006 static int 12007 ctl_i_t_nexus_reset(union ctl_io *io) 12008 { 12009 struct ctl_softc *softc = CTL_SOFTC(io); 12010 uint32_t initidx; 12011 12012 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12013 union ctl_ha_msg msg_info; 12014 12015 msg_info.hdr.nexus = io->io_hdr.nexus; 12016 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12017 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12018 msg_info.hdr.original_sc = NULL; 12019 msg_info.hdr.serializing_sc = NULL; 12020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12021 sizeof(msg_info.task), M_WAITOK); 12022 } 12023 12024 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12025 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 12026 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12027 return (0); 12028 } 12029 12030 static int 12031 ctl_abort_task(union ctl_io *io) 12032 { 12033 struct ctl_softc *softc = CTL_SOFTC(io); 12034 struct ctl_io_hdr *xioh; 12035 struct ctl_lun *lun; 12036 uint32_t targ_lun; 12037 12038 /* 12039 * Look up the LUN. 12040 */ 12041 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12042 mtx_lock(&softc->ctl_lock); 12043 if (targ_lun >= ctl_max_luns || 12044 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12045 mtx_unlock(&softc->ctl_lock); 12046 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12047 return (1); 12048 } 12049 12050 mtx_lock(&lun->lun_lock); 12051 mtx_unlock(&softc->ctl_lock); 12052 /* 12053 * Run through the OOA queue and attempt to find the given I/O. 12054 * The target port, initiator ID, tag type and tag number have to 12055 * match the values that we got from the initiator. If we have an 12056 * untagged command to abort, simply abort the first untagged command 12057 * we come to. We only allow one untagged command at a time of course. 12058 */ 12059 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12060 union ctl_io *xio = (union ctl_io *)xioh; 12061 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12062 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12063 || (xioh->flags & CTL_FLAG_ABORT)) 12064 continue; 12065 12066 /* 12067 * If the abort says that the task is untagged, the 12068 * task in the queue must be untagged. Otherwise, 12069 * we just check to see whether the tag numbers 12070 * match. This is because the QLogic firmware 12071 * doesn't pass back the tag type in an abort 12072 * request. 12073 */ 12074 #if 0 12075 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12076 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12077 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12078 #else 12079 /* 12080 * XXX KDM we've got problems with FC, because it 12081 * doesn't send down a tag type with aborts. So we 12082 * can only really go by the tag number... 12083 * This may cause problems with parallel SCSI. 12084 * Need to figure that out!! 12085 */ 12086 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12087 #endif 12088 xioh->flags |= CTL_FLAG_ABORT; 12089 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12090 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12091 union ctl_ha_msg msg_info; 12092 12093 msg_info.hdr.nexus = io->io_hdr.nexus; 12094 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12095 msg_info.task.tag_num = io->taskio.tag_num; 12096 msg_info.task.tag_type = io->taskio.tag_type; 12097 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12098 msg_info.hdr.original_sc = NULL; 12099 msg_info.hdr.serializing_sc = NULL; 12100 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12101 sizeof(msg_info.task), M_NOWAIT); 12102 } 12103 ctl_try_unblock_io(lun, xio, FALSE); 12104 } 12105 } 12106 mtx_unlock(&lun->lun_lock); 12107 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12108 return (0); 12109 } 12110 12111 static int 12112 ctl_query_task(union ctl_io *io, int task_set) 12113 { 12114 struct ctl_softc *softc = CTL_SOFTC(io); 12115 struct ctl_io_hdr *xioh; 12116 struct ctl_lun *lun; 12117 int found = 0; 12118 uint32_t targ_lun; 12119 12120 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12121 mtx_lock(&softc->ctl_lock); 12122 if (targ_lun >= ctl_max_luns || 12123 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12124 mtx_unlock(&softc->ctl_lock); 12125 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12126 return (1); 12127 } 12128 mtx_lock(&lun->lun_lock); 12129 mtx_unlock(&softc->ctl_lock); 12130 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12131 union ctl_io *xio = (union ctl_io *)xioh; 12132 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12133 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12134 || (xioh->flags & CTL_FLAG_ABORT)) 12135 continue; 12136 12137 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12138 found = 1; 12139 break; 12140 } 12141 } 12142 mtx_unlock(&lun->lun_lock); 12143 if (found) 12144 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12145 else 12146 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12147 return (0); 12148 } 12149 12150 static int 12151 ctl_query_async_event(union ctl_io *io) 12152 { 12153 struct ctl_softc *softc = CTL_SOFTC(io); 12154 struct ctl_lun *lun; 12155 ctl_ua_type ua; 12156 uint32_t targ_lun, initidx; 12157 12158 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12159 mtx_lock(&softc->ctl_lock); 12160 if (targ_lun >= ctl_max_luns || 12161 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12162 mtx_unlock(&softc->ctl_lock); 12163 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12164 return (1); 12165 } 12166 mtx_lock(&lun->lun_lock); 12167 mtx_unlock(&softc->ctl_lock); 12168 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12169 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12170 mtx_unlock(&lun->lun_lock); 12171 if (ua != CTL_UA_NONE) 12172 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12173 else 12174 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12175 return (0); 12176 } 12177 12178 static void 12179 ctl_run_task(union ctl_io *io) 12180 { 12181 int retval = 1; 12182 12183 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12184 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12185 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12186 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12187 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12188 switch (io->taskio.task_action) { 12189 case CTL_TASK_ABORT_TASK: 12190 retval = ctl_abort_task(io); 12191 break; 12192 case CTL_TASK_ABORT_TASK_SET: 12193 case CTL_TASK_CLEAR_TASK_SET: 12194 retval = ctl_abort_task_set(io); 12195 break; 12196 case CTL_TASK_CLEAR_ACA: 12197 break; 12198 case CTL_TASK_I_T_NEXUS_RESET: 12199 retval = ctl_i_t_nexus_reset(io); 12200 break; 12201 case CTL_TASK_LUN_RESET: 12202 retval = ctl_lun_reset(io); 12203 break; 12204 case CTL_TASK_TARGET_RESET: 12205 case CTL_TASK_BUS_RESET: 12206 retval = ctl_target_reset(io); 12207 break; 12208 case CTL_TASK_PORT_LOGIN: 12209 break; 12210 case CTL_TASK_PORT_LOGOUT: 12211 break; 12212 case CTL_TASK_QUERY_TASK: 12213 retval = ctl_query_task(io, 0); 12214 break; 12215 case CTL_TASK_QUERY_TASK_SET: 12216 retval = ctl_query_task(io, 1); 12217 break; 12218 case CTL_TASK_QUERY_ASYNC_EVENT: 12219 retval = ctl_query_async_event(io); 12220 break; 12221 default: 12222 printf("%s: got unknown task management event %d\n", 12223 __func__, io->taskio.task_action); 12224 break; 12225 } 12226 if (retval == 0) 12227 io->io_hdr.status = CTL_SUCCESS; 12228 else 12229 io->io_hdr.status = CTL_ERROR; 12230 ctl_done(io); 12231 } 12232 12233 /* 12234 * For HA operation. Handle commands that come in from the other 12235 * controller. 12236 */ 12237 static void 12238 ctl_handle_isc(union ctl_io *io) 12239 { 12240 struct ctl_softc *softc = CTL_SOFTC(io); 12241 struct ctl_lun *lun; 12242 const struct ctl_cmd_entry *entry; 12243 uint32_t targ_lun; 12244 12245 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12246 switch (io->io_hdr.msg_type) { 12247 case CTL_MSG_SERIALIZE: 12248 ctl_serialize_other_sc_cmd(&io->scsiio); 12249 break; 12250 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12251 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12252 if (targ_lun >= ctl_max_luns || 12253 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12254 ctl_done(io); 12255 break; 12256 } 12257 mtx_lock(&lun->lun_lock); 12258 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12259 mtx_unlock(&lun->lun_lock); 12260 ctl_done(io); 12261 break; 12262 } 12263 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12264 mtx_unlock(&lun->lun_lock); 12265 ctl_enqueue_rtr(io); 12266 break; 12267 case CTL_MSG_FINISH_IO: 12268 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12269 ctl_done(io); 12270 break; 12271 } 12272 if (targ_lun >= ctl_max_luns || 12273 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12274 ctl_free_io(io); 12275 break; 12276 } 12277 mtx_lock(&lun->lun_lock); 12278 ctl_try_unblock_others(lun, io, TRUE); 12279 LIST_REMOVE(&io->io_hdr, ooa_links); 12280 mtx_unlock(&lun->lun_lock); 12281 ctl_free_io(io); 12282 break; 12283 case CTL_MSG_PERS_ACTION: 12284 ctl_hndl_per_res_out_on_other_sc(io); 12285 ctl_free_io(io); 12286 break; 12287 case CTL_MSG_BAD_JUJU: 12288 ctl_done(io); 12289 break; 12290 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12291 ctl_datamove_remote(io); 12292 break; 12293 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12294 ctl_datamove_done(io, false); 12295 break; 12296 case CTL_MSG_FAILOVER: 12297 ctl_failover_lun(io); 12298 ctl_free_io(io); 12299 break; 12300 default: 12301 printf("%s: Invalid message type %d\n", 12302 __func__, io->io_hdr.msg_type); 12303 ctl_free_io(io); 12304 break; 12305 } 12306 12307 } 12308 12309 /* 12310 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12311 * there is no match. 12312 */ 12313 static ctl_lun_error_pattern 12314 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12315 { 12316 const struct ctl_cmd_entry *entry; 12317 ctl_lun_error_pattern filtered_pattern, pattern; 12318 12319 pattern = desc->error_pattern; 12320 12321 /* 12322 * XXX KDM we need more data passed into this function to match a 12323 * custom pattern, and we actually need to implement custom pattern 12324 * matching. 12325 */ 12326 if (pattern & CTL_LUN_PAT_CMD) 12327 return (CTL_LUN_PAT_CMD); 12328 12329 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12330 return (CTL_LUN_PAT_ANY); 12331 12332 entry = ctl_get_cmd_entry(ctsio, NULL); 12333 12334 filtered_pattern = entry->pattern & pattern; 12335 12336 /* 12337 * If the user requested specific flags in the pattern (e.g. 12338 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12339 * flags. 12340 * 12341 * If the user did not specify any flags, it doesn't matter whether 12342 * or not the command supports the flags. 12343 */ 12344 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12345 (pattern & ~CTL_LUN_PAT_MASK)) 12346 return (CTL_LUN_PAT_NONE); 12347 12348 /* 12349 * If the user asked for a range check, see if the requested LBA 12350 * range overlaps with this command's LBA range. 12351 */ 12352 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12353 uint64_t lba1; 12354 uint64_t len1; 12355 ctl_action action; 12356 int retval; 12357 12358 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12359 if (retval != 0) 12360 return (CTL_LUN_PAT_NONE); 12361 12362 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12363 desc->lba_range.len, FALSE); 12364 /* 12365 * A "pass" means that the LBA ranges don't overlap, so 12366 * this doesn't match the user's range criteria. 12367 */ 12368 if (action == CTL_ACTION_PASS) 12369 return (CTL_LUN_PAT_NONE); 12370 } 12371 12372 return (filtered_pattern); 12373 } 12374 12375 static void 12376 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12377 { 12378 struct ctl_error_desc *desc, *desc2; 12379 12380 mtx_assert(&lun->lun_lock, MA_OWNED); 12381 12382 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12383 ctl_lun_error_pattern pattern; 12384 /* 12385 * Check to see whether this particular command matches 12386 * the pattern in the descriptor. 12387 */ 12388 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12389 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12390 continue; 12391 12392 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12393 case CTL_LUN_INJ_ABORTED: 12394 ctl_set_aborted(&io->scsiio); 12395 break; 12396 case CTL_LUN_INJ_MEDIUM_ERR: 12397 ctl_set_medium_error(&io->scsiio, 12398 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12399 CTL_FLAG_DATA_OUT); 12400 break; 12401 case CTL_LUN_INJ_UA: 12402 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12403 * OCCURRED */ 12404 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12405 break; 12406 case CTL_LUN_INJ_CUSTOM: 12407 /* 12408 * We're assuming the user knows what he is doing. 12409 * Just copy the sense information without doing 12410 * checks. 12411 */ 12412 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12413 MIN(sizeof(desc->custom_sense), 12414 sizeof(io->scsiio.sense_data))); 12415 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12416 io->scsiio.sense_len = SSD_FULL_SIZE; 12417 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12418 break; 12419 case CTL_LUN_INJ_NONE: 12420 default: 12421 /* 12422 * If this is an error injection type we don't know 12423 * about, clear the continuous flag (if it is set) 12424 * so it will get deleted below. 12425 */ 12426 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12427 break; 12428 } 12429 /* 12430 * By default, each error injection action is a one-shot 12431 */ 12432 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12433 continue; 12434 12435 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12436 12437 free(desc, M_CTL); 12438 } 12439 } 12440 12441 #ifdef CTL_IO_DELAY 12442 static void 12443 ctl_datamove_timer_wakeup(void *arg) 12444 { 12445 union ctl_io *io; 12446 12447 io = (union ctl_io *)arg; 12448 12449 ctl_datamove(io); 12450 } 12451 #endif /* CTL_IO_DELAY */ 12452 12453 static void 12454 ctl_datamove_done_process(union ctl_io *io) 12455 { 12456 #ifdef CTL_TIME_IO 12457 struct bintime cur_bt; 12458 #endif 12459 12460 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 12461 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 12462 12463 #ifdef CTL_TIME_IO 12464 getbinuptime(&cur_bt); 12465 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12466 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12467 #endif 12468 io->io_hdr.num_dmas++; 12469 12470 if ((io->io_hdr.port_status != 0) && 12471 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12472 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12473 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 12474 /*retry_count*/ io->io_hdr.port_status); 12475 } else if (io->scsiio.kern_data_resid != 0 && 12476 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 12477 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12478 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12479 ctl_set_invalid_field_ciu(&io->scsiio); 12480 } else if (ctl_debug & CTL_DEBUG_CDB_DATA) 12481 ctl_data_print(io); 12482 } 12483 12484 void 12485 ctl_datamove_done(union ctl_io *io, bool samethr) 12486 { 12487 12488 ctl_datamove_done_process(io); 12489 io->scsiio.be_move_done(io, samethr); 12490 } 12491 12492 void 12493 ctl_datamove(union ctl_io *io) 12494 { 12495 void (*fe_datamove)(union ctl_io *io); 12496 12497 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12498 12499 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12500 12501 /* No data transferred yet. Frontend must update this when done. */ 12502 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12503 12504 #ifdef CTL_TIME_IO 12505 getbinuptime(&io->io_hdr.dma_start_bt); 12506 #endif /* CTL_TIME_IO */ 12507 12508 #ifdef CTL_IO_DELAY 12509 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12510 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12511 } else { 12512 struct ctl_lun *lun; 12513 12514 lun = CTL_LUN(io); 12515 if ((lun != NULL) 12516 && (lun->delay_info.datamove_delay > 0)) { 12517 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12518 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12519 callout_reset(&io->io_hdr.delay_callout, 12520 lun->delay_info.datamove_delay * hz, 12521 ctl_datamove_timer_wakeup, io); 12522 if (lun->delay_info.datamove_type == 12523 CTL_DELAY_TYPE_ONESHOT) 12524 lun->delay_info.datamove_delay = 0; 12525 return; 12526 } 12527 } 12528 #endif 12529 12530 /* 12531 * This command has been aborted. Set the port status, so we fail 12532 * the data move. 12533 */ 12534 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12535 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12536 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12537 io->io_hdr.nexus.targ_port, 12538 io->io_hdr.nexus.targ_lun); 12539 io->io_hdr.port_status = 31337; 12540 ctl_datamove_done_process(io); 12541 io->scsiio.be_move_done(io, true); 12542 return; 12543 } 12544 12545 /* Don't confuse frontend with zero length data move. */ 12546 if (io->scsiio.kern_data_len == 0) { 12547 ctl_datamove_done_process(io); 12548 io->scsiio.be_move_done(io, true); 12549 return; 12550 } 12551 12552 fe_datamove = CTL_PORT(io)->fe_datamove; 12553 fe_datamove(io); 12554 } 12555 12556 static void 12557 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12558 { 12559 union ctl_ha_msg msg; 12560 #ifdef CTL_TIME_IO 12561 struct bintime cur_bt; 12562 #endif 12563 12564 memset(&msg, 0, sizeof(msg)); 12565 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12566 msg.hdr.original_sc = io; 12567 msg.hdr.serializing_sc = io->io_hdr.remote_io; 12568 msg.hdr.nexus = io->io_hdr.nexus; 12569 msg.hdr.status = io->io_hdr.status; 12570 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12571 msg.scsi.tag_num = io->scsiio.tag_num; 12572 msg.scsi.tag_type = io->scsiio.tag_type; 12573 msg.scsi.scsi_status = io->scsiio.scsi_status; 12574 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12575 io->scsiio.sense_len); 12576 msg.scsi.sense_len = io->scsiio.sense_len; 12577 msg.scsi.port_status = io->io_hdr.port_status; 12578 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12579 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12580 ctl_failover_io(io, /*have_lock*/ have_lock); 12581 return; 12582 } 12583 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12584 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12585 msg.scsi.sense_len, M_WAITOK); 12586 12587 #ifdef CTL_TIME_IO 12588 getbinuptime(&cur_bt); 12589 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12590 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12591 #endif 12592 io->io_hdr.num_dmas++; 12593 } 12594 12595 /* 12596 * The DMA to the remote side is done, now we need to tell the other side 12597 * we're done so it can continue with its data movement. 12598 */ 12599 static void 12600 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12601 { 12602 union ctl_io *io; 12603 uint32_t i; 12604 12605 io = rq->context; 12606 12607 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12608 printf("%s: ISC DMA write failed with error %d", __func__, 12609 rq->ret); 12610 ctl_set_internal_failure(&io->scsiio, 12611 /*sks_valid*/ 1, 12612 /*retry_count*/ rq->ret); 12613 } 12614 12615 ctl_dt_req_free(rq); 12616 12617 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12618 free(CTL_LSGLT(io)[i].addr, M_CTL); 12619 free(CTL_RSGL(io), M_CTL); 12620 CTL_RSGL(io) = NULL; 12621 CTL_LSGL(io) = NULL; 12622 12623 /* 12624 * The data is in local and remote memory, so now we need to send 12625 * status (good or back) back to the other side. 12626 */ 12627 ctl_send_datamove_done(io, /*have_lock*/ 0); 12628 } 12629 12630 /* 12631 * We've moved the data from the host/controller into local memory. Now we 12632 * need to push it over to the remote controller's memory. 12633 */ 12634 static int 12635 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr) 12636 { 12637 int retval; 12638 12639 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12640 ctl_datamove_remote_write_cb); 12641 return (retval); 12642 } 12643 12644 static void 12645 ctl_datamove_remote_write(union ctl_io *io) 12646 { 12647 int retval; 12648 void (*fe_datamove)(union ctl_io *io); 12649 12650 /* 12651 * - Get the data from the host/HBA into local memory. 12652 * - DMA memory from the local controller to the remote controller. 12653 * - Send status back to the remote controller. 12654 */ 12655 12656 retval = ctl_datamove_remote_sgl_setup(io); 12657 if (retval != 0) 12658 return; 12659 12660 /* Switch the pointer over so the FETD knows what to do */ 12661 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12662 12663 /* 12664 * Use a custom move done callback, since we need to send completion 12665 * back to the other controller, not to the backend on this side. 12666 */ 12667 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12668 12669 fe_datamove = CTL_PORT(io)->fe_datamove; 12670 fe_datamove(io); 12671 } 12672 12673 static int 12674 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr) 12675 { 12676 uint32_t i; 12677 12678 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12679 free(CTL_LSGLT(io)[i].addr, M_CTL); 12680 free(CTL_RSGL(io), M_CTL); 12681 CTL_RSGL(io) = NULL; 12682 CTL_LSGL(io) = NULL; 12683 12684 /* 12685 * The read is done, now we need to send status (good or bad) back 12686 * to the other side. 12687 */ 12688 ctl_send_datamove_done(io, /*have_lock*/ 0); 12689 12690 return (0); 12691 } 12692 12693 static void 12694 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12695 { 12696 union ctl_io *io; 12697 void (*fe_datamove)(union ctl_io *io); 12698 12699 io = rq->context; 12700 12701 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12702 printf("%s: ISC DMA read failed with error %d\n", __func__, 12703 rq->ret); 12704 ctl_set_internal_failure(&io->scsiio, 12705 /*sks_valid*/ 1, 12706 /*retry_count*/ rq->ret); 12707 } 12708 12709 ctl_dt_req_free(rq); 12710 12711 /* Switch the pointer over so the FETD knows what to do */ 12712 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12713 12714 /* 12715 * Use a custom move done callback, since we need to send completion 12716 * back to the other controller, not to the backend on this side. 12717 */ 12718 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12719 12720 /* XXX KDM add checks like the ones in ctl_datamove? */ 12721 12722 fe_datamove = CTL_PORT(io)->fe_datamove; 12723 fe_datamove(io); 12724 } 12725 12726 static int 12727 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12728 { 12729 struct ctl_sg_entry *local_sglist; 12730 uint32_t len_to_go; 12731 int retval; 12732 int i; 12733 12734 retval = 0; 12735 local_sglist = CTL_LSGL(io); 12736 len_to_go = io->scsiio.kern_data_len; 12737 12738 /* 12739 * The difficult thing here is that the size of the various 12740 * S/G segments may be different than the size from the 12741 * remote controller. That'll make it harder when DMAing 12742 * the data back to the other side. 12743 */ 12744 for (i = 0; len_to_go > 0; i++) { 12745 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12746 local_sglist[i].addr = 12747 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12748 12749 len_to_go -= local_sglist[i].len; 12750 } 12751 /* 12752 * Reset the number of S/G entries accordingly. The original 12753 * number of S/G entries is available in rem_sg_entries. 12754 */ 12755 io->scsiio.kern_sg_entries = i; 12756 12757 return (retval); 12758 } 12759 12760 static int 12761 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12762 ctl_ha_dt_cb callback) 12763 { 12764 struct ctl_ha_dt_req *rq; 12765 struct ctl_sg_entry *remote_sglist, *local_sglist; 12766 uint32_t local_used, remote_used, total_used; 12767 int i, j, isc_ret; 12768 12769 rq = ctl_dt_req_alloc(); 12770 12771 /* 12772 * If we failed to allocate the request, and if the DMA didn't fail 12773 * anyway, set busy status. This is just a resource allocation 12774 * failure. 12775 */ 12776 if ((rq == NULL) 12777 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12778 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12779 ctl_set_busy(&io->scsiio); 12780 12781 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12782 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12783 if (rq != NULL) 12784 ctl_dt_req_free(rq); 12785 12786 /* 12787 * The data move failed. We need to return status back 12788 * to the other controller. No point in trying to DMA 12789 * data to the remote controller. 12790 */ 12791 12792 ctl_send_datamove_done(io, /*have_lock*/ 0); 12793 12794 return (1); 12795 } 12796 12797 local_sglist = CTL_LSGL(io); 12798 remote_sglist = CTL_RSGL(io); 12799 local_used = 0; 12800 remote_used = 0; 12801 total_used = 0; 12802 12803 /* 12804 * Pull/push the data over the wire from/to the other controller. 12805 * This takes into account the possibility that the local and 12806 * remote sglists may not be identical in terms of the size of 12807 * the elements and the number of elements. 12808 * 12809 * One fundamental assumption here is that the length allocated for 12810 * both the local and remote sglists is identical. Otherwise, we've 12811 * essentially got a coding error of some sort. 12812 */ 12813 isc_ret = CTL_HA_STATUS_SUCCESS; 12814 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12815 uint32_t cur_len; 12816 uint8_t *tmp_ptr; 12817 12818 rq->command = command; 12819 rq->context = io; 12820 12821 /* 12822 * Both pointers should be aligned. But it is possible 12823 * that the allocation length is not. They should both 12824 * also have enough slack left over at the end, though, 12825 * to round up to the next 8 byte boundary. 12826 */ 12827 cur_len = MIN(local_sglist[i].len - local_used, 12828 remote_sglist[j].len - remote_used); 12829 rq->size = cur_len; 12830 12831 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12832 tmp_ptr += local_used; 12833 12834 #if 0 12835 /* Use physical addresses when talking to ISC hardware */ 12836 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12837 /* XXX KDM use busdma */ 12838 rq->local = vtophys(tmp_ptr); 12839 } else 12840 rq->local = tmp_ptr; 12841 #else 12842 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12843 ("HA does not support BUS_ADDR")); 12844 rq->local = tmp_ptr; 12845 #endif 12846 12847 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12848 tmp_ptr += remote_used; 12849 rq->remote = tmp_ptr; 12850 12851 rq->callback = NULL; 12852 12853 local_used += cur_len; 12854 if (local_used >= local_sglist[i].len) { 12855 i++; 12856 local_used = 0; 12857 } 12858 12859 remote_used += cur_len; 12860 if (remote_used >= remote_sglist[j].len) { 12861 j++; 12862 remote_used = 0; 12863 } 12864 total_used += cur_len; 12865 12866 if (total_used >= io->scsiio.kern_data_len) 12867 rq->callback = callback; 12868 12869 isc_ret = ctl_dt_single(rq); 12870 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12871 break; 12872 } 12873 if (isc_ret != CTL_HA_STATUS_WAIT) { 12874 rq->ret = isc_ret; 12875 callback(rq); 12876 } 12877 12878 return (0); 12879 } 12880 12881 static void 12882 ctl_datamove_remote_read(union ctl_io *io) 12883 { 12884 int retval; 12885 uint32_t i; 12886 12887 /* 12888 * This will send an error to the other controller in the case of a 12889 * failure. 12890 */ 12891 retval = ctl_datamove_remote_sgl_setup(io); 12892 if (retval != 0) 12893 return; 12894 12895 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12896 ctl_datamove_remote_read_cb); 12897 if (retval != 0) { 12898 /* 12899 * Make sure we free memory if there was an error.. The 12900 * ctl_datamove_remote_xfer() function will send the 12901 * datamove done message, or call the callback with an 12902 * error if there is a problem. 12903 */ 12904 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12905 free(CTL_LSGLT(io)[i].addr, M_CTL); 12906 free(CTL_RSGL(io), M_CTL); 12907 CTL_RSGL(io) = NULL; 12908 CTL_LSGL(io) = NULL; 12909 } 12910 } 12911 12912 /* 12913 * Process a datamove request from the other controller. This is used for 12914 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12915 * first. Once that is complete, the data gets DMAed into the remote 12916 * controller's memory. For reads, we DMA from the remote controller's 12917 * memory into our memory first, and then move it out to the FETD. 12918 */ 12919 static void 12920 ctl_datamove_remote(union ctl_io *io) 12921 { 12922 12923 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12924 12925 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12926 ctl_failover_io(io, /*have_lock*/ 0); 12927 return; 12928 } 12929 12930 /* 12931 * Note that we look for an aborted I/O here, but don't do some of 12932 * the other checks that ctl_datamove() normally does. 12933 * We don't need to run the datamove delay code, since that should 12934 * have been done if need be on the other controller. 12935 */ 12936 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12937 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12938 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12939 io->io_hdr.nexus.targ_port, 12940 io->io_hdr.nexus.targ_lun); 12941 io->io_hdr.port_status = 31338; 12942 ctl_send_datamove_done(io, /*have_lock*/ 0); 12943 return; 12944 } 12945 12946 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12947 ctl_datamove_remote_write(io); 12948 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12949 ctl_datamove_remote_read(io); 12950 else { 12951 io->io_hdr.port_status = 31339; 12952 ctl_send_datamove_done(io, /*have_lock*/ 0); 12953 } 12954 } 12955 12956 static void 12957 ctl_process_done(union ctl_io *io) 12958 { 12959 struct ctl_softc *softc = CTL_SOFTC(io); 12960 struct ctl_port *port = CTL_PORT(io); 12961 struct ctl_lun *lun = CTL_LUN(io); 12962 void (*fe_done)(union ctl_io *io); 12963 union ctl_ha_msg msg; 12964 12965 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12966 fe_done = port->fe_done; 12967 12968 #ifdef CTL_TIME_IO 12969 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12970 char str[256]; 12971 char path_str[64]; 12972 struct sbuf sb; 12973 12974 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12975 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12976 12977 sbuf_cat(&sb, path_str); 12978 switch (io->io_hdr.io_type) { 12979 case CTL_IO_SCSI: 12980 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12981 sbuf_printf(&sb, "\n"); 12982 sbuf_cat(&sb, path_str); 12983 sbuf_printf(&sb, "Tag: 0x%04x/%d, Prio: %d\n", 12984 io->scsiio.tag_num, io->scsiio.tag_type, 12985 io->scsiio.priority); 12986 break; 12987 case CTL_IO_TASK: 12988 sbuf_printf(&sb, "Task Action: %d Tag: 0x%04x/%d\n", 12989 io->taskio.task_action, 12990 io->taskio.tag_num, io->taskio.tag_type); 12991 break; 12992 default: 12993 panic("%s: Invalid CTL I/O type %d\n", 12994 __func__, io->io_hdr.io_type); 12995 } 12996 sbuf_cat(&sb, path_str); 12997 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12998 (intmax_t)time_uptime - io->io_hdr.start_time); 12999 sbuf_finish(&sb); 13000 printf("%s", sbuf_data(&sb)); 13001 } 13002 #endif /* CTL_TIME_IO */ 13003 13004 switch (io->io_hdr.io_type) { 13005 case CTL_IO_SCSI: 13006 break; 13007 case CTL_IO_TASK: 13008 if (ctl_debug & CTL_DEBUG_INFO) 13009 ctl_io_error_print(io, NULL); 13010 fe_done(io); 13011 return; 13012 default: 13013 panic("%s: Invalid CTL I/O type %d\n", 13014 __func__, io->io_hdr.io_type); 13015 } 13016 13017 if (lun == NULL) { 13018 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13019 io->io_hdr.nexus.targ_mapped_lun)); 13020 goto bailout; 13021 } 13022 13023 mtx_lock(&lun->lun_lock); 13024 13025 /* 13026 * Check to see if we have any informational exception and status 13027 * of this command can be modified to report it in form of either 13028 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13029 */ 13030 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13031 io->io_hdr.status == CTL_SUCCESS && 13032 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13033 uint8_t mrie = lun->MODE_IE.mrie; 13034 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13035 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13036 if (((mrie == SIEP_MRIE_REC_COND && per) || 13037 mrie == SIEP_MRIE_REC_UNCOND || 13038 mrie == SIEP_MRIE_NO_SENSE) && 13039 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13040 CTL_CMD_FLAG_NO_SENSE) == 0) { 13041 ctl_set_sense(&io->scsiio, 13042 /*current_error*/ 1, 13043 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13044 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13045 /*asc*/ lun->ie_asc, 13046 /*ascq*/ lun->ie_ascq, 13047 SSD_ELEM_NONE); 13048 lun->ie_reported = 1; 13049 } 13050 } else if (lun->ie_reported < 0) 13051 lun->ie_reported = 0; 13052 13053 /* 13054 * Check to see if we have any errors to inject here. We only 13055 * inject errors for commands that don't already have errors set. 13056 */ 13057 if (!STAILQ_EMPTY(&lun->error_list) && 13058 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13059 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13060 ctl_inject_error(lun, io); 13061 13062 /* 13063 * XXX KDM how do we treat commands that aren't completed 13064 * successfully? 13065 * 13066 * XXX KDM should we also track I/O latency? 13067 */ 13068 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13069 io->io_hdr.io_type == CTL_IO_SCSI) { 13070 int type; 13071 #ifdef CTL_TIME_IO 13072 struct bintime bt; 13073 13074 getbinuptime(&bt); 13075 bintime_sub(&bt, &io->io_hdr.start_bt); 13076 #endif 13077 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13078 CTL_FLAG_DATA_IN) 13079 type = CTL_STATS_READ; 13080 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13081 CTL_FLAG_DATA_OUT) 13082 type = CTL_STATS_WRITE; 13083 else 13084 type = CTL_STATS_NO_IO; 13085 13086 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13087 lun->stats.operations[type] ++; 13088 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13089 #ifdef CTL_TIME_IO 13090 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13091 bintime_add(&lun->stats.time[type], &bt); 13092 #endif 13093 13094 mtx_lock(&port->port_lock); 13095 port->stats.bytes[type] += io->scsiio.kern_total_len; 13096 port->stats.operations[type] ++; 13097 port->stats.dmas[type] += io->io_hdr.num_dmas; 13098 #ifdef CTL_TIME_IO 13099 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13100 bintime_add(&port->stats.time[type], &bt); 13101 #endif 13102 mtx_unlock(&port->port_lock); 13103 } 13104 13105 /* 13106 * Run through the blocked queue of this I/O and see if anything 13107 * can be unblocked, now that this I/O is done and will be removed. 13108 * We need to do it before removal to have OOA position to start. 13109 */ 13110 ctl_try_unblock_others(lun, io, TRUE); 13111 13112 /* 13113 * Remove this from the OOA queue. 13114 */ 13115 LIST_REMOVE(&io->io_hdr, ooa_links); 13116 #ifdef CTL_TIME_IO 13117 if (LIST_EMPTY(&lun->ooa_queue)) 13118 lun->last_busy = getsbinuptime(); 13119 #endif 13120 13121 /* 13122 * If the LUN has been invalidated, free it if there is nothing 13123 * left on its OOA queue. 13124 */ 13125 if ((lun->flags & CTL_LUN_INVALID) 13126 && LIST_EMPTY(&lun->ooa_queue)) { 13127 mtx_unlock(&lun->lun_lock); 13128 ctl_free_lun(lun); 13129 } else 13130 mtx_unlock(&lun->lun_lock); 13131 13132 bailout: 13133 13134 /* 13135 * If this command has been aborted, make sure we set the status 13136 * properly. The FETD is responsible for freeing the I/O and doing 13137 * whatever it needs to do to clean up its state. 13138 */ 13139 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13140 ctl_set_task_aborted(&io->scsiio); 13141 13142 /* 13143 * If enabled, print command error status. 13144 */ 13145 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13146 (ctl_debug & CTL_DEBUG_INFO) != 0) 13147 ctl_io_error_print(io, NULL); 13148 13149 /* 13150 * Tell the FETD or the other shelf controller we're done with this 13151 * command. Note that only SCSI commands get to this point. Task 13152 * management commands are completed above. 13153 */ 13154 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13155 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13156 memset(&msg, 0, sizeof(msg)); 13157 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13158 msg.hdr.serializing_sc = io->io_hdr.remote_io; 13159 msg.hdr.nexus = io->io_hdr.nexus; 13160 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13161 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13162 M_WAITOK); 13163 } 13164 13165 fe_done(io); 13166 } 13167 13168 /* 13169 * Front end should call this if it doesn't do autosense. When the request 13170 * sense comes back in from the initiator, we'll dequeue this and send it. 13171 */ 13172 int 13173 ctl_queue_sense(union ctl_io *io) 13174 { 13175 struct ctl_softc *softc = CTL_SOFTC(io); 13176 struct ctl_port *port = CTL_PORT(io); 13177 struct ctl_lun *lun; 13178 struct scsi_sense_data *ps; 13179 uint32_t initidx, p, targ_lun; 13180 13181 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13182 13183 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13184 13185 /* 13186 * LUN lookup will likely move to the ctl_work_thread() once we 13187 * have our new queueing infrastructure (that doesn't put things on 13188 * a per-LUN queue initially). That is so that we can handle 13189 * things like an INQUIRY to a LUN that we don't have enabled. We 13190 * can't deal with that right now. 13191 * If we don't have a LUN for this, just toss the sense information. 13192 */ 13193 mtx_lock(&softc->ctl_lock); 13194 if (targ_lun >= ctl_max_luns || 13195 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13196 mtx_unlock(&softc->ctl_lock); 13197 goto bailout; 13198 } 13199 mtx_lock(&lun->lun_lock); 13200 mtx_unlock(&softc->ctl_lock); 13201 13202 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13203 p = initidx / CTL_MAX_INIT_PER_PORT; 13204 if (lun->pending_sense[p] == NULL) { 13205 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13206 M_CTL, M_NOWAIT | M_ZERO); 13207 } 13208 if ((ps = lun->pending_sense[p]) != NULL) { 13209 ps += initidx % CTL_MAX_INIT_PER_PORT; 13210 memset(ps, 0, sizeof(*ps)); 13211 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13212 } 13213 mtx_unlock(&lun->lun_lock); 13214 13215 bailout: 13216 ctl_free_io(io); 13217 return (CTL_RETVAL_COMPLETE); 13218 } 13219 13220 /* 13221 * Primary command inlet from frontend ports. All SCSI and task I/O 13222 * requests must go through this function. 13223 */ 13224 int 13225 ctl_queue(union ctl_io *io) 13226 { 13227 struct ctl_port *port = CTL_PORT(io); 13228 13229 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13230 13231 #ifdef CTL_TIME_IO 13232 io->io_hdr.start_time = time_uptime; 13233 getbinuptime(&io->io_hdr.start_bt); 13234 #endif /* CTL_TIME_IO */ 13235 13236 /* Map FE-specific LUN ID into global one. */ 13237 io->io_hdr.nexus.targ_mapped_lun = 13238 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13239 13240 switch (io->io_hdr.io_type) { 13241 case CTL_IO_SCSI: 13242 case CTL_IO_TASK: 13243 if (ctl_debug & CTL_DEBUG_CDB) 13244 ctl_io_print(io); 13245 ctl_enqueue_incoming(io); 13246 break; 13247 default: 13248 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13249 return (EINVAL); 13250 } 13251 13252 return (CTL_RETVAL_COMPLETE); 13253 } 13254 13255 int 13256 ctl_run(union ctl_io *io) 13257 { 13258 struct ctl_port *port = CTL_PORT(io); 13259 13260 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0])); 13261 13262 #ifdef CTL_TIME_IO 13263 io->io_hdr.start_time = time_uptime; 13264 getbinuptime(&io->io_hdr.start_bt); 13265 #endif /* CTL_TIME_IO */ 13266 13267 /* Map FE-specific LUN ID into global one. */ 13268 io->io_hdr.nexus.targ_mapped_lun = 13269 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13270 13271 switch (io->io_hdr.io_type) { 13272 case CTL_IO_SCSI: 13273 if (ctl_debug & CTL_DEBUG_CDB) 13274 ctl_io_print(io); 13275 ctl_scsiio_precheck(&io->scsiio); 13276 break; 13277 case CTL_IO_TASK: 13278 if (ctl_debug & CTL_DEBUG_CDB) 13279 ctl_io_print(io); 13280 ctl_run_task(io); 13281 break; 13282 default: 13283 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type); 13284 return (EINVAL); 13285 } 13286 13287 return (CTL_RETVAL_COMPLETE); 13288 } 13289 13290 #ifdef CTL_IO_DELAY 13291 static void 13292 ctl_done_timer_wakeup(void *arg) 13293 { 13294 union ctl_io *io; 13295 13296 io = (union ctl_io *)arg; 13297 ctl_done(io); 13298 } 13299 #endif /* CTL_IO_DELAY */ 13300 13301 void 13302 ctl_serseq_done(union ctl_io *io) 13303 { 13304 struct ctl_lun *lun = CTL_LUN(io); 13305 13306 if (lun->be_lun == NULL || 13307 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13308 return; 13309 mtx_lock(&lun->lun_lock); 13310 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13311 ctl_try_unblock_others(lun, io, FALSE); 13312 mtx_unlock(&lun->lun_lock); 13313 } 13314 13315 void 13316 ctl_done(union ctl_io *io) 13317 { 13318 13319 /* 13320 * Enable this to catch duplicate completion issues. 13321 */ 13322 #if 0 13323 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13324 printf("%s: type %d msg %d cdb %x iptl: " 13325 "%u:%u:%u tag 0x%04x " 13326 "flag %#x status %x\n", 13327 __func__, 13328 io->io_hdr.io_type, 13329 io->io_hdr.msg_type, 13330 io->scsiio.cdb[0], 13331 io->io_hdr.nexus.initid, 13332 io->io_hdr.nexus.targ_port, 13333 io->io_hdr.nexus.targ_lun, 13334 (io->io_hdr.io_type == 13335 CTL_IO_TASK) ? 13336 io->taskio.tag_num : 13337 io->scsiio.tag_num, 13338 io->io_hdr.flags, 13339 io->io_hdr.status); 13340 } else 13341 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13342 #endif 13343 13344 /* 13345 * This is an internal copy of an I/O, and should not go through 13346 * the normal done processing logic. 13347 */ 13348 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13349 return; 13350 13351 #ifdef CTL_IO_DELAY 13352 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13353 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13354 } else { 13355 struct ctl_lun *lun = CTL_LUN(io); 13356 13357 if ((lun != NULL) 13358 && (lun->delay_info.done_delay > 0)) { 13359 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13360 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13361 callout_reset(&io->io_hdr.delay_callout, 13362 lun->delay_info.done_delay * hz, 13363 ctl_done_timer_wakeup, io); 13364 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13365 lun->delay_info.done_delay = 0; 13366 return; 13367 } 13368 } 13369 #endif /* CTL_IO_DELAY */ 13370 13371 ctl_enqueue_done(io); 13372 } 13373 13374 static void 13375 ctl_work_thread(void *arg) 13376 { 13377 struct ctl_thread *thr = (struct ctl_thread *)arg; 13378 struct ctl_softc *softc = thr->ctl_softc; 13379 union ctl_io *io; 13380 int retval; 13381 13382 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13383 thread_lock(curthread); 13384 sched_prio(curthread, PUSER - 1); 13385 thread_unlock(curthread); 13386 13387 while (!softc->shutdown) { 13388 /* 13389 * We handle the queues in this order: 13390 * - ISC 13391 * - done queue (to free up resources, unblock other commands) 13392 * - incoming queue 13393 * - RtR queue 13394 * 13395 * If those queues are empty, we break out of the loop and 13396 * go to sleep. 13397 */ 13398 mtx_lock(&thr->queue_lock); 13399 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13400 if (io != NULL) { 13401 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13402 mtx_unlock(&thr->queue_lock); 13403 ctl_handle_isc(io); 13404 continue; 13405 } 13406 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13407 if (io != NULL) { 13408 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13409 /* clear any blocked commands, call fe_done */ 13410 mtx_unlock(&thr->queue_lock); 13411 ctl_process_done(io); 13412 continue; 13413 } 13414 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13415 if (io != NULL) { 13416 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13417 mtx_unlock(&thr->queue_lock); 13418 if (io->io_hdr.io_type == CTL_IO_TASK) 13419 ctl_run_task(io); 13420 else 13421 ctl_scsiio_precheck(&io->scsiio); 13422 continue; 13423 } 13424 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13425 if (io != NULL) { 13426 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13427 mtx_unlock(&thr->queue_lock); 13428 retval = ctl_scsiio(&io->scsiio); 13429 if (retval != CTL_RETVAL_COMPLETE) 13430 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13431 continue; 13432 } 13433 13434 /* Sleep until we have something to do. */ 13435 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0); 13436 } 13437 thr->thread = NULL; 13438 kthread_exit(); 13439 } 13440 13441 static void 13442 ctl_thresh_thread(void *arg) 13443 { 13444 struct ctl_softc *softc = (struct ctl_softc *)arg; 13445 struct ctl_lun *lun; 13446 struct ctl_logical_block_provisioning_page *page; 13447 const char *attr; 13448 union ctl_ha_msg msg; 13449 uint64_t thres, val; 13450 int i, e, set; 13451 13452 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13453 thread_lock(curthread); 13454 sched_prio(curthread, PUSER - 1); 13455 thread_unlock(curthread); 13456 13457 while (!softc->shutdown) { 13458 mtx_lock(&softc->ctl_lock); 13459 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13460 if ((lun->flags & CTL_LUN_DISABLED) || 13461 (lun->flags & CTL_LUN_NO_MEDIA) || 13462 lun->backend->lun_attr == NULL) 13463 continue; 13464 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13465 softc->ha_mode == CTL_HA_MODE_XFER) 13466 continue; 13467 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13468 continue; 13469 e = 0; 13470 page = &lun->MODE_LBP; 13471 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13472 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13473 continue; 13474 thres = scsi_4btoul(page->descr[i].count); 13475 thres <<= CTL_LBP_EXPONENT; 13476 switch (page->descr[i].resource) { 13477 case 0x01: 13478 attr = "blocksavail"; 13479 break; 13480 case 0x02: 13481 attr = "blocksused"; 13482 break; 13483 case 0xf1: 13484 attr = "poolblocksavail"; 13485 break; 13486 case 0xf2: 13487 attr = "poolblocksused"; 13488 break; 13489 default: 13490 continue; 13491 } 13492 mtx_unlock(&softc->ctl_lock); // XXX 13493 val = lun->backend->lun_attr(lun->be_lun, attr); 13494 mtx_lock(&softc->ctl_lock); 13495 if (val == UINT64_MAX) 13496 continue; 13497 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13498 == SLBPPD_ARMING_INC) 13499 e = (val >= thres); 13500 else 13501 e = (val <= thres); 13502 if (e) 13503 break; 13504 } 13505 mtx_lock(&lun->lun_lock); 13506 if (e) { 13507 scsi_u64to8b((uint8_t *)&page->descr[i] - 13508 (uint8_t *)page, lun->ua_tpt_info); 13509 if (lun->lasttpt == 0 || 13510 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13511 lun->lasttpt = time_uptime; 13512 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13513 set = 1; 13514 } else 13515 set = 0; 13516 } else { 13517 lun->lasttpt = 0; 13518 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13519 set = -1; 13520 } 13521 mtx_unlock(&lun->lun_lock); 13522 if (set != 0 && 13523 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13524 /* Send msg to other side. */ 13525 bzero(&msg.ua, sizeof(msg.ua)); 13526 msg.hdr.msg_type = CTL_MSG_UA; 13527 msg.hdr.nexus.initid = -1; 13528 msg.hdr.nexus.targ_port = -1; 13529 msg.hdr.nexus.targ_lun = lun->lun; 13530 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13531 msg.ua.ua_all = 1; 13532 msg.ua.ua_set = (set > 0); 13533 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13534 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13535 mtx_unlock(&softc->ctl_lock); // XXX 13536 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13537 sizeof(msg.ua), M_WAITOK); 13538 mtx_lock(&softc->ctl_lock); 13539 } 13540 } 13541 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13542 PDROP, "-", CTL_LBP_PERIOD * hz); 13543 } 13544 softc->thresh_thread = NULL; 13545 kthread_exit(); 13546 } 13547 13548 static void 13549 ctl_enqueue_incoming(union ctl_io *io) 13550 { 13551 struct ctl_softc *softc = CTL_SOFTC(io); 13552 struct ctl_thread *thr; 13553 u_int idx; 13554 13555 idx = (io->io_hdr.nexus.targ_port * 127 + 13556 io->io_hdr.nexus.initid) % worker_threads; 13557 thr = &softc->threads[idx]; 13558 mtx_lock(&thr->queue_lock); 13559 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13560 mtx_unlock(&thr->queue_lock); 13561 wakeup(thr); 13562 } 13563 13564 static void 13565 ctl_enqueue_rtr(union ctl_io *io) 13566 { 13567 struct ctl_softc *softc = CTL_SOFTC(io); 13568 struct ctl_thread *thr; 13569 13570 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13571 mtx_lock(&thr->queue_lock); 13572 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13573 mtx_unlock(&thr->queue_lock); 13574 wakeup(thr); 13575 } 13576 13577 static void 13578 ctl_enqueue_done(union ctl_io *io) 13579 { 13580 struct ctl_softc *softc = CTL_SOFTC(io); 13581 struct ctl_thread *thr; 13582 13583 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13584 mtx_lock(&thr->queue_lock); 13585 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13586 mtx_unlock(&thr->queue_lock); 13587 wakeup(thr); 13588 } 13589 13590 static void 13591 ctl_enqueue_isc(union ctl_io *io) 13592 { 13593 struct ctl_softc *softc = CTL_SOFTC(io); 13594 struct ctl_thread *thr; 13595 13596 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13597 mtx_lock(&thr->queue_lock); 13598 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13599 mtx_unlock(&thr->queue_lock); 13600 wakeup(thr); 13601 } 13602 13603 /* 13604 * vim: ts=8 13605 */ 13606