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/sysctl.h> 70 #include <sys/nv.h> 71 #include <sys/dnv.h> 72 #include <vm/uma.h> 73 74 #include <cam/cam.h> 75 #include <cam/scsi/scsi_all.h> 76 #include <cam/scsi/scsi_cd.h> 77 #include <cam/scsi/scsi_da.h> 78 #include <cam/ctl/ctl_io.h> 79 #include <cam/ctl/ctl.h> 80 #include <cam/ctl/ctl_frontend.h> 81 #include <cam/ctl/ctl_util.h> 82 #include <cam/ctl/ctl_backend.h> 83 #include <cam/ctl/ctl_ioctl.h> 84 #include <cam/ctl/ctl_ha.h> 85 #include <cam/ctl/ctl_private.h> 86 #include <cam/ctl/ctl_debug.h> 87 #include <cam/ctl/ctl_scsi_all.h> 88 #include <cam/ctl/ctl_error.h> 89 90 struct ctl_softc *control_softc = NULL; 91 92 /* 93 * Template mode pages. 94 */ 95 96 /* 97 * Note that these are default values only. The actual values will be 98 * filled in when the user does a mode sense. 99 */ 100 const static struct scsi_da_rw_recovery_page rw_er_page_default = { 101 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 102 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 103 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 104 /*read_retry_count*/0, 105 /*correction_span*/0, 106 /*head_offset_count*/0, 107 /*data_strobe_offset_cnt*/0, 108 /*byte8*/SMS_RWER_LBPERE, 109 /*write_retry_count*/0, 110 /*reserved2*/0, 111 /*recovery_time_limit*/{0, 0}, 112 }; 113 114 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 117 /*byte3*/SMS_RWER_PER, 118 /*read_retry_count*/0, 119 /*correction_span*/0, 120 /*head_offset_count*/0, 121 /*data_strobe_offset_cnt*/0, 122 /*byte8*/SMS_RWER_LBPERE, 123 /*write_retry_count*/0, 124 /*reserved2*/0, 125 /*recovery_time_limit*/{0, 0}, 126 }; 127 128 const static struct scsi_format_page format_page_default = { 129 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 130 /*page_length*/sizeof(struct scsi_format_page) - 2, 131 /*tracks_per_zone*/ {0, 0}, 132 /*alt_sectors_per_zone*/ {0, 0}, 133 /*alt_tracks_per_zone*/ {0, 0}, 134 /*alt_tracks_per_lun*/ {0, 0}, 135 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 136 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 137 /*bytes_per_sector*/ {0, 0}, 138 /*interleave*/ {0, 0}, 139 /*track_skew*/ {0, 0}, 140 /*cylinder_skew*/ {0, 0}, 141 /*flags*/ SFP_HSEC, 142 /*reserved*/ {0, 0, 0} 143 }; 144 145 const static struct scsi_format_page format_page_changeable = { 146 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 147 /*page_length*/sizeof(struct scsi_format_page) - 2, 148 /*tracks_per_zone*/ {0, 0}, 149 /*alt_sectors_per_zone*/ {0, 0}, 150 /*alt_tracks_per_zone*/ {0, 0}, 151 /*alt_tracks_per_lun*/ {0, 0}, 152 /*sectors_per_track*/ {0, 0}, 153 /*bytes_per_sector*/ {0, 0}, 154 /*interleave*/ {0, 0}, 155 /*track_skew*/ {0, 0}, 156 /*cylinder_skew*/ {0, 0}, 157 /*flags*/ 0, 158 /*reserved*/ {0, 0, 0} 159 }; 160 161 const static struct scsi_rigid_disk_page rigid_disk_page_default = { 162 /*page_code*/SMS_RIGID_DISK_PAGE, 163 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 164 /*cylinders*/ {0, 0, 0}, 165 /*heads*/ CTL_DEFAULT_HEADS, 166 /*start_write_precomp*/ {0, 0, 0}, 167 /*start_reduced_current*/ {0, 0, 0}, 168 /*step_rate*/ {0, 0}, 169 /*landing_zone_cylinder*/ {0, 0, 0}, 170 /*rpl*/ SRDP_RPL_DISABLED, 171 /*rotational_offset*/ 0, 172 /*reserved1*/ 0, 173 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 174 CTL_DEFAULT_ROTATION_RATE & 0xff}, 175 /*reserved2*/ {0, 0} 176 }; 177 178 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 179 /*page_code*/SMS_RIGID_DISK_PAGE, 180 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 181 /*cylinders*/ {0, 0, 0}, 182 /*heads*/ 0, 183 /*start_write_precomp*/ {0, 0, 0}, 184 /*start_reduced_current*/ {0, 0, 0}, 185 /*step_rate*/ {0, 0}, 186 /*landing_zone_cylinder*/ {0, 0, 0}, 187 /*rpl*/ 0, 188 /*rotational_offset*/ 0, 189 /*reserved1*/ 0, 190 /*rotation_rate*/ {0, 0}, 191 /*reserved2*/ {0, 0} 192 }; 193 194 const static struct scsi_da_verify_recovery_page verify_er_page_default = { 195 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 196 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 197 /*byte3*/0, 198 /*read_retry_count*/0, 199 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 200 /*recovery_time_limit*/{0, 0}, 201 }; 202 203 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 204 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 205 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 206 /*byte3*/SMS_VER_PER, 207 /*read_retry_count*/0, 208 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 209 /*recovery_time_limit*/{0, 0}, 210 }; 211 212 const static struct scsi_caching_page caching_page_default = { 213 /*page_code*/SMS_CACHING_PAGE, 214 /*page_length*/sizeof(struct scsi_caching_page) - 2, 215 /*flags1*/ SCP_DISC | SCP_WCE, 216 /*ret_priority*/ 0, 217 /*disable_pf_transfer_len*/ {0xff, 0xff}, 218 /*min_prefetch*/ {0, 0}, 219 /*max_prefetch*/ {0xff, 0xff}, 220 /*max_pf_ceiling*/ {0xff, 0xff}, 221 /*flags2*/ 0, 222 /*cache_segments*/ 0, 223 /*cache_seg_size*/ {0, 0}, 224 /*reserved*/ 0, 225 /*non_cache_seg_size*/ {0, 0, 0} 226 }; 227 228 const static struct scsi_caching_page caching_page_changeable = { 229 /*page_code*/SMS_CACHING_PAGE, 230 /*page_length*/sizeof(struct scsi_caching_page) - 2, 231 /*flags1*/ SCP_WCE | SCP_RCD, 232 /*ret_priority*/ 0, 233 /*disable_pf_transfer_len*/ {0, 0}, 234 /*min_prefetch*/ {0, 0}, 235 /*max_prefetch*/ {0, 0}, 236 /*max_pf_ceiling*/ {0, 0}, 237 /*flags2*/ 0, 238 /*cache_segments*/ 0, 239 /*cache_seg_size*/ {0, 0}, 240 /*reserved*/ 0, 241 /*non_cache_seg_size*/ {0, 0, 0} 242 }; 243 244 const static struct scsi_control_page control_page_default = { 245 /*page_code*/SMS_CONTROL_MODE_PAGE, 246 /*page_length*/sizeof(struct scsi_control_page) - 2, 247 /*rlec*/0, 248 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 249 /*eca_and_aen*/0, 250 /*flags4*/SCP_TAS, 251 /*aen_holdoff_period*/{0, 0}, 252 /*busy_timeout_period*/{0, 0}, 253 /*extended_selftest_completion_time*/{0, 0} 254 }; 255 256 const static struct scsi_control_page control_page_changeable = { 257 /*page_code*/SMS_CONTROL_MODE_PAGE, 258 /*page_length*/sizeof(struct scsi_control_page) - 2, 259 /*rlec*/SCP_DSENSE, 260 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 261 /*eca_and_aen*/SCP_SWP, 262 /*flags4*/0, 263 /*aen_holdoff_period*/{0, 0}, 264 /*busy_timeout_period*/{0, 0}, 265 /*extended_selftest_completion_time*/{0, 0} 266 }; 267 268 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 269 270 const static struct scsi_control_ext_page control_ext_page_default = { 271 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 272 /*subpage_code*/0x01, 273 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 274 /*flags*/0, 275 /*prio*/0, 276 /*max_sense*/0 277 }; 278 279 const static struct scsi_control_ext_page control_ext_page_changeable = { 280 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 281 /*subpage_code*/0x01, 282 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 283 /*flags*/0, 284 /*prio*/0, 285 /*max_sense*/0xff 286 }; 287 288 const static struct scsi_info_exceptions_page ie_page_default = { 289 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 290 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 291 /*info_flags*/SIEP_FLAGS_EWASC, 292 /*mrie*/SIEP_MRIE_NO, 293 /*interval_timer*/{0, 0, 0, 0}, 294 /*report_count*/{0, 0, 0, 1} 295 }; 296 297 const static struct scsi_info_exceptions_page ie_page_changeable = { 298 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 299 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 300 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 301 SIEP_FLAGS_LOGERR, 302 /*mrie*/0x0f, 303 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 304 /*report_count*/{0xff, 0xff, 0xff, 0xff} 305 }; 306 307 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 308 309 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 310 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 311 /*subpage_code*/0x02, 312 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 313 /*flags*/0, 314 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 315 /*descr*/{}}, 316 {{/*flags*/0, 317 /*resource*/0x01, 318 /*reserved*/{0, 0}, 319 /*count*/{0, 0, 0, 0}}, 320 {/*flags*/0, 321 /*resource*/0x02, 322 /*reserved*/{0, 0}, 323 /*count*/{0, 0, 0, 0}}, 324 {/*flags*/0, 325 /*resource*/0xf1, 326 /*reserved*/{0, 0}, 327 /*count*/{0, 0, 0, 0}}, 328 {/*flags*/0, 329 /*resource*/0xf2, 330 /*reserved*/{0, 0}, 331 /*count*/{0, 0, 0, 0}} 332 } 333 }; 334 335 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 336 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 337 /*subpage_code*/0x02, 338 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 339 /*flags*/SLBPP_SITUA, 340 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 341 /*descr*/{}}, 342 {{/*flags*/0, 343 /*resource*/0, 344 /*reserved*/{0, 0}, 345 /*count*/{0, 0, 0, 0}}, 346 {/*flags*/0, 347 /*resource*/0, 348 /*reserved*/{0, 0}, 349 /*count*/{0, 0, 0, 0}}, 350 {/*flags*/0, 351 /*resource*/0, 352 /*reserved*/{0, 0}, 353 /*count*/{0, 0, 0, 0}}, 354 {/*flags*/0, 355 /*resource*/0, 356 /*reserved*/{0, 0}, 357 /*count*/{0, 0, 0, 0}} 358 } 359 }; 360 361 const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 362 /*page_code*/SMS_CDDVD_CAPS_PAGE, 363 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 364 /*caps1*/0x3f, 365 /*caps2*/0x00, 366 /*caps3*/0xf0, 367 /*caps4*/0x00, 368 /*caps5*/0x29, 369 /*caps6*/0x00, 370 /*obsolete*/{0, 0}, 371 /*nvol_levels*/{0, 0}, 372 /*buffer_size*/{8, 0}, 373 /*obsolete2*/{0, 0}, 374 /*reserved*/0, 375 /*digital*/0, 376 /*obsolete3*/0, 377 /*copy_management*/0, 378 /*reserved2*/0, 379 /*rotation_control*/0, 380 /*cur_write_speed*/0, 381 /*num_speed_descr*/0, 382 }; 383 384 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 385 /*page_code*/SMS_CDDVD_CAPS_PAGE, 386 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 387 /*caps1*/0, 388 /*caps2*/0, 389 /*caps3*/0, 390 /*caps4*/0, 391 /*caps5*/0, 392 /*caps6*/0, 393 /*obsolete*/{0, 0}, 394 /*nvol_levels*/{0, 0}, 395 /*buffer_size*/{0, 0}, 396 /*obsolete2*/{0, 0}, 397 /*reserved*/0, 398 /*digital*/0, 399 /*obsolete3*/0, 400 /*copy_management*/0, 401 /*reserved2*/0, 402 /*rotation_control*/0, 403 /*cur_write_speed*/0, 404 /*num_speed_descr*/0, 405 }; 406 407 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 408 static int worker_threads = -1; 409 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 410 &worker_threads, 1, "Number of worker threads"); 411 static int ctl_debug = CTL_DEBUG_NONE; 412 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 413 &ctl_debug, 0, "Enabled debug flags"); 414 static int ctl_lun_map_size = 1024; 415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 416 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 417 #ifdef CTL_TIME_IO 418 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS; 419 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN, 420 &ctl_time_io_secs, 0, "Log requests taking more seconds"); 421 #endif 422 423 /* 424 * Maximum number of LUNs we support. MUST be a power of 2. 425 */ 426 #define CTL_DEFAULT_MAX_LUNS 1024 427 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 428 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns); 429 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN, 430 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs"); 431 432 /* 433 * Maximum number of ports registered at one time. 434 */ 435 #define CTL_DEFAULT_MAX_PORTS 256 436 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 437 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports); 438 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN, 439 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports"); 440 441 /* 442 * Maximum number of initiators we support. 443 */ 444 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports) 445 446 /* 447 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 448 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 449 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 450 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 451 */ 452 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 453 454 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 455 int param); 456 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 457 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 458 static int ctl_init(void); 459 static int ctl_shutdown(void); 460 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 461 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 462 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 463 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 464 struct ctl_ooa *ooa_hdr, 465 struct ctl_ooa_entry *kern_entries); 466 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 467 struct thread *td); 468 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 469 struct ctl_be_lun *be_lun); 470 static int ctl_free_lun(struct ctl_lun *lun); 471 static void ctl_create_lun(struct ctl_be_lun *be_lun); 472 473 static int ctl_do_mode_select(union ctl_io *io); 474 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 475 uint64_t res_key, uint64_t sa_res_key, 476 uint8_t type, uint32_t residx, 477 struct ctl_scsiio *ctsio, 478 struct scsi_per_res_out *cdb, 479 struct scsi_per_res_out_parms* param); 480 static void ctl_pro_preempt_other(struct ctl_lun *lun, 481 union ctl_ha_msg *msg); 482 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 483 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 484 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 485 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 486 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 487 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 488 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 489 int alloc_len); 490 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 491 int alloc_len); 492 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 493 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 494 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 495 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 496 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 497 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 498 bool seq); 499 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 500 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 501 union ctl_io *pending_io, union ctl_io *ooa_io); 502 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 503 union ctl_io *starting_io); 504 static int ctl_check_blocked(struct ctl_lun *lun); 505 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 506 const struct ctl_cmd_entry *entry, 507 struct ctl_scsiio *ctsio); 508 static void ctl_failover_lun(union ctl_io *io); 509 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 510 struct ctl_scsiio *ctsio); 511 static int ctl_scsiio(struct ctl_scsiio *ctsio); 512 513 static int ctl_target_reset(union ctl_io *io); 514 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, 515 ctl_ua_type ua_type); 516 static int ctl_lun_reset(union ctl_io *io); 517 static int ctl_abort_task(union ctl_io *io); 518 static int ctl_abort_task_set(union ctl_io *io); 519 static int ctl_query_task(union ctl_io *io, int task_set); 520 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 521 ctl_ua_type ua_type); 522 static int ctl_i_t_nexus_reset(union ctl_io *io); 523 static int ctl_query_async_event(union ctl_io *io); 524 static void ctl_run_task(union ctl_io *io); 525 #ifdef CTL_IO_DELAY 526 static void ctl_datamove_timer_wakeup(void *arg); 527 static void ctl_done_timer_wakeup(void *arg); 528 #endif /* CTL_IO_DELAY */ 529 530 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 531 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 532 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 533 static void ctl_datamove_remote_write(union ctl_io *io); 534 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 535 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 536 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 537 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 538 ctl_ha_dt_cb callback); 539 static void ctl_datamove_remote_read(union ctl_io *io); 540 static void ctl_datamove_remote(union ctl_io *io); 541 static void ctl_process_done(union ctl_io *io); 542 static void ctl_lun_thread(void *arg); 543 static void ctl_thresh_thread(void *arg); 544 static void ctl_work_thread(void *arg); 545 static void ctl_enqueue_incoming(union ctl_io *io); 546 static void ctl_enqueue_rtr(union ctl_io *io); 547 static void ctl_enqueue_done(union ctl_io *io); 548 static void ctl_enqueue_isc(union ctl_io *io); 549 static const struct ctl_cmd_entry * 550 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 551 static const struct ctl_cmd_entry * 552 ctl_validate_command(struct ctl_scsiio *ctsio); 553 static int ctl_cmd_applicable(uint8_t lun_type, 554 const struct ctl_cmd_entry *entry); 555 static int ctl_ha_init(void); 556 static int ctl_ha_shutdown(void); 557 558 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 559 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 560 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 561 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 562 563 /* 564 * Load the serialization table. This isn't very pretty, but is probably 565 * the easiest way to do it. 566 */ 567 #include "ctl_ser_table.c" 568 569 /* 570 * We only need to define open, close and ioctl routines for this driver. 571 */ 572 static struct cdevsw ctl_cdevsw = { 573 .d_version = D_VERSION, 574 .d_flags = 0, 575 .d_open = ctl_open, 576 .d_close = ctl_close, 577 .d_ioctl = ctl_ioctl, 578 .d_name = "ctl", 579 }; 580 581 582 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 583 584 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 585 586 static moduledata_t ctl_moduledata = { 587 "ctl", 588 ctl_module_event_handler, 589 NULL 590 }; 591 592 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 593 MODULE_VERSION(ctl, 1); 594 595 static struct ctl_frontend ha_frontend = 596 { 597 .name = "ha", 598 .init = ctl_ha_init, 599 .shutdown = ctl_ha_shutdown, 600 }; 601 602 static int 603 ctl_ha_init(void) 604 { 605 struct ctl_softc *softc = control_softc; 606 607 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 608 &softc->othersc_pool) != 0) 609 return (ENOMEM); 610 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 611 ctl_pool_free(softc->othersc_pool); 612 return (EIO); 613 } 614 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 615 != CTL_HA_STATUS_SUCCESS) { 616 ctl_ha_msg_destroy(softc); 617 ctl_pool_free(softc->othersc_pool); 618 return (EIO); 619 } 620 return (0); 621 }; 622 623 static int 624 ctl_ha_shutdown(void) 625 { 626 struct ctl_softc *softc = control_softc; 627 struct ctl_port *port; 628 629 ctl_ha_msg_shutdown(softc); 630 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 631 return (EIO); 632 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 633 return (EIO); 634 ctl_pool_free(softc->othersc_pool); 635 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 636 ctl_port_deregister(port); 637 free(port->port_name, M_CTL); 638 free(port, M_CTL); 639 } 640 return (0); 641 }; 642 643 static void 644 ctl_ha_datamove(union ctl_io *io) 645 { 646 struct ctl_lun *lun = CTL_LUN(io); 647 struct ctl_sg_entry *sgl; 648 union ctl_ha_msg msg; 649 uint32_t sg_entries_sent; 650 int do_sg_copy, i, j; 651 652 memset(&msg.dt, 0, sizeof(msg.dt)); 653 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 654 msg.hdr.original_sc = io->io_hdr.remote_io; 655 msg.hdr.serializing_sc = io; 656 msg.hdr.nexus = io->io_hdr.nexus; 657 msg.hdr.status = io->io_hdr.status; 658 msg.dt.flags = io->io_hdr.flags; 659 660 /* 661 * We convert everything into a S/G list here. We can't 662 * pass by reference, only by value between controllers. 663 * So we can't pass a pointer to the S/G list, only as many 664 * S/G entries as we can fit in here. If it's possible for 665 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 666 * then we need to break this up into multiple transfers. 667 */ 668 if (io->scsiio.kern_sg_entries == 0) { 669 msg.dt.kern_sg_entries = 1; 670 #if 0 671 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 672 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 673 } else { 674 /* XXX KDM use busdma here! */ 675 msg.dt.sg_list[0].addr = 676 (void *)vtophys(io->scsiio.kern_data_ptr); 677 } 678 #else 679 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 680 ("HA does not support BUS_ADDR")); 681 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 682 #endif 683 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 684 do_sg_copy = 0; 685 } else { 686 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 687 do_sg_copy = 1; 688 } 689 690 msg.dt.kern_data_len = io->scsiio.kern_data_len; 691 msg.dt.kern_total_len = io->scsiio.kern_total_len; 692 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 693 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 694 msg.dt.sg_sequence = 0; 695 696 /* 697 * Loop until we've sent all of the S/G entries. On the 698 * other end, we'll recompose these S/G entries into one 699 * contiguous list before processing. 700 */ 701 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 702 msg.dt.sg_sequence++) { 703 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 704 sizeof(msg.dt.sg_list[0])), 705 msg.dt.kern_sg_entries - sg_entries_sent); 706 if (do_sg_copy != 0) { 707 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 708 for (i = sg_entries_sent, j = 0; 709 i < msg.dt.cur_sg_entries; i++, j++) { 710 #if 0 711 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 712 msg.dt.sg_list[j].addr = sgl[i].addr; 713 } else { 714 /* XXX KDM use busdma here! */ 715 msg.dt.sg_list[j].addr = 716 (void *)vtophys(sgl[i].addr); 717 } 718 #else 719 KASSERT((io->io_hdr.flags & 720 CTL_FLAG_BUS_ADDR) == 0, 721 ("HA does not support BUS_ADDR")); 722 msg.dt.sg_list[j].addr = sgl[i].addr; 723 #endif 724 msg.dt.sg_list[j].len = sgl[i].len; 725 } 726 } 727 728 sg_entries_sent += msg.dt.cur_sg_entries; 729 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 730 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 731 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 732 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 733 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 734 io->io_hdr.port_status = 31341; 735 io->scsiio.be_move_done(io); 736 return; 737 } 738 msg.dt.sent_sg_entries = sg_entries_sent; 739 } 740 741 /* 742 * Officially handover the request from us to peer. 743 * If failover has just happened, then we must return error. 744 * If failover happen just after, then it is not our problem. 745 */ 746 if (lun) 747 mtx_lock(&lun->lun_lock); 748 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 749 if (lun) 750 mtx_unlock(&lun->lun_lock); 751 io->io_hdr.port_status = 31342; 752 io->scsiio.be_move_done(io); 753 return; 754 } 755 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 756 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 757 if (lun) 758 mtx_unlock(&lun->lun_lock); 759 } 760 761 static void 762 ctl_ha_done(union ctl_io *io) 763 { 764 union ctl_ha_msg msg; 765 766 if (io->io_hdr.io_type == CTL_IO_SCSI) { 767 memset(&msg, 0, sizeof(msg)); 768 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 769 msg.hdr.original_sc = io->io_hdr.remote_io; 770 msg.hdr.nexus = io->io_hdr.nexus; 771 msg.hdr.status = io->io_hdr.status; 772 msg.scsi.scsi_status = io->scsiio.scsi_status; 773 msg.scsi.tag_num = io->scsiio.tag_num; 774 msg.scsi.tag_type = io->scsiio.tag_type; 775 msg.scsi.sense_len = io->scsiio.sense_len; 776 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 777 io->scsiio.sense_len); 778 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 779 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 780 msg.scsi.sense_len, M_WAITOK); 781 } 782 ctl_free_io(io); 783 } 784 785 static void 786 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 787 union ctl_ha_msg *msg_info) 788 { 789 struct ctl_scsiio *ctsio; 790 791 if (msg_info->hdr.original_sc == NULL) { 792 printf("%s: original_sc == NULL!\n", __func__); 793 /* XXX KDM now what? */ 794 return; 795 } 796 797 ctsio = &msg_info->hdr.original_sc->scsiio; 798 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 799 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 800 ctsio->io_hdr.status = msg_info->hdr.status; 801 ctsio->scsi_status = msg_info->scsi.scsi_status; 802 ctsio->sense_len = msg_info->scsi.sense_len; 803 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 804 msg_info->scsi.sense_len); 805 ctl_enqueue_isc((union ctl_io *)ctsio); 806 } 807 808 static void 809 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 810 union ctl_ha_msg *msg_info) 811 { 812 struct ctl_scsiio *ctsio; 813 814 if (msg_info->hdr.serializing_sc == NULL) { 815 printf("%s: serializing_sc == NULL!\n", __func__); 816 /* XXX KDM now what? */ 817 return; 818 } 819 820 ctsio = &msg_info->hdr.serializing_sc->scsiio; 821 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 822 ctl_enqueue_isc((union ctl_io *)ctsio); 823 } 824 825 void 826 ctl_isc_announce_lun(struct ctl_lun *lun) 827 { 828 struct ctl_softc *softc = lun->ctl_softc; 829 union ctl_ha_msg *msg; 830 struct ctl_ha_msg_lun_pr_key pr_key; 831 int i, k; 832 833 if (softc->ha_link != CTL_HA_LINK_ONLINE) 834 return; 835 mtx_lock(&lun->lun_lock); 836 i = sizeof(msg->lun); 837 if (lun->lun_devid) 838 i += lun->lun_devid->len; 839 i += sizeof(pr_key) * lun->pr_key_count; 840 alloc: 841 mtx_unlock(&lun->lun_lock); 842 msg = malloc(i, M_CTL, M_WAITOK); 843 mtx_lock(&lun->lun_lock); 844 k = sizeof(msg->lun); 845 if (lun->lun_devid) 846 k += lun->lun_devid->len; 847 k += sizeof(pr_key) * lun->pr_key_count; 848 if (i < k) { 849 free(msg, M_CTL); 850 i = k; 851 goto alloc; 852 } 853 bzero(&msg->lun, sizeof(msg->lun)); 854 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 855 msg->hdr.nexus.targ_lun = lun->lun; 856 msg->hdr.nexus.targ_mapped_lun = lun->lun; 857 msg->lun.flags = lun->flags; 858 msg->lun.pr_generation = lun->pr_generation; 859 msg->lun.pr_res_idx = lun->pr_res_idx; 860 msg->lun.pr_res_type = lun->pr_res_type; 861 msg->lun.pr_key_count = lun->pr_key_count; 862 i = 0; 863 if (lun->lun_devid) { 864 msg->lun.lun_devid_len = lun->lun_devid->len; 865 memcpy(&msg->lun.data[i], lun->lun_devid->data, 866 msg->lun.lun_devid_len); 867 i += msg->lun.lun_devid_len; 868 } 869 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 870 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 871 continue; 872 pr_key.pr_iid = k; 873 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 874 i += sizeof(pr_key); 875 } 876 mtx_unlock(&lun->lun_lock); 877 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 878 M_WAITOK); 879 free(msg, M_CTL); 880 881 if (lun->flags & CTL_LUN_PRIMARY_SC) { 882 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 883 ctl_isc_announce_mode(lun, -1, 884 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 885 lun->mode_pages.index[i].subpage); 886 } 887 } 888 } 889 890 void 891 ctl_isc_announce_port(struct ctl_port *port) 892 { 893 struct ctl_softc *softc = port->ctl_softc; 894 union ctl_ha_msg *msg; 895 int i; 896 897 if (port->targ_port < softc->port_min || 898 port->targ_port >= softc->port_max || 899 softc->ha_link != CTL_HA_LINK_ONLINE) 900 return; 901 i = sizeof(msg->port) + strlen(port->port_name) + 1; 902 if (port->lun_map) 903 i += port->lun_map_size * sizeof(uint32_t); 904 if (port->port_devid) 905 i += port->port_devid->len; 906 if (port->target_devid) 907 i += port->target_devid->len; 908 if (port->init_devid) 909 i += port->init_devid->len; 910 msg = malloc(i, M_CTL, M_WAITOK); 911 bzero(&msg->port, sizeof(msg->port)); 912 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 913 msg->hdr.nexus.targ_port = port->targ_port; 914 msg->port.port_type = port->port_type; 915 msg->port.physical_port = port->physical_port; 916 msg->port.virtual_port = port->virtual_port; 917 msg->port.status = port->status; 918 i = 0; 919 msg->port.name_len = sprintf(&msg->port.data[i], 920 "%d:%s", softc->ha_id, port->port_name) + 1; 921 i += msg->port.name_len; 922 if (port->lun_map) { 923 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 924 memcpy(&msg->port.data[i], port->lun_map, 925 msg->port.lun_map_len); 926 i += msg->port.lun_map_len; 927 } 928 if (port->port_devid) { 929 msg->port.port_devid_len = port->port_devid->len; 930 memcpy(&msg->port.data[i], port->port_devid->data, 931 msg->port.port_devid_len); 932 i += msg->port.port_devid_len; 933 } 934 if (port->target_devid) { 935 msg->port.target_devid_len = port->target_devid->len; 936 memcpy(&msg->port.data[i], port->target_devid->data, 937 msg->port.target_devid_len); 938 i += msg->port.target_devid_len; 939 } 940 if (port->init_devid) { 941 msg->port.init_devid_len = port->init_devid->len; 942 memcpy(&msg->port.data[i], port->init_devid->data, 943 msg->port.init_devid_len); 944 i += msg->port.init_devid_len; 945 } 946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 947 M_WAITOK); 948 free(msg, M_CTL); 949 } 950 951 void 952 ctl_isc_announce_iid(struct ctl_port *port, int iid) 953 { 954 struct ctl_softc *softc = port->ctl_softc; 955 union ctl_ha_msg *msg; 956 int i, l; 957 958 if (port->targ_port < softc->port_min || 959 port->targ_port >= softc->port_max || 960 softc->ha_link != CTL_HA_LINK_ONLINE) 961 return; 962 mtx_lock(&softc->ctl_lock); 963 i = sizeof(msg->iid); 964 l = 0; 965 if (port->wwpn_iid[iid].name) 966 l = strlen(port->wwpn_iid[iid].name) + 1; 967 i += l; 968 msg = malloc(i, M_CTL, M_NOWAIT); 969 if (msg == NULL) { 970 mtx_unlock(&softc->ctl_lock); 971 return; 972 } 973 bzero(&msg->iid, sizeof(msg->iid)); 974 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 975 msg->hdr.nexus.targ_port = port->targ_port; 976 msg->hdr.nexus.initid = iid; 977 msg->iid.in_use = port->wwpn_iid[iid].in_use; 978 msg->iid.name_len = l; 979 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 980 if (port->wwpn_iid[iid].name) 981 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 982 mtx_unlock(&softc->ctl_lock); 983 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 984 free(msg, M_CTL); 985 } 986 987 void 988 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 989 uint8_t page, uint8_t subpage) 990 { 991 struct ctl_softc *softc = lun->ctl_softc; 992 union ctl_ha_msg msg; 993 u_int i; 994 995 if (softc->ha_link != CTL_HA_LINK_ONLINE) 996 return; 997 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 998 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 999 page && lun->mode_pages.index[i].subpage == subpage) 1000 break; 1001 } 1002 if (i == CTL_NUM_MODE_PAGES) 1003 return; 1004 1005 /* Don't try to replicate pages not present on this device. */ 1006 if (lun->mode_pages.index[i].page_data == NULL) 1007 return; 1008 1009 bzero(&msg.mode, sizeof(msg.mode)); 1010 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 1011 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 1012 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 1013 msg.hdr.nexus.targ_lun = lun->lun; 1014 msg.hdr.nexus.targ_mapped_lun = lun->lun; 1015 msg.mode.page_code = page; 1016 msg.mode.subpage = subpage; 1017 msg.mode.page_len = lun->mode_pages.index[i].page_len; 1018 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 1019 msg.mode.page_len); 1020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 1021 M_WAITOK); 1022 } 1023 1024 static void 1025 ctl_isc_ha_link_up(struct ctl_softc *softc) 1026 { 1027 struct ctl_port *port; 1028 struct ctl_lun *lun; 1029 union ctl_ha_msg msg; 1030 int i; 1031 1032 /* Announce this node parameters to peer for validation. */ 1033 msg.login.msg_type = CTL_MSG_LOGIN; 1034 msg.login.version = CTL_HA_VERSION; 1035 msg.login.ha_mode = softc->ha_mode; 1036 msg.login.ha_id = softc->ha_id; 1037 msg.login.max_luns = ctl_max_luns; 1038 msg.login.max_ports = ctl_max_ports; 1039 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1040 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1041 M_WAITOK); 1042 1043 STAILQ_FOREACH(port, &softc->port_list, links) { 1044 ctl_isc_announce_port(port); 1045 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1046 if (port->wwpn_iid[i].in_use) 1047 ctl_isc_announce_iid(port, i); 1048 } 1049 } 1050 STAILQ_FOREACH(lun, &softc->lun_list, links) 1051 ctl_isc_announce_lun(lun); 1052 } 1053 1054 static void 1055 ctl_isc_ha_link_down(struct ctl_softc *softc) 1056 { 1057 struct ctl_port *port; 1058 struct ctl_lun *lun; 1059 union ctl_io *io; 1060 int i; 1061 1062 mtx_lock(&softc->ctl_lock); 1063 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1064 mtx_lock(&lun->lun_lock); 1065 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1066 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1067 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1068 } 1069 mtx_unlock(&lun->lun_lock); 1070 1071 mtx_unlock(&softc->ctl_lock); 1072 io = ctl_alloc_io(softc->othersc_pool); 1073 mtx_lock(&softc->ctl_lock); 1074 ctl_zero_io(io); 1075 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1076 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1077 ctl_enqueue_isc(io); 1078 } 1079 1080 STAILQ_FOREACH(port, &softc->port_list, links) { 1081 if (port->targ_port >= softc->port_min && 1082 port->targ_port < softc->port_max) 1083 continue; 1084 port->status &= ~CTL_PORT_STATUS_ONLINE; 1085 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1086 port->wwpn_iid[i].in_use = 0; 1087 free(port->wwpn_iid[i].name, M_CTL); 1088 port->wwpn_iid[i].name = NULL; 1089 } 1090 } 1091 mtx_unlock(&softc->ctl_lock); 1092 } 1093 1094 static void 1095 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1096 { 1097 struct ctl_lun *lun; 1098 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1099 1100 mtx_lock(&softc->ctl_lock); 1101 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1102 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1103 mtx_unlock(&softc->ctl_lock); 1104 return; 1105 } 1106 mtx_lock(&lun->lun_lock); 1107 mtx_unlock(&softc->ctl_lock); 1108 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1109 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1110 if (msg->ua.ua_all) { 1111 if (msg->ua.ua_set) 1112 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1113 else 1114 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1115 } else { 1116 if (msg->ua.ua_set) 1117 ctl_est_ua(lun, iid, msg->ua.ua_type); 1118 else 1119 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1120 } 1121 mtx_unlock(&lun->lun_lock); 1122 } 1123 1124 static void 1125 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1126 { 1127 struct ctl_lun *lun; 1128 struct ctl_ha_msg_lun_pr_key pr_key; 1129 int i, k; 1130 ctl_lun_flags oflags; 1131 uint32_t targ_lun; 1132 1133 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1134 mtx_lock(&softc->ctl_lock); 1135 if (targ_lun >= ctl_max_luns || 1136 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1137 mtx_unlock(&softc->ctl_lock); 1138 return; 1139 } 1140 mtx_lock(&lun->lun_lock); 1141 mtx_unlock(&softc->ctl_lock); 1142 if (lun->flags & CTL_LUN_DISABLED) { 1143 mtx_unlock(&lun->lun_lock); 1144 return; 1145 } 1146 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1147 if (msg->lun.lun_devid_len != i || (i > 0 && 1148 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1149 mtx_unlock(&lun->lun_lock); 1150 printf("%s: Received conflicting HA LUN %d\n", 1151 __func__, targ_lun); 1152 return; 1153 } else { 1154 /* Record whether peer is primary. */ 1155 oflags = lun->flags; 1156 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1157 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1158 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1159 else 1160 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1161 if (oflags != lun->flags) 1162 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1163 1164 /* If peer is primary and we are not -- use data */ 1165 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1166 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1167 lun->pr_generation = msg->lun.pr_generation; 1168 lun->pr_res_idx = msg->lun.pr_res_idx; 1169 lun->pr_res_type = msg->lun.pr_res_type; 1170 lun->pr_key_count = msg->lun.pr_key_count; 1171 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1172 ctl_clr_prkey(lun, k); 1173 for (k = 0; k < msg->lun.pr_key_count; k++) { 1174 memcpy(&pr_key, &msg->lun.data[i], 1175 sizeof(pr_key)); 1176 ctl_alloc_prkey(lun, pr_key.pr_iid); 1177 ctl_set_prkey(lun, pr_key.pr_iid, 1178 pr_key.pr_key); 1179 i += sizeof(pr_key); 1180 } 1181 } 1182 1183 mtx_unlock(&lun->lun_lock); 1184 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1185 __func__, targ_lun, 1186 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1187 "primary" : "secondary")); 1188 1189 /* If we are primary but peer doesn't know -- notify */ 1190 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1191 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1192 ctl_isc_announce_lun(lun); 1193 } 1194 } 1195 1196 static void 1197 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1198 { 1199 struct ctl_port *port; 1200 struct ctl_lun *lun; 1201 int i, new; 1202 1203 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1204 if (port == NULL) { 1205 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1206 msg->hdr.nexus.targ_port)); 1207 new = 1; 1208 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1209 port->frontend = &ha_frontend; 1210 port->targ_port = msg->hdr.nexus.targ_port; 1211 port->fe_datamove = ctl_ha_datamove; 1212 port->fe_done = ctl_ha_done; 1213 } else if (port->frontend == &ha_frontend) { 1214 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1215 msg->hdr.nexus.targ_port)); 1216 new = 0; 1217 } else { 1218 printf("%s: Received conflicting HA port %d\n", 1219 __func__, msg->hdr.nexus.targ_port); 1220 return; 1221 } 1222 port->port_type = msg->port.port_type; 1223 port->physical_port = msg->port.physical_port; 1224 port->virtual_port = msg->port.virtual_port; 1225 port->status = msg->port.status; 1226 i = 0; 1227 free(port->port_name, M_CTL); 1228 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1229 M_CTL); 1230 i += msg->port.name_len; 1231 if (msg->port.lun_map_len != 0) { 1232 if (port->lun_map == NULL || 1233 port->lun_map_size * sizeof(uint32_t) < 1234 msg->port.lun_map_len) { 1235 port->lun_map_size = 0; 1236 free(port->lun_map, M_CTL); 1237 port->lun_map = malloc(msg->port.lun_map_len, 1238 M_CTL, M_WAITOK); 1239 } 1240 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1241 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1242 i += msg->port.lun_map_len; 1243 } else { 1244 port->lun_map_size = 0; 1245 free(port->lun_map, M_CTL); 1246 port->lun_map = NULL; 1247 } 1248 if (msg->port.port_devid_len != 0) { 1249 if (port->port_devid == NULL || 1250 port->port_devid->len < msg->port.port_devid_len) { 1251 free(port->port_devid, M_CTL); 1252 port->port_devid = malloc(sizeof(struct ctl_devid) + 1253 msg->port.port_devid_len, M_CTL, M_WAITOK); 1254 } 1255 memcpy(port->port_devid->data, &msg->port.data[i], 1256 msg->port.port_devid_len); 1257 port->port_devid->len = msg->port.port_devid_len; 1258 i += msg->port.port_devid_len; 1259 } else { 1260 free(port->port_devid, M_CTL); 1261 port->port_devid = NULL; 1262 } 1263 if (msg->port.target_devid_len != 0) { 1264 if (port->target_devid == NULL || 1265 port->target_devid->len < msg->port.target_devid_len) { 1266 free(port->target_devid, M_CTL); 1267 port->target_devid = malloc(sizeof(struct ctl_devid) + 1268 msg->port.target_devid_len, M_CTL, M_WAITOK); 1269 } 1270 memcpy(port->target_devid->data, &msg->port.data[i], 1271 msg->port.target_devid_len); 1272 port->target_devid->len = msg->port.target_devid_len; 1273 i += msg->port.target_devid_len; 1274 } else { 1275 free(port->target_devid, M_CTL); 1276 port->target_devid = NULL; 1277 } 1278 if (msg->port.init_devid_len != 0) { 1279 if (port->init_devid == NULL || 1280 port->init_devid->len < msg->port.init_devid_len) { 1281 free(port->init_devid, M_CTL); 1282 port->init_devid = malloc(sizeof(struct ctl_devid) + 1283 msg->port.init_devid_len, M_CTL, M_WAITOK); 1284 } 1285 memcpy(port->init_devid->data, &msg->port.data[i], 1286 msg->port.init_devid_len); 1287 port->init_devid->len = msg->port.init_devid_len; 1288 i += msg->port.init_devid_len; 1289 } else { 1290 free(port->init_devid, M_CTL); 1291 port->init_devid = NULL; 1292 } 1293 if (new) { 1294 if (ctl_port_register(port) != 0) { 1295 printf("%s: ctl_port_register() failed with error\n", 1296 __func__); 1297 } 1298 } 1299 mtx_lock(&softc->ctl_lock); 1300 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1301 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1302 continue; 1303 mtx_lock(&lun->lun_lock); 1304 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1305 mtx_unlock(&lun->lun_lock); 1306 } 1307 mtx_unlock(&softc->ctl_lock); 1308 } 1309 1310 static void 1311 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1312 { 1313 struct ctl_port *port; 1314 int iid; 1315 1316 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1317 if (port == NULL) { 1318 printf("%s: Received IID for unknown port %d\n", 1319 __func__, msg->hdr.nexus.targ_port); 1320 return; 1321 } 1322 iid = msg->hdr.nexus.initid; 1323 if (port->wwpn_iid[iid].in_use != 0 && 1324 msg->iid.in_use == 0) 1325 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1326 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1327 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1328 free(port->wwpn_iid[iid].name, M_CTL); 1329 if (msg->iid.name_len) { 1330 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1331 msg->iid.name_len, M_CTL); 1332 } else 1333 port->wwpn_iid[iid].name = NULL; 1334 } 1335 1336 static void 1337 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1338 { 1339 1340 if (msg->login.version != CTL_HA_VERSION) { 1341 printf("CTL HA peers have different versions %d != %d\n", 1342 msg->login.version, CTL_HA_VERSION); 1343 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1344 return; 1345 } 1346 if (msg->login.ha_mode != softc->ha_mode) { 1347 printf("CTL HA peers have different ha_mode %d != %d\n", 1348 msg->login.ha_mode, softc->ha_mode); 1349 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1350 return; 1351 } 1352 if (msg->login.ha_id == softc->ha_id) { 1353 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1354 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1355 return; 1356 } 1357 if (msg->login.max_luns != ctl_max_luns || 1358 msg->login.max_ports != ctl_max_ports || 1359 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1360 printf("CTL HA peers have different limits\n"); 1361 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1362 return; 1363 } 1364 } 1365 1366 static void 1367 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1368 { 1369 struct ctl_lun *lun; 1370 u_int i; 1371 uint32_t initidx, targ_lun; 1372 1373 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1374 mtx_lock(&softc->ctl_lock); 1375 if (targ_lun >= ctl_max_luns || 1376 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1377 mtx_unlock(&softc->ctl_lock); 1378 return; 1379 } 1380 mtx_lock(&lun->lun_lock); 1381 mtx_unlock(&softc->ctl_lock); 1382 if (lun->flags & CTL_LUN_DISABLED) { 1383 mtx_unlock(&lun->lun_lock); 1384 return; 1385 } 1386 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1387 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1388 msg->mode.page_code && 1389 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1390 break; 1391 } 1392 if (i == CTL_NUM_MODE_PAGES) { 1393 mtx_unlock(&lun->lun_lock); 1394 return; 1395 } 1396 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1397 lun->mode_pages.index[i].page_len); 1398 initidx = ctl_get_initindex(&msg->hdr.nexus); 1399 if (initidx != -1) 1400 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1401 mtx_unlock(&lun->lun_lock); 1402 } 1403 1404 /* 1405 * ISC (Inter Shelf Communication) event handler. Events from the HA 1406 * subsystem come in here. 1407 */ 1408 static void 1409 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1410 { 1411 struct ctl_softc *softc = control_softc; 1412 union ctl_io *io; 1413 struct ctl_prio *presio; 1414 ctl_ha_status isc_status; 1415 1416 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1417 if (event == CTL_HA_EVT_MSG_RECV) { 1418 union ctl_ha_msg *msg, msgbuf; 1419 1420 if (param > sizeof(msgbuf)) 1421 msg = malloc(param, M_CTL, M_WAITOK); 1422 else 1423 msg = &msgbuf; 1424 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1425 M_WAITOK); 1426 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1427 printf("%s: Error receiving message: %d\n", 1428 __func__, isc_status); 1429 if (msg != &msgbuf) 1430 free(msg, M_CTL); 1431 return; 1432 } 1433 1434 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1435 switch (msg->hdr.msg_type) { 1436 case CTL_MSG_SERIALIZE: 1437 io = ctl_alloc_io(softc->othersc_pool); 1438 ctl_zero_io(io); 1439 // populate ctsio from msg 1440 io->io_hdr.io_type = CTL_IO_SCSI; 1441 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1442 io->io_hdr.remote_io = msg->hdr.original_sc; 1443 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1444 CTL_FLAG_IO_ACTIVE; 1445 /* 1446 * If we're in serialization-only mode, we don't 1447 * want to go through full done processing. Thus 1448 * the COPY flag. 1449 * 1450 * XXX KDM add another flag that is more specific. 1451 */ 1452 if (softc->ha_mode != CTL_HA_MODE_XFER) 1453 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1454 io->io_hdr.nexus = msg->hdr.nexus; 1455 #if 0 1456 printf("port %u, iid %u, lun %u\n", 1457 io->io_hdr.nexus.targ_port, 1458 io->io_hdr.nexus.initid, 1459 io->io_hdr.nexus.targ_lun); 1460 #endif 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 #if 0 1542 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1543 __func__, sgl[i].addr, sgl[i].len, j, i); 1544 #endif 1545 } 1546 1547 /* 1548 * If this is the last piece of the I/O, we've got 1549 * the full S/G list. Queue processing in the thread. 1550 * Otherwise wait for the next piece. 1551 */ 1552 if (msg->dt.sg_last != 0) 1553 ctl_enqueue_isc(io); 1554 break; 1555 } 1556 /* Performed on the Serializing (primary) SC, XFER mode only */ 1557 case CTL_MSG_DATAMOVE_DONE: { 1558 if (msg->hdr.serializing_sc == NULL) { 1559 printf("%s: serializing_sc == NULL!\n", 1560 __func__); 1561 /* XXX KDM now what? */ 1562 break; 1563 } 1564 /* 1565 * We grab the sense information here in case 1566 * there was a failure, so we can return status 1567 * back to the initiator. 1568 */ 1569 io = msg->hdr.serializing_sc; 1570 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1571 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1572 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1573 io->io_hdr.port_status = msg->scsi.port_status; 1574 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1575 if (msg->hdr.status != CTL_STATUS_NONE) { 1576 io->io_hdr.status = msg->hdr.status; 1577 io->scsiio.scsi_status = msg->scsi.scsi_status; 1578 io->scsiio.sense_len = msg->scsi.sense_len; 1579 memcpy(&io->scsiio.sense_data, 1580 &msg->scsi.sense_data, 1581 msg->scsi.sense_len); 1582 if (msg->hdr.status == CTL_SUCCESS) 1583 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1584 } 1585 ctl_enqueue_isc(io); 1586 break; 1587 } 1588 1589 /* Preformed on Originating SC, SER_ONLY mode */ 1590 case CTL_MSG_R2R: 1591 io = msg->hdr.original_sc; 1592 if (io == NULL) { 1593 printf("%s: original_sc == NULL!\n", 1594 __func__); 1595 break; 1596 } 1597 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1598 io->io_hdr.msg_type = CTL_MSG_R2R; 1599 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1600 ctl_enqueue_isc(io); 1601 break; 1602 1603 /* 1604 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1605 * mode. 1606 * Performed on the Originating (i.e. secondary) SC in XFER 1607 * mode 1608 */ 1609 case CTL_MSG_FINISH_IO: 1610 if (softc->ha_mode == CTL_HA_MODE_XFER) 1611 ctl_isc_handler_finish_xfer(softc, msg); 1612 else 1613 ctl_isc_handler_finish_ser_only(softc, msg); 1614 break; 1615 1616 /* Preformed on Originating SC */ 1617 case CTL_MSG_BAD_JUJU: 1618 io = msg->hdr.original_sc; 1619 if (io == NULL) { 1620 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1621 __func__); 1622 break; 1623 } 1624 ctl_copy_sense_data(msg, io); 1625 /* 1626 * IO should have already been cleaned up on other 1627 * SC so clear this flag so we won't send a message 1628 * back to finish the IO there. 1629 */ 1630 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1631 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1632 1633 /* io = msg->hdr.serializing_sc; */ 1634 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1635 ctl_enqueue_isc(io); 1636 break; 1637 1638 /* Handle resets sent from the other side */ 1639 case CTL_MSG_MANAGE_TASKS: { 1640 struct ctl_taskio *taskio; 1641 taskio = (struct ctl_taskio *)ctl_alloc_io( 1642 softc->othersc_pool); 1643 ctl_zero_io((union ctl_io *)taskio); 1644 taskio->io_hdr.io_type = CTL_IO_TASK; 1645 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1646 taskio->io_hdr.nexus = msg->hdr.nexus; 1647 taskio->task_action = msg->task.task_action; 1648 taskio->tag_num = msg->task.tag_num; 1649 taskio->tag_type = msg->task.tag_type; 1650 #ifdef CTL_TIME_IO 1651 taskio->io_hdr.start_time = time_uptime; 1652 getbinuptime(&taskio->io_hdr.start_bt); 1653 #endif /* CTL_TIME_IO */ 1654 ctl_run_task((union ctl_io *)taskio); 1655 break; 1656 } 1657 /* Persistent Reserve action which needs attention */ 1658 case CTL_MSG_PERS_ACTION: 1659 presio = (struct ctl_prio *)ctl_alloc_io( 1660 softc->othersc_pool); 1661 ctl_zero_io((union ctl_io *)presio); 1662 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1663 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1664 presio->io_hdr.nexus = msg->hdr.nexus; 1665 presio->pr_msg = msg->pr; 1666 ctl_enqueue_isc((union ctl_io *)presio); 1667 break; 1668 case CTL_MSG_UA: 1669 ctl_isc_ua(softc, msg, param); 1670 break; 1671 case CTL_MSG_PORT_SYNC: 1672 ctl_isc_port_sync(softc, msg, param); 1673 break; 1674 case CTL_MSG_LUN_SYNC: 1675 ctl_isc_lun_sync(softc, msg, param); 1676 break; 1677 case CTL_MSG_IID_SYNC: 1678 ctl_isc_iid_sync(softc, msg, param); 1679 break; 1680 case CTL_MSG_LOGIN: 1681 ctl_isc_login(softc, msg, param); 1682 break; 1683 case CTL_MSG_MODE_SYNC: 1684 ctl_isc_mode_sync(softc, msg, param); 1685 break; 1686 default: 1687 printf("Received HA message of unknown type %d\n", 1688 msg->hdr.msg_type); 1689 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1690 break; 1691 } 1692 if (msg != &msgbuf) 1693 free(msg, M_CTL); 1694 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1695 printf("CTL: HA link status changed from %d to %d\n", 1696 softc->ha_link, param); 1697 if (param == softc->ha_link) 1698 return; 1699 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1700 softc->ha_link = param; 1701 ctl_isc_ha_link_down(softc); 1702 } else { 1703 softc->ha_link = param; 1704 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1705 ctl_isc_ha_link_up(softc); 1706 } 1707 return; 1708 } else { 1709 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1710 return; 1711 } 1712 } 1713 1714 static void 1715 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1716 { 1717 1718 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1719 src->scsi.sense_len); 1720 dest->scsiio.scsi_status = src->scsi.scsi_status; 1721 dest->scsiio.sense_len = src->scsi.sense_len; 1722 dest->io_hdr.status = src->hdr.status; 1723 } 1724 1725 static void 1726 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1727 { 1728 1729 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1730 src->scsiio.sense_len); 1731 dest->scsi.scsi_status = src->scsiio.scsi_status; 1732 dest->scsi.sense_len = src->scsiio.sense_len; 1733 dest->hdr.status = src->io_hdr.status; 1734 } 1735 1736 void 1737 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1738 { 1739 struct ctl_softc *softc = lun->ctl_softc; 1740 ctl_ua_type *pu; 1741 1742 if (initidx < softc->init_min || initidx >= softc->init_max) 1743 return; 1744 mtx_assert(&lun->lun_lock, MA_OWNED); 1745 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1746 if (pu == NULL) 1747 return; 1748 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1749 } 1750 1751 void 1752 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1753 { 1754 int i; 1755 1756 mtx_assert(&lun->lun_lock, MA_OWNED); 1757 if (lun->pending_ua[port] == NULL) 1758 return; 1759 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1760 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1761 continue; 1762 lun->pending_ua[port][i] |= ua; 1763 } 1764 } 1765 1766 void 1767 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1768 { 1769 struct ctl_softc *softc = lun->ctl_softc; 1770 int i; 1771 1772 mtx_assert(&lun->lun_lock, MA_OWNED); 1773 for (i = softc->port_min; i < softc->port_max; i++) 1774 ctl_est_ua_port(lun, i, except, ua); 1775 } 1776 1777 void 1778 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1779 { 1780 struct ctl_softc *softc = lun->ctl_softc; 1781 ctl_ua_type *pu; 1782 1783 if (initidx < softc->init_min || initidx >= softc->init_max) 1784 return; 1785 mtx_assert(&lun->lun_lock, MA_OWNED); 1786 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1787 if (pu == NULL) 1788 return; 1789 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1790 } 1791 1792 void 1793 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1794 { 1795 struct ctl_softc *softc = lun->ctl_softc; 1796 int i, j; 1797 1798 mtx_assert(&lun->lun_lock, MA_OWNED); 1799 for (i = softc->port_min; i < softc->port_max; i++) { 1800 if (lun->pending_ua[i] == NULL) 1801 continue; 1802 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1803 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1804 continue; 1805 lun->pending_ua[i][j] &= ~ua; 1806 } 1807 } 1808 } 1809 1810 void 1811 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1812 ctl_ua_type ua_type) 1813 { 1814 struct ctl_lun *lun; 1815 1816 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1817 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1818 mtx_lock(&lun->lun_lock); 1819 ctl_clr_ua(lun, initidx, ua_type); 1820 mtx_unlock(&lun->lun_lock); 1821 } 1822 } 1823 1824 static int 1825 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1826 { 1827 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1828 struct ctl_lun *lun; 1829 struct ctl_lun_req ireq; 1830 int error, value; 1831 1832 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1833 error = sysctl_handle_int(oidp, &value, 0, req); 1834 if ((error != 0) || (req->newptr == NULL)) 1835 return (error); 1836 1837 mtx_lock(&softc->ctl_lock); 1838 if (value == 0) 1839 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1840 else 1841 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1842 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1843 mtx_unlock(&softc->ctl_lock); 1844 bzero(&ireq, sizeof(ireq)); 1845 ireq.reqtype = CTL_LUNREQ_MODIFY; 1846 ireq.reqdata.modify.lun_id = lun->lun; 1847 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1848 curthread); 1849 if (ireq.status != CTL_LUN_OK) { 1850 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1851 __func__, ireq.status, ireq.error_str); 1852 } 1853 mtx_lock(&softc->ctl_lock); 1854 } 1855 mtx_unlock(&softc->ctl_lock); 1856 return (0); 1857 } 1858 1859 static int 1860 ctl_init(void) 1861 { 1862 struct make_dev_args args; 1863 struct ctl_softc *softc; 1864 int i, error; 1865 1866 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1867 M_WAITOK | M_ZERO); 1868 1869 make_dev_args_init(&args); 1870 args.mda_devsw = &ctl_cdevsw; 1871 args.mda_uid = UID_ROOT; 1872 args.mda_gid = GID_OPERATOR; 1873 args.mda_mode = 0600; 1874 args.mda_si_drv1 = softc; 1875 args.mda_si_drv2 = NULL; 1876 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1877 if (error != 0) { 1878 free(softc, M_DEVBUF); 1879 control_softc = NULL; 1880 return (error); 1881 } 1882 1883 sysctl_ctx_init(&softc->sysctl_ctx); 1884 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1885 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1886 CTLFLAG_RD, 0, "CAM Target Layer"); 1887 1888 if (softc->sysctl_tree == NULL) { 1889 printf("%s: unable to allocate sysctl tree\n", __func__); 1890 destroy_dev(softc->dev); 1891 free(softc, M_DEVBUF); 1892 control_softc = NULL; 1893 return (ENOMEM); 1894 } 1895 1896 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1897 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1898 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1899 softc->flags = 0; 1900 1901 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1902 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1903 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1904 1905 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) { 1906 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n", 1907 ctl_max_luns, CTL_DEFAULT_MAX_LUNS); 1908 ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 1909 } 1910 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns, 1911 M_DEVBUF, M_WAITOK | M_ZERO); 1912 softc->ctl_lun_mask = malloc(sizeof(uint32_t) * 1913 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1914 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) { 1915 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n", 1916 ctl_max_ports, CTL_DEFAULT_MAX_PORTS); 1917 ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 1918 } 1919 softc->ctl_port_mask = malloc(sizeof(uint32_t) * 1920 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1921 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports, 1922 M_DEVBUF, M_WAITOK | M_ZERO); 1923 1924 1925 /* 1926 * In Copan's HA scheme, the "master" and "slave" roles are 1927 * figured out through the slot the controller is in. Although it 1928 * is an active/active system, someone has to be in charge. 1929 */ 1930 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1931 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1932 "HA head ID (0 - no HA)"); 1933 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1934 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1935 softc->is_single = 1; 1936 softc->port_cnt = ctl_max_ports; 1937 softc->port_min = 0; 1938 } else { 1939 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 1940 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1941 } 1942 softc->port_max = softc->port_min + softc->port_cnt; 1943 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1944 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1945 1946 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1947 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1948 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1949 1950 STAILQ_INIT(&softc->lun_list); 1951 STAILQ_INIT(&softc->pending_lun_queue); 1952 STAILQ_INIT(&softc->fe_list); 1953 STAILQ_INIT(&softc->port_list); 1954 STAILQ_INIT(&softc->be_list); 1955 ctl_tpc_init(softc); 1956 1957 if (worker_threads <= 0) 1958 worker_threads = max(1, mp_ncpus / 4); 1959 if (worker_threads > CTL_MAX_THREADS) 1960 worker_threads = CTL_MAX_THREADS; 1961 1962 for (i = 0; i < worker_threads; i++) { 1963 struct ctl_thread *thr = &softc->threads[i]; 1964 1965 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1966 thr->ctl_softc = softc; 1967 STAILQ_INIT(&thr->incoming_queue); 1968 STAILQ_INIT(&thr->rtr_queue); 1969 STAILQ_INIT(&thr->done_queue); 1970 STAILQ_INIT(&thr->isc_queue); 1971 1972 error = kproc_kthread_add(ctl_work_thread, thr, 1973 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1974 if (error != 0) { 1975 printf("error creating CTL work thread!\n"); 1976 return (error); 1977 } 1978 } 1979 error = kproc_kthread_add(ctl_lun_thread, softc, 1980 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun"); 1981 if (error != 0) { 1982 printf("error creating CTL lun thread!\n"); 1983 return (error); 1984 } 1985 error = kproc_kthread_add(ctl_thresh_thread, softc, 1986 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 1987 if (error != 0) { 1988 printf("error creating CTL threshold thread!\n"); 1989 return (error); 1990 } 1991 1992 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1993 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1994 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1995 1996 if (softc->is_single == 0) { 1997 if (ctl_frontend_register(&ha_frontend) != 0) 1998 softc->is_single = 1; 1999 } 2000 return (0); 2001 } 2002 2003 static int 2004 ctl_shutdown(void) 2005 { 2006 struct ctl_softc *softc = control_softc; 2007 int i; 2008 2009 if (softc->is_single == 0) 2010 ctl_frontend_deregister(&ha_frontend); 2011 2012 destroy_dev(softc->dev); 2013 2014 /* Shutdown CTL threads. */ 2015 softc->shutdown = 1; 2016 for (i = 0; i < worker_threads; i++) { 2017 struct ctl_thread *thr = &softc->threads[i]; 2018 while (thr->thread != NULL) { 2019 wakeup(thr); 2020 if (thr->thread != NULL) 2021 pause("CTL thr shutdown", 1); 2022 } 2023 mtx_destroy(&thr->queue_lock); 2024 } 2025 while (softc->lun_thread != NULL) { 2026 wakeup(&softc->pending_lun_queue); 2027 if (softc->lun_thread != NULL) 2028 pause("CTL thr shutdown", 1); 2029 } 2030 while (softc->thresh_thread != NULL) { 2031 wakeup(softc->thresh_thread); 2032 if (softc->thresh_thread != NULL) 2033 pause("CTL thr shutdown", 1); 2034 } 2035 2036 ctl_tpc_shutdown(softc); 2037 uma_zdestroy(softc->io_zone); 2038 mtx_destroy(&softc->ctl_lock); 2039 2040 free(softc->ctl_luns, M_DEVBUF); 2041 free(softc->ctl_lun_mask, M_DEVBUF); 2042 free(softc->ctl_port_mask, M_DEVBUF); 2043 free(softc->ctl_ports, M_DEVBUF); 2044 2045 sysctl_ctx_free(&softc->sysctl_ctx); 2046 2047 free(softc, M_DEVBUF); 2048 control_softc = NULL; 2049 return (0); 2050 } 2051 2052 static int 2053 ctl_module_event_handler(module_t mod, int what, void *arg) 2054 { 2055 2056 switch (what) { 2057 case MOD_LOAD: 2058 return (ctl_init()); 2059 case MOD_UNLOAD: 2060 return (ctl_shutdown()); 2061 default: 2062 return (EOPNOTSUPP); 2063 } 2064 } 2065 2066 /* 2067 * XXX KDM should we do some access checks here? Bump a reference count to 2068 * prevent a CTL module from being unloaded while someone has it open? 2069 */ 2070 static int 2071 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2072 { 2073 return (0); 2074 } 2075 2076 static int 2077 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2078 { 2079 return (0); 2080 } 2081 2082 /* 2083 * Remove an initiator by port number and initiator ID. 2084 * Returns 0 for success, -1 for failure. 2085 */ 2086 int 2087 ctl_remove_initiator(struct ctl_port *port, int iid) 2088 { 2089 struct ctl_softc *softc = port->ctl_softc; 2090 int last; 2091 2092 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2093 2094 if (iid > CTL_MAX_INIT_PER_PORT) { 2095 printf("%s: initiator ID %u > maximun %u!\n", 2096 __func__, iid, CTL_MAX_INIT_PER_PORT); 2097 return (-1); 2098 } 2099 2100 mtx_lock(&softc->ctl_lock); 2101 last = (--port->wwpn_iid[iid].in_use == 0); 2102 port->wwpn_iid[iid].last_use = time_uptime; 2103 mtx_unlock(&softc->ctl_lock); 2104 if (last) 2105 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2106 ctl_isc_announce_iid(port, iid); 2107 2108 return (0); 2109 } 2110 2111 /* 2112 * Add an initiator to the initiator map. 2113 * Returns iid for success, < 0 for failure. 2114 */ 2115 int 2116 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2117 { 2118 struct ctl_softc *softc = port->ctl_softc; 2119 time_t best_time; 2120 int i, best; 2121 2122 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2123 2124 if (iid >= CTL_MAX_INIT_PER_PORT) { 2125 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2126 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2127 free(name, M_CTL); 2128 return (-1); 2129 } 2130 2131 mtx_lock(&softc->ctl_lock); 2132 2133 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2134 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2135 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2136 iid = i; 2137 break; 2138 } 2139 if (name != NULL && port->wwpn_iid[i].name != NULL && 2140 strcmp(name, port->wwpn_iid[i].name) == 0) { 2141 iid = i; 2142 break; 2143 } 2144 } 2145 } 2146 2147 if (iid < 0) { 2148 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2149 if (port->wwpn_iid[i].in_use == 0 && 2150 port->wwpn_iid[i].wwpn == 0 && 2151 port->wwpn_iid[i].name == NULL) { 2152 iid = i; 2153 break; 2154 } 2155 } 2156 } 2157 2158 if (iid < 0) { 2159 best = -1; 2160 best_time = INT32_MAX; 2161 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2162 if (port->wwpn_iid[i].in_use == 0) { 2163 if (port->wwpn_iid[i].last_use < best_time) { 2164 best = i; 2165 best_time = port->wwpn_iid[i].last_use; 2166 } 2167 } 2168 } 2169 iid = best; 2170 } 2171 2172 if (iid < 0) { 2173 mtx_unlock(&softc->ctl_lock); 2174 free(name, M_CTL); 2175 return (-2); 2176 } 2177 2178 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2179 /* 2180 * This is not an error yet. 2181 */ 2182 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2183 #if 0 2184 printf("%s: port %d iid %u WWPN %#jx arrived" 2185 " again\n", __func__, port->targ_port, 2186 iid, (uintmax_t)wwpn); 2187 #endif 2188 goto take; 2189 } 2190 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2191 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2192 #if 0 2193 printf("%s: port %d iid %u name '%s' arrived" 2194 " again\n", __func__, port->targ_port, 2195 iid, name); 2196 #endif 2197 goto take; 2198 } 2199 2200 /* 2201 * This is an error, but what do we do about it? The 2202 * driver is telling us we have a new WWPN for this 2203 * initiator ID, so we pretty much need to use it. 2204 */ 2205 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2206 " but WWPN %#jx '%s' is still at that address\n", 2207 __func__, port->targ_port, iid, wwpn, name, 2208 (uintmax_t)port->wwpn_iid[iid].wwpn, 2209 port->wwpn_iid[iid].name); 2210 } 2211 take: 2212 free(port->wwpn_iid[iid].name, M_CTL); 2213 port->wwpn_iid[iid].name = name; 2214 port->wwpn_iid[iid].wwpn = wwpn; 2215 port->wwpn_iid[iid].in_use++; 2216 mtx_unlock(&softc->ctl_lock); 2217 ctl_isc_announce_iid(port, iid); 2218 2219 return (iid); 2220 } 2221 2222 static int 2223 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2224 { 2225 int len; 2226 2227 switch (port->port_type) { 2228 case CTL_PORT_FC: 2229 { 2230 struct scsi_transportid_fcp *id = 2231 (struct scsi_transportid_fcp *)buf; 2232 if (port->wwpn_iid[iid].wwpn == 0) 2233 return (0); 2234 memset(id, 0, sizeof(*id)); 2235 id->format_protocol = SCSI_PROTO_FC; 2236 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2237 return (sizeof(*id)); 2238 } 2239 case CTL_PORT_ISCSI: 2240 { 2241 struct scsi_transportid_iscsi_port *id = 2242 (struct scsi_transportid_iscsi_port *)buf; 2243 if (port->wwpn_iid[iid].name == NULL) 2244 return (0); 2245 memset(id, 0, 256); 2246 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2247 SCSI_PROTO_ISCSI; 2248 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2249 len = roundup2(min(len, 252), 4); 2250 scsi_ulto2b(len, id->additional_length); 2251 return (sizeof(*id) + len); 2252 } 2253 case CTL_PORT_SAS: 2254 { 2255 struct scsi_transportid_sas *id = 2256 (struct scsi_transportid_sas *)buf; 2257 if (port->wwpn_iid[iid].wwpn == 0) 2258 return (0); 2259 memset(id, 0, sizeof(*id)); 2260 id->format_protocol = SCSI_PROTO_SAS; 2261 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2262 return (sizeof(*id)); 2263 } 2264 default: 2265 { 2266 struct scsi_transportid_spi *id = 2267 (struct scsi_transportid_spi *)buf; 2268 memset(id, 0, sizeof(*id)); 2269 id->format_protocol = SCSI_PROTO_SPI; 2270 scsi_ulto2b(iid, id->scsi_addr); 2271 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2272 return (sizeof(*id)); 2273 } 2274 } 2275 } 2276 2277 /* 2278 * Serialize a command that went down the "wrong" side, and so was sent to 2279 * this controller for execution. The logic is a little different than the 2280 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2281 * sent back to the other side, but in the success case, we execute the 2282 * command on this side (XFER mode) or tell the other side to execute it 2283 * (SER_ONLY mode). 2284 */ 2285 static void 2286 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2287 { 2288 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2289 struct ctl_port *port = CTL_PORT(ctsio); 2290 union ctl_ha_msg msg_info; 2291 struct ctl_lun *lun; 2292 const struct ctl_cmd_entry *entry; 2293 uint32_t targ_lun; 2294 2295 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2296 2297 /* Make sure that we know about this port. */ 2298 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2299 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2300 /*retry_count*/ 1); 2301 goto badjuju; 2302 } 2303 2304 /* Make sure that we know about this LUN. */ 2305 mtx_lock(&softc->ctl_lock); 2306 if (targ_lun >= ctl_max_luns || 2307 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2308 mtx_unlock(&softc->ctl_lock); 2309 2310 /* 2311 * The other node would not send this request to us unless 2312 * received announce that we are primary node for this LUN. 2313 * If this LUN does not exist now, it is probably result of 2314 * a race, so respond to initiator in the most opaque way. 2315 */ 2316 ctl_set_busy(ctsio); 2317 goto badjuju; 2318 } 2319 mtx_lock(&lun->lun_lock); 2320 mtx_unlock(&softc->ctl_lock); 2321 2322 /* 2323 * If the LUN is invalid, pretend that it doesn't exist. 2324 * It will go away as soon as all pending I/Os completed. 2325 */ 2326 if (lun->flags & CTL_LUN_DISABLED) { 2327 mtx_unlock(&lun->lun_lock); 2328 ctl_set_busy(ctsio); 2329 goto badjuju; 2330 } 2331 2332 entry = ctl_get_cmd_entry(ctsio, NULL); 2333 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2334 mtx_unlock(&lun->lun_lock); 2335 goto badjuju; 2336 } 2337 2338 CTL_LUN(ctsio) = lun; 2339 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2340 2341 /* 2342 * Every I/O goes into the OOA queue for a 2343 * particular LUN, and stays there until completion. 2344 */ 2345 #ifdef CTL_TIME_IO 2346 if (TAILQ_EMPTY(&lun->ooa_queue)) 2347 lun->idle_time += getsbinuptime() - lun->last_busy; 2348 #endif 2349 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2350 2351 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2352 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2353 ooa_links))) { 2354 case CTL_ACTION_BLOCK: 2355 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2356 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2357 blocked_links); 2358 mtx_unlock(&lun->lun_lock); 2359 break; 2360 case CTL_ACTION_PASS: 2361 case CTL_ACTION_SKIP: 2362 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2363 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2364 ctl_enqueue_rtr((union ctl_io *)ctsio); 2365 mtx_unlock(&lun->lun_lock); 2366 } else { 2367 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2368 mtx_unlock(&lun->lun_lock); 2369 2370 /* send msg back to other side */ 2371 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2372 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2373 msg_info.hdr.msg_type = CTL_MSG_R2R; 2374 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2375 sizeof(msg_info.hdr), M_WAITOK); 2376 } 2377 break; 2378 case CTL_ACTION_OVERLAP: 2379 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2380 mtx_unlock(&lun->lun_lock); 2381 ctl_set_overlapped_cmd(ctsio); 2382 goto badjuju; 2383 case CTL_ACTION_OVERLAP_TAG: 2384 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2385 mtx_unlock(&lun->lun_lock); 2386 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2387 goto badjuju; 2388 case CTL_ACTION_ERROR: 2389 default: 2390 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2391 mtx_unlock(&lun->lun_lock); 2392 2393 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2394 /*retry_count*/ 0); 2395 badjuju: 2396 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2397 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2398 msg_info.hdr.serializing_sc = NULL; 2399 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2401 sizeof(msg_info.scsi), M_WAITOK); 2402 ctl_free_io((union ctl_io *)ctsio); 2403 break; 2404 } 2405 } 2406 2407 /* 2408 * Returns 0 for success, errno for failure. 2409 */ 2410 static void 2411 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2412 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2413 { 2414 union ctl_io *io; 2415 2416 mtx_lock(&lun->lun_lock); 2417 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2418 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2419 ooa_links)) { 2420 struct ctl_ooa_entry *entry; 2421 2422 /* 2423 * If we've got more than we can fit, just count the 2424 * remaining entries. 2425 */ 2426 if (*cur_fill_num >= ooa_hdr->alloc_num) 2427 continue; 2428 2429 entry = &kern_entries[*cur_fill_num]; 2430 2431 entry->tag_num = io->scsiio.tag_num; 2432 entry->lun_num = lun->lun; 2433 #ifdef CTL_TIME_IO 2434 entry->start_bt = io->io_hdr.start_bt; 2435 #endif 2436 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2437 entry->cdb_len = io->scsiio.cdb_len; 2438 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2439 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2440 2441 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2442 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2443 2444 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2445 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2446 2447 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2448 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2449 2450 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2451 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2452 } 2453 mtx_unlock(&lun->lun_lock); 2454 } 2455 2456 /* 2457 * Escape characters that are illegal or not recommended in XML. 2458 */ 2459 int 2460 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2461 { 2462 char *end = str + size; 2463 int retval; 2464 2465 retval = 0; 2466 2467 for (; *str && str < end; str++) { 2468 switch (*str) { 2469 case '&': 2470 retval = sbuf_printf(sb, "&"); 2471 break; 2472 case '>': 2473 retval = sbuf_printf(sb, ">"); 2474 break; 2475 case '<': 2476 retval = sbuf_printf(sb, "<"); 2477 break; 2478 default: 2479 retval = sbuf_putc(sb, *str); 2480 break; 2481 } 2482 2483 if (retval != 0) 2484 break; 2485 2486 } 2487 2488 return (retval); 2489 } 2490 2491 static void 2492 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2493 { 2494 struct scsi_vpd_id_descriptor *desc; 2495 int i; 2496 2497 if (id == NULL || id->len < 4) 2498 return; 2499 desc = (struct scsi_vpd_id_descriptor *)id->data; 2500 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2501 case SVPD_ID_TYPE_T10: 2502 sbuf_printf(sb, "t10."); 2503 break; 2504 case SVPD_ID_TYPE_EUI64: 2505 sbuf_printf(sb, "eui."); 2506 break; 2507 case SVPD_ID_TYPE_NAA: 2508 sbuf_printf(sb, "naa."); 2509 break; 2510 case SVPD_ID_TYPE_SCSI_NAME: 2511 break; 2512 } 2513 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2514 case SVPD_ID_CODESET_BINARY: 2515 for (i = 0; i < desc->length; i++) 2516 sbuf_printf(sb, "%02x", desc->identifier[i]); 2517 break; 2518 case SVPD_ID_CODESET_ASCII: 2519 sbuf_printf(sb, "%.*s", (int)desc->length, 2520 (char *)desc->identifier); 2521 break; 2522 case SVPD_ID_CODESET_UTF8: 2523 sbuf_printf(sb, "%s", (char *)desc->identifier); 2524 break; 2525 } 2526 } 2527 2528 static int 2529 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2530 struct thread *td) 2531 { 2532 struct ctl_softc *softc = dev->si_drv1; 2533 struct ctl_port *port; 2534 struct ctl_lun *lun; 2535 int retval; 2536 2537 retval = 0; 2538 2539 switch (cmd) { 2540 case CTL_IO: 2541 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2542 break; 2543 case CTL_ENABLE_PORT: 2544 case CTL_DISABLE_PORT: 2545 case CTL_SET_PORT_WWNS: { 2546 struct ctl_port *port; 2547 struct ctl_port_entry *entry; 2548 2549 entry = (struct ctl_port_entry *)addr; 2550 2551 mtx_lock(&softc->ctl_lock); 2552 STAILQ_FOREACH(port, &softc->port_list, links) { 2553 int action, done; 2554 2555 if (port->targ_port < softc->port_min || 2556 port->targ_port >= softc->port_max) 2557 continue; 2558 2559 action = 0; 2560 done = 0; 2561 if ((entry->port_type == CTL_PORT_NONE) 2562 && (entry->targ_port == port->targ_port)) { 2563 /* 2564 * If the user only wants to enable or 2565 * disable or set WWNs on a specific port, 2566 * do the operation and we're done. 2567 */ 2568 action = 1; 2569 done = 1; 2570 } else if (entry->port_type & port->port_type) { 2571 /* 2572 * Compare the user's type mask with the 2573 * particular frontend type to see if we 2574 * have a match. 2575 */ 2576 action = 1; 2577 done = 0; 2578 2579 /* 2580 * Make sure the user isn't trying to set 2581 * WWNs on multiple ports at the same time. 2582 */ 2583 if (cmd == CTL_SET_PORT_WWNS) { 2584 printf("%s: Can't set WWNs on " 2585 "multiple ports\n", __func__); 2586 retval = EINVAL; 2587 break; 2588 } 2589 } 2590 if (action == 0) 2591 continue; 2592 2593 /* 2594 * XXX KDM we have to drop the lock here, because 2595 * the online/offline operations can potentially 2596 * block. We need to reference count the frontends 2597 * so they can't go away, 2598 */ 2599 if (cmd == CTL_ENABLE_PORT) { 2600 mtx_unlock(&softc->ctl_lock); 2601 ctl_port_online(port); 2602 mtx_lock(&softc->ctl_lock); 2603 } else if (cmd == CTL_DISABLE_PORT) { 2604 mtx_unlock(&softc->ctl_lock); 2605 ctl_port_offline(port); 2606 mtx_lock(&softc->ctl_lock); 2607 } else if (cmd == CTL_SET_PORT_WWNS) { 2608 ctl_port_set_wwns(port, 2609 (entry->flags & CTL_PORT_WWNN_VALID) ? 2610 1 : 0, entry->wwnn, 2611 (entry->flags & CTL_PORT_WWPN_VALID) ? 2612 1 : 0, entry->wwpn); 2613 } 2614 if (done != 0) 2615 break; 2616 } 2617 mtx_unlock(&softc->ctl_lock); 2618 break; 2619 } 2620 case CTL_GET_OOA: { 2621 struct ctl_ooa *ooa_hdr; 2622 struct ctl_ooa_entry *entries; 2623 uint32_t cur_fill_num; 2624 2625 ooa_hdr = (struct ctl_ooa *)addr; 2626 2627 if ((ooa_hdr->alloc_len == 0) 2628 || (ooa_hdr->alloc_num == 0)) { 2629 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2630 "must be non-zero\n", __func__, 2631 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2632 retval = EINVAL; 2633 break; 2634 } 2635 2636 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2637 sizeof(struct ctl_ooa_entry))) { 2638 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2639 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2640 __func__, ooa_hdr->alloc_len, 2641 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2642 retval = EINVAL; 2643 break; 2644 } 2645 2646 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2647 if (entries == NULL) { 2648 printf("%s: could not allocate %d bytes for OOA " 2649 "dump\n", __func__, ooa_hdr->alloc_len); 2650 retval = ENOMEM; 2651 break; 2652 } 2653 2654 mtx_lock(&softc->ctl_lock); 2655 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2656 (ooa_hdr->lun_num >= ctl_max_luns || 2657 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2658 mtx_unlock(&softc->ctl_lock); 2659 free(entries, M_CTL); 2660 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2661 __func__, (uintmax_t)ooa_hdr->lun_num); 2662 retval = EINVAL; 2663 break; 2664 } 2665 2666 cur_fill_num = 0; 2667 2668 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2669 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2670 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2671 ooa_hdr, entries); 2672 } 2673 } else { 2674 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2675 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2676 entries); 2677 } 2678 mtx_unlock(&softc->ctl_lock); 2679 2680 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2681 ooa_hdr->fill_len = ooa_hdr->fill_num * 2682 sizeof(struct ctl_ooa_entry); 2683 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2684 if (retval != 0) { 2685 printf("%s: error copying out %d bytes for OOA dump\n", 2686 __func__, ooa_hdr->fill_len); 2687 } 2688 2689 getbinuptime(&ooa_hdr->cur_bt); 2690 2691 if (cur_fill_num > ooa_hdr->alloc_num) { 2692 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2693 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2694 } else { 2695 ooa_hdr->dropped_num = 0; 2696 ooa_hdr->status = CTL_OOA_OK; 2697 } 2698 2699 free(entries, M_CTL); 2700 break; 2701 } 2702 case CTL_DELAY_IO: { 2703 struct ctl_io_delay_info *delay_info; 2704 2705 delay_info = (struct ctl_io_delay_info *)addr; 2706 2707 #ifdef CTL_IO_DELAY 2708 mtx_lock(&softc->ctl_lock); 2709 if (delay_info->lun_id >= ctl_max_luns || 2710 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2711 mtx_unlock(&softc->ctl_lock); 2712 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2713 break; 2714 } 2715 mtx_lock(&lun->lun_lock); 2716 mtx_unlock(&softc->ctl_lock); 2717 delay_info->status = CTL_DELAY_STATUS_OK; 2718 switch (delay_info->delay_type) { 2719 case CTL_DELAY_TYPE_CONT: 2720 case CTL_DELAY_TYPE_ONESHOT: 2721 break; 2722 default: 2723 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2724 break; 2725 } 2726 switch (delay_info->delay_loc) { 2727 case CTL_DELAY_LOC_DATAMOVE: 2728 lun->delay_info.datamove_type = delay_info->delay_type; 2729 lun->delay_info.datamove_delay = delay_info->delay_secs; 2730 break; 2731 case CTL_DELAY_LOC_DONE: 2732 lun->delay_info.done_type = delay_info->delay_type; 2733 lun->delay_info.done_delay = delay_info->delay_secs; 2734 break; 2735 default: 2736 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2737 break; 2738 } 2739 mtx_unlock(&lun->lun_lock); 2740 #else 2741 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2742 #endif /* CTL_IO_DELAY */ 2743 break; 2744 } 2745 case CTL_ERROR_INJECT: { 2746 struct ctl_error_desc *err_desc, *new_err_desc; 2747 2748 err_desc = (struct ctl_error_desc *)addr; 2749 2750 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2751 M_WAITOK | M_ZERO); 2752 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2753 2754 mtx_lock(&softc->ctl_lock); 2755 if (err_desc->lun_id >= ctl_max_luns || 2756 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2757 mtx_unlock(&softc->ctl_lock); 2758 free(new_err_desc, M_CTL); 2759 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2760 __func__, (uintmax_t)err_desc->lun_id); 2761 retval = EINVAL; 2762 break; 2763 } 2764 mtx_lock(&lun->lun_lock); 2765 mtx_unlock(&softc->ctl_lock); 2766 2767 /* 2768 * We could do some checking here to verify the validity 2769 * of the request, but given the complexity of error 2770 * injection requests, the checking logic would be fairly 2771 * complex. 2772 * 2773 * For now, if the request is invalid, it just won't get 2774 * executed and might get deleted. 2775 */ 2776 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2777 2778 /* 2779 * XXX KDM check to make sure the serial number is unique, 2780 * in case we somehow manage to wrap. That shouldn't 2781 * happen for a very long time, but it's the right thing to 2782 * do. 2783 */ 2784 new_err_desc->serial = lun->error_serial; 2785 err_desc->serial = lun->error_serial; 2786 lun->error_serial++; 2787 2788 mtx_unlock(&lun->lun_lock); 2789 break; 2790 } 2791 case CTL_ERROR_INJECT_DELETE: { 2792 struct ctl_error_desc *delete_desc, *desc, *desc2; 2793 int delete_done; 2794 2795 delete_desc = (struct ctl_error_desc *)addr; 2796 delete_done = 0; 2797 2798 mtx_lock(&softc->ctl_lock); 2799 if (delete_desc->lun_id >= ctl_max_luns || 2800 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2801 mtx_unlock(&softc->ctl_lock); 2802 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2803 __func__, (uintmax_t)delete_desc->lun_id); 2804 retval = EINVAL; 2805 break; 2806 } 2807 mtx_lock(&lun->lun_lock); 2808 mtx_unlock(&softc->ctl_lock); 2809 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2810 if (desc->serial != delete_desc->serial) 2811 continue; 2812 2813 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2814 links); 2815 free(desc, M_CTL); 2816 delete_done = 1; 2817 } 2818 mtx_unlock(&lun->lun_lock); 2819 if (delete_done == 0) { 2820 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2821 "error serial %ju on LUN %u\n", __func__, 2822 delete_desc->serial, delete_desc->lun_id); 2823 retval = EINVAL; 2824 break; 2825 } 2826 break; 2827 } 2828 case CTL_DUMP_STRUCTS: { 2829 int j, k; 2830 struct ctl_port *port; 2831 struct ctl_frontend *fe; 2832 2833 mtx_lock(&softc->ctl_lock); 2834 printf("CTL Persistent Reservation information start:\n"); 2835 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2836 mtx_lock(&lun->lun_lock); 2837 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2838 mtx_unlock(&lun->lun_lock); 2839 continue; 2840 } 2841 2842 for (j = 0; j < ctl_max_ports; j++) { 2843 if (lun->pr_keys[j] == NULL) 2844 continue; 2845 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2846 if (lun->pr_keys[j][k] == 0) 2847 continue; 2848 printf(" LUN %ju port %d iid %d key " 2849 "%#jx\n", lun->lun, j, k, 2850 (uintmax_t)lun->pr_keys[j][k]); 2851 } 2852 } 2853 mtx_unlock(&lun->lun_lock); 2854 } 2855 printf("CTL Persistent Reservation information end\n"); 2856 printf("CTL Ports:\n"); 2857 STAILQ_FOREACH(port, &softc->port_list, links) { 2858 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2859 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2860 port->frontend->name, port->port_type, 2861 port->physical_port, port->virtual_port, 2862 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2863 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2864 if (port->wwpn_iid[j].in_use == 0 && 2865 port->wwpn_iid[j].wwpn == 0 && 2866 port->wwpn_iid[j].name == NULL) 2867 continue; 2868 2869 printf(" iid %u use %d WWPN %#jx '%s'\n", 2870 j, port->wwpn_iid[j].in_use, 2871 (uintmax_t)port->wwpn_iid[j].wwpn, 2872 port->wwpn_iid[j].name); 2873 } 2874 } 2875 printf("CTL Port information end\n"); 2876 mtx_unlock(&softc->ctl_lock); 2877 /* 2878 * XXX KDM calling this without a lock. We'd likely want 2879 * to drop the lock before calling the frontend's dump 2880 * routine anyway. 2881 */ 2882 printf("CTL Frontends:\n"); 2883 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2884 printf(" Frontend '%s'\n", fe->name); 2885 if (fe->fe_dump != NULL) 2886 fe->fe_dump(); 2887 } 2888 printf("CTL Frontend information end\n"); 2889 break; 2890 } 2891 case CTL_LUN_REQ: { 2892 struct ctl_lun_req *lun_req; 2893 struct ctl_backend_driver *backend; 2894 void *packed; 2895 nvlist_t *tmp_args_nvl; 2896 size_t packed_len; 2897 2898 lun_req = (struct ctl_lun_req *)addr; 2899 tmp_args_nvl = lun_req->args_nvl; 2900 2901 backend = ctl_backend_find(lun_req->backend); 2902 if (backend == NULL) { 2903 lun_req->status = CTL_LUN_ERROR; 2904 snprintf(lun_req->error_str, 2905 sizeof(lun_req->error_str), 2906 "Backend \"%s\" not found.", 2907 lun_req->backend); 2908 break; 2909 } 2910 2911 if (lun_req->args != NULL) { 2912 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2913 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2914 free(packed, M_CTL); 2915 lun_req->status = CTL_LUN_ERROR; 2916 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2917 "Cannot copyin args."); 2918 break; 2919 } 2920 lun_req->args_nvl = nvlist_unpack(packed, 2921 lun_req->args_len, 0); 2922 free(packed, M_CTL); 2923 2924 if (lun_req->args_nvl == NULL) { 2925 lun_req->status = CTL_LUN_ERROR; 2926 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2927 "Cannot unpack args nvlist."); 2928 break; 2929 } 2930 } else 2931 lun_req->args_nvl = nvlist_create(0); 2932 2933 retval = backend->ioctl(dev, cmd, addr, flag, td); 2934 nvlist_destroy(lun_req->args_nvl); 2935 lun_req->args_nvl = tmp_args_nvl; 2936 2937 if (lun_req->result_nvl != NULL) { 2938 if (lun_req->result != NULL) { 2939 packed = nvlist_pack(lun_req->result_nvl, 2940 &packed_len); 2941 if (packed == NULL) { 2942 lun_req->status = CTL_LUN_ERROR; 2943 snprintf(lun_req->error_str, 2944 sizeof(lun_req->error_str), 2945 "Cannot pack result nvlist."); 2946 break; 2947 } 2948 2949 if (packed_len > lun_req->result_len) { 2950 lun_req->status = CTL_LUN_ERROR; 2951 snprintf(lun_req->error_str, 2952 sizeof(lun_req->error_str), 2953 "Result nvlist too large."); 2954 free(packed, M_NVLIST); 2955 break; 2956 } 2957 2958 if (copyout(packed, lun_req->result, packed_len)) { 2959 lun_req->status = CTL_LUN_ERROR; 2960 snprintf(lun_req->error_str, 2961 sizeof(lun_req->error_str), 2962 "Cannot copyout() the result."); 2963 free(packed, M_NVLIST); 2964 break; 2965 } 2966 2967 lun_req->result_len = packed_len; 2968 free(packed, M_NVLIST); 2969 } 2970 2971 nvlist_destroy(lun_req->result_nvl); 2972 } 2973 break; 2974 } 2975 case CTL_LUN_LIST: { 2976 struct sbuf *sb; 2977 struct ctl_lun_list *list; 2978 const char *name, *value; 2979 void *cookie; 2980 int type; 2981 2982 list = (struct ctl_lun_list *)addr; 2983 2984 /* 2985 * Allocate a fixed length sbuf here, based on the length 2986 * of the user's buffer. We could allocate an auto-extending 2987 * buffer, and then tell the user how much larger our 2988 * amount of data is than his buffer, but that presents 2989 * some problems: 2990 * 2991 * 1. The sbuf(9) routines use a blocking malloc, and so 2992 * we can't hold a lock while calling them with an 2993 * auto-extending buffer. 2994 * 2995 * 2. There is not currently a LUN reference counting 2996 * mechanism, outside of outstanding transactions on 2997 * the LUN's OOA queue. So a LUN could go away on us 2998 * while we're getting the LUN number, backend-specific 2999 * information, etc. Thus, given the way things 3000 * currently work, we need to hold the CTL lock while 3001 * grabbing LUN information. 3002 * 3003 * So, from the user's standpoint, the best thing to do is 3004 * allocate what he thinks is a reasonable buffer length, 3005 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3006 * double the buffer length and try again. (And repeat 3007 * that until he succeeds.) 3008 */ 3009 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3010 if (sb == NULL) { 3011 list->status = CTL_LUN_LIST_ERROR; 3012 snprintf(list->error_str, sizeof(list->error_str), 3013 "Unable to allocate %d bytes for LUN list", 3014 list->alloc_len); 3015 break; 3016 } 3017 3018 sbuf_printf(sb, "<ctllunlist>\n"); 3019 3020 mtx_lock(&softc->ctl_lock); 3021 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3022 mtx_lock(&lun->lun_lock); 3023 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3024 (uintmax_t)lun->lun); 3025 3026 /* 3027 * Bail out as soon as we see that we've overfilled 3028 * the buffer. 3029 */ 3030 if (retval != 0) 3031 break; 3032 3033 retval = sbuf_printf(sb, "\t<backend_type>%s" 3034 "</backend_type>\n", 3035 (lun->backend == NULL) ? "none" : 3036 lun->backend->name); 3037 3038 if (retval != 0) 3039 break; 3040 3041 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3042 lun->be_lun->lun_type); 3043 3044 if (retval != 0) 3045 break; 3046 3047 if (lun->backend == NULL) { 3048 retval = sbuf_printf(sb, "</lun>\n"); 3049 if (retval != 0) 3050 break; 3051 continue; 3052 } 3053 3054 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3055 (lun->be_lun->maxlba > 0) ? 3056 lun->be_lun->maxlba + 1 : 0); 3057 3058 if (retval != 0) 3059 break; 3060 3061 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3062 lun->be_lun->blocksize); 3063 3064 if (retval != 0) 3065 break; 3066 3067 retval = sbuf_printf(sb, "\t<serial_number>"); 3068 3069 if (retval != 0) 3070 break; 3071 3072 retval = ctl_sbuf_printf_esc(sb, 3073 lun->be_lun->serial_num, 3074 sizeof(lun->be_lun->serial_num)); 3075 3076 if (retval != 0) 3077 break; 3078 3079 retval = sbuf_printf(sb, "</serial_number>\n"); 3080 3081 if (retval != 0) 3082 break; 3083 3084 retval = sbuf_printf(sb, "\t<device_id>"); 3085 3086 if (retval != 0) 3087 break; 3088 3089 retval = ctl_sbuf_printf_esc(sb, 3090 lun->be_lun->device_id, 3091 sizeof(lun->be_lun->device_id)); 3092 3093 if (retval != 0) 3094 break; 3095 3096 retval = sbuf_printf(sb, "</device_id>\n"); 3097 3098 if (retval != 0) 3099 break; 3100 3101 if (lun->backend->lun_info != NULL) { 3102 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3103 if (retval != 0) 3104 break; 3105 } 3106 3107 cookie = NULL; 3108 while ((name = nvlist_next(lun->be_lun->options, &type, 3109 &cookie)) != NULL) { 3110 sbuf_printf(sb, "\t<%s>", name); 3111 3112 if (type == NV_TYPE_STRING) { 3113 value = dnvlist_get_string( 3114 lun->be_lun->options, name, NULL); 3115 if (value != NULL) 3116 sbuf_printf(sb, "%s", value); 3117 } 3118 3119 sbuf_printf(sb, "</%s>\n", name); 3120 } 3121 3122 retval = sbuf_printf(sb, "</lun>\n"); 3123 3124 if (retval != 0) 3125 break; 3126 mtx_unlock(&lun->lun_lock); 3127 } 3128 if (lun != NULL) 3129 mtx_unlock(&lun->lun_lock); 3130 mtx_unlock(&softc->ctl_lock); 3131 3132 if ((retval != 0) 3133 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3134 retval = 0; 3135 sbuf_delete(sb); 3136 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3137 snprintf(list->error_str, sizeof(list->error_str), 3138 "Out of space, %d bytes is too small", 3139 list->alloc_len); 3140 break; 3141 } 3142 3143 sbuf_finish(sb); 3144 3145 retval = copyout(sbuf_data(sb), list->lun_xml, 3146 sbuf_len(sb) + 1); 3147 3148 list->fill_len = sbuf_len(sb) + 1; 3149 list->status = CTL_LUN_LIST_OK; 3150 sbuf_delete(sb); 3151 break; 3152 } 3153 case CTL_ISCSI: { 3154 struct ctl_iscsi *ci; 3155 struct ctl_frontend *fe; 3156 3157 ci = (struct ctl_iscsi *)addr; 3158 3159 fe = ctl_frontend_find("iscsi"); 3160 if (fe == NULL) { 3161 ci->status = CTL_ISCSI_ERROR; 3162 snprintf(ci->error_str, sizeof(ci->error_str), 3163 "Frontend \"iscsi\" not found."); 3164 break; 3165 } 3166 3167 retval = fe->ioctl(dev, cmd, addr, flag, td); 3168 break; 3169 } 3170 case CTL_PORT_REQ: { 3171 struct ctl_req *req; 3172 struct ctl_frontend *fe; 3173 void *packed; 3174 nvlist_t *tmp_args_nvl; 3175 size_t packed_len; 3176 3177 req = (struct ctl_req *)addr; 3178 tmp_args_nvl = req->args_nvl; 3179 3180 fe = ctl_frontend_find(req->driver); 3181 if (fe == NULL) { 3182 req->status = CTL_LUN_ERROR; 3183 snprintf(req->error_str, sizeof(req->error_str), 3184 "Frontend \"%s\" not found.", req->driver); 3185 break; 3186 } 3187 3188 if (req->args != NULL) { 3189 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3190 if (copyin(req->args, packed, req->args_len) != 0) { 3191 free(packed, M_CTL); 3192 req->status = CTL_LUN_ERROR; 3193 snprintf(req->error_str, sizeof(req->error_str), 3194 "Cannot copyin args."); 3195 break; 3196 } 3197 req->args_nvl = nvlist_unpack(packed, 3198 req->args_len, 0); 3199 free(packed, M_CTL); 3200 3201 if (req->args_nvl == NULL) { 3202 req->status = CTL_LUN_ERROR; 3203 snprintf(req->error_str, sizeof(req->error_str), 3204 "Cannot unpack args nvlist."); 3205 break; 3206 } 3207 } else 3208 req->args_nvl = nvlist_create(0); 3209 3210 if (fe->ioctl) 3211 retval = fe->ioctl(dev, cmd, addr, flag, td); 3212 else 3213 retval = ENODEV; 3214 3215 nvlist_destroy(req->args_nvl); 3216 req->args_nvl = tmp_args_nvl; 3217 3218 if (req->result_nvl != NULL) { 3219 if (req->result != NULL) { 3220 packed = nvlist_pack(req->result_nvl, 3221 &packed_len); 3222 if (packed == NULL) { 3223 req->status = CTL_LUN_ERROR; 3224 snprintf(req->error_str, 3225 sizeof(req->error_str), 3226 "Cannot pack result nvlist."); 3227 break; 3228 } 3229 3230 if (packed_len > req->result_len) { 3231 req->status = CTL_LUN_ERROR; 3232 snprintf(req->error_str, 3233 sizeof(req->error_str), 3234 "Result nvlist too large."); 3235 free(packed, M_NVLIST); 3236 break; 3237 } 3238 3239 if (copyout(packed, req->result, packed_len)) { 3240 req->status = CTL_LUN_ERROR; 3241 snprintf(req->error_str, 3242 sizeof(req->error_str), 3243 "Cannot copyout() the result."); 3244 free(packed, M_NVLIST); 3245 break; 3246 } 3247 3248 req->result_len = packed_len; 3249 free(packed, M_NVLIST); 3250 } 3251 3252 nvlist_destroy(req->result_nvl); 3253 } 3254 break; 3255 } 3256 case CTL_PORT_LIST: { 3257 struct sbuf *sb; 3258 struct ctl_port *port; 3259 struct ctl_lun_list *list; 3260 const char *name, *value; 3261 void *cookie; 3262 int j, type; 3263 uint32_t plun; 3264 3265 list = (struct ctl_lun_list *)addr; 3266 3267 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3268 if (sb == NULL) { 3269 list->status = CTL_LUN_LIST_ERROR; 3270 snprintf(list->error_str, sizeof(list->error_str), 3271 "Unable to allocate %d bytes for LUN list", 3272 list->alloc_len); 3273 break; 3274 } 3275 3276 sbuf_printf(sb, "<ctlportlist>\n"); 3277 3278 mtx_lock(&softc->ctl_lock); 3279 STAILQ_FOREACH(port, &softc->port_list, links) { 3280 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3281 (uintmax_t)port->targ_port); 3282 3283 /* 3284 * Bail out as soon as we see that we've overfilled 3285 * the buffer. 3286 */ 3287 if (retval != 0) 3288 break; 3289 3290 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3291 "</frontend_type>\n", port->frontend->name); 3292 if (retval != 0) 3293 break; 3294 3295 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3296 port->port_type); 3297 if (retval != 0) 3298 break; 3299 3300 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3301 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3302 if (retval != 0) 3303 break; 3304 3305 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3306 port->port_name); 3307 if (retval != 0) 3308 break; 3309 3310 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3311 port->physical_port); 3312 if (retval != 0) 3313 break; 3314 3315 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3316 port->virtual_port); 3317 if (retval != 0) 3318 break; 3319 3320 if (port->target_devid != NULL) { 3321 sbuf_printf(sb, "\t<target>"); 3322 ctl_id_sbuf(port->target_devid, sb); 3323 sbuf_printf(sb, "</target>\n"); 3324 } 3325 3326 if (port->port_devid != NULL) { 3327 sbuf_printf(sb, "\t<port>"); 3328 ctl_id_sbuf(port->port_devid, sb); 3329 sbuf_printf(sb, "</port>\n"); 3330 } 3331 3332 if (port->port_info != NULL) { 3333 retval = port->port_info(port->onoff_arg, sb); 3334 if (retval != 0) 3335 break; 3336 } 3337 3338 cookie = NULL; 3339 while ((name = nvlist_next(port->options, &type, 3340 &cookie)) != NULL) { 3341 sbuf_printf(sb, "\t<%s>", name); 3342 3343 if (type == NV_TYPE_STRING) { 3344 value = dnvlist_get_string(port->options, 3345 name, NULL); 3346 if (value != NULL) 3347 sbuf_printf(sb, "%s", value); 3348 } 3349 3350 sbuf_printf(sb, "</%s>\n", name); 3351 } 3352 3353 if (port->lun_map != NULL) { 3354 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3355 for (j = 0; j < port->lun_map_size; j++) { 3356 plun = ctl_lun_map_from_port(port, j); 3357 if (plun == UINT32_MAX) 3358 continue; 3359 sbuf_printf(sb, 3360 "\t<lun id=\"%u\">%u</lun>\n", 3361 j, plun); 3362 } 3363 } 3364 3365 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3366 if (port->wwpn_iid[j].in_use == 0 || 3367 (port->wwpn_iid[j].wwpn == 0 && 3368 port->wwpn_iid[j].name == NULL)) 3369 continue; 3370 3371 if (port->wwpn_iid[j].name != NULL) 3372 retval = sbuf_printf(sb, 3373 "\t<initiator id=\"%u\">%s</initiator>\n", 3374 j, port->wwpn_iid[j].name); 3375 else 3376 retval = sbuf_printf(sb, 3377 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3378 j, port->wwpn_iid[j].wwpn); 3379 if (retval != 0) 3380 break; 3381 } 3382 if (retval != 0) 3383 break; 3384 3385 retval = sbuf_printf(sb, "</targ_port>\n"); 3386 if (retval != 0) 3387 break; 3388 } 3389 mtx_unlock(&softc->ctl_lock); 3390 3391 if ((retval != 0) 3392 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3393 retval = 0; 3394 sbuf_delete(sb); 3395 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3396 snprintf(list->error_str, sizeof(list->error_str), 3397 "Out of space, %d bytes is too small", 3398 list->alloc_len); 3399 break; 3400 } 3401 3402 sbuf_finish(sb); 3403 3404 retval = copyout(sbuf_data(sb), list->lun_xml, 3405 sbuf_len(sb) + 1); 3406 3407 list->fill_len = sbuf_len(sb) + 1; 3408 list->status = CTL_LUN_LIST_OK; 3409 sbuf_delete(sb); 3410 break; 3411 } 3412 case CTL_LUN_MAP: { 3413 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3414 struct ctl_port *port; 3415 3416 mtx_lock(&softc->ctl_lock); 3417 if (lm->port < softc->port_min || 3418 lm->port >= softc->port_max || 3419 (port = softc->ctl_ports[lm->port]) == NULL) { 3420 mtx_unlock(&softc->ctl_lock); 3421 return (ENXIO); 3422 } 3423 if (port->status & CTL_PORT_STATUS_ONLINE) { 3424 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3425 if (ctl_lun_map_to_port(port, lun->lun) == 3426 UINT32_MAX) 3427 continue; 3428 mtx_lock(&lun->lun_lock); 3429 ctl_est_ua_port(lun, lm->port, -1, 3430 CTL_UA_LUN_CHANGE); 3431 mtx_unlock(&lun->lun_lock); 3432 } 3433 } 3434 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3435 if (lm->plun != UINT32_MAX) { 3436 if (lm->lun == UINT32_MAX) 3437 retval = ctl_lun_map_unset(port, lm->plun); 3438 else if (lm->lun < ctl_max_luns && 3439 softc->ctl_luns[lm->lun] != NULL) 3440 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3441 else 3442 return (ENXIO); 3443 } else { 3444 if (lm->lun == UINT32_MAX) 3445 retval = ctl_lun_map_deinit(port); 3446 else 3447 retval = ctl_lun_map_init(port); 3448 } 3449 if (port->status & CTL_PORT_STATUS_ONLINE) 3450 ctl_isc_announce_port(port); 3451 break; 3452 } 3453 case CTL_GET_LUN_STATS: { 3454 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3455 int i; 3456 3457 /* 3458 * XXX KDM no locking here. If the LUN list changes, 3459 * things can blow up. 3460 */ 3461 i = 0; 3462 stats->status = CTL_SS_OK; 3463 stats->fill_len = 0; 3464 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3465 if (lun->lun < stats->first_item) 3466 continue; 3467 if (stats->fill_len + sizeof(lun->stats) > 3468 stats->alloc_len) { 3469 stats->status = CTL_SS_NEED_MORE_SPACE; 3470 break; 3471 } 3472 retval = copyout(&lun->stats, &stats->stats[i++], 3473 sizeof(lun->stats)); 3474 if (retval != 0) 3475 break; 3476 stats->fill_len += sizeof(lun->stats); 3477 } 3478 stats->num_items = softc->num_luns; 3479 stats->flags = CTL_STATS_FLAG_NONE; 3480 #ifdef CTL_TIME_IO 3481 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3482 #endif 3483 getnanouptime(&stats->timestamp); 3484 break; 3485 } 3486 case CTL_GET_PORT_STATS: { 3487 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3488 int i; 3489 3490 /* 3491 * XXX KDM no locking here. If the LUN list changes, 3492 * things can blow up. 3493 */ 3494 i = 0; 3495 stats->status = CTL_SS_OK; 3496 stats->fill_len = 0; 3497 STAILQ_FOREACH(port, &softc->port_list, links) { 3498 if (port->targ_port < stats->first_item) 3499 continue; 3500 if (stats->fill_len + sizeof(port->stats) > 3501 stats->alloc_len) { 3502 stats->status = CTL_SS_NEED_MORE_SPACE; 3503 break; 3504 } 3505 retval = copyout(&port->stats, &stats->stats[i++], 3506 sizeof(port->stats)); 3507 if (retval != 0) 3508 break; 3509 stats->fill_len += sizeof(port->stats); 3510 } 3511 stats->num_items = softc->num_ports; 3512 stats->flags = CTL_STATS_FLAG_NONE; 3513 #ifdef CTL_TIME_IO 3514 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3515 #endif 3516 getnanouptime(&stats->timestamp); 3517 break; 3518 } 3519 default: { 3520 /* XXX KDM should we fix this? */ 3521 #if 0 3522 struct ctl_backend_driver *backend; 3523 unsigned int type; 3524 int found; 3525 3526 found = 0; 3527 3528 /* 3529 * We encode the backend type as the ioctl type for backend 3530 * ioctls. So parse it out here, and then search for a 3531 * backend of this type. 3532 */ 3533 type = _IOC_TYPE(cmd); 3534 3535 STAILQ_FOREACH(backend, &softc->be_list, links) { 3536 if (backend->type == type) { 3537 found = 1; 3538 break; 3539 } 3540 } 3541 if (found == 0) { 3542 printf("ctl: unknown ioctl command %#lx or backend " 3543 "%d\n", cmd, type); 3544 retval = EINVAL; 3545 break; 3546 } 3547 retval = backend->ioctl(dev, cmd, addr, flag, td); 3548 #endif 3549 retval = ENOTTY; 3550 break; 3551 } 3552 } 3553 return (retval); 3554 } 3555 3556 uint32_t 3557 ctl_get_initindex(struct ctl_nexus *nexus) 3558 { 3559 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3560 } 3561 3562 int 3563 ctl_lun_map_init(struct ctl_port *port) 3564 { 3565 struct ctl_softc *softc = port->ctl_softc; 3566 struct ctl_lun *lun; 3567 int size = ctl_lun_map_size; 3568 uint32_t i; 3569 3570 if (port->lun_map == NULL || port->lun_map_size < size) { 3571 port->lun_map_size = 0; 3572 free(port->lun_map, M_CTL); 3573 port->lun_map = malloc(size * sizeof(uint32_t), 3574 M_CTL, M_NOWAIT); 3575 } 3576 if (port->lun_map == NULL) 3577 return (ENOMEM); 3578 for (i = 0; i < size; i++) 3579 port->lun_map[i] = UINT32_MAX; 3580 port->lun_map_size = size; 3581 if (port->status & CTL_PORT_STATUS_ONLINE) { 3582 if (port->lun_disable != NULL) { 3583 STAILQ_FOREACH(lun, &softc->lun_list, links) 3584 port->lun_disable(port->targ_lun_arg, lun->lun); 3585 } 3586 ctl_isc_announce_port(port); 3587 } 3588 return (0); 3589 } 3590 3591 int 3592 ctl_lun_map_deinit(struct ctl_port *port) 3593 { 3594 struct ctl_softc *softc = port->ctl_softc; 3595 struct ctl_lun *lun; 3596 3597 if (port->lun_map == NULL) 3598 return (0); 3599 port->lun_map_size = 0; 3600 free(port->lun_map, M_CTL); 3601 port->lun_map = NULL; 3602 if (port->status & CTL_PORT_STATUS_ONLINE) { 3603 if (port->lun_enable != NULL) { 3604 STAILQ_FOREACH(lun, &softc->lun_list, links) 3605 port->lun_enable(port->targ_lun_arg, lun->lun); 3606 } 3607 ctl_isc_announce_port(port); 3608 } 3609 return (0); 3610 } 3611 3612 int 3613 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3614 { 3615 int status; 3616 uint32_t old; 3617 3618 if (port->lun_map == NULL) { 3619 status = ctl_lun_map_init(port); 3620 if (status != 0) 3621 return (status); 3622 } 3623 if (plun >= port->lun_map_size) 3624 return (EINVAL); 3625 old = port->lun_map[plun]; 3626 port->lun_map[plun] = glun; 3627 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3628 if (port->lun_enable != NULL) 3629 port->lun_enable(port->targ_lun_arg, plun); 3630 ctl_isc_announce_port(port); 3631 } 3632 return (0); 3633 } 3634 3635 int 3636 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3637 { 3638 uint32_t old; 3639 3640 if (port->lun_map == NULL || plun >= port->lun_map_size) 3641 return (0); 3642 old = port->lun_map[plun]; 3643 port->lun_map[plun] = UINT32_MAX; 3644 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3645 if (port->lun_disable != NULL) 3646 port->lun_disable(port->targ_lun_arg, plun); 3647 ctl_isc_announce_port(port); 3648 } 3649 return (0); 3650 } 3651 3652 uint32_t 3653 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3654 { 3655 3656 if (port == NULL) 3657 return (UINT32_MAX); 3658 if (port->lun_map == NULL) 3659 return (lun_id); 3660 if (lun_id > port->lun_map_size) 3661 return (UINT32_MAX); 3662 return (port->lun_map[lun_id]); 3663 } 3664 3665 uint32_t 3666 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3667 { 3668 uint32_t i; 3669 3670 if (port == NULL) 3671 return (UINT32_MAX); 3672 if (port->lun_map == NULL) 3673 return (lun_id); 3674 for (i = 0; i < port->lun_map_size; i++) { 3675 if (port->lun_map[i] == lun_id) 3676 return (i); 3677 } 3678 return (UINT32_MAX); 3679 } 3680 3681 uint32_t 3682 ctl_decode_lun(uint64_t encoded) 3683 { 3684 uint8_t lun[8]; 3685 uint32_t result = 0xffffffff; 3686 3687 be64enc(lun, encoded); 3688 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3689 case RPL_LUNDATA_ATYP_PERIPH: 3690 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3691 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3692 result = lun[1]; 3693 break; 3694 case RPL_LUNDATA_ATYP_FLAT: 3695 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3696 lun[6] == 0 && lun[7] == 0) 3697 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3698 break; 3699 case RPL_LUNDATA_ATYP_EXTLUN: 3700 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3701 case 0x02: 3702 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3703 case 0x00: 3704 result = lun[1]; 3705 break; 3706 case 0x10: 3707 result = (lun[1] << 16) + (lun[2] << 8) + 3708 lun[3]; 3709 break; 3710 case 0x20: 3711 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3712 result = (lun[2] << 24) + 3713 (lun[3] << 16) + (lun[4] << 8) + 3714 lun[5]; 3715 break; 3716 } 3717 break; 3718 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3719 result = 0xffffffff; 3720 break; 3721 } 3722 break; 3723 } 3724 return (result); 3725 } 3726 3727 uint64_t 3728 ctl_encode_lun(uint32_t decoded) 3729 { 3730 uint64_t l = decoded; 3731 3732 if (l <= 0xff) 3733 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3734 if (l <= 0x3fff) 3735 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3736 if (l <= 0xffffff) 3737 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3738 (l << 32)); 3739 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3740 } 3741 3742 int 3743 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3744 { 3745 int i; 3746 3747 for (i = first; i < last; i++) { 3748 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3749 return (i); 3750 } 3751 return (-1); 3752 } 3753 3754 int 3755 ctl_set_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_clear_mask(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 (-1); 3780 else 3781 mask[chunk] &= ~(1 << piece); 3782 3783 return (0); 3784 } 3785 3786 int 3787 ctl_is_set(uint32_t *mask, uint32_t bit) 3788 { 3789 uint32_t chunk, piece; 3790 3791 chunk = bit >> 5; 3792 piece = bit % (sizeof(uint32_t) * 8); 3793 3794 if ((mask[chunk] & (1 << piece)) == 0) 3795 return (0); 3796 else 3797 return (1); 3798 } 3799 3800 static uint64_t 3801 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3802 { 3803 uint64_t *t; 3804 3805 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3806 if (t == NULL) 3807 return (0); 3808 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3809 } 3810 3811 static void 3812 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3813 { 3814 uint64_t *t; 3815 3816 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3817 if (t == NULL) 3818 return; 3819 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3820 } 3821 3822 static void 3823 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3824 { 3825 uint64_t *p; 3826 u_int i; 3827 3828 i = residx/CTL_MAX_INIT_PER_PORT; 3829 if (lun->pr_keys[i] != NULL) 3830 return; 3831 mtx_unlock(&lun->lun_lock); 3832 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3833 M_WAITOK | M_ZERO); 3834 mtx_lock(&lun->lun_lock); 3835 if (lun->pr_keys[i] == NULL) 3836 lun->pr_keys[i] = p; 3837 else 3838 free(p, M_CTL); 3839 } 3840 3841 static void 3842 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3843 { 3844 uint64_t *t; 3845 3846 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3847 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3848 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3849 } 3850 3851 /* 3852 * ctl_softc, pool_name, total_ctl_io are passed in. 3853 * npool is passed out. 3854 */ 3855 int 3856 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3857 uint32_t total_ctl_io, void **npool) 3858 { 3859 struct ctl_io_pool *pool; 3860 3861 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3862 M_NOWAIT | M_ZERO); 3863 if (pool == NULL) 3864 return (ENOMEM); 3865 3866 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3867 pool->ctl_softc = ctl_softc; 3868 #ifdef IO_POOLS 3869 pool->zone = uma_zsecond_create(pool->name, NULL, 3870 NULL, NULL, NULL, ctl_softc->io_zone); 3871 /* uma_prealloc(pool->zone, total_ctl_io); */ 3872 #else 3873 pool->zone = ctl_softc->io_zone; 3874 #endif 3875 3876 *npool = pool; 3877 return (0); 3878 } 3879 3880 void 3881 ctl_pool_free(struct ctl_io_pool *pool) 3882 { 3883 3884 if (pool == NULL) 3885 return; 3886 3887 #ifdef IO_POOLS 3888 uma_zdestroy(pool->zone); 3889 #endif 3890 free(pool, M_CTL); 3891 } 3892 3893 union ctl_io * 3894 ctl_alloc_io(void *pool_ref) 3895 { 3896 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3897 union ctl_io *io; 3898 3899 io = uma_zalloc(pool->zone, M_WAITOK); 3900 if (io != NULL) { 3901 io->io_hdr.pool = pool_ref; 3902 CTL_SOFTC(io) = pool->ctl_softc; 3903 } 3904 return (io); 3905 } 3906 3907 union ctl_io * 3908 ctl_alloc_io_nowait(void *pool_ref) 3909 { 3910 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3911 union ctl_io *io; 3912 3913 io = uma_zalloc(pool->zone, M_NOWAIT); 3914 if (io != NULL) { 3915 io->io_hdr.pool = pool_ref; 3916 CTL_SOFTC(io) = pool->ctl_softc; 3917 } 3918 return (io); 3919 } 3920 3921 void 3922 ctl_free_io(union ctl_io *io) 3923 { 3924 struct ctl_io_pool *pool; 3925 3926 if (io == NULL) 3927 return; 3928 3929 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3930 uma_zfree(pool->zone, io); 3931 } 3932 3933 void 3934 ctl_zero_io(union ctl_io *io) 3935 { 3936 struct ctl_io_pool *pool; 3937 3938 if (io == NULL) 3939 return; 3940 3941 /* 3942 * May need to preserve linked list pointers at some point too. 3943 */ 3944 pool = io->io_hdr.pool; 3945 memset(io, 0, sizeof(*io)); 3946 io->io_hdr.pool = pool; 3947 CTL_SOFTC(io) = pool->ctl_softc; 3948 } 3949 3950 int 3951 ctl_expand_number(const char *buf, uint64_t *num) 3952 { 3953 char *endptr; 3954 uint64_t number; 3955 unsigned shift; 3956 3957 number = strtoq(buf, &endptr, 0); 3958 3959 switch (tolower((unsigned char)*endptr)) { 3960 case 'e': 3961 shift = 60; 3962 break; 3963 case 'p': 3964 shift = 50; 3965 break; 3966 case 't': 3967 shift = 40; 3968 break; 3969 case 'g': 3970 shift = 30; 3971 break; 3972 case 'm': 3973 shift = 20; 3974 break; 3975 case 'k': 3976 shift = 10; 3977 break; 3978 case 'b': 3979 case '\0': /* No unit. */ 3980 *num = number; 3981 return (0); 3982 default: 3983 /* Unrecognized unit. */ 3984 return (-1); 3985 } 3986 3987 if ((number << shift) >> shift != number) { 3988 /* Overflow */ 3989 return (-1); 3990 } 3991 *num = number << shift; 3992 return (0); 3993 } 3994 3995 3996 /* 3997 * This routine could be used in the future to load default and/or saved 3998 * mode page parameters for a particuar lun. 3999 */ 4000 static int 4001 ctl_init_page_index(struct ctl_lun *lun) 4002 { 4003 int i, page_code; 4004 struct ctl_page_index *page_index; 4005 const char *value; 4006 uint64_t ival; 4007 4008 memcpy(&lun->mode_pages.index, page_index_template, 4009 sizeof(page_index_template)); 4010 4011 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4012 4013 page_index = &lun->mode_pages.index[i]; 4014 if (lun->be_lun->lun_type == T_DIRECT && 4015 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4016 continue; 4017 if (lun->be_lun->lun_type == T_PROCESSOR && 4018 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4019 continue; 4020 if (lun->be_lun->lun_type == T_CDROM && 4021 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4022 continue; 4023 4024 page_code = page_index->page_code & SMPH_PC_MASK; 4025 switch (page_code) { 4026 case SMS_RW_ERROR_RECOVERY_PAGE: { 4027 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4028 ("subpage %#x for page %#x is incorrect!", 4029 page_index->subpage, page_code)); 4030 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4031 &rw_er_page_default, 4032 sizeof(rw_er_page_default)); 4033 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4034 &rw_er_page_changeable, 4035 sizeof(rw_er_page_changeable)); 4036 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4037 &rw_er_page_default, 4038 sizeof(rw_er_page_default)); 4039 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4040 &rw_er_page_default, 4041 sizeof(rw_er_page_default)); 4042 page_index->page_data = 4043 (uint8_t *)lun->mode_pages.rw_er_page; 4044 break; 4045 } 4046 case SMS_FORMAT_DEVICE_PAGE: { 4047 struct scsi_format_page *format_page; 4048 4049 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4050 ("subpage %#x for page %#x is incorrect!", 4051 page_index->subpage, page_code)); 4052 4053 /* 4054 * Sectors per track are set above. Bytes per 4055 * sector need to be set here on a per-LUN basis. 4056 */ 4057 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4058 &format_page_default, 4059 sizeof(format_page_default)); 4060 memcpy(&lun->mode_pages.format_page[ 4061 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4062 sizeof(format_page_changeable)); 4063 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4064 &format_page_default, 4065 sizeof(format_page_default)); 4066 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4067 &format_page_default, 4068 sizeof(format_page_default)); 4069 4070 format_page = &lun->mode_pages.format_page[ 4071 CTL_PAGE_CURRENT]; 4072 scsi_ulto2b(lun->be_lun->blocksize, 4073 format_page->bytes_per_sector); 4074 4075 format_page = &lun->mode_pages.format_page[ 4076 CTL_PAGE_DEFAULT]; 4077 scsi_ulto2b(lun->be_lun->blocksize, 4078 format_page->bytes_per_sector); 4079 4080 format_page = &lun->mode_pages.format_page[ 4081 CTL_PAGE_SAVED]; 4082 scsi_ulto2b(lun->be_lun->blocksize, 4083 format_page->bytes_per_sector); 4084 4085 page_index->page_data = 4086 (uint8_t *)lun->mode_pages.format_page; 4087 break; 4088 } 4089 case SMS_RIGID_DISK_PAGE: { 4090 struct scsi_rigid_disk_page *rigid_disk_page; 4091 uint32_t sectors_per_cylinder; 4092 uint64_t cylinders; 4093 #ifndef __XSCALE__ 4094 int shift; 4095 #endif /* !__XSCALE__ */ 4096 4097 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4098 ("subpage %#x for page %#x is incorrect!", 4099 page_index->subpage, page_code)); 4100 4101 /* 4102 * Rotation rate and sectors per track are set 4103 * above. We calculate the cylinders here based on 4104 * capacity. Due to the number of heads and 4105 * sectors per track we're using, smaller arrays 4106 * may turn out to have 0 cylinders. Linux and 4107 * FreeBSD don't pay attention to these mode pages 4108 * to figure out capacity, but Solaris does. It 4109 * seems to deal with 0 cylinders just fine, and 4110 * works out a fake geometry based on the capacity. 4111 */ 4112 memcpy(&lun->mode_pages.rigid_disk_page[ 4113 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4114 sizeof(rigid_disk_page_default)); 4115 memcpy(&lun->mode_pages.rigid_disk_page[ 4116 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4117 sizeof(rigid_disk_page_changeable)); 4118 4119 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4120 CTL_DEFAULT_HEADS; 4121 4122 /* 4123 * The divide method here will be more accurate, 4124 * probably, but results in floating point being 4125 * used in the kernel on i386 (__udivdi3()). On the 4126 * XScale, though, __udivdi3() is implemented in 4127 * software. 4128 * 4129 * The shift method for cylinder calculation is 4130 * accurate if sectors_per_cylinder is a power of 4131 * 2. Otherwise it might be slightly off -- you 4132 * might have a bit of a truncation problem. 4133 */ 4134 #ifdef __XSCALE__ 4135 cylinders = (lun->be_lun->maxlba + 1) / 4136 sectors_per_cylinder; 4137 #else 4138 for (shift = 31; shift > 0; shift--) { 4139 if (sectors_per_cylinder & (1 << shift)) 4140 break; 4141 } 4142 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4143 #endif 4144 4145 /* 4146 * We've basically got 3 bytes, or 24 bits for the 4147 * cylinder size in the mode page. If we're over, 4148 * just round down to 2^24. 4149 */ 4150 if (cylinders > 0xffffff) 4151 cylinders = 0xffffff; 4152 4153 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4154 CTL_PAGE_DEFAULT]; 4155 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4156 4157 if ((value = dnvlist_get_string(lun->be_lun->options, 4158 "rpm", NULL)) != NULL) { 4159 scsi_ulto2b(strtol(value, NULL, 0), 4160 rigid_disk_page->rotation_rate); 4161 } 4162 4163 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4164 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4165 sizeof(rigid_disk_page_default)); 4166 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4167 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4168 sizeof(rigid_disk_page_default)); 4169 4170 page_index->page_data = 4171 (uint8_t *)lun->mode_pages.rigid_disk_page; 4172 break; 4173 } 4174 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4175 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4176 ("subpage %#x for page %#x is incorrect!", 4177 page_index->subpage, page_code)); 4178 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4179 &verify_er_page_default, 4180 sizeof(verify_er_page_default)); 4181 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4182 &verify_er_page_changeable, 4183 sizeof(verify_er_page_changeable)); 4184 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4185 &verify_er_page_default, 4186 sizeof(verify_er_page_default)); 4187 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4188 &verify_er_page_default, 4189 sizeof(verify_er_page_default)); 4190 page_index->page_data = 4191 (uint8_t *)lun->mode_pages.verify_er_page; 4192 break; 4193 } 4194 case SMS_CACHING_PAGE: { 4195 struct scsi_caching_page *caching_page; 4196 4197 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4198 ("subpage %#x for page %#x is incorrect!", 4199 page_index->subpage, page_code)); 4200 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4201 &caching_page_default, 4202 sizeof(caching_page_default)); 4203 memcpy(&lun->mode_pages.caching_page[ 4204 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4205 sizeof(caching_page_changeable)); 4206 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4207 &caching_page_default, 4208 sizeof(caching_page_default)); 4209 caching_page = &lun->mode_pages.caching_page[ 4210 CTL_PAGE_SAVED]; 4211 value = dnvlist_get_string(lun->be_lun->options, 4212 "writecache", NULL); 4213 if (value != NULL && strcmp(value, "off") == 0) 4214 caching_page->flags1 &= ~SCP_WCE; 4215 value = dnvlist_get_string(lun->be_lun->options, 4216 "readcache", NULL); 4217 if (value != NULL && strcmp(value, "off") == 0) 4218 caching_page->flags1 |= SCP_RCD; 4219 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4220 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4221 sizeof(caching_page_default)); 4222 page_index->page_data = 4223 (uint8_t *)lun->mode_pages.caching_page; 4224 break; 4225 } 4226 case SMS_CONTROL_MODE_PAGE: { 4227 switch (page_index->subpage) { 4228 case SMS_SUBPAGE_PAGE_0: { 4229 struct scsi_control_page *control_page; 4230 4231 memcpy(&lun->mode_pages.control_page[ 4232 CTL_PAGE_DEFAULT], 4233 &control_page_default, 4234 sizeof(control_page_default)); 4235 memcpy(&lun->mode_pages.control_page[ 4236 CTL_PAGE_CHANGEABLE], 4237 &control_page_changeable, 4238 sizeof(control_page_changeable)); 4239 memcpy(&lun->mode_pages.control_page[ 4240 CTL_PAGE_SAVED], 4241 &control_page_default, 4242 sizeof(control_page_default)); 4243 control_page = &lun->mode_pages.control_page[ 4244 CTL_PAGE_SAVED]; 4245 value = dnvlist_get_string(lun->be_lun->options, 4246 "reordering", NULL); 4247 if (value != NULL && 4248 strcmp(value, "unrestricted") == 0) { 4249 control_page->queue_flags &= 4250 ~SCP_QUEUE_ALG_MASK; 4251 control_page->queue_flags |= 4252 SCP_QUEUE_ALG_UNRESTRICTED; 4253 } 4254 memcpy(&lun->mode_pages.control_page[ 4255 CTL_PAGE_CURRENT], 4256 &lun->mode_pages.control_page[ 4257 CTL_PAGE_SAVED], 4258 sizeof(control_page_default)); 4259 page_index->page_data = 4260 (uint8_t *)lun->mode_pages.control_page; 4261 break; 4262 } 4263 case 0x01: 4264 memcpy(&lun->mode_pages.control_ext_page[ 4265 CTL_PAGE_DEFAULT], 4266 &control_ext_page_default, 4267 sizeof(control_ext_page_default)); 4268 memcpy(&lun->mode_pages.control_ext_page[ 4269 CTL_PAGE_CHANGEABLE], 4270 &control_ext_page_changeable, 4271 sizeof(control_ext_page_changeable)); 4272 memcpy(&lun->mode_pages.control_ext_page[ 4273 CTL_PAGE_SAVED], 4274 &control_ext_page_default, 4275 sizeof(control_ext_page_default)); 4276 memcpy(&lun->mode_pages.control_ext_page[ 4277 CTL_PAGE_CURRENT], 4278 &lun->mode_pages.control_ext_page[ 4279 CTL_PAGE_SAVED], 4280 sizeof(control_ext_page_default)); 4281 page_index->page_data = 4282 (uint8_t *)lun->mode_pages.control_ext_page; 4283 break; 4284 default: 4285 panic("subpage %#x for page %#x is incorrect!", 4286 page_index->subpage, page_code); 4287 } 4288 break; 4289 } 4290 case SMS_INFO_EXCEPTIONS_PAGE: { 4291 switch (page_index->subpage) { 4292 case SMS_SUBPAGE_PAGE_0: 4293 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4294 &ie_page_default, 4295 sizeof(ie_page_default)); 4296 memcpy(&lun->mode_pages.ie_page[ 4297 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4298 sizeof(ie_page_changeable)); 4299 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4300 &ie_page_default, 4301 sizeof(ie_page_default)); 4302 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4303 &ie_page_default, 4304 sizeof(ie_page_default)); 4305 page_index->page_data = 4306 (uint8_t *)lun->mode_pages.ie_page; 4307 break; 4308 case 0x02: { 4309 struct ctl_logical_block_provisioning_page *page; 4310 4311 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4312 &lbp_page_default, 4313 sizeof(lbp_page_default)); 4314 memcpy(&lun->mode_pages.lbp_page[ 4315 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4316 sizeof(lbp_page_changeable)); 4317 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4318 &lbp_page_default, 4319 sizeof(lbp_page_default)); 4320 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4321 value = dnvlist_get_string(lun->be_lun->options, 4322 "avail-threshold", NULL); 4323 if (value != NULL && 4324 ctl_expand_number(value, &ival) == 0) { 4325 page->descr[0].flags |= SLBPPD_ENABLED | 4326 SLBPPD_ARMING_DEC; 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[0].count); 4333 } 4334 value = dnvlist_get_string(lun->be_lun->options, 4335 "used-threshold", NULL); 4336 if (value != NULL && 4337 ctl_expand_number(value, &ival) == 0) { 4338 page->descr[1].flags |= SLBPPD_ENABLED | 4339 SLBPPD_ARMING_INC; 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[1].count); 4346 } 4347 value = dnvlist_get_string(lun->be_lun->options, 4348 "pool-avail-threshold", NULL); 4349 if (value != NULL && 4350 ctl_expand_number(value, &ival) == 0) { 4351 page->descr[2].flags |= SLBPPD_ENABLED | 4352 SLBPPD_ARMING_DEC; 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[2].count); 4359 } 4360 value = dnvlist_get_string(lun->be_lun->options, 4361 "pool-used-threshold", NULL); 4362 if (value != NULL && 4363 ctl_expand_number(value, &ival) == 0) { 4364 page->descr[3].flags |= SLBPPD_ENABLED | 4365 SLBPPD_ARMING_INC; 4366 if (lun->be_lun->blocksize) 4367 ival /= lun->be_lun->blocksize; 4368 else 4369 ival /= 512; 4370 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4371 page->descr[3].count); 4372 } 4373 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4374 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4375 sizeof(lbp_page_default)); 4376 page_index->page_data = 4377 (uint8_t *)lun->mode_pages.lbp_page; 4378 break; 4379 } 4380 default: 4381 panic("subpage %#x for page %#x is incorrect!", 4382 page_index->subpage, page_code); 4383 } 4384 break; 4385 } 4386 case SMS_CDDVD_CAPS_PAGE:{ 4387 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4388 ("subpage %#x for page %#x is incorrect!", 4389 page_index->subpage, page_code)); 4390 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4391 &cddvd_page_default, 4392 sizeof(cddvd_page_default)); 4393 memcpy(&lun->mode_pages.cddvd_page[ 4394 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4395 sizeof(cddvd_page_changeable)); 4396 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4397 &cddvd_page_default, 4398 sizeof(cddvd_page_default)); 4399 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4400 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4401 sizeof(cddvd_page_default)); 4402 page_index->page_data = 4403 (uint8_t *)lun->mode_pages.cddvd_page; 4404 break; 4405 } 4406 default: 4407 panic("invalid page code value %#x", page_code); 4408 } 4409 } 4410 4411 return (CTL_RETVAL_COMPLETE); 4412 } 4413 4414 static int 4415 ctl_init_log_page_index(struct ctl_lun *lun) 4416 { 4417 struct ctl_page_index *page_index; 4418 int i, j, k, prev; 4419 4420 memcpy(&lun->log_pages.index, log_page_index_template, 4421 sizeof(log_page_index_template)); 4422 4423 prev = -1; 4424 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4425 4426 page_index = &lun->log_pages.index[i]; 4427 if (lun->be_lun->lun_type == T_DIRECT && 4428 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4429 continue; 4430 if (lun->be_lun->lun_type == T_PROCESSOR && 4431 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4432 continue; 4433 if (lun->be_lun->lun_type == T_CDROM && 4434 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4435 continue; 4436 4437 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4438 lun->backend->lun_attr == NULL) 4439 continue; 4440 4441 if (page_index->page_code != prev) { 4442 lun->log_pages.pages_page[j] = page_index->page_code; 4443 prev = page_index->page_code; 4444 j++; 4445 } 4446 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4447 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4448 k++; 4449 } 4450 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4451 lun->log_pages.index[0].page_len = j; 4452 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4453 lun->log_pages.index[1].page_len = k * 2; 4454 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4455 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4456 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4457 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4458 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4459 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4460 4461 return (CTL_RETVAL_COMPLETE); 4462 } 4463 4464 static int 4465 hex2bin(const char *str, uint8_t *buf, int buf_size) 4466 { 4467 int i; 4468 u_char c; 4469 4470 memset(buf, 0, buf_size); 4471 while (isspace(str[0])) 4472 str++; 4473 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4474 str += 2; 4475 buf_size *= 2; 4476 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4477 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4478 str++; 4479 c = str[i]; 4480 if (isdigit(c)) 4481 c -= '0'; 4482 else if (isalpha(c)) 4483 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4484 else 4485 break; 4486 if (c >= 16) 4487 break; 4488 if ((i & 1) == 0) 4489 buf[i / 2] |= (c << 4); 4490 else 4491 buf[i / 2] |= c; 4492 } 4493 return ((i + 1) / 2); 4494 } 4495 4496 /* 4497 * LUN allocation. 4498 * 4499 * Requirements: 4500 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4501 * wants us to allocate the LUN and he can block. 4502 * - ctl_softc is always set 4503 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4504 * 4505 * Returns 0 for success, non-zero (errno) for failure. 4506 */ 4507 static int 4508 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4509 struct ctl_be_lun *const be_lun) 4510 { 4511 struct ctl_lun *nlun, *lun; 4512 struct scsi_vpd_id_descriptor *desc; 4513 struct scsi_vpd_id_t10 *t10id; 4514 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4515 int lun_number, lun_malloced; 4516 int devidlen, idlen1, idlen2 = 0, len; 4517 4518 if (be_lun == NULL) 4519 return (EINVAL); 4520 4521 /* 4522 * We currently only support Direct Access or Processor LUN types. 4523 */ 4524 switch (be_lun->lun_type) { 4525 case T_DIRECT: 4526 case T_PROCESSOR: 4527 case T_CDROM: 4528 break; 4529 case T_SEQUENTIAL: 4530 case T_CHANGER: 4531 default: 4532 be_lun->lun_config_status(be_lun->be_lun, 4533 CTL_LUN_CONFIG_FAILURE); 4534 break; 4535 } 4536 if (ctl_lun == NULL) { 4537 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4538 lun_malloced = 1; 4539 } else { 4540 lun_malloced = 0; 4541 lun = ctl_lun; 4542 } 4543 4544 memset(lun, 0, sizeof(*lun)); 4545 if (lun_malloced) 4546 lun->flags = CTL_LUN_MALLOCED; 4547 4548 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4549 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4550 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4551 M_DEVBUF, M_WAITOK | M_ZERO); 4552 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4553 M_DEVBUF, M_WAITOK | M_ZERO); 4554 4555 /* Generate LUN ID. */ 4556 devidlen = max(CTL_DEVID_MIN_LEN, 4557 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4558 idlen1 = sizeof(*t10id) + devidlen; 4559 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4560 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4561 if (scsiname != NULL) { 4562 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4563 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4564 } 4565 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4566 if (eui != NULL) { 4567 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4568 } 4569 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4570 if (naa != NULL) { 4571 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4572 } 4573 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4574 if (uuid != NULL) { 4575 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4576 } 4577 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4578 M_CTL, M_WAITOK | M_ZERO); 4579 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4580 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4581 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4582 desc->length = idlen1; 4583 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4584 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4585 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4586 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4587 } else { 4588 strncpy(t10id->vendor, vendor, 4589 min(sizeof(t10id->vendor), strlen(vendor))); 4590 } 4591 strncpy((char *)t10id->vendor_spec_id, 4592 (char *)be_lun->device_id, devidlen); 4593 if (scsiname != NULL) { 4594 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4595 desc->length); 4596 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4597 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4598 SVPD_ID_TYPE_SCSI_NAME; 4599 desc->length = idlen2; 4600 strlcpy(desc->identifier, scsiname, idlen2); 4601 } 4602 if (eui != NULL) { 4603 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4604 desc->length); 4605 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4606 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4607 SVPD_ID_TYPE_EUI64; 4608 desc->length = hex2bin(eui, desc->identifier, 16); 4609 desc->length = desc->length > 12 ? 16 : 4610 (desc->length > 8 ? 12 : 8); 4611 len -= 16 - desc->length; 4612 } 4613 if (naa != NULL) { 4614 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4615 desc->length); 4616 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4617 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4618 SVPD_ID_TYPE_NAA; 4619 desc->length = hex2bin(naa, desc->identifier, 16); 4620 desc->length = desc->length > 8 ? 16 : 8; 4621 len -= 16 - desc->length; 4622 } 4623 if (uuid != NULL) { 4624 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4625 desc->length); 4626 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4627 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4628 SVPD_ID_TYPE_UUID; 4629 desc->identifier[0] = 0x10; 4630 hex2bin(uuid, &desc->identifier[2], 16); 4631 desc->length = 18; 4632 } 4633 lun->lun_devid->len = len; 4634 4635 mtx_lock(&ctl_softc->ctl_lock); 4636 /* 4637 * See if the caller requested a particular LUN number. If so, see 4638 * if it is available. Otherwise, allocate the first available LUN. 4639 */ 4640 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4641 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4642 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4643 mtx_unlock(&ctl_softc->ctl_lock); 4644 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4645 printf("ctl: requested LUN ID %d is higher " 4646 "than ctl_max_luns - 1 (%d)\n", 4647 be_lun->req_lun_id, ctl_max_luns - 1); 4648 } else { 4649 /* 4650 * XXX KDM return an error, or just assign 4651 * another LUN ID in this case?? 4652 */ 4653 printf("ctl: requested LUN ID %d is already " 4654 "in use\n", be_lun->req_lun_id); 4655 } 4656 fail: 4657 free(lun->lun_devid, M_CTL); 4658 if (lun->flags & CTL_LUN_MALLOCED) 4659 free(lun, M_CTL); 4660 be_lun->lun_config_status(be_lun->be_lun, 4661 CTL_LUN_CONFIG_FAILURE); 4662 return (ENOSPC); 4663 } 4664 lun_number = be_lun->req_lun_id; 4665 } else { 4666 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4667 if (lun_number == -1) { 4668 mtx_unlock(&ctl_softc->ctl_lock); 4669 printf("ctl: can't allocate LUN, out of LUNs\n"); 4670 goto fail; 4671 } 4672 } 4673 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4674 mtx_unlock(&ctl_softc->ctl_lock); 4675 4676 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4677 lun->lun = lun_number; 4678 lun->be_lun = be_lun; 4679 /* 4680 * The processor LUN is always enabled. Disk LUNs come on line 4681 * disabled, and must be enabled by the backend. 4682 */ 4683 lun->flags |= CTL_LUN_DISABLED; 4684 lun->backend = be_lun->be; 4685 be_lun->ctl_lun = lun; 4686 be_lun->lun_id = lun_number; 4687 atomic_add_int(&be_lun->be->num_luns, 1); 4688 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4689 lun->flags |= CTL_LUN_EJECTED; 4690 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4691 lun->flags |= CTL_LUN_NO_MEDIA; 4692 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4693 lun->flags |= CTL_LUN_STOPPED; 4694 4695 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4696 lun->flags |= CTL_LUN_PRIMARY_SC; 4697 4698 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4699 if (value != NULL) { 4700 if (strcmp(value, "on") == 0) 4701 lun->flags |= CTL_LUN_REMOVABLE; 4702 } else if (be_lun->lun_type == T_CDROM) 4703 lun->flags |= CTL_LUN_REMOVABLE; 4704 4705 lun->ctl_softc = ctl_softc; 4706 #ifdef CTL_TIME_IO 4707 lun->last_busy = getsbinuptime(); 4708 #endif 4709 TAILQ_INIT(&lun->ooa_queue); 4710 TAILQ_INIT(&lun->blocked_queue); 4711 STAILQ_INIT(&lun->error_list); 4712 lun->ie_reported = 1; 4713 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4714 ctl_tpc_lun_init(lun); 4715 if (lun->flags & CTL_LUN_REMOVABLE) { 4716 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4717 M_CTL, M_WAITOK); 4718 } 4719 4720 /* 4721 * Initialize the mode and log page index. 4722 */ 4723 ctl_init_page_index(lun); 4724 ctl_init_log_page_index(lun); 4725 4726 /* Setup statistics gathering */ 4727 lun->stats.item = lun_number; 4728 4729 /* 4730 * Now, before we insert this lun on the lun list, set the lun 4731 * inventory changed UA for all other luns. 4732 */ 4733 mtx_lock(&ctl_softc->ctl_lock); 4734 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4735 mtx_lock(&nlun->lun_lock); 4736 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4737 mtx_unlock(&nlun->lun_lock); 4738 } 4739 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4740 ctl_softc->ctl_luns[lun_number] = lun; 4741 ctl_softc->num_luns++; 4742 mtx_unlock(&ctl_softc->ctl_lock); 4743 4744 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4745 return (0); 4746 } 4747 4748 /* 4749 * Delete a LUN. 4750 * Assumptions: 4751 * - LUN has already been marked invalid and any pending I/O has been taken 4752 * care of. 4753 */ 4754 static int 4755 ctl_free_lun(struct ctl_lun *lun) 4756 { 4757 struct ctl_softc *softc = lun->ctl_softc; 4758 struct ctl_lun *nlun; 4759 int i; 4760 4761 KASSERT(TAILQ_EMPTY(&lun->ooa_queue), 4762 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4763 4764 mtx_lock(&softc->ctl_lock); 4765 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4766 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4767 softc->ctl_luns[lun->lun] = NULL; 4768 softc->num_luns--; 4769 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4770 mtx_lock(&nlun->lun_lock); 4771 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4772 mtx_unlock(&nlun->lun_lock); 4773 } 4774 mtx_unlock(&softc->ctl_lock); 4775 4776 /* 4777 * Tell the backend to free resources, if this LUN has a backend. 4778 */ 4779 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4780 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4781 4782 lun->ie_reportcnt = UINT32_MAX; 4783 callout_drain(&lun->ie_callout); 4784 ctl_tpc_lun_shutdown(lun); 4785 mtx_destroy(&lun->lun_lock); 4786 free(lun->lun_devid, M_CTL); 4787 for (i = 0; i < ctl_max_ports; i++) 4788 free(lun->pending_ua[i], M_CTL); 4789 free(lun->pending_ua, M_DEVBUF); 4790 for (i = 0; i < ctl_max_ports; i++) 4791 free(lun->pr_keys[i], M_CTL); 4792 free(lun->pr_keys, M_DEVBUF); 4793 free(lun->write_buffer, M_CTL); 4794 free(lun->prevent, M_CTL); 4795 if (lun->flags & CTL_LUN_MALLOCED) 4796 free(lun, M_CTL); 4797 4798 return (0); 4799 } 4800 4801 static void 4802 ctl_create_lun(struct ctl_be_lun *be_lun) 4803 { 4804 4805 /* 4806 * ctl_alloc_lun() should handle all potential failure cases. 4807 */ 4808 ctl_alloc_lun(control_softc, NULL, be_lun); 4809 } 4810 4811 int 4812 ctl_add_lun(struct ctl_be_lun *be_lun) 4813 { 4814 struct ctl_softc *softc = control_softc; 4815 4816 mtx_lock(&softc->ctl_lock); 4817 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4818 mtx_unlock(&softc->ctl_lock); 4819 wakeup(&softc->pending_lun_queue); 4820 4821 return (0); 4822 } 4823 4824 int 4825 ctl_enable_lun(struct ctl_be_lun *be_lun) 4826 { 4827 struct ctl_softc *softc; 4828 struct ctl_port *port, *nport; 4829 struct ctl_lun *lun; 4830 int retval; 4831 4832 lun = (struct ctl_lun *)be_lun->ctl_lun; 4833 softc = lun->ctl_softc; 4834 4835 mtx_lock(&softc->ctl_lock); 4836 mtx_lock(&lun->lun_lock); 4837 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4838 /* 4839 * eh? Why did we get called if the LUN is already 4840 * enabled? 4841 */ 4842 mtx_unlock(&lun->lun_lock); 4843 mtx_unlock(&softc->ctl_lock); 4844 return (0); 4845 } 4846 lun->flags &= ~CTL_LUN_DISABLED; 4847 mtx_unlock(&lun->lun_lock); 4848 4849 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4850 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4851 port->lun_map != NULL || port->lun_enable == NULL) 4852 continue; 4853 4854 /* 4855 * Drop the lock while we call the FETD's enable routine. 4856 * This can lead to a callback into CTL (at least in the 4857 * case of the internal initiator frontend. 4858 */ 4859 mtx_unlock(&softc->ctl_lock); 4860 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4861 mtx_lock(&softc->ctl_lock); 4862 if (retval != 0) { 4863 printf("%s: FETD %s port %d returned error " 4864 "%d for lun_enable on lun %jd\n", 4865 __func__, port->port_name, port->targ_port, 4866 retval, (intmax_t)lun->lun); 4867 } 4868 } 4869 4870 mtx_unlock(&softc->ctl_lock); 4871 ctl_isc_announce_lun(lun); 4872 4873 return (0); 4874 } 4875 4876 int 4877 ctl_disable_lun(struct ctl_be_lun *be_lun) 4878 { 4879 struct ctl_softc *softc; 4880 struct ctl_port *port; 4881 struct ctl_lun *lun; 4882 int retval; 4883 4884 lun = (struct ctl_lun *)be_lun->ctl_lun; 4885 softc = lun->ctl_softc; 4886 4887 mtx_lock(&softc->ctl_lock); 4888 mtx_lock(&lun->lun_lock); 4889 if (lun->flags & CTL_LUN_DISABLED) { 4890 mtx_unlock(&lun->lun_lock); 4891 mtx_unlock(&softc->ctl_lock); 4892 return (0); 4893 } 4894 lun->flags |= CTL_LUN_DISABLED; 4895 mtx_unlock(&lun->lun_lock); 4896 4897 STAILQ_FOREACH(port, &softc->port_list, links) { 4898 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4899 port->lun_map != NULL || port->lun_disable == NULL) 4900 continue; 4901 4902 /* 4903 * Drop the lock before we call the frontend's disable 4904 * routine, to avoid lock order reversals. 4905 * 4906 * XXX KDM what happens if the frontend list changes while 4907 * we're traversing it? It's unlikely, but should be handled. 4908 */ 4909 mtx_unlock(&softc->ctl_lock); 4910 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4911 mtx_lock(&softc->ctl_lock); 4912 if (retval != 0) { 4913 printf("%s: FETD %s port %d returned error " 4914 "%d for lun_disable on lun %jd\n", 4915 __func__, port->port_name, port->targ_port, 4916 retval, (intmax_t)lun->lun); 4917 } 4918 } 4919 4920 mtx_unlock(&softc->ctl_lock); 4921 ctl_isc_announce_lun(lun); 4922 4923 return (0); 4924 } 4925 4926 int 4927 ctl_start_lun(struct ctl_be_lun *be_lun) 4928 { 4929 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4930 4931 mtx_lock(&lun->lun_lock); 4932 lun->flags &= ~CTL_LUN_STOPPED; 4933 mtx_unlock(&lun->lun_lock); 4934 return (0); 4935 } 4936 4937 int 4938 ctl_stop_lun(struct ctl_be_lun *be_lun) 4939 { 4940 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4941 4942 mtx_lock(&lun->lun_lock); 4943 lun->flags |= CTL_LUN_STOPPED; 4944 mtx_unlock(&lun->lun_lock); 4945 return (0); 4946 } 4947 4948 int 4949 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4950 { 4951 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4952 4953 mtx_lock(&lun->lun_lock); 4954 lun->flags |= CTL_LUN_NO_MEDIA; 4955 mtx_unlock(&lun->lun_lock); 4956 return (0); 4957 } 4958 4959 int 4960 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4961 { 4962 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4963 union ctl_ha_msg msg; 4964 4965 mtx_lock(&lun->lun_lock); 4966 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4967 if (lun->flags & CTL_LUN_REMOVABLE) 4968 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4969 mtx_unlock(&lun->lun_lock); 4970 if ((lun->flags & CTL_LUN_REMOVABLE) && 4971 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4972 bzero(&msg.ua, sizeof(msg.ua)); 4973 msg.hdr.msg_type = CTL_MSG_UA; 4974 msg.hdr.nexus.initid = -1; 4975 msg.hdr.nexus.targ_port = -1; 4976 msg.hdr.nexus.targ_lun = lun->lun; 4977 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4978 msg.ua.ua_all = 1; 4979 msg.ua.ua_set = 1; 4980 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4981 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4982 M_WAITOK); 4983 } 4984 return (0); 4985 } 4986 4987 int 4988 ctl_lun_ejected(struct ctl_be_lun *be_lun) 4989 { 4990 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4991 4992 mtx_lock(&lun->lun_lock); 4993 lun->flags |= CTL_LUN_EJECTED; 4994 mtx_unlock(&lun->lun_lock); 4995 return (0); 4996 } 4997 4998 int 4999 ctl_lun_primary(struct ctl_be_lun *be_lun) 5000 { 5001 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5002 5003 mtx_lock(&lun->lun_lock); 5004 lun->flags |= CTL_LUN_PRIMARY_SC; 5005 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5006 mtx_unlock(&lun->lun_lock); 5007 ctl_isc_announce_lun(lun); 5008 return (0); 5009 } 5010 5011 int 5012 ctl_lun_secondary(struct ctl_be_lun *be_lun) 5013 { 5014 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5015 5016 mtx_lock(&lun->lun_lock); 5017 lun->flags &= ~CTL_LUN_PRIMARY_SC; 5018 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 5019 mtx_unlock(&lun->lun_lock); 5020 ctl_isc_announce_lun(lun); 5021 return (0); 5022 } 5023 5024 int 5025 ctl_invalidate_lun(struct ctl_be_lun *be_lun) 5026 { 5027 struct ctl_lun *lun; 5028 5029 lun = (struct ctl_lun *)be_lun->ctl_lun; 5030 5031 mtx_lock(&lun->lun_lock); 5032 5033 /* 5034 * The LUN needs to be disabled before it can be marked invalid. 5035 */ 5036 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5037 mtx_unlock(&lun->lun_lock); 5038 return (-1); 5039 } 5040 /* 5041 * Mark the LUN invalid. 5042 */ 5043 lun->flags |= CTL_LUN_INVALID; 5044 5045 /* 5046 * If there is nothing in the OOA queue, go ahead and free the LUN. 5047 * If we have something in the OOA queue, we'll free it when the 5048 * last I/O completes. 5049 */ 5050 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5051 mtx_unlock(&lun->lun_lock); 5052 ctl_free_lun(lun); 5053 } else 5054 mtx_unlock(&lun->lun_lock); 5055 5056 return (0); 5057 } 5058 5059 void 5060 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5061 { 5062 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5063 union ctl_ha_msg msg; 5064 5065 mtx_lock(&lun->lun_lock); 5066 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5067 mtx_unlock(&lun->lun_lock); 5068 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5069 /* Send msg to other side. */ 5070 bzero(&msg.ua, sizeof(msg.ua)); 5071 msg.hdr.msg_type = CTL_MSG_UA; 5072 msg.hdr.nexus.initid = -1; 5073 msg.hdr.nexus.targ_port = -1; 5074 msg.hdr.nexus.targ_lun = lun->lun; 5075 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5076 msg.ua.ua_all = 1; 5077 msg.ua.ua_set = 1; 5078 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5079 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5080 M_WAITOK); 5081 } 5082 } 5083 5084 /* 5085 * Backend "memory move is complete" callback for requests that never 5086 * make it down to say RAIDCore's configuration code. 5087 */ 5088 int 5089 ctl_config_move_done(union ctl_io *io) 5090 { 5091 int retval; 5092 5093 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5094 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5095 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5096 5097 if ((io->io_hdr.port_status != 0) && 5098 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5099 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5100 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 5101 /*retry_count*/ io->io_hdr.port_status); 5102 } else if (io->scsiio.kern_data_resid != 0 && 5103 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 5104 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5105 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5106 ctl_set_invalid_field_ciu(&io->scsiio); 5107 } 5108 5109 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5110 ctl_data_print(io); 5111 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5112 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5113 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5114 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5115 /* 5116 * XXX KDM just assuming a single pointer here, and not a 5117 * S/G list. If we start using S/G lists for config data, 5118 * we'll need to know how to clean them up here as well. 5119 */ 5120 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5121 free(io->scsiio.kern_data_ptr, M_CTL); 5122 ctl_done(io); 5123 retval = CTL_RETVAL_COMPLETE; 5124 } else { 5125 /* 5126 * XXX KDM now we need to continue data movement. Some 5127 * options: 5128 * - call ctl_scsiio() again? We don't do this for data 5129 * writes, because for those at least we know ahead of 5130 * time where the write will go and how long it is. For 5131 * config writes, though, that information is largely 5132 * contained within the write itself, thus we need to 5133 * parse out the data again. 5134 * 5135 * - Call some other function once the data is in? 5136 */ 5137 5138 /* 5139 * XXX KDM call ctl_scsiio() again for now, and check flag 5140 * bits to see whether we're allocated or not. 5141 */ 5142 retval = ctl_scsiio(&io->scsiio); 5143 } 5144 return (retval); 5145 } 5146 5147 /* 5148 * This gets called by a backend driver when it is done with a 5149 * data_submit method. 5150 */ 5151 void 5152 ctl_data_submit_done(union ctl_io *io) 5153 { 5154 /* 5155 * If the IO_CONT flag is set, we need to call the supplied 5156 * function to continue processing the I/O, instead of completing 5157 * the I/O just yet. 5158 * 5159 * If there is an error, though, we don't want to keep processing. 5160 * Instead, just send status back to the initiator. 5161 */ 5162 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5163 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5164 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5165 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5166 io->scsiio.io_cont(io); 5167 return; 5168 } 5169 ctl_done(io); 5170 } 5171 5172 /* 5173 * This gets called by a backend driver when it is done with a 5174 * configuration write. 5175 */ 5176 void 5177 ctl_config_write_done(union ctl_io *io) 5178 { 5179 uint8_t *buf; 5180 5181 /* 5182 * If the IO_CONT flag is set, we need to call the supplied 5183 * function to continue processing the I/O, instead of completing 5184 * the I/O just yet. 5185 * 5186 * If there is an error, though, we don't want to keep processing. 5187 * Instead, just send status back to the initiator. 5188 */ 5189 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5190 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5191 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5192 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5193 io->scsiio.io_cont(io); 5194 return; 5195 } 5196 /* 5197 * Since a configuration write can be done for commands that actually 5198 * have data allocated, like write buffer, and commands that have 5199 * no data, like start/stop unit, we need to check here. 5200 */ 5201 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5202 buf = io->scsiio.kern_data_ptr; 5203 else 5204 buf = NULL; 5205 ctl_done(io); 5206 if (buf) 5207 free(buf, M_CTL); 5208 } 5209 5210 void 5211 ctl_config_read_done(union ctl_io *io) 5212 { 5213 uint8_t *buf; 5214 5215 /* 5216 * If there is some error -- we are done, skip data transfer. 5217 */ 5218 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5219 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5220 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5221 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5222 buf = io->scsiio.kern_data_ptr; 5223 else 5224 buf = NULL; 5225 ctl_done(io); 5226 if (buf) 5227 free(buf, M_CTL); 5228 return; 5229 } 5230 5231 /* 5232 * If the IO_CONT flag is set, we need to call the supplied 5233 * function to continue processing the I/O, instead of completing 5234 * the I/O just yet. 5235 */ 5236 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5237 io->scsiio.io_cont(io); 5238 return; 5239 } 5240 5241 ctl_datamove(io); 5242 } 5243 5244 /* 5245 * SCSI release command. 5246 */ 5247 int 5248 ctl_scsi_release(struct ctl_scsiio *ctsio) 5249 { 5250 struct ctl_lun *lun = CTL_LUN(ctsio); 5251 uint32_t residx; 5252 5253 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5254 5255 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5256 5257 /* 5258 * XXX KDM right now, we only support LUN reservation. We don't 5259 * support 3rd party reservations, or extent reservations, which 5260 * might actually need the parameter list. If we've gotten this 5261 * far, we've got a LUN reservation. Anything else got kicked out 5262 * above. So, according to SPC, ignore the length. 5263 */ 5264 5265 mtx_lock(&lun->lun_lock); 5266 5267 /* 5268 * According to SPC, it is not an error for an intiator to attempt 5269 * to release a reservation on a LUN that isn't reserved, or that 5270 * is reserved by another initiator. The reservation can only be 5271 * released, though, by the initiator who made it or by one of 5272 * several reset type events. 5273 */ 5274 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5275 lun->flags &= ~CTL_LUN_RESERVED; 5276 5277 mtx_unlock(&lun->lun_lock); 5278 5279 ctl_set_success(ctsio); 5280 ctl_done((union ctl_io *)ctsio); 5281 return (CTL_RETVAL_COMPLETE); 5282 } 5283 5284 int 5285 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5286 { 5287 struct ctl_lun *lun = CTL_LUN(ctsio); 5288 uint32_t residx; 5289 5290 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5291 5292 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5293 5294 /* 5295 * XXX KDM right now, we only support LUN reservation. We don't 5296 * support 3rd party reservations, or extent reservations, which 5297 * might actually need the parameter list. If we've gotten this 5298 * far, we've got a LUN reservation. Anything else got kicked out 5299 * above. So, according to SPC, ignore the length. 5300 */ 5301 5302 mtx_lock(&lun->lun_lock); 5303 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5304 ctl_set_reservation_conflict(ctsio); 5305 goto bailout; 5306 } 5307 5308 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5309 if (lun->flags & CTL_LUN_PR_RESERVED) { 5310 ctl_set_success(ctsio); 5311 goto bailout; 5312 } 5313 5314 lun->flags |= CTL_LUN_RESERVED; 5315 lun->res_idx = residx; 5316 ctl_set_success(ctsio); 5317 5318 bailout: 5319 mtx_unlock(&lun->lun_lock); 5320 ctl_done((union ctl_io *)ctsio); 5321 return (CTL_RETVAL_COMPLETE); 5322 } 5323 5324 int 5325 ctl_start_stop(struct ctl_scsiio *ctsio) 5326 { 5327 struct ctl_lun *lun = CTL_LUN(ctsio); 5328 struct scsi_start_stop_unit *cdb; 5329 int retval; 5330 5331 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5332 5333 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5334 5335 if ((cdb->how & SSS_PC_MASK) == 0) { 5336 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5337 (cdb->how & SSS_START) == 0) { 5338 uint32_t residx; 5339 5340 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5341 if (ctl_get_prkey(lun, residx) == 0 || 5342 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5343 5344 ctl_set_reservation_conflict(ctsio); 5345 ctl_done((union ctl_io *)ctsio); 5346 return (CTL_RETVAL_COMPLETE); 5347 } 5348 } 5349 5350 if ((cdb->how & SSS_LOEJ) && 5351 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5352 ctl_set_invalid_field(ctsio, 5353 /*sks_valid*/ 1, 5354 /*command*/ 1, 5355 /*field*/ 4, 5356 /*bit_valid*/ 1, 5357 /*bit*/ 1); 5358 ctl_done((union ctl_io *)ctsio); 5359 return (CTL_RETVAL_COMPLETE); 5360 } 5361 5362 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5363 lun->prevent_count > 0) { 5364 /* "Medium removal prevented" */ 5365 ctl_set_sense(ctsio, /*current_error*/ 1, 5366 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5367 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5368 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5369 ctl_done((union ctl_io *)ctsio); 5370 return (CTL_RETVAL_COMPLETE); 5371 } 5372 } 5373 5374 retval = lun->backend->config_write((union ctl_io *)ctsio); 5375 return (retval); 5376 } 5377 5378 int 5379 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5380 { 5381 struct ctl_lun *lun = CTL_LUN(ctsio); 5382 struct scsi_prevent *cdb; 5383 int retval; 5384 uint32_t initidx; 5385 5386 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5387 5388 cdb = (struct scsi_prevent *)ctsio->cdb; 5389 5390 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5391 ctl_set_invalid_opcode(ctsio); 5392 ctl_done((union ctl_io *)ctsio); 5393 return (CTL_RETVAL_COMPLETE); 5394 } 5395 5396 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5397 mtx_lock(&lun->lun_lock); 5398 if ((cdb->how & PR_PREVENT) && 5399 ctl_is_set(lun->prevent, initidx) == 0) { 5400 ctl_set_mask(lun->prevent, initidx); 5401 lun->prevent_count++; 5402 } else if ((cdb->how & PR_PREVENT) == 0 && 5403 ctl_is_set(lun->prevent, initidx)) { 5404 ctl_clear_mask(lun->prevent, initidx); 5405 lun->prevent_count--; 5406 } 5407 mtx_unlock(&lun->lun_lock); 5408 retval = lun->backend->config_write((union ctl_io *)ctsio); 5409 return (retval); 5410 } 5411 5412 /* 5413 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5414 * we don't really do anything with the LBA and length fields if the user 5415 * passes them in. Instead we'll just flush out the cache for the entire 5416 * LUN. 5417 */ 5418 int 5419 ctl_sync_cache(struct ctl_scsiio *ctsio) 5420 { 5421 struct ctl_lun *lun = CTL_LUN(ctsio); 5422 struct ctl_lba_len_flags *lbalen; 5423 uint64_t starting_lba; 5424 uint32_t block_count; 5425 int retval; 5426 uint8_t byte2; 5427 5428 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5429 5430 retval = 0; 5431 5432 switch (ctsio->cdb[0]) { 5433 case SYNCHRONIZE_CACHE: { 5434 struct scsi_sync_cache *cdb; 5435 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5436 5437 starting_lba = scsi_4btoul(cdb->begin_lba); 5438 block_count = scsi_2btoul(cdb->lb_count); 5439 byte2 = cdb->byte2; 5440 break; 5441 } 5442 case SYNCHRONIZE_CACHE_16: { 5443 struct scsi_sync_cache_16 *cdb; 5444 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5445 5446 starting_lba = scsi_8btou64(cdb->begin_lba); 5447 block_count = scsi_4btoul(cdb->lb_count); 5448 byte2 = cdb->byte2; 5449 break; 5450 } 5451 default: 5452 ctl_set_invalid_opcode(ctsio); 5453 ctl_done((union ctl_io *)ctsio); 5454 goto bailout; 5455 break; /* NOTREACHED */ 5456 } 5457 5458 /* 5459 * We check the LBA and length, but don't do anything with them. 5460 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5461 * get flushed. This check will just help satisfy anyone who wants 5462 * to see an error for an out of range LBA. 5463 */ 5464 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5465 ctl_set_lba_out_of_range(ctsio, 5466 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5467 ctl_done((union ctl_io *)ctsio); 5468 goto bailout; 5469 } 5470 5471 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5472 lbalen->lba = starting_lba; 5473 lbalen->len = block_count; 5474 lbalen->flags = byte2; 5475 retval = lun->backend->config_write((union ctl_io *)ctsio); 5476 5477 bailout: 5478 return (retval); 5479 } 5480 5481 int 5482 ctl_format(struct ctl_scsiio *ctsio) 5483 { 5484 struct scsi_format *cdb; 5485 int length, defect_list_len; 5486 5487 CTL_DEBUG_PRINT(("ctl_format\n")); 5488 5489 cdb = (struct scsi_format *)ctsio->cdb; 5490 5491 length = 0; 5492 if (cdb->byte2 & SF_FMTDATA) { 5493 if (cdb->byte2 & SF_LONGLIST) 5494 length = sizeof(struct scsi_format_header_long); 5495 else 5496 length = sizeof(struct scsi_format_header_short); 5497 } 5498 5499 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5500 && (length > 0)) { 5501 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5502 ctsio->kern_data_len = length; 5503 ctsio->kern_total_len = length; 5504 ctsio->kern_rel_offset = 0; 5505 ctsio->kern_sg_entries = 0; 5506 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5507 ctsio->be_move_done = ctl_config_move_done; 5508 ctl_datamove((union ctl_io *)ctsio); 5509 5510 return (CTL_RETVAL_COMPLETE); 5511 } 5512 5513 defect_list_len = 0; 5514 5515 if (cdb->byte2 & SF_FMTDATA) { 5516 if (cdb->byte2 & SF_LONGLIST) { 5517 struct scsi_format_header_long *header; 5518 5519 header = (struct scsi_format_header_long *) 5520 ctsio->kern_data_ptr; 5521 5522 defect_list_len = scsi_4btoul(header->defect_list_len); 5523 if (defect_list_len != 0) { 5524 ctl_set_invalid_field(ctsio, 5525 /*sks_valid*/ 1, 5526 /*command*/ 0, 5527 /*field*/ 2, 5528 /*bit_valid*/ 0, 5529 /*bit*/ 0); 5530 goto bailout; 5531 } 5532 } else { 5533 struct scsi_format_header_short *header; 5534 5535 header = (struct scsi_format_header_short *) 5536 ctsio->kern_data_ptr; 5537 5538 defect_list_len = scsi_2btoul(header->defect_list_len); 5539 if (defect_list_len != 0) { 5540 ctl_set_invalid_field(ctsio, 5541 /*sks_valid*/ 1, 5542 /*command*/ 0, 5543 /*field*/ 2, 5544 /*bit_valid*/ 0, 5545 /*bit*/ 0); 5546 goto bailout; 5547 } 5548 } 5549 } 5550 5551 ctl_set_success(ctsio); 5552 bailout: 5553 5554 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5555 free(ctsio->kern_data_ptr, M_CTL); 5556 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5557 } 5558 5559 ctl_done((union ctl_io *)ctsio); 5560 return (CTL_RETVAL_COMPLETE); 5561 } 5562 5563 int 5564 ctl_read_buffer(struct ctl_scsiio *ctsio) 5565 { 5566 struct ctl_lun *lun = CTL_LUN(ctsio); 5567 uint64_t buffer_offset; 5568 uint32_t len; 5569 uint8_t byte2; 5570 static uint8_t descr[4]; 5571 static uint8_t echo_descr[4] = { 0 }; 5572 5573 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5574 5575 switch (ctsio->cdb[0]) { 5576 case READ_BUFFER: { 5577 struct scsi_read_buffer *cdb; 5578 5579 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5580 buffer_offset = scsi_3btoul(cdb->offset); 5581 len = scsi_3btoul(cdb->length); 5582 byte2 = cdb->byte2; 5583 break; 5584 } 5585 case READ_BUFFER_16: { 5586 struct scsi_read_buffer_16 *cdb; 5587 5588 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5589 buffer_offset = scsi_8btou64(cdb->offset); 5590 len = scsi_4btoul(cdb->length); 5591 byte2 = cdb->byte2; 5592 break; 5593 } 5594 default: /* This shouldn't happen. */ 5595 ctl_set_invalid_opcode(ctsio); 5596 ctl_done((union ctl_io *)ctsio); 5597 return (CTL_RETVAL_COMPLETE); 5598 } 5599 5600 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5601 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5602 ctl_set_invalid_field(ctsio, 5603 /*sks_valid*/ 1, 5604 /*command*/ 1, 5605 /*field*/ 6, 5606 /*bit_valid*/ 0, 5607 /*bit*/ 0); 5608 ctl_done((union ctl_io *)ctsio); 5609 return (CTL_RETVAL_COMPLETE); 5610 } 5611 5612 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5613 descr[0] = 0; 5614 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5615 ctsio->kern_data_ptr = descr; 5616 len = min(len, sizeof(descr)); 5617 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5618 ctsio->kern_data_ptr = echo_descr; 5619 len = min(len, sizeof(echo_descr)); 5620 } else { 5621 if (lun->write_buffer == NULL) { 5622 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5623 M_CTL, M_WAITOK); 5624 } 5625 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5626 } 5627 ctsio->kern_data_len = len; 5628 ctsio->kern_total_len = len; 5629 ctsio->kern_rel_offset = 0; 5630 ctsio->kern_sg_entries = 0; 5631 ctl_set_success(ctsio); 5632 ctsio->be_move_done = ctl_config_move_done; 5633 ctl_datamove((union ctl_io *)ctsio); 5634 return (CTL_RETVAL_COMPLETE); 5635 } 5636 5637 int 5638 ctl_write_buffer(struct ctl_scsiio *ctsio) 5639 { 5640 struct ctl_lun *lun = CTL_LUN(ctsio); 5641 struct scsi_write_buffer *cdb; 5642 int buffer_offset, len; 5643 5644 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5645 5646 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5647 5648 len = scsi_3btoul(cdb->length); 5649 buffer_offset = scsi_3btoul(cdb->offset); 5650 5651 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5652 ctl_set_invalid_field(ctsio, 5653 /*sks_valid*/ 1, 5654 /*command*/ 1, 5655 /*field*/ 6, 5656 /*bit_valid*/ 0, 5657 /*bit*/ 0); 5658 ctl_done((union ctl_io *)ctsio); 5659 return (CTL_RETVAL_COMPLETE); 5660 } 5661 5662 /* 5663 * If we've got a kernel request that hasn't been malloced yet, 5664 * malloc it and tell the caller the data buffer is here. 5665 */ 5666 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5667 if (lun->write_buffer == NULL) { 5668 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5669 M_CTL, M_WAITOK); 5670 } 5671 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5672 ctsio->kern_data_len = len; 5673 ctsio->kern_total_len = len; 5674 ctsio->kern_rel_offset = 0; 5675 ctsio->kern_sg_entries = 0; 5676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5677 ctsio->be_move_done = ctl_config_move_done; 5678 ctl_datamove((union ctl_io *)ctsio); 5679 5680 return (CTL_RETVAL_COMPLETE); 5681 } 5682 5683 ctl_set_success(ctsio); 5684 ctl_done((union ctl_io *)ctsio); 5685 return (CTL_RETVAL_COMPLETE); 5686 } 5687 5688 int 5689 ctl_write_same(struct ctl_scsiio *ctsio) 5690 { 5691 struct ctl_lun *lun = CTL_LUN(ctsio); 5692 struct ctl_lba_len_flags *lbalen; 5693 uint64_t lba; 5694 uint32_t num_blocks; 5695 int len, retval; 5696 uint8_t byte2; 5697 5698 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5699 5700 switch (ctsio->cdb[0]) { 5701 case WRITE_SAME_10: { 5702 struct scsi_write_same_10 *cdb; 5703 5704 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5705 5706 lba = scsi_4btoul(cdb->addr); 5707 num_blocks = scsi_2btoul(cdb->length); 5708 byte2 = cdb->byte2; 5709 break; 5710 } 5711 case WRITE_SAME_16: { 5712 struct scsi_write_same_16 *cdb; 5713 5714 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5715 5716 lba = scsi_8btou64(cdb->addr); 5717 num_blocks = scsi_4btoul(cdb->length); 5718 byte2 = cdb->byte2; 5719 break; 5720 } 5721 default: 5722 /* 5723 * We got a command we don't support. This shouldn't 5724 * happen, commands should be filtered out above us. 5725 */ 5726 ctl_set_invalid_opcode(ctsio); 5727 ctl_done((union ctl_io *)ctsio); 5728 5729 return (CTL_RETVAL_COMPLETE); 5730 break; /* NOTREACHED */ 5731 } 5732 5733 /* ANCHOR flag can be used only together with UNMAP */ 5734 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5735 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5736 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5737 ctl_done((union ctl_io *)ctsio); 5738 return (CTL_RETVAL_COMPLETE); 5739 } 5740 5741 /* 5742 * The first check is to make sure we're in bounds, the second 5743 * check is to catch wrap-around problems. If the lba + num blocks 5744 * is less than the lba, then we've wrapped around and the block 5745 * range is invalid anyway. 5746 */ 5747 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5748 || ((lba + num_blocks) < lba)) { 5749 ctl_set_lba_out_of_range(ctsio, 5750 MAX(lba, lun->be_lun->maxlba + 1)); 5751 ctl_done((union ctl_io *)ctsio); 5752 return (CTL_RETVAL_COMPLETE); 5753 } 5754 5755 /* Zero number of blocks means "to the last logical block" */ 5756 if (num_blocks == 0) { 5757 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5758 ctl_set_invalid_field(ctsio, 5759 /*sks_valid*/ 0, 5760 /*command*/ 1, 5761 /*field*/ 0, 5762 /*bit_valid*/ 0, 5763 /*bit*/ 0); 5764 ctl_done((union ctl_io *)ctsio); 5765 return (CTL_RETVAL_COMPLETE); 5766 } 5767 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5768 } 5769 5770 len = lun->be_lun->blocksize; 5771 5772 /* 5773 * If we've got a kernel request that hasn't been malloced yet, 5774 * malloc it and tell the caller the data buffer is here. 5775 */ 5776 if ((byte2 & SWS_NDOB) == 0 && 5777 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5778 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5779 ctsio->kern_data_len = len; 5780 ctsio->kern_total_len = len; 5781 ctsio->kern_rel_offset = 0; 5782 ctsio->kern_sg_entries = 0; 5783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5784 ctsio->be_move_done = ctl_config_move_done; 5785 ctl_datamove((union ctl_io *)ctsio); 5786 5787 return (CTL_RETVAL_COMPLETE); 5788 } 5789 5790 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5791 lbalen->lba = lba; 5792 lbalen->len = num_blocks; 5793 lbalen->flags = byte2; 5794 retval = lun->backend->config_write((union ctl_io *)ctsio); 5795 5796 return (retval); 5797 } 5798 5799 int 5800 ctl_unmap(struct ctl_scsiio *ctsio) 5801 { 5802 struct ctl_lun *lun = CTL_LUN(ctsio); 5803 struct scsi_unmap *cdb; 5804 struct ctl_ptr_len_flags *ptrlen; 5805 struct scsi_unmap_header *hdr; 5806 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5807 uint64_t lba; 5808 uint32_t num_blocks; 5809 int len, retval; 5810 uint8_t byte2; 5811 5812 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5813 5814 cdb = (struct scsi_unmap *)ctsio->cdb; 5815 len = scsi_2btoul(cdb->length); 5816 byte2 = cdb->byte2; 5817 5818 /* 5819 * If we've got a kernel request that hasn't been malloced yet, 5820 * malloc it and tell the caller the data buffer is here. 5821 */ 5822 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5823 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5824 ctsio->kern_data_len = len; 5825 ctsio->kern_total_len = len; 5826 ctsio->kern_rel_offset = 0; 5827 ctsio->kern_sg_entries = 0; 5828 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5829 ctsio->be_move_done = ctl_config_move_done; 5830 ctl_datamove((union ctl_io *)ctsio); 5831 5832 return (CTL_RETVAL_COMPLETE); 5833 } 5834 5835 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5836 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5837 if (len < sizeof (*hdr) || 5838 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5839 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5840 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5841 ctl_set_invalid_field(ctsio, 5842 /*sks_valid*/ 0, 5843 /*command*/ 0, 5844 /*field*/ 0, 5845 /*bit_valid*/ 0, 5846 /*bit*/ 0); 5847 goto done; 5848 } 5849 len = scsi_2btoul(hdr->desc_length); 5850 buf = (struct scsi_unmap_desc *)(hdr + 1); 5851 end = buf + len / sizeof(*buf); 5852 5853 endnz = buf; 5854 for (range = buf; range < end; range++) { 5855 lba = scsi_8btou64(range->lba); 5856 num_blocks = scsi_4btoul(range->length); 5857 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5858 || ((lba + num_blocks) < lba)) { 5859 ctl_set_lba_out_of_range(ctsio, 5860 MAX(lba, lun->be_lun->maxlba + 1)); 5861 ctl_done((union ctl_io *)ctsio); 5862 return (CTL_RETVAL_COMPLETE); 5863 } 5864 if (num_blocks != 0) 5865 endnz = range + 1; 5866 } 5867 5868 /* 5869 * Block backend can not handle zero last range. 5870 * Filter it out and return if there is nothing left. 5871 */ 5872 len = (uint8_t *)endnz - (uint8_t *)buf; 5873 if (len == 0) { 5874 ctl_set_success(ctsio); 5875 goto done; 5876 } 5877 5878 mtx_lock(&lun->lun_lock); 5879 ptrlen = (struct ctl_ptr_len_flags *) 5880 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5881 ptrlen->ptr = (void *)buf; 5882 ptrlen->len = len; 5883 ptrlen->flags = byte2; 5884 ctl_check_blocked(lun); 5885 mtx_unlock(&lun->lun_lock); 5886 5887 retval = lun->backend->config_write((union ctl_io *)ctsio); 5888 return (retval); 5889 5890 done: 5891 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5892 free(ctsio->kern_data_ptr, M_CTL); 5893 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5894 } 5895 ctl_done((union ctl_io *)ctsio); 5896 return (CTL_RETVAL_COMPLETE); 5897 } 5898 5899 int 5900 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5901 struct ctl_page_index *page_index, uint8_t *page_ptr) 5902 { 5903 struct ctl_lun *lun = CTL_LUN(ctsio); 5904 uint8_t *current_cp; 5905 int set_ua; 5906 uint32_t initidx; 5907 5908 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5909 set_ua = 0; 5910 5911 current_cp = (page_index->page_data + (page_index->page_len * 5912 CTL_PAGE_CURRENT)); 5913 5914 mtx_lock(&lun->lun_lock); 5915 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5916 memcpy(current_cp, page_ptr, page_index->page_len); 5917 set_ua = 1; 5918 } 5919 if (set_ua != 0) 5920 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5921 mtx_unlock(&lun->lun_lock); 5922 if (set_ua) { 5923 ctl_isc_announce_mode(lun, 5924 ctl_get_initindex(&ctsio->io_hdr.nexus), 5925 page_index->page_code, page_index->subpage); 5926 } 5927 return (CTL_RETVAL_COMPLETE); 5928 } 5929 5930 static void 5931 ctl_ie_timer(void *arg) 5932 { 5933 struct ctl_lun *lun = arg; 5934 uint64_t t; 5935 5936 if (lun->ie_asc == 0) 5937 return; 5938 5939 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5940 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5941 else 5942 lun->ie_reported = 0; 5943 5944 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5945 lun->ie_reportcnt++; 5946 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5947 if (t == 0 || t == UINT32_MAX) 5948 t = 3000; /* 5 min */ 5949 callout_schedule(&lun->ie_callout, t * hz / 10); 5950 } 5951 } 5952 5953 int 5954 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5955 struct ctl_page_index *page_index, uint8_t *page_ptr) 5956 { 5957 struct ctl_lun *lun = CTL_LUN(ctsio); 5958 struct scsi_info_exceptions_page *pg; 5959 uint64_t t; 5960 5961 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5962 5963 pg = (struct scsi_info_exceptions_page *)page_ptr; 5964 mtx_lock(&lun->lun_lock); 5965 if (pg->info_flags & SIEP_FLAGS_TEST) { 5966 lun->ie_asc = 0x5d; 5967 lun->ie_ascq = 0xff; 5968 if (pg->mrie == SIEP_MRIE_UA) { 5969 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5970 lun->ie_reported = 1; 5971 } else { 5972 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5973 lun->ie_reported = -1; 5974 } 5975 lun->ie_reportcnt = 1; 5976 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5977 lun->ie_reportcnt++; 5978 t = scsi_4btoul(pg->interval_timer); 5979 if (t == 0 || t == UINT32_MAX) 5980 t = 3000; /* 5 min */ 5981 callout_reset(&lun->ie_callout, t * hz / 10, 5982 ctl_ie_timer, lun); 5983 } 5984 } else { 5985 lun->ie_asc = 0; 5986 lun->ie_ascq = 0; 5987 lun->ie_reported = 1; 5988 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5989 lun->ie_reportcnt = UINT32_MAX; 5990 callout_stop(&lun->ie_callout); 5991 } 5992 mtx_unlock(&lun->lun_lock); 5993 return (CTL_RETVAL_COMPLETE); 5994 } 5995 5996 static int 5997 ctl_do_mode_select(union ctl_io *io) 5998 { 5999 struct ctl_lun *lun = CTL_LUN(io); 6000 struct scsi_mode_page_header *page_header; 6001 struct ctl_page_index *page_index; 6002 struct ctl_scsiio *ctsio; 6003 int page_len, page_len_offset, page_len_size; 6004 union ctl_modepage_info *modepage_info; 6005 uint16_t *len_left, *len_used; 6006 int retval, i; 6007 6008 ctsio = &io->scsiio; 6009 page_index = NULL; 6010 page_len = 0; 6011 6012 modepage_info = (union ctl_modepage_info *) 6013 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6014 len_left = &modepage_info->header.len_left; 6015 len_used = &modepage_info->header.len_used; 6016 6017 do_next_page: 6018 6019 page_header = (struct scsi_mode_page_header *) 6020 (ctsio->kern_data_ptr + *len_used); 6021 6022 if (*len_left == 0) { 6023 free(ctsio->kern_data_ptr, M_CTL); 6024 ctl_set_success(ctsio); 6025 ctl_done((union ctl_io *)ctsio); 6026 return (CTL_RETVAL_COMPLETE); 6027 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6028 6029 free(ctsio->kern_data_ptr, M_CTL); 6030 ctl_set_param_len_error(ctsio); 6031 ctl_done((union ctl_io *)ctsio); 6032 return (CTL_RETVAL_COMPLETE); 6033 6034 } else if ((page_header->page_code & SMPH_SPF) 6035 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6036 6037 free(ctsio->kern_data_ptr, M_CTL); 6038 ctl_set_param_len_error(ctsio); 6039 ctl_done((union ctl_io *)ctsio); 6040 return (CTL_RETVAL_COMPLETE); 6041 } 6042 6043 6044 /* 6045 * XXX KDM should we do something with the block descriptor? 6046 */ 6047 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6048 page_index = &lun->mode_pages.index[i]; 6049 if (lun->be_lun->lun_type == T_DIRECT && 6050 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6051 continue; 6052 if (lun->be_lun->lun_type == T_PROCESSOR && 6053 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6054 continue; 6055 if (lun->be_lun->lun_type == T_CDROM && 6056 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6057 continue; 6058 6059 if ((page_index->page_code & SMPH_PC_MASK) != 6060 (page_header->page_code & SMPH_PC_MASK)) 6061 continue; 6062 6063 /* 6064 * If neither page has a subpage code, then we've got a 6065 * match. 6066 */ 6067 if (((page_index->page_code & SMPH_SPF) == 0) 6068 && ((page_header->page_code & SMPH_SPF) == 0)) { 6069 page_len = page_header->page_length; 6070 break; 6071 } 6072 6073 /* 6074 * If both pages have subpages, then the subpage numbers 6075 * have to match. 6076 */ 6077 if ((page_index->page_code & SMPH_SPF) 6078 && (page_header->page_code & SMPH_SPF)) { 6079 struct scsi_mode_page_header_sp *sph; 6080 6081 sph = (struct scsi_mode_page_header_sp *)page_header; 6082 if (page_index->subpage == sph->subpage) { 6083 page_len = scsi_2btoul(sph->page_length); 6084 break; 6085 } 6086 } 6087 } 6088 6089 /* 6090 * If we couldn't find the page, or if we don't have a mode select 6091 * handler for it, send back an error to the user. 6092 */ 6093 if ((i >= CTL_NUM_MODE_PAGES) 6094 || (page_index->select_handler == NULL)) { 6095 ctl_set_invalid_field(ctsio, 6096 /*sks_valid*/ 1, 6097 /*command*/ 0, 6098 /*field*/ *len_used, 6099 /*bit_valid*/ 0, 6100 /*bit*/ 0); 6101 free(ctsio->kern_data_ptr, M_CTL); 6102 ctl_done((union ctl_io *)ctsio); 6103 return (CTL_RETVAL_COMPLETE); 6104 } 6105 6106 if (page_index->page_code & SMPH_SPF) { 6107 page_len_offset = 2; 6108 page_len_size = 2; 6109 } else { 6110 page_len_size = 1; 6111 page_len_offset = 1; 6112 } 6113 6114 /* 6115 * If the length the initiator gives us isn't the one we specify in 6116 * the mode page header, or if they didn't specify enough data in 6117 * the CDB to avoid truncating this page, kick out the request. 6118 */ 6119 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6120 ctl_set_invalid_field(ctsio, 6121 /*sks_valid*/ 1, 6122 /*command*/ 0, 6123 /*field*/ *len_used + page_len_offset, 6124 /*bit_valid*/ 0, 6125 /*bit*/ 0); 6126 free(ctsio->kern_data_ptr, M_CTL); 6127 ctl_done((union ctl_io *)ctsio); 6128 return (CTL_RETVAL_COMPLETE); 6129 } 6130 if (*len_left < page_index->page_len) { 6131 free(ctsio->kern_data_ptr, M_CTL); 6132 ctl_set_param_len_error(ctsio); 6133 ctl_done((union ctl_io *)ctsio); 6134 return (CTL_RETVAL_COMPLETE); 6135 } 6136 6137 /* 6138 * Run through the mode page, checking to make sure that the bits 6139 * the user changed are actually legal for him to change. 6140 */ 6141 for (i = 0; i < page_index->page_len; i++) { 6142 uint8_t *user_byte, *change_mask, *current_byte; 6143 int bad_bit; 6144 int j; 6145 6146 user_byte = (uint8_t *)page_header + i; 6147 change_mask = page_index->page_data + 6148 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6149 current_byte = page_index->page_data + 6150 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6151 6152 /* 6153 * Check to see whether the user set any bits in this byte 6154 * that he is not allowed to set. 6155 */ 6156 if ((*user_byte & ~(*change_mask)) == 6157 (*current_byte & ~(*change_mask))) 6158 continue; 6159 6160 /* 6161 * Go through bit by bit to determine which one is illegal. 6162 */ 6163 bad_bit = 0; 6164 for (j = 7; j >= 0; j--) { 6165 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6166 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6167 bad_bit = i; 6168 break; 6169 } 6170 } 6171 ctl_set_invalid_field(ctsio, 6172 /*sks_valid*/ 1, 6173 /*command*/ 0, 6174 /*field*/ *len_used + i, 6175 /*bit_valid*/ 1, 6176 /*bit*/ bad_bit); 6177 free(ctsio->kern_data_ptr, M_CTL); 6178 ctl_done((union ctl_io *)ctsio); 6179 return (CTL_RETVAL_COMPLETE); 6180 } 6181 6182 /* 6183 * Decrement these before we call the page handler, since we may 6184 * end up getting called back one way or another before the handler 6185 * returns to this context. 6186 */ 6187 *len_left -= page_index->page_len; 6188 *len_used += page_index->page_len; 6189 6190 retval = page_index->select_handler(ctsio, page_index, 6191 (uint8_t *)page_header); 6192 6193 /* 6194 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6195 * wait until this queued command completes to finish processing 6196 * the mode page. If it returns anything other than 6197 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6198 * already set the sense information, freed the data pointer, and 6199 * completed the io for us. 6200 */ 6201 if (retval != CTL_RETVAL_COMPLETE) 6202 goto bailout_no_done; 6203 6204 /* 6205 * If the initiator sent us more than one page, parse the next one. 6206 */ 6207 if (*len_left > 0) 6208 goto do_next_page; 6209 6210 ctl_set_success(ctsio); 6211 free(ctsio->kern_data_ptr, M_CTL); 6212 ctl_done((union ctl_io *)ctsio); 6213 6214 bailout_no_done: 6215 6216 return (CTL_RETVAL_COMPLETE); 6217 6218 } 6219 6220 int 6221 ctl_mode_select(struct ctl_scsiio *ctsio) 6222 { 6223 struct ctl_lun *lun = CTL_LUN(ctsio); 6224 union ctl_modepage_info *modepage_info; 6225 int bd_len, i, header_size, param_len, rtd; 6226 uint32_t initidx; 6227 6228 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6229 switch (ctsio->cdb[0]) { 6230 case MODE_SELECT_6: { 6231 struct scsi_mode_select_6 *cdb; 6232 6233 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6234 6235 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6236 param_len = cdb->length; 6237 header_size = sizeof(struct scsi_mode_header_6); 6238 break; 6239 } 6240 case MODE_SELECT_10: { 6241 struct scsi_mode_select_10 *cdb; 6242 6243 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6244 6245 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6246 param_len = scsi_2btoul(cdb->length); 6247 header_size = sizeof(struct scsi_mode_header_10); 6248 break; 6249 } 6250 default: 6251 ctl_set_invalid_opcode(ctsio); 6252 ctl_done((union ctl_io *)ctsio); 6253 return (CTL_RETVAL_COMPLETE); 6254 } 6255 6256 if (rtd) { 6257 if (param_len != 0) { 6258 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6259 /*command*/ 1, /*field*/ 0, 6260 /*bit_valid*/ 0, /*bit*/ 0); 6261 ctl_done((union ctl_io *)ctsio); 6262 return (CTL_RETVAL_COMPLETE); 6263 } 6264 6265 /* Revert to defaults. */ 6266 ctl_init_page_index(lun); 6267 mtx_lock(&lun->lun_lock); 6268 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6269 mtx_unlock(&lun->lun_lock); 6270 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6271 ctl_isc_announce_mode(lun, -1, 6272 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6273 lun->mode_pages.index[i].subpage); 6274 } 6275 ctl_set_success(ctsio); 6276 ctl_done((union ctl_io *)ctsio); 6277 return (CTL_RETVAL_COMPLETE); 6278 } 6279 6280 /* 6281 * From SPC-3: 6282 * "A parameter list length of zero indicates that the Data-Out Buffer 6283 * shall be empty. This condition shall not be considered as an error." 6284 */ 6285 if (param_len == 0) { 6286 ctl_set_success(ctsio); 6287 ctl_done((union ctl_io *)ctsio); 6288 return (CTL_RETVAL_COMPLETE); 6289 } 6290 6291 /* 6292 * Since we'll hit this the first time through, prior to 6293 * allocation, we don't need to free a data buffer here. 6294 */ 6295 if (param_len < header_size) { 6296 ctl_set_param_len_error(ctsio); 6297 ctl_done((union ctl_io *)ctsio); 6298 return (CTL_RETVAL_COMPLETE); 6299 } 6300 6301 /* 6302 * Allocate the data buffer and grab the user's data. In theory, 6303 * we shouldn't have to sanity check the parameter list length here 6304 * because the maximum size is 64K. We should be able to malloc 6305 * that much without too many problems. 6306 */ 6307 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6308 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6309 ctsio->kern_data_len = param_len; 6310 ctsio->kern_total_len = param_len; 6311 ctsio->kern_rel_offset = 0; 6312 ctsio->kern_sg_entries = 0; 6313 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6314 ctsio->be_move_done = ctl_config_move_done; 6315 ctl_datamove((union ctl_io *)ctsio); 6316 6317 return (CTL_RETVAL_COMPLETE); 6318 } 6319 6320 switch (ctsio->cdb[0]) { 6321 case MODE_SELECT_6: { 6322 struct scsi_mode_header_6 *mh6; 6323 6324 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6325 bd_len = mh6->blk_desc_len; 6326 break; 6327 } 6328 case MODE_SELECT_10: { 6329 struct scsi_mode_header_10 *mh10; 6330 6331 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6332 bd_len = scsi_2btoul(mh10->blk_desc_len); 6333 break; 6334 } 6335 default: 6336 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6337 } 6338 6339 if (param_len < (header_size + bd_len)) { 6340 free(ctsio->kern_data_ptr, M_CTL); 6341 ctl_set_param_len_error(ctsio); 6342 ctl_done((union ctl_io *)ctsio); 6343 return (CTL_RETVAL_COMPLETE); 6344 } 6345 6346 /* 6347 * Set the IO_CONT flag, so that if this I/O gets passed to 6348 * ctl_config_write_done(), it'll get passed back to 6349 * ctl_do_mode_select() for further processing, or completion if 6350 * we're all done. 6351 */ 6352 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6353 ctsio->io_cont = ctl_do_mode_select; 6354 6355 modepage_info = (union ctl_modepage_info *) 6356 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6357 memset(modepage_info, 0, sizeof(*modepage_info)); 6358 modepage_info->header.len_left = param_len - header_size - bd_len; 6359 modepage_info->header.len_used = header_size + bd_len; 6360 6361 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6362 } 6363 6364 int 6365 ctl_mode_sense(struct ctl_scsiio *ctsio) 6366 { 6367 struct ctl_lun *lun = CTL_LUN(ctsio); 6368 int pc, page_code, dbd, subpage; 6369 int alloc_len, page_len, header_len, total_len; 6370 struct scsi_mode_block_descr *block_desc; 6371 struct ctl_page_index *page_index; 6372 6373 dbd = 0; 6374 block_desc = NULL; 6375 6376 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6377 6378 switch (ctsio->cdb[0]) { 6379 case MODE_SENSE_6: { 6380 struct scsi_mode_sense_6 *cdb; 6381 6382 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6383 6384 header_len = sizeof(struct scsi_mode_hdr_6); 6385 if (cdb->byte2 & SMS_DBD) 6386 dbd = 1; 6387 else 6388 header_len += sizeof(struct scsi_mode_block_descr); 6389 6390 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6391 page_code = cdb->page & SMS_PAGE_CODE; 6392 subpage = cdb->subpage; 6393 alloc_len = cdb->length; 6394 break; 6395 } 6396 case MODE_SENSE_10: { 6397 struct scsi_mode_sense_10 *cdb; 6398 6399 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6400 6401 header_len = sizeof(struct scsi_mode_hdr_10); 6402 6403 if (cdb->byte2 & SMS_DBD) 6404 dbd = 1; 6405 else 6406 header_len += sizeof(struct scsi_mode_block_descr); 6407 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6408 page_code = cdb->page & SMS_PAGE_CODE; 6409 subpage = cdb->subpage; 6410 alloc_len = scsi_2btoul(cdb->length); 6411 break; 6412 } 6413 default: 6414 ctl_set_invalid_opcode(ctsio); 6415 ctl_done((union ctl_io *)ctsio); 6416 return (CTL_RETVAL_COMPLETE); 6417 break; /* NOTREACHED */ 6418 } 6419 6420 /* 6421 * We have to make a first pass through to calculate the size of 6422 * the pages that match the user's query. Then we allocate enough 6423 * memory to hold it, and actually copy the data into the buffer. 6424 */ 6425 switch (page_code) { 6426 case SMS_ALL_PAGES_PAGE: { 6427 u_int i; 6428 6429 page_len = 0; 6430 6431 /* 6432 * At the moment, values other than 0 and 0xff here are 6433 * reserved according to SPC-3. 6434 */ 6435 if ((subpage != SMS_SUBPAGE_PAGE_0) 6436 && (subpage != SMS_SUBPAGE_ALL)) { 6437 ctl_set_invalid_field(ctsio, 6438 /*sks_valid*/ 1, 6439 /*command*/ 1, 6440 /*field*/ 3, 6441 /*bit_valid*/ 0, 6442 /*bit*/ 0); 6443 ctl_done((union ctl_io *)ctsio); 6444 return (CTL_RETVAL_COMPLETE); 6445 } 6446 6447 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6448 page_index = &lun->mode_pages.index[i]; 6449 6450 /* Make sure the page is supported for this dev type */ 6451 if (lun->be_lun->lun_type == T_DIRECT && 6452 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6453 continue; 6454 if (lun->be_lun->lun_type == T_PROCESSOR && 6455 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6456 continue; 6457 if (lun->be_lun->lun_type == T_CDROM && 6458 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6459 continue; 6460 6461 /* 6462 * We don't use this subpage if the user didn't 6463 * request all subpages. 6464 */ 6465 if ((page_index->subpage != 0) 6466 && (subpage == SMS_SUBPAGE_PAGE_0)) 6467 continue; 6468 6469 #if 0 6470 printf("found page %#x len %d\n", 6471 page_index->page_code & SMPH_PC_MASK, 6472 page_index->page_len); 6473 #endif 6474 page_len += page_index->page_len; 6475 } 6476 break; 6477 } 6478 default: { 6479 u_int i; 6480 6481 page_len = 0; 6482 6483 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6484 page_index = &lun->mode_pages.index[i]; 6485 6486 /* Make sure the page is supported for this dev type */ 6487 if (lun->be_lun->lun_type == T_DIRECT && 6488 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6489 continue; 6490 if (lun->be_lun->lun_type == T_PROCESSOR && 6491 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6492 continue; 6493 if (lun->be_lun->lun_type == T_CDROM && 6494 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6495 continue; 6496 6497 /* Look for the right page code */ 6498 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6499 continue; 6500 6501 /* Look for the right subpage or the subpage wildcard*/ 6502 if ((page_index->subpage != subpage) 6503 && (subpage != SMS_SUBPAGE_ALL)) 6504 continue; 6505 6506 #if 0 6507 printf("found page %#x len %d\n", 6508 page_index->page_code & SMPH_PC_MASK, 6509 page_index->page_len); 6510 #endif 6511 6512 page_len += page_index->page_len; 6513 } 6514 6515 if (page_len == 0) { 6516 ctl_set_invalid_field(ctsio, 6517 /*sks_valid*/ 1, 6518 /*command*/ 1, 6519 /*field*/ 2, 6520 /*bit_valid*/ 1, 6521 /*bit*/ 5); 6522 ctl_done((union ctl_io *)ctsio); 6523 return (CTL_RETVAL_COMPLETE); 6524 } 6525 break; 6526 } 6527 } 6528 6529 total_len = header_len + page_len; 6530 #if 0 6531 printf("header_len = %d, page_len = %d, total_len = %d\n", 6532 header_len, page_len, total_len); 6533 #endif 6534 6535 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6536 ctsio->kern_sg_entries = 0; 6537 ctsio->kern_rel_offset = 0; 6538 ctsio->kern_data_len = min(total_len, alloc_len); 6539 ctsio->kern_total_len = ctsio->kern_data_len; 6540 6541 switch (ctsio->cdb[0]) { 6542 case MODE_SENSE_6: { 6543 struct scsi_mode_hdr_6 *header; 6544 6545 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6546 6547 header->datalen = MIN(total_len - 1, 254); 6548 if (lun->be_lun->lun_type == T_DIRECT) { 6549 header->dev_specific = 0x10; /* DPOFUA */ 6550 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6551 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6552 header->dev_specific |= 0x80; /* WP */ 6553 } 6554 if (dbd) 6555 header->block_descr_len = 0; 6556 else 6557 header->block_descr_len = 6558 sizeof(struct scsi_mode_block_descr); 6559 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6560 break; 6561 } 6562 case MODE_SENSE_10: { 6563 struct scsi_mode_hdr_10 *header; 6564 int datalen; 6565 6566 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6567 6568 datalen = MIN(total_len - 2, 65533); 6569 scsi_ulto2b(datalen, header->datalen); 6570 if (lun->be_lun->lun_type == T_DIRECT) { 6571 header->dev_specific = 0x10; /* DPOFUA */ 6572 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6573 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6574 header->dev_specific |= 0x80; /* WP */ 6575 } 6576 if (dbd) 6577 scsi_ulto2b(0, header->block_descr_len); 6578 else 6579 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6580 header->block_descr_len); 6581 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6582 break; 6583 } 6584 default: 6585 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6586 } 6587 6588 /* 6589 * If we've got a disk, use its blocksize in the block 6590 * descriptor. Otherwise, just set it to 0. 6591 */ 6592 if (dbd == 0) { 6593 if (lun->be_lun->lun_type == T_DIRECT) 6594 scsi_ulto3b(lun->be_lun->blocksize, 6595 block_desc->block_len); 6596 else 6597 scsi_ulto3b(0, block_desc->block_len); 6598 } 6599 6600 switch (page_code) { 6601 case SMS_ALL_PAGES_PAGE: { 6602 int i, data_used; 6603 6604 data_used = header_len; 6605 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6606 struct ctl_page_index *page_index; 6607 6608 page_index = &lun->mode_pages.index[i]; 6609 if (lun->be_lun->lun_type == T_DIRECT && 6610 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6611 continue; 6612 if (lun->be_lun->lun_type == T_PROCESSOR && 6613 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6614 continue; 6615 if (lun->be_lun->lun_type == T_CDROM && 6616 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6617 continue; 6618 6619 /* 6620 * We don't use this subpage if the user didn't 6621 * request all subpages. We already checked (above) 6622 * to make sure the user only specified a subpage 6623 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6624 */ 6625 if ((page_index->subpage != 0) 6626 && (subpage == SMS_SUBPAGE_PAGE_0)) 6627 continue; 6628 6629 /* 6630 * Call the handler, if it exists, to update the 6631 * page to the latest values. 6632 */ 6633 if (page_index->sense_handler != NULL) 6634 page_index->sense_handler(ctsio, page_index,pc); 6635 6636 memcpy(ctsio->kern_data_ptr + data_used, 6637 page_index->page_data + 6638 (page_index->page_len * pc), 6639 page_index->page_len); 6640 data_used += page_index->page_len; 6641 } 6642 break; 6643 } 6644 default: { 6645 int i, data_used; 6646 6647 data_used = header_len; 6648 6649 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6650 struct ctl_page_index *page_index; 6651 6652 page_index = &lun->mode_pages.index[i]; 6653 6654 /* Look for the right page code */ 6655 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6656 continue; 6657 6658 /* Look for the right subpage or the subpage wildcard*/ 6659 if ((page_index->subpage != subpage) 6660 && (subpage != SMS_SUBPAGE_ALL)) 6661 continue; 6662 6663 /* Make sure the page is supported for this dev type */ 6664 if (lun->be_lun->lun_type == T_DIRECT && 6665 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6666 continue; 6667 if (lun->be_lun->lun_type == T_PROCESSOR && 6668 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6669 continue; 6670 if (lun->be_lun->lun_type == T_CDROM && 6671 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6672 continue; 6673 6674 /* 6675 * Call the handler, if it exists, to update the 6676 * page to the latest values. 6677 */ 6678 if (page_index->sense_handler != NULL) 6679 page_index->sense_handler(ctsio, page_index,pc); 6680 6681 memcpy(ctsio->kern_data_ptr + data_used, 6682 page_index->page_data + 6683 (page_index->page_len * pc), 6684 page_index->page_len); 6685 data_used += page_index->page_len; 6686 } 6687 break; 6688 } 6689 } 6690 6691 ctl_set_success(ctsio); 6692 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6693 ctsio->be_move_done = ctl_config_move_done; 6694 ctl_datamove((union ctl_io *)ctsio); 6695 return (CTL_RETVAL_COMPLETE); 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 6823 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6824 6825 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6826 data->hdr.param_control = SLP_LBIN; 6827 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6828 sizeof(struct scsi_log_param_header); 6829 data->ie_asc = lun->ie_asc; 6830 data->ie_ascq = lun->ie_ascq; 6831 data->temperature = 0xff; 6832 return (0); 6833 } 6834 6835 int 6836 ctl_log_sense(struct ctl_scsiio *ctsio) 6837 { 6838 struct ctl_lun *lun = CTL_LUN(ctsio); 6839 int i, pc, page_code, subpage; 6840 int alloc_len, total_len; 6841 struct ctl_page_index *page_index; 6842 struct scsi_log_sense *cdb; 6843 struct scsi_log_header *header; 6844 6845 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6846 6847 cdb = (struct scsi_log_sense *)ctsio->cdb; 6848 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6849 page_code = cdb->page & SLS_PAGE_CODE; 6850 subpage = cdb->subpage; 6851 alloc_len = scsi_2btoul(cdb->length); 6852 6853 page_index = NULL; 6854 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6855 page_index = &lun->log_pages.index[i]; 6856 6857 /* Look for the right page code */ 6858 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6859 continue; 6860 6861 /* Look for the right subpage or the subpage wildcard*/ 6862 if (page_index->subpage != subpage) 6863 continue; 6864 6865 break; 6866 } 6867 if (i >= CTL_NUM_LOG_PAGES) { 6868 ctl_set_invalid_field(ctsio, 6869 /*sks_valid*/ 1, 6870 /*command*/ 1, 6871 /*field*/ 2, 6872 /*bit_valid*/ 0, 6873 /*bit*/ 0); 6874 ctl_done((union ctl_io *)ctsio); 6875 return (CTL_RETVAL_COMPLETE); 6876 } 6877 6878 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6879 6880 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6881 ctsio->kern_sg_entries = 0; 6882 ctsio->kern_rel_offset = 0; 6883 ctsio->kern_data_len = min(total_len, alloc_len); 6884 ctsio->kern_total_len = ctsio->kern_data_len; 6885 6886 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6887 header->page = page_index->page_code; 6888 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6889 header->page |= SL_DS; 6890 if (page_index->subpage) { 6891 header->page |= SL_SPF; 6892 header->subpage = page_index->subpage; 6893 } 6894 scsi_ulto2b(page_index->page_len, header->datalen); 6895 6896 /* 6897 * Call the handler, if it exists, to update the 6898 * page to the latest values. 6899 */ 6900 if (page_index->sense_handler != NULL) 6901 page_index->sense_handler(ctsio, page_index, pc); 6902 6903 memcpy(header + 1, page_index->page_data, page_index->page_len); 6904 6905 ctl_set_success(ctsio); 6906 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6907 ctsio->be_move_done = ctl_config_move_done; 6908 ctl_datamove((union ctl_io *)ctsio); 6909 return (CTL_RETVAL_COMPLETE); 6910 } 6911 6912 int 6913 ctl_read_capacity(struct ctl_scsiio *ctsio) 6914 { 6915 struct ctl_lun *lun = CTL_LUN(ctsio); 6916 struct scsi_read_capacity *cdb; 6917 struct scsi_read_capacity_data *data; 6918 uint32_t lba; 6919 6920 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6921 6922 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6923 6924 lba = scsi_4btoul(cdb->addr); 6925 if (((cdb->pmi & SRC_PMI) == 0) 6926 && (lba != 0)) { 6927 ctl_set_invalid_field(/*ctsio*/ ctsio, 6928 /*sks_valid*/ 1, 6929 /*command*/ 1, 6930 /*field*/ 2, 6931 /*bit_valid*/ 0, 6932 /*bit*/ 0); 6933 ctl_done((union ctl_io *)ctsio); 6934 return (CTL_RETVAL_COMPLETE); 6935 } 6936 6937 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6938 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6939 ctsio->kern_data_len = sizeof(*data); 6940 ctsio->kern_total_len = sizeof(*data); 6941 ctsio->kern_rel_offset = 0; 6942 ctsio->kern_sg_entries = 0; 6943 6944 /* 6945 * If the maximum LBA is greater than 0xfffffffe, the user must 6946 * issue a SERVICE ACTION IN (16) command, with the read capacity 6947 * serivce action set. 6948 */ 6949 if (lun->be_lun->maxlba > 0xfffffffe) 6950 scsi_ulto4b(0xffffffff, data->addr); 6951 else 6952 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6953 6954 /* 6955 * XXX KDM this may not be 512 bytes... 6956 */ 6957 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6958 6959 ctl_set_success(ctsio); 6960 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6961 ctsio->be_move_done = ctl_config_move_done; 6962 ctl_datamove((union ctl_io *)ctsio); 6963 return (CTL_RETVAL_COMPLETE); 6964 } 6965 6966 int 6967 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6968 { 6969 struct ctl_lun *lun = CTL_LUN(ctsio); 6970 struct scsi_read_capacity_16 *cdb; 6971 struct scsi_read_capacity_data_long *data; 6972 uint64_t lba; 6973 uint32_t alloc_len; 6974 6975 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6976 6977 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6978 6979 alloc_len = scsi_4btoul(cdb->alloc_len); 6980 lba = scsi_8btou64(cdb->addr); 6981 6982 if ((cdb->reladr & SRC16_PMI) 6983 && (lba != 0)) { 6984 ctl_set_invalid_field(/*ctsio*/ ctsio, 6985 /*sks_valid*/ 1, 6986 /*command*/ 1, 6987 /*field*/ 2, 6988 /*bit_valid*/ 0, 6989 /*bit*/ 0); 6990 ctl_done((union ctl_io *)ctsio); 6991 return (CTL_RETVAL_COMPLETE); 6992 } 6993 6994 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6995 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6996 ctsio->kern_rel_offset = 0; 6997 ctsio->kern_sg_entries = 0; 6998 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 6999 ctsio->kern_total_len = ctsio->kern_data_len; 7000 7001 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7002 /* XXX KDM this may not be 512 bytes... */ 7003 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7004 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7005 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7006 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7007 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7008 7009 ctl_set_success(ctsio); 7010 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7011 ctsio->be_move_done = ctl_config_move_done; 7012 ctl_datamove((union ctl_io *)ctsio); 7013 return (CTL_RETVAL_COMPLETE); 7014 } 7015 7016 int 7017 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7018 { 7019 struct ctl_lun *lun = CTL_LUN(ctsio); 7020 struct scsi_get_lba_status *cdb; 7021 struct scsi_get_lba_status_data *data; 7022 struct ctl_lba_len_flags *lbalen; 7023 uint64_t lba; 7024 uint32_t alloc_len, total_len; 7025 int retval; 7026 7027 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7028 7029 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7030 lba = scsi_8btou64(cdb->addr); 7031 alloc_len = scsi_4btoul(cdb->alloc_len); 7032 7033 if (lba > lun->be_lun->maxlba) { 7034 ctl_set_lba_out_of_range(ctsio, lba); 7035 ctl_done((union ctl_io *)ctsio); 7036 return (CTL_RETVAL_COMPLETE); 7037 } 7038 7039 total_len = sizeof(*data) + sizeof(data->descr[0]); 7040 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7041 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7042 ctsio->kern_rel_offset = 0; 7043 ctsio->kern_sg_entries = 0; 7044 ctsio->kern_data_len = min(total_len, alloc_len); 7045 ctsio->kern_total_len = ctsio->kern_data_len; 7046 7047 /* Fill dummy data in case backend can't tell anything. */ 7048 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7049 scsi_u64to8b(lba, data->descr[0].addr); 7050 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7051 data->descr[0].length); 7052 data->descr[0].status = 0; /* Mapped or unknown. */ 7053 7054 ctl_set_success(ctsio); 7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7056 ctsio->be_move_done = ctl_config_move_done; 7057 7058 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7059 lbalen->lba = lba; 7060 lbalen->len = total_len; 7061 lbalen->flags = 0; 7062 retval = lun->backend->config_read((union ctl_io *)ctsio); 7063 return (retval); 7064 } 7065 7066 int 7067 ctl_read_defect(struct ctl_scsiio *ctsio) 7068 { 7069 struct scsi_read_defect_data_10 *ccb10; 7070 struct scsi_read_defect_data_12 *ccb12; 7071 struct scsi_read_defect_data_hdr_10 *data10; 7072 struct scsi_read_defect_data_hdr_12 *data12; 7073 uint32_t alloc_len, data_len; 7074 uint8_t format; 7075 7076 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7077 7078 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7079 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7080 format = ccb10->format; 7081 alloc_len = scsi_2btoul(ccb10->alloc_length); 7082 data_len = sizeof(*data10); 7083 } else { 7084 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7085 format = ccb12->format; 7086 alloc_len = scsi_4btoul(ccb12->alloc_length); 7087 data_len = sizeof(*data12); 7088 } 7089 if (alloc_len == 0) { 7090 ctl_set_success(ctsio); 7091 ctl_done((union ctl_io *)ctsio); 7092 return (CTL_RETVAL_COMPLETE); 7093 } 7094 7095 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7096 ctsio->kern_rel_offset = 0; 7097 ctsio->kern_sg_entries = 0; 7098 ctsio->kern_data_len = min(data_len, alloc_len); 7099 ctsio->kern_total_len = ctsio->kern_data_len; 7100 7101 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7102 data10 = (struct scsi_read_defect_data_hdr_10 *) 7103 ctsio->kern_data_ptr; 7104 data10->format = format; 7105 scsi_ulto2b(0, data10->length); 7106 } else { 7107 data12 = (struct scsi_read_defect_data_hdr_12 *) 7108 ctsio->kern_data_ptr; 7109 data12->format = format; 7110 scsi_ulto2b(0, data12->generation); 7111 scsi_ulto4b(0, data12->length); 7112 } 7113 7114 ctl_set_success(ctsio); 7115 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7116 ctsio->be_move_done = ctl_config_move_done; 7117 ctl_datamove((union ctl_io *)ctsio); 7118 return (CTL_RETVAL_COMPLETE); 7119 } 7120 7121 int 7122 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7123 { 7124 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7125 struct ctl_lun *lun = CTL_LUN(ctsio); 7126 struct scsi_maintenance_in *cdb; 7127 int retval; 7128 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7129 int num_ha_groups, num_target_ports, shared_group; 7130 struct ctl_port *port; 7131 struct scsi_target_group_data *rtg_ptr; 7132 struct scsi_target_group_data_extended *rtg_ext_ptr; 7133 struct scsi_target_port_group_descriptor *tpg_desc; 7134 7135 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7136 7137 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7138 retval = CTL_RETVAL_COMPLETE; 7139 7140 switch (cdb->byte2 & STG_PDF_MASK) { 7141 case STG_PDF_LENGTH: 7142 ext = 0; 7143 break; 7144 case STG_PDF_EXTENDED: 7145 ext = 1; 7146 break; 7147 default: 7148 ctl_set_invalid_field(/*ctsio*/ ctsio, 7149 /*sks_valid*/ 1, 7150 /*command*/ 1, 7151 /*field*/ 2, 7152 /*bit_valid*/ 1, 7153 /*bit*/ 5); 7154 ctl_done((union ctl_io *)ctsio); 7155 return(retval); 7156 } 7157 7158 num_target_ports = 0; 7159 shared_group = (softc->is_single != 0); 7160 mtx_lock(&softc->ctl_lock); 7161 STAILQ_FOREACH(port, &softc->port_list, links) { 7162 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7163 continue; 7164 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7165 continue; 7166 num_target_ports++; 7167 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7168 shared_group = 1; 7169 } 7170 mtx_unlock(&softc->ctl_lock); 7171 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7172 7173 if (ext) 7174 total_len = sizeof(struct scsi_target_group_data_extended); 7175 else 7176 total_len = sizeof(struct scsi_target_group_data); 7177 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7178 (shared_group + num_ha_groups) + 7179 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7180 7181 alloc_len = scsi_4btoul(cdb->length); 7182 7183 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7184 ctsio->kern_sg_entries = 0; 7185 ctsio->kern_rel_offset = 0; 7186 ctsio->kern_data_len = min(total_len, alloc_len); 7187 ctsio->kern_total_len = ctsio->kern_data_len; 7188 7189 if (ext) { 7190 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7191 ctsio->kern_data_ptr; 7192 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7193 rtg_ext_ptr->format_type = 0x10; 7194 rtg_ext_ptr->implicit_transition_time = 0; 7195 tpg_desc = &rtg_ext_ptr->groups[0]; 7196 } else { 7197 rtg_ptr = (struct scsi_target_group_data *) 7198 ctsio->kern_data_ptr; 7199 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7200 tpg_desc = &rtg_ptr->groups[0]; 7201 } 7202 7203 mtx_lock(&softc->ctl_lock); 7204 pg = softc->port_min / softc->port_cnt; 7205 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7206 /* Some shelf is known to be primary. */ 7207 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7208 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7209 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7210 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7211 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7212 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7213 else 7214 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7215 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7216 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7217 } else { 7218 ts = os; 7219 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7220 } 7221 } else { 7222 /* No known primary shelf. */ 7223 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7224 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7225 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7226 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7227 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7228 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7229 } else { 7230 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7231 } 7232 } 7233 if (shared_group) { 7234 tpg_desc->pref_state = ts; 7235 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7236 TPG_U_SUP | TPG_T_SUP; 7237 scsi_ulto2b(1, tpg_desc->target_port_group); 7238 tpg_desc->status = TPG_IMPLICIT; 7239 pc = 0; 7240 STAILQ_FOREACH(port, &softc->port_list, links) { 7241 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7242 continue; 7243 if (!softc->is_single && 7244 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7245 continue; 7246 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7247 continue; 7248 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7249 relative_target_port_identifier); 7250 pc++; 7251 } 7252 tpg_desc->target_port_count = pc; 7253 tpg_desc = (struct scsi_target_port_group_descriptor *) 7254 &tpg_desc->descriptors[pc]; 7255 } 7256 for (g = 0; g < num_ha_groups; g++) { 7257 tpg_desc->pref_state = (g == pg) ? ts : os; 7258 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7259 TPG_U_SUP | TPG_T_SUP; 7260 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7261 tpg_desc->status = TPG_IMPLICIT; 7262 pc = 0; 7263 STAILQ_FOREACH(port, &softc->port_list, links) { 7264 if (port->targ_port < g * softc->port_cnt || 7265 port->targ_port >= (g + 1) * softc->port_cnt) 7266 continue; 7267 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7268 continue; 7269 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7270 continue; 7271 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7272 continue; 7273 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7274 relative_target_port_identifier); 7275 pc++; 7276 } 7277 tpg_desc->target_port_count = pc; 7278 tpg_desc = (struct scsi_target_port_group_descriptor *) 7279 &tpg_desc->descriptors[pc]; 7280 } 7281 mtx_unlock(&softc->ctl_lock); 7282 7283 ctl_set_success(ctsio); 7284 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7285 ctsio->be_move_done = ctl_config_move_done; 7286 ctl_datamove((union ctl_io *)ctsio); 7287 return(retval); 7288 } 7289 7290 int 7291 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7292 { 7293 struct ctl_lun *lun = CTL_LUN(ctsio); 7294 struct scsi_report_supported_opcodes *cdb; 7295 const struct ctl_cmd_entry *entry, *sentry; 7296 struct scsi_report_supported_opcodes_all *all; 7297 struct scsi_report_supported_opcodes_descr *descr; 7298 struct scsi_report_supported_opcodes_one *one; 7299 int retval; 7300 int alloc_len, total_len; 7301 int opcode, service_action, i, j, num; 7302 7303 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7304 7305 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7306 retval = CTL_RETVAL_COMPLETE; 7307 7308 opcode = cdb->requested_opcode; 7309 service_action = scsi_2btoul(cdb->requested_service_action); 7310 switch (cdb->options & RSO_OPTIONS_MASK) { 7311 case RSO_OPTIONS_ALL: 7312 num = 0; 7313 for (i = 0; i < 256; i++) { 7314 entry = &ctl_cmd_table[i]; 7315 if (entry->flags & CTL_CMD_FLAG_SA5) { 7316 for (j = 0; j < 32; j++) { 7317 sentry = &((const struct ctl_cmd_entry *) 7318 entry->execute)[j]; 7319 if (ctl_cmd_applicable( 7320 lun->be_lun->lun_type, sentry)) 7321 num++; 7322 } 7323 } else { 7324 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7325 entry)) 7326 num++; 7327 } 7328 } 7329 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7330 num * sizeof(struct scsi_report_supported_opcodes_descr); 7331 break; 7332 case RSO_OPTIONS_OC: 7333 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7334 ctl_set_invalid_field(/*ctsio*/ ctsio, 7335 /*sks_valid*/ 1, 7336 /*command*/ 1, 7337 /*field*/ 2, 7338 /*bit_valid*/ 1, 7339 /*bit*/ 2); 7340 ctl_done((union ctl_io *)ctsio); 7341 return (CTL_RETVAL_COMPLETE); 7342 } 7343 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7344 break; 7345 case RSO_OPTIONS_OC_SA: 7346 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7347 service_action >= 32) { 7348 ctl_set_invalid_field(/*ctsio*/ ctsio, 7349 /*sks_valid*/ 1, 7350 /*command*/ 1, 7351 /*field*/ 2, 7352 /*bit_valid*/ 1, 7353 /*bit*/ 2); 7354 ctl_done((union ctl_io *)ctsio); 7355 return (CTL_RETVAL_COMPLETE); 7356 } 7357 /* FALLTHROUGH */ 7358 case RSO_OPTIONS_OC_ASA: 7359 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7360 break; 7361 default: 7362 ctl_set_invalid_field(/*ctsio*/ ctsio, 7363 /*sks_valid*/ 1, 7364 /*command*/ 1, 7365 /*field*/ 2, 7366 /*bit_valid*/ 1, 7367 /*bit*/ 2); 7368 ctl_done((union ctl_io *)ctsio); 7369 return (CTL_RETVAL_COMPLETE); 7370 } 7371 7372 alloc_len = scsi_4btoul(cdb->length); 7373 7374 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7375 ctsio->kern_sg_entries = 0; 7376 ctsio->kern_rel_offset = 0; 7377 ctsio->kern_data_len = min(total_len, alloc_len); 7378 ctsio->kern_total_len = ctsio->kern_data_len; 7379 7380 switch (cdb->options & RSO_OPTIONS_MASK) { 7381 case RSO_OPTIONS_ALL: 7382 all = (struct scsi_report_supported_opcodes_all *) 7383 ctsio->kern_data_ptr; 7384 num = 0; 7385 for (i = 0; i < 256; i++) { 7386 entry = &ctl_cmd_table[i]; 7387 if (entry->flags & CTL_CMD_FLAG_SA5) { 7388 for (j = 0; j < 32; j++) { 7389 sentry = &((const struct ctl_cmd_entry *) 7390 entry->execute)[j]; 7391 if (!ctl_cmd_applicable( 7392 lun->be_lun->lun_type, sentry)) 7393 continue; 7394 descr = &all->descr[num++]; 7395 descr->opcode = i; 7396 scsi_ulto2b(j, descr->service_action); 7397 descr->flags = RSO_SERVACTV; 7398 scsi_ulto2b(sentry->length, 7399 descr->cdb_length); 7400 } 7401 } else { 7402 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7403 entry)) 7404 continue; 7405 descr = &all->descr[num++]; 7406 descr->opcode = i; 7407 scsi_ulto2b(0, descr->service_action); 7408 descr->flags = 0; 7409 scsi_ulto2b(entry->length, descr->cdb_length); 7410 } 7411 } 7412 scsi_ulto4b( 7413 num * sizeof(struct scsi_report_supported_opcodes_descr), 7414 all->length); 7415 break; 7416 case RSO_OPTIONS_OC: 7417 one = (struct scsi_report_supported_opcodes_one *) 7418 ctsio->kern_data_ptr; 7419 entry = &ctl_cmd_table[opcode]; 7420 goto fill_one; 7421 case RSO_OPTIONS_OC_SA: 7422 one = (struct scsi_report_supported_opcodes_one *) 7423 ctsio->kern_data_ptr; 7424 entry = &ctl_cmd_table[opcode]; 7425 entry = &((const struct ctl_cmd_entry *) 7426 entry->execute)[service_action]; 7427 fill_one: 7428 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7429 one->support = 3; 7430 scsi_ulto2b(entry->length, one->cdb_length); 7431 one->cdb_usage[0] = opcode; 7432 memcpy(&one->cdb_usage[1], entry->usage, 7433 entry->length - 1); 7434 } else 7435 one->support = 1; 7436 break; 7437 case RSO_OPTIONS_OC_ASA: 7438 one = (struct scsi_report_supported_opcodes_one *) 7439 ctsio->kern_data_ptr; 7440 entry = &ctl_cmd_table[opcode]; 7441 if (entry->flags & CTL_CMD_FLAG_SA5) { 7442 entry = &((const struct ctl_cmd_entry *) 7443 entry->execute)[service_action]; 7444 } else if (service_action != 0) { 7445 one->support = 1; 7446 break; 7447 } 7448 goto fill_one; 7449 } 7450 7451 ctl_set_success(ctsio); 7452 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7453 ctsio->be_move_done = ctl_config_move_done; 7454 ctl_datamove((union ctl_io *)ctsio); 7455 return(retval); 7456 } 7457 7458 int 7459 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7460 { 7461 struct scsi_report_supported_tmf *cdb; 7462 struct scsi_report_supported_tmf_ext_data *data; 7463 int retval; 7464 int alloc_len, total_len; 7465 7466 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7467 7468 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7469 7470 retval = CTL_RETVAL_COMPLETE; 7471 7472 if (cdb->options & RST_REPD) 7473 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7474 else 7475 total_len = sizeof(struct scsi_report_supported_tmf_data); 7476 alloc_len = scsi_4btoul(cdb->length); 7477 7478 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7479 ctsio->kern_sg_entries = 0; 7480 ctsio->kern_rel_offset = 0; 7481 ctsio->kern_data_len = min(total_len, alloc_len); 7482 ctsio->kern_total_len = ctsio->kern_data_len; 7483 7484 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7485 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7486 RST_TRS; 7487 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7488 data->length = total_len - 4; 7489 7490 ctl_set_success(ctsio); 7491 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7492 ctsio->be_move_done = ctl_config_move_done; 7493 ctl_datamove((union ctl_io *)ctsio); 7494 return (retval); 7495 } 7496 7497 int 7498 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7499 { 7500 struct scsi_report_timestamp *cdb; 7501 struct scsi_report_timestamp_data *data; 7502 struct timeval tv; 7503 int64_t timestamp; 7504 int retval; 7505 int alloc_len, total_len; 7506 7507 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7508 7509 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7510 7511 retval = CTL_RETVAL_COMPLETE; 7512 7513 total_len = sizeof(struct scsi_report_timestamp_data); 7514 alloc_len = scsi_4btoul(cdb->length); 7515 7516 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7517 ctsio->kern_sg_entries = 0; 7518 ctsio->kern_rel_offset = 0; 7519 ctsio->kern_data_len = min(total_len, alloc_len); 7520 ctsio->kern_total_len = ctsio->kern_data_len; 7521 7522 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7523 scsi_ulto2b(sizeof(*data) - 2, data->length); 7524 data->origin = RTS_ORIG_OUTSIDE; 7525 getmicrotime(&tv); 7526 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7527 scsi_ulto4b(timestamp >> 16, data->timestamp); 7528 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7529 7530 ctl_set_success(ctsio); 7531 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7532 ctsio->be_move_done = ctl_config_move_done; 7533 ctl_datamove((union ctl_io *)ctsio); 7534 return (retval); 7535 } 7536 7537 int 7538 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7539 { 7540 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7541 struct ctl_lun *lun = CTL_LUN(ctsio); 7542 struct scsi_per_res_in *cdb; 7543 int alloc_len, total_len = 0; 7544 /* struct scsi_per_res_in_rsrv in_data; */ 7545 uint64_t key; 7546 7547 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7548 7549 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7550 7551 alloc_len = scsi_2btoul(cdb->length); 7552 7553 retry: 7554 mtx_lock(&lun->lun_lock); 7555 switch (cdb->action) { 7556 case SPRI_RK: /* read keys */ 7557 total_len = sizeof(struct scsi_per_res_in_keys) + 7558 lun->pr_key_count * 7559 sizeof(struct scsi_per_res_key); 7560 break; 7561 case SPRI_RR: /* read reservation */ 7562 if (lun->flags & CTL_LUN_PR_RESERVED) 7563 total_len = sizeof(struct scsi_per_res_in_rsrv); 7564 else 7565 total_len = sizeof(struct scsi_per_res_in_header); 7566 break; 7567 case SPRI_RC: /* report capabilities */ 7568 total_len = sizeof(struct scsi_per_res_cap); 7569 break; 7570 case SPRI_RS: /* read full status */ 7571 total_len = sizeof(struct scsi_per_res_in_header) + 7572 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7573 lun->pr_key_count; 7574 break; 7575 default: 7576 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7577 } 7578 mtx_unlock(&lun->lun_lock); 7579 7580 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7581 ctsio->kern_rel_offset = 0; 7582 ctsio->kern_sg_entries = 0; 7583 ctsio->kern_data_len = min(total_len, alloc_len); 7584 ctsio->kern_total_len = ctsio->kern_data_len; 7585 7586 mtx_lock(&lun->lun_lock); 7587 switch (cdb->action) { 7588 case SPRI_RK: { // read keys 7589 struct scsi_per_res_in_keys *res_keys; 7590 int i, key_count; 7591 7592 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7593 7594 /* 7595 * We had to drop the lock to allocate our buffer, which 7596 * leaves time for someone to come in with another 7597 * persistent reservation. (That is unlikely, though, 7598 * since this should be the only persistent reservation 7599 * command active right now.) 7600 */ 7601 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7602 (lun->pr_key_count * 7603 sizeof(struct scsi_per_res_key)))){ 7604 mtx_unlock(&lun->lun_lock); 7605 free(ctsio->kern_data_ptr, M_CTL); 7606 printf("%s: reservation length changed, retrying\n", 7607 __func__); 7608 goto retry; 7609 } 7610 7611 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7612 7613 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7614 lun->pr_key_count, res_keys->header.length); 7615 7616 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7617 if ((key = ctl_get_prkey(lun, i)) == 0) 7618 continue; 7619 7620 /* 7621 * We used lun->pr_key_count to calculate the 7622 * size to allocate. If it turns out the number of 7623 * initiators with the registered flag set is 7624 * larger than that (i.e. they haven't been kept in 7625 * sync), we've got a problem. 7626 */ 7627 if (key_count >= lun->pr_key_count) { 7628 key_count++; 7629 continue; 7630 } 7631 scsi_u64to8b(key, res_keys->keys[key_count].key); 7632 key_count++; 7633 } 7634 break; 7635 } 7636 case SPRI_RR: { // read reservation 7637 struct scsi_per_res_in_rsrv *res; 7638 int tmp_len, header_only; 7639 7640 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7641 7642 scsi_ulto4b(lun->pr_generation, res->header.generation); 7643 7644 if (lun->flags & CTL_LUN_PR_RESERVED) 7645 { 7646 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7647 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7648 res->header.length); 7649 header_only = 0; 7650 } else { 7651 tmp_len = sizeof(struct scsi_per_res_in_header); 7652 scsi_ulto4b(0, res->header.length); 7653 header_only = 1; 7654 } 7655 7656 /* 7657 * We had to drop the lock to allocate our buffer, which 7658 * leaves time for someone to come in with another 7659 * persistent reservation. (That is unlikely, though, 7660 * since this should be the only persistent reservation 7661 * command active right now.) 7662 */ 7663 if (tmp_len != total_len) { 7664 mtx_unlock(&lun->lun_lock); 7665 free(ctsio->kern_data_ptr, M_CTL); 7666 printf("%s: reservation status changed, retrying\n", 7667 __func__); 7668 goto retry; 7669 } 7670 7671 /* 7672 * No reservation held, so we're done. 7673 */ 7674 if (header_only != 0) 7675 break; 7676 7677 /* 7678 * If the registration is an All Registrants type, the key 7679 * is 0, since it doesn't really matter. 7680 */ 7681 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7682 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7683 res->data.reservation); 7684 } 7685 res->data.scopetype = lun->pr_res_type; 7686 break; 7687 } 7688 case SPRI_RC: //report capabilities 7689 { 7690 struct scsi_per_res_cap *res_cap; 7691 uint16_t type_mask; 7692 7693 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7694 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7695 res_cap->flags1 = SPRI_CRH; 7696 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7697 type_mask = SPRI_TM_WR_EX_AR | 7698 SPRI_TM_EX_AC_RO | 7699 SPRI_TM_WR_EX_RO | 7700 SPRI_TM_EX_AC | 7701 SPRI_TM_WR_EX | 7702 SPRI_TM_EX_AC_AR; 7703 scsi_ulto2b(type_mask, res_cap->type_mask); 7704 break; 7705 } 7706 case SPRI_RS: { // read full status 7707 struct scsi_per_res_in_full *res_status; 7708 struct scsi_per_res_in_full_desc *res_desc; 7709 struct ctl_port *port; 7710 int i, len; 7711 7712 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7713 7714 /* 7715 * We had to drop the lock to allocate our buffer, which 7716 * leaves time for someone to come in with another 7717 * persistent reservation. (That is unlikely, though, 7718 * since this should be the only persistent reservation 7719 * command active right now.) 7720 */ 7721 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7722 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7723 lun->pr_key_count)){ 7724 mtx_unlock(&lun->lun_lock); 7725 free(ctsio->kern_data_ptr, M_CTL); 7726 printf("%s: reservation length changed, retrying\n", 7727 __func__); 7728 goto retry; 7729 } 7730 7731 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7732 7733 res_desc = &res_status->desc[0]; 7734 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7735 if ((key = ctl_get_prkey(lun, i)) == 0) 7736 continue; 7737 7738 scsi_u64to8b(key, res_desc->res_key.key); 7739 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7740 (lun->pr_res_idx == i || 7741 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7742 res_desc->flags = SPRI_FULL_R_HOLDER; 7743 res_desc->scopetype = lun->pr_res_type; 7744 } 7745 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7746 res_desc->rel_trgt_port_id); 7747 len = 0; 7748 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7749 if (port != NULL) 7750 len = ctl_create_iid(port, 7751 i % CTL_MAX_INIT_PER_PORT, 7752 res_desc->transport_id); 7753 scsi_ulto4b(len, res_desc->additional_length); 7754 res_desc = (struct scsi_per_res_in_full_desc *) 7755 &res_desc->transport_id[len]; 7756 } 7757 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7758 res_status->header.length); 7759 break; 7760 } 7761 default: 7762 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7763 } 7764 mtx_unlock(&lun->lun_lock); 7765 7766 ctl_set_success(ctsio); 7767 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7768 ctsio->be_move_done = ctl_config_move_done; 7769 ctl_datamove((union ctl_io *)ctsio); 7770 return (CTL_RETVAL_COMPLETE); 7771 } 7772 7773 /* 7774 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7775 * it should return. 7776 */ 7777 static int 7778 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7779 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7780 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7781 struct scsi_per_res_out_parms* param) 7782 { 7783 union ctl_ha_msg persis_io; 7784 int i; 7785 7786 mtx_lock(&lun->lun_lock); 7787 if (sa_res_key == 0) { 7788 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7789 /* validate scope and type */ 7790 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7791 SPR_LU_SCOPE) { 7792 mtx_unlock(&lun->lun_lock); 7793 ctl_set_invalid_field(/*ctsio*/ ctsio, 7794 /*sks_valid*/ 1, 7795 /*command*/ 1, 7796 /*field*/ 2, 7797 /*bit_valid*/ 1, 7798 /*bit*/ 4); 7799 ctl_done((union ctl_io *)ctsio); 7800 return (1); 7801 } 7802 7803 if (type>8 || type==2 || type==4 || type==0) { 7804 mtx_unlock(&lun->lun_lock); 7805 ctl_set_invalid_field(/*ctsio*/ ctsio, 7806 /*sks_valid*/ 1, 7807 /*command*/ 1, 7808 /*field*/ 2, 7809 /*bit_valid*/ 1, 7810 /*bit*/ 0); 7811 ctl_done((union ctl_io *)ctsio); 7812 return (1); 7813 } 7814 7815 /* 7816 * Unregister everybody else and build UA for 7817 * them 7818 */ 7819 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7820 if (i == residx || ctl_get_prkey(lun, i) == 0) 7821 continue; 7822 7823 ctl_clr_prkey(lun, i); 7824 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7825 } 7826 lun->pr_key_count = 1; 7827 lun->pr_res_type = type; 7828 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7829 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7830 lun->pr_res_idx = residx; 7831 lun->pr_generation++; 7832 mtx_unlock(&lun->lun_lock); 7833 7834 /* send msg to other side */ 7835 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7836 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7837 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7838 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7839 persis_io.pr.pr_info.res_type = type; 7840 memcpy(persis_io.pr.pr_info.sa_res_key, 7841 param->serv_act_res_key, 7842 sizeof(param->serv_act_res_key)); 7843 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7844 sizeof(persis_io.pr), M_WAITOK); 7845 } else { 7846 /* not all registrants */ 7847 mtx_unlock(&lun->lun_lock); 7848 free(ctsio->kern_data_ptr, M_CTL); 7849 ctl_set_invalid_field(ctsio, 7850 /*sks_valid*/ 1, 7851 /*command*/ 0, 7852 /*field*/ 8, 7853 /*bit_valid*/ 0, 7854 /*bit*/ 0); 7855 ctl_done((union ctl_io *)ctsio); 7856 return (1); 7857 } 7858 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7859 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7860 int found = 0; 7861 7862 if (res_key == sa_res_key) { 7863 /* special case */ 7864 /* 7865 * The spec implies this is not good but doesn't 7866 * say what to do. There are two choices either 7867 * generate a res conflict or check condition 7868 * with illegal field in parameter data. Since 7869 * that is what is done when the sa_res_key is 7870 * zero I'll take that approach since this has 7871 * to do with the sa_res_key. 7872 */ 7873 mtx_unlock(&lun->lun_lock); 7874 free(ctsio->kern_data_ptr, M_CTL); 7875 ctl_set_invalid_field(ctsio, 7876 /*sks_valid*/ 1, 7877 /*command*/ 0, 7878 /*field*/ 8, 7879 /*bit_valid*/ 0, 7880 /*bit*/ 0); 7881 ctl_done((union ctl_io *)ctsio); 7882 return (1); 7883 } 7884 7885 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7886 if (ctl_get_prkey(lun, i) != sa_res_key) 7887 continue; 7888 7889 found = 1; 7890 ctl_clr_prkey(lun, i); 7891 lun->pr_key_count--; 7892 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7893 } 7894 if (!found) { 7895 mtx_unlock(&lun->lun_lock); 7896 free(ctsio->kern_data_ptr, M_CTL); 7897 ctl_set_reservation_conflict(ctsio); 7898 ctl_done((union ctl_io *)ctsio); 7899 return (CTL_RETVAL_COMPLETE); 7900 } 7901 lun->pr_generation++; 7902 mtx_unlock(&lun->lun_lock); 7903 7904 /* send msg to other side */ 7905 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7906 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7907 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7908 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7909 persis_io.pr.pr_info.res_type = type; 7910 memcpy(persis_io.pr.pr_info.sa_res_key, 7911 param->serv_act_res_key, 7912 sizeof(param->serv_act_res_key)); 7913 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7914 sizeof(persis_io.pr), M_WAITOK); 7915 } else { 7916 /* Reserved but not all registrants */ 7917 /* sa_res_key is res holder */ 7918 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7919 /* validate scope and type */ 7920 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7921 SPR_LU_SCOPE) { 7922 mtx_unlock(&lun->lun_lock); 7923 ctl_set_invalid_field(/*ctsio*/ ctsio, 7924 /*sks_valid*/ 1, 7925 /*command*/ 1, 7926 /*field*/ 2, 7927 /*bit_valid*/ 1, 7928 /*bit*/ 4); 7929 ctl_done((union ctl_io *)ctsio); 7930 return (1); 7931 } 7932 7933 if (type>8 || type==2 || type==4 || type==0) { 7934 mtx_unlock(&lun->lun_lock); 7935 ctl_set_invalid_field(/*ctsio*/ ctsio, 7936 /*sks_valid*/ 1, 7937 /*command*/ 1, 7938 /*field*/ 2, 7939 /*bit_valid*/ 1, 7940 /*bit*/ 0); 7941 ctl_done((union ctl_io *)ctsio); 7942 return (1); 7943 } 7944 7945 /* 7946 * Do the following: 7947 * if sa_res_key != res_key remove all 7948 * registrants w/sa_res_key and generate UA 7949 * for these registrants(Registrations 7950 * Preempted) if it wasn't an exclusive 7951 * reservation generate UA(Reservations 7952 * Preempted) for all other registered nexuses 7953 * if the type has changed. Establish the new 7954 * reservation and holder. If res_key and 7955 * sa_res_key are the same do the above 7956 * except don't unregister the res holder. 7957 */ 7958 7959 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7960 if (i == residx || ctl_get_prkey(lun, i) == 0) 7961 continue; 7962 7963 if (sa_res_key == ctl_get_prkey(lun, i)) { 7964 ctl_clr_prkey(lun, i); 7965 lun->pr_key_count--; 7966 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7967 } else if (type != lun->pr_res_type && 7968 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 7969 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 7970 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7971 } 7972 } 7973 lun->pr_res_type = type; 7974 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7975 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7976 lun->pr_res_idx = residx; 7977 else 7978 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7979 lun->pr_generation++; 7980 mtx_unlock(&lun->lun_lock); 7981 7982 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7983 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7984 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7985 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7986 persis_io.pr.pr_info.res_type = type; 7987 memcpy(persis_io.pr.pr_info.sa_res_key, 7988 param->serv_act_res_key, 7989 sizeof(param->serv_act_res_key)); 7990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7991 sizeof(persis_io.pr), M_WAITOK); 7992 } else { 7993 /* 7994 * sa_res_key is not the res holder just 7995 * remove registrants 7996 */ 7997 int found=0; 7998 7999 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8000 if (sa_res_key != ctl_get_prkey(lun, i)) 8001 continue; 8002 8003 found = 1; 8004 ctl_clr_prkey(lun, i); 8005 lun->pr_key_count--; 8006 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8007 } 8008 8009 if (!found) { 8010 mtx_unlock(&lun->lun_lock); 8011 free(ctsio->kern_data_ptr, M_CTL); 8012 ctl_set_reservation_conflict(ctsio); 8013 ctl_done((union ctl_io *)ctsio); 8014 return (1); 8015 } 8016 lun->pr_generation++; 8017 mtx_unlock(&lun->lun_lock); 8018 8019 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8020 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8021 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8022 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8023 persis_io.pr.pr_info.res_type = type; 8024 memcpy(persis_io.pr.pr_info.sa_res_key, 8025 param->serv_act_res_key, 8026 sizeof(param->serv_act_res_key)); 8027 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8028 sizeof(persis_io.pr), M_WAITOK); 8029 } 8030 } 8031 return (0); 8032 } 8033 8034 static void 8035 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8036 { 8037 uint64_t sa_res_key; 8038 int i; 8039 8040 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8041 8042 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8043 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8044 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8045 if (sa_res_key == 0) { 8046 /* 8047 * Unregister everybody else and build UA for 8048 * them 8049 */ 8050 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8051 if (i == msg->pr.pr_info.residx || 8052 ctl_get_prkey(lun, i) == 0) 8053 continue; 8054 8055 ctl_clr_prkey(lun, i); 8056 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8057 } 8058 8059 lun->pr_key_count = 1; 8060 lun->pr_res_type = msg->pr.pr_info.res_type; 8061 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8062 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8063 lun->pr_res_idx = msg->pr.pr_info.residx; 8064 } else { 8065 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8066 if (sa_res_key == ctl_get_prkey(lun, i)) 8067 continue; 8068 8069 ctl_clr_prkey(lun, i); 8070 lun->pr_key_count--; 8071 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8072 } 8073 } 8074 } else { 8075 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8076 if (i == msg->pr.pr_info.residx || 8077 ctl_get_prkey(lun, i) == 0) 8078 continue; 8079 8080 if (sa_res_key == ctl_get_prkey(lun, i)) { 8081 ctl_clr_prkey(lun, i); 8082 lun->pr_key_count--; 8083 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8084 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8085 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8086 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8087 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8088 } 8089 } 8090 lun->pr_res_type = msg->pr.pr_info.res_type; 8091 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8092 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8093 lun->pr_res_idx = msg->pr.pr_info.residx; 8094 else 8095 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8096 } 8097 lun->pr_generation++; 8098 8099 } 8100 8101 8102 int 8103 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8104 { 8105 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8106 struct ctl_lun *lun = CTL_LUN(ctsio); 8107 int retval; 8108 u_int32_t param_len; 8109 struct scsi_per_res_out *cdb; 8110 struct scsi_per_res_out_parms* param; 8111 uint32_t residx; 8112 uint64_t res_key, sa_res_key, key; 8113 uint8_t type; 8114 union ctl_ha_msg persis_io; 8115 int i; 8116 8117 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8118 8119 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8120 retval = CTL_RETVAL_COMPLETE; 8121 8122 /* 8123 * We only support whole-LUN scope. The scope & type are ignored for 8124 * register, register and ignore existing key and clear. 8125 * We sometimes ignore scope and type on preempts too!! 8126 * Verify reservation type here as well. 8127 */ 8128 type = cdb->scope_type & SPR_TYPE_MASK; 8129 if ((cdb->action == SPRO_RESERVE) 8130 || (cdb->action == SPRO_RELEASE)) { 8131 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8132 ctl_set_invalid_field(/*ctsio*/ ctsio, 8133 /*sks_valid*/ 1, 8134 /*command*/ 1, 8135 /*field*/ 2, 8136 /*bit_valid*/ 1, 8137 /*bit*/ 4); 8138 ctl_done((union ctl_io *)ctsio); 8139 return (CTL_RETVAL_COMPLETE); 8140 } 8141 8142 if (type>8 || type==2 || type==4 || type==0) { 8143 ctl_set_invalid_field(/*ctsio*/ ctsio, 8144 /*sks_valid*/ 1, 8145 /*command*/ 1, 8146 /*field*/ 2, 8147 /*bit_valid*/ 1, 8148 /*bit*/ 0); 8149 ctl_done((union ctl_io *)ctsio); 8150 return (CTL_RETVAL_COMPLETE); 8151 } 8152 } 8153 8154 param_len = scsi_4btoul(cdb->length); 8155 8156 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8157 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8158 ctsio->kern_data_len = param_len; 8159 ctsio->kern_total_len = param_len; 8160 ctsio->kern_rel_offset = 0; 8161 ctsio->kern_sg_entries = 0; 8162 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8163 ctsio->be_move_done = ctl_config_move_done; 8164 ctl_datamove((union ctl_io *)ctsio); 8165 8166 return (CTL_RETVAL_COMPLETE); 8167 } 8168 8169 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8170 8171 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8172 res_key = scsi_8btou64(param->res_key.key); 8173 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8174 8175 /* 8176 * Validate the reservation key here except for SPRO_REG_IGNO 8177 * This must be done for all other service actions 8178 */ 8179 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8180 mtx_lock(&lun->lun_lock); 8181 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8182 if (res_key != key) { 8183 /* 8184 * The current key passed in doesn't match 8185 * the one the initiator previously 8186 * registered. 8187 */ 8188 mtx_unlock(&lun->lun_lock); 8189 free(ctsio->kern_data_ptr, M_CTL); 8190 ctl_set_reservation_conflict(ctsio); 8191 ctl_done((union ctl_io *)ctsio); 8192 return (CTL_RETVAL_COMPLETE); 8193 } 8194 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8195 /* 8196 * We are not registered 8197 */ 8198 mtx_unlock(&lun->lun_lock); 8199 free(ctsio->kern_data_ptr, M_CTL); 8200 ctl_set_reservation_conflict(ctsio); 8201 ctl_done((union ctl_io *)ctsio); 8202 return (CTL_RETVAL_COMPLETE); 8203 } else if (res_key != 0) { 8204 /* 8205 * We are not registered and trying to register but 8206 * the register key isn't zero. 8207 */ 8208 mtx_unlock(&lun->lun_lock); 8209 free(ctsio->kern_data_ptr, M_CTL); 8210 ctl_set_reservation_conflict(ctsio); 8211 ctl_done((union ctl_io *)ctsio); 8212 return (CTL_RETVAL_COMPLETE); 8213 } 8214 mtx_unlock(&lun->lun_lock); 8215 } 8216 8217 switch (cdb->action & SPRO_ACTION_MASK) { 8218 case SPRO_REGISTER: 8219 case SPRO_REG_IGNO: { 8220 8221 #if 0 8222 printf("Registration received\n"); 8223 #endif 8224 8225 /* 8226 * We don't support any of these options, as we report in 8227 * the read capabilities request (see 8228 * ctl_persistent_reserve_in(), above). 8229 */ 8230 if ((param->flags & SPR_SPEC_I_PT) 8231 || (param->flags & SPR_ALL_TG_PT) 8232 || (param->flags & SPR_APTPL)) { 8233 int bit_ptr; 8234 8235 if (param->flags & SPR_APTPL) 8236 bit_ptr = 0; 8237 else if (param->flags & SPR_ALL_TG_PT) 8238 bit_ptr = 2; 8239 else /* SPR_SPEC_I_PT */ 8240 bit_ptr = 3; 8241 8242 free(ctsio->kern_data_ptr, M_CTL); 8243 ctl_set_invalid_field(ctsio, 8244 /*sks_valid*/ 1, 8245 /*command*/ 0, 8246 /*field*/ 20, 8247 /*bit_valid*/ 1, 8248 /*bit*/ bit_ptr); 8249 ctl_done((union ctl_io *)ctsio); 8250 return (CTL_RETVAL_COMPLETE); 8251 } 8252 8253 mtx_lock(&lun->lun_lock); 8254 8255 /* 8256 * The initiator wants to clear the 8257 * key/unregister. 8258 */ 8259 if (sa_res_key == 0) { 8260 if ((res_key == 0 8261 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8262 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8263 && ctl_get_prkey(lun, residx) == 0)) { 8264 mtx_unlock(&lun->lun_lock); 8265 goto done; 8266 } 8267 8268 ctl_clr_prkey(lun, residx); 8269 lun->pr_key_count--; 8270 8271 if (residx == lun->pr_res_idx) { 8272 lun->flags &= ~CTL_LUN_PR_RESERVED; 8273 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8274 8275 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8276 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8277 lun->pr_key_count) { 8278 /* 8279 * If the reservation is a registrants 8280 * only type we need to generate a UA 8281 * for other registered inits. The 8282 * sense code should be RESERVATIONS 8283 * RELEASED 8284 */ 8285 8286 for (i = softc->init_min; i < softc->init_max; i++){ 8287 if (ctl_get_prkey(lun, i) == 0) 8288 continue; 8289 ctl_est_ua(lun, i, 8290 CTL_UA_RES_RELEASE); 8291 } 8292 } 8293 lun->pr_res_type = 0; 8294 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8295 if (lun->pr_key_count==0) { 8296 lun->flags &= ~CTL_LUN_PR_RESERVED; 8297 lun->pr_res_type = 0; 8298 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8299 } 8300 } 8301 lun->pr_generation++; 8302 mtx_unlock(&lun->lun_lock); 8303 8304 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8305 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8306 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8307 persis_io.pr.pr_info.residx = residx; 8308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8309 sizeof(persis_io.pr), M_WAITOK); 8310 } else /* sa_res_key != 0 */ { 8311 8312 /* 8313 * If we aren't registered currently then increment 8314 * the key count and set the registered flag. 8315 */ 8316 ctl_alloc_prkey(lun, residx); 8317 if (ctl_get_prkey(lun, residx) == 0) 8318 lun->pr_key_count++; 8319 ctl_set_prkey(lun, residx, sa_res_key); 8320 lun->pr_generation++; 8321 mtx_unlock(&lun->lun_lock); 8322 8323 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8324 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8325 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8326 persis_io.pr.pr_info.residx = residx; 8327 memcpy(persis_io.pr.pr_info.sa_res_key, 8328 param->serv_act_res_key, 8329 sizeof(param->serv_act_res_key)); 8330 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8331 sizeof(persis_io.pr), M_WAITOK); 8332 } 8333 8334 break; 8335 } 8336 case SPRO_RESERVE: 8337 #if 0 8338 printf("Reserve executed type %d\n", type); 8339 #endif 8340 mtx_lock(&lun->lun_lock); 8341 if (lun->flags & CTL_LUN_PR_RESERVED) { 8342 /* 8343 * if this isn't the reservation holder and it's 8344 * not a "all registrants" type or if the type is 8345 * different then we have a conflict 8346 */ 8347 if ((lun->pr_res_idx != residx 8348 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8349 || lun->pr_res_type != type) { 8350 mtx_unlock(&lun->lun_lock); 8351 free(ctsio->kern_data_ptr, M_CTL); 8352 ctl_set_reservation_conflict(ctsio); 8353 ctl_done((union ctl_io *)ctsio); 8354 return (CTL_RETVAL_COMPLETE); 8355 } 8356 mtx_unlock(&lun->lun_lock); 8357 } else /* create a reservation */ { 8358 /* 8359 * If it's not an "all registrants" type record 8360 * reservation holder 8361 */ 8362 if (type != SPR_TYPE_WR_EX_AR 8363 && type != SPR_TYPE_EX_AC_AR) 8364 lun->pr_res_idx = residx; /* Res holder */ 8365 else 8366 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8367 8368 lun->flags |= CTL_LUN_PR_RESERVED; 8369 lun->pr_res_type = type; 8370 8371 mtx_unlock(&lun->lun_lock); 8372 8373 /* send msg to other side */ 8374 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8375 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8376 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8377 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8378 persis_io.pr.pr_info.res_type = type; 8379 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8380 sizeof(persis_io.pr), M_WAITOK); 8381 } 8382 break; 8383 8384 case SPRO_RELEASE: 8385 mtx_lock(&lun->lun_lock); 8386 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8387 /* No reservation exists return good status */ 8388 mtx_unlock(&lun->lun_lock); 8389 goto done; 8390 } 8391 /* 8392 * Is this nexus a reservation holder? 8393 */ 8394 if (lun->pr_res_idx != residx 8395 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8396 /* 8397 * not a res holder return good status but 8398 * do nothing 8399 */ 8400 mtx_unlock(&lun->lun_lock); 8401 goto done; 8402 } 8403 8404 if (lun->pr_res_type != type) { 8405 mtx_unlock(&lun->lun_lock); 8406 free(ctsio->kern_data_ptr, M_CTL); 8407 ctl_set_illegal_pr_release(ctsio); 8408 ctl_done((union ctl_io *)ctsio); 8409 return (CTL_RETVAL_COMPLETE); 8410 } 8411 8412 /* okay to release */ 8413 lun->flags &= ~CTL_LUN_PR_RESERVED; 8414 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8415 lun->pr_res_type = 0; 8416 8417 /* 8418 * If this isn't an exclusive access reservation and NUAR 8419 * is not set, generate UA for all other registrants. 8420 */ 8421 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8422 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8423 for (i = softc->init_min; i < softc->init_max; i++) { 8424 if (i == residx || ctl_get_prkey(lun, i) == 0) 8425 continue; 8426 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8427 } 8428 } 8429 mtx_unlock(&lun->lun_lock); 8430 8431 /* Send msg to other side */ 8432 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8433 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8434 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8435 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8436 sizeof(persis_io.pr), M_WAITOK); 8437 break; 8438 8439 case SPRO_CLEAR: 8440 /* send msg to other side */ 8441 8442 mtx_lock(&lun->lun_lock); 8443 lun->flags &= ~CTL_LUN_PR_RESERVED; 8444 lun->pr_res_type = 0; 8445 lun->pr_key_count = 0; 8446 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8447 8448 ctl_clr_prkey(lun, residx); 8449 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8450 if (ctl_get_prkey(lun, i) != 0) { 8451 ctl_clr_prkey(lun, i); 8452 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8453 } 8454 lun->pr_generation++; 8455 mtx_unlock(&lun->lun_lock); 8456 8457 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8458 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8459 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8460 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8461 sizeof(persis_io.pr), M_WAITOK); 8462 break; 8463 8464 case SPRO_PREEMPT: 8465 case SPRO_PRE_ABO: { 8466 int nretval; 8467 8468 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8469 residx, ctsio, cdb, param); 8470 if (nretval != 0) 8471 return (CTL_RETVAL_COMPLETE); 8472 break; 8473 } 8474 default: 8475 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8476 } 8477 8478 done: 8479 free(ctsio->kern_data_ptr, M_CTL); 8480 ctl_set_success(ctsio); 8481 ctl_done((union ctl_io *)ctsio); 8482 8483 return (retval); 8484 } 8485 8486 /* 8487 * This routine is for handling a message from the other SC pertaining to 8488 * persistent reserve out. All the error checking will have been done 8489 * so only perorming the action need be done here to keep the two 8490 * in sync. 8491 */ 8492 static void 8493 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8494 { 8495 struct ctl_softc *softc = CTL_SOFTC(io); 8496 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8497 struct ctl_lun *lun; 8498 int i; 8499 uint32_t residx, targ_lun; 8500 8501 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8502 mtx_lock(&softc->ctl_lock); 8503 if (targ_lun >= ctl_max_luns || 8504 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8505 mtx_unlock(&softc->ctl_lock); 8506 return; 8507 } 8508 mtx_lock(&lun->lun_lock); 8509 mtx_unlock(&softc->ctl_lock); 8510 if (lun->flags & CTL_LUN_DISABLED) { 8511 mtx_unlock(&lun->lun_lock); 8512 return; 8513 } 8514 residx = ctl_get_initindex(&msg->hdr.nexus); 8515 switch(msg->pr.pr_info.action) { 8516 case CTL_PR_REG_KEY: 8517 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8518 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8519 lun->pr_key_count++; 8520 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8521 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8522 lun->pr_generation++; 8523 break; 8524 8525 case CTL_PR_UNREG_KEY: 8526 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8527 lun->pr_key_count--; 8528 8529 /* XXX Need to see if the reservation has been released */ 8530 /* if so do we need to generate UA? */ 8531 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8532 lun->flags &= ~CTL_LUN_PR_RESERVED; 8533 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8534 8535 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8536 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8537 lun->pr_key_count) { 8538 /* 8539 * If the reservation is a registrants 8540 * only type we need to generate a UA 8541 * for other registered inits. The 8542 * sense code should be RESERVATIONS 8543 * RELEASED 8544 */ 8545 8546 for (i = softc->init_min; i < softc->init_max; i++) { 8547 if (ctl_get_prkey(lun, i) == 0) 8548 continue; 8549 8550 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8551 } 8552 } 8553 lun->pr_res_type = 0; 8554 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8555 if (lun->pr_key_count==0) { 8556 lun->flags &= ~CTL_LUN_PR_RESERVED; 8557 lun->pr_res_type = 0; 8558 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8559 } 8560 } 8561 lun->pr_generation++; 8562 break; 8563 8564 case CTL_PR_RESERVE: 8565 lun->flags |= CTL_LUN_PR_RESERVED; 8566 lun->pr_res_type = msg->pr.pr_info.res_type; 8567 lun->pr_res_idx = msg->pr.pr_info.residx; 8568 8569 break; 8570 8571 case CTL_PR_RELEASE: 8572 /* 8573 * If this isn't an exclusive access reservation and NUAR 8574 * is not set, generate UA for all other registrants. 8575 */ 8576 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8577 lun->pr_res_type != SPR_TYPE_WR_EX && 8578 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8579 for (i = softc->init_min; i < softc->init_max; i++) { 8580 if (i == residx || ctl_get_prkey(lun, i) == 0) 8581 continue; 8582 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8583 } 8584 } 8585 8586 lun->flags &= ~CTL_LUN_PR_RESERVED; 8587 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8588 lun->pr_res_type = 0; 8589 break; 8590 8591 case CTL_PR_PREEMPT: 8592 ctl_pro_preempt_other(lun, msg); 8593 break; 8594 case CTL_PR_CLEAR: 8595 lun->flags &= ~CTL_LUN_PR_RESERVED; 8596 lun->pr_res_type = 0; 8597 lun->pr_key_count = 0; 8598 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8599 8600 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8601 if (ctl_get_prkey(lun, i) == 0) 8602 continue; 8603 ctl_clr_prkey(lun, i); 8604 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8605 } 8606 lun->pr_generation++; 8607 break; 8608 } 8609 8610 mtx_unlock(&lun->lun_lock); 8611 } 8612 8613 int 8614 ctl_read_write(struct ctl_scsiio *ctsio) 8615 { 8616 struct ctl_lun *lun = CTL_LUN(ctsio); 8617 struct ctl_lba_len_flags *lbalen; 8618 uint64_t lba; 8619 uint32_t num_blocks; 8620 int flags, retval; 8621 int isread; 8622 8623 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8624 8625 flags = 0; 8626 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8627 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8628 switch (ctsio->cdb[0]) { 8629 case READ_6: 8630 case WRITE_6: { 8631 struct scsi_rw_6 *cdb; 8632 8633 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8634 8635 lba = scsi_3btoul(cdb->addr); 8636 /* only 5 bits are valid in the most significant address byte */ 8637 lba &= 0x1fffff; 8638 num_blocks = cdb->length; 8639 /* 8640 * This is correct according to SBC-2. 8641 */ 8642 if (num_blocks == 0) 8643 num_blocks = 256; 8644 break; 8645 } 8646 case READ_10: 8647 case WRITE_10: { 8648 struct scsi_rw_10 *cdb; 8649 8650 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8651 if (cdb->byte2 & SRW10_FUA) 8652 flags |= CTL_LLF_FUA; 8653 if (cdb->byte2 & SRW10_DPO) 8654 flags |= CTL_LLF_DPO; 8655 lba = scsi_4btoul(cdb->addr); 8656 num_blocks = scsi_2btoul(cdb->length); 8657 break; 8658 } 8659 case WRITE_VERIFY_10: { 8660 struct scsi_write_verify_10 *cdb; 8661 8662 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8663 flags |= CTL_LLF_FUA; 8664 if (cdb->byte2 & SWV_DPO) 8665 flags |= CTL_LLF_DPO; 8666 lba = scsi_4btoul(cdb->addr); 8667 num_blocks = scsi_2btoul(cdb->length); 8668 break; 8669 } 8670 case READ_12: 8671 case WRITE_12: { 8672 struct scsi_rw_12 *cdb; 8673 8674 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8675 if (cdb->byte2 & SRW12_FUA) 8676 flags |= CTL_LLF_FUA; 8677 if (cdb->byte2 & SRW12_DPO) 8678 flags |= CTL_LLF_DPO; 8679 lba = scsi_4btoul(cdb->addr); 8680 num_blocks = scsi_4btoul(cdb->length); 8681 break; 8682 } 8683 case WRITE_VERIFY_12: { 8684 struct scsi_write_verify_12 *cdb; 8685 8686 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8687 flags |= CTL_LLF_FUA; 8688 if (cdb->byte2 & SWV_DPO) 8689 flags |= CTL_LLF_DPO; 8690 lba = scsi_4btoul(cdb->addr); 8691 num_blocks = scsi_4btoul(cdb->length); 8692 break; 8693 } 8694 case READ_16: 8695 case WRITE_16: { 8696 struct scsi_rw_16 *cdb; 8697 8698 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8699 if (cdb->byte2 & SRW12_FUA) 8700 flags |= CTL_LLF_FUA; 8701 if (cdb->byte2 & SRW12_DPO) 8702 flags |= CTL_LLF_DPO; 8703 lba = scsi_8btou64(cdb->addr); 8704 num_blocks = scsi_4btoul(cdb->length); 8705 break; 8706 } 8707 case WRITE_ATOMIC_16: { 8708 struct scsi_write_atomic_16 *cdb; 8709 8710 if (lun->be_lun->atomicblock == 0) { 8711 ctl_set_invalid_opcode(ctsio); 8712 ctl_done((union ctl_io *)ctsio); 8713 return (CTL_RETVAL_COMPLETE); 8714 } 8715 8716 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8717 if (cdb->byte2 & SRW12_FUA) 8718 flags |= CTL_LLF_FUA; 8719 if (cdb->byte2 & SRW12_DPO) 8720 flags |= CTL_LLF_DPO; 8721 lba = scsi_8btou64(cdb->addr); 8722 num_blocks = scsi_2btoul(cdb->length); 8723 if (num_blocks > lun->be_lun->atomicblock) { 8724 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8725 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8726 /*bit*/ 0); 8727 ctl_done((union ctl_io *)ctsio); 8728 return (CTL_RETVAL_COMPLETE); 8729 } 8730 break; 8731 } 8732 case WRITE_VERIFY_16: { 8733 struct scsi_write_verify_16 *cdb; 8734 8735 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8736 flags |= CTL_LLF_FUA; 8737 if (cdb->byte2 & SWV_DPO) 8738 flags |= CTL_LLF_DPO; 8739 lba = scsi_8btou64(cdb->addr); 8740 num_blocks = scsi_4btoul(cdb->length); 8741 break; 8742 } 8743 default: 8744 /* 8745 * We got a command we don't support. This shouldn't 8746 * happen, commands should be filtered out above us. 8747 */ 8748 ctl_set_invalid_opcode(ctsio); 8749 ctl_done((union ctl_io *)ctsio); 8750 8751 return (CTL_RETVAL_COMPLETE); 8752 break; /* NOTREACHED */ 8753 } 8754 8755 /* 8756 * The first check is to make sure we're in bounds, the second 8757 * check is to catch wrap-around problems. If the lba + num blocks 8758 * is less than the lba, then we've wrapped around and the block 8759 * range is invalid anyway. 8760 */ 8761 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8762 || ((lba + num_blocks) < lba)) { 8763 ctl_set_lba_out_of_range(ctsio, 8764 MAX(lba, lun->be_lun->maxlba + 1)); 8765 ctl_done((union ctl_io *)ctsio); 8766 return (CTL_RETVAL_COMPLETE); 8767 } 8768 8769 /* 8770 * According to SBC-3, a transfer length of 0 is not an error. 8771 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8772 * translates to 256 blocks for those commands. 8773 */ 8774 if (num_blocks == 0) { 8775 ctl_set_success(ctsio); 8776 ctl_done((union ctl_io *)ctsio); 8777 return (CTL_RETVAL_COMPLETE); 8778 } 8779 8780 /* Set FUA and/or DPO if caches are disabled. */ 8781 if (isread) { 8782 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8783 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8784 } else { 8785 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8786 flags |= CTL_LLF_FUA; 8787 } 8788 8789 lbalen = (struct ctl_lba_len_flags *) 8790 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8791 lbalen->lba = lba; 8792 lbalen->len = num_blocks; 8793 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8794 8795 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8796 ctsio->kern_rel_offset = 0; 8797 8798 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8799 8800 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8801 return (retval); 8802 } 8803 8804 static int 8805 ctl_cnw_cont(union ctl_io *io) 8806 { 8807 struct ctl_lun *lun = CTL_LUN(io); 8808 struct ctl_scsiio *ctsio; 8809 struct ctl_lba_len_flags *lbalen; 8810 int retval; 8811 8812 ctsio = &io->scsiio; 8813 ctsio->io_hdr.status = CTL_STATUS_NONE; 8814 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8815 lbalen = (struct ctl_lba_len_flags *) 8816 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8817 lbalen->flags &= ~CTL_LLF_COMPARE; 8818 lbalen->flags |= CTL_LLF_WRITE; 8819 8820 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8821 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8822 return (retval); 8823 } 8824 8825 int 8826 ctl_cnw(struct ctl_scsiio *ctsio) 8827 { 8828 struct ctl_lun *lun = CTL_LUN(ctsio); 8829 struct ctl_lba_len_flags *lbalen; 8830 uint64_t lba; 8831 uint32_t num_blocks; 8832 int flags, retval; 8833 8834 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8835 8836 flags = 0; 8837 switch (ctsio->cdb[0]) { 8838 case COMPARE_AND_WRITE: { 8839 struct scsi_compare_and_write *cdb; 8840 8841 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8842 if (cdb->byte2 & SRW10_FUA) 8843 flags |= CTL_LLF_FUA; 8844 if (cdb->byte2 & SRW10_DPO) 8845 flags |= CTL_LLF_DPO; 8846 lba = scsi_8btou64(cdb->addr); 8847 num_blocks = cdb->length; 8848 break; 8849 } 8850 default: 8851 /* 8852 * We got a command we don't support. This shouldn't 8853 * happen, commands should be filtered out above us. 8854 */ 8855 ctl_set_invalid_opcode(ctsio); 8856 ctl_done((union ctl_io *)ctsio); 8857 8858 return (CTL_RETVAL_COMPLETE); 8859 break; /* NOTREACHED */ 8860 } 8861 8862 /* 8863 * The first check is to make sure we're in bounds, the second 8864 * check is to catch wrap-around problems. If the lba + num blocks 8865 * is less than the lba, then we've wrapped around and the block 8866 * range is invalid anyway. 8867 */ 8868 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8869 || ((lba + num_blocks) < lba)) { 8870 ctl_set_lba_out_of_range(ctsio, 8871 MAX(lba, lun->be_lun->maxlba + 1)); 8872 ctl_done((union ctl_io *)ctsio); 8873 return (CTL_RETVAL_COMPLETE); 8874 } 8875 8876 /* 8877 * According to SBC-3, a transfer length of 0 is not an error. 8878 */ 8879 if (num_blocks == 0) { 8880 ctl_set_success(ctsio); 8881 ctl_done((union ctl_io *)ctsio); 8882 return (CTL_RETVAL_COMPLETE); 8883 } 8884 8885 /* Set FUA if write cache is disabled. */ 8886 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8887 flags |= CTL_LLF_FUA; 8888 8889 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8890 ctsio->kern_rel_offset = 0; 8891 8892 /* 8893 * Set the IO_CONT flag, so that if this I/O gets passed to 8894 * ctl_data_submit_done(), it'll get passed back to 8895 * ctl_ctl_cnw_cont() for further processing. 8896 */ 8897 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8898 ctsio->io_cont = ctl_cnw_cont; 8899 8900 lbalen = (struct ctl_lba_len_flags *) 8901 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8902 lbalen->lba = lba; 8903 lbalen->len = num_blocks; 8904 lbalen->flags = CTL_LLF_COMPARE | flags; 8905 8906 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8907 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8908 return (retval); 8909 } 8910 8911 int 8912 ctl_verify(struct ctl_scsiio *ctsio) 8913 { 8914 struct ctl_lun *lun = CTL_LUN(ctsio); 8915 struct ctl_lba_len_flags *lbalen; 8916 uint64_t lba; 8917 uint32_t num_blocks; 8918 int bytchk, flags; 8919 int retval; 8920 8921 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8922 8923 bytchk = 0; 8924 flags = CTL_LLF_FUA; 8925 switch (ctsio->cdb[0]) { 8926 case VERIFY_10: { 8927 struct scsi_verify_10 *cdb; 8928 8929 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8930 if (cdb->byte2 & SVFY_BYTCHK) 8931 bytchk = 1; 8932 if (cdb->byte2 & SVFY_DPO) 8933 flags |= CTL_LLF_DPO; 8934 lba = scsi_4btoul(cdb->addr); 8935 num_blocks = scsi_2btoul(cdb->length); 8936 break; 8937 } 8938 case VERIFY_12: { 8939 struct scsi_verify_12 *cdb; 8940 8941 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8942 if (cdb->byte2 & SVFY_BYTCHK) 8943 bytchk = 1; 8944 if (cdb->byte2 & SVFY_DPO) 8945 flags |= CTL_LLF_DPO; 8946 lba = scsi_4btoul(cdb->addr); 8947 num_blocks = scsi_4btoul(cdb->length); 8948 break; 8949 } 8950 case VERIFY_16: { 8951 struct scsi_rw_16 *cdb; 8952 8953 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8954 if (cdb->byte2 & SVFY_BYTCHK) 8955 bytchk = 1; 8956 if (cdb->byte2 & SVFY_DPO) 8957 flags |= CTL_LLF_DPO; 8958 lba = scsi_8btou64(cdb->addr); 8959 num_blocks = scsi_4btoul(cdb->length); 8960 break; 8961 } 8962 default: 8963 /* 8964 * We got a command we don't support. This shouldn't 8965 * happen, commands should be filtered out above us. 8966 */ 8967 ctl_set_invalid_opcode(ctsio); 8968 ctl_done((union ctl_io *)ctsio); 8969 return (CTL_RETVAL_COMPLETE); 8970 } 8971 8972 /* 8973 * The first check is to make sure we're in bounds, the second 8974 * check is to catch wrap-around problems. If the lba + num blocks 8975 * is less than the lba, then we've wrapped around and the block 8976 * range is invalid anyway. 8977 */ 8978 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8979 || ((lba + num_blocks) < lba)) { 8980 ctl_set_lba_out_of_range(ctsio, 8981 MAX(lba, lun->be_lun->maxlba + 1)); 8982 ctl_done((union ctl_io *)ctsio); 8983 return (CTL_RETVAL_COMPLETE); 8984 } 8985 8986 /* 8987 * According to SBC-3, a transfer length of 0 is not an error. 8988 */ 8989 if (num_blocks == 0) { 8990 ctl_set_success(ctsio); 8991 ctl_done((union ctl_io *)ctsio); 8992 return (CTL_RETVAL_COMPLETE); 8993 } 8994 8995 lbalen = (struct ctl_lba_len_flags *) 8996 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8997 lbalen->lba = lba; 8998 lbalen->len = num_blocks; 8999 if (bytchk) { 9000 lbalen->flags = CTL_LLF_COMPARE | flags; 9001 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9002 } else { 9003 lbalen->flags = CTL_LLF_VERIFY | flags; 9004 ctsio->kern_total_len = 0; 9005 } 9006 ctsio->kern_rel_offset = 0; 9007 9008 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9009 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9010 return (retval); 9011 } 9012 9013 int 9014 ctl_report_luns(struct ctl_scsiio *ctsio) 9015 { 9016 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9017 struct ctl_port *port = CTL_PORT(ctsio); 9018 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9019 struct scsi_report_luns *cdb; 9020 struct scsi_report_luns_data *lun_data; 9021 int num_filled, num_luns, num_port_luns, retval; 9022 uint32_t alloc_len, lun_datalen; 9023 uint32_t initidx, targ_lun_id, lun_id; 9024 9025 retval = CTL_RETVAL_COMPLETE; 9026 cdb = (struct scsi_report_luns *)ctsio->cdb; 9027 9028 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9029 9030 num_luns = 0; 9031 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9032 mtx_lock(&softc->ctl_lock); 9033 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9034 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9035 num_luns++; 9036 } 9037 mtx_unlock(&softc->ctl_lock); 9038 9039 switch (cdb->select_report) { 9040 case RPL_REPORT_DEFAULT: 9041 case RPL_REPORT_ALL: 9042 case RPL_REPORT_NONSUBSID: 9043 break; 9044 case RPL_REPORT_WELLKNOWN: 9045 case RPL_REPORT_ADMIN: 9046 case RPL_REPORT_CONGLOM: 9047 num_luns = 0; 9048 break; 9049 default: 9050 ctl_set_invalid_field(ctsio, 9051 /*sks_valid*/ 1, 9052 /*command*/ 1, 9053 /*field*/ 2, 9054 /*bit_valid*/ 0, 9055 /*bit*/ 0); 9056 ctl_done((union ctl_io *)ctsio); 9057 return (retval); 9058 break; /* NOTREACHED */ 9059 } 9060 9061 alloc_len = scsi_4btoul(cdb->length); 9062 /* 9063 * The initiator has to allocate at least 16 bytes for this request, 9064 * so he can at least get the header and the first LUN. Otherwise 9065 * we reject the request (per SPC-3 rev 14, section 6.21). 9066 */ 9067 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9068 sizeof(struct scsi_report_luns_lundata))) { 9069 ctl_set_invalid_field(ctsio, 9070 /*sks_valid*/ 1, 9071 /*command*/ 1, 9072 /*field*/ 6, 9073 /*bit_valid*/ 0, 9074 /*bit*/ 0); 9075 ctl_done((union ctl_io *)ctsio); 9076 return (retval); 9077 } 9078 9079 lun_datalen = sizeof(*lun_data) + 9080 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9081 9082 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9083 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9084 ctsio->kern_sg_entries = 0; 9085 9086 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9087 9088 mtx_lock(&softc->ctl_lock); 9089 for (targ_lun_id = 0, num_filled = 0; 9090 targ_lun_id < num_port_luns && num_filled < num_luns; 9091 targ_lun_id++) { 9092 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9093 if (lun_id == UINT32_MAX) 9094 continue; 9095 lun = softc->ctl_luns[lun_id]; 9096 if (lun == NULL) 9097 continue; 9098 9099 be64enc(lun_data->luns[num_filled++].lundata, 9100 ctl_encode_lun(targ_lun_id)); 9101 9102 /* 9103 * According to SPC-3, rev 14 section 6.21: 9104 * 9105 * "The execution of a REPORT LUNS command to any valid and 9106 * installed logical unit shall clear the REPORTED LUNS DATA 9107 * HAS CHANGED unit attention condition for all logical 9108 * units of that target with respect to the requesting 9109 * initiator. A valid and installed logical unit is one 9110 * having a PERIPHERAL QUALIFIER of 000b in the standard 9111 * INQUIRY data (see 6.4.2)." 9112 * 9113 * If request_lun is NULL, the LUN this report luns command 9114 * was issued to is either disabled or doesn't exist. In that 9115 * case, we shouldn't clear any pending lun change unit 9116 * attention. 9117 */ 9118 if (request_lun != NULL) { 9119 mtx_lock(&lun->lun_lock); 9120 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9121 mtx_unlock(&lun->lun_lock); 9122 } 9123 } 9124 mtx_unlock(&softc->ctl_lock); 9125 9126 /* 9127 * It's quite possible that we've returned fewer LUNs than we allocated 9128 * space for. Trim it. 9129 */ 9130 lun_datalen = sizeof(*lun_data) + 9131 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9132 ctsio->kern_rel_offset = 0; 9133 ctsio->kern_sg_entries = 0; 9134 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9135 ctsio->kern_total_len = ctsio->kern_data_len; 9136 9137 /* 9138 * We set this to the actual data length, regardless of how much 9139 * space we actually have to return results. If the user looks at 9140 * this value, he'll know whether or not he allocated enough space 9141 * and reissue the command if necessary. We don't support well 9142 * known logical units, so if the user asks for that, return none. 9143 */ 9144 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9145 9146 /* 9147 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9148 * this request. 9149 */ 9150 ctl_set_success(ctsio); 9151 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9152 ctsio->be_move_done = ctl_config_move_done; 9153 ctl_datamove((union ctl_io *)ctsio); 9154 return (retval); 9155 } 9156 9157 int 9158 ctl_request_sense(struct ctl_scsiio *ctsio) 9159 { 9160 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9161 struct ctl_lun *lun = CTL_LUN(ctsio); 9162 struct scsi_request_sense *cdb; 9163 struct scsi_sense_data *sense_ptr, *ps; 9164 uint32_t initidx; 9165 int have_error; 9166 u_int sense_len = SSD_FULL_SIZE; 9167 scsi_sense_data_type sense_format; 9168 ctl_ua_type ua_type; 9169 uint8_t asc = 0, ascq = 0; 9170 9171 cdb = (struct scsi_request_sense *)ctsio->cdb; 9172 9173 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9174 9175 /* 9176 * Determine which sense format the user wants. 9177 */ 9178 if (cdb->byte2 & SRS_DESC) 9179 sense_format = SSD_TYPE_DESC; 9180 else 9181 sense_format = SSD_TYPE_FIXED; 9182 9183 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9184 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9185 ctsio->kern_sg_entries = 0; 9186 ctsio->kern_rel_offset = 0; 9187 9188 /* 9189 * struct scsi_sense_data, which is currently set to 256 bytes, is 9190 * larger than the largest allowed value for the length field in the 9191 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9192 */ 9193 ctsio->kern_data_len = cdb->length; 9194 ctsio->kern_total_len = cdb->length; 9195 9196 /* 9197 * If we don't have a LUN, we don't have any pending sense. 9198 */ 9199 if (lun == NULL || 9200 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9201 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9202 /* "Logical unit not supported" */ 9203 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9204 /*current_error*/ 1, 9205 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9206 /*asc*/ 0x25, 9207 /*ascq*/ 0x00, 9208 SSD_ELEM_NONE); 9209 goto send; 9210 } 9211 9212 have_error = 0; 9213 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9214 /* 9215 * Check for pending sense, and then for pending unit attentions. 9216 * Pending sense gets returned first, then pending unit attentions. 9217 */ 9218 mtx_lock(&lun->lun_lock); 9219 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9220 if (ps != NULL) 9221 ps += initidx % CTL_MAX_INIT_PER_PORT; 9222 if (ps != NULL && ps->error_code != 0) { 9223 scsi_sense_data_type stored_format; 9224 9225 /* 9226 * Check to see which sense format was used for the stored 9227 * sense data. 9228 */ 9229 stored_format = scsi_sense_type(ps); 9230 9231 /* 9232 * If the user requested a different sense format than the 9233 * one we stored, then we need to convert it to the other 9234 * format. If we're going from descriptor to fixed format 9235 * sense data, we may lose things in translation, depending 9236 * on what options were used. 9237 * 9238 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9239 * for some reason we'll just copy it out as-is. 9240 */ 9241 if ((stored_format == SSD_TYPE_FIXED) 9242 && (sense_format == SSD_TYPE_DESC)) 9243 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9244 ps, (struct scsi_sense_data_desc *)sense_ptr); 9245 else if ((stored_format == SSD_TYPE_DESC) 9246 && (sense_format == SSD_TYPE_FIXED)) 9247 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9248 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9249 else 9250 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9251 9252 ps->error_code = 0; 9253 have_error = 1; 9254 } else { 9255 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9256 sense_format); 9257 if (ua_type != CTL_UA_NONE) 9258 have_error = 1; 9259 } 9260 if (have_error == 0) { 9261 /* 9262 * Report informational exception if have one and allowed. 9263 */ 9264 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9265 asc = lun->ie_asc; 9266 ascq = lun->ie_ascq; 9267 } 9268 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9269 /*current_error*/ 1, 9270 /*sense_key*/ SSD_KEY_NO_SENSE, 9271 /*asc*/ asc, 9272 /*ascq*/ ascq, 9273 SSD_ELEM_NONE); 9274 } 9275 mtx_unlock(&lun->lun_lock); 9276 9277 send: 9278 /* 9279 * We report the SCSI status as OK, since the status of the command 9280 * itself is OK. We're reporting sense as parameter data. 9281 */ 9282 ctl_set_success(ctsio); 9283 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9284 ctsio->be_move_done = ctl_config_move_done; 9285 ctl_datamove((union ctl_io *)ctsio); 9286 return (CTL_RETVAL_COMPLETE); 9287 } 9288 9289 int 9290 ctl_tur(struct ctl_scsiio *ctsio) 9291 { 9292 9293 CTL_DEBUG_PRINT(("ctl_tur\n")); 9294 9295 ctl_set_success(ctsio); 9296 ctl_done((union ctl_io *)ctsio); 9297 9298 return (CTL_RETVAL_COMPLETE); 9299 } 9300 9301 /* 9302 * SCSI VPD page 0x00, the Supported VPD Pages page. 9303 */ 9304 static int 9305 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9306 { 9307 struct ctl_lun *lun = CTL_LUN(ctsio); 9308 struct scsi_vpd_supported_pages *pages; 9309 int sup_page_size; 9310 int p; 9311 9312 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9313 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9314 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9315 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9316 ctsio->kern_rel_offset = 0; 9317 ctsio->kern_sg_entries = 0; 9318 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9319 ctsio->kern_total_len = ctsio->kern_data_len; 9320 9321 /* 9322 * The control device is always connected. The disk device, on the 9323 * other hand, may not be online all the time. Need to change this 9324 * to figure out whether the disk device is actually online or not. 9325 */ 9326 if (lun != NULL) 9327 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9328 lun->be_lun->lun_type; 9329 else 9330 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9331 9332 p = 0; 9333 /* Supported VPD pages */ 9334 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9335 /* Serial Number */ 9336 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9337 /* Device Identification */ 9338 pages->page_list[p++] = SVPD_DEVICE_ID; 9339 /* Extended INQUIRY Data */ 9340 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9341 /* Mode Page Policy */ 9342 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9343 /* SCSI Ports */ 9344 pages->page_list[p++] = SVPD_SCSI_PORTS; 9345 /* Third-party Copy */ 9346 pages->page_list[p++] = SVPD_SCSI_TPC; 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_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9729 { 9730 struct ctl_lun *lun = CTL_LUN(ctsio); 9731 struct scsi_vpd_block_limits *bl_ptr; 9732 const char *val; 9733 uint64_t ival; 9734 9735 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9736 bl_ptr = (struct scsi_vpd_block_limits *)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(sizeof(*bl_ptr), 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 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9750 lun->be_lun->lun_type; 9751 else 9752 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9753 9754 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9755 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9756 bl_ptr->max_cmp_write_len = 0xff; 9757 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9758 if (lun != NULL) { 9759 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9760 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9761 ival = 0xffffffff; 9762 val = dnvlist_get_string(lun->be_lun->options, 9763 "unmap_max_lba", NULL); 9764 if (val != NULL) 9765 ctl_expand_number(val, &ival); 9766 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9767 ival = 0xffffffff; 9768 val = dnvlist_get_string(lun->be_lun->options, 9769 "unmap_max_descr", NULL); 9770 if (val != NULL) 9771 ctl_expand_number(val, &ival); 9772 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9773 if (lun->be_lun->ublockexp != 0) { 9774 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9775 bl_ptr->opt_unmap_grain); 9776 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9777 bl_ptr->unmap_grain_align); 9778 } 9779 } 9780 scsi_ulto4b(lun->be_lun->atomicblock, 9781 bl_ptr->max_atomic_transfer_length); 9782 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9783 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9784 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9785 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9786 ival = UINT64_MAX; 9787 val = dnvlist_get_string(lun->be_lun->options, 9788 "write_same_max_lba", NULL); 9789 if (val != NULL) 9790 ctl_expand_number(val, &ival); 9791 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9792 } 9793 9794 ctl_set_success(ctsio); 9795 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9796 ctsio->be_move_done = ctl_config_move_done; 9797 ctl_datamove((union ctl_io *)ctsio); 9798 return (CTL_RETVAL_COMPLETE); 9799 } 9800 9801 static int 9802 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9803 { 9804 struct ctl_lun *lun = CTL_LUN(ctsio); 9805 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9806 const char *value; 9807 u_int i; 9808 9809 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9810 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9811 ctsio->kern_sg_entries = 0; 9812 ctsio->kern_rel_offset = 0; 9813 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 9814 ctsio->kern_total_len = ctsio->kern_data_len; 9815 9816 /* 9817 * The control device is always connected. The disk device, on the 9818 * other hand, may not be online all the time. Need to change this 9819 * to figure out whether the disk device is actually online or not. 9820 */ 9821 if (lun != NULL) 9822 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9823 lun->be_lun->lun_type; 9824 else 9825 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9826 bdc_ptr->page_code = SVPD_BDC; 9827 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9828 if (lun != NULL && 9829 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 9830 i = strtol(value, NULL, 0); 9831 else 9832 i = CTL_DEFAULT_ROTATION_RATE; 9833 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9834 if (lun != NULL && 9835 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 9836 i = strtol(value, NULL, 0); 9837 else 9838 i = 0; 9839 bdc_ptr->wab_wac_ff = (i & 0x0f); 9840 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9841 9842 ctl_set_success(ctsio); 9843 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9844 ctsio->be_move_done = ctl_config_move_done; 9845 ctl_datamove((union ctl_io *)ctsio); 9846 return (CTL_RETVAL_COMPLETE); 9847 } 9848 9849 static int 9850 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9851 { 9852 struct ctl_lun *lun = CTL_LUN(ctsio); 9853 struct scsi_vpd_logical_block_prov *lbp_ptr; 9854 const char *value; 9855 9856 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9857 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9858 ctsio->kern_sg_entries = 0; 9859 ctsio->kern_rel_offset = 0; 9860 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 9861 ctsio->kern_total_len = ctsio->kern_data_len; 9862 9863 /* 9864 * The control device is always connected. The disk device, on the 9865 * other hand, may not be online all the time. Need to change this 9866 * to figure out whether the disk device is actually online or not. 9867 */ 9868 if (lun != NULL) 9869 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9870 lun->be_lun->lun_type; 9871 else 9872 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9873 9874 lbp_ptr->page_code = SVPD_LBP; 9875 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9876 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9877 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9878 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9879 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9880 value = dnvlist_get_string(lun->be_lun->options, 9881 "provisioning_type", NULL); 9882 if (value != NULL) { 9883 if (strcmp(value, "resource") == 0) 9884 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 9885 else if (strcmp(value, "thin") == 0) 9886 lbp_ptr->prov_type = SVPD_LBP_THIN; 9887 } else 9888 lbp_ptr->prov_type = SVPD_LBP_THIN; 9889 } 9890 9891 ctl_set_success(ctsio); 9892 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9893 ctsio->be_move_done = ctl_config_move_done; 9894 ctl_datamove((union ctl_io *)ctsio); 9895 return (CTL_RETVAL_COMPLETE); 9896 } 9897 9898 /* 9899 * INQUIRY with the EVPD bit set. 9900 */ 9901 static int 9902 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9903 { 9904 struct ctl_lun *lun = CTL_LUN(ctsio); 9905 struct scsi_inquiry *cdb; 9906 int alloc_len, retval; 9907 9908 cdb = (struct scsi_inquiry *)ctsio->cdb; 9909 alloc_len = scsi_2btoul(cdb->length); 9910 9911 switch (cdb->page_code) { 9912 case SVPD_SUPPORTED_PAGES: 9913 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9914 break; 9915 case SVPD_UNIT_SERIAL_NUMBER: 9916 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9917 break; 9918 case SVPD_DEVICE_ID: 9919 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9920 break; 9921 case SVPD_EXTENDED_INQUIRY_DATA: 9922 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9923 break; 9924 case SVPD_MODE_PAGE_POLICY: 9925 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 9926 break; 9927 case SVPD_SCSI_PORTS: 9928 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 9929 break; 9930 case SVPD_SCSI_TPC: 9931 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 9932 break; 9933 case SVPD_BLOCK_LIMITS: 9934 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9935 goto err; 9936 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9937 break; 9938 case SVPD_BDC: 9939 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9940 goto err; 9941 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 9942 break; 9943 case SVPD_LBP: 9944 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9945 goto err; 9946 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9947 break; 9948 default: 9949 err: 9950 ctl_set_invalid_field(ctsio, 9951 /*sks_valid*/ 1, 9952 /*command*/ 1, 9953 /*field*/ 2, 9954 /*bit_valid*/ 0, 9955 /*bit*/ 0); 9956 ctl_done((union ctl_io *)ctsio); 9957 retval = CTL_RETVAL_COMPLETE; 9958 break; 9959 } 9960 9961 return (retval); 9962 } 9963 9964 /* 9965 * Standard INQUIRY data. 9966 */ 9967 static int 9968 ctl_inquiry_std(struct ctl_scsiio *ctsio) 9969 { 9970 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9971 struct ctl_port *port = CTL_PORT(ctsio); 9972 struct ctl_lun *lun = CTL_LUN(ctsio); 9973 struct scsi_inquiry_data *inq_ptr; 9974 struct scsi_inquiry *cdb; 9975 const char *val; 9976 uint32_t alloc_len, data_len; 9977 ctl_port_type port_type; 9978 9979 port_type = port->port_type; 9980 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 9981 port_type = CTL_PORT_SCSI; 9982 9983 cdb = (struct scsi_inquiry *)ctsio->cdb; 9984 alloc_len = scsi_2btoul(cdb->length); 9985 9986 /* 9987 * We malloc the full inquiry data size here and fill it 9988 * in. If the user only asks for less, we'll give him 9989 * that much. 9990 */ 9991 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 9992 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9993 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 9994 ctsio->kern_sg_entries = 0; 9995 ctsio->kern_rel_offset = 0; 9996 ctsio->kern_data_len = min(data_len, alloc_len); 9997 ctsio->kern_total_len = ctsio->kern_data_len; 9998 9999 if (lun != NULL) { 10000 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10001 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10002 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10003 lun->be_lun->lun_type; 10004 } else { 10005 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10006 lun->be_lun->lun_type; 10007 } 10008 if (lun->flags & CTL_LUN_REMOVABLE) 10009 inq_ptr->dev_qual2 |= SID_RMB; 10010 } else 10011 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10012 10013 /* RMB in byte 2 is 0 */ 10014 inq_ptr->version = SCSI_REV_SPC5; 10015 10016 /* 10017 * According to SAM-3, even if a device only supports a single 10018 * level of LUN addressing, it should still set the HISUP bit: 10019 * 10020 * 4.9.1 Logical unit numbers overview 10021 * 10022 * All logical unit number formats described in this standard are 10023 * hierarchical in structure even when only a single level in that 10024 * hierarchy is used. The HISUP bit shall be set to one in the 10025 * standard INQUIRY data (see SPC-2) when any logical unit number 10026 * format described in this standard is used. Non-hierarchical 10027 * formats are outside the scope of this standard. 10028 * 10029 * Therefore we set the HiSup bit here. 10030 * 10031 * The response format is 2, per SPC-3. 10032 */ 10033 inq_ptr->response_format = SID_HiSup | 2; 10034 10035 inq_ptr->additional_length = data_len - 10036 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10037 CTL_DEBUG_PRINT(("additional_length = %d\n", 10038 inq_ptr->additional_length)); 10039 10040 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10041 if (port_type == CTL_PORT_SCSI) 10042 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10043 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10044 inq_ptr->flags = SID_CmdQue; 10045 if (port_type == CTL_PORT_SCSI) 10046 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10047 10048 /* 10049 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10050 * We have 8 bytes for the vendor name, and 16 bytes for the device 10051 * name and 4 bytes for the revision. 10052 */ 10053 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10054 "vendor", NULL)) == NULL) { 10055 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10056 } else { 10057 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10058 strncpy(inq_ptr->vendor, val, 10059 min(sizeof(inq_ptr->vendor), strlen(val))); 10060 } 10061 if (lun == NULL) { 10062 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10063 sizeof(inq_ptr->product)); 10064 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10065 NULL)) == NULL) { 10066 switch (lun->be_lun->lun_type) { 10067 case T_DIRECT: 10068 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10069 sizeof(inq_ptr->product)); 10070 break; 10071 case T_PROCESSOR: 10072 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10073 sizeof(inq_ptr->product)); 10074 break; 10075 case T_CDROM: 10076 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10077 sizeof(inq_ptr->product)); 10078 break; 10079 default: 10080 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10081 sizeof(inq_ptr->product)); 10082 break; 10083 } 10084 } else { 10085 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10086 strncpy(inq_ptr->product, val, 10087 min(sizeof(inq_ptr->product), strlen(val))); 10088 } 10089 10090 /* 10091 * XXX make this a macro somewhere so it automatically gets 10092 * incremented when we make changes. 10093 */ 10094 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10095 "revision", NULL)) == NULL) { 10096 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10097 } else { 10098 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10099 strncpy(inq_ptr->revision, val, 10100 min(sizeof(inq_ptr->revision), strlen(val))); 10101 } 10102 10103 /* 10104 * For parallel SCSI, we support double transition and single 10105 * transition clocking. We also support QAS (Quick Arbitration 10106 * and Selection) and Information Unit transfers on both the 10107 * control and array devices. 10108 */ 10109 if (port_type == CTL_PORT_SCSI) 10110 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10111 SID_SPI_IUS; 10112 10113 /* SAM-6 (no version claimed) */ 10114 scsi_ulto2b(0x00C0, inq_ptr->version1); 10115 /* SPC-5 (no version claimed) */ 10116 scsi_ulto2b(0x05C0, inq_ptr->version2); 10117 if (port_type == CTL_PORT_FC) { 10118 /* FCP-2 ANSI INCITS.350:2003 */ 10119 scsi_ulto2b(0x0917, inq_ptr->version3); 10120 } else if (port_type == CTL_PORT_SCSI) { 10121 /* SPI-4 ANSI INCITS.362:200x */ 10122 scsi_ulto2b(0x0B56, inq_ptr->version3); 10123 } else if (port_type == CTL_PORT_ISCSI) { 10124 /* iSCSI (no version claimed) */ 10125 scsi_ulto2b(0x0960, inq_ptr->version3); 10126 } else if (port_type == CTL_PORT_SAS) { 10127 /* SAS (no version claimed) */ 10128 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10129 } else if (port_type == CTL_PORT_UMASS) { 10130 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10131 scsi_ulto2b(0x1730, inq_ptr->version3); 10132 } 10133 10134 if (lun == NULL) { 10135 /* SBC-4 (no version claimed) */ 10136 scsi_ulto2b(0x0600, inq_ptr->version4); 10137 } else { 10138 switch (lun->be_lun->lun_type) { 10139 case T_DIRECT: 10140 /* SBC-4 (no version claimed) */ 10141 scsi_ulto2b(0x0600, inq_ptr->version4); 10142 break; 10143 case T_PROCESSOR: 10144 break; 10145 case T_CDROM: 10146 /* MMC-6 (no version claimed) */ 10147 scsi_ulto2b(0x04E0, inq_ptr->version4); 10148 break; 10149 default: 10150 break; 10151 } 10152 } 10153 10154 ctl_set_success(ctsio); 10155 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10156 ctsio->be_move_done = ctl_config_move_done; 10157 ctl_datamove((union ctl_io *)ctsio); 10158 return (CTL_RETVAL_COMPLETE); 10159 } 10160 10161 int 10162 ctl_inquiry(struct ctl_scsiio *ctsio) 10163 { 10164 struct scsi_inquiry *cdb; 10165 int retval; 10166 10167 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10168 10169 cdb = (struct scsi_inquiry *)ctsio->cdb; 10170 if (cdb->byte2 & SI_EVPD) 10171 retval = ctl_inquiry_evpd(ctsio); 10172 else if (cdb->page_code == 0) 10173 retval = ctl_inquiry_std(ctsio); 10174 else { 10175 ctl_set_invalid_field(ctsio, 10176 /*sks_valid*/ 1, 10177 /*command*/ 1, 10178 /*field*/ 2, 10179 /*bit_valid*/ 0, 10180 /*bit*/ 0); 10181 ctl_done((union ctl_io *)ctsio); 10182 return (CTL_RETVAL_COMPLETE); 10183 } 10184 10185 return (retval); 10186 } 10187 10188 int 10189 ctl_get_config(struct ctl_scsiio *ctsio) 10190 { 10191 struct ctl_lun *lun = CTL_LUN(ctsio); 10192 struct scsi_get_config_header *hdr; 10193 struct scsi_get_config_feature *feature; 10194 struct scsi_get_config *cdb; 10195 uint32_t alloc_len, data_len; 10196 int rt, starting; 10197 10198 cdb = (struct scsi_get_config *)ctsio->cdb; 10199 rt = (cdb->rt & SGC_RT_MASK); 10200 starting = scsi_2btoul(cdb->starting_feature); 10201 alloc_len = scsi_2btoul(cdb->length); 10202 10203 data_len = sizeof(struct scsi_get_config_header) + 10204 sizeof(struct scsi_get_config_feature) + 8 + 10205 sizeof(struct scsi_get_config_feature) + 8 + 10206 sizeof(struct scsi_get_config_feature) + 4 + 10207 sizeof(struct scsi_get_config_feature) + 4 + 10208 sizeof(struct scsi_get_config_feature) + 8 + 10209 sizeof(struct scsi_get_config_feature) + 10210 sizeof(struct scsi_get_config_feature) + 4 + 10211 sizeof(struct scsi_get_config_feature) + 4 + 10212 sizeof(struct scsi_get_config_feature) + 4 + 10213 sizeof(struct scsi_get_config_feature) + 4 + 10214 sizeof(struct scsi_get_config_feature) + 4 + 10215 sizeof(struct scsi_get_config_feature) + 4; 10216 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10217 ctsio->kern_sg_entries = 0; 10218 ctsio->kern_rel_offset = 0; 10219 10220 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10221 if (lun->flags & CTL_LUN_NO_MEDIA) 10222 scsi_ulto2b(0x0000, hdr->current_profile); 10223 else 10224 scsi_ulto2b(0x0010, hdr->current_profile); 10225 feature = (struct scsi_get_config_feature *)(hdr + 1); 10226 10227 if (starting > 0x003b) 10228 goto done; 10229 if (starting > 0x003a) 10230 goto f3b; 10231 if (starting > 0x002b) 10232 goto f3a; 10233 if (starting > 0x002a) 10234 goto f2b; 10235 if (starting > 0x001f) 10236 goto f2a; 10237 if (starting > 0x001e) 10238 goto f1f; 10239 if (starting > 0x001d) 10240 goto f1e; 10241 if (starting > 0x0010) 10242 goto f1d; 10243 if (starting > 0x0003) 10244 goto f10; 10245 if (starting > 0x0002) 10246 goto f3; 10247 if (starting > 0x0001) 10248 goto f2; 10249 if (starting > 0x0000) 10250 goto f1; 10251 10252 /* Profile List */ 10253 scsi_ulto2b(0x0000, feature->feature_code); 10254 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10255 feature->add_length = 8; 10256 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10257 feature->feature_data[2] = 0x00; 10258 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10259 feature->feature_data[6] = 0x01; 10260 feature = (struct scsi_get_config_feature *) 10261 &feature->feature_data[feature->add_length]; 10262 10263 f1: /* Core */ 10264 scsi_ulto2b(0x0001, feature->feature_code); 10265 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10266 feature->add_length = 8; 10267 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10268 feature->feature_data[4] = 0x03; 10269 feature = (struct scsi_get_config_feature *) 10270 &feature->feature_data[feature->add_length]; 10271 10272 f2: /* Morphing */ 10273 scsi_ulto2b(0x0002, feature->feature_code); 10274 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10275 feature->add_length = 4; 10276 feature->feature_data[0] = 0x02; 10277 feature = (struct scsi_get_config_feature *) 10278 &feature->feature_data[feature->add_length]; 10279 10280 f3: /* Removable Medium */ 10281 scsi_ulto2b(0x0003, feature->feature_code); 10282 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10283 feature->add_length = 4; 10284 feature->feature_data[0] = 0x39; 10285 feature = (struct scsi_get_config_feature *) 10286 &feature->feature_data[feature->add_length]; 10287 10288 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10289 goto done; 10290 10291 f10: /* Random Read */ 10292 scsi_ulto2b(0x0010, feature->feature_code); 10293 feature->flags = 0x00; 10294 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10295 feature->flags |= SGC_F_CURRENT; 10296 feature->add_length = 8; 10297 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10298 scsi_ulto2b(1, &feature->feature_data[4]); 10299 feature->feature_data[6] = 0x00; 10300 feature = (struct scsi_get_config_feature *) 10301 &feature->feature_data[feature->add_length]; 10302 10303 f1d: /* Multi-Read */ 10304 scsi_ulto2b(0x001D, feature->feature_code); 10305 feature->flags = 0x00; 10306 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10307 feature->flags |= SGC_F_CURRENT; 10308 feature->add_length = 0; 10309 feature = (struct scsi_get_config_feature *) 10310 &feature->feature_data[feature->add_length]; 10311 10312 f1e: /* CD Read */ 10313 scsi_ulto2b(0x001E, feature->feature_code); 10314 feature->flags = 0x00; 10315 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10316 feature->flags |= SGC_F_CURRENT; 10317 feature->add_length = 4; 10318 feature->feature_data[0] = 0x00; 10319 feature = (struct scsi_get_config_feature *) 10320 &feature->feature_data[feature->add_length]; 10321 10322 f1f: /* DVD Read */ 10323 scsi_ulto2b(0x001F, feature->feature_code); 10324 feature->flags = 0x08; 10325 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10326 feature->flags |= SGC_F_CURRENT; 10327 feature->add_length = 4; 10328 feature->feature_data[0] = 0x01; 10329 feature->feature_data[2] = 0x03; 10330 feature = (struct scsi_get_config_feature *) 10331 &feature->feature_data[feature->add_length]; 10332 10333 f2a: /* DVD+RW */ 10334 scsi_ulto2b(0x002A, feature->feature_code); 10335 feature->flags = 0x04; 10336 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10337 feature->flags |= SGC_F_CURRENT; 10338 feature->add_length = 4; 10339 feature->feature_data[0] = 0x00; 10340 feature->feature_data[1] = 0x00; 10341 feature = (struct scsi_get_config_feature *) 10342 &feature->feature_data[feature->add_length]; 10343 10344 f2b: /* DVD+R */ 10345 scsi_ulto2b(0x002B, feature->feature_code); 10346 feature->flags = 0x00; 10347 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10348 feature->flags |= SGC_F_CURRENT; 10349 feature->add_length = 4; 10350 feature->feature_data[0] = 0x00; 10351 feature = (struct scsi_get_config_feature *) 10352 &feature->feature_data[feature->add_length]; 10353 10354 f3a: /* DVD+RW Dual Layer */ 10355 scsi_ulto2b(0x003A, feature->feature_code); 10356 feature->flags = 0x00; 10357 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10358 feature->flags |= SGC_F_CURRENT; 10359 feature->add_length = 4; 10360 feature->feature_data[0] = 0x00; 10361 feature->feature_data[1] = 0x00; 10362 feature = (struct scsi_get_config_feature *) 10363 &feature->feature_data[feature->add_length]; 10364 10365 f3b: /* DVD+R Dual Layer */ 10366 scsi_ulto2b(0x003B, feature->feature_code); 10367 feature->flags = 0x00; 10368 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10369 feature->flags |= SGC_F_CURRENT; 10370 feature->add_length = 4; 10371 feature->feature_data[0] = 0x00; 10372 feature = (struct scsi_get_config_feature *) 10373 &feature->feature_data[feature->add_length]; 10374 10375 done: 10376 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10377 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10378 feature = (struct scsi_get_config_feature *)(hdr + 1); 10379 if (scsi_2btoul(feature->feature_code) == starting) 10380 feature = (struct scsi_get_config_feature *) 10381 &feature->feature_data[feature->add_length]; 10382 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10383 } 10384 scsi_ulto4b(data_len - 4, hdr->data_length); 10385 ctsio->kern_data_len = min(data_len, alloc_len); 10386 ctsio->kern_total_len = ctsio->kern_data_len; 10387 10388 ctl_set_success(ctsio); 10389 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10390 ctsio->be_move_done = ctl_config_move_done; 10391 ctl_datamove((union ctl_io *)ctsio); 10392 return (CTL_RETVAL_COMPLETE); 10393 } 10394 10395 int 10396 ctl_get_event_status(struct ctl_scsiio *ctsio) 10397 { 10398 struct scsi_get_event_status_header *hdr; 10399 struct scsi_get_event_status *cdb; 10400 uint32_t alloc_len, data_len; 10401 10402 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10403 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10404 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10405 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10406 ctl_done((union ctl_io *)ctsio); 10407 return (CTL_RETVAL_COMPLETE); 10408 } 10409 alloc_len = scsi_2btoul(cdb->length); 10410 10411 data_len = sizeof(struct scsi_get_event_status_header); 10412 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10413 ctsio->kern_sg_entries = 0; 10414 ctsio->kern_rel_offset = 0; 10415 ctsio->kern_data_len = min(data_len, alloc_len); 10416 ctsio->kern_total_len = ctsio->kern_data_len; 10417 10418 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10419 scsi_ulto2b(0, hdr->descr_length); 10420 hdr->nea_class = SGESN_NEA; 10421 hdr->supported_class = 0; 10422 10423 ctl_set_success(ctsio); 10424 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10425 ctsio->be_move_done = ctl_config_move_done; 10426 ctl_datamove((union ctl_io *)ctsio); 10427 return (CTL_RETVAL_COMPLETE); 10428 } 10429 10430 int 10431 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10432 { 10433 struct scsi_mechanism_status_header *hdr; 10434 struct scsi_mechanism_status *cdb; 10435 uint32_t alloc_len, data_len; 10436 10437 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10438 alloc_len = scsi_2btoul(cdb->length); 10439 10440 data_len = sizeof(struct scsi_mechanism_status_header); 10441 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10442 ctsio->kern_sg_entries = 0; 10443 ctsio->kern_rel_offset = 0; 10444 ctsio->kern_data_len = min(data_len, alloc_len); 10445 ctsio->kern_total_len = ctsio->kern_data_len; 10446 10447 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10448 hdr->state1 = 0x00; 10449 hdr->state2 = 0xe0; 10450 scsi_ulto3b(0, hdr->lba); 10451 hdr->slots_num = 0; 10452 scsi_ulto2b(0, hdr->slots_length); 10453 10454 ctl_set_success(ctsio); 10455 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10456 ctsio->be_move_done = ctl_config_move_done; 10457 ctl_datamove((union ctl_io *)ctsio); 10458 return (CTL_RETVAL_COMPLETE); 10459 } 10460 10461 static void 10462 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10463 { 10464 10465 lba += 150; 10466 buf[0] = 0; 10467 buf[1] = bin2bcd((lba / 75) / 60); 10468 buf[2] = bin2bcd((lba / 75) % 60); 10469 buf[3] = bin2bcd(lba % 75); 10470 } 10471 10472 int 10473 ctl_read_toc(struct ctl_scsiio *ctsio) 10474 { 10475 struct ctl_lun *lun = CTL_LUN(ctsio); 10476 struct scsi_read_toc_hdr *hdr; 10477 struct scsi_read_toc_type01_descr *descr; 10478 struct scsi_read_toc *cdb; 10479 uint32_t alloc_len, data_len; 10480 int format, msf; 10481 10482 cdb = (struct scsi_read_toc *)ctsio->cdb; 10483 msf = (cdb->byte2 & CD_MSF) != 0; 10484 format = cdb->format; 10485 alloc_len = scsi_2btoul(cdb->data_len); 10486 10487 data_len = sizeof(struct scsi_read_toc_hdr); 10488 if (format == 0) 10489 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10490 else 10491 data_len += sizeof(struct scsi_read_toc_type01_descr); 10492 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10493 ctsio->kern_sg_entries = 0; 10494 ctsio->kern_rel_offset = 0; 10495 ctsio->kern_data_len = min(data_len, alloc_len); 10496 ctsio->kern_total_len = ctsio->kern_data_len; 10497 10498 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10499 if (format == 0) { 10500 scsi_ulto2b(0x12, hdr->data_length); 10501 hdr->first = 1; 10502 hdr->last = 1; 10503 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10504 descr->addr_ctl = 0x14; 10505 descr->track_number = 1; 10506 if (msf) 10507 ctl_ultomsf(0, descr->track_start); 10508 else 10509 scsi_ulto4b(0, descr->track_start); 10510 descr++; 10511 descr->addr_ctl = 0x14; 10512 descr->track_number = 0xaa; 10513 if (msf) 10514 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10515 else 10516 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10517 } else { 10518 scsi_ulto2b(0x0a, hdr->data_length); 10519 hdr->first = 1; 10520 hdr->last = 1; 10521 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10522 descr->addr_ctl = 0x14; 10523 descr->track_number = 1; 10524 if (msf) 10525 ctl_ultomsf(0, descr->track_start); 10526 else 10527 scsi_ulto4b(0, descr->track_start); 10528 } 10529 10530 ctl_set_success(ctsio); 10531 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10532 ctsio->be_move_done = ctl_config_move_done; 10533 ctl_datamove((union ctl_io *)ctsio); 10534 return (CTL_RETVAL_COMPLETE); 10535 } 10536 10537 /* 10538 * For known CDB types, parse the LBA and length. 10539 */ 10540 static int 10541 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10542 { 10543 if (io->io_hdr.io_type != CTL_IO_SCSI) 10544 return (1); 10545 10546 switch (io->scsiio.cdb[0]) { 10547 case COMPARE_AND_WRITE: { 10548 struct scsi_compare_and_write *cdb; 10549 10550 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10551 10552 *lba = scsi_8btou64(cdb->addr); 10553 *len = cdb->length; 10554 break; 10555 } 10556 case READ_6: 10557 case WRITE_6: { 10558 struct scsi_rw_6 *cdb; 10559 10560 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10561 10562 *lba = scsi_3btoul(cdb->addr); 10563 /* only 5 bits are valid in the most significant address byte */ 10564 *lba &= 0x1fffff; 10565 *len = cdb->length; 10566 break; 10567 } 10568 case READ_10: 10569 case WRITE_10: { 10570 struct scsi_rw_10 *cdb; 10571 10572 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10573 10574 *lba = scsi_4btoul(cdb->addr); 10575 *len = scsi_2btoul(cdb->length); 10576 break; 10577 } 10578 case WRITE_VERIFY_10: { 10579 struct scsi_write_verify_10 *cdb; 10580 10581 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10582 10583 *lba = scsi_4btoul(cdb->addr); 10584 *len = scsi_2btoul(cdb->length); 10585 break; 10586 } 10587 case READ_12: 10588 case WRITE_12: { 10589 struct scsi_rw_12 *cdb; 10590 10591 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10592 10593 *lba = scsi_4btoul(cdb->addr); 10594 *len = scsi_4btoul(cdb->length); 10595 break; 10596 } 10597 case WRITE_VERIFY_12: { 10598 struct scsi_write_verify_12 *cdb; 10599 10600 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10601 10602 *lba = scsi_4btoul(cdb->addr); 10603 *len = scsi_4btoul(cdb->length); 10604 break; 10605 } 10606 case READ_16: 10607 case WRITE_16: { 10608 struct scsi_rw_16 *cdb; 10609 10610 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10611 10612 *lba = scsi_8btou64(cdb->addr); 10613 *len = scsi_4btoul(cdb->length); 10614 break; 10615 } 10616 case WRITE_ATOMIC_16: { 10617 struct scsi_write_atomic_16 *cdb; 10618 10619 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10620 10621 *lba = scsi_8btou64(cdb->addr); 10622 *len = scsi_2btoul(cdb->length); 10623 break; 10624 } 10625 case WRITE_VERIFY_16: { 10626 struct scsi_write_verify_16 *cdb; 10627 10628 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10629 10630 *lba = scsi_8btou64(cdb->addr); 10631 *len = scsi_4btoul(cdb->length); 10632 break; 10633 } 10634 case WRITE_SAME_10: { 10635 struct scsi_write_same_10 *cdb; 10636 10637 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10638 10639 *lba = scsi_4btoul(cdb->addr); 10640 *len = scsi_2btoul(cdb->length); 10641 break; 10642 } 10643 case WRITE_SAME_16: { 10644 struct scsi_write_same_16 *cdb; 10645 10646 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10647 10648 *lba = scsi_8btou64(cdb->addr); 10649 *len = scsi_4btoul(cdb->length); 10650 break; 10651 } 10652 case VERIFY_10: { 10653 struct scsi_verify_10 *cdb; 10654 10655 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10656 10657 *lba = scsi_4btoul(cdb->addr); 10658 *len = scsi_2btoul(cdb->length); 10659 break; 10660 } 10661 case VERIFY_12: { 10662 struct scsi_verify_12 *cdb; 10663 10664 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10665 10666 *lba = scsi_4btoul(cdb->addr); 10667 *len = scsi_4btoul(cdb->length); 10668 break; 10669 } 10670 case VERIFY_16: { 10671 struct scsi_verify_16 *cdb; 10672 10673 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10674 10675 *lba = scsi_8btou64(cdb->addr); 10676 *len = scsi_4btoul(cdb->length); 10677 break; 10678 } 10679 case UNMAP: { 10680 *lba = 0; 10681 *len = UINT64_MAX; 10682 break; 10683 } 10684 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10685 struct scsi_get_lba_status *cdb; 10686 10687 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10688 *lba = scsi_8btou64(cdb->addr); 10689 *len = UINT32_MAX; 10690 break; 10691 } 10692 default: 10693 return (1); 10694 break; /* NOTREACHED */ 10695 } 10696 10697 return (0); 10698 } 10699 10700 static ctl_action 10701 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10702 bool seq) 10703 { 10704 uint64_t endlba1, endlba2; 10705 10706 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10707 endlba2 = lba2 + len2 - 1; 10708 10709 if ((endlba1 < lba2) || (endlba2 < lba1)) 10710 return (CTL_ACTION_PASS); 10711 else 10712 return (CTL_ACTION_BLOCK); 10713 } 10714 10715 static int 10716 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10717 { 10718 struct ctl_ptr_len_flags *ptrlen; 10719 struct scsi_unmap_desc *buf, *end, *range; 10720 uint64_t lba; 10721 uint32_t len; 10722 10723 /* If not UNMAP -- go other way. */ 10724 if (io->io_hdr.io_type != CTL_IO_SCSI || 10725 io->scsiio.cdb[0] != UNMAP) 10726 return (CTL_ACTION_ERROR); 10727 10728 /* If UNMAP without data -- block and wait for data. */ 10729 ptrlen = (struct ctl_ptr_len_flags *) 10730 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10731 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10732 ptrlen->ptr == NULL) 10733 return (CTL_ACTION_BLOCK); 10734 10735 /* UNMAP with data -- check for collision. */ 10736 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10737 end = buf + ptrlen->len / sizeof(*buf); 10738 for (range = buf; range < end; range++) { 10739 lba = scsi_8btou64(range->lba); 10740 len = scsi_4btoul(range->length); 10741 if ((lba < lba2 + len2) && (lba + len > lba2)) 10742 return (CTL_ACTION_BLOCK); 10743 } 10744 return (CTL_ACTION_PASS); 10745 } 10746 10747 static ctl_action 10748 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10749 { 10750 uint64_t lba1, lba2; 10751 uint64_t len1, len2; 10752 int retval; 10753 10754 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10755 return (CTL_ACTION_ERROR); 10756 10757 retval = ctl_extent_check_unmap(io1, lba2, len2); 10758 if (retval != CTL_ACTION_ERROR) 10759 return (retval); 10760 10761 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10762 return (CTL_ACTION_ERROR); 10763 10764 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10765 seq = FALSE; 10766 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10767 } 10768 10769 static ctl_action 10770 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10771 { 10772 uint64_t lba1, lba2; 10773 uint64_t len1, len2; 10774 10775 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10776 return (CTL_ACTION_PASS); 10777 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10778 return (CTL_ACTION_ERROR); 10779 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10780 return (CTL_ACTION_ERROR); 10781 10782 if (lba1 + len1 == lba2) 10783 return (CTL_ACTION_BLOCK); 10784 return (CTL_ACTION_PASS); 10785 } 10786 10787 static ctl_action 10788 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10789 union ctl_io *ooa_io) 10790 { 10791 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10792 const ctl_serialize_action *serialize_row; 10793 10794 /* 10795 * The initiator attempted multiple untagged commands at the same 10796 * time. Can't do that. 10797 */ 10798 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10799 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10800 && ((pending_io->io_hdr.nexus.targ_port == 10801 ooa_io->io_hdr.nexus.targ_port) 10802 && (pending_io->io_hdr.nexus.initid == 10803 ooa_io->io_hdr.nexus.initid)) 10804 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10805 CTL_FLAG_STATUS_SENT)) == 0)) 10806 return (CTL_ACTION_OVERLAP); 10807 10808 /* 10809 * The initiator attempted to send multiple tagged commands with 10810 * the same ID. (It's fine if different initiators have the same 10811 * tag ID.) 10812 * 10813 * Even if all of those conditions are true, we don't kill the I/O 10814 * if the command ahead of us has been aborted. We won't end up 10815 * sending it to the FETD, and it's perfectly legal to resend a 10816 * command with the same tag number as long as the previous 10817 * instance of this tag number has been aborted somehow. 10818 */ 10819 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10820 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10821 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10822 && ((pending_io->io_hdr.nexus.targ_port == 10823 ooa_io->io_hdr.nexus.targ_port) 10824 && (pending_io->io_hdr.nexus.initid == 10825 ooa_io->io_hdr.nexus.initid)) 10826 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10827 CTL_FLAG_STATUS_SENT)) == 0)) 10828 return (CTL_ACTION_OVERLAP_TAG); 10829 10830 /* 10831 * If we get a head of queue tag, SAM-3 says that we should 10832 * immediately execute it. 10833 * 10834 * What happens if this command would normally block for some other 10835 * reason? e.g. a request sense with a head of queue tag 10836 * immediately after a write. Normally that would block, but this 10837 * will result in its getting executed immediately... 10838 * 10839 * We currently return "pass" instead of "skip", so we'll end up 10840 * going through the rest of the queue to check for overlapped tags. 10841 * 10842 * XXX KDM check for other types of blockage first?? 10843 */ 10844 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10845 return (CTL_ACTION_PASS); 10846 10847 /* 10848 * Ordered tags have to block until all items ahead of them 10849 * have completed. If we get called with an ordered tag, we always 10850 * block, if something else is ahead of us in the queue. 10851 */ 10852 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10853 return (CTL_ACTION_BLOCK); 10854 10855 /* 10856 * Simple tags get blocked until all head of queue and ordered tags 10857 * ahead of them have completed. I'm lumping untagged commands in 10858 * with simple tags here. XXX KDM is that the right thing to do? 10859 */ 10860 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10861 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10862 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10863 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10864 return (CTL_ACTION_BLOCK); 10865 10866 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10867 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 10868 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 10869 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 10870 pending_io->scsiio.cdb[1], pending_io)); 10871 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10872 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 10873 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 10874 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 10875 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 10876 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 10877 ooa_io->scsiio.cdb[1], ooa_io)); 10878 10879 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10880 10881 switch (serialize_row[pending_entry->seridx]) { 10882 case CTL_SER_BLOCK: 10883 return (CTL_ACTION_BLOCK); 10884 case CTL_SER_EXTENT: 10885 return (ctl_extent_check(ooa_io, pending_io, 10886 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10887 case CTL_SER_EXTENTOPT: 10888 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10889 SCP_QUEUE_ALG_UNRESTRICTED) 10890 return (ctl_extent_check(ooa_io, pending_io, 10891 (lun->be_lun && 10892 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10893 return (CTL_ACTION_PASS); 10894 case CTL_SER_EXTENTSEQ: 10895 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10896 return (ctl_extent_check_seq(ooa_io, pending_io)); 10897 return (CTL_ACTION_PASS); 10898 case CTL_SER_PASS: 10899 return (CTL_ACTION_PASS); 10900 case CTL_SER_BLOCKOPT: 10901 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10902 SCP_QUEUE_ALG_UNRESTRICTED) 10903 return (CTL_ACTION_BLOCK); 10904 return (CTL_ACTION_PASS); 10905 case CTL_SER_SKIP: 10906 return (CTL_ACTION_SKIP); 10907 default: 10908 panic("%s: Invalid serialization value %d for %d => %d", 10909 __func__, serialize_row[pending_entry->seridx], 10910 pending_entry->seridx, ooa_entry->seridx); 10911 } 10912 10913 return (CTL_ACTION_ERROR); 10914 } 10915 10916 /* 10917 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10918 * Assumptions: 10919 * - pending_io is generally either incoming, or on the blocked queue 10920 * - starting I/O is the I/O we want to start the check with. 10921 */ 10922 static ctl_action 10923 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10924 union ctl_io *starting_io) 10925 { 10926 union ctl_io *ooa_io; 10927 ctl_action action; 10928 10929 mtx_assert(&lun->lun_lock, MA_OWNED); 10930 10931 /* 10932 * Run back along the OOA queue, starting with the current 10933 * blocked I/O and going through every I/O before it on the 10934 * queue. If starting_io is NULL, we'll just end up returning 10935 * CTL_ACTION_PASS. 10936 */ 10937 for (ooa_io = starting_io; ooa_io != NULL; 10938 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10939 ooa_links)){ 10940 10941 /* 10942 * This routine just checks to see whether 10943 * cur_blocked is blocked by ooa_io, which is ahead 10944 * of it in the queue. It doesn't queue/dequeue 10945 * cur_blocked. 10946 */ 10947 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10948 switch (action) { 10949 case CTL_ACTION_BLOCK: 10950 case CTL_ACTION_OVERLAP: 10951 case CTL_ACTION_OVERLAP_TAG: 10952 case CTL_ACTION_SKIP: 10953 case CTL_ACTION_ERROR: 10954 return (action); 10955 break; /* NOTREACHED */ 10956 case CTL_ACTION_PASS: 10957 break; 10958 default: 10959 panic("%s: Invalid action %d\n", __func__, action); 10960 } 10961 } 10962 10963 return (CTL_ACTION_PASS); 10964 } 10965 10966 /* 10967 * Assumptions: 10968 * - An I/O has just completed, and has been removed from the per-LUN OOA 10969 * queue, so some items on the blocked queue may now be unblocked. 10970 */ 10971 static int 10972 ctl_check_blocked(struct ctl_lun *lun) 10973 { 10974 struct ctl_softc *softc = lun->ctl_softc; 10975 union ctl_io *cur_blocked, *next_blocked; 10976 10977 mtx_assert(&lun->lun_lock, MA_OWNED); 10978 10979 /* 10980 * Run forward from the head of the blocked queue, checking each 10981 * entry against the I/Os prior to it on the OOA queue to see if 10982 * there is still any blockage. 10983 * 10984 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10985 * with our removing a variable on it while it is traversing the 10986 * list. 10987 */ 10988 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10989 cur_blocked != NULL; cur_blocked = next_blocked) { 10990 union ctl_io *prev_ooa; 10991 ctl_action action; 10992 10993 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10994 blocked_links); 10995 10996 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10997 ctl_ooaq, ooa_links); 10998 10999 /* 11000 * If cur_blocked happens to be the first item in the OOA 11001 * queue now, prev_ooa will be NULL, and the action 11002 * returned will just be CTL_ACTION_PASS. 11003 */ 11004 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11005 11006 switch (action) { 11007 case CTL_ACTION_BLOCK: 11008 /* Nothing to do here, still blocked */ 11009 break; 11010 case CTL_ACTION_OVERLAP: 11011 case CTL_ACTION_OVERLAP_TAG: 11012 /* 11013 * This shouldn't happen! In theory we've already 11014 * checked this command for overlap... 11015 */ 11016 break; 11017 case CTL_ACTION_PASS: 11018 case CTL_ACTION_SKIP: { 11019 const struct ctl_cmd_entry *entry; 11020 11021 /* 11022 * The skip case shouldn't happen, this transaction 11023 * should have never made it onto the blocked queue. 11024 */ 11025 /* 11026 * This I/O is no longer blocked, we can remove it 11027 * from the blocked queue. Since this is a TAILQ 11028 * (doubly linked list), we can do O(1) removals 11029 * from any place on the list. 11030 */ 11031 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11032 blocked_links); 11033 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11034 11035 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11036 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11037 /* 11038 * Need to send IO back to original side to 11039 * run 11040 */ 11041 union ctl_ha_msg msg_info; 11042 11043 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11044 msg_info.hdr.original_sc = 11045 cur_blocked->io_hdr.remote_io; 11046 msg_info.hdr.serializing_sc = cur_blocked; 11047 msg_info.hdr.msg_type = CTL_MSG_R2R; 11048 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11049 sizeof(msg_info.hdr), M_NOWAIT); 11050 break; 11051 } 11052 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11053 11054 /* 11055 * Check this I/O for LUN state changes that may 11056 * have happened while this command was blocked. 11057 * The LUN state may have been changed by a command 11058 * ahead of us in the queue, so we need to re-check 11059 * for any states that can be caused by SCSI 11060 * commands. 11061 */ 11062 if (ctl_scsiio_lun_check(lun, entry, 11063 &cur_blocked->scsiio) == 0) { 11064 cur_blocked->io_hdr.flags |= 11065 CTL_FLAG_IS_WAS_ON_RTR; 11066 ctl_enqueue_rtr(cur_blocked); 11067 } else 11068 ctl_done(cur_blocked); 11069 break; 11070 } 11071 default: 11072 /* 11073 * This probably shouldn't happen -- we shouldn't 11074 * get CTL_ACTION_ERROR, or anything else. 11075 */ 11076 break; 11077 } 11078 } 11079 11080 return (CTL_RETVAL_COMPLETE); 11081 } 11082 11083 /* 11084 * This routine (with one exception) checks LUN flags that can be set by 11085 * commands ahead of us in the OOA queue. These flags have to be checked 11086 * when a command initially comes in, and when we pull a command off the 11087 * blocked queue and are preparing to execute it. The reason we have to 11088 * check these flags for commands on the blocked queue is that the LUN 11089 * state may have been changed by a command ahead of us while we're on the 11090 * blocked queue. 11091 * 11092 * Ordering is somewhat important with these checks, so please pay 11093 * careful attention to the placement of any new checks. 11094 */ 11095 static int 11096 ctl_scsiio_lun_check(struct ctl_lun *lun, 11097 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11098 { 11099 struct ctl_softc *softc = lun->ctl_softc; 11100 int retval; 11101 uint32_t residx; 11102 11103 retval = 0; 11104 11105 mtx_assert(&lun->lun_lock, MA_OWNED); 11106 11107 /* 11108 * If this shelf is a secondary shelf controller, we may have to 11109 * reject some commands disallowed by HA mode and link state. 11110 */ 11111 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11112 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11113 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11114 ctl_set_lun_unavail(ctsio); 11115 retval = 1; 11116 goto bailout; 11117 } 11118 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11119 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11120 ctl_set_lun_transit(ctsio); 11121 retval = 1; 11122 goto bailout; 11123 } 11124 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11125 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11126 ctl_set_lun_standby(ctsio); 11127 retval = 1; 11128 goto bailout; 11129 } 11130 11131 /* The rest of checks are only done on executing side */ 11132 if (softc->ha_mode == CTL_HA_MODE_XFER) 11133 goto bailout; 11134 } 11135 11136 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11137 if (lun->be_lun && 11138 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11139 ctl_set_hw_write_protected(ctsio); 11140 retval = 1; 11141 goto bailout; 11142 } 11143 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11144 ctl_set_sense(ctsio, /*current_error*/ 1, 11145 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11146 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11147 retval = 1; 11148 goto bailout; 11149 } 11150 } 11151 11152 /* 11153 * Check for a reservation conflict. If this command isn't allowed 11154 * even on reserved LUNs, and if this initiator isn't the one who 11155 * reserved us, reject the command with a reservation conflict. 11156 */ 11157 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11158 if ((lun->flags & CTL_LUN_RESERVED) 11159 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11160 if (lun->res_idx != residx) { 11161 ctl_set_reservation_conflict(ctsio); 11162 retval = 1; 11163 goto bailout; 11164 } 11165 } 11166 11167 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11168 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11169 /* No reservation or command is allowed. */; 11170 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11171 (lun->pr_res_type == SPR_TYPE_WR_EX || 11172 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11173 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11174 /* The command is allowed for Write Exclusive resv. */; 11175 } else { 11176 /* 11177 * if we aren't registered or it's a res holder type 11178 * reservation and this isn't the res holder then set a 11179 * conflict. 11180 */ 11181 if (ctl_get_prkey(lun, residx) == 0 || 11182 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11183 ctl_set_reservation_conflict(ctsio); 11184 retval = 1; 11185 goto bailout; 11186 } 11187 } 11188 11189 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11190 if (lun->flags & CTL_LUN_EJECTED) 11191 ctl_set_lun_ejected(ctsio); 11192 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11193 if (lun->flags & CTL_LUN_REMOVABLE) 11194 ctl_set_lun_no_media(ctsio); 11195 else 11196 ctl_set_lun_int_reqd(ctsio); 11197 } else if (lun->flags & CTL_LUN_STOPPED) 11198 ctl_set_lun_stopped(ctsio); 11199 else 11200 goto bailout; 11201 retval = 1; 11202 goto bailout; 11203 } 11204 11205 bailout: 11206 return (retval); 11207 } 11208 11209 static void 11210 ctl_failover_io(union ctl_io *io, int have_lock) 11211 { 11212 ctl_set_busy(&io->scsiio); 11213 ctl_done(io); 11214 } 11215 11216 static void 11217 ctl_failover_lun(union ctl_io *rio) 11218 { 11219 struct ctl_softc *softc = CTL_SOFTC(rio); 11220 struct ctl_lun *lun; 11221 struct ctl_io_hdr *io, *next_io; 11222 uint32_t targ_lun; 11223 11224 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11225 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11226 11227 /* Find and lock the LUN. */ 11228 mtx_lock(&softc->ctl_lock); 11229 if (targ_lun > ctl_max_luns || 11230 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11231 mtx_unlock(&softc->ctl_lock); 11232 return; 11233 } 11234 mtx_lock(&lun->lun_lock); 11235 mtx_unlock(&softc->ctl_lock); 11236 if (lun->flags & CTL_LUN_DISABLED) { 11237 mtx_unlock(&lun->lun_lock); 11238 return; 11239 } 11240 11241 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11242 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11243 /* We are master */ 11244 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11245 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11246 io->flags |= CTL_FLAG_ABORT; 11247 io->flags |= CTL_FLAG_FAILOVER; 11248 } else { /* This can be only due to DATAMOVE */ 11249 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11250 io->flags &= ~CTL_FLAG_DMA_INPROG; 11251 io->flags |= CTL_FLAG_IO_ACTIVE; 11252 io->port_status = 31340; 11253 ctl_enqueue_isc((union ctl_io *)io); 11254 } 11255 } 11256 /* We are slave */ 11257 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11258 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11259 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11260 io->flags |= CTL_FLAG_FAILOVER; 11261 } else { 11262 ctl_set_busy(&((union ctl_io *)io)-> 11263 scsiio); 11264 ctl_done((union ctl_io *)io); 11265 } 11266 } 11267 } 11268 } else { /* SERIALIZE modes */ 11269 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11270 next_io) { 11271 /* We are master */ 11272 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11273 TAILQ_REMOVE(&lun->blocked_queue, io, 11274 blocked_links); 11275 io->flags &= ~CTL_FLAG_BLOCKED; 11276 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11277 ctl_free_io((union ctl_io *)io); 11278 } 11279 } 11280 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11281 /* We are master */ 11282 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11283 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11284 ctl_free_io((union ctl_io *)io); 11285 } 11286 /* We are slave */ 11287 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11288 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11289 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11290 ctl_set_busy(&((union ctl_io *)io)-> 11291 scsiio); 11292 ctl_done((union ctl_io *)io); 11293 } 11294 } 11295 } 11296 ctl_check_blocked(lun); 11297 } 11298 mtx_unlock(&lun->lun_lock); 11299 } 11300 11301 static int 11302 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11303 { 11304 struct ctl_lun *lun; 11305 const struct ctl_cmd_entry *entry; 11306 uint32_t initidx, targ_lun; 11307 int retval = 0; 11308 11309 lun = NULL; 11310 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11311 if (targ_lun < ctl_max_luns) 11312 lun = softc->ctl_luns[targ_lun]; 11313 if (lun) { 11314 /* 11315 * If the LUN is invalid, pretend that it doesn't exist. 11316 * It will go away as soon as all pending I/O has been 11317 * completed. 11318 */ 11319 mtx_lock(&lun->lun_lock); 11320 if (lun->flags & CTL_LUN_DISABLED) { 11321 mtx_unlock(&lun->lun_lock); 11322 lun = NULL; 11323 } 11324 } 11325 CTL_LUN(ctsio) = lun; 11326 if (lun) { 11327 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11328 11329 /* 11330 * Every I/O goes into the OOA queue for a particular LUN, 11331 * and stays there until completion. 11332 */ 11333 #ifdef CTL_TIME_IO 11334 if (TAILQ_EMPTY(&lun->ooa_queue)) 11335 lun->idle_time += getsbinuptime() - lun->last_busy; 11336 #endif 11337 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11338 } 11339 11340 /* Get command entry and return error if it is unsuppotyed. */ 11341 entry = ctl_validate_command(ctsio); 11342 if (entry == NULL) { 11343 if (lun) 11344 mtx_unlock(&lun->lun_lock); 11345 return (retval); 11346 } 11347 11348 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11349 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11350 11351 /* 11352 * Check to see whether we can send this command to LUNs that don't 11353 * exist. This should pretty much only be the case for inquiry 11354 * and request sense. Further checks, below, really require having 11355 * a LUN, so we can't really check the command anymore. Just put 11356 * it on the rtr queue. 11357 */ 11358 if (lun == NULL) { 11359 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11360 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11361 ctl_enqueue_rtr((union ctl_io *)ctsio); 11362 return (retval); 11363 } 11364 11365 ctl_set_unsupported_lun(ctsio); 11366 ctl_done((union ctl_io *)ctsio); 11367 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11368 return (retval); 11369 } else { 11370 /* 11371 * Make sure we support this particular command on this LUN. 11372 * e.g., we don't support writes to the control LUN. 11373 */ 11374 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11375 mtx_unlock(&lun->lun_lock); 11376 ctl_set_invalid_opcode(ctsio); 11377 ctl_done((union ctl_io *)ctsio); 11378 return (retval); 11379 } 11380 } 11381 11382 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11383 11384 /* 11385 * If we've got a request sense, it'll clear the contingent 11386 * allegiance condition. Otherwise, if we have a CA condition for 11387 * this initiator, clear it, because it sent down a command other 11388 * than request sense. 11389 */ 11390 if (ctsio->cdb[0] != REQUEST_SENSE) { 11391 struct scsi_sense_data *ps; 11392 11393 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11394 if (ps != NULL) 11395 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11396 } 11397 11398 /* 11399 * If the command has this flag set, it handles its own unit 11400 * attention reporting, we shouldn't do anything. Otherwise we 11401 * check for any pending unit attentions, and send them back to the 11402 * initiator. We only do this when a command initially comes in, 11403 * not when we pull it off the blocked queue. 11404 * 11405 * According to SAM-3, section 5.3.2, the order that things get 11406 * presented back to the host is basically unit attentions caused 11407 * by some sort of reset event, busy status, reservation conflicts 11408 * or task set full, and finally any other status. 11409 * 11410 * One issue here is that some of the unit attentions we report 11411 * don't fall into the "reset" category (e.g. "reported luns data 11412 * has changed"). So reporting it here, before the reservation 11413 * check, may be technically wrong. I guess the only thing to do 11414 * would be to check for and report the reset events here, and then 11415 * check for the other unit attention types after we check for a 11416 * reservation conflict. 11417 * 11418 * XXX KDM need to fix this 11419 */ 11420 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11421 ctl_ua_type ua_type; 11422 u_int sense_len = 0; 11423 11424 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11425 &sense_len, SSD_TYPE_NONE); 11426 if (ua_type != CTL_UA_NONE) { 11427 mtx_unlock(&lun->lun_lock); 11428 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11429 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11430 ctsio->sense_len = sense_len; 11431 ctl_done((union ctl_io *)ctsio); 11432 return (retval); 11433 } 11434 } 11435 11436 11437 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11438 mtx_unlock(&lun->lun_lock); 11439 ctl_done((union ctl_io *)ctsio); 11440 return (retval); 11441 } 11442 11443 /* 11444 * XXX CHD this is where we want to send IO to other side if 11445 * this LUN is secondary on this SC. We will need to make a copy 11446 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11447 * the copy we send as FROM_OTHER. 11448 * We also need to stuff the address of the original IO so we can 11449 * find it easily. Something similar will need be done on the other 11450 * side so when we are done we can find the copy. 11451 */ 11452 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11453 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11454 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11455 union ctl_ha_msg msg_info; 11456 int isc_retval; 11457 11458 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11459 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11460 mtx_unlock(&lun->lun_lock); 11461 11462 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11463 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11464 msg_info.hdr.serializing_sc = NULL; 11465 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11466 msg_info.scsi.tag_num = ctsio->tag_num; 11467 msg_info.scsi.tag_type = ctsio->tag_type; 11468 msg_info.scsi.cdb_len = ctsio->cdb_len; 11469 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11470 11471 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11472 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11473 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11474 ctl_set_busy(ctsio); 11475 ctl_done((union ctl_io *)ctsio); 11476 return (retval); 11477 } 11478 return (retval); 11479 } 11480 11481 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11482 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11483 ctl_ooaq, ooa_links))) { 11484 case CTL_ACTION_BLOCK: 11485 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11486 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11487 blocked_links); 11488 mtx_unlock(&lun->lun_lock); 11489 return (retval); 11490 case CTL_ACTION_PASS: 11491 case CTL_ACTION_SKIP: 11492 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11493 mtx_unlock(&lun->lun_lock); 11494 ctl_enqueue_rtr((union ctl_io *)ctsio); 11495 break; 11496 case CTL_ACTION_OVERLAP: 11497 mtx_unlock(&lun->lun_lock); 11498 ctl_set_overlapped_cmd(ctsio); 11499 ctl_done((union ctl_io *)ctsio); 11500 break; 11501 case CTL_ACTION_OVERLAP_TAG: 11502 mtx_unlock(&lun->lun_lock); 11503 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11504 ctl_done((union ctl_io *)ctsio); 11505 break; 11506 case CTL_ACTION_ERROR: 11507 default: 11508 mtx_unlock(&lun->lun_lock); 11509 ctl_set_internal_failure(ctsio, 11510 /*sks_valid*/ 0, 11511 /*retry_count*/ 0); 11512 ctl_done((union ctl_io *)ctsio); 11513 break; 11514 } 11515 return (retval); 11516 } 11517 11518 const struct ctl_cmd_entry * 11519 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11520 { 11521 const struct ctl_cmd_entry *entry; 11522 int service_action; 11523 11524 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11525 if (sa) 11526 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11527 if (entry->flags & CTL_CMD_FLAG_SA5) { 11528 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11529 entry = &((const struct ctl_cmd_entry *) 11530 entry->execute)[service_action]; 11531 } 11532 return (entry); 11533 } 11534 11535 const struct ctl_cmd_entry * 11536 ctl_validate_command(struct ctl_scsiio *ctsio) 11537 { 11538 const struct ctl_cmd_entry *entry; 11539 int i, sa; 11540 uint8_t diff; 11541 11542 entry = ctl_get_cmd_entry(ctsio, &sa); 11543 if (entry->execute == NULL) { 11544 if (sa) 11545 ctl_set_invalid_field(ctsio, 11546 /*sks_valid*/ 1, 11547 /*command*/ 1, 11548 /*field*/ 1, 11549 /*bit_valid*/ 1, 11550 /*bit*/ 4); 11551 else 11552 ctl_set_invalid_opcode(ctsio); 11553 ctl_done((union ctl_io *)ctsio); 11554 return (NULL); 11555 } 11556 KASSERT(entry->length > 0, 11557 ("Not defined length for command 0x%02x/0x%02x", 11558 ctsio->cdb[0], ctsio->cdb[1])); 11559 for (i = 1; i < entry->length; i++) { 11560 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11561 if (diff == 0) 11562 continue; 11563 ctl_set_invalid_field(ctsio, 11564 /*sks_valid*/ 1, 11565 /*command*/ 1, 11566 /*field*/ i, 11567 /*bit_valid*/ 1, 11568 /*bit*/ fls(diff) - 1); 11569 ctl_done((union ctl_io *)ctsio); 11570 return (NULL); 11571 } 11572 return (entry); 11573 } 11574 11575 static int 11576 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11577 { 11578 11579 switch (lun_type) { 11580 case T_DIRECT: 11581 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11582 return (0); 11583 break; 11584 case T_PROCESSOR: 11585 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11586 return (0); 11587 break; 11588 case T_CDROM: 11589 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11590 return (0); 11591 break; 11592 default: 11593 return (0); 11594 } 11595 return (1); 11596 } 11597 11598 static int 11599 ctl_scsiio(struct ctl_scsiio *ctsio) 11600 { 11601 int retval; 11602 const struct ctl_cmd_entry *entry; 11603 11604 retval = CTL_RETVAL_COMPLETE; 11605 11606 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11607 11608 entry = ctl_get_cmd_entry(ctsio, NULL); 11609 11610 /* 11611 * If this I/O has been aborted, just send it straight to 11612 * ctl_done() without executing it. 11613 */ 11614 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11615 ctl_done((union ctl_io *)ctsio); 11616 goto bailout; 11617 } 11618 11619 /* 11620 * All the checks should have been handled by ctl_scsiio_precheck(). 11621 * We should be clear now to just execute the I/O. 11622 */ 11623 retval = entry->execute(ctsio); 11624 11625 bailout: 11626 return (retval); 11627 } 11628 11629 static int 11630 ctl_target_reset(union ctl_io *io) 11631 { 11632 struct ctl_softc *softc = CTL_SOFTC(io); 11633 struct ctl_port *port = CTL_PORT(io); 11634 struct ctl_lun *lun; 11635 uint32_t initidx; 11636 ctl_ua_type ua_type; 11637 11638 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11639 union ctl_ha_msg msg_info; 11640 11641 msg_info.hdr.nexus = io->io_hdr.nexus; 11642 msg_info.task.task_action = io->taskio.task_action; 11643 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11644 msg_info.hdr.original_sc = NULL; 11645 msg_info.hdr.serializing_sc = NULL; 11646 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11647 sizeof(msg_info.task), M_WAITOK); 11648 } 11649 11650 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11651 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11652 ua_type = CTL_UA_TARG_RESET; 11653 else 11654 ua_type = CTL_UA_BUS_RESET; 11655 mtx_lock(&softc->ctl_lock); 11656 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11657 if (port != NULL && 11658 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11659 continue; 11660 ctl_do_lun_reset(lun, initidx, ua_type); 11661 } 11662 mtx_unlock(&softc->ctl_lock); 11663 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11664 return (0); 11665 } 11666 11667 /* 11668 * The LUN should always be set. The I/O is optional, and is used to 11669 * distinguish between I/Os sent by this initiator, and by other 11670 * initiators. We set unit attention for initiators other than this one. 11671 * SAM-3 is vague on this point. It does say that a unit attention should 11672 * be established for other initiators when a LUN is reset (see section 11673 * 5.7.3), but it doesn't specifically say that the unit attention should 11674 * be established for this particular initiator when a LUN is reset. Here 11675 * is the relevant text, from SAM-3 rev 8: 11676 * 11677 * 5.7.2 When a SCSI initiator port aborts its own tasks 11678 * 11679 * When a SCSI initiator port causes its own task(s) to be aborted, no 11680 * notification that the task(s) have been aborted shall be returned to 11681 * the SCSI initiator port other than the completion response for the 11682 * command or task management function action that caused the task(s) to 11683 * be aborted and notification(s) associated with related effects of the 11684 * action (e.g., a reset unit attention condition). 11685 * 11686 * XXX KDM for now, we're setting unit attention for all initiators. 11687 */ 11688 static void 11689 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11690 { 11691 union ctl_io *xio; 11692 int i; 11693 11694 mtx_lock(&lun->lun_lock); 11695 /* Abort tasks. */ 11696 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11697 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11698 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11699 } 11700 /* Clear CA. */ 11701 for (i = 0; i < ctl_max_ports; i++) { 11702 free(lun->pending_sense[i], M_CTL); 11703 lun->pending_sense[i] = NULL; 11704 } 11705 /* Clear reservation. */ 11706 lun->flags &= ~CTL_LUN_RESERVED; 11707 /* Clear prevent media removal. */ 11708 if (lun->prevent) { 11709 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11710 ctl_clear_mask(lun->prevent, i); 11711 lun->prevent_count = 0; 11712 } 11713 /* Clear TPC status */ 11714 ctl_tpc_lun_clear(lun, -1); 11715 /* Establish UA. */ 11716 #if 0 11717 ctl_est_ua_all(lun, initidx, ua_type); 11718 #else 11719 ctl_est_ua_all(lun, -1, ua_type); 11720 #endif 11721 mtx_unlock(&lun->lun_lock); 11722 } 11723 11724 static int 11725 ctl_lun_reset(union ctl_io *io) 11726 { 11727 struct ctl_softc *softc = CTL_SOFTC(io); 11728 struct ctl_lun *lun; 11729 uint32_t targ_lun, initidx; 11730 11731 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11732 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11733 mtx_lock(&softc->ctl_lock); 11734 if (targ_lun >= ctl_max_luns || 11735 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11736 mtx_unlock(&softc->ctl_lock); 11737 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11738 return (1); 11739 } 11740 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11741 mtx_unlock(&softc->ctl_lock); 11742 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11743 11744 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11745 union ctl_ha_msg msg_info; 11746 11747 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11748 msg_info.hdr.nexus = io->io_hdr.nexus; 11749 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11750 msg_info.hdr.original_sc = NULL; 11751 msg_info.hdr.serializing_sc = NULL; 11752 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11753 sizeof(msg_info.task), M_WAITOK); 11754 } 11755 return (0); 11756 } 11757 11758 static void 11759 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11760 int other_sc) 11761 { 11762 union ctl_io *xio; 11763 11764 mtx_assert(&lun->lun_lock, MA_OWNED); 11765 11766 /* 11767 * Run through the OOA queue and attempt to find the given I/O. 11768 * The target port, initiator ID, tag type and tag number have to 11769 * match the values that we got from the initiator. If we have an 11770 * untagged command to abort, simply abort the first untagged command 11771 * we come to. We only allow one untagged command at a time of course. 11772 */ 11773 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11774 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11775 11776 if ((targ_port == UINT32_MAX || 11777 targ_port == xio->io_hdr.nexus.targ_port) && 11778 (init_id == UINT32_MAX || 11779 init_id == xio->io_hdr.nexus.initid)) { 11780 if (targ_port != xio->io_hdr.nexus.targ_port || 11781 init_id != xio->io_hdr.nexus.initid) 11782 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11783 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11784 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11785 union ctl_ha_msg msg_info; 11786 11787 msg_info.hdr.nexus = xio->io_hdr.nexus; 11788 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11789 msg_info.task.tag_num = xio->scsiio.tag_num; 11790 msg_info.task.tag_type = xio->scsiio.tag_type; 11791 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11792 msg_info.hdr.original_sc = NULL; 11793 msg_info.hdr.serializing_sc = NULL; 11794 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11795 sizeof(msg_info.task), M_NOWAIT); 11796 } 11797 } 11798 } 11799 } 11800 11801 static int 11802 ctl_abort_task_set(union ctl_io *io) 11803 { 11804 struct ctl_softc *softc = CTL_SOFTC(io); 11805 struct ctl_lun *lun; 11806 uint32_t targ_lun; 11807 11808 /* 11809 * Look up the LUN. 11810 */ 11811 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11812 mtx_lock(&softc->ctl_lock); 11813 if (targ_lun >= ctl_max_luns || 11814 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11815 mtx_unlock(&softc->ctl_lock); 11816 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11817 return (1); 11818 } 11819 11820 mtx_lock(&lun->lun_lock); 11821 mtx_unlock(&softc->ctl_lock); 11822 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11823 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11824 io->io_hdr.nexus.initid, 11825 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11826 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11827 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11828 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11829 } 11830 mtx_unlock(&lun->lun_lock); 11831 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11832 return (0); 11833 } 11834 11835 static void 11836 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 11837 ctl_ua_type ua_type) 11838 { 11839 struct ctl_lun *lun; 11840 struct scsi_sense_data *ps; 11841 uint32_t p, i; 11842 11843 p = initidx / CTL_MAX_INIT_PER_PORT; 11844 i = initidx % CTL_MAX_INIT_PER_PORT; 11845 mtx_lock(&softc->ctl_lock); 11846 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11847 mtx_lock(&lun->lun_lock); 11848 /* Abort tasks. */ 11849 ctl_abort_tasks_lun(lun, p, i, 1); 11850 /* Clear CA. */ 11851 ps = lun->pending_sense[p]; 11852 if (ps != NULL) 11853 ps[i].error_code = 0; 11854 /* Clear reservation. */ 11855 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11856 lun->flags &= ~CTL_LUN_RESERVED; 11857 /* Clear prevent media removal. */ 11858 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 11859 ctl_clear_mask(lun->prevent, initidx); 11860 lun->prevent_count--; 11861 } 11862 /* Clear TPC status */ 11863 ctl_tpc_lun_clear(lun, initidx); 11864 /* Establish UA. */ 11865 ctl_est_ua(lun, initidx, ua_type); 11866 mtx_unlock(&lun->lun_lock); 11867 } 11868 mtx_unlock(&softc->ctl_lock); 11869 } 11870 11871 static int 11872 ctl_i_t_nexus_reset(union ctl_io *io) 11873 { 11874 struct ctl_softc *softc = CTL_SOFTC(io); 11875 uint32_t initidx; 11876 11877 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11878 union ctl_ha_msg msg_info; 11879 11880 msg_info.hdr.nexus = io->io_hdr.nexus; 11881 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11882 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11883 msg_info.hdr.original_sc = NULL; 11884 msg_info.hdr.serializing_sc = NULL; 11885 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11886 sizeof(msg_info.task), M_WAITOK); 11887 } 11888 11889 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11890 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 11891 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11892 return (0); 11893 } 11894 11895 static int 11896 ctl_abort_task(union ctl_io *io) 11897 { 11898 struct ctl_softc *softc = CTL_SOFTC(io); 11899 union ctl_io *xio; 11900 struct ctl_lun *lun; 11901 #if 0 11902 struct sbuf sb; 11903 char printbuf[128]; 11904 #endif 11905 int found; 11906 uint32_t targ_lun; 11907 11908 found = 0; 11909 11910 /* 11911 * Look up the LUN. 11912 */ 11913 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11914 mtx_lock(&softc->ctl_lock); 11915 if (targ_lun >= ctl_max_luns || 11916 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11917 mtx_unlock(&softc->ctl_lock); 11918 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11919 return (1); 11920 } 11921 11922 #if 0 11923 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11924 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11925 #endif 11926 11927 mtx_lock(&lun->lun_lock); 11928 mtx_unlock(&softc->ctl_lock); 11929 /* 11930 * Run through the OOA queue and attempt to find the given I/O. 11931 * The target port, initiator ID, tag type and tag number have to 11932 * match the values that we got from the initiator. If we have an 11933 * untagged command to abort, simply abort the first untagged command 11934 * we come to. We only allow one untagged command at a time of course. 11935 */ 11936 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11937 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11938 #if 0 11939 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11940 11941 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11942 lun->lun, xio->scsiio.tag_num, 11943 xio->scsiio.tag_type, 11944 (xio->io_hdr.blocked_links.tqe_prev 11945 == NULL) ? "" : " BLOCKED", 11946 (xio->io_hdr.flags & 11947 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11948 (xio->io_hdr.flags & 11949 CTL_FLAG_ABORT) ? " ABORT" : "", 11950 (xio->io_hdr.flags & 11951 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11952 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11953 sbuf_finish(&sb); 11954 printf("%s\n", sbuf_data(&sb)); 11955 #endif 11956 11957 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11958 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11959 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11960 continue; 11961 11962 /* 11963 * If the abort says that the task is untagged, the 11964 * task in the queue must be untagged. Otherwise, 11965 * we just check to see whether the tag numbers 11966 * match. This is because the QLogic firmware 11967 * doesn't pass back the tag type in an abort 11968 * request. 11969 */ 11970 #if 0 11971 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11972 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11973 || (xio->scsiio.tag_num == io->taskio.tag_num)) 11974 #endif 11975 /* 11976 * XXX KDM we've got problems with FC, because it 11977 * doesn't send down a tag type with aborts. So we 11978 * can only really go by the tag number... 11979 * This may cause problems with parallel SCSI. 11980 * Need to figure that out!! 11981 */ 11982 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11983 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11984 found = 1; 11985 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 11986 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11987 union ctl_ha_msg msg_info; 11988 11989 msg_info.hdr.nexus = io->io_hdr.nexus; 11990 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11991 msg_info.task.tag_num = io->taskio.tag_num; 11992 msg_info.task.tag_type = io->taskio.tag_type; 11993 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11994 msg_info.hdr.original_sc = NULL; 11995 msg_info.hdr.serializing_sc = NULL; 11996 #if 0 11997 printf("Sent Abort to other side\n"); 11998 #endif 11999 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12000 sizeof(msg_info.task), M_NOWAIT); 12001 } 12002 #if 0 12003 printf("ctl_abort_task: found I/O to abort\n"); 12004 #endif 12005 } 12006 } 12007 mtx_unlock(&lun->lun_lock); 12008 12009 if (found == 0) { 12010 /* 12011 * This isn't really an error. It's entirely possible for 12012 * the abort and command completion to cross on the wire. 12013 * This is more of an informative/diagnostic error. 12014 */ 12015 #if 0 12016 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12017 "%u:%u:%u tag %d type %d\n", 12018 io->io_hdr.nexus.initid, 12019 io->io_hdr.nexus.targ_port, 12020 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12021 io->taskio.tag_type); 12022 #endif 12023 } 12024 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12025 return (0); 12026 } 12027 12028 static int 12029 ctl_query_task(union ctl_io *io, int task_set) 12030 { 12031 struct ctl_softc *softc = CTL_SOFTC(io); 12032 union ctl_io *xio; 12033 struct ctl_lun *lun; 12034 int found = 0; 12035 uint32_t targ_lun; 12036 12037 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12038 mtx_lock(&softc->ctl_lock); 12039 if (targ_lun >= ctl_max_luns || 12040 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12041 mtx_unlock(&softc->ctl_lock); 12042 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12043 return (1); 12044 } 12045 mtx_lock(&lun->lun_lock); 12046 mtx_unlock(&softc->ctl_lock); 12047 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12048 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12049 12050 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12051 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12052 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12053 continue; 12054 12055 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12056 found = 1; 12057 break; 12058 } 12059 } 12060 mtx_unlock(&lun->lun_lock); 12061 if (found) 12062 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12063 else 12064 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12065 return (0); 12066 } 12067 12068 static int 12069 ctl_query_async_event(union ctl_io *io) 12070 { 12071 struct ctl_softc *softc = CTL_SOFTC(io); 12072 struct ctl_lun *lun; 12073 ctl_ua_type ua; 12074 uint32_t targ_lun, initidx; 12075 12076 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12077 mtx_lock(&softc->ctl_lock); 12078 if (targ_lun >= ctl_max_luns || 12079 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12080 mtx_unlock(&softc->ctl_lock); 12081 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12082 return (1); 12083 } 12084 mtx_lock(&lun->lun_lock); 12085 mtx_unlock(&softc->ctl_lock); 12086 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12087 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12088 mtx_unlock(&lun->lun_lock); 12089 if (ua != CTL_UA_NONE) 12090 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12091 else 12092 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12093 return (0); 12094 } 12095 12096 static void 12097 ctl_run_task(union ctl_io *io) 12098 { 12099 int retval = 1; 12100 12101 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12102 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12103 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12104 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12105 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12106 switch (io->taskio.task_action) { 12107 case CTL_TASK_ABORT_TASK: 12108 retval = ctl_abort_task(io); 12109 break; 12110 case CTL_TASK_ABORT_TASK_SET: 12111 case CTL_TASK_CLEAR_TASK_SET: 12112 retval = ctl_abort_task_set(io); 12113 break; 12114 case CTL_TASK_CLEAR_ACA: 12115 break; 12116 case CTL_TASK_I_T_NEXUS_RESET: 12117 retval = ctl_i_t_nexus_reset(io); 12118 break; 12119 case CTL_TASK_LUN_RESET: 12120 retval = ctl_lun_reset(io); 12121 break; 12122 case CTL_TASK_TARGET_RESET: 12123 case CTL_TASK_BUS_RESET: 12124 retval = ctl_target_reset(io); 12125 break; 12126 case CTL_TASK_PORT_LOGIN: 12127 break; 12128 case CTL_TASK_PORT_LOGOUT: 12129 break; 12130 case CTL_TASK_QUERY_TASK: 12131 retval = ctl_query_task(io, 0); 12132 break; 12133 case CTL_TASK_QUERY_TASK_SET: 12134 retval = ctl_query_task(io, 1); 12135 break; 12136 case CTL_TASK_QUERY_ASYNC_EVENT: 12137 retval = ctl_query_async_event(io); 12138 break; 12139 default: 12140 printf("%s: got unknown task management event %d\n", 12141 __func__, io->taskio.task_action); 12142 break; 12143 } 12144 if (retval == 0) 12145 io->io_hdr.status = CTL_SUCCESS; 12146 else 12147 io->io_hdr.status = CTL_ERROR; 12148 ctl_done(io); 12149 } 12150 12151 /* 12152 * For HA operation. Handle commands that come in from the other 12153 * controller. 12154 */ 12155 static void 12156 ctl_handle_isc(union ctl_io *io) 12157 { 12158 struct ctl_softc *softc = CTL_SOFTC(io); 12159 struct ctl_lun *lun; 12160 const struct ctl_cmd_entry *entry; 12161 uint32_t targ_lun; 12162 12163 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12164 switch (io->io_hdr.msg_type) { 12165 case CTL_MSG_SERIALIZE: 12166 ctl_serialize_other_sc_cmd(&io->scsiio); 12167 break; 12168 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12169 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12170 if (targ_lun >= ctl_max_luns || 12171 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12172 ctl_done(io); 12173 break; 12174 } 12175 mtx_lock(&lun->lun_lock); 12176 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12177 mtx_unlock(&lun->lun_lock); 12178 ctl_done(io); 12179 break; 12180 } 12181 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12182 mtx_unlock(&lun->lun_lock); 12183 ctl_enqueue_rtr(io); 12184 break; 12185 case CTL_MSG_FINISH_IO: 12186 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12187 ctl_done(io); 12188 break; 12189 } 12190 if (targ_lun >= ctl_max_luns || 12191 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12192 ctl_free_io(io); 12193 break; 12194 } 12195 mtx_lock(&lun->lun_lock); 12196 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12197 ctl_check_blocked(lun); 12198 mtx_unlock(&lun->lun_lock); 12199 ctl_free_io(io); 12200 break; 12201 case CTL_MSG_PERS_ACTION: 12202 ctl_hndl_per_res_out_on_other_sc(io); 12203 ctl_free_io(io); 12204 break; 12205 case CTL_MSG_BAD_JUJU: 12206 ctl_done(io); 12207 break; 12208 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12209 ctl_datamove_remote(io); 12210 break; 12211 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12212 io->scsiio.be_move_done(io); 12213 break; 12214 case CTL_MSG_FAILOVER: 12215 ctl_failover_lun(io); 12216 ctl_free_io(io); 12217 break; 12218 default: 12219 printf("%s: Invalid message type %d\n", 12220 __func__, io->io_hdr.msg_type); 12221 ctl_free_io(io); 12222 break; 12223 } 12224 12225 } 12226 12227 12228 /* 12229 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12230 * there is no match. 12231 */ 12232 static ctl_lun_error_pattern 12233 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12234 { 12235 const struct ctl_cmd_entry *entry; 12236 ctl_lun_error_pattern filtered_pattern, pattern; 12237 12238 pattern = desc->error_pattern; 12239 12240 /* 12241 * XXX KDM we need more data passed into this function to match a 12242 * custom pattern, and we actually need to implement custom pattern 12243 * matching. 12244 */ 12245 if (pattern & CTL_LUN_PAT_CMD) 12246 return (CTL_LUN_PAT_CMD); 12247 12248 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12249 return (CTL_LUN_PAT_ANY); 12250 12251 entry = ctl_get_cmd_entry(ctsio, NULL); 12252 12253 filtered_pattern = entry->pattern & pattern; 12254 12255 /* 12256 * If the user requested specific flags in the pattern (e.g. 12257 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12258 * flags. 12259 * 12260 * If the user did not specify any flags, it doesn't matter whether 12261 * or not the command supports the flags. 12262 */ 12263 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12264 (pattern & ~CTL_LUN_PAT_MASK)) 12265 return (CTL_LUN_PAT_NONE); 12266 12267 /* 12268 * If the user asked for a range check, see if the requested LBA 12269 * range overlaps with this command's LBA range. 12270 */ 12271 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12272 uint64_t lba1; 12273 uint64_t len1; 12274 ctl_action action; 12275 int retval; 12276 12277 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12278 if (retval != 0) 12279 return (CTL_LUN_PAT_NONE); 12280 12281 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12282 desc->lba_range.len, FALSE); 12283 /* 12284 * A "pass" means that the LBA ranges don't overlap, so 12285 * this doesn't match the user's range criteria. 12286 */ 12287 if (action == CTL_ACTION_PASS) 12288 return (CTL_LUN_PAT_NONE); 12289 } 12290 12291 return (filtered_pattern); 12292 } 12293 12294 static void 12295 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12296 { 12297 struct ctl_error_desc *desc, *desc2; 12298 12299 mtx_assert(&lun->lun_lock, MA_OWNED); 12300 12301 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12302 ctl_lun_error_pattern pattern; 12303 /* 12304 * Check to see whether this particular command matches 12305 * the pattern in the descriptor. 12306 */ 12307 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12308 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12309 continue; 12310 12311 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12312 case CTL_LUN_INJ_ABORTED: 12313 ctl_set_aborted(&io->scsiio); 12314 break; 12315 case CTL_LUN_INJ_MEDIUM_ERR: 12316 ctl_set_medium_error(&io->scsiio, 12317 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12318 CTL_FLAG_DATA_OUT); 12319 break; 12320 case CTL_LUN_INJ_UA: 12321 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12322 * OCCURRED */ 12323 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12324 break; 12325 case CTL_LUN_INJ_CUSTOM: 12326 /* 12327 * We're assuming the user knows what he is doing. 12328 * Just copy the sense information without doing 12329 * checks. 12330 */ 12331 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12332 MIN(sizeof(desc->custom_sense), 12333 sizeof(io->scsiio.sense_data))); 12334 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12335 io->scsiio.sense_len = SSD_FULL_SIZE; 12336 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12337 break; 12338 case CTL_LUN_INJ_NONE: 12339 default: 12340 /* 12341 * If this is an error injection type we don't know 12342 * about, clear the continuous flag (if it is set) 12343 * so it will get deleted below. 12344 */ 12345 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12346 break; 12347 } 12348 /* 12349 * By default, each error injection action is a one-shot 12350 */ 12351 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12352 continue; 12353 12354 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12355 12356 free(desc, M_CTL); 12357 } 12358 } 12359 12360 #ifdef CTL_IO_DELAY 12361 static void 12362 ctl_datamove_timer_wakeup(void *arg) 12363 { 12364 union ctl_io *io; 12365 12366 io = (union ctl_io *)arg; 12367 12368 ctl_datamove(io); 12369 } 12370 #endif /* CTL_IO_DELAY */ 12371 12372 void 12373 ctl_datamove(union ctl_io *io) 12374 { 12375 void (*fe_datamove)(union ctl_io *io); 12376 12377 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12378 12379 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12380 12381 /* No data transferred yet. Frontend must update this when done. */ 12382 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12383 12384 #ifdef CTL_TIME_IO 12385 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12386 char str[256]; 12387 char path_str[64]; 12388 struct sbuf sb; 12389 12390 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12391 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12392 12393 sbuf_cat(&sb, path_str); 12394 switch (io->io_hdr.io_type) { 12395 case CTL_IO_SCSI: 12396 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12397 sbuf_printf(&sb, "\n"); 12398 sbuf_cat(&sb, path_str); 12399 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12400 io->scsiio.tag_num, io->scsiio.tag_type); 12401 break; 12402 case CTL_IO_TASK: 12403 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12404 "Tag Type: %d\n", io->taskio.task_action, 12405 io->taskio.tag_num, io->taskio.tag_type); 12406 break; 12407 default: 12408 panic("%s: Invalid CTL I/O type %d\n", 12409 __func__, io->io_hdr.io_type); 12410 } 12411 sbuf_cat(&sb, path_str); 12412 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12413 (intmax_t)time_uptime - io->io_hdr.start_time); 12414 sbuf_finish(&sb); 12415 printf("%s", sbuf_data(&sb)); 12416 } 12417 #endif /* CTL_TIME_IO */ 12418 12419 #ifdef CTL_IO_DELAY 12420 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12421 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12422 } else { 12423 struct ctl_lun *lun; 12424 12425 lun = CTL_LUN(io); 12426 if ((lun != NULL) 12427 && (lun->delay_info.datamove_delay > 0)) { 12428 12429 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12430 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12431 callout_reset(&io->io_hdr.delay_callout, 12432 lun->delay_info.datamove_delay * hz, 12433 ctl_datamove_timer_wakeup, io); 12434 if (lun->delay_info.datamove_type == 12435 CTL_DELAY_TYPE_ONESHOT) 12436 lun->delay_info.datamove_delay = 0; 12437 return; 12438 } 12439 } 12440 #endif 12441 12442 /* 12443 * This command has been aborted. Set the port status, so we fail 12444 * the data move. 12445 */ 12446 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12447 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12448 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12449 io->io_hdr.nexus.targ_port, 12450 io->io_hdr.nexus.targ_lun); 12451 io->io_hdr.port_status = 31337; 12452 /* 12453 * Note that the backend, in this case, will get the 12454 * callback in its context. In other cases it may get 12455 * called in the frontend's interrupt thread context. 12456 */ 12457 io->scsiio.be_move_done(io); 12458 return; 12459 } 12460 12461 /* Don't confuse frontend with zero length data move. */ 12462 if (io->scsiio.kern_data_len == 0) { 12463 io->scsiio.be_move_done(io); 12464 return; 12465 } 12466 12467 fe_datamove = CTL_PORT(io)->fe_datamove; 12468 fe_datamove(io); 12469 } 12470 12471 static void 12472 ctl_send_datamove_done(union ctl_io *io, int have_lock) 12473 { 12474 union ctl_ha_msg msg; 12475 #ifdef CTL_TIME_IO 12476 struct bintime cur_bt; 12477 #endif 12478 12479 memset(&msg, 0, sizeof(msg)); 12480 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12481 msg.hdr.original_sc = io; 12482 msg.hdr.serializing_sc = io->io_hdr.remote_io; 12483 msg.hdr.nexus = io->io_hdr.nexus; 12484 msg.hdr.status = io->io_hdr.status; 12485 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12486 msg.scsi.tag_num = io->scsiio.tag_num; 12487 msg.scsi.tag_type = io->scsiio.tag_type; 12488 msg.scsi.scsi_status = io->scsiio.scsi_status; 12489 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12490 io->scsiio.sense_len); 12491 msg.scsi.sense_len = io->scsiio.sense_len; 12492 msg.scsi.port_status = io->io_hdr.port_status; 12493 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12494 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12495 ctl_failover_io(io, /*have_lock*/ have_lock); 12496 return; 12497 } 12498 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12499 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12500 msg.scsi.sense_len, M_WAITOK); 12501 12502 #ifdef CTL_TIME_IO 12503 getbinuptime(&cur_bt); 12504 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12505 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12506 #endif 12507 io->io_hdr.num_dmas++; 12508 } 12509 12510 /* 12511 * The DMA to the remote side is done, now we need to tell the other side 12512 * we're done so it can continue with its data movement. 12513 */ 12514 static void 12515 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12516 { 12517 union ctl_io *io; 12518 uint32_t i; 12519 12520 io = rq->context; 12521 12522 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12523 printf("%s: ISC DMA write failed with error %d", __func__, 12524 rq->ret); 12525 ctl_set_internal_failure(&io->scsiio, 12526 /*sks_valid*/ 1, 12527 /*retry_count*/ rq->ret); 12528 } 12529 12530 ctl_dt_req_free(rq); 12531 12532 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12533 free(CTL_LSGLT(io)[i].addr, M_CTL); 12534 free(CTL_RSGL(io), M_CTL); 12535 CTL_RSGL(io) = NULL; 12536 CTL_LSGL(io) = NULL; 12537 12538 /* 12539 * The data is in local and remote memory, so now we need to send 12540 * status (good or back) back to the other side. 12541 */ 12542 ctl_send_datamove_done(io, /*have_lock*/ 0); 12543 } 12544 12545 /* 12546 * We've moved the data from the host/controller into local memory. Now we 12547 * need to push it over to the remote controller's memory. 12548 */ 12549 static int 12550 ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12551 { 12552 int retval; 12553 12554 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12555 ctl_datamove_remote_write_cb); 12556 return (retval); 12557 } 12558 12559 static void 12560 ctl_datamove_remote_write(union ctl_io *io) 12561 { 12562 int retval; 12563 void (*fe_datamove)(union ctl_io *io); 12564 12565 /* 12566 * - Get the data from the host/HBA into local memory. 12567 * - DMA memory from the local controller to the remote controller. 12568 * - Send status back to the remote controller. 12569 */ 12570 12571 retval = ctl_datamove_remote_sgl_setup(io); 12572 if (retval != 0) 12573 return; 12574 12575 /* Switch the pointer over so the FETD knows what to do */ 12576 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12577 12578 /* 12579 * Use a custom move done callback, since we need to send completion 12580 * back to the other controller, not to the backend on this side. 12581 */ 12582 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12583 12584 fe_datamove = CTL_PORT(io)->fe_datamove; 12585 fe_datamove(io); 12586 } 12587 12588 static int 12589 ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12590 { 12591 #if 0 12592 char str[256]; 12593 char path_str[64]; 12594 struct sbuf sb; 12595 #endif 12596 uint32_t i; 12597 12598 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12599 free(CTL_LSGLT(io)[i].addr, M_CTL); 12600 free(CTL_RSGL(io), M_CTL); 12601 CTL_RSGL(io) = NULL; 12602 CTL_LSGL(io) = NULL; 12603 12604 #if 0 12605 scsi_path_string(io, path_str, sizeof(path_str)); 12606 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12607 sbuf_cat(&sb, path_str); 12608 scsi_command_string(&io->scsiio, NULL, &sb); 12609 sbuf_printf(&sb, "\n"); 12610 sbuf_cat(&sb, path_str); 12611 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12612 io->scsiio.tag_num, io->scsiio.tag_type); 12613 sbuf_cat(&sb, path_str); 12614 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12615 io->io_hdr.flags, io->io_hdr.status); 12616 sbuf_finish(&sb); 12617 printk("%s", sbuf_data(&sb)); 12618 #endif 12619 12620 12621 /* 12622 * The read is done, now we need to send status (good or bad) back 12623 * to the other side. 12624 */ 12625 ctl_send_datamove_done(io, /*have_lock*/ 0); 12626 12627 return (0); 12628 } 12629 12630 static void 12631 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12632 { 12633 union ctl_io *io; 12634 void (*fe_datamove)(union ctl_io *io); 12635 12636 io = rq->context; 12637 12638 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12639 printf("%s: ISC DMA read failed with error %d\n", __func__, 12640 rq->ret); 12641 ctl_set_internal_failure(&io->scsiio, 12642 /*sks_valid*/ 1, 12643 /*retry_count*/ rq->ret); 12644 } 12645 12646 ctl_dt_req_free(rq); 12647 12648 /* Switch the pointer over so the FETD knows what to do */ 12649 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 12650 12651 /* 12652 * Use a custom move done callback, since we need to send completion 12653 * back to the other controller, not to the backend on this side. 12654 */ 12655 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12656 12657 /* XXX KDM add checks like the ones in ctl_datamove? */ 12658 12659 fe_datamove = CTL_PORT(io)->fe_datamove; 12660 fe_datamove(io); 12661 } 12662 12663 static int 12664 ctl_datamove_remote_sgl_setup(union ctl_io *io) 12665 { 12666 struct ctl_sg_entry *local_sglist; 12667 uint32_t len_to_go; 12668 int retval; 12669 int i; 12670 12671 retval = 0; 12672 local_sglist = CTL_LSGL(io); 12673 len_to_go = io->scsiio.kern_data_len; 12674 12675 /* 12676 * The difficult thing here is that the size of the various 12677 * S/G segments may be different than the size from the 12678 * remote controller. That'll make it harder when DMAing 12679 * the data back to the other side. 12680 */ 12681 for (i = 0; len_to_go > 0; i++) { 12682 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12683 local_sglist[i].addr = 12684 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12685 12686 len_to_go -= local_sglist[i].len; 12687 } 12688 /* 12689 * Reset the number of S/G entries accordingly. The original 12690 * number of S/G entries is available in rem_sg_entries. 12691 */ 12692 io->scsiio.kern_sg_entries = i; 12693 12694 #if 0 12695 printf("%s: kern_sg_entries = %d\n", __func__, 12696 io->scsiio.kern_sg_entries); 12697 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12698 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12699 local_sglist[i].addr, local_sglist[i].len); 12700 #endif 12701 12702 return (retval); 12703 } 12704 12705 static int 12706 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12707 ctl_ha_dt_cb callback) 12708 { 12709 struct ctl_ha_dt_req *rq; 12710 struct ctl_sg_entry *remote_sglist, *local_sglist; 12711 uint32_t local_used, remote_used, total_used; 12712 int i, j, isc_ret; 12713 12714 rq = ctl_dt_req_alloc(); 12715 12716 /* 12717 * If we failed to allocate the request, and if the DMA didn't fail 12718 * anyway, set busy status. This is just a resource allocation 12719 * failure. 12720 */ 12721 if ((rq == NULL) 12722 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12723 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12724 ctl_set_busy(&io->scsiio); 12725 12726 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12727 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12728 12729 if (rq != NULL) 12730 ctl_dt_req_free(rq); 12731 12732 /* 12733 * The data move failed. We need to return status back 12734 * to the other controller. No point in trying to DMA 12735 * data to the remote controller. 12736 */ 12737 12738 ctl_send_datamove_done(io, /*have_lock*/ 0); 12739 12740 return (1); 12741 } 12742 12743 local_sglist = CTL_LSGL(io); 12744 remote_sglist = CTL_RSGL(io); 12745 local_used = 0; 12746 remote_used = 0; 12747 total_used = 0; 12748 12749 /* 12750 * Pull/push the data over the wire from/to the other controller. 12751 * This takes into account the possibility that the local and 12752 * remote sglists may not be identical in terms of the size of 12753 * the elements and the number of elements. 12754 * 12755 * One fundamental assumption here is that the length allocated for 12756 * both the local and remote sglists is identical. Otherwise, we've 12757 * essentially got a coding error of some sort. 12758 */ 12759 isc_ret = CTL_HA_STATUS_SUCCESS; 12760 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12761 uint32_t cur_len; 12762 uint8_t *tmp_ptr; 12763 12764 rq->command = command; 12765 rq->context = io; 12766 12767 /* 12768 * Both pointers should be aligned. But it is possible 12769 * that the allocation length is not. They should both 12770 * also have enough slack left over at the end, though, 12771 * to round up to the next 8 byte boundary. 12772 */ 12773 cur_len = MIN(local_sglist[i].len - local_used, 12774 remote_sglist[j].len - remote_used); 12775 rq->size = cur_len; 12776 12777 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12778 tmp_ptr += local_used; 12779 12780 #if 0 12781 /* Use physical addresses when talking to ISC hardware */ 12782 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12783 /* XXX KDM use busdma */ 12784 rq->local = vtophys(tmp_ptr); 12785 } else 12786 rq->local = tmp_ptr; 12787 #else 12788 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12789 ("HA does not support BUS_ADDR")); 12790 rq->local = tmp_ptr; 12791 #endif 12792 12793 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12794 tmp_ptr += remote_used; 12795 rq->remote = tmp_ptr; 12796 12797 rq->callback = NULL; 12798 12799 local_used += cur_len; 12800 if (local_used >= local_sglist[i].len) { 12801 i++; 12802 local_used = 0; 12803 } 12804 12805 remote_used += cur_len; 12806 if (remote_used >= remote_sglist[j].len) { 12807 j++; 12808 remote_used = 0; 12809 } 12810 total_used += cur_len; 12811 12812 if (total_used >= io->scsiio.kern_data_len) 12813 rq->callback = callback; 12814 12815 #if 0 12816 printf("%s: %s: local %p remote %p size %d\n", __func__, 12817 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12818 rq->local, rq->remote, rq->size); 12819 #endif 12820 12821 isc_ret = ctl_dt_single(rq); 12822 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12823 break; 12824 } 12825 if (isc_ret != CTL_HA_STATUS_WAIT) { 12826 rq->ret = isc_ret; 12827 callback(rq); 12828 } 12829 12830 return (0); 12831 } 12832 12833 static void 12834 ctl_datamove_remote_read(union ctl_io *io) 12835 { 12836 int retval; 12837 uint32_t i; 12838 12839 /* 12840 * This will send an error to the other controller in the case of a 12841 * failure. 12842 */ 12843 retval = ctl_datamove_remote_sgl_setup(io); 12844 if (retval != 0) 12845 return; 12846 12847 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12848 ctl_datamove_remote_read_cb); 12849 if (retval != 0) { 12850 /* 12851 * Make sure we free memory if there was an error.. The 12852 * ctl_datamove_remote_xfer() function will send the 12853 * datamove done message, or call the callback with an 12854 * error if there is a problem. 12855 */ 12856 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12857 free(CTL_LSGLT(io)[i].addr, M_CTL); 12858 free(CTL_RSGL(io), M_CTL); 12859 CTL_RSGL(io) = NULL; 12860 CTL_LSGL(io) = NULL; 12861 } 12862 } 12863 12864 /* 12865 * Process a datamove request from the other controller. This is used for 12866 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12867 * first. Once that is complete, the data gets DMAed into the remote 12868 * controller's memory. For reads, we DMA from the remote controller's 12869 * memory into our memory first, and then move it out to the FETD. 12870 */ 12871 static void 12872 ctl_datamove_remote(union ctl_io *io) 12873 { 12874 12875 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12876 12877 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12878 ctl_failover_io(io, /*have_lock*/ 0); 12879 return; 12880 } 12881 12882 /* 12883 * Note that we look for an aborted I/O here, but don't do some of 12884 * the other checks that ctl_datamove() normally does. 12885 * We don't need to run the datamove delay code, since that should 12886 * have been done if need be on the other controller. 12887 */ 12888 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12889 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12890 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12891 io->io_hdr.nexus.targ_port, 12892 io->io_hdr.nexus.targ_lun); 12893 io->io_hdr.port_status = 31338; 12894 ctl_send_datamove_done(io, /*have_lock*/ 0); 12895 return; 12896 } 12897 12898 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12899 ctl_datamove_remote_write(io); 12900 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12901 ctl_datamove_remote_read(io); 12902 else { 12903 io->io_hdr.port_status = 31339; 12904 ctl_send_datamove_done(io, /*have_lock*/ 0); 12905 } 12906 } 12907 12908 static void 12909 ctl_process_done(union ctl_io *io) 12910 { 12911 struct ctl_softc *softc = CTL_SOFTC(io); 12912 struct ctl_port *port = CTL_PORT(io); 12913 struct ctl_lun *lun = CTL_LUN(io); 12914 void (*fe_done)(union ctl_io *io); 12915 union ctl_ha_msg msg; 12916 12917 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12918 fe_done = port->fe_done; 12919 12920 #ifdef CTL_TIME_IO 12921 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12922 char str[256]; 12923 char path_str[64]; 12924 struct sbuf sb; 12925 12926 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12927 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12928 12929 sbuf_cat(&sb, path_str); 12930 switch (io->io_hdr.io_type) { 12931 case CTL_IO_SCSI: 12932 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12933 sbuf_printf(&sb, "\n"); 12934 sbuf_cat(&sb, path_str); 12935 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12936 io->scsiio.tag_num, io->scsiio.tag_type); 12937 break; 12938 case CTL_IO_TASK: 12939 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12940 "Tag Type: %d\n", io->taskio.task_action, 12941 io->taskio.tag_num, io->taskio.tag_type); 12942 break; 12943 default: 12944 panic("%s: Invalid CTL I/O type %d\n", 12945 __func__, io->io_hdr.io_type); 12946 } 12947 sbuf_cat(&sb, path_str); 12948 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12949 (intmax_t)time_uptime - io->io_hdr.start_time); 12950 sbuf_finish(&sb); 12951 printf("%s", sbuf_data(&sb)); 12952 } 12953 #endif /* CTL_TIME_IO */ 12954 12955 switch (io->io_hdr.io_type) { 12956 case CTL_IO_SCSI: 12957 break; 12958 case CTL_IO_TASK: 12959 if (ctl_debug & CTL_DEBUG_INFO) 12960 ctl_io_error_print(io, NULL); 12961 fe_done(io); 12962 return; 12963 default: 12964 panic("%s: Invalid CTL I/O type %d\n", 12965 __func__, io->io_hdr.io_type); 12966 } 12967 12968 if (lun == NULL) { 12969 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12970 io->io_hdr.nexus.targ_mapped_lun)); 12971 goto bailout; 12972 } 12973 12974 mtx_lock(&lun->lun_lock); 12975 12976 /* 12977 * Check to see if we have any informational exception and status 12978 * of this command can be modified to report it in form of either 12979 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 12980 */ 12981 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 12982 io->io_hdr.status == CTL_SUCCESS && 12983 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 12984 uint8_t mrie = lun->MODE_IE.mrie; 12985 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 12986 (lun->MODE_VER.byte3 & SMS_VER_PER)); 12987 if (((mrie == SIEP_MRIE_REC_COND && per) || 12988 mrie == SIEP_MRIE_REC_UNCOND || 12989 mrie == SIEP_MRIE_NO_SENSE) && 12990 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 12991 CTL_CMD_FLAG_NO_SENSE) == 0) { 12992 ctl_set_sense(&io->scsiio, 12993 /*current_error*/ 1, 12994 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 12995 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 12996 /*asc*/ lun->ie_asc, 12997 /*ascq*/ lun->ie_ascq, 12998 SSD_ELEM_NONE); 12999 lun->ie_reported = 1; 13000 } 13001 } else if (lun->ie_reported < 0) 13002 lun->ie_reported = 0; 13003 13004 /* 13005 * Check to see if we have any errors to inject here. We only 13006 * inject errors for commands that don't already have errors set. 13007 */ 13008 if (!STAILQ_EMPTY(&lun->error_list) && 13009 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13010 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13011 ctl_inject_error(lun, io); 13012 13013 /* 13014 * XXX KDM how do we treat commands that aren't completed 13015 * successfully? 13016 * 13017 * XXX KDM should we also track I/O latency? 13018 */ 13019 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13020 io->io_hdr.io_type == CTL_IO_SCSI) { 13021 int type; 13022 #ifdef CTL_TIME_IO 13023 struct bintime bt; 13024 13025 getbinuptime(&bt); 13026 bintime_sub(&bt, &io->io_hdr.start_bt); 13027 #endif 13028 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13029 CTL_FLAG_DATA_IN) 13030 type = CTL_STATS_READ; 13031 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13032 CTL_FLAG_DATA_OUT) 13033 type = CTL_STATS_WRITE; 13034 else 13035 type = CTL_STATS_NO_IO; 13036 13037 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13038 lun->stats.operations[type] ++; 13039 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13040 #ifdef CTL_TIME_IO 13041 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13042 bintime_add(&lun->stats.time[type], &bt); 13043 #endif 13044 13045 mtx_lock(&port->port_lock); 13046 port->stats.bytes[type] += io->scsiio.kern_total_len; 13047 port->stats.operations[type] ++; 13048 port->stats.dmas[type] += io->io_hdr.num_dmas; 13049 #ifdef CTL_TIME_IO 13050 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13051 bintime_add(&port->stats.time[type], &bt); 13052 #endif 13053 mtx_unlock(&port->port_lock); 13054 } 13055 13056 /* 13057 * Remove this from the OOA queue. 13058 */ 13059 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13060 #ifdef CTL_TIME_IO 13061 if (TAILQ_EMPTY(&lun->ooa_queue)) 13062 lun->last_busy = getsbinuptime(); 13063 #endif 13064 13065 /* 13066 * Run through the blocked queue on this LUN and see if anything 13067 * has become unblocked, now that this transaction is done. 13068 */ 13069 ctl_check_blocked(lun); 13070 13071 /* 13072 * If the LUN has been invalidated, free it if there is nothing 13073 * left on its OOA queue. 13074 */ 13075 if ((lun->flags & CTL_LUN_INVALID) 13076 && TAILQ_EMPTY(&lun->ooa_queue)) { 13077 mtx_unlock(&lun->lun_lock); 13078 ctl_free_lun(lun); 13079 } else 13080 mtx_unlock(&lun->lun_lock); 13081 13082 bailout: 13083 13084 /* 13085 * If this command has been aborted, make sure we set the status 13086 * properly. The FETD is responsible for freeing the I/O and doing 13087 * whatever it needs to do to clean up its state. 13088 */ 13089 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13090 ctl_set_task_aborted(&io->scsiio); 13091 13092 /* 13093 * If enabled, print command error status. 13094 */ 13095 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13096 (ctl_debug & CTL_DEBUG_INFO) != 0) 13097 ctl_io_error_print(io, NULL); 13098 13099 /* 13100 * Tell the FETD or the other shelf controller we're done with this 13101 * command. Note that only SCSI commands get to this point. Task 13102 * management commands are completed above. 13103 */ 13104 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13105 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13106 memset(&msg, 0, sizeof(msg)); 13107 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13108 msg.hdr.serializing_sc = io->io_hdr.remote_io; 13109 msg.hdr.nexus = io->io_hdr.nexus; 13110 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13111 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13112 M_WAITOK); 13113 } 13114 13115 fe_done(io); 13116 } 13117 13118 /* 13119 * Front end should call this if it doesn't do autosense. When the request 13120 * sense comes back in from the initiator, we'll dequeue this and send it. 13121 */ 13122 int 13123 ctl_queue_sense(union ctl_io *io) 13124 { 13125 struct ctl_softc *softc = CTL_SOFTC(io); 13126 struct ctl_port *port = CTL_PORT(io); 13127 struct ctl_lun *lun; 13128 struct scsi_sense_data *ps; 13129 uint32_t initidx, p, targ_lun; 13130 13131 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13132 13133 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13134 13135 /* 13136 * LUN lookup will likely move to the ctl_work_thread() once we 13137 * have our new queueing infrastructure (that doesn't put things on 13138 * a per-LUN queue initially). That is so that we can handle 13139 * things like an INQUIRY to a LUN that we don't have enabled. We 13140 * can't deal with that right now. 13141 * If we don't have a LUN for this, just toss the sense information. 13142 */ 13143 mtx_lock(&softc->ctl_lock); 13144 if (targ_lun >= ctl_max_luns || 13145 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13146 mtx_unlock(&softc->ctl_lock); 13147 goto bailout; 13148 } 13149 mtx_lock(&lun->lun_lock); 13150 mtx_unlock(&softc->ctl_lock); 13151 13152 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13153 p = initidx / CTL_MAX_INIT_PER_PORT; 13154 if (lun->pending_sense[p] == NULL) { 13155 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13156 M_CTL, M_NOWAIT | M_ZERO); 13157 } 13158 if ((ps = lun->pending_sense[p]) != NULL) { 13159 ps += initidx % CTL_MAX_INIT_PER_PORT; 13160 memset(ps, 0, sizeof(*ps)); 13161 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13162 } 13163 mtx_unlock(&lun->lun_lock); 13164 13165 bailout: 13166 ctl_free_io(io); 13167 return (CTL_RETVAL_COMPLETE); 13168 } 13169 13170 /* 13171 * Primary command inlet from frontend ports. All SCSI and task I/O 13172 * requests must go through this function. 13173 */ 13174 int 13175 ctl_queue(union ctl_io *io) 13176 { 13177 struct ctl_port *port = CTL_PORT(io); 13178 13179 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13180 13181 #ifdef CTL_TIME_IO 13182 io->io_hdr.start_time = time_uptime; 13183 getbinuptime(&io->io_hdr.start_bt); 13184 #endif /* CTL_TIME_IO */ 13185 13186 /* Map FE-specific LUN ID into global one. */ 13187 io->io_hdr.nexus.targ_mapped_lun = 13188 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13189 13190 switch (io->io_hdr.io_type) { 13191 case CTL_IO_SCSI: 13192 case CTL_IO_TASK: 13193 if (ctl_debug & CTL_DEBUG_CDB) 13194 ctl_io_print(io); 13195 ctl_enqueue_incoming(io); 13196 break; 13197 default: 13198 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13199 return (EINVAL); 13200 } 13201 13202 return (CTL_RETVAL_COMPLETE); 13203 } 13204 13205 #ifdef CTL_IO_DELAY 13206 static void 13207 ctl_done_timer_wakeup(void *arg) 13208 { 13209 union ctl_io *io; 13210 13211 io = (union ctl_io *)arg; 13212 ctl_done(io); 13213 } 13214 #endif /* CTL_IO_DELAY */ 13215 13216 void 13217 ctl_serseq_done(union ctl_io *io) 13218 { 13219 struct ctl_lun *lun = CTL_LUN(io);; 13220 13221 if (lun->be_lun == NULL || 13222 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13223 return; 13224 mtx_lock(&lun->lun_lock); 13225 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13226 ctl_check_blocked(lun); 13227 mtx_unlock(&lun->lun_lock); 13228 } 13229 13230 void 13231 ctl_done(union ctl_io *io) 13232 { 13233 13234 /* 13235 * Enable this to catch duplicate completion issues. 13236 */ 13237 #if 0 13238 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13239 printf("%s: type %d msg %d cdb %x iptl: " 13240 "%u:%u:%u tag 0x%04x " 13241 "flag %#x status %x\n", 13242 __func__, 13243 io->io_hdr.io_type, 13244 io->io_hdr.msg_type, 13245 io->scsiio.cdb[0], 13246 io->io_hdr.nexus.initid, 13247 io->io_hdr.nexus.targ_port, 13248 io->io_hdr.nexus.targ_lun, 13249 (io->io_hdr.io_type == 13250 CTL_IO_TASK) ? 13251 io->taskio.tag_num : 13252 io->scsiio.tag_num, 13253 io->io_hdr.flags, 13254 io->io_hdr.status); 13255 } else 13256 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13257 #endif 13258 13259 /* 13260 * This is an internal copy of an I/O, and should not go through 13261 * the normal done processing logic. 13262 */ 13263 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13264 return; 13265 13266 #ifdef CTL_IO_DELAY 13267 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13268 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13269 } else { 13270 struct ctl_lun *lun = CTL_LUN(io); 13271 13272 if ((lun != NULL) 13273 && (lun->delay_info.done_delay > 0)) { 13274 13275 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13276 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13277 callout_reset(&io->io_hdr.delay_callout, 13278 lun->delay_info.done_delay * hz, 13279 ctl_done_timer_wakeup, io); 13280 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13281 lun->delay_info.done_delay = 0; 13282 return; 13283 } 13284 } 13285 #endif /* CTL_IO_DELAY */ 13286 13287 ctl_enqueue_done(io); 13288 } 13289 13290 static void 13291 ctl_work_thread(void *arg) 13292 { 13293 struct ctl_thread *thr = (struct ctl_thread *)arg; 13294 struct ctl_softc *softc = thr->ctl_softc; 13295 union ctl_io *io; 13296 int retval; 13297 13298 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13299 13300 while (!softc->shutdown) { 13301 /* 13302 * We handle the queues in this order: 13303 * - ISC 13304 * - done queue (to free up resources, unblock other commands) 13305 * - incoming queue 13306 * - RtR queue 13307 * 13308 * If those queues are empty, we break out of the loop and 13309 * go to sleep. 13310 */ 13311 mtx_lock(&thr->queue_lock); 13312 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13313 if (io != NULL) { 13314 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13315 mtx_unlock(&thr->queue_lock); 13316 ctl_handle_isc(io); 13317 continue; 13318 } 13319 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13320 if (io != NULL) { 13321 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13322 /* clear any blocked commands, call fe_done */ 13323 mtx_unlock(&thr->queue_lock); 13324 ctl_process_done(io); 13325 continue; 13326 } 13327 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13328 if (io != NULL) { 13329 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13330 mtx_unlock(&thr->queue_lock); 13331 if (io->io_hdr.io_type == CTL_IO_TASK) 13332 ctl_run_task(io); 13333 else 13334 ctl_scsiio_precheck(softc, &io->scsiio); 13335 continue; 13336 } 13337 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13338 if (io != NULL) { 13339 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13340 mtx_unlock(&thr->queue_lock); 13341 retval = ctl_scsiio(&io->scsiio); 13342 if (retval != CTL_RETVAL_COMPLETE) 13343 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13344 continue; 13345 } 13346 13347 /* Sleep until we have something to do. */ 13348 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13349 } 13350 thr->thread = NULL; 13351 kthread_exit(); 13352 } 13353 13354 static void 13355 ctl_lun_thread(void *arg) 13356 { 13357 struct ctl_softc *softc = (struct ctl_softc *)arg; 13358 struct ctl_be_lun *be_lun; 13359 13360 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13361 13362 while (!softc->shutdown) { 13363 mtx_lock(&softc->ctl_lock); 13364 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13365 if (be_lun != NULL) { 13366 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13367 mtx_unlock(&softc->ctl_lock); 13368 ctl_create_lun(be_lun); 13369 continue; 13370 } 13371 13372 /* Sleep until we have something to do. */ 13373 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13374 PDROP | PRIBIO, "-", 0); 13375 } 13376 softc->lun_thread = NULL; 13377 kthread_exit(); 13378 } 13379 13380 static void 13381 ctl_thresh_thread(void *arg) 13382 { 13383 struct ctl_softc *softc = (struct ctl_softc *)arg; 13384 struct ctl_lun *lun; 13385 struct ctl_logical_block_provisioning_page *page; 13386 const char *attr; 13387 union ctl_ha_msg msg; 13388 uint64_t thres, val; 13389 int i, e, set; 13390 13391 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13392 13393 while (!softc->shutdown) { 13394 mtx_lock(&softc->ctl_lock); 13395 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13396 if ((lun->flags & CTL_LUN_DISABLED) || 13397 (lun->flags & CTL_LUN_NO_MEDIA) || 13398 lun->backend->lun_attr == NULL) 13399 continue; 13400 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13401 softc->ha_mode == CTL_HA_MODE_XFER) 13402 continue; 13403 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13404 continue; 13405 e = 0; 13406 page = &lun->MODE_LBP; 13407 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13408 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13409 continue; 13410 thres = scsi_4btoul(page->descr[i].count); 13411 thres <<= CTL_LBP_EXPONENT; 13412 switch (page->descr[i].resource) { 13413 case 0x01: 13414 attr = "blocksavail"; 13415 break; 13416 case 0x02: 13417 attr = "blocksused"; 13418 break; 13419 case 0xf1: 13420 attr = "poolblocksavail"; 13421 break; 13422 case 0xf2: 13423 attr = "poolblocksused"; 13424 break; 13425 default: 13426 continue; 13427 } 13428 mtx_unlock(&softc->ctl_lock); // XXX 13429 val = lun->backend->lun_attr( 13430 lun->be_lun->be_lun, attr); 13431 mtx_lock(&softc->ctl_lock); 13432 if (val == UINT64_MAX) 13433 continue; 13434 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13435 == SLBPPD_ARMING_INC) 13436 e = (val >= thres); 13437 else 13438 e = (val <= thres); 13439 if (e) 13440 break; 13441 } 13442 mtx_lock(&lun->lun_lock); 13443 if (e) { 13444 scsi_u64to8b((uint8_t *)&page->descr[i] - 13445 (uint8_t *)page, lun->ua_tpt_info); 13446 if (lun->lasttpt == 0 || 13447 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13448 lun->lasttpt = time_uptime; 13449 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13450 set = 1; 13451 } else 13452 set = 0; 13453 } else { 13454 lun->lasttpt = 0; 13455 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13456 set = -1; 13457 } 13458 mtx_unlock(&lun->lun_lock); 13459 if (set != 0 && 13460 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13461 /* Send msg to other side. */ 13462 bzero(&msg.ua, sizeof(msg.ua)); 13463 msg.hdr.msg_type = CTL_MSG_UA; 13464 msg.hdr.nexus.initid = -1; 13465 msg.hdr.nexus.targ_port = -1; 13466 msg.hdr.nexus.targ_lun = lun->lun; 13467 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13468 msg.ua.ua_all = 1; 13469 msg.ua.ua_set = (set > 0); 13470 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13471 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13472 mtx_unlock(&softc->ctl_lock); // XXX 13473 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13474 sizeof(msg.ua), M_WAITOK); 13475 mtx_lock(&softc->ctl_lock); 13476 } 13477 } 13478 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13479 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz); 13480 } 13481 softc->thresh_thread = NULL; 13482 kthread_exit(); 13483 } 13484 13485 static void 13486 ctl_enqueue_incoming(union ctl_io *io) 13487 { 13488 struct ctl_softc *softc = CTL_SOFTC(io); 13489 struct ctl_thread *thr; 13490 u_int idx; 13491 13492 idx = (io->io_hdr.nexus.targ_port * 127 + 13493 io->io_hdr.nexus.initid) % worker_threads; 13494 thr = &softc->threads[idx]; 13495 mtx_lock(&thr->queue_lock); 13496 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13497 mtx_unlock(&thr->queue_lock); 13498 wakeup(thr); 13499 } 13500 13501 static void 13502 ctl_enqueue_rtr(union ctl_io *io) 13503 { 13504 struct ctl_softc *softc = CTL_SOFTC(io); 13505 struct ctl_thread *thr; 13506 13507 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13508 mtx_lock(&thr->queue_lock); 13509 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13510 mtx_unlock(&thr->queue_lock); 13511 wakeup(thr); 13512 } 13513 13514 static void 13515 ctl_enqueue_done(union ctl_io *io) 13516 { 13517 struct ctl_softc *softc = CTL_SOFTC(io); 13518 struct ctl_thread *thr; 13519 13520 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13521 mtx_lock(&thr->queue_lock); 13522 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13523 mtx_unlock(&thr->queue_lock); 13524 wakeup(thr); 13525 } 13526 13527 static void 13528 ctl_enqueue_isc(union ctl_io *io) 13529 { 13530 struct ctl_softc *softc = CTL_SOFTC(io); 13531 struct ctl_thread *thr; 13532 13533 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13534 mtx_lock(&thr->queue_lock); 13535 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13536 mtx_unlock(&thr->queue_lock); 13537 wakeup(thr); 13538 } 13539 13540 /* 13541 * vim: ts=8 13542 */ 13543