1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 5 * Copyright (c) 2012 The FreeBSD Foundation 6 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org> 7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org> 8 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org> 9 * All rights reserved. 10 * 11 * Portions of this software were developed by Edward Tomasz Napierala 12 * under sponsorship from the FreeBSD Foundation. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions, and the following disclaimer, 19 * without modification. 20 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 21 * substantially similar to the "NO WARRANTY" disclaimer below 22 * ("Disclaimer") and any redistribution must be conditioned upon 23 * including a substantially similar Disclaimer requirement for further 24 * binary redistribution. 25 * 26 * NO WARRANTY 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGES. 38 * 39 * $Id$ 40 */ 41 /* 42 * CAM Target Layer, a SCSI device emulation subsystem. 43 * 44 * Author: Ken Merry <ken@FreeBSD.org> 45 */ 46 47 #include <sys/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/ctype.h> 53 #include <sys/kernel.h> 54 #include <sys/types.h> 55 #include <sys/kthread.h> 56 #include <sys/bio.h> 57 #include <sys/fcntl.h> 58 #include <sys/lock.h> 59 #include <sys/module.h> 60 #include <sys/mutex.h> 61 #include <sys/condvar.h> 62 #include <sys/malloc.h> 63 #include <sys/conf.h> 64 #include <sys/ioccom.h> 65 #include <sys/queue.h> 66 #include <sys/sbuf.h> 67 #include <sys/smp.h> 68 #include <sys/endian.h> 69 #include <sys/proc.h> 70 #include <sys/sched.h> 71 #include <sys/sysctl.h> 72 #include <sys/nv.h> 73 #include <sys/dnv.h> 74 #include <vm/uma.h> 75 76 #include <cam/cam.h> 77 #include <cam/scsi/scsi_all.h> 78 #include <cam/scsi/scsi_cd.h> 79 #include <cam/scsi/scsi_da.h> 80 #include <cam/ctl/ctl_io.h> 81 #include <cam/ctl/ctl.h> 82 #include <cam/ctl/ctl_frontend.h> 83 #include <cam/ctl/ctl_util.h> 84 #include <cam/ctl/ctl_backend.h> 85 #include <cam/ctl/ctl_ioctl.h> 86 #include <cam/ctl/ctl_ha.h> 87 #include <cam/ctl/ctl_private.h> 88 #include <cam/ctl/ctl_debug.h> 89 #include <cam/ctl/ctl_scsi_all.h> 90 #include <cam/ctl/ctl_error.h> 91 92 struct ctl_softc *control_softc = NULL; 93 94 /* 95 * Template mode pages. 96 */ 97 98 /* 99 * Note that these are default values only. The actual values will be 100 * filled in when the user does a mode sense. 101 */ 102 const static struct scsi_da_rw_recovery_page rw_er_page_default = { 103 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 104 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 105 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 106 /*read_retry_count*/0, 107 /*correction_span*/0, 108 /*head_offset_count*/0, 109 /*data_strobe_offset_cnt*/0, 110 /*byte8*/SMS_RWER_LBPERE, 111 /*write_retry_count*/0, 112 /*reserved2*/0, 113 /*recovery_time_limit*/{0, 0}, 114 }; 115 116 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 117 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 118 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 119 /*byte3*/SMS_RWER_PER, 120 /*read_retry_count*/0, 121 /*correction_span*/0, 122 /*head_offset_count*/0, 123 /*data_strobe_offset_cnt*/0, 124 /*byte8*/SMS_RWER_LBPERE, 125 /*write_retry_count*/0, 126 /*reserved2*/0, 127 /*recovery_time_limit*/{0, 0}, 128 }; 129 130 const static struct scsi_format_page format_page_default = { 131 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 132 /*page_length*/sizeof(struct scsi_format_page) - 2, 133 /*tracks_per_zone*/ {0, 0}, 134 /*alt_sectors_per_zone*/ {0, 0}, 135 /*alt_tracks_per_zone*/ {0, 0}, 136 /*alt_tracks_per_lun*/ {0, 0}, 137 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 138 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 139 /*bytes_per_sector*/ {0, 0}, 140 /*interleave*/ {0, 0}, 141 /*track_skew*/ {0, 0}, 142 /*cylinder_skew*/ {0, 0}, 143 /*flags*/ SFP_HSEC, 144 /*reserved*/ {0, 0, 0} 145 }; 146 147 const static struct scsi_format_page format_page_changeable = { 148 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 149 /*page_length*/sizeof(struct scsi_format_page) - 2, 150 /*tracks_per_zone*/ {0, 0}, 151 /*alt_sectors_per_zone*/ {0, 0}, 152 /*alt_tracks_per_zone*/ {0, 0}, 153 /*alt_tracks_per_lun*/ {0, 0}, 154 /*sectors_per_track*/ {0, 0}, 155 /*bytes_per_sector*/ {0, 0}, 156 /*interleave*/ {0, 0}, 157 /*track_skew*/ {0, 0}, 158 /*cylinder_skew*/ {0, 0}, 159 /*flags*/ 0, 160 /*reserved*/ {0, 0, 0} 161 }; 162 163 const static struct scsi_rigid_disk_page rigid_disk_page_default = { 164 /*page_code*/SMS_RIGID_DISK_PAGE, 165 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 166 /*cylinders*/ {0, 0, 0}, 167 /*heads*/ CTL_DEFAULT_HEADS, 168 /*start_write_precomp*/ {0, 0, 0}, 169 /*start_reduced_current*/ {0, 0, 0}, 170 /*step_rate*/ {0, 0}, 171 /*landing_zone_cylinder*/ {0, 0, 0}, 172 /*rpl*/ SRDP_RPL_DISABLED, 173 /*rotational_offset*/ 0, 174 /*reserved1*/ 0, 175 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 176 CTL_DEFAULT_ROTATION_RATE & 0xff}, 177 /*reserved2*/ {0, 0} 178 }; 179 180 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 181 /*page_code*/SMS_RIGID_DISK_PAGE, 182 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 183 /*cylinders*/ {0, 0, 0}, 184 /*heads*/ 0, 185 /*start_write_precomp*/ {0, 0, 0}, 186 /*start_reduced_current*/ {0, 0, 0}, 187 /*step_rate*/ {0, 0}, 188 /*landing_zone_cylinder*/ {0, 0, 0}, 189 /*rpl*/ 0, 190 /*rotational_offset*/ 0, 191 /*reserved1*/ 0, 192 /*rotation_rate*/ {0, 0}, 193 /*reserved2*/ {0, 0} 194 }; 195 196 const static struct scsi_da_verify_recovery_page verify_er_page_default = { 197 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 198 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 199 /*byte3*/0, 200 /*read_retry_count*/0, 201 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 202 /*recovery_time_limit*/{0, 0}, 203 }; 204 205 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 206 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 207 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 208 /*byte3*/SMS_VER_PER, 209 /*read_retry_count*/0, 210 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 211 /*recovery_time_limit*/{0, 0}, 212 }; 213 214 const static struct scsi_caching_page caching_page_default = { 215 /*page_code*/SMS_CACHING_PAGE, 216 /*page_length*/sizeof(struct scsi_caching_page) - 2, 217 /*flags1*/ SCP_DISC | SCP_WCE, 218 /*ret_priority*/ 0, 219 /*disable_pf_transfer_len*/ {0xff, 0xff}, 220 /*min_prefetch*/ {0, 0}, 221 /*max_prefetch*/ {0xff, 0xff}, 222 /*max_pf_ceiling*/ {0xff, 0xff}, 223 /*flags2*/ 0, 224 /*cache_segments*/ 0, 225 /*cache_seg_size*/ {0, 0}, 226 /*reserved*/ 0, 227 /*non_cache_seg_size*/ {0, 0, 0} 228 }; 229 230 const static struct scsi_caching_page caching_page_changeable = { 231 /*page_code*/SMS_CACHING_PAGE, 232 /*page_length*/sizeof(struct scsi_caching_page) - 2, 233 /*flags1*/ SCP_WCE | SCP_RCD, 234 /*ret_priority*/ 0, 235 /*disable_pf_transfer_len*/ {0, 0}, 236 /*min_prefetch*/ {0, 0}, 237 /*max_prefetch*/ {0, 0}, 238 /*max_pf_ceiling*/ {0, 0}, 239 /*flags2*/ 0, 240 /*cache_segments*/ 0, 241 /*cache_seg_size*/ {0, 0}, 242 /*reserved*/ 0, 243 /*non_cache_seg_size*/ {0, 0, 0} 244 }; 245 246 const static struct scsi_control_page control_page_default = { 247 /*page_code*/SMS_CONTROL_MODE_PAGE, 248 /*page_length*/sizeof(struct scsi_control_page) - 2, 249 /*rlec*/0, 250 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 251 /*eca_and_aen*/0, 252 /*flags4*/SCP_TAS, 253 /*aen_holdoff_period*/{0, 0}, 254 /*busy_timeout_period*/{0, 0}, 255 /*extended_selftest_completion_time*/{0, 0} 256 }; 257 258 const static struct scsi_control_page control_page_changeable = { 259 /*page_code*/SMS_CONTROL_MODE_PAGE, 260 /*page_length*/sizeof(struct scsi_control_page) - 2, 261 /*rlec*/SCP_DSENSE, 262 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 263 /*eca_and_aen*/SCP_SWP, 264 /*flags4*/0, 265 /*aen_holdoff_period*/{0, 0}, 266 /*busy_timeout_period*/{0, 0}, 267 /*extended_selftest_completion_time*/{0, 0} 268 }; 269 270 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 271 272 const static struct scsi_control_ext_page control_ext_page_default = { 273 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 274 /*subpage_code*/0x01, 275 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 276 /*flags*/0, 277 /*prio*/0, 278 /*max_sense*/0 279 }; 280 281 const static struct scsi_control_ext_page control_ext_page_changeable = { 282 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 283 /*subpage_code*/0x01, 284 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 285 /*flags*/0, 286 /*prio*/0, 287 /*max_sense*/0xff 288 }; 289 290 const static struct scsi_info_exceptions_page ie_page_default = { 291 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 292 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 293 /*info_flags*/SIEP_FLAGS_EWASC, 294 /*mrie*/SIEP_MRIE_NO, 295 /*interval_timer*/{0, 0, 0, 0}, 296 /*report_count*/{0, 0, 0, 1} 297 }; 298 299 const static struct scsi_info_exceptions_page ie_page_changeable = { 300 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 301 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 302 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 303 SIEP_FLAGS_LOGERR, 304 /*mrie*/0x0f, 305 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 306 /*report_count*/{0xff, 0xff, 0xff, 0xff} 307 }; 308 309 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 310 311 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 312 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 313 /*subpage_code*/0x02, 314 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 315 /*flags*/0, 316 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 317 /*descr*/{}}, 318 {{/*flags*/0, 319 /*resource*/0x01, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0x02, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}}, 326 {/*flags*/0, 327 /*resource*/0xf1, 328 /*reserved*/{0, 0}, 329 /*count*/{0, 0, 0, 0}}, 330 {/*flags*/0, 331 /*resource*/0xf2, 332 /*reserved*/{0, 0}, 333 /*count*/{0, 0, 0, 0}} 334 } 335 }; 336 337 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 338 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 339 /*subpage_code*/0x02, 340 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 341 /*flags*/SLBPP_SITUA, 342 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 343 /*descr*/{}}, 344 {{/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}}, 352 {/*flags*/0, 353 /*resource*/0, 354 /*reserved*/{0, 0}, 355 /*count*/{0, 0, 0, 0}}, 356 {/*flags*/0, 357 /*resource*/0, 358 /*reserved*/{0, 0}, 359 /*count*/{0, 0, 0, 0}} 360 } 361 }; 362 363 const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 364 /*page_code*/SMS_CDDVD_CAPS_PAGE, 365 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 366 /*caps1*/0x3f, 367 /*caps2*/0x00, 368 /*caps3*/0xf0, 369 /*caps4*/0x00, 370 /*caps5*/0x29, 371 /*caps6*/0x00, 372 /*obsolete*/{0, 0}, 373 /*nvol_levels*/{0, 0}, 374 /*buffer_size*/{8, 0}, 375 /*obsolete2*/{0, 0}, 376 /*reserved*/0, 377 /*digital*/0, 378 /*obsolete3*/0, 379 /*copy_management*/0, 380 /*reserved2*/0, 381 /*rotation_control*/0, 382 /*cur_write_speed*/0, 383 /*num_speed_descr*/0, 384 }; 385 386 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 387 /*page_code*/SMS_CDDVD_CAPS_PAGE, 388 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 389 /*caps1*/0, 390 /*caps2*/0, 391 /*caps3*/0, 392 /*caps4*/0, 393 /*caps5*/0, 394 /*caps6*/0, 395 /*obsolete*/{0, 0}, 396 /*nvol_levels*/{0, 0}, 397 /*buffer_size*/{0, 0}, 398 /*obsolete2*/{0, 0}, 399 /*reserved*/0, 400 /*digital*/0, 401 /*obsolete3*/0, 402 /*copy_management*/0, 403 /*reserved2*/0, 404 /*rotation_control*/0, 405 /*cur_write_speed*/0, 406 /*num_speed_descr*/0, 407 }; 408 409 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 410 "CAM Target Layer"); 411 static int worker_threads = -1; 412 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 413 &worker_threads, 1, "Number of worker threads"); 414 static int ctl_debug = CTL_DEBUG_NONE; 415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 416 &ctl_debug, 0, "Enabled debug flags"); 417 static int ctl_lun_map_size = 1024; 418 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 419 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 420 #ifdef CTL_TIME_IO 421 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS; 422 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN, 423 &ctl_time_io_secs, 0, "Log requests taking more seconds"); 424 #endif 425 426 /* 427 * Maximum number of LUNs we support. MUST be a power of 2. 428 */ 429 #define CTL_DEFAULT_MAX_LUNS 1024 430 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 431 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns); 432 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN, 433 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs"); 434 435 /* 436 * Maximum number of ports registered at one time. 437 */ 438 #define CTL_DEFAULT_MAX_PORTS 256 439 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 440 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports); 441 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN, 442 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports"); 443 444 /* 445 * Maximum number of initiators we support. 446 */ 447 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports) 448 449 /* 450 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 451 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 452 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92), 453 * Block limits (0xB0), Block Device Characteristics (0xB1) and 454 * Logical Block Provisioning (0xB2) 455 */ 456 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11 457 458 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 459 int param); 460 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 461 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 462 static int ctl_init(void); 463 static int ctl_shutdown(void); 464 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 465 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 466 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 467 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 468 struct ctl_ooa *ooa_hdr, 469 struct ctl_ooa_entry *kern_entries); 470 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 471 struct thread *td); 472 static int ctl_enable_lun(struct ctl_lun *lun); 473 static int ctl_disable_lun(struct ctl_lun *lun); 474 static int ctl_free_lun(struct ctl_lun *lun); 475 476 static int ctl_do_mode_select(union ctl_io *io); 477 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 478 uint64_t res_key, uint64_t sa_res_key, 479 uint8_t type, uint32_t residx, 480 struct ctl_scsiio *ctsio, 481 struct scsi_per_res_out *cdb, 482 struct scsi_per_res_out_parms* param); 483 static void ctl_pro_preempt_other(struct ctl_lun *lun, 484 union ctl_ha_msg *msg); 485 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 486 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 487 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 488 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 489 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 490 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 491 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 492 int alloc_len); 493 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len); 494 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 495 int alloc_len); 496 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 497 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 498 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 499 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 500 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 501 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 502 bool seq); 503 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 504 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 505 union ctl_io *pending_io, union ctl_io *ooa_io); 506 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 507 union ctl_io **starting_io); 508 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, 509 bool skip); 510 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io, 511 bool skip); 512 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 513 const struct ctl_cmd_entry *entry, 514 struct ctl_scsiio *ctsio); 515 static void ctl_failover_lun(union ctl_io *io); 516 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 517 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); 540 static void ctl_datamove_remote_write(union ctl_io *io); 541 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 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 588 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 589 590 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 591 592 static moduledata_t ctl_moduledata = { 593 "ctl", 594 ctl_module_event_handler, 595 NULL 596 }; 597 598 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 599 MODULE_VERSION(ctl, 1); 600 601 static struct ctl_frontend ha_frontend = 602 { 603 .name = "ha", 604 .init = ctl_ha_init, 605 .shutdown = ctl_ha_shutdown, 606 }; 607 608 static int 609 ctl_ha_init(void) 610 { 611 struct ctl_softc *softc = control_softc; 612 613 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 614 &softc->othersc_pool) != 0) 615 return (ENOMEM); 616 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 617 ctl_pool_free(softc->othersc_pool); 618 return (EIO); 619 } 620 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 621 != CTL_HA_STATUS_SUCCESS) { 622 ctl_ha_msg_destroy(softc); 623 ctl_pool_free(softc->othersc_pool); 624 return (EIO); 625 } 626 return (0); 627 }; 628 629 static int 630 ctl_ha_shutdown(void) 631 { 632 struct ctl_softc *softc = control_softc; 633 struct ctl_port *port; 634 635 ctl_ha_msg_shutdown(softc); 636 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 637 return (EIO); 638 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 639 return (EIO); 640 ctl_pool_free(softc->othersc_pool); 641 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 642 ctl_port_deregister(port); 643 free(port->port_name, M_CTL); 644 free(port, M_CTL); 645 } 646 return (0); 647 }; 648 649 static void 650 ctl_ha_datamove(union ctl_io *io) 651 { 652 struct ctl_lun *lun = CTL_LUN(io); 653 struct ctl_sg_entry *sgl; 654 union ctl_ha_msg msg; 655 uint32_t sg_entries_sent; 656 int do_sg_copy, i, j; 657 658 memset(&msg.dt, 0, sizeof(msg.dt)); 659 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 660 msg.hdr.original_sc = io->io_hdr.remote_io; 661 msg.hdr.serializing_sc = io; 662 msg.hdr.nexus = io->io_hdr.nexus; 663 msg.hdr.status = io->io_hdr.status; 664 msg.dt.flags = io->io_hdr.flags; 665 666 /* 667 * We convert everything into a S/G list here. We can't 668 * pass by reference, only by value between controllers. 669 * So we can't pass a pointer to the S/G list, only as many 670 * S/G entries as we can fit in here. If it's possible for 671 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 672 * then we need to break this up into multiple transfers. 673 */ 674 if (io->scsiio.kern_sg_entries == 0) { 675 msg.dt.kern_sg_entries = 1; 676 #if 0 677 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 678 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 679 } else { 680 /* XXX KDM use busdma here! */ 681 msg.dt.sg_list[0].addr = 682 (void *)vtophys(io->scsiio.kern_data_ptr); 683 } 684 #else 685 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 686 ("HA does not support BUS_ADDR")); 687 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 688 #endif 689 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 690 do_sg_copy = 0; 691 } else { 692 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 693 do_sg_copy = 1; 694 } 695 696 msg.dt.kern_data_len = io->scsiio.kern_data_len; 697 msg.dt.kern_total_len = io->scsiio.kern_total_len; 698 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 699 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 700 msg.dt.sg_sequence = 0; 701 702 /* 703 * Loop until we've sent all of the S/G entries. On the 704 * other end, we'll recompose these S/G entries into one 705 * contiguous list before processing. 706 */ 707 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 708 msg.dt.sg_sequence++) { 709 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 710 sizeof(msg.dt.sg_list[0])), 711 msg.dt.kern_sg_entries - sg_entries_sent); 712 if (do_sg_copy != 0) { 713 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 714 for (i = sg_entries_sent, j = 0; 715 i < msg.dt.cur_sg_entries; i++, j++) { 716 #if 0 717 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 718 msg.dt.sg_list[j].addr = sgl[i].addr; 719 } else { 720 /* XXX KDM use busdma here! */ 721 msg.dt.sg_list[j].addr = 722 (void *)vtophys(sgl[i].addr); 723 } 724 #else 725 KASSERT((io->io_hdr.flags & 726 CTL_FLAG_BUS_ADDR) == 0, 727 ("HA does not support BUS_ADDR")); 728 msg.dt.sg_list[j].addr = sgl[i].addr; 729 #endif 730 msg.dt.sg_list[j].len = sgl[i].len; 731 } 732 } 733 734 sg_entries_sent += msg.dt.cur_sg_entries; 735 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 736 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 737 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 738 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 739 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 740 io->io_hdr.port_status = 31341; 741 io->scsiio.be_move_done(io); 742 return; 743 } 744 msg.dt.sent_sg_entries = sg_entries_sent; 745 } 746 747 /* 748 * Officially handover the request from us to peer. 749 * If failover has just happened, then we must return error. 750 * If failover happen just after, then it is not our problem. 751 */ 752 if (lun) 753 mtx_lock(&lun->lun_lock); 754 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 755 if (lun) 756 mtx_unlock(&lun->lun_lock); 757 io->io_hdr.port_status = 31342; 758 io->scsiio.be_move_done(io); 759 return; 760 } 761 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 762 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 763 if (lun) 764 mtx_unlock(&lun->lun_lock); 765 } 766 767 static void 768 ctl_ha_done(union ctl_io *io) 769 { 770 union ctl_ha_msg msg; 771 772 if (io->io_hdr.io_type == CTL_IO_SCSI) { 773 memset(&msg, 0, sizeof(msg)); 774 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 775 msg.hdr.original_sc = io->io_hdr.remote_io; 776 msg.hdr.nexus = io->io_hdr.nexus; 777 msg.hdr.status = io->io_hdr.status; 778 msg.scsi.scsi_status = io->scsiio.scsi_status; 779 msg.scsi.tag_num = io->scsiio.tag_num; 780 msg.scsi.tag_type = io->scsiio.tag_type; 781 msg.scsi.sense_len = io->scsiio.sense_len; 782 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 783 io->scsiio.sense_len); 784 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 785 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 786 msg.scsi.sense_len, M_WAITOK); 787 } 788 ctl_free_io(io); 789 } 790 791 static void 792 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 793 union ctl_ha_msg *msg_info) 794 { 795 struct ctl_scsiio *ctsio; 796 797 if (msg_info->hdr.original_sc == NULL) { 798 printf("%s: original_sc == NULL!\n", __func__); 799 /* XXX KDM now what? */ 800 return; 801 } 802 803 ctsio = &msg_info->hdr.original_sc->scsiio; 804 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 805 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 806 ctsio->io_hdr.status = msg_info->hdr.status; 807 ctsio->scsi_status = msg_info->scsi.scsi_status; 808 ctsio->sense_len = msg_info->scsi.sense_len; 809 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 810 msg_info->scsi.sense_len); 811 ctl_enqueue_isc((union ctl_io *)ctsio); 812 } 813 814 static void 815 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 816 union ctl_ha_msg *msg_info) 817 { 818 struct ctl_scsiio *ctsio; 819 820 if (msg_info->hdr.serializing_sc == NULL) { 821 printf("%s: serializing_sc == NULL!\n", __func__); 822 /* XXX KDM now what? */ 823 return; 824 } 825 826 ctsio = &msg_info->hdr.serializing_sc->scsiio; 827 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 828 ctl_enqueue_isc((union ctl_io *)ctsio); 829 } 830 831 void 832 ctl_isc_announce_lun(struct ctl_lun *lun) 833 { 834 struct ctl_softc *softc = lun->ctl_softc; 835 union ctl_ha_msg *msg; 836 struct ctl_ha_msg_lun_pr_key pr_key; 837 int i, k; 838 839 if (softc->ha_link != CTL_HA_LINK_ONLINE) 840 return; 841 mtx_lock(&lun->lun_lock); 842 i = sizeof(msg->lun); 843 if (lun->lun_devid) 844 i += lun->lun_devid->len; 845 i += sizeof(pr_key) * lun->pr_key_count; 846 alloc: 847 mtx_unlock(&lun->lun_lock); 848 msg = malloc(i, M_CTL, M_WAITOK); 849 mtx_lock(&lun->lun_lock); 850 k = sizeof(msg->lun); 851 if (lun->lun_devid) 852 k += lun->lun_devid->len; 853 k += sizeof(pr_key) * lun->pr_key_count; 854 if (i < k) { 855 free(msg, M_CTL); 856 i = k; 857 goto alloc; 858 } 859 bzero(&msg->lun, sizeof(msg->lun)); 860 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 861 msg->hdr.nexus.targ_lun = lun->lun; 862 msg->hdr.nexus.targ_mapped_lun = lun->lun; 863 msg->lun.flags = lun->flags; 864 msg->lun.pr_generation = lun->pr_generation; 865 msg->lun.pr_res_idx = lun->pr_res_idx; 866 msg->lun.pr_res_type = lun->pr_res_type; 867 msg->lun.pr_key_count = lun->pr_key_count; 868 i = 0; 869 if (lun->lun_devid) { 870 msg->lun.lun_devid_len = lun->lun_devid->len; 871 memcpy(&msg->lun.data[i], lun->lun_devid->data, 872 msg->lun.lun_devid_len); 873 i += msg->lun.lun_devid_len; 874 } 875 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 876 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 877 continue; 878 pr_key.pr_iid = k; 879 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 880 i += sizeof(pr_key); 881 } 882 mtx_unlock(&lun->lun_lock); 883 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 884 M_WAITOK); 885 free(msg, M_CTL); 886 887 if (lun->flags & CTL_LUN_PRIMARY_SC) { 888 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 889 ctl_isc_announce_mode(lun, -1, 890 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 891 lun->mode_pages.index[i].subpage); 892 } 893 } 894 } 895 896 void 897 ctl_isc_announce_port(struct ctl_port *port) 898 { 899 struct ctl_softc *softc = port->ctl_softc; 900 union ctl_ha_msg *msg; 901 int i; 902 903 if (port->targ_port < softc->port_min || 904 port->targ_port >= softc->port_max || 905 softc->ha_link != CTL_HA_LINK_ONLINE) 906 return; 907 i = sizeof(msg->port) + strlen(port->port_name) + 1; 908 if (port->lun_map) 909 i += port->lun_map_size * sizeof(uint32_t); 910 if (port->port_devid) 911 i += port->port_devid->len; 912 if (port->target_devid) 913 i += port->target_devid->len; 914 if (port->init_devid) 915 i += port->init_devid->len; 916 msg = malloc(i, M_CTL, M_WAITOK); 917 bzero(&msg->port, sizeof(msg->port)); 918 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 919 msg->hdr.nexus.targ_port = port->targ_port; 920 msg->port.port_type = port->port_type; 921 msg->port.physical_port = port->physical_port; 922 msg->port.virtual_port = port->virtual_port; 923 msg->port.status = port->status; 924 i = 0; 925 msg->port.name_len = sprintf(&msg->port.data[i], 926 "%d:%s", softc->ha_id, port->port_name) + 1; 927 i += msg->port.name_len; 928 if (port->lun_map) { 929 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 930 memcpy(&msg->port.data[i], port->lun_map, 931 msg->port.lun_map_len); 932 i += msg->port.lun_map_len; 933 } 934 if (port->port_devid) { 935 msg->port.port_devid_len = port->port_devid->len; 936 memcpy(&msg->port.data[i], port->port_devid->data, 937 msg->port.port_devid_len); 938 i += msg->port.port_devid_len; 939 } 940 if (port->target_devid) { 941 msg->port.target_devid_len = port->target_devid->len; 942 memcpy(&msg->port.data[i], port->target_devid->data, 943 msg->port.target_devid_len); 944 i += msg->port.target_devid_len; 945 } 946 if (port->init_devid) { 947 msg->port.init_devid_len = port->init_devid->len; 948 memcpy(&msg->port.data[i], port->init_devid->data, 949 msg->port.init_devid_len); 950 i += msg->port.init_devid_len; 951 } 952 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 953 M_WAITOK); 954 free(msg, M_CTL); 955 } 956 957 void 958 ctl_isc_announce_iid(struct ctl_port *port, int iid) 959 { 960 struct ctl_softc *softc = port->ctl_softc; 961 union ctl_ha_msg *msg; 962 int i, l; 963 964 if (port->targ_port < softc->port_min || 965 port->targ_port >= softc->port_max || 966 softc->ha_link != CTL_HA_LINK_ONLINE) 967 return; 968 mtx_lock(&softc->ctl_lock); 969 i = sizeof(msg->iid); 970 l = 0; 971 if (port->wwpn_iid[iid].name) 972 l = strlen(port->wwpn_iid[iid].name) + 1; 973 i += l; 974 msg = malloc(i, M_CTL, M_NOWAIT); 975 if (msg == NULL) { 976 mtx_unlock(&softc->ctl_lock); 977 return; 978 } 979 bzero(&msg->iid, sizeof(msg->iid)); 980 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 981 msg->hdr.nexus.targ_port = port->targ_port; 982 msg->hdr.nexus.initid = iid; 983 msg->iid.in_use = port->wwpn_iid[iid].in_use; 984 msg->iid.name_len = l; 985 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 986 if (port->wwpn_iid[iid].name) 987 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 988 mtx_unlock(&softc->ctl_lock); 989 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 990 free(msg, M_CTL); 991 } 992 993 void 994 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 995 uint8_t page, uint8_t subpage) 996 { 997 struct ctl_softc *softc = lun->ctl_softc; 998 union ctl_ha_msg msg; 999 u_int i; 1000 1001 if (softc->ha_link != CTL_HA_LINK_ONLINE) 1002 return; 1003 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1004 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1005 page && lun->mode_pages.index[i].subpage == subpage) 1006 break; 1007 } 1008 if (i == CTL_NUM_MODE_PAGES) 1009 return; 1010 1011 /* Don't try to replicate pages not present on this device. */ 1012 if (lun->mode_pages.index[i].page_data == NULL) 1013 return; 1014 1015 bzero(&msg.mode, sizeof(msg.mode)); 1016 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 1017 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 1018 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 1019 msg.hdr.nexus.targ_lun = lun->lun; 1020 msg.hdr.nexus.targ_mapped_lun = lun->lun; 1021 msg.mode.page_code = page; 1022 msg.mode.subpage = subpage; 1023 msg.mode.page_len = lun->mode_pages.index[i].page_len; 1024 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 1025 msg.mode.page_len); 1026 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 1027 M_WAITOK); 1028 } 1029 1030 static void 1031 ctl_isc_ha_link_up(struct ctl_softc *softc) 1032 { 1033 struct ctl_port *port; 1034 struct ctl_lun *lun; 1035 union ctl_ha_msg msg; 1036 int i; 1037 1038 /* Announce this node parameters to peer for validation. */ 1039 msg.login.msg_type = CTL_MSG_LOGIN; 1040 msg.login.version = CTL_HA_VERSION; 1041 msg.login.ha_mode = softc->ha_mode; 1042 msg.login.ha_id = softc->ha_id; 1043 msg.login.max_luns = ctl_max_luns; 1044 msg.login.max_ports = ctl_max_ports; 1045 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1046 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1047 M_WAITOK); 1048 1049 STAILQ_FOREACH(port, &softc->port_list, links) { 1050 ctl_isc_announce_port(port); 1051 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1052 if (port->wwpn_iid[i].in_use) 1053 ctl_isc_announce_iid(port, i); 1054 } 1055 } 1056 STAILQ_FOREACH(lun, &softc->lun_list, links) 1057 ctl_isc_announce_lun(lun); 1058 } 1059 1060 static void 1061 ctl_isc_ha_link_down(struct ctl_softc *softc) 1062 { 1063 struct ctl_port *port; 1064 struct ctl_lun *lun; 1065 union ctl_io *io; 1066 int i; 1067 1068 mtx_lock(&softc->ctl_lock); 1069 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1070 mtx_lock(&lun->lun_lock); 1071 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1072 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1073 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1074 } 1075 mtx_unlock(&lun->lun_lock); 1076 1077 mtx_unlock(&softc->ctl_lock); 1078 io = ctl_alloc_io(softc->othersc_pool); 1079 mtx_lock(&softc->ctl_lock); 1080 ctl_zero_io(io); 1081 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1082 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1083 ctl_enqueue_isc(io); 1084 } 1085 1086 STAILQ_FOREACH(port, &softc->port_list, links) { 1087 if (port->targ_port >= softc->port_min && 1088 port->targ_port < softc->port_max) 1089 continue; 1090 port->status &= ~CTL_PORT_STATUS_ONLINE; 1091 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1092 port->wwpn_iid[i].in_use = 0; 1093 free(port->wwpn_iid[i].name, M_CTL); 1094 port->wwpn_iid[i].name = NULL; 1095 } 1096 } 1097 mtx_unlock(&softc->ctl_lock); 1098 } 1099 1100 static void 1101 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1102 { 1103 struct ctl_lun *lun; 1104 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1105 1106 mtx_lock(&softc->ctl_lock); 1107 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1108 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1109 mtx_unlock(&softc->ctl_lock); 1110 return; 1111 } 1112 mtx_lock(&lun->lun_lock); 1113 mtx_unlock(&softc->ctl_lock); 1114 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1115 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1116 if (msg->ua.ua_all) { 1117 if (msg->ua.ua_set) 1118 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1119 else 1120 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1121 } else { 1122 if (msg->ua.ua_set) 1123 ctl_est_ua(lun, iid, msg->ua.ua_type); 1124 else 1125 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1126 } 1127 mtx_unlock(&lun->lun_lock); 1128 } 1129 1130 static void 1131 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1132 { 1133 struct ctl_lun *lun; 1134 struct ctl_ha_msg_lun_pr_key pr_key; 1135 int i, k; 1136 ctl_lun_flags oflags; 1137 uint32_t targ_lun; 1138 1139 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1140 mtx_lock(&softc->ctl_lock); 1141 if (targ_lun >= ctl_max_luns || 1142 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1143 mtx_unlock(&softc->ctl_lock); 1144 return; 1145 } 1146 mtx_lock(&lun->lun_lock); 1147 mtx_unlock(&softc->ctl_lock); 1148 if (lun->flags & CTL_LUN_DISABLED) { 1149 mtx_unlock(&lun->lun_lock); 1150 return; 1151 } 1152 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1153 if (msg->lun.lun_devid_len != i || (i > 0 && 1154 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1155 mtx_unlock(&lun->lun_lock); 1156 printf("%s: Received conflicting HA LUN %d\n", 1157 __func__, targ_lun); 1158 return; 1159 } else { 1160 /* Record whether peer is primary. */ 1161 oflags = lun->flags; 1162 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1163 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1164 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1165 else 1166 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1167 if (oflags != lun->flags) 1168 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1169 1170 /* If peer is primary and we are not -- use data */ 1171 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1172 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1173 lun->pr_generation = msg->lun.pr_generation; 1174 lun->pr_res_idx = msg->lun.pr_res_idx; 1175 lun->pr_res_type = msg->lun.pr_res_type; 1176 lun->pr_key_count = msg->lun.pr_key_count; 1177 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1178 ctl_clr_prkey(lun, k); 1179 for (k = 0; k < msg->lun.pr_key_count; k++) { 1180 memcpy(&pr_key, &msg->lun.data[i], 1181 sizeof(pr_key)); 1182 ctl_alloc_prkey(lun, pr_key.pr_iid); 1183 ctl_set_prkey(lun, pr_key.pr_iid, 1184 pr_key.pr_key); 1185 i += sizeof(pr_key); 1186 } 1187 } 1188 1189 mtx_unlock(&lun->lun_lock); 1190 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1191 __func__, targ_lun, 1192 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1193 "primary" : "secondary")); 1194 1195 /* If we are primary but peer doesn't know -- notify */ 1196 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1197 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1198 ctl_isc_announce_lun(lun); 1199 } 1200 } 1201 1202 static void 1203 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1204 { 1205 struct ctl_port *port; 1206 struct ctl_lun *lun; 1207 int i, new; 1208 1209 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1210 if (port == NULL) { 1211 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1212 msg->hdr.nexus.targ_port)); 1213 new = 1; 1214 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1215 port->frontend = &ha_frontend; 1216 port->targ_port = msg->hdr.nexus.targ_port; 1217 port->fe_datamove = ctl_ha_datamove; 1218 port->fe_done = ctl_ha_done; 1219 } else if (port->frontend == &ha_frontend) { 1220 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1221 msg->hdr.nexus.targ_port)); 1222 new = 0; 1223 } else { 1224 printf("%s: Received conflicting HA port %d\n", 1225 __func__, msg->hdr.nexus.targ_port); 1226 return; 1227 } 1228 port->port_type = msg->port.port_type; 1229 port->physical_port = msg->port.physical_port; 1230 port->virtual_port = msg->port.virtual_port; 1231 port->status = msg->port.status; 1232 i = 0; 1233 free(port->port_name, M_CTL); 1234 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1235 M_CTL); 1236 i += msg->port.name_len; 1237 if (msg->port.lun_map_len != 0) { 1238 if (port->lun_map == NULL || 1239 port->lun_map_size * sizeof(uint32_t) < 1240 msg->port.lun_map_len) { 1241 port->lun_map_size = 0; 1242 free(port->lun_map, M_CTL); 1243 port->lun_map = malloc(msg->port.lun_map_len, 1244 M_CTL, M_WAITOK); 1245 } 1246 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1247 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1248 i += msg->port.lun_map_len; 1249 } else { 1250 port->lun_map_size = 0; 1251 free(port->lun_map, M_CTL); 1252 port->lun_map = NULL; 1253 } 1254 if (msg->port.port_devid_len != 0) { 1255 if (port->port_devid == NULL || 1256 port->port_devid->len < msg->port.port_devid_len) { 1257 free(port->port_devid, M_CTL); 1258 port->port_devid = malloc(sizeof(struct ctl_devid) + 1259 msg->port.port_devid_len, M_CTL, M_WAITOK); 1260 } 1261 memcpy(port->port_devid->data, &msg->port.data[i], 1262 msg->port.port_devid_len); 1263 port->port_devid->len = msg->port.port_devid_len; 1264 i += msg->port.port_devid_len; 1265 } else { 1266 free(port->port_devid, M_CTL); 1267 port->port_devid = NULL; 1268 } 1269 if (msg->port.target_devid_len != 0) { 1270 if (port->target_devid == NULL || 1271 port->target_devid->len < msg->port.target_devid_len) { 1272 free(port->target_devid, M_CTL); 1273 port->target_devid = malloc(sizeof(struct ctl_devid) + 1274 msg->port.target_devid_len, M_CTL, M_WAITOK); 1275 } 1276 memcpy(port->target_devid->data, &msg->port.data[i], 1277 msg->port.target_devid_len); 1278 port->target_devid->len = msg->port.target_devid_len; 1279 i += msg->port.target_devid_len; 1280 } else { 1281 free(port->target_devid, M_CTL); 1282 port->target_devid = NULL; 1283 } 1284 if (msg->port.init_devid_len != 0) { 1285 if (port->init_devid == NULL || 1286 port->init_devid->len < msg->port.init_devid_len) { 1287 free(port->init_devid, M_CTL); 1288 port->init_devid = malloc(sizeof(struct ctl_devid) + 1289 msg->port.init_devid_len, M_CTL, M_WAITOK); 1290 } 1291 memcpy(port->init_devid->data, &msg->port.data[i], 1292 msg->port.init_devid_len); 1293 port->init_devid->len = msg->port.init_devid_len; 1294 i += msg->port.init_devid_len; 1295 } else { 1296 free(port->init_devid, M_CTL); 1297 port->init_devid = NULL; 1298 } 1299 if (new) { 1300 if (ctl_port_register(port) != 0) { 1301 printf("%s: ctl_port_register() failed with error\n", 1302 __func__); 1303 } 1304 } 1305 mtx_lock(&softc->ctl_lock); 1306 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1307 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1308 continue; 1309 mtx_lock(&lun->lun_lock); 1310 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1311 mtx_unlock(&lun->lun_lock); 1312 } 1313 mtx_unlock(&softc->ctl_lock); 1314 } 1315 1316 static void 1317 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1318 { 1319 struct ctl_port *port; 1320 int iid; 1321 1322 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1323 if (port == NULL) { 1324 printf("%s: Received IID for unknown port %d\n", 1325 __func__, msg->hdr.nexus.targ_port); 1326 return; 1327 } 1328 iid = msg->hdr.nexus.initid; 1329 if (port->wwpn_iid[iid].in_use != 0 && 1330 msg->iid.in_use == 0) 1331 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1332 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1333 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1334 free(port->wwpn_iid[iid].name, M_CTL); 1335 if (msg->iid.name_len) { 1336 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1337 msg->iid.name_len, M_CTL); 1338 } else 1339 port->wwpn_iid[iid].name = NULL; 1340 } 1341 1342 static void 1343 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1344 { 1345 1346 if (msg->login.version != CTL_HA_VERSION) { 1347 printf("CTL HA peers have different versions %d != %d\n", 1348 msg->login.version, CTL_HA_VERSION); 1349 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1350 return; 1351 } 1352 if (msg->login.ha_mode != softc->ha_mode) { 1353 printf("CTL HA peers have different ha_mode %d != %d\n", 1354 msg->login.ha_mode, softc->ha_mode); 1355 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1356 return; 1357 } 1358 if (msg->login.ha_id == softc->ha_id) { 1359 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1360 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1361 return; 1362 } 1363 if (msg->login.max_luns != ctl_max_luns || 1364 msg->login.max_ports != ctl_max_ports || 1365 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1366 printf("CTL HA peers have different limits\n"); 1367 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1368 return; 1369 } 1370 } 1371 1372 static void 1373 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1374 { 1375 struct ctl_lun *lun; 1376 u_int i; 1377 uint32_t initidx, targ_lun; 1378 1379 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1380 mtx_lock(&softc->ctl_lock); 1381 if (targ_lun >= ctl_max_luns || 1382 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1383 mtx_unlock(&softc->ctl_lock); 1384 return; 1385 } 1386 mtx_lock(&lun->lun_lock); 1387 mtx_unlock(&softc->ctl_lock); 1388 if (lun->flags & CTL_LUN_DISABLED) { 1389 mtx_unlock(&lun->lun_lock); 1390 return; 1391 } 1392 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1393 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1394 msg->mode.page_code && 1395 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1396 break; 1397 } 1398 if (i == CTL_NUM_MODE_PAGES) { 1399 mtx_unlock(&lun->lun_lock); 1400 return; 1401 } 1402 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1403 lun->mode_pages.index[i].page_len); 1404 initidx = ctl_get_initindex(&msg->hdr.nexus); 1405 if (initidx != -1) 1406 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1407 mtx_unlock(&lun->lun_lock); 1408 } 1409 1410 /* 1411 * ISC (Inter Shelf Communication) event handler. Events from the HA 1412 * subsystem come in here. 1413 */ 1414 static void 1415 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1416 { 1417 struct ctl_softc *softc = control_softc; 1418 union ctl_io *io; 1419 struct ctl_prio *presio; 1420 ctl_ha_status isc_status; 1421 1422 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1423 if (event == CTL_HA_EVT_MSG_RECV) { 1424 union ctl_ha_msg *msg, msgbuf; 1425 1426 if (param > sizeof(msgbuf)) 1427 msg = malloc(param, M_CTL, M_WAITOK); 1428 else 1429 msg = &msgbuf; 1430 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1431 M_WAITOK); 1432 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1433 printf("%s: Error receiving message: %d\n", 1434 __func__, isc_status); 1435 if (msg != &msgbuf) 1436 free(msg, M_CTL); 1437 return; 1438 } 1439 1440 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->hdr.msg_type)); 1441 switch (msg->hdr.msg_type) { 1442 case CTL_MSG_SERIALIZE: 1443 io = ctl_alloc_io(softc->othersc_pool); 1444 ctl_zero_io(io); 1445 // populate ctsio from msg 1446 io->io_hdr.io_type = CTL_IO_SCSI; 1447 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1448 io->io_hdr.remote_io = msg->hdr.original_sc; 1449 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1450 CTL_FLAG_IO_ACTIVE; 1451 /* 1452 * If we're in serialization-only mode, we don't 1453 * want to go through full done processing. Thus 1454 * the COPY flag. 1455 * 1456 * XXX KDM add another flag that is more specific. 1457 */ 1458 if (softc->ha_mode != CTL_HA_MODE_XFER) 1459 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1460 io->io_hdr.nexus = msg->hdr.nexus; 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 /* 1921 * In Copan's HA scheme, the "master" and "slave" roles are 1922 * figured out through the slot the controller is in. Although it 1923 * is an active/active system, someone has to be in charge. 1924 */ 1925 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1926 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1927 "HA head ID (0 - no HA)"); 1928 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1929 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1930 softc->is_single = 1; 1931 softc->port_cnt = ctl_max_ports; 1932 softc->port_min = 0; 1933 } else { 1934 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 1935 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1936 } 1937 softc->port_max = softc->port_min + softc->port_cnt; 1938 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1939 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1940 1941 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1942 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1943 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1944 1945 STAILQ_INIT(&softc->lun_list); 1946 STAILQ_INIT(&softc->fe_list); 1947 STAILQ_INIT(&softc->port_list); 1948 STAILQ_INIT(&softc->be_list); 1949 ctl_tpc_init(softc); 1950 1951 if (worker_threads <= 0) 1952 worker_threads = max(1, mp_ncpus / 4); 1953 if (worker_threads > CTL_MAX_THREADS) 1954 worker_threads = CTL_MAX_THREADS; 1955 1956 for (i = 0; i < worker_threads; i++) { 1957 struct ctl_thread *thr = &softc->threads[i]; 1958 1959 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1960 thr->ctl_softc = softc; 1961 STAILQ_INIT(&thr->incoming_queue); 1962 STAILQ_INIT(&thr->rtr_queue); 1963 STAILQ_INIT(&thr->done_queue); 1964 STAILQ_INIT(&thr->isc_queue); 1965 1966 error = kproc_kthread_add(ctl_work_thread, thr, 1967 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1968 if (error != 0) { 1969 printf("error creating CTL work thread!\n"); 1970 return (error); 1971 } 1972 } 1973 error = kproc_kthread_add(ctl_thresh_thread, softc, 1974 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 1975 if (error != 0) { 1976 printf("error creating CTL threshold thread!\n"); 1977 return (error); 1978 } 1979 1980 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1981 OID_AUTO, "ha_role", 1982 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 1983 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1984 1985 if (softc->is_single == 0) { 1986 if (ctl_frontend_register(&ha_frontend) != 0) 1987 softc->is_single = 1; 1988 } 1989 return (0); 1990 } 1991 1992 static int 1993 ctl_shutdown(void) 1994 { 1995 struct ctl_softc *softc = control_softc; 1996 int i; 1997 1998 if (softc->is_single == 0) 1999 ctl_frontend_deregister(&ha_frontend); 2000 2001 destroy_dev(softc->dev); 2002 2003 /* Shutdown CTL threads. */ 2004 softc->shutdown = 1; 2005 for (i = 0; i < worker_threads; i++) { 2006 struct ctl_thread *thr = &softc->threads[i]; 2007 while (thr->thread != NULL) { 2008 wakeup(thr); 2009 if (thr->thread != NULL) 2010 pause("CTL thr shutdown", 1); 2011 } 2012 mtx_destroy(&thr->queue_lock); 2013 } 2014 while (softc->thresh_thread != NULL) { 2015 wakeup(softc->thresh_thread); 2016 if (softc->thresh_thread != NULL) 2017 pause("CTL thr shutdown", 1); 2018 } 2019 2020 ctl_tpc_shutdown(softc); 2021 uma_zdestroy(softc->io_zone); 2022 mtx_destroy(&softc->ctl_lock); 2023 2024 free(softc->ctl_luns, M_DEVBUF); 2025 free(softc->ctl_lun_mask, M_DEVBUF); 2026 free(softc->ctl_port_mask, M_DEVBUF); 2027 free(softc->ctl_ports, M_DEVBUF); 2028 2029 sysctl_ctx_free(&softc->sysctl_ctx); 2030 2031 free(softc, M_DEVBUF); 2032 control_softc = NULL; 2033 return (0); 2034 } 2035 2036 static int 2037 ctl_module_event_handler(module_t mod, int what, void *arg) 2038 { 2039 2040 switch (what) { 2041 case MOD_LOAD: 2042 return (ctl_init()); 2043 case MOD_UNLOAD: 2044 return (ctl_shutdown()); 2045 default: 2046 return (EOPNOTSUPP); 2047 } 2048 } 2049 2050 /* 2051 * XXX KDM should we do some access checks here? Bump a reference count to 2052 * prevent a CTL module from being unloaded while someone has it open? 2053 */ 2054 static int 2055 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2056 { 2057 return (0); 2058 } 2059 2060 static int 2061 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2062 { 2063 return (0); 2064 } 2065 2066 /* 2067 * Remove an initiator by port number and initiator ID. 2068 * Returns 0 for success, -1 for failure. 2069 */ 2070 int 2071 ctl_remove_initiator(struct ctl_port *port, int iid) 2072 { 2073 struct ctl_softc *softc = port->ctl_softc; 2074 int last; 2075 2076 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2077 2078 if (iid > CTL_MAX_INIT_PER_PORT) { 2079 printf("%s: initiator ID %u > maximun %u!\n", 2080 __func__, iid, CTL_MAX_INIT_PER_PORT); 2081 return (-1); 2082 } 2083 2084 mtx_lock(&softc->ctl_lock); 2085 last = (--port->wwpn_iid[iid].in_use == 0); 2086 port->wwpn_iid[iid].last_use = time_uptime; 2087 mtx_unlock(&softc->ctl_lock); 2088 if (last) 2089 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2090 ctl_isc_announce_iid(port, iid); 2091 2092 return (0); 2093 } 2094 2095 /* 2096 * Add an initiator to the initiator map. 2097 * Returns iid for success, < 0 for failure. 2098 */ 2099 int 2100 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2101 { 2102 struct ctl_softc *softc = port->ctl_softc; 2103 time_t best_time; 2104 int i, best; 2105 2106 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2107 2108 if (iid >= CTL_MAX_INIT_PER_PORT) { 2109 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2110 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2111 free(name, M_CTL); 2112 return (-1); 2113 } 2114 2115 mtx_lock(&softc->ctl_lock); 2116 2117 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2118 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2119 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2120 iid = i; 2121 break; 2122 } 2123 if (name != NULL && port->wwpn_iid[i].name != NULL && 2124 strcmp(name, port->wwpn_iid[i].name) == 0) { 2125 iid = i; 2126 break; 2127 } 2128 } 2129 } 2130 2131 if (iid < 0) { 2132 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2133 if (port->wwpn_iid[i].in_use == 0 && 2134 port->wwpn_iid[i].wwpn == 0 && 2135 port->wwpn_iid[i].name == NULL) { 2136 iid = i; 2137 break; 2138 } 2139 } 2140 } 2141 2142 if (iid < 0) { 2143 best = -1; 2144 best_time = INT32_MAX; 2145 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2146 if (port->wwpn_iid[i].in_use == 0) { 2147 if (port->wwpn_iid[i].last_use < best_time) { 2148 best = i; 2149 best_time = port->wwpn_iid[i].last_use; 2150 } 2151 } 2152 } 2153 iid = best; 2154 } 2155 2156 if (iid < 0) { 2157 mtx_unlock(&softc->ctl_lock); 2158 free(name, M_CTL); 2159 return (-2); 2160 } 2161 2162 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2163 /* 2164 * This is not an error yet. 2165 */ 2166 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2167 #if 0 2168 printf("%s: port %d iid %u WWPN %#jx arrived" 2169 " again\n", __func__, port->targ_port, 2170 iid, (uintmax_t)wwpn); 2171 #endif 2172 goto take; 2173 } 2174 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2175 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2176 #if 0 2177 printf("%s: port %d iid %u name '%s' arrived" 2178 " again\n", __func__, port->targ_port, 2179 iid, name); 2180 #endif 2181 goto take; 2182 } 2183 2184 /* 2185 * This is an error, but what do we do about it? The 2186 * driver is telling us we have a new WWPN for this 2187 * initiator ID, so we pretty much need to use it. 2188 */ 2189 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2190 " but WWPN %#jx '%s' is still at that address\n", 2191 __func__, port->targ_port, iid, wwpn, name, 2192 (uintmax_t)port->wwpn_iid[iid].wwpn, 2193 port->wwpn_iid[iid].name); 2194 } 2195 take: 2196 free(port->wwpn_iid[iid].name, M_CTL); 2197 port->wwpn_iid[iid].name = name; 2198 port->wwpn_iid[iid].wwpn = wwpn; 2199 port->wwpn_iid[iid].in_use++; 2200 mtx_unlock(&softc->ctl_lock); 2201 ctl_isc_announce_iid(port, iid); 2202 2203 return (iid); 2204 } 2205 2206 static int 2207 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2208 { 2209 int len; 2210 2211 switch (port->port_type) { 2212 case CTL_PORT_FC: 2213 { 2214 struct scsi_transportid_fcp *id = 2215 (struct scsi_transportid_fcp *)buf; 2216 if (port->wwpn_iid[iid].wwpn == 0) 2217 return (0); 2218 memset(id, 0, sizeof(*id)); 2219 id->format_protocol = SCSI_PROTO_FC; 2220 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2221 return (sizeof(*id)); 2222 } 2223 case CTL_PORT_ISCSI: 2224 { 2225 struct scsi_transportid_iscsi_port *id = 2226 (struct scsi_transportid_iscsi_port *)buf; 2227 if (port->wwpn_iid[iid].name == NULL) 2228 return (0); 2229 memset(id, 0, 256); 2230 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2231 SCSI_PROTO_ISCSI; 2232 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2233 len = roundup2(min(len, 252), 4); 2234 scsi_ulto2b(len, id->additional_length); 2235 return (sizeof(*id) + len); 2236 } 2237 case CTL_PORT_SAS: 2238 { 2239 struct scsi_transportid_sas *id = 2240 (struct scsi_transportid_sas *)buf; 2241 if (port->wwpn_iid[iid].wwpn == 0) 2242 return (0); 2243 memset(id, 0, sizeof(*id)); 2244 id->format_protocol = SCSI_PROTO_SAS; 2245 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2246 return (sizeof(*id)); 2247 } 2248 default: 2249 { 2250 struct scsi_transportid_spi *id = 2251 (struct scsi_transportid_spi *)buf; 2252 memset(id, 0, sizeof(*id)); 2253 id->format_protocol = SCSI_PROTO_SPI; 2254 scsi_ulto2b(iid, id->scsi_addr); 2255 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2256 return (sizeof(*id)); 2257 } 2258 } 2259 } 2260 2261 /* 2262 * Serialize a command that went down the "wrong" side, and so was sent to 2263 * this controller for execution. The logic is a little different than the 2264 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2265 * sent back to the other side, but in the success case, we execute the 2266 * command on this side (XFER mode) or tell the other side to execute it 2267 * (SER_ONLY mode). 2268 */ 2269 static void 2270 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2271 { 2272 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2273 struct ctl_port *port = CTL_PORT(ctsio); 2274 union ctl_ha_msg msg_info; 2275 struct ctl_lun *lun; 2276 const struct ctl_cmd_entry *entry; 2277 union ctl_io *bio; 2278 uint32_t targ_lun; 2279 2280 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2281 2282 /* Make sure that we know about this port. */ 2283 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2284 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2285 /*retry_count*/ 1); 2286 goto badjuju; 2287 } 2288 2289 /* Make sure that we know about this LUN. */ 2290 mtx_lock(&softc->ctl_lock); 2291 if (targ_lun >= ctl_max_luns || 2292 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2293 mtx_unlock(&softc->ctl_lock); 2294 2295 /* 2296 * The other node would not send this request to us unless 2297 * received announce that we are primary node for this LUN. 2298 * If this LUN does not exist now, it is probably result of 2299 * a race, so respond to initiator in the most opaque way. 2300 */ 2301 ctl_set_busy(ctsio); 2302 goto badjuju; 2303 } 2304 mtx_lock(&lun->lun_lock); 2305 mtx_unlock(&softc->ctl_lock); 2306 2307 /* 2308 * If the LUN is invalid, pretend that it doesn't exist. 2309 * It will go away as soon as all pending I/Os completed. 2310 */ 2311 if (lun->flags & CTL_LUN_DISABLED) { 2312 mtx_unlock(&lun->lun_lock); 2313 ctl_set_busy(ctsio); 2314 goto badjuju; 2315 } 2316 2317 entry = ctl_get_cmd_entry(ctsio, NULL); 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 (TAILQ_EMPTY(&lun->ooa_queue)) 2332 lun->idle_time += getsbinuptime() - lun->last_busy; 2333 #endif 2334 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2335 2336 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links); 2337 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 2338 case CTL_ACTION_BLOCK: 2339 ctsio->io_hdr.blocker = bio; 2340 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 2341 blocked_links); 2342 mtx_unlock(&lun->lun_lock); 2343 break; 2344 case CTL_ACTION_PASS: 2345 case CTL_ACTION_SKIP: 2346 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2347 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2348 ctl_enqueue_rtr((union ctl_io *)ctsio); 2349 mtx_unlock(&lun->lun_lock); 2350 } else { 2351 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2352 mtx_unlock(&lun->lun_lock); 2353 2354 /* send msg back to other side */ 2355 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2356 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2357 msg_info.hdr.msg_type = CTL_MSG_R2R; 2358 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2359 sizeof(msg_info.hdr), M_WAITOK); 2360 } 2361 break; 2362 case CTL_ACTION_OVERLAP: 2363 TAILQ_REMOVE(&lun->ooa_queue, &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 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2369 mtx_unlock(&lun->lun_lock); 2370 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2371 goto badjuju; 2372 case CTL_ACTION_ERROR: 2373 default: 2374 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2375 mtx_unlock(&lun->lun_lock); 2376 2377 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2378 /*retry_count*/ 0); 2379 badjuju: 2380 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2381 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2382 msg_info.hdr.serializing_sc = NULL; 2383 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2384 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2385 sizeof(msg_info.scsi), M_WAITOK); 2386 ctl_free_io((union ctl_io *)ctsio); 2387 break; 2388 } 2389 } 2390 2391 /* 2392 * Returns 0 for success, errno for failure. 2393 */ 2394 static void 2395 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2396 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2397 { 2398 union ctl_io *io; 2399 2400 mtx_lock(&lun->lun_lock); 2401 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2402 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2403 ooa_links)) { 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 continue; 2412 2413 entry = &kern_entries[*cur_fill_num]; 2414 2415 entry->tag_num = io->scsiio.tag_num; 2416 entry->lun_num = lun->lun; 2417 #ifdef CTL_TIME_IO 2418 entry->start_bt = io->io_hdr.start_bt; 2419 #endif 2420 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2421 entry->cdb_len = io->scsiio.cdb_len; 2422 if (io->io_hdr.blocker != NULL) 2423 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2424 2425 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2426 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2427 2428 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2429 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2430 2431 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2432 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2433 2434 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2435 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2436 } 2437 mtx_unlock(&lun->lun_lock); 2438 } 2439 2440 /* 2441 * Escape characters that are illegal or not recommended in XML. 2442 */ 2443 int 2444 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2445 { 2446 char *end = str + size; 2447 int retval; 2448 2449 retval = 0; 2450 2451 for (; *str && str < end; str++) { 2452 switch (*str) { 2453 case '&': 2454 retval = sbuf_printf(sb, "&"); 2455 break; 2456 case '>': 2457 retval = sbuf_printf(sb, ">"); 2458 break; 2459 case '<': 2460 retval = sbuf_printf(sb, "<"); 2461 break; 2462 default: 2463 retval = sbuf_putc(sb, *str); 2464 break; 2465 } 2466 2467 if (retval != 0) 2468 break; 2469 2470 } 2471 2472 return (retval); 2473 } 2474 2475 static void 2476 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2477 { 2478 struct scsi_vpd_id_descriptor *desc; 2479 int i; 2480 2481 if (id == NULL || id->len < 4) 2482 return; 2483 desc = (struct scsi_vpd_id_descriptor *)id->data; 2484 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2485 case SVPD_ID_TYPE_T10: 2486 sbuf_printf(sb, "t10."); 2487 break; 2488 case SVPD_ID_TYPE_EUI64: 2489 sbuf_printf(sb, "eui."); 2490 break; 2491 case SVPD_ID_TYPE_NAA: 2492 sbuf_printf(sb, "naa."); 2493 break; 2494 case SVPD_ID_TYPE_SCSI_NAME: 2495 break; 2496 } 2497 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2498 case SVPD_ID_CODESET_BINARY: 2499 for (i = 0; i < desc->length; i++) 2500 sbuf_printf(sb, "%02x", desc->identifier[i]); 2501 break; 2502 case SVPD_ID_CODESET_ASCII: 2503 sbuf_printf(sb, "%.*s", (int)desc->length, 2504 (char *)desc->identifier); 2505 break; 2506 case SVPD_ID_CODESET_UTF8: 2507 sbuf_printf(sb, "%s", (char *)desc->identifier); 2508 break; 2509 } 2510 } 2511 2512 static int 2513 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2514 struct thread *td) 2515 { 2516 struct ctl_softc *softc = dev->si_drv1; 2517 struct ctl_port *port; 2518 struct ctl_lun *lun; 2519 int retval; 2520 2521 retval = 0; 2522 2523 switch (cmd) { 2524 case CTL_IO: 2525 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2526 break; 2527 case CTL_ENABLE_PORT: 2528 case CTL_DISABLE_PORT: 2529 case CTL_SET_PORT_WWNS: { 2530 struct ctl_port *port; 2531 struct ctl_port_entry *entry; 2532 2533 entry = (struct ctl_port_entry *)addr; 2534 2535 mtx_lock(&softc->ctl_lock); 2536 STAILQ_FOREACH(port, &softc->port_list, links) { 2537 int action, done; 2538 2539 if (port->targ_port < softc->port_min || 2540 port->targ_port >= softc->port_max) 2541 continue; 2542 2543 action = 0; 2544 done = 0; 2545 if ((entry->port_type == CTL_PORT_NONE) 2546 && (entry->targ_port == port->targ_port)) { 2547 /* 2548 * If the user only wants to enable or 2549 * disable or set WWNs on a specific port, 2550 * do the operation and we're done. 2551 */ 2552 action = 1; 2553 done = 1; 2554 } else if (entry->port_type & port->port_type) { 2555 /* 2556 * Compare the user's type mask with the 2557 * particular frontend type to see if we 2558 * have a match. 2559 */ 2560 action = 1; 2561 done = 0; 2562 2563 /* 2564 * Make sure the user isn't trying to set 2565 * WWNs on multiple ports at the same time. 2566 */ 2567 if (cmd == CTL_SET_PORT_WWNS) { 2568 printf("%s: Can't set WWNs on " 2569 "multiple ports\n", __func__); 2570 retval = EINVAL; 2571 break; 2572 } 2573 } 2574 if (action == 0) 2575 continue; 2576 2577 /* 2578 * XXX KDM we have to drop the lock here, because 2579 * the online/offline operations can potentially 2580 * block. We need to reference count the frontends 2581 * so they can't go away, 2582 */ 2583 if (cmd == CTL_ENABLE_PORT) { 2584 mtx_unlock(&softc->ctl_lock); 2585 ctl_port_online(port); 2586 mtx_lock(&softc->ctl_lock); 2587 } else if (cmd == CTL_DISABLE_PORT) { 2588 mtx_unlock(&softc->ctl_lock); 2589 ctl_port_offline(port); 2590 mtx_lock(&softc->ctl_lock); 2591 } else if (cmd == CTL_SET_PORT_WWNS) { 2592 ctl_port_set_wwns(port, 2593 (entry->flags & CTL_PORT_WWNN_VALID) ? 2594 1 : 0, entry->wwnn, 2595 (entry->flags & CTL_PORT_WWPN_VALID) ? 2596 1 : 0, entry->wwpn); 2597 } 2598 if (done != 0) 2599 break; 2600 } 2601 mtx_unlock(&softc->ctl_lock); 2602 break; 2603 } 2604 case CTL_GET_OOA: { 2605 struct ctl_ooa *ooa_hdr; 2606 struct ctl_ooa_entry *entries; 2607 uint32_t cur_fill_num; 2608 2609 ooa_hdr = (struct ctl_ooa *)addr; 2610 2611 if ((ooa_hdr->alloc_len == 0) 2612 || (ooa_hdr->alloc_num == 0)) { 2613 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2614 "must be non-zero\n", __func__, 2615 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2616 retval = EINVAL; 2617 break; 2618 } 2619 2620 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2621 sizeof(struct ctl_ooa_entry))) { 2622 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2623 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2624 __func__, ooa_hdr->alloc_len, 2625 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2626 retval = EINVAL; 2627 break; 2628 } 2629 2630 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2631 if (entries == NULL) { 2632 printf("%s: could not allocate %d bytes for OOA " 2633 "dump\n", __func__, ooa_hdr->alloc_len); 2634 retval = ENOMEM; 2635 break; 2636 } 2637 2638 mtx_lock(&softc->ctl_lock); 2639 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2640 (ooa_hdr->lun_num >= ctl_max_luns || 2641 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2642 mtx_unlock(&softc->ctl_lock); 2643 free(entries, M_CTL); 2644 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2645 __func__, (uintmax_t)ooa_hdr->lun_num); 2646 retval = EINVAL; 2647 break; 2648 } 2649 2650 cur_fill_num = 0; 2651 2652 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2653 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2654 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2655 ooa_hdr, entries); 2656 } 2657 } else { 2658 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2659 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2660 entries); 2661 } 2662 mtx_unlock(&softc->ctl_lock); 2663 2664 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2665 ooa_hdr->fill_len = ooa_hdr->fill_num * 2666 sizeof(struct ctl_ooa_entry); 2667 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2668 if (retval != 0) { 2669 printf("%s: error copying out %d bytes for OOA dump\n", 2670 __func__, ooa_hdr->fill_len); 2671 } 2672 2673 getbinuptime(&ooa_hdr->cur_bt); 2674 2675 if (cur_fill_num > ooa_hdr->alloc_num) { 2676 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2677 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2678 } else { 2679 ooa_hdr->dropped_num = 0; 2680 ooa_hdr->status = CTL_OOA_OK; 2681 } 2682 2683 free(entries, M_CTL); 2684 break; 2685 } 2686 case CTL_DELAY_IO: { 2687 struct ctl_io_delay_info *delay_info; 2688 2689 delay_info = (struct ctl_io_delay_info *)addr; 2690 2691 #ifdef CTL_IO_DELAY 2692 mtx_lock(&softc->ctl_lock); 2693 if (delay_info->lun_id >= ctl_max_luns || 2694 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2695 mtx_unlock(&softc->ctl_lock); 2696 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2697 break; 2698 } 2699 mtx_lock(&lun->lun_lock); 2700 mtx_unlock(&softc->ctl_lock); 2701 delay_info->status = CTL_DELAY_STATUS_OK; 2702 switch (delay_info->delay_type) { 2703 case CTL_DELAY_TYPE_CONT: 2704 case CTL_DELAY_TYPE_ONESHOT: 2705 break; 2706 default: 2707 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2708 break; 2709 } 2710 switch (delay_info->delay_loc) { 2711 case CTL_DELAY_LOC_DATAMOVE: 2712 lun->delay_info.datamove_type = delay_info->delay_type; 2713 lun->delay_info.datamove_delay = delay_info->delay_secs; 2714 break; 2715 case CTL_DELAY_LOC_DONE: 2716 lun->delay_info.done_type = delay_info->delay_type; 2717 lun->delay_info.done_delay = delay_info->delay_secs; 2718 break; 2719 default: 2720 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2721 break; 2722 } 2723 mtx_unlock(&lun->lun_lock); 2724 #else 2725 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2726 #endif /* CTL_IO_DELAY */ 2727 break; 2728 } 2729 case CTL_ERROR_INJECT: { 2730 struct ctl_error_desc *err_desc, *new_err_desc; 2731 2732 err_desc = (struct ctl_error_desc *)addr; 2733 2734 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2735 M_WAITOK | M_ZERO); 2736 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2737 2738 mtx_lock(&softc->ctl_lock); 2739 if (err_desc->lun_id >= ctl_max_luns || 2740 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2741 mtx_unlock(&softc->ctl_lock); 2742 free(new_err_desc, M_CTL); 2743 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2744 __func__, (uintmax_t)err_desc->lun_id); 2745 retval = EINVAL; 2746 break; 2747 } 2748 mtx_lock(&lun->lun_lock); 2749 mtx_unlock(&softc->ctl_lock); 2750 2751 /* 2752 * We could do some checking here to verify the validity 2753 * of the request, but given the complexity of error 2754 * injection requests, the checking logic would be fairly 2755 * complex. 2756 * 2757 * For now, if the request is invalid, it just won't get 2758 * executed and might get deleted. 2759 */ 2760 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2761 2762 /* 2763 * XXX KDM check to make sure the serial number is unique, 2764 * in case we somehow manage to wrap. That shouldn't 2765 * happen for a very long time, but it's the right thing to 2766 * do. 2767 */ 2768 new_err_desc->serial = lun->error_serial; 2769 err_desc->serial = lun->error_serial; 2770 lun->error_serial++; 2771 2772 mtx_unlock(&lun->lun_lock); 2773 break; 2774 } 2775 case CTL_ERROR_INJECT_DELETE: { 2776 struct ctl_error_desc *delete_desc, *desc, *desc2; 2777 int delete_done; 2778 2779 delete_desc = (struct ctl_error_desc *)addr; 2780 delete_done = 0; 2781 2782 mtx_lock(&softc->ctl_lock); 2783 if (delete_desc->lun_id >= ctl_max_luns || 2784 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2785 mtx_unlock(&softc->ctl_lock); 2786 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2787 __func__, (uintmax_t)delete_desc->lun_id); 2788 retval = EINVAL; 2789 break; 2790 } 2791 mtx_lock(&lun->lun_lock); 2792 mtx_unlock(&softc->ctl_lock); 2793 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2794 if (desc->serial != delete_desc->serial) 2795 continue; 2796 2797 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2798 links); 2799 free(desc, M_CTL); 2800 delete_done = 1; 2801 } 2802 mtx_unlock(&lun->lun_lock); 2803 if (delete_done == 0) { 2804 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2805 "error serial %ju on LUN %u\n", __func__, 2806 delete_desc->serial, delete_desc->lun_id); 2807 retval = EINVAL; 2808 break; 2809 } 2810 break; 2811 } 2812 case CTL_DUMP_STRUCTS: { 2813 int j, k; 2814 struct ctl_port *port; 2815 struct ctl_frontend *fe; 2816 2817 mtx_lock(&softc->ctl_lock); 2818 printf("CTL Persistent Reservation information start:\n"); 2819 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2820 mtx_lock(&lun->lun_lock); 2821 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2822 mtx_unlock(&lun->lun_lock); 2823 continue; 2824 } 2825 2826 for (j = 0; j < ctl_max_ports; j++) { 2827 if (lun->pr_keys[j] == NULL) 2828 continue; 2829 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2830 if (lun->pr_keys[j][k] == 0) 2831 continue; 2832 printf(" LUN %ju port %d iid %d key " 2833 "%#jx\n", lun->lun, j, k, 2834 (uintmax_t)lun->pr_keys[j][k]); 2835 } 2836 } 2837 mtx_unlock(&lun->lun_lock); 2838 } 2839 printf("CTL Persistent Reservation information end\n"); 2840 printf("CTL Ports:\n"); 2841 STAILQ_FOREACH(port, &softc->port_list, links) { 2842 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2843 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2844 port->frontend->name, port->port_type, 2845 port->physical_port, port->virtual_port, 2846 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2847 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2848 if (port->wwpn_iid[j].in_use == 0 && 2849 port->wwpn_iid[j].wwpn == 0 && 2850 port->wwpn_iid[j].name == NULL) 2851 continue; 2852 2853 printf(" iid %u use %d WWPN %#jx '%s'\n", 2854 j, port->wwpn_iid[j].in_use, 2855 (uintmax_t)port->wwpn_iid[j].wwpn, 2856 port->wwpn_iid[j].name); 2857 } 2858 } 2859 printf("CTL Port information end\n"); 2860 mtx_unlock(&softc->ctl_lock); 2861 /* 2862 * XXX KDM calling this without a lock. We'd likely want 2863 * to drop the lock before calling the frontend's dump 2864 * routine anyway. 2865 */ 2866 printf("CTL Frontends:\n"); 2867 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2868 printf(" Frontend '%s'\n", fe->name); 2869 if (fe->fe_dump != NULL) 2870 fe->fe_dump(); 2871 } 2872 printf("CTL Frontend information end\n"); 2873 break; 2874 } 2875 case CTL_LUN_REQ: { 2876 struct ctl_lun_req *lun_req; 2877 struct ctl_backend_driver *backend; 2878 void *packed; 2879 nvlist_t *tmp_args_nvl; 2880 size_t packed_len; 2881 2882 lun_req = (struct ctl_lun_req *)addr; 2883 tmp_args_nvl = lun_req->args_nvl; 2884 2885 backend = ctl_backend_find(lun_req->backend); 2886 if (backend == NULL) { 2887 lun_req->status = CTL_LUN_ERROR; 2888 snprintf(lun_req->error_str, 2889 sizeof(lun_req->error_str), 2890 "Backend \"%s\" not found.", 2891 lun_req->backend); 2892 break; 2893 } 2894 2895 if (lun_req->args != NULL) { 2896 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2897 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2898 free(packed, M_CTL); 2899 lun_req->status = CTL_LUN_ERROR; 2900 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2901 "Cannot copyin args."); 2902 break; 2903 } 2904 lun_req->args_nvl = nvlist_unpack(packed, 2905 lun_req->args_len, 0); 2906 free(packed, M_CTL); 2907 2908 if (lun_req->args_nvl == NULL) { 2909 lun_req->status = CTL_LUN_ERROR; 2910 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2911 "Cannot unpack args nvlist."); 2912 break; 2913 } 2914 } else 2915 lun_req->args_nvl = nvlist_create(0); 2916 2917 retval = backend->ioctl(dev, cmd, addr, flag, td); 2918 nvlist_destroy(lun_req->args_nvl); 2919 lun_req->args_nvl = tmp_args_nvl; 2920 2921 if (lun_req->result_nvl != NULL) { 2922 if (lun_req->result != NULL) { 2923 packed = nvlist_pack(lun_req->result_nvl, 2924 &packed_len); 2925 if (packed == NULL) { 2926 lun_req->status = CTL_LUN_ERROR; 2927 snprintf(lun_req->error_str, 2928 sizeof(lun_req->error_str), 2929 "Cannot pack result nvlist."); 2930 break; 2931 } 2932 2933 if (packed_len > lun_req->result_len) { 2934 lun_req->status = CTL_LUN_ERROR; 2935 snprintf(lun_req->error_str, 2936 sizeof(lun_req->error_str), 2937 "Result nvlist too large."); 2938 free(packed, M_NVLIST); 2939 break; 2940 } 2941 2942 if (copyout(packed, lun_req->result, packed_len)) { 2943 lun_req->status = CTL_LUN_ERROR; 2944 snprintf(lun_req->error_str, 2945 sizeof(lun_req->error_str), 2946 "Cannot copyout() the result."); 2947 free(packed, M_NVLIST); 2948 break; 2949 } 2950 2951 lun_req->result_len = packed_len; 2952 free(packed, M_NVLIST); 2953 } 2954 2955 nvlist_destroy(lun_req->result_nvl); 2956 } 2957 break; 2958 } 2959 case CTL_LUN_LIST: { 2960 struct sbuf *sb; 2961 struct ctl_lun_list *list; 2962 const char *name, *value; 2963 void *cookie; 2964 int type; 2965 2966 list = (struct ctl_lun_list *)addr; 2967 2968 /* 2969 * Allocate a fixed length sbuf here, based on the length 2970 * of the user's buffer. We could allocate an auto-extending 2971 * buffer, and then tell the user how much larger our 2972 * amount of data is than his buffer, but that presents 2973 * some problems: 2974 * 2975 * 1. The sbuf(9) routines use a blocking malloc, and so 2976 * we can't hold a lock while calling them with an 2977 * auto-extending buffer. 2978 * 2979 * 2. There is not currently a LUN reference counting 2980 * mechanism, outside of outstanding transactions on 2981 * the LUN's OOA queue. So a LUN could go away on us 2982 * while we're getting the LUN number, backend-specific 2983 * information, etc. Thus, given the way things 2984 * currently work, we need to hold the CTL lock while 2985 * grabbing LUN information. 2986 * 2987 * So, from the user's standpoint, the best thing to do is 2988 * allocate what he thinks is a reasonable buffer length, 2989 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2990 * double the buffer length and try again. (And repeat 2991 * that until he succeeds.) 2992 */ 2993 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 2994 if (sb == NULL) { 2995 list->status = CTL_LUN_LIST_ERROR; 2996 snprintf(list->error_str, sizeof(list->error_str), 2997 "Unable to allocate %d bytes for LUN list", 2998 list->alloc_len); 2999 break; 3000 } 3001 3002 sbuf_printf(sb, "<ctllunlist>\n"); 3003 3004 mtx_lock(&softc->ctl_lock); 3005 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3006 mtx_lock(&lun->lun_lock); 3007 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3008 (uintmax_t)lun->lun); 3009 3010 /* 3011 * Bail out as soon as we see that we've overfilled 3012 * the buffer. 3013 */ 3014 if (retval != 0) 3015 break; 3016 3017 retval = sbuf_printf(sb, "\t<backend_type>%s" 3018 "</backend_type>\n", 3019 (lun->backend == NULL) ? "none" : 3020 lun->backend->name); 3021 3022 if (retval != 0) 3023 break; 3024 3025 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3026 lun->be_lun->lun_type); 3027 3028 if (retval != 0) 3029 break; 3030 3031 if (lun->backend == NULL) { 3032 retval = sbuf_printf(sb, "</lun>\n"); 3033 if (retval != 0) 3034 break; 3035 continue; 3036 } 3037 3038 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3039 (lun->be_lun->maxlba > 0) ? 3040 lun->be_lun->maxlba + 1 : 0); 3041 3042 if (retval != 0) 3043 break; 3044 3045 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3046 lun->be_lun->blocksize); 3047 3048 if (retval != 0) 3049 break; 3050 3051 retval = sbuf_printf(sb, "\t<serial_number>"); 3052 3053 if (retval != 0) 3054 break; 3055 3056 retval = ctl_sbuf_printf_esc(sb, 3057 lun->be_lun->serial_num, 3058 sizeof(lun->be_lun->serial_num)); 3059 3060 if (retval != 0) 3061 break; 3062 3063 retval = sbuf_printf(sb, "</serial_number>\n"); 3064 3065 if (retval != 0) 3066 break; 3067 3068 retval = sbuf_printf(sb, "\t<device_id>"); 3069 3070 if (retval != 0) 3071 break; 3072 3073 retval = ctl_sbuf_printf_esc(sb, 3074 lun->be_lun->device_id, 3075 sizeof(lun->be_lun->device_id)); 3076 3077 if (retval != 0) 3078 break; 3079 3080 retval = sbuf_printf(sb, "</device_id>\n"); 3081 3082 if (retval != 0) 3083 break; 3084 3085 if (lun->backend->lun_info != NULL) { 3086 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3087 if (retval != 0) 3088 break; 3089 } 3090 3091 cookie = NULL; 3092 while ((name = nvlist_next(lun->be_lun->options, &type, 3093 &cookie)) != NULL) { 3094 sbuf_printf(sb, "\t<%s>", name); 3095 3096 if (type == NV_TYPE_STRING) { 3097 value = dnvlist_get_string( 3098 lun->be_lun->options, name, NULL); 3099 if (value != NULL) 3100 sbuf_printf(sb, "%s", value); 3101 } 3102 3103 sbuf_printf(sb, "</%s>\n", name); 3104 } 3105 3106 retval = sbuf_printf(sb, "</lun>\n"); 3107 3108 if (retval != 0) 3109 break; 3110 mtx_unlock(&lun->lun_lock); 3111 } 3112 if (lun != NULL) 3113 mtx_unlock(&lun->lun_lock); 3114 mtx_unlock(&softc->ctl_lock); 3115 3116 if ((retval != 0) 3117 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3118 retval = 0; 3119 sbuf_delete(sb); 3120 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3121 snprintf(list->error_str, sizeof(list->error_str), 3122 "Out of space, %d bytes is too small", 3123 list->alloc_len); 3124 break; 3125 } 3126 3127 sbuf_finish(sb); 3128 3129 retval = copyout(sbuf_data(sb), list->lun_xml, 3130 sbuf_len(sb) + 1); 3131 3132 list->fill_len = sbuf_len(sb) + 1; 3133 list->status = CTL_LUN_LIST_OK; 3134 sbuf_delete(sb); 3135 break; 3136 } 3137 case CTL_ISCSI: { 3138 struct ctl_iscsi *ci; 3139 struct ctl_frontend *fe; 3140 3141 ci = (struct ctl_iscsi *)addr; 3142 3143 fe = ctl_frontend_find("iscsi"); 3144 if (fe == NULL) { 3145 ci->status = CTL_ISCSI_ERROR; 3146 snprintf(ci->error_str, sizeof(ci->error_str), 3147 "Frontend \"iscsi\" not found."); 3148 break; 3149 } 3150 3151 retval = fe->ioctl(dev, cmd, addr, flag, td); 3152 break; 3153 } 3154 case CTL_PORT_REQ: { 3155 struct ctl_req *req; 3156 struct ctl_frontend *fe; 3157 void *packed; 3158 nvlist_t *tmp_args_nvl; 3159 size_t packed_len; 3160 3161 req = (struct ctl_req *)addr; 3162 tmp_args_nvl = req->args_nvl; 3163 3164 fe = ctl_frontend_find(req->driver); 3165 if (fe == NULL) { 3166 req->status = CTL_LUN_ERROR; 3167 snprintf(req->error_str, sizeof(req->error_str), 3168 "Frontend \"%s\" not found.", req->driver); 3169 break; 3170 } 3171 3172 if (req->args != NULL) { 3173 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3174 if (copyin(req->args, packed, req->args_len) != 0) { 3175 free(packed, M_CTL); 3176 req->status = CTL_LUN_ERROR; 3177 snprintf(req->error_str, sizeof(req->error_str), 3178 "Cannot copyin args."); 3179 break; 3180 } 3181 req->args_nvl = nvlist_unpack(packed, 3182 req->args_len, 0); 3183 free(packed, M_CTL); 3184 3185 if (req->args_nvl == NULL) { 3186 req->status = CTL_LUN_ERROR; 3187 snprintf(req->error_str, sizeof(req->error_str), 3188 "Cannot unpack args nvlist."); 3189 break; 3190 } 3191 } else 3192 req->args_nvl = nvlist_create(0); 3193 3194 if (fe->ioctl) 3195 retval = fe->ioctl(dev, cmd, addr, flag, td); 3196 else 3197 retval = ENODEV; 3198 3199 nvlist_destroy(req->args_nvl); 3200 req->args_nvl = tmp_args_nvl; 3201 3202 if (req->result_nvl != NULL) { 3203 if (req->result != NULL) { 3204 packed = nvlist_pack(req->result_nvl, 3205 &packed_len); 3206 if (packed == NULL) { 3207 req->status = CTL_LUN_ERROR; 3208 snprintf(req->error_str, 3209 sizeof(req->error_str), 3210 "Cannot pack result nvlist."); 3211 break; 3212 } 3213 3214 if (packed_len > req->result_len) { 3215 req->status = CTL_LUN_ERROR; 3216 snprintf(req->error_str, 3217 sizeof(req->error_str), 3218 "Result nvlist too large."); 3219 free(packed, M_NVLIST); 3220 break; 3221 } 3222 3223 if (copyout(packed, req->result, packed_len)) { 3224 req->status = CTL_LUN_ERROR; 3225 snprintf(req->error_str, 3226 sizeof(req->error_str), 3227 "Cannot copyout() the result."); 3228 free(packed, M_NVLIST); 3229 break; 3230 } 3231 3232 req->result_len = packed_len; 3233 free(packed, M_NVLIST); 3234 } 3235 3236 nvlist_destroy(req->result_nvl); 3237 } 3238 break; 3239 } 3240 case CTL_PORT_LIST: { 3241 struct sbuf *sb; 3242 struct ctl_port *port; 3243 struct ctl_lun_list *list; 3244 const char *name, *value; 3245 void *cookie; 3246 int j, type; 3247 uint32_t plun; 3248 3249 list = (struct ctl_lun_list *)addr; 3250 3251 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3252 if (sb == NULL) { 3253 list->status = CTL_LUN_LIST_ERROR; 3254 snprintf(list->error_str, sizeof(list->error_str), 3255 "Unable to allocate %d bytes for LUN list", 3256 list->alloc_len); 3257 break; 3258 } 3259 3260 sbuf_printf(sb, "<ctlportlist>\n"); 3261 3262 mtx_lock(&softc->ctl_lock); 3263 STAILQ_FOREACH(port, &softc->port_list, links) { 3264 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3265 (uintmax_t)port->targ_port); 3266 3267 /* 3268 * Bail out as soon as we see that we've overfilled 3269 * the buffer. 3270 */ 3271 if (retval != 0) 3272 break; 3273 3274 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3275 "</frontend_type>\n", port->frontend->name); 3276 if (retval != 0) 3277 break; 3278 3279 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3280 port->port_type); 3281 if (retval != 0) 3282 break; 3283 3284 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3285 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3286 if (retval != 0) 3287 break; 3288 3289 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3290 port->port_name); 3291 if (retval != 0) 3292 break; 3293 3294 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3295 port->physical_port); 3296 if (retval != 0) 3297 break; 3298 3299 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3300 port->virtual_port); 3301 if (retval != 0) 3302 break; 3303 3304 if (port->target_devid != NULL) { 3305 sbuf_printf(sb, "\t<target>"); 3306 ctl_id_sbuf(port->target_devid, sb); 3307 sbuf_printf(sb, "</target>\n"); 3308 } 3309 3310 if (port->port_devid != NULL) { 3311 sbuf_printf(sb, "\t<port>"); 3312 ctl_id_sbuf(port->port_devid, sb); 3313 sbuf_printf(sb, "</port>\n"); 3314 } 3315 3316 if (port->port_info != NULL) { 3317 retval = port->port_info(port->onoff_arg, sb); 3318 if (retval != 0) 3319 break; 3320 } 3321 3322 cookie = NULL; 3323 while ((name = nvlist_next(port->options, &type, 3324 &cookie)) != NULL) { 3325 sbuf_printf(sb, "\t<%s>", name); 3326 3327 if (type == NV_TYPE_STRING) { 3328 value = dnvlist_get_string(port->options, 3329 name, NULL); 3330 if (value != NULL) 3331 sbuf_printf(sb, "%s", value); 3332 } 3333 3334 sbuf_printf(sb, "</%s>\n", name); 3335 } 3336 3337 if (port->lun_map != NULL) { 3338 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3339 for (j = 0; j < port->lun_map_size; j++) { 3340 plun = ctl_lun_map_from_port(port, j); 3341 if (plun == UINT32_MAX) 3342 continue; 3343 sbuf_printf(sb, 3344 "\t<lun id=\"%u\">%u</lun>\n", 3345 j, plun); 3346 } 3347 } 3348 3349 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3350 if (port->wwpn_iid[j].in_use == 0 || 3351 (port->wwpn_iid[j].wwpn == 0 && 3352 port->wwpn_iid[j].name == NULL)) 3353 continue; 3354 3355 if (port->wwpn_iid[j].name != NULL) 3356 retval = sbuf_printf(sb, 3357 "\t<initiator id=\"%u\">%s</initiator>\n", 3358 j, port->wwpn_iid[j].name); 3359 else 3360 retval = sbuf_printf(sb, 3361 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3362 j, port->wwpn_iid[j].wwpn); 3363 if (retval != 0) 3364 break; 3365 } 3366 if (retval != 0) 3367 break; 3368 3369 retval = sbuf_printf(sb, "</targ_port>\n"); 3370 if (retval != 0) 3371 break; 3372 } 3373 mtx_unlock(&softc->ctl_lock); 3374 3375 if ((retval != 0) 3376 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3377 retval = 0; 3378 sbuf_delete(sb); 3379 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3380 snprintf(list->error_str, sizeof(list->error_str), 3381 "Out of space, %d bytes is too small", 3382 list->alloc_len); 3383 break; 3384 } 3385 3386 sbuf_finish(sb); 3387 3388 retval = copyout(sbuf_data(sb), list->lun_xml, 3389 sbuf_len(sb) + 1); 3390 3391 list->fill_len = sbuf_len(sb) + 1; 3392 list->status = CTL_LUN_LIST_OK; 3393 sbuf_delete(sb); 3394 break; 3395 } 3396 case CTL_LUN_MAP: { 3397 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3398 struct ctl_port *port; 3399 3400 mtx_lock(&softc->ctl_lock); 3401 if (lm->port < softc->port_min || 3402 lm->port >= softc->port_max || 3403 (port = softc->ctl_ports[lm->port]) == NULL) { 3404 mtx_unlock(&softc->ctl_lock); 3405 return (ENXIO); 3406 } 3407 if (port->status & CTL_PORT_STATUS_ONLINE) { 3408 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3409 if (ctl_lun_map_to_port(port, lun->lun) == 3410 UINT32_MAX) 3411 continue; 3412 mtx_lock(&lun->lun_lock); 3413 ctl_est_ua_port(lun, lm->port, -1, 3414 CTL_UA_LUN_CHANGE); 3415 mtx_unlock(&lun->lun_lock); 3416 } 3417 } 3418 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3419 if (lm->plun != UINT32_MAX) { 3420 if (lm->lun == UINT32_MAX) 3421 retval = ctl_lun_map_unset(port, lm->plun); 3422 else if (lm->lun < ctl_max_luns && 3423 softc->ctl_luns[lm->lun] != NULL) 3424 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3425 else 3426 return (ENXIO); 3427 } else { 3428 if (lm->lun == UINT32_MAX) 3429 retval = ctl_lun_map_deinit(port); 3430 else 3431 retval = ctl_lun_map_init(port); 3432 } 3433 if (port->status & CTL_PORT_STATUS_ONLINE) 3434 ctl_isc_announce_port(port); 3435 break; 3436 } 3437 case CTL_GET_LUN_STATS: { 3438 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3439 int i; 3440 3441 /* 3442 * XXX KDM no locking here. If the LUN list changes, 3443 * things can blow up. 3444 */ 3445 i = 0; 3446 stats->status = CTL_SS_OK; 3447 stats->fill_len = 0; 3448 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3449 if (lun->lun < stats->first_item) 3450 continue; 3451 if (stats->fill_len + sizeof(lun->stats) > 3452 stats->alloc_len) { 3453 stats->status = CTL_SS_NEED_MORE_SPACE; 3454 break; 3455 } 3456 retval = copyout(&lun->stats, &stats->stats[i++], 3457 sizeof(lun->stats)); 3458 if (retval != 0) 3459 break; 3460 stats->fill_len += sizeof(lun->stats); 3461 } 3462 stats->num_items = softc->num_luns; 3463 stats->flags = CTL_STATS_FLAG_NONE; 3464 #ifdef CTL_TIME_IO 3465 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3466 #endif 3467 getnanouptime(&stats->timestamp); 3468 break; 3469 } 3470 case CTL_GET_PORT_STATS: { 3471 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3472 int i; 3473 3474 /* 3475 * XXX KDM no locking here. If the LUN list changes, 3476 * things can blow up. 3477 */ 3478 i = 0; 3479 stats->status = CTL_SS_OK; 3480 stats->fill_len = 0; 3481 STAILQ_FOREACH(port, &softc->port_list, links) { 3482 if (port->targ_port < stats->first_item) 3483 continue; 3484 if (stats->fill_len + sizeof(port->stats) > 3485 stats->alloc_len) { 3486 stats->status = CTL_SS_NEED_MORE_SPACE; 3487 break; 3488 } 3489 retval = copyout(&port->stats, &stats->stats[i++], 3490 sizeof(port->stats)); 3491 if (retval != 0) 3492 break; 3493 stats->fill_len += sizeof(port->stats); 3494 } 3495 stats->num_items = softc->num_ports; 3496 stats->flags = CTL_STATS_FLAG_NONE; 3497 #ifdef CTL_TIME_IO 3498 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3499 #endif 3500 getnanouptime(&stats->timestamp); 3501 break; 3502 } 3503 default: { 3504 /* XXX KDM should we fix this? */ 3505 #if 0 3506 struct ctl_backend_driver *backend; 3507 unsigned int type; 3508 int found; 3509 3510 found = 0; 3511 3512 /* 3513 * We encode the backend type as the ioctl type for backend 3514 * ioctls. So parse it out here, and then search for a 3515 * backend of this type. 3516 */ 3517 type = _IOC_TYPE(cmd); 3518 3519 STAILQ_FOREACH(backend, &softc->be_list, links) { 3520 if (backend->type == type) { 3521 found = 1; 3522 break; 3523 } 3524 } 3525 if (found == 0) { 3526 printf("ctl: unknown ioctl command %#lx or backend " 3527 "%d\n", cmd, type); 3528 retval = EINVAL; 3529 break; 3530 } 3531 retval = backend->ioctl(dev, cmd, addr, flag, td); 3532 #endif 3533 retval = ENOTTY; 3534 break; 3535 } 3536 } 3537 return (retval); 3538 } 3539 3540 uint32_t 3541 ctl_get_initindex(struct ctl_nexus *nexus) 3542 { 3543 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3544 } 3545 3546 int 3547 ctl_lun_map_init(struct ctl_port *port) 3548 { 3549 struct ctl_softc *softc = port->ctl_softc; 3550 struct ctl_lun *lun; 3551 int size = ctl_lun_map_size; 3552 uint32_t i; 3553 3554 if (port->lun_map == NULL || port->lun_map_size < size) { 3555 port->lun_map_size = 0; 3556 free(port->lun_map, M_CTL); 3557 port->lun_map = malloc(size * sizeof(uint32_t), 3558 M_CTL, M_NOWAIT); 3559 } 3560 if (port->lun_map == NULL) 3561 return (ENOMEM); 3562 for (i = 0; i < size; i++) 3563 port->lun_map[i] = UINT32_MAX; 3564 port->lun_map_size = size; 3565 if (port->status & CTL_PORT_STATUS_ONLINE) { 3566 if (port->lun_disable != NULL) { 3567 STAILQ_FOREACH(lun, &softc->lun_list, links) 3568 port->lun_disable(port->targ_lun_arg, lun->lun); 3569 } 3570 ctl_isc_announce_port(port); 3571 } 3572 return (0); 3573 } 3574 3575 int 3576 ctl_lun_map_deinit(struct ctl_port *port) 3577 { 3578 struct ctl_softc *softc = port->ctl_softc; 3579 struct ctl_lun *lun; 3580 3581 if (port->lun_map == NULL) 3582 return (0); 3583 port->lun_map_size = 0; 3584 free(port->lun_map, M_CTL); 3585 port->lun_map = NULL; 3586 if (port->status & CTL_PORT_STATUS_ONLINE) { 3587 if (port->lun_enable != NULL) { 3588 STAILQ_FOREACH(lun, &softc->lun_list, links) 3589 port->lun_enable(port->targ_lun_arg, lun->lun); 3590 } 3591 ctl_isc_announce_port(port); 3592 } 3593 return (0); 3594 } 3595 3596 int 3597 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3598 { 3599 int status; 3600 uint32_t old; 3601 3602 if (port->lun_map == NULL) { 3603 status = ctl_lun_map_init(port); 3604 if (status != 0) 3605 return (status); 3606 } 3607 if (plun >= port->lun_map_size) 3608 return (EINVAL); 3609 old = port->lun_map[plun]; 3610 port->lun_map[plun] = glun; 3611 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3612 if (port->lun_enable != NULL) 3613 port->lun_enable(port->targ_lun_arg, plun); 3614 ctl_isc_announce_port(port); 3615 } 3616 return (0); 3617 } 3618 3619 int 3620 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3621 { 3622 uint32_t old; 3623 3624 if (port->lun_map == NULL || plun >= port->lun_map_size) 3625 return (0); 3626 old = port->lun_map[plun]; 3627 port->lun_map[plun] = UINT32_MAX; 3628 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3629 if (port->lun_disable != NULL) 3630 port->lun_disable(port->targ_lun_arg, plun); 3631 ctl_isc_announce_port(port); 3632 } 3633 return (0); 3634 } 3635 3636 uint32_t 3637 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3638 { 3639 3640 if (port == NULL) 3641 return (UINT32_MAX); 3642 if (port->lun_map == NULL) 3643 return (lun_id); 3644 if (lun_id > port->lun_map_size) 3645 return (UINT32_MAX); 3646 return (port->lun_map[lun_id]); 3647 } 3648 3649 uint32_t 3650 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3651 { 3652 uint32_t i; 3653 3654 if (port == NULL) 3655 return (UINT32_MAX); 3656 if (port->lun_map == NULL) 3657 return (lun_id); 3658 for (i = 0; i < port->lun_map_size; i++) { 3659 if (port->lun_map[i] == lun_id) 3660 return (i); 3661 } 3662 return (UINT32_MAX); 3663 } 3664 3665 uint32_t 3666 ctl_decode_lun(uint64_t encoded) 3667 { 3668 uint8_t lun[8]; 3669 uint32_t result = 0xffffffff; 3670 3671 be64enc(lun, encoded); 3672 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3673 case RPL_LUNDATA_ATYP_PERIPH: 3674 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3675 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3676 result = lun[1]; 3677 break; 3678 case RPL_LUNDATA_ATYP_FLAT: 3679 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3680 lun[6] == 0 && lun[7] == 0) 3681 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3682 break; 3683 case RPL_LUNDATA_ATYP_EXTLUN: 3684 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3685 case 0x02: 3686 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3687 case 0x00: 3688 result = lun[1]; 3689 break; 3690 case 0x10: 3691 result = (lun[1] << 16) + (lun[2] << 8) + 3692 lun[3]; 3693 break; 3694 case 0x20: 3695 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3696 result = (lun[2] << 24) + 3697 (lun[3] << 16) + (lun[4] << 8) + 3698 lun[5]; 3699 break; 3700 } 3701 break; 3702 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3703 result = 0xffffffff; 3704 break; 3705 } 3706 break; 3707 } 3708 return (result); 3709 } 3710 3711 uint64_t 3712 ctl_encode_lun(uint32_t decoded) 3713 { 3714 uint64_t l = decoded; 3715 3716 if (l <= 0xff) 3717 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3718 if (l <= 0x3fff) 3719 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3720 if (l <= 0xffffff) 3721 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3722 (l << 32)); 3723 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3724 } 3725 3726 int 3727 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3728 { 3729 int i; 3730 3731 for (i = first; i < last; i++) { 3732 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3733 return (i); 3734 } 3735 return (-1); 3736 } 3737 3738 int 3739 ctl_set_mask(uint32_t *mask, uint32_t bit) 3740 { 3741 uint32_t chunk, piece; 3742 3743 chunk = bit >> 5; 3744 piece = bit % (sizeof(uint32_t) * 8); 3745 3746 if ((mask[chunk] & (1 << piece)) != 0) 3747 return (-1); 3748 else 3749 mask[chunk] |= (1 << piece); 3750 3751 return (0); 3752 } 3753 3754 int 3755 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3756 { 3757 uint32_t chunk, piece; 3758 3759 chunk = bit >> 5; 3760 piece = bit % (sizeof(uint32_t) * 8); 3761 3762 if ((mask[chunk] & (1 << piece)) == 0) 3763 return (-1); 3764 else 3765 mask[chunk] &= ~(1 << piece); 3766 3767 return (0); 3768 } 3769 3770 int 3771 ctl_is_set(uint32_t *mask, uint32_t bit) 3772 { 3773 uint32_t chunk, piece; 3774 3775 chunk = bit >> 5; 3776 piece = bit % (sizeof(uint32_t) * 8); 3777 3778 if ((mask[chunk] & (1 << piece)) == 0) 3779 return (0); 3780 else 3781 return (1); 3782 } 3783 3784 static uint64_t 3785 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3786 { 3787 uint64_t *t; 3788 3789 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3790 if (t == NULL) 3791 return (0); 3792 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3793 } 3794 3795 static void 3796 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3797 { 3798 uint64_t *t; 3799 3800 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3801 if (t == NULL) 3802 return; 3803 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3804 } 3805 3806 static void 3807 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3808 { 3809 uint64_t *p; 3810 u_int i; 3811 3812 i = residx/CTL_MAX_INIT_PER_PORT; 3813 if (lun->pr_keys[i] != NULL) 3814 return; 3815 mtx_unlock(&lun->lun_lock); 3816 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3817 M_WAITOK | M_ZERO); 3818 mtx_lock(&lun->lun_lock); 3819 if (lun->pr_keys[i] == NULL) 3820 lun->pr_keys[i] = p; 3821 else 3822 free(p, M_CTL); 3823 } 3824 3825 static void 3826 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3827 { 3828 uint64_t *t; 3829 3830 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3831 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3832 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3833 } 3834 3835 /* 3836 * ctl_softc, pool_name, total_ctl_io are passed in. 3837 * npool is passed out. 3838 */ 3839 int 3840 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3841 uint32_t total_ctl_io, void **npool) 3842 { 3843 struct ctl_io_pool *pool; 3844 3845 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3846 M_NOWAIT | M_ZERO); 3847 if (pool == NULL) 3848 return (ENOMEM); 3849 3850 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3851 pool->ctl_softc = ctl_softc; 3852 #ifdef IO_POOLS 3853 pool->zone = uma_zsecond_create(pool->name, NULL, 3854 NULL, NULL, NULL, ctl_softc->io_zone); 3855 /* uma_prealloc(pool->zone, total_ctl_io); */ 3856 #else 3857 pool->zone = ctl_softc->io_zone; 3858 #endif 3859 3860 *npool = pool; 3861 return (0); 3862 } 3863 3864 void 3865 ctl_pool_free(struct ctl_io_pool *pool) 3866 { 3867 3868 if (pool == NULL) 3869 return; 3870 3871 #ifdef IO_POOLS 3872 uma_zdestroy(pool->zone); 3873 #endif 3874 free(pool, M_CTL); 3875 } 3876 3877 union ctl_io * 3878 ctl_alloc_io(void *pool_ref) 3879 { 3880 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3881 union ctl_io *io; 3882 3883 io = uma_zalloc(pool->zone, M_WAITOK); 3884 if (io != NULL) { 3885 io->io_hdr.pool = pool_ref; 3886 CTL_SOFTC(io) = pool->ctl_softc; 3887 TAILQ_INIT(&io->io_hdr.blocked_queue); 3888 } 3889 return (io); 3890 } 3891 3892 union ctl_io * 3893 ctl_alloc_io_nowait(void *pool_ref) 3894 { 3895 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3896 union ctl_io *io; 3897 3898 io = uma_zalloc(pool->zone, M_NOWAIT); 3899 if (io != NULL) { 3900 io->io_hdr.pool = pool_ref; 3901 CTL_SOFTC(io) = pool->ctl_softc; 3902 TAILQ_INIT(&io->io_hdr.blocked_queue); 3903 } 3904 return (io); 3905 } 3906 3907 void 3908 ctl_free_io(union ctl_io *io) 3909 { 3910 struct ctl_io_pool *pool; 3911 3912 if (io == NULL) 3913 return; 3914 3915 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3916 uma_zfree(pool->zone, io); 3917 } 3918 3919 void 3920 ctl_zero_io(union ctl_io *io) 3921 { 3922 struct ctl_io_pool *pool; 3923 3924 if (io == NULL) 3925 return; 3926 3927 /* 3928 * May need to preserve linked list pointers at some point too. 3929 */ 3930 pool = io->io_hdr.pool; 3931 memset(io, 0, sizeof(*io)); 3932 io->io_hdr.pool = pool; 3933 CTL_SOFTC(io) = pool->ctl_softc; 3934 TAILQ_INIT(&io->io_hdr.blocked_queue); 3935 } 3936 3937 int 3938 ctl_expand_number(const char *buf, uint64_t *num) 3939 { 3940 char *endptr; 3941 uint64_t number; 3942 unsigned shift; 3943 3944 number = strtoq(buf, &endptr, 0); 3945 3946 switch (tolower((unsigned char)*endptr)) { 3947 case 'e': 3948 shift = 60; 3949 break; 3950 case 'p': 3951 shift = 50; 3952 break; 3953 case 't': 3954 shift = 40; 3955 break; 3956 case 'g': 3957 shift = 30; 3958 break; 3959 case 'm': 3960 shift = 20; 3961 break; 3962 case 'k': 3963 shift = 10; 3964 break; 3965 case 'b': 3966 case '\0': /* No unit. */ 3967 *num = number; 3968 return (0); 3969 default: 3970 /* Unrecognized unit. */ 3971 return (-1); 3972 } 3973 3974 if ((number << shift) >> shift != number) { 3975 /* Overflow */ 3976 return (-1); 3977 } 3978 *num = number << shift; 3979 return (0); 3980 } 3981 3982 3983 /* 3984 * This routine could be used in the future to load default and/or saved 3985 * mode page parameters for a particuar lun. 3986 */ 3987 static int 3988 ctl_init_page_index(struct ctl_lun *lun) 3989 { 3990 int i, page_code; 3991 struct ctl_page_index *page_index; 3992 const char *value; 3993 uint64_t ival; 3994 3995 memcpy(&lun->mode_pages.index, page_index_template, 3996 sizeof(page_index_template)); 3997 3998 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3999 4000 page_index = &lun->mode_pages.index[i]; 4001 if (lun->be_lun->lun_type == T_DIRECT && 4002 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4003 continue; 4004 if (lun->be_lun->lun_type == T_PROCESSOR && 4005 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4006 continue; 4007 if (lun->be_lun->lun_type == T_CDROM && 4008 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4009 continue; 4010 4011 page_code = page_index->page_code & SMPH_PC_MASK; 4012 switch (page_code) { 4013 case SMS_RW_ERROR_RECOVERY_PAGE: { 4014 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4015 ("subpage %#x for page %#x is incorrect!", 4016 page_index->subpage, page_code)); 4017 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4018 &rw_er_page_default, 4019 sizeof(rw_er_page_default)); 4020 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4021 &rw_er_page_changeable, 4022 sizeof(rw_er_page_changeable)); 4023 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4024 &rw_er_page_default, 4025 sizeof(rw_er_page_default)); 4026 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4027 &rw_er_page_default, 4028 sizeof(rw_er_page_default)); 4029 page_index->page_data = 4030 (uint8_t *)lun->mode_pages.rw_er_page; 4031 break; 4032 } 4033 case SMS_FORMAT_DEVICE_PAGE: { 4034 struct scsi_format_page *format_page; 4035 4036 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4037 ("subpage %#x for page %#x is incorrect!", 4038 page_index->subpage, page_code)); 4039 4040 /* 4041 * Sectors per track are set above. Bytes per 4042 * sector need to be set here on a per-LUN basis. 4043 */ 4044 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4045 &format_page_default, 4046 sizeof(format_page_default)); 4047 memcpy(&lun->mode_pages.format_page[ 4048 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4049 sizeof(format_page_changeable)); 4050 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4051 &format_page_default, 4052 sizeof(format_page_default)); 4053 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4054 &format_page_default, 4055 sizeof(format_page_default)); 4056 4057 format_page = &lun->mode_pages.format_page[ 4058 CTL_PAGE_CURRENT]; 4059 scsi_ulto2b(lun->be_lun->blocksize, 4060 format_page->bytes_per_sector); 4061 4062 format_page = &lun->mode_pages.format_page[ 4063 CTL_PAGE_DEFAULT]; 4064 scsi_ulto2b(lun->be_lun->blocksize, 4065 format_page->bytes_per_sector); 4066 4067 format_page = &lun->mode_pages.format_page[ 4068 CTL_PAGE_SAVED]; 4069 scsi_ulto2b(lun->be_lun->blocksize, 4070 format_page->bytes_per_sector); 4071 4072 page_index->page_data = 4073 (uint8_t *)lun->mode_pages.format_page; 4074 break; 4075 } 4076 case SMS_RIGID_DISK_PAGE: { 4077 struct scsi_rigid_disk_page *rigid_disk_page; 4078 uint32_t sectors_per_cylinder; 4079 uint64_t cylinders; 4080 #ifndef __XSCALE__ 4081 int shift; 4082 #endif /* !__XSCALE__ */ 4083 4084 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4085 ("subpage %#x for page %#x is incorrect!", 4086 page_index->subpage, page_code)); 4087 4088 /* 4089 * Rotation rate and sectors per track are set 4090 * above. We calculate the cylinders here based on 4091 * capacity. Due to the number of heads and 4092 * sectors per track we're using, smaller arrays 4093 * may turn out to have 0 cylinders. Linux and 4094 * FreeBSD don't pay attention to these mode pages 4095 * to figure out capacity, but Solaris does. It 4096 * seems to deal with 0 cylinders just fine, and 4097 * works out a fake geometry based on the capacity. 4098 */ 4099 memcpy(&lun->mode_pages.rigid_disk_page[ 4100 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4101 sizeof(rigid_disk_page_default)); 4102 memcpy(&lun->mode_pages.rigid_disk_page[ 4103 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4104 sizeof(rigid_disk_page_changeable)); 4105 4106 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4107 CTL_DEFAULT_HEADS; 4108 4109 /* 4110 * The divide method here will be more accurate, 4111 * probably, but results in floating point being 4112 * used in the kernel on i386 (__udivdi3()). On the 4113 * XScale, though, __udivdi3() is implemented in 4114 * software. 4115 * 4116 * The shift method for cylinder calculation is 4117 * accurate if sectors_per_cylinder is a power of 4118 * 2. Otherwise it might be slightly off -- you 4119 * might have a bit of a truncation problem. 4120 */ 4121 #ifdef __XSCALE__ 4122 cylinders = (lun->be_lun->maxlba + 1) / 4123 sectors_per_cylinder; 4124 #else 4125 for (shift = 31; shift > 0; shift--) { 4126 if (sectors_per_cylinder & (1 << shift)) 4127 break; 4128 } 4129 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4130 #endif 4131 4132 /* 4133 * We've basically got 3 bytes, or 24 bits for the 4134 * cylinder size in the mode page. If we're over, 4135 * just round down to 2^24. 4136 */ 4137 if (cylinders > 0xffffff) 4138 cylinders = 0xffffff; 4139 4140 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4141 CTL_PAGE_DEFAULT]; 4142 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4143 4144 if ((value = dnvlist_get_string(lun->be_lun->options, 4145 "rpm", NULL)) != NULL) { 4146 scsi_ulto2b(strtol(value, NULL, 0), 4147 rigid_disk_page->rotation_rate); 4148 } 4149 4150 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4151 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4152 sizeof(rigid_disk_page_default)); 4153 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4154 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4155 sizeof(rigid_disk_page_default)); 4156 4157 page_index->page_data = 4158 (uint8_t *)lun->mode_pages.rigid_disk_page; 4159 break; 4160 } 4161 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4162 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4163 ("subpage %#x for page %#x is incorrect!", 4164 page_index->subpage, page_code)); 4165 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4166 &verify_er_page_default, 4167 sizeof(verify_er_page_default)); 4168 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4169 &verify_er_page_changeable, 4170 sizeof(verify_er_page_changeable)); 4171 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4172 &verify_er_page_default, 4173 sizeof(verify_er_page_default)); 4174 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4175 &verify_er_page_default, 4176 sizeof(verify_er_page_default)); 4177 page_index->page_data = 4178 (uint8_t *)lun->mode_pages.verify_er_page; 4179 break; 4180 } 4181 case SMS_CACHING_PAGE: { 4182 struct scsi_caching_page *caching_page; 4183 4184 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4185 ("subpage %#x for page %#x is incorrect!", 4186 page_index->subpage, page_code)); 4187 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4188 &caching_page_default, 4189 sizeof(caching_page_default)); 4190 memcpy(&lun->mode_pages.caching_page[ 4191 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4192 sizeof(caching_page_changeable)); 4193 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4194 &caching_page_default, 4195 sizeof(caching_page_default)); 4196 caching_page = &lun->mode_pages.caching_page[ 4197 CTL_PAGE_SAVED]; 4198 value = dnvlist_get_string(lun->be_lun->options, 4199 "writecache", NULL); 4200 if (value != NULL && strcmp(value, "off") == 0) 4201 caching_page->flags1 &= ~SCP_WCE; 4202 value = dnvlist_get_string(lun->be_lun->options, 4203 "readcache", NULL); 4204 if (value != NULL && strcmp(value, "off") == 0) 4205 caching_page->flags1 |= SCP_RCD; 4206 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4207 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4208 sizeof(caching_page_default)); 4209 page_index->page_data = 4210 (uint8_t *)lun->mode_pages.caching_page; 4211 break; 4212 } 4213 case SMS_CONTROL_MODE_PAGE: { 4214 switch (page_index->subpage) { 4215 case SMS_SUBPAGE_PAGE_0: { 4216 struct scsi_control_page *control_page; 4217 4218 memcpy(&lun->mode_pages.control_page[ 4219 CTL_PAGE_DEFAULT], 4220 &control_page_default, 4221 sizeof(control_page_default)); 4222 memcpy(&lun->mode_pages.control_page[ 4223 CTL_PAGE_CHANGEABLE], 4224 &control_page_changeable, 4225 sizeof(control_page_changeable)); 4226 memcpy(&lun->mode_pages.control_page[ 4227 CTL_PAGE_SAVED], 4228 &control_page_default, 4229 sizeof(control_page_default)); 4230 control_page = &lun->mode_pages.control_page[ 4231 CTL_PAGE_SAVED]; 4232 value = dnvlist_get_string(lun->be_lun->options, 4233 "reordering", NULL); 4234 if (value != NULL && 4235 strcmp(value, "unrestricted") == 0) { 4236 control_page->queue_flags &= 4237 ~SCP_QUEUE_ALG_MASK; 4238 control_page->queue_flags |= 4239 SCP_QUEUE_ALG_UNRESTRICTED; 4240 } 4241 memcpy(&lun->mode_pages.control_page[ 4242 CTL_PAGE_CURRENT], 4243 &lun->mode_pages.control_page[ 4244 CTL_PAGE_SAVED], 4245 sizeof(control_page_default)); 4246 page_index->page_data = 4247 (uint8_t *)lun->mode_pages.control_page; 4248 break; 4249 } 4250 case 0x01: 4251 memcpy(&lun->mode_pages.control_ext_page[ 4252 CTL_PAGE_DEFAULT], 4253 &control_ext_page_default, 4254 sizeof(control_ext_page_default)); 4255 memcpy(&lun->mode_pages.control_ext_page[ 4256 CTL_PAGE_CHANGEABLE], 4257 &control_ext_page_changeable, 4258 sizeof(control_ext_page_changeable)); 4259 memcpy(&lun->mode_pages.control_ext_page[ 4260 CTL_PAGE_SAVED], 4261 &control_ext_page_default, 4262 sizeof(control_ext_page_default)); 4263 memcpy(&lun->mode_pages.control_ext_page[ 4264 CTL_PAGE_CURRENT], 4265 &lun->mode_pages.control_ext_page[ 4266 CTL_PAGE_SAVED], 4267 sizeof(control_ext_page_default)); 4268 page_index->page_data = 4269 (uint8_t *)lun->mode_pages.control_ext_page; 4270 break; 4271 default: 4272 panic("subpage %#x for page %#x is incorrect!", 4273 page_index->subpage, page_code); 4274 } 4275 break; 4276 } 4277 case SMS_INFO_EXCEPTIONS_PAGE: { 4278 switch (page_index->subpage) { 4279 case SMS_SUBPAGE_PAGE_0: 4280 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4281 &ie_page_default, 4282 sizeof(ie_page_default)); 4283 memcpy(&lun->mode_pages.ie_page[ 4284 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4285 sizeof(ie_page_changeable)); 4286 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4287 &ie_page_default, 4288 sizeof(ie_page_default)); 4289 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4290 &ie_page_default, 4291 sizeof(ie_page_default)); 4292 page_index->page_data = 4293 (uint8_t *)lun->mode_pages.ie_page; 4294 break; 4295 case 0x02: { 4296 struct ctl_logical_block_provisioning_page *page; 4297 4298 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4299 &lbp_page_default, 4300 sizeof(lbp_page_default)); 4301 memcpy(&lun->mode_pages.lbp_page[ 4302 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4303 sizeof(lbp_page_changeable)); 4304 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4305 &lbp_page_default, 4306 sizeof(lbp_page_default)); 4307 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4308 value = dnvlist_get_string(lun->be_lun->options, 4309 "avail-threshold", NULL); 4310 if (value != NULL && 4311 ctl_expand_number(value, &ival) == 0) { 4312 page->descr[0].flags |= SLBPPD_ENABLED | 4313 SLBPPD_ARMING_DEC; 4314 if (lun->be_lun->blocksize) 4315 ival /= lun->be_lun->blocksize; 4316 else 4317 ival /= 512; 4318 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4319 page->descr[0].count); 4320 } 4321 value = dnvlist_get_string(lun->be_lun->options, 4322 "used-threshold", NULL); 4323 if (value != NULL && 4324 ctl_expand_number(value, &ival) == 0) { 4325 page->descr[1].flags |= SLBPPD_ENABLED | 4326 SLBPPD_ARMING_INC; 4327 if (lun->be_lun->blocksize) 4328 ival /= lun->be_lun->blocksize; 4329 else 4330 ival /= 512; 4331 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4332 page->descr[1].count); 4333 } 4334 value = dnvlist_get_string(lun->be_lun->options, 4335 "pool-avail-threshold", NULL); 4336 if (value != NULL && 4337 ctl_expand_number(value, &ival) == 0) { 4338 page->descr[2].flags |= SLBPPD_ENABLED | 4339 SLBPPD_ARMING_DEC; 4340 if (lun->be_lun->blocksize) 4341 ival /= lun->be_lun->blocksize; 4342 else 4343 ival /= 512; 4344 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4345 page->descr[2].count); 4346 } 4347 value = dnvlist_get_string(lun->be_lun->options, 4348 "pool-used-threshold", NULL); 4349 if (value != NULL && 4350 ctl_expand_number(value, &ival) == 0) { 4351 page->descr[3].flags |= SLBPPD_ENABLED | 4352 SLBPPD_ARMING_INC; 4353 if (lun->be_lun->blocksize) 4354 ival /= lun->be_lun->blocksize; 4355 else 4356 ival /= 512; 4357 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4358 page->descr[3].count); 4359 } 4360 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4361 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4362 sizeof(lbp_page_default)); 4363 page_index->page_data = 4364 (uint8_t *)lun->mode_pages.lbp_page; 4365 break; 4366 } 4367 default: 4368 panic("subpage %#x for page %#x is incorrect!", 4369 page_index->subpage, page_code); 4370 } 4371 break; 4372 } 4373 case SMS_CDDVD_CAPS_PAGE:{ 4374 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4375 ("subpage %#x for page %#x is incorrect!", 4376 page_index->subpage, page_code)); 4377 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4378 &cddvd_page_default, 4379 sizeof(cddvd_page_default)); 4380 memcpy(&lun->mode_pages.cddvd_page[ 4381 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4382 sizeof(cddvd_page_changeable)); 4383 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4384 &cddvd_page_default, 4385 sizeof(cddvd_page_default)); 4386 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4387 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4388 sizeof(cddvd_page_default)); 4389 page_index->page_data = 4390 (uint8_t *)lun->mode_pages.cddvd_page; 4391 break; 4392 } 4393 default: 4394 panic("invalid page code value %#x", page_code); 4395 } 4396 } 4397 4398 return (CTL_RETVAL_COMPLETE); 4399 } 4400 4401 static int 4402 ctl_init_log_page_index(struct ctl_lun *lun) 4403 { 4404 struct ctl_page_index *page_index; 4405 int i, j, k, prev; 4406 4407 memcpy(&lun->log_pages.index, log_page_index_template, 4408 sizeof(log_page_index_template)); 4409 4410 prev = -1; 4411 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4412 4413 page_index = &lun->log_pages.index[i]; 4414 if (lun->be_lun->lun_type == T_DIRECT && 4415 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4416 continue; 4417 if (lun->be_lun->lun_type == T_PROCESSOR && 4418 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4419 continue; 4420 if (lun->be_lun->lun_type == T_CDROM && 4421 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4422 continue; 4423 4424 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4425 lun->backend->lun_attr == NULL) 4426 continue; 4427 4428 if (page_index->page_code != prev) { 4429 lun->log_pages.pages_page[j] = page_index->page_code; 4430 prev = page_index->page_code; 4431 j++; 4432 } 4433 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4434 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4435 k++; 4436 } 4437 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4438 lun->log_pages.index[0].page_len = j; 4439 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4440 lun->log_pages.index[1].page_len = k * 2; 4441 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page; 4442 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page); 4443 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0]; 4444 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS; 4445 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page; 4446 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page); 4447 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page; 4448 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page); 4449 4450 return (CTL_RETVAL_COMPLETE); 4451 } 4452 4453 static int 4454 hex2bin(const char *str, uint8_t *buf, int buf_size) 4455 { 4456 int i; 4457 u_char c; 4458 4459 memset(buf, 0, buf_size); 4460 while (isspace(str[0])) 4461 str++; 4462 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4463 str += 2; 4464 buf_size *= 2; 4465 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4466 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4467 str++; 4468 c = str[i]; 4469 if (isdigit(c)) 4470 c -= '0'; 4471 else if (isalpha(c)) 4472 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4473 else 4474 break; 4475 if (c >= 16) 4476 break; 4477 if ((i & 1) == 0) 4478 buf[i / 2] |= (c << 4); 4479 else 4480 buf[i / 2] |= c; 4481 } 4482 return ((i + 1) / 2); 4483 } 4484 4485 /* 4486 * Add LUN. 4487 * 4488 * Returns 0 for success, non-zero (errno) for failure. 4489 */ 4490 int 4491 ctl_add_lun(struct ctl_be_lun *be_lun) 4492 { 4493 struct ctl_softc *ctl_softc = control_softc; 4494 struct ctl_lun *nlun, *lun; 4495 struct scsi_vpd_id_descriptor *desc; 4496 struct scsi_vpd_id_t10 *t10id; 4497 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4498 int lun_number; 4499 int devidlen, idlen1, idlen2 = 0, len; 4500 4501 /* 4502 * We support only Direct Access, CD-ROM or Processor LUN types. 4503 */ 4504 switch (be_lun->lun_type) { 4505 case T_DIRECT: 4506 case T_PROCESSOR: 4507 case T_CDROM: 4508 break; 4509 case T_SEQUENTIAL: 4510 case T_CHANGER: 4511 default: 4512 return (EINVAL); 4513 } 4514 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO); 4515 4516 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4517 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4518 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4519 M_DEVBUF, M_WAITOK | M_ZERO); 4520 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4521 M_DEVBUF, M_WAITOK | M_ZERO); 4522 4523 /* Generate LUN ID. */ 4524 devidlen = max(CTL_DEVID_MIN_LEN, 4525 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4526 idlen1 = sizeof(*t10id) + devidlen; 4527 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4528 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4529 if (scsiname != NULL) { 4530 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4531 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4532 } 4533 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4534 if (eui != NULL) { 4535 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4536 } 4537 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4538 if (naa != NULL) { 4539 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4540 } 4541 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4542 if (uuid != NULL) { 4543 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4544 } 4545 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4546 M_CTL, M_WAITOK | M_ZERO); 4547 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4548 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4549 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4550 desc->length = idlen1; 4551 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4552 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4553 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4554 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4555 } else { 4556 strncpy(t10id->vendor, vendor, 4557 min(sizeof(t10id->vendor), strlen(vendor))); 4558 } 4559 strncpy((char *)t10id->vendor_spec_id, 4560 (char *)be_lun->device_id, devidlen); 4561 if (scsiname != NULL) { 4562 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4563 desc->length); 4564 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4565 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4566 SVPD_ID_TYPE_SCSI_NAME; 4567 desc->length = idlen2; 4568 strlcpy(desc->identifier, scsiname, idlen2); 4569 } 4570 if (eui != NULL) { 4571 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4572 desc->length); 4573 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4574 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4575 SVPD_ID_TYPE_EUI64; 4576 desc->length = hex2bin(eui, desc->identifier, 16); 4577 desc->length = desc->length > 12 ? 16 : 4578 (desc->length > 8 ? 12 : 8); 4579 len -= 16 - desc->length; 4580 } 4581 if (naa != NULL) { 4582 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4583 desc->length); 4584 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4585 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4586 SVPD_ID_TYPE_NAA; 4587 desc->length = hex2bin(naa, desc->identifier, 16); 4588 desc->length = desc->length > 8 ? 16 : 8; 4589 len -= 16 - desc->length; 4590 } 4591 if (uuid != NULL) { 4592 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4593 desc->length); 4594 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4595 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4596 SVPD_ID_TYPE_UUID; 4597 desc->identifier[0] = 0x10; 4598 hex2bin(uuid, &desc->identifier[2], 16); 4599 desc->length = 18; 4600 } 4601 lun->lun_devid->len = len; 4602 4603 mtx_lock(&ctl_softc->ctl_lock); 4604 /* 4605 * See if the caller requested a particular LUN number. If so, see 4606 * if it is available. Otherwise, allocate the first available LUN. 4607 */ 4608 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4609 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4610 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4611 mtx_unlock(&ctl_softc->ctl_lock); 4612 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4613 printf("ctl: requested LUN ID %d is higher " 4614 "than ctl_max_luns - 1 (%d)\n", 4615 be_lun->req_lun_id, ctl_max_luns - 1); 4616 } else { 4617 /* 4618 * XXX KDM return an error, or just assign 4619 * another LUN ID in this case?? 4620 */ 4621 printf("ctl: requested LUN ID %d is already " 4622 "in use\n", be_lun->req_lun_id); 4623 } 4624 fail: 4625 free(lun->lun_devid, M_CTL); 4626 free(lun, M_CTL); 4627 return (ENOSPC); 4628 } 4629 lun_number = be_lun->req_lun_id; 4630 } else { 4631 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4632 if (lun_number == -1) { 4633 mtx_unlock(&ctl_softc->ctl_lock); 4634 printf("ctl: can't allocate LUN, out of LUNs\n"); 4635 goto fail; 4636 } 4637 } 4638 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4639 mtx_unlock(&ctl_softc->ctl_lock); 4640 4641 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4642 lun->lun = lun_number; 4643 lun->be_lun = be_lun; 4644 /* 4645 * The processor LUN is always enabled. Disk LUNs come on line 4646 * disabled, and must be enabled by the backend. 4647 */ 4648 lun->flags |= CTL_LUN_DISABLED; 4649 lun->backend = be_lun->be; 4650 be_lun->ctl_lun = lun; 4651 be_lun->lun_id = lun_number; 4652 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4653 lun->flags |= CTL_LUN_EJECTED; 4654 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4655 lun->flags |= CTL_LUN_NO_MEDIA; 4656 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4657 lun->flags |= CTL_LUN_STOPPED; 4658 4659 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4660 lun->flags |= CTL_LUN_PRIMARY_SC; 4661 4662 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4663 if (value != NULL) { 4664 if (strcmp(value, "on") == 0) 4665 lun->flags |= CTL_LUN_REMOVABLE; 4666 } else if (be_lun->lun_type == T_CDROM) 4667 lun->flags |= CTL_LUN_REMOVABLE; 4668 4669 lun->ctl_softc = ctl_softc; 4670 #ifdef CTL_TIME_IO 4671 lun->last_busy = getsbinuptime(); 4672 #endif 4673 TAILQ_INIT(&lun->ooa_queue); 4674 STAILQ_INIT(&lun->error_list); 4675 lun->ie_reported = 1; 4676 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4677 ctl_tpc_lun_init(lun); 4678 if (lun->flags & CTL_LUN_REMOVABLE) { 4679 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4680 M_CTL, M_WAITOK); 4681 } 4682 4683 /* 4684 * Initialize the mode and log page index. 4685 */ 4686 ctl_init_page_index(lun); 4687 ctl_init_log_page_index(lun); 4688 4689 /* Setup statistics gathering */ 4690 lun->stats.item = lun_number; 4691 4692 /* 4693 * Now, before we insert this lun on the lun list, set the lun 4694 * inventory changed UA for all other luns. 4695 */ 4696 mtx_lock(&ctl_softc->ctl_lock); 4697 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4698 mtx_lock(&nlun->lun_lock); 4699 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4700 mtx_unlock(&nlun->lun_lock); 4701 } 4702 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4703 ctl_softc->ctl_luns[lun_number] = lun; 4704 ctl_softc->num_luns++; 4705 mtx_unlock(&ctl_softc->ctl_lock); 4706 4707 /* 4708 * We successfully added the LUN, attempt to enable it. 4709 */ 4710 if (ctl_enable_lun(lun) != 0) { 4711 printf("%s: ctl_enable_lun() failed!\n", __func__); 4712 mtx_lock(&ctl_softc->ctl_lock); 4713 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links); 4714 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number); 4715 ctl_softc->ctl_luns[lun_number] = NULL; 4716 ctl_softc->num_luns--; 4717 mtx_unlock(&ctl_softc->ctl_lock); 4718 free(lun->lun_devid, M_CTL); 4719 free(lun, M_CTL); 4720 return (EIO); 4721 } 4722 4723 return (0); 4724 } 4725 4726 /* 4727 * Free LUN that has no active requests. 4728 */ 4729 static int 4730 ctl_free_lun(struct ctl_lun *lun) 4731 { 4732 struct ctl_softc *softc = lun->ctl_softc; 4733 struct ctl_lun *nlun; 4734 int i; 4735 4736 KASSERT(TAILQ_EMPTY(&lun->ooa_queue), 4737 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4738 4739 mtx_lock(&softc->ctl_lock); 4740 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4741 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4742 softc->ctl_luns[lun->lun] = NULL; 4743 softc->num_luns--; 4744 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4745 mtx_lock(&nlun->lun_lock); 4746 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4747 mtx_unlock(&nlun->lun_lock); 4748 } 4749 mtx_unlock(&softc->ctl_lock); 4750 4751 /* 4752 * Tell the backend to free resources, if this LUN has a backend. 4753 */ 4754 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4755 4756 lun->ie_reportcnt = UINT32_MAX; 4757 callout_drain(&lun->ie_callout); 4758 ctl_tpc_lun_shutdown(lun); 4759 mtx_destroy(&lun->lun_lock); 4760 free(lun->lun_devid, M_CTL); 4761 for (i = 0; i < ctl_max_ports; i++) 4762 free(lun->pending_ua[i], M_CTL); 4763 free(lun->pending_ua, M_DEVBUF); 4764 for (i = 0; i < ctl_max_ports; i++) 4765 free(lun->pr_keys[i], M_CTL); 4766 free(lun->pr_keys, M_DEVBUF); 4767 free(lun->write_buffer, M_CTL); 4768 free(lun->prevent, M_CTL); 4769 free(lun, M_CTL); 4770 4771 return (0); 4772 } 4773 4774 static int 4775 ctl_enable_lun(struct ctl_lun *lun) 4776 { 4777 struct ctl_softc *softc; 4778 struct ctl_port *port, *nport; 4779 int retval; 4780 4781 softc = lun->ctl_softc; 4782 4783 mtx_lock(&softc->ctl_lock); 4784 mtx_lock(&lun->lun_lock); 4785 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0, 4786 ("%s: LUN not disabled", __func__)); 4787 lun->flags &= ~CTL_LUN_DISABLED; 4788 mtx_unlock(&lun->lun_lock); 4789 4790 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4791 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4792 port->lun_map != NULL || port->lun_enable == NULL) 4793 continue; 4794 4795 /* 4796 * Drop the lock while we call the FETD's enable routine. 4797 * This can lead to a callback into CTL (at least in the 4798 * case of the internal initiator frontend. 4799 */ 4800 mtx_unlock(&softc->ctl_lock); 4801 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4802 mtx_lock(&softc->ctl_lock); 4803 if (retval != 0) { 4804 printf("%s: FETD %s port %d returned error " 4805 "%d for lun_enable on lun %jd\n", 4806 __func__, port->port_name, port->targ_port, 4807 retval, (intmax_t)lun->lun); 4808 } 4809 } 4810 4811 mtx_unlock(&softc->ctl_lock); 4812 ctl_isc_announce_lun(lun); 4813 4814 return (0); 4815 } 4816 4817 static int 4818 ctl_disable_lun(struct ctl_lun *lun) 4819 { 4820 struct ctl_softc *softc; 4821 struct ctl_port *port; 4822 int retval; 4823 4824 softc = lun->ctl_softc; 4825 4826 mtx_lock(&softc->ctl_lock); 4827 mtx_lock(&lun->lun_lock); 4828 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0, 4829 ("%s: LUN not enabled", __func__)); 4830 lun->flags |= CTL_LUN_DISABLED; 4831 mtx_unlock(&lun->lun_lock); 4832 4833 STAILQ_FOREACH(port, &softc->port_list, links) { 4834 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4835 port->lun_map != NULL || port->lun_disable == NULL) 4836 continue; 4837 4838 /* 4839 * Drop the lock before we call the frontend's disable 4840 * routine, to avoid lock order reversals. 4841 * 4842 * XXX KDM what happens if the frontend list changes while 4843 * we're traversing it? It's unlikely, but should be handled. 4844 */ 4845 mtx_unlock(&softc->ctl_lock); 4846 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4847 mtx_lock(&softc->ctl_lock); 4848 if (retval != 0) { 4849 printf("%s: FETD %s port %d returned error " 4850 "%d for lun_disable on lun %jd\n", 4851 __func__, port->port_name, port->targ_port, 4852 retval, (intmax_t)lun->lun); 4853 } 4854 } 4855 4856 mtx_unlock(&softc->ctl_lock); 4857 ctl_isc_announce_lun(lun); 4858 4859 return (0); 4860 } 4861 4862 int 4863 ctl_start_lun(struct ctl_be_lun *be_lun) 4864 { 4865 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4866 4867 mtx_lock(&lun->lun_lock); 4868 lun->flags &= ~CTL_LUN_STOPPED; 4869 mtx_unlock(&lun->lun_lock); 4870 return (0); 4871 } 4872 4873 int 4874 ctl_stop_lun(struct ctl_be_lun *be_lun) 4875 { 4876 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4877 4878 mtx_lock(&lun->lun_lock); 4879 lun->flags |= CTL_LUN_STOPPED; 4880 mtx_unlock(&lun->lun_lock); 4881 return (0); 4882 } 4883 4884 int 4885 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4886 { 4887 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4888 4889 mtx_lock(&lun->lun_lock); 4890 lun->flags |= CTL_LUN_NO_MEDIA; 4891 mtx_unlock(&lun->lun_lock); 4892 return (0); 4893 } 4894 4895 int 4896 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4897 { 4898 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4899 union ctl_ha_msg msg; 4900 4901 mtx_lock(&lun->lun_lock); 4902 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4903 if (lun->flags & CTL_LUN_REMOVABLE) 4904 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4905 mtx_unlock(&lun->lun_lock); 4906 if ((lun->flags & CTL_LUN_REMOVABLE) && 4907 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4908 bzero(&msg.ua, sizeof(msg.ua)); 4909 msg.hdr.msg_type = CTL_MSG_UA; 4910 msg.hdr.nexus.initid = -1; 4911 msg.hdr.nexus.targ_port = -1; 4912 msg.hdr.nexus.targ_lun = lun->lun; 4913 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4914 msg.ua.ua_all = 1; 4915 msg.ua.ua_set = 1; 4916 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4917 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4918 M_WAITOK); 4919 } 4920 return (0); 4921 } 4922 4923 int 4924 ctl_lun_ejected(struct ctl_be_lun *be_lun) 4925 { 4926 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4927 4928 mtx_lock(&lun->lun_lock); 4929 lun->flags |= CTL_LUN_EJECTED; 4930 mtx_unlock(&lun->lun_lock); 4931 return (0); 4932 } 4933 4934 int 4935 ctl_lun_primary(struct ctl_be_lun *be_lun) 4936 { 4937 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4938 4939 mtx_lock(&lun->lun_lock); 4940 lun->flags |= CTL_LUN_PRIMARY_SC; 4941 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4942 mtx_unlock(&lun->lun_lock); 4943 ctl_isc_announce_lun(lun); 4944 return (0); 4945 } 4946 4947 int 4948 ctl_lun_secondary(struct ctl_be_lun *be_lun) 4949 { 4950 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4951 4952 mtx_lock(&lun->lun_lock); 4953 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4954 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4955 mtx_unlock(&lun->lun_lock); 4956 ctl_isc_announce_lun(lun); 4957 return (0); 4958 } 4959 4960 /* 4961 * Remove LUN. If there are active requests, wait for completion. 4962 * 4963 * Returns 0 for success, non-zero (errno) for failure. 4964 * Completion is reported to backed via the lun_shutdown() method. 4965 */ 4966 int 4967 ctl_remove_lun(struct ctl_be_lun *be_lun) 4968 { 4969 struct ctl_lun *lun; 4970 4971 lun = (struct ctl_lun *)be_lun->ctl_lun; 4972 4973 ctl_disable_lun(lun); 4974 4975 mtx_lock(&lun->lun_lock); 4976 lun->flags |= CTL_LUN_INVALID; 4977 4978 /* 4979 * If there is nothing in the OOA queue, go ahead and free the LUN. 4980 * If we have something in the OOA queue, we'll free it when the 4981 * last I/O completes. 4982 */ 4983 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4984 mtx_unlock(&lun->lun_lock); 4985 ctl_free_lun(lun); 4986 } else 4987 mtx_unlock(&lun->lun_lock); 4988 4989 return (0); 4990 } 4991 4992 void 4993 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4994 { 4995 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4996 union ctl_ha_msg msg; 4997 4998 mtx_lock(&lun->lun_lock); 4999 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5000 mtx_unlock(&lun->lun_lock); 5001 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5002 /* Send msg to other side. */ 5003 bzero(&msg.ua, sizeof(msg.ua)); 5004 msg.hdr.msg_type = CTL_MSG_UA; 5005 msg.hdr.nexus.initid = -1; 5006 msg.hdr.nexus.targ_port = -1; 5007 msg.hdr.nexus.targ_lun = lun->lun; 5008 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5009 msg.ua.ua_all = 1; 5010 msg.ua.ua_set = 1; 5011 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5012 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5013 M_WAITOK); 5014 } 5015 } 5016 5017 /* 5018 * Backend "memory move is complete" callback for requests that never 5019 * make it down to say RAIDCore's configuration code. 5020 */ 5021 int 5022 ctl_config_move_done(union ctl_io *io) 5023 { 5024 int retval; 5025 5026 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5027 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5028 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5029 5030 if ((io->io_hdr.port_status != 0) && 5031 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5032 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5033 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 5034 /*retry_count*/ io->io_hdr.port_status); 5035 } else if (io->scsiio.kern_data_resid != 0 && 5036 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 5037 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5038 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5039 ctl_set_invalid_field_ciu(&io->scsiio); 5040 } 5041 5042 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5043 ctl_data_print(io); 5044 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5045 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5046 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5047 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5048 /* 5049 * XXX KDM just assuming a single pointer here, and not a 5050 * S/G list. If we start using S/G lists for config data, 5051 * we'll need to know how to clean them up here as well. 5052 */ 5053 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5054 free(io->scsiio.kern_data_ptr, M_CTL); 5055 ctl_done(io); 5056 retval = CTL_RETVAL_COMPLETE; 5057 } else { 5058 /* 5059 * XXX KDM now we need to continue data movement. Some 5060 * options: 5061 * - call ctl_scsiio() again? We don't do this for data 5062 * writes, because for those at least we know ahead of 5063 * time where the write will go and how long it is. For 5064 * config writes, though, that information is largely 5065 * contained within the write itself, thus we need to 5066 * parse out the data again. 5067 * 5068 * - Call some other function once the data is in? 5069 */ 5070 5071 /* 5072 * XXX KDM call ctl_scsiio() again for now, and check flag 5073 * bits to see whether we're allocated or not. 5074 */ 5075 retval = ctl_scsiio(&io->scsiio); 5076 } 5077 return (retval); 5078 } 5079 5080 /* 5081 * This gets called by a backend driver when it is done with a 5082 * data_submit method. 5083 */ 5084 void 5085 ctl_data_submit_done(union ctl_io *io) 5086 { 5087 /* 5088 * If the IO_CONT flag is set, we need to call the supplied 5089 * function to continue processing the I/O, instead of completing 5090 * the I/O just yet. 5091 * 5092 * If there is an error, though, we don't want to keep processing. 5093 * Instead, just send status back to the initiator. 5094 */ 5095 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5096 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5097 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5098 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5099 io->scsiio.io_cont(io); 5100 return; 5101 } 5102 ctl_done(io); 5103 } 5104 5105 /* 5106 * This gets called by a backend driver when it is done with a 5107 * configuration write. 5108 */ 5109 void 5110 ctl_config_write_done(union ctl_io *io) 5111 { 5112 uint8_t *buf; 5113 5114 /* 5115 * If the IO_CONT flag is set, we need to call the supplied 5116 * function to continue processing the I/O, instead of completing 5117 * the I/O just yet. 5118 * 5119 * If there is an error, though, we don't want to keep processing. 5120 * Instead, just send status back to the initiator. 5121 */ 5122 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5123 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5124 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5125 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5126 io->scsiio.io_cont(io); 5127 return; 5128 } 5129 /* 5130 * Since a configuration write can be done for commands that actually 5131 * have data allocated, like write buffer, and commands that have 5132 * no data, like start/stop unit, we need to check here. 5133 */ 5134 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5135 buf = io->scsiio.kern_data_ptr; 5136 else 5137 buf = NULL; 5138 ctl_done(io); 5139 if (buf) 5140 free(buf, M_CTL); 5141 } 5142 5143 void 5144 ctl_config_read_done(union ctl_io *io) 5145 { 5146 uint8_t *buf; 5147 5148 /* 5149 * If there is some error -- we are done, skip data transfer. 5150 */ 5151 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5152 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5153 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5154 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5155 buf = io->scsiio.kern_data_ptr; 5156 else 5157 buf = NULL; 5158 ctl_done(io); 5159 if (buf) 5160 free(buf, M_CTL); 5161 return; 5162 } 5163 5164 /* 5165 * If the IO_CONT flag is set, we need to call the supplied 5166 * function to continue processing the I/O, instead of completing 5167 * the I/O just yet. 5168 */ 5169 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5170 io->scsiio.io_cont(io); 5171 return; 5172 } 5173 5174 ctl_datamove(io); 5175 } 5176 5177 /* 5178 * SCSI release command. 5179 */ 5180 int 5181 ctl_scsi_release(struct ctl_scsiio *ctsio) 5182 { 5183 struct ctl_lun *lun = CTL_LUN(ctsio); 5184 uint32_t residx; 5185 5186 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5187 5188 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5189 5190 /* 5191 * XXX KDM right now, we only support LUN reservation. We don't 5192 * support 3rd party reservations, or extent reservations, which 5193 * might actually need the parameter list. If we've gotten this 5194 * far, we've got a LUN reservation. Anything else got kicked out 5195 * above. So, according to SPC, ignore the length. 5196 */ 5197 5198 mtx_lock(&lun->lun_lock); 5199 5200 /* 5201 * According to SPC, it is not an error for an intiator to attempt 5202 * to release a reservation on a LUN that isn't reserved, or that 5203 * is reserved by another initiator. The reservation can only be 5204 * released, though, by the initiator who made it or by one of 5205 * several reset type events. 5206 */ 5207 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5208 lun->flags &= ~CTL_LUN_RESERVED; 5209 5210 mtx_unlock(&lun->lun_lock); 5211 5212 ctl_set_success(ctsio); 5213 ctl_done((union ctl_io *)ctsio); 5214 return (CTL_RETVAL_COMPLETE); 5215 } 5216 5217 int 5218 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5219 { 5220 struct ctl_lun *lun = CTL_LUN(ctsio); 5221 uint32_t residx; 5222 5223 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5224 5225 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5226 5227 /* 5228 * XXX KDM right now, we only support LUN reservation. We don't 5229 * support 3rd party reservations, or extent reservations, which 5230 * might actually need the parameter list. If we've gotten this 5231 * far, we've got a LUN reservation. Anything else got kicked out 5232 * above. So, according to SPC, ignore the length. 5233 */ 5234 5235 mtx_lock(&lun->lun_lock); 5236 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5237 ctl_set_reservation_conflict(ctsio); 5238 goto bailout; 5239 } 5240 5241 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5242 if (lun->flags & CTL_LUN_PR_RESERVED) { 5243 ctl_set_success(ctsio); 5244 goto bailout; 5245 } 5246 5247 lun->flags |= CTL_LUN_RESERVED; 5248 lun->res_idx = residx; 5249 ctl_set_success(ctsio); 5250 5251 bailout: 5252 mtx_unlock(&lun->lun_lock); 5253 ctl_done((union ctl_io *)ctsio); 5254 return (CTL_RETVAL_COMPLETE); 5255 } 5256 5257 int 5258 ctl_start_stop(struct ctl_scsiio *ctsio) 5259 { 5260 struct ctl_lun *lun = CTL_LUN(ctsio); 5261 struct scsi_start_stop_unit *cdb; 5262 int retval; 5263 5264 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5265 5266 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5267 5268 if ((cdb->how & SSS_PC_MASK) == 0) { 5269 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5270 (cdb->how & SSS_START) == 0) { 5271 uint32_t residx; 5272 5273 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5274 if (ctl_get_prkey(lun, residx) == 0 || 5275 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5276 5277 ctl_set_reservation_conflict(ctsio); 5278 ctl_done((union ctl_io *)ctsio); 5279 return (CTL_RETVAL_COMPLETE); 5280 } 5281 } 5282 5283 if ((cdb->how & SSS_LOEJ) && 5284 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5285 ctl_set_invalid_field(ctsio, 5286 /*sks_valid*/ 1, 5287 /*command*/ 1, 5288 /*field*/ 4, 5289 /*bit_valid*/ 1, 5290 /*bit*/ 1); 5291 ctl_done((union ctl_io *)ctsio); 5292 return (CTL_RETVAL_COMPLETE); 5293 } 5294 5295 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5296 lun->prevent_count > 0) { 5297 /* "Medium removal prevented" */ 5298 ctl_set_sense(ctsio, /*current_error*/ 1, 5299 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5300 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5301 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5302 ctl_done((union ctl_io *)ctsio); 5303 return (CTL_RETVAL_COMPLETE); 5304 } 5305 } 5306 5307 retval = lun->backend->config_write((union ctl_io *)ctsio); 5308 return (retval); 5309 } 5310 5311 int 5312 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5313 { 5314 struct ctl_lun *lun = CTL_LUN(ctsio); 5315 struct scsi_prevent *cdb; 5316 int retval; 5317 uint32_t initidx; 5318 5319 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5320 5321 cdb = (struct scsi_prevent *)ctsio->cdb; 5322 5323 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5324 ctl_set_invalid_opcode(ctsio); 5325 ctl_done((union ctl_io *)ctsio); 5326 return (CTL_RETVAL_COMPLETE); 5327 } 5328 5329 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5330 mtx_lock(&lun->lun_lock); 5331 if ((cdb->how & PR_PREVENT) && 5332 ctl_is_set(lun->prevent, initidx) == 0) { 5333 ctl_set_mask(lun->prevent, initidx); 5334 lun->prevent_count++; 5335 } else if ((cdb->how & PR_PREVENT) == 0 && 5336 ctl_is_set(lun->prevent, initidx)) { 5337 ctl_clear_mask(lun->prevent, initidx); 5338 lun->prevent_count--; 5339 } 5340 mtx_unlock(&lun->lun_lock); 5341 retval = lun->backend->config_write((union ctl_io *)ctsio); 5342 return (retval); 5343 } 5344 5345 /* 5346 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5347 * we don't really do anything with the LBA and length fields if the user 5348 * passes them in. Instead we'll just flush out the cache for the entire 5349 * LUN. 5350 */ 5351 int 5352 ctl_sync_cache(struct ctl_scsiio *ctsio) 5353 { 5354 struct ctl_lun *lun = CTL_LUN(ctsio); 5355 struct ctl_lba_len_flags *lbalen; 5356 uint64_t starting_lba; 5357 uint32_t block_count; 5358 int retval; 5359 uint8_t byte2; 5360 5361 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5362 5363 retval = 0; 5364 5365 switch (ctsio->cdb[0]) { 5366 case SYNCHRONIZE_CACHE: { 5367 struct scsi_sync_cache *cdb; 5368 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5369 5370 starting_lba = scsi_4btoul(cdb->begin_lba); 5371 block_count = scsi_2btoul(cdb->lb_count); 5372 byte2 = cdb->byte2; 5373 break; 5374 } 5375 case SYNCHRONIZE_CACHE_16: { 5376 struct scsi_sync_cache_16 *cdb; 5377 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5378 5379 starting_lba = scsi_8btou64(cdb->begin_lba); 5380 block_count = scsi_4btoul(cdb->lb_count); 5381 byte2 = cdb->byte2; 5382 break; 5383 } 5384 default: 5385 ctl_set_invalid_opcode(ctsio); 5386 ctl_done((union ctl_io *)ctsio); 5387 goto bailout; 5388 break; /* NOTREACHED */ 5389 } 5390 5391 /* 5392 * We check the LBA and length, but don't do anything with them. 5393 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5394 * get flushed. This check will just help satisfy anyone who wants 5395 * to see an error for an out of range LBA. 5396 */ 5397 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5398 ctl_set_lba_out_of_range(ctsio, 5399 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5400 ctl_done((union ctl_io *)ctsio); 5401 goto bailout; 5402 } 5403 5404 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5405 lbalen->lba = starting_lba; 5406 lbalen->len = block_count; 5407 lbalen->flags = byte2; 5408 retval = lun->backend->config_write((union ctl_io *)ctsio); 5409 5410 bailout: 5411 return (retval); 5412 } 5413 5414 int 5415 ctl_format(struct ctl_scsiio *ctsio) 5416 { 5417 struct scsi_format *cdb; 5418 int length, defect_list_len; 5419 5420 CTL_DEBUG_PRINT(("ctl_format\n")); 5421 5422 cdb = (struct scsi_format *)ctsio->cdb; 5423 5424 length = 0; 5425 if (cdb->byte2 & SF_FMTDATA) { 5426 if (cdb->byte2 & SF_LONGLIST) 5427 length = sizeof(struct scsi_format_header_long); 5428 else 5429 length = sizeof(struct scsi_format_header_short); 5430 } 5431 5432 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5433 && (length > 0)) { 5434 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5435 ctsio->kern_data_len = length; 5436 ctsio->kern_total_len = length; 5437 ctsio->kern_rel_offset = 0; 5438 ctsio->kern_sg_entries = 0; 5439 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5440 ctsio->be_move_done = ctl_config_move_done; 5441 ctl_datamove((union ctl_io *)ctsio); 5442 5443 return (CTL_RETVAL_COMPLETE); 5444 } 5445 5446 defect_list_len = 0; 5447 5448 if (cdb->byte2 & SF_FMTDATA) { 5449 if (cdb->byte2 & SF_LONGLIST) { 5450 struct scsi_format_header_long *header; 5451 5452 header = (struct scsi_format_header_long *) 5453 ctsio->kern_data_ptr; 5454 5455 defect_list_len = scsi_4btoul(header->defect_list_len); 5456 if (defect_list_len != 0) { 5457 ctl_set_invalid_field(ctsio, 5458 /*sks_valid*/ 1, 5459 /*command*/ 0, 5460 /*field*/ 2, 5461 /*bit_valid*/ 0, 5462 /*bit*/ 0); 5463 goto bailout; 5464 } 5465 } else { 5466 struct scsi_format_header_short *header; 5467 5468 header = (struct scsi_format_header_short *) 5469 ctsio->kern_data_ptr; 5470 5471 defect_list_len = scsi_2btoul(header->defect_list_len); 5472 if (defect_list_len != 0) { 5473 ctl_set_invalid_field(ctsio, 5474 /*sks_valid*/ 1, 5475 /*command*/ 0, 5476 /*field*/ 2, 5477 /*bit_valid*/ 0, 5478 /*bit*/ 0); 5479 goto bailout; 5480 } 5481 } 5482 } 5483 5484 ctl_set_success(ctsio); 5485 bailout: 5486 5487 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5488 free(ctsio->kern_data_ptr, M_CTL); 5489 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5490 } 5491 5492 ctl_done((union ctl_io *)ctsio); 5493 return (CTL_RETVAL_COMPLETE); 5494 } 5495 5496 int 5497 ctl_read_buffer(struct ctl_scsiio *ctsio) 5498 { 5499 struct ctl_lun *lun = CTL_LUN(ctsio); 5500 uint64_t buffer_offset; 5501 uint32_t len; 5502 uint8_t byte2; 5503 static uint8_t descr[4]; 5504 static uint8_t echo_descr[4] = { 0 }; 5505 5506 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5507 5508 switch (ctsio->cdb[0]) { 5509 case READ_BUFFER: { 5510 struct scsi_read_buffer *cdb; 5511 5512 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5513 buffer_offset = scsi_3btoul(cdb->offset); 5514 len = scsi_3btoul(cdb->length); 5515 byte2 = cdb->byte2; 5516 break; 5517 } 5518 case READ_BUFFER_16: { 5519 struct scsi_read_buffer_16 *cdb; 5520 5521 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5522 buffer_offset = scsi_8btou64(cdb->offset); 5523 len = scsi_4btoul(cdb->length); 5524 byte2 = cdb->byte2; 5525 break; 5526 } 5527 default: /* This shouldn't happen. */ 5528 ctl_set_invalid_opcode(ctsio); 5529 ctl_done((union ctl_io *)ctsio); 5530 return (CTL_RETVAL_COMPLETE); 5531 } 5532 5533 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5534 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5535 ctl_set_invalid_field(ctsio, 5536 /*sks_valid*/ 1, 5537 /*command*/ 1, 5538 /*field*/ 6, 5539 /*bit_valid*/ 0, 5540 /*bit*/ 0); 5541 ctl_done((union ctl_io *)ctsio); 5542 return (CTL_RETVAL_COMPLETE); 5543 } 5544 5545 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5546 descr[0] = 0; 5547 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5548 ctsio->kern_data_ptr = descr; 5549 len = min(len, sizeof(descr)); 5550 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5551 ctsio->kern_data_ptr = echo_descr; 5552 len = min(len, sizeof(echo_descr)); 5553 } else { 5554 if (lun->write_buffer == NULL) { 5555 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5556 M_CTL, M_WAITOK); 5557 } 5558 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5559 } 5560 ctsio->kern_data_len = len; 5561 ctsio->kern_total_len = len; 5562 ctsio->kern_rel_offset = 0; 5563 ctsio->kern_sg_entries = 0; 5564 ctl_set_success(ctsio); 5565 ctsio->be_move_done = ctl_config_move_done; 5566 ctl_datamove((union ctl_io *)ctsio); 5567 return (CTL_RETVAL_COMPLETE); 5568 } 5569 5570 int 5571 ctl_write_buffer(struct ctl_scsiio *ctsio) 5572 { 5573 struct ctl_lun *lun = CTL_LUN(ctsio); 5574 struct scsi_write_buffer *cdb; 5575 int buffer_offset, len; 5576 5577 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5578 5579 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5580 5581 len = scsi_3btoul(cdb->length); 5582 buffer_offset = scsi_3btoul(cdb->offset); 5583 5584 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5585 ctl_set_invalid_field(ctsio, 5586 /*sks_valid*/ 1, 5587 /*command*/ 1, 5588 /*field*/ 6, 5589 /*bit_valid*/ 0, 5590 /*bit*/ 0); 5591 ctl_done((union ctl_io *)ctsio); 5592 return (CTL_RETVAL_COMPLETE); 5593 } 5594 5595 /* 5596 * If we've got a kernel request that hasn't been malloced yet, 5597 * malloc it and tell the caller the data buffer is here. 5598 */ 5599 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5600 if (lun->write_buffer == NULL) { 5601 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5602 M_CTL, M_WAITOK); 5603 } 5604 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5605 ctsio->kern_data_len = len; 5606 ctsio->kern_total_len = len; 5607 ctsio->kern_rel_offset = 0; 5608 ctsio->kern_sg_entries = 0; 5609 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5610 ctsio->be_move_done = ctl_config_move_done; 5611 ctl_datamove((union ctl_io *)ctsio); 5612 5613 return (CTL_RETVAL_COMPLETE); 5614 } 5615 5616 ctl_set_success(ctsio); 5617 ctl_done((union ctl_io *)ctsio); 5618 return (CTL_RETVAL_COMPLETE); 5619 } 5620 5621 static int 5622 ctl_write_same_cont(union ctl_io *io) 5623 { 5624 struct ctl_lun *lun = CTL_LUN(io); 5625 struct ctl_scsiio *ctsio; 5626 struct ctl_lba_len_flags *lbalen; 5627 int retval; 5628 5629 ctsio = &io->scsiio; 5630 ctsio->io_hdr.status = CTL_STATUS_NONE; 5631 lbalen = (struct ctl_lba_len_flags *) 5632 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5633 lbalen->lba += lbalen->len; 5634 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) { 5635 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 5636 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba; 5637 } 5638 5639 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n")); 5640 retval = lun->backend->config_write((union ctl_io *)ctsio); 5641 return (retval); 5642 } 5643 5644 int 5645 ctl_write_same(struct ctl_scsiio *ctsio) 5646 { 5647 struct ctl_lun *lun = CTL_LUN(ctsio); 5648 struct ctl_lba_len_flags *lbalen; 5649 const char *val; 5650 uint64_t lba, ival; 5651 uint32_t num_blocks; 5652 int len, retval; 5653 uint8_t byte2; 5654 5655 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5656 5657 switch (ctsio->cdb[0]) { 5658 case WRITE_SAME_10: { 5659 struct scsi_write_same_10 *cdb; 5660 5661 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5662 5663 lba = scsi_4btoul(cdb->addr); 5664 num_blocks = scsi_2btoul(cdb->length); 5665 byte2 = cdb->byte2; 5666 break; 5667 } 5668 case WRITE_SAME_16: { 5669 struct scsi_write_same_16 *cdb; 5670 5671 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5672 5673 lba = scsi_8btou64(cdb->addr); 5674 num_blocks = scsi_4btoul(cdb->length); 5675 byte2 = cdb->byte2; 5676 break; 5677 } 5678 default: 5679 /* 5680 * We got a command we don't support. This shouldn't 5681 * happen, commands should be filtered out above us. 5682 */ 5683 ctl_set_invalid_opcode(ctsio); 5684 ctl_done((union ctl_io *)ctsio); 5685 5686 return (CTL_RETVAL_COMPLETE); 5687 break; /* NOTREACHED */ 5688 } 5689 5690 /* ANCHOR flag can be used only together with UNMAP */ 5691 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5692 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5693 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5694 ctl_done((union ctl_io *)ctsio); 5695 return (CTL_RETVAL_COMPLETE); 5696 } 5697 5698 /* 5699 * The first check is to make sure we're in bounds, the second 5700 * check is to catch wrap-around problems. If the lba + num blocks 5701 * is less than the lba, then we've wrapped around and the block 5702 * range is invalid anyway. 5703 */ 5704 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5705 || ((lba + num_blocks) < lba)) { 5706 ctl_set_lba_out_of_range(ctsio, 5707 MAX(lba, lun->be_lun->maxlba + 1)); 5708 ctl_done((union ctl_io *)ctsio); 5709 return (CTL_RETVAL_COMPLETE); 5710 } 5711 5712 /* Zero number of blocks means "to the last logical block" */ 5713 if (num_blocks == 0) { 5714 ival = UINT64_MAX; 5715 val = dnvlist_get_string(lun->be_lun->options, 5716 "write_same_max_lba", NULL); 5717 if (val != NULL) 5718 ctl_expand_number(val, &ival); 5719 if ((lun->be_lun->maxlba + 1) - lba > ival) { 5720 ctl_set_invalid_field(ctsio, 5721 /*sks_valid*/ 1, /*command*/ 1, 5722 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10, 5723 /*bit_valid*/ 0, /*bit*/ 0); 5724 ctl_done((union ctl_io *)ctsio); 5725 return (CTL_RETVAL_COMPLETE); 5726 } 5727 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5728 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 5729 ctsio->io_cont = ctl_write_same_cont; 5730 num_blocks = 1 << 31; 5731 } else 5732 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5733 } 5734 5735 len = lun->be_lun->blocksize; 5736 5737 /* 5738 * If we've got a kernel request that hasn't been malloced yet, 5739 * malloc it and tell the caller the data buffer is here. 5740 */ 5741 if ((byte2 & SWS_NDOB) == 0 && 5742 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5743 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5744 ctsio->kern_data_len = len; 5745 ctsio->kern_total_len = len; 5746 ctsio->kern_rel_offset = 0; 5747 ctsio->kern_sg_entries = 0; 5748 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5749 ctsio->be_move_done = ctl_config_move_done; 5750 ctl_datamove((union ctl_io *)ctsio); 5751 5752 return (CTL_RETVAL_COMPLETE); 5753 } 5754 5755 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5756 lbalen->lba = lba; 5757 lbalen->len = num_blocks; 5758 lbalen->flags = byte2; 5759 retval = lun->backend->config_write((union ctl_io *)ctsio); 5760 5761 return (retval); 5762 } 5763 5764 int 5765 ctl_unmap(struct ctl_scsiio *ctsio) 5766 { 5767 struct ctl_lun *lun = CTL_LUN(ctsio); 5768 struct scsi_unmap *cdb; 5769 struct ctl_ptr_len_flags *ptrlen; 5770 struct scsi_unmap_header *hdr; 5771 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5772 uint64_t lba; 5773 uint32_t num_blocks; 5774 int len, retval; 5775 uint8_t byte2; 5776 5777 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5778 5779 cdb = (struct scsi_unmap *)ctsio->cdb; 5780 len = scsi_2btoul(cdb->length); 5781 byte2 = cdb->byte2; 5782 5783 /* 5784 * If we've got a kernel request that hasn't been malloced yet, 5785 * malloc it and tell the caller the data buffer is here. 5786 */ 5787 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5788 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5789 ctsio->kern_data_len = len; 5790 ctsio->kern_total_len = len; 5791 ctsio->kern_rel_offset = 0; 5792 ctsio->kern_sg_entries = 0; 5793 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5794 ctsio->be_move_done = ctl_config_move_done; 5795 ctl_datamove((union ctl_io *)ctsio); 5796 5797 return (CTL_RETVAL_COMPLETE); 5798 } 5799 5800 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5801 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5802 if (len < sizeof (*hdr) || 5803 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5804 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5805 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5806 ctl_set_invalid_field(ctsio, 5807 /*sks_valid*/ 0, 5808 /*command*/ 0, 5809 /*field*/ 0, 5810 /*bit_valid*/ 0, 5811 /*bit*/ 0); 5812 goto done; 5813 } 5814 len = scsi_2btoul(hdr->desc_length); 5815 buf = (struct scsi_unmap_desc *)(hdr + 1); 5816 end = buf + len / sizeof(*buf); 5817 5818 endnz = buf; 5819 for (range = buf; range < end; range++) { 5820 lba = scsi_8btou64(range->lba); 5821 num_blocks = scsi_4btoul(range->length); 5822 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5823 || ((lba + num_blocks) < lba)) { 5824 ctl_set_lba_out_of_range(ctsio, 5825 MAX(lba, lun->be_lun->maxlba + 1)); 5826 ctl_done((union ctl_io *)ctsio); 5827 return (CTL_RETVAL_COMPLETE); 5828 } 5829 if (num_blocks != 0) 5830 endnz = range + 1; 5831 } 5832 5833 /* 5834 * Block backend can not handle zero last range. 5835 * Filter it out and return if there is nothing left. 5836 */ 5837 len = (uint8_t *)endnz - (uint8_t *)buf; 5838 if (len == 0) { 5839 ctl_set_success(ctsio); 5840 goto done; 5841 } 5842 5843 mtx_lock(&lun->lun_lock); 5844 ptrlen = (struct ctl_ptr_len_flags *) 5845 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5846 ptrlen->ptr = (void *)buf; 5847 ptrlen->len = len; 5848 ptrlen->flags = byte2; 5849 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE); 5850 mtx_unlock(&lun->lun_lock); 5851 5852 retval = lun->backend->config_write((union ctl_io *)ctsio); 5853 return (retval); 5854 5855 done: 5856 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5857 free(ctsio->kern_data_ptr, M_CTL); 5858 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5859 } 5860 ctl_done((union ctl_io *)ctsio); 5861 return (CTL_RETVAL_COMPLETE); 5862 } 5863 5864 int 5865 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5866 struct ctl_page_index *page_index, uint8_t *page_ptr) 5867 { 5868 struct ctl_lun *lun = CTL_LUN(ctsio); 5869 uint8_t *current_cp; 5870 int set_ua; 5871 uint32_t initidx; 5872 5873 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5874 set_ua = 0; 5875 5876 current_cp = (page_index->page_data + (page_index->page_len * 5877 CTL_PAGE_CURRENT)); 5878 5879 mtx_lock(&lun->lun_lock); 5880 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5881 memcpy(current_cp, page_ptr, page_index->page_len); 5882 set_ua = 1; 5883 } 5884 if (set_ua != 0) 5885 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5886 mtx_unlock(&lun->lun_lock); 5887 if (set_ua) { 5888 ctl_isc_announce_mode(lun, 5889 ctl_get_initindex(&ctsio->io_hdr.nexus), 5890 page_index->page_code, page_index->subpage); 5891 } 5892 return (CTL_RETVAL_COMPLETE); 5893 } 5894 5895 static void 5896 ctl_ie_timer(void *arg) 5897 { 5898 struct ctl_lun *lun = arg; 5899 uint64_t t; 5900 5901 if (lun->ie_asc == 0) 5902 return; 5903 5904 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5905 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5906 else 5907 lun->ie_reported = 0; 5908 5909 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5910 lun->ie_reportcnt++; 5911 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5912 if (t == 0 || t == UINT32_MAX) 5913 t = 3000; /* 5 min */ 5914 callout_schedule(&lun->ie_callout, t * hz / 10); 5915 } 5916 } 5917 5918 int 5919 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5920 struct ctl_page_index *page_index, uint8_t *page_ptr) 5921 { 5922 struct ctl_lun *lun = CTL_LUN(ctsio); 5923 struct scsi_info_exceptions_page *pg; 5924 uint64_t t; 5925 5926 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5927 5928 pg = (struct scsi_info_exceptions_page *)page_ptr; 5929 mtx_lock(&lun->lun_lock); 5930 if (pg->info_flags & SIEP_FLAGS_TEST) { 5931 lun->ie_asc = 0x5d; 5932 lun->ie_ascq = 0xff; 5933 if (pg->mrie == SIEP_MRIE_UA) { 5934 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5935 lun->ie_reported = 1; 5936 } else { 5937 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5938 lun->ie_reported = -1; 5939 } 5940 lun->ie_reportcnt = 1; 5941 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5942 lun->ie_reportcnt++; 5943 t = scsi_4btoul(pg->interval_timer); 5944 if (t == 0 || t == UINT32_MAX) 5945 t = 3000; /* 5 min */ 5946 callout_reset(&lun->ie_callout, t * hz / 10, 5947 ctl_ie_timer, lun); 5948 } 5949 } else { 5950 lun->ie_asc = 0; 5951 lun->ie_ascq = 0; 5952 lun->ie_reported = 1; 5953 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5954 lun->ie_reportcnt = UINT32_MAX; 5955 callout_stop(&lun->ie_callout); 5956 } 5957 mtx_unlock(&lun->lun_lock); 5958 return (CTL_RETVAL_COMPLETE); 5959 } 5960 5961 static int 5962 ctl_do_mode_select(union ctl_io *io) 5963 { 5964 struct ctl_lun *lun = CTL_LUN(io); 5965 struct scsi_mode_page_header *page_header; 5966 struct ctl_page_index *page_index; 5967 struct ctl_scsiio *ctsio; 5968 int page_len, page_len_offset, page_len_size; 5969 union ctl_modepage_info *modepage_info; 5970 uint16_t *len_left, *len_used; 5971 int retval, i; 5972 5973 ctsio = &io->scsiio; 5974 page_index = NULL; 5975 page_len = 0; 5976 5977 modepage_info = (union ctl_modepage_info *) 5978 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5979 len_left = &modepage_info->header.len_left; 5980 len_used = &modepage_info->header.len_used; 5981 5982 do_next_page: 5983 5984 page_header = (struct scsi_mode_page_header *) 5985 (ctsio->kern_data_ptr + *len_used); 5986 5987 if (*len_left == 0) { 5988 free(ctsio->kern_data_ptr, M_CTL); 5989 ctl_set_success(ctsio); 5990 ctl_done((union ctl_io *)ctsio); 5991 return (CTL_RETVAL_COMPLETE); 5992 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5993 5994 free(ctsio->kern_data_ptr, M_CTL); 5995 ctl_set_param_len_error(ctsio); 5996 ctl_done((union ctl_io *)ctsio); 5997 return (CTL_RETVAL_COMPLETE); 5998 5999 } else if ((page_header->page_code & SMPH_SPF) 6000 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6001 6002 free(ctsio->kern_data_ptr, M_CTL); 6003 ctl_set_param_len_error(ctsio); 6004 ctl_done((union ctl_io *)ctsio); 6005 return (CTL_RETVAL_COMPLETE); 6006 } 6007 6008 6009 /* 6010 * XXX KDM should we do something with the block descriptor? 6011 */ 6012 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6013 page_index = &lun->mode_pages.index[i]; 6014 if (lun->be_lun->lun_type == T_DIRECT && 6015 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6016 continue; 6017 if (lun->be_lun->lun_type == T_PROCESSOR && 6018 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6019 continue; 6020 if (lun->be_lun->lun_type == T_CDROM && 6021 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6022 continue; 6023 6024 if ((page_index->page_code & SMPH_PC_MASK) != 6025 (page_header->page_code & SMPH_PC_MASK)) 6026 continue; 6027 6028 /* 6029 * If neither page has a subpage code, then we've got a 6030 * match. 6031 */ 6032 if (((page_index->page_code & SMPH_SPF) == 0) 6033 && ((page_header->page_code & SMPH_SPF) == 0)) { 6034 page_len = page_header->page_length; 6035 break; 6036 } 6037 6038 /* 6039 * If both pages have subpages, then the subpage numbers 6040 * have to match. 6041 */ 6042 if ((page_index->page_code & SMPH_SPF) 6043 && (page_header->page_code & SMPH_SPF)) { 6044 struct scsi_mode_page_header_sp *sph; 6045 6046 sph = (struct scsi_mode_page_header_sp *)page_header; 6047 if (page_index->subpage == sph->subpage) { 6048 page_len = scsi_2btoul(sph->page_length); 6049 break; 6050 } 6051 } 6052 } 6053 6054 /* 6055 * If we couldn't find the page, or if we don't have a mode select 6056 * handler for it, send back an error to the user. 6057 */ 6058 if ((i >= CTL_NUM_MODE_PAGES) 6059 || (page_index->select_handler == NULL)) { 6060 ctl_set_invalid_field(ctsio, 6061 /*sks_valid*/ 1, 6062 /*command*/ 0, 6063 /*field*/ *len_used, 6064 /*bit_valid*/ 0, 6065 /*bit*/ 0); 6066 free(ctsio->kern_data_ptr, M_CTL); 6067 ctl_done((union ctl_io *)ctsio); 6068 return (CTL_RETVAL_COMPLETE); 6069 } 6070 6071 if (page_index->page_code & SMPH_SPF) { 6072 page_len_offset = 2; 6073 page_len_size = 2; 6074 } else { 6075 page_len_size = 1; 6076 page_len_offset = 1; 6077 } 6078 6079 /* 6080 * If the length the initiator gives us isn't the one we specify in 6081 * the mode page header, or if they didn't specify enough data in 6082 * the CDB to avoid truncating this page, kick out the request. 6083 */ 6084 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6085 ctl_set_invalid_field(ctsio, 6086 /*sks_valid*/ 1, 6087 /*command*/ 0, 6088 /*field*/ *len_used + page_len_offset, 6089 /*bit_valid*/ 0, 6090 /*bit*/ 0); 6091 free(ctsio->kern_data_ptr, M_CTL); 6092 ctl_done((union ctl_io *)ctsio); 6093 return (CTL_RETVAL_COMPLETE); 6094 } 6095 if (*len_left < page_index->page_len) { 6096 free(ctsio->kern_data_ptr, M_CTL); 6097 ctl_set_param_len_error(ctsio); 6098 ctl_done((union ctl_io *)ctsio); 6099 return (CTL_RETVAL_COMPLETE); 6100 } 6101 6102 /* 6103 * Run through the mode page, checking to make sure that the bits 6104 * the user changed are actually legal for him to change. 6105 */ 6106 for (i = 0; i < page_index->page_len; i++) { 6107 uint8_t *user_byte, *change_mask, *current_byte; 6108 int bad_bit; 6109 int j; 6110 6111 user_byte = (uint8_t *)page_header + i; 6112 change_mask = page_index->page_data + 6113 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6114 current_byte = page_index->page_data + 6115 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6116 6117 /* 6118 * Check to see whether the user set any bits in this byte 6119 * that he is not allowed to set. 6120 */ 6121 if ((*user_byte & ~(*change_mask)) == 6122 (*current_byte & ~(*change_mask))) 6123 continue; 6124 6125 /* 6126 * Go through bit by bit to determine which one is illegal. 6127 */ 6128 bad_bit = 0; 6129 for (j = 7; j >= 0; j--) { 6130 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6131 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6132 bad_bit = i; 6133 break; 6134 } 6135 } 6136 ctl_set_invalid_field(ctsio, 6137 /*sks_valid*/ 1, 6138 /*command*/ 0, 6139 /*field*/ *len_used + i, 6140 /*bit_valid*/ 1, 6141 /*bit*/ bad_bit); 6142 free(ctsio->kern_data_ptr, M_CTL); 6143 ctl_done((union ctl_io *)ctsio); 6144 return (CTL_RETVAL_COMPLETE); 6145 } 6146 6147 /* 6148 * Decrement these before we call the page handler, since we may 6149 * end up getting called back one way or another before the handler 6150 * returns to this context. 6151 */ 6152 *len_left -= page_index->page_len; 6153 *len_used += page_index->page_len; 6154 6155 retval = page_index->select_handler(ctsio, page_index, 6156 (uint8_t *)page_header); 6157 6158 /* 6159 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6160 * wait until this queued command completes to finish processing 6161 * the mode page. If it returns anything other than 6162 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6163 * already set the sense information, freed the data pointer, and 6164 * completed the io for us. 6165 */ 6166 if (retval != CTL_RETVAL_COMPLETE) 6167 goto bailout_no_done; 6168 6169 /* 6170 * If the initiator sent us more than one page, parse the next one. 6171 */ 6172 if (*len_left > 0) 6173 goto do_next_page; 6174 6175 ctl_set_success(ctsio); 6176 free(ctsio->kern_data_ptr, M_CTL); 6177 ctl_done((union ctl_io *)ctsio); 6178 6179 bailout_no_done: 6180 6181 return (CTL_RETVAL_COMPLETE); 6182 6183 } 6184 6185 int 6186 ctl_mode_select(struct ctl_scsiio *ctsio) 6187 { 6188 struct ctl_lun *lun = CTL_LUN(ctsio); 6189 union ctl_modepage_info *modepage_info; 6190 int bd_len, i, header_size, param_len, rtd; 6191 uint32_t initidx; 6192 6193 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6194 switch (ctsio->cdb[0]) { 6195 case MODE_SELECT_6: { 6196 struct scsi_mode_select_6 *cdb; 6197 6198 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6199 6200 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6201 param_len = cdb->length; 6202 header_size = sizeof(struct scsi_mode_header_6); 6203 break; 6204 } 6205 case MODE_SELECT_10: { 6206 struct scsi_mode_select_10 *cdb; 6207 6208 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6209 6210 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6211 param_len = scsi_2btoul(cdb->length); 6212 header_size = sizeof(struct scsi_mode_header_10); 6213 break; 6214 } 6215 default: 6216 ctl_set_invalid_opcode(ctsio); 6217 ctl_done((union ctl_io *)ctsio); 6218 return (CTL_RETVAL_COMPLETE); 6219 } 6220 6221 if (rtd) { 6222 if (param_len != 0) { 6223 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6224 /*command*/ 1, /*field*/ 0, 6225 /*bit_valid*/ 0, /*bit*/ 0); 6226 ctl_done((union ctl_io *)ctsio); 6227 return (CTL_RETVAL_COMPLETE); 6228 } 6229 6230 /* Revert to defaults. */ 6231 ctl_init_page_index(lun); 6232 mtx_lock(&lun->lun_lock); 6233 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6234 mtx_unlock(&lun->lun_lock); 6235 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6236 ctl_isc_announce_mode(lun, -1, 6237 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6238 lun->mode_pages.index[i].subpage); 6239 } 6240 ctl_set_success(ctsio); 6241 ctl_done((union ctl_io *)ctsio); 6242 return (CTL_RETVAL_COMPLETE); 6243 } 6244 6245 /* 6246 * From SPC-3: 6247 * "A parameter list length of zero indicates that the Data-Out Buffer 6248 * shall be empty. This condition shall not be considered as an error." 6249 */ 6250 if (param_len == 0) { 6251 ctl_set_success(ctsio); 6252 ctl_done((union ctl_io *)ctsio); 6253 return (CTL_RETVAL_COMPLETE); 6254 } 6255 6256 /* 6257 * Since we'll hit this the first time through, prior to 6258 * allocation, we don't need to free a data buffer here. 6259 */ 6260 if (param_len < header_size) { 6261 ctl_set_param_len_error(ctsio); 6262 ctl_done((union ctl_io *)ctsio); 6263 return (CTL_RETVAL_COMPLETE); 6264 } 6265 6266 /* 6267 * Allocate the data buffer and grab the user's data. In theory, 6268 * we shouldn't have to sanity check the parameter list length here 6269 * because the maximum size is 64K. We should be able to malloc 6270 * that much without too many problems. 6271 */ 6272 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6273 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6274 ctsio->kern_data_len = param_len; 6275 ctsio->kern_total_len = param_len; 6276 ctsio->kern_rel_offset = 0; 6277 ctsio->kern_sg_entries = 0; 6278 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6279 ctsio->be_move_done = ctl_config_move_done; 6280 ctl_datamove((union ctl_io *)ctsio); 6281 6282 return (CTL_RETVAL_COMPLETE); 6283 } 6284 6285 switch (ctsio->cdb[0]) { 6286 case MODE_SELECT_6: { 6287 struct scsi_mode_header_6 *mh6; 6288 6289 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6290 bd_len = mh6->blk_desc_len; 6291 break; 6292 } 6293 case MODE_SELECT_10: { 6294 struct scsi_mode_header_10 *mh10; 6295 6296 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6297 bd_len = scsi_2btoul(mh10->blk_desc_len); 6298 break; 6299 } 6300 default: 6301 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6302 } 6303 6304 if (param_len < (header_size + bd_len)) { 6305 free(ctsio->kern_data_ptr, M_CTL); 6306 ctl_set_param_len_error(ctsio); 6307 ctl_done((union ctl_io *)ctsio); 6308 return (CTL_RETVAL_COMPLETE); 6309 } 6310 6311 /* 6312 * Set the IO_CONT flag, so that if this I/O gets passed to 6313 * ctl_config_write_done(), it'll get passed back to 6314 * ctl_do_mode_select() for further processing, or completion if 6315 * we're all done. 6316 */ 6317 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6318 ctsio->io_cont = ctl_do_mode_select; 6319 6320 modepage_info = (union ctl_modepage_info *) 6321 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6322 memset(modepage_info, 0, sizeof(*modepage_info)); 6323 modepage_info->header.len_left = param_len - header_size - bd_len; 6324 modepage_info->header.len_used = header_size + bd_len; 6325 6326 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6327 } 6328 6329 int 6330 ctl_mode_sense(struct ctl_scsiio *ctsio) 6331 { 6332 struct ctl_lun *lun = CTL_LUN(ctsio); 6333 int pc, page_code, llba, subpage; 6334 int alloc_len, page_len, header_len, bd_len, total_len; 6335 void *block_desc; 6336 struct ctl_page_index *page_index; 6337 6338 llba = 0; 6339 6340 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6341 6342 switch (ctsio->cdb[0]) { 6343 case MODE_SENSE_6: { 6344 struct scsi_mode_sense_6 *cdb; 6345 6346 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6347 6348 header_len = sizeof(struct scsi_mode_hdr_6); 6349 if (cdb->byte2 & SMS_DBD) 6350 bd_len = 0; 6351 else 6352 bd_len = sizeof(struct scsi_mode_block_descr); 6353 header_len += bd_len; 6354 6355 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6356 page_code = cdb->page & SMS_PAGE_CODE; 6357 subpage = cdb->subpage; 6358 alloc_len = cdb->length; 6359 break; 6360 } 6361 case MODE_SENSE_10: { 6362 struct scsi_mode_sense_10 *cdb; 6363 6364 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6365 6366 header_len = sizeof(struct scsi_mode_hdr_10); 6367 if (cdb->byte2 & SMS_DBD) { 6368 bd_len = 0; 6369 } else if (lun->be_lun->lun_type == T_DIRECT) { 6370 if (cdb->byte2 & SMS10_LLBAA) { 6371 llba = 1; 6372 bd_len = sizeof(struct scsi_mode_block_descr_dlong); 6373 } else 6374 bd_len = sizeof(struct scsi_mode_block_descr_dshort); 6375 } else 6376 bd_len = sizeof(struct scsi_mode_block_descr); 6377 header_len += bd_len; 6378 6379 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6380 page_code = cdb->page & SMS_PAGE_CODE; 6381 subpage = cdb->subpage; 6382 alloc_len = scsi_2btoul(cdb->length); 6383 break; 6384 } 6385 default: 6386 ctl_set_invalid_opcode(ctsio); 6387 ctl_done((union ctl_io *)ctsio); 6388 return (CTL_RETVAL_COMPLETE); 6389 break; /* NOTREACHED */ 6390 } 6391 6392 /* 6393 * We have to make a first pass through to calculate the size of 6394 * the pages that match the user's query. Then we allocate enough 6395 * memory to hold it, and actually copy the data into the buffer. 6396 */ 6397 switch (page_code) { 6398 case SMS_ALL_PAGES_PAGE: { 6399 u_int i; 6400 6401 page_len = 0; 6402 6403 /* 6404 * At the moment, values other than 0 and 0xff here are 6405 * reserved according to SPC-3. 6406 */ 6407 if ((subpage != SMS_SUBPAGE_PAGE_0) 6408 && (subpage != SMS_SUBPAGE_ALL)) { 6409 ctl_set_invalid_field(ctsio, 6410 /*sks_valid*/ 1, 6411 /*command*/ 1, 6412 /*field*/ 3, 6413 /*bit_valid*/ 0, 6414 /*bit*/ 0); 6415 ctl_done((union ctl_io *)ctsio); 6416 return (CTL_RETVAL_COMPLETE); 6417 } 6418 6419 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6420 page_index = &lun->mode_pages.index[i]; 6421 6422 /* Make sure the page is supported for this dev type */ 6423 if (lun->be_lun->lun_type == T_DIRECT && 6424 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6425 continue; 6426 if (lun->be_lun->lun_type == T_PROCESSOR && 6427 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6428 continue; 6429 if (lun->be_lun->lun_type == T_CDROM && 6430 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6431 continue; 6432 6433 /* 6434 * We don't use this subpage if the user didn't 6435 * request all subpages. 6436 */ 6437 if ((page_index->subpage != 0) 6438 && (subpage == SMS_SUBPAGE_PAGE_0)) 6439 continue; 6440 6441 page_len += page_index->page_len; 6442 } 6443 break; 6444 } 6445 default: { 6446 u_int i; 6447 6448 page_len = 0; 6449 6450 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6451 page_index = &lun->mode_pages.index[i]; 6452 6453 /* Make sure the page is supported for this dev type */ 6454 if (lun->be_lun->lun_type == T_DIRECT && 6455 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6456 continue; 6457 if (lun->be_lun->lun_type == T_PROCESSOR && 6458 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6459 continue; 6460 if (lun->be_lun->lun_type == T_CDROM && 6461 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6462 continue; 6463 6464 /* Look for the right page code */ 6465 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6466 continue; 6467 6468 /* Look for the right subpage or the subpage wildcard*/ 6469 if ((page_index->subpage != subpage) 6470 && (subpage != SMS_SUBPAGE_ALL)) 6471 continue; 6472 6473 page_len += page_index->page_len; 6474 } 6475 6476 if (page_len == 0) { 6477 ctl_set_invalid_field(ctsio, 6478 /*sks_valid*/ 1, 6479 /*command*/ 1, 6480 /*field*/ 2, 6481 /*bit_valid*/ 1, 6482 /*bit*/ 5); 6483 ctl_done((union ctl_io *)ctsio); 6484 return (CTL_RETVAL_COMPLETE); 6485 } 6486 break; 6487 } 6488 } 6489 6490 total_len = header_len + page_len; 6491 6492 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6493 ctsio->kern_sg_entries = 0; 6494 ctsio->kern_rel_offset = 0; 6495 ctsio->kern_data_len = min(total_len, alloc_len); 6496 ctsio->kern_total_len = ctsio->kern_data_len; 6497 6498 switch (ctsio->cdb[0]) { 6499 case MODE_SENSE_6: { 6500 struct scsi_mode_hdr_6 *header; 6501 6502 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6503 6504 header->datalen = MIN(total_len - 1, 254); 6505 if (lun->be_lun->lun_type == T_DIRECT) { 6506 header->dev_specific = 0x10; /* DPOFUA */ 6507 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6508 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6509 header->dev_specific |= 0x80; /* WP */ 6510 } 6511 header->block_descr_len = bd_len; 6512 block_desc = &header[1]; 6513 break; 6514 } 6515 case MODE_SENSE_10: { 6516 struct scsi_mode_hdr_10 *header; 6517 int datalen; 6518 6519 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6520 6521 datalen = MIN(total_len - 2, 65533); 6522 scsi_ulto2b(datalen, header->datalen); 6523 if (lun->be_lun->lun_type == T_DIRECT) { 6524 header->dev_specific = 0x10; /* DPOFUA */ 6525 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6526 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6527 header->dev_specific |= 0x80; /* WP */ 6528 } 6529 if (llba) 6530 header->flags |= SMH_LONGLBA; 6531 scsi_ulto2b(bd_len, header->block_descr_len); 6532 block_desc = &header[1]; 6533 break; 6534 } 6535 default: 6536 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6537 } 6538 6539 /* 6540 * If we've got a disk, use its blocksize in the block 6541 * descriptor. Otherwise, just set it to 0. 6542 */ 6543 if (bd_len > 0) { 6544 if (lun->be_lun->lun_type == T_DIRECT) { 6545 if (llba) { 6546 struct scsi_mode_block_descr_dlong *bd = block_desc; 6547 if (lun->be_lun->maxlba != 0) 6548 scsi_u64to8b(lun->be_lun->maxlba + 1, 6549 bd->num_blocks); 6550 scsi_ulto4b(lun->be_lun->blocksize, 6551 bd->block_len); 6552 } else { 6553 struct scsi_mode_block_descr_dshort *bd = block_desc; 6554 if (lun->be_lun->maxlba != 0) 6555 scsi_ulto4b(MIN(lun->be_lun->maxlba+1, 6556 UINT32_MAX), bd->num_blocks); 6557 scsi_ulto3b(lun->be_lun->blocksize, 6558 bd->block_len); 6559 } 6560 } else { 6561 struct scsi_mode_block_descr *bd = block_desc; 6562 scsi_ulto3b(0, bd->block_len); 6563 } 6564 } 6565 6566 switch (page_code) { 6567 case SMS_ALL_PAGES_PAGE: { 6568 int i, data_used; 6569 6570 data_used = header_len; 6571 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6572 struct ctl_page_index *page_index; 6573 6574 page_index = &lun->mode_pages.index[i]; 6575 if (lun->be_lun->lun_type == T_DIRECT && 6576 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6577 continue; 6578 if (lun->be_lun->lun_type == T_PROCESSOR && 6579 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6580 continue; 6581 if (lun->be_lun->lun_type == T_CDROM && 6582 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6583 continue; 6584 6585 /* 6586 * We don't use this subpage if the user didn't 6587 * request all subpages. We already checked (above) 6588 * to make sure the user only specified a subpage 6589 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6590 */ 6591 if ((page_index->subpage != 0) 6592 && (subpage == SMS_SUBPAGE_PAGE_0)) 6593 continue; 6594 6595 /* 6596 * Call the handler, if it exists, to update the 6597 * page to the latest values. 6598 */ 6599 if (page_index->sense_handler != NULL) 6600 page_index->sense_handler(ctsio, page_index,pc); 6601 6602 memcpy(ctsio->kern_data_ptr + data_used, 6603 page_index->page_data + 6604 (page_index->page_len * pc), 6605 page_index->page_len); 6606 data_used += page_index->page_len; 6607 } 6608 break; 6609 } 6610 default: { 6611 int i, data_used; 6612 6613 data_used = header_len; 6614 6615 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6616 struct ctl_page_index *page_index; 6617 6618 page_index = &lun->mode_pages.index[i]; 6619 6620 /* Look for the right page code */ 6621 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6622 continue; 6623 6624 /* Look for the right subpage or the subpage wildcard*/ 6625 if ((page_index->subpage != subpage) 6626 && (subpage != SMS_SUBPAGE_ALL)) 6627 continue; 6628 6629 /* Make sure the page is supported for this dev type */ 6630 if (lun->be_lun->lun_type == T_DIRECT && 6631 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6632 continue; 6633 if (lun->be_lun->lun_type == T_PROCESSOR && 6634 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6635 continue; 6636 if (lun->be_lun->lun_type == T_CDROM && 6637 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6638 continue; 6639 6640 /* 6641 * Call the handler, if it exists, to update the 6642 * page to the latest values. 6643 */ 6644 if (page_index->sense_handler != NULL) 6645 page_index->sense_handler(ctsio, page_index,pc); 6646 6647 memcpy(ctsio->kern_data_ptr + data_used, 6648 page_index->page_data + 6649 (page_index->page_len * pc), 6650 page_index->page_len); 6651 data_used += page_index->page_len; 6652 } 6653 break; 6654 } 6655 } 6656 6657 ctl_set_success(ctsio); 6658 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6659 ctsio->be_move_done = ctl_config_move_done; 6660 ctl_datamove((union ctl_io *)ctsio); 6661 return (CTL_RETVAL_COMPLETE); 6662 } 6663 6664 int 6665 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio, 6666 struct ctl_page_index *page_index, 6667 int pc) 6668 { 6669 struct ctl_lun *lun = CTL_LUN(ctsio); 6670 struct scsi_log_temperature *data; 6671 const char *value; 6672 6673 data = (struct scsi_log_temperature *)page_index->page_data; 6674 6675 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code); 6676 data->hdr.param_control = SLP_LBIN; 6677 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6678 sizeof(struct scsi_log_param_header); 6679 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6680 NULL)) != NULL) 6681 data->temperature = strtol(value, NULL, 0); 6682 else 6683 data->temperature = 0xff; 6684 data++; 6685 6686 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code); 6687 data->hdr.param_control = SLP_LBIN; 6688 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6689 sizeof(struct scsi_log_param_header); 6690 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature", 6691 NULL)) != NULL) 6692 data->temperature = strtol(value, NULL, 0); 6693 else 6694 data->temperature = 0xff; 6695 return (0); 6696 } 6697 6698 int 6699 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6700 struct ctl_page_index *page_index, 6701 int pc) 6702 { 6703 struct ctl_lun *lun = CTL_LUN(ctsio); 6704 struct scsi_log_param_header *phdr; 6705 uint8_t *data; 6706 uint64_t val; 6707 6708 data = page_index->page_data; 6709 6710 if (lun->backend->lun_attr != NULL && 6711 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6712 != UINT64_MAX) { 6713 phdr = (struct scsi_log_param_header *)data; 6714 scsi_ulto2b(0x0001, phdr->param_code); 6715 phdr->param_control = SLP_LBIN | SLP_LP; 6716 phdr->param_len = 8; 6717 data = (uint8_t *)(phdr + 1); 6718 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6719 data[4] = 0x02; /* per-pool */ 6720 data += phdr->param_len; 6721 } 6722 6723 if (lun->backend->lun_attr != NULL && 6724 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6725 != UINT64_MAX) { 6726 phdr = (struct scsi_log_param_header *)data; 6727 scsi_ulto2b(0x0002, phdr->param_code); 6728 phdr->param_control = SLP_LBIN | SLP_LP; 6729 phdr->param_len = 8; 6730 data = (uint8_t *)(phdr + 1); 6731 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6732 data[4] = 0x01; /* per-LUN */ 6733 data += phdr->param_len; 6734 } 6735 6736 if (lun->backend->lun_attr != NULL && 6737 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6738 != UINT64_MAX) { 6739 phdr = (struct scsi_log_param_header *)data; 6740 scsi_ulto2b(0x00f1, phdr->param_code); 6741 phdr->param_control = SLP_LBIN | SLP_LP; 6742 phdr->param_len = 8; 6743 data = (uint8_t *)(phdr + 1); 6744 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6745 data[4] = 0x02; /* per-pool */ 6746 data += phdr->param_len; 6747 } 6748 6749 if (lun->backend->lun_attr != NULL && 6750 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6751 != UINT64_MAX) { 6752 phdr = (struct scsi_log_param_header *)data; 6753 scsi_ulto2b(0x00f2, phdr->param_code); 6754 phdr->param_control = SLP_LBIN | SLP_LP; 6755 phdr->param_len = 8; 6756 data = (uint8_t *)(phdr + 1); 6757 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6758 data[4] = 0x02; /* per-pool */ 6759 data += phdr->param_len; 6760 } 6761 6762 page_index->page_len = data - page_index->page_data; 6763 return (0); 6764 } 6765 6766 int 6767 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6768 struct ctl_page_index *page_index, 6769 int pc) 6770 { 6771 struct ctl_lun *lun = CTL_LUN(ctsio); 6772 struct stat_page *data; 6773 struct bintime *t; 6774 6775 data = (struct stat_page *)page_index->page_data; 6776 6777 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6778 data->sap.hdr.param_control = SLP_LBIN; 6779 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6780 sizeof(struct scsi_log_param_header); 6781 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6782 data->sap.read_num); 6783 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6784 data->sap.write_num); 6785 if (lun->be_lun->blocksize > 0) { 6786 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6787 lun->be_lun->blocksize, data->sap.recvieved_lba); 6788 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6789 lun->be_lun->blocksize, data->sap.transmitted_lba); 6790 } 6791 t = &lun->stats.time[CTL_STATS_READ]; 6792 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6793 data->sap.read_int); 6794 t = &lun->stats.time[CTL_STATS_WRITE]; 6795 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6796 data->sap.write_int); 6797 scsi_u64to8b(0, data->sap.weighted_num); 6798 scsi_u64to8b(0, data->sap.weighted_int); 6799 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6800 data->it.hdr.param_control = SLP_LBIN; 6801 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6802 sizeof(struct scsi_log_param_header); 6803 #ifdef CTL_TIME_IO 6804 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6805 #endif 6806 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6807 data->it.hdr.param_control = SLP_LBIN; 6808 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6809 sizeof(struct scsi_log_param_header); 6810 scsi_ulto4b(3, data->ti.exponent); 6811 scsi_ulto4b(1, data->ti.integer); 6812 return (0); 6813 } 6814 6815 int 6816 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6817 struct ctl_page_index *page_index, 6818 int pc) 6819 { 6820 struct ctl_lun *lun = CTL_LUN(ctsio); 6821 struct scsi_log_informational_exceptions *data; 6822 const char *value; 6823 6824 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6825 6826 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6827 data->hdr.param_control = SLP_LBIN; 6828 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6829 sizeof(struct scsi_log_param_header); 6830 data->ie_asc = lun->ie_asc; 6831 data->ie_ascq = lun->ie_ascq; 6832 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6833 NULL)) != NULL) 6834 data->temperature = strtol(value, NULL, 0); 6835 else 6836 data->temperature = 0xff; 6837 return (0); 6838 } 6839 6840 int 6841 ctl_log_sense(struct ctl_scsiio *ctsio) 6842 { 6843 struct ctl_lun *lun = CTL_LUN(ctsio); 6844 int i, pc, page_code, subpage; 6845 int alloc_len, total_len; 6846 struct ctl_page_index *page_index; 6847 struct scsi_log_sense *cdb; 6848 struct scsi_log_header *header; 6849 6850 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6851 6852 cdb = (struct scsi_log_sense *)ctsio->cdb; 6853 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6854 page_code = cdb->page & SLS_PAGE_CODE; 6855 subpage = cdb->subpage; 6856 alloc_len = scsi_2btoul(cdb->length); 6857 6858 page_index = NULL; 6859 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6860 page_index = &lun->log_pages.index[i]; 6861 6862 /* Look for the right page code */ 6863 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6864 continue; 6865 6866 /* Look for the right subpage or the subpage wildcard*/ 6867 if (page_index->subpage != subpage) 6868 continue; 6869 6870 break; 6871 } 6872 if (i >= CTL_NUM_LOG_PAGES) { 6873 ctl_set_invalid_field(ctsio, 6874 /*sks_valid*/ 1, 6875 /*command*/ 1, 6876 /*field*/ 2, 6877 /*bit_valid*/ 0, 6878 /*bit*/ 0); 6879 ctl_done((union ctl_io *)ctsio); 6880 return (CTL_RETVAL_COMPLETE); 6881 } 6882 6883 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6884 6885 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6886 ctsio->kern_sg_entries = 0; 6887 ctsio->kern_rel_offset = 0; 6888 ctsio->kern_data_len = min(total_len, alloc_len); 6889 ctsio->kern_total_len = ctsio->kern_data_len; 6890 6891 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6892 header->page = page_index->page_code; 6893 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6894 header->page |= SL_DS; 6895 if (page_index->subpage) { 6896 header->page |= SL_SPF; 6897 header->subpage = page_index->subpage; 6898 } 6899 scsi_ulto2b(page_index->page_len, header->datalen); 6900 6901 /* 6902 * Call the handler, if it exists, to update the 6903 * page to the latest values. 6904 */ 6905 if (page_index->sense_handler != NULL) 6906 page_index->sense_handler(ctsio, page_index, pc); 6907 6908 memcpy(header + 1, page_index->page_data, page_index->page_len); 6909 6910 ctl_set_success(ctsio); 6911 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6912 ctsio->be_move_done = ctl_config_move_done; 6913 ctl_datamove((union ctl_io *)ctsio); 6914 return (CTL_RETVAL_COMPLETE); 6915 } 6916 6917 int 6918 ctl_read_capacity(struct ctl_scsiio *ctsio) 6919 { 6920 struct ctl_lun *lun = CTL_LUN(ctsio); 6921 struct scsi_read_capacity *cdb; 6922 struct scsi_read_capacity_data *data; 6923 uint32_t lba; 6924 6925 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6926 6927 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6928 6929 lba = scsi_4btoul(cdb->addr); 6930 if (((cdb->pmi & SRC_PMI) == 0) 6931 && (lba != 0)) { 6932 ctl_set_invalid_field(/*ctsio*/ ctsio, 6933 /*sks_valid*/ 1, 6934 /*command*/ 1, 6935 /*field*/ 2, 6936 /*bit_valid*/ 0, 6937 /*bit*/ 0); 6938 ctl_done((union ctl_io *)ctsio); 6939 return (CTL_RETVAL_COMPLETE); 6940 } 6941 6942 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6943 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6944 ctsio->kern_data_len = sizeof(*data); 6945 ctsio->kern_total_len = sizeof(*data); 6946 ctsio->kern_rel_offset = 0; 6947 ctsio->kern_sg_entries = 0; 6948 6949 /* 6950 * If the maximum LBA is greater than 0xfffffffe, the user must 6951 * issue a SERVICE ACTION IN (16) command, with the read capacity 6952 * serivce action set. 6953 */ 6954 if (lun->be_lun->maxlba > 0xfffffffe) 6955 scsi_ulto4b(0xffffffff, data->addr); 6956 else 6957 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6958 6959 /* 6960 * XXX KDM this may not be 512 bytes... 6961 */ 6962 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6963 6964 ctl_set_success(ctsio); 6965 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6966 ctsio->be_move_done = ctl_config_move_done; 6967 ctl_datamove((union ctl_io *)ctsio); 6968 return (CTL_RETVAL_COMPLETE); 6969 } 6970 6971 int 6972 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6973 { 6974 struct ctl_lun *lun = CTL_LUN(ctsio); 6975 struct scsi_read_capacity_16 *cdb; 6976 struct scsi_read_capacity_data_long *data; 6977 uint64_t lba; 6978 uint32_t alloc_len; 6979 6980 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6981 6982 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6983 6984 alloc_len = scsi_4btoul(cdb->alloc_len); 6985 lba = scsi_8btou64(cdb->addr); 6986 6987 if ((cdb->reladr & SRC16_PMI) 6988 && (lba != 0)) { 6989 ctl_set_invalid_field(/*ctsio*/ ctsio, 6990 /*sks_valid*/ 1, 6991 /*command*/ 1, 6992 /*field*/ 2, 6993 /*bit_valid*/ 0, 6994 /*bit*/ 0); 6995 ctl_done((union ctl_io *)ctsio); 6996 return (CTL_RETVAL_COMPLETE); 6997 } 6998 6999 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7000 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7001 ctsio->kern_rel_offset = 0; 7002 ctsio->kern_sg_entries = 0; 7003 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 7004 ctsio->kern_total_len = ctsio->kern_data_len; 7005 7006 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7007 /* XXX KDM this may not be 512 bytes... */ 7008 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7009 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7010 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7011 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7012 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7013 7014 ctl_set_success(ctsio); 7015 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7016 ctsio->be_move_done = ctl_config_move_done; 7017 ctl_datamove((union ctl_io *)ctsio); 7018 return (CTL_RETVAL_COMPLETE); 7019 } 7020 7021 int 7022 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7023 { 7024 struct ctl_lun *lun = CTL_LUN(ctsio); 7025 struct scsi_get_lba_status *cdb; 7026 struct scsi_get_lba_status_data *data; 7027 struct ctl_lba_len_flags *lbalen; 7028 uint64_t lba; 7029 uint32_t alloc_len, total_len; 7030 int retval; 7031 7032 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7033 7034 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7035 lba = scsi_8btou64(cdb->addr); 7036 alloc_len = scsi_4btoul(cdb->alloc_len); 7037 7038 if (lba > lun->be_lun->maxlba) { 7039 ctl_set_lba_out_of_range(ctsio, lba); 7040 ctl_done((union ctl_io *)ctsio); 7041 return (CTL_RETVAL_COMPLETE); 7042 } 7043 7044 total_len = sizeof(*data) + sizeof(data->descr[0]); 7045 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7046 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7047 ctsio->kern_rel_offset = 0; 7048 ctsio->kern_sg_entries = 0; 7049 ctsio->kern_data_len = min(total_len, alloc_len); 7050 ctsio->kern_total_len = ctsio->kern_data_len; 7051 7052 /* Fill dummy data in case backend can't tell anything. */ 7053 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7054 scsi_u64to8b(lba, data->descr[0].addr); 7055 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7056 data->descr[0].length); 7057 data->descr[0].status = 0; /* Mapped or unknown. */ 7058 7059 ctl_set_success(ctsio); 7060 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7061 ctsio->be_move_done = ctl_config_move_done; 7062 7063 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7064 lbalen->lba = lba; 7065 lbalen->len = total_len; 7066 lbalen->flags = 0; 7067 retval = lun->backend->config_read((union ctl_io *)ctsio); 7068 return (retval); 7069 } 7070 7071 int 7072 ctl_read_defect(struct ctl_scsiio *ctsio) 7073 { 7074 struct scsi_read_defect_data_10 *ccb10; 7075 struct scsi_read_defect_data_12 *ccb12; 7076 struct scsi_read_defect_data_hdr_10 *data10; 7077 struct scsi_read_defect_data_hdr_12 *data12; 7078 uint32_t alloc_len, data_len; 7079 uint8_t format; 7080 7081 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7082 7083 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7084 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7085 format = ccb10->format; 7086 alloc_len = scsi_2btoul(ccb10->alloc_length); 7087 data_len = sizeof(*data10); 7088 } else { 7089 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7090 format = ccb12->format; 7091 alloc_len = scsi_4btoul(ccb12->alloc_length); 7092 data_len = sizeof(*data12); 7093 } 7094 if (alloc_len == 0) { 7095 ctl_set_success(ctsio); 7096 ctl_done((union ctl_io *)ctsio); 7097 return (CTL_RETVAL_COMPLETE); 7098 } 7099 7100 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7101 ctsio->kern_rel_offset = 0; 7102 ctsio->kern_sg_entries = 0; 7103 ctsio->kern_data_len = min(data_len, alloc_len); 7104 ctsio->kern_total_len = ctsio->kern_data_len; 7105 7106 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7107 data10 = (struct scsi_read_defect_data_hdr_10 *) 7108 ctsio->kern_data_ptr; 7109 data10->format = format; 7110 scsi_ulto2b(0, data10->length); 7111 } else { 7112 data12 = (struct scsi_read_defect_data_hdr_12 *) 7113 ctsio->kern_data_ptr; 7114 data12->format = format; 7115 scsi_ulto2b(0, data12->generation); 7116 scsi_ulto4b(0, data12->length); 7117 } 7118 7119 ctl_set_success(ctsio); 7120 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7121 ctsio->be_move_done = ctl_config_move_done; 7122 ctl_datamove((union ctl_io *)ctsio); 7123 return (CTL_RETVAL_COMPLETE); 7124 } 7125 7126 int 7127 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7128 { 7129 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7130 struct ctl_lun *lun = CTL_LUN(ctsio); 7131 struct scsi_maintenance_in *cdb; 7132 int retval; 7133 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7134 int num_ha_groups, num_target_ports, shared_group; 7135 struct ctl_port *port; 7136 struct scsi_target_group_data *rtg_ptr; 7137 struct scsi_target_group_data_extended *rtg_ext_ptr; 7138 struct scsi_target_port_group_descriptor *tpg_desc; 7139 7140 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7141 7142 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7143 retval = CTL_RETVAL_COMPLETE; 7144 7145 switch (cdb->byte2 & STG_PDF_MASK) { 7146 case STG_PDF_LENGTH: 7147 ext = 0; 7148 break; 7149 case STG_PDF_EXTENDED: 7150 ext = 1; 7151 break; 7152 default: 7153 ctl_set_invalid_field(/*ctsio*/ ctsio, 7154 /*sks_valid*/ 1, 7155 /*command*/ 1, 7156 /*field*/ 2, 7157 /*bit_valid*/ 1, 7158 /*bit*/ 5); 7159 ctl_done((union ctl_io *)ctsio); 7160 return(retval); 7161 } 7162 7163 num_target_ports = 0; 7164 shared_group = (softc->is_single != 0); 7165 mtx_lock(&softc->ctl_lock); 7166 STAILQ_FOREACH(port, &softc->port_list, links) { 7167 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7168 continue; 7169 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7170 continue; 7171 num_target_ports++; 7172 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7173 shared_group = 1; 7174 } 7175 mtx_unlock(&softc->ctl_lock); 7176 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7177 7178 if (ext) 7179 total_len = sizeof(struct scsi_target_group_data_extended); 7180 else 7181 total_len = sizeof(struct scsi_target_group_data); 7182 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7183 (shared_group + num_ha_groups) + 7184 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7185 7186 alloc_len = scsi_4btoul(cdb->length); 7187 7188 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7189 ctsio->kern_sg_entries = 0; 7190 ctsio->kern_rel_offset = 0; 7191 ctsio->kern_data_len = min(total_len, alloc_len); 7192 ctsio->kern_total_len = ctsio->kern_data_len; 7193 7194 if (ext) { 7195 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7196 ctsio->kern_data_ptr; 7197 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7198 rtg_ext_ptr->format_type = 0x10; 7199 rtg_ext_ptr->implicit_transition_time = 0; 7200 tpg_desc = &rtg_ext_ptr->groups[0]; 7201 } else { 7202 rtg_ptr = (struct scsi_target_group_data *) 7203 ctsio->kern_data_ptr; 7204 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7205 tpg_desc = &rtg_ptr->groups[0]; 7206 } 7207 7208 mtx_lock(&softc->ctl_lock); 7209 pg = softc->port_min / softc->port_cnt; 7210 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7211 /* Some shelf is known to be primary. */ 7212 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7213 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7214 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7215 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7216 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7217 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7218 else 7219 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7220 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7221 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7222 } else { 7223 ts = os; 7224 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7225 } 7226 } else { 7227 /* No known primary shelf. */ 7228 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7229 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7230 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7231 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7232 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7233 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7234 } else { 7235 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7236 } 7237 } 7238 if (shared_group) { 7239 tpg_desc->pref_state = ts; 7240 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7241 TPG_U_SUP | TPG_T_SUP; 7242 scsi_ulto2b(1, tpg_desc->target_port_group); 7243 tpg_desc->status = TPG_IMPLICIT; 7244 pc = 0; 7245 STAILQ_FOREACH(port, &softc->port_list, links) { 7246 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7247 continue; 7248 if (!softc->is_single && 7249 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7250 continue; 7251 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7252 continue; 7253 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7254 relative_target_port_identifier); 7255 pc++; 7256 } 7257 tpg_desc->target_port_count = pc; 7258 tpg_desc = (struct scsi_target_port_group_descriptor *) 7259 &tpg_desc->descriptors[pc]; 7260 } 7261 for (g = 0; g < num_ha_groups; g++) { 7262 tpg_desc->pref_state = (g == pg) ? ts : os; 7263 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7264 TPG_U_SUP | TPG_T_SUP; 7265 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7266 tpg_desc->status = TPG_IMPLICIT; 7267 pc = 0; 7268 STAILQ_FOREACH(port, &softc->port_list, links) { 7269 if (port->targ_port < g * softc->port_cnt || 7270 port->targ_port >= (g + 1) * softc->port_cnt) 7271 continue; 7272 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7273 continue; 7274 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7275 continue; 7276 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7277 continue; 7278 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7279 relative_target_port_identifier); 7280 pc++; 7281 } 7282 tpg_desc->target_port_count = pc; 7283 tpg_desc = (struct scsi_target_port_group_descriptor *) 7284 &tpg_desc->descriptors[pc]; 7285 } 7286 mtx_unlock(&softc->ctl_lock); 7287 7288 ctl_set_success(ctsio); 7289 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7290 ctsio->be_move_done = ctl_config_move_done; 7291 ctl_datamove((union ctl_io *)ctsio); 7292 return(retval); 7293 } 7294 7295 int 7296 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7297 { 7298 struct ctl_lun *lun = CTL_LUN(ctsio); 7299 struct scsi_report_supported_opcodes *cdb; 7300 const struct ctl_cmd_entry *entry, *sentry; 7301 struct scsi_report_supported_opcodes_all *all; 7302 struct scsi_report_supported_opcodes_descr *descr; 7303 struct scsi_report_supported_opcodes_one *one; 7304 int retval; 7305 int alloc_len, total_len; 7306 int opcode, service_action, i, j, num; 7307 7308 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7309 7310 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7311 retval = CTL_RETVAL_COMPLETE; 7312 7313 opcode = cdb->requested_opcode; 7314 service_action = scsi_2btoul(cdb->requested_service_action); 7315 switch (cdb->options & RSO_OPTIONS_MASK) { 7316 case RSO_OPTIONS_ALL: 7317 num = 0; 7318 for (i = 0; i < 256; i++) { 7319 entry = &ctl_cmd_table[i]; 7320 if (entry->flags & CTL_CMD_FLAG_SA5) { 7321 for (j = 0; j < 32; j++) { 7322 sentry = &((const struct ctl_cmd_entry *) 7323 entry->execute)[j]; 7324 if (ctl_cmd_applicable( 7325 lun->be_lun->lun_type, sentry)) 7326 num++; 7327 } 7328 } else { 7329 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7330 entry)) 7331 num++; 7332 } 7333 } 7334 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7335 num * sizeof(struct scsi_report_supported_opcodes_descr); 7336 break; 7337 case RSO_OPTIONS_OC: 7338 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7339 ctl_set_invalid_field(/*ctsio*/ ctsio, 7340 /*sks_valid*/ 1, 7341 /*command*/ 1, 7342 /*field*/ 2, 7343 /*bit_valid*/ 1, 7344 /*bit*/ 2); 7345 ctl_done((union ctl_io *)ctsio); 7346 return (CTL_RETVAL_COMPLETE); 7347 } 7348 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7349 break; 7350 case RSO_OPTIONS_OC_SA: 7351 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7352 service_action >= 32) { 7353 ctl_set_invalid_field(/*ctsio*/ ctsio, 7354 /*sks_valid*/ 1, 7355 /*command*/ 1, 7356 /*field*/ 2, 7357 /*bit_valid*/ 1, 7358 /*bit*/ 2); 7359 ctl_done((union ctl_io *)ctsio); 7360 return (CTL_RETVAL_COMPLETE); 7361 } 7362 /* FALLTHROUGH */ 7363 case RSO_OPTIONS_OC_ASA: 7364 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7365 break; 7366 default: 7367 ctl_set_invalid_field(/*ctsio*/ ctsio, 7368 /*sks_valid*/ 1, 7369 /*command*/ 1, 7370 /*field*/ 2, 7371 /*bit_valid*/ 1, 7372 /*bit*/ 2); 7373 ctl_done((union ctl_io *)ctsio); 7374 return (CTL_RETVAL_COMPLETE); 7375 } 7376 7377 alloc_len = scsi_4btoul(cdb->length); 7378 7379 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7380 ctsio->kern_sg_entries = 0; 7381 ctsio->kern_rel_offset = 0; 7382 ctsio->kern_data_len = min(total_len, alloc_len); 7383 ctsio->kern_total_len = ctsio->kern_data_len; 7384 7385 switch (cdb->options & RSO_OPTIONS_MASK) { 7386 case RSO_OPTIONS_ALL: 7387 all = (struct scsi_report_supported_opcodes_all *) 7388 ctsio->kern_data_ptr; 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 continue; 7399 descr = &all->descr[num++]; 7400 descr->opcode = i; 7401 scsi_ulto2b(j, descr->service_action); 7402 descr->flags = RSO_SERVACTV; 7403 scsi_ulto2b(sentry->length, 7404 descr->cdb_length); 7405 } 7406 } else { 7407 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7408 entry)) 7409 continue; 7410 descr = &all->descr[num++]; 7411 descr->opcode = i; 7412 scsi_ulto2b(0, descr->service_action); 7413 descr->flags = 0; 7414 scsi_ulto2b(entry->length, descr->cdb_length); 7415 } 7416 } 7417 scsi_ulto4b( 7418 num * sizeof(struct scsi_report_supported_opcodes_descr), 7419 all->length); 7420 break; 7421 case RSO_OPTIONS_OC: 7422 one = (struct scsi_report_supported_opcodes_one *) 7423 ctsio->kern_data_ptr; 7424 entry = &ctl_cmd_table[opcode]; 7425 goto fill_one; 7426 case RSO_OPTIONS_OC_SA: 7427 one = (struct scsi_report_supported_opcodes_one *) 7428 ctsio->kern_data_ptr; 7429 entry = &ctl_cmd_table[opcode]; 7430 entry = &((const struct ctl_cmd_entry *) 7431 entry->execute)[service_action]; 7432 fill_one: 7433 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7434 one->support = 3; 7435 scsi_ulto2b(entry->length, one->cdb_length); 7436 one->cdb_usage[0] = opcode; 7437 memcpy(&one->cdb_usage[1], entry->usage, 7438 entry->length - 1); 7439 } else 7440 one->support = 1; 7441 break; 7442 case RSO_OPTIONS_OC_ASA: 7443 one = (struct scsi_report_supported_opcodes_one *) 7444 ctsio->kern_data_ptr; 7445 entry = &ctl_cmd_table[opcode]; 7446 if (entry->flags & CTL_CMD_FLAG_SA5) { 7447 entry = &((const struct ctl_cmd_entry *) 7448 entry->execute)[service_action]; 7449 } else if (service_action != 0) { 7450 one->support = 1; 7451 break; 7452 } 7453 goto fill_one; 7454 } 7455 7456 ctl_set_success(ctsio); 7457 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7458 ctsio->be_move_done = ctl_config_move_done; 7459 ctl_datamove((union ctl_io *)ctsio); 7460 return(retval); 7461 } 7462 7463 int 7464 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7465 { 7466 struct scsi_report_supported_tmf *cdb; 7467 struct scsi_report_supported_tmf_ext_data *data; 7468 int retval; 7469 int alloc_len, total_len; 7470 7471 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7472 7473 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7474 7475 retval = CTL_RETVAL_COMPLETE; 7476 7477 if (cdb->options & RST_REPD) 7478 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7479 else 7480 total_len = sizeof(struct scsi_report_supported_tmf_data); 7481 alloc_len = scsi_4btoul(cdb->length); 7482 7483 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7484 ctsio->kern_sg_entries = 0; 7485 ctsio->kern_rel_offset = 0; 7486 ctsio->kern_data_len = min(total_len, alloc_len); 7487 ctsio->kern_total_len = ctsio->kern_data_len; 7488 7489 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7490 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7491 RST_TRS; 7492 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7493 data->length = total_len - 4; 7494 7495 ctl_set_success(ctsio); 7496 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7497 ctsio->be_move_done = ctl_config_move_done; 7498 ctl_datamove((union ctl_io *)ctsio); 7499 return (retval); 7500 } 7501 7502 int 7503 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7504 { 7505 struct scsi_report_timestamp *cdb; 7506 struct scsi_report_timestamp_data *data; 7507 struct timeval tv; 7508 int64_t timestamp; 7509 int retval; 7510 int alloc_len, total_len; 7511 7512 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7513 7514 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7515 7516 retval = CTL_RETVAL_COMPLETE; 7517 7518 total_len = sizeof(struct scsi_report_timestamp_data); 7519 alloc_len = scsi_4btoul(cdb->length); 7520 7521 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7522 ctsio->kern_sg_entries = 0; 7523 ctsio->kern_rel_offset = 0; 7524 ctsio->kern_data_len = min(total_len, alloc_len); 7525 ctsio->kern_total_len = ctsio->kern_data_len; 7526 7527 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7528 scsi_ulto2b(sizeof(*data) - 2, data->length); 7529 data->origin = RTS_ORIG_OUTSIDE; 7530 getmicrotime(&tv); 7531 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7532 scsi_ulto4b(timestamp >> 16, data->timestamp); 7533 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7534 7535 ctl_set_success(ctsio); 7536 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7537 ctsio->be_move_done = ctl_config_move_done; 7538 ctl_datamove((union ctl_io *)ctsio); 7539 return (retval); 7540 } 7541 7542 int 7543 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7544 { 7545 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7546 struct ctl_lun *lun = CTL_LUN(ctsio); 7547 struct scsi_per_res_in *cdb; 7548 int alloc_len, total_len = 0; 7549 /* struct scsi_per_res_in_rsrv in_data; */ 7550 uint64_t key; 7551 7552 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7553 7554 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7555 7556 alloc_len = scsi_2btoul(cdb->length); 7557 7558 retry: 7559 mtx_lock(&lun->lun_lock); 7560 switch (cdb->action) { 7561 case SPRI_RK: /* read keys */ 7562 total_len = sizeof(struct scsi_per_res_in_keys) + 7563 lun->pr_key_count * 7564 sizeof(struct scsi_per_res_key); 7565 break; 7566 case SPRI_RR: /* read reservation */ 7567 if (lun->flags & CTL_LUN_PR_RESERVED) 7568 total_len = sizeof(struct scsi_per_res_in_rsrv); 7569 else 7570 total_len = sizeof(struct scsi_per_res_in_header); 7571 break; 7572 case SPRI_RC: /* report capabilities */ 7573 total_len = sizeof(struct scsi_per_res_cap); 7574 break; 7575 case SPRI_RS: /* read full status */ 7576 total_len = sizeof(struct scsi_per_res_in_header) + 7577 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7578 lun->pr_key_count; 7579 break; 7580 default: 7581 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7582 } 7583 mtx_unlock(&lun->lun_lock); 7584 7585 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7586 ctsio->kern_rel_offset = 0; 7587 ctsio->kern_sg_entries = 0; 7588 ctsio->kern_data_len = min(total_len, alloc_len); 7589 ctsio->kern_total_len = ctsio->kern_data_len; 7590 7591 mtx_lock(&lun->lun_lock); 7592 switch (cdb->action) { 7593 case SPRI_RK: { // read keys 7594 struct scsi_per_res_in_keys *res_keys; 7595 int i, key_count; 7596 7597 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7598 7599 /* 7600 * We had to drop the lock to allocate our buffer, which 7601 * leaves time for someone to come in with another 7602 * persistent reservation. (That is unlikely, though, 7603 * since this should be the only persistent reservation 7604 * command active right now.) 7605 */ 7606 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7607 (lun->pr_key_count * 7608 sizeof(struct scsi_per_res_key)))){ 7609 mtx_unlock(&lun->lun_lock); 7610 free(ctsio->kern_data_ptr, M_CTL); 7611 printf("%s: reservation length changed, retrying\n", 7612 __func__); 7613 goto retry; 7614 } 7615 7616 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7617 7618 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7619 lun->pr_key_count, res_keys->header.length); 7620 7621 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7622 if ((key = ctl_get_prkey(lun, i)) == 0) 7623 continue; 7624 7625 /* 7626 * We used lun->pr_key_count to calculate the 7627 * size to allocate. If it turns out the number of 7628 * initiators with the registered flag set is 7629 * larger than that (i.e. they haven't been kept in 7630 * sync), we've got a problem. 7631 */ 7632 if (key_count >= lun->pr_key_count) { 7633 key_count++; 7634 continue; 7635 } 7636 scsi_u64to8b(key, res_keys->keys[key_count].key); 7637 key_count++; 7638 } 7639 break; 7640 } 7641 case SPRI_RR: { // read reservation 7642 struct scsi_per_res_in_rsrv *res; 7643 int tmp_len, header_only; 7644 7645 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7646 7647 scsi_ulto4b(lun->pr_generation, res->header.generation); 7648 7649 if (lun->flags & CTL_LUN_PR_RESERVED) 7650 { 7651 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7652 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7653 res->header.length); 7654 header_only = 0; 7655 } else { 7656 tmp_len = sizeof(struct scsi_per_res_in_header); 7657 scsi_ulto4b(0, res->header.length); 7658 header_only = 1; 7659 } 7660 7661 /* 7662 * We had to drop the lock to allocate our buffer, which 7663 * leaves time for someone to come in with another 7664 * persistent reservation. (That is unlikely, though, 7665 * since this should be the only persistent reservation 7666 * command active right now.) 7667 */ 7668 if (tmp_len != total_len) { 7669 mtx_unlock(&lun->lun_lock); 7670 free(ctsio->kern_data_ptr, M_CTL); 7671 printf("%s: reservation status changed, retrying\n", 7672 __func__); 7673 goto retry; 7674 } 7675 7676 /* 7677 * No reservation held, so we're done. 7678 */ 7679 if (header_only != 0) 7680 break; 7681 7682 /* 7683 * If the registration is an All Registrants type, the key 7684 * is 0, since it doesn't really matter. 7685 */ 7686 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7687 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7688 res->data.reservation); 7689 } 7690 res->data.scopetype = lun->pr_res_type; 7691 break; 7692 } 7693 case SPRI_RC: //report capabilities 7694 { 7695 struct scsi_per_res_cap *res_cap; 7696 uint16_t type_mask; 7697 7698 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7699 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7700 res_cap->flags1 = SPRI_CRH; 7701 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7702 type_mask = SPRI_TM_WR_EX_AR | 7703 SPRI_TM_EX_AC_RO | 7704 SPRI_TM_WR_EX_RO | 7705 SPRI_TM_EX_AC | 7706 SPRI_TM_WR_EX | 7707 SPRI_TM_EX_AC_AR; 7708 scsi_ulto2b(type_mask, res_cap->type_mask); 7709 break; 7710 } 7711 case SPRI_RS: { // read full status 7712 struct scsi_per_res_in_full *res_status; 7713 struct scsi_per_res_in_full_desc *res_desc; 7714 struct ctl_port *port; 7715 int i, len; 7716 7717 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7718 7719 /* 7720 * We had to drop the lock to allocate our buffer, which 7721 * leaves time for someone to come in with another 7722 * persistent reservation. (That is unlikely, though, 7723 * since this should be the only persistent reservation 7724 * command active right now.) 7725 */ 7726 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7727 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7728 lun->pr_key_count)){ 7729 mtx_unlock(&lun->lun_lock); 7730 free(ctsio->kern_data_ptr, M_CTL); 7731 printf("%s: reservation length changed, retrying\n", 7732 __func__); 7733 goto retry; 7734 } 7735 7736 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7737 7738 res_desc = &res_status->desc[0]; 7739 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7740 if ((key = ctl_get_prkey(lun, i)) == 0) 7741 continue; 7742 7743 scsi_u64to8b(key, res_desc->res_key.key); 7744 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7745 (lun->pr_res_idx == i || 7746 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7747 res_desc->flags = SPRI_FULL_R_HOLDER; 7748 res_desc->scopetype = lun->pr_res_type; 7749 } 7750 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7751 res_desc->rel_trgt_port_id); 7752 len = 0; 7753 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7754 if (port != NULL) 7755 len = ctl_create_iid(port, 7756 i % CTL_MAX_INIT_PER_PORT, 7757 res_desc->transport_id); 7758 scsi_ulto4b(len, res_desc->additional_length); 7759 res_desc = (struct scsi_per_res_in_full_desc *) 7760 &res_desc->transport_id[len]; 7761 } 7762 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7763 res_status->header.length); 7764 break; 7765 } 7766 default: 7767 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7768 } 7769 mtx_unlock(&lun->lun_lock); 7770 7771 ctl_set_success(ctsio); 7772 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7773 ctsio->be_move_done = ctl_config_move_done; 7774 ctl_datamove((union ctl_io *)ctsio); 7775 return (CTL_RETVAL_COMPLETE); 7776 } 7777 7778 /* 7779 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7780 * it should return. 7781 */ 7782 static int 7783 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7784 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7785 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7786 struct scsi_per_res_out_parms* param) 7787 { 7788 union ctl_ha_msg persis_io; 7789 int i; 7790 7791 mtx_lock(&lun->lun_lock); 7792 if (sa_res_key == 0) { 7793 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7794 /* validate scope and type */ 7795 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7796 SPR_LU_SCOPE) { 7797 mtx_unlock(&lun->lun_lock); 7798 ctl_set_invalid_field(/*ctsio*/ ctsio, 7799 /*sks_valid*/ 1, 7800 /*command*/ 1, 7801 /*field*/ 2, 7802 /*bit_valid*/ 1, 7803 /*bit*/ 4); 7804 ctl_done((union ctl_io *)ctsio); 7805 return (1); 7806 } 7807 7808 if (type>8 || type==2 || type==4 || type==0) { 7809 mtx_unlock(&lun->lun_lock); 7810 ctl_set_invalid_field(/*ctsio*/ ctsio, 7811 /*sks_valid*/ 1, 7812 /*command*/ 1, 7813 /*field*/ 2, 7814 /*bit_valid*/ 1, 7815 /*bit*/ 0); 7816 ctl_done((union ctl_io *)ctsio); 7817 return (1); 7818 } 7819 7820 /* 7821 * Unregister everybody else and build UA for 7822 * them 7823 */ 7824 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7825 if (i == residx || ctl_get_prkey(lun, i) == 0) 7826 continue; 7827 7828 ctl_clr_prkey(lun, i); 7829 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7830 } 7831 lun->pr_key_count = 1; 7832 lun->pr_res_type = type; 7833 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7834 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7835 lun->pr_res_idx = residx; 7836 lun->pr_generation++; 7837 mtx_unlock(&lun->lun_lock); 7838 7839 /* send msg to other side */ 7840 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7841 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7842 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7843 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7844 persis_io.pr.pr_info.res_type = type; 7845 memcpy(persis_io.pr.pr_info.sa_res_key, 7846 param->serv_act_res_key, 7847 sizeof(param->serv_act_res_key)); 7848 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7849 sizeof(persis_io.pr), M_WAITOK); 7850 } else { 7851 /* not all registrants */ 7852 mtx_unlock(&lun->lun_lock); 7853 free(ctsio->kern_data_ptr, M_CTL); 7854 ctl_set_invalid_field(ctsio, 7855 /*sks_valid*/ 1, 7856 /*command*/ 0, 7857 /*field*/ 8, 7858 /*bit_valid*/ 0, 7859 /*bit*/ 0); 7860 ctl_done((union ctl_io *)ctsio); 7861 return (1); 7862 } 7863 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7864 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7865 int found = 0; 7866 7867 if (res_key == sa_res_key) { 7868 /* special case */ 7869 /* 7870 * The spec implies this is not good but doesn't 7871 * say what to do. There are two choices either 7872 * generate a res conflict or check condition 7873 * with illegal field in parameter data. Since 7874 * that is what is done when the sa_res_key is 7875 * zero I'll take that approach since this has 7876 * to do with the sa_res_key. 7877 */ 7878 mtx_unlock(&lun->lun_lock); 7879 free(ctsio->kern_data_ptr, M_CTL); 7880 ctl_set_invalid_field(ctsio, 7881 /*sks_valid*/ 1, 7882 /*command*/ 0, 7883 /*field*/ 8, 7884 /*bit_valid*/ 0, 7885 /*bit*/ 0); 7886 ctl_done((union ctl_io *)ctsio); 7887 return (1); 7888 } 7889 7890 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7891 if (ctl_get_prkey(lun, i) != sa_res_key) 7892 continue; 7893 7894 found = 1; 7895 ctl_clr_prkey(lun, i); 7896 lun->pr_key_count--; 7897 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7898 } 7899 if (!found) { 7900 mtx_unlock(&lun->lun_lock); 7901 free(ctsio->kern_data_ptr, M_CTL); 7902 ctl_set_reservation_conflict(ctsio); 7903 ctl_done((union ctl_io *)ctsio); 7904 return (CTL_RETVAL_COMPLETE); 7905 } 7906 lun->pr_generation++; 7907 mtx_unlock(&lun->lun_lock); 7908 7909 /* send msg to other side */ 7910 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7911 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7912 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7913 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7914 persis_io.pr.pr_info.res_type = type; 7915 memcpy(persis_io.pr.pr_info.sa_res_key, 7916 param->serv_act_res_key, 7917 sizeof(param->serv_act_res_key)); 7918 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7919 sizeof(persis_io.pr), M_WAITOK); 7920 } else { 7921 /* Reserved but not all registrants */ 7922 /* sa_res_key is res holder */ 7923 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7924 /* validate scope and type */ 7925 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7926 SPR_LU_SCOPE) { 7927 mtx_unlock(&lun->lun_lock); 7928 ctl_set_invalid_field(/*ctsio*/ ctsio, 7929 /*sks_valid*/ 1, 7930 /*command*/ 1, 7931 /*field*/ 2, 7932 /*bit_valid*/ 1, 7933 /*bit*/ 4); 7934 ctl_done((union ctl_io *)ctsio); 7935 return (1); 7936 } 7937 7938 if (type>8 || type==2 || type==4 || type==0) { 7939 mtx_unlock(&lun->lun_lock); 7940 ctl_set_invalid_field(/*ctsio*/ ctsio, 7941 /*sks_valid*/ 1, 7942 /*command*/ 1, 7943 /*field*/ 2, 7944 /*bit_valid*/ 1, 7945 /*bit*/ 0); 7946 ctl_done((union ctl_io *)ctsio); 7947 return (1); 7948 } 7949 7950 /* 7951 * Do the following: 7952 * if sa_res_key != res_key remove all 7953 * registrants w/sa_res_key and generate UA 7954 * for these registrants(Registrations 7955 * Preempted) if it wasn't an exclusive 7956 * reservation generate UA(Reservations 7957 * Preempted) for all other registered nexuses 7958 * if the type has changed. Establish the new 7959 * reservation and holder. If res_key and 7960 * sa_res_key are the same do the above 7961 * except don't unregister the res holder. 7962 */ 7963 7964 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7965 if (i == residx || ctl_get_prkey(lun, i) == 0) 7966 continue; 7967 7968 if (sa_res_key == ctl_get_prkey(lun, i)) { 7969 ctl_clr_prkey(lun, i); 7970 lun->pr_key_count--; 7971 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7972 } else if (type != lun->pr_res_type && 7973 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 7974 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 7975 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7976 } 7977 } 7978 lun->pr_res_type = type; 7979 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7980 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7981 lun->pr_res_idx = residx; 7982 else 7983 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7984 lun->pr_generation++; 7985 mtx_unlock(&lun->lun_lock); 7986 7987 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7988 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7989 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7990 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7991 persis_io.pr.pr_info.res_type = type; 7992 memcpy(persis_io.pr.pr_info.sa_res_key, 7993 param->serv_act_res_key, 7994 sizeof(param->serv_act_res_key)); 7995 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7996 sizeof(persis_io.pr), M_WAITOK); 7997 } else { 7998 /* 7999 * sa_res_key is not the res holder just 8000 * remove registrants 8001 */ 8002 int found=0; 8003 8004 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8005 if (sa_res_key != ctl_get_prkey(lun, i)) 8006 continue; 8007 8008 found = 1; 8009 ctl_clr_prkey(lun, i); 8010 lun->pr_key_count--; 8011 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8012 } 8013 8014 if (!found) { 8015 mtx_unlock(&lun->lun_lock); 8016 free(ctsio->kern_data_ptr, M_CTL); 8017 ctl_set_reservation_conflict(ctsio); 8018 ctl_done((union ctl_io *)ctsio); 8019 return (1); 8020 } 8021 lun->pr_generation++; 8022 mtx_unlock(&lun->lun_lock); 8023 8024 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8025 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8026 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8027 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8028 persis_io.pr.pr_info.res_type = type; 8029 memcpy(persis_io.pr.pr_info.sa_res_key, 8030 param->serv_act_res_key, 8031 sizeof(param->serv_act_res_key)); 8032 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8033 sizeof(persis_io.pr), M_WAITOK); 8034 } 8035 } 8036 return (0); 8037 } 8038 8039 static void 8040 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8041 { 8042 uint64_t sa_res_key; 8043 int i; 8044 8045 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8046 8047 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8048 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8049 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8050 if (sa_res_key == 0) { 8051 /* 8052 * Unregister everybody else and build UA for 8053 * them 8054 */ 8055 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8056 if (i == msg->pr.pr_info.residx || 8057 ctl_get_prkey(lun, i) == 0) 8058 continue; 8059 8060 ctl_clr_prkey(lun, i); 8061 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8062 } 8063 8064 lun->pr_key_count = 1; 8065 lun->pr_res_type = msg->pr.pr_info.res_type; 8066 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8067 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8068 lun->pr_res_idx = msg->pr.pr_info.residx; 8069 } else { 8070 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8071 if (sa_res_key == ctl_get_prkey(lun, i)) 8072 continue; 8073 8074 ctl_clr_prkey(lun, i); 8075 lun->pr_key_count--; 8076 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8077 } 8078 } 8079 } else { 8080 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8081 if (i == msg->pr.pr_info.residx || 8082 ctl_get_prkey(lun, i) == 0) 8083 continue; 8084 8085 if (sa_res_key == ctl_get_prkey(lun, i)) { 8086 ctl_clr_prkey(lun, i); 8087 lun->pr_key_count--; 8088 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8089 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8090 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8091 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8092 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8093 } 8094 } 8095 lun->pr_res_type = msg->pr.pr_info.res_type; 8096 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8097 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8098 lun->pr_res_idx = msg->pr.pr_info.residx; 8099 else 8100 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8101 } 8102 lun->pr_generation++; 8103 8104 } 8105 8106 8107 int 8108 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8109 { 8110 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8111 struct ctl_lun *lun = CTL_LUN(ctsio); 8112 int retval; 8113 u_int32_t param_len; 8114 struct scsi_per_res_out *cdb; 8115 struct scsi_per_res_out_parms* param; 8116 uint32_t residx; 8117 uint64_t res_key, sa_res_key, key; 8118 uint8_t type; 8119 union ctl_ha_msg persis_io; 8120 int i; 8121 8122 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8123 8124 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8125 retval = CTL_RETVAL_COMPLETE; 8126 8127 /* 8128 * We only support whole-LUN scope. The scope & type are ignored for 8129 * register, register and ignore existing key and clear. 8130 * We sometimes ignore scope and type on preempts too!! 8131 * Verify reservation type here as well. 8132 */ 8133 type = cdb->scope_type & SPR_TYPE_MASK; 8134 if ((cdb->action == SPRO_RESERVE) 8135 || (cdb->action == SPRO_RELEASE)) { 8136 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8137 ctl_set_invalid_field(/*ctsio*/ ctsio, 8138 /*sks_valid*/ 1, 8139 /*command*/ 1, 8140 /*field*/ 2, 8141 /*bit_valid*/ 1, 8142 /*bit*/ 4); 8143 ctl_done((union ctl_io *)ctsio); 8144 return (CTL_RETVAL_COMPLETE); 8145 } 8146 8147 if (type>8 || type==2 || type==4 || type==0) { 8148 ctl_set_invalid_field(/*ctsio*/ ctsio, 8149 /*sks_valid*/ 1, 8150 /*command*/ 1, 8151 /*field*/ 2, 8152 /*bit_valid*/ 1, 8153 /*bit*/ 0); 8154 ctl_done((union ctl_io *)ctsio); 8155 return (CTL_RETVAL_COMPLETE); 8156 } 8157 } 8158 8159 param_len = scsi_4btoul(cdb->length); 8160 8161 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8162 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8163 ctsio->kern_data_len = param_len; 8164 ctsio->kern_total_len = param_len; 8165 ctsio->kern_rel_offset = 0; 8166 ctsio->kern_sg_entries = 0; 8167 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8168 ctsio->be_move_done = ctl_config_move_done; 8169 ctl_datamove((union ctl_io *)ctsio); 8170 8171 return (CTL_RETVAL_COMPLETE); 8172 } 8173 8174 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8175 8176 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8177 res_key = scsi_8btou64(param->res_key.key); 8178 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8179 8180 /* 8181 * Validate the reservation key here except for SPRO_REG_IGNO 8182 * This must be done for all other service actions 8183 */ 8184 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8185 mtx_lock(&lun->lun_lock); 8186 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8187 if (res_key != key) { 8188 /* 8189 * The current key passed in doesn't match 8190 * the one the initiator previously 8191 * registered. 8192 */ 8193 mtx_unlock(&lun->lun_lock); 8194 free(ctsio->kern_data_ptr, M_CTL); 8195 ctl_set_reservation_conflict(ctsio); 8196 ctl_done((union ctl_io *)ctsio); 8197 return (CTL_RETVAL_COMPLETE); 8198 } 8199 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8200 /* 8201 * We are not registered 8202 */ 8203 mtx_unlock(&lun->lun_lock); 8204 free(ctsio->kern_data_ptr, M_CTL); 8205 ctl_set_reservation_conflict(ctsio); 8206 ctl_done((union ctl_io *)ctsio); 8207 return (CTL_RETVAL_COMPLETE); 8208 } else if (res_key != 0) { 8209 /* 8210 * We are not registered and trying to register but 8211 * the register key isn't zero. 8212 */ 8213 mtx_unlock(&lun->lun_lock); 8214 free(ctsio->kern_data_ptr, M_CTL); 8215 ctl_set_reservation_conflict(ctsio); 8216 ctl_done((union ctl_io *)ctsio); 8217 return (CTL_RETVAL_COMPLETE); 8218 } 8219 mtx_unlock(&lun->lun_lock); 8220 } 8221 8222 switch (cdb->action & SPRO_ACTION_MASK) { 8223 case SPRO_REGISTER: 8224 case SPRO_REG_IGNO: { 8225 8226 /* 8227 * We don't support any of these options, as we report in 8228 * the read capabilities request (see 8229 * ctl_persistent_reserve_in(), above). 8230 */ 8231 if ((param->flags & SPR_SPEC_I_PT) 8232 || (param->flags & SPR_ALL_TG_PT) 8233 || (param->flags & SPR_APTPL)) { 8234 int bit_ptr; 8235 8236 if (param->flags & SPR_APTPL) 8237 bit_ptr = 0; 8238 else if (param->flags & SPR_ALL_TG_PT) 8239 bit_ptr = 2; 8240 else /* SPR_SPEC_I_PT */ 8241 bit_ptr = 3; 8242 8243 free(ctsio->kern_data_ptr, M_CTL); 8244 ctl_set_invalid_field(ctsio, 8245 /*sks_valid*/ 1, 8246 /*command*/ 0, 8247 /*field*/ 20, 8248 /*bit_valid*/ 1, 8249 /*bit*/ bit_ptr); 8250 ctl_done((union ctl_io *)ctsio); 8251 return (CTL_RETVAL_COMPLETE); 8252 } 8253 8254 mtx_lock(&lun->lun_lock); 8255 8256 /* 8257 * The initiator wants to clear the 8258 * key/unregister. 8259 */ 8260 if (sa_res_key == 0) { 8261 if ((res_key == 0 8262 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8263 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8264 && ctl_get_prkey(lun, residx) == 0)) { 8265 mtx_unlock(&lun->lun_lock); 8266 goto done; 8267 } 8268 8269 ctl_clr_prkey(lun, residx); 8270 lun->pr_key_count--; 8271 8272 if (residx == lun->pr_res_idx) { 8273 lun->flags &= ~CTL_LUN_PR_RESERVED; 8274 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8275 8276 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8277 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8278 lun->pr_key_count) { 8279 /* 8280 * If the reservation is a registrants 8281 * only type we need to generate a UA 8282 * for other registered inits. The 8283 * sense code should be RESERVATIONS 8284 * RELEASED 8285 */ 8286 8287 for (i = softc->init_min; i < softc->init_max; i++){ 8288 if (ctl_get_prkey(lun, i) == 0) 8289 continue; 8290 ctl_est_ua(lun, i, 8291 CTL_UA_RES_RELEASE); 8292 } 8293 } 8294 lun->pr_res_type = 0; 8295 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8296 if (lun->pr_key_count==0) { 8297 lun->flags &= ~CTL_LUN_PR_RESERVED; 8298 lun->pr_res_type = 0; 8299 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8300 } 8301 } 8302 lun->pr_generation++; 8303 mtx_unlock(&lun->lun_lock); 8304 8305 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8306 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8307 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8308 persis_io.pr.pr_info.residx = residx; 8309 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8310 sizeof(persis_io.pr), M_WAITOK); 8311 } else /* sa_res_key != 0 */ { 8312 8313 /* 8314 * If we aren't registered currently then increment 8315 * the key count and set the registered flag. 8316 */ 8317 ctl_alloc_prkey(lun, residx); 8318 if (ctl_get_prkey(lun, residx) == 0) 8319 lun->pr_key_count++; 8320 ctl_set_prkey(lun, residx, sa_res_key); 8321 lun->pr_generation++; 8322 mtx_unlock(&lun->lun_lock); 8323 8324 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8325 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8326 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8327 persis_io.pr.pr_info.residx = residx; 8328 memcpy(persis_io.pr.pr_info.sa_res_key, 8329 param->serv_act_res_key, 8330 sizeof(param->serv_act_res_key)); 8331 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8332 sizeof(persis_io.pr), M_WAITOK); 8333 } 8334 8335 break; 8336 } 8337 case SPRO_RESERVE: 8338 mtx_lock(&lun->lun_lock); 8339 if (lun->flags & CTL_LUN_PR_RESERVED) { 8340 /* 8341 * if this isn't the reservation holder and it's 8342 * not a "all registrants" type or if the type is 8343 * different then we have a conflict 8344 */ 8345 if ((lun->pr_res_idx != residx 8346 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8347 || lun->pr_res_type != type) { 8348 mtx_unlock(&lun->lun_lock); 8349 free(ctsio->kern_data_ptr, M_CTL); 8350 ctl_set_reservation_conflict(ctsio); 8351 ctl_done((union ctl_io *)ctsio); 8352 return (CTL_RETVAL_COMPLETE); 8353 } 8354 mtx_unlock(&lun->lun_lock); 8355 } else /* create a reservation */ { 8356 /* 8357 * If it's not an "all registrants" type record 8358 * reservation holder 8359 */ 8360 if (type != SPR_TYPE_WR_EX_AR 8361 && type != SPR_TYPE_EX_AC_AR) 8362 lun->pr_res_idx = residx; /* Res holder */ 8363 else 8364 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8365 8366 lun->flags |= CTL_LUN_PR_RESERVED; 8367 lun->pr_res_type = type; 8368 8369 mtx_unlock(&lun->lun_lock); 8370 8371 /* send msg to other side */ 8372 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8373 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8374 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8375 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8376 persis_io.pr.pr_info.res_type = type; 8377 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8378 sizeof(persis_io.pr), M_WAITOK); 8379 } 8380 break; 8381 8382 case SPRO_RELEASE: 8383 mtx_lock(&lun->lun_lock); 8384 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8385 /* No reservation exists return good status */ 8386 mtx_unlock(&lun->lun_lock); 8387 goto done; 8388 } 8389 /* 8390 * Is this nexus a reservation holder? 8391 */ 8392 if (lun->pr_res_idx != residx 8393 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8394 /* 8395 * not a res holder return good status but 8396 * do nothing 8397 */ 8398 mtx_unlock(&lun->lun_lock); 8399 goto done; 8400 } 8401 8402 if (lun->pr_res_type != type) { 8403 mtx_unlock(&lun->lun_lock); 8404 free(ctsio->kern_data_ptr, M_CTL); 8405 ctl_set_illegal_pr_release(ctsio); 8406 ctl_done((union ctl_io *)ctsio); 8407 return (CTL_RETVAL_COMPLETE); 8408 } 8409 8410 /* okay to release */ 8411 lun->flags &= ~CTL_LUN_PR_RESERVED; 8412 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8413 lun->pr_res_type = 0; 8414 8415 /* 8416 * If this isn't an exclusive access reservation and NUAR 8417 * is not set, generate UA for all other registrants. 8418 */ 8419 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8420 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8421 for (i = softc->init_min; i < softc->init_max; i++) { 8422 if (i == residx || ctl_get_prkey(lun, i) == 0) 8423 continue; 8424 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8425 } 8426 } 8427 mtx_unlock(&lun->lun_lock); 8428 8429 /* Send msg to other side */ 8430 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8431 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8432 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8433 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8434 sizeof(persis_io.pr), M_WAITOK); 8435 break; 8436 8437 case SPRO_CLEAR: 8438 /* send msg to other side */ 8439 8440 mtx_lock(&lun->lun_lock); 8441 lun->flags &= ~CTL_LUN_PR_RESERVED; 8442 lun->pr_res_type = 0; 8443 lun->pr_key_count = 0; 8444 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8445 8446 ctl_clr_prkey(lun, residx); 8447 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8448 if (ctl_get_prkey(lun, i) != 0) { 8449 ctl_clr_prkey(lun, i); 8450 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8451 } 8452 lun->pr_generation++; 8453 mtx_unlock(&lun->lun_lock); 8454 8455 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8456 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8457 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8458 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8459 sizeof(persis_io.pr), M_WAITOK); 8460 break; 8461 8462 case SPRO_PREEMPT: 8463 case SPRO_PRE_ABO: { 8464 int nretval; 8465 8466 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8467 residx, ctsio, cdb, param); 8468 if (nretval != 0) 8469 return (CTL_RETVAL_COMPLETE); 8470 break; 8471 } 8472 default: 8473 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8474 } 8475 8476 done: 8477 free(ctsio->kern_data_ptr, M_CTL); 8478 ctl_set_success(ctsio); 8479 ctl_done((union ctl_io *)ctsio); 8480 8481 return (retval); 8482 } 8483 8484 /* 8485 * This routine is for handling a message from the other SC pertaining to 8486 * persistent reserve out. All the error checking will have been done 8487 * so only perorming the action need be done here to keep the two 8488 * in sync. 8489 */ 8490 static void 8491 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8492 { 8493 struct ctl_softc *softc = CTL_SOFTC(io); 8494 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8495 struct ctl_lun *lun; 8496 int i; 8497 uint32_t residx, targ_lun; 8498 8499 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8500 mtx_lock(&softc->ctl_lock); 8501 if (targ_lun >= ctl_max_luns || 8502 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8503 mtx_unlock(&softc->ctl_lock); 8504 return; 8505 } 8506 mtx_lock(&lun->lun_lock); 8507 mtx_unlock(&softc->ctl_lock); 8508 if (lun->flags & CTL_LUN_DISABLED) { 8509 mtx_unlock(&lun->lun_lock); 8510 return; 8511 } 8512 residx = ctl_get_initindex(&msg->hdr.nexus); 8513 switch(msg->pr.pr_info.action) { 8514 case CTL_PR_REG_KEY: 8515 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8516 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8517 lun->pr_key_count++; 8518 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8519 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8520 lun->pr_generation++; 8521 break; 8522 8523 case CTL_PR_UNREG_KEY: 8524 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8525 lun->pr_key_count--; 8526 8527 /* XXX Need to see if the reservation has been released */ 8528 /* if so do we need to generate UA? */ 8529 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8530 lun->flags &= ~CTL_LUN_PR_RESERVED; 8531 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8532 8533 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8534 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8535 lun->pr_key_count) { 8536 /* 8537 * If the reservation is a registrants 8538 * only type we need to generate a UA 8539 * for other registered inits. The 8540 * sense code should be RESERVATIONS 8541 * RELEASED 8542 */ 8543 8544 for (i = softc->init_min; i < softc->init_max; i++) { 8545 if (ctl_get_prkey(lun, i) == 0) 8546 continue; 8547 8548 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8549 } 8550 } 8551 lun->pr_res_type = 0; 8552 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8553 if (lun->pr_key_count==0) { 8554 lun->flags &= ~CTL_LUN_PR_RESERVED; 8555 lun->pr_res_type = 0; 8556 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8557 } 8558 } 8559 lun->pr_generation++; 8560 break; 8561 8562 case CTL_PR_RESERVE: 8563 lun->flags |= CTL_LUN_PR_RESERVED; 8564 lun->pr_res_type = msg->pr.pr_info.res_type; 8565 lun->pr_res_idx = msg->pr.pr_info.residx; 8566 8567 break; 8568 8569 case CTL_PR_RELEASE: 8570 /* 8571 * If this isn't an exclusive access reservation and NUAR 8572 * is not set, generate UA for all other registrants. 8573 */ 8574 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8575 lun->pr_res_type != SPR_TYPE_WR_EX && 8576 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8577 for (i = softc->init_min; i < softc->init_max; i++) { 8578 if (i == residx || ctl_get_prkey(lun, i) == 0) 8579 continue; 8580 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8581 } 8582 } 8583 8584 lun->flags &= ~CTL_LUN_PR_RESERVED; 8585 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8586 lun->pr_res_type = 0; 8587 break; 8588 8589 case CTL_PR_PREEMPT: 8590 ctl_pro_preempt_other(lun, msg); 8591 break; 8592 case CTL_PR_CLEAR: 8593 lun->flags &= ~CTL_LUN_PR_RESERVED; 8594 lun->pr_res_type = 0; 8595 lun->pr_key_count = 0; 8596 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8597 8598 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8599 if (ctl_get_prkey(lun, i) == 0) 8600 continue; 8601 ctl_clr_prkey(lun, i); 8602 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8603 } 8604 lun->pr_generation++; 8605 break; 8606 } 8607 8608 mtx_unlock(&lun->lun_lock); 8609 } 8610 8611 int 8612 ctl_read_write(struct ctl_scsiio *ctsio) 8613 { 8614 struct ctl_lun *lun = CTL_LUN(ctsio); 8615 struct ctl_lba_len_flags *lbalen; 8616 uint64_t lba; 8617 uint32_t num_blocks; 8618 int flags, retval; 8619 int isread; 8620 8621 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8622 8623 flags = 0; 8624 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8625 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8626 switch (ctsio->cdb[0]) { 8627 case READ_6: 8628 case WRITE_6: { 8629 struct scsi_rw_6 *cdb; 8630 8631 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8632 8633 lba = scsi_3btoul(cdb->addr); 8634 /* only 5 bits are valid in the most significant address byte */ 8635 lba &= 0x1fffff; 8636 num_blocks = cdb->length; 8637 /* 8638 * This is correct according to SBC-2. 8639 */ 8640 if (num_blocks == 0) 8641 num_blocks = 256; 8642 break; 8643 } 8644 case READ_10: 8645 case WRITE_10: { 8646 struct scsi_rw_10 *cdb; 8647 8648 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8649 if (cdb->byte2 & SRW10_FUA) 8650 flags |= CTL_LLF_FUA; 8651 if (cdb->byte2 & SRW10_DPO) 8652 flags |= CTL_LLF_DPO; 8653 lba = scsi_4btoul(cdb->addr); 8654 num_blocks = scsi_2btoul(cdb->length); 8655 break; 8656 } 8657 case WRITE_VERIFY_10: { 8658 struct scsi_write_verify_10 *cdb; 8659 8660 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8661 flags |= CTL_LLF_FUA; 8662 if (cdb->byte2 & SWV_DPO) 8663 flags |= CTL_LLF_DPO; 8664 lba = scsi_4btoul(cdb->addr); 8665 num_blocks = scsi_2btoul(cdb->length); 8666 break; 8667 } 8668 case READ_12: 8669 case WRITE_12: { 8670 struct scsi_rw_12 *cdb; 8671 8672 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8673 if (cdb->byte2 & SRW12_FUA) 8674 flags |= CTL_LLF_FUA; 8675 if (cdb->byte2 & SRW12_DPO) 8676 flags |= CTL_LLF_DPO; 8677 lba = scsi_4btoul(cdb->addr); 8678 num_blocks = scsi_4btoul(cdb->length); 8679 break; 8680 } 8681 case WRITE_VERIFY_12: { 8682 struct scsi_write_verify_12 *cdb; 8683 8684 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8685 flags |= CTL_LLF_FUA; 8686 if (cdb->byte2 & SWV_DPO) 8687 flags |= CTL_LLF_DPO; 8688 lba = scsi_4btoul(cdb->addr); 8689 num_blocks = scsi_4btoul(cdb->length); 8690 break; 8691 } 8692 case READ_16: 8693 case WRITE_16: { 8694 struct scsi_rw_16 *cdb; 8695 8696 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8697 if (cdb->byte2 & SRW12_FUA) 8698 flags |= CTL_LLF_FUA; 8699 if (cdb->byte2 & SRW12_DPO) 8700 flags |= CTL_LLF_DPO; 8701 lba = scsi_8btou64(cdb->addr); 8702 num_blocks = scsi_4btoul(cdb->length); 8703 break; 8704 } 8705 case WRITE_ATOMIC_16: { 8706 struct scsi_write_atomic_16 *cdb; 8707 8708 if (lun->be_lun->atomicblock == 0) { 8709 ctl_set_invalid_opcode(ctsio); 8710 ctl_done((union ctl_io *)ctsio); 8711 return (CTL_RETVAL_COMPLETE); 8712 } 8713 8714 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8715 if (cdb->byte2 & SRW12_FUA) 8716 flags |= CTL_LLF_FUA; 8717 if (cdb->byte2 & SRW12_DPO) 8718 flags |= CTL_LLF_DPO; 8719 lba = scsi_8btou64(cdb->addr); 8720 num_blocks = scsi_2btoul(cdb->length); 8721 if (num_blocks > lun->be_lun->atomicblock) { 8722 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8723 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8724 /*bit*/ 0); 8725 ctl_done((union ctl_io *)ctsio); 8726 return (CTL_RETVAL_COMPLETE); 8727 } 8728 break; 8729 } 8730 case WRITE_VERIFY_16: { 8731 struct scsi_write_verify_16 *cdb; 8732 8733 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8734 flags |= CTL_LLF_FUA; 8735 if (cdb->byte2 & SWV_DPO) 8736 flags |= CTL_LLF_DPO; 8737 lba = scsi_8btou64(cdb->addr); 8738 num_blocks = scsi_4btoul(cdb->length); 8739 break; 8740 } 8741 default: 8742 /* 8743 * We got a command we don't support. This shouldn't 8744 * happen, commands should be filtered out above us. 8745 */ 8746 ctl_set_invalid_opcode(ctsio); 8747 ctl_done((union ctl_io *)ctsio); 8748 8749 return (CTL_RETVAL_COMPLETE); 8750 break; /* NOTREACHED */ 8751 } 8752 8753 /* 8754 * The first check is to make sure we're in bounds, the second 8755 * check is to catch wrap-around problems. If the lba + num blocks 8756 * is less than the lba, then we've wrapped around and the block 8757 * range is invalid anyway. 8758 */ 8759 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8760 || ((lba + num_blocks) < lba)) { 8761 ctl_set_lba_out_of_range(ctsio, 8762 MAX(lba, lun->be_lun->maxlba + 1)); 8763 ctl_done((union ctl_io *)ctsio); 8764 return (CTL_RETVAL_COMPLETE); 8765 } 8766 8767 /* 8768 * According to SBC-3, a transfer length of 0 is not an error. 8769 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8770 * translates to 256 blocks for those commands. 8771 */ 8772 if (num_blocks == 0) { 8773 ctl_set_success(ctsio); 8774 ctl_done((union ctl_io *)ctsio); 8775 return (CTL_RETVAL_COMPLETE); 8776 } 8777 8778 /* Set FUA and/or DPO if caches are disabled. */ 8779 if (isread) { 8780 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8781 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8782 } else { 8783 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8784 flags |= CTL_LLF_FUA; 8785 } 8786 8787 lbalen = (struct ctl_lba_len_flags *) 8788 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8789 lbalen->lba = lba; 8790 lbalen->len = num_blocks; 8791 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8792 8793 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8794 ctsio->kern_rel_offset = 0; 8795 8796 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8797 8798 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8799 return (retval); 8800 } 8801 8802 static int 8803 ctl_cnw_cont(union ctl_io *io) 8804 { 8805 struct ctl_lun *lun = CTL_LUN(io); 8806 struct ctl_scsiio *ctsio; 8807 struct ctl_lba_len_flags *lbalen; 8808 int retval; 8809 8810 ctsio = &io->scsiio; 8811 ctsio->io_hdr.status = CTL_STATUS_NONE; 8812 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8813 lbalen = (struct ctl_lba_len_flags *) 8814 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8815 lbalen->flags &= ~CTL_LLF_COMPARE; 8816 lbalen->flags |= CTL_LLF_WRITE; 8817 8818 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8819 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8820 return (retval); 8821 } 8822 8823 int 8824 ctl_cnw(struct ctl_scsiio *ctsio) 8825 { 8826 struct ctl_lun *lun = CTL_LUN(ctsio); 8827 struct ctl_lba_len_flags *lbalen; 8828 uint64_t lba; 8829 uint32_t num_blocks; 8830 int flags, retval; 8831 8832 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8833 8834 flags = 0; 8835 switch (ctsio->cdb[0]) { 8836 case COMPARE_AND_WRITE: { 8837 struct scsi_compare_and_write *cdb; 8838 8839 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8840 if (cdb->byte2 & SRW10_FUA) 8841 flags |= CTL_LLF_FUA; 8842 if (cdb->byte2 & SRW10_DPO) 8843 flags |= CTL_LLF_DPO; 8844 lba = scsi_8btou64(cdb->addr); 8845 num_blocks = cdb->length; 8846 break; 8847 } 8848 default: 8849 /* 8850 * We got a command we don't support. This shouldn't 8851 * happen, commands should be filtered out above us. 8852 */ 8853 ctl_set_invalid_opcode(ctsio); 8854 ctl_done((union ctl_io *)ctsio); 8855 8856 return (CTL_RETVAL_COMPLETE); 8857 break; /* NOTREACHED */ 8858 } 8859 8860 /* 8861 * The first check is to make sure we're in bounds, the second 8862 * check is to catch wrap-around problems. If the lba + num blocks 8863 * is less than the lba, then we've wrapped around and the block 8864 * range is invalid anyway. 8865 */ 8866 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8867 || ((lba + num_blocks) < lba)) { 8868 ctl_set_lba_out_of_range(ctsio, 8869 MAX(lba, lun->be_lun->maxlba + 1)); 8870 ctl_done((union ctl_io *)ctsio); 8871 return (CTL_RETVAL_COMPLETE); 8872 } 8873 8874 /* 8875 * According to SBC-3, a transfer length of 0 is not an error. 8876 */ 8877 if (num_blocks == 0) { 8878 ctl_set_success(ctsio); 8879 ctl_done((union ctl_io *)ctsio); 8880 return (CTL_RETVAL_COMPLETE); 8881 } 8882 8883 /* Set FUA if write cache is disabled. */ 8884 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8885 flags |= CTL_LLF_FUA; 8886 8887 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8888 ctsio->kern_rel_offset = 0; 8889 8890 /* 8891 * Set the IO_CONT flag, so that if this I/O gets passed to 8892 * ctl_data_submit_done(), it'll get passed back to 8893 * ctl_ctl_cnw_cont() for further processing. 8894 */ 8895 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8896 ctsio->io_cont = ctl_cnw_cont; 8897 8898 lbalen = (struct ctl_lba_len_flags *) 8899 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8900 lbalen->lba = lba; 8901 lbalen->len = num_blocks; 8902 lbalen->flags = CTL_LLF_COMPARE | flags; 8903 8904 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8905 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8906 return (retval); 8907 } 8908 8909 int 8910 ctl_verify(struct ctl_scsiio *ctsio) 8911 { 8912 struct ctl_lun *lun = CTL_LUN(ctsio); 8913 struct ctl_lba_len_flags *lbalen; 8914 uint64_t lba; 8915 uint32_t num_blocks; 8916 int bytchk, flags; 8917 int retval; 8918 8919 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8920 8921 bytchk = 0; 8922 flags = CTL_LLF_FUA; 8923 switch (ctsio->cdb[0]) { 8924 case VERIFY_10: { 8925 struct scsi_verify_10 *cdb; 8926 8927 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8928 if (cdb->byte2 & SVFY_BYTCHK) 8929 bytchk = 1; 8930 if (cdb->byte2 & SVFY_DPO) 8931 flags |= CTL_LLF_DPO; 8932 lba = scsi_4btoul(cdb->addr); 8933 num_blocks = scsi_2btoul(cdb->length); 8934 break; 8935 } 8936 case VERIFY_12: { 8937 struct scsi_verify_12 *cdb; 8938 8939 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8940 if (cdb->byte2 & SVFY_BYTCHK) 8941 bytchk = 1; 8942 if (cdb->byte2 & SVFY_DPO) 8943 flags |= CTL_LLF_DPO; 8944 lba = scsi_4btoul(cdb->addr); 8945 num_blocks = scsi_4btoul(cdb->length); 8946 break; 8947 } 8948 case VERIFY_16: { 8949 struct scsi_rw_16 *cdb; 8950 8951 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8952 if (cdb->byte2 & SVFY_BYTCHK) 8953 bytchk = 1; 8954 if (cdb->byte2 & SVFY_DPO) 8955 flags |= CTL_LLF_DPO; 8956 lba = scsi_8btou64(cdb->addr); 8957 num_blocks = scsi_4btoul(cdb->length); 8958 break; 8959 } 8960 default: 8961 /* 8962 * We got a command we don't support. This shouldn't 8963 * happen, commands should be filtered out above us. 8964 */ 8965 ctl_set_invalid_opcode(ctsio); 8966 ctl_done((union ctl_io *)ctsio); 8967 return (CTL_RETVAL_COMPLETE); 8968 } 8969 8970 /* 8971 * The first check is to make sure we're in bounds, the second 8972 * check is to catch wrap-around problems. If the lba + num blocks 8973 * is less than the lba, then we've wrapped around and the block 8974 * range is invalid anyway. 8975 */ 8976 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8977 || ((lba + num_blocks) < lba)) { 8978 ctl_set_lba_out_of_range(ctsio, 8979 MAX(lba, lun->be_lun->maxlba + 1)); 8980 ctl_done((union ctl_io *)ctsio); 8981 return (CTL_RETVAL_COMPLETE); 8982 } 8983 8984 /* 8985 * According to SBC-3, a transfer length of 0 is not an error. 8986 */ 8987 if (num_blocks == 0) { 8988 ctl_set_success(ctsio); 8989 ctl_done((union ctl_io *)ctsio); 8990 return (CTL_RETVAL_COMPLETE); 8991 } 8992 8993 lbalen = (struct ctl_lba_len_flags *) 8994 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8995 lbalen->lba = lba; 8996 lbalen->len = num_blocks; 8997 if (bytchk) { 8998 lbalen->flags = CTL_LLF_COMPARE | flags; 8999 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9000 } else { 9001 lbalen->flags = CTL_LLF_VERIFY | flags; 9002 ctsio->kern_total_len = 0; 9003 } 9004 ctsio->kern_rel_offset = 0; 9005 9006 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9007 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9008 return (retval); 9009 } 9010 9011 int 9012 ctl_report_luns(struct ctl_scsiio *ctsio) 9013 { 9014 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9015 struct ctl_port *port = CTL_PORT(ctsio); 9016 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9017 struct scsi_report_luns *cdb; 9018 struct scsi_report_luns_data *lun_data; 9019 int num_filled, num_luns, num_port_luns, retval; 9020 uint32_t alloc_len, lun_datalen; 9021 uint32_t initidx, targ_lun_id, lun_id; 9022 9023 retval = CTL_RETVAL_COMPLETE; 9024 cdb = (struct scsi_report_luns *)ctsio->cdb; 9025 9026 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9027 9028 num_luns = 0; 9029 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9030 mtx_lock(&softc->ctl_lock); 9031 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9032 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9033 num_luns++; 9034 } 9035 mtx_unlock(&softc->ctl_lock); 9036 9037 switch (cdb->select_report) { 9038 case RPL_REPORT_DEFAULT: 9039 case RPL_REPORT_ALL: 9040 case RPL_REPORT_NONSUBSID: 9041 break; 9042 case RPL_REPORT_WELLKNOWN: 9043 case RPL_REPORT_ADMIN: 9044 case RPL_REPORT_CONGLOM: 9045 num_luns = 0; 9046 break; 9047 default: 9048 ctl_set_invalid_field(ctsio, 9049 /*sks_valid*/ 1, 9050 /*command*/ 1, 9051 /*field*/ 2, 9052 /*bit_valid*/ 0, 9053 /*bit*/ 0); 9054 ctl_done((union ctl_io *)ctsio); 9055 return (retval); 9056 break; /* NOTREACHED */ 9057 } 9058 9059 alloc_len = scsi_4btoul(cdb->length); 9060 /* 9061 * The initiator has to allocate at least 16 bytes for this request, 9062 * so he can at least get the header and the first LUN. Otherwise 9063 * we reject the request (per SPC-3 rev 14, section 6.21). 9064 */ 9065 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9066 sizeof(struct scsi_report_luns_lundata))) { 9067 ctl_set_invalid_field(ctsio, 9068 /*sks_valid*/ 1, 9069 /*command*/ 1, 9070 /*field*/ 6, 9071 /*bit_valid*/ 0, 9072 /*bit*/ 0); 9073 ctl_done((union ctl_io *)ctsio); 9074 return (retval); 9075 } 9076 9077 lun_datalen = sizeof(*lun_data) + 9078 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9079 9080 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9081 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9082 ctsio->kern_sg_entries = 0; 9083 9084 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9085 9086 mtx_lock(&softc->ctl_lock); 9087 for (targ_lun_id = 0, num_filled = 0; 9088 targ_lun_id < num_port_luns && num_filled < num_luns; 9089 targ_lun_id++) { 9090 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9091 if (lun_id == UINT32_MAX) 9092 continue; 9093 lun = softc->ctl_luns[lun_id]; 9094 if (lun == NULL) 9095 continue; 9096 9097 be64enc(lun_data->luns[num_filled++].lundata, 9098 ctl_encode_lun(targ_lun_id)); 9099 9100 /* 9101 * According to SPC-3, rev 14 section 6.21: 9102 * 9103 * "The execution of a REPORT LUNS command to any valid and 9104 * installed logical unit shall clear the REPORTED LUNS DATA 9105 * HAS CHANGED unit attention condition for all logical 9106 * units of that target with respect to the requesting 9107 * initiator. A valid and installed logical unit is one 9108 * having a PERIPHERAL QUALIFIER of 000b in the standard 9109 * INQUIRY data (see 6.4.2)." 9110 * 9111 * If request_lun is NULL, the LUN this report luns command 9112 * was issued to is either disabled or doesn't exist. In that 9113 * case, we shouldn't clear any pending lun change unit 9114 * attention. 9115 */ 9116 if (request_lun != NULL) { 9117 mtx_lock(&lun->lun_lock); 9118 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9119 mtx_unlock(&lun->lun_lock); 9120 } 9121 } 9122 mtx_unlock(&softc->ctl_lock); 9123 9124 /* 9125 * It's quite possible that we've returned fewer LUNs than we allocated 9126 * space for. Trim it. 9127 */ 9128 lun_datalen = sizeof(*lun_data) + 9129 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9130 ctsio->kern_rel_offset = 0; 9131 ctsio->kern_sg_entries = 0; 9132 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9133 ctsio->kern_total_len = ctsio->kern_data_len; 9134 9135 /* 9136 * We set this to the actual data length, regardless of how much 9137 * space we actually have to return results. If the user looks at 9138 * this value, he'll know whether or not he allocated enough space 9139 * and reissue the command if necessary. We don't support well 9140 * known logical units, so if the user asks for that, return none. 9141 */ 9142 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9143 9144 /* 9145 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9146 * this request. 9147 */ 9148 ctl_set_success(ctsio); 9149 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9150 ctsio->be_move_done = ctl_config_move_done; 9151 ctl_datamove((union ctl_io *)ctsio); 9152 return (retval); 9153 } 9154 9155 int 9156 ctl_request_sense(struct ctl_scsiio *ctsio) 9157 { 9158 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9159 struct ctl_lun *lun = CTL_LUN(ctsio); 9160 struct scsi_request_sense *cdb; 9161 struct scsi_sense_data *sense_ptr, *ps; 9162 uint32_t initidx; 9163 int have_error; 9164 u_int sense_len = SSD_FULL_SIZE; 9165 scsi_sense_data_type sense_format; 9166 ctl_ua_type ua_type; 9167 uint8_t asc = 0, ascq = 0; 9168 9169 cdb = (struct scsi_request_sense *)ctsio->cdb; 9170 9171 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9172 9173 /* 9174 * Determine which sense format the user wants. 9175 */ 9176 if (cdb->byte2 & SRS_DESC) 9177 sense_format = SSD_TYPE_DESC; 9178 else 9179 sense_format = SSD_TYPE_FIXED; 9180 9181 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9182 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9183 ctsio->kern_sg_entries = 0; 9184 ctsio->kern_rel_offset = 0; 9185 9186 /* 9187 * struct scsi_sense_data, which is currently set to 256 bytes, is 9188 * larger than the largest allowed value for the length field in the 9189 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9190 */ 9191 ctsio->kern_data_len = cdb->length; 9192 ctsio->kern_total_len = cdb->length; 9193 9194 /* 9195 * If we don't have a LUN, we don't have any pending sense. 9196 */ 9197 if (lun == NULL || 9198 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9199 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9200 /* "Logical unit not supported" */ 9201 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9202 /*current_error*/ 1, 9203 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9204 /*asc*/ 0x25, 9205 /*ascq*/ 0x00, 9206 SSD_ELEM_NONE); 9207 goto send; 9208 } 9209 9210 have_error = 0; 9211 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9212 /* 9213 * Check for pending sense, and then for pending unit attentions. 9214 * Pending sense gets returned first, then pending unit attentions. 9215 */ 9216 mtx_lock(&lun->lun_lock); 9217 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9218 if (ps != NULL) 9219 ps += initidx % CTL_MAX_INIT_PER_PORT; 9220 if (ps != NULL && ps->error_code != 0) { 9221 scsi_sense_data_type stored_format; 9222 9223 /* 9224 * Check to see which sense format was used for the stored 9225 * sense data. 9226 */ 9227 stored_format = scsi_sense_type(ps); 9228 9229 /* 9230 * If the user requested a different sense format than the 9231 * one we stored, then we need to convert it to the other 9232 * format. If we're going from descriptor to fixed format 9233 * sense data, we may lose things in translation, depending 9234 * on what options were used. 9235 * 9236 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9237 * for some reason we'll just copy it out as-is. 9238 */ 9239 if ((stored_format == SSD_TYPE_FIXED) 9240 && (sense_format == SSD_TYPE_DESC)) 9241 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9242 ps, (struct scsi_sense_data_desc *)sense_ptr); 9243 else if ((stored_format == SSD_TYPE_DESC) 9244 && (sense_format == SSD_TYPE_FIXED)) 9245 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9246 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9247 else 9248 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9249 9250 ps->error_code = 0; 9251 have_error = 1; 9252 } else { 9253 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9254 sense_format); 9255 if (ua_type != CTL_UA_NONE) 9256 have_error = 1; 9257 } 9258 if (have_error == 0) { 9259 /* 9260 * Report informational exception if have one and allowed. 9261 */ 9262 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9263 asc = lun->ie_asc; 9264 ascq = lun->ie_ascq; 9265 } 9266 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9267 /*current_error*/ 1, 9268 /*sense_key*/ SSD_KEY_NO_SENSE, 9269 /*asc*/ asc, 9270 /*ascq*/ ascq, 9271 SSD_ELEM_NONE); 9272 } 9273 mtx_unlock(&lun->lun_lock); 9274 9275 send: 9276 /* 9277 * We report the SCSI status as OK, since the status of the command 9278 * itself is OK. We're reporting sense as parameter data. 9279 */ 9280 ctl_set_success(ctsio); 9281 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9282 ctsio->be_move_done = ctl_config_move_done; 9283 ctl_datamove((union ctl_io *)ctsio); 9284 return (CTL_RETVAL_COMPLETE); 9285 } 9286 9287 int 9288 ctl_tur(struct ctl_scsiio *ctsio) 9289 { 9290 9291 CTL_DEBUG_PRINT(("ctl_tur\n")); 9292 9293 ctl_set_success(ctsio); 9294 ctl_done((union ctl_io *)ctsio); 9295 9296 return (CTL_RETVAL_COMPLETE); 9297 } 9298 9299 /* 9300 * SCSI VPD page 0x00, the Supported VPD Pages page. 9301 */ 9302 static int 9303 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9304 { 9305 struct ctl_lun *lun = CTL_LUN(ctsio); 9306 struct scsi_vpd_supported_pages *pages; 9307 int sup_page_size; 9308 int p; 9309 9310 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9311 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9312 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9313 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9314 ctsio->kern_rel_offset = 0; 9315 ctsio->kern_sg_entries = 0; 9316 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9317 ctsio->kern_total_len = ctsio->kern_data_len; 9318 9319 /* 9320 * The control device is always connected. The disk device, on the 9321 * other hand, may not be online all the time. Need to change this 9322 * to figure out whether the disk device is actually online or not. 9323 */ 9324 if (lun != NULL) 9325 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9326 lun->be_lun->lun_type; 9327 else 9328 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9329 9330 p = 0; 9331 /* Supported VPD pages */ 9332 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9333 /* Serial Number */ 9334 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9335 /* Device Identification */ 9336 pages->page_list[p++] = SVPD_DEVICE_ID; 9337 /* Extended INQUIRY Data */ 9338 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9339 /* Mode Page Policy */ 9340 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9341 /* SCSI Ports */ 9342 pages->page_list[p++] = SVPD_SCSI_PORTS; 9343 /* Third-party Copy */ 9344 pages->page_list[p++] = SVPD_SCSI_TPC; 9345 /* SCSI Feature Sets */ 9346 pages->page_list[p++] = SVPD_SCSI_SFS; 9347 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9348 /* Block limits */ 9349 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9350 /* Block Device Characteristics */ 9351 pages->page_list[p++] = SVPD_BDC; 9352 /* Logical Block Provisioning */ 9353 pages->page_list[p++] = SVPD_LBP; 9354 } 9355 pages->length = p; 9356 9357 ctl_set_success(ctsio); 9358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9359 ctsio->be_move_done = ctl_config_move_done; 9360 ctl_datamove((union ctl_io *)ctsio); 9361 return (CTL_RETVAL_COMPLETE); 9362 } 9363 9364 /* 9365 * SCSI VPD page 0x80, the Unit Serial Number page. 9366 */ 9367 static int 9368 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9369 { 9370 struct ctl_lun *lun = CTL_LUN(ctsio); 9371 struct scsi_vpd_unit_serial_number *sn_ptr; 9372 int data_len; 9373 9374 data_len = 4 + CTL_SN_LEN; 9375 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9376 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9377 ctsio->kern_rel_offset = 0; 9378 ctsio->kern_sg_entries = 0; 9379 ctsio->kern_data_len = min(data_len, alloc_len); 9380 ctsio->kern_total_len = ctsio->kern_data_len; 9381 9382 /* 9383 * The control device is always connected. The disk device, on the 9384 * other hand, may not be online all the time. Need to change this 9385 * to figure out whether the disk device is actually online or not. 9386 */ 9387 if (lun != NULL) 9388 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9389 lun->be_lun->lun_type; 9390 else 9391 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9392 9393 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9394 sn_ptr->length = CTL_SN_LEN; 9395 /* 9396 * If we don't have a LUN, we just leave the serial number as 9397 * all spaces. 9398 */ 9399 if (lun != NULL) { 9400 strncpy((char *)sn_ptr->serial_num, 9401 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9402 } else 9403 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9404 9405 ctl_set_success(ctsio); 9406 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9407 ctsio->be_move_done = ctl_config_move_done; 9408 ctl_datamove((union ctl_io *)ctsio); 9409 return (CTL_RETVAL_COMPLETE); 9410 } 9411 9412 9413 /* 9414 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9415 */ 9416 static int 9417 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9418 { 9419 struct ctl_lun *lun = CTL_LUN(ctsio); 9420 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9421 int data_len; 9422 9423 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9424 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9425 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9426 ctsio->kern_sg_entries = 0; 9427 ctsio->kern_rel_offset = 0; 9428 ctsio->kern_data_len = min(data_len, alloc_len); 9429 ctsio->kern_total_len = ctsio->kern_data_len; 9430 9431 /* 9432 * The control device is always connected. The disk device, on the 9433 * other hand, may not be online all the time. 9434 */ 9435 if (lun != NULL) 9436 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9437 lun->be_lun->lun_type; 9438 else 9439 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9440 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9441 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9442 /* 9443 * We support head of queue, ordered and simple tags. 9444 */ 9445 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9446 /* 9447 * Volatile cache supported. 9448 */ 9449 eid_ptr->flags3 = SVPD_EID_V_SUP; 9450 9451 /* 9452 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9453 * attention for a particular IT nexus on all LUNs once we report 9454 * it to that nexus once. This bit is required as of SPC-4. 9455 */ 9456 eid_ptr->flags4 = SVPD_EID_LUICLR; 9457 9458 /* 9459 * We support revert to defaults (RTD) bit in MODE SELECT. 9460 */ 9461 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9462 9463 /* 9464 * XXX KDM in order to correctly answer this, we would need 9465 * information from the SIM to determine how much sense data it 9466 * can send. So this would really be a path inquiry field, most 9467 * likely. This can be set to a maximum of 252 according to SPC-4, 9468 * but the hardware may or may not be able to support that much. 9469 * 0 just means that the maximum sense data length is not reported. 9470 */ 9471 eid_ptr->max_sense_length = 0; 9472 9473 ctl_set_success(ctsio); 9474 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9475 ctsio->be_move_done = ctl_config_move_done; 9476 ctl_datamove((union ctl_io *)ctsio); 9477 return (CTL_RETVAL_COMPLETE); 9478 } 9479 9480 static int 9481 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9482 { 9483 struct ctl_lun *lun = CTL_LUN(ctsio); 9484 struct scsi_vpd_mode_page_policy *mpp_ptr; 9485 int data_len; 9486 9487 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9488 sizeof(struct scsi_vpd_mode_page_policy_descr); 9489 9490 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9491 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9492 ctsio->kern_rel_offset = 0; 9493 ctsio->kern_sg_entries = 0; 9494 ctsio->kern_data_len = min(data_len, alloc_len); 9495 ctsio->kern_total_len = ctsio->kern_data_len; 9496 9497 /* 9498 * The control device is always connected. The disk device, on the 9499 * other hand, may not be online all the time. 9500 */ 9501 if (lun != NULL) 9502 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9503 lun->be_lun->lun_type; 9504 else 9505 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9506 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9507 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9508 mpp_ptr->descr[0].page_code = 0x3f; 9509 mpp_ptr->descr[0].subpage_code = 0xff; 9510 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9511 9512 ctl_set_success(ctsio); 9513 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9514 ctsio->be_move_done = ctl_config_move_done; 9515 ctl_datamove((union ctl_io *)ctsio); 9516 return (CTL_RETVAL_COMPLETE); 9517 } 9518 9519 /* 9520 * SCSI VPD page 0x83, the Device Identification page. 9521 */ 9522 static int 9523 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9524 { 9525 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9526 struct ctl_port *port = CTL_PORT(ctsio); 9527 struct ctl_lun *lun = CTL_LUN(ctsio); 9528 struct scsi_vpd_device_id *devid_ptr; 9529 struct scsi_vpd_id_descriptor *desc; 9530 int data_len, g; 9531 uint8_t proto; 9532 9533 data_len = sizeof(struct scsi_vpd_device_id) + 9534 sizeof(struct scsi_vpd_id_descriptor) + 9535 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9536 sizeof(struct scsi_vpd_id_descriptor) + 9537 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9538 if (lun && lun->lun_devid) 9539 data_len += lun->lun_devid->len; 9540 if (port && port->port_devid) 9541 data_len += port->port_devid->len; 9542 if (port && port->target_devid) 9543 data_len += port->target_devid->len; 9544 9545 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9546 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9547 ctsio->kern_sg_entries = 0; 9548 ctsio->kern_rel_offset = 0; 9549 ctsio->kern_sg_entries = 0; 9550 ctsio->kern_data_len = min(data_len, alloc_len); 9551 ctsio->kern_total_len = ctsio->kern_data_len; 9552 9553 /* 9554 * The control device is always connected. The disk device, on the 9555 * other hand, may not be online all the time. 9556 */ 9557 if (lun != NULL) 9558 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9559 lun->be_lun->lun_type; 9560 else 9561 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9562 devid_ptr->page_code = SVPD_DEVICE_ID; 9563 scsi_ulto2b(data_len - 4, devid_ptr->length); 9564 9565 if (port && port->port_type == CTL_PORT_FC) 9566 proto = SCSI_PROTO_FC << 4; 9567 else if (port && port->port_type == CTL_PORT_SAS) 9568 proto = SCSI_PROTO_SAS << 4; 9569 else if (port && port->port_type == CTL_PORT_ISCSI) 9570 proto = SCSI_PROTO_ISCSI << 4; 9571 else 9572 proto = SCSI_PROTO_SPI << 4; 9573 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9574 9575 /* 9576 * We're using a LUN association here. i.e., this device ID is a 9577 * per-LUN identifier. 9578 */ 9579 if (lun && lun->lun_devid) { 9580 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9581 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9582 lun->lun_devid->len); 9583 } 9584 9585 /* 9586 * This is for the WWPN which is a port association. 9587 */ 9588 if (port && port->port_devid) { 9589 memcpy(desc, port->port_devid->data, port->port_devid->len); 9590 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9591 port->port_devid->len); 9592 } 9593 9594 /* 9595 * This is for the Relative Target Port(type 4h) identifier 9596 */ 9597 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9598 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9599 SVPD_ID_TYPE_RELTARG; 9600 desc->length = 4; 9601 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9602 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9603 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9604 9605 /* 9606 * This is for the Target Port Group(type 5h) identifier 9607 */ 9608 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9609 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9610 SVPD_ID_TYPE_TPORTGRP; 9611 desc->length = 4; 9612 if (softc->is_single || 9613 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9614 g = 1; 9615 else 9616 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9617 scsi_ulto2b(g, &desc->identifier[2]); 9618 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9619 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9620 9621 /* 9622 * This is for the Target identifier 9623 */ 9624 if (port && port->target_devid) { 9625 memcpy(desc, port->target_devid->data, port->target_devid->len); 9626 } 9627 9628 ctl_set_success(ctsio); 9629 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9630 ctsio->be_move_done = ctl_config_move_done; 9631 ctl_datamove((union ctl_io *)ctsio); 9632 return (CTL_RETVAL_COMPLETE); 9633 } 9634 9635 static int 9636 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9637 { 9638 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9639 struct ctl_lun *lun = CTL_LUN(ctsio); 9640 struct scsi_vpd_scsi_ports *sp; 9641 struct scsi_vpd_port_designation *pd; 9642 struct scsi_vpd_port_designation_cont *pdc; 9643 struct ctl_port *port; 9644 int data_len, num_target_ports, iid_len, id_len; 9645 9646 num_target_ports = 0; 9647 iid_len = 0; 9648 id_len = 0; 9649 mtx_lock(&softc->ctl_lock); 9650 STAILQ_FOREACH(port, &softc->port_list, links) { 9651 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9652 continue; 9653 if (lun != NULL && 9654 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9655 continue; 9656 num_target_ports++; 9657 if (port->init_devid) 9658 iid_len += port->init_devid->len; 9659 if (port->port_devid) 9660 id_len += port->port_devid->len; 9661 } 9662 mtx_unlock(&softc->ctl_lock); 9663 9664 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9665 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9666 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9667 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9668 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9669 ctsio->kern_sg_entries = 0; 9670 ctsio->kern_rel_offset = 0; 9671 ctsio->kern_sg_entries = 0; 9672 ctsio->kern_data_len = min(data_len, alloc_len); 9673 ctsio->kern_total_len = ctsio->kern_data_len; 9674 9675 /* 9676 * The control device is always connected. The disk device, on the 9677 * other hand, may not be online all the time. Need to change this 9678 * to figure out whether the disk device is actually online or not. 9679 */ 9680 if (lun != NULL) 9681 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9682 lun->be_lun->lun_type; 9683 else 9684 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9685 9686 sp->page_code = SVPD_SCSI_PORTS; 9687 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9688 sp->page_length); 9689 pd = &sp->design[0]; 9690 9691 mtx_lock(&softc->ctl_lock); 9692 STAILQ_FOREACH(port, &softc->port_list, links) { 9693 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9694 continue; 9695 if (lun != NULL && 9696 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9697 continue; 9698 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9699 if (port->init_devid) { 9700 iid_len = port->init_devid->len; 9701 memcpy(pd->initiator_transportid, 9702 port->init_devid->data, port->init_devid->len); 9703 } else 9704 iid_len = 0; 9705 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9706 pdc = (struct scsi_vpd_port_designation_cont *) 9707 (&pd->initiator_transportid[iid_len]); 9708 if (port->port_devid) { 9709 id_len = port->port_devid->len; 9710 memcpy(pdc->target_port_descriptors, 9711 port->port_devid->data, port->port_devid->len); 9712 } else 9713 id_len = 0; 9714 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9715 pd = (struct scsi_vpd_port_designation *) 9716 ((uint8_t *)pdc->target_port_descriptors + id_len); 9717 } 9718 mtx_unlock(&softc->ctl_lock); 9719 9720 ctl_set_success(ctsio); 9721 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9722 ctsio->be_move_done = ctl_config_move_done; 9723 ctl_datamove((union ctl_io *)ctsio); 9724 return (CTL_RETVAL_COMPLETE); 9725 } 9726 9727 static int 9728 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len) 9729 { 9730 struct ctl_lun *lun = CTL_LUN(ctsio); 9731 struct scsi_vpd_sfs *sfs_ptr; 9732 int sfs_page_size, n; 9733 9734 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2; 9735 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO); 9736 sfs_ptr = (struct scsi_vpd_sfs *)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(sfs_page_size, 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 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9750 lun->be_lun->lun_type; 9751 else 9752 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9753 9754 sfs_ptr->page_code = SVPD_SCSI_SFS; 9755 n = 0; 9756 /* Discovery 2016 */ 9757 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]); 9758 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9759 /* SBC Base 2016 */ 9760 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]); 9761 /* SBC Base 2010 */ 9762 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]); 9763 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9764 /* Basic Provisioning 2016 */ 9765 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]); 9766 } 9767 /* Drive Maintenance 2016 */ 9768 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]); 9769 } 9770 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length); 9771 9772 ctl_set_success(ctsio); 9773 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9774 ctsio->be_move_done = ctl_config_move_done; 9775 ctl_datamove((union ctl_io *)ctsio); 9776 return (CTL_RETVAL_COMPLETE); 9777 } 9778 9779 static int 9780 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9781 { 9782 struct ctl_lun *lun = CTL_LUN(ctsio); 9783 struct scsi_vpd_block_limits *bl_ptr; 9784 const char *val; 9785 uint64_t ival; 9786 9787 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9788 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9789 ctsio->kern_sg_entries = 0; 9790 ctsio->kern_rel_offset = 0; 9791 ctsio->kern_sg_entries = 0; 9792 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9793 ctsio->kern_total_len = ctsio->kern_data_len; 9794 9795 /* 9796 * The control device is always connected. The disk device, on the 9797 * other hand, may not be online all the time. Need to change this 9798 * to figure out whether the disk device is actually online or not. 9799 */ 9800 if (lun != NULL) 9801 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9802 lun->be_lun->lun_type; 9803 else 9804 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9805 9806 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9807 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9808 bl_ptr->max_cmp_write_len = 0xff; 9809 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9810 if (lun != NULL) { 9811 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9812 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9813 ival = 0xffffffff; 9814 val = dnvlist_get_string(lun->be_lun->options, 9815 "unmap_max_lba", NULL); 9816 if (val != NULL) 9817 ctl_expand_number(val, &ival); 9818 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9819 ival = 0xffffffff; 9820 val = dnvlist_get_string(lun->be_lun->options, 9821 "unmap_max_descr", NULL); 9822 if (val != NULL) 9823 ctl_expand_number(val, &ival); 9824 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9825 if (lun->be_lun->ublockexp != 0) { 9826 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9827 bl_ptr->opt_unmap_grain); 9828 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9829 bl_ptr->unmap_grain_align); 9830 } 9831 } 9832 scsi_ulto4b(lun->be_lun->atomicblock, 9833 bl_ptr->max_atomic_transfer_length); 9834 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9835 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9836 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9837 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9838 ival = UINT64_MAX; 9839 val = dnvlist_get_string(lun->be_lun->options, 9840 "write_same_max_lba", NULL); 9841 if (val != NULL) 9842 ctl_expand_number(val, &ival); 9843 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9844 if (lun->be_lun->maxlba + 1 > ival) 9845 bl_ptr->flags |= SVPD_BL_WSNZ; 9846 } 9847 9848 ctl_set_success(ctsio); 9849 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9850 ctsio->be_move_done = ctl_config_move_done; 9851 ctl_datamove((union ctl_io *)ctsio); 9852 return (CTL_RETVAL_COMPLETE); 9853 } 9854 9855 static int 9856 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9857 { 9858 struct ctl_lun *lun = CTL_LUN(ctsio); 9859 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9860 const char *value; 9861 u_int i; 9862 9863 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9864 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9865 ctsio->kern_sg_entries = 0; 9866 ctsio->kern_rel_offset = 0; 9867 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 9868 ctsio->kern_total_len = ctsio->kern_data_len; 9869 9870 /* 9871 * The control device is always connected. The disk device, on the 9872 * other hand, may not be online all the time. Need to change this 9873 * to figure out whether the disk device is actually online or not. 9874 */ 9875 if (lun != NULL) 9876 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9877 lun->be_lun->lun_type; 9878 else 9879 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9880 bdc_ptr->page_code = SVPD_BDC; 9881 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9882 if (lun != NULL && 9883 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 9884 i = strtol(value, NULL, 0); 9885 else 9886 i = CTL_DEFAULT_ROTATION_RATE; 9887 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9888 if (lun != NULL && 9889 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 9890 i = strtol(value, NULL, 0); 9891 else 9892 i = 0; 9893 bdc_ptr->wab_wac_ff = (i & 0x0f); 9894 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS; 9895 9896 ctl_set_success(ctsio); 9897 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9898 ctsio->be_move_done = ctl_config_move_done; 9899 ctl_datamove((union ctl_io *)ctsio); 9900 return (CTL_RETVAL_COMPLETE); 9901 } 9902 9903 static int 9904 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9905 { 9906 struct ctl_lun *lun = CTL_LUN(ctsio); 9907 struct scsi_vpd_logical_block_prov *lbp_ptr; 9908 const char *value; 9909 9910 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9911 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9912 ctsio->kern_sg_entries = 0; 9913 ctsio->kern_rel_offset = 0; 9914 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 9915 ctsio->kern_total_len = ctsio->kern_data_len; 9916 9917 /* 9918 * The control device is always connected. The disk device, on the 9919 * other hand, may not be online all the time. Need to change this 9920 * to figure out whether the disk device is actually online or not. 9921 */ 9922 if (lun != NULL) 9923 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9924 lun->be_lun->lun_type; 9925 else 9926 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9927 9928 lbp_ptr->page_code = SVPD_LBP; 9929 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9930 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9931 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9932 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9933 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9934 value = dnvlist_get_string(lun->be_lun->options, 9935 "provisioning_type", NULL); 9936 if (value != NULL) { 9937 if (strcmp(value, "resource") == 0) 9938 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 9939 else if (strcmp(value, "thin") == 0) 9940 lbp_ptr->prov_type = SVPD_LBP_THIN; 9941 } else 9942 lbp_ptr->prov_type = SVPD_LBP_THIN; 9943 } 9944 9945 ctl_set_success(ctsio); 9946 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9947 ctsio->be_move_done = ctl_config_move_done; 9948 ctl_datamove((union ctl_io *)ctsio); 9949 return (CTL_RETVAL_COMPLETE); 9950 } 9951 9952 /* 9953 * INQUIRY with the EVPD bit set. 9954 */ 9955 static int 9956 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9957 { 9958 struct ctl_lun *lun = CTL_LUN(ctsio); 9959 struct scsi_inquiry *cdb; 9960 int alloc_len, retval; 9961 9962 cdb = (struct scsi_inquiry *)ctsio->cdb; 9963 alloc_len = scsi_2btoul(cdb->length); 9964 9965 switch (cdb->page_code) { 9966 case SVPD_SUPPORTED_PAGES: 9967 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9968 break; 9969 case SVPD_UNIT_SERIAL_NUMBER: 9970 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9971 break; 9972 case SVPD_DEVICE_ID: 9973 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9974 break; 9975 case SVPD_EXTENDED_INQUIRY_DATA: 9976 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9977 break; 9978 case SVPD_MODE_PAGE_POLICY: 9979 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 9980 break; 9981 case SVPD_SCSI_PORTS: 9982 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 9983 break; 9984 case SVPD_SCSI_TPC: 9985 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 9986 break; 9987 case SVPD_SCSI_SFS: 9988 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len); 9989 break; 9990 case SVPD_BLOCK_LIMITS: 9991 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9992 goto err; 9993 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9994 break; 9995 case SVPD_BDC: 9996 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9997 goto err; 9998 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 9999 break; 10000 case SVPD_LBP: 10001 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10002 goto err; 10003 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10004 break; 10005 default: 10006 err: 10007 ctl_set_invalid_field(ctsio, 10008 /*sks_valid*/ 1, 10009 /*command*/ 1, 10010 /*field*/ 2, 10011 /*bit_valid*/ 0, 10012 /*bit*/ 0); 10013 ctl_done((union ctl_io *)ctsio); 10014 retval = CTL_RETVAL_COMPLETE; 10015 break; 10016 } 10017 10018 return (retval); 10019 } 10020 10021 /* 10022 * Standard INQUIRY data. 10023 */ 10024 static int 10025 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10026 { 10027 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10028 struct ctl_port *port = CTL_PORT(ctsio); 10029 struct ctl_lun *lun = CTL_LUN(ctsio); 10030 struct scsi_inquiry_data *inq_ptr; 10031 struct scsi_inquiry *cdb; 10032 const char *val; 10033 uint32_t alloc_len, data_len; 10034 ctl_port_type port_type; 10035 10036 port_type = port->port_type; 10037 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10038 port_type = CTL_PORT_SCSI; 10039 10040 cdb = (struct scsi_inquiry *)ctsio->cdb; 10041 alloc_len = scsi_2btoul(cdb->length); 10042 10043 /* 10044 * We malloc the full inquiry data size here and fill it 10045 * in. If the user only asks for less, we'll give him 10046 * that much. 10047 */ 10048 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10049 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10050 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10051 ctsio->kern_sg_entries = 0; 10052 ctsio->kern_rel_offset = 0; 10053 ctsio->kern_data_len = min(data_len, alloc_len); 10054 ctsio->kern_total_len = ctsio->kern_data_len; 10055 10056 if (lun != NULL) { 10057 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10058 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10059 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10060 lun->be_lun->lun_type; 10061 } else { 10062 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10063 lun->be_lun->lun_type; 10064 } 10065 if (lun->flags & CTL_LUN_REMOVABLE) 10066 inq_ptr->dev_qual2 |= SID_RMB; 10067 } else 10068 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10069 10070 /* RMB in byte 2 is 0 */ 10071 inq_ptr->version = SCSI_REV_SPC5; 10072 10073 /* 10074 * According to SAM-3, even if a device only supports a single 10075 * level of LUN addressing, it should still set the HISUP bit: 10076 * 10077 * 4.9.1 Logical unit numbers overview 10078 * 10079 * All logical unit number formats described in this standard are 10080 * hierarchical in structure even when only a single level in that 10081 * hierarchy is used. The HISUP bit shall be set to one in the 10082 * standard INQUIRY data (see SPC-2) when any logical unit number 10083 * format described in this standard is used. Non-hierarchical 10084 * formats are outside the scope of this standard. 10085 * 10086 * Therefore we set the HiSup bit here. 10087 * 10088 * The response format is 2, per SPC-3. 10089 */ 10090 inq_ptr->response_format = SID_HiSup | 2; 10091 10092 inq_ptr->additional_length = data_len - 10093 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10094 CTL_DEBUG_PRINT(("additional_length = %d\n", 10095 inq_ptr->additional_length)); 10096 10097 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10098 if (port_type == CTL_PORT_SCSI) 10099 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10100 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10101 inq_ptr->flags = SID_CmdQue; 10102 if (port_type == CTL_PORT_SCSI) 10103 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10104 10105 /* 10106 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10107 * We have 8 bytes for the vendor name, and 16 bytes for the device 10108 * name and 4 bytes for the revision. 10109 */ 10110 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10111 "vendor", NULL)) == NULL) { 10112 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10113 } else { 10114 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10115 strncpy(inq_ptr->vendor, val, 10116 min(sizeof(inq_ptr->vendor), strlen(val))); 10117 } 10118 if (lun == NULL) { 10119 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10120 sizeof(inq_ptr->product)); 10121 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10122 NULL)) == NULL) { 10123 switch (lun->be_lun->lun_type) { 10124 case T_DIRECT: 10125 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10126 sizeof(inq_ptr->product)); 10127 break; 10128 case T_PROCESSOR: 10129 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10130 sizeof(inq_ptr->product)); 10131 break; 10132 case T_CDROM: 10133 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10134 sizeof(inq_ptr->product)); 10135 break; 10136 default: 10137 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10138 sizeof(inq_ptr->product)); 10139 break; 10140 } 10141 } else { 10142 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10143 strncpy(inq_ptr->product, val, 10144 min(sizeof(inq_ptr->product), strlen(val))); 10145 } 10146 10147 /* 10148 * XXX make this a macro somewhere so it automatically gets 10149 * incremented when we make changes. 10150 */ 10151 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10152 "revision", NULL)) == NULL) { 10153 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10154 } else { 10155 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10156 strncpy(inq_ptr->revision, val, 10157 min(sizeof(inq_ptr->revision), strlen(val))); 10158 } 10159 10160 /* 10161 * For parallel SCSI, we support double transition and single 10162 * transition clocking. We also support QAS (Quick Arbitration 10163 * and Selection) and Information Unit transfers on both the 10164 * control and array devices. 10165 */ 10166 if (port_type == CTL_PORT_SCSI) 10167 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10168 SID_SPI_IUS; 10169 10170 /* SAM-6 (no version claimed) */ 10171 scsi_ulto2b(0x00C0, inq_ptr->version1); 10172 /* SPC-5 (no version claimed) */ 10173 scsi_ulto2b(0x05C0, inq_ptr->version2); 10174 if (port_type == CTL_PORT_FC) { 10175 /* FCP-2 ANSI INCITS.350:2003 */ 10176 scsi_ulto2b(0x0917, inq_ptr->version3); 10177 } else if (port_type == CTL_PORT_SCSI) { 10178 /* SPI-4 ANSI INCITS.362:200x */ 10179 scsi_ulto2b(0x0B56, inq_ptr->version3); 10180 } else if (port_type == CTL_PORT_ISCSI) { 10181 /* iSCSI (no version claimed) */ 10182 scsi_ulto2b(0x0960, inq_ptr->version3); 10183 } else if (port_type == CTL_PORT_SAS) { 10184 /* SAS (no version claimed) */ 10185 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10186 } else if (port_type == CTL_PORT_UMASS) { 10187 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10188 scsi_ulto2b(0x1730, inq_ptr->version3); 10189 } 10190 10191 if (lun == NULL) { 10192 /* SBC-4 (no version claimed) */ 10193 scsi_ulto2b(0x0600, inq_ptr->version4); 10194 } else { 10195 switch (lun->be_lun->lun_type) { 10196 case T_DIRECT: 10197 /* SBC-4 (no version claimed) */ 10198 scsi_ulto2b(0x0600, inq_ptr->version4); 10199 break; 10200 case T_PROCESSOR: 10201 break; 10202 case T_CDROM: 10203 /* MMC-6 (no version claimed) */ 10204 scsi_ulto2b(0x04E0, inq_ptr->version4); 10205 break; 10206 default: 10207 break; 10208 } 10209 } 10210 10211 ctl_set_success(ctsio); 10212 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10213 ctsio->be_move_done = ctl_config_move_done; 10214 ctl_datamove((union ctl_io *)ctsio); 10215 return (CTL_RETVAL_COMPLETE); 10216 } 10217 10218 int 10219 ctl_inquiry(struct ctl_scsiio *ctsio) 10220 { 10221 struct scsi_inquiry *cdb; 10222 int retval; 10223 10224 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10225 10226 cdb = (struct scsi_inquiry *)ctsio->cdb; 10227 if (cdb->byte2 & SI_EVPD) 10228 retval = ctl_inquiry_evpd(ctsio); 10229 else if (cdb->page_code == 0) 10230 retval = ctl_inquiry_std(ctsio); 10231 else { 10232 ctl_set_invalid_field(ctsio, 10233 /*sks_valid*/ 1, 10234 /*command*/ 1, 10235 /*field*/ 2, 10236 /*bit_valid*/ 0, 10237 /*bit*/ 0); 10238 ctl_done((union ctl_io *)ctsio); 10239 return (CTL_RETVAL_COMPLETE); 10240 } 10241 10242 return (retval); 10243 } 10244 10245 int 10246 ctl_get_config(struct ctl_scsiio *ctsio) 10247 { 10248 struct ctl_lun *lun = CTL_LUN(ctsio); 10249 struct scsi_get_config_header *hdr; 10250 struct scsi_get_config_feature *feature; 10251 struct scsi_get_config *cdb; 10252 uint32_t alloc_len, data_len; 10253 int rt, starting; 10254 10255 cdb = (struct scsi_get_config *)ctsio->cdb; 10256 rt = (cdb->rt & SGC_RT_MASK); 10257 starting = scsi_2btoul(cdb->starting_feature); 10258 alloc_len = scsi_2btoul(cdb->length); 10259 10260 data_len = sizeof(struct scsi_get_config_header) + 10261 sizeof(struct scsi_get_config_feature) + 8 + 10262 sizeof(struct scsi_get_config_feature) + 8 + 10263 sizeof(struct scsi_get_config_feature) + 4 + 10264 sizeof(struct scsi_get_config_feature) + 4 + 10265 sizeof(struct scsi_get_config_feature) + 8 + 10266 sizeof(struct scsi_get_config_feature) + 10267 sizeof(struct scsi_get_config_feature) + 4 + 10268 sizeof(struct scsi_get_config_feature) + 4 + 10269 sizeof(struct scsi_get_config_feature) + 4 + 10270 sizeof(struct scsi_get_config_feature) + 4 + 10271 sizeof(struct scsi_get_config_feature) + 4 + 10272 sizeof(struct scsi_get_config_feature) + 4; 10273 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10274 ctsio->kern_sg_entries = 0; 10275 ctsio->kern_rel_offset = 0; 10276 10277 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10278 if (lun->flags & CTL_LUN_NO_MEDIA) 10279 scsi_ulto2b(0x0000, hdr->current_profile); 10280 else 10281 scsi_ulto2b(0x0010, hdr->current_profile); 10282 feature = (struct scsi_get_config_feature *)(hdr + 1); 10283 10284 if (starting > 0x003b) 10285 goto done; 10286 if (starting > 0x003a) 10287 goto f3b; 10288 if (starting > 0x002b) 10289 goto f3a; 10290 if (starting > 0x002a) 10291 goto f2b; 10292 if (starting > 0x001f) 10293 goto f2a; 10294 if (starting > 0x001e) 10295 goto f1f; 10296 if (starting > 0x001d) 10297 goto f1e; 10298 if (starting > 0x0010) 10299 goto f1d; 10300 if (starting > 0x0003) 10301 goto f10; 10302 if (starting > 0x0002) 10303 goto f3; 10304 if (starting > 0x0001) 10305 goto f2; 10306 if (starting > 0x0000) 10307 goto f1; 10308 10309 /* Profile List */ 10310 scsi_ulto2b(0x0000, feature->feature_code); 10311 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10312 feature->add_length = 8; 10313 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10314 feature->feature_data[2] = 0x00; 10315 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10316 feature->feature_data[6] = 0x01; 10317 feature = (struct scsi_get_config_feature *) 10318 &feature->feature_data[feature->add_length]; 10319 10320 f1: /* Core */ 10321 scsi_ulto2b(0x0001, feature->feature_code); 10322 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10323 feature->add_length = 8; 10324 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10325 feature->feature_data[4] = 0x03; 10326 feature = (struct scsi_get_config_feature *) 10327 &feature->feature_data[feature->add_length]; 10328 10329 f2: /* Morphing */ 10330 scsi_ulto2b(0x0002, feature->feature_code); 10331 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10332 feature->add_length = 4; 10333 feature->feature_data[0] = 0x02; 10334 feature = (struct scsi_get_config_feature *) 10335 &feature->feature_data[feature->add_length]; 10336 10337 f3: /* Removable Medium */ 10338 scsi_ulto2b(0x0003, feature->feature_code); 10339 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10340 feature->add_length = 4; 10341 feature->feature_data[0] = 0x39; 10342 feature = (struct scsi_get_config_feature *) 10343 &feature->feature_data[feature->add_length]; 10344 10345 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10346 goto done; 10347 10348 f10: /* Random Read */ 10349 scsi_ulto2b(0x0010, feature->feature_code); 10350 feature->flags = 0x00; 10351 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10352 feature->flags |= SGC_F_CURRENT; 10353 feature->add_length = 8; 10354 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10355 scsi_ulto2b(1, &feature->feature_data[4]); 10356 feature->feature_data[6] = 0x00; 10357 feature = (struct scsi_get_config_feature *) 10358 &feature->feature_data[feature->add_length]; 10359 10360 f1d: /* Multi-Read */ 10361 scsi_ulto2b(0x001D, feature->feature_code); 10362 feature->flags = 0x00; 10363 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10364 feature->flags |= SGC_F_CURRENT; 10365 feature->add_length = 0; 10366 feature = (struct scsi_get_config_feature *) 10367 &feature->feature_data[feature->add_length]; 10368 10369 f1e: /* CD Read */ 10370 scsi_ulto2b(0x001E, feature->feature_code); 10371 feature->flags = 0x00; 10372 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10373 feature->flags |= SGC_F_CURRENT; 10374 feature->add_length = 4; 10375 feature->feature_data[0] = 0x00; 10376 feature = (struct scsi_get_config_feature *) 10377 &feature->feature_data[feature->add_length]; 10378 10379 f1f: /* DVD Read */ 10380 scsi_ulto2b(0x001F, feature->feature_code); 10381 feature->flags = 0x08; 10382 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10383 feature->flags |= SGC_F_CURRENT; 10384 feature->add_length = 4; 10385 feature->feature_data[0] = 0x01; 10386 feature->feature_data[2] = 0x03; 10387 feature = (struct scsi_get_config_feature *) 10388 &feature->feature_data[feature->add_length]; 10389 10390 f2a: /* DVD+RW */ 10391 scsi_ulto2b(0x002A, feature->feature_code); 10392 feature->flags = 0x04; 10393 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10394 feature->flags |= SGC_F_CURRENT; 10395 feature->add_length = 4; 10396 feature->feature_data[0] = 0x00; 10397 feature->feature_data[1] = 0x00; 10398 feature = (struct scsi_get_config_feature *) 10399 &feature->feature_data[feature->add_length]; 10400 10401 f2b: /* DVD+R */ 10402 scsi_ulto2b(0x002B, feature->feature_code); 10403 feature->flags = 0x00; 10404 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10405 feature->flags |= SGC_F_CURRENT; 10406 feature->add_length = 4; 10407 feature->feature_data[0] = 0x00; 10408 feature = (struct scsi_get_config_feature *) 10409 &feature->feature_data[feature->add_length]; 10410 10411 f3a: /* DVD+RW Dual Layer */ 10412 scsi_ulto2b(0x003A, feature->feature_code); 10413 feature->flags = 0x00; 10414 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10415 feature->flags |= SGC_F_CURRENT; 10416 feature->add_length = 4; 10417 feature->feature_data[0] = 0x00; 10418 feature->feature_data[1] = 0x00; 10419 feature = (struct scsi_get_config_feature *) 10420 &feature->feature_data[feature->add_length]; 10421 10422 f3b: /* DVD+R Dual Layer */ 10423 scsi_ulto2b(0x003B, feature->feature_code); 10424 feature->flags = 0x00; 10425 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10426 feature->flags |= SGC_F_CURRENT; 10427 feature->add_length = 4; 10428 feature->feature_data[0] = 0x00; 10429 feature = (struct scsi_get_config_feature *) 10430 &feature->feature_data[feature->add_length]; 10431 10432 done: 10433 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10434 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10435 feature = (struct scsi_get_config_feature *)(hdr + 1); 10436 if (scsi_2btoul(feature->feature_code) == starting) 10437 feature = (struct scsi_get_config_feature *) 10438 &feature->feature_data[feature->add_length]; 10439 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10440 } 10441 scsi_ulto4b(data_len - 4, hdr->data_length); 10442 ctsio->kern_data_len = min(data_len, alloc_len); 10443 ctsio->kern_total_len = ctsio->kern_data_len; 10444 10445 ctl_set_success(ctsio); 10446 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10447 ctsio->be_move_done = ctl_config_move_done; 10448 ctl_datamove((union ctl_io *)ctsio); 10449 return (CTL_RETVAL_COMPLETE); 10450 } 10451 10452 int 10453 ctl_get_event_status(struct ctl_scsiio *ctsio) 10454 { 10455 struct scsi_get_event_status_header *hdr; 10456 struct scsi_get_event_status *cdb; 10457 uint32_t alloc_len, data_len; 10458 10459 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10460 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10461 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10462 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10463 ctl_done((union ctl_io *)ctsio); 10464 return (CTL_RETVAL_COMPLETE); 10465 } 10466 alloc_len = scsi_2btoul(cdb->length); 10467 10468 data_len = sizeof(struct scsi_get_event_status_header); 10469 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10470 ctsio->kern_sg_entries = 0; 10471 ctsio->kern_rel_offset = 0; 10472 ctsio->kern_data_len = min(data_len, alloc_len); 10473 ctsio->kern_total_len = ctsio->kern_data_len; 10474 10475 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10476 scsi_ulto2b(0, hdr->descr_length); 10477 hdr->nea_class = SGESN_NEA; 10478 hdr->supported_class = 0; 10479 10480 ctl_set_success(ctsio); 10481 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10482 ctsio->be_move_done = ctl_config_move_done; 10483 ctl_datamove((union ctl_io *)ctsio); 10484 return (CTL_RETVAL_COMPLETE); 10485 } 10486 10487 int 10488 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10489 { 10490 struct scsi_mechanism_status_header *hdr; 10491 struct scsi_mechanism_status *cdb; 10492 uint32_t alloc_len, data_len; 10493 10494 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10495 alloc_len = scsi_2btoul(cdb->length); 10496 10497 data_len = sizeof(struct scsi_mechanism_status_header); 10498 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10499 ctsio->kern_sg_entries = 0; 10500 ctsio->kern_rel_offset = 0; 10501 ctsio->kern_data_len = min(data_len, alloc_len); 10502 ctsio->kern_total_len = ctsio->kern_data_len; 10503 10504 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10505 hdr->state1 = 0x00; 10506 hdr->state2 = 0xe0; 10507 scsi_ulto3b(0, hdr->lba); 10508 hdr->slots_num = 0; 10509 scsi_ulto2b(0, hdr->slots_length); 10510 10511 ctl_set_success(ctsio); 10512 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10513 ctsio->be_move_done = ctl_config_move_done; 10514 ctl_datamove((union ctl_io *)ctsio); 10515 return (CTL_RETVAL_COMPLETE); 10516 } 10517 10518 static void 10519 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10520 { 10521 10522 lba += 150; 10523 buf[0] = 0; 10524 buf[1] = bin2bcd((lba / 75) / 60); 10525 buf[2] = bin2bcd((lba / 75) % 60); 10526 buf[3] = bin2bcd(lba % 75); 10527 } 10528 10529 int 10530 ctl_read_toc(struct ctl_scsiio *ctsio) 10531 { 10532 struct ctl_lun *lun = CTL_LUN(ctsio); 10533 struct scsi_read_toc_hdr *hdr; 10534 struct scsi_read_toc_type01_descr *descr; 10535 struct scsi_read_toc *cdb; 10536 uint32_t alloc_len, data_len; 10537 int format, msf; 10538 10539 cdb = (struct scsi_read_toc *)ctsio->cdb; 10540 msf = (cdb->byte2 & CD_MSF) != 0; 10541 format = cdb->format; 10542 alloc_len = scsi_2btoul(cdb->data_len); 10543 10544 data_len = sizeof(struct scsi_read_toc_hdr); 10545 if (format == 0) 10546 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10547 else 10548 data_len += sizeof(struct scsi_read_toc_type01_descr); 10549 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10550 ctsio->kern_sg_entries = 0; 10551 ctsio->kern_rel_offset = 0; 10552 ctsio->kern_data_len = min(data_len, alloc_len); 10553 ctsio->kern_total_len = ctsio->kern_data_len; 10554 10555 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10556 if (format == 0) { 10557 scsi_ulto2b(0x12, hdr->data_length); 10558 hdr->first = 1; 10559 hdr->last = 1; 10560 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10561 descr->addr_ctl = 0x14; 10562 descr->track_number = 1; 10563 if (msf) 10564 ctl_ultomsf(0, descr->track_start); 10565 else 10566 scsi_ulto4b(0, descr->track_start); 10567 descr++; 10568 descr->addr_ctl = 0x14; 10569 descr->track_number = 0xaa; 10570 if (msf) 10571 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10572 else 10573 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10574 } else { 10575 scsi_ulto2b(0x0a, hdr->data_length); 10576 hdr->first = 1; 10577 hdr->last = 1; 10578 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10579 descr->addr_ctl = 0x14; 10580 descr->track_number = 1; 10581 if (msf) 10582 ctl_ultomsf(0, descr->track_start); 10583 else 10584 scsi_ulto4b(0, descr->track_start); 10585 } 10586 10587 ctl_set_success(ctsio); 10588 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10589 ctsio->be_move_done = ctl_config_move_done; 10590 ctl_datamove((union ctl_io *)ctsio); 10591 return (CTL_RETVAL_COMPLETE); 10592 } 10593 10594 /* 10595 * For known CDB types, parse the LBA and length. 10596 */ 10597 static int 10598 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10599 { 10600 if (io->io_hdr.io_type != CTL_IO_SCSI) 10601 return (1); 10602 10603 switch (io->scsiio.cdb[0]) { 10604 case COMPARE_AND_WRITE: { 10605 struct scsi_compare_and_write *cdb; 10606 10607 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10608 10609 *lba = scsi_8btou64(cdb->addr); 10610 *len = cdb->length; 10611 break; 10612 } 10613 case READ_6: 10614 case WRITE_6: { 10615 struct scsi_rw_6 *cdb; 10616 10617 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10618 10619 *lba = scsi_3btoul(cdb->addr); 10620 /* only 5 bits are valid in the most significant address byte */ 10621 *lba &= 0x1fffff; 10622 *len = cdb->length; 10623 break; 10624 } 10625 case READ_10: 10626 case WRITE_10: { 10627 struct scsi_rw_10 *cdb; 10628 10629 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10630 10631 *lba = scsi_4btoul(cdb->addr); 10632 *len = scsi_2btoul(cdb->length); 10633 break; 10634 } 10635 case WRITE_VERIFY_10: { 10636 struct scsi_write_verify_10 *cdb; 10637 10638 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10639 10640 *lba = scsi_4btoul(cdb->addr); 10641 *len = scsi_2btoul(cdb->length); 10642 break; 10643 } 10644 case READ_12: 10645 case WRITE_12: { 10646 struct scsi_rw_12 *cdb; 10647 10648 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10649 10650 *lba = scsi_4btoul(cdb->addr); 10651 *len = scsi_4btoul(cdb->length); 10652 break; 10653 } 10654 case WRITE_VERIFY_12: { 10655 struct scsi_write_verify_12 *cdb; 10656 10657 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10658 10659 *lba = scsi_4btoul(cdb->addr); 10660 *len = scsi_4btoul(cdb->length); 10661 break; 10662 } 10663 case READ_16: 10664 case WRITE_16: { 10665 struct scsi_rw_16 *cdb; 10666 10667 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10668 10669 *lba = scsi_8btou64(cdb->addr); 10670 *len = scsi_4btoul(cdb->length); 10671 break; 10672 } 10673 case WRITE_ATOMIC_16: { 10674 struct scsi_write_atomic_16 *cdb; 10675 10676 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10677 10678 *lba = scsi_8btou64(cdb->addr); 10679 *len = scsi_2btoul(cdb->length); 10680 break; 10681 } 10682 case WRITE_VERIFY_16: { 10683 struct scsi_write_verify_16 *cdb; 10684 10685 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10686 10687 *lba = scsi_8btou64(cdb->addr); 10688 *len = scsi_4btoul(cdb->length); 10689 break; 10690 } 10691 case WRITE_SAME_10: { 10692 struct scsi_write_same_10 *cdb; 10693 10694 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10695 10696 *lba = scsi_4btoul(cdb->addr); 10697 *len = scsi_2btoul(cdb->length); 10698 break; 10699 } 10700 case WRITE_SAME_16: { 10701 struct scsi_write_same_16 *cdb; 10702 10703 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10704 10705 *lba = scsi_8btou64(cdb->addr); 10706 *len = scsi_4btoul(cdb->length); 10707 break; 10708 } 10709 case VERIFY_10: { 10710 struct scsi_verify_10 *cdb; 10711 10712 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10713 10714 *lba = scsi_4btoul(cdb->addr); 10715 *len = scsi_2btoul(cdb->length); 10716 break; 10717 } 10718 case VERIFY_12: { 10719 struct scsi_verify_12 *cdb; 10720 10721 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10722 10723 *lba = scsi_4btoul(cdb->addr); 10724 *len = scsi_4btoul(cdb->length); 10725 break; 10726 } 10727 case VERIFY_16: { 10728 struct scsi_verify_16 *cdb; 10729 10730 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10731 10732 *lba = scsi_8btou64(cdb->addr); 10733 *len = scsi_4btoul(cdb->length); 10734 break; 10735 } 10736 case UNMAP: { 10737 *lba = 0; 10738 *len = UINT64_MAX; 10739 break; 10740 } 10741 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10742 struct scsi_get_lba_status *cdb; 10743 10744 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10745 *lba = scsi_8btou64(cdb->addr); 10746 *len = UINT32_MAX; 10747 break; 10748 } 10749 default: 10750 return (1); 10751 break; /* NOTREACHED */ 10752 } 10753 10754 return (0); 10755 } 10756 10757 static ctl_action 10758 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10759 bool seq) 10760 { 10761 uint64_t endlba1, endlba2; 10762 10763 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10764 endlba2 = lba2 + len2 - 1; 10765 10766 if ((endlba1 < lba2) || (endlba2 < lba1)) 10767 return (CTL_ACTION_PASS); 10768 else 10769 return (CTL_ACTION_BLOCK); 10770 } 10771 10772 static int 10773 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10774 { 10775 struct ctl_ptr_len_flags *ptrlen; 10776 struct scsi_unmap_desc *buf, *end, *range; 10777 uint64_t lba; 10778 uint32_t len; 10779 10780 /* If not UNMAP -- go other way. */ 10781 if (io->io_hdr.io_type != CTL_IO_SCSI || 10782 io->scsiio.cdb[0] != UNMAP) 10783 return (CTL_ACTION_ERROR); 10784 10785 /* If UNMAP without data -- block and wait for data. */ 10786 ptrlen = (struct ctl_ptr_len_flags *) 10787 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10788 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10789 ptrlen->ptr == NULL) 10790 return (CTL_ACTION_BLOCK); 10791 10792 /* UNMAP with data -- check for collision. */ 10793 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10794 end = buf + ptrlen->len / sizeof(*buf); 10795 for (range = buf; range < end; range++) { 10796 lba = scsi_8btou64(range->lba); 10797 len = scsi_4btoul(range->length); 10798 if ((lba < lba2 + len2) && (lba + len > lba2)) 10799 return (CTL_ACTION_BLOCK); 10800 } 10801 return (CTL_ACTION_PASS); 10802 } 10803 10804 static ctl_action 10805 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10806 { 10807 uint64_t lba1, lba2; 10808 uint64_t len1, len2; 10809 int retval; 10810 10811 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10812 return (CTL_ACTION_ERROR); 10813 10814 retval = ctl_extent_check_unmap(io1, lba2, len2); 10815 if (retval != CTL_ACTION_ERROR) 10816 return (retval); 10817 10818 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10819 return (CTL_ACTION_ERROR); 10820 10821 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10822 seq = FALSE; 10823 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10824 } 10825 10826 static ctl_action 10827 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10828 { 10829 uint64_t lba1, lba2; 10830 uint64_t len1, len2; 10831 10832 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10833 return (CTL_ACTION_PASS); 10834 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10835 return (CTL_ACTION_ERROR); 10836 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10837 return (CTL_ACTION_ERROR); 10838 10839 if (lba1 + len1 == lba2) 10840 return (CTL_ACTION_BLOCK); 10841 return (CTL_ACTION_PASS); 10842 } 10843 10844 static ctl_action 10845 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10846 union ctl_io *ooa_io) 10847 { 10848 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10849 const ctl_serialize_action *serialize_row; 10850 10851 /* 10852 * Aborted commands are not going to be executed and may even 10853 * not report completion, so we don't care about their order. 10854 * Let them complete ASAP to clean the OOA queue. 10855 */ 10856 if (pending_io->io_hdr.flags & CTL_FLAG_ABORT) 10857 return (CTL_ACTION_SKIP); 10858 10859 /* 10860 * The initiator attempted multiple untagged commands at the same 10861 * time. Can't do that. 10862 */ 10863 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10864 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10865 && ((pending_io->io_hdr.nexus.targ_port == 10866 ooa_io->io_hdr.nexus.targ_port) 10867 && (pending_io->io_hdr.nexus.initid == 10868 ooa_io->io_hdr.nexus.initid)) 10869 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10870 CTL_FLAG_STATUS_SENT)) == 0)) 10871 return (CTL_ACTION_OVERLAP); 10872 10873 /* 10874 * The initiator attempted to send multiple tagged commands with 10875 * the same ID. (It's fine if different initiators have the same 10876 * tag ID.) 10877 * 10878 * Even if all of those conditions are true, we don't kill the I/O 10879 * if the command ahead of us has been aborted. We won't end up 10880 * sending it to the FETD, and it's perfectly legal to resend a 10881 * command with the same tag number as long as the previous 10882 * instance of this tag number has been aborted somehow. 10883 */ 10884 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10885 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10886 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10887 && ((pending_io->io_hdr.nexus.targ_port == 10888 ooa_io->io_hdr.nexus.targ_port) 10889 && (pending_io->io_hdr.nexus.initid == 10890 ooa_io->io_hdr.nexus.initid)) 10891 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10892 CTL_FLAG_STATUS_SENT)) == 0)) 10893 return (CTL_ACTION_OVERLAP_TAG); 10894 10895 /* 10896 * If we get a head of queue tag, SAM-3 says that we should 10897 * immediately execute it. 10898 * 10899 * What happens if this command would normally block for some other 10900 * reason? e.g. a request sense with a head of queue tag 10901 * immediately after a write. Normally that would block, but this 10902 * will result in its getting executed immediately... 10903 * 10904 * We currently return "pass" instead of "skip", so we'll end up 10905 * going through the rest of the queue to check for overlapped tags. 10906 * 10907 * XXX KDM check for other types of blockage first?? 10908 */ 10909 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10910 return (CTL_ACTION_PASS); 10911 10912 /* 10913 * Ordered tags have to block until all items ahead of them 10914 * have completed. If we get called with an ordered tag, we always 10915 * block, if something else is ahead of us in the queue. 10916 */ 10917 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10918 return (CTL_ACTION_BLOCK); 10919 10920 /* 10921 * Simple tags get blocked until all head of queue and ordered tags 10922 * ahead of them have completed. I'm lumping untagged commands in 10923 * with simple tags here. XXX KDM is that the right thing to do? 10924 */ 10925 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10926 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10927 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10928 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10929 return (CTL_ACTION_BLOCK); 10930 10931 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10932 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 10933 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 10934 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 10935 pending_io->scsiio.cdb[1], pending_io)); 10936 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10937 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 10938 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 10939 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 10940 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 10941 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 10942 ooa_io->scsiio.cdb[1], ooa_io)); 10943 10944 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10945 10946 switch (serialize_row[pending_entry->seridx]) { 10947 case CTL_SER_BLOCK: 10948 return (CTL_ACTION_BLOCK); 10949 case CTL_SER_EXTENT: 10950 return (ctl_extent_check(ooa_io, pending_io, 10951 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10952 case CTL_SER_EXTENTOPT: 10953 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10954 SCP_QUEUE_ALG_UNRESTRICTED) 10955 return (ctl_extent_check(ooa_io, pending_io, 10956 (lun->be_lun && 10957 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10958 return (CTL_ACTION_PASS); 10959 case CTL_SER_EXTENTSEQ: 10960 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10961 return (ctl_extent_check_seq(ooa_io, pending_io)); 10962 return (CTL_ACTION_PASS); 10963 case CTL_SER_PASS: 10964 return (CTL_ACTION_PASS); 10965 case CTL_SER_BLOCKOPT: 10966 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10967 SCP_QUEUE_ALG_UNRESTRICTED) 10968 return (CTL_ACTION_BLOCK); 10969 return (CTL_ACTION_PASS); 10970 case CTL_SER_SKIP: 10971 return (CTL_ACTION_SKIP); 10972 default: 10973 panic("%s: Invalid serialization value %d for %d => %d", 10974 __func__, serialize_row[pending_entry->seridx], 10975 pending_entry->seridx, ooa_entry->seridx); 10976 } 10977 10978 return (CTL_ACTION_ERROR); 10979 } 10980 10981 /* 10982 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10983 * Assumptions: 10984 * - pending_io is generally either incoming, or on the blocked queue 10985 * - starting I/O is the I/O we want to start the check with. 10986 */ 10987 static ctl_action 10988 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10989 union ctl_io **starting_io) 10990 { 10991 union ctl_io *ooa_io; 10992 ctl_action action; 10993 10994 mtx_assert(&lun->lun_lock, MA_OWNED); 10995 10996 /* 10997 * Run back along the OOA queue, starting with the current 10998 * blocked I/O and going through every I/O before it on the 10999 * queue. If starting_io is NULL, we'll just end up returning 11000 * CTL_ACTION_PASS. 11001 */ 11002 for (ooa_io = *starting_io; ooa_io != NULL; 11003 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11004 ooa_links)){ 11005 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11006 if (action != CTL_ACTION_PASS) { 11007 *starting_io = ooa_io; 11008 return (action); 11009 } 11010 } 11011 11012 *starting_io = NULL; 11013 return (CTL_ACTION_PASS); 11014 } 11015 11016 /* 11017 * Try to unblock the specified I/O. 11018 * 11019 * skip parameter allows explicitly skip present blocker of the I/O, 11020 * starting from the previous one on OOA queue. It can be used when 11021 * we know for sure that the blocker I/O does no longer count. 11022 */ 11023 static void 11024 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11025 { 11026 struct ctl_softc *softc = lun->ctl_softc; 11027 union ctl_io *bio, *obio; 11028 const struct ctl_cmd_entry *entry; 11029 union ctl_ha_msg msg_info; 11030 ctl_action action; 11031 11032 mtx_assert(&lun->lun_lock, MA_OWNED); 11033 11034 if (io->io_hdr.blocker == NULL) 11035 return; 11036 11037 obio = bio = io->io_hdr.blocker; 11038 if (skip) 11039 bio = (union ctl_io *)TAILQ_PREV(&bio->io_hdr, ctl_ooaq, 11040 ooa_links); 11041 action = ctl_check_ooa(lun, io, &bio); 11042 if (action == CTL_ACTION_BLOCK) { 11043 /* Still blocked, but may be by different I/O now. */ 11044 if (bio != obio) { 11045 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, 11046 &io->io_hdr, blocked_links); 11047 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, 11048 &io->io_hdr, blocked_links); 11049 io->io_hdr.blocker = bio; 11050 } 11051 return; 11052 } 11053 11054 /* No longer blocked, one way or another. */ 11055 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links); 11056 io->io_hdr.blocker = NULL; 11057 11058 switch (action) { 11059 case CTL_ACTION_OVERLAP: 11060 ctl_set_overlapped_cmd(&io->scsiio); 11061 goto error; 11062 case CTL_ACTION_OVERLAP_TAG: 11063 ctl_set_overlapped_tag(&io->scsiio, 11064 io->scsiio.tag_num & 0xff); 11065 goto error; 11066 case CTL_ACTION_PASS: 11067 case CTL_ACTION_SKIP: 11068 11069 /* Serializing commands from the other SC retire there. */ 11070 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11071 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11072 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11073 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11074 msg_info.hdr.serializing_sc = io; 11075 msg_info.hdr.msg_type = CTL_MSG_R2R; 11076 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11077 sizeof(msg_info.hdr), M_NOWAIT); 11078 break; 11079 } 11080 11081 /* 11082 * Check this I/O for LUN state changes that may have happened 11083 * while this command was blocked. The LUN state may have been 11084 * changed by a command ahead of us in the queue. 11085 */ 11086 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11087 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 11088 ctl_done(io); 11089 break; 11090 } 11091 11092 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11093 ctl_enqueue_rtr(io); 11094 break; 11095 case CTL_ACTION_ERROR: 11096 default: 11097 ctl_set_internal_failure(&io->scsiio, 11098 /*sks_valid*/ 0, 11099 /*retry_count*/ 0); 11100 11101 error: 11102 /* Serializing commands from the other SC are done here. */ 11103 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11104 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11105 ctl_try_unblock_others(lun, io, TRUE); 11106 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 11107 11108 ctl_copy_sense_data_back(io, &msg_info); 11109 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11110 msg_info.hdr.serializing_sc = NULL; 11111 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 11112 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11113 sizeof(msg_info.scsi), M_WAITOK); 11114 ctl_free_io(io); 11115 break; 11116 } 11117 11118 ctl_done(io); 11119 break; 11120 } 11121 } 11122 11123 /* 11124 * Try to unblock I/Os blocked by the specified I/O. 11125 * 11126 * skip parameter allows explicitly skip the specified I/O as blocker, 11127 * starting from the previous one on the OOA queue. It can be used when 11128 * we know for sure that the specified I/O does no longer count (done). 11129 * It has to be still on OOA queue though so that we know where to start. 11130 */ 11131 static void 11132 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip) 11133 { 11134 union ctl_io *io, *next_io; 11135 11136 mtx_assert(&lun->lun_lock, MA_OWNED); 11137 11138 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue); 11139 io != NULL; io = next_io) { 11140 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links); 11141 11142 KASSERT(io->io_hdr.blocker != NULL, 11143 ("I/O %p on blocked list without blocker", io)); 11144 ctl_try_unblock_io(lun, io, skip); 11145 } 11146 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue), 11147 ("blocked_queue is not empty after skipping %p", bio)); 11148 } 11149 11150 /* 11151 * This routine (with one exception) checks LUN flags that can be set by 11152 * commands ahead of us in the OOA queue. These flags have to be checked 11153 * when a command initially comes in, and when we pull a command off the 11154 * blocked queue and are preparing to execute it. The reason we have to 11155 * check these flags for commands on the blocked queue is that the LUN 11156 * state may have been changed by a command ahead of us while we're on the 11157 * blocked queue. 11158 * 11159 * Ordering is somewhat important with these checks, so please pay 11160 * careful attention to the placement of any new checks. 11161 */ 11162 static int 11163 ctl_scsiio_lun_check(struct ctl_lun *lun, 11164 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11165 { 11166 struct ctl_softc *softc = lun->ctl_softc; 11167 int retval; 11168 uint32_t residx; 11169 11170 retval = 0; 11171 11172 mtx_assert(&lun->lun_lock, MA_OWNED); 11173 11174 /* 11175 * If this shelf is a secondary shelf controller, we may have to 11176 * reject some commands disallowed by HA mode and link state. 11177 */ 11178 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11179 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11180 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11181 ctl_set_lun_unavail(ctsio); 11182 retval = 1; 11183 goto bailout; 11184 } 11185 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11186 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11187 ctl_set_lun_transit(ctsio); 11188 retval = 1; 11189 goto bailout; 11190 } 11191 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11192 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11193 ctl_set_lun_standby(ctsio); 11194 retval = 1; 11195 goto bailout; 11196 } 11197 11198 /* The rest of checks are only done on executing side */ 11199 if (softc->ha_mode == CTL_HA_MODE_XFER) 11200 goto bailout; 11201 } 11202 11203 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11204 if (lun->be_lun && 11205 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11206 ctl_set_hw_write_protected(ctsio); 11207 retval = 1; 11208 goto bailout; 11209 } 11210 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11211 ctl_set_sense(ctsio, /*current_error*/ 1, 11212 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11213 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11214 retval = 1; 11215 goto bailout; 11216 } 11217 } 11218 11219 /* 11220 * Check for a reservation conflict. If this command isn't allowed 11221 * even on reserved LUNs, and if this initiator isn't the one who 11222 * reserved us, reject the command with a reservation conflict. 11223 */ 11224 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11225 if ((lun->flags & CTL_LUN_RESERVED) 11226 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11227 if (lun->res_idx != residx) { 11228 ctl_set_reservation_conflict(ctsio); 11229 retval = 1; 11230 goto bailout; 11231 } 11232 } 11233 11234 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11235 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11236 /* No reservation or command is allowed. */; 11237 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11238 (lun->pr_res_type == SPR_TYPE_WR_EX || 11239 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11240 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11241 /* The command is allowed for Write Exclusive resv. */; 11242 } else { 11243 /* 11244 * if we aren't registered or it's a res holder type 11245 * reservation and this isn't the res holder then set a 11246 * conflict. 11247 */ 11248 if (ctl_get_prkey(lun, residx) == 0 || 11249 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11250 ctl_set_reservation_conflict(ctsio); 11251 retval = 1; 11252 goto bailout; 11253 } 11254 } 11255 11256 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11257 if (lun->flags & CTL_LUN_EJECTED) 11258 ctl_set_lun_ejected(ctsio); 11259 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11260 if (lun->flags & CTL_LUN_REMOVABLE) 11261 ctl_set_lun_no_media(ctsio); 11262 else 11263 ctl_set_lun_int_reqd(ctsio); 11264 } else if (lun->flags & CTL_LUN_STOPPED) 11265 ctl_set_lun_stopped(ctsio); 11266 else 11267 goto bailout; 11268 retval = 1; 11269 goto bailout; 11270 } 11271 11272 bailout: 11273 return (retval); 11274 } 11275 11276 static void 11277 ctl_failover_io(union ctl_io *io, int have_lock) 11278 { 11279 ctl_set_busy(&io->scsiio); 11280 ctl_done(io); 11281 } 11282 11283 static void 11284 ctl_failover_lun(union ctl_io *rio) 11285 { 11286 struct ctl_softc *softc = CTL_SOFTC(rio); 11287 struct ctl_lun *lun; 11288 struct ctl_io_hdr *io, *next_io; 11289 uint32_t targ_lun; 11290 11291 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11292 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun)); 11293 11294 /* Find and lock the LUN. */ 11295 mtx_lock(&softc->ctl_lock); 11296 if (targ_lun > ctl_max_luns || 11297 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11298 mtx_unlock(&softc->ctl_lock); 11299 return; 11300 } 11301 mtx_lock(&lun->lun_lock); 11302 mtx_unlock(&softc->ctl_lock); 11303 if (lun->flags & CTL_LUN_DISABLED) { 11304 mtx_unlock(&lun->lun_lock); 11305 return; 11306 } 11307 11308 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11309 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11310 /* We are master */ 11311 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11312 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11313 io->flags |= CTL_FLAG_ABORT; 11314 io->flags |= CTL_FLAG_FAILOVER; 11315 ctl_try_unblock_io(lun, 11316 (union ctl_io *)io, FALSE); 11317 } else { /* This can be only due to DATAMOVE */ 11318 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11319 io->flags &= ~CTL_FLAG_DMA_INPROG; 11320 io->flags |= CTL_FLAG_IO_ACTIVE; 11321 io->port_status = 31340; 11322 ctl_enqueue_isc((union ctl_io *)io); 11323 } 11324 } else 11325 /* We are slave */ 11326 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11327 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11328 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11329 io->flags |= CTL_FLAG_FAILOVER; 11330 } else { 11331 ctl_set_busy(&((union ctl_io *)io)-> 11332 scsiio); 11333 ctl_done((union ctl_io *)io); 11334 } 11335 } 11336 } 11337 } else { /* SERIALIZE modes */ 11338 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11339 /* We are master */ 11340 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11341 if (io->blocker != NULL) { 11342 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue, 11343 io, blocked_links); 11344 io->blocker = NULL; 11345 } 11346 ctl_try_unblock_others(lun, (union ctl_io *)io, 11347 TRUE); 11348 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11349 ctl_free_io((union ctl_io *)io); 11350 } else 11351 /* We are slave */ 11352 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11353 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11354 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11355 ctl_set_busy(&((union ctl_io *)io)-> 11356 scsiio); 11357 ctl_done((union ctl_io *)io); 11358 } 11359 } 11360 } 11361 } 11362 mtx_unlock(&lun->lun_lock); 11363 } 11364 11365 static int 11366 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11367 { 11368 struct ctl_lun *lun; 11369 const struct ctl_cmd_entry *entry; 11370 union ctl_io *bio; 11371 uint32_t initidx, targ_lun; 11372 int retval = 0; 11373 11374 lun = NULL; 11375 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11376 if (targ_lun < ctl_max_luns) 11377 lun = softc->ctl_luns[targ_lun]; 11378 if (lun) { 11379 /* 11380 * If the LUN is invalid, pretend that it doesn't exist. 11381 * It will go away as soon as all pending I/O has been 11382 * completed. 11383 */ 11384 mtx_lock(&lun->lun_lock); 11385 if (lun->flags & CTL_LUN_DISABLED) { 11386 mtx_unlock(&lun->lun_lock); 11387 lun = NULL; 11388 } 11389 } 11390 CTL_LUN(ctsio) = lun; 11391 if (lun) { 11392 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11393 11394 /* 11395 * Every I/O goes into the OOA queue for a particular LUN, 11396 * and stays there until completion. 11397 */ 11398 #ifdef CTL_TIME_IO 11399 if (TAILQ_EMPTY(&lun->ooa_queue)) 11400 lun->idle_time += getsbinuptime() - lun->last_busy; 11401 #endif 11402 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11403 } 11404 11405 /* Get command entry and return error if it is unsuppotyed. */ 11406 entry = ctl_validate_command(ctsio); 11407 if (entry == NULL) { 11408 if (lun) 11409 mtx_unlock(&lun->lun_lock); 11410 return (retval); 11411 } 11412 11413 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11414 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11415 11416 /* 11417 * Check to see whether we can send this command to LUNs that don't 11418 * exist. This should pretty much only be the case for inquiry 11419 * and request sense. Further checks, below, really require having 11420 * a LUN, so we can't really check the command anymore. Just put 11421 * it on the rtr queue. 11422 */ 11423 if (lun == NULL) { 11424 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11425 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11426 ctl_enqueue_rtr((union ctl_io *)ctsio); 11427 return (retval); 11428 } 11429 11430 ctl_set_unsupported_lun(ctsio); 11431 ctl_done((union ctl_io *)ctsio); 11432 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11433 return (retval); 11434 } else { 11435 /* 11436 * Make sure we support this particular command on this LUN. 11437 * e.g., we don't support writes to the control LUN. 11438 */ 11439 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11440 mtx_unlock(&lun->lun_lock); 11441 ctl_set_invalid_opcode(ctsio); 11442 ctl_done((union ctl_io *)ctsio); 11443 return (retval); 11444 } 11445 } 11446 11447 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11448 11449 /* 11450 * If we've got a request sense, it'll clear the contingent 11451 * allegiance condition. Otherwise, if we have a CA condition for 11452 * this initiator, clear it, because it sent down a command other 11453 * than request sense. 11454 */ 11455 if (ctsio->cdb[0] != REQUEST_SENSE) { 11456 struct scsi_sense_data *ps; 11457 11458 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11459 if (ps != NULL) 11460 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11461 } 11462 11463 /* 11464 * If the command has this flag set, it handles its own unit 11465 * attention reporting, we shouldn't do anything. Otherwise we 11466 * check for any pending unit attentions, and send them back to the 11467 * initiator. We only do this when a command initially comes in, 11468 * not when we pull it off the blocked queue. 11469 * 11470 * According to SAM-3, section 5.3.2, the order that things get 11471 * presented back to the host is basically unit attentions caused 11472 * by some sort of reset event, busy status, reservation conflicts 11473 * or task set full, and finally any other status. 11474 * 11475 * One issue here is that some of the unit attentions we report 11476 * don't fall into the "reset" category (e.g. "reported luns data 11477 * has changed"). So reporting it here, before the reservation 11478 * check, may be technically wrong. I guess the only thing to do 11479 * would be to check for and report the reset events here, and then 11480 * check for the other unit attention types after we check for a 11481 * reservation conflict. 11482 * 11483 * XXX KDM need to fix this 11484 */ 11485 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11486 ctl_ua_type ua_type; 11487 u_int sense_len = 0; 11488 11489 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11490 &sense_len, SSD_TYPE_NONE); 11491 if (ua_type != CTL_UA_NONE) { 11492 mtx_unlock(&lun->lun_lock); 11493 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11494 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11495 ctsio->sense_len = sense_len; 11496 ctl_done((union ctl_io *)ctsio); 11497 return (retval); 11498 } 11499 } 11500 11501 11502 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11503 mtx_unlock(&lun->lun_lock); 11504 ctl_done((union ctl_io *)ctsio); 11505 return (retval); 11506 } 11507 11508 /* 11509 * XXX CHD this is where we want to send IO to other side if 11510 * this LUN is secondary on this SC. We will need to make a copy 11511 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11512 * the copy we send as FROM_OTHER. 11513 * We also need to stuff the address of the original IO so we can 11514 * find it easily. Something similar will need be done on the other 11515 * side so when we are done we can find the copy. 11516 */ 11517 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11518 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11519 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11520 union ctl_ha_msg msg_info; 11521 int isc_retval; 11522 11523 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11524 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11525 mtx_unlock(&lun->lun_lock); 11526 11527 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11528 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11529 msg_info.hdr.serializing_sc = NULL; 11530 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11531 msg_info.scsi.tag_num = ctsio->tag_num; 11532 msg_info.scsi.tag_type = ctsio->tag_type; 11533 msg_info.scsi.cdb_len = ctsio->cdb_len; 11534 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11535 11536 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11537 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11538 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11539 ctl_set_busy(ctsio); 11540 ctl_done((union ctl_io *)ctsio); 11541 return (retval); 11542 } 11543 return (retval); 11544 } 11545 11546 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links); 11547 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 11548 case CTL_ACTION_BLOCK: 11549 ctsio->io_hdr.blocker = bio; 11550 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 11551 blocked_links); 11552 mtx_unlock(&lun->lun_lock); 11553 return (retval); 11554 case CTL_ACTION_PASS: 11555 case CTL_ACTION_SKIP: 11556 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11557 mtx_unlock(&lun->lun_lock); 11558 ctl_enqueue_rtr((union ctl_io *)ctsio); 11559 break; 11560 case CTL_ACTION_OVERLAP: 11561 mtx_unlock(&lun->lun_lock); 11562 ctl_set_overlapped_cmd(ctsio); 11563 ctl_done((union ctl_io *)ctsio); 11564 break; 11565 case CTL_ACTION_OVERLAP_TAG: 11566 mtx_unlock(&lun->lun_lock); 11567 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11568 ctl_done((union ctl_io *)ctsio); 11569 break; 11570 case CTL_ACTION_ERROR: 11571 default: 11572 mtx_unlock(&lun->lun_lock); 11573 ctl_set_internal_failure(ctsio, 11574 /*sks_valid*/ 0, 11575 /*retry_count*/ 0); 11576 ctl_done((union ctl_io *)ctsio); 11577 break; 11578 } 11579 return (retval); 11580 } 11581 11582 const struct ctl_cmd_entry * 11583 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11584 { 11585 const struct ctl_cmd_entry *entry; 11586 int service_action; 11587 11588 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11589 if (sa) 11590 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11591 if (entry->flags & CTL_CMD_FLAG_SA5) { 11592 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11593 entry = &((const struct ctl_cmd_entry *) 11594 entry->execute)[service_action]; 11595 } 11596 return (entry); 11597 } 11598 11599 const struct ctl_cmd_entry * 11600 ctl_validate_command(struct ctl_scsiio *ctsio) 11601 { 11602 const struct ctl_cmd_entry *entry; 11603 int i, sa; 11604 uint8_t diff; 11605 11606 entry = ctl_get_cmd_entry(ctsio, &sa); 11607 if (entry->execute == NULL) { 11608 if (sa) 11609 ctl_set_invalid_field(ctsio, 11610 /*sks_valid*/ 1, 11611 /*command*/ 1, 11612 /*field*/ 1, 11613 /*bit_valid*/ 1, 11614 /*bit*/ 4); 11615 else 11616 ctl_set_invalid_opcode(ctsio); 11617 ctl_done((union ctl_io *)ctsio); 11618 return (NULL); 11619 } 11620 KASSERT(entry->length > 0, 11621 ("Not defined length for command 0x%02x/0x%02x", 11622 ctsio->cdb[0], ctsio->cdb[1])); 11623 for (i = 1; i < entry->length; i++) { 11624 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11625 if (diff == 0) 11626 continue; 11627 ctl_set_invalid_field(ctsio, 11628 /*sks_valid*/ 1, 11629 /*command*/ 1, 11630 /*field*/ i, 11631 /*bit_valid*/ 1, 11632 /*bit*/ fls(diff) - 1); 11633 ctl_done((union ctl_io *)ctsio); 11634 return (NULL); 11635 } 11636 return (entry); 11637 } 11638 11639 static int 11640 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11641 { 11642 11643 switch (lun_type) { 11644 case T_DIRECT: 11645 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11646 return (0); 11647 break; 11648 case T_PROCESSOR: 11649 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11650 return (0); 11651 break; 11652 case T_CDROM: 11653 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11654 return (0); 11655 break; 11656 default: 11657 return (0); 11658 } 11659 return (1); 11660 } 11661 11662 static int 11663 ctl_scsiio(struct ctl_scsiio *ctsio) 11664 { 11665 int retval; 11666 const struct ctl_cmd_entry *entry; 11667 11668 retval = CTL_RETVAL_COMPLETE; 11669 11670 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11671 11672 entry = ctl_get_cmd_entry(ctsio, NULL); 11673 11674 /* 11675 * If this I/O has been aborted, just send it straight to 11676 * ctl_done() without executing it. 11677 */ 11678 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11679 ctl_done((union ctl_io *)ctsio); 11680 goto bailout; 11681 } 11682 11683 /* 11684 * All the checks should have been handled by ctl_scsiio_precheck(). 11685 * We should be clear now to just execute the I/O. 11686 */ 11687 retval = entry->execute(ctsio); 11688 11689 bailout: 11690 return (retval); 11691 } 11692 11693 static int 11694 ctl_target_reset(union ctl_io *io) 11695 { 11696 struct ctl_softc *softc = CTL_SOFTC(io); 11697 struct ctl_port *port = CTL_PORT(io); 11698 struct ctl_lun *lun; 11699 uint32_t initidx; 11700 ctl_ua_type ua_type; 11701 11702 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11703 union ctl_ha_msg msg_info; 11704 11705 msg_info.hdr.nexus = io->io_hdr.nexus; 11706 msg_info.task.task_action = io->taskio.task_action; 11707 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11708 msg_info.hdr.original_sc = NULL; 11709 msg_info.hdr.serializing_sc = NULL; 11710 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11711 sizeof(msg_info.task), M_WAITOK); 11712 } 11713 11714 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11715 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11716 ua_type = CTL_UA_TARG_RESET; 11717 else 11718 ua_type = CTL_UA_BUS_RESET; 11719 mtx_lock(&softc->ctl_lock); 11720 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11721 if (port != NULL && 11722 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11723 continue; 11724 ctl_do_lun_reset(lun, initidx, ua_type); 11725 } 11726 mtx_unlock(&softc->ctl_lock); 11727 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11728 return (0); 11729 } 11730 11731 /* 11732 * The LUN should always be set. The I/O is optional, and is used to 11733 * distinguish between I/Os sent by this initiator, and by other 11734 * initiators. We set unit attention for initiators other than this one. 11735 * SAM-3 is vague on this point. It does say that a unit attention should 11736 * be established for other initiators when a LUN is reset (see section 11737 * 5.7.3), but it doesn't specifically say that the unit attention should 11738 * be established for this particular initiator when a LUN is reset. Here 11739 * is the relevant text, from SAM-3 rev 8: 11740 * 11741 * 5.7.2 When a SCSI initiator port aborts its own tasks 11742 * 11743 * When a SCSI initiator port causes its own task(s) to be aborted, no 11744 * notification that the task(s) have been aborted shall be returned to 11745 * the SCSI initiator port other than the completion response for the 11746 * command or task management function action that caused the task(s) to 11747 * be aborted and notification(s) associated with related effects of the 11748 * action (e.g., a reset unit attention condition). 11749 * 11750 * XXX KDM for now, we're setting unit attention for all initiators. 11751 */ 11752 static void 11753 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11754 { 11755 union ctl_io *xio; 11756 int i; 11757 11758 mtx_lock(&lun->lun_lock); 11759 /* Abort tasks. */ 11760 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11761 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11762 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11763 ctl_try_unblock_io(lun, xio, FALSE); 11764 } 11765 /* Clear CA. */ 11766 for (i = 0; i < ctl_max_ports; i++) { 11767 free(lun->pending_sense[i], M_CTL); 11768 lun->pending_sense[i] = NULL; 11769 } 11770 /* Clear reservation. */ 11771 lun->flags &= ~CTL_LUN_RESERVED; 11772 /* Clear prevent media removal. */ 11773 if (lun->prevent) { 11774 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11775 ctl_clear_mask(lun->prevent, i); 11776 lun->prevent_count = 0; 11777 } 11778 /* Clear TPC status */ 11779 ctl_tpc_lun_clear(lun, -1); 11780 /* Establish UA. */ 11781 #if 0 11782 ctl_est_ua_all(lun, initidx, ua_type); 11783 #else 11784 ctl_est_ua_all(lun, -1, ua_type); 11785 #endif 11786 mtx_unlock(&lun->lun_lock); 11787 } 11788 11789 static int 11790 ctl_lun_reset(union ctl_io *io) 11791 { 11792 struct ctl_softc *softc = CTL_SOFTC(io); 11793 struct ctl_lun *lun; 11794 uint32_t targ_lun, initidx; 11795 11796 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11797 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11798 mtx_lock(&softc->ctl_lock); 11799 if (targ_lun >= ctl_max_luns || 11800 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11801 mtx_unlock(&softc->ctl_lock); 11802 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11803 return (1); 11804 } 11805 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11806 mtx_unlock(&softc->ctl_lock); 11807 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11808 11809 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11810 union ctl_ha_msg msg_info; 11811 11812 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11813 msg_info.hdr.nexus = io->io_hdr.nexus; 11814 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11815 msg_info.hdr.original_sc = NULL; 11816 msg_info.hdr.serializing_sc = NULL; 11817 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11818 sizeof(msg_info.task), M_WAITOK); 11819 } 11820 return (0); 11821 } 11822 11823 static void 11824 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11825 int other_sc) 11826 { 11827 union ctl_io *xio; 11828 11829 mtx_assert(&lun->lun_lock, MA_OWNED); 11830 11831 /* 11832 * Run through the OOA queue and attempt to find the given I/O. 11833 * The target port, initiator ID, tag type and tag number have to 11834 * match the values that we got from the initiator. If we have an 11835 * untagged command to abort, simply abort the first untagged command 11836 * we come to. We only allow one untagged command at a time of course. 11837 */ 11838 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11839 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11840 11841 if ((targ_port == UINT32_MAX || 11842 targ_port == xio->io_hdr.nexus.targ_port) && 11843 (init_id == UINT32_MAX || 11844 init_id == xio->io_hdr.nexus.initid)) { 11845 if (targ_port != xio->io_hdr.nexus.targ_port || 11846 init_id != xio->io_hdr.nexus.initid) 11847 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11848 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11849 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11850 union ctl_ha_msg msg_info; 11851 11852 msg_info.hdr.nexus = xio->io_hdr.nexus; 11853 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11854 msg_info.task.tag_num = xio->scsiio.tag_num; 11855 msg_info.task.tag_type = xio->scsiio.tag_type; 11856 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11857 msg_info.hdr.original_sc = NULL; 11858 msg_info.hdr.serializing_sc = NULL; 11859 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11860 sizeof(msg_info.task), M_NOWAIT); 11861 } 11862 ctl_try_unblock_io(lun, xio, FALSE); 11863 } 11864 } 11865 } 11866 11867 static int 11868 ctl_abort_task_set(union ctl_io *io) 11869 { 11870 struct ctl_softc *softc = CTL_SOFTC(io); 11871 struct ctl_lun *lun; 11872 uint32_t targ_lun; 11873 11874 /* 11875 * Look up the LUN. 11876 */ 11877 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11878 mtx_lock(&softc->ctl_lock); 11879 if (targ_lun >= ctl_max_luns || 11880 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11881 mtx_unlock(&softc->ctl_lock); 11882 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11883 return (1); 11884 } 11885 11886 mtx_lock(&lun->lun_lock); 11887 mtx_unlock(&softc->ctl_lock); 11888 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11889 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11890 io->io_hdr.nexus.initid, 11891 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11892 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11893 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11894 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11895 } 11896 mtx_unlock(&lun->lun_lock); 11897 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11898 return (0); 11899 } 11900 11901 static void 11902 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 11903 ctl_ua_type ua_type) 11904 { 11905 struct ctl_lun *lun; 11906 struct scsi_sense_data *ps; 11907 uint32_t p, i; 11908 11909 p = initidx / CTL_MAX_INIT_PER_PORT; 11910 i = initidx % CTL_MAX_INIT_PER_PORT; 11911 mtx_lock(&softc->ctl_lock); 11912 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11913 mtx_lock(&lun->lun_lock); 11914 /* Abort tasks. */ 11915 ctl_abort_tasks_lun(lun, p, i, 1); 11916 /* Clear CA. */ 11917 ps = lun->pending_sense[p]; 11918 if (ps != NULL) 11919 ps[i].error_code = 0; 11920 /* Clear reservation. */ 11921 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11922 lun->flags &= ~CTL_LUN_RESERVED; 11923 /* Clear prevent media removal. */ 11924 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 11925 ctl_clear_mask(lun->prevent, initidx); 11926 lun->prevent_count--; 11927 } 11928 /* Clear TPC status */ 11929 ctl_tpc_lun_clear(lun, initidx); 11930 /* Establish UA. */ 11931 ctl_est_ua(lun, initidx, ua_type); 11932 mtx_unlock(&lun->lun_lock); 11933 } 11934 mtx_unlock(&softc->ctl_lock); 11935 } 11936 11937 static int 11938 ctl_i_t_nexus_reset(union ctl_io *io) 11939 { 11940 struct ctl_softc *softc = CTL_SOFTC(io); 11941 uint32_t initidx; 11942 11943 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11944 union ctl_ha_msg msg_info; 11945 11946 msg_info.hdr.nexus = io->io_hdr.nexus; 11947 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11948 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11949 msg_info.hdr.original_sc = NULL; 11950 msg_info.hdr.serializing_sc = NULL; 11951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11952 sizeof(msg_info.task), M_WAITOK); 11953 } 11954 11955 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11956 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 11957 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11958 return (0); 11959 } 11960 11961 static int 11962 ctl_abort_task(union ctl_io *io) 11963 { 11964 struct ctl_softc *softc = CTL_SOFTC(io); 11965 union ctl_io *xio; 11966 struct ctl_lun *lun; 11967 uint32_t targ_lun; 11968 11969 /* 11970 * Look up the LUN. 11971 */ 11972 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11973 mtx_lock(&softc->ctl_lock); 11974 if (targ_lun >= ctl_max_luns || 11975 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11976 mtx_unlock(&softc->ctl_lock); 11977 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11978 return (1); 11979 } 11980 11981 mtx_lock(&lun->lun_lock); 11982 mtx_unlock(&softc->ctl_lock); 11983 /* 11984 * Run through the OOA queue and attempt to find the given I/O. 11985 * The target port, initiator ID, tag type and tag number have to 11986 * match the values that we got from the initiator. If we have an 11987 * untagged command to abort, simply abort the first untagged command 11988 * we come to. We only allow one untagged command at a time of course. 11989 */ 11990 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11991 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11992 11993 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11994 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11995 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11996 continue; 11997 11998 /* 11999 * If the abort says that the task is untagged, the 12000 * task in the queue must be untagged. Otherwise, 12001 * we just check to see whether the tag numbers 12002 * match. This is because the QLogic firmware 12003 * doesn't pass back the tag type in an abort 12004 * request. 12005 */ 12006 #if 0 12007 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12008 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12009 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12010 #else 12011 /* 12012 * XXX KDM we've got problems with FC, because it 12013 * doesn't send down a tag type with aborts. So we 12014 * can only really go by the tag number... 12015 * This may cause problems with parallel SCSI. 12016 * Need to figure that out!! 12017 */ 12018 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12019 #endif 12020 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12021 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12022 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12023 union ctl_ha_msg msg_info; 12024 12025 msg_info.hdr.nexus = io->io_hdr.nexus; 12026 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12027 msg_info.task.tag_num = io->taskio.tag_num; 12028 msg_info.task.tag_type = io->taskio.tag_type; 12029 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12030 msg_info.hdr.original_sc = NULL; 12031 msg_info.hdr.serializing_sc = NULL; 12032 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12033 sizeof(msg_info.task), M_NOWAIT); 12034 } 12035 ctl_try_unblock_io(lun, xio, FALSE); 12036 } 12037 } 12038 mtx_unlock(&lun->lun_lock); 12039 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12040 return (0); 12041 } 12042 12043 static int 12044 ctl_query_task(union ctl_io *io, int task_set) 12045 { 12046 struct ctl_softc *softc = CTL_SOFTC(io); 12047 union ctl_io *xio; 12048 struct ctl_lun *lun; 12049 int found = 0; 12050 uint32_t targ_lun; 12051 12052 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12053 mtx_lock(&softc->ctl_lock); 12054 if (targ_lun >= ctl_max_luns || 12055 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12056 mtx_unlock(&softc->ctl_lock); 12057 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12058 return (1); 12059 } 12060 mtx_lock(&lun->lun_lock); 12061 mtx_unlock(&softc->ctl_lock); 12062 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12063 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12064 12065 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12066 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12067 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12068 continue; 12069 12070 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12071 found = 1; 12072 break; 12073 } 12074 } 12075 mtx_unlock(&lun->lun_lock); 12076 if (found) 12077 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12078 else 12079 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12080 return (0); 12081 } 12082 12083 static int 12084 ctl_query_async_event(union ctl_io *io) 12085 { 12086 struct ctl_softc *softc = CTL_SOFTC(io); 12087 struct ctl_lun *lun; 12088 ctl_ua_type ua; 12089 uint32_t targ_lun, initidx; 12090 12091 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12092 mtx_lock(&softc->ctl_lock); 12093 if (targ_lun >= ctl_max_luns || 12094 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12095 mtx_unlock(&softc->ctl_lock); 12096 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12097 return (1); 12098 } 12099 mtx_lock(&lun->lun_lock); 12100 mtx_unlock(&softc->ctl_lock); 12101 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12102 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12103 mtx_unlock(&lun->lun_lock); 12104 if (ua != CTL_UA_NONE) 12105 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12106 else 12107 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12108 return (0); 12109 } 12110 12111 static void 12112 ctl_run_task(union ctl_io *io) 12113 { 12114 int retval = 1; 12115 12116 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12117 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12118 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12119 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12120 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12121 switch (io->taskio.task_action) { 12122 case CTL_TASK_ABORT_TASK: 12123 retval = ctl_abort_task(io); 12124 break; 12125 case CTL_TASK_ABORT_TASK_SET: 12126 case CTL_TASK_CLEAR_TASK_SET: 12127 retval = ctl_abort_task_set(io); 12128 break; 12129 case CTL_TASK_CLEAR_ACA: 12130 break; 12131 case CTL_TASK_I_T_NEXUS_RESET: 12132 retval = ctl_i_t_nexus_reset(io); 12133 break; 12134 case CTL_TASK_LUN_RESET: 12135 retval = ctl_lun_reset(io); 12136 break; 12137 case CTL_TASK_TARGET_RESET: 12138 case CTL_TASK_BUS_RESET: 12139 retval = ctl_target_reset(io); 12140 break; 12141 case CTL_TASK_PORT_LOGIN: 12142 break; 12143 case CTL_TASK_PORT_LOGOUT: 12144 break; 12145 case CTL_TASK_QUERY_TASK: 12146 retval = ctl_query_task(io, 0); 12147 break; 12148 case CTL_TASK_QUERY_TASK_SET: 12149 retval = ctl_query_task(io, 1); 12150 break; 12151 case CTL_TASK_QUERY_ASYNC_EVENT: 12152 retval = ctl_query_async_event(io); 12153 break; 12154 default: 12155 printf("%s: got unknown task management event %d\n", 12156 __func__, io->taskio.task_action); 12157 break; 12158 } 12159 if (retval == 0) 12160 io->io_hdr.status = CTL_SUCCESS; 12161 else 12162 io->io_hdr.status = CTL_ERROR; 12163 ctl_done(io); 12164 } 12165 12166 /* 12167 * For HA operation. Handle commands that come in from the other 12168 * controller. 12169 */ 12170 static void 12171 ctl_handle_isc(union ctl_io *io) 12172 { 12173 struct ctl_softc *softc = CTL_SOFTC(io); 12174 struct ctl_lun *lun; 12175 const struct ctl_cmd_entry *entry; 12176 uint32_t targ_lun; 12177 12178 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12179 switch (io->io_hdr.msg_type) { 12180 case CTL_MSG_SERIALIZE: 12181 ctl_serialize_other_sc_cmd(&io->scsiio); 12182 break; 12183 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12184 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12185 if (targ_lun >= ctl_max_luns || 12186 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12187 ctl_done(io); 12188 break; 12189 } 12190 mtx_lock(&lun->lun_lock); 12191 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12192 mtx_unlock(&lun->lun_lock); 12193 ctl_done(io); 12194 break; 12195 } 12196 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12197 mtx_unlock(&lun->lun_lock); 12198 ctl_enqueue_rtr(io); 12199 break; 12200 case CTL_MSG_FINISH_IO: 12201 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12202 ctl_done(io); 12203 break; 12204 } 12205 if (targ_lun >= ctl_max_luns || 12206 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12207 ctl_free_io(io); 12208 break; 12209 } 12210 mtx_lock(&lun->lun_lock); 12211 ctl_try_unblock_others(lun, io, TRUE); 12212 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12213 mtx_unlock(&lun->lun_lock); 12214 ctl_free_io(io); 12215 break; 12216 case CTL_MSG_PERS_ACTION: 12217 ctl_hndl_per_res_out_on_other_sc(io); 12218 ctl_free_io(io); 12219 break; 12220 case CTL_MSG_BAD_JUJU: 12221 ctl_done(io); 12222 break; 12223 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12224 ctl_datamove_remote(io); 12225 break; 12226 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12227 io->scsiio.be_move_done(io); 12228 break; 12229 case CTL_MSG_FAILOVER: 12230 ctl_failover_lun(io); 12231 ctl_free_io(io); 12232 break; 12233 default: 12234 printf("%s: Invalid message type %d\n", 12235 __func__, io->io_hdr.msg_type); 12236 ctl_free_io(io); 12237 break; 12238 } 12239 12240 } 12241 12242 12243 /* 12244 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12245 * there is no match. 12246 */ 12247 static ctl_lun_error_pattern 12248 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12249 { 12250 const struct ctl_cmd_entry *entry; 12251 ctl_lun_error_pattern filtered_pattern, pattern; 12252 12253 pattern = desc->error_pattern; 12254 12255 /* 12256 * XXX KDM we need more data passed into this function to match a 12257 * custom pattern, and we actually need to implement custom pattern 12258 * matching. 12259 */ 12260 if (pattern & CTL_LUN_PAT_CMD) 12261 return (CTL_LUN_PAT_CMD); 12262 12263 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12264 return (CTL_LUN_PAT_ANY); 12265 12266 entry = ctl_get_cmd_entry(ctsio, NULL); 12267 12268 filtered_pattern = entry->pattern & pattern; 12269 12270 /* 12271 * If the user requested specific flags in the pattern (e.g. 12272 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12273 * flags. 12274 * 12275 * If the user did not specify any flags, it doesn't matter whether 12276 * or not the command supports the flags. 12277 */ 12278 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12279 (pattern & ~CTL_LUN_PAT_MASK)) 12280 return (CTL_LUN_PAT_NONE); 12281 12282 /* 12283 * If the user asked for a range check, see if the requested LBA 12284 * range overlaps with this command's LBA range. 12285 */ 12286 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12287 uint64_t lba1; 12288 uint64_t len1; 12289 ctl_action action; 12290 int retval; 12291 12292 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12293 if (retval != 0) 12294 return (CTL_LUN_PAT_NONE); 12295 12296 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12297 desc->lba_range.len, FALSE); 12298 /* 12299 * A "pass" means that the LBA ranges don't overlap, so 12300 * this doesn't match the user's range criteria. 12301 */ 12302 if (action == CTL_ACTION_PASS) 12303 return (CTL_LUN_PAT_NONE); 12304 } 12305 12306 return (filtered_pattern); 12307 } 12308 12309 static void 12310 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12311 { 12312 struct ctl_error_desc *desc, *desc2; 12313 12314 mtx_assert(&lun->lun_lock, MA_OWNED); 12315 12316 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12317 ctl_lun_error_pattern pattern; 12318 /* 12319 * Check to see whether this particular command matches 12320 * the pattern in the descriptor. 12321 */ 12322 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12323 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12324 continue; 12325 12326 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12327 case CTL_LUN_INJ_ABORTED: 12328 ctl_set_aborted(&io->scsiio); 12329 break; 12330 case CTL_LUN_INJ_MEDIUM_ERR: 12331 ctl_set_medium_error(&io->scsiio, 12332 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12333 CTL_FLAG_DATA_OUT); 12334 break; 12335 case CTL_LUN_INJ_UA: 12336 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12337 * OCCURRED */ 12338 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12339 break; 12340 case CTL_LUN_INJ_CUSTOM: 12341 /* 12342 * We're assuming the user knows what he is doing. 12343 * Just copy the sense information without doing 12344 * checks. 12345 */ 12346 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12347 MIN(sizeof(desc->custom_sense), 12348 sizeof(io->scsiio.sense_data))); 12349 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12350 io->scsiio.sense_len = SSD_FULL_SIZE; 12351 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12352 break; 12353 case CTL_LUN_INJ_NONE: 12354 default: 12355 /* 12356 * If this is an error injection type we don't know 12357 * about, clear the continuous flag (if it is set) 12358 * so it will get deleted below. 12359 */ 12360 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12361 break; 12362 } 12363 /* 12364 * By default, each error injection action is a one-shot 12365 */ 12366 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12367 continue; 12368 12369 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12370 12371 free(desc, M_CTL); 12372 } 12373 } 12374 12375 #ifdef CTL_IO_DELAY 12376 static void 12377 ctl_datamove_timer_wakeup(void *arg) 12378 { 12379 union ctl_io *io; 12380 12381 io = (union ctl_io *)arg; 12382 12383 ctl_datamove(io); 12384 } 12385 #endif /* CTL_IO_DELAY */ 12386 12387 void 12388 ctl_datamove(union ctl_io *io) 12389 { 12390 void (*fe_datamove)(union ctl_io *io); 12391 12392 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12393 12394 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12395 12396 /* No data transferred yet. Frontend must update this when done. */ 12397 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12398 12399 #ifdef CTL_TIME_IO 12400 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12401 char str[256]; 12402 char path_str[64]; 12403 struct sbuf sb; 12404 12405 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12406 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12407 12408 sbuf_cat(&sb, path_str); 12409 switch (io->io_hdr.io_type) { 12410 case CTL_IO_SCSI: 12411 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12412 sbuf_printf(&sb, "\n"); 12413 sbuf_cat(&sb, path_str); 12414 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12415 io->scsiio.tag_num, io->scsiio.tag_type); 12416 break; 12417 case CTL_IO_TASK: 12418 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12419 "Tag Type: %d\n", io->taskio.task_action, 12420 io->taskio.tag_num, io->taskio.tag_type); 12421 break; 12422 default: 12423 panic("%s: Invalid CTL I/O type %d\n", 12424 __func__, io->io_hdr.io_type); 12425 } 12426 sbuf_cat(&sb, path_str); 12427 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12428 (intmax_t)time_uptime - io->io_hdr.start_time); 12429 sbuf_finish(&sb); 12430 printf("%s", sbuf_data(&sb)); 12431 } 12432 #endif /* CTL_TIME_IO */ 12433 12434 #ifdef CTL_IO_DELAY 12435 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12436 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12437 } else { 12438 struct ctl_lun *lun; 12439 12440 lun = CTL_LUN(io); 12441 if ((lun != NULL) 12442 && (lun->delay_info.datamove_delay > 0)) { 12443 12444 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12445 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12446 callout_reset(&io->io_hdr.delay_callout, 12447 lun->delay_info.datamove_delay * hz, 12448 ctl_datamove_timer_wakeup, io); 12449 if (lun->delay_info.datamove_type == 12450 CTL_DELAY_TYPE_ONESHOT) 12451 lun->delay_info.datamove_delay = 0; 12452 return; 12453 } 12454 } 12455 #endif 12456 12457 /* 12458 * This command has been aborted. Set the port status, so we fail 12459 * the data move. 12460 */ 12461 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12462 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12463 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12464 io->io_hdr.nexus.targ_port, 12465 io->io_hdr.nexus.targ_lun); 12466 io->io_hdr.port_status = 31337; 12467 /* 12468 * Note that the backend, in this case, will get the 12469 * callback in its context. In other cases it may get 12470 * called in the frontend's interrupt thread context. 12471 */ 12472 io->scsiio.be_move_done(io); 12473 return; 12474 } 12475 12476 /* Don't confuse frontend with zero length data move. */ 12477 if (io->scsiio.kern_data_len == 0) { 12478 io->scsiio.be_move_done(io); 12479 return; 12480 } 12481 12482 fe_datamove = CTL_PORT(io)->fe_datamove; 12483 fe_datamove(io); 12484 } 12485 12486 static void 12487 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12488 { 12489 union ctl_ha_msg msg; 12490 #ifdef CTL_TIME_IO 12491 struct bintime cur_bt; 12492 #endif 12493 12494 memset(&msg, 0, sizeof(msg)); 12495 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12496 msg.hdr.original_sc = io; 12497 msg.hdr.serializing_sc = io->io_hdr.remote_io; 12498 msg.hdr.nexus = io->io_hdr.nexus; 12499 msg.hdr.status = io->io_hdr.status; 12500 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12501 msg.scsi.tag_num = io->scsiio.tag_num; 12502 msg.scsi.tag_type = io->scsiio.tag_type; 12503 msg.scsi.scsi_status = io->scsiio.scsi_status; 12504 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12505 io->scsiio.sense_len); 12506 msg.scsi.sense_len = io->scsiio.sense_len; 12507 msg.scsi.port_status = io->io_hdr.port_status; 12508 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12509 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12510 ctl_failover_io(io, /*have_lock*/ have_lock); 12511 return; 12512 } 12513 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12514 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12515 msg.scsi.sense_len, M_WAITOK); 12516 12517 #ifdef CTL_TIME_IO 12518 getbinuptime(&cur_bt); 12519 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12520 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12521 #endif 12522 io->io_hdr.num_dmas++; 12523 } 12524 12525 /* 12526 * The DMA to the remote side is done, now we need to tell the other side 12527 * we're done so it can continue with its data movement. 12528 */ 12529 static void 12530 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12531 { 12532 union ctl_io *io; 12533 uint32_t i; 12534 12535 io = rq->context; 12536 12537 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12538 printf("%s: ISC DMA write failed with error %d", __func__, 12539 rq->ret); 12540 ctl_set_internal_failure(&io->scsiio, 12541 /*sks_valid*/ 1, 12542 /*retry_count*/ rq->ret); 12543 } 12544 12545 ctl_dt_req_free(rq); 12546 12547 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12548 free(CTL_LSGLT(io)[i].addr, M_CTL); 12549 free(CTL_RSGL(io), M_CTL); 12550 CTL_RSGL(io) = NULL; 12551 CTL_LSGL(io) = NULL; 12552 12553 /* 12554 * The data is in local and remote memory, so now we need to send 12555 * status (good or back) back to the other side. 12556 */ 12557 ctl_send_datamove_done(io, /*have_lock*/ 0); 12558 } 12559 12560 /* 12561 * We've moved the data from the host/controller into local memory. Now we 12562 * need to push it over to the remote controller's memory. 12563 */ 12564 static int 12565 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12566 { 12567 int retval; 12568 12569 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12570 ctl_datamove_remote_write_cb); 12571 return (retval); 12572 } 12573 12574 static void 12575 ctl_datamove_remote_write(union ctl_io *io) 12576 { 12577 int retval; 12578 void (*fe_datamove)(union ctl_io *io); 12579 12580 /* 12581 * - Get the data from the host/HBA into local memory. 12582 * - DMA memory from the local controller to the remote controller. 12583 * - Send status back to the remote controller. 12584 */ 12585 12586 retval = ctl_datamove_remote_sgl_setup(io); 12587 if (retval != 0) 12588 return; 12589 12590 /* Switch the pointer over so the FETD knows what to do */ 12591 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12592 12593 /* 12594 * Use a custom move done callback, since we need to send completion 12595 * back to the other controller, not to the backend on this side. 12596 */ 12597 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12598 12599 fe_datamove = CTL_PORT(io)->fe_datamove; 12600 fe_datamove(io); 12601 } 12602 12603 static int 12604 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12605 { 12606 uint32_t i; 12607 12608 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12609 free(CTL_LSGLT(io)[i].addr, M_CTL); 12610 free(CTL_RSGL(io), M_CTL); 12611 CTL_RSGL(io) = NULL; 12612 CTL_LSGL(io) = NULL; 12613 12614 /* 12615 * The read is done, now we need to send status (good or bad) back 12616 * to the other side. 12617 */ 12618 ctl_send_datamove_done(io, /*have_lock*/ 0); 12619 12620 return (0); 12621 } 12622 12623 static void 12624 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12625 { 12626 union ctl_io *io; 12627 void (*fe_datamove)(union ctl_io *io); 12628 12629 io = rq->context; 12630 12631 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12632 printf("%s: ISC DMA read failed with error %d\n", __func__, 12633 rq->ret); 12634 ctl_set_internal_failure(&io->scsiio, 12635 /*sks_valid*/ 1, 12636 /*retry_count*/ rq->ret); 12637 } 12638 12639 ctl_dt_req_free(rq); 12640 12641 /* Switch the pointer over so the FETD knows what to do */ 12642 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12643 12644 /* 12645 * Use a custom move done callback, since we need to send completion 12646 * back to the other controller, not to the backend on this side. 12647 */ 12648 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12649 12650 /* XXX KDM add checks like the ones in ctl_datamove? */ 12651 12652 fe_datamove = CTL_PORT(io)->fe_datamove; 12653 fe_datamove(io); 12654 } 12655 12656 static int 12657 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12658 { 12659 struct ctl_sg_entry *local_sglist; 12660 uint32_t len_to_go; 12661 int retval; 12662 int i; 12663 12664 retval = 0; 12665 local_sglist = CTL_LSGL(io); 12666 len_to_go = io->scsiio.kern_data_len; 12667 12668 /* 12669 * The difficult thing here is that the size of the various 12670 * S/G segments may be different than the size from the 12671 * remote controller. That'll make it harder when DMAing 12672 * the data back to the other side. 12673 */ 12674 for (i = 0; len_to_go > 0; i++) { 12675 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12676 local_sglist[i].addr = 12677 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12678 12679 len_to_go -= local_sglist[i].len; 12680 } 12681 /* 12682 * Reset the number of S/G entries accordingly. The original 12683 * number of S/G entries is available in rem_sg_entries. 12684 */ 12685 io->scsiio.kern_sg_entries = i; 12686 12687 return (retval); 12688 } 12689 12690 static int 12691 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12692 ctl_ha_dt_cb callback) 12693 { 12694 struct ctl_ha_dt_req *rq; 12695 struct ctl_sg_entry *remote_sglist, *local_sglist; 12696 uint32_t local_used, remote_used, total_used; 12697 int i, j, isc_ret; 12698 12699 rq = ctl_dt_req_alloc(); 12700 12701 /* 12702 * If we failed to allocate the request, and if the DMA didn't fail 12703 * anyway, set busy status. This is just a resource allocation 12704 * failure. 12705 */ 12706 if ((rq == NULL) 12707 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12708 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12709 ctl_set_busy(&io->scsiio); 12710 12711 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12712 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12713 12714 if (rq != NULL) 12715 ctl_dt_req_free(rq); 12716 12717 /* 12718 * The data move failed. We need to return status back 12719 * to the other controller. No point in trying to DMA 12720 * data to the remote controller. 12721 */ 12722 12723 ctl_send_datamove_done(io, /*have_lock*/ 0); 12724 12725 return (1); 12726 } 12727 12728 local_sglist = CTL_LSGL(io); 12729 remote_sglist = CTL_RSGL(io); 12730 local_used = 0; 12731 remote_used = 0; 12732 total_used = 0; 12733 12734 /* 12735 * Pull/push the data over the wire from/to the other controller. 12736 * This takes into account the possibility that the local and 12737 * remote sglists may not be identical in terms of the size of 12738 * the elements and the number of elements. 12739 * 12740 * One fundamental assumption here is that the length allocated for 12741 * both the local and remote sglists is identical. Otherwise, we've 12742 * essentially got a coding error of some sort. 12743 */ 12744 isc_ret = CTL_HA_STATUS_SUCCESS; 12745 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12746 uint32_t cur_len; 12747 uint8_t *tmp_ptr; 12748 12749 rq->command = command; 12750 rq->context = io; 12751 12752 /* 12753 * Both pointers should be aligned. But it is possible 12754 * that the allocation length is not. They should both 12755 * also have enough slack left over at the end, though, 12756 * to round up to the next 8 byte boundary. 12757 */ 12758 cur_len = MIN(local_sglist[i].len - local_used, 12759 remote_sglist[j].len - remote_used); 12760 rq->size = cur_len; 12761 12762 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12763 tmp_ptr += local_used; 12764 12765 #if 0 12766 /* Use physical addresses when talking to ISC hardware */ 12767 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12768 /* XXX KDM use busdma */ 12769 rq->local = vtophys(tmp_ptr); 12770 } else 12771 rq->local = tmp_ptr; 12772 #else 12773 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12774 ("HA does not support BUS_ADDR")); 12775 rq->local = tmp_ptr; 12776 #endif 12777 12778 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12779 tmp_ptr += remote_used; 12780 rq->remote = tmp_ptr; 12781 12782 rq->callback = NULL; 12783 12784 local_used += cur_len; 12785 if (local_used >= local_sglist[i].len) { 12786 i++; 12787 local_used = 0; 12788 } 12789 12790 remote_used += cur_len; 12791 if (remote_used >= remote_sglist[j].len) { 12792 j++; 12793 remote_used = 0; 12794 } 12795 total_used += cur_len; 12796 12797 if (total_used >= io->scsiio.kern_data_len) 12798 rq->callback = callback; 12799 12800 isc_ret = ctl_dt_single(rq); 12801 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12802 break; 12803 } 12804 if (isc_ret != CTL_HA_STATUS_WAIT) { 12805 rq->ret = isc_ret; 12806 callback(rq); 12807 } 12808 12809 return (0); 12810 } 12811 12812 static void 12813 ctl_datamove_remote_read(union ctl_io *io) 12814 { 12815 int retval; 12816 uint32_t i; 12817 12818 /* 12819 * This will send an error to the other controller in the case of a 12820 * failure. 12821 */ 12822 retval = ctl_datamove_remote_sgl_setup(io); 12823 if (retval != 0) 12824 return; 12825 12826 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12827 ctl_datamove_remote_read_cb); 12828 if (retval != 0) { 12829 /* 12830 * Make sure we free memory if there was an error.. The 12831 * ctl_datamove_remote_xfer() function will send the 12832 * datamove done message, or call the callback with an 12833 * error if there is a problem. 12834 */ 12835 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12836 free(CTL_LSGLT(io)[i].addr, M_CTL); 12837 free(CTL_RSGL(io), M_CTL); 12838 CTL_RSGL(io) = NULL; 12839 CTL_LSGL(io) = NULL; 12840 } 12841 } 12842 12843 /* 12844 * Process a datamove request from the other controller. This is used for 12845 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12846 * first. Once that is complete, the data gets DMAed into the remote 12847 * controller's memory. For reads, we DMA from the remote controller's 12848 * memory into our memory first, and then move it out to the FETD. 12849 */ 12850 static void 12851 ctl_datamove_remote(union ctl_io *io) 12852 { 12853 12854 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12855 12856 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12857 ctl_failover_io(io, /*have_lock*/ 0); 12858 return; 12859 } 12860 12861 /* 12862 * Note that we look for an aborted I/O here, but don't do some of 12863 * the other checks that ctl_datamove() normally does. 12864 * We don't need to run the datamove delay code, since that should 12865 * have been done if need be on the other controller. 12866 */ 12867 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12868 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12869 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12870 io->io_hdr.nexus.targ_port, 12871 io->io_hdr.nexus.targ_lun); 12872 io->io_hdr.port_status = 31338; 12873 ctl_send_datamove_done(io, /*have_lock*/ 0); 12874 return; 12875 } 12876 12877 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12878 ctl_datamove_remote_write(io); 12879 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12880 ctl_datamove_remote_read(io); 12881 else { 12882 io->io_hdr.port_status = 31339; 12883 ctl_send_datamove_done(io, /*have_lock*/ 0); 12884 } 12885 } 12886 12887 static void 12888 ctl_process_done(union ctl_io *io) 12889 { 12890 struct ctl_softc *softc = CTL_SOFTC(io); 12891 struct ctl_port *port = CTL_PORT(io); 12892 struct ctl_lun *lun = CTL_LUN(io); 12893 void (*fe_done)(union ctl_io *io); 12894 union ctl_ha_msg msg; 12895 12896 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12897 fe_done = port->fe_done; 12898 12899 #ifdef CTL_TIME_IO 12900 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12901 char str[256]; 12902 char path_str[64]; 12903 struct sbuf sb; 12904 12905 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12906 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12907 12908 sbuf_cat(&sb, path_str); 12909 switch (io->io_hdr.io_type) { 12910 case CTL_IO_SCSI: 12911 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12912 sbuf_printf(&sb, "\n"); 12913 sbuf_cat(&sb, path_str); 12914 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12915 io->scsiio.tag_num, io->scsiio.tag_type); 12916 break; 12917 case CTL_IO_TASK: 12918 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12919 "Tag Type: %d\n", io->taskio.task_action, 12920 io->taskio.tag_num, io->taskio.tag_type); 12921 break; 12922 default: 12923 panic("%s: Invalid CTL I/O type %d\n", 12924 __func__, io->io_hdr.io_type); 12925 } 12926 sbuf_cat(&sb, path_str); 12927 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12928 (intmax_t)time_uptime - io->io_hdr.start_time); 12929 sbuf_finish(&sb); 12930 printf("%s", sbuf_data(&sb)); 12931 } 12932 #endif /* CTL_TIME_IO */ 12933 12934 switch (io->io_hdr.io_type) { 12935 case CTL_IO_SCSI: 12936 break; 12937 case CTL_IO_TASK: 12938 if (ctl_debug & CTL_DEBUG_INFO) 12939 ctl_io_error_print(io, NULL); 12940 fe_done(io); 12941 return; 12942 default: 12943 panic("%s: Invalid CTL I/O type %d\n", 12944 __func__, io->io_hdr.io_type); 12945 } 12946 12947 if (lun == NULL) { 12948 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12949 io->io_hdr.nexus.targ_mapped_lun)); 12950 goto bailout; 12951 } 12952 12953 mtx_lock(&lun->lun_lock); 12954 12955 /* 12956 * Check to see if we have any informational exception and status 12957 * of this command can be modified to report it in form of either 12958 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 12959 */ 12960 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 12961 io->io_hdr.status == CTL_SUCCESS && 12962 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 12963 uint8_t mrie = lun->MODE_IE.mrie; 12964 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 12965 (lun->MODE_VER.byte3 & SMS_VER_PER)); 12966 if (((mrie == SIEP_MRIE_REC_COND && per) || 12967 mrie == SIEP_MRIE_REC_UNCOND || 12968 mrie == SIEP_MRIE_NO_SENSE) && 12969 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 12970 CTL_CMD_FLAG_NO_SENSE) == 0) { 12971 ctl_set_sense(&io->scsiio, 12972 /*current_error*/ 1, 12973 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 12974 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 12975 /*asc*/ lun->ie_asc, 12976 /*ascq*/ lun->ie_ascq, 12977 SSD_ELEM_NONE); 12978 lun->ie_reported = 1; 12979 } 12980 } else if (lun->ie_reported < 0) 12981 lun->ie_reported = 0; 12982 12983 /* 12984 * Check to see if we have any errors to inject here. We only 12985 * inject errors for commands that don't already have errors set. 12986 */ 12987 if (!STAILQ_EMPTY(&lun->error_list) && 12988 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 12989 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 12990 ctl_inject_error(lun, io); 12991 12992 /* 12993 * XXX KDM how do we treat commands that aren't completed 12994 * successfully? 12995 * 12996 * XXX KDM should we also track I/O latency? 12997 */ 12998 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 12999 io->io_hdr.io_type == CTL_IO_SCSI) { 13000 int type; 13001 #ifdef CTL_TIME_IO 13002 struct bintime bt; 13003 13004 getbinuptime(&bt); 13005 bintime_sub(&bt, &io->io_hdr.start_bt); 13006 #endif 13007 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13008 CTL_FLAG_DATA_IN) 13009 type = CTL_STATS_READ; 13010 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13011 CTL_FLAG_DATA_OUT) 13012 type = CTL_STATS_WRITE; 13013 else 13014 type = CTL_STATS_NO_IO; 13015 13016 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13017 lun->stats.operations[type] ++; 13018 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13019 #ifdef CTL_TIME_IO 13020 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13021 bintime_add(&lun->stats.time[type], &bt); 13022 #endif 13023 13024 mtx_lock(&port->port_lock); 13025 port->stats.bytes[type] += io->scsiio.kern_total_len; 13026 port->stats.operations[type] ++; 13027 port->stats.dmas[type] += io->io_hdr.num_dmas; 13028 #ifdef CTL_TIME_IO 13029 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13030 bintime_add(&port->stats.time[type], &bt); 13031 #endif 13032 mtx_unlock(&port->port_lock); 13033 } 13034 13035 /* 13036 * Run through the blocked queue of this I/O and see if anything 13037 * can be unblocked, now that this I/O is done and will be removed. 13038 * We need to do it before removal to have OOA position to start. 13039 */ 13040 ctl_try_unblock_others(lun, io, TRUE); 13041 13042 /* 13043 * Remove this from the OOA queue. 13044 */ 13045 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13046 #ifdef CTL_TIME_IO 13047 if (TAILQ_EMPTY(&lun->ooa_queue)) 13048 lun->last_busy = getsbinuptime(); 13049 #endif 13050 13051 /* 13052 * If the LUN has been invalidated, free it if there is nothing 13053 * left on its OOA queue. 13054 */ 13055 if ((lun->flags & CTL_LUN_INVALID) 13056 && TAILQ_EMPTY(&lun->ooa_queue)) { 13057 mtx_unlock(&lun->lun_lock); 13058 ctl_free_lun(lun); 13059 } else 13060 mtx_unlock(&lun->lun_lock); 13061 13062 bailout: 13063 13064 /* 13065 * If this command has been aborted, make sure we set the status 13066 * properly. The FETD is responsible for freeing the I/O and doing 13067 * whatever it needs to do to clean up its state. 13068 */ 13069 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13070 ctl_set_task_aborted(&io->scsiio); 13071 13072 /* 13073 * If enabled, print command error status. 13074 */ 13075 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13076 (ctl_debug & CTL_DEBUG_INFO) != 0) 13077 ctl_io_error_print(io, NULL); 13078 13079 /* 13080 * Tell the FETD or the other shelf controller we're done with this 13081 * command. Note that only SCSI commands get to this point. Task 13082 * management commands are completed above. 13083 */ 13084 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13085 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13086 memset(&msg, 0, sizeof(msg)); 13087 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13088 msg.hdr.serializing_sc = io->io_hdr.remote_io; 13089 msg.hdr.nexus = io->io_hdr.nexus; 13090 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13091 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13092 M_WAITOK); 13093 } 13094 13095 fe_done(io); 13096 } 13097 13098 /* 13099 * Front end should call this if it doesn't do autosense. When the request 13100 * sense comes back in from the initiator, we'll dequeue this and send it. 13101 */ 13102 int 13103 ctl_queue_sense(union ctl_io *io) 13104 { 13105 struct ctl_softc *softc = CTL_SOFTC(io); 13106 struct ctl_port *port = CTL_PORT(io); 13107 struct ctl_lun *lun; 13108 struct scsi_sense_data *ps; 13109 uint32_t initidx, p, targ_lun; 13110 13111 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13112 13113 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13114 13115 /* 13116 * LUN lookup will likely move to the ctl_work_thread() once we 13117 * have our new queueing infrastructure (that doesn't put things on 13118 * a per-LUN queue initially). That is so that we can handle 13119 * things like an INQUIRY to a LUN that we don't have enabled. We 13120 * can't deal with that right now. 13121 * If we don't have a LUN for this, just toss the sense information. 13122 */ 13123 mtx_lock(&softc->ctl_lock); 13124 if (targ_lun >= ctl_max_luns || 13125 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13126 mtx_unlock(&softc->ctl_lock); 13127 goto bailout; 13128 } 13129 mtx_lock(&lun->lun_lock); 13130 mtx_unlock(&softc->ctl_lock); 13131 13132 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13133 p = initidx / CTL_MAX_INIT_PER_PORT; 13134 if (lun->pending_sense[p] == NULL) { 13135 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13136 M_CTL, M_NOWAIT | M_ZERO); 13137 } 13138 if ((ps = lun->pending_sense[p]) != NULL) { 13139 ps += initidx % CTL_MAX_INIT_PER_PORT; 13140 memset(ps, 0, sizeof(*ps)); 13141 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13142 } 13143 mtx_unlock(&lun->lun_lock); 13144 13145 bailout: 13146 ctl_free_io(io); 13147 return (CTL_RETVAL_COMPLETE); 13148 } 13149 13150 /* 13151 * Primary command inlet from frontend ports. All SCSI and task I/O 13152 * requests must go through this function. 13153 */ 13154 int 13155 ctl_queue(union ctl_io *io) 13156 { 13157 struct ctl_port *port = CTL_PORT(io); 13158 13159 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13160 13161 #ifdef CTL_TIME_IO 13162 io->io_hdr.start_time = time_uptime; 13163 getbinuptime(&io->io_hdr.start_bt); 13164 #endif /* CTL_TIME_IO */ 13165 13166 /* Map FE-specific LUN ID into global one. */ 13167 io->io_hdr.nexus.targ_mapped_lun = 13168 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13169 13170 switch (io->io_hdr.io_type) { 13171 case CTL_IO_SCSI: 13172 case CTL_IO_TASK: 13173 if (ctl_debug & CTL_DEBUG_CDB) 13174 ctl_io_print(io); 13175 ctl_enqueue_incoming(io); 13176 break; 13177 default: 13178 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13179 return (EINVAL); 13180 } 13181 13182 return (CTL_RETVAL_COMPLETE); 13183 } 13184 13185 #ifdef CTL_IO_DELAY 13186 static void 13187 ctl_done_timer_wakeup(void *arg) 13188 { 13189 union ctl_io *io; 13190 13191 io = (union ctl_io *)arg; 13192 ctl_done(io); 13193 } 13194 #endif /* CTL_IO_DELAY */ 13195 13196 void 13197 ctl_serseq_done(union ctl_io *io) 13198 { 13199 struct ctl_lun *lun = CTL_LUN(io); 13200 13201 if (lun->be_lun == NULL || 13202 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13203 return; 13204 mtx_lock(&lun->lun_lock); 13205 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13206 ctl_try_unblock_others(lun, io, FALSE); 13207 mtx_unlock(&lun->lun_lock); 13208 } 13209 13210 void 13211 ctl_done(union ctl_io *io) 13212 { 13213 13214 /* 13215 * Enable this to catch duplicate completion issues. 13216 */ 13217 #if 0 13218 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13219 printf("%s: type %d msg %d cdb %x iptl: " 13220 "%u:%u:%u tag 0x%04x " 13221 "flag %#x status %x\n", 13222 __func__, 13223 io->io_hdr.io_type, 13224 io->io_hdr.msg_type, 13225 io->scsiio.cdb[0], 13226 io->io_hdr.nexus.initid, 13227 io->io_hdr.nexus.targ_port, 13228 io->io_hdr.nexus.targ_lun, 13229 (io->io_hdr.io_type == 13230 CTL_IO_TASK) ? 13231 io->taskio.tag_num : 13232 io->scsiio.tag_num, 13233 io->io_hdr.flags, 13234 io->io_hdr.status); 13235 } else 13236 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13237 #endif 13238 13239 /* 13240 * This is an internal copy of an I/O, and should not go through 13241 * the normal done processing logic. 13242 */ 13243 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13244 return; 13245 13246 #ifdef CTL_IO_DELAY 13247 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13248 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13249 } else { 13250 struct ctl_lun *lun = CTL_LUN(io); 13251 13252 if ((lun != NULL) 13253 && (lun->delay_info.done_delay > 0)) { 13254 13255 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13256 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13257 callout_reset(&io->io_hdr.delay_callout, 13258 lun->delay_info.done_delay * hz, 13259 ctl_done_timer_wakeup, io); 13260 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13261 lun->delay_info.done_delay = 0; 13262 return; 13263 } 13264 } 13265 #endif /* CTL_IO_DELAY */ 13266 13267 ctl_enqueue_done(io); 13268 } 13269 13270 static void 13271 ctl_work_thread(void *arg) 13272 { 13273 struct ctl_thread *thr = (struct ctl_thread *)arg; 13274 struct ctl_softc *softc = thr->ctl_softc; 13275 union ctl_io *io; 13276 int retval; 13277 13278 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13279 thread_lock(curthread); 13280 sched_prio(curthread, PUSER - 1); 13281 thread_unlock(curthread); 13282 13283 while (!softc->shutdown) { 13284 /* 13285 * We handle the queues in this order: 13286 * - ISC 13287 * - done queue (to free up resources, unblock other commands) 13288 * - incoming queue 13289 * - RtR queue 13290 * 13291 * If those queues are empty, we break out of the loop and 13292 * go to sleep. 13293 */ 13294 mtx_lock(&thr->queue_lock); 13295 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13296 if (io != NULL) { 13297 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13298 mtx_unlock(&thr->queue_lock); 13299 ctl_handle_isc(io); 13300 continue; 13301 } 13302 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13303 if (io != NULL) { 13304 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13305 /* clear any blocked commands, call fe_done */ 13306 mtx_unlock(&thr->queue_lock); 13307 ctl_process_done(io); 13308 continue; 13309 } 13310 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13311 if (io != NULL) { 13312 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13313 mtx_unlock(&thr->queue_lock); 13314 if (io->io_hdr.io_type == CTL_IO_TASK) 13315 ctl_run_task(io); 13316 else 13317 ctl_scsiio_precheck(softc, &io->scsiio); 13318 continue; 13319 } 13320 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13321 if (io != NULL) { 13322 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13323 mtx_unlock(&thr->queue_lock); 13324 retval = ctl_scsiio(&io->scsiio); 13325 if (retval != CTL_RETVAL_COMPLETE) 13326 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13327 continue; 13328 } 13329 13330 /* Sleep until we have something to do. */ 13331 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0); 13332 } 13333 thr->thread = NULL; 13334 kthread_exit(); 13335 } 13336 13337 static void 13338 ctl_thresh_thread(void *arg) 13339 { 13340 struct ctl_softc *softc = (struct ctl_softc *)arg; 13341 struct ctl_lun *lun; 13342 struct ctl_logical_block_provisioning_page *page; 13343 const char *attr; 13344 union ctl_ha_msg msg; 13345 uint64_t thres, val; 13346 int i, e, set; 13347 13348 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13349 thread_lock(curthread); 13350 sched_prio(curthread, PUSER - 1); 13351 thread_unlock(curthread); 13352 13353 while (!softc->shutdown) { 13354 mtx_lock(&softc->ctl_lock); 13355 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13356 if ((lun->flags & CTL_LUN_DISABLED) || 13357 (lun->flags & CTL_LUN_NO_MEDIA) || 13358 lun->backend->lun_attr == NULL) 13359 continue; 13360 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13361 softc->ha_mode == CTL_HA_MODE_XFER) 13362 continue; 13363 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13364 continue; 13365 e = 0; 13366 page = &lun->MODE_LBP; 13367 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13368 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13369 continue; 13370 thres = scsi_4btoul(page->descr[i].count); 13371 thres <<= CTL_LBP_EXPONENT; 13372 switch (page->descr[i].resource) { 13373 case 0x01: 13374 attr = "blocksavail"; 13375 break; 13376 case 0x02: 13377 attr = "blocksused"; 13378 break; 13379 case 0xf1: 13380 attr = "poolblocksavail"; 13381 break; 13382 case 0xf2: 13383 attr = "poolblocksused"; 13384 break; 13385 default: 13386 continue; 13387 } 13388 mtx_unlock(&softc->ctl_lock); // XXX 13389 val = lun->backend->lun_attr( 13390 lun->be_lun->be_lun, attr); 13391 mtx_lock(&softc->ctl_lock); 13392 if (val == UINT64_MAX) 13393 continue; 13394 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13395 == SLBPPD_ARMING_INC) 13396 e = (val >= thres); 13397 else 13398 e = (val <= thres); 13399 if (e) 13400 break; 13401 } 13402 mtx_lock(&lun->lun_lock); 13403 if (e) { 13404 scsi_u64to8b((uint8_t *)&page->descr[i] - 13405 (uint8_t *)page, lun->ua_tpt_info); 13406 if (lun->lasttpt == 0 || 13407 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13408 lun->lasttpt = time_uptime; 13409 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13410 set = 1; 13411 } else 13412 set = 0; 13413 } else { 13414 lun->lasttpt = 0; 13415 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13416 set = -1; 13417 } 13418 mtx_unlock(&lun->lun_lock); 13419 if (set != 0 && 13420 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13421 /* Send msg to other side. */ 13422 bzero(&msg.ua, sizeof(msg.ua)); 13423 msg.hdr.msg_type = CTL_MSG_UA; 13424 msg.hdr.nexus.initid = -1; 13425 msg.hdr.nexus.targ_port = -1; 13426 msg.hdr.nexus.targ_lun = lun->lun; 13427 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13428 msg.ua.ua_all = 1; 13429 msg.ua.ua_set = (set > 0); 13430 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13431 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13432 mtx_unlock(&softc->ctl_lock); // XXX 13433 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13434 sizeof(msg.ua), M_WAITOK); 13435 mtx_lock(&softc->ctl_lock); 13436 } 13437 } 13438 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13439 PDROP, "-", CTL_LBP_PERIOD * hz); 13440 } 13441 softc->thresh_thread = NULL; 13442 kthread_exit(); 13443 } 13444 13445 static void 13446 ctl_enqueue_incoming(union ctl_io *io) 13447 { 13448 struct ctl_softc *softc = CTL_SOFTC(io); 13449 struct ctl_thread *thr; 13450 u_int idx; 13451 13452 idx = (io->io_hdr.nexus.targ_port * 127 + 13453 io->io_hdr.nexus.initid) % worker_threads; 13454 thr = &softc->threads[idx]; 13455 mtx_lock(&thr->queue_lock); 13456 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13457 mtx_unlock(&thr->queue_lock); 13458 wakeup(thr); 13459 } 13460 13461 static void 13462 ctl_enqueue_rtr(union ctl_io *io) 13463 { 13464 struct ctl_softc *softc = CTL_SOFTC(io); 13465 struct ctl_thread *thr; 13466 13467 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13468 mtx_lock(&thr->queue_lock); 13469 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13470 mtx_unlock(&thr->queue_lock); 13471 wakeup(thr); 13472 } 13473 13474 static void 13475 ctl_enqueue_done(union ctl_io *io) 13476 { 13477 struct ctl_softc *softc = CTL_SOFTC(io); 13478 struct ctl_thread *thr; 13479 13480 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13481 mtx_lock(&thr->queue_lock); 13482 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13483 mtx_unlock(&thr->queue_lock); 13484 wakeup(thr); 13485 } 13486 13487 static void 13488 ctl_enqueue_isc(union ctl_io *io) 13489 { 13490 struct ctl_softc *softc = CTL_SOFTC(io); 13491 struct ctl_thread *thr; 13492 13493 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13494 mtx_lock(&thr->queue_lock); 13495 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13496 mtx_unlock(&thr->queue_lock); 13497 wakeup(thr); 13498 } 13499 13500 /* 13501 * vim: ts=8 13502 */ 13503