1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 5 * Copyright (c) 2012 The FreeBSD Foundation 6 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org> 7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org> 8 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org> 9 * All rights reserved. 10 * 11 * Portions of this software were developed by Edward Tomasz Napierala 12 * under sponsorship from the FreeBSD Foundation. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions, and the following disclaimer, 19 * without modification. 20 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 21 * substantially similar to the "NO WARRANTY" disclaimer below 22 * ("Disclaimer") and any redistribution must be conditioned upon 23 * including a substantially similar Disclaimer requirement for further 24 * binary redistribution. 25 * 26 * NO WARRANTY 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGES. 38 * 39 * $Id$ 40 */ 41 /* 42 * CAM Target Layer, a SCSI device emulation subsystem. 43 * 44 * Author: Ken Merry <ken@FreeBSD.org> 45 */ 46 47 #include <sys/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/ctype.h> 53 #include <sys/kernel.h> 54 #include <sys/types.h> 55 #include <sys/kthread.h> 56 #include <sys/bio.h> 57 #include <sys/fcntl.h> 58 #include <sys/lock.h> 59 #include <sys/module.h> 60 #include <sys/mutex.h> 61 #include <sys/condvar.h> 62 #include <sys/malloc.h> 63 #include <sys/conf.h> 64 #include <sys/ioccom.h> 65 #include <sys/queue.h> 66 #include <sys/sbuf.h> 67 #include <sys/smp.h> 68 #include <sys/endian.h> 69 #include <sys/proc.h> 70 #include <sys/sched.h> 71 #include <sys/sysctl.h> 72 #include <sys/nv.h> 73 #include <sys/dnv.h> 74 #include <vm/uma.h> 75 76 #include <cam/cam.h> 77 #include <cam/scsi/scsi_all.h> 78 #include <cam/scsi/scsi_cd.h> 79 #include <cam/scsi/scsi_da.h> 80 #include <cam/ctl/ctl_io.h> 81 #include <cam/ctl/ctl.h> 82 #include <cam/ctl/ctl_frontend.h> 83 #include <cam/ctl/ctl_util.h> 84 #include <cam/ctl/ctl_backend.h> 85 #include <cam/ctl/ctl_ioctl.h> 86 #include <cam/ctl/ctl_ha.h> 87 #include <cam/ctl/ctl_private.h> 88 #include <cam/ctl/ctl_debug.h> 89 #include <cam/ctl/ctl_scsi_all.h> 90 #include <cam/ctl/ctl_error.h> 91 92 struct ctl_softc *control_softc = NULL; 93 94 /* 95 * Template mode pages. 96 */ 97 98 /* 99 * Note that these are default values only. The actual values will be 100 * filled in when the user does a mode sense. 101 */ 102 const static struct scsi_da_rw_recovery_page rw_er_page_default = { 103 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 104 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 105 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 106 /*read_retry_count*/0, 107 /*correction_span*/0, 108 /*head_offset_count*/0, 109 /*data_strobe_offset_cnt*/0, 110 /*byte8*/SMS_RWER_LBPERE, 111 /*write_retry_count*/0, 112 /*reserved2*/0, 113 /*recovery_time_limit*/{0, 0}, 114 }; 115 116 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 117 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 118 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 119 /*byte3*/SMS_RWER_PER, 120 /*read_retry_count*/0, 121 /*correction_span*/0, 122 /*head_offset_count*/0, 123 /*data_strobe_offset_cnt*/0, 124 /*byte8*/SMS_RWER_LBPERE, 125 /*write_retry_count*/0, 126 /*reserved2*/0, 127 /*recovery_time_limit*/{0, 0}, 128 }; 129 130 const static struct scsi_format_page format_page_default = { 131 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 132 /*page_length*/sizeof(struct scsi_format_page) - 2, 133 /*tracks_per_zone*/ {0, 0}, 134 /*alt_sectors_per_zone*/ {0, 0}, 135 /*alt_tracks_per_zone*/ {0, 0}, 136 /*alt_tracks_per_lun*/ {0, 0}, 137 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 138 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 139 /*bytes_per_sector*/ {0, 0}, 140 /*interleave*/ {0, 0}, 141 /*track_skew*/ {0, 0}, 142 /*cylinder_skew*/ {0, 0}, 143 /*flags*/ SFP_HSEC, 144 /*reserved*/ {0, 0, 0} 145 }; 146 147 const static struct scsi_format_page format_page_changeable = { 148 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 149 /*page_length*/sizeof(struct scsi_format_page) - 2, 150 /*tracks_per_zone*/ {0, 0}, 151 /*alt_sectors_per_zone*/ {0, 0}, 152 /*alt_tracks_per_zone*/ {0, 0}, 153 /*alt_tracks_per_lun*/ {0, 0}, 154 /*sectors_per_track*/ {0, 0}, 155 /*bytes_per_sector*/ {0, 0}, 156 /*interleave*/ {0, 0}, 157 /*track_skew*/ {0, 0}, 158 /*cylinder_skew*/ {0, 0}, 159 /*flags*/ 0, 160 /*reserved*/ {0, 0, 0} 161 }; 162 163 const static struct scsi_rigid_disk_page rigid_disk_page_default = { 164 /*page_code*/SMS_RIGID_DISK_PAGE, 165 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 166 /*cylinders*/ {0, 0, 0}, 167 /*heads*/ CTL_DEFAULT_HEADS, 168 /*start_write_precomp*/ {0, 0, 0}, 169 /*start_reduced_current*/ {0, 0, 0}, 170 /*step_rate*/ {0, 0}, 171 /*landing_zone_cylinder*/ {0, 0, 0}, 172 /*rpl*/ SRDP_RPL_DISABLED, 173 /*rotational_offset*/ 0, 174 /*reserved1*/ 0, 175 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 176 CTL_DEFAULT_ROTATION_RATE & 0xff}, 177 /*reserved2*/ {0, 0} 178 }; 179 180 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 181 /*page_code*/SMS_RIGID_DISK_PAGE, 182 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 183 /*cylinders*/ {0, 0, 0}, 184 /*heads*/ 0, 185 /*start_write_precomp*/ {0, 0, 0}, 186 /*start_reduced_current*/ {0, 0, 0}, 187 /*step_rate*/ {0, 0}, 188 /*landing_zone_cylinder*/ {0, 0, 0}, 189 /*rpl*/ 0, 190 /*rotational_offset*/ 0, 191 /*reserved1*/ 0, 192 /*rotation_rate*/ {0, 0}, 193 /*reserved2*/ {0, 0} 194 }; 195 196 const static struct scsi_da_verify_recovery_page verify_er_page_default = { 197 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 198 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 199 /*byte3*/0, 200 /*read_retry_count*/0, 201 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 202 /*recovery_time_limit*/{0, 0}, 203 }; 204 205 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 206 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 207 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 208 /*byte3*/SMS_VER_PER, 209 /*read_retry_count*/0, 210 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 211 /*recovery_time_limit*/{0, 0}, 212 }; 213 214 const static struct scsi_caching_page caching_page_default = { 215 /*page_code*/SMS_CACHING_PAGE, 216 /*page_length*/sizeof(struct scsi_caching_page) - 2, 217 /*flags1*/ SCP_DISC | SCP_WCE, 218 /*ret_priority*/ 0, 219 /*disable_pf_transfer_len*/ {0xff, 0xff}, 220 /*min_prefetch*/ {0, 0}, 221 /*max_prefetch*/ {0xff, 0xff}, 222 /*max_pf_ceiling*/ {0xff, 0xff}, 223 /*flags2*/ 0, 224 /*cache_segments*/ 0, 225 /*cache_seg_size*/ {0, 0}, 226 /*reserved*/ 0, 227 /*non_cache_seg_size*/ {0, 0, 0} 228 }; 229 230 const static struct scsi_caching_page caching_page_changeable = { 231 /*page_code*/SMS_CACHING_PAGE, 232 /*page_length*/sizeof(struct scsi_caching_page) - 2, 233 /*flags1*/ SCP_WCE | SCP_RCD, 234 /*ret_priority*/ 0, 235 /*disable_pf_transfer_len*/ {0, 0}, 236 /*min_prefetch*/ {0, 0}, 237 /*max_prefetch*/ {0, 0}, 238 /*max_pf_ceiling*/ {0, 0}, 239 /*flags2*/ 0, 240 /*cache_segments*/ 0, 241 /*cache_seg_size*/ {0, 0}, 242 /*reserved*/ 0, 243 /*non_cache_seg_size*/ {0, 0, 0} 244 }; 245 246 const static struct scsi_control_page control_page_default = { 247 /*page_code*/SMS_CONTROL_MODE_PAGE, 248 /*page_length*/sizeof(struct scsi_control_page) - 2, 249 /*rlec*/0, 250 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 251 /*eca_and_aen*/0, 252 /*flags4*/SCP_TAS, 253 /*aen_holdoff_period*/{0, 0}, 254 /*busy_timeout_period*/{0, 0}, 255 /*extended_selftest_completion_time*/{0, 0} 256 }; 257 258 const static struct scsi_control_page control_page_changeable = { 259 /*page_code*/SMS_CONTROL_MODE_PAGE, 260 /*page_length*/sizeof(struct scsi_control_page) - 2, 261 /*rlec*/SCP_DSENSE, 262 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 263 /*eca_and_aen*/SCP_SWP, 264 /*flags4*/0, 265 /*aen_holdoff_period*/{0, 0}, 266 /*busy_timeout_period*/{0, 0}, 267 /*extended_selftest_completion_time*/{0, 0} 268 }; 269 270 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 271 272 const static struct scsi_control_ext_page control_ext_page_default = { 273 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 274 /*subpage_code*/0x01, 275 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 276 /*flags*/0, 277 /*prio*/0, 278 /*max_sense*/0 279 }; 280 281 const static struct scsi_control_ext_page control_ext_page_changeable = { 282 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 283 /*subpage_code*/0x01, 284 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 285 /*flags*/0, 286 /*prio*/0, 287 /*max_sense*/0xff 288 }; 289 290 const static struct scsi_info_exceptions_page ie_page_default = { 291 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 292 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 293 /*info_flags*/SIEP_FLAGS_EWASC, 294 /*mrie*/SIEP_MRIE_NO, 295 /*interval_timer*/{0, 0, 0, 0}, 296 /*report_count*/{0, 0, 0, 1} 297 }; 298 299 const static struct scsi_info_exceptions_page ie_page_changeable = { 300 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 301 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 302 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 303 SIEP_FLAGS_LOGERR, 304 /*mrie*/0x0f, 305 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 306 /*report_count*/{0xff, 0xff, 0xff, 0xff} 307 }; 308 309 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 310 311 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 312 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 313 /*subpage_code*/0x02, 314 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 315 /*flags*/0, 316 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 317 /*descr*/{}}, 318 {{/*flags*/0, 319 /*resource*/0x01, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0x02, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}}, 326 {/*flags*/0, 327 /*resource*/0xf1, 328 /*reserved*/{0, 0}, 329 /*count*/{0, 0, 0, 0}}, 330 {/*flags*/0, 331 /*resource*/0xf2, 332 /*reserved*/{0, 0}, 333 /*count*/{0, 0, 0, 0}} 334 } 335 }; 336 337 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 338 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 339 /*subpage_code*/0x02, 340 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 341 /*flags*/SLBPP_SITUA, 342 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 343 /*descr*/{}}, 344 {{/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}}, 352 {/*flags*/0, 353 /*resource*/0, 354 /*reserved*/{0, 0}, 355 /*count*/{0, 0, 0, 0}}, 356 {/*flags*/0, 357 /*resource*/0, 358 /*reserved*/{0, 0}, 359 /*count*/{0, 0, 0, 0}} 360 } 361 }; 362 363 const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 364 /*page_code*/SMS_CDDVD_CAPS_PAGE, 365 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 366 /*caps1*/0x3f, 367 /*caps2*/0x00, 368 /*caps3*/0xf0, 369 /*caps4*/0x00, 370 /*caps5*/0x29, 371 /*caps6*/0x00, 372 /*obsolete*/{0, 0}, 373 /*nvol_levels*/{0, 0}, 374 /*buffer_size*/{8, 0}, 375 /*obsolete2*/{0, 0}, 376 /*reserved*/0, 377 /*digital*/0, 378 /*obsolete3*/0, 379 /*copy_management*/0, 380 /*reserved2*/0, 381 /*rotation_control*/0, 382 /*cur_write_speed*/0, 383 /*num_speed_descr*/0, 384 }; 385 386 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 387 /*page_code*/SMS_CDDVD_CAPS_PAGE, 388 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 389 /*caps1*/0, 390 /*caps2*/0, 391 /*caps3*/0, 392 /*caps4*/0, 393 /*caps5*/0, 394 /*caps6*/0, 395 /*obsolete*/{0, 0}, 396 /*nvol_levels*/{0, 0}, 397 /*buffer_size*/{0, 0}, 398 /*obsolete2*/{0, 0}, 399 /*reserved*/0, 400 /*digital*/0, 401 /*obsolete3*/0, 402 /*copy_management*/0, 403 /*reserved2*/0, 404 /*rotation_control*/0, 405 /*cur_write_speed*/0, 406 /*num_speed_descr*/0, 407 }; 408 409 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 410 "CAM Target Layer"); 411 static int worker_threads = -1; 412 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 413 &worker_threads, 1, "Number of worker threads"); 414 static int ctl_debug = CTL_DEBUG_NONE; 415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 416 &ctl_debug, 0, "Enabled debug flags"); 417 static int ctl_lun_map_size = 1024; 418 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 419 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 420 #ifdef CTL_TIME_IO 421 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS; 422 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN, 423 &ctl_time_io_secs, 0, "Log requests taking more seconds"); 424 #endif 425 426 /* 427 * Maximum number of LUNs we support. MUST be a power of 2. 428 */ 429 #define CTL_DEFAULT_MAX_LUNS 1024 430 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 431 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns); 432 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN, 433 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs"); 434 435 /* 436 * Maximum number of ports registered at one time. 437 */ 438 #define CTL_DEFAULT_MAX_PORTS 256 439 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 440 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports); 441 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN, 442 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports"); 443 444 /* 445 * Maximum number of initiators we support. 446 */ 447 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports) 448 449 /* 450 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 451 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 452 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92), 453 * Block limits (0xB0), Block Device Characteristics (0xB1) and 454 * Logical Block Provisioning (0xB2) 455 */ 456 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11 457 458 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 459 int param); 460 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 461 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 462 static int ctl_init(void); 463 static int ctl_shutdown(void); 464 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 465 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 466 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 467 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 468 struct ctl_ooa *ooa_hdr, 469 struct ctl_ooa_entry *kern_entries); 470 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 471 struct thread *td); 472 static int ctl_enable_lun(struct ctl_lun *lun); 473 static int ctl_disable_lun(struct ctl_lun *lun); 474 static int ctl_free_lun(struct ctl_lun *lun); 475 476 static int ctl_do_mode_select(union ctl_io *io); 477 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 478 uint64_t res_key, uint64_t sa_res_key, 479 uint8_t type, uint32_t residx, 480 struct ctl_scsiio *ctsio, 481 struct scsi_per_res_out *cdb, 482 struct scsi_per_res_out_parms* param); 483 static void ctl_pro_preempt_other(struct ctl_lun *lun, 484 union ctl_ha_msg *msg); 485 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 486 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 487 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 488 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 489 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 490 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 491 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 492 int alloc_len); 493 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len); 494 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 495 int alloc_len); 496 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 497 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 498 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 499 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 500 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 501 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 502 bool seq); 503 static ctl_action ctl_seq_check(union ctl_io *io1, union ctl_io *io2); 504 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 505 union ctl_io *pending_io, const uint8_t *serialize_row, 506 union ctl_io *ooa_io); 507 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 508 union ctl_io **starting_io); 509 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, 510 bool skip); 511 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io, 512 bool skip); 513 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 514 const struct ctl_cmd_entry *entry, 515 struct ctl_scsiio *ctsio); 516 static void ctl_failover_lun(union ctl_io *io); 517 static void ctl_scsiio_precheck(struct ctl_scsiio *ctsio); 518 static int ctl_scsiio(struct ctl_scsiio *ctsio); 519 520 static int ctl_target_reset(union ctl_io *io); 521 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, 522 ctl_ua_type ua_type); 523 static int ctl_lun_reset(union ctl_io *io); 524 static int ctl_abort_task(union ctl_io *io); 525 static int ctl_abort_task_set(union ctl_io *io); 526 static int ctl_query_task(union ctl_io *io, int task_set); 527 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 528 ctl_ua_type ua_type); 529 static int ctl_i_t_nexus_reset(union ctl_io *io); 530 static int ctl_query_async_event(union ctl_io *io); 531 static void ctl_run_task(union ctl_io *io); 532 #ifdef CTL_IO_DELAY 533 static void ctl_datamove_timer_wakeup(void *arg); 534 static void ctl_done_timer_wakeup(void *arg); 535 #endif /* CTL_IO_DELAY */ 536 537 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 538 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 539 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr); 540 static void ctl_datamove_remote_write(union ctl_io *io); 541 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr); 542 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 543 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 544 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 545 ctl_ha_dt_cb callback); 546 static void ctl_datamove_remote_read(union ctl_io *io); 547 static void ctl_datamove_remote(union ctl_io *io); 548 static void ctl_process_done(union ctl_io *io); 549 static void ctl_thresh_thread(void *arg); 550 static void ctl_work_thread(void *arg); 551 static void ctl_enqueue_incoming(union ctl_io *io); 552 static void ctl_enqueue_rtr(union ctl_io *io); 553 static void ctl_enqueue_done(union ctl_io *io); 554 static void ctl_enqueue_isc(union ctl_io *io); 555 static const struct ctl_cmd_entry * 556 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 557 static const struct ctl_cmd_entry * 558 ctl_validate_command(struct ctl_scsiio *ctsio); 559 static int ctl_cmd_applicable(uint8_t lun_type, 560 const struct ctl_cmd_entry *entry); 561 static int ctl_ha_init(void); 562 static int ctl_ha_shutdown(void); 563 564 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 565 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 566 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 567 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 568 569 /* 570 * Load the serialization table. This isn't very pretty, but is probably 571 * the easiest way to do it. 572 */ 573 #include "ctl_ser_table.c" 574 575 /* 576 * We only need to define open, close and ioctl routines for this driver. 577 */ 578 static struct cdevsw ctl_cdevsw = { 579 .d_version = D_VERSION, 580 .d_flags = 0, 581 .d_open = ctl_open, 582 .d_close = ctl_close, 583 .d_ioctl = ctl_ioctl, 584 .d_name = "ctl", 585 }; 586 587 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 588 589 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 590 591 static moduledata_t ctl_moduledata = { 592 "ctl", 593 ctl_module_event_handler, 594 NULL 595 }; 596 597 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 598 MODULE_VERSION(ctl, 1); 599 600 static struct ctl_frontend ha_frontend = 601 { 602 .name = "ha", 603 .init = ctl_ha_init, 604 .shutdown = ctl_ha_shutdown, 605 }; 606 607 static int 608 ctl_ha_init(void) 609 { 610 struct ctl_softc *softc = control_softc; 611 612 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 613 &softc->othersc_pool) != 0) 614 return (ENOMEM); 615 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 616 ctl_pool_free(softc->othersc_pool); 617 return (EIO); 618 } 619 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 620 != CTL_HA_STATUS_SUCCESS) { 621 ctl_ha_msg_destroy(softc); 622 ctl_pool_free(softc->othersc_pool); 623 return (EIO); 624 } 625 return (0); 626 }; 627 628 static int 629 ctl_ha_shutdown(void) 630 { 631 struct ctl_softc *softc = control_softc; 632 struct ctl_port *port; 633 634 ctl_ha_msg_shutdown(softc); 635 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 636 return (EIO); 637 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 638 return (EIO); 639 ctl_pool_free(softc->othersc_pool); 640 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 641 ctl_port_deregister(port); 642 free(port->port_name, M_CTL); 643 free(port, M_CTL); 644 } 645 return (0); 646 }; 647 648 static void 649 ctl_ha_datamove(union ctl_io *io) 650 { 651 struct ctl_lun *lun = CTL_LUN(io); 652 struct ctl_sg_entry *sgl; 653 union ctl_ha_msg msg; 654 uint32_t sg_entries_sent; 655 int do_sg_copy, i, j; 656 657 memset(&msg.dt, 0, sizeof(msg.dt)); 658 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 659 msg.hdr.original_sc = io->io_hdr.remote_io; 660 msg.hdr.serializing_sc = io; 661 msg.hdr.nexus = io->io_hdr.nexus; 662 msg.hdr.status = io->io_hdr.status; 663 msg.dt.flags = io->io_hdr.flags; 664 665 /* 666 * We convert everything into a S/G list here. We can't 667 * pass by reference, only by value between controllers. 668 * So we can't pass a pointer to the S/G list, only as many 669 * S/G entries as we can fit in here. If it's possible for 670 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 671 * then we need to break this up into multiple transfers. 672 */ 673 if (io->scsiio.kern_sg_entries == 0) { 674 msg.dt.kern_sg_entries = 1; 675 #if 0 676 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 677 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 678 } else { 679 /* XXX KDM use busdma here! */ 680 msg.dt.sg_list[0].addr = 681 (void *)vtophys(io->scsiio.kern_data_ptr); 682 } 683 #else 684 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 685 ("HA does not support BUS_ADDR")); 686 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 687 #endif 688 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 689 do_sg_copy = 0; 690 } else { 691 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 692 do_sg_copy = 1; 693 } 694 695 msg.dt.kern_data_len = io->scsiio.kern_data_len; 696 msg.dt.kern_total_len = io->scsiio.kern_total_len; 697 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 698 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 699 msg.dt.sg_sequence = 0; 700 701 /* 702 * Loop until we've sent all of the S/G entries. On the 703 * other end, we'll recompose these S/G entries into one 704 * contiguous list before processing. 705 */ 706 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 707 msg.dt.sg_sequence++) { 708 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 709 sizeof(msg.dt.sg_list[0])), 710 msg.dt.kern_sg_entries - sg_entries_sent); 711 if (do_sg_copy != 0) { 712 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 713 for (i = sg_entries_sent, j = 0; 714 i < msg.dt.cur_sg_entries; i++, j++) { 715 #if 0 716 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 717 msg.dt.sg_list[j].addr = sgl[i].addr; 718 } else { 719 /* XXX KDM use busdma here! */ 720 msg.dt.sg_list[j].addr = 721 (void *)vtophys(sgl[i].addr); 722 } 723 #else 724 KASSERT((io->io_hdr.flags & 725 CTL_FLAG_BUS_ADDR) == 0, 726 ("HA does not support BUS_ADDR")); 727 msg.dt.sg_list[j].addr = sgl[i].addr; 728 #endif 729 msg.dt.sg_list[j].len = sgl[i].len; 730 } 731 } 732 733 sg_entries_sent += msg.dt.cur_sg_entries; 734 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 735 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 736 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 737 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 738 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 739 io->io_hdr.port_status = 31341; 740 ctl_datamove_done(io, true); 741 return; 742 } 743 msg.dt.sent_sg_entries = sg_entries_sent; 744 } 745 746 /* 747 * Officially handover the request from us to peer. 748 * If failover has just happened, then we must return error. 749 * If failover happen just after, then it is not our problem. 750 */ 751 if (lun) 752 mtx_lock(&lun->lun_lock); 753 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 754 if (lun) 755 mtx_unlock(&lun->lun_lock); 756 io->io_hdr.port_status = 31342; 757 ctl_datamove_done(io, true); 758 return; 759 } 760 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 761 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 762 if (lun) 763 mtx_unlock(&lun->lun_lock); 764 } 765 766 static void 767 ctl_ha_done(union ctl_io *io) 768 { 769 union ctl_ha_msg msg; 770 771 if (io->io_hdr.io_type == CTL_IO_SCSI) { 772 memset(&msg, 0, sizeof(msg)); 773 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 774 msg.hdr.original_sc = io->io_hdr.remote_io; 775 msg.hdr.nexus = io->io_hdr.nexus; 776 msg.hdr.status = io->io_hdr.status; 777 msg.scsi.scsi_status = io->scsiio.scsi_status; 778 msg.scsi.tag_num = io->scsiio.tag_num; 779 msg.scsi.tag_type = io->scsiio.tag_type; 780 msg.scsi.sense_len = io->scsiio.sense_len; 781 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 782 io->scsiio.sense_len); 783 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 784 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 785 msg.scsi.sense_len, M_WAITOK); 786 } 787 ctl_free_io(io); 788 } 789 790 static void 791 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 792 union ctl_ha_msg *msg_info) 793 { 794 struct ctl_scsiio *ctsio; 795 796 if (msg_info->hdr.original_sc == NULL) { 797 printf("%s: original_sc == NULL!\n", __func__); 798 /* XXX KDM now what? */ 799 return; 800 } 801 802 ctsio = &msg_info->hdr.original_sc->scsiio; 803 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 804 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 805 ctsio->io_hdr.status = msg_info->hdr.status; 806 ctsio->scsi_status = msg_info->scsi.scsi_status; 807 ctsio->sense_len = msg_info->scsi.sense_len; 808 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 809 msg_info->scsi.sense_len); 810 ctl_enqueue_isc((union ctl_io *)ctsio); 811 } 812 813 static void 814 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 815 union ctl_ha_msg *msg_info) 816 { 817 struct ctl_scsiio *ctsio; 818 819 if (msg_info->hdr.serializing_sc == NULL) { 820 printf("%s: serializing_sc == NULL!\n", __func__); 821 /* XXX KDM now what? */ 822 return; 823 } 824 825 ctsio = &msg_info->hdr.serializing_sc->scsiio; 826 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 827 ctl_enqueue_isc((union ctl_io *)ctsio); 828 } 829 830 void 831 ctl_isc_announce_lun(struct ctl_lun *lun) 832 { 833 struct ctl_softc *softc = lun->ctl_softc; 834 union ctl_ha_msg *msg; 835 struct ctl_ha_msg_lun_pr_key pr_key; 836 int i, k; 837 838 if (softc->ha_link != CTL_HA_LINK_ONLINE) 839 return; 840 mtx_lock(&lun->lun_lock); 841 i = sizeof(msg->lun); 842 if (lun->lun_devid) 843 i += lun->lun_devid->len; 844 i += sizeof(pr_key) * lun->pr_key_count; 845 alloc: 846 mtx_unlock(&lun->lun_lock); 847 msg = malloc(i, M_CTL, M_WAITOK); 848 mtx_lock(&lun->lun_lock); 849 k = sizeof(msg->lun); 850 if (lun->lun_devid) 851 k += lun->lun_devid->len; 852 k += sizeof(pr_key) * lun->pr_key_count; 853 if (i < k) { 854 free(msg, M_CTL); 855 i = k; 856 goto alloc; 857 } 858 bzero(&msg->lun, sizeof(msg->lun)); 859 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 860 msg->hdr.nexus.targ_lun = lun->lun; 861 msg->hdr.nexus.targ_mapped_lun = lun->lun; 862 msg->lun.flags = lun->flags; 863 msg->lun.pr_generation = lun->pr_generation; 864 msg->lun.pr_res_idx = lun->pr_res_idx; 865 msg->lun.pr_res_type = lun->pr_res_type; 866 msg->lun.pr_key_count = lun->pr_key_count; 867 i = 0; 868 if (lun->lun_devid) { 869 msg->lun.lun_devid_len = lun->lun_devid->len; 870 memcpy(&msg->lun.data[i], lun->lun_devid->data, 871 msg->lun.lun_devid_len); 872 i += msg->lun.lun_devid_len; 873 } 874 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 875 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 876 continue; 877 pr_key.pr_iid = k; 878 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 879 i += sizeof(pr_key); 880 } 881 mtx_unlock(&lun->lun_lock); 882 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 883 M_WAITOK); 884 free(msg, M_CTL); 885 886 if (lun->flags & CTL_LUN_PRIMARY_SC) { 887 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 888 ctl_isc_announce_mode(lun, -1, 889 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 890 lun->mode_pages.index[i].subpage); 891 } 892 } 893 } 894 895 void 896 ctl_isc_announce_port(struct ctl_port *port) 897 { 898 struct ctl_softc *softc = port->ctl_softc; 899 union ctl_ha_msg *msg; 900 int i; 901 902 if (port->targ_port < softc->port_min || 903 port->targ_port >= softc->port_max || 904 softc->ha_link != CTL_HA_LINK_ONLINE) 905 return; 906 i = sizeof(msg->port) + strlen(port->port_name) + 1; 907 if (port->lun_map) 908 i += port->lun_map_size * sizeof(uint32_t); 909 if (port->port_devid) 910 i += port->port_devid->len; 911 if (port->target_devid) 912 i += port->target_devid->len; 913 if (port->init_devid) 914 i += port->init_devid->len; 915 msg = malloc(i, M_CTL, M_WAITOK); 916 bzero(&msg->port, sizeof(msg->port)); 917 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 918 msg->hdr.nexus.targ_port = port->targ_port; 919 msg->port.port_type = port->port_type; 920 msg->port.physical_port = port->physical_port; 921 msg->port.virtual_port = port->virtual_port; 922 msg->port.status = port->status; 923 i = 0; 924 msg->port.name_len = sprintf(&msg->port.data[i], 925 "%d:%s", softc->ha_id, port->port_name) + 1; 926 i += msg->port.name_len; 927 if (port->lun_map) { 928 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 929 memcpy(&msg->port.data[i], port->lun_map, 930 msg->port.lun_map_len); 931 i += msg->port.lun_map_len; 932 } 933 if (port->port_devid) { 934 msg->port.port_devid_len = port->port_devid->len; 935 memcpy(&msg->port.data[i], port->port_devid->data, 936 msg->port.port_devid_len); 937 i += msg->port.port_devid_len; 938 } 939 if (port->target_devid) { 940 msg->port.target_devid_len = port->target_devid->len; 941 memcpy(&msg->port.data[i], port->target_devid->data, 942 msg->port.target_devid_len); 943 i += msg->port.target_devid_len; 944 } 945 if (port->init_devid) { 946 msg->port.init_devid_len = port->init_devid->len; 947 memcpy(&msg->port.data[i], port->init_devid->data, 948 msg->port.init_devid_len); 949 i += msg->port.init_devid_len; 950 } 951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 952 M_WAITOK); 953 free(msg, M_CTL); 954 } 955 956 void 957 ctl_isc_announce_iid(struct ctl_port *port, int iid) 958 { 959 struct ctl_softc *softc = port->ctl_softc; 960 union ctl_ha_msg *msg; 961 int i, l; 962 963 if (port->targ_port < softc->port_min || 964 port->targ_port >= softc->port_max || 965 softc->ha_link != CTL_HA_LINK_ONLINE) 966 return; 967 mtx_lock(&softc->ctl_lock); 968 i = sizeof(msg->iid); 969 l = 0; 970 if (port->wwpn_iid[iid].name) 971 l = strlen(port->wwpn_iid[iid].name) + 1; 972 i += l; 973 msg = malloc(i, M_CTL, M_NOWAIT); 974 if (msg == NULL) { 975 mtx_unlock(&softc->ctl_lock); 976 return; 977 } 978 bzero(&msg->iid, sizeof(msg->iid)); 979 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 980 msg->hdr.nexus.targ_port = port->targ_port; 981 msg->hdr.nexus.initid = iid; 982 msg->iid.in_use = port->wwpn_iid[iid].in_use; 983 msg->iid.name_len = l; 984 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 985 if (port->wwpn_iid[iid].name) 986 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 987 mtx_unlock(&softc->ctl_lock); 988 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 989 free(msg, M_CTL); 990 } 991 992 void 993 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 994 uint8_t page, uint8_t subpage) 995 { 996 struct ctl_softc *softc = lun->ctl_softc; 997 union ctl_ha_msg msg; 998 u_int i; 999 1000 if (softc->ha_link != CTL_HA_LINK_ONLINE) 1001 return; 1002 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1003 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1004 page && lun->mode_pages.index[i].subpage == subpage) 1005 break; 1006 } 1007 if (i == CTL_NUM_MODE_PAGES) 1008 return; 1009 1010 /* Don't try to replicate pages not present on this device. */ 1011 if (lun->mode_pages.index[i].page_data == NULL) 1012 return; 1013 1014 bzero(&msg.mode, sizeof(msg.mode)); 1015 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 1016 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 1017 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 1018 msg.hdr.nexus.targ_lun = lun->lun; 1019 msg.hdr.nexus.targ_mapped_lun = lun->lun; 1020 msg.mode.page_code = page; 1021 msg.mode.subpage = subpage; 1022 msg.mode.page_len = lun->mode_pages.index[i].page_len; 1023 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 1024 msg.mode.page_len); 1025 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 1026 M_WAITOK); 1027 } 1028 1029 static void 1030 ctl_isc_ha_link_up(struct ctl_softc *softc) 1031 { 1032 struct ctl_port *port; 1033 struct ctl_lun *lun; 1034 union ctl_ha_msg msg; 1035 int i; 1036 1037 /* Announce this node parameters to peer for validation. */ 1038 msg.login.msg_type = CTL_MSG_LOGIN; 1039 msg.login.version = CTL_HA_VERSION; 1040 msg.login.ha_mode = softc->ha_mode; 1041 msg.login.ha_id = softc->ha_id; 1042 msg.login.max_luns = ctl_max_luns; 1043 msg.login.max_ports = ctl_max_ports; 1044 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1045 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1046 M_WAITOK); 1047 1048 STAILQ_FOREACH(port, &softc->port_list, links) { 1049 ctl_isc_announce_port(port); 1050 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1051 if (port->wwpn_iid[i].in_use) 1052 ctl_isc_announce_iid(port, i); 1053 } 1054 } 1055 STAILQ_FOREACH(lun, &softc->lun_list, links) 1056 ctl_isc_announce_lun(lun); 1057 } 1058 1059 static void 1060 ctl_isc_ha_link_down(struct ctl_softc *softc) 1061 { 1062 struct ctl_port *port; 1063 struct ctl_lun *lun; 1064 union ctl_io *io; 1065 int i; 1066 1067 mtx_lock(&softc->ctl_lock); 1068 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1069 mtx_lock(&lun->lun_lock); 1070 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1071 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1072 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1073 } 1074 mtx_unlock(&lun->lun_lock); 1075 1076 mtx_unlock(&softc->ctl_lock); 1077 io = ctl_alloc_io(softc->othersc_pool); 1078 mtx_lock(&softc->ctl_lock); 1079 ctl_zero_io(io); 1080 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1081 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1082 ctl_enqueue_isc(io); 1083 } 1084 1085 STAILQ_FOREACH(port, &softc->port_list, links) { 1086 if (port->targ_port >= softc->port_min && 1087 port->targ_port < softc->port_max) 1088 continue; 1089 port->status &= ~CTL_PORT_STATUS_ONLINE; 1090 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1091 port->wwpn_iid[i].in_use = 0; 1092 free(port->wwpn_iid[i].name, M_CTL); 1093 port->wwpn_iid[i].name = NULL; 1094 } 1095 } 1096 mtx_unlock(&softc->ctl_lock); 1097 } 1098 1099 static void 1100 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1101 { 1102 struct ctl_lun *lun; 1103 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1104 1105 mtx_lock(&softc->ctl_lock); 1106 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1107 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1108 mtx_unlock(&softc->ctl_lock); 1109 return; 1110 } 1111 mtx_lock(&lun->lun_lock); 1112 mtx_unlock(&softc->ctl_lock); 1113 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1114 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1115 if (msg->ua.ua_all) { 1116 if (msg->ua.ua_set) 1117 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1118 else 1119 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1120 } else { 1121 if (msg->ua.ua_set) 1122 ctl_est_ua(lun, iid, msg->ua.ua_type); 1123 else 1124 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1125 } 1126 mtx_unlock(&lun->lun_lock); 1127 } 1128 1129 static void 1130 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1131 { 1132 struct ctl_lun *lun; 1133 struct ctl_ha_msg_lun_pr_key pr_key; 1134 int i, k; 1135 ctl_lun_flags oflags; 1136 uint32_t targ_lun; 1137 1138 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1139 mtx_lock(&softc->ctl_lock); 1140 if (targ_lun >= ctl_max_luns || 1141 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1142 mtx_unlock(&softc->ctl_lock); 1143 return; 1144 } 1145 mtx_lock(&lun->lun_lock); 1146 mtx_unlock(&softc->ctl_lock); 1147 if (lun->flags & CTL_LUN_DISABLED) { 1148 mtx_unlock(&lun->lun_lock); 1149 return; 1150 } 1151 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1152 if (msg->lun.lun_devid_len != i || (i > 0 && 1153 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1154 mtx_unlock(&lun->lun_lock); 1155 printf("%s: Received conflicting HA LUN %d\n", 1156 __func__, targ_lun); 1157 return; 1158 } else { 1159 /* Record whether peer is primary. */ 1160 oflags = lun->flags; 1161 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1162 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1163 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1164 else 1165 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1166 if (oflags != lun->flags) 1167 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1168 1169 /* If peer is primary and we are not -- use data */ 1170 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1171 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1172 lun->pr_generation = msg->lun.pr_generation; 1173 lun->pr_res_idx = msg->lun.pr_res_idx; 1174 lun->pr_res_type = msg->lun.pr_res_type; 1175 lun->pr_key_count = msg->lun.pr_key_count; 1176 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1177 ctl_clr_prkey(lun, k); 1178 for (k = 0; k < msg->lun.pr_key_count; k++) { 1179 memcpy(&pr_key, &msg->lun.data[i], 1180 sizeof(pr_key)); 1181 ctl_alloc_prkey(lun, pr_key.pr_iid); 1182 ctl_set_prkey(lun, pr_key.pr_iid, 1183 pr_key.pr_key); 1184 i += sizeof(pr_key); 1185 } 1186 } 1187 1188 mtx_unlock(&lun->lun_lock); 1189 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1190 __func__, targ_lun, 1191 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1192 "primary" : "secondary")); 1193 1194 /* If we are primary but peer doesn't know -- notify */ 1195 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1196 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1197 ctl_isc_announce_lun(lun); 1198 } 1199 } 1200 1201 static void 1202 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1203 { 1204 struct ctl_port *port; 1205 struct ctl_lun *lun; 1206 int i, new; 1207 1208 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1209 if (port == NULL) { 1210 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1211 msg->hdr.nexus.targ_port)); 1212 new = 1; 1213 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1214 port->frontend = &ha_frontend; 1215 port->targ_port = msg->hdr.nexus.targ_port; 1216 port->fe_datamove = ctl_ha_datamove; 1217 port->fe_done = ctl_ha_done; 1218 } else if (port->frontend == &ha_frontend) { 1219 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1220 msg->hdr.nexus.targ_port)); 1221 new = 0; 1222 } else { 1223 printf("%s: Received conflicting HA port %d\n", 1224 __func__, msg->hdr.nexus.targ_port); 1225 return; 1226 } 1227 port->port_type = msg->port.port_type; 1228 port->physical_port = msg->port.physical_port; 1229 port->virtual_port = msg->port.virtual_port; 1230 port->status = msg->port.status; 1231 i = 0; 1232 free(port->port_name, M_CTL); 1233 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1234 M_CTL); 1235 i += msg->port.name_len; 1236 if (msg->port.lun_map_len != 0) { 1237 if (port->lun_map == NULL || 1238 port->lun_map_size * sizeof(uint32_t) < 1239 msg->port.lun_map_len) { 1240 port->lun_map_size = 0; 1241 free(port->lun_map, M_CTL); 1242 port->lun_map = malloc(msg->port.lun_map_len, 1243 M_CTL, M_WAITOK); 1244 } 1245 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1246 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1247 i += msg->port.lun_map_len; 1248 } else { 1249 port->lun_map_size = 0; 1250 free(port->lun_map, M_CTL); 1251 port->lun_map = NULL; 1252 } 1253 if (msg->port.port_devid_len != 0) { 1254 if (port->port_devid == NULL || 1255 port->port_devid->len < msg->port.port_devid_len) { 1256 free(port->port_devid, M_CTL); 1257 port->port_devid = malloc(sizeof(struct ctl_devid) + 1258 msg->port.port_devid_len, M_CTL, M_WAITOK); 1259 } 1260 memcpy(port->port_devid->data, &msg->port.data[i], 1261 msg->port.port_devid_len); 1262 port->port_devid->len = msg->port.port_devid_len; 1263 i += msg->port.port_devid_len; 1264 } else { 1265 free(port->port_devid, M_CTL); 1266 port->port_devid = NULL; 1267 } 1268 if (msg->port.target_devid_len != 0) { 1269 if (port->target_devid == NULL || 1270 port->target_devid->len < msg->port.target_devid_len) { 1271 free(port->target_devid, M_CTL); 1272 port->target_devid = malloc(sizeof(struct ctl_devid) + 1273 msg->port.target_devid_len, M_CTL, M_WAITOK); 1274 } 1275 memcpy(port->target_devid->data, &msg->port.data[i], 1276 msg->port.target_devid_len); 1277 port->target_devid->len = msg->port.target_devid_len; 1278 i += msg->port.target_devid_len; 1279 } else { 1280 free(port->target_devid, M_CTL); 1281 port->target_devid = NULL; 1282 } 1283 if (msg->port.init_devid_len != 0) { 1284 if (port->init_devid == NULL || 1285 port->init_devid->len < msg->port.init_devid_len) { 1286 free(port->init_devid, M_CTL); 1287 port->init_devid = malloc(sizeof(struct ctl_devid) + 1288 msg->port.init_devid_len, M_CTL, M_WAITOK); 1289 } 1290 memcpy(port->init_devid->data, &msg->port.data[i], 1291 msg->port.init_devid_len); 1292 port->init_devid->len = msg->port.init_devid_len; 1293 i += msg->port.init_devid_len; 1294 } else { 1295 free(port->init_devid, M_CTL); 1296 port->init_devid = NULL; 1297 } 1298 if (new) { 1299 if (ctl_port_register(port) != 0) { 1300 printf("%s: ctl_port_register() failed with error\n", 1301 __func__); 1302 } 1303 } 1304 mtx_lock(&softc->ctl_lock); 1305 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1306 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1307 continue; 1308 mtx_lock(&lun->lun_lock); 1309 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1310 mtx_unlock(&lun->lun_lock); 1311 } 1312 mtx_unlock(&softc->ctl_lock); 1313 } 1314 1315 static void 1316 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1317 { 1318 struct ctl_port *port; 1319 int iid; 1320 1321 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1322 if (port == NULL) { 1323 printf("%s: Received IID for unknown port %d\n", 1324 __func__, msg->hdr.nexus.targ_port); 1325 return; 1326 } 1327 iid = msg->hdr.nexus.initid; 1328 if (port->wwpn_iid[iid].in_use != 0 && 1329 msg->iid.in_use == 0) 1330 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1331 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1332 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1333 free(port->wwpn_iid[iid].name, M_CTL); 1334 if (msg->iid.name_len) { 1335 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1336 msg->iid.name_len, M_CTL); 1337 } else 1338 port->wwpn_iid[iid].name = NULL; 1339 } 1340 1341 static void 1342 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1343 { 1344 1345 if (msg->login.version != CTL_HA_VERSION) { 1346 printf("CTL HA peers have different versions %d != %d\n", 1347 msg->login.version, CTL_HA_VERSION); 1348 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1349 return; 1350 } 1351 if (msg->login.ha_mode != softc->ha_mode) { 1352 printf("CTL HA peers have different ha_mode %d != %d\n", 1353 msg->login.ha_mode, softc->ha_mode); 1354 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1355 return; 1356 } 1357 if (msg->login.ha_id == softc->ha_id) { 1358 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1359 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1360 return; 1361 } 1362 if (msg->login.max_luns != ctl_max_luns || 1363 msg->login.max_ports != ctl_max_ports || 1364 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1365 printf("CTL HA peers have different limits\n"); 1366 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1367 return; 1368 } 1369 } 1370 1371 static void 1372 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1373 { 1374 struct ctl_lun *lun; 1375 u_int i; 1376 uint32_t initidx, targ_lun; 1377 1378 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1379 mtx_lock(&softc->ctl_lock); 1380 if (targ_lun >= ctl_max_luns || 1381 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1382 mtx_unlock(&softc->ctl_lock); 1383 return; 1384 } 1385 mtx_lock(&lun->lun_lock); 1386 mtx_unlock(&softc->ctl_lock); 1387 if (lun->flags & CTL_LUN_DISABLED) { 1388 mtx_unlock(&lun->lun_lock); 1389 return; 1390 } 1391 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1392 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1393 msg->mode.page_code && 1394 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1395 break; 1396 } 1397 if (i == CTL_NUM_MODE_PAGES) { 1398 mtx_unlock(&lun->lun_lock); 1399 return; 1400 } 1401 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1402 lun->mode_pages.index[i].page_len); 1403 initidx = ctl_get_initindex(&msg->hdr.nexus); 1404 if (initidx != -1) 1405 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1406 mtx_unlock(&lun->lun_lock); 1407 } 1408 1409 /* 1410 * ISC (Inter Shelf Communication) event handler. Events from the HA 1411 * subsystem come in here. 1412 */ 1413 static void 1414 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1415 { 1416 struct ctl_softc *softc = control_softc; 1417 union ctl_io *io; 1418 struct ctl_prio *presio; 1419 ctl_ha_status isc_status; 1420 1421 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1422 if (event == CTL_HA_EVT_MSG_RECV) { 1423 union ctl_ha_msg *msg, msgbuf; 1424 1425 if (param > sizeof(msgbuf)) 1426 msg = malloc(param, M_CTL, M_WAITOK); 1427 else 1428 msg = &msgbuf; 1429 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1430 M_WAITOK); 1431 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1432 printf("%s: Error receiving message: %d\n", 1433 __func__, isc_status); 1434 if (msg != &msgbuf) 1435 free(msg, M_CTL); 1436 return; 1437 } 1438 1439 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->hdr.msg_type)); 1440 switch (msg->hdr.msg_type) { 1441 case CTL_MSG_SERIALIZE: 1442 io = ctl_alloc_io(softc->othersc_pool); 1443 ctl_zero_io(io); 1444 // populate ctsio from msg 1445 io->io_hdr.io_type = CTL_IO_SCSI; 1446 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1447 io->io_hdr.remote_io = msg->hdr.original_sc; 1448 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1449 CTL_FLAG_IO_ACTIVE; 1450 /* 1451 * If we're in serialization-only mode, we don't 1452 * want to go through full done processing. Thus 1453 * the COPY flag. 1454 * 1455 * XXX KDM add another flag that is more specific. 1456 */ 1457 if (softc->ha_mode != CTL_HA_MODE_XFER) 1458 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1459 io->io_hdr.nexus = msg->hdr.nexus; 1460 io->scsiio.priority = msg->scsi.priority; 1461 io->scsiio.tag_num = msg->scsi.tag_num; 1462 io->scsiio.tag_type = msg->scsi.tag_type; 1463 #ifdef CTL_TIME_IO 1464 io->io_hdr.start_time = time_uptime; 1465 getbinuptime(&io->io_hdr.start_bt); 1466 #endif /* CTL_TIME_IO */ 1467 io->scsiio.cdb_len = msg->scsi.cdb_len; 1468 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1469 CTL_MAX_CDBLEN); 1470 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1471 const struct ctl_cmd_entry *entry; 1472 1473 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1474 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1475 io->io_hdr.flags |= 1476 entry->flags & CTL_FLAG_DATA_MASK; 1477 } 1478 ctl_enqueue_isc(io); 1479 break; 1480 1481 /* Performed on the Originating SC, XFER mode only */ 1482 case CTL_MSG_DATAMOVE: { 1483 struct ctl_sg_entry *sgl; 1484 int i, j; 1485 1486 io = msg->hdr.original_sc; 1487 if (io == NULL) { 1488 printf("%s: original_sc == NULL!\n", __func__); 1489 /* XXX KDM do something here */ 1490 break; 1491 } 1492 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1493 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1494 /* 1495 * Keep track of this, we need to send it back over 1496 * when the datamove is complete. 1497 */ 1498 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1499 if (msg->hdr.status == CTL_SUCCESS) 1500 io->io_hdr.status = msg->hdr.status; 1501 1502 if (msg->dt.sg_sequence == 0) { 1503 #ifdef CTL_TIME_IO 1504 getbinuptime(&io->io_hdr.dma_start_bt); 1505 #endif 1506 i = msg->dt.kern_sg_entries + 1507 msg->dt.kern_data_len / 1508 CTL_HA_DATAMOVE_SEGMENT + 1; 1509 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1510 M_WAITOK | M_ZERO); 1511 CTL_RSGL(io) = sgl; 1512 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries]; 1513 1514 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1515 1516 io->scsiio.kern_sg_entries = 1517 msg->dt.kern_sg_entries; 1518 io->scsiio.rem_sg_entries = 1519 msg->dt.kern_sg_entries; 1520 io->scsiio.kern_data_len = 1521 msg->dt.kern_data_len; 1522 io->scsiio.kern_total_len = 1523 msg->dt.kern_total_len; 1524 io->scsiio.kern_data_resid = 1525 msg->dt.kern_data_resid; 1526 io->scsiio.kern_rel_offset = 1527 msg->dt.kern_rel_offset; 1528 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1529 io->io_hdr.flags |= msg->dt.flags & 1530 CTL_FLAG_BUS_ADDR; 1531 } else 1532 sgl = (struct ctl_sg_entry *) 1533 io->scsiio.kern_data_ptr; 1534 1535 for (i = msg->dt.sent_sg_entries, j = 0; 1536 i < (msg->dt.sent_sg_entries + 1537 msg->dt.cur_sg_entries); i++, j++) { 1538 sgl[i].addr = msg->dt.sg_list[j].addr; 1539 sgl[i].len = msg->dt.sg_list[j].len; 1540 } 1541 1542 /* 1543 * If this is the last piece of the I/O, we've got 1544 * the full S/G list. Queue processing in the thread. 1545 * Otherwise wait for the next piece. 1546 */ 1547 if (msg->dt.sg_last != 0) 1548 ctl_enqueue_isc(io); 1549 break; 1550 } 1551 /* Performed on the Serializing (primary) SC, XFER mode only */ 1552 case CTL_MSG_DATAMOVE_DONE: { 1553 if (msg->hdr.serializing_sc == NULL) { 1554 printf("%s: serializing_sc == NULL!\n", 1555 __func__); 1556 /* XXX KDM now what? */ 1557 break; 1558 } 1559 /* 1560 * We grab the sense information here in case 1561 * there was a failure, so we can return status 1562 * back to the initiator. 1563 */ 1564 io = msg->hdr.serializing_sc; 1565 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1566 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1567 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1568 io->io_hdr.port_status = msg->scsi.port_status; 1569 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1570 if (msg->hdr.status != CTL_STATUS_NONE) { 1571 io->io_hdr.status = msg->hdr.status; 1572 io->scsiio.scsi_status = msg->scsi.scsi_status; 1573 io->scsiio.sense_len = msg->scsi.sense_len; 1574 memcpy(&io->scsiio.sense_data, 1575 &msg->scsi.sense_data, 1576 msg->scsi.sense_len); 1577 if (msg->hdr.status == CTL_SUCCESS) 1578 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1579 } 1580 ctl_enqueue_isc(io); 1581 break; 1582 } 1583 1584 /* Preformed on Originating SC, SER_ONLY mode */ 1585 case CTL_MSG_R2R: 1586 io = msg->hdr.original_sc; 1587 if (io == NULL) { 1588 printf("%s: original_sc == NULL!\n", 1589 __func__); 1590 break; 1591 } 1592 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1593 io->io_hdr.msg_type = CTL_MSG_R2R; 1594 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1595 ctl_enqueue_isc(io); 1596 break; 1597 1598 /* 1599 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1600 * mode. 1601 * Performed on the Originating (i.e. secondary) SC in XFER 1602 * mode 1603 */ 1604 case CTL_MSG_FINISH_IO: 1605 if (softc->ha_mode == CTL_HA_MODE_XFER) 1606 ctl_isc_handler_finish_xfer(softc, msg); 1607 else 1608 ctl_isc_handler_finish_ser_only(softc, msg); 1609 break; 1610 1611 /* Preformed on Originating SC */ 1612 case CTL_MSG_BAD_JUJU: 1613 io = msg->hdr.original_sc; 1614 if (io == NULL) { 1615 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1616 __func__); 1617 break; 1618 } 1619 ctl_copy_sense_data(msg, io); 1620 /* 1621 * IO should have already been cleaned up on other 1622 * SC so clear this flag so we won't send a message 1623 * back to finish the IO there. 1624 */ 1625 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1626 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1627 1628 /* io = msg->hdr.serializing_sc; */ 1629 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1630 ctl_enqueue_isc(io); 1631 break; 1632 1633 /* Handle resets sent from the other side */ 1634 case CTL_MSG_MANAGE_TASKS: { 1635 struct ctl_taskio *taskio; 1636 taskio = (struct ctl_taskio *)ctl_alloc_io( 1637 softc->othersc_pool); 1638 ctl_zero_io((union ctl_io *)taskio); 1639 taskio->io_hdr.io_type = CTL_IO_TASK; 1640 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1641 taskio->io_hdr.nexus = msg->hdr.nexus; 1642 taskio->task_action = msg->task.task_action; 1643 taskio->tag_num = msg->task.tag_num; 1644 taskio->tag_type = msg->task.tag_type; 1645 #ifdef CTL_TIME_IO 1646 taskio->io_hdr.start_time = time_uptime; 1647 getbinuptime(&taskio->io_hdr.start_bt); 1648 #endif /* CTL_TIME_IO */ 1649 ctl_run_task((union ctl_io *)taskio); 1650 break; 1651 } 1652 /* Persistent Reserve action which needs attention */ 1653 case CTL_MSG_PERS_ACTION: 1654 presio = (struct ctl_prio *)ctl_alloc_io( 1655 softc->othersc_pool); 1656 ctl_zero_io((union ctl_io *)presio); 1657 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1658 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1659 presio->io_hdr.nexus = msg->hdr.nexus; 1660 presio->pr_msg = msg->pr; 1661 ctl_enqueue_isc((union ctl_io *)presio); 1662 break; 1663 case CTL_MSG_UA: 1664 ctl_isc_ua(softc, msg, param); 1665 break; 1666 case CTL_MSG_PORT_SYNC: 1667 ctl_isc_port_sync(softc, msg, param); 1668 break; 1669 case CTL_MSG_LUN_SYNC: 1670 ctl_isc_lun_sync(softc, msg, param); 1671 break; 1672 case CTL_MSG_IID_SYNC: 1673 ctl_isc_iid_sync(softc, msg, param); 1674 break; 1675 case CTL_MSG_LOGIN: 1676 ctl_isc_login(softc, msg, param); 1677 break; 1678 case CTL_MSG_MODE_SYNC: 1679 ctl_isc_mode_sync(softc, msg, param); 1680 break; 1681 default: 1682 printf("Received HA message of unknown type %d\n", 1683 msg->hdr.msg_type); 1684 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1685 break; 1686 } 1687 if (msg != &msgbuf) 1688 free(msg, M_CTL); 1689 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1690 printf("CTL: HA link status changed from %d to %d\n", 1691 softc->ha_link, param); 1692 if (param == softc->ha_link) 1693 return; 1694 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1695 softc->ha_link = param; 1696 ctl_isc_ha_link_down(softc); 1697 } else { 1698 softc->ha_link = param; 1699 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1700 ctl_isc_ha_link_up(softc); 1701 } 1702 return; 1703 } else { 1704 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1705 return; 1706 } 1707 } 1708 1709 static void 1710 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1711 { 1712 1713 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1714 src->scsi.sense_len); 1715 dest->scsiio.scsi_status = src->scsi.scsi_status; 1716 dest->scsiio.sense_len = src->scsi.sense_len; 1717 dest->io_hdr.status = src->hdr.status; 1718 } 1719 1720 static void 1721 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1722 { 1723 1724 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1725 src->scsiio.sense_len); 1726 dest->scsi.scsi_status = src->scsiio.scsi_status; 1727 dest->scsi.sense_len = src->scsiio.sense_len; 1728 dest->hdr.status = src->io_hdr.status; 1729 } 1730 1731 void 1732 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1733 { 1734 struct ctl_softc *softc = lun->ctl_softc; 1735 ctl_ua_type *pu; 1736 1737 if (initidx < softc->init_min || initidx >= softc->init_max) 1738 return; 1739 mtx_assert(&lun->lun_lock, MA_OWNED); 1740 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1741 if (pu == NULL) 1742 return; 1743 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1744 } 1745 1746 void 1747 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1748 { 1749 int i; 1750 1751 mtx_assert(&lun->lun_lock, MA_OWNED); 1752 if (lun->pending_ua[port] == NULL) 1753 return; 1754 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1755 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1756 continue; 1757 lun->pending_ua[port][i] |= ua; 1758 } 1759 } 1760 1761 void 1762 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1763 { 1764 struct ctl_softc *softc = lun->ctl_softc; 1765 int i; 1766 1767 mtx_assert(&lun->lun_lock, MA_OWNED); 1768 for (i = softc->port_min; i < softc->port_max; i++) 1769 ctl_est_ua_port(lun, i, except, ua); 1770 } 1771 1772 void 1773 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1774 { 1775 struct ctl_softc *softc = lun->ctl_softc; 1776 ctl_ua_type *pu; 1777 1778 if (initidx < softc->init_min || initidx >= softc->init_max) 1779 return; 1780 mtx_assert(&lun->lun_lock, MA_OWNED); 1781 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1782 if (pu == NULL) 1783 return; 1784 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1785 } 1786 1787 void 1788 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1789 { 1790 struct ctl_softc *softc = lun->ctl_softc; 1791 int i, j; 1792 1793 mtx_assert(&lun->lun_lock, MA_OWNED); 1794 for (i = softc->port_min; i < softc->port_max; i++) { 1795 if (lun->pending_ua[i] == NULL) 1796 continue; 1797 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1798 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1799 continue; 1800 lun->pending_ua[i][j] &= ~ua; 1801 } 1802 } 1803 } 1804 1805 void 1806 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1807 ctl_ua_type ua_type) 1808 { 1809 struct ctl_lun *lun; 1810 1811 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1812 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1813 mtx_lock(&lun->lun_lock); 1814 ctl_clr_ua(lun, initidx, ua_type); 1815 mtx_unlock(&lun->lun_lock); 1816 } 1817 } 1818 1819 static int 1820 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1821 { 1822 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1823 struct ctl_lun *lun; 1824 struct ctl_lun_req ireq; 1825 int error, value; 1826 1827 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1828 error = sysctl_handle_int(oidp, &value, 0, req); 1829 if ((error != 0) || (req->newptr == NULL)) 1830 return (error); 1831 1832 mtx_lock(&softc->ctl_lock); 1833 if (value == 0) 1834 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1835 else 1836 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1837 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1838 mtx_unlock(&softc->ctl_lock); 1839 bzero(&ireq, sizeof(ireq)); 1840 ireq.reqtype = CTL_LUNREQ_MODIFY; 1841 ireq.reqdata.modify.lun_id = lun->lun; 1842 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1843 curthread); 1844 if (ireq.status != CTL_LUN_OK) { 1845 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1846 __func__, ireq.status, ireq.error_str); 1847 } 1848 mtx_lock(&softc->ctl_lock); 1849 } 1850 mtx_unlock(&softc->ctl_lock); 1851 return (0); 1852 } 1853 1854 static int 1855 ctl_init(void) 1856 { 1857 struct make_dev_args args; 1858 struct ctl_softc *softc; 1859 int i, error; 1860 1861 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1862 M_WAITOK | M_ZERO); 1863 1864 make_dev_args_init(&args); 1865 args.mda_devsw = &ctl_cdevsw; 1866 args.mda_uid = UID_ROOT; 1867 args.mda_gid = GID_OPERATOR; 1868 args.mda_mode = 0600; 1869 args.mda_si_drv1 = softc; 1870 args.mda_si_drv2 = NULL; 1871 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1872 if (error != 0) { 1873 free(softc, M_DEVBUF); 1874 control_softc = NULL; 1875 return (error); 1876 } 1877 1878 sysctl_ctx_init(&softc->sysctl_ctx); 1879 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1880 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1881 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer"); 1882 1883 if (softc->sysctl_tree == NULL) { 1884 printf("%s: unable to allocate sysctl tree\n", __func__); 1885 destroy_dev(softc->dev); 1886 free(softc, M_DEVBUF); 1887 control_softc = NULL; 1888 return (ENOMEM); 1889 } 1890 1891 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1892 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1893 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1894 softc->flags = 0; 1895 1896 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1897 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1898 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1899 1900 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) { 1901 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n", 1902 ctl_max_luns, CTL_DEFAULT_MAX_LUNS); 1903 ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 1904 } 1905 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns, 1906 M_DEVBUF, M_WAITOK | M_ZERO); 1907 softc->ctl_lun_mask = malloc(sizeof(uint32_t) * 1908 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1909 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) { 1910 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n", 1911 ctl_max_ports, CTL_DEFAULT_MAX_PORTS); 1912 ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 1913 } 1914 softc->ctl_port_mask = malloc(sizeof(uint32_t) * 1915 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1916 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports, 1917 M_DEVBUF, M_WAITOK | M_ZERO); 1918 1919 /* 1920 * In Copan's HA scheme, the "master" and "slave" roles are 1921 * figured out through the slot the controller is in. Although it 1922 * is an active/active system, someone has to be in charge. 1923 */ 1924 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1925 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1926 "HA head ID (0 - no HA)"); 1927 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1928 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1929 softc->is_single = 1; 1930 softc->port_cnt = ctl_max_ports; 1931 softc->port_min = 0; 1932 } else { 1933 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 1934 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1935 } 1936 softc->port_max = softc->port_min + softc->port_cnt; 1937 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1938 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1939 1940 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1941 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1942 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1943 1944 STAILQ_INIT(&softc->lun_list); 1945 STAILQ_INIT(&softc->fe_list); 1946 STAILQ_INIT(&softc->port_list); 1947 STAILQ_INIT(&softc->be_list); 1948 ctl_tpc_init(softc); 1949 1950 if (worker_threads <= 0) 1951 worker_threads = max(1, mp_ncpus / 4); 1952 if (worker_threads > CTL_MAX_THREADS) 1953 worker_threads = CTL_MAX_THREADS; 1954 1955 for (i = 0; i < worker_threads; i++) { 1956 struct ctl_thread *thr = &softc->threads[i]; 1957 1958 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1959 thr->ctl_softc = softc; 1960 STAILQ_INIT(&thr->incoming_queue); 1961 STAILQ_INIT(&thr->rtr_queue); 1962 STAILQ_INIT(&thr->done_queue); 1963 STAILQ_INIT(&thr->isc_queue); 1964 1965 error = kproc_kthread_add(ctl_work_thread, thr, 1966 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1967 if (error != 0) { 1968 printf("error creating CTL work thread!\n"); 1969 return (error); 1970 } 1971 } 1972 error = kproc_kthread_add(ctl_thresh_thread, softc, 1973 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 1974 if (error != 0) { 1975 printf("error creating CTL threshold thread!\n"); 1976 return (error); 1977 } 1978 1979 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1980 OID_AUTO, "ha_role", 1981 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 1982 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1983 1984 if (softc->is_single == 0) { 1985 if (ctl_frontend_register(&ha_frontend) != 0) 1986 softc->is_single = 1; 1987 } 1988 return (0); 1989 } 1990 1991 static int 1992 ctl_shutdown(void) 1993 { 1994 struct ctl_softc *softc = control_softc; 1995 int i; 1996 1997 if (softc->is_single == 0) 1998 ctl_frontend_deregister(&ha_frontend); 1999 2000 destroy_dev(softc->dev); 2001 2002 /* Shutdown CTL threads. */ 2003 softc->shutdown = 1; 2004 for (i = 0; i < worker_threads; i++) { 2005 struct ctl_thread *thr = &softc->threads[i]; 2006 while (thr->thread != NULL) { 2007 wakeup(thr); 2008 if (thr->thread != NULL) 2009 pause("CTL thr shutdown", 1); 2010 } 2011 mtx_destroy(&thr->queue_lock); 2012 } 2013 while (softc->thresh_thread != NULL) { 2014 wakeup(softc->thresh_thread); 2015 if (softc->thresh_thread != NULL) 2016 pause("CTL thr shutdown", 1); 2017 } 2018 2019 ctl_tpc_shutdown(softc); 2020 uma_zdestroy(softc->io_zone); 2021 mtx_destroy(&softc->ctl_lock); 2022 2023 free(softc->ctl_luns, M_DEVBUF); 2024 free(softc->ctl_lun_mask, M_DEVBUF); 2025 free(softc->ctl_port_mask, M_DEVBUF); 2026 free(softc->ctl_ports, M_DEVBUF); 2027 2028 sysctl_ctx_free(&softc->sysctl_ctx); 2029 2030 free(softc, M_DEVBUF); 2031 control_softc = NULL; 2032 return (0); 2033 } 2034 2035 static int 2036 ctl_module_event_handler(module_t mod, int what, void *arg) 2037 { 2038 2039 switch (what) { 2040 case MOD_LOAD: 2041 return (ctl_init()); 2042 case MOD_UNLOAD: 2043 return (ctl_shutdown()); 2044 default: 2045 return (EOPNOTSUPP); 2046 } 2047 } 2048 2049 /* 2050 * XXX KDM should we do some access checks here? Bump a reference count to 2051 * prevent a CTL module from being unloaded while someone has it open? 2052 */ 2053 static int 2054 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2055 { 2056 return (0); 2057 } 2058 2059 static int 2060 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2061 { 2062 return (0); 2063 } 2064 2065 /* 2066 * Remove an initiator by port number and initiator ID. 2067 * Returns 0 for success, -1 for failure. 2068 */ 2069 int 2070 ctl_remove_initiator(struct ctl_port *port, int iid) 2071 { 2072 struct ctl_softc *softc = port->ctl_softc; 2073 int last; 2074 2075 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2076 2077 if (iid > CTL_MAX_INIT_PER_PORT) { 2078 printf("%s: initiator ID %u > maximun %u!\n", 2079 __func__, iid, CTL_MAX_INIT_PER_PORT); 2080 return (-1); 2081 } 2082 2083 mtx_lock(&softc->ctl_lock); 2084 last = (--port->wwpn_iid[iid].in_use == 0); 2085 port->wwpn_iid[iid].last_use = time_uptime; 2086 mtx_unlock(&softc->ctl_lock); 2087 if (last) 2088 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2089 ctl_isc_announce_iid(port, iid); 2090 2091 return (0); 2092 } 2093 2094 /* 2095 * Add an initiator to the initiator map. 2096 * Returns iid for success, < 0 for failure. 2097 */ 2098 int 2099 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2100 { 2101 struct ctl_softc *softc = port->ctl_softc; 2102 time_t best_time; 2103 int i, best; 2104 2105 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2106 2107 if (iid >= CTL_MAX_INIT_PER_PORT) { 2108 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2109 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2110 free(name, M_CTL); 2111 return (-1); 2112 } 2113 2114 mtx_lock(&softc->ctl_lock); 2115 2116 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2117 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2118 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2119 iid = i; 2120 break; 2121 } 2122 if (name != NULL && port->wwpn_iid[i].name != NULL && 2123 strcmp(name, port->wwpn_iid[i].name) == 0) { 2124 iid = i; 2125 break; 2126 } 2127 } 2128 } 2129 2130 if (iid < 0) { 2131 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2132 if (port->wwpn_iid[i].in_use == 0 && 2133 port->wwpn_iid[i].wwpn == 0 && 2134 port->wwpn_iid[i].name == NULL) { 2135 iid = i; 2136 break; 2137 } 2138 } 2139 } 2140 2141 if (iid < 0) { 2142 best = -1; 2143 best_time = INT32_MAX; 2144 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2145 if (port->wwpn_iid[i].in_use == 0) { 2146 if (port->wwpn_iid[i].last_use < best_time) { 2147 best = i; 2148 best_time = port->wwpn_iid[i].last_use; 2149 } 2150 } 2151 } 2152 iid = best; 2153 } 2154 2155 if (iid < 0) { 2156 mtx_unlock(&softc->ctl_lock); 2157 free(name, M_CTL); 2158 return (-2); 2159 } 2160 2161 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2162 /* 2163 * This is not an error yet. 2164 */ 2165 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2166 #if 0 2167 printf("%s: port %d iid %u WWPN %#jx arrived" 2168 " again\n", __func__, port->targ_port, 2169 iid, (uintmax_t)wwpn); 2170 #endif 2171 goto take; 2172 } 2173 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2174 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2175 #if 0 2176 printf("%s: port %d iid %u name '%s' arrived" 2177 " again\n", __func__, port->targ_port, 2178 iid, name); 2179 #endif 2180 goto take; 2181 } 2182 2183 /* 2184 * This is an error, but what do we do about it? The 2185 * driver is telling us we have a new WWPN for this 2186 * initiator ID, so we pretty much need to use it. 2187 */ 2188 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2189 " but WWPN %#jx '%s' is still at that address\n", 2190 __func__, port->targ_port, iid, wwpn, name, 2191 (uintmax_t)port->wwpn_iid[iid].wwpn, 2192 port->wwpn_iid[iid].name); 2193 } 2194 take: 2195 free(port->wwpn_iid[iid].name, M_CTL); 2196 port->wwpn_iid[iid].name = name; 2197 port->wwpn_iid[iid].wwpn = wwpn; 2198 port->wwpn_iid[iid].in_use++; 2199 mtx_unlock(&softc->ctl_lock); 2200 ctl_isc_announce_iid(port, iid); 2201 2202 return (iid); 2203 } 2204 2205 static int 2206 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2207 { 2208 int len; 2209 2210 switch (port->port_type) { 2211 case CTL_PORT_FC: 2212 { 2213 struct scsi_transportid_fcp *id = 2214 (struct scsi_transportid_fcp *)buf; 2215 if (port->wwpn_iid[iid].wwpn == 0) 2216 return (0); 2217 memset(id, 0, sizeof(*id)); 2218 id->format_protocol = SCSI_PROTO_FC; 2219 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2220 return (sizeof(*id)); 2221 } 2222 case CTL_PORT_ISCSI: 2223 { 2224 struct scsi_transportid_iscsi_port *id = 2225 (struct scsi_transportid_iscsi_port *)buf; 2226 if (port->wwpn_iid[iid].name == NULL) 2227 return (0); 2228 memset(id, 0, 256); 2229 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2230 SCSI_PROTO_ISCSI; 2231 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2232 len = roundup2(min(len, 252), 4); 2233 scsi_ulto2b(len, id->additional_length); 2234 return (sizeof(*id) + len); 2235 } 2236 case CTL_PORT_SAS: 2237 { 2238 struct scsi_transportid_sas *id = 2239 (struct scsi_transportid_sas *)buf; 2240 if (port->wwpn_iid[iid].wwpn == 0) 2241 return (0); 2242 memset(id, 0, sizeof(*id)); 2243 id->format_protocol = SCSI_PROTO_SAS; 2244 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2245 return (sizeof(*id)); 2246 } 2247 default: 2248 { 2249 struct scsi_transportid_spi *id = 2250 (struct scsi_transportid_spi *)buf; 2251 memset(id, 0, sizeof(*id)); 2252 id->format_protocol = SCSI_PROTO_SPI; 2253 scsi_ulto2b(iid, id->scsi_addr); 2254 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2255 return (sizeof(*id)); 2256 } 2257 } 2258 } 2259 2260 /* 2261 * Serialize a command that went down the "wrong" side, and so was sent to 2262 * this controller for execution. The logic is a little different than the 2263 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2264 * sent back to the other side, but in the success case, we execute the 2265 * command on this side (XFER mode) or tell the other side to execute it 2266 * (SER_ONLY mode). 2267 */ 2268 static void 2269 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2270 { 2271 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2272 struct ctl_port *port = CTL_PORT(ctsio); 2273 union ctl_ha_msg msg_info; 2274 struct ctl_lun *lun; 2275 const struct ctl_cmd_entry *entry; 2276 union ctl_io *bio; 2277 uint32_t targ_lun; 2278 2279 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2280 2281 /* Make sure that we know about this port. */ 2282 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2283 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2284 /*retry_count*/ 1); 2285 goto badjuju; 2286 } 2287 2288 /* Make sure that we know about this LUN. */ 2289 mtx_lock(&softc->ctl_lock); 2290 if (targ_lun >= ctl_max_luns || 2291 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2292 mtx_unlock(&softc->ctl_lock); 2293 2294 /* 2295 * The other node would not send this request to us unless 2296 * received announce that we are primary node for this LUN. 2297 * If this LUN does not exist now, it is probably result of 2298 * a race, so respond to initiator in the most opaque way. 2299 */ 2300 ctl_set_busy(ctsio); 2301 goto badjuju; 2302 } 2303 mtx_lock(&lun->lun_lock); 2304 mtx_unlock(&softc->ctl_lock); 2305 2306 /* 2307 * If the LUN is invalid, pretend that it doesn't exist. 2308 * It will go away as soon as all pending I/Os completed. 2309 */ 2310 if (lun->flags & CTL_LUN_DISABLED) { 2311 mtx_unlock(&lun->lun_lock); 2312 ctl_set_busy(ctsio); 2313 goto badjuju; 2314 } 2315 2316 entry = ctl_get_cmd_entry(ctsio, NULL); 2317 ctsio->seridx = entry->seridx; 2318 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2319 mtx_unlock(&lun->lun_lock); 2320 goto badjuju; 2321 } 2322 2323 CTL_LUN(ctsio) = lun; 2324 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2325 2326 /* 2327 * Every I/O goes into the OOA queue for a 2328 * particular LUN, and stays there until completion. 2329 */ 2330 #ifdef CTL_TIME_IO 2331 if (LIST_EMPTY(&lun->ooa_queue)) 2332 lun->idle_time += getsbinuptime() - lun->last_busy; 2333 #endif 2334 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2335 2336 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 2337 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 2338 case CTL_ACTION_PASS: 2339 case CTL_ACTION_SKIP: 2340 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2341 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2342 ctl_enqueue_rtr((union ctl_io *)ctsio); 2343 mtx_unlock(&lun->lun_lock); 2344 } else { 2345 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2346 mtx_unlock(&lun->lun_lock); 2347 2348 /* send msg back to other side */ 2349 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2350 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2351 msg_info.hdr.msg_type = CTL_MSG_R2R; 2352 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2353 sizeof(msg_info.hdr), M_WAITOK); 2354 } 2355 break; 2356 case CTL_ACTION_BLOCK: 2357 ctsio->io_hdr.blocker = bio; 2358 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 2359 blocked_links); 2360 mtx_unlock(&lun->lun_lock); 2361 break; 2362 case CTL_ACTION_OVERLAP: 2363 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2364 mtx_unlock(&lun->lun_lock); 2365 ctl_set_overlapped_cmd(ctsio); 2366 goto badjuju; 2367 case CTL_ACTION_OVERLAP_TAG: 2368 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2369 mtx_unlock(&lun->lun_lock); 2370 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2371 badjuju: 2372 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2373 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2374 msg_info.hdr.serializing_sc = NULL; 2375 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2376 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2377 sizeof(msg_info.scsi), M_WAITOK); 2378 ctl_free_io((union ctl_io *)ctsio); 2379 break; 2380 default: 2381 __assert_unreachable(); 2382 } 2383 } 2384 2385 /* 2386 * Returns 0 for success, errno for failure. 2387 */ 2388 static void 2389 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2390 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2391 { 2392 struct ctl_io_hdr *ioh; 2393 2394 mtx_lock(&lun->lun_lock); 2395 ioh = LIST_FIRST(&lun->ooa_queue); 2396 if (ioh == NULL) { 2397 mtx_unlock(&lun->lun_lock); 2398 return; 2399 } 2400 while (LIST_NEXT(ioh, ooa_links) != NULL) 2401 ioh = LIST_NEXT(ioh, ooa_links); 2402 for ( ; ioh; ioh = LIST_PREV(ioh, &lun->ooa_queue, ctl_io_hdr, ooa_links)) { 2403 union ctl_io *io = (union ctl_io *)ioh; 2404 struct ctl_ooa_entry *entry; 2405 2406 /* 2407 * If we've got more than we can fit, just count the 2408 * remaining entries. 2409 */ 2410 if (*cur_fill_num >= ooa_hdr->alloc_num) { 2411 (*cur_fill_num)++; 2412 continue; 2413 } 2414 2415 entry = &kern_entries[*cur_fill_num]; 2416 2417 entry->tag_num = io->scsiio.tag_num; 2418 entry->lun_num = lun->lun; 2419 #ifdef CTL_TIME_IO 2420 entry->start_bt = io->io_hdr.start_bt; 2421 #endif 2422 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2423 entry->cdb_len = io->scsiio.cdb_len; 2424 if (io->io_hdr.blocker != NULL) 2425 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2426 2427 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2428 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2429 2430 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2431 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2432 2433 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2434 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2435 2436 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2437 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2438 2439 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) 2440 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED; 2441 2442 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) 2443 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT; 2444 (*cur_fill_num)++; 2445 } 2446 mtx_unlock(&lun->lun_lock); 2447 } 2448 2449 /* 2450 * Escape characters that are illegal or not recommended in XML. 2451 */ 2452 int 2453 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2454 { 2455 char *end = str + size; 2456 int retval; 2457 2458 retval = 0; 2459 2460 for (; *str && str < end; str++) { 2461 switch (*str) { 2462 case '&': 2463 retval = sbuf_printf(sb, "&"); 2464 break; 2465 case '>': 2466 retval = sbuf_printf(sb, ">"); 2467 break; 2468 case '<': 2469 retval = sbuf_printf(sb, "<"); 2470 break; 2471 default: 2472 retval = sbuf_putc(sb, *str); 2473 break; 2474 } 2475 2476 if (retval != 0) 2477 break; 2478 } 2479 2480 return (retval); 2481 } 2482 2483 static void 2484 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2485 { 2486 struct scsi_vpd_id_descriptor *desc; 2487 int i; 2488 2489 if (id == NULL || id->len < 4) 2490 return; 2491 desc = (struct scsi_vpd_id_descriptor *)id->data; 2492 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2493 case SVPD_ID_TYPE_T10: 2494 sbuf_printf(sb, "t10."); 2495 break; 2496 case SVPD_ID_TYPE_EUI64: 2497 sbuf_printf(sb, "eui."); 2498 break; 2499 case SVPD_ID_TYPE_NAA: 2500 sbuf_printf(sb, "naa."); 2501 break; 2502 case SVPD_ID_TYPE_SCSI_NAME: 2503 break; 2504 } 2505 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2506 case SVPD_ID_CODESET_BINARY: 2507 for (i = 0; i < desc->length; i++) 2508 sbuf_printf(sb, "%02x", desc->identifier[i]); 2509 break; 2510 case SVPD_ID_CODESET_ASCII: 2511 sbuf_printf(sb, "%.*s", (int)desc->length, 2512 (char *)desc->identifier); 2513 break; 2514 case SVPD_ID_CODESET_UTF8: 2515 sbuf_printf(sb, "%s", (char *)desc->identifier); 2516 break; 2517 } 2518 } 2519 2520 static int 2521 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2522 struct thread *td) 2523 { 2524 struct ctl_softc *softc = dev->si_drv1; 2525 struct ctl_port *port; 2526 struct ctl_lun *lun; 2527 int retval; 2528 2529 retval = 0; 2530 2531 switch (cmd) { 2532 case CTL_IO: 2533 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2534 break; 2535 case CTL_ENABLE_PORT: 2536 case CTL_DISABLE_PORT: 2537 case CTL_SET_PORT_WWNS: { 2538 struct ctl_port *port; 2539 struct ctl_port_entry *entry; 2540 2541 entry = (struct ctl_port_entry *)addr; 2542 2543 mtx_lock(&softc->ctl_lock); 2544 STAILQ_FOREACH(port, &softc->port_list, links) { 2545 int action, done; 2546 2547 if (port->targ_port < softc->port_min || 2548 port->targ_port >= softc->port_max) 2549 continue; 2550 2551 action = 0; 2552 done = 0; 2553 if ((entry->port_type == CTL_PORT_NONE) 2554 && (entry->targ_port == port->targ_port)) { 2555 /* 2556 * If the user only wants to enable or 2557 * disable or set WWNs on a specific port, 2558 * do the operation and we're done. 2559 */ 2560 action = 1; 2561 done = 1; 2562 } else if (entry->port_type & port->port_type) { 2563 /* 2564 * Compare the user's type mask with the 2565 * particular frontend type to see if we 2566 * have a match. 2567 */ 2568 action = 1; 2569 done = 0; 2570 2571 /* 2572 * Make sure the user isn't trying to set 2573 * WWNs on multiple ports at the same time. 2574 */ 2575 if (cmd == CTL_SET_PORT_WWNS) { 2576 printf("%s: Can't set WWNs on " 2577 "multiple ports\n", __func__); 2578 retval = EINVAL; 2579 break; 2580 } 2581 } 2582 if (action == 0) 2583 continue; 2584 2585 /* 2586 * XXX KDM we have to drop the lock here, because 2587 * the online/offline operations can potentially 2588 * block. We need to reference count the frontends 2589 * so they can't go away, 2590 */ 2591 if (cmd == CTL_ENABLE_PORT) { 2592 mtx_unlock(&softc->ctl_lock); 2593 ctl_port_online(port); 2594 mtx_lock(&softc->ctl_lock); 2595 } else if (cmd == CTL_DISABLE_PORT) { 2596 mtx_unlock(&softc->ctl_lock); 2597 ctl_port_offline(port); 2598 mtx_lock(&softc->ctl_lock); 2599 } else if (cmd == CTL_SET_PORT_WWNS) { 2600 ctl_port_set_wwns(port, 2601 (entry->flags & CTL_PORT_WWNN_VALID) ? 2602 1 : 0, entry->wwnn, 2603 (entry->flags & CTL_PORT_WWPN_VALID) ? 2604 1 : 0, entry->wwpn); 2605 } 2606 if (done != 0) 2607 break; 2608 } 2609 mtx_unlock(&softc->ctl_lock); 2610 break; 2611 } 2612 case CTL_GET_OOA: { 2613 struct ctl_ooa *ooa_hdr; 2614 struct ctl_ooa_entry *entries; 2615 uint32_t cur_fill_num; 2616 2617 ooa_hdr = (struct ctl_ooa *)addr; 2618 2619 if ((ooa_hdr->alloc_len == 0) 2620 || (ooa_hdr->alloc_num == 0)) { 2621 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2622 "must be non-zero\n", __func__, 2623 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2624 retval = EINVAL; 2625 break; 2626 } 2627 2628 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2629 sizeof(struct ctl_ooa_entry))) { 2630 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2631 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2632 __func__, ooa_hdr->alloc_len, 2633 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2634 retval = EINVAL; 2635 break; 2636 } 2637 2638 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2639 if (entries == NULL) { 2640 printf("%s: could not allocate %d bytes for OOA " 2641 "dump\n", __func__, ooa_hdr->alloc_len); 2642 retval = ENOMEM; 2643 break; 2644 } 2645 2646 mtx_lock(&softc->ctl_lock); 2647 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2648 (ooa_hdr->lun_num >= ctl_max_luns || 2649 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2650 mtx_unlock(&softc->ctl_lock); 2651 free(entries, M_CTL); 2652 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2653 __func__, (uintmax_t)ooa_hdr->lun_num); 2654 retval = EINVAL; 2655 break; 2656 } 2657 2658 cur_fill_num = 0; 2659 2660 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2661 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2662 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2663 ooa_hdr, entries); 2664 } 2665 } else { 2666 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2667 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2668 entries); 2669 } 2670 mtx_unlock(&softc->ctl_lock); 2671 2672 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2673 ooa_hdr->fill_len = ooa_hdr->fill_num * 2674 sizeof(struct ctl_ooa_entry); 2675 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2676 if (retval != 0) { 2677 printf("%s: error copying out %d bytes for OOA dump\n", 2678 __func__, ooa_hdr->fill_len); 2679 } 2680 2681 getbinuptime(&ooa_hdr->cur_bt); 2682 2683 if (cur_fill_num > ooa_hdr->alloc_num) { 2684 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2685 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2686 } else { 2687 ooa_hdr->dropped_num = 0; 2688 ooa_hdr->status = CTL_OOA_OK; 2689 } 2690 2691 free(entries, M_CTL); 2692 break; 2693 } 2694 case CTL_DELAY_IO: { 2695 struct ctl_io_delay_info *delay_info; 2696 2697 delay_info = (struct ctl_io_delay_info *)addr; 2698 2699 #ifdef CTL_IO_DELAY 2700 mtx_lock(&softc->ctl_lock); 2701 if (delay_info->lun_id >= ctl_max_luns || 2702 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2703 mtx_unlock(&softc->ctl_lock); 2704 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2705 break; 2706 } 2707 mtx_lock(&lun->lun_lock); 2708 mtx_unlock(&softc->ctl_lock); 2709 delay_info->status = CTL_DELAY_STATUS_OK; 2710 switch (delay_info->delay_type) { 2711 case CTL_DELAY_TYPE_CONT: 2712 case CTL_DELAY_TYPE_ONESHOT: 2713 break; 2714 default: 2715 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2716 break; 2717 } 2718 switch (delay_info->delay_loc) { 2719 case CTL_DELAY_LOC_DATAMOVE: 2720 lun->delay_info.datamove_type = delay_info->delay_type; 2721 lun->delay_info.datamove_delay = delay_info->delay_secs; 2722 break; 2723 case CTL_DELAY_LOC_DONE: 2724 lun->delay_info.done_type = delay_info->delay_type; 2725 lun->delay_info.done_delay = delay_info->delay_secs; 2726 break; 2727 default: 2728 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2729 break; 2730 } 2731 mtx_unlock(&lun->lun_lock); 2732 #else 2733 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2734 #endif /* CTL_IO_DELAY */ 2735 break; 2736 } 2737 case CTL_ERROR_INJECT: { 2738 struct ctl_error_desc *err_desc, *new_err_desc; 2739 2740 err_desc = (struct ctl_error_desc *)addr; 2741 2742 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2743 M_WAITOK | M_ZERO); 2744 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2745 2746 mtx_lock(&softc->ctl_lock); 2747 if (err_desc->lun_id >= ctl_max_luns || 2748 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2749 mtx_unlock(&softc->ctl_lock); 2750 free(new_err_desc, M_CTL); 2751 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2752 __func__, (uintmax_t)err_desc->lun_id); 2753 retval = EINVAL; 2754 break; 2755 } 2756 mtx_lock(&lun->lun_lock); 2757 mtx_unlock(&softc->ctl_lock); 2758 2759 /* 2760 * We could do some checking here to verify the validity 2761 * of the request, but given the complexity of error 2762 * injection requests, the checking logic would be fairly 2763 * complex. 2764 * 2765 * For now, if the request is invalid, it just won't get 2766 * executed and might get deleted. 2767 */ 2768 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2769 2770 /* 2771 * XXX KDM check to make sure the serial number is unique, 2772 * in case we somehow manage to wrap. That shouldn't 2773 * happen for a very long time, but it's the right thing to 2774 * do. 2775 */ 2776 new_err_desc->serial = lun->error_serial; 2777 err_desc->serial = lun->error_serial; 2778 lun->error_serial++; 2779 2780 mtx_unlock(&lun->lun_lock); 2781 break; 2782 } 2783 case CTL_ERROR_INJECT_DELETE: { 2784 struct ctl_error_desc *delete_desc, *desc, *desc2; 2785 int delete_done; 2786 2787 delete_desc = (struct ctl_error_desc *)addr; 2788 delete_done = 0; 2789 2790 mtx_lock(&softc->ctl_lock); 2791 if (delete_desc->lun_id >= ctl_max_luns || 2792 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2793 mtx_unlock(&softc->ctl_lock); 2794 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2795 __func__, (uintmax_t)delete_desc->lun_id); 2796 retval = EINVAL; 2797 break; 2798 } 2799 mtx_lock(&lun->lun_lock); 2800 mtx_unlock(&softc->ctl_lock); 2801 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2802 if (desc->serial != delete_desc->serial) 2803 continue; 2804 2805 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2806 links); 2807 free(desc, M_CTL); 2808 delete_done = 1; 2809 } 2810 mtx_unlock(&lun->lun_lock); 2811 if (delete_done == 0) { 2812 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2813 "error serial %ju on LUN %u\n", __func__, 2814 delete_desc->serial, delete_desc->lun_id); 2815 retval = EINVAL; 2816 break; 2817 } 2818 break; 2819 } 2820 case CTL_DUMP_STRUCTS: { 2821 int j, k; 2822 struct ctl_port *port; 2823 struct ctl_frontend *fe; 2824 2825 mtx_lock(&softc->ctl_lock); 2826 printf("CTL Persistent Reservation information start:\n"); 2827 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2828 mtx_lock(&lun->lun_lock); 2829 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2830 mtx_unlock(&lun->lun_lock); 2831 continue; 2832 } 2833 2834 for (j = 0; j < ctl_max_ports; j++) { 2835 if (lun->pr_keys[j] == NULL) 2836 continue; 2837 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2838 if (lun->pr_keys[j][k] == 0) 2839 continue; 2840 printf(" LUN %ju port %d iid %d key " 2841 "%#jx\n", lun->lun, j, k, 2842 (uintmax_t)lun->pr_keys[j][k]); 2843 } 2844 } 2845 mtx_unlock(&lun->lun_lock); 2846 } 2847 printf("CTL Persistent Reservation information end\n"); 2848 printf("CTL Ports:\n"); 2849 STAILQ_FOREACH(port, &softc->port_list, links) { 2850 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2851 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2852 port->frontend->name, port->port_type, 2853 port->physical_port, port->virtual_port, 2854 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2855 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2856 if (port->wwpn_iid[j].in_use == 0 && 2857 port->wwpn_iid[j].wwpn == 0 && 2858 port->wwpn_iid[j].name == NULL) 2859 continue; 2860 2861 printf(" iid %u use %d WWPN %#jx '%s'\n", 2862 j, port->wwpn_iid[j].in_use, 2863 (uintmax_t)port->wwpn_iid[j].wwpn, 2864 port->wwpn_iid[j].name); 2865 } 2866 } 2867 printf("CTL Port information end\n"); 2868 mtx_unlock(&softc->ctl_lock); 2869 /* 2870 * XXX KDM calling this without a lock. We'd likely want 2871 * to drop the lock before calling the frontend's dump 2872 * routine anyway. 2873 */ 2874 printf("CTL Frontends:\n"); 2875 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2876 printf(" Frontend '%s'\n", fe->name); 2877 if (fe->fe_dump != NULL) 2878 fe->fe_dump(); 2879 } 2880 printf("CTL Frontend information end\n"); 2881 break; 2882 } 2883 case CTL_LUN_REQ: { 2884 struct ctl_lun_req *lun_req; 2885 struct ctl_backend_driver *backend; 2886 void *packed; 2887 nvlist_t *tmp_args_nvl; 2888 size_t packed_len; 2889 2890 lun_req = (struct ctl_lun_req *)addr; 2891 tmp_args_nvl = lun_req->args_nvl; 2892 2893 backend = ctl_backend_find(lun_req->backend); 2894 if (backend == NULL) { 2895 lun_req->status = CTL_LUN_ERROR; 2896 snprintf(lun_req->error_str, 2897 sizeof(lun_req->error_str), 2898 "Backend \"%s\" not found.", 2899 lun_req->backend); 2900 break; 2901 } 2902 2903 if (lun_req->args != NULL) { 2904 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2905 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2906 free(packed, M_CTL); 2907 lun_req->status = CTL_LUN_ERROR; 2908 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2909 "Cannot copyin args."); 2910 break; 2911 } 2912 lun_req->args_nvl = nvlist_unpack(packed, 2913 lun_req->args_len, 0); 2914 free(packed, M_CTL); 2915 2916 if (lun_req->args_nvl == NULL) { 2917 lun_req->status = CTL_LUN_ERROR; 2918 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2919 "Cannot unpack args nvlist."); 2920 break; 2921 } 2922 } else 2923 lun_req->args_nvl = nvlist_create(0); 2924 2925 retval = backend->ioctl(dev, cmd, addr, flag, td); 2926 nvlist_destroy(lun_req->args_nvl); 2927 lun_req->args_nvl = tmp_args_nvl; 2928 2929 if (lun_req->result_nvl != NULL) { 2930 if (lun_req->result != NULL) { 2931 packed = nvlist_pack(lun_req->result_nvl, 2932 &packed_len); 2933 if (packed == NULL) { 2934 lun_req->status = CTL_LUN_ERROR; 2935 snprintf(lun_req->error_str, 2936 sizeof(lun_req->error_str), 2937 "Cannot pack result nvlist."); 2938 break; 2939 } 2940 2941 if (packed_len > lun_req->result_len) { 2942 lun_req->status = CTL_LUN_ERROR; 2943 snprintf(lun_req->error_str, 2944 sizeof(lun_req->error_str), 2945 "Result nvlist too large."); 2946 free(packed, M_NVLIST); 2947 break; 2948 } 2949 2950 if (copyout(packed, lun_req->result, packed_len)) { 2951 lun_req->status = CTL_LUN_ERROR; 2952 snprintf(lun_req->error_str, 2953 sizeof(lun_req->error_str), 2954 "Cannot copyout() the result."); 2955 free(packed, M_NVLIST); 2956 break; 2957 } 2958 2959 lun_req->result_len = packed_len; 2960 free(packed, M_NVLIST); 2961 } 2962 2963 nvlist_destroy(lun_req->result_nvl); 2964 } 2965 break; 2966 } 2967 case CTL_LUN_LIST: { 2968 struct sbuf *sb; 2969 struct ctl_lun_list *list; 2970 const char *name, *value; 2971 void *cookie; 2972 int type; 2973 2974 list = (struct ctl_lun_list *)addr; 2975 2976 /* 2977 * Allocate a fixed length sbuf here, based on the length 2978 * of the user's buffer. We could allocate an auto-extending 2979 * buffer, and then tell the user how much larger our 2980 * amount of data is than his buffer, but that presents 2981 * some problems: 2982 * 2983 * 1. The sbuf(9) routines use a blocking malloc, and so 2984 * we can't hold a lock while calling them with an 2985 * auto-extending buffer. 2986 * 2987 * 2. There is not currently a LUN reference counting 2988 * mechanism, outside of outstanding transactions on 2989 * the LUN's OOA queue. So a LUN could go away on us 2990 * while we're getting the LUN number, backend-specific 2991 * information, etc. Thus, given the way things 2992 * currently work, we need to hold the CTL lock while 2993 * grabbing LUN information. 2994 * 2995 * So, from the user's standpoint, the best thing to do is 2996 * allocate what he thinks is a reasonable buffer length, 2997 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2998 * double the buffer length and try again. (And repeat 2999 * that until he succeeds.) 3000 */ 3001 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3002 if (sb == NULL) { 3003 list->status = CTL_LUN_LIST_ERROR; 3004 snprintf(list->error_str, sizeof(list->error_str), 3005 "Unable to allocate %d bytes for LUN list", 3006 list->alloc_len); 3007 break; 3008 } 3009 3010 sbuf_printf(sb, "<ctllunlist>\n"); 3011 3012 mtx_lock(&softc->ctl_lock); 3013 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3014 mtx_lock(&lun->lun_lock); 3015 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3016 (uintmax_t)lun->lun); 3017 3018 /* 3019 * Bail out as soon as we see that we've overfilled 3020 * the buffer. 3021 */ 3022 if (retval != 0) 3023 break; 3024 3025 retval = sbuf_printf(sb, "\t<backend_type>%s" 3026 "</backend_type>\n", 3027 (lun->backend == NULL) ? "none" : 3028 lun->backend->name); 3029 3030 if (retval != 0) 3031 break; 3032 3033 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3034 lun->be_lun->lun_type); 3035 3036 if (retval != 0) 3037 break; 3038 3039 if (lun->backend == NULL) { 3040 retval = sbuf_printf(sb, "</lun>\n"); 3041 if (retval != 0) 3042 break; 3043 continue; 3044 } 3045 3046 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3047 (lun->be_lun->maxlba > 0) ? 3048 lun->be_lun->maxlba + 1 : 0); 3049 3050 if (retval != 0) 3051 break; 3052 3053 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3054 lun->be_lun->blocksize); 3055 3056 if (retval != 0) 3057 break; 3058 3059 retval = sbuf_printf(sb, "\t<serial_number>"); 3060 3061 if (retval != 0) 3062 break; 3063 3064 retval = ctl_sbuf_printf_esc(sb, 3065 lun->be_lun->serial_num, 3066 sizeof(lun->be_lun->serial_num)); 3067 3068 if (retval != 0) 3069 break; 3070 3071 retval = sbuf_printf(sb, "</serial_number>\n"); 3072 3073 if (retval != 0) 3074 break; 3075 3076 retval = sbuf_printf(sb, "\t<device_id>"); 3077 3078 if (retval != 0) 3079 break; 3080 3081 retval = ctl_sbuf_printf_esc(sb, 3082 lun->be_lun->device_id, 3083 sizeof(lun->be_lun->device_id)); 3084 3085 if (retval != 0) 3086 break; 3087 3088 retval = sbuf_printf(sb, "</device_id>\n"); 3089 3090 if (retval != 0) 3091 break; 3092 3093 if (lun->backend->lun_info != NULL) { 3094 retval = lun->backend->lun_info(lun->be_lun, sb); 3095 if (retval != 0) 3096 break; 3097 } 3098 3099 cookie = NULL; 3100 while ((name = nvlist_next(lun->be_lun->options, &type, 3101 &cookie)) != NULL) { 3102 sbuf_printf(sb, "\t<%s>", name); 3103 3104 if (type == NV_TYPE_STRING) { 3105 value = dnvlist_get_string( 3106 lun->be_lun->options, name, NULL); 3107 if (value != NULL) 3108 sbuf_printf(sb, "%s", value); 3109 } 3110 3111 sbuf_printf(sb, "</%s>\n", name); 3112 } 3113 3114 retval = sbuf_printf(sb, "</lun>\n"); 3115 3116 if (retval != 0) 3117 break; 3118 mtx_unlock(&lun->lun_lock); 3119 } 3120 if (lun != NULL) 3121 mtx_unlock(&lun->lun_lock); 3122 mtx_unlock(&softc->ctl_lock); 3123 3124 if ((retval != 0) 3125 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3126 retval = 0; 3127 sbuf_delete(sb); 3128 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3129 snprintf(list->error_str, sizeof(list->error_str), 3130 "Out of space, %d bytes is too small", 3131 list->alloc_len); 3132 break; 3133 } 3134 3135 sbuf_finish(sb); 3136 3137 retval = copyout(sbuf_data(sb), list->lun_xml, 3138 sbuf_len(sb) + 1); 3139 3140 list->fill_len = sbuf_len(sb) + 1; 3141 list->status = CTL_LUN_LIST_OK; 3142 sbuf_delete(sb); 3143 break; 3144 } 3145 case CTL_ISCSI: { 3146 struct ctl_iscsi *ci; 3147 struct ctl_frontend *fe; 3148 3149 ci = (struct ctl_iscsi *)addr; 3150 3151 fe = ctl_frontend_find("iscsi"); 3152 if (fe == NULL) { 3153 ci->status = CTL_ISCSI_ERROR; 3154 snprintf(ci->error_str, sizeof(ci->error_str), 3155 "Frontend \"iscsi\" not found."); 3156 break; 3157 } 3158 3159 retval = fe->ioctl(dev, cmd, addr, flag, td); 3160 break; 3161 } 3162 case CTL_PORT_REQ: { 3163 struct ctl_req *req; 3164 struct ctl_frontend *fe; 3165 void *packed; 3166 nvlist_t *tmp_args_nvl; 3167 size_t packed_len; 3168 3169 req = (struct ctl_req *)addr; 3170 tmp_args_nvl = req->args_nvl; 3171 3172 fe = ctl_frontend_find(req->driver); 3173 if (fe == NULL) { 3174 req->status = CTL_LUN_ERROR; 3175 snprintf(req->error_str, sizeof(req->error_str), 3176 "Frontend \"%s\" not found.", req->driver); 3177 break; 3178 } 3179 3180 if (req->args != NULL) { 3181 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3182 if (copyin(req->args, packed, req->args_len) != 0) { 3183 free(packed, M_CTL); 3184 req->status = CTL_LUN_ERROR; 3185 snprintf(req->error_str, sizeof(req->error_str), 3186 "Cannot copyin args."); 3187 break; 3188 } 3189 req->args_nvl = nvlist_unpack(packed, 3190 req->args_len, 0); 3191 free(packed, M_CTL); 3192 3193 if (req->args_nvl == NULL) { 3194 req->status = CTL_LUN_ERROR; 3195 snprintf(req->error_str, sizeof(req->error_str), 3196 "Cannot unpack args nvlist."); 3197 break; 3198 } 3199 } else 3200 req->args_nvl = nvlist_create(0); 3201 3202 if (fe->ioctl) 3203 retval = fe->ioctl(dev, cmd, addr, flag, td); 3204 else 3205 retval = ENODEV; 3206 3207 nvlist_destroy(req->args_nvl); 3208 req->args_nvl = tmp_args_nvl; 3209 3210 if (req->result_nvl != NULL) { 3211 if (req->result != NULL) { 3212 packed = nvlist_pack(req->result_nvl, 3213 &packed_len); 3214 if (packed == NULL) { 3215 req->status = CTL_LUN_ERROR; 3216 snprintf(req->error_str, 3217 sizeof(req->error_str), 3218 "Cannot pack result nvlist."); 3219 break; 3220 } 3221 3222 if (packed_len > req->result_len) { 3223 req->status = CTL_LUN_ERROR; 3224 snprintf(req->error_str, 3225 sizeof(req->error_str), 3226 "Result nvlist too large."); 3227 free(packed, M_NVLIST); 3228 break; 3229 } 3230 3231 if (copyout(packed, req->result, packed_len)) { 3232 req->status = CTL_LUN_ERROR; 3233 snprintf(req->error_str, 3234 sizeof(req->error_str), 3235 "Cannot copyout() the result."); 3236 free(packed, M_NVLIST); 3237 break; 3238 } 3239 3240 req->result_len = packed_len; 3241 free(packed, M_NVLIST); 3242 } 3243 3244 nvlist_destroy(req->result_nvl); 3245 } 3246 break; 3247 } 3248 case CTL_PORT_LIST: { 3249 struct sbuf *sb; 3250 struct ctl_port *port; 3251 struct ctl_lun_list *list; 3252 const char *name, *value; 3253 void *cookie; 3254 int j, type; 3255 uint32_t plun; 3256 3257 list = (struct ctl_lun_list *)addr; 3258 3259 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3260 if (sb == NULL) { 3261 list->status = CTL_LUN_LIST_ERROR; 3262 snprintf(list->error_str, sizeof(list->error_str), 3263 "Unable to allocate %d bytes for LUN list", 3264 list->alloc_len); 3265 break; 3266 } 3267 3268 sbuf_printf(sb, "<ctlportlist>\n"); 3269 3270 mtx_lock(&softc->ctl_lock); 3271 STAILQ_FOREACH(port, &softc->port_list, links) { 3272 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3273 (uintmax_t)port->targ_port); 3274 3275 /* 3276 * Bail out as soon as we see that we've overfilled 3277 * the buffer. 3278 */ 3279 if (retval != 0) 3280 break; 3281 3282 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3283 "</frontend_type>\n", port->frontend->name); 3284 if (retval != 0) 3285 break; 3286 3287 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3288 port->port_type); 3289 if (retval != 0) 3290 break; 3291 3292 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3293 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3294 if (retval != 0) 3295 break; 3296 3297 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3298 port->port_name); 3299 if (retval != 0) 3300 break; 3301 3302 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3303 port->physical_port); 3304 if (retval != 0) 3305 break; 3306 3307 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3308 port->virtual_port); 3309 if (retval != 0) 3310 break; 3311 3312 if (port->target_devid != NULL) { 3313 sbuf_printf(sb, "\t<target>"); 3314 ctl_id_sbuf(port->target_devid, sb); 3315 sbuf_printf(sb, "</target>\n"); 3316 } 3317 3318 if (port->port_devid != NULL) { 3319 sbuf_printf(sb, "\t<port>"); 3320 ctl_id_sbuf(port->port_devid, sb); 3321 sbuf_printf(sb, "</port>\n"); 3322 } 3323 3324 if (port->port_info != NULL) { 3325 retval = port->port_info(port->onoff_arg, sb); 3326 if (retval != 0) 3327 break; 3328 } 3329 3330 cookie = NULL; 3331 while ((name = nvlist_next(port->options, &type, 3332 &cookie)) != NULL) { 3333 sbuf_printf(sb, "\t<%s>", name); 3334 3335 if (type == NV_TYPE_STRING) { 3336 value = dnvlist_get_string(port->options, 3337 name, NULL); 3338 if (value != NULL) 3339 sbuf_printf(sb, "%s", value); 3340 } 3341 3342 sbuf_printf(sb, "</%s>\n", name); 3343 } 3344 3345 if (port->lun_map != NULL) { 3346 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3347 for (j = 0; j < port->lun_map_size; j++) { 3348 plun = ctl_lun_map_from_port(port, j); 3349 if (plun == UINT32_MAX) 3350 continue; 3351 sbuf_printf(sb, 3352 "\t<lun id=\"%u\">%u</lun>\n", 3353 j, plun); 3354 } 3355 } 3356 3357 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3358 if (port->wwpn_iid[j].in_use == 0 || 3359 (port->wwpn_iid[j].wwpn == 0 && 3360 port->wwpn_iid[j].name == NULL)) 3361 continue; 3362 3363 if (port->wwpn_iid[j].name != NULL) 3364 retval = sbuf_printf(sb, 3365 "\t<initiator id=\"%u\">%s</initiator>\n", 3366 j, port->wwpn_iid[j].name); 3367 else 3368 retval = sbuf_printf(sb, 3369 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3370 j, port->wwpn_iid[j].wwpn); 3371 if (retval != 0) 3372 break; 3373 } 3374 if (retval != 0) 3375 break; 3376 3377 retval = sbuf_printf(sb, "</targ_port>\n"); 3378 if (retval != 0) 3379 break; 3380 } 3381 mtx_unlock(&softc->ctl_lock); 3382 3383 if ((retval != 0) 3384 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3385 retval = 0; 3386 sbuf_delete(sb); 3387 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3388 snprintf(list->error_str, sizeof(list->error_str), 3389 "Out of space, %d bytes is too small", 3390 list->alloc_len); 3391 break; 3392 } 3393 3394 sbuf_finish(sb); 3395 3396 retval = copyout(sbuf_data(sb), list->lun_xml, 3397 sbuf_len(sb) + 1); 3398 3399 list->fill_len = sbuf_len(sb) + 1; 3400 list->status = CTL_LUN_LIST_OK; 3401 sbuf_delete(sb); 3402 break; 3403 } 3404 case CTL_LUN_MAP: { 3405 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3406 struct ctl_port *port; 3407 3408 mtx_lock(&softc->ctl_lock); 3409 if (lm->port < softc->port_min || 3410 lm->port >= softc->port_max || 3411 (port = softc->ctl_ports[lm->port]) == NULL) { 3412 mtx_unlock(&softc->ctl_lock); 3413 return (ENXIO); 3414 } 3415 if (port->status & CTL_PORT_STATUS_ONLINE) { 3416 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3417 if (ctl_lun_map_to_port(port, lun->lun) == 3418 UINT32_MAX) 3419 continue; 3420 mtx_lock(&lun->lun_lock); 3421 ctl_est_ua_port(lun, lm->port, -1, 3422 CTL_UA_LUN_CHANGE); 3423 mtx_unlock(&lun->lun_lock); 3424 } 3425 } 3426 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3427 if (lm->plun != UINT32_MAX) { 3428 if (lm->lun == UINT32_MAX) 3429 retval = ctl_lun_map_unset(port, lm->plun); 3430 else if (lm->lun < ctl_max_luns && 3431 softc->ctl_luns[lm->lun] != NULL) 3432 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3433 else 3434 return (ENXIO); 3435 } else { 3436 if (lm->lun == UINT32_MAX) 3437 retval = ctl_lun_map_deinit(port); 3438 else 3439 retval = ctl_lun_map_init(port); 3440 } 3441 if (port->status & CTL_PORT_STATUS_ONLINE) 3442 ctl_isc_announce_port(port); 3443 break; 3444 } 3445 case CTL_GET_LUN_STATS: { 3446 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3447 int i; 3448 3449 /* 3450 * XXX KDM no locking here. If the LUN list changes, 3451 * things can blow up. 3452 */ 3453 i = 0; 3454 stats->status = CTL_SS_OK; 3455 stats->fill_len = 0; 3456 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3457 if (lun->lun < stats->first_item) 3458 continue; 3459 if (stats->fill_len + sizeof(lun->stats) > 3460 stats->alloc_len) { 3461 stats->status = CTL_SS_NEED_MORE_SPACE; 3462 break; 3463 } 3464 retval = copyout(&lun->stats, &stats->stats[i++], 3465 sizeof(lun->stats)); 3466 if (retval != 0) 3467 break; 3468 stats->fill_len += sizeof(lun->stats); 3469 } 3470 stats->num_items = softc->num_luns; 3471 stats->flags = CTL_STATS_FLAG_NONE; 3472 #ifdef CTL_TIME_IO 3473 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3474 #endif 3475 getnanouptime(&stats->timestamp); 3476 break; 3477 } 3478 case CTL_GET_PORT_STATS: { 3479 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3480 int i; 3481 3482 /* 3483 * XXX KDM no locking here. If the LUN list changes, 3484 * things can blow up. 3485 */ 3486 i = 0; 3487 stats->status = CTL_SS_OK; 3488 stats->fill_len = 0; 3489 STAILQ_FOREACH(port, &softc->port_list, links) { 3490 if (port->targ_port < stats->first_item) 3491 continue; 3492 if (stats->fill_len + sizeof(port->stats) > 3493 stats->alloc_len) { 3494 stats->status = CTL_SS_NEED_MORE_SPACE; 3495 break; 3496 } 3497 retval = copyout(&port->stats, &stats->stats[i++], 3498 sizeof(port->stats)); 3499 if (retval != 0) 3500 break; 3501 stats->fill_len += sizeof(port->stats); 3502 } 3503 stats->num_items = softc->num_ports; 3504 stats->flags = CTL_STATS_FLAG_NONE; 3505 #ifdef CTL_TIME_IO 3506 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3507 #endif 3508 getnanouptime(&stats->timestamp); 3509 break; 3510 } 3511 default: { 3512 /* XXX KDM should we fix this? */ 3513 #if 0 3514 struct ctl_backend_driver *backend; 3515 unsigned int type; 3516 int found; 3517 3518 found = 0; 3519 3520 /* 3521 * We encode the backend type as the ioctl type for backend 3522 * ioctls. So parse it out here, and then search for a 3523 * backend of this type. 3524 */ 3525 type = _IOC_TYPE(cmd); 3526 3527 STAILQ_FOREACH(backend, &softc->be_list, links) { 3528 if (backend->type == type) { 3529 found = 1; 3530 break; 3531 } 3532 } 3533 if (found == 0) { 3534 printf("ctl: unknown ioctl command %#lx or backend " 3535 "%d\n", cmd, type); 3536 retval = EINVAL; 3537 break; 3538 } 3539 retval = backend->ioctl(dev, cmd, addr, flag, td); 3540 #endif 3541 retval = ENOTTY; 3542 break; 3543 } 3544 } 3545 return (retval); 3546 } 3547 3548 uint32_t 3549 ctl_get_initindex(struct ctl_nexus *nexus) 3550 { 3551 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3552 } 3553 3554 int 3555 ctl_lun_map_init(struct ctl_port *port) 3556 { 3557 struct ctl_softc *softc = port->ctl_softc; 3558 struct ctl_lun *lun; 3559 int size = ctl_lun_map_size; 3560 uint32_t i; 3561 3562 if (port->lun_map == NULL || port->lun_map_size < size) { 3563 port->lun_map_size = 0; 3564 free(port->lun_map, M_CTL); 3565 port->lun_map = malloc(size * sizeof(uint32_t), 3566 M_CTL, M_NOWAIT); 3567 } 3568 if (port->lun_map == NULL) 3569 return (ENOMEM); 3570 for (i = 0; i < size; i++) 3571 port->lun_map[i] = UINT32_MAX; 3572 port->lun_map_size = size; 3573 if (port->status & CTL_PORT_STATUS_ONLINE) { 3574 if (port->lun_disable != NULL) { 3575 STAILQ_FOREACH(lun, &softc->lun_list, links) 3576 port->lun_disable(port->targ_lun_arg, lun->lun); 3577 } 3578 ctl_isc_announce_port(port); 3579 } 3580 return (0); 3581 } 3582 3583 int 3584 ctl_lun_map_deinit(struct ctl_port *port) 3585 { 3586 struct ctl_softc *softc = port->ctl_softc; 3587 struct ctl_lun *lun; 3588 3589 if (port->lun_map == NULL) 3590 return (0); 3591 port->lun_map_size = 0; 3592 free(port->lun_map, M_CTL); 3593 port->lun_map = NULL; 3594 if (port->status & CTL_PORT_STATUS_ONLINE) { 3595 if (port->lun_enable != NULL) { 3596 STAILQ_FOREACH(lun, &softc->lun_list, links) 3597 port->lun_enable(port->targ_lun_arg, lun->lun); 3598 } 3599 ctl_isc_announce_port(port); 3600 } 3601 return (0); 3602 } 3603 3604 int 3605 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3606 { 3607 int status; 3608 uint32_t old; 3609 3610 if (port->lun_map == NULL) { 3611 status = ctl_lun_map_init(port); 3612 if (status != 0) 3613 return (status); 3614 } 3615 if (plun >= port->lun_map_size) 3616 return (EINVAL); 3617 old = port->lun_map[plun]; 3618 port->lun_map[plun] = glun; 3619 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3620 if (port->lun_enable != NULL) 3621 port->lun_enable(port->targ_lun_arg, plun); 3622 ctl_isc_announce_port(port); 3623 } 3624 return (0); 3625 } 3626 3627 int 3628 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3629 { 3630 uint32_t old; 3631 3632 if (port->lun_map == NULL || plun >= port->lun_map_size) 3633 return (0); 3634 old = port->lun_map[plun]; 3635 port->lun_map[plun] = UINT32_MAX; 3636 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3637 if (port->lun_disable != NULL) 3638 port->lun_disable(port->targ_lun_arg, plun); 3639 ctl_isc_announce_port(port); 3640 } 3641 return (0); 3642 } 3643 3644 uint32_t 3645 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3646 { 3647 3648 if (port == NULL) 3649 return (UINT32_MAX); 3650 if (port->lun_map == NULL) 3651 return (lun_id); 3652 if (lun_id > port->lun_map_size) 3653 return (UINT32_MAX); 3654 return (port->lun_map[lun_id]); 3655 } 3656 3657 uint32_t 3658 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3659 { 3660 uint32_t i; 3661 3662 if (port == NULL) 3663 return (UINT32_MAX); 3664 if (port->lun_map == NULL) 3665 return (lun_id); 3666 for (i = 0; i < port->lun_map_size; i++) { 3667 if (port->lun_map[i] == lun_id) 3668 return (i); 3669 } 3670 return (UINT32_MAX); 3671 } 3672 3673 uint32_t 3674 ctl_decode_lun(uint64_t encoded) 3675 { 3676 uint8_t lun[8]; 3677 uint32_t result = 0xffffffff; 3678 3679 be64enc(lun, encoded); 3680 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3681 case RPL_LUNDATA_ATYP_PERIPH: 3682 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3683 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3684 result = lun[1]; 3685 break; 3686 case RPL_LUNDATA_ATYP_FLAT: 3687 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3688 lun[6] == 0 && lun[7] == 0) 3689 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3690 break; 3691 case RPL_LUNDATA_ATYP_EXTLUN: 3692 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3693 case 0x02: 3694 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3695 case 0x00: 3696 result = lun[1]; 3697 break; 3698 case 0x10: 3699 result = (lun[1] << 16) + (lun[2] << 8) + 3700 lun[3]; 3701 break; 3702 case 0x20: 3703 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3704 result = (lun[2] << 24) + 3705 (lun[3] << 16) + (lun[4] << 8) + 3706 lun[5]; 3707 break; 3708 } 3709 break; 3710 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3711 result = 0xffffffff; 3712 break; 3713 } 3714 break; 3715 } 3716 return (result); 3717 } 3718 3719 uint64_t 3720 ctl_encode_lun(uint32_t decoded) 3721 { 3722 uint64_t l = decoded; 3723 3724 if (l <= 0xff) 3725 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3726 if (l <= 0x3fff) 3727 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3728 if (l <= 0xffffff) 3729 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3730 (l << 32)); 3731 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3732 } 3733 3734 int 3735 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3736 { 3737 int i; 3738 3739 for (i = first; i < last; i++) { 3740 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3741 return (i); 3742 } 3743 return (-1); 3744 } 3745 3746 int 3747 ctl_set_mask(uint32_t *mask, uint32_t bit) 3748 { 3749 uint32_t chunk, piece; 3750 3751 chunk = bit >> 5; 3752 piece = bit % (sizeof(uint32_t) * 8); 3753 3754 if ((mask[chunk] & (1 << piece)) != 0) 3755 return (-1); 3756 else 3757 mask[chunk] |= (1 << piece); 3758 3759 return (0); 3760 } 3761 3762 int 3763 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3764 { 3765 uint32_t chunk, piece; 3766 3767 chunk = bit >> 5; 3768 piece = bit % (sizeof(uint32_t) * 8); 3769 3770 if ((mask[chunk] & (1 << piece)) == 0) 3771 return (-1); 3772 else 3773 mask[chunk] &= ~(1 << piece); 3774 3775 return (0); 3776 } 3777 3778 int 3779 ctl_is_set(uint32_t *mask, uint32_t bit) 3780 { 3781 uint32_t chunk, piece; 3782 3783 chunk = bit >> 5; 3784 piece = bit % (sizeof(uint32_t) * 8); 3785 3786 if ((mask[chunk] & (1 << piece)) == 0) 3787 return (0); 3788 else 3789 return (1); 3790 } 3791 3792 static uint64_t 3793 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3794 { 3795 uint64_t *t; 3796 3797 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3798 if (t == NULL) 3799 return (0); 3800 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3801 } 3802 3803 static void 3804 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3805 { 3806 uint64_t *t; 3807 3808 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3809 if (t == NULL) 3810 return; 3811 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3812 } 3813 3814 static void 3815 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3816 { 3817 uint64_t *p; 3818 u_int i; 3819 3820 i = residx/CTL_MAX_INIT_PER_PORT; 3821 if (lun->pr_keys[i] != NULL) 3822 return; 3823 mtx_unlock(&lun->lun_lock); 3824 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3825 M_WAITOK | M_ZERO); 3826 mtx_lock(&lun->lun_lock); 3827 if (lun->pr_keys[i] == NULL) 3828 lun->pr_keys[i] = p; 3829 else 3830 free(p, M_CTL); 3831 } 3832 3833 static void 3834 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3835 { 3836 uint64_t *t; 3837 3838 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3839 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3840 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3841 } 3842 3843 /* 3844 * ctl_softc, pool_name, total_ctl_io are passed in. 3845 * npool is passed out. 3846 */ 3847 int 3848 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3849 uint32_t total_ctl_io, void **npool) 3850 { 3851 struct ctl_io_pool *pool; 3852 3853 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3854 M_NOWAIT | M_ZERO); 3855 if (pool == NULL) 3856 return (ENOMEM); 3857 3858 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3859 pool->ctl_softc = ctl_softc; 3860 #ifdef IO_POOLS 3861 pool->zone = uma_zsecond_create(pool->name, NULL, 3862 NULL, NULL, NULL, ctl_softc->io_zone); 3863 /* uma_prealloc(pool->zone, total_ctl_io); */ 3864 #else 3865 pool->zone = ctl_softc->io_zone; 3866 #endif 3867 3868 *npool = pool; 3869 return (0); 3870 } 3871 3872 void 3873 ctl_pool_free(struct ctl_io_pool *pool) 3874 { 3875 3876 if (pool == NULL) 3877 return; 3878 3879 #ifdef IO_POOLS 3880 uma_zdestroy(pool->zone); 3881 #endif 3882 free(pool, M_CTL); 3883 } 3884 3885 union ctl_io * 3886 ctl_alloc_io(void *pool_ref) 3887 { 3888 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3889 union ctl_io *io; 3890 3891 io = uma_zalloc(pool->zone, M_WAITOK); 3892 if (io != NULL) { 3893 io->io_hdr.pool = pool_ref; 3894 CTL_SOFTC(io) = pool->ctl_softc; 3895 TAILQ_INIT(&io->io_hdr.blocked_queue); 3896 } 3897 return (io); 3898 } 3899 3900 union ctl_io * 3901 ctl_alloc_io_nowait(void *pool_ref) 3902 { 3903 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3904 union ctl_io *io; 3905 3906 io = uma_zalloc(pool->zone, M_NOWAIT); 3907 if (io != NULL) { 3908 io->io_hdr.pool = pool_ref; 3909 CTL_SOFTC(io) = pool->ctl_softc; 3910 TAILQ_INIT(&io->io_hdr.blocked_queue); 3911 } 3912 return (io); 3913 } 3914 3915 void 3916 ctl_free_io(union ctl_io *io) 3917 { 3918 struct ctl_io_pool *pool; 3919 3920 if (io == NULL) 3921 return; 3922 3923 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3924 uma_zfree(pool->zone, io); 3925 } 3926 3927 void 3928 ctl_zero_io(union ctl_io *io) 3929 { 3930 struct ctl_io_pool *pool; 3931 3932 if (io == NULL) 3933 return; 3934 3935 /* 3936 * May need to preserve linked list pointers at some point too. 3937 */ 3938 pool = io->io_hdr.pool; 3939 memset(io, 0, sizeof(*io)); 3940 io->io_hdr.pool = pool; 3941 CTL_SOFTC(io) = pool->ctl_softc; 3942 TAILQ_INIT(&io->io_hdr.blocked_queue); 3943 } 3944 3945 int 3946 ctl_expand_number(const char *buf, uint64_t *num) 3947 { 3948 char *endptr; 3949 uint64_t number; 3950 unsigned shift; 3951 3952 number = strtoq(buf, &endptr, 0); 3953 3954 switch (tolower((unsigned char)*endptr)) { 3955 case 'e': 3956 shift = 60; 3957 break; 3958 case 'p': 3959 shift = 50; 3960 break; 3961 case 't': 3962 shift = 40; 3963 break; 3964 case 'g': 3965 shift = 30; 3966 break; 3967 case 'm': 3968 shift = 20; 3969 break; 3970 case 'k': 3971 shift = 10; 3972 break; 3973 case 'b': 3974 case '\0': /* No unit. */ 3975 *num = number; 3976 return (0); 3977 default: 3978 /* Unrecognized unit. */ 3979 return (-1); 3980 } 3981 3982 if ((number << shift) >> shift != number) { 3983 /* Overflow */ 3984 return (-1); 3985 } 3986 *num = number << shift; 3987 return (0); 3988 } 3989 3990 /* 3991 * This routine could be used in the future to load default and/or saved 3992 * mode page parameters for a particuar lun. 3993 */ 3994 static int 3995 ctl_init_page_index(struct ctl_lun *lun) 3996 { 3997 int i, page_code; 3998 struct ctl_page_index *page_index; 3999 const char *value; 4000 uint64_t ival; 4001 4002 memcpy(&lun->mode_pages.index, page_index_template, 4003 sizeof(page_index_template)); 4004 4005 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4006 page_index = &lun->mode_pages.index[i]; 4007 if (lun->be_lun->lun_type == T_DIRECT && 4008 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4009 continue; 4010 if (lun->be_lun->lun_type == T_PROCESSOR && 4011 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4012 continue; 4013 if (lun->be_lun->lun_type == T_CDROM && 4014 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4015 continue; 4016 4017 page_code = page_index->page_code & SMPH_PC_MASK; 4018 switch (page_code) { 4019 case SMS_RW_ERROR_RECOVERY_PAGE: { 4020 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4021 ("subpage %#x for page %#x is incorrect!", 4022 page_index->subpage, page_code)); 4023 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4024 &rw_er_page_default, 4025 sizeof(rw_er_page_default)); 4026 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4027 &rw_er_page_changeable, 4028 sizeof(rw_er_page_changeable)); 4029 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4030 &rw_er_page_default, 4031 sizeof(rw_er_page_default)); 4032 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4033 &rw_er_page_default, 4034 sizeof(rw_er_page_default)); 4035 page_index->page_data = 4036 (uint8_t *)lun->mode_pages.rw_er_page; 4037 break; 4038 } 4039 case SMS_FORMAT_DEVICE_PAGE: { 4040 struct scsi_format_page *format_page; 4041 4042 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4043 ("subpage %#x for page %#x is incorrect!", 4044 page_index->subpage, page_code)); 4045 4046 /* 4047 * Sectors per track are set above. Bytes per 4048 * sector need to be set here on a per-LUN basis. 4049 */ 4050 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4051 &format_page_default, 4052 sizeof(format_page_default)); 4053 memcpy(&lun->mode_pages.format_page[ 4054 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4055 sizeof(format_page_changeable)); 4056 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4057 &format_page_default, 4058 sizeof(format_page_default)); 4059 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4060 &format_page_default, 4061 sizeof(format_page_default)); 4062 4063 format_page = &lun->mode_pages.format_page[ 4064 CTL_PAGE_CURRENT]; 4065 scsi_ulto2b(lun->be_lun->blocksize, 4066 format_page->bytes_per_sector); 4067 4068 format_page = &lun->mode_pages.format_page[ 4069 CTL_PAGE_DEFAULT]; 4070 scsi_ulto2b(lun->be_lun->blocksize, 4071 format_page->bytes_per_sector); 4072 4073 format_page = &lun->mode_pages.format_page[ 4074 CTL_PAGE_SAVED]; 4075 scsi_ulto2b(lun->be_lun->blocksize, 4076 format_page->bytes_per_sector); 4077 4078 page_index->page_data = 4079 (uint8_t *)lun->mode_pages.format_page; 4080 break; 4081 } 4082 case SMS_RIGID_DISK_PAGE: { 4083 struct scsi_rigid_disk_page *rigid_disk_page; 4084 uint32_t sectors_per_cylinder; 4085 uint64_t cylinders; 4086 #ifndef __XSCALE__ 4087 int shift; 4088 #endif /* !__XSCALE__ */ 4089 4090 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4091 ("subpage %#x for page %#x is incorrect!", 4092 page_index->subpage, page_code)); 4093 4094 /* 4095 * Rotation rate and sectors per track are set 4096 * above. We calculate the cylinders here based on 4097 * capacity. Due to the number of heads and 4098 * sectors per track we're using, smaller arrays 4099 * may turn out to have 0 cylinders. Linux and 4100 * FreeBSD don't pay attention to these mode pages 4101 * to figure out capacity, but Solaris does. It 4102 * seems to deal with 0 cylinders just fine, and 4103 * works out a fake geometry based on the capacity. 4104 */ 4105 memcpy(&lun->mode_pages.rigid_disk_page[ 4106 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4107 sizeof(rigid_disk_page_default)); 4108 memcpy(&lun->mode_pages.rigid_disk_page[ 4109 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4110 sizeof(rigid_disk_page_changeable)); 4111 4112 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4113 CTL_DEFAULT_HEADS; 4114 4115 /* 4116 * The divide method here will be more accurate, 4117 * probably, but results in floating point being 4118 * used in the kernel on i386 (__udivdi3()). On the 4119 * XScale, though, __udivdi3() is implemented in 4120 * software. 4121 * 4122 * The shift method for cylinder calculation is 4123 * accurate if sectors_per_cylinder is a power of 4124 * 2. Otherwise it might be slightly off -- you 4125 * might have a bit of a truncation problem. 4126 */ 4127 #ifdef __XSCALE__ 4128 cylinders = (lun->be_lun->maxlba + 1) / 4129 sectors_per_cylinder; 4130 #else 4131 for (shift = 31; shift > 0; shift--) { 4132 if (sectors_per_cylinder & (1 << shift)) 4133 break; 4134 } 4135 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4136 #endif 4137 4138 /* 4139 * We've basically got 3 bytes, or 24 bits for the 4140 * cylinder size in the mode page. If we're over, 4141 * just round down to 2^24. 4142 */ 4143 if (cylinders > 0xffffff) 4144 cylinders = 0xffffff; 4145 4146 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4147 CTL_PAGE_DEFAULT]; 4148 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4149 4150 if ((value = dnvlist_get_string(lun->be_lun->options, 4151 "rpm", NULL)) != NULL) { 4152 scsi_ulto2b(strtol(value, NULL, 0), 4153 rigid_disk_page->rotation_rate); 4154 } 4155 4156 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4157 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4158 sizeof(rigid_disk_page_default)); 4159 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4160 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4161 sizeof(rigid_disk_page_default)); 4162 4163 page_index->page_data = 4164 (uint8_t *)lun->mode_pages.rigid_disk_page; 4165 break; 4166 } 4167 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4168 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4169 ("subpage %#x for page %#x is incorrect!", 4170 page_index->subpage, page_code)); 4171 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4172 &verify_er_page_default, 4173 sizeof(verify_er_page_default)); 4174 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4175 &verify_er_page_changeable, 4176 sizeof(verify_er_page_changeable)); 4177 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4178 &verify_er_page_default, 4179 sizeof(verify_er_page_default)); 4180 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4181 &verify_er_page_default, 4182 sizeof(verify_er_page_default)); 4183 page_index->page_data = 4184 (uint8_t *)lun->mode_pages.verify_er_page; 4185 break; 4186 } 4187 case SMS_CACHING_PAGE: { 4188 struct scsi_caching_page *caching_page; 4189 4190 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4191 ("subpage %#x for page %#x is incorrect!", 4192 page_index->subpage, page_code)); 4193 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4194 &caching_page_default, 4195 sizeof(caching_page_default)); 4196 memcpy(&lun->mode_pages.caching_page[ 4197 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4198 sizeof(caching_page_changeable)); 4199 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4200 &caching_page_default, 4201 sizeof(caching_page_default)); 4202 caching_page = &lun->mode_pages.caching_page[ 4203 CTL_PAGE_SAVED]; 4204 value = dnvlist_get_string(lun->be_lun->options, 4205 "writecache", NULL); 4206 if (value != NULL && strcmp(value, "off") == 0) 4207 caching_page->flags1 &= ~SCP_WCE; 4208 value = dnvlist_get_string(lun->be_lun->options, 4209 "readcache", NULL); 4210 if (value != NULL && strcmp(value, "off") == 0) 4211 caching_page->flags1 |= SCP_RCD; 4212 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4213 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4214 sizeof(caching_page_default)); 4215 page_index->page_data = 4216 (uint8_t *)lun->mode_pages.caching_page; 4217 break; 4218 } 4219 case SMS_CONTROL_MODE_PAGE: { 4220 switch (page_index->subpage) { 4221 case SMS_SUBPAGE_PAGE_0: { 4222 struct scsi_control_page *control_page; 4223 4224 memcpy(&lun->mode_pages.control_page[ 4225 CTL_PAGE_DEFAULT], 4226 &control_page_default, 4227 sizeof(control_page_default)); 4228 memcpy(&lun->mode_pages.control_page[ 4229 CTL_PAGE_CHANGEABLE], 4230 &control_page_changeable, 4231 sizeof(control_page_changeable)); 4232 memcpy(&lun->mode_pages.control_page[ 4233 CTL_PAGE_SAVED], 4234 &control_page_default, 4235 sizeof(control_page_default)); 4236 control_page = &lun->mode_pages.control_page[ 4237 CTL_PAGE_SAVED]; 4238 value = dnvlist_get_string(lun->be_lun->options, 4239 "reordering", NULL); 4240 if (value != NULL && 4241 strcmp(value, "unrestricted") == 0) { 4242 control_page->queue_flags &= 4243 ~SCP_QUEUE_ALG_MASK; 4244 control_page->queue_flags |= 4245 SCP_QUEUE_ALG_UNRESTRICTED; 4246 } 4247 memcpy(&lun->mode_pages.control_page[ 4248 CTL_PAGE_CURRENT], 4249 &lun->mode_pages.control_page[ 4250 CTL_PAGE_SAVED], 4251 sizeof(control_page_default)); 4252 page_index->page_data = 4253 (uint8_t *)lun->mode_pages.control_page; 4254 break; 4255 } 4256 case 0x01: 4257 memcpy(&lun->mode_pages.control_ext_page[ 4258 CTL_PAGE_DEFAULT], 4259 &control_ext_page_default, 4260 sizeof(control_ext_page_default)); 4261 memcpy(&lun->mode_pages.control_ext_page[ 4262 CTL_PAGE_CHANGEABLE], 4263 &control_ext_page_changeable, 4264 sizeof(control_ext_page_changeable)); 4265 memcpy(&lun->mode_pages.control_ext_page[ 4266 CTL_PAGE_SAVED], 4267 &control_ext_page_default, 4268 sizeof(control_ext_page_default)); 4269 memcpy(&lun->mode_pages.control_ext_page[ 4270 CTL_PAGE_CURRENT], 4271 &lun->mode_pages.control_ext_page[ 4272 CTL_PAGE_SAVED], 4273 sizeof(control_ext_page_default)); 4274 page_index->page_data = 4275 (uint8_t *)lun->mode_pages.control_ext_page; 4276 break; 4277 default: 4278 panic("subpage %#x for page %#x is incorrect!", 4279 page_index->subpage, page_code); 4280 } 4281 break; 4282 } 4283 case SMS_INFO_EXCEPTIONS_PAGE: { 4284 switch (page_index->subpage) { 4285 case SMS_SUBPAGE_PAGE_0: 4286 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4287 &ie_page_default, 4288 sizeof(ie_page_default)); 4289 memcpy(&lun->mode_pages.ie_page[ 4290 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4291 sizeof(ie_page_changeable)); 4292 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4293 &ie_page_default, 4294 sizeof(ie_page_default)); 4295 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4296 &ie_page_default, 4297 sizeof(ie_page_default)); 4298 page_index->page_data = 4299 (uint8_t *)lun->mode_pages.ie_page; 4300 break; 4301 case 0x02: { 4302 struct ctl_logical_block_provisioning_page *page; 4303 4304 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4305 &lbp_page_default, 4306 sizeof(lbp_page_default)); 4307 memcpy(&lun->mode_pages.lbp_page[ 4308 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4309 sizeof(lbp_page_changeable)); 4310 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4311 &lbp_page_default, 4312 sizeof(lbp_page_default)); 4313 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4314 value = dnvlist_get_string(lun->be_lun->options, 4315 "avail-threshold", NULL); 4316 if (value != NULL && 4317 ctl_expand_number(value, &ival) == 0) { 4318 page->descr[0].flags |= SLBPPD_ENABLED | 4319 SLBPPD_ARMING_DEC; 4320 if (lun->be_lun->blocksize) 4321 ival /= lun->be_lun->blocksize; 4322 else 4323 ival /= 512; 4324 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4325 page->descr[0].count); 4326 } 4327 value = dnvlist_get_string(lun->be_lun->options, 4328 "used-threshold", NULL); 4329 if (value != NULL && 4330 ctl_expand_number(value, &ival) == 0) { 4331 page->descr[1].flags |= SLBPPD_ENABLED | 4332 SLBPPD_ARMING_INC; 4333 if (lun->be_lun->blocksize) 4334 ival /= lun->be_lun->blocksize; 4335 else 4336 ival /= 512; 4337 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4338 page->descr[1].count); 4339 } 4340 value = dnvlist_get_string(lun->be_lun->options, 4341 "pool-avail-threshold", NULL); 4342 if (value != NULL && 4343 ctl_expand_number(value, &ival) == 0) { 4344 page->descr[2].flags |= SLBPPD_ENABLED | 4345 SLBPPD_ARMING_DEC; 4346 if (lun->be_lun->blocksize) 4347 ival /= lun->be_lun->blocksize; 4348 else 4349 ival /= 512; 4350 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4351 page->descr[2].count); 4352 } 4353 value = dnvlist_get_string(lun->be_lun->options, 4354 "pool-used-threshold", NULL); 4355 if (value != NULL && 4356 ctl_expand_number(value, &ival) == 0) { 4357 page->descr[3].flags |= SLBPPD_ENABLED | 4358 SLBPPD_ARMING_INC; 4359 if (lun->be_lun->blocksize) 4360 ival /= lun->be_lun->blocksize; 4361 else 4362 ival /= 512; 4363 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4364 page->descr[3].count); 4365 } 4366 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4367 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4368 sizeof(lbp_page_default)); 4369 page_index->page_data = 4370 (uint8_t *)lun->mode_pages.lbp_page; 4371 break; 4372 } 4373 default: 4374 panic("subpage %#x for page %#x is incorrect!", 4375 page_index->subpage, page_code); 4376 } 4377 break; 4378 } 4379 case SMS_CDDVD_CAPS_PAGE:{ 4380 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4381 ("subpage %#x for page %#x is incorrect!", 4382 page_index->subpage, page_code)); 4383 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4384 &cddvd_page_default, 4385 sizeof(cddvd_page_default)); 4386 memcpy(&lun->mode_pages.cddvd_page[ 4387 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4388 sizeof(cddvd_page_changeable)); 4389 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4390 &cddvd_page_default, 4391 sizeof(cddvd_page_default)); 4392 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4393 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4394 sizeof(cddvd_page_default)); 4395 page_index->page_data = 4396 (uint8_t *)lun->mode_pages.cddvd_page; 4397 break; 4398 } 4399 default: 4400 panic("invalid page code value %#x", page_code); 4401 } 4402 } 4403 4404 return (CTL_RETVAL_COMPLETE); 4405 } 4406 4407 static int 4408 ctl_init_log_page_index(struct ctl_lun *lun) 4409 { 4410 struct ctl_page_index *page_index; 4411 int i, j, k, prev; 4412 4413 memcpy(&lun->log_pages.index, log_page_index_template, 4414 sizeof(log_page_index_template)); 4415 4416 prev = -1; 4417 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4418 page_index = &lun->log_pages.index[i]; 4419 if (lun->be_lun->lun_type == T_DIRECT && 4420 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4421 continue; 4422 if (lun->be_lun->lun_type == T_PROCESSOR && 4423 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4424 continue; 4425 if (lun->be_lun->lun_type == T_CDROM && 4426 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4427 continue; 4428 4429 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4430 lun->backend->lun_attr == NULL) 4431 continue; 4432 4433 if (page_index->page_code != prev) { 4434 lun->log_pages.pages_page[j] = page_index->page_code; 4435 prev = page_index->page_code; 4436 j++; 4437 } 4438 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4439 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4440 k++; 4441 } 4442 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4443 lun->log_pages.index[0].page_len = j; 4444 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4445 lun->log_pages.index[1].page_len = k * 2; 4446 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page; 4447 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page); 4448 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0]; 4449 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS; 4450 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page; 4451 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page); 4452 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page; 4453 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page); 4454 4455 return (CTL_RETVAL_COMPLETE); 4456 } 4457 4458 static int 4459 hex2bin(const char *str, uint8_t *buf, int buf_size) 4460 { 4461 int i; 4462 u_char c; 4463 4464 memset(buf, 0, buf_size); 4465 while (isspace(str[0])) 4466 str++; 4467 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4468 str += 2; 4469 buf_size *= 2; 4470 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4471 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4472 str++; 4473 c = str[i]; 4474 if (isdigit(c)) 4475 c -= '0'; 4476 else if (isalpha(c)) 4477 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4478 else 4479 break; 4480 if (c >= 16) 4481 break; 4482 if ((i & 1) == 0) 4483 buf[i / 2] |= (c << 4); 4484 else 4485 buf[i / 2] |= c; 4486 } 4487 return ((i + 1) / 2); 4488 } 4489 4490 /* 4491 * Add LUN. 4492 * 4493 * Returns 0 for success, non-zero (errno) for failure. 4494 */ 4495 int 4496 ctl_add_lun(struct ctl_be_lun *be_lun) 4497 { 4498 struct ctl_softc *ctl_softc = control_softc; 4499 struct ctl_lun *nlun, *lun; 4500 struct scsi_vpd_id_descriptor *desc; 4501 struct scsi_vpd_id_t10 *t10id; 4502 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4503 int lun_number; 4504 int devidlen, idlen1, idlen2 = 0, len; 4505 4506 /* 4507 * We support only Direct Access, CD-ROM or Processor LUN types. 4508 */ 4509 switch (be_lun->lun_type) { 4510 case T_DIRECT: 4511 case T_PROCESSOR: 4512 case T_CDROM: 4513 break; 4514 case T_SEQUENTIAL: 4515 case T_CHANGER: 4516 default: 4517 return (EINVAL); 4518 } 4519 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO); 4520 4521 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4522 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4523 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4524 M_DEVBUF, M_WAITOK | M_ZERO); 4525 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4526 M_DEVBUF, M_WAITOK | M_ZERO); 4527 4528 /* Generate LUN ID. */ 4529 devidlen = max(CTL_DEVID_MIN_LEN, 4530 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4531 idlen1 = sizeof(*t10id) + devidlen; 4532 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4533 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4534 if (scsiname != NULL) { 4535 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4536 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4537 } 4538 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4539 if (eui != NULL) { 4540 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4541 } 4542 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4543 if (naa != NULL) { 4544 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4545 } 4546 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4547 if (uuid != NULL) { 4548 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4549 } 4550 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4551 M_CTL, M_WAITOK | M_ZERO); 4552 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4553 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4554 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4555 desc->length = idlen1; 4556 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4557 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4558 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4559 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4560 } else { 4561 strncpy(t10id->vendor, vendor, 4562 min(sizeof(t10id->vendor), strlen(vendor))); 4563 } 4564 strncpy((char *)t10id->vendor_spec_id, 4565 (char *)be_lun->device_id, devidlen); 4566 if (scsiname != NULL) { 4567 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4568 desc->length); 4569 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4570 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4571 SVPD_ID_TYPE_SCSI_NAME; 4572 desc->length = idlen2; 4573 strlcpy(desc->identifier, scsiname, idlen2); 4574 } 4575 if (eui != NULL) { 4576 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4577 desc->length); 4578 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4579 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4580 SVPD_ID_TYPE_EUI64; 4581 desc->length = hex2bin(eui, desc->identifier, 16); 4582 desc->length = desc->length > 12 ? 16 : 4583 (desc->length > 8 ? 12 : 8); 4584 len -= 16 - desc->length; 4585 } 4586 if (naa != NULL) { 4587 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4588 desc->length); 4589 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4590 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4591 SVPD_ID_TYPE_NAA; 4592 desc->length = hex2bin(naa, desc->identifier, 16); 4593 desc->length = desc->length > 8 ? 16 : 8; 4594 len -= 16 - desc->length; 4595 } 4596 if (uuid != NULL) { 4597 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4598 desc->length); 4599 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4600 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4601 SVPD_ID_TYPE_UUID; 4602 desc->identifier[0] = 0x10; 4603 hex2bin(uuid, &desc->identifier[2], 16); 4604 desc->length = 18; 4605 } 4606 lun->lun_devid->len = len; 4607 4608 mtx_lock(&ctl_softc->ctl_lock); 4609 /* 4610 * See if the caller requested a particular LUN number. If so, see 4611 * if it is available. Otherwise, allocate the first available LUN. 4612 */ 4613 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4614 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4615 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4616 mtx_unlock(&ctl_softc->ctl_lock); 4617 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4618 printf("ctl: requested LUN ID %d is higher " 4619 "than ctl_max_luns - 1 (%d)\n", 4620 be_lun->req_lun_id, ctl_max_luns - 1); 4621 } else { 4622 /* 4623 * XXX KDM return an error, or just assign 4624 * another LUN ID in this case?? 4625 */ 4626 printf("ctl: requested LUN ID %d is already " 4627 "in use\n", be_lun->req_lun_id); 4628 } 4629 fail: 4630 free(lun->lun_devid, M_CTL); 4631 free(lun, M_CTL); 4632 return (ENOSPC); 4633 } 4634 lun_number = be_lun->req_lun_id; 4635 } else { 4636 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4637 if (lun_number == -1) { 4638 mtx_unlock(&ctl_softc->ctl_lock); 4639 printf("ctl: can't allocate LUN, out of LUNs\n"); 4640 goto fail; 4641 } 4642 } 4643 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4644 mtx_unlock(&ctl_softc->ctl_lock); 4645 4646 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4647 lun->lun = lun_number; 4648 lun->be_lun = be_lun; 4649 /* 4650 * The processor LUN is always enabled. Disk LUNs come on line 4651 * disabled, and must be enabled by the backend. 4652 */ 4653 lun->flags |= CTL_LUN_DISABLED; 4654 lun->backend = be_lun->be; 4655 be_lun->ctl_lun = lun; 4656 be_lun->lun_id = lun_number; 4657 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4658 lun->flags |= CTL_LUN_EJECTED; 4659 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4660 lun->flags |= CTL_LUN_NO_MEDIA; 4661 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4662 lun->flags |= CTL_LUN_STOPPED; 4663 4664 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4665 lun->flags |= CTL_LUN_PRIMARY_SC; 4666 4667 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4668 if (value != NULL) { 4669 if (strcmp(value, "on") == 0) 4670 lun->flags |= CTL_LUN_REMOVABLE; 4671 } else if (be_lun->lun_type == T_CDROM) 4672 lun->flags |= CTL_LUN_REMOVABLE; 4673 4674 lun->ctl_softc = ctl_softc; 4675 #ifdef CTL_TIME_IO 4676 lun->last_busy = getsbinuptime(); 4677 #endif 4678 LIST_INIT(&lun->ooa_queue); 4679 STAILQ_INIT(&lun->error_list); 4680 lun->ie_reported = 1; 4681 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4682 ctl_tpc_lun_init(lun); 4683 if (lun->flags & CTL_LUN_REMOVABLE) { 4684 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4685 M_CTL, M_WAITOK); 4686 } 4687 4688 /* 4689 * Initialize the mode and log page index. 4690 */ 4691 ctl_init_page_index(lun); 4692 ctl_init_log_page_index(lun); 4693 4694 /* Setup statistics gathering */ 4695 lun->stats.item = lun_number; 4696 4697 /* 4698 * Now, before we insert this lun on the lun list, set the lun 4699 * inventory changed UA for all other luns. 4700 */ 4701 mtx_lock(&ctl_softc->ctl_lock); 4702 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4703 mtx_lock(&nlun->lun_lock); 4704 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4705 mtx_unlock(&nlun->lun_lock); 4706 } 4707 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4708 ctl_softc->ctl_luns[lun_number] = lun; 4709 ctl_softc->num_luns++; 4710 mtx_unlock(&ctl_softc->ctl_lock); 4711 4712 /* 4713 * We successfully added the LUN, attempt to enable it. 4714 */ 4715 if (ctl_enable_lun(lun) != 0) { 4716 printf("%s: ctl_enable_lun() failed!\n", __func__); 4717 mtx_lock(&ctl_softc->ctl_lock); 4718 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links); 4719 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number); 4720 ctl_softc->ctl_luns[lun_number] = NULL; 4721 ctl_softc->num_luns--; 4722 mtx_unlock(&ctl_softc->ctl_lock); 4723 free(lun->lun_devid, M_CTL); 4724 free(lun, M_CTL); 4725 return (EIO); 4726 } 4727 4728 return (0); 4729 } 4730 4731 /* 4732 * Free LUN that has no active requests. 4733 */ 4734 static int 4735 ctl_free_lun(struct ctl_lun *lun) 4736 { 4737 struct ctl_softc *softc = lun->ctl_softc; 4738 struct ctl_lun *nlun; 4739 int i; 4740 4741 KASSERT(LIST_EMPTY(&lun->ooa_queue), 4742 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4743 4744 mtx_lock(&softc->ctl_lock); 4745 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4746 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4747 softc->ctl_luns[lun->lun] = NULL; 4748 softc->num_luns--; 4749 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4750 mtx_lock(&nlun->lun_lock); 4751 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4752 mtx_unlock(&nlun->lun_lock); 4753 } 4754 mtx_unlock(&softc->ctl_lock); 4755 4756 /* 4757 * Tell the backend to free resources, if this LUN has a backend. 4758 */ 4759 lun->be_lun->lun_shutdown(lun->be_lun); 4760 4761 lun->ie_reportcnt = UINT32_MAX; 4762 callout_drain(&lun->ie_callout); 4763 ctl_tpc_lun_shutdown(lun); 4764 mtx_destroy(&lun->lun_lock); 4765 free(lun->lun_devid, M_CTL); 4766 for (i = 0; i < ctl_max_ports; i++) 4767 free(lun->pending_ua[i], M_CTL); 4768 free(lun->pending_ua, M_DEVBUF); 4769 for (i = 0; i < ctl_max_ports; i++) 4770 free(lun->pr_keys[i], M_CTL); 4771 free(lun->pr_keys, M_DEVBUF); 4772 free(lun->write_buffer, M_CTL); 4773 free(lun->prevent, M_CTL); 4774 free(lun, M_CTL); 4775 4776 return (0); 4777 } 4778 4779 static int 4780 ctl_enable_lun(struct ctl_lun *lun) 4781 { 4782 struct ctl_softc *softc; 4783 struct ctl_port *port, *nport; 4784 int retval; 4785 4786 softc = lun->ctl_softc; 4787 4788 mtx_lock(&softc->ctl_lock); 4789 mtx_lock(&lun->lun_lock); 4790 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0, 4791 ("%s: LUN not disabled", __func__)); 4792 lun->flags &= ~CTL_LUN_DISABLED; 4793 mtx_unlock(&lun->lun_lock); 4794 4795 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4796 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4797 port->lun_map != NULL || port->lun_enable == NULL) 4798 continue; 4799 4800 /* 4801 * Drop the lock while we call the FETD's enable routine. 4802 * This can lead to a callback into CTL (at least in the 4803 * case of the internal initiator frontend. 4804 */ 4805 mtx_unlock(&softc->ctl_lock); 4806 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4807 mtx_lock(&softc->ctl_lock); 4808 if (retval != 0) { 4809 printf("%s: FETD %s port %d returned error " 4810 "%d for lun_enable on lun %jd\n", 4811 __func__, port->port_name, port->targ_port, 4812 retval, (intmax_t)lun->lun); 4813 } 4814 } 4815 4816 mtx_unlock(&softc->ctl_lock); 4817 ctl_isc_announce_lun(lun); 4818 4819 return (0); 4820 } 4821 4822 static int 4823 ctl_disable_lun(struct ctl_lun *lun) 4824 { 4825 struct ctl_softc *softc; 4826 struct ctl_port *port; 4827 int retval; 4828 4829 softc = lun->ctl_softc; 4830 4831 mtx_lock(&softc->ctl_lock); 4832 mtx_lock(&lun->lun_lock); 4833 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0, 4834 ("%s: LUN not enabled", __func__)); 4835 lun->flags |= CTL_LUN_DISABLED; 4836 mtx_unlock(&lun->lun_lock); 4837 4838 STAILQ_FOREACH(port, &softc->port_list, links) { 4839 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4840 port->lun_map != NULL || port->lun_disable == NULL) 4841 continue; 4842 4843 /* 4844 * Drop the lock before we call the frontend's disable 4845 * routine, to avoid lock order reversals. 4846 * 4847 * XXX KDM what happens if the frontend list changes while 4848 * we're traversing it? It's unlikely, but should be handled. 4849 */ 4850 mtx_unlock(&softc->ctl_lock); 4851 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4852 mtx_lock(&softc->ctl_lock); 4853 if (retval != 0) { 4854 printf("%s: FETD %s port %d returned error " 4855 "%d for lun_disable on lun %jd\n", 4856 __func__, port->port_name, port->targ_port, 4857 retval, (intmax_t)lun->lun); 4858 } 4859 } 4860 4861 mtx_unlock(&softc->ctl_lock); 4862 ctl_isc_announce_lun(lun); 4863 4864 return (0); 4865 } 4866 4867 int 4868 ctl_start_lun(struct ctl_be_lun *be_lun) 4869 { 4870 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4871 4872 mtx_lock(&lun->lun_lock); 4873 lun->flags &= ~CTL_LUN_STOPPED; 4874 mtx_unlock(&lun->lun_lock); 4875 return (0); 4876 } 4877 4878 int 4879 ctl_stop_lun(struct ctl_be_lun *be_lun) 4880 { 4881 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4882 4883 mtx_lock(&lun->lun_lock); 4884 lun->flags |= CTL_LUN_STOPPED; 4885 mtx_unlock(&lun->lun_lock); 4886 return (0); 4887 } 4888 4889 int 4890 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4891 { 4892 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4893 4894 mtx_lock(&lun->lun_lock); 4895 lun->flags |= CTL_LUN_NO_MEDIA; 4896 mtx_unlock(&lun->lun_lock); 4897 return (0); 4898 } 4899 4900 int 4901 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4902 { 4903 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4904 union ctl_ha_msg msg; 4905 4906 mtx_lock(&lun->lun_lock); 4907 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4908 if (lun->flags & CTL_LUN_REMOVABLE) 4909 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4910 mtx_unlock(&lun->lun_lock); 4911 if ((lun->flags & CTL_LUN_REMOVABLE) && 4912 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4913 bzero(&msg.ua, sizeof(msg.ua)); 4914 msg.hdr.msg_type = CTL_MSG_UA; 4915 msg.hdr.nexus.initid = -1; 4916 msg.hdr.nexus.targ_port = -1; 4917 msg.hdr.nexus.targ_lun = lun->lun; 4918 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4919 msg.ua.ua_all = 1; 4920 msg.ua.ua_set = 1; 4921 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4922 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4923 M_WAITOK); 4924 } 4925 return (0); 4926 } 4927 4928 int 4929 ctl_lun_ejected(struct ctl_be_lun *be_lun) 4930 { 4931 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4932 4933 mtx_lock(&lun->lun_lock); 4934 lun->flags |= CTL_LUN_EJECTED; 4935 mtx_unlock(&lun->lun_lock); 4936 return (0); 4937 } 4938 4939 int 4940 ctl_lun_primary(struct ctl_be_lun *be_lun) 4941 { 4942 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4943 4944 mtx_lock(&lun->lun_lock); 4945 lun->flags |= CTL_LUN_PRIMARY_SC; 4946 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4947 mtx_unlock(&lun->lun_lock); 4948 ctl_isc_announce_lun(lun); 4949 return (0); 4950 } 4951 4952 int 4953 ctl_lun_secondary(struct ctl_be_lun *be_lun) 4954 { 4955 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4956 4957 mtx_lock(&lun->lun_lock); 4958 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4959 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4960 mtx_unlock(&lun->lun_lock); 4961 ctl_isc_announce_lun(lun); 4962 return (0); 4963 } 4964 4965 /* 4966 * Remove LUN. If there are active requests, wait for completion. 4967 * 4968 * Returns 0 for success, non-zero (errno) for failure. 4969 * Completion is reported to backed via the lun_shutdown() method. 4970 */ 4971 int 4972 ctl_remove_lun(struct ctl_be_lun *be_lun) 4973 { 4974 struct ctl_lun *lun; 4975 4976 lun = (struct ctl_lun *)be_lun->ctl_lun; 4977 4978 ctl_disable_lun(lun); 4979 4980 mtx_lock(&lun->lun_lock); 4981 lun->flags |= CTL_LUN_INVALID; 4982 4983 /* 4984 * If there is nothing in the OOA queue, go ahead and free the LUN. 4985 * If we have something in the OOA queue, we'll free it when the 4986 * last I/O completes. 4987 */ 4988 if (LIST_EMPTY(&lun->ooa_queue)) { 4989 mtx_unlock(&lun->lun_lock); 4990 ctl_free_lun(lun); 4991 } else 4992 mtx_unlock(&lun->lun_lock); 4993 4994 return (0); 4995 } 4996 4997 void 4998 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4999 { 5000 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5001 union ctl_ha_msg msg; 5002 5003 mtx_lock(&lun->lun_lock); 5004 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5005 mtx_unlock(&lun->lun_lock); 5006 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5007 /* Send msg to other side. */ 5008 bzero(&msg.ua, sizeof(msg.ua)); 5009 msg.hdr.msg_type = CTL_MSG_UA; 5010 msg.hdr.nexus.initid = -1; 5011 msg.hdr.nexus.targ_port = -1; 5012 msg.hdr.nexus.targ_lun = lun->lun; 5013 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5014 msg.ua.ua_all = 1; 5015 msg.ua.ua_set = 1; 5016 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5017 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5018 M_WAITOK); 5019 } 5020 } 5021 5022 /* 5023 * Backend "memory move is complete" callback for requests that never 5024 * make it down to say RAIDCore's configuration code. 5025 */ 5026 int 5027 ctl_config_move_done(union ctl_io *io, bool samethr) 5028 { 5029 int retval; 5030 5031 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5032 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5033 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 5034 5035 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5036 ctl_data_print(io); 5037 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5038 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5039 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5040 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5041 /* 5042 * XXX KDM just assuming a single pointer here, and not a 5043 * S/G list. If we start using S/G lists for config data, 5044 * we'll need to know how to clean them up here as well. 5045 */ 5046 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5047 free(io->scsiio.kern_data_ptr, M_CTL); 5048 ctl_done(io); 5049 retval = CTL_RETVAL_COMPLETE; 5050 } else { 5051 /* 5052 * XXX KDM now we need to continue data movement. Some 5053 * options: 5054 * - call ctl_scsiio() again? We don't do this for data 5055 * writes, because for those at least we know ahead of 5056 * time where the write will go and how long it is. For 5057 * config writes, though, that information is largely 5058 * contained within the write itself, thus we need to 5059 * parse out the data again. 5060 * 5061 * - Call some other function once the data is in? 5062 */ 5063 5064 /* 5065 * XXX KDM call ctl_scsiio() again for now, and check flag 5066 * bits to see whether we're allocated or not. 5067 */ 5068 retval = ctl_scsiio(&io->scsiio); 5069 } 5070 return (retval); 5071 } 5072 5073 /* 5074 * This gets called by a backend driver when it is done with a 5075 * data_submit method. 5076 */ 5077 void 5078 ctl_data_submit_done(union ctl_io *io) 5079 { 5080 /* 5081 * If the IO_CONT flag is set, we need to call the supplied 5082 * function to continue processing the I/O, instead of completing 5083 * the I/O just yet. 5084 * 5085 * If there is an error, though, we don't want to keep processing. 5086 * Instead, just send status back to the initiator. 5087 */ 5088 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5089 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5090 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5091 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5092 io->scsiio.io_cont(io); 5093 return; 5094 } 5095 ctl_done(io); 5096 } 5097 5098 /* 5099 * This gets called by a backend driver when it is done with a 5100 * configuration write. 5101 */ 5102 void 5103 ctl_config_write_done(union ctl_io *io) 5104 { 5105 uint8_t *buf; 5106 5107 /* 5108 * If the IO_CONT flag is set, we need to call the supplied 5109 * function to continue processing the I/O, instead of completing 5110 * the I/O just yet. 5111 * 5112 * If there is an error, though, we don't want to keep processing. 5113 * Instead, just send status back to the initiator. 5114 */ 5115 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5116 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5117 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5118 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5119 io->scsiio.io_cont(io); 5120 return; 5121 } 5122 /* 5123 * Since a configuration write can be done for commands that actually 5124 * have data allocated, like write buffer, and commands that have 5125 * no data, like start/stop unit, we need to check here. 5126 */ 5127 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5128 buf = io->scsiio.kern_data_ptr; 5129 else 5130 buf = NULL; 5131 ctl_done(io); 5132 if (buf) 5133 free(buf, M_CTL); 5134 } 5135 5136 void 5137 ctl_config_read_done(union ctl_io *io) 5138 { 5139 uint8_t *buf; 5140 5141 /* 5142 * If there is some error -- we are done, skip data transfer. 5143 */ 5144 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5145 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5146 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5147 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5148 buf = io->scsiio.kern_data_ptr; 5149 else 5150 buf = NULL; 5151 ctl_done(io); 5152 if (buf) 5153 free(buf, M_CTL); 5154 return; 5155 } 5156 5157 /* 5158 * If the IO_CONT flag is set, we need to call the supplied 5159 * function to continue processing the I/O, instead of completing 5160 * the I/O just yet. 5161 */ 5162 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5163 io->scsiio.io_cont(io); 5164 return; 5165 } 5166 5167 ctl_datamove(io); 5168 } 5169 5170 /* 5171 * SCSI release command. 5172 */ 5173 int 5174 ctl_scsi_release(struct ctl_scsiio *ctsio) 5175 { 5176 struct ctl_lun *lun = CTL_LUN(ctsio); 5177 uint32_t residx; 5178 5179 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5180 5181 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5182 5183 /* 5184 * XXX KDM right now, we only support LUN reservation. We don't 5185 * support 3rd party reservations, or extent reservations, which 5186 * might actually need the parameter list. If we've gotten this 5187 * far, we've got a LUN reservation. Anything else got kicked out 5188 * above. So, according to SPC, ignore the length. 5189 */ 5190 5191 mtx_lock(&lun->lun_lock); 5192 5193 /* 5194 * According to SPC, it is not an error for an intiator to attempt 5195 * to release a reservation on a LUN that isn't reserved, or that 5196 * is reserved by another initiator. The reservation can only be 5197 * released, though, by the initiator who made it or by one of 5198 * several reset type events. 5199 */ 5200 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5201 lun->flags &= ~CTL_LUN_RESERVED; 5202 5203 mtx_unlock(&lun->lun_lock); 5204 5205 ctl_set_success(ctsio); 5206 ctl_done((union ctl_io *)ctsio); 5207 return (CTL_RETVAL_COMPLETE); 5208 } 5209 5210 int 5211 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5212 { 5213 struct ctl_lun *lun = CTL_LUN(ctsio); 5214 uint32_t residx; 5215 5216 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5217 5218 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5219 5220 /* 5221 * XXX KDM right now, we only support LUN reservation. We don't 5222 * support 3rd party reservations, or extent reservations, which 5223 * might actually need the parameter list. If we've gotten this 5224 * far, we've got a LUN reservation. Anything else got kicked out 5225 * above. So, according to SPC, ignore the length. 5226 */ 5227 5228 mtx_lock(&lun->lun_lock); 5229 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5230 ctl_set_reservation_conflict(ctsio); 5231 goto bailout; 5232 } 5233 5234 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5235 if (lun->flags & CTL_LUN_PR_RESERVED) { 5236 ctl_set_success(ctsio); 5237 goto bailout; 5238 } 5239 5240 lun->flags |= CTL_LUN_RESERVED; 5241 lun->res_idx = residx; 5242 ctl_set_success(ctsio); 5243 5244 bailout: 5245 mtx_unlock(&lun->lun_lock); 5246 ctl_done((union ctl_io *)ctsio); 5247 return (CTL_RETVAL_COMPLETE); 5248 } 5249 5250 int 5251 ctl_start_stop(struct ctl_scsiio *ctsio) 5252 { 5253 struct ctl_lun *lun = CTL_LUN(ctsio); 5254 struct scsi_start_stop_unit *cdb; 5255 int retval; 5256 5257 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5258 5259 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5260 5261 if ((cdb->how & SSS_PC_MASK) == 0) { 5262 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5263 (cdb->how & SSS_START) == 0) { 5264 uint32_t residx; 5265 5266 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5267 if (ctl_get_prkey(lun, residx) == 0 || 5268 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5269 ctl_set_reservation_conflict(ctsio); 5270 ctl_done((union ctl_io *)ctsio); 5271 return (CTL_RETVAL_COMPLETE); 5272 } 5273 } 5274 5275 if ((cdb->how & SSS_LOEJ) && 5276 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5277 ctl_set_invalid_field(ctsio, 5278 /*sks_valid*/ 1, 5279 /*command*/ 1, 5280 /*field*/ 4, 5281 /*bit_valid*/ 1, 5282 /*bit*/ 1); 5283 ctl_done((union ctl_io *)ctsio); 5284 return (CTL_RETVAL_COMPLETE); 5285 } 5286 5287 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5288 lun->prevent_count > 0) { 5289 /* "Medium removal prevented" */ 5290 ctl_set_sense(ctsio, /*current_error*/ 1, 5291 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5292 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5293 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5294 ctl_done((union ctl_io *)ctsio); 5295 return (CTL_RETVAL_COMPLETE); 5296 } 5297 } 5298 5299 retval = lun->backend->config_write((union ctl_io *)ctsio); 5300 return (retval); 5301 } 5302 5303 int 5304 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5305 { 5306 struct ctl_lun *lun = CTL_LUN(ctsio); 5307 struct scsi_prevent *cdb; 5308 int retval; 5309 uint32_t initidx; 5310 5311 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5312 5313 cdb = (struct scsi_prevent *)ctsio->cdb; 5314 5315 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5316 ctl_set_invalid_opcode(ctsio); 5317 ctl_done((union ctl_io *)ctsio); 5318 return (CTL_RETVAL_COMPLETE); 5319 } 5320 5321 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5322 mtx_lock(&lun->lun_lock); 5323 if ((cdb->how & PR_PREVENT) && 5324 ctl_is_set(lun->prevent, initidx) == 0) { 5325 ctl_set_mask(lun->prevent, initidx); 5326 lun->prevent_count++; 5327 } else if ((cdb->how & PR_PREVENT) == 0 && 5328 ctl_is_set(lun->prevent, initidx)) { 5329 ctl_clear_mask(lun->prevent, initidx); 5330 lun->prevent_count--; 5331 } 5332 mtx_unlock(&lun->lun_lock); 5333 retval = lun->backend->config_write((union ctl_io *)ctsio); 5334 return (retval); 5335 } 5336 5337 /* 5338 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5339 * we don't really do anything with the LBA and length fields if the user 5340 * passes them in. Instead we'll just flush out the cache for the entire 5341 * LUN. 5342 */ 5343 int 5344 ctl_sync_cache(struct ctl_scsiio *ctsio) 5345 { 5346 struct ctl_lun *lun = CTL_LUN(ctsio); 5347 struct ctl_lba_len_flags *lbalen; 5348 uint64_t starting_lba; 5349 uint32_t block_count; 5350 int retval; 5351 uint8_t byte2; 5352 5353 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5354 5355 retval = 0; 5356 5357 switch (ctsio->cdb[0]) { 5358 case SYNCHRONIZE_CACHE: { 5359 struct scsi_sync_cache *cdb; 5360 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5361 5362 starting_lba = scsi_4btoul(cdb->begin_lba); 5363 block_count = scsi_2btoul(cdb->lb_count); 5364 byte2 = cdb->byte2; 5365 break; 5366 } 5367 case SYNCHRONIZE_CACHE_16: { 5368 struct scsi_sync_cache_16 *cdb; 5369 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5370 5371 starting_lba = scsi_8btou64(cdb->begin_lba); 5372 block_count = scsi_4btoul(cdb->lb_count); 5373 byte2 = cdb->byte2; 5374 break; 5375 } 5376 default: 5377 ctl_set_invalid_opcode(ctsio); 5378 ctl_done((union ctl_io *)ctsio); 5379 goto bailout; 5380 break; /* NOTREACHED */ 5381 } 5382 5383 /* 5384 * We check the LBA and length, but don't do anything with them. 5385 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5386 * get flushed. This check will just help satisfy anyone who wants 5387 * to see an error for an out of range LBA. 5388 */ 5389 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5390 ctl_set_lba_out_of_range(ctsio, 5391 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5392 ctl_done((union ctl_io *)ctsio); 5393 goto bailout; 5394 } 5395 5396 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5397 lbalen->lba = starting_lba; 5398 lbalen->len = block_count; 5399 lbalen->flags = byte2; 5400 retval = lun->backend->config_write((union ctl_io *)ctsio); 5401 5402 bailout: 5403 return (retval); 5404 } 5405 5406 int 5407 ctl_format(struct ctl_scsiio *ctsio) 5408 { 5409 struct scsi_format *cdb; 5410 int length, defect_list_len; 5411 5412 CTL_DEBUG_PRINT(("ctl_format\n")); 5413 5414 cdb = (struct scsi_format *)ctsio->cdb; 5415 5416 length = 0; 5417 if (cdb->byte2 & SF_FMTDATA) { 5418 if (cdb->byte2 & SF_LONGLIST) 5419 length = sizeof(struct scsi_format_header_long); 5420 else 5421 length = sizeof(struct scsi_format_header_short); 5422 } 5423 5424 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5425 && (length > 0)) { 5426 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5427 ctsio->kern_data_len = length; 5428 ctsio->kern_total_len = length; 5429 ctsio->kern_rel_offset = 0; 5430 ctsio->kern_sg_entries = 0; 5431 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5432 ctsio->be_move_done = ctl_config_move_done; 5433 ctl_datamove((union ctl_io *)ctsio); 5434 5435 return (CTL_RETVAL_COMPLETE); 5436 } 5437 5438 defect_list_len = 0; 5439 5440 if (cdb->byte2 & SF_FMTDATA) { 5441 if (cdb->byte2 & SF_LONGLIST) { 5442 struct scsi_format_header_long *header; 5443 5444 header = (struct scsi_format_header_long *) 5445 ctsio->kern_data_ptr; 5446 5447 defect_list_len = scsi_4btoul(header->defect_list_len); 5448 if (defect_list_len != 0) { 5449 ctl_set_invalid_field(ctsio, 5450 /*sks_valid*/ 1, 5451 /*command*/ 0, 5452 /*field*/ 2, 5453 /*bit_valid*/ 0, 5454 /*bit*/ 0); 5455 goto bailout; 5456 } 5457 } else { 5458 struct scsi_format_header_short *header; 5459 5460 header = (struct scsi_format_header_short *) 5461 ctsio->kern_data_ptr; 5462 5463 defect_list_len = scsi_2btoul(header->defect_list_len); 5464 if (defect_list_len != 0) { 5465 ctl_set_invalid_field(ctsio, 5466 /*sks_valid*/ 1, 5467 /*command*/ 0, 5468 /*field*/ 2, 5469 /*bit_valid*/ 0, 5470 /*bit*/ 0); 5471 goto bailout; 5472 } 5473 } 5474 } 5475 5476 ctl_set_success(ctsio); 5477 bailout: 5478 5479 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5480 free(ctsio->kern_data_ptr, M_CTL); 5481 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5482 } 5483 5484 ctl_done((union ctl_io *)ctsio); 5485 return (CTL_RETVAL_COMPLETE); 5486 } 5487 5488 int 5489 ctl_read_buffer(struct ctl_scsiio *ctsio) 5490 { 5491 struct ctl_lun *lun = CTL_LUN(ctsio); 5492 uint64_t buffer_offset; 5493 uint32_t len; 5494 uint8_t byte2; 5495 static uint8_t descr[4]; 5496 static uint8_t echo_descr[4] = { 0 }; 5497 5498 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5499 5500 switch (ctsio->cdb[0]) { 5501 case READ_BUFFER: { 5502 struct scsi_read_buffer *cdb; 5503 5504 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5505 buffer_offset = scsi_3btoul(cdb->offset); 5506 len = scsi_3btoul(cdb->length); 5507 byte2 = cdb->byte2; 5508 break; 5509 } 5510 case READ_BUFFER_16: { 5511 struct scsi_read_buffer_16 *cdb; 5512 5513 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5514 buffer_offset = scsi_8btou64(cdb->offset); 5515 len = scsi_4btoul(cdb->length); 5516 byte2 = cdb->byte2; 5517 break; 5518 } 5519 default: /* This shouldn't happen. */ 5520 ctl_set_invalid_opcode(ctsio); 5521 ctl_done((union ctl_io *)ctsio); 5522 return (CTL_RETVAL_COMPLETE); 5523 } 5524 5525 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5526 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5527 ctl_set_invalid_field(ctsio, 5528 /*sks_valid*/ 1, 5529 /*command*/ 1, 5530 /*field*/ 6, 5531 /*bit_valid*/ 0, 5532 /*bit*/ 0); 5533 ctl_done((union ctl_io *)ctsio); 5534 return (CTL_RETVAL_COMPLETE); 5535 } 5536 5537 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5538 descr[0] = 0; 5539 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5540 ctsio->kern_data_ptr = descr; 5541 len = min(len, sizeof(descr)); 5542 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5543 ctsio->kern_data_ptr = echo_descr; 5544 len = min(len, sizeof(echo_descr)); 5545 } else { 5546 if (lun->write_buffer == NULL) { 5547 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5548 M_CTL, M_WAITOK); 5549 } 5550 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5551 } 5552 ctsio->kern_data_len = len; 5553 ctsio->kern_total_len = len; 5554 ctsio->kern_rel_offset = 0; 5555 ctsio->kern_sg_entries = 0; 5556 ctl_set_success(ctsio); 5557 ctsio->be_move_done = ctl_config_move_done; 5558 ctl_datamove((union ctl_io *)ctsio); 5559 return (CTL_RETVAL_COMPLETE); 5560 } 5561 5562 int 5563 ctl_write_buffer(struct ctl_scsiio *ctsio) 5564 { 5565 struct ctl_lun *lun = CTL_LUN(ctsio); 5566 struct scsi_write_buffer *cdb; 5567 int buffer_offset, len; 5568 5569 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5570 5571 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5572 5573 len = scsi_3btoul(cdb->length); 5574 buffer_offset = scsi_3btoul(cdb->offset); 5575 5576 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5577 ctl_set_invalid_field(ctsio, 5578 /*sks_valid*/ 1, 5579 /*command*/ 1, 5580 /*field*/ 6, 5581 /*bit_valid*/ 0, 5582 /*bit*/ 0); 5583 ctl_done((union ctl_io *)ctsio); 5584 return (CTL_RETVAL_COMPLETE); 5585 } 5586 5587 /* 5588 * If we've got a kernel request that hasn't been malloced yet, 5589 * malloc it and tell the caller the data buffer is here. 5590 */ 5591 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5592 if (lun->write_buffer == NULL) { 5593 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5594 M_CTL, M_WAITOK); 5595 } 5596 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5597 ctsio->kern_data_len = len; 5598 ctsio->kern_total_len = len; 5599 ctsio->kern_rel_offset = 0; 5600 ctsio->kern_sg_entries = 0; 5601 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5602 ctsio->be_move_done = ctl_config_move_done; 5603 ctl_datamove((union ctl_io *)ctsio); 5604 5605 return (CTL_RETVAL_COMPLETE); 5606 } 5607 5608 ctl_set_success(ctsio); 5609 ctl_done((union ctl_io *)ctsio); 5610 return (CTL_RETVAL_COMPLETE); 5611 } 5612 5613 static int 5614 ctl_write_same_cont(union ctl_io *io) 5615 { 5616 struct ctl_lun *lun = CTL_LUN(io); 5617 struct ctl_scsiio *ctsio; 5618 struct ctl_lba_len_flags *lbalen; 5619 int retval; 5620 5621 ctsio = &io->scsiio; 5622 ctsio->io_hdr.status = CTL_STATUS_NONE; 5623 lbalen = (struct ctl_lba_len_flags *) 5624 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5625 lbalen->lba += lbalen->len; 5626 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) { 5627 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 5628 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba; 5629 } 5630 5631 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n")); 5632 retval = lun->backend->config_write((union ctl_io *)ctsio); 5633 return (retval); 5634 } 5635 5636 int 5637 ctl_write_same(struct ctl_scsiio *ctsio) 5638 { 5639 struct ctl_lun *lun = CTL_LUN(ctsio); 5640 struct ctl_lba_len_flags *lbalen; 5641 const char *val; 5642 uint64_t lba, ival; 5643 uint32_t num_blocks; 5644 int len, retval; 5645 uint8_t byte2; 5646 5647 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5648 5649 switch (ctsio->cdb[0]) { 5650 case WRITE_SAME_10: { 5651 struct scsi_write_same_10 *cdb; 5652 5653 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5654 5655 lba = scsi_4btoul(cdb->addr); 5656 num_blocks = scsi_2btoul(cdb->length); 5657 byte2 = cdb->byte2; 5658 break; 5659 } 5660 case WRITE_SAME_16: { 5661 struct scsi_write_same_16 *cdb; 5662 5663 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5664 5665 lba = scsi_8btou64(cdb->addr); 5666 num_blocks = scsi_4btoul(cdb->length); 5667 byte2 = cdb->byte2; 5668 break; 5669 } 5670 default: 5671 /* 5672 * We got a command we don't support. This shouldn't 5673 * happen, commands should be filtered out above us. 5674 */ 5675 ctl_set_invalid_opcode(ctsio); 5676 ctl_done((union ctl_io *)ctsio); 5677 5678 return (CTL_RETVAL_COMPLETE); 5679 break; /* NOTREACHED */ 5680 } 5681 5682 /* ANCHOR flag can be used only together with UNMAP */ 5683 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5684 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5685 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5686 ctl_done((union ctl_io *)ctsio); 5687 return (CTL_RETVAL_COMPLETE); 5688 } 5689 5690 /* 5691 * The first check is to make sure we're in bounds, the second 5692 * check is to catch wrap-around problems. If the lba + num blocks 5693 * is less than the lba, then we've wrapped around and the block 5694 * range is invalid anyway. 5695 */ 5696 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5697 || ((lba + num_blocks) < lba)) { 5698 ctl_set_lba_out_of_range(ctsio, 5699 MAX(lba, lun->be_lun->maxlba + 1)); 5700 ctl_done((union ctl_io *)ctsio); 5701 return (CTL_RETVAL_COMPLETE); 5702 } 5703 5704 /* Zero number of blocks means "to the last logical block" */ 5705 if (num_blocks == 0) { 5706 ival = UINT64_MAX; 5707 val = dnvlist_get_string(lun->be_lun->options, 5708 "write_same_max_lba", NULL); 5709 if (val != NULL) 5710 ctl_expand_number(val, &ival); 5711 if ((lun->be_lun->maxlba + 1) - lba > ival) { 5712 ctl_set_invalid_field(ctsio, 5713 /*sks_valid*/ 1, /*command*/ 1, 5714 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10, 5715 /*bit_valid*/ 0, /*bit*/ 0); 5716 ctl_done((union ctl_io *)ctsio); 5717 return (CTL_RETVAL_COMPLETE); 5718 } 5719 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5720 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 5721 ctsio->io_cont = ctl_write_same_cont; 5722 num_blocks = 1 << 31; 5723 } else 5724 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5725 } 5726 5727 len = lun->be_lun->blocksize; 5728 5729 /* 5730 * If we've got a kernel request that hasn't been malloced yet, 5731 * malloc it and tell the caller the data buffer is here. 5732 */ 5733 if ((byte2 & SWS_NDOB) == 0 && 5734 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5735 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5736 ctsio->kern_data_len = len; 5737 ctsio->kern_total_len = len; 5738 ctsio->kern_rel_offset = 0; 5739 ctsio->kern_sg_entries = 0; 5740 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5741 ctsio->be_move_done = ctl_config_move_done; 5742 ctl_datamove((union ctl_io *)ctsio); 5743 5744 return (CTL_RETVAL_COMPLETE); 5745 } 5746 5747 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5748 lbalen->lba = lba; 5749 lbalen->len = num_blocks; 5750 lbalen->flags = byte2; 5751 retval = lun->backend->config_write((union ctl_io *)ctsio); 5752 5753 return (retval); 5754 } 5755 5756 int 5757 ctl_unmap(struct ctl_scsiio *ctsio) 5758 { 5759 struct ctl_lun *lun = CTL_LUN(ctsio); 5760 struct scsi_unmap *cdb; 5761 struct ctl_ptr_len_flags *ptrlen; 5762 struct scsi_unmap_header *hdr; 5763 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5764 uint64_t lba; 5765 uint32_t num_blocks; 5766 int len, retval; 5767 uint8_t byte2; 5768 5769 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5770 5771 cdb = (struct scsi_unmap *)ctsio->cdb; 5772 len = scsi_2btoul(cdb->length); 5773 byte2 = cdb->byte2; 5774 5775 /* 5776 * If we've got a kernel request that hasn't been malloced yet, 5777 * malloc it and tell the caller the data buffer is here. 5778 */ 5779 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5780 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5781 ctsio->kern_data_len = len; 5782 ctsio->kern_total_len = len; 5783 ctsio->kern_rel_offset = 0; 5784 ctsio->kern_sg_entries = 0; 5785 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5786 ctsio->be_move_done = ctl_config_move_done; 5787 ctl_datamove((union ctl_io *)ctsio); 5788 5789 return (CTL_RETVAL_COMPLETE); 5790 } 5791 5792 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5793 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5794 if (len < sizeof (*hdr) || 5795 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5796 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5797 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5798 ctl_set_invalid_field(ctsio, 5799 /*sks_valid*/ 0, 5800 /*command*/ 0, 5801 /*field*/ 0, 5802 /*bit_valid*/ 0, 5803 /*bit*/ 0); 5804 goto done; 5805 } 5806 len = scsi_2btoul(hdr->desc_length); 5807 buf = (struct scsi_unmap_desc *)(hdr + 1); 5808 end = buf + len / sizeof(*buf); 5809 5810 endnz = buf; 5811 for (range = buf; range < end; range++) { 5812 lba = scsi_8btou64(range->lba); 5813 num_blocks = scsi_4btoul(range->length); 5814 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5815 || ((lba + num_blocks) < lba)) { 5816 ctl_set_lba_out_of_range(ctsio, 5817 MAX(lba, lun->be_lun->maxlba + 1)); 5818 ctl_done((union ctl_io *)ctsio); 5819 return (CTL_RETVAL_COMPLETE); 5820 } 5821 if (num_blocks != 0) 5822 endnz = range + 1; 5823 } 5824 5825 /* 5826 * Block backend can not handle zero last range. 5827 * Filter it out and return if there is nothing left. 5828 */ 5829 len = (uint8_t *)endnz - (uint8_t *)buf; 5830 if (len == 0) { 5831 ctl_set_success(ctsio); 5832 goto done; 5833 } 5834 5835 mtx_lock(&lun->lun_lock); 5836 ptrlen = (struct ctl_ptr_len_flags *) 5837 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5838 ptrlen->ptr = (void *)buf; 5839 ptrlen->len = len; 5840 ptrlen->flags = byte2; 5841 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE); 5842 mtx_unlock(&lun->lun_lock); 5843 5844 retval = lun->backend->config_write((union ctl_io *)ctsio); 5845 return (retval); 5846 5847 done: 5848 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5849 free(ctsio->kern_data_ptr, M_CTL); 5850 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5851 } 5852 ctl_done((union ctl_io *)ctsio); 5853 return (CTL_RETVAL_COMPLETE); 5854 } 5855 5856 int 5857 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5858 struct ctl_page_index *page_index, uint8_t *page_ptr) 5859 { 5860 struct ctl_lun *lun = CTL_LUN(ctsio); 5861 uint8_t *current_cp; 5862 int set_ua; 5863 uint32_t initidx; 5864 5865 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5866 set_ua = 0; 5867 5868 current_cp = (page_index->page_data + (page_index->page_len * 5869 CTL_PAGE_CURRENT)); 5870 5871 mtx_lock(&lun->lun_lock); 5872 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5873 memcpy(current_cp, page_ptr, page_index->page_len); 5874 set_ua = 1; 5875 } 5876 if (set_ua != 0) 5877 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5878 mtx_unlock(&lun->lun_lock); 5879 if (set_ua) { 5880 ctl_isc_announce_mode(lun, 5881 ctl_get_initindex(&ctsio->io_hdr.nexus), 5882 page_index->page_code, page_index->subpage); 5883 } 5884 return (CTL_RETVAL_COMPLETE); 5885 } 5886 5887 static void 5888 ctl_ie_timer(void *arg) 5889 { 5890 struct ctl_lun *lun = arg; 5891 uint64_t t; 5892 5893 if (lun->ie_asc == 0) 5894 return; 5895 5896 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5897 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5898 else 5899 lun->ie_reported = 0; 5900 5901 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5902 lun->ie_reportcnt++; 5903 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5904 if (t == 0 || t == UINT32_MAX) 5905 t = 3000; /* 5 min */ 5906 callout_schedule_sbt(&lun->ie_callout, SBT_1S / 10 * t, 5907 SBT_1S / 10, 0); 5908 } 5909 } 5910 5911 int 5912 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5913 struct ctl_page_index *page_index, uint8_t *page_ptr) 5914 { 5915 struct ctl_lun *lun = CTL_LUN(ctsio); 5916 struct scsi_info_exceptions_page *pg; 5917 uint64_t t; 5918 5919 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5920 5921 pg = (struct scsi_info_exceptions_page *)page_ptr; 5922 mtx_lock(&lun->lun_lock); 5923 if (pg->info_flags & SIEP_FLAGS_TEST) { 5924 lun->ie_asc = 0x5d; 5925 lun->ie_ascq = 0xff; 5926 if (pg->mrie == SIEP_MRIE_UA) { 5927 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5928 lun->ie_reported = 1; 5929 } else { 5930 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5931 lun->ie_reported = -1; 5932 } 5933 lun->ie_reportcnt = 1; 5934 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5935 lun->ie_reportcnt++; 5936 t = scsi_4btoul(pg->interval_timer); 5937 if (t == 0 || t == UINT32_MAX) 5938 t = 3000; /* 5 min */ 5939 callout_reset_sbt(&lun->ie_callout, SBT_1S / 10 * t, 5940 SBT_1S / 10, ctl_ie_timer, lun, 0); 5941 } 5942 } else { 5943 lun->ie_asc = 0; 5944 lun->ie_ascq = 0; 5945 lun->ie_reported = 1; 5946 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5947 lun->ie_reportcnt = UINT32_MAX; 5948 callout_stop(&lun->ie_callout); 5949 } 5950 mtx_unlock(&lun->lun_lock); 5951 return (CTL_RETVAL_COMPLETE); 5952 } 5953 5954 static int 5955 ctl_do_mode_select(union ctl_io *io) 5956 { 5957 struct ctl_lun *lun = CTL_LUN(io); 5958 struct scsi_mode_page_header *page_header; 5959 struct ctl_page_index *page_index; 5960 struct ctl_scsiio *ctsio; 5961 int page_len, page_len_offset, page_len_size; 5962 union ctl_modepage_info *modepage_info; 5963 uint16_t *len_left, *len_used; 5964 int retval, i; 5965 5966 ctsio = &io->scsiio; 5967 page_index = NULL; 5968 page_len = 0; 5969 5970 modepage_info = (union ctl_modepage_info *) 5971 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5972 len_left = &modepage_info->header.len_left; 5973 len_used = &modepage_info->header.len_used; 5974 5975 do_next_page: 5976 5977 page_header = (struct scsi_mode_page_header *) 5978 (ctsio->kern_data_ptr + *len_used); 5979 5980 if (*len_left == 0) { 5981 free(ctsio->kern_data_ptr, M_CTL); 5982 ctl_set_success(ctsio); 5983 ctl_done((union ctl_io *)ctsio); 5984 return (CTL_RETVAL_COMPLETE); 5985 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5986 free(ctsio->kern_data_ptr, M_CTL); 5987 ctl_set_param_len_error(ctsio); 5988 ctl_done((union ctl_io *)ctsio); 5989 return (CTL_RETVAL_COMPLETE); 5990 5991 } else if ((page_header->page_code & SMPH_SPF) 5992 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5993 free(ctsio->kern_data_ptr, M_CTL); 5994 ctl_set_param_len_error(ctsio); 5995 ctl_done((union ctl_io *)ctsio); 5996 return (CTL_RETVAL_COMPLETE); 5997 } 5998 5999 /* 6000 * XXX KDM should we do something with the block descriptor? 6001 */ 6002 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6003 page_index = &lun->mode_pages.index[i]; 6004 if (lun->be_lun->lun_type == T_DIRECT && 6005 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6006 continue; 6007 if (lun->be_lun->lun_type == T_PROCESSOR && 6008 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6009 continue; 6010 if (lun->be_lun->lun_type == T_CDROM && 6011 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6012 continue; 6013 6014 if ((page_index->page_code & SMPH_PC_MASK) != 6015 (page_header->page_code & SMPH_PC_MASK)) 6016 continue; 6017 6018 /* 6019 * If neither page has a subpage code, then we've got a 6020 * match. 6021 */ 6022 if (((page_index->page_code & SMPH_SPF) == 0) 6023 && ((page_header->page_code & SMPH_SPF) == 0)) { 6024 page_len = page_header->page_length; 6025 break; 6026 } 6027 6028 /* 6029 * If both pages have subpages, then the subpage numbers 6030 * have to match. 6031 */ 6032 if ((page_index->page_code & SMPH_SPF) 6033 && (page_header->page_code & SMPH_SPF)) { 6034 struct scsi_mode_page_header_sp *sph; 6035 6036 sph = (struct scsi_mode_page_header_sp *)page_header; 6037 if (page_index->subpage == sph->subpage) { 6038 page_len = scsi_2btoul(sph->page_length); 6039 break; 6040 } 6041 } 6042 } 6043 6044 /* 6045 * If we couldn't find the page, or if we don't have a mode select 6046 * handler for it, send back an error to the user. 6047 */ 6048 if ((i >= CTL_NUM_MODE_PAGES) 6049 || (page_index->select_handler == NULL)) { 6050 ctl_set_invalid_field(ctsio, 6051 /*sks_valid*/ 1, 6052 /*command*/ 0, 6053 /*field*/ *len_used, 6054 /*bit_valid*/ 0, 6055 /*bit*/ 0); 6056 free(ctsio->kern_data_ptr, M_CTL); 6057 ctl_done((union ctl_io *)ctsio); 6058 return (CTL_RETVAL_COMPLETE); 6059 } 6060 6061 if (page_index->page_code & SMPH_SPF) { 6062 page_len_offset = 2; 6063 page_len_size = 2; 6064 } else { 6065 page_len_size = 1; 6066 page_len_offset = 1; 6067 } 6068 6069 /* 6070 * If the length the initiator gives us isn't the one we specify in 6071 * the mode page header, or if they didn't specify enough data in 6072 * the CDB to avoid truncating this page, kick out the request. 6073 */ 6074 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6075 ctl_set_invalid_field(ctsio, 6076 /*sks_valid*/ 1, 6077 /*command*/ 0, 6078 /*field*/ *len_used + page_len_offset, 6079 /*bit_valid*/ 0, 6080 /*bit*/ 0); 6081 free(ctsio->kern_data_ptr, M_CTL); 6082 ctl_done((union ctl_io *)ctsio); 6083 return (CTL_RETVAL_COMPLETE); 6084 } 6085 if (*len_left < page_index->page_len) { 6086 free(ctsio->kern_data_ptr, M_CTL); 6087 ctl_set_param_len_error(ctsio); 6088 ctl_done((union ctl_io *)ctsio); 6089 return (CTL_RETVAL_COMPLETE); 6090 } 6091 6092 /* 6093 * Run through the mode page, checking to make sure that the bits 6094 * the user changed are actually legal for him to change. 6095 */ 6096 for (i = 0; i < page_index->page_len; i++) { 6097 uint8_t *user_byte, *change_mask, *current_byte; 6098 int bad_bit; 6099 int j; 6100 6101 user_byte = (uint8_t *)page_header + i; 6102 change_mask = page_index->page_data + 6103 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6104 current_byte = page_index->page_data + 6105 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6106 6107 /* 6108 * Check to see whether the user set any bits in this byte 6109 * that he is not allowed to set. 6110 */ 6111 if ((*user_byte & ~(*change_mask)) == 6112 (*current_byte & ~(*change_mask))) 6113 continue; 6114 6115 /* 6116 * Go through bit by bit to determine which one is illegal. 6117 */ 6118 bad_bit = 0; 6119 for (j = 7; j >= 0; j--) { 6120 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6121 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6122 bad_bit = i; 6123 break; 6124 } 6125 } 6126 ctl_set_invalid_field(ctsio, 6127 /*sks_valid*/ 1, 6128 /*command*/ 0, 6129 /*field*/ *len_used + i, 6130 /*bit_valid*/ 1, 6131 /*bit*/ bad_bit); 6132 free(ctsio->kern_data_ptr, M_CTL); 6133 ctl_done((union ctl_io *)ctsio); 6134 return (CTL_RETVAL_COMPLETE); 6135 } 6136 6137 /* 6138 * Decrement these before we call the page handler, since we may 6139 * end up getting called back one way or another before the handler 6140 * returns to this context. 6141 */ 6142 *len_left -= page_index->page_len; 6143 *len_used += page_index->page_len; 6144 6145 retval = page_index->select_handler(ctsio, page_index, 6146 (uint8_t *)page_header); 6147 6148 /* 6149 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6150 * wait until this queued command completes to finish processing 6151 * the mode page. If it returns anything other than 6152 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6153 * already set the sense information, freed the data pointer, and 6154 * completed the io for us. 6155 */ 6156 if (retval != CTL_RETVAL_COMPLETE) 6157 goto bailout_no_done; 6158 6159 /* 6160 * If the initiator sent us more than one page, parse the next one. 6161 */ 6162 if (*len_left > 0) 6163 goto do_next_page; 6164 6165 ctl_set_success(ctsio); 6166 free(ctsio->kern_data_ptr, M_CTL); 6167 ctl_done((union ctl_io *)ctsio); 6168 6169 bailout_no_done: 6170 6171 return (CTL_RETVAL_COMPLETE); 6172 6173 } 6174 6175 int 6176 ctl_mode_select(struct ctl_scsiio *ctsio) 6177 { 6178 struct ctl_lun *lun = CTL_LUN(ctsio); 6179 union ctl_modepage_info *modepage_info; 6180 int bd_len, i, header_size, param_len, rtd; 6181 uint32_t initidx; 6182 6183 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6184 switch (ctsio->cdb[0]) { 6185 case MODE_SELECT_6: { 6186 struct scsi_mode_select_6 *cdb; 6187 6188 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6189 6190 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6191 param_len = cdb->length; 6192 header_size = sizeof(struct scsi_mode_header_6); 6193 break; 6194 } 6195 case MODE_SELECT_10: { 6196 struct scsi_mode_select_10 *cdb; 6197 6198 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6199 6200 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6201 param_len = scsi_2btoul(cdb->length); 6202 header_size = sizeof(struct scsi_mode_header_10); 6203 break; 6204 } 6205 default: 6206 ctl_set_invalid_opcode(ctsio); 6207 ctl_done((union ctl_io *)ctsio); 6208 return (CTL_RETVAL_COMPLETE); 6209 } 6210 6211 if (rtd) { 6212 if (param_len != 0) { 6213 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6214 /*command*/ 1, /*field*/ 0, 6215 /*bit_valid*/ 0, /*bit*/ 0); 6216 ctl_done((union ctl_io *)ctsio); 6217 return (CTL_RETVAL_COMPLETE); 6218 } 6219 6220 /* Revert to defaults. */ 6221 ctl_init_page_index(lun); 6222 mtx_lock(&lun->lun_lock); 6223 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6224 mtx_unlock(&lun->lun_lock); 6225 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6226 ctl_isc_announce_mode(lun, -1, 6227 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6228 lun->mode_pages.index[i].subpage); 6229 } 6230 ctl_set_success(ctsio); 6231 ctl_done((union ctl_io *)ctsio); 6232 return (CTL_RETVAL_COMPLETE); 6233 } 6234 6235 /* 6236 * From SPC-3: 6237 * "A parameter list length of zero indicates that the Data-Out Buffer 6238 * shall be empty. This condition shall not be considered as an error." 6239 */ 6240 if (param_len == 0) { 6241 ctl_set_success(ctsio); 6242 ctl_done((union ctl_io *)ctsio); 6243 return (CTL_RETVAL_COMPLETE); 6244 } 6245 6246 /* 6247 * Since we'll hit this the first time through, prior to 6248 * allocation, we don't need to free a data buffer here. 6249 */ 6250 if (param_len < header_size) { 6251 ctl_set_param_len_error(ctsio); 6252 ctl_done((union ctl_io *)ctsio); 6253 return (CTL_RETVAL_COMPLETE); 6254 } 6255 6256 /* 6257 * Allocate the data buffer and grab the user's data. In theory, 6258 * we shouldn't have to sanity check the parameter list length here 6259 * because the maximum size is 64K. We should be able to malloc 6260 * that much without too many problems. 6261 */ 6262 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6263 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6264 ctsio->kern_data_len = param_len; 6265 ctsio->kern_total_len = param_len; 6266 ctsio->kern_rel_offset = 0; 6267 ctsio->kern_sg_entries = 0; 6268 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6269 ctsio->be_move_done = ctl_config_move_done; 6270 ctl_datamove((union ctl_io *)ctsio); 6271 6272 return (CTL_RETVAL_COMPLETE); 6273 } 6274 6275 switch (ctsio->cdb[0]) { 6276 case MODE_SELECT_6: { 6277 struct scsi_mode_header_6 *mh6; 6278 6279 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6280 bd_len = mh6->blk_desc_len; 6281 break; 6282 } 6283 case MODE_SELECT_10: { 6284 struct scsi_mode_header_10 *mh10; 6285 6286 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6287 bd_len = scsi_2btoul(mh10->blk_desc_len); 6288 break; 6289 } 6290 default: 6291 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6292 } 6293 6294 if (param_len < (header_size + bd_len)) { 6295 free(ctsio->kern_data_ptr, M_CTL); 6296 ctl_set_param_len_error(ctsio); 6297 ctl_done((union ctl_io *)ctsio); 6298 return (CTL_RETVAL_COMPLETE); 6299 } 6300 6301 /* 6302 * Set the IO_CONT flag, so that if this I/O gets passed to 6303 * ctl_config_write_done(), it'll get passed back to 6304 * ctl_do_mode_select() for further processing, or completion if 6305 * we're all done. 6306 */ 6307 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6308 ctsio->io_cont = ctl_do_mode_select; 6309 6310 modepage_info = (union ctl_modepage_info *) 6311 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6312 memset(modepage_info, 0, sizeof(*modepage_info)); 6313 modepage_info->header.len_left = param_len - header_size - bd_len; 6314 modepage_info->header.len_used = header_size + bd_len; 6315 6316 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6317 } 6318 6319 int 6320 ctl_mode_sense(struct ctl_scsiio *ctsio) 6321 { 6322 struct ctl_lun *lun = CTL_LUN(ctsio); 6323 int pc, page_code, llba, subpage; 6324 int alloc_len, page_len, header_len, bd_len, total_len; 6325 void *block_desc; 6326 struct ctl_page_index *page_index; 6327 6328 llba = 0; 6329 6330 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6331 6332 switch (ctsio->cdb[0]) { 6333 case MODE_SENSE_6: { 6334 struct scsi_mode_sense_6 *cdb; 6335 6336 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6337 6338 header_len = sizeof(struct scsi_mode_hdr_6); 6339 if (cdb->byte2 & SMS_DBD) 6340 bd_len = 0; 6341 else 6342 bd_len = sizeof(struct scsi_mode_block_descr); 6343 header_len += bd_len; 6344 6345 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6346 page_code = cdb->page & SMS_PAGE_CODE; 6347 subpage = cdb->subpage; 6348 alloc_len = cdb->length; 6349 break; 6350 } 6351 case MODE_SENSE_10: { 6352 struct scsi_mode_sense_10 *cdb; 6353 6354 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6355 6356 header_len = sizeof(struct scsi_mode_hdr_10); 6357 if (cdb->byte2 & SMS_DBD) { 6358 bd_len = 0; 6359 } else if (lun->be_lun->lun_type == T_DIRECT) { 6360 if (cdb->byte2 & SMS10_LLBAA) { 6361 llba = 1; 6362 bd_len = sizeof(struct scsi_mode_block_descr_dlong); 6363 } else 6364 bd_len = sizeof(struct scsi_mode_block_descr_dshort); 6365 } else 6366 bd_len = sizeof(struct scsi_mode_block_descr); 6367 header_len += bd_len; 6368 6369 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6370 page_code = cdb->page & SMS_PAGE_CODE; 6371 subpage = cdb->subpage; 6372 alloc_len = scsi_2btoul(cdb->length); 6373 break; 6374 } 6375 default: 6376 ctl_set_invalid_opcode(ctsio); 6377 ctl_done((union ctl_io *)ctsio); 6378 return (CTL_RETVAL_COMPLETE); 6379 break; /* NOTREACHED */ 6380 } 6381 6382 /* 6383 * We have to make a first pass through to calculate the size of 6384 * the pages that match the user's query. Then we allocate enough 6385 * memory to hold it, and actually copy the data into the buffer. 6386 */ 6387 switch (page_code) { 6388 case SMS_ALL_PAGES_PAGE: { 6389 u_int i; 6390 6391 page_len = 0; 6392 6393 /* 6394 * At the moment, values other than 0 and 0xff here are 6395 * reserved according to SPC-3. 6396 */ 6397 if ((subpage != SMS_SUBPAGE_PAGE_0) 6398 && (subpage != SMS_SUBPAGE_ALL)) { 6399 ctl_set_invalid_field(ctsio, 6400 /*sks_valid*/ 1, 6401 /*command*/ 1, 6402 /*field*/ 3, 6403 /*bit_valid*/ 0, 6404 /*bit*/ 0); 6405 ctl_done((union ctl_io *)ctsio); 6406 return (CTL_RETVAL_COMPLETE); 6407 } 6408 6409 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6410 page_index = &lun->mode_pages.index[i]; 6411 6412 /* Make sure the page is supported for this dev type */ 6413 if (lun->be_lun->lun_type == T_DIRECT && 6414 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6415 continue; 6416 if (lun->be_lun->lun_type == T_PROCESSOR && 6417 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6418 continue; 6419 if (lun->be_lun->lun_type == T_CDROM && 6420 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6421 continue; 6422 6423 /* 6424 * We don't use this subpage if the user didn't 6425 * request all subpages. 6426 */ 6427 if ((page_index->subpage != 0) 6428 && (subpage == SMS_SUBPAGE_PAGE_0)) 6429 continue; 6430 6431 page_len += page_index->page_len; 6432 } 6433 break; 6434 } 6435 default: { 6436 u_int i; 6437 6438 page_len = 0; 6439 6440 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6441 page_index = &lun->mode_pages.index[i]; 6442 6443 /* Make sure the page is supported for this dev type */ 6444 if (lun->be_lun->lun_type == T_DIRECT && 6445 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6446 continue; 6447 if (lun->be_lun->lun_type == T_PROCESSOR && 6448 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6449 continue; 6450 if (lun->be_lun->lun_type == T_CDROM && 6451 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6452 continue; 6453 6454 /* Look for the right page code */ 6455 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6456 continue; 6457 6458 /* Look for the right subpage or the subpage wildcard*/ 6459 if ((page_index->subpage != subpage) 6460 && (subpage != SMS_SUBPAGE_ALL)) 6461 continue; 6462 6463 page_len += page_index->page_len; 6464 } 6465 6466 if (page_len == 0) { 6467 ctl_set_invalid_field(ctsio, 6468 /*sks_valid*/ 1, 6469 /*command*/ 1, 6470 /*field*/ 2, 6471 /*bit_valid*/ 1, 6472 /*bit*/ 5); 6473 ctl_done((union ctl_io *)ctsio); 6474 return (CTL_RETVAL_COMPLETE); 6475 } 6476 break; 6477 } 6478 } 6479 6480 total_len = header_len + page_len; 6481 6482 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6483 ctsio->kern_sg_entries = 0; 6484 ctsio->kern_rel_offset = 0; 6485 ctsio->kern_data_len = min(total_len, alloc_len); 6486 ctsio->kern_total_len = ctsio->kern_data_len; 6487 6488 switch (ctsio->cdb[0]) { 6489 case MODE_SENSE_6: { 6490 struct scsi_mode_hdr_6 *header; 6491 6492 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6493 6494 header->datalen = MIN(total_len - 1, 254); 6495 if (lun->be_lun->lun_type == T_DIRECT) { 6496 header->dev_specific = 0x10; /* DPOFUA */ 6497 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6498 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6499 header->dev_specific |= 0x80; /* WP */ 6500 } 6501 header->block_descr_len = bd_len; 6502 block_desc = &header[1]; 6503 break; 6504 } 6505 case MODE_SENSE_10: { 6506 struct scsi_mode_hdr_10 *header; 6507 int datalen; 6508 6509 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6510 6511 datalen = MIN(total_len - 2, 65533); 6512 scsi_ulto2b(datalen, header->datalen); 6513 if (lun->be_lun->lun_type == T_DIRECT) { 6514 header->dev_specific = 0x10; /* DPOFUA */ 6515 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6516 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6517 header->dev_specific |= 0x80; /* WP */ 6518 } 6519 if (llba) 6520 header->flags |= SMH_LONGLBA; 6521 scsi_ulto2b(bd_len, header->block_descr_len); 6522 block_desc = &header[1]; 6523 break; 6524 } 6525 default: 6526 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6527 } 6528 6529 /* 6530 * If we've got a disk, use its blocksize in the block 6531 * descriptor. Otherwise, just set it to 0. 6532 */ 6533 if (bd_len > 0) { 6534 if (lun->be_lun->lun_type == T_DIRECT) { 6535 if (llba) { 6536 struct scsi_mode_block_descr_dlong *bd = block_desc; 6537 if (lun->be_lun->maxlba != 0) 6538 scsi_u64to8b(lun->be_lun->maxlba + 1, 6539 bd->num_blocks); 6540 scsi_ulto4b(lun->be_lun->blocksize, 6541 bd->block_len); 6542 } else { 6543 struct scsi_mode_block_descr_dshort *bd = block_desc; 6544 if (lun->be_lun->maxlba != 0) 6545 scsi_ulto4b(MIN(lun->be_lun->maxlba+1, 6546 UINT32_MAX), bd->num_blocks); 6547 scsi_ulto3b(lun->be_lun->blocksize, 6548 bd->block_len); 6549 } 6550 } else { 6551 struct scsi_mode_block_descr *bd = block_desc; 6552 scsi_ulto3b(0, bd->block_len); 6553 } 6554 } 6555 6556 switch (page_code) { 6557 case SMS_ALL_PAGES_PAGE: { 6558 int i, data_used; 6559 6560 data_used = header_len; 6561 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6562 struct ctl_page_index *page_index; 6563 6564 page_index = &lun->mode_pages.index[i]; 6565 if (lun->be_lun->lun_type == T_DIRECT && 6566 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6567 continue; 6568 if (lun->be_lun->lun_type == T_PROCESSOR && 6569 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6570 continue; 6571 if (lun->be_lun->lun_type == T_CDROM && 6572 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6573 continue; 6574 6575 /* 6576 * We don't use this subpage if the user didn't 6577 * request all subpages. We already checked (above) 6578 * to make sure the user only specified a subpage 6579 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6580 */ 6581 if ((page_index->subpage != 0) 6582 && (subpage == SMS_SUBPAGE_PAGE_0)) 6583 continue; 6584 6585 /* 6586 * Call the handler, if it exists, to update the 6587 * page to the latest values. 6588 */ 6589 if (page_index->sense_handler != NULL) 6590 page_index->sense_handler(ctsio, page_index,pc); 6591 6592 memcpy(ctsio->kern_data_ptr + data_used, 6593 page_index->page_data + 6594 (page_index->page_len * pc), 6595 page_index->page_len); 6596 data_used += page_index->page_len; 6597 } 6598 break; 6599 } 6600 default: { 6601 int i, data_used; 6602 6603 data_used = header_len; 6604 6605 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6606 struct ctl_page_index *page_index; 6607 6608 page_index = &lun->mode_pages.index[i]; 6609 6610 /* Look for the right page code */ 6611 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6612 continue; 6613 6614 /* Look for the right subpage or the subpage wildcard*/ 6615 if ((page_index->subpage != subpage) 6616 && (subpage != SMS_SUBPAGE_ALL)) 6617 continue; 6618 6619 /* Make sure the page is supported for this dev type */ 6620 if (lun->be_lun->lun_type == T_DIRECT && 6621 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6622 continue; 6623 if (lun->be_lun->lun_type == T_PROCESSOR && 6624 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6625 continue; 6626 if (lun->be_lun->lun_type == T_CDROM && 6627 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6628 continue; 6629 6630 /* 6631 * Call the handler, if it exists, to update the 6632 * page to the latest values. 6633 */ 6634 if (page_index->sense_handler != NULL) 6635 page_index->sense_handler(ctsio, page_index,pc); 6636 6637 memcpy(ctsio->kern_data_ptr + data_used, 6638 page_index->page_data + 6639 (page_index->page_len * pc), 6640 page_index->page_len); 6641 data_used += page_index->page_len; 6642 } 6643 break; 6644 } 6645 } 6646 6647 ctl_set_success(ctsio); 6648 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6649 ctsio->be_move_done = ctl_config_move_done; 6650 ctl_datamove((union ctl_io *)ctsio); 6651 return (CTL_RETVAL_COMPLETE); 6652 } 6653 6654 int 6655 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio, 6656 struct ctl_page_index *page_index, 6657 int pc) 6658 { 6659 struct ctl_lun *lun = CTL_LUN(ctsio); 6660 struct scsi_log_temperature *data; 6661 const char *value; 6662 6663 data = (struct scsi_log_temperature *)page_index->page_data; 6664 6665 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code); 6666 data->hdr.param_control = SLP_LBIN; 6667 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6668 sizeof(struct scsi_log_param_header); 6669 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6670 NULL)) != NULL) 6671 data->temperature = strtol(value, NULL, 0); 6672 else 6673 data->temperature = 0xff; 6674 data++; 6675 6676 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code); 6677 data->hdr.param_control = SLP_LBIN; 6678 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6679 sizeof(struct scsi_log_param_header); 6680 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature", 6681 NULL)) != NULL) 6682 data->temperature = strtol(value, NULL, 0); 6683 else 6684 data->temperature = 0xff; 6685 return (0); 6686 } 6687 6688 int 6689 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6690 struct ctl_page_index *page_index, 6691 int pc) 6692 { 6693 struct ctl_lun *lun = CTL_LUN(ctsio); 6694 struct scsi_log_param_header *phdr; 6695 uint8_t *data; 6696 uint64_t val; 6697 6698 data = page_index->page_data; 6699 6700 if (lun->backend->lun_attr != NULL && 6701 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail")) 6702 != UINT64_MAX) { 6703 phdr = (struct scsi_log_param_header *)data; 6704 scsi_ulto2b(0x0001, phdr->param_code); 6705 phdr->param_control = SLP_LBIN | SLP_LP; 6706 phdr->param_len = 8; 6707 data = (uint8_t *)(phdr + 1); 6708 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6709 data[4] = 0x02; /* per-pool */ 6710 data += phdr->param_len; 6711 } 6712 6713 if (lun->backend->lun_attr != NULL && 6714 (val = lun->backend->lun_attr(lun->be_lun, "blocksused")) 6715 != UINT64_MAX) { 6716 phdr = (struct scsi_log_param_header *)data; 6717 scsi_ulto2b(0x0002, phdr->param_code); 6718 phdr->param_control = SLP_LBIN | SLP_LP; 6719 phdr->param_len = 8; 6720 data = (uint8_t *)(phdr + 1); 6721 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6722 data[4] = 0x01; /* per-LUN */ 6723 data += phdr->param_len; 6724 } 6725 6726 if (lun->backend->lun_attr != NULL && 6727 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail")) 6728 != UINT64_MAX) { 6729 phdr = (struct scsi_log_param_header *)data; 6730 scsi_ulto2b(0x00f1, phdr->param_code); 6731 phdr->param_control = SLP_LBIN | SLP_LP; 6732 phdr->param_len = 8; 6733 data = (uint8_t *)(phdr + 1); 6734 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6735 data[4] = 0x02; /* per-pool */ 6736 data += phdr->param_len; 6737 } 6738 6739 if (lun->backend->lun_attr != NULL && 6740 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused")) 6741 != UINT64_MAX) { 6742 phdr = (struct scsi_log_param_header *)data; 6743 scsi_ulto2b(0x00f2, phdr->param_code); 6744 phdr->param_control = SLP_LBIN | SLP_LP; 6745 phdr->param_len = 8; 6746 data = (uint8_t *)(phdr + 1); 6747 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6748 data[4] = 0x02; /* per-pool */ 6749 data += phdr->param_len; 6750 } 6751 6752 page_index->page_len = data - page_index->page_data; 6753 return (0); 6754 } 6755 6756 int 6757 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6758 struct ctl_page_index *page_index, 6759 int pc) 6760 { 6761 struct ctl_lun *lun = CTL_LUN(ctsio); 6762 struct stat_page *data; 6763 struct bintime *t; 6764 6765 data = (struct stat_page *)page_index->page_data; 6766 6767 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6768 data->sap.hdr.param_control = SLP_LBIN; 6769 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6770 sizeof(struct scsi_log_param_header); 6771 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6772 data->sap.read_num); 6773 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6774 data->sap.write_num); 6775 if (lun->be_lun->blocksize > 0) { 6776 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6777 lun->be_lun->blocksize, data->sap.recvieved_lba); 6778 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6779 lun->be_lun->blocksize, data->sap.transmitted_lba); 6780 } 6781 t = &lun->stats.time[CTL_STATS_READ]; 6782 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6783 data->sap.read_int); 6784 t = &lun->stats.time[CTL_STATS_WRITE]; 6785 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6786 data->sap.write_int); 6787 scsi_u64to8b(0, data->sap.weighted_num); 6788 scsi_u64to8b(0, data->sap.weighted_int); 6789 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6790 data->it.hdr.param_control = SLP_LBIN; 6791 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6792 sizeof(struct scsi_log_param_header); 6793 #ifdef CTL_TIME_IO 6794 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6795 #endif 6796 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6797 data->it.hdr.param_control = SLP_LBIN; 6798 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6799 sizeof(struct scsi_log_param_header); 6800 scsi_ulto4b(3, data->ti.exponent); 6801 scsi_ulto4b(1, data->ti.integer); 6802 return (0); 6803 } 6804 6805 int 6806 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6807 struct ctl_page_index *page_index, 6808 int pc) 6809 { 6810 struct ctl_lun *lun = CTL_LUN(ctsio); 6811 struct scsi_log_informational_exceptions *data; 6812 const char *value; 6813 6814 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6815 6816 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6817 data->hdr.param_control = SLP_LBIN; 6818 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6819 sizeof(struct scsi_log_param_header); 6820 data->ie_asc = lun->ie_asc; 6821 data->ie_ascq = lun->ie_ascq; 6822 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6823 NULL)) != NULL) 6824 data->temperature = strtol(value, NULL, 0); 6825 else 6826 data->temperature = 0xff; 6827 return (0); 6828 } 6829 6830 int 6831 ctl_log_sense(struct ctl_scsiio *ctsio) 6832 { 6833 struct ctl_lun *lun = CTL_LUN(ctsio); 6834 int i, pc, page_code, subpage; 6835 int alloc_len, total_len; 6836 struct ctl_page_index *page_index; 6837 struct scsi_log_sense *cdb; 6838 struct scsi_log_header *header; 6839 6840 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6841 6842 cdb = (struct scsi_log_sense *)ctsio->cdb; 6843 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6844 page_code = cdb->page & SLS_PAGE_CODE; 6845 subpage = cdb->subpage; 6846 alloc_len = scsi_2btoul(cdb->length); 6847 6848 page_index = NULL; 6849 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6850 page_index = &lun->log_pages.index[i]; 6851 6852 /* Look for the right page code */ 6853 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6854 continue; 6855 6856 /* Look for the right subpage or the subpage wildcard*/ 6857 if (page_index->subpage != subpage) 6858 continue; 6859 6860 break; 6861 } 6862 if (i >= CTL_NUM_LOG_PAGES) { 6863 ctl_set_invalid_field(ctsio, 6864 /*sks_valid*/ 1, 6865 /*command*/ 1, 6866 /*field*/ 2, 6867 /*bit_valid*/ 0, 6868 /*bit*/ 0); 6869 ctl_done((union ctl_io *)ctsio); 6870 return (CTL_RETVAL_COMPLETE); 6871 } 6872 6873 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6874 6875 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6876 ctsio->kern_sg_entries = 0; 6877 ctsio->kern_rel_offset = 0; 6878 ctsio->kern_data_len = min(total_len, alloc_len); 6879 ctsio->kern_total_len = ctsio->kern_data_len; 6880 6881 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6882 header->page = page_index->page_code; 6883 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6884 header->page |= SL_DS; 6885 if (page_index->subpage) { 6886 header->page |= SL_SPF; 6887 header->subpage = page_index->subpage; 6888 } 6889 scsi_ulto2b(page_index->page_len, header->datalen); 6890 6891 /* 6892 * Call the handler, if it exists, to update the 6893 * page to the latest values. 6894 */ 6895 if (page_index->sense_handler != NULL) 6896 page_index->sense_handler(ctsio, page_index, pc); 6897 6898 memcpy(header + 1, page_index->page_data, page_index->page_len); 6899 6900 ctl_set_success(ctsio); 6901 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6902 ctsio->be_move_done = ctl_config_move_done; 6903 ctl_datamove((union ctl_io *)ctsio); 6904 return (CTL_RETVAL_COMPLETE); 6905 } 6906 6907 int 6908 ctl_read_capacity(struct ctl_scsiio *ctsio) 6909 { 6910 struct ctl_lun *lun = CTL_LUN(ctsio); 6911 struct scsi_read_capacity *cdb; 6912 struct scsi_read_capacity_data *data; 6913 uint32_t lba; 6914 6915 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6916 6917 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6918 6919 lba = scsi_4btoul(cdb->addr); 6920 if (((cdb->pmi & SRC_PMI) == 0) 6921 && (lba != 0)) { 6922 ctl_set_invalid_field(/*ctsio*/ ctsio, 6923 /*sks_valid*/ 1, 6924 /*command*/ 1, 6925 /*field*/ 2, 6926 /*bit_valid*/ 0, 6927 /*bit*/ 0); 6928 ctl_done((union ctl_io *)ctsio); 6929 return (CTL_RETVAL_COMPLETE); 6930 } 6931 6932 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6933 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6934 ctsio->kern_data_len = sizeof(*data); 6935 ctsio->kern_total_len = sizeof(*data); 6936 ctsio->kern_rel_offset = 0; 6937 ctsio->kern_sg_entries = 0; 6938 6939 /* 6940 * If the maximum LBA is greater than 0xfffffffe, the user must 6941 * issue a SERVICE ACTION IN (16) command, with the read capacity 6942 * serivce action set. 6943 */ 6944 if (lun->be_lun->maxlba > 0xfffffffe) 6945 scsi_ulto4b(0xffffffff, data->addr); 6946 else 6947 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6948 6949 /* 6950 * XXX KDM this may not be 512 bytes... 6951 */ 6952 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6953 6954 ctl_set_success(ctsio); 6955 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6956 ctsio->be_move_done = ctl_config_move_done; 6957 ctl_datamove((union ctl_io *)ctsio); 6958 return (CTL_RETVAL_COMPLETE); 6959 } 6960 6961 int 6962 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6963 { 6964 struct ctl_lun *lun = CTL_LUN(ctsio); 6965 struct scsi_read_capacity_16 *cdb; 6966 struct scsi_read_capacity_data_long *data; 6967 uint64_t lba; 6968 uint32_t alloc_len; 6969 6970 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6971 6972 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6973 6974 alloc_len = scsi_4btoul(cdb->alloc_len); 6975 lba = scsi_8btou64(cdb->addr); 6976 6977 if ((cdb->reladr & SRC16_PMI) 6978 && (lba != 0)) { 6979 ctl_set_invalid_field(/*ctsio*/ ctsio, 6980 /*sks_valid*/ 1, 6981 /*command*/ 1, 6982 /*field*/ 2, 6983 /*bit_valid*/ 0, 6984 /*bit*/ 0); 6985 ctl_done((union ctl_io *)ctsio); 6986 return (CTL_RETVAL_COMPLETE); 6987 } 6988 6989 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6990 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6991 ctsio->kern_rel_offset = 0; 6992 ctsio->kern_sg_entries = 0; 6993 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 6994 ctsio->kern_total_len = ctsio->kern_data_len; 6995 6996 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6997 /* XXX KDM this may not be 512 bytes... */ 6998 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6999 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7000 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7001 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7002 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7003 7004 ctl_set_success(ctsio); 7005 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7006 ctsio->be_move_done = ctl_config_move_done; 7007 ctl_datamove((union ctl_io *)ctsio); 7008 return (CTL_RETVAL_COMPLETE); 7009 } 7010 7011 int 7012 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7013 { 7014 struct ctl_lun *lun = CTL_LUN(ctsio); 7015 struct scsi_get_lba_status *cdb; 7016 struct scsi_get_lba_status_data *data; 7017 struct ctl_lba_len_flags *lbalen; 7018 uint64_t lba; 7019 uint32_t alloc_len, total_len; 7020 int retval; 7021 7022 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7023 7024 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7025 lba = scsi_8btou64(cdb->addr); 7026 alloc_len = scsi_4btoul(cdb->alloc_len); 7027 7028 if (lba > lun->be_lun->maxlba) { 7029 ctl_set_lba_out_of_range(ctsio, lba); 7030 ctl_done((union ctl_io *)ctsio); 7031 return (CTL_RETVAL_COMPLETE); 7032 } 7033 7034 total_len = sizeof(*data) + sizeof(data->descr[0]); 7035 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7036 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7037 ctsio->kern_rel_offset = 0; 7038 ctsio->kern_sg_entries = 0; 7039 ctsio->kern_data_len = min(total_len, alloc_len); 7040 ctsio->kern_total_len = ctsio->kern_data_len; 7041 7042 /* Fill dummy data in case backend can't tell anything. */ 7043 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7044 scsi_u64to8b(lba, data->descr[0].addr); 7045 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7046 data->descr[0].length); 7047 data->descr[0].status = 0; /* Mapped or unknown. */ 7048 7049 ctl_set_success(ctsio); 7050 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7051 ctsio->be_move_done = ctl_config_move_done; 7052 7053 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7054 lbalen->lba = lba; 7055 lbalen->len = total_len; 7056 lbalen->flags = 0; 7057 retval = lun->backend->config_read((union ctl_io *)ctsio); 7058 return (retval); 7059 } 7060 7061 int 7062 ctl_read_defect(struct ctl_scsiio *ctsio) 7063 { 7064 struct scsi_read_defect_data_10 *ccb10; 7065 struct scsi_read_defect_data_12 *ccb12; 7066 struct scsi_read_defect_data_hdr_10 *data10; 7067 struct scsi_read_defect_data_hdr_12 *data12; 7068 uint32_t alloc_len, data_len; 7069 uint8_t format; 7070 7071 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7072 7073 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7074 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7075 format = ccb10->format; 7076 alloc_len = scsi_2btoul(ccb10->alloc_length); 7077 data_len = sizeof(*data10); 7078 } else { 7079 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7080 format = ccb12->format; 7081 alloc_len = scsi_4btoul(ccb12->alloc_length); 7082 data_len = sizeof(*data12); 7083 } 7084 if (alloc_len == 0) { 7085 ctl_set_success(ctsio); 7086 ctl_done((union ctl_io *)ctsio); 7087 return (CTL_RETVAL_COMPLETE); 7088 } 7089 7090 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7091 ctsio->kern_rel_offset = 0; 7092 ctsio->kern_sg_entries = 0; 7093 ctsio->kern_data_len = min(data_len, alloc_len); 7094 ctsio->kern_total_len = ctsio->kern_data_len; 7095 7096 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7097 data10 = (struct scsi_read_defect_data_hdr_10 *) 7098 ctsio->kern_data_ptr; 7099 data10->format = format; 7100 scsi_ulto2b(0, data10->length); 7101 } else { 7102 data12 = (struct scsi_read_defect_data_hdr_12 *) 7103 ctsio->kern_data_ptr; 7104 data12->format = format; 7105 scsi_ulto2b(0, data12->generation); 7106 scsi_ulto4b(0, data12->length); 7107 } 7108 7109 ctl_set_success(ctsio); 7110 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7111 ctsio->be_move_done = ctl_config_move_done; 7112 ctl_datamove((union ctl_io *)ctsio); 7113 return (CTL_RETVAL_COMPLETE); 7114 } 7115 7116 int 7117 ctl_report_ident_info(struct ctl_scsiio *ctsio) 7118 { 7119 struct ctl_lun *lun = CTL_LUN(ctsio); 7120 struct scsi_report_ident_info *cdb; 7121 struct scsi_report_ident_info_data *rii_ptr; 7122 struct scsi_report_ident_info_descr *riid_ptr; 7123 const char *oii, *otii; 7124 int retval, alloc_len, total_len = 0, len = 0; 7125 7126 CTL_DEBUG_PRINT(("ctl_report_ident_info\n")); 7127 7128 cdb = (struct scsi_report_ident_info *)ctsio->cdb; 7129 retval = CTL_RETVAL_COMPLETE; 7130 7131 total_len = sizeof(struct scsi_report_ident_info_data); 7132 switch (cdb->type) { 7133 case RII_LUII: 7134 oii = dnvlist_get_string(lun->be_lun->options, 7135 "ident_info", NULL); 7136 if (oii) 7137 len = strlen(oii); /* Approximately */ 7138 break; 7139 case RII_LUTII: 7140 otii = dnvlist_get_string(lun->be_lun->options, 7141 "text_ident_info", NULL); 7142 if (otii) 7143 len = strlen(otii) + 1; /* NULL-terminated */ 7144 break; 7145 case RII_IIS: 7146 len = 2 * sizeof(struct scsi_report_ident_info_descr); 7147 break; 7148 default: 7149 ctl_set_invalid_field(/*ctsio*/ ctsio, 7150 /*sks_valid*/ 1, 7151 /*command*/ 1, 7152 /*field*/ 11, 7153 /*bit_valid*/ 1, 7154 /*bit*/ 2); 7155 ctl_done((union ctl_io *)ctsio); 7156 return(retval); 7157 } 7158 total_len += len; 7159 alloc_len = scsi_4btoul(cdb->length); 7160 7161 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7162 ctsio->kern_sg_entries = 0; 7163 ctsio->kern_rel_offset = 0; 7164 ctsio->kern_data_len = min(total_len, alloc_len); 7165 ctsio->kern_total_len = ctsio->kern_data_len; 7166 7167 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr; 7168 switch (cdb->type) { 7169 case RII_LUII: 7170 if (oii) { 7171 if (oii[0] == '0' && oii[1] == 'x') 7172 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len); 7173 else 7174 strncpy((uint8_t *)(rii_ptr + 1), oii, len); 7175 } 7176 break; 7177 case RII_LUTII: 7178 if (otii) 7179 strlcpy((uint8_t *)(rii_ptr + 1), otii, len); 7180 break; 7181 case RII_IIS: 7182 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1); 7183 riid_ptr->type = RII_LUII; 7184 scsi_ulto2b(0xffff, riid_ptr->length); 7185 riid_ptr++; 7186 riid_ptr->type = RII_LUTII; 7187 scsi_ulto2b(0xffff, riid_ptr->length); 7188 } 7189 scsi_ulto2b(len, rii_ptr->length); 7190 7191 ctl_set_success(ctsio); 7192 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7193 ctsio->be_move_done = ctl_config_move_done; 7194 ctl_datamove((union ctl_io *)ctsio); 7195 return(retval); 7196 } 7197 7198 int 7199 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7200 { 7201 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7202 struct ctl_lun *lun = CTL_LUN(ctsio); 7203 struct scsi_maintenance_in *cdb; 7204 int retval; 7205 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7206 int num_ha_groups, num_target_ports, shared_group; 7207 struct ctl_port *port; 7208 struct scsi_target_group_data *rtg_ptr; 7209 struct scsi_target_group_data_extended *rtg_ext_ptr; 7210 struct scsi_target_port_group_descriptor *tpg_desc; 7211 7212 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7213 7214 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7215 retval = CTL_RETVAL_COMPLETE; 7216 7217 switch (cdb->byte2 & STG_PDF_MASK) { 7218 case STG_PDF_LENGTH: 7219 ext = 0; 7220 break; 7221 case STG_PDF_EXTENDED: 7222 ext = 1; 7223 break; 7224 default: 7225 ctl_set_invalid_field(/*ctsio*/ ctsio, 7226 /*sks_valid*/ 1, 7227 /*command*/ 1, 7228 /*field*/ 2, 7229 /*bit_valid*/ 1, 7230 /*bit*/ 5); 7231 ctl_done((union ctl_io *)ctsio); 7232 return(retval); 7233 } 7234 7235 num_target_ports = 0; 7236 shared_group = (softc->is_single != 0); 7237 mtx_lock(&softc->ctl_lock); 7238 STAILQ_FOREACH(port, &softc->port_list, links) { 7239 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7240 continue; 7241 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7242 continue; 7243 num_target_ports++; 7244 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7245 shared_group = 1; 7246 } 7247 mtx_unlock(&softc->ctl_lock); 7248 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7249 7250 if (ext) 7251 total_len = sizeof(struct scsi_target_group_data_extended); 7252 else 7253 total_len = sizeof(struct scsi_target_group_data); 7254 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7255 (shared_group + num_ha_groups) + 7256 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7257 7258 alloc_len = scsi_4btoul(cdb->length); 7259 7260 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7261 ctsio->kern_sg_entries = 0; 7262 ctsio->kern_rel_offset = 0; 7263 ctsio->kern_data_len = min(total_len, alloc_len); 7264 ctsio->kern_total_len = ctsio->kern_data_len; 7265 7266 if (ext) { 7267 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7268 ctsio->kern_data_ptr; 7269 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7270 rtg_ext_ptr->format_type = 0x10; 7271 rtg_ext_ptr->implicit_transition_time = 0; 7272 tpg_desc = &rtg_ext_ptr->groups[0]; 7273 } else { 7274 rtg_ptr = (struct scsi_target_group_data *) 7275 ctsio->kern_data_ptr; 7276 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7277 tpg_desc = &rtg_ptr->groups[0]; 7278 } 7279 7280 mtx_lock(&softc->ctl_lock); 7281 pg = softc->port_min / softc->port_cnt; 7282 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7283 /* Some shelf is known to be primary. */ 7284 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7285 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7286 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7287 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7288 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7289 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7290 else 7291 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7292 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7293 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7294 } else { 7295 ts = os; 7296 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7297 } 7298 } else { 7299 /* No known primary shelf. */ 7300 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7301 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7302 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7303 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7304 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7305 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7306 } else { 7307 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7308 } 7309 } 7310 if (shared_group) { 7311 tpg_desc->pref_state = ts; 7312 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7313 TPG_U_SUP | TPG_T_SUP; 7314 scsi_ulto2b(1, tpg_desc->target_port_group); 7315 tpg_desc->status = TPG_IMPLICIT; 7316 pc = 0; 7317 STAILQ_FOREACH(port, &softc->port_list, links) { 7318 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7319 continue; 7320 if (!softc->is_single && 7321 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7322 continue; 7323 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7324 continue; 7325 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7326 relative_target_port_identifier); 7327 pc++; 7328 } 7329 tpg_desc->target_port_count = pc; 7330 tpg_desc = (struct scsi_target_port_group_descriptor *) 7331 &tpg_desc->descriptors[pc]; 7332 } 7333 for (g = 0; g < num_ha_groups; g++) { 7334 tpg_desc->pref_state = (g == pg) ? ts : os; 7335 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7336 TPG_U_SUP | TPG_T_SUP; 7337 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7338 tpg_desc->status = TPG_IMPLICIT; 7339 pc = 0; 7340 STAILQ_FOREACH(port, &softc->port_list, links) { 7341 if (port->targ_port < g * softc->port_cnt || 7342 port->targ_port >= (g + 1) * softc->port_cnt) 7343 continue; 7344 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7345 continue; 7346 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7347 continue; 7348 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7349 continue; 7350 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7351 relative_target_port_identifier); 7352 pc++; 7353 } 7354 tpg_desc->target_port_count = pc; 7355 tpg_desc = (struct scsi_target_port_group_descriptor *) 7356 &tpg_desc->descriptors[pc]; 7357 } 7358 mtx_unlock(&softc->ctl_lock); 7359 7360 ctl_set_success(ctsio); 7361 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7362 ctsio->be_move_done = ctl_config_move_done; 7363 ctl_datamove((union ctl_io *)ctsio); 7364 return(retval); 7365 } 7366 7367 int 7368 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7369 { 7370 struct ctl_lun *lun = CTL_LUN(ctsio); 7371 struct scsi_report_supported_opcodes *cdb; 7372 const struct ctl_cmd_entry *entry, *sentry; 7373 struct scsi_report_supported_opcodes_all *all; 7374 struct scsi_report_supported_opcodes_descr *descr; 7375 struct scsi_report_supported_opcodes_one *one; 7376 int retval; 7377 int alloc_len, total_len; 7378 int opcode, service_action, i, j, num; 7379 7380 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7381 7382 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7383 retval = CTL_RETVAL_COMPLETE; 7384 7385 opcode = cdb->requested_opcode; 7386 service_action = scsi_2btoul(cdb->requested_service_action); 7387 switch (cdb->options & RSO_OPTIONS_MASK) { 7388 case RSO_OPTIONS_ALL: 7389 num = 0; 7390 for (i = 0; i < 256; i++) { 7391 entry = &ctl_cmd_table[i]; 7392 if (entry->flags & CTL_CMD_FLAG_SA5) { 7393 for (j = 0; j < 32; j++) { 7394 sentry = &((const struct ctl_cmd_entry *) 7395 entry->execute)[j]; 7396 if (ctl_cmd_applicable( 7397 lun->be_lun->lun_type, sentry)) 7398 num++; 7399 } 7400 } else { 7401 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7402 entry)) 7403 num++; 7404 } 7405 } 7406 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7407 num * sizeof(struct scsi_report_supported_opcodes_descr); 7408 break; 7409 case RSO_OPTIONS_OC: 7410 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7411 ctl_set_invalid_field(/*ctsio*/ ctsio, 7412 /*sks_valid*/ 1, 7413 /*command*/ 1, 7414 /*field*/ 2, 7415 /*bit_valid*/ 1, 7416 /*bit*/ 2); 7417 ctl_done((union ctl_io *)ctsio); 7418 return (CTL_RETVAL_COMPLETE); 7419 } 7420 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7421 break; 7422 case RSO_OPTIONS_OC_SA: 7423 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7424 service_action >= 32) { 7425 ctl_set_invalid_field(/*ctsio*/ ctsio, 7426 /*sks_valid*/ 1, 7427 /*command*/ 1, 7428 /*field*/ 2, 7429 /*bit_valid*/ 1, 7430 /*bit*/ 2); 7431 ctl_done((union ctl_io *)ctsio); 7432 return (CTL_RETVAL_COMPLETE); 7433 } 7434 /* FALLTHROUGH */ 7435 case RSO_OPTIONS_OC_ASA: 7436 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7437 break; 7438 default: 7439 ctl_set_invalid_field(/*ctsio*/ ctsio, 7440 /*sks_valid*/ 1, 7441 /*command*/ 1, 7442 /*field*/ 2, 7443 /*bit_valid*/ 1, 7444 /*bit*/ 2); 7445 ctl_done((union ctl_io *)ctsio); 7446 return (CTL_RETVAL_COMPLETE); 7447 } 7448 7449 alloc_len = scsi_4btoul(cdb->length); 7450 7451 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7452 ctsio->kern_sg_entries = 0; 7453 ctsio->kern_rel_offset = 0; 7454 ctsio->kern_data_len = min(total_len, alloc_len); 7455 ctsio->kern_total_len = ctsio->kern_data_len; 7456 7457 switch (cdb->options & RSO_OPTIONS_MASK) { 7458 case RSO_OPTIONS_ALL: 7459 all = (struct scsi_report_supported_opcodes_all *) 7460 ctsio->kern_data_ptr; 7461 num = 0; 7462 for (i = 0; i < 256; i++) { 7463 entry = &ctl_cmd_table[i]; 7464 if (entry->flags & CTL_CMD_FLAG_SA5) { 7465 for (j = 0; j < 32; j++) { 7466 sentry = &((const struct ctl_cmd_entry *) 7467 entry->execute)[j]; 7468 if (!ctl_cmd_applicable( 7469 lun->be_lun->lun_type, sentry)) 7470 continue; 7471 descr = &all->descr[num++]; 7472 descr->opcode = i; 7473 scsi_ulto2b(j, descr->service_action); 7474 descr->flags = RSO_SERVACTV; 7475 scsi_ulto2b(sentry->length, 7476 descr->cdb_length); 7477 } 7478 } else { 7479 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7480 entry)) 7481 continue; 7482 descr = &all->descr[num++]; 7483 descr->opcode = i; 7484 scsi_ulto2b(0, descr->service_action); 7485 descr->flags = 0; 7486 scsi_ulto2b(entry->length, descr->cdb_length); 7487 } 7488 } 7489 scsi_ulto4b( 7490 num * sizeof(struct scsi_report_supported_opcodes_descr), 7491 all->length); 7492 break; 7493 case RSO_OPTIONS_OC: 7494 one = (struct scsi_report_supported_opcodes_one *) 7495 ctsio->kern_data_ptr; 7496 entry = &ctl_cmd_table[opcode]; 7497 goto fill_one; 7498 case RSO_OPTIONS_OC_SA: 7499 one = (struct scsi_report_supported_opcodes_one *) 7500 ctsio->kern_data_ptr; 7501 entry = &ctl_cmd_table[opcode]; 7502 entry = &((const struct ctl_cmd_entry *) 7503 entry->execute)[service_action]; 7504 fill_one: 7505 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7506 one->support = 3; 7507 scsi_ulto2b(entry->length, one->cdb_length); 7508 one->cdb_usage[0] = opcode; 7509 memcpy(&one->cdb_usage[1], entry->usage, 7510 entry->length - 1); 7511 } else 7512 one->support = 1; 7513 break; 7514 case RSO_OPTIONS_OC_ASA: 7515 one = (struct scsi_report_supported_opcodes_one *) 7516 ctsio->kern_data_ptr; 7517 entry = &ctl_cmd_table[opcode]; 7518 if (entry->flags & CTL_CMD_FLAG_SA5) { 7519 entry = &((const struct ctl_cmd_entry *) 7520 entry->execute)[service_action]; 7521 } else if (service_action != 0) { 7522 one->support = 1; 7523 break; 7524 } 7525 goto fill_one; 7526 } 7527 7528 ctl_set_success(ctsio); 7529 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7530 ctsio->be_move_done = ctl_config_move_done; 7531 ctl_datamove((union ctl_io *)ctsio); 7532 return(retval); 7533 } 7534 7535 int 7536 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7537 { 7538 struct scsi_report_supported_tmf *cdb; 7539 struct scsi_report_supported_tmf_ext_data *data; 7540 int retval; 7541 int alloc_len, total_len; 7542 7543 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7544 7545 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7546 7547 retval = CTL_RETVAL_COMPLETE; 7548 7549 if (cdb->options & RST_REPD) 7550 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7551 else 7552 total_len = sizeof(struct scsi_report_supported_tmf_data); 7553 alloc_len = scsi_4btoul(cdb->length); 7554 7555 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7556 ctsio->kern_sg_entries = 0; 7557 ctsio->kern_rel_offset = 0; 7558 ctsio->kern_data_len = min(total_len, alloc_len); 7559 ctsio->kern_total_len = ctsio->kern_data_len; 7560 7561 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7562 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7563 RST_TRS; 7564 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7565 data->length = total_len - 4; 7566 7567 ctl_set_success(ctsio); 7568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7569 ctsio->be_move_done = ctl_config_move_done; 7570 ctl_datamove((union ctl_io *)ctsio); 7571 return (retval); 7572 } 7573 7574 int 7575 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7576 { 7577 struct scsi_report_timestamp *cdb; 7578 struct scsi_report_timestamp_data *data; 7579 struct timeval tv; 7580 int64_t timestamp; 7581 int retval; 7582 int alloc_len, total_len; 7583 7584 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7585 7586 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7587 7588 retval = CTL_RETVAL_COMPLETE; 7589 7590 total_len = sizeof(struct scsi_report_timestamp_data); 7591 alloc_len = scsi_4btoul(cdb->length); 7592 7593 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7594 ctsio->kern_sg_entries = 0; 7595 ctsio->kern_rel_offset = 0; 7596 ctsio->kern_data_len = min(total_len, alloc_len); 7597 ctsio->kern_total_len = ctsio->kern_data_len; 7598 7599 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7600 scsi_ulto2b(sizeof(*data) - 2, data->length); 7601 data->origin = RTS_ORIG_OUTSIDE; 7602 getmicrotime(&tv); 7603 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7604 scsi_ulto4b(timestamp >> 16, data->timestamp); 7605 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7606 7607 ctl_set_success(ctsio); 7608 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7609 ctsio->be_move_done = ctl_config_move_done; 7610 ctl_datamove((union ctl_io *)ctsio); 7611 return (retval); 7612 } 7613 7614 int 7615 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7616 { 7617 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7618 struct ctl_lun *lun = CTL_LUN(ctsio); 7619 struct scsi_per_res_in *cdb; 7620 int alloc_len, total_len = 0; 7621 /* struct scsi_per_res_in_rsrv in_data; */ 7622 uint64_t key; 7623 7624 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7625 7626 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7627 7628 alloc_len = scsi_2btoul(cdb->length); 7629 7630 retry: 7631 mtx_lock(&lun->lun_lock); 7632 switch (cdb->action) { 7633 case SPRI_RK: /* read keys */ 7634 total_len = sizeof(struct scsi_per_res_in_keys) + 7635 lun->pr_key_count * 7636 sizeof(struct scsi_per_res_key); 7637 break; 7638 case SPRI_RR: /* read reservation */ 7639 if (lun->flags & CTL_LUN_PR_RESERVED) 7640 total_len = sizeof(struct scsi_per_res_in_rsrv); 7641 else 7642 total_len = sizeof(struct scsi_per_res_in_header); 7643 break; 7644 case SPRI_RC: /* report capabilities */ 7645 total_len = sizeof(struct scsi_per_res_cap); 7646 break; 7647 case SPRI_RS: /* read full status */ 7648 total_len = sizeof(struct scsi_per_res_in_header) + 7649 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7650 lun->pr_key_count; 7651 break; 7652 default: 7653 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7654 } 7655 mtx_unlock(&lun->lun_lock); 7656 7657 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7658 ctsio->kern_rel_offset = 0; 7659 ctsio->kern_sg_entries = 0; 7660 ctsio->kern_data_len = min(total_len, alloc_len); 7661 ctsio->kern_total_len = ctsio->kern_data_len; 7662 7663 mtx_lock(&lun->lun_lock); 7664 switch (cdb->action) { 7665 case SPRI_RK: { // read keys 7666 struct scsi_per_res_in_keys *res_keys; 7667 int i, key_count; 7668 7669 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7670 7671 /* 7672 * We had to drop the lock to allocate our buffer, which 7673 * leaves time for someone to come in with another 7674 * persistent reservation. (That is unlikely, though, 7675 * since this should be the only persistent reservation 7676 * command active right now.) 7677 */ 7678 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7679 (lun->pr_key_count * 7680 sizeof(struct scsi_per_res_key)))){ 7681 mtx_unlock(&lun->lun_lock); 7682 free(ctsio->kern_data_ptr, M_CTL); 7683 printf("%s: reservation length changed, retrying\n", 7684 __func__); 7685 goto retry; 7686 } 7687 7688 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7689 7690 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7691 lun->pr_key_count, res_keys->header.length); 7692 7693 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7694 if ((key = ctl_get_prkey(lun, i)) == 0) 7695 continue; 7696 7697 /* 7698 * We used lun->pr_key_count to calculate the 7699 * size to allocate. If it turns out the number of 7700 * initiators with the registered flag set is 7701 * larger than that (i.e. they haven't been kept in 7702 * sync), we've got a problem. 7703 */ 7704 if (key_count >= lun->pr_key_count) { 7705 key_count++; 7706 continue; 7707 } 7708 scsi_u64to8b(key, res_keys->keys[key_count].key); 7709 key_count++; 7710 } 7711 break; 7712 } 7713 case SPRI_RR: { // read reservation 7714 struct scsi_per_res_in_rsrv *res; 7715 int tmp_len, header_only; 7716 7717 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7718 7719 scsi_ulto4b(lun->pr_generation, res->header.generation); 7720 7721 if (lun->flags & CTL_LUN_PR_RESERVED) 7722 { 7723 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7724 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7725 res->header.length); 7726 header_only = 0; 7727 } else { 7728 tmp_len = sizeof(struct scsi_per_res_in_header); 7729 scsi_ulto4b(0, res->header.length); 7730 header_only = 1; 7731 } 7732 7733 /* 7734 * We had to drop the lock to allocate our buffer, which 7735 * leaves time for someone to come in with another 7736 * persistent reservation. (That is unlikely, though, 7737 * since this should be the only persistent reservation 7738 * command active right now.) 7739 */ 7740 if (tmp_len != total_len) { 7741 mtx_unlock(&lun->lun_lock); 7742 free(ctsio->kern_data_ptr, M_CTL); 7743 printf("%s: reservation status changed, retrying\n", 7744 __func__); 7745 goto retry; 7746 } 7747 7748 /* 7749 * No reservation held, so we're done. 7750 */ 7751 if (header_only != 0) 7752 break; 7753 7754 /* 7755 * If the registration is an All Registrants type, the key 7756 * is 0, since it doesn't really matter. 7757 */ 7758 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7759 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7760 res->data.reservation); 7761 } 7762 res->data.scopetype = lun->pr_res_type; 7763 break; 7764 } 7765 case SPRI_RC: //report capabilities 7766 { 7767 struct scsi_per_res_cap *res_cap; 7768 uint16_t type_mask; 7769 7770 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7771 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7772 res_cap->flags1 = SPRI_CRH; 7773 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7774 type_mask = SPRI_TM_WR_EX_AR | 7775 SPRI_TM_EX_AC_RO | 7776 SPRI_TM_WR_EX_RO | 7777 SPRI_TM_EX_AC | 7778 SPRI_TM_WR_EX | 7779 SPRI_TM_EX_AC_AR; 7780 scsi_ulto2b(type_mask, res_cap->type_mask); 7781 break; 7782 } 7783 case SPRI_RS: { // read full status 7784 struct scsi_per_res_in_full *res_status; 7785 struct scsi_per_res_in_full_desc *res_desc; 7786 struct ctl_port *port; 7787 int i, len; 7788 7789 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7790 7791 /* 7792 * We had to drop the lock to allocate our buffer, which 7793 * leaves time for someone to come in with another 7794 * persistent reservation. (That is unlikely, though, 7795 * since this should be the only persistent reservation 7796 * command active right now.) 7797 */ 7798 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7799 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7800 lun->pr_key_count)){ 7801 mtx_unlock(&lun->lun_lock); 7802 free(ctsio->kern_data_ptr, M_CTL); 7803 printf("%s: reservation length changed, retrying\n", 7804 __func__); 7805 goto retry; 7806 } 7807 7808 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7809 7810 res_desc = &res_status->desc[0]; 7811 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7812 if ((key = ctl_get_prkey(lun, i)) == 0) 7813 continue; 7814 7815 scsi_u64to8b(key, res_desc->res_key.key); 7816 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7817 (lun->pr_res_idx == i || 7818 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7819 res_desc->flags = SPRI_FULL_R_HOLDER; 7820 res_desc->scopetype = lun->pr_res_type; 7821 } 7822 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7823 res_desc->rel_trgt_port_id); 7824 len = 0; 7825 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7826 if (port != NULL) 7827 len = ctl_create_iid(port, 7828 i % CTL_MAX_INIT_PER_PORT, 7829 res_desc->transport_id); 7830 scsi_ulto4b(len, res_desc->additional_length); 7831 res_desc = (struct scsi_per_res_in_full_desc *) 7832 &res_desc->transport_id[len]; 7833 } 7834 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7835 res_status->header.length); 7836 break; 7837 } 7838 default: 7839 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7840 } 7841 mtx_unlock(&lun->lun_lock); 7842 7843 ctl_set_success(ctsio); 7844 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7845 ctsio->be_move_done = ctl_config_move_done; 7846 ctl_datamove((union ctl_io *)ctsio); 7847 return (CTL_RETVAL_COMPLETE); 7848 } 7849 7850 /* 7851 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7852 * it should return. 7853 */ 7854 static int 7855 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7856 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7857 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7858 struct scsi_per_res_out_parms* param) 7859 { 7860 union ctl_ha_msg persis_io; 7861 int i; 7862 7863 mtx_lock(&lun->lun_lock); 7864 if (sa_res_key == 0) { 7865 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7866 /* validate scope and type */ 7867 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7868 SPR_LU_SCOPE) { 7869 mtx_unlock(&lun->lun_lock); 7870 ctl_set_invalid_field(/*ctsio*/ ctsio, 7871 /*sks_valid*/ 1, 7872 /*command*/ 1, 7873 /*field*/ 2, 7874 /*bit_valid*/ 1, 7875 /*bit*/ 4); 7876 ctl_done((union ctl_io *)ctsio); 7877 return (1); 7878 } 7879 7880 if (type>8 || type==2 || type==4 || type==0) { 7881 mtx_unlock(&lun->lun_lock); 7882 ctl_set_invalid_field(/*ctsio*/ ctsio, 7883 /*sks_valid*/ 1, 7884 /*command*/ 1, 7885 /*field*/ 2, 7886 /*bit_valid*/ 1, 7887 /*bit*/ 0); 7888 ctl_done((union ctl_io *)ctsio); 7889 return (1); 7890 } 7891 7892 /* 7893 * Unregister everybody else and build UA for 7894 * them 7895 */ 7896 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7897 if (i == residx || ctl_get_prkey(lun, i) == 0) 7898 continue; 7899 7900 ctl_clr_prkey(lun, i); 7901 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7902 } 7903 lun->pr_key_count = 1; 7904 lun->pr_res_type = type; 7905 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7906 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7907 lun->pr_res_idx = residx; 7908 lun->pr_generation++; 7909 mtx_unlock(&lun->lun_lock); 7910 7911 /* send msg to other side */ 7912 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7913 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7914 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7915 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7916 persis_io.pr.pr_info.res_type = type; 7917 memcpy(persis_io.pr.pr_info.sa_res_key, 7918 param->serv_act_res_key, 7919 sizeof(param->serv_act_res_key)); 7920 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7921 sizeof(persis_io.pr), M_WAITOK); 7922 } else { 7923 /* not all registrants */ 7924 mtx_unlock(&lun->lun_lock); 7925 free(ctsio->kern_data_ptr, M_CTL); 7926 ctl_set_invalid_field(ctsio, 7927 /*sks_valid*/ 1, 7928 /*command*/ 0, 7929 /*field*/ 8, 7930 /*bit_valid*/ 0, 7931 /*bit*/ 0); 7932 ctl_done((union ctl_io *)ctsio); 7933 return (1); 7934 } 7935 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7936 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7937 int found = 0; 7938 7939 if (res_key == sa_res_key) { 7940 /* special case */ 7941 /* 7942 * The spec implies this is not good but doesn't 7943 * say what to do. There are two choices either 7944 * generate a res conflict or check condition 7945 * with illegal field in parameter data. Since 7946 * that is what is done when the sa_res_key is 7947 * zero I'll take that approach since this has 7948 * to do with the sa_res_key. 7949 */ 7950 mtx_unlock(&lun->lun_lock); 7951 free(ctsio->kern_data_ptr, M_CTL); 7952 ctl_set_invalid_field(ctsio, 7953 /*sks_valid*/ 1, 7954 /*command*/ 0, 7955 /*field*/ 8, 7956 /*bit_valid*/ 0, 7957 /*bit*/ 0); 7958 ctl_done((union ctl_io *)ctsio); 7959 return (1); 7960 } 7961 7962 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7963 if (ctl_get_prkey(lun, i) != sa_res_key) 7964 continue; 7965 7966 found = 1; 7967 ctl_clr_prkey(lun, i); 7968 lun->pr_key_count--; 7969 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7970 } 7971 if (!found) { 7972 mtx_unlock(&lun->lun_lock); 7973 free(ctsio->kern_data_ptr, M_CTL); 7974 ctl_set_reservation_conflict(ctsio); 7975 ctl_done((union ctl_io *)ctsio); 7976 return (CTL_RETVAL_COMPLETE); 7977 } 7978 lun->pr_generation++; 7979 mtx_unlock(&lun->lun_lock); 7980 7981 /* send msg to other side */ 7982 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7983 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7984 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7985 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7986 persis_io.pr.pr_info.res_type = type; 7987 memcpy(persis_io.pr.pr_info.sa_res_key, 7988 param->serv_act_res_key, 7989 sizeof(param->serv_act_res_key)); 7990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7991 sizeof(persis_io.pr), M_WAITOK); 7992 } else { 7993 /* Reserved but not all registrants */ 7994 /* sa_res_key is res holder */ 7995 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7996 /* validate scope and type */ 7997 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7998 SPR_LU_SCOPE) { 7999 mtx_unlock(&lun->lun_lock); 8000 ctl_set_invalid_field(/*ctsio*/ ctsio, 8001 /*sks_valid*/ 1, 8002 /*command*/ 1, 8003 /*field*/ 2, 8004 /*bit_valid*/ 1, 8005 /*bit*/ 4); 8006 ctl_done((union ctl_io *)ctsio); 8007 return (1); 8008 } 8009 8010 if (type>8 || type==2 || type==4 || type==0) { 8011 mtx_unlock(&lun->lun_lock); 8012 ctl_set_invalid_field(/*ctsio*/ ctsio, 8013 /*sks_valid*/ 1, 8014 /*command*/ 1, 8015 /*field*/ 2, 8016 /*bit_valid*/ 1, 8017 /*bit*/ 0); 8018 ctl_done((union ctl_io *)ctsio); 8019 return (1); 8020 } 8021 8022 /* 8023 * Do the following: 8024 * if sa_res_key != res_key remove all 8025 * registrants w/sa_res_key and generate UA 8026 * for these registrants(Registrations 8027 * Preempted) if it wasn't an exclusive 8028 * reservation generate UA(Reservations 8029 * Preempted) for all other registered nexuses 8030 * if the type has changed. Establish the new 8031 * reservation and holder. If res_key and 8032 * sa_res_key are the same do the above 8033 * except don't unregister the res holder. 8034 */ 8035 8036 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8037 if (i == residx || ctl_get_prkey(lun, i) == 0) 8038 continue; 8039 8040 if (sa_res_key == ctl_get_prkey(lun, i)) { 8041 ctl_clr_prkey(lun, i); 8042 lun->pr_key_count--; 8043 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8044 } else if (type != lun->pr_res_type && 8045 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8046 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8047 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8048 } 8049 } 8050 lun->pr_res_type = type; 8051 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8052 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8053 lun->pr_res_idx = residx; 8054 else 8055 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8056 lun->pr_generation++; 8057 mtx_unlock(&lun->lun_lock); 8058 8059 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8060 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8061 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8062 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8063 persis_io.pr.pr_info.res_type = type; 8064 memcpy(persis_io.pr.pr_info.sa_res_key, 8065 param->serv_act_res_key, 8066 sizeof(param->serv_act_res_key)); 8067 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8068 sizeof(persis_io.pr), M_WAITOK); 8069 } else { 8070 /* 8071 * sa_res_key is not the res holder just 8072 * remove registrants 8073 */ 8074 int found=0; 8075 8076 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8077 if (sa_res_key != ctl_get_prkey(lun, i)) 8078 continue; 8079 8080 found = 1; 8081 ctl_clr_prkey(lun, i); 8082 lun->pr_key_count--; 8083 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8084 } 8085 8086 if (!found) { 8087 mtx_unlock(&lun->lun_lock); 8088 free(ctsio->kern_data_ptr, M_CTL); 8089 ctl_set_reservation_conflict(ctsio); 8090 ctl_done((union ctl_io *)ctsio); 8091 return (1); 8092 } 8093 lun->pr_generation++; 8094 mtx_unlock(&lun->lun_lock); 8095 8096 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8097 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8098 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8099 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8100 persis_io.pr.pr_info.res_type = type; 8101 memcpy(persis_io.pr.pr_info.sa_res_key, 8102 param->serv_act_res_key, 8103 sizeof(param->serv_act_res_key)); 8104 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8105 sizeof(persis_io.pr), M_WAITOK); 8106 } 8107 } 8108 return (0); 8109 } 8110 8111 static void 8112 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8113 { 8114 uint64_t sa_res_key; 8115 int i; 8116 8117 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8118 8119 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8120 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8121 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8122 if (sa_res_key == 0) { 8123 /* 8124 * Unregister everybody else and build UA for 8125 * them 8126 */ 8127 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8128 if (i == msg->pr.pr_info.residx || 8129 ctl_get_prkey(lun, i) == 0) 8130 continue; 8131 8132 ctl_clr_prkey(lun, i); 8133 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8134 } 8135 8136 lun->pr_key_count = 1; 8137 lun->pr_res_type = msg->pr.pr_info.res_type; 8138 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8139 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8140 lun->pr_res_idx = msg->pr.pr_info.residx; 8141 } else { 8142 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8143 if (sa_res_key == ctl_get_prkey(lun, i)) 8144 continue; 8145 8146 ctl_clr_prkey(lun, i); 8147 lun->pr_key_count--; 8148 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8149 } 8150 } 8151 } else { 8152 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8153 if (i == msg->pr.pr_info.residx || 8154 ctl_get_prkey(lun, i) == 0) 8155 continue; 8156 8157 if (sa_res_key == ctl_get_prkey(lun, i)) { 8158 ctl_clr_prkey(lun, i); 8159 lun->pr_key_count--; 8160 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8161 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8162 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8163 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8164 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8165 } 8166 } 8167 lun->pr_res_type = msg->pr.pr_info.res_type; 8168 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8169 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8170 lun->pr_res_idx = msg->pr.pr_info.residx; 8171 else 8172 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8173 } 8174 lun->pr_generation++; 8175 8176 } 8177 8178 int 8179 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8180 { 8181 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8182 struct ctl_lun *lun = CTL_LUN(ctsio); 8183 int retval; 8184 u_int32_t param_len; 8185 struct scsi_per_res_out *cdb; 8186 struct scsi_per_res_out_parms* param; 8187 uint32_t residx; 8188 uint64_t res_key, sa_res_key, key; 8189 uint8_t type; 8190 union ctl_ha_msg persis_io; 8191 int i; 8192 8193 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8194 8195 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8196 retval = CTL_RETVAL_COMPLETE; 8197 8198 /* 8199 * We only support whole-LUN scope. The scope & type are ignored for 8200 * register, register and ignore existing key and clear. 8201 * We sometimes ignore scope and type on preempts too!! 8202 * Verify reservation type here as well. 8203 */ 8204 type = cdb->scope_type & SPR_TYPE_MASK; 8205 if ((cdb->action == SPRO_RESERVE) 8206 || (cdb->action == SPRO_RELEASE)) { 8207 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8208 ctl_set_invalid_field(/*ctsio*/ ctsio, 8209 /*sks_valid*/ 1, 8210 /*command*/ 1, 8211 /*field*/ 2, 8212 /*bit_valid*/ 1, 8213 /*bit*/ 4); 8214 ctl_done((union ctl_io *)ctsio); 8215 return (CTL_RETVAL_COMPLETE); 8216 } 8217 8218 if (type>8 || type==2 || type==4 || type==0) { 8219 ctl_set_invalid_field(/*ctsio*/ ctsio, 8220 /*sks_valid*/ 1, 8221 /*command*/ 1, 8222 /*field*/ 2, 8223 /*bit_valid*/ 1, 8224 /*bit*/ 0); 8225 ctl_done((union ctl_io *)ctsio); 8226 return (CTL_RETVAL_COMPLETE); 8227 } 8228 } 8229 8230 param_len = scsi_4btoul(cdb->length); 8231 8232 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8233 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8234 ctsio->kern_data_len = param_len; 8235 ctsio->kern_total_len = param_len; 8236 ctsio->kern_rel_offset = 0; 8237 ctsio->kern_sg_entries = 0; 8238 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8239 ctsio->be_move_done = ctl_config_move_done; 8240 ctl_datamove((union ctl_io *)ctsio); 8241 8242 return (CTL_RETVAL_COMPLETE); 8243 } 8244 8245 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8246 8247 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8248 res_key = scsi_8btou64(param->res_key.key); 8249 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8250 8251 /* 8252 * Validate the reservation key here except for SPRO_REG_IGNO 8253 * This must be done for all other service actions 8254 */ 8255 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8256 mtx_lock(&lun->lun_lock); 8257 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8258 if (res_key != key) { 8259 /* 8260 * The current key passed in doesn't match 8261 * the one the initiator previously 8262 * registered. 8263 */ 8264 mtx_unlock(&lun->lun_lock); 8265 free(ctsio->kern_data_ptr, M_CTL); 8266 ctl_set_reservation_conflict(ctsio); 8267 ctl_done((union ctl_io *)ctsio); 8268 return (CTL_RETVAL_COMPLETE); 8269 } 8270 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8271 /* 8272 * We are not registered 8273 */ 8274 mtx_unlock(&lun->lun_lock); 8275 free(ctsio->kern_data_ptr, M_CTL); 8276 ctl_set_reservation_conflict(ctsio); 8277 ctl_done((union ctl_io *)ctsio); 8278 return (CTL_RETVAL_COMPLETE); 8279 } else if (res_key != 0) { 8280 /* 8281 * We are not registered and trying to register but 8282 * the register key isn't zero. 8283 */ 8284 mtx_unlock(&lun->lun_lock); 8285 free(ctsio->kern_data_ptr, M_CTL); 8286 ctl_set_reservation_conflict(ctsio); 8287 ctl_done((union ctl_io *)ctsio); 8288 return (CTL_RETVAL_COMPLETE); 8289 } 8290 mtx_unlock(&lun->lun_lock); 8291 } 8292 8293 switch (cdb->action & SPRO_ACTION_MASK) { 8294 case SPRO_REGISTER: 8295 case SPRO_REG_IGNO: { 8296 /* 8297 * We don't support any of these options, as we report in 8298 * the read capabilities request (see 8299 * ctl_persistent_reserve_in(), above). 8300 */ 8301 if ((param->flags & SPR_SPEC_I_PT) 8302 || (param->flags & SPR_ALL_TG_PT) 8303 || (param->flags & SPR_APTPL)) { 8304 int bit_ptr; 8305 8306 if (param->flags & SPR_APTPL) 8307 bit_ptr = 0; 8308 else if (param->flags & SPR_ALL_TG_PT) 8309 bit_ptr = 2; 8310 else /* SPR_SPEC_I_PT */ 8311 bit_ptr = 3; 8312 8313 free(ctsio->kern_data_ptr, M_CTL); 8314 ctl_set_invalid_field(ctsio, 8315 /*sks_valid*/ 1, 8316 /*command*/ 0, 8317 /*field*/ 20, 8318 /*bit_valid*/ 1, 8319 /*bit*/ bit_ptr); 8320 ctl_done((union ctl_io *)ctsio); 8321 return (CTL_RETVAL_COMPLETE); 8322 } 8323 8324 mtx_lock(&lun->lun_lock); 8325 8326 /* 8327 * The initiator wants to clear the 8328 * key/unregister. 8329 */ 8330 if (sa_res_key == 0) { 8331 if ((res_key == 0 8332 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8333 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8334 && ctl_get_prkey(lun, residx) == 0)) { 8335 mtx_unlock(&lun->lun_lock); 8336 goto done; 8337 } 8338 8339 ctl_clr_prkey(lun, residx); 8340 lun->pr_key_count--; 8341 8342 if (residx == lun->pr_res_idx) { 8343 lun->flags &= ~CTL_LUN_PR_RESERVED; 8344 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8345 8346 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8347 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8348 lun->pr_key_count) { 8349 /* 8350 * If the reservation is a registrants 8351 * only type we need to generate a UA 8352 * for other registered inits. The 8353 * sense code should be RESERVATIONS 8354 * RELEASED 8355 */ 8356 8357 for (i = softc->init_min; i < softc->init_max; i++){ 8358 if (ctl_get_prkey(lun, i) == 0) 8359 continue; 8360 ctl_est_ua(lun, i, 8361 CTL_UA_RES_RELEASE); 8362 } 8363 } 8364 lun->pr_res_type = 0; 8365 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8366 if (lun->pr_key_count==0) { 8367 lun->flags &= ~CTL_LUN_PR_RESERVED; 8368 lun->pr_res_type = 0; 8369 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8370 } 8371 } 8372 lun->pr_generation++; 8373 mtx_unlock(&lun->lun_lock); 8374 8375 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8376 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8377 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8378 persis_io.pr.pr_info.residx = residx; 8379 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8380 sizeof(persis_io.pr), M_WAITOK); 8381 } else /* sa_res_key != 0 */ { 8382 /* 8383 * If we aren't registered currently then increment 8384 * the key count and set the registered flag. 8385 */ 8386 ctl_alloc_prkey(lun, residx); 8387 if (ctl_get_prkey(lun, residx) == 0) 8388 lun->pr_key_count++; 8389 ctl_set_prkey(lun, residx, sa_res_key); 8390 lun->pr_generation++; 8391 mtx_unlock(&lun->lun_lock); 8392 8393 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8394 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8395 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8396 persis_io.pr.pr_info.residx = residx; 8397 memcpy(persis_io.pr.pr_info.sa_res_key, 8398 param->serv_act_res_key, 8399 sizeof(param->serv_act_res_key)); 8400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8401 sizeof(persis_io.pr), M_WAITOK); 8402 } 8403 8404 break; 8405 } 8406 case SPRO_RESERVE: 8407 mtx_lock(&lun->lun_lock); 8408 if (lun->flags & CTL_LUN_PR_RESERVED) { 8409 /* 8410 * if this isn't the reservation holder and it's 8411 * not a "all registrants" type or if the type is 8412 * different then we have a conflict 8413 */ 8414 if ((lun->pr_res_idx != residx 8415 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8416 || lun->pr_res_type != type) { 8417 mtx_unlock(&lun->lun_lock); 8418 free(ctsio->kern_data_ptr, M_CTL); 8419 ctl_set_reservation_conflict(ctsio); 8420 ctl_done((union ctl_io *)ctsio); 8421 return (CTL_RETVAL_COMPLETE); 8422 } 8423 mtx_unlock(&lun->lun_lock); 8424 } else /* create a reservation */ { 8425 /* 8426 * If it's not an "all registrants" type record 8427 * reservation holder 8428 */ 8429 if (type != SPR_TYPE_WR_EX_AR 8430 && type != SPR_TYPE_EX_AC_AR) 8431 lun->pr_res_idx = residx; /* Res holder */ 8432 else 8433 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8434 8435 lun->flags |= CTL_LUN_PR_RESERVED; 8436 lun->pr_res_type = type; 8437 8438 mtx_unlock(&lun->lun_lock); 8439 8440 /* send msg to other side */ 8441 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8442 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8443 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8444 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8445 persis_io.pr.pr_info.res_type = type; 8446 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8447 sizeof(persis_io.pr), M_WAITOK); 8448 } 8449 break; 8450 8451 case SPRO_RELEASE: 8452 mtx_lock(&lun->lun_lock); 8453 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8454 /* No reservation exists return good status */ 8455 mtx_unlock(&lun->lun_lock); 8456 goto done; 8457 } 8458 /* 8459 * Is this nexus a reservation holder? 8460 */ 8461 if (lun->pr_res_idx != residx 8462 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8463 /* 8464 * not a res holder return good status but 8465 * do nothing 8466 */ 8467 mtx_unlock(&lun->lun_lock); 8468 goto done; 8469 } 8470 8471 if (lun->pr_res_type != type) { 8472 mtx_unlock(&lun->lun_lock); 8473 free(ctsio->kern_data_ptr, M_CTL); 8474 ctl_set_illegal_pr_release(ctsio); 8475 ctl_done((union ctl_io *)ctsio); 8476 return (CTL_RETVAL_COMPLETE); 8477 } 8478 8479 /* okay to release */ 8480 lun->flags &= ~CTL_LUN_PR_RESERVED; 8481 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8482 lun->pr_res_type = 0; 8483 8484 /* 8485 * If this isn't an exclusive access reservation and NUAR 8486 * is not set, generate UA for all other registrants. 8487 */ 8488 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8489 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8490 for (i = softc->init_min; i < softc->init_max; i++) { 8491 if (i == residx || ctl_get_prkey(lun, i) == 0) 8492 continue; 8493 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8494 } 8495 } 8496 mtx_unlock(&lun->lun_lock); 8497 8498 /* Send msg to other side */ 8499 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8500 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8501 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8502 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8503 sizeof(persis_io.pr), M_WAITOK); 8504 break; 8505 8506 case SPRO_CLEAR: 8507 /* send msg to other side */ 8508 8509 mtx_lock(&lun->lun_lock); 8510 lun->flags &= ~CTL_LUN_PR_RESERVED; 8511 lun->pr_res_type = 0; 8512 lun->pr_key_count = 0; 8513 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8514 8515 ctl_clr_prkey(lun, residx); 8516 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8517 if (ctl_get_prkey(lun, i) != 0) { 8518 ctl_clr_prkey(lun, i); 8519 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8520 } 8521 lun->pr_generation++; 8522 mtx_unlock(&lun->lun_lock); 8523 8524 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8525 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8526 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8527 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8528 sizeof(persis_io.pr), M_WAITOK); 8529 break; 8530 8531 case SPRO_PREEMPT: 8532 case SPRO_PRE_ABO: { 8533 int nretval; 8534 8535 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8536 residx, ctsio, cdb, param); 8537 if (nretval != 0) 8538 return (CTL_RETVAL_COMPLETE); 8539 break; 8540 } 8541 default: 8542 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8543 } 8544 8545 done: 8546 free(ctsio->kern_data_ptr, M_CTL); 8547 ctl_set_success(ctsio); 8548 ctl_done((union ctl_io *)ctsio); 8549 8550 return (retval); 8551 } 8552 8553 /* 8554 * This routine is for handling a message from the other SC pertaining to 8555 * persistent reserve out. All the error checking will have been done 8556 * so only performing the action need be done here to keep the two 8557 * in sync. 8558 */ 8559 static void 8560 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8561 { 8562 struct ctl_softc *softc = CTL_SOFTC(io); 8563 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8564 struct ctl_lun *lun; 8565 int i; 8566 uint32_t residx, targ_lun; 8567 8568 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8569 mtx_lock(&softc->ctl_lock); 8570 if (targ_lun >= ctl_max_luns || 8571 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8572 mtx_unlock(&softc->ctl_lock); 8573 return; 8574 } 8575 mtx_lock(&lun->lun_lock); 8576 mtx_unlock(&softc->ctl_lock); 8577 if (lun->flags & CTL_LUN_DISABLED) { 8578 mtx_unlock(&lun->lun_lock); 8579 return; 8580 } 8581 residx = ctl_get_initindex(&msg->hdr.nexus); 8582 switch(msg->pr.pr_info.action) { 8583 case CTL_PR_REG_KEY: 8584 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8585 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8586 lun->pr_key_count++; 8587 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8588 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8589 lun->pr_generation++; 8590 break; 8591 8592 case CTL_PR_UNREG_KEY: 8593 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8594 lun->pr_key_count--; 8595 8596 /* XXX Need to see if the reservation has been released */ 8597 /* if so do we need to generate UA? */ 8598 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8599 lun->flags &= ~CTL_LUN_PR_RESERVED; 8600 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8601 8602 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8603 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8604 lun->pr_key_count) { 8605 /* 8606 * If the reservation is a registrants 8607 * only type we need to generate a UA 8608 * for other registered inits. The 8609 * sense code should be RESERVATIONS 8610 * RELEASED 8611 */ 8612 8613 for (i = softc->init_min; i < softc->init_max; i++) { 8614 if (ctl_get_prkey(lun, i) == 0) 8615 continue; 8616 8617 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8618 } 8619 } 8620 lun->pr_res_type = 0; 8621 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8622 if (lun->pr_key_count==0) { 8623 lun->flags &= ~CTL_LUN_PR_RESERVED; 8624 lun->pr_res_type = 0; 8625 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8626 } 8627 } 8628 lun->pr_generation++; 8629 break; 8630 8631 case CTL_PR_RESERVE: 8632 lun->flags |= CTL_LUN_PR_RESERVED; 8633 lun->pr_res_type = msg->pr.pr_info.res_type; 8634 lun->pr_res_idx = msg->pr.pr_info.residx; 8635 8636 break; 8637 8638 case CTL_PR_RELEASE: 8639 /* 8640 * If this isn't an exclusive access reservation and NUAR 8641 * is not set, generate UA for all other registrants. 8642 */ 8643 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8644 lun->pr_res_type != SPR_TYPE_WR_EX && 8645 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8646 for (i = softc->init_min; i < softc->init_max; i++) { 8647 if (i == residx || ctl_get_prkey(lun, i) == 0) 8648 continue; 8649 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8650 } 8651 } 8652 8653 lun->flags &= ~CTL_LUN_PR_RESERVED; 8654 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8655 lun->pr_res_type = 0; 8656 break; 8657 8658 case CTL_PR_PREEMPT: 8659 ctl_pro_preempt_other(lun, msg); 8660 break; 8661 case CTL_PR_CLEAR: 8662 lun->flags &= ~CTL_LUN_PR_RESERVED; 8663 lun->pr_res_type = 0; 8664 lun->pr_key_count = 0; 8665 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8666 8667 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8668 if (ctl_get_prkey(lun, i) == 0) 8669 continue; 8670 ctl_clr_prkey(lun, i); 8671 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8672 } 8673 lun->pr_generation++; 8674 break; 8675 } 8676 8677 mtx_unlock(&lun->lun_lock); 8678 } 8679 8680 int 8681 ctl_read_write(struct ctl_scsiio *ctsio) 8682 { 8683 struct ctl_lun *lun = CTL_LUN(ctsio); 8684 struct ctl_lba_len_flags *lbalen; 8685 uint64_t lba; 8686 uint32_t num_blocks; 8687 int flags, retval; 8688 int isread; 8689 8690 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8691 8692 flags = 0; 8693 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8694 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8695 switch (ctsio->cdb[0]) { 8696 case READ_6: 8697 case WRITE_6: { 8698 struct scsi_rw_6 *cdb; 8699 8700 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8701 8702 lba = scsi_3btoul(cdb->addr); 8703 /* only 5 bits are valid in the most significant address byte */ 8704 lba &= 0x1fffff; 8705 num_blocks = cdb->length; 8706 /* 8707 * This is correct according to SBC-2. 8708 */ 8709 if (num_blocks == 0) 8710 num_blocks = 256; 8711 break; 8712 } 8713 case READ_10: 8714 case WRITE_10: { 8715 struct scsi_rw_10 *cdb; 8716 8717 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8718 if (cdb->byte2 & SRW10_FUA) 8719 flags |= CTL_LLF_FUA; 8720 if (cdb->byte2 & SRW10_DPO) 8721 flags |= CTL_LLF_DPO; 8722 lba = scsi_4btoul(cdb->addr); 8723 num_blocks = scsi_2btoul(cdb->length); 8724 break; 8725 } 8726 case WRITE_VERIFY_10: { 8727 struct scsi_write_verify_10 *cdb; 8728 8729 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8730 flags |= CTL_LLF_FUA; 8731 if (cdb->byte2 & SWV_DPO) 8732 flags |= CTL_LLF_DPO; 8733 lba = scsi_4btoul(cdb->addr); 8734 num_blocks = scsi_2btoul(cdb->length); 8735 break; 8736 } 8737 case READ_12: 8738 case WRITE_12: { 8739 struct scsi_rw_12 *cdb; 8740 8741 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8742 if (cdb->byte2 & SRW12_FUA) 8743 flags |= CTL_LLF_FUA; 8744 if (cdb->byte2 & SRW12_DPO) 8745 flags |= CTL_LLF_DPO; 8746 lba = scsi_4btoul(cdb->addr); 8747 num_blocks = scsi_4btoul(cdb->length); 8748 break; 8749 } 8750 case WRITE_VERIFY_12: { 8751 struct scsi_write_verify_12 *cdb; 8752 8753 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8754 flags |= CTL_LLF_FUA; 8755 if (cdb->byte2 & SWV_DPO) 8756 flags |= CTL_LLF_DPO; 8757 lba = scsi_4btoul(cdb->addr); 8758 num_blocks = scsi_4btoul(cdb->length); 8759 break; 8760 } 8761 case READ_16: 8762 case WRITE_16: { 8763 struct scsi_rw_16 *cdb; 8764 8765 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8766 if (cdb->byte2 & SRW12_FUA) 8767 flags |= CTL_LLF_FUA; 8768 if (cdb->byte2 & SRW12_DPO) 8769 flags |= CTL_LLF_DPO; 8770 lba = scsi_8btou64(cdb->addr); 8771 num_blocks = scsi_4btoul(cdb->length); 8772 break; 8773 } 8774 case WRITE_ATOMIC_16: { 8775 struct scsi_write_atomic_16 *cdb; 8776 8777 if (lun->be_lun->atomicblock == 0) { 8778 ctl_set_invalid_opcode(ctsio); 8779 ctl_done((union ctl_io *)ctsio); 8780 return (CTL_RETVAL_COMPLETE); 8781 } 8782 8783 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8784 if (cdb->byte2 & SRW12_FUA) 8785 flags |= CTL_LLF_FUA; 8786 if (cdb->byte2 & SRW12_DPO) 8787 flags |= CTL_LLF_DPO; 8788 lba = scsi_8btou64(cdb->addr); 8789 num_blocks = scsi_2btoul(cdb->length); 8790 if (num_blocks > lun->be_lun->atomicblock) { 8791 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8792 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8793 /*bit*/ 0); 8794 ctl_done((union ctl_io *)ctsio); 8795 return (CTL_RETVAL_COMPLETE); 8796 } 8797 break; 8798 } 8799 case WRITE_VERIFY_16: { 8800 struct scsi_write_verify_16 *cdb; 8801 8802 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8803 flags |= CTL_LLF_FUA; 8804 if (cdb->byte2 & SWV_DPO) 8805 flags |= CTL_LLF_DPO; 8806 lba = scsi_8btou64(cdb->addr); 8807 num_blocks = scsi_4btoul(cdb->length); 8808 break; 8809 } 8810 default: 8811 /* 8812 * We got a command we don't support. This shouldn't 8813 * happen, commands should be filtered out above us. 8814 */ 8815 ctl_set_invalid_opcode(ctsio); 8816 ctl_done((union ctl_io *)ctsio); 8817 8818 return (CTL_RETVAL_COMPLETE); 8819 break; /* NOTREACHED */ 8820 } 8821 8822 /* 8823 * The first check is to make sure we're in bounds, the second 8824 * check is to catch wrap-around problems. If the lba + num blocks 8825 * is less than the lba, then we've wrapped around and the block 8826 * range is invalid anyway. 8827 */ 8828 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8829 || ((lba + num_blocks) < lba)) { 8830 ctl_set_lba_out_of_range(ctsio, 8831 MAX(lba, lun->be_lun->maxlba + 1)); 8832 ctl_done((union ctl_io *)ctsio); 8833 return (CTL_RETVAL_COMPLETE); 8834 } 8835 8836 /* 8837 * According to SBC-3, a transfer length of 0 is not an error. 8838 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8839 * translates to 256 blocks for those commands. 8840 */ 8841 if (num_blocks == 0) { 8842 ctl_set_success(ctsio); 8843 ctl_done((union ctl_io *)ctsio); 8844 return (CTL_RETVAL_COMPLETE); 8845 } 8846 8847 /* Set FUA and/or DPO if caches are disabled. */ 8848 if (isread) { 8849 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8850 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8851 } else { 8852 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8853 flags |= CTL_LLF_FUA; 8854 } 8855 8856 lbalen = (struct ctl_lba_len_flags *) 8857 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8858 lbalen->lba = lba; 8859 lbalen->len = num_blocks; 8860 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8861 8862 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8863 ctsio->kern_rel_offset = 0; 8864 8865 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8866 8867 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8868 return (retval); 8869 } 8870 8871 static int 8872 ctl_cnw_cont(union ctl_io *io) 8873 { 8874 struct ctl_lun *lun = CTL_LUN(io); 8875 struct ctl_scsiio *ctsio; 8876 struct ctl_lba_len_flags *lbalen; 8877 int retval; 8878 8879 ctsio = &io->scsiio; 8880 ctsio->io_hdr.status = CTL_STATUS_NONE; 8881 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8882 lbalen = (struct ctl_lba_len_flags *) 8883 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8884 lbalen->flags &= ~CTL_LLF_COMPARE; 8885 lbalen->flags |= CTL_LLF_WRITE; 8886 8887 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8888 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8889 return (retval); 8890 } 8891 8892 int 8893 ctl_cnw(struct ctl_scsiio *ctsio) 8894 { 8895 struct ctl_lun *lun = CTL_LUN(ctsio); 8896 struct ctl_lba_len_flags *lbalen; 8897 uint64_t lba; 8898 uint32_t num_blocks; 8899 int flags, retval; 8900 8901 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8902 8903 flags = 0; 8904 switch (ctsio->cdb[0]) { 8905 case COMPARE_AND_WRITE: { 8906 struct scsi_compare_and_write *cdb; 8907 8908 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8909 if (cdb->byte2 & SRW10_FUA) 8910 flags |= CTL_LLF_FUA; 8911 if (cdb->byte2 & SRW10_DPO) 8912 flags |= CTL_LLF_DPO; 8913 lba = scsi_8btou64(cdb->addr); 8914 num_blocks = cdb->length; 8915 break; 8916 } 8917 default: 8918 /* 8919 * We got a command we don't support. This shouldn't 8920 * happen, commands should be filtered out above us. 8921 */ 8922 ctl_set_invalid_opcode(ctsio); 8923 ctl_done((union ctl_io *)ctsio); 8924 8925 return (CTL_RETVAL_COMPLETE); 8926 break; /* NOTREACHED */ 8927 } 8928 8929 /* 8930 * The first check is to make sure we're in bounds, the second 8931 * check is to catch wrap-around problems. If the lba + num blocks 8932 * is less than the lba, then we've wrapped around and the block 8933 * range is invalid anyway. 8934 */ 8935 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8936 || ((lba + num_blocks) < lba)) { 8937 ctl_set_lba_out_of_range(ctsio, 8938 MAX(lba, lun->be_lun->maxlba + 1)); 8939 ctl_done((union ctl_io *)ctsio); 8940 return (CTL_RETVAL_COMPLETE); 8941 } 8942 8943 /* 8944 * According to SBC-3, a transfer length of 0 is not an error. 8945 */ 8946 if (num_blocks == 0) { 8947 ctl_set_success(ctsio); 8948 ctl_done((union ctl_io *)ctsio); 8949 return (CTL_RETVAL_COMPLETE); 8950 } 8951 8952 /* Set FUA if write cache is disabled. */ 8953 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8954 flags |= CTL_LLF_FUA; 8955 8956 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8957 ctsio->kern_rel_offset = 0; 8958 8959 /* 8960 * Set the IO_CONT flag, so that if this I/O gets passed to 8961 * ctl_data_submit_done(), it'll get passed back to 8962 * ctl_ctl_cnw_cont() for further processing. 8963 */ 8964 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8965 ctsio->io_cont = ctl_cnw_cont; 8966 8967 lbalen = (struct ctl_lba_len_flags *) 8968 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8969 lbalen->lba = lba; 8970 lbalen->len = num_blocks; 8971 lbalen->flags = CTL_LLF_COMPARE | flags; 8972 8973 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8974 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8975 return (retval); 8976 } 8977 8978 int 8979 ctl_verify(struct ctl_scsiio *ctsio) 8980 { 8981 struct ctl_lun *lun = CTL_LUN(ctsio); 8982 struct ctl_lba_len_flags *lbalen; 8983 uint64_t lba; 8984 uint32_t num_blocks; 8985 int bytchk, flags; 8986 int retval; 8987 8988 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8989 8990 bytchk = 0; 8991 flags = CTL_LLF_FUA; 8992 switch (ctsio->cdb[0]) { 8993 case VERIFY_10: { 8994 struct scsi_verify_10 *cdb; 8995 8996 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8997 if (cdb->byte2 & SVFY_BYTCHK) 8998 bytchk = 1; 8999 if (cdb->byte2 & SVFY_DPO) 9000 flags |= CTL_LLF_DPO; 9001 lba = scsi_4btoul(cdb->addr); 9002 num_blocks = scsi_2btoul(cdb->length); 9003 break; 9004 } 9005 case VERIFY_12: { 9006 struct scsi_verify_12 *cdb; 9007 9008 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9009 if (cdb->byte2 & SVFY_BYTCHK) 9010 bytchk = 1; 9011 if (cdb->byte2 & SVFY_DPO) 9012 flags |= CTL_LLF_DPO; 9013 lba = scsi_4btoul(cdb->addr); 9014 num_blocks = scsi_4btoul(cdb->length); 9015 break; 9016 } 9017 case VERIFY_16: { 9018 struct scsi_rw_16 *cdb; 9019 9020 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9021 if (cdb->byte2 & SVFY_BYTCHK) 9022 bytchk = 1; 9023 if (cdb->byte2 & SVFY_DPO) 9024 flags |= CTL_LLF_DPO; 9025 lba = scsi_8btou64(cdb->addr); 9026 num_blocks = scsi_4btoul(cdb->length); 9027 break; 9028 } 9029 default: 9030 /* 9031 * We got a command we don't support. This shouldn't 9032 * happen, commands should be filtered out above us. 9033 */ 9034 ctl_set_invalid_opcode(ctsio); 9035 ctl_done((union ctl_io *)ctsio); 9036 return (CTL_RETVAL_COMPLETE); 9037 } 9038 9039 /* 9040 * The first check is to make sure we're in bounds, the second 9041 * check is to catch wrap-around problems. If the lba + num blocks 9042 * is less than the lba, then we've wrapped around and the block 9043 * range is invalid anyway. 9044 */ 9045 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9046 || ((lba + num_blocks) < lba)) { 9047 ctl_set_lba_out_of_range(ctsio, 9048 MAX(lba, lun->be_lun->maxlba + 1)); 9049 ctl_done((union ctl_io *)ctsio); 9050 return (CTL_RETVAL_COMPLETE); 9051 } 9052 9053 /* 9054 * According to SBC-3, a transfer length of 0 is not an error. 9055 */ 9056 if (num_blocks == 0) { 9057 ctl_set_success(ctsio); 9058 ctl_done((union ctl_io *)ctsio); 9059 return (CTL_RETVAL_COMPLETE); 9060 } 9061 9062 lbalen = (struct ctl_lba_len_flags *) 9063 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9064 lbalen->lba = lba; 9065 lbalen->len = num_blocks; 9066 if (bytchk) { 9067 lbalen->flags = CTL_LLF_COMPARE | flags; 9068 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9069 } else { 9070 lbalen->flags = CTL_LLF_VERIFY | flags; 9071 ctsio->kern_total_len = 0; 9072 } 9073 ctsio->kern_rel_offset = 0; 9074 9075 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9076 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9077 return (retval); 9078 } 9079 9080 int 9081 ctl_report_luns(struct ctl_scsiio *ctsio) 9082 { 9083 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9084 struct ctl_port *port = CTL_PORT(ctsio); 9085 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9086 struct scsi_report_luns *cdb; 9087 struct scsi_report_luns_data *lun_data; 9088 int num_filled, num_luns, num_port_luns, retval; 9089 uint32_t alloc_len, lun_datalen; 9090 uint32_t initidx, targ_lun_id, lun_id; 9091 9092 retval = CTL_RETVAL_COMPLETE; 9093 cdb = (struct scsi_report_luns *)ctsio->cdb; 9094 9095 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9096 9097 num_luns = 0; 9098 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9099 mtx_lock(&softc->ctl_lock); 9100 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9101 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9102 num_luns++; 9103 } 9104 mtx_unlock(&softc->ctl_lock); 9105 9106 switch (cdb->select_report) { 9107 case RPL_REPORT_DEFAULT: 9108 case RPL_REPORT_ALL: 9109 case RPL_REPORT_NONSUBSID: 9110 break; 9111 case RPL_REPORT_WELLKNOWN: 9112 case RPL_REPORT_ADMIN: 9113 case RPL_REPORT_CONGLOM: 9114 num_luns = 0; 9115 break; 9116 default: 9117 ctl_set_invalid_field(ctsio, 9118 /*sks_valid*/ 1, 9119 /*command*/ 1, 9120 /*field*/ 2, 9121 /*bit_valid*/ 0, 9122 /*bit*/ 0); 9123 ctl_done((union ctl_io *)ctsio); 9124 return (retval); 9125 break; /* NOTREACHED */ 9126 } 9127 9128 alloc_len = scsi_4btoul(cdb->length); 9129 /* 9130 * The initiator has to allocate at least 16 bytes for this request, 9131 * so he can at least get the header and the first LUN. Otherwise 9132 * we reject the request (per SPC-3 rev 14, section 6.21). 9133 */ 9134 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9135 sizeof(struct scsi_report_luns_lundata))) { 9136 ctl_set_invalid_field(ctsio, 9137 /*sks_valid*/ 1, 9138 /*command*/ 1, 9139 /*field*/ 6, 9140 /*bit_valid*/ 0, 9141 /*bit*/ 0); 9142 ctl_done((union ctl_io *)ctsio); 9143 return (retval); 9144 } 9145 9146 lun_datalen = sizeof(*lun_data) + 9147 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9148 9149 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9150 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9151 ctsio->kern_sg_entries = 0; 9152 9153 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9154 9155 mtx_lock(&softc->ctl_lock); 9156 for (targ_lun_id = 0, num_filled = 0; 9157 targ_lun_id < num_port_luns && num_filled < num_luns; 9158 targ_lun_id++) { 9159 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9160 if (lun_id == UINT32_MAX) 9161 continue; 9162 lun = softc->ctl_luns[lun_id]; 9163 if (lun == NULL) 9164 continue; 9165 9166 be64enc(lun_data->luns[num_filled++].lundata, 9167 ctl_encode_lun(targ_lun_id)); 9168 9169 /* 9170 * According to SPC-3, rev 14 section 6.21: 9171 * 9172 * "The execution of a REPORT LUNS command to any valid and 9173 * installed logical unit shall clear the REPORTED LUNS DATA 9174 * HAS CHANGED unit attention condition for all logical 9175 * units of that target with respect to the requesting 9176 * initiator. A valid and installed logical unit is one 9177 * having a PERIPHERAL QUALIFIER of 000b in the standard 9178 * INQUIRY data (see 6.4.2)." 9179 * 9180 * If request_lun is NULL, the LUN this report luns command 9181 * was issued to is either disabled or doesn't exist. In that 9182 * case, we shouldn't clear any pending lun change unit 9183 * attention. 9184 */ 9185 if (request_lun != NULL) { 9186 mtx_lock(&lun->lun_lock); 9187 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9188 mtx_unlock(&lun->lun_lock); 9189 } 9190 } 9191 mtx_unlock(&softc->ctl_lock); 9192 9193 /* 9194 * It's quite possible that we've returned fewer LUNs than we allocated 9195 * space for. Trim it. 9196 */ 9197 lun_datalen = sizeof(*lun_data) + 9198 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9199 ctsio->kern_rel_offset = 0; 9200 ctsio->kern_sg_entries = 0; 9201 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9202 ctsio->kern_total_len = ctsio->kern_data_len; 9203 9204 /* 9205 * We set this to the actual data length, regardless of how much 9206 * space we actually have to return results. If the user looks at 9207 * this value, he'll know whether or not he allocated enough space 9208 * and reissue the command if necessary. We don't support well 9209 * known logical units, so if the user asks for that, return none. 9210 */ 9211 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9212 9213 /* 9214 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9215 * this request. 9216 */ 9217 ctl_set_success(ctsio); 9218 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9219 ctsio->be_move_done = ctl_config_move_done; 9220 ctl_datamove((union ctl_io *)ctsio); 9221 return (retval); 9222 } 9223 9224 int 9225 ctl_request_sense(struct ctl_scsiio *ctsio) 9226 { 9227 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9228 struct ctl_lun *lun = CTL_LUN(ctsio); 9229 struct scsi_request_sense *cdb; 9230 struct scsi_sense_data *sense_ptr, *ps; 9231 uint32_t initidx; 9232 int have_error; 9233 u_int sense_len = SSD_FULL_SIZE; 9234 scsi_sense_data_type sense_format; 9235 ctl_ua_type ua_type; 9236 uint8_t asc = 0, ascq = 0; 9237 9238 cdb = (struct scsi_request_sense *)ctsio->cdb; 9239 9240 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9241 9242 /* 9243 * Determine which sense format the user wants. 9244 */ 9245 if (cdb->byte2 & SRS_DESC) 9246 sense_format = SSD_TYPE_DESC; 9247 else 9248 sense_format = SSD_TYPE_FIXED; 9249 9250 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9251 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9252 ctsio->kern_sg_entries = 0; 9253 ctsio->kern_rel_offset = 0; 9254 9255 /* 9256 * struct scsi_sense_data, which is currently set to 256 bytes, is 9257 * larger than the largest allowed value for the length field in the 9258 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9259 */ 9260 ctsio->kern_data_len = cdb->length; 9261 ctsio->kern_total_len = cdb->length; 9262 9263 /* 9264 * If we don't have a LUN, we don't have any pending sense. 9265 */ 9266 if (lun == NULL || 9267 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9268 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9269 /* "Logical unit not supported" */ 9270 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9271 /*current_error*/ 1, 9272 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9273 /*asc*/ 0x25, 9274 /*ascq*/ 0x00, 9275 SSD_ELEM_NONE); 9276 goto send; 9277 } 9278 9279 have_error = 0; 9280 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9281 /* 9282 * Check for pending sense, and then for pending unit attentions. 9283 * Pending sense gets returned first, then pending unit attentions. 9284 */ 9285 mtx_lock(&lun->lun_lock); 9286 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9287 if (ps != NULL) 9288 ps += initidx % CTL_MAX_INIT_PER_PORT; 9289 if (ps != NULL && ps->error_code != 0) { 9290 scsi_sense_data_type stored_format; 9291 9292 /* 9293 * Check to see which sense format was used for the stored 9294 * sense data. 9295 */ 9296 stored_format = scsi_sense_type(ps); 9297 9298 /* 9299 * If the user requested a different sense format than the 9300 * one we stored, then we need to convert it to the other 9301 * format. If we're going from descriptor to fixed format 9302 * sense data, we may lose things in translation, depending 9303 * on what options were used. 9304 * 9305 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9306 * for some reason we'll just copy it out as-is. 9307 */ 9308 if ((stored_format == SSD_TYPE_FIXED) 9309 && (sense_format == SSD_TYPE_DESC)) 9310 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9311 ps, (struct scsi_sense_data_desc *)sense_ptr); 9312 else if ((stored_format == SSD_TYPE_DESC) 9313 && (sense_format == SSD_TYPE_FIXED)) 9314 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9315 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9316 else 9317 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9318 9319 ps->error_code = 0; 9320 have_error = 1; 9321 } else { 9322 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9323 sense_format); 9324 if (ua_type != CTL_UA_NONE) 9325 have_error = 1; 9326 } 9327 if (have_error == 0) { 9328 /* 9329 * Report informational exception if have one and allowed. 9330 */ 9331 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9332 asc = lun->ie_asc; 9333 ascq = lun->ie_ascq; 9334 } 9335 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9336 /*current_error*/ 1, 9337 /*sense_key*/ SSD_KEY_NO_SENSE, 9338 /*asc*/ asc, 9339 /*ascq*/ ascq, 9340 SSD_ELEM_NONE); 9341 } 9342 mtx_unlock(&lun->lun_lock); 9343 9344 send: 9345 /* 9346 * We report the SCSI status as OK, since the status of the command 9347 * itself is OK. We're reporting sense as parameter data. 9348 */ 9349 ctl_set_success(ctsio); 9350 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9351 ctsio->be_move_done = ctl_config_move_done; 9352 ctl_datamove((union ctl_io *)ctsio); 9353 return (CTL_RETVAL_COMPLETE); 9354 } 9355 9356 int 9357 ctl_tur(struct ctl_scsiio *ctsio) 9358 { 9359 9360 CTL_DEBUG_PRINT(("ctl_tur\n")); 9361 9362 ctl_set_success(ctsio); 9363 ctl_done((union ctl_io *)ctsio); 9364 9365 return (CTL_RETVAL_COMPLETE); 9366 } 9367 9368 /* 9369 * SCSI VPD page 0x00, the Supported VPD Pages page. 9370 */ 9371 static int 9372 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9373 { 9374 struct ctl_lun *lun = CTL_LUN(ctsio); 9375 struct scsi_vpd_supported_pages *pages; 9376 int sup_page_size; 9377 int p; 9378 9379 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9380 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9381 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9382 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9383 ctsio->kern_rel_offset = 0; 9384 ctsio->kern_sg_entries = 0; 9385 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9386 ctsio->kern_total_len = ctsio->kern_data_len; 9387 9388 /* 9389 * The control device is always connected. The disk device, on the 9390 * other hand, may not be online all the time. Need to change this 9391 * to figure out whether the disk device is actually online or not. 9392 */ 9393 if (lun != NULL) 9394 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9395 lun->be_lun->lun_type; 9396 else 9397 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9398 9399 p = 0; 9400 /* Supported VPD pages */ 9401 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9402 /* Serial Number */ 9403 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9404 /* Device Identification */ 9405 pages->page_list[p++] = SVPD_DEVICE_ID; 9406 /* Extended INQUIRY Data */ 9407 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9408 /* Mode Page Policy */ 9409 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9410 /* SCSI Ports */ 9411 pages->page_list[p++] = SVPD_SCSI_PORTS; 9412 /* Third-party Copy */ 9413 pages->page_list[p++] = SVPD_SCSI_TPC; 9414 /* SCSI Feature Sets */ 9415 pages->page_list[p++] = SVPD_SCSI_SFS; 9416 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9417 /* Block limits */ 9418 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9419 /* Block Device Characteristics */ 9420 pages->page_list[p++] = SVPD_BDC; 9421 /* Logical Block Provisioning */ 9422 pages->page_list[p++] = SVPD_LBP; 9423 } 9424 pages->length = p; 9425 9426 ctl_set_success(ctsio); 9427 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9428 ctsio->be_move_done = ctl_config_move_done; 9429 ctl_datamove((union ctl_io *)ctsio); 9430 return (CTL_RETVAL_COMPLETE); 9431 } 9432 9433 /* 9434 * SCSI VPD page 0x80, the Unit Serial Number page. 9435 */ 9436 static int 9437 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9438 { 9439 struct ctl_lun *lun = CTL_LUN(ctsio); 9440 struct scsi_vpd_unit_serial_number *sn_ptr; 9441 int data_len; 9442 9443 data_len = 4 + CTL_SN_LEN; 9444 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9445 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9446 ctsio->kern_rel_offset = 0; 9447 ctsio->kern_sg_entries = 0; 9448 ctsio->kern_data_len = min(data_len, alloc_len); 9449 ctsio->kern_total_len = ctsio->kern_data_len; 9450 9451 /* 9452 * The control device is always connected. The disk device, on the 9453 * other hand, may not be online all the time. Need to change this 9454 * to figure out whether the disk device is actually online or not. 9455 */ 9456 if (lun != NULL) 9457 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9458 lun->be_lun->lun_type; 9459 else 9460 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9461 9462 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9463 sn_ptr->length = CTL_SN_LEN; 9464 /* 9465 * If we don't have a LUN, we just leave the serial number as 9466 * all spaces. 9467 */ 9468 if (lun != NULL) { 9469 strncpy((char *)sn_ptr->serial_num, 9470 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9471 } else 9472 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9473 9474 ctl_set_success(ctsio); 9475 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9476 ctsio->be_move_done = ctl_config_move_done; 9477 ctl_datamove((union ctl_io *)ctsio); 9478 return (CTL_RETVAL_COMPLETE); 9479 } 9480 9481 /* 9482 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9483 */ 9484 static int 9485 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9486 { 9487 struct ctl_lun *lun = CTL_LUN(ctsio); 9488 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9489 int data_len; 9490 9491 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9492 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9493 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9494 ctsio->kern_sg_entries = 0; 9495 ctsio->kern_rel_offset = 0; 9496 ctsio->kern_data_len = min(data_len, alloc_len); 9497 ctsio->kern_total_len = ctsio->kern_data_len; 9498 9499 /* 9500 * The control device is always connected. The disk device, on the 9501 * other hand, may not be online all the time. 9502 */ 9503 if (lun != NULL) 9504 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9505 lun->be_lun->lun_type; 9506 else 9507 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9508 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9509 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9510 /* 9511 * We support head of queue, ordered and simple tags. 9512 */ 9513 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9514 /* 9515 * Volatile cache supported. 9516 */ 9517 eid_ptr->flags3 = SVPD_EID_V_SUP; 9518 9519 /* 9520 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9521 * attention for a particular IT nexus on all LUNs once we report 9522 * it to that nexus once. This bit is required as of SPC-4. 9523 */ 9524 eid_ptr->flags4 = SVPD_EID_LUICLR; 9525 9526 /* 9527 * We support revert to defaults (RTD) bit in MODE SELECT. 9528 */ 9529 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9530 9531 /* 9532 * XXX KDM in order to correctly answer this, we would need 9533 * information from the SIM to determine how much sense data it 9534 * can send. So this would really be a path inquiry field, most 9535 * likely. This can be set to a maximum of 252 according to SPC-4, 9536 * but the hardware may or may not be able to support that much. 9537 * 0 just means that the maximum sense data length is not reported. 9538 */ 9539 eid_ptr->max_sense_length = 0; 9540 9541 ctl_set_success(ctsio); 9542 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9543 ctsio->be_move_done = ctl_config_move_done; 9544 ctl_datamove((union ctl_io *)ctsio); 9545 return (CTL_RETVAL_COMPLETE); 9546 } 9547 9548 static int 9549 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9550 { 9551 struct ctl_lun *lun = CTL_LUN(ctsio); 9552 struct scsi_vpd_mode_page_policy *mpp_ptr; 9553 int data_len; 9554 9555 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9556 sizeof(struct scsi_vpd_mode_page_policy_descr); 9557 9558 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9559 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9560 ctsio->kern_rel_offset = 0; 9561 ctsio->kern_sg_entries = 0; 9562 ctsio->kern_data_len = min(data_len, alloc_len); 9563 ctsio->kern_total_len = ctsio->kern_data_len; 9564 9565 /* 9566 * The control device is always connected. The disk device, on the 9567 * other hand, may not be online all the time. 9568 */ 9569 if (lun != NULL) 9570 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9571 lun->be_lun->lun_type; 9572 else 9573 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9574 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9575 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9576 mpp_ptr->descr[0].page_code = 0x3f; 9577 mpp_ptr->descr[0].subpage_code = 0xff; 9578 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9579 9580 ctl_set_success(ctsio); 9581 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9582 ctsio->be_move_done = ctl_config_move_done; 9583 ctl_datamove((union ctl_io *)ctsio); 9584 return (CTL_RETVAL_COMPLETE); 9585 } 9586 9587 /* 9588 * SCSI VPD page 0x83, the Device Identification page. 9589 */ 9590 static int 9591 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9592 { 9593 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9594 struct ctl_port *port = CTL_PORT(ctsio); 9595 struct ctl_lun *lun = CTL_LUN(ctsio); 9596 struct scsi_vpd_device_id *devid_ptr; 9597 struct scsi_vpd_id_descriptor *desc; 9598 int data_len, g; 9599 uint8_t proto; 9600 9601 data_len = sizeof(struct scsi_vpd_device_id) + 9602 sizeof(struct scsi_vpd_id_descriptor) + 9603 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9604 sizeof(struct scsi_vpd_id_descriptor) + 9605 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9606 if (lun && lun->lun_devid) 9607 data_len += lun->lun_devid->len; 9608 if (port && port->port_devid) 9609 data_len += port->port_devid->len; 9610 if (port && port->target_devid) 9611 data_len += port->target_devid->len; 9612 9613 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9614 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9615 ctsio->kern_sg_entries = 0; 9616 ctsio->kern_rel_offset = 0; 9617 ctsio->kern_sg_entries = 0; 9618 ctsio->kern_data_len = min(data_len, alloc_len); 9619 ctsio->kern_total_len = ctsio->kern_data_len; 9620 9621 /* 9622 * The control device is always connected. The disk device, on the 9623 * other hand, may not be online all the time. 9624 */ 9625 if (lun != NULL) 9626 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9627 lun->be_lun->lun_type; 9628 else 9629 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9630 devid_ptr->page_code = SVPD_DEVICE_ID; 9631 scsi_ulto2b(data_len - 4, devid_ptr->length); 9632 9633 if (port && port->port_type == CTL_PORT_FC) 9634 proto = SCSI_PROTO_FC << 4; 9635 else if (port && port->port_type == CTL_PORT_SAS) 9636 proto = SCSI_PROTO_SAS << 4; 9637 else if (port && port->port_type == CTL_PORT_ISCSI) 9638 proto = SCSI_PROTO_ISCSI << 4; 9639 else 9640 proto = SCSI_PROTO_SPI << 4; 9641 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9642 9643 /* 9644 * We're using a LUN association here. i.e., this device ID is a 9645 * per-LUN identifier. 9646 */ 9647 if (lun && lun->lun_devid) { 9648 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9649 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9650 lun->lun_devid->len); 9651 } 9652 9653 /* 9654 * This is for the WWPN which is a port association. 9655 */ 9656 if (port && port->port_devid) { 9657 memcpy(desc, port->port_devid->data, port->port_devid->len); 9658 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9659 port->port_devid->len); 9660 } 9661 9662 /* 9663 * This is for the Relative Target Port(type 4h) identifier 9664 */ 9665 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9666 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9667 SVPD_ID_TYPE_RELTARG; 9668 desc->length = 4; 9669 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9670 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9671 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9672 9673 /* 9674 * This is for the Target Port Group(type 5h) identifier 9675 */ 9676 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9677 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9678 SVPD_ID_TYPE_TPORTGRP; 9679 desc->length = 4; 9680 if (softc->is_single || 9681 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9682 g = 1; 9683 else 9684 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9685 scsi_ulto2b(g, &desc->identifier[2]); 9686 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9687 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9688 9689 /* 9690 * This is for the Target identifier 9691 */ 9692 if (port && port->target_devid) { 9693 memcpy(desc, port->target_devid->data, port->target_devid->len); 9694 } 9695 9696 ctl_set_success(ctsio); 9697 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9698 ctsio->be_move_done = ctl_config_move_done; 9699 ctl_datamove((union ctl_io *)ctsio); 9700 return (CTL_RETVAL_COMPLETE); 9701 } 9702 9703 static int 9704 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9705 { 9706 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9707 struct ctl_lun *lun = CTL_LUN(ctsio); 9708 struct scsi_vpd_scsi_ports *sp; 9709 struct scsi_vpd_port_designation *pd; 9710 struct scsi_vpd_port_designation_cont *pdc; 9711 struct ctl_port *port; 9712 int data_len, num_target_ports, iid_len, id_len; 9713 9714 num_target_ports = 0; 9715 iid_len = 0; 9716 id_len = 0; 9717 mtx_lock(&softc->ctl_lock); 9718 STAILQ_FOREACH(port, &softc->port_list, links) { 9719 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9720 continue; 9721 if (lun != NULL && 9722 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9723 continue; 9724 num_target_ports++; 9725 if (port->init_devid) 9726 iid_len += port->init_devid->len; 9727 if (port->port_devid) 9728 id_len += port->port_devid->len; 9729 } 9730 mtx_unlock(&softc->ctl_lock); 9731 9732 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9733 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9734 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9735 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9736 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9737 ctsio->kern_sg_entries = 0; 9738 ctsio->kern_rel_offset = 0; 9739 ctsio->kern_sg_entries = 0; 9740 ctsio->kern_data_len = min(data_len, alloc_len); 9741 ctsio->kern_total_len = ctsio->kern_data_len; 9742 9743 /* 9744 * The control device is always connected. The disk device, on the 9745 * other hand, may not be online all the time. Need to change this 9746 * to figure out whether the disk device is actually online or not. 9747 */ 9748 if (lun != NULL) 9749 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9750 lun->be_lun->lun_type; 9751 else 9752 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9753 9754 sp->page_code = SVPD_SCSI_PORTS; 9755 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9756 sp->page_length); 9757 pd = &sp->design[0]; 9758 9759 mtx_lock(&softc->ctl_lock); 9760 STAILQ_FOREACH(port, &softc->port_list, links) { 9761 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9762 continue; 9763 if (lun != NULL && 9764 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9765 continue; 9766 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9767 if (port->init_devid) { 9768 iid_len = port->init_devid->len; 9769 memcpy(pd->initiator_transportid, 9770 port->init_devid->data, port->init_devid->len); 9771 } else 9772 iid_len = 0; 9773 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9774 pdc = (struct scsi_vpd_port_designation_cont *) 9775 (&pd->initiator_transportid[iid_len]); 9776 if (port->port_devid) { 9777 id_len = port->port_devid->len; 9778 memcpy(pdc->target_port_descriptors, 9779 port->port_devid->data, port->port_devid->len); 9780 } else 9781 id_len = 0; 9782 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9783 pd = (struct scsi_vpd_port_designation *) 9784 ((uint8_t *)pdc->target_port_descriptors + id_len); 9785 } 9786 mtx_unlock(&softc->ctl_lock); 9787 9788 ctl_set_success(ctsio); 9789 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9790 ctsio->be_move_done = ctl_config_move_done; 9791 ctl_datamove((union ctl_io *)ctsio); 9792 return (CTL_RETVAL_COMPLETE); 9793 } 9794 9795 static int 9796 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len) 9797 { 9798 struct ctl_lun *lun = CTL_LUN(ctsio); 9799 struct scsi_vpd_sfs *sfs_ptr; 9800 int sfs_page_size, n; 9801 9802 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2; 9803 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO); 9804 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr; 9805 ctsio->kern_sg_entries = 0; 9806 ctsio->kern_rel_offset = 0; 9807 ctsio->kern_sg_entries = 0; 9808 ctsio->kern_data_len = min(sfs_page_size, alloc_len); 9809 ctsio->kern_total_len = ctsio->kern_data_len; 9810 9811 /* 9812 * The control device is always connected. The disk device, on the 9813 * other hand, may not be online all the time. Need to change this 9814 * to figure out whether the disk device is actually online or not. 9815 */ 9816 if (lun != NULL) 9817 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9818 lun->be_lun->lun_type; 9819 else 9820 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9821 9822 sfs_ptr->page_code = SVPD_SCSI_SFS; 9823 n = 0; 9824 /* Discovery 2016 */ 9825 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]); 9826 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9827 /* SBC Base 2016 */ 9828 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]); 9829 /* SBC Base 2010 */ 9830 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]); 9831 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9832 /* Basic Provisioning 2016 */ 9833 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]); 9834 } 9835 /* Drive Maintenance 2016 */ 9836 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]); 9837 } 9838 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length); 9839 9840 ctl_set_success(ctsio); 9841 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9842 ctsio->be_move_done = ctl_config_move_done; 9843 ctl_datamove((union ctl_io *)ctsio); 9844 return (CTL_RETVAL_COMPLETE); 9845 } 9846 9847 static int 9848 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9849 { 9850 struct ctl_lun *lun = CTL_LUN(ctsio); 9851 struct scsi_vpd_block_limits *bl_ptr; 9852 const char *val; 9853 uint64_t ival; 9854 9855 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9856 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9857 ctsio->kern_sg_entries = 0; 9858 ctsio->kern_rel_offset = 0; 9859 ctsio->kern_sg_entries = 0; 9860 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9861 ctsio->kern_total_len = ctsio->kern_data_len; 9862 9863 /* 9864 * The control device is always connected. The disk device, on the 9865 * other hand, may not be online all the time. Need to change this 9866 * to figure out whether the disk device is actually online or not. 9867 */ 9868 if (lun != NULL) 9869 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9870 lun->be_lun->lun_type; 9871 else 9872 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9873 9874 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9875 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9876 bl_ptr->max_cmp_write_len = 0xff; 9877 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9878 if (lun != NULL) { 9879 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9880 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9881 ival = 0xffffffff; 9882 val = dnvlist_get_string(lun->be_lun->options, 9883 "unmap_max_lba", NULL); 9884 if (val != NULL) 9885 ctl_expand_number(val, &ival); 9886 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9887 ival = 0xffffffff; 9888 val = dnvlist_get_string(lun->be_lun->options, 9889 "unmap_max_descr", NULL); 9890 if (val != NULL) 9891 ctl_expand_number(val, &ival); 9892 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9893 if (lun->be_lun->ublockexp != 0) { 9894 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9895 bl_ptr->opt_unmap_grain); 9896 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9897 bl_ptr->unmap_grain_align); 9898 } 9899 } 9900 scsi_ulto4b(lun->be_lun->atomicblock, 9901 bl_ptr->max_atomic_transfer_length); 9902 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9903 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9904 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9905 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9906 ival = UINT64_MAX; 9907 val = dnvlist_get_string(lun->be_lun->options, 9908 "write_same_max_lba", NULL); 9909 if (val != NULL) 9910 ctl_expand_number(val, &ival); 9911 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9912 if (lun->be_lun->maxlba + 1 > ival) 9913 bl_ptr->flags |= SVPD_BL_WSNZ; 9914 } 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_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9925 { 9926 struct ctl_lun *lun = CTL_LUN(ctsio); 9927 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9928 const char *value; 9929 u_int i; 9930 9931 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9932 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9933 ctsio->kern_sg_entries = 0; 9934 ctsio->kern_rel_offset = 0; 9935 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 9936 ctsio->kern_total_len = ctsio->kern_data_len; 9937 9938 /* 9939 * The control device is always connected. The disk device, on the 9940 * other hand, may not be online all the time. Need to change this 9941 * to figure out whether the disk device is actually online or not. 9942 */ 9943 if (lun != NULL) 9944 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9945 lun->be_lun->lun_type; 9946 else 9947 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9948 bdc_ptr->page_code = SVPD_BDC; 9949 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9950 if (lun != NULL && 9951 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 9952 i = strtol(value, NULL, 0); 9953 else 9954 i = CTL_DEFAULT_ROTATION_RATE; 9955 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9956 if (lun != NULL && 9957 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 9958 i = strtol(value, NULL, 0); 9959 else 9960 i = 0; 9961 bdc_ptr->wab_wac_ff = (i & 0x0f); 9962 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS; 9963 9964 ctl_set_success(ctsio); 9965 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9966 ctsio->be_move_done = ctl_config_move_done; 9967 ctl_datamove((union ctl_io *)ctsio); 9968 return (CTL_RETVAL_COMPLETE); 9969 } 9970 9971 static int 9972 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9973 { 9974 struct ctl_lun *lun = CTL_LUN(ctsio); 9975 struct scsi_vpd_logical_block_prov *lbp_ptr; 9976 const char *value; 9977 9978 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9979 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9980 ctsio->kern_sg_entries = 0; 9981 ctsio->kern_rel_offset = 0; 9982 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 9983 ctsio->kern_total_len = ctsio->kern_data_len; 9984 9985 /* 9986 * The control device is always connected. The disk device, on the 9987 * other hand, may not be online all the time. Need to change this 9988 * to figure out whether the disk device is actually online or not. 9989 */ 9990 if (lun != NULL) 9991 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9992 lun->be_lun->lun_type; 9993 else 9994 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9995 9996 lbp_ptr->page_code = SVPD_LBP; 9997 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9998 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9999 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10000 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10001 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10002 value = dnvlist_get_string(lun->be_lun->options, 10003 "provisioning_type", NULL); 10004 if (value != NULL) { 10005 if (strcmp(value, "resource") == 0) 10006 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10007 else if (strcmp(value, "thin") == 0) 10008 lbp_ptr->prov_type = SVPD_LBP_THIN; 10009 } else 10010 lbp_ptr->prov_type = SVPD_LBP_THIN; 10011 } 10012 10013 ctl_set_success(ctsio); 10014 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10015 ctsio->be_move_done = ctl_config_move_done; 10016 ctl_datamove((union ctl_io *)ctsio); 10017 return (CTL_RETVAL_COMPLETE); 10018 } 10019 10020 /* 10021 * INQUIRY with the EVPD bit set. 10022 */ 10023 static int 10024 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10025 { 10026 struct ctl_lun *lun = CTL_LUN(ctsio); 10027 struct scsi_inquiry *cdb; 10028 int alloc_len, retval; 10029 10030 cdb = (struct scsi_inquiry *)ctsio->cdb; 10031 alloc_len = scsi_2btoul(cdb->length); 10032 10033 switch (cdb->page_code) { 10034 case SVPD_SUPPORTED_PAGES: 10035 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10036 break; 10037 case SVPD_UNIT_SERIAL_NUMBER: 10038 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10039 break; 10040 case SVPD_DEVICE_ID: 10041 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10042 break; 10043 case SVPD_EXTENDED_INQUIRY_DATA: 10044 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10045 break; 10046 case SVPD_MODE_PAGE_POLICY: 10047 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10048 break; 10049 case SVPD_SCSI_PORTS: 10050 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10051 break; 10052 case SVPD_SCSI_TPC: 10053 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10054 break; 10055 case SVPD_SCSI_SFS: 10056 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len); 10057 break; 10058 case SVPD_BLOCK_LIMITS: 10059 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10060 goto err; 10061 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10062 break; 10063 case SVPD_BDC: 10064 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10065 goto err; 10066 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10067 break; 10068 case SVPD_LBP: 10069 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10070 goto err; 10071 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10072 break; 10073 default: 10074 err: 10075 ctl_set_invalid_field(ctsio, 10076 /*sks_valid*/ 1, 10077 /*command*/ 1, 10078 /*field*/ 2, 10079 /*bit_valid*/ 0, 10080 /*bit*/ 0); 10081 ctl_done((union ctl_io *)ctsio); 10082 retval = CTL_RETVAL_COMPLETE; 10083 break; 10084 } 10085 10086 return (retval); 10087 } 10088 10089 /* 10090 * Standard INQUIRY data. 10091 */ 10092 static int 10093 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10094 { 10095 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10096 struct ctl_port *port = CTL_PORT(ctsio); 10097 struct ctl_lun *lun = CTL_LUN(ctsio); 10098 struct scsi_inquiry_data *inq_ptr; 10099 struct scsi_inquiry *cdb; 10100 const char *val; 10101 uint32_t alloc_len, data_len; 10102 ctl_port_type port_type; 10103 10104 port_type = port->port_type; 10105 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10106 port_type = CTL_PORT_SCSI; 10107 10108 cdb = (struct scsi_inquiry *)ctsio->cdb; 10109 alloc_len = scsi_2btoul(cdb->length); 10110 10111 /* 10112 * We malloc the full inquiry data size here and fill it 10113 * in. If the user only asks for less, we'll give him 10114 * that much. 10115 */ 10116 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10117 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10118 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10119 ctsio->kern_sg_entries = 0; 10120 ctsio->kern_rel_offset = 0; 10121 ctsio->kern_data_len = min(data_len, alloc_len); 10122 ctsio->kern_total_len = ctsio->kern_data_len; 10123 10124 if (lun != NULL) { 10125 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10126 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10127 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10128 lun->be_lun->lun_type; 10129 } else { 10130 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10131 lun->be_lun->lun_type; 10132 } 10133 if (lun->flags & CTL_LUN_REMOVABLE) 10134 inq_ptr->dev_qual2 |= SID_RMB; 10135 } else 10136 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10137 10138 /* RMB in byte 2 is 0 */ 10139 inq_ptr->version = SCSI_REV_SPC5; 10140 10141 /* 10142 * According to SAM-3, even if a device only supports a single 10143 * level of LUN addressing, it should still set the HISUP bit: 10144 * 10145 * 4.9.1 Logical unit numbers overview 10146 * 10147 * All logical unit number formats described in this standard are 10148 * hierarchical in structure even when only a single level in that 10149 * hierarchy is used. The HISUP bit shall be set to one in the 10150 * standard INQUIRY data (see SPC-2) when any logical unit number 10151 * format described in this standard is used. Non-hierarchical 10152 * formats are outside the scope of this standard. 10153 * 10154 * Therefore we set the HiSup bit here. 10155 * 10156 * The response format is 2, per SPC-3. 10157 */ 10158 inq_ptr->response_format = SID_HiSup | 2; 10159 10160 inq_ptr->additional_length = data_len - 10161 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10162 CTL_DEBUG_PRINT(("additional_length = %d\n", 10163 inq_ptr->additional_length)); 10164 10165 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10166 if (port_type == CTL_PORT_SCSI) 10167 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10168 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10169 inq_ptr->flags = SID_CmdQue; 10170 if (port_type == CTL_PORT_SCSI) 10171 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10172 10173 /* 10174 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10175 * We have 8 bytes for the vendor name, and 16 bytes for the device 10176 * name and 4 bytes for the revision. 10177 */ 10178 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10179 "vendor", NULL)) == NULL) { 10180 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10181 } else { 10182 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10183 strncpy(inq_ptr->vendor, val, 10184 min(sizeof(inq_ptr->vendor), strlen(val))); 10185 } 10186 if (lun == NULL) { 10187 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10188 sizeof(inq_ptr->product)); 10189 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10190 NULL)) == NULL) { 10191 switch (lun->be_lun->lun_type) { 10192 case T_DIRECT: 10193 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10194 sizeof(inq_ptr->product)); 10195 break; 10196 case T_PROCESSOR: 10197 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10198 sizeof(inq_ptr->product)); 10199 break; 10200 case T_CDROM: 10201 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10202 sizeof(inq_ptr->product)); 10203 break; 10204 default: 10205 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10206 sizeof(inq_ptr->product)); 10207 break; 10208 } 10209 } else { 10210 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10211 strncpy(inq_ptr->product, val, 10212 min(sizeof(inq_ptr->product), strlen(val))); 10213 } 10214 10215 /* 10216 * XXX make this a macro somewhere so it automatically gets 10217 * incremented when we make changes. 10218 */ 10219 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10220 "revision", NULL)) == NULL) { 10221 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10222 } else { 10223 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10224 strncpy(inq_ptr->revision, val, 10225 min(sizeof(inq_ptr->revision), strlen(val))); 10226 } 10227 10228 /* 10229 * For parallel SCSI, we support double transition and single 10230 * transition clocking. We also support QAS (Quick Arbitration 10231 * and Selection) and Information Unit transfers on both the 10232 * control and array devices. 10233 */ 10234 if (port_type == CTL_PORT_SCSI) 10235 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10236 SID_SPI_IUS; 10237 10238 /* SAM-6 (no version claimed) */ 10239 scsi_ulto2b(0x00C0, inq_ptr->version1); 10240 /* SPC-5 (no version claimed) */ 10241 scsi_ulto2b(0x05C0, inq_ptr->version2); 10242 if (port_type == CTL_PORT_FC) { 10243 /* FCP-2 ANSI INCITS.350:2003 */ 10244 scsi_ulto2b(0x0917, inq_ptr->version3); 10245 } else if (port_type == CTL_PORT_SCSI) { 10246 /* SPI-4 ANSI INCITS.362:200x */ 10247 scsi_ulto2b(0x0B56, inq_ptr->version3); 10248 } else if (port_type == CTL_PORT_ISCSI) { 10249 /* iSCSI (no version claimed) */ 10250 scsi_ulto2b(0x0960, inq_ptr->version3); 10251 } else if (port_type == CTL_PORT_SAS) { 10252 /* SAS (no version claimed) */ 10253 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10254 } else if (port_type == CTL_PORT_UMASS) { 10255 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10256 scsi_ulto2b(0x1730, inq_ptr->version3); 10257 } 10258 10259 if (lun == NULL) { 10260 /* SBC-4 (no version claimed) */ 10261 scsi_ulto2b(0x0600, inq_ptr->version4); 10262 } else { 10263 switch (lun->be_lun->lun_type) { 10264 case T_DIRECT: 10265 /* SBC-4 (no version claimed) */ 10266 scsi_ulto2b(0x0600, inq_ptr->version4); 10267 break; 10268 case T_PROCESSOR: 10269 break; 10270 case T_CDROM: 10271 /* MMC-6 (no version claimed) */ 10272 scsi_ulto2b(0x04E0, inq_ptr->version4); 10273 break; 10274 default: 10275 break; 10276 } 10277 } 10278 10279 ctl_set_success(ctsio); 10280 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10281 ctsio->be_move_done = ctl_config_move_done; 10282 ctl_datamove((union ctl_io *)ctsio); 10283 return (CTL_RETVAL_COMPLETE); 10284 } 10285 10286 int 10287 ctl_inquiry(struct ctl_scsiio *ctsio) 10288 { 10289 struct scsi_inquiry *cdb; 10290 int retval; 10291 10292 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10293 10294 cdb = (struct scsi_inquiry *)ctsio->cdb; 10295 if (cdb->byte2 & SI_EVPD) 10296 retval = ctl_inquiry_evpd(ctsio); 10297 else if (cdb->page_code == 0) 10298 retval = ctl_inquiry_std(ctsio); 10299 else { 10300 ctl_set_invalid_field(ctsio, 10301 /*sks_valid*/ 1, 10302 /*command*/ 1, 10303 /*field*/ 2, 10304 /*bit_valid*/ 0, 10305 /*bit*/ 0); 10306 ctl_done((union ctl_io *)ctsio); 10307 return (CTL_RETVAL_COMPLETE); 10308 } 10309 10310 return (retval); 10311 } 10312 10313 int 10314 ctl_get_config(struct ctl_scsiio *ctsio) 10315 { 10316 struct ctl_lun *lun = CTL_LUN(ctsio); 10317 struct scsi_get_config_header *hdr; 10318 struct scsi_get_config_feature *feature; 10319 struct scsi_get_config *cdb; 10320 uint32_t alloc_len, data_len; 10321 int rt, starting; 10322 10323 cdb = (struct scsi_get_config *)ctsio->cdb; 10324 rt = (cdb->rt & SGC_RT_MASK); 10325 starting = scsi_2btoul(cdb->starting_feature); 10326 alloc_len = scsi_2btoul(cdb->length); 10327 10328 data_len = sizeof(struct scsi_get_config_header) + 10329 sizeof(struct scsi_get_config_feature) + 8 + 10330 sizeof(struct scsi_get_config_feature) + 8 + 10331 sizeof(struct scsi_get_config_feature) + 4 + 10332 sizeof(struct scsi_get_config_feature) + 4 + 10333 sizeof(struct scsi_get_config_feature) + 8 + 10334 sizeof(struct scsi_get_config_feature) + 10335 sizeof(struct scsi_get_config_feature) + 4 + 10336 sizeof(struct scsi_get_config_feature) + 4 + 10337 sizeof(struct scsi_get_config_feature) + 4 + 10338 sizeof(struct scsi_get_config_feature) + 4 + 10339 sizeof(struct scsi_get_config_feature) + 4 + 10340 sizeof(struct scsi_get_config_feature) + 4; 10341 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10342 ctsio->kern_sg_entries = 0; 10343 ctsio->kern_rel_offset = 0; 10344 10345 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10346 if (lun->flags & CTL_LUN_NO_MEDIA) 10347 scsi_ulto2b(0x0000, hdr->current_profile); 10348 else 10349 scsi_ulto2b(0x0010, hdr->current_profile); 10350 feature = (struct scsi_get_config_feature *)(hdr + 1); 10351 10352 if (starting > 0x003b) 10353 goto done; 10354 if (starting > 0x003a) 10355 goto f3b; 10356 if (starting > 0x002b) 10357 goto f3a; 10358 if (starting > 0x002a) 10359 goto f2b; 10360 if (starting > 0x001f) 10361 goto f2a; 10362 if (starting > 0x001e) 10363 goto f1f; 10364 if (starting > 0x001d) 10365 goto f1e; 10366 if (starting > 0x0010) 10367 goto f1d; 10368 if (starting > 0x0003) 10369 goto f10; 10370 if (starting > 0x0002) 10371 goto f3; 10372 if (starting > 0x0001) 10373 goto f2; 10374 if (starting > 0x0000) 10375 goto f1; 10376 10377 /* Profile List */ 10378 scsi_ulto2b(0x0000, feature->feature_code); 10379 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10380 feature->add_length = 8; 10381 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10382 feature->feature_data[2] = 0x00; 10383 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10384 feature->feature_data[6] = 0x01; 10385 feature = (struct scsi_get_config_feature *) 10386 &feature->feature_data[feature->add_length]; 10387 10388 f1: /* Core */ 10389 scsi_ulto2b(0x0001, feature->feature_code); 10390 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10391 feature->add_length = 8; 10392 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10393 feature->feature_data[4] = 0x03; 10394 feature = (struct scsi_get_config_feature *) 10395 &feature->feature_data[feature->add_length]; 10396 10397 f2: /* Morphing */ 10398 scsi_ulto2b(0x0002, feature->feature_code); 10399 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10400 feature->add_length = 4; 10401 feature->feature_data[0] = 0x02; 10402 feature = (struct scsi_get_config_feature *) 10403 &feature->feature_data[feature->add_length]; 10404 10405 f3: /* Removable Medium */ 10406 scsi_ulto2b(0x0003, feature->feature_code); 10407 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10408 feature->add_length = 4; 10409 feature->feature_data[0] = 0x39; 10410 feature = (struct scsi_get_config_feature *) 10411 &feature->feature_data[feature->add_length]; 10412 10413 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10414 goto done; 10415 10416 f10: /* Random Read */ 10417 scsi_ulto2b(0x0010, feature->feature_code); 10418 feature->flags = 0x00; 10419 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10420 feature->flags |= SGC_F_CURRENT; 10421 feature->add_length = 8; 10422 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10423 scsi_ulto2b(1, &feature->feature_data[4]); 10424 feature->feature_data[6] = 0x00; 10425 feature = (struct scsi_get_config_feature *) 10426 &feature->feature_data[feature->add_length]; 10427 10428 f1d: /* Multi-Read */ 10429 scsi_ulto2b(0x001D, feature->feature_code); 10430 feature->flags = 0x00; 10431 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10432 feature->flags |= SGC_F_CURRENT; 10433 feature->add_length = 0; 10434 feature = (struct scsi_get_config_feature *) 10435 &feature->feature_data[feature->add_length]; 10436 10437 f1e: /* CD Read */ 10438 scsi_ulto2b(0x001E, feature->feature_code); 10439 feature->flags = 0x00; 10440 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10441 feature->flags |= SGC_F_CURRENT; 10442 feature->add_length = 4; 10443 feature->feature_data[0] = 0x00; 10444 feature = (struct scsi_get_config_feature *) 10445 &feature->feature_data[feature->add_length]; 10446 10447 f1f: /* DVD Read */ 10448 scsi_ulto2b(0x001F, feature->feature_code); 10449 feature->flags = 0x08; 10450 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10451 feature->flags |= SGC_F_CURRENT; 10452 feature->add_length = 4; 10453 feature->feature_data[0] = 0x01; 10454 feature->feature_data[2] = 0x03; 10455 feature = (struct scsi_get_config_feature *) 10456 &feature->feature_data[feature->add_length]; 10457 10458 f2a: /* DVD+RW */ 10459 scsi_ulto2b(0x002A, feature->feature_code); 10460 feature->flags = 0x04; 10461 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10462 feature->flags |= SGC_F_CURRENT; 10463 feature->add_length = 4; 10464 feature->feature_data[0] = 0x00; 10465 feature->feature_data[1] = 0x00; 10466 feature = (struct scsi_get_config_feature *) 10467 &feature->feature_data[feature->add_length]; 10468 10469 f2b: /* DVD+R */ 10470 scsi_ulto2b(0x002B, feature->feature_code); 10471 feature->flags = 0x00; 10472 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10473 feature->flags |= SGC_F_CURRENT; 10474 feature->add_length = 4; 10475 feature->feature_data[0] = 0x00; 10476 feature = (struct scsi_get_config_feature *) 10477 &feature->feature_data[feature->add_length]; 10478 10479 f3a: /* DVD+RW Dual Layer */ 10480 scsi_ulto2b(0x003A, feature->feature_code); 10481 feature->flags = 0x00; 10482 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10483 feature->flags |= SGC_F_CURRENT; 10484 feature->add_length = 4; 10485 feature->feature_data[0] = 0x00; 10486 feature->feature_data[1] = 0x00; 10487 feature = (struct scsi_get_config_feature *) 10488 &feature->feature_data[feature->add_length]; 10489 10490 f3b: /* DVD+R Dual Layer */ 10491 scsi_ulto2b(0x003B, feature->feature_code); 10492 feature->flags = 0x00; 10493 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10494 feature->flags |= SGC_F_CURRENT; 10495 feature->add_length = 4; 10496 feature->feature_data[0] = 0x00; 10497 feature = (struct scsi_get_config_feature *) 10498 &feature->feature_data[feature->add_length]; 10499 10500 done: 10501 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10502 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10503 feature = (struct scsi_get_config_feature *)(hdr + 1); 10504 if (scsi_2btoul(feature->feature_code) == starting) 10505 feature = (struct scsi_get_config_feature *) 10506 &feature->feature_data[feature->add_length]; 10507 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10508 } 10509 scsi_ulto4b(data_len - 4, hdr->data_length); 10510 ctsio->kern_data_len = min(data_len, alloc_len); 10511 ctsio->kern_total_len = ctsio->kern_data_len; 10512 10513 ctl_set_success(ctsio); 10514 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10515 ctsio->be_move_done = ctl_config_move_done; 10516 ctl_datamove((union ctl_io *)ctsio); 10517 return (CTL_RETVAL_COMPLETE); 10518 } 10519 10520 int 10521 ctl_get_event_status(struct ctl_scsiio *ctsio) 10522 { 10523 struct scsi_get_event_status_header *hdr; 10524 struct scsi_get_event_status *cdb; 10525 uint32_t alloc_len, data_len; 10526 10527 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10528 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10529 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10530 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10531 ctl_done((union ctl_io *)ctsio); 10532 return (CTL_RETVAL_COMPLETE); 10533 } 10534 alloc_len = scsi_2btoul(cdb->length); 10535 10536 data_len = sizeof(struct scsi_get_event_status_header); 10537 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10538 ctsio->kern_sg_entries = 0; 10539 ctsio->kern_rel_offset = 0; 10540 ctsio->kern_data_len = min(data_len, alloc_len); 10541 ctsio->kern_total_len = ctsio->kern_data_len; 10542 10543 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10544 scsi_ulto2b(0, hdr->descr_length); 10545 hdr->nea_class = SGESN_NEA; 10546 hdr->supported_class = 0; 10547 10548 ctl_set_success(ctsio); 10549 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10550 ctsio->be_move_done = ctl_config_move_done; 10551 ctl_datamove((union ctl_io *)ctsio); 10552 return (CTL_RETVAL_COMPLETE); 10553 } 10554 10555 int 10556 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10557 { 10558 struct scsi_mechanism_status_header *hdr; 10559 struct scsi_mechanism_status *cdb; 10560 uint32_t alloc_len, data_len; 10561 10562 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10563 alloc_len = scsi_2btoul(cdb->length); 10564 10565 data_len = sizeof(struct scsi_mechanism_status_header); 10566 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10567 ctsio->kern_sg_entries = 0; 10568 ctsio->kern_rel_offset = 0; 10569 ctsio->kern_data_len = min(data_len, alloc_len); 10570 ctsio->kern_total_len = ctsio->kern_data_len; 10571 10572 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10573 hdr->state1 = 0x00; 10574 hdr->state2 = 0xe0; 10575 scsi_ulto3b(0, hdr->lba); 10576 hdr->slots_num = 0; 10577 scsi_ulto2b(0, hdr->slots_length); 10578 10579 ctl_set_success(ctsio); 10580 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10581 ctsio->be_move_done = ctl_config_move_done; 10582 ctl_datamove((union ctl_io *)ctsio); 10583 return (CTL_RETVAL_COMPLETE); 10584 } 10585 10586 static void 10587 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10588 { 10589 10590 lba += 150; 10591 buf[0] = 0; 10592 buf[1] = bin2bcd((lba / 75) / 60); 10593 buf[2] = bin2bcd((lba / 75) % 60); 10594 buf[3] = bin2bcd(lba % 75); 10595 } 10596 10597 int 10598 ctl_read_toc(struct ctl_scsiio *ctsio) 10599 { 10600 struct ctl_lun *lun = CTL_LUN(ctsio); 10601 struct scsi_read_toc_hdr *hdr; 10602 struct scsi_read_toc_type01_descr *descr; 10603 struct scsi_read_toc *cdb; 10604 uint32_t alloc_len, data_len; 10605 int format, msf; 10606 10607 cdb = (struct scsi_read_toc *)ctsio->cdb; 10608 msf = (cdb->byte2 & CD_MSF) != 0; 10609 format = cdb->format; 10610 alloc_len = scsi_2btoul(cdb->data_len); 10611 10612 data_len = sizeof(struct scsi_read_toc_hdr); 10613 if (format == 0) 10614 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10615 else 10616 data_len += sizeof(struct scsi_read_toc_type01_descr); 10617 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10618 ctsio->kern_sg_entries = 0; 10619 ctsio->kern_rel_offset = 0; 10620 ctsio->kern_data_len = min(data_len, alloc_len); 10621 ctsio->kern_total_len = ctsio->kern_data_len; 10622 10623 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10624 if (format == 0) { 10625 scsi_ulto2b(0x12, hdr->data_length); 10626 hdr->first = 1; 10627 hdr->last = 1; 10628 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10629 descr->addr_ctl = 0x14; 10630 descr->track_number = 1; 10631 if (msf) 10632 ctl_ultomsf(0, descr->track_start); 10633 else 10634 scsi_ulto4b(0, descr->track_start); 10635 descr++; 10636 descr->addr_ctl = 0x14; 10637 descr->track_number = 0xaa; 10638 if (msf) 10639 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10640 else 10641 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10642 } else { 10643 scsi_ulto2b(0x0a, hdr->data_length); 10644 hdr->first = 1; 10645 hdr->last = 1; 10646 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10647 descr->addr_ctl = 0x14; 10648 descr->track_number = 1; 10649 if (msf) 10650 ctl_ultomsf(0, descr->track_start); 10651 else 10652 scsi_ulto4b(0, descr->track_start); 10653 } 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 /* 10663 * For known CDB types, parse the LBA and length. 10664 */ 10665 static int 10666 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10667 { 10668 10669 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10670 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10671 10672 switch (io->scsiio.cdb[0]) { 10673 case COMPARE_AND_WRITE: { 10674 struct scsi_compare_and_write *cdb; 10675 10676 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10677 10678 *lba = scsi_8btou64(cdb->addr); 10679 *len = cdb->length; 10680 break; 10681 } 10682 case READ_6: 10683 case WRITE_6: { 10684 struct scsi_rw_6 *cdb; 10685 10686 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10687 10688 *lba = scsi_3btoul(cdb->addr); 10689 /* only 5 bits are valid in the most significant address byte */ 10690 *lba &= 0x1fffff; 10691 *len = cdb->length; 10692 break; 10693 } 10694 case READ_10: 10695 case WRITE_10: { 10696 struct scsi_rw_10 *cdb; 10697 10698 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10699 10700 *lba = scsi_4btoul(cdb->addr); 10701 *len = scsi_2btoul(cdb->length); 10702 break; 10703 } 10704 case WRITE_VERIFY_10: { 10705 struct scsi_write_verify_10 *cdb; 10706 10707 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10708 10709 *lba = scsi_4btoul(cdb->addr); 10710 *len = scsi_2btoul(cdb->length); 10711 break; 10712 } 10713 case READ_12: 10714 case WRITE_12: { 10715 struct scsi_rw_12 *cdb; 10716 10717 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10718 10719 *lba = scsi_4btoul(cdb->addr); 10720 *len = scsi_4btoul(cdb->length); 10721 break; 10722 } 10723 case WRITE_VERIFY_12: { 10724 struct scsi_write_verify_12 *cdb; 10725 10726 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10727 10728 *lba = scsi_4btoul(cdb->addr); 10729 *len = scsi_4btoul(cdb->length); 10730 break; 10731 } 10732 case READ_16: 10733 case WRITE_16: { 10734 struct scsi_rw_16 *cdb; 10735 10736 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10737 10738 *lba = scsi_8btou64(cdb->addr); 10739 *len = scsi_4btoul(cdb->length); 10740 break; 10741 } 10742 case WRITE_ATOMIC_16: { 10743 struct scsi_write_atomic_16 *cdb; 10744 10745 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10746 10747 *lba = scsi_8btou64(cdb->addr); 10748 *len = scsi_2btoul(cdb->length); 10749 break; 10750 } 10751 case WRITE_VERIFY_16: { 10752 struct scsi_write_verify_16 *cdb; 10753 10754 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10755 10756 *lba = scsi_8btou64(cdb->addr); 10757 *len = scsi_4btoul(cdb->length); 10758 break; 10759 } 10760 case WRITE_SAME_10: { 10761 struct scsi_write_same_10 *cdb; 10762 10763 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10764 10765 *lba = scsi_4btoul(cdb->addr); 10766 *len = scsi_2btoul(cdb->length); 10767 break; 10768 } 10769 case WRITE_SAME_16: { 10770 struct scsi_write_same_16 *cdb; 10771 10772 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10773 10774 *lba = scsi_8btou64(cdb->addr); 10775 *len = scsi_4btoul(cdb->length); 10776 break; 10777 } 10778 case VERIFY_10: { 10779 struct scsi_verify_10 *cdb; 10780 10781 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10782 10783 *lba = scsi_4btoul(cdb->addr); 10784 *len = scsi_2btoul(cdb->length); 10785 break; 10786 } 10787 case VERIFY_12: { 10788 struct scsi_verify_12 *cdb; 10789 10790 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10791 10792 *lba = scsi_4btoul(cdb->addr); 10793 *len = scsi_4btoul(cdb->length); 10794 break; 10795 } 10796 case VERIFY_16: { 10797 struct scsi_verify_16 *cdb; 10798 10799 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10800 10801 *lba = scsi_8btou64(cdb->addr); 10802 *len = scsi_4btoul(cdb->length); 10803 break; 10804 } 10805 case UNMAP: { 10806 *lba = 0; 10807 *len = UINT64_MAX; 10808 break; 10809 } 10810 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10811 struct scsi_get_lba_status *cdb; 10812 10813 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10814 *lba = scsi_8btou64(cdb->addr); 10815 *len = UINT32_MAX; 10816 break; 10817 } 10818 default: 10819 *lba = 0; 10820 *len = UINT64_MAX; 10821 return (1); 10822 } 10823 10824 return (0); 10825 } 10826 10827 static ctl_action 10828 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10829 bool seq) 10830 { 10831 uint64_t endlba1, endlba2; 10832 10833 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10834 endlba2 = lba2 + len2 - 1; 10835 10836 if ((endlba1 < lba2) || (endlba2 < lba1)) 10837 return (CTL_ACTION_PASS); 10838 else 10839 return (CTL_ACTION_BLOCK); 10840 } 10841 10842 static int 10843 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10844 { 10845 struct ctl_ptr_len_flags *ptrlen; 10846 struct scsi_unmap_desc *buf, *end, *range; 10847 uint64_t lba; 10848 uint32_t len; 10849 10850 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 10851 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 10852 10853 /* If not UNMAP -- go other way. */ 10854 if (io->scsiio.cdb[0] != UNMAP) 10855 return (CTL_ACTION_SKIP); 10856 10857 /* If UNMAP without data -- block and wait for data. */ 10858 ptrlen = (struct ctl_ptr_len_flags *) 10859 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10860 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10861 ptrlen->ptr == NULL) 10862 return (CTL_ACTION_BLOCK); 10863 10864 /* UNMAP with data -- check for collision. */ 10865 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10866 end = buf + ptrlen->len / sizeof(*buf); 10867 for (range = buf; range < end; range++) { 10868 lba = scsi_8btou64(range->lba); 10869 len = scsi_4btoul(range->length); 10870 if ((lba < lba2 + len2) && (lba + len > lba2)) 10871 return (CTL_ACTION_BLOCK); 10872 } 10873 return (CTL_ACTION_PASS); 10874 } 10875 10876 static ctl_action 10877 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10878 { 10879 uint64_t lba1, lba2; 10880 uint64_t len1, len2; 10881 int retval; 10882 10883 retval = ctl_get_lba_len(io2, &lba2, &len2); 10884 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10885 10886 retval = ctl_extent_check_unmap(io1, lba2, len2); 10887 if (retval != CTL_ACTION_SKIP) 10888 return (retval); 10889 10890 retval = ctl_get_lba_len(io1, &lba1, &len1); 10891 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10892 10893 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)) 10894 seq = FALSE; 10895 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10896 } 10897 10898 static ctl_action 10899 ctl_seq_check(union ctl_io *io1, union ctl_io *io2) 10900 { 10901 uint64_t lba1, lba2; 10902 uint64_t len1, len2; 10903 int retval __diagused; 10904 10905 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10906 return (CTL_ACTION_PASS); 10907 retval = ctl_get_lba_len(io1, &lba1, &len1); 10908 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10909 retval = ctl_get_lba_len(io2, &lba2, &len2); 10910 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 10911 10912 if (lba1 + len1 == lba2) 10913 return (CTL_ACTION_BLOCK); 10914 return (CTL_ACTION_PASS); 10915 } 10916 10917 static ctl_action 10918 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10919 const uint8_t *serialize_row, union ctl_io *ooa_io) 10920 { 10921 10922 /* 10923 * The initiator attempted multiple untagged commands at the same 10924 * time. Can't do that. 10925 */ 10926 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10927 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10928 && ((pending_io->io_hdr.nexus.targ_port == 10929 ooa_io->io_hdr.nexus.targ_port) 10930 && (pending_io->io_hdr.nexus.initid == 10931 ooa_io->io_hdr.nexus.initid)) 10932 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10933 CTL_FLAG_STATUS_SENT)) == 0)) 10934 return (CTL_ACTION_OVERLAP); 10935 10936 /* 10937 * The initiator attempted to send multiple tagged commands with 10938 * the same ID. (It's fine if different initiators have the same 10939 * tag ID.) 10940 * 10941 * Even if all of those conditions are true, we don't kill the I/O 10942 * if the command ahead of us has been aborted. We won't end up 10943 * sending it to the FETD, and it's perfectly legal to resend a 10944 * command with the same tag number as long as the previous 10945 * instance of this tag number has been aborted somehow. 10946 */ 10947 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10948 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10949 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10950 && ((pending_io->io_hdr.nexus.targ_port == 10951 ooa_io->io_hdr.nexus.targ_port) 10952 && (pending_io->io_hdr.nexus.initid == 10953 ooa_io->io_hdr.nexus.initid)) 10954 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10955 CTL_FLAG_STATUS_SENT)) == 0)) 10956 return (CTL_ACTION_OVERLAP_TAG); 10957 10958 /* 10959 * If we get a head of queue tag, SAM-3 says that we should 10960 * immediately execute it. 10961 * 10962 * What happens if this command would normally block for some other 10963 * reason? e.g. a request sense with a head of queue tag 10964 * immediately after a write. Normally that would block, but this 10965 * will result in its getting executed immediately... 10966 * 10967 * We currently return "pass" instead of "skip", so we'll end up 10968 * going through the rest of the queue to check for overlapped tags. 10969 * 10970 * XXX KDM check for other types of blockage first?? 10971 */ 10972 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 10973 return (CTL_ACTION_PASS); 10974 10975 /* 10976 * Simple tags get blocked until all head of queue and ordered tags 10977 * ahead of them have completed. I'm lumping untagged commands in 10978 * with simple tags here. XXX KDM is that the right thing to do? 10979 */ 10980 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) || 10981 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 10982 return (CTL_ACTION_BLOCK); 10983 10984 /* Unsupported command in OOA queue. */ 10985 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD)) 10986 return (CTL_ACTION_PASS); 10987 10988 switch (serialize_row[ooa_io->scsiio.seridx]) { 10989 case CTL_SER_SEQ: 10990 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10991 return (ctl_seq_check(ooa_io, pending_io)); 10992 /* FALLTHROUGH */ 10993 case CTL_SER_PASS: 10994 return (CTL_ACTION_PASS); 10995 case CTL_SER_EXTENTOPT: 10996 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 10997 SCP_QUEUE_ALG_UNRESTRICTED) 10998 return (CTL_ACTION_PASS); 10999 /* FALLTHROUGH */ 11000 case CTL_SER_EXTENT: 11001 return (ctl_extent_check(ooa_io, pending_io, 11002 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11003 case CTL_SER_BLOCKOPT: 11004 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 11005 SCP_QUEUE_ALG_UNRESTRICTED) 11006 return (CTL_ACTION_PASS); 11007 /* FALLTHROUGH */ 11008 case CTL_SER_BLOCK: 11009 return (CTL_ACTION_BLOCK); 11010 default: 11011 __assert_unreachable(); 11012 } 11013 } 11014 11015 /* 11016 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11017 * Assumptions: 11018 * - pending_io is generally either incoming, or on the blocked queue 11019 * - starting I/O is the I/O we want to start the check with. 11020 */ 11021 static ctl_action 11022 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11023 union ctl_io **starting_io) 11024 { 11025 union ctl_io *ooa_io = *starting_io; 11026 const uint8_t *serialize_row; 11027 ctl_action action; 11028 11029 mtx_assert(&lun->lun_lock, MA_OWNED); 11030 11031 /* 11032 * Aborted commands are not going to be executed and may even 11033 * not report completion, so we don't care about their order. 11034 * Let them complete ASAP to clean the OOA queue. 11035 */ 11036 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT)) 11037 return (CTL_ACTION_SKIP); 11038 11039 /* 11040 * Ordered tags have to block until all items ahead of them have 11041 * completed. If we get called with an ordered tag, we always 11042 * block, if something else is ahead of us in the queue. 11043 */ 11044 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) && 11045 (ooa_io != NULL)) 11046 return (CTL_ACTION_BLOCK); 11047 11048 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx]; 11049 11050 /* 11051 * Run back along the OOA queue, starting with the current 11052 * blocked I/O and going through every I/O before it on the 11053 * queue. If starting_io is NULL, we'll just end up returning 11054 * CTL_ACTION_PASS. 11055 */ 11056 for (; ooa_io != NULL; 11057 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) { 11058 action = ctl_check_for_blockage(lun, pending_io, serialize_row, 11059 ooa_io); 11060 if (action != CTL_ACTION_PASS) { 11061 *starting_io = ooa_io; 11062 return (action); 11063 } 11064 } 11065 11066 *starting_io = NULL; 11067 return (CTL_ACTION_PASS); 11068 } 11069 11070 /* 11071 * Try to unblock the specified I/O. 11072 * 11073 * skip parameter allows explicitly skip present blocker of the I/O, 11074 * starting from the previous one on OOA queue. It can be used when 11075 * we know for sure that the blocker I/O does no longer count. 11076 */ 11077 static void 11078 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11079 { 11080 struct ctl_softc *softc = lun->ctl_softc; 11081 union ctl_io *bio, *obio; 11082 const struct ctl_cmd_entry *entry; 11083 union ctl_ha_msg msg_info; 11084 ctl_action action; 11085 11086 mtx_assert(&lun->lun_lock, MA_OWNED); 11087 11088 if (io->io_hdr.blocker == NULL) 11089 return; 11090 11091 obio = bio = io->io_hdr.blocker; 11092 if (skip) 11093 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links); 11094 action = ctl_check_ooa(lun, io, &bio); 11095 if (action == CTL_ACTION_BLOCK) { 11096 /* Still blocked, but may be by different I/O now. */ 11097 if (bio != obio) { 11098 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, 11099 &io->io_hdr, blocked_links); 11100 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, 11101 &io->io_hdr, blocked_links); 11102 io->io_hdr.blocker = bio; 11103 } 11104 return; 11105 } 11106 11107 /* No longer blocked, one way or another. */ 11108 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links); 11109 io->io_hdr.blocker = NULL; 11110 11111 switch (action) { 11112 case CTL_ACTION_PASS: 11113 case CTL_ACTION_SKIP: 11114 11115 /* Serializing commands from the other SC retire there. */ 11116 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11117 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11118 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11119 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11120 msg_info.hdr.serializing_sc = io; 11121 msg_info.hdr.msg_type = CTL_MSG_R2R; 11122 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11123 sizeof(msg_info.hdr), M_NOWAIT); 11124 break; 11125 } 11126 11127 /* 11128 * Check this I/O for LUN state changes that may have happened 11129 * while this command was blocked. The LUN state may have been 11130 * changed by a command ahead of us in the queue. 11131 */ 11132 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11133 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 11134 ctl_done(io); 11135 break; 11136 } 11137 11138 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11139 ctl_enqueue_rtr(io); 11140 break; 11141 default: 11142 __assert_unreachable(); 11143 case CTL_ACTION_OVERLAP: 11144 ctl_set_overlapped_cmd(&io->scsiio); 11145 goto error; 11146 case CTL_ACTION_OVERLAP_TAG: 11147 ctl_set_overlapped_tag(&io->scsiio, 11148 io->scsiio.tag_num & 0xff); 11149 error: 11150 /* Serializing commands from the other SC are done here. */ 11151 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11152 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11153 ctl_try_unblock_others(lun, io, TRUE); 11154 LIST_REMOVE(&io->io_hdr, ooa_links); 11155 11156 ctl_copy_sense_data_back(io, &msg_info); 11157 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11158 msg_info.hdr.serializing_sc = NULL; 11159 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 11160 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11161 sizeof(msg_info.scsi), M_WAITOK); 11162 ctl_free_io(io); 11163 break; 11164 } 11165 11166 ctl_done(io); 11167 break; 11168 } 11169 } 11170 11171 /* 11172 * Try to unblock I/Os blocked by the specified I/O. 11173 * 11174 * skip parameter allows explicitly skip the specified I/O as blocker, 11175 * starting from the previous one on the OOA queue. It can be used when 11176 * we know for sure that the specified I/O does no longer count (done). 11177 * It has to be still on OOA queue though so that we know where to start. 11178 */ 11179 static void 11180 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip) 11181 { 11182 union ctl_io *io, *next_io; 11183 11184 mtx_assert(&lun->lun_lock, MA_OWNED); 11185 11186 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue); 11187 io != NULL; io = next_io) { 11188 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links); 11189 11190 KASSERT(io->io_hdr.blocker != NULL, 11191 ("I/O %p on blocked list without blocker", io)); 11192 ctl_try_unblock_io(lun, io, skip); 11193 } 11194 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue), 11195 ("blocked_queue is not empty after skipping %p", bio)); 11196 } 11197 11198 /* 11199 * This routine (with one exception) checks LUN flags that can be set by 11200 * commands ahead of us in the OOA queue. These flags have to be checked 11201 * when a command initially comes in, and when we pull a command off the 11202 * blocked queue and are preparing to execute it. The reason we have to 11203 * check these flags for commands on the blocked queue is that the LUN 11204 * state may have been changed by a command ahead of us while we're on the 11205 * blocked queue. 11206 * 11207 * Ordering is somewhat important with these checks, so please pay 11208 * careful attention to the placement of any new checks. 11209 */ 11210 static int 11211 ctl_scsiio_lun_check(struct ctl_lun *lun, 11212 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11213 { 11214 struct ctl_softc *softc = lun->ctl_softc; 11215 int retval; 11216 uint32_t residx; 11217 11218 retval = 0; 11219 11220 mtx_assert(&lun->lun_lock, MA_OWNED); 11221 11222 /* 11223 * If this shelf is a secondary shelf controller, we may have to 11224 * reject some commands disallowed by HA mode and link state. 11225 */ 11226 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11227 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11228 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11229 ctl_set_lun_unavail(ctsio); 11230 retval = 1; 11231 goto bailout; 11232 } 11233 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11234 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11235 ctl_set_lun_transit(ctsio); 11236 retval = 1; 11237 goto bailout; 11238 } 11239 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11240 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11241 ctl_set_lun_standby(ctsio); 11242 retval = 1; 11243 goto bailout; 11244 } 11245 11246 /* The rest of checks are only done on executing side */ 11247 if (softc->ha_mode == CTL_HA_MODE_XFER) 11248 goto bailout; 11249 } 11250 11251 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11252 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11253 ctl_set_hw_write_protected(ctsio); 11254 retval = 1; 11255 goto bailout; 11256 } 11257 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11258 ctl_set_sense(ctsio, /*current_error*/ 1, 11259 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11260 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11261 retval = 1; 11262 goto bailout; 11263 } 11264 } 11265 11266 /* 11267 * Check for a reservation conflict. If this command isn't allowed 11268 * even on reserved LUNs, and if this initiator isn't the one who 11269 * reserved us, reject the command with a reservation conflict. 11270 */ 11271 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11272 if ((lun->flags & CTL_LUN_RESERVED) 11273 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11274 if (lun->res_idx != residx) { 11275 ctl_set_reservation_conflict(ctsio); 11276 retval = 1; 11277 goto bailout; 11278 } 11279 } 11280 11281 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11282 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11283 /* No reservation or command is allowed. */; 11284 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11285 (lun->pr_res_type == SPR_TYPE_WR_EX || 11286 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11287 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11288 /* The command is allowed for Write Exclusive resv. */; 11289 } else { 11290 /* 11291 * if we aren't registered or it's a res holder type 11292 * reservation and this isn't the res holder then set a 11293 * conflict. 11294 */ 11295 if (ctl_get_prkey(lun, residx) == 0 || 11296 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11297 ctl_set_reservation_conflict(ctsio); 11298 retval = 1; 11299 goto bailout; 11300 } 11301 } 11302 11303 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11304 if (lun->flags & CTL_LUN_EJECTED) 11305 ctl_set_lun_ejected(ctsio); 11306 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11307 if (lun->flags & CTL_LUN_REMOVABLE) 11308 ctl_set_lun_no_media(ctsio); 11309 else 11310 ctl_set_lun_int_reqd(ctsio); 11311 } else if (lun->flags & CTL_LUN_STOPPED) 11312 ctl_set_lun_stopped(ctsio); 11313 else 11314 goto bailout; 11315 retval = 1; 11316 goto bailout; 11317 } 11318 11319 bailout: 11320 return (retval); 11321 } 11322 11323 static void 11324 ctl_failover_io(union ctl_io *io, int have_lock) 11325 { 11326 ctl_set_busy(&io->scsiio); 11327 ctl_done(io); 11328 } 11329 11330 static void 11331 ctl_failover_lun(union ctl_io *rio) 11332 { 11333 struct ctl_softc *softc = CTL_SOFTC(rio); 11334 struct ctl_lun *lun; 11335 struct ctl_io_hdr *io, *next_io; 11336 uint32_t targ_lun; 11337 11338 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11339 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun)); 11340 11341 /* Find and lock the LUN. */ 11342 mtx_lock(&softc->ctl_lock); 11343 if (targ_lun > ctl_max_luns || 11344 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11345 mtx_unlock(&softc->ctl_lock); 11346 return; 11347 } 11348 mtx_lock(&lun->lun_lock); 11349 mtx_unlock(&softc->ctl_lock); 11350 if (lun->flags & CTL_LUN_DISABLED) { 11351 mtx_unlock(&lun->lun_lock); 11352 return; 11353 } 11354 11355 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11356 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11357 /* We are master */ 11358 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11359 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11360 io->flags |= CTL_FLAG_ABORT | 11361 CTL_FLAG_FAILOVER; 11362 ctl_try_unblock_io(lun, 11363 (union ctl_io *)io, FALSE); 11364 } else { /* This can be only due to DATAMOVE */ 11365 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11366 io->flags &= ~CTL_FLAG_DMA_INPROG; 11367 io->flags |= CTL_FLAG_IO_ACTIVE; 11368 io->port_status = 31340; 11369 ctl_enqueue_isc((union ctl_io *)io); 11370 } 11371 } else 11372 /* We are slave */ 11373 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11374 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11375 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11376 io->flags |= CTL_FLAG_FAILOVER; 11377 } else { 11378 ctl_set_busy(&((union ctl_io *)io)-> 11379 scsiio); 11380 ctl_done((union ctl_io *)io); 11381 } 11382 } 11383 } 11384 } else { /* SERIALIZE modes */ 11385 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11386 /* We are master */ 11387 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11388 if (io->blocker != NULL) { 11389 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue, 11390 io, blocked_links); 11391 io->blocker = NULL; 11392 } 11393 ctl_try_unblock_others(lun, (union ctl_io *)io, 11394 TRUE); 11395 LIST_REMOVE(io, ooa_links); 11396 ctl_free_io((union ctl_io *)io); 11397 } else 11398 /* We are slave */ 11399 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11400 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11401 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11402 ctl_set_busy(&((union ctl_io *)io)-> 11403 scsiio); 11404 ctl_done((union ctl_io *)io); 11405 } 11406 } 11407 } 11408 } 11409 mtx_unlock(&lun->lun_lock); 11410 } 11411 11412 static void 11413 ctl_scsiio_precheck(struct ctl_scsiio *ctsio) 11414 { 11415 struct ctl_softc *softc = CTL_SOFTC(ctsio); 11416 struct ctl_lun *lun; 11417 const struct ctl_cmd_entry *entry; 11418 union ctl_io *bio; 11419 uint32_t initidx, targ_lun; 11420 11421 lun = NULL; 11422 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11423 if (targ_lun < ctl_max_luns) 11424 lun = softc->ctl_luns[targ_lun]; 11425 if (lun) { 11426 /* 11427 * If the LUN is invalid, pretend that it doesn't exist. 11428 * It will go away as soon as all pending I/O has been 11429 * completed. 11430 */ 11431 mtx_lock(&lun->lun_lock); 11432 if (lun->flags & CTL_LUN_DISABLED) { 11433 mtx_unlock(&lun->lun_lock); 11434 lun = NULL; 11435 } 11436 } 11437 CTL_LUN(ctsio) = lun; 11438 if (lun) { 11439 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11440 11441 /* 11442 * Every I/O goes into the OOA queue for a particular LUN, 11443 * and stays there until completion. 11444 */ 11445 #ifdef CTL_TIME_IO 11446 if (LIST_EMPTY(&lun->ooa_queue)) 11447 lun->idle_time += getsbinuptime() - lun->last_busy; 11448 #endif 11449 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11450 } 11451 11452 /* Get command entry and return error if it is unsuppotyed. */ 11453 entry = ctl_validate_command(ctsio); 11454 if (entry == NULL) { 11455 if (lun) 11456 mtx_unlock(&lun->lun_lock); 11457 return; 11458 } 11459 11460 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11461 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11462 11463 /* 11464 * Check to see whether we can send this command to LUNs that don't 11465 * exist. This should pretty much only be the case for inquiry 11466 * and request sense. Further checks, below, really require having 11467 * a LUN, so we can't really check the command anymore. Just put 11468 * it on the rtr queue. 11469 */ 11470 if (lun == NULL) { 11471 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11472 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11473 ctl_enqueue_rtr((union ctl_io *)ctsio); 11474 return; 11475 } 11476 11477 ctl_set_unsupported_lun(ctsio); 11478 ctl_done((union ctl_io *)ctsio); 11479 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11480 return; 11481 } else { 11482 /* 11483 * Make sure we support this particular command on this LUN. 11484 * e.g., we don't support writes to the control LUN. 11485 */ 11486 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11487 mtx_unlock(&lun->lun_lock); 11488 ctl_set_invalid_opcode(ctsio); 11489 ctl_done((union ctl_io *)ctsio); 11490 return; 11491 } 11492 } 11493 11494 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11495 11496 /* 11497 * If we've got a request sense, it'll clear the contingent 11498 * allegiance condition. Otherwise, if we have a CA condition for 11499 * this initiator, clear it, because it sent down a command other 11500 * than request sense. 11501 */ 11502 if (ctsio->cdb[0] != REQUEST_SENSE) { 11503 struct scsi_sense_data *ps; 11504 11505 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11506 if (ps != NULL) 11507 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11508 } 11509 11510 /* 11511 * If the command has this flag set, it handles its own unit 11512 * attention reporting, we shouldn't do anything. Otherwise we 11513 * check for any pending unit attentions, and send them back to the 11514 * initiator. We only do this when a command initially comes in, 11515 * not when we pull it off the blocked queue. 11516 * 11517 * According to SAM-3, section 5.3.2, the order that things get 11518 * presented back to the host is basically unit attentions caused 11519 * by some sort of reset event, busy status, reservation conflicts 11520 * or task set full, and finally any other status. 11521 * 11522 * One issue here is that some of the unit attentions we report 11523 * don't fall into the "reset" category (e.g. "reported luns data 11524 * has changed"). So reporting it here, before the reservation 11525 * check, may be technically wrong. I guess the only thing to do 11526 * would be to check for and report the reset events here, and then 11527 * check for the other unit attention types after we check for a 11528 * reservation conflict. 11529 * 11530 * XXX KDM need to fix this 11531 */ 11532 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11533 ctl_ua_type ua_type; 11534 u_int sense_len = 0; 11535 11536 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11537 &sense_len, SSD_TYPE_NONE); 11538 if (ua_type != CTL_UA_NONE) { 11539 mtx_unlock(&lun->lun_lock); 11540 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11541 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11542 ctsio->sense_len = sense_len; 11543 ctl_done((union ctl_io *)ctsio); 11544 return; 11545 } 11546 } 11547 11548 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11549 mtx_unlock(&lun->lun_lock); 11550 ctl_done((union ctl_io *)ctsio); 11551 return; 11552 } 11553 11554 /* 11555 * XXX CHD this is where we want to send IO to other side if 11556 * this LUN is secondary on this SC. We will need to make a copy 11557 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11558 * the copy we send as FROM_OTHER. 11559 * We also need to stuff the address of the original IO so we can 11560 * find it easily. Something similar will need be done on the other 11561 * side so when we are done we can find the copy. 11562 */ 11563 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11564 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11565 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11566 union ctl_ha_msg msg_info; 11567 int isc_retval; 11568 11569 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11570 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11571 mtx_unlock(&lun->lun_lock); 11572 11573 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11574 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11575 msg_info.hdr.serializing_sc = NULL; 11576 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11577 msg_info.scsi.tag_num = ctsio->tag_num; 11578 msg_info.scsi.tag_type = ctsio->tag_type; 11579 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11580 msg_info.scsi.cdb_len = ctsio->cdb_len; 11581 msg_info.scsi.priority = ctsio->priority; 11582 11583 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11584 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11585 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11586 ctl_set_busy(ctsio); 11587 ctl_done((union ctl_io *)ctsio); 11588 return; 11589 } 11590 return; 11591 } 11592 11593 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 11594 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 11595 case CTL_ACTION_PASS: 11596 case CTL_ACTION_SKIP: 11597 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11598 mtx_unlock(&lun->lun_lock); 11599 ctl_enqueue_rtr((union ctl_io *)ctsio); 11600 break; 11601 case CTL_ACTION_BLOCK: 11602 ctsio->io_hdr.blocker = bio; 11603 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 11604 blocked_links); 11605 mtx_unlock(&lun->lun_lock); 11606 break; 11607 case CTL_ACTION_OVERLAP: 11608 mtx_unlock(&lun->lun_lock); 11609 ctl_set_overlapped_cmd(ctsio); 11610 ctl_done((union ctl_io *)ctsio); 11611 break; 11612 case CTL_ACTION_OVERLAP_TAG: 11613 mtx_unlock(&lun->lun_lock); 11614 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11615 ctl_done((union ctl_io *)ctsio); 11616 break; 11617 default: 11618 __assert_unreachable(); 11619 } 11620 } 11621 11622 const struct ctl_cmd_entry * 11623 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11624 { 11625 const struct ctl_cmd_entry *entry; 11626 int service_action; 11627 11628 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11629 if (sa) 11630 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11631 if (entry->flags & CTL_CMD_FLAG_SA5) { 11632 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11633 entry = &((const struct ctl_cmd_entry *) 11634 entry->execute)[service_action]; 11635 } 11636 return (entry); 11637 } 11638 11639 const struct ctl_cmd_entry * 11640 ctl_validate_command(struct ctl_scsiio *ctsio) 11641 { 11642 const struct ctl_cmd_entry *entry; 11643 int i, sa; 11644 uint8_t diff; 11645 11646 entry = ctl_get_cmd_entry(ctsio, &sa); 11647 ctsio->seridx = entry->seridx; 11648 if (entry->execute == NULL) { 11649 if (sa) 11650 ctl_set_invalid_field(ctsio, 11651 /*sks_valid*/ 1, 11652 /*command*/ 1, 11653 /*field*/ 1, 11654 /*bit_valid*/ 1, 11655 /*bit*/ 4); 11656 else 11657 ctl_set_invalid_opcode(ctsio); 11658 ctl_done((union ctl_io *)ctsio); 11659 return (NULL); 11660 } 11661 KASSERT(entry->length > 0, 11662 ("Not defined length for command 0x%02x/0x%02x", 11663 ctsio->cdb[0], ctsio->cdb[1])); 11664 for (i = 1; i < entry->length; i++) { 11665 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11666 if (diff == 0) 11667 continue; 11668 ctl_set_invalid_field(ctsio, 11669 /*sks_valid*/ 1, 11670 /*command*/ 1, 11671 /*field*/ i, 11672 /*bit_valid*/ 1, 11673 /*bit*/ fls(diff) - 1); 11674 ctl_done((union ctl_io *)ctsio); 11675 return (NULL); 11676 } 11677 return (entry); 11678 } 11679 11680 static int 11681 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11682 { 11683 11684 switch (lun_type) { 11685 case T_DIRECT: 11686 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11687 return (0); 11688 break; 11689 case T_PROCESSOR: 11690 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11691 return (0); 11692 break; 11693 case T_CDROM: 11694 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11695 return (0); 11696 break; 11697 default: 11698 return (0); 11699 } 11700 return (1); 11701 } 11702 11703 static int 11704 ctl_scsiio(struct ctl_scsiio *ctsio) 11705 { 11706 int retval; 11707 const struct ctl_cmd_entry *entry; 11708 11709 retval = CTL_RETVAL_COMPLETE; 11710 11711 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11712 11713 entry = ctl_get_cmd_entry(ctsio, NULL); 11714 11715 /* 11716 * If this I/O has been aborted, just send it straight to 11717 * ctl_done() without executing it. 11718 */ 11719 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11720 ctl_done((union ctl_io *)ctsio); 11721 goto bailout; 11722 } 11723 11724 /* 11725 * All the checks should have been handled by ctl_scsiio_precheck(). 11726 * We should be clear now to just execute the I/O. 11727 */ 11728 retval = entry->execute(ctsio); 11729 11730 bailout: 11731 return (retval); 11732 } 11733 11734 static int 11735 ctl_target_reset(union ctl_io *io) 11736 { 11737 struct ctl_softc *softc = CTL_SOFTC(io); 11738 struct ctl_port *port = CTL_PORT(io); 11739 struct ctl_lun *lun; 11740 uint32_t initidx; 11741 ctl_ua_type ua_type; 11742 11743 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11744 union ctl_ha_msg msg_info; 11745 11746 msg_info.hdr.nexus = io->io_hdr.nexus; 11747 msg_info.task.task_action = io->taskio.task_action; 11748 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11749 msg_info.hdr.original_sc = NULL; 11750 msg_info.hdr.serializing_sc = NULL; 11751 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11752 sizeof(msg_info.task), M_WAITOK); 11753 } 11754 11755 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11756 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11757 ua_type = CTL_UA_TARG_RESET; 11758 else 11759 ua_type = CTL_UA_BUS_RESET; 11760 mtx_lock(&softc->ctl_lock); 11761 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11762 if (port != NULL && 11763 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11764 continue; 11765 ctl_do_lun_reset(lun, initidx, ua_type); 11766 } 11767 mtx_unlock(&softc->ctl_lock); 11768 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11769 return (0); 11770 } 11771 11772 /* 11773 * The LUN should always be set. The I/O is optional, and is used to 11774 * distinguish between I/Os sent by this initiator, and by other 11775 * initiators. We set unit attention for initiators other than this one. 11776 * SAM-3 is vague on this point. It does say that a unit attention should 11777 * be established for other initiators when a LUN is reset (see section 11778 * 5.7.3), but it doesn't specifically say that the unit attention should 11779 * be established for this particular initiator when a LUN is reset. Here 11780 * is the relevant text, from SAM-3 rev 8: 11781 * 11782 * 5.7.2 When a SCSI initiator port aborts its own tasks 11783 * 11784 * When a SCSI initiator port causes its own task(s) to be aborted, no 11785 * notification that the task(s) have been aborted shall be returned to 11786 * the SCSI initiator port other than the completion response for the 11787 * command or task management function action that caused the task(s) to 11788 * be aborted and notification(s) associated with related effects of the 11789 * action (e.g., a reset unit attention condition). 11790 * 11791 * XXX KDM for now, we're setting unit attention for all initiators. 11792 */ 11793 static void 11794 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11795 { 11796 struct ctl_io_hdr *xioh; 11797 int i; 11798 11799 mtx_lock(&lun->lun_lock); 11800 /* Abort tasks. */ 11801 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11802 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11803 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE); 11804 } 11805 /* Clear CA. */ 11806 for (i = 0; i < ctl_max_ports; i++) { 11807 free(lun->pending_sense[i], M_CTL); 11808 lun->pending_sense[i] = NULL; 11809 } 11810 /* Clear reservation. */ 11811 lun->flags &= ~CTL_LUN_RESERVED; 11812 /* Clear prevent media removal. */ 11813 if (lun->prevent) { 11814 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11815 ctl_clear_mask(lun->prevent, i); 11816 lun->prevent_count = 0; 11817 } 11818 /* Clear TPC status */ 11819 ctl_tpc_lun_clear(lun, -1); 11820 /* Establish UA. */ 11821 #if 0 11822 ctl_est_ua_all(lun, initidx, ua_type); 11823 #else 11824 ctl_est_ua_all(lun, -1, ua_type); 11825 #endif 11826 mtx_unlock(&lun->lun_lock); 11827 } 11828 11829 static int 11830 ctl_lun_reset(union ctl_io *io) 11831 { 11832 struct ctl_softc *softc = CTL_SOFTC(io); 11833 struct ctl_lun *lun; 11834 uint32_t targ_lun, initidx; 11835 11836 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11837 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11838 mtx_lock(&softc->ctl_lock); 11839 if (targ_lun >= ctl_max_luns || 11840 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11841 mtx_unlock(&softc->ctl_lock); 11842 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11843 return (1); 11844 } 11845 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11846 mtx_unlock(&softc->ctl_lock); 11847 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11848 11849 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11850 union ctl_ha_msg msg_info; 11851 11852 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11853 msg_info.hdr.nexus = io->io_hdr.nexus; 11854 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11855 msg_info.hdr.original_sc = NULL; 11856 msg_info.hdr.serializing_sc = NULL; 11857 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11858 sizeof(msg_info.task), M_WAITOK); 11859 } 11860 return (0); 11861 } 11862 11863 static void 11864 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11865 int other_sc) 11866 { 11867 struct ctl_io_hdr *xioh; 11868 11869 mtx_assert(&lun->lun_lock, MA_OWNED); 11870 11871 /* 11872 * Run through the OOA queue and attempt to find the given I/O. 11873 * The target port, initiator ID, tag type and tag number have to 11874 * match the values that we got from the initiator. If we have an 11875 * untagged command to abort, simply abort the first untagged command 11876 * we come to. We only allow one untagged command at a time of course. 11877 */ 11878 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 11879 union ctl_io *xio = (union ctl_io *)xioh; 11880 if ((targ_port == UINT32_MAX || 11881 targ_port == xioh->nexus.targ_port) && 11882 (init_id == UINT32_MAX || 11883 init_id == xioh->nexus.initid)) { 11884 if (targ_port != xioh->nexus.targ_port || 11885 init_id != xioh->nexus.initid) 11886 xioh->flags |= CTL_FLAG_ABORT_STATUS; 11887 xioh->flags |= CTL_FLAG_ABORT; 11888 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11889 union ctl_ha_msg msg_info; 11890 11891 msg_info.hdr.nexus = xioh->nexus; 11892 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11893 msg_info.task.tag_num = xio->scsiio.tag_num; 11894 msg_info.task.tag_type = xio->scsiio.tag_type; 11895 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11896 msg_info.hdr.original_sc = NULL; 11897 msg_info.hdr.serializing_sc = NULL; 11898 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11899 sizeof(msg_info.task), M_NOWAIT); 11900 } 11901 ctl_try_unblock_io(lun, xio, FALSE); 11902 } 11903 } 11904 } 11905 11906 static int 11907 ctl_abort_task_set(union ctl_io *io) 11908 { 11909 struct ctl_softc *softc = CTL_SOFTC(io); 11910 struct ctl_lun *lun; 11911 uint32_t targ_lun; 11912 11913 /* 11914 * Look up the LUN. 11915 */ 11916 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11917 mtx_lock(&softc->ctl_lock); 11918 if (targ_lun >= ctl_max_luns || 11919 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11920 mtx_unlock(&softc->ctl_lock); 11921 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11922 return (1); 11923 } 11924 11925 mtx_lock(&lun->lun_lock); 11926 mtx_unlock(&softc->ctl_lock); 11927 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11928 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11929 io->io_hdr.nexus.initid, 11930 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11931 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11932 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11933 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11934 } 11935 mtx_unlock(&lun->lun_lock); 11936 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11937 return (0); 11938 } 11939 11940 static void 11941 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 11942 ctl_ua_type ua_type) 11943 { 11944 struct ctl_lun *lun; 11945 struct scsi_sense_data *ps; 11946 uint32_t p, i; 11947 11948 p = initidx / CTL_MAX_INIT_PER_PORT; 11949 i = initidx % CTL_MAX_INIT_PER_PORT; 11950 mtx_lock(&softc->ctl_lock); 11951 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11952 mtx_lock(&lun->lun_lock); 11953 /* Abort tasks. */ 11954 ctl_abort_tasks_lun(lun, p, i, 1); 11955 /* Clear CA. */ 11956 ps = lun->pending_sense[p]; 11957 if (ps != NULL) 11958 ps[i].error_code = 0; 11959 /* Clear reservation. */ 11960 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11961 lun->flags &= ~CTL_LUN_RESERVED; 11962 /* Clear prevent media removal. */ 11963 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 11964 ctl_clear_mask(lun->prevent, initidx); 11965 lun->prevent_count--; 11966 } 11967 /* Clear TPC status */ 11968 ctl_tpc_lun_clear(lun, initidx); 11969 /* Establish UA. */ 11970 ctl_est_ua(lun, initidx, ua_type); 11971 mtx_unlock(&lun->lun_lock); 11972 } 11973 mtx_unlock(&softc->ctl_lock); 11974 } 11975 11976 static int 11977 ctl_i_t_nexus_reset(union ctl_io *io) 11978 { 11979 struct ctl_softc *softc = CTL_SOFTC(io); 11980 uint32_t initidx; 11981 11982 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11983 union ctl_ha_msg msg_info; 11984 11985 msg_info.hdr.nexus = io->io_hdr.nexus; 11986 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11987 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11988 msg_info.hdr.original_sc = NULL; 11989 msg_info.hdr.serializing_sc = NULL; 11990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11991 sizeof(msg_info.task), M_WAITOK); 11992 } 11993 11994 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11995 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 11996 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11997 return (0); 11998 } 11999 12000 static int 12001 ctl_abort_task(union ctl_io *io) 12002 { 12003 struct ctl_softc *softc = CTL_SOFTC(io); 12004 struct ctl_io_hdr *xioh; 12005 struct ctl_lun *lun; 12006 uint32_t targ_lun; 12007 12008 /* 12009 * Look up the LUN. 12010 */ 12011 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12012 mtx_lock(&softc->ctl_lock); 12013 if (targ_lun >= ctl_max_luns || 12014 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12015 mtx_unlock(&softc->ctl_lock); 12016 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12017 return (1); 12018 } 12019 12020 mtx_lock(&lun->lun_lock); 12021 mtx_unlock(&softc->ctl_lock); 12022 /* 12023 * Run through the OOA queue and attempt to find the given I/O. 12024 * The target port, initiator ID, tag type and tag number have to 12025 * match the values that we got from the initiator. If we have an 12026 * untagged command to abort, simply abort the first untagged command 12027 * we come to. We only allow one untagged command at a time of course. 12028 */ 12029 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12030 union ctl_io *xio = (union ctl_io *)xioh; 12031 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12032 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12033 || (xioh->flags & CTL_FLAG_ABORT)) 12034 continue; 12035 12036 /* 12037 * If the abort says that the task is untagged, the 12038 * task in the queue must be untagged. Otherwise, 12039 * we just check to see whether the tag numbers 12040 * match. This is because the QLogic firmware 12041 * doesn't pass back the tag type in an abort 12042 * request. 12043 */ 12044 #if 0 12045 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12046 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12047 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12048 #else 12049 /* 12050 * XXX KDM we've got problems with FC, because it 12051 * doesn't send down a tag type with aborts. So we 12052 * can only really go by the tag number... 12053 * This may cause problems with parallel SCSI. 12054 * Need to figure that out!! 12055 */ 12056 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12057 #endif 12058 xioh->flags |= CTL_FLAG_ABORT; 12059 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12060 !(lun->flags & CTL_LUN_PRIMARY_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_ABORT_TASK; 12065 msg_info.task.tag_num = io->taskio.tag_num; 12066 msg_info.task.tag_type = io->taskio.tag_type; 12067 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12068 msg_info.hdr.original_sc = NULL; 12069 msg_info.hdr.serializing_sc = NULL; 12070 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12071 sizeof(msg_info.task), M_NOWAIT); 12072 } 12073 ctl_try_unblock_io(lun, xio, FALSE); 12074 } 12075 } 12076 mtx_unlock(&lun->lun_lock); 12077 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12078 return (0); 12079 } 12080 12081 static int 12082 ctl_query_task(union ctl_io *io, int task_set) 12083 { 12084 struct ctl_softc *softc = CTL_SOFTC(io); 12085 struct ctl_io_hdr *xioh; 12086 struct ctl_lun *lun; 12087 int found = 0; 12088 uint32_t targ_lun; 12089 12090 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12091 mtx_lock(&softc->ctl_lock); 12092 if (targ_lun >= ctl_max_luns || 12093 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12094 mtx_unlock(&softc->ctl_lock); 12095 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12096 return (1); 12097 } 12098 mtx_lock(&lun->lun_lock); 12099 mtx_unlock(&softc->ctl_lock); 12100 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12101 union ctl_io *xio = (union ctl_io *)xioh; 12102 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12103 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12104 || (xioh->flags & CTL_FLAG_ABORT)) 12105 continue; 12106 12107 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12108 found = 1; 12109 break; 12110 } 12111 } 12112 mtx_unlock(&lun->lun_lock); 12113 if (found) 12114 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12115 else 12116 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12117 return (0); 12118 } 12119 12120 static int 12121 ctl_query_async_event(union ctl_io *io) 12122 { 12123 struct ctl_softc *softc = CTL_SOFTC(io); 12124 struct ctl_lun *lun; 12125 ctl_ua_type ua; 12126 uint32_t targ_lun, initidx; 12127 12128 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12129 mtx_lock(&softc->ctl_lock); 12130 if (targ_lun >= ctl_max_luns || 12131 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12132 mtx_unlock(&softc->ctl_lock); 12133 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12134 return (1); 12135 } 12136 mtx_lock(&lun->lun_lock); 12137 mtx_unlock(&softc->ctl_lock); 12138 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12139 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12140 mtx_unlock(&lun->lun_lock); 12141 if (ua != CTL_UA_NONE) 12142 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12143 else 12144 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12145 return (0); 12146 } 12147 12148 static void 12149 ctl_run_task(union ctl_io *io) 12150 { 12151 int retval = 1; 12152 12153 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12154 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12155 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12156 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12157 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12158 switch (io->taskio.task_action) { 12159 case CTL_TASK_ABORT_TASK: 12160 retval = ctl_abort_task(io); 12161 break; 12162 case CTL_TASK_ABORT_TASK_SET: 12163 case CTL_TASK_CLEAR_TASK_SET: 12164 retval = ctl_abort_task_set(io); 12165 break; 12166 case CTL_TASK_CLEAR_ACA: 12167 break; 12168 case CTL_TASK_I_T_NEXUS_RESET: 12169 retval = ctl_i_t_nexus_reset(io); 12170 break; 12171 case CTL_TASK_LUN_RESET: 12172 retval = ctl_lun_reset(io); 12173 break; 12174 case CTL_TASK_TARGET_RESET: 12175 case CTL_TASK_BUS_RESET: 12176 retval = ctl_target_reset(io); 12177 break; 12178 case CTL_TASK_PORT_LOGIN: 12179 break; 12180 case CTL_TASK_PORT_LOGOUT: 12181 break; 12182 case CTL_TASK_QUERY_TASK: 12183 retval = ctl_query_task(io, 0); 12184 break; 12185 case CTL_TASK_QUERY_TASK_SET: 12186 retval = ctl_query_task(io, 1); 12187 break; 12188 case CTL_TASK_QUERY_ASYNC_EVENT: 12189 retval = ctl_query_async_event(io); 12190 break; 12191 default: 12192 printf("%s: got unknown task management event %d\n", 12193 __func__, io->taskio.task_action); 12194 break; 12195 } 12196 if (retval == 0) 12197 io->io_hdr.status = CTL_SUCCESS; 12198 else 12199 io->io_hdr.status = CTL_ERROR; 12200 ctl_done(io); 12201 } 12202 12203 /* 12204 * For HA operation. Handle commands that come in from the other 12205 * controller. 12206 */ 12207 static void 12208 ctl_handle_isc(union ctl_io *io) 12209 { 12210 struct ctl_softc *softc = CTL_SOFTC(io); 12211 struct ctl_lun *lun; 12212 const struct ctl_cmd_entry *entry; 12213 uint32_t targ_lun; 12214 12215 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12216 switch (io->io_hdr.msg_type) { 12217 case CTL_MSG_SERIALIZE: 12218 ctl_serialize_other_sc_cmd(&io->scsiio); 12219 break; 12220 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12221 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12222 if (targ_lun >= ctl_max_luns || 12223 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12224 ctl_done(io); 12225 break; 12226 } 12227 mtx_lock(&lun->lun_lock); 12228 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12229 mtx_unlock(&lun->lun_lock); 12230 ctl_done(io); 12231 break; 12232 } 12233 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12234 mtx_unlock(&lun->lun_lock); 12235 ctl_enqueue_rtr(io); 12236 break; 12237 case CTL_MSG_FINISH_IO: 12238 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12239 ctl_done(io); 12240 break; 12241 } 12242 if (targ_lun >= ctl_max_luns || 12243 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12244 ctl_free_io(io); 12245 break; 12246 } 12247 mtx_lock(&lun->lun_lock); 12248 ctl_try_unblock_others(lun, io, TRUE); 12249 LIST_REMOVE(&io->io_hdr, ooa_links); 12250 mtx_unlock(&lun->lun_lock); 12251 ctl_free_io(io); 12252 break; 12253 case CTL_MSG_PERS_ACTION: 12254 ctl_hndl_per_res_out_on_other_sc(io); 12255 ctl_free_io(io); 12256 break; 12257 case CTL_MSG_BAD_JUJU: 12258 ctl_done(io); 12259 break; 12260 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12261 ctl_datamove_remote(io); 12262 break; 12263 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12264 ctl_datamove_done(io, false); 12265 break; 12266 case CTL_MSG_FAILOVER: 12267 ctl_failover_lun(io); 12268 ctl_free_io(io); 12269 break; 12270 default: 12271 printf("%s: Invalid message type %d\n", 12272 __func__, io->io_hdr.msg_type); 12273 ctl_free_io(io); 12274 break; 12275 } 12276 12277 } 12278 12279 /* 12280 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12281 * there is no match. 12282 */ 12283 static ctl_lun_error_pattern 12284 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12285 { 12286 const struct ctl_cmd_entry *entry; 12287 ctl_lun_error_pattern filtered_pattern, pattern; 12288 12289 pattern = desc->error_pattern; 12290 12291 /* 12292 * XXX KDM we need more data passed into this function to match a 12293 * custom pattern, and we actually need to implement custom pattern 12294 * matching. 12295 */ 12296 if (pattern & CTL_LUN_PAT_CMD) 12297 return (CTL_LUN_PAT_CMD); 12298 12299 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12300 return (CTL_LUN_PAT_ANY); 12301 12302 entry = ctl_get_cmd_entry(ctsio, NULL); 12303 12304 filtered_pattern = entry->pattern & pattern; 12305 12306 /* 12307 * If the user requested specific flags in the pattern (e.g. 12308 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12309 * flags. 12310 * 12311 * If the user did not specify any flags, it doesn't matter whether 12312 * or not the command supports the flags. 12313 */ 12314 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12315 (pattern & ~CTL_LUN_PAT_MASK)) 12316 return (CTL_LUN_PAT_NONE); 12317 12318 /* 12319 * If the user asked for a range check, see if the requested LBA 12320 * range overlaps with this command's LBA range. 12321 */ 12322 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12323 uint64_t lba1; 12324 uint64_t len1; 12325 ctl_action action; 12326 int retval; 12327 12328 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12329 if (retval != 0) 12330 return (CTL_LUN_PAT_NONE); 12331 12332 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12333 desc->lba_range.len, FALSE); 12334 /* 12335 * A "pass" means that the LBA ranges don't overlap, so 12336 * this doesn't match the user's range criteria. 12337 */ 12338 if (action == CTL_ACTION_PASS) 12339 return (CTL_LUN_PAT_NONE); 12340 } 12341 12342 return (filtered_pattern); 12343 } 12344 12345 static void 12346 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12347 { 12348 struct ctl_error_desc *desc, *desc2; 12349 12350 mtx_assert(&lun->lun_lock, MA_OWNED); 12351 12352 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12353 ctl_lun_error_pattern pattern; 12354 /* 12355 * Check to see whether this particular command matches 12356 * the pattern in the descriptor. 12357 */ 12358 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12359 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12360 continue; 12361 12362 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12363 case CTL_LUN_INJ_ABORTED: 12364 ctl_set_aborted(&io->scsiio); 12365 break; 12366 case CTL_LUN_INJ_MEDIUM_ERR: 12367 ctl_set_medium_error(&io->scsiio, 12368 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12369 CTL_FLAG_DATA_OUT); 12370 break; 12371 case CTL_LUN_INJ_UA: 12372 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12373 * OCCURRED */ 12374 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12375 break; 12376 case CTL_LUN_INJ_CUSTOM: 12377 /* 12378 * We're assuming the user knows what he is doing. 12379 * Just copy the sense information without doing 12380 * checks. 12381 */ 12382 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12383 MIN(sizeof(desc->custom_sense), 12384 sizeof(io->scsiio.sense_data))); 12385 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12386 io->scsiio.sense_len = SSD_FULL_SIZE; 12387 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12388 break; 12389 case CTL_LUN_INJ_NONE: 12390 default: 12391 /* 12392 * If this is an error injection type we don't know 12393 * about, clear the continuous flag (if it is set) 12394 * so it will get deleted below. 12395 */ 12396 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12397 break; 12398 } 12399 /* 12400 * By default, each error injection action is a one-shot 12401 */ 12402 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12403 continue; 12404 12405 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12406 12407 free(desc, M_CTL); 12408 } 12409 } 12410 12411 #ifdef CTL_IO_DELAY 12412 static void 12413 ctl_datamove_timer_wakeup(void *arg) 12414 { 12415 union ctl_io *io; 12416 12417 io = (union ctl_io *)arg; 12418 12419 ctl_datamove(io); 12420 } 12421 #endif /* CTL_IO_DELAY */ 12422 12423 static void 12424 ctl_datamove_done_process(union ctl_io *io) 12425 { 12426 #ifdef CTL_TIME_IO 12427 struct bintime cur_bt; 12428 #endif 12429 12430 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 12431 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 12432 12433 #ifdef CTL_TIME_IO 12434 getbinuptime(&cur_bt); 12435 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12436 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12437 #endif 12438 io->io_hdr.num_dmas++; 12439 12440 if ((io->io_hdr.port_status != 0) && 12441 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12442 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12443 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 12444 /*retry_count*/ io->io_hdr.port_status); 12445 } else if (io->scsiio.kern_data_resid != 0 && 12446 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 12447 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 12448 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 12449 ctl_set_invalid_field_ciu(&io->scsiio); 12450 } else if (ctl_debug & CTL_DEBUG_CDB_DATA) 12451 ctl_data_print(io); 12452 } 12453 12454 void 12455 ctl_datamove_done(union ctl_io *io, bool samethr) 12456 { 12457 12458 ctl_datamove_done_process(io); 12459 io->scsiio.be_move_done(io, samethr); 12460 } 12461 12462 void 12463 ctl_datamove(union ctl_io *io) 12464 { 12465 void (*fe_datamove)(union ctl_io *io); 12466 12467 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12468 12469 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12470 12471 /* No data transferred yet. Frontend must update this when done. */ 12472 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12473 12474 #ifdef CTL_TIME_IO 12475 getbinuptime(&io->io_hdr.dma_start_bt); 12476 #endif /* CTL_TIME_IO */ 12477 12478 #ifdef CTL_IO_DELAY 12479 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12480 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12481 } else { 12482 struct ctl_lun *lun; 12483 12484 lun = CTL_LUN(io); 12485 if ((lun != NULL) 12486 && (lun->delay_info.datamove_delay > 0)) { 12487 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12488 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12489 callout_reset(&io->io_hdr.delay_callout, 12490 lun->delay_info.datamove_delay * hz, 12491 ctl_datamove_timer_wakeup, io); 12492 if (lun->delay_info.datamove_type == 12493 CTL_DELAY_TYPE_ONESHOT) 12494 lun->delay_info.datamove_delay = 0; 12495 return; 12496 } 12497 } 12498 #endif 12499 12500 /* 12501 * This command has been aborted. Set the port status, so we fail 12502 * the data move. 12503 */ 12504 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12505 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12506 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12507 io->io_hdr.nexus.targ_port, 12508 io->io_hdr.nexus.targ_lun); 12509 io->io_hdr.port_status = 31337; 12510 ctl_datamove_done_process(io); 12511 io->scsiio.be_move_done(io, true); 12512 return; 12513 } 12514 12515 /* Don't confuse frontend with zero length data move. */ 12516 if (io->scsiio.kern_data_len == 0) { 12517 ctl_datamove_done_process(io); 12518 io->scsiio.be_move_done(io, true); 12519 return; 12520 } 12521 12522 fe_datamove = CTL_PORT(io)->fe_datamove; 12523 fe_datamove(io); 12524 } 12525 12526 static void 12527 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12528 { 12529 union ctl_ha_msg msg; 12530 #ifdef CTL_TIME_IO 12531 struct bintime cur_bt; 12532 #endif 12533 12534 memset(&msg, 0, sizeof(msg)); 12535 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12536 msg.hdr.original_sc = io; 12537 msg.hdr.serializing_sc = io->io_hdr.remote_io; 12538 msg.hdr.nexus = io->io_hdr.nexus; 12539 msg.hdr.status = io->io_hdr.status; 12540 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12541 msg.scsi.tag_num = io->scsiio.tag_num; 12542 msg.scsi.tag_type = io->scsiio.tag_type; 12543 msg.scsi.scsi_status = io->scsiio.scsi_status; 12544 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12545 io->scsiio.sense_len); 12546 msg.scsi.sense_len = io->scsiio.sense_len; 12547 msg.scsi.port_status = io->io_hdr.port_status; 12548 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12549 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12550 ctl_failover_io(io, /*have_lock*/ have_lock); 12551 return; 12552 } 12553 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12554 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12555 msg.scsi.sense_len, M_WAITOK); 12556 12557 #ifdef CTL_TIME_IO 12558 getbinuptime(&cur_bt); 12559 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12560 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12561 #endif 12562 io->io_hdr.num_dmas++; 12563 } 12564 12565 /* 12566 * The DMA to the remote side is done, now we need to tell the other side 12567 * we're done so it can continue with its data movement. 12568 */ 12569 static void 12570 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12571 { 12572 union ctl_io *io; 12573 uint32_t i; 12574 12575 io = rq->context; 12576 12577 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12578 printf("%s: ISC DMA write failed with error %d", __func__, 12579 rq->ret); 12580 ctl_set_internal_failure(&io->scsiio, 12581 /*sks_valid*/ 1, 12582 /*retry_count*/ rq->ret); 12583 } 12584 12585 ctl_dt_req_free(rq); 12586 12587 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12588 free(CTL_LSGLT(io)[i].addr, M_CTL); 12589 free(CTL_RSGL(io), M_CTL); 12590 CTL_RSGL(io) = NULL; 12591 CTL_LSGL(io) = NULL; 12592 12593 /* 12594 * The data is in local and remote memory, so now we need to send 12595 * status (good or back) back to the other side. 12596 */ 12597 ctl_send_datamove_done(io, /*have_lock*/ 0); 12598 } 12599 12600 /* 12601 * We've moved the data from the host/controller into local memory. Now we 12602 * need to push it over to the remote controller's memory. 12603 */ 12604 static int 12605 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr) 12606 { 12607 int retval; 12608 12609 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12610 ctl_datamove_remote_write_cb); 12611 return (retval); 12612 } 12613 12614 static void 12615 ctl_datamove_remote_write(union ctl_io *io) 12616 { 12617 int retval; 12618 void (*fe_datamove)(union ctl_io *io); 12619 12620 /* 12621 * - Get the data from the host/HBA into local memory. 12622 * - DMA memory from the local controller to the remote controller. 12623 * - Send status back to the remote controller. 12624 */ 12625 12626 retval = ctl_datamove_remote_sgl_setup(io); 12627 if (retval != 0) 12628 return; 12629 12630 /* Switch the pointer over so the FETD knows what to do */ 12631 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12632 12633 /* 12634 * Use a custom move done callback, since we need to send completion 12635 * back to the other controller, not to the backend on this side. 12636 */ 12637 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12638 12639 fe_datamove = CTL_PORT(io)->fe_datamove; 12640 fe_datamove(io); 12641 } 12642 12643 static int 12644 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr) 12645 { 12646 uint32_t i; 12647 12648 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12649 free(CTL_LSGLT(io)[i].addr, M_CTL); 12650 free(CTL_RSGL(io), M_CTL); 12651 CTL_RSGL(io) = NULL; 12652 CTL_LSGL(io) = NULL; 12653 12654 /* 12655 * The read is done, now we need to send status (good or bad) back 12656 * to the other side. 12657 */ 12658 ctl_send_datamove_done(io, /*have_lock*/ 0); 12659 12660 return (0); 12661 } 12662 12663 static void 12664 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12665 { 12666 union ctl_io *io; 12667 void (*fe_datamove)(union ctl_io *io); 12668 12669 io = rq->context; 12670 12671 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12672 printf("%s: ISC DMA read failed with error %d\n", __func__, 12673 rq->ret); 12674 ctl_set_internal_failure(&io->scsiio, 12675 /*sks_valid*/ 1, 12676 /*retry_count*/ rq->ret); 12677 } 12678 12679 ctl_dt_req_free(rq); 12680 12681 /* Switch the pointer over so the FETD knows what to do */ 12682 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12683 12684 /* 12685 * Use a custom move done callback, since we need to send completion 12686 * back to the other controller, not to the backend on this side. 12687 */ 12688 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12689 12690 /* XXX KDM add checks like the ones in ctl_datamove? */ 12691 12692 fe_datamove = CTL_PORT(io)->fe_datamove; 12693 fe_datamove(io); 12694 } 12695 12696 static int 12697 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12698 { 12699 struct ctl_sg_entry *local_sglist; 12700 uint32_t len_to_go; 12701 int retval; 12702 int i; 12703 12704 retval = 0; 12705 local_sglist = CTL_LSGL(io); 12706 len_to_go = io->scsiio.kern_data_len; 12707 12708 /* 12709 * The difficult thing here is that the size of the various 12710 * S/G segments may be different than the size from the 12711 * remote controller. That'll make it harder when DMAing 12712 * the data back to the other side. 12713 */ 12714 for (i = 0; len_to_go > 0; i++) { 12715 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12716 local_sglist[i].addr = 12717 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12718 12719 len_to_go -= local_sglist[i].len; 12720 } 12721 /* 12722 * Reset the number of S/G entries accordingly. The original 12723 * number of S/G entries is available in rem_sg_entries. 12724 */ 12725 io->scsiio.kern_sg_entries = i; 12726 12727 return (retval); 12728 } 12729 12730 static int 12731 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12732 ctl_ha_dt_cb callback) 12733 { 12734 struct ctl_ha_dt_req *rq; 12735 struct ctl_sg_entry *remote_sglist, *local_sglist; 12736 uint32_t local_used, remote_used, total_used; 12737 int i, j, isc_ret; 12738 12739 rq = ctl_dt_req_alloc(); 12740 12741 /* 12742 * If we failed to allocate the request, and if the DMA didn't fail 12743 * anyway, set busy status. This is just a resource allocation 12744 * failure. 12745 */ 12746 if ((rq == NULL) 12747 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12748 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12749 ctl_set_busy(&io->scsiio); 12750 12751 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12752 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12753 if (rq != NULL) 12754 ctl_dt_req_free(rq); 12755 12756 /* 12757 * The data move failed. We need to return status back 12758 * to the other controller. No point in trying to DMA 12759 * data to the remote controller. 12760 */ 12761 12762 ctl_send_datamove_done(io, /*have_lock*/ 0); 12763 12764 return (1); 12765 } 12766 12767 local_sglist = CTL_LSGL(io); 12768 remote_sglist = CTL_RSGL(io); 12769 local_used = 0; 12770 remote_used = 0; 12771 total_used = 0; 12772 12773 /* 12774 * Pull/push the data over the wire from/to the other controller. 12775 * This takes into account the possibility that the local and 12776 * remote sglists may not be identical in terms of the size of 12777 * the elements and the number of elements. 12778 * 12779 * One fundamental assumption here is that the length allocated for 12780 * both the local and remote sglists is identical. Otherwise, we've 12781 * essentially got a coding error of some sort. 12782 */ 12783 isc_ret = CTL_HA_STATUS_SUCCESS; 12784 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12785 uint32_t cur_len; 12786 uint8_t *tmp_ptr; 12787 12788 rq->command = command; 12789 rq->context = io; 12790 12791 /* 12792 * Both pointers should be aligned. But it is possible 12793 * that the allocation length is not. They should both 12794 * also have enough slack left over at the end, though, 12795 * to round up to the next 8 byte boundary. 12796 */ 12797 cur_len = MIN(local_sglist[i].len - local_used, 12798 remote_sglist[j].len - remote_used); 12799 rq->size = cur_len; 12800 12801 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12802 tmp_ptr += local_used; 12803 12804 #if 0 12805 /* Use physical addresses when talking to ISC hardware */ 12806 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12807 /* XXX KDM use busdma */ 12808 rq->local = vtophys(tmp_ptr); 12809 } else 12810 rq->local = tmp_ptr; 12811 #else 12812 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12813 ("HA does not support BUS_ADDR")); 12814 rq->local = tmp_ptr; 12815 #endif 12816 12817 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12818 tmp_ptr += remote_used; 12819 rq->remote = tmp_ptr; 12820 12821 rq->callback = NULL; 12822 12823 local_used += cur_len; 12824 if (local_used >= local_sglist[i].len) { 12825 i++; 12826 local_used = 0; 12827 } 12828 12829 remote_used += cur_len; 12830 if (remote_used >= remote_sglist[j].len) { 12831 j++; 12832 remote_used = 0; 12833 } 12834 total_used += cur_len; 12835 12836 if (total_used >= io->scsiio.kern_data_len) 12837 rq->callback = callback; 12838 12839 isc_ret = ctl_dt_single(rq); 12840 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12841 break; 12842 } 12843 if (isc_ret != CTL_HA_STATUS_WAIT) { 12844 rq->ret = isc_ret; 12845 callback(rq); 12846 } 12847 12848 return (0); 12849 } 12850 12851 static void 12852 ctl_datamove_remote_read(union ctl_io *io) 12853 { 12854 int retval; 12855 uint32_t i; 12856 12857 /* 12858 * This will send an error to the other controller in the case of a 12859 * failure. 12860 */ 12861 retval = ctl_datamove_remote_sgl_setup(io); 12862 if (retval != 0) 12863 return; 12864 12865 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12866 ctl_datamove_remote_read_cb); 12867 if (retval != 0) { 12868 /* 12869 * Make sure we free memory if there was an error.. The 12870 * ctl_datamove_remote_xfer() function will send the 12871 * datamove done message, or call the callback with an 12872 * error if there is a problem. 12873 */ 12874 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12875 free(CTL_LSGLT(io)[i].addr, M_CTL); 12876 free(CTL_RSGL(io), M_CTL); 12877 CTL_RSGL(io) = NULL; 12878 CTL_LSGL(io) = NULL; 12879 } 12880 } 12881 12882 /* 12883 * Process a datamove request from the other controller. This is used for 12884 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12885 * first. Once that is complete, the data gets DMAed into the remote 12886 * controller's memory. For reads, we DMA from the remote controller's 12887 * memory into our memory first, and then move it out to the FETD. 12888 */ 12889 static void 12890 ctl_datamove_remote(union ctl_io *io) 12891 { 12892 12893 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12894 12895 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12896 ctl_failover_io(io, /*have_lock*/ 0); 12897 return; 12898 } 12899 12900 /* 12901 * Note that we look for an aborted I/O here, but don't do some of 12902 * the other checks that ctl_datamove() normally does. 12903 * We don't need to run the datamove delay code, since that should 12904 * have been done if need be on the other controller. 12905 */ 12906 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12907 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12908 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12909 io->io_hdr.nexus.targ_port, 12910 io->io_hdr.nexus.targ_lun); 12911 io->io_hdr.port_status = 31338; 12912 ctl_send_datamove_done(io, /*have_lock*/ 0); 12913 return; 12914 } 12915 12916 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12917 ctl_datamove_remote_write(io); 12918 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12919 ctl_datamove_remote_read(io); 12920 else { 12921 io->io_hdr.port_status = 31339; 12922 ctl_send_datamove_done(io, /*have_lock*/ 0); 12923 } 12924 } 12925 12926 static void 12927 ctl_process_done(union ctl_io *io) 12928 { 12929 struct ctl_softc *softc = CTL_SOFTC(io); 12930 struct ctl_port *port = CTL_PORT(io); 12931 struct ctl_lun *lun = CTL_LUN(io); 12932 void (*fe_done)(union ctl_io *io); 12933 union ctl_ha_msg msg; 12934 12935 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12936 fe_done = port->fe_done; 12937 12938 #ifdef CTL_TIME_IO 12939 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12940 char str[256]; 12941 char path_str[64]; 12942 struct sbuf sb; 12943 12944 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12945 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12946 12947 sbuf_cat(&sb, path_str); 12948 switch (io->io_hdr.io_type) { 12949 case CTL_IO_SCSI: 12950 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12951 sbuf_printf(&sb, "\n"); 12952 sbuf_cat(&sb, path_str); 12953 sbuf_printf(&sb, "Tag: 0x%04x/%d, Prio: %d\n", 12954 io->scsiio.tag_num, io->scsiio.tag_type, 12955 io->scsiio.priority); 12956 break; 12957 case CTL_IO_TASK: 12958 sbuf_printf(&sb, "Task Action: %d Tag: 0x%04x/%d\n", 12959 io->taskio.task_action, 12960 io->taskio.tag_num, io->taskio.tag_type); 12961 break; 12962 default: 12963 panic("%s: Invalid CTL I/O type %d\n", 12964 __func__, io->io_hdr.io_type); 12965 } 12966 sbuf_cat(&sb, path_str); 12967 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12968 (intmax_t)time_uptime - io->io_hdr.start_time); 12969 sbuf_finish(&sb); 12970 printf("%s", sbuf_data(&sb)); 12971 } 12972 #endif /* CTL_TIME_IO */ 12973 12974 switch (io->io_hdr.io_type) { 12975 case CTL_IO_SCSI: 12976 break; 12977 case CTL_IO_TASK: 12978 if (ctl_debug & CTL_DEBUG_INFO) 12979 ctl_io_error_print(io, NULL); 12980 fe_done(io); 12981 return; 12982 default: 12983 panic("%s: Invalid CTL I/O type %d\n", 12984 __func__, io->io_hdr.io_type); 12985 } 12986 12987 if (lun == NULL) { 12988 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12989 io->io_hdr.nexus.targ_mapped_lun)); 12990 goto bailout; 12991 } 12992 12993 mtx_lock(&lun->lun_lock); 12994 12995 /* 12996 * Check to see if we have any informational exception and status 12997 * of this command can be modified to report it in form of either 12998 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 12999 */ 13000 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13001 io->io_hdr.status == CTL_SUCCESS && 13002 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13003 uint8_t mrie = lun->MODE_IE.mrie; 13004 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13005 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13006 if (((mrie == SIEP_MRIE_REC_COND && per) || 13007 mrie == SIEP_MRIE_REC_UNCOND || 13008 mrie == SIEP_MRIE_NO_SENSE) && 13009 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13010 CTL_CMD_FLAG_NO_SENSE) == 0) { 13011 ctl_set_sense(&io->scsiio, 13012 /*current_error*/ 1, 13013 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13014 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13015 /*asc*/ lun->ie_asc, 13016 /*ascq*/ lun->ie_ascq, 13017 SSD_ELEM_NONE); 13018 lun->ie_reported = 1; 13019 } 13020 } else if (lun->ie_reported < 0) 13021 lun->ie_reported = 0; 13022 13023 /* 13024 * Check to see if we have any errors to inject here. We only 13025 * inject errors for commands that don't already have errors set. 13026 */ 13027 if (!STAILQ_EMPTY(&lun->error_list) && 13028 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13029 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13030 ctl_inject_error(lun, io); 13031 13032 /* 13033 * XXX KDM how do we treat commands that aren't completed 13034 * successfully? 13035 * 13036 * XXX KDM should we also track I/O latency? 13037 */ 13038 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13039 io->io_hdr.io_type == CTL_IO_SCSI) { 13040 int type; 13041 #ifdef CTL_TIME_IO 13042 struct bintime bt; 13043 13044 getbinuptime(&bt); 13045 bintime_sub(&bt, &io->io_hdr.start_bt); 13046 #endif 13047 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13048 CTL_FLAG_DATA_IN) 13049 type = CTL_STATS_READ; 13050 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13051 CTL_FLAG_DATA_OUT) 13052 type = CTL_STATS_WRITE; 13053 else 13054 type = CTL_STATS_NO_IO; 13055 13056 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13057 lun->stats.operations[type] ++; 13058 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13059 #ifdef CTL_TIME_IO 13060 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13061 bintime_add(&lun->stats.time[type], &bt); 13062 #endif 13063 13064 mtx_lock(&port->port_lock); 13065 port->stats.bytes[type] += io->scsiio.kern_total_len; 13066 port->stats.operations[type] ++; 13067 port->stats.dmas[type] += io->io_hdr.num_dmas; 13068 #ifdef CTL_TIME_IO 13069 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13070 bintime_add(&port->stats.time[type], &bt); 13071 #endif 13072 mtx_unlock(&port->port_lock); 13073 } 13074 13075 /* 13076 * Run through the blocked queue of this I/O and see if anything 13077 * can be unblocked, now that this I/O is done and will be removed. 13078 * We need to do it before removal to have OOA position to start. 13079 */ 13080 ctl_try_unblock_others(lun, io, TRUE); 13081 13082 /* 13083 * Remove this from the OOA queue. 13084 */ 13085 LIST_REMOVE(&io->io_hdr, ooa_links); 13086 #ifdef CTL_TIME_IO 13087 if (LIST_EMPTY(&lun->ooa_queue)) 13088 lun->last_busy = getsbinuptime(); 13089 #endif 13090 13091 /* 13092 * If the LUN has been invalidated, free it if there is nothing 13093 * left on its OOA queue. 13094 */ 13095 if ((lun->flags & CTL_LUN_INVALID) 13096 && LIST_EMPTY(&lun->ooa_queue)) { 13097 mtx_unlock(&lun->lun_lock); 13098 ctl_free_lun(lun); 13099 } else 13100 mtx_unlock(&lun->lun_lock); 13101 13102 bailout: 13103 13104 /* 13105 * If this command has been aborted, make sure we set the status 13106 * properly. The FETD is responsible for freeing the I/O and doing 13107 * whatever it needs to do to clean up its state. 13108 */ 13109 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13110 ctl_set_task_aborted(&io->scsiio); 13111 13112 /* 13113 * If enabled, print command error status. 13114 */ 13115 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13116 (ctl_debug & CTL_DEBUG_INFO) != 0) 13117 ctl_io_error_print(io, NULL); 13118 13119 /* 13120 * Tell the FETD or the other shelf controller we're done with this 13121 * command. Note that only SCSI commands get to this point. Task 13122 * management commands are completed above. 13123 */ 13124 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13125 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13126 memset(&msg, 0, sizeof(msg)); 13127 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13128 msg.hdr.serializing_sc = io->io_hdr.remote_io; 13129 msg.hdr.nexus = io->io_hdr.nexus; 13130 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13131 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13132 M_WAITOK); 13133 } 13134 13135 fe_done(io); 13136 } 13137 13138 /* 13139 * Front end should call this if it doesn't do autosense. When the request 13140 * sense comes back in from the initiator, we'll dequeue this and send it. 13141 */ 13142 int 13143 ctl_queue_sense(union ctl_io *io) 13144 { 13145 struct ctl_softc *softc = CTL_SOFTC(io); 13146 struct ctl_port *port = CTL_PORT(io); 13147 struct ctl_lun *lun; 13148 struct scsi_sense_data *ps; 13149 uint32_t initidx, p, targ_lun; 13150 13151 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13152 13153 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13154 13155 /* 13156 * LUN lookup will likely move to the ctl_work_thread() once we 13157 * have our new queueing infrastructure (that doesn't put things on 13158 * a per-LUN queue initially). That is so that we can handle 13159 * things like an INQUIRY to a LUN that we don't have enabled. We 13160 * can't deal with that right now. 13161 * If we don't have a LUN for this, just toss the sense information. 13162 */ 13163 mtx_lock(&softc->ctl_lock); 13164 if (targ_lun >= ctl_max_luns || 13165 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13166 mtx_unlock(&softc->ctl_lock); 13167 goto bailout; 13168 } 13169 mtx_lock(&lun->lun_lock); 13170 mtx_unlock(&softc->ctl_lock); 13171 13172 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13173 p = initidx / CTL_MAX_INIT_PER_PORT; 13174 if (lun->pending_sense[p] == NULL) { 13175 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13176 M_CTL, M_NOWAIT | M_ZERO); 13177 } 13178 if ((ps = lun->pending_sense[p]) != NULL) { 13179 ps += initidx % CTL_MAX_INIT_PER_PORT; 13180 memset(ps, 0, sizeof(*ps)); 13181 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13182 } 13183 mtx_unlock(&lun->lun_lock); 13184 13185 bailout: 13186 ctl_free_io(io); 13187 return (CTL_RETVAL_COMPLETE); 13188 } 13189 13190 /* 13191 * Primary command inlet from frontend ports. All SCSI and task I/O 13192 * requests must go through this function. 13193 */ 13194 int 13195 ctl_queue(union ctl_io *io) 13196 { 13197 struct ctl_port *port = CTL_PORT(io); 13198 13199 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13200 13201 #ifdef CTL_TIME_IO 13202 io->io_hdr.start_time = time_uptime; 13203 getbinuptime(&io->io_hdr.start_bt); 13204 #endif /* CTL_TIME_IO */ 13205 13206 /* Map FE-specific LUN ID into global one. */ 13207 io->io_hdr.nexus.targ_mapped_lun = 13208 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13209 13210 switch (io->io_hdr.io_type) { 13211 case CTL_IO_SCSI: 13212 case CTL_IO_TASK: 13213 if (ctl_debug & CTL_DEBUG_CDB) 13214 ctl_io_print(io); 13215 ctl_enqueue_incoming(io); 13216 break; 13217 default: 13218 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13219 return (EINVAL); 13220 } 13221 13222 return (CTL_RETVAL_COMPLETE); 13223 } 13224 13225 int 13226 ctl_run(union ctl_io *io) 13227 { 13228 struct ctl_port *port = CTL_PORT(io); 13229 13230 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0])); 13231 13232 #ifdef CTL_TIME_IO 13233 io->io_hdr.start_time = time_uptime; 13234 getbinuptime(&io->io_hdr.start_bt); 13235 #endif /* CTL_TIME_IO */ 13236 13237 /* Map FE-specific LUN ID into global one. */ 13238 io->io_hdr.nexus.targ_mapped_lun = 13239 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13240 13241 switch (io->io_hdr.io_type) { 13242 case CTL_IO_SCSI: 13243 if (ctl_debug & CTL_DEBUG_CDB) 13244 ctl_io_print(io); 13245 ctl_scsiio_precheck(&io->scsiio); 13246 break; 13247 case CTL_IO_TASK: 13248 if (ctl_debug & CTL_DEBUG_CDB) 13249 ctl_io_print(io); 13250 ctl_run_task(io); 13251 break; 13252 default: 13253 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type); 13254 return (EINVAL); 13255 } 13256 13257 return (CTL_RETVAL_COMPLETE); 13258 } 13259 13260 #ifdef CTL_IO_DELAY 13261 static void 13262 ctl_done_timer_wakeup(void *arg) 13263 { 13264 union ctl_io *io; 13265 13266 io = (union ctl_io *)arg; 13267 ctl_done(io); 13268 } 13269 #endif /* CTL_IO_DELAY */ 13270 13271 void 13272 ctl_serseq_done(union ctl_io *io) 13273 { 13274 struct ctl_lun *lun = CTL_LUN(io); 13275 13276 /* This is racy, but should not be a problem. */ 13277 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) { 13278 mtx_lock(&lun->lun_lock); 13279 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13280 ctl_try_unblock_others(lun, io, FALSE); 13281 mtx_unlock(&lun->lun_lock); 13282 } else 13283 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13284 } 13285 13286 void 13287 ctl_done(union ctl_io *io) 13288 { 13289 13290 /* 13291 * Enable this to catch duplicate completion issues. 13292 */ 13293 #if 0 13294 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13295 printf("%s: type %d msg %d cdb %x iptl: " 13296 "%u:%u:%u tag 0x%04x " 13297 "flag %#x status %x\n", 13298 __func__, 13299 io->io_hdr.io_type, 13300 io->io_hdr.msg_type, 13301 io->scsiio.cdb[0], 13302 io->io_hdr.nexus.initid, 13303 io->io_hdr.nexus.targ_port, 13304 io->io_hdr.nexus.targ_lun, 13305 (io->io_hdr.io_type == 13306 CTL_IO_TASK) ? 13307 io->taskio.tag_num : 13308 io->scsiio.tag_num, 13309 io->io_hdr.flags, 13310 io->io_hdr.status); 13311 } else 13312 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13313 #endif 13314 13315 /* 13316 * This is an internal copy of an I/O, and should not go through 13317 * the normal done processing logic. 13318 */ 13319 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13320 return; 13321 13322 #ifdef CTL_IO_DELAY 13323 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13324 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13325 } else { 13326 struct ctl_lun *lun = CTL_LUN(io); 13327 13328 if ((lun != NULL) 13329 && (lun->delay_info.done_delay > 0)) { 13330 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13331 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13332 callout_reset(&io->io_hdr.delay_callout, 13333 lun->delay_info.done_delay * hz, 13334 ctl_done_timer_wakeup, io); 13335 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13336 lun->delay_info.done_delay = 0; 13337 return; 13338 } 13339 } 13340 #endif /* CTL_IO_DELAY */ 13341 13342 ctl_enqueue_done(io); 13343 } 13344 13345 static void 13346 ctl_work_thread(void *arg) 13347 { 13348 struct ctl_thread *thr = (struct ctl_thread *)arg; 13349 struct ctl_softc *softc = thr->ctl_softc; 13350 union ctl_io *io; 13351 int retval; 13352 13353 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13354 thread_lock(curthread); 13355 sched_prio(curthread, PUSER - 1); 13356 thread_unlock(curthread); 13357 13358 while (!softc->shutdown) { 13359 /* 13360 * We handle the queues in this order: 13361 * - ISC 13362 * - done queue (to free up resources, unblock other commands) 13363 * - incoming queue 13364 * - RtR queue 13365 * 13366 * If those queues are empty, we break out of the loop and 13367 * go to sleep. 13368 */ 13369 mtx_lock(&thr->queue_lock); 13370 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13371 if (io != NULL) { 13372 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13373 mtx_unlock(&thr->queue_lock); 13374 ctl_handle_isc(io); 13375 continue; 13376 } 13377 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13378 if (io != NULL) { 13379 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13380 /* clear any blocked commands, call fe_done */ 13381 mtx_unlock(&thr->queue_lock); 13382 ctl_process_done(io); 13383 continue; 13384 } 13385 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13386 if (io != NULL) { 13387 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13388 mtx_unlock(&thr->queue_lock); 13389 if (io->io_hdr.io_type == CTL_IO_TASK) 13390 ctl_run_task(io); 13391 else 13392 ctl_scsiio_precheck(&io->scsiio); 13393 continue; 13394 } 13395 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13396 if (io != NULL) { 13397 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13398 mtx_unlock(&thr->queue_lock); 13399 retval = ctl_scsiio(&io->scsiio); 13400 if (retval != CTL_RETVAL_COMPLETE) 13401 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13402 continue; 13403 } 13404 13405 /* Sleep until we have something to do. */ 13406 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0); 13407 } 13408 thr->thread = NULL; 13409 kthread_exit(); 13410 } 13411 13412 static void 13413 ctl_thresh_thread(void *arg) 13414 { 13415 struct ctl_softc *softc = (struct ctl_softc *)arg; 13416 struct ctl_lun *lun; 13417 struct ctl_logical_block_provisioning_page *page; 13418 const char *attr; 13419 union ctl_ha_msg msg; 13420 uint64_t thres, val; 13421 int i, e, set; 13422 13423 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13424 thread_lock(curthread); 13425 sched_prio(curthread, PUSER - 1); 13426 thread_unlock(curthread); 13427 13428 while (!softc->shutdown) { 13429 mtx_lock(&softc->ctl_lock); 13430 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13431 if ((lun->flags & CTL_LUN_DISABLED) || 13432 (lun->flags & CTL_LUN_NO_MEDIA) || 13433 lun->backend->lun_attr == NULL) 13434 continue; 13435 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13436 softc->ha_mode == CTL_HA_MODE_XFER) 13437 continue; 13438 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13439 continue; 13440 e = 0; 13441 page = &lun->MODE_LBP; 13442 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13443 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13444 continue; 13445 thres = scsi_4btoul(page->descr[i].count); 13446 thres <<= CTL_LBP_EXPONENT; 13447 switch (page->descr[i].resource) { 13448 case 0x01: 13449 attr = "blocksavail"; 13450 break; 13451 case 0x02: 13452 attr = "blocksused"; 13453 break; 13454 case 0xf1: 13455 attr = "poolblocksavail"; 13456 break; 13457 case 0xf2: 13458 attr = "poolblocksused"; 13459 break; 13460 default: 13461 continue; 13462 } 13463 mtx_unlock(&softc->ctl_lock); // XXX 13464 val = lun->backend->lun_attr(lun->be_lun, attr); 13465 mtx_lock(&softc->ctl_lock); 13466 if (val == UINT64_MAX) 13467 continue; 13468 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13469 == SLBPPD_ARMING_INC) 13470 e = (val >= thres); 13471 else 13472 e = (val <= thres); 13473 if (e) 13474 break; 13475 } 13476 mtx_lock(&lun->lun_lock); 13477 if (e) { 13478 scsi_u64to8b((uint8_t *)&page->descr[i] - 13479 (uint8_t *)page, lun->ua_tpt_info); 13480 if (lun->lasttpt == 0 || 13481 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13482 lun->lasttpt = time_uptime; 13483 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13484 set = 1; 13485 } else 13486 set = 0; 13487 } else { 13488 lun->lasttpt = 0; 13489 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13490 set = -1; 13491 } 13492 mtx_unlock(&lun->lun_lock); 13493 if (set != 0 && 13494 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13495 /* Send msg to other side. */ 13496 bzero(&msg.ua, sizeof(msg.ua)); 13497 msg.hdr.msg_type = CTL_MSG_UA; 13498 msg.hdr.nexus.initid = -1; 13499 msg.hdr.nexus.targ_port = -1; 13500 msg.hdr.nexus.targ_lun = lun->lun; 13501 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13502 msg.ua.ua_all = 1; 13503 msg.ua.ua_set = (set > 0); 13504 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13505 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13506 mtx_unlock(&softc->ctl_lock); // XXX 13507 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13508 sizeof(msg.ua), M_WAITOK); 13509 mtx_lock(&softc->ctl_lock); 13510 } 13511 } 13512 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13513 PDROP, "-", CTL_LBP_PERIOD * hz); 13514 } 13515 softc->thresh_thread = NULL; 13516 kthread_exit(); 13517 } 13518 13519 static void 13520 ctl_enqueue_incoming(union ctl_io *io) 13521 { 13522 struct ctl_softc *softc = CTL_SOFTC(io); 13523 struct ctl_thread *thr; 13524 u_int idx; 13525 13526 idx = (io->io_hdr.nexus.targ_port * 127 + 13527 io->io_hdr.nexus.initid) % worker_threads; 13528 thr = &softc->threads[idx]; 13529 mtx_lock(&thr->queue_lock); 13530 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13531 mtx_unlock(&thr->queue_lock); 13532 wakeup(thr); 13533 } 13534 13535 static void 13536 ctl_enqueue_rtr(union ctl_io *io) 13537 { 13538 struct ctl_softc *softc = CTL_SOFTC(io); 13539 struct ctl_thread *thr; 13540 13541 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13542 mtx_lock(&thr->queue_lock); 13543 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13544 mtx_unlock(&thr->queue_lock); 13545 wakeup(thr); 13546 } 13547 13548 static void 13549 ctl_enqueue_done(union ctl_io *io) 13550 { 13551 struct ctl_softc *softc = CTL_SOFTC(io); 13552 struct ctl_thread *thr; 13553 13554 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13555 mtx_lock(&thr->queue_lock); 13556 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13557 mtx_unlock(&thr->queue_lock); 13558 wakeup(thr); 13559 } 13560 13561 static void 13562 ctl_enqueue_isc(union ctl_io *io) 13563 { 13564 struct ctl_softc *softc = CTL_SOFTC(io); 13565 struct ctl_thread *thr; 13566 13567 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13568 mtx_lock(&thr->queue_lock); 13569 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13570 mtx_unlock(&thr->queue_lock); 13571 wakeup(thr); 13572 } 13573 13574 /* 13575 * vim: ts=8 13576 */ 13577