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_seq_check(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, const uint8_t *serialize_row, 506 union ctl_io *ooa_io); 507 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 508 union ctl_io **starting_io); 509 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, 510 bool skip); 511 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io, 512 bool skip); 513 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 514 const struct ctl_cmd_entry *entry, 515 struct ctl_scsiio *ctsio); 516 static void ctl_failover_lun(union ctl_io *io); 517 static void ctl_scsiio_precheck(struct ctl_scsiio *ctsio); 518 static int ctl_scsiio(struct ctl_scsiio *ctsio); 519 520 static int ctl_target_reset(union ctl_io *io); 521 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, 522 ctl_ua_type ua_type); 523 static int ctl_lun_reset(union ctl_io *io); 524 static int ctl_abort_task(union ctl_io *io); 525 static int ctl_abort_task_set(union ctl_io *io); 526 static int ctl_query_task(union ctl_io *io, int task_set); 527 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 528 ctl_ua_type ua_type); 529 static int ctl_i_t_nexus_reset(union ctl_io *io); 530 static int ctl_query_async_event(union ctl_io *io); 531 static void ctl_run_task(union ctl_io *io); 532 #ifdef CTL_IO_DELAY 533 static void ctl_datamove_timer_wakeup(void *arg); 534 static void ctl_done_timer_wakeup(void *arg); 535 #endif /* CTL_IO_DELAY */ 536 537 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 538 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 539 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr); 540 static void ctl_datamove_remote_write(union ctl_io *io); 541 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr); 542 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 543 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 544 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 545 ctl_ha_dt_cb callback); 546 static void ctl_datamove_remote_read(union ctl_io *io); 547 static void ctl_datamove_remote(union ctl_io *io); 548 static void ctl_process_done(union ctl_io *io); 549 static void ctl_thresh_thread(void *arg); 550 static void ctl_work_thread(void *arg); 551 static void ctl_enqueue_incoming(union ctl_io *io); 552 static void ctl_enqueue_rtr(union ctl_io *io); 553 static void ctl_enqueue_done(union ctl_io *io); 554 static void ctl_enqueue_isc(union ctl_io *io); 555 static const struct ctl_cmd_entry * 556 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 557 static const struct ctl_cmd_entry * 558 ctl_validate_command(struct ctl_scsiio *ctsio); 559 static int ctl_cmd_applicable(uint8_t lun_type, 560 const struct ctl_cmd_entry *entry); 561 static int ctl_ha_init(void); 562 static int ctl_ha_shutdown(void); 563 564 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 565 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 566 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 567 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 568 569 /* 570 * Load the serialization table. This isn't very pretty, but is probably 571 * the easiest way to do it. 572 */ 573 #include "ctl_ser_table.c" 574 575 /* 576 * We only need to define open, close and ioctl routines for this driver. 577 */ 578 static struct cdevsw ctl_cdevsw = { 579 .d_version = D_VERSION, 580 .d_flags = 0, 581 .d_open = ctl_open, 582 .d_close = ctl_close, 583 .d_ioctl = ctl_ioctl, 584 .d_name = "ctl", 585 }; 586 587 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 588 589 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 590 591 static moduledata_t ctl_moduledata = { 592 "ctl", 593 ctl_module_event_handler, 594 NULL 595 }; 596 597 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 598 MODULE_VERSION(ctl, 1); 599 600 static struct ctl_frontend ha_frontend = 601 { 602 .name = "ha", 603 .init = ctl_ha_init, 604 .shutdown = ctl_ha_shutdown, 605 }; 606 607 static int 608 ctl_ha_init(void) 609 { 610 struct ctl_softc *softc = control_softc; 611 612 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 613 &softc->othersc_pool) != 0) 614 return (ENOMEM); 615 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 616 ctl_pool_free(softc->othersc_pool); 617 return (EIO); 618 } 619 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 620 != CTL_HA_STATUS_SUCCESS) { 621 ctl_ha_msg_destroy(softc); 622 ctl_pool_free(softc->othersc_pool); 623 return (EIO); 624 } 625 return (0); 626 }; 627 628 static int 629 ctl_ha_shutdown(void) 630 { 631 struct ctl_softc *softc = control_softc; 632 struct ctl_port *port; 633 634 ctl_ha_msg_shutdown(softc); 635 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 636 return (EIO); 637 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 638 return (EIO); 639 ctl_pool_free(softc->othersc_pool); 640 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 641 ctl_port_deregister(port); 642 free(port->port_name, M_CTL); 643 free(port, M_CTL); 644 } 645 return (0); 646 }; 647 648 static void 649 ctl_ha_datamove(union ctl_io *io) 650 { 651 struct ctl_lun *lun = CTL_LUN(io); 652 struct ctl_sg_entry *sgl; 653 union ctl_ha_msg msg; 654 uint32_t sg_entries_sent; 655 int do_sg_copy, i, j; 656 657 memset(&msg.dt, 0, sizeof(msg.dt)); 658 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 659 msg.hdr.original_sc = io->io_hdr.remote_io; 660 msg.hdr.serializing_sc = io; 661 msg.hdr.nexus = io->io_hdr.nexus; 662 msg.hdr.status = io->io_hdr.status; 663 msg.dt.flags = io->io_hdr.flags; 664 665 /* 666 * We convert everything into a S/G list here. We can't 667 * pass by reference, only by value between controllers. 668 * So we can't pass a pointer to the S/G list, only as many 669 * S/G entries as we can fit in here. If it's possible for 670 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 671 * then we need to break this up into multiple transfers. 672 */ 673 if (io->scsiio.kern_sg_entries == 0) { 674 msg.dt.kern_sg_entries = 1; 675 #if 0 676 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 677 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 678 } else { 679 /* XXX KDM use busdma here! */ 680 msg.dt.sg_list[0].addr = 681 (void *)vtophys(io->scsiio.kern_data_ptr); 682 } 683 #else 684 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 685 ("HA does not support BUS_ADDR")); 686 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 687 #endif 688 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 689 do_sg_copy = 0; 690 } else { 691 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 692 do_sg_copy = 1; 693 } 694 695 msg.dt.kern_data_len = io->scsiio.kern_data_len; 696 msg.dt.kern_total_len = io->scsiio.kern_total_len; 697 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 698 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 699 msg.dt.sg_sequence = 0; 700 701 /* 702 * Loop until we've sent all of the S/G entries. On the 703 * other end, we'll recompose these S/G entries into one 704 * contiguous list before processing. 705 */ 706 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 707 msg.dt.sg_sequence++) { 708 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 709 sizeof(msg.dt.sg_list[0])), 710 msg.dt.kern_sg_entries - sg_entries_sent); 711 if (do_sg_copy != 0) { 712 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 713 for (i = sg_entries_sent, j = 0; 714 i < msg.dt.cur_sg_entries; i++, j++) { 715 #if 0 716 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 717 msg.dt.sg_list[j].addr = sgl[i].addr; 718 } else { 719 /* XXX KDM use busdma here! */ 720 msg.dt.sg_list[j].addr = 721 (void *)vtophys(sgl[i].addr); 722 } 723 #else 724 KASSERT((io->io_hdr.flags & 725 CTL_FLAG_BUS_ADDR) == 0, 726 ("HA does not support BUS_ADDR")); 727 msg.dt.sg_list[j].addr = sgl[i].addr; 728 #endif 729 msg.dt.sg_list[j].len = sgl[i].len; 730 } 731 } 732 733 sg_entries_sent += msg.dt.cur_sg_entries; 734 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 735 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 736 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 737 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 738 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 739 io->io_hdr.port_status = 31341; 740 ctl_datamove_done(io, true); 741 return; 742 } 743 msg.dt.sent_sg_entries = sg_entries_sent; 744 } 745 746 /* 747 * Officially handover the request from us to peer. 748 * If failover has just happened, then we must return error. 749 * If failover happen just after, then it is not our problem. 750 */ 751 if (lun) 752 mtx_lock(&lun->lun_lock); 753 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 754 if (lun) 755 mtx_unlock(&lun->lun_lock); 756 io->io_hdr.port_status = 31342; 757 ctl_datamove_done(io, true); 758 return; 759 } 760 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 761 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 762 if (lun) 763 mtx_unlock(&lun->lun_lock); 764 } 765 766 static void 767 ctl_ha_done(union ctl_io *io) 768 { 769 union ctl_ha_msg msg; 770 771 if (io->io_hdr.io_type == CTL_IO_SCSI) { 772 memset(&msg, 0, sizeof(msg)); 773 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 774 msg.hdr.original_sc = io->io_hdr.remote_io; 775 msg.hdr.nexus = io->io_hdr.nexus; 776 msg.hdr.status = io->io_hdr.status; 777 msg.scsi.scsi_status = io->scsiio.scsi_status; 778 msg.scsi.tag_num = io->scsiio.tag_num; 779 msg.scsi.tag_type = io->scsiio.tag_type; 780 msg.scsi.sense_len = io->scsiio.sense_len; 781 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 782 io->scsiio.sense_len); 783 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 784 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 785 msg.scsi.sense_len, M_WAITOK); 786 } 787 ctl_free_io(io); 788 } 789 790 static void 791 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 792 union ctl_ha_msg *msg_info) 793 { 794 struct ctl_scsiio *ctsio; 795 796 if (msg_info->hdr.original_sc == NULL) { 797 printf("%s: original_sc == NULL!\n", __func__); 798 /* XXX KDM now what? */ 799 return; 800 } 801 802 ctsio = &msg_info->hdr.original_sc->scsiio; 803 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 804 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 805 ctsio->io_hdr.status = msg_info->hdr.status; 806 ctsio->scsi_status = msg_info->scsi.scsi_status; 807 ctsio->sense_len = msg_info->scsi.sense_len; 808 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 809 msg_info->scsi.sense_len); 810 ctl_enqueue_isc((union ctl_io *)ctsio); 811 } 812 813 static void 814 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 815 union ctl_ha_msg *msg_info) 816 { 817 struct ctl_scsiio *ctsio; 818 819 if (msg_info->hdr.serializing_sc == NULL) { 820 printf("%s: serializing_sc == NULL!\n", __func__); 821 /* XXX KDM now what? */ 822 return; 823 } 824 825 ctsio = &msg_info->hdr.serializing_sc->scsiio; 826 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 827 ctl_enqueue_isc((union ctl_io *)ctsio); 828 } 829 830 void 831 ctl_isc_announce_lun(struct ctl_lun *lun) 832 { 833 struct ctl_softc *softc = lun->ctl_softc; 834 union ctl_ha_msg *msg; 835 struct ctl_ha_msg_lun_pr_key pr_key; 836 int i, k; 837 838 if (softc->ha_link != CTL_HA_LINK_ONLINE) 839 return; 840 mtx_lock(&lun->lun_lock); 841 i = sizeof(msg->lun); 842 if (lun->lun_devid) 843 i += lun->lun_devid->len; 844 i += sizeof(pr_key) * lun->pr_key_count; 845 alloc: 846 mtx_unlock(&lun->lun_lock); 847 msg = malloc(i, M_CTL, M_WAITOK); 848 mtx_lock(&lun->lun_lock); 849 k = sizeof(msg->lun); 850 if (lun->lun_devid) 851 k += lun->lun_devid->len; 852 k += sizeof(pr_key) * lun->pr_key_count; 853 if (i < k) { 854 free(msg, M_CTL); 855 i = k; 856 goto alloc; 857 } 858 bzero(&msg->lun, sizeof(msg->lun)); 859 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 860 msg->hdr.nexus.targ_lun = lun->lun; 861 msg->hdr.nexus.targ_mapped_lun = lun->lun; 862 msg->lun.flags = lun->flags; 863 msg->lun.pr_generation = lun->pr_generation; 864 msg->lun.pr_res_idx = lun->pr_res_idx; 865 msg->lun.pr_res_type = lun->pr_res_type; 866 msg->lun.pr_key_count = lun->pr_key_count; 867 i = 0; 868 if (lun->lun_devid) { 869 msg->lun.lun_devid_len = lun->lun_devid->len; 870 memcpy(&msg->lun.data[i], lun->lun_devid->data, 871 msg->lun.lun_devid_len); 872 i += msg->lun.lun_devid_len; 873 } 874 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 875 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 876 continue; 877 pr_key.pr_iid = k; 878 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 879 i += sizeof(pr_key); 880 } 881 mtx_unlock(&lun->lun_lock); 882 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->lun, sizeof(msg->lun) + i, 883 M_WAITOK); 884 free(msg, M_CTL); 885 886 if (lun->flags & CTL_LUN_PRIMARY_SC) { 887 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 888 ctl_isc_announce_mode(lun, -1, 889 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 890 lun->mode_pages.index[i].subpage); 891 } 892 } 893 } 894 895 void 896 ctl_isc_announce_port(struct ctl_port *port) 897 { 898 struct ctl_softc *softc = port->ctl_softc; 899 union ctl_ha_msg *msg; 900 int i; 901 902 if (port->targ_port < softc->port_min || 903 port->targ_port >= softc->port_max || 904 softc->ha_link != CTL_HA_LINK_ONLINE) 905 return; 906 i = sizeof(msg->port) + strlen(port->port_name) + 1; 907 if (port->lun_map) 908 i += port->lun_map_size * sizeof(uint32_t); 909 if (port->port_devid) 910 i += port->port_devid->len; 911 if (port->target_devid) 912 i += port->target_devid->len; 913 if (port->init_devid) 914 i += port->init_devid->len; 915 msg = malloc(i, M_CTL, M_WAITOK); 916 bzero(&msg->port, sizeof(msg->port)); 917 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 918 msg->hdr.nexus.targ_port = port->targ_port; 919 msg->port.port_type = port->port_type; 920 msg->port.physical_port = port->physical_port; 921 msg->port.virtual_port = port->virtual_port; 922 msg->port.status = port->status; 923 i = 0; 924 msg->port.name_len = sprintf(&msg->port.data[i], 925 "%d:%s", softc->ha_id, port->port_name) + 1; 926 i += msg->port.name_len; 927 if (port->lun_map) { 928 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 929 memcpy(&msg->port.data[i], port->lun_map, 930 msg->port.lun_map_len); 931 i += msg->port.lun_map_len; 932 } 933 if (port->port_devid) { 934 msg->port.port_devid_len = port->port_devid->len; 935 memcpy(&msg->port.data[i], port->port_devid->data, 936 msg->port.port_devid_len); 937 i += msg->port.port_devid_len; 938 } 939 if (port->target_devid) { 940 msg->port.target_devid_len = port->target_devid->len; 941 memcpy(&msg->port.data[i], port->target_devid->data, 942 msg->port.target_devid_len); 943 i += msg->port.target_devid_len; 944 } 945 if (port->init_devid) { 946 msg->port.init_devid_len = port->init_devid->len; 947 memcpy(&msg->port.data[i], port->init_devid->data, 948 msg->port.init_devid_len); 949 i += msg->port.init_devid_len; 950 } 951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 952 M_WAITOK); 953 free(msg, M_CTL); 954 } 955 956 void 957 ctl_isc_announce_iid(struct ctl_port *port, int iid) 958 { 959 struct ctl_softc *softc = port->ctl_softc; 960 union ctl_ha_msg *msg; 961 int i, l; 962 963 if (port->targ_port < softc->port_min || 964 port->targ_port >= softc->port_max || 965 softc->ha_link != CTL_HA_LINK_ONLINE) 966 return; 967 mtx_lock(&softc->ctl_lock); 968 i = sizeof(msg->iid); 969 l = 0; 970 if (port->wwpn_iid[iid].name) 971 l = strlen(port->wwpn_iid[iid].name) + 1; 972 i += l; 973 msg = malloc(i, M_CTL, M_NOWAIT); 974 if (msg == NULL) { 975 mtx_unlock(&softc->ctl_lock); 976 return; 977 } 978 bzero(&msg->iid, sizeof(msg->iid)); 979 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 980 msg->hdr.nexus.targ_port = port->targ_port; 981 msg->hdr.nexus.initid = iid; 982 msg->iid.in_use = port->wwpn_iid[iid].in_use; 983 msg->iid.name_len = l; 984 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 985 if (port->wwpn_iid[iid].name) 986 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 987 mtx_unlock(&softc->ctl_lock); 988 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 989 free(msg, M_CTL); 990 } 991 992 void 993 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 994 uint8_t page, uint8_t subpage) 995 { 996 struct ctl_softc *softc = lun->ctl_softc; 997 union ctl_ha_msg *msg; 998 u_int i, l; 999 1000 if (softc->ha_link != CTL_HA_LINK_ONLINE) 1001 return; 1002 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1003 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1004 page && lun->mode_pages.index[i].subpage == subpage) 1005 break; 1006 } 1007 if (i == CTL_NUM_MODE_PAGES) 1008 return; 1009 1010 /* Don't try to replicate pages not present on this device. */ 1011 if (lun->mode_pages.index[i].page_data == NULL) 1012 return; 1013 1014 l = sizeof(msg->mode) + lun->mode_pages.index[i].page_len; 1015 msg = malloc(l, M_CTL, M_WAITOK | M_ZERO); 1016 msg->hdr.msg_type = CTL_MSG_MODE_SYNC; 1017 msg->hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 1018 msg->hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 1019 msg->hdr.nexus.targ_lun = lun->lun; 1020 msg->hdr.nexus.targ_mapped_lun = lun->lun; 1021 msg->mode.page_code = page; 1022 msg->mode.subpage = subpage; 1023 msg->mode.page_len = lun->mode_pages.index[i].page_len; 1024 memcpy(msg->mode.data, lun->mode_pages.index[i].page_data, 1025 msg->mode.page_len); 1026 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->mode, l, M_WAITOK); 1027 free(msg, M_CTL); 1028 } 1029 1030 static void 1031 ctl_isc_ha_link_up(struct ctl_softc *softc) 1032 { 1033 struct ctl_port *port; 1034 struct ctl_lun *lun; 1035 union ctl_ha_msg msg; 1036 int i; 1037 1038 /* Announce this node parameters to peer for validation. */ 1039 msg.login.msg_type = CTL_MSG_LOGIN; 1040 msg.login.version = CTL_HA_VERSION; 1041 msg.login.ha_mode = softc->ha_mode; 1042 msg.login.ha_id = softc->ha_id; 1043 msg.login.max_luns = ctl_max_luns; 1044 msg.login.max_ports = ctl_max_ports; 1045 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1046 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1047 M_WAITOK); 1048 1049 STAILQ_FOREACH(port, &softc->port_list, links) { 1050 ctl_isc_announce_port(port); 1051 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1052 if (port->wwpn_iid[i].in_use) 1053 ctl_isc_announce_iid(port, i); 1054 } 1055 } 1056 STAILQ_FOREACH(lun, &softc->lun_list, links) 1057 ctl_isc_announce_lun(lun); 1058 } 1059 1060 static void 1061 ctl_isc_ha_link_down(struct ctl_softc *softc) 1062 { 1063 struct ctl_port *port; 1064 struct ctl_lun *lun; 1065 union ctl_io *io; 1066 int i; 1067 1068 mtx_lock(&softc->ctl_lock); 1069 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1070 mtx_lock(&lun->lun_lock); 1071 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1072 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1073 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1074 } 1075 mtx_unlock(&lun->lun_lock); 1076 1077 mtx_unlock(&softc->ctl_lock); 1078 io = ctl_alloc_io(softc->othersc_pool); 1079 mtx_lock(&softc->ctl_lock); 1080 ctl_zero_io(io); 1081 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1082 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1083 ctl_enqueue_isc(io); 1084 } 1085 1086 STAILQ_FOREACH(port, &softc->port_list, links) { 1087 if (port->targ_port >= softc->port_min && 1088 port->targ_port < softc->port_max) 1089 continue; 1090 port->status &= ~CTL_PORT_STATUS_ONLINE; 1091 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1092 port->wwpn_iid[i].in_use = 0; 1093 free(port->wwpn_iid[i].name, M_CTL); 1094 port->wwpn_iid[i].name = NULL; 1095 } 1096 } 1097 mtx_unlock(&softc->ctl_lock); 1098 } 1099 1100 static void 1101 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1102 { 1103 struct ctl_lun *lun; 1104 uint32_t iid; 1105 1106 if (len < sizeof(msg->ua)) { 1107 printf("%s: Received truncated message %d < %zu\n", 1108 __func__, len, sizeof(msg->ua)); 1109 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1110 return; 1111 } 1112 1113 mtx_lock(&softc->ctl_lock); 1114 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1115 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1116 mtx_unlock(&softc->ctl_lock); 1117 return; 1118 } 1119 mtx_lock(&lun->lun_lock); 1120 mtx_unlock(&softc->ctl_lock); 1121 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1122 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1123 iid = ctl_get_initindex(&msg->hdr.nexus); 1124 if (msg->ua.ua_all) { 1125 if (msg->ua.ua_set) 1126 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1127 else 1128 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1129 } else { 1130 if (msg->ua.ua_set) 1131 ctl_est_ua(lun, iid, msg->ua.ua_type); 1132 else 1133 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1134 } 1135 mtx_unlock(&lun->lun_lock); 1136 } 1137 1138 static void 1139 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1140 { 1141 struct ctl_lun *lun; 1142 struct ctl_ha_msg_lun_pr_key pr_key; 1143 int i, k; 1144 ctl_lun_flags oflags; 1145 uint32_t targ_lun; 1146 1147 if (len < offsetof(struct ctl_ha_msg_lun, data[0])) { 1148 printf("%s: Received truncated message %d < %zu\n", 1149 __func__, len, offsetof(struct ctl_ha_msg_lun, data[0])); 1150 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1151 return; 1152 } 1153 i = msg->lun.lun_devid_len + msg->lun.pr_key_count * sizeof(pr_key); 1154 if (len < offsetof(struct ctl_ha_msg_lun, data[i])) { 1155 printf("%s: Received truncated message data %d < %zu\n", 1156 __func__, len, offsetof(struct ctl_ha_msg_lun, data[i])); 1157 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1158 return; 1159 } 1160 1161 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1162 mtx_lock(&softc->ctl_lock); 1163 if (targ_lun >= ctl_max_luns || 1164 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1165 mtx_unlock(&softc->ctl_lock); 1166 return; 1167 } 1168 mtx_lock(&lun->lun_lock); 1169 mtx_unlock(&softc->ctl_lock); 1170 if (lun->flags & CTL_LUN_DISABLED) { 1171 mtx_unlock(&lun->lun_lock); 1172 return; 1173 } 1174 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1175 if (msg->lun.lun_devid_len != i || (i > 0 && 1176 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1177 mtx_unlock(&lun->lun_lock); 1178 printf("%s: Received conflicting HA LUN %d\n", 1179 __func__, targ_lun); 1180 return; 1181 } else { 1182 /* Record whether peer is primary. */ 1183 oflags = lun->flags; 1184 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1185 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1186 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1187 else 1188 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1189 if (oflags != lun->flags) 1190 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1191 1192 /* If peer is primary and we are not -- use data */ 1193 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1194 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1195 lun->pr_generation = msg->lun.pr_generation; 1196 lun->pr_res_idx = msg->lun.pr_res_idx; 1197 lun->pr_res_type = msg->lun.pr_res_type; 1198 lun->pr_key_count = msg->lun.pr_key_count; 1199 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1200 ctl_clr_prkey(lun, k); 1201 for (k = 0; k < msg->lun.pr_key_count; k++) { 1202 memcpy(&pr_key, &msg->lun.data[i], 1203 sizeof(pr_key)); 1204 ctl_alloc_prkey(lun, pr_key.pr_iid); 1205 ctl_set_prkey(lun, pr_key.pr_iid, 1206 pr_key.pr_key); 1207 i += sizeof(pr_key); 1208 } 1209 } 1210 1211 mtx_unlock(&lun->lun_lock); 1212 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1213 __func__, targ_lun, 1214 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1215 "primary" : "secondary")); 1216 1217 /* If we are primary but peer doesn't know -- notify */ 1218 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1219 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1220 ctl_isc_announce_lun(lun); 1221 } 1222 } 1223 1224 static void 1225 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1226 { 1227 struct ctl_port *port; 1228 struct ctl_lun *lun; 1229 int i, new; 1230 1231 if (len < offsetof(struct ctl_ha_msg_port, data[0])) { 1232 printf("%s: Received truncated message %d < %zu\n", 1233 __func__, len, offsetof(struct ctl_ha_msg_port, data[0])); 1234 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1235 return; 1236 } 1237 i = msg->port.name_len + msg->port.lun_map_len + 1238 msg->port.port_devid_len + msg->port.target_devid_len + 1239 msg->port.init_devid_len; 1240 if (len < offsetof(struct ctl_ha_msg_port, data[i])) { 1241 printf("%s: Received truncated message data %d < %zu\n", 1242 __func__, len, offsetof(struct ctl_ha_msg_port, data[i])); 1243 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1244 return; 1245 } 1246 1247 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1248 if (port == NULL) { 1249 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1250 msg->hdr.nexus.targ_port)); 1251 new = 1; 1252 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1253 port->frontend = &ha_frontend; 1254 port->targ_port = msg->hdr.nexus.targ_port; 1255 port->fe_datamove = ctl_ha_datamove; 1256 port->fe_done = ctl_ha_done; 1257 } else if (port->frontend == &ha_frontend) { 1258 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1259 msg->hdr.nexus.targ_port)); 1260 new = 0; 1261 } else { 1262 printf("%s: Received conflicting HA port %d\n", 1263 __func__, msg->hdr.nexus.targ_port); 1264 return; 1265 } 1266 port->port_type = msg->port.port_type; 1267 port->physical_port = msg->port.physical_port; 1268 port->virtual_port = msg->port.virtual_port; 1269 port->status = msg->port.status; 1270 i = 0; 1271 free(port->port_name, M_CTL); 1272 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1273 M_CTL); 1274 i += msg->port.name_len; 1275 if (msg->port.lun_map_len != 0) { 1276 if (port->lun_map == NULL || 1277 port->lun_map_size * sizeof(uint32_t) < 1278 msg->port.lun_map_len) { 1279 port->lun_map_size = 0; 1280 free(port->lun_map, M_CTL); 1281 port->lun_map = malloc(msg->port.lun_map_len, 1282 M_CTL, M_WAITOK); 1283 } 1284 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1285 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1286 i += msg->port.lun_map_len; 1287 } else { 1288 port->lun_map_size = 0; 1289 free(port->lun_map, M_CTL); 1290 port->lun_map = NULL; 1291 } 1292 if (msg->port.port_devid_len != 0) { 1293 if (port->port_devid == NULL || 1294 port->port_devid->len < msg->port.port_devid_len) { 1295 free(port->port_devid, M_CTL); 1296 port->port_devid = malloc(sizeof(struct ctl_devid) + 1297 msg->port.port_devid_len, M_CTL, M_WAITOK); 1298 } 1299 memcpy(port->port_devid->data, &msg->port.data[i], 1300 msg->port.port_devid_len); 1301 port->port_devid->len = msg->port.port_devid_len; 1302 i += msg->port.port_devid_len; 1303 } else { 1304 free(port->port_devid, M_CTL); 1305 port->port_devid = NULL; 1306 } 1307 if (msg->port.target_devid_len != 0) { 1308 if (port->target_devid == NULL || 1309 port->target_devid->len < msg->port.target_devid_len) { 1310 free(port->target_devid, M_CTL); 1311 port->target_devid = malloc(sizeof(struct ctl_devid) + 1312 msg->port.target_devid_len, M_CTL, M_WAITOK); 1313 } 1314 memcpy(port->target_devid->data, &msg->port.data[i], 1315 msg->port.target_devid_len); 1316 port->target_devid->len = msg->port.target_devid_len; 1317 i += msg->port.target_devid_len; 1318 } else { 1319 free(port->target_devid, M_CTL); 1320 port->target_devid = NULL; 1321 } 1322 if (msg->port.init_devid_len != 0) { 1323 if (port->init_devid == NULL || 1324 port->init_devid->len < msg->port.init_devid_len) { 1325 free(port->init_devid, M_CTL); 1326 port->init_devid = malloc(sizeof(struct ctl_devid) + 1327 msg->port.init_devid_len, M_CTL, M_WAITOK); 1328 } 1329 memcpy(port->init_devid->data, &msg->port.data[i], 1330 msg->port.init_devid_len); 1331 port->init_devid->len = msg->port.init_devid_len; 1332 i += msg->port.init_devid_len; 1333 } else { 1334 free(port->init_devid, M_CTL); 1335 port->init_devid = NULL; 1336 } 1337 if (new) { 1338 if (ctl_port_register(port) != 0) { 1339 printf("%s: ctl_port_register() failed with error\n", 1340 __func__); 1341 } 1342 } 1343 mtx_lock(&softc->ctl_lock); 1344 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1345 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1346 continue; 1347 mtx_lock(&lun->lun_lock); 1348 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1349 mtx_unlock(&lun->lun_lock); 1350 } 1351 mtx_unlock(&softc->ctl_lock); 1352 } 1353 1354 static void 1355 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1356 { 1357 struct ctl_port *port; 1358 int i, iid; 1359 1360 if (len < offsetof(struct ctl_ha_msg_iid, data[0])) { 1361 printf("%s: Received truncated message %d < %zu\n", 1362 __func__, len, offsetof(struct ctl_ha_msg_iid, data[0])); 1363 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1364 return; 1365 } 1366 i = msg->iid.name_len; 1367 if (len < offsetof(struct ctl_ha_msg_iid, data[i])) { 1368 printf("%s: Received truncated message data %d < %zu\n", 1369 __func__, len, offsetof(struct ctl_ha_msg_iid, data[i])); 1370 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1371 return; 1372 } 1373 1374 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1375 if (port == NULL) { 1376 printf("%s: Received IID for unknown port %d\n", 1377 __func__, msg->hdr.nexus.targ_port); 1378 return; 1379 } 1380 iid = msg->hdr.nexus.initid; 1381 if (port->wwpn_iid[iid].in_use != 0 && 1382 msg->iid.in_use == 0) 1383 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1384 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1385 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1386 free(port->wwpn_iid[iid].name, M_CTL); 1387 if (msg->iid.name_len) { 1388 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1389 msg->iid.name_len, M_CTL); 1390 } else 1391 port->wwpn_iid[iid].name = NULL; 1392 } 1393 1394 static void 1395 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1396 { 1397 1398 if (len < sizeof(msg->login)) { 1399 printf("%s: Received truncated message %d < %zu\n", 1400 __func__, len, sizeof(msg->login)); 1401 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1402 return; 1403 } 1404 1405 if (msg->login.version != CTL_HA_VERSION) { 1406 printf("CTL HA peers have different versions %d != %d\n", 1407 msg->login.version, CTL_HA_VERSION); 1408 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1409 return; 1410 } 1411 if (msg->login.ha_mode != softc->ha_mode) { 1412 printf("CTL HA peers have different ha_mode %d != %d\n", 1413 msg->login.ha_mode, softc->ha_mode); 1414 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1415 return; 1416 } 1417 if (msg->login.ha_id == softc->ha_id) { 1418 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1419 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1420 return; 1421 } 1422 if (msg->login.max_luns != ctl_max_luns || 1423 msg->login.max_ports != ctl_max_ports || 1424 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1425 printf("CTL HA peers have different limits\n"); 1426 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1427 return; 1428 } 1429 } 1430 1431 static void 1432 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1433 { 1434 struct ctl_lun *lun; 1435 u_int i; 1436 uint32_t initidx, targ_lun; 1437 1438 if (len < offsetof(struct ctl_ha_msg_mode, data[0])) { 1439 printf("%s: Received truncated message %d < %zu\n", 1440 __func__, len, offsetof(struct ctl_ha_msg_mode, data[0])); 1441 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1442 return; 1443 } 1444 i = msg->mode.page_len; 1445 if (len < offsetof(struct ctl_ha_msg_mode, data[i])) { 1446 printf("%s: Received truncated message data %d < %zu\n", 1447 __func__, len, offsetof(struct ctl_ha_msg_mode, data[i])); 1448 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1449 return; 1450 } 1451 1452 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1453 mtx_lock(&softc->ctl_lock); 1454 if (targ_lun >= ctl_max_luns || 1455 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1456 mtx_unlock(&softc->ctl_lock); 1457 return; 1458 } 1459 mtx_lock(&lun->lun_lock); 1460 mtx_unlock(&softc->ctl_lock); 1461 if (lun->flags & CTL_LUN_DISABLED) { 1462 mtx_unlock(&lun->lun_lock); 1463 return; 1464 } 1465 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1466 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1467 msg->mode.page_code && 1468 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1469 break; 1470 } 1471 if (i == CTL_NUM_MODE_PAGES) { 1472 mtx_unlock(&lun->lun_lock); 1473 return; 1474 } 1475 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1476 min(lun->mode_pages.index[i].page_len, msg->mode.page_len)); 1477 initidx = ctl_get_initindex(&msg->hdr.nexus); 1478 if (initidx != -1) 1479 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1480 mtx_unlock(&lun->lun_lock); 1481 } 1482 1483 /* 1484 * ISC (Inter Shelf Communication) event handler. Events from the HA 1485 * subsystem come in here. 1486 */ 1487 static void 1488 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1489 { 1490 struct ctl_softc *softc = control_softc; 1491 union ctl_io *io; 1492 struct ctl_prio *presio; 1493 ctl_ha_status isc_status; 1494 1495 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1496 if (event == CTL_HA_EVT_MSG_RECV) { 1497 union ctl_ha_msg *msg, msgbuf; 1498 1499 if (param > sizeof(msgbuf)) 1500 msg = malloc(param, M_CTL, M_WAITOK); 1501 else 1502 msg = &msgbuf; 1503 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1504 M_WAITOK); 1505 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1506 printf("%s: Error receiving message: %d\n", 1507 __func__, isc_status); 1508 if (msg != &msgbuf) 1509 free(msg, M_CTL); 1510 return; 1511 } 1512 1513 CTL_DEBUG_PRINT(("CTL: msg_type %d len %d\n", 1514 msg->hdr.msg_type, param)); 1515 switch (msg->hdr.msg_type) { 1516 case CTL_MSG_SERIALIZE: 1517 io = ctl_alloc_io(softc->othersc_pool); 1518 ctl_zero_io(io); 1519 // populate ctsio from msg 1520 io->io_hdr.io_type = CTL_IO_SCSI; 1521 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1522 io->io_hdr.remote_io = msg->hdr.original_sc; 1523 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1524 CTL_FLAG_IO_ACTIVE; 1525 /* 1526 * If we're in serialization-only mode, we don't 1527 * want to go through full done processing. Thus 1528 * the COPY flag. 1529 * 1530 * XXX KDM add another flag that is more specific. 1531 */ 1532 if (softc->ha_mode != CTL_HA_MODE_XFER) 1533 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1534 io->io_hdr.nexus = msg->hdr.nexus; 1535 io->scsiio.priority = msg->scsi.priority; 1536 io->scsiio.tag_num = msg->scsi.tag_num; 1537 io->scsiio.tag_type = msg->scsi.tag_type; 1538 #ifdef CTL_TIME_IO 1539 io->io_hdr.start_time = time_uptime; 1540 getbinuptime(&io->io_hdr.start_bt); 1541 #endif /* CTL_TIME_IO */ 1542 io->scsiio.cdb_len = msg->scsi.cdb_len; 1543 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1544 CTL_MAX_CDBLEN); 1545 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1546 const struct ctl_cmd_entry *entry; 1547 1548 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1549 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1550 io->io_hdr.flags |= 1551 entry->flags & CTL_FLAG_DATA_MASK; 1552 } 1553 ctl_enqueue_isc(io); 1554 break; 1555 1556 /* Performed on the Originating SC, XFER mode only */ 1557 case CTL_MSG_DATAMOVE: { 1558 struct ctl_sg_entry *sgl; 1559 int i, j; 1560 1561 io = msg->hdr.original_sc; 1562 if (io == NULL) { 1563 printf("%s: original_sc == NULL!\n", __func__); 1564 /* XXX KDM do something here */ 1565 break; 1566 } 1567 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1568 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1569 /* 1570 * Keep track of this, we need to send it back over 1571 * when the datamove is complete. 1572 */ 1573 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1574 if (msg->hdr.status == CTL_SUCCESS) 1575 io->io_hdr.status = msg->hdr.status; 1576 1577 if (msg->dt.sg_sequence == 0) { 1578 #ifdef CTL_TIME_IO 1579 getbinuptime(&io->io_hdr.dma_start_bt); 1580 #endif 1581 i = msg->dt.kern_sg_entries + 1582 msg->dt.kern_data_len / 1583 CTL_HA_DATAMOVE_SEGMENT + 1; 1584 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1585 M_WAITOK | M_ZERO); 1586 CTL_RSGL(io) = sgl; 1587 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries]; 1588 1589 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1590 1591 io->scsiio.kern_sg_entries = 1592 msg->dt.kern_sg_entries; 1593 io->scsiio.rem_sg_entries = 1594 msg->dt.kern_sg_entries; 1595 io->scsiio.kern_data_len = 1596 msg->dt.kern_data_len; 1597 io->scsiio.kern_total_len = 1598 msg->dt.kern_total_len; 1599 io->scsiio.kern_data_resid = 1600 msg->dt.kern_data_resid; 1601 io->scsiio.kern_rel_offset = 1602 msg->dt.kern_rel_offset; 1603 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1604 io->io_hdr.flags |= msg->dt.flags & 1605 CTL_FLAG_BUS_ADDR; 1606 } else 1607 sgl = (struct ctl_sg_entry *) 1608 io->scsiio.kern_data_ptr; 1609 1610 for (i = msg->dt.sent_sg_entries, j = 0; 1611 i < (msg->dt.sent_sg_entries + 1612 msg->dt.cur_sg_entries); i++, j++) { 1613 sgl[i].addr = msg->dt.sg_list[j].addr; 1614 sgl[i].len = msg->dt.sg_list[j].len; 1615 } 1616 1617 /* 1618 * If this is the last piece of the I/O, we've got 1619 * the full S/G list. Queue processing in the thread. 1620 * Otherwise wait for the next piece. 1621 */ 1622 if (msg->dt.sg_last != 0) 1623 ctl_enqueue_isc(io); 1624 break; 1625 } 1626 /* Performed on the Serializing (primary) SC, XFER mode only */ 1627 case CTL_MSG_DATAMOVE_DONE: { 1628 if (msg->hdr.serializing_sc == NULL) { 1629 printf("%s: serializing_sc == NULL!\n", 1630 __func__); 1631 /* XXX KDM now what? */ 1632 break; 1633 } 1634 /* 1635 * We grab the sense information here in case 1636 * there was a failure, so we can return status 1637 * back to the initiator. 1638 */ 1639 io = msg->hdr.serializing_sc; 1640 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1641 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1642 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1643 io->io_hdr.port_status = msg->scsi.port_status; 1644 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1645 if (msg->hdr.status != CTL_STATUS_NONE) { 1646 io->io_hdr.status = msg->hdr.status; 1647 io->scsiio.scsi_status = msg->scsi.scsi_status; 1648 io->scsiio.sense_len = msg->scsi.sense_len; 1649 memcpy(&io->scsiio.sense_data, 1650 &msg->scsi.sense_data, 1651 msg->scsi.sense_len); 1652 if (msg->hdr.status == CTL_SUCCESS) 1653 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1654 } 1655 ctl_enqueue_isc(io); 1656 break; 1657 } 1658 1659 /* Preformed on Originating SC, SER_ONLY mode */ 1660 case CTL_MSG_R2R: 1661 io = msg->hdr.original_sc; 1662 if (io == NULL) { 1663 printf("%s: original_sc == NULL!\n", 1664 __func__); 1665 break; 1666 } 1667 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1668 io->io_hdr.msg_type = CTL_MSG_R2R; 1669 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1670 ctl_enqueue_isc(io); 1671 break; 1672 1673 /* 1674 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1675 * mode. 1676 * Performed on the Originating (i.e. secondary) SC in XFER 1677 * mode 1678 */ 1679 case CTL_MSG_FINISH_IO: 1680 if (softc->ha_mode == CTL_HA_MODE_XFER) 1681 ctl_isc_handler_finish_xfer(softc, msg); 1682 else 1683 ctl_isc_handler_finish_ser_only(softc, msg); 1684 break; 1685 1686 /* Preformed on Originating SC */ 1687 case CTL_MSG_BAD_JUJU: 1688 io = msg->hdr.original_sc; 1689 if (io == NULL) { 1690 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1691 __func__); 1692 break; 1693 } 1694 ctl_copy_sense_data(msg, io); 1695 /* 1696 * IO should have already been cleaned up on other 1697 * SC so clear this flag so we won't send a message 1698 * back to finish the IO there. 1699 */ 1700 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1701 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1702 1703 /* io = msg->hdr.serializing_sc; */ 1704 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1705 ctl_enqueue_isc(io); 1706 break; 1707 1708 /* Handle resets sent from the other side */ 1709 case CTL_MSG_MANAGE_TASKS: { 1710 struct ctl_taskio *taskio; 1711 taskio = (struct ctl_taskio *)ctl_alloc_io( 1712 softc->othersc_pool); 1713 ctl_zero_io((union ctl_io *)taskio); 1714 taskio->io_hdr.io_type = CTL_IO_TASK; 1715 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1716 taskio->io_hdr.nexus = msg->hdr.nexus; 1717 taskio->task_action = msg->task.task_action; 1718 taskio->tag_num = msg->task.tag_num; 1719 taskio->tag_type = msg->task.tag_type; 1720 #ifdef CTL_TIME_IO 1721 taskio->io_hdr.start_time = time_uptime; 1722 getbinuptime(&taskio->io_hdr.start_bt); 1723 #endif /* CTL_TIME_IO */ 1724 ctl_run_task((union ctl_io *)taskio); 1725 break; 1726 } 1727 /* Persistent Reserve action which needs attention */ 1728 case CTL_MSG_PERS_ACTION: 1729 presio = (struct ctl_prio *)ctl_alloc_io( 1730 softc->othersc_pool); 1731 ctl_zero_io((union ctl_io *)presio); 1732 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1733 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1734 presio->io_hdr.nexus = msg->hdr.nexus; 1735 presio->pr_msg = msg->pr; 1736 ctl_enqueue_isc((union ctl_io *)presio); 1737 break; 1738 case CTL_MSG_UA: 1739 ctl_isc_ua(softc, msg, param); 1740 break; 1741 case CTL_MSG_PORT_SYNC: 1742 ctl_isc_port_sync(softc, msg, param); 1743 break; 1744 case CTL_MSG_LUN_SYNC: 1745 ctl_isc_lun_sync(softc, msg, param); 1746 break; 1747 case CTL_MSG_IID_SYNC: 1748 ctl_isc_iid_sync(softc, msg, param); 1749 break; 1750 case CTL_MSG_LOGIN: 1751 ctl_isc_login(softc, msg, param); 1752 break; 1753 case CTL_MSG_MODE_SYNC: 1754 ctl_isc_mode_sync(softc, msg, param); 1755 break; 1756 default: 1757 printf("Received HA message of unknown type %d\n", 1758 msg->hdr.msg_type); 1759 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1760 break; 1761 } 1762 if (msg != &msgbuf) 1763 free(msg, M_CTL); 1764 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1765 printf("CTL: HA link status changed from %d to %d\n", 1766 softc->ha_link, param); 1767 if (param == softc->ha_link) 1768 return; 1769 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1770 softc->ha_link = param; 1771 ctl_isc_ha_link_down(softc); 1772 } else { 1773 softc->ha_link = param; 1774 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1775 ctl_isc_ha_link_up(softc); 1776 } 1777 return; 1778 } else { 1779 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1780 return; 1781 } 1782 } 1783 1784 static void 1785 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1786 { 1787 1788 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1789 src->scsi.sense_len); 1790 dest->scsiio.scsi_status = src->scsi.scsi_status; 1791 dest->scsiio.sense_len = src->scsi.sense_len; 1792 dest->io_hdr.status = src->hdr.status; 1793 } 1794 1795 static void 1796 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1797 { 1798 1799 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1800 src->scsiio.sense_len); 1801 dest->scsi.scsi_status = src->scsiio.scsi_status; 1802 dest->scsi.sense_len = src->scsiio.sense_len; 1803 dest->hdr.status = src->io_hdr.status; 1804 } 1805 1806 void 1807 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1808 { 1809 struct ctl_softc *softc = lun->ctl_softc; 1810 ctl_ua_type *pu; 1811 1812 if (initidx < softc->init_min || initidx >= softc->init_max) 1813 return; 1814 mtx_assert(&lun->lun_lock, MA_OWNED); 1815 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1816 if (pu == NULL) 1817 return; 1818 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1819 } 1820 1821 void 1822 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1823 { 1824 int i; 1825 1826 mtx_assert(&lun->lun_lock, MA_OWNED); 1827 if (lun->pending_ua[port] == NULL) 1828 return; 1829 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1830 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1831 continue; 1832 lun->pending_ua[port][i] |= ua; 1833 } 1834 } 1835 1836 void 1837 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1838 { 1839 struct ctl_softc *softc = lun->ctl_softc; 1840 int i; 1841 1842 mtx_assert(&lun->lun_lock, MA_OWNED); 1843 for (i = softc->port_min; i < softc->port_max; i++) 1844 ctl_est_ua_port(lun, i, except, ua); 1845 } 1846 1847 void 1848 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1849 { 1850 struct ctl_softc *softc = lun->ctl_softc; 1851 ctl_ua_type *pu; 1852 1853 if (initidx < softc->init_min || initidx >= softc->init_max) 1854 return; 1855 mtx_assert(&lun->lun_lock, MA_OWNED); 1856 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1857 if (pu == NULL) 1858 return; 1859 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1860 } 1861 1862 void 1863 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1864 { 1865 struct ctl_softc *softc = lun->ctl_softc; 1866 int i, j; 1867 1868 mtx_assert(&lun->lun_lock, MA_OWNED); 1869 for (i = softc->port_min; i < softc->port_max; i++) { 1870 if (lun->pending_ua[i] == NULL) 1871 continue; 1872 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1873 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1874 continue; 1875 lun->pending_ua[i][j] &= ~ua; 1876 } 1877 } 1878 } 1879 1880 void 1881 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1882 ctl_ua_type ua_type) 1883 { 1884 struct ctl_lun *lun; 1885 1886 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1887 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1888 mtx_lock(&lun->lun_lock); 1889 ctl_clr_ua(lun, initidx, ua_type); 1890 mtx_unlock(&lun->lun_lock); 1891 } 1892 } 1893 1894 static int 1895 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1896 { 1897 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1898 struct ctl_lun *lun; 1899 struct ctl_lun_req ireq; 1900 int error, value; 1901 1902 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1903 error = sysctl_handle_int(oidp, &value, 0, req); 1904 if ((error != 0) || (req->newptr == NULL)) 1905 return (error); 1906 1907 mtx_lock(&softc->ctl_lock); 1908 if (value == 0) 1909 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1910 else 1911 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1912 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1913 mtx_unlock(&softc->ctl_lock); 1914 bzero(&ireq, sizeof(ireq)); 1915 ireq.reqtype = CTL_LUNREQ_MODIFY; 1916 ireq.reqdata.modify.lun_id = lun->lun; 1917 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1918 curthread); 1919 if (ireq.status != CTL_LUN_OK) { 1920 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1921 __func__, ireq.status, ireq.error_str); 1922 } 1923 mtx_lock(&softc->ctl_lock); 1924 } 1925 mtx_unlock(&softc->ctl_lock); 1926 return (0); 1927 } 1928 1929 static int 1930 ctl_init(void) 1931 { 1932 struct make_dev_args args; 1933 struct ctl_softc *softc; 1934 int i, error; 1935 1936 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1937 M_WAITOK | M_ZERO); 1938 1939 make_dev_args_init(&args); 1940 args.mda_devsw = &ctl_cdevsw; 1941 args.mda_uid = UID_ROOT; 1942 args.mda_gid = GID_OPERATOR; 1943 args.mda_mode = 0600; 1944 args.mda_si_drv1 = softc; 1945 args.mda_si_drv2 = NULL; 1946 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1947 if (error != 0) { 1948 free(softc, M_DEVBUF); 1949 control_softc = NULL; 1950 return (error); 1951 } 1952 1953 sysctl_ctx_init(&softc->sysctl_ctx); 1954 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1955 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1956 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer"); 1957 1958 if (softc->sysctl_tree == NULL) { 1959 printf("%s: unable to allocate sysctl tree\n", __func__); 1960 destroy_dev(softc->dev); 1961 free(softc, M_DEVBUF); 1962 control_softc = NULL; 1963 return (ENOMEM); 1964 } 1965 1966 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1967 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1968 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1969 softc->flags = 0; 1970 1971 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1972 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1973 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1974 1975 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) { 1976 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n", 1977 ctl_max_luns, CTL_DEFAULT_MAX_LUNS); 1978 ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 1979 } 1980 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns, 1981 M_DEVBUF, M_WAITOK | M_ZERO); 1982 softc->ctl_lun_mask = malloc(sizeof(uint32_t) * 1983 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1984 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) { 1985 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n", 1986 ctl_max_ports, CTL_DEFAULT_MAX_PORTS); 1987 ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 1988 } 1989 softc->ctl_port_mask = malloc(sizeof(uint32_t) * 1990 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1991 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports, 1992 M_DEVBUF, M_WAITOK | M_ZERO); 1993 1994 /* 1995 * In Copan's HA scheme, the "master" and "slave" roles are 1996 * figured out through the slot the controller is in. Although it 1997 * is an active/active system, someone has to be in charge. 1998 */ 1999 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 2000 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 2001 "HA head ID (0 - no HA)"); 2002 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 2003 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 2004 softc->is_single = 1; 2005 softc->port_cnt = ctl_max_ports; 2006 softc->port_min = 0; 2007 } else { 2008 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 2009 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 2010 } 2011 softc->port_max = softc->port_min + softc->port_cnt; 2012 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 2013 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 2014 2015 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 2016 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 2017 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 2018 2019 STAILQ_INIT(&softc->lun_list); 2020 STAILQ_INIT(&softc->fe_list); 2021 STAILQ_INIT(&softc->port_list); 2022 STAILQ_INIT(&softc->be_list); 2023 ctl_tpc_init(softc); 2024 2025 if (worker_threads <= 0) 2026 worker_threads = max(1, mp_ncpus / 4); 2027 if (worker_threads > CTL_MAX_THREADS) 2028 worker_threads = CTL_MAX_THREADS; 2029 2030 for (i = 0; i < worker_threads; i++) { 2031 struct ctl_thread *thr = &softc->threads[i]; 2032 2033 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 2034 thr->ctl_softc = softc; 2035 STAILQ_INIT(&thr->incoming_queue); 2036 STAILQ_INIT(&thr->rtr_queue); 2037 STAILQ_INIT(&thr->done_queue); 2038 STAILQ_INIT(&thr->isc_queue); 2039 2040 error = kproc_kthread_add(ctl_work_thread, thr, 2041 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 2042 if (error != 0) { 2043 printf("error creating CTL work thread!\n"); 2044 return (error); 2045 } 2046 } 2047 error = kproc_kthread_add(ctl_thresh_thread, softc, 2048 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 2049 if (error != 0) { 2050 printf("error creating CTL threshold thread!\n"); 2051 return (error); 2052 } 2053 2054 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 2055 OID_AUTO, "ha_role", 2056 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 2057 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 2058 2059 if (softc->is_single == 0) { 2060 if (ctl_frontend_register(&ha_frontend) != 0) 2061 softc->is_single = 1; 2062 } 2063 return (0); 2064 } 2065 2066 static int 2067 ctl_shutdown(void) 2068 { 2069 struct ctl_softc *softc = control_softc; 2070 int i; 2071 2072 if (softc->is_single == 0) 2073 ctl_frontend_deregister(&ha_frontend); 2074 2075 destroy_dev(softc->dev); 2076 2077 /* Shutdown CTL threads. */ 2078 softc->shutdown = 1; 2079 for (i = 0; i < worker_threads; i++) { 2080 struct ctl_thread *thr = &softc->threads[i]; 2081 while (thr->thread != NULL) { 2082 wakeup(thr); 2083 if (thr->thread != NULL) 2084 pause("CTL thr shutdown", 1); 2085 } 2086 mtx_destroy(&thr->queue_lock); 2087 } 2088 while (softc->thresh_thread != NULL) { 2089 wakeup(softc->thresh_thread); 2090 if (softc->thresh_thread != NULL) 2091 pause("CTL thr shutdown", 1); 2092 } 2093 2094 ctl_tpc_shutdown(softc); 2095 uma_zdestroy(softc->io_zone); 2096 mtx_destroy(&softc->ctl_lock); 2097 2098 free(softc->ctl_luns, M_DEVBUF); 2099 free(softc->ctl_lun_mask, M_DEVBUF); 2100 free(softc->ctl_port_mask, M_DEVBUF); 2101 free(softc->ctl_ports, M_DEVBUF); 2102 2103 sysctl_ctx_free(&softc->sysctl_ctx); 2104 2105 free(softc, M_DEVBUF); 2106 control_softc = NULL; 2107 return (0); 2108 } 2109 2110 static int 2111 ctl_module_event_handler(module_t mod, int what, void *arg) 2112 { 2113 2114 switch (what) { 2115 case MOD_LOAD: 2116 return (ctl_init()); 2117 case MOD_UNLOAD: 2118 return (ctl_shutdown()); 2119 default: 2120 return (EOPNOTSUPP); 2121 } 2122 } 2123 2124 /* 2125 * XXX KDM should we do some access checks here? Bump a reference count to 2126 * prevent a CTL module from being unloaded while someone has it open? 2127 */ 2128 static int 2129 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2130 { 2131 return (0); 2132 } 2133 2134 static int 2135 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2136 { 2137 return (0); 2138 } 2139 2140 /* 2141 * Remove an initiator by port number and initiator ID. 2142 * Returns 0 for success, -1 for failure. 2143 */ 2144 int 2145 ctl_remove_initiator(struct ctl_port *port, int iid) 2146 { 2147 struct ctl_softc *softc = port->ctl_softc; 2148 int last; 2149 2150 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2151 2152 if (iid > CTL_MAX_INIT_PER_PORT) { 2153 printf("%s: initiator ID %u > maximun %u!\n", 2154 __func__, iid, CTL_MAX_INIT_PER_PORT); 2155 return (-1); 2156 } 2157 2158 mtx_lock(&softc->ctl_lock); 2159 last = (--port->wwpn_iid[iid].in_use == 0); 2160 port->wwpn_iid[iid].last_use = time_uptime; 2161 mtx_unlock(&softc->ctl_lock); 2162 if (last) 2163 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2164 ctl_isc_announce_iid(port, iid); 2165 2166 return (0); 2167 } 2168 2169 /* 2170 * Add an initiator to the initiator map. 2171 * Returns iid for success, < 0 for failure. 2172 */ 2173 int 2174 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2175 { 2176 struct ctl_softc *softc = port->ctl_softc; 2177 time_t best_time; 2178 int i, best; 2179 2180 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2181 2182 if (iid >= CTL_MAX_INIT_PER_PORT) { 2183 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2184 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2185 free(name, M_CTL); 2186 return (-1); 2187 } 2188 2189 mtx_lock(&softc->ctl_lock); 2190 2191 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2192 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2193 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2194 iid = i; 2195 break; 2196 } 2197 if (name != NULL && port->wwpn_iid[i].name != NULL && 2198 strcmp(name, port->wwpn_iid[i].name) == 0) { 2199 iid = i; 2200 break; 2201 } 2202 } 2203 } 2204 2205 if (iid < 0) { 2206 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2207 if (port->wwpn_iid[i].in_use == 0 && 2208 port->wwpn_iid[i].wwpn == 0 && 2209 port->wwpn_iid[i].name == NULL) { 2210 iid = i; 2211 break; 2212 } 2213 } 2214 } 2215 2216 if (iid < 0) { 2217 best = -1; 2218 best_time = INT32_MAX; 2219 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2220 if (port->wwpn_iid[i].in_use == 0) { 2221 if (port->wwpn_iid[i].last_use < best_time) { 2222 best = i; 2223 best_time = port->wwpn_iid[i].last_use; 2224 } 2225 } 2226 } 2227 iid = best; 2228 } 2229 2230 if (iid < 0) { 2231 mtx_unlock(&softc->ctl_lock); 2232 free(name, M_CTL); 2233 return (-2); 2234 } 2235 2236 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2237 /* 2238 * This is not an error yet. 2239 */ 2240 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2241 #if 0 2242 printf("%s: port %d iid %u WWPN %#jx arrived" 2243 " again\n", __func__, port->targ_port, 2244 iid, (uintmax_t)wwpn); 2245 #endif 2246 goto take; 2247 } 2248 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2249 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2250 #if 0 2251 printf("%s: port %d iid %u name '%s' arrived" 2252 " again\n", __func__, port->targ_port, 2253 iid, name); 2254 #endif 2255 goto take; 2256 } 2257 2258 /* 2259 * This is an error, but what do we do about it? The 2260 * driver is telling us we have a new WWPN for this 2261 * initiator ID, so we pretty much need to use it. 2262 */ 2263 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2264 " but WWPN %#jx '%s' is still at that address\n", 2265 __func__, port->targ_port, iid, wwpn, name, 2266 (uintmax_t)port->wwpn_iid[iid].wwpn, 2267 port->wwpn_iid[iid].name); 2268 } 2269 take: 2270 free(port->wwpn_iid[iid].name, M_CTL); 2271 port->wwpn_iid[iid].name = name; 2272 port->wwpn_iid[iid].wwpn = wwpn; 2273 port->wwpn_iid[iid].in_use++; 2274 mtx_unlock(&softc->ctl_lock); 2275 ctl_isc_announce_iid(port, iid); 2276 2277 return (iid); 2278 } 2279 2280 static int 2281 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2282 { 2283 int len; 2284 2285 switch (port->port_type) { 2286 case CTL_PORT_FC: 2287 { 2288 struct scsi_transportid_fcp *id = 2289 (struct scsi_transportid_fcp *)buf; 2290 if (port->wwpn_iid[iid].wwpn == 0) 2291 return (0); 2292 memset(id, 0, sizeof(*id)); 2293 id->format_protocol = SCSI_PROTO_FC; 2294 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2295 return (sizeof(*id)); 2296 } 2297 case CTL_PORT_ISCSI: 2298 { 2299 struct scsi_transportid_iscsi_port *id = 2300 (struct scsi_transportid_iscsi_port *)buf; 2301 if (port->wwpn_iid[iid].name == NULL) 2302 return (0); 2303 memset(id, 0, 256); 2304 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2305 SCSI_PROTO_ISCSI; 2306 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2307 len = roundup2(min(len, 252), 4); 2308 scsi_ulto2b(len, id->additional_length); 2309 return (sizeof(*id) + len); 2310 } 2311 case CTL_PORT_SAS: 2312 { 2313 struct scsi_transportid_sas *id = 2314 (struct scsi_transportid_sas *)buf; 2315 if (port->wwpn_iid[iid].wwpn == 0) 2316 return (0); 2317 memset(id, 0, sizeof(*id)); 2318 id->format_protocol = SCSI_PROTO_SAS; 2319 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2320 return (sizeof(*id)); 2321 } 2322 default: 2323 { 2324 struct scsi_transportid_spi *id = 2325 (struct scsi_transportid_spi *)buf; 2326 memset(id, 0, sizeof(*id)); 2327 id->format_protocol = SCSI_PROTO_SPI; 2328 scsi_ulto2b(iid, id->scsi_addr); 2329 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2330 return (sizeof(*id)); 2331 } 2332 } 2333 } 2334 2335 /* 2336 * Serialize a command that went down the "wrong" side, and so was sent to 2337 * this controller for execution. The logic is a little different than the 2338 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2339 * sent back to the other side, but in the success case, we execute the 2340 * command on this side (XFER mode) or tell the other side to execute it 2341 * (SER_ONLY mode). 2342 */ 2343 static void 2344 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2345 { 2346 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2347 struct ctl_port *port = CTL_PORT(ctsio); 2348 union ctl_ha_msg msg_info; 2349 struct ctl_lun *lun; 2350 const struct ctl_cmd_entry *entry; 2351 union ctl_io *bio; 2352 uint32_t targ_lun; 2353 2354 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2355 2356 /* Make sure that we know about this port. */ 2357 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2358 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2359 /*retry_count*/ 1); 2360 goto badjuju; 2361 } 2362 2363 /* Make sure that we know about this LUN. */ 2364 mtx_lock(&softc->ctl_lock); 2365 if (targ_lun >= ctl_max_luns || 2366 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2367 mtx_unlock(&softc->ctl_lock); 2368 2369 /* 2370 * The other node would not send this request to us unless 2371 * received announce that we are primary node for this LUN. 2372 * If this LUN does not exist now, it is probably result of 2373 * a race, so respond to initiator in the most opaque way. 2374 */ 2375 ctl_set_busy(ctsio); 2376 goto badjuju; 2377 } 2378 mtx_lock(&lun->lun_lock); 2379 mtx_unlock(&softc->ctl_lock); 2380 2381 /* 2382 * If the LUN is invalid, pretend that it doesn't exist. 2383 * It will go away as soon as all pending I/Os completed. 2384 */ 2385 if (lun->flags & CTL_LUN_DISABLED) { 2386 mtx_unlock(&lun->lun_lock); 2387 ctl_set_busy(ctsio); 2388 goto badjuju; 2389 } 2390 2391 entry = ctl_get_cmd_entry(ctsio, NULL); 2392 ctsio->seridx = entry->seridx; 2393 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2394 mtx_unlock(&lun->lun_lock); 2395 goto badjuju; 2396 } 2397 2398 CTL_LUN(ctsio) = lun; 2399 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2400 2401 /* 2402 * Every I/O goes into the OOA queue for a 2403 * particular LUN, and stays there until completion. 2404 */ 2405 #ifdef CTL_TIME_IO 2406 if (LIST_EMPTY(&lun->ooa_queue)) 2407 lun->idle_time += getsbinuptime() - lun->last_busy; 2408 #endif 2409 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2410 2411 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 2412 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 2413 case CTL_ACTION_PASS: 2414 case CTL_ACTION_SKIP: 2415 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2416 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2417 ctl_enqueue_rtr((union ctl_io *)ctsio); 2418 mtx_unlock(&lun->lun_lock); 2419 } else { 2420 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2421 mtx_unlock(&lun->lun_lock); 2422 2423 /* send msg back to other side */ 2424 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2425 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2426 msg_info.hdr.msg_type = CTL_MSG_R2R; 2427 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2428 sizeof(msg_info.hdr), M_WAITOK); 2429 } 2430 break; 2431 case CTL_ACTION_BLOCK: 2432 ctsio->io_hdr.blocker = bio; 2433 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 2434 blocked_links); 2435 mtx_unlock(&lun->lun_lock); 2436 break; 2437 case CTL_ACTION_OVERLAP: 2438 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2439 mtx_unlock(&lun->lun_lock); 2440 ctl_set_overlapped_cmd(ctsio); 2441 goto badjuju; 2442 case CTL_ACTION_OVERLAP_TAG: 2443 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2444 mtx_unlock(&lun->lun_lock); 2445 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2446 badjuju: 2447 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2448 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2449 msg_info.hdr.serializing_sc = NULL; 2450 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2451 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2452 sizeof(msg_info.scsi), M_WAITOK); 2453 ctl_free_io((union ctl_io *)ctsio); 2454 break; 2455 default: 2456 __assert_unreachable(); 2457 } 2458 } 2459 2460 /* 2461 * Returns 0 for success, errno for failure. 2462 */ 2463 static void 2464 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2465 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2466 { 2467 struct ctl_io_hdr *ioh; 2468 2469 mtx_lock(&lun->lun_lock); 2470 ioh = LIST_FIRST(&lun->ooa_queue); 2471 if (ioh == NULL) { 2472 mtx_unlock(&lun->lun_lock); 2473 return; 2474 } 2475 while (LIST_NEXT(ioh, ooa_links) != NULL) 2476 ioh = LIST_NEXT(ioh, ooa_links); 2477 for ( ; ioh; ioh = LIST_PREV(ioh, &lun->ooa_queue, ctl_io_hdr, ooa_links)) { 2478 union ctl_io *io = (union ctl_io *)ioh; 2479 struct ctl_ooa_entry *entry; 2480 2481 /* 2482 * If we've got more than we can fit, just count the 2483 * remaining entries. 2484 */ 2485 if (*cur_fill_num >= ooa_hdr->alloc_num) { 2486 (*cur_fill_num)++; 2487 continue; 2488 } 2489 2490 entry = &kern_entries[*cur_fill_num]; 2491 2492 entry->tag_num = io->scsiio.tag_num; 2493 entry->lun_num = lun->lun; 2494 #ifdef CTL_TIME_IO 2495 entry->start_bt = io->io_hdr.start_bt; 2496 #endif 2497 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2498 entry->cdb_len = io->scsiio.cdb_len; 2499 if (io->io_hdr.blocker != NULL) 2500 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2501 2502 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2503 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2504 2505 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2506 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2507 2508 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2509 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2510 2511 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2512 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2513 2514 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) 2515 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED; 2516 2517 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) 2518 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT; 2519 (*cur_fill_num)++; 2520 } 2521 mtx_unlock(&lun->lun_lock); 2522 } 2523 2524 /* 2525 * Escape characters that are illegal or not recommended in XML. 2526 */ 2527 int 2528 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2529 { 2530 char *end = str + size; 2531 int retval; 2532 2533 retval = 0; 2534 2535 for (; *str && str < end; str++) { 2536 switch (*str) { 2537 case '&': 2538 retval = sbuf_printf(sb, "&"); 2539 break; 2540 case '>': 2541 retval = sbuf_printf(sb, ">"); 2542 break; 2543 case '<': 2544 retval = sbuf_printf(sb, "<"); 2545 break; 2546 default: 2547 retval = sbuf_putc(sb, *str); 2548 break; 2549 } 2550 2551 if (retval != 0) 2552 break; 2553 } 2554 2555 return (retval); 2556 } 2557 2558 static void 2559 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2560 { 2561 struct scsi_vpd_id_descriptor *desc; 2562 int i; 2563 2564 if (id == NULL || id->len < 4) 2565 return; 2566 desc = (struct scsi_vpd_id_descriptor *)id->data; 2567 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2568 case SVPD_ID_TYPE_T10: 2569 sbuf_printf(sb, "t10."); 2570 break; 2571 case SVPD_ID_TYPE_EUI64: 2572 sbuf_printf(sb, "eui."); 2573 break; 2574 case SVPD_ID_TYPE_NAA: 2575 sbuf_printf(sb, "naa."); 2576 break; 2577 case SVPD_ID_TYPE_SCSI_NAME: 2578 break; 2579 } 2580 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2581 case SVPD_ID_CODESET_BINARY: 2582 for (i = 0; i < desc->length; i++) 2583 sbuf_printf(sb, "%02x", desc->identifier[i]); 2584 break; 2585 case SVPD_ID_CODESET_ASCII: 2586 sbuf_printf(sb, "%.*s", (int)desc->length, 2587 (char *)desc->identifier); 2588 break; 2589 case SVPD_ID_CODESET_UTF8: 2590 sbuf_printf(sb, "%s", (char *)desc->identifier); 2591 break; 2592 } 2593 } 2594 2595 static int 2596 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2597 struct thread *td) 2598 { 2599 struct ctl_softc *softc = dev->si_drv1; 2600 struct ctl_port *port; 2601 struct ctl_lun *lun; 2602 int retval; 2603 2604 retval = 0; 2605 2606 switch (cmd) { 2607 case CTL_IO: 2608 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2609 break; 2610 case CTL_ENABLE_PORT: 2611 case CTL_DISABLE_PORT: 2612 case CTL_SET_PORT_WWNS: { 2613 struct ctl_port *port; 2614 struct ctl_port_entry *entry; 2615 2616 entry = (struct ctl_port_entry *)addr; 2617 2618 mtx_lock(&softc->ctl_lock); 2619 STAILQ_FOREACH(port, &softc->port_list, links) { 2620 int action, done; 2621 2622 if (port->targ_port < softc->port_min || 2623 port->targ_port >= softc->port_max) 2624 continue; 2625 2626 action = 0; 2627 done = 0; 2628 if ((entry->port_type == CTL_PORT_NONE) 2629 && (entry->targ_port == port->targ_port)) { 2630 /* 2631 * If the user only wants to enable or 2632 * disable or set WWNs on a specific port, 2633 * do the operation and we're done. 2634 */ 2635 action = 1; 2636 done = 1; 2637 } else if (entry->port_type & port->port_type) { 2638 /* 2639 * Compare the user's type mask with the 2640 * particular frontend type to see if we 2641 * have a match. 2642 */ 2643 action = 1; 2644 done = 0; 2645 2646 /* 2647 * Make sure the user isn't trying to set 2648 * WWNs on multiple ports at the same time. 2649 */ 2650 if (cmd == CTL_SET_PORT_WWNS) { 2651 printf("%s: Can't set WWNs on " 2652 "multiple ports\n", __func__); 2653 retval = EINVAL; 2654 break; 2655 } 2656 } 2657 if (action == 0) 2658 continue; 2659 2660 /* 2661 * XXX KDM we have to drop the lock here, because 2662 * the online/offline operations can potentially 2663 * block. We need to reference count the frontends 2664 * so they can't go away, 2665 */ 2666 if (cmd == CTL_ENABLE_PORT) { 2667 mtx_unlock(&softc->ctl_lock); 2668 ctl_port_online(port); 2669 mtx_lock(&softc->ctl_lock); 2670 } else if (cmd == CTL_DISABLE_PORT) { 2671 mtx_unlock(&softc->ctl_lock); 2672 ctl_port_offline(port); 2673 mtx_lock(&softc->ctl_lock); 2674 } else if (cmd == CTL_SET_PORT_WWNS) { 2675 ctl_port_set_wwns(port, 2676 (entry->flags & CTL_PORT_WWNN_VALID) ? 2677 1 : 0, entry->wwnn, 2678 (entry->flags & CTL_PORT_WWPN_VALID) ? 2679 1 : 0, entry->wwpn); 2680 } 2681 if (done != 0) 2682 break; 2683 } 2684 mtx_unlock(&softc->ctl_lock); 2685 break; 2686 } 2687 case CTL_GET_OOA: { 2688 struct ctl_ooa *ooa_hdr; 2689 struct ctl_ooa_entry *entries; 2690 uint32_t cur_fill_num; 2691 2692 ooa_hdr = (struct ctl_ooa *)addr; 2693 2694 if ((ooa_hdr->alloc_len == 0) 2695 || (ooa_hdr->alloc_num == 0)) { 2696 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2697 "must be non-zero\n", __func__, 2698 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2699 retval = EINVAL; 2700 break; 2701 } 2702 2703 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2704 sizeof(struct ctl_ooa_entry))) { 2705 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2706 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2707 __func__, ooa_hdr->alloc_len, 2708 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2709 retval = EINVAL; 2710 break; 2711 } 2712 2713 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2714 if (entries == NULL) { 2715 printf("%s: could not allocate %d bytes for OOA " 2716 "dump\n", __func__, ooa_hdr->alloc_len); 2717 retval = ENOMEM; 2718 break; 2719 } 2720 2721 mtx_lock(&softc->ctl_lock); 2722 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2723 (ooa_hdr->lun_num >= ctl_max_luns || 2724 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2725 mtx_unlock(&softc->ctl_lock); 2726 free(entries, M_CTL); 2727 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2728 __func__, (uintmax_t)ooa_hdr->lun_num); 2729 retval = EINVAL; 2730 break; 2731 } 2732 2733 cur_fill_num = 0; 2734 2735 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2736 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2737 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2738 ooa_hdr, entries); 2739 } 2740 } else { 2741 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2742 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2743 entries); 2744 } 2745 mtx_unlock(&softc->ctl_lock); 2746 2747 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2748 ooa_hdr->fill_len = ooa_hdr->fill_num * 2749 sizeof(struct ctl_ooa_entry); 2750 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2751 if (retval != 0) { 2752 printf("%s: error copying out %d bytes for OOA dump\n", 2753 __func__, ooa_hdr->fill_len); 2754 } 2755 2756 getbinuptime(&ooa_hdr->cur_bt); 2757 2758 if (cur_fill_num > ooa_hdr->alloc_num) { 2759 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2760 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2761 } else { 2762 ooa_hdr->dropped_num = 0; 2763 ooa_hdr->status = CTL_OOA_OK; 2764 } 2765 2766 free(entries, M_CTL); 2767 break; 2768 } 2769 case CTL_DELAY_IO: { 2770 struct ctl_io_delay_info *delay_info; 2771 2772 delay_info = (struct ctl_io_delay_info *)addr; 2773 2774 #ifdef CTL_IO_DELAY 2775 mtx_lock(&softc->ctl_lock); 2776 if (delay_info->lun_id >= ctl_max_luns || 2777 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2778 mtx_unlock(&softc->ctl_lock); 2779 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2780 break; 2781 } 2782 mtx_lock(&lun->lun_lock); 2783 mtx_unlock(&softc->ctl_lock); 2784 delay_info->status = CTL_DELAY_STATUS_OK; 2785 switch (delay_info->delay_type) { 2786 case CTL_DELAY_TYPE_CONT: 2787 case CTL_DELAY_TYPE_ONESHOT: 2788 break; 2789 default: 2790 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2791 break; 2792 } 2793 switch (delay_info->delay_loc) { 2794 case CTL_DELAY_LOC_DATAMOVE: 2795 lun->delay_info.datamove_type = delay_info->delay_type; 2796 lun->delay_info.datamove_delay = delay_info->delay_secs; 2797 break; 2798 case CTL_DELAY_LOC_DONE: 2799 lun->delay_info.done_type = delay_info->delay_type; 2800 lun->delay_info.done_delay = delay_info->delay_secs; 2801 break; 2802 default: 2803 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2804 break; 2805 } 2806 mtx_unlock(&lun->lun_lock); 2807 #else 2808 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2809 #endif /* CTL_IO_DELAY */ 2810 break; 2811 } 2812 case CTL_ERROR_INJECT: { 2813 struct ctl_error_desc *err_desc, *new_err_desc; 2814 2815 err_desc = (struct ctl_error_desc *)addr; 2816 2817 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2818 M_WAITOK | M_ZERO); 2819 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2820 2821 mtx_lock(&softc->ctl_lock); 2822 if (err_desc->lun_id >= ctl_max_luns || 2823 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2824 mtx_unlock(&softc->ctl_lock); 2825 free(new_err_desc, M_CTL); 2826 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2827 __func__, (uintmax_t)err_desc->lun_id); 2828 retval = EINVAL; 2829 break; 2830 } 2831 mtx_lock(&lun->lun_lock); 2832 mtx_unlock(&softc->ctl_lock); 2833 2834 /* 2835 * We could do some checking here to verify the validity 2836 * of the request, but given the complexity of error 2837 * injection requests, the checking logic would be fairly 2838 * complex. 2839 * 2840 * For now, if the request is invalid, it just won't get 2841 * executed and might get deleted. 2842 */ 2843 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2844 2845 /* 2846 * XXX KDM check to make sure the serial number is unique, 2847 * in case we somehow manage to wrap. That shouldn't 2848 * happen for a very long time, but it's the right thing to 2849 * do. 2850 */ 2851 new_err_desc->serial = lun->error_serial; 2852 err_desc->serial = lun->error_serial; 2853 lun->error_serial++; 2854 2855 mtx_unlock(&lun->lun_lock); 2856 break; 2857 } 2858 case CTL_ERROR_INJECT_DELETE: { 2859 struct ctl_error_desc *delete_desc, *desc, *desc2; 2860 int delete_done; 2861 2862 delete_desc = (struct ctl_error_desc *)addr; 2863 delete_done = 0; 2864 2865 mtx_lock(&softc->ctl_lock); 2866 if (delete_desc->lun_id >= ctl_max_luns || 2867 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2868 mtx_unlock(&softc->ctl_lock); 2869 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2870 __func__, (uintmax_t)delete_desc->lun_id); 2871 retval = EINVAL; 2872 break; 2873 } 2874 mtx_lock(&lun->lun_lock); 2875 mtx_unlock(&softc->ctl_lock); 2876 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2877 if (desc->serial != delete_desc->serial) 2878 continue; 2879 2880 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2881 links); 2882 free(desc, M_CTL); 2883 delete_done = 1; 2884 } 2885 mtx_unlock(&lun->lun_lock); 2886 if (delete_done == 0) { 2887 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2888 "error serial %ju on LUN %u\n", __func__, 2889 delete_desc->serial, delete_desc->lun_id); 2890 retval = EINVAL; 2891 break; 2892 } 2893 break; 2894 } 2895 case CTL_DUMP_STRUCTS: { 2896 int j, k; 2897 struct ctl_port *port; 2898 struct ctl_frontend *fe; 2899 2900 mtx_lock(&softc->ctl_lock); 2901 printf("CTL Persistent Reservation information start:\n"); 2902 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2903 mtx_lock(&lun->lun_lock); 2904 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2905 mtx_unlock(&lun->lun_lock); 2906 continue; 2907 } 2908 2909 for (j = 0; j < ctl_max_ports; j++) { 2910 if (lun->pr_keys[j] == NULL) 2911 continue; 2912 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2913 if (lun->pr_keys[j][k] == 0) 2914 continue; 2915 printf(" LUN %ju port %d iid %d key " 2916 "%#jx\n", lun->lun, j, k, 2917 (uintmax_t)lun->pr_keys[j][k]); 2918 } 2919 } 2920 mtx_unlock(&lun->lun_lock); 2921 } 2922 printf("CTL Persistent Reservation information end\n"); 2923 printf("CTL Ports:\n"); 2924 STAILQ_FOREACH(port, &softc->port_list, links) { 2925 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2926 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2927 port->frontend->name, port->port_type, 2928 port->physical_port, port->virtual_port, 2929 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2930 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2931 if (port->wwpn_iid[j].in_use == 0 && 2932 port->wwpn_iid[j].wwpn == 0 && 2933 port->wwpn_iid[j].name == NULL) 2934 continue; 2935 2936 printf(" iid %u use %d WWPN %#jx '%s'\n", 2937 j, port->wwpn_iid[j].in_use, 2938 (uintmax_t)port->wwpn_iid[j].wwpn, 2939 port->wwpn_iid[j].name); 2940 } 2941 } 2942 printf("CTL Port information end\n"); 2943 mtx_unlock(&softc->ctl_lock); 2944 /* 2945 * XXX KDM calling this without a lock. We'd likely want 2946 * to drop the lock before calling the frontend's dump 2947 * routine anyway. 2948 */ 2949 printf("CTL Frontends:\n"); 2950 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2951 printf(" Frontend '%s'\n", fe->name); 2952 if (fe->fe_dump != NULL) 2953 fe->fe_dump(); 2954 } 2955 printf("CTL Frontend information end\n"); 2956 break; 2957 } 2958 case CTL_LUN_REQ: { 2959 struct ctl_lun_req *lun_req; 2960 struct ctl_backend_driver *backend; 2961 void *packed; 2962 nvlist_t *tmp_args_nvl; 2963 size_t packed_len; 2964 2965 lun_req = (struct ctl_lun_req *)addr; 2966 tmp_args_nvl = lun_req->args_nvl; 2967 2968 backend = ctl_backend_find(lun_req->backend); 2969 if (backend == NULL) { 2970 lun_req->status = CTL_LUN_ERROR; 2971 snprintf(lun_req->error_str, 2972 sizeof(lun_req->error_str), 2973 "Backend \"%s\" not found.", 2974 lun_req->backend); 2975 break; 2976 } 2977 2978 if (lun_req->args != NULL) { 2979 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2980 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2981 free(packed, M_CTL); 2982 lun_req->status = CTL_LUN_ERROR; 2983 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2984 "Cannot copyin args."); 2985 break; 2986 } 2987 lun_req->args_nvl = nvlist_unpack(packed, 2988 lun_req->args_len, 0); 2989 free(packed, M_CTL); 2990 2991 if (lun_req->args_nvl == NULL) { 2992 lun_req->status = CTL_LUN_ERROR; 2993 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2994 "Cannot unpack args nvlist."); 2995 break; 2996 } 2997 } else 2998 lun_req->args_nvl = nvlist_create(0); 2999 3000 retval = backend->ioctl(dev, cmd, addr, flag, td); 3001 nvlist_destroy(lun_req->args_nvl); 3002 lun_req->args_nvl = tmp_args_nvl; 3003 3004 if (lun_req->result_nvl != NULL) { 3005 if (lun_req->result != NULL) { 3006 packed = nvlist_pack(lun_req->result_nvl, 3007 &packed_len); 3008 if (packed == NULL) { 3009 lun_req->status = CTL_LUN_ERROR; 3010 snprintf(lun_req->error_str, 3011 sizeof(lun_req->error_str), 3012 "Cannot pack result nvlist."); 3013 break; 3014 } 3015 3016 if (packed_len > lun_req->result_len) { 3017 lun_req->status = CTL_LUN_ERROR; 3018 snprintf(lun_req->error_str, 3019 sizeof(lun_req->error_str), 3020 "Result nvlist too large."); 3021 free(packed, M_NVLIST); 3022 break; 3023 } 3024 3025 if (copyout(packed, lun_req->result, packed_len)) { 3026 lun_req->status = CTL_LUN_ERROR; 3027 snprintf(lun_req->error_str, 3028 sizeof(lun_req->error_str), 3029 "Cannot copyout() the result."); 3030 free(packed, M_NVLIST); 3031 break; 3032 } 3033 3034 lun_req->result_len = packed_len; 3035 free(packed, M_NVLIST); 3036 } 3037 3038 nvlist_destroy(lun_req->result_nvl); 3039 } 3040 break; 3041 } 3042 case CTL_LUN_LIST: { 3043 struct sbuf *sb; 3044 struct ctl_lun_list *list; 3045 const char *name, *value; 3046 void *cookie; 3047 int type; 3048 3049 list = (struct ctl_lun_list *)addr; 3050 3051 /* 3052 * Allocate a fixed length sbuf here, based on the length 3053 * of the user's buffer. We could allocate an auto-extending 3054 * buffer, and then tell the user how much larger our 3055 * amount of data is than his buffer, but that presents 3056 * some problems: 3057 * 3058 * 1. The sbuf(9) routines use a blocking malloc, and so 3059 * we can't hold a lock while calling them with an 3060 * auto-extending buffer. 3061 * 3062 * 2. There is not currently a LUN reference counting 3063 * mechanism, outside of outstanding transactions on 3064 * the LUN's OOA queue. So a LUN could go away on us 3065 * while we're getting the LUN number, backend-specific 3066 * information, etc. Thus, given the way things 3067 * currently work, we need to hold the CTL lock while 3068 * grabbing LUN information. 3069 * 3070 * So, from the user's standpoint, the best thing to do is 3071 * allocate what he thinks is a reasonable buffer length, 3072 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3073 * double the buffer length and try again. (And repeat 3074 * that until he succeeds.) 3075 */ 3076 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3077 if (sb == NULL) { 3078 list->status = CTL_LUN_LIST_ERROR; 3079 snprintf(list->error_str, sizeof(list->error_str), 3080 "Unable to allocate %d bytes for LUN list", 3081 list->alloc_len); 3082 break; 3083 } 3084 3085 sbuf_printf(sb, "<ctllunlist>\n"); 3086 3087 mtx_lock(&softc->ctl_lock); 3088 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3089 mtx_lock(&lun->lun_lock); 3090 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3091 (uintmax_t)lun->lun); 3092 3093 /* 3094 * Bail out as soon as we see that we've overfilled 3095 * the buffer. 3096 */ 3097 if (retval != 0) 3098 break; 3099 3100 retval = sbuf_printf(sb, "\t<backend_type>%s" 3101 "</backend_type>\n", 3102 (lun->backend == NULL) ? "none" : 3103 lun->backend->name); 3104 3105 if (retval != 0) 3106 break; 3107 3108 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3109 lun->be_lun->lun_type); 3110 3111 if (retval != 0) 3112 break; 3113 3114 if (lun->backend == NULL) { 3115 retval = sbuf_printf(sb, "</lun>\n"); 3116 if (retval != 0) 3117 break; 3118 continue; 3119 } 3120 3121 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3122 (lun->be_lun->maxlba > 0) ? 3123 lun->be_lun->maxlba + 1 : 0); 3124 3125 if (retval != 0) 3126 break; 3127 3128 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3129 lun->be_lun->blocksize); 3130 3131 if (retval != 0) 3132 break; 3133 3134 retval = sbuf_printf(sb, "\t<serial_number>"); 3135 3136 if (retval != 0) 3137 break; 3138 3139 retval = ctl_sbuf_printf_esc(sb, 3140 lun->be_lun->serial_num, 3141 sizeof(lun->be_lun->serial_num)); 3142 3143 if (retval != 0) 3144 break; 3145 3146 retval = sbuf_printf(sb, "</serial_number>\n"); 3147 3148 if (retval != 0) 3149 break; 3150 3151 retval = sbuf_printf(sb, "\t<device_id>"); 3152 3153 if (retval != 0) 3154 break; 3155 3156 retval = ctl_sbuf_printf_esc(sb, 3157 lun->be_lun->device_id, 3158 sizeof(lun->be_lun->device_id)); 3159 3160 if (retval != 0) 3161 break; 3162 3163 retval = sbuf_printf(sb, "</device_id>\n"); 3164 3165 if (retval != 0) 3166 break; 3167 3168 if (lun->backend->lun_info != NULL) { 3169 retval = lun->backend->lun_info(lun->be_lun, sb); 3170 if (retval != 0) 3171 break; 3172 } 3173 3174 cookie = NULL; 3175 while ((name = nvlist_next(lun->be_lun->options, &type, 3176 &cookie)) != NULL) { 3177 sbuf_printf(sb, "\t<%s>", name); 3178 3179 if (type == NV_TYPE_STRING) { 3180 value = dnvlist_get_string( 3181 lun->be_lun->options, name, NULL); 3182 if (value != NULL) 3183 sbuf_printf(sb, "%s", value); 3184 } 3185 3186 sbuf_printf(sb, "</%s>\n", name); 3187 } 3188 3189 retval = sbuf_printf(sb, "</lun>\n"); 3190 3191 if (retval != 0) 3192 break; 3193 mtx_unlock(&lun->lun_lock); 3194 } 3195 if (lun != NULL) 3196 mtx_unlock(&lun->lun_lock); 3197 mtx_unlock(&softc->ctl_lock); 3198 3199 if ((retval != 0) 3200 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3201 retval = 0; 3202 sbuf_delete(sb); 3203 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3204 snprintf(list->error_str, sizeof(list->error_str), 3205 "Out of space, %d bytes is too small", 3206 list->alloc_len); 3207 break; 3208 } 3209 3210 sbuf_finish(sb); 3211 3212 retval = copyout(sbuf_data(sb), list->lun_xml, 3213 sbuf_len(sb) + 1); 3214 3215 list->fill_len = sbuf_len(sb) + 1; 3216 list->status = CTL_LUN_LIST_OK; 3217 sbuf_delete(sb); 3218 break; 3219 } 3220 case CTL_ISCSI: { 3221 struct ctl_iscsi *ci; 3222 struct ctl_frontend *fe; 3223 3224 ci = (struct ctl_iscsi *)addr; 3225 3226 fe = ctl_frontend_find("iscsi"); 3227 if (fe == NULL) { 3228 ci->status = CTL_ISCSI_ERROR; 3229 snprintf(ci->error_str, sizeof(ci->error_str), 3230 "Frontend \"iscsi\" not found."); 3231 break; 3232 } 3233 3234 retval = fe->ioctl(dev, cmd, addr, flag, td); 3235 break; 3236 } 3237 case CTL_PORT_REQ: { 3238 struct ctl_req *req; 3239 struct ctl_frontend *fe; 3240 void *packed; 3241 nvlist_t *tmp_args_nvl; 3242 size_t packed_len; 3243 3244 req = (struct ctl_req *)addr; 3245 tmp_args_nvl = req->args_nvl; 3246 3247 fe = ctl_frontend_find(req->driver); 3248 if (fe == NULL) { 3249 req->status = CTL_LUN_ERROR; 3250 snprintf(req->error_str, sizeof(req->error_str), 3251 "Frontend \"%s\" not found.", req->driver); 3252 break; 3253 } 3254 3255 if (req->args != NULL) { 3256 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3257 if (copyin(req->args, packed, req->args_len) != 0) { 3258 free(packed, M_CTL); 3259 req->status = CTL_LUN_ERROR; 3260 snprintf(req->error_str, sizeof(req->error_str), 3261 "Cannot copyin args."); 3262 break; 3263 } 3264 req->args_nvl = nvlist_unpack(packed, 3265 req->args_len, 0); 3266 free(packed, M_CTL); 3267 3268 if (req->args_nvl == NULL) { 3269 req->status = CTL_LUN_ERROR; 3270 snprintf(req->error_str, sizeof(req->error_str), 3271 "Cannot unpack args nvlist."); 3272 break; 3273 } 3274 } else 3275 req->args_nvl = nvlist_create(0); 3276 3277 if (fe->ioctl) 3278 retval = fe->ioctl(dev, cmd, addr, flag, td); 3279 else 3280 retval = ENODEV; 3281 3282 nvlist_destroy(req->args_nvl); 3283 req->args_nvl = tmp_args_nvl; 3284 3285 if (req->result_nvl != NULL) { 3286 if (req->result != NULL) { 3287 packed = nvlist_pack(req->result_nvl, 3288 &packed_len); 3289 if (packed == NULL) { 3290 req->status = CTL_LUN_ERROR; 3291 snprintf(req->error_str, 3292 sizeof(req->error_str), 3293 "Cannot pack result nvlist."); 3294 break; 3295 } 3296 3297 if (packed_len > req->result_len) { 3298 req->status = CTL_LUN_ERROR; 3299 snprintf(req->error_str, 3300 sizeof(req->error_str), 3301 "Result nvlist too large."); 3302 free(packed, M_NVLIST); 3303 break; 3304 } 3305 3306 if (copyout(packed, req->result, packed_len)) { 3307 req->status = CTL_LUN_ERROR; 3308 snprintf(req->error_str, 3309 sizeof(req->error_str), 3310 "Cannot copyout() the result."); 3311 free(packed, M_NVLIST); 3312 break; 3313 } 3314 3315 req->result_len = packed_len; 3316 free(packed, M_NVLIST); 3317 } 3318 3319 nvlist_destroy(req->result_nvl); 3320 } 3321 break; 3322 } 3323 case CTL_PORT_LIST: { 3324 struct sbuf *sb; 3325 struct ctl_port *port; 3326 struct ctl_lun_list *list; 3327 const char *name, *value; 3328 void *cookie; 3329 int j, type; 3330 uint32_t plun; 3331 3332 list = (struct ctl_lun_list *)addr; 3333 3334 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3335 if (sb == NULL) { 3336 list->status = CTL_LUN_LIST_ERROR; 3337 snprintf(list->error_str, sizeof(list->error_str), 3338 "Unable to allocate %d bytes for LUN list", 3339 list->alloc_len); 3340 break; 3341 } 3342 3343 sbuf_printf(sb, "<ctlportlist>\n"); 3344 3345 mtx_lock(&softc->ctl_lock); 3346 STAILQ_FOREACH(port, &softc->port_list, links) { 3347 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3348 (uintmax_t)port->targ_port); 3349 3350 /* 3351 * Bail out as soon as we see that we've overfilled 3352 * the buffer. 3353 */ 3354 if (retval != 0) 3355 break; 3356 3357 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3358 "</frontend_type>\n", port->frontend->name); 3359 if (retval != 0) 3360 break; 3361 3362 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3363 port->port_type); 3364 if (retval != 0) 3365 break; 3366 3367 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3368 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3369 if (retval != 0) 3370 break; 3371 3372 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3373 port->port_name); 3374 if (retval != 0) 3375 break; 3376 3377 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3378 port->physical_port); 3379 if (retval != 0) 3380 break; 3381 3382 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3383 port->virtual_port); 3384 if (retval != 0) 3385 break; 3386 3387 if (port->target_devid != NULL) { 3388 sbuf_printf(sb, "\t<target>"); 3389 ctl_id_sbuf(port->target_devid, sb); 3390 sbuf_printf(sb, "</target>\n"); 3391 } 3392 3393 if (port->port_devid != NULL) { 3394 sbuf_printf(sb, "\t<port>"); 3395 ctl_id_sbuf(port->port_devid, sb); 3396 sbuf_printf(sb, "</port>\n"); 3397 } 3398 3399 if (port->port_info != NULL) { 3400 retval = port->port_info(port->onoff_arg, sb); 3401 if (retval != 0) 3402 break; 3403 } 3404 3405 cookie = NULL; 3406 while ((name = nvlist_next(port->options, &type, 3407 &cookie)) != NULL) { 3408 sbuf_printf(sb, "\t<%s>", name); 3409 3410 if (type == NV_TYPE_STRING) { 3411 value = dnvlist_get_string(port->options, 3412 name, NULL); 3413 if (value != NULL) 3414 sbuf_printf(sb, "%s", value); 3415 } 3416 3417 sbuf_printf(sb, "</%s>\n", name); 3418 } 3419 3420 if (port->lun_map != NULL) { 3421 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3422 for (j = 0; j < port->lun_map_size; j++) { 3423 plun = ctl_lun_map_from_port(port, j); 3424 if (plun == UINT32_MAX) 3425 continue; 3426 sbuf_printf(sb, 3427 "\t<lun id=\"%u\">%u</lun>\n", 3428 j, plun); 3429 } 3430 } 3431 3432 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3433 if (port->wwpn_iid[j].in_use == 0 || 3434 (port->wwpn_iid[j].wwpn == 0 && 3435 port->wwpn_iid[j].name == NULL)) 3436 continue; 3437 3438 if (port->wwpn_iid[j].name != NULL) 3439 retval = sbuf_printf(sb, 3440 "\t<initiator id=\"%u\">%s</initiator>\n", 3441 j, port->wwpn_iid[j].name); 3442 else 3443 retval = sbuf_printf(sb, 3444 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3445 j, port->wwpn_iid[j].wwpn); 3446 if (retval != 0) 3447 break; 3448 } 3449 if (retval != 0) 3450 break; 3451 3452 retval = sbuf_printf(sb, "</targ_port>\n"); 3453 if (retval != 0) 3454 break; 3455 } 3456 mtx_unlock(&softc->ctl_lock); 3457 3458 if ((retval != 0) 3459 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3460 retval = 0; 3461 sbuf_delete(sb); 3462 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3463 snprintf(list->error_str, sizeof(list->error_str), 3464 "Out of space, %d bytes is too small", 3465 list->alloc_len); 3466 break; 3467 } 3468 3469 sbuf_finish(sb); 3470 3471 retval = copyout(sbuf_data(sb), list->lun_xml, 3472 sbuf_len(sb) + 1); 3473 3474 list->fill_len = sbuf_len(sb) + 1; 3475 list->status = CTL_LUN_LIST_OK; 3476 sbuf_delete(sb); 3477 break; 3478 } 3479 case CTL_LUN_MAP: { 3480 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3481 struct ctl_port *port; 3482 3483 mtx_lock(&softc->ctl_lock); 3484 if (lm->port < softc->port_min || 3485 lm->port >= softc->port_max || 3486 (port = softc->ctl_ports[lm->port]) == NULL) { 3487 mtx_unlock(&softc->ctl_lock); 3488 return (ENXIO); 3489 } 3490 if (port->status & CTL_PORT_STATUS_ONLINE) { 3491 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3492 if (ctl_lun_map_to_port(port, lun->lun) == 3493 UINT32_MAX) 3494 continue; 3495 mtx_lock(&lun->lun_lock); 3496 ctl_est_ua_port(lun, lm->port, -1, 3497 CTL_UA_LUN_CHANGE); 3498 mtx_unlock(&lun->lun_lock); 3499 } 3500 } 3501 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3502 if (lm->plun != UINT32_MAX) { 3503 if (lm->lun == UINT32_MAX) 3504 retval = ctl_lun_map_unset(port, lm->plun); 3505 else if (lm->lun < ctl_max_luns && 3506 softc->ctl_luns[lm->lun] != NULL) 3507 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3508 else 3509 return (ENXIO); 3510 } else { 3511 if (lm->lun == UINT32_MAX) 3512 retval = ctl_lun_map_deinit(port); 3513 else 3514 retval = ctl_lun_map_init(port); 3515 } 3516 if (port->status & CTL_PORT_STATUS_ONLINE) 3517 ctl_isc_announce_port(port); 3518 break; 3519 } 3520 case CTL_GET_LUN_STATS: { 3521 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3522 int i; 3523 3524 /* 3525 * XXX KDM no locking here. If the LUN list changes, 3526 * things can blow up. 3527 */ 3528 i = 0; 3529 stats->status = CTL_SS_OK; 3530 stats->fill_len = 0; 3531 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3532 if (lun->lun < stats->first_item) 3533 continue; 3534 if (stats->fill_len + sizeof(lun->stats) > 3535 stats->alloc_len) { 3536 stats->status = CTL_SS_NEED_MORE_SPACE; 3537 break; 3538 } 3539 retval = copyout(&lun->stats, &stats->stats[i++], 3540 sizeof(lun->stats)); 3541 if (retval != 0) 3542 break; 3543 stats->fill_len += sizeof(lun->stats); 3544 } 3545 stats->num_items = softc->num_luns; 3546 stats->flags = CTL_STATS_FLAG_NONE; 3547 #ifdef CTL_TIME_IO 3548 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3549 #endif 3550 getnanouptime(&stats->timestamp); 3551 break; 3552 } 3553 case CTL_GET_PORT_STATS: { 3554 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3555 int i; 3556 3557 /* 3558 * XXX KDM no locking here. If the LUN list changes, 3559 * things can blow up. 3560 */ 3561 i = 0; 3562 stats->status = CTL_SS_OK; 3563 stats->fill_len = 0; 3564 STAILQ_FOREACH(port, &softc->port_list, links) { 3565 if (port->targ_port < stats->first_item) 3566 continue; 3567 if (stats->fill_len + sizeof(port->stats) > 3568 stats->alloc_len) { 3569 stats->status = CTL_SS_NEED_MORE_SPACE; 3570 break; 3571 } 3572 retval = copyout(&port->stats, &stats->stats[i++], 3573 sizeof(port->stats)); 3574 if (retval != 0) 3575 break; 3576 stats->fill_len += sizeof(port->stats); 3577 } 3578 stats->num_items = softc->num_ports; 3579 stats->flags = CTL_STATS_FLAG_NONE; 3580 #ifdef CTL_TIME_IO 3581 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3582 #endif 3583 getnanouptime(&stats->timestamp); 3584 break; 3585 } 3586 default: { 3587 /* XXX KDM should we fix this? */ 3588 #if 0 3589 struct ctl_backend_driver *backend; 3590 unsigned int type; 3591 int found; 3592 3593 found = 0; 3594 3595 /* 3596 * We encode the backend type as the ioctl type for backend 3597 * ioctls. So parse it out here, and then search for a 3598 * backend of this type. 3599 */ 3600 type = _IOC_TYPE(cmd); 3601 3602 STAILQ_FOREACH(backend, &softc->be_list, links) { 3603 if (backend->type == type) { 3604 found = 1; 3605 break; 3606 } 3607 } 3608 if (found == 0) { 3609 printf("ctl: unknown ioctl command %#lx or backend " 3610 "%d\n", cmd, type); 3611 retval = EINVAL; 3612 break; 3613 } 3614 retval = backend->ioctl(dev, cmd, addr, flag, td); 3615 #endif 3616 retval = ENOTTY; 3617 break; 3618 } 3619 } 3620 return (retval); 3621 } 3622 3623 uint32_t 3624 ctl_get_initindex(struct ctl_nexus *nexus) 3625 { 3626 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3627 } 3628 3629 int 3630 ctl_lun_map_init(struct ctl_port *port) 3631 { 3632 struct ctl_softc *softc = port->ctl_softc; 3633 struct ctl_lun *lun; 3634 int size = ctl_lun_map_size; 3635 uint32_t i; 3636 3637 if (port->lun_map == NULL || port->lun_map_size < size) { 3638 port->lun_map_size = 0; 3639 free(port->lun_map, M_CTL); 3640 port->lun_map = malloc(size * sizeof(uint32_t), 3641 M_CTL, M_NOWAIT); 3642 } 3643 if (port->lun_map == NULL) 3644 return (ENOMEM); 3645 for (i = 0; i < size; i++) 3646 port->lun_map[i] = UINT32_MAX; 3647 port->lun_map_size = size; 3648 if (port->status & CTL_PORT_STATUS_ONLINE) { 3649 if (port->lun_disable != NULL) { 3650 STAILQ_FOREACH(lun, &softc->lun_list, links) 3651 port->lun_disable(port->targ_lun_arg, lun->lun); 3652 } 3653 ctl_isc_announce_port(port); 3654 } 3655 return (0); 3656 } 3657 3658 int 3659 ctl_lun_map_deinit(struct ctl_port *port) 3660 { 3661 struct ctl_softc *softc = port->ctl_softc; 3662 struct ctl_lun *lun; 3663 3664 if (port->lun_map == NULL) 3665 return (0); 3666 port->lun_map_size = 0; 3667 free(port->lun_map, M_CTL); 3668 port->lun_map = NULL; 3669 if (port->status & CTL_PORT_STATUS_ONLINE) { 3670 if (port->lun_enable != NULL) { 3671 STAILQ_FOREACH(lun, &softc->lun_list, links) 3672 port->lun_enable(port->targ_lun_arg, lun->lun); 3673 } 3674 ctl_isc_announce_port(port); 3675 } 3676 return (0); 3677 } 3678 3679 int 3680 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3681 { 3682 int status; 3683 uint32_t old; 3684 3685 if (port->lun_map == NULL) { 3686 status = ctl_lun_map_init(port); 3687 if (status != 0) 3688 return (status); 3689 } 3690 if (plun >= port->lun_map_size) 3691 return (EINVAL); 3692 old = port->lun_map[plun]; 3693 port->lun_map[plun] = glun; 3694 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3695 if (port->lun_enable != NULL) 3696 port->lun_enable(port->targ_lun_arg, plun); 3697 ctl_isc_announce_port(port); 3698 } 3699 return (0); 3700 } 3701 3702 int 3703 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3704 { 3705 uint32_t old; 3706 3707 if (port->lun_map == NULL || plun >= port->lun_map_size) 3708 return (0); 3709 old = port->lun_map[plun]; 3710 port->lun_map[plun] = UINT32_MAX; 3711 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3712 if (port->lun_disable != NULL) 3713 port->lun_disable(port->targ_lun_arg, plun); 3714 ctl_isc_announce_port(port); 3715 } 3716 return (0); 3717 } 3718 3719 uint32_t 3720 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3721 { 3722 3723 if (port == NULL) 3724 return (UINT32_MAX); 3725 if (port->lun_map == NULL) 3726 return (lun_id); 3727 if (lun_id > port->lun_map_size) 3728 return (UINT32_MAX); 3729 return (port->lun_map[lun_id]); 3730 } 3731 3732 uint32_t 3733 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3734 { 3735 uint32_t i; 3736 3737 if (port == NULL) 3738 return (UINT32_MAX); 3739 if (port->lun_map == NULL) 3740 return (lun_id); 3741 for (i = 0; i < port->lun_map_size; i++) { 3742 if (port->lun_map[i] == lun_id) 3743 return (i); 3744 } 3745 return (UINT32_MAX); 3746 } 3747 3748 uint32_t 3749 ctl_decode_lun(uint64_t encoded) 3750 { 3751 uint8_t lun[8]; 3752 uint32_t result = 0xffffffff; 3753 3754 be64enc(lun, encoded); 3755 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3756 case RPL_LUNDATA_ATYP_PERIPH: 3757 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3758 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3759 result = lun[1]; 3760 break; 3761 case RPL_LUNDATA_ATYP_FLAT: 3762 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3763 lun[6] == 0 && lun[7] == 0) 3764 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3765 break; 3766 case RPL_LUNDATA_ATYP_EXTLUN: 3767 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3768 case 0x02: 3769 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3770 case 0x00: 3771 result = lun[1]; 3772 break; 3773 case 0x10: 3774 result = (lun[1] << 16) + (lun[2] << 8) + 3775 lun[3]; 3776 break; 3777 case 0x20: 3778 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3779 result = (lun[2] << 24) + 3780 (lun[3] << 16) + (lun[4] << 8) + 3781 lun[5]; 3782 break; 3783 } 3784 break; 3785 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3786 result = 0xffffffff; 3787 break; 3788 } 3789 break; 3790 } 3791 return (result); 3792 } 3793 3794 uint64_t 3795 ctl_encode_lun(uint32_t decoded) 3796 { 3797 uint64_t l = decoded; 3798 3799 if (l <= 0xff) 3800 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3801 if (l <= 0x3fff) 3802 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3803 if (l <= 0xffffff) 3804 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3805 (l << 32)); 3806 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3807 } 3808 3809 int 3810 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3811 { 3812 int i; 3813 3814 for (i = first; i < last; i++) { 3815 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3816 return (i); 3817 } 3818 return (-1); 3819 } 3820 3821 int 3822 ctl_set_mask(uint32_t *mask, uint32_t bit) 3823 { 3824 uint32_t chunk, piece; 3825 3826 chunk = bit >> 5; 3827 piece = bit % (sizeof(uint32_t) * 8); 3828 3829 if ((mask[chunk] & (1 << piece)) != 0) 3830 return (-1); 3831 else 3832 mask[chunk] |= (1 << piece); 3833 3834 return (0); 3835 } 3836 3837 int 3838 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3839 { 3840 uint32_t chunk, piece; 3841 3842 chunk = bit >> 5; 3843 piece = bit % (sizeof(uint32_t) * 8); 3844 3845 if ((mask[chunk] & (1 << piece)) == 0) 3846 return (-1); 3847 else 3848 mask[chunk] &= ~(1 << piece); 3849 3850 return (0); 3851 } 3852 3853 int 3854 ctl_is_set(uint32_t *mask, uint32_t bit) 3855 { 3856 uint32_t chunk, piece; 3857 3858 chunk = bit >> 5; 3859 piece = bit % (sizeof(uint32_t) * 8); 3860 3861 if ((mask[chunk] & (1 << piece)) == 0) 3862 return (0); 3863 else 3864 return (1); 3865 } 3866 3867 static uint64_t 3868 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3869 { 3870 uint64_t *t; 3871 3872 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3873 if (t == NULL) 3874 return (0); 3875 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3876 } 3877 3878 static void 3879 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3880 { 3881 uint64_t *t; 3882 3883 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3884 if (t == NULL) 3885 return; 3886 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3887 } 3888 3889 static void 3890 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3891 { 3892 uint64_t *p; 3893 u_int i; 3894 3895 i = residx/CTL_MAX_INIT_PER_PORT; 3896 if (lun->pr_keys[i] != NULL) 3897 return; 3898 mtx_unlock(&lun->lun_lock); 3899 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3900 M_WAITOK | M_ZERO); 3901 mtx_lock(&lun->lun_lock); 3902 if (lun->pr_keys[i] == NULL) 3903 lun->pr_keys[i] = p; 3904 else 3905 free(p, M_CTL); 3906 } 3907 3908 static void 3909 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3910 { 3911 uint64_t *t; 3912 3913 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3914 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3915 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3916 } 3917 3918 /* 3919 * ctl_softc, pool_name, total_ctl_io are passed in. 3920 * npool is passed out. 3921 */ 3922 int 3923 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3924 uint32_t total_ctl_io, void **npool) 3925 { 3926 struct ctl_io_pool *pool; 3927 3928 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3929 M_NOWAIT | M_ZERO); 3930 if (pool == NULL) 3931 return (ENOMEM); 3932 3933 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3934 pool->ctl_softc = ctl_softc; 3935 #ifdef IO_POOLS 3936 pool->zone = uma_zsecond_create(pool->name, NULL, 3937 NULL, NULL, NULL, ctl_softc->io_zone); 3938 /* uma_prealloc(pool->zone, total_ctl_io); */ 3939 #else 3940 pool->zone = ctl_softc->io_zone; 3941 #endif 3942 3943 *npool = pool; 3944 return (0); 3945 } 3946 3947 void 3948 ctl_pool_free(struct ctl_io_pool *pool) 3949 { 3950 3951 if (pool == NULL) 3952 return; 3953 3954 #ifdef IO_POOLS 3955 uma_zdestroy(pool->zone); 3956 #endif 3957 free(pool, M_CTL); 3958 } 3959 3960 union ctl_io * 3961 ctl_alloc_io(void *pool_ref) 3962 { 3963 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3964 union ctl_io *io; 3965 3966 io = uma_zalloc(pool->zone, M_WAITOK); 3967 if (io != NULL) { 3968 io->io_hdr.pool = pool_ref; 3969 CTL_SOFTC(io) = pool->ctl_softc; 3970 TAILQ_INIT(&io->io_hdr.blocked_queue); 3971 } 3972 return (io); 3973 } 3974 3975 union ctl_io * 3976 ctl_alloc_io_nowait(void *pool_ref) 3977 { 3978 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3979 union ctl_io *io; 3980 3981 io = uma_zalloc(pool->zone, M_NOWAIT); 3982 if (io != NULL) { 3983 io->io_hdr.pool = pool_ref; 3984 CTL_SOFTC(io) = pool->ctl_softc; 3985 TAILQ_INIT(&io->io_hdr.blocked_queue); 3986 } 3987 return (io); 3988 } 3989 3990 void 3991 ctl_free_io(union ctl_io *io) 3992 { 3993 struct ctl_io_pool *pool; 3994 3995 if (io == NULL) 3996 return; 3997 3998 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3999 uma_zfree(pool->zone, io); 4000 } 4001 4002 void 4003 ctl_zero_io(union ctl_io *io) 4004 { 4005 struct ctl_io_pool *pool; 4006 4007 if (io == NULL) 4008 return; 4009 4010 /* 4011 * May need to preserve linked list pointers at some point too. 4012 */ 4013 pool = io->io_hdr.pool; 4014 memset(io, 0, sizeof(*io)); 4015 io->io_hdr.pool = pool; 4016 CTL_SOFTC(io) = pool->ctl_softc; 4017 TAILQ_INIT(&io->io_hdr.blocked_queue); 4018 } 4019 4020 int 4021 ctl_expand_number(const char *buf, uint64_t *num) 4022 { 4023 char *endptr; 4024 uint64_t number; 4025 unsigned shift; 4026 4027 number = strtoq(buf, &endptr, 0); 4028 4029 switch (tolower((unsigned char)*endptr)) { 4030 case 'e': 4031 shift = 60; 4032 break; 4033 case 'p': 4034 shift = 50; 4035 break; 4036 case 't': 4037 shift = 40; 4038 break; 4039 case 'g': 4040 shift = 30; 4041 break; 4042 case 'm': 4043 shift = 20; 4044 break; 4045 case 'k': 4046 shift = 10; 4047 break; 4048 case 'b': 4049 case '\0': /* No unit. */ 4050 *num = number; 4051 return (0); 4052 default: 4053 /* Unrecognized unit. */ 4054 return (-1); 4055 } 4056 4057 if ((number << shift) >> shift != number) { 4058 /* Overflow */ 4059 return (-1); 4060 } 4061 *num = number << shift; 4062 return (0); 4063 } 4064 4065 /* 4066 * This routine could be used in the future to load default and/or saved 4067 * mode page parameters for a particuar lun. 4068 */ 4069 static int 4070 ctl_init_page_index(struct ctl_lun *lun) 4071 { 4072 int i, page_code; 4073 struct ctl_page_index *page_index; 4074 const char *value; 4075 uint64_t ival; 4076 4077 memcpy(&lun->mode_pages.index, page_index_template, 4078 sizeof(page_index_template)); 4079 4080 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4081 page_index = &lun->mode_pages.index[i]; 4082 if (lun->be_lun->lun_type == T_DIRECT && 4083 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4084 continue; 4085 if (lun->be_lun->lun_type == T_PROCESSOR && 4086 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4087 continue; 4088 if (lun->be_lun->lun_type == T_CDROM && 4089 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4090 continue; 4091 4092 page_code = page_index->page_code & SMPH_PC_MASK; 4093 switch (page_code) { 4094 case SMS_RW_ERROR_RECOVERY_PAGE: { 4095 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4096 ("subpage %#x for page %#x is incorrect!", 4097 page_index->subpage, page_code)); 4098 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4099 &rw_er_page_default, 4100 sizeof(rw_er_page_default)); 4101 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4102 &rw_er_page_changeable, 4103 sizeof(rw_er_page_changeable)); 4104 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4105 &rw_er_page_default, 4106 sizeof(rw_er_page_default)); 4107 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4108 &rw_er_page_default, 4109 sizeof(rw_er_page_default)); 4110 page_index->page_data = 4111 (uint8_t *)lun->mode_pages.rw_er_page; 4112 break; 4113 } 4114 case SMS_FORMAT_DEVICE_PAGE: { 4115 struct scsi_format_page *format_page; 4116 4117 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4118 ("subpage %#x for page %#x is incorrect!", 4119 page_index->subpage, page_code)); 4120 4121 /* 4122 * Sectors per track are set above. Bytes per 4123 * sector need to be set here on a per-LUN basis. 4124 */ 4125 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4126 &format_page_default, 4127 sizeof(format_page_default)); 4128 memcpy(&lun->mode_pages.format_page[ 4129 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4130 sizeof(format_page_changeable)); 4131 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4132 &format_page_default, 4133 sizeof(format_page_default)); 4134 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4135 &format_page_default, 4136 sizeof(format_page_default)); 4137 4138 format_page = &lun->mode_pages.format_page[ 4139 CTL_PAGE_CURRENT]; 4140 scsi_ulto2b(lun->be_lun->blocksize, 4141 format_page->bytes_per_sector); 4142 4143 format_page = &lun->mode_pages.format_page[ 4144 CTL_PAGE_DEFAULT]; 4145 scsi_ulto2b(lun->be_lun->blocksize, 4146 format_page->bytes_per_sector); 4147 4148 format_page = &lun->mode_pages.format_page[ 4149 CTL_PAGE_SAVED]; 4150 scsi_ulto2b(lun->be_lun->blocksize, 4151 format_page->bytes_per_sector); 4152 4153 page_index->page_data = 4154 (uint8_t *)lun->mode_pages.format_page; 4155 break; 4156 } 4157 case SMS_RIGID_DISK_PAGE: { 4158 struct scsi_rigid_disk_page *rigid_disk_page; 4159 uint32_t sectors_per_cylinder; 4160 uint64_t cylinders; 4161 #ifndef __XSCALE__ 4162 int shift; 4163 #endif /* !__XSCALE__ */ 4164 4165 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4166 ("subpage %#x for page %#x is incorrect!", 4167 page_index->subpage, page_code)); 4168 4169 /* 4170 * Rotation rate and sectors per track are set 4171 * above. We calculate the cylinders here based on 4172 * capacity. Due to the number of heads and 4173 * sectors per track we're using, smaller arrays 4174 * may turn out to have 0 cylinders. Linux and 4175 * FreeBSD don't pay attention to these mode pages 4176 * to figure out capacity, but Solaris does. It 4177 * seems to deal with 0 cylinders just fine, and 4178 * works out a fake geometry based on the capacity. 4179 */ 4180 memcpy(&lun->mode_pages.rigid_disk_page[ 4181 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4182 sizeof(rigid_disk_page_default)); 4183 memcpy(&lun->mode_pages.rigid_disk_page[ 4184 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4185 sizeof(rigid_disk_page_changeable)); 4186 4187 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4188 CTL_DEFAULT_HEADS; 4189 4190 /* 4191 * The divide method here will be more accurate, 4192 * probably, but results in floating point being 4193 * used in the kernel on i386 (__udivdi3()). On the 4194 * XScale, though, __udivdi3() is implemented in 4195 * software. 4196 * 4197 * The shift method for cylinder calculation is 4198 * accurate if sectors_per_cylinder is a power of 4199 * 2. Otherwise it might be slightly off -- you 4200 * might have a bit of a truncation problem. 4201 */ 4202 #ifdef __XSCALE__ 4203 cylinders = (lun->be_lun->maxlba + 1) / 4204 sectors_per_cylinder; 4205 #else 4206 for (shift = 31; shift > 0; shift--) { 4207 if (sectors_per_cylinder & (1 << shift)) 4208 break; 4209 } 4210 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4211 #endif 4212 4213 /* 4214 * We've basically got 3 bytes, or 24 bits for the 4215 * cylinder size in the mode page. If we're over, 4216 * just round down to 2^24. 4217 */ 4218 if (cylinders > 0xffffff) 4219 cylinders = 0xffffff; 4220 4221 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4222 CTL_PAGE_DEFAULT]; 4223 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4224 4225 if ((value = dnvlist_get_string(lun->be_lun->options, 4226 "rpm", NULL)) != NULL) { 4227 scsi_ulto2b(strtol(value, NULL, 0), 4228 rigid_disk_page->rotation_rate); 4229 } 4230 4231 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4232 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4233 sizeof(rigid_disk_page_default)); 4234 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4235 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4236 sizeof(rigid_disk_page_default)); 4237 4238 page_index->page_data = 4239 (uint8_t *)lun->mode_pages.rigid_disk_page; 4240 break; 4241 } 4242 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4243 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4244 ("subpage %#x for page %#x is incorrect!", 4245 page_index->subpage, page_code)); 4246 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4247 &verify_er_page_default, 4248 sizeof(verify_er_page_default)); 4249 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4250 &verify_er_page_changeable, 4251 sizeof(verify_er_page_changeable)); 4252 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4253 &verify_er_page_default, 4254 sizeof(verify_er_page_default)); 4255 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4256 &verify_er_page_default, 4257 sizeof(verify_er_page_default)); 4258 page_index->page_data = 4259 (uint8_t *)lun->mode_pages.verify_er_page; 4260 break; 4261 } 4262 case SMS_CACHING_PAGE: { 4263 struct scsi_caching_page *caching_page; 4264 4265 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4266 ("subpage %#x for page %#x is incorrect!", 4267 page_index->subpage, page_code)); 4268 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4269 &caching_page_default, 4270 sizeof(caching_page_default)); 4271 memcpy(&lun->mode_pages.caching_page[ 4272 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4273 sizeof(caching_page_changeable)); 4274 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4275 &caching_page_default, 4276 sizeof(caching_page_default)); 4277 caching_page = &lun->mode_pages.caching_page[ 4278 CTL_PAGE_SAVED]; 4279 value = dnvlist_get_string(lun->be_lun->options, 4280 "writecache", NULL); 4281 if (value != NULL && strcmp(value, "off") == 0) 4282 caching_page->flags1 &= ~SCP_WCE; 4283 value = dnvlist_get_string(lun->be_lun->options, 4284 "readcache", NULL); 4285 if (value != NULL && strcmp(value, "off") == 0) 4286 caching_page->flags1 |= SCP_RCD; 4287 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4288 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4289 sizeof(caching_page_default)); 4290 page_index->page_data = 4291 (uint8_t *)lun->mode_pages.caching_page; 4292 break; 4293 } 4294 case SMS_CONTROL_MODE_PAGE: { 4295 switch (page_index->subpage) { 4296 case SMS_SUBPAGE_PAGE_0: { 4297 struct scsi_control_page *control_page; 4298 4299 memcpy(&lun->mode_pages.control_page[ 4300 CTL_PAGE_DEFAULT], 4301 &control_page_default, 4302 sizeof(control_page_default)); 4303 memcpy(&lun->mode_pages.control_page[ 4304 CTL_PAGE_CHANGEABLE], 4305 &control_page_changeable, 4306 sizeof(control_page_changeable)); 4307 memcpy(&lun->mode_pages.control_page[ 4308 CTL_PAGE_SAVED], 4309 &control_page_default, 4310 sizeof(control_page_default)); 4311 control_page = &lun->mode_pages.control_page[ 4312 CTL_PAGE_SAVED]; 4313 value = dnvlist_get_string(lun->be_lun->options, 4314 "reordering", NULL); 4315 if (value != NULL && 4316 strcmp(value, "unrestricted") == 0) { 4317 control_page->queue_flags &= 4318 ~SCP_QUEUE_ALG_MASK; 4319 control_page->queue_flags |= 4320 SCP_QUEUE_ALG_UNRESTRICTED; 4321 } 4322 memcpy(&lun->mode_pages.control_page[ 4323 CTL_PAGE_CURRENT], 4324 &lun->mode_pages.control_page[ 4325 CTL_PAGE_SAVED], 4326 sizeof(control_page_default)); 4327 page_index->page_data = 4328 (uint8_t *)lun->mode_pages.control_page; 4329 break; 4330 } 4331 case 0x01: 4332 memcpy(&lun->mode_pages.control_ext_page[ 4333 CTL_PAGE_DEFAULT], 4334 &control_ext_page_default, 4335 sizeof(control_ext_page_default)); 4336 memcpy(&lun->mode_pages.control_ext_page[ 4337 CTL_PAGE_CHANGEABLE], 4338 &control_ext_page_changeable, 4339 sizeof(control_ext_page_changeable)); 4340 memcpy(&lun->mode_pages.control_ext_page[ 4341 CTL_PAGE_SAVED], 4342 &control_ext_page_default, 4343 sizeof(control_ext_page_default)); 4344 memcpy(&lun->mode_pages.control_ext_page[ 4345 CTL_PAGE_CURRENT], 4346 &lun->mode_pages.control_ext_page[ 4347 CTL_PAGE_SAVED], 4348 sizeof(control_ext_page_default)); 4349 page_index->page_data = 4350 (uint8_t *)lun->mode_pages.control_ext_page; 4351 break; 4352 default: 4353 panic("subpage %#x for page %#x is incorrect!", 4354 page_index->subpage, page_code); 4355 } 4356 break; 4357 } 4358 case SMS_INFO_EXCEPTIONS_PAGE: { 4359 switch (page_index->subpage) { 4360 case SMS_SUBPAGE_PAGE_0: 4361 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4362 &ie_page_default, 4363 sizeof(ie_page_default)); 4364 memcpy(&lun->mode_pages.ie_page[ 4365 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4366 sizeof(ie_page_changeable)); 4367 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4368 &ie_page_default, 4369 sizeof(ie_page_default)); 4370 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4371 &ie_page_default, 4372 sizeof(ie_page_default)); 4373 page_index->page_data = 4374 (uint8_t *)lun->mode_pages.ie_page; 4375 break; 4376 case 0x02: { 4377 struct ctl_logical_block_provisioning_page *page; 4378 4379 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4380 &lbp_page_default, 4381 sizeof(lbp_page_default)); 4382 memcpy(&lun->mode_pages.lbp_page[ 4383 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4384 sizeof(lbp_page_changeable)); 4385 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4386 &lbp_page_default, 4387 sizeof(lbp_page_default)); 4388 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4389 value = dnvlist_get_string(lun->be_lun->options, 4390 "avail-threshold", NULL); 4391 if (value != NULL && 4392 ctl_expand_number(value, &ival) == 0) { 4393 page->descr[0].flags |= SLBPPD_ENABLED | 4394 SLBPPD_ARMING_DEC; 4395 if (lun->be_lun->blocksize) 4396 ival /= lun->be_lun->blocksize; 4397 else 4398 ival /= 512; 4399 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4400 page->descr[0].count); 4401 } 4402 value = dnvlist_get_string(lun->be_lun->options, 4403 "used-threshold", NULL); 4404 if (value != NULL && 4405 ctl_expand_number(value, &ival) == 0) { 4406 page->descr[1].flags |= SLBPPD_ENABLED | 4407 SLBPPD_ARMING_INC; 4408 if (lun->be_lun->blocksize) 4409 ival /= lun->be_lun->blocksize; 4410 else 4411 ival /= 512; 4412 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4413 page->descr[1].count); 4414 } 4415 value = dnvlist_get_string(lun->be_lun->options, 4416 "pool-avail-threshold", NULL); 4417 if (value != NULL && 4418 ctl_expand_number(value, &ival) == 0) { 4419 page->descr[2].flags |= SLBPPD_ENABLED | 4420 SLBPPD_ARMING_DEC; 4421 if (lun->be_lun->blocksize) 4422 ival /= lun->be_lun->blocksize; 4423 else 4424 ival /= 512; 4425 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4426 page->descr[2].count); 4427 } 4428 value = dnvlist_get_string(lun->be_lun->options, 4429 "pool-used-threshold", NULL); 4430 if (value != NULL && 4431 ctl_expand_number(value, &ival) == 0) { 4432 page->descr[3].flags |= SLBPPD_ENABLED | 4433 SLBPPD_ARMING_INC; 4434 if (lun->be_lun->blocksize) 4435 ival /= lun->be_lun->blocksize; 4436 else 4437 ival /= 512; 4438 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4439 page->descr[3].count); 4440 } 4441 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4442 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4443 sizeof(lbp_page_default)); 4444 page_index->page_data = 4445 (uint8_t *)lun->mode_pages.lbp_page; 4446 break; 4447 } 4448 default: 4449 panic("subpage %#x for page %#x is incorrect!", 4450 page_index->subpage, page_code); 4451 } 4452 break; 4453 } 4454 case SMS_CDDVD_CAPS_PAGE:{ 4455 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4456 ("subpage %#x for page %#x is incorrect!", 4457 page_index->subpage, page_code)); 4458 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4459 &cddvd_page_default, 4460 sizeof(cddvd_page_default)); 4461 memcpy(&lun->mode_pages.cddvd_page[ 4462 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4463 sizeof(cddvd_page_changeable)); 4464 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4465 &cddvd_page_default, 4466 sizeof(cddvd_page_default)); 4467 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4468 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4469 sizeof(cddvd_page_default)); 4470 page_index->page_data = 4471 (uint8_t *)lun->mode_pages.cddvd_page; 4472 break; 4473 } 4474 default: 4475 panic("invalid page code value %#x", page_code); 4476 } 4477 } 4478 4479 return (CTL_RETVAL_COMPLETE); 4480 } 4481 4482 static int 4483 ctl_init_log_page_index(struct ctl_lun *lun) 4484 { 4485 struct ctl_page_index *page_index; 4486 int i, j, k, prev; 4487 4488 memcpy(&lun->log_pages.index, log_page_index_template, 4489 sizeof(log_page_index_template)); 4490 4491 prev = -1; 4492 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4493 page_index = &lun->log_pages.index[i]; 4494 if (lun->be_lun->lun_type == T_DIRECT && 4495 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4496 continue; 4497 if (lun->be_lun->lun_type == T_PROCESSOR && 4498 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4499 continue; 4500 if (lun->be_lun->lun_type == T_CDROM && 4501 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4502 continue; 4503 4504 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4505 lun->backend->lun_attr == NULL) 4506 continue; 4507 4508 if (page_index->page_code != prev) { 4509 lun->log_pages.pages_page[j] = page_index->page_code; 4510 prev = page_index->page_code; 4511 j++; 4512 } 4513 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4514 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4515 k++; 4516 } 4517 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4518 lun->log_pages.index[0].page_len = j; 4519 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4520 lun->log_pages.index[1].page_len = k * 2; 4521 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page; 4522 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page); 4523 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0]; 4524 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS; 4525 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page; 4526 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page); 4527 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page; 4528 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page); 4529 4530 return (CTL_RETVAL_COMPLETE); 4531 } 4532 4533 static int 4534 hex2bin(const char *str, uint8_t *buf, int buf_size) 4535 { 4536 int i; 4537 u_char c; 4538 4539 memset(buf, 0, buf_size); 4540 while (isspace(str[0])) 4541 str++; 4542 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4543 str += 2; 4544 buf_size *= 2; 4545 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4546 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4547 str++; 4548 c = str[i]; 4549 if (isdigit(c)) 4550 c -= '0'; 4551 else if (isalpha(c)) 4552 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4553 else 4554 break; 4555 if (c >= 16) 4556 break; 4557 if ((i & 1) == 0) 4558 buf[i / 2] |= (c << 4); 4559 else 4560 buf[i / 2] |= c; 4561 } 4562 return ((i + 1) / 2); 4563 } 4564 4565 /* 4566 * Add LUN. 4567 * 4568 * Returns 0 for success, non-zero (errno) for failure. 4569 */ 4570 int 4571 ctl_add_lun(struct ctl_be_lun *be_lun) 4572 { 4573 struct ctl_softc *ctl_softc = control_softc; 4574 struct ctl_lun *nlun, *lun; 4575 struct scsi_vpd_id_descriptor *desc; 4576 struct scsi_vpd_id_t10 *t10id; 4577 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4578 int lun_number; 4579 int devidlen, idlen1, idlen2 = 0, len; 4580 4581 /* 4582 * We support only Direct Access, CD-ROM or Processor LUN types. 4583 */ 4584 switch (be_lun->lun_type) { 4585 case T_DIRECT: 4586 case T_PROCESSOR: 4587 case T_CDROM: 4588 break; 4589 case T_SEQUENTIAL: 4590 case T_CHANGER: 4591 default: 4592 return (EINVAL); 4593 } 4594 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO); 4595 4596 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4597 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4598 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4599 M_DEVBUF, M_WAITOK | M_ZERO); 4600 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4601 M_DEVBUF, M_WAITOK | M_ZERO); 4602 4603 /* Generate LUN ID. */ 4604 devidlen = max(CTL_DEVID_MIN_LEN, 4605 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4606 idlen1 = sizeof(*t10id) + devidlen; 4607 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4608 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4609 if (scsiname != NULL) { 4610 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4611 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4612 } 4613 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4614 if (eui != NULL) { 4615 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4616 } 4617 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4618 if (naa != NULL) { 4619 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4620 } 4621 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4622 if (uuid != NULL) { 4623 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4624 } 4625 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4626 M_CTL, M_WAITOK | M_ZERO); 4627 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4628 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4629 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4630 desc->length = idlen1; 4631 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4632 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4633 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4634 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4635 } else { 4636 strncpy(t10id->vendor, vendor, 4637 min(sizeof(t10id->vendor), strlen(vendor))); 4638 } 4639 strncpy((char *)t10id->vendor_spec_id, 4640 (char *)be_lun->device_id, devidlen); 4641 if (scsiname != NULL) { 4642 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4643 desc->length); 4644 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4645 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4646 SVPD_ID_TYPE_SCSI_NAME; 4647 desc->length = idlen2; 4648 strlcpy(desc->identifier, scsiname, idlen2); 4649 } 4650 if (eui != NULL) { 4651 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4652 desc->length); 4653 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4654 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4655 SVPD_ID_TYPE_EUI64; 4656 desc->length = hex2bin(eui, desc->identifier, 16); 4657 desc->length = desc->length > 12 ? 16 : 4658 (desc->length > 8 ? 12 : 8); 4659 len -= 16 - desc->length; 4660 } 4661 if (naa != NULL) { 4662 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4663 desc->length); 4664 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4665 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4666 SVPD_ID_TYPE_NAA; 4667 desc->length = hex2bin(naa, desc->identifier, 16); 4668 desc->length = desc->length > 8 ? 16 : 8; 4669 len -= 16 - desc->length; 4670 } 4671 if (uuid != NULL) { 4672 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4673 desc->length); 4674 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4675 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4676 SVPD_ID_TYPE_UUID; 4677 desc->identifier[0] = 0x10; 4678 hex2bin(uuid, &desc->identifier[2], 16); 4679 desc->length = 18; 4680 } 4681 lun->lun_devid->len = len; 4682 4683 mtx_lock(&ctl_softc->ctl_lock); 4684 /* 4685 * See if the caller requested a particular LUN number. If so, see 4686 * if it is available. Otherwise, allocate the first available LUN. 4687 */ 4688 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4689 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4690 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4691 mtx_unlock(&ctl_softc->ctl_lock); 4692 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4693 printf("ctl: requested LUN ID %d is higher " 4694 "than ctl_max_luns - 1 (%d)\n", 4695 be_lun->req_lun_id, ctl_max_luns - 1); 4696 } else { 4697 /* 4698 * XXX KDM return an error, or just assign 4699 * another LUN ID in this case?? 4700 */ 4701 printf("ctl: requested LUN ID %d is already " 4702 "in use\n", be_lun->req_lun_id); 4703 } 4704 fail: 4705 free(lun->lun_devid, M_CTL); 4706 free(lun, M_CTL); 4707 return (ENOSPC); 4708 } 4709 lun_number = be_lun->req_lun_id; 4710 } else { 4711 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4712 if (lun_number == -1) { 4713 mtx_unlock(&ctl_softc->ctl_lock); 4714 printf("ctl: can't allocate LUN, out of LUNs\n"); 4715 goto fail; 4716 } 4717 } 4718 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4719 mtx_unlock(&ctl_softc->ctl_lock); 4720 4721 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4722 lun->lun = lun_number; 4723 lun->be_lun = be_lun; 4724 /* 4725 * The processor LUN is always enabled. Disk LUNs come on line 4726 * disabled, and must be enabled by the backend. 4727 */ 4728 lun->flags |= CTL_LUN_DISABLED; 4729 lun->backend = be_lun->be; 4730 be_lun->ctl_lun = lun; 4731 be_lun->lun_id = lun_number; 4732 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4733 lun->flags |= CTL_LUN_EJECTED; 4734 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4735 lun->flags |= CTL_LUN_NO_MEDIA; 4736 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4737 lun->flags |= CTL_LUN_STOPPED; 4738 4739 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4740 lun->flags |= CTL_LUN_PRIMARY_SC; 4741 4742 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4743 if (value != NULL) { 4744 if (strcmp(value, "on") == 0) 4745 lun->flags |= CTL_LUN_REMOVABLE; 4746 } else if (be_lun->lun_type == T_CDROM) 4747 lun->flags |= CTL_LUN_REMOVABLE; 4748 4749 lun->ctl_softc = ctl_softc; 4750 #ifdef CTL_TIME_IO 4751 lun->last_busy = getsbinuptime(); 4752 #endif 4753 LIST_INIT(&lun->ooa_queue); 4754 STAILQ_INIT(&lun->error_list); 4755 lun->ie_reported = 1; 4756 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4757 ctl_tpc_lun_init(lun); 4758 if (lun->flags & CTL_LUN_REMOVABLE) { 4759 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4760 M_CTL, M_WAITOK); 4761 } 4762 4763 /* 4764 * Initialize the mode and log page index. 4765 */ 4766 ctl_init_page_index(lun); 4767 ctl_init_log_page_index(lun); 4768 4769 /* Setup statistics gathering */ 4770 lun->stats.item = lun_number; 4771 4772 /* 4773 * Now, before we insert this lun on the lun list, set the lun 4774 * inventory changed UA for all other luns. 4775 */ 4776 mtx_lock(&ctl_softc->ctl_lock); 4777 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4778 mtx_lock(&nlun->lun_lock); 4779 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4780 mtx_unlock(&nlun->lun_lock); 4781 } 4782 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4783 ctl_softc->ctl_luns[lun_number] = lun; 4784 ctl_softc->num_luns++; 4785 mtx_unlock(&ctl_softc->ctl_lock); 4786 4787 /* 4788 * We successfully added the LUN, attempt to enable it. 4789 */ 4790 if (ctl_enable_lun(lun) != 0) { 4791 printf("%s: ctl_enable_lun() failed!\n", __func__); 4792 mtx_lock(&ctl_softc->ctl_lock); 4793 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links); 4794 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number); 4795 ctl_softc->ctl_luns[lun_number] = NULL; 4796 ctl_softc->num_luns--; 4797 mtx_unlock(&ctl_softc->ctl_lock); 4798 free(lun->lun_devid, M_CTL); 4799 free(lun, M_CTL); 4800 return (EIO); 4801 } 4802 4803 return (0); 4804 } 4805 4806 /* 4807 * Free LUN that has no active requests. 4808 */ 4809 static int 4810 ctl_free_lun(struct ctl_lun *lun) 4811 { 4812 struct ctl_softc *softc = lun->ctl_softc; 4813 struct ctl_lun *nlun; 4814 int i; 4815 4816 KASSERT(LIST_EMPTY(&lun->ooa_queue), 4817 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4818 4819 mtx_lock(&softc->ctl_lock); 4820 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4821 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4822 softc->ctl_luns[lun->lun] = NULL; 4823 softc->num_luns--; 4824 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4825 mtx_lock(&nlun->lun_lock); 4826 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4827 mtx_unlock(&nlun->lun_lock); 4828 } 4829 mtx_unlock(&softc->ctl_lock); 4830 4831 /* 4832 * Tell the backend to free resources, if this LUN has a backend. 4833 */ 4834 lun->be_lun->lun_shutdown(lun->be_lun); 4835 4836 lun->ie_reportcnt = UINT32_MAX; 4837 callout_drain(&lun->ie_callout); 4838 ctl_tpc_lun_shutdown(lun); 4839 mtx_destroy(&lun->lun_lock); 4840 free(lun->lun_devid, M_CTL); 4841 for (i = 0; i < ctl_max_ports; i++) 4842 free(lun->pending_ua[i], M_CTL); 4843 free(lun->pending_ua, M_DEVBUF); 4844 for (i = 0; i < ctl_max_ports; i++) 4845 free(lun->pr_keys[i], M_CTL); 4846 free(lun->pr_keys, M_DEVBUF); 4847 free(lun->write_buffer, M_CTL); 4848 free(lun->prevent, M_CTL); 4849 free(lun, M_CTL); 4850 4851 return (0); 4852 } 4853 4854 static int 4855 ctl_enable_lun(struct ctl_lun *lun) 4856 { 4857 struct ctl_softc *softc; 4858 struct ctl_port *port, *nport; 4859 int retval; 4860 4861 softc = lun->ctl_softc; 4862 4863 mtx_lock(&softc->ctl_lock); 4864 mtx_lock(&lun->lun_lock); 4865 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0, 4866 ("%s: LUN not disabled", __func__)); 4867 lun->flags &= ~CTL_LUN_DISABLED; 4868 mtx_unlock(&lun->lun_lock); 4869 4870 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4871 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4872 port->lun_map != NULL || port->lun_enable == NULL) 4873 continue; 4874 4875 /* 4876 * Drop the lock while we call the FETD's enable routine. 4877 * This can lead to a callback into CTL (at least in the 4878 * case of the internal initiator frontend. 4879 */ 4880 mtx_unlock(&softc->ctl_lock); 4881 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4882 mtx_lock(&softc->ctl_lock); 4883 if (retval != 0) { 4884 printf("%s: FETD %s port %d returned error " 4885 "%d for lun_enable on lun %jd\n", 4886 __func__, port->port_name, port->targ_port, 4887 retval, (intmax_t)lun->lun); 4888 } 4889 } 4890 4891 mtx_unlock(&softc->ctl_lock); 4892 ctl_isc_announce_lun(lun); 4893 4894 return (0); 4895 } 4896 4897 static int 4898 ctl_disable_lun(struct ctl_lun *lun) 4899 { 4900 struct ctl_softc *softc; 4901 struct ctl_port *port; 4902 int retval; 4903 4904 softc = lun->ctl_softc; 4905 4906 mtx_lock(&softc->ctl_lock); 4907 mtx_lock(&lun->lun_lock); 4908 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0, 4909 ("%s: LUN not enabled", __func__)); 4910 lun->flags |= CTL_LUN_DISABLED; 4911 mtx_unlock(&lun->lun_lock); 4912 4913 STAILQ_FOREACH(port, &softc->port_list, links) { 4914 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4915 port->lun_map != NULL || port->lun_disable == NULL) 4916 continue; 4917 4918 /* 4919 * Drop the lock before we call the frontend's disable 4920 * routine, to avoid lock order reversals. 4921 * 4922 * XXX KDM what happens if the frontend list changes while 4923 * we're traversing it? It's unlikely, but should be handled. 4924 */ 4925 mtx_unlock(&softc->ctl_lock); 4926 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4927 mtx_lock(&softc->ctl_lock); 4928 if (retval != 0) { 4929 printf("%s: FETD %s port %d returned error " 4930 "%d for lun_disable on lun %jd\n", 4931 __func__, port->port_name, port->targ_port, 4932 retval, (intmax_t)lun->lun); 4933 } 4934 } 4935 4936 mtx_unlock(&softc->ctl_lock); 4937 ctl_isc_announce_lun(lun); 4938 4939 return (0); 4940 } 4941 4942 int 4943 ctl_start_lun(struct ctl_be_lun *be_lun) 4944 { 4945 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4946 4947 mtx_lock(&lun->lun_lock); 4948 lun->flags &= ~CTL_LUN_STOPPED; 4949 mtx_unlock(&lun->lun_lock); 4950 return (0); 4951 } 4952 4953 int 4954 ctl_stop_lun(struct ctl_be_lun *be_lun) 4955 { 4956 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4957 4958 mtx_lock(&lun->lun_lock); 4959 lun->flags |= CTL_LUN_STOPPED; 4960 mtx_unlock(&lun->lun_lock); 4961 return (0); 4962 } 4963 4964 int 4965 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4966 { 4967 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4968 4969 mtx_lock(&lun->lun_lock); 4970 lun->flags |= CTL_LUN_NO_MEDIA; 4971 mtx_unlock(&lun->lun_lock); 4972 return (0); 4973 } 4974 4975 int 4976 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4977 { 4978 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4979 union ctl_ha_msg msg; 4980 4981 mtx_lock(&lun->lun_lock); 4982 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4983 if (lun->flags & CTL_LUN_REMOVABLE) 4984 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4985 mtx_unlock(&lun->lun_lock); 4986 if ((lun->flags & CTL_LUN_REMOVABLE) && 4987 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4988 bzero(&msg.ua, sizeof(msg.ua)); 4989 msg.hdr.msg_type = CTL_MSG_UA; 4990 msg.hdr.nexus.initid = -1; 4991 msg.hdr.nexus.targ_port = -1; 4992 msg.hdr.nexus.targ_lun = lun->lun; 4993 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4994 msg.ua.ua_all = 1; 4995 msg.ua.ua_set = 1; 4996 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4997 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4998 M_WAITOK); 4999 } 5000 return (0); 5001 } 5002 5003 int 5004 ctl_lun_ejected(struct ctl_be_lun *be_lun) 5005 { 5006 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5007 5008 mtx_lock(&lun->lun_lock); 5009 lun->flags |= CTL_LUN_EJECTED; 5010 mtx_unlock(&lun->lun_lock); 5011 return (0); 5012 } 5013 5014 int 5015 ctl_lun_primary(struct ctl_be_lun *be_lun) 5016 { 5017 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5018 5019 mtx_lock(&lun->lun_lock); 5020 lun->flags |= CTL_LUN_PRIMARY_SC; 5021 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5022 mtx_unlock(&lun->lun_lock); 5023 ctl_isc_announce_lun(lun); 5024 return (0); 5025 } 5026 5027 int 5028 ctl_lun_secondary(struct ctl_be_lun *be_lun) 5029 { 5030 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5031 5032 mtx_lock(&lun->lun_lock); 5033 lun->flags &= ~CTL_LUN_PRIMARY_SC; 5034 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5035 mtx_unlock(&lun->lun_lock); 5036 ctl_isc_announce_lun(lun); 5037 return (0); 5038 } 5039 5040 /* 5041 * Remove LUN. If there are active requests, wait for completion. 5042 * 5043 * Returns 0 for success, non-zero (errno) for failure. 5044 * Completion is reported to backed via the lun_shutdown() method. 5045 */ 5046 int 5047 ctl_remove_lun(struct ctl_be_lun *be_lun) 5048 { 5049 struct ctl_lun *lun; 5050 5051 lun = (struct ctl_lun *)be_lun->ctl_lun; 5052 5053 ctl_disable_lun(lun); 5054 5055 mtx_lock(&lun->lun_lock); 5056 lun->flags |= CTL_LUN_INVALID; 5057 5058 /* 5059 * If there is nothing in the OOA queue, go ahead and free the LUN. 5060 * If we have something in the OOA queue, we'll free it when the 5061 * last I/O completes. 5062 */ 5063 if (LIST_EMPTY(&lun->ooa_queue)) { 5064 mtx_unlock(&lun->lun_lock); 5065 ctl_free_lun(lun); 5066 } else 5067 mtx_unlock(&lun->lun_lock); 5068 5069 return (0); 5070 } 5071 5072 void 5073 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5074 { 5075 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5076 union ctl_ha_msg msg; 5077 5078 mtx_lock(&lun->lun_lock); 5079 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5080 mtx_unlock(&lun->lun_lock); 5081 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5082 /* Send msg to other side. */ 5083 bzero(&msg.ua, sizeof(msg.ua)); 5084 msg.hdr.msg_type = CTL_MSG_UA; 5085 msg.hdr.nexus.initid = -1; 5086 msg.hdr.nexus.targ_port = -1; 5087 msg.hdr.nexus.targ_lun = lun->lun; 5088 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5089 msg.ua.ua_all = 1; 5090 msg.ua.ua_set = 1; 5091 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5092 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5093 M_WAITOK); 5094 } 5095 } 5096 5097 /* 5098 * Backend "memory move is complete" callback for requests that never 5099 * make it down to say RAIDCore's configuration code. 5100 */ 5101 int 5102 ctl_config_move_done(union ctl_io *io, bool samethr) 5103 { 5104 int retval; 5105 5106 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5107 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5108 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 5109 5110 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5111 ctl_data_print(io); 5112 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5113 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5114 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5115 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5116 /* 5117 * XXX KDM just assuming a single pointer here, and not a 5118 * S/G list. If we start using S/G lists for config data, 5119 * we'll need to know how to clean them up here as well. 5120 */ 5121 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5122 free(io->scsiio.kern_data_ptr, M_CTL); 5123 ctl_done(io); 5124 retval = CTL_RETVAL_COMPLETE; 5125 } else { 5126 /* 5127 * XXX KDM now we need to continue data movement. Some 5128 * options: 5129 * - call ctl_scsiio() again? We don't do this for data 5130 * writes, because for those at least we know ahead of 5131 * time where the write will go and how long it is. For 5132 * config writes, though, that information is largely 5133 * contained within the write itself, thus we need to 5134 * parse out the data again. 5135 * 5136 * - Call some other function once the data is in? 5137 */ 5138 5139 /* 5140 * XXX KDM call ctl_scsiio() again for now, and check flag 5141 * bits to see whether we're allocated or not. 5142 */ 5143 retval = ctl_scsiio(&io->scsiio); 5144 } 5145 return (retval); 5146 } 5147 5148 /* 5149 * This gets called by a backend driver when it is done with a 5150 * data_submit method. 5151 */ 5152 void 5153 ctl_data_submit_done(union ctl_io *io) 5154 { 5155 /* 5156 * If the IO_CONT flag is set, we need to call the supplied 5157 * function to continue processing the I/O, instead of completing 5158 * the I/O just yet. 5159 * 5160 * If there is an error, though, we don't want to keep processing. 5161 * Instead, just send status back to the initiator. 5162 */ 5163 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5164 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5165 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5166 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5167 io->scsiio.io_cont(io); 5168 return; 5169 } 5170 ctl_done(io); 5171 } 5172 5173 /* 5174 * This gets called by a backend driver when it is done with a 5175 * configuration write. 5176 */ 5177 void 5178 ctl_config_write_done(union ctl_io *io) 5179 { 5180 uint8_t *buf; 5181 5182 /* 5183 * If the IO_CONT flag is set, we need to call the supplied 5184 * function to continue processing the I/O, instead of completing 5185 * the I/O just yet. 5186 * 5187 * If there is an error, though, we don't want to keep processing. 5188 * Instead, just send status back to the initiator. 5189 */ 5190 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5191 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5192 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5193 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5194 io->scsiio.io_cont(io); 5195 return; 5196 } 5197 /* 5198 * Since a configuration write can be done for commands that actually 5199 * have data allocated, like write buffer, and commands that have 5200 * no data, like start/stop unit, we need to check here. 5201 */ 5202 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5203 buf = io->scsiio.kern_data_ptr; 5204 else 5205 buf = NULL; 5206 ctl_done(io); 5207 if (buf) 5208 free(buf, M_CTL); 5209 } 5210 5211 void 5212 ctl_config_read_done(union ctl_io *io) 5213 { 5214 uint8_t *buf; 5215 5216 /* 5217 * If there is some error -- we are done, skip data transfer. 5218 */ 5219 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5220 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5221 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5222 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5223 buf = io->scsiio.kern_data_ptr; 5224 else 5225 buf = NULL; 5226 ctl_done(io); 5227 if (buf) 5228 free(buf, M_CTL); 5229 return; 5230 } 5231 5232 /* 5233 * If the IO_CONT flag is set, we need to call the supplied 5234 * function to continue processing the I/O, instead of completing 5235 * the I/O just yet. 5236 */ 5237 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5238 io->scsiio.io_cont(io); 5239 return; 5240 } 5241 5242 ctl_datamove(io); 5243 } 5244 5245 /* 5246 * SCSI release command. 5247 */ 5248 int 5249 ctl_scsi_release(struct ctl_scsiio *ctsio) 5250 { 5251 struct ctl_lun *lun = CTL_LUN(ctsio); 5252 uint32_t residx; 5253 5254 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5255 5256 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5257 5258 /* 5259 * XXX KDM right now, we only support LUN reservation. We don't 5260 * support 3rd party reservations, or extent reservations, which 5261 * might actually need the parameter list. If we've gotten this 5262 * far, we've got a LUN reservation. Anything else got kicked out 5263 * above. So, according to SPC, ignore the length. 5264 */ 5265 5266 mtx_lock(&lun->lun_lock); 5267 5268 /* 5269 * According to SPC, it is not an error for an intiator to attempt 5270 * to release a reservation on a LUN that isn't reserved, or that 5271 * is reserved by another initiator. The reservation can only be 5272 * released, though, by the initiator who made it or by one of 5273 * several reset type events. 5274 */ 5275 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5276 lun->flags &= ~CTL_LUN_RESERVED; 5277 5278 mtx_unlock(&lun->lun_lock); 5279 5280 ctl_set_success(ctsio); 5281 ctl_done((union ctl_io *)ctsio); 5282 return (CTL_RETVAL_COMPLETE); 5283 } 5284 5285 int 5286 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5287 { 5288 struct ctl_lun *lun = CTL_LUN(ctsio); 5289 uint32_t residx; 5290 5291 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5292 5293 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5294 5295 /* 5296 * XXX KDM right now, we only support LUN reservation. We don't 5297 * support 3rd party reservations, or extent reservations, which 5298 * might actually need the parameter list. If we've gotten this 5299 * far, we've got a LUN reservation. Anything else got kicked out 5300 * above. So, according to SPC, ignore the length. 5301 */ 5302 5303 mtx_lock(&lun->lun_lock); 5304 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5305 ctl_set_reservation_conflict(ctsio); 5306 goto bailout; 5307 } 5308 5309 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5310 if (lun->flags & CTL_LUN_PR_RESERVED) { 5311 ctl_set_success(ctsio); 5312 goto bailout; 5313 } 5314 5315 lun->flags |= CTL_LUN_RESERVED; 5316 lun->res_idx = residx; 5317 ctl_set_success(ctsio); 5318 5319 bailout: 5320 mtx_unlock(&lun->lun_lock); 5321 ctl_done((union ctl_io *)ctsio); 5322 return (CTL_RETVAL_COMPLETE); 5323 } 5324 5325 int 5326 ctl_start_stop(struct ctl_scsiio *ctsio) 5327 { 5328 struct ctl_lun *lun = CTL_LUN(ctsio); 5329 struct scsi_start_stop_unit *cdb; 5330 int retval; 5331 5332 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5333 5334 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5335 5336 if ((cdb->how & SSS_PC_MASK) == 0) { 5337 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5338 (cdb->how & SSS_START) == 0) { 5339 uint32_t residx; 5340 5341 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5342 if (ctl_get_prkey(lun, residx) == 0 || 5343 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5344 ctl_set_reservation_conflict(ctsio); 5345 ctl_done((union ctl_io *)ctsio); 5346 return (CTL_RETVAL_COMPLETE); 5347 } 5348 } 5349 5350 if ((cdb->how & SSS_LOEJ) && 5351 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5352 ctl_set_invalid_field(ctsio, 5353 /*sks_valid*/ 1, 5354 /*command*/ 1, 5355 /*field*/ 4, 5356 /*bit_valid*/ 1, 5357 /*bit*/ 1); 5358 ctl_done((union ctl_io *)ctsio); 5359 return (CTL_RETVAL_COMPLETE); 5360 } 5361 5362 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5363 lun->prevent_count > 0) { 5364 /* "Medium removal prevented" */ 5365 ctl_set_sense(ctsio, /*current_error*/ 1, 5366 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5367 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5368 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5369 ctl_done((union ctl_io *)ctsio); 5370 return (CTL_RETVAL_COMPLETE); 5371 } 5372 } 5373 5374 retval = lun->backend->config_write((union ctl_io *)ctsio); 5375 return (retval); 5376 } 5377 5378 int 5379 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5380 { 5381 struct ctl_lun *lun = CTL_LUN(ctsio); 5382 struct scsi_prevent *cdb; 5383 int retval; 5384 uint32_t initidx; 5385 5386 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5387 5388 cdb = (struct scsi_prevent *)ctsio->cdb; 5389 5390 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5391 ctl_set_invalid_opcode(ctsio); 5392 ctl_done((union ctl_io *)ctsio); 5393 return (CTL_RETVAL_COMPLETE); 5394 } 5395 5396 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5397 mtx_lock(&lun->lun_lock); 5398 if ((cdb->how & PR_PREVENT) && 5399 ctl_is_set(lun->prevent, initidx) == 0) { 5400 ctl_set_mask(lun->prevent, initidx); 5401 lun->prevent_count++; 5402 } else if ((cdb->how & PR_PREVENT) == 0 && 5403 ctl_is_set(lun->prevent, initidx)) { 5404 ctl_clear_mask(lun->prevent, initidx); 5405 lun->prevent_count--; 5406 } 5407 mtx_unlock(&lun->lun_lock); 5408 retval = lun->backend->config_write((union ctl_io *)ctsio); 5409 return (retval); 5410 } 5411 5412 /* 5413 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5414 * we don't really do anything with the LBA and length fields if the user 5415 * passes them in. Instead we'll just flush out the cache for the entire 5416 * LUN. 5417 */ 5418 int 5419 ctl_sync_cache(struct ctl_scsiio *ctsio) 5420 { 5421 struct ctl_lun *lun = CTL_LUN(ctsio); 5422 struct ctl_lba_len_flags *lbalen; 5423 uint64_t starting_lba; 5424 uint32_t block_count; 5425 int retval; 5426 uint8_t byte2; 5427 5428 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5429 5430 retval = 0; 5431 5432 switch (ctsio->cdb[0]) { 5433 case SYNCHRONIZE_CACHE: { 5434 struct scsi_sync_cache *cdb; 5435 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5436 5437 starting_lba = scsi_4btoul(cdb->begin_lba); 5438 block_count = scsi_2btoul(cdb->lb_count); 5439 byte2 = cdb->byte2; 5440 break; 5441 } 5442 case SYNCHRONIZE_CACHE_16: { 5443 struct scsi_sync_cache_16 *cdb; 5444 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5445 5446 starting_lba = scsi_8btou64(cdb->begin_lba); 5447 block_count = scsi_4btoul(cdb->lb_count); 5448 byte2 = cdb->byte2; 5449 break; 5450 } 5451 default: 5452 ctl_set_invalid_opcode(ctsio); 5453 ctl_done((union ctl_io *)ctsio); 5454 goto bailout; 5455 break; /* NOTREACHED */ 5456 } 5457 5458 /* 5459 * We check the LBA and length, but don't do anything with them. 5460 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5461 * get flushed. This check will just help satisfy anyone who wants 5462 * to see an error for an out of range LBA. 5463 */ 5464 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5465 ctl_set_lba_out_of_range(ctsio, 5466 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5467 ctl_done((union ctl_io *)ctsio); 5468 goto bailout; 5469 } 5470 5471 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5472 lbalen->lba = starting_lba; 5473 lbalen->len = block_count; 5474 lbalen->flags = byte2; 5475 retval = lun->backend->config_write((union ctl_io *)ctsio); 5476 5477 bailout: 5478 return (retval); 5479 } 5480 5481 int 5482 ctl_format(struct ctl_scsiio *ctsio) 5483 { 5484 struct scsi_format *cdb; 5485 int length, defect_list_len; 5486 5487 CTL_DEBUG_PRINT(("ctl_format\n")); 5488 5489 cdb = (struct scsi_format *)ctsio->cdb; 5490 5491 length = 0; 5492 if (cdb->byte2 & SF_FMTDATA) { 5493 if (cdb->byte2 & SF_LONGLIST) 5494 length = sizeof(struct scsi_format_header_long); 5495 else 5496 length = sizeof(struct scsi_format_header_short); 5497 } 5498 5499 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5500 && (length > 0)) { 5501 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5502 ctsio->kern_data_len = length; 5503 ctsio->kern_total_len = length; 5504 ctsio->kern_rel_offset = 0; 5505 ctsio->kern_sg_entries = 0; 5506 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5507 ctsio->be_move_done = ctl_config_move_done; 5508 ctl_datamove((union ctl_io *)ctsio); 5509 5510 return (CTL_RETVAL_COMPLETE); 5511 } 5512 5513 defect_list_len = 0; 5514 5515 if (cdb->byte2 & SF_FMTDATA) { 5516 if (cdb->byte2 & SF_LONGLIST) { 5517 struct scsi_format_header_long *header; 5518 5519 header = (struct scsi_format_header_long *) 5520 ctsio->kern_data_ptr; 5521 5522 defect_list_len = scsi_4btoul(header->defect_list_len); 5523 if (defect_list_len != 0) { 5524 ctl_set_invalid_field(ctsio, 5525 /*sks_valid*/ 1, 5526 /*command*/ 0, 5527 /*field*/ 2, 5528 /*bit_valid*/ 0, 5529 /*bit*/ 0); 5530 goto bailout; 5531 } 5532 } else { 5533 struct scsi_format_header_short *header; 5534 5535 header = (struct scsi_format_header_short *) 5536 ctsio->kern_data_ptr; 5537 5538 defect_list_len = scsi_2btoul(header->defect_list_len); 5539 if (defect_list_len != 0) { 5540 ctl_set_invalid_field(ctsio, 5541 /*sks_valid*/ 1, 5542 /*command*/ 0, 5543 /*field*/ 2, 5544 /*bit_valid*/ 0, 5545 /*bit*/ 0); 5546 goto bailout; 5547 } 5548 } 5549 } 5550 5551 ctl_set_success(ctsio); 5552 bailout: 5553 5554 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5555 free(ctsio->kern_data_ptr, M_CTL); 5556 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5557 } 5558 5559 ctl_done((union ctl_io *)ctsio); 5560 return (CTL_RETVAL_COMPLETE); 5561 } 5562 5563 int 5564 ctl_read_buffer(struct ctl_scsiio *ctsio) 5565 { 5566 struct ctl_lun *lun = CTL_LUN(ctsio); 5567 uint64_t buffer_offset; 5568 uint32_t len; 5569 uint8_t byte2; 5570 static uint8_t descr[4]; 5571 static uint8_t echo_descr[4] = { 0 }; 5572 5573 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5574 5575 switch (ctsio->cdb[0]) { 5576 case READ_BUFFER: { 5577 struct scsi_read_buffer *cdb; 5578 5579 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5580 buffer_offset = scsi_3btoul(cdb->offset); 5581 len = scsi_3btoul(cdb->length); 5582 byte2 = cdb->byte2; 5583 break; 5584 } 5585 case READ_BUFFER_16: { 5586 struct scsi_read_buffer_16 *cdb; 5587 5588 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5589 buffer_offset = scsi_8btou64(cdb->offset); 5590 len = scsi_4btoul(cdb->length); 5591 byte2 = cdb->byte2; 5592 break; 5593 } 5594 default: /* This shouldn't happen. */ 5595 ctl_set_invalid_opcode(ctsio); 5596 ctl_done((union ctl_io *)ctsio); 5597 return (CTL_RETVAL_COMPLETE); 5598 } 5599 5600 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5601 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5602 ctl_set_invalid_field(ctsio, 5603 /*sks_valid*/ 1, 5604 /*command*/ 1, 5605 /*field*/ 6, 5606 /*bit_valid*/ 0, 5607 /*bit*/ 0); 5608 ctl_done((union ctl_io *)ctsio); 5609 return (CTL_RETVAL_COMPLETE); 5610 } 5611 5612 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5613 descr[0] = 0; 5614 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5615 ctsio->kern_data_ptr = descr; 5616 len = min(len, sizeof(descr)); 5617 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5618 ctsio->kern_data_ptr = echo_descr; 5619 len = min(len, sizeof(echo_descr)); 5620 } else { 5621 if (lun->write_buffer == NULL) { 5622 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5623 M_CTL, M_WAITOK); 5624 } 5625 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5626 } 5627 ctsio->kern_data_len = len; 5628 ctsio->kern_total_len = len; 5629 ctsio->kern_rel_offset = 0; 5630 ctsio->kern_sg_entries = 0; 5631 ctl_set_success(ctsio); 5632 ctsio->be_move_done = ctl_config_move_done; 5633 ctl_datamove((union ctl_io *)ctsio); 5634 return (CTL_RETVAL_COMPLETE); 5635 } 5636 5637 int 5638 ctl_write_buffer(struct ctl_scsiio *ctsio) 5639 { 5640 struct ctl_lun *lun = CTL_LUN(ctsio); 5641 struct scsi_write_buffer *cdb; 5642 int buffer_offset, len; 5643 5644 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5645 5646 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5647 5648 len = scsi_3btoul(cdb->length); 5649 buffer_offset = scsi_3btoul(cdb->offset); 5650 5651 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5652 ctl_set_invalid_field(ctsio, 5653 /*sks_valid*/ 1, 5654 /*command*/ 1, 5655 /*field*/ 6, 5656 /*bit_valid*/ 0, 5657 /*bit*/ 0); 5658 ctl_done((union ctl_io *)ctsio); 5659 return (CTL_RETVAL_COMPLETE); 5660 } 5661 5662 /* 5663 * If we've got a kernel request that hasn't been malloced yet, 5664 * malloc it and tell the caller the data buffer is here. 5665 */ 5666 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5667 if (lun->write_buffer == NULL) { 5668 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5669 M_CTL, M_WAITOK); 5670 } 5671 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5672 ctsio->kern_data_len = len; 5673 ctsio->kern_total_len = len; 5674 ctsio->kern_rel_offset = 0; 5675 ctsio->kern_sg_entries = 0; 5676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5677 ctsio->be_move_done = ctl_config_move_done; 5678 ctl_datamove((union ctl_io *)ctsio); 5679 5680 return (CTL_RETVAL_COMPLETE); 5681 } 5682 5683 ctl_set_success(ctsio); 5684 ctl_done((union ctl_io *)ctsio); 5685 return (CTL_RETVAL_COMPLETE); 5686 } 5687 5688 static int 5689 ctl_write_same_cont(union ctl_io *io) 5690 { 5691 struct ctl_lun *lun = CTL_LUN(io); 5692 struct ctl_scsiio *ctsio; 5693 struct ctl_lba_len_flags *lbalen; 5694 int retval; 5695 5696 ctsio = &io->scsiio; 5697 ctsio->io_hdr.status = CTL_STATUS_NONE; 5698 lbalen = (struct ctl_lba_len_flags *) 5699 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5700 lbalen->lba += lbalen->len; 5701 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) { 5702 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 5703 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba; 5704 } 5705 5706 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n")); 5707 retval = lun->backend->config_write((union ctl_io *)ctsio); 5708 return (retval); 5709 } 5710 5711 int 5712 ctl_write_same(struct ctl_scsiio *ctsio) 5713 { 5714 struct ctl_lun *lun = CTL_LUN(ctsio); 5715 struct ctl_lba_len_flags *lbalen; 5716 const char *val; 5717 uint64_t lba, ival; 5718 uint32_t num_blocks; 5719 int len, retval; 5720 uint8_t byte2; 5721 5722 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5723 5724 switch (ctsio->cdb[0]) { 5725 case WRITE_SAME_10: { 5726 struct scsi_write_same_10 *cdb; 5727 5728 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5729 5730 lba = scsi_4btoul(cdb->addr); 5731 num_blocks = scsi_2btoul(cdb->length); 5732 byte2 = cdb->byte2; 5733 break; 5734 } 5735 case WRITE_SAME_16: { 5736 struct scsi_write_same_16 *cdb; 5737 5738 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5739 5740 lba = scsi_8btou64(cdb->addr); 5741 num_blocks = scsi_4btoul(cdb->length); 5742 byte2 = cdb->byte2; 5743 break; 5744 } 5745 default: 5746 /* 5747 * We got a command we don't support. This shouldn't 5748 * happen, commands should be filtered out above us. 5749 */ 5750 ctl_set_invalid_opcode(ctsio); 5751 ctl_done((union ctl_io *)ctsio); 5752 5753 return (CTL_RETVAL_COMPLETE); 5754 break; /* NOTREACHED */ 5755 } 5756 5757 /* ANCHOR flag can be used only together with UNMAP */ 5758 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5759 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5760 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5761 ctl_done((union ctl_io *)ctsio); 5762 return (CTL_RETVAL_COMPLETE); 5763 } 5764 5765 /* 5766 * The first check is to make sure we're in bounds, the second 5767 * check is to catch wrap-around problems. If the lba + num blocks 5768 * is less than the lba, then we've wrapped around and the block 5769 * range is invalid anyway. 5770 */ 5771 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5772 || ((lba + num_blocks) < lba)) { 5773 ctl_set_lba_out_of_range(ctsio, 5774 MAX(lba, lun->be_lun->maxlba + 1)); 5775 ctl_done((union ctl_io *)ctsio); 5776 return (CTL_RETVAL_COMPLETE); 5777 } 5778 5779 /* Zero number of blocks means "to the last logical block" */ 5780 if (num_blocks == 0) { 5781 ival = UINT64_MAX; 5782 val = dnvlist_get_string(lun->be_lun->options, 5783 "write_same_max_lba", NULL); 5784 if (val != NULL) 5785 ctl_expand_number(val, &ival); 5786 if ((lun->be_lun->maxlba + 1) - lba > ival) { 5787 ctl_set_invalid_field(ctsio, 5788 /*sks_valid*/ 1, /*command*/ 1, 5789 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10, 5790 /*bit_valid*/ 0, /*bit*/ 0); 5791 ctl_done((union ctl_io *)ctsio); 5792 return (CTL_RETVAL_COMPLETE); 5793 } 5794 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5795 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 5796 ctsio->io_cont = ctl_write_same_cont; 5797 num_blocks = 1 << 31; 5798 } else 5799 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5800 } 5801 5802 len = lun->be_lun->blocksize; 5803 5804 /* 5805 * If we've got a kernel request that hasn't been malloced yet, 5806 * malloc it and tell the caller the data buffer is here. 5807 */ 5808 if ((byte2 & SWS_NDOB) == 0 && 5809 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5810 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5811 ctsio->kern_data_len = len; 5812 ctsio->kern_total_len = len; 5813 ctsio->kern_rel_offset = 0; 5814 ctsio->kern_sg_entries = 0; 5815 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5816 ctsio->be_move_done = ctl_config_move_done; 5817 ctl_datamove((union ctl_io *)ctsio); 5818 5819 return (CTL_RETVAL_COMPLETE); 5820 } 5821 5822 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5823 lbalen->lba = lba; 5824 lbalen->len = num_blocks; 5825 lbalen->flags = byte2; 5826 retval = lun->backend->config_write((union ctl_io *)ctsio); 5827 5828 return (retval); 5829 } 5830 5831 int 5832 ctl_unmap(struct ctl_scsiio *ctsio) 5833 { 5834 struct ctl_lun *lun = CTL_LUN(ctsio); 5835 struct scsi_unmap *cdb; 5836 struct ctl_ptr_len_flags *ptrlen; 5837 struct scsi_unmap_header *hdr; 5838 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5839 uint64_t lba; 5840 uint32_t num_blocks; 5841 int len, retval; 5842 uint8_t byte2; 5843 5844 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5845 5846 cdb = (struct scsi_unmap *)ctsio->cdb; 5847 len = scsi_2btoul(cdb->length); 5848 byte2 = cdb->byte2; 5849 5850 /* 5851 * If we've got a kernel request that hasn't been malloced yet, 5852 * malloc it and tell the caller the data buffer is here. 5853 */ 5854 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5855 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5856 ctsio->kern_data_len = len; 5857 ctsio->kern_total_len = len; 5858 ctsio->kern_rel_offset = 0; 5859 ctsio->kern_sg_entries = 0; 5860 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5861 ctsio->be_move_done = ctl_config_move_done; 5862 ctl_datamove((union ctl_io *)ctsio); 5863 5864 return (CTL_RETVAL_COMPLETE); 5865 } 5866 5867 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5868 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5869 if (len < sizeof (*hdr) || 5870 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5871 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5872 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5873 ctl_set_invalid_field(ctsio, 5874 /*sks_valid*/ 0, 5875 /*command*/ 0, 5876 /*field*/ 0, 5877 /*bit_valid*/ 0, 5878 /*bit*/ 0); 5879 goto done; 5880 } 5881 len = scsi_2btoul(hdr->desc_length); 5882 buf = (struct scsi_unmap_desc *)(hdr + 1); 5883 end = buf + len / sizeof(*buf); 5884 5885 endnz = buf; 5886 for (range = buf; range < end; range++) { 5887 lba = scsi_8btou64(range->lba); 5888 num_blocks = scsi_4btoul(range->length); 5889 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5890 || ((lba + num_blocks) < lba)) { 5891 ctl_set_lba_out_of_range(ctsio, 5892 MAX(lba, lun->be_lun->maxlba + 1)); 5893 ctl_done((union ctl_io *)ctsio); 5894 return (CTL_RETVAL_COMPLETE); 5895 } 5896 if (num_blocks != 0) 5897 endnz = range + 1; 5898 } 5899 5900 /* 5901 * Block backend can not handle zero last range. 5902 * Filter it out and return if there is nothing left. 5903 */ 5904 len = (uint8_t *)endnz - (uint8_t *)buf; 5905 if (len == 0) { 5906 ctl_set_success(ctsio); 5907 goto done; 5908 } 5909 5910 mtx_lock(&lun->lun_lock); 5911 ptrlen = (struct ctl_ptr_len_flags *) 5912 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5913 ptrlen->ptr = (void *)buf; 5914 ptrlen->len = len; 5915 ptrlen->flags = byte2; 5916 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE); 5917 mtx_unlock(&lun->lun_lock); 5918 5919 retval = lun->backend->config_write((union ctl_io *)ctsio); 5920 return (retval); 5921 5922 done: 5923 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5924 free(ctsio->kern_data_ptr, M_CTL); 5925 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5926 } 5927 ctl_done((union ctl_io *)ctsio); 5928 return (CTL_RETVAL_COMPLETE); 5929 } 5930 5931 int 5932 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5933 struct ctl_page_index *page_index, uint8_t *page_ptr) 5934 { 5935 struct ctl_lun *lun = CTL_LUN(ctsio); 5936 uint8_t *current_cp; 5937 int set_ua; 5938 uint32_t initidx; 5939 5940 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5941 set_ua = 0; 5942 5943 current_cp = (page_index->page_data + (page_index->page_len * 5944 CTL_PAGE_CURRENT)); 5945 5946 mtx_lock(&lun->lun_lock); 5947 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5948 memcpy(current_cp, page_ptr, page_index->page_len); 5949 set_ua = 1; 5950 } 5951 if (set_ua != 0) 5952 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5953 mtx_unlock(&lun->lun_lock); 5954 if (set_ua) { 5955 ctl_isc_announce_mode(lun, 5956 ctl_get_initindex(&ctsio->io_hdr.nexus), 5957 page_index->page_code, page_index->subpage); 5958 } 5959 return (CTL_RETVAL_COMPLETE); 5960 } 5961 5962 static void 5963 ctl_ie_timer(void *arg) 5964 { 5965 struct ctl_lun *lun = arg; 5966 uint64_t t; 5967 5968 if (lun->ie_asc == 0) 5969 return; 5970 5971 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5972 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5973 else 5974 lun->ie_reported = 0; 5975 5976 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5977 lun->ie_reportcnt++; 5978 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5979 if (t == 0 || t == UINT32_MAX) 5980 t = 3000; /* 5 min */ 5981 callout_schedule_sbt(&lun->ie_callout, SBT_1S / 10 * t, 5982 SBT_1S / 10, 0); 5983 } 5984 } 5985 5986 int 5987 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5988 struct ctl_page_index *page_index, uint8_t *page_ptr) 5989 { 5990 struct ctl_lun *lun = CTL_LUN(ctsio); 5991 struct scsi_info_exceptions_page *pg; 5992 uint64_t t; 5993 5994 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5995 5996 pg = (struct scsi_info_exceptions_page *)page_ptr; 5997 mtx_lock(&lun->lun_lock); 5998 if (pg->info_flags & SIEP_FLAGS_TEST) { 5999 lun->ie_asc = 0x5d; 6000 lun->ie_ascq = 0xff; 6001 if (pg->mrie == SIEP_MRIE_UA) { 6002 ctl_est_ua_all(lun, -1, CTL_UA_IE); 6003 lun->ie_reported = 1; 6004 } else { 6005 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 6006 lun->ie_reported = -1; 6007 } 6008 lun->ie_reportcnt = 1; 6009 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 6010 lun->ie_reportcnt++; 6011 t = scsi_4btoul(pg->interval_timer); 6012 if (t == 0 || t == UINT32_MAX) 6013 t = 3000; /* 5 min */ 6014 callout_reset_sbt(&lun->ie_callout, SBT_1S / 10 * t, 6015 SBT_1S / 10, ctl_ie_timer, lun, 0); 6016 } 6017 } else { 6018 lun->ie_asc = 0; 6019 lun->ie_ascq = 0; 6020 lun->ie_reported = 1; 6021 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 6022 lun->ie_reportcnt = UINT32_MAX; 6023 callout_stop(&lun->ie_callout); 6024 } 6025 mtx_unlock(&lun->lun_lock); 6026 return (CTL_RETVAL_COMPLETE); 6027 } 6028 6029 static int 6030 ctl_do_mode_select(union ctl_io *io) 6031 { 6032 struct ctl_lun *lun = CTL_LUN(io); 6033 struct scsi_mode_page_header *page_header; 6034 struct ctl_page_index *page_index; 6035 struct ctl_scsiio *ctsio; 6036 int page_len, page_len_offset, page_len_size; 6037 union ctl_modepage_info *modepage_info; 6038 uint16_t *len_left, *len_used; 6039 int retval, i; 6040 6041 ctsio = &io->scsiio; 6042 page_index = NULL; 6043 page_len = 0; 6044 6045 modepage_info = (union ctl_modepage_info *) 6046 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6047 len_left = &modepage_info->header.len_left; 6048 len_used = &modepage_info->header.len_used; 6049 6050 do_next_page: 6051 6052 page_header = (struct scsi_mode_page_header *) 6053 (ctsio->kern_data_ptr + *len_used); 6054 6055 if (*len_left == 0) { 6056 free(ctsio->kern_data_ptr, M_CTL); 6057 ctl_set_success(ctsio); 6058 ctl_done((union ctl_io *)ctsio); 6059 return (CTL_RETVAL_COMPLETE); 6060 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6061 free(ctsio->kern_data_ptr, M_CTL); 6062 ctl_set_param_len_error(ctsio); 6063 ctl_done((union ctl_io *)ctsio); 6064 return (CTL_RETVAL_COMPLETE); 6065 6066 } else if ((page_header->page_code & SMPH_SPF) 6067 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6068 free(ctsio->kern_data_ptr, M_CTL); 6069 ctl_set_param_len_error(ctsio); 6070 ctl_done((union ctl_io *)ctsio); 6071 return (CTL_RETVAL_COMPLETE); 6072 } 6073 6074 /* 6075 * XXX KDM should we do something with the block descriptor? 6076 */ 6077 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6078 page_index = &lun->mode_pages.index[i]; 6079 if (lun->be_lun->lun_type == T_DIRECT && 6080 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6081 continue; 6082 if (lun->be_lun->lun_type == T_PROCESSOR && 6083 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6084 continue; 6085 if (lun->be_lun->lun_type == T_CDROM && 6086 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6087 continue; 6088 6089 if ((page_index->page_code & SMPH_PC_MASK) != 6090 (page_header->page_code & SMPH_PC_MASK)) 6091 continue; 6092 6093 /* 6094 * If neither page has a subpage code, then we've got a 6095 * match. 6096 */ 6097 if (((page_index->page_code & SMPH_SPF) == 0) 6098 && ((page_header->page_code & SMPH_SPF) == 0)) { 6099 page_len = page_header->page_length; 6100 break; 6101 } 6102 6103 /* 6104 * If both pages have subpages, then the subpage numbers 6105 * have to match. 6106 */ 6107 if ((page_index->page_code & SMPH_SPF) 6108 && (page_header->page_code & SMPH_SPF)) { 6109 struct scsi_mode_page_header_sp *sph; 6110 6111 sph = (struct scsi_mode_page_header_sp *)page_header; 6112 if (page_index->subpage == sph->subpage) { 6113 page_len = scsi_2btoul(sph->page_length); 6114 break; 6115 } 6116 } 6117 } 6118 6119 /* 6120 * If we couldn't find the page, or if we don't have a mode select 6121 * handler for it, send back an error to the user. 6122 */ 6123 if ((i >= CTL_NUM_MODE_PAGES) 6124 || (page_index->select_handler == NULL)) { 6125 ctl_set_invalid_field(ctsio, 6126 /*sks_valid*/ 1, 6127 /*command*/ 0, 6128 /*field*/ *len_used, 6129 /*bit_valid*/ 0, 6130 /*bit*/ 0); 6131 free(ctsio->kern_data_ptr, M_CTL); 6132 ctl_done((union ctl_io *)ctsio); 6133 return (CTL_RETVAL_COMPLETE); 6134 } 6135 6136 if (page_index->page_code & SMPH_SPF) { 6137 page_len_offset = 2; 6138 page_len_size = 2; 6139 } else { 6140 page_len_size = 1; 6141 page_len_offset = 1; 6142 } 6143 6144 /* 6145 * If the length the initiator gives us isn't the one we specify in 6146 * the mode page header, or if they didn't specify enough data in 6147 * the CDB to avoid truncating this page, kick out the request. 6148 */ 6149 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6150 ctl_set_invalid_field(ctsio, 6151 /*sks_valid*/ 1, 6152 /*command*/ 0, 6153 /*field*/ *len_used + page_len_offset, 6154 /*bit_valid*/ 0, 6155 /*bit*/ 0); 6156 free(ctsio->kern_data_ptr, M_CTL); 6157 ctl_done((union ctl_io *)ctsio); 6158 return (CTL_RETVAL_COMPLETE); 6159 } 6160 if (*len_left < page_index->page_len) { 6161 free(ctsio->kern_data_ptr, M_CTL); 6162 ctl_set_param_len_error(ctsio); 6163 ctl_done((union ctl_io *)ctsio); 6164 return (CTL_RETVAL_COMPLETE); 6165 } 6166 6167 /* 6168 * Run through the mode page, checking to make sure that the bits 6169 * the user changed are actually legal for him to change. 6170 */ 6171 for (i = 0; i < page_index->page_len; i++) { 6172 uint8_t *user_byte, *change_mask, *current_byte; 6173 int bad_bit; 6174 int j; 6175 6176 user_byte = (uint8_t *)page_header + i; 6177 change_mask = page_index->page_data + 6178 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6179 current_byte = page_index->page_data + 6180 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6181 6182 /* 6183 * Check to see whether the user set any bits in this byte 6184 * that he is not allowed to set. 6185 */ 6186 if ((*user_byte & ~(*change_mask)) == 6187 (*current_byte & ~(*change_mask))) 6188 continue; 6189 6190 /* 6191 * Go through bit by bit to determine which one is illegal. 6192 */ 6193 bad_bit = 0; 6194 for (j = 7; j >= 0; j--) { 6195 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6196 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6197 bad_bit = i; 6198 break; 6199 } 6200 } 6201 ctl_set_invalid_field(ctsio, 6202 /*sks_valid*/ 1, 6203 /*command*/ 0, 6204 /*field*/ *len_used + i, 6205 /*bit_valid*/ 1, 6206 /*bit*/ bad_bit); 6207 free(ctsio->kern_data_ptr, M_CTL); 6208 ctl_done((union ctl_io *)ctsio); 6209 return (CTL_RETVAL_COMPLETE); 6210 } 6211 6212 /* 6213 * Decrement these before we call the page handler, since we may 6214 * end up getting called back one way or another before the handler 6215 * returns to this context. 6216 */ 6217 *len_left -= page_index->page_len; 6218 *len_used += page_index->page_len; 6219 6220 retval = page_index->select_handler(ctsio, page_index, 6221 (uint8_t *)page_header); 6222 6223 /* 6224 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6225 * wait until this queued command completes to finish processing 6226 * the mode page. If it returns anything other than 6227 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6228 * already set the sense information, freed the data pointer, and 6229 * completed the io for us. 6230 */ 6231 if (retval != CTL_RETVAL_COMPLETE) 6232 goto bailout_no_done; 6233 6234 /* 6235 * If the initiator sent us more than one page, parse the next one. 6236 */ 6237 if (*len_left > 0) 6238 goto do_next_page; 6239 6240 ctl_set_success(ctsio); 6241 free(ctsio->kern_data_ptr, M_CTL); 6242 ctl_done((union ctl_io *)ctsio); 6243 6244 bailout_no_done: 6245 6246 return (CTL_RETVAL_COMPLETE); 6247 6248 } 6249 6250 int 6251 ctl_mode_select(struct ctl_scsiio *ctsio) 6252 { 6253 struct ctl_lun *lun = CTL_LUN(ctsio); 6254 union ctl_modepage_info *modepage_info; 6255 int bd_len, i, header_size, param_len, rtd; 6256 uint32_t initidx; 6257 6258 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6259 switch (ctsio->cdb[0]) { 6260 case MODE_SELECT_6: { 6261 struct scsi_mode_select_6 *cdb; 6262 6263 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6264 6265 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6266 param_len = cdb->length; 6267 header_size = sizeof(struct scsi_mode_header_6); 6268 break; 6269 } 6270 case MODE_SELECT_10: { 6271 struct scsi_mode_select_10 *cdb; 6272 6273 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6274 6275 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6276 param_len = scsi_2btoul(cdb->length); 6277 header_size = sizeof(struct scsi_mode_header_10); 6278 break; 6279 } 6280 default: 6281 ctl_set_invalid_opcode(ctsio); 6282 ctl_done((union ctl_io *)ctsio); 6283 return (CTL_RETVAL_COMPLETE); 6284 } 6285 6286 if (rtd) { 6287 if (param_len != 0) { 6288 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6289 /*command*/ 1, /*field*/ 0, 6290 /*bit_valid*/ 0, /*bit*/ 0); 6291 ctl_done((union ctl_io *)ctsio); 6292 return (CTL_RETVAL_COMPLETE); 6293 } 6294 6295 /* Revert to defaults. */ 6296 ctl_init_page_index(lun); 6297 mtx_lock(&lun->lun_lock); 6298 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6299 mtx_unlock(&lun->lun_lock); 6300 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6301 ctl_isc_announce_mode(lun, -1, 6302 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6303 lun->mode_pages.index[i].subpage); 6304 } 6305 ctl_set_success(ctsio); 6306 ctl_done((union ctl_io *)ctsio); 6307 return (CTL_RETVAL_COMPLETE); 6308 } 6309 6310 /* 6311 * From SPC-3: 6312 * "A parameter list length of zero indicates that the Data-Out Buffer 6313 * shall be empty. This condition shall not be considered as an error." 6314 */ 6315 if (param_len == 0) { 6316 ctl_set_success(ctsio); 6317 ctl_done((union ctl_io *)ctsio); 6318 return (CTL_RETVAL_COMPLETE); 6319 } 6320 6321 /* 6322 * Since we'll hit this the first time through, prior to 6323 * allocation, we don't need to free a data buffer here. 6324 */ 6325 if (param_len < header_size) { 6326 ctl_set_param_len_error(ctsio); 6327 ctl_done((union ctl_io *)ctsio); 6328 return (CTL_RETVAL_COMPLETE); 6329 } 6330 6331 /* 6332 * Allocate the data buffer and grab the user's data. In theory, 6333 * we shouldn't have to sanity check the parameter list length here 6334 * because the maximum size is 64K. We should be able to malloc 6335 * that much without too many problems. 6336 */ 6337 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6338 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6339 ctsio->kern_data_len = param_len; 6340 ctsio->kern_total_len = param_len; 6341 ctsio->kern_rel_offset = 0; 6342 ctsio->kern_sg_entries = 0; 6343 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6344 ctsio->be_move_done = ctl_config_move_done; 6345 ctl_datamove((union ctl_io *)ctsio); 6346 6347 return (CTL_RETVAL_COMPLETE); 6348 } 6349 6350 switch (ctsio->cdb[0]) { 6351 case MODE_SELECT_6: { 6352 struct scsi_mode_header_6 *mh6; 6353 6354 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6355 bd_len = mh6->blk_desc_len; 6356 break; 6357 } 6358 case MODE_SELECT_10: { 6359 struct scsi_mode_header_10 *mh10; 6360 6361 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6362 bd_len = scsi_2btoul(mh10->blk_desc_len); 6363 break; 6364 } 6365 default: 6366 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6367 } 6368 6369 if (param_len < (header_size + bd_len)) { 6370 free(ctsio->kern_data_ptr, M_CTL); 6371 ctl_set_param_len_error(ctsio); 6372 ctl_done((union ctl_io *)ctsio); 6373 return (CTL_RETVAL_COMPLETE); 6374 } 6375 6376 /* 6377 * Set the IO_CONT flag, so that if this I/O gets passed to 6378 * ctl_config_write_done(), it'll get passed back to 6379 * ctl_do_mode_select() for further processing, or completion if 6380 * we're all done. 6381 */ 6382 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6383 ctsio->io_cont = ctl_do_mode_select; 6384 6385 modepage_info = (union ctl_modepage_info *) 6386 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6387 memset(modepage_info, 0, sizeof(*modepage_info)); 6388 modepage_info->header.len_left = param_len - header_size - bd_len; 6389 modepage_info->header.len_used = header_size + bd_len; 6390 6391 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6392 } 6393 6394 int 6395 ctl_mode_sense(struct ctl_scsiio *ctsio) 6396 { 6397 struct ctl_lun *lun = CTL_LUN(ctsio); 6398 int pc, page_code, llba, subpage; 6399 int alloc_len, page_len, header_len, bd_len, total_len; 6400 void *block_desc; 6401 struct ctl_page_index *page_index; 6402 6403 llba = 0; 6404 6405 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6406 6407 switch (ctsio->cdb[0]) { 6408 case MODE_SENSE_6: { 6409 struct scsi_mode_sense_6 *cdb; 6410 6411 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6412 6413 header_len = sizeof(struct scsi_mode_hdr_6); 6414 if (cdb->byte2 & SMS_DBD) 6415 bd_len = 0; 6416 else 6417 bd_len = sizeof(struct scsi_mode_block_descr); 6418 header_len += bd_len; 6419 6420 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6421 page_code = cdb->page & SMS_PAGE_CODE; 6422 subpage = cdb->subpage; 6423 alloc_len = cdb->length; 6424 break; 6425 } 6426 case MODE_SENSE_10: { 6427 struct scsi_mode_sense_10 *cdb; 6428 6429 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6430 6431 header_len = sizeof(struct scsi_mode_hdr_10); 6432 if (cdb->byte2 & SMS_DBD) { 6433 bd_len = 0; 6434 } else if (lun->be_lun->lun_type == T_DIRECT) { 6435 if (cdb->byte2 & SMS10_LLBAA) { 6436 llba = 1; 6437 bd_len = sizeof(struct scsi_mode_block_descr_dlong); 6438 } else 6439 bd_len = sizeof(struct scsi_mode_block_descr_dshort); 6440 } else 6441 bd_len = sizeof(struct scsi_mode_block_descr); 6442 header_len += bd_len; 6443 6444 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6445 page_code = cdb->page & SMS_PAGE_CODE; 6446 subpage = cdb->subpage; 6447 alloc_len = scsi_2btoul(cdb->length); 6448 break; 6449 } 6450 default: 6451 ctl_set_invalid_opcode(ctsio); 6452 ctl_done((union ctl_io *)ctsio); 6453 return (CTL_RETVAL_COMPLETE); 6454 break; /* NOTREACHED */ 6455 } 6456 6457 /* 6458 * We have to make a first pass through to calculate the size of 6459 * the pages that match the user's query. Then we allocate enough 6460 * memory to hold it, and actually copy the data into the buffer. 6461 */ 6462 switch (page_code) { 6463 case SMS_ALL_PAGES_PAGE: { 6464 u_int i; 6465 6466 page_len = 0; 6467 6468 /* 6469 * At the moment, values other than 0 and 0xff here are 6470 * reserved according to SPC-3. 6471 */ 6472 if ((subpage != SMS_SUBPAGE_PAGE_0) 6473 && (subpage != SMS_SUBPAGE_ALL)) { 6474 ctl_set_invalid_field(ctsio, 6475 /*sks_valid*/ 1, 6476 /*command*/ 1, 6477 /*field*/ 3, 6478 /*bit_valid*/ 0, 6479 /*bit*/ 0); 6480 ctl_done((union ctl_io *)ctsio); 6481 return (CTL_RETVAL_COMPLETE); 6482 } 6483 6484 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6485 page_index = &lun->mode_pages.index[i]; 6486 6487 /* Make sure the page is supported for this dev type */ 6488 if (lun->be_lun->lun_type == T_DIRECT && 6489 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6490 continue; 6491 if (lun->be_lun->lun_type == T_PROCESSOR && 6492 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6493 continue; 6494 if (lun->be_lun->lun_type == T_CDROM && 6495 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6496 continue; 6497 6498 /* 6499 * We don't use this subpage if the user didn't 6500 * request all subpages. 6501 */ 6502 if ((page_index->subpage != 0) 6503 && (subpage == SMS_SUBPAGE_PAGE_0)) 6504 continue; 6505 6506 page_len += page_index->page_len; 6507 } 6508 break; 6509 } 6510 default: { 6511 u_int i; 6512 6513 page_len = 0; 6514 6515 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6516 page_index = &lun->mode_pages.index[i]; 6517 6518 /* Make sure the page is supported for this dev type */ 6519 if (lun->be_lun->lun_type == T_DIRECT && 6520 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6521 continue; 6522 if (lun->be_lun->lun_type == T_PROCESSOR && 6523 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6524 continue; 6525 if (lun->be_lun->lun_type == T_CDROM && 6526 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6527 continue; 6528 6529 /* Look for the right page code */ 6530 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6531 continue; 6532 6533 /* Look for the right subpage or the subpage wildcard*/ 6534 if ((page_index->subpage != subpage) 6535 && (subpage != SMS_SUBPAGE_ALL)) 6536 continue; 6537 6538 page_len += page_index->page_len; 6539 } 6540 6541 if (page_len == 0) { 6542 ctl_set_invalid_field(ctsio, 6543 /*sks_valid*/ 1, 6544 /*command*/ 1, 6545 /*field*/ 2, 6546 /*bit_valid*/ 1, 6547 /*bit*/ 5); 6548 ctl_done((union ctl_io *)ctsio); 6549 return (CTL_RETVAL_COMPLETE); 6550 } 6551 break; 6552 } 6553 } 6554 6555 total_len = header_len + page_len; 6556 6557 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6558 ctsio->kern_sg_entries = 0; 6559 ctsio->kern_rel_offset = 0; 6560 ctsio->kern_data_len = min(total_len, alloc_len); 6561 ctsio->kern_total_len = ctsio->kern_data_len; 6562 6563 switch (ctsio->cdb[0]) { 6564 case MODE_SENSE_6: { 6565 struct scsi_mode_hdr_6 *header; 6566 6567 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6568 6569 header->datalen = MIN(total_len - 1, 254); 6570 if (lun->be_lun->lun_type == T_DIRECT) { 6571 header->dev_specific = 0x10; /* DPOFUA */ 6572 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6573 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6574 header->dev_specific |= 0x80; /* WP */ 6575 } 6576 header->block_descr_len = bd_len; 6577 block_desc = &header[1]; 6578 break; 6579 } 6580 case MODE_SENSE_10: { 6581 struct scsi_mode_hdr_10 *header; 6582 int datalen; 6583 6584 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6585 6586 datalen = MIN(total_len - 2, 65533); 6587 scsi_ulto2b(datalen, header->datalen); 6588 if (lun->be_lun->lun_type == T_DIRECT) { 6589 header->dev_specific = 0x10; /* DPOFUA */ 6590 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6591 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6592 header->dev_specific |= 0x80; /* WP */ 6593 } 6594 if (llba) 6595 header->flags |= SMH_LONGLBA; 6596 scsi_ulto2b(bd_len, header->block_descr_len); 6597 block_desc = &header[1]; 6598 break; 6599 } 6600 default: 6601 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6602 } 6603 6604 /* 6605 * If we've got a disk, use its blocksize in the block 6606 * descriptor. Otherwise, just set it to 0. 6607 */ 6608 if (bd_len > 0) { 6609 if (lun->be_lun->lun_type == T_DIRECT) { 6610 if (llba) { 6611 struct scsi_mode_block_descr_dlong *bd = block_desc; 6612 if (lun->be_lun->maxlba != 0) 6613 scsi_u64to8b(lun->be_lun->maxlba + 1, 6614 bd->num_blocks); 6615 scsi_ulto4b(lun->be_lun->blocksize, 6616 bd->block_len); 6617 } else { 6618 struct scsi_mode_block_descr_dshort *bd = block_desc; 6619 if (lun->be_lun->maxlba != 0) 6620 scsi_ulto4b(MIN(lun->be_lun->maxlba+1, 6621 UINT32_MAX), bd->num_blocks); 6622 scsi_ulto3b(lun->be_lun->blocksize, 6623 bd->block_len); 6624 } 6625 } else { 6626 struct scsi_mode_block_descr *bd = block_desc; 6627 scsi_ulto3b(0, bd->block_len); 6628 } 6629 } 6630 6631 switch (page_code) { 6632 case SMS_ALL_PAGES_PAGE: { 6633 int i, data_used; 6634 6635 data_used = header_len; 6636 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6637 struct ctl_page_index *page_index; 6638 6639 page_index = &lun->mode_pages.index[i]; 6640 if (lun->be_lun->lun_type == T_DIRECT && 6641 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6642 continue; 6643 if (lun->be_lun->lun_type == T_PROCESSOR && 6644 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6645 continue; 6646 if (lun->be_lun->lun_type == T_CDROM && 6647 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6648 continue; 6649 6650 /* 6651 * We don't use this subpage if the user didn't 6652 * request all subpages. We already checked (above) 6653 * to make sure the user only specified a subpage 6654 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6655 */ 6656 if ((page_index->subpage != 0) 6657 && (subpage == SMS_SUBPAGE_PAGE_0)) 6658 continue; 6659 6660 /* 6661 * Call the handler, if it exists, to update the 6662 * page to the latest values. 6663 */ 6664 if (page_index->sense_handler != NULL) 6665 page_index->sense_handler(ctsio, page_index,pc); 6666 6667 memcpy(ctsio->kern_data_ptr + data_used, 6668 page_index->page_data + 6669 (page_index->page_len * pc), 6670 page_index->page_len); 6671 data_used += page_index->page_len; 6672 } 6673 break; 6674 } 6675 default: { 6676 int i, data_used; 6677 6678 data_used = header_len; 6679 6680 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6681 struct ctl_page_index *page_index; 6682 6683 page_index = &lun->mode_pages.index[i]; 6684 6685 /* Look for the right page code */ 6686 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6687 continue; 6688 6689 /* Look for the right subpage or the subpage wildcard*/ 6690 if ((page_index->subpage != subpage) 6691 && (subpage != SMS_SUBPAGE_ALL)) 6692 continue; 6693 6694 /* Make sure the page is supported for this dev type */ 6695 if (lun->be_lun->lun_type == T_DIRECT && 6696 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6697 continue; 6698 if (lun->be_lun->lun_type == T_PROCESSOR && 6699 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6700 continue; 6701 if (lun->be_lun->lun_type == T_CDROM && 6702 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6703 continue; 6704 6705 /* 6706 * Call the handler, if it exists, to update the 6707 * page to the latest values. 6708 */ 6709 if (page_index->sense_handler != NULL) 6710 page_index->sense_handler(ctsio, page_index,pc); 6711 6712 memcpy(ctsio->kern_data_ptr + data_used, 6713 page_index->page_data + 6714 (page_index->page_len * pc), 6715 page_index->page_len); 6716 data_used += page_index->page_len; 6717 } 6718 break; 6719 } 6720 } 6721 6722 ctl_set_success(ctsio); 6723 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6724 ctsio->be_move_done = ctl_config_move_done; 6725 ctl_datamove((union ctl_io *)ctsio); 6726 return (CTL_RETVAL_COMPLETE); 6727 } 6728 6729 int 6730 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio, 6731 struct ctl_page_index *page_index, 6732 int pc) 6733 { 6734 struct ctl_lun *lun = CTL_LUN(ctsio); 6735 struct scsi_log_temperature *data; 6736 const char *value; 6737 6738 data = (struct scsi_log_temperature *)page_index->page_data; 6739 6740 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code); 6741 data->hdr.param_control = SLP_LBIN; 6742 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6743 sizeof(struct scsi_log_param_header); 6744 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6745 NULL)) != NULL) 6746 data->temperature = strtol(value, NULL, 0); 6747 else 6748 data->temperature = 0xff; 6749 data++; 6750 6751 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code); 6752 data->hdr.param_control = SLP_LBIN; 6753 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6754 sizeof(struct scsi_log_param_header); 6755 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature", 6756 NULL)) != NULL) 6757 data->temperature = strtol(value, NULL, 0); 6758 else 6759 data->temperature = 0xff; 6760 return (0); 6761 } 6762 6763 int 6764 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6765 struct ctl_page_index *page_index, 6766 int pc) 6767 { 6768 struct ctl_lun *lun = CTL_LUN(ctsio); 6769 struct scsi_log_param_header *phdr; 6770 uint8_t *data; 6771 uint64_t val; 6772 6773 data = page_index->page_data; 6774 6775 if (lun->backend->lun_attr != NULL && 6776 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail")) 6777 != UINT64_MAX) { 6778 phdr = (struct scsi_log_param_header *)data; 6779 scsi_ulto2b(0x0001, phdr->param_code); 6780 phdr->param_control = SLP_LBIN | SLP_LP; 6781 phdr->param_len = 8; 6782 data = (uint8_t *)(phdr + 1); 6783 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6784 data[4] = 0x02; /* per-pool */ 6785 data += phdr->param_len; 6786 } 6787 6788 if (lun->backend->lun_attr != NULL && 6789 (val = lun->backend->lun_attr(lun->be_lun, "blocksused")) 6790 != UINT64_MAX) { 6791 phdr = (struct scsi_log_param_header *)data; 6792 scsi_ulto2b(0x0002, phdr->param_code); 6793 phdr->param_control = SLP_LBIN | SLP_LP; 6794 phdr->param_len = 8; 6795 data = (uint8_t *)(phdr + 1); 6796 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6797 data[4] = 0x01; /* per-LUN */ 6798 data += phdr->param_len; 6799 } 6800 6801 if (lun->backend->lun_attr != NULL && 6802 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail")) 6803 != UINT64_MAX) { 6804 phdr = (struct scsi_log_param_header *)data; 6805 scsi_ulto2b(0x00f1, phdr->param_code); 6806 phdr->param_control = SLP_LBIN | SLP_LP; 6807 phdr->param_len = 8; 6808 data = (uint8_t *)(phdr + 1); 6809 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6810 data[4] = 0x02; /* per-pool */ 6811 data += phdr->param_len; 6812 } 6813 6814 if (lun->backend->lun_attr != NULL && 6815 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused")) 6816 != UINT64_MAX) { 6817 phdr = (struct scsi_log_param_header *)data; 6818 scsi_ulto2b(0x00f2, phdr->param_code); 6819 phdr->param_control = SLP_LBIN | SLP_LP; 6820 phdr->param_len = 8; 6821 data = (uint8_t *)(phdr + 1); 6822 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6823 data[4] = 0x02; /* per-pool */ 6824 data += phdr->param_len; 6825 } 6826 6827 page_index->page_len = data - page_index->page_data; 6828 return (0); 6829 } 6830 6831 int 6832 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6833 struct ctl_page_index *page_index, 6834 int pc) 6835 { 6836 struct ctl_lun *lun = CTL_LUN(ctsio); 6837 struct stat_page *data; 6838 struct bintime *t; 6839 6840 data = (struct stat_page *)page_index->page_data; 6841 6842 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6843 data->sap.hdr.param_control = SLP_LBIN; 6844 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6845 sizeof(struct scsi_log_param_header); 6846 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6847 data->sap.read_num); 6848 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6849 data->sap.write_num); 6850 if (lun->be_lun->blocksize > 0) { 6851 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6852 lun->be_lun->blocksize, data->sap.recvieved_lba); 6853 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6854 lun->be_lun->blocksize, data->sap.transmitted_lba); 6855 } 6856 t = &lun->stats.time[CTL_STATS_READ]; 6857 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6858 data->sap.read_int); 6859 t = &lun->stats.time[CTL_STATS_WRITE]; 6860 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6861 data->sap.write_int); 6862 scsi_u64to8b(0, data->sap.weighted_num); 6863 scsi_u64to8b(0, data->sap.weighted_int); 6864 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6865 data->it.hdr.param_control = SLP_LBIN; 6866 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6867 sizeof(struct scsi_log_param_header); 6868 #ifdef CTL_TIME_IO 6869 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6870 #endif 6871 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6872 data->it.hdr.param_control = SLP_LBIN; 6873 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6874 sizeof(struct scsi_log_param_header); 6875 scsi_ulto4b(3, data->ti.exponent); 6876 scsi_ulto4b(1, data->ti.integer); 6877 return (0); 6878 } 6879 6880 int 6881 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6882 struct ctl_page_index *page_index, 6883 int pc) 6884 { 6885 struct ctl_lun *lun = CTL_LUN(ctsio); 6886 struct scsi_log_informational_exceptions *data; 6887 const char *value; 6888 6889 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6890 6891 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6892 data->hdr.param_control = SLP_LBIN; 6893 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6894 sizeof(struct scsi_log_param_header); 6895 data->ie_asc = lun->ie_asc; 6896 data->ie_ascq = lun->ie_ascq; 6897 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6898 NULL)) != NULL) 6899 data->temperature = strtol(value, NULL, 0); 6900 else 6901 data->temperature = 0xff; 6902 return (0); 6903 } 6904 6905 int 6906 ctl_log_sense(struct ctl_scsiio *ctsio) 6907 { 6908 struct ctl_lun *lun = CTL_LUN(ctsio); 6909 int i, pc, page_code, subpage; 6910 int alloc_len, total_len; 6911 struct ctl_page_index *page_index; 6912 struct scsi_log_sense *cdb; 6913 struct scsi_log_header *header; 6914 6915 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6916 6917 cdb = (struct scsi_log_sense *)ctsio->cdb; 6918 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6919 page_code = cdb->page & SLS_PAGE_CODE; 6920 subpage = cdb->subpage; 6921 alloc_len = scsi_2btoul(cdb->length); 6922 6923 page_index = NULL; 6924 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6925 page_index = &lun->log_pages.index[i]; 6926 6927 /* Look for the right page code */ 6928 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6929 continue; 6930 6931 /* Look for the right subpage or the subpage wildcard*/ 6932 if (page_index->subpage != subpage) 6933 continue; 6934 6935 break; 6936 } 6937 if (i >= CTL_NUM_LOG_PAGES) { 6938 ctl_set_invalid_field(ctsio, 6939 /*sks_valid*/ 1, 6940 /*command*/ 1, 6941 /*field*/ 2, 6942 /*bit_valid*/ 0, 6943 /*bit*/ 0); 6944 ctl_done((union ctl_io *)ctsio); 6945 return (CTL_RETVAL_COMPLETE); 6946 } 6947 6948 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6949 6950 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6951 ctsio->kern_sg_entries = 0; 6952 ctsio->kern_rel_offset = 0; 6953 ctsio->kern_data_len = min(total_len, alloc_len); 6954 ctsio->kern_total_len = ctsio->kern_data_len; 6955 6956 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6957 header->page = page_index->page_code; 6958 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6959 header->page |= SL_DS; 6960 if (page_index->subpage) { 6961 header->page |= SL_SPF; 6962 header->subpage = page_index->subpage; 6963 } 6964 scsi_ulto2b(page_index->page_len, header->datalen); 6965 6966 /* 6967 * Call the handler, if it exists, to update the 6968 * page to the latest values. 6969 */ 6970 if (page_index->sense_handler != NULL) 6971 page_index->sense_handler(ctsio, page_index, pc); 6972 6973 memcpy(header + 1, page_index->page_data, page_index->page_len); 6974 6975 ctl_set_success(ctsio); 6976 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6977 ctsio->be_move_done = ctl_config_move_done; 6978 ctl_datamove((union ctl_io *)ctsio); 6979 return (CTL_RETVAL_COMPLETE); 6980 } 6981 6982 int 6983 ctl_read_capacity(struct ctl_scsiio *ctsio) 6984 { 6985 struct ctl_lun *lun = CTL_LUN(ctsio); 6986 struct scsi_read_capacity *cdb; 6987 struct scsi_read_capacity_data *data; 6988 uint32_t lba; 6989 6990 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6991 6992 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6993 6994 lba = scsi_4btoul(cdb->addr); 6995 if (((cdb->pmi & SRC_PMI) == 0) 6996 && (lba != 0)) { 6997 ctl_set_invalid_field(/*ctsio*/ ctsio, 6998 /*sks_valid*/ 1, 6999 /*command*/ 1, 7000 /*field*/ 2, 7001 /*bit_valid*/ 0, 7002 /*bit*/ 0); 7003 ctl_done((union ctl_io *)ctsio); 7004 return (CTL_RETVAL_COMPLETE); 7005 } 7006 7007 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7008 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7009 ctsio->kern_data_len = sizeof(*data); 7010 ctsio->kern_total_len = sizeof(*data); 7011 ctsio->kern_rel_offset = 0; 7012 ctsio->kern_sg_entries = 0; 7013 7014 /* 7015 * If the maximum LBA is greater than 0xfffffffe, the user must 7016 * issue a SERVICE ACTION IN (16) command, with the read capacity 7017 * serivce action set. 7018 */ 7019 if (lun->be_lun->maxlba > 0xfffffffe) 7020 scsi_ulto4b(0xffffffff, data->addr); 7021 else 7022 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7023 7024 /* 7025 * XXX KDM this may not be 512 bytes... 7026 */ 7027 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7028 7029 ctl_set_success(ctsio); 7030 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7031 ctsio->be_move_done = ctl_config_move_done; 7032 ctl_datamove((union ctl_io *)ctsio); 7033 return (CTL_RETVAL_COMPLETE); 7034 } 7035 7036 int 7037 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7038 { 7039 struct ctl_lun *lun = CTL_LUN(ctsio); 7040 struct scsi_read_capacity_16 *cdb; 7041 struct scsi_read_capacity_data_long *data; 7042 uint64_t lba; 7043 uint32_t alloc_len; 7044 7045 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7046 7047 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7048 7049 alloc_len = scsi_4btoul(cdb->alloc_len); 7050 lba = scsi_8btou64(cdb->addr); 7051 7052 if ((cdb->reladr & SRC16_PMI) 7053 && (lba != 0)) { 7054 ctl_set_invalid_field(/*ctsio*/ ctsio, 7055 /*sks_valid*/ 1, 7056 /*command*/ 1, 7057 /*field*/ 2, 7058 /*bit_valid*/ 0, 7059 /*bit*/ 0); 7060 ctl_done((union ctl_io *)ctsio); 7061 return (CTL_RETVAL_COMPLETE); 7062 } 7063 7064 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7065 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7066 ctsio->kern_rel_offset = 0; 7067 ctsio->kern_sg_entries = 0; 7068 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 7069 ctsio->kern_total_len = ctsio->kern_data_len; 7070 7071 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7072 /* XXX KDM this may not be 512 bytes... */ 7073 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7074 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7075 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7076 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7077 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7078 7079 ctl_set_success(ctsio); 7080 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7081 ctsio->be_move_done = ctl_config_move_done; 7082 ctl_datamove((union ctl_io *)ctsio); 7083 return (CTL_RETVAL_COMPLETE); 7084 } 7085 7086 int 7087 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7088 { 7089 struct ctl_lun *lun = CTL_LUN(ctsio); 7090 struct scsi_get_lba_status *cdb; 7091 struct scsi_get_lba_status_data *data; 7092 struct ctl_lba_len_flags *lbalen; 7093 uint64_t lba; 7094 uint32_t alloc_len, total_len; 7095 int retval; 7096 7097 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7098 7099 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7100 lba = scsi_8btou64(cdb->addr); 7101 alloc_len = scsi_4btoul(cdb->alloc_len); 7102 7103 if (lba > lun->be_lun->maxlba) { 7104 ctl_set_lba_out_of_range(ctsio, lba); 7105 ctl_done((union ctl_io *)ctsio); 7106 return (CTL_RETVAL_COMPLETE); 7107 } 7108 7109 total_len = sizeof(*data) + sizeof(data->descr[0]); 7110 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7111 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7112 ctsio->kern_rel_offset = 0; 7113 ctsio->kern_sg_entries = 0; 7114 ctsio->kern_data_len = min(total_len, alloc_len); 7115 ctsio->kern_total_len = ctsio->kern_data_len; 7116 7117 /* Fill dummy data in case backend can't tell anything. */ 7118 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7119 scsi_u64to8b(lba, data->descr[0].addr); 7120 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7121 data->descr[0].length); 7122 data->descr[0].status = 0; /* Mapped or unknown. */ 7123 7124 ctl_set_success(ctsio); 7125 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7126 ctsio->be_move_done = ctl_config_move_done; 7127 7128 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7129 lbalen->lba = lba; 7130 lbalen->len = total_len; 7131 lbalen->flags = 0; 7132 retval = lun->backend->config_read((union ctl_io *)ctsio); 7133 return (retval); 7134 } 7135 7136 int 7137 ctl_read_defect(struct ctl_scsiio *ctsio) 7138 { 7139 struct scsi_read_defect_data_10 *ccb10; 7140 struct scsi_read_defect_data_12 *ccb12; 7141 struct scsi_read_defect_data_hdr_10 *data10; 7142 struct scsi_read_defect_data_hdr_12 *data12; 7143 uint32_t alloc_len, data_len; 7144 uint8_t format; 7145 7146 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7147 7148 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7149 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7150 format = ccb10->format; 7151 alloc_len = scsi_2btoul(ccb10->alloc_length); 7152 data_len = sizeof(*data10); 7153 } else { 7154 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7155 format = ccb12->format; 7156 alloc_len = scsi_4btoul(ccb12->alloc_length); 7157 data_len = sizeof(*data12); 7158 } 7159 if (alloc_len == 0) { 7160 ctl_set_success(ctsio); 7161 ctl_done((union ctl_io *)ctsio); 7162 return (CTL_RETVAL_COMPLETE); 7163 } 7164 7165 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7166 ctsio->kern_rel_offset = 0; 7167 ctsio->kern_sg_entries = 0; 7168 ctsio->kern_data_len = min(data_len, alloc_len); 7169 ctsio->kern_total_len = ctsio->kern_data_len; 7170 7171 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7172 data10 = (struct scsi_read_defect_data_hdr_10 *) 7173 ctsio->kern_data_ptr; 7174 data10->format = format; 7175 scsi_ulto2b(0, data10->length); 7176 } else { 7177 data12 = (struct scsi_read_defect_data_hdr_12 *) 7178 ctsio->kern_data_ptr; 7179 data12->format = format; 7180 scsi_ulto2b(0, data12->generation); 7181 scsi_ulto4b(0, data12->length); 7182 } 7183 7184 ctl_set_success(ctsio); 7185 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7186 ctsio->be_move_done = ctl_config_move_done; 7187 ctl_datamove((union ctl_io *)ctsio); 7188 return (CTL_RETVAL_COMPLETE); 7189 } 7190 7191 int 7192 ctl_report_ident_info(struct ctl_scsiio *ctsio) 7193 { 7194 struct ctl_lun *lun = CTL_LUN(ctsio); 7195 struct scsi_report_ident_info *cdb; 7196 struct scsi_report_ident_info_data *rii_ptr; 7197 struct scsi_report_ident_info_descr *riid_ptr; 7198 const char *oii, *otii; 7199 int retval, alloc_len, total_len = 0, len = 0; 7200 7201 CTL_DEBUG_PRINT(("ctl_report_ident_info\n")); 7202 7203 cdb = (struct scsi_report_ident_info *)ctsio->cdb; 7204 retval = CTL_RETVAL_COMPLETE; 7205 7206 total_len = sizeof(struct scsi_report_ident_info_data); 7207 switch (cdb->type) { 7208 case RII_LUII: 7209 oii = dnvlist_get_string(lun->be_lun->options, 7210 "ident_info", NULL); 7211 if (oii) 7212 len = strlen(oii); /* Approximately */ 7213 break; 7214 case RII_LUTII: 7215 otii = dnvlist_get_string(lun->be_lun->options, 7216 "text_ident_info", NULL); 7217 if (otii) 7218 len = strlen(otii) + 1; /* NULL-terminated */ 7219 break; 7220 case RII_IIS: 7221 len = 2 * sizeof(struct scsi_report_ident_info_descr); 7222 break; 7223 default: 7224 ctl_set_invalid_field(/*ctsio*/ ctsio, 7225 /*sks_valid*/ 1, 7226 /*command*/ 1, 7227 /*field*/ 11, 7228 /*bit_valid*/ 1, 7229 /*bit*/ 2); 7230 ctl_done((union ctl_io *)ctsio); 7231 return(retval); 7232 } 7233 total_len += len; 7234 alloc_len = scsi_4btoul(cdb->length); 7235 7236 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7237 ctsio->kern_sg_entries = 0; 7238 ctsio->kern_rel_offset = 0; 7239 ctsio->kern_data_len = min(total_len, alloc_len); 7240 ctsio->kern_total_len = ctsio->kern_data_len; 7241 7242 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr; 7243 switch (cdb->type) { 7244 case RII_LUII: 7245 if (oii) { 7246 if (oii[0] == '0' && oii[1] == 'x') 7247 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len); 7248 else 7249 strncpy((uint8_t *)(rii_ptr + 1), oii, len); 7250 } 7251 break; 7252 case RII_LUTII: 7253 if (otii) 7254 strlcpy((uint8_t *)(rii_ptr + 1), otii, len); 7255 break; 7256 case RII_IIS: 7257 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1); 7258 riid_ptr->type = RII_LUII; 7259 scsi_ulto2b(0xffff, riid_ptr->length); 7260 riid_ptr++; 7261 riid_ptr->type = RII_LUTII; 7262 scsi_ulto2b(0xffff, riid_ptr->length); 7263 } 7264 scsi_ulto2b(len, rii_ptr->length); 7265 7266 ctl_set_success(ctsio); 7267 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7268 ctsio->be_move_done = ctl_config_move_done; 7269 ctl_datamove((union ctl_io *)ctsio); 7270 return(retval); 7271 } 7272 7273 int 7274 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7275 { 7276 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7277 struct ctl_lun *lun = CTL_LUN(ctsio); 7278 struct scsi_maintenance_in *cdb; 7279 int retval; 7280 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7281 int num_ha_groups, num_target_ports, shared_group; 7282 struct ctl_port *port; 7283 struct scsi_target_group_data *rtg_ptr; 7284 struct scsi_target_group_data_extended *rtg_ext_ptr; 7285 struct scsi_target_port_group_descriptor *tpg_desc; 7286 7287 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7288 7289 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7290 retval = CTL_RETVAL_COMPLETE; 7291 7292 switch (cdb->byte2 & STG_PDF_MASK) { 7293 case STG_PDF_LENGTH: 7294 ext = 0; 7295 break; 7296 case STG_PDF_EXTENDED: 7297 ext = 1; 7298 break; 7299 default: 7300 ctl_set_invalid_field(/*ctsio*/ ctsio, 7301 /*sks_valid*/ 1, 7302 /*command*/ 1, 7303 /*field*/ 2, 7304 /*bit_valid*/ 1, 7305 /*bit*/ 5); 7306 ctl_done((union ctl_io *)ctsio); 7307 return(retval); 7308 } 7309 7310 num_target_ports = 0; 7311 shared_group = (softc->is_single != 0); 7312 mtx_lock(&softc->ctl_lock); 7313 STAILQ_FOREACH(port, &softc->port_list, links) { 7314 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7315 continue; 7316 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7317 continue; 7318 num_target_ports++; 7319 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7320 shared_group = 1; 7321 } 7322 mtx_unlock(&softc->ctl_lock); 7323 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7324 7325 if (ext) 7326 total_len = sizeof(struct scsi_target_group_data_extended); 7327 else 7328 total_len = sizeof(struct scsi_target_group_data); 7329 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7330 (shared_group + num_ha_groups) + 7331 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7332 7333 alloc_len = scsi_4btoul(cdb->length); 7334 7335 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7336 ctsio->kern_sg_entries = 0; 7337 ctsio->kern_rel_offset = 0; 7338 ctsio->kern_data_len = min(total_len, alloc_len); 7339 ctsio->kern_total_len = ctsio->kern_data_len; 7340 7341 if (ext) { 7342 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7343 ctsio->kern_data_ptr; 7344 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7345 rtg_ext_ptr->format_type = 0x10; 7346 rtg_ext_ptr->implicit_transition_time = 0; 7347 tpg_desc = &rtg_ext_ptr->groups[0]; 7348 } else { 7349 rtg_ptr = (struct scsi_target_group_data *) 7350 ctsio->kern_data_ptr; 7351 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7352 tpg_desc = &rtg_ptr->groups[0]; 7353 } 7354 7355 mtx_lock(&softc->ctl_lock); 7356 pg = softc->port_min / softc->port_cnt; 7357 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7358 /* Some shelf is known to be primary. */ 7359 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7360 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7361 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7362 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7363 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7364 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7365 else 7366 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7367 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7368 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7369 } else { 7370 ts = os; 7371 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7372 } 7373 } else { 7374 /* No known primary shelf. */ 7375 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7376 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7377 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7378 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7379 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7380 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7381 } else { 7382 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7383 } 7384 } 7385 if (shared_group) { 7386 tpg_desc->pref_state = ts; 7387 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7388 TPG_U_SUP | TPG_T_SUP; 7389 scsi_ulto2b(1, tpg_desc->target_port_group); 7390 tpg_desc->status = TPG_IMPLICIT; 7391 pc = 0; 7392 STAILQ_FOREACH(port, &softc->port_list, links) { 7393 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7394 continue; 7395 if (!softc->is_single && 7396 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7397 continue; 7398 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7399 continue; 7400 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7401 relative_target_port_identifier); 7402 pc++; 7403 } 7404 tpg_desc->target_port_count = pc; 7405 tpg_desc = (struct scsi_target_port_group_descriptor *) 7406 &tpg_desc->descriptors[pc]; 7407 } 7408 for (g = 0; g < num_ha_groups; g++) { 7409 tpg_desc->pref_state = (g == pg) ? ts : os; 7410 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7411 TPG_U_SUP | TPG_T_SUP; 7412 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7413 tpg_desc->status = TPG_IMPLICIT; 7414 pc = 0; 7415 STAILQ_FOREACH(port, &softc->port_list, links) { 7416 if (port->targ_port < g * softc->port_cnt || 7417 port->targ_port >= (g + 1) * softc->port_cnt) 7418 continue; 7419 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7420 continue; 7421 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7422 continue; 7423 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7424 continue; 7425 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7426 relative_target_port_identifier); 7427 pc++; 7428 } 7429 tpg_desc->target_port_count = pc; 7430 tpg_desc = (struct scsi_target_port_group_descriptor *) 7431 &tpg_desc->descriptors[pc]; 7432 } 7433 mtx_unlock(&softc->ctl_lock); 7434 7435 ctl_set_success(ctsio); 7436 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7437 ctsio->be_move_done = ctl_config_move_done; 7438 ctl_datamove((union ctl_io *)ctsio); 7439 return(retval); 7440 } 7441 7442 int 7443 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7444 { 7445 struct ctl_lun *lun = CTL_LUN(ctsio); 7446 struct scsi_report_supported_opcodes *cdb; 7447 const struct ctl_cmd_entry *entry, *sentry; 7448 struct scsi_report_supported_opcodes_all *all; 7449 struct scsi_report_supported_opcodes_descr *descr; 7450 struct scsi_report_supported_opcodes_one *one; 7451 int retval; 7452 int alloc_len, total_len; 7453 int opcode, service_action, i, j, num; 7454 7455 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7456 7457 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7458 retval = CTL_RETVAL_COMPLETE; 7459 7460 opcode = cdb->requested_opcode; 7461 service_action = scsi_2btoul(cdb->requested_service_action); 7462 switch (cdb->options & RSO_OPTIONS_MASK) { 7463 case RSO_OPTIONS_ALL: 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 num++; 7474 } 7475 } else { 7476 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7477 entry)) 7478 num++; 7479 } 7480 } 7481 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7482 num * sizeof(struct scsi_report_supported_opcodes_descr); 7483 break; 7484 case RSO_OPTIONS_OC: 7485 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7486 ctl_set_invalid_field(/*ctsio*/ ctsio, 7487 /*sks_valid*/ 1, 7488 /*command*/ 1, 7489 /*field*/ 2, 7490 /*bit_valid*/ 1, 7491 /*bit*/ 2); 7492 ctl_done((union ctl_io *)ctsio); 7493 return (CTL_RETVAL_COMPLETE); 7494 } 7495 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7496 break; 7497 case RSO_OPTIONS_OC_SA: 7498 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7499 service_action >= 32) { 7500 ctl_set_invalid_field(/*ctsio*/ ctsio, 7501 /*sks_valid*/ 1, 7502 /*command*/ 1, 7503 /*field*/ 2, 7504 /*bit_valid*/ 1, 7505 /*bit*/ 2); 7506 ctl_done((union ctl_io *)ctsio); 7507 return (CTL_RETVAL_COMPLETE); 7508 } 7509 /* FALLTHROUGH */ 7510 case RSO_OPTIONS_OC_ASA: 7511 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7512 break; 7513 default: 7514 ctl_set_invalid_field(/*ctsio*/ ctsio, 7515 /*sks_valid*/ 1, 7516 /*command*/ 1, 7517 /*field*/ 2, 7518 /*bit_valid*/ 1, 7519 /*bit*/ 2); 7520 ctl_done((union ctl_io *)ctsio); 7521 return (CTL_RETVAL_COMPLETE); 7522 } 7523 7524 alloc_len = scsi_4btoul(cdb->length); 7525 7526 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7527 ctsio->kern_sg_entries = 0; 7528 ctsio->kern_rel_offset = 0; 7529 ctsio->kern_data_len = min(total_len, alloc_len); 7530 ctsio->kern_total_len = ctsio->kern_data_len; 7531 7532 switch (cdb->options & RSO_OPTIONS_MASK) { 7533 case RSO_OPTIONS_ALL: 7534 all = (struct scsi_report_supported_opcodes_all *) 7535 ctsio->kern_data_ptr; 7536 num = 0; 7537 for (i = 0; i < 256; i++) { 7538 entry = &ctl_cmd_table[i]; 7539 if (entry->flags & CTL_CMD_FLAG_SA5) { 7540 for (j = 0; j < 32; j++) { 7541 sentry = &((const struct ctl_cmd_entry *) 7542 entry->execute)[j]; 7543 if (!ctl_cmd_applicable( 7544 lun->be_lun->lun_type, sentry)) 7545 continue; 7546 descr = &all->descr[num++]; 7547 descr->opcode = i; 7548 scsi_ulto2b(j, descr->service_action); 7549 descr->flags = RSO_SERVACTV; 7550 scsi_ulto2b(sentry->length, 7551 descr->cdb_length); 7552 } 7553 } else { 7554 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7555 entry)) 7556 continue; 7557 descr = &all->descr[num++]; 7558 descr->opcode = i; 7559 scsi_ulto2b(0, descr->service_action); 7560 descr->flags = 0; 7561 scsi_ulto2b(entry->length, descr->cdb_length); 7562 } 7563 } 7564 scsi_ulto4b( 7565 num * sizeof(struct scsi_report_supported_opcodes_descr), 7566 all->length); 7567 break; 7568 case RSO_OPTIONS_OC: 7569 one = (struct scsi_report_supported_opcodes_one *) 7570 ctsio->kern_data_ptr; 7571 entry = &ctl_cmd_table[opcode]; 7572 goto fill_one; 7573 case RSO_OPTIONS_OC_SA: 7574 one = (struct scsi_report_supported_opcodes_one *) 7575 ctsio->kern_data_ptr; 7576 entry = &ctl_cmd_table[opcode]; 7577 entry = &((const struct ctl_cmd_entry *) 7578 entry->execute)[service_action]; 7579 fill_one: 7580 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7581 one->support = 3; 7582 scsi_ulto2b(entry->length, one->cdb_length); 7583 one->cdb_usage[0] = opcode; 7584 memcpy(&one->cdb_usage[1], entry->usage, 7585 entry->length - 1); 7586 } else 7587 one->support = 1; 7588 break; 7589 case RSO_OPTIONS_OC_ASA: 7590 one = (struct scsi_report_supported_opcodes_one *) 7591 ctsio->kern_data_ptr; 7592 entry = &ctl_cmd_table[opcode]; 7593 if (entry->flags & CTL_CMD_FLAG_SA5) { 7594 entry = &((const struct ctl_cmd_entry *) 7595 entry->execute)[service_action]; 7596 } else if (service_action != 0) { 7597 one->support = 1; 7598 break; 7599 } 7600 goto fill_one; 7601 } 7602 7603 ctl_set_success(ctsio); 7604 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7605 ctsio->be_move_done = ctl_config_move_done; 7606 ctl_datamove((union ctl_io *)ctsio); 7607 return(retval); 7608 } 7609 7610 int 7611 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7612 { 7613 struct scsi_report_supported_tmf *cdb; 7614 struct scsi_report_supported_tmf_ext_data *data; 7615 int retval; 7616 int alloc_len, total_len; 7617 7618 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7619 7620 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7621 7622 retval = CTL_RETVAL_COMPLETE; 7623 7624 if (cdb->options & RST_REPD) 7625 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7626 else 7627 total_len = sizeof(struct scsi_report_supported_tmf_data); 7628 alloc_len = scsi_4btoul(cdb->length); 7629 7630 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7631 ctsio->kern_sg_entries = 0; 7632 ctsio->kern_rel_offset = 0; 7633 ctsio->kern_data_len = min(total_len, alloc_len); 7634 ctsio->kern_total_len = ctsio->kern_data_len; 7635 7636 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7637 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7638 RST_TRS; 7639 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7640 data->length = total_len - 4; 7641 7642 ctl_set_success(ctsio); 7643 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7644 ctsio->be_move_done = ctl_config_move_done; 7645 ctl_datamove((union ctl_io *)ctsio); 7646 return (retval); 7647 } 7648 7649 int 7650 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7651 { 7652 struct scsi_report_timestamp *cdb; 7653 struct scsi_report_timestamp_data *data; 7654 struct timeval tv; 7655 int64_t timestamp; 7656 int retval; 7657 int alloc_len, total_len; 7658 7659 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7660 7661 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7662 7663 retval = CTL_RETVAL_COMPLETE; 7664 7665 total_len = sizeof(struct scsi_report_timestamp_data); 7666 alloc_len = scsi_4btoul(cdb->length); 7667 7668 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7669 ctsio->kern_sg_entries = 0; 7670 ctsio->kern_rel_offset = 0; 7671 ctsio->kern_data_len = min(total_len, alloc_len); 7672 ctsio->kern_total_len = ctsio->kern_data_len; 7673 7674 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7675 scsi_ulto2b(sizeof(*data) - 2, data->length); 7676 data->origin = RTS_ORIG_OUTSIDE; 7677 getmicrotime(&tv); 7678 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7679 scsi_ulto4b(timestamp >> 16, data->timestamp); 7680 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7681 7682 ctl_set_success(ctsio); 7683 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7684 ctsio->be_move_done = ctl_config_move_done; 7685 ctl_datamove((union ctl_io *)ctsio); 7686 return (retval); 7687 } 7688 7689 int 7690 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7691 { 7692 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7693 struct ctl_lun *lun = CTL_LUN(ctsio); 7694 struct scsi_per_res_in *cdb; 7695 int alloc_len, total_len = 0; 7696 /* struct scsi_per_res_in_rsrv in_data; */ 7697 uint64_t key; 7698 7699 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7700 7701 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7702 7703 alloc_len = scsi_2btoul(cdb->length); 7704 7705 retry: 7706 mtx_lock(&lun->lun_lock); 7707 switch (cdb->action) { 7708 case SPRI_RK: /* read keys */ 7709 total_len = sizeof(struct scsi_per_res_in_keys) + 7710 lun->pr_key_count * 7711 sizeof(struct scsi_per_res_key); 7712 break; 7713 case SPRI_RR: /* read reservation */ 7714 if (lun->flags & CTL_LUN_PR_RESERVED) 7715 total_len = sizeof(struct scsi_per_res_in_rsrv); 7716 else 7717 total_len = sizeof(struct scsi_per_res_in_header); 7718 break; 7719 case SPRI_RC: /* report capabilities */ 7720 total_len = sizeof(struct scsi_per_res_cap); 7721 break; 7722 case SPRI_RS: /* read full status */ 7723 total_len = sizeof(struct scsi_per_res_in_header) + 7724 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7725 lun->pr_key_count; 7726 break; 7727 default: 7728 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7729 } 7730 mtx_unlock(&lun->lun_lock); 7731 7732 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7733 ctsio->kern_rel_offset = 0; 7734 ctsio->kern_sg_entries = 0; 7735 ctsio->kern_data_len = min(total_len, alloc_len); 7736 ctsio->kern_total_len = ctsio->kern_data_len; 7737 7738 mtx_lock(&lun->lun_lock); 7739 switch (cdb->action) { 7740 case SPRI_RK: { // read keys 7741 struct scsi_per_res_in_keys *res_keys; 7742 int i, key_count; 7743 7744 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7745 7746 /* 7747 * We had to drop the lock to allocate our buffer, which 7748 * leaves time for someone to come in with another 7749 * persistent reservation. (That is unlikely, though, 7750 * since this should be the only persistent reservation 7751 * command active right now.) 7752 */ 7753 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7754 (lun->pr_key_count * 7755 sizeof(struct scsi_per_res_key)))){ 7756 mtx_unlock(&lun->lun_lock); 7757 free(ctsio->kern_data_ptr, M_CTL); 7758 printf("%s: reservation length changed, retrying\n", 7759 __func__); 7760 goto retry; 7761 } 7762 7763 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7764 7765 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7766 lun->pr_key_count, res_keys->header.length); 7767 7768 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7769 if ((key = ctl_get_prkey(lun, i)) == 0) 7770 continue; 7771 7772 /* 7773 * We used lun->pr_key_count to calculate the 7774 * size to allocate. If it turns out the number of 7775 * initiators with the registered flag set is 7776 * larger than that (i.e. they haven't been kept in 7777 * sync), we've got a problem. 7778 */ 7779 if (key_count >= lun->pr_key_count) { 7780 key_count++; 7781 continue; 7782 } 7783 scsi_u64to8b(key, res_keys->keys[key_count].key); 7784 key_count++; 7785 } 7786 break; 7787 } 7788 case SPRI_RR: { // read reservation 7789 struct scsi_per_res_in_rsrv *res; 7790 int tmp_len, header_only; 7791 7792 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7793 7794 scsi_ulto4b(lun->pr_generation, res->header.generation); 7795 7796 if (lun->flags & CTL_LUN_PR_RESERVED) 7797 { 7798 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7799 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7800 res->header.length); 7801 header_only = 0; 7802 } else { 7803 tmp_len = sizeof(struct scsi_per_res_in_header); 7804 scsi_ulto4b(0, res->header.length); 7805 header_only = 1; 7806 } 7807 7808 /* 7809 * We had to drop the lock to allocate our buffer, which 7810 * leaves time for someone to come in with another 7811 * persistent reservation. (That is unlikely, though, 7812 * since this should be the only persistent reservation 7813 * command active right now.) 7814 */ 7815 if (tmp_len != total_len) { 7816 mtx_unlock(&lun->lun_lock); 7817 free(ctsio->kern_data_ptr, M_CTL); 7818 printf("%s: reservation status changed, retrying\n", 7819 __func__); 7820 goto retry; 7821 } 7822 7823 /* 7824 * No reservation held, so we're done. 7825 */ 7826 if (header_only != 0) 7827 break; 7828 7829 /* 7830 * If the registration is an All Registrants type, the key 7831 * is 0, since it doesn't really matter. 7832 */ 7833 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7834 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7835 res->data.reservation); 7836 } 7837 res->data.scopetype = lun->pr_res_type; 7838 break; 7839 } 7840 case SPRI_RC: //report capabilities 7841 { 7842 struct scsi_per_res_cap *res_cap; 7843 uint16_t type_mask; 7844 7845 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7846 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7847 res_cap->flags1 = SPRI_CRH; 7848 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7849 type_mask = SPRI_TM_WR_EX_AR | 7850 SPRI_TM_EX_AC_RO | 7851 SPRI_TM_WR_EX_RO | 7852 SPRI_TM_EX_AC | 7853 SPRI_TM_WR_EX | 7854 SPRI_TM_EX_AC_AR; 7855 scsi_ulto2b(type_mask, res_cap->type_mask); 7856 break; 7857 } 7858 case SPRI_RS: { // read full status 7859 struct scsi_per_res_in_full *res_status; 7860 struct scsi_per_res_in_full_desc *res_desc; 7861 struct ctl_port *port; 7862 int i, len; 7863 7864 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7865 7866 /* 7867 * We had to drop the lock to allocate our buffer, which 7868 * leaves time for someone to come in with another 7869 * persistent reservation. (That is unlikely, though, 7870 * since this should be the only persistent reservation 7871 * command active right now.) 7872 */ 7873 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7874 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7875 lun->pr_key_count)){ 7876 mtx_unlock(&lun->lun_lock); 7877 free(ctsio->kern_data_ptr, M_CTL); 7878 printf("%s: reservation length changed, retrying\n", 7879 __func__); 7880 goto retry; 7881 } 7882 7883 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7884 7885 res_desc = &res_status->desc[0]; 7886 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7887 if ((key = ctl_get_prkey(lun, i)) == 0) 7888 continue; 7889 7890 scsi_u64to8b(key, res_desc->res_key.key); 7891 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7892 (lun->pr_res_idx == i || 7893 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7894 res_desc->flags = SPRI_FULL_R_HOLDER; 7895 res_desc->scopetype = lun->pr_res_type; 7896 } 7897 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7898 res_desc->rel_trgt_port_id); 7899 len = 0; 7900 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7901 if (port != NULL) 7902 len = ctl_create_iid(port, 7903 i % CTL_MAX_INIT_PER_PORT, 7904 res_desc->transport_id); 7905 scsi_ulto4b(len, res_desc->additional_length); 7906 res_desc = (struct scsi_per_res_in_full_desc *) 7907 &res_desc->transport_id[len]; 7908 } 7909 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7910 res_status->header.length); 7911 break; 7912 } 7913 default: 7914 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7915 } 7916 mtx_unlock(&lun->lun_lock); 7917 7918 ctl_set_success(ctsio); 7919 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7920 ctsio->be_move_done = ctl_config_move_done; 7921 ctl_datamove((union ctl_io *)ctsio); 7922 return (CTL_RETVAL_COMPLETE); 7923 } 7924 7925 /* 7926 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7927 * it should return. 7928 */ 7929 static int 7930 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7931 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7932 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7933 struct scsi_per_res_out_parms* param) 7934 { 7935 union ctl_ha_msg persis_io; 7936 int i; 7937 7938 mtx_lock(&lun->lun_lock); 7939 if (sa_res_key == 0) { 7940 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7941 /* validate scope and type */ 7942 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7943 SPR_LU_SCOPE) { 7944 mtx_unlock(&lun->lun_lock); 7945 ctl_set_invalid_field(/*ctsio*/ ctsio, 7946 /*sks_valid*/ 1, 7947 /*command*/ 1, 7948 /*field*/ 2, 7949 /*bit_valid*/ 1, 7950 /*bit*/ 4); 7951 ctl_done((union ctl_io *)ctsio); 7952 return (1); 7953 } 7954 7955 if (type>8 || type==2 || type==4 || type==0) { 7956 mtx_unlock(&lun->lun_lock); 7957 ctl_set_invalid_field(/*ctsio*/ ctsio, 7958 /*sks_valid*/ 1, 7959 /*command*/ 1, 7960 /*field*/ 2, 7961 /*bit_valid*/ 1, 7962 /*bit*/ 0); 7963 ctl_done((union ctl_io *)ctsio); 7964 return (1); 7965 } 7966 7967 /* 7968 * Unregister everybody else and build UA for 7969 * them 7970 */ 7971 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7972 if (i == residx || ctl_get_prkey(lun, i) == 0) 7973 continue; 7974 7975 ctl_clr_prkey(lun, i); 7976 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7977 } 7978 lun->pr_key_count = 1; 7979 lun->pr_res_type = type; 7980 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7981 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7982 lun->pr_res_idx = residx; 7983 lun->pr_generation++; 7984 mtx_unlock(&lun->lun_lock); 7985 7986 /* send msg to other side */ 7987 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7988 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7989 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7990 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7991 persis_io.pr.pr_info.res_type = type; 7992 memcpy(persis_io.pr.pr_info.sa_res_key, 7993 param->serv_act_res_key, 7994 sizeof(param->serv_act_res_key)); 7995 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7996 sizeof(persis_io.pr), M_WAITOK); 7997 } else { 7998 /* not all registrants */ 7999 mtx_unlock(&lun->lun_lock); 8000 free(ctsio->kern_data_ptr, M_CTL); 8001 ctl_set_invalid_field(ctsio, 8002 /*sks_valid*/ 1, 8003 /*command*/ 0, 8004 /*field*/ 8, 8005 /*bit_valid*/ 0, 8006 /*bit*/ 0); 8007 ctl_done((union ctl_io *)ctsio); 8008 return (1); 8009 } 8010 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8011 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8012 int found = 0; 8013 8014 if (res_key == sa_res_key) { 8015 /* special case */ 8016 /* 8017 * The spec implies this is not good but doesn't 8018 * say what to do. There are two choices either 8019 * generate a res conflict or check condition 8020 * with illegal field in parameter data. Since 8021 * that is what is done when the sa_res_key is 8022 * zero I'll take that approach since this has 8023 * to do with the sa_res_key. 8024 */ 8025 mtx_unlock(&lun->lun_lock); 8026 free(ctsio->kern_data_ptr, M_CTL); 8027 ctl_set_invalid_field(ctsio, 8028 /*sks_valid*/ 1, 8029 /*command*/ 0, 8030 /*field*/ 8, 8031 /*bit_valid*/ 0, 8032 /*bit*/ 0); 8033 ctl_done((union ctl_io *)ctsio); 8034 return (1); 8035 } 8036 8037 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8038 if (ctl_get_prkey(lun, i) != sa_res_key) 8039 continue; 8040 8041 found = 1; 8042 ctl_clr_prkey(lun, i); 8043 lun->pr_key_count--; 8044 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8045 } 8046 if (!found) { 8047 mtx_unlock(&lun->lun_lock); 8048 free(ctsio->kern_data_ptr, M_CTL); 8049 ctl_set_reservation_conflict(ctsio); 8050 ctl_done((union ctl_io *)ctsio); 8051 return (CTL_RETVAL_COMPLETE); 8052 } 8053 lun->pr_generation++; 8054 mtx_unlock(&lun->lun_lock); 8055 8056 /* send msg to other side */ 8057 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8058 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8059 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8060 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8061 persis_io.pr.pr_info.res_type = type; 8062 memcpy(persis_io.pr.pr_info.sa_res_key, 8063 param->serv_act_res_key, 8064 sizeof(param->serv_act_res_key)); 8065 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8066 sizeof(persis_io.pr), M_WAITOK); 8067 } else { 8068 /* Reserved but not all registrants */ 8069 /* sa_res_key is res holder */ 8070 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8071 /* validate scope and type */ 8072 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8073 SPR_LU_SCOPE) { 8074 mtx_unlock(&lun->lun_lock); 8075 ctl_set_invalid_field(/*ctsio*/ ctsio, 8076 /*sks_valid*/ 1, 8077 /*command*/ 1, 8078 /*field*/ 2, 8079 /*bit_valid*/ 1, 8080 /*bit*/ 4); 8081 ctl_done((union ctl_io *)ctsio); 8082 return (1); 8083 } 8084 8085 if (type>8 || type==2 || type==4 || type==0) { 8086 mtx_unlock(&lun->lun_lock); 8087 ctl_set_invalid_field(/*ctsio*/ ctsio, 8088 /*sks_valid*/ 1, 8089 /*command*/ 1, 8090 /*field*/ 2, 8091 /*bit_valid*/ 1, 8092 /*bit*/ 0); 8093 ctl_done((union ctl_io *)ctsio); 8094 return (1); 8095 } 8096 8097 /* 8098 * Do the following: 8099 * if sa_res_key != res_key remove all 8100 * registrants w/sa_res_key and generate UA 8101 * for these registrants(Registrations 8102 * Preempted) if it wasn't an exclusive 8103 * reservation generate UA(Reservations 8104 * Preempted) for all other registered nexuses 8105 * if the type has changed. Establish the new 8106 * reservation and holder. If res_key and 8107 * sa_res_key are the same do the above 8108 * except don't unregister the res holder. 8109 */ 8110 8111 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8112 if (i == residx || ctl_get_prkey(lun, i) == 0) 8113 continue; 8114 8115 if (sa_res_key == ctl_get_prkey(lun, i)) { 8116 ctl_clr_prkey(lun, i); 8117 lun->pr_key_count--; 8118 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8119 } else if (type != lun->pr_res_type && 8120 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8121 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8122 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8123 } 8124 } 8125 lun->pr_res_type = type; 8126 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8127 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8128 lun->pr_res_idx = residx; 8129 else 8130 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8131 lun->pr_generation++; 8132 mtx_unlock(&lun->lun_lock); 8133 8134 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8135 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8136 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8137 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8138 persis_io.pr.pr_info.res_type = type; 8139 memcpy(persis_io.pr.pr_info.sa_res_key, 8140 param->serv_act_res_key, 8141 sizeof(param->serv_act_res_key)); 8142 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8143 sizeof(persis_io.pr), M_WAITOK); 8144 } else { 8145 /* 8146 * sa_res_key is not the res holder just 8147 * remove registrants 8148 */ 8149 int found=0; 8150 8151 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8152 if (sa_res_key != ctl_get_prkey(lun, i)) 8153 continue; 8154 8155 found = 1; 8156 ctl_clr_prkey(lun, i); 8157 lun->pr_key_count--; 8158 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8159 } 8160 8161 if (!found) { 8162 mtx_unlock(&lun->lun_lock); 8163 free(ctsio->kern_data_ptr, M_CTL); 8164 ctl_set_reservation_conflict(ctsio); 8165 ctl_done((union ctl_io *)ctsio); 8166 return (1); 8167 } 8168 lun->pr_generation++; 8169 mtx_unlock(&lun->lun_lock); 8170 8171 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8172 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8173 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8174 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8175 persis_io.pr.pr_info.res_type = type; 8176 memcpy(persis_io.pr.pr_info.sa_res_key, 8177 param->serv_act_res_key, 8178 sizeof(param->serv_act_res_key)); 8179 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8180 sizeof(persis_io.pr), M_WAITOK); 8181 } 8182 } 8183 return (0); 8184 } 8185 8186 static void 8187 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8188 { 8189 uint64_t sa_res_key; 8190 int i; 8191 8192 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8193 8194 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8195 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8196 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8197 if (sa_res_key == 0) { 8198 /* 8199 * Unregister everybody else and build UA for 8200 * them 8201 */ 8202 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8203 if (i == msg->pr.pr_info.residx || 8204 ctl_get_prkey(lun, i) == 0) 8205 continue; 8206 8207 ctl_clr_prkey(lun, i); 8208 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8209 } 8210 8211 lun->pr_key_count = 1; 8212 lun->pr_res_type = msg->pr.pr_info.res_type; 8213 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8214 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8215 lun->pr_res_idx = msg->pr.pr_info.residx; 8216 } else { 8217 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8218 if (sa_res_key == ctl_get_prkey(lun, i)) 8219 continue; 8220 8221 ctl_clr_prkey(lun, i); 8222 lun->pr_key_count--; 8223 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8224 } 8225 } 8226 } else { 8227 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8228 if (i == msg->pr.pr_info.residx || 8229 ctl_get_prkey(lun, i) == 0) 8230 continue; 8231 8232 if (sa_res_key == ctl_get_prkey(lun, i)) { 8233 ctl_clr_prkey(lun, i); 8234 lun->pr_key_count--; 8235 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8236 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8237 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8238 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8239 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8240 } 8241 } 8242 lun->pr_res_type = msg->pr.pr_info.res_type; 8243 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8244 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8245 lun->pr_res_idx = msg->pr.pr_info.residx; 8246 else 8247 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8248 } 8249 lun->pr_generation++; 8250 8251 } 8252 8253 int 8254 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8255 { 8256 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8257 struct ctl_lun *lun = CTL_LUN(ctsio); 8258 int retval; 8259 u_int32_t param_len; 8260 struct scsi_per_res_out *cdb; 8261 struct scsi_per_res_out_parms* param; 8262 uint32_t residx; 8263 uint64_t res_key, sa_res_key, key; 8264 uint8_t type; 8265 union ctl_ha_msg persis_io; 8266 int i; 8267 8268 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8269 8270 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8271 retval = CTL_RETVAL_COMPLETE; 8272 8273 /* 8274 * We only support whole-LUN scope. The scope & type are ignored for 8275 * register, register and ignore existing key and clear. 8276 * We sometimes ignore scope and type on preempts too!! 8277 * Verify reservation type here as well. 8278 */ 8279 type = cdb->scope_type & SPR_TYPE_MASK; 8280 if ((cdb->action == SPRO_RESERVE) 8281 || (cdb->action == SPRO_RELEASE)) { 8282 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8283 ctl_set_invalid_field(/*ctsio*/ ctsio, 8284 /*sks_valid*/ 1, 8285 /*command*/ 1, 8286 /*field*/ 2, 8287 /*bit_valid*/ 1, 8288 /*bit*/ 4); 8289 ctl_done((union ctl_io *)ctsio); 8290 return (CTL_RETVAL_COMPLETE); 8291 } 8292 8293 if (type>8 || type==2 || type==4 || type==0) { 8294 ctl_set_invalid_field(/*ctsio*/ ctsio, 8295 /*sks_valid*/ 1, 8296 /*command*/ 1, 8297 /*field*/ 2, 8298 /*bit_valid*/ 1, 8299 /*bit*/ 0); 8300 ctl_done((union ctl_io *)ctsio); 8301 return (CTL_RETVAL_COMPLETE); 8302 } 8303 } 8304 8305 param_len = scsi_4btoul(cdb->length); 8306 8307 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8308 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8309 ctsio->kern_data_len = param_len; 8310 ctsio->kern_total_len = param_len; 8311 ctsio->kern_rel_offset = 0; 8312 ctsio->kern_sg_entries = 0; 8313 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8314 ctsio->be_move_done = ctl_config_move_done; 8315 ctl_datamove((union ctl_io *)ctsio); 8316 8317 return (CTL_RETVAL_COMPLETE); 8318 } 8319 8320 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8321 8322 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8323 res_key = scsi_8btou64(param->res_key.key); 8324 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8325 8326 /* 8327 * Validate the reservation key here except for SPRO_REG_IGNO 8328 * This must be done for all other service actions 8329 */ 8330 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8331 mtx_lock(&lun->lun_lock); 8332 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8333 if (res_key != key) { 8334 /* 8335 * The current key passed in doesn't match 8336 * the one the initiator previously 8337 * registered. 8338 */ 8339 mtx_unlock(&lun->lun_lock); 8340 free(ctsio->kern_data_ptr, M_CTL); 8341 ctl_set_reservation_conflict(ctsio); 8342 ctl_done((union ctl_io *)ctsio); 8343 return (CTL_RETVAL_COMPLETE); 8344 } 8345 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8346 /* 8347 * We are not registered 8348 */ 8349 mtx_unlock(&lun->lun_lock); 8350 free(ctsio->kern_data_ptr, M_CTL); 8351 ctl_set_reservation_conflict(ctsio); 8352 ctl_done((union ctl_io *)ctsio); 8353 return (CTL_RETVAL_COMPLETE); 8354 } else if (res_key != 0) { 8355 /* 8356 * We are not registered and trying to register but 8357 * the register key isn't zero. 8358 */ 8359 mtx_unlock(&lun->lun_lock); 8360 free(ctsio->kern_data_ptr, M_CTL); 8361 ctl_set_reservation_conflict(ctsio); 8362 ctl_done((union ctl_io *)ctsio); 8363 return (CTL_RETVAL_COMPLETE); 8364 } 8365 mtx_unlock(&lun->lun_lock); 8366 } 8367 8368 switch (cdb->action & SPRO_ACTION_MASK) { 8369 case SPRO_REGISTER: 8370 case SPRO_REG_IGNO: { 8371 /* 8372 * We don't support any of these options, as we report in 8373 * the read capabilities request (see 8374 * ctl_persistent_reserve_in(), above). 8375 */ 8376 if ((param->flags & SPR_SPEC_I_PT) 8377 || (param->flags & SPR_ALL_TG_PT) 8378 || (param->flags & SPR_APTPL)) { 8379 int bit_ptr; 8380 8381 if (param->flags & SPR_APTPL) 8382 bit_ptr = 0; 8383 else if (param->flags & SPR_ALL_TG_PT) 8384 bit_ptr = 2; 8385 else /* SPR_SPEC_I_PT */ 8386 bit_ptr = 3; 8387 8388 free(ctsio->kern_data_ptr, M_CTL); 8389 ctl_set_invalid_field(ctsio, 8390 /*sks_valid*/ 1, 8391 /*command*/ 0, 8392 /*field*/ 20, 8393 /*bit_valid*/ 1, 8394 /*bit*/ bit_ptr); 8395 ctl_done((union ctl_io *)ctsio); 8396 return (CTL_RETVAL_COMPLETE); 8397 } 8398 8399 mtx_lock(&lun->lun_lock); 8400 8401 /* 8402 * The initiator wants to clear the 8403 * key/unregister. 8404 */ 8405 if (sa_res_key == 0) { 8406 if ((res_key == 0 8407 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8408 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8409 && ctl_get_prkey(lun, residx) == 0)) { 8410 mtx_unlock(&lun->lun_lock); 8411 goto done; 8412 } 8413 8414 ctl_clr_prkey(lun, residx); 8415 lun->pr_key_count--; 8416 8417 if (residx == lun->pr_res_idx) { 8418 lun->flags &= ~CTL_LUN_PR_RESERVED; 8419 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8420 8421 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8422 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8423 lun->pr_key_count) { 8424 /* 8425 * If the reservation is a registrants 8426 * only type we need to generate a UA 8427 * for other registered inits. The 8428 * sense code should be RESERVATIONS 8429 * RELEASED 8430 */ 8431 8432 for (i = softc->init_min; i < softc->init_max; i++){ 8433 if (ctl_get_prkey(lun, i) == 0) 8434 continue; 8435 ctl_est_ua(lun, i, 8436 CTL_UA_RES_RELEASE); 8437 } 8438 } 8439 lun->pr_res_type = 0; 8440 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8441 if (lun->pr_key_count==0) { 8442 lun->flags &= ~CTL_LUN_PR_RESERVED; 8443 lun->pr_res_type = 0; 8444 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8445 } 8446 } 8447 lun->pr_generation++; 8448 mtx_unlock(&lun->lun_lock); 8449 8450 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8451 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8452 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8453 persis_io.pr.pr_info.residx = residx; 8454 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8455 sizeof(persis_io.pr), M_WAITOK); 8456 } else /* sa_res_key != 0 */ { 8457 /* 8458 * If we aren't registered currently then increment 8459 * the key count and set the registered flag. 8460 */ 8461 ctl_alloc_prkey(lun, residx); 8462 if (ctl_get_prkey(lun, residx) == 0) 8463 lun->pr_key_count++; 8464 ctl_set_prkey(lun, residx, sa_res_key); 8465 lun->pr_generation++; 8466 mtx_unlock(&lun->lun_lock); 8467 8468 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8469 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8470 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8471 persis_io.pr.pr_info.residx = residx; 8472 memcpy(persis_io.pr.pr_info.sa_res_key, 8473 param->serv_act_res_key, 8474 sizeof(param->serv_act_res_key)); 8475 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8476 sizeof(persis_io.pr), M_WAITOK); 8477 } 8478 8479 break; 8480 } 8481 case SPRO_RESERVE: 8482 mtx_lock(&lun->lun_lock); 8483 if (lun->flags & CTL_LUN_PR_RESERVED) { 8484 /* 8485 * if this isn't the reservation holder and it's 8486 * not a "all registrants" type or if the type is 8487 * different then we have a conflict 8488 */ 8489 if ((lun->pr_res_idx != residx 8490 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8491 || lun->pr_res_type != type) { 8492 mtx_unlock(&lun->lun_lock); 8493 free(ctsio->kern_data_ptr, M_CTL); 8494 ctl_set_reservation_conflict(ctsio); 8495 ctl_done((union ctl_io *)ctsio); 8496 return (CTL_RETVAL_COMPLETE); 8497 } 8498 mtx_unlock(&lun->lun_lock); 8499 } else /* create a reservation */ { 8500 /* 8501 * If it's not an "all registrants" type record 8502 * reservation holder 8503 */ 8504 if (type != SPR_TYPE_WR_EX_AR 8505 && type != SPR_TYPE_EX_AC_AR) 8506 lun->pr_res_idx = residx; /* Res holder */ 8507 else 8508 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8509 8510 lun->flags |= CTL_LUN_PR_RESERVED; 8511 lun->pr_res_type = type; 8512 8513 mtx_unlock(&lun->lun_lock); 8514 8515 /* send msg to other side */ 8516 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8517 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8518 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8519 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8520 persis_io.pr.pr_info.res_type = type; 8521 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8522 sizeof(persis_io.pr), M_WAITOK); 8523 } 8524 break; 8525 8526 case SPRO_RELEASE: 8527 mtx_lock(&lun->lun_lock); 8528 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8529 /* No reservation exists return good status */ 8530 mtx_unlock(&lun->lun_lock); 8531 goto done; 8532 } 8533 /* 8534 * Is this nexus a reservation holder? 8535 */ 8536 if (lun->pr_res_idx != residx 8537 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8538 /* 8539 * not a res holder return good status but 8540 * do nothing 8541 */ 8542 mtx_unlock(&lun->lun_lock); 8543 goto done; 8544 } 8545 8546 if (lun->pr_res_type != type) { 8547 mtx_unlock(&lun->lun_lock); 8548 free(ctsio->kern_data_ptr, M_CTL); 8549 ctl_set_illegal_pr_release(ctsio); 8550 ctl_done((union ctl_io *)ctsio); 8551 return (CTL_RETVAL_COMPLETE); 8552 } 8553 8554 /* okay to release */ 8555 lun->flags &= ~CTL_LUN_PR_RESERVED; 8556 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8557 lun->pr_res_type = 0; 8558 8559 /* 8560 * If this isn't an exclusive access reservation and NUAR 8561 * is not set, generate UA for all other registrants. 8562 */ 8563 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8564 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8565 for (i = softc->init_min; i < softc->init_max; i++) { 8566 if (i == residx || ctl_get_prkey(lun, i) == 0) 8567 continue; 8568 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8569 } 8570 } 8571 mtx_unlock(&lun->lun_lock); 8572 8573 /* Send msg to other side */ 8574 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8575 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8576 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8577 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8578 sizeof(persis_io.pr), M_WAITOK); 8579 break; 8580 8581 case SPRO_CLEAR: 8582 /* send msg to other side */ 8583 8584 mtx_lock(&lun->lun_lock); 8585 lun->flags &= ~CTL_LUN_PR_RESERVED; 8586 lun->pr_res_type = 0; 8587 lun->pr_key_count = 0; 8588 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8589 8590 ctl_clr_prkey(lun, residx); 8591 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8592 if (ctl_get_prkey(lun, i) != 0) { 8593 ctl_clr_prkey(lun, i); 8594 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8595 } 8596 lun->pr_generation++; 8597 mtx_unlock(&lun->lun_lock); 8598 8599 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8600 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8601 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8602 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8603 sizeof(persis_io.pr), M_WAITOK); 8604 break; 8605 8606 case SPRO_PREEMPT: 8607 case SPRO_PRE_ABO: { 8608 int nretval; 8609 8610 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8611 residx, ctsio, cdb, param); 8612 if (nretval != 0) 8613 return (CTL_RETVAL_COMPLETE); 8614 break; 8615 } 8616 default: 8617 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8618 } 8619 8620 done: 8621 free(ctsio->kern_data_ptr, M_CTL); 8622 ctl_set_success(ctsio); 8623 ctl_done((union ctl_io *)ctsio); 8624 8625 return (retval); 8626 } 8627 8628 /* 8629 * This routine is for handling a message from the other SC pertaining to 8630 * persistent reserve out. All the error checking will have been done 8631 * so only performing the action need be done here to keep the two 8632 * in sync. 8633 */ 8634 static void 8635 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8636 { 8637 struct ctl_softc *softc = CTL_SOFTC(io); 8638 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8639 struct ctl_lun *lun; 8640 int i; 8641 uint32_t residx, targ_lun; 8642 8643 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8644 mtx_lock(&softc->ctl_lock); 8645 if (targ_lun >= ctl_max_luns || 8646 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8647 mtx_unlock(&softc->ctl_lock); 8648 return; 8649 } 8650 mtx_lock(&lun->lun_lock); 8651 mtx_unlock(&softc->ctl_lock); 8652 if (lun->flags & CTL_LUN_DISABLED) { 8653 mtx_unlock(&lun->lun_lock); 8654 return; 8655 } 8656 residx = ctl_get_initindex(&msg->hdr.nexus); 8657 switch(msg->pr.pr_info.action) { 8658 case CTL_PR_REG_KEY: 8659 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8660 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8661 lun->pr_key_count++; 8662 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8663 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8664 lun->pr_generation++; 8665 break; 8666 8667 case CTL_PR_UNREG_KEY: 8668 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8669 lun->pr_key_count--; 8670 8671 /* XXX Need to see if the reservation has been released */ 8672 /* if so do we need to generate UA? */ 8673 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8674 lun->flags &= ~CTL_LUN_PR_RESERVED; 8675 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8676 8677 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8678 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8679 lun->pr_key_count) { 8680 /* 8681 * If the reservation is a registrants 8682 * only type we need to generate a UA 8683 * for other registered inits. The 8684 * sense code should be RESERVATIONS 8685 * RELEASED 8686 */ 8687 8688 for (i = softc->init_min; i < softc->init_max; i++) { 8689 if (ctl_get_prkey(lun, i) == 0) 8690 continue; 8691 8692 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8693 } 8694 } 8695 lun->pr_res_type = 0; 8696 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8697 if (lun->pr_key_count==0) { 8698 lun->flags &= ~CTL_LUN_PR_RESERVED; 8699 lun->pr_res_type = 0; 8700 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8701 } 8702 } 8703 lun->pr_generation++; 8704 break; 8705 8706 case CTL_PR_RESERVE: 8707 lun->flags |= CTL_LUN_PR_RESERVED; 8708 lun->pr_res_type = msg->pr.pr_info.res_type; 8709 lun->pr_res_idx = msg->pr.pr_info.residx; 8710 8711 break; 8712 8713 case CTL_PR_RELEASE: 8714 /* 8715 * If this isn't an exclusive access reservation and NUAR 8716 * is not set, generate UA for all other registrants. 8717 */ 8718 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8719 lun->pr_res_type != SPR_TYPE_WR_EX && 8720 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8721 for (i = softc->init_min; i < softc->init_max; i++) { 8722 if (i == residx || ctl_get_prkey(lun, i) == 0) 8723 continue; 8724 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8725 } 8726 } 8727 8728 lun->flags &= ~CTL_LUN_PR_RESERVED; 8729 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8730 lun->pr_res_type = 0; 8731 break; 8732 8733 case CTL_PR_PREEMPT: 8734 ctl_pro_preempt_other(lun, msg); 8735 break; 8736 case CTL_PR_CLEAR: 8737 lun->flags &= ~CTL_LUN_PR_RESERVED; 8738 lun->pr_res_type = 0; 8739 lun->pr_key_count = 0; 8740 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8741 8742 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8743 if (ctl_get_prkey(lun, i) == 0) 8744 continue; 8745 ctl_clr_prkey(lun, i); 8746 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8747 } 8748 lun->pr_generation++; 8749 break; 8750 } 8751 8752 mtx_unlock(&lun->lun_lock); 8753 } 8754 8755 int 8756 ctl_read_write(struct ctl_scsiio *ctsio) 8757 { 8758 struct ctl_lun *lun = CTL_LUN(ctsio); 8759 struct ctl_lba_len_flags *lbalen; 8760 uint64_t lba; 8761 uint32_t num_blocks; 8762 int flags, retval; 8763 int isread; 8764 8765 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8766 8767 flags = 0; 8768 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8769 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8770 switch (ctsio->cdb[0]) { 8771 case READ_6: 8772 case WRITE_6: { 8773 struct scsi_rw_6 *cdb; 8774 8775 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8776 8777 lba = scsi_3btoul(cdb->addr); 8778 /* only 5 bits are valid in the most significant address byte */ 8779 lba &= 0x1fffff; 8780 num_blocks = cdb->length; 8781 /* 8782 * This is correct according to SBC-2. 8783 */ 8784 if (num_blocks == 0) 8785 num_blocks = 256; 8786 break; 8787 } 8788 case READ_10: 8789 case WRITE_10: { 8790 struct scsi_rw_10 *cdb; 8791 8792 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8793 if (cdb->byte2 & SRW10_FUA) 8794 flags |= CTL_LLF_FUA; 8795 if (cdb->byte2 & SRW10_DPO) 8796 flags |= CTL_LLF_DPO; 8797 lba = scsi_4btoul(cdb->addr); 8798 num_blocks = scsi_2btoul(cdb->length); 8799 break; 8800 } 8801 case WRITE_VERIFY_10: { 8802 struct scsi_write_verify_10 *cdb; 8803 8804 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8805 flags |= CTL_LLF_FUA; 8806 if (cdb->byte2 & SWV_DPO) 8807 flags |= CTL_LLF_DPO; 8808 lba = scsi_4btoul(cdb->addr); 8809 num_blocks = scsi_2btoul(cdb->length); 8810 break; 8811 } 8812 case READ_12: 8813 case WRITE_12: { 8814 struct scsi_rw_12 *cdb; 8815 8816 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8817 if (cdb->byte2 & SRW12_FUA) 8818 flags |= CTL_LLF_FUA; 8819 if (cdb->byte2 & SRW12_DPO) 8820 flags |= CTL_LLF_DPO; 8821 lba = scsi_4btoul(cdb->addr); 8822 num_blocks = scsi_4btoul(cdb->length); 8823 break; 8824 } 8825 case WRITE_VERIFY_12: { 8826 struct scsi_write_verify_12 *cdb; 8827 8828 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8829 flags |= CTL_LLF_FUA; 8830 if (cdb->byte2 & SWV_DPO) 8831 flags |= CTL_LLF_DPO; 8832 lba = scsi_4btoul(cdb->addr); 8833 num_blocks = scsi_4btoul(cdb->length); 8834 break; 8835 } 8836 case READ_16: 8837 case WRITE_16: { 8838 struct scsi_rw_16 *cdb; 8839 8840 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8841 if (cdb->byte2 & SRW12_FUA) 8842 flags |= CTL_LLF_FUA; 8843 if (cdb->byte2 & SRW12_DPO) 8844 flags |= CTL_LLF_DPO; 8845 lba = scsi_8btou64(cdb->addr); 8846 num_blocks = scsi_4btoul(cdb->length); 8847 break; 8848 } 8849 case WRITE_ATOMIC_16: { 8850 struct scsi_write_atomic_16 *cdb; 8851 8852 if (lun->be_lun->atomicblock == 0) { 8853 ctl_set_invalid_opcode(ctsio); 8854 ctl_done((union ctl_io *)ctsio); 8855 return (CTL_RETVAL_COMPLETE); 8856 } 8857 8858 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8859 if (cdb->byte2 & SRW12_FUA) 8860 flags |= CTL_LLF_FUA; 8861 if (cdb->byte2 & SRW12_DPO) 8862 flags |= CTL_LLF_DPO; 8863 lba = scsi_8btou64(cdb->addr); 8864 num_blocks = scsi_2btoul(cdb->length); 8865 if (num_blocks > lun->be_lun->atomicblock) { 8866 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8867 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8868 /*bit*/ 0); 8869 ctl_done((union ctl_io *)ctsio); 8870 return (CTL_RETVAL_COMPLETE); 8871 } 8872 break; 8873 } 8874 case WRITE_VERIFY_16: { 8875 struct scsi_write_verify_16 *cdb; 8876 8877 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8878 flags |= CTL_LLF_FUA; 8879 if (cdb->byte2 & SWV_DPO) 8880 flags |= CTL_LLF_DPO; 8881 lba = scsi_8btou64(cdb->addr); 8882 num_blocks = scsi_4btoul(cdb->length); 8883 break; 8884 } 8885 default: 8886 /* 8887 * We got a command we don't support. This shouldn't 8888 * happen, commands should be filtered out above us. 8889 */ 8890 ctl_set_invalid_opcode(ctsio); 8891 ctl_done((union ctl_io *)ctsio); 8892 8893 return (CTL_RETVAL_COMPLETE); 8894 break; /* NOTREACHED */ 8895 } 8896 8897 /* 8898 * The first check is to make sure we're in bounds, the second 8899 * check is to catch wrap-around problems. If the lba + num blocks 8900 * is less than the lba, then we've wrapped around and the block 8901 * range is invalid anyway. 8902 */ 8903 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8904 || ((lba + num_blocks) < lba)) { 8905 ctl_set_lba_out_of_range(ctsio, 8906 MAX(lba, lun->be_lun->maxlba + 1)); 8907 ctl_done((union ctl_io *)ctsio); 8908 return (CTL_RETVAL_COMPLETE); 8909 } 8910 8911 /* 8912 * According to SBC-3, a transfer length of 0 is not an error. 8913 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8914 * translates to 256 blocks for those commands. 8915 */ 8916 if (num_blocks == 0) { 8917 ctl_set_success(ctsio); 8918 ctl_done((union ctl_io *)ctsio); 8919 return (CTL_RETVAL_COMPLETE); 8920 } 8921 8922 /* Set FUA and/or DPO if caches are disabled. */ 8923 if (isread) { 8924 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8925 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8926 } else { 8927 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8928 flags |= CTL_LLF_FUA; 8929 } 8930 8931 lbalen = (struct ctl_lba_len_flags *) 8932 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8933 lbalen->lba = lba; 8934 lbalen->len = num_blocks; 8935 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8936 8937 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8938 ctsio->kern_rel_offset = 0; 8939 8940 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8941 8942 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8943 return (retval); 8944 } 8945 8946 static int 8947 ctl_cnw_cont(union ctl_io *io) 8948 { 8949 struct ctl_lun *lun = CTL_LUN(io); 8950 struct ctl_scsiio *ctsio; 8951 struct ctl_lba_len_flags *lbalen; 8952 int retval; 8953 8954 ctsio = &io->scsiio; 8955 ctsio->io_hdr.status = CTL_STATUS_NONE; 8956 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8957 lbalen = (struct ctl_lba_len_flags *) 8958 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8959 lbalen->flags &= ~CTL_LLF_COMPARE; 8960 lbalen->flags |= CTL_LLF_WRITE; 8961 8962 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8963 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8964 return (retval); 8965 } 8966 8967 int 8968 ctl_cnw(struct ctl_scsiio *ctsio) 8969 { 8970 struct ctl_lun *lun = CTL_LUN(ctsio); 8971 struct ctl_lba_len_flags *lbalen; 8972 uint64_t lba; 8973 uint32_t num_blocks; 8974 int flags, retval; 8975 8976 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8977 8978 flags = 0; 8979 switch (ctsio->cdb[0]) { 8980 case COMPARE_AND_WRITE: { 8981 struct scsi_compare_and_write *cdb; 8982 8983 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8984 if (cdb->byte2 & SRW10_FUA) 8985 flags |= CTL_LLF_FUA; 8986 if (cdb->byte2 & SRW10_DPO) 8987 flags |= CTL_LLF_DPO; 8988 lba = scsi_8btou64(cdb->addr); 8989 num_blocks = cdb->length; 8990 break; 8991 } 8992 default: 8993 /* 8994 * We got a command we don't support. This shouldn't 8995 * happen, commands should be filtered out above us. 8996 */ 8997 ctl_set_invalid_opcode(ctsio); 8998 ctl_done((union ctl_io *)ctsio); 8999 9000 return (CTL_RETVAL_COMPLETE); 9001 break; /* NOTREACHED */ 9002 } 9003 9004 /* 9005 * The first check is to make sure we're in bounds, the second 9006 * check is to catch wrap-around problems. If the lba + num blocks 9007 * is less than the lba, then we've wrapped around and the block 9008 * range is invalid anyway. 9009 */ 9010 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9011 || ((lba + num_blocks) < lba)) { 9012 ctl_set_lba_out_of_range(ctsio, 9013 MAX(lba, lun->be_lun->maxlba + 1)); 9014 ctl_done((union ctl_io *)ctsio); 9015 return (CTL_RETVAL_COMPLETE); 9016 } 9017 9018 /* 9019 * According to SBC-3, a transfer length of 0 is not an error. 9020 */ 9021 if (num_blocks == 0) { 9022 ctl_set_success(ctsio); 9023 ctl_done((union ctl_io *)ctsio); 9024 return (CTL_RETVAL_COMPLETE); 9025 } 9026 9027 /* Set FUA if write cache is disabled. */ 9028 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 9029 flags |= CTL_LLF_FUA; 9030 9031 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9032 ctsio->kern_rel_offset = 0; 9033 9034 /* 9035 * Set the IO_CONT flag, so that if this I/O gets passed to 9036 * ctl_data_submit_done(), it'll get passed back to 9037 * ctl_ctl_cnw_cont() for further processing. 9038 */ 9039 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9040 ctsio->io_cont = ctl_cnw_cont; 9041 9042 lbalen = (struct ctl_lba_len_flags *) 9043 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9044 lbalen->lba = lba; 9045 lbalen->len = num_blocks; 9046 lbalen->flags = CTL_LLF_COMPARE | flags; 9047 9048 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9049 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9050 return (retval); 9051 } 9052 9053 int 9054 ctl_verify(struct ctl_scsiio *ctsio) 9055 { 9056 struct ctl_lun *lun = CTL_LUN(ctsio); 9057 struct ctl_lba_len_flags *lbalen; 9058 uint64_t lba; 9059 uint32_t num_blocks; 9060 int bytchk, flags; 9061 int retval; 9062 9063 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9064 9065 bytchk = 0; 9066 flags = CTL_LLF_FUA; 9067 switch (ctsio->cdb[0]) { 9068 case VERIFY_10: { 9069 struct scsi_verify_10 *cdb; 9070 9071 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9072 if (cdb->byte2 & SVFY_BYTCHK) 9073 bytchk = 1; 9074 if (cdb->byte2 & SVFY_DPO) 9075 flags |= CTL_LLF_DPO; 9076 lba = scsi_4btoul(cdb->addr); 9077 num_blocks = scsi_2btoul(cdb->length); 9078 break; 9079 } 9080 case VERIFY_12: { 9081 struct scsi_verify_12 *cdb; 9082 9083 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9084 if (cdb->byte2 & SVFY_BYTCHK) 9085 bytchk = 1; 9086 if (cdb->byte2 & SVFY_DPO) 9087 flags |= CTL_LLF_DPO; 9088 lba = scsi_4btoul(cdb->addr); 9089 num_blocks = scsi_4btoul(cdb->length); 9090 break; 9091 } 9092 case VERIFY_16: { 9093 struct scsi_rw_16 *cdb; 9094 9095 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9096 if (cdb->byte2 & SVFY_BYTCHK) 9097 bytchk = 1; 9098 if (cdb->byte2 & SVFY_DPO) 9099 flags |= CTL_LLF_DPO; 9100 lba = scsi_8btou64(cdb->addr); 9101 num_blocks = scsi_4btoul(cdb->length); 9102 break; 9103 } 9104 default: 9105 /* 9106 * We got a command we don't support. This shouldn't 9107 * happen, commands should be filtered out above us. 9108 */ 9109 ctl_set_invalid_opcode(ctsio); 9110 ctl_done((union ctl_io *)ctsio); 9111 return (CTL_RETVAL_COMPLETE); 9112 } 9113 9114 /* 9115 * The first check is to make sure we're in bounds, the second 9116 * check is to catch wrap-around problems. If the lba + num blocks 9117 * is less than the lba, then we've wrapped around and the block 9118 * range is invalid anyway. 9119 */ 9120 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9121 || ((lba + num_blocks) < lba)) { 9122 ctl_set_lba_out_of_range(ctsio, 9123 MAX(lba, lun->be_lun->maxlba + 1)); 9124 ctl_done((union ctl_io *)ctsio); 9125 return (CTL_RETVAL_COMPLETE); 9126 } 9127 9128 /* 9129 * According to SBC-3, a transfer length of 0 is not an error. 9130 */ 9131 if (num_blocks == 0) { 9132 ctl_set_success(ctsio); 9133 ctl_done((union ctl_io *)ctsio); 9134 return (CTL_RETVAL_COMPLETE); 9135 } 9136 9137 lbalen = (struct ctl_lba_len_flags *) 9138 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9139 lbalen->lba = lba; 9140 lbalen->len = num_blocks; 9141 if (bytchk) { 9142 lbalen->flags = CTL_LLF_COMPARE | flags; 9143 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9144 } else { 9145 lbalen->flags = CTL_LLF_VERIFY | flags; 9146 ctsio->kern_total_len = 0; 9147 } 9148 ctsio->kern_rel_offset = 0; 9149 9150 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9151 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9152 return (retval); 9153 } 9154 9155 int 9156 ctl_report_luns(struct ctl_scsiio *ctsio) 9157 { 9158 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9159 struct ctl_port *port = CTL_PORT(ctsio); 9160 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9161 struct scsi_report_luns *cdb; 9162 struct scsi_report_luns_data *lun_data; 9163 int num_filled, num_luns, num_port_luns, retval; 9164 uint32_t alloc_len, lun_datalen; 9165 uint32_t initidx, targ_lun_id, lun_id; 9166 9167 retval = CTL_RETVAL_COMPLETE; 9168 cdb = (struct scsi_report_luns *)ctsio->cdb; 9169 9170 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9171 9172 num_luns = 0; 9173 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9174 mtx_lock(&softc->ctl_lock); 9175 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9176 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9177 num_luns++; 9178 } 9179 mtx_unlock(&softc->ctl_lock); 9180 9181 switch (cdb->select_report) { 9182 case RPL_REPORT_DEFAULT: 9183 case RPL_REPORT_ALL: 9184 case RPL_REPORT_NONSUBSID: 9185 break; 9186 case RPL_REPORT_WELLKNOWN: 9187 case RPL_REPORT_ADMIN: 9188 case RPL_REPORT_CONGLOM: 9189 num_luns = 0; 9190 break; 9191 default: 9192 ctl_set_invalid_field(ctsio, 9193 /*sks_valid*/ 1, 9194 /*command*/ 1, 9195 /*field*/ 2, 9196 /*bit_valid*/ 0, 9197 /*bit*/ 0); 9198 ctl_done((union ctl_io *)ctsio); 9199 return (retval); 9200 break; /* NOTREACHED */ 9201 } 9202 9203 alloc_len = scsi_4btoul(cdb->length); 9204 /* 9205 * The initiator has to allocate at least 16 bytes for this request, 9206 * so he can at least get the header and the first LUN. Otherwise 9207 * we reject the request (per SPC-3 rev 14, section 6.21). 9208 */ 9209 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9210 sizeof(struct scsi_report_luns_lundata))) { 9211 ctl_set_invalid_field(ctsio, 9212 /*sks_valid*/ 1, 9213 /*command*/ 1, 9214 /*field*/ 6, 9215 /*bit_valid*/ 0, 9216 /*bit*/ 0); 9217 ctl_done((union ctl_io *)ctsio); 9218 return (retval); 9219 } 9220 9221 lun_datalen = sizeof(*lun_data) + 9222 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9223 9224 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9225 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9226 ctsio->kern_sg_entries = 0; 9227 9228 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9229 9230 mtx_lock(&softc->ctl_lock); 9231 for (targ_lun_id = 0, num_filled = 0; 9232 targ_lun_id < num_port_luns && num_filled < num_luns; 9233 targ_lun_id++) { 9234 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9235 if (lun_id == UINT32_MAX) 9236 continue; 9237 lun = softc->ctl_luns[lun_id]; 9238 if (lun == NULL) 9239 continue; 9240 9241 be64enc(lun_data->luns[num_filled++].lundata, 9242 ctl_encode_lun(targ_lun_id)); 9243 9244 /* 9245 * According to SPC-3, rev 14 section 6.21: 9246 * 9247 * "The execution of a REPORT LUNS command to any valid and 9248 * installed logical unit shall clear the REPORTED LUNS DATA 9249 * HAS CHANGED unit attention condition for all logical 9250 * units of that target with respect to the requesting 9251 * initiator. A valid and installed logical unit is one 9252 * having a PERIPHERAL QUALIFIER of 000b in the standard 9253 * INQUIRY data (see 6.4.2)." 9254 * 9255 * If request_lun is NULL, the LUN this report luns command 9256 * was issued to is either disabled or doesn't exist. In that 9257 * case, we shouldn't clear any pending lun change unit 9258 * attention. 9259 */ 9260 if (request_lun != NULL) { 9261 mtx_lock(&lun->lun_lock); 9262 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9263 mtx_unlock(&lun->lun_lock); 9264 } 9265 } 9266 mtx_unlock(&softc->ctl_lock); 9267 9268 /* 9269 * It's quite possible that we've returned fewer LUNs than we allocated 9270 * space for. Trim it. 9271 */ 9272 lun_datalen = sizeof(*lun_data) + 9273 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9274 ctsio->kern_rel_offset = 0; 9275 ctsio->kern_sg_entries = 0; 9276 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9277 ctsio->kern_total_len = ctsio->kern_data_len; 9278 9279 /* 9280 * We set this to the actual data length, regardless of how much 9281 * space we actually have to return results. If the user looks at 9282 * this value, he'll know whether or not he allocated enough space 9283 * and reissue the command if necessary. We don't support well 9284 * known logical units, so if the user asks for that, return none. 9285 */ 9286 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9287 9288 /* 9289 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9290 * this request. 9291 */ 9292 ctl_set_success(ctsio); 9293 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9294 ctsio->be_move_done = ctl_config_move_done; 9295 ctl_datamove((union ctl_io *)ctsio); 9296 return (retval); 9297 } 9298 9299 int 9300 ctl_request_sense(struct ctl_scsiio *ctsio) 9301 { 9302 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9303 struct ctl_lun *lun = CTL_LUN(ctsio); 9304 struct scsi_request_sense *cdb; 9305 struct scsi_sense_data *sense_ptr, *ps; 9306 uint32_t initidx; 9307 int have_error; 9308 u_int sense_len = SSD_FULL_SIZE; 9309 scsi_sense_data_type sense_format; 9310 ctl_ua_type ua_type; 9311 uint8_t asc = 0, ascq = 0; 9312 9313 cdb = (struct scsi_request_sense *)ctsio->cdb; 9314 9315 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9316 9317 /* 9318 * Determine which sense format the user wants. 9319 */ 9320 if (cdb->byte2 & SRS_DESC) 9321 sense_format = SSD_TYPE_DESC; 9322 else 9323 sense_format = SSD_TYPE_FIXED; 9324 9325 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9326 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9327 ctsio->kern_sg_entries = 0; 9328 ctsio->kern_rel_offset = 0; 9329 9330 /* 9331 * struct scsi_sense_data, which is currently set to 256 bytes, is 9332 * larger than the largest allowed value for the length field in the 9333 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9334 */ 9335 ctsio->kern_data_len = cdb->length; 9336 ctsio->kern_total_len = cdb->length; 9337 9338 /* 9339 * If we don't have a LUN, we don't have any pending sense. 9340 */ 9341 if (lun == NULL || 9342 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9343 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9344 /* "Logical unit not supported" */ 9345 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9346 /*current_error*/ 1, 9347 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9348 /*asc*/ 0x25, 9349 /*ascq*/ 0x00, 9350 SSD_ELEM_NONE); 9351 goto send; 9352 } 9353 9354 have_error = 0; 9355 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9356 /* 9357 * Check for pending sense, and then for pending unit attentions. 9358 * Pending sense gets returned first, then pending unit attentions. 9359 */ 9360 mtx_lock(&lun->lun_lock); 9361 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9362 if (ps != NULL) 9363 ps += initidx % CTL_MAX_INIT_PER_PORT; 9364 if (ps != NULL && ps->error_code != 0) { 9365 scsi_sense_data_type stored_format; 9366 9367 /* 9368 * Check to see which sense format was used for the stored 9369 * sense data. 9370 */ 9371 stored_format = scsi_sense_type(ps); 9372 9373 /* 9374 * If the user requested a different sense format than the 9375 * one we stored, then we need to convert it to the other 9376 * format. If we're going from descriptor to fixed format 9377 * sense data, we may lose things in translation, depending 9378 * on what options were used. 9379 * 9380 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9381 * for some reason we'll just copy it out as-is. 9382 */ 9383 if ((stored_format == SSD_TYPE_FIXED) 9384 && (sense_format == SSD_TYPE_DESC)) 9385 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9386 ps, (struct scsi_sense_data_desc *)sense_ptr); 9387 else if ((stored_format == SSD_TYPE_DESC) 9388 && (sense_format == SSD_TYPE_FIXED)) 9389 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9390 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9391 else 9392 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9393 9394 ps->error_code = 0; 9395 have_error = 1; 9396 } else { 9397 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9398 sense_format); 9399 if (ua_type != CTL_UA_NONE) 9400 have_error = 1; 9401 } 9402 if (have_error == 0) { 9403 /* 9404 * Report informational exception if have one and allowed. 9405 */ 9406 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9407 asc = lun->ie_asc; 9408 ascq = lun->ie_ascq; 9409 } 9410 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9411 /*current_error*/ 1, 9412 /*sense_key*/ SSD_KEY_NO_SENSE, 9413 /*asc*/ asc, 9414 /*ascq*/ ascq, 9415 SSD_ELEM_NONE); 9416 } 9417 mtx_unlock(&lun->lun_lock); 9418 9419 send: 9420 /* 9421 * We report the SCSI status as OK, since the status of the command 9422 * itself is OK. We're reporting sense as parameter data. 9423 */ 9424 ctl_set_success(ctsio); 9425 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9426 ctsio->be_move_done = ctl_config_move_done; 9427 ctl_datamove((union ctl_io *)ctsio); 9428 return (CTL_RETVAL_COMPLETE); 9429 } 9430 9431 int 9432 ctl_tur(struct ctl_scsiio *ctsio) 9433 { 9434 9435 CTL_DEBUG_PRINT(("ctl_tur\n")); 9436 9437 ctl_set_success(ctsio); 9438 ctl_done((union ctl_io *)ctsio); 9439 9440 return (CTL_RETVAL_COMPLETE); 9441 } 9442 9443 /* 9444 * SCSI VPD page 0x00, the Supported VPD Pages page. 9445 */ 9446 static int 9447 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9448 { 9449 struct ctl_lun *lun = CTL_LUN(ctsio); 9450 struct scsi_vpd_supported_pages *pages; 9451 int sup_page_size; 9452 int p; 9453 9454 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9455 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9456 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9457 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9458 ctsio->kern_rel_offset = 0; 9459 ctsio->kern_sg_entries = 0; 9460 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9461 ctsio->kern_total_len = ctsio->kern_data_len; 9462 9463 /* 9464 * The control device is always connected. The disk device, on the 9465 * other hand, may not be online all the time. Need to change this 9466 * to figure out whether the disk device is actually online or not. 9467 */ 9468 if (lun != NULL) 9469 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9470 lun->be_lun->lun_type; 9471 else 9472 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9473 9474 p = 0; 9475 /* Supported VPD pages */ 9476 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9477 /* Serial Number */ 9478 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9479 /* Device Identification */ 9480 pages->page_list[p++] = SVPD_DEVICE_ID; 9481 /* Extended INQUIRY Data */ 9482 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9483 /* Mode Page Policy */ 9484 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9485 /* SCSI Ports */ 9486 pages->page_list[p++] = SVPD_SCSI_PORTS; 9487 /* Third-party Copy */ 9488 pages->page_list[p++] = SVPD_SCSI_TPC; 9489 /* SCSI Feature Sets */ 9490 pages->page_list[p++] = SVPD_SCSI_SFS; 9491 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9492 /* Block limits */ 9493 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9494 /* Block Device Characteristics */ 9495 pages->page_list[p++] = SVPD_BDC; 9496 /* Logical Block Provisioning */ 9497 pages->page_list[p++] = SVPD_LBP; 9498 } 9499 pages->length = p; 9500 9501 ctl_set_success(ctsio); 9502 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9503 ctsio->be_move_done = ctl_config_move_done; 9504 ctl_datamove((union ctl_io *)ctsio); 9505 return (CTL_RETVAL_COMPLETE); 9506 } 9507 9508 /* 9509 * SCSI VPD page 0x80, the Unit Serial Number page. 9510 */ 9511 static int 9512 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9513 { 9514 struct ctl_lun *lun = CTL_LUN(ctsio); 9515 struct scsi_vpd_unit_serial_number *sn_ptr; 9516 int data_len; 9517 9518 data_len = 4 + CTL_SN_LEN; 9519 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9520 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9521 ctsio->kern_rel_offset = 0; 9522 ctsio->kern_sg_entries = 0; 9523 ctsio->kern_data_len = min(data_len, alloc_len); 9524 ctsio->kern_total_len = ctsio->kern_data_len; 9525 9526 /* 9527 * The control device is always connected. The disk device, on the 9528 * other hand, may not be online all the time. Need to change this 9529 * to figure out whether the disk device is actually online or not. 9530 */ 9531 if (lun != NULL) 9532 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9533 lun->be_lun->lun_type; 9534 else 9535 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9536 9537 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9538 sn_ptr->length = CTL_SN_LEN; 9539 /* 9540 * If we don't have a LUN, we just leave the serial number as 9541 * all spaces. 9542 */ 9543 if (lun != NULL) { 9544 strncpy((char *)sn_ptr->serial_num, 9545 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9546 } else 9547 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9548 9549 ctl_set_success(ctsio); 9550 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9551 ctsio->be_move_done = ctl_config_move_done; 9552 ctl_datamove((union ctl_io *)ctsio); 9553 return (CTL_RETVAL_COMPLETE); 9554 } 9555 9556 /* 9557 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9558 */ 9559 static int 9560 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9561 { 9562 struct ctl_lun *lun = CTL_LUN(ctsio); 9563 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9564 int data_len; 9565 9566 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9567 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9568 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9569 ctsio->kern_sg_entries = 0; 9570 ctsio->kern_rel_offset = 0; 9571 ctsio->kern_data_len = min(data_len, alloc_len); 9572 ctsio->kern_total_len = ctsio->kern_data_len; 9573 9574 /* 9575 * The control device is always connected. The disk device, on the 9576 * other hand, may not be online all the time. 9577 */ 9578 if (lun != NULL) 9579 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9580 lun->be_lun->lun_type; 9581 else 9582 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9583 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9584 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9585 /* 9586 * We support head of queue, ordered and simple tags. 9587 */ 9588 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9589 /* 9590 * Volatile cache supported. 9591 */ 9592 eid_ptr->flags3 = SVPD_EID_V_SUP; 9593 9594 /* 9595 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9596 * attention for a particular IT nexus on all LUNs once we report 9597 * it to that nexus once. This bit is required as of SPC-4. 9598 */ 9599 eid_ptr->flags4 = SVPD_EID_LUICLR; 9600 9601 /* 9602 * We support revert to defaults (RTD) bit in MODE SELECT. 9603 */ 9604 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9605 9606 /* 9607 * XXX KDM in order to correctly answer this, we would need 9608 * information from the SIM to determine how much sense data it 9609 * can send. So this would really be a path inquiry field, most 9610 * likely. This can be set to a maximum of 252 according to SPC-4, 9611 * but the hardware may or may not be able to support that much. 9612 * 0 just means that the maximum sense data length is not reported. 9613 */ 9614 eid_ptr->max_sense_length = 0; 9615 9616 ctl_set_success(ctsio); 9617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9618 ctsio->be_move_done = ctl_config_move_done; 9619 ctl_datamove((union ctl_io *)ctsio); 9620 return (CTL_RETVAL_COMPLETE); 9621 } 9622 9623 static int 9624 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9625 { 9626 struct ctl_lun *lun = CTL_LUN(ctsio); 9627 struct scsi_vpd_mode_page_policy *mpp_ptr; 9628 int data_len; 9629 9630 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9631 sizeof(struct scsi_vpd_mode_page_policy_descr); 9632 9633 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9634 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9635 ctsio->kern_rel_offset = 0; 9636 ctsio->kern_sg_entries = 0; 9637 ctsio->kern_data_len = min(data_len, alloc_len); 9638 ctsio->kern_total_len = ctsio->kern_data_len; 9639 9640 /* 9641 * The control device is always connected. The disk device, on the 9642 * other hand, may not be online all the time. 9643 */ 9644 if (lun != NULL) 9645 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9646 lun->be_lun->lun_type; 9647 else 9648 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9649 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9650 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9651 mpp_ptr->descr[0].page_code = 0x3f; 9652 mpp_ptr->descr[0].subpage_code = 0xff; 9653 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9654 9655 ctl_set_success(ctsio); 9656 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9657 ctsio->be_move_done = ctl_config_move_done; 9658 ctl_datamove((union ctl_io *)ctsio); 9659 return (CTL_RETVAL_COMPLETE); 9660 } 9661 9662 /* 9663 * SCSI VPD page 0x83, the Device Identification page. 9664 */ 9665 static int 9666 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9667 { 9668 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9669 struct ctl_port *port = CTL_PORT(ctsio); 9670 struct ctl_lun *lun = CTL_LUN(ctsio); 9671 struct scsi_vpd_device_id *devid_ptr; 9672 struct scsi_vpd_id_descriptor *desc; 9673 int data_len, g; 9674 uint8_t proto; 9675 9676 data_len = sizeof(struct scsi_vpd_device_id) + 9677 sizeof(struct scsi_vpd_id_descriptor) + 9678 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9679 sizeof(struct scsi_vpd_id_descriptor) + 9680 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9681 if (lun && lun->lun_devid) 9682 data_len += lun->lun_devid->len; 9683 if (port && port->port_devid) 9684 data_len += port->port_devid->len; 9685 if (port && port->target_devid) 9686 data_len += port->target_devid->len; 9687 9688 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9689 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9690 ctsio->kern_sg_entries = 0; 9691 ctsio->kern_rel_offset = 0; 9692 ctsio->kern_sg_entries = 0; 9693 ctsio->kern_data_len = min(data_len, alloc_len); 9694 ctsio->kern_total_len = ctsio->kern_data_len; 9695 9696 /* 9697 * The control device is always connected. The disk device, on the 9698 * other hand, may not be online all the time. 9699 */ 9700 if (lun != NULL) 9701 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9702 lun->be_lun->lun_type; 9703 else 9704 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9705 devid_ptr->page_code = SVPD_DEVICE_ID; 9706 scsi_ulto2b(data_len - 4, devid_ptr->length); 9707 9708 if (port && port->port_type == CTL_PORT_FC) 9709 proto = SCSI_PROTO_FC << 4; 9710 else if (port && port->port_type == CTL_PORT_SAS) 9711 proto = SCSI_PROTO_SAS << 4; 9712 else if (port && port->port_type == CTL_PORT_ISCSI) 9713 proto = SCSI_PROTO_ISCSI << 4; 9714 else 9715 proto = SCSI_PROTO_SPI << 4; 9716 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9717 9718 /* 9719 * We're using a LUN association here. i.e., this device ID is a 9720 * per-LUN identifier. 9721 */ 9722 if (lun && lun->lun_devid) { 9723 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9724 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9725 lun->lun_devid->len); 9726 } 9727 9728 /* 9729 * This is for the WWPN which is a port association. 9730 */ 9731 if (port && port->port_devid) { 9732 memcpy(desc, port->port_devid->data, port->port_devid->len); 9733 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9734 port->port_devid->len); 9735 } 9736 9737 /* 9738 * This is for the Relative Target Port(type 4h) identifier 9739 */ 9740 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9741 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9742 SVPD_ID_TYPE_RELTARG; 9743 desc->length = 4; 9744 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9745 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9746 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9747 9748 /* 9749 * This is for the Target Port Group(type 5h) identifier 9750 */ 9751 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9752 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9753 SVPD_ID_TYPE_TPORTGRP; 9754 desc->length = 4; 9755 if (softc->is_single || 9756 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9757 g = 1; 9758 else 9759 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9760 scsi_ulto2b(g, &desc->identifier[2]); 9761 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9762 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9763 9764 /* 9765 * This is for the Target identifier 9766 */ 9767 if (port && port->target_devid) { 9768 memcpy(desc, port->target_devid->data, port->target_devid->len); 9769 } 9770 9771 ctl_set_success(ctsio); 9772 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9773 ctsio->be_move_done = ctl_config_move_done; 9774 ctl_datamove((union ctl_io *)ctsio); 9775 return (CTL_RETVAL_COMPLETE); 9776 } 9777 9778 static int 9779 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9780 { 9781 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9782 struct ctl_lun *lun = CTL_LUN(ctsio); 9783 struct scsi_vpd_scsi_ports *sp; 9784 struct scsi_vpd_port_designation *pd; 9785 struct scsi_vpd_port_designation_cont *pdc; 9786 struct ctl_port *port; 9787 int data_len, num_target_ports, iid_len, id_len; 9788 9789 num_target_ports = 0; 9790 iid_len = 0; 9791 id_len = 0; 9792 mtx_lock(&softc->ctl_lock); 9793 STAILQ_FOREACH(port, &softc->port_list, links) { 9794 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9795 continue; 9796 if (lun != NULL && 9797 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9798 continue; 9799 num_target_ports++; 9800 if (port->init_devid) 9801 iid_len += port->init_devid->len; 9802 if (port->port_devid) 9803 id_len += port->port_devid->len; 9804 } 9805 mtx_unlock(&softc->ctl_lock); 9806 9807 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9808 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9809 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9810 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9811 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9812 ctsio->kern_sg_entries = 0; 9813 ctsio->kern_rel_offset = 0; 9814 ctsio->kern_sg_entries = 0; 9815 ctsio->kern_data_len = min(data_len, alloc_len); 9816 ctsio->kern_total_len = ctsio->kern_data_len; 9817 9818 /* 9819 * The control device is always connected. The disk device, on the 9820 * other hand, may not be online all the time. Need to change this 9821 * to figure out whether the disk device is actually online or not. 9822 */ 9823 if (lun != NULL) 9824 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9825 lun->be_lun->lun_type; 9826 else 9827 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9828 9829 sp->page_code = SVPD_SCSI_PORTS; 9830 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9831 sp->page_length); 9832 pd = &sp->design[0]; 9833 9834 mtx_lock(&softc->ctl_lock); 9835 STAILQ_FOREACH(port, &softc->port_list, links) { 9836 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9837 continue; 9838 if (lun != NULL && 9839 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9840 continue; 9841 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9842 if (port->init_devid) { 9843 iid_len = port->init_devid->len; 9844 memcpy(pd->initiator_transportid, 9845 port->init_devid->data, port->init_devid->len); 9846 } else 9847 iid_len = 0; 9848 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9849 pdc = (struct scsi_vpd_port_designation_cont *) 9850 (&pd->initiator_transportid[iid_len]); 9851 if (port->port_devid) { 9852 id_len = port->port_devid->len; 9853 memcpy(pdc->target_port_descriptors, 9854 port->port_devid->data, port->port_devid->len); 9855 } else 9856 id_len = 0; 9857 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9858 pd = (struct scsi_vpd_port_designation *) 9859 ((uint8_t *)pdc->target_port_descriptors + id_len); 9860 } 9861 mtx_unlock(&softc->ctl_lock); 9862 9863 ctl_set_success(ctsio); 9864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9865 ctsio->be_move_done = ctl_config_move_done; 9866 ctl_datamove((union ctl_io *)ctsio); 9867 return (CTL_RETVAL_COMPLETE); 9868 } 9869 9870 static int 9871 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len) 9872 { 9873 struct ctl_lun *lun = CTL_LUN(ctsio); 9874 struct scsi_vpd_sfs *sfs_ptr; 9875 int sfs_page_size, n; 9876 9877 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2; 9878 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO); 9879 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr; 9880 ctsio->kern_sg_entries = 0; 9881 ctsio->kern_rel_offset = 0; 9882 ctsio->kern_sg_entries = 0; 9883 ctsio->kern_data_len = min(sfs_page_size, alloc_len); 9884 ctsio->kern_total_len = ctsio->kern_data_len; 9885 9886 /* 9887 * The control device is always connected. The disk device, on the 9888 * other hand, may not be online all the time. Need to change this 9889 * to figure out whether the disk device is actually online or not. 9890 */ 9891 if (lun != NULL) 9892 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9893 lun->be_lun->lun_type; 9894 else 9895 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9896 9897 sfs_ptr->page_code = SVPD_SCSI_SFS; 9898 n = 0; 9899 /* Discovery 2016 */ 9900 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]); 9901 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9902 /* SBC Base 2016 */ 9903 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]); 9904 /* SBC Base 2010 */ 9905 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]); 9906 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9907 /* Basic Provisioning 2016 */ 9908 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]); 9909 } 9910 /* Drive Maintenance 2016 */ 9911 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]); 9912 } 9913 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length); 9914 9915 ctl_set_success(ctsio); 9916 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9917 ctsio->be_move_done = ctl_config_move_done; 9918 ctl_datamove((union ctl_io *)ctsio); 9919 return (CTL_RETVAL_COMPLETE); 9920 } 9921 9922 static int 9923 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9924 { 9925 struct ctl_lun *lun = CTL_LUN(ctsio); 9926 struct scsi_vpd_block_limits *bl_ptr; 9927 const char *val; 9928 uint64_t ival; 9929 9930 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9931 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9932 ctsio->kern_sg_entries = 0; 9933 ctsio->kern_rel_offset = 0; 9934 ctsio->kern_sg_entries = 0; 9935 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9936 ctsio->kern_total_len = ctsio->kern_data_len; 9937 9938 /* 9939 * The control device is always connected. The disk device, on the 9940 * other hand, may not be online all the time. Need to change this 9941 * to figure out whether the disk device is actually online or not. 9942 */ 9943 if (lun != NULL) 9944 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9945 lun->be_lun->lun_type; 9946 else 9947 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9948 9949 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9950 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9951 bl_ptr->max_cmp_write_len = 0xff; 9952 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9953 if (lun != NULL) { 9954 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9955 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9956 ival = 0xffffffff; 9957 val = dnvlist_get_string(lun->be_lun->options, 9958 "unmap_max_lba", NULL); 9959 if (val != NULL) 9960 ctl_expand_number(val, &ival); 9961 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9962 ival = 0xffffffff; 9963 val = dnvlist_get_string(lun->be_lun->options, 9964 "unmap_max_descr", NULL); 9965 if (val != NULL) 9966 ctl_expand_number(val, &ival); 9967 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9968 if (lun->be_lun->ublockexp != 0) { 9969 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9970 bl_ptr->opt_unmap_grain); 9971 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9972 bl_ptr->unmap_grain_align); 9973 } 9974 } 9975 scsi_ulto4b(lun->be_lun->atomicblock, 9976 bl_ptr->max_atomic_transfer_length); 9977 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9978 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9979 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9980 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9981 ival = UINT64_MAX; 9982 val = dnvlist_get_string(lun->be_lun->options, 9983 "write_same_max_lba", NULL); 9984 if (val != NULL) 9985 ctl_expand_number(val, &ival); 9986 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9987 if (lun->be_lun->maxlba + 1 > ival) 9988 bl_ptr->flags |= SVPD_BL_WSNZ; 9989 } 9990 9991 ctl_set_success(ctsio); 9992 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9993 ctsio->be_move_done = ctl_config_move_done; 9994 ctl_datamove((union ctl_io *)ctsio); 9995 return (CTL_RETVAL_COMPLETE); 9996 } 9997 9998 static int 9999 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10000 { 10001 struct ctl_lun *lun = CTL_LUN(ctsio); 10002 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10003 const char *value; 10004 u_int i; 10005 10006 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10007 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10008 ctsio->kern_sg_entries = 0; 10009 ctsio->kern_rel_offset = 0; 10010 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 10011 ctsio->kern_total_len = ctsio->kern_data_len; 10012 10013 /* 10014 * The control device is always connected. The disk device, on the 10015 * other hand, may not be online all the time. Need to change this 10016 * to figure out whether the disk device is actually online or not. 10017 */ 10018 if (lun != NULL) 10019 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10020 lun->be_lun->lun_type; 10021 else 10022 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10023 bdc_ptr->page_code = SVPD_BDC; 10024 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10025 if (lun != NULL && 10026 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 10027 i = strtol(value, NULL, 0); 10028 else 10029 i = CTL_DEFAULT_ROTATION_RATE; 10030 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10031 if (lun != NULL && 10032 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 10033 i = strtol(value, NULL, 0); 10034 else 10035 i = 0; 10036 bdc_ptr->wab_wac_ff = (i & 0x0f); 10037 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS; 10038 10039 ctl_set_success(ctsio); 10040 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10041 ctsio->be_move_done = ctl_config_move_done; 10042 ctl_datamove((union ctl_io *)ctsio); 10043 return (CTL_RETVAL_COMPLETE); 10044 } 10045 10046 static int 10047 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10048 { 10049 struct ctl_lun *lun = CTL_LUN(ctsio); 10050 struct scsi_vpd_logical_block_prov *lbp_ptr; 10051 const char *value; 10052 10053 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10054 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10055 ctsio->kern_sg_entries = 0; 10056 ctsio->kern_rel_offset = 0; 10057 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 10058 ctsio->kern_total_len = ctsio->kern_data_len; 10059 10060 /* 10061 * The control device is always connected. The disk device, on the 10062 * other hand, may not be online all the time. Need to change this 10063 * to figure out whether the disk device is actually online or not. 10064 */ 10065 if (lun != NULL) 10066 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10067 lun->be_lun->lun_type; 10068 else 10069 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10070 10071 lbp_ptr->page_code = SVPD_LBP; 10072 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10073 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10074 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10075 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10076 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10077 value = dnvlist_get_string(lun->be_lun->options, 10078 "provisioning_type", NULL); 10079 if (value != NULL) { 10080 if (strcmp(value, "resource") == 0) 10081 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10082 else if (strcmp(value, "thin") == 0) 10083 lbp_ptr->prov_type = SVPD_LBP_THIN; 10084 } else 10085 lbp_ptr->prov_type = SVPD_LBP_THIN; 10086 } 10087 10088 ctl_set_success(ctsio); 10089 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10090 ctsio->be_move_done = ctl_config_move_done; 10091 ctl_datamove((union ctl_io *)ctsio); 10092 return (CTL_RETVAL_COMPLETE); 10093 } 10094 10095 /* 10096 * INQUIRY with the EVPD bit set. 10097 */ 10098 static int 10099 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10100 { 10101 struct ctl_lun *lun = CTL_LUN(ctsio); 10102 struct scsi_inquiry *cdb; 10103 int alloc_len, retval; 10104 10105 cdb = (struct scsi_inquiry *)ctsio->cdb; 10106 alloc_len = scsi_2btoul(cdb->length); 10107 10108 switch (cdb->page_code) { 10109 case SVPD_SUPPORTED_PAGES: 10110 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10111 break; 10112 case SVPD_UNIT_SERIAL_NUMBER: 10113 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10114 break; 10115 case SVPD_DEVICE_ID: 10116 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10117 break; 10118 case SVPD_EXTENDED_INQUIRY_DATA: 10119 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10120 break; 10121 case SVPD_MODE_PAGE_POLICY: 10122 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10123 break; 10124 case SVPD_SCSI_PORTS: 10125 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10126 break; 10127 case SVPD_SCSI_TPC: 10128 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10129 break; 10130 case SVPD_SCSI_SFS: 10131 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len); 10132 break; 10133 case SVPD_BLOCK_LIMITS: 10134 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10135 goto err; 10136 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10137 break; 10138 case SVPD_BDC: 10139 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10140 goto err; 10141 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10142 break; 10143 case SVPD_LBP: 10144 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10145 goto err; 10146 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10147 break; 10148 default: 10149 err: 10150 ctl_set_invalid_field(ctsio, 10151 /*sks_valid*/ 1, 10152 /*command*/ 1, 10153 /*field*/ 2, 10154 /*bit_valid*/ 0, 10155 /*bit*/ 0); 10156 ctl_done((union ctl_io *)ctsio); 10157 retval = CTL_RETVAL_COMPLETE; 10158 break; 10159 } 10160 10161 return (retval); 10162 } 10163 10164 /* 10165 * Standard INQUIRY data. 10166 */ 10167 static int 10168 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10169 { 10170 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10171 struct ctl_port *port = CTL_PORT(ctsio); 10172 struct ctl_lun *lun = CTL_LUN(ctsio); 10173 struct scsi_inquiry_data *inq_ptr; 10174 struct scsi_inquiry *cdb; 10175 const char *val; 10176 uint32_t alloc_len, data_len; 10177 ctl_port_type port_type; 10178 10179 port_type = port->port_type; 10180 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10181 port_type = CTL_PORT_SCSI; 10182 10183 cdb = (struct scsi_inquiry *)ctsio->cdb; 10184 alloc_len = scsi_2btoul(cdb->length); 10185 10186 /* 10187 * We malloc the full inquiry data size here and fill it 10188 * in. If the user only asks for less, we'll give him 10189 * that much. 10190 */ 10191 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10192 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10193 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10194 ctsio->kern_sg_entries = 0; 10195 ctsio->kern_rel_offset = 0; 10196 ctsio->kern_data_len = min(data_len, alloc_len); 10197 ctsio->kern_total_len = ctsio->kern_data_len; 10198 10199 if (lun != NULL) { 10200 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10201 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10202 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10203 lun->be_lun->lun_type; 10204 } else { 10205 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10206 lun->be_lun->lun_type; 10207 } 10208 if (lun->flags & CTL_LUN_REMOVABLE) 10209 inq_ptr->dev_qual2 |= SID_RMB; 10210 } else 10211 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10212 10213 /* RMB in byte 2 is 0 */ 10214 inq_ptr->version = SCSI_REV_SPC5; 10215 10216 /* 10217 * According to SAM-3, even if a device only supports a single 10218 * level of LUN addressing, it should still set the HISUP bit: 10219 * 10220 * 4.9.1 Logical unit numbers overview 10221 * 10222 * All logical unit number formats described in this standard are 10223 * hierarchical in structure even when only a single level in that 10224 * hierarchy is used. The HISUP bit shall be set to one in the 10225 * standard INQUIRY data (see SPC-2) when any logical unit number 10226 * format described in this standard is used. Non-hierarchical 10227 * formats are outside the scope of this standard. 10228 * 10229 * Therefore we set the HiSup bit here. 10230 * 10231 * The response format is 2, per SPC-3. 10232 */ 10233 inq_ptr->response_format = SID_HiSup | 2; 10234 10235 inq_ptr->additional_length = data_len - 10236 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10237 CTL_DEBUG_PRINT(("additional_length = %d\n", 10238 inq_ptr->additional_length)); 10239 10240 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10241 if (port_type == CTL_PORT_SCSI) 10242 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10243 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10244 inq_ptr->flags = SID_CmdQue; 10245 if (port_type == CTL_PORT_SCSI) 10246 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10247 10248 /* 10249 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10250 * We have 8 bytes for the vendor name, and 16 bytes for the device 10251 * name and 4 bytes for the revision. 10252 */ 10253 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10254 "vendor", NULL)) == NULL) { 10255 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10256 } else { 10257 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10258 strncpy(inq_ptr->vendor, val, 10259 min(sizeof(inq_ptr->vendor), strlen(val))); 10260 } 10261 if (lun == NULL) { 10262 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10263 sizeof(inq_ptr->product)); 10264 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10265 NULL)) == NULL) { 10266 switch (lun->be_lun->lun_type) { 10267 case T_DIRECT: 10268 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10269 sizeof(inq_ptr->product)); 10270 break; 10271 case T_PROCESSOR: 10272 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10273 sizeof(inq_ptr->product)); 10274 break; 10275 case T_CDROM: 10276 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10277 sizeof(inq_ptr->product)); 10278 break; 10279 default: 10280 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10281 sizeof(inq_ptr->product)); 10282 break; 10283 } 10284 } else { 10285 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10286 strncpy(inq_ptr->product, val, 10287 min(sizeof(inq_ptr->product), strlen(val))); 10288 } 10289 10290 /* 10291 * XXX make this a macro somewhere so it automatically gets 10292 * incremented when we make changes. 10293 */ 10294 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10295 "revision", NULL)) == NULL) { 10296 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10297 } else { 10298 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10299 strncpy(inq_ptr->revision, val, 10300 min(sizeof(inq_ptr->revision), strlen(val))); 10301 } 10302 10303 /* 10304 * For parallel SCSI, we support double transition and single 10305 * transition clocking. We also support QAS (Quick Arbitration 10306 * and Selection) and Information Unit transfers on both the 10307 * control and array devices. 10308 */ 10309 if (port_type == CTL_PORT_SCSI) 10310 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10311 SID_SPI_IUS; 10312 10313 /* SAM-6 (no version claimed) */ 10314 scsi_ulto2b(0x00C0, inq_ptr->version1); 10315 /* SPC-5 (no version claimed) */ 10316 scsi_ulto2b(0x05C0, inq_ptr->version2); 10317 if (port_type == CTL_PORT_FC) { 10318 /* FCP-2 ANSI INCITS.350:2003 */ 10319 scsi_ulto2b(0x0917, inq_ptr->version3); 10320 } else if (port_type == CTL_PORT_SCSI) { 10321 /* SPI-4 ANSI INCITS.362:200x */ 10322 scsi_ulto2b(0x0B56, inq_ptr->version3); 10323 } else if (port_type == CTL_PORT_ISCSI) { 10324 /* iSCSI (no version claimed) */ 10325 scsi_ulto2b(0x0960, inq_ptr->version3); 10326 } else if (port_type == CTL_PORT_SAS) { 10327 /* SAS (no version claimed) */ 10328 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10329 } else if (port_type == CTL_PORT_UMASS) { 10330 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10331 scsi_ulto2b(0x1730, inq_ptr->version3); 10332 } 10333 10334 if (lun == NULL) { 10335 /* SBC-4 (no version claimed) */ 10336 scsi_ulto2b(0x0600, inq_ptr->version4); 10337 } else { 10338 switch (lun->be_lun->lun_type) { 10339 case T_DIRECT: 10340 /* SBC-4 (no version claimed) */ 10341 scsi_ulto2b(0x0600, inq_ptr->version4); 10342 break; 10343 case T_PROCESSOR: 10344 break; 10345 case T_CDROM: 10346 /* MMC-6 (no version claimed) */ 10347 scsi_ulto2b(0x04E0, inq_ptr->version4); 10348 break; 10349 default: 10350 break; 10351 } 10352 } 10353 10354 ctl_set_success(ctsio); 10355 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10356 ctsio->be_move_done = ctl_config_move_done; 10357 ctl_datamove((union ctl_io *)ctsio); 10358 return (CTL_RETVAL_COMPLETE); 10359 } 10360 10361 int 10362 ctl_inquiry(struct ctl_scsiio *ctsio) 10363 { 10364 struct scsi_inquiry *cdb; 10365 int retval; 10366 10367 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10368 10369 cdb = (struct scsi_inquiry *)ctsio->cdb; 10370 if (cdb->byte2 & SI_EVPD) 10371 retval = ctl_inquiry_evpd(ctsio); 10372 else if (cdb->page_code == 0) 10373 retval = ctl_inquiry_std(ctsio); 10374 else { 10375 ctl_set_invalid_field(ctsio, 10376 /*sks_valid*/ 1, 10377 /*command*/ 1, 10378 /*field*/ 2, 10379 /*bit_valid*/ 0, 10380 /*bit*/ 0); 10381 ctl_done((union ctl_io *)ctsio); 10382 return (CTL_RETVAL_COMPLETE); 10383 } 10384 10385 return (retval); 10386 } 10387 10388 int 10389 ctl_get_config(struct ctl_scsiio *ctsio) 10390 { 10391 struct ctl_lun *lun = CTL_LUN(ctsio); 10392 struct scsi_get_config_header *hdr; 10393 struct scsi_get_config_feature *feature; 10394 struct scsi_get_config *cdb; 10395 uint32_t alloc_len, data_len; 10396 int rt, starting; 10397 10398 cdb = (struct scsi_get_config *)ctsio->cdb; 10399 rt = (cdb->rt & SGC_RT_MASK); 10400 starting = scsi_2btoul(cdb->starting_feature); 10401 alloc_len = scsi_2btoul(cdb->length); 10402 10403 data_len = sizeof(struct scsi_get_config_header) + 10404 sizeof(struct scsi_get_config_feature) + 8 + 10405 sizeof(struct scsi_get_config_feature) + 8 + 10406 sizeof(struct scsi_get_config_feature) + 4 + 10407 sizeof(struct scsi_get_config_feature) + 4 + 10408 sizeof(struct scsi_get_config_feature) + 8 + 10409 sizeof(struct scsi_get_config_feature) + 10410 sizeof(struct scsi_get_config_feature) + 4 + 10411 sizeof(struct scsi_get_config_feature) + 4 + 10412 sizeof(struct scsi_get_config_feature) + 4 + 10413 sizeof(struct scsi_get_config_feature) + 4 + 10414 sizeof(struct scsi_get_config_feature) + 4 + 10415 sizeof(struct scsi_get_config_feature) + 4; 10416 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10417 ctsio->kern_sg_entries = 0; 10418 ctsio->kern_rel_offset = 0; 10419 10420 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10421 if (lun->flags & CTL_LUN_NO_MEDIA) 10422 scsi_ulto2b(0x0000, hdr->current_profile); 10423 else 10424 scsi_ulto2b(0x0010, hdr->current_profile); 10425 feature = (struct scsi_get_config_feature *)(hdr + 1); 10426 10427 if (starting > 0x003b) 10428 goto done; 10429 if (starting > 0x003a) 10430 goto f3b; 10431 if (starting > 0x002b) 10432 goto f3a; 10433 if (starting > 0x002a) 10434 goto f2b; 10435 if (starting > 0x001f) 10436 goto f2a; 10437 if (starting > 0x001e) 10438 goto f1f; 10439 if (starting > 0x001d) 10440 goto f1e; 10441 if (starting > 0x0010) 10442 goto f1d; 10443 if (starting > 0x0003) 10444 goto f10; 10445 if (starting > 0x0002) 10446 goto f3; 10447 if (starting > 0x0001) 10448 goto f2; 10449 if (starting > 0x0000) 10450 goto f1; 10451 10452 /* Profile List */ 10453 scsi_ulto2b(0x0000, feature->feature_code); 10454 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10455 feature->add_length = 8; 10456 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10457 feature->feature_data[2] = 0x00; 10458 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10459 feature->feature_data[6] = 0x01; 10460 feature = (struct scsi_get_config_feature *) 10461 &feature->feature_data[feature->add_length]; 10462 10463 f1: /* Core */ 10464 scsi_ulto2b(0x0001, feature->feature_code); 10465 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10466 feature->add_length = 8; 10467 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10468 feature->feature_data[4] = 0x03; 10469 feature = (struct scsi_get_config_feature *) 10470 &feature->feature_data[feature->add_length]; 10471 10472 f2: /* Morphing */ 10473 scsi_ulto2b(0x0002, feature->feature_code); 10474 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10475 feature->add_length = 4; 10476 feature->feature_data[0] = 0x02; 10477 feature = (struct scsi_get_config_feature *) 10478 &feature->feature_data[feature->add_length]; 10479 10480 f3: /* Removable Medium */ 10481 scsi_ulto2b(0x0003, feature->feature_code); 10482 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10483 feature->add_length = 4; 10484 feature->feature_data[0] = 0x39; 10485 feature = (struct scsi_get_config_feature *) 10486 &feature->feature_data[feature->add_length]; 10487 10488 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10489 goto done; 10490 10491 f10: /* Random Read */ 10492 scsi_ulto2b(0x0010, feature->feature_code); 10493 feature->flags = 0x00; 10494 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10495 feature->flags |= SGC_F_CURRENT; 10496 feature->add_length = 8; 10497 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10498 scsi_ulto2b(1, &feature->feature_data[4]); 10499 feature->feature_data[6] = 0x00; 10500 feature = (struct scsi_get_config_feature *) 10501 &feature->feature_data[feature->add_length]; 10502 10503 f1d: /* Multi-Read */ 10504 scsi_ulto2b(0x001D, feature->feature_code); 10505 feature->flags = 0x00; 10506 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10507 feature->flags |= SGC_F_CURRENT; 10508 feature->add_length = 0; 10509 feature = (struct scsi_get_config_feature *) 10510 &feature->feature_data[feature->add_length]; 10511 10512 f1e: /* CD Read */ 10513 scsi_ulto2b(0x001E, feature->feature_code); 10514 feature->flags = 0x00; 10515 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10516 feature->flags |= SGC_F_CURRENT; 10517 feature->add_length = 4; 10518 feature->feature_data[0] = 0x00; 10519 feature = (struct scsi_get_config_feature *) 10520 &feature->feature_data[feature->add_length]; 10521 10522 f1f: /* DVD Read */ 10523 scsi_ulto2b(0x001F, feature->feature_code); 10524 feature->flags = 0x08; 10525 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10526 feature->flags |= SGC_F_CURRENT; 10527 feature->add_length = 4; 10528 feature->feature_data[0] = 0x01; 10529 feature->feature_data[2] = 0x03; 10530 feature = (struct scsi_get_config_feature *) 10531 &feature->feature_data[feature->add_length]; 10532 10533 f2a: /* DVD+RW */ 10534 scsi_ulto2b(0x002A, feature->feature_code); 10535 feature->flags = 0x04; 10536 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10537 feature->flags |= SGC_F_CURRENT; 10538 feature->add_length = 4; 10539 feature->feature_data[0] = 0x00; 10540 feature->feature_data[1] = 0x00; 10541 feature = (struct scsi_get_config_feature *) 10542 &feature->feature_data[feature->add_length]; 10543 10544 f2b: /* DVD+R */ 10545 scsi_ulto2b(0x002B, feature->feature_code); 10546 feature->flags = 0x00; 10547 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10548 feature->flags |= SGC_F_CURRENT; 10549 feature->add_length = 4; 10550 feature->feature_data[0] = 0x00; 10551 feature = (struct scsi_get_config_feature *) 10552 &feature->feature_data[feature->add_length]; 10553 10554 f3a: /* DVD+RW Dual Layer */ 10555 scsi_ulto2b(0x003A, feature->feature_code); 10556 feature->flags = 0x00; 10557 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10558 feature->flags |= SGC_F_CURRENT; 10559 feature->add_length = 4; 10560 feature->feature_data[0] = 0x00; 10561 feature->feature_data[1] = 0x00; 10562 feature = (struct scsi_get_config_feature *) 10563 &feature->feature_data[feature->add_length]; 10564 10565 f3b: /* DVD+R Dual Layer */ 10566 scsi_ulto2b(0x003B, feature->feature_code); 10567 feature->flags = 0x00; 10568 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10569 feature->flags |= SGC_F_CURRENT; 10570 feature->add_length = 4; 10571 feature->feature_data[0] = 0x00; 10572 feature = (struct scsi_get_config_feature *) 10573 &feature->feature_data[feature->add_length]; 10574 10575 done: 10576 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10577 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10578 feature = (struct scsi_get_config_feature *)(hdr + 1); 10579 if (scsi_2btoul(feature->feature_code) == starting) 10580 feature = (struct scsi_get_config_feature *) 10581 &feature->feature_data[feature->add_length]; 10582 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10583 } 10584 scsi_ulto4b(data_len - 4, hdr->data_length); 10585 ctsio->kern_data_len = min(data_len, alloc_len); 10586 ctsio->kern_total_len = ctsio->kern_data_len; 10587 10588 ctl_set_success(ctsio); 10589 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10590 ctsio->be_move_done = ctl_config_move_done; 10591 ctl_datamove((union ctl_io *)ctsio); 10592 return (CTL_RETVAL_COMPLETE); 10593 } 10594 10595 int 10596 ctl_get_event_status(struct ctl_scsiio *ctsio) 10597 { 10598 struct scsi_get_event_status_header *hdr; 10599 struct scsi_get_event_status *cdb; 10600 uint32_t alloc_len, data_len; 10601 10602 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10603 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10604 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10605 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10606 ctl_done((union ctl_io *)ctsio); 10607 return (CTL_RETVAL_COMPLETE); 10608 } 10609 alloc_len = scsi_2btoul(cdb->length); 10610 10611 data_len = sizeof(struct scsi_get_event_status_header); 10612 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10613 ctsio->kern_sg_entries = 0; 10614 ctsio->kern_rel_offset = 0; 10615 ctsio->kern_data_len = min(data_len, alloc_len); 10616 ctsio->kern_total_len = ctsio->kern_data_len; 10617 10618 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10619 scsi_ulto2b(0, hdr->descr_length); 10620 hdr->nea_class = SGESN_NEA; 10621 hdr->supported_class = 0; 10622 10623 ctl_set_success(ctsio); 10624 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10625 ctsio->be_move_done = ctl_config_move_done; 10626 ctl_datamove((union ctl_io *)ctsio); 10627 return (CTL_RETVAL_COMPLETE); 10628 } 10629 10630 int 10631 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10632 { 10633 struct scsi_mechanism_status_header *hdr; 10634 struct scsi_mechanism_status *cdb; 10635 uint32_t alloc_len, data_len; 10636 10637 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10638 alloc_len = scsi_2btoul(cdb->length); 10639 10640 data_len = sizeof(struct scsi_mechanism_status_header); 10641 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10642 ctsio->kern_sg_entries = 0; 10643 ctsio->kern_rel_offset = 0; 10644 ctsio->kern_data_len = min(data_len, alloc_len); 10645 ctsio->kern_total_len = ctsio->kern_data_len; 10646 10647 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10648 hdr->state1 = 0x00; 10649 hdr->state2 = 0xe0; 10650 scsi_ulto3b(0, hdr->lba); 10651 hdr->slots_num = 0; 10652 scsi_ulto2b(0, hdr->slots_length); 10653 10654 ctl_set_success(ctsio); 10655 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10656 ctsio->be_move_done = ctl_config_move_done; 10657 ctl_datamove((union ctl_io *)ctsio); 10658 return (CTL_RETVAL_COMPLETE); 10659 } 10660 10661 static void 10662 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10663 { 10664 10665 lba += 150; 10666 buf[0] = 0; 10667 buf[1] = bin2bcd((lba / 75) / 60); 10668 buf[2] = bin2bcd((lba / 75) % 60); 10669 buf[3] = bin2bcd(lba % 75); 10670 } 10671 10672 int 10673 ctl_read_toc(struct ctl_scsiio *ctsio) 10674 { 10675 struct ctl_lun *lun = CTL_LUN(ctsio); 10676 struct scsi_read_toc_hdr *hdr; 10677 struct scsi_read_toc_type01_descr *descr; 10678 struct scsi_read_toc *cdb; 10679 uint32_t alloc_len, data_len; 10680 int format, msf; 10681 10682 cdb = (struct scsi_read_toc *)ctsio->cdb; 10683 msf = (cdb->byte2 & CD_MSF) != 0; 10684 format = cdb->format; 10685 alloc_len = scsi_2btoul(cdb->data_len); 10686 10687 data_len = sizeof(struct scsi_read_toc_hdr); 10688 if (format == 0) 10689 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10690 else 10691 data_len += sizeof(struct scsi_read_toc_type01_descr); 10692 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10693 ctsio->kern_sg_entries = 0; 10694 ctsio->kern_rel_offset = 0; 10695 ctsio->kern_data_len = min(data_len, alloc_len); 10696 ctsio->kern_total_len = ctsio->kern_data_len; 10697 10698 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10699 if (format == 0) { 10700 scsi_ulto2b(0x12, hdr->data_length); 10701 hdr->first = 1; 10702 hdr->last = 1; 10703 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10704 descr->addr_ctl = 0x14; 10705 descr->track_number = 1; 10706 if (msf) 10707 ctl_ultomsf(0, descr->track_start); 10708 else 10709 scsi_ulto4b(0, descr->track_start); 10710 descr++; 10711 descr->addr_ctl = 0x14; 10712 descr->track_number = 0xaa; 10713 if (msf) 10714 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10715 else 10716 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10717 } else { 10718 scsi_ulto2b(0x0a, hdr->data_length); 10719 hdr->first = 1; 10720 hdr->last = 1; 10721 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10722 descr->addr_ctl = 0x14; 10723 descr->track_number = 1; 10724 if (msf) 10725 ctl_ultomsf(0, descr->track_start); 10726 else 10727 scsi_ulto4b(0, descr->track_start); 10728 } 10729 10730 ctl_set_success(ctsio); 10731 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10732 ctsio->be_move_done = ctl_config_move_done; 10733 ctl_datamove((union ctl_io *)ctsio); 10734 return (CTL_RETVAL_COMPLETE); 10735 } 10736 10737 /* 10738 * For known CDB types, parse the LBA and length. 10739 */ 10740 static int 10741 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10742 { 10743 10744 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10745 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10746 10747 switch (io->scsiio.cdb[0]) { 10748 case COMPARE_AND_WRITE: { 10749 struct scsi_compare_and_write *cdb; 10750 10751 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10752 10753 *lba = scsi_8btou64(cdb->addr); 10754 *len = cdb->length; 10755 break; 10756 } 10757 case READ_6: 10758 case WRITE_6: { 10759 struct scsi_rw_6 *cdb; 10760 10761 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10762 10763 *lba = scsi_3btoul(cdb->addr); 10764 /* only 5 bits are valid in the most significant address byte */ 10765 *lba &= 0x1fffff; 10766 *len = cdb->length; 10767 break; 10768 } 10769 case READ_10: 10770 case WRITE_10: { 10771 struct scsi_rw_10 *cdb; 10772 10773 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10774 10775 *lba = scsi_4btoul(cdb->addr); 10776 *len = scsi_2btoul(cdb->length); 10777 break; 10778 } 10779 case WRITE_VERIFY_10: { 10780 struct scsi_write_verify_10 *cdb; 10781 10782 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10783 10784 *lba = scsi_4btoul(cdb->addr); 10785 *len = scsi_2btoul(cdb->length); 10786 break; 10787 } 10788 case READ_12: 10789 case WRITE_12: { 10790 struct scsi_rw_12 *cdb; 10791 10792 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10793 10794 *lba = scsi_4btoul(cdb->addr); 10795 *len = scsi_4btoul(cdb->length); 10796 break; 10797 } 10798 case WRITE_VERIFY_12: { 10799 struct scsi_write_verify_12 *cdb; 10800 10801 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10802 10803 *lba = scsi_4btoul(cdb->addr); 10804 *len = scsi_4btoul(cdb->length); 10805 break; 10806 } 10807 case READ_16: 10808 case WRITE_16: { 10809 struct scsi_rw_16 *cdb; 10810 10811 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10812 10813 *lba = scsi_8btou64(cdb->addr); 10814 *len = scsi_4btoul(cdb->length); 10815 break; 10816 } 10817 case WRITE_ATOMIC_16: { 10818 struct scsi_write_atomic_16 *cdb; 10819 10820 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10821 10822 *lba = scsi_8btou64(cdb->addr); 10823 *len = scsi_2btoul(cdb->length); 10824 break; 10825 } 10826 case WRITE_VERIFY_16: { 10827 struct scsi_write_verify_16 *cdb; 10828 10829 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10830 10831 *lba = scsi_8btou64(cdb->addr); 10832 *len = scsi_4btoul(cdb->length); 10833 break; 10834 } 10835 case WRITE_SAME_10: { 10836 struct scsi_write_same_10 *cdb; 10837 10838 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10839 10840 *lba = scsi_4btoul(cdb->addr); 10841 *len = scsi_2btoul(cdb->length); 10842 break; 10843 } 10844 case WRITE_SAME_16: { 10845 struct scsi_write_same_16 *cdb; 10846 10847 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10848 10849 *lba = scsi_8btou64(cdb->addr); 10850 *len = scsi_4btoul(cdb->length); 10851 break; 10852 } 10853 case VERIFY_10: { 10854 struct scsi_verify_10 *cdb; 10855 10856 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10857 10858 *lba = scsi_4btoul(cdb->addr); 10859 *len = scsi_2btoul(cdb->length); 10860 break; 10861 } 10862 case VERIFY_12: { 10863 struct scsi_verify_12 *cdb; 10864 10865 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10866 10867 *lba = scsi_4btoul(cdb->addr); 10868 *len = scsi_4btoul(cdb->length); 10869 break; 10870 } 10871 case VERIFY_16: { 10872 struct scsi_verify_16 *cdb; 10873 10874 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10875 10876 *lba = scsi_8btou64(cdb->addr); 10877 *len = scsi_4btoul(cdb->length); 10878 break; 10879 } 10880 case UNMAP: { 10881 *lba = 0; 10882 *len = UINT64_MAX; 10883 break; 10884 } 10885 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10886 struct scsi_get_lba_status *cdb; 10887 10888 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10889 *lba = scsi_8btou64(cdb->addr); 10890 *len = UINT32_MAX; 10891 break; 10892 } 10893 default: 10894 *lba = 0; 10895 *len = UINT64_MAX; 10896 return (1); 10897 } 10898 10899 return (0); 10900 } 10901 10902 static ctl_action 10903 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10904 bool seq) 10905 { 10906 uint64_t endlba1, endlba2; 10907 10908 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10909 endlba2 = lba2 + len2 - 1; 10910 10911 if ((endlba1 < lba2) || (endlba2 < lba1)) 10912 return (CTL_ACTION_PASS); 10913 else 10914 return (CTL_ACTION_BLOCK); 10915 } 10916 10917 static int 10918 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10919 { 10920 struct ctl_ptr_len_flags *ptrlen; 10921 struct scsi_unmap_desc *buf, *end, *range; 10922 uint64_t lba; 10923 uint32_t len; 10924 10925 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10926 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10927 10928 /* If not UNMAP -- go other way. */ 10929 if (io->scsiio.cdb[0] != UNMAP) 10930 return (CTL_ACTION_SKIP); 10931 10932 /* If UNMAP without data -- block and wait for data. */ 10933 ptrlen = (struct ctl_ptr_len_flags *) 10934 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10935 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10936 ptrlen->ptr == NULL) 10937 return (CTL_ACTION_BLOCK); 10938 10939 /* UNMAP with data -- check for collision. */ 10940 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10941 end = buf + ptrlen->len / sizeof(*buf); 10942 for (range = buf; range < end; range++) { 10943 lba = scsi_8btou64(range->lba); 10944 len = scsi_4btoul(range->length); 10945 if ((lba < lba2 + len2) && (lba + len > lba2)) 10946 return (CTL_ACTION_BLOCK); 10947 } 10948 return (CTL_ACTION_PASS); 10949 } 10950 10951 static ctl_action 10952 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10953 { 10954 uint64_t lba1, lba2; 10955 uint64_t len1, len2; 10956 int retval; 10957 10958 retval = ctl_get_lba_len(io2, &lba2, &len2); 10959 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10960 10961 retval = ctl_extent_check_unmap(io1, lba2, len2); 10962 if (retval != CTL_ACTION_SKIP) 10963 return (retval); 10964 10965 retval = ctl_get_lba_len(io1, &lba1, &len1); 10966 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10967 10968 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)) 10969 seq = FALSE; 10970 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10971 } 10972 10973 static ctl_action 10974 ctl_seq_check(union ctl_io *io1, union ctl_io *io2) 10975 { 10976 uint64_t lba1, lba2; 10977 uint64_t len1, len2; 10978 int retval __diagused; 10979 10980 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10981 return (CTL_ACTION_PASS); 10982 retval = ctl_get_lba_len(io1, &lba1, &len1); 10983 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10984 retval = ctl_get_lba_len(io2, &lba2, &len2); 10985 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10986 10987 if (lba1 + len1 == lba2) 10988 return (CTL_ACTION_BLOCK); 10989 return (CTL_ACTION_PASS); 10990 } 10991 10992 static ctl_action 10993 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10994 const uint8_t *serialize_row, union ctl_io *ooa_io) 10995 { 10996 10997 /* 10998 * The initiator attempted multiple untagged commands at the same 10999 * time. Can't do that. 11000 */ 11001 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11002 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11003 && ((pending_io->io_hdr.nexus.targ_port == 11004 ooa_io->io_hdr.nexus.targ_port) 11005 && (pending_io->io_hdr.nexus.initid == 11006 ooa_io->io_hdr.nexus.initid)) 11007 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11008 CTL_FLAG_STATUS_SENT)) == 0)) 11009 return (CTL_ACTION_OVERLAP); 11010 11011 /* 11012 * The initiator attempted to send multiple tagged commands with 11013 * the same ID. (It's fine if different initiators have the same 11014 * tag ID.) 11015 * 11016 * Even if all of those conditions are true, we don't kill the I/O 11017 * if the command ahead of us has been aborted. We won't end up 11018 * sending it to the FETD, and it's perfectly legal to resend a 11019 * command with the same tag number as long as the previous 11020 * instance of this tag number has been aborted somehow. 11021 */ 11022 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11023 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11024 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11025 && ((pending_io->io_hdr.nexus.targ_port == 11026 ooa_io->io_hdr.nexus.targ_port) 11027 && (pending_io->io_hdr.nexus.initid == 11028 ooa_io->io_hdr.nexus.initid)) 11029 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11030 CTL_FLAG_STATUS_SENT)) == 0)) 11031 return (CTL_ACTION_OVERLAP_TAG); 11032 11033 /* 11034 * If we get a head of queue tag, SAM-3 says that we should 11035 * immediately execute it. 11036 * 11037 * What happens if this command would normally block for some other 11038 * reason? e.g. a request sense with a head of queue tag 11039 * immediately after a write. Normally that would block, but this 11040 * will result in its getting executed immediately... 11041 * 11042 * We currently return "pass" instead of "skip", so we'll end up 11043 * going through the rest of the queue to check for overlapped tags. 11044 * 11045 * XXX KDM check for other types of blockage first?? 11046 */ 11047 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 11048 return (CTL_ACTION_PASS); 11049 11050 /* 11051 * Simple tags get blocked until all head of queue and ordered tags 11052 * ahead of them have completed. I'm lumping untagged commands in 11053 * with simple tags here. XXX KDM is that the right thing to do? 11054 */ 11055 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) || 11056 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 11057 return (CTL_ACTION_BLOCK); 11058 11059 /* Unsupported command in OOA queue. */ 11060 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD)) 11061 return (CTL_ACTION_PASS); 11062 11063 switch (serialize_row[ooa_io->scsiio.seridx]) { 11064 case CTL_SER_SEQ: 11065 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11066 return (ctl_seq_check(ooa_io, pending_io)); 11067 /* FALLTHROUGH */ 11068 case CTL_SER_PASS: 11069 return (CTL_ACTION_PASS); 11070 case CTL_SER_EXTENTOPT: 11071 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 11072 SCP_QUEUE_ALG_UNRESTRICTED) 11073 return (CTL_ACTION_PASS); 11074 /* FALLTHROUGH */ 11075 case CTL_SER_EXTENT: 11076 return (ctl_extent_check(ooa_io, pending_io, 11077 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11078 case CTL_SER_BLOCKOPT: 11079 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 11080 SCP_QUEUE_ALG_UNRESTRICTED) 11081 return (CTL_ACTION_PASS); 11082 /* FALLTHROUGH */ 11083 case CTL_SER_BLOCK: 11084 return (CTL_ACTION_BLOCK); 11085 default: 11086 __assert_unreachable(); 11087 } 11088 } 11089 11090 /* 11091 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11092 * Assumptions: 11093 * - pending_io is generally either incoming, or on the blocked queue 11094 * - starting I/O is the I/O we want to start the check with. 11095 */ 11096 static ctl_action 11097 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11098 union ctl_io **starting_io) 11099 { 11100 union ctl_io *ooa_io = *starting_io; 11101 const uint8_t *serialize_row; 11102 ctl_action action; 11103 11104 mtx_assert(&lun->lun_lock, MA_OWNED); 11105 11106 /* 11107 * Aborted commands are not going to be executed and may even 11108 * not report completion, so we don't care about their order. 11109 * Let them complete ASAP to clean the OOA queue. 11110 */ 11111 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT)) 11112 return (CTL_ACTION_SKIP); 11113 11114 /* 11115 * Ordered tags have to block until all items ahead of them have 11116 * completed. If we get called with an ordered tag, we always 11117 * block, if something else is ahead of us in the queue. 11118 */ 11119 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) && 11120 (ooa_io != NULL)) 11121 return (CTL_ACTION_BLOCK); 11122 11123 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx]; 11124 11125 /* 11126 * Run back along the OOA queue, starting with the current 11127 * blocked I/O and going through every I/O before it on the 11128 * queue. If starting_io is NULL, we'll just end up returning 11129 * CTL_ACTION_PASS. 11130 */ 11131 for (; ooa_io != NULL; 11132 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) { 11133 action = ctl_check_for_blockage(lun, pending_io, serialize_row, 11134 ooa_io); 11135 if (action != CTL_ACTION_PASS) { 11136 *starting_io = ooa_io; 11137 return (action); 11138 } 11139 } 11140 11141 *starting_io = NULL; 11142 return (CTL_ACTION_PASS); 11143 } 11144 11145 /* 11146 * Try to unblock the specified I/O. 11147 * 11148 * skip parameter allows explicitly skip present blocker of the I/O, 11149 * starting from the previous one on OOA queue. It can be used when 11150 * we know for sure that the blocker I/O does no longer count. 11151 */ 11152 static void 11153 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11154 { 11155 struct ctl_softc *softc = lun->ctl_softc; 11156 union ctl_io *bio, *obio; 11157 const struct ctl_cmd_entry *entry; 11158 union ctl_ha_msg msg_info; 11159 ctl_action action; 11160 11161 mtx_assert(&lun->lun_lock, MA_OWNED); 11162 11163 if (io->io_hdr.blocker == NULL) 11164 return; 11165 11166 obio = bio = io->io_hdr.blocker; 11167 if (skip) 11168 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links); 11169 action = ctl_check_ooa(lun, io, &bio); 11170 if (action == CTL_ACTION_BLOCK) { 11171 /* Still blocked, but may be by different I/O now. */ 11172 if (bio != obio) { 11173 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, 11174 &io->io_hdr, blocked_links); 11175 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, 11176 &io->io_hdr, blocked_links); 11177 io->io_hdr.blocker = bio; 11178 } 11179 return; 11180 } 11181 11182 /* No longer blocked, one way or another. */ 11183 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links); 11184 io->io_hdr.blocker = NULL; 11185 11186 switch (action) { 11187 case CTL_ACTION_PASS: 11188 case CTL_ACTION_SKIP: 11189 11190 /* Serializing commands from the other SC retire there. */ 11191 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11192 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11193 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11194 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11195 msg_info.hdr.serializing_sc = io; 11196 msg_info.hdr.msg_type = CTL_MSG_R2R; 11197 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11198 sizeof(msg_info.hdr), M_NOWAIT); 11199 break; 11200 } 11201 11202 /* 11203 * Check this I/O for LUN state changes that may have happened 11204 * while this command was blocked. The LUN state may have been 11205 * changed by a command ahead of us in the queue. 11206 */ 11207 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11208 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 11209 ctl_done(io); 11210 break; 11211 } 11212 11213 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11214 ctl_enqueue_rtr(io); 11215 break; 11216 default: 11217 __assert_unreachable(); 11218 case CTL_ACTION_OVERLAP: 11219 ctl_set_overlapped_cmd(&io->scsiio); 11220 goto error; 11221 case CTL_ACTION_OVERLAP_TAG: 11222 ctl_set_overlapped_tag(&io->scsiio, 11223 io->scsiio.tag_num & 0xff); 11224 error: 11225 /* Serializing commands from the other SC are done here. */ 11226 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11227 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11228 ctl_try_unblock_others(lun, io, TRUE); 11229 LIST_REMOVE(&io->io_hdr, ooa_links); 11230 11231 ctl_copy_sense_data_back(io, &msg_info); 11232 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11233 msg_info.hdr.serializing_sc = NULL; 11234 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 11235 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11236 sizeof(msg_info.scsi), M_WAITOK); 11237 ctl_free_io(io); 11238 break; 11239 } 11240 11241 ctl_done(io); 11242 break; 11243 } 11244 } 11245 11246 /* 11247 * Try to unblock I/Os blocked by the specified I/O. 11248 * 11249 * skip parameter allows explicitly skip the specified I/O as blocker, 11250 * starting from the previous one on the OOA queue. It can be used when 11251 * we know for sure that the specified I/O does no longer count (done). 11252 * It has to be still on OOA queue though so that we know where to start. 11253 */ 11254 static void 11255 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip) 11256 { 11257 union ctl_io *io, *next_io; 11258 11259 mtx_assert(&lun->lun_lock, MA_OWNED); 11260 11261 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue); 11262 io != NULL; io = next_io) { 11263 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links); 11264 11265 KASSERT(io->io_hdr.blocker != NULL, 11266 ("I/O %p on blocked list without blocker", io)); 11267 ctl_try_unblock_io(lun, io, skip); 11268 } 11269 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue), 11270 ("blocked_queue is not empty after skipping %p", bio)); 11271 } 11272 11273 /* 11274 * This routine (with one exception) checks LUN flags that can be set by 11275 * commands ahead of us in the OOA queue. These flags have to be checked 11276 * when a command initially comes in, and when we pull a command off the 11277 * blocked queue and are preparing to execute it. The reason we have to 11278 * check these flags for commands on the blocked queue is that the LUN 11279 * state may have been changed by a command ahead of us while we're on the 11280 * blocked queue. 11281 * 11282 * Ordering is somewhat important with these checks, so please pay 11283 * careful attention to the placement of any new checks. 11284 */ 11285 static int 11286 ctl_scsiio_lun_check(struct ctl_lun *lun, 11287 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11288 { 11289 struct ctl_softc *softc = lun->ctl_softc; 11290 int retval; 11291 uint32_t residx; 11292 11293 retval = 0; 11294 11295 mtx_assert(&lun->lun_lock, MA_OWNED); 11296 11297 /* 11298 * If this shelf is a secondary shelf controller, we may have to 11299 * reject some commands disallowed by HA mode and link state. 11300 */ 11301 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11302 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11303 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11304 ctl_set_lun_unavail(ctsio); 11305 retval = 1; 11306 goto bailout; 11307 } 11308 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11309 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11310 ctl_set_lun_transit(ctsio); 11311 retval = 1; 11312 goto bailout; 11313 } 11314 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11315 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11316 ctl_set_lun_standby(ctsio); 11317 retval = 1; 11318 goto bailout; 11319 } 11320 11321 /* The rest of checks are only done on executing side */ 11322 if (softc->ha_mode == CTL_HA_MODE_XFER) 11323 goto bailout; 11324 } 11325 11326 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11327 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11328 ctl_set_hw_write_protected(ctsio); 11329 retval = 1; 11330 goto bailout; 11331 } 11332 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11333 ctl_set_sense(ctsio, /*current_error*/ 1, 11334 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11335 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11336 retval = 1; 11337 goto bailout; 11338 } 11339 } 11340 11341 /* 11342 * Check for a reservation conflict. If this command isn't allowed 11343 * even on reserved LUNs, and if this initiator isn't the one who 11344 * reserved us, reject the command with a reservation conflict. 11345 */ 11346 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11347 if ((lun->flags & CTL_LUN_RESERVED) 11348 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11349 if (lun->res_idx != residx) { 11350 ctl_set_reservation_conflict(ctsio); 11351 retval = 1; 11352 goto bailout; 11353 } 11354 } 11355 11356 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11357 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11358 /* No reservation or command is allowed. */; 11359 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11360 (lun->pr_res_type == SPR_TYPE_WR_EX || 11361 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11362 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11363 /* The command is allowed for Write Exclusive resv. */; 11364 } else { 11365 /* 11366 * if we aren't registered or it's a res holder type 11367 * reservation and this isn't the res holder then set a 11368 * conflict. 11369 */ 11370 if (ctl_get_prkey(lun, residx) == 0 || 11371 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11372 ctl_set_reservation_conflict(ctsio); 11373 retval = 1; 11374 goto bailout; 11375 } 11376 } 11377 11378 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11379 if (lun->flags & CTL_LUN_EJECTED) 11380 ctl_set_lun_ejected(ctsio); 11381 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11382 if (lun->flags & CTL_LUN_REMOVABLE) 11383 ctl_set_lun_no_media(ctsio); 11384 else 11385 ctl_set_lun_int_reqd(ctsio); 11386 } else if (lun->flags & CTL_LUN_STOPPED) 11387 ctl_set_lun_stopped(ctsio); 11388 else 11389 goto bailout; 11390 retval = 1; 11391 goto bailout; 11392 } 11393 11394 bailout: 11395 return (retval); 11396 } 11397 11398 static void 11399 ctl_failover_io(union ctl_io *io, int have_lock) 11400 { 11401 ctl_set_busy(&io->scsiio); 11402 ctl_done(io); 11403 } 11404 11405 static void 11406 ctl_failover_lun(union ctl_io *rio) 11407 { 11408 struct ctl_softc *softc = CTL_SOFTC(rio); 11409 struct ctl_lun *lun; 11410 struct ctl_io_hdr *io, *next_io; 11411 uint32_t targ_lun; 11412 11413 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11414 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun)); 11415 11416 /* Find and lock the LUN. */ 11417 mtx_lock(&softc->ctl_lock); 11418 if (targ_lun > ctl_max_luns || 11419 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11420 mtx_unlock(&softc->ctl_lock); 11421 return; 11422 } 11423 mtx_lock(&lun->lun_lock); 11424 mtx_unlock(&softc->ctl_lock); 11425 if (lun->flags & CTL_LUN_DISABLED) { 11426 mtx_unlock(&lun->lun_lock); 11427 return; 11428 } 11429 11430 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11431 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11432 /* We are master */ 11433 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11434 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11435 io->flags |= CTL_FLAG_ABORT | 11436 CTL_FLAG_FAILOVER; 11437 ctl_try_unblock_io(lun, 11438 (union ctl_io *)io, FALSE); 11439 } else { /* This can be only due to DATAMOVE */ 11440 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11441 io->flags &= ~CTL_FLAG_DMA_INPROG; 11442 io->flags |= CTL_FLAG_IO_ACTIVE; 11443 io->port_status = 31340; 11444 ctl_enqueue_isc((union ctl_io *)io); 11445 } 11446 } else 11447 /* We are slave */ 11448 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11449 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11450 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11451 io->flags |= CTL_FLAG_FAILOVER; 11452 } else { 11453 ctl_set_busy(&((union ctl_io *)io)-> 11454 scsiio); 11455 ctl_done((union ctl_io *)io); 11456 } 11457 } 11458 } 11459 } else { /* SERIALIZE modes */ 11460 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11461 /* We are master */ 11462 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11463 if (io->blocker != NULL) { 11464 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue, 11465 io, blocked_links); 11466 io->blocker = NULL; 11467 } 11468 ctl_try_unblock_others(lun, (union ctl_io *)io, 11469 TRUE); 11470 LIST_REMOVE(io, ooa_links); 11471 ctl_free_io((union ctl_io *)io); 11472 } else 11473 /* We are slave */ 11474 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11475 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11476 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11477 ctl_set_busy(&((union ctl_io *)io)-> 11478 scsiio); 11479 ctl_done((union ctl_io *)io); 11480 } 11481 } 11482 } 11483 } 11484 mtx_unlock(&lun->lun_lock); 11485 } 11486 11487 static void 11488 ctl_scsiio_precheck(struct ctl_scsiio *ctsio) 11489 { 11490 struct ctl_softc *softc = CTL_SOFTC(ctsio); 11491 struct ctl_lun *lun; 11492 const struct ctl_cmd_entry *entry; 11493 union ctl_io *bio; 11494 uint32_t initidx, targ_lun; 11495 11496 lun = NULL; 11497 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11498 if (targ_lun < ctl_max_luns) 11499 lun = softc->ctl_luns[targ_lun]; 11500 if (lun) { 11501 /* 11502 * If the LUN is invalid, pretend that it doesn't exist. 11503 * It will go away as soon as all pending I/O has been 11504 * completed. 11505 */ 11506 mtx_lock(&lun->lun_lock); 11507 if (lun->flags & CTL_LUN_DISABLED) { 11508 mtx_unlock(&lun->lun_lock); 11509 lun = NULL; 11510 } 11511 } 11512 CTL_LUN(ctsio) = lun; 11513 if (lun) { 11514 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11515 11516 /* 11517 * Every I/O goes into the OOA queue for a particular LUN, 11518 * and stays there until completion. 11519 */ 11520 #ifdef CTL_TIME_IO 11521 if (LIST_EMPTY(&lun->ooa_queue)) 11522 lun->idle_time += getsbinuptime() - lun->last_busy; 11523 #endif 11524 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11525 } 11526 11527 /* Get command entry and return error if it is unsuppotyed. */ 11528 entry = ctl_validate_command(ctsio); 11529 if (entry == NULL) { 11530 if (lun) 11531 mtx_unlock(&lun->lun_lock); 11532 return; 11533 } 11534 11535 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11536 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11537 11538 /* 11539 * Check to see whether we can send this command to LUNs that don't 11540 * exist. This should pretty much only be the case for inquiry 11541 * and request sense. Further checks, below, really require having 11542 * a LUN, so we can't really check the command anymore. Just put 11543 * it on the rtr queue. 11544 */ 11545 if (lun == NULL) { 11546 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11547 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11548 ctl_enqueue_rtr((union ctl_io *)ctsio); 11549 return; 11550 } 11551 11552 ctl_set_unsupported_lun(ctsio); 11553 ctl_done((union ctl_io *)ctsio); 11554 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11555 return; 11556 } else { 11557 /* 11558 * Make sure we support this particular command on this LUN. 11559 * e.g., we don't support writes to the control LUN. 11560 */ 11561 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11562 mtx_unlock(&lun->lun_lock); 11563 ctl_set_invalid_opcode(ctsio); 11564 ctl_done((union ctl_io *)ctsio); 11565 return; 11566 } 11567 } 11568 11569 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11570 11571 /* 11572 * If we've got a request sense, it'll clear the contingent 11573 * allegiance condition. Otherwise, if we have a CA condition for 11574 * this initiator, clear it, because it sent down a command other 11575 * than request sense. 11576 */ 11577 if (ctsio->cdb[0] != REQUEST_SENSE) { 11578 struct scsi_sense_data *ps; 11579 11580 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11581 if (ps != NULL) 11582 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11583 } 11584 11585 /* 11586 * If the command has this flag set, it handles its own unit 11587 * attention reporting, we shouldn't do anything. Otherwise we 11588 * check for any pending unit attentions, and send them back to the 11589 * initiator. We only do this when a command initially comes in, 11590 * not when we pull it off the blocked queue. 11591 * 11592 * According to SAM-3, section 5.3.2, the order that things get 11593 * presented back to the host is basically unit attentions caused 11594 * by some sort of reset event, busy status, reservation conflicts 11595 * or task set full, and finally any other status. 11596 * 11597 * One issue here is that some of the unit attentions we report 11598 * don't fall into the "reset" category (e.g. "reported luns data 11599 * has changed"). So reporting it here, before the reservation 11600 * check, may be technically wrong. I guess the only thing to do 11601 * would be to check for and report the reset events here, and then 11602 * check for the other unit attention types after we check for a 11603 * reservation conflict. 11604 * 11605 * XXX KDM need to fix this 11606 */ 11607 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11608 ctl_ua_type ua_type; 11609 u_int sense_len = 0; 11610 11611 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11612 &sense_len, SSD_TYPE_NONE); 11613 if (ua_type != CTL_UA_NONE) { 11614 mtx_unlock(&lun->lun_lock); 11615 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11616 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11617 ctsio->sense_len = sense_len; 11618 ctl_done((union ctl_io *)ctsio); 11619 return; 11620 } 11621 } 11622 11623 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11624 mtx_unlock(&lun->lun_lock); 11625 ctl_done((union ctl_io *)ctsio); 11626 return; 11627 } 11628 11629 /* 11630 * XXX CHD this is where we want to send IO to other side if 11631 * this LUN is secondary on this SC. We will need to make a copy 11632 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11633 * the copy we send as FROM_OTHER. 11634 * We also need to stuff the address of the original IO so we can 11635 * find it easily. Something similar will need be done on the other 11636 * side so when we are done we can find the copy. 11637 */ 11638 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11639 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11640 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11641 union ctl_ha_msg msg_info; 11642 int isc_retval; 11643 11644 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11645 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11646 mtx_unlock(&lun->lun_lock); 11647 11648 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11649 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11650 msg_info.hdr.serializing_sc = NULL; 11651 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11652 msg_info.scsi.tag_num = ctsio->tag_num; 11653 msg_info.scsi.tag_type = ctsio->tag_type; 11654 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11655 msg_info.scsi.cdb_len = ctsio->cdb_len; 11656 msg_info.scsi.priority = ctsio->priority; 11657 11658 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11659 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11660 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11661 ctl_set_busy(ctsio); 11662 ctl_done((union ctl_io *)ctsio); 11663 return; 11664 } 11665 return; 11666 } 11667 11668 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 11669 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 11670 case CTL_ACTION_PASS: 11671 case CTL_ACTION_SKIP: 11672 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11673 mtx_unlock(&lun->lun_lock); 11674 ctl_enqueue_rtr((union ctl_io *)ctsio); 11675 break; 11676 case CTL_ACTION_BLOCK: 11677 ctsio->io_hdr.blocker = bio; 11678 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 11679 blocked_links); 11680 mtx_unlock(&lun->lun_lock); 11681 break; 11682 case CTL_ACTION_OVERLAP: 11683 mtx_unlock(&lun->lun_lock); 11684 ctl_set_overlapped_cmd(ctsio); 11685 ctl_done((union ctl_io *)ctsio); 11686 break; 11687 case CTL_ACTION_OVERLAP_TAG: 11688 mtx_unlock(&lun->lun_lock); 11689 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11690 ctl_done((union ctl_io *)ctsio); 11691 break; 11692 default: 11693 __assert_unreachable(); 11694 } 11695 } 11696 11697 const struct ctl_cmd_entry * 11698 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11699 { 11700 const struct ctl_cmd_entry *entry; 11701 int service_action; 11702 11703 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11704 if (sa) 11705 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11706 if (entry->flags & CTL_CMD_FLAG_SA5) { 11707 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11708 entry = &((const struct ctl_cmd_entry *) 11709 entry->execute)[service_action]; 11710 } 11711 return (entry); 11712 } 11713 11714 const struct ctl_cmd_entry * 11715 ctl_validate_command(struct ctl_scsiio *ctsio) 11716 { 11717 const struct ctl_cmd_entry *entry; 11718 int i, sa; 11719 uint8_t diff; 11720 11721 entry = ctl_get_cmd_entry(ctsio, &sa); 11722 ctsio->seridx = entry->seridx; 11723 if (entry->execute == NULL) { 11724 if (sa) 11725 ctl_set_invalid_field(ctsio, 11726 /*sks_valid*/ 1, 11727 /*command*/ 1, 11728 /*field*/ 1, 11729 /*bit_valid*/ 1, 11730 /*bit*/ 4); 11731 else 11732 ctl_set_invalid_opcode(ctsio); 11733 ctl_done((union ctl_io *)ctsio); 11734 return (NULL); 11735 } 11736 KASSERT(entry->length > 0, 11737 ("Not defined length for command 0x%02x/0x%02x", 11738 ctsio->cdb[0], ctsio->cdb[1])); 11739 for (i = 1; i < entry->length; i++) { 11740 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11741 if (diff == 0) 11742 continue; 11743 ctl_set_invalid_field(ctsio, 11744 /*sks_valid*/ 1, 11745 /*command*/ 1, 11746 /*field*/ i, 11747 /*bit_valid*/ 1, 11748 /*bit*/ fls(diff) - 1); 11749 ctl_done((union ctl_io *)ctsio); 11750 return (NULL); 11751 } 11752 return (entry); 11753 } 11754 11755 static int 11756 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11757 { 11758 11759 switch (lun_type) { 11760 case T_DIRECT: 11761 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11762 return (0); 11763 break; 11764 case T_PROCESSOR: 11765 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11766 return (0); 11767 break; 11768 case T_CDROM: 11769 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11770 return (0); 11771 break; 11772 default: 11773 return (0); 11774 } 11775 return (1); 11776 } 11777 11778 static int 11779 ctl_scsiio(struct ctl_scsiio *ctsio) 11780 { 11781 int retval; 11782 const struct ctl_cmd_entry *entry; 11783 11784 retval = CTL_RETVAL_COMPLETE; 11785 11786 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11787 11788 entry = ctl_get_cmd_entry(ctsio, NULL); 11789 11790 /* 11791 * If this I/O has been aborted, just send it straight to 11792 * ctl_done() without executing it. 11793 */ 11794 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11795 ctl_done((union ctl_io *)ctsio); 11796 goto bailout; 11797 } 11798 11799 /* 11800 * All the checks should have been handled by ctl_scsiio_precheck(). 11801 * We should be clear now to just execute the I/O. 11802 */ 11803 retval = entry->execute(ctsio); 11804 11805 bailout: 11806 return (retval); 11807 } 11808 11809 static int 11810 ctl_target_reset(union ctl_io *io) 11811 { 11812 struct ctl_softc *softc = CTL_SOFTC(io); 11813 struct ctl_port *port = CTL_PORT(io); 11814 struct ctl_lun *lun; 11815 uint32_t initidx; 11816 ctl_ua_type ua_type; 11817 11818 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11819 union ctl_ha_msg msg_info; 11820 11821 msg_info.hdr.nexus = io->io_hdr.nexus; 11822 msg_info.task.task_action = io->taskio.task_action; 11823 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11824 msg_info.hdr.original_sc = NULL; 11825 msg_info.hdr.serializing_sc = NULL; 11826 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11827 sizeof(msg_info.task), M_WAITOK); 11828 } 11829 11830 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11831 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11832 ua_type = CTL_UA_TARG_RESET; 11833 else 11834 ua_type = CTL_UA_BUS_RESET; 11835 mtx_lock(&softc->ctl_lock); 11836 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11837 if (port != NULL && 11838 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11839 continue; 11840 ctl_do_lun_reset(lun, initidx, ua_type); 11841 } 11842 mtx_unlock(&softc->ctl_lock); 11843 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11844 return (0); 11845 } 11846 11847 /* 11848 * The LUN should always be set. The I/O is optional, and is used to 11849 * distinguish between I/Os sent by this initiator, and by other 11850 * initiators. We set unit attention for initiators other than this one. 11851 * SAM-3 is vague on this point. It does say that a unit attention should 11852 * be established for other initiators when a LUN is reset (see section 11853 * 5.7.3), but it doesn't specifically say that the unit attention should 11854 * be established for this particular initiator when a LUN is reset. Here 11855 * is the relevant text, from SAM-3 rev 8: 11856 * 11857 * 5.7.2 When a SCSI initiator port aborts its own tasks 11858 * 11859 * When a SCSI initiator port causes its own task(s) to be aborted, no 11860 * notification that the task(s) have been aborted shall be returned to 11861 * the SCSI initiator port other than the completion response for the 11862 * command or task management function action that caused the task(s) to 11863 * be aborted and notification(s) associated with related effects of the 11864 * action (e.g., a reset unit attention condition). 11865 * 11866 * XXX KDM for now, we're setting unit attention for all initiators. 11867 */ 11868 static void 11869 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11870 { 11871 struct ctl_io_hdr *xioh; 11872 int i; 11873 11874 mtx_lock(&lun->lun_lock); 11875 /* Abort tasks. */ 11876 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11877 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11878 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE); 11879 } 11880 /* Clear CA. */ 11881 for (i = 0; i < ctl_max_ports; i++) { 11882 free(lun->pending_sense[i], M_CTL); 11883 lun->pending_sense[i] = NULL; 11884 } 11885 /* Clear reservation. */ 11886 lun->flags &= ~CTL_LUN_RESERVED; 11887 /* Clear prevent media removal. */ 11888 if (lun->prevent) { 11889 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11890 ctl_clear_mask(lun->prevent, i); 11891 lun->prevent_count = 0; 11892 } 11893 /* Clear TPC status */ 11894 ctl_tpc_lun_clear(lun, -1); 11895 /* Establish UA. */ 11896 #if 0 11897 ctl_est_ua_all(lun, initidx, ua_type); 11898 #else 11899 ctl_est_ua_all(lun, -1, ua_type); 11900 #endif 11901 mtx_unlock(&lun->lun_lock); 11902 } 11903 11904 static int 11905 ctl_lun_reset(union ctl_io *io) 11906 { 11907 struct ctl_softc *softc = CTL_SOFTC(io); 11908 struct ctl_lun *lun; 11909 uint32_t targ_lun, initidx; 11910 11911 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11912 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11913 mtx_lock(&softc->ctl_lock); 11914 if (targ_lun >= ctl_max_luns || 11915 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11916 mtx_unlock(&softc->ctl_lock); 11917 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11918 return (1); 11919 } 11920 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11921 mtx_unlock(&softc->ctl_lock); 11922 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11923 11924 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11925 union ctl_ha_msg msg_info; 11926 11927 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11928 msg_info.hdr.nexus = io->io_hdr.nexus; 11929 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11930 msg_info.hdr.original_sc = NULL; 11931 msg_info.hdr.serializing_sc = NULL; 11932 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11933 sizeof(msg_info.task), M_WAITOK); 11934 } 11935 return (0); 11936 } 11937 11938 static void 11939 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11940 int other_sc) 11941 { 11942 struct ctl_io_hdr *xioh; 11943 11944 mtx_assert(&lun->lun_lock, MA_OWNED); 11945 11946 /* 11947 * Run through the OOA queue and attempt to find the given I/O. 11948 * The target port, initiator ID, tag type and tag number have to 11949 * match the values that we got from the initiator. If we have an 11950 * untagged command to abort, simply abort the first untagged command 11951 * we come to. We only allow one untagged command at a time of course. 11952 */ 11953 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11954 union ctl_io *xio = (union ctl_io *)xioh; 11955 if ((targ_port == UINT32_MAX || 11956 targ_port == xioh->nexus.targ_port) && 11957 (init_id == UINT32_MAX || 11958 init_id == xioh->nexus.initid)) { 11959 if (targ_port != xioh->nexus.targ_port || 11960 init_id != xioh->nexus.initid) 11961 xioh->flags |= CTL_FLAG_ABORT_STATUS; 11962 xioh->flags |= CTL_FLAG_ABORT; 11963 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11964 union ctl_ha_msg msg_info; 11965 11966 msg_info.hdr.nexus = xioh->nexus; 11967 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11968 msg_info.task.tag_num = xio->scsiio.tag_num; 11969 msg_info.task.tag_type = xio->scsiio.tag_type; 11970 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11971 msg_info.hdr.original_sc = NULL; 11972 msg_info.hdr.serializing_sc = NULL; 11973 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11974 sizeof(msg_info.task), M_NOWAIT); 11975 } 11976 ctl_try_unblock_io(lun, xio, FALSE); 11977 } 11978 } 11979 } 11980 11981 static int 11982 ctl_abort_task_set(union ctl_io *io) 11983 { 11984 struct ctl_softc *softc = CTL_SOFTC(io); 11985 struct ctl_lun *lun; 11986 uint32_t targ_lun; 11987 11988 /* 11989 * Look up the LUN. 11990 */ 11991 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11992 mtx_lock(&softc->ctl_lock); 11993 if (targ_lun >= ctl_max_luns || 11994 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11995 mtx_unlock(&softc->ctl_lock); 11996 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11997 return (1); 11998 } 11999 12000 mtx_lock(&lun->lun_lock); 12001 mtx_unlock(&softc->ctl_lock); 12002 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12003 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12004 io->io_hdr.nexus.initid, 12005 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12006 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12007 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12008 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12009 } 12010 mtx_unlock(&lun->lun_lock); 12011 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12012 return (0); 12013 } 12014 12015 static void 12016 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 12017 ctl_ua_type ua_type) 12018 { 12019 struct ctl_lun *lun; 12020 struct scsi_sense_data *ps; 12021 uint32_t p, i; 12022 12023 p = initidx / CTL_MAX_INIT_PER_PORT; 12024 i = initidx % CTL_MAX_INIT_PER_PORT; 12025 mtx_lock(&softc->ctl_lock); 12026 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12027 mtx_lock(&lun->lun_lock); 12028 /* Abort tasks. */ 12029 ctl_abort_tasks_lun(lun, p, i, 1); 12030 /* Clear CA. */ 12031 ps = lun->pending_sense[p]; 12032 if (ps != NULL) 12033 ps[i].error_code = 0; 12034 /* Clear reservation. */ 12035 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12036 lun->flags &= ~CTL_LUN_RESERVED; 12037 /* Clear prevent media removal. */ 12038 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 12039 ctl_clear_mask(lun->prevent, initidx); 12040 lun->prevent_count--; 12041 } 12042 /* Clear TPC status */ 12043 ctl_tpc_lun_clear(lun, initidx); 12044 /* Establish UA. */ 12045 ctl_est_ua(lun, initidx, ua_type); 12046 mtx_unlock(&lun->lun_lock); 12047 } 12048 mtx_unlock(&softc->ctl_lock); 12049 } 12050 12051 static int 12052 ctl_i_t_nexus_reset(union ctl_io *io) 12053 { 12054 struct ctl_softc *softc = CTL_SOFTC(io); 12055 uint32_t initidx; 12056 12057 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12058 union ctl_ha_msg msg_info; 12059 12060 msg_info.hdr.nexus = io->io_hdr.nexus; 12061 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12062 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12063 msg_info.hdr.original_sc = NULL; 12064 msg_info.hdr.serializing_sc = NULL; 12065 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12066 sizeof(msg_info.task), M_WAITOK); 12067 } 12068 12069 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12070 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 12071 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12072 return (0); 12073 } 12074 12075 static int 12076 ctl_abort_task(union ctl_io *io) 12077 { 12078 struct ctl_softc *softc = CTL_SOFTC(io); 12079 struct ctl_io_hdr *xioh; 12080 struct ctl_lun *lun; 12081 uint32_t targ_lun; 12082 12083 /* 12084 * Look up the LUN. 12085 */ 12086 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12087 mtx_lock(&softc->ctl_lock); 12088 if (targ_lun >= ctl_max_luns || 12089 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12090 mtx_unlock(&softc->ctl_lock); 12091 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12092 return (1); 12093 } 12094 12095 mtx_lock(&lun->lun_lock); 12096 mtx_unlock(&softc->ctl_lock); 12097 /* 12098 * Run through the OOA queue and attempt to find the given I/O. 12099 * The target port, initiator ID, tag type and tag number have to 12100 * match the values that we got from the initiator. If we have an 12101 * untagged command to abort, simply abort the first untagged command 12102 * we come to. We only allow one untagged command at a time of course. 12103 */ 12104 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12105 union ctl_io *xio = (union ctl_io *)xioh; 12106 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12107 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12108 || (xioh->flags & CTL_FLAG_ABORT)) 12109 continue; 12110 12111 /* 12112 * If the abort says that the task is untagged, the 12113 * task in the queue must be untagged. Otherwise, 12114 * we just check to see whether the tag numbers 12115 * match. This is because the QLogic firmware 12116 * doesn't pass back the tag type in an abort 12117 * request. 12118 */ 12119 #if 0 12120 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12121 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12122 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12123 #else 12124 /* 12125 * XXX KDM we've got problems with FC, because it 12126 * doesn't send down a tag type with aborts. So we 12127 * can only really go by the tag number... 12128 * This may cause problems with parallel SCSI. 12129 * Need to figure that out!! 12130 */ 12131 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12132 #endif 12133 xioh->flags |= CTL_FLAG_ABORT; 12134 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12135 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12136 union ctl_ha_msg msg_info; 12137 12138 msg_info.hdr.nexus = io->io_hdr.nexus; 12139 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12140 msg_info.task.tag_num = io->taskio.tag_num; 12141 msg_info.task.tag_type = io->taskio.tag_type; 12142 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12143 msg_info.hdr.original_sc = NULL; 12144 msg_info.hdr.serializing_sc = NULL; 12145 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12146 sizeof(msg_info.task), M_NOWAIT); 12147 } 12148 ctl_try_unblock_io(lun, xio, FALSE); 12149 } 12150 } 12151 mtx_unlock(&lun->lun_lock); 12152 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12153 return (0); 12154 } 12155 12156 static int 12157 ctl_query_task(union ctl_io *io, int task_set) 12158 { 12159 struct ctl_softc *softc = CTL_SOFTC(io); 12160 struct ctl_io_hdr *xioh; 12161 struct ctl_lun *lun; 12162 int found = 0; 12163 uint32_t targ_lun; 12164 12165 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12166 mtx_lock(&softc->ctl_lock); 12167 if (targ_lun >= ctl_max_luns || 12168 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12169 mtx_unlock(&softc->ctl_lock); 12170 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12171 return (1); 12172 } 12173 mtx_lock(&lun->lun_lock); 12174 mtx_unlock(&softc->ctl_lock); 12175 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12176 union ctl_io *xio = (union ctl_io *)xioh; 12177 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12178 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12179 || (xioh->flags & CTL_FLAG_ABORT)) 12180 continue; 12181 12182 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12183 found = 1; 12184 break; 12185 } 12186 } 12187 mtx_unlock(&lun->lun_lock); 12188 if (found) 12189 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12190 else 12191 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12192 return (0); 12193 } 12194 12195 static int 12196 ctl_query_async_event(union ctl_io *io) 12197 { 12198 struct ctl_softc *softc = CTL_SOFTC(io); 12199 struct ctl_lun *lun; 12200 ctl_ua_type ua; 12201 uint32_t targ_lun, initidx; 12202 12203 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12204 mtx_lock(&softc->ctl_lock); 12205 if (targ_lun >= ctl_max_luns || 12206 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12207 mtx_unlock(&softc->ctl_lock); 12208 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12209 return (1); 12210 } 12211 mtx_lock(&lun->lun_lock); 12212 mtx_unlock(&softc->ctl_lock); 12213 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12214 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12215 mtx_unlock(&lun->lun_lock); 12216 if (ua != CTL_UA_NONE) 12217 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12218 else 12219 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12220 return (0); 12221 } 12222 12223 static void 12224 ctl_run_task(union ctl_io *io) 12225 { 12226 int retval = 1; 12227 12228 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12229 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12230 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12231 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12232 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12233 switch (io->taskio.task_action) { 12234 case CTL_TASK_ABORT_TASK: 12235 retval = ctl_abort_task(io); 12236 break; 12237 case CTL_TASK_ABORT_TASK_SET: 12238 case CTL_TASK_CLEAR_TASK_SET: 12239 retval = ctl_abort_task_set(io); 12240 break; 12241 case CTL_TASK_CLEAR_ACA: 12242 break; 12243 case CTL_TASK_I_T_NEXUS_RESET: 12244 retval = ctl_i_t_nexus_reset(io); 12245 break; 12246 case CTL_TASK_LUN_RESET: 12247 retval = ctl_lun_reset(io); 12248 break; 12249 case CTL_TASK_TARGET_RESET: 12250 case CTL_TASK_BUS_RESET: 12251 retval = ctl_target_reset(io); 12252 break; 12253 case CTL_TASK_PORT_LOGIN: 12254 break; 12255 case CTL_TASK_PORT_LOGOUT: 12256 break; 12257 case CTL_TASK_QUERY_TASK: 12258 retval = ctl_query_task(io, 0); 12259 break; 12260 case CTL_TASK_QUERY_TASK_SET: 12261 retval = ctl_query_task(io, 1); 12262 break; 12263 case CTL_TASK_QUERY_ASYNC_EVENT: 12264 retval = ctl_query_async_event(io); 12265 break; 12266 default: 12267 printf("%s: got unknown task management event %d\n", 12268 __func__, io->taskio.task_action); 12269 break; 12270 } 12271 if (retval == 0) 12272 io->io_hdr.status = CTL_SUCCESS; 12273 else 12274 io->io_hdr.status = CTL_ERROR; 12275 ctl_done(io); 12276 } 12277 12278 /* 12279 * For HA operation. Handle commands that come in from the other 12280 * controller. 12281 */ 12282 static void 12283 ctl_handle_isc(union ctl_io *io) 12284 { 12285 struct ctl_softc *softc = CTL_SOFTC(io); 12286 struct ctl_lun *lun; 12287 const struct ctl_cmd_entry *entry; 12288 uint32_t targ_lun; 12289 12290 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12291 switch (io->io_hdr.msg_type) { 12292 case CTL_MSG_SERIALIZE: 12293 ctl_serialize_other_sc_cmd(&io->scsiio); 12294 break; 12295 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12296 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12297 if (targ_lun >= ctl_max_luns || 12298 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12299 ctl_done(io); 12300 break; 12301 } 12302 mtx_lock(&lun->lun_lock); 12303 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12304 mtx_unlock(&lun->lun_lock); 12305 ctl_done(io); 12306 break; 12307 } 12308 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12309 mtx_unlock(&lun->lun_lock); 12310 ctl_enqueue_rtr(io); 12311 break; 12312 case CTL_MSG_FINISH_IO: 12313 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12314 ctl_done(io); 12315 break; 12316 } 12317 if (targ_lun >= ctl_max_luns || 12318 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12319 ctl_free_io(io); 12320 break; 12321 } 12322 mtx_lock(&lun->lun_lock); 12323 ctl_try_unblock_others(lun, io, TRUE); 12324 LIST_REMOVE(&io->io_hdr, ooa_links); 12325 mtx_unlock(&lun->lun_lock); 12326 ctl_free_io(io); 12327 break; 12328 case CTL_MSG_PERS_ACTION: 12329 ctl_hndl_per_res_out_on_other_sc(io); 12330 ctl_free_io(io); 12331 break; 12332 case CTL_MSG_BAD_JUJU: 12333 ctl_done(io); 12334 break; 12335 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12336 ctl_datamove_remote(io); 12337 break; 12338 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12339 ctl_datamove_done(io, false); 12340 break; 12341 case CTL_MSG_FAILOVER: 12342 ctl_failover_lun(io); 12343 ctl_free_io(io); 12344 break; 12345 default: 12346 printf("%s: Invalid message type %d\n", 12347 __func__, io->io_hdr.msg_type); 12348 ctl_free_io(io); 12349 break; 12350 } 12351 12352 } 12353 12354 /* 12355 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12356 * there is no match. 12357 */ 12358 static ctl_lun_error_pattern 12359 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12360 { 12361 const struct ctl_cmd_entry *entry; 12362 ctl_lun_error_pattern filtered_pattern, pattern; 12363 12364 pattern = desc->error_pattern; 12365 12366 /* 12367 * XXX KDM we need more data passed into this function to match a 12368 * custom pattern, and we actually need to implement custom pattern 12369 * matching. 12370 */ 12371 if (pattern & CTL_LUN_PAT_CMD) 12372 return (CTL_LUN_PAT_CMD); 12373 12374 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12375 return (CTL_LUN_PAT_ANY); 12376 12377 entry = ctl_get_cmd_entry(ctsio, NULL); 12378 12379 filtered_pattern = entry->pattern & pattern; 12380 12381 /* 12382 * If the user requested specific flags in the pattern (e.g. 12383 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12384 * flags. 12385 * 12386 * If the user did not specify any flags, it doesn't matter whether 12387 * or not the command supports the flags. 12388 */ 12389 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12390 (pattern & ~CTL_LUN_PAT_MASK)) 12391 return (CTL_LUN_PAT_NONE); 12392 12393 /* 12394 * If the user asked for a range check, see if the requested LBA 12395 * range overlaps with this command's LBA range. 12396 */ 12397 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12398 uint64_t lba1; 12399 uint64_t len1; 12400 ctl_action action; 12401 int retval; 12402 12403 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12404 if (retval != 0) 12405 return (CTL_LUN_PAT_NONE); 12406 12407 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12408 desc->lba_range.len, FALSE); 12409 /* 12410 * A "pass" means that the LBA ranges don't overlap, so 12411 * this doesn't match the user's range criteria. 12412 */ 12413 if (action == CTL_ACTION_PASS) 12414 return (CTL_LUN_PAT_NONE); 12415 } 12416 12417 return (filtered_pattern); 12418 } 12419 12420 static void 12421 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12422 { 12423 struct ctl_error_desc *desc, *desc2; 12424 12425 mtx_assert(&lun->lun_lock, MA_OWNED); 12426 12427 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12428 ctl_lun_error_pattern pattern; 12429 /* 12430 * Check to see whether this particular command matches 12431 * the pattern in the descriptor. 12432 */ 12433 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12434 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12435 continue; 12436 12437 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12438 case CTL_LUN_INJ_ABORTED: 12439 ctl_set_aborted(&io->scsiio); 12440 break; 12441 case CTL_LUN_INJ_MEDIUM_ERR: 12442 ctl_set_medium_error(&io->scsiio, 12443 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12444 CTL_FLAG_DATA_OUT); 12445 break; 12446 case CTL_LUN_INJ_UA: 12447 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12448 * OCCURRED */ 12449 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12450 break; 12451 case CTL_LUN_INJ_CUSTOM: 12452 /* 12453 * We're assuming the user knows what he is doing. 12454 * Just copy the sense information without doing 12455 * checks. 12456 */ 12457 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12458 MIN(sizeof(desc->custom_sense), 12459 sizeof(io->scsiio.sense_data))); 12460 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12461 io->scsiio.sense_len = SSD_FULL_SIZE; 12462 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12463 break; 12464 case CTL_LUN_INJ_NONE: 12465 default: 12466 /* 12467 * If this is an error injection type we don't know 12468 * about, clear the continuous flag (if it is set) 12469 * so it will get deleted below. 12470 */ 12471 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12472 break; 12473 } 12474 /* 12475 * By default, each error injection action is a one-shot 12476 */ 12477 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12478 continue; 12479 12480 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12481 12482 free(desc, M_CTL); 12483 } 12484 } 12485 12486 #ifdef CTL_IO_DELAY 12487 static void 12488 ctl_datamove_timer_wakeup(void *arg) 12489 { 12490 union ctl_io *io; 12491 12492 io = (union ctl_io *)arg; 12493 12494 ctl_datamove(io); 12495 } 12496 #endif /* CTL_IO_DELAY */ 12497 12498 static void 12499 ctl_datamove_done_process(union ctl_io *io) 12500 { 12501 #ifdef CTL_TIME_IO 12502 struct bintime cur_bt; 12503 #endif 12504 12505 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 12506 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 12507 12508 #ifdef CTL_TIME_IO 12509 getbinuptime(&cur_bt); 12510 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12511 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12512 #endif 12513 io->io_hdr.num_dmas++; 12514 12515 if ((io->io_hdr.port_status != 0) && 12516 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12517 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12518 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 12519 /*retry_count*/ io->io_hdr.port_status); 12520 } else if (io->scsiio.kern_data_resid != 0 && 12521 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 12522 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12523 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12524 ctl_set_invalid_field_ciu(&io->scsiio); 12525 } else if (ctl_debug & CTL_DEBUG_CDB_DATA) 12526 ctl_data_print(io); 12527 } 12528 12529 void 12530 ctl_datamove_done(union ctl_io *io, bool samethr) 12531 { 12532 12533 ctl_datamove_done_process(io); 12534 io->scsiio.be_move_done(io, samethr); 12535 } 12536 12537 void 12538 ctl_datamove(union ctl_io *io) 12539 { 12540 void (*fe_datamove)(union ctl_io *io); 12541 12542 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12543 12544 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12545 12546 /* No data transferred yet. Frontend must update this when done. */ 12547 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12548 12549 #ifdef CTL_TIME_IO 12550 getbinuptime(&io->io_hdr.dma_start_bt); 12551 #endif /* CTL_TIME_IO */ 12552 12553 #ifdef CTL_IO_DELAY 12554 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12555 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12556 } else { 12557 struct ctl_lun *lun; 12558 12559 lun = CTL_LUN(io); 12560 if ((lun != NULL) 12561 && (lun->delay_info.datamove_delay > 0)) { 12562 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12563 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12564 callout_reset(&io->io_hdr.delay_callout, 12565 lun->delay_info.datamove_delay * hz, 12566 ctl_datamove_timer_wakeup, io); 12567 if (lun->delay_info.datamove_type == 12568 CTL_DELAY_TYPE_ONESHOT) 12569 lun->delay_info.datamove_delay = 0; 12570 return; 12571 } 12572 } 12573 #endif 12574 12575 /* 12576 * This command has been aborted. Set the port status, so we fail 12577 * the data move. 12578 */ 12579 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12580 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12581 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12582 io->io_hdr.nexus.targ_port, 12583 io->io_hdr.nexus.targ_lun); 12584 io->io_hdr.port_status = 31337; 12585 ctl_datamove_done_process(io); 12586 io->scsiio.be_move_done(io, true); 12587 return; 12588 } 12589 12590 /* Don't confuse frontend with zero length data move. */ 12591 if (io->scsiio.kern_data_len == 0) { 12592 ctl_datamove_done_process(io); 12593 io->scsiio.be_move_done(io, true); 12594 return; 12595 } 12596 12597 fe_datamove = CTL_PORT(io)->fe_datamove; 12598 fe_datamove(io); 12599 } 12600 12601 static void 12602 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12603 { 12604 union ctl_ha_msg msg; 12605 #ifdef CTL_TIME_IO 12606 struct bintime cur_bt; 12607 #endif 12608 12609 memset(&msg, 0, sizeof(msg)); 12610 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12611 msg.hdr.original_sc = io; 12612 msg.hdr.serializing_sc = io->io_hdr.remote_io; 12613 msg.hdr.nexus = io->io_hdr.nexus; 12614 msg.hdr.status = io->io_hdr.status; 12615 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12616 msg.scsi.tag_num = io->scsiio.tag_num; 12617 msg.scsi.tag_type = io->scsiio.tag_type; 12618 msg.scsi.scsi_status = io->scsiio.scsi_status; 12619 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12620 io->scsiio.sense_len); 12621 msg.scsi.sense_len = io->scsiio.sense_len; 12622 msg.scsi.port_status = io->io_hdr.port_status; 12623 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12624 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12625 ctl_failover_io(io, /*have_lock*/ have_lock); 12626 return; 12627 } 12628 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12629 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12630 msg.scsi.sense_len, M_WAITOK); 12631 12632 #ifdef CTL_TIME_IO 12633 getbinuptime(&cur_bt); 12634 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12635 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12636 #endif 12637 io->io_hdr.num_dmas++; 12638 } 12639 12640 /* 12641 * The DMA to the remote side is done, now we need to tell the other side 12642 * we're done so it can continue with its data movement. 12643 */ 12644 static void 12645 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12646 { 12647 union ctl_io *io; 12648 uint32_t i; 12649 12650 io = rq->context; 12651 12652 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12653 printf("%s: ISC DMA write failed with error %d", __func__, 12654 rq->ret); 12655 ctl_set_internal_failure(&io->scsiio, 12656 /*sks_valid*/ 1, 12657 /*retry_count*/ rq->ret); 12658 } 12659 12660 ctl_dt_req_free(rq); 12661 12662 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12663 free(CTL_LSGLT(io)[i].addr, M_CTL); 12664 free(CTL_RSGL(io), M_CTL); 12665 CTL_RSGL(io) = NULL; 12666 CTL_LSGL(io) = NULL; 12667 12668 /* 12669 * The data is in local and remote memory, so now we need to send 12670 * status (good or back) back to the other side. 12671 */ 12672 ctl_send_datamove_done(io, /*have_lock*/ 0); 12673 } 12674 12675 /* 12676 * We've moved the data from the host/controller into local memory. Now we 12677 * need to push it over to the remote controller's memory. 12678 */ 12679 static int 12680 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr) 12681 { 12682 int retval; 12683 12684 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12685 ctl_datamove_remote_write_cb); 12686 return (retval); 12687 } 12688 12689 static void 12690 ctl_datamove_remote_write(union ctl_io *io) 12691 { 12692 int retval; 12693 void (*fe_datamove)(union ctl_io *io); 12694 12695 /* 12696 * - Get the data from the host/HBA into local memory. 12697 * - DMA memory from the local controller to the remote controller. 12698 * - Send status back to the remote controller. 12699 */ 12700 12701 retval = ctl_datamove_remote_sgl_setup(io); 12702 if (retval != 0) 12703 return; 12704 12705 /* Switch the pointer over so the FETD knows what to do */ 12706 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12707 12708 /* 12709 * Use a custom move done callback, since we need to send completion 12710 * back to the other controller, not to the backend on this side. 12711 */ 12712 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12713 12714 fe_datamove = CTL_PORT(io)->fe_datamove; 12715 fe_datamove(io); 12716 } 12717 12718 static int 12719 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr) 12720 { 12721 uint32_t i; 12722 12723 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12724 free(CTL_LSGLT(io)[i].addr, M_CTL); 12725 free(CTL_RSGL(io), M_CTL); 12726 CTL_RSGL(io) = NULL; 12727 CTL_LSGL(io) = NULL; 12728 12729 /* 12730 * The read is done, now we need to send status (good or bad) back 12731 * to the other side. 12732 */ 12733 ctl_send_datamove_done(io, /*have_lock*/ 0); 12734 12735 return (0); 12736 } 12737 12738 static void 12739 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12740 { 12741 union ctl_io *io; 12742 void (*fe_datamove)(union ctl_io *io); 12743 12744 io = rq->context; 12745 12746 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12747 printf("%s: ISC DMA read failed with error %d\n", __func__, 12748 rq->ret); 12749 ctl_set_internal_failure(&io->scsiio, 12750 /*sks_valid*/ 1, 12751 /*retry_count*/ rq->ret); 12752 } 12753 12754 ctl_dt_req_free(rq); 12755 12756 /* Switch the pointer over so the FETD knows what to do */ 12757 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12758 12759 /* 12760 * Use a custom move done callback, since we need to send completion 12761 * back to the other controller, not to the backend on this side. 12762 */ 12763 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12764 12765 /* XXX KDM add checks like the ones in ctl_datamove? */ 12766 12767 fe_datamove = CTL_PORT(io)->fe_datamove; 12768 fe_datamove(io); 12769 } 12770 12771 static int 12772 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12773 { 12774 struct ctl_sg_entry *local_sglist; 12775 uint32_t len_to_go; 12776 int retval; 12777 int i; 12778 12779 retval = 0; 12780 local_sglist = CTL_LSGL(io); 12781 len_to_go = io->scsiio.kern_data_len; 12782 12783 /* 12784 * The difficult thing here is that the size of the various 12785 * S/G segments may be different than the size from the 12786 * remote controller. That'll make it harder when DMAing 12787 * the data back to the other side. 12788 */ 12789 for (i = 0; len_to_go > 0; i++) { 12790 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12791 local_sglist[i].addr = 12792 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12793 12794 len_to_go -= local_sglist[i].len; 12795 } 12796 /* 12797 * Reset the number of S/G entries accordingly. The original 12798 * number of S/G entries is available in rem_sg_entries. 12799 */ 12800 io->scsiio.kern_sg_entries = i; 12801 12802 return (retval); 12803 } 12804 12805 static int 12806 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12807 ctl_ha_dt_cb callback) 12808 { 12809 struct ctl_ha_dt_req *rq; 12810 struct ctl_sg_entry *remote_sglist, *local_sglist; 12811 uint32_t local_used, remote_used, total_used; 12812 int i, j, isc_ret; 12813 12814 rq = ctl_dt_req_alloc(); 12815 12816 /* 12817 * If we failed to allocate the request, and if the DMA didn't fail 12818 * anyway, set busy status. This is just a resource allocation 12819 * failure. 12820 */ 12821 if ((rq == NULL) 12822 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12823 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12824 ctl_set_busy(&io->scsiio); 12825 12826 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12827 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12828 if (rq != NULL) 12829 ctl_dt_req_free(rq); 12830 12831 /* 12832 * The data move failed. We need to return status back 12833 * to the other controller. No point in trying to DMA 12834 * data to the remote controller. 12835 */ 12836 12837 ctl_send_datamove_done(io, /*have_lock*/ 0); 12838 12839 return (1); 12840 } 12841 12842 local_sglist = CTL_LSGL(io); 12843 remote_sglist = CTL_RSGL(io); 12844 local_used = 0; 12845 remote_used = 0; 12846 total_used = 0; 12847 12848 /* 12849 * Pull/push the data over the wire from/to the other controller. 12850 * This takes into account the possibility that the local and 12851 * remote sglists may not be identical in terms of the size of 12852 * the elements and the number of elements. 12853 * 12854 * One fundamental assumption here is that the length allocated for 12855 * both the local and remote sglists is identical. Otherwise, we've 12856 * essentially got a coding error of some sort. 12857 */ 12858 isc_ret = CTL_HA_STATUS_SUCCESS; 12859 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12860 uint32_t cur_len; 12861 uint8_t *tmp_ptr; 12862 12863 rq->command = command; 12864 rq->context = io; 12865 12866 /* 12867 * Both pointers should be aligned. But it is possible 12868 * that the allocation length is not. They should both 12869 * also have enough slack left over at the end, though, 12870 * to round up to the next 8 byte boundary. 12871 */ 12872 cur_len = MIN(local_sglist[i].len - local_used, 12873 remote_sglist[j].len - remote_used); 12874 rq->size = cur_len; 12875 12876 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12877 tmp_ptr += local_used; 12878 12879 #if 0 12880 /* Use physical addresses when talking to ISC hardware */ 12881 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12882 /* XXX KDM use busdma */ 12883 rq->local = vtophys(tmp_ptr); 12884 } else 12885 rq->local = tmp_ptr; 12886 #else 12887 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12888 ("HA does not support BUS_ADDR")); 12889 rq->local = tmp_ptr; 12890 #endif 12891 12892 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12893 tmp_ptr += remote_used; 12894 rq->remote = tmp_ptr; 12895 12896 rq->callback = NULL; 12897 12898 local_used += cur_len; 12899 if (local_used >= local_sglist[i].len) { 12900 i++; 12901 local_used = 0; 12902 } 12903 12904 remote_used += cur_len; 12905 if (remote_used >= remote_sglist[j].len) { 12906 j++; 12907 remote_used = 0; 12908 } 12909 total_used += cur_len; 12910 12911 if (total_used >= io->scsiio.kern_data_len) 12912 rq->callback = callback; 12913 12914 isc_ret = ctl_dt_single(rq); 12915 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12916 break; 12917 } 12918 if (isc_ret != CTL_HA_STATUS_WAIT) { 12919 rq->ret = isc_ret; 12920 callback(rq); 12921 } 12922 12923 return (0); 12924 } 12925 12926 static void 12927 ctl_datamove_remote_read(union ctl_io *io) 12928 { 12929 int retval; 12930 uint32_t i; 12931 12932 /* 12933 * This will send an error to the other controller in the case of a 12934 * failure. 12935 */ 12936 retval = ctl_datamove_remote_sgl_setup(io); 12937 if (retval != 0) 12938 return; 12939 12940 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12941 ctl_datamove_remote_read_cb); 12942 if (retval != 0) { 12943 /* 12944 * Make sure we free memory if there was an error.. The 12945 * ctl_datamove_remote_xfer() function will send the 12946 * datamove done message, or call the callback with an 12947 * error if there is a problem. 12948 */ 12949 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12950 free(CTL_LSGLT(io)[i].addr, M_CTL); 12951 free(CTL_RSGL(io), M_CTL); 12952 CTL_RSGL(io) = NULL; 12953 CTL_LSGL(io) = NULL; 12954 } 12955 } 12956 12957 /* 12958 * Process a datamove request from the other controller. This is used for 12959 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12960 * first. Once that is complete, the data gets DMAed into the remote 12961 * controller's memory. For reads, we DMA from the remote controller's 12962 * memory into our memory first, and then move it out to the FETD. 12963 */ 12964 static void 12965 ctl_datamove_remote(union ctl_io *io) 12966 { 12967 12968 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12969 12970 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12971 ctl_failover_io(io, /*have_lock*/ 0); 12972 return; 12973 } 12974 12975 /* 12976 * Note that we look for an aborted I/O here, but don't do some of 12977 * the other checks that ctl_datamove() normally does. 12978 * We don't need to run the datamove delay code, since that should 12979 * have been done if need be on the other controller. 12980 */ 12981 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12982 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12983 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12984 io->io_hdr.nexus.targ_port, 12985 io->io_hdr.nexus.targ_lun); 12986 io->io_hdr.port_status = 31338; 12987 ctl_send_datamove_done(io, /*have_lock*/ 0); 12988 return; 12989 } 12990 12991 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12992 ctl_datamove_remote_write(io); 12993 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12994 ctl_datamove_remote_read(io); 12995 else { 12996 io->io_hdr.port_status = 31339; 12997 ctl_send_datamove_done(io, /*have_lock*/ 0); 12998 } 12999 } 13000 13001 static void 13002 ctl_process_done(union ctl_io *io) 13003 { 13004 struct ctl_softc *softc = CTL_SOFTC(io); 13005 struct ctl_port *port = CTL_PORT(io); 13006 struct ctl_lun *lun = CTL_LUN(io); 13007 void (*fe_done)(union ctl_io *io); 13008 union ctl_ha_msg msg; 13009 13010 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13011 fe_done = port->fe_done; 13012 13013 #ifdef CTL_TIME_IO 13014 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13015 char str[256]; 13016 char path_str[64]; 13017 struct sbuf sb; 13018 13019 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13020 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13021 13022 sbuf_cat(&sb, path_str); 13023 switch (io->io_hdr.io_type) { 13024 case CTL_IO_SCSI: 13025 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13026 sbuf_printf(&sb, "\n"); 13027 sbuf_cat(&sb, path_str); 13028 sbuf_printf(&sb, "Tag: 0x%04x/%d, Prio: %d\n", 13029 io->scsiio.tag_num, io->scsiio.tag_type, 13030 io->scsiio.priority); 13031 break; 13032 case CTL_IO_TASK: 13033 sbuf_printf(&sb, "Task Action: %d Tag: 0x%04x/%d\n", 13034 io->taskio.task_action, 13035 io->taskio.tag_num, io->taskio.tag_type); 13036 break; 13037 default: 13038 panic("%s: Invalid CTL I/O type %d\n", 13039 __func__, io->io_hdr.io_type); 13040 } 13041 sbuf_cat(&sb, path_str); 13042 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13043 (intmax_t)time_uptime - io->io_hdr.start_time); 13044 sbuf_finish(&sb); 13045 printf("%s", sbuf_data(&sb)); 13046 } 13047 #endif /* CTL_TIME_IO */ 13048 13049 switch (io->io_hdr.io_type) { 13050 case CTL_IO_SCSI: 13051 break; 13052 case CTL_IO_TASK: 13053 if (ctl_debug & CTL_DEBUG_INFO) 13054 ctl_io_error_print(io, NULL); 13055 fe_done(io); 13056 return; 13057 default: 13058 panic("%s: Invalid CTL I/O type %d\n", 13059 __func__, io->io_hdr.io_type); 13060 } 13061 13062 if (lun == NULL) { 13063 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13064 io->io_hdr.nexus.targ_mapped_lun)); 13065 goto bailout; 13066 } 13067 13068 mtx_lock(&lun->lun_lock); 13069 13070 /* 13071 * Check to see if we have any informational exception and status 13072 * of this command can be modified to report it in form of either 13073 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13074 */ 13075 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13076 io->io_hdr.status == CTL_SUCCESS && 13077 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13078 uint8_t mrie = lun->MODE_IE.mrie; 13079 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13080 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13081 if (((mrie == SIEP_MRIE_REC_COND && per) || 13082 mrie == SIEP_MRIE_REC_UNCOND || 13083 mrie == SIEP_MRIE_NO_SENSE) && 13084 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13085 CTL_CMD_FLAG_NO_SENSE) == 0) { 13086 ctl_set_sense(&io->scsiio, 13087 /*current_error*/ 1, 13088 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13089 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13090 /*asc*/ lun->ie_asc, 13091 /*ascq*/ lun->ie_ascq, 13092 SSD_ELEM_NONE); 13093 lun->ie_reported = 1; 13094 } 13095 } else if (lun->ie_reported < 0) 13096 lun->ie_reported = 0; 13097 13098 /* 13099 * Check to see if we have any errors to inject here. We only 13100 * inject errors for commands that don't already have errors set. 13101 */ 13102 if (!STAILQ_EMPTY(&lun->error_list) && 13103 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13104 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13105 ctl_inject_error(lun, io); 13106 13107 /* 13108 * XXX KDM how do we treat commands that aren't completed 13109 * successfully? 13110 * 13111 * XXX KDM should we also track I/O latency? 13112 */ 13113 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13114 io->io_hdr.io_type == CTL_IO_SCSI) { 13115 int type; 13116 #ifdef CTL_TIME_IO 13117 struct bintime bt; 13118 13119 getbinuptime(&bt); 13120 bintime_sub(&bt, &io->io_hdr.start_bt); 13121 #endif 13122 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13123 CTL_FLAG_DATA_IN) 13124 type = CTL_STATS_READ; 13125 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13126 CTL_FLAG_DATA_OUT) 13127 type = CTL_STATS_WRITE; 13128 else 13129 type = CTL_STATS_NO_IO; 13130 13131 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13132 lun->stats.operations[type] ++; 13133 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13134 #ifdef CTL_TIME_IO 13135 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13136 bintime_add(&lun->stats.time[type], &bt); 13137 #endif 13138 13139 mtx_lock(&port->port_lock); 13140 port->stats.bytes[type] += io->scsiio.kern_total_len; 13141 port->stats.operations[type] ++; 13142 port->stats.dmas[type] += io->io_hdr.num_dmas; 13143 #ifdef CTL_TIME_IO 13144 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13145 bintime_add(&port->stats.time[type], &bt); 13146 #endif 13147 mtx_unlock(&port->port_lock); 13148 } 13149 13150 /* 13151 * Run through the blocked queue of this I/O and see if anything 13152 * can be unblocked, now that this I/O is done and will be removed. 13153 * We need to do it before removal to have OOA position to start. 13154 */ 13155 ctl_try_unblock_others(lun, io, TRUE); 13156 13157 /* 13158 * Remove this from the OOA queue. 13159 */ 13160 LIST_REMOVE(&io->io_hdr, ooa_links); 13161 #ifdef CTL_TIME_IO 13162 if (LIST_EMPTY(&lun->ooa_queue)) 13163 lun->last_busy = getsbinuptime(); 13164 #endif 13165 13166 /* 13167 * If the LUN has been invalidated, free it if there is nothing 13168 * left on its OOA queue. 13169 */ 13170 if ((lun->flags & CTL_LUN_INVALID) 13171 && LIST_EMPTY(&lun->ooa_queue)) { 13172 mtx_unlock(&lun->lun_lock); 13173 ctl_free_lun(lun); 13174 } else 13175 mtx_unlock(&lun->lun_lock); 13176 13177 bailout: 13178 13179 /* 13180 * If this command has been aborted, make sure we set the status 13181 * properly. The FETD is responsible for freeing the I/O and doing 13182 * whatever it needs to do to clean up its state. 13183 */ 13184 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13185 ctl_set_task_aborted(&io->scsiio); 13186 13187 /* 13188 * If enabled, print command error status. 13189 */ 13190 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13191 (ctl_debug & CTL_DEBUG_INFO) != 0) 13192 ctl_io_error_print(io, NULL); 13193 13194 /* 13195 * Tell the FETD or the other shelf controller we're done with this 13196 * command. Note that only SCSI commands get to this point. Task 13197 * management commands are completed above. 13198 */ 13199 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13200 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13201 memset(&msg, 0, sizeof(msg)); 13202 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13203 msg.hdr.serializing_sc = io->io_hdr.remote_io; 13204 msg.hdr.nexus = io->io_hdr.nexus; 13205 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13206 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13207 M_WAITOK); 13208 } 13209 13210 fe_done(io); 13211 } 13212 13213 /* 13214 * Front end should call this if it doesn't do autosense. When the request 13215 * sense comes back in from the initiator, we'll dequeue this and send it. 13216 */ 13217 int 13218 ctl_queue_sense(union ctl_io *io) 13219 { 13220 struct ctl_softc *softc = CTL_SOFTC(io); 13221 struct ctl_port *port = CTL_PORT(io); 13222 struct ctl_lun *lun; 13223 struct scsi_sense_data *ps; 13224 uint32_t initidx, p, targ_lun; 13225 13226 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13227 13228 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13229 13230 /* 13231 * LUN lookup will likely move to the ctl_work_thread() once we 13232 * have our new queueing infrastructure (that doesn't put things on 13233 * a per-LUN queue initially). That is so that we can handle 13234 * things like an INQUIRY to a LUN that we don't have enabled. We 13235 * can't deal with that right now. 13236 * If we don't have a LUN for this, just toss the sense information. 13237 */ 13238 mtx_lock(&softc->ctl_lock); 13239 if (targ_lun >= ctl_max_luns || 13240 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13241 mtx_unlock(&softc->ctl_lock); 13242 goto bailout; 13243 } 13244 mtx_lock(&lun->lun_lock); 13245 mtx_unlock(&softc->ctl_lock); 13246 13247 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13248 p = initidx / CTL_MAX_INIT_PER_PORT; 13249 if (lun->pending_sense[p] == NULL) { 13250 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13251 M_CTL, M_NOWAIT | M_ZERO); 13252 } 13253 if ((ps = lun->pending_sense[p]) != NULL) { 13254 ps += initidx % CTL_MAX_INIT_PER_PORT; 13255 memset(ps, 0, sizeof(*ps)); 13256 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13257 } 13258 mtx_unlock(&lun->lun_lock); 13259 13260 bailout: 13261 ctl_free_io(io); 13262 return (CTL_RETVAL_COMPLETE); 13263 } 13264 13265 /* 13266 * Primary command inlet from frontend ports. All SCSI and task I/O 13267 * requests must go through this function. 13268 */ 13269 int 13270 ctl_queue(union ctl_io *io) 13271 { 13272 struct ctl_port *port = CTL_PORT(io); 13273 13274 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13275 13276 #ifdef CTL_TIME_IO 13277 io->io_hdr.start_time = time_uptime; 13278 getbinuptime(&io->io_hdr.start_bt); 13279 #endif /* CTL_TIME_IO */ 13280 13281 /* Map FE-specific LUN ID into global one. */ 13282 io->io_hdr.nexus.targ_mapped_lun = 13283 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13284 13285 switch (io->io_hdr.io_type) { 13286 case CTL_IO_SCSI: 13287 case CTL_IO_TASK: 13288 if (ctl_debug & CTL_DEBUG_CDB) 13289 ctl_io_print(io); 13290 ctl_enqueue_incoming(io); 13291 break; 13292 default: 13293 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13294 return (EINVAL); 13295 } 13296 13297 return (CTL_RETVAL_COMPLETE); 13298 } 13299 13300 int 13301 ctl_run(union ctl_io *io) 13302 { 13303 struct ctl_port *port = CTL_PORT(io); 13304 13305 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0])); 13306 13307 #ifdef CTL_TIME_IO 13308 io->io_hdr.start_time = time_uptime; 13309 getbinuptime(&io->io_hdr.start_bt); 13310 #endif /* CTL_TIME_IO */ 13311 13312 /* Map FE-specific LUN ID into global one. */ 13313 io->io_hdr.nexus.targ_mapped_lun = 13314 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13315 13316 switch (io->io_hdr.io_type) { 13317 case CTL_IO_SCSI: 13318 if (ctl_debug & CTL_DEBUG_CDB) 13319 ctl_io_print(io); 13320 ctl_scsiio_precheck(&io->scsiio); 13321 break; 13322 case CTL_IO_TASK: 13323 if (ctl_debug & CTL_DEBUG_CDB) 13324 ctl_io_print(io); 13325 ctl_run_task(io); 13326 break; 13327 default: 13328 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type); 13329 return (EINVAL); 13330 } 13331 13332 return (CTL_RETVAL_COMPLETE); 13333 } 13334 13335 #ifdef CTL_IO_DELAY 13336 static void 13337 ctl_done_timer_wakeup(void *arg) 13338 { 13339 union ctl_io *io; 13340 13341 io = (union ctl_io *)arg; 13342 ctl_done(io); 13343 } 13344 #endif /* CTL_IO_DELAY */ 13345 13346 void 13347 ctl_serseq_done(union ctl_io *io) 13348 { 13349 struct ctl_lun *lun = CTL_LUN(io); 13350 13351 /* This is racy, but should not be a problem. */ 13352 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) { 13353 mtx_lock(&lun->lun_lock); 13354 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13355 ctl_try_unblock_others(lun, io, FALSE); 13356 mtx_unlock(&lun->lun_lock); 13357 } else 13358 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13359 } 13360 13361 void 13362 ctl_done(union ctl_io *io) 13363 { 13364 13365 /* 13366 * Enable this to catch duplicate completion issues. 13367 */ 13368 #if 0 13369 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13370 printf("%s: type %d msg %d cdb %x iptl: " 13371 "%u:%u:%u tag 0x%04x " 13372 "flag %#x status %x\n", 13373 __func__, 13374 io->io_hdr.io_type, 13375 io->io_hdr.msg_type, 13376 io->scsiio.cdb[0], 13377 io->io_hdr.nexus.initid, 13378 io->io_hdr.nexus.targ_port, 13379 io->io_hdr.nexus.targ_lun, 13380 (io->io_hdr.io_type == 13381 CTL_IO_TASK) ? 13382 io->taskio.tag_num : 13383 io->scsiio.tag_num, 13384 io->io_hdr.flags, 13385 io->io_hdr.status); 13386 } else 13387 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13388 #endif 13389 13390 /* 13391 * This is an internal copy of an I/O, and should not go through 13392 * the normal done processing logic. 13393 */ 13394 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13395 return; 13396 13397 #ifdef CTL_IO_DELAY 13398 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13399 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13400 } else { 13401 struct ctl_lun *lun = CTL_LUN(io); 13402 13403 if ((lun != NULL) 13404 && (lun->delay_info.done_delay > 0)) { 13405 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13406 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13407 callout_reset(&io->io_hdr.delay_callout, 13408 lun->delay_info.done_delay * hz, 13409 ctl_done_timer_wakeup, io); 13410 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13411 lun->delay_info.done_delay = 0; 13412 return; 13413 } 13414 } 13415 #endif /* CTL_IO_DELAY */ 13416 13417 ctl_enqueue_done(io); 13418 } 13419 13420 static void 13421 ctl_work_thread(void *arg) 13422 { 13423 struct ctl_thread *thr = (struct ctl_thread *)arg; 13424 struct ctl_softc *softc = thr->ctl_softc; 13425 union ctl_io *io; 13426 int retval; 13427 13428 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13429 thread_lock(curthread); 13430 sched_prio(curthread, PUSER - 1); 13431 thread_unlock(curthread); 13432 13433 while (!softc->shutdown) { 13434 /* 13435 * We handle the queues in this order: 13436 * - ISC 13437 * - done queue (to free up resources, unblock other commands) 13438 * - incoming queue 13439 * - RtR queue 13440 * 13441 * If those queues are empty, we break out of the loop and 13442 * go to sleep. 13443 */ 13444 mtx_lock(&thr->queue_lock); 13445 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13446 if (io != NULL) { 13447 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13448 mtx_unlock(&thr->queue_lock); 13449 ctl_handle_isc(io); 13450 continue; 13451 } 13452 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13453 if (io != NULL) { 13454 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13455 /* clear any blocked commands, call fe_done */ 13456 mtx_unlock(&thr->queue_lock); 13457 ctl_process_done(io); 13458 continue; 13459 } 13460 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13461 if (io != NULL) { 13462 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13463 mtx_unlock(&thr->queue_lock); 13464 if (io->io_hdr.io_type == CTL_IO_TASK) 13465 ctl_run_task(io); 13466 else 13467 ctl_scsiio_precheck(&io->scsiio); 13468 continue; 13469 } 13470 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13471 if (io != NULL) { 13472 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13473 mtx_unlock(&thr->queue_lock); 13474 retval = ctl_scsiio(&io->scsiio); 13475 if (retval != CTL_RETVAL_COMPLETE) 13476 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13477 continue; 13478 } 13479 13480 /* Sleep until we have something to do. */ 13481 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0); 13482 } 13483 thr->thread = NULL; 13484 kthread_exit(); 13485 } 13486 13487 static void 13488 ctl_thresh_thread(void *arg) 13489 { 13490 struct ctl_softc *softc = (struct ctl_softc *)arg; 13491 struct ctl_lun *lun; 13492 struct ctl_logical_block_provisioning_page *page; 13493 const char *attr; 13494 union ctl_ha_msg msg; 13495 uint64_t thres, val; 13496 int i, e, set; 13497 13498 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13499 thread_lock(curthread); 13500 sched_prio(curthread, PUSER - 1); 13501 thread_unlock(curthread); 13502 13503 while (!softc->shutdown) { 13504 mtx_lock(&softc->ctl_lock); 13505 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13506 if ((lun->flags & CTL_LUN_DISABLED) || 13507 (lun->flags & CTL_LUN_NO_MEDIA) || 13508 lun->backend->lun_attr == NULL) 13509 continue; 13510 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13511 softc->ha_mode == CTL_HA_MODE_XFER) 13512 continue; 13513 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13514 continue; 13515 e = 0; 13516 page = &lun->MODE_LBP; 13517 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13518 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13519 continue; 13520 thres = scsi_4btoul(page->descr[i].count); 13521 thres <<= CTL_LBP_EXPONENT; 13522 switch (page->descr[i].resource) { 13523 case 0x01: 13524 attr = "blocksavail"; 13525 break; 13526 case 0x02: 13527 attr = "blocksused"; 13528 break; 13529 case 0xf1: 13530 attr = "poolblocksavail"; 13531 break; 13532 case 0xf2: 13533 attr = "poolblocksused"; 13534 break; 13535 default: 13536 continue; 13537 } 13538 mtx_unlock(&softc->ctl_lock); // XXX 13539 val = lun->backend->lun_attr(lun->be_lun, attr); 13540 mtx_lock(&softc->ctl_lock); 13541 if (val == UINT64_MAX) 13542 continue; 13543 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13544 == SLBPPD_ARMING_INC) 13545 e = (val >= thres); 13546 else 13547 e = (val <= thres); 13548 if (e) 13549 break; 13550 } 13551 mtx_lock(&lun->lun_lock); 13552 if (e) { 13553 scsi_u64to8b((uint8_t *)&page->descr[i] - 13554 (uint8_t *)page, lun->ua_tpt_info); 13555 if (lun->lasttpt == 0 || 13556 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13557 lun->lasttpt = time_uptime; 13558 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13559 set = 1; 13560 } else 13561 set = 0; 13562 } else { 13563 lun->lasttpt = 0; 13564 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13565 set = -1; 13566 } 13567 mtx_unlock(&lun->lun_lock); 13568 if (set != 0 && 13569 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13570 /* Send msg to other side. */ 13571 bzero(&msg.ua, sizeof(msg.ua)); 13572 msg.hdr.msg_type = CTL_MSG_UA; 13573 msg.hdr.nexus.initid = -1; 13574 msg.hdr.nexus.targ_port = -1; 13575 msg.hdr.nexus.targ_lun = lun->lun; 13576 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13577 msg.ua.ua_all = 1; 13578 msg.ua.ua_set = (set > 0); 13579 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13580 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13581 mtx_unlock(&softc->ctl_lock); // XXX 13582 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13583 sizeof(msg.ua), M_WAITOK); 13584 mtx_lock(&softc->ctl_lock); 13585 } 13586 } 13587 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13588 PDROP, "-", CTL_LBP_PERIOD * hz); 13589 } 13590 softc->thresh_thread = NULL; 13591 kthread_exit(); 13592 } 13593 13594 static void 13595 ctl_enqueue_incoming(union ctl_io *io) 13596 { 13597 struct ctl_softc *softc = CTL_SOFTC(io); 13598 struct ctl_thread *thr; 13599 u_int idx; 13600 13601 idx = (io->io_hdr.nexus.targ_port * 127 + 13602 io->io_hdr.nexus.initid) % worker_threads; 13603 thr = &softc->threads[idx]; 13604 mtx_lock(&thr->queue_lock); 13605 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13606 mtx_unlock(&thr->queue_lock); 13607 wakeup(thr); 13608 } 13609 13610 static void 13611 ctl_enqueue_rtr(union ctl_io *io) 13612 { 13613 struct ctl_softc *softc = CTL_SOFTC(io); 13614 struct ctl_thread *thr; 13615 13616 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13617 mtx_lock(&thr->queue_lock); 13618 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13619 mtx_unlock(&thr->queue_lock); 13620 wakeup(thr); 13621 } 13622 13623 static void 13624 ctl_enqueue_done(union ctl_io *io) 13625 { 13626 struct ctl_softc *softc = CTL_SOFTC(io); 13627 struct ctl_thread *thr; 13628 13629 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13630 mtx_lock(&thr->queue_lock); 13631 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13632 mtx_unlock(&thr->queue_lock); 13633 wakeup(thr); 13634 } 13635 13636 static void 13637 ctl_enqueue_isc(union ctl_io *io) 13638 { 13639 struct ctl_softc *softc = CTL_SOFTC(io); 13640 struct ctl_thread *thr; 13641 13642 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13643 mtx_lock(&thr->queue_lock); 13644 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13645 mtx_unlock(&thr->queue_lock); 13646 wakeup(thr); 13647 } 13648 13649 /* 13650 * vim: ts=8 13651 */ 13652