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_SENT_2OTHER_SC; 804 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 805 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 806 ctsio->io_hdr.status = msg_info->hdr.status; 807 ctsio->scsi_status = msg_info->scsi.scsi_status; 808 ctsio->sense_len = msg_info->scsi.sense_len; 809 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 810 msg_info->scsi.sense_len); 811 ctl_enqueue_isc((union ctl_io *)ctsio); 812 } 813 814 static void 815 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 816 union ctl_ha_msg *msg_info) 817 { 818 struct ctl_scsiio *ctsio; 819 820 if (msg_info->hdr.serializing_sc == NULL) { 821 printf("%s: serializing_sc == NULL!\n", __func__); 822 /* XXX KDM now what? */ 823 return; 824 } 825 826 ctsio = &msg_info->hdr.serializing_sc->scsiio; 827 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 828 ctl_enqueue_isc((union ctl_io *)ctsio); 829 } 830 831 void 832 ctl_isc_announce_lun(struct ctl_lun *lun) 833 { 834 struct ctl_softc *softc = lun->ctl_softc; 835 union ctl_ha_msg *msg; 836 struct ctl_ha_msg_lun_pr_key pr_key; 837 int i, k; 838 839 if (softc->ha_link != CTL_HA_LINK_ONLINE) 840 return; 841 mtx_lock(&lun->lun_lock); 842 i = sizeof(msg->lun); 843 if (lun->lun_devid) 844 i += lun->lun_devid->len; 845 i += sizeof(pr_key) * lun->pr_key_count; 846 alloc: 847 mtx_unlock(&lun->lun_lock); 848 msg = malloc(i, M_CTL, M_WAITOK); 849 mtx_lock(&lun->lun_lock); 850 k = sizeof(msg->lun); 851 if (lun->lun_devid) 852 k += lun->lun_devid->len; 853 k += sizeof(pr_key) * lun->pr_key_count; 854 if (i < k) { 855 free(msg, M_CTL); 856 i = k; 857 goto alloc; 858 } 859 bzero(&msg->lun, sizeof(msg->lun)); 860 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 861 msg->hdr.nexus.targ_lun = lun->lun; 862 msg->hdr.nexus.targ_mapped_lun = lun->lun; 863 msg->lun.flags = lun->flags; 864 msg->lun.pr_generation = lun->pr_generation; 865 msg->lun.pr_res_idx = lun->pr_res_idx; 866 msg->lun.pr_res_type = lun->pr_res_type; 867 msg->lun.pr_key_count = lun->pr_key_count; 868 i = 0; 869 if (lun->lun_devid) { 870 msg->lun.lun_devid_len = lun->lun_devid->len; 871 memcpy(&msg->lun.data[i], lun->lun_devid->data, 872 msg->lun.lun_devid_len); 873 i += msg->lun.lun_devid_len; 874 } 875 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 876 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 877 continue; 878 pr_key.pr_iid = k; 879 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 880 i += sizeof(pr_key); 881 } 882 mtx_unlock(&lun->lun_lock); 883 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->lun, sizeof(msg->lun) + i, 884 M_WAITOK); 885 free(msg, M_CTL); 886 887 if (lun->flags & CTL_LUN_PRIMARY_SC) { 888 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 889 ctl_isc_announce_mode(lun, -1, 890 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 891 lun->mode_pages.index[i].subpage); 892 } 893 } 894 } 895 896 void 897 ctl_isc_announce_port(struct ctl_port *port) 898 { 899 struct ctl_softc *softc = port->ctl_softc; 900 union ctl_ha_msg *msg; 901 int i; 902 903 if (port->targ_port < softc->port_min || 904 port->targ_port >= softc->port_max || 905 softc->ha_link != CTL_HA_LINK_ONLINE) 906 return; 907 i = sizeof(msg->port) + strlen(port->port_name) + 1; 908 if (port->lun_map) 909 i += port->lun_map_size * sizeof(uint32_t); 910 if (port->port_devid) 911 i += port->port_devid->len; 912 if (port->target_devid) 913 i += port->target_devid->len; 914 if (port->init_devid) 915 i += port->init_devid->len; 916 msg = malloc(i, M_CTL, M_WAITOK); 917 bzero(&msg->port, sizeof(msg->port)); 918 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 919 msg->hdr.nexus.targ_port = port->targ_port; 920 msg->port.port_type = port->port_type; 921 msg->port.physical_port = port->physical_port; 922 msg->port.virtual_port = port->virtual_port; 923 msg->port.status = port->status; 924 i = 0; 925 msg->port.name_len = sprintf(&msg->port.data[i], 926 "%d:%s", softc->ha_id, port->port_name) + 1; 927 i += msg->port.name_len; 928 if (port->lun_map) { 929 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 930 memcpy(&msg->port.data[i], port->lun_map, 931 msg->port.lun_map_len); 932 i += msg->port.lun_map_len; 933 } 934 if (port->port_devid) { 935 msg->port.port_devid_len = port->port_devid->len; 936 memcpy(&msg->port.data[i], port->port_devid->data, 937 msg->port.port_devid_len); 938 i += msg->port.port_devid_len; 939 } 940 if (port->target_devid) { 941 msg->port.target_devid_len = port->target_devid->len; 942 memcpy(&msg->port.data[i], port->target_devid->data, 943 msg->port.target_devid_len); 944 i += msg->port.target_devid_len; 945 } 946 if (port->init_devid) { 947 msg->port.init_devid_len = port->init_devid->len; 948 memcpy(&msg->port.data[i], port->init_devid->data, 949 msg->port.init_devid_len); 950 i += msg->port.init_devid_len; 951 } 952 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 953 M_WAITOK); 954 free(msg, M_CTL); 955 } 956 957 void 958 ctl_isc_announce_iid(struct ctl_port *port, int iid) 959 { 960 struct ctl_softc *softc = port->ctl_softc; 961 union ctl_ha_msg *msg; 962 int i, l; 963 964 if (port->targ_port < softc->port_min || 965 port->targ_port >= softc->port_max || 966 softc->ha_link != CTL_HA_LINK_ONLINE) 967 return; 968 mtx_lock(&softc->ctl_lock); 969 i = sizeof(msg->iid); 970 l = 0; 971 if (port->wwpn_iid[iid].name) 972 l = strlen(port->wwpn_iid[iid].name) + 1; 973 i += l; 974 msg = malloc(i, M_CTL, M_NOWAIT); 975 if (msg == NULL) { 976 mtx_unlock(&softc->ctl_lock); 977 return; 978 } 979 bzero(&msg->iid, sizeof(msg->iid)); 980 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 981 msg->hdr.nexus.targ_port = port->targ_port; 982 msg->hdr.nexus.initid = iid; 983 msg->iid.in_use = port->wwpn_iid[iid].in_use; 984 msg->iid.name_len = l; 985 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 986 if (port->wwpn_iid[iid].name) 987 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 988 mtx_unlock(&softc->ctl_lock); 989 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 990 free(msg, M_CTL); 991 } 992 993 void 994 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 995 uint8_t page, uint8_t subpage) 996 { 997 struct ctl_softc *softc = lun->ctl_softc; 998 union ctl_ha_msg *msg; 999 u_int i, l; 1000 1001 if (softc->ha_link != CTL_HA_LINK_ONLINE) 1002 return; 1003 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1004 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1005 page && lun->mode_pages.index[i].subpage == subpage) 1006 break; 1007 } 1008 if (i == CTL_NUM_MODE_PAGES) 1009 return; 1010 1011 /* Don't try to replicate pages not present on this device. */ 1012 if (lun->mode_pages.index[i].page_data == NULL) 1013 return; 1014 1015 l = sizeof(msg->mode) + lun->mode_pages.index[i].page_len; 1016 msg = malloc(l, M_CTL, M_WAITOK | M_ZERO); 1017 msg->hdr.msg_type = CTL_MSG_MODE_SYNC; 1018 msg->hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 1019 msg->hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 1020 msg->hdr.nexus.targ_lun = lun->lun; 1021 msg->hdr.nexus.targ_mapped_lun = lun->lun; 1022 msg->mode.page_code = page; 1023 msg->mode.subpage = subpage; 1024 msg->mode.page_len = lun->mode_pages.index[i].page_len; 1025 memcpy(msg->mode.data, lun->mode_pages.index[i].page_data, 1026 msg->mode.page_len); 1027 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->mode, l, M_WAITOK); 1028 free(msg, M_CTL); 1029 } 1030 1031 static void 1032 ctl_isc_ha_link_up(struct ctl_softc *softc) 1033 { 1034 struct ctl_port *port; 1035 struct ctl_lun *lun; 1036 union ctl_ha_msg msg; 1037 int i; 1038 1039 /* Announce this node parameters to peer for validation. */ 1040 msg.login.msg_type = CTL_MSG_LOGIN; 1041 msg.login.version = CTL_HA_VERSION; 1042 msg.login.ha_mode = softc->ha_mode; 1043 msg.login.ha_id = softc->ha_id; 1044 msg.login.max_luns = ctl_max_luns; 1045 msg.login.max_ports = ctl_max_ports; 1046 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1047 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1048 M_WAITOK); 1049 1050 STAILQ_FOREACH(port, &softc->port_list, links) { 1051 ctl_isc_announce_port(port); 1052 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1053 if (port->wwpn_iid[i].in_use) 1054 ctl_isc_announce_iid(port, i); 1055 } 1056 } 1057 STAILQ_FOREACH(lun, &softc->lun_list, links) 1058 ctl_isc_announce_lun(lun); 1059 } 1060 1061 static void 1062 ctl_isc_ha_link_down(struct ctl_softc *softc) 1063 { 1064 struct ctl_port *port; 1065 struct ctl_lun *lun; 1066 union ctl_io *io; 1067 int i; 1068 1069 mtx_lock(&softc->ctl_lock); 1070 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1071 mtx_lock(&lun->lun_lock); 1072 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1073 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1074 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1075 } 1076 mtx_unlock(&lun->lun_lock); 1077 1078 mtx_unlock(&softc->ctl_lock); 1079 io = ctl_alloc_io(softc->othersc_pool); 1080 mtx_lock(&softc->ctl_lock); 1081 ctl_zero_io(io); 1082 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1083 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1084 ctl_enqueue_isc(io); 1085 } 1086 1087 STAILQ_FOREACH(port, &softc->port_list, links) { 1088 if (port->targ_port >= softc->port_min && 1089 port->targ_port < softc->port_max) 1090 continue; 1091 port->status &= ~CTL_PORT_STATUS_ONLINE; 1092 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1093 port->wwpn_iid[i].in_use = 0; 1094 free(port->wwpn_iid[i].name, M_CTL); 1095 port->wwpn_iid[i].name = NULL; 1096 } 1097 } 1098 mtx_unlock(&softc->ctl_lock); 1099 } 1100 1101 static void 1102 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1103 { 1104 struct ctl_lun *lun; 1105 uint32_t iid; 1106 1107 if (len < sizeof(msg->ua)) { 1108 printf("%s: Received truncated message %d < %zu\n", 1109 __func__, len, sizeof(msg->ua)); 1110 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1111 return; 1112 } 1113 1114 mtx_lock(&softc->ctl_lock); 1115 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1116 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1117 mtx_unlock(&softc->ctl_lock); 1118 return; 1119 } 1120 mtx_lock(&lun->lun_lock); 1121 mtx_unlock(&softc->ctl_lock); 1122 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1123 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1124 iid = ctl_get_initindex(&msg->hdr.nexus); 1125 if (msg->ua.ua_all) { 1126 if (msg->ua.ua_set) 1127 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1128 else 1129 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1130 } else { 1131 if (msg->ua.ua_set) 1132 ctl_est_ua(lun, iid, msg->ua.ua_type); 1133 else 1134 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1135 } 1136 mtx_unlock(&lun->lun_lock); 1137 } 1138 1139 static void 1140 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1141 { 1142 struct ctl_lun *lun; 1143 struct ctl_ha_msg_lun_pr_key pr_key; 1144 int i, k; 1145 ctl_lun_flags oflags; 1146 uint32_t targ_lun; 1147 1148 if (len < offsetof(struct ctl_ha_msg_lun, data[0])) { 1149 printf("%s: Received truncated message %d < %zu\n", 1150 __func__, len, offsetof(struct ctl_ha_msg_lun, data[0])); 1151 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1152 return; 1153 } 1154 i = msg->lun.lun_devid_len + msg->lun.pr_key_count * sizeof(pr_key); 1155 if (len < offsetof(struct ctl_ha_msg_lun, data[i])) { 1156 printf("%s: Received truncated message data %d < %zu\n", 1157 __func__, len, offsetof(struct ctl_ha_msg_lun, data[i])); 1158 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1159 return; 1160 } 1161 1162 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1163 mtx_lock(&softc->ctl_lock); 1164 if (targ_lun >= ctl_max_luns || 1165 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1166 mtx_unlock(&softc->ctl_lock); 1167 return; 1168 } 1169 mtx_lock(&lun->lun_lock); 1170 mtx_unlock(&softc->ctl_lock); 1171 if (lun->flags & CTL_LUN_DISABLED) { 1172 mtx_unlock(&lun->lun_lock); 1173 return; 1174 } 1175 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1176 if (msg->lun.lun_devid_len != i || (i > 0 && 1177 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1178 mtx_unlock(&lun->lun_lock); 1179 printf("%s: Received conflicting HA LUN %d\n", 1180 __func__, targ_lun); 1181 return; 1182 } else { 1183 /* Record whether peer is primary. */ 1184 oflags = lun->flags; 1185 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1186 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1187 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1188 else 1189 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1190 if (oflags != lun->flags) 1191 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1192 1193 /* If peer is primary and we are not -- use data */ 1194 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1195 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1196 lun->pr_generation = msg->lun.pr_generation; 1197 lun->pr_res_idx = msg->lun.pr_res_idx; 1198 lun->pr_res_type = msg->lun.pr_res_type; 1199 lun->pr_key_count = msg->lun.pr_key_count; 1200 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1201 ctl_clr_prkey(lun, k); 1202 for (k = 0; k < msg->lun.pr_key_count; k++) { 1203 memcpy(&pr_key, &msg->lun.data[i], 1204 sizeof(pr_key)); 1205 ctl_alloc_prkey(lun, pr_key.pr_iid); 1206 ctl_set_prkey(lun, pr_key.pr_iid, 1207 pr_key.pr_key); 1208 i += sizeof(pr_key); 1209 } 1210 } 1211 1212 mtx_unlock(&lun->lun_lock); 1213 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1214 __func__, targ_lun, 1215 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1216 "primary" : "secondary")); 1217 1218 /* If we are primary but peer doesn't know -- notify */ 1219 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1220 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1221 ctl_isc_announce_lun(lun); 1222 } 1223 } 1224 1225 static void 1226 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1227 { 1228 struct ctl_port *port; 1229 struct ctl_lun *lun; 1230 int i, new; 1231 1232 if (len < offsetof(struct ctl_ha_msg_port, data[0])) { 1233 printf("%s: Received truncated message %d < %zu\n", 1234 __func__, len, offsetof(struct ctl_ha_msg_port, data[0])); 1235 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1236 return; 1237 } 1238 i = msg->port.name_len + msg->port.lun_map_len + 1239 msg->port.port_devid_len + msg->port.target_devid_len + 1240 msg->port.init_devid_len; 1241 if (len < offsetof(struct ctl_ha_msg_port, data[i])) { 1242 printf("%s: Received truncated message data %d < %zu\n", 1243 __func__, len, offsetof(struct ctl_ha_msg_port, data[i])); 1244 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1245 return; 1246 } 1247 1248 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1249 if (port == NULL) { 1250 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1251 msg->hdr.nexus.targ_port)); 1252 new = 1; 1253 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1254 port->frontend = &ha_frontend; 1255 port->targ_port = msg->hdr.nexus.targ_port; 1256 port->fe_datamove = ctl_ha_datamove; 1257 port->fe_done = ctl_ha_done; 1258 } else if (port->frontend == &ha_frontend) { 1259 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1260 msg->hdr.nexus.targ_port)); 1261 new = 0; 1262 } else { 1263 printf("%s: Received conflicting HA port %d\n", 1264 __func__, msg->hdr.nexus.targ_port); 1265 return; 1266 } 1267 port->port_type = msg->port.port_type; 1268 port->physical_port = msg->port.physical_port; 1269 port->virtual_port = msg->port.virtual_port; 1270 port->status = msg->port.status; 1271 i = 0; 1272 free(port->port_name, M_CTL); 1273 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1274 M_CTL); 1275 i += msg->port.name_len; 1276 if (msg->port.lun_map_len != 0) { 1277 if (port->lun_map == NULL || 1278 port->lun_map_size * sizeof(uint32_t) < 1279 msg->port.lun_map_len) { 1280 port->lun_map_size = 0; 1281 free(port->lun_map, M_CTL); 1282 port->lun_map = malloc(msg->port.lun_map_len, 1283 M_CTL, M_WAITOK); 1284 } 1285 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1286 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1287 i += msg->port.lun_map_len; 1288 } else { 1289 port->lun_map_size = 0; 1290 free(port->lun_map, M_CTL); 1291 port->lun_map = NULL; 1292 } 1293 if (msg->port.port_devid_len != 0) { 1294 if (port->port_devid == NULL || 1295 port->port_devid->len < msg->port.port_devid_len) { 1296 free(port->port_devid, M_CTL); 1297 port->port_devid = malloc(sizeof(struct ctl_devid) + 1298 msg->port.port_devid_len, M_CTL, M_WAITOK); 1299 } 1300 memcpy(port->port_devid->data, &msg->port.data[i], 1301 msg->port.port_devid_len); 1302 port->port_devid->len = msg->port.port_devid_len; 1303 i += msg->port.port_devid_len; 1304 } else { 1305 free(port->port_devid, M_CTL); 1306 port->port_devid = NULL; 1307 } 1308 if (msg->port.target_devid_len != 0) { 1309 if (port->target_devid == NULL || 1310 port->target_devid->len < msg->port.target_devid_len) { 1311 free(port->target_devid, M_CTL); 1312 port->target_devid = malloc(sizeof(struct ctl_devid) + 1313 msg->port.target_devid_len, M_CTL, M_WAITOK); 1314 } 1315 memcpy(port->target_devid->data, &msg->port.data[i], 1316 msg->port.target_devid_len); 1317 port->target_devid->len = msg->port.target_devid_len; 1318 i += msg->port.target_devid_len; 1319 } else { 1320 free(port->target_devid, M_CTL); 1321 port->target_devid = NULL; 1322 } 1323 if (msg->port.init_devid_len != 0) { 1324 if (port->init_devid == NULL || 1325 port->init_devid->len < msg->port.init_devid_len) { 1326 free(port->init_devid, M_CTL); 1327 port->init_devid = malloc(sizeof(struct ctl_devid) + 1328 msg->port.init_devid_len, M_CTL, M_WAITOK); 1329 } 1330 memcpy(port->init_devid->data, &msg->port.data[i], 1331 msg->port.init_devid_len); 1332 port->init_devid->len = msg->port.init_devid_len; 1333 i += msg->port.init_devid_len; 1334 } else { 1335 free(port->init_devid, M_CTL); 1336 port->init_devid = NULL; 1337 } 1338 if (new) { 1339 if (ctl_port_register(port) != 0) { 1340 printf("%s: ctl_port_register() failed with error\n", 1341 __func__); 1342 } 1343 } 1344 mtx_lock(&softc->ctl_lock); 1345 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1346 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1347 continue; 1348 mtx_lock(&lun->lun_lock); 1349 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1350 mtx_unlock(&lun->lun_lock); 1351 } 1352 mtx_unlock(&softc->ctl_lock); 1353 } 1354 1355 static void 1356 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1357 { 1358 struct ctl_port *port; 1359 int i, iid; 1360 1361 if (len < offsetof(struct ctl_ha_msg_iid, data[0])) { 1362 printf("%s: Received truncated message %d < %zu\n", 1363 __func__, len, offsetof(struct ctl_ha_msg_iid, data[0])); 1364 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1365 return; 1366 } 1367 i = msg->iid.name_len; 1368 if (len < offsetof(struct ctl_ha_msg_iid, data[i])) { 1369 printf("%s: Received truncated message data %d < %zu\n", 1370 __func__, len, offsetof(struct ctl_ha_msg_iid, data[i])); 1371 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1372 return; 1373 } 1374 1375 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1376 if (port == NULL) { 1377 printf("%s: Received IID for unknown port %d\n", 1378 __func__, msg->hdr.nexus.targ_port); 1379 return; 1380 } 1381 iid = msg->hdr.nexus.initid; 1382 if (port->wwpn_iid[iid].in_use != 0 && 1383 msg->iid.in_use == 0) 1384 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1385 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1386 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1387 free(port->wwpn_iid[iid].name, M_CTL); 1388 if (msg->iid.name_len) { 1389 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1390 msg->iid.name_len, M_CTL); 1391 } else 1392 port->wwpn_iid[iid].name = NULL; 1393 } 1394 1395 static void 1396 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1397 { 1398 1399 if (len < sizeof(msg->login)) { 1400 printf("%s: Received truncated message %d < %zu\n", 1401 __func__, len, sizeof(msg->login)); 1402 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1403 return; 1404 } 1405 1406 if (msg->login.version != CTL_HA_VERSION) { 1407 printf("CTL HA peers have different versions %d != %d\n", 1408 msg->login.version, CTL_HA_VERSION); 1409 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1410 return; 1411 } 1412 if (msg->login.ha_mode != softc->ha_mode) { 1413 printf("CTL HA peers have different ha_mode %d != %d\n", 1414 msg->login.ha_mode, softc->ha_mode); 1415 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1416 return; 1417 } 1418 if (msg->login.ha_id == softc->ha_id) { 1419 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1420 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1421 return; 1422 } 1423 if (msg->login.max_luns != ctl_max_luns || 1424 msg->login.max_ports != ctl_max_ports || 1425 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1426 printf("CTL HA peers have different limits\n"); 1427 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1428 return; 1429 } 1430 } 1431 1432 static void 1433 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1434 { 1435 struct ctl_lun *lun; 1436 u_int i; 1437 uint32_t initidx, targ_lun; 1438 1439 if (len < offsetof(struct ctl_ha_msg_mode, data[0])) { 1440 printf("%s: Received truncated message %d < %zu\n", 1441 __func__, len, offsetof(struct ctl_ha_msg_mode, data[0])); 1442 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1443 return; 1444 } 1445 i = msg->mode.page_len; 1446 if (len < offsetof(struct ctl_ha_msg_mode, data[i])) { 1447 printf("%s: Received truncated message data %d < %zu\n", 1448 __func__, len, offsetof(struct ctl_ha_msg_mode, data[i])); 1449 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1450 return; 1451 } 1452 1453 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1454 mtx_lock(&softc->ctl_lock); 1455 if (targ_lun >= ctl_max_luns || 1456 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1457 mtx_unlock(&softc->ctl_lock); 1458 return; 1459 } 1460 mtx_lock(&lun->lun_lock); 1461 mtx_unlock(&softc->ctl_lock); 1462 if (lun->flags & CTL_LUN_DISABLED) { 1463 mtx_unlock(&lun->lun_lock); 1464 return; 1465 } 1466 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1467 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1468 msg->mode.page_code && 1469 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1470 break; 1471 } 1472 if (i == CTL_NUM_MODE_PAGES) { 1473 mtx_unlock(&lun->lun_lock); 1474 return; 1475 } 1476 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1477 min(lun->mode_pages.index[i].page_len, msg->mode.page_len)); 1478 initidx = ctl_get_initindex(&msg->hdr.nexus); 1479 if (initidx != -1) 1480 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1481 mtx_unlock(&lun->lun_lock); 1482 } 1483 1484 /* 1485 * ISC (Inter Shelf Communication) event handler. Events from the HA 1486 * subsystem come in here. 1487 */ 1488 static void 1489 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1490 { 1491 struct ctl_softc *softc = control_softc; 1492 union ctl_io *io; 1493 struct ctl_prio *presio; 1494 ctl_ha_status isc_status; 1495 1496 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1497 if (event == CTL_HA_EVT_MSG_RECV) { 1498 union ctl_ha_msg *msg, msgbuf; 1499 1500 if (param > sizeof(msgbuf)) 1501 msg = malloc(param, M_CTL, M_WAITOK); 1502 else 1503 msg = &msgbuf; 1504 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1505 M_WAITOK); 1506 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1507 printf("%s: Error receiving message: %d\n", 1508 __func__, isc_status); 1509 if (msg != &msgbuf) 1510 free(msg, M_CTL); 1511 return; 1512 } 1513 1514 CTL_DEBUG_PRINT(("CTL: msg_type %d len %d\n", 1515 msg->hdr.msg_type, param)); 1516 switch (msg->hdr.msg_type) { 1517 case CTL_MSG_SERIALIZE: 1518 io = ctl_alloc_io(softc->othersc_pool); 1519 ctl_zero_io(io); 1520 // populate ctsio from msg 1521 io->io_hdr.io_type = CTL_IO_SCSI; 1522 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1523 io->io_hdr.remote_io = msg->hdr.original_sc; 1524 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1525 CTL_FLAG_IO_ACTIVE; 1526 /* 1527 * If we're in serialization-only mode, we don't 1528 * want to go through full done processing. Thus 1529 * the COPY flag. 1530 * 1531 * XXX KDM add another flag that is more specific. 1532 */ 1533 if (softc->ha_mode != CTL_HA_MODE_XFER) 1534 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1535 io->io_hdr.nexus = msg->hdr.nexus; 1536 io->scsiio.priority = msg->scsi.priority; 1537 io->scsiio.tag_num = msg->scsi.tag_num; 1538 io->scsiio.tag_type = msg->scsi.tag_type; 1539 #ifdef CTL_TIME_IO 1540 io->io_hdr.start_time = time_uptime; 1541 getbinuptime(&io->io_hdr.start_bt); 1542 #endif /* CTL_TIME_IO */ 1543 io->scsiio.cdb_len = msg->scsi.cdb_len; 1544 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1545 CTL_MAX_CDBLEN); 1546 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1547 const struct ctl_cmd_entry *entry; 1548 1549 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1550 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1551 io->io_hdr.flags |= 1552 entry->flags & CTL_FLAG_DATA_MASK; 1553 } 1554 ctl_enqueue_isc(io); 1555 break; 1556 1557 /* Performed on the Originating SC, XFER mode only */ 1558 case CTL_MSG_DATAMOVE: { 1559 struct ctl_sg_entry *sgl; 1560 int i, j; 1561 1562 io = msg->hdr.original_sc; 1563 if (io == NULL) { 1564 printf("%s: original_sc == NULL!\n", __func__); 1565 /* XXX KDM do something here */ 1566 break; 1567 } 1568 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1569 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1570 /* 1571 * Keep track of this, we need to send it back over 1572 * when the datamove is complete. 1573 */ 1574 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1575 if (msg->hdr.status == CTL_SUCCESS) 1576 io->io_hdr.status = msg->hdr.status; 1577 1578 if (msg->dt.sg_sequence == 0) { 1579 #ifdef CTL_TIME_IO 1580 getbinuptime(&io->io_hdr.dma_start_bt); 1581 #endif 1582 i = msg->dt.kern_sg_entries + 1583 msg->dt.kern_data_len / 1584 CTL_HA_DATAMOVE_SEGMENT + 1; 1585 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1586 M_WAITOK | M_ZERO); 1587 CTL_RSGL(io) = sgl; 1588 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries]; 1589 1590 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1591 1592 io->scsiio.kern_sg_entries = 1593 msg->dt.kern_sg_entries; 1594 io->scsiio.rem_sg_entries = 1595 msg->dt.kern_sg_entries; 1596 io->scsiio.kern_data_len = 1597 msg->dt.kern_data_len; 1598 io->scsiio.kern_total_len = 1599 msg->dt.kern_total_len; 1600 io->scsiio.kern_data_resid = 1601 msg->dt.kern_data_resid; 1602 io->scsiio.kern_rel_offset = 1603 msg->dt.kern_rel_offset; 1604 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1605 io->io_hdr.flags |= msg->dt.flags & 1606 CTL_FLAG_BUS_ADDR; 1607 } else 1608 sgl = (struct ctl_sg_entry *) 1609 io->scsiio.kern_data_ptr; 1610 1611 for (i = msg->dt.sent_sg_entries, j = 0; 1612 i < (msg->dt.sent_sg_entries + 1613 msg->dt.cur_sg_entries); i++, j++) { 1614 sgl[i].addr = msg->dt.sg_list[j].addr; 1615 sgl[i].len = msg->dt.sg_list[j].len; 1616 } 1617 1618 /* 1619 * If this is the last piece of the I/O, we've got 1620 * the full S/G list. Queue processing in the thread. 1621 * Otherwise wait for the next piece. 1622 */ 1623 if (msg->dt.sg_last != 0) 1624 ctl_enqueue_isc(io); 1625 break; 1626 } 1627 /* Performed on the Serializing (primary) SC, XFER mode only */ 1628 case CTL_MSG_DATAMOVE_DONE: { 1629 if (msg->hdr.serializing_sc == NULL) { 1630 printf("%s: serializing_sc == NULL!\n", 1631 __func__); 1632 /* XXX KDM now what? */ 1633 break; 1634 } 1635 /* 1636 * We grab the sense information here in case 1637 * there was a failure, so we can return status 1638 * back to the initiator. 1639 */ 1640 io = msg->hdr.serializing_sc; 1641 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1642 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1643 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1644 io->io_hdr.port_status = msg->scsi.port_status; 1645 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1646 if (msg->hdr.status != CTL_STATUS_NONE) { 1647 io->io_hdr.status = msg->hdr.status; 1648 io->scsiio.scsi_status = msg->scsi.scsi_status; 1649 io->scsiio.sense_len = msg->scsi.sense_len; 1650 memcpy(&io->scsiio.sense_data, 1651 &msg->scsi.sense_data, 1652 msg->scsi.sense_len); 1653 if (msg->hdr.status == CTL_SUCCESS) 1654 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1655 } 1656 ctl_enqueue_isc(io); 1657 break; 1658 } 1659 1660 /* Preformed on Originating SC, SER_ONLY mode */ 1661 case CTL_MSG_R2R: 1662 io = msg->hdr.original_sc; 1663 if (io == NULL) { 1664 printf("%s: original_sc == NULL!\n", 1665 __func__); 1666 break; 1667 } 1668 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1669 io->io_hdr.msg_type = CTL_MSG_R2R; 1670 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1671 ctl_enqueue_isc(io); 1672 break; 1673 1674 /* 1675 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1676 * mode. 1677 * Performed on the Originating (i.e. secondary) SC in XFER 1678 * mode 1679 */ 1680 case CTL_MSG_FINISH_IO: 1681 if (softc->ha_mode == CTL_HA_MODE_XFER) 1682 ctl_isc_handler_finish_xfer(softc, msg); 1683 else 1684 ctl_isc_handler_finish_ser_only(softc, msg); 1685 break; 1686 1687 /* Preformed on Originating SC */ 1688 case CTL_MSG_BAD_JUJU: 1689 io = msg->hdr.original_sc; 1690 if (io == NULL) { 1691 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1692 __func__); 1693 break; 1694 } 1695 ctl_copy_sense_data(msg, io); 1696 /* 1697 * IO should have already been cleaned up on other 1698 * SC so clear this flag so we won't send a message 1699 * back to finish the IO there. 1700 */ 1701 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1702 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1703 1704 /* io = msg->hdr.serializing_sc; */ 1705 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1706 ctl_enqueue_isc(io); 1707 break; 1708 1709 /* Handle resets sent from the other side */ 1710 case CTL_MSG_MANAGE_TASKS: { 1711 struct ctl_taskio *taskio; 1712 taskio = (struct ctl_taskio *)ctl_alloc_io( 1713 softc->othersc_pool); 1714 ctl_zero_io((union ctl_io *)taskio); 1715 taskio->io_hdr.io_type = CTL_IO_TASK; 1716 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1717 taskio->io_hdr.nexus = msg->hdr.nexus; 1718 taskio->task_action = msg->task.task_action; 1719 taskio->tag_num = msg->task.tag_num; 1720 taskio->tag_type = msg->task.tag_type; 1721 #ifdef CTL_TIME_IO 1722 taskio->io_hdr.start_time = time_uptime; 1723 getbinuptime(&taskio->io_hdr.start_bt); 1724 #endif /* CTL_TIME_IO */ 1725 ctl_run_task((union ctl_io *)taskio); 1726 break; 1727 } 1728 /* Persistent Reserve action which needs attention */ 1729 case CTL_MSG_PERS_ACTION: 1730 presio = (struct ctl_prio *)ctl_alloc_io( 1731 softc->othersc_pool); 1732 ctl_zero_io((union ctl_io *)presio); 1733 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1734 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1735 presio->io_hdr.nexus = msg->hdr.nexus; 1736 presio->pr_msg = msg->pr; 1737 ctl_enqueue_isc((union ctl_io *)presio); 1738 break; 1739 case CTL_MSG_UA: 1740 ctl_isc_ua(softc, msg, param); 1741 break; 1742 case CTL_MSG_PORT_SYNC: 1743 ctl_isc_port_sync(softc, msg, param); 1744 break; 1745 case CTL_MSG_LUN_SYNC: 1746 ctl_isc_lun_sync(softc, msg, param); 1747 break; 1748 case CTL_MSG_IID_SYNC: 1749 ctl_isc_iid_sync(softc, msg, param); 1750 break; 1751 case CTL_MSG_LOGIN: 1752 ctl_isc_login(softc, msg, param); 1753 break; 1754 case CTL_MSG_MODE_SYNC: 1755 ctl_isc_mode_sync(softc, msg, param); 1756 break; 1757 default: 1758 printf("Received HA message of unknown type %d\n", 1759 msg->hdr.msg_type); 1760 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1761 break; 1762 } 1763 if (msg != &msgbuf) 1764 free(msg, M_CTL); 1765 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1766 printf("CTL: HA link status changed from %d to %d\n", 1767 softc->ha_link, param); 1768 if (param == softc->ha_link) 1769 return; 1770 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1771 softc->ha_link = param; 1772 ctl_isc_ha_link_down(softc); 1773 } else { 1774 softc->ha_link = param; 1775 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1776 ctl_isc_ha_link_up(softc); 1777 } 1778 return; 1779 } else { 1780 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1781 return; 1782 } 1783 } 1784 1785 static void 1786 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1787 { 1788 1789 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1790 src->scsi.sense_len); 1791 dest->scsiio.scsi_status = src->scsi.scsi_status; 1792 dest->scsiio.sense_len = src->scsi.sense_len; 1793 dest->io_hdr.status = src->hdr.status; 1794 } 1795 1796 static void 1797 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1798 { 1799 1800 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1801 src->scsiio.sense_len); 1802 dest->scsi.scsi_status = src->scsiio.scsi_status; 1803 dest->scsi.sense_len = src->scsiio.sense_len; 1804 dest->hdr.status = src->io_hdr.status; 1805 } 1806 1807 void 1808 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1809 { 1810 struct ctl_softc *softc = lun->ctl_softc; 1811 ctl_ua_type *pu; 1812 1813 if (initidx < softc->init_min || initidx >= softc->init_max) 1814 return; 1815 mtx_assert(&lun->lun_lock, MA_OWNED); 1816 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1817 if (pu == NULL) 1818 return; 1819 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1820 } 1821 1822 void 1823 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1824 { 1825 int i; 1826 1827 mtx_assert(&lun->lun_lock, MA_OWNED); 1828 if (lun->pending_ua[port] == NULL) 1829 return; 1830 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1831 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1832 continue; 1833 lun->pending_ua[port][i] |= ua; 1834 } 1835 } 1836 1837 void 1838 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1839 { 1840 struct ctl_softc *softc = lun->ctl_softc; 1841 int i; 1842 1843 mtx_assert(&lun->lun_lock, MA_OWNED); 1844 for (i = softc->port_min; i < softc->port_max; i++) 1845 ctl_est_ua_port(lun, i, except, ua); 1846 } 1847 1848 void 1849 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1850 { 1851 struct ctl_softc *softc = lun->ctl_softc; 1852 ctl_ua_type *pu; 1853 1854 if (initidx < softc->init_min || initidx >= softc->init_max) 1855 return; 1856 mtx_assert(&lun->lun_lock, MA_OWNED); 1857 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1858 if (pu == NULL) 1859 return; 1860 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1861 } 1862 1863 void 1864 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1865 { 1866 struct ctl_softc *softc = lun->ctl_softc; 1867 int i, j; 1868 1869 mtx_assert(&lun->lun_lock, MA_OWNED); 1870 for (i = softc->port_min; i < softc->port_max; i++) { 1871 if (lun->pending_ua[i] == NULL) 1872 continue; 1873 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1874 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1875 continue; 1876 lun->pending_ua[i][j] &= ~ua; 1877 } 1878 } 1879 } 1880 1881 void 1882 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1883 ctl_ua_type ua_type) 1884 { 1885 struct ctl_lun *lun; 1886 1887 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1888 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1889 mtx_lock(&lun->lun_lock); 1890 ctl_clr_ua(lun, initidx, ua_type); 1891 mtx_unlock(&lun->lun_lock); 1892 } 1893 } 1894 1895 static int 1896 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1897 { 1898 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1899 struct ctl_lun *lun; 1900 struct ctl_lun_req ireq; 1901 int error, value; 1902 1903 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1904 error = sysctl_handle_int(oidp, &value, 0, req); 1905 if ((error != 0) || (req->newptr == NULL)) 1906 return (error); 1907 1908 mtx_lock(&softc->ctl_lock); 1909 if (value == 0) 1910 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1911 else 1912 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1913 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1914 mtx_unlock(&softc->ctl_lock); 1915 bzero(&ireq, sizeof(ireq)); 1916 ireq.reqtype = CTL_LUNREQ_MODIFY; 1917 ireq.reqdata.modify.lun_id = lun->lun; 1918 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1919 curthread); 1920 if (ireq.status != CTL_LUN_OK) { 1921 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1922 __func__, ireq.status, ireq.error_str); 1923 } 1924 mtx_lock(&softc->ctl_lock); 1925 } 1926 mtx_unlock(&softc->ctl_lock); 1927 return (0); 1928 } 1929 1930 static int 1931 ctl_init(void) 1932 { 1933 struct make_dev_args args; 1934 struct ctl_softc *softc; 1935 int i, error; 1936 1937 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1938 M_WAITOK | M_ZERO); 1939 1940 make_dev_args_init(&args); 1941 args.mda_devsw = &ctl_cdevsw; 1942 args.mda_uid = UID_ROOT; 1943 args.mda_gid = GID_OPERATOR; 1944 args.mda_mode = 0600; 1945 args.mda_si_drv1 = softc; 1946 args.mda_si_drv2 = NULL; 1947 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1948 if (error != 0) { 1949 free(softc, M_DEVBUF); 1950 control_softc = NULL; 1951 return (error); 1952 } 1953 1954 sysctl_ctx_init(&softc->sysctl_ctx); 1955 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1956 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1957 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer"); 1958 1959 if (softc->sysctl_tree == NULL) { 1960 printf("%s: unable to allocate sysctl tree\n", __func__); 1961 destroy_dev(softc->dev); 1962 free(softc, M_DEVBUF); 1963 control_softc = NULL; 1964 return (ENOMEM); 1965 } 1966 1967 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1968 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1969 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1970 softc->flags = 0; 1971 1972 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1973 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1974 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1975 1976 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) { 1977 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n", 1978 ctl_max_luns, CTL_DEFAULT_MAX_LUNS); 1979 ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 1980 } 1981 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns, 1982 M_DEVBUF, M_WAITOK | M_ZERO); 1983 softc->ctl_lun_mask = malloc(sizeof(uint32_t) * 1984 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1985 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) { 1986 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n", 1987 ctl_max_ports, CTL_DEFAULT_MAX_PORTS); 1988 ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 1989 } 1990 softc->ctl_port_mask = malloc(sizeof(uint32_t) * 1991 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1992 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports, 1993 M_DEVBUF, M_WAITOK | M_ZERO); 1994 1995 /* 1996 * In Copan's HA scheme, the "master" and "slave" roles are 1997 * figured out through the slot the controller is in. Although it 1998 * is an active/active system, someone has to be in charge. 1999 */ 2000 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 2001 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 2002 "HA head ID (0 - no HA)"); 2003 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 2004 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 2005 softc->is_single = 1; 2006 softc->port_cnt = ctl_max_ports; 2007 softc->port_min = 0; 2008 } else { 2009 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 2010 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 2011 } 2012 softc->port_max = softc->port_min + softc->port_cnt; 2013 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 2014 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 2015 2016 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 2017 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 2018 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 2019 2020 STAILQ_INIT(&softc->lun_list); 2021 STAILQ_INIT(&softc->fe_list); 2022 STAILQ_INIT(&softc->port_list); 2023 STAILQ_INIT(&softc->be_list); 2024 ctl_tpc_init(softc); 2025 2026 if (worker_threads <= 0) 2027 worker_threads = max(1, mp_ncpus / 4); 2028 if (worker_threads > CTL_MAX_THREADS) 2029 worker_threads = CTL_MAX_THREADS; 2030 2031 for (i = 0; i < worker_threads; i++) { 2032 struct ctl_thread *thr = &softc->threads[i]; 2033 2034 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 2035 thr->ctl_softc = softc; 2036 STAILQ_INIT(&thr->incoming_queue); 2037 STAILQ_INIT(&thr->rtr_queue); 2038 STAILQ_INIT(&thr->done_queue); 2039 STAILQ_INIT(&thr->isc_queue); 2040 2041 error = kproc_kthread_add(ctl_work_thread, thr, 2042 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 2043 if (error != 0) { 2044 printf("error creating CTL work thread!\n"); 2045 return (error); 2046 } 2047 } 2048 error = kproc_kthread_add(ctl_thresh_thread, softc, 2049 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 2050 if (error != 0) { 2051 printf("error creating CTL threshold thread!\n"); 2052 return (error); 2053 } 2054 2055 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 2056 OID_AUTO, "ha_role", 2057 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 2058 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 2059 2060 if (softc->is_single == 0) { 2061 if (ctl_frontend_register(&ha_frontend) != 0) 2062 softc->is_single = 1; 2063 } 2064 return (0); 2065 } 2066 2067 static int 2068 ctl_shutdown(void) 2069 { 2070 struct ctl_softc *softc = control_softc; 2071 int i; 2072 2073 if (softc->is_single == 0) 2074 ctl_frontend_deregister(&ha_frontend); 2075 2076 destroy_dev(softc->dev); 2077 2078 /* Shutdown CTL threads. */ 2079 softc->shutdown = 1; 2080 for (i = 0; i < worker_threads; i++) { 2081 struct ctl_thread *thr = &softc->threads[i]; 2082 while (thr->thread != NULL) { 2083 wakeup(thr); 2084 if (thr->thread != NULL) 2085 pause("CTL thr shutdown", 1); 2086 } 2087 mtx_destroy(&thr->queue_lock); 2088 } 2089 while (softc->thresh_thread != NULL) { 2090 wakeup(softc->thresh_thread); 2091 if (softc->thresh_thread != NULL) 2092 pause("CTL thr shutdown", 1); 2093 } 2094 2095 ctl_tpc_shutdown(softc); 2096 uma_zdestroy(softc->io_zone); 2097 mtx_destroy(&softc->ctl_lock); 2098 2099 free(softc->ctl_luns, M_DEVBUF); 2100 free(softc->ctl_lun_mask, M_DEVBUF); 2101 free(softc->ctl_port_mask, M_DEVBUF); 2102 free(softc->ctl_ports, M_DEVBUF); 2103 2104 sysctl_ctx_free(&softc->sysctl_ctx); 2105 2106 free(softc, M_DEVBUF); 2107 control_softc = NULL; 2108 return (0); 2109 } 2110 2111 static int 2112 ctl_module_event_handler(module_t mod, int what, void *arg) 2113 { 2114 2115 switch (what) { 2116 case MOD_LOAD: 2117 return (ctl_init()); 2118 case MOD_UNLOAD: 2119 return (ctl_shutdown()); 2120 default: 2121 return (EOPNOTSUPP); 2122 } 2123 } 2124 2125 /* 2126 * XXX KDM should we do some access checks here? Bump a reference count to 2127 * prevent a CTL module from being unloaded while someone has it open? 2128 */ 2129 static int 2130 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2131 { 2132 return (0); 2133 } 2134 2135 static int 2136 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2137 { 2138 return (0); 2139 } 2140 2141 /* 2142 * Remove an initiator by port number and initiator ID. 2143 * Returns 0 for success, -1 for failure. 2144 */ 2145 int 2146 ctl_remove_initiator(struct ctl_port *port, int iid) 2147 { 2148 struct ctl_softc *softc = port->ctl_softc; 2149 int last; 2150 2151 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2152 2153 if (iid > CTL_MAX_INIT_PER_PORT) { 2154 printf("%s: initiator ID %u > maximun %u!\n", 2155 __func__, iid, CTL_MAX_INIT_PER_PORT); 2156 return (-1); 2157 } 2158 2159 mtx_lock(&softc->ctl_lock); 2160 last = (--port->wwpn_iid[iid].in_use == 0); 2161 port->wwpn_iid[iid].last_use = time_uptime; 2162 mtx_unlock(&softc->ctl_lock); 2163 if (last) 2164 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2165 ctl_isc_announce_iid(port, iid); 2166 2167 return (0); 2168 } 2169 2170 /* 2171 * Add an initiator to the initiator map. 2172 * Returns iid for success, < 0 for failure. 2173 */ 2174 int 2175 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2176 { 2177 struct ctl_softc *softc = port->ctl_softc; 2178 time_t best_time; 2179 int i, best; 2180 2181 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2182 2183 if (iid >= CTL_MAX_INIT_PER_PORT) { 2184 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2185 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2186 free(name, M_CTL); 2187 return (-1); 2188 } 2189 2190 mtx_lock(&softc->ctl_lock); 2191 2192 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2193 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2194 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2195 iid = i; 2196 break; 2197 } 2198 if (name != NULL && port->wwpn_iid[i].name != NULL && 2199 strcmp(name, port->wwpn_iid[i].name) == 0) { 2200 iid = i; 2201 break; 2202 } 2203 } 2204 } 2205 2206 if (iid < 0) { 2207 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2208 if (port->wwpn_iid[i].in_use == 0 && 2209 port->wwpn_iid[i].wwpn == 0 && 2210 port->wwpn_iid[i].name == NULL) { 2211 iid = i; 2212 break; 2213 } 2214 } 2215 } 2216 2217 if (iid < 0) { 2218 best = -1; 2219 best_time = INT32_MAX; 2220 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2221 if (port->wwpn_iid[i].in_use == 0) { 2222 if (port->wwpn_iid[i].last_use < best_time) { 2223 best = i; 2224 best_time = port->wwpn_iid[i].last_use; 2225 } 2226 } 2227 } 2228 iid = best; 2229 } 2230 2231 if (iid < 0) { 2232 mtx_unlock(&softc->ctl_lock); 2233 free(name, M_CTL); 2234 return (-2); 2235 } 2236 2237 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2238 /* 2239 * This is not an error yet. 2240 */ 2241 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2242 #if 0 2243 printf("%s: port %d iid %u WWPN %#jx arrived" 2244 " again\n", __func__, port->targ_port, 2245 iid, (uintmax_t)wwpn); 2246 #endif 2247 goto take; 2248 } 2249 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2250 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2251 #if 0 2252 printf("%s: port %d iid %u name '%s' arrived" 2253 " again\n", __func__, port->targ_port, 2254 iid, name); 2255 #endif 2256 goto take; 2257 } 2258 2259 /* 2260 * This is an error, but what do we do about it? The 2261 * driver is telling us we have a new WWPN for this 2262 * initiator ID, so we pretty much need to use it. 2263 */ 2264 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2265 " but WWPN %#jx '%s' is still at that address\n", 2266 __func__, port->targ_port, iid, wwpn, name, 2267 (uintmax_t)port->wwpn_iid[iid].wwpn, 2268 port->wwpn_iid[iid].name); 2269 } 2270 take: 2271 free(port->wwpn_iid[iid].name, M_CTL); 2272 port->wwpn_iid[iid].name = name; 2273 port->wwpn_iid[iid].wwpn = wwpn; 2274 port->wwpn_iid[iid].in_use++; 2275 mtx_unlock(&softc->ctl_lock); 2276 ctl_isc_announce_iid(port, iid); 2277 2278 return (iid); 2279 } 2280 2281 static int 2282 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2283 { 2284 int len; 2285 2286 switch (port->port_type) { 2287 case CTL_PORT_FC: 2288 { 2289 struct scsi_transportid_fcp *id = 2290 (struct scsi_transportid_fcp *)buf; 2291 if (port->wwpn_iid[iid].wwpn == 0) 2292 return (0); 2293 memset(id, 0, sizeof(*id)); 2294 id->format_protocol = SCSI_PROTO_FC; 2295 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2296 return (sizeof(*id)); 2297 } 2298 case CTL_PORT_ISCSI: 2299 { 2300 struct scsi_transportid_iscsi_port *id = 2301 (struct scsi_transportid_iscsi_port *)buf; 2302 if (port->wwpn_iid[iid].name == NULL) 2303 return (0); 2304 memset(id, 0, 256); 2305 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2306 SCSI_PROTO_ISCSI; 2307 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2308 len = roundup2(min(len, 252), 4); 2309 scsi_ulto2b(len, id->additional_length); 2310 return (sizeof(*id) + len); 2311 } 2312 case CTL_PORT_SAS: 2313 { 2314 struct scsi_transportid_sas *id = 2315 (struct scsi_transportid_sas *)buf; 2316 if (port->wwpn_iid[iid].wwpn == 0) 2317 return (0); 2318 memset(id, 0, sizeof(*id)); 2319 id->format_protocol = SCSI_PROTO_SAS; 2320 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2321 return (sizeof(*id)); 2322 } 2323 default: 2324 { 2325 struct scsi_transportid_spi *id = 2326 (struct scsi_transportid_spi *)buf; 2327 memset(id, 0, sizeof(*id)); 2328 id->format_protocol = SCSI_PROTO_SPI; 2329 scsi_ulto2b(iid, id->scsi_addr); 2330 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2331 return (sizeof(*id)); 2332 } 2333 } 2334 } 2335 2336 /* 2337 * Serialize a command that went down the "wrong" side, and so was sent to 2338 * this controller for execution. The logic is a little different than the 2339 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2340 * sent back to the other side, but in the success case, we execute the 2341 * command on this side (XFER mode) or tell the other side to execute it 2342 * (SER_ONLY mode). 2343 */ 2344 static void 2345 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2346 { 2347 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2348 struct ctl_port *port = CTL_PORT(ctsio); 2349 union ctl_ha_msg msg_info; 2350 struct ctl_lun *lun; 2351 const struct ctl_cmd_entry *entry; 2352 union ctl_io *bio; 2353 uint32_t targ_lun; 2354 2355 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2356 2357 /* Make sure that we know about this port. */ 2358 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2359 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2360 /*retry_count*/ 1); 2361 goto badjuju; 2362 } 2363 2364 /* Make sure that we know about this LUN. */ 2365 mtx_lock(&softc->ctl_lock); 2366 if (targ_lun >= ctl_max_luns || 2367 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2368 mtx_unlock(&softc->ctl_lock); 2369 2370 /* 2371 * The other node would not send this request to us unless 2372 * received announce that we are primary node for this LUN. 2373 * If this LUN does not exist now, it is probably result of 2374 * a race, so respond to initiator in the most opaque way. 2375 */ 2376 ctl_set_busy(ctsio); 2377 goto badjuju; 2378 } 2379 mtx_lock(&lun->lun_lock); 2380 mtx_unlock(&softc->ctl_lock); 2381 2382 /* 2383 * If the LUN is invalid, pretend that it doesn't exist. 2384 * It will go away as soon as all pending I/Os completed. 2385 */ 2386 if (lun->flags & CTL_LUN_DISABLED) { 2387 mtx_unlock(&lun->lun_lock); 2388 ctl_set_busy(ctsio); 2389 goto badjuju; 2390 } 2391 2392 entry = ctl_get_cmd_entry(ctsio, NULL); 2393 ctsio->seridx = entry->seridx; 2394 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2395 mtx_unlock(&lun->lun_lock); 2396 goto badjuju; 2397 } 2398 2399 CTL_LUN(ctsio) = lun; 2400 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2401 2402 /* 2403 * Every I/O goes into the OOA queue for a 2404 * particular LUN, and stays there until completion. 2405 */ 2406 #ifdef CTL_TIME_IO 2407 if (LIST_EMPTY(&lun->ooa_queue)) 2408 lun->idle_time += getsbinuptime() - lun->last_busy; 2409 #endif 2410 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2411 2412 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 2413 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 2414 case CTL_ACTION_PASS: 2415 case CTL_ACTION_SKIP: 2416 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2417 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2418 ctl_enqueue_rtr((union ctl_io *)ctsio); 2419 mtx_unlock(&lun->lun_lock); 2420 } else { 2421 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2422 mtx_unlock(&lun->lun_lock); 2423 2424 /* send msg back to other side */ 2425 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2426 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2427 msg_info.hdr.msg_type = CTL_MSG_R2R; 2428 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2429 sizeof(msg_info.hdr), M_WAITOK); 2430 } 2431 break; 2432 case CTL_ACTION_BLOCK: 2433 ctsio->io_hdr.blocker = bio; 2434 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 2435 blocked_links); 2436 mtx_unlock(&lun->lun_lock); 2437 break; 2438 case CTL_ACTION_OVERLAP: 2439 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2440 mtx_unlock(&lun->lun_lock); 2441 ctl_set_overlapped_cmd(ctsio); 2442 goto badjuju; 2443 case CTL_ACTION_OVERLAP_TAG: 2444 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2445 mtx_unlock(&lun->lun_lock); 2446 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2447 badjuju: 2448 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2449 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2450 msg_info.hdr.serializing_sc = NULL; 2451 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2452 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2453 sizeof(msg_info.scsi), M_WAITOK); 2454 ctl_free_io((union ctl_io *)ctsio); 2455 break; 2456 default: 2457 __assert_unreachable(); 2458 } 2459 } 2460 2461 /* 2462 * Returns 0 for success, errno for failure. 2463 */ 2464 static void 2465 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2466 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2467 { 2468 struct ctl_io_hdr *ioh; 2469 2470 mtx_lock(&lun->lun_lock); 2471 ioh = LIST_FIRST(&lun->ooa_queue); 2472 if (ioh == NULL) { 2473 mtx_unlock(&lun->lun_lock); 2474 return; 2475 } 2476 while (LIST_NEXT(ioh, ooa_links) != NULL) 2477 ioh = LIST_NEXT(ioh, ooa_links); 2478 for ( ; ioh; ioh = LIST_PREV(ioh, &lun->ooa_queue, ctl_io_hdr, ooa_links)) { 2479 union ctl_io *io = (union ctl_io *)ioh; 2480 struct ctl_ooa_entry *entry; 2481 2482 /* 2483 * If we've got more than we can fit, just count the 2484 * remaining entries. 2485 */ 2486 if (*cur_fill_num >= ooa_hdr->alloc_num) { 2487 (*cur_fill_num)++; 2488 continue; 2489 } 2490 2491 entry = &kern_entries[*cur_fill_num]; 2492 2493 entry->tag_num = io->scsiio.tag_num; 2494 entry->lun_num = lun->lun; 2495 #ifdef CTL_TIME_IO 2496 entry->start_bt = io->io_hdr.start_bt; 2497 #endif 2498 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2499 entry->cdb_len = io->scsiio.cdb_len; 2500 if (io->io_hdr.blocker != NULL) 2501 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2502 2503 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2504 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2505 2506 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2507 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2508 2509 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2510 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2511 2512 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2513 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2514 2515 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) 2516 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED; 2517 2518 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) 2519 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT; 2520 (*cur_fill_num)++; 2521 } 2522 mtx_unlock(&lun->lun_lock); 2523 } 2524 2525 /* 2526 * Escape characters that are illegal or not recommended in XML. 2527 */ 2528 int 2529 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2530 { 2531 char *end = str + size; 2532 int retval; 2533 2534 retval = 0; 2535 2536 for (; *str && str < end; str++) { 2537 switch (*str) { 2538 case '&': 2539 retval = sbuf_printf(sb, "&"); 2540 break; 2541 case '>': 2542 retval = sbuf_printf(sb, ">"); 2543 break; 2544 case '<': 2545 retval = sbuf_printf(sb, "<"); 2546 break; 2547 default: 2548 retval = sbuf_putc(sb, *str); 2549 break; 2550 } 2551 2552 if (retval != 0) 2553 break; 2554 } 2555 2556 return (retval); 2557 } 2558 2559 static void 2560 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2561 { 2562 struct scsi_vpd_id_descriptor *desc; 2563 int i; 2564 2565 if (id == NULL || id->len < 4) 2566 return; 2567 desc = (struct scsi_vpd_id_descriptor *)id->data; 2568 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2569 case SVPD_ID_TYPE_T10: 2570 sbuf_printf(sb, "t10."); 2571 break; 2572 case SVPD_ID_TYPE_EUI64: 2573 sbuf_printf(sb, "eui."); 2574 break; 2575 case SVPD_ID_TYPE_NAA: 2576 sbuf_printf(sb, "naa."); 2577 break; 2578 case SVPD_ID_TYPE_SCSI_NAME: 2579 break; 2580 } 2581 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2582 case SVPD_ID_CODESET_BINARY: 2583 for (i = 0; i < desc->length; i++) 2584 sbuf_printf(sb, "%02x", desc->identifier[i]); 2585 break; 2586 case SVPD_ID_CODESET_ASCII: 2587 sbuf_printf(sb, "%.*s", (int)desc->length, 2588 (char *)desc->identifier); 2589 break; 2590 case SVPD_ID_CODESET_UTF8: 2591 sbuf_printf(sb, "%s", (char *)desc->identifier); 2592 break; 2593 } 2594 } 2595 2596 static int 2597 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2598 struct thread *td) 2599 { 2600 struct ctl_softc *softc = dev->si_drv1; 2601 struct ctl_port *port; 2602 struct ctl_lun *lun; 2603 int retval; 2604 2605 retval = 0; 2606 2607 switch (cmd) { 2608 case CTL_IO: 2609 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2610 break; 2611 case CTL_ENABLE_PORT: 2612 case CTL_DISABLE_PORT: 2613 case CTL_SET_PORT_WWNS: { 2614 struct ctl_port *port; 2615 struct ctl_port_entry *entry; 2616 2617 entry = (struct ctl_port_entry *)addr; 2618 2619 mtx_lock(&softc->ctl_lock); 2620 STAILQ_FOREACH(port, &softc->port_list, links) { 2621 int action, done; 2622 2623 if (port->targ_port < softc->port_min || 2624 port->targ_port >= softc->port_max) 2625 continue; 2626 2627 action = 0; 2628 done = 0; 2629 if ((entry->port_type == CTL_PORT_NONE) 2630 && (entry->targ_port == port->targ_port)) { 2631 /* 2632 * If the user only wants to enable or 2633 * disable or set WWNs on a specific port, 2634 * do the operation and we're done. 2635 */ 2636 action = 1; 2637 done = 1; 2638 } else if (entry->port_type & port->port_type) { 2639 /* 2640 * Compare the user's type mask with the 2641 * particular frontend type to see if we 2642 * have a match. 2643 */ 2644 action = 1; 2645 done = 0; 2646 2647 /* 2648 * Make sure the user isn't trying to set 2649 * WWNs on multiple ports at the same time. 2650 */ 2651 if (cmd == CTL_SET_PORT_WWNS) { 2652 printf("%s: Can't set WWNs on " 2653 "multiple ports\n", __func__); 2654 retval = EINVAL; 2655 break; 2656 } 2657 } 2658 if (action == 0) 2659 continue; 2660 2661 /* 2662 * XXX KDM we have to drop the lock here, because 2663 * the online/offline operations can potentially 2664 * block. We need to reference count the frontends 2665 * so they can't go away, 2666 */ 2667 if (cmd == CTL_ENABLE_PORT) { 2668 mtx_unlock(&softc->ctl_lock); 2669 ctl_port_online(port); 2670 mtx_lock(&softc->ctl_lock); 2671 } else if (cmd == CTL_DISABLE_PORT) { 2672 mtx_unlock(&softc->ctl_lock); 2673 ctl_port_offline(port); 2674 mtx_lock(&softc->ctl_lock); 2675 } else if (cmd == CTL_SET_PORT_WWNS) { 2676 ctl_port_set_wwns(port, 2677 (entry->flags & CTL_PORT_WWNN_VALID) ? 2678 1 : 0, entry->wwnn, 2679 (entry->flags & CTL_PORT_WWPN_VALID) ? 2680 1 : 0, entry->wwpn); 2681 } 2682 if (done != 0) 2683 break; 2684 } 2685 mtx_unlock(&softc->ctl_lock); 2686 break; 2687 } 2688 case CTL_GET_OOA: { 2689 struct ctl_ooa *ooa_hdr; 2690 struct ctl_ooa_entry *entries; 2691 uint32_t cur_fill_num; 2692 2693 ooa_hdr = (struct ctl_ooa *)addr; 2694 2695 if ((ooa_hdr->alloc_len == 0) 2696 || (ooa_hdr->alloc_num == 0)) { 2697 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2698 "must be non-zero\n", __func__, 2699 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2700 retval = EINVAL; 2701 break; 2702 } 2703 2704 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2705 sizeof(struct ctl_ooa_entry))) { 2706 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2707 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2708 __func__, ooa_hdr->alloc_len, 2709 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2710 retval = EINVAL; 2711 break; 2712 } 2713 2714 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2715 if (entries == NULL) { 2716 printf("%s: could not allocate %d bytes for OOA " 2717 "dump\n", __func__, ooa_hdr->alloc_len); 2718 retval = ENOMEM; 2719 break; 2720 } 2721 2722 mtx_lock(&softc->ctl_lock); 2723 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2724 (ooa_hdr->lun_num >= ctl_max_luns || 2725 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2726 mtx_unlock(&softc->ctl_lock); 2727 free(entries, M_CTL); 2728 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2729 __func__, (uintmax_t)ooa_hdr->lun_num); 2730 retval = EINVAL; 2731 break; 2732 } 2733 2734 cur_fill_num = 0; 2735 2736 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2737 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2738 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2739 ooa_hdr, entries); 2740 } 2741 } else { 2742 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2743 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2744 entries); 2745 } 2746 mtx_unlock(&softc->ctl_lock); 2747 2748 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2749 ooa_hdr->fill_len = ooa_hdr->fill_num * 2750 sizeof(struct ctl_ooa_entry); 2751 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2752 if (retval != 0) { 2753 printf("%s: error copying out %d bytes for OOA dump\n", 2754 __func__, ooa_hdr->fill_len); 2755 } 2756 2757 getbinuptime(&ooa_hdr->cur_bt); 2758 2759 if (cur_fill_num > ooa_hdr->alloc_num) { 2760 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2761 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2762 } else { 2763 ooa_hdr->dropped_num = 0; 2764 ooa_hdr->status = CTL_OOA_OK; 2765 } 2766 2767 free(entries, M_CTL); 2768 break; 2769 } 2770 case CTL_DELAY_IO: { 2771 struct ctl_io_delay_info *delay_info; 2772 2773 delay_info = (struct ctl_io_delay_info *)addr; 2774 2775 #ifdef CTL_IO_DELAY 2776 mtx_lock(&softc->ctl_lock); 2777 if (delay_info->lun_id >= ctl_max_luns || 2778 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2779 mtx_unlock(&softc->ctl_lock); 2780 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2781 break; 2782 } 2783 mtx_lock(&lun->lun_lock); 2784 mtx_unlock(&softc->ctl_lock); 2785 delay_info->status = CTL_DELAY_STATUS_OK; 2786 switch (delay_info->delay_type) { 2787 case CTL_DELAY_TYPE_CONT: 2788 case CTL_DELAY_TYPE_ONESHOT: 2789 break; 2790 default: 2791 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2792 break; 2793 } 2794 switch (delay_info->delay_loc) { 2795 case CTL_DELAY_LOC_DATAMOVE: 2796 lun->delay_info.datamove_type = delay_info->delay_type; 2797 lun->delay_info.datamove_delay = delay_info->delay_secs; 2798 break; 2799 case CTL_DELAY_LOC_DONE: 2800 lun->delay_info.done_type = delay_info->delay_type; 2801 lun->delay_info.done_delay = delay_info->delay_secs; 2802 break; 2803 default: 2804 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2805 break; 2806 } 2807 mtx_unlock(&lun->lun_lock); 2808 #else 2809 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2810 #endif /* CTL_IO_DELAY */ 2811 break; 2812 } 2813 case CTL_ERROR_INJECT: { 2814 struct ctl_error_desc *err_desc, *new_err_desc; 2815 2816 err_desc = (struct ctl_error_desc *)addr; 2817 2818 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2819 M_WAITOK | M_ZERO); 2820 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2821 2822 mtx_lock(&softc->ctl_lock); 2823 if (err_desc->lun_id >= ctl_max_luns || 2824 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2825 mtx_unlock(&softc->ctl_lock); 2826 free(new_err_desc, M_CTL); 2827 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2828 __func__, (uintmax_t)err_desc->lun_id); 2829 retval = EINVAL; 2830 break; 2831 } 2832 mtx_lock(&lun->lun_lock); 2833 mtx_unlock(&softc->ctl_lock); 2834 2835 /* 2836 * We could do some checking here to verify the validity 2837 * of the request, but given the complexity of error 2838 * injection requests, the checking logic would be fairly 2839 * complex. 2840 * 2841 * For now, if the request is invalid, it just won't get 2842 * executed and might get deleted. 2843 */ 2844 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2845 2846 /* 2847 * XXX KDM check to make sure the serial number is unique, 2848 * in case we somehow manage to wrap. That shouldn't 2849 * happen for a very long time, but it's the right thing to 2850 * do. 2851 */ 2852 new_err_desc->serial = lun->error_serial; 2853 err_desc->serial = lun->error_serial; 2854 lun->error_serial++; 2855 2856 mtx_unlock(&lun->lun_lock); 2857 break; 2858 } 2859 case CTL_ERROR_INJECT_DELETE: { 2860 struct ctl_error_desc *delete_desc, *desc, *desc2; 2861 int delete_done; 2862 2863 delete_desc = (struct ctl_error_desc *)addr; 2864 delete_done = 0; 2865 2866 mtx_lock(&softc->ctl_lock); 2867 if (delete_desc->lun_id >= ctl_max_luns || 2868 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2869 mtx_unlock(&softc->ctl_lock); 2870 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2871 __func__, (uintmax_t)delete_desc->lun_id); 2872 retval = EINVAL; 2873 break; 2874 } 2875 mtx_lock(&lun->lun_lock); 2876 mtx_unlock(&softc->ctl_lock); 2877 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2878 if (desc->serial != delete_desc->serial) 2879 continue; 2880 2881 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2882 links); 2883 free(desc, M_CTL); 2884 delete_done = 1; 2885 } 2886 mtx_unlock(&lun->lun_lock); 2887 if (delete_done == 0) { 2888 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2889 "error serial %ju on LUN %u\n", __func__, 2890 delete_desc->serial, delete_desc->lun_id); 2891 retval = EINVAL; 2892 break; 2893 } 2894 break; 2895 } 2896 case CTL_DUMP_STRUCTS: { 2897 int j, k; 2898 struct ctl_port *port; 2899 struct ctl_frontend *fe; 2900 2901 mtx_lock(&softc->ctl_lock); 2902 printf("CTL Persistent Reservation information start:\n"); 2903 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2904 mtx_lock(&lun->lun_lock); 2905 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2906 mtx_unlock(&lun->lun_lock); 2907 continue; 2908 } 2909 2910 for (j = 0; j < ctl_max_ports; j++) { 2911 if (lun->pr_keys[j] == NULL) 2912 continue; 2913 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2914 if (lun->pr_keys[j][k] == 0) 2915 continue; 2916 printf(" LUN %ju port %d iid %d key " 2917 "%#jx\n", lun->lun, j, k, 2918 (uintmax_t)lun->pr_keys[j][k]); 2919 } 2920 } 2921 mtx_unlock(&lun->lun_lock); 2922 } 2923 printf("CTL Persistent Reservation information end\n"); 2924 printf("CTL Ports:\n"); 2925 STAILQ_FOREACH(port, &softc->port_list, links) { 2926 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2927 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2928 port->frontend->name, port->port_type, 2929 port->physical_port, port->virtual_port, 2930 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2931 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2932 if (port->wwpn_iid[j].in_use == 0 && 2933 port->wwpn_iid[j].wwpn == 0 && 2934 port->wwpn_iid[j].name == NULL) 2935 continue; 2936 2937 printf(" iid %u use %d WWPN %#jx '%s'\n", 2938 j, port->wwpn_iid[j].in_use, 2939 (uintmax_t)port->wwpn_iid[j].wwpn, 2940 port->wwpn_iid[j].name); 2941 } 2942 } 2943 printf("CTL Port information end\n"); 2944 mtx_unlock(&softc->ctl_lock); 2945 /* 2946 * XXX KDM calling this without a lock. We'd likely want 2947 * to drop the lock before calling the frontend's dump 2948 * routine anyway. 2949 */ 2950 printf("CTL Frontends:\n"); 2951 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2952 printf(" Frontend '%s'\n", fe->name); 2953 if (fe->fe_dump != NULL) 2954 fe->fe_dump(); 2955 } 2956 printf("CTL Frontend information end\n"); 2957 break; 2958 } 2959 case CTL_LUN_REQ: { 2960 struct ctl_lun_req *lun_req; 2961 struct ctl_backend_driver *backend; 2962 void *packed; 2963 nvlist_t *tmp_args_nvl; 2964 size_t packed_len; 2965 2966 lun_req = (struct ctl_lun_req *)addr; 2967 tmp_args_nvl = lun_req->args_nvl; 2968 2969 backend = ctl_backend_find(lun_req->backend); 2970 if (backend == NULL) { 2971 lun_req->status = CTL_LUN_ERROR; 2972 snprintf(lun_req->error_str, 2973 sizeof(lun_req->error_str), 2974 "Backend \"%s\" not found.", 2975 lun_req->backend); 2976 break; 2977 } 2978 2979 if (lun_req->args != NULL) { 2980 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2981 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2982 free(packed, M_CTL); 2983 lun_req->status = CTL_LUN_ERROR; 2984 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2985 "Cannot copyin args."); 2986 break; 2987 } 2988 lun_req->args_nvl = nvlist_unpack(packed, 2989 lun_req->args_len, 0); 2990 free(packed, M_CTL); 2991 2992 if (lun_req->args_nvl == NULL) { 2993 lun_req->status = CTL_LUN_ERROR; 2994 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2995 "Cannot unpack args nvlist."); 2996 break; 2997 } 2998 } else 2999 lun_req->args_nvl = nvlist_create(0); 3000 3001 retval = backend->ioctl(dev, cmd, addr, flag, td); 3002 nvlist_destroy(lun_req->args_nvl); 3003 lun_req->args_nvl = tmp_args_nvl; 3004 3005 if (lun_req->result_nvl != NULL) { 3006 if (lun_req->result != NULL) { 3007 packed = nvlist_pack(lun_req->result_nvl, 3008 &packed_len); 3009 if (packed == NULL) { 3010 lun_req->status = CTL_LUN_ERROR; 3011 snprintf(lun_req->error_str, 3012 sizeof(lun_req->error_str), 3013 "Cannot pack result nvlist."); 3014 break; 3015 } 3016 3017 if (packed_len > lun_req->result_len) { 3018 lun_req->status = CTL_LUN_ERROR; 3019 snprintf(lun_req->error_str, 3020 sizeof(lun_req->error_str), 3021 "Result nvlist too large."); 3022 free(packed, M_NVLIST); 3023 break; 3024 } 3025 3026 if (copyout(packed, lun_req->result, packed_len)) { 3027 lun_req->status = CTL_LUN_ERROR; 3028 snprintf(lun_req->error_str, 3029 sizeof(lun_req->error_str), 3030 "Cannot copyout() the result."); 3031 free(packed, M_NVLIST); 3032 break; 3033 } 3034 3035 lun_req->result_len = packed_len; 3036 free(packed, M_NVLIST); 3037 } 3038 3039 nvlist_destroy(lun_req->result_nvl); 3040 } 3041 break; 3042 } 3043 case CTL_LUN_LIST: { 3044 struct sbuf *sb; 3045 struct ctl_lun_list *list; 3046 const char *name, *value; 3047 void *cookie; 3048 int type; 3049 3050 list = (struct ctl_lun_list *)addr; 3051 3052 /* 3053 * Allocate a fixed length sbuf here, based on the length 3054 * of the user's buffer. We could allocate an auto-extending 3055 * buffer, and then tell the user how much larger our 3056 * amount of data is than his buffer, but that presents 3057 * some problems: 3058 * 3059 * 1. The sbuf(9) routines use a blocking malloc, and so 3060 * we can't hold a lock while calling them with an 3061 * auto-extending buffer. 3062 * 3063 * 2. There is not currently a LUN reference counting 3064 * mechanism, outside of outstanding transactions on 3065 * the LUN's OOA queue. So a LUN could go away on us 3066 * while we're getting the LUN number, backend-specific 3067 * information, etc. Thus, given the way things 3068 * currently work, we need to hold the CTL lock while 3069 * grabbing LUN information. 3070 * 3071 * So, from the user's standpoint, the best thing to do is 3072 * allocate what he thinks is a reasonable buffer length, 3073 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3074 * double the buffer length and try again. (And repeat 3075 * that until he succeeds.) 3076 */ 3077 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3078 if (sb == NULL) { 3079 list->status = CTL_LUN_LIST_ERROR; 3080 snprintf(list->error_str, sizeof(list->error_str), 3081 "Unable to allocate %d bytes for LUN list", 3082 list->alloc_len); 3083 break; 3084 } 3085 3086 sbuf_printf(sb, "<ctllunlist>\n"); 3087 3088 mtx_lock(&softc->ctl_lock); 3089 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3090 mtx_lock(&lun->lun_lock); 3091 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3092 (uintmax_t)lun->lun); 3093 3094 /* 3095 * Bail out as soon as we see that we've overfilled 3096 * the buffer. 3097 */ 3098 if (retval != 0) 3099 break; 3100 3101 retval = sbuf_printf(sb, "\t<backend_type>%s" 3102 "</backend_type>\n", 3103 (lun->backend == NULL) ? "none" : 3104 lun->backend->name); 3105 3106 if (retval != 0) 3107 break; 3108 3109 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3110 lun->be_lun->lun_type); 3111 3112 if (retval != 0) 3113 break; 3114 3115 if (lun->backend == NULL) { 3116 retval = sbuf_printf(sb, "</lun>\n"); 3117 if (retval != 0) 3118 break; 3119 continue; 3120 } 3121 3122 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3123 (lun->be_lun->maxlba > 0) ? 3124 lun->be_lun->maxlba + 1 : 0); 3125 3126 if (retval != 0) 3127 break; 3128 3129 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3130 lun->be_lun->blocksize); 3131 3132 if (retval != 0) 3133 break; 3134 3135 retval = sbuf_printf(sb, "\t<serial_number>"); 3136 3137 if (retval != 0) 3138 break; 3139 3140 retval = ctl_sbuf_printf_esc(sb, 3141 lun->be_lun->serial_num, 3142 sizeof(lun->be_lun->serial_num)); 3143 3144 if (retval != 0) 3145 break; 3146 3147 retval = sbuf_printf(sb, "</serial_number>\n"); 3148 3149 if (retval != 0) 3150 break; 3151 3152 retval = sbuf_printf(sb, "\t<device_id>"); 3153 3154 if (retval != 0) 3155 break; 3156 3157 retval = ctl_sbuf_printf_esc(sb, 3158 lun->be_lun->device_id, 3159 sizeof(lun->be_lun->device_id)); 3160 3161 if (retval != 0) 3162 break; 3163 3164 retval = sbuf_printf(sb, "</device_id>\n"); 3165 3166 if (retval != 0) 3167 break; 3168 3169 if (lun->backend->lun_info != NULL) { 3170 retval = lun->backend->lun_info(lun->be_lun, sb); 3171 if (retval != 0) 3172 break; 3173 } 3174 3175 cookie = NULL; 3176 while ((name = nvlist_next(lun->be_lun->options, &type, 3177 &cookie)) != NULL) { 3178 sbuf_printf(sb, "\t<%s>", name); 3179 3180 if (type == NV_TYPE_STRING) { 3181 value = dnvlist_get_string( 3182 lun->be_lun->options, name, NULL); 3183 if (value != NULL) 3184 sbuf_printf(sb, "%s", value); 3185 } 3186 3187 sbuf_printf(sb, "</%s>\n", name); 3188 } 3189 3190 retval = sbuf_printf(sb, "</lun>\n"); 3191 3192 if (retval != 0) 3193 break; 3194 mtx_unlock(&lun->lun_lock); 3195 } 3196 if (lun != NULL) 3197 mtx_unlock(&lun->lun_lock); 3198 mtx_unlock(&softc->ctl_lock); 3199 3200 if ((retval != 0) 3201 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3202 retval = 0; 3203 sbuf_delete(sb); 3204 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3205 snprintf(list->error_str, sizeof(list->error_str), 3206 "Out of space, %d bytes is too small", 3207 list->alloc_len); 3208 break; 3209 } 3210 3211 sbuf_finish(sb); 3212 3213 retval = copyout(sbuf_data(sb), list->lun_xml, 3214 sbuf_len(sb) + 1); 3215 3216 list->fill_len = sbuf_len(sb) + 1; 3217 list->status = CTL_LUN_LIST_OK; 3218 sbuf_delete(sb); 3219 break; 3220 } 3221 case CTL_ISCSI: { 3222 struct ctl_iscsi *ci; 3223 struct ctl_frontend *fe; 3224 3225 ci = (struct ctl_iscsi *)addr; 3226 3227 fe = ctl_frontend_find("iscsi"); 3228 if (fe == NULL) { 3229 ci->status = CTL_ISCSI_ERROR; 3230 snprintf(ci->error_str, sizeof(ci->error_str), 3231 "Frontend \"iscsi\" not found."); 3232 break; 3233 } 3234 3235 retval = fe->ioctl(dev, cmd, addr, flag, td); 3236 break; 3237 } 3238 case CTL_PORT_REQ: { 3239 struct ctl_req *req; 3240 struct ctl_frontend *fe; 3241 void *packed; 3242 nvlist_t *tmp_args_nvl; 3243 size_t packed_len; 3244 3245 req = (struct ctl_req *)addr; 3246 tmp_args_nvl = req->args_nvl; 3247 3248 fe = ctl_frontend_find(req->driver); 3249 if (fe == NULL) { 3250 req->status = CTL_LUN_ERROR; 3251 snprintf(req->error_str, sizeof(req->error_str), 3252 "Frontend \"%s\" not found.", req->driver); 3253 break; 3254 } 3255 3256 if (req->args != NULL) { 3257 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3258 if (copyin(req->args, packed, req->args_len) != 0) { 3259 free(packed, M_CTL); 3260 req->status = CTL_LUN_ERROR; 3261 snprintf(req->error_str, sizeof(req->error_str), 3262 "Cannot copyin args."); 3263 break; 3264 } 3265 req->args_nvl = nvlist_unpack(packed, 3266 req->args_len, 0); 3267 free(packed, M_CTL); 3268 3269 if (req->args_nvl == NULL) { 3270 req->status = CTL_LUN_ERROR; 3271 snprintf(req->error_str, sizeof(req->error_str), 3272 "Cannot unpack args nvlist."); 3273 break; 3274 } 3275 } else 3276 req->args_nvl = nvlist_create(0); 3277 3278 if (fe->ioctl) 3279 retval = fe->ioctl(dev, cmd, addr, flag, td); 3280 else 3281 retval = ENODEV; 3282 3283 nvlist_destroy(req->args_nvl); 3284 req->args_nvl = tmp_args_nvl; 3285 3286 if (req->result_nvl != NULL) { 3287 if (req->result != NULL) { 3288 packed = nvlist_pack(req->result_nvl, 3289 &packed_len); 3290 if (packed == NULL) { 3291 req->status = CTL_LUN_ERROR; 3292 snprintf(req->error_str, 3293 sizeof(req->error_str), 3294 "Cannot pack result nvlist."); 3295 break; 3296 } 3297 3298 if (packed_len > req->result_len) { 3299 req->status = CTL_LUN_ERROR; 3300 snprintf(req->error_str, 3301 sizeof(req->error_str), 3302 "Result nvlist too large."); 3303 free(packed, M_NVLIST); 3304 break; 3305 } 3306 3307 if (copyout(packed, req->result, packed_len)) { 3308 req->status = CTL_LUN_ERROR; 3309 snprintf(req->error_str, 3310 sizeof(req->error_str), 3311 "Cannot copyout() the result."); 3312 free(packed, M_NVLIST); 3313 break; 3314 } 3315 3316 req->result_len = packed_len; 3317 free(packed, M_NVLIST); 3318 } 3319 3320 nvlist_destroy(req->result_nvl); 3321 } 3322 break; 3323 } 3324 case CTL_PORT_LIST: { 3325 struct sbuf *sb; 3326 struct ctl_port *port; 3327 struct ctl_lun_list *list; 3328 const char *name, *value; 3329 void *cookie; 3330 int j, type; 3331 uint32_t plun; 3332 3333 list = (struct ctl_lun_list *)addr; 3334 3335 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3336 if (sb == NULL) { 3337 list->status = CTL_LUN_LIST_ERROR; 3338 snprintf(list->error_str, sizeof(list->error_str), 3339 "Unable to allocate %d bytes for LUN list", 3340 list->alloc_len); 3341 break; 3342 } 3343 3344 sbuf_printf(sb, "<ctlportlist>\n"); 3345 3346 mtx_lock(&softc->ctl_lock); 3347 STAILQ_FOREACH(port, &softc->port_list, links) { 3348 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3349 (uintmax_t)port->targ_port); 3350 3351 /* 3352 * Bail out as soon as we see that we've overfilled 3353 * the buffer. 3354 */ 3355 if (retval != 0) 3356 break; 3357 3358 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3359 "</frontend_type>\n", port->frontend->name); 3360 if (retval != 0) 3361 break; 3362 3363 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3364 port->port_type); 3365 if (retval != 0) 3366 break; 3367 3368 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3369 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3370 if (retval != 0) 3371 break; 3372 3373 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3374 port->port_name); 3375 if (retval != 0) 3376 break; 3377 3378 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3379 port->physical_port); 3380 if (retval != 0) 3381 break; 3382 3383 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3384 port->virtual_port); 3385 if (retval != 0) 3386 break; 3387 3388 if (port->target_devid != NULL) { 3389 sbuf_printf(sb, "\t<target>"); 3390 ctl_id_sbuf(port->target_devid, sb); 3391 sbuf_printf(sb, "</target>\n"); 3392 } 3393 3394 if (port->port_devid != NULL) { 3395 sbuf_printf(sb, "\t<port>"); 3396 ctl_id_sbuf(port->port_devid, sb); 3397 sbuf_printf(sb, "</port>\n"); 3398 } 3399 3400 if (port->port_info != NULL) { 3401 retval = port->port_info(port->onoff_arg, sb); 3402 if (retval != 0) 3403 break; 3404 } 3405 3406 cookie = NULL; 3407 while ((name = nvlist_next(port->options, &type, 3408 &cookie)) != NULL) { 3409 sbuf_printf(sb, "\t<%s>", name); 3410 3411 if (type == NV_TYPE_STRING) { 3412 value = dnvlist_get_string(port->options, 3413 name, NULL); 3414 if (value != NULL) 3415 sbuf_printf(sb, "%s", value); 3416 } 3417 3418 sbuf_printf(sb, "</%s>\n", name); 3419 } 3420 3421 if (port->lun_map != NULL) { 3422 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3423 for (j = 0; j < port->lun_map_size; j++) { 3424 plun = ctl_lun_map_from_port(port, j); 3425 if (plun == UINT32_MAX) 3426 continue; 3427 sbuf_printf(sb, 3428 "\t<lun id=\"%u\">%u</lun>\n", 3429 j, plun); 3430 } 3431 } 3432 3433 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3434 if (port->wwpn_iid[j].in_use == 0 || 3435 (port->wwpn_iid[j].wwpn == 0 && 3436 port->wwpn_iid[j].name == NULL)) 3437 continue; 3438 3439 if (port->wwpn_iid[j].name != NULL) 3440 retval = sbuf_printf(sb, 3441 "\t<initiator id=\"%u\">%s</initiator>\n", 3442 j, port->wwpn_iid[j].name); 3443 else 3444 retval = sbuf_printf(sb, 3445 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3446 j, port->wwpn_iid[j].wwpn); 3447 if (retval != 0) 3448 break; 3449 } 3450 if (retval != 0) 3451 break; 3452 3453 retval = sbuf_printf(sb, "</targ_port>\n"); 3454 if (retval != 0) 3455 break; 3456 } 3457 mtx_unlock(&softc->ctl_lock); 3458 3459 if ((retval != 0) 3460 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3461 retval = 0; 3462 sbuf_delete(sb); 3463 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3464 snprintf(list->error_str, sizeof(list->error_str), 3465 "Out of space, %d bytes is too small", 3466 list->alloc_len); 3467 break; 3468 } 3469 3470 sbuf_finish(sb); 3471 3472 retval = copyout(sbuf_data(sb), list->lun_xml, 3473 sbuf_len(sb) + 1); 3474 3475 list->fill_len = sbuf_len(sb) + 1; 3476 list->status = CTL_LUN_LIST_OK; 3477 sbuf_delete(sb); 3478 break; 3479 } 3480 case CTL_LUN_MAP: { 3481 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3482 struct ctl_port *port; 3483 3484 mtx_lock(&softc->ctl_lock); 3485 if (lm->port < softc->port_min || 3486 lm->port >= softc->port_max || 3487 (port = softc->ctl_ports[lm->port]) == NULL) { 3488 mtx_unlock(&softc->ctl_lock); 3489 return (ENXIO); 3490 } 3491 if (port->status & CTL_PORT_STATUS_ONLINE) { 3492 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3493 if (ctl_lun_map_to_port(port, lun->lun) == 3494 UINT32_MAX) 3495 continue; 3496 mtx_lock(&lun->lun_lock); 3497 ctl_est_ua_port(lun, lm->port, -1, 3498 CTL_UA_LUN_CHANGE); 3499 mtx_unlock(&lun->lun_lock); 3500 } 3501 } 3502 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3503 if (lm->plun != UINT32_MAX) { 3504 if (lm->lun == UINT32_MAX) 3505 retval = ctl_lun_map_unset(port, lm->plun); 3506 else if (lm->lun < ctl_max_luns && 3507 softc->ctl_luns[lm->lun] != NULL) 3508 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3509 else 3510 return (ENXIO); 3511 } else { 3512 if (lm->lun == UINT32_MAX) 3513 retval = ctl_lun_map_deinit(port); 3514 else 3515 retval = ctl_lun_map_init(port); 3516 } 3517 if (port->status & CTL_PORT_STATUS_ONLINE) 3518 ctl_isc_announce_port(port); 3519 break; 3520 } 3521 case CTL_GET_LUN_STATS: { 3522 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3523 int i; 3524 3525 /* 3526 * XXX KDM no locking here. If the LUN list changes, 3527 * things can blow up. 3528 */ 3529 i = 0; 3530 stats->status = CTL_SS_OK; 3531 stats->fill_len = 0; 3532 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3533 if (lun->lun < stats->first_item) 3534 continue; 3535 if (stats->fill_len + sizeof(lun->stats) > 3536 stats->alloc_len) { 3537 stats->status = CTL_SS_NEED_MORE_SPACE; 3538 break; 3539 } 3540 retval = copyout(&lun->stats, &stats->stats[i++], 3541 sizeof(lun->stats)); 3542 if (retval != 0) 3543 break; 3544 stats->fill_len += sizeof(lun->stats); 3545 } 3546 stats->num_items = softc->num_luns; 3547 stats->flags = CTL_STATS_FLAG_NONE; 3548 #ifdef CTL_TIME_IO 3549 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3550 #endif 3551 getnanouptime(&stats->timestamp); 3552 break; 3553 } 3554 case CTL_GET_PORT_STATS: { 3555 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3556 int i; 3557 3558 /* 3559 * XXX KDM no locking here. If the LUN list changes, 3560 * things can blow up. 3561 */ 3562 i = 0; 3563 stats->status = CTL_SS_OK; 3564 stats->fill_len = 0; 3565 STAILQ_FOREACH(port, &softc->port_list, links) { 3566 if (port->targ_port < stats->first_item) 3567 continue; 3568 if (stats->fill_len + sizeof(port->stats) > 3569 stats->alloc_len) { 3570 stats->status = CTL_SS_NEED_MORE_SPACE; 3571 break; 3572 } 3573 retval = copyout(&port->stats, &stats->stats[i++], 3574 sizeof(port->stats)); 3575 if (retval != 0) 3576 break; 3577 stats->fill_len += sizeof(port->stats); 3578 } 3579 stats->num_items = softc->num_ports; 3580 stats->flags = CTL_STATS_FLAG_NONE; 3581 #ifdef CTL_TIME_IO 3582 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3583 #endif 3584 getnanouptime(&stats->timestamp); 3585 break; 3586 } 3587 default: { 3588 /* XXX KDM should we fix this? */ 3589 #if 0 3590 struct ctl_backend_driver *backend; 3591 unsigned int type; 3592 int found; 3593 3594 found = 0; 3595 3596 /* 3597 * We encode the backend type as the ioctl type for backend 3598 * ioctls. So parse it out here, and then search for a 3599 * backend of this type. 3600 */ 3601 type = _IOC_TYPE(cmd); 3602 3603 STAILQ_FOREACH(backend, &softc->be_list, links) { 3604 if (backend->type == type) { 3605 found = 1; 3606 break; 3607 } 3608 } 3609 if (found == 0) { 3610 printf("ctl: unknown ioctl command %#lx or backend " 3611 "%d\n", cmd, type); 3612 retval = EINVAL; 3613 break; 3614 } 3615 retval = backend->ioctl(dev, cmd, addr, flag, td); 3616 #endif 3617 retval = ENOTTY; 3618 break; 3619 } 3620 } 3621 return (retval); 3622 } 3623 3624 uint32_t 3625 ctl_get_initindex(struct ctl_nexus *nexus) 3626 { 3627 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3628 } 3629 3630 int 3631 ctl_lun_map_init(struct ctl_port *port) 3632 { 3633 struct ctl_softc *softc = port->ctl_softc; 3634 struct ctl_lun *lun; 3635 int size = ctl_lun_map_size; 3636 uint32_t i; 3637 3638 if (port->lun_map == NULL || port->lun_map_size < size) { 3639 port->lun_map_size = 0; 3640 free(port->lun_map, M_CTL); 3641 port->lun_map = malloc(size * sizeof(uint32_t), 3642 M_CTL, M_NOWAIT); 3643 } 3644 if (port->lun_map == NULL) 3645 return (ENOMEM); 3646 for (i = 0; i < size; i++) 3647 port->lun_map[i] = UINT32_MAX; 3648 port->lun_map_size = size; 3649 if (port->status & CTL_PORT_STATUS_ONLINE) { 3650 if (port->lun_disable != NULL) { 3651 STAILQ_FOREACH(lun, &softc->lun_list, links) 3652 port->lun_disable(port->targ_lun_arg, lun->lun); 3653 } 3654 ctl_isc_announce_port(port); 3655 } 3656 return (0); 3657 } 3658 3659 int 3660 ctl_lun_map_deinit(struct ctl_port *port) 3661 { 3662 struct ctl_softc *softc = port->ctl_softc; 3663 struct ctl_lun *lun; 3664 3665 if (port->lun_map == NULL) 3666 return (0); 3667 port->lun_map_size = 0; 3668 free(port->lun_map, M_CTL); 3669 port->lun_map = NULL; 3670 if (port->status & CTL_PORT_STATUS_ONLINE) { 3671 if (port->lun_enable != NULL) { 3672 STAILQ_FOREACH(lun, &softc->lun_list, links) 3673 port->lun_enable(port->targ_lun_arg, lun->lun); 3674 } 3675 ctl_isc_announce_port(port); 3676 } 3677 return (0); 3678 } 3679 3680 int 3681 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3682 { 3683 int status; 3684 uint32_t old; 3685 3686 if (port->lun_map == NULL) { 3687 status = ctl_lun_map_init(port); 3688 if (status != 0) 3689 return (status); 3690 } 3691 if (plun >= port->lun_map_size) 3692 return (EINVAL); 3693 old = port->lun_map[plun]; 3694 port->lun_map[plun] = glun; 3695 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3696 if (port->lun_enable != NULL) 3697 port->lun_enable(port->targ_lun_arg, plun); 3698 ctl_isc_announce_port(port); 3699 } 3700 return (0); 3701 } 3702 3703 int 3704 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3705 { 3706 uint32_t old; 3707 3708 if (port->lun_map == NULL || plun >= port->lun_map_size) 3709 return (0); 3710 old = port->lun_map[plun]; 3711 port->lun_map[plun] = UINT32_MAX; 3712 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3713 if (port->lun_disable != NULL) 3714 port->lun_disable(port->targ_lun_arg, plun); 3715 ctl_isc_announce_port(port); 3716 } 3717 return (0); 3718 } 3719 3720 uint32_t 3721 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3722 { 3723 3724 if (port == NULL) 3725 return (UINT32_MAX); 3726 if (port->lun_map == NULL) 3727 return (lun_id); 3728 if (lun_id > port->lun_map_size) 3729 return (UINT32_MAX); 3730 return (port->lun_map[lun_id]); 3731 } 3732 3733 uint32_t 3734 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3735 { 3736 uint32_t i; 3737 3738 if (port == NULL) 3739 return (UINT32_MAX); 3740 if (port->lun_map == NULL) 3741 return (lun_id); 3742 for (i = 0; i < port->lun_map_size; i++) { 3743 if (port->lun_map[i] == lun_id) 3744 return (i); 3745 } 3746 return (UINT32_MAX); 3747 } 3748 3749 uint32_t 3750 ctl_decode_lun(uint64_t encoded) 3751 { 3752 uint8_t lun[8]; 3753 uint32_t result = 0xffffffff; 3754 3755 be64enc(lun, encoded); 3756 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3757 case RPL_LUNDATA_ATYP_PERIPH: 3758 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3759 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3760 result = lun[1]; 3761 break; 3762 case RPL_LUNDATA_ATYP_FLAT: 3763 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3764 lun[6] == 0 && lun[7] == 0) 3765 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3766 break; 3767 case RPL_LUNDATA_ATYP_EXTLUN: 3768 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3769 case 0x02: 3770 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3771 case 0x00: 3772 result = lun[1]; 3773 break; 3774 case 0x10: 3775 result = (lun[1] << 16) + (lun[2] << 8) + 3776 lun[3]; 3777 break; 3778 case 0x20: 3779 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3780 result = (lun[2] << 24) + 3781 (lun[3] << 16) + (lun[4] << 8) + 3782 lun[5]; 3783 break; 3784 } 3785 break; 3786 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3787 result = 0xffffffff; 3788 break; 3789 } 3790 break; 3791 } 3792 return (result); 3793 } 3794 3795 uint64_t 3796 ctl_encode_lun(uint32_t decoded) 3797 { 3798 uint64_t l = decoded; 3799 3800 if (l <= 0xff) 3801 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3802 if (l <= 0x3fff) 3803 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3804 if (l <= 0xffffff) 3805 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3806 (l << 32)); 3807 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3808 } 3809 3810 int 3811 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3812 { 3813 int i; 3814 3815 for (i = first; i < last; i++) { 3816 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3817 return (i); 3818 } 3819 return (-1); 3820 } 3821 3822 int 3823 ctl_set_mask(uint32_t *mask, uint32_t bit) 3824 { 3825 uint32_t chunk, piece; 3826 3827 chunk = bit >> 5; 3828 piece = bit % (sizeof(uint32_t) * 8); 3829 3830 if ((mask[chunk] & (1 << piece)) != 0) 3831 return (-1); 3832 else 3833 mask[chunk] |= (1 << piece); 3834 3835 return (0); 3836 } 3837 3838 int 3839 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3840 { 3841 uint32_t chunk, piece; 3842 3843 chunk = bit >> 5; 3844 piece = bit % (sizeof(uint32_t) * 8); 3845 3846 if ((mask[chunk] & (1 << piece)) == 0) 3847 return (-1); 3848 else 3849 mask[chunk] &= ~(1 << piece); 3850 3851 return (0); 3852 } 3853 3854 int 3855 ctl_is_set(uint32_t *mask, uint32_t bit) 3856 { 3857 uint32_t chunk, piece; 3858 3859 chunk = bit >> 5; 3860 piece = bit % (sizeof(uint32_t) * 8); 3861 3862 if ((mask[chunk] & (1 << piece)) == 0) 3863 return (0); 3864 else 3865 return (1); 3866 } 3867 3868 static uint64_t 3869 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3870 { 3871 uint64_t *t; 3872 3873 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3874 if (t == NULL) 3875 return (0); 3876 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3877 } 3878 3879 static void 3880 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3881 { 3882 uint64_t *t; 3883 3884 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3885 if (t == NULL) 3886 return; 3887 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3888 } 3889 3890 static void 3891 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3892 { 3893 uint64_t *p; 3894 u_int i; 3895 3896 i = residx/CTL_MAX_INIT_PER_PORT; 3897 if (lun->pr_keys[i] != NULL) 3898 return; 3899 mtx_unlock(&lun->lun_lock); 3900 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3901 M_WAITOK | M_ZERO); 3902 mtx_lock(&lun->lun_lock); 3903 if (lun->pr_keys[i] == NULL) 3904 lun->pr_keys[i] = p; 3905 else 3906 free(p, M_CTL); 3907 } 3908 3909 static void 3910 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3911 { 3912 uint64_t *t; 3913 3914 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3915 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3916 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3917 } 3918 3919 /* 3920 * ctl_softc, pool_name, total_ctl_io are passed in. 3921 * npool is passed out. 3922 */ 3923 int 3924 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3925 uint32_t total_ctl_io, void **npool) 3926 { 3927 struct ctl_io_pool *pool; 3928 3929 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3930 M_NOWAIT | M_ZERO); 3931 if (pool == NULL) 3932 return (ENOMEM); 3933 3934 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3935 pool->ctl_softc = ctl_softc; 3936 #ifdef IO_POOLS 3937 pool->zone = uma_zsecond_create(pool->name, NULL, 3938 NULL, NULL, NULL, ctl_softc->io_zone); 3939 /* uma_prealloc(pool->zone, total_ctl_io); */ 3940 #else 3941 pool->zone = ctl_softc->io_zone; 3942 #endif 3943 3944 *npool = pool; 3945 return (0); 3946 } 3947 3948 void 3949 ctl_pool_free(struct ctl_io_pool *pool) 3950 { 3951 3952 if (pool == NULL) 3953 return; 3954 3955 #ifdef IO_POOLS 3956 uma_zdestroy(pool->zone); 3957 #endif 3958 free(pool, M_CTL); 3959 } 3960 3961 union ctl_io * 3962 ctl_alloc_io(void *pool_ref) 3963 { 3964 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3965 union ctl_io *io; 3966 3967 io = uma_zalloc(pool->zone, M_WAITOK); 3968 if (io != NULL) { 3969 io->io_hdr.pool = pool_ref; 3970 CTL_SOFTC(io) = pool->ctl_softc; 3971 TAILQ_INIT(&io->io_hdr.blocked_queue); 3972 } 3973 return (io); 3974 } 3975 3976 union ctl_io * 3977 ctl_alloc_io_nowait(void *pool_ref) 3978 { 3979 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3980 union ctl_io *io; 3981 3982 io = uma_zalloc(pool->zone, M_NOWAIT); 3983 if (io != NULL) { 3984 io->io_hdr.pool = pool_ref; 3985 CTL_SOFTC(io) = pool->ctl_softc; 3986 TAILQ_INIT(&io->io_hdr.blocked_queue); 3987 } 3988 return (io); 3989 } 3990 3991 void 3992 ctl_free_io(union ctl_io *io) 3993 { 3994 struct ctl_io_pool *pool; 3995 3996 if (io == NULL) 3997 return; 3998 3999 pool = (struct ctl_io_pool *)io->io_hdr.pool; 4000 uma_zfree(pool->zone, io); 4001 } 4002 4003 void 4004 ctl_zero_io(union ctl_io *io) 4005 { 4006 struct ctl_io_pool *pool; 4007 4008 if (io == NULL) 4009 return; 4010 4011 /* 4012 * May need to preserve linked list pointers at some point too. 4013 */ 4014 pool = io->io_hdr.pool; 4015 memset(io, 0, sizeof(*io)); 4016 io->io_hdr.pool = pool; 4017 CTL_SOFTC(io) = pool->ctl_softc; 4018 TAILQ_INIT(&io->io_hdr.blocked_queue); 4019 } 4020 4021 int 4022 ctl_expand_number(const char *buf, uint64_t *num) 4023 { 4024 char *endptr; 4025 uint64_t number; 4026 unsigned shift; 4027 4028 number = strtoq(buf, &endptr, 0); 4029 4030 switch (tolower((unsigned char)*endptr)) { 4031 case 'e': 4032 shift = 60; 4033 break; 4034 case 'p': 4035 shift = 50; 4036 break; 4037 case 't': 4038 shift = 40; 4039 break; 4040 case 'g': 4041 shift = 30; 4042 break; 4043 case 'm': 4044 shift = 20; 4045 break; 4046 case 'k': 4047 shift = 10; 4048 break; 4049 case 'b': 4050 case '\0': /* No unit. */ 4051 *num = number; 4052 return (0); 4053 default: 4054 /* Unrecognized unit. */ 4055 return (-1); 4056 } 4057 4058 if ((number << shift) >> shift != number) { 4059 /* Overflow */ 4060 return (-1); 4061 } 4062 *num = number << shift; 4063 return (0); 4064 } 4065 4066 /* 4067 * This routine could be used in the future to load default and/or saved 4068 * mode page parameters for a particuar lun. 4069 */ 4070 static int 4071 ctl_init_page_index(struct ctl_lun *lun) 4072 { 4073 int i, page_code; 4074 struct ctl_page_index *page_index; 4075 const char *value; 4076 uint64_t ival; 4077 4078 memcpy(&lun->mode_pages.index, page_index_template, 4079 sizeof(page_index_template)); 4080 4081 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4082 page_index = &lun->mode_pages.index[i]; 4083 if (lun->be_lun->lun_type == T_DIRECT && 4084 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4085 continue; 4086 if (lun->be_lun->lun_type == T_PROCESSOR && 4087 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4088 continue; 4089 if (lun->be_lun->lun_type == T_CDROM && 4090 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4091 continue; 4092 4093 page_code = page_index->page_code & SMPH_PC_MASK; 4094 switch (page_code) { 4095 case SMS_RW_ERROR_RECOVERY_PAGE: { 4096 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4097 ("subpage %#x for page %#x is incorrect!", 4098 page_index->subpage, page_code)); 4099 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4100 &rw_er_page_default, 4101 sizeof(rw_er_page_default)); 4102 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4103 &rw_er_page_changeable, 4104 sizeof(rw_er_page_changeable)); 4105 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4106 &rw_er_page_default, 4107 sizeof(rw_er_page_default)); 4108 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4109 &rw_er_page_default, 4110 sizeof(rw_er_page_default)); 4111 page_index->page_data = 4112 (uint8_t *)lun->mode_pages.rw_er_page; 4113 break; 4114 } 4115 case SMS_FORMAT_DEVICE_PAGE: { 4116 struct scsi_format_page *format_page; 4117 4118 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4119 ("subpage %#x for page %#x is incorrect!", 4120 page_index->subpage, page_code)); 4121 4122 /* 4123 * Sectors per track are set above. Bytes per 4124 * sector need to be set here on a per-LUN basis. 4125 */ 4126 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4127 &format_page_default, 4128 sizeof(format_page_default)); 4129 memcpy(&lun->mode_pages.format_page[ 4130 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4131 sizeof(format_page_changeable)); 4132 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4133 &format_page_default, 4134 sizeof(format_page_default)); 4135 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4136 &format_page_default, 4137 sizeof(format_page_default)); 4138 4139 format_page = &lun->mode_pages.format_page[ 4140 CTL_PAGE_CURRENT]; 4141 scsi_ulto2b(lun->be_lun->blocksize, 4142 format_page->bytes_per_sector); 4143 4144 format_page = &lun->mode_pages.format_page[ 4145 CTL_PAGE_DEFAULT]; 4146 scsi_ulto2b(lun->be_lun->blocksize, 4147 format_page->bytes_per_sector); 4148 4149 format_page = &lun->mode_pages.format_page[ 4150 CTL_PAGE_SAVED]; 4151 scsi_ulto2b(lun->be_lun->blocksize, 4152 format_page->bytes_per_sector); 4153 4154 page_index->page_data = 4155 (uint8_t *)lun->mode_pages.format_page; 4156 break; 4157 } 4158 case SMS_RIGID_DISK_PAGE: { 4159 struct scsi_rigid_disk_page *rigid_disk_page; 4160 uint32_t sectors_per_cylinder; 4161 uint64_t cylinders; 4162 #ifndef __XSCALE__ 4163 int shift; 4164 #endif /* !__XSCALE__ */ 4165 4166 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4167 ("subpage %#x for page %#x is incorrect!", 4168 page_index->subpage, page_code)); 4169 4170 /* 4171 * Rotation rate and sectors per track are set 4172 * above. We calculate the cylinders here based on 4173 * capacity. Due to the number of heads and 4174 * sectors per track we're using, smaller arrays 4175 * may turn out to have 0 cylinders. Linux and 4176 * FreeBSD don't pay attention to these mode pages 4177 * to figure out capacity, but Solaris does. It 4178 * seems to deal with 0 cylinders just fine, and 4179 * works out a fake geometry based on the capacity. 4180 */ 4181 memcpy(&lun->mode_pages.rigid_disk_page[ 4182 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4183 sizeof(rigid_disk_page_default)); 4184 memcpy(&lun->mode_pages.rigid_disk_page[ 4185 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4186 sizeof(rigid_disk_page_changeable)); 4187 4188 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4189 CTL_DEFAULT_HEADS; 4190 4191 /* 4192 * The divide method here will be more accurate, 4193 * probably, but results in floating point being 4194 * used in the kernel on i386 (__udivdi3()). On the 4195 * XScale, though, __udivdi3() is implemented in 4196 * software. 4197 * 4198 * The shift method for cylinder calculation is 4199 * accurate if sectors_per_cylinder is a power of 4200 * 2. Otherwise it might be slightly off -- you 4201 * might have a bit of a truncation problem. 4202 */ 4203 #ifdef __XSCALE__ 4204 cylinders = (lun->be_lun->maxlba + 1) / 4205 sectors_per_cylinder; 4206 #else 4207 for (shift = 31; shift > 0; shift--) { 4208 if (sectors_per_cylinder & (1 << shift)) 4209 break; 4210 } 4211 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4212 #endif 4213 4214 /* 4215 * We've basically got 3 bytes, or 24 bits for the 4216 * cylinder size in the mode page. If we're over, 4217 * just round down to 2^24. 4218 */ 4219 if (cylinders > 0xffffff) 4220 cylinders = 0xffffff; 4221 4222 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4223 CTL_PAGE_DEFAULT]; 4224 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4225 4226 if ((value = dnvlist_get_string(lun->be_lun->options, 4227 "rpm", NULL)) != NULL) { 4228 scsi_ulto2b(strtol(value, NULL, 0), 4229 rigid_disk_page->rotation_rate); 4230 } 4231 4232 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4233 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4234 sizeof(rigid_disk_page_default)); 4235 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4236 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4237 sizeof(rigid_disk_page_default)); 4238 4239 page_index->page_data = 4240 (uint8_t *)lun->mode_pages.rigid_disk_page; 4241 break; 4242 } 4243 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4244 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4245 ("subpage %#x for page %#x is incorrect!", 4246 page_index->subpage, page_code)); 4247 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4248 &verify_er_page_default, 4249 sizeof(verify_er_page_default)); 4250 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4251 &verify_er_page_changeable, 4252 sizeof(verify_er_page_changeable)); 4253 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4254 &verify_er_page_default, 4255 sizeof(verify_er_page_default)); 4256 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4257 &verify_er_page_default, 4258 sizeof(verify_er_page_default)); 4259 page_index->page_data = 4260 (uint8_t *)lun->mode_pages.verify_er_page; 4261 break; 4262 } 4263 case SMS_CACHING_PAGE: { 4264 struct scsi_caching_page *caching_page; 4265 4266 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4267 ("subpage %#x for page %#x is incorrect!", 4268 page_index->subpage, page_code)); 4269 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4270 &caching_page_default, 4271 sizeof(caching_page_default)); 4272 memcpy(&lun->mode_pages.caching_page[ 4273 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4274 sizeof(caching_page_changeable)); 4275 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4276 &caching_page_default, 4277 sizeof(caching_page_default)); 4278 caching_page = &lun->mode_pages.caching_page[ 4279 CTL_PAGE_SAVED]; 4280 value = dnvlist_get_string(lun->be_lun->options, 4281 "writecache", NULL); 4282 if (value != NULL && strcmp(value, "off") == 0) 4283 caching_page->flags1 &= ~SCP_WCE; 4284 value = dnvlist_get_string(lun->be_lun->options, 4285 "readcache", NULL); 4286 if (value != NULL && strcmp(value, "off") == 0) 4287 caching_page->flags1 |= SCP_RCD; 4288 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4289 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4290 sizeof(caching_page_default)); 4291 page_index->page_data = 4292 (uint8_t *)lun->mode_pages.caching_page; 4293 break; 4294 } 4295 case SMS_CONTROL_MODE_PAGE: { 4296 switch (page_index->subpage) { 4297 case SMS_SUBPAGE_PAGE_0: { 4298 struct scsi_control_page *control_page; 4299 4300 memcpy(&lun->mode_pages.control_page[ 4301 CTL_PAGE_DEFAULT], 4302 &control_page_default, 4303 sizeof(control_page_default)); 4304 memcpy(&lun->mode_pages.control_page[ 4305 CTL_PAGE_CHANGEABLE], 4306 &control_page_changeable, 4307 sizeof(control_page_changeable)); 4308 memcpy(&lun->mode_pages.control_page[ 4309 CTL_PAGE_SAVED], 4310 &control_page_default, 4311 sizeof(control_page_default)); 4312 control_page = &lun->mode_pages.control_page[ 4313 CTL_PAGE_SAVED]; 4314 value = dnvlist_get_string(lun->be_lun->options, 4315 "reordering", NULL); 4316 if (value != NULL && 4317 strcmp(value, "unrestricted") == 0) { 4318 control_page->queue_flags &= 4319 ~SCP_QUEUE_ALG_MASK; 4320 control_page->queue_flags |= 4321 SCP_QUEUE_ALG_UNRESTRICTED; 4322 } 4323 memcpy(&lun->mode_pages.control_page[ 4324 CTL_PAGE_CURRENT], 4325 &lun->mode_pages.control_page[ 4326 CTL_PAGE_SAVED], 4327 sizeof(control_page_default)); 4328 page_index->page_data = 4329 (uint8_t *)lun->mode_pages.control_page; 4330 break; 4331 } 4332 case 0x01: 4333 memcpy(&lun->mode_pages.control_ext_page[ 4334 CTL_PAGE_DEFAULT], 4335 &control_ext_page_default, 4336 sizeof(control_ext_page_default)); 4337 memcpy(&lun->mode_pages.control_ext_page[ 4338 CTL_PAGE_CHANGEABLE], 4339 &control_ext_page_changeable, 4340 sizeof(control_ext_page_changeable)); 4341 memcpy(&lun->mode_pages.control_ext_page[ 4342 CTL_PAGE_SAVED], 4343 &control_ext_page_default, 4344 sizeof(control_ext_page_default)); 4345 memcpy(&lun->mode_pages.control_ext_page[ 4346 CTL_PAGE_CURRENT], 4347 &lun->mode_pages.control_ext_page[ 4348 CTL_PAGE_SAVED], 4349 sizeof(control_ext_page_default)); 4350 page_index->page_data = 4351 (uint8_t *)lun->mode_pages.control_ext_page; 4352 break; 4353 default: 4354 panic("subpage %#x for page %#x is incorrect!", 4355 page_index->subpage, page_code); 4356 } 4357 break; 4358 } 4359 case SMS_INFO_EXCEPTIONS_PAGE: { 4360 switch (page_index->subpage) { 4361 case SMS_SUBPAGE_PAGE_0: 4362 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4363 &ie_page_default, 4364 sizeof(ie_page_default)); 4365 memcpy(&lun->mode_pages.ie_page[ 4366 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4367 sizeof(ie_page_changeable)); 4368 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4369 &ie_page_default, 4370 sizeof(ie_page_default)); 4371 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4372 &ie_page_default, 4373 sizeof(ie_page_default)); 4374 page_index->page_data = 4375 (uint8_t *)lun->mode_pages.ie_page; 4376 break; 4377 case 0x02: { 4378 struct ctl_logical_block_provisioning_page *page; 4379 4380 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4381 &lbp_page_default, 4382 sizeof(lbp_page_default)); 4383 memcpy(&lun->mode_pages.lbp_page[ 4384 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4385 sizeof(lbp_page_changeable)); 4386 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4387 &lbp_page_default, 4388 sizeof(lbp_page_default)); 4389 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4390 value = dnvlist_get_string(lun->be_lun->options, 4391 "avail-threshold", NULL); 4392 if (value != NULL && 4393 ctl_expand_number(value, &ival) == 0) { 4394 page->descr[0].flags |= SLBPPD_ENABLED | 4395 SLBPPD_ARMING_DEC; 4396 if (lun->be_lun->blocksize) 4397 ival /= lun->be_lun->blocksize; 4398 else 4399 ival /= 512; 4400 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4401 page->descr[0].count); 4402 } 4403 value = dnvlist_get_string(lun->be_lun->options, 4404 "used-threshold", NULL); 4405 if (value != NULL && 4406 ctl_expand_number(value, &ival) == 0) { 4407 page->descr[1].flags |= SLBPPD_ENABLED | 4408 SLBPPD_ARMING_INC; 4409 if (lun->be_lun->blocksize) 4410 ival /= lun->be_lun->blocksize; 4411 else 4412 ival /= 512; 4413 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4414 page->descr[1].count); 4415 } 4416 value = dnvlist_get_string(lun->be_lun->options, 4417 "pool-avail-threshold", NULL); 4418 if (value != NULL && 4419 ctl_expand_number(value, &ival) == 0) { 4420 page->descr[2].flags |= SLBPPD_ENABLED | 4421 SLBPPD_ARMING_DEC; 4422 if (lun->be_lun->blocksize) 4423 ival /= lun->be_lun->blocksize; 4424 else 4425 ival /= 512; 4426 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4427 page->descr[2].count); 4428 } 4429 value = dnvlist_get_string(lun->be_lun->options, 4430 "pool-used-threshold", NULL); 4431 if (value != NULL && 4432 ctl_expand_number(value, &ival) == 0) { 4433 page->descr[3].flags |= SLBPPD_ENABLED | 4434 SLBPPD_ARMING_INC; 4435 if (lun->be_lun->blocksize) 4436 ival /= lun->be_lun->blocksize; 4437 else 4438 ival /= 512; 4439 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4440 page->descr[3].count); 4441 } 4442 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4443 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4444 sizeof(lbp_page_default)); 4445 page_index->page_data = 4446 (uint8_t *)lun->mode_pages.lbp_page; 4447 break; 4448 } 4449 default: 4450 panic("subpage %#x for page %#x is incorrect!", 4451 page_index->subpage, page_code); 4452 } 4453 break; 4454 } 4455 case SMS_CDDVD_CAPS_PAGE:{ 4456 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4457 ("subpage %#x for page %#x is incorrect!", 4458 page_index->subpage, page_code)); 4459 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4460 &cddvd_page_default, 4461 sizeof(cddvd_page_default)); 4462 memcpy(&lun->mode_pages.cddvd_page[ 4463 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4464 sizeof(cddvd_page_changeable)); 4465 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4466 &cddvd_page_default, 4467 sizeof(cddvd_page_default)); 4468 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4469 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4470 sizeof(cddvd_page_default)); 4471 page_index->page_data = 4472 (uint8_t *)lun->mode_pages.cddvd_page; 4473 break; 4474 } 4475 default: 4476 panic("invalid page code value %#x", page_code); 4477 } 4478 } 4479 4480 return (CTL_RETVAL_COMPLETE); 4481 } 4482 4483 static int 4484 ctl_init_log_page_index(struct ctl_lun *lun) 4485 { 4486 struct ctl_page_index *page_index; 4487 int i, j, k, prev; 4488 4489 memcpy(&lun->log_pages.index, log_page_index_template, 4490 sizeof(log_page_index_template)); 4491 4492 prev = -1; 4493 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4494 page_index = &lun->log_pages.index[i]; 4495 if (lun->be_lun->lun_type == T_DIRECT && 4496 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4497 continue; 4498 if (lun->be_lun->lun_type == T_PROCESSOR && 4499 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4500 continue; 4501 if (lun->be_lun->lun_type == T_CDROM && 4502 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4503 continue; 4504 4505 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4506 lun->backend->lun_attr == NULL) 4507 continue; 4508 4509 if (page_index->page_code != prev) { 4510 lun->log_pages.pages_page[j] = page_index->page_code; 4511 prev = page_index->page_code; 4512 j++; 4513 } 4514 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4515 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4516 k++; 4517 } 4518 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4519 lun->log_pages.index[0].page_len = j; 4520 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4521 lun->log_pages.index[1].page_len = k * 2; 4522 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page; 4523 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page); 4524 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0]; 4525 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS; 4526 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page; 4527 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page); 4528 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page; 4529 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page); 4530 4531 return (CTL_RETVAL_COMPLETE); 4532 } 4533 4534 static int 4535 hex2bin(const char *str, uint8_t *buf, int buf_size) 4536 { 4537 int i; 4538 u_char c; 4539 4540 memset(buf, 0, buf_size); 4541 while (isspace(str[0])) 4542 str++; 4543 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4544 str += 2; 4545 buf_size *= 2; 4546 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4547 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4548 str++; 4549 c = str[i]; 4550 if (isdigit(c)) 4551 c -= '0'; 4552 else if (isalpha(c)) 4553 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4554 else 4555 break; 4556 if (c >= 16) 4557 break; 4558 if ((i & 1) == 0) 4559 buf[i / 2] |= (c << 4); 4560 else 4561 buf[i / 2] |= c; 4562 } 4563 return ((i + 1) / 2); 4564 } 4565 4566 /* 4567 * Add LUN. 4568 * 4569 * Returns 0 for success, non-zero (errno) for failure. 4570 */ 4571 int 4572 ctl_add_lun(struct ctl_be_lun *be_lun) 4573 { 4574 struct ctl_softc *ctl_softc = control_softc; 4575 struct ctl_lun *nlun, *lun; 4576 struct scsi_vpd_id_descriptor *desc; 4577 struct scsi_vpd_id_t10 *t10id; 4578 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4579 int lun_number; 4580 int devidlen, idlen1, idlen2 = 0, len; 4581 4582 /* 4583 * We support only Direct Access, CD-ROM or Processor LUN types. 4584 */ 4585 switch (be_lun->lun_type) { 4586 case T_DIRECT: 4587 case T_PROCESSOR: 4588 case T_CDROM: 4589 break; 4590 case T_SEQUENTIAL: 4591 case T_CHANGER: 4592 default: 4593 return (EINVAL); 4594 } 4595 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO); 4596 4597 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4598 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4599 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4600 M_DEVBUF, M_WAITOK | M_ZERO); 4601 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4602 M_DEVBUF, M_WAITOK | M_ZERO); 4603 4604 /* Generate LUN ID. */ 4605 devidlen = max(CTL_DEVID_MIN_LEN, 4606 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4607 idlen1 = sizeof(*t10id) + devidlen; 4608 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4609 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4610 if (scsiname != NULL) { 4611 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4612 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4613 } 4614 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4615 if (eui != NULL) { 4616 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4617 } 4618 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4619 if (naa != NULL) { 4620 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4621 } 4622 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4623 if (uuid != NULL) { 4624 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4625 } 4626 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4627 M_CTL, M_WAITOK | M_ZERO); 4628 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4629 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4630 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4631 desc->length = idlen1; 4632 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4633 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4634 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4635 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4636 } else { 4637 strncpy(t10id->vendor, vendor, 4638 min(sizeof(t10id->vendor), strlen(vendor))); 4639 } 4640 strncpy((char *)t10id->vendor_spec_id, 4641 (char *)be_lun->device_id, devidlen); 4642 if (scsiname != NULL) { 4643 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4644 desc->length); 4645 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4646 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4647 SVPD_ID_TYPE_SCSI_NAME; 4648 desc->length = idlen2; 4649 strlcpy(desc->identifier, scsiname, idlen2); 4650 } 4651 if (eui != NULL) { 4652 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4653 desc->length); 4654 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4655 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4656 SVPD_ID_TYPE_EUI64; 4657 desc->length = hex2bin(eui, desc->identifier, 16); 4658 desc->length = desc->length > 12 ? 16 : 4659 (desc->length > 8 ? 12 : 8); 4660 len -= 16 - desc->length; 4661 } 4662 if (naa != NULL) { 4663 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4664 desc->length); 4665 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4666 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4667 SVPD_ID_TYPE_NAA; 4668 desc->length = hex2bin(naa, desc->identifier, 16); 4669 desc->length = desc->length > 8 ? 16 : 8; 4670 len -= 16 - desc->length; 4671 } 4672 if (uuid != NULL) { 4673 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4674 desc->length); 4675 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4676 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4677 SVPD_ID_TYPE_UUID; 4678 desc->identifier[0] = 0x10; 4679 hex2bin(uuid, &desc->identifier[2], 16); 4680 desc->length = 18; 4681 } 4682 lun->lun_devid->len = len; 4683 4684 mtx_lock(&ctl_softc->ctl_lock); 4685 /* 4686 * See if the caller requested a particular LUN number. If so, see 4687 * if it is available. Otherwise, allocate the first available LUN. 4688 */ 4689 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4690 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4691 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4692 mtx_unlock(&ctl_softc->ctl_lock); 4693 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4694 printf("ctl: requested LUN ID %d is higher " 4695 "than ctl_max_luns - 1 (%d)\n", 4696 be_lun->req_lun_id, ctl_max_luns - 1); 4697 } else { 4698 /* 4699 * XXX KDM return an error, or just assign 4700 * another LUN ID in this case?? 4701 */ 4702 printf("ctl: requested LUN ID %d is already " 4703 "in use\n", be_lun->req_lun_id); 4704 } 4705 fail: 4706 free(lun->lun_devid, M_CTL); 4707 free(lun, M_CTL); 4708 return (ENOSPC); 4709 } 4710 lun_number = be_lun->req_lun_id; 4711 } else { 4712 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4713 if (lun_number == -1) { 4714 mtx_unlock(&ctl_softc->ctl_lock); 4715 printf("ctl: can't allocate LUN, out of LUNs\n"); 4716 goto fail; 4717 } 4718 } 4719 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4720 mtx_unlock(&ctl_softc->ctl_lock); 4721 4722 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4723 lun->lun = lun_number; 4724 lun->be_lun = be_lun; 4725 /* 4726 * The processor LUN is always enabled. Disk LUNs come on line 4727 * disabled, and must be enabled by the backend. 4728 */ 4729 lun->flags |= CTL_LUN_DISABLED; 4730 lun->backend = be_lun->be; 4731 be_lun->ctl_lun = lun; 4732 be_lun->lun_id = lun_number; 4733 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4734 lun->flags |= CTL_LUN_EJECTED; 4735 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4736 lun->flags |= CTL_LUN_NO_MEDIA; 4737 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4738 lun->flags |= CTL_LUN_STOPPED; 4739 4740 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4741 lun->flags |= CTL_LUN_PRIMARY_SC; 4742 4743 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4744 if (value != NULL) { 4745 if (strcmp(value, "on") == 0) 4746 lun->flags |= CTL_LUN_REMOVABLE; 4747 } else if (be_lun->lun_type == T_CDROM) 4748 lun->flags |= CTL_LUN_REMOVABLE; 4749 4750 lun->ctl_softc = ctl_softc; 4751 #ifdef CTL_TIME_IO 4752 lun->last_busy = getsbinuptime(); 4753 #endif 4754 LIST_INIT(&lun->ooa_queue); 4755 STAILQ_INIT(&lun->error_list); 4756 lun->ie_reported = 1; 4757 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4758 ctl_tpc_lun_init(lun); 4759 if (lun->flags & CTL_LUN_REMOVABLE) { 4760 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4761 M_CTL, M_WAITOK); 4762 } 4763 4764 /* 4765 * Initialize the mode and log page index. 4766 */ 4767 ctl_init_page_index(lun); 4768 ctl_init_log_page_index(lun); 4769 4770 /* Setup statistics gathering */ 4771 lun->stats.item = lun_number; 4772 4773 /* 4774 * Now, before we insert this lun on the lun list, set the lun 4775 * inventory changed UA for all other luns. 4776 */ 4777 mtx_lock(&ctl_softc->ctl_lock); 4778 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4779 mtx_lock(&nlun->lun_lock); 4780 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4781 mtx_unlock(&nlun->lun_lock); 4782 } 4783 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4784 ctl_softc->ctl_luns[lun_number] = lun; 4785 ctl_softc->num_luns++; 4786 mtx_unlock(&ctl_softc->ctl_lock); 4787 4788 /* 4789 * We successfully added the LUN, attempt to enable it. 4790 */ 4791 if (ctl_enable_lun(lun) != 0) { 4792 printf("%s: ctl_enable_lun() failed!\n", __func__); 4793 mtx_lock(&ctl_softc->ctl_lock); 4794 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links); 4795 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number); 4796 ctl_softc->ctl_luns[lun_number] = NULL; 4797 ctl_softc->num_luns--; 4798 mtx_unlock(&ctl_softc->ctl_lock); 4799 free(lun->lun_devid, M_CTL); 4800 free(lun, M_CTL); 4801 return (EIO); 4802 } 4803 4804 return (0); 4805 } 4806 4807 /* 4808 * Free LUN that has no active requests. 4809 */ 4810 static int 4811 ctl_free_lun(struct ctl_lun *lun) 4812 { 4813 struct ctl_softc *softc = lun->ctl_softc; 4814 struct ctl_lun *nlun; 4815 int i; 4816 4817 KASSERT(LIST_EMPTY(&lun->ooa_queue), 4818 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4819 4820 mtx_lock(&softc->ctl_lock); 4821 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4822 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4823 softc->ctl_luns[lun->lun] = NULL; 4824 softc->num_luns--; 4825 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4826 mtx_lock(&nlun->lun_lock); 4827 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4828 mtx_unlock(&nlun->lun_lock); 4829 } 4830 mtx_unlock(&softc->ctl_lock); 4831 4832 /* 4833 * Tell the backend to free resources, if this LUN has a backend. 4834 */ 4835 lun->be_lun->lun_shutdown(lun->be_lun); 4836 4837 lun->ie_reportcnt = UINT32_MAX; 4838 callout_drain(&lun->ie_callout); 4839 ctl_tpc_lun_shutdown(lun); 4840 mtx_destroy(&lun->lun_lock); 4841 free(lun->lun_devid, M_CTL); 4842 for (i = 0; i < ctl_max_ports; i++) 4843 free(lun->pending_ua[i], M_CTL); 4844 free(lun->pending_ua, M_DEVBUF); 4845 for (i = 0; i < ctl_max_ports; i++) 4846 free(lun->pr_keys[i], M_CTL); 4847 free(lun->pr_keys, M_DEVBUF); 4848 free(lun->write_buffer, M_CTL); 4849 free(lun->prevent, M_CTL); 4850 free(lun, M_CTL); 4851 4852 return (0); 4853 } 4854 4855 static int 4856 ctl_enable_lun(struct ctl_lun *lun) 4857 { 4858 struct ctl_softc *softc; 4859 struct ctl_port *port, *nport; 4860 int retval; 4861 4862 softc = lun->ctl_softc; 4863 4864 mtx_lock(&softc->ctl_lock); 4865 mtx_lock(&lun->lun_lock); 4866 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0, 4867 ("%s: LUN not disabled", __func__)); 4868 lun->flags &= ~CTL_LUN_DISABLED; 4869 mtx_unlock(&lun->lun_lock); 4870 4871 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4872 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4873 port->lun_map != NULL || port->lun_enable == NULL) 4874 continue; 4875 4876 /* 4877 * Drop the lock while we call the FETD's enable routine. 4878 * This can lead to a callback into CTL (at least in the 4879 * case of the internal initiator frontend. 4880 */ 4881 mtx_unlock(&softc->ctl_lock); 4882 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4883 mtx_lock(&softc->ctl_lock); 4884 if (retval != 0) { 4885 printf("%s: FETD %s port %d returned error " 4886 "%d for lun_enable on lun %jd\n", 4887 __func__, port->port_name, port->targ_port, 4888 retval, (intmax_t)lun->lun); 4889 } 4890 } 4891 4892 mtx_unlock(&softc->ctl_lock); 4893 ctl_isc_announce_lun(lun); 4894 4895 return (0); 4896 } 4897 4898 static int 4899 ctl_disable_lun(struct ctl_lun *lun) 4900 { 4901 struct ctl_softc *softc; 4902 struct ctl_port *port; 4903 int retval; 4904 4905 softc = lun->ctl_softc; 4906 4907 mtx_lock(&softc->ctl_lock); 4908 mtx_lock(&lun->lun_lock); 4909 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0, 4910 ("%s: LUN not enabled", __func__)); 4911 lun->flags |= CTL_LUN_DISABLED; 4912 mtx_unlock(&lun->lun_lock); 4913 4914 STAILQ_FOREACH(port, &softc->port_list, links) { 4915 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4916 port->lun_map != NULL || port->lun_disable == NULL) 4917 continue; 4918 4919 /* 4920 * Drop the lock before we call the frontend's disable 4921 * routine, to avoid lock order reversals. 4922 * 4923 * XXX KDM what happens if the frontend list changes while 4924 * we're traversing it? It's unlikely, but should be handled. 4925 */ 4926 mtx_unlock(&softc->ctl_lock); 4927 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4928 mtx_lock(&softc->ctl_lock); 4929 if (retval != 0) { 4930 printf("%s: FETD %s port %d returned error " 4931 "%d for lun_disable on lun %jd\n", 4932 __func__, port->port_name, port->targ_port, 4933 retval, (intmax_t)lun->lun); 4934 } 4935 } 4936 4937 mtx_unlock(&softc->ctl_lock); 4938 ctl_isc_announce_lun(lun); 4939 4940 return (0); 4941 } 4942 4943 int 4944 ctl_start_lun(struct ctl_be_lun *be_lun) 4945 { 4946 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4947 4948 mtx_lock(&lun->lun_lock); 4949 lun->flags &= ~CTL_LUN_STOPPED; 4950 mtx_unlock(&lun->lun_lock); 4951 return (0); 4952 } 4953 4954 int 4955 ctl_stop_lun(struct ctl_be_lun *be_lun) 4956 { 4957 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4958 4959 mtx_lock(&lun->lun_lock); 4960 lun->flags |= CTL_LUN_STOPPED; 4961 mtx_unlock(&lun->lun_lock); 4962 return (0); 4963 } 4964 4965 int 4966 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4967 { 4968 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4969 4970 mtx_lock(&lun->lun_lock); 4971 lun->flags |= CTL_LUN_NO_MEDIA; 4972 mtx_unlock(&lun->lun_lock); 4973 return (0); 4974 } 4975 4976 int 4977 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4978 { 4979 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4980 union ctl_ha_msg msg; 4981 4982 mtx_lock(&lun->lun_lock); 4983 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4984 if (lun->flags & CTL_LUN_REMOVABLE) 4985 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4986 mtx_unlock(&lun->lun_lock); 4987 if ((lun->flags & CTL_LUN_REMOVABLE) && 4988 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4989 bzero(&msg.ua, sizeof(msg.ua)); 4990 msg.hdr.msg_type = CTL_MSG_UA; 4991 msg.hdr.nexus.initid = -1; 4992 msg.hdr.nexus.targ_port = -1; 4993 msg.hdr.nexus.targ_lun = lun->lun; 4994 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4995 msg.ua.ua_all = 1; 4996 msg.ua.ua_set = 1; 4997 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4998 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4999 M_WAITOK); 5000 } 5001 return (0); 5002 } 5003 5004 int 5005 ctl_lun_ejected(struct ctl_be_lun *be_lun) 5006 { 5007 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5008 5009 mtx_lock(&lun->lun_lock); 5010 lun->flags |= CTL_LUN_EJECTED; 5011 mtx_unlock(&lun->lun_lock); 5012 return (0); 5013 } 5014 5015 int 5016 ctl_lun_primary(struct ctl_be_lun *be_lun) 5017 { 5018 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5019 5020 mtx_lock(&lun->lun_lock); 5021 lun->flags |= CTL_LUN_PRIMARY_SC; 5022 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5023 mtx_unlock(&lun->lun_lock); 5024 ctl_isc_announce_lun(lun); 5025 return (0); 5026 } 5027 5028 int 5029 ctl_lun_secondary(struct ctl_be_lun *be_lun) 5030 { 5031 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5032 5033 mtx_lock(&lun->lun_lock); 5034 lun->flags &= ~CTL_LUN_PRIMARY_SC; 5035 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5036 mtx_unlock(&lun->lun_lock); 5037 ctl_isc_announce_lun(lun); 5038 return (0); 5039 } 5040 5041 /* 5042 * Remove LUN. If there are active requests, wait for completion. 5043 * 5044 * Returns 0 for success, non-zero (errno) for failure. 5045 * Completion is reported to backed via the lun_shutdown() method. 5046 */ 5047 int 5048 ctl_remove_lun(struct ctl_be_lun *be_lun) 5049 { 5050 struct ctl_lun *lun; 5051 5052 lun = (struct ctl_lun *)be_lun->ctl_lun; 5053 5054 ctl_disable_lun(lun); 5055 5056 mtx_lock(&lun->lun_lock); 5057 lun->flags |= CTL_LUN_INVALID; 5058 5059 /* 5060 * If there is nothing in the OOA queue, go ahead and free the LUN. 5061 * If we have something in the OOA queue, we'll free it when the 5062 * last I/O completes. 5063 */ 5064 if (LIST_EMPTY(&lun->ooa_queue)) { 5065 mtx_unlock(&lun->lun_lock); 5066 ctl_free_lun(lun); 5067 } else 5068 mtx_unlock(&lun->lun_lock); 5069 5070 return (0); 5071 } 5072 5073 void 5074 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5075 { 5076 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5077 union ctl_ha_msg msg; 5078 5079 mtx_lock(&lun->lun_lock); 5080 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5081 mtx_unlock(&lun->lun_lock); 5082 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5083 /* Send msg to other side. */ 5084 bzero(&msg.ua, sizeof(msg.ua)); 5085 msg.hdr.msg_type = CTL_MSG_UA; 5086 msg.hdr.nexus.initid = -1; 5087 msg.hdr.nexus.targ_port = -1; 5088 msg.hdr.nexus.targ_lun = lun->lun; 5089 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5090 msg.ua.ua_all = 1; 5091 msg.ua.ua_set = 1; 5092 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5094 M_WAITOK); 5095 } 5096 } 5097 5098 /* 5099 * Backend "memory move is complete" callback for requests that never 5100 * make it down to say RAIDCore's configuration code. 5101 */ 5102 int 5103 ctl_config_move_done(union ctl_io *io, bool samethr) 5104 { 5105 int retval; 5106 5107 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5108 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5109 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 5110 5111 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5112 ctl_data_print(io); 5113 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5114 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5115 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5116 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5117 /* 5118 * XXX KDM just assuming a single pointer here, and not a 5119 * S/G list. If we start using S/G lists for config data, 5120 * we'll need to know how to clean them up here as well. 5121 */ 5122 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5123 free(io->scsiio.kern_data_ptr, M_CTL); 5124 ctl_done(io); 5125 retval = CTL_RETVAL_COMPLETE; 5126 } else { 5127 /* 5128 * XXX KDM now we need to continue data movement. Some 5129 * options: 5130 * - call ctl_scsiio() again? We don't do this for data 5131 * writes, because for those at least we know ahead of 5132 * time where the write will go and how long it is. For 5133 * config writes, though, that information is largely 5134 * contained within the write itself, thus we need to 5135 * parse out the data again. 5136 * 5137 * - Call some other function once the data is in? 5138 */ 5139 5140 /* 5141 * XXX KDM call ctl_scsiio() again for now, and check flag 5142 * bits to see whether we're allocated or not. 5143 */ 5144 retval = ctl_scsiio(&io->scsiio); 5145 } 5146 return (retval); 5147 } 5148 5149 /* 5150 * This gets called by a backend driver when it is done with a 5151 * data_submit method. 5152 */ 5153 void 5154 ctl_data_submit_done(union ctl_io *io) 5155 { 5156 /* 5157 * If the IO_CONT flag is set, we need to call the supplied 5158 * function to continue processing the I/O, instead of completing 5159 * the I/O just yet. 5160 * 5161 * If there is an error, though, we don't want to keep processing. 5162 * Instead, just send status back to the initiator. 5163 */ 5164 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5165 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5166 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5167 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5168 io->scsiio.io_cont(io); 5169 return; 5170 } 5171 ctl_done(io); 5172 } 5173 5174 /* 5175 * This gets called by a backend driver when it is done with a 5176 * configuration write. 5177 */ 5178 void 5179 ctl_config_write_done(union ctl_io *io) 5180 { 5181 uint8_t *buf; 5182 5183 /* 5184 * If the IO_CONT flag is set, we need to call the supplied 5185 * function to continue processing the I/O, instead of completing 5186 * the I/O just yet. 5187 * 5188 * If there is an error, though, we don't want to keep processing. 5189 * Instead, just send status back to the initiator. 5190 */ 5191 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5192 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5193 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5194 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5195 io->scsiio.io_cont(io); 5196 return; 5197 } 5198 /* 5199 * Since a configuration write can be done for commands that actually 5200 * have data allocated, like write buffer, and commands that have 5201 * no data, like start/stop unit, we need to check here. 5202 */ 5203 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5204 buf = io->scsiio.kern_data_ptr; 5205 else 5206 buf = NULL; 5207 ctl_done(io); 5208 if (buf) 5209 free(buf, M_CTL); 5210 } 5211 5212 void 5213 ctl_config_read_done(union ctl_io *io) 5214 { 5215 uint8_t *buf; 5216 5217 /* 5218 * If there is some error -- we are done, skip data transfer. 5219 */ 5220 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5221 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5222 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5223 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5224 buf = io->scsiio.kern_data_ptr; 5225 else 5226 buf = NULL; 5227 ctl_done(io); 5228 if (buf) 5229 free(buf, M_CTL); 5230 return; 5231 } 5232 5233 /* 5234 * If the IO_CONT flag is set, we need to call the supplied 5235 * function to continue processing the I/O, instead of completing 5236 * the I/O just yet. 5237 */ 5238 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5239 io->scsiio.io_cont(io); 5240 return; 5241 } 5242 5243 ctl_datamove(io); 5244 } 5245 5246 /* 5247 * SCSI release command. 5248 */ 5249 int 5250 ctl_scsi_release(struct ctl_scsiio *ctsio) 5251 { 5252 struct ctl_lun *lun = CTL_LUN(ctsio); 5253 uint32_t residx; 5254 5255 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5256 5257 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5258 5259 /* 5260 * XXX KDM right now, we only support LUN reservation. We don't 5261 * support 3rd party reservations, or extent reservations, which 5262 * might actually need the parameter list. If we've gotten this 5263 * far, we've got a LUN reservation. Anything else got kicked out 5264 * above. So, according to SPC, ignore the length. 5265 */ 5266 5267 mtx_lock(&lun->lun_lock); 5268 5269 /* 5270 * According to SPC, it is not an error for an intiator to attempt 5271 * to release a reservation on a LUN that isn't reserved, or that 5272 * is reserved by another initiator. The reservation can only be 5273 * released, though, by the initiator who made it or by one of 5274 * several reset type events. 5275 */ 5276 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5277 lun->flags &= ~CTL_LUN_RESERVED; 5278 5279 mtx_unlock(&lun->lun_lock); 5280 5281 ctl_set_success(ctsio); 5282 ctl_done((union ctl_io *)ctsio); 5283 return (CTL_RETVAL_COMPLETE); 5284 } 5285 5286 int 5287 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5288 { 5289 struct ctl_lun *lun = CTL_LUN(ctsio); 5290 uint32_t residx; 5291 5292 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5293 5294 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5295 5296 /* 5297 * XXX KDM right now, we only support LUN reservation. We don't 5298 * support 3rd party reservations, or extent reservations, which 5299 * might actually need the parameter list. If we've gotten this 5300 * far, we've got a LUN reservation. Anything else got kicked out 5301 * above. So, according to SPC, ignore the length. 5302 */ 5303 5304 mtx_lock(&lun->lun_lock); 5305 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5306 ctl_set_reservation_conflict(ctsio); 5307 goto bailout; 5308 } 5309 5310 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5311 if (lun->flags & CTL_LUN_PR_RESERVED) { 5312 ctl_set_success(ctsio); 5313 goto bailout; 5314 } 5315 5316 lun->flags |= CTL_LUN_RESERVED; 5317 lun->res_idx = residx; 5318 ctl_set_success(ctsio); 5319 5320 bailout: 5321 mtx_unlock(&lun->lun_lock); 5322 ctl_done((union ctl_io *)ctsio); 5323 return (CTL_RETVAL_COMPLETE); 5324 } 5325 5326 int 5327 ctl_start_stop(struct ctl_scsiio *ctsio) 5328 { 5329 struct ctl_lun *lun = CTL_LUN(ctsio); 5330 struct scsi_start_stop_unit *cdb; 5331 int retval; 5332 5333 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5334 5335 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5336 5337 if ((cdb->how & SSS_PC_MASK) == 0) { 5338 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5339 (cdb->how & SSS_START) == 0) { 5340 uint32_t residx; 5341 5342 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5343 if (ctl_get_prkey(lun, residx) == 0 || 5344 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5345 ctl_set_reservation_conflict(ctsio); 5346 ctl_done((union ctl_io *)ctsio); 5347 return (CTL_RETVAL_COMPLETE); 5348 } 5349 } 5350 5351 if ((cdb->how & SSS_LOEJ) && 5352 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5353 ctl_set_invalid_field(ctsio, 5354 /*sks_valid*/ 1, 5355 /*command*/ 1, 5356 /*field*/ 4, 5357 /*bit_valid*/ 1, 5358 /*bit*/ 1); 5359 ctl_done((union ctl_io *)ctsio); 5360 return (CTL_RETVAL_COMPLETE); 5361 } 5362 5363 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5364 lun->prevent_count > 0) { 5365 /* "Medium removal prevented" */ 5366 ctl_set_sense(ctsio, /*current_error*/ 1, 5367 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5368 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5369 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5370 ctl_done((union ctl_io *)ctsio); 5371 return (CTL_RETVAL_COMPLETE); 5372 } 5373 } 5374 5375 retval = lun->backend->config_write((union ctl_io *)ctsio); 5376 return (retval); 5377 } 5378 5379 int 5380 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5381 { 5382 struct ctl_lun *lun = CTL_LUN(ctsio); 5383 struct scsi_prevent *cdb; 5384 int retval; 5385 uint32_t initidx; 5386 5387 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5388 5389 cdb = (struct scsi_prevent *)ctsio->cdb; 5390 5391 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5392 ctl_set_invalid_opcode(ctsio); 5393 ctl_done((union ctl_io *)ctsio); 5394 return (CTL_RETVAL_COMPLETE); 5395 } 5396 5397 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5398 mtx_lock(&lun->lun_lock); 5399 if ((cdb->how & PR_PREVENT) && 5400 ctl_is_set(lun->prevent, initidx) == 0) { 5401 ctl_set_mask(lun->prevent, initidx); 5402 lun->prevent_count++; 5403 } else if ((cdb->how & PR_PREVENT) == 0 && 5404 ctl_is_set(lun->prevent, initidx)) { 5405 ctl_clear_mask(lun->prevent, initidx); 5406 lun->prevent_count--; 5407 } 5408 mtx_unlock(&lun->lun_lock); 5409 retval = lun->backend->config_write((union ctl_io *)ctsio); 5410 return (retval); 5411 } 5412 5413 /* 5414 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5415 * we don't really do anything with the LBA and length fields if the user 5416 * passes them in. Instead we'll just flush out the cache for the entire 5417 * LUN. 5418 */ 5419 int 5420 ctl_sync_cache(struct ctl_scsiio *ctsio) 5421 { 5422 struct ctl_lun *lun = CTL_LUN(ctsio); 5423 struct ctl_lba_len_flags *lbalen; 5424 uint64_t starting_lba; 5425 uint32_t block_count; 5426 int retval; 5427 uint8_t byte2; 5428 5429 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5430 5431 retval = 0; 5432 5433 switch (ctsio->cdb[0]) { 5434 case SYNCHRONIZE_CACHE: { 5435 struct scsi_sync_cache *cdb; 5436 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5437 5438 starting_lba = scsi_4btoul(cdb->begin_lba); 5439 block_count = scsi_2btoul(cdb->lb_count); 5440 byte2 = cdb->byte2; 5441 break; 5442 } 5443 case SYNCHRONIZE_CACHE_16: { 5444 struct scsi_sync_cache_16 *cdb; 5445 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5446 5447 starting_lba = scsi_8btou64(cdb->begin_lba); 5448 block_count = scsi_4btoul(cdb->lb_count); 5449 byte2 = cdb->byte2; 5450 break; 5451 } 5452 default: 5453 ctl_set_invalid_opcode(ctsio); 5454 ctl_done((union ctl_io *)ctsio); 5455 goto bailout; 5456 break; /* NOTREACHED */ 5457 } 5458 5459 /* 5460 * We check the LBA and length, but don't do anything with them. 5461 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5462 * get flushed. This check will just help satisfy anyone who wants 5463 * to see an error for an out of range LBA. 5464 */ 5465 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5466 ctl_set_lba_out_of_range(ctsio, 5467 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5468 ctl_done((union ctl_io *)ctsio); 5469 goto bailout; 5470 } 5471 5472 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5473 lbalen->lba = starting_lba; 5474 lbalen->len = block_count; 5475 lbalen->flags = byte2; 5476 retval = lun->backend->config_write((union ctl_io *)ctsio); 5477 5478 bailout: 5479 return (retval); 5480 } 5481 5482 int 5483 ctl_format(struct ctl_scsiio *ctsio) 5484 { 5485 struct scsi_format *cdb; 5486 int length, defect_list_len; 5487 5488 CTL_DEBUG_PRINT(("ctl_format\n")); 5489 5490 cdb = (struct scsi_format *)ctsio->cdb; 5491 5492 length = 0; 5493 if (cdb->byte2 & SF_FMTDATA) { 5494 if (cdb->byte2 & SF_LONGLIST) 5495 length = sizeof(struct scsi_format_header_long); 5496 else 5497 length = sizeof(struct scsi_format_header_short); 5498 } 5499 5500 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5501 && (length > 0)) { 5502 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5503 ctsio->kern_data_len = length; 5504 ctsio->kern_total_len = length; 5505 ctsio->kern_rel_offset = 0; 5506 ctsio->kern_sg_entries = 0; 5507 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5508 ctsio->be_move_done = ctl_config_move_done; 5509 ctl_datamove((union ctl_io *)ctsio); 5510 5511 return (CTL_RETVAL_COMPLETE); 5512 } 5513 5514 defect_list_len = 0; 5515 5516 if (cdb->byte2 & SF_FMTDATA) { 5517 if (cdb->byte2 & SF_LONGLIST) { 5518 struct scsi_format_header_long *header; 5519 5520 header = (struct scsi_format_header_long *) 5521 ctsio->kern_data_ptr; 5522 5523 defect_list_len = scsi_4btoul(header->defect_list_len); 5524 if (defect_list_len != 0) { 5525 ctl_set_invalid_field(ctsio, 5526 /*sks_valid*/ 1, 5527 /*command*/ 0, 5528 /*field*/ 2, 5529 /*bit_valid*/ 0, 5530 /*bit*/ 0); 5531 goto bailout; 5532 } 5533 } else { 5534 struct scsi_format_header_short *header; 5535 5536 header = (struct scsi_format_header_short *) 5537 ctsio->kern_data_ptr; 5538 5539 defect_list_len = scsi_2btoul(header->defect_list_len); 5540 if (defect_list_len != 0) { 5541 ctl_set_invalid_field(ctsio, 5542 /*sks_valid*/ 1, 5543 /*command*/ 0, 5544 /*field*/ 2, 5545 /*bit_valid*/ 0, 5546 /*bit*/ 0); 5547 goto bailout; 5548 } 5549 } 5550 } 5551 5552 ctl_set_success(ctsio); 5553 bailout: 5554 5555 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5556 free(ctsio->kern_data_ptr, M_CTL); 5557 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5558 } 5559 5560 ctl_done((union ctl_io *)ctsio); 5561 return (CTL_RETVAL_COMPLETE); 5562 } 5563 5564 int 5565 ctl_read_buffer(struct ctl_scsiio *ctsio) 5566 { 5567 struct ctl_lun *lun = CTL_LUN(ctsio); 5568 uint64_t buffer_offset; 5569 uint32_t len; 5570 uint8_t byte2; 5571 static uint8_t descr[4]; 5572 static uint8_t echo_descr[4] = { 0 }; 5573 5574 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5575 5576 switch (ctsio->cdb[0]) { 5577 case READ_BUFFER: { 5578 struct scsi_read_buffer *cdb; 5579 5580 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5581 buffer_offset = scsi_3btoul(cdb->offset); 5582 len = scsi_3btoul(cdb->length); 5583 byte2 = cdb->byte2; 5584 break; 5585 } 5586 case READ_BUFFER_16: { 5587 struct scsi_read_buffer_16 *cdb; 5588 5589 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5590 buffer_offset = scsi_8btou64(cdb->offset); 5591 len = scsi_4btoul(cdb->length); 5592 byte2 = cdb->byte2; 5593 break; 5594 } 5595 default: /* This shouldn't happen. */ 5596 ctl_set_invalid_opcode(ctsio); 5597 ctl_done((union ctl_io *)ctsio); 5598 return (CTL_RETVAL_COMPLETE); 5599 } 5600 5601 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5602 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5603 ctl_set_invalid_field(ctsio, 5604 /*sks_valid*/ 1, 5605 /*command*/ 1, 5606 /*field*/ 6, 5607 /*bit_valid*/ 0, 5608 /*bit*/ 0); 5609 ctl_done((union ctl_io *)ctsio); 5610 return (CTL_RETVAL_COMPLETE); 5611 } 5612 5613 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5614 descr[0] = 0; 5615 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5616 ctsio->kern_data_ptr = descr; 5617 len = min(len, sizeof(descr)); 5618 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5619 ctsio->kern_data_ptr = echo_descr; 5620 len = min(len, sizeof(echo_descr)); 5621 } else { 5622 if (lun->write_buffer == NULL) { 5623 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5624 M_CTL, M_WAITOK); 5625 } 5626 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5627 } 5628 ctsio->kern_data_len = len; 5629 ctsio->kern_total_len = len; 5630 ctsio->kern_rel_offset = 0; 5631 ctsio->kern_sg_entries = 0; 5632 ctl_set_success(ctsio); 5633 ctsio->be_move_done = ctl_config_move_done; 5634 ctl_datamove((union ctl_io *)ctsio); 5635 return (CTL_RETVAL_COMPLETE); 5636 } 5637 5638 int 5639 ctl_write_buffer(struct ctl_scsiio *ctsio) 5640 { 5641 struct ctl_lun *lun = CTL_LUN(ctsio); 5642 struct scsi_write_buffer *cdb; 5643 int buffer_offset, len; 5644 5645 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5646 5647 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5648 5649 len = scsi_3btoul(cdb->length); 5650 buffer_offset = scsi_3btoul(cdb->offset); 5651 5652 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5653 ctl_set_invalid_field(ctsio, 5654 /*sks_valid*/ 1, 5655 /*command*/ 1, 5656 /*field*/ 6, 5657 /*bit_valid*/ 0, 5658 /*bit*/ 0); 5659 ctl_done((union ctl_io *)ctsio); 5660 return (CTL_RETVAL_COMPLETE); 5661 } 5662 5663 /* 5664 * If we've got a kernel request that hasn't been malloced yet, 5665 * malloc it and tell the caller the data buffer is here. 5666 */ 5667 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5668 if (lun->write_buffer == NULL) { 5669 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5670 M_CTL, M_WAITOK); 5671 } 5672 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5673 ctsio->kern_data_len = len; 5674 ctsio->kern_total_len = len; 5675 ctsio->kern_rel_offset = 0; 5676 ctsio->kern_sg_entries = 0; 5677 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5678 ctsio->be_move_done = ctl_config_move_done; 5679 ctl_datamove((union ctl_io *)ctsio); 5680 5681 return (CTL_RETVAL_COMPLETE); 5682 } 5683 5684 ctl_set_success(ctsio); 5685 ctl_done((union ctl_io *)ctsio); 5686 return (CTL_RETVAL_COMPLETE); 5687 } 5688 5689 static int 5690 ctl_write_same_cont(union ctl_io *io) 5691 { 5692 struct ctl_lun *lun = CTL_LUN(io); 5693 struct ctl_scsiio *ctsio; 5694 struct ctl_lba_len_flags *lbalen; 5695 int retval; 5696 5697 ctsio = &io->scsiio; 5698 ctsio->io_hdr.status = CTL_STATUS_NONE; 5699 lbalen = (struct ctl_lba_len_flags *) 5700 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5701 lbalen->lba += lbalen->len; 5702 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) { 5703 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 5704 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba; 5705 } 5706 5707 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n")); 5708 retval = lun->backend->config_write((union ctl_io *)ctsio); 5709 return (retval); 5710 } 5711 5712 int 5713 ctl_write_same(struct ctl_scsiio *ctsio) 5714 { 5715 struct ctl_lun *lun = CTL_LUN(ctsio); 5716 struct ctl_lba_len_flags *lbalen; 5717 const char *val; 5718 uint64_t lba, ival; 5719 uint32_t num_blocks; 5720 int len, retval; 5721 uint8_t byte2; 5722 5723 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5724 5725 switch (ctsio->cdb[0]) { 5726 case WRITE_SAME_10: { 5727 struct scsi_write_same_10 *cdb; 5728 5729 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5730 5731 lba = scsi_4btoul(cdb->addr); 5732 num_blocks = scsi_2btoul(cdb->length); 5733 byte2 = cdb->byte2; 5734 break; 5735 } 5736 case WRITE_SAME_16: { 5737 struct scsi_write_same_16 *cdb; 5738 5739 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5740 5741 lba = scsi_8btou64(cdb->addr); 5742 num_blocks = scsi_4btoul(cdb->length); 5743 byte2 = cdb->byte2; 5744 break; 5745 } 5746 default: 5747 /* 5748 * We got a command we don't support. This shouldn't 5749 * happen, commands should be filtered out above us. 5750 */ 5751 ctl_set_invalid_opcode(ctsio); 5752 ctl_done((union ctl_io *)ctsio); 5753 5754 return (CTL_RETVAL_COMPLETE); 5755 break; /* NOTREACHED */ 5756 } 5757 5758 /* ANCHOR flag can be used only together with UNMAP */ 5759 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5760 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5761 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5762 ctl_done((union ctl_io *)ctsio); 5763 return (CTL_RETVAL_COMPLETE); 5764 } 5765 5766 /* 5767 * The first check is to make sure we're in bounds, the second 5768 * check is to catch wrap-around problems. If the lba + num blocks 5769 * is less than the lba, then we've wrapped around and the block 5770 * range is invalid anyway. 5771 */ 5772 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5773 || ((lba + num_blocks) < lba)) { 5774 ctl_set_lba_out_of_range(ctsio, 5775 MAX(lba, lun->be_lun->maxlba + 1)); 5776 ctl_done((union ctl_io *)ctsio); 5777 return (CTL_RETVAL_COMPLETE); 5778 } 5779 5780 /* Zero number of blocks means "to the last logical block" */ 5781 if (num_blocks == 0) { 5782 ival = UINT64_MAX; 5783 val = dnvlist_get_string(lun->be_lun->options, 5784 "write_same_max_lba", NULL); 5785 if (val != NULL) 5786 ctl_expand_number(val, &ival); 5787 if ((lun->be_lun->maxlba + 1) - lba > ival) { 5788 ctl_set_invalid_field(ctsio, 5789 /*sks_valid*/ 1, /*command*/ 1, 5790 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10, 5791 /*bit_valid*/ 0, /*bit*/ 0); 5792 ctl_done((union ctl_io *)ctsio); 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5796 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 5797 ctsio->io_cont = ctl_write_same_cont; 5798 num_blocks = 1 << 31; 5799 } else 5800 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5801 } 5802 5803 len = lun->be_lun->blocksize; 5804 5805 /* 5806 * If we've got a kernel request that hasn't been malloced yet, 5807 * malloc it and tell the caller the data buffer is here. 5808 */ 5809 if ((byte2 & SWS_NDOB) == 0 && 5810 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5811 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5812 ctsio->kern_data_len = len; 5813 ctsio->kern_total_len = len; 5814 ctsio->kern_rel_offset = 0; 5815 ctsio->kern_sg_entries = 0; 5816 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5817 ctsio->be_move_done = ctl_config_move_done; 5818 ctl_datamove((union ctl_io *)ctsio); 5819 5820 return (CTL_RETVAL_COMPLETE); 5821 } 5822 5823 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5824 lbalen->lba = lba; 5825 lbalen->len = num_blocks; 5826 lbalen->flags = byte2; 5827 retval = lun->backend->config_write((union ctl_io *)ctsio); 5828 5829 return (retval); 5830 } 5831 5832 int 5833 ctl_unmap(struct ctl_scsiio *ctsio) 5834 { 5835 struct ctl_lun *lun = CTL_LUN(ctsio); 5836 struct scsi_unmap *cdb; 5837 struct ctl_ptr_len_flags *ptrlen; 5838 struct scsi_unmap_header *hdr; 5839 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5840 uint64_t lba; 5841 uint32_t num_blocks; 5842 int len, retval; 5843 uint8_t byte2; 5844 5845 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5846 5847 cdb = (struct scsi_unmap *)ctsio->cdb; 5848 len = scsi_2btoul(cdb->length); 5849 byte2 = cdb->byte2; 5850 5851 /* 5852 * If we've got a kernel request that hasn't been malloced yet, 5853 * malloc it and tell the caller the data buffer is here. 5854 */ 5855 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5856 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5857 ctsio->kern_data_len = len; 5858 ctsio->kern_total_len = len; 5859 ctsio->kern_rel_offset = 0; 5860 ctsio->kern_sg_entries = 0; 5861 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5862 ctsio->be_move_done = ctl_config_move_done; 5863 ctl_datamove((union ctl_io *)ctsio); 5864 5865 return (CTL_RETVAL_COMPLETE); 5866 } 5867 5868 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5869 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5870 if (len < sizeof (*hdr) || 5871 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5872 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5873 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5874 ctl_set_invalid_field(ctsio, 5875 /*sks_valid*/ 0, 5876 /*command*/ 0, 5877 /*field*/ 0, 5878 /*bit_valid*/ 0, 5879 /*bit*/ 0); 5880 goto done; 5881 } 5882 len = scsi_2btoul(hdr->desc_length); 5883 buf = (struct scsi_unmap_desc *)(hdr + 1); 5884 end = buf + len / sizeof(*buf); 5885 5886 endnz = buf; 5887 for (range = buf; range < end; range++) { 5888 lba = scsi_8btou64(range->lba); 5889 num_blocks = scsi_4btoul(range->length); 5890 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5891 || ((lba + num_blocks) < lba)) { 5892 ctl_set_lba_out_of_range(ctsio, 5893 MAX(lba, lun->be_lun->maxlba + 1)); 5894 ctl_done((union ctl_io *)ctsio); 5895 return (CTL_RETVAL_COMPLETE); 5896 } 5897 if (num_blocks != 0) 5898 endnz = range + 1; 5899 } 5900 5901 /* 5902 * Block backend can not handle zero last range. 5903 * Filter it out and return if there is nothing left. 5904 */ 5905 len = (uint8_t *)endnz - (uint8_t *)buf; 5906 if (len == 0) { 5907 ctl_set_success(ctsio); 5908 goto done; 5909 } 5910 5911 mtx_lock(&lun->lun_lock); 5912 ptrlen = (struct ctl_ptr_len_flags *) 5913 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5914 ptrlen->ptr = (void *)buf; 5915 ptrlen->len = len; 5916 ptrlen->flags = byte2; 5917 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE); 5918 mtx_unlock(&lun->lun_lock); 5919 5920 retval = lun->backend->config_write((union ctl_io *)ctsio); 5921 return (retval); 5922 5923 done: 5924 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5925 free(ctsio->kern_data_ptr, M_CTL); 5926 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5927 } 5928 ctl_done((union ctl_io *)ctsio); 5929 return (CTL_RETVAL_COMPLETE); 5930 } 5931 5932 int 5933 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5934 struct ctl_page_index *page_index, uint8_t *page_ptr) 5935 { 5936 struct ctl_lun *lun = CTL_LUN(ctsio); 5937 uint8_t *current_cp; 5938 int set_ua; 5939 uint32_t initidx; 5940 5941 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5942 set_ua = 0; 5943 5944 current_cp = (page_index->page_data + (page_index->page_len * 5945 CTL_PAGE_CURRENT)); 5946 5947 mtx_lock(&lun->lun_lock); 5948 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5949 memcpy(current_cp, page_ptr, page_index->page_len); 5950 set_ua = 1; 5951 } 5952 if (set_ua != 0) 5953 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5954 mtx_unlock(&lun->lun_lock); 5955 if (set_ua) { 5956 ctl_isc_announce_mode(lun, 5957 ctl_get_initindex(&ctsio->io_hdr.nexus), 5958 page_index->page_code, page_index->subpage); 5959 } 5960 return (CTL_RETVAL_COMPLETE); 5961 } 5962 5963 static void 5964 ctl_ie_timer(void *arg) 5965 { 5966 struct ctl_lun *lun = arg; 5967 uint64_t t; 5968 5969 if (lun->ie_asc == 0) 5970 return; 5971 5972 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5973 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5974 else 5975 lun->ie_reported = 0; 5976 5977 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5978 lun->ie_reportcnt++; 5979 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5980 if (t == 0 || t == UINT32_MAX) 5981 t = 3000; /* 5 min */ 5982 callout_schedule_sbt(&lun->ie_callout, SBT_1S / 10 * t, 5983 SBT_1S / 10, 0); 5984 } 5985 } 5986 5987 int 5988 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5989 struct ctl_page_index *page_index, uint8_t *page_ptr) 5990 { 5991 struct ctl_lun *lun = CTL_LUN(ctsio); 5992 struct scsi_info_exceptions_page *pg; 5993 uint64_t t; 5994 5995 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5996 5997 pg = (struct scsi_info_exceptions_page *)page_ptr; 5998 mtx_lock(&lun->lun_lock); 5999 if (pg->info_flags & SIEP_FLAGS_TEST) { 6000 lun->ie_asc = 0x5d; 6001 lun->ie_ascq = 0xff; 6002 if (pg->mrie == SIEP_MRIE_UA) { 6003 ctl_est_ua_all(lun, -1, CTL_UA_IE); 6004 lun->ie_reported = 1; 6005 } else { 6006 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 6007 lun->ie_reported = -1; 6008 } 6009 lun->ie_reportcnt = 1; 6010 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 6011 lun->ie_reportcnt++; 6012 t = scsi_4btoul(pg->interval_timer); 6013 if (t == 0 || t == UINT32_MAX) 6014 t = 3000; /* 5 min */ 6015 callout_reset_sbt(&lun->ie_callout, SBT_1S / 10 * t, 6016 SBT_1S / 10, ctl_ie_timer, lun, 0); 6017 } 6018 } else { 6019 lun->ie_asc = 0; 6020 lun->ie_ascq = 0; 6021 lun->ie_reported = 1; 6022 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 6023 lun->ie_reportcnt = UINT32_MAX; 6024 callout_stop(&lun->ie_callout); 6025 } 6026 mtx_unlock(&lun->lun_lock); 6027 return (CTL_RETVAL_COMPLETE); 6028 } 6029 6030 static int 6031 ctl_do_mode_select(union ctl_io *io) 6032 { 6033 struct ctl_lun *lun = CTL_LUN(io); 6034 struct scsi_mode_page_header *page_header; 6035 struct ctl_page_index *page_index; 6036 struct ctl_scsiio *ctsio; 6037 int page_len, page_len_offset, page_len_size; 6038 union ctl_modepage_info *modepage_info; 6039 uint16_t *len_left, *len_used; 6040 int retval, i; 6041 6042 ctsio = &io->scsiio; 6043 page_index = NULL; 6044 page_len = 0; 6045 6046 modepage_info = (union ctl_modepage_info *) 6047 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6048 len_left = &modepage_info->header.len_left; 6049 len_used = &modepage_info->header.len_used; 6050 6051 do_next_page: 6052 6053 page_header = (struct scsi_mode_page_header *) 6054 (ctsio->kern_data_ptr + *len_used); 6055 6056 if (*len_left == 0) { 6057 free(ctsio->kern_data_ptr, M_CTL); 6058 ctl_set_success(ctsio); 6059 ctl_done((union ctl_io *)ctsio); 6060 return (CTL_RETVAL_COMPLETE); 6061 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6062 free(ctsio->kern_data_ptr, M_CTL); 6063 ctl_set_param_len_error(ctsio); 6064 ctl_done((union ctl_io *)ctsio); 6065 return (CTL_RETVAL_COMPLETE); 6066 6067 } else if ((page_header->page_code & SMPH_SPF) 6068 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6069 free(ctsio->kern_data_ptr, M_CTL); 6070 ctl_set_param_len_error(ctsio); 6071 ctl_done((union ctl_io *)ctsio); 6072 return (CTL_RETVAL_COMPLETE); 6073 } 6074 6075 /* 6076 * XXX KDM should we do something with the block descriptor? 6077 */ 6078 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6079 page_index = &lun->mode_pages.index[i]; 6080 if (lun->be_lun->lun_type == T_DIRECT && 6081 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6082 continue; 6083 if (lun->be_lun->lun_type == T_PROCESSOR && 6084 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6085 continue; 6086 if (lun->be_lun->lun_type == T_CDROM && 6087 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6088 continue; 6089 6090 if ((page_index->page_code & SMPH_PC_MASK) != 6091 (page_header->page_code & SMPH_PC_MASK)) 6092 continue; 6093 6094 /* 6095 * If neither page has a subpage code, then we've got a 6096 * match. 6097 */ 6098 if (((page_index->page_code & SMPH_SPF) == 0) 6099 && ((page_header->page_code & SMPH_SPF) == 0)) { 6100 page_len = page_header->page_length; 6101 break; 6102 } 6103 6104 /* 6105 * If both pages have subpages, then the subpage numbers 6106 * have to match. 6107 */ 6108 if ((page_index->page_code & SMPH_SPF) 6109 && (page_header->page_code & SMPH_SPF)) { 6110 struct scsi_mode_page_header_sp *sph; 6111 6112 sph = (struct scsi_mode_page_header_sp *)page_header; 6113 if (page_index->subpage == sph->subpage) { 6114 page_len = scsi_2btoul(sph->page_length); 6115 break; 6116 } 6117 } 6118 } 6119 6120 /* 6121 * If we couldn't find the page, or if we don't have a mode select 6122 * handler for it, send back an error to the user. 6123 */ 6124 if ((i >= CTL_NUM_MODE_PAGES) 6125 || (page_index->select_handler == NULL)) { 6126 ctl_set_invalid_field(ctsio, 6127 /*sks_valid*/ 1, 6128 /*command*/ 0, 6129 /*field*/ *len_used, 6130 /*bit_valid*/ 0, 6131 /*bit*/ 0); 6132 free(ctsio->kern_data_ptr, M_CTL); 6133 ctl_done((union ctl_io *)ctsio); 6134 return (CTL_RETVAL_COMPLETE); 6135 } 6136 6137 if (page_index->page_code & SMPH_SPF) { 6138 page_len_offset = 2; 6139 page_len_size = 2; 6140 } else { 6141 page_len_size = 1; 6142 page_len_offset = 1; 6143 } 6144 6145 /* 6146 * If the length the initiator gives us isn't the one we specify in 6147 * the mode page header, or if they didn't specify enough data in 6148 * the CDB to avoid truncating this page, kick out the request. 6149 */ 6150 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6151 ctl_set_invalid_field(ctsio, 6152 /*sks_valid*/ 1, 6153 /*command*/ 0, 6154 /*field*/ *len_used + page_len_offset, 6155 /*bit_valid*/ 0, 6156 /*bit*/ 0); 6157 free(ctsio->kern_data_ptr, M_CTL); 6158 ctl_done((union ctl_io *)ctsio); 6159 return (CTL_RETVAL_COMPLETE); 6160 } 6161 if (*len_left < page_index->page_len) { 6162 free(ctsio->kern_data_ptr, M_CTL); 6163 ctl_set_param_len_error(ctsio); 6164 ctl_done((union ctl_io *)ctsio); 6165 return (CTL_RETVAL_COMPLETE); 6166 } 6167 6168 /* 6169 * Run through the mode page, checking to make sure that the bits 6170 * the user changed are actually legal for him to change. 6171 */ 6172 for (i = 0; i < page_index->page_len; i++) { 6173 uint8_t *user_byte, *change_mask, *current_byte; 6174 int bad_bit; 6175 int j; 6176 6177 user_byte = (uint8_t *)page_header + i; 6178 change_mask = page_index->page_data + 6179 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6180 current_byte = page_index->page_data + 6181 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6182 6183 /* 6184 * Check to see whether the user set any bits in this byte 6185 * that he is not allowed to set. 6186 */ 6187 if ((*user_byte & ~(*change_mask)) == 6188 (*current_byte & ~(*change_mask))) 6189 continue; 6190 6191 /* 6192 * Go through bit by bit to determine which one is illegal. 6193 */ 6194 bad_bit = 0; 6195 for (j = 7; j >= 0; j--) { 6196 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6197 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6198 bad_bit = i; 6199 break; 6200 } 6201 } 6202 ctl_set_invalid_field(ctsio, 6203 /*sks_valid*/ 1, 6204 /*command*/ 0, 6205 /*field*/ *len_used + i, 6206 /*bit_valid*/ 1, 6207 /*bit*/ bad_bit); 6208 free(ctsio->kern_data_ptr, M_CTL); 6209 ctl_done((union ctl_io *)ctsio); 6210 return (CTL_RETVAL_COMPLETE); 6211 } 6212 6213 /* 6214 * Decrement these before we call the page handler, since we may 6215 * end up getting called back one way or another before the handler 6216 * returns to this context. 6217 */ 6218 *len_left -= page_index->page_len; 6219 *len_used += page_index->page_len; 6220 6221 retval = page_index->select_handler(ctsio, page_index, 6222 (uint8_t *)page_header); 6223 6224 /* 6225 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6226 * wait until this queued command completes to finish processing 6227 * the mode page. If it returns anything other than 6228 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6229 * already set the sense information, freed the data pointer, and 6230 * completed the io for us. 6231 */ 6232 if (retval != CTL_RETVAL_COMPLETE) 6233 goto bailout_no_done; 6234 6235 /* 6236 * If the initiator sent us more than one page, parse the next one. 6237 */ 6238 if (*len_left > 0) 6239 goto do_next_page; 6240 6241 ctl_set_success(ctsio); 6242 free(ctsio->kern_data_ptr, M_CTL); 6243 ctl_done((union ctl_io *)ctsio); 6244 6245 bailout_no_done: 6246 6247 return (CTL_RETVAL_COMPLETE); 6248 6249 } 6250 6251 int 6252 ctl_mode_select(struct ctl_scsiio *ctsio) 6253 { 6254 struct ctl_lun *lun = CTL_LUN(ctsio); 6255 union ctl_modepage_info *modepage_info; 6256 int bd_len, i, header_size, param_len, rtd; 6257 uint32_t initidx; 6258 6259 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6260 switch (ctsio->cdb[0]) { 6261 case MODE_SELECT_6: { 6262 struct scsi_mode_select_6 *cdb; 6263 6264 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6265 6266 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6267 param_len = cdb->length; 6268 header_size = sizeof(struct scsi_mode_header_6); 6269 break; 6270 } 6271 case MODE_SELECT_10: { 6272 struct scsi_mode_select_10 *cdb; 6273 6274 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6275 6276 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6277 param_len = scsi_2btoul(cdb->length); 6278 header_size = sizeof(struct scsi_mode_header_10); 6279 break; 6280 } 6281 default: 6282 ctl_set_invalid_opcode(ctsio); 6283 ctl_done((union ctl_io *)ctsio); 6284 return (CTL_RETVAL_COMPLETE); 6285 } 6286 6287 if (rtd) { 6288 if (param_len != 0) { 6289 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6290 /*command*/ 1, /*field*/ 0, 6291 /*bit_valid*/ 0, /*bit*/ 0); 6292 ctl_done((union ctl_io *)ctsio); 6293 return (CTL_RETVAL_COMPLETE); 6294 } 6295 6296 /* Revert to defaults. */ 6297 ctl_init_page_index(lun); 6298 mtx_lock(&lun->lun_lock); 6299 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6300 mtx_unlock(&lun->lun_lock); 6301 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6302 ctl_isc_announce_mode(lun, -1, 6303 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6304 lun->mode_pages.index[i].subpage); 6305 } 6306 ctl_set_success(ctsio); 6307 ctl_done((union ctl_io *)ctsio); 6308 return (CTL_RETVAL_COMPLETE); 6309 } 6310 6311 /* 6312 * From SPC-3: 6313 * "A parameter list length of zero indicates that the Data-Out Buffer 6314 * shall be empty. This condition shall not be considered as an error." 6315 */ 6316 if (param_len == 0) { 6317 ctl_set_success(ctsio); 6318 ctl_done((union ctl_io *)ctsio); 6319 return (CTL_RETVAL_COMPLETE); 6320 } 6321 6322 /* 6323 * Since we'll hit this the first time through, prior to 6324 * allocation, we don't need to free a data buffer here. 6325 */ 6326 if (param_len < header_size) { 6327 ctl_set_param_len_error(ctsio); 6328 ctl_done((union ctl_io *)ctsio); 6329 return (CTL_RETVAL_COMPLETE); 6330 } 6331 6332 /* 6333 * Allocate the data buffer and grab the user's data. In theory, 6334 * we shouldn't have to sanity check the parameter list length here 6335 * because the maximum size is 64K. We should be able to malloc 6336 * that much without too many problems. 6337 */ 6338 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6339 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6340 ctsio->kern_data_len = param_len; 6341 ctsio->kern_total_len = param_len; 6342 ctsio->kern_rel_offset = 0; 6343 ctsio->kern_sg_entries = 0; 6344 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6345 ctsio->be_move_done = ctl_config_move_done; 6346 ctl_datamove((union ctl_io *)ctsio); 6347 6348 return (CTL_RETVAL_COMPLETE); 6349 } 6350 6351 switch (ctsio->cdb[0]) { 6352 case MODE_SELECT_6: { 6353 struct scsi_mode_header_6 *mh6; 6354 6355 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6356 bd_len = mh6->blk_desc_len; 6357 break; 6358 } 6359 case MODE_SELECT_10: { 6360 struct scsi_mode_header_10 *mh10; 6361 6362 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6363 bd_len = scsi_2btoul(mh10->blk_desc_len); 6364 break; 6365 } 6366 default: 6367 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6368 } 6369 6370 if (param_len < (header_size + bd_len)) { 6371 free(ctsio->kern_data_ptr, M_CTL); 6372 ctl_set_param_len_error(ctsio); 6373 ctl_done((union ctl_io *)ctsio); 6374 return (CTL_RETVAL_COMPLETE); 6375 } 6376 6377 /* 6378 * Set the IO_CONT flag, so that if this I/O gets passed to 6379 * ctl_config_write_done(), it'll get passed back to 6380 * ctl_do_mode_select() for further processing, or completion if 6381 * we're all done. 6382 */ 6383 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6384 ctsio->io_cont = ctl_do_mode_select; 6385 6386 modepage_info = (union ctl_modepage_info *) 6387 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6388 memset(modepage_info, 0, sizeof(*modepage_info)); 6389 modepage_info->header.len_left = param_len - header_size - bd_len; 6390 modepage_info->header.len_used = header_size + bd_len; 6391 6392 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6393 } 6394 6395 int 6396 ctl_mode_sense(struct ctl_scsiio *ctsio) 6397 { 6398 struct ctl_lun *lun = CTL_LUN(ctsio); 6399 int pc, page_code, llba, subpage; 6400 int alloc_len, page_len, header_len, bd_len, total_len; 6401 void *block_desc; 6402 struct ctl_page_index *page_index; 6403 6404 llba = 0; 6405 6406 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6407 6408 switch (ctsio->cdb[0]) { 6409 case MODE_SENSE_6: { 6410 struct scsi_mode_sense_6 *cdb; 6411 6412 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6413 6414 header_len = sizeof(struct scsi_mode_hdr_6); 6415 if (cdb->byte2 & SMS_DBD) 6416 bd_len = 0; 6417 else 6418 bd_len = sizeof(struct scsi_mode_block_descr); 6419 header_len += bd_len; 6420 6421 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6422 page_code = cdb->page & SMS_PAGE_CODE; 6423 subpage = cdb->subpage; 6424 alloc_len = cdb->length; 6425 break; 6426 } 6427 case MODE_SENSE_10: { 6428 struct scsi_mode_sense_10 *cdb; 6429 6430 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6431 6432 header_len = sizeof(struct scsi_mode_hdr_10); 6433 if (cdb->byte2 & SMS_DBD) { 6434 bd_len = 0; 6435 } else if (lun->be_lun->lun_type == T_DIRECT) { 6436 if (cdb->byte2 & SMS10_LLBAA) { 6437 llba = 1; 6438 bd_len = sizeof(struct scsi_mode_block_descr_dlong); 6439 } else 6440 bd_len = sizeof(struct scsi_mode_block_descr_dshort); 6441 } else 6442 bd_len = sizeof(struct scsi_mode_block_descr); 6443 header_len += bd_len; 6444 6445 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6446 page_code = cdb->page & SMS_PAGE_CODE; 6447 subpage = cdb->subpage; 6448 alloc_len = scsi_2btoul(cdb->length); 6449 break; 6450 } 6451 default: 6452 ctl_set_invalid_opcode(ctsio); 6453 ctl_done((union ctl_io *)ctsio); 6454 return (CTL_RETVAL_COMPLETE); 6455 break; /* NOTREACHED */ 6456 } 6457 6458 /* 6459 * We have to make a first pass through to calculate the size of 6460 * the pages that match the user's query. Then we allocate enough 6461 * memory to hold it, and actually copy the data into the buffer. 6462 */ 6463 switch (page_code) { 6464 case SMS_ALL_PAGES_PAGE: { 6465 u_int i; 6466 6467 page_len = 0; 6468 6469 /* 6470 * At the moment, values other than 0 and 0xff here are 6471 * reserved according to SPC-3. 6472 */ 6473 if ((subpage != SMS_SUBPAGE_PAGE_0) 6474 && (subpage != SMS_SUBPAGE_ALL)) { 6475 ctl_set_invalid_field(ctsio, 6476 /*sks_valid*/ 1, 6477 /*command*/ 1, 6478 /*field*/ 3, 6479 /*bit_valid*/ 0, 6480 /*bit*/ 0); 6481 ctl_done((union ctl_io *)ctsio); 6482 return (CTL_RETVAL_COMPLETE); 6483 } 6484 6485 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6486 page_index = &lun->mode_pages.index[i]; 6487 6488 /* Make sure the page is supported for this dev type */ 6489 if (lun->be_lun->lun_type == T_DIRECT && 6490 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6491 continue; 6492 if (lun->be_lun->lun_type == T_PROCESSOR && 6493 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6494 continue; 6495 if (lun->be_lun->lun_type == T_CDROM && 6496 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6497 continue; 6498 6499 /* 6500 * We don't use this subpage if the user didn't 6501 * request all subpages. 6502 */ 6503 if ((page_index->subpage != 0) 6504 && (subpage == SMS_SUBPAGE_PAGE_0)) 6505 continue; 6506 6507 page_len += page_index->page_len; 6508 } 6509 break; 6510 } 6511 default: { 6512 u_int i; 6513 6514 page_len = 0; 6515 6516 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6517 page_index = &lun->mode_pages.index[i]; 6518 6519 /* Make sure the page is supported for this dev type */ 6520 if (lun->be_lun->lun_type == T_DIRECT && 6521 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6522 continue; 6523 if (lun->be_lun->lun_type == T_PROCESSOR && 6524 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6525 continue; 6526 if (lun->be_lun->lun_type == T_CDROM && 6527 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6528 continue; 6529 6530 /* Look for the right page code */ 6531 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6532 continue; 6533 6534 /* Look for the right subpage or the subpage wildcard*/ 6535 if ((page_index->subpage != subpage) 6536 && (subpage != SMS_SUBPAGE_ALL)) 6537 continue; 6538 6539 page_len += page_index->page_len; 6540 } 6541 6542 if (page_len == 0) { 6543 ctl_set_invalid_field(ctsio, 6544 /*sks_valid*/ 1, 6545 /*command*/ 1, 6546 /*field*/ 2, 6547 /*bit_valid*/ 1, 6548 /*bit*/ 5); 6549 ctl_done((union ctl_io *)ctsio); 6550 return (CTL_RETVAL_COMPLETE); 6551 } 6552 break; 6553 } 6554 } 6555 6556 total_len = header_len + page_len; 6557 6558 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6559 ctsio->kern_sg_entries = 0; 6560 ctsio->kern_rel_offset = 0; 6561 ctsio->kern_data_len = min(total_len, alloc_len); 6562 ctsio->kern_total_len = ctsio->kern_data_len; 6563 6564 switch (ctsio->cdb[0]) { 6565 case MODE_SENSE_6: { 6566 struct scsi_mode_hdr_6 *header; 6567 6568 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6569 6570 header->datalen = MIN(total_len - 1, 254); 6571 if (lun->be_lun->lun_type == T_DIRECT) { 6572 header->dev_specific = 0x10; /* DPOFUA */ 6573 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6574 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6575 header->dev_specific |= 0x80; /* WP */ 6576 } 6577 header->block_descr_len = bd_len; 6578 block_desc = &header[1]; 6579 break; 6580 } 6581 case MODE_SENSE_10: { 6582 struct scsi_mode_hdr_10 *header; 6583 int datalen; 6584 6585 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6586 6587 datalen = MIN(total_len - 2, 65533); 6588 scsi_ulto2b(datalen, header->datalen); 6589 if (lun->be_lun->lun_type == T_DIRECT) { 6590 header->dev_specific = 0x10; /* DPOFUA */ 6591 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6592 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6593 header->dev_specific |= 0x80; /* WP */ 6594 } 6595 if (llba) 6596 header->flags |= SMH_LONGLBA; 6597 scsi_ulto2b(bd_len, header->block_descr_len); 6598 block_desc = &header[1]; 6599 break; 6600 } 6601 default: 6602 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6603 } 6604 6605 /* 6606 * If we've got a disk, use its blocksize in the block 6607 * descriptor. Otherwise, just set it to 0. 6608 */ 6609 if (bd_len > 0) { 6610 if (lun->be_lun->lun_type == T_DIRECT) { 6611 if (llba) { 6612 struct scsi_mode_block_descr_dlong *bd = block_desc; 6613 if (lun->be_lun->maxlba != 0) 6614 scsi_u64to8b(lun->be_lun->maxlba + 1, 6615 bd->num_blocks); 6616 scsi_ulto4b(lun->be_lun->blocksize, 6617 bd->block_len); 6618 } else { 6619 struct scsi_mode_block_descr_dshort *bd = block_desc; 6620 if (lun->be_lun->maxlba != 0) 6621 scsi_ulto4b(MIN(lun->be_lun->maxlba+1, 6622 UINT32_MAX), bd->num_blocks); 6623 scsi_ulto3b(lun->be_lun->blocksize, 6624 bd->block_len); 6625 } 6626 } else { 6627 struct scsi_mode_block_descr *bd = block_desc; 6628 scsi_ulto3b(0, bd->block_len); 6629 } 6630 } 6631 6632 switch (page_code) { 6633 case SMS_ALL_PAGES_PAGE: { 6634 int i, data_used; 6635 6636 data_used = header_len; 6637 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6638 struct ctl_page_index *page_index; 6639 6640 page_index = &lun->mode_pages.index[i]; 6641 if (lun->be_lun->lun_type == T_DIRECT && 6642 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6643 continue; 6644 if (lun->be_lun->lun_type == T_PROCESSOR && 6645 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6646 continue; 6647 if (lun->be_lun->lun_type == T_CDROM && 6648 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6649 continue; 6650 6651 /* 6652 * We don't use this subpage if the user didn't 6653 * request all subpages. We already checked (above) 6654 * to make sure the user only specified a subpage 6655 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6656 */ 6657 if ((page_index->subpage != 0) 6658 && (subpage == SMS_SUBPAGE_PAGE_0)) 6659 continue; 6660 6661 /* 6662 * Call the handler, if it exists, to update the 6663 * page to the latest values. 6664 */ 6665 if (page_index->sense_handler != NULL) 6666 page_index->sense_handler(ctsio, page_index,pc); 6667 6668 memcpy(ctsio->kern_data_ptr + data_used, 6669 page_index->page_data + 6670 (page_index->page_len * pc), 6671 page_index->page_len); 6672 data_used += page_index->page_len; 6673 } 6674 break; 6675 } 6676 default: { 6677 int i, data_used; 6678 6679 data_used = header_len; 6680 6681 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6682 struct ctl_page_index *page_index; 6683 6684 page_index = &lun->mode_pages.index[i]; 6685 6686 /* Look for the right page code */ 6687 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6688 continue; 6689 6690 /* Look for the right subpage or the subpage wildcard*/ 6691 if ((page_index->subpage != subpage) 6692 && (subpage != SMS_SUBPAGE_ALL)) 6693 continue; 6694 6695 /* Make sure the page is supported for this dev type */ 6696 if (lun->be_lun->lun_type == T_DIRECT && 6697 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6698 continue; 6699 if (lun->be_lun->lun_type == T_PROCESSOR && 6700 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6701 continue; 6702 if (lun->be_lun->lun_type == T_CDROM && 6703 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6704 continue; 6705 6706 /* 6707 * Call the handler, if it exists, to update the 6708 * page to the latest values. 6709 */ 6710 if (page_index->sense_handler != NULL) 6711 page_index->sense_handler(ctsio, page_index,pc); 6712 6713 memcpy(ctsio->kern_data_ptr + data_used, 6714 page_index->page_data + 6715 (page_index->page_len * pc), 6716 page_index->page_len); 6717 data_used += page_index->page_len; 6718 } 6719 break; 6720 } 6721 } 6722 6723 ctl_set_success(ctsio); 6724 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6725 ctsio->be_move_done = ctl_config_move_done; 6726 ctl_datamove((union ctl_io *)ctsio); 6727 return (CTL_RETVAL_COMPLETE); 6728 } 6729 6730 int 6731 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio, 6732 struct ctl_page_index *page_index, 6733 int pc) 6734 { 6735 struct ctl_lun *lun = CTL_LUN(ctsio); 6736 struct scsi_log_temperature *data; 6737 const char *value; 6738 6739 data = (struct scsi_log_temperature *)page_index->page_data; 6740 6741 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code); 6742 data->hdr.param_control = SLP_LBIN; 6743 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6744 sizeof(struct scsi_log_param_header); 6745 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6746 NULL)) != NULL) 6747 data->temperature = strtol(value, NULL, 0); 6748 else 6749 data->temperature = 0xff; 6750 data++; 6751 6752 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code); 6753 data->hdr.param_control = SLP_LBIN; 6754 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6755 sizeof(struct scsi_log_param_header); 6756 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature", 6757 NULL)) != NULL) 6758 data->temperature = strtol(value, NULL, 0); 6759 else 6760 data->temperature = 0xff; 6761 return (0); 6762 } 6763 6764 int 6765 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6766 struct ctl_page_index *page_index, 6767 int pc) 6768 { 6769 struct ctl_lun *lun = CTL_LUN(ctsio); 6770 struct scsi_log_param_header *phdr; 6771 uint8_t *data; 6772 uint64_t val; 6773 6774 data = page_index->page_data; 6775 6776 if (lun->backend->lun_attr != NULL && 6777 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail")) 6778 != UINT64_MAX) { 6779 phdr = (struct scsi_log_param_header *)data; 6780 scsi_ulto2b(0x0001, phdr->param_code); 6781 phdr->param_control = SLP_LBIN | SLP_LP; 6782 phdr->param_len = 8; 6783 data = (uint8_t *)(phdr + 1); 6784 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6785 data[4] = 0x02; /* per-pool */ 6786 data += phdr->param_len; 6787 } 6788 6789 if (lun->backend->lun_attr != NULL && 6790 (val = lun->backend->lun_attr(lun->be_lun, "blocksused")) 6791 != UINT64_MAX) { 6792 phdr = (struct scsi_log_param_header *)data; 6793 scsi_ulto2b(0x0002, phdr->param_code); 6794 phdr->param_control = SLP_LBIN | SLP_LP; 6795 phdr->param_len = 8; 6796 data = (uint8_t *)(phdr + 1); 6797 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6798 data[4] = 0x01; /* per-LUN */ 6799 data += phdr->param_len; 6800 } 6801 6802 if (lun->backend->lun_attr != NULL && 6803 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail")) 6804 != UINT64_MAX) { 6805 phdr = (struct scsi_log_param_header *)data; 6806 scsi_ulto2b(0x00f1, phdr->param_code); 6807 phdr->param_control = SLP_LBIN | SLP_LP; 6808 phdr->param_len = 8; 6809 data = (uint8_t *)(phdr + 1); 6810 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6811 data[4] = 0x02; /* per-pool */ 6812 data += phdr->param_len; 6813 } 6814 6815 if (lun->backend->lun_attr != NULL && 6816 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused")) 6817 != UINT64_MAX) { 6818 phdr = (struct scsi_log_param_header *)data; 6819 scsi_ulto2b(0x00f2, phdr->param_code); 6820 phdr->param_control = SLP_LBIN | SLP_LP; 6821 phdr->param_len = 8; 6822 data = (uint8_t *)(phdr + 1); 6823 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6824 data[4] = 0x02; /* per-pool */ 6825 data += phdr->param_len; 6826 } 6827 6828 page_index->page_len = data - page_index->page_data; 6829 return (0); 6830 } 6831 6832 int 6833 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6834 struct ctl_page_index *page_index, 6835 int pc) 6836 { 6837 struct ctl_lun *lun = CTL_LUN(ctsio); 6838 struct stat_page *data; 6839 struct bintime *t; 6840 6841 data = (struct stat_page *)page_index->page_data; 6842 6843 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6844 data->sap.hdr.param_control = SLP_LBIN; 6845 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6846 sizeof(struct scsi_log_param_header); 6847 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6848 data->sap.read_num); 6849 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6850 data->sap.write_num); 6851 if (lun->be_lun->blocksize > 0) { 6852 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6853 lun->be_lun->blocksize, data->sap.recvieved_lba); 6854 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6855 lun->be_lun->blocksize, data->sap.transmitted_lba); 6856 } 6857 t = &lun->stats.time[CTL_STATS_READ]; 6858 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6859 data->sap.read_int); 6860 t = &lun->stats.time[CTL_STATS_WRITE]; 6861 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6862 data->sap.write_int); 6863 scsi_u64to8b(0, data->sap.weighted_num); 6864 scsi_u64to8b(0, data->sap.weighted_int); 6865 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6866 data->it.hdr.param_control = SLP_LBIN; 6867 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6868 sizeof(struct scsi_log_param_header); 6869 #ifdef CTL_TIME_IO 6870 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6871 #endif 6872 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6873 data->it.hdr.param_control = SLP_LBIN; 6874 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6875 sizeof(struct scsi_log_param_header); 6876 scsi_ulto4b(3, data->ti.exponent); 6877 scsi_ulto4b(1, data->ti.integer); 6878 return (0); 6879 } 6880 6881 int 6882 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6883 struct ctl_page_index *page_index, 6884 int pc) 6885 { 6886 struct ctl_lun *lun = CTL_LUN(ctsio); 6887 struct scsi_log_informational_exceptions *data; 6888 const char *value; 6889 6890 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6891 6892 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6893 data->hdr.param_control = SLP_LBIN; 6894 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6895 sizeof(struct scsi_log_param_header); 6896 data->ie_asc = lun->ie_asc; 6897 data->ie_ascq = lun->ie_ascq; 6898 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6899 NULL)) != NULL) 6900 data->temperature = strtol(value, NULL, 0); 6901 else 6902 data->temperature = 0xff; 6903 return (0); 6904 } 6905 6906 int 6907 ctl_log_sense(struct ctl_scsiio *ctsio) 6908 { 6909 struct ctl_lun *lun = CTL_LUN(ctsio); 6910 int i, pc, page_code, subpage; 6911 int alloc_len, total_len; 6912 struct ctl_page_index *page_index; 6913 struct scsi_log_sense *cdb; 6914 struct scsi_log_header *header; 6915 6916 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6917 6918 cdb = (struct scsi_log_sense *)ctsio->cdb; 6919 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6920 page_code = cdb->page & SLS_PAGE_CODE; 6921 subpage = cdb->subpage; 6922 alloc_len = scsi_2btoul(cdb->length); 6923 6924 page_index = NULL; 6925 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6926 page_index = &lun->log_pages.index[i]; 6927 6928 /* Look for the right page code */ 6929 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6930 continue; 6931 6932 /* Look for the right subpage or the subpage wildcard*/ 6933 if (page_index->subpage != subpage) 6934 continue; 6935 6936 break; 6937 } 6938 if (i >= CTL_NUM_LOG_PAGES) { 6939 ctl_set_invalid_field(ctsio, 6940 /*sks_valid*/ 1, 6941 /*command*/ 1, 6942 /*field*/ 2, 6943 /*bit_valid*/ 0, 6944 /*bit*/ 0); 6945 ctl_done((union ctl_io *)ctsio); 6946 return (CTL_RETVAL_COMPLETE); 6947 } 6948 6949 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6950 6951 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6952 ctsio->kern_sg_entries = 0; 6953 ctsio->kern_rel_offset = 0; 6954 ctsio->kern_data_len = min(total_len, alloc_len); 6955 ctsio->kern_total_len = ctsio->kern_data_len; 6956 6957 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6958 header->page = page_index->page_code; 6959 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6960 header->page |= SL_DS; 6961 if (page_index->subpage) { 6962 header->page |= SL_SPF; 6963 header->subpage = page_index->subpage; 6964 } 6965 scsi_ulto2b(page_index->page_len, header->datalen); 6966 6967 /* 6968 * Call the handler, if it exists, to update the 6969 * page to the latest values. 6970 */ 6971 if (page_index->sense_handler != NULL) 6972 page_index->sense_handler(ctsio, page_index, pc); 6973 6974 memcpy(header + 1, page_index->page_data, page_index->page_len); 6975 6976 ctl_set_success(ctsio); 6977 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6978 ctsio->be_move_done = ctl_config_move_done; 6979 ctl_datamove((union ctl_io *)ctsio); 6980 return (CTL_RETVAL_COMPLETE); 6981 } 6982 6983 int 6984 ctl_read_capacity(struct ctl_scsiio *ctsio) 6985 { 6986 struct ctl_lun *lun = CTL_LUN(ctsio); 6987 struct scsi_read_capacity *cdb; 6988 struct scsi_read_capacity_data *data; 6989 uint32_t lba; 6990 6991 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6992 6993 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6994 6995 lba = scsi_4btoul(cdb->addr); 6996 if (((cdb->pmi & SRC_PMI) == 0) 6997 && (lba != 0)) { 6998 ctl_set_invalid_field(/*ctsio*/ ctsio, 6999 /*sks_valid*/ 1, 7000 /*command*/ 1, 7001 /*field*/ 2, 7002 /*bit_valid*/ 0, 7003 /*bit*/ 0); 7004 ctl_done((union ctl_io *)ctsio); 7005 return (CTL_RETVAL_COMPLETE); 7006 } 7007 7008 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7009 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7010 ctsio->kern_data_len = sizeof(*data); 7011 ctsio->kern_total_len = sizeof(*data); 7012 ctsio->kern_rel_offset = 0; 7013 ctsio->kern_sg_entries = 0; 7014 7015 /* 7016 * If the maximum LBA is greater than 0xfffffffe, the user must 7017 * issue a SERVICE ACTION IN (16) command, with the read capacity 7018 * serivce action set. 7019 */ 7020 if (lun->be_lun->maxlba > 0xfffffffe) 7021 scsi_ulto4b(0xffffffff, data->addr); 7022 else 7023 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7024 7025 /* 7026 * XXX KDM this may not be 512 bytes... 7027 */ 7028 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7029 7030 ctl_set_success(ctsio); 7031 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7032 ctsio->be_move_done = ctl_config_move_done; 7033 ctl_datamove((union ctl_io *)ctsio); 7034 return (CTL_RETVAL_COMPLETE); 7035 } 7036 7037 int 7038 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7039 { 7040 struct ctl_lun *lun = CTL_LUN(ctsio); 7041 struct scsi_read_capacity_16 *cdb; 7042 struct scsi_read_capacity_data_long *data; 7043 uint64_t lba; 7044 uint32_t alloc_len; 7045 7046 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7047 7048 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7049 7050 alloc_len = scsi_4btoul(cdb->alloc_len); 7051 lba = scsi_8btou64(cdb->addr); 7052 7053 if ((cdb->reladr & SRC16_PMI) 7054 && (lba != 0)) { 7055 ctl_set_invalid_field(/*ctsio*/ ctsio, 7056 /*sks_valid*/ 1, 7057 /*command*/ 1, 7058 /*field*/ 2, 7059 /*bit_valid*/ 0, 7060 /*bit*/ 0); 7061 ctl_done((union ctl_io *)ctsio); 7062 return (CTL_RETVAL_COMPLETE); 7063 } 7064 7065 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7066 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7067 ctsio->kern_rel_offset = 0; 7068 ctsio->kern_sg_entries = 0; 7069 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 7070 ctsio->kern_total_len = ctsio->kern_data_len; 7071 7072 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7073 /* XXX KDM this may not be 512 bytes... */ 7074 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7075 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7076 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7077 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7078 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7079 7080 ctl_set_success(ctsio); 7081 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7082 ctsio->be_move_done = ctl_config_move_done; 7083 ctl_datamove((union ctl_io *)ctsio); 7084 return (CTL_RETVAL_COMPLETE); 7085 } 7086 7087 int 7088 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7089 { 7090 struct ctl_lun *lun = CTL_LUN(ctsio); 7091 struct scsi_get_lba_status *cdb; 7092 struct scsi_get_lba_status_data *data; 7093 struct ctl_lba_len_flags *lbalen; 7094 uint64_t lba; 7095 uint32_t alloc_len, total_len; 7096 int retval; 7097 7098 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7099 7100 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7101 lba = scsi_8btou64(cdb->addr); 7102 alloc_len = scsi_4btoul(cdb->alloc_len); 7103 7104 if (lba > lun->be_lun->maxlba) { 7105 ctl_set_lba_out_of_range(ctsio, lba); 7106 ctl_done((union ctl_io *)ctsio); 7107 return (CTL_RETVAL_COMPLETE); 7108 } 7109 7110 total_len = sizeof(*data) + sizeof(data->descr[0]); 7111 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7112 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7113 ctsio->kern_rel_offset = 0; 7114 ctsio->kern_sg_entries = 0; 7115 ctsio->kern_data_len = min(total_len, alloc_len); 7116 ctsio->kern_total_len = ctsio->kern_data_len; 7117 7118 /* Fill dummy data in case backend can't tell anything. */ 7119 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7120 scsi_u64to8b(lba, data->descr[0].addr); 7121 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7122 data->descr[0].length); 7123 data->descr[0].status = 0; /* Mapped or unknown. */ 7124 7125 ctl_set_success(ctsio); 7126 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7127 ctsio->be_move_done = ctl_config_move_done; 7128 7129 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7130 lbalen->lba = lba; 7131 lbalen->len = total_len; 7132 lbalen->flags = 0; 7133 retval = lun->backend->config_read((union ctl_io *)ctsio); 7134 return (retval); 7135 } 7136 7137 int 7138 ctl_read_defect(struct ctl_scsiio *ctsio) 7139 { 7140 struct scsi_read_defect_data_10 *ccb10; 7141 struct scsi_read_defect_data_12 *ccb12; 7142 struct scsi_read_defect_data_hdr_10 *data10; 7143 struct scsi_read_defect_data_hdr_12 *data12; 7144 uint32_t alloc_len, data_len; 7145 uint8_t format; 7146 7147 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7148 7149 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7150 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7151 format = ccb10->format; 7152 alloc_len = scsi_2btoul(ccb10->alloc_length); 7153 data_len = sizeof(*data10); 7154 } else { 7155 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7156 format = ccb12->format; 7157 alloc_len = scsi_4btoul(ccb12->alloc_length); 7158 data_len = sizeof(*data12); 7159 } 7160 if (alloc_len == 0) { 7161 ctl_set_success(ctsio); 7162 ctl_done((union ctl_io *)ctsio); 7163 return (CTL_RETVAL_COMPLETE); 7164 } 7165 7166 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7167 ctsio->kern_rel_offset = 0; 7168 ctsio->kern_sg_entries = 0; 7169 ctsio->kern_data_len = min(data_len, alloc_len); 7170 ctsio->kern_total_len = ctsio->kern_data_len; 7171 7172 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7173 data10 = (struct scsi_read_defect_data_hdr_10 *) 7174 ctsio->kern_data_ptr; 7175 data10->format = format; 7176 scsi_ulto2b(0, data10->length); 7177 } else { 7178 data12 = (struct scsi_read_defect_data_hdr_12 *) 7179 ctsio->kern_data_ptr; 7180 data12->format = format; 7181 scsi_ulto2b(0, data12->generation); 7182 scsi_ulto4b(0, data12->length); 7183 } 7184 7185 ctl_set_success(ctsio); 7186 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7187 ctsio->be_move_done = ctl_config_move_done; 7188 ctl_datamove((union ctl_io *)ctsio); 7189 return (CTL_RETVAL_COMPLETE); 7190 } 7191 7192 int 7193 ctl_report_ident_info(struct ctl_scsiio *ctsio) 7194 { 7195 struct ctl_lun *lun = CTL_LUN(ctsio); 7196 struct scsi_report_ident_info *cdb; 7197 struct scsi_report_ident_info_data *rii_ptr; 7198 struct scsi_report_ident_info_descr *riid_ptr; 7199 const char *oii, *otii; 7200 int retval, alloc_len, total_len = 0, len = 0; 7201 7202 CTL_DEBUG_PRINT(("ctl_report_ident_info\n")); 7203 7204 cdb = (struct scsi_report_ident_info *)ctsio->cdb; 7205 retval = CTL_RETVAL_COMPLETE; 7206 7207 total_len = sizeof(struct scsi_report_ident_info_data); 7208 switch (cdb->type) { 7209 case RII_LUII: 7210 oii = dnvlist_get_string(lun->be_lun->options, 7211 "ident_info", NULL); 7212 if (oii) 7213 len = strlen(oii); /* Approximately */ 7214 break; 7215 case RII_LUTII: 7216 otii = dnvlist_get_string(lun->be_lun->options, 7217 "text_ident_info", NULL); 7218 if (otii) 7219 len = strlen(otii) + 1; /* NULL-terminated */ 7220 break; 7221 case RII_IIS: 7222 len = 2 * sizeof(struct scsi_report_ident_info_descr); 7223 break; 7224 default: 7225 ctl_set_invalid_field(/*ctsio*/ ctsio, 7226 /*sks_valid*/ 1, 7227 /*command*/ 1, 7228 /*field*/ 11, 7229 /*bit_valid*/ 1, 7230 /*bit*/ 2); 7231 ctl_done((union ctl_io *)ctsio); 7232 return(retval); 7233 } 7234 total_len += len; 7235 alloc_len = scsi_4btoul(cdb->length); 7236 7237 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7238 ctsio->kern_sg_entries = 0; 7239 ctsio->kern_rel_offset = 0; 7240 ctsio->kern_data_len = min(total_len, alloc_len); 7241 ctsio->kern_total_len = ctsio->kern_data_len; 7242 7243 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr; 7244 switch (cdb->type) { 7245 case RII_LUII: 7246 if (oii) { 7247 if (oii[0] == '0' && oii[1] == 'x') 7248 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len); 7249 else 7250 strncpy((uint8_t *)(rii_ptr + 1), oii, len); 7251 } 7252 break; 7253 case RII_LUTII: 7254 if (otii) 7255 strlcpy((uint8_t *)(rii_ptr + 1), otii, len); 7256 break; 7257 case RII_IIS: 7258 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1); 7259 riid_ptr->type = RII_LUII; 7260 scsi_ulto2b(0xffff, riid_ptr->length); 7261 riid_ptr++; 7262 riid_ptr->type = RII_LUTII; 7263 scsi_ulto2b(0xffff, riid_ptr->length); 7264 } 7265 scsi_ulto2b(len, rii_ptr->length); 7266 7267 ctl_set_success(ctsio); 7268 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7269 ctsio->be_move_done = ctl_config_move_done; 7270 ctl_datamove((union ctl_io *)ctsio); 7271 return(retval); 7272 } 7273 7274 int 7275 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7276 { 7277 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7278 struct ctl_lun *lun = CTL_LUN(ctsio); 7279 struct scsi_maintenance_in *cdb; 7280 int retval; 7281 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7282 int num_ha_groups, num_target_ports, shared_group; 7283 struct ctl_port *port; 7284 struct scsi_target_group_data *rtg_ptr; 7285 struct scsi_target_group_data_extended *rtg_ext_ptr; 7286 struct scsi_target_port_group_descriptor *tpg_desc; 7287 7288 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7289 7290 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7291 retval = CTL_RETVAL_COMPLETE; 7292 7293 switch (cdb->byte2 & STG_PDF_MASK) { 7294 case STG_PDF_LENGTH: 7295 ext = 0; 7296 break; 7297 case STG_PDF_EXTENDED: 7298 ext = 1; 7299 break; 7300 default: 7301 ctl_set_invalid_field(/*ctsio*/ ctsio, 7302 /*sks_valid*/ 1, 7303 /*command*/ 1, 7304 /*field*/ 2, 7305 /*bit_valid*/ 1, 7306 /*bit*/ 5); 7307 ctl_done((union ctl_io *)ctsio); 7308 return(retval); 7309 } 7310 7311 num_target_ports = 0; 7312 shared_group = (softc->is_single != 0); 7313 mtx_lock(&softc->ctl_lock); 7314 STAILQ_FOREACH(port, &softc->port_list, links) { 7315 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7316 continue; 7317 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7318 continue; 7319 num_target_ports++; 7320 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7321 shared_group = 1; 7322 } 7323 mtx_unlock(&softc->ctl_lock); 7324 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7325 7326 if (ext) 7327 total_len = sizeof(struct scsi_target_group_data_extended); 7328 else 7329 total_len = sizeof(struct scsi_target_group_data); 7330 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7331 (shared_group + num_ha_groups) + 7332 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7333 7334 alloc_len = scsi_4btoul(cdb->length); 7335 7336 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7337 ctsio->kern_sg_entries = 0; 7338 ctsio->kern_rel_offset = 0; 7339 ctsio->kern_data_len = min(total_len, alloc_len); 7340 ctsio->kern_total_len = ctsio->kern_data_len; 7341 7342 if (ext) { 7343 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7344 ctsio->kern_data_ptr; 7345 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7346 rtg_ext_ptr->format_type = 0x10; 7347 rtg_ext_ptr->implicit_transition_time = 0; 7348 tpg_desc = &rtg_ext_ptr->groups[0]; 7349 } else { 7350 rtg_ptr = (struct scsi_target_group_data *) 7351 ctsio->kern_data_ptr; 7352 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7353 tpg_desc = &rtg_ptr->groups[0]; 7354 } 7355 7356 mtx_lock(&softc->ctl_lock); 7357 pg = softc->port_min / softc->port_cnt; 7358 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7359 /* Some shelf is known to be primary. */ 7360 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7361 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7362 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7363 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7364 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7365 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7366 else 7367 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7368 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7369 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7370 } else { 7371 ts = os; 7372 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7373 } 7374 } else { 7375 /* No known primary shelf. */ 7376 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7377 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7378 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7379 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7380 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7381 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7382 } else { 7383 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7384 } 7385 } 7386 if (shared_group) { 7387 tpg_desc->pref_state = ts; 7388 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7389 TPG_U_SUP | TPG_T_SUP; 7390 scsi_ulto2b(1, tpg_desc->target_port_group); 7391 tpg_desc->status = TPG_IMPLICIT; 7392 pc = 0; 7393 STAILQ_FOREACH(port, &softc->port_list, links) { 7394 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7395 continue; 7396 if (!softc->is_single && 7397 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7398 continue; 7399 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7400 continue; 7401 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7402 relative_target_port_identifier); 7403 pc++; 7404 } 7405 tpg_desc->target_port_count = pc; 7406 tpg_desc = (struct scsi_target_port_group_descriptor *) 7407 &tpg_desc->descriptors[pc]; 7408 } 7409 for (g = 0; g < num_ha_groups; g++) { 7410 tpg_desc->pref_state = (g == pg) ? ts : os; 7411 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7412 TPG_U_SUP | TPG_T_SUP; 7413 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7414 tpg_desc->status = TPG_IMPLICIT; 7415 pc = 0; 7416 STAILQ_FOREACH(port, &softc->port_list, links) { 7417 if (port->targ_port < g * softc->port_cnt || 7418 port->targ_port >= (g + 1) * softc->port_cnt) 7419 continue; 7420 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7421 continue; 7422 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7423 continue; 7424 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7425 continue; 7426 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7427 relative_target_port_identifier); 7428 pc++; 7429 } 7430 tpg_desc->target_port_count = pc; 7431 tpg_desc = (struct scsi_target_port_group_descriptor *) 7432 &tpg_desc->descriptors[pc]; 7433 } 7434 mtx_unlock(&softc->ctl_lock); 7435 7436 ctl_set_success(ctsio); 7437 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7438 ctsio->be_move_done = ctl_config_move_done; 7439 ctl_datamove((union ctl_io *)ctsio); 7440 return(retval); 7441 } 7442 7443 int 7444 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7445 { 7446 struct ctl_lun *lun = CTL_LUN(ctsio); 7447 struct scsi_report_supported_opcodes *cdb; 7448 const struct ctl_cmd_entry *entry, *sentry; 7449 struct scsi_report_supported_opcodes_all *all; 7450 struct scsi_report_supported_opcodes_descr *descr; 7451 struct scsi_report_supported_opcodes_one *one; 7452 int retval; 7453 int alloc_len, total_len; 7454 int opcode, service_action, i, j, num; 7455 7456 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7457 7458 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7459 retval = CTL_RETVAL_COMPLETE; 7460 7461 opcode = cdb->requested_opcode; 7462 service_action = scsi_2btoul(cdb->requested_service_action); 7463 switch (cdb->options & RSO_OPTIONS_MASK) { 7464 case RSO_OPTIONS_ALL: 7465 num = 0; 7466 for (i = 0; i < 256; i++) { 7467 entry = &ctl_cmd_table[i]; 7468 if (entry->flags & CTL_CMD_FLAG_SA5) { 7469 for (j = 0; j < 32; j++) { 7470 sentry = &((const struct ctl_cmd_entry *) 7471 entry->execute)[j]; 7472 if (ctl_cmd_applicable( 7473 lun->be_lun->lun_type, sentry)) 7474 num++; 7475 } 7476 } else { 7477 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7478 entry)) 7479 num++; 7480 } 7481 } 7482 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7483 num * sizeof(struct scsi_report_supported_opcodes_descr); 7484 break; 7485 case RSO_OPTIONS_OC: 7486 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7487 ctl_set_invalid_field(/*ctsio*/ ctsio, 7488 /*sks_valid*/ 1, 7489 /*command*/ 1, 7490 /*field*/ 2, 7491 /*bit_valid*/ 1, 7492 /*bit*/ 2); 7493 ctl_done((union ctl_io *)ctsio); 7494 return (CTL_RETVAL_COMPLETE); 7495 } 7496 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7497 break; 7498 case RSO_OPTIONS_OC_SA: 7499 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7500 service_action >= 32) { 7501 ctl_set_invalid_field(/*ctsio*/ ctsio, 7502 /*sks_valid*/ 1, 7503 /*command*/ 1, 7504 /*field*/ 2, 7505 /*bit_valid*/ 1, 7506 /*bit*/ 2); 7507 ctl_done((union ctl_io *)ctsio); 7508 return (CTL_RETVAL_COMPLETE); 7509 } 7510 /* FALLTHROUGH */ 7511 case RSO_OPTIONS_OC_ASA: 7512 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7513 break; 7514 default: 7515 ctl_set_invalid_field(/*ctsio*/ ctsio, 7516 /*sks_valid*/ 1, 7517 /*command*/ 1, 7518 /*field*/ 2, 7519 /*bit_valid*/ 1, 7520 /*bit*/ 2); 7521 ctl_done((union ctl_io *)ctsio); 7522 return (CTL_RETVAL_COMPLETE); 7523 } 7524 7525 alloc_len = scsi_4btoul(cdb->length); 7526 7527 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7528 ctsio->kern_sg_entries = 0; 7529 ctsio->kern_rel_offset = 0; 7530 ctsio->kern_data_len = min(total_len, alloc_len); 7531 ctsio->kern_total_len = ctsio->kern_data_len; 7532 7533 switch (cdb->options & RSO_OPTIONS_MASK) { 7534 case RSO_OPTIONS_ALL: 7535 all = (struct scsi_report_supported_opcodes_all *) 7536 ctsio->kern_data_ptr; 7537 num = 0; 7538 for (i = 0; i < 256; i++) { 7539 entry = &ctl_cmd_table[i]; 7540 if (entry->flags & CTL_CMD_FLAG_SA5) { 7541 for (j = 0; j < 32; j++) { 7542 sentry = &((const struct ctl_cmd_entry *) 7543 entry->execute)[j]; 7544 if (!ctl_cmd_applicable( 7545 lun->be_lun->lun_type, sentry)) 7546 continue; 7547 descr = &all->descr[num++]; 7548 descr->opcode = i; 7549 scsi_ulto2b(j, descr->service_action); 7550 descr->flags = RSO_SERVACTV; 7551 scsi_ulto2b(sentry->length, 7552 descr->cdb_length); 7553 } 7554 } else { 7555 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7556 entry)) 7557 continue; 7558 descr = &all->descr[num++]; 7559 descr->opcode = i; 7560 scsi_ulto2b(0, descr->service_action); 7561 descr->flags = 0; 7562 scsi_ulto2b(entry->length, descr->cdb_length); 7563 } 7564 } 7565 scsi_ulto4b( 7566 num * sizeof(struct scsi_report_supported_opcodes_descr), 7567 all->length); 7568 break; 7569 case RSO_OPTIONS_OC: 7570 one = (struct scsi_report_supported_opcodes_one *) 7571 ctsio->kern_data_ptr; 7572 entry = &ctl_cmd_table[opcode]; 7573 goto fill_one; 7574 case RSO_OPTIONS_OC_SA: 7575 one = (struct scsi_report_supported_opcodes_one *) 7576 ctsio->kern_data_ptr; 7577 entry = &ctl_cmd_table[opcode]; 7578 entry = &((const struct ctl_cmd_entry *) 7579 entry->execute)[service_action]; 7580 fill_one: 7581 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7582 one->support = 3; 7583 scsi_ulto2b(entry->length, one->cdb_length); 7584 one->cdb_usage[0] = opcode; 7585 memcpy(&one->cdb_usage[1], entry->usage, 7586 entry->length - 1); 7587 } else 7588 one->support = 1; 7589 break; 7590 case RSO_OPTIONS_OC_ASA: 7591 one = (struct scsi_report_supported_opcodes_one *) 7592 ctsio->kern_data_ptr; 7593 entry = &ctl_cmd_table[opcode]; 7594 if (entry->flags & CTL_CMD_FLAG_SA5) { 7595 entry = &((const struct ctl_cmd_entry *) 7596 entry->execute)[service_action]; 7597 } else if (service_action != 0) { 7598 one->support = 1; 7599 break; 7600 } 7601 goto fill_one; 7602 } 7603 7604 ctl_set_success(ctsio); 7605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7606 ctsio->be_move_done = ctl_config_move_done; 7607 ctl_datamove((union ctl_io *)ctsio); 7608 return(retval); 7609 } 7610 7611 int 7612 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7613 { 7614 struct scsi_report_supported_tmf *cdb; 7615 struct scsi_report_supported_tmf_ext_data *data; 7616 int retval; 7617 int alloc_len, total_len; 7618 7619 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7620 7621 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7622 7623 retval = CTL_RETVAL_COMPLETE; 7624 7625 if (cdb->options & RST_REPD) 7626 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7627 else 7628 total_len = sizeof(struct scsi_report_supported_tmf_data); 7629 alloc_len = scsi_4btoul(cdb->length); 7630 7631 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7632 ctsio->kern_sg_entries = 0; 7633 ctsio->kern_rel_offset = 0; 7634 ctsio->kern_data_len = min(total_len, alloc_len); 7635 ctsio->kern_total_len = ctsio->kern_data_len; 7636 7637 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7638 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7639 RST_TRS; 7640 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7641 data->length = total_len - 4; 7642 7643 ctl_set_success(ctsio); 7644 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7645 ctsio->be_move_done = ctl_config_move_done; 7646 ctl_datamove((union ctl_io *)ctsio); 7647 return (retval); 7648 } 7649 7650 int 7651 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7652 { 7653 struct scsi_report_timestamp *cdb; 7654 struct scsi_report_timestamp_data *data; 7655 struct timeval tv; 7656 int64_t timestamp; 7657 int retval; 7658 int alloc_len, total_len; 7659 7660 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7661 7662 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7663 7664 retval = CTL_RETVAL_COMPLETE; 7665 7666 total_len = sizeof(struct scsi_report_timestamp_data); 7667 alloc_len = scsi_4btoul(cdb->length); 7668 7669 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7670 ctsio->kern_sg_entries = 0; 7671 ctsio->kern_rel_offset = 0; 7672 ctsio->kern_data_len = min(total_len, alloc_len); 7673 ctsio->kern_total_len = ctsio->kern_data_len; 7674 7675 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7676 scsi_ulto2b(sizeof(*data) - 2, data->length); 7677 data->origin = RTS_ORIG_OUTSIDE; 7678 getmicrotime(&tv); 7679 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7680 scsi_ulto4b(timestamp >> 16, data->timestamp); 7681 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7682 7683 ctl_set_success(ctsio); 7684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7685 ctsio->be_move_done = ctl_config_move_done; 7686 ctl_datamove((union ctl_io *)ctsio); 7687 return (retval); 7688 } 7689 7690 int 7691 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7692 { 7693 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7694 struct ctl_lun *lun = CTL_LUN(ctsio); 7695 struct scsi_per_res_in *cdb; 7696 int alloc_len, total_len = 0; 7697 /* struct scsi_per_res_in_rsrv in_data; */ 7698 uint64_t key; 7699 7700 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7701 7702 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7703 7704 alloc_len = scsi_2btoul(cdb->length); 7705 7706 retry: 7707 mtx_lock(&lun->lun_lock); 7708 switch (cdb->action) { 7709 case SPRI_RK: /* read keys */ 7710 total_len = sizeof(struct scsi_per_res_in_keys) + 7711 lun->pr_key_count * 7712 sizeof(struct scsi_per_res_key); 7713 break; 7714 case SPRI_RR: /* read reservation */ 7715 if (lun->flags & CTL_LUN_PR_RESERVED) 7716 total_len = sizeof(struct scsi_per_res_in_rsrv); 7717 else 7718 total_len = sizeof(struct scsi_per_res_in_header); 7719 break; 7720 case SPRI_RC: /* report capabilities */ 7721 total_len = sizeof(struct scsi_per_res_cap); 7722 break; 7723 case SPRI_RS: /* read full status */ 7724 total_len = sizeof(struct scsi_per_res_in_header) + 7725 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7726 lun->pr_key_count; 7727 break; 7728 default: 7729 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7730 } 7731 mtx_unlock(&lun->lun_lock); 7732 7733 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7734 ctsio->kern_rel_offset = 0; 7735 ctsio->kern_sg_entries = 0; 7736 ctsio->kern_data_len = min(total_len, alloc_len); 7737 ctsio->kern_total_len = ctsio->kern_data_len; 7738 7739 mtx_lock(&lun->lun_lock); 7740 switch (cdb->action) { 7741 case SPRI_RK: { // read keys 7742 struct scsi_per_res_in_keys *res_keys; 7743 int i, key_count; 7744 7745 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7746 7747 /* 7748 * We had to drop the lock to allocate our buffer, which 7749 * leaves time for someone to come in with another 7750 * persistent reservation. (That is unlikely, though, 7751 * since this should be the only persistent reservation 7752 * command active right now.) 7753 */ 7754 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7755 (lun->pr_key_count * 7756 sizeof(struct scsi_per_res_key)))){ 7757 mtx_unlock(&lun->lun_lock); 7758 free(ctsio->kern_data_ptr, M_CTL); 7759 printf("%s: reservation length changed, retrying\n", 7760 __func__); 7761 goto retry; 7762 } 7763 7764 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7765 7766 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7767 lun->pr_key_count, res_keys->header.length); 7768 7769 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7770 if ((key = ctl_get_prkey(lun, i)) == 0) 7771 continue; 7772 7773 /* 7774 * We used lun->pr_key_count to calculate the 7775 * size to allocate. If it turns out the number of 7776 * initiators with the registered flag set is 7777 * larger than that (i.e. they haven't been kept in 7778 * sync), we've got a problem. 7779 */ 7780 if (key_count >= lun->pr_key_count) { 7781 key_count++; 7782 continue; 7783 } 7784 scsi_u64to8b(key, res_keys->keys[key_count].key); 7785 key_count++; 7786 } 7787 break; 7788 } 7789 case SPRI_RR: { // read reservation 7790 struct scsi_per_res_in_rsrv *res; 7791 int tmp_len, header_only; 7792 7793 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7794 7795 scsi_ulto4b(lun->pr_generation, res->header.generation); 7796 7797 if (lun->flags & CTL_LUN_PR_RESERVED) 7798 { 7799 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7800 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7801 res->header.length); 7802 header_only = 0; 7803 } else { 7804 tmp_len = sizeof(struct scsi_per_res_in_header); 7805 scsi_ulto4b(0, res->header.length); 7806 header_only = 1; 7807 } 7808 7809 /* 7810 * We had to drop the lock to allocate our buffer, which 7811 * leaves time for someone to come in with another 7812 * persistent reservation. (That is unlikely, though, 7813 * since this should be the only persistent reservation 7814 * command active right now.) 7815 */ 7816 if (tmp_len != total_len) { 7817 mtx_unlock(&lun->lun_lock); 7818 free(ctsio->kern_data_ptr, M_CTL); 7819 printf("%s: reservation status changed, retrying\n", 7820 __func__); 7821 goto retry; 7822 } 7823 7824 /* 7825 * No reservation held, so we're done. 7826 */ 7827 if (header_only != 0) 7828 break; 7829 7830 /* 7831 * If the registration is an All Registrants type, the key 7832 * is 0, since it doesn't really matter. 7833 */ 7834 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7835 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7836 res->data.reservation); 7837 } 7838 res->data.scopetype = lun->pr_res_type; 7839 break; 7840 } 7841 case SPRI_RC: //report capabilities 7842 { 7843 struct scsi_per_res_cap *res_cap; 7844 uint16_t type_mask; 7845 7846 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7847 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7848 res_cap->flags1 = SPRI_CRH; 7849 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7850 type_mask = SPRI_TM_WR_EX_AR | 7851 SPRI_TM_EX_AC_RO | 7852 SPRI_TM_WR_EX_RO | 7853 SPRI_TM_EX_AC | 7854 SPRI_TM_WR_EX | 7855 SPRI_TM_EX_AC_AR; 7856 scsi_ulto2b(type_mask, res_cap->type_mask); 7857 break; 7858 } 7859 case SPRI_RS: { // read full status 7860 struct scsi_per_res_in_full *res_status; 7861 struct scsi_per_res_in_full_desc *res_desc; 7862 struct ctl_port *port; 7863 int i, len; 7864 7865 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7866 7867 /* 7868 * We had to drop the lock to allocate our buffer, which 7869 * leaves time for someone to come in with another 7870 * persistent reservation. (That is unlikely, though, 7871 * since this should be the only persistent reservation 7872 * command active right now.) 7873 */ 7874 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7875 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7876 lun->pr_key_count)){ 7877 mtx_unlock(&lun->lun_lock); 7878 free(ctsio->kern_data_ptr, M_CTL); 7879 printf("%s: reservation length changed, retrying\n", 7880 __func__); 7881 goto retry; 7882 } 7883 7884 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7885 7886 res_desc = &res_status->desc[0]; 7887 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7888 if ((key = ctl_get_prkey(lun, i)) == 0) 7889 continue; 7890 7891 scsi_u64to8b(key, res_desc->res_key.key); 7892 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7893 (lun->pr_res_idx == i || 7894 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7895 res_desc->flags = SPRI_FULL_R_HOLDER; 7896 res_desc->scopetype = lun->pr_res_type; 7897 } 7898 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7899 res_desc->rel_trgt_port_id); 7900 len = 0; 7901 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7902 if (port != NULL) 7903 len = ctl_create_iid(port, 7904 i % CTL_MAX_INIT_PER_PORT, 7905 res_desc->transport_id); 7906 scsi_ulto4b(len, res_desc->additional_length); 7907 res_desc = (struct scsi_per_res_in_full_desc *) 7908 &res_desc->transport_id[len]; 7909 } 7910 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7911 res_status->header.length); 7912 break; 7913 } 7914 default: 7915 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7916 } 7917 mtx_unlock(&lun->lun_lock); 7918 7919 ctl_set_success(ctsio); 7920 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7921 ctsio->be_move_done = ctl_config_move_done; 7922 ctl_datamove((union ctl_io *)ctsio); 7923 return (CTL_RETVAL_COMPLETE); 7924 } 7925 7926 /* 7927 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7928 * it should return. 7929 */ 7930 static int 7931 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7932 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7933 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7934 struct scsi_per_res_out_parms* param) 7935 { 7936 union ctl_ha_msg persis_io; 7937 int i; 7938 7939 mtx_lock(&lun->lun_lock); 7940 if (sa_res_key == 0) { 7941 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7942 /* validate scope and type */ 7943 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7944 SPR_LU_SCOPE) { 7945 mtx_unlock(&lun->lun_lock); 7946 ctl_set_invalid_field(/*ctsio*/ ctsio, 7947 /*sks_valid*/ 1, 7948 /*command*/ 1, 7949 /*field*/ 2, 7950 /*bit_valid*/ 1, 7951 /*bit*/ 4); 7952 ctl_done((union ctl_io *)ctsio); 7953 return (1); 7954 } 7955 7956 if (type>8 || type==2 || type==4 || type==0) { 7957 mtx_unlock(&lun->lun_lock); 7958 ctl_set_invalid_field(/*ctsio*/ ctsio, 7959 /*sks_valid*/ 1, 7960 /*command*/ 1, 7961 /*field*/ 2, 7962 /*bit_valid*/ 1, 7963 /*bit*/ 0); 7964 ctl_done((union ctl_io *)ctsio); 7965 return (1); 7966 } 7967 7968 /* 7969 * Unregister everybody else and build UA for 7970 * them 7971 */ 7972 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7973 if (i == residx || ctl_get_prkey(lun, i) == 0) 7974 continue; 7975 7976 ctl_clr_prkey(lun, i); 7977 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7978 } 7979 lun->pr_key_count = 1; 7980 lun->pr_res_type = type; 7981 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7982 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7983 lun->pr_res_idx = residx; 7984 lun->pr_generation++; 7985 mtx_unlock(&lun->lun_lock); 7986 7987 /* send msg to other side */ 7988 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7989 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7990 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7991 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7992 persis_io.pr.pr_info.res_type = type; 7993 memcpy(persis_io.pr.pr_info.sa_res_key, 7994 param->serv_act_res_key, 7995 sizeof(param->serv_act_res_key)); 7996 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7997 sizeof(persis_io.pr), M_WAITOK); 7998 } else { 7999 /* not all registrants */ 8000 mtx_unlock(&lun->lun_lock); 8001 free(ctsio->kern_data_ptr, M_CTL); 8002 ctl_set_invalid_field(ctsio, 8003 /*sks_valid*/ 1, 8004 /*command*/ 0, 8005 /*field*/ 8, 8006 /*bit_valid*/ 0, 8007 /*bit*/ 0); 8008 ctl_done((union ctl_io *)ctsio); 8009 return (1); 8010 } 8011 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8012 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8013 int found = 0; 8014 8015 if (res_key == sa_res_key) { 8016 /* special case */ 8017 /* 8018 * The spec implies this is not good but doesn't 8019 * say what to do. There are two choices either 8020 * generate a res conflict or check condition 8021 * with illegal field in parameter data. Since 8022 * that is what is done when the sa_res_key is 8023 * zero I'll take that approach since this has 8024 * to do with the sa_res_key. 8025 */ 8026 mtx_unlock(&lun->lun_lock); 8027 free(ctsio->kern_data_ptr, M_CTL); 8028 ctl_set_invalid_field(ctsio, 8029 /*sks_valid*/ 1, 8030 /*command*/ 0, 8031 /*field*/ 8, 8032 /*bit_valid*/ 0, 8033 /*bit*/ 0); 8034 ctl_done((union ctl_io *)ctsio); 8035 return (1); 8036 } 8037 8038 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8039 if (ctl_get_prkey(lun, i) != sa_res_key) 8040 continue; 8041 8042 found = 1; 8043 ctl_clr_prkey(lun, i); 8044 lun->pr_key_count--; 8045 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8046 } 8047 if (!found) { 8048 mtx_unlock(&lun->lun_lock); 8049 free(ctsio->kern_data_ptr, M_CTL); 8050 ctl_set_reservation_conflict(ctsio); 8051 ctl_done((union ctl_io *)ctsio); 8052 return (CTL_RETVAL_COMPLETE); 8053 } 8054 lun->pr_generation++; 8055 mtx_unlock(&lun->lun_lock); 8056 8057 /* send msg to other side */ 8058 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8059 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8060 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8061 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8062 persis_io.pr.pr_info.res_type = type; 8063 memcpy(persis_io.pr.pr_info.sa_res_key, 8064 param->serv_act_res_key, 8065 sizeof(param->serv_act_res_key)); 8066 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8067 sizeof(persis_io.pr), M_WAITOK); 8068 } else { 8069 /* Reserved but not all registrants */ 8070 /* sa_res_key is res holder */ 8071 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8072 /* validate scope and type */ 8073 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8074 SPR_LU_SCOPE) { 8075 mtx_unlock(&lun->lun_lock); 8076 ctl_set_invalid_field(/*ctsio*/ ctsio, 8077 /*sks_valid*/ 1, 8078 /*command*/ 1, 8079 /*field*/ 2, 8080 /*bit_valid*/ 1, 8081 /*bit*/ 4); 8082 ctl_done((union ctl_io *)ctsio); 8083 return (1); 8084 } 8085 8086 if (type>8 || type==2 || type==4 || type==0) { 8087 mtx_unlock(&lun->lun_lock); 8088 ctl_set_invalid_field(/*ctsio*/ ctsio, 8089 /*sks_valid*/ 1, 8090 /*command*/ 1, 8091 /*field*/ 2, 8092 /*bit_valid*/ 1, 8093 /*bit*/ 0); 8094 ctl_done((union ctl_io *)ctsio); 8095 return (1); 8096 } 8097 8098 /* 8099 * Do the following: 8100 * if sa_res_key != res_key remove all 8101 * registrants w/sa_res_key and generate UA 8102 * for these registrants(Registrations 8103 * Preempted) if it wasn't an exclusive 8104 * reservation generate UA(Reservations 8105 * Preempted) for all other registered nexuses 8106 * if the type has changed. Establish the new 8107 * reservation and holder. If res_key and 8108 * sa_res_key are the same do the above 8109 * except don't unregister the res holder. 8110 */ 8111 8112 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8113 if (i == residx || ctl_get_prkey(lun, i) == 0) 8114 continue; 8115 8116 if (sa_res_key == ctl_get_prkey(lun, i)) { 8117 ctl_clr_prkey(lun, i); 8118 lun->pr_key_count--; 8119 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8120 } else if (type != lun->pr_res_type && 8121 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8122 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8123 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8124 } 8125 } 8126 lun->pr_res_type = type; 8127 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8128 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8129 lun->pr_res_idx = residx; 8130 else 8131 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8132 lun->pr_generation++; 8133 mtx_unlock(&lun->lun_lock); 8134 8135 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8136 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8137 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8138 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8139 persis_io.pr.pr_info.res_type = type; 8140 memcpy(persis_io.pr.pr_info.sa_res_key, 8141 param->serv_act_res_key, 8142 sizeof(param->serv_act_res_key)); 8143 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8144 sizeof(persis_io.pr), M_WAITOK); 8145 } else { 8146 /* 8147 * sa_res_key is not the res holder just 8148 * remove registrants 8149 */ 8150 int found=0; 8151 8152 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8153 if (sa_res_key != ctl_get_prkey(lun, i)) 8154 continue; 8155 8156 found = 1; 8157 ctl_clr_prkey(lun, i); 8158 lun->pr_key_count--; 8159 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8160 } 8161 8162 if (!found) { 8163 mtx_unlock(&lun->lun_lock); 8164 free(ctsio->kern_data_ptr, M_CTL); 8165 ctl_set_reservation_conflict(ctsio); 8166 ctl_done((union ctl_io *)ctsio); 8167 return (1); 8168 } 8169 lun->pr_generation++; 8170 mtx_unlock(&lun->lun_lock); 8171 8172 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8173 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8174 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8175 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8176 persis_io.pr.pr_info.res_type = type; 8177 memcpy(persis_io.pr.pr_info.sa_res_key, 8178 param->serv_act_res_key, 8179 sizeof(param->serv_act_res_key)); 8180 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8181 sizeof(persis_io.pr), M_WAITOK); 8182 } 8183 } 8184 return (0); 8185 } 8186 8187 static void 8188 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8189 { 8190 uint64_t sa_res_key; 8191 int i; 8192 8193 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8194 8195 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8196 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8197 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8198 if (sa_res_key == 0) { 8199 /* 8200 * Unregister everybody else and build UA for 8201 * them 8202 */ 8203 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8204 if (i == msg->pr.pr_info.residx || 8205 ctl_get_prkey(lun, i) == 0) 8206 continue; 8207 8208 ctl_clr_prkey(lun, i); 8209 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8210 } 8211 8212 lun->pr_key_count = 1; 8213 lun->pr_res_type = msg->pr.pr_info.res_type; 8214 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8215 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8216 lun->pr_res_idx = msg->pr.pr_info.residx; 8217 } else { 8218 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8219 if (sa_res_key == ctl_get_prkey(lun, i)) 8220 continue; 8221 8222 ctl_clr_prkey(lun, i); 8223 lun->pr_key_count--; 8224 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8225 } 8226 } 8227 } else { 8228 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8229 if (i == msg->pr.pr_info.residx || 8230 ctl_get_prkey(lun, i) == 0) 8231 continue; 8232 8233 if (sa_res_key == ctl_get_prkey(lun, i)) { 8234 ctl_clr_prkey(lun, i); 8235 lun->pr_key_count--; 8236 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8237 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8238 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8239 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8240 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8241 } 8242 } 8243 lun->pr_res_type = msg->pr.pr_info.res_type; 8244 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8245 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8246 lun->pr_res_idx = msg->pr.pr_info.residx; 8247 else 8248 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8249 } 8250 lun->pr_generation++; 8251 8252 } 8253 8254 int 8255 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8256 { 8257 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8258 struct ctl_lun *lun = CTL_LUN(ctsio); 8259 int retval; 8260 u_int32_t param_len; 8261 struct scsi_per_res_out *cdb; 8262 struct scsi_per_res_out_parms* param; 8263 uint32_t residx; 8264 uint64_t res_key, sa_res_key, key; 8265 uint8_t type; 8266 union ctl_ha_msg persis_io; 8267 int i; 8268 8269 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8270 8271 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8272 retval = CTL_RETVAL_COMPLETE; 8273 8274 /* 8275 * We only support whole-LUN scope. The scope & type are ignored for 8276 * register, register and ignore existing key and clear. 8277 * We sometimes ignore scope and type on preempts too!! 8278 * Verify reservation type here as well. 8279 */ 8280 type = cdb->scope_type & SPR_TYPE_MASK; 8281 if ((cdb->action == SPRO_RESERVE) 8282 || (cdb->action == SPRO_RELEASE)) { 8283 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8284 ctl_set_invalid_field(/*ctsio*/ ctsio, 8285 /*sks_valid*/ 1, 8286 /*command*/ 1, 8287 /*field*/ 2, 8288 /*bit_valid*/ 1, 8289 /*bit*/ 4); 8290 ctl_done((union ctl_io *)ctsio); 8291 return (CTL_RETVAL_COMPLETE); 8292 } 8293 8294 if (type>8 || type==2 || type==4 || type==0) { 8295 ctl_set_invalid_field(/*ctsio*/ ctsio, 8296 /*sks_valid*/ 1, 8297 /*command*/ 1, 8298 /*field*/ 2, 8299 /*bit_valid*/ 1, 8300 /*bit*/ 0); 8301 ctl_done((union ctl_io *)ctsio); 8302 return (CTL_RETVAL_COMPLETE); 8303 } 8304 } 8305 8306 param_len = scsi_4btoul(cdb->length); 8307 8308 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8309 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8310 ctsio->kern_data_len = param_len; 8311 ctsio->kern_total_len = param_len; 8312 ctsio->kern_rel_offset = 0; 8313 ctsio->kern_sg_entries = 0; 8314 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8315 ctsio->be_move_done = ctl_config_move_done; 8316 ctl_datamove((union ctl_io *)ctsio); 8317 8318 return (CTL_RETVAL_COMPLETE); 8319 } 8320 8321 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8322 8323 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8324 res_key = scsi_8btou64(param->res_key.key); 8325 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8326 8327 /* 8328 * Validate the reservation key here except for SPRO_REG_IGNO 8329 * This must be done for all other service actions 8330 */ 8331 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8332 mtx_lock(&lun->lun_lock); 8333 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8334 if (res_key != key) { 8335 /* 8336 * The current key passed in doesn't match 8337 * the one the initiator previously 8338 * registered. 8339 */ 8340 mtx_unlock(&lun->lun_lock); 8341 free(ctsio->kern_data_ptr, M_CTL); 8342 ctl_set_reservation_conflict(ctsio); 8343 ctl_done((union ctl_io *)ctsio); 8344 return (CTL_RETVAL_COMPLETE); 8345 } 8346 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8347 /* 8348 * We are not registered 8349 */ 8350 mtx_unlock(&lun->lun_lock); 8351 free(ctsio->kern_data_ptr, M_CTL); 8352 ctl_set_reservation_conflict(ctsio); 8353 ctl_done((union ctl_io *)ctsio); 8354 return (CTL_RETVAL_COMPLETE); 8355 } else if (res_key != 0) { 8356 /* 8357 * We are not registered and trying to register but 8358 * the register key isn't zero. 8359 */ 8360 mtx_unlock(&lun->lun_lock); 8361 free(ctsio->kern_data_ptr, M_CTL); 8362 ctl_set_reservation_conflict(ctsio); 8363 ctl_done((union ctl_io *)ctsio); 8364 return (CTL_RETVAL_COMPLETE); 8365 } 8366 mtx_unlock(&lun->lun_lock); 8367 } 8368 8369 switch (cdb->action & SPRO_ACTION_MASK) { 8370 case SPRO_REGISTER: 8371 case SPRO_REG_IGNO: { 8372 /* 8373 * We don't support any of these options, as we report in 8374 * the read capabilities request (see 8375 * ctl_persistent_reserve_in(), above). 8376 */ 8377 if ((param->flags & SPR_SPEC_I_PT) 8378 || (param->flags & SPR_ALL_TG_PT) 8379 || (param->flags & SPR_APTPL)) { 8380 int bit_ptr; 8381 8382 if (param->flags & SPR_APTPL) 8383 bit_ptr = 0; 8384 else if (param->flags & SPR_ALL_TG_PT) 8385 bit_ptr = 2; 8386 else /* SPR_SPEC_I_PT */ 8387 bit_ptr = 3; 8388 8389 free(ctsio->kern_data_ptr, M_CTL); 8390 ctl_set_invalid_field(ctsio, 8391 /*sks_valid*/ 1, 8392 /*command*/ 0, 8393 /*field*/ 20, 8394 /*bit_valid*/ 1, 8395 /*bit*/ bit_ptr); 8396 ctl_done((union ctl_io *)ctsio); 8397 return (CTL_RETVAL_COMPLETE); 8398 } 8399 8400 mtx_lock(&lun->lun_lock); 8401 8402 /* 8403 * The initiator wants to clear the 8404 * key/unregister. 8405 */ 8406 if (sa_res_key == 0) { 8407 if ((res_key == 0 8408 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8409 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8410 && ctl_get_prkey(lun, residx) == 0)) { 8411 mtx_unlock(&lun->lun_lock); 8412 goto done; 8413 } 8414 8415 ctl_clr_prkey(lun, residx); 8416 lun->pr_key_count--; 8417 8418 if (residx == lun->pr_res_idx) { 8419 lun->flags &= ~CTL_LUN_PR_RESERVED; 8420 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8421 8422 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8423 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8424 lun->pr_key_count) { 8425 /* 8426 * If the reservation is a registrants 8427 * only type we need to generate a UA 8428 * for other registered inits. The 8429 * sense code should be RESERVATIONS 8430 * RELEASED 8431 */ 8432 8433 for (i = softc->init_min; i < softc->init_max; i++){ 8434 if (ctl_get_prkey(lun, i) == 0) 8435 continue; 8436 ctl_est_ua(lun, i, 8437 CTL_UA_RES_RELEASE); 8438 } 8439 } 8440 lun->pr_res_type = 0; 8441 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8442 if (lun->pr_key_count==0) { 8443 lun->flags &= ~CTL_LUN_PR_RESERVED; 8444 lun->pr_res_type = 0; 8445 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8446 } 8447 } 8448 lun->pr_generation++; 8449 mtx_unlock(&lun->lun_lock); 8450 8451 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8452 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8453 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8454 persis_io.pr.pr_info.residx = residx; 8455 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8456 sizeof(persis_io.pr), M_WAITOK); 8457 } else /* sa_res_key != 0 */ { 8458 /* 8459 * If we aren't registered currently then increment 8460 * the key count and set the registered flag. 8461 */ 8462 ctl_alloc_prkey(lun, residx); 8463 if (ctl_get_prkey(lun, residx) == 0) 8464 lun->pr_key_count++; 8465 ctl_set_prkey(lun, residx, sa_res_key); 8466 lun->pr_generation++; 8467 mtx_unlock(&lun->lun_lock); 8468 8469 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8470 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8471 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8472 persis_io.pr.pr_info.residx = residx; 8473 memcpy(persis_io.pr.pr_info.sa_res_key, 8474 param->serv_act_res_key, 8475 sizeof(param->serv_act_res_key)); 8476 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8477 sizeof(persis_io.pr), M_WAITOK); 8478 } 8479 8480 break; 8481 } 8482 case SPRO_RESERVE: 8483 mtx_lock(&lun->lun_lock); 8484 if (lun->flags & CTL_LUN_PR_RESERVED) { 8485 /* 8486 * if this isn't the reservation holder and it's 8487 * not a "all registrants" type or if the type is 8488 * different then we have a conflict 8489 */ 8490 if ((lun->pr_res_idx != residx 8491 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8492 || lun->pr_res_type != type) { 8493 mtx_unlock(&lun->lun_lock); 8494 free(ctsio->kern_data_ptr, M_CTL); 8495 ctl_set_reservation_conflict(ctsio); 8496 ctl_done((union ctl_io *)ctsio); 8497 return (CTL_RETVAL_COMPLETE); 8498 } 8499 mtx_unlock(&lun->lun_lock); 8500 } else /* create a reservation */ { 8501 /* 8502 * If it's not an "all registrants" type record 8503 * reservation holder 8504 */ 8505 if (type != SPR_TYPE_WR_EX_AR 8506 && type != SPR_TYPE_EX_AC_AR) 8507 lun->pr_res_idx = residx; /* Res holder */ 8508 else 8509 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8510 8511 lun->flags |= CTL_LUN_PR_RESERVED; 8512 lun->pr_res_type = type; 8513 8514 mtx_unlock(&lun->lun_lock); 8515 8516 /* send msg to other side */ 8517 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8518 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8519 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8520 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8521 persis_io.pr.pr_info.res_type = type; 8522 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8523 sizeof(persis_io.pr), M_WAITOK); 8524 } 8525 break; 8526 8527 case SPRO_RELEASE: 8528 mtx_lock(&lun->lun_lock); 8529 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8530 /* No reservation exists return good status */ 8531 mtx_unlock(&lun->lun_lock); 8532 goto done; 8533 } 8534 /* 8535 * Is this nexus a reservation holder? 8536 */ 8537 if (lun->pr_res_idx != residx 8538 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8539 /* 8540 * not a res holder return good status but 8541 * do nothing 8542 */ 8543 mtx_unlock(&lun->lun_lock); 8544 goto done; 8545 } 8546 8547 if (lun->pr_res_type != type) { 8548 mtx_unlock(&lun->lun_lock); 8549 free(ctsio->kern_data_ptr, M_CTL); 8550 ctl_set_illegal_pr_release(ctsio); 8551 ctl_done((union ctl_io *)ctsio); 8552 return (CTL_RETVAL_COMPLETE); 8553 } 8554 8555 /* okay to release */ 8556 lun->flags &= ~CTL_LUN_PR_RESERVED; 8557 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8558 lun->pr_res_type = 0; 8559 8560 /* 8561 * If this isn't an exclusive access reservation and NUAR 8562 * is not set, generate UA for all other registrants. 8563 */ 8564 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8565 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8566 for (i = softc->init_min; i < softc->init_max; i++) { 8567 if (i == residx || ctl_get_prkey(lun, i) == 0) 8568 continue; 8569 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8570 } 8571 } 8572 mtx_unlock(&lun->lun_lock); 8573 8574 /* Send msg to other side */ 8575 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8576 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8577 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8578 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8579 sizeof(persis_io.pr), M_WAITOK); 8580 break; 8581 8582 case SPRO_CLEAR: 8583 /* send msg to other side */ 8584 8585 mtx_lock(&lun->lun_lock); 8586 lun->flags &= ~CTL_LUN_PR_RESERVED; 8587 lun->pr_res_type = 0; 8588 lun->pr_key_count = 0; 8589 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8590 8591 ctl_clr_prkey(lun, residx); 8592 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8593 if (ctl_get_prkey(lun, i) != 0) { 8594 ctl_clr_prkey(lun, i); 8595 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8596 } 8597 lun->pr_generation++; 8598 mtx_unlock(&lun->lun_lock); 8599 8600 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8601 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8602 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8603 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8604 sizeof(persis_io.pr), M_WAITOK); 8605 break; 8606 8607 case SPRO_PREEMPT: 8608 case SPRO_PRE_ABO: { 8609 int nretval; 8610 8611 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8612 residx, ctsio, cdb, param); 8613 if (nretval != 0) 8614 return (CTL_RETVAL_COMPLETE); 8615 break; 8616 } 8617 default: 8618 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8619 } 8620 8621 done: 8622 free(ctsio->kern_data_ptr, M_CTL); 8623 ctl_set_success(ctsio); 8624 ctl_done((union ctl_io *)ctsio); 8625 8626 return (retval); 8627 } 8628 8629 /* 8630 * This routine is for handling a message from the other SC pertaining to 8631 * persistent reserve out. All the error checking will have been done 8632 * so only performing the action need be done here to keep the two 8633 * in sync. 8634 */ 8635 static void 8636 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8637 { 8638 struct ctl_softc *softc = CTL_SOFTC(io); 8639 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8640 struct ctl_lun *lun; 8641 int i; 8642 uint32_t residx, targ_lun; 8643 8644 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8645 mtx_lock(&softc->ctl_lock); 8646 if (targ_lun >= ctl_max_luns || 8647 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8648 mtx_unlock(&softc->ctl_lock); 8649 return; 8650 } 8651 mtx_lock(&lun->lun_lock); 8652 mtx_unlock(&softc->ctl_lock); 8653 if (lun->flags & CTL_LUN_DISABLED) { 8654 mtx_unlock(&lun->lun_lock); 8655 return; 8656 } 8657 residx = ctl_get_initindex(&msg->hdr.nexus); 8658 switch(msg->pr.pr_info.action) { 8659 case CTL_PR_REG_KEY: 8660 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8661 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8662 lun->pr_key_count++; 8663 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8664 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8665 lun->pr_generation++; 8666 break; 8667 8668 case CTL_PR_UNREG_KEY: 8669 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8670 lun->pr_key_count--; 8671 8672 /* XXX Need to see if the reservation has been released */ 8673 /* if so do we need to generate UA? */ 8674 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8675 lun->flags &= ~CTL_LUN_PR_RESERVED; 8676 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8677 8678 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8679 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8680 lun->pr_key_count) { 8681 /* 8682 * If the reservation is a registrants 8683 * only type we need to generate a UA 8684 * for other registered inits. The 8685 * sense code should be RESERVATIONS 8686 * RELEASED 8687 */ 8688 8689 for (i = softc->init_min; i < softc->init_max; i++) { 8690 if (ctl_get_prkey(lun, i) == 0) 8691 continue; 8692 8693 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8694 } 8695 } 8696 lun->pr_res_type = 0; 8697 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8698 if (lun->pr_key_count==0) { 8699 lun->flags &= ~CTL_LUN_PR_RESERVED; 8700 lun->pr_res_type = 0; 8701 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8702 } 8703 } 8704 lun->pr_generation++; 8705 break; 8706 8707 case CTL_PR_RESERVE: 8708 lun->flags |= CTL_LUN_PR_RESERVED; 8709 lun->pr_res_type = msg->pr.pr_info.res_type; 8710 lun->pr_res_idx = msg->pr.pr_info.residx; 8711 8712 break; 8713 8714 case CTL_PR_RELEASE: 8715 /* 8716 * If this isn't an exclusive access reservation and NUAR 8717 * is not set, generate UA for all other registrants. 8718 */ 8719 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8720 lun->pr_res_type != SPR_TYPE_WR_EX && 8721 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8722 for (i = softc->init_min; i < softc->init_max; i++) { 8723 if (i == residx || ctl_get_prkey(lun, i) == 0) 8724 continue; 8725 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8726 } 8727 } 8728 8729 lun->flags &= ~CTL_LUN_PR_RESERVED; 8730 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8731 lun->pr_res_type = 0; 8732 break; 8733 8734 case CTL_PR_PREEMPT: 8735 ctl_pro_preempt_other(lun, msg); 8736 break; 8737 case CTL_PR_CLEAR: 8738 lun->flags &= ~CTL_LUN_PR_RESERVED; 8739 lun->pr_res_type = 0; 8740 lun->pr_key_count = 0; 8741 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8742 8743 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8744 if (ctl_get_prkey(lun, i) == 0) 8745 continue; 8746 ctl_clr_prkey(lun, i); 8747 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8748 } 8749 lun->pr_generation++; 8750 break; 8751 } 8752 8753 mtx_unlock(&lun->lun_lock); 8754 } 8755 8756 int 8757 ctl_read_write(struct ctl_scsiio *ctsio) 8758 { 8759 struct ctl_lun *lun = CTL_LUN(ctsio); 8760 struct ctl_lba_len_flags *lbalen; 8761 uint64_t lba; 8762 uint32_t num_blocks; 8763 int flags, retval; 8764 int isread; 8765 8766 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8767 8768 flags = 0; 8769 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8770 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8771 switch (ctsio->cdb[0]) { 8772 case READ_6: 8773 case WRITE_6: { 8774 struct scsi_rw_6 *cdb; 8775 8776 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8777 8778 lba = scsi_3btoul(cdb->addr); 8779 /* only 5 bits are valid in the most significant address byte */ 8780 lba &= 0x1fffff; 8781 num_blocks = cdb->length; 8782 /* 8783 * This is correct according to SBC-2. 8784 */ 8785 if (num_blocks == 0) 8786 num_blocks = 256; 8787 break; 8788 } 8789 case READ_10: 8790 case WRITE_10: { 8791 struct scsi_rw_10 *cdb; 8792 8793 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8794 if (cdb->byte2 & SRW10_FUA) 8795 flags |= CTL_LLF_FUA; 8796 if (cdb->byte2 & SRW10_DPO) 8797 flags |= CTL_LLF_DPO; 8798 lba = scsi_4btoul(cdb->addr); 8799 num_blocks = scsi_2btoul(cdb->length); 8800 break; 8801 } 8802 case WRITE_VERIFY_10: { 8803 struct scsi_write_verify_10 *cdb; 8804 8805 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8806 flags |= CTL_LLF_FUA; 8807 if (cdb->byte2 & SWV_DPO) 8808 flags |= CTL_LLF_DPO; 8809 lba = scsi_4btoul(cdb->addr); 8810 num_blocks = scsi_2btoul(cdb->length); 8811 break; 8812 } 8813 case READ_12: 8814 case WRITE_12: { 8815 struct scsi_rw_12 *cdb; 8816 8817 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8818 if (cdb->byte2 & SRW12_FUA) 8819 flags |= CTL_LLF_FUA; 8820 if (cdb->byte2 & SRW12_DPO) 8821 flags |= CTL_LLF_DPO; 8822 lba = scsi_4btoul(cdb->addr); 8823 num_blocks = scsi_4btoul(cdb->length); 8824 break; 8825 } 8826 case WRITE_VERIFY_12: { 8827 struct scsi_write_verify_12 *cdb; 8828 8829 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8830 flags |= CTL_LLF_FUA; 8831 if (cdb->byte2 & SWV_DPO) 8832 flags |= CTL_LLF_DPO; 8833 lba = scsi_4btoul(cdb->addr); 8834 num_blocks = scsi_4btoul(cdb->length); 8835 break; 8836 } 8837 case READ_16: 8838 case WRITE_16: { 8839 struct scsi_rw_16 *cdb; 8840 8841 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8842 if (cdb->byte2 & SRW12_FUA) 8843 flags |= CTL_LLF_FUA; 8844 if (cdb->byte2 & SRW12_DPO) 8845 flags |= CTL_LLF_DPO; 8846 lba = scsi_8btou64(cdb->addr); 8847 num_blocks = scsi_4btoul(cdb->length); 8848 break; 8849 } 8850 case WRITE_ATOMIC_16: { 8851 struct scsi_write_atomic_16 *cdb; 8852 8853 if (lun->be_lun->atomicblock == 0) { 8854 ctl_set_invalid_opcode(ctsio); 8855 ctl_done((union ctl_io *)ctsio); 8856 return (CTL_RETVAL_COMPLETE); 8857 } 8858 8859 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8860 if (cdb->byte2 & SRW12_FUA) 8861 flags |= CTL_LLF_FUA; 8862 if (cdb->byte2 & SRW12_DPO) 8863 flags |= CTL_LLF_DPO; 8864 lba = scsi_8btou64(cdb->addr); 8865 num_blocks = scsi_2btoul(cdb->length); 8866 if (num_blocks > lun->be_lun->atomicblock) { 8867 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8868 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8869 /*bit*/ 0); 8870 ctl_done((union ctl_io *)ctsio); 8871 return (CTL_RETVAL_COMPLETE); 8872 } 8873 break; 8874 } 8875 case WRITE_VERIFY_16: { 8876 struct scsi_write_verify_16 *cdb; 8877 8878 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8879 flags |= CTL_LLF_FUA; 8880 if (cdb->byte2 & SWV_DPO) 8881 flags |= CTL_LLF_DPO; 8882 lba = scsi_8btou64(cdb->addr); 8883 num_blocks = scsi_4btoul(cdb->length); 8884 break; 8885 } 8886 default: 8887 /* 8888 * We got a command we don't support. This shouldn't 8889 * happen, commands should be filtered out above us. 8890 */ 8891 ctl_set_invalid_opcode(ctsio); 8892 ctl_done((union ctl_io *)ctsio); 8893 8894 return (CTL_RETVAL_COMPLETE); 8895 break; /* NOTREACHED */ 8896 } 8897 8898 /* 8899 * The first check is to make sure we're in bounds, the second 8900 * check is to catch wrap-around problems. If the lba + num blocks 8901 * is less than the lba, then we've wrapped around and the block 8902 * range is invalid anyway. 8903 */ 8904 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8905 || ((lba + num_blocks) < lba)) { 8906 ctl_set_lba_out_of_range(ctsio, 8907 MAX(lba, lun->be_lun->maxlba + 1)); 8908 ctl_done((union ctl_io *)ctsio); 8909 return (CTL_RETVAL_COMPLETE); 8910 } 8911 8912 /* 8913 * According to SBC-3, a transfer length of 0 is not an error. 8914 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8915 * translates to 256 blocks for those commands. 8916 */ 8917 if (num_blocks == 0) { 8918 ctl_set_success(ctsio); 8919 ctl_done((union ctl_io *)ctsio); 8920 return (CTL_RETVAL_COMPLETE); 8921 } 8922 8923 /* Set FUA and/or DPO if caches are disabled. */ 8924 if (isread) { 8925 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8926 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8927 } else { 8928 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8929 flags |= CTL_LLF_FUA; 8930 } 8931 8932 lbalen = (struct ctl_lba_len_flags *) 8933 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8934 lbalen->lba = lba; 8935 lbalen->len = num_blocks; 8936 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8937 8938 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8939 ctsio->kern_rel_offset = 0; 8940 8941 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8942 8943 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8944 return (retval); 8945 } 8946 8947 static int 8948 ctl_cnw_cont(union ctl_io *io) 8949 { 8950 struct ctl_lun *lun = CTL_LUN(io); 8951 struct ctl_scsiio *ctsio; 8952 struct ctl_lba_len_flags *lbalen; 8953 int retval; 8954 8955 ctsio = &io->scsiio; 8956 ctsio->io_hdr.status = CTL_STATUS_NONE; 8957 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8958 lbalen = (struct ctl_lba_len_flags *) 8959 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8960 lbalen->flags &= ~CTL_LLF_COMPARE; 8961 lbalen->flags |= CTL_LLF_WRITE; 8962 8963 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8964 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8965 return (retval); 8966 } 8967 8968 int 8969 ctl_cnw(struct ctl_scsiio *ctsio) 8970 { 8971 struct ctl_lun *lun = CTL_LUN(ctsio); 8972 struct ctl_lba_len_flags *lbalen; 8973 uint64_t lba; 8974 uint32_t num_blocks; 8975 int flags, retval; 8976 8977 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8978 8979 flags = 0; 8980 switch (ctsio->cdb[0]) { 8981 case COMPARE_AND_WRITE: { 8982 struct scsi_compare_and_write *cdb; 8983 8984 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8985 if (cdb->byte2 & SRW10_FUA) 8986 flags |= CTL_LLF_FUA; 8987 if (cdb->byte2 & SRW10_DPO) 8988 flags |= CTL_LLF_DPO; 8989 lba = scsi_8btou64(cdb->addr); 8990 num_blocks = cdb->length; 8991 break; 8992 } 8993 default: 8994 /* 8995 * We got a command we don't support. This shouldn't 8996 * happen, commands should be filtered out above us. 8997 */ 8998 ctl_set_invalid_opcode(ctsio); 8999 ctl_done((union ctl_io *)ctsio); 9000 9001 return (CTL_RETVAL_COMPLETE); 9002 break; /* NOTREACHED */ 9003 } 9004 9005 /* 9006 * The first check is to make sure we're in bounds, the second 9007 * check is to catch wrap-around problems. If the lba + num blocks 9008 * is less than the lba, then we've wrapped around and the block 9009 * range is invalid anyway. 9010 */ 9011 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9012 || ((lba + num_blocks) < lba)) { 9013 ctl_set_lba_out_of_range(ctsio, 9014 MAX(lba, lun->be_lun->maxlba + 1)); 9015 ctl_done((union ctl_io *)ctsio); 9016 return (CTL_RETVAL_COMPLETE); 9017 } 9018 9019 /* 9020 * According to SBC-3, a transfer length of 0 is not an error. 9021 */ 9022 if (num_blocks == 0) { 9023 ctl_set_success(ctsio); 9024 ctl_done((union ctl_io *)ctsio); 9025 return (CTL_RETVAL_COMPLETE); 9026 } 9027 9028 /* Set FUA if write cache is disabled. */ 9029 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 9030 flags |= CTL_LLF_FUA; 9031 9032 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9033 ctsio->kern_rel_offset = 0; 9034 9035 /* 9036 * Set the IO_CONT flag, so that if this I/O gets passed to 9037 * ctl_data_submit_done(), it'll get passed back to 9038 * ctl_ctl_cnw_cont() for further processing. 9039 */ 9040 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9041 ctsio->io_cont = ctl_cnw_cont; 9042 9043 lbalen = (struct ctl_lba_len_flags *) 9044 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9045 lbalen->lba = lba; 9046 lbalen->len = num_blocks; 9047 lbalen->flags = CTL_LLF_COMPARE | flags; 9048 9049 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9050 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9051 return (retval); 9052 } 9053 9054 int 9055 ctl_verify(struct ctl_scsiio *ctsio) 9056 { 9057 struct ctl_lun *lun = CTL_LUN(ctsio); 9058 struct ctl_lba_len_flags *lbalen; 9059 uint64_t lba; 9060 uint32_t num_blocks; 9061 int bytchk, flags; 9062 int retval; 9063 9064 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9065 9066 bytchk = 0; 9067 flags = CTL_LLF_FUA; 9068 switch (ctsio->cdb[0]) { 9069 case VERIFY_10: { 9070 struct scsi_verify_10 *cdb; 9071 9072 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9073 if (cdb->byte2 & SVFY_BYTCHK) 9074 bytchk = 1; 9075 if (cdb->byte2 & SVFY_DPO) 9076 flags |= CTL_LLF_DPO; 9077 lba = scsi_4btoul(cdb->addr); 9078 num_blocks = scsi_2btoul(cdb->length); 9079 break; 9080 } 9081 case VERIFY_12: { 9082 struct scsi_verify_12 *cdb; 9083 9084 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9085 if (cdb->byte2 & SVFY_BYTCHK) 9086 bytchk = 1; 9087 if (cdb->byte2 & SVFY_DPO) 9088 flags |= CTL_LLF_DPO; 9089 lba = scsi_4btoul(cdb->addr); 9090 num_blocks = scsi_4btoul(cdb->length); 9091 break; 9092 } 9093 case VERIFY_16: { 9094 struct scsi_rw_16 *cdb; 9095 9096 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9097 if (cdb->byte2 & SVFY_BYTCHK) 9098 bytchk = 1; 9099 if (cdb->byte2 & SVFY_DPO) 9100 flags |= CTL_LLF_DPO; 9101 lba = scsi_8btou64(cdb->addr); 9102 num_blocks = scsi_4btoul(cdb->length); 9103 break; 9104 } 9105 default: 9106 /* 9107 * We got a command we don't support. This shouldn't 9108 * happen, commands should be filtered out above us. 9109 */ 9110 ctl_set_invalid_opcode(ctsio); 9111 ctl_done((union ctl_io *)ctsio); 9112 return (CTL_RETVAL_COMPLETE); 9113 } 9114 9115 /* 9116 * The first check is to make sure we're in bounds, the second 9117 * check is to catch wrap-around problems. If the lba + num blocks 9118 * is less than the lba, then we've wrapped around and the block 9119 * range is invalid anyway. 9120 */ 9121 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9122 || ((lba + num_blocks) < lba)) { 9123 ctl_set_lba_out_of_range(ctsio, 9124 MAX(lba, lun->be_lun->maxlba + 1)); 9125 ctl_done((union ctl_io *)ctsio); 9126 return (CTL_RETVAL_COMPLETE); 9127 } 9128 9129 /* 9130 * According to SBC-3, a transfer length of 0 is not an error. 9131 */ 9132 if (num_blocks == 0) { 9133 ctl_set_success(ctsio); 9134 ctl_done((union ctl_io *)ctsio); 9135 return (CTL_RETVAL_COMPLETE); 9136 } 9137 9138 lbalen = (struct ctl_lba_len_flags *) 9139 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9140 lbalen->lba = lba; 9141 lbalen->len = num_blocks; 9142 if (bytchk) { 9143 lbalen->flags = CTL_LLF_COMPARE | flags; 9144 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9145 } else { 9146 lbalen->flags = CTL_LLF_VERIFY | flags; 9147 ctsio->kern_total_len = 0; 9148 } 9149 ctsio->kern_rel_offset = 0; 9150 9151 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9152 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9153 return (retval); 9154 } 9155 9156 int 9157 ctl_report_luns(struct ctl_scsiio *ctsio) 9158 { 9159 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9160 struct ctl_port *port = CTL_PORT(ctsio); 9161 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9162 struct scsi_report_luns *cdb; 9163 struct scsi_report_luns_data *lun_data; 9164 int num_filled, num_luns, num_port_luns, retval; 9165 uint32_t alloc_len, lun_datalen; 9166 uint32_t initidx, targ_lun_id, lun_id; 9167 9168 retval = CTL_RETVAL_COMPLETE; 9169 cdb = (struct scsi_report_luns *)ctsio->cdb; 9170 9171 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9172 9173 num_luns = 0; 9174 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9175 mtx_lock(&softc->ctl_lock); 9176 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9177 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9178 num_luns++; 9179 } 9180 mtx_unlock(&softc->ctl_lock); 9181 9182 switch (cdb->select_report) { 9183 case RPL_REPORT_DEFAULT: 9184 case RPL_REPORT_ALL: 9185 case RPL_REPORT_NONSUBSID: 9186 break; 9187 case RPL_REPORT_WELLKNOWN: 9188 case RPL_REPORT_ADMIN: 9189 case RPL_REPORT_CONGLOM: 9190 num_luns = 0; 9191 break; 9192 default: 9193 ctl_set_invalid_field(ctsio, 9194 /*sks_valid*/ 1, 9195 /*command*/ 1, 9196 /*field*/ 2, 9197 /*bit_valid*/ 0, 9198 /*bit*/ 0); 9199 ctl_done((union ctl_io *)ctsio); 9200 return (retval); 9201 break; /* NOTREACHED */ 9202 } 9203 9204 alloc_len = scsi_4btoul(cdb->length); 9205 /* 9206 * The initiator has to allocate at least 16 bytes for this request, 9207 * so he can at least get the header and the first LUN. Otherwise 9208 * we reject the request (per SPC-3 rev 14, section 6.21). 9209 */ 9210 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9211 sizeof(struct scsi_report_luns_lundata))) { 9212 ctl_set_invalid_field(ctsio, 9213 /*sks_valid*/ 1, 9214 /*command*/ 1, 9215 /*field*/ 6, 9216 /*bit_valid*/ 0, 9217 /*bit*/ 0); 9218 ctl_done((union ctl_io *)ctsio); 9219 return (retval); 9220 } 9221 9222 lun_datalen = sizeof(*lun_data) + 9223 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9224 9225 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9226 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9227 ctsio->kern_sg_entries = 0; 9228 9229 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9230 9231 mtx_lock(&softc->ctl_lock); 9232 for (targ_lun_id = 0, num_filled = 0; 9233 targ_lun_id < num_port_luns && num_filled < num_luns; 9234 targ_lun_id++) { 9235 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9236 if (lun_id == UINT32_MAX) 9237 continue; 9238 lun = softc->ctl_luns[lun_id]; 9239 if (lun == NULL) 9240 continue; 9241 9242 be64enc(lun_data->luns[num_filled++].lundata, 9243 ctl_encode_lun(targ_lun_id)); 9244 9245 /* 9246 * According to SPC-3, rev 14 section 6.21: 9247 * 9248 * "The execution of a REPORT LUNS command to any valid and 9249 * installed logical unit shall clear the REPORTED LUNS DATA 9250 * HAS CHANGED unit attention condition for all logical 9251 * units of that target with respect to the requesting 9252 * initiator. A valid and installed logical unit is one 9253 * having a PERIPHERAL QUALIFIER of 000b in the standard 9254 * INQUIRY data (see 6.4.2)." 9255 * 9256 * If request_lun is NULL, the LUN this report luns command 9257 * was issued to is either disabled or doesn't exist. In that 9258 * case, we shouldn't clear any pending lun change unit 9259 * attention. 9260 */ 9261 if (request_lun != NULL) { 9262 mtx_lock(&lun->lun_lock); 9263 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9264 mtx_unlock(&lun->lun_lock); 9265 } 9266 } 9267 mtx_unlock(&softc->ctl_lock); 9268 9269 /* 9270 * It's quite possible that we've returned fewer LUNs than we allocated 9271 * space for. Trim it. 9272 */ 9273 lun_datalen = sizeof(*lun_data) + 9274 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9275 ctsio->kern_rel_offset = 0; 9276 ctsio->kern_sg_entries = 0; 9277 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9278 ctsio->kern_total_len = ctsio->kern_data_len; 9279 9280 /* 9281 * We set this to the actual data length, regardless of how much 9282 * space we actually have to return results. If the user looks at 9283 * this value, he'll know whether or not he allocated enough space 9284 * and reissue the command if necessary. We don't support well 9285 * known logical units, so if the user asks for that, return none. 9286 */ 9287 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9288 9289 /* 9290 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9291 * this request. 9292 */ 9293 ctl_set_success(ctsio); 9294 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9295 ctsio->be_move_done = ctl_config_move_done; 9296 ctl_datamove((union ctl_io *)ctsio); 9297 return (retval); 9298 } 9299 9300 int 9301 ctl_request_sense(struct ctl_scsiio *ctsio) 9302 { 9303 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9304 struct ctl_lun *lun = CTL_LUN(ctsio); 9305 struct scsi_request_sense *cdb; 9306 struct scsi_sense_data *sense_ptr, *ps; 9307 uint32_t initidx; 9308 int have_error; 9309 u_int sense_len = SSD_FULL_SIZE; 9310 scsi_sense_data_type sense_format; 9311 ctl_ua_type ua_type; 9312 uint8_t asc = 0, ascq = 0; 9313 9314 cdb = (struct scsi_request_sense *)ctsio->cdb; 9315 9316 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9317 9318 /* 9319 * Determine which sense format the user wants. 9320 */ 9321 if (cdb->byte2 & SRS_DESC) 9322 sense_format = SSD_TYPE_DESC; 9323 else 9324 sense_format = SSD_TYPE_FIXED; 9325 9326 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9327 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9328 ctsio->kern_sg_entries = 0; 9329 ctsio->kern_rel_offset = 0; 9330 9331 /* 9332 * struct scsi_sense_data, which is currently set to 256 bytes, is 9333 * larger than the largest allowed value for the length field in the 9334 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9335 */ 9336 ctsio->kern_data_len = cdb->length; 9337 ctsio->kern_total_len = cdb->length; 9338 9339 /* 9340 * If we don't have a LUN, we don't have any pending sense. 9341 */ 9342 if (lun == NULL || 9343 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9344 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9345 /* "Logical unit not supported" */ 9346 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9347 /*current_error*/ 1, 9348 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9349 /*asc*/ 0x25, 9350 /*ascq*/ 0x00, 9351 SSD_ELEM_NONE); 9352 goto send; 9353 } 9354 9355 have_error = 0; 9356 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9357 /* 9358 * Check for pending sense, and then for pending unit attentions. 9359 * Pending sense gets returned first, then pending unit attentions. 9360 */ 9361 mtx_lock(&lun->lun_lock); 9362 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9363 if (ps != NULL) 9364 ps += initidx % CTL_MAX_INIT_PER_PORT; 9365 if (ps != NULL && ps->error_code != 0) { 9366 scsi_sense_data_type stored_format; 9367 9368 /* 9369 * Check to see which sense format was used for the stored 9370 * sense data. 9371 */ 9372 stored_format = scsi_sense_type(ps); 9373 9374 /* 9375 * If the user requested a different sense format than the 9376 * one we stored, then we need to convert it to the other 9377 * format. If we're going from descriptor to fixed format 9378 * sense data, we may lose things in translation, depending 9379 * on what options were used. 9380 * 9381 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9382 * for some reason we'll just copy it out as-is. 9383 */ 9384 if ((stored_format == SSD_TYPE_FIXED) 9385 && (sense_format == SSD_TYPE_DESC)) 9386 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9387 ps, (struct scsi_sense_data_desc *)sense_ptr); 9388 else if ((stored_format == SSD_TYPE_DESC) 9389 && (sense_format == SSD_TYPE_FIXED)) 9390 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9391 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9392 else 9393 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9394 9395 ps->error_code = 0; 9396 have_error = 1; 9397 } else { 9398 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9399 sense_format); 9400 if (ua_type != CTL_UA_NONE) 9401 have_error = 1; 9402 } 9403 if (have_error == 0) { 9404 /* 9405 * Report informational exception if have one and allowed. 9406 */ 9407 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9408 asc = lun->ie_asc; 9409 ascq = lun->ie_ascq; 9410 } 9411 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9412 /*current_error*/ 1, 9413 /*sense_key*/ SSD_KEY_NO_SENSE, 9414 /*asc*/ asc, 9415 /*ascq*/ ascq, 9416 SSD_ELEM_NONE); 9417 } 9418 mtx_unlock(&lun->lun_lock); 9419 9420 send: 9421 /* 9422 * We report the SCSI status as OK, since the status of the command 9423 * itself is OK. We're reporting sense as parameter data. 9424 */ 9425 ctl_set_success(ctsio); 9426 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9427 ctsio->be_move_done = ctl_config_move_done; 9428 ctl_datamove((union ctl_io *)ctsio); 9429 return (CTL_RETVAL_COMPLETE); 9430 } 9431 9432 int 9433 ctl_tur(struct ctl_scsiio *ctsio) 9434 { 9435 9436 CTL_DEBUG_PRINT(("ctl_tur\n")); 9437 9438 ctl_set_success(ctsio); 9439 ctl_done((union ctl_io *)ctsio); 9440 9441 return (CTL_RETVAL_COMPLETE); 9442 } 9443 9444 /* 9445 * SCSI VPD page 0x00, the Supported VPD Pages page. 9446 */ 9447 static int 9448 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9449 { 9450 struct ctl_lun *lun = CTL_LUN(ctsio); 9451 struct scsi_vpd_supported_pages *pages; 9452 int sup_page_size; 9453 int p; 9454 9455 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9456 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9457 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9458 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9459 ctsio->kern_rel_offset = 0; 9460 ctsio->kern_sg_entries = 0; 9461 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9462 ctsio->kern_total_len = ctsio->kern_data_len; 9463 9464 /* 9465 * The control device is always connected. The disk device, on the 9466 * other hand, may not be online all the time. Need to change this 9467 * to figure out whether the disk device is actually online or not. 9468 */ 9469 if (lun != NULL) 9470 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9471 lun->be_lun->lun_type; 9472 else 9473 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9474 9475 p = 0; 9476 /* Supported VPD pages */ 9477 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9478 /* Serial Number */ 9479 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9480 /* Device Identification */ 9481 pages->page_list[p++] = SVPD_DEVICE_ID; 9482 /* Extended INQUIRY Data */ 9483 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9484 /* Mode Page Policy */ 9485 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9486 /* SCSI Ports */ 9487 pages->page_list[p++] = SVPD_SCSI_PORTS; 9488 /* Third-party Copy */ 9489 pages->page_list[p++] = SVPD_SCSI_TPC; 9490 /* SCSI Feature Sets */ 9491 pages->page_list[p++] = SVPD_SCSI_SFS; 9492 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9493 /* Block limits */ 9494 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9495 /* Block Device Characteristics */ 9496 pages->page_list[p++] = SVPD_BDC; 9497 /* Logical Block Provisioning */ 9498 pages->page_list[p++] = SVPD_LBP; 9499 } 9500 pages->length = p; 9501 9502 ctl_set_success(ctsio); 9503 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9504 ctsio->be_move_done = ctl_config_move_done; 9505 ctl_datamove((union ctl_io *)ctsio); 9506 return (CTL_RETVAL_COMPLETE); 9507 } 9508 9509 /* 9510 * SCSI VPD page 0x80, the Unit Serial Number page. 9511 */ 9512 static int 9513 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9514 { 9515 struct ctl_lun *lun = CTL_LUN(ctsio); 9516 struct scsi_vpd_unit_serial_number *sn_ptr; 9517 int data_len; 9518 9519 data_len = 4 + CTL_SN_LEN; 9520 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9521 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9522 ctsio->kern_rel_offset = 0; 9523 ctsio->kern_sg_entries = 0; 9524 ctsio->kern_data_len = min(data_len, alloc_len); 9525 ctsio->kern_total_len = ctsio->kern_data_len; 9526 9527 /* 9528 * The control device is always connected. The disk device, on the 9529 * other hand, may not be online all the time. Need to change this 9530 * to figure out whether the disk device is actually online or not. 9531 */ 9532 if (lun != NULL) 9533 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9534 lun->be_lun->lun_type; 9535 else 9536 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9537 9538 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9539 sn_ptr->length = CTL_SN_LEN; 9540 /* 9541 * If we don't have a LUN, we just leave the serial number as 9542 * all spaces. 9543 */ 9544 if (lun != NULL) { 9545 strncpy((char *)sn_ptr->serial_num, 9546 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9547 } else 9548 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9549 9550 ctl_set_success(ctsio); 9551 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9552 ctsio->be_move_done = ctl_config_move_done; 9553 ctl_datamove((union ctl_io *)ctsio); 9554 return (CTL_RETVAL_COMPLETE); 9555 } 9556 9557 /* 9558 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9559 */ 9560 static int 9561 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9562 { 9563 struct ctl_lun *lun = CTL_LUN(ctsio); 9564 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9565 int data_len; 9566 9567 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9568 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9569 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9570 ctsio->kern_sg_entries = 0; 9571 ctsio->kern_rel_offset = 0; 9572 ctsio->kern_data_len = min(data_len, alloc_len); 9573 ctsio->kern_total_len = ctsio->kern_data_len; 9574 9575 /* 9576 * The control device is always connected. The disk device, on the 9577 * other hand, may not be online all the time. 9578 */ 9579 if (lun != NULL) 9580 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9581 lun->be_lun->lun_type; 9582 else 9583 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9584 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9585 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9586 /* 9587 * We support head of queue, ordered and simple tags. 9588 */ 9589 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9590 /* 9591 * Volatile cache supported. 9592 */ 9593 eid_ptr->flags3 = SVPD_EID_V_SUP; 9594 9595 /* 9596 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9597 * attention for a particular IT nexus on all LUNs once we report 9598 * it to that nexus once. This bit is required as of SPC-4. 9599 */ 9600 eid_ptr->flags4 = SVPD_EID_LUICLR; 9601 9602 /* 9603 * We support revert to defaults (RTD) bit in MODE SELECT. 9604 */ 9605 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9606 9607 /* 9608 * XXX KDM in order to correctly answer this, we would need 9609 * information from the SIM to determine how much sense data it 9610 * can send. So this would really be a path inquiry field, most 9611 * likely. This can be set to a maximum of 252 according to SPC-4, 9612 * but the hardware may or may not be able to support that much. 9613 * 0 just means that the maximum sense data length is not reported. 9614 */ 9615 eid_ptr->max_sense_length = 0; 9616 9617 ctl_set_success(ctsio); 9618 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9619 ctsio->be_move_done = ctl_config_move_done; 9620 ctl_datamove((union ctl_io *)ctsio); 9621 return (CTL_RETVAL_COMPLETE); 9622 } 9623 9624 static int 9625 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9626 { 9627 struct ctl_lun *lun = CTL_LUN(ctsio); 9628 struct scsi_vpd_mode_page_policy *mpp_ptr; 9629 int data_len; 9630 9631 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9632 sizeof(struct scsi_vpd_mode_page_policy_descr); 9633 9634 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9635 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9636 ctsio->kern_rel_offset = 0; 9637 ctsio->kern_sg_entries = 0; 9638 ctsio->kern_data_len = min(data_len, alloc_len); 9639 ctsio->kern_total_len = ctsio->kern_data_len; 9640 9641 /* 9642 * The control device is always connected. The disk device, on the 9643 * other hand, may not be online all the time. 9644 */ 9645 if (lun != NULL) 9646 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9647 lun->be_lun->lun_type; 9648 else 9649 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9650 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9651 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9652 mpp_ptr->descr[0].page_code = 0x3f; 9653 mpp_ptr->descr[0].subpage_code = 0xff; 9654 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9655 9656 ctl_set_success(ctsio); 9657 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9658 ctsio->be_move_done = ctl_config_move_done; 9659 ctl_datamove((union ctl_io *)ctsio); 9660 return (CTL_RETVAL_COMPLETE); 9661 } 9662 9663 /* 9664 * SCSI VPD page 0x83, the Device Identification page. 9665 */ 9666 static int 9667 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9668 { 9669 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9670 struct ctl_port *port = CTL_PORT(ctsio); 9671 struct ctl_lun *lun = CTL_LUN(ctsio); 9672 struct scsi_vpd_device_id *devid_ptr; 9673 struct scsi_vpd_id_descriptor *desc; 9674 int data_len, g; 9675 uint8_t proto; 9676 9677 data_len = sizeof(struct scsi_vpd_device_id) + 9678 sizeof(struct scsi_vpd_id_descriptor) + 9679 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9680 sizeof(struct scsi_vpd_id_descriptor) + 9681 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9682 if (lun && lun->lun_devid) 9683 data_len += lun->lun_devid->len; 9684 if (port && port->port_devid) 9685 data_len += port->port_devid->len; 9686 if (port && port->target_devid) 9687 data_len += port->target_devid->len; 9688 9689 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9690 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9691 ctsio->kern_sg_entries = 0; 9692 ctsio->kern_rel_offset = 0; 9693 ctsio->kern_sg_entries = 0; 9694 ctsio->kern_data_len = min(data_len, alloc_len); 9695 ctsio->kern_total_len = ctsio->kern_data_len; 9696 9697 /* 9698 * The control device is always connected. The disk device, on the 9699 * other hand, may not be online all the time. 9700 */ 9701 if (lun != NULL) 9702 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9703 lun->be_lun->lun_type; 9704 else 9705 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9706 devid_ptr->page_code = SVPD_DEVICE_ID; 9707 scsi_ulto2b(data_len - 4, devid_ptr->length); 9708 9709 if (port && port->port_type == CTL_PORT_FC) 9710 proto = SCSI_PROTO_FC << 4; 9711 else if (port && port->port_type == CTL_PORT_SAS) 9712 proto = SCSI_PROTO_SAS << 4; 9713 else if (port && port->port_type == CTL_PORT_ISCSI) 9714 proto = SCSI_PROTO_ISCSI << 4; 9715 else 9716 proto = SCSI_PROTO_SPI << 4; 9717 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9718 9719 /* 9720 * We're using a LUN association here. i.e., this device ID is a 9721 * per-LUN identifier. 9722 */ 9723 if (lun && lun->lun_devid) { 9724 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9725 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9726 lun->lun_devid->len); 9727 } 9728 9729 /* 9730 * This is for the WWPN which is a port association. 9731 */ 9732 if (port && port->port_devid) { 9733 memcpy(desc, port->port_devid->data, port->port_devid->len); 9734 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9735 port->port_devid->len); 9736 } 9737 9738 /* 9739 * This is for the Relative Target Port(type 4h) identifier 9740 */ 9741 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9742 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9743 SVPD_ID_TYPE_RELTARG; 9744 desc->length = 4; 9745 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9746 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9747 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9748 9749 /* 9750 * This is for the Target Port Group(type 5h) identifier 9751 */ 9752 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9753 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9754 SVPD_ID_TYPE_TPORTGRP; 9755 desc->length = 4; 9756 if (softc->is_single || 9757 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9758 g = 1; 9759 else 9760 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9761 scsi_ulto2b(g, &desc->identifier[2]); 9762 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9763 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9764 9765 /* 9766 * This is for the Target identifier 9767 */ 9768 if (port && port->target_devid) { 9769 memcpy(desc, port->target_devid->data, port->target_devid->len); 9770 } 9771 9772 ctl_set_success(ctsio); 9773 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9774 ctsio->be_move_done = ctl_config_move_done; 9775 ctl_datamove((union ctl_io *)ctsio); 9776 return (CTL_RETVAL_COMPLETE); 9777 } 9778 9779 static int 9780 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9781 { 9782 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9783 struct ctl_lun *lun = CTL_LUN(ctsio); 9784 struct scsi_vpd_scsi_ports *sp; 9785 struct scsi_vpd_port_designation *pd; 9786 struct scsi_vpd_port_designation_cont *pdc; 9787 struct ctl_port *port; 9788 int data_len, num_target_ports, iid_len, id_len; 9789 9790 num_target_ports = 0; 9791 iid_len = 0; 9792 id_len = 0; 9793 mtx_lock(&softc->ctl_lock); 9794 STAILQ_FOREACH(port, &softc->port_list, links) { 9795 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9796 continue; 9797 if (lun != NULL && 9798 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9799 continue; 9800 num_target_ports++; 9801 if (port->init_devid) 9802 iid_len += port->init_devid->len; 9803 if (port->port_devid) 9804 id_len += port->port_devid->len; 9805 } 9806 mtx_unlock(&softc->ctl_lock); 9807 9808 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9809 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9810 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9811 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9812 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9813 ctsio->kern_sg_entries = 0; 9814 ctsio->kern_rel_offset = 0; 9815 ctsio->kern_sg_entries = 0; 9816 ctsio->kern_data_len = min(data_len, alloc_len); 9817 ctsio->kern_total_len = ctsio->kern_data_len; 9818 9819 /* 9820 * The control device is always connected. The disk device, on the 9821 * other hand, may not be online all the time. Need to change this 9822 * to figure out whether the disk device is actually online or not. 9823 */ 9824 if (lun != NULL) 9825 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9826 lun->be_lun->lun_type; 9827 else 9828 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9829 9830 sp->page_code = SVPD_SCSI_PORTS; 9831 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9832 sp->page_length); 9833 pd = &sp->design[0]; 9834 9835 mtx_lock(&softc->ctl_lock); 9836 STAILQ_FOREACH(port, &softc->port_list, links) { 9837 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9838 continue; 9839 if (lun != NULL && 9840 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9841 continue; 9842 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9843 if (port->init_devid) { 9844 iid_len = port->init_devid->len; 9845 memcpy(pd->initiator_transportid, 9846 port->init_devid->data, port->init_devid->len); 9847 } else 9848 iid_len = 0; 9849 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9850 pdc = (struct scsi_vpd_port_designation_cont *) 9851 (&pd->initiator_transportid[iid_len]); 9852 if (port->port_devid) { 9853 id_len = port->port_devid->len; 9854 memcpy(pdc->target_port_descriptors, 9855 port->port_devid->data, port->port_devid->len); 9856 } else 9857 id_len = 0; 9858 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9859 pd = (struct scsi_vpd_port_designation *) 9860 ((uint8_t *)pdc->target_port_descriptors + id_len); 9861 } 9862 mtx_unlock(&softc->ctl_lock); 9863 9864 ctl_set_success(ctsio); 9865 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9866 ctsio->be_move_done = ctl_config_move_done; 9867 ctl_datamove((union ctl_io *)ctsio); 9868 return (CTL_RETVAL_COMPLETE); 9869 } 9870 9871 static int 9872 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len) 9873 { 9874 struct ctl_lun *lun = CTL_LUN(ctsio); 9875 struct scsi_vpd_sfs *sfs_ptr; 9876 int sfs_page_size, n; 9877 9878 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2; 9879 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO); 9880 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr; 9881 ctsio->kern_sg_entries = 0; 9882 ctsio->kern_rel_offset = 0; 9883 ctsio->kern_sg_entries = 0; 9884 ctsio->kern_data_len = min(sfs_page_size, alloc_len); 9885 ctsio->kern_total_len = ctsio->kern_data_len; 9886 9887 /* 9888 * The control device is always connected. The disk device, on the 9889 * other hand, may not be online all the time. Need to change this 9890 * to figure out whether the disk device is actually online or not. 9891 */ 9892 if (lun != NULL) 9893 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9894 lun->be_lun->lun_type; 9895 else 9896 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9897 9898 sfs_ptr->page_code = SVPD_SCSI_SFS; 9899 n = 0; 9900 /* Discovery 2016 */ 9901 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]); 9902 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9903 /* SBC Base 2016 */ 9904 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]); 9905 /* SBC Base 2010 */ 9906 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]); 9907 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9908 /* Basic Provisioning 2016 */ 9909 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]); 9910 } 9911 /* Drive Maintenance 2016 */ 9912 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]); 9913 } 9914 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length); 9915 9916 ctl_set_success(ctsio); 9917 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9918 ctsio->be_move_done = ctl_config_move_done; 9919 ctl_datamove((union ctl_io *)ctsio); 9920 return (CTL_RETVAL_COMPLETE); 9921 } 9922 9923 static int 9924 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9925 { 9926 struct ctl_lun *lun = CTL_LUN(ctsio); 9927 struct scsi_vpd_block_limits *bl_ptr; 9928 const char *val; 9929 uint64_t ival; 9930 9931 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9932 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9933 ctsio->kern_sg_entries = 0; 9934 ctsio->kern_rel_offset = 0; 9935 ctsio->kern_sg_entries = 0; 9936 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9937 ctsio->kern_total_len = ctsio->kern_data_len; 9938 9939 /* 9940 * The control device is always connected. The disk device, on the 9941 * other hand, may not be online all the time. Need to change this 9942 * to figure out whether the disk device is actually online or not. 9943 */ 9944 if (lun != NULL) 9945 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9946 lun->be_lun->lun_type; 9947 else 9948 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9949 9950 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9951 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9952 bl_ptr->max_cmp_write_len = 0xff; 9953 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9954 if (lun != NULL) { 9955 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9956 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9957 ival = 0xffffffff; 9958 val = dnvlist_get_string(lun->be_lun->options, 9959 "unmap_max_lba", NULL); 9960 if (val != NULL) 9961 ctl_expand_number(val, &ival); 9962 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9963 ival = 0xffffffff; 9964 val = dnvlist_get_string(lun->be_lun->options, 9965 "unmap_max_descr", NULL); 9966 if (val != NULL) 9967 ctl_expand_number(val, &ival); 9968 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9969 if (lun->be_lun->ublockexp != 0) { 9970 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9971 bl_ptr->opt_unmap_grain); 9972 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9973 bl_ptr->unmap_grain_align); 9974 } 9975 } 9976 scsi_ulto4b(lun->be_lun->atomicblock, 9977 bl_ptr->max_atomic_transfer_length); 9978 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9979 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9980 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9981 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9982 ival = UINT64_MAX; 9983 val = dnvlist_get_string(lun->be_lun->options, 9984 "write_same_max_lba", NULL); 9985 if (val != NULL) 9986 ctl_expand_number(val, &ival); 9987 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9988 if (lun->be_lun->maxlba + 1 > ival) 9989 bl_ptr->flags |= SVPD_BL_WSNZ; 9990 } 9991 9992 ctl_set_success(ctsio); 9993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9994 ctsio->be_move_done = ctl_config_move_done; 9995 ctl_datamove((union ctl_io *)ctsio); 9996 return (CTL_RETVAL_COMPLETE); 9997 } 9998 9999 static int 10000 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10001 { 10002 struct ctl_lun *lun = CTL_LUN(ctsio); 10003 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10004 const char *value; 10005 u_int i; 10006 10007 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10008 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10009 ctsio->kern_sg_entries = 0; 10010 ctsio->kern_rel_offset = 0; 10011 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 10012 ctsio->kern_total_len = ctsio->kern_data_len; 10013 10014 /* 10015 * The control device is always connected. The disk device, on the 10016 * other hand, may not be online all the time. Need to change this 10017 * to figure out whether the disk device is actually online or not. 10018 */ 10019 if (lun != NULL) 10020 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10021 lun->be_lun->lun_type; 10022 else 10023 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10024 bdc_ptr->page_code = SVPD_BDC; 10025 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10026 if (lun != NULL && 10027 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 10028 i = strtol(value, NULL, 0); 10029 else 10030 i = CTL_DEFAULT_ROTATION_RATE; 10031 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10032 if (lun != NULL && 10033 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 10034 i = strtol(value, NULL, 0); 10035 else 10036 i = 0; 10037 bdc_ptr->wab_wac_ff = (i & 0x0f); 10038 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS; 10039 10040 ctl_set_success(ctsio); 10041 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10042 ctsio->be_move_done = ctl_config_move_done; 10043 ctl_datamove((union ctl_io *)ctsio); 10044 return (CTL_RETVAL_COMPLETE); 10045 } 10046 10047 static int 10048 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10049 { 10050 struct ctl_lun *lun = CTL_LUN(ctsio); 10051 struct scsi_vpd_logical_block_prov *lbp_ptr; 10052 const char *value; 10053 10054 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10055 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10056 ctsio->kern_sg_entries = 0; 10057 ctsio->kern_rel_offset = 0; 10058 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 10059 ctsio->kern_total_len = ctsio->kern_data_len; 10060 10061 /* 10062 * The control device is always connected. The disk device, on the 10063 * other hand, may not be online all the time. Need to change this 10064 * to figure out whether the disk device is actually online or not. 10065 */ 10066 if (lun != NULL) 10067 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10068 lun->be_lun->lun_type; 10069 else 10070 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10071 10072 lbp_ptr->page_code = SVPD_LBP; 10073 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10074 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10075 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10076 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10077 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10078 value = dnvlist_get_string(lun->be_lun->options, 10079 "provisioning_type", NULL); 10080 if (value != NULL) { 10081 if (strcmp(value, "resource") == 0) 10082 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10083 else if (strcmp(value, "thin") == 0) 10084 lbp_ptr->prov_type = SVPD_LBP_THIN; 10085 } else 10086 lbp_ptr->prov_type = SVPD_LBP_THIN; 10087 } 10088 10089 ctl_set_success(ctsio); 10090 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10091 ctsio->be_move_done = ctl_config_move_done; 10092 ctl_datamove((union ctl_io *)ctsio); 10093 return (CTL_RETVAL_COMPLETE); 10094 } 10095 10096 /* 10097 * INQUIRY with the EVPD bit set. 10098 */ 10099 static int 10100 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10101 { 10102 struct ctl_lun *lun = CTL_LUN(ctsio); 10103 struct scsi_inquiry *cdb; 10104 int alloc_len, retval; 10105 10106 cdb = (struct scsi_inquiry *)ctsio->cdb; 10107 alloc_len = scsi_2btoul(cdb->length); 10108 10109 switch (cdb->page_code) { 10110 case SVPD_SUPPORTED_PAGES: 10111 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10112 break; 10113 case SVPD_UNIT_SERIAL_NUMBER: 10114 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10115 break; 10116 case SVPD_DEVICE_ID: 10117 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10118 break; 10119 case SVPD_EXTENDED_INQUIRY_DATA: 10120 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10121 break; 10122 case SVPD_MODE_PAGE_POLICY: 10123 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10124 break; 10125 case SVPD_SCSI_PORTS: 10126 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10127 break; 10128 case SVPD_SCSI_TPC: 10129 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10130 break; 10131 case SVPD_SCSI_SFS: 10132 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len); 10133 break; 10134 case SVPD_BLOCK_LIMITS: 10135 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10136 goto err; 10137 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10138 break; 10139 case SVPD_BDC: 10140 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10141 goto err; 10142 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10143 break; 10144 case SVPD_LBP: 10145 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10146 goto err; 10147 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10148 break; 10149 default: 10150 err: 10151 ctl_set_invalid_field(ctsio, 10152 /*sks_valid*/ 1, 10153 /*command*/ 1, 10154 /*field*/ 2, 10155 /*bit_valid*/ 0, 10156 /*bit*/ 0); 10157 ctl_done((union ctl_io *)ctsio); 10158 retval = CTL_RETVAL_COMPLETE; 10159 break; 10160 } 10161 10162 return (retval); 10163 } 10164 10165 /* 10166 * Standard INQUIRY data. 10167 */ 10168 static int 10169 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10170 { 10171 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10172 struct ctl_port *port = CTL_PORT(ctsio); 10173 struct ctl_lun *lun = CTL_LUN(ctsio); 10174 struct scsi_inquiry_data *inq_ptr; 10175 struct scsi_inquiry *cdb; 10176 const char *val; 10177 uint32_t alloc_len, data_len; 10178 ctl_port_type port_type; 10179 10180 port_type = port->port_type; 10181 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10182 port_type = CTL_PORT_SCSI; 10183 10184 cdb = (struct scsi_inquiry *)ctsio->cdb; 10185 alloc_len = scsi_2btoul(cdb->length); 10186 10187 /* 10188 * We malloc the full inquiry data size here and fill it 10189 * in. If the user only asks for less, we'll give him 10190 * that much. 10191 */ 10192 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10193 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10194 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10195 ctsio->kern_sg_entries = 0; 10196 ctsio->kern_rel_offset = 0; 10197 ctsio->kern_data_len = min(data_len, alloc_len); 10198 ctsio->kern_total_len = ctsio->kern_data_len; 10199 10200 if (lun != NULL) { 10201 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10202 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10203 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10204 lun->be_lun->lun_type; 10205 } else { 10206 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10207 lun->be_lun->lun_type; 10208 } 10209 if (lun->flags & CTL_LUN_REMOVABLE) 10210 inq_ptr->dev_qual2 |= SID_RMB; 10211 } else 10212 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10213 10214 /* RMB in byte 2 is 0 */ 10215 inq_ptr->version = SCSI_REV_SPC5; 10216 10217 /* 10218 * According to SAM-3, even if a device only supports a single 10219 * level of LUN addressing, it should still set the HISUP bit: 10220 * 10221 * 4.9.1 Logical unit numbers overview 10222 * 10223 * All logical unit number formats described in this standard are 10224 * hierarchical in structure even when only a single level in that 10225 * hierarchy is used. The HISUP bit shall be set to one in the 10226 * standard INQUIRY data (see SPC-2) when any logical unit number 10227 * format described in this standard is used. Non-hierarchical 10228 * formats are outside the scope of this standard. 10229 * 10230 * Therefore we set the HiSup bit here. 10231 * 10232 * The response format is 2, per SPC-3. 10233 */ 10234 inq_ptr->response_format = SID_HiSup | 2; 10235 10236 inq_ptr->additional_length = data_len - 10237 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10238 CTL_DEBUG_PRINT(("additional_length = %d\n", 10239 inq_ptr->additional_length)); 10240 10241 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10242 if (port_type == CTL_PORT_SCSI) 10243 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10244 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10245 inq_ptr->flags = SID_CmdQue; 10246 if (port_type == CTL_PORT_SCSI) 10247 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10248 10249 /* 10250 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10251 * We have 8 bytes for the vendor name, and 16 bytes for the device 10252 * name and 4 bytes for the revision. 10253 */ 10254 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10255 "vendor", NULL)) == NULL) { 10256 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10257 } else { 10258 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10259 strncpy(inq_ptr->vendor, val, 10260 min(sizeof(inq_ptr->vendor), strlen(val))); 10261 } 10262 if (lun == NULL) { 10263 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10264 sizeof(inq_ptr->product)); 10265 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10266 NULL)) == NULL) { 10267 switch (lun->be_lun->lun_type) { 10268 case T_DIRECT: 10269 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10270 sizeof(inq_ptr->product)); 10271 break; 10272 case T_PROCESSOR: 10273 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10274 sizeof(inq_ptr->product)); 10275 break; 10276 case T_CDROM: 10277 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10278 sizeof(inq_ptr->product)); 10279 break; 10280 default: 10281 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10282 sizeof(inq_ptr->product)); 10283 break; 10284 } 10285 } else { 10286 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10287 strncpy(inq_ptr->product, val, 10288 min(sizeof(inq_ptr->product), strlen(val))); 10289 } 10290 10291 /* 10292 * XXX make this a macro somewhere so it automatically gets 10293 * incremented when we make changes. 10294 */ 10295 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10296 "revision", NULL)) == NULL) { 10297 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10298 } else { 10299 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10300 strncpy(inq_ptr->revision, val, 10301 min(sizeof(inq_ptr->revision), strlen(val))); 10302 } 10303 10304 /* 10305 * For parallel SCSI, we support double transition and single 10306 * transition clocking. We also support QAS (Quick Arbitration 10307 * and Selection) and Information Unit transfers on both the 10308 * control and array devices. 10309 */ 10310 if (port_type == CTL_PORT_SCSI) 10311 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10312 SID_SPI_IUS; 10313 10314 /* SAM-6 (no version claimed) */ 10315 scsi_ulto2b(0x00C0, inq_ptr->version1); 10316 /* SPC-5 (no version claimed) */ 10317 scsi_ulto2b(0x05C0, inq_ptr->version2); 10318 if (port_type == CTL_PORT_FC) { 10319 /* FCP-2 ANSI INCITS.350:2003 */ 10320 scsi_ulto2b(0x0917, inq_ptr->version3); 10321 } else if (port_type == CTL_PORT_SCSI) { 10322 /* SPI-4 ANSI INCITS.362:200x */ 10323 scsi_ulto2b(0x0B56, inq_ptr->version3); 10324 } else if (port_type == CTL_PORT_ISCSI) { 10325 /* iSCSI (no version claimed) */ 10326 scsi_ulto2b(0x0960, inq_ptr->version3); 10327 } else if (port_type == CTL_PORT_SAS) { 10328 /* SAS (no version claimed) */ 10329 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10330 } else if (port_type == CTL_PORT_UMASS) { 10331 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10332 scsi_ulto2b(0x1730, inq_ptr->version3); 10333 } 10334 10335 if (lun == NULL) { 10336 /* SBC-4 (no version claimed) */ 10337 scsi_ulto2b(0x0600, inq_ptr->version4); 10338 } else { 10339 switch (lun->be_lun->lun_type) { 10340 case T_DIRECT: 10341 /* SBC-4 (no version claimed) */ 10342 scsi_ulto2b(0x0600, inq_ptr->version4); 10343 break; 10344 case T_PROCESSOR: 10345 break; 10346 case T_CDROM: 10347 /* MMC-6 (no version claimed) */ 10348 scsi_ulto2b(0x04E0, inq_ptr->version4); 10349 break; 10350 default: 10351 break; 10352 } 10353 } 10354 10355 ctl_set_success(ctsio); 10356 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10357 ctsio->be_move_done = ctl_config_move_done; 10358 ctl_datamove((union ctl_io *)ctsio); 10359 return (CTL_RETVAL_COMPLETE); 10360 } 10361 10362 int 10363 ctl_inquiry(struct ctl_scsiio *ctsio) 10364 { 10365 struct scsi_inquiry *cdb; 10366 int retval; 10367 10368 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10369 10370 cdb = (struct scsi_inquiry *)ctsio->cdb; 10371 if (cdb->byte2 & SI_EVPD) 10372 retval = ctl_inquiry_evpd(ctsio); 10373 else if (cdb->page_code == 0) 10374 retval = ctl_inquiry_std(ctsio); 10375 else { 10376 ctl_set_invalid_field(ctsio, 10377 /*sks_valid*/ 1, 10378 /*command*/ 1, 10379 /*field*/ 2, 10380 /*bit_valid*/ 0, 10381 /*bit*/ 0); 10382 ctl_done((union ctl_io *)ctsio); 10383 return (CTL_RETVAL_COMPLETE); 10384 } 10385 10386 return (retval); 10387 } 10388 10389 int 10390 ctl_get_config(struct ctl_scsiio *ctsio) 10391 { 10392 struct ctl_lun *lun = CTL_LUN(ctsio); 10393 struct scsi_get_config_header *hdr; 10394 struct scsi_get_config_feature *feature; 10395 struct scsi_get_config *cdb; 10396 uint32_t alloc_len, data_len; 10397 int rt, starting; 10398 10399 cdb = (struct scsi_get_config *)ctsio->cdb; 10400 rt = (cdb->rt & SGC_RT_MASK); 10401 starting = scsi_2btoul(cdb->starting_feature); 10402 alloc_len = scsi_2btoul(cdb->length); 10403 10404 data_len = sizeof(struct scsi_get_config_header) + 10405 sizeof(struct scsi_get_config_feature) + 8 + 10406 sizeof(struct scsi_get_config_feature) + 8 + 10407 sizeof(struct scsi_get_config_feature) + 4 + 10408 sizeof(struct scsi_get_config_feature) + 4 + 10409 sizeof(struct scsi_get_config_feature) + 8 + 10410 sizeof(struct scsi_get_config_feature) + 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 sizeof(struct scsi_get_config_feature) + 4; 10417 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10418 ctsio->kern_sg_entries = 0; 10419 ctsio->kern_rel_offset = 0; 10420 10421 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10422 if (lun->flags & CTL_LUN_NO_MEDIA) 10423 scsi_ulto2b(0x0000, hdr->current_profile); 10424 else 10425 scsi_ulto2b(0x0010, hdr->current_profile); 10426 feature = (struct scsi_get_config_feature *)(hdr + 1); 10427 10428 if (starting > 0x003b) 10429 goto done; 10430 if (starting > 0x003a) 10431 goto f3b; 10432 if (starting > 0x002b) 10433 goto f3a; 10434 if (starting > 0x002a) 10435 goto f2b; 10436 if (starting > 0x001f) 10437 goto f2a; 10438 if (starting > 0x001e) 10439 goto f1f; 10440 if (starting > 0x001d) 10441 goto f1e; 10442 if (starting > 0x0010) 10443 goto f1d; 10444 if (starting > 0x0003) 10445 goto f10; 10446 if (starting > 0x0002) 10447 goto f3; 10448 if (starting > 0x0001) 10449 goto f2; 10450 if (starting > 0x0000) 10451 goto f1; 10452 10453 /* Profile List */ 10454 scsi_ulto2b(0x0000, feature->feature_code); 10455 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10456 feature->add_length = 8; 10457 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10458 feature->feature_data[2] = 0x00; 10459 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10460 feature->feature_data[6] = 0x01; 10461 feature = (struct scsi_get_config_feature *) 10462 &feature->feature_data[feature->add_length]; 10463 10464 f1: /* Core */ 10465 scsi_ulto2b(0x0001, feature->feature_code); 10466 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10467 feature->add_length = 8; 10468 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10469 feature->feature_data[4] = 0x03; 10470 feature = (struct scsi_get_config_feature *) 10471 &feature->feature_data[feature->add_length]; 10472 10473 f2: /* Morphing */ 10474 scsi_ulto2b(0x0002, feature->feature_code); 10475 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10476 feature->add_length = 4; 10477 feature->feature_data[0] = 0x02; 10478 feature = (struct scsi_get_config_feature *) 10479 &feature->feature_data[feature->add_length]; 10480 10481 f3: /* Removable Medium */ 10482 scsi_ulto2b(0x0003, feature->feature_code); 10483 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10484 feature->add_length = 4; 10485 feature->feature_data[0] = 0x39; 10486 feature = (struct scsi_get_config_feature *) 10487 &feature->feature_data[feature->add_length]; 10488 10489 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10490 goto done; 10491 10492 f10: /* Random Read */ 10493 scsi_ulto2b(0x0010, feature->feature_code); 10494 feature->flags = 0x00; 10495 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10496 feature->flags |= SGC_F_CURRENT; 10497 feature->add_length = 8; 10498 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10499 scsi_ulto2b(1, &feature->feature_data[4]); 10500 feature->feature_data[6] = 0x00; 10501 feature = (struct scsi_get_config_feature *) 10502 &feature->feature_data[feature->add_length]; 10503 10504 f1d: /* Multi-Read */ 10505 scsi_ulto2b(0x001D, feature->feature_code); 10506 feature->flags = 0x00; 10507 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10508 feature->flags |= SGC_F_CURRENT; 10509 feature->add_length = 0; 10510 feature = (struct scsi_get_config_feature *) 10511 &feature->feature_data[feature->add_length]; 10512 10513 f1e: /* CD Read */ 10514 scsi_ulto2b(0x001E, feature->feature_code); 10515 feature->flags = 0x00; 10516 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10517 feature->flags |= SGC_F_CURRENT; 10518 feature->add_length = 4; 10519 feature->feature_data[0] = 0x00; 10520 feature = (struct scsi_get_config_feature *) 10521 &feature->feature_data[feature->add_length]; 10522 10523 f1f: /* DVD Read */ 10524 scsi_ulto2b(0x001F, feature->feature_code); 10525 feature->flags = 0x08; 10526 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10527 feature->flags |= SGC_F_CURRENT; 10528 feature->add_length = 4; 10529 feature->feature_data[0] = 0x01; 10530 feature->feature_data[2] = 0x03; 10531 feature = (struct scsi_get_config_feature *) 10532 &feature->feature_data[feature->add_length]; 10533 10534 f2a: /* DVD+RW */ 10535 scsi_ulto2b(0x002A, feature->feature_code); 10536 feature->flags = 0x04; 10537 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10538 feature->flags |= SGC_F_CURRENT; 10539 feature->add_length = 4; 10540 feature->feature_data[0] = 0x00; 10541 feature->feature_data[1] = 0x00; 10542 feature = (struct scsi_get_config_feature *) 10543 &feature->feature_data[feature->add_length]; 10544 10545 f2b: /* DVD+R */ 10546 scsi_ulto2b(0x002B, feature->feature_code); 10547 feature->flags = 0x00; 10548 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10549 feature->flags |= SGC_F_CURRENT; 10550 feature->add_length = 4; 10551 feature->feature_data[0] = 0x00; 10552 feature = (struct scsi_get_config_feature *) 10553 &feature->feature_data[feature->add_length]; 10554 10555 f3a: /* DVD+RW Dual Layer */ 10556 scsi_ulto2b(0x003A, feature->feature_code); 10557 feature->flags = 0x00; 10558 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10559 feature->flags |= SGC_F_CURRENT; 10560 feature->add_length = 4; 10561 feature->feature_data[0] = 0x00; 10562 feature->feature_data[1] = 0x00; 10563 feature = (struct scsi_get_config_feature *) 10564 &feature->feature_data[feature->add_length]; 10565 10566 f3b: /* DVD+R Dual Layer */ 10567 scsi_ulto2b(0x003B, feature->feature_code); 10568 feature->flags = 0x00; 10569 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10570 feature->flags |= SGC_F_CURRENT; 10571 feature->add_length = 4; 10572 feature->feature_data[0] = 0x00; 10573 feature = (struct scsi_get_config_feature *) 10574 &feature->feature_data[feature->add_length]; 10575 10576 done: 10577 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10578 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10579 feature = (struct scsi_get_config_feature *)(hdr + 1); 10580 if (scsi_2btoul(feature->feature_code) == starting) 10581 feature = (struct scsi_get_config_feature *) 10582 &feature->feature_data[feature->add_length]; 10583 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10584 } 10585 scsi_ulto4b(data_len - 4, hdr->data_length); 10586 ctsio->kern_data_len = min(data_len, alloc_len); 10587 ctsio->kern_total_len = ctsio->kern_data_len; 10588 10589 ctl_set_success(ctsio); 10590 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10591 ctsio->be_move_done = ctl_config_move_done; 10592 ctl_datamove((union ctl_io *)ctsio); 10593 return (CTL_RETVAL_COMPLETE); 10594 } 10595 10596 int 10597 ctl_get_event_status(struct ctl_scsiio *ctsio) 10598 { 10599 struct scsi_get_event_status_header *hdr; 10600 struct scsi_get_event_status *cdb; 10601 uint32_t alloc_len, data_len; 10602 10603 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10604 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10605 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10606 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10607 ctl_done((union ctl_io *)ctsio); 10608 return (CTL_RETVAL_COMPLETE); 10609 } 10610 alloc_len = scsi_2btoul(cdb->length); 10611 10612 data_len = sizeof(struct scsi_get_event_status_header); 10613 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10614 ctsio->kern_sg_entries = 0; 10615 ctsio->kern_rel_offset = 0; 10616 ctsio->kern_data_len = min(data_len, alloc_len); 10617 ctsio->kern_total_len = ctsio->kern_data_len; 10618 10619 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10620 scsi_ulto2b(0, hdr->descr_length); 10621 hdr->nea_class = SGESN_NEA; 10622 hdr->supported_class = 0; 10623 10624 ctl_set_success(ctsio); 10625 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10626 ctsio->be_move_done = ctl_config_move_done; 10627 ctl_datamove((union ctl_io *)ctsio); 10628 return (CTL_RETVAL_COMPLETE); 10629 } 10630 10631 int 10632 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10633 { 10634 struct scsi_mechanism_status_header *hdr; 10635 struct scsi_mechanism_status *cdb; 10636 uint32_t alloc_len, data_len; 10637 10638 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10639 alloc_len = scsi_2btoul(cdb->length); 10640 10641 data_len = sizeof(struct scsi_mechanism_status_header); 10642 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10643 ctsio->kern_sg_entries = 0; 10644 ctsio->kern_rel_offset = 0; 10645 ctsio->kern_data_len = min(data_len, alloc_len); 10646 ctsio->kern_total_len = ctsio->kern_data_len; 10647 10648 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10649 hdr->state1 = 0x00; 10650 hdr->state2 = 0xe0; 10651 scsi_ulto3b(0, hdr->lba); 10652 hdr->slots_num = 0; 10653 scsi_ulto2b(0, hdr->slots_length); 10654 10655 ctl_set_success(ctsio); 10656 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10657 ctsio->be_move_done = ctl_config_move_done; 10658 ctl_datamove((union ctl_io *)ctsio); 10659 return (CTL_RETVAL_COMPLETE); 10660 } 10661 10662 static void 10663 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10664 { 10665 10666 lba += 150; 10667 buf[0] = 0; 10668 buf[1] = bin2bcd((lba / 75) / 60); 10669 buf[2] = bin2bcd((lba / 75) % 60); 10670 buf[3] = bin2bcd(lba % 75); 10671 } 10672 10673 int 10674 ctl_read_toc(struct ctl_scsiio *ctsio) 10675 { 10676 struct ctl_lun *lun = CTL_LUN(ctsio); 10677 struct scsi_read_toc_hdr *hdr; 10678 struct scsi_read_toc_type01_descr *descr; 10679 struct scsi_read_toc *cdb; 10680 uint32_t alloc_len, data_len; 10681 int format, msf; 10682 10683 cdb = (struct scsi_read_toc *)ctsio->cdb; 10684 msf = (cdb->byte2 & CD_MSF) != 0; 10685 format = cdb->format; 10686 alloc_len = scsi_2btoul(cdb->data_len); 10687 10688 data_len = sizeof(struct scsi_read_toc_hdr); 10689 if (format == 0) 10690 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10691 else 10692 data_len += sizeof(struct scsi_read_toc_type01_descr); 10693 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10694 ctsio->kern_sg_entries = 0; 10695 ctsio->kern_rel_offset = 0; 10696 ctsio->kern_data_len = min(data_len, alloc_len); 10697 ctsio->kern_total_len = ctsio->kern_data_len; 10698 10699 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10700 if (format == 0) { 10701 scsi_ulto2b(0x12, hdr->data_length); 10702 hdr->first = 1; 10703 hdr->last = 1; 10704 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10705 descr->addr_ctl = 0x14; 10706 descr->track_number = 1; 10707 if (msf) 10708 ctl_ultomsf(0, descr->track_start); 10709 else 10710 scsi_ulto4b(0, descr->track_start); 10711 descr++; 10712 descr->addr_ctl = 0x14; 10713 descr->track_number = 0xaa; 10714 if (msf) 10715 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10716 else 10717 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10718 } else { 10719 scsi_ulto2b(0x0a, hdr->data_length); 10720 hdr->first = 1; 10721 hdr->last = 1; 10722 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10723 descr->addr_ctl = 0x14; 10724 descr->track_number = 1; 10725 if (msf) 10726 ctl_ultomsf(0, descr->track_start); 10727 else 10728 scsi_ulto4b(0, descr->track_start); 10729 } 10730 10731 ctl_set_success(ctsio); 10732 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10733 ctsio->be_move_done = ctl_config_move_done; 10734 ctl_datamove((union ctl_io *)ctsio); 10735 return (CTL_RETVAL_COMPLETE); 10736 } 10737 10738 /* 10739 * For known CDB types, parse the LBA and length. 10740 */ 10741 static int 10742 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10743 { 10744 10745 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10746 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10747 10748 switch (io->scsiio.cdb[0]) { 10749 case COMPARE_AND_WRITE: { 10750 struct scsi_compare_and_write *cdb; 10751 10752 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10753 10754 *lba = scsi_8btou64(cdb->addr); 10755 *len = cdb->length; 10756 break; 10757 } 10758 case READ_6: 10759 case WRITE_6: { 10760 struct scsi_rw_6 *cdb; 10761 10762 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10763 10764 *lba = scsi_3btoul(cdb->addr); 10765 /* only 5 bits are valid in the most significant address byte */ 10766 *lba &= 0x1fffff; 10767 *len = cdb->length; 10768 break; 10769 } 10770 case READ_10: 10771 case WRITE_10: { 10772 struct scsi_rw_10 *cdb; 10773 10774 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10775 10776 *lba = scsi_4btoul(cdb->addr); 10777 *len = scsi_2btoul(cdb->length); 10778 break; 10779 } 10780 case WRITE_VERIFY_10: { 10781 struct scsi_write_verify_10 *cdb; 10782 10783 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10784 10785 *lba = scsi_4btoul(cdb->addr); 10786 *len = scsi_2btoul(cdb->length); 10787 break; 10788 } 10789 case READ_12: 10790 case WRITE_12: { 10791 struct scsi_rw_12 *cdb; 10792 10793 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10794 10795 *lba = scsi_4btoul(cdb->addr); 10796 *len = scsi_4btoul(cdb->length); 10797 break; 10798 } 10799 case WRITE_VERIFY_12: { 10800 struct scsi_write_verify_12 *cdb; 10801 10802 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10803 10804 *lba = scsi_4btoul(cdb->addr); 10805 *len = scsi_4btoul(cdb->length); 10806 break; 10807 } 10808 case READ_16: 10809 case WRITE_16: { 10810 struct scsi_rw_16 *cdb; 10811 10812 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10813 10814 *lba = scsi_8btou64(cdb->addr); 10815 *len = scsi_4btoul(cdb->length); 10816 break; 10817 } 10818 case WRITE_ATOMIC_16: { 10819 struct scsi_write_atomic_16 *cdb; 10820 10821 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10822 10823 *lba = scsi_8btou64(cdb->addr); 10824 *len = scsi_2btoul(cdb->length); 10825 break; 10826 } 10827 case WRITE_VERIFY_16: { 10828 struct scsi_write_verify_16 *cdb; 10829 10830 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10831 10832 *lba = scsi_8btou64(cdb->addr); 10833 *len = scsi_4btoul(cdb->length); 10834 break; 10835 } 10836 case WRITE_SAME_10: { 10837 struct scsi_write_same_10 *cdb; 10838 10839 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10840 10841 *lba = scsi_4btoul(cdb->addr); 10842 *len = scsi_2btoul(cdb->length); 10843 break; 10844 } 10845 case WRITE_SAME_16: { 10846 struct scsi_write_same_16 *cdb; 10847 10848 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10849 10850 *lba = scsi_8btou64(cdb->addr); 10851 *len = scsi_4btoul(cdb->length); 10852 break; 10853 } 10854 case VERIFY_10: { 10855 struct scsi_verify_10 *cdb; 10856 10857 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10858 10859 *lba = scsi_4btoul(cdb->addr); 10860 *len = scsi_2btoul(cdb->length); 10861 break; 10862 } 10863 case VERIFY_12: { 10864 struct scsi_verify_12 *cdb; 10865 10866 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10867 10868 *lba = scsi_4btoul(cdb->addr); 10869 *len = scsi_4btoul(cdb->length); 10870 break; 10871 } 10872 case VERIFY_16: { 10873 struct scsi_verify_16 *cdb; 10874 10875 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10876 10877 *lba = scsi_8btou64(cdb->addr); 10878 *len = scsi_4btoul(cdb->length); 10879 break; 10880 } 10881 case UNMAP: { 10882 *lba = 0; 10883 *len = UINT64_MAX; 10884 break; 10885 } 10886 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10887 struct scsi_get_lba_status *cdb; 10888 10889 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10890 *lba = scsi_8btou64(cdb->addr); 10891 *len = UINT32_MAX; 10892 break; 10893 } 10894 default: 10895 *lba = 0; 10896 *len = UINT64_MAX; 10897 return (1); 10898 } 10899 10900 return (0); 10901 } 10902 10903 static ctl_action 10904 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10905 bool seq) 10906 { 10907 uint64_t endlba1, endlba2; 10908 10909 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10910 endlba2 = lba2 + len2 - 1; 10911 10912 if ((endlba1 < lba2) || (endlba2 < lba1)) 10913 return (CTL_ACTION_PASS); 10914 else 10915 return (CTL_ACTION_BLOCK); 10916 } 10917 10918 static int 10919 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10920 { 10921 struct ctl_ptr_len_flags *ptrlen; 10922 struct scsi_unmap_desc *buf, *end, *range; 10923 uint64_t lba; 10924 uint32_t len; 10925 10926 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10927 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10928 10929 /* If not UNMAP -- go other way. */ 10930 if (io->scsiio.cdb[0] != UNMAP) 10931 return (CTL_ACTION_SKIP); 10932 10933 /* If UNMAP without data -- block and wait for data. */ 10934 ptrlen = (struct ctl_ptr_len_flags *) 10935 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10936 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10937 ptrlen->ptr == NULL) 10938 return (CTL_ACTION_BLOCK); 10939 10940 /* UNMAP with data -- check for collision. */ 10941 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10942 end = buf + ptrlen->len / sizeof(*buf); 10943 for (range = buf; range < end; range++) { 10944 lba = scsi_8btou64(range->lba); 10945 len = scsi_4btoul(range->length); 10946 if ((lba < lba2 + len2) && (lba + len > lba2)) 10947 return (CTL_ACTION_BLOCK); 10948 } 10949 return (CTL_ACTION_PASS); 10950 } 10951 10952 static ctl_action 10953 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10954 { 10955 uint64_t lba1, lba2; 10956 uint64_t len1, len2; 10957 int retval; 10958 10959 retval = ctl_get_lba_len(io2, &lba2, &len2); 10960 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10961 10962 retval = ctl_extent_check_unmap(io1, lba2, len2); 10963 if (retval != CTL_ACTION_SKIP) 10964 return (retval); 10965 10966 retval = ctl_get_lba_len(io1, &lba1, &len1); 10967 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10968 10969 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)) 10970 seq = FALSE; 10971 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10972 } 10973 10974 static ctl_action 10975 ctl_seq_check(union ctl_io *io1, union ctl_io *io2) 10976 { 10977 uint64_t lba1, lba2; 10978 uint64_t len1, len2; 10979 int retval __diagused; 10980 10981 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10982 return (CTL_ACTION_PASS); 10983 retval = ctl_get_lba_len(io1, &lba1, &len1); 10984 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10985 retval = ctl_get_lba_len(io2, &lba2, &len2); 10986 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10987 10988 if (lba1 + len1 == lba2) 10989 return (CTL_ACTION_BLOCK); 10990 return (CTL_ACTION_PASS); 10991 } 10992 10993 static ctl_action 10994 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10995 const uint8_t *serialize_row, union ctl_io *ooa_io) 10996 { 10997 10998 /* 10999 * The initiator attempted multiple untagged commands at the same 11000 * time. Can't do that. 11001 */ 11002 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11003 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11004 && ((pending_io->io_hdr.nexus.targ_port == 11005 ooa_io->io_hdr.nexus.targ_port) 11006 && (pending_io->io_hdr.nexus.initid == 11007 ooa_io->io_hdr.nexus.initid)) 11008 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11009 CTL_FLAG_STATUS_SENT)) == 0)) 11010 return (CTL_ACTION_OVERLAP); 11011 11012 /* 11013 * The initiator attempted to send multiple tagged commands with 11014 * the same ID. (It's fine if different initiators have the same 11015 * tag ID.) 11016 * 11017 * Even if all of those conditions are true, we don't kill the I/O 11018 * if the command ahead of us has been aborted. We won't end up 11019 * sending it to the FETD, and it's perfectly legal to resend a 11020 * command with the same tag number as long as the previous 11021 * instance of this tag number has been aborted somehow. 11022 */ 11023 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11024 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11025 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11026 && ((pending_io->io_hdr.nexus.targ_port == 11027 ooa_io->io_hdr.nexus.targ_port) 11028 && (pending_io->io_hdr.nexus.initid == 11029 ooa_io->io_hdr.nexus.initid)) 11030 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11031 CTL_FLAG_STATUS_SENT)) == 0)) 11032 return (CTL_ACTION_OVERLAP_TAG); 11033 11034 /* 11035 * If we get a head of queue tag, SAM-3 says that we should 11036 * immediately execute it. 11037 * 11038 * What happens if this command would normally block for some other 11039 * reason? e.g. a request sense with a head of queue tag 11040 * immediately after a write. Normally that would block, but this 11041 * will result in its getting executed immediately... 11042 * 11043 * We currently return "pass" instead of "skip", so we'll end up 11044 * going through the rest of the queue to check for overlapped tags. 11045 * 11046 * XXX KDM check for other types of blockage first?? 11047 */ 11048 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 11049 return (CTL_ACTION_PASS); 11050 11051 /* 11052 * Simple tags get blocked until all head of queue and ordered tags 11053 * ahead of them have completed. I'm lumping untagged commands in 11054 * with simple tags here. XXX KDM is that the right thing to do? 11055 */ 11056 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) || 11057 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 11058 return (CTL_ACTION_BLOCK); 11059 11060 /* Unsupported command in OOA queue. */ 11061 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD)) 11062 return (CTL_ACTION_PASS); 11063 11064 switch (serialize_row[ooa_io->scsiio.seridx]) { 11065 case CTL_SER_SEQ: 11066 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11067 return (ctl_seq_check(ooa_io, pending_io)); 11068 /* FALLTHROUGH */ 11069 case CTL_SER_PASS: 11070 return (CTL_ACTION_PASS); 11071 case CTL_SER_EXTENTOPT: 11072 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 11073 SCP_QUEUE_ALG_UNRESTRICTED) 11074 return (CTL_ACTION_PASS); 11075 /* FALLTHROUGH */ 11076 case CTL_SER_EXTENT: 11077 return (ctl_extent_check(ooa_io, pending_io, 11078 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11079 case CTL_SER_BLOCKOPT: 11080 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 11081 SCP_QUEUE_ALG_UNRESTRICTED) 11082 return (CTL_ACTION_PASS); 11083 /* FALLTHROUGH */ 11084 case CTL_SER_BLOCK: 11085 return (CTL_ACTION_BLOCK); 11086 default: 11087 __assert_unreachable(); 11088 } 11089 } 11090 11091 /* 11092 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11093 * Assumptions: 11094 * - pending_io is generally either incoming, or on the blocked queue 11095 * - starting I/O is the I/O we want to start the check with. 11096 */ 11097 static ctl_action 11098 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11099 union ctl_io **starting_io) 11100 { 11101 union ctl_io *ooa_io = *starting_io; 11102 const uint8_t *serialize_row; 11103 ctl_action action; 11104 11105 mtx_assert(&lun->lun_lock, MA_OWNED); 11106 11107 /* 11108 * Aborted commands are not going to be executed and may even 11109 * not report completion, so we don't care about their order. 11110 * Let them complete ASAP to clean the OOA queue. 11111 */ 11112 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT)) 11113 return (CTL_ACTION_SKIP); 11114 11115 /* 11116 * Ordered tags have to block until all items ahead of them have 11117 * completed. If we get called with an ordered tag, we always 11118 * block, if something else is ahead of us in the queue. 11119 */ 11120 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) && 11121 (ooa_io != NULL)) 11122 return (CTL_ACTION_BLOCK); 11123 11124 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx]; 11125 11126 /* 11127 * Run back along the OOA queue, starting with the current 11128 * blocked I/O and going through every I/O before it on the 11129 * queue. If starting_io is NULL, we'll just end up returning 11130 * CTL_ACTION_PASS. 11131 */ 11132 for (; ooa_io != NULL; 11133 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) { 11134 action = ctl_check_for_blockage(lun, pending_io, serialize_row, 11135 ooa_io); 11136 if (action != CTL_ACTION_PASS) { 11137 *starting_io = ooa_io; 11138 return (action); 11139 } 11140 } 11141 11142 *starting_io = NULL; 11143 return (CTL_ACTION_PASS); 11144 } 11145 11146 /* 11147 * Try to unblock the specified I/O. 11148 * 11149 * skip parameter allows explicitly skip present blocker of the I/O, 11150 * starting from the previous one on OOA queue. It can be used when 11151 * we know for sure that the blocker I/O does no longer count. 11152 */ 11153 static void 11154 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11155 { 11156 struct ctl_softc *softc = lun->ctl_softc; 11157 union ctl_io *bio, *obio; 11158 const struct ctl_cmd_entry *entry; 11159 union ctl_ha_msg msg_info; 11160 ctl_action action; 11161 11162 mtx_assert(&lun->lun_lock, MA_OWNED); 11163 11164 if (io->io_hdr.blocker == NULL) 11165 return; 11166 11167 obio = bio = io->io_hdr.blocker; 11168 if (skip) 11169 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links); 11170 action = ctl_check_ooa(lun, io, &bio); 11171 if (action == CTL_ACTION_BLOCK) { 11172 /* Still blocked, but may be by different I/O now. */ 11173 if (bio != obio) { 11174 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, 11175 &io->io_hdr, blocked_links); 11176 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, 11177 &io->io_hdr, blocked_links); 11178 io->io_hdr.blocker = bio; 11179 } 11180 return; 11181 } 11182 11183 /* No longer blocked, one way or another. */ 11184 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links); 11185 io->io_hdr.blocker = NULL; 11186 11187 switch (action) { 11188 case CTL_ACTION_PASS: 11189 case CTL_ACTION_SKIP: 11190 11191 /* Serializing commands from the other SC retire there. */ 11192 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11193 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11194 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11195 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11196 msg_info.hdr.serializing_sc = io; 11197 msg_info.hdr.msg_type = CTL_MSG_R2R; 11198 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11199 sizeof(msg_info.hdr), M_NOWAIT); 11200 break; 11201 } 11202 11203 /* 11204 * Check this I/O for LUN state changes that may have happened 11205 * while this command was blocked. The LUN state may have been 11206 * changed by a command ahead of us in the queue. 11207 */ 11208 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11209 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 11210 ctl_done(io); 11211 break; 11212 } 11213 11214 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11215 ctl_enqueue_rtr(io); 11216 break; 11217 default: 11218 __assert_unreachable(); 11219 case CTL_ACTION_OVERLAP: 11220 ctl_set_overlapped_cmd(&io->scsiio); 11221 goto error; 11222 case CTL_ACTION_OVERLAP_TAG: 11223 ctl_set_overlapped_tag(&io->scsiio, 11224 io->scsiio.tag_num & 0xff); 11225 error: 11226 /* Serializing commands from the other SC are done here. */ 11227 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11228 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11229 ctl_try_unblock_others(lun, io, TRUE); 11230 LIST_REMOVE(&io->io_hdr, ooa_links); 11231 11232 ctl_copy_sense_data_back(io, &msg_info); 11233 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11234 msg_info.hdr.serializing_sc = NULL; 11235 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 11236 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11237 sizeof(msg_info.scsi), M_WAITOK); 11238 ctl_free_io(io); 11239 break; 11240 } 11241 11242 ctl_done(io); 11243 break; 11244 } 11245 } 11246 11247 /* 11248 * Try to unblock I/Os blocked by the specified I/O. 11249 * 11250 * skip parameter allows explicitly skip the specified I/O as blocker, 11251 * starting from the previous one on the OOA queue. It can be used when 11252 * we know for sure that the specified I/O does no longer count (done). 11253 * It has to be still on OOA queue though so that we know where to start. 11254 */ 11255 static void 11256 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip) 11257 { 11258 union ctl_io *io, *next_io; 11259 11260 mtx_assert(&lun->lun_lock, MA_OWNED); 11261 11262 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue); 11263 io != NULL; io = next_io) { 11264 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links); 11265 11266 KASSERT(io->io_hdr.blocker != NULL, 11267 ("I/O %p on blocked list without blocker", io)); 11268 ctl_try_unblock_io(lun, io, skip); 11269 } 11270 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue), 11271 ("blocked_queue is not empty after skipping %p", bio)); 11272 } 11273 11274 /* 11275 * This routine (with one exception) checks LUN flags that can be set by 11276 * commands ahead of us in the OOA queue. These flags have to be checked 11277 * when a command initially comes in, and when we pull a command off the 11278 * blocked queue and are preparing to execute it. The reason we have to 11279 * check these flags for commands on the blocked queue is that the LUN 11280 * state may have been changed by a command ahead of us while we're on the 11281 * blocked queue. 11282 * 11283 * Ordering is somewhat important with these checks, so please pay 11284 * careful attention to the placement of any new checks. 11285 */ 11286 static int 11287 ctl_scsiio_lun_check(struct ctl_lun *lun, 11288 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11289 { 11290 struct ctl_softc *softc = lun->ctl_softc; 11291 int retval; 11292 uint32_t residx; 11293 11294 retval = 0; 11295 11296 mtx_assert(&lun->lun_lock, MA_OWNED); 11297 11298 /* 11299 * If this shelf is a secondary shelf controller, we may have to 11300 * reject some commands disallowed by HA mode and link state. 11301 */ 11302 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11303 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11304 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11305 ctl_set_lun_unavail(ctsio); 11306 retval = 1; 11307 goto bailout; 11308 } 11309 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11310 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11311 ctl_set_lun_transit(ctsio); 11312 retval = 1; 11313 goto bailout; 11314 } 11315 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11316 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11317 ctl_set_lun_standby(ctsio); 11318 retval = 1; 11319 goto bailout; 11320 } 11321 11322 /* The rest of checks are only done on executing side */ 11323 if (softc->ha_mode == CTL_HA_MODE_XFER) 11324 goto bailout; 11325 } 11326 11327 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11328 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11329 ctl_set_hw_write_protected(ctsio); 11330 retval = 1; 11331 goto bailout; 11332 } 11333 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11334 ctl_set_sense(ctsio, /*current_error*/ 1, 11335 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11336 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11337 retval = 1; 11338 goto bailout; 11339 } 11340 } 11341 11342 /* 11343 * Check for a reservation conflict. If this command isn't allowed 11344 * even on reserved LUNs, and if this initiator isn't the one who 11345 * reserved us, reject the command with a reservation conflict. 11346 */ 11347 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11348 if ((lun->flags & CTL_LUN_RESERVED) 11349 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11350 if (lun->res_idx != residx) { 11351 ctl_set_reservation_conflict(ctsio); 11352 retval = 1; 11353 goto bailout; 11354 } 11355 } 11356 11357 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11358 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11359 /* No reservation or command is allowed. */; 11360 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11361 (lun->pr_res_type == SPR_TYPE_WR_EX || 11362 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11363 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11364 /* The command is allowed for Write Exclusive resv. */; 11365 } else { 11366 /* 11367 * if we aren't registered or it's a res holder type 11368 * reservation and this isn't the res holder then set a 11369 * conflict. 11370 */ 11371 if (ctl_get_prkey(lun, residx) == 0 || 11372 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11373 ctl_set_reservation_conflict(ctsio); 11374 retval = 1; 11375 goto bailout; 11376 } 11377 } 11378 11379 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11380 if (lun->flags & CTL_LUN_EJECTED) 11381 ctl_set_lun_ejected(ctsio); 11382 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11383 if (lun->flags & CTL_LUN_REMOVABLE) 11384 ctl_set_lun_no_media(ctsio); 11385 else 11386 ctl_set_lun_int_reqd(ctsio); 11387 } else if (lun->flags & CTL_LUN_STOPPED) 11388 ctl_set_lun_stopped(ctsio); 11389 else 11390 goto bailout; 11391 retval = 1; 11392 goto bailout; 11393 } 11394 11395 bailout: 11396 return (retval); 11397 } 11398 11399 static void 11400 ctl_failover_io(union ctl_io *io, int have_lock) 11401 { 11402 ctl_set_busy(&io->scsiio); 11403 ctl_done(io); 11404 } 11405 11406 static void 11407 ctl_failover_lun(union ctl_io *rio) 11408 { 11409 struct ctl_softc *softc = CTL_SOFTC(rio); 11410 struct ctl_lun *lun; 11411 struct ctl_io_hdr *io, *next_io; 11412 uint32_t targ_lun; 11413 11414 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11415 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun)); 11416 11417 /* Find and lock the LUN. */ 11418 mtx_lock(&softc->ctl_lock); 11419 if (targ_lun > ctl_max_luns || 11420 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11421 mtx_unlock(&softc->ctl_lock); 11422 return; 11423 } 11424 mtx_lock(&lun->lun_lock); 11425 mtx_unlock(&softc->ctl_lock); 11426 if (lun->flags & CTL_LUN_DISABLED) { 11427 mtx_unlock(&lun->lun_lock); 11428 return; 11429 } 11430 11431 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11432 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11433 /* We are master */ 11434 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11435 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11436 io->flags |= CTL_FLAG_ABORT | 11437 CTL_FLAG_FAILOVER; 11438 ctl_try_unblock_io(lun, 11439 (union ctl_io *)io, FALSE); 11440 } else { /* This can be only due to DATAMOVE */ 11441 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11442 io->flags &= ~CTL_FLAG_DMA_INPROG; 11443 io->flags |= CTL_FLAG_IO_ACTIVE; 11444 io->port_status = 31340; 11445 ctl_enqueue_isc((union ctl_io *)io); 11446 } 11447 } else 11448 /* We are slave */ 11449 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11450 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11451 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11452 io->flags |= CTL_FLAG_FAILOVER; 11453 } else { 11454 ctl_set_busy(&((union ctl_io *)io)-> 11455 scsiio); 11456 ctl_done((union ctl_io *)io); 11457 } 11458 } 11459 } 11460 } else { /* SERIALIZE modes */ 11461 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11462 /* We are master */ 11463 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11464 if (io->blocker != NULL) { 11465 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue, 11466 io, blocked_links); 11467 io->blocker = NULL; 11468 } 11469 ctl_try_unblock_others(lun, (union ctl_io *)io, 11470 TRUE); 11471 LIST_REMOVE(io, ooa_links); 11472 ctl_free_io((union ctl_io *)io); 11473 } else 11474 /* We are slave */ 11475 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11476 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11477 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11478 ctl_set_busy(&((union ctl_io *)io)-> 11479 scsiio); 11480 ctl_done((union ctl_io *)io); 11481 } 11482 } 11483 } 11484 } 11485 mtx_unlock(&lun->lun_lock); 11486 } 11487 11488 static void 11489 ctl_scsiio_precheck(struct ctl_scsiio *ctsio) 11490 { 11491 struct ctl_softc *softc = CTL_SOFTC(ctsio); 11492 struct ctl_lun *lun; 11493 const struct ctl_cmd_entry *entry; 11494 union ctl_io *bio; 11495 uint32_t initidx, targ_lun; 11496 11497 lun = NULL; 11498 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11499 if (targ_lun < ctl_max_luns) 11500 lun = softc->ctl_luns[targ_lun]; 11501 if (lun) { 11502 /* 11503 * If the LUN is invalid, pretend that it doesn't exist. 11504 * It will go away as soon as all pending I/O has been 11505 * completed. 11506 */ 11507 mtx_lock(&lun->lun_lock); 11508 if (lun->flags & CTL_LUN_DISABLED) { 11509 mtx_unlock(&lun->lun_lock); 11510 lun = NULL; 11511 } 11512 } 11513 CTL_LUN(ctsio) = lun; 11514 if (lun) { 11515 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11516 11517 /* 11518 * Every I/O goes into the OOA queue for a particular LUN, 11519 * and stays there until completion. 11520 */ 11521 #ifdef CTL_TIME_IO 11522 if (LIST_EMPTY(&lun->ooa_queue)) 11523 lun->idle_time += getsbinuptime() - lun->last_busy; 11524 #endif 11525 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11526 } 11527 11528 /* Get command entry and return error if it is unsuppotyed. */ 11529 entry = ctl_validate_command(ctsio); 11530 if (entry == NULL) { 11531 if (lun) 11532 mtx_unlock(&lun->lun_lock); 11533 return; 11534 } 11535 11536 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11537 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11538 11539 /* 11540 * Check to see whether we can send this command to LUNs that don't 11541 * exist. This should pretty much only be the case for inquiry 11542 * and request sense. Further checks, below, really require having 11543 * a LUN, so we can't really check the command anymore. Just put 11544 * it on the rtr queue. 11545 */ 11546 if (lun == NULL) { 11547 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11548 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11549 ctl_enqueue_rtr((union ctl_io *)ctsio); 11550 return; 11551 } 11552 11553 ctl_set_unsupported_lun(ctsio); 11554 ctl_done((union ctl_io *)ctsio); 11555 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11556 return; 11557 } else { 11558 /* 11559 * Make sure we support this particular command on this LUN. 11560 * e.g., we don't support writes to the control LUN. 11561 */ 11562 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11563 mtx_unlock(&lun->lun_lock); 11564 ctl_set_invalid_opcode(ctsio); 11565 ctl_done((union ctl_io *)ctsio); 11566 return; 11567 } 11568 } 11569 11570 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11571 11572 /* 11573 * If we've got a request sense, it'll clear the contingent 11574 * allegiance condition. Otherwise, if we have a CA condition for 11575 * this initiator, clear it, because it sent down a command other 11576 * than request sense. 11577 */ 11578 if (ctsio->cdb[0] != REQUEST_SENSE) { 11579 struct scsi_sense_data *ps; 11580 11581 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11582 if (ps != NULL) 11583 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11584 } 11585 11586 /* 11587 * If the command has this flag set, it handles its own unit 11588 * attention reporting, we shouldn't do anything. Otherwise we 11589 * check for any pending unit attentions, and send them back to the 11590 * initiator. We only do this when a command initially comes in, 11591 * not when we pull it off the blocked queue. 11592 * 11593 * According to SAM-3, section 5.3.2, the order that things get 11594 * presented back to the host is basically unit attentions caused 11595 * by some sort of reset event, busy status, reservation conflicts 11596 * or task set full, and finally any other status. 11597 * 11598 * One issue here is that some of the unit attentions we report 11599 * don't fall into the "reset" category (e.g. "reported luns data 11600 * has changed"). So reporting it here, before the reservation 11601 * check, may be technically wrong. I guess the only thing to do 11602 * would be to check for and report the reset events here, and then 11603 * check for the other unit attention types after we check for a 11604 * reservation conflict. 11605 * 11606 * XXX KDM need to fix this 11607 */ 11608 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11609 ctl_ua_type ua_type; 11610 u_int sense_len = 0; 11611 11612 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11613 &sense_len, SSD_TYPE_NONE); 11614 if (ua_type != CTL_UA_NONE) { 11615 mtx_unlock(&lun->lun_lock); 11616 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11617 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11618 ctsio->sense_len = sense_len; 11619 ctl_done((union ctl_io *)ctsio); 11620 return; 11621 } 11622 } 11623 11624 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11625 mtx_unlock(&lun->lun_lock); 11626 ctl_done((union ctl_io *)ctsio); 11627 return; 11628 } 11629 11630 /* 11631 * XXX CHD this is where we want to send IO to other side if 11632 * this LUN is secondary on this SC. We will need to make a copy 11633 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11634 * the copy we send as FROM_OTHER. 11635 * We also need to stuff the address of the original IO so we can 11636 * find it easily. Something similar will need be done on the other 11637 * side so when we are done we can find the copy. 11638 */ 11639 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11640 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11641 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11642 union ctl_ha_msg msg_info; 11643 int isc_retval; 11644 11645 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11646 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11647 mtx_unlock(&lun->lun_lock); 11648 11649 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11650 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11651 msg_info.hdr.serializing_sc = NULL; 11652 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11653 msg_info.scsi.tag_num = ctsio->tag_num; 11654 msg_info.scsi.tag_type = ctsio->tag_type; 11655 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11656 msg_info.scsi.cdb_len = ctsio->cdb_len; 11657 msg_info.scsi.priority = ctsio->priority; 11658 11659 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11660 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11661 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11662 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11663 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 11664 ctl_set_busy(ctsio); 11665 ctl_done((union ctl_io *)ctsio); 11666 return; 11667 } 11668 return; 11669 } 11670 11671 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 11672 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 11673 case CTL_ACTION_PASS: 11674 case CTL_ACTION_SKIP: 11675 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11676 mtx_unlock(&lun->lun_lock); 11677 ctl_enqueue_rtr((union ctl_io *)ctsio); 11678 break; 11679 case CTL_ACTION_BLOCK: 11680 ctsio->io_hdr.blocker = bio; 11681 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 11682 blocked_links); 11683 mtx_unlock(&lun->lun_lock); 11684 break; 11685 case CTL_ACTION_OVERLAP: 11686 mtx_unlock(&lun->lun_lock); 11687 ctl_set_overlapped_cmd(ctsio); 11688 ctl_done((union ctl_io *)ctsio); 11689 break; 11690 case CTL_ACTION_OVERLAP_TAG: 11691 mtx_unlock(&lun->lun_lock); 11692 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11693 ctl_done((union ctl_io *)ctsio); 11694 break; 11695 default: 11696 __assert_unreachable(); 11697 } 11698 } 11699 11700 const struct ctl_cmd_entry * 11701 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11702 { 11703 const struct ctl_cmd_entry *entry; 11704 int service_action; 11705 11706 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11707 if (sa) 11708 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11709 if (entry->flags & CTL_CMD_FLAG_SA5) { 11710 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11711 entry = &((const struct ctl_cmd_entry *) 11712 entry->execute)[service_action]; 11713 } 11714 return (entry); 11715 } 11716 11717 const struct ctl_cmd_entry * 11718 ctl_validate_command(struct ctl_scsiio *ctsio) 11719 { 11720 const struct ctl_cmd_entry *entry; 11721 int i, sa; 11722 uint8_t diff; 11723 11724 entry = ctl_get_cmd_entry(ctsio, &sa); 11725 ctsio->seridx = entry->seridx; 11726 if (entry->execute == NULL) { 11727 if (sa) 11728 ctl_set_invalid_field(ctsio, 11729 /*sks_valid*/ 1, 11730 /*command*/ 1, 11731 /*field*/ 1, 11732 /*bit_valid*/ 1, 11733 /*bit*/ 4); 11734 else 11735 ctl_set_invalid_opcode(ctsio); 11736 ctl_done((union ctl_io *)ctsio); 11737 return (NULL); 11738 } 11739 KASSERT(entry->length > 0, 11740 ("Not defined length for command 0x%02x/0x%02x", 11741 ctsio->cdb[0], ctsio->cdb[1])); 11742 for (i = 1; i < entry->length; i++) { 11743 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11744 if (diff == 0) 11745 continue; 11746 ctl_set_invalid_field(ctsio, 11747 /*sks_valid*/ 1, 11748 /*command*/ 1, 11749 /*field*/ i, 11750 /*bit_valid*/ 1, 11751 /*bit*/ fls(diff) - 1); 11752 ctl_done((union ctl_io *)ctsio); 11753 return (NULL); 11754 } 11755 return (entry); 11756 } 11757 11758 static int 11759 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11760 { 11761 11762 switch (lun_type) { 11763 case T_DIRECT: 11764 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11765 return (0); 11766 break; 11767 case T_PROCESSOR: 11768 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11769 return (0); 11770 break; 11771 case T_CDROM: 11772 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11773 return (0); 11774 break; 11775 default: 11776 return (0); 11777 } 11778 return (1); 11779 } 11780 11781 static int 11782 ctl_scsiio(struct ctl_scsiio *ctsio) 11783 { 11784 int retval; 11785 const struct ctl_cmd_entry *entry; 11786 11787 retval = CTL_RETVAL_COMPLETE; 11788 11789 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11790 11791 entry = ctl_get_cmd_entry(ctsio, NULL); 11792 11793 /* 11794 * If this I/O has been aborted, just send it straight to 11795 * ctl_done() without executing it. 11796 */ 11797 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11798 ctl_done((union ctl_io *)ctsio); 11799 goto bailout; 11800 } 11801 11802 /* 11803 * All the checks should have been handled by ctl_scsiio_precheck(). 11804 * We should be clear now to just execute the I/O. 11805 */ 11806 retval = entry->execute(ctsio); 11807 11808 bailout: 11809 return (retval); 11810 } 11811 11812 static int 11813 ctl_target_reset(union ctl_io *io) 11814 { 11815 struct ctl_softc *softc = CTL_SOFTC(io); 11816 struct ctl_port *port = CTL_PORT(io); 11817 struct ctl_lun *lun; 11818 uint32_t initidx; 11819 ctl_ua_type ua_type; 11820 11821 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11822 union ctl_ha_msg msg_info; 11823 11824 msg_info.hdr.nexus = io->io_hdr.nexus; 11825 msg_info.task.task_action = io->taskio.task_action; 11826 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11827 msg_info.hdr.original_sc = NULL; 11828 msg_info.hdr.serializing_sc = NULL; 11829 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11830 sizeof(msg_info.task), M_WAITOK); 11831 } 11832 11833 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11834 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11835 ua_type = CTL_UA_TARG_RESET; 11836 else 11837 ua_type = CTL_UA_BUS_RESET; 11838 mtx_lock(&softc->ctl_lock); 11839 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11840 if (port != NULL && 11841 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11842 continue; 11843 ctl_do_lun_reset(lun, initidx, ua_type); 11844 } 11845 mtx_unlock(&softc->ctl_lock); 11846 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11847 return (0); 11848 } 11849 11850 /* 11851 * The LUN should always be set. The I/O is optional, and is used to 11852 * distinguish between I/Os sent by this initiator, and by other 11853 * initiators. We set unit attention for initiators other than this one. 11854 * SAM-3 is vague on this point. It does say that a unit attention should 11855 * be established for other initiators when a LUN is reset (see section 11856 * 5.7.3), but it doesn't specifically say that the unit attention should 11857 * be established for this particular initiator when a LUN is reset. Here 11858 * is the relevant text, from SAM-3 rev 8: 11859 * 11860 * 5.7.2 When a SCSI initiator port aborts its own tasks 11861 * 11862 * When a SCSI initiator port causes its own task(s) to be aborted, no 11863 * notification that the task(s) have been aborted shall be returned to 11864 * the SCSI initiator port other than the completion response for the 11865 * command or task management function action that caused the task(s) to 11866 * be aborted and notification(s) associated with related effects of the 11867 * action (e.g., a reset unit attention condition). 11868 * 11869 * XXX KDM for now, we're setting unit attention for all initiators. 11870 */ 11871 static void 11872 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11873 { 11874 struct ctl_io_hdr *xioh; 11875 int i; 11876 11877 mtx_lock(&lun->lun_lock); 11878 /* Abort tasks. */ 11879 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11880 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11881 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE); 11882 } 11883 /* Clear CA. */ 11884 for (i = 0; i < ctl_max_ports; i++) { 11885 free(lun->pending_sense[i], M_CTL); 11886 lun->pending_sense[i] = NULL; 11887 } 11888 /* Clear reservation. */ 11889 lun->flags &= ~CTL_LUN_RESERVED; 11890 /* Clear prevent media removal. */ 11891 if (lun->prevent) { 11892 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11893 ctl_clear_mask(lun->prevent, i); 11894 lun->prevent_count = 0; 11895 } 11896 /* Clear TPC status */ 11897 ctl_tpc_lun_clear(lun, -1); 11898 /* Establish UA. */ 11899 #if 0 11900 ctl_est_ua_all(lun, initidx, ua_type); 11901 #else 11902 ctl_est_ua_all(lun, -1, ua_type); 11903 #endif 11904 mtx_unlock(&lun->lun_lock); 11905 } 11906 11907 static int 11908 ctl_lun_reset(union ctl_io *io) 11909 { 11910 struct ctl_softc *softc = CTL_SOFTC(io); 11911 struct ctl_lun *lun; 11912 uint32_t targ_lun, initidx; 11913 11914 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11915 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11916 mtx_lock(&softc->ctl_lock); 11917 if (targ_lun >= ctl_max_luns || 11918 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11919 mtx_unlock(&softc->ctl_lock); 11920 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11921 return (1); 11922 } 11923 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11924 mtx_unlock(&softc->ctl_lock); 11925 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11926 11927 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11928 union ctl_ha_msg msg_info; 11929 11930 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11931 msg_info.hdr.nexus = io->io_hdr.nexus; 11932 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11933 msg_info.hdr.original_sc = NULL; 11934 msg_info.hdr.serializing_sc = NULL; 11935 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11936 sizeof(msg_info.task), M_WAITOK); 11937 } 11938 return (0); 11939 } 11940 11941 static void 11942 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11943 int other_sc) 11944 { 11945 struct ctl_io_hdr *xioh; 11946 11947 mtx_assert(&lun->lun_lock, MA_OWNED); 11948 11949 /* 11950 * Run through the OOA queue and attempt to find the given I/O. 11951 * The target port, initiator ID, tag type and tag number have to 11952 * match the values that we got from the initiator. If we have an 11953 * untagged command to abort, simply abort the first untagged command 11954 * we come to. We only allow one untagged command at a time of course. 11955 */ 11956 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11957 union ctl_io *xio = (union ctl_io *)xioh; 11958 if ((targ_port == UINT32_MAX || 11959 targ_port == xioh->nexus.targ_port) && 11960 (init_id == UINT32_MAX || 11961 init_id == xioh->nexus.initid)) { 11962 if (targ_port != xioh->nexus.targ_port || 11963 init_id != xioh->nexus.initid) 11964 xioh->flags |= CTL_FLAG_ABORT_STATUS; 11965 xioh->flags |= CTL_FLAG_ABORT; 11966 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11967 union ctl_ha_msg msg_info; 11968 11969 msg_info.hdr.nexus = xioh->nexus; 11970 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11971 msg_info.task.tag_num = xio->scsiio.tag_num; 11972 msg_info.task.tag_type = xio->scsiio.tag_type; 11973 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11974 msg_info.hdr.original_sc = NULL; 11975 msg_info.hdr.serializing_sc = NULL; 11976 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11977 sizeof(msg_info.task), M_NOWAIT); 11978 } 11979 ctl_try_unblock_io(lun, xio, FALSE); 11980 } 11981 } 11982 } 11983 11984 static int 11985 ctl_abort_task_set(union ctl_io *io) 11986 { 11987 struct ctl_softc *softc = CTL_SOFTC(io); 11988 struct ctl_lun *lun; 11989 uint32_t targ_lun; 11990 11991 /* 11992 * Look up the LUN. 11993 */ 11994 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11995 mtx_lock(&softc->ctl_lock); 11996 if (targ_lun >= ctl_max_luns || 11997 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11998 mtx_unlock(&softc->ctl_lock); 11999 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12000 return (1); 12001 } 12002 12003 mtx_lock(&lun->lun_lock); 12004 mtx_unlock(&softc->ctl_lock); 12005 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12006 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12007 io->io_hdr.nexus.initid, 12008 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12009 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12010 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12011 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12012 } 12013 mtx_unlock(&lun->lun_lock); 12014 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12015 return (0); 12016 } 12017 12018 static void 12019 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 12020 ctl_ua_type ua_type) 12021 { 12022 struct ctl_lun *lun; 12023 struct scsi_sense_data *ps; 12024 uint32_t p, i; 12025 12026 p = initidx / CTL_MAX_INIT_PER_PORT; 12027 i = initidx % CTL_MAX_INIT_PER_PORT; 12028 mtx_lock(&softc->ctl_lock); 12029 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12030 mtx_lock(&lun->lun_lock); 12031 /* Abort tasks. */ 12032 ctl_abort_tasks_lun(lun, p, i, 1); 12033 /* Clear CA. */ 12034 ps = lun->pending_sense[p]; 12035 if (ps != NULL) 12036 ps[i].error_code = 0; 12037 /* Clear reservation. */ 12038 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12039 lun->flags &= ~CTL_LUN_RESERVED; 12040 /* Clear prevent media removal. */ 12041 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 12042 ctl_clear_mask(lun->prevent, initidx); 12043 lun->prevent_count--; 12044 } 12045 /* Clear TPC status */ 12046 ctl_tpc_lun_clear(lun, initidx); 12047 /* Establish UA. */ 12048 ctl_est_ua(lun, initidx, ua_type); 12049 mtx_unlock(&lun->lun_lock); 12050 } 12051 mtx_unlock(&softc->ctl_lock); 12052 } 12053 12054 static int 12055 ctl_i_t_nexus_reset(union ctl_io *io) 12056 { 12057 struct ctl_softc *softc = CTL_SOFTC(io); 12058 uint32_t initidx; 12059 12060 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12061 union ctl_ha_msg msg_info; 12062 12063 msg_info.hdr.nexus = io->io_hdr.nexus; 12064 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12065 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12066 msg_info.hdr.original_sc = NULL; 12067 msg_info.hdr.serializing_sc = NULL; 12068 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12069 sizeof(msg_info.task), M_WAITOK); 12070 } 12071 12072 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12073 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 12074 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12075 return (0); 12076 } 12077 12078 static int 12079 ctl_abort_task(union ctl_io *io) 12080 { 12081 struct ctl_softc *softc = CTL_SOFTC(io); 12082 struct ctl_io_hdr *xioh; 12083 struct ctl_lun *lun; 12084 uint32_t targ_lun; 12085 12086 /* 12087 * Look up the LUN. 12088 */ 12089 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12090 mtx_lock(&softc->ctl_lock); 12091 if (targ_lun >= ctl_max_luns || 12092 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12093 mtx_unlock(&softc->ctl_lock); 12094 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12095 return (1); 12096 } 12097 12098 mtx_lock(&lun->lun_lock); 12099 mtx_unlock(&softc->ctl_lock); 12100 /* 12101 * Run through the OOA queue and attempt to find the given I/O. 12102 * The target port, initiator ID, tag type and tag number have to 12103 * match the values that we got from the initiator. If we have an 12104 * untagged command to abort, simply abort the first untagged command 12105 * we come to. We only allow one untagged command at a time of course. 12106 */ 12107 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12108 union ctl_io *xio = (union ctl_io *)xioh; 12109 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12110 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12111 || (xioh->flags & CTL_FLAG_ABORT)) 12112 continue; 12113 12114 /* 12115 * If the abort says that the task is untagged, the 12116 * task in the queue must be untagged. Otherwise, 12117 * we just check to see whether the tag numbers 12118 * match. This is because the QLogic firmware 12119 * doesn't pass back the tag type in an abort 12120 * request. 12121 */ 12122 #if 0 12123 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12124 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12125 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12126 #else 12127 /* 12128 * XXX KDM we've got problems with FC, because it 12129 * doesn't send down a tag type with aborts. So we 12130 * can only really go by the tag number... 12131 * This may cause problems with parallel SCSI. 12132 * Need to figure that out!! 12133 */ 12134 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12135 #endif 12136 xioh->flags |= CTL_FLAG_ABORT; 12137 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12138 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12139 union ctl_ha_msg msg_info; 12140 12141 msg_info.hdr.nexus = io->io_hdr.nexus; 12142 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12143 msg_info.task.tag_num = io->taskio.tag_num; 12144 msg_info.task.tag_type = io->taskio.tag_type; 12145 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12146 msg_info.hdr.original_sc = NULL; 12147 msg_info.hdr.serializing_sc = NULL; 12148 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12149 sizeof(msg_info.task), M_NOWAIT); 12150 } 12151 ctl_try_unblock_io(lun, xio, FALSE); 12152 } 12153 } 12154 mtx_unlock(&lun->lun_lock); 12155 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12156 return (0); 12157 } 12158 12159 static int 12160 ctl_query_task(union ctl_io *io, int task_set) 12161 { 12162 struct ctl_softc *softc = CTL_SOFTC(io); 12163 struct ctl_io_hdr *xioh; 12164 struct ctl_lun *lun; 12165 int found = 0; 12166 uint32_t targ_lun; 12167 12168 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12169 mtx_lock(&softc->ctl_lock); 12170 if (targ_lun >= ctl_max_luns || 12171 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12172 mtx_unlock(&softc->ctl_lock); 12173 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12174 return (1); 12175 } 12176 mtx_lock(&lun->lun_lock); 12177 mtx_unlock(&softc->ctl_lock); 12178 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12179 union ctl_io *xio = (union ctl_io *)xioh; 12180 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12181 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12182 || (xioh->flags & CTL_FLAG_ABORT)) 12183 continue; 12184 12185 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12186 found = 1; 12187 break; 12188 } 12189 } 12190 mtx_unlock(&lun->lun_lock); 12191 if (found) 12192 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12193 else 12194 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12195 return (0); 12196 } 12197 12198 static int 12199 ctl_query_async_event(union ctl_io *io) 12200 { 12201 struct ctl_softc *softc = CTL_SOFTC(io); 12202 struct ctl_lun *lun; 12203 ctl_ua_type ua; 12204 uint32_t targ_lun, initidx; 12205 12206 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12207 mtx_lock(&softc->ctl_lock); 12208 if (targ_lun >= ctl_max_luns || 12209 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12210 mtx_unlock(&softc->ctl_lock); 12211 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12212 return (1); 12213 } 12214 mtx_lock(&lun->lun_lock); 12215 mtx_unlock(&softc->ctl_lock); 12216 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12217 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12218 mtx_unlock(&lun->lun_lock); 12219 if (ua != CTL_UA_NONE) 12220 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12221 else 12222 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12223 return (0); 12224 } 12225 12226 static void 12227 ctl_run_task(union ctl_io *io) 12228 { 12229 int retval = 1; 12230 12231 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12232 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12233 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12234 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12235 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12236 switch (io->taskio.task_action) { 12237 case CTL_TASK_ABORT_TASK: 12238 retval = ctl_abort_task(io); 12239 break; 12240 case CTL_TASK_ABORT_TASK_SET: 12241 case CTL_TASK_CLEAR_TASK_SET: 12242 retval = ctl_abort_task_set(io); 12243 break; 12244 case CTL_TASK_CLEAR_ACA: 12245 break; 12246 case CTL_TASK_I_T_NEXUS_RESET: 12247 retval = ctl_i_t_nexus_reset(io); 12248 break; 12249 case CTL_TASK_LUN_RESET: 12250 retval = ctl_lun_reset(io); 12251 break; 12252 case CTL_TASK_TARGET_RESET: 12253 case CTL_TASK_BUS_RESET: 12254 retval = ctl_target_reset(io); 12255 break; 12256 case CTL_TASK_PORT_LOGIN: 12257 break; 12258 case CTL_TASK_PORT_LOGOUT: 12259 break; 12260 case CTL_TASK_QUERY_TASK: 12261 retval = ctl_query_task(io, 0); 12262 break; 12263 case CTL_TASK_QUERY_TASK_SET: 12264 retval = ctl_query_task(io, 1); 12265 break; 12266 case CTL_TASK_QUERY_ASYNC_EVENT: 12267 retval = ctl_query_async_event(io); 12268 break; 12269 default: 12270 printf("%s: got unknown task management event %d\n", 12271 __func__, io->taskio.task_action); 12272 break; 12273 } 12274 if (retval == 0) 12275 io->io_hdr.status = CTL_SUCCESS; 12276 else 12277 io->io_hdr.status = CTL_ERROR; 12278 ctl_done(io); 12279 } 12280 12281 /* 12282 * For HA operation. Handle commands that come in from the other 12283 * controller. 12284 */ 12285 static void 12286 ctl_handle_isc(union ctl_io *io) 12287 { 12288 struct ctl_softc *softc = CTL_SOFTC(io); 12289 struct ctl_lun *lun; 12290 const struct ctl_cmd_entry *entry; 12291 uint32_t targ_lun; 12292 12293 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12294 switch (io->io_hdr.msg_type) { 12295 case CTL_MSG_SERIALIZE: 12296 ctl_serialize_other_sc_cmd(&io->scsiio); 12297 break; 12298 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12299 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12300 if (targ_lun >= ctl_max_luns || 12301 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12302 ctl_done(io); 12303 break; 12304 } 12305 mtx_lock(&lun->lun_lock); 12306 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12307 mtx_unlock(&lun->lun_lock); 12308 ctl_done(io); 12309 break; 12310 } 12311 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12312 mtx_unlock(&lun->lun_lock); 12313 ctl_enqueue_rtr(io); 12314 break; 12315 case CTL_MSG_FINISH_IO: 12316 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12317 ctl_done(io); 12318 break; 12319 } 12320 if (targ_lun >= ctl_max_luns || 12321 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12322 ctl_free_io(io); 12323 break; 12324 } 12325 mtx_lock(&lun->lun_lock); 12326 ctl_try_unblock_others(lun, io, TRUE); 12327 LIST_REMOVE(&io->io_hdr, ooa_links); 12328 mtx_unlock(&lun->lun_lock); 12329 ctl_free_io(io); 12330 break; 12331 case CTL_MSG_PERS_ACTION: 12332 ctl_hndl_per_res_out_on_other_sc(io); 12333 ctl_free_io(io); 12334 break; 12335 case CTL_MSG_BAD_JUJU: 12336 ctl_done(io); 12337 break; 12338 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12339 ctl_datamove_remote(io); 12340 break; 12341 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12342 ctl_datamove_done(io, false); 12343 break; 12344 case CTL_MSG_FAILOVER: 12345 ctl_failover_lun(io); 12346 ctl_free_io(io); 12347 break; 12348 default: 12349 printf("%s: Invalid message type %d\n", 12350 __func__, io->io_hdr.msg_type); 12351 ctl_free_io(io); 12352 break; 12353 } 12354 12355 } 12356 12357 /* 12358 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12359 * there is no match. 12360 */ 12361 static ctl_lun_error_pattern 12362 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12363 { 12364 const struct ctl_cmd_entry *entry; 12365 ctl_lun_error_pattern filtered_pattern, pattern; 12366 12367 pattern = desc->error_pattern; 12368 12369 /* 12370 * XXX KDM we need more data passed into this function to match a 12371 * custom pattern, and we actually need to implement custom pattern 12372 * matching. 12373 */ 12374 if (pattern & CTL_LUN_PAT_CMD) 12375 return (CTL_LUN_PAT_CMD); 12376 12377 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12378 return (CTL_LUN_PAT_ANY); 12379 12380 entry = ctl_get_cmd_entry(ctsio, NULL); 12381 12382 filtered_pattern = entry->pattern & pattern; 12383 12384 /* 12385 * If the user requested specific flags in the pattern (e.g. 12386 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12387 * flags. 12388 * 12389 * If the user did not specify any flags, it doesn't matter whether 12390 * or not the command supports the flags. 12391 */ 12392 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12393 (pattern & ~CTL_LUN_PAT_MASK)) 12394 return (CTL_LUN_PAT_NONE); 12395 12396 /* 12397 * If the user asked for a range check, see if the requested LBA 12398 * range overlaps with this command's LBA range. 12399 */ 12400 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12401 uint64_t lba1; 12402 uint64_t len1; 12403 ctl_action action; 12404 int retval; 12405 12406 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12407 if (retval != 0) 12408 return (CTL_LUN_PAT_NONE); 12409 12410 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12411 desc->lba_range.len, FALSE); 12412 /* 12413 * A "pass" means that the LBA ranges don't overlap, so 12414 * this doesn't match the user's range criteria. 12415 */ 12416 if (action == CTL_ACTION_PASS) 12417 return (CTL_LUN_PAT_NONE); 12418 } 12419 12420 return (filtered_pattern); 12421 } 12422 12423 static void 12424 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12425 { 12426 struct ctl_error_desc *desc, *desc2; 12427 12428 mtx_assert(&lun->lun_lock, MA_OWNED); 12429 12430 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12431 ctl_lun_error_pattern pattern; 12432 /* 12433 * Check to see whether this particular command matches 12434 * the pattern in the descriptor. 12435 */ 12436 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12437 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12438 continue; 12439 12440 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12441 case CTL_LUN_INJ_ABORTED: 12442 ctl_set_aborted(&io->scsiio); 12443 break; 12444 case CTL_LUN_INJ_MEDIUM_ERR: 12445 ctl_set_medium_error(&io->scsiio, 12446 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12447 CTL_FLAG_DATA_OUT); 12448 break; 12449 case CTL_LUN_INJ_UA: 12450 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12451 * OCCURRED */ 12452 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12453 break; 12454 case CTL_LUN_INJ_CUSTOM: 12455 /* 12456 * We're assuming the user knows what he is doing. 12457 * Just copy the sense information without doing 12458 * checks. 12459 */ 12460 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12461 MIN(sizeof(desc->custom_sense), 12462 sizeof(io->scsiio.sense_data))); 12463 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12464 io->scsiio.sense_len = SSD_FULL_SIZE; 12465 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12466 break; 12467 case CTL_LUN_INJ_NONE: 12468 default: 12469 /* 12470 * If this is an error injection type we don't know 12471 * about, clear the continuous flag (if it is set) 12472 * so it will get deleted below. 12473 */ 12474 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12475 break; 12476 } 12477 /* 12478 * By default, each error injection action is a one-shot 12479 */ 12480 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12481 continue; 12482 12483 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12484 12485 free(desc, M_CTL); 12486 } 12487 } 12488 12489 #ifdef CTL_IO_DELAY 12490 static void 12491 ctl_datamove_timer_wakeup(void *arg) 12492 { 12493 union ctl_io *io; 12494 12495 io = (union ctl_io *)arg; 12496 12497 ctl_datamove(io); 12498 } 12499 #endif /* CTL_IO_DELAY */ 12500 12501 static void 12502 ctl_datamove_done_process(union ctl_io *io) 12503 { 12504 #ifdef CTL_TIME_IO 12505 struct bintime cur_bt; 12506 #endif 12507 12508 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 12509 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 12510 12511 #ifdef CTL_TIME_IO 12512 getbinuptime(&cur_bt); 12513 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12514 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12515 #endif 12516 io->io_hdr.num_dmas++; 12517 12518 if ((io->io_hdr.port_status != 0) && 12519 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12520 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12521 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 12522 /*retry_count*/ io->io_hdr.port_status); 12523 } else if (io->scsiio.kern_data_resid != 0 && 12524 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 12525 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12526 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12527 ctl_set_invalid_field_ciu(&io->scsiio); 12528 } else if (ctl_debug & CTL_DEBUG_CDB_DATA) 12529 ctl_data_print(io); 12530 } 12531 12532 void 12533 ctl_datamove_done(union ctl_io *io, bool samethr) 12534 { 12535 12536 ctl_datamove_done_process(io); 12537 io->scsiio.be_move_done(io, samethr); 12538 } 12539 12540 void 12541 ctl_datamove(union ctl_io *io) 12542 { 12543 void (*fe_datamove)(union ctl_io *io); 12544 12545 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12546 12547 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12548 12549 /* No data transferred yet. Frontend must update this when done. */ 12550 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12551 12552 #ifdef CTL_TIME_IO 12553 getbinuptime(&io->io_hdr.dma_start_bt); 12554 #endif /* CTL_TIME_IO */ 12555 12556 #ifdef CTL_IO_DELAY 12557 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12558 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12559 } else { 12560 struct ctl_lun *lun; 12561 12562 lun = CTL_LUN(io); 12563 if ((lun != NULL) 12564 && (lun->delay_info.datamove_delay > 0)) { 12565 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12566 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12567 callout_reset(&io->io_hdr.delay_callout, 12568 lun->delay_info.datamove_delay * hz, 12569 ctl_datamove_timer_wakeup, io); 12570 if (lun->delay_info.datamove_type == 12571 CTL_DELAY_TYPE_ONESHOT) 12572 lun->delay_info.datamove_delay = 0; 12573 return; 12574 } 12575 } 12576 #endif 12577 12578 /* 12579 * This command has been aborted. Set the port status, so we fail 12580 * the data move. 12581 */ 12582 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12583 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12584 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12585 io->io_hdr.nexus.targ_port, 12586 io->io_hdr.nexus.targ_lun); 12587 io->io_hdr.port_status = 31337; 12588 ctl_datamove_done_process(io); 12589 io->scsiio.be_move_done(io, true); 12590 return; 12591 } 12592 12593 /* Don't confuse frontend with zero length data move. */ 12594 if (io->scsiio.kern_data_len == 0) { 12595 ctl_datamove_done_process(io); 12596 io->scsiio.be_move_done(io, true); 12597 return; 12598 } 12599 12600 fe_datamove = CTL_PORT(io)->fe_datamove; 12601 fe_datamove(io); 12602 } 12603 12604 static void 12605 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12606 { 12607 union ctl_ha_msg msg; 12608 #ifdef CTL_TIME_IO 12609 struct bintime cur_bt; 12610 #endif 12611 12612 memset(&msg, 0, sizeof(msg)); 12613 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12614 msg.hdr.original_sc = io; 12615 msg.hdr.serializing_sc = io->io_hdr.remote_io; 12616 msg.hdr.nexus = io->io_hdr.nexus; 12617 msg.hdr.status = io->io_hdr.status; 12618 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12619 msg.scsi.tag_num = io->scsiio.tag_num; 12620 msg.scsi.tag_type = io->scsiio.tag_type; 12621 msg.scsi.scsi_status = io->scsiio.scsi_status; 12622 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12623 io->scsiio.sense_len); 12624 msg.scsi.sense_len = io->scsiio.sense_len; 12625 msg.scsi.port_status = io->io_hdr.port_status; 12626 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12627 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12628 ctl_failover_io(io, /*have_lock*/ have_lock); 12629 return; 12630 } 12631 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12632 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12633 msg.scsi.sense_len, M_WAITOK); 12634 12635 #ifdef CTL_TIME_IO 12636 getbinuptime(&cur_bt); 12637 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12638 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12639 #endif 12640 io->io_hdr.num_dmas++; 12641 } 12642 12643 /* 12644 * The DMA to the remote side is done, now we need to tell the other side 12645 * we're done so it can continue with its data movement. 12646 */ 12647 static void 12648 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12649 { 12650 union ctl_io *io; 12651 uint32_t i; 12652 12653 io = rq->context; 12654 12655 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12656 printf("%s: ISC DMA write failed with error %d", __func__, 12657 rq->ret); 12658 ctl_set_internal_failure(&io->scsiio, 12659 /*sks_valid*/ 1, 12660 /*retry_count*/ rq->ret); 12661 } 12662 12663 ctl_dt_req_free(rq); 12664 12665 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12666 free(CTL_LSGLT(io)[i].addr, M_CTL); 12667 free(CTL_RSGL(io), M_CTL); 12668 CTL_RSGL(io) = NULL; 12669 CTL_LSGL(io) = NULL; 12670 12671 /* 12672 * The data is in local and remote memory, so now we need to send 12673 * status (good or back) back to the other side. 12674 */ 12675 ctl_send_datamove_done(io, /*have_lock*/ 0); 12676 } 12677 12678 /* 12679 * We've moved the data from the host/controller into local memory. Now we 12680 * need to push it over to the remote controller's memory. 12681 */ 12682 static int 12683 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr) 12684 { 12685 int retval; 12686 12687 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12688 ctl_datamove_remote_write_cb); 12689 return (retval); 12690 } 12691 12692 static void 12693 ctl_datamove_remote_write(union ctl_io *io) 12694 { 12695 int retval; 12696 void (*fe_datamove)(union ctl_io *io); 12697 12698 /* 12699 * - Get the data from the host/HBA into local memory. 12700 * - DMA memory from the local controller to the remote controller. 12701 * - Send status back to the remote controller. 12702 */ 12703 12704 retval = ctl_datamove_remote_sgl_setup(io); 12705 if (retval != 0) 12706 return; 12707 12708 /* Switch the pointer over so the FETD knows what to do */ 12709 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12710 12711 /* 12712 * Use a custom move done callback, since we need to send completion 12713 * back to the other controller, not to the backend on this side. 12714 */ 12715 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12716 12717 fe_datamove = CTL_PORT(io)->fe_datamove; 12718 fe_datamove(io); 12719 } 12720 12721 static int 12722 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr) 12723 { 12724 uint32_t i; 12725 12726 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12727 free(CTL_LSGLT(io)[i].addr, M_CTL); 12728 free(CTL_RSGL(io), M_CTL); 12729 CTL_RSGL(io) = NULL; 12730 CTL_LSGL(io) = NULL; 12731 12732 /* 12733 * The read is done, now we need to send status (good or bad) back 12734 * to the other side. 12735 */ 12736 ctl_send_datamove_done(io, /*have_lock*/ 0); 12737 12738 return (0); 12739 } 12740 12741 static void 12742 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12743 { 12744 union ctl_io *io; 12745 void (*fe_datamove)(union ctl_io *io); 12746 12747 io = rq->context; 12748 12749 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12750 printf("%s: ISC DMA read failed with error %d\n", __func__, 12751 rq->ret); 12752 ctl_set_internal_failure(&io->scsiio, 12753 /*sks_valid*/ 1, 12754 /*retry_count*/ rq->ret); 12755 } 12756 12757 ctl_dt_req_free(rq); 12758 12759 /* Switch the pointer over so the FETD knows what to do */ 12760 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12761 12762 /* 12763 * Use a custom move done callback, since we need to send completion 12764 * back to the other controller, not to the backend on this side. 12765 */ 12766 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12767 12768 /* XXX KDM add checks like the ones in ctl_datamove? */ 12769 12770 fe_datamove = CTL_PORT(io)->fe_datamove; 12771 fe_datamove(io); 12772 } 12773 12774 static int 12775 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12776 { 12777 struct ctl_sg_entry *local_sglist; 12778 uint32_t len_to_go; 12779 int retval; 12780 int i; 12781 12782 retval = 0; 12783 local_sglist = CTL_LSGL(io); 12784 len_to_go = io->scsiio.kern_data_len; 12785 12786 /* 12787 * The difficult thing here is that the size of the various 12788 * S/G segments may be different than the size from the 12789 * remote controller. That'll make it harder when DMAing 12790 * the data back to the other side. 12791 */ 12792 for (i = 0; len_to_go > 0; i++) { 12793 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12794 local_sglist[i].addr = 12795 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12796 12797 len_to_go -= local_sglist[i].len; 12798 } 12799 /* 12800 * Reset the number of S/G entries accordingly. The original 12801 * number of S/G entries is available in rem_sg_entries. 12802 */ 12803 io->scsiio.kern_sg_entries = i; 12804 12805 return (retval); 12806 } 12807 12808 static int 12809 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12810 ctl_ha_dt_cb callback) 12811 { 12812 struct ctl_ha_dt_req *rq; 12813 struct ctl_sg_entry *remote_sglist, *local_sglist; 12814 uint32_t local_used, remote_used, total_used; 12815 int i, j, isc_ret; 12816 12817 rq = ctl_dt_req_alloc(); 12818 12819 /* 12820 * If we failed to allocate the request, and if the DMA didn't fail 12821 * anyway, set busy status. This is just a resource allocation 12822 * failure. 12823 */ 12824 if ((rq == NULL) 12825 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12826 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12827 ctl_set_busy(&io->scsiio); 12828 12829 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12830 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12831 if (rq != NULL) 12832 ctl_dt_req_free(rq); 12833 12834 /* 12835 * The data move failed. We need to return status back 12836 * to the other controller. No point in trying to DMA 12837 * data to the remote controller. 12838 */ 12839 12840 ctl_send_datamove_done(io, /*have_lock*/ 0); 12841 12842 return (1); 12843 } 12844 12845 local_sglist = CTL_LSGL(io); 12846 remote_sglist = CTL_RSGL(io); 12847 local_used = 0; 12848 remote_used = 0; 12849 total_used = 0; 12850 12851 /* 12852 * Pull/push the data over the wire from/to the other controller. 12853 * This takes into account the possibility that the local and 12854 * remote sglists may not be identical in terms of the size of 12855 * the elements and the number of elements. 12856 * 12857 * One fundamental assumption here is that the length allocated for 12858 * both the local and remote sglists is identical. Otherwise, we've 12859 * essentially got a coding error of some sort. 12860 */ 12861 isc_ret = CTL_HA_STATUS_SUCCESS; 12862 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12863 uint32_t cur_len; 12864 uint8_t *tmp_ptr; 12865 12866 rq->command = command; 12867 rq->context = io; 12868 12869 /* 12870 * Both pointers should be aligned. But it is possible 12871 * that the allocation length is not. They should both 12872 * also have enough slack left over at the end, though, 12873 * to round up to the next 8 byte boundary. 12874 */ 12875 cur_len = MIN(local_sglist[i].len - local_used, 12876 remote_sglist[j].len - remote_used); 12877 rq->size = cur_len; 12878 12879 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12880 tmp_ptr += local_used; 12881 12882 #if 0 12883 /* Use physical addresses when talking to ISC hardware */ 12884 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12885 /* XXX KDM use busdma */ 12886 rq->local = vtophys(tmp_ptr); 12887 } else 12888 rq->local = tmp_ptr; 12889 #else 12890 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12891 ("HA does not support BUS_ADDR")); 12892 rq->local = tmp_ptr; 12893 #endif 12894 12895 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12896 tmp_ptr += remote_used; 12897 rq->remote = tmp_ptr; 12898 12899 rq->callback = NULL; 12900 12901 local_used += cur_len; 12902 if (local_used >= local_sglist[i].len) { 12903 i++; 12904 local_used = 0; 12905 } 12906 12907 remote_used += cur_len; 12908 if (remote_used >= remote_sglist[j].len) { 12909 j++; 12910 remote_used = 0; 12911 } 12912 total_used += cur_len; 12913 12914 if (total_used >= io->scsiio.kern_data_len) 12915 rq->callback = callback; 12916 12917 isc_ret = ctl_dt_single(rq); 12918 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12919 break; 12920 } 12921 if (isc_ret != CTL_HA_STATUS_WAIT) { 12922 rq->ret = isc_ret; 12923 callback(rq); 12924 } 12925 12926 return (0); 12927 } 12928 12929 static void 12930 ctl_datamove_remote_read(union ctl_io *io) 12931 { 12932 int retval; 12933 uint32_t i; 12934 12935 /* 12936 * This will send an error to the other controller in the case of a 12937 * failure. 12938 */ 12939 retval = ctl_datamove_remote_sgl_setup(io); 12940 if (retval != 0) 12941 return; 12942 12943 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12944 ctl_datamove_remote_read_cb); 12945 if (retval != 0) { 12946 /* 12947 * Make sure we free memory if there was an error.. The 12948 * ctl_datamove_remote_xfer() function will send the 12949 * datamove done message, or call the callback with an 12950 * error if there is a problem. 12951 */ 12952 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12953 free(CTL_LSGLT(io)[i].addr, M_CTL); 12954 free(CTL_RSGL(io), M_CTL); 12955 CTL_RSGL(io) = NULL; 12956 CTL_LSGL(io) = NULL; 12957 } 12958 } 12959 12960 /* 12961 * Process a datamove request from the other controller. This is used for 12962 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12963 * first. Once that is complete, the data gets DMAed into the remote 12964 * controller's memory. For reads, we DMA from the remote controller's 12965 * memory into our memory first, and then move it out to the FETD. 12966 */ 12967 static void 12968 ctl_datamove_remote(union ctl_io *io) 12969 { 12970 12971 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12972 12973 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12974 ctl_failover_io(io, /*have_lock*/ 0); 12975 return; 12976 } 12977 12978 /* 12979 * Note that we look for an aborted I/O here, but don't do some of 12980 * the other checks that ctl_datamove() normally does. 12981 * We don't need to run the datamove delay code, since that should 12982 * have been done if need be on the other controller. 12983 */ 12984 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12985 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12986 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12987 io->io_hdr.nexus.targ_port, 12988 io->io_hdr.nexus.targ_lun); 12989 io->io_hdr.port_status = 31338; 12990 ctl_send_datamove_done(io, /*have_lock*/ 0); 12991 return; 12992 } 12993 12994 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12995 ctl_datamove_remote_write(io); 12996 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12997 ctl_datamove_remote_read(io); 12998 else { 12999 io->io_hdr.port_status = 31339; 13000 ctl_send_datamove_done(io, /*have_lock*/ 0); 13001 } 13002 } 13003 13004 static void 13005 ctl_process_done(union ctl_io *io) 13006 { 13007 struct ctl_softc *softc = CTL_SOFTC(io); 13008 struct ctl_port *port = CTL_PORT(io); 13009 struct ctl_lun *lun = CTL_LUN(io); 13010 void (*fe_done)(union ctl_io *io); 13011 union ctl_ha_msg msg; 13012 13013 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13014 fe_done = port->fe_done; 13015 13016 #ifdef CTL_TIME_IO 13017 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13018 char str[256]; 13019 char path_str[64]; 13020 struct sbuf sb; 13021 13022 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13023 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13024 13025 sbuf_cat(&sb, path_str); 13026 switch (io->io_hdr.io_type) { 13027 case CTL_IO_SCSI: 13028 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13029 sbuf_printf(&sb, "\n"); 13030 sbuf_cat(&sb, path_str); 13031 sbuf_printf(&sb, "Tag: 0x%04x/%d, Prio: %d\n", 13032 io->scsiio.tag_num, io->scsiio.tag_type, 13033 io->scsiio.priority); 13034 break; 13035 case CTL_IO_TASK: 13036 sbuf_printf(&sb, "Task Action: %d Tag: 0x%04x/%d\n", 13037 io->taskio.task_action, 13038 io->taskio.tag_num, io->taskio.tag_type); 13039 break; 13040 default: 13041 panic("%s: Invalid CTL I/O type %d\n", 13042 __func__, io->io_hdr.io_type); 13043 } 13044 sbuf_cat(&sb, path_str); 13045 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13046 (intmax_t)time_uptime - io->io_hdr.start_time); 13047 sbuf_finish(&sb); 13048 printf("%s", sbuf_data(&sb)); 13049 } 13050 #endif /* CTL_TIME_IO */ 13051 13052 switch (io->io_hdr.io_type) { 13053 case CTL_IO_SCSI: 13054 break; 13055 case CTL_IO_TASK: 13056 if (ctl_debug & CTL_DEBUG_INFO) 13057 ctl_io_error_print(io, NULL); 13058 fe_done(io); 13059 return; 13060 default: 13061 panic("%s: Invalid CTL I/O type %d\n", 13062 __func__, io->io_hdr.io_type); 13063 } 13064 13065 if (lun == NULL) { 13066 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13067 io->io_hdr.nexus.targ_mapped_lun)); 13068 goto bailout; 13069 } 13070 13071 mtx_lock(&lun->lun_lock); 13072 13073 /* 13074 * Check to see if we have any informational exception and status 13075 * of this command can be modified to report it in form of either 13076 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13077 */ 13078 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13079 io->io_hdr.status == CTL_SUCCESS && 13080 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13081 uint8_t mrie = lun->MODE_IE.mrie; 13082 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13083 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13084 if (((mrie == SIEP_MRIE_REC_COND && per) || 13085 mrie == SIEP_MRIE_REC_UNCOND || 13086 mrie == SIEP_MRIE_NO_SENSE) && 13087 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13088 CTL_CMD_FLAG_NO_SENSE) == 0) { 13089 ctl_set_sense(&io->scsiio, 13090 /*current_error*/ 1, 13091 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13092 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13093 /*asc*/ lun->ie_asc, 13094 /*ascq*/ lun->ie_ascq, 13095 SSD_ELEM_NONE); 13096 lun->ie_reported = 1; 13097 } 13098 } else if (lun->ie_reported < 0) 13099 lun->ie_reported = 0; 13100 13101 /* 13102 * Check to see if we have any errors to inject here. We only 13103 * inject errors for commands that don't already have errors set. 13104 */ 13105 if (!STAILQ_EMPTY(&lun->error_list) && 13106 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13107 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13108 ctl_inject_error(lun, io); 13109 13110 /* 13111 * XXX KDM how do we treat commands that aren't completed 13112 * successfully? 13113 * 13114 * XXX KDM should we also track I/O latency? 13115 */ 13116 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13117 io->io_hdr.io_type == CTL_IO_SCSI) { 13118 int type; 13119 #ifdef CTL_TIME_IO 13120 struct bintime bt; 13121 13122 getbinuptime(&bt); 13123 bintime_sub(&bt, &io->io_hdr.start_bt); 13124 #endif 13125 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13126 CTL_FLAG_DATA_IN) 13127 type = CTL_STATS_READ; 13128 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13129 CTL_FLAG_DATA_OUT) 13130 type = CTL_STATS_WRITE; 13131 else 13132 type = CTL_STATS_NO_IO; 13133 13134 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13135 lun->stats.operations[type] ++; 13136 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13137 #ifdef CTL_TIME_IO 13138 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13139 bintime_add(&lun->stats.time[type], &bt); 13140 #endif 13141 13142 mtx_lock(&port->port_lock); 13143 port->stats.bytes[type] += io->scsiio.kern_total_len; 13144 port->stats.operations[type] ++; 13145 port->stats.dmas[type] += io->io_hdr.num_dmas; 13146 #ifdef CTL_TIME_IO 13147 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13148 bintime_add(&port->stats.time[type], &bt); 13149 #endif 13150 mtx_unlock(&port->port_lock); 13151 } 13152 13153 /* 13154 * Run through the blocked queue of this I/O and see if anything 13155 * can be unblocked, now that this I/O is done and will be removed. 13156 * We need to do it before removal to have OOA position to start. 13157 */ 13158 ctl_try_unblock_others(lun, io, TRUE); 13159 13160 /* 13161 * Remove this from the OOA queue. 13162 */ 13163 LIST_REMOVE(&io->io_hdr, ooa_links); 13164 #ifdef CTL_TIME_IO 13165 if (LIST_EMPTY(&lun->ooa_queue)) 13166 lun->last_busy = getsbinuptime(); 13167 #endif 13168 13169 /* 13170 * If the LUN has been invalidated, free it if there is nothing 13171 * left on its OOA queue. 13172 */ 13173 if ((lun->flags & CTL_LUN_INVALID) 13174 && LIST_EMPTY(&lun->ooa_queue)) { 13175 mtx_unlock(&lun->lun_lock); 13176 ctl_free_lun(lun); 13177 } else 13178 mtx_unlock(&lun->lun_lock); 13179 13180 bailout: 13181 13182 /* 13183 * If this command has been aborted, make sure we set the status 13184 * properly. The FETD is responsible for freeing the I/O and doing 13185 * whatever it needs to do to clean up its state. 13186 */ 13187 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13188 ctl_set_task_aborted(&io->scsiio); 13189 13190 /* 13191 * If enabled, print command error status. 13192 */ 13193 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13194 (ctl_debug & CTL_DEBUG_INFO) != 0) 13195 ctl_io_error_print(io, NULL); 13196 13197 /* 13198 * Tell the FETD or the other shelf controller we're done with this 13199 * command. Note that only SCSI commands get to this point. Task 13200 * management commands are completed above. 13201 */ 13202 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13203 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13204 memset(&msg, 0, sizeof(msg)); 13205 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13206 msg.hdr.serializing_sc = io->io_hdr.remote_io; 13207 msg.hdr.nexus = io->io_hdr.nexus; 13208 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13209 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13210 M_WAITOK); 13211 } 13212 13213 fe_done(io); 13214 } 13215 13216 /* 13217 * Front end should call this if it doesn't do autosense. When the request 13218 * sense comes back in from the initiator, we'll dequeue this and send it. 13219 */ 13220 int 13221 ctl_queue_sense(union ctl_io *io) 13222 { 13223 struct ctl_softc *softc = CTL_SOFTC(io); 13224 struct ctl_port *port = CTL_PORT(io); 13225 struct ctl_lun *lun; 13226 struct scsi_sense_data *ps; 13227 uint32_t initidx, p, targ_lun; 13228 13229 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13230 13231 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13232 13233 /* 13234 * LUN lookup will likely move to the ctl_work_thread() once we 13235 * have our new queueing infrastructure (that doesn't put things on 13236 * a per-LUN queue initially). That is so that we can handle 13237 * things like an INQUIRY to a LUN that we don't have enabled. We 13238 * can't deal with that right now. 13239 * If we don't have a LUN for this, just toss the sense information. 13240 */ 13241 mtx_lock(&softc->ctl_lock); 13242 if (targ_lun >= ctl_max_luns || 13243 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13244 mtx_unlock(&softc->ctl_lock); 13245 goto bailout; 13246 } 13247 mtx_lock(&lun->lun_lock); 13248 mtx_unlock(&softc->ctl_lock); 13249 13250 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13251 p = initidx / CTL_MAX_INIT_PER_PORT; 13252 if (lun->pending_sense[p] == NULL) { 13253 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13254 M_CTL, M_NOWAIT | M_ZERO); 13255 } 13256 if ((ps = lun->pending_sense[p]) != NULL) { 13257 ps += initidx % CTL_MAX_INIT_PER_PORT; 13258 memset(ps, 0, sizeof(*ps)); 13259 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13260 } 13261 mtx_unlock(&lun->lun_lock); 13262 13263 bailout: 13264 ctl_free_io(io); 13265 return (CTL_RETVAL_COMPLETE); 13266 } 13267 13268 /* 13269 * Primary command inlet from frontend ports. All SCSI and task I/O 13270 * requests must go through this function. 13271 */ 13272 int 13273 ctl_queue(union ctl_io *io) 13274 { 13275 struct ctl_port *port = CTL_PORT(io); 13276 13277 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13278 13279 #ifdef CTL_TIME_IO 13280 io->io_hdr.start_time = time_uptime; 13281 getbinuptime(&io->io_hdr.start_bt); 13282 #endif /* CTL_TIME_IO */ 13283 13284 /* Map FE-specific LUN ID into global one. */ 13285 io->io_hdr.nexus.targ_mapped_lun = 13286 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13287 13288 switch (io->io_hdr.io_type) { 13289 case CTL_IO_SCSI: 13290 case CTL_IO_TASK: 13291 if (ctl_debug & CTL_DEBUG_CDB) 13292 ctl_io_print(io); 13293 ctl_enqueue_incoming(io); 13294 break; 13295 default: 13296 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13297 return (EINVAL); 13298 } 13299 13300 return (CTL_RETVAL_COMPLETE); 13301 } 13302 13303 int 13304 ctl_run(union ctl_io *io) 13305 { 13306 struct ctl_port *port = CTL_PORT(io); 13307 13308 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0])); 13309 13310 #ifdef CTL_TIME_IO 13311 io->io_hdr.start_time = time_uptime; 13312 getbinuptime(&io->io_hdr.start_bt); 13313 #endif /* CTL_TIME_IO */ 13314 13315 /* Map FE-specific LUN ID into global one. */ 13316 io->io_hdr.nexus.targ_mapped_lun = 13317 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13318 13319 switch (io->io_hdr.io_type) { 13320 case CTL_IO_SCSI: 13321 if (ctl_debug & CTL_DEBUG_CDB) 13322 ctl_io_print(io); 13323 ctl_scsiio_precheck(&io->scsiio); 13324 break; 13325 case CTL_IO_TASK: 13326 if (ctl_debug & CTL_DEBUG_CDB) 13327 ctl_io_print(io); 13328 ctl_run_task(io); 13329 break; 13330 default: 13331 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type); 13332 return (EINVAL); 13333 } 13334 13335 return (CTL_RETVAL_COMPLETE); 13336 } 13337 13338 #ifdef CTL_IO_DELAY 13339 static void 13340 ctl_done_timer_wakeup(void *arg) 13341 { 13342 union ctl_io *io; 13343 13344 io = (union ctl_io *)arg; 13345 ctl_done(io); 13346 } 13347 #endif /* CTL_IO_DELAY */ 13348 13349 void 13350 ctl_serseq_done(union ctl_io *io) 13351 { 13352 struct ctl_lun *lun = CTL_LUN(io); 13353 13354 /* This is racy, but should not be a problem. */ 13355 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) { 13356 mtx_lock(&lun->lun_lock); 13357 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13358 ctl_try_unblock_others(lun, io, FALSE); 13359 mtx_unlock(&lun->lun_lock); 13360 } else 13361 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13362 } 13363 13364 void 13365 ctl_done(union ctl_io *io) 13366 { 13367 13368 /* 13369 * Enable this to catch duplicate completion issues. 13370 */ 13371 #if 0 13372 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13373 printf("%s: type %d msg %d cdb %x iptl: " 13374 "%u:%u:%u tag 0x%04x " 13375 "flag %#x status %x\n", 13376 __func__, 13377 io->io_hdr.io_type, 13378 io->io_hdr.msg_type, 13379 io->scsiio.cdb[0], 13380 io->io_hdr.nexus.initid, 13381 io->io_hdr.nexus.targ_port, 13382 io->io_hdr.nexus.targ_lun, 13383 (io->io_hdr.io_type == 13384 CTL_IO_TASK) ? 13385 io->taskio.tag_num : 13386 io->scsiio.tag_num, 13387 io->io_hdr.flags, 13388 io->io_hdr.status); 13389 } else 13390 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13391 #endif 13392 13393 /* 13394 * This is an internal copy of an I/O, and should not go through 13395 * the normal done processing logic. 13396 */ 13397 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13398 return; 13399 13400 #ifdef CTL_IO_DELAY 13401 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13402 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13403 } else { 13404 struct ctl_lun *lun = CTL_LUN(io); 13405 13406 if ((lun != NULL) 13407 && (lun->delay_info.done_delay > 0)) { 13408 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13409 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13410 callout_reset(&io->io_hdr.delay_callout, 13411 lun->delay_info.done_delay * hz, 13412 ctl_done_timer_wakeup, io); 13413 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13414 lun->delay_info.done_delay = 0; 13415 return; 13416 } 13417 } 13418 #endif /* CTL_IO_DELAY */ 13419 13420 ctl_enqueue_done(io); 13421 } 13422 13423 static void 13424 ctl_work_thread(void *arg) 13425 { 13426 struct ctl_thread *thr = (struct ctl_thread *)arg; 13427 struct ctl_softc *softc = thr->ctl_softc; 13428 union ctl_io *io; 13429 int retval; 13430 13431 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13432 thread_lock(curthread); 13433 sched_prio(curthread, PUSER - 1); 13434 thread_unlock(curthread); 13435 13436 while (!softc->shutdown) { 13437 /* 13438 * We handle the queues in this order: 13439 * - ISC 13440 * - done queue (to free up resources, unblock other commands) 13441 * - incoming queue 13442 * - RtR queue 13443 * 13444 * If those queues are empty, we break out of the loop and 13445 * go to sleep. 13446 */ 13447 mtx_lock(&thr->queue_lock); 13448 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13449 if (io != NULL) { 13450 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13451 mtx_unlock(&thr->queue_lock); 13452 ctl_handle_isc(io); 13453 continue; 13454 } 13455 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13456 if (io != NULL) { 13457 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13458 /* clear any blocked commands, call fe_done */ 13459 mtx_unlock(&thr->queue_lock); 13460 ctl_process_done(io); 13461 continue; 13462 } 13463 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13464 if (io != NULL) { 13465 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13466 mtx_unlock(&thr->queue_lock); 13467 if (io->io_hdr.io_type == CTL_IO_TASK) 13468 ctl_run_task(io); 13469 else 13470 ctl_scsiio_precheck(&io->scsiio); 13471 continue; 13472 } 13473 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13474 if (io != NULL) { 13475 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13476 mtx_unlock(&thr->queue_lock); 13477 retval = ctl_scsiio(&io->scsiio); 13478 if (retval != CTL_RETVAL_COMPLETE) 13479 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13480 continue; 13481 } 13482 13483 /* Sleep until we have something to do. */ 13484 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0); 13485 } 13486 thr->thread = NULL; 13487 kthread_exit(); 13488 } 13489 13490 static void 13491 ctl_thresh_thread(void *arg) 13492 { 13493 struct ctl_softc *softc = (struct ctl_softc *)arg; 13494 struct ctl_lun *lun; 13495 struct ctl_logical_block_provisioning_page *page; 13496 const char *attr; 13497 union ctl_ha_msg msg; 13498 uint64_t thres, val; 13499 int i, e, set; 13500 13501 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13502 thread_lock(curthread); 13503 sched_prio(curthread, PUSER - 1); 13504 thread_unlock(curthread); 13505 13506 while (!softc->shutdown) { 13507 mtx_lock(&softc->ctl_lock); 13508 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13509 if ((lun->flags & CTL_LUN_DISABLED) || 13510 (lun->flags & CTL_LUN_NO_MEDIA) || 13511 lun->backend->lun_attr == NULL) 13512 continue; 13513 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13514 softc->ha_mode == CTL_HA_MODE_XFER) 13515 continue; 13516 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13517 continue; 13518 e = 0; 13519 page = &lun->MODE_LBP; 13520 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13521 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13522 continue; 13523 thres = scsi_4btoul(page->descr[i].count); 13524 thres <<= CTL_LBP_EXPONENT; 13525 switch (page->descr[i].resource) { 13526 case 0x01: 13527 attr = "blocksavail"; 13528 break; 13529 case 0x02: 13530 attr = "blocksused"; 13531 break; 13532 case 0xf1: 13533 attr = "poolblocksavail"; 13534 break; 13535 case 0xf2: 13536 attr = "poolblocksused"; 13537 break; 13538 default: 13539 continue; 13540 } 13541 mtx_unlock(&softc->ctl_lock); // XXX 13542 val = lun->backend->lun_attr(lun->be_lun, attr); 13543 mtx_lock(&softc->ctl_lock); 13544 if (val == UINT64_MAX) 13545 continue; 13546 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13547 == SLBPPD_ARMING_INC) 13548 e = (val >= thres); 13549 else 13550 e = (val <= thres); 13551 if (e) 13552 break; 13553 } 13554 mtx_lock(&lun->lun_lock); 13555 if (e) { 13556 scsi_u64to8b((uint8_t *)&page->descr[i] - 13557 (uint8_t *)page, lun->ua_tpt_info); 13558 if (lun->lasttpt == 0 || 13559 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13560 lun->lasttpt = time_uptime; 13561 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13562 set = 1; 13563 } else 13564 set = 0; 13565 } else { 13566 lun->lasttpt = 0; 13567 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13568 set = -1; 13569 } 13570 mtx_unlock(&lun->lun_lock); 13571 if (set != 0 && 13572 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13573 /* Send msg to other side. */ 13574 bzero(&msg.ua, sizeof(msg.ua)); 13575 msg.hdr.msg_type = CTL_MSG_UA; 13576 msg.hdr.nexus.initid = -1; 13577 msg.hdr.nexus.targ_port = -1; 13578 msg.hdr.nexus.targ_lun = lun->lun; 13579 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13580 msg.ua.ua_all = 1; 13581 msg.ua.ua_set = (set > 0); 13582 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13583 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13584 mtx_unlock(&softc->ctl_lock); // XXX 13585 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13586 sizeof(msg.ua), M_WAITOK); 13587 mtx_lock(&softc->ctl_lock); 13588 } 13589 } 13590 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13591 PDROP, "-", CTL_LBP_PERIOD * hz); 13592 } 13593 softc->thresh_thread = NULL; 13594 kthread_exit(); 13595 } 13596 13597 static void 13598 ctl_enqueue_incoming(union ctl_io *io) 13599 { 13600 struct ctl_softc *softc = CTL_SOFTC(io); 13601 struct ctl_thread *thr; 13602 u_int idx; 13603 13604 idx = (io->io_hdr.nexus.targ_port * 127 + 13605 io->io_hdr.nexus.initid) % worker_threads; 13606 thr = &softc->threads[idx]; 13607 mtx_lock(&thr->queue_lock); 13608 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13609 mtx_unlock(&thr->queue_lock); 13610 wakeup(thr); 13611 } 13612 13613 static void 13614 ctl_enqueue_rtr(union ctl_io *io) 13615 { 13616 struct ctl_softc *softc = CTL_SOFTC(io); 13617 struct ctl_thread *thr; 13618 13619 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13620 mtx_lock(&thr->queue_lock); 13621 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13622 mtx_unlock(&thr->queue_lock); 13623 wakeup(thr); 13624 } 13625 13626 static void 13627 ctl_enqueue_done(union ctl_io *io) 13628 { 13629 struct ctl_softc *softc = CTL_SOFTC(io); 13630 struct ctl_thread *thr; 13631 13632 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13633 mtx_lock(&thr->queue_lock); 13634 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13635 mtx_unlock(&thr->queue_lock); 13636 wakeup(thr); 13637 } 13638 13639 static void 13640 ctl_enqueue_isc(union ctl_io *io) 13641 { 13642 struct ctl_softc *softc = CTL_SOFTC(io); 13643 struct ctl_thread *thr; 13644 13645 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13646 mtx_lock(&thr->queue_lock); 13647 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13648 mtx_unlock(&thr->queue_lock); 13649 wakeup(thr); 13650 } 13651 13652 /* 13653 * vim: ts=8 13654 */ 13655