1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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/param.h> 48 #include <sys/systm.h> 49 #include <sys/ctype.h> 50 #include <sys/kernel.h> 51 #include <sys/types.h> 52 #include <sys/kthread.h> 53 #include <sys/bio.h> 54 #include <sys/fcntl.h> 55 #include <sys/lock.h> 56 #include <sys/module.h> 57 #include <sys/mutex.h> 58 #include <sys/condvar.h> 59 #include <sys/malloc.h> 60 #include <sys/conf.h> 61 #include <sys/ioccom.h> 62 #include <sys/queue.h> 63 #include <sys/sbuf.h> 64 #include <sys/smp.h> 65 #include <sys/endian.h> 66 #include <sys/proc.h> 67 #include <sys/sched.h> 68 #include <sys/sysctl.h> 69 #include <sys/nv.h> 70 #include <sys/dnv.h> 71 #include <vm/uma.h> 72 73 #include <cam/cam.h> 74 #include <cam/scsi/scsi_all.h> 75 #include <cam/scsi/scsi_cd.h> 76 #include <cam/scsi/scsi_da.h> 77 #include <cam/ctl/ctl_io.h> 78 #include <cam/ctl/ctl.h> 79 #include <cam/ctl/ctl_frontend.h> 80 #include <cam/ctl/ctl_util.h> 81 #include <cam/ctl/ctl_backend.h> 82 #include <cam/ctl/ctl_ioctl.h> 83 #include <cam/ctl/ctl_ha.h> 84 #include <cam/ctl/ctl_private.h> 85 #include <cam/ctl/ctl_debug.h> 86 #include <cam/ctl/ctl_nvme_all.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_da_verify_recovery_page verify_er_page_default = { 129 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 130 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 131 /*byte3*/0, 132 /*read_retry_count*/0, 133 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 134 /*recovery_time_limit*/{0, 0}, 135 }; 136 137 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 138 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 139 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 140 /*byte3*/SMS_VER_PER, 141 /*read_retry_count*/0, 142 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 143 /*recovery_time_limit*/{0, 0}, 144 }; 145 146 const static struct scsi_caching_page caching_page_default = { 147 /*page_code*/SMS_CACHING_PAGE, 148 /*page_length*/sizeof(struct scsi_caching_page) - 2, 149 /*flags1*/ SCP_DISC | SCP_WCE, 150 /*ret_priority*/ 0, 151 /*disable_pf_transfer_len*/ {0xff, 0xff}, 152 /*min_prefetch*/ {0, 0}, 153 /*max_prefetch*/ {0xff, 0xff}, 154 /*max_pf_ceiling*/ {0xff, 0xff}, 155 /*flags2*/ 0, 156 /*cache_segments*/ 0, 157 /*cache_seg_size*/ {0, 0}, 158 /*reserved*/ 0, 159 /*non_cache_seg_size*/ {0, 0, 0} 160 }; 161 162 const static struct scsi_caching_page caching_page_changeable = { 163 /*page_code*/SMS_CACHING_PAGE, 164 /*page_length*/sizeof(struct scsi_caching_page) - 2, 165 /*flags1*/ SCP_WCE | SCP_RCD, 166 /*ret_priority*/ 0, 167 /*disable_pf_transfer_len*/ {0, 0}, 168 /*min_prefetch*/ {0, 0}, 169 /*max_prefetch*/ {0, 0}, 170 /*max_pf_ceiling*/ {0, 0}, 171 /*flags2*/ 0, 172 /*cache_segments*/ 0, 173 /*cache_seg_size*/ {0, 0}, 174 /*reserved*/ 0, 175 /*non_cache_seg_size*/ {0, 0, 0} 176 }; 177 178 const static struct scsi_control_page control_page_default = { 179 /*page_code*/SMS_CONTROL_MODE_PAGE, 180 /*page_length*/sizeof(struct scsi_control_page) - 2, 181 /*rlec*/0, 182 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 183 /*eca_and_aen*/0, 184 /*flags4*/SCP_TAS, 185 /*aen_holdoff_period*/{0, 0}, 186 /*busy_timeout_period*/{0, 0}, 187 /*extended_selftest_completion_time*/{0, 0} 188 }; 189 190 const static struct scsi_control_page control_page_changeable = { 191 /*page_code*/SMS_CONTROL_MODE_PAGE, 192 /*page_length*/sizeof(struct scsi_control_page) - 2, 193 /*rlec*/SCP_DSENSE, 194 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 195 /*eca_and_aen*/SCP_SWP, 196 /*flags4*/0, 197 /*aen_holdoff_period*/{0, 0}, 198 /*busy_timeout_period*/{0, 0}, 199 /*extended_selftest_completion_time*/{0, 0} 200 }; 201 202 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 203 204 const static struct scsi_control_ext_page control_ext_page_default = { 205 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 206 /*subpage_code*/0x01, 207 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 208 /*flags*/0, 209 /*prio*/0, 210 /*max_sense*/0 211 }; 212 213 const static struct scsi_control_ext_page control_ext_page_changeable = { 214 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 215 /*subpage_code*/0x01, 216 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 217 /*flags*/0, 218 /*prio*/0, 219 /*max_sense*/0xff 220 }; 221 222 const static struct scsi_info_exceptions_page ie_page_default = { 223 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 224 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 225 /*info_flags*/SIEP_FLAGS_EWASC, 226 /*mrie*/SIEP_MRIE_NO, 227 /*interval_timer*/{0, 0, 0, 0}, 228 /*report_count*/{0, 0, 0, 1} 229 }; 230 231 const static struct scsi_info_exceptions_page ie_page_changeable = { 232 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 233 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 234 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 235 SIEP_FLAGS_LOGERR, 236 /*mrie*/0x0f, 237 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 238 /*report_count*/{0xff, 0xff, 0xff, 0xff} 239 }; 240 241 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 242 243 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 244 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 245 /*subpage_code*/0x02, 246 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 247 /*flags*/0, 248 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 249 /*descr*/{}}, 250 {{/*flags*/0, 251 /*resource*/0x01, 252 /*reserved*/{0, 0}, 253 /*count*/{0, 0, 0, 0}}, 254 {/*flags*/0, 255 /*resource*/0x02, 256 /*reserved*/{0, 0}, 257 /*count*/{0, 0, 0, 0}}, 258 {/*flags*/0, 259 /*resource*/0xf1, 260 /*reserved*/{0, 0}, 261 /*count*/{0, 0, 0, 0}}, 262 {/*flags*/0, 263 /*resource*/0xf2, 264 /*reserved*/{0, 0}, 265 /*count*/{0, 0, 0, 0}} 266 } 267 }; 268 269 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 270 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 271 /*subpage_code*/0x02, 272 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 273 /*flags*/SLBPP_SITUA, 274 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 275 /*descr*/{}}, 276 {{/*flags*/0, 277 /*resource*/0, 278 /*reserved*/{0, 0}, 279 /*count*/{0, 0, 0, 0}}, 280 {/*flags*/0, 281 /*resource*/0, 282 /*reserved*/{0, 0}, 283 /*count*/{0, 0, 0, 0}}, 284 {/*flags*/0, 285 /*resource*/0, 286 /*reserved*/{0, 0}, 287 /*count*/{0, 0, 0, 0}}, 288 {/*flags*/0, 289 /*resource*/0, 290 /*reserved*/{0, 0}, 291 /*count*/{0, 0, 0, 0}} 292 } 293 }; 294 295 const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 296 /*page_code*/SMS_CDDVD_CAPS_PAGE, 297 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 298 /*caps1*/0x3f, 299 /*caps2*/0x00, 300 /*caps3*/0xf0, 301 /*caps4*/0x00, 302 /*caps5*/0x29, 303 /*caps6*/0x00, 304 /*obsolete*/{0, 0}, 305 /*nvol_levels*/{0, 0}, 306 /*buffer_size*/{8, 0}, 307 /*obsolete2*/{0, 0}, 308 /*reserved*/0, 309 /*digital*/0, 310 /*obsolete3*/0, 311 /*copy_management*/0, 312 /*reserved2*/0, 313 /*rotation_control*/0, 314 /*cur_write_speed*/0, 315 /*num_speed_descr*/0, 316 }; 317 318 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 319 /*page_code*/SMS_CDDVD_CAPS_PAGE, 320 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 321 /*caps1*/0, 322 /*caps2*/0, 323 /*caps3*/0, 324 /*caps4*/0, 325 /*caps5*/0, 326 /*caps6*/0, 327 /*obsolete*/{0, 0}, 328 /*nvol_levels*/{0, 0}, 329 /*buffer_size*/{0, 0}, 330 /*obsolete2*/{0, 0}, 331 /*reserved*/0, 332 /*digital*/0, 333 /*obsolete3*/0, 334 /*copy_management*/0, 335 /*reserved2*/0, 336 /*rotation_control*/0, 337 /*cur_write_speed*/0, 338 /*num_speed_descr*/0, 339 }; 340 341 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 342 "CAM Target Layer"); 343 static int worker_threads = -1; 344 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 345 &worker_threads, 1, "Number of worker threads"); 346 static int ctl_debug = CTL_DEBUG_NONE; 347 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 348 &ctl_debug, 0, "Enabled debug flags"); 349 static int ctl_lun_map_size = 1024; 350 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 351 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 352 #ifdef CTL_TIME_IO 353 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS; 354 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN, 355 &ctl_time_io_secs, 0, "Log requests taking more seconds"); 356 #endif 357 358 /* 359 * Maximum number of LUNs we support. MUST be a power of 2. 360 */ 361 #define CTL_DEFAULT_MAX_LUNS 1024 362 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 363 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns); 364 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN, 365 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs"); 366 367 /* 368 * Maximum number of ports registered at one time. 369 */ 370 #define CTL_DEFAULT_MAX_PORTS 1024 371 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 372 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports); 373 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN, 374 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports"); 375 376 /* 377 * Maximum number of initiators we support. 378 */ 379 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports) 380 381 /* 382 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 383 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 384 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92), 385 * Block limits (0xB0), Block Device Characteristics (0xB1) and 386 * Logical Block Provisioning (0xB2) 387 */ 388 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11 389 390 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 391 int param); 392 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 393 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 394 static int ctl_init(void); 395 static int ctl_shutdown(void); 396 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 397 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 398 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 399 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 400 struct ctl_ooa *ooa_hdr, 401 struct ctl_ooa_entry *kern_entries); 402 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 403 struct thread *td); 404 static int ctl_enable_lun(struct ctl_lun *lun); 405 static int ctl_disable_lun(struct ctl_lun *lun); 406 static int ctl_free_lun(struct ctl_lun *lun); 407 408 static int ctl_do_mode_select(union ctl_io *io); 409 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 410 uint64_t res_key, uint64_t sa_res_key, 411 uint8_t type, uint32_t residx, 412 struct ctl_scsiio *ctsio, 413 struct scsi_per_res_out *cdb, 414 struct scsi_per_res_out_parms* param); 415 static void ctl_pro_preempt_other(struct ctl_lun *lun, 416 union ctl_ha_msg *msg); 417 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 418 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 419 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 420 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 421 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 422 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 423 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 424 int alloc_len); 425 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len); 426 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 427 int alloc_len); 428 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 429 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 430 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 431 static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 432 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 433 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 434 bool seq); 435 static ctl_action ctl_seq_check(union ctl_io *io1, union ctl_io *io2); 436 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 437 union ctl_io *pending_io, const uint8_t *serialize_row, 438 union ctl_io *ooa_io); 439 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 440 union ctl_io **starting_io); 441 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, 442 bool skip); 443 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io, 444 bool skip); 445 static int ctl_scsiio_lun_check(struct ctl_lun *lun, 446 const struct ctl_cmd_entry *entry, 447 struct ctl_scsiio *ctsio); 448 static void ctl_failover_lun(union ctl_io *io); 449 static void ctl_scsiio_precheck(struct ctl_scsiio *ctsio); 450 static int ctl_scsiio(struct ctl_scsiio *ctsio); 451 static void ctl_nvmeio_precheck(struct ctl_nvmeio *ctnio); 452 static int ctl_nvmeio(struct ctl_nvmeio *ctnio); 453 454 static int ctl_target_reset(union ctl_io *io); 455 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, 456 ctl_ua_type ua_type); 457 static int ctl_lun_reset(union ctl_io *io); 458 static int ctl_abort_task(union ctl_io *io); 459 static int ctl_abort_task_set(union ctl_io *io); 460 static int ctl_query_task(union ctl_io *io, int task_set); 461 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 462 ctl_ua_type ua_type); 463 static int ctl_i_t_nexus_reset(union ctl_io *io); 464 static int ctl_query_async_event(union ctl_io *io); 465 static void ctl_run_task(union ctl_io *io); 466 #ifdef CTL_IO_DELAY 467 static void ctl_datamove_timer_wakeup(void *arg); 468 static void ctl_done_timer_wakeup(void *arg); 469 #endif /* CTL_IO_DELAY */ 470 471 static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 472 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 473 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr); 474 static void ctl_datamove_remote_write(union ctl_io *io); 475 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr); 476 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 477 static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 478 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 479 ctl_ha_dt_cb callback); 480 static void ctl_datamove_remote_read(union ctl_io *io); 481 static void ctl_datamove_remote(union ctl_io *io); 482 static void ctl_process_done(union ctl_io *io); 483 static void ctl_thresh_thread(void *arg); 484 static void ctl_work_thread(void *arg); 485 static void ctl_enqueue_incoming(union ctl_io *io); 486 static void ctl_enqueue_rtr(union ctl_io *io); 487 static void ctl_enqueue_done(union ctl_io *io); 488 static void ctl_enqueue_isc(union ctl_io *io); 489 static const struct ctl_cmd_entry * 490 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 491 static const struct ctl_cmd_entry * 492 ctl_validate_command(struct ctl_scsiio *ctsio); 493 static int ctl_cmd_applicable(uint8_t lun_type, 494 const struct ctl_cmd_entry *entry); 495 static int ctl_ha_init(void); 496 static int ctl_ha_shutdown(void); 497 498 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 499 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 500 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 501 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 502 503 /* 504 * Load the serialization table. This isn't very pretty, but is probably 505 * the easiest way to do it. 506 */ 507 #include "ctl_ser_table.c" 508 509 /* 510 * We only need to define open, close and ioctl routines for this driver. 511 */ 512 static struct cdevsw ctl_cdevsw = { 513 .d_version = D_VERSION, 514 .d_flags = 0, 515 .d_open = ctl_open, 516 .d_close = ctl_close, 517 .d_ioctl = ctl_ioctl, 518 .d_name = "ctl", 519 }; 520 521 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 522 523 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 524 525 static moduledata_t ctl_moduledata = { 526 "ctl", 527 ctl_module_event_handler, 528 NULL 529 }; 530 531 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 532 MODULE_VERSION(ctl, 1); 533 534 static void 535 ctl_be_move_done(union ctl_io *io, bool samethr) 536 { 537 switch (io->io_hdr.io_type) { 538 case CTL_IO_SCSI: 539 io->scsiio.be_move_done(io, samethr); 540 break; 541 case CTL_IO_NVME: 542 case CTL_IO_NVME_ADMIN: 543 io->nvmeio.be_move_done(io, samethr); 544 break; 545 default: 546 __assert_unreachable(); 547 } 548 } 549 550 static void 551 ctl_continue_io(union ctl_io *io) 552 { 553 switch (io->io_hdr.io_type) { 554 case CTL_IO_SCSI: 555 io->scsiio.io_cont(io); 556 break; 557 case CTL_IO_NVME: 558 case CTL_IO_NVME_ADMIN: 559 io->nvmeio.io_cont(io); 560 break; 561 default: 562 __assert_unreachable(); 563 } 564 } 565 566 static struct ctl_frontend ha_frontend = 567 { 568 .name = "ha", 569 .init = ctl_ha_init, 570 .shutdown = ctl_ha_shutdown, 571 }; 572 573 static int 574 ctl_ha_init(void) 575 { 576 struct ctl_softc *softc = control_softc; 577 578 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 579 &softc->othersc_pool) != 0) 580 return (ENOMEM); 581 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 582 ctl_pool_free(softc->othersc_pool); 583 return (EIO); 584 } 585 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 586 != CTL_HA_STATUS_SUCCESS) { 587 ctl_ha_msg_destroy(softc); 588 ctl_pool_free(softc->othersc_pool); 589 return (EIO); 590 } 591 return (0); 592 }; 593 594 static int 595 ctl_ha_shutdown(void) 596 { 597 struct ctl_softc *softc = control_softc; 598 struct ctl_port *port; 599 600 ctl_ha_msg_shutdown(softc); 601 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 602 return (EIO); 603 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 604 return (EIO); 605 ctl_pool_free(softc->othersc_pool); 606 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 607 ctl_port_deregister(port); 608 free(port->port_name, M_CTL); 609 free(port, M_CTL); 610 } 611 return (0); 612 }; 613 614 static void 615 ctl_ha_datamove(union ctl_io *io) 616 { 617 struct ctl_lun *lun = CTL_LUN(io); 618 struct ctl_sg_entry *sgl; 619 union ctl_ha_msg msg; 620 uint32_t sg_entries_sent; 621 int do_sg_copy, i, j; 622 623 CTL_IO_ASSERT(io, SCSI); 624 625 memset(&msg.dt, 0, sizeof(msg.dt)); 626 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 627 msg.hdr.original_sc = io->io_hdr.remote_io; 628 msg.hdr.serializing_sc = io; 629 msg.hdr.nexus = io->io_hdr.nexus; 630 msg.hdr.status = io->io_hdr.status; 631 msg.dt.flags = io->io_hdr.flags; 632 633 /* 634 * We convert everything into a S/G list here. We can't 635 * pass by reference, only by value between controllers. 636 * So we can't pass a pointer to the S/G list, only as many 637 * S/G entries as we can fit in here. If it's possible for 638 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 639 * then we need to break this up into multiple transfers. 640 */ 641 if (ctl_kern_sg_entries(io) == 0) { 642 msg.dt.kern_sg_entries = 1; 643 #if 0 644 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 645 msg.dt.sg_list[0].addr = ctl_kern_data_ptr(io); 646 } else { 647 /* XXX KDM use busdma here! */ 648 msg.dt.sg_list[0].addr = 649 (void *)vtophys(ctl_kern_data_ptr(io)); 650 } 651 #else 652 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 653 ("HA does not support BUS_ADDR")); 654 msg.dt.sg_list[0].addr = ctl_kern_data_ptr(io); 655 #endif 656 msg.dt.sg_list[0].len = ctl_kern_data_len(io); 657 do_sg_copy = 0; 658 } else { 659 msg.dt.kern_sg_entries = ctl_kern_sg_entries(io); 660 do_sg_copy = 1; 661 } 662 663 msg.dt.kern_data_len = ctl_kern_data_len(io); 664 msg.dt.kern_total_len = ctl_kern_total_len(io); 665 msg.dt.kern_data_resid = ctl_kern_data_resid(io); 666 msg.dt.kern_rel_offset = ctl_kern_rel_offset(io); 667 msg.dt.sg_sequence = 0; 668 669 /* 670 * Loop until we've sent all of the S/G entries. On the 671 * other end, we'll recompose these S/G entries into one 672 * contiguous list before processing. 673 */ 674 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 675 msg.dt.sg_sequence++) { 676 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 677 sizeof(msg.dt.sg_list[0])), 678 msg.dt.kern_sg_entries - sg_entries_sent); 679 if (do_sg_copy != 0) { 680 sgl = (struct ctl_sg_entry *)ctl_kern_data_ptr(io); 681 for (i = sg_entries_sent, j = 0; 682 i < msg.dt.cur_sg_entries; i++, j++) { 683 #if 0 684 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 685 msg.dt.sg_list[j].addr = sgl[i].addr; 686 } else { 687 /* XXX KDM use busdma here! */ 688 msg.dt.sg_list[j].addr = 689 (void *)vtophys(sgl[i].addr); 690 } 691 #else 692 KASSERT((io->io_hdr.flags & 693 CTL_FLAG_BUS_ADDR) == 0, 694 ("HA does not support BUS_ADDR")); 695 msg.dt.sg_list[j].addr = sgl[i].addr; 696 #endif 697 msg.dt.sg_list[j].len = sgl[i].len; 698 } 699 } 700 701 sg_entries_sent += msg.dt.cur_sg_entries; 702 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 703 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 704 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 705 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 706 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 707 io->io_hdr.port_status = 31341; 708 ctl_datamove_done(io, true); 709 return; 710 } 711 msg.dt.sent_sg_entries = sg_entries_sent; 712 } 713 714 /* 715 * Officially handover the request from us to peer. 716 * If failover has just happened, then we must return error. 717 * If failover happen just after, then it is not our problem. 718 */ 719 if (lun) 720 mtx_lock(&lun->lun_lock); 721 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 722 if (lun) 723 mtx_unlock(&lun->lun_lock); 724 io->io_hdr.port_status = 31342; 725 ctl_datamove_done(io, true); 726 return; 727 } 728 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 729 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 730 if (lun) 731 mtx_unlock(&lun->lun_lock); 732 } 733 734 static void 735 ctl_ha_done(union ctl_io *io) 736 { 737 union ctl_ha_msg msg; 738 739 if (io->io_hdr.io_type == CTL_IO_SCSI) { 740 memset(&msg, 0, sizeof(msg)); 741 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 742 msg.hdr.original_sc = io->io_hdr.remote_io; 743 msg.hdr.nexus = io->io_hdr.nexus; 744 msg.hdr.status = io->io_hdr.status; 745 msg.scsi.scsi_status = io->scsiio.scsi_status; 746 msg.scsi.tag_num = io->scsiio.tag_num; 747 msg.scsi.tag_type = io->scsiio.tag_type; 748 msg.scsi.sense_len = io->scsiio.sense_len; 749 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 750 io->scsiio.sense_len); 751 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 752 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 753 msg.scsi.sense_len, M_WAITOK); 754 } 755 ctl_free_io(io); 756 } 757 758 static void 759 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 760 union ctl_ha_msg *msg_info) 761 { 762 struct ctl_scsiio *ctsio; 763 764 if (msg_info->hdr.original_sc == NULL) { 765 printf("%s: original_sc == NULL!\n", __func__); 766 /* XXX KDM now what? */ 767 return; 768 } 769 770 ctsio = &msg_info->hdr.original_sc->scsiio; 771 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 772 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 773 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 774 ctsio->io_hdr.status = msg_info->hdr.status; 775 ctsio->scsi_status = msg_info->scsi.scsi_status; 776 ctsio->sense_len = msg_info->scsi.sense_len; 777 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 778 msg_info->scsi.sense_len); 779 ctl_enqueue_isc((union ctl_io *)ctsio); 780 } 781 782 static void 783 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 784 union ctl_ha_msg *msg_info) 785 { 786 struct ctl_scsiio *ctsio; 787 788 if (msg_info->hdr.serializing_sc == NULL) { 789 printf("%s: serializing_sc == NULL!\n", __func__); 790 /* XXX KDM now what? */ 791 return; 792 } 793 794 ctsio = &msg_info->hdr.serializing_sc->scsiio; 795 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 796 ctl_enqueue_isc((union ctl_io *)ctsio); 797 } 798 799 void 800 ctl_isc_announce_lun(struct ctl_lun *lun) 801 { 802 struct ctl_softc *softc = lun->ctl_softc; 803 union ctl_ha_msg *msg; 804 struct ctl_ha_msg_lun_pr_key pr_key; 805 int i, k; 806 807 if (softc->ha_link != CTL_HA_LINK_ONLINE) 808 return; 809 mtx_lock(&lun->lun_lock); 810 i = sizeof(msg->lun); 811 if (lun->lun_devid) 812 i += lun->lun_devid->len; 813 i += sizeof(pr_key) * lun->pr_key_count; 814 alloc: 815 mtx_unlock(&lun->lun_lock); 816 msg = malloc(i, M_CTL, M_WAITOK); 817 mtx_lock(&lun->lun_lock); 818 k = sizeof(msg->lun); 819 if (lun->lun_devid) 820 k += lun->lun_devid->len; 821 k += sizeof(pr_key) * lun->pr_key_count; 822 if (i < k) { 823 free(msg, M_CTL); 824 i = k; 825 goto alloc; 826 } 827 bzero(&msg->lun, sizeof(msg->lun)); 828 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 829 msg->hdr.nexus.targ_lun = lun->lun; 830 msg->hdr.nexus.targ_mapped_lun = lun->lun; 831 msg->lun.flags = lun->flags; 832 msg->lun.pr_generation = lun->pr_generation; 833 msg->lun.pr_res_idx = lun->pr_res_idx; 834 msg->lun.pr_res_type = lun->pr_res_type; 835 msg->lun.pr_key_count = lun->pr_key_count; 836 i = 0; 837 if (lun->lun_devid) { 838 msg->lun.lun_devid_len = lun->lun_devid->len; 839 memcpy(&msg->lun.data[i], lun->lun_devid->data, 840 msg->lun.lun_devid_len); 841 i += msg->lun.lun_devid_len; 842 } 843 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 844 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 845 continue; 846 pr_key.pr_iid = k; 847 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 848 i += sizeof(pr_key); 849 } 850 mtx_unlock(&lun->lun_lock); 851 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->lun, sizeof(msg->lun) + i, 852 M_WAITOK); 853 free(msg, M_CTL); 854 855 if (lun->flags & CTL_LUN_PRIMARY_SC) { 856 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 857 ctl_isc_announce_mode(lun, -1, 858 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 859 lun->mode_pages.index[i].subpage); 860 } 861 } 862 } 863 864 void 865 ctl_isc_announce_port(struct ctl_port *port) 866 { 867 struct ctl_softc *softc = port->ctl_softc; 868 union ctl_ha_msg *msg; 869 int i; 870 871 if (port->targ_port < softc->port_min || 872 port->targ_port >= softc->port_max || 873 softc->ha_link != CTL_HA_LINK_ONLINE) 874 return; 875 i = sizeof(msg->port) + strlen(port->port_name) + 1; 876 if (port->lun_map) 877 i += port->lun_map_size * sizeof(uint32_t); 878 if (port->port_devid) 879 i += port->port_devid->len; 880 if (port->target_devid) 881 i += port->target_devid->len; 882 if (port->init_devid) 883 i += port->init_devid->len; 884 msg = malloc(i, M_CTL, M_WAITOK); 885 bzero(&msg->port, sizeof(msg->port)); 886 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 887 msg->hdr.nexus.targ_port = port->targ_port; 888 msg->port.port_type = port->port_type; 889 msg->port.physical_port = port->physical_port; 890 msg->port.virtual_port = port->virtual_port; 891 msg->port.status = port->status; 892 i = 0; 893 msg->port.name_len = sprintf(&msg->port.data[i], 894 "%d:%s", softc->ha_id, port->port_name) + 1; 895 i += msg->port.name_len; 896 if (port->lun_map) { 897 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 898 memcpy(&msg->port.data[i], port->lun_map, 899 msg->port.lun_map_len); 900 i += msg->port.lun_map_len; 901 } 902 if (port->port_devid) { 903 msg->port.port_devid_len = port->port_devid->len; 904 memcpy(&msg->port.data[i], port->port_devid->data, 905 msg->port.port_devid_len); 906 i += msg->port.port_devid_len; 907 } 908 if (port->target_devid) { 909 msg->port.target_devid_len = port->target_devid->len; 910 memcpy(&msg->port.data[i], port->target_devid->data, 911 msg->port.target_devid_len); 912 i += msg->port.target_devid_len; 913 } 914 if (port->init_devid) { 915 msg->port.init_devid_len = port->init_devid->len; 916 memcpy(&msg->port.data[i], port->init_devid->data, 917 msg->port.init_devid_len); 918 i += msg->port.init_devid_len; 919 } 920 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 921 M_WAITOK); 922 free(msg, M_CTL); 923 } 924 925 void 926 ctl_isc_announce_iid(struct ctl_port *port, int iid) 927 { 928 struct ctl_softc *softc = port->ctl_softc; 929 union ctl_ha_msg *msg; 930 int i, l; 931 932 if (port->targ_port < softc->port_min || 933 port->targ_port >= softc->port_max || 934 softc->ha_link != CTL_HA_LINK_ONLINE) 935 return; 936 mtx_lock(&softc->ctl_lock); 937 i = sizeof(msg->iid); 938 l = 0; 939 if (port->wwpn_iid[iid].name) 940 l = strlen(port->wwpn_iid[iid].name) + 1; 941 i += l; 942 msg = malloc(i, M_CTL, M_NOWAIT); 943 if (msg == NULL) { 944 mtx_unlock(&softc->ctl_lock); 945 return; 946 } 947 bzero(&msg->iid, sizeof(msg->iid)); 948 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 949 msg->hdr.nexus.targ_port = port->targ_port; 950 msg->hdr.nexus.initid = iid; 951 msg->iid.in_use = port->wwpn_iid[iid].in_use; 952 msg->iid.name_len = l; 953 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 954 if (port->wwpn_iid[iid].name) 955 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 956 mtx_unlock(&softc->ctl_lock); 957 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 958 free(msg, M_CTL); 959 } 960 961 void 962 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 963 uint8_t page, uint8_t subpage) 964 { 965 struct ctl_softc *softc = lun->ctl_softc; 966 union ctl_ha_msg *msg; 967 u_int i, l; 968 969 if (softc->ha_link != CTL_HA_LINK_ONLINE) 970 return; 971 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 972 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 973 page && lun->mode_pages.index[i].subpage == subpage) 974 break; 975 } 976 if (i == CTL_NUM_MODE_PAGES) 977 return; 978 979 /* Don't try to replicate pages not present on this device. */ 980 if (lun->mode_pages.index[i].page_data == NULL) 981 return; 982 983 l = sizeof(msg->mode) + lun->mode_pages.index[i].page_len; 984 msg = malloc(l, M_CTL, M_WAITOK | M_ZERO); 985 msg->hdr.msg_type = CTL_MSG_MODE_SYNC; 986 msg->hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 987 msg->hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 988 msg->hdr.nexus.targ_lun = lun->lun; 989 msg->hdr.nexus.targ_mapped_lun = lun->lun; 990 msg->mode.page_code = page; 991 msg->mode.subpage = subpage; 992 msg->mode.page_len = lun->mode_pages.index[i].page_len; 993 memcpy(msg->mode.data, lun->mode_pages.index[i].page_data, 994 msg->mode.page_len); 995 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->mode, l, M_WAITOK); 996 free(msg, M_CTL); 997 } 998 999 static void 1000 ctl_isc_ha_link_up(struct ctl_softc *softc) 1001 { 1002 struct ctl_port *port; 1003 struct ctl_lun *lun; 1004 union ctl_ha_msg msg; 1005 int i; 1006 1007 /* Announce this node parameters to peer for validation. */ 1008 msg.login.msg_type = CTL_MSG_LOGIN; 1009 msg.login.version = CTL_HA_VERSION; 1010 msg.login.ha_mode = softc->ha_mode; 1011 msg.login.ha_id = softc->ha_id; 1012 msg.login.max_luns = ctl_max_luns; 1013 msg.login.max_ports = ctl_max_ports; 1014 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1015 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1016 M_WAITOK); 1017 1018 STAILQ_FOREACH(port, &softc->port_list, links) { 1019 ctl_isc_announce_port(port); 1020 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1021 if (port->wwpn_iid[i].in_use) 1022 ctl_isc_announce_iid(port, i); 1023 } 1024 } 1025 STAILQ_FOREACH(lun, &softc->lun_list, links) 1026 ctl_isc_announce_lun(lun); 1027 } 1028 1029 static void 1030 ctl_isc_ha_link_down(struct ctl_softc *softc) 1031 { 1032 struct ctl_port *port; 1033 struct ctl_lun *lun; 1034 union ctl_io *io; 1035 int i; 1036 1037 mtx_lock(&softc->ctl_lock); 1038 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1039 mtx_lock(&lun->lun_lock); 1040 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1041 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1042 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1043 } 1044 mtx_unlock(&lun->lun_lock); 1045 1046 mtx_unlock(&softc->ctl_lock); 1047 io = ctl_alloc_io(softc->othersc_pool); 1048 mtx_lock(&softc->ctl_lock); 1049 ctl_zero_io(io); 1050 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1051 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1052 ctl_enqueue_isc(io); 1053 } 1054 1055 STAILQ_FOREACH(port, &softc->port_list, links) { 1056 if (port->targ_port >= softc->port_min && 1057 port->targ_port < softc->port_max) 1058 continue; 1059 port->status &= ~CTL_PORT_STATUS_ONLINE; 1060 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1061 port->wwpn_iid[i].in_use = 0; 1062 free(port->wwpn_iid[i].name, M_CTL); 1063 port->wwpn_iid[i].name = NULL; 1064 } 1065 } 1066 mtx_unlock(&softc->ctl_lock); 1067 } 1068 1069 static void 1070 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1071 { 1072 struct ctl_lun *lun; 1073 uint32_t iid; 1074 1075 if (len < sizeof(msg->ua)) { 1076 printf("%s: Received truncated message %d < %zu\n", 1077 __func__, len, sizeof(msg->ua)); 1078 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1079 return; 1080 } 1081 1082 mtx_lock(&softc->ctl_lock); 1083 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns || 1084 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1085 mtx_unlock(&softc->ctl_lock); 1086 return; 1087 } 1088 mtx_lock(&lun->lun_lock); 1089 mtx_unlock(&softc->ctl_lock); 1090 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1091 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1092 iid = ctl_get_initindex(&msg->hdr.nexus); 1093 if (msg->ua.ua_all) { 1094 if (msg->ua.ua_set) 1095 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1096 else 1097 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1098 } else { 1099 if (msg->ua.ua_set) 1100 ctl_est_ua(lun, iid, msg->ua.ua_type); 1101 else 1102 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1103 } 1104 mtx_unlock(&lun->lun_lock); 1105 } 1106 1107 static void 1108 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1109 { 1110 struct ctl_lun *lun; 1111 struct ctl_ha_msg_lun_pr_key pr_key; 1112 int i, k; 1113 ctl_lun_flags oflags; 1114 uint32_t targ_lun; 1115 1116 if (len < offsetof(struct ctl_ha_msg_lun, data[0])) { 1117 printf("%s: Received truncated message %d < %zu\n", 1118 __func__, len, offsetof(struct ctl_ha_msg_lun, data[0])); 1119 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1120 return; 1121 } 1122 i = msg->lun.lun_devid_len + msg->lun.pr_key_count * sizeof(pr_key); 1123 if (len < offsetof(struct ctl_ha_msg_lun, data[i])) { 1124 printf("%s: Received truncated message data %d < %zu\n", 1125 __func__, len, offsetof(struct ctl_ha_msg_lun, data[i])); 1126 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1127 return; 1128 } 1129 1130 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1131 mtx_lock(&softc->ctl_lock); 1132 if (targ_lun >= ctl_max_luns || 1133 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1134 mtx_unlock(&softc->ctl_lock); 1135 return; 1136 } 1137 mtx_lock(&lun->lun_lock); 1138 mtx_unlock(&softc->ctl_lock); 1139 if (lun->flags & CTL_LUN_DISABLED) { 1140 mtx_unlock(&lun->lun_lock); 1141 return; 1142 } 1143 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1144 if (msg->lun.lun_devid_len != i || (i > 0 && 1145 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1146 mtx_unlock(&lun->lun_lock); 1147 printf("%s: Received conflicting HA LUN %d\n", 1148 __func__, targ_lun); 1149 return; 1150 } else { 1151 /* Record whether peer is primary. */ 1152 oflags = lun->flags; 1153 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1154 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1155 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1156 else 1157 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1158 if (oflags != lun->flags) 1159 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1160 1161 /* If peer is primary and we are not -- use data */ 1162 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1163 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1164 lun->pr_generation = msg->lun.pr_generation; 1165 lun->pr_res_idx = msg->lun.pr_res_idx; 1166 lun->pr_res_type = msg->lun.pr_res_type; 1167 lun->pr_key_count = msg->lun.pr_key_count; 1168 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1169 ctl_clr_prkey(lun, k); 1170 for (k = 0; k < msg->lun.pr_key_count; k++) { 1171 memcpy(&pr_key, &msg->lun.data[i], 1172 sizeof(pr_key)); 1173 ctl_alloc_prkey(lun, pr_key.pr_iid); 1174 ctl_set_prkey(lun, pr_key.pr_iid, 1175 pr_key.pr_key); 1176 i += sizeof(pr_key); 1177 } 1178 } 1179 1180 mtx_unlock(&lun->lun_lock); 1181 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1182 __func__, targ_lun, 1183 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1184 "primary" : "secondary")); 1185 1186 /* If we are primary but peer doesn't know -- notify */ 1187 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1188 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1189 ctl_isc_announce_lun(lun); 1190 } 1191 } 1192 1193 static void 1194 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1195 { 1196 struct ctl_port *port; 1197 struct ctl_lun *lun; 1198 int i, new; 1199 1200 if (len < offsetof(struct ctl_ha_msg_port, data[0])) { 1201 printf("%s: Received truncated message %d < %zu\n", 1202 __func__, len, offsetof(struct ctl_ha_msg_port, data[0])); 1203 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1204 return; 1205 } 1206 i = msg->port.name_len + msg->port.lun_map_len + 1207 msg->port.port_devid_len + msg->port.target_devid_len + 1208 msg->port.init_devid_len; 1209 if (len < offsetof(struct ctl_ha_msg_port, data[i])) { 1210 printf("%s: Received truncated message data %d < %zu\n", 1211 __func__, len, offsetof(struct ctl_ha_msg_port, data[i])); 1212 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1213 return; 1214 } 1215 1216 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1217 if (port == NULL) { 1218 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1219 msg->hdr.nexus.targ_port)); 1220 new = 1; 1221 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1222 port->frontend = &ha_frontend; 1223 port->targ_port = msg->hdr.nexus.targ_port; 1224 port->fe_datamove = ctl_ha_datamove; 1225 port->fe_done = ctl_ha_done; 1226 } else if (port->frontend == &ha_frontend) { 1227 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1228 msg->hdr.nexus.targ_port)); 1229 new = 0; 1230 } else { 1231 printf("%s: Received conflicting HA port %d\n", 1232 __func__, msg->hdr.nexus.targ_port); 1233 return; 1234 } 1235 port->port_type = msg->port.port_type; 1236 port->physical_port = msg->port.physical_port; 1237 port->virtual_port = msg->port.virtual_port; 1238 port->status = msg->port.status; 1239 i = 0; 1240 free(port->port_name, M_CTL); 1241 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1242 M_CTL); 1243 i += msg->port.name_len; 1244 if (msg->port.lun_map_len != 0) { 1245 if (port->lun_map == NULL || 1246 port->lun_map_size * sizeof(uint32_t) < 1247 msg->port.lun_map_len) { 1248 port->lun_map_size = 0; 1249 free(port->lun_map, M_CTL); 1250 port->lun_map = malloc(msg->port.lun_map_len, 1251 M_CTL, M_WAITOK); 1252 } 1253 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1254 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1255 i += msg->port.lun_map_len; 1256 } else { 1257 port->lun_map_size = 0; 1258 free(port->lun_map, M_CTL); 1259 port->lun_map = NULL; 1260 } 1261 if (msg->port.port_devid_len != 0) { 1262 if (port->port_devid == NULL || 1263 port->port_devid->len < msg->port.port_devid_len) { 1264 free(port->port_devid, M_CTL); 1265 port->port_devid = malloc(sizeof(struct ctl_devid) + 1266 msg->port.port_devid_len, M_CTL, M_WAITOK); 1267 } 1268 memcpy(port->port_devid->data, &msg->port.data[i], 1269 msg->port.port_devid_len); 1270 port->port_devid->len = msg->port.port_devid_len; 1271 i += msg->port.port_devid_len; 1272 } else { 1273 free(port->port_devid, M_CTL); 1274 port->port_devid = NULL; 1275 } 1276 if (msg->port.target_devid_len != 0) { 1277 if (port->target_devid == NULL || 1278 port->target_devid->len < msg->port.target_devid_len) { 1279 free(port->target_devid, M_CTL); 1280 port->target_devid = malloc(sizeof(struct ctl_devid) + 1281 msg->port.target_devid_len, M_CTL, M_WAITOK); 1282 } 1283 memcpy(port->target_devid->data, &msg->port.data[i], 1284 msg->port.target_devid_len); 1285 port->target_devid->len = msg->port.target_devid_len; 1286 i += msg->port.target_devid_len; 1287 } else { 1288 free(port->target_devid, M_CTL); 1289 port->target_devid = NULL; 1290 } 1291 if (msg->port.init_devid_len != 0) { 1292 if (port->init_devid == NULL || 1293 port->init_devid->len < msg->port.init_devid_len) { 1294 free(port->init_devid, M_CTL); 1295 port->init_devid = malloc(sizeof(struct ctl_devid) + 1296 msg->port.init_devid_len, M_CTL, M_WAITOK); 1297 } 1298 memcpy(port->init_devid->data, &msg->port.data[i], 1299 msg->port.init_devid_len); 1300 port->init_devid->len = msg->port.init_devid_len; 1301 i += msg->port.init_devid_len; 1302 } else { 1303 free(port->init_devid, M_CTL); 1304 port->init_devid = NULL; 1305 } 1306 if (new) { 1307 if (ctl_port_register(port) != 0) { 1308 printf("%s: ctl_port_register() failed with error\n", 1309 __func__); 1310 } 1311 } 1312 mtx_lock(&softc->ctl_lock); 1313 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1314 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1315 continue; 1316 mtx_lock(&lun->lun_lock); 1317 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1318 mtx_unlock(&lun->lun_lock); 1319 } 1320 mtx_unlock(&softc->ctl_lock); 1321 } 1322 1323 static void 1324 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1325 { 1326 struct ctl_port *port; 1327 int i, iid; 1328 1329 if (len < offsetof(struct ctl_ha_msg_iid, data[0])) { 1330 printf("%s: Received truncated message %d < %zu\n", 1331 __func__, len, offsetof(struct ctl_ha_msg_iid, data[0])); 1332 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1333 return; 1334 } 1335 i = msg->iid.name_len; 1336 if (len < offsetof(struct ctl_ha_msg_iid, data[i])) { 1337 printf("%s: Received truncated message data %d < %zu\n", 1338 __func__, len, offsetof(struct ctl_ha_msg_iid, data[i])); 1339 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1340 return; 1341 } 1342 1343 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1344 if (port == NULL) { 1345 printf("%s: Received IID for unknown port %d\n", 1346 __func__, msg->hdr.nexus.targ_port); 1347 return; 1348 } 1349 iid = msg->hdr.nexus.initid; 1350 if (port->wwpn_iid[iid].in_use != 0 && 1351 msg->iid.in_use == 0) 1352 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1353 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1354 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1355 free(port->wwpn_iid[iid].name, M_CTL); 1356 if (msg->iid.name_len) { 1357 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1358 msg->iid.name_len, M_CTL); 1359 } else 1360 port->wwpn_iid[iid].name = NULL; 1361 } 1362 1363 static void 1364 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1365 { 1366 1367 if (len < sizeof(msg->login)) { 1368 printf("%s: Received truncated message %d < %zu\n", 1369 __func__, len, sizeof(msg->login)); 1370 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1371 return; 1372 } 1373 1374 if (msg->login.version != CTL_HA_VERSION) { 1375 printf("CTL HA peers have different versions %d != %d\n", 1376 msg->login.version, CTL_HA_VERSION); 1377 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1378 return; 1379 } 1380 if (msg->login.ha_mode != softc->ha_mode) { 1381 printf("CTL HA peers have different ha_mode %d != %d\n", 1382 msg->login.ha_mode, softc->ha_mode); 1383 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1384 return; 1385 } 1386 if (msg->login.ha_id == softc->ha_id) { 1387 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1388 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1389 return; 1390 } 1391 if (msg->login.max_luns != ctl_max_luns || 1392 msg->login.max_ports != ctl_max_ports || 1393 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1394 printf("CTL HA peers have different limits\n"); 1395 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1396 return; 1397 } 1398 } 1399 1400 static void 1401 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1402 { 1403 struct ctl_lun *lun; 1404 u_int i; 1405 uint32_t initidx, targ_lun; 1406 1407 if (len < offsetof(struct ctl_ha_msg_mode, data[0])) { 1408 printf("%s: Received truncated message %d < %zu\n", 1409 __func__, len, offsetof(struct ctl_ha_msg_mode, data[0])); 1410 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1411 return; 1412 } 1413 i = msg->mode.page_len; 1414 if (len < offsetof(struct ctl_ha_msg_mode, data[i])) { 1415 printf("%s: Received truncated message data %d < %zu\n", 1416 __func__, len, offsetof(struct ctl_ha_msg_mode, data[i])); 1417 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1418 return; 1419 } 1420 1421 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1422 mtx_lock(&softc->ctl_lock); 1423 if (targ_lun >= ctl_max_luns || 1424 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1425 mtx_unlock(&softc->ctl_lock); 1426 return; 1427 } 1428 mtx_lock(&lun->lun_lock); 1429 mtx_unlock(&softc->ctl_lock); 1430 if (lun->flags & CTL_LUN_DISABLED) { 1431 mtx_unlock(&lun->lun_lock); 1432 return; 1433 } 1434 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1435 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1436 msg->mode.page_code && 1437 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1438 break; 1439 } 1440 if (i == CTL_NUM_MODE_PAGES) { 1441 mtx_unlock(&lun->lun_lock); 1442 return; 1443 } 1444 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1445 min(lun->mode_pages.index[i].page_len, msg->mode.page_len)); 1446 initidx = ctl_get_initindex(&msg->hdr.nexus); 1447 if (initidx != -1) 1448 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1449 mtx_unlock(&lun->lun_lock); 1450 } 1451 1452 /* 1453 * ISC (Inter Shelf Communication) event handler. Events from the HA 1454 * subsystem come in here. 1455 */ 1456 static void 1457 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1458 { 1459 struct ctl_softc *softc = control_softc; 1460 union ctl_io *io; 1461 struct ctl_prio *presio; 1462 ctl_ha_status isc_status; 1463 1464 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1465 if (event == CTL_HA_EVT_MSG_RECV) { 1466 union ctl_ha_msg *msg, msgbuf; 1467 1468 if (param > sizeof(msgbuf)) 1469 msg = malloc(param, M_CTL, M_WAITOK); 1470 else 1471 msg = &msgbuf; 1472 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1473 M_WAITOK); 1474 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1475 printf("%s: Error receiving message: %d\n", 1476 __func__, isc_status); 1477 if (msg != &msgbuf) 1478 free(msg, M_CTL); 1479 return; 1480 } 1481 1482 CTL_DEBUG_PRINT(("CTL: msg_type %d len %d\n", 1483 msg->hdr.msg_type, param)); 1484 switch (msg->hdr.msg_type) { 1485 case CTL_MSG_SERIALIZE: 1486 io = ctl_alloc_io(softc->othersc_pool); 1487 ctl_zero_io(io); 1488 // populate ctsio from msg 1489 io->io_hdr.io_type = CTL_IO_SCSI; 1490 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1491 io->io_hdr.remote_io = msg->hdr.original_sc; 1492 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1493 CTL_FLAG_IO_ACTIVE; 1494 /* 1495 * If we're in serialization-only mode, we don't 1496 * want to go through full done processing. Thus 1497 * the COPY flag. 1498 * 1499 * XXX KDM add another flag that is more specific. 1500 */ 1501 if (softc->ha_mode != CTL_HA_MODE_XFER) 1502 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1503 io->io_hdr.nexus = msg->hdr.nexus; 1504 io->scsiio.priority = msg->scsi.priority; 1505 io->scsiio.tag_num = msg->scsi.tag_num; 1506 io->scsiio.tag_type = msg->scsi.tag_type; 1507 #ifdef CTL_TIME_IO 1508 io->io_hdr.start_time = time_uptime; 1509 getbinuptime(&io->io_hdr.start_bt); 1510 #endif /* CTL_TIME_IO */ 1511 io->scsiio.cdb_len = msg->scsi.cdb_len; 1512 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1513 CTL_MAX_CDBLEN); 1514 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1515 const struct ctl_cmd_entry *entry; 1516 1517 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1518 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1519 io->io_hdr.flags |= 1520 entry->flags & CTL_FLAG_DATA_MASK; 1521 } 1522 ctl_enqueue_isc(io); 1523 break; 1524 1525 /* Performed on the Originating SC, XFER mode only */ 1526 case CTL_MSG_DATAMOVE: { 1527 struct ctl_sg_entry *sgl; 1528 int i, j; 1529 1530 io = msg->hdr.original_sc; 1531 if (io == NULL) { 1532 printf("%s: original_sc == NULL!\n", __func__); 1533 /* XXX KDM do something here */ 1534 break; 1535 } 1536 CTL_IO_ASSERT(io, SCSI); 1537 1538 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1539 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1540 /* 1541 * Keep track of this, we need to send it back over 1542 * when the datamove is complete. 1543 */ 1544 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1545 if (msg->hdr.status == CTL_SUCCESS) 1546 io->io_hdr.status = msg->hdr.status; 1547 1548 if (msg->dt.sg_sequence == 0) { 1549 #ifdef CTL_TIME_IO 1550 getbinuptime(&io->io_hdr.dma_start_bt); 1551 #endif 1552 i = msg->dt.kern_sg_entries + 1553 msg->dt.kern_data_len / 1554 CTL_HA_DATAMOVE_SEGMENT + 1; 1555 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1556 M_WAITOK | M_ZERO); 1557 CTL_RSGL(io) = sgl; 1558 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries]; 1559 1560 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1561 1562 io->scsiio.kern_sg_entries = 1563 msg->dt.kern_sg_entries; 1564 io->scsiio.rem_sg_entries = 1565 msg->dt.kern_sg_entries; 1566 io->scsiio.kern_data_len = 1567 msg->dt.kern_data_len; 1568 io->scsiio.kern_total_len = 1569 msg->dt.kern_total_len; 1570 io->scsiio.kern_data_resid = 1571 msg->dt.kern_data_resid; 1572 io->scsiio.kern_rel_offset = 1573 msg->dt.kern_rel_offset; 1574 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1575 io->io_hdr.flags |= msg->dt.flags & 1576 CTL_FLAG_BUS_ADDR; 1577 } else 1578 sgl = (struct ctl_sg_entry *) 1579 io->scsiio.kern_data_ptr; 1580 1581 for (i = msg->dt.sent_sg_entries, j = 0; 1582 i < (msg->dt.sent_sg_entries + 1583 msg->dt.cur_sg_entries); i++, j++) { 1584 sgl[i].addr = msg->dt.sg_list[j].addr; 1585 sgl[i].len = msg->dt.sg_list[j].len; 1586 } 1587 1588 /* 1589 * If this is the last piece of the I/O, we've got 1590 * the full S/G list. Queue processing in the thread. 1591 * Otherwise wait for the next piece. 1592 */ 1593 if (msg->dt.sg_last != 0) 1594 ctl_enqueue_isc(io); 1595 break; 1596 } 1597 /* Performed on the Serializing (primary) SC, XFER mode only */ 1598 case CTL_MSG_DATAMOVE_DONE: { 1599 if (msg->hdr.serializing_sc == NULL) { 1600 printf("%s: serializing_sc == NULL!\n", 1601 __func__); 1602 /* XXX KDM now what? */ 1603 break; 1604 } 1605 /* 1606 * We grab the sense information here in case 1607 * there was a failure, so we can return status 1608 * back to the initiator. 1609 */ 1610 io = msg->hdr.serializing_sc; 1611 CTL_IO_ASSERT(io, SCSI); 1612 1613 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1614 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1615 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1616 io->io_hdr.port_status = msg->scsi.port_status; 1617 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1618 if (msg->hdr.status != CTL_STATUS_NONE) { 1619 io->io_hdr.status = msg->hdr.status; 1620 io->scsiio.scsi_status = msg->scsi.scsi_status; 1621 io->scsiio.sense_len = msg->scsi.sense_len; 1622 memcpy(&io->scsiio.sense_data, 1623 &msg->scsi.sense_data, 1624 msg->scsi.sense_len); 1625 if (msg->hdr.status == CTL_SUCCESS) 1626 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1627 } 1628 ctl_enqueue_isc(io); 1629 break; 1630 } 1631 1632 /* Preformed on Originating SC, SER_ONLY mode */ 1633 case CTL_MSG_R2R: 1634 io = msg->hdr.original_sc; 1635 if (io == NULL) { 1636 printf("%s: original_sc == NULL!\n", 1637 __func__); 1638 break; 1639 } 1640 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1641 io->io_hdr.msg_type = CTL_MSG_R2R; 1642 io->io_hdr.remote_io = msg->hdr.serializing_sc; 1643 ctl_enqueue_isc(io); 1644 break; 1645 1646 /* 1647 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1648 * mode. 1649 * Performed on the Originating (i.e. secondary) SC in XFER 1650 * mode 1651 */ 1652 case CTL_MSG_FINISH_IO: 1653 if (softc->ha_mode == CTL_HA_MODE_XFER) 1654 ctl_isc_handler_finish_xfer(softc, msg); 1655 else 1656 ctl_isc_handler_finish_ser_only(softc, msg); 1657 break; 1658 1659 /* Preformed on Originating SC */ 1660 case CTL_MSG_BAD_JUJU: 1661 io = msg->hdr.original_sc; 1662 if (io == NULL) { 1663 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1664 __func__); 1665 break; 1666 } 1667 ctl_copy_sense_data(msg, io); 1668 /* 1669 * IO should have already been cleaned up on other 1670 * SC so clear this flag so we won't send a message 1671 * back to finish the IO there. 1672 */ 1673 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1674 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1675 1676 /* io = msg->hdr.serializing_sc; */ 1677 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1678 ctl_enqueue_isc(io); 1679 break; 1680 1681 /* Handle resets sent from the other side */ 1682 case CTL_MSG_MANAGE_TASKS: { 1683 struct ctl_taskio *taskio; 1684 taskio = (struct ctl_taskio *)ctl_alloc_io( 1685 softc->othersc_pool); 1686 ctl_zero_io((union ctl_io *)taskio); 1687 taskio->io_hdr.io_type = CTL_IO_TASK; 1688 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1689 taskio->io_hdr.nexus = msg->hdr.nexus; 1690 taskio->task_action = msg->task.task_action; 1691 taskio->tag_num = msg->task.tag_num; 1692 taskio->tag_type = msg->task.tag_type; 1693 #ifdef CTL_TIME_IO 1694 taskio->io_hdr.start_time = time_uptime; 1695 getbinuptime(&taskio->io_hdr.start_bt); 1696 #endif /* CTL_TIME_IO */ 1697 ctl_run_task((union ctl_io *)taskio); 1698 break; 1699 } 1700 /* Persistent Reserve action which needs attention */ 1701 case CTL_MSG_PERS_ACTION: 1702 presio = (struct ctl_prio *)ctl_alloc_io( 1703 softc->othersc_pool); 1704 ctl_zero_io((union ctl_io *)presio); 1705 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1706 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1707 presio->io_hdr.nexus = msg->hdr.nexus; 1708 presio->pr_msg = msg->pr; 1709 ctl_enqueue_isc((union ctl_io *)presio); 1710 break; 1711 case CTL_MSG_UA: 1712 ctl_isc_ua(softc, msg, param); 1713 break; 1714 case CTL_MSG_PORT_SYNC: 1715 ctl_isc_port_sync(softc, msg, param); 1716 break; 1717 case CTL_MSG_LUN_SYNC: 1718 ctl_isc_lun_sync(softc, msg, param); 1719 break; 1720 case CTL_MSG_IID_SYNC: 1721 ctl_isc_iid_sync(softc, msg, param); 1722 break; 1723 case CTL_MSG_LOGIN: 1724 ctl_isc_login(softc, msg, param); 1725 break; 1726 case CTL_MSG_MODE_SYNC: 1727 ctl_isc_mode_sync(softc, msg, param); 1728 break; 1729 default: 1730 printf("Received HA message of unknown type %d\n", 1731 msg->hdr.msg_type); 1732 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1733 break; 1734 } 1735 if (msg != &msgbuf) 1736 free(msg, M_CTL); 1737 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1738 printf("CTL: HA link status changed from %d to %d\n", 1739 softc->ha_link, param); 1740 if (param == softc->ha_link) 1741 return; 1742 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1743 softc->ha_link = param; 1744 ctl_isc_ha_link_down(softc); 1745 } else { 1746 softc->ha_link = param; 1747 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1748 ctl_isc_ha_link_up(softc); 1749 } 1750 return; 1751 } else { 1752 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1753 return; 1754 } 1755 } 1756 1757 static void 1758 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1759 { 1760 1761 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1762 src->scsi.sense_len); 1763 dest->scsiio.scsi_status = src->scsi.scsi_status; 1764 dest->scsiio.sense_len = src->scsi.sense_len; 1765 dest->io_hdr.status = src->hdr.status; 1766 } 1767 1768 static void 1769 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1770 { 1771 1772 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1773 src->scsiio.sense_len); 1774 dest->scsi.scsi_status = src->scsiio.scsi_status; 1775 dest->scsi.sense_len = src->scsiio.sense_len; 1776 dest->hdr.status = src->io_hdr.status; 1777 } 1778 1779 void 1780 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1781 { 1782 struct ctl_softc *softc = lun->ctl_softc; 1783 ctl_ua_type *pu; 1784 1785 if (initidx < softc->init_min || initidx >= softc->init_max) 1786 return; 1787 mtx_assert(&lun->lun_lock, MA_OWNED); 1788 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1789 if (pu == NULL) 1790 return; 1791 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1792 } 1793 1794 void 1795 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1796 { 1797 int i; 1798 1799 mtx_assert(&lun->lun_lock, MA_OWNED); 1800 if (lun->pending_ua[port] == NULL) 1801 return; 1802 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1803 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1804 continue; 1805 lun->pending_ua[port][i] |= ua; 1806 } 1807 } 1808 1809 void 1810 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1811 { 1812 struct ctl_softc *softc = lun->ctl_softc; 1813 int i; 1814 1815 mtx_assert(&lun->lun_lock, MA_OWNED); 1816 for (i = softc->port_min; i < softc->port_max; i++) 1817 ctl_est_ua_port(lun, i, except, ua); 1818 } 1819 1820 void 1821 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1822 { 1823 struct ctl_softc *softc = lun->ctl_softc; 1824 ctl_ua_type *pu; 1825 1826 if (initidx < softc->init_min || initidx >= softc->init_max) 1827 return; 1828 mtx_assert(&lun->lun_lock, MA_OWNED); 1829 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1830 if (pu == NULL) 1831 return; 1832 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1833 } 1834 1835 void 1836 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1837 { 1838 struct ctl_softc *softc = lun->ctl_softc; 1839 int i, j; 1840 1841 mtx_assert(&lun->lun_lock, MA_OWNED); 1842 for (i = softc->port_min; i < softc->port_max; i++) { 1843 if (lun->pending_ua[i] == NULL) 1844 continue; 1845 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1846 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1847 continue; 1848 lun->pending_ua[i][j] &= ~ua; 1849 } 1850 } 1851 } 1852 1853 void 1854 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1855 ctl_ua_type ua_type) 1856 { 1857 struct ctl_lun *lun; 1858 1859 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1860 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1861 mtx_lock(&lun->lun_lock); 1862 ctl_clr_ua(lun, initidx, ua_type); 1863 mtx_unlock(&lun->lun_lock); 1864 } 1865 } 1866 1867 static int 1868 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1869 { 1870 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1871 struct ctl_lun *lun; 1872 struct ctl_lun_req ireq; 1873 int error, value; 1874 1875 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1876 error = sysctl_handle_int(oidp, &value, 0, req); 1877 if ((error != 0) || (req->newptr == NULL)) 1878 return (error); 1879 1880 mtx_lock(&softc->ctl_lock); 1881 if (value == 0) 1882 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1883 else 1884 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1885 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1886 mtx_unlock(&softc->ctl_lock); 1887 bzero(&ireq, sizeof(ireq)); 1888 ireq.reqtype = CTL_LUNREQ_MODIFY; 1889 ireq.reqdata.modify.lun_id = lun->lun; 1890 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1891 curthread); 1892 if (ireq.status != CTL_LUN_OK) { 1893 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1894 __func__, ireq.status, ireq.error_str); 1895 } 1896 mtx_lock(&softc->ctl_lock); 1897 } 1898 mtx_unlock(&softc->ctl_lock); 1899 return (0); 1900 } 1901 1902 static int 1903 ctl_init(void) 1904 { 1905 struct make_dev_args args; 1906 struct ctl_softc *softc; 1907 int i, error; 1908 1909 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1910 M_WAITOK | M_ZERO); 1911 1912 make_dev_args_init(&args); 1913 args.mda_devsw = &ctl_cdevsw; 1914 args.mda_uid = UID_ROOT; 1915 args.mda_gid = GID_OPERATOR; 1916 args.mda_mode = 0600; 1917 args.mda_si_drv1 = softc; 1918 args.mda_si_drv2 = NULL; 1919 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1920 if (error != 0) { 1921 free(softc, M_DEVBUF); 1922 control_softc = NULL; 1923 return (error); 1924 } 1925 1926 sysctl_ctx_init(&softc->sysctl_ctx); 1927 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1928 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1929 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer"); 1930 1931 if (softc->sysctl_tree == NULL) { 1932 printf("%s: unable to allocate sysctl tree\n", __func__); 1933 destroy_dev(softc->dev); 1934 free(softc, M_DEVBUF); 1935 control_softc = NULL; 1936 return (ENOMEM); 1937 } 1938 1939 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1940 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1941 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1942 softc->flags = 0; 1943 1944 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1945 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1946 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1947 1948 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) { 1949 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n", 1950 ctl_max_luns, CTL_DEFAULT_MAX_LUNS); 1951 ctl_max_luns = CTL_DEFAULT_MAX_LUNS; 1952 } 1953 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns, 1954 M_DEVBUF, M_WAITOK | M_ZERO); 1955 softc->ctl_lun_mask = malloc(sizeof(uint32_t) * 1956 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1957 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) { 1958 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n", 1959 ctl_max_ports, CTL_DEFAULT_MAX_PORTS); 1960 ctl_max_ports = CTL_DEFAULT_MAX_PORTS; 1961 } 1962 softc->ctl_port_mask = malloc(sizeof(uint32_t) * 1963 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO); 1964 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports, 1965 M_DEVBUF, M_WAITOK | M_ZERO); 1966 1967 /* 1968 * In Copan's HA scheme, the "master" and "slave" roles are 1969 * figured out through the slot the controller is in. Although it 1970 * is an active/active system, someone has to be in charge. 1971 */ 1972 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1973 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1974 "HA head ID (0 - no HA)"); 1975 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1976 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1977 softc->is_single = 1; 1978 softc->port_cnt = ctl_max_ports; 1979 softc->port_min = 0; 1980 } else { 1981 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES; 1982 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1983 } 1984 softc->port_max = softc->port_min + softc->port_cnt; 1985 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1986 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1987 1988 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1989 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1990 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1991 1992 STAILQ_INIT(&softc->lun_list); 1993 STAILQ_INIT(&softc->fe_list); 1994 STAILQ_INIT(&softc->port_list); 1995 STAILQ_INIT(&softc->be_list); 1996 ctl_tpc_init(softc); 1997 1998 if (worker_threads <= 0) 1999 worker_threads = max(1, mp_ncpus / 4); 2000 if (worker_threads > CTL_MAX_THREADS) 2001 worker_threads = CTL_MAX_THREADS; 2002 2003 for (i = 0; i < worker_threads; i++) { 2004 struct ctl_thread *thr = &softc->threads[i]; 2005 2006 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 2007 thr->ctl_softc = softc; 2008 STAILQ_INIT(&thr->incoming_queue); 2009 STAILQ_INIT(&thr->rtr_queue); 2010 STAILQ_INIT(&thr->done_queue); 2011 STAILQ_INIT(&thr->isc_queue); 2012 2013 error = kproc_kthread_add(ctl_work_thread, thr, 2014 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 2015 if (error != 0) { 2016 printf("error creating CTL work thread!\n"); 2017 return (error); 2018 } 2019 } 2020 error = kproc_kthread_add(ctl_thresh_thread, softc, 2021 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 2022 if (error != 0) { 2023 printf("error creating CTL threshold thread!\n"); 2024 return (error); 2025 } 2026 2027 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 2028 OID_AUTO, "ha_role", 2029 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 2030 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 2031 2032 if (softc->is_single == 0) { 2033 if (ctl_frontend_register(&ha_frontend) != 0) 2034 softc->is_single = 1; 2035 } 2036 return (0); 2037 } 2038 2039 static int 2040 ctl_shutdown(void) 2041 { 2042 struct ctl_softc *softc = control_softc; 2043 int i; 2044 2045 if (softc->is_single == 0) 2046 ctl_frontend_deregister(&ha_frontend); 2047 2048 destroy_dev(softc->dev); 2049 2050 /* Shutdown CTL threads. */ 2051 softc->shutdown = 1; 2052 for (i = 0; i < worker_threads; i++) { 2053 struct ctl_thread *thr = &softc->threads[i]; 2054 while (thr->thread != NULL) { 2055 wakeup(thr); 2056 if (thr->thread != NULL) 2057 pause("CTL thr shutdown", 1); 2058 } 2059 mtx_destroy(&thr->queue_lock); 2060 } 2061 while (softc->thresh_thread != NULL) { 2062 wakeup(softc->thresh_thread); 2063 if (softc->thresh_thread != NULL) 2064 pause("CTL thr shutdown", 1); 2065 } 2066 2067 ctl_tpc_shutdown(softc); 2068 uma_zdestroy(softc->io_zone); 2069 mtx_destroy(&softc->ctl_lock); 2070 2071 free(softc->ctl_luns, M_DEVBUF); 2072 free(softc->ctl_lun_mask, M_DEVBUF); 2073 free(softc->ctl_port_mask, M_DEVBUF); 2074 free(softc->ctl_ports, M_DEVBUF); 2075 2076 sysctl_ctx_free(&softc->sysctl_ctx); 2077 2078 free(softc, M_DEVBUF); 2079 control_softc = NULL; 2080 return (0); 2081 } 2082 2083 static int 2084 ctl_module_event_handler(module_t mod, int what, void *arg) 2085 { 2086 2087 switch (what) { 2088 case MOD_LOAD: 2089 return (ctl_init()); 2090 case MOD_UNLOAD: 2091 return (ctl_shutdown()); 2092 default: 2093 return (EOPNOTSUPP); 2094 } 2095 } 2096 2097 /* 2098 * XXX KDM should we do some access checks here? Bump a reference count to 2099 * prevent a CTL module from being unloaded while someone has it open? 2100 */ 2101 static int 2102 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2103 { 2104 return (0); 2105 } 2106 2107 static int 2108 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2109 { 2110 return (0); 2111 } 2112 2113 /* 2114 * Remove an initiator by port number and initiator ID. 2115 * Returns 0 for success, -1 for failure. 2116 */ 2117 int 2118 ctl_remove_initiator(struct ctl_port *port, int iid) 2119 { 2120 struct ctl_softc *softc = port->ctl_softc; 2121 int last; 2122 2123 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2124 2125 if (iid > CTL_MAX_INIT_PER_PORT) { 2126 printf("%s: initiator ID %u > maximun %u!\n", 2127 __func__, iid, CTL_MAX_INIT_PER_PORT); 2128 return (-1); 2129 } 2130 2131 mtx_lock(&softc->ctl_lock); 2132 last = (--port->wwpn_iid[iid].in_use == 0); 2133 port->wwpn_iid[iid].last_use = time_uptime; 2134 mtx_unlock(&softc->ctl_lock); 2135 if (last) 2136 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2137 ctl_isc_announce_iid(port, iid); 2138 2139 return (0); 2140 } 2141 2142 /* 2143 * Add an initiator to the initiator map. 2144 * Returns iid for success, < 0 for failure. 2145 */ 2146 int 2147 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2148 { 2149 struct ctl_softc *softc = port->ctl_softc; 2150 time_t best_time; 2151 int i, best; 2152 2153 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2154 2155 if (iid >= CTL_MAX_INIT_PER_PORT) { 2156 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2157 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2158 free(name, M_CTL); 2159 return (-1); 2160 } 2161 2162 mtx_lock(&softc->ctl_lock); 2163 2164 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2165 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2166 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2167 iid = i; 2168 break; 2169 } 2170 if (name != NULL && port->wwpn_iid[i].name != NULL && 2171 strcmp(name, port->wwpn_iid[i].name) == 0) { 2172 iid = i; 2173 break; 2174 } 2175 } 2176 } 2177 2178 if (iid < 0) { 2179 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2180 if (port->wwpn_iid[i].in_use == 0 && 2181 port->wwpn_iid[i].wwpn == 0 && 2182 port->wwpn_iid[i].name == NULL) { 2183 iid = i; 2184 break; 2185 } 2186 } 2187 } 2188 2189 if (iid < 0) { 2190 best = -1; 2191 best_time = INT32_MAX; 2192 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2193 if (port->wwpn_iid[i].in_use == 0) { 2194 if (port->wwpn_iid[i].last_use < best_time) { 2195 best = i; 2196 best_time = port->wwpn_iid[i].last_use; 2197 } 2198 } 2199 } 2200 iid = best; 2201 } 2202 2203 if (iid < 0) { 2204 mtx_unlock(&softc->ctl_lock); 2205 free(name, M_CTL); 2206 return (-2); 2207 } 2208 2209 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2210 /* 2211 * This is not an error yet. 2212 */ 2213 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2214 #if 0 2215 printf("%s: port %d iid %u WWPN %#jx arrived" 2216 " again\n", __func__, port->targ_port, 2217 iid, (uintmax_t)wwpn); 2218 #endif 2219 goto take; 2220 } 2221 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2222 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2223 #if 0 2224 printf("%s: port %d iid %u name '%s' arrived" 2225 " again\n", __func__, port->targ_port, 2226 iid, name); 2227 #endif 2228 goto take; 2229 } 2230 2231 /* 2232 * This is an error, but what do we do about it? The 2233 * driver is telling us we have a new WWPN for this 2234 * initiator ID, so we pretty much need to use it. 2235 */ 2236 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2237 " but WWPN %#jx '%s' is still at that address\n", 2238 __func__, port->targ_port, iid, wwpn, name, 2239 (uintmax_t)port->wwpn_iid[iid].wwpn, 2240 port->wwpn_iid[iid].name); 2241 } 2242 take: 2243 free(port->wwpn_iid[iid].name, M_CTL); 2244 port->wwpn_iid[iid].name = name; 2245 port->wwpn_iid[iid].wwpn = wwpn; 2246 port->wwpn_iid[iid].in_use++; 2247 mtx_unlock(&softc->ctl_lock); 2248 ctl_isc_announce_iid(port, iid); 2249 2250 return (iid); 2251 } 2252 2253 static int 2254 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2255 { 2256 int len; 2257 2258 switch (port->port_type) { 2259 case CTL_PORT_FC: 2260 { 2261 struct scsi_transportid_fcp *id = 2262 (struct scsi_transportid_fcp *)buf; 2263 if (port->wwpn_iid[iid].wwpn == 0) 2264 return (0); 2265 memset(id, 0, sizeof(*id)); 2266 id->format_protocol = SCSI_PROTO_FC; 2267 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2268 return (sizeof(*id)); 2269 } 2270 case CTL_PORT_ISCSI: 2271 { 2272 struct scsi_transportid_iscsi_port *id = 2273 (struct scsi_transportid_iscsi_port *)buf; 2274 if (port->wwpn_iid[iid].name == NULL) 2275 return (0); 2276 memset(id, 0, 256); 2277 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2278 SCSI_PROTO_ISCSI; 2279 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2280 len = roundup2(min(len, 252), 4); 2281 scsi_ulto2b(len, id->additional_length); 2282 return (sizeof(*id) + len); 2283 } 2284 case CTL_PORT_SAS: 2285 { 2286 struct scsi_transportid_sas *id = 2287 (struct scsi_transportid_sas *)buf; 2288 if (port->wwpn_iid[iid].wwpn == 0) 2289 return (0); 2290 memset(id, 0, sizeof(*id)); 2291 id->format_protocol = SCSI_PROTO_SAS; 2292 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2293 return (sizeof(*id)); 2294 } 2295 default: 2296 { 2297 struct scsi_transportid_spi *id = 2298 (struct scsi_transportid_spi *)buf; 2299 memset(id, 0, sizeof(*id)); 2300 id->format_protocol = SCSI_PROTO_SPI; 2301 scsi_ulto2b(iid, id->scsi_addr); 2302 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2303 return (sizeof(*id)); 2304 } 2305 } 2306 } 2307 2308 /* 2309 * Serialize a command that went down the "wrong" side, and so was sent to 2310 * this controller for execution. The logic is a little different than the 2311 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2312 * sent back to the other side, but in the success case, we execute the 2313 * command on this side (XFER mode) or tell the other side to execute it 2314 * (SER_ONLY mode). 2315 */ 2316 static void 2317 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2318 { 2319 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2320 struct ctl_port *port = CTL_PORT(ctsio); 2321 union ctl_ha_msg msg_info; 2322 struct ctl_lun *lun; 2323 const struct ctl_cmd_entry *entry; 2324 union ctl_io *bio; 2325 uint32_t targ_lun; 2326 2327 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2328 2329 /* Make sure that we know about this port. */ 2330 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2331 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2332 /*retry_count*/ 1); 2333 goto badjuju; 2334 } 2335 2336 /* Make sure that we know about this LUN. */ 2337 mtx_lock(&softc->ctl_lock); 2338 if (targ_lun >= ctl_max_luns || 2339 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2340 mtx_unlock(&softc->ctl_lock); 2341 2342 /* 2343 * The other node would not send this request to us unless 2344 * received announce that we are primary node for this LUN. 2345 * If this LUN does not exist now, it is probably result of 2346 * a race, so respond to initiator in the most opaque way. 2347 */ 2348 ctl_set_busy(ctsio); 2349 goto badjuju; 2350 } 2351 mtx_lock(&lun->lun_lock); 2352 mtx_unlock(&softc->ctl_lock); 2353 2354 /* 2355 * If the LUN is invalid, pretend that it doesn't exist. 2356 * It will go away as soon as all pending I/Os completed. 2357 */ 2358 if (lun->flags & CTL_LUN_DISABLED) { 2359 mtx_unlock(&lun->lun_lock); 2360 ctl_set_busy(ctsio); 2361 goto badjuju; 2362 } 2363 2364 entry = ctl_get_cmd_entry(ctsio, NULL); 2365 ctsio->seridx = entry->seridx; 2366 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2367 mtx_unlock(&lun->lun_lock); 2368 goto badjuju; 2369 } 2370 2371 CTL_LUN(ctsio) = lun; 2372 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2373 2374 /* 2375 * Every I/O goes into the OOA queue for a 2376 * particular LUN, and stays there until completion. 2377 */ 2378 #ifdef CTL_TIME_IO 2379 if (LIST_EMPTY(&lun->ooa_queue)) 2380 lun->idle_time += getsbinuptime() - lun->last_busy; 2381 #endif 2382 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2383 2384 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 2385 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 2386 case CTL_ACTION_PASS: 2387 case CTL_ACTION_SKIP: 2388 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2389 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2390 ctl_enqueue_rtr((union ctl_io *)ctsio); 2391 mtx_unlock(&lun->lun_lock); 2392 } else { 2393 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2394 mtx_unlock(&lun->lun_lock); 2395 2396 /* send msg back to other side */ 2397 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2398 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2399 msg_info.hdr.msg_type = CTL_MSG_R2R; 2400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2401 sizeof(msg_info.hdr), M_WAITOK); 2402 } 2403 break; 2404 case CTL_ACTION_BLOCK: 2405 ctsio->io_hdr.blocker = bio; 2406 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 2407 blocked_links); 2408 mtx_unlock(&lun->lun_lock); 2409 break; 2410 case CTL_ACTION_OVERLAP: 2411 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2412 mtx_unlock(&lun->lun_lock); 2413 ctl_set_overlapped_cmd(ctsio); 2414 goto badjuju; 2415 case CTL_ACTION_OVERLAP_TAG: 2416 LIST_REMOVE(&ctsio->io_hdr, ooa_links); 2417 mtx_unlock(&lun->lun_lock); 2418 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 2419 badjuju: 2420 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2421 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io; 2422 msg_info.hdr.serializing_sc = NULL; 2423 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2424 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2425 sizeof(msg_info.scsi), M_WAITOK); 2426 ctl_free_io((union ctl_io *)ctsio); 2427 break; 2428 default: 2429 __assert_unreachable(); 2430 } 2431 } 2432 2433 /* 2434 * Returns 0 for success, errno for failure. 2435 */ 2436 static void 2437 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2438 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2439 { 2440 struct ctl_io_hdr *ioh; 2441 2442 mtx_lock(&lun->lun_lock); 2443 ioh = LIST_FIRST(&lun->ooa_queue); 2444 if (ioh == NULL) { 2445 mtx_unlock(&lun->lun_lock); 2446 return; 2447 } 2448 while (LIST_NEXT(ioh, ooa_links) != NULL) 2449 ioh = LIST_NEXT(ioh, ooa_links); 2450 for ( ; ioh; ioh = LIST_PREV(ioh, &lun->ooa_queue, ctl_io_hdr, ooa_links)) { 2451 union ctl_io *io = (union ctl_io *)ioh; 2452 struct ctl_ooa_entry *entry; 2453 2454 CTL_IO_ASSERT(io, SCSI); 2455 2456 /* 2457 * If we've got more than we can fit, just count the 2458 * remaining entries. 2459 */ 2460 if (*cur_fill_num >= ooa_hdr->alloc_num) { 2461 (*cur_fill_num)++; 2462 continue; 2463 } 2464 2465 entry = &kern_entries[*cur_fill_num]; 2466 2467 entry->tag_num = io->scsiio.tag_num; 2468 entry->tag_type = io->scsiio.tag_type; 2469 entry->lun_num = lun->lun; 2470 #ifdef CTL_TIME_IO 2471 entry->start_bt = io->io_hdr.start_bt; 2472 #endif 2473 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2474 entry->cdb_len = io->scsiio.cdb_len; 2475 if (io->io_hdr.blocker != NULL) 2476 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2477 2478 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2479 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2480 2481 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2482 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2483 2484 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2485 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2486 2487 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2488 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2489 2490 if (io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) 2491 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_QUEUED; 2492 2493 if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) 2494 entry->cmd_flags |= CTL_OOACMD_FLAG_STATUS_SENT; 2495 (*cur_fill_num)++; 2496 } 2497 mtx_unlock(&lun->lun_lock); 2498 } 2499 2500 /* 2501 * Escape characters that are illegal or not recommended in XML. 2502 */ 2503 int 2504 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2505 { 2506 char *end = str + size; 2507 int retval; 2508 2509 retval = 0; 2510 2511 for (; *str && str < end; str++) { 2512 switch (*str) { 2513 case '&': 2514 retval = sbuf_cat(sb, "&"); 2515 break; 2516 case '>': 2517 retval = sbuf_cat(sb, ">"); 2518 break; 2519 case '<': 2520 retval = sbuf_cat(sb, "<"); 2521 break; 2522 default: 2523 retval = sbuf_putc(sb, *str); 2524 break; 2525 } 2526 2527 if (retval != 0) 2528 break; 2529 } 2530 2531 return (retval); 2532 } 2533 2534 static void 2535 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2536 { 2537 struct scsi_vpd_id_descriptor *desc; 2538 int i; 2539 2540 if (id == NULL || id->len < 4) 2541 return; 2542 desc = (struct scsi_vpd_id_descriptor *)id->data; 2543 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2544 case SVPD_ID_TYPE_T10: 2545 sbuf_cat(sb, "t10."); 2546 break; 2547 case SVPD_ID_TYPE_EUI64: 2548 sbuf_cat(sb, "eui."); 2549 break; 2550 case SVPD_ID_TYPE_NAA: 2551 sbuf_cat(sb, "naa."); 2552 break; 2553 case SVPD_ID_TYPE_SCSI_NAME: 2554 break; 2555 } 2556 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2557 case SVPD_ID_CODESET_BINARY: 2558 for (i = 0; i < desc->length; i++) 2559 sbuf_printf(sb, "%02x", desc->identifier[i]); 2560 break; 2561 case SVPD_ID_CODESET_ASCII: 2562 sbuf_printf(sb, "%.*s", (int)desc->length, 2563 (char *)desc->identifier); 2564 break; 2565 case SVPD_ID_CODESET_UTF8: 2566 sbuf_cat(sb, (char *)desc->identifier); 2567 break; 2568 } 2569 } 2570 2571 static int 2572 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2573 struct thread *td) 2574 { 2575 struct ctl_softc *softc = dev->si_drv1; 2576 struct ctl_port *port; 2577 struct ctl_lun *lun; 2578 int retval; 2579 2580 retval = 0; 2581 2582 switch (cmd) { 2583 case CTL_IO: 2584 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2585 break; 2586 case CTL_ENABLE_PORT: 2587 case CTL_DISABLE_PORT: 2588 case CTL_SET_PORT_WWNS: { 2589 struct ctl_port *port; 2590 struct ctl_port_entry *entry; 2591 2592 entry = (struct ctl_port_entry *)addr; 2593 2594 mtx_lock(&softc->ctl_lock); 2595 STAILQ_FOREACH(port, &softc->port_list, links) { 2596 int action, done; 2597 2598 if (port->targ_port < softc->port_min || 2599 port->targ_port >= softc->port_max) 2600 continue; 2601 2602 action = 0; 2603 done = 0; 2604 if ((entry->port_type == CTL_PORT_NONE) 2605 && (entry->targ_port == port->targ_port)) { 2606 /* 2607 * If the user only wants to enable or 2608 * disable or set WWNs on a specific port, 2609 * do the operation and we're done. 2610 */ 2611 action = 1; 2612 done = 1; 2613 } else if (entry->port_type & port->port_type) { 2614 /* 2615 * Compare the user's type mask with the 2616 * particular frontend type to see if we 2617 * have a match. 2618 */ 2619 action = 1; 2620 done = 0; 2621 2622 /* 2623 * Make sure the user isn't trying to set 2624 * WWNs on multiple ports at the same time. 2625 */ 2626 if (cmd == CTL_SET_PORT_WWNS) { 2627 printf("%s: Can't set WWNs on " 2628 "multiple ports\n", __func__); 2629 retval = EINVAL; 2630 break; 2631 } 2632 } 2633 if (action == 0) 2634 continue; 2635 2636 /* 2637 * XXX KDM we have to drop the lock here, because 2638 * the online/offline operations can potentially 2639 * block. We need to reference count the frontends 2640 * so they can't go away, 2641 */ 2642 if (cmd == CTL_ENABLE_PORT) { 2643 mtx_unlock(&softc->ctl_lock); 2644 ctl_port_online(port); 2645 mtx_lock(&softc->ctl_lock); 2646 } else if (cmd == CTL_DISABLE_PORT) { 2647 mtx_unlock(&softc->ctl_lock); 2648 ctl_port_offline(port); 2649 mtx_lock(&softc->ctl_lock); 2650 } else if (cmd == CTL_SET_PORT_WWNS) { 2651 ctl_port_set_wwns(port, 2652 (entry->flags & CTL_PORT_WWNN_VALID) ? 2653 1 : 0, entry->wwnn, 2654 (entry->flags & CTL_PORT_WWPN_VALID) ? 2655 1 : 0, entry->wwpn); 2656 } 2657 if (done != 0) 2658 break; 2659 } 2660 mtx_unlock(&softc->ctl_lock); 2661 break; 2662 } 2663 case CTL_GET_OOA: { 2664 struct ctl_ooa *ooa_hdr; 2665 struct ctl_ooa_entry *entries; 2666 uint32_t cur_fill_num; 2667 2668 ooa_hdr = (struct ctl_ooa *)addr; 2669 2670 if ((ooa_hdr->alloc_len == 0) 2671 || (ooa_hdr->alloc_num == 0)) { 2672 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2673 "must be non-zero\n", __func__, 2674 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2675 retval = EINVAL; 2676 break; 2677 } 2678 2679 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2680 sizeof(struct ctl_ooa_entry))) { 2681 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2682 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2683 __func__, ooa_hdr->alloc_len, 2684 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2685 retval = EINVAL; 2686 break; 2687 } 2688 2689 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2690 if (entries == NULL) { 2691 printf("%s: could not allocate %d bytes for OOA " 2692 "dump\n", __func__, ooa_hdr->alloc_len); 2693 retval = ENOMEM; 2694 break; 2695 } 2696 2697 mtx_lock(&softc->ctl_lock); 2698 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2699 (ooa_hdr->lun_num >= ctl_max_luns || 2700 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2701 mtx_unlock(&softc->ctl_lock); 2702 free(entries, M_CTL); 2703 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2704 __func__, (uintmax_t)ooa_hdr->lun_num); 2705 retval = EINVAL; 2706 break; 2707 } 2708 2709 cur_fill_num = 0; 2710 2711 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2712 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2713 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2714 ooa_hdr, entries); 2715 } 2716 } else { 2717 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2718 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2719 entries); 2720 } 2721 mtx_unlock(&softc->ctl_lock); 2722 2723 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2724 ooa_hdr->fill_len = ooa_hdr->fill_num * 2725 sizeof(struct ctl_ooa_entry); 2726 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2727 if (retval != 0) { 2728 printf("%s: error copying out %d bytes for OOA dump\n", 2729 __func__, ooa_hdr->fill_len); 2730 } 2731 2732 getbinuptime(&ooa_hdr->cur_bt); 2733 2734 if (cur_fill_num > ooa_hdr->alloc_num) { 2735 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2736 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2737 } else { 2738 ooa_hdr->dropped_num = 0; 2739 ooa_hdr->status = CTL_OOA_OK; 2740 } 2741 2742 free(entries, M_CTL); 2743 break; 2744 } 2745 case CTL_DELAY_IO: { 2746 struct ctl_io_delay_info *delay_info; 2747 2748 delay_info = (struct ctl_io_delay_info *)addr; 2749 2750 #ifdef CTL_IO_DELAY 2751 mtx_lock(&softc->ctl_lock); 2752 if (delay_info->lun_id >= ctl_max_luns || 2753 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2754 mtx_unlock(&softc->ctl_lock); 2755 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2756 break; 2757 } 2758 mtx_lock(&lun->lun_lock); 2759 mtx_unlock(&softc->ctl_lock); 2760 delay_info->status = CTL_DELAY_STATUS_OK; 2761 switch (delay_info->delay_type) { 2762 case CTL_DELAY_TYPE_CONT: 2763 case CTL_DELAY_TYPE_ONESHOT: 2764 break; 2765 default: 2766 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2767 break; 2768 } 2769 switch (delay_info->delay_loc) { 2770 case CTL_DELAY_LOC_DATAMOVE: 2771 lun->delay_info.datamove_type = delay_info->delay_type; 2772 lun->delay_info.datamove_delay = delay_info->delay_secs; 2773 break; 2774 case CTL_DELAY_LOC_DONE: 2775 lun->delay_info.done_type = delay_info->delay_type; 2776 lun->delay_info.done_delay = delay_info->delay_secs; 2777 break; 2778 default: 2779 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2780 break; 2781 } 2782 mtx_unlock(&lun->lun_lock); 2783 #else 2784 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2785 #endif /* CTL_IO_DELAY */ 2786 break; 2787 } 2788 case CTL_ERROR_INJECT: { 2789 struct ctl_error_desc *err_desc, *new_err_desc; 2790 2791 err_desc = (struct ctl_error_desc *)addr; 2792 2793 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2794 M_WAITOK | M_ZERO); 2795 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2796 2797 mtx_lock(&softc->ctl_lock); 2798 if (err_desc->lun_id >= ctl_max_luns || 2799 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2800 mtx_unlock(&softc->ctl_lock); 2801 free(new_err_desc, M_CTL); 2802 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2803 __func__, (uintmax_t)err_desc->lun_id); 2804 retval = EINVAL; 2805 break; 2806 } 2807 mtx_lock(&lun->lun_lock); 2808 mtx_unlock(&softc->ctl_lock); 2809 2810 /* 2811 * We could do some checking here to verify the validity 2812 * of the request, but given the complexity of error 2813 * injection requests, the checking logic would be fairly 2814 * complex. 2815 * 2816 * For now, if the request is invalid, it just won't get 2817 * executed and might get deleted. 2818 */ 2819 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2820 2821 /* 2822 * XXX KDM check to make sure the serial number is unique, 2823 * in case we somehow manage to wrap. That shouldn't 2824 * happen for a very long time, but it's the right thing to 2825 * do. 2826 */ 2827 new_err_desc->serial = lun->error_serial; 2828 err_desc->serial = lun->error_serial; 2829 lun->error_serial++; 2830 2831 mtx_unlock(&lun->lun_lock); 2832 break; 2833 } 2834 case CTL_ERROR_INJECT_DELETE: { 2835 struct ctl_error_desc *delete_desc, *desc, *desc2; 2836 int delete_done; 2837 2838 delete_desc = (struct ctl_error_desc *)addr; 2839 delete_done = 0; 2840 2841 mtx_lock(&softc->ctl_lock); 2842 if (delete_desc->lun_id >= ctl_max_luns || 2843 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2844 mtx_unlock(&softc->ctl_lock); 2845 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2846 __func__, (uintmax_t)delete_desc->lun_id); 2847 retval = EINVAL; 2848 break; 2849 } 2850 mtx_lock(&lun->lun_lock); 2851 mtx_unlock(&softc->ctl_lock); 2852 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2853 if (desc->serial != delete_desc->serial) 2854 continue; 2855 2856 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2857 links); 2858 free(desc, M_CTL); 2859 delete_done = 1; 2860 } 2861 mtx_unlock(&lun->lun_lock); 2862 if (delete_done == 0) { 2863 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2864 "error serial %ju on LUN %u\n", __func__, 2865 delete_desc->serial, delete_desc->lun_id); 2866 retval = EINVAL; 2867 break; 2868 } 2869 break; 2870 } 2871 case CTL_DUMP_STRUCTS: { 2872 int j, k; 2873 struct ctl_port *port; 2874 struct ctl_frontend *fe; 2875 2876 mtx_lock(&softc->ctl_lock); 2877 printf("CTL Persistent Reservation information start:\n"); 2878 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2879 mtx_lock(&lun->lun_lock); 2880 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2881 mtx_unlock(&lun->lun_lock); 2882 continue; 2883 } 2884 2885 for (j = 0; j < ctl_max_ports; j++) { 2886 if (lun->pr_keys[j] == NULL) 2887 continue; 2888 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2889 if (lun->pr_keys[j][k] == 0) 2890 continue; 2891 printf(" LUN %ju port %d iid %d key " 2892 "%#jx\n", lun->lun, j, k, 2893 (uintmax_t)lun->pr_keys[j][k]); 2894 } 2895 } 2896 mtx_unlock(&lun->lun_lock); 2897 } 2898 printf("CTL Persistent Reservation information end\n"); 2899 printf("CTL Ports:\n"); 2900 STAILQ_FOREACH(port, &softc->port_list, links) { 2901 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2902 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2903 port->frontend->name, port->port_type, 2904 port->physical_port, port->virtual_port, 2905 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2906 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2907 if (port->wwpn_iid[j].in_use == 0 && 2908 port->wwpn_iid[j].wwpn == 0 && 2909 port->wwpn_iid[j].name == NULL) 2910 continue; 2911 2912 printf(" iid %u use %d WWPN %#jx '%s'\n", 2913 j, port->wwpn_iid[j].in_use, 2914 (uintmax_t)port->wwpn_iid[j].wwpn, 2915 port->wwpn_iid[j].name); 2916 } 2917 } 2918 printf("CTL Port information end\n"); 2919 mtx_unlock(&softc->ctl_lock); 2920 /* 2921 * XXX KDM calling this without a lock. We'd likely want 2922 * to drop the lock before calling the frontend's dump 2923 * routine anyway. 2924 */ 2925 printf("CTL Frontends:\n"); 2926 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2927 printf(" Frontend '%s'\n", fe->name); 2928 if (fe->fe_dump != NULL) 2929 fe->fe_dump(); 2930 } 2931 printf("CTL Frontend information end\n"); 2932 break; 2933 } 2934 case CTL_LUN_REQ: { 2935 struct ctl_lun_req *lun_req; 2936 struct ctl_backend_driver *backend; 2937 void *packed; 2938 nvlist_t *tmp_args_nvl; 2939 size_t packed_len; 2940 2941 lun_req = (struct ctl_lun_req *)addr; 2942 tmp_args_nvl = lun_req->args_nvl; 2943 2944 backend = ctl_backend_find(lun_req->backend); 2945 if (backend == NULL) { 2946 lun_req->status = CTL_LUN_ERROR; 2947 snprintf(lun_req->error_str, 2948 sizeof(lun_req->error_str), 2949 "Backend \"%s\" not found.", 2950 lun_req->backend); 2951 break; 2952 } 2953 2954 if (lun_req->args != NULL) { 2955 if (lun_req->args_len > CTL_MAX_ARGS_LEN) { 2956 lun_req->status = CTL_LUN_ERROR; 2957 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2958 "Too big args."); 2959 break; 2960 } 2961 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK); 2962 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) { 2963 free(packed, M_CTL); 2964 lun_req->status = CTL_LUN_ERROR; 2965 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2966 "Cannot copyin args."); 2967 break; 2968 } 2969 lun_req->args_nvl = nvlist_unpack(packed, 2970 lun_req->args_len, 0); 2971 free(packed, M_CTL); 2972 2973 if (lun_req->args_nvl == NULL) { 2974 lun_req->status = CTL_LUN_ERROR; 2975 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 2976 "Cannot unpack args nvlist."); 2977 break; 2978 } 2979 } else 2980 lun_req->args_nvl = nvlist_create(0); 2981 2982 lun_req->result_nvl = NULL; 2983 retval = backend->ioctl(dev, cmd, addr, flag, td); 2984 nvlist_destroy(lun_req->args_nvl); 2985 lun_req->args_nvl = tmp_args_nvl; 2986 2987 if (lun_req->result_nvl != NULL) { 2988 if (lun_req->result != NULL) { 2989 packed = nvlist_pack(lun_req->result_nvl, 2990 &packed_len); 2991 if (packed == NULL) { 2992 lun_req->status = CTL_LUN_ERROR; 2993 snprintf(lun_req->error_str, 2994 sizeof(lun_req->error_str), 2995 "Cannot pack result nvlist."); 2996 break; 2997 } 2998 2999 if (packed_len > lun_req->result_len) { 3000 lun_req->status = CTL_LUN_ERROR; 3001 snprintf(lun_req->error_str, 3002 sizeof(lun_req->error_str), 3003 "Result nvlist too large."); 3004 free(packed, M_NVLIST); 3005 break; 3006 } 3007 3008 if (copyout(packed, lun_req->result, packed_len)) { 3009 lun_req->status = CTL_LUN_ERROR; 3010 snprintf(lun_req->error_str, 3011 sizeof(lun_req->error_str), 3012 "Cannot copyout() the result."); 3013 free(packed, M_NVLIST); 3014 break; 3015 } 3016 3017 lun_req->result_len = packed_len; 3018 free(packed, M_NVLIST); 3019 } 3020 3021 nvlist_destroy(lun_req->result_nvl); 3022 } 3023 break; 3024 } 3025 case CTL_LUN_LIST: { 3026 struct sbuf *sb; 3027 struct ctl_lun_list *list; 3028 const char *name, *value; 3029 void *cookie; 3030 int type; 3031 3032 list = (struct ctl_lun_list *)addr; 3033 3034 /* 3035 * Allocate a fixed length sbuf here, based on the length 3036 * of the user's buffer. We could allocate an auto-extending 3037 * buffer, and then tell the user how much larger our 3038 * amount of data is than his buffer, but that presents 3039 * some problems: 3040 * 3041 * 1. The sbuf(9) routines use a blocking malloc, and so 3042 * we can't hold a lock while calling them with an 3043 * auto-extending buffer. 3044 * 3045 * 2. There is not currently a LUN reference counting 3046 * mechanism, outside of outstanding transactions on 3047 * the LUN's OOA queue. So a LUN could go away on us 3048 * while we're getting the LUN number, backend-specific 3049 * information, etc. Thus, given the way things 3050 * currently work, we need to hold the CTL lock while 3051 * grabbing LUN information. 3052 * 3053 * So, from the user's standpoint, the best thing to do is 3054 * allocate what he thinks is a reasonable buffer length, 3055 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3056 * double the buffer length and try again. (And repeat 3057 * that until he succeeds.) 3058 */ 3059 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3060 if (sb == NULL) { 3061 list->status = CTL_LUN_LIST_ERROR; 3062 snprintf(list->error_str, sizeof(list->error_str), 3063 "Unable to allocate %d bytes for LUN list", 3064 list->alloc_len); 3065 break; 3066 } 3067 3068 sbuf_cat(sb, "<ctllunlist>\n"); 3069 3070 mtx_lock(&softc->ctl_lock); 3071 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3072 mtx_lock(&lun->lun_lock); 3073 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3074 (uintmax_t)lun->lun); 3075 3076 /* 3077 * Bail out as soon as we see that we've overfilled 3078 * the buffer. 3079 */ 3080 if (retval != 0) 3081 break; 3082 3083 retval = sbuf_printf(sb, "\t<backend_type>%s" 3084 "</backend_type>\n", 3085 (lun->backend == NULL) ? "none" : 3086 lun->backend->name); 3087 3088 if (retval != 0) 3089 break; 3090 3091 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3092 lun->be_lun->lun_type); 3093 3094 if (retval != 0) 3095 break; 3096 3097 if (lun->backend == NULL) { 3098 retval = sbuf_cat(sb, "</lun>\n"); 3099 if (retval != 0) 3100 break; 3101 continue; 3102 } 3103 3104 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3105 (lun->be_lun->maxlba > 0) ? 3106 lun->be_lun->maxlba + 1 : 0); 3107 3108 if (retval != 0) 3109 break; 3110 3111 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3112 lun->be_lun->blocksize); 3113 3114 if (retval != 0) 3115 break; 3116 3117 retval = sbuf_cat(sb, "\t<serial_number>"); 3118 3119 if (retval != 0) 3120 break; 3121 3122 retval = ctl_sbuf_printf_esc(sb, 3123 lun->be_lun->serial_num, 3124 sizeof(lun->be_lun->serial_num)); 3125 3126 if (retval != 0) 3127 break; 3128 3129 retval = sbuf_cat(sb, "</serial_number>\n"); 3130 3131 if (retval != 0) 3132 break; 3133 3134 retval = sbuf_cat(sb, "\t<device_id>"); 3135 3136 if (retval != 0) 3137 break; 3138 3139 retval = ctl_sbuf_printf_esc(sb, 3140 lun->be_lun->device_id, 3141 sizeof(lun->be_lun->device_id)); 3142 3143 if (retval != 0) 3144 break; 3145 3146 retval = sbuf_cat(sb, "</device_id>\n"); 3147 3148 if (retval != 0) 3149 break; 3150 3151 if (lun->backend->lun_info != NULL) { 3152 retval = lun->backend->lun_info(lun->be_lun, sb); 3153 if (retval != 0) 3154 break; 3155 } 3156 3157 cookie = NULL; 3158 while ((name = nvlist_next(lun->be_lun->options, &type, 3159 &cookie)) != NULL) { 3160 sbuf_printf(sb, "\t<%s>", name); 3161 3162 if (type == NV_TYPE_STRING) { 3163 value = dnvlist_get_string( 3164 lun->be_lun->options, name, NULL); 3165 if (value != NULL) 3166 sbuf_cat(sb, value); 3167 } 3168 3169 sbuf_printf(sb, "</%s>\n", name); 3170 } 3171 3172 retval = sbuf_cat(sb, "</lun>\n"); 3173 3174 if (retval != 0) 3175 break; 3176 mtx_unlock(&lun->lun_lock); 3177 } 3178 if (lun != NULL) 3179 mtx_unlock(&lun->lun_lock); 3180 mtx_unlock(&softc->ctl_lock); 3181 3182 if ((retval != 0) 3183 || ((retval = sbuf_cat(sb, "</ctllunlist>\n")) != 0)) { 3184 retval = 0; 3185 sbuf_delete(sb); 3186 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3187 snprintf(list->error_str, sizeof(list->error_str), 3188 "Out of space, %d bytes is too small", 3189 list->alloc_len); 3190 break; 3191 } 3192 3193 sbuf_finish(sb); 3194 3195 retval = copyout(sbuf_data(sb), list->lun_xml, 3196 sbuf_len(sb) + 1); 3197 3198 list->fill_len = sbuf_len(sb) + 1; 3199 list->status = CTL_LUN_LIST_OK; 3200 sbuf_delete(sb); 3201 break; 3202 } 3203 case CTL_ISCSI: { 3204 struct ctl_iscsi *ci; 3205 struct ctl_frontend *fe; 3206 3207 ci = (struct ctl_iscsi *)addr; 3208 3209 fe = ctl_frontend_find("iscsi"); 3210 if (fe == NULL) { 3211 ci->status = CTL_ISCSI_ERROR; 3212 snprintf(ci->error_str, sizeof(ci->error_str), 3213 "Frontend \"iscsi\" not found."); 3214 break; 3215 } 3216 3217 retval = fe->ioctl(dev, cmd, addr, flag, td); 3218 break; 3219 } 3220 case CTL_NVMF: { 3221 struct ctl_nvmf *cn; 3222 struct ctl_frontend *fe; 3223 3224 cn = (struct ctl_nvmf *)addr; 3225 3226 fe = ctl_frontend_find("nvmf"); 3227 if (fe == NULL) { 3228 cn->status = CTL_NVMF_ERROR; 3229 snprintf(cn->error_str, sizeof(cn->error_str), 3230 "Frontend \"nvmf\" not found."); 3231 break; 3232 } 3233 3234 retval = fe->ioctl(dev, cmd, addr, flag, td); 3235 break; 3236 } 3237 case CTL_PORT_REQ: { 3238 struct ctl_req *req; 3239 struct ctl_frontend *fe; 3240 void *packed; 3241 nvlist_t *tmp_args_nvl; 3242 size_t packed_len; 3243 3244 req = (struct ctl_req *)addr; 3245 tmp_args_nvl = req->args_nvl; 3246 3247 fe = ctl_frontend_find(req->driver); 3248 if (fe == NULL) { 3249 req->status = CTL_LUN_ERROR; 3250 snprintf(req->error_str, sizeof(req->error_str), 3251 "Frontend \"%s\" not found.", req->driver); 3252 break; 3253 } 3254 3255 if (req->args != NULL) { 3256 if (req->args_len > CTL_MAX_ARGS_LEN) { 3257 req->status = CTL_LUN_ERROR; 3258 snprintf(req->error_str, sizeof(req->error_str), 3259 "Too big args."); 3260 break; 3261 } 3262 packed = malloc(req->args_len, M_CTL, M_WAITOK); 3263 if (copyin(req->args, packed, req->args_len) != 0) { 3264 free(packed, M_CTL); 3265 req->status = CTL_LUN_ERROR; 3266 snprintf(req->error_str, sizeof(req->error_str), 3267 "Cannot copyin args."); 3268 break; 3269 } 3270 req->args_nvl = nvlist_unpack(packed, 3271 req->args_len, 0); 3272 free(packed, M_CTL); 3273 3274 if (req->args_nvl == NULL) { 3275 req->status = CTL_LUN_ERROR; 3276 snprintf(req->error_str, sizeof(req->error_str), 3277 "Cannot unpack args nvlist."); 3278 break; 3279 } 3280 } else 3281 req->args_nvl = nvlist_create(0); 3282 3283 req->result_nvl = NULL; 3284 if (fe->ioctl) 3285 retval = fe->ioctl(dev, cmd, addr, flag, td); 3286 else 3287 retval = ENODEV; 3288 3289 nvlist_destroy(req->args_nvl); 3290 req->args_nvl = tmp_args_nvl; 3291 3292 if (req->result_nvl != NULL) { 3293 if (req->result != NULL) { 3294 packed = nvlist_pack(req->result_nvl, 3295 &packed_len); 3296 if (packed == NULL) { 3297 req->status = CTL_LUN_ERROR; 3298 snprintf(req->error_str, 3299 sizeof(req->error_str), 3300 "Cannot pack result nvlist."); 3301 break; 3302 } 3303 3304 if (packed_len > req->result_len) { 3305 req->status = CTL_LUN_ERROR; 3306 snprintf(req->error_str, 3307 sizeof(req->error_str), 3308 "Result nvlist too large."); 3309 free(packed, M_NVLIST); 3310 break; 3311 } 3312 3313 if (copyout(packed, req->result, packed_len)) { 3314 req->status = CTL_LUN_ERROR; 3315 snprintf(req->error_str, 3316 sizeof(req->error_str), 3317 "Cannot copyout() the result."); 3318 free(packed, M_NVLIST); 3319 break; 3320 } 3321 3322 req->result_len = packed_len; 3323 free(packed, M_NVLIST); 3324 } 3325 3326 nvlist_destroy(req->result_nvl); 3327 } 3328 break; 3329 } 3330 case CTL_PORT_LIST: { 3331 struct sbuf *sb; 3332 struct ctl_port *port; 3333 struct ctl_lun_list *list; 3334 const char *name, *value; 3335 void *cookie; 3336 int j, type; 3337 uint32_t plun; 3338 3339 list = (struct ctl_lun_list *)addr; 3340 3341 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3342 if (sb == NULL) { 3343 list->status = CTL_LUN_LIST_ERROR; 3344 snprintf(list->error_str, sizeof(list->error_str), 3345 "Unable to allocate %d bytes for LUN list", 3346 list->alloc_len); 3347 break; 3348 } 3349 3350 sbuf_cat(sb, "<ctlportlist>\n"); 3351 3352 mtx_lock(&softc->ctl_lock); 3353 STAILQ_FOREACH(port, &softc->port_list, links) { 3354 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3355 (uintmax_t)port->targ_port); 3356 3357 /* 3358 * Bail out as soon as we see that we've overfilled 3359 * the buffer. 3360 */ 3361 if (retval != 0) 3362 break; 3363 3364 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3365 "</frontend_type>\n", port->frontend->name); 3366 if (retval != 0) 3367 break; 3368 3369 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3370 port->port_type); 3371 if (retval != 0) 3372 break; 3373 3374 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3375 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3376 if (retval != 0) 3377 break; 3378 3379 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3380 port->port_name); 3381 if (retval != 0) 3382 break; 3383 3384 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3385 port->physical_port); 3386 if (retval != 0) 3387 break; 3388 3389 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3390 port->virtual_port); 3391 if (retval != 0) 3392 break; 3393 3394 if (port->target_devid != NULL) { 3395 sbuf_cat(sb, "\t<target>"); 3396 ctl_id_sbuf(port->target_devid, sb); 3397 sbuf_cat(sb, "</target>\n"); 3398 } 3399 3400 if (port->port_devid != NULL) { 3401 sbuf_cat(sb, "\t<port>"); 3402 ctl_id_sbuf(port->port_devid, sb); 3403 sbuf_cat(sb, "</port>\n"); 3404 } 3405 3406 if (port->port_info != NULL) { 3407 retval = port->port_info(port->onoff_arg, sb); 3408 if (retval != 0) 3409 break; 3410 } 3411 3412 cookie = NULL; 3413 while ((name = nvlist_next(port->options, &type, 3414 &cookie)) != NULL) { 3415 sbuf_printf(sb, "\t<%s>", name); 3416 3417 if (type == NV_TYPE_STRING) { 3418 value = dnvlist_get_string(port->options, 3419 name, NULL); 3420 if (value != NULL) 3421 sbuf_printf(sb, "%s", value); 3422 } 3423 3424 sbuf_printf(sb, "</%s>\n", name); 3425 } 3426 3427 if (port->lun_map != NULL) { 3428 sbuf_cat(sb, "\t<lun_map>on</lun_map>\n"); 3429 for (j = 0; j < port->lun_map_size; j++) { 3430 plun = ctl_lun_map_from_port(port, j); 3431 if (plun == UINT32_MAX) 3432 continue; 3433 sbuf_printf(sb, 3434 "\t<lun id=\"%u\">%u</lun>\n", 3435 j, plun); 3436 } 3437 } 3438 3439 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3440 if (port->wwpn_iid[j].in_use == 0 || 3441 (port->wwpn_iid[j].wwpn == 0 && 3442 port->wwpn_iid[j].name == NULL)) 3443 continue; 3444 3445 if (port->wwpn_iid[j].name != NULL) 3446 retval = sbuf_printf(sb, 3447 "\t<initiator id=\"%u\">%s</initiator>\n", 3448 j, port->wwpn_iid[j].name); 3449 else 3450 retval = sbuf_printf(sb, 3451 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3452 j, port->wwpn_iid[j].wwpn); 3453 if (retval != 0) 3454 break; 3455 } 3456 if (retval != 0) 3457 break; 3458 3459 retval = sbuf_cat(sb, "</targ_port>\n"); 3460 if (retval != 0) 3461 break; 3462 } 3463 mtx_unlock(&softc->ctl_lock); 3464 3465 if ((retval != 0) 3466 || ((retval = sbuf_cat(sb, "</ctlportlist>\n")) != 0)) { 3467 retval = 0; 3468 sbuf_delete(sb); 3469 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3470 snprintf(list->error_str, sizeof(list->error_str), 3471 "Out of space, %d bytes is too small", 3472 list->alloc_len); 3473 break; 3474 } 3475 3476 sbuf_finish(sb); 3477 3478 retval = copyout(sbuf_data(sb), list->lun_xml, 3479 sbuf_len(sb) + 1); 3480 3481 list->fill_len = sbuf_len(sb) + 1; 3482 list->status = CTL_LUN_LIST_OK; 3483 sbuf_delete(sb); 3484 break; 3485 } 3486 case CTL_LUN_MAP: { 3487 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3488 struct ctl_port *port; 3489 3490 mtx_lock(&softc->ctl_lock); 3491 if (lm->port < softc->port_min || 3492 lm->port >= softc->port_max || 3493 (port = softc->ctl_ports[lm->port]) == NULL) { 3494 mtx_unlock(&softc->ctl_lock); 3495 return (ENXIO); 3496 } 3497 if (port->status & CTL_PORT_STATUS_ONLINE) { 3498 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3499 if (ctl_lun_map_to_port(port, lun->lun) == 3500 UINT32_MAX) 3501 continue; 3502 mtx_lock(&lun->lun_lock); 3503 ctl_est_ua_port(lun, lm->port, -1, 3504 CTL_UA_LUN_CHANGE); 3505 mtx_unlock(&lun->lun_lock); 3506 } 3507 } 3508 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3509 if (lm->plun != UINT32_MAX) { 3510 if (lm->lun == UINT32_MAX) 3511 retval = ctl_lun_map_unset(port, lm->plun); 3512 else if (lm->lun < ctl_max_luns && 3513 softc->ctl_luns[lm->lun] != NULL) 3514 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3515 else 3516 return (ENXIO); 3517 } else { 3518 if (lm->lun == UINT32_MAX) 3519 retval = ctl_lun_map_deinit(port); 3520 else 3521 retval = ctl_lun_map_init(port); 3522 } 3523 if (port->status & CTL_PORT_STATUS_ONLINE) 3524 ctl_isc_announce_port(port); 3525 break; 3526 } 3527 case CTL_GET_LUN_STATS: { 3528 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3529 int i; 3530 3531 /* 3532 * XXX KDM no locking here. If the LUN list changes, 3533 * things can blow up. 3534 */ 3535 i = 0; 3536 stats->status = CTL_SS_OK; 3537 stats->fill_len = 0; 3538 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3539 if (lun->lun < stats->first_item) 3540 continue; 3541 if (stats->fill_len + sizeof(lun->stats) > 3542 stats->alloc_len) { 3543 stats->status = CTL_SS_NEED_MORE_SPACE; 3544 break; 3545 } 3546 retval = copyout(&lun->stats, &stats->stats[i++], 3547 sizeof(lun->stats)); 3548 if (retval != 0) 3549 break; 3550 stats->fill_len += sizeof(lun->stats); 3551 } 3552 stats->num_items = softc->num_luns; 3553 stats->flags = CTL_STATS_FLAG_NONE; 3554 #ifdef CTL_TIME_IO 3555 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3556 #endif 3557 getnanouptime(&stats->timestamp); 3558 break; 3559 } 3560 case CTL_GET_PORT_STATS: { 3561 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3562 int i; 3563 3564 /* 3565 * XXX KDM no locking here. If the LUN list changes, 3566 * things can blow up. 3567 */ 3568 i = 0; 3569 stats->status = CTL_SS_OK; 3570 stats->fill_len = 0; 3571 STAILQ_FOREACH(port, &softc->port_list, links) { 3572 if (port->targ_port < stats->first_item) 3573 continue; 3574 if (stats->fill_len + sizeof(port->stats) > 3575 stats->alloc_len) { 3576 stats->status = CTL_SS_NEED_MORE_SPACE; 3577 break; 3578 } 3579 retval = copyout(&port->stats, &stats->stats[i++], 3580 sizeof(port->stats)); 3581 if (retval != 0) 3582 break; 3583 stats->fill_len += sizeof(port->stats); 3584 } 3585 stats->num_items = softc->num_ports; 3586 stats->flags = CTL_STATS_FLAG_NONE; 3587 #ifdef CTL_TIME_IO 3588 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3589 #endif 3590 getnanouptime(&stats->timestamp); 3591 break; 3592 } 3593 default: { 3594 /* XXX KDM should we fix this? */ 3595 #if 0 3596 struct ctl_backend_driver *backend; 3597 unsigned int type; 3598 int found; 3599 3600 found = 0; 3601 3602 /* 3603 * We encode the backend type as the ioctl type for backend 3604 * ioctls. So parse it out here, and then search for a 3605 * backend of this type. 3606 */ 3607 type = _IOC_TYPE(cmd); 3608 3609 STAILQ_FOREACH(backend, &softc->be_list, links) { 3610 if (backend->type == type) { 3611 found = 1; 3612 break; 3613 } 3614 } 3615 if (found == 0) { 3616 printf("ctl: unknown ioctl command %#lx or backend " 3617 "%d\n", cmd, type); 3618 retval = EINVAL; 3619 break; 3620 } 3621 retval = backend->ioctl(dev, cmd, addr, flag, td); 3622 #endif 3623 retval = ENOTTY; 3624 break; 3625 } 3626 } 3627 return (retval); 3628 } 3629 3630 uint32_t 3631 ctl_get_initindex(struct ctl_nexus *nexus) 3632 { 3633 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3634 } 3635 3636 int 3637 ctl_lun_map_init(struct ctl_port *port) 3638 { 3639 struct ctl_softc *softc = port->ctl_softc; 3640 struct ctl_lun *lun; 3641 int size = ctl_lun_map_size; 3642 uint32_t i; 3643 3644 if (port->lun_map == NULL || port->lun_map_size < size) { 3645 port->lun_map_size = 0; 3646 free(port->lun_map, M_CTL); 3647 port->lun_map = malloc(size * sizeof(uint32_t), 3648 M_CTL, M_NOWAIT); 3649 } 3650 if (port->lun_map == NULL) 3651 return (ENOMEM); 3652 for (i = 0; i < size; i++) 3653 port->lun_map[i] = UINT32_MAX; 3654 port->lun_map_size = size; 3655 if (port->status & CTL_PORT_STATUS_ONLINE) { 3656 if (port->lun_disable != NULL) { 3657 STAILQ_FOREACH(lun, &softc->lun_list, links) 3658 port->lun_disable(port->targ_lun_arg, lun->lun); 3659 } 3660 ctl_isc_announce_port(port); 3661 } 3662 return (0); 3663 } 3664 3665 int 3666 ctl_lun_map_deinit(struct ctl_port *port) 3667 { 3668 struct ctl_softc *softc = port->ctl_softc; 3669 struct ctl_lun *lun; 3670 3671 if (port->lun_map == NULL) 3672 return (0); 3673 port->lun_map_size = 0; 3674 free(port->lun_map, M_CTL); 3675 port->lun_map = NULL; 3676 if (port->status & CTL_PORT_STATUS_ONLINE) { 3677 if (port->lun_enable != NULL) { 3678 STAILQ_FOREACH(lun, &softc->lun_list, links) 3679 port->lun_enable(port->targ_lun_arg, lun->lun); 3680 } 3681 ctl_isc_announce_port(port); 3682 } 3683 return (0); 3684 } 3685 3686 int 3687 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3688 { 3689 int status; 3690 uint32_t old; 3691 3692 if (port->lun_map == NULL) { 3693 status = ctl_lun_map_init(port); 3694 if (status != 0) 3695 return (status); 3696 } 3697 if (plun >= port->lun_map_size) 3698 return (EINVAL); 3699 old = port->lun_map[plun]; 3700 port->lun_map[plun] = glun; 3701 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3702 if (port->lun_enable != NULL) 3703 port->lun_enable(port->targ_lun_arg, plun); 3704 ctl_isc_announce_port(port); 3705 } 3706 return (0); 3707 } 3708 3709 int 3710 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3711 { 3712 uint32_t old; 3713 3714 if (port->lun_map == NULL || plun >= port->lun_map_size) 3715 return (0); 3716 old = port->lun_map[plun]; 3717 port->lun_map[plun] = UINT32_MAX; 3718 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3719 if (port->lun_disable != NULL) 3720 port->lun_disable(port->targ_lun_arg, plun); 3721 ctl_isc_announce_port(port); 3722 } 3723 return (0); 3724 } 3725 3726 uint32_t 3727 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3728 { 3729 3730 if (port == NULL) 3731 return (UINT32_MAX); 3732 if (port->lun_map == NULL) 3733 return (lun_id); 3734 if (lun_id > port->lun_map_size) 3735 return (UINT32_MAX); 3736 return (port->lun_map[lun_id]); 3737 } 3738 3739 uint32_t 3740 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3741 { 3742 uint32_t i; 3743 3744 if (port == NULL) 3745 return (UINT32_MAX); 3746 if (port->lun_map == NULL) 3747 return (lun_id); 3748 for (i = 0; i < port->lun_map_size; i++) { 3749 if (port->lun_map[i] == lun_id) 3750 return (i); 3751 } 3752 return (UINT32_MAX); 3753 } 3754 3755 uint32_t 3756 ctl_decode_lun(uint64_t encoded) 3757 { 3758 uint8_t lun[8]; 3759 uint32_t result = 0xffffffff; 3760 3761 be64enc(lun, encoded); 3762 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3763 case RPL_LUNDATA_ATYP_PERIPH: 3764 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3765 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3766 result = lun[1]; 3767 break; 3768 case RPL_LUNDATA_ATYP_FLAT: 3769 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3770 lun[6] == 0 && lun[7] == 0) 3771 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3772 break; 3773 case RPL_LUNDATA_ATYP_EXTLUN: 3774 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3775 case 0x02: 3776 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3777 case 0x00: 3778 result = lun[1]; 3779 break; 3780 case 0x10: 3781 result = (lun[1] << 16) + (lun[2] << 8) + 3782 lun[3]; 3783 break; 3784 case 0x20: 3785 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3786 result = (lun[2] << 24) + 3787 (lun[3] << 16) + (lun[4] << 8) + 3788 lun[5]; 3789 break; 3790 } 3791 break; 3792 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3793 result = 0xffffffff; 3794 break; 3795 } 3796 break; 3797 } 3798 return (result); 3799 } 3800 3801 uint64_t 3802 ctl_encode_lun(uint32_t decoded) 3803 { 3804 uint64_t l = decoded; 3805 3806 if (l <= 0xff) 3807 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3808 if (l <= 0x3fff) 3809 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3810 if (l <= 0xffffff) 3811 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3812 (l << 32)); 3813 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3814 } 3815 3816 int 3817 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3818 { 3819 int i; 3820 3821 for (i = first; i < last; i++) { 3822 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3823 return (i); 3824 } 3825 return (-1); 3826 } 3827 3828 int 3829 ctl_set_mask(uint32_t *mask, uint32_t bit) 3830 { 3831 uint32_t chunk, piece; 3832 3833 chunk = bit >> 5; 3834 piece = bit % (sizeof(uint32_t) * 8); 3835 3836 if ((mask[chunk] & (1 << piece)) != 0) 3837 return (-1); 3838 else 3839 mask[chunk] |= (1 << piece); 3840 3841 return (0); 3842 } 3843 3844 int 3845 ctl_clear_mask(uint32_t *mask, uint32_t bit) 3846 { 3847 uint32_t chunk, piece; 3848 3849 chunk = bit >> 5; 3850 piece = bit % (sizeof(uint32_t) * 8); 3851 3852 if ((mask[chunk] & (1 << piece)) == 0) 3853 return (-1); 3854 else 3855 mask[chunk] &= ~(1 << piece); 3856 3857 return (0); 3858 } 3859 3860 int 3861 ctl_is_set(uint32_t *mask, uint32_t bit) 3862 { 3863 uint32_t chunk, piece; 3864 3865 chunk = bit >> 5; 3866 piece = bit % (sizeof(uint32_t) * 8); 3867 3868 if ((mask[chunk] & (1 << piece)) == 0) 3869 return (0); 3870 else 3871 return (1); 3872 } 3873 3874 static uint64_t 3875 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3876 { 3877 uint64_t *t; 3878 3879 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3880 if (t == NULL) 3881 return (0); 3882 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3883 } 3884 3885 static void 3886 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3887 { 3888 uint64_t *t; 3889 3890 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3891 if (t == NULL) 3892 return; 3893 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3894 } 3895 3896 static void 3897 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3898 { 3899 uint64_t *p; 3900 u_int i; 3901 3902 i = residx/CTL_MAX_INIT_PER_PORT; 3903 if (lun->pr_keys[i] != NULL) 3904 return; 3905 mtx_unlock(&lun->lun_lock); 3906 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3907 M_WAITOK | M_ZERO); 3908 mtx_lock(&lun->lun_lock); 3909 if (lun->pr_keys[i] == NULL) 3910 lun->pr_keys[i] = p; 3911 else 3912 free(p, M_CTL); 3913 } 3914 3915 static void 3916 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3917 { 3918 uint64_t *t; 3919 3920 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3921 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3922 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3923 } 3924 3925 /* 3926 * ctl_softc, pool_name, total_ctl_io are passed in. 3927 * npool is passed out. 3928 */ 3929 int 3930 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3931 uint32_t total_ctl_io, void **npool) 3932 { 3933 struct ctl_io_pool *pool; 3934 3935 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3936 M_NOWAIT | M_ZERO); 3937 if (pool == NULL) 3938 return (ENOMEM); 3939 3940 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3941 pool->ctl_softc = ctl_softc; 3942 #ifdef IO_POOLS 3943 pool->zone = uma_zsecond_create(pool->name, NULL, 3944 NULL, NULL, NULL, ctl_softc->io_zone); 3945 /* uma_prealloc(pool->zone, total_ctl_io); */ 3946 #else 3947 pool->zone = ctl_softc->io_zone; 3948 #endif 3949 3950 *npool = pool; 3951 return (0); 3952 } 3953 3954 void 3955 ctl_pool_free(struct ctl_io_pool *pool) 3956 { 3957 3958 if (pool == NULL) 3959 return; 3960 3961 #ifdef IO_POOLS 3962 uma_zdestroy(pool->zone); 3963 #endif 3964 free(pool, M_CTL); 3965 } 3966 3967 union ctl_io * 3968 ctl_alloc_io(void *pool_ref) 3969 { 3970 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3971 union ctl_io *io; 3972 3973 io = uma_zalloc(pool->zone, M_WAITOK); 3974 if (io != NULL) { 3975 io->io_hdr.pool = pool_ref; 3976 CTL_SOFTC(io) = pool->ctl_softc; 3977 TAILQ_INIT(&io->io_hdr.blocked_queue); 3978 } 3979 return (io); 3980 } 3981 3982 union ctl_io * 3983 ctl_alloc_io_nowait(void *pool_ref) 3984 { 3985 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3986 union ctl_io *io; 3987 3988 io = uma_zalloc(pool->zone, M_NOWAIT); 3989 if (io != NULL) { 3990 io->io_hdr.pool = pool_ref; 3991 CTL_SOFTC(io) = pool->ctl_softc; 3992 TAILQ_INIT(&io->io_hdr.blocked_queue); 3993 } 3994 return (io); 3995 } 3996 3997 void 3998 ctl_free_io(union ctl_io *io) 3999 { 4000 struct ctl_io_pool *pool; 4001 4002 if (io == NULL) 4003 return; 4004 4005 pool = (struct ctl_io_pool *)io->io_hdr.pool; 4006 uma_zfree(pool->zone, io); 4007 } 4008 4009 void 4010 ctl_zero_io(union ctl_io *io) 4011 { 4012 struct ctl_io_pool *pool; 4013 4014 if (io == NULL) 4015 return; 4016 4017 /* 4018 * May need to preserve linked list pointers at some point too. 4019 */ 4020 pool = io->io_hdr.pool; 4021 memset(io, 0, sizeof(*io)); 4022 io->io_hdr.pool = pool; 4023 CTL_SOFTC(io) = pool->ctl_softc; 4024 TAILQ_INIT(&io->io_hdr.blocked_queue); 4025 } 4026 4027 int 4028 ctl_expand_number(const char *buf, uint64_t *num) 4029 { 4030 char *endptr; 4031 uint64_t number; 4032 unsigned shift; 4033 4034 number = strtoq(buf, &endptr, 0); 4035 4036 switch (tolower((unsigned char)*endptr)) { 4037 case 'e': 4038 shift = 60; 4039 break; 4040 case 'p': 4041 shift = 50; 4042 break; 4043 case 't': 4044 shift = 40; 4045 break; 4046 case 'g': 4047 shift = 30; 4048 break; 4049 case 'm': 4050 shift = 20; 4051 break; 4052 case 'k': 4053 shift = 10; 4054 break; 4055 case 'b': 4056 case '\0': /* No unit. */ 4057 *num = number; 4058 return (0); 4059 default: 4060 /* Unrecognized unit. */ 4061 return (-1); 4062 } 4063 4064 if ((number << shift) >> shift != number) { 4065 /* Overflow */ 4066 return (-1); 4067 } 4068 *num = number << shift; 4069 return (0); 4070 } 4071 4072 /* 4073 * This routine could be used in the future to load default and/or saved 4074 * mode page parameters for a particuar lun. 4075 */ 4076 static int 4077 ctl_init_page_index(struct ctl_lun *lun) 4078 { 4079 int i, page_code; 4080 struct ctl_page_index *page_index; 4081 const char *value; 4082 uint64_t ival; 4083 4084 memcpy(&lun->mode_pages.index, page_index_template, 4085 sizeof(page_index_template)); 4086 4087 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4088 page_index = &lun->mode_pages.index[i]; 4089 if (lun->be_lun->lun_type == T_DIRECT && 4090 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4091 continue; 4092 if (lun->be_lun->lun_type == T_PROCESSOR && 4093 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4094 continue; 4095 if (lun->be_lun->lun_type == T_CDROM && 4096 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4097 continue; 4098 4099 page_code = page_index->page_code & SMPH_PC_MASK; 4100 switch (page_code) { 4101 case SMS_RW_ERROR_RECOVERY_PAGE: { 4102 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4103 ("subpage %#x for page %#x is incorrect!", 4104 page_index->subpage, page_code)); 4105 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4106 &rw_er_page_default, 4107 sizeof(rw_er_page_default)); 4108 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4109 &rw_er_page_changeable, 4110 sizeof(rw_er_page_changeable)); 4111 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4112 &rw_er_page_default, 4113 sizeof(rw_er_page_default)); 4114 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4115 &rw_er_page_default, 4116 sizeof(rw_er_page_default)); 4117 page_index->page_data = 4118 (uint8_t *)lun->mode_pages.rw_er_page; 4119 break; 4120 } 4121 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4122 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4123 ("subpage %#x for page %#x is incorrect!", 4124 page_index->subpage, page_code)); 4125 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4126 &verify_er_page_default, 4127 sizeof(verify_er_page_default)); 4128 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4129 &verify_er_page_changeable, 4130 sizeof(verify_er_page_changeable)); 4131 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4132 &verify_er_page_default, 4133 sizeof(verify_er_page_default)); 4134 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4135 &verify_er_page_default, 4136 sizeof(verify_er_page_default)); 4137 page_index->page_data = 4138 (uint8_t *)lun->mode_pages.verify_er_page; 4139 break; 4140 } 4141 case SMS_CACHING_PAGE: { 4142 struct scsi_caching_page *caching_page; 4143 4144 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4145 ("subpage %#x for page %#x is incorrect!", 4146 page_index->subpage, page_code)); 4147 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4148 &caching_page_default, 4149 sizeof(caching_page_default)); 4150 memcpy(&lun->mode_pages.caching_page[ 4151 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4152 sizeof(caching_page_changeable)); 4153 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4154 &caching_page_default, 4155 sizeof(caching_page_default)); 4156 caching_page = &lun->mode_pages.caching_page[ 4157 CTL_PAGE_SAVED]; 4158 value = dnvlist_get_string(lun->be_lun->options, 4159 "writecache", NULL); 4160 if (value != NULL && strcmp(value, "off") == 0) 4161 caching_page->flags1 &= ~SCP_WCE; 4162 value = dnvlist_get_string(lun->be_lun->options, 4163 "readcache", NULL); 4164 if (value != NULL && strcmp(value, "off") == 0) 4165 caching_page->flags1 |= SCP_RCD; 4166 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4167 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4168 sizeof(caching_page_default)); 4169 page_index->page_data = 4170 (uint8_t *)lun->mode_pages.caching_page; 4171 break; 4172 } 4173 case SMS_CONTROL_MODE_PAGE: { 4174 switch (page_index->subpage) { 4175 case SMS_SUBPAGE_PAGE_0: { 4176 struct scsi_control_page *control_page; 4177 4178 memcpy(&lun->mode_pages.control_page[ 4179 CTL_PAGE_DEFAULT], 4180 &control_page_default, 4181 sizeof(control_page_default)); 4182 memcpy(&lun->mode_pages.control_page[ 4183 CTL_PAGE_CHANGEABLE], 4184 &control_page_changeable, 4185 sizeof(control_page_changeable)); 4186 memcpy(&lun->mode_pages.control_page[ 4187 CTL_PAGE_SAVED], 4188 &control_page_default, 4189 sizeof(control_page_default)); 4190 control_page = &lun->mode_pages.control_page[ 4191 CTL_PAGE_SAVED]; 4192 value = dnvlist_get_string(lun->be_lun->options, 4193 "reordering", NULL); 4194 if (value != NULL && 4195 strcmp(value, "unrestricted") == 0) { 4196 control_page->queue_flags &= 4197 ~SCP_QUEUE_ALG_MASK; 4198 control_page->queue_flags |= 4199 SCP_QUEUE_ALG_UNRESTRICTED; 4200 } 4201 memcpy(&lun->mode_pages.control_page[ 4202 CTL_PAGE_CURRENT], 4203 &lun->mode_pages.control_page[ 4204 CTL_PAGE_SAVED], 4205 sizeof(control_page_default)); 4206 page_index->page_data = 4207 (uint8_t *)lun->mode_pages.control_page; 4208 break; 4209 } 4210 case 0x01: 4211 memcpy(&lun->mode_pages.control_ext_page[ 4212 CTL_PAGE_DEFAULT], 4213 &control_ext_page_default, 4214 sizeof(control_ext_page_default)); 4215 memcpy(&lun->mode_pages.control_ext_page[ 4216 CTL_PAGE_CHANGEABLE], 4217 &control_ext_page_changeable, 4218 sizeof(control_ext_page_changeable)); 4219 memcpy(&lun->mode_pages.control_ext_page[ 4220 CTL_PAGE_SAVED], 4221 &control_ext_page_default, 4222 sizeof(control_ext_page_default)); 4223 memcpy(&lun->mode_pages.control_ext_page[ 4224 CTL_PAGE_CURRENT], 4225 &lun->mode_pages.control_ext_page[ 4226 CTL_PAGE_SAVED], 4227 sizeof(control_ext_page_default)); 4228 page_index->page_data = 4229 (uint8_t *)lun->mode_pages.control_ext_page; 4230 break; 4231 default: 4232 panic("subpage %#x for page %#x is incorrect!", 4233 page_index->subpage, page_code); 4234 } 4235 break; 4236 } 4237 case SMS_INFO_EXCEPTIONS_PAGE: { 4238 switch (page_index->subpage) { 4239 case SMS_SUBPAGE_PAGE_0: 4240 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4241 &ie_page_default, 4242 sizeof(ie_page_default)); 4243 memcpy(&lun->mode_pages.ie_page[ 4244 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4245 sizeof(ie_page_changeable)); 4246 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4247 &ie_page_default, 4248 sizeof(ie_page_default)); 4249 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4250 &ie_page_default, 4251 sizeof(ie_page_default)); 4252 page_index->page_data = 4253 (uint8_t *)lun->mode_pages.ie_page; 4254 break; 4255 case 0x02: { 4256 struct ctl_logical_block_provisioning_page *page; 4257 4258 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4259 &lbp_page_default, 4260 sizeof(lbp_page_default)); 4261 memcpy(&lun->mode_pages.lbp_page[ 4262 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4263 sizeof(lbp_page_changeable)); 4264 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4265 &lbp_page_default, 4266 sizeof(lbp_page_default)); 4267 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4268 value = dnvlist_get_string(lun->be_lun->options, 4269 "avail-threshold", NULL); 4270 if (value != NULL && 4271 ctl_expand_number(value, &ival) == 0) { 4272 page->descr[0].flags |= SLBPPD_ENABLED | 4273 SLBPPD_ARMING_DEC; 4274 if (lun->be_lun->blocksize) 4275 ival /= lun->be_lun->blocksize; 4276 else 4277 ival /= 512; 4278 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4279 page->descr[0].count); 4280 } 4281 value = dnvlist_get_string(lun->be_lun->options, 4282 "used-threshold", NULL); 4283 if (value != NULL && 4284 ctl_expand_number(value, &ival) == 0) { 4285 page->descr[1].flags |= SLBPPD_ENABLED | 4286 SLBPPD_ARMING_INC; 4287 if (lun->be_lun->blocksize) 4288 ival /= lun->be_lun->blocksize; 4289 else 4290 ival /= 512; 4291 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4292 page->descr[1].count); 4293 } 4294 value = dnvlist_get_string(lun->be_lun->options, 4295 "pool-avail-threshold", NULL); 4296 if (value != NULL && 4297 ctl_expand_number(value, &ival) == 0) { 4298 page->descr[2].flags |= SLBPPD_ENABLED | 4299 SLBPPD_ARMING_DEC; 4300 if (lun->be_lun->blocksize) 4301 ival /= lun->be_lun->blocksize; 4302 else 4303 ival /= 512; 4304 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4305 page->descr[2].count); 4306 } 4307 value = dnvlist_get_string(lun->be_lun->options, 4308 "pool-used-threshold", NULL); 4309 if (value != NULL && 4310 ctl_expand_number(value, &ival) == 0) { 4311 page->descr[3].flags |= SLBPPD_ENABLED | 4312 SLBPPD_ARMING_INC; 4313 if (lun->be_lun->blocksize) 4314 ival /= lun->be_lun->blocksize; 4315 else 4316 ival /= 512; 4317 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4318 page->descr[3].count); 4319 } 4320 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4321 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4322 sizeof(lbp_page_default)); 4323 page_index->page_data = 4324 (uint8_t *)lun->mode_pages.lbp_page; 4325 break; 4326 } 4327 default: 4328 panic("subpage %#x for page %#x is incorrect!", 4329 page_index->subpage, page_code); 4330 } 4331 break; 4332 } 4333 case SMS_CDDVD_CAPS_PAGE:{ 4334 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4335 ("subpage %#x for page %#x is incorrect!", 4336 page_index->subpage, page_code)); 4337 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4338 &cddvd_page_default, 4339 sizeof(cddvd_page_default)); 4340 memcpy(&lun->mode_pages.cddvd_page[ 4341 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4342 sizeof(cddvd_page_changeable)); 4343 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4344 &cddvd_page_default, 4345 sizeof(cddvd_page_default)); 4346 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4347 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4348 sizeof(cddvd_page_default)); 4349 page_index->page_data = 4350 (uint8_t *)lun->mode_pages.cddvd_page; 4351 break; 4352 } 4353 default: 4354 panic("invalid page code value %#x", page_code); 4355 } 4356 } 4357 4358 return (CTL_RETVAL_COMPLETE); 4359 } 4360 4361 static int 4362 ctl_init_log_page_index(struct ctl_lun *lun) 4363 { 4364 struct ctl_page_index *page_index; 4365 int i, j, k, prev; 4366 4367 memcpy(&lun->log_pages.index, log_page_index_template, 4368 sizeof(log_page_index_template)); 4369 4370 prev = -1; 4371 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4372 page_index = &lun->log_pages.index[i]; 4373 if (lun->be_lun->lun_type == T_DIRECT && 4374 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4375 continue; 4376 if (lun->be_lun->lun_type == T_PROCESSOR && 4377 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4378 continue; 4379 if (lun->be_lun->lun_type == T_CDROM && 4380 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4381 continue; 4382 4383 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4384 lun->backend->lun_attr == NULL) 4385 continue; 4386 4387 if (page_index->page_code != prev) { 4388 lun->log_pages.pages_page[j] = page_index->page_code; 4389 prev = page_index->page_code; 4390 j++; 4391 } 4392 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4393 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4394 k++; 4395 } 4396 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4397 lun->log_pages.index[0].page_len = j; 4398 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4399 lun->log_pages.index[1].page_len = k * 2; 4400 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page; 4401 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page); 4402 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0]; 4403 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS; 4404 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page; 4405 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page); 4406 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page; 4407 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page); 4408 4409 return (CTL_RETVAL_COMPLETE); 4410 } 4411 4412 static int 4413 hex2bin(const char *str, uint8_t *buf, int buf_size) 4414 { 4415 int i; 4416 u_char c; 4417 4418 memset(buf, 0, buf_size); 4419 while (isspace(str[0])) 4420 str++; 4421 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4422 str += 2; 4423 buf_size *= 2; 4424 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4425 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4426 str++; 4427 c = str[i]; 4428 if (isdigit(c)) 4429 c -= '0'; 4430 else if (isalpha(c)) 4431 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4432 else 4433 break; 4434 if (c >= 16) 4435 break; 4436 if ((i & 1) == 0) 4437 buf[i / 2] |= (c << 4); 4438 else 4439 buf[i / 2] |= c; 4440 } 4441 return ((i + 1) / 2); 4442 } 4443 4444 /* 4445 * Add LUN. 4446 * 4447 * Returns 0 for success, non-zero (errno) for failure. 4448 */ 4449 int 4450 ctl_add_lun(struct ctl_be_lun *be_lun) 4451 { 4452 struct ctl_softc *ctl_softc = control_softc; 4453 struct ctl_lun *nlun, *lun; 4454 struct scsi_vpd_id_descriptor *desc; 4455 struct scsi_vpd_id_t10 *t10id; 4456 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4457 int lun_number; 4458 int devidlen, idlen1, idlen2 = 0, len; 4459 4460 /* 4461 * We support only Direct Access, CD-ROM or Processor LUN types. 4462 */ 4463 switch (be_lun->lun_type) { 4464 case T_DIRECT: 4465 case T_PROCESSOR: 4466 case T_CDROM: 4467 break; 4468 case T_SEQUENTIAL: 4469 case T_CHANGER: 4470 default: 4471 return (EINVAL); 4472 } 4473 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK | M_ZERO); 4474 4475 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) * 4476 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO); 4477 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports, 4478 M_DEVBUF, M_WAITOK | M_ZERO); 4479 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports, 4480 M_DEVBUF, M_WAITOK | M_ZERO); 4481 4482 /* Generate LUN ID. */ 4483 devidlen = max(CTL_DEVID_MIN_LEN, 4484 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4485 idlen1 = sizeof(*t10id) + devidlen; 4486 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4487 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL); 4488 if (scsiname != NULL) { 4489 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4490 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4491 } 4492 eui = dnvlist_get_string(be_lun->options, "eui", NULL); 4493 if (eui != NULL) { 4494 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4495 } 4496 naa = dnvlist_get_string(be_lun->options, "naa", NULL); 4497 if (naa != NULL) { 4498 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4499 } 4500 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL); 4501 if (uuid != NULL) { 4502 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4503 } 4504 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4505 M_CTL, M_WAITOK | M_ZERO); 4506 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4507 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4508 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4509 desc->length = idlen1; 4510 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4511 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4512 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) { 4513 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4514 } else { 4515 strncpy(t10id->vendor, vendor, 4516 min(sizeof(t10id->vendor), strlen(vendor))); 4517 } 4518 strncpy((char *)t10id->vendor_spec_id, 4519 (char *)be_lun->device_id, devidlen); 4520 if (scsiname != NULL) { 4521 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4522 desc->length); 4523 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4524 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4525 SVPD_ID_TYPE_SCSI_NAME; 4526 desc->length = idlen2; 4527 strlcpy(desc->identifier, scsiname, idlen2); 4528 } 4529 if (eui != NULL) { 4530 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4531 desc->length); 4532 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4533 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4534 SVPD_ID_TYPE_EUI64; 4535 desc->length = hex2bin(eui, desc->identifier, 16); 4536 desc->length = desc->length > 12 ? 16 : 4537 (desc->length > 8 ? 12 : 8); 4538 len -= 16 - desc->length; 4539 } 4540 if (naa != NULL) { 4541 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4542 desc->length); 4543 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4544 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4545 SVPD_ID_TYPE_NAA; 4546 desc->length = hex2bin(naa, desc->identifier, 16); 4547 desc->length = desc->length > 8 ? 16 : 8; 4548 len -= 16 - desc->length; 4549 } 4550 if (uuid != NULL) { 4551 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4552 desc->length); 4553 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4554 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4555 SVPD_ID_TYPE_UUID; 4556 desc->identifier[0] = 0x10; 4557 hex2bin(uuid, &desc->identifier[2], 16); 4558 desc->length = 18; 4559 } 4560 lun->lun_devid->len = len; 4561 4562 mtx_lock(&ctl_softc->ctl_lock); 4563 /* 4564 * See if the caller requested a particular LUN number. If so, see 4565 * if it is available. Otherwise, allocate the first available LUN. 4566 */ 4567 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4568 if ((be_lun->req_lun_id > (ctl_max_luns - 1)) 4569 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4570 mtx_unlock(&ctl_softc->ctl_lock); 4571 if (be_lun->req_lun_id > (ctl_max_luns - 1)) { 4572 printf("ctl: requested LUN ID %d is higher " 4573 "than ctl_max_luns - 1 (%d)\n", 4574 be_lun->req_lun_id, ctl_max_luns - 1); 4575 } else { 4576 /* 4577 * XXX KDM return an error, or just assign 4578 * another LUN ID in this case?? 4579 */ 4580 printf("ctl: requested LUN ID %d is already " 4581 "in use\n", be_lun->req_lun_id); 4582 } 4583 fail: 4584 free(lun->lun_devid, M_CTL); 4585 free(lun, M_CTL); 4586 return (ENOSPC); 4587 } 4588 lun_number = be_lun->req_lun_id; 4589 } else { 4590 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns); 4591 if (lun_number == -1) { 4592 mtx_unlock(&ctl_softc->ctl_lock); 4593 printf("ctl: can't allocate LUN, out of LUNs\n"); 4594 goto fail; 4595 } 4596 } 4597 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4598 mtx_unlock(&ctl_softc->ctl_lock); 4599 4600 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4601 lun->lun = lun_number; 4602 lun->be_lun = be_lun; 4603 /* 4604 * The processor LUN is always enabled. Disk LUNs come on line 4605 * disabled, and must be enabled by the backend. 4606 */ 4607 lun->flags |= CTL_LUN_DISABLED; 4608 lun->backend = be_lun->be; 4609 be_lun->ctl_lun = lun; 4610 be_lun->lun_id = lun_number; 4611 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4612 lun->flags |= CTL_LUN_EJECTED; 4613 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4614 lun->flags |= CTL_LUN_NO_MEDIA; 4615 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4616 lun->flags |= CTL_LUN_STOPPED; 4617 4618 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4619 lun->flags |= CTL_LUN_PRIMARY_SC; 4620 4621 value = dnvlist_get_string(be_lun->options, "removable", NULL); 4622 if (value != NULL) { 4623 if (strcmp(value, "on") == 0) 4624 lun->flags |= CTL_LUN_REMOVABLE; 4625 } else if (be_lun->lun_type == T_CDROM) 4626 lun->flags |= CTL_LUN_REMOVABLE; 4627 4628 lun->ctl_softc = ctl_softc; 4629 #ifdef CTL_TIME_IO 4630 lun->last_busy = getsbinuptime(); 4631 #endif 4632 LIST_INIT(&lun->ooa_queue); 4633 STAILQ_INIT(&lun->error_list); 4634 lun->ie_reported = 1; 4635 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4636 ctl_tpc_lun_init(lun); 4637 if (lun->flags & CTL_LUN_REMOVABLE) { 4638 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4639 M_CTL, M_WAITOK); 4640 } 4641 4642 /* 4643 * Initialize the mode and log page index. 4644 */ 4645 ctl_init_page_index(lun); 4646 ctl_init_log_page_index(lun); 4647 4648 /* Setup statistics gathering */ 4649 lun->stats.item = lun_number; 4650 4651 /* 4652 * Now, before we insert this lun on the lun list, set the lun 4653 * inventory changed UA for all other luns. 4654 */ 4655 mtx_lock(&ctl_softc->ctl_lock); 4656 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4657 mtx_lock(&nlun->lun_lock); 4658 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4659 mtx_unlock(&nlun->lun_lock); 4660 } 4661 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4662 ctl_softc->ctl_luns[lun_number] = lun; 4663 ctl_softc->num_luns++; 4664 mtx_unlock(&ctl_softc->ctl_lock); 4665 4666 /* 4667 * We successfully added the LUN, attempt to enable it. 4668 */ 4669 if (ctl_enable_lun(lun) != 0) { 4670 printf("%s: ctl_enable_lun() failed!\n", __func__); 4671 mtx_lock(&ctl_softc->ctl_lock); 4672 STAILQ_REMOVE(&ctl_softc->lun_list, lun, ctl_lun, links); 4673 ctl_clear_mask(ctl_softc->ctl_lun_mask, lun_number); 4674 ctl_softc->ctl_luns[lun_number] = NULL; 4675 ctl_softc->num_luns--; 4676 mtx_unlock(&ctl_softc->ctl_lock); 4677 free(lun->lun_devid, M_CTL); 4678 free(lun, M_CTL); 4679 return (EIO); 4680 } 4681 4682 return (0); 4683 } 4684 4685 /* 4686 * Free LUN that has no active requests. 4687 */ 4688 static int 4689 ctl_free_lun(struct ctl_lun *lun) 4690 { 4691 struct ctl_softc *softc = lun->ctl_softc; 4692 struct ctl_lun *nlun; 4693 int i; 4694 4695 KASSERT(LIST_EMPTY(&lun->ooa_queue), 4696 ("Freeing a LUN %p with outstanding I/O!\n", lun)); 4697 4698 mtx_lock(&softc->ctl_lock); 4699 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4700 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4701 softc->ctl_luns[lun->lun] = NULL; 4702 softc->num_luns--; 4703 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4704 mtx_lock(&nlun->lun_lock); 4705 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4706 mtx_unlock(&nlun->lun_lock); 4707 } 4708 mtx_unlock(&softc->ctl_lock); 4709 4710 /* 4711 * Tell the backend to free resources, if this LUN has a backend. 4712 */ 4713 lun->be_lun->lun_shutdown(lun->be_lun); 4714 4715 lun->ie_reportcnt = UINT32_MAX; 4716 callout_drain(&lun->ie_callout); 4717 ctl_tpc_lun_shutdown(lun); 4718 mtx_destroy(&lun->lun_lock); 4719 free(lun->lun_devid, M_CTL); 4720 for (i = 0; i < ctl_max_ports; i++) 4721 free(lun->pending_ua[i], M_CTL); 4722 free(lun->pending_ua, M_DEVBUF); 4723 for (i = 0; i < ctl_max_ports; i++) 4724 free(lun->pr_keys[i], M_CTL); 4725 free(lun->pr_keys, M_DEVBUF); 4726 free(lun->write_buffer, M_CTL); 4727 free(lun->prevent, M_CTL); 4728 free(lun, M_CTL); 4729 4730 return (0); 4731 } 4732 4733 static int 4734 ctl_enable_lun(struct ctl_lun *lun) 4735 { 4736 struct ctl_softc *softc; 4737 struct ctl_port *port, *nport; 4738 int retval; 4739 4740 softc = lun->ctl_softc; 4741 4742 mtx_lock(&softc->ctl_lock); 4743 mtx_lock(&lun->lun_lock); 4744 KASSERT((lun->flags & CTL_LUN_DISABLED) != 0, 4745 ("%s: LUN not disabled", __func__)); 4746 lun->flags &= ~CTL_LUN_DISABLED; 4747 mtx_unlock(&lun->lun_lock); 4748 4749 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4750 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4751 port->lun_map != NULL || port->lun_enable == NULL) 4752 continue; 4753 4754 /* 4755 * Drop the lock while we call the FETD's enable routine. 4756 * This can lead to a callback into CTL (at least in the 4757 * case of the internal initiator frontend. 4758 */ 4759 mtx_unlock(&softc->ctl_lock); 4760 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4761 mtx_lock(&softc->ctl_lock); 4762 if (retval != 0) { 4763 printf("%s: FETD %s port %d returned error " 4764 "%d for lun_enable on lun %jd\n", 4765 __func__, port->port_name, port->targ_port, 4766 retval, (intmax_t)lun->lun); 4767 } 4768 } 4769 4770 mtx_unlock(&softc->ctl_lock); 4771 ctl_isc_announce_lun(lun); 4772 4773 return (0); 4774 } 4775 4776 static int 4777 ctl_disable_lun(struct ctl_lun *lun) 4778 { 4779 struct ctl_softc *softc; 4780 struct ctl_port *port; 4781 int retval; 4782 4783 softc = lun->ctl_softc; 4784 4785 mtx_lock(&softc->ctl_lock); 4786 mtx_lock(&lun->lun_lock); 4787 KASSERT((lun->flags & CTL_LUN_DISABLED) == 0, 4788 ("%s: LUN not enabled", __func__)); 4789 lun->flags |= CTL_LUN_DISABLED; 4790 mtx_unlock(&lun->lun_lock); 4791 4792 STAILQ_FOREACH(port, &softc->port_list, links) { 4793 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4794 port->lun_map != NULL || port->lun_disable == NULL) 4795 continue; 4796 4797 /* 4798 * Drop the lock before we call the frontend's disable 4799 * routine, to avoid lock order reversals. 4800 * 4801 * XXX KDM what happens if the frontend list changes while 4802 * we're traversing it? It's unlikely, but should be handled. 4803 */ 4804 mtx_unlock(&softc->ctl_lock); 4805 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4806 mtx_lock(&softc->ctl_lock); 4807 if (retval != 0) { 4808 printf("%s: FETD %s port %d returned error " 4809 "%d for lun_disable on lun %jd\n", 4810 __func__, port->port_name, port->targ_port, 4811 retval, (intmax_t)lun->lun); 4812 } 4813 } 4814 4815 mtx_unlock(&softc->ctl_lock); 4816 ctl_isc_announce_lun(lun); 4817 4818 return (0); 4819 } 4820 4821 int 4822 ctl_start_lun(struct ctl_be_lun *be_lun) 4823 { 4824 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4825 4826 mtx_lock(&lun->lun_lock); 4827 lun->flags &= ~CTL_LUN_STOPPED; 4828 mtx_unlock(&lun->lun_lock); 4829 return (0); 4830 } 4831 4832 int 4833 ctl_stop_lun(struct ctl_be_lun *be_lun) 4834 { 4835 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4836 4837 mtx_lock(&lun->lun_lock); 4838 lun->flags |= CTL_LUN_STOPPED; 4839 mtx_unlock(&lun->lun_lock); 4840 return (0); 4841 } 4842 4843 int 4844 ctl_lun_no_media(struct ctl_be_lun *be_lun) 4845 { 4846 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4847 4848 mtx_lock(&lun->lun_lock); 4849 lun->flags |= CTL_LUN_NO_MEDIA; 4850 mtx_unlock(&lun->lun_lock); 4851 return (0); 4852 } 4853 4854 int 4855 ctl_lun_has_media(struct ctl_be_lun *be_lun) 4856 { 4857 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4858 union ctl_ha_msg msg; 4859 4860 mtx_lock(&lun->lun_lock); 4861 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4862 if (lun->flags & CTL_LUN_REMOVABLE) 4863 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4864 mtx_unlock(&lun->lun_lock); 4865 if ((lun->flags & CTL_LUN_REMOVABLE) && 4866 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4867 bzero(&msg.ua, sizeof(msg.ua)); 4868 msg.hdr.msg_type = CTL_MSG_UA; 4869 msg.hdr.nexus.initid = -1; 4870 msg.hdr.nexus.targ_port = -1; 4871 msg.hdr.nexus.targ_lun = lun->lun; 4872 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4873 msg.ua.ua_all = 1; 4874 msg.ua.ua_set = 1; 4875 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4876 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4877 M_WAITOK); 4878 } 4879 return (0); 4880 } 4881 4882 int 4883 ctl_lun_ejected(struct ctl_be_lun *be_lun) 4884 { 4885 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4886 4887 mtx_lock(&lun->lun_lock); 4888 lun->flags |= CTL_LUN_EJECTED; 4889 mtx_unlock(&lun->lun_lock); 4890 return (0); 4891 } 4892 4893 int 4894 ctl_lun_primary(struct ctl_be_lun *be_lun) 4895 { 4896 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4897 4898 mtx_lock(&lun->lun_lock); 4899 lun->flags |= CTL_LUN_PRIMARY_SC; 4900 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4901 mtx_unlock(&lun->lun_lock); 4902 ctl_isc_announce_lun(lun); 4903 return (0); 4904 } 4905 4906 int 4907 ctl_lun_secondary(struct ctl_be_lun *be_lun) 4908 { 4909 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4910 4911 mtx_lock(&lun->lun_lock); 4912 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4913 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4914 mtx_unlock(&lun->lun_lock); 4915 ctl_isc_announce_lun(lun); 4916 return (0); 4917 } 4918 4919 /* 4920 * Remove LUN. If there are active requests, wait for completion. 4921 * 4922 * Returns 0 for success, non-zero (errno) for failure. 4923 * Completion is reported to backed via the lun_shutdown() method. 4924 */ 4925 int 4926 ctl_remove_lun(struct ctl_be_lun *be_lun) 4927 { 4928 struct ctl_lun *lun; 4929 4930 lun = (struct ctl_lun *)be_lun->ctl_lun; 4931 4932 ctl_disable_lun(lun); 4933 4934 mtx_lock(&lun->lun_lock); 4935 lun->flags |= CTL_LUN_INVALID; 4936 4937 /* 4938 * If there is nothing in the OOA queue, go ahead and free the LUN. 4939 * If we have something in the OOA queue, we'll free it when the 4940 * last I/O completes. 4941 */ 4942 if (LIST_EMPTY(&lun->ooa_queue)) { 4943 mtx_unlock(&lun->lun_lock); 4944 ctl_free_lun(lun); 4945 } else 4946 mtx_unlock(&lun->lun_lock); 4947 4948 return (0); 4949 } 4950 4951 void 4952 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4953 { 4954 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4955 union ctl_ha_msg msg; 4956 4957 mtx_lock(&lun->lun_lock); 4958 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4959 mtx_unlock(&lun->lun_lock); 4960 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4961 /* Send msg to other side. */ 4962 bzero(&msg.ua, sizeof(msg.ua)); 4963 msg.hdr.msg_type = CTL_MSG_UA; 4964 msg.hdr.nexus.initid = -1; 4965 msg.hdr.nexus.targ_port = -1; 4966 msg.hdr.nexus.targ_lun = lun->lun; 4967 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4968 msg.ua.ua_all = 1; 4969 msg.ua.ua_set = 1; 4970 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4971 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4972 M_WAITOK); 4973 } 4974 } 4975 4976 void 4977 ctl_lun_nsdata_ids(struct ctl_be_lun *be_lun, 4978 struct nvme_namespace_data *nsdata) 4979 { 4980 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4981 struct scsi_vpd_id_descriptor *idd; 4982 4983 if (lun->lun_devid == NULL) 4984 return; 4985 4986 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *) 4987 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_naa); 4988 if (idd != NULL) { 4989 if (idd->length == 16) { 4990 memcpy(nsdata->nguid, idd->identifier, 16); 4991 return; 4992 } 4993 if (idd->length == 8) { 4994 memcpy(nsdata->eui64, idd->identifier, 8); 4995 return; 4996 } 4997 } 4998 4999 idd = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *) 5000 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_eui64); 5001 if (idd != NULL) { 5002 if (idd->length == 8) { 5003 memcpy(nsdata->eui64, idd->identifier, 8); 5004 return; 5005 } 5006 } 5007 } 5008 5009 void 5010 ctl_lun_nvme_ids(struct ctl_be_lun *be_lun, void *data) 5011 { 5012 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5013 struct scsi_vpd_id_descriptor *naa, *eui64, *uuid; 5014 char *p; 5015 5016 memset(data, 0, 4096); 5017 5018 if (lun->lun_devid == NULL) 5019 return; 5020 5021 naa = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *) 5022 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_naa); 5023 eui64 = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *) 5024 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_eui64); 5025 uuid = scsi_get_devid_desc((struct scsi_vpd_id_descriptor *) 5026 lun->lun_devid->data, lun->lun_devid->len, scsi_devid_is_lun_uuid); 5027 5028 p = data; 5029 5030 /* EUI64 */ 5031 if ((naa != NULL && naa->length == 8) || eui64 != NULL) { 5032 *p++ = 1; 5033 *p++ = 8; 5034 p += 2; 5035 if (naa != NULL && naa->length == 8) 5036 memcpy(p, naa->identifier, 8); 5037 else 5038 memcpy(p, eui64->identifier, 8); 5039 p += 8; 5040 } 5041 5042 /* NGUID */ 5043 if (naa != NULL && naa->length == 16) { 5044 *p++ = 1; 5045 *p++ = 16; 5046 p += 2; 5047 memcpy(p, naa->identifier, 16); 5048 p += 16; 5049 } 5050 5051 /* UUID */ 5052 if (uuid != NULL) { 5053 *p++ = 1; 5054 *p++ = uuid->length; 5055 p += 2; 5056 memcpy(p, uuid->identifier, uuid->length); 5057 p += uuid->length; 5058 } 5059 } 5060 5061 /* 5062 * Backend "memory move is complete" callback for requests that never 5063 * make it down to say RAIDCore's configuration code. 5064 */ 5065 int 5066 ctl_config_move_done(union ctl_io *io, bool samethr) 5067 { 5068 int retval; 5069 5070 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5071 5072 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5073 ctl_data_print(io); 5074 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5075 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5076 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5077 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5078 /* 5079 * XXX KDM just assuming a single pointer here, and not a 5080 * S/G list. If we start using S/G lists for config data, 5081 * we'll need to know how to clean them up here as well. 5082 */ 5083 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5084 free(ctl_kern_data_ptr(io), M_CTL); 5085 ctl_done(io); 5086 retval = CTL_RETVAL_COMPLETE; 5087 } else { 5088 /* 5089 * XXX KDM now we need to continue data movement. Some 5090 * options: 5091 * - call ctl_scsiio() again? We don't do this for data 5092 * writes, because for those at least we know ahead of 5093 * time where the write will go and how long it is. For 5094 * config writes, though, that information is largely 5095 * contained within the write itself, thus we need to 5096 * parse out the data again. 5097 * 5098 * - Call some other function once the data is in? 5099 */ 5100 5101 /* 5102 * XXX KDM call ctl_scsiio() again for now, and check flag 5103 * bits to see whether we're allocated or not. 5104 */ 5105 switch (io->io_hdr.io_type) { 5106 case CTL_IO_SCSI: 5107 retval = ctl_scsiio(&io->scsiio); 5108 break; 5109 case CTL_IO_NVME: 5110 case CTL_IO_NVME_ADMIN: 5111 retval = ctl_nvmeio(&io->nvmeio); 5112 break; 5113 default: 5114 __assert_unreachable(); 5115 } 5116 } 5117 return (retval); 5118 } 5119 5120 /* 5121 * This gets called by a backend driver when it is done with a 5122 * data_submit method. 5123 */ 5124 void 5125 ctl_data_submit_done(union ctl_io *io) 5126 { 5127 /* 5128 * If the IO_CONT flag is set, we need to call the supplied 5129 * function to continue processing the I/O, instead of completing 5130 * the I/O just yet. 5131 * 5132 * If there is an error, though, we don't want to keep processing. 5133 * Instead, just send status back to the initiator. 5134 */ 5135 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5136 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5137 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5138 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5139 ctl_continue_io(io); 5140 return; 5141 } 5142 ctl_done(io); 5143 } 5144 5145 /* 5146 * This gets called by a backend driver when it is done with a 5147 * configuration write. 5148 */ 5149 void 5150 ctl_config_write_done(union ctl_io *io) 5151 { 5152 uint8_t *buf; 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 ctl_continue_io(io); 5167 return; 5168 } 5169 /* 5170 * Since a configuration write can be done for commands that actually 5171 * have data allocated, like write buffer, and commands that have 5172 * no data, like start/stop unit, we need to check here. 5173 */ 5174 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5175 buf = ctl_kern_data_ptr(io); 5176 else 5177 buf = NULL; 5178 ctl_done(io); 5179 if (buf) 5180 free(buf, M_CTL); 5181 } 5182 5183 void 5184 ctl_config_read_done(union ctl_io *io) 5185 { 5186 uint8_t *buf; 5187 5188 /* 5189 * If there is some error -- we are done, skip data transfer. 5190 */ 5191 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5192 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5193 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5194 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5195 buf = ctl_kern_data_ptr(io); 5196 else 5197 buf = NULL; 5198 ctl_done(io); 5199 if (buf) 5200 free(buf, M_CTL); 5201 return; 5202 } 5203 5204 /* 5205 * If the IO_CONT flag is set, we need to call the supplied 5206 * function to continue processing the I/O, instead of completing 5207 * the I/O just yet. 5208 */ 5209 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5210 ctl_continue_io(io); 5211 return; 5212 } 5213 5214 ctl_datamove(io); 5215 } 5216 5217 /* 5218 * SCSI release command. 5219 */ 5220 int 5221 ctl_scsi_release(struct ctl_scsiio *ctsio) 5222 { 5223 struct ctl_lun *lun = CTL_LUN(ctsio); 5224 uint32_t residx; 5225 5226 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5227 5228 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5229 5230 /* 5231 * XXX KDM right now, we only support LUN reservation. We don't 5232 * support 3rd party reservations, or extent reservations, which 5233 * might actually need the parameter list. If we've gotten this 5234 * far, we've got a LUN reservation. Anything else got kicked out 5235 * above. So, according to SPC, ignore the length. 5236 */ 5237 5238 mtx_lock(&lun->lun_lock); 5239 5240 /* 5241 * According to SPC, it is not an error for an intiator to attempt 5242 * to release a reservation on a LUN that isn't reserved, or that 5243 * is reserved by another initiator. The reservation can only be 5244 * released, though, by the initiator who made it or by one of 5245 * several reset type events. 5246 */ 5247 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5248 lun->flags &= ~CTL_LUN_RESERVED; 5249 5250 mtx_unlock(&lun->lun_lock); 5251 5252 ctl_set_success(ctsio); 5253 ctl_done((union ctl_io *)ctsio); 5254 return (CTL_RETVAL_COMPLETE); 5255 } 5256 5257 int 5258 ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5259 { 5260 struct ctl_lun *lun = CTL_LUN(ctsio); 5261 uint32_t residx; 5262 5263 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5264 5265 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5266 5267 /* 5268 * XXX KDM right now, we only support LUN reservation. We don't 5269 * support 3rd party reservations, or extent reservations, which 5270 * might actually need the parameter list. If we've gotten this 5271 * far, we've got a LUN reservation. Anything else got kicked out 5272 * above. So, according to SPC, ignore the length. 5273 */ 5274 5275 mtx_lock(&lun->lun_lock); 5276 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5277 ctl_set_reservation_conflict(ctsio); 5278 goto bailout; 5279 } 5280 5281 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5282 if (lun->flags & CTL_LUN_PR_RESERVED) { 5283 ctl_set_success(ctsio); 5284 goto bailout; 5285 } 5286 5287 lun->flags |= CTL_LUN_RESERVED; 5288 lun->res_idx = residx; 5289 ctl_set_success(ctsio); 5290 5291 bailout: 5292 mtx_unlock(&lun->lun_lock); 5293 ctl_done((union ctl_io *)ctsio); 5294 return (CTL_RETVAL_COMPLETE); 5295 } 5296 5297 int 5298 ctl_start_stop(struct ctl_scsiio *ctsio) 5299 { 5300 struct ctl_lun *lun = CTL_LUN(ctsio); 5301 struct scsi_start_stop_unit *cdb; 5302 int retval; 5303 5304 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5305 5306 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5307 5308 if ((cdb->how & SSS_PC_MASK) == 0) { 5309 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5310 (cdb->how & SSS_START) == 0) { 5311 uint32_t residx; 5312 5313 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5314 if (ctl_get_prkey(lun, residx) == 0 || 5315 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5316 ctl_set_reservation_conflict(ctsio); 5317 ctl_done((union ctl_io *)ctsio); 5318 return (CTL_RETVAL_COMPLETE); 5319 } 5320 } 5321 5322 if ((cdb->how & SSS_LOEJ) && 5323 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5324 ctl_set_invalid_field(ctsio, 5325 /*sks_valid*/ 1, 5326 /*command*/ 1, 5327 /*field*/ 4, 5328 /*bit_valid*/ 1, 5329 /*bit*/ 1); 5330 ctl_done((union ctl_io *)ctsio); 5331 return (CTL_RETVAL_COMPLETE); 5332 } 5333 5334 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5335 lun->prevent_count > 0) { 5336 /* "Medium removal prevented" */ 5337 ctl_set_sense(ctsio, /*current_error*/ 1, 5338 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5339 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5340 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5341 ctl_done((union ctl_io *)ctsio); 5342 return (CTL_RETVAL_COMPLETE); 5343 } 5344 } 5345 5346 retval = lun->backend->config_write((union ctl_io *)ctsio); 5347 return (retval); 5348 } 5349 5350 int 5351 ctl_prevent_allow(struct ctl_scsiio *ctsio) 5352 { 5353 struct ctl_lun *lun = CTL_LUN(ctsio); 5354 struct scsi_prevent *cdb; 5355 int retval; 5356 uint32_t initidx; 5357 5358 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5359 5360 cdb = (struct scsi_prevent *)ctsio->cdb; 5361 5362 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5363 ctl_set_invalid_opcode(ctsio); 5364 ctl_done((union ctl_io *)ctsio); 5365 return (CTL_RETVAL_COMPLETE); 5366 } 5367 5368 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5369 mtx_lock(&lun->lun_lock); 5370 if ((cdb->how & PR_PREVENT) && 5371 ctl_is_set(lun->prevent, initidx) == 0) { 5372 ctl_set_mask(lun->prevent, initidx); 5373 lun->prevent_count++; 5374 } else if ((cdb->how & PR_PREVENT) == 0 && 5375 ctl_is_set(lun->prevent, initidx)) { 5376 ctl_clear_mask(lun->prevent, initidx); 5377 lun->prevent_count--; 5378 } 5379 mtx_unlock(&lun->lun_lock); 5380 retval = lun->backend->config_write((union ctl_io *)ctsio); 5381 return (retval); 5382 } 5383 5384 /* 5385 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5386 * we don't really do anything with the LBA and length fields if the user 5387 * passes them in. Instead we'll just flush out the cache for the entire 5388 * LUN. 5389 */ 5390 int 5391 ctl_sync_cache(struct ctl_scsiio *ctsio) 5392 { 5393 struct ctl_lun *lun = CTL_LUN(ctsio); 5394 struct ctl_lba_len_flags *lbalen; 5395 uint64_t starting_lba; 5396 uint32_t block_count; 5397 int retval; 5398 uint8_t byte2; 5399 5400 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5401 5402 retval = 0; 5403 5404 switch (ctsio->cdb[0]) { 5405 case SYNCHRONIZE_CACHE: { 5406 struct scsi_sync_cache *cdb; 5407 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5408 5409 starting_lba = scsi_4btoul(cdb->begin_lba); 5410 block_count = scsi_2btoul(cdb->lb_count); 5411 byte2 = cdb->byte2; 5412 break; 5413 } 5414 case SYNCHRONIZE_CACHE_16: { 5415 struct scsi_sync_cache_16 *cdb; 5416 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5417 5418 starting_lba = scsi_8btou64(cdb->begin_lba); 5419 block_count = scsi_4btoul(cdb->lb_count); 5420 byte2 = cdb->byte2; 5421 break; 5422 } 5423 default: 5424 ctl_set_invalid_opcode(ctsio); 5425 ctl_done((union ctl_io *)ctsio); 5426 goto bailout; 5427 break; /* NOTREACHED */ 5428 } 5429 5430 /* 5431 * We check the LBA and length, but don't do anything with them. 5432 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5433 * get flushed. This check will just help satisfy anyone who wants 5434 * to see an error for an out of range LBA. 5435 */ 5436 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5437 ctl_set_lba_out_of_range(ctsio, 5438 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5439 ctl_done((union ctl_io *)ctsio); 5440 goto bailout; 5441 } 5442 5443 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5444 lbalen->lba = starting_lba; 5445 lbalen->len = block_count; 5446 lbalen->flags = byte2; 5447 retval = lun->backend->config_write((union ctl_io *)ctsio); 5448 5449 bailout: 5450 return (retval); 5451 } 5452 5453 int 5454 ctl_format(struct ctl_scsiio *ctsio) 5455 { 5456 struct scsi_format *cdb; 5457 int length, defect_list_len; 5458 5459 CTL_DEBUG_PRINT(("ctl_format\n")); 5460 5461 cdb = (struct scsi_format *)ctsio->cdb; 5462 5463 length = 0; 5464 if (cdb->byte2 & SF_FMTDATA) { 5465 if (cdb->byte2 & SF_LONGLIST) 5466 length = sizeof(struct scsi_format_header_long); 5467 else 5468 length = sizeof(struct scsi_format_header_short); 5469 } 5470 5471 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5472 && (length > 0)) { 5473 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5474 ctsio->kern_data_len = length; 5475 ctsio->kern_total_len = length; 5476 ctsio->kern_rel_offset = 0; 5477 ctsio->kern_sg_entries = 0; 5478 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5479 ctsio->be_move_done = ctl_config_move_done; 5480 ctl_datamove((union ctl_io *)ctsio); 5481 5482 return (CTL_RETVAL_COMPLETE); 5483 } 5484 5485 defect_list_len = 0; 5486 5487 if (cdb->byte2 & SF_FMTDATA) { 5488 if (cdb->byte2 & SF_LONGLIST) { 5489 struct scsi_format_header_long *header; 5490 5491 header = (struct scsi_format_header_long *) 5492 ctsio->kern_data_ptr; 5493 5494 defect_list_len = scsi_4btoul(header->defect_list_len); 5495 if (defect_list_len != 0) { 5496 ctl_set_invalid_field(ctsio, 5497 /*sks_valid*/ 1, 5498 /*command*/ 0, 5499 /*field*/ 2, 5500 /*bit_valid*/ 0, 5501 /*bit*/ 0); 5502 goto bailout; 5503 } 5504 } else { 5505 struct scsi_format_header_short *header; 5506 5507 header = (struct scsi_format_header_short *) 5508 ctsio->kern_data_ptr; 5509 5510 defect_list_len = scsi_2btoul(header->defect_list_len); 5511 if (defect_list_len != 0) { 5512 ctl_set_invalid_field(ctsio, 5513 /*sks_valid*/ 1, 5514 /*command*/ 0, 5515 /*field*/ 2, 5516 /*bit_valid*/ 0, 5517 /*bit*/ 0); 5518 goto bailout; 5519 } 5520 } 5521 } 5522 5523 ctl_set_success(ctsio); 5524 bailout: 5525 5526 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5527 free(ctsio->kern_data_ptr, M_CTL); 5528 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5529 } 5530 5531 ctl_done((union ctl_io *)ctsio); 5532 return (CTL_RETVAL_COMPLETE); 5533 } 5534 5535 int 5536 ctl_read_buffer(struct ctl_scsiio *ctsio) 5537 { 5538 struct ctl_lun *lun = CTL_LUN(ctsio); 5539 uint64_t buffer_offset; 5540 uint32_t len; 5541 uint8_t byte2; 5542 static uint8_t descr[4]; 5543 static uint8_t echo_descr[4] = { 0 }; 5544 5545 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5546 5547 switch (ctsio->cdb[0]) { 5548 case READ_BUFFER: { 5549 struct scsi_read_buffer *cdb; 5550 5551 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5552 buffer_offset = scsi_3btoul(cdb->offset); 5553 len = scsi_3btoul(cdb->length); 5554 byte2 = cdb->byte2; 5555 break; 5556 } 5557 case READ_BUFFER_16: { 5558 struct scsi_read_buffer_16 *cdb; 5559 5560 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5561 buffer_offset = scsi_8btou64(cdb->offset); 5562 len = scsi_4btoul(cdb->length); 5563 byte2 = cdb->byte2; 5564 break; 5565 } 5566 default: /* This shouldn't happen. */ 5567 ctl_set_invalid_opcode(ctsio); 5568 ctl_done((union ctl_io *)ctsio); 5569 return (CTL_RETVAL_COMPLETE); 5570 } 5571 5572 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5573 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5574 ctl_set_invalid_field(ctsio, 5575 /*sks_valid*/ 1, 5576 /*command*/ 1, 5577 /*field*/ 6, 5578 /*bit_valid*/ 0, 5579 /*bit*/ 0); 5580 ctl_done((union ctl_io *)ctsio); 5581 return (CTL_RETVAL_COMPLETE); 5582 } 5583 5584 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5585 descr[0] = 0; 5586 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5587 ctsio->kern_data_ptr = descr; 5588 len = min(len, sizeof(descr)); 5589 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5590 ctsio->kern_data_ptr = echo_descr; 5591 len = min(len, sizeof(echo_descr)); 5592 } else { 5593 if (lun->write_buffer == NULL) { 5594 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5595 M_CTL, M_WAITOK); 5596 } 5597 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5598 } 5599 ctsio->kern_data_len = len; 5600 ctsio->kern_total_len = len; 5601 ctsio->kern_rel_offset = 0; 5602 ctsio->kern_sg_entries = 0; 5603 ctl_set_success(ctsio); 5604 ctsio->be_move_done = ctl_config_move_done; 5605 ctl_datamove((union ctl_io *)ctsio); 5606 return (CTL_RETVAL_COMPLETE); 5607 } 5608 5609 int 5610 ctl_write_buffer(struct ctl_scsiio *ctsio) 5611 { 5612 struct ctl_lun *lun = CTL_LUN(ctsio); 5613 struct scsi_write_buffer *cdb; 5614 int buffer_offset, len; 5615 5616 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5617 5618 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5619 5620 len = scsi_3btoul(cdb->length); 5621 buffer_offset = scsi_3btoul(cdb->offset); 5622 5623 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5624 ctl_set_invalid_field(ctsio, 5625 /*sks_valid*/ 1, 5626 /*command*/ 1, 5627 /*field*/ 6, 5628 /*bit_valid*/ 0, 5629 /*bit*/ 0); 5630 ctl_done((union ctl_io *)ctsio); 5631 return (CTL_RETVAL_COMPLETE); 5632 } 5633 5634 /* 5635 * If we've got a kernel request that hasn't been malloced yet, 5636 * malloc it and tell the caller the data buffer is here. 5637 */ 5638 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5639 if (lun->write_buffer == NULL) { 5640 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5641 M_CTL, M_WAITOK); 5642 } 5643 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5644 ctsio->kern_data_len = len; 5645 ctsio->kern_total_len = len; 5646 ctsio->kern_rel_offset = 0; 5647 ctsio->kern_sg_entries = 0; 5648 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5649 ctsio->be_move_done = ctl_config_move_done; 5650 ctl_datamove((union ctl_io *)ctsio); 5651 5652 return (CTL_RETVAL_COMPLETE); 5653 } 5654 5655 ctl_set_success(ctsio); 5656 ctl_done((union ctl_io *)ctsio); 5657 return (CTL_RETVAL_COMPLETE); 5658 } 5659 5660 static int 5661 ctl_write_same_cont(union ctl_io *io) 5662 { 5663 struct ctl_lun *lun = CTL_LUN(io); 5664 struct ctl_scsiio *ctsio; 5665 struct ctl_lba_len_flags *lbalen; 5666 int retval; 5667 5668 CTL_IO_ASSERT(io, SCSI); 5669 5670 ctsio = &io->scsiio; 5671 ctsio->io_hdr.status = CTL_STATUS_NONE; 5672 lbalen = (struct ctl_lba_len_flags *) 5673 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5674 lbalen->lba += lbalen->len; 5675 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) { 5676 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 5677 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba; 5678 } 5679 5680 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n")); 5681 retval = lun->backend->config_write((union ctl_io *)ctsio); 5682 return (retval); 5683 } 5684 5685 int 5686 ctl_write_same(struct ctl_scsiio *ctsio) 5687 { 5688 struct ctl_lun *lun = CTL_LUN(ctsio); 5689 struct ctl_lba_len_flags *lbalen; 5690 const char *val; 5691 uint64_t lba, ival; 5692 uint32_t num_blocks; 5693 int len, retval; 5694 uint8_t byte2; 5695 5696 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5697 5698 switch (ctsio->cdb[0]) { 5699 case WRITE_SAME_10: { 5700 struct scsi_write_same_10 *cdb; 5701 5702 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5703 5704 lba = scsi_4btoul(cdb->addr); 5705 num_blocks = scsi_2btoul(cdb->length); 5706 byte2 = cdb->byte2; 5707 break; 5708 } 5709 case WRITE_SAME_16: { 5710 struct scsi_write_same_16 *cdb; 5711 5712 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5713 5714 lba = scsi_8btou64(cdb->addr); 5715 num_blocks = scsi_4btoul(cdb->length); 5716 byte2 = cdb->byte2; 5717 break; 5718 } 5719 default: 5720 /* 5721 * We got a command we don't support. This shouldn't 5722 * happen, commands should be filtered out above us. 5723 */ 5724 ctl_set_invalid_opcode(ctsio); 5725 ctl_done((union ctl_io *)ctsio); 5726 5727 return (CTL_RETVAL_COMPLETE); 5728 break; /* NOTREACHED */ 5729 } 5730 5731 /* ANCHOR flag can be used only together with UNMAP */ 5732 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5733 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5734 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5735 ctl_done((union ctl_io *)ctsio); 5736 return (CTL_RETVAL_COMPLETE); 5737 } 5738 5739 /* 5740 * The first check is to make sure we're in bounds, the second 5741 * check is to catch wrap-around problems. If the lba + num blocks 5742 * is less than the lba, then we've wrapped around and the block 5743 * range is invalid anyway. 5744 */ 5745 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5746 || ((lba + num_blocks) < lba)) { 5747 ctl_set_lba_out_of_range(ctsio, 5748 MAX(lba, lun->be_lun->maxlba + 1)); 5749 ctl_done((union ctl_io *)ctsio); 5750 return (CTL_RETVAL_COMPLETE); 5751 } 5752 5753 /* Zero number of blocks means "to the last logical block" */ 5754 if (num_blocks == 0) { 5755 ival = UINT64_MAX; 5756 val = dnvlist_get_string(lun->be_lun->options, 5757 "write_same_max_lba", NULL); 5758 if (val != NULL) 5759 ctl_expand_number(val, &ival); 5760 if ((lun->be_lun->maxlba + 1) - lba > ival) { 5761 ctl_set_invalid_field(ctsio, 5762 /*sks_valid*/ 1, /*command*/ 1, 5763 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10, 5764 /*bit_valid*/ 0, /*bit*/ 0); 5765 ctl_done((union ctl_io *)ctsio); 5766 return (CTL_RETVAL_COMPLETE); 5767 } 5768 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5769 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 5770 ctsio->io_cont = ctl_write_same_cont; 5771 num_blocks = 1 << 31; 5772 } else 5773 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5774 } 5775 5776 len = lun->be_lun->blocksize; 5777 5778 /* 5779 * If we've got a kernel request that hasn't been malloced yet, 5780 * malloc it and tell the caller the data buffer is here. 5781 */ 5782 if ((byte2 & SWS_NDOB) == 0 && 5783 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5784 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5785 ctsio->kern_data_len = len; 5786 ctsio->kern_total_len = len; 5787 ctsio->kern_rel_offset = 0; 5788 ctsio->kern_sg_entries = 0; 5789 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5790 ctsio->be_move_done = ctl_config_move_done; 5791 ctl_datamove((union ctl_io *)ctsio); 5792 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 5796 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5797 lbalen->lba = lba; 5798 lbalen->len = num_blocks; 5799 lbalen->flags = byte2; 5800 retval = lun->backend->config_write((union ctl_io *)ctsio); 5801 5802 return (retval); 5803 } 5804 5805 int 5806 ctl_unmap(struct ctl_scsiio *ctsio) 5807 { 5808 struct ctl_lun *lun = CTL_LUN(ctsio); 5809 struct scsi_unmap *cdb; 5810 struct ctl_ptr_len_flags *ptrlen; 5811 struct scsi_unmap_header *hdr; 5812 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5813 uint64_t lba; 5814 uint32_t num_blocks; 5815 int len, retval; 5816 uint8_t byte2; 5817 5818 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5819 5820 cdb = (struct scsi_unmap *)ctsio->cdb; 5821 len = scsi_2btoul(cdb->length); 5822 byte2 = cdb->byte2; 5823 5824 /* 5825 * If we've got a kernel request that hasn't been malloced yet, 5826 * malloc it and tell the caller the data buffer is here. 5827 */ 5828 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5829 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5830 ctsio->kern_data_len = len; 5831 ctsio->kern_total_len = len; 5832 ctsio->kern_rel_offset = 0; 5833 ctsio->kern_sg_entries = 0; 5834 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5835 ctsio->be_move_done = ctl_config_move_done; 5836 ctl_datamove((union ctl_io *)ctsio); 5837 5838 return (CTL_RETVAL_COMPLETE); 5839 } 5840 5841 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5842 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5843 if (len < sizeof (*hdr) || 5844 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5845 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5846 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5847 ctl_set_invalid_field(ctsio, 5848 /*sks_valid*/ 0, 5849 /*command*/ 0, 5850 /*field*/ 0, 5851 /*bit_valid*/ 0, 5852 /*bit*/ 0); 5853 goto done; 5854 } 5855 len = scsi_2btoul(hdr->desc_length); 5856 buf = (struct scsi_unmap_desc *)(hdr + 1); 5857 end = buf + len / sizeof(*buf); 5858 5859 endnz = buf; 5860 for (range = buf; range < end; range++) { 5861 lba = scsi_8btou64(range->lba); 5862 num_blocks = scsi_4btoul(range->length); 5863 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5864 || ((lba + num_blocks) < lba)) { 5865 ctl_set_lba_out_of_range(ctsio, 5866 MAX(lba, lun->be_lun->maxlba + 1)); 5867 ctl_done((union ctl_io *)ctsio); 5868 return (CTL_RETVAL_COMPLETE); 5869 } 5870 if (num_blocks != 0) 5871 endnz = range + 1; 5872 } 5873 5874 /* 5875 * Block backend can not handle zero last range. 5876 * Filter it out and return if there is nothing left. 5877 */ 5878 len = (uint8_t *)endnz - (uint8_t *)buf; 5879 if (len == 0) { 5880 ctl_set_success(ctsio); 5881 goto done; 5882 } 5883 5884 mtx_lock(&lun->lun_lock); 5885 ptrlen = (struct ctl_ptr_len_flags *) 5886 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5887 ptrlen->ptr = (void *)buf; 5888 ptrlen->len = len; 5889 ptrlen->flags = byte2; 5890 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE); 5891 mtx_unlock(&lun->lun_lock); 5892 5893 retval = lun->backend->config_write((union ctl_io *)ctsio); 5894 return (retval); 5895 5896 done: 5897 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5898 free(ctsio->kern_data_ptr, M_CTL); 5899 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5900 } 5901 ctl_done((union ctl_io *)ctsio); 5902 return (CTL_RETVAL_COMPLETE); 5903 } 5904 5905 int 5906 ctl_default_page_handler(struct ctl_scsiio *ctsio, 5907 struct ctl_page_index *page_index, uint8_t *page_ptr) 5908 { 5909 struct ctl_lun *lun = CTL_LUN(ctsio); 5910 uint8_t *current_cp; 5911 int set_ua; 5912 uint32_t initidx; 5913 5914 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5915 set_ua = 0; 5916 5917 current_cp = (page_index->page_data + (page_index->page_len * 5918 CTL_PAGE_CURRENT)); 5919 5920 mtx_lock(&lun->lun_lock); 5921 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5922 memcpy(current_cp, page_ptr, page_index->page_len); 5923 set_ua = 1; 5924 } 5925 if (set_ua != 0) 5926 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5927 mtx_unlock(&lun->lun_lock); 5928 if (set_ua) { 5929 ctl_isc_announce_mode(lun, 5930 ctl_get_initindex(&ctsio->io_hdr.nexus), 5931 page_index->page_code, page_index->subpage); 5932 } 5933 return (CTL_RETVAL_COMPLETE); 5934 } 5935 5936 static void 5937 ctl_ie_timer(void *arg) 5938 { 5939 struct ctl_lun *lun = arg; 5940 uint64_t t; 5941 5942 if (lun->ie_asc == 0) 5943 return; 5944 5945 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5946 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5947 else 5948 lun->ie_reported = 0; 5949 5950 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5951 lun->ie_reportcnt++; 5952 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5953 if (t == 0 || t == UINT32_MAX) 5954 t = 3000; /* 5 min */ 5955 callout_schedule_sbt(&lun->ie_callout, SBT_1S / 10 * t, 5956 SBT_1S / 10, 0); 5957 } 5958 } 5959 5960 int 5961 ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5962 struct ctl_page_index *page_index, uint8_t *page_ptr) 5963 { 5964 struct ctl_lun *lun = CTL_LUN(ctsio); 5965 struct scsi_info_exceptions_page *pg; 5966 uint64_t t; 5967 5968 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5969 5970 pg = (struct scsi_info_exceptions_page *)page_ptr; 5971 mtx_lock(&lun->lun_lock); 5972 if (pg->info_flags & SIEP_FLAGS_TEST) { 5973 lun->ie_asc = 0x5d; 5974 lun->ie_ascq = 0xff; 5975 if (pg->mrie == SIEP_MRIE_UA) { 5976 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5977 lun->ie_reported = 1; 5978 } else { 5979 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5980 lun->ie_reported = -1; 5981 } 5982 lun->ie_reportcnt = 1; 5983 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5984 lun->ie_reportcnt++; 5985 t = scsi_4btoul(pg->interval_timer); 5986 if (t == 0 || t == UINT32_MAX) 5987 t = 3000; /* 5 min */ 5988 callout_reset_sbt(&lun->ie_callout, SBT_1S / 10 * t, 5989 SBT_1S / 10, ctl_ie_timer, lun, 0); 5990 } 5991 } else { 5992 lun->ie_asc = 0; 5993 lun->ie_ascq = 0; 5994 lun->ie_reported = 1; 5995 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5996 lun->ie_reportcnt = UINT32_MAX; 5997 callout_stop(&lun->ie_callout); 5998 } 5999 mtx_unlock(&lun->lun_lock); 6000 return (CTL_RETVAL_COMPLETE); 6001 } 6002 6003 static int 6004 ctl_do_mode_select(union ctl_io *io) 6005 { 6006 struct ctl_lun *lun = CTL_LUN(io); 6007 struct scsi_mode_page_header *page_header; 6008 struct ctl_page_index *page_index; 6009 struct ctl_scsiio *ctsio; 6010 int page_len, page_len_offset, page_len_size; 6011 union ctl_modepage_info *modepage_info; 6012 uint16_t *len_left, *len_used; 6013 int retval, i; 6014 6015 CTL_IO_ASSERT(io, SCSI); 6016 6017 ctsio = &io->scsiio; 6018 page_index = NULL; 6019 page_len = 0; 6020 6021 modepage_info = (union ctl_modepage_info *) 6022 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6023 len_left = &modepage_info->header.len_left; 6024 len_used = &modepage_info->header.len_used; 6025 6026 do_next_page: 6027 6028 page_header = (struct scsi_mode_page_header *) 6029 (ctsio->kern_data_ptr + *len_used); 6030 6031 if (*len_left == 0) { 6032 free(ctsio->kern_data_ptr, M_CTL); 6033 ctl_set_success(ctsio); 6034 ctl_done((union ctl_io *)ctsio); 6035 return (CTL_RETVAL_COMPLETE); 6036 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 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 } else if ((page_header->page_code & SMPH_SPF) 6043 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6044 free(ctsio->kern_data_ptr, M_CTL); 6045 ctl_set_param_len_error(ctsio); 6046 ctl_done((union ctl_io *)ctsio); 6047 return (CTL_RETVAL_COMPLETE); 6048 } 6049 6050 /* 6051 * XXX KDM should we do something with the block descriptor? 6052 */ 6053 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6054 page_index = &lun->mode_pages.index[i]; 6055 if (lun->be_lun->lun_type == T_DIRECT && 6056 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6057 continue; 6058 if (lun->be_lun->lun_type == T_PROCESSOR && 6059 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6060 continue; 6061 if (lun->be_lun->lun_type == T_CDROM && 6062 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6063 continue; 6064 6065 if ((page_index->page_code & SMPH_PC_MASK) != 6066 (page_header->page_code & SMPH_PC_MASK)) 6067 continue; 6068 6069 /* 6070 * If neither page has a subpage code, then we've got a 6071 * match. 6072 */ 6073 if (((page_index->page_code & SMPH_SPF) == 0) 6074 && ((page_header->page_code & SMPH_SPF) == 0)) { 6075 page_len = page_header->page_length; 6076 break; 6077 } 6078 6079 /* 6080 * If both pages have subpages, then the subpage numbers 6081 * have to match. 6082 */ 6083 if ((page_index->page_code & SMPH_SPF) 6084 && (page_header->page_code & SMPH_SPF)) { 6085 struct scsi_mode_page_header_sp *sph; 6086 6087 sph = (struct scsi_mode_page_header_sp *)page_header; 6088 if (page_index->subpage == sph->subpage) { 6089 page_len = scsi_2btoul(sph->page_length); 6090 break; 6091 } 6092 } 6093 } 6094 6095 /* 6096 * If we couldn't find the page, or if we don't have a mode select 6097 * handler for it, send back an error to the user. 6098 */ 6099 if ((i >= CTL_NUM_MODE_PAGES) 6100 || (page_index->select_handler == NULL)) { 6101 ctl_set_invalid_field(ctsio, 6102 /*sks_valid*/ 1, 6103 /*command*/ 0, 6104 /*field*/ *len_used, 6105 /*bit_valid*/ 0, 6106 /*bit*/ 0); 6107 free(ctsio->kern_data_ptr, M_CTL); 6108 ctl_done((union ctl_io *)ctsio); 6109 return (CTL_RETVAL_COMPLETE); 6110 } 6111 6112 if (page_index->page_code & SMPH_SPF) { 6113 page_len_offset = 2; 6114 page_len_size = 2; 6115 } else { 6116 page_len_size = 1; 6117 page_len_offset = 1; 6118 } 6119 6120 /* 6121 * If the length the initiator gives us isn't the one we specify in 6122 * the mode page header, or if they didn't specify enough data in 6123 * the CDB to avoid truncating this page, kick out the request. 6124 */ 6125 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6126 ctl_set_invalid_field(ctsio, 6127 /*sks_valid*/ 1, 6128 /*command*/ 0, 6129 /*field*/ *len_used + page_len_offset, 6130 /*bit_valid*/ 0, 6131 /*bit*/ 0); 6132 free(ctsio->kern_data_ptr, M_CTL); 6133 ctl_done((union ctl_io *)ctsio); 6134 return (CTL_RETVAL_COMPLETE); 6135 } 6136 if (*len_left < page_index->page_len) { 6137 free(ctsio->kern_data_ptr, M_CTL); 6138 ctl_set_param_len_error(ctsio); 6139 ctl_done((union ctl_io *)ctsio); 6140 return (CTL_RETVAL_COMPLETE); 6141 } 6142 6143 /* 6144 * Run through the mode page, checking to make sure that the bits 6145 * the user changed are actually legal for him to change. 6146 */ 6147 for (i = 0; i < page_index->page_len; i++) { 6148 uint8_t *user_byte, *change_mask, *current_byte; 6149 int bad_bit; 6150 int j; 6151 6152 user_byte = (uint8_t *)page_header + i; 6153 change_mask = page_index->page_data + 6154 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6155 current_byte = page_index->page_data + 6156 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6157 6158 /* 6159 * Check to see whether the user set any bits in this byte 6160 * that he is not allowed to set. 6161 */ 6162 if ((*user_byte & ~(*change_mask)) == 6163 (*current_byte & ~(*change_mask))) 6164 continue; 6165 6166 /* 6167 * Go through bit by bit to determine which one is illegal. 6168 */ 6169 bad_bit = 0; 6170 for (j = 7; j >= 0; j--) { 6171 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6172 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6173 bad_bit = i; 6174 break; 6175 } 6176 } 6177 ctl_set_invalid_field(ctsio, 6178 /*sks_valid*/ 1, 6179 /*command*/ 0, 6180 /*field*/ *len_used + i, 6181 /*bit_valid*/ 1, 6182 /*bit*/ bad_bit); 6183 free(ctsio->kern_data_ptr, M_CTL); 6184 ctl_done((union ctl_io *)ctsio); 6185 return (CTL_RETVAL_COMPLETE); 6186 } 6187 6188 /* 6189 * Decrement these before we call the page handler, since we may 6190 * end up getting called back one way or another before the handler 6191 * returns to this context. 6192 */ 6193 *len_left -= page_index->page_len; 6194 *len_used += page_index->page_len; 6195 6196 retval = page_index->select_handler(ctsio, page_index, 6197 (uint8_t *)page_header); 6198 6199 /* 6200 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6201 * wait until this queued command completes to finish processing 6202 * the mode page. If it returns anything other than 6203 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6204 * already set the sense information, freed the data pointer, and 6205 * completed the io for us. 6206 */ 6207 if (retval != CTL_RETVAL_COMPLETE) 6208 goto bailout_no_done; 6209 6210 /* 6211 * If the initiator sent us more than one page, parse the next one. 6212 */ 6213 if (*len_left > 0) 6214 goto do_next_page; 6215 6216 ctl_set_success(ctsio); 6217 free(ctsio->kern_data_ptr, M_CTL); 6218 ctl_done((union ctl_io *)ctsio); 6219 6220 bailout_no_done: 6221 6222 return (CTL_RETVAL_COMPLETE); 6223 6224 } 6225 6226 int 6227 ctl_mode_select(struct ctl_scsiio *ctsio) 6228 { 6229 struct ctl_lun *lun = CTL_LUN(ctsio); 6230 union ctl_modepage_info *modepage_info; 6231 int bd_len, i, header_size, param_len, rtd; 6232 uint32_t initidx; 6233 6234 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6235 switch (ctsio->cdb[0]) { 6236 case MODE_SELECT_6: { 6237 struct scsi_mode_select_6 *cdb; 6238 6239 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6240 6241 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6242 param_len = cdb->length; 6243 header_size = sizeof(struct scsi_mode_header_6); 6244 break; 6245 } 6246 case MODE_SELECT_10: { 6247 struct scsi_mode_select_10 *cdb; 6248 6249 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6250 6251 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6252 param_len = scsi_2btoul(cdb->length); 6253 header_size = sizeof(struct scsi_mode_header_10); 6254 break; 6255 } 6256 default: 6257 ctl_set_invalid_opcode(ctsio); 6258 ctl_done((union ctl_io *)ctsio); 6259 return (CTL_RETVAL_COMPLETE); 6260 } 6261 6262 if (rtd) { 6263 if (param_len != 0) { 6264 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6265 /*command*/ 1, /*field*/ 0, 6266 /*bit_valid*/ 0, /*bit*/ 0); 6267 ctl_done((union ctl_io *)ctsio); 6268 return (CTL_RETVAL_COMPLETE); 6269 } 6270 6271 /* Revert to defaults. */ 6272 ctl_init_page_index(lun); 6273 mtx_lock(&lun->lun_lock); 6274 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6275 mtx_unlock(&lun->lun_lock); 6276 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6277 ctl_isc_announce_mode(lun, -1, 6278 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6279 lun->mode_pages.index[i].subpage); 6280 } 6281 ctl_set_success(ctsio); 6282 ctl_done((union ctl_io *)ctsio); 6283 return (CTL_RETVAL_COMPLETE); 6284 } 6285 6286 /* 6287 * From SPC-3: 6288 * "A parameter list length of zero indicates that the Data-Out Buffer 6289 * shall be empty. This condition shall not be considered as an error." 6290 */ 6291 if (param_len == 0) { 6292 ctl_set_success(ctsio); 6293 ctl_done((union ctl_io *)ctsio); 6294 return (CTL_RETVAL_COMPLETE); 6295 } 6296 6297 /* 6298 * Since we'll hit this the first time through, prior to 6299 * allocation, we don't need to free a data buffer here. 6300 */ 6301 if (param_len < header_size) { 6302 ctl_set_param_len_error(ctsio); 6303 ctl_done((union ctl_io *)ctsio); 6304 return (CTL_RETVAL_COMPLETE); 6305 } 6306 6307 /* 6308 * Allocate the data buffer and grab the user's data. In theory, 6309 * we shouldn't have to sanity check the parameter list length here 6310 * because the maximum size is 64K. We should be able to malloc 6311 * that much without too many problems. 6312 */ 6313 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6314 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6315 ctsio->kern_data_len = param_len; 6316 ctsio->kern_total_len = param_len; 6317 ctsio->kern_rel_offset = 0; 6318 ctsio->kern_sg_entries = 0; 6319 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6320 ctsio->be_move_done = ctl_config_move_done; 6321 ctl_datamove((union ctl_io *)ctsio); 6322 6323 return (CTL_RETVAL_COMPLETE); 6324 } 6325 6326 switch (ctsio->cdb[0]) { 6327 case MODE_SELECT_6: { 6328 struct scsi_mode_header_6 *mh6; 6329 6330 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6331 bd_len = mh6->blk_desc_len; 6332 break; 6333 } 6334 case MODE_SELECT_10: { 6335 struct scsi_mode_header_10 *mh10; 6336 6337 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6338 bd_len = scsi_2btoul(mh10->blk_desc_len); 6339 break; 6340 } 6341 default: 6342 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6343 } 6344 6345 if (param_len < (header_size + bd_len)) { 6346 free(ctsio->kern_data_ptr, M_CTL); 6347 ctl_set_param_len_error(ctsio); 6348 ctl_done((union ctl_io *)ctsio); 6349 return (CTL_RETVAL_COMPLETE); 6350 } 6351 6352 /* 6353 * Set the IO_CONT flag, so that if this I/O gets passed to 6354 * ctl_config_write_done(), it'll get passed back to 6355 * ctl_do_mode_select() for further processing, or completion if 6356 * we're all done. 6357 */ 6358 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6359 ctsio->io_cont = ctl_do_mode_select; 6360 6361 modepage_info = (union ctl_modepage_info *) 6362 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6363 memset(modepage_info, 0, sizeof(*modepage_info)); 6364 modepage_info->header.len_left = param_len - header_size - bd_len; 6365 modepage_info->header.len_used = header_size + bd_len; 6366 6367 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6368 } 6369 6370 int 6371 ctl_mode_sense(struct ctl_scsiio *ctsio) 6372 { 6373 struct ctl_lun *lun = CTL_LUN(ctsio); 6374 int pc, page_code, llba, subpage; 6375 int alloc_len, page_len, header_len, bd_len, total_len; 6376 void *block_desc; 6377 struct ctl_page_index *page_index; 6378 6379 llba = 0; 6380 6381 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6382 6383 switch (ctsio->cdb[0]) { 6384 case MODE_SENSE_6: { 6385 struct scsi_mode_sense_6 *cdb; 6386 6387 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6388 6389 header_len = sizeof(struct scsi_mode_hdr_6); 6390 if (cdb->byte2 & SMS_DBD) 6391 bd_len = 0; 6392 else 6393 bd_len = sizeof(struct scsi_mode_block_descr); 6394 header_len += bd_len; 6395 6396 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6397 page_code = cdb->page & SMS_PAGE_CODE; 6398 subpage = cdb->subpage; 6399 alloc_len = cdb->length; 6400 break; 6401 } 6402 case MODE_SENSE_10: { 6403 struct scsi_mode_sense_10 *cdb; 6404 6405 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6406 6407 header_len = sizeof(struct scsi_mode_hdr_10); 6408 if (cdb->byte2 & SMS_DBD) { 6409 bd_len = 0; 6410 } else if (lun->be_lun->lun_type == T_DIRECT) { 6411 if (cdb->byte2 & SMS10_LLBAA) { 6412 llba = 1; 6413 bd_len = sizeof(struct scsi_mode_block_descr_dlong); 6414 } else 6415 bd_len = sizeof(struct scsi_mode_block_descr_dshort); 6416 } else 6417 bd_len = sizeof(struct scsi_mode_block_descr); 6418 header_len += bd_len; 6419 6420 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6421 page_code = cdb->page & SMS_PAGE_CODE; 6422 subpage = cdb->subpage; 6423 alloc_len = scsi_2btoul(cdb->length); 6424 break; 6425 } 6426 default: 6427 ctl_set_invalid_opcode(ctsio); 6428 ctl_done((union ctl_io *)ctsio); 6429 return (CTL_RETVAL_COMPLETE); 6430 break; /* NOTREACHED */ 6431 } 6432 6433 /* 6434 * We have to make a first pass through to calculate the size of 6435 * the pages that match the user's query. Then we allocate enough 6436 * memory to hold it, and actually copy the data into the buffer. 6437 */ 6438 switch (page_code) { 6439 case SMS_ALL_PAGES_PAGE: { 6440 u_int i; 6441 6442 page_len = 0; 6443 6444 /* 6445 * At the moment, values other than 0 and 0xff here are 6446 * reserved according to SPC-3. 6447 */ 6448 if ((subpage != SMS_SUBPAGE_PAGE_0) 6449 && (subpage != SMS_SUBPAGE_ALL)) { 6450 ctl_set_invalid_field(ctsio, 6451 /*sks_valid*/ 1, 6452 /*command*/ 1, 6453 /*field*/ 3, 6454 /*bit_valid*/ 0, 6455 /*bit*/ 0); 6456 ctl_done((union ctl_io *)ctsio); 6457 return (CTL_RETVAL_COMPLETE); 6458 } 6459 6460 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6461 page_index = &lun->mode_pages.index[i]; 6462 6463 /* Make sure the page is supported for this dev type */ 6464 if (lun->be_lun->lun_type == T_DIRECT && 6465 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6466 continue; 6467 if (lun->be_lun->lun_type == T_PROCESSOR && 6468 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6469 continue; 6470 if (lun->be_lun->lun_type == T_CDROM && 6471 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6472 continue; 6473 6474 /* 6475 * We don't use this subpage if the user didn't 6476 * request all subpages. 6477 */ 6478 if ((page_index->subpage != 0) 6479 && (subpage == SMS_SUBPAGE_PAGE_0)) 6480 continue; 6481 6482 page_len += page_index->page_len; 6483 } 6484 break; 6485 } 6486 default: { 6487 u_int i; 6488 6489 page_len = 0; 6490 6491 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6492 page_index = &lun->mode_pages.index[i]; 6493 6494 /* Make sure the page is supported for this dev type */ 6495 if (lun->be_lun->lun_type == T_DIRECT && 6496 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6497 continue; 6498 if (lun->be_lun->lun_type == T_PROCESSOR && 6499 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6500 continue; 6501 if (lun->be_lun->lun_type == T_CDROM && 6502 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6503 continue; 6504 6505 /* Look for the right page code */ 6506 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6507 continue; 6508 6509 /* Look for the right subpage or the subpage wildcard*/ 6510 if ((page_index->subpage != subpage) 6511 && (subpage != SMS_SUBPAGE_ALL)) 6512 continue; 6513 6514 page_len += page_index->page_len; 6515 } 6516 6517 if (page_len == 0) { 6518 ctl_set_invalid_field(ctsio, 6519 /*sks_valid*/ 1, 6520 /*command*/ 1, 6521 /*field*/ 2, 6522 /*bit_valid*/ 1, 6523 /*bit*/ 5); 6524 ctl_done((union ctl_io *)ctsio); 6525 return (CTL_RETVAL_COMPLETE); 6526 } 6527 break; 6528 } 6529 } 6530 6531 total_len = header_len + page_len; 6532 6533 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6534 ctsio->kern_sg_entries = 0; 6535 ctsio->kern_rel_offset = 0; 6536 ctsio->kern_data_len = min(total_len, alloc_len); 6537 ctsio->kern_total_len = ctsio->kern_data_len; 6538 6539 switch (ctsio->cdb[0]) { 6540 case MODE_SENSE_6: { 6541 struct scsi_mode_hdr_6 *header; 6542 6543 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6544 6545 header->datalen = MIN(total_len - 1, 254); 6546 if (lun->be_lun->lun_type == T_DIRECT) { 6547 header->dev_specific = 0x10; /* DPOFUA */ 6548 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6549 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6550 header->dev_specific |= 0x80; /* WP */ 6551 } 6552 header->block_descr_len = bd_len; 6553 block_desc = &header[1]; 6554 break; 6555 } 6556 case MODE_SENSE_10: { 6557 struct scsi_mode_hdr_10 *header; 6558 int datalen; 6559 6560 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6561 6562 datalen = MIN(total_len - 2, 65533); 6563 scsi_ulto2b(datalen, header->datalen); 6564 if (lun->be_lun->lun_type == T_DIRECT) { 6565 header->dev_specific = 0x10; /* DPOFUA */ 6566 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6567 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6568 header->dev_specific |= 0x80; /* WP */ 6569 } 6570 if (llba) 6571 header->flags |= SMH_LONGLBA; 6572 scsi_ulto2b(bd_len, header->block_descr_len); 6573 block_desc = &header[1]; 6574 break; 6575 } 6576 default: 6577 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6578 } 6579 6580 /* 6581 * If we've got a disk, use its blocksize in the block 6582 * descriptor. Otherwise, just set it to 0. 6583 */ 6584 if (bd_len > 0) { 6585 if (lun->be_lun->lun_type == T_DIRECT) { 6586 if (llba) { 6587 struct scsi_mode_block_descr_dlong *bd = block_desc; 6588 if (lun->be_lun->maxlba != 0) 6589 scsi_u64to8b(lun->be_lun->maxlba + 1, 6590 bd->num_blocks); 6591 scsi_ulto4b(lun->be_lun->blocksize, 6592 bd->block_len); 6593 } else { 6594 struct scsi_mode_block_descr_dshort *bd = block_desc; 6595 if (lun->be_lun->maxlba != 0) 6596 scsi_ulto4b(MIN(lun->be_lun->maxlba+1, 6597 UINT32_MAX), bd->num_blocks); 6598 scsi_ulto3b(lun->be_lun->blocksize, 6599 bd->block_len); 6600 } 6601 } else { 6602 struct scsi_mode_block_descr *bd = block_desc; 6603 scsi_ulto3b(0, bd->block_len); 6604 } 6605 } 6606 6607 switch (page_code) { 6608 case SMS_ALL_PAGES_PAGE: { 6609 int i, data_used; 6610 6611 data_used = header_len; 6612 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6613 struct ctl_page_index *page_index; 6614 6615 page_index = &lun->mode_pages.index[i]; 6616 if (lun->be_lun->lun_type == T_DIRECT && 6617 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6618 continue; 6619 if (lun->be_lun->lun_type == T_PROCESSOR && 6620 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6621 continue; 6622 if (lun->be_lun->lun_type == T_CDROM && 6623 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6624 continue; 6625 6626 /* 6627 * We don't use this subpage if the user didn't 6628 * request all subpages. We already checked (above) 6629 * to make sure the user only specified a subpage 6630 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6631 */ 6632 if ((page_index->subpage != 0) 6633 && (subpage == SMS_SUBPAGE_PAGE_0)) 6634 continue; 6635 6636 /* 6637 * Call the handler, if it exists, to update the 6638 * page to the latest values. 6639 */ 6640 if (page_index->sense_handler != NULL) 6641 page_index->sense_handler(ctsio, page_index,pc); 6642 6643 memcpy(ctsio->kern_data_ptr + data_used, 6644 page_index->page_data + 6645 (page_index->page_len * pc), 6646 page_index->page_len); 6647 data_used += page_index->page_len; 6648 } 6649 break; 6650 } 6651 default: { 6652 int i, data_used; 6653 6654 data_used = header_len; 6655 6656 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6657 struct ctl_page_index *page_index; 6658 6659 page_index = &lun->mode_pages.index[i]; 6660 6661 /* Look for the right page code */ 6662 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6663 continue; 6664 6665 /* Look for the right subpage or the subpage wildcard*/ 6666 if ((page_index->subpage != subpage) 6667 && (subpage != SMS_SUBPAGE_ALL)) 6668 continue; 6669 6670 /* Make sure the page is supported for this dev type */ 6671 if (lun->be_lun->lun_type == T_DIRECT && 6672 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6673 continue; 6674 if (lun->be_lun->lun_type == T_PROCESSOR && 6675 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6676 continue; 6677 if (lun->be_lun->lun_type == T_CDROM && 6678 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6679 continue; 6680 6681 /* 6682 * Call the handler, if it exists, to update the 6683 * page to the latest values. 6684 */ 6685 if (page_index->sense_handler != NULL) 6686 page_index->sense_handler(ctsio, page_index,pc); 6687 6688 memcpy(ctsio->kern_data_ptr + data_used, 6689 page_index->page_data + 6690 (page_index->page_len * pc), 6691 page_index->page_len); 6692 data_used += page_index->page_len; 6693 } 6694 break; 6695 } 6696 } 6697 6698 ctl_set_success(ctsio); 6699 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6700 ctsio->be_move_done = ctl_config_move_done; 6701 ctl_datamove((union ctl_io *)ctsio); 6702 return (CTL_RETVAL_COMPLETE); 6703 } 6704 6705 int 6706 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio, 6707 struct ctl_page_index *page_index, 6708 int pc) 6709 { 6710 struct ctl_lun *lun = CTL_LUN(ctsio); 6711 struct scsi_log_temperature *data; 6712 const char *value; 6713 6714 data = (struct scsi_log_temperature *)page_index->page_data; 6715 6716 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code); 6717 data->hdr.param_control = SLP_LBIN; 6718 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6719 sizeof(struct scsi_log_param_header); 6720 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6721 NULL)) != NULL) 6722 data->temperature = strtol(value, NULL, 0); 6723 else 6724 data->temperature = 0xff; 6725 data++; 6726 6727 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code); 6728 data->hdr.param_control = SLP_LBIN; 6729 data->hdr.param_len = sizeof(struct scsi_log_temperature) - 6730 sizeof(struct scsi_log_param_header); 6731 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature", 6732 NULL)) != NULL) 6733 data->temperature = strtol(value, NULL, 0); 6734 else 6735 data->temperature = 0xff; 6736 return (0); 6737 } 6738 6739 int 6740 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6741 struct ctl_page_index *page_index, 6742 int pc) 6743 { 6744 struct ctl_lun *lun = CTL_LUN(ctsio); 6745 struct scsi_log_param_header *phdr; 6746 uint8_t *data; 6747 uint64_t val; 6748 6749 data = page_index->page_data; 6750 6751 if (lun->backend->lun_attr != NULL && 6752 (val = lun->backend->lun_attr(lun->be_lun, "blocksavail")) 6753 != UINT64_MAX) { 6754 phdr = (struct scsi_log_param_header *)data; 6755 scsi_ulto2b(0x0001, phdr->param_code); 6756 phdr->param_control = SLP_LBIN | SLP_LP; 6757 phdr->param_len = 8; 6758 data = (uint8_t *)(phdr + 1); 6759 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6760 data[4] = 0x02; /* per-pool */ 6761 data += phdr->param_len; 6762 } 6763 6764 if (lun->backend->lun_attr != NULL && 6765 (val = lun->backend->lun_attr(lun->be_lun, "blocksused")) 6766 != UINT64_MAX) { 6767 phdr = (struct scsi_log_param_header *)data; 6768 scsi_ulto2b(0x0002, phdr->param_code); 6769 phdr->param_control = SLP_LBIN | SLP_LP; 6770 phdr->param_len = 8; 6771 data = (uint8_t *)(phdr + 1); 6772 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6773 data[4] = 0x01; /* per-LUN */ 6774 data += phdr->param_len; 6775 } 6776 6777 if (lun->backend->lun_attr != NULL && 6778 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksavail")) 6779 != UINT64_MAX) { 6780 phdr = (struct scsi_log_param_header *)data; 6781 scsi_ulto2b(0x00f1, phdr->param_code); 6782 phdr->param_control = SLP_LBIN | SLP_LP; 6783 phdr->param_len = 8; 6784 data = (uint8_t *)(phdr + 1); 6785 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6786 data[4] = 0x02; /* per-pool */ 6787 data += phdr->param_len; 6788 } 6789 6790 if (lun->backend->lun_attr != NULL && 6791 (val = lun->backend->lun_attr(lun->be_lun, "poolblocksused")) 6792 != UINT64_MAX) { 6793 phdr = (struct scsi_log_param_header *)data; 6794 scsi_ulto2b(0x00f2, phdr->param_code); 6795 phdr->param_control = SLP_LBIN | SLP_LP; 6796 phdr->param_len = 8; 6797 data = (uint8_t *)(phdr + 1); 6798 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6799 data[4] = 0x02; /* per-pool */ 6800 data += phdr->param_len; 6801 } 6802 6803 page_index->page_len = data - page_index->page_data; 6804 return (0); 6805 } 6806 6807 int 6808 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6809 struct ctl_page_index *page_index, 6810 int pc) 6811 { 6812 struct ctl_lun *lun = CTL_LUN(ctsio); 6813 struct stat_page *data; 6814 struct bintime *t; 6815 6816 data = (struct stat_page *)page_index->page_data; 6817 6818 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6819 data->sap.hdr.param_control = SLP_LBIN; 6820 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6821 sizeof(struct scsi_log_param_header); 6822 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6823 data->sap.read_num); 6824 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6825 data->sap.write_num); 6826 if (lun->be_lun->blocksize > 0) { 6827 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6828 lun->be_lun->blocksize, data->sap.recvieved_lba); 6829 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6830 lun->be_lun->blocksize, data->sap.transmitted_lba); 6831 } 6832 t = &lun->stats.time[CTL_STATS_READ]; 6833 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6834 data->sap.read_int); 6835 t = &lun->stats.time[CTL_STATS_WRITE]; 6836 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6837 data->sap.write_int); 6838 scsi_u64to8b(0, data->sap.weighted_num); 6839 scsi_u64to8b(0, data->sap.weighted_int); 6840 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6841 data->it.hdr.param_control = SLP_LBIN; 6842 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6843 sizeof(struct scsi_log_param_header); 6844 #ifdef CTL_TIME_IO 6845 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6846 #endif 6847 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6848 data->it.hdr.param_control = SLP_LBIN; 6849 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6850 sizeof(struct scsi_log_param_header); 6851 scsi_ulto4b(3, data->ti.exponent); 6852 scsi_ulto4b(1, data->ti.integer); 6853 return (0); 6854 } 6855 6856 int 6857 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6858 struct ctl_page_index *page_index, 6859 int pc) 6860 { 6861 struct ctl_lun *lun = CTL_LUN(ctsio); 6862 struct scsi_log_informational_exceptions *data; 6863 const char *value; 6864 6865 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6866 6867 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6868 data->hdr.param_control = SLP_LBIN; 6869 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6870 sizeof(struct scsi_log_param_header); 6871 data->ie_asc = lun->ie_asc; 6872 data->ie_ascq = lun->ie_ascq; 6873 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature", 6874 NULL)) != NULL) 6875 data->temperature = strtol(value, NULL, 0); 6876 else 6877 data->temperature = 0xff; 6878 return (0); 6879 } 6880 6881 int 6882 ctl_log_sense(struct ctl_scsiio *ctsio) 6883 { 6884 struct ctl_lun *lun = CTL_LUN(ctsio); 6885 int i, pc, page_code, subpage; 6886 int alloc_len, total_len; 6887 struct ctl_page_index *page_index; 6888 struct scsi_log_sense *cdb; 6889 struct scsi_log_header *header; 6890 6891 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6892 6893 cdb = (struct scsi_log_sense *)ctsio->cdb; 6894 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6895 page_code = cdb->page & SLS_PAGE_CODE; 6896 subpage = cdb->subpage; 6897 alloc_len = scsi_2btoul(cdb->length); 6898 6899 page_index = NULL; 6900 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6901 page_index = &lun->log_pages.index[i]; 6902 6903 /* Look for the right page code */ 6904 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6905 continue; 6906 6907 /* Look for the right subpage or the subpage wildcard*/ 6908 if (page_index->subpage != subpage) 6909 continue; 6910 6911 break; 6912 } 6913 if (i >= CTL_NUM_LOG_PAGES) { 6914 ctl_set_invalid_field(ctsio, 6915 /*sks_valid*/ 1, 6916 /*command*/ 1, 6917 /*field*/ 2, 6918 /*bit_valid*/ 0, 6919 /*bit*/ 0); 6920 ctl_done((union ctl_io *)ctsio); 6921 return (CTL_RETVAL_COMPLETE); 6922 } 6923 6924 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6925 6926 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6927 ctsio->kern_sg_entries = 0; 6928 ctsio->kern_rel_offset = 0; 6929 ctsio->kern_data_len = min(total_len, alloc_len); 6930 ctsio->kern_total_len = ctsio->kern_data_len; 6931 6932 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6933 header->page = page_index->page_code; 6934 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6935 header->page |= SL_DS; 6936 if (page_index->subpage) { 6937 header->page |= SL_SPF; 6938 header->subpage = page_index->subpage; 6939 } 6940 scsi_ulto2b(page_index->page_len, header->datalen); 6941 6942 /* 6943 * Call the handler, if it exists, to update the 6944 * page to the latest values. 6945 */ 6946 if (page_index->sense_handler != NULL) 6947 page_index->sense_handler(ctsio, page_index, pc); 6948 6949 memcpy(header + 1, page_index->page_data, page_index->page_len); 6950 6951 ctl_set_success(ctsio); 6952 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6953 ctsio->be_move_done = ctl_config_move_done; 6954 ctl_datamove((union ctl_io *)ctsio); 6955 return (CTL_RETVAL_COMPLETE); 6956 } 6957 6958 int 6959 ctl_read_capacity(struct ctl_scsiio *ctsio) 6960 { 6961 struct ctl_lun *lun = CTL_LUN(ctsio); 6962 struct scsi_read_capacity *cdb; 6963 struct scsi_read_capacity_data *data; 6964 uint32_t lba; 6965 6966 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6967 6968 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6969 6970 lba = scsi_4btoul(cdb->addr); 6971 if (((cdb->pmi & SRC_PMI) == 0) 6972 && (lba != 0)) { 6973 ctl_set_invalid_field(/*ctsio*/ ctsio, 6974 /*sks_valid*/ 1, 6975 /*command*/ 1, 6976 /*field*/ 2, 6977 /*bit_valid*/ 0, 6978 /*bit*/ 0); 6979 ctl_done((union ctl_io *)ctsio); 6980 return (CTL_RETVAL_COMPLETE); 6981 } 6982 6983 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6984 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6985 ctsio->kern_data_len = sizeof(*data); 6986 ctsio->kern_total_len = sizeof(*data); 6987 ctsio->kern_rel_offset = 0; 6988 ctsio->kern_sg_entries = 0; 6989 6990 /* 6991 * If the maximum LBA is greater than 0xfffffffe, the user must 6992 * issue a SERVICE ACTION IN (16) command, with the read capacity 6993 * serivce action set. 6994 */ 6995 if (lun->be_lun->maxlba > 0xfffffffe) 6996 scsi_ulto4b(0xffffffff, data->addr); 6997 else 6998 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6999 7000 /* 7001 * XXX KDM this may not be 512 bytes... 7002 */ 7003 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7004 7005 ctl_set_success(ctsio); 7006 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7007 ctsio->be_move_done = ctl_config_move_done; 7008 ctl_datamove((union ctl_io *)ctsio); 7009 return (CTL_RETVAL_COMPLETE); 7010 } 7011 7012 int 7013 ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7014 { 7015 struct ctl_lun *lun = CTL_LUN(ctsio); 7016 struct scsi_read_capacity_16 *cdb; 7017 struct scsi_read_capacity_data_long *data; 7018 uint64_t lba; 7019 uint32_t alloc_len; 7020 7021 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7022 7023 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7024 7025 alloc_len = scsi_4btoul(cdb->alloc_len); 7026 lba = scsi_8btou64(cdb->addr); 7027 7028 if ((cdb->reladr & SRC16_PMI) 7029 && (lba != 0)) { 7030 ctl_set_invalid_field(/*ctsio*/ ctsio, 7031 /*sks_valid*/ 1, 7032 /*command*/ 1, 7033 /*field*/ 2, 7034 /*bit_valid*/ 0, 7035 /*bit*/ 0); 7036 ctl_done((union ctl_io *)ctsio); 7037 return (CTL_RETVAL_COMPLETE); 7038 } 7039 7040 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7041 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7042 ctsio->kern_rel_offset = 0; 7043 ctsio->kern_sg_entries = 0; 7044 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 7045 ctsio->kern_total_len = ctsio->kern_data_len; 7046 7047 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7048 /* XXX KDM this may not be 512 bytes... */ 7049 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7050 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7051 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7052 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7053 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7054 7055 ctl_set_success(ctsio); 7056 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7057 ctsio->be_move_done = ctl_config_move_done; 7058 ctl_datamove((union ctl_io *)ctsio); 7059 return (CTL_RETVAL_COMPLETE); 7060 } 7061 7062 int 7063 ctl_get_lba_status(struct ctl_scsiio *ctsio) 7064 { 7065 struct ctl_lun *lun = CTL_LUN(ctsio); 7066 struct scsi_get_lba_status *cdb; 7067 struct scsi_get_lba_status_data *data; 7068 struct ctl_lba_len_flags *lbalen; 7069 uint64_t lba; 7070 uint32_t alloc_len, total_len; 7071 int retval; 7072 7073 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7074 7075 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7076 lba = scsi_8btou64(cdb->addr); 7077 alloc_len = scsi_4btoul(cdb->alloc_len); 7078 7079 if (lba > lun->be_lun->maxlba) { 7080 ctl_set_lba_out_of_range(ctsio, lba); 7081 ctl_done((union ctl_io *)ctsio); 7082 return (CTL_RETVAL_COMPLETE); 7083 } 7084 7085 total_len = sizeof(*data) + sizeof(data->descr[0]); 7086 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7087 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7088 ctsio->kern_rel_offset = 0; 7089 ctsio->kern_sg_entries = 0; 7090 ctsio->kern_data_len = min(total_len, alloc_len); 7091 ctsio->kern_total_len = ctsio->kern_data_len; 7092 7093 /* Fill dummy data in case backend can't tell anything. */ 7094 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7095 scsi_u64to8b(lba, data->descr[0].addr); 7096 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7097 data->descr[0].length); 7098 data->descr[0].status = 0; /* Mapped or unknown. */ 7099 7100 ctl_set_success(ctsio); 7101 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7102 ctsio->be_move_done = ctl_config_move_done; 7103 7104 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7105 lbalen->lba = lba; 7106 lbalen->len = total_len; 7107 lbalen->flags = 0; 7108 retval = lun->backend->config_read((union ctl_io *)ctsio); 7109 return (retval); 7110 } 7111 7112 int 7113 ctl_read_defect(struct ctl_scsiio *ctsio) 7114 { 7115 struct scsi_read_defect_data_10 *ccb10; 7116 struct scsi_read_defect_data_12 *ccb12; 7117 struct scsi_read_defect_data_hdr_10 *data10; 7118 struct scsi_read_defect_data_hdr_12 *data12; 7119 uint32_t alloc_len, data_len; 7120 uint8_t format; 7121 7122 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7123 7124 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7125 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7126 format = ccb10->format; 7127 alloc_len = scsi_2btoul(ccb10->alloc_length); 7128 data_len = sizeof(*data10); 7129 } else { 7130 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7131 format = ccb12->format; 7132 alloc_len = scsi_4btoul(ccb12->alloc_length); 7133 data_len = sizeof(*data12); 7134 } 7135 if (alloc_len == 0) { 7136 ctl_set_success(ctsio); 7137 ctl_done((union ctl_io *)ctsio); 7138 return (CTL_RETVAL_COMPLETE); 7139 } 7140 7141 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7142 ctsio->kern_rel_offset = 0; 7143 ctsio->kern_sg_entries = 0; 7144 ctsio->kern_data_len = min(data_len, alloc_len); 7145 ctsio->kern_total_len = ctsio->kern_data_len; 7146 7147 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7148 data10 = (struct scsi_read_defect_data_hdr_10 *) 7149 ctsio->kern_data_ptr; 7150 data10->format = format; 7151 scsi_ulto2b(0, data10->length); 7152 } else { 7153 data12 = (struct scsi_read_defect_data_hdr_12 *) 7154 ctsio->kern_data_ptr; 7155 data12->format = format; 7156 scsi_ulto2b(0, data12->generation); 7157 scsi_ulto4b(0, data12->length); 7158 } 7159 7160 ctl_set_success(ctsio); 7161 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7162 ctsio->be_move_done = ctl_config_move_done; 7163 ctl_datamove((union ctl_io *)ctsio); 7164 return (CTL_RETVAL_COMPLETE); 7165 } 7166 7167 int 7168 ctl_report_ident_info(struct ctl_scsiio *ctsio) 7169 { 7170 struct ctl_lun *lun = CTL_LUN(ctsio); 7171 struct scsi_report_ident_info *cdb; 7172 struct scsi_report_ident_info_data *rii_ptr; 7173 struct scsi_report_ident_info_descr *riid_ptr; 7174 const char *oii, *otii; 7175 int retval, alloc_len, total_len = 0, len = 0; 7176 7177 CTL_DEBUG_PRINT(("ctl_report_ident_info\n")); 7178 7179 cdb = (struct scsi_report_ident_info *)ctsio->cdb; 7180 retval = CTL_RETVAL_COMPLETE; 7181 7182 total_len = sizeof(struct scsi_report_ident_info_data); 7183 switch (cdb->type) { 7184 case RII_LUII: 7185 oii = dnvlist_get_string(lun->be_lun->options, 7186 "ident_info", NULL); 7187 if (oii) 7188 len = strlen(oii); /* Approximately */ 7189 break; 7190 case RII_LUTII: 7191 otii = dnvlist_get_string(lun->be_lun->options, 7192 "text_ident_info", NULL); 7193 if (otii) 7194 len = strlen(otii) + 1; /* NULL-terminated */ 7195 break; 7196 case RII_IIS: 7197 len = 2 * sizeof(struct scsi_report_ident_info_descr); 7198 break; 7199 default: 7200 ctl_set_invalid_field(/*ctsio*/ ctsio, 7201 /*sks_valid*/ 1, 7202 /*command*/ 1, 7203 /*field*/ 11, 7204 /*bit_valid*/ 1, 7205 /*bit*/ 2); 7206 ctl_done((union ctl_io *)ctsio); 7207 return(retval); 7208 } 7209 total_len += len; 7210 alloc_len = scsi_4btoul(cdb->length); 7211 7212 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7213 ctsio->kern_sg_entries = 0; 7214 ctsio->kern_rel_offset = 0; 7215 ctsio->kern_data_len = min(total_len, alloc_len); 7216 ctsio->kern_total_len = ctsio->kern_data_len; 7217 7218 rii_ptr = (struct scsi_report_ident_info_data *)ctsio->kern_data_ptr; 7219 switch (cdb->type) { 7220 case RII_LUII: 7221 if (oii) { 7222 if (oii[0] == '0' && oii[1] == 'x') 7223 len = hex2bin(oii, (uint8_t *)(rii_ptr + 1), len); 7224 else 7225 strncpy((uint8_t *)(rii_ptr + 1), oii, len); 7226 } 7227 break; 7228 case RII_LUTII: 7229 if (otii) 7230 strlcpy((uint8_t *)(rii_ptr + 1), otii, len); 7231 break; 7232 case RII_IIS: 7233 riid_ptr = (struct scsi_report_ident_info_descr *)(rii_ptr + 1); 7234 riid_ptr->type = RII_LUII; 7235 scsi_ulto2b(0xffff, riid_ptr->length); 7236 riid_ptr++; 7237 riid_ptr->type = RII_LUTII; 7238 scsi_ulto2b(0xffff, riid_ptr->length); 7239 } 7240 scsi_ulto2b(len, rii_ptr->length); 7241 7242 ctl_set_success(ctsio); 7243 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7244 ctsio->be_move_done = ctl_config_move_done; 7245 ctl_datamove((union ctl_io *)ctsio); 7246 return(retval); 7247 } 7248 7249 int 7250 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7251 { 7252 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7253 struct ctl_lun *lun = CTL_LUN(ctsio); 7254 struct scsi_maintenance_in *cdb; 7255 int retval; 7256 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7257 int num_ha_groups, num_target_ports, shared_group; 7258 struct ctl_port *port; 7259 struct scsi_target_group_data *rtg_ptr; 7260 struct scsi_target_group_data_extended *rtg_ext_ptr; 7261 struct scsi_target_port_group_descriptor *tpg_desc; 7262 7263 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7264 7265 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7266 retval = CTL_RETVAL_COMPLETE; 7267 7268 switch (cdb->byte2 & STG_PDF_MASK) { 7269 case STG_PDF_LENGTH: 7270 ext = 0; 7271 break; 7272 case STG_PDF_EXTENDED: 7273 ext = 1; 7274 break; 7275 default: 7276 ctl_set_invalid_field(/*ctsio*/ ctsio, 7277 /*sks_valid*/ 1, 7278 /*command*/ 1, 7279 /*field*/ 2, 7280 /*bit_valid*/ 1, 7281 /*bit*/ 5); 7282 ctl_done((union ctl_io *)ctsio); 7283 return(retval); 7284 } 7285 7286 num_target_ports = 0; 7287 shared_group = (softc->is_single != 0); 7288 mtx_lock(&softc->ctl_lock); 7289 STAILQ_FOREACH(port, &softc->port_list, links) { 7290 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7291 continue; 7292 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7293 continue; 7294 num_target_ports++; 7295 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7296 shared_group = 1; 7297 } 7298 mtx_unlock(&softc->ctl_lock); 7299 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7300 7301 if (ext) 7302 total_len = sizeof(struct scsi_target_group_data_extended); 7303 else 7304 total_len = sizeof(struct scsi_target_group_data); 7305 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7306 (shared_group + num_ha_groups) + 7307 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7308 7309 alloc_len = scsi_4btoul(cdb->length); 7310 7311 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7312 ctsio->kern_sg_entries = 0; 7313 ctsio->kern_rel_offset = 0; 7314 ctsio->kern_data_len = min(total_len, alloc_len); 7315 ctsio->kern_total_len = ctsio->kern_data_len; 7316 7317 if (ext) { 7318 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7319 ctsio->kern_data_ptr; 7320 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7321 rtg_ext_ptr->format_type = 0x10; 7322 rtg_ext_ptr->implicit_transition_time = 0; 7323 tpg_desc = &rtg_ext_ptr->groups[0]; 7324 } else { 7325 rtg_ptr = (struct scsi_target_group_data *) 7326 ctsio->kern_data_ptr; 7327 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7328 tpg_desc = &rtg_ptr->groups[0]; 7329 } 7330 7331 mtx_lock(&softc->ctl_lock); 7332 pg = softc->port_min / softc->port_cnt; 7333 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7334 /* Some shelf is known to be primary. */ 7335 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7336 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7337 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7338 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7339 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7340 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7341 else 7342 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7343 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7344 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7345 } else { 7346 ts = os; 7347 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7348 } 7349 } else { 7350 /* No known primary shelf. */ 7351 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7352 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7353 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7354 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7355 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7356 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7357 } else { 7358 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7359 } 7360 } 7361 if (shared_group) { 7362 tpg_desc->pref_state = ts; 7363 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7364 TPG_U_SUP | TPG_T_SUP; 7365 scsi_ulto2b(1, tpg_desc->target_port_group); 7366 tpg_desc->status = TPG_IMPLICIT; 7367 pc = 0; 7368 STAILQ_FOREACH(port, &softc->port_list, links) { 7369 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7370 continue; 7371 if (!softc->is_single && 7372 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7373 continue; 7374 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7375 continue; 7376 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7377 relative_target_port_identifier); 7378 pc++; 7379 } 7380 tpg_desc->target_port_count = pc; 7381 tpg_desc = (struct scsi_target_port_group_descriptor *) 7382 &tpg_desc->descriptors[pc]; 7383 } 7384 for (g = 0; g < num_ha_groups; g++) { 7385 tpg_desc->pref_state = (g == pg) ? ts : os; 7386 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7387 TPG_U_SUP | TPG_T_SUP; 7388 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7389 tpg_desc->status = TPG_IMPLICIT; 7390 pc = 0; 7391 STAILQ_FOREACH(port, &softc->port_list, links) { 7392 if (port->targ_port < g * softc->port_cnt || 7393 port->targ_port >= (g + 1) * softc->port_cnt) 7394 continue; 7395 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7396 continue; 7397 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7398 continue; 7399 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7400 continue; 7401 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7402 relative_target_port_identifier); 7403 pc++; 7404 } 7405 tpg_desc->target_port_count = pc; 7406 tpg_desc = (struct scsi_target_port_group_descriptor *) 7407 &tpg_desc->descriptors[pc]; 7408 } 7409 mtx_unlock(&softc->ctl_lock); 7410 7411 ctl_set_success(ctsio); 7412 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7413 ctsio->be_move_done = ctl_config_move_done; 7414 ctl_datamove((union ctl_io *)ctsio); 7415 return(retval); 7416 } 7417 7418 int 7419 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7420 { 7421 struct ctl_lun *lun = CTL_LUN(ctsio); 7422 struct scsi_report_supported_opcodes *cdb; 7423 const struct ctl_cmd_entry *entry, *sentry; 7424 struct scsi_report_supported_opcodes_all *all; 7425 struct scsi_report_supported_opcodes_descr *descr; 7426 struct scsi_report_supported_opcodes_one *one; 7427 int retval; 7428 int alloc_len, total_len; 7429 int opcode, service_action, i, j, num; 7430 7431 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7432 7433 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7434 retval = CTL_RETVAL_COMPLETE; 7435 7436 opcode = cdb->requested_opcode; 7437 service_action = scsi_2btoul(cdb->requested_service_action); 7438 switch (cdb->options & RSO_OPTIONS_MASK) { 7439 case RSO_OPTIONS_ALL: 7440 num = 0; 7441 for (i = 0; i < 256; i++) { 7442 entry = &ctl_cmd_table[i]; 7443 if (entry->flags & CTL_CMD_FLAG_SA5) { 7444 for (j = 0; j < 32; j++) { 7445 sentry = &((const struct ctl_cmd_entry *) 7446 entry->execute)[j]; 7447 if (ctl_cmd_applicable( 7448 lun->be_lun->lun_type, sentry)) 7449 num++; 7450 } 7451 } else { 7452 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7453 entry)) 7454 num++; 7455 } 7456 } 7457 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7458 num * sizeof(struct scsi_report_supported_opcodes_descr); 7459 break; 7460 case RSO_OPTIONS_OC: 7461 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7462 ctl_set_invalid_field(/*ctsio*/ ctsio, 7463 /*sks_valid*/ 1, 7464 /*command*/ 1, 7465 /*field*/ 2, 7466 /*bit_valid*/ 1, 7467 /*bit*/ 2); 7468 ctl_done((union ctl_io *)ctsio); 7469 return (CTL_RETVAL_COMPLETE); 7470 } 7471 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7472 break; 7473 case RSO_OPTIONS_OC_SA: 7474 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7475 service_action >= 32) { 7476 ctl_set_invalid_field(/*ctsio*/ ctsio, 7477 /*sks_valid*/ 1, 7478 /*command*/ 1, 7479 /*field*/ 2, 7480 /*bit_valid*/ 1, 7481 /*bit*/ 2); 7482 ctl_done((union ctl_io *)ctsio); 7483 return (CTL_RETVAL_COMPLETE); 7484 } 7485 /* FALLTHROUGH */ 7486 case RSO_OPTIONS_OC_ASA: 7487 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7488 break; 7489 default: 7490 ctl_set_invalid_field(/*ctsio*/ ctsio, 7491 /*sks_valid*/ 1, 7492 /*command*/ 1, 7493 /*field*/ 2, 7494 /*bit_valid*/ 1, 7495 /*bit*/ 2); 7496 ctl_done((union ctl_io *)ctsio); 7497 return (CTL_RETVAL_COMPLETE); 7498 } 7499 7500 alloc_len = scsi_4btoul(cdb->length); 7501 7502 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7503 ctsio->kern_sg_entries = 0; 7504 ctsio->kern_rel_offset = 0; 7505 ctsio->kern_data_len = min(total_len, alloc_len); 7506 ctsio->kern_total_len = ctsio->kern_data_len; 7507 7508 switch (cdb->options & RSO_OPTIONS_MASK) { 7509 case RSO_OPTIONS_ALL: 7510 all = (struct scsi_report_supported_opcodes_all *) 7511 ctsio->kern_data_ptr; 7512 num = 0; 7513 for (i = 0; i < 256; i++) { 7514 entry = &ctl_cmd_table[i]; 7515 if (entry->flags & CTL_CMD_FLAG_SA5) { 7516 for (j = 0; j < 32; j++) { 7517 sentry = &((const struct ctl_cmd_entry *) 7518 entry->execute)[j]; 7519 if (!ctl_cmd_applicable( 7520 lun->be_lun->lun_type, sentry)) 7521 continue; 7522 descr = &all->descr[num++]; 7523 descr->opcode = i; 7524 scsi_ulto2b(j, descr->service_action); 7525 descr->flags = RSO_SERVACTV; 7526 scsi_ulto2b(sentry->length, 7527 descr->cdb_length); 7528 } 7529 } else { 7530 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7531 entry)) 7532 continue; 7533 descr = &all->descr[num++]; 7534 descr->opcode = i; 7535 scsi_ulto2b(0, descr->service_action); 7536 descr->flags = 0; 7537 scsi_ulto2b(entry->length, descr->cdb_length); 7538 } 7539 } 7540 scsi_ulto4b( 7541 num * sizeof(struct scsi_report_supported_opcodes_descr), 7542 all->length); 7543 break; 7544 case RSO_OPTIONS_OC: 7545 one = (struct scsi_report_supported_opcodes_one *) 7546 ctsio->kern_data_ptr; 7547 entry = &ctl_cmd_table[opcode]; 7548 goto fill_one; 7549 case RSO_OPTIONS_OC_SA: 7550 one = (struct scsi_report_supported_opcodes_one *) 7551 ctsio->kern_data_ptr; 7552 entry = &ctl_cmd_table[opcode]; 7553 entry = &((const struct ctl_cmd_entry *) 7554 entry->execute)[service_action]; 7555 fill_one: 7556 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7557 one->support = 3; 7558 scsi_ulto2b(entry->length, one->cdb_length); 7559 one->cdb_usage[0] = opcode; 7560 memcpy(&one->cdb_usage[1], entry->usage, 7561 entry->length - 1); 7562 } else 7563 one->support = 1; 7564 break; 7565 case RSO_OPTIONS_OC_ASA: 7566 one = (struct scsi_report_supported_opcodes_one *) 7567 ctsio->kern_data_ptr; 7568 entry = &ctl_cmd_table[opcode]; 7569 if (entry->flags & CTL_CMD_FLAG_SA5) { 7570 entry = &((const struct ctl_cmd_entry *) 7571 entry->execute)[service_action]; 7572 } else if (service_action != 0) { 7573 one->support = 1; 7574 break; 7575 } 7576 goto fill_one; 7577 } 7578 7579 ctl_set_success(ctsio); 7580 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7581 ctsio->be_move_done = ctl_config_move_done; 7582 ctl_datamove((union ctl_io *)ctsio); 7583 return(retval); 7584 } 7585 7586 int 7587 ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7588 { 7589 struct scsi_report_supported_tmf *cdb; 7590 struct scsi_report_supported_tmf_ext_data *data; 7591 int retval; 7592 int alloc_len, total_len; 7593 7594 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7595 7596 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7597 7598 retval = CTL_RETVAL_COMPLETE; 7599 7600 if (cdb->options & RST_REPD) 7601 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7602 else 7603 total_len = sizeof(struct scsi_report_supported_tmf_data); 7604 alloc_len = scsi_4btoul(cdb->length); 7605 7606 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7607 ctsio->kern_sg_entries = 0; 7608 ctsio->kern_rel_offset = 0; 7609 ctsio->kern_data_len = min(total_len, alloc_len); 7610 ctsio->kern_total_len = ctsio->kern_data_len; 7611 7612 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7613 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7614 RST_TRS; 7615 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7616 data->length = total_len - 4; 7617 7618 ctl_set_success(ctsio); 7619 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7620 ctsio->be_move_done = ctl_config_move_done; 7621 ctl_datamove((union ctl_io *)ctsio); 7622 return (retval); 7623 } 7624 7625 int 7626 ctl_report_timestamp(struct ctl_scsiio *ctsio) 7627 { 7628 struct scsi_report_timestamp *cdb; 7629 struct scsi_report_timestamp_data *data; 7630 struct timeval tv; 7631 int64_t timestamp; 7632 int retval; 7633 int alloc_len, total_len; 7634 7635 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7636 7637 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7638 7639 retval = CTL_RETVAL_COMPLETE; 7640 7641 total_len = sizeof(struct scsi_report_timestamp_data); 7642 alloc_len = scsi_4btoul(cdb->length); 7643 7644 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7645 ctsio->kern_sg_entries = 0; 7646 ctsio->kern_rel_offset = 0; 7647 ctsio->kern_data_len = min(total_len, alloc_len); 7648 ctsio->kern_total_len = ctsio->kern_data_len; 7649 7650 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7651 scsi_ulto2b(sizeof(*data) - 2, data->length); 7652 data->origin = RTS_ORIG_OUTSIDE; 7653 getmicrotime(&tv); 7654 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7655 scsi_ulto4b(timestamp >> 16, data->timestamp); 7656 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7657 7658 ctl_set_success(ctsio); 7659 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7660 ctsio->be_move_done = ctl_config_move_done; 7661 ctl_datamove((union ctl_io *)ctsio); 7662 return (retval); 7663 } 7664 7665 int 7666 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7667 { 7668 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7669 struct ctl_lun *lun = CTL_LUN(ctsio); 7670 struct scsi_per_res_in *cdb; 7671 int alloc_len, total_len = 0; 7672 /* struct scsi_per_res_in_rsrv in_data; */ 7673 uint64_t key; 7674 7675 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7676 7677 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7678 7679 alloc_len = scsi_2btoul(cdb->length); 7680 7681 retry: 7682 mtx_lock(&lun->lun_lock); 7683 switch (cdb->action) { 7684 case SPRI_RK: /* read keys */ 7685 total_len = sizeof(struct scsi_per_res_in_keys) + 7686 lun->pr_key_count * 7687 sizeof(struct scsi_per_res_key); 7688 break; 7689 case SPRI_RR: /* read reservation */ 7690 if (lun->flags & CTL_LUN_PR_RESERVED) 7691 total_len = sizeof(struct scsi_per_res_in_rsrv); 7692 else 7693 total_len = sizeof(struct scsi_per_res_in_header); 7694 break; 7695 case SPRI_RC: /* report capabilities */ 7696 total_len = sizeof(struct scsi_per_res_cap); 7697 break; 7698 case SPRI_RS: /* read full status */ 7699 total_len = sizeof(struct scsi_per_res_in_header) + 7700 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7701 lun->pr_key_count; 7702 break; 7703 default: 7704 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7705 } 7706 mtx_unlock(&lun->lun_lock); 7707 7708 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7709 ctsio->kern_rel_offset = 0; 7710 ctsio->kern_sg_entries = 0; 7711 ctsio->kern_data_len = min(total_len, alloc_len); 7712 ctsio->kern_total_len = ctsio->kern_data_len; 7713 7714 mtx_lock(&lun->lun_lock); 7715 switch (cdb->action) { 7716 case SPRI_RK: { // read keys 7717 struct scsi_per_res_in_keys *res_keys; 7718 int i, key_count; 7719 7720 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7721 7722 /* 7723 * We had to drop the lock to allocate our buffer, which 7724 * leaves time for someone to come in with another 7725 * persistent reservation. (That is unlikely, though, 7726 * since this should be the only persistent reservation 7727 * command active right now.) 7728 */ 7729 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7730 (lun->pr_key_count * 7731 sizeof(struct scsi_per_res_key)))){ 7732 mtx_unlock(&lun->lun_lock); 7733 free(ctsio->kern_data_ptr, M_CTL); 7734 printf("%s: reservation length changed, retrying\n", 7735 __func__); 7736 goto retry; 7737 } 7738 7739 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7740 7741 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7742 lun->pr_key_count, res_keys->header.length); 7743 7744 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7745 if ((key = ctl_get_prkey(lun, i)) == 0) 7746 continue; 7747 7748 /* 7749 * We used lun->pr_key_count to calculate the 7750 * size to allocate. If it turns out the number of 7751 * initiators with the registered flag set is 7752 * larger than that (i.e. they haven't been kept in 7753 * sync), we've got a problem. 7754 */ 7755 if (key_count >= lun->pr_key_count) { 7756 key_count++; 7757 continue; 7758 } 7759 scsi_u64to8b(key, res_keys->keys[key_count].key); 7760 key_count++; 7761 } 7762 break; 7763 } 7764 case SPRI_RR: { // read reservation 7765 struct scsi_per_res_in_rsrv *res; 7766 int tmp_len, header_only; 7767 7768 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7769 7770 scsi_ulto4b(lun->pr_generation, res->header.generation); 7771 7772 if (lun->flags & CTL_LUN_PR_RESERVED) 7773 { 7774 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7775 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7776 res->header.length); 7777 header_only = 0; 7778 } else { 7779 tmp_len = sizeof(struct scsi_per_res_in_header); 7780 scsi_ulto4b(0, res->header.length); 7781 header_only = 1; 7782 } 7783 7784 /* 7785 * We had to drop the lock to allocate our buffer, which 7786 * leaves time for someone to come in with another 7787 * persistent reservation. (That is unlikely, though, 7788 * since this should be the only persistent reservation 7789 * command active right now.) 7790 */ 7791 if (tmp_len != total_len) { 7792 mtx_unlock(&lun->lun_lock); 7793 free(ctsio->kern_data_ptr, M_CTL); 7794 printf("%s: reservation status changed, retrying\n", 7795 __func__); 7796 goto retry; 7797 } 7798 7799 /* 7800 * No reservation held, so we're done. 7801 */ 7802 if (header_only != 0) 7803 break; 7804 7805 /* 7806 * If the registration is an All Registrants type, the key 7807 * is 0, since it doesn't really matter. 7808 */ 7809 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7810 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7811 res->data.reservation); 7812 } 7813 res->data.scopetype = lun->pr_res_type; 7814 break; 7815 } 7816 case SPRI_RC: //report capabilities 7817 { 7818 struct scsi_per_res_cap *res_cap; 7819 uint16_t type_mask; 7820 7821 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7822 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7823 res_cap->flags1 = SPRI_CRH; 7824 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7825 type_mask = SPRI_TM_WR_EX_AR | 7826 SPRI_TM_EX_AC_RO | 7827 SPRI_TM_WR_EX_RO | 7828 SPRI_TM_EX_AC | 7829 SPRI_TM_WR_EX | 7830 SPRI_TM_EX_AC_AR; 7831 scsi_ulto2b(type_mask, res_cap->type_mask); 7832 break; 7833 } 7834 case SPRI_RS: { // read full status 7835 struct scsi_per_res_in_full *res_status; 7836 struct scsi_per_res_in_full_desc *res_desc; 7837 struct ctl_port *port; 7838 int i, len; 7839 7840 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7841 7842 /* 7843 * We had to drop the lock to allocate our buffer, which 7844 * leaves time for someone to come in with another 7845 * persistent reservation. (That is unlikely, though, 7846 * since this should be the only persistent reservation 7847 * command active right now.) 7848 */ 7849 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7850 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7851 lun->pr_key_count)){ 7852 mtx_unlock(&lun->lun_lock); 7853 free(ctsio->kern_data_ptr, M_CTL); 7854 printf("%s: reservation length changed, retrying\n", 7855 __func__); 7856 goto retry; 7857 } 7858 7859 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7860 7861 res_desc = &res_status->desc[0]; 7862 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7863 if ((key = ctl_get_prkey(lun, i)) == 0) 7864 continue; 7865 7866 scsi_u64to8b(key, res_desc->res_key.key); 7867 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7868 (lun->pr_res_idx == i || 7869 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7870 res_desc->flags = SPRI_FULL_R_HOLDER; 7871 res_desc->scopetype = lun->pr_res_type; 7872 } 7873 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7874 res_desc->rel_trgt_port_id); 7875 len = 0; 7876 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7877 if (port != NULL) 7878 len = ctl_create_iid(port, 7879 i % CTL_MAX_INIT_PER_PORT, 7880 res_desc->transport_id); 7881 scsi_ulto4b(len, res_desc->additional_length); 7882 res_desc = (struct scsi_per_res_in_full_desc *) 7883 &res_desc->transport_id[len]; 7884 } 7885 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7886 res_status->header.length); 7887 break; 7888 } 7889 default: 7890 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7891 } 7892 mtx_unlock(&lun->lun_lock); 7893 7894 ctl_set_success(ctsio); 7895 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7896 ctsio->be_move_done = ctl_config_move_done; 7897 ctl_datamove((union ctl_io *)ctsio); 7898 return (CTL_RETVAL_COMPLETE); 7899 } 7900 7901 /* 7902 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7903 * it should return. 7904 */ 7905 static int 7906 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7907 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7908 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7909 struct scsi_per_res_out_parms* param) 7910 { 7911 union ctl_ha_msg persis_io; 7912 int i; 7913 7914 mtx_lock(&lun->lun_lock); 7915 if (sa_res_key == 0) { 7916 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7917 /* validate scope and type */ 7918 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7919 SPR_LU_SCOPE) { 7920 mtx_unlock(&lun->lun_lock); 7921 ctl_set_invalid_field(/*ctsio*/ ctsio, 7922 /*sks_valid*/ 1, 7923 /*command*/ 1, 7924 /*field*/ 2, 7925 /*bit_valid*/ 1, 7926 /*bit*/ 4); 7927 ctl_done((union ctl_io *)ctsio); 7928 return (1); 7929 } 7930 7931 if (type>8 || type==2 || type==4 || type==0) { 7932 mtx_unlock(&lun->lun_lock); 7933 ctl_set_invalid_field(/*ctsio*/ ctsio, 7934 /*sks_valid*/ 1, 7935 /*command*/ 1, 7936 /*field*/ 2, 7937 /*bit_valid*/ 1, 7938 /*bit*/ 0); 7939 ctl_done((union ctl_io *)ctsio); 7940 return (1); 7941 } 7942 7943 /* 7944 * Unregister everybody else and build UA for 7945 * them 7946 */ 7947 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7948 if (i == residx || ctl_get_prkey(lun, i) == 0) 7949 continue; 7950 7951 ctl_clr_prkey(lun, i); 7952 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7953 } 7954 lun->pr_key_count = 1; 7955 lun->pr_res_type = type; 7956 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7957 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7958 lun->pr_res_idx = residx; 7959 lun->pr_generation++; 7960 mtx_unlock(&lun->lun_lock); 7961 7962 /* send msg to other side */ 7963 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7964 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7965 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7966 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7967 persis_io.pr.pr_info.res_type = type; 7968 memcpy(persis_io.pr.pr_info.sa_res_key, 7969 param->serv_act_res_key, 7970 sizeof(param->serv_act_res_key)); 7971 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7972 sizeof(persis_io.pr), M_WAITOK); 7973 } else { 7974 /* not all registrants */ 7975 mtx_unlock(&lun->lun_lock); 7976 free(ctsio->kern_data_ptr, M_CTL); 7977 ctl_set_invalid_field(ctsio, 7978 /*sks_valid*/ 1, 7979 /*command*/ 0, 7980 /*field*/ 8, 7981 /*bit_valid*/ 0, 7982 /*bit*/ 0); 7983 ctl_done((union ctl_io *)ctsio); 7984 return (1); 7985 } 7986 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7987 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7988 int found = 0; 7989 7990 if (res_key == sa_res_key) { 7991 /* special case */ 7992 /* 7993 * The spec implies this is not good but doesn't 7994 * say what to do. There are two choices either 7995 * generate a res conflict or check condition 7996 * with illegal field in parameter data. Since 7997 * that is what is done when the sa_res_key is 7998 * zero I'll take that approach since this has 7999 * to do with the sa_res_key. 8000 */ 8001 mtx_unlock(&lun->lun_lock); 8002 free(ctsio->kern_data_ptr, M_CTL); 8003 ctl_set_invalid_field(ctsio, 8004 /*sks_valid*/ 1, 8005 /*command*/ 0, 8006 /*field*/ 8, 8007 /*bit_valid*/ 0, 8008 /*bit*/ 0); 8009 ctl_done((union ctl_io *)ctsio); 8010 return (1); 8011 } 8012 8013 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8014 if (ctl_get_prkey(lun, i) != sa_res_key) 8015 continue; 8016 8017 found = 1; 8018 ctl_clr_prkey(lun, i); 8019 lun->pr_key_count--; 8020 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8021 } 8022 if (!found) { 8023 mtx_unlock(&lun->lun_lock); 8024 free(ctsio->kern_data_ptr, M_CTL); 8025 ctl_set_reservation_conflict(ctsio); 8026 ctl_done((union ctl_io *)ctsio); 8027 return (CTL_RETVAL_COMPLETE); 8028 } 8029 lun->pr_generation++; 8030 mtx_unlock(&lun->lun_lock); 8031 8032 /* send msg to other side */ 8033 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8034 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8035 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8036 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8037 persis_io.pr.pr_info.res_type = type; 8038 memcpy(persis_io.pr.pr_info.sa_res_key, 8039 param->serv_act_res_key, 8040 sizeof(param->serv_act_res_key)); 8041 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8042 sizeof(persis_io.pr), M_WAITOK); 8043 } else { 8044 /* Reserved but not all registrants */ 8045 /* sa_res_key is res holder */ 8046 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8047 /* validate scope and type */ 8048 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8049 SPR_LU_SCOPE) { 8050 mtx_unlock(&lun->lun_lock); 8051 ctl_set_invalid_field(/*ctsio*/ ctsio, 8052 /*sks_valid*/ 1, 8053 /*command*/ 1, 8054 /*field*/ 2, 8055 /*bit_valid*/ 1, 8056 /*bit*/ 4); 8057 ctl_done((union ctl_io *)ctsio); 8058 return (1); 8059 } 8060 8061 if (type>8 || type==2 || type==4 || type==0) { 8062 mtx_unlock(&lun->lun_lock); 8063 ctl_set_invalid_field(/*ctsio*/ ctsio, 8064 /*sks_valid*/ 1, 8065 /*command*/ 1, 8066 /*field*/ 2, 8067 /*bit_valid*/ 1, 8068 /*bit*/ 0); 8069 ctl_done((union ctl_io *)ctsio); 8070 return (1); 8071 } 8072 8073 /* 8074 * Do the following: 8075 * if sa_res_key != res_key remove all 8076 * registrants w/sa_res_key and generate UA 8077 * for these registrants(Registrations 8078 * Preempted) if it wasn't an exclusive 8079 * reservation generate UA(Reservations 8080 * Preempted) for all other registered nexuses 8081 * if the type has changed. Establish the new 8082 * reservation and holder. If res_key and 8083 * sa_res_key are the same do the above 8084 * except don't unregister the res holder. 8085 */ 8086 8087 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8088 if (i == residx || ctl_get_prkey(lun, i) == 0) 8089 continue; 8090 8091 if (sa_res_key == ctl_get_prkey(lun, i)) { 8092 ctl_clr_prkey(lun, i); 8093 lun->pr_key_count--; 8094 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8095 } else if (type != lun->pr_res_type && 8096 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8097 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8098 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8099 } 8100 } 8101 lun->pr_res_type = type; 8102 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8103 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8104 lun->pr_res_idx = residx; 8105 else 8106 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8107 lun->pr_generation++; 8108 mtx_unlock(&lun->lun_lock); 8109 8110 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8111 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8112 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8113 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8114 persis_io.pr.pr_info.res_type = type; 8115 memcpy(persis_io.pr.pr_info.sa_res_key, 8116 param->serv_act_res_key, 8117 sizeof(param->serv_act_res_key)); 8118 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8119 sizeof(persis_io.pr), M_WAITOK); 8120 } else { 8121 /* 8122 * sa_res_key is not the res holder just 8123 * remove registrants 8124 */ 8125 int found=0; 8126 8127 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8128 if (sa_res_key != ctl_get_prkey(lun, i)) 8129 continue; 8130 8131 found = 1; 8132 ctl_clr_prkey(lun, i); 8133 lun->pr_key_count--; 8134 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8135 } 8136 8137 if (!found) { 8138 mtx_unlock(&lun->lun_lock); 8139 free(ctsio->kern_data_ptr, M_CTL); 8140 ctl_set_reservation_conflict(ctsio); 8141 ctl_done((union ctl_io *)ctsio); 8142 return (1); 8143 } 8144 lun->pr_generation++; 8145 mtx_unlock(&lun->lun_lock); 8146 8147 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8148 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8149 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8150 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8151 persis_io.pr.pr_info.res_type = type; 8152 memcpy(persis_io.pr.pr_info.sa_res_key, 8153 param->serv_act_res_key, 8154 sizeof(param->serv_act_res_key)); 8155 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8156 sizeof(persis_io.pr), M_WAITOK); 8157 } 8158 } 8159 return (0); 8160 } 8161 8162 static void 8163 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8164 { 8165 uint64_t sa_res_key; 8166 int i; 8167 8168 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8169 8170 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8171 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8172 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8173 if (sa_res_key == 0) { 8174 /* 8175 * Unregister everybody else and build UA for 8176 * them 8177 */ 8178 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8179 if (i == msg->pr.pr_info.residx || 8180 ctl_get_prkey(lun, i) == 0) 8181 continue; 8182 8183 ctl_clr_prkey(lun, i); 8184 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8185 } 8186 8187 lun->pr_key_count = 1; 8188 lun->pr_res_type = msg->pr.pr_info.res_type; 8189 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8190 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8191 lun->pr_res_idx = msg->pr.pr_info.residx; 8192 } else { 8193 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8194 if (sa_res_key == ctl_get_prkey(lun, i)) 8195 continue; 8196 8197 ctl_clr_prkey(lun, i); 8198 lun->pr_key_count--; 8199 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8200 } 8201 } 8202 } else { 8203 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8204 if (i == msg->pr.pr_info.residx || 8205 ctl_get_prkey(lun, i) == 0) 8206 continue; 8207 8208 if (sa_res_key == ctl_get_prkey(lun, i)) { 8209 ctl_clr_prkey(lun, i); 8210 lun->pr_key_count--; 8211 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8212 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8213 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8214 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8215 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8216 } 8217 } 8218 lun->pr_res_type = msg->pr.pr_info.res_type; 8219 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8220 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8221 lun->pr_res_idx = msg->pr.pr_info.residx; 8222 else 8223 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8224 } 8225 lun->pr_generation++; 8226 8227 } 8228 8229 int 8230 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8231 { 8232 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8233 struct ctl_lun *lun = CTL_LUN(ctsio); 8234 int retval; 8235 uint32_t param_len; 8236 struct scsi_per_res_out *cdb; 8237 struct scsi_per_res_out_parms* param; 8238 uint32_t residx; 8239 uint64_t res_key, sa_res_key, key; 8240 uint8_t type; 8241 union ctl_ha_msg persis_io; 8242 int i; 8243 8244 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8245 8246 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8247 retval = CTL_RETVAL_COMPLETE; 8248 8249 /* 8250 * We only support whole-LUN scope. The scope & type are ignored for 8251 * register, register and ignore existing key and clear. 8252 * We sometimes ignore scope and type on preempts too!! 8253 * Verify reservation type here as well. 8254 */ 8255 type = cdb->scope_type & SPR_TYPE_MASK; 8256 if ((cdb->action == SPRO_RESERVE) 8257 || (cdb->action == SPRO_RELEASE)) { 8258 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8259 ctl_set_invalid_field(/*ctsio*/ ctsio, 8260 /*sks_valid*/ 1, 8261 /*command*/ 1, 8262 /*field*/ 2, 8263 /*bit_valid*/ 1, 8264 /*bit*/ 4); 8265 ctl_done((union ctl_io *)ctsio); 8266 return (CTL_RETVAL_COMPLETE); 8267 } 8268 8269 if (type>8 || type==2 || type==4 || type==0) { 8270 ctl_set_invalid_field(/*ctsio*/ ctsio, 8271 /*sks_valid*/ 1, 8272 /*command*/ 1, 8273 /*field*/ 2, 8274 /*bit_valid*/ 1, 8275 /*bit*/ 0); 8276 ctl_done((union ctl_io *)ctsio); 8277 return (CTL_RETVAL_COMPLETE); 8278 } 8279 } 8280 8281 param_len = scsi_4btoul(cdb->length); 8282 8283 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8284 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8285 ctsio->kern_data_len = param_len; 8286 ctsio->kern_total_len = param_len; 8287 ctsio->kern_rel_offset = 0; 8288 ctsio->kern_sg_entries = 0; 8289 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8290 ctsio->be_move_done = ctl_config_move_done; 8291 ctl_datamove((union ctl_io *)ctsio); 8292 8293 return (CTL_RETVAL_COMPLETE); 8294 } 8295 8296 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8297 8298 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8299 res_key = scsi_8btou64(param->res_key.key); 8300 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8301 8302 /* 8303 * Validate the reservation key here except for SPRO_REG_IGNO 8304 * This must be done for all other service actions 8305 */ 8306 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8307 mtx_lock(&lun->lun_lock); 8308 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8309 if (res_key != key) { 8310 /* 8311 * The current key passed in doesn't match 8312 * the one the initiator previously 8313 * registered. 8314 */ 8315 mtx_unlock(&lun->lun_lock); 8316 free(ctsio->kern_data_ptr, M_CTL); 8317 ctl_set_reservation_conflict(ctsio); 8318 ctl_done((union ctl_io *)ctsio); 8319 return (CTL_RETVAL_COMPLETE); 8320 } 8321 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8322 /* 8323 * We are not registered 8324 */ 8325 mtx_unlock(&lun->lun_lock); 8326 free(ctsio->kern_data_ptr, M_CTL); 8327 ctl_set_reservation_conflict(ctsio); 8328 ctl_done((union ctl_io *)ctsio); 8329 return (CTL_RETVAL_COMPLETE); 8330 } else if (res_key != 0) { 8331 /* 8332 * We are not registered and trying to register but 8333 * the register key isn't zero. 8334 */ 8335 mtx_unlock(&lun->lun_lock); 8336 free(ctsio->kern_data_ptr, M_CTL); 8337 ctl_set_reservation_conflict(ctsio); 8338 ctl_done((union ctl_io *)ctsio); 8339 return (CTL_RETVAL_COMPLETE); 8340 } 8341 mtx_unlock(&lun->lun_lock); 8342 } 8343 8344 switch (cdb->action & SPRO_ACTION_MASK) { 8345 case SPRO_REGISTER: 8346 case SPRO_REG_IGNO: { 8347 /* 8348 * We don't support any of these options, as we report in 8349 * the read capabilities request (see 8350 * ctl_persistent_reserve_in(), above). 8351 */ 8352 if ((param->flags & SPR_SPEC_I_PT) 8353 || (param->flags & SPR_ALL_TG_PT) 8354 || (param->flags & SPR_APTPL)) { 8355 int bit_ptr; 8356 8357 if (param->flags & SPR_APTPL) 8358 bit_ptr = 0; 8359 else if (param->flags & SPR_ALL_TG_PT) 8360 bit_ptr = 2; 8361 else /* SPR_SPEC_I_PT */ 8362 bit_ptr = 3; 8363 8364 free(ctsio->kern_data_ptr, M_CTL); 8365 ctl_set_invalid_field(ctsio, 8366 /*sks_valid*/ 1, 8367 /*command*/ 0, 8368 /*field*/ 20, 8369 /*bit_valid*/ 1, 8370 /*bit*/ bit_ptr); 8371 ctl_done((union ctl_io *)ctsio); 8372 return (CTL_RETVAL_COMPLETE); 8373 } 8374 8375 mtx_lock(&lun->lun_lock); 8376 8377 /* 8378 * The initiator wants to clear the 8379 * key/unregister. 8380 */ 8381 if (sa_res_key == 0) { 8382 if ((res_key == 0 8383 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8384 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8385 && ctl_get_prkey(lun, residx) == 0)) { 8386 mtx_unlock(&lun->lun_lock); 8387 goto done; 8388 } 8389 8390 ctl_clr_prkey(lun, residx); 8391 lun->pr_key_count--; 8392 8393 if (residx == lun->pr_res_idx) { 8394 lun->flags &= ~CTL_LUN_PR_RESERVED; 8395 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8396 8397 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8398 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8399 lun->pr_key_count) { 8400 /* 8401 * If the reservation is a registrants 8402 * only type we need to generate a UA 8403 * for other registered inits. The 8404 * sense code should be RESERVATIONS 8405 * RELEASED 8406 */ 8407 8408 for (i = softc->init_min; i < softc->init_max; i++){ 8409 if (ctl_get_prkey(lun, i) == 0) 8410 continue; 8411 ctl_est_ua(lun, i, 8412 CTL_UA_RES_RELEASE); 8413 } 8414 } 8415 lun->pr_res_type = 0; 8416 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8417 if (lun->pr_key_count==0) { 8418 lun->flags &= ~CTL_LUN_PR_RESERVED; 8419 lun->pr_res_type = 0; 8420 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8421 } 8422 } 8423 lun->pr_generation++; 8424 mtx_unlock(&lun->lun_lock); 8425 8426 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8427 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8428 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8429 persis_io.pr.pr_info.residx = residx; 8430 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8431 sizeof(persis_io.pr), M_WAITOK); 8432 } else /* sa_res_key != 0 */ { 8433 /* 8434 * If we aren't registered currently then increment 8435 * the key count and set the registered flag. 8436 */ 8437 ctl_alloc_prkey(lun, residx); 8438 if (ctl_get_prkey(lun, residx) == 0) 8439 lun->pr_key_count++; 8440 ctl_set_prkey(lun, residx, sa_res_key); 8441 lun->pr_generation++; 8442 mtx_unlock(&lun->lun_lock); 8443 8444 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8445 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8446 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8447 persis_io.pr.pr_info.residx = residx; 8448 memcpy(persis_io.pr.pr_info.sa_res_key, 8449 param->serv_act_res_key, 8450 sizeof(param->serv_act_res_key)); 8451 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8452 sizeof(persis_io.pr), M_WAITOK); 8453 } 8454 8455 break; 8456 } 8457 case SPRO_RESERVE: 8458 mtx_lock(&lun->lun_lock); 8459 if (lun->flags & CTL_LUN_PR_RESERVED) { 8460 /* 8461 * if this isn't the reservation holder and it's 8462 * not a "all registrants" type or if the type is 8463 * different then we have a conflict 8464 */ 8465 if ((lun->pr_res_idx != residx 8466 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8467 || lun->pr_res_type != type) { 8468 mtx_unlock(&lun->lun_lock); 8469 free(ctsio->kern_data_ptr, M_CTL); 8470 ctl_set_reservation_conflict(ctsio); 8471 ctl_done((union ctl_io *)ctsio); 8472 return (CTL_RETVAL_COMPLETE); 8473 } 8474 mtx_unlock(&lun->lun_lock); 8475 } else /* create a reservation */ { 8476 /* 8477 * If it's not an "all registrants" type record 8478 * reservation holder 8479 */ 8480 if (type != SPR_TYPE_WR_EX_AR 8481 && type != SPR_TYPE_EX_AC_AR) 8482 lun->pr_res_idx = residx; /* Res holder */ 8483 else 8484 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8485 8486 lun->flags |= CTL_LUN_PR_RESERVED; 8487 lun->pr_res_type = type; 8488 8489 mtx_unlock(&lun->lun_lock); 8490 8491 /* send msg to other side */ 8492 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8493 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8494 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8495 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8496 persis_io.pr.pr_info.res_type = type; 8497 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8498 sizeof(persis_io.pr), M_WAITOK); 8499 } 8500 break; 8501 8502 case SPRO_RELEASE: 8503 mtx_lock(&lun->lun_lock); 8504 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8505 /* No reservation exists return good status */ 8506 mtx_unlock(&lun->lun_lock); 8507 goto done; 8508 } 8509 /* 8510 * Is this nexus a reservation holder? 8511 */ 8512 if (lun->pr_res_idx != residx 8513 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8514 /* 8515 * not a res holder return good status but 8516 * do nothing 8517 */ 8518 mtx_unlock(&lun->lun_lock); 8519 goto done; 8520 } 8521 8522 if (lun->pr_res_type != type) { 8523 mtx_unlock(&lun->lun_lock); 8524 free(ctsio->kern_data_ptr, M_CTL); 8525 ctl_set_illegal_pr_release(ctsio); 8526 ctl_done((union ctl_io *)ctsio); 8527 return (CTL_RETVAL_COMPLETE); 8528 } 8529 8530 /* okay to release */ 8531 lun->flags &= ~CTL_LUN_PR_RESERVED; 8532 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8533 lun->pr_res_type = 0; 8534 8535 /* 8536 * If this isn't an exclusive access reservation and NUAR 8537 * is not set, generate UA for all other registrants. 8538 */ 8539 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8540 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8541 for (i = softc->init_min; i < softc->init_max; i++) { 8542 if (i == residx || ctl_get_prkey(lun, i) == 0) 8543 continue; 8544 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8545 } 8546 } 8547 mtx_unlock(&lun->lun_lock); 8548 8549 /* Send msg to other side */ 8550 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8551 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8552 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8553 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8554 sizeof(persis_io.pr), M_WAITOK); 8555 break; 8556 8557 case SPRO_CLEAR: 8558 /* send msg to other side */ 8559 8560 mtx_lock(&lun->lun_lock); 8561 lun->flags &= ~CTL_LUN_PR_RESERVED; 8562 lun->pr_res_type = 0; 8563 lun->pr_key_count = 0; 8564 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8565 8566 ctl_clr_prkey(lun, residx); 8567 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8568 if (ctl_get_prkey(lun, i) != 0) { 8569 ctl_clr_prkey(lun, i); 8570 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8571 } 8572 lun->pr_generation++; 8573 mtx_unlock(&lun->lun_lock); 8574 8575 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8576 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8577 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8578 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8579 sizeof(persis_io.pr), M_WAITOK); 8580 break; 8581 8582 case SPRO_PREEMPT: 8583 case SPRO_PRE_ABO: { 8584 int nretval; 8585 8586 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8587 residx, ctsio, cdb, param); 8588 if (nretval != 0) 8589 return (CTL_RETVAL_COMPLETE); 8590 break; 8591 } 8592 default: 8593 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8594 } 8595 8596 done: 8597 free(ctsio->kern_data_ptr, M_CTL); 8598 ctl_set_success(ctsio); 8599 ctl_done((union ctl_io *)ctsio); 8600 8601 return (retval); 8602 } 8603 8604 /* 8605 * This routine is for handling a message from the other SC pertaining to 8606 * persistent reserve out. All the error checking will have been done 8607 * so only performing the action need be done here to keep the two 8608 * in sync. 8609 */ 8610 static void 8611 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8612 { 8613 struct ctl_softc *softc = CTL_SOFTC(io); 8614 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8615 struct ctl_lun *lun; 8616 int i; 8617 uint32_t residx, targ_lun; 8618 8619 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8620 mtx_lock(&softc->ctl_lock); 8621 if (targ_lun >= ctl_max_luns || 8622 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8623 mtx_unlock(&softc->ctl_lock); 8624 return; 8625 } 8626 mtx_lock(&lun->lun_lock); 8627 mtx_unlock(&softc->ctl_lock); 8628 if (lun->flags & CTL_LUN_DISABLED) { 8629 mtx_unlock(&lun->lun_lock); 8630 return; 8631 } 8632 residx = ctl_get_initindex(&msg->hdr.nexus); 8633 switch(msg->pr.pr_info.action) { 8634 case CTL_PR_REG_KEY: 8635 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8636 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8637 lun->pr_key_count++; 8638 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8639 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8640 lun->pr_generation++; 8641 break; 8642 8643 case CTL_PR_UNREG_KEY: 8644 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8645 lun->pr_key_count--; 8646 8647 /* XXX Need to see if the reservation has been released */ 8648 /* if so do we need to generate UA? */ 8649 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8650 lun->flags &= ~CTL_LUN_PR_RESERVED; 8651 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8652 8653 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8654 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8655 lun->pr_key_count) { 8656 /* 8657 * If the reservation is a registrants 8658 * only type we need to generate a UA 8659 * for other registered inits. The 8660 * sense code should be RESERVATIONS 8661 * RELEASED 8662 */ 8663 8664 for (i = softc->init_min; i < softc->init_max; i++) { 8665 if (ctl_get_prkey(lun, i) == 0) 8666 continue; 8667 8668 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8669 } 8670 } 8671 lun->pr_res_type = 0; 8672 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8673 if (lun->pr_key_count==0) { 8674 lun->flags &= ~CTL_LUN_PR_RESERVED; 8675 lun->pr_res_type = 0; 8676 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8677 } 8678 } 8679 lun->pr_generation++; 8680 break; 8681 8682 case CTL_PR_RESERVE: 8683 lun->flags |= CTL_LUN_PR_RESERVED; 8684 lun->pr_res_type = msg->pr.pr_info.res_type; 8685 lun->pr_res_idx = msg->pr.pr_info.residx; 8686 8687 break; 8688 8689 case CTL_PR_RELEASE: 8690 /* 8691 * If this isn't an exclusive access reservation and NUAR 8692 * is not set, generate UA for all other registrants. 8693 */ 8694 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8695 lun->pr_res_type != SPR_TYPE_WR_EX && 8696 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8697 for (i = softc->init_min; i < softc->init_max; i++) { 8698 if (i == residx || ctl_get_prkey(lun, i) == 0) 8699 continue; 8700 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8701 } 8702 } 8703 8704 lun->flags &= ~CTL_LUN_PR_RESERVED; 8705 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8706 lun->pr_res_type = 0; 8707 break; 8708 8709 case CTL_PR_PREEMPT: 8710 ctl_pro_preempt_other(lun, msg); 8711 break; 8712 case CTL_PR_CLEAR: 8713 lun->flags &= ~CTL_LUN_PR_RESERVED; 8714 lun->pr_res_type = 0; 8715 lun->pr_key_count = 0; 8716 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8717 8718 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8719 if (ctl_get_prkey(lun, i) == 0) 8720 continue; 8721 ctl_clr_prkey(lun, i); 8722 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8723 } 8724 lun->pr_generation++; 8725 break; 8726 } 8727 8728 mtx_unlock(&lun->lun_lock); 8729 } 8730 8731 int 8732 ctl_read_write(struct ctl_scsiio *ctsio) 8733 { 8734 struct ctl_lun *lun = CTL_LUN(ctsio); 8735 struct ctl_lba_len_flags *lbalen; 8736 uint64_t lba; 8737 uint32_t num_blocks; 8738 int flags, retval; 8739 int isread; 8740 8741 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8742 8743 flags = 0; 8744 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8745 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8746 switch (ctsio->cdb[0]) { 8747 case READ_6: 8748 case WRITE_6: { 8749 struct scsi_rw_6 *cdb; 8750 8751 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8752 8753 lba = scsi_3btoul(cdb->addr); 8754 /* only 5 bits are valid in the most significant address byte */ 8755 lba &= 0x1fffff; 8756 num_blocks = cdb->length; 8757 /* 8758 * This is correct according to SBC-2. 8759 */ 8760 if (num_blocks == 0) 8761 num_blocks = 256; 8762 break; 8763 } 8764 case READ_10: 8765 case WRITE_10: { 8766 struct scsi_rw_10 *cdb; 8767 8768 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8769 if (cdb->byte2 & SRW10_FUA) 8770 flags |= CTL_LLF_FUA; 8771 if (cdb->byte2 & SRW10_DPO) 8772 flags |= CTL_LLF_DPO; 8773 lba = scsi_4btoul(cdb->addr); 8774 num_blocks = scsi_2btoul(cdb->length); 8775 break; 8776 } 8777 case WRITE_VERIFY_10: { 8778 struct scsi_write_verify_10 *cdb; 8779 8780 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8781 flags |= CTL_LLF_FUA; 8782 if (cdb->byte2 & SWV_DPO) 8783 flags |= CTL_LLF_DPO; 8784 lba = scsi_4btoul(cdb->addr); 8785 num_blocks = scsi_2btoul(cdb->length); 8786 break; 8787 } 8788 case READ_12: 8789 case WRITE_12: { 8790 struct scsi_rw_12 *cdb; 8791 8792 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8793 if (cdb->byte2 & SRW12_FUA) 8794 flags |= CTL_LLF_FUA; 8795 if (cdb->byte2 & SRW12_DPO) 8796 flags |= CTL_LLF_DPO; 8797 lba = scsi_4btoul(cdb->addr); 8798 num_blocks = scsi_4btoul(cdb->length); 8799 break; 8800 } 8801 case WRITE_VERIFY_12: { 8802 struct scsi_write_verify_12 *cdb; 8803 8804 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8805 flags |= CTL_LLF_FUA; 8806 if (cdb->byte2 & SWV_DPO) 8807 flags |= CTL_LLF_DPO; 8808 lba = scsi_4btoul(cdb->addr); 8809 num_blocks = scsi_4btoul(cdb->length); 8810 break; 8811 } 8812 case READ_16: 8813 case WRITE_16: { 8814 struct scsi_rw_16 *cdb; 8815 8816 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8817 if (cdb->byte2 & SRW12_FUA) 8818 flags |= CTL_LLF_FUA; 8819 if (cdb->byte2 & SRW12_DPO) 8820 flags |= CTL_LLF_DPO; 8821 lba = scsi_8btou64(cdb->addr); 8822 num_blocks = scsi_4btoul(cdb->length); 8823 break; 8824 } 8825 case WRITE_ATOMIC_16: { 8826 struct scsi_write_atomic_16 *cdb; 8827 8828 if (lun->be_lun->atomicblock == 0) { 8829 ctl_set_invalid_opcode(ctsio); 8830 ctl_done((union ctl_io *)ctsio); 8831 return (CTL_RETVAL_COMPLETE); 8832 } 8833 8834 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8835 if (cdb->byte2 & SRW12_FUA) 8836 flags |= CTL_LLF_FUA; 8837 if (cdb->byte2 & SRW12_DPO) 8838 flags |= CTL_LLF_DPO; 8839 lba = scsi_8btou64(cdb->addr); 8840 num_blocks = scsi_2btoul(cdb->length); 8841 if (num_blocks > lun->be_lun->atomicblock) { 8842 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8843 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8844 /*bit*/ 0); 8845 ctl_done((union ctl_io *)ctsio); 8846 return (CTL_RETVAL_COMPLETE); 8847 } 8848 break; 8849 } 8850 case WRITE_VERIFY_16: { 8851 struct scsi_write_verify_16 *cdb; 8852 8853 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8854 flags |= CTL_LLF_FUA; 8855 if (cdb->byte2 & SWV_DPO) 8856 flags |= CTL_LLF_DPO; 8857 lba = scsi_8btou64(cdb->addr); 8858 num_blocks = scsi_4btoul(cdb->length); 8859 break; 8860 } 8861 default: 8862 /* 8863 * We got a command we don't support. This shouldn't 8864 * happen, commands should be filtered out above us. 8865 */ 8866 ctl_set_invalid_opcode(ctsio); 8867 ctl_done((union ctl_io *)ctsio); 8868 8869 return (CTL_RETVAL_COMPLETE); 8870 break; /* NOTREACHED */ 8871 } 8872 8873 /* 8874 * The first check is to make sure we're in bounds, the second 8875 * check is to catch wrap-around problems. If the lba + num blocks 8876 * is less than the lba, then we've wrapped around and the block 8877 * range is invalid anyway. 8878 */ 8879 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8880 || ((lba + num_blocks) < lba)) { 8881 ctl_set_lba_out_of_range(ctsio, 8882 MAX(lba, lun->be_lun->maxlba + 1)); 8883 ctl_done((union ctl_io *)ctsio); 8884 return (CTL_RETVAL_COMPLETE); 8885 } 8886 8887 /* 8888 * According to SBC-3, a transfer length of 0 is not an error. 8889 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8890 * translates to 256 blocks for those commands. 8891 */ 8892 if (num_blocks == 0) { 8893 ctl_set_success(ctsio); 8894 ctl_done((union ctl_io *)ctsio); 8895 return (CTL_RETVAL_COMPLETE); 8896 } 8897 8898 /* Set FUA and/or DPO if caches are disabled. */ 8899 if (isread) { 8900 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8901 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8902 } else { 8903 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8904 flags |= CTL_LLF_FUA; 8905 } 8906 8907 lbalen = (struct ctl_lba_len_flags *) 8908 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8909 lbalen->lba = lba; 8910 lbalen->len = num_blocks; 8911 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8912 8913 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8914 ctsio->kern_rel_offset = 0; 8915 8916 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8917 8918 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8919 return (retval); 8920 } 8921 8922 static int 8923 ctl_cnw_cont(union ctl_io *io) 8924 { 8925 struct ctl_lun *lun = CTL_LUN(io); 8926 struct ctl_scsiio *ctsio; 8927 struct ctl_lba_len_flags *lbalen; 8928 int retval; 8929 8930 CTL_IO_ASSERT(io, SCSI); 8931 8932 ctsio = &io->scsiio; 8933 ctsio->io_hdr.status = CTL_STATUS_NONE; 8934 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8935 lbalen = (struct ctl_lba_len_flags *) 8936 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8937 lbalen->flags &= ~CTL_LLF_COMPARE; 8938 lbalen->flags |= CTL_LLF_WRITE; 8939 8940 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8941 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8942 return (retval); 8943 } 8944 8945 int 8946 ctl_cnw(struct ctl_scsiio *ctsio) 8947 { 8948 struct ctl_lun *lun = CTL_LUN(ctsio); 8949 struct ctl_lba_len_flags *lbalen; 8950 uint64_t lba; 8951 uint32_t num_blocks; 8952 int flags, retval; 8953 8954 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8955 8956 flags = 0; 8957 switch (ctsio->cdb[0]) { 8958 case COMPARE_AND_WRITE: { 8959 struct scsi_compare_and_write *cdb; 8960 8961 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8962 if (cdb->byte2 & SRW10_FUA) 8963 flags |= CTL_LLF_FUA; 8964 if (cdb->byte2 & SRW10_DPO) 8965 flags |= CTL_LLF_DPO; 8966 lba = scsi_8btou64(cdb->addr); 8967 num_blocks = cdb->length; 8968 break; 8969 } 8970 default: 8971 /* 8972 * We got a command we don't support. This shouldn't 8973 * happen, commands should be filtered out above us. 8974 */ 8975 ctl_set_invalid_opcode(ctsio); 8976 ctl_done((union ctl_io *)ctsio); 8977 8978 return (CTL_RETVAL_COMPLETE); 8979 break; /* NOTREACHED */ 8980 } 8981 8982 /* 8983 * The first check is to make sure we're in bounds, the second 8984 * check is to catch wrap-around problems. If the lba + num blocks 8985 * is less than the lba, then we've wrapped around and the block 8986 * range is invalid anyway. 8987 */ 8988 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8989 || ((lba + num_blocks) < lba)) { 8990 ctl_set_lba_out_of_range(ctsio, 8991 MAX(lba, lun->be_lun->maxlba + 1)); 8992 ctl_done((union ctl_io *)ctsio); 8993 return (CTL_RETVAL_COMPLETE); 8994 } 8995 8996 /* 8997 * According to SBC-3, a transfer length of 0 is not an error. 8998 */ 8999 if (num_blocks == 0) { 9000 ctl_set_success(ctsio); 9001 ctl_done((union ctl_io *)ctsio); 9002 return (CTL_RETVAL_COMPLETE); 9003 } 9004 9005 /* Set FUA if write cache is disabled. */ 9006 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 9007 flags |= CTL_LLF_FUA; 9008 9009 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9010 ctsio->kern_rel_offset = 0; 9011 9012 /* 9013 * Set the IO_CONT flag, so that if this I/O gets passed to 9014 * ctl_data_submit_done(), it'll get passed back to 9015 * ctl_ctl_cnw_cont() for further processing. 9016 */ 9017 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9018 ctsio->io_cont = ctl_cnw_cont; 9019 9020 lbalen = (struct ctl_lba_len_flags *) 9021 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9022 lbalen->lba = lba; 9023 lbalen->len = num_blocks; 9024 lbalen->flags = CTL_LLF_COMPARE | flags; 9025 9026 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9027 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9028 return (retval); 9029 } 9030 9031 int 9032 ctl_verify(struct ctl_scsiio *ctsio) 9033 { 9034 struct ctl_lun *lun = CTL_LUN(ctsio); 9035 struct ctl_lba_len_flags *lbalen; 9036 uint64_t lba; 9037 uint32_t num_blocks; 9038 int bytchk, flags; 9039 int retval; 9040 9041 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9042 9043 bytchk = 0; 9044 flags = CTL_LLF_FUA; 9045 switch (ctsio->cdb[0]) { 9046 case VERIFY_10: { 9047 struct scsi_verify_10 *cdb; 9048 9049 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9050 if (cdb->byte2 & SVFY_BYTCHK) 9051 bytchk = 1; 9052 if (cdb->byte2 & SVFY_DPO) 9053 flags |= CTL_LLF_DPO; 9054 lba = scsi_4btoul(cdb->addr); 9055 num_blocks = scsi_2btoul(cdb->length); 9056 break; 9057 } 9058 case VERIFY_12: { 9059 struct scsi_verify_12 *cdb; 9060 9061 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9062 if (cdb->byte2 & SVFY_BYTCHK) 9063 bytchk = 1; 9064 if (cdb->byte2 & SVFY_DPO) 9065 flags |= CTL_LLF_DPO; 9066 lba = scsi_4btoul(cdb->addr); 9067 num_blocks = scsi_4btoul(cdb->length); 9068 break; 9069 } 9070 case VERIFY_16: { 9071 struct scsi_rw_16 *cdb; 9072 9073 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9074 if (cdb->byte2 & SVFY_BYTCHK) 9075 bytchk = 1; 9076 if (cdb->byte2 & SVFY_DPO) 9077 flags |= CTL_LLF_DPO; 9078 lba = scsi_8btou64(cdb->addr); 9079 num_blocks = scsi_4btoul(cdb->length); 9080 break; 9081 } 9082 default: 9083 /* 9084 * We got a command we don't support. This shouldn't 9085 * happen, commands should be filtered out above us. 9086 */ 9087 ctl_set_invalid_opcode(ctsio); 9088 ctl_done((union ctl_io *)ctsio); 9089 return (CTL_RETVAL_COMPLETE); 9090 } 9091 9092 /* 9093 * The first check is to make sure we're in bounds, the second 9094 * check is to catch wrap-around problems. If the lba + num blocks 9095 * is less than the lba, then we've wrapped around and the block 9096 * range is invalid anyway. 9097 */ 9098 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9099 || ((lba + num_blocks) < lba)) { 9100 ctl_set_lba_out_of_range(ctsio, 9101 MAX(lba, lun->be_lun->maxlba + 1)); 9102 ctl_done((union ctl_io *)ctsio); 9103 return (CTL_RETVAL_COMPLETE); 9104 } 9105 9106 /* 9107 * According to SBC-3, a transfer length of 0 is not an error. 9108 */ 9109 if (num_blocks == 0) { 9110 ctl_set_success(ctsio); 9111 ctl_done((union ctl_io *)ctsio); 9112 return (CTL_RETVAL_COMPLETE); 9113 } 9114 9115 lbalen = (struct ctl_lba_len_flags *) 9116 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9117 lbalen->lba = lba; 9118 lbalen->len = num_blocks; 9119 if (bytchk) { 9120 lbalen->flags = CTL_LLF_COMPARE | flags; 9121 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9122 } else { 9123 lbalen->flags = CTL_LLF_VERIFY | flags; 9124 ctsio->kern_total_len = 0; 9125 } 9126 ctsio->kern_rel_offset = 0; 9127 9128 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9129 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9130 return (retval); 9131 } 9132 9133 int 9134 ctl_report_luns(struct ctl_scsiio *ctsio) 9135 { 9136 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9137 struct ctl_port *port = CTL_PORT(ctsio); 9138 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9139 struct scsi_report_luns *cdb; 9140 struct scsi_report_luns_data *lun_data; 9141 int num_filled, num_luns, num_port_luns, retval; 9142 uint32_t alloc_len, lun_datalen; 9143 uint32_t initidx, targ_lun_id, lun_id; 9144 9145 retval = CTL_RETVAL_COMPLETE; 9146 cdb = (struct scsi_report_luns *)ctsio->cdb; 9147 9148 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9149 9150 num_luns = 0; 9151 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns; 9152 mtx_lock(&softc->ctl_lock); 9153 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9154 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9155 num_luns++; 9156 } 9157 mtx_unlock(&softc->ctl_lock); 9158 9159 switch (cdb->select_report) { 9160 case RPL_REPORT_DEFAULT: 9161 case RPL_REPORT_ALL: 9162 case RPL_REPORT_NONSUBSID: 9163 break; 9164 case RPL_REPORT_WELLKNOWN: 9165 case RPL_REPORT_ADMIN: 9166 case RPL_REPORT_CONGLOM: 9167 num_luns = 0; 9168 break; 9169 default: 9170 ctl_set_invalid_field(ctsio, 9171 /*sks_valid*/ 1, 9172 /*command*/ 1, 9173 /*field*/ 2, 9174 /*bit_valid*/ 0, 9175 /*bit*/ 0); 9176 ctl_done((union ctl_io *)ctsio); 9177 return (retval); 9178 break; /* NOTREACHED */ 9179 } 9180 9181 alloc_len = scsi_4btoul(cdb->length); 9182 /* 9183 * The initiator has to allocate at least 16 bytes for this request, 9184 * so he can at least get the header and the first LUN. Otherwise 9185 * we reject the request (per SPC-3 rev 14, section 6.21). 9186 */ 9187 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9188 sizeof(struct scsi_report_luns_lundata))) { 9189 ctl_set_invalid_field(ctsio, 9190 /*sks_valid*/ 1, 9191 /*command*/ 1, 9192 /*field*/ 6, 9193 /*bit_valid*/ 0, 9194 /*bit*/ 0); 9195 ctl_done((union ctl_io *)ctsio); 9196 return (retval); 9197 } 9198 9199 lun_datalen = sizeof(*lun_data) + 9200 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9201 9202 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9203 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9204 ctsio->kern_sg_entries = 0; 9205 9206 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9207 9208 mtx_lock(&softc->ctl_lock); 9209 for (targ_lun_id = 0, num_filled = 0; 9210 targ_lun_id < num_port_luns && num_filled < num_luns; 9211 targ_lun_id++) { 9212 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9213 if (lun_id == UINT32_MAX) 9214 continue; 9215 lun = softc->ctl_luns[lun_id]; 9216 if (lun == NULL) 9217 continue; 9218 9219 be64enc(lun_data->luns[num_filled++].lundata, 9220 ctl_encode_lun(targ_lun_id)); 9221 9222 /* 9223 * According to SPC-3, rev 14 section 6.21: 9224 * 9225 * "The execution of a REPORT LUNS command to any valid and 9226 * installed logical unit shall clear the REPORTED LUNS DATA 9227 * HAS CHANGED unit attention condition for all logical 9228 * units of that target with respect to the requesting 9229 * initiator. A valid and installed logical unit is one 9230 * having a PERIPHERAL QUALIFIER of 000b in the standard 9231 * INQUIRY data (see 6.4.2)." 9232 * 9233 * If request_lun is NULL, the LUN this report luns command 9234 * was issued to is either disabled or doesn't exist. In that 9235 * case, we shouldn't clear any pending lun change unit 9236 * attention. 9237 */ 9238 if (request_lun != NULL) { 9239 mtx_lock(&lun->lun_lock); 9240 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9241 mtx_unlock(&lun->lun_lock); 9242 } 9243 } 9244 mtx_unlock(&softc->ctl_lock); 9245 9246 /* 9247 * It's quite possible that we've returned fewer LUNs than we allocated 9248 * space for. Trim it. 9249 */ 9250 lun_datalen = sizeof(*lun_data) + 9251 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9252 ctsio->kern_rel_offset = 0; 9253 ctsio->kern_sg_entries = 0; 9254 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9255 ctsio->kern_total_len = ctsio->kern_data_len; 9256 9257 /* 9258 * We set this to the actual data length, regardless of how much 9259 * space we actually have to return results. If the user looks at 9260 * this value, he'll know whether or not he allocated enough space 9261 * and reissue the command if necessary. We don't support well 9262 * known logical units, so if the user asks for that, return none. 9263 */ 9264 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9265 9266 /* 9267 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9268 * this request. 9269 */ 9270 ctl_set_success(ctsio); 9271 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9272 ctsio->be_move_done = ctl_config_move_done; 9273 ctl_datamove((union ctl_io *)ctsio); 9274 return (retval); 9275 } 9276 9277 int 9278 ctl_request_sense(struct ctl_scsiio *ctsio) 9279 { 9280 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9281 struct ctl_lun *lun = CTL_LUN(ctsio); 9282 struct scsi_request_sense *cdb; 9283 struct scsi_sense_data *sense_ptr, *ps; 9284 uint32_t initidx; 9285 int have_error; 9286 u_int sense_len = SSD_FULL_SIZE; 9287 scsi_sense_data_type sense_format; 9288 ctl_ua_type ua_type; 9289 uint8_t asc = 0, ascq = 0; 9290 9291 cdb = (struct scsi_request_sense *)ctsio->cdb; 9292 9293 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9294 9295 /* 9296 * Determine which sense format the user wants. 9297 */ 9298 if (cdb->byte2 & SRS_DESC) 9299 sense_format = SSD_TYPE_DESC; 9300 else 9301 sense_format = SSD_TYPE_FIXED; 9302 9303 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9304 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9305 ctsio->kern_sg_entries = 0; 9306 ctsio->kern_rel_offset = 0; 9307 ctsio->kern_data_len = ctsio->kern_total_len = 9308 MIN(cdb->length, sizeof(*sense_ptr)); 9309 9310 /* 9311 * If we don't have a LUN, we don't have any pending sense. 9312 */ 9313 if (lun == NULL || 9314 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9315 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9316 /* "Logical unit not supported" */ 9317 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9318 /*current_error*/ 1, 9319 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9320 /*asc*/ 0x25, 9321 /*ascq*/ 0x00, 9322 SSD_ELEM_NONE); 9323 goto send; 9324 } 9325 9326 have_error = 0; 9327 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9328 /* 9329 * Check for pending sense, and then for pending unit attentions. 9330 * Pending sense gets returned first, then pending unit attentions. 9331 */ 9332 mtx_lock(&lun->lun_lock); 9333 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9334 if (ps != NULL) 9335 ps += initidx % CTL_MAX_INIT_PER_PORT; 9336 if (ps != NULL && ps->error_code != 0) { 9337 scsi_sense_data_type stored_format; 9338 9339 /* 9340 * Check to see which sense format was used for the stored 9341 * sense data. 9342 */ 9343 stored_format = scsi_sense_type(ps); 9344 9345 /* 9346 * If the user requested a different sense format than the 9347 * one we stored, then we need to convert it to the other 9348 * format. If we're going from descriptor to fixed format 9349 * sense data, we may lose things in translation, depending 9350 * on what options were used. 9351 * 9352 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9353 * for some reason we'll just copy it out as-is. 9354 */ 9355 if ((stored_format == SSD_TYPE_FIXED) 9356 && (sense_format == SSD_TYPE_DESC)) 9357 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9358 ps, (struct scsi_sense_data_desc *)sense_ptr); 9359 else if ((stored_format == SSD_TYPE_DESC) 9360 && (sense_format == SSD_TYPE_FIXED)) 9361 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9362 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9363 else 9364 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9365 9366 ps->error_code = 0; 9367 have_error = 1; 9368 } else { 9369 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9370 sense_format); 9371 if (ua_type != CTL_UA_NONE) 9372 have_error = 1; 9373 } 9374 if (have_error == 0) { 9375 /* 9376 * Report informational exception if have one and allowed. 9377 */ 9378 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9379 asc = lun->ie_asc; 9380 ascq = lun->ie_ascq; 9381 } 9382 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9383 /*current_error*/ 1, 9384 /*sense_key*/ SSD_KEY_NO_SENSE, 9385 /*asc*/ asc, 9386 /*ascq*/ ascq, 9387 SSD_ELEM_NONE); 9388 } 9389 mtx_unlock(&lun->lun_lock); 9390 9391 send: 9392 /* 9393 * We report the SCSI status as OK, since the status of the command 9394 * itself is OK. We're reporting sense as parameter data. 9395 */ 9396 ctl_set_success(ctsio); 9397 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9398 ctsio->be_move_done = ctl_config_move_done; 9399 ctl_datamove((union ctl_io *)ctsio); 9400 return (CTL_RETVAL_COMPLETE); 9401 } 9402 9403 int 9404 ctl_tur(struct ctl_scsiio *ctsio) 9405 { 9406 9407 CTL_DEBUG_PRINT(("ctl_tur\n")); 9408 9409 ctl_set_success(ctsio); 9410 ctl_done((union ctl_io *)ctsio); 9411 9412 return (CTL_RETVAL_COMPLETE); 9413 } 9414 9415 /* 9416 * SCSI VPD page 0x00, the Supported VPD Pages page. 9417 */ 9418 static int 9419 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9420 { 9421 struct ctl_lun *lun = CTL_LUN(ctsio); 9422 struct scsi_vpd_supported_pages *pages; 9423 int sup_page_size; 9424 int p; 9425 9426 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9427 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9428 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9429 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9430 ctsio->kern_rel_offset = 0; 9431 ctsio->kern_sg_entries = 0; 9432 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9433 ctsio->kern_total_len = ctsio->kern_data_len; 9434 9435 /* 9436 * The control device is always connected. The disk device, on the 9437 * other hand, may not be online all the time. Need to change this 9438 * to figure out whether the disk device is actually online or not. 9439 */ 9440 if (lun != NULL) 9441 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9442 lun->be_lun->lun_type; 9443 else 9444 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9445 9446 p = 0; 9447 /* Supported VPD pages */ 9448 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9449 /* Serial Number */ 9450 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9451 /* Device Identification */ 9452 pages->page_list[p++] = SVPD_DEVICE_ID; 9453 /* Extended INQUIRY Data */ 9454 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9455 /* Mode Page Policy */ 9456 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9457 /* SCSI Ports */ 9458 pages->page_list[p++] = SVPD_SCSI_PORTS; 9459 /* Third-party Copy */ 9460 pages->page_list[p++] = SVPD_SCSI_TPC; 9461 /* SCSI Feature Sets */ 9462 pages->page_list[p++] = SVPD_SCSI_SFS; 9463 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9464 /* Block limits */ 9465 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9466 /* Block Device Characteristics */ 9467 pages->page_list[p++] = SVPD_BDC; 9468 /* Logical Block Provisioning */ 9469 pages->page_list[p++] = SVPD_LBP; 9470 } 9471 pages->length = p; 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 /* 9481 * SCSI VPD page 0x80, the Unit Serial Number page. 9482 */ 9483 static int 9484 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9485 { 9486 struct ctl_lun *lun = CTL_LUN(ctsio); 9487 struct scsi_vpd_unit_serial_number *sn_ptr; 9488 int data_len; 9489 9490 data_len = 4 + CTL_SN_LEN; 9491 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9492 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9493 ctsio->kern_rel_offset = 0; 9494 ctsio->kern_sg_entries = 0; 9495 ctsio->kern_data_len = min(data_len, alloc_len); 9496 ctsio->kern_total_len = ctsio->kern_data_len; 9497 9498 /* 9499 * The control device is always connected. The disk device, on the 9500 * other hand, may not be online all the time. Need to change this 9501 * to figure out whether the disk device is actually online or not. 9502 */ 9503 if (lun != NULL) 9504 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9505 lun->be_lun->lun_type; 9506 else 9507 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9508 9509 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9510 sn_ptr->length = CTL_SN_LEN; 9511 /* 9512 * If we don't have a LUN, we just leave the serial number as 9513 * all spaces. 9514 */ 9515 if (lun != NULL) { 9516 strncpy((char *)sn_ptr->serial_num, 9517 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9518 } else 9519 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9520 9521 ctl_set_success(ctsio); 9522 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9523 ctsio->be_move_done = ctl_config_move_done; 9524 ctl_datamove((union ctl_io *)ctsio); 9525 return (CTL_RETVAL_COMPLETE); 9526 } 9527 9528 /* 9529 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9530 */ 9531 static int 9532 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9533 { 9534 struct ctl_lun *lun = CTL_LUN(ctsio); 9535 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9536 int data_len; 9537 9538 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9539 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9540 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9541 ctsio->kern_sg_entries = 0; 9542 ctsio->kern_rel_offset = 0; 9543 ctsio->kern_data_len = min(data_len, alloc_len); 9544 ctsio->kern_total_len = ctsio->kern_data_len; 9545 9546 /* 9547 * The control device is always connected. The disk device, on the 9548 * other hand, may not be online all the time. 9549 */ 9550 if (lun != NULL) 9551 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9552 lun->be_lun->lun_type; 9553 else 9554 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9555 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9556 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9557 /* 9558 * We support head of queue, ordered and simple tags. 9559 */ 9560 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9561 /* 9562 * Volatile cache supported. 9563 */ 9564 eid_ptr->flags3 = SVPD_EID_V_SUP; 9565 9566 /* 9567 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9568 * attention for a particular IT nexus on all LUNs once we report 9569 * it to that nexus once. This bit is required as of SPC-4. 9570 */ 9571 eid_ptr->flags4 = SVPD_EID_LUICLR; 9572 9573 /* 9574 * We support revert to defaults (RTD) bit in MODE SELECT. 9575 */ 9576 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9577 9578 /* 9579 * XXX KDM in order to correctly answer this, we would need 9580 * information from the SIM to determine how much sense data it 9581 * can send. So this would really be a path inquiry field, most 9582 * likely. This can be set to a maximum of 252 according to SPC-4, 9583 * but the hardware may or may not be able to support that much. 9584 * 0 just means that the maximum sense data length is not reported. 9585 */ 9586 eid_ptr->max_sense_length = 0; 9587 9588 ctl_set_success(ctsio); 9589 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9590 ctsio->be_move_done = ctl_config_move_done; 9591 ctl_datamove((union ctl_io *)ctsio); 9592 return (CTL_RETVAL_COMPLETE); 9593 } 9594 9595 static int 9596 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9597 { 9598 struct ctl_lun *lun = CTL_LUN(ctsio); 9599 struct scsi_vpd_mode_page_policy *mpp_ptr; 9600 int data_len; 9601 9602 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9603 sizeof(struct scsi_vpd_mode_page_policy_descr); 9604 9605 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9606 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9607 ctsio->kern_rel_offset = 0; 9608 ctsio->kern_sg_entries = 0; 9609 ctsio->kern_data_len = min(data_len, alloc_len); 9610 ctsio->kern_total_len = ctsio->kern_data_len; 9611 9612 /* 9613 * The control device is always connected. The disk device, on the 9614 * other hand, may not be online all the time. 9615 */ 9616 if (lun != NULL) 9617 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9618 lun->be_lun->lun_type; 9619 else 9620 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9621 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9622 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9623 mpp_ptr->descr[0].page_code = 0x3f; 9624 mpp_ptr->descr[0].subpage_code = 0xff; 9625 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9626 9627 ctl_set_success(ctsio); 9628 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9629 ctsio->be_move_done = ctl_config_move_done; 9630 ctl_datamove((union ctl_io *)ctsio); 9631 return (CTL_RETVAL_COMPLETE); 9632 } 9633 9634 /* 9635 * SCSI VPD page 0x83, the Device Identification page. 9636 */ 9637 static int 9638 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9639 { 9640 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9641 struct ctl_port *port = CTL_PORT(ctsio); 9642 struct ctl_lun *lun = CTL_LUN(ctsio); 9643 struct scsi_vpd_device_id *devid_ptr; 9644 struct scsi_vpd_id_descriptor *desc; 9645 int data_len, g; 9646 uint8_t proto; 9647 9648 data_len = sizeof(struct scsi_vpd_device_id) + 9649 sizeof(struct scsi_vpd_id_descriptor) + 9650 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9651 sizeof(struct scsi_vpd_id_descriptor) + 9652 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9653 if (lun && lun->lun_devid) 9654 data_len += lun->lun_devid->len; 9655 if (port && port->port_devid) 9656 data_len += port->port_devid->len; 9657 if (port && port->target_devid) 9658 data_len += port->target_devid->len; 9659 9660 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9661 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9662 ctsio->kern_sg_entries = 0; 9663 ctsio->kern_rel_offset = 0; 9664 ctsio->kern_sg_entries = 0; 9665 ctsio->kern_data_len = min(data_len, alloc_len); 9666 ctsio->kern_total_len = ctsio->kern_data_len; 9667 9668 /* 9669 * The control device is always connected. The disk device, on the 9670 * other hand, may not be online all the time. 9671 */ 9672 if (lun != NULL) 9673 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9674 lun->be_lun->lun_type; 9675 else 9676 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9677 devid_ptr->page_code = SVPD_DEVICE_ID; 9678 scsi_ulto2b(data_len - 4, devid_ptr->length); 9679 9680 if (port && port->port_type == CTL_PORT_FC) 9681 proto = SCSI_PROTO_FC << 4; 9682 else if (port && port->port_type == CTL_PORT_SAS) 9683 proto = SCSI_PROTO_SAS << 4; 9684 else if (port && port->port_type == CTL_PORT_ISCSI) 9685 proto = SCSI_PROTO_ISCSI << 4; 9686 else 9687 proto = SCSI_PROTO_SPI << 4; 9688 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9689 9690 /* 9691 * We're using a LUN association here. i.e., this device ID is a 9692 * per-LUN identifier. 9693 */ 9694 if (lun && lun->lun_devid) { 9695 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9696 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9697 lun->lun_devid->len); 9698 } 9699 9700 /* 9701 * This is for the WWPN which is a port association. 9702 */ 9703 if (port && port->port_devid) { 9704 memcpy(desc, port->port_devid->data, port->port_devid->len); 9705 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9706 port->port_devid->len); 9707 } 9708 9709 /* 9710 * This is for the Relative Target Port(type 4h) identifier 9711 */ 9712 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9713 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9714 SVPD_ID_TYPE_RELTARG; 9715 desc->length = 4; 9716 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9717 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9718 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9719 9720 /* 9721 * This is for the Target Port Group(type 5h) identifier 9722 */ 9723 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9724 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9725 SVPD_ID_TYPE_TPORTGRP; 9726 desc->length = 4; 9727 if (softc->is_single || 9728 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9729 g = 1; 9730 else 9731 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9732 scsi_ulto2b(g, &desc->identifier[2]); 9733 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9734 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9735 9736 /* 9737 * This is for the Target identifier 9738 */ 9739 if (port && port->target_devid) { 9740 memcpy(desc, port->target_devid->data, port->target_devid->len); 9741 } 9742 9743 ctl_set_success(ctsio); 9744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9745 ctsio->be_move_done = ctl_config_move_done; 9746 ctl_datamove((union ctl_io *)ctsio); 9747 return (CTL_RETVAL_COMPLETE); 9748 } 9749 9750 static int 9751 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9752 { 9753 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9754 struct ctl_lun *lun = CTL_LUN(ctsio); 9755 struct scsi_vpd_scsi_ports *sp; 9756 struct scsi_vpd_port_designation *pd; 9757 struct scsi_vpd_port_designation_cont *pdc; 9758 struct ctl_port *port; 9759 int data_len, num_target_ports, iid_len, id_len; 9760 9761 num_target_ports = 0; 9762 iid_len = 0; 9763 id_len = 0; 9764 mtx_lock(&softc->ctl_lock); 9765 STAILQ_FOREACH(port, &softc->port_list, links) { 9766 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9767 continue; 9768 if (lun != NULL && 9769 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9770 continue; 9771 num_target_ports++; 9772 if (port->init_devid) 9773 iid_len += port->init_devid->len; 9774 if (port->port_devid) 9775 id_len += port->port_devid->len; 9776 } 9777 mtx_unlock(&softc->ctl_lock); 9778 9779 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9780 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9781 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9782 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9783 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9784 ctsio->kern_sg_entries = 0; 9785 ctsio->kern_rel_offset = 0; 9786 ctsio->kern_sg_entries = 0; 9787 ctsio->kern_data_len = min(data_len, alloc_len); 9788 ctsio->kern_total_len = ctsio->kern_data_len; 9789 9790 /* 9791 * The control device is always connected. The disk device, on the 9792 * other hand, may not be online all the time. Need to change this 9793 * to figure out whether the disk device is actually online or not. 9794 */ 9795 if (lun != NULL) 9796 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9797 lun->be_lun->lun_type; 9798 else 9799 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9800 9801 sp->page_code = SVPD_SCSI_PORTS; 9802 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9803 sp->page_length); 9804 pd = &sp->design[0]; 9805 9806 mtx_lock(&softc->ctl_lock); 9807 STAILQ_FOREACH(port, &softc->port_list, links) { 9808 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9809 continue; 9810 if (lun != NULL && 9811 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9812 continue; 9813 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9814 if (port->init_devid) { 9815 iid_len = port->init_devid->len; 9816 memcpy(pd->initiator_transportid, 9817 port->init_devid->data, port->init_devid->len); 9818 } else 9819 iid_len = 0; 9820 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9821 pdc = (struct scsi_vpd_port_designation_cont *) 9822 (&pd->initiator_transportid[iid_len]); 9823 if (port->port_devid) { 9824 id_len = port->port_devid->len; 9825 memcpy(pdc->target_port_descriptors, 9826 port->port_devid->data, port->port_devid->len); 9827 } else 9828 id_len = 0; 9829 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9830 pd = (struct scsi_vpd_port_designation *) 9831 ((uint8_t *)pdc->target_port_descriptors + id_len); 9832 } 9833 mtx_unlock(&softc->ctl_lock); 9834 9835 ctl_set_success(ctsio); 9836 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9837 ctsio->be_move_done = ctl_config_move_done; 9838 ctl_datamove((union ctl_io *)ctsio); 9839 return (CTL_RETVAL_COMPLETE); 9840 } 9841 9842 static int 9843 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len) 9844 { 9845 struct ctl_lun *lun = CTL_LUN(ctsio); 9846 struct scsi_vpd_sfs *sfs_ptr; 9847 int sfs_page_size, n; 9848 9849 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2; 9850 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO); 9851 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr; 9852 ctsio->kern_sg_entries = 0; 9853 ctsio->kern_rel_offset = 0; 9854 ctsio->kern_sg_entries = 0; 9855 ctsio->kern_data_len = min(sfs_page_size, alloc_len); 9856 ctsio->kern_total_len = ctsio->kern_data_len; 9857 9858 /* 9859 * The control device is always connected. The disk device, on the 9860 * other hand, may not be online all the time. Need to change this 9861 * to figure out whether the disk device is actually online or not. 9862 */ 9863 if (lun != NULL) 9864 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9865 lun->be_lun->lun_type; 9866 else 9867 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9868 9869 sfs_ptr->page_code = SVPD_SCSI_SFS; 9870 n = 0; 9871 /* Discovery 2016 */ 9872 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]); 9873 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9874 /* SBC Base 2016 */ 9875 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]); 9876 /* SBC Base 2010 */ 9877 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]); 9878 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9879 /* Basic Provisioning 2016 */ 9880 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]); 9881 } 9882 /* Drive Maintenance 2016 */ 9883 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]); 9884 } 9885 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length); 9886 9887 ctl_set_success(ctsio); 9888 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9889 ctsio->be_move_done = ctl_config_move_done; 9890 ctl_datamove((union ctl_io *)ctsio); 9891 return (CTL_RETVAL_COMPLETE); 9892 } 9893 9894 static int 9895 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9896 { 9897 struct ctl_lun *lun = CTL_LUN(ctsio); 9898 struct scsi_vpd_block_limits *bl_ptr; 9899 const char *val; 9900 uint64_t ival; 9901 9902 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9903 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9904 ctsio->kern_sg_entries = 0; 9905 ctsio->kern_rel_offset = 0; 9906 ctsio->kern_sg_entries = 0; 9907 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9908 ctsio->kern_total_len = ctsio->kern_data_len; 9909 9910 /* 9911 * The control device is always connected. The disk device, on the 9912 * other hand, may not be online all the time. Need to change this 9913 * to figure out whether the disk device is actually online or not. 9914 */ 9915 if (lun != NULL) 9916 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9917 lun->be_lun->lun_type; 9918 else 9919 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9920 9921 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9922 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9923 bl_ptr->max_cmp_write_len = 0xff; 9924 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9925 if (lun != NULL) { 9926 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9927 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9928 ival = 0xffffffff; 9929 val = dnvlist_get_string(lun->be_lun->options, 9930 "unmap_max_lba", NULL); 9931 if (val != NULL) 9932 ctl_expand_number(val, &ival); 9933 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9934 ival = 0xffffffff; 9935 val = dnvlist_get_string(lun->be_lun->options, 9936 "unmap_max_descr", NULL); 9937 if (val != NULL) 9938 ctl_expand_number(val, &ival); 9939 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9940 if (lun->be_lun->ublockexp != 0) { 9941 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9942 bl_ptr->opt_unmap_grain); 9943 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9944 bl_ptr->unmap_grain_align); 9945 } 9946 } 9947 scsi_ulto4b(lun->be_lun->atomicblock, 9948 bl_ptr->max_atomic_transfer_length); 9949 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9950 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9951 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9952 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9953 ival = UINT64_MAX; 9954 val = dnvlist_get_string(lun->be_lun->options, 9955 "write_same_max_lba", NULL); 9956 if (val != NULL) 9957 ctl_expand_number(val, &ival); 9958 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9959 if (lun->be_lun->maxlba + 1 > ival) 9960 bl_ptr->flags |= SVPD_BL_WSNZ; 9961 } 9962 9963 ctl_set_success(ctsio); 9964 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9965 ctsio->be_move_done = ctl_config_move_done; 9966 ctl_datamove((union ctl_io *)ctsio); 9967 return (CTL_RETVAL_COMPLETE); 9968 } 9969 9970 static int 9971 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9972 { 9973 struct ctl_lun *lun = CTL_LUN(ctsio); 9974 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9975 const char *value; 9976 u_int i; 9977 9978 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9979 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9980 ctsio->kern_sg_entries = 0; 9981 ctsio->kern_rel_offset = 0; 9982 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 9983 ctsio->kern_total_len = ctsio->kern_data_len; 9984 9985 /* 9986 * The control device is always connected. The disk device, on the 9987 * other hand, may not be online all the time. Need to change this 9988 * to figure out whether the disk device is actually online or not. 9989 */ 9990 if (lun != NULL) 9991 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9992 lun->be_lun->lun_type; 9993 else 9994 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9995 bdc_ptr->page_code = SVPD_BDC; 9996 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9997 if (lun != NULL && 9998 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL) 9999 i = strtol(value, NULL, 0); 10000 else 10001 i = CTL_DEFAULT_ROTATION_RATE; 10002 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10003 if (lun != NULL && 10004 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL) 10005 i = strtol(value, NULL, 0); 10006 else 10007 i = 0; 10008 bdc_ptr->wab_wac_ff = (i & 0x0f); 10009 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS; 10010 10011 ctl_set_success(ctsio); 10012 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10013 ctsio->be_move_done = ctl_config_move_done; 10014 ctl_datamove((union ctl_io *)ctsio); 10015 return (CTL_RETVAL_COMPLETE); 10016 } 10017 10018 static int 10019 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10020 { 10021 struct ctl_lun *lun = CTL_LUN(ctsio); 10022 struct scsi_vpd_logical_block_prov *lbp_ptr; 10023 const char *value; 10024 10025 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10026 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10027 ctsio->kern_sg_entries = 0; 10028 ctsio->kern_rel_offset = 0; 10029 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 10030 ctsio->kern_total_len = ctsio->kern_data_len; 10031 10032 /* 10033 * The control device is always connected. The disk device, on the 10034 * other hand, may not be online all the time. Need to change this 10035 * to figure out whether the disk device is actually online or not. 10036 */ 10037 if (lun != NULL) 10038 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10039 lun->be_lun->lun_type; 10040 else 10041 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10042 10043 lbp_ptr->page_code = SVPD_LBP; 10044 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10045 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10046 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10047 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10048 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10049 value = dnvlist_get_string(lun->be_lun->options, 10050 "provisioning_type", NULL); 10051 if (value != NULL) { 10052 if (strcmp(value, "resource") == 0) 10053 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10054 else if (strcmp(value, "thin") == 0) 10055 lbp_ptr->prov_type = SVPD_LBP_THIN; 10056 } else 10057 lbp_ptr->prov_type = SVPD_LBP_THIN; 10058 } 10059 10060 ctl_set_success(ctsio); 10061 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10062 ctsio->be_move_done = ctl_config_move_done; 10063 ctl_datamove((union ctl_io *)ctsio); 10064 return (CTL_RETVAL_COMPLETE); 10065 } 10066 10067 /* 10068 * INQUIRY with the EVPD bit set. 10069 */ 10070 static int 10071 ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10072 { 10073 struct ctl_lun *lun = CTL_LUN(ctsio); 10074 struct scsi_inquiry *cdb; 10075 int alloc_len, retval; 10076 10077 cdb = (struct scsi_inquiry *)ctsio->cdb; 10078 alloc_len = scsi_2btoul(cdb->length); 10079 10080 switch (cdb->page_code) { 10081 case SVPD_SUPPORTED_PAGES: 10082 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10083 break; 10084 case SVPD_UNIT_SERIAL_NUMBER: 10085 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10086 break; 10087 case SVPD_DEVICE_ID: 10088 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10089 break; 10090 case SVPD_EXTENDED_INQUIRY_DATA: 10091 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10092 break; 10093 case SVPD_MODE_PAGE_POLICY: 10094 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10095 break; 10096 case SVPD_SCSI_PORTS: 10097 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10098 break; 10099 case SVPD_SCSI_TPC: 10100 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10101 break; 10102 case SVPD_SCSI_SFS: 10103 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len); 10104 break; 10105 case SVPD_BLOCK_LIMITS: 10106 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10107 goto err; 10108 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10109 break; 10110 case SVPD_BDC: 10111 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10112 goto err; 10113 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10114 break; 10115 case SVPD_LBP: 10116 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10117 goto err; 10118 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10119 break; 10120 default: 10121 err: 10122 ctl_set_invalid_field(ctsio, 10123 /*sks_valid*/ 1, 10124 /*command*/ 1, 10125 /*field*/ 2, 10126 /*bit_valid*/ 0, 10127 /*bit*/ 0); 10128 ctl_done((union ctl_io *)ctsio); 10129 retval = CTL_RETVAL_COMPLETE; 10130 break; 10131 } 10132 10133 return (retval); 10134 } 10135 10136 /* 10137 * Standard INQUIRY data. 10138 */ 10139 static int 10140 ctl_inquiry_std(struct ctl_scsiio *ctsio) 10141 { 10142 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10143 struct ctl_port *port = CTL_PORT(ctsio); 10144 struct ctl_lun *lun = CTL_LUN(ctsio); 10145 struct scsi_inquiry_data *inq_ptr; 10146 struct scsi_inquiry *cdb; 10147 const char *val; 10148 uint32_t alloc_len, data_len; 10149 ctl_port_type port_type; 10150 10151 port_type = port->port_type; 10152 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10153 port_type = CTL_PORT_SCSI; 10154 10155 cdb = (struct scsi_inquiry *)ctsio->cdb; 10156 alloc_len = scsi_2btoul(cdb->length); 10157 10158 /* 10159 * We malloc the full inquiry data size here and fill it 10160 * in. If the user only asks for less, we'll give him 10161 * that much. 10162 */ 10163 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10164 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10165 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10166 ctsio->kern_sg_entries = 0; 10167 ctsio->kern_rel_offset = 0; 10168 ctsio->kern_data_len = min(data_len, alloc_len); 10169 ctsio->kern_total_len = ctsio->kern_data_len; 10170 10171 if (lun != NULL) { 10172 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10173 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10174 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10175 lun->be_lun->lun_type; 10176 } else { 10177 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10178 lun->be_lun->lun_type; 10179 } 10180 if (lun->flags & CTL_LUN_REMOVABLE) 10181 inq_ptr->dev_qual2 |= SID_RMB; 10182 } else 10183 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10184 10185 /* RMB in byte 2 is 0 */ 10186 inq_ptr->version = SCSI_REV_SPC5; 10187 10188 /* 10189 * According to SAM-3, even if a device only supports a single 10190 * level of LUN addressing, it should still set the HISUP bit: 10191 * 10192 * 4.9.1 Logical unit numbers overview 10193 * 10194 * All logical unit number formats described in this standard are 10195 * hierarchical in structure even when only a single level in that 10196 * hierarchy is used. The HISUP bit shall be set to one in the 10197 * standard INQUIRY data (see SPC-2) when any logical unit number 10198 * format described in this standard is used. Non-hierarchical 10199 * formats are outside the scope of this standard. 10200 * 10201 * Therefore we set the HiSup bit here. 10202 * 10203 * The response format is 2, per SPC-3. 10204 */ 10205 inq_ptr->response_format = SID_HiSup | 2; 10206 10207 inq_ptr->additional_length = data_len - 10208 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10209 CTL_DEBUG_PRINT(("additional_length = %d\n", 10210 inq_ptr->additional_length)); 10211 10212 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10213 if (port_type == CTL_PORT_SCSI) 10214 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10215 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10216 inq_ptr->flags = SID_CmdQue; 10217 if (port_type == CTL_PORT_SCSI) 10218 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10219 10220 /* 10221 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10222 * We have 8 bytes for the vendor name, and 16 bytes for the device 10223 * name and 4 bytes for the revision. 10224 */ 10225 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10226 "vendor", NULL)) == NULL) { 10227 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10228 } else { 10229 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10230 strncpy(inq_ptr->vendor, val, 10231 min(sizeof(inq_ptr->vendor), strlen(val))); 10232 } 10233 if (lun == NULL) { 10234 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10235 sizeof(inq_ptr->product)); 10236 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product", 10237 NULL)) == NULL) { 10238 switch (lun->be_lun->lun_type) { 10239 case T_DIRECT: 10240 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10241 sizeof(inq_ptr->product)); 10242 break; 10243 case T_PROCESSOR: 10244 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10245 sizeof(inq_ptr->product)); 10246 break; 10247 case T_CDROM: 10248 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10249 sizeof(inq_ptr->product)); 10250 break; 10251 default: 10252 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10253 sizeof(inq_ptr->product)); 10254 break; 10255 } 10256 } else { 10257 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10258 strncpy(inq_ptr->product, val, 10259 min(sizeof(inq_ptr->product), strlen(val))); 10260 } 10261 10262 /* 10263 * XXX make this a macro somewhere so it automatically gets 10264 * incremented when we make changes. 10265 */ 10266 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options, 10267 "revision", NULL)) == NULL) { 10268 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10269 } else { 10270 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10271 strncpy(inq_ptr->revision, val, 10272 min(sizeof(inq_ptr->revision), strlen(val))); 10273 } 10274 10275 /* 10276 * For parallel SCSI, we support double transition and single 10277 * transition clocking. We also support QAS (Quick Arbitration 10278 * and Selection) and Information Unit transfers on both the 10279 * control and array devices. 10280 */ 10281 if (port_type == CTL_PORT_SCSI) 10282 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10283 SID_SPI_IUS; 10284 10285 /* SAM-6 (no version claimed) */ 10286 scsi_ulto2b(0x00C0, inq_ptr->version1); 10287 /* SPC-5 (no version claimed) */ 10288 scsi_ulto2b(0x05C0, inq_ptr->version2); 10289 if (port_type == CTL_PORT_FC) { 10290 /* FCP-2 ANSI INCITS.350:2003 */ 10291 scsi_ulto2b(0x0917, inq_ptr->version3); 10292 } else if (port_type == CTL_PORT_SCSI) { 10293 /* SPI-4 ANSI INCITS.362:200x */ 10294 scsi_ulto2b(0x0B56, inq_ptr->version3); 10295 } else if (port_type == CTL_PORT_ISCSI) { 10296 /* iSCSI (no version claimed) */ 10297 scsi_ulto2b(0x0960, inq_ptr->version3); 10298 } else if (port_type == CTL_PORT_SAS) { 10299 /* SAS (no version claimed) */ 10300 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10301 } else if (port_type == CTL_PORT_UMASS) { 10302 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */ 10303 scsi_ulto2b(0x1730, inq_ptr->version3); 10304 } 10305 10306 if (lun == NULL) { 10307 /* SBC-4 (no version claimed) */ 10308 scsi_ulto2b(0x0600, inq_ptr->version4); 10309 } else { 10310 switch (lun->be_lun->lun_type) { 10311 case T_DIRECT: 10312 /* SBC-4 (no version claimed) */ 10313 scsi_ulto2b(0x0600, inq_ptr->version4); 10314 break; 10315 case T_PROCESSOR: 10316 break; 10317 case T_CDROM: 10318 /* MMC-6 (no version claimed) */ 10319 scsi_ulto2b(0x04E0, inq_ptr->version4); 10320 break; 10321 default: 10322 break; 10323 } 10324 } 10325 10326 ctl_set_success(ctsio); 10327 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10328 ctsio->be_move_done = ctl_config_move_done; 10329 ctl_datamove((union ctl_io *)ctsio); 10330 return (CTL_RETVAL_COMPLETE); 10331 } 10332 10333 int 10334 ctl_inquiry(struct ctl_scsiio *ctsio) 10335 { 10336 struct scsi_inquiry *cdb; 10337 int retval; 10338 10339 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10340 10341 cdb = (struct scsi_inquiry *)ctsio->cdb; 10342 if (cdb->byte2 & SI_EVPD) 10343 retval = ctl_inquiry_evpd(ctsio); 10344 else if (cdb->page_code == 0) 10345 retval = ctl_inquiry_std(ctsio); 10346 else { 10347 ctl_set_invalid_field(ctsio, 10348 /*sks_valid*/ 1, 10349 /*command*/ 1, 10350 /*field*/ 2, 10351 /*bit_valid*/ 0, 10352 /*bit*/ 0); 10353 ctl_done((union ctl_io *)ctsio); 10354 return (CTL_RETVAL_COMPLETE); 10355 } 10356 10357 return (retval); 10358 } 10359 10360 int 10361 ctl_get_config(struct ctl_scsiio *ctsio) 10362 { 10363 struct ctl_lun *lun = CTL_LUN(ctsio); 10364 struct scsi_get_config_header *hdr; 10365 struct scsi_get_config_feature *feature; 10366 struct scsi_get_config *cdb; 10367 uint32_t alloc_len, data_len; 10368 int rt, starting; 10369 10370 cdb = (struct scsi_get_config *)ctsio->cdb; 10371 rt = (cdb->rt & SGC_RT_MASK); 10372 starting = scsi_2btoul(cdb->starting_feature); 10373 alloc_len = scsi_2btoul(cdb->length); 10374 10375 data_len = sizeof(struct scsi_get_config_header) + 10376 sizeof(struct scsi_get_config_feature) + 8 + 10377 sizeof(struct scsi_get_config_feature) + 8 + 10378 sizeof(struct scsi_get_config_feature) + 4 + 10379 sizeof(struct scsi_get_config_feature) + 4 + 10380 sizeof(struct scsi_get_config_feature) + 8 + 10381 sizeof(struct scsi_get_config_feature) + 10382 sizeof(struct scsi_get_config_feature) + 4 + 10383 sizeof(struct scsi_get_config_feature) + 4 + 10384 sizeof(struct scsi_get_config_feature) + 4 + 10385 sizeof(struct scsi_get_config_feature) + 4 + 10386 sizeof(struct scsi_get_config_feature) + 4 + 10387 sizeof(struct scsi_get_config_feature) + 4; 10388 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10389 ctsio->kern_sg_entries = 0; 10390 ctsio->kern_rel_offset = 0; 10391 10392 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10393 if (lun->flags & CTL_LUN_NO_MEDIA) 10394 scsi_ulto2b(0x0000, hdr->current_profile); 10395 else 10396 scsi_ulto2b(0x0010, hdr->current_profile); 10397 feature = (struct scsi_get_config_feature *)(hdr + 1); 10398 10399 if (starting > 0x003b) 10400 goto done; 10401 if (starting > 0x003a) 10402 goto f3b; 10403 if (starting > 0x002b) 10404 goto f3a; 10405 if (starting > 0x002a) 10406 goto f2b; 10407 if (starting > 0x001f) 10408 goto f2a; 10409 if (starting > 0x001e) 10410 goto f1f; 10411 if (starting > 0x001d) 10412 goto f1e; 10413 if (starting > 0x0010) 10414 goto f1d; 10415 if (starting > 0x0003) 10416 goto f10; 10417 if (starting > 0x0002) 10418 goto f3; 10419 if (starting > 0x0001) 10420 goto f2; 10421 if (starting > 0x0000) 10422 goto f1; 10423 10424 /* Profile List */ 10425 scsi_ulto2b(0x0000, feature->feature_code); 10426 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10427 feature->add_length = 8; 10428 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10429 feature->feature_data[2] = 0x00; 10430 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10431 feature->feature_data[6] = 0x01; 10432 feature = (struct scsi_get_config_feature *) 10433 &feature->feature_data[feature->add_length]; 10434 10435 f1: /* Core */ 10436 scsi_ulto2b(0x0001, feature->feature_code); 10437 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10438 feature->add_length = 8; 10439 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10440 feature->feature_data[4] = 0x03; 10441 feature = (struct scsi_get_config_feature *) 10442 &feature->feature_data[feature->add_length]; 10443 10444 f2: /* Morphing */ 10445 scsi_ulto2b(0x0002, feature->feature_code); 10446 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10447 feature->add_length = 4; 10448 feature->feature_data[0] = 0x02; 10449 feature = (struct scsi_get_config_feature *) 10450 &feature->feature_data[feature->add_length]; 10451 10452 f3: /* Removable Medium */ 10453 scsi_ulto2b(0x0003, feature->feature_code); 10454 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10455 feature->add_length = 4; 10456 feature->feature_data[0] = 0x39; 10457 feature = (struct scsi_get_config_feature *) 10458 &feature->feature_data[feature->add_length]; 10459 10460 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10461 goto done; 10462 10463 f10: /* Random Read */ 10464 scsi_ulto2b(0x0010, feature->feature_code); 10465 feature->flags = 0x00; 10466 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10467 feature->flags |= SGC_F_CURRENT; 10468 feature->add_length = 8; 10469 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10470 scsi_ulto2b(1, &feature->feature_data[4]); 10471 feature->feature_data[6] = 0x00; 10472 feature = (struct scsi_get_config_feature *) 10473 &feature->feature_data[feature->add_length]; 10474 10475 f1d: /* Multi-Read */ 10476 scsi_ulto2b(0x001D, feature->feature_code); 10477 feature->flags = 0x00; 10478 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10479 feature->flags |= SGC_F_CURRENT; 10480 feature->add_length = 0; 10481 feature = (struct scsi_get_config_feature *) 10482 &feature->feature_data[feature->add_length]; 10483 10484 f1e: /* CD Read */ 10485 scsi_ulto2b(0x001E, feature->feature_code); 10486 feature->flags = 0x00; 10487 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10488 feature->flags |= SGC_F_CURRENT; 10489 feature->add_length = 4; 10490 feature->feature_data[0] = 0x00; 10491 feature = (struct scsi_get_config_feature *) 10492 &feature->feature_data[feature->add_length]; 10493 10494 f1f: /* DVD Read */ 10495 scsi_ulto2b(0x001F, feature->feature_code); 10496 feature->flags = 0x08; 10497 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10498 feature->flags |= SGC_F_CURRENT; 10499 feature->add_length = 4; 10500 feature->feature_data[0] = 0x01; 10501 feature->feature_data[2] = 0x03; 10502 feature = (struct scsi_get_config_feature *) 10503 &feature->feature_data[feature->add_length]; 10504 10505 f2a: /* DVD+RW */ 10506 scsi_ulto2b(0x002A, feature->feature_code); 10507 feature->flags = 0x04; 10508 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10509 feature->flags |= SGC_F_CURRENT; 10510 feature->add_length = 4; 10511 feature->feature_data[0] = 0x00; 10512 feature->feature_data[1] = 0x00; 10513 feature = (struct scsi_get_config_feature *) 10514 &feature->feature_data[feature->add_length]; 10515 10516 f2b: /* DVD+R */ 10517 scsi_ulto2b(0x002B, feature->feature_code); 10518 feature->flags = 0x00; 10519 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10520 feature->flags |= SGC_F_CURRENT; 10521 feature->add_length = 4; 10522 feature->feature_data[0] = 0x00; 10523 feature = (struct scsi_get_config_feature *) 10524 &feature->feature_data[feature->add_length]; 10525 10526 f3a: /* DVD+RW Dual Layer */ 10527 scsi_ulto2b(0x003A, feature->feature_code); 10528 feature->flags = 0x00; 10529 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10530 feature->flags |= SGC_F_CURRENT; 10531 feature->add_length = 4; 10532 feature->feature_data[0] = 0x00; 10533 feature->feature_data[1] = 0x00; 10534 feature = (struct scsi_get_config_feature *) 10535 &feature->feature_data[feature->add_length]; 10536 10537 f3b: /* DVD+R Dual Layer */ 10538 scsi_ulto2b(0x003B, feature->feature_code); 10539 feature->flags = 0x00; 10540 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10541 feature->flags |= SGC_F_CURRENT; 10542 feature->add_length = 4; 10543 feature->feature_data[0] = 0x00; 10544 feature = (struct scsi_get_config_feature *) 10545 &feature->feature_data[feature->add_length]; 10546 10547 done: 10548 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10549 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10550 feature = (struct scsi_get_config_feature *)(hdr + 1); 10551 if (scsi_2btoul(feature->feature_code) == starting) 10552 feature = (struct scsi_get_config_feature *) 10553 &feature->feature_data[feature->add_length]; 10554 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10555 } 10556 scsi_ulto4b(data_len - 4, hdr->data_length); 10557 ctsio->kern_data_len = min(data_len, alloc_len); 10558 ctsio->kern_total_len = ctsio->kern_data_len; 10559 10560 ctl_set_success(ctsio); 10561 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10562 ctsio->be_move_done = ctl_config_move_done; 10563 ctl_datamove((union ctl_io *)ctsio); 10564 return (CTL_RETVAL_COMPLETE); 10565 } 10566 10567 int 10568 ctl_get_event_status(struct ctl_scsiio *ctsio) 10569 { 10570 struct scsi_get_event_status_header *hdr; 10571 struct scsi_get_event_status *cdb; 10572 uint32_t alloc_len, data_len; 10573 10574 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10575 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10576 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10577 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10578 ctl_done((union ctl_io *)ctsio); 10579 return (CTL_RETVAL_COMPLETE); 10580 } 10581 alloc_len = scsi_2btoul(cdb->length); 10582 10583 data_len = sizeof(struct scsi_get_event_status_header); 10584 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10585 ctsio->kern_sg_entries = 0; 10586 ctsio->kern_rel_offset = 0; 10587 ctsio->kern_data_len = min(data_len, alloc_len); 10588 ctsio->kern_total_len = ctsio->kern_data_len; 10589 10590 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10591 scsi_ulto2b(0, hdr->descr_length); 10592 hdr->nea_class = SGESN_NEA; 10593 hdr->supported_class = 0; 10594 10595 ctl_set_success(ctsio); 10596 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10597 ctsio->be_move_done = ctl_config_move_done; 10598 ctl_datamove((union ctl_io *)ctsio); 10599 return (CTL_RETVAL_COMPLETE); 10600 } 10601 10602 int 10603 ctl_mechanism_status(struct ctl_scsiio *ctsio) 10604 { 10605 struct scsi_mechanism_status_header *hdr; 10606 struct scsi_mechanism_status *cdb; 10607 uint32_t alloc_len, data_len; 10608 10609 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10610 alloc_len = scsi_2btoul(cdb->length); 10611 10612 data_len = sizeof(struct scsi_mechanism_status_header); 10613 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10614 ctsio->kern_sg_entries = 0; 10615 ctsio->kern_rel_offset = 0; 10616 ctsio->kern_data_len = min(data_len, alloc_len); 10617 ctsio->kern_total_len = ctsio->kern_data_len; 10618 10619 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10620 hdr->state1 = 0x00; 10621 hdr->state2 = 0xe0; 10622 scsi_ulto3b(0, hdr->lba); 10623 hdr->slots_num = 0; 10624 scsi_ulto2b(0, hdr->slots_length); 10625 10626 ctl_set_success(ctsio); 10627 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10628 ctsio->be_move_done = ctl_config_move_done; 10629 ctl_datamove((union ctl_io *)ctsio); 10630 return (CTL_RETVAL_COMPLETE); 10631 } 10632 10633 static void 10634 ctl_ultomsf(uint32_t lba, uint8_t *buf) 10635 { 10636 10637 lba += 150; 10638 buf[0] = 0; 10639 buf[1] = bin2bcd((lba / 75) / 60); 10640 buf[2] = bin2bcd((lba / 75) % 60); 10641 buf[3] = bin2bcd(lba % 75); 10642 } 10643 10644 int 10645 ctl_read_toc(struct ctl_scsiio *ctsio) 10646 { 10647 struct ctl_lun *lun = CTL_LUN(ctsio); 10648 struct scsi_read_toc_hdr *hdr; 10649 struct scsi_read_toc_type01_descr *descr; 10650 struct scsi_read_toc *cdb; 10651 uint32_t alloc_len, data_len; 10652 int format, msf; 10653 10654 cdb = (struct scsi_read_toc *)ctsio->cdb; 10655 msf = (cdb->byte2 & CD_MSF) != 0; 10656 format = cdb->format; 10657 alloc_len = scsi_2btoul(cdb->data_len); 10658 10659 data_len = sizeof(struct scsi_read_toc_hdr); 10660 if (format == 0) 10661 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10662 else 10663 data_len += sizeof(struct scsi_read_toc_type01_descr); 10664 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10665 ctsio->kern_sg_entries = 0; 10666 ctsio->kern_rel_offset = 0; 10667 ctsio->kern_data_len = min(data_len, alloc_len); 10668 ctsio->kern_total_len = ctsio->kern_data_len; 10669 10670 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10671 if (format == 0) { 10672 scsi_ulto2b(0x12, hdr->data_length); 10673 hdr->first = 1; 10674 hdr->last = 1; 10675 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10676 descr->addr_ctl = 0x14; 10677 descr->track_number = 1; 10678 if (msf) 10679 ctl_ultomsf(0, descr->track_start); 10680 else 10681 scsi_ulto4b(0, descr->track_start); 10682 descr++; 10683 descr->addr_ctl = 0x14; 10684 descr->track_number = 0xaa; 10685 if (msf) 10686 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10687 else 10688 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10689 } else { 10690 scsi_ulto2b(0x0a, hdr->data_length); 10691 hdr->first = 1; 10692 hdr->last = 1; 10693 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10694 descr->addr_ctl = 0x14; 10695 descr->track_number = 1; 10696 if (msf) 10697 ctl_ultomsf(0, descr->track_start); 10698 else 10699 scsi_ulto4b(0, descr->track_start); 10700 } 10701 10702 ctl_set_success(ctsio); 10703 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10704 ctsio->be_move_done = ctl_config_move_done; 10705 ctl_datamove((union ctl_io *)ctsio); 10706 return (CTL_RETVAL_COMPLETE); 10707 } 10708 10709 /* 10710 * For NVMe commands, parse the LBA and length. 10711 */ 10712 static bool 10713 ctl_nvme_get_lba_len(struct ctl_nvmeio *ctnio, uint64_t *lba, uint32_t *len) 10714 { 10715 CTL_IO_ASSERT(ctnio, NVME); 10716 10717 switch (ctnio->cmd.opc) { 10718 case NVME_OPC_WRITE: 10719 case NVME_OPC_READ: 10720 case NVME_OPC_WRITE_UNCORRECTABLE: 10721 case NVME_OPC_COMPARE: 10722 case NVME_OPC_WRITE_ZEROES: 10723 case NVME_OPC_VERIFY: 10724 *lba = (uint64_t)le32toh(ctnio->cmd.cdw11) << 32 | 10725 le32toh(ctnio->cmd.cdw10); 10726 *len = (le32toh(ctnio->cmd.cdw12) & 0xffff) + 1; 10727 return (true); 10728 default: 10729 *lba = 0; 10730 *len = 0; 10731 return (false); 10732 } 10733 } 10734 10735 static bool 10736 ctl_nvme_fua(struct ctl_nvmeio *ctnio) 10737 { 10738 return ((le32toh(ctnio->cmd.cdw12) & (1U << 30)) != 0); 10739 } 10740 10741 int 10742 ctl_nvme_identify(struct ctl_nvmeio *ctnio) 10743 { 10744 struct ctl_lun *lun = CTL_LUN(ctnio); 10745 size_t len; 10746 int retval; 10747 uint8_t cns; 10748 10749 CTL_DEBUG_PRINT(("ctl_nvme_identify\n")); 10750 10751 CTL_IO_ASSERT(ctnio, NVME_ADMIN); 10752 MPASS(ctnio->cmd.opc == NVME_OPC_IDENTIFY); 10753 10754 /* 10755 * The data buffer for Identify is always 4096 bytes, see 10756 * 5.51.1 in NVMe base specification 1.4. 10757 */ 10758 len = 4096; 10759 10760 ctnio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 10761 ctnio->kern_data_len = len; 10762 ctnio->kern_total_len = len; 10763 ctnio->kern_rel_offset = 0; 10764 ctnio->kern_sg_entries = 0; 10765 10766 ctl_nvme_set_success(ctnio); 10767 ctnio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10768 ctnio->be_move_done = ctl_config_move_done; 10769 10770 /* 10771 * If we don't have a LUN, return an empty result for CNS == 0. 10772 */ 10773 if (lun == NULL) { 10774 cns = le32toh(ctnio->cmd.cdw10) & 0xff; 10775 switch (cns) { 10776 case 0: 10777 memset(ctnio->kern_data_ptr, 0, len); 10778 ctl_datamove((union ctl_io *)ctnio); 10779 break; 10780 default: 10781 ctl_nvme_set_invalid_field(ctnio); 10782 break; 10783 } 10784 return (CTL_RETVAL_COMPLETE); 10785 } 10786 10787 retval = lun->backend->config_read((union ctl_io *)ctnio); 10788 return (retval); 10789 } 10790 10791 int 10792 ctl_nvme_flush(struct ctl_nvmeio *ctnio) 10793 { 10794 struct ctl_lun *lun = CTL_LUN(ctnio); 10795 int retval; 10796 10797 CTL_DEBUG_PRINT(("ctl_nvme_flush\n")); 10798 10799 CTL_IO_ASSERT(ctnio, NVME); 10800 MPASS(ctnio->cmd.opc == NVME_OPC_FLUSH); 10801 10802 /* 10803 * NVMe flushes always flush the entire namespace, not an LBA 10804 * range. 10805 */ 10806 retval = lun->backend->config_write((union ctl_io *)ctnio); 10807 10808 return (retval); 10809 } 10810 10811 int 10812 ctl_nvme_read_write(struct ctl_nvmeio *ctnio) 10813 { 10814 struct ctl_lun *lun = CTL_LUN(ctnio); 10815 struct ctl_lba_len_flags *lbalen; 10816 uint64_t lba; 10817 uint32_t num_blocks; 10818 int flags, retval; 10819 bool isread; 10820 10821 CTL_DEBUG_PRINT(("ctl_nvme_read_write: command: %#x\n", 10822 ctnio->cmd.opc)); 10823 10824 CTL_IO_ASSERT(ctnio, NVME); 10825 MPASS(ctnio->cmd.opc == NVME_OPC_WRITE || 10826 ctnio->cmd.opc == NVME_OPC_READ); 10827 10828 flags = 0; 10829 isread = ctnio->cmd.opc == NVME_OPC_READ; 10830 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks); 10831 10832 /* 10833 * The first check is to make sure we're in bounds, the second 10834 * check is to catch wrap-around problems. If the lba + num blocks 10835 * is less than the lba, then we've wrapped around and the block 10836 * range is invalid anyway. 10837 */ 10838 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 10839 || ((lba + num_blocks) < lba)) { 10840 ctl_nvme_set_lba_out_of_range(ctnio); 10841 ctl_done((union ctl_io *)ctnio); 10842 return (CTL_RETVAL_COMPLETE); 10843 } 10844 10845 /* 10846 * Set FUA and/or DPO if caches are disabled. 10847 * 10848 * For a read this may not be quite correct for the block 10849 * backend as any earlier writes to the LBA range should be 10850 * flushed to backing store as part of the read. 10851 */ 10852 if (ctl_nvme_fua(ctnio)) { 10853 flags |= CTL_LLF_FUA; 10854 if (isread) 10855 flags |= CTL_LLF_DPO; 10856 } 10857 10858 lbalen = (struct ctl_lba_len_flags *) 10859 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10860 lbalen->lba = lba; 10861 lbalen->len = num_blocks; 10862 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 10863 10864 ctnio->kern_total_len = num_blocks * lun->be_lun->blocksize; 10865 ctnio->kern_rel_offset = 0; 10866 10867 CTL_DEBUG_PRINT(("ctl_nvme_read_write: calling data_submit()\n")); 10868 10869 retval = lun->backend->data_submit((union ctl_io *)ctnio); 10870 return (retval); 10871 } 10872 10873 int 10874 ctl_nvme_write_uncorrectable(struct ctl_nvmeio *ctnio) 10875 { 10876 struct ctl_lun *lun = CTL_LUN(ctnio); 10877 struct ctl_lba_len_flags *lbalen; 10878 uint64_t lba; 10879 uint32_t num_blocks; 10880 int retval; 10881 10882 CTL_DEBUG_PRINT(("ctl_nvme_write_uncorrectable\n")); 10883 10884 CTL_IO_ASSERT(ctnio, NVME); 10885 MPASS(ctnio->cmd.opc == NVME_OPC_WRITE_UNCORRECTABLE); 10886 10887 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks); 10888 10889 /* 10890 * The first check is to make sure we're in bounds, the second 10891 * check is to catch wrap-around problems. If the lba + num blocks 10892 * is less than the lba, then we've wrapped around and the block 10893 * range is invalid anyway. 10894 */ 10895 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 10896 || ((lba + num_blocks) < lba)) { 10897 ctl_nvme_set_lba_out_of_range(ctnio); 10898 ctl_done((union ctl_io *)ctnio); 10899 return (CTL_RETVAL_COMPLETE); 10900 } 10901 10902 lbalen = (struct ctl_lba_len_flags *) 10903 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10904 lbalen->lba = lba; 10905 lbalen->len = num_blocks; 10906 lbalen->flags = 0; 10907 retval = lun->backend->config_write((union ctl_io *)ctnio); 10908 10909 return (retval); 10910 } 10911 10912 int 10913 ctl_nvme_compare(struct ctl_nvmeio *ctnio) 10914 { 10915 struct ctl_lun *lun = CTL_LUN(ctnio); 10916 struct ctl_lba_len_flags *lbalen; 10917 uint64_t lba; 10918 uint32_t num_blocks; 10919 int flags; 10920 int retval; 10921 10922 CTL_DEBUG_PRINT(("ctl_nvme_compare\n")); 10923 10924 CTL_IO_ASSERT(ctnio, NVME); 10925 MPASS(ctnio->cmd.opc == NVME_OPC_COMPARE); 10926 10927 flags = 0; 10928 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks); 10929 if (ctl_nvme_fua(ctnio)) 10930 flags |= CTL_LLF_FUA; 10931 10932 /* 10933 * The first check is to make sure we're in bounds, the second 10934 * check is to catch wrap-around problems. If the lba + num blocks 10935 * is less than the lba, then we've wrapped around and the block 10936 * range is invalid anyway. 10937 */ 10938 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 10939 || ((lba + num_blocks) < lba)) { 10940 ctl_nvme_set_lba_out_of_range(ctnio); 10941 ctl_done((union ctl_io *)ctnio); 10942 return (CTL_RETVAL_COMPLETE); 10943 } 10944 10945 lbalen = (struct ctl_lba_len_flags *) 10946 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10947 lbalen->lba = lba; 10948 lbalen->len = num_blocks; 10949 lbalen->flags = CTL_LLF_COMPARE | flags; 10950 ctnio->kern_total_len = num_blocks * lun->be_lun->blocksize; 10951 ctnio->kern_rel_offset = 0; 10952 10953 CTL_DEBUG_PRINT(("ctl_nvme_compare: calling data_submit()\n")); 10954 retval = lun->backend->data_submit((union ctl_io *)ctnio); 10955 return (retval); 10956 } 10957 10958 int 10959 ctl_nvme_write_zeroes(struct ctl_nvmeio *ctnio) 10960 { 10961 struct ctl_lun *lun = CTL_LUN(ctnio); 10962 struct ctl_lba_len_flags *lbalen; 10963 uint64_t lba; 10964 uint32_t num_blocks; 10965 int retval; 10966 10967 CTL_DEBUG_PRINT(("ctl_nvme_write_zeroes\n")); 10968 10969 CTL_IO_ASSERT(ctnio, NVME); 10970 MPASS(ctnio->cmd.opc == NVME_OPC_WRITE_ZEROES); 10971 10972 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks); 10973 10974 /* 10975 * The first check is to make sure we're in bounds, the second 10976 * check is to catch wrap-around problems. If the lba + num blocks 10977 * is less than the lba, then we've wrapped around and the block 10978 * range is invalid anyway. 10979 */ 10980 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 10981 || ((lba + num_blocks) < lba)) { 10982 ctl_nvme_set_lba_out_of_range(ctnio); 10983 ctl_done((union ctl_io *)ctnio); 10984 return (CTL_RETVAL_COMPLETE); 10985 } 10986 10987 lbalen = (struct ctl_lba_len_flags *) 10988 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10989 lbalen->lba = lba; 10990 lbalen->len = num_blocks; 10991 lbalen->flags = 0; 10992 retval = lun->backend->config_write((union ctl_io *)ctnio); 10993 10994 return (retval); 10995 } 10996 10997 int 10998 ctl_nvme_dataset_management(struct ctl_nvmeio *ctnio) 10999 { 11000 struct ctl_lun *lun = CTL_LUN(ctnio); 11001 struct nvme_dsm_range *r; 11002 uint64_t lba; 11003 uint32_t len, num_blocks; 11004 u_int i, ranges; 11005 int retval; 11006 11007 CTL_DEBUG_PRINT(("ctl_nvme_dataset_management\n")); 11008 11009 CTL_IO_ASSERT(ctnio, NVME); 11010 MPASS(ctnio->cmd.opc == NVME_OPC_DATASET_MANAGEMENT); 11011 11012 ranges = le32toh(ctnio->cmd.cdw10) & 0xff; 11013 len = ranges * sizeof(struct nvme_dsm_range); 11014 11015 /* 11016 * If we've got a kernel request that hasn't been malloced yet, 11017 * malloc it and tell the caller the data buffer is here. 11018 */ 11019 if ((ctnio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 11020 ctnio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 11021 ctnio->kern_data_len = len; 11022 ctnio->kern_total_len = len; 11023 ctnio->kern_rel_offset = 0; 11024 ctnio->kern_sg_entries = 0; 11025 ctnio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 11026 ctnio->be_move_done = ctl_config_move_done; 11027 ctl_datamove((union ctl_io *)ctnio); 11028 11029 return (CTL_RETVAL_COMPLETE); 11030 } 11031 11032 /* 11033 * Require a flat buffer of the correct size. 11034 */ 11035 if (ctnio->kern_sg_entries > 0 || 11036 ctnio->kern_total_len - ctnio->kern_data_resid != len) 11037 return (CTL_RETVAL_ERROR); 11038 11039 /* 11040 * Verify that none of the ranges are out of bounds. 11041 */ 11042 r = (struct nvme_dsm_range *)ctnio->kern_data_ptr; 11043 for (i = 0; i < ranges; i++) { 11044 lba = le64toh(r[i].starting_lba); 11045 num_blocks = le32toh(r[i].length); 11046 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 11047 || ((lba + num_blocks) < lba)) { 11048 ctl_nvme_set_lba_out_of_range(ctnio); 11049 ctl_done((union ctl_io *)ctnio); 11050 return (CTL_RETVAL_COMPLETE); 11051 } 11052 } 11053 11054 CTL_DEBUG_PRINT(("ctl_nvme_dataset_management: calling config_write()\n")); 11055 retval = lun->backend->config_write((union ctl_io *)ctnio); 11056 return (retval); 11057 } 11058 11059 int 11060 ctl_nvme_verify(struct ctl_nvmeio *ctnio) 11061 { 11062 struct ctl_lun *lun = CTL_LUN(ctnio); 11063 struct ctl_lba_len_flags *lbalen; 11064 uint64_t lba; 11065 uint32_t num_blocks; 11066 int flags; 11067 int retval; 11068 11069 CTL_DEBUG_PRINT(("ctl_nvme_verify\n")); 11070 11071 CTL_IO_ASSERT(ctnio, NVME); 11072 MPASS(ctnio->cmd.opc == NVME_OPC_VERIFY); 11073 11074 flags = 0; 11075 ctl_nvme_get_lba_len(ctnio, &lba, &num_blocks); 11076 if (ctl_nvme_fua(ctnio)) 11077 flags |= CTL_LLF_FUA; 11078 11079 /* 11080 * The first check is to make sure we're in bounds, the second 11081 * check is to catch wrap-around problems. If the lba + num blocks 11082 * is less than the lba, then we've wrapped around and the block 11083 * range is invalid anyway. 11084 */ 11085 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 11086 || ((lba + num_blocks) < lba)) { 11087 ctl_nvme_set_lba_out_of_range(ctnio); 11088 ctl_done((union ctl_io *)ctnio); 11089 return (CTL_RETVAL_COMPLETE); 11090 } 11091 11092 lbalen = (struct ctl_lba_len_flags *) 11093 &ctnio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 11094 lbalen->lba = lba; 11095 lbalen->len = num_blocks; 11096 lbalen->flags = CTL_LLF_VERIFY | flags; 11097 ctnio->kern_total_len = 0; 11098 ctnio->kern_rel_offset = 0; 11099 11100 CTL_DEBUG_PRINT(("ctl_nvme_verify: calling data_submit()\n")); 11101 retval = lun->backend->data_submit((union ctl_io *)ctnio); 11102 return (retval); 11103 } 11104 11105 static const struct ctl_nvme_cmd_entry * 11106 ctl_nvme_get_cmd_entry(struct ctl_nvmeio *ctnio) 11107 { 11108 const struct ctl_nvme_cmd_entry *entry; 11109 11110 switch (ctnio->io_hdr.io_type) { 11111 case CTL_IO_NVME: 11112 entry = &nvme_nvm_cmd_table[ctnio->cmd.opc]; 11113 break; 11114 case CTL_IO_NVME_ADMIN: 11115 entry = &nvme_admin_cmd_table[ctnio->cmd.opc]; 11116 break; 11117 default: 11118 __assert_unreachable(); 11119 } 11120 return (entry); 11121 } 11122 11123 static const struct ctl_nvme_cmd_entry * 11124 ctl_nvme_validate_command(struct ctl_nvmeio *ctnio) 11125 { 11126 const struct ctl_nvme_cmd_entry *entry; 11127 11128 entry = ctl_nvme_get_cmd_entry(ctnio); 11129 if (entry->execute == NULL) { 11130 ctl_nvme_set_invalid_opcode(ctnio); 11131 ctl_done((union ctl_io *)ctnio); 11132 return (NULL); 11133 } 11134 11135 /* Validate fused commands. */ 11136 switch (NVMEV(NVME_CMD_FUSE, ctnio->cmd.fuse)) { 11137 case NVME_FUSE_NORMAL: 11138 break; 11139 case NVME_FUSE_FIRST: 11140 if (ctnio->io_hdr.io_type != CTL_IO_NVME || 11141 ctnio->cmd.opc != NVME_OPC_COMPARE) { 11142 ctl_nvme_set_invalid_field(ctnio); 11143 ctl_done((union ctl_io *)ctnio); 11144 return (NULL); 11145 } 11146 break; 11147 case NVME_FUSE_SECOND: 11148 if (ctnio->io_hdr.io_type != CTL_IO_NVME || 11149 ctnio->cmd.opc != NVME_OPC_COMPARE) { 11150 ctl_nvme_set_invalid_field(ctnio); 11151 ctl_done((union ctl_io *)ctnio); 11152 return (NULL); 11153 } 11154 break; 11155 default: 11156 ctl_nvme_set_invalid_field(ctnio); 11157 ctl_done((union ctl_io *)ctnio); 11158 return (NULL); 11159 } 11160 11161 return (entry); 11162 } 11163 11164 /* 11165 * This is a simpler version of ctl_scsiio_lun_check that fails 11166 * requests on a LUN without active media. 11167 * 11168 * Returns true if the command has been completed with an error. 11169 */ 11170 static bool 11171 ctl_nvmeio_lun_check(struct ctl_lun *lun, 11172 const struct ctl_nvme_cmd_entry *entry, struct ctl_nvmeio *ctnio) 11173 { 11174 mtx_assert(&lun->lun_lock, MA_OWNED); 11175 11176 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11177 if ((lun->flags & (CTL_LUN_EJECTED | CTL_LUN_NO_MEDIA | 11178 CTL_LUN_STOPPED)) != 0) { 11179 ctl_nvme_set_namespace_not_ready(ctnio); 11180 return (true); 11181 } 11182 } 11183 11184 return (false); 11185 } 11186 11187 /* 11188 * Check for blockage against the OOA (Order Of Arrival) queue. 11189 * Assumptions: 11190 * - pending_io is generally either incoming, or on the blocked queue 11191 * - starting I/O is the I/O we want to start the check with. 11192 */ 11193 static ctl_action 11194 ctl_nvme_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11195 union ctl_io **starting_io, union ctl_io **aborted_io) 11196 { 11197 union ctl_io *ooa_io = *starting_io; 11198 11199 CTL_IO_ASSERT(pending_io, NVME, NVME_ADMIN); 11200 11201 mtx_assert(&lun->lun_lock, MA_OWNED); 11202 11203 *aborted_io = NULL; 11204 11205 /* 11206 * Aborted commands are not going to be executed and may even 11207 * not report completion, so we don't care about their order. 11208 * Let them complete ASAP to clean the OOA queue. 11209 */ 11210 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT)) 11211 return (CTL_ACTION_PASS); 11212 11213 /* 11214 * NVMe has rather simple command ordering requirements. In 11215 * particular, there is no requirement on the controller to 11216 * enforce a specific order for overlapping LBAs. The only 11217 * constraint is that fused operations (Compare and Write), 11218 * must be completed as a unit. 11219 * 11220 * To support fused operations, the following strategy is used: 11221 * - the first half of a fused command is not enqueued to rtr 11222 * until the second half is enqueued 11223 * - the second half of a fused command blocks on the first 11224 * half of a fuse command 11225 * - subsequent commands block on the second half of the 11226 * fused command 11227 */ 11228 11229 /* 11230 * Is the previously submitted command the first half of a 11231 * fused operation? 11232 */ 11233 if (ooa_io != NULL && 11234 NVMEV(NVME_CMD_FUSE, ooa_io->nvmeio.cmd.fuse) == NVME_FUSE_FIRST) { 11235 /* 11236 * If this is the second half, enqueue the first half 11237 * and block the second half on the first half. 11238 */ 11239 if (NVMEV(NVME_CMD_FUSE, pending_io->nvmeio.cmd.fuse) == 11240 NVME_FUSE_SECOND) { 11241 /* 11242 * XXX: Do we need to wait for other rtr requests 11243 * to drain so this is truly atomic? 11244 */ 11245 return (CTL_ACTION_FUSED); 11246 } 11247 11248 /* Abort the first half. */ 11249 ctl_nvme_set_missing_fused_command(&ooa_io->nvmeio); 11250 *aborted_io = ooa_io; 11251 } else { 11252 switch (NVMEV(NVME_CMD_FUSE, pending_io->nvmeio.cmd.fuse)) { 11253 case NVME_FUSE_FIRST: 11254 /* First half, wait for the second half. */ 11255 return (CTL_ACTION_SKIP); 11256 case NVME_FUSE_SECOND: 11257 /* Second half without a matching first half, abort. */ 11258 ctl_nvme_set_missing_fused_command(&pending_io->nvmeio); 11259 *aborted_io = pending_io; 11260 return (CTL_ACTION_SKIP); 11261 } 11262 } 11263 11264 /* 11265 * Scan the OOA queue looking for the most recent second half 11266 * of a fused op. 11267 */ 11268 for (; ooa_io != NULL; 11269 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) { 11270 if (NVMEV(NVME_CMD_FUSE, ooa_io->nvmeio.cmd.fuse) == 11271 NVME_FUSE_SECOND) { 11272 *starting_io = ooa_io; 11273 return (CTL_ACTION_BLOCK); 11274 } 11275 } 11276 11277 *starting_io = NULL; 11278 return (CTL_ACTION_PASS); 11279 } 11280 11281 static void 11282 ctl_nvmeio_precheck(struct ctl_nvmeio *ctnio) 11283 { 11284 struct ctl_softc *softc = CTL_SOFTC(ctnio); 11285 struct ctl_lun *lun; 11286 const struct ctl_nvme_cmd_entry *entry; 11287 union ctl_io *bio, *aborted_io; 11288 uint32_t targ_lun; 11289 11290 lun = NULL; 11291 targ_lun = ctnio->io_hdr.nexus.targ_mapped_lun; 11292 if (targ_lun < ctl_max_luns) 11293 lun = softc->ctl_luns[targ_lun]; 11294 if (lun != NULL) { 11295 /* 11296 * If the LUN is invalid, pretend that it doesn't exist. 11297 * It will go away as soon as all pending I/O has been 11298 * completed. 11299 */ 11300 mtx_lock(&lun->lun_lock); 11301 if (lun->flags & CTL_LUN_DISABLED) { 11302 mtx_unlock(&lun->lun_lock); 11303 lun = NULL; 11304 } 11305 } 11306 CTL_LUN(ctnio) = lun; 11307 if (lun != NULL) { 11308 CTL_BACKEND_LUN(ctnio) = lun->be_lun; 11309 11310 /* 11311 * Every I/O goes into the OOA queue for a particular LUN, 11312 * and stays there until completion. 11313 */ 11314 #ifdef CTL_TIME_IO 11315 if (LIST_EMPTY(&lun->ooa_queue)) 11316 lun->idle_time += getsbinuptime() - lun->last_busy; 11317 #endif 11318 LIST_INSERT_HEAD(&lun->ooa_queue, &ctnio->io_hdr, ooa_links); 11319 } 11320 11321 /* Get command entry and return error if it is unsupported. */ 11322 entry = ctl_nvme_validate_command(ctnio); 11323 if (entry == NULL) { 11324 if (lun) 11325 mtx_unlock(&lun->lun_lock); 11326 return; 11327 } 11328 11329 ctnio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11330 ctnio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11331 11332 /* All NVMe commands other than IDENTIFY require a LUN. */ 11333 if (lun == NULL) { 11334 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11335 ctnio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11336 ctl_enqueue_rtr((union ctl_io *)ctnio); 11337 return; 11338 } 11339 11340 ctl_nvme_set_invalid_namespace(ctnio); 11341 ctl_done((union ctl_io *)ctnio); 11342 CTL_DEBUG_PRINT(("ctl_nvmeio_precheck: bailing out due to invalid LUN\n")); 11343 return; 11344 } else { 11345 /* 11346 * NVMe namespaces can only be backed by T_DIRECT LUNs. 11347 */ 11348 if (lun->be_lun->lun_type != T_DIRECT) { 11349 mtx_unlock(&lun->lun_lock); 11350 ctl_nvme_set_invalid_namespace(ctnio); 11351 ctl_done((union ctl_io *)ctnio); 11352 return; 11353 } 11354 } 11355 11356 if (ctl_nvmeio_lun_check(lun, entry, ctnio) != 0) { 11357 mtx_unlock(&lun->lun_lock); 11358 ctl_done((union ctl_io *)ctnio); 11359 return; 11360 } 11361 11362 bio = (union ctl_io *)LIST_NEXT(&ctnio->io_hdr, ooa_links); 11363 switch (ctl_nvme_check_ooa(lun, (union ctl_io *)ctnio, &bio, 11364 &aborted_io)) { 11365 case CTL_ACTION_PASS: 11366 ctnio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11367 mtx_unlock(&lun->lun_lock); 11368 ctl_enqueue_rtr((union ctl_io *)ctnio); 11369 break; 11370 case CTL_ACTION_FUSED: 11371 /* Block the second half on the first half. */ 11372 ctnio->io_hdr.blocker = bio; 11373 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctnio->io_hdr, 11374 blocked_links); 11375 11376 /* Pass the first half. */ 11377 bio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11378 mtx_unlock(&lun->lun_lock); 11379 ctl_enqueue_rtr(bio); 11380 break; 11381 case CTL_ACTION_SKIP: 11382 mtx_unlock(&lun->lun_lock); 11383 break; 11384 case CTL_ACTION_BLOCK: 11385 ctnio->io_hdr.blocker = bio; 11386 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctnio->io_hdr, 11387 blocked_links); 11388 mtx_unlock(&lun->lun_lock); 11389 break; 11390 default: 11391 __assert_unreachable(); 11392 } 11393 if (aborted_io != NULL) 11394 ctl_done(aborted_io); 11395 } 11396 11397 static int 11398 ctl_nvmeio(struct ctl_nvmeio *ctnio) 11399 { 11400 const struct ctl_nvme_cmd_entry *entry; 11401 int retval; 11402 11403 CTL_DEBUG_PRINT(("ctl_nvmeio %s opc=%02X\n", 11404 ctnio->io_hdr.io_type == CTL_IO_NVME ? "nvm" : "admin", 11405 ctnio->cmd.opc)); 11406 11407 entry = ctl_nvme_get_cmd_entry(ctnio); 11408 MPASS(entry != NULL); 11409 11410 /* 11411 * If this I/O has been aborted, just send it straight to 11412 * ctl_done() without executing it. 11413 */ 11414 if (ctnio->io_hdr.flags & CTL_FLAG_ABORT) { 11415 ctl_done((union ctl_io *)ctnio); 11416 return (CTL_RETVAL_COMPLETE); 11417 } 11418 11419 /* 11420 * All the checks should have been handled by ctl_nvmeio_precheck(). 11421 * We should be clear now to just execute the I/O. 11422 */ 11423 retval = entry->execute(ctnio); 11424 11425 return (retval); 11426 } 11427 11428 /* 11429 * For known CDB types, parse the LBA and length. 11430 */ 11431 static int 11432 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 11433 { 11434 11435 CTL_IO_ASSERT(io, SCSI); 11436 11437 switch (io->scsiio.cdb[0]) { 11438 case COMPARE_AND_WRITE: { 11439 struct scsi_compare_and_write *cdb; 11440 11441 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 11442 11443 *lba = scsi_8btou64(cdb->addr); 11444 *len = cdb->length; 11445 break; 11446 } 11447 case READ_6: 11448 case WRITE_6: { 11449 struct scsi_rw_6 *cdb; 11450 11451 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 11452 11453 *lba = scsi_3btoul(cdb->addr); 11454 /* only 5 bits are valid in the most significant address byte */ 11455 *lba &= 0x1fffff; 11456 *len = cdb->length; 11457 break; 11458 } 11459 case READ_10: 11460 case WRITE_10: { 11461 struct scsi_rw_10 *cdb; 11462 11463 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 11464 11465 *lba = scsi_4btoul(cdb->addr); 11466 *len = scsi_2btoul(cdb->length); 11467 break; 11468 } 11469 case WRITE_VERIFY_10: { 11470 struct scsi_write_verify_10 *cdb; 11471 11472 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 11473 11474 *lba = scsi_4btoul(cdb->addr); 11475 *len = scsi_2btoul(cdb->length); 11476 break; 11477 } 11478 case READ_12: 11479 case WRITE_12: { 11480 struct scsi_rw_12 *cdb; 11481 11482 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 11483 11484 *lba = scsi_4btoul(cdb->addr); 11485 *len = scsi_4btoul(cdb->length); 11486 break; 11487 } 11488 case WRITE_VERIFY_12: { 11489 struct scsi_write_verify_12 *cdb; 11490 11491 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 11492 11493 *lba = scsi_4btoul(cdb->addr); 11494 *len = scsi_4btoul(cdb->length); 11495 break; 11496 } 11497 case READ_16: 11498 case WRITE_16: { 11499 struct scsi_rw_16 *cdb; 11500 11501 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 11502 11503 *lba = scsi_8btou64(cdb->addr); 11504 *len = scsi_4btoul(cdb->length); 11505 break; 11506 } 11507 case WRITE_ATOMIC_16: { 11508 struct scsi_write_atomic_16 *cdb; 11509 11510 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 11511 11512 *lba = scsi_8btou64(cdb->addr); 11513 *len = scsi_2btoul(cdb->length); 11514 break; 11515 } 11516 case WRITE_VERIFY_16: { 11517 struct scsi_write_verify_16 *cdb; 11518 11519 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 11520 11521 *lba = scsi_8btou64(cdb->addr); 11522 *len = scsi_4btoul(cdb->length); 11523 break; 11524 } 11525 case WRITE_SAME_10: { 11526 struct scsi_write_same_10 *cdb; 11527 11528 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 11529 11530 *lba = scsi_4btoul(cdb->addr); 11531 *len = scsi_2btoul(cdb->length); 11532 break; 11533 } 11534 case WRITE_SAME_16: { 11535 struct scsi_write_same_16 *cdb; 11536 11537 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 11538 11539 *lba = scsi_8btou64(cdb->addr); 11540 *len = scsi_4btoul(cdb->length); 11541 break; 11542 } 11543 case VERIFY_10: { 11544 struct scsi_verify_10 *cdb; 11545 11546 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 11547 11548 *lba = scsi_4btoul(cdb->addr); 11549 *len = scsi_2btoul(cdb->length); 11550 break; 11551 } 11552 case VERIFY_12: { 11553 struct scsi_verify_12 *cdb; 11554 11555 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 11556 11557 *lba = scsi_4btoul(cdb->addr); 11558 *len = scsi_4btoul(cdb->length); 11559 break; 11560 } 11561 case VERIFY_16: { 11562 struct scsi_verify_16 *cdb; 11563 11564 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 11565 11566 *lba = scsi_8btou64(cdb->addr); 11567 *len = scsi_4btoul(cdb->length); 11568 break; 11569 } 11570 case UNMAP: { 11571 *lba = 0; 11572 *len = UINT64_MAX; 11573 break; 11574 } 11575 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 11576 struct scsi_get_lba_status *cdb; 11577 11578 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 11579 *lba = scsi_8btou64(cdb->addr); 11580 *len = UINT32_MAX; 11581 break; 11582 } 11583 default: 11584 *lba = 0; 11585 *len = UINT64_MAX; 11586 return (1); 11587 } 11588 11589 return (0); 11590 } 11591 11592 static ctl_action 11593 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 11594 bool seq) 11595 { 11596 uint64_t endlba1, endlba2; 11597 11598 endlba1 = lba1 + len1 - (seq ? 0 : 1); 11599 endlba2 = lba2 + len2 - 1; 11600 11601 if ((endlba1 < lba2) || (endlba2 < lba1)) 11602 return (CTL_ACTION_PASS); 11603 else 11604 return (CTL_ACTION_BLOCK); 11605 } 11606 11607 static int 11608 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 11609 { 11610 struct ctl_ptr_len_flags *ptrlen; 11611 struct scsi_unmap_desc *buf, *end, *range; 11612 uint64_t lba; 11613 uint32_t len; 11614 11615 CTL_IO_ASSERT(io, SCSI); 11616 11617 /* If not UNMAP -- go other way. */ 11618 if (io->scsiio.cdb[0] != UNMAP) 11619 return (CTL_ACTION_SKIP); 11620 11621 /* If UNMAP without data -- block and wait for data. */ 11622 ptrlen = (struct ctl_ptr_len_flags *) 11623 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 11624 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 11625 ptrlen->ptr == NULL) 11626 return (CTL_ACTION_BLOCK); 11627 11628 /* UNMAP with data -- check for collision. */ 11629 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 11630 end = buf + ptrlen->len / sizeof(*buf); 11631 for (range = buf; range < end; range++) { 11632 lba = scsi_8btou64(range->lba); 11633 len = scsi_4btoul(range->length); 11634 if ((lba < lba2 + len2) && (lba + len > lba2)) 11635 return (CTL_ACTION_BLOCK); 11636 } 11637 return (CTL_ACTION_PASS); 11638 } 11639 11640 static ctl_action 11641 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 11642 { 11643 uint64_t lba1, lba2; 11644 uint64_t len1, len2; 11645 int retval; 11646 11647 retval = ctl_get_lba_len(io2, &lba2, &len2); 11648 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 11649 11650 retval = ctl_extent_check_unmap(io1, lba2, len2); 11651 if (retval != CTL_ACTION_SKIP) 11652 return (retval); 11653 11654 retval = ctl_get_lba_len(io1, &lba1, &len1); 11655 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 11656 11657 if (seq && (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)) 11658 seq = FALSE; 11659 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 11660 } 11661 11662 static ctl_action 11663 ctl_seq_check(union ctl_io *io1, union ctl_io *io2) 11664 { 11665 uint64_t lba1, lba2; 11666 uint64_t len1, len2; 11667 int retval __diagused; 11668 11669 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 11670 return (CTL_ACTION_PASS); 11671 retval = ctl_get_lba_len(io1, &lba1, &len1); 11672 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 11673 retval = ctl_get_lba_len(io2, &lba2, &len2); 11674 KASSERT(retval == 0, ("ctl_get_lba_len() error")); 11675 11676 if (lba1 + len1 == lba2) 11677 return (CTL_ACTION_BLOCK); 11678 return (CTL_ACTION_PASS); 11679 } 11680 11681 static ctl_action 11682 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 11683 const uint8_t *serialize_row, union ctl_io *ooa_io) 11684 { 11685 CTL_IO_ASSERT(pending_io, SCSI); 11686 CTL_IO_ASSERT(ooa_io, SCSI); 11687 11688 /* 11689 * The initiator attempted multiple untagged commands at the same 11690 * time. Can't do that. 11691 */ 11692 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11693 && __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11694 && ((pending_io->io_hdr.nexus.targ_port == 11695 ooa_io->io_hdr.nexus.targ_port) 11696 && (pending_io->io_hdr.nexus.initid == 11697 ooa_io->io_hdr.nexus.initid)) 11698 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11699 CTL_FLAG_STATUS_SENT)) == 0)) 11700 return (CTL_ACTION_OVERLAP); 11701 11702 /* 11703 * The initiator attempted to send multiple tagged commands with 11704 * the same ID. (It's fine if different initiators have the same 11705 * tag ID.) 11706 * 11707 * Even if all of those conditions are true, we don't kill the I/O 11708 * if the command ahead of us has been aborted. We won't end up 11709 * sending it to the FETD, and it's perfectly legal to resend a 11710 * command with the same tag number as long as the previous 11711 * instance of this tag number has been aborted somehow. 11712 */ 11713 if (__predict_true(pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11714 && __predict_true(ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11715 && __predict_false(pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11716 && ((pending_io->io_hdr.nexus.targ_port == 11717 ooa_io->io_hdr.nexus.targ_port) 11718 && (pending_io->io_hdr.nexus.initid == 11719 ooa_io->io_hdr.nexus.initid)) 11720 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11721 CTL_FLAG_STATUS_SENT)) == 0)) 11722 return (CTL_ACTION_OVERLAP_TAG); 11723 11724 /* 11725 * If we get a head of queue tag, SAM-3 says that we should 11726 * immediately execute it. 11727 * 11728 * What happens if this command would normally block for some other 11729 * reason? e.g. a request sense with a head of queue tag 11730 * immediately after a write. Normally that would block, but this 11731 * will result in its getting executed immediately... 11732 * 11733 * We currently return "pass" instead of "skip", so we'll end up 11734 * going through the rest of the queue to check for overlapped tags. 11735 * 11736 * XXX KDM check for other types of blockage first?? 11737 */ 11738 if (__predict_false(pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 11739 return (CTL_ACTION_PASS); 11740 11741 /* 11742 * Simple tags get blocked until all head of queue and ordered tags 11743 * ahead of them have completed. I'm lumping untagged commands in 11744 * with simple tags here. XXX KDM is that the right thing to do? 11745 */ 11746 if (__predict_false(ooa_io->scsiio.tag_type == CTL_TAG_ORDERED) || 11747 __predict_false(ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 11748 return (CTL_ACTION_BLOCK); 11749 11750 /* Unsupported command in OOA queue. */ 11751 if (__predict_false(ooa_io->scsiio.seridx == CTL_SERIDX_INVLD)) 11752 return (CTL_ACTION_PASS); 11753 11754 switch (serialize_row[ooa_io->scsiio.seridx]) { 11755 case CTL_SER_SEQ: 11756 if (lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11757 return (ctl_seq_check(ooa_io, pending_io)); 11758 /* FALLTHROUGH */ 11759 case CTL_SER_PASS: 11760 return (CTL_ACTION_PASS); 11761 case CTL_SER_EXTENTOPT: 11762 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 11763 SCP_QUEUE_ALG_UNRESTRICTED) 11764 return (CTL_ACTION_PASS); 11765 /* FALLTHROUGH */ 11766 case CTL_SER_EXTENT: 11767 return (ctl_extent_check(ooa_io, pending_io, 11768 (lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11769 case CTL_SER_BLOCKOPT: 11770 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) == 11771 SCP_QUEUE_ALG_UNRESTRICTED) 11772 return (CTL_ACTION_PASS); 11773 /* FALLTHROUGH */ 11774 case CTL_SER_BLOCK: 11775 return (CTL_ACTION_BLOCK); 11776 default: 11777 __assert_unreachable(); 11778 } 11779 } 11780 11781 /* 11782 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11783 * Assumptions: 11784 * - pending_io is generally either incoming, or on the blocked queue 11785 * - starting I/O is the I/O we want to start the check with. 11786 */ 11787 static ctl_action 11788 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11789 union ctl_io **starting_io) 11790 { 11791 union ctl_io *ooa_io = *starting_io; 11792 const uint8_t *serialize_row; 11793 ctl_action action; 11794 11795 CTL_IO_ASSERT(pending_io, SCSI); 11796 11797 mtx_assert(&lun->lun_lock, MA_OWNED); 11798 11799 /* 11800 * Aborted commands are not going to be executed and may even 11801 * not report completion, so we don't care about their order. 11802 * Let them complete ASAP to clean the OOA queue. 11803 */ 11804 if (__predict_false(pending_io->io_hdr.flags & CTL_FLAG_ABORT)) 11805 return (CTL_ACTION_SKIP); 11806 11807 /* 11808 * Ordered tags have to block until all items ahead of them have 11809 * completed. If we get called with an ordered tag, we always 11810 * block, if something else is ahead of us in the queue. 11811 */ 11812 if ((pending_io->scsiio.tag_type == CTL_TAG_ORDERED) && 11813 (ooa_io != NULL)) 11814 return (CTL_ACTION_BLOCK); 11815 11816 serialize_row = ctl_serialize_table[pending_io->scsiio.seridx]; 11817 11818 /* 11819 * Run back along the OOA queue, starting with the current 11820 * blocked I/O and going through every I/O before it on the 11821 * queue. If starting_io is NULL, we'll just end up returning 11822 * CTL_ACTION_PASS. 11823 */ 11824 for (; ooa_io != NULL; 11825 ooa_io = (union ctl_io *)LIST_NEXT(&ooa_io->io_hdr, ooa_links)) { 11826 action = ctl_check_for_blockage(lun, pending_io, serialize_row, 11827 ooa_io); 11828 if (action != CTL_ACTION_PASS) { 11829 *starting_io = ooa_io; 11830 return (action); 11831 } 11832 } 11833 11834 *starting_io = NULL; 11835 return (CTL_ACTION_PASS); 11836 } 11837 11838 /* 11839 * Try to unblock the specified I/O. 11840 * 11841 * skip parameter allows explicitly skip present blocker of the I/O, 11842 * starting from the previous one on OOA queue. It can be used when 11843 * we know for sure that the blocker I/O does no longer count. 11844 */ 11845 static void 11846 ctl_scsi_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11847 { 11848 struct ctl_softc *softc = lun->ctl_softc; 11849 union ctl_io *bio, *obio; 11850 const struct ctl_cmd_entry *entry; 11851 union ctl_ha_msg msg_info; 11852 ctl_action action; 11853 11854 CTL_IO_ASSERT(io, SCSI); 11855 11856 mtx_assert(&lun->lun_lock, MA_OWNED); 11857 11858 if (io->io_hdr.blocker == NULL) 11859 return; 11860 11861 obio = bio = io->io_hdr.blocker; 11862 if (skip) 11863 bio = (union ctl_io *)LIST_NEXT(&bio->io_hdr, ooa_links); 11864 action = ctl_check_ooa(lun, io, &bio); 11865 if (action == CTL_ACTION_BLOCK) { 11866 /* Still blocked, but may be by different I/O now. */ 11867 if (bio != obio) { 11868 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, 11869 &io->io_hdr, blocked_links); 11870 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, 11871 &io->io_hdr, blocked_links); 11872 io->io_hdr.blocker = bio; 11873 } 11874 return; 11875 } 11876 11877 /* No longer blocked, one way or another. */ 11878 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links); 11879 io->io_hdr.blocker = NULL; 11880 11881 switch (action) { 11882 case CTL_ACTION_PASS: 11883 case CTL_ACTION_SKIP: 11884 11885 /* Serializing commands from the other SC retire there. */ 11886 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11887 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11888 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11889 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11890 msg_info.hdr.serializing_sc = io; 11891 msg_info.hdr.msg_type = CTL_MSG_R2R; 11892 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11893 sizeof(msg_info.hdr), M_NOWAIT); 11894 break; 11895 } 11896 11897 /* 11898 * Check this I/O for LUN state changes that may have happened 11899 * while this command was blocked. The LUN state may have been 11900 * changed by a command ahead of us in the queue. 11901 */ 11902 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 11903 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 11904 ctl_done(io); 11905 break; 11906 } 11907 11908 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11909 ctl_enqueue_rtr(io); 11910 break; 11911 default: 11912 __assert_unreachable(); 11913 case CTL_ACTION_OVERLAP: 11914 ctl_set_overlapped_cmd(&io->scsiio); 11915 goto error; 11916 case CTL_ACTION_OVERLAP_TAG: 11917 ctl_set_overlapped_tag(&io->scsiio, 11918 io->scsiio.tag_num & 0xff); 11919 error: 11920 /* Serializing commands from the other SC are done here. */ 11921 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) && 11922 (softc->ha_mode != CTL_HA_MODE_XFER)) { 11923 ctl_try_unblock_others(lun, io, TRUE); 11924 LIST_REMOVE(&io->io_hdr, ooa_links); 11925 11926 ctl_copy_sense_data_back(io, &msg_info); 11927 msg_info.hdr.original_sc = io->io_hdr.remote_io; 11928 msg_info.hdr.serializing_sc = NULL; 11929 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 11930 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11931 sizeof(msg_info.scsi), M_WAITOK); 11932 ctl_free_io(io); 11933 break; 11934 } 11935 11936 ctl_done(io); 11937 break; 11938 } 11939 } 11940 11941 static void 11942 ctl_nvme_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11943 { 11944 union ctl_io *bio; 11945 const struct ctl_nvme_cmd_entry *entry; 11946 11947 CTL_IO_ASSERT(io, NVME, NVME_ADMIN); 11948 11949 mtx_assert(&lun->lun_lock, MA_OWNED); 11950 11951 if (io->io_hdr.blocker == NULL) 11952 return; 11953 11954 /* 11955 * If this is the second half of a fused operation, it should 11956 * be the only io on the blocked list. If the first half 11957 * failed, complete the second half with an appropriate error. 11958 */ 11959 bio = io->io_hdr.blocker; 11960 if (NVMEV(NVME_CMD_FUSE, io->nvmeio.cmd.fuse) == NVME_FUSE_SECOND) { 11961 MPASS(io == 11962 (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue)); 11963 MPASS(TAILQ_NEXT(&io->io_hdr, blocked_links) == NULL); 11964 11965 TAILQ_REMOVE(&bio->io_hdr.blocked_queue, &io->io_hdr, 11966 blocked_links); 11967 if (bio->io_hdr.status != CTL_SUCCESS) { 11968 ctl_nvme_set_failed_fused_command(&io->nvmeio); 11969 ctl_done(io); 11970 return; 11971 } 11972 } else { 11973 /* 11974 * This must be a command that was blocked on the 11975 * second half of a fused operation. 11976 */ 11977 MPASS(NVMEV(NVME_CMD_FUSE, bio->nvmeio.cmd.fuse) == 11978 NVME_FUSE_SECOND); 11979 TAILQ_REMOVE(&bio->io_hdr.blocked_queue, &io->io_hdr, 11980 blocked_links); 11981 } 11982 11983 entry = ctl_nvme_get_cmd_entry(&io->nvmeio); 11984 if (ctl_nvmeio_lun_check(lun, entry, &io->nvmeio) != 0) { 11985 ctl_done(io); 11986 return; 11987 } 11988 11989 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11990 ctl_enqueue_rtr(io); 11991 } 11992 11993 static void 11994 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip) 11995 { 11996 switch (io->io_hdr.io_type) { 11997 case CTL_IO_SCSI: 11998 return (ctl_scsi_try_unblock_io(lun, io, skip)); 11999 case CTL_IO_NVME: 12000 case CTL_IO_NVME_ADMIN: 12001 return (ctl_nvme_try_unblock_io(lun, io, skip)); 12002 default: 12003 __assert_unreachable(); 12004 } 12005 } 12006 12007 /* 12008 * Try to unblock I/Os blocked by the specified I/O. 12009 * 12010 * skip parameter allows explicitly skip the specified I/O as blocker, 12011 * starting from the previous one on the OOA queue. It can be used when 12012 * we know for sure that the specified I/O does no longer count (done). 12013 * It has to be still on OOA queue though so that we know where to start. 12014 */ 12015 static void 12016 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip) 12017 { 12018 union ctl_io *io, *next_io; 12019 12020 mtx_assert(&lun->lun_lock, MA_OWNED); 12021 12022 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue); 12023 io != NULL; io = next_io) { 12024 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links); 12025 12026 KASSERT(io->io_hdr.blocker != NULL, 12027 ("I/O %p on blocked list without blocker", io)); 12028 ctl_try_unblock_io(lun, io, skip); 12029 } 12030 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue), 12031 ("blocked_queue is not empty after skipping %p", bio)); 12032 } 12033 12034 /* 12035 * This routine (with one exception) checks LUN flags that can be set by 12036 * commands ahead of us in the OOA queue. These flags have to be checked 12037 * when a command initially comes in, and when we pull a command off the 12038 * blocked queue and are preparing to execute it. The reason we have to 12039 * check these flags for commands on the blocked queue is that the LUN 12040 * state may have been changed by a command ahead of us while we're on the 12041 * blocked queue. 12042 * 12043 * Ordering is somewhat important with these checks, so please pay 12044 * careful attention to the placement of any new checks. 12045 */ 12046 static int 12047 ctl_scsiio_lun_check(struct ctl_lun *lun, 12048 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 12049 { 12050 struct ctl_softc *softc = lun->ctl_softc; 12051 int retval; 12052 uint32_t residx; 12053 12054 retval = 0; 12055 12056 mtx_assert(&lun->lun_lock, MA_OWNED); 12057 12058 /* 12059 * If this shelf is a secondary shelf controller, we may have to 12060 * reject some commands disallowed by HA mode and link state. 12061 */ 12062 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 12063 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 12064 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 12065 ctl_set_lun_unavail(ctsio); 12066 retval = 1; 12067 goto bailout; 12068 } 12069 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 12070 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 12071 ctl_set_lun_transit(ctsio); 12072 retval = 1; 12073 goto bailout; 12074 } 12075 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 12076 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 12077 ctl_set_lun_standby(ctsio); 12078 retval = 1; 12079 goto bailout; 12080 } 12081 12082 /* The rest of checks are only done on executing side */ 12083 if (softc->ha_mode == CTL_HA_MODE_XFER) 12084 goto bailout; 12085 } 12086 12087 if (entry->pattern & CTL_LUN_PAT_WRITE) { 12088 if (lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 12089 ctl_set_hw_write_protected(ctsio); 12090 retval = 1; 12091 goto bailout; 12092 } 12093 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 12094 ctl_set_sense(ctsio, /*current_error*/ 1, 12095 /*sense_key*/ SSD_KEY_DATA_PROTECT, 12096 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 12097 retval = 1; 12098 goto bailout; 12099 } 12100 } 12101 12102 /* 12103 * Check for a reservation conflict. If this command isn't allowed 12104 * even on reserved LUNs, and if this initiator isn't the one who 12105 * reserved us, reject the command with a reservation conflict. 12106 */ 12107 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 12108 if ((lun->flags & CTL_LUN_RESERVED) 12109 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 12110 if (lun->res_idx != residx) { 12111 ctl_set_reservation_conflict(ctsio); 12112 retval = 1; 12113 goto bailout; 12114 } 12115 } 12116 12117 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 12118 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 12119 /* No reservation or command is allowed. */; 12120 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 12121 (lun->pr_res_type == SPR_TYPE_WR_EX || 12122 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 12123 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 12124 /* The command is allowed for Write Exclusive resv. */; 12125 } else { 12126 /* 12127 * if we aren't registered or it's a res holder type 12128 * reservation and this isn't the res holder then set a 12129 * conflict. 12130 */ 12131 if (ctl_get_prkey(lun, residx) == 0 || 12132 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 12133 ctl_set_reservation_conflict(ctsio); 12134 retval = 1; 12135 goto bailout; 12136 } 12137 } 12138 12139 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 12140 if (lun->flags & CTL_LUN_EJECTED) 12141 ctl_set_lun_ejected(ctsio); 12142 else if (lun->flags & CTL_LUN_NO_MEDIA) { 12143 if (lun->flags & CTL_LUN_REMOVABLE) 12144 ctl_set_lun_no_media(ctsio); 12145 else 12146 ctl_set_lun_int_reqd(ctsio); 12147 } else if (lun->flags & CTL_LUN_STOPPED) 12148 ctl_set_lun_stopped(ctsio); 12149 else 12150 goto bailout; 12151 retval = 1; 12152 goto bailout; 12153 } 12154 12155 bailout: 12156 return (retval); 12157 } 12158 12159 static void 12160 ctl_failover_io(union ctl_io *io, int have_lock) 12161 { 12162 CTL_IO_ASSERT(io, SCSI); 12163 12164 ctl_set_busy(&io->scsiio); 12165 ctl_done(io); 12166 } 12167 12168 static void 12169 ctl_failover_lun(union ctl_io *rio) 12170 { 12171 struct ctl_softc *softc = CTL_SOFTC(rio); 12172 struct ctl_lun *lun; 12173 struct ctl_io_hdr *io, *next_io; 12174 uint32_t targ_lun; 12175 12176 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 12177 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun)); 12178 12179 /* Find and lock the LUN. */ 12180 mtx_lock(&softc->ctl_lock); 12181 if (targ_lun > ctl_max_luns || 12182 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12183 mtx_unlock(&softc->ctl_lock); 12184 return; 12185 } 12186 mtx_lock(&lun->lun_lock); 12187 mtx_unlock(&softc->ctl_lock); 12188 if (lun->flags & CTL_LUN_DISABLED) { 12189 mtx_unlock(&lun->lun_lock); 12190 return; 12191 } 12192 12193 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12194 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 12195 /* We are master */ 12196 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 12197 if (io->flags & CTL_FLAG_IO_ACTIVE) { 12198 io->flags |= CTL_FLAG_ABORT | 12199 CTL_FLAG_FAILOVER; 12200 ctl_try_unblock_io(lun, 12201 (union ctl_io *)io, FALSE); 12202 } else { /* This can be only due to DATAMOVE */ 12203 io->msg_type = CTL_MSG_DATAMOVE_DONE; 12204 io->flags &= ~CTL_FLAG_DMA_INPROG; 12205 io->flags |= CTL_FLAG_IO_ACTIVE; 12206 io->port_status = 31340; 12207 ctl_enqueue_isc((union ctl_io *)io); 12208 } 12209 } else 12210 /* We are slave */ 12211 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 12212 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 12213 if (io->flags & CTL_FLAG_IO_ACTIVE) { 12214 io->flags |= CTL_FLAG_FAILOVER; 12215 } else { 12216 ctl_set_busy(&((union ctl_io *)io)-> 12217 scsiio); 12218 ctl_done((union ctl_io *)io); 12219 } 12220 } 12221 } 12222 } else { /* SERIALIZE modes */ 12223 LIST_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 12224 /* We are master */ 12225 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 12226 if (io->blocker != NULL) { 12227 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue, 12228 io, blocked_links); 12229 io->blocker = NULL; 12230 } 12231 ctl_try_unblock_others(lun, (union ctl_io *)io, 12232 TRUE); 12233 LIST_REMOVE(io, ooa_links); 12234 ctl_free_io((union ctl_io *)io); 12235 } else 12236 /* We are slave */ 12237 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 12238 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 12239 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 12240 ctl_set_busy(&((union ctl_io *)io)-> 12241 scsiio); 12242 ctl_done((union ctl_io *)io); 12243 } 12244 } 12245 } 12246 } 12247 mtx_unlock(&lun->lun_lock); 12248 } 12249 12250 static void 12251 ctl_scsiio_precheck(struct ctl_scsiio *ctsio) 12252 { 12253 struct ctl_softc *softc = CTL_SOFTC(ctsio); 12254 struct ctl_lun *lun; 12255 const struct ctl_cmd_entry *entry; 12256 union ctl_io *bio; 12257 uint32_t initidx, targ_lun; 12258 12259 lun = NULL; 12260 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 12261 if (targ_lun < ctl_max_luns) 12262 lun = softc->ctl_luns[targ_lun]; 12263 if (lun) { 12264 /* 12265 * If the LUN is invalid, pretend that it doesn't exist. 12266 * It will go away as soon as all pending I/O has been 12267 * completed. 12268 */ 12269 mtx_lock(&lun->lun_lock); 12270 if (lun->flags & CTL_LUN_DISABLED) { 12271 mtx_unlock(&lun->lun_lock); 12272 lun = NULL; 12273 } 12274 } 12275 CTL_LUN(ctsio) = lun; 12276 if (lun) { 12277 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 12278 12279 /* 12280 * Every I/O goes into the OOA queue for a particular LUN, 12281 * and stays there until completion. 12282 */ 12283 #ifdef CTL_TIME_IO 12284 if (LIST_EMPTY(&lun->ooa_queue)) 12285 lun->idle_time += getsbinuptime() - lun->last_busy; 12286 #endif 12287 LIST_INSERT_HEAD(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 12288 } 12289 12290 /* Get command entry and return error if it is unsuppotyed. */ 12291 entry = ctl_validate_command(ctsio); 12292 if (entry == NULL) { 12293 if (lun) 12294 mtx_unlock(&lun->lun_lock); 12295 return; 12296 } 12297 12298 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 12299 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 12300 12301 /* 12302 * Check to see whether we can send this command to LUNs that don't 12303 * exist. This should pretty much only be the case for inquiry 12304 * and request sense. Further checks, below, really require having 12305 * a LUN, so we can't really check the command anymore. Just put 12306 * it on the rtr queue. 12307 */ 12308 if (lun == NULL) { 12309 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 12310 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12311 ctl_enqueue_rtr((union ctl_io *)ctsio); 12312 return; 12313 } 12314 12315 ctl_set_unsupported_lun(ctsio); 12316 ctl_done((union ctl_io *)ctsio); 12317 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 12318 return; 12319 } else { 12320 /* 12321 * Make sure we support this particular command on this LUN. 12322 * e.g., we don't support writes to the control LUN. 12323 */ 12324 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 12325 mtx_unlock(&lun->lun_lock); 12326 ctl_set_invalid_opcode(ctsio); 12327 ctl_done((union ctl_io *)ctsio); 12328 return; 12329 } 12330 } 12331 12332 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 12333 12334 /* 12335 * If we've got a request sense, it'll clear the contingent 12336 * allegiance condition. Otherwise, if we have a CA condition for 12337 * this initiator, clear it, because it sent down a command other 12338 * than request sense. 12339 */ 12340 if (ctsio->cdb[0] != REQUEST_SENSE) { 12341 struct scsi_sense_data *ps; 12342 12343 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 12344 if (ps != NULL) 12345 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 12346 } 12347 12348 /* 12349 * If the command has this flag set, it handles its own unit 12350 * attention reporting, we shouldn't do anything. Otherwise we 12351 * check for any pending unit attentions, and send them back to the 12352 * initiator. We only do this when a command initially comes in, 12353 * not when we pull it off the blocked queue. 12354 * 12355 * According to SAM-3, section 5.3.2, the order that things get 12356 * presented back to the host is basically unit attentions caused 12357 * by some sort of reset event, busy status, reservation conflicts 12358 * or task set full, and finally any other status. 12359 * 12360 * One issue here is that some of the unit attentions we report 12361 * don't fall into the "reset" category (e.g. "reported luns data 12362 * has changed"). So reporting it here, before the reservation 12363 * check, may be technically wrong. I guess the only thing to do 12364 * would be to check for and report the reset events here, and then 12365 * check for the other unit attention types after we check for a 12366 * reservation conflict. 12367 * 12368 * XXX KDM need to fix this 12369 */ 12370 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 12371 ctl_ua_type ua_type; 12372 u_int sense_len = 0; 12373 12374 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 12375 &sense_len, SSD_TYPE_NONE); 12376 if (ua_type != CTL_UA_NONE) { 12377 mtx_unlock(&lun->lun_lock); 12378 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 12379 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12380 ctsio->sense_len = sense_len; 12381 ctl_done((union ctl_io *)ctsio); 12382 return; 12383 } 12384 } 12385 12386 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 12387 mtx_unlock(&lun->lun_lock); 12388 ctl_done((union ctl_io *)ctsio); 12389 return; 12390 } 12391 12392 /* 12393 * XXX CHD this is where we want to send IO to other side if 12394 * this LUN is secondary on this SC. We will need to make a copy 12395 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 12396 * the copy we send as FROM_OTHER. 12397 * We also need to stuff the address of the original IO so we can 12398 * find it easily. Something similar will need be done on the other 12399 * side so when we are done we can find the copy. 12400 */ 12401 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 12402 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 12403 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 12404 union ctl_ha_msg msg_info; 12405 int isc_retval; 12406 12407 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12408 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12409 mtx_unlock(&lun->lun_lock); 12410 12411 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 12412 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 12413 msg_info.hdr.serializing_sc = NULL; 12414 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 12415 msg_info.scsi.tag_num = ctsio->tag_num; 12416 msg_info.scsi.tag_type = ctsio->tag_type; 12417 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 12418 msg_info.scsi.cdb_len = ctsio->cdb_len; 12419 msg_info.scsi.priority = ctsio->priority; 12420 12421 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12422 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 12423 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 12424 ctsio->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 12425 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 12426 ctl_set_busy(ctsio); 12427 ctl_done((union ctl_io *)ctsio); 12428 return; 12429 } 12430 return; 12431 } 12432 12433 bio = (union ctl_io *)LIST_NEXT(&ctsio->io_hdr, ooa_links); 12434 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) { 12435 case CTL_ACTION_PASS: 12436 case CTL_ACTION_SKIP: 12437 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12438 mtx_unlock(&lun->lun_lock); 12439 ctl_enqueue_rtr((union ctl_io *)ctsio); 12440 break; 12441 case CTL_ACTION_BLOCK: 12442 ctsio->io_hdr.blocker = bio; 12443 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr, 12444 blocked_links); 12445 mtx_unlock(&lun->lun_lock); 12446 break; 12447 case CTL_ACTION_OVERLAP: 12448 mtx_unlock(&lun->lun_lock); 12449 ctl_set_overlapped_cmd(ctsio); 12450 ctl_done((union ctl_io *)ctsio); 12451 break; 12452 case CTL_ACTION_OVERLAP_TAG: 12453 mtx_unlock(&lun->lun_lock); 12454 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 12455 ctl_done((union ctl_io *)ctsio); 12456 break; 12457 default: 12458 __assert_unreachable(); 12459 } 12460 } 12461 12462 const struct ctl_cmd_entry * 12463 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 12464 { 12465 const struct ctl_cmd_entry *entry; 12466 int service_action; 12467 12468 entry = &ctl_cmd_table[ctsio->cdb[0]]; 12469 if (sa) 12470 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 12471 if (entry->flags & CTL_CMD_FLAG_SA5) { 12472 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 12473 entry = &((const struct ctl_cmd_entry *) 12474 entry->execute)[service_action]; 12475 } 12476 return (entry); 12477 } 12478 12479 const struct ctl_cmd_entry * 12480 ctl_validate_command(struct ctl_scsiio *ctsio) 12481 { 12482 const struct ctl_cmd_entry *entry; 12483 int i, sa; 12484 uint8_t diff; 12485 12486 entry = ctl_get_cmd_entry(ctsio, &sa); 12487 ctsio->seridx = entry->seridx; 12488 if (entry->execute == NULL) { 12489 if (sa) 12490 ctl_set_invalid_field(ctsio, 12491 /*sks_valid*/ 1, 12492 /*command*/ 1, 12493 /*field*/ 1, 12494 /*bit_valid*/ 1, 12495 /*bit*/ 4); 12496 else 12497 ctl_set_invalid_opcode(ctsio); 12498 ctl_done((union ctl_io *)ctsio); 12499 return (NULL); 12500 } 12501 KASSERT(entry->length > 0, 12502 ("Not defined length for command 0x%02x/0x%02x", 12503 ctsio->cdb[0], ctsio->cdb[1])); 12504 for (i = 1; i < entry->length; i++) { 12505 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 12506 if (diff == 0) 12507 continue; 12508 ctl_set_invalid_field(ctsio, 12509 /*sks_valid*/ 1, 12510 /*command*/ 1, 12511 /*field*/ i, 12512 /*bit_valid*/ 1, 12513 /*bit*/ fls(diff) - 1); 12514 ctl_done((union ctl_io *)ctsio); 12515 return (NULL); 12516 } 12517 return (entry); 12518 } 12519 12520 static int 12521 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 12522 { 12523 12524 switch (lun_type) { 12525 case T_DIRECT: 12526 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 12527 return (0); 12528 break; 12529 case T_PROCESSOR: 12530 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 12531 return (0); 12532 break; 12533 case T_CDROM: 12534 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 12535 return (0); 12536 break; 12537 default: 12538 return (0); 12539 } 12540 return (1); 12541 } 12542 12543 static int 12544 ctl_scsiio(struct ctl_scsiio *ctsio) 12545 { 12546 int retval; 12547 const struct ctl_cmd_entry *entry; 12548 12549 retval = CTL_RETVAL_COMPLETE; 12550 12551 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 12552 12553 entry = ctl_get_cmd_entry(ctsio, NULL); 12554 12555 /* 12556 * If this I/O has been aborted, just send it straight to 12557 * ctl_done() without executing it. 12558 */ 12559 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 12560 ctl_done((union ctl_io *)ctsio); 12561 goto bailout; 12562 } 12563 12564 /* 12565 * All the checks should have been handled by ctl_scsiio_precheck(). 12566 * We should be clear now to just execute the I/O. 12567 */ 12568 retval = entry->execute(ctsio); 12569 12570 bailout: 12571 return (retval); 12572 } 12573 12574 static int 12575 ctl_target_reset(union ctl_io *io) 12576 { 12577 struct ctl_softc *softc = CTL_SOFTC(io); 12578 struct ctl_port *port = CTL_PORT(io); 12579 struct ctl_lun *lun; 12580 uint32_t initidx; 12581 ctl_ua_type ua_type; 12582 12583 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12584 union ctl_ha_msg msg_info; 12585 12586 msg_info.hdr.nexus = io->io_hdr.nexus; 12587 msg_info.task.task_action = io->taskio.task_action; 12588 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12589 msg_info.hdr.original_sc = NULL; 12590 msg_info.hdr.serializing_sc = NULL; 12591 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12592 sizeof(msg_info.task), M_WAITOK); 12593 } 12594 12595 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12596 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 12597 ua_type = CTL_UA_TARG_RESET; 12598 else 12599 ua_type = CTL_UA_BUS_RESET; 12600 mtx_lock(&softc->ctl_lock); 12601 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12602 if (port != NULL && 12603 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 12604 continue; 12605 ctl_do_lun_reset(lun, initidx, ua_type); 12606 } 12607 mtx_unlock(&softc->ctl_lock); 12608 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12609 return (0); 12610 } 12611 12612 /* 12613 * The LUN should always be set. The I/O is optional, and is used to 12614 * distinguish between I/Os sent by this initiator, and by other 12615 * initiators. We set unit attention for initiators other than this one. 12616 * SAM-3 is vague on this point. It does say that a unit attention should 12617 * be established for other initiators when a LUN is reset (see section 12618 * 5.7.3), but it doesn't specifically say that the unit attention should 12619 * be established for this particular initiator when a LUN is reset. Here 12620 * is the relevant text, from SAM-3 rev 8: 12621 * 12622 * 5.7.2 When a SCSI initiator port aborts its own tasks 12623 * 12624 * When a SCSI initiator port causes its own task(s) to be aborted, no 12625 * notification that the task(s) have been aborted shall be returned to 12626 * the SCSI initiator port other than the completion response for the 12627 * command or task management function action that caused the task(s) to 12628 * be aborted and notification(s) associated with related effects of the 12629 * action (e.g., a reset unit attention condition). 12630 * 12631 * XXX KDM for now, we're setting unit attention for all initiators. 12632 */ 12633 static void 12634 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 12635 { 12636 struct ctl_io_hdr *xioh; 12637 int i; 12638 12639 mtx_lock(&lun->lun_lock); 12640 /* Abort tasks. */ 12641 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12642 xioh->flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12643 ctl_try_unblock_io(lun, (union ctl_io *)xioh, FALSE); 12644 } 12645 /* Clear CA. */ 12646 for (i = 0; i < ctl_max_ports; i++) { 12647 free(lun->pending_sense[i], M_CTL); 12648 lun->pending_sense[i] = NULL; 12649 } 12650 /* Clear reservation. */ 12651 lun->flags &= ~CTL_LUN_RESERVED; 12652 /* Clear prevent media removal. */ 12653 if (lun->prevent) { 12654 for (i = 0; i < CTL_MAX_INITIATORS; i++) 12655 ctl_clear_mask(lun->prevent, i); 12656 lun->prevent_count = 0; 12657 } 12658 /* Clear TPC status */ 12659 ctl_tpc_lun_clear(lun, -1); 12660 /* Establish UA. */ 12661 #if 0 12662 ctl_est_ua_all(lun, initidx, ua_type); 12663 #else 12664 ctl_est_ua_all(lun, -1, ua_type); 12665 #endif 12666 mtx_unlock(&lun->lun_lock); 12667 } 12668 12669 static int 12670 ctl_lun_reset(union ctl_io *io) 12671 { 12672 struct ctl_softc *softc = CTL_SOFTC(io); 12673 struct ctl_lun *lun; 12674 uint32_t targ_lun, initidx; 12675 12676 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12677 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12678 mtx_lock(&softc->ctl_lock); 12679 if (targ_lun >= ctl_max_luns || 12680 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12681 mtx_unlock(&softc->ctl_lock); 12682 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12683 return (1); 12684 } 12685 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 12686 mtx_unlock(&softc->ctl_lock); 12687 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12688 12689 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 12690 union ctl_ha_msg msg_info; 12691 12692 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12693 msg_info.hdr.nexus = io->io_hdr.nexus; 12694 msg_info.task.task_action = CTL_TASK_LUN_RESET; 12695 msg_info.hdr.original_sc = NULL; 12696 msg_info.hdr.serializing_sc = NULL; 12697 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12698 sizeof(msg_info.task), M_WAITOK); 12699 } 12700 return (0); 12701 } 12702 12703 static void 12704 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12705 int other_sc) 12706 { 12707 struct ctl_io_hdr *xioh; 12708 12709 mtx_assert(&lun->lun_lock, MA_OWNED); 12710 12711 /* 12712 * Run through the OOA queue and attempt to find the given I/O. 12713 * The target port, initiator ID, tag type and tag number have to 12714 * match the values that we got from the initiator. If we have an 12715 * untagged command to abort, simply abort the first untagged command 12716 * we come to. We only allow one untagged command at a time of course. 12717 */ 12718 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12719 union ctl_io *xio = (union ctl_io *)xioh; 12720 12721 if ((targ_port == UINT32_MAX || 12722 targ_port == xioh->nexus.targ_port) && 12723 (init_id == UINT32_MAX || 12724 init_id == xioh->nexus.initid)) { 12725 if (targ_port != xioh->nexus.targ_port || 12726 init_id != xioh->nexus.initid) 12727 xioh->flags |= CTL_FLAG_ABORT_STATUS; 12728 xioh->flags |= CTL_FLAG_ABORT; 12729 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12730 union ctl_ha_msg msg_info; 12731 12732 CTL_IO_ASSERT(xio, SCSI); 12733 msg_info.hdr.nexus = xioh->nexus; 12734 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12735 msg_info.task.tag_num = xio->scsiio.tag_num; 12736 msg_info.task.tag_type = xio->scsiio.tag_type; 12737 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12738 msg_info.hdr.original_sc = NULL; 12739 msg_info.hdr.serializing_sc = NULL; 12740 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12741 sizeof(msg_info.task), M_NOWAIT); 12742 } 12743 ctl_try_unblock_io(lun, xio, FALSE); 12744 } 12745 } 12746 } 12747 12748 static int 12749 ctl_abort_task_set(union ctl_io *io) 12750 { 12751 struct ctl_softc *softc = CTL_SOFTC(io); 12752 struct ctl_lun *lun; 12753 uint32_t targ_lun; 12754 12755 /* 12756 * Look up the LUN. 12757 */ 12758 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12759 mtx_lock(&softc->ctl_lock); 12760 if (targ_lun >= ctl_max_luns || 12761 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12762 mtx_unlock(&softc->ctl_lock); 12763 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12764 return (1); 12765 } 12766 12767 mtx_lock(&lun->lun_lock); 12768 mtx_unlock(&softc->ctl_lock); 12769 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12770 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12771 io->io_hdr.nexus.initid, 12772 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12773 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12774 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12775 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12776 } 12777 mtx_unlock(&lun->lun_lock); 12778 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12779 return (0); 12780 } 12781 12782 static void 12783 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 12784 ctl_ua_type ua_type) 12785 { 12786 struct ctl_lun *lun; 12787 struct scsi_sense_data *ps; 12788 uint32_t p, i; 12789 12790 p = initidx / CTL_MAX_INIT_PER_PORT; 12791 i = initidx % CTL_MAX_INIT_PER_PORT; 12792 mtx_lock(&softc->ctl_lock); 12793 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12794 mtx_lock(&lun->lun_lock); 12795 /* Abort tasks. */ 12796 ctl_abort_tasks_lun(lun, p, i, 1); 12797 /* Clear CA. */ 12798 ps = lun->pending_sense[p]; 12799 if (ps != NULL) 12800 ps[i].error_code = 0; 12801 /* Clear reservation. */ 12802 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12803 lun->flags &= ~CTL_LUN_RESERVED; 12804 /* Clear prevent media removal. */ 12805 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 12806 ctl_clear_mask(lun->prevent, initidx); 12807 lun->prevent_count--; 12808 } 12809 /* Clear TPC status */ 12810 ctl_tpc_lun_clear(lun, initidx); 12811 /* Establish UA. */ 12812 ctl_est_ua(lun, initidx, ua_type); 12813 mtx_unlock(&lun->lun_lock); 12814 } 12815 mtx_unlock(&softc->ctl_lock); 12816 } 12817 12818 static int 12819 ctl_i_t_nexus_reset(union ctl_io *io) 12820 { 12821 struct ctl_softc *softc = CTL_SOFTC(io); 12822 uint32_t initidx; 12823 12824 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12825 union ctl_ha_msg msg_info; 12826 12827 msg_info.hdr.nexus = io->io_hdr.nexus; 12828 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12829 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12830 msg_info.hdr.original_sc = NULL; 12831 msg_info.hdr.serializing_sc = NULL; 12832 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12833 sizeof(msg_info.task), M_WAITOK); 12834 } 12835 12836 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12837 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 12838 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12839 return (0); 12840 } 12841 12842 static int 12843 ctl_abort_task(union ctl_io *io) 12844 { 12845 struct ctl_softc *softc = CTL_SOFTC(io); 12846 struct ctl_io_hdr *xioh; 12847 struct ctl_lun *lun; 12848 uint32_t targ_lun; 12849 12850 /* 12851 * Look up the LUN. 12852 */ 12853 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12854 mtx_lock(&softc->ctl_lock); 12855 if (targ_lun >= ctl_max_luns || 12856 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12857 mtx_unlock(&softc->ctl_lock); 12858 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12859 return (1); 12860 } 12861 12862 mtx_lock(&lun->lun_lock); 12863 mtx_unlock(&softc->ctl_lock); 12864 /* 12865 * Run through the OOA queue and attempt to find the given I/O. 12866 * The target port, initiator ID, tag type and tag number have to 12867 * match the values that we got from the initiator. If we have an 12868 * untagged command to abort, simply abort the first untagged command 12869 * we come to. We only allow one untagged command at a time of course. 12870 */ 12871 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12872 union ctl_io *xio = (union ctl_io *)xioh; 12873 12874 CTL_IO_ASSERT(xio, SCSI); 12875 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12876 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12877 || (xioh->flags & CTL_FLAG_ABORT)) 12878 continue; 12879 12880 /* 12881 * If the abort says that the task is untagged, the 12882 * task in the queue must be untagged. Otherwise, 12883 * we just check to see whether the tag numbers 12884 * match. This is because the QLogic firmware 12885 * doesn't pass back the tag type in an abort 12886 * request. 12887 */ 12888 #if 0 12889 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12890 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12891 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12892 #else 12893 /* 12894 * XXX KDM we've got problems with FC, because it 12895 * doesn't send down a tag type with aborts. So we 12896 * can only really go by the tag number... 12897 * This may cause problems with parallel SCSI. 12898 * Need to figure that out!! 12899 */ 12900 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12901 #endif 12902 xioh->flags |= CTL_FLAG_ABORT; 12903 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12904 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12905 union ctl_ha_msg msg_info; 12906 12907 msg_info.hdr.nexus = io->io_hdr.nexus; 12908 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12909 msg_info.task.tag_num = io->taskio.tag_num; 12910 msg_info.task.tag_type = io->taskio.tag_type; 12911 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12912 msg_info.hdr.original_sc = NULL; 12913 msg_info.hdr.serializing_sc = NULL; 12914 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12915 sizeof(msg_info.task), M_NOWAIT); 12916 } 12917 ctl_try_unblock_io(lun, xio, FALSE); 12918 } 12919 } 12920 mtx_unlock(&lun->lun_lock); 12921 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12922 return (0); 12923 } 12924 12925 static int 12926 ctl_query_task(union ctl_io *io, int task_set) 12927 { 12928 struct ctl_softc *softc = CTL_SOFTC(io); 12929 struct ctl_io_hdr *xioh; 12930 struct ctl_lun *lun; 12931 int found = 0; 12932 uint32_t targ_lun; 12933 12934 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12935 mtx_lock(&softc->ctl_lock); 12936 if (targ_lun >= ctl_max_luns || 12937 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12938 mtx_unlock(&softc->ctl_lock); 12939 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12940 return (1); 12941 } 12942 mtx_lock(&lun->lun_lock); 12943 mtx_unlock(&softc->ctl_lock); 12944 LIST_FOREACH(xioh, &lun->ooa_queue, ooa_links) { 12945 union ctl_io *xio = (union ctl_io *)xioh; 12946 12947 CTL_IO_ASSERT(xio, SCSI); 12948 if ((xioh->nexus.targ_port != io->io_hdr.nexus.targ_port) 12949 || (xioh->nexus.initid != io->io_hdr.nexus.initid) 12950 || (xioh->flags & CTL_FLAG_ABORT)) 12951 continue; 12952 12953 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12954 found = 1; 12955 break; 12956 } 12957 } 12958 mtx_unlock(&lun->lun_lock); 12959 if (found) 12960 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12961 else 12962 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12963 return (0); 12964 } 12965 12966 static int 12967 ctl_query_async_event(union ctl_io *io) 12968 { 12969 struct ctl_softc *softc = CTL_SOFTC(io); 12970 struct ctl_lun *lun; 12971 ctl_ua_type ua; 12972 uint32_t targ_lun, initidx; 12973 12974 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12975 mtx_lock(&softc->ctl_lock); 12976 if (targ_lun >= ctl_max_luns || 12977 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12978 mtx_unlock(&softc->ctl_lock); 12979 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12980 return (1); 12981 } 12982 mtx_lock(&lun->lun_lock); 12983 mtx_unlock(&softc->ctl_lock); 12984 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12985 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12986 mtx_unlock(&lun->lun_lock); 12987 if (ua != CTL_UA_NONE) 12988 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12989 else 12990 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12991 return (0); 12992 } 12993 12994 static void 12995 ctl_run_task(union ctl_io *io) 12996 { 12997 int retval = 1; 12998 12999 CTL_DEBUG_PRINT(("ctl_run_task\n")); 13000 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 13001 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 13002 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 13003 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 13004 switch (io->taskio.task_action) { 13005 case CTL_TASK_ABORT_TASK: 13006 retval = ctl_abort_task(io); 13007 break; 13008 case CTL_TASK_ABORT_TASK_SET: 13009 case CTL_TASK_CLEAR_TASK_SET: 13010 retval = ctl_abort_task_set(io); 13011 break; 13012 case CTL_TASK_CLEAR_ACA: 13013 break; 13014 case CTL_TASK_I_T_NEXUS_RESET: 13015 retval = ctl_i_t_nexus_reset(io); 13016 break; 13017 case CTL_TASK_LUN_RESET: 13018 retval = ctl_lun_reset(io); 13019 break; 13020 case CTL_TASK_TARGET_RESET: 13021 case CTL_TASK_BUS_RESET: 13022 retval = ctl_target_reset(io); 13023 break; 13024 case CTL_TASK_PORT_LOGIN: 13025 break; 13026 case CTL_TASK_PORT_LOGOUT: 13027 break; 13028 case CTL_TASK_QUERY_TASK: 13029 retval = ctl_query_task(io, 0); 13030 break; 13031 case CTL_TASK_QUERY_TASK_SET: 13032 retval = ctl_query_task(io, 1); 13033 break; 13034 case CTL_TASK_QUERY_ASYNC_EVENT: 13035 retval = ctl_query_async_event(io); 13036 break; 13037 default: 13038 printf("%s: got unknown task management event %d\n", 13039 __func__, io->taskio.task_action); 13040 break; 13041 } 13042 if (retval == 0) 13043 io->io_hdr.status = CTL_SUCCESS; 13044 else 13045 io->io_hdr.status = CTL_ERROR; 13046 ctl_done(io); 13047 } 13048 13049 /* 13050 * For HA operation. Handle commands that come in from the other 13051 * controller. 13052 */ 13053 static void 13054 ctl_handle_isc(union ctl_io *io) 13055 { 13056 struct ctl_softc *softc = CTL_SOFTC(io); 13057 struct ctl_lun *lun; 13058 const struct ctl_cmd_entry *entry; 13059 uint32_t targ_lun; 13060 13061 CTL_IO_ASSERT(io, SCSI); 13062 13063 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 13064 switch (io->io_hdr.msg_type) { 13065 case CTL_MSG_SERIALIZE: 13066 ctl_serialize_other_sc_cmd(&io->scsiio); 13067 break; 13068 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 13069 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 13070 if (targ_lun >= ctl_max_luns || 13071 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13072 ctl_done(io); 13073 break; 13074 } 13075 mtx_lock(&lun->lun_lock); 13076 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 13077 mtx_unlock(&lun->lun_lock); 13078 ctl_done(io); 13079 break; 13080 } 13081 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 13082 mtx_unlock(&lun->lun_lock); 13083 ctl_enqueue_rtr(io); 13084 break; 13085 case CTL_MSG_FINISH_IO: 13086 if (softc->ha_mode == CTL_HA_MODE_XFER) { 13087 ctl_done(io); 13088 break; 13089 } 13090 if (targ_lun >= ctl_max_luns || 13091 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13092 ctl_free_io(io); 13093 break; 13094 } 13095 mtx_lock(&lun->lun_lock); 13096 ctl_try_unblock_others(lun, io, TRUE); 13097 LIST_REMOVE(&io->io_hdr, ooa_links); 13098 mtx_unlock(&lun->lun_lock); 13099 ctl_free_io(io); 13100 break; 13101 case CTL_MSG_PERS_ACTION: 13102 ctl_hndl_per_res_out_on_other_sc(io); 13103 ctl_free_io(io); 13104 break; 13105 case CTL_MSG_BAD_JUJU: 13106 ctl_done(io); 13107 break; 13108 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 13109 ctl_datamove_remote(io); 13110 break; 13111 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 13112 ctl_datamove_done(io, false); 13113 break; 13114 case CTL_MSG_FAILOVER: 13115 ctl_failover_lun(io); 13116 ctl_free_io(io); 13117 break; 13118 default: 13119 printf("%s: Invalid message type %d\n", 13120 __func__, io->io_hdr.msg_type); 13121 ctl_free_io(io); 13122 break; 13123 } 13124 13125 } 13126 13127 /* 13128 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 13129 * there is no match. 13130 */ 13131 static ctl_lun_error_pattern 13132 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 13133 { 13134 const struct ctl_cmd_entry *entry; 13135 ctl_lun_error_pattern filtered_pattern, pattern; 13136 13137 pattern = desc->error_pattern; 13138 13139 /* 13140 * XXX KDM we need more data passed into this function to match a 13141 * custom pattern, and we actually need to implement custom pattern 13142 * matching. 13143 */ 13144 if (pattern & CTL_LUN_PAT_CMD) 13145 return (CTL_LUN_PAT_CMD); 13146 13147 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 13148 return (CTL_LUN_PAT_ANY); 13149 13150 entry = ctl_get_cmd_entry(ctsio, NULL); 13151 13152 filtered_pattern = entry->pattern & pattern; 13153 13154 /* 13155 * If the user requested specific flags in the pattern (e.g. 13156 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 13157 * flags. 13158 * 13159 * If the user did not specify any flags, it doesn't matter whether 13160 * or not the command supports the flags. 13161 */ 13162 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 13163 (pattern & ~CTL_LUN_PAT_MASK)) 13164 return (CTL_LUN_PAT_NONE); 13165 13166 /* 13167 * If the user asked for a range check, see if the requested LBA 13168 * range overlaps with this command's LBA range. 13169 */ 13170 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 13171 uint64_t lba1; 13172 uint64_t len1; 13173 ctl_action action; 13174 int retval; 13175 13176 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 13177 if (retval != 0) 13178 return (CTL_LUN_PAT_NONE); 13179 13180 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 13181 desc->lba_range.len, FALSE); 13182 /* 13183 * A "pass" means that the LBA ranges don't overlap, so 13184 * this doesn't match the user's range criteria. 13185 */ 13186 if (action == CTL_ACTION_PASS) 13187 return (CTL_LUN_PAT_NONE); 13188 } 13189 13190 return (filtered_pattern); 13191 } 13192 13193 static void 13194 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 13195 { 13196 struct ctl_error_desc *desc, *desc2; 13197 13198 CTL_IO_ASSERT(io, SCSI); 13199 13200 mtx_assert(&lun->lun_lock, MA_OWNED); 13201 13202 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 13203 ctl_lun_error_pattern pattern; 13204 /* 13205 * Check to see whether this particular command matches 13206 * the pattern in the descriptor. 13207 */ 13208 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 13209 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 13210 continue; 13211 13212 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 13213 case CTL_LUN_INJ_ABORTED: 13214 ctl_set_aborted(&io->scsiio); 13215 break; 13216 case CTL_LUN_INJ_MEDIUM_ERR: 13217 ctl_set_medium_error(&io->scsiio, 13218 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 13219 CTL_FLAG_DATA_OUT); 13220 break; 13221 case CTL_LUN_INJ_UA: 13222 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 13223 * OCCURRED */ 13224 ctl_set_ua(&io->scsiio, 0x29, 0x00); 13225 break; 13226 case CTL_LUN_INJ_CUSTOM: 13227 /* 13228 * We're assuming the user knows what he is doing. 13229 * Just copy the sense information without doing 13230 * checks. 13231 */ 13232 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 13233 MIN(sizeof(desc->custom_sense), 13234 sizeof(io->scsiio.sense_data))); 13235 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 13236 io->scsiio.sense_len = SSD_FULL_SIZE; 13237 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 13238 break; 13239 case CTL_LUN_INJ_NONE: 13240 default: 13241 /* 13242 * If this is an error injection type we don't know 13243 * about, clear the continuous flag (if it is set) 13244 * so it will get deleted below. 13245 */ 13246 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 13247 break; 13248 } 13249 /* 13250 * By default, each error injection action is a one-shot 13251 */ 13252 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 13253 continue; 13254 13255 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 13256 13257 free(desc, M_CTL); 13258 } 13259 } 13260 13261 #ifdef CTL_IO_DELAY 13262 static void 13263 ctl_datamove_timer_wakeup(void *arg) 13264 { 13265 union ctl_io *io; 13266 13267 io = (union ctl_io *)arg; 13268 13269 ctl_datamove(io); 13270 } 13271 #endif /* CTL_IO_DELAY */ 13272 13273 static void 13274 ctl_datamove_done_process(union ctl_io *io) 13275 { 13276 #ifdef CTL_TIME_IO 13277 struct bintime cur_bt; 13278 13279 getbinuptime(&cur_bt); 13280 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 13281 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 13282 #endif 13283 io->io_hdr.num_dmas++; 13284 13285 if ((io->io_hdr.port_status != 0) && 13286 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 13287 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 13288 switch (io->io_hdr.io_type) { 13289 case CTL_IO_SCSI: 13290 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 13291 /*retry_count*/ io->io_hdr.port_status); 13292 break; 13293 case CTL_IO_NVME: 13294 case CTL_IO_NVME_ADMIN: 13295 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13296 ctl_nvme_set_command_aborted(&io->nvmeio); 13297 else 13298 ctl_nvme_set_data_transfer_error(&io->nvmeio); 13299 break; 13300 default: 13301 __assert_unreachable(); 13302 } 13303 } else if (ctl_kern_data_resid(io) != 0 && 13304 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 13305 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 13306 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 13307 switch (io->io_hdr.io_type) { 13308 case CTL_IO_SCSI: 13309 ctl_set_invalid_field_ciu(&io->scsiio); 13310 break; 13311 case CTL_IO_NVME: 13312 case CTL_IO_NVME_ADMIN: 13313 ctl_nvme_set_data_transfer_error(&io->nvmeio); 13314 break; 13315 default: 13316 __assert_unreachable(); 13317 } 13318 } else if (ctl_debug & CTL_DEBUG_CDB_DATA) 13319 ctl_data_print(io); 13320 } 13321 13322 void 13323 ctl_datamove_done(union ctl_io *io, bool samethr) 13324 { 13325 13326 ctl_datamove_done_process(io); 13327 ctl_be_move_done(io, samethr); 13328 } 13329 13330 void 13331 ctl_datamove(union ctl_io *io) 13332 { 13333 void (*fe_datamove)(union ctl_io *io); 13334 13335 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 13336 13337 CTL_DEBUG_PRINT(("ctl_datamove\n")); 13338 13339 /* No data transferred yet. Frontend must update this when done. */ 13340 ctl_set_kern_data_resid(io, ctl_kern_data_len(io)); 13341 13342 #ifdef CTL_TIME_IO 13343 getbinuptime(&io->io_hdr.dma_start_bt); 13344 #endif /* CTL_TIME_IO */ 13345 13346 #ifdef CTL_IO_DELAY 13347 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13348 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13349 } else { 13350 struct ctl_lun *lun; 13351 13352 lun = CTL_LUN(io); 13353 if ((lun != NULL) 13354 && (lun->delay_info.datamove_delay > 0)) { 13355 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13356 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13357 callout_reset(&io->io_hdr.delay_callout, 13358 lun->delay_info.datamove_delay * hz, 13359 ctl_datamove_timer_wakeup, io); 13360 if (lun->delay_info.datamove_type == 13361 CTL_DELAY_TYPE_ONESHOT) 13362 lun->delay_info.datamove_delay = 0; 13363 return; 13364 } 13365 } 13366 #endif 13367 13368 /* 13369 * This command has been aborted. Set the port status, so we fail 13370 * the data move. 13371 */ 13372 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13373 switch (io->io_hdr.io_type) { 13374 case CTL_IO_SCSI: 13375 printf("ctl_datamove: tag 0x%jx on (%u:%u:%u) aborted\n", 13376 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13377 io->io_hdr.nexus.targ_port, 13378 io->io_hdr.nexus.targ_lun); 13379 break; 13380 case CTL_IO_NVME: 13381 case CTL_IO_NVME_ADMIN: 13382 printf("ctl_datamove: CID 0x%x on (%u:%u:%u) aborted\n", 13383 le16toh(io->nvmeio.cmd.cid), 13384 io->io_hdr.nexus.initid, io->io_hdr.nexus.targ_port, 13385 io->io_hdr.nexus.targ_lun); 13386 break; 13387 default: 13388 __assert_unreachable(); 13389 } 13390 io->io_hdr.port_status = 31337; 13391 ctl_datamove_done_process(io); 13392 ctl_be_move_done(io, true); 13393 return; 13394 } 13395 13396 /* Don't confuse frontend with zero length data move. */ 13397 if (ctl_kern_data_len(io) == 0) { 13398 ctl_datamove_done_process(io); 13399 ctl_be_move_done(io, true); 13400 return; 13401 } 13402 13403 fe_datamove = CTL_PORT(io)->fe_datamove; 13404 fe_datamove(io); 13405 } 13406 13407 static void 13408 ctl_send_datamove_done(union ctl_io *io, int have_lock) 13409 { 13410 union ctl_ha_msg msg; 13411 #ifdef CTL_TIME_IO 13412 struct bintime cur_bt; 13413 #endif 13414 13415 CTL_IO_ASSERT(io, SCSI); 13416 13417 memset(&msg, 0, sizeof(msg)); 13418 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 13419 msg.hdr.original_sc = io; 13420 msg.hdr.serializing_sc = io->io_hdr.remote_io; 13421 msg.hdr.nexus = io->io_hdr.nexus; 13422 msg.hdr.status = io->io_hdr.status; 13423 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 13424 msg.scsi.tag_num = io->scsiio.tag_num; 13425 msg.scsi.tag_type = io->scsiio.tag_type; 13426 msg.scsi.scsi_status = io->scsiio.scsi_status; 13427 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13428 io->scsiio.sense_len); 13429 msg.scsi.sense_len = io->scsiio.sense_len; 13430 msg.scsi.port_status = io->io_hdr.port_status; 13431 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13432 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13433 ctl_failover_io(io, /*have_lock*/ have_lock); 13434 return; 13435 } 13436 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13437 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 13438 msg.scsi.sense_len, M_WAITOK); 13439 13440 #ifdef CTL_TIME_IO 13441 getbinuptime(&cur_bt); 13442 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 13443 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 13444 #endif 13445 io->io_hdr.num_dmas++; 13446 } 13447 13448 /* 13449 * The DMA to the remote side is done, now we need to tell the other side 13450 * we're done so it can continue with its data movement. 13451 */ 13452 static void 13453 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 13454 { 13455 union ctl_io *io; 13456 uint32_t i; 13457 13458 io = rq->context; 13459 CTL_IO_ASSERT(io, SCSI); 13460 13461 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13462 printf("%s: ISC DMA write failed with error %d", __func__, 13463 rq->ret); 13464 ctl_set_internal_failure(&io->scsiio, 13465 /*sks_valid*/ 1, 13466 /*retry_count*/ rq->ret); 13467 } 13468 13469 ctl_dt_req_free(rq); 13470 13471 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13472 free(CTL_LSGLT(io)[i].addr, M_CTL); 13473 free(CTL_RSGL(io), M_CTL); 13474 CTL_RSGL(io) = NULL; 13475 CTL_LSGL(io) = NULL; 13476 13477 /* 13478 * The data is in local and remote memory, so now we need to send 13479 * status (good or back) back to the other side. 13480 */ 13481 ctl_send_datamove_done(io, /*have_lock*/ 0); 13482 } 13483 13484 /* 13485 * We've moved the data from the host/controller into local memory. Now we 13486 * need to push it over to the remote controller's memory. 13487 */ 13488 static int 13489 ctl_datamove_remote_dm_write_cb(union ctl_io *io, bool samethr) 13490 { 13491 int retval; 13492 13493 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13494 ctl_datamove_remote_write_cb); 13495 return (retval); 13496 } 13497 13498 static void 13499 ctl_datamove_remote_write(union ctl_io *io) 13500 { 13501 int retval; 13502 void (*fe_datamove)(union ctl_io *io); 13503 13504 CTL_IO_ASSERT(io, SCSI); 13505 13506 /* 13507 * - Get the data from the host/HBA into local memory. 13508 * - DMA memory from the local controller to the remote controller. 13509 * - Send status back to the remote controller. 13510 */ 13511 13512 retval = ctl_datamove_remote_sgl_setup(io); 13513 if (retval != 0) 13514 return; 13515 13516 /* Switch the pointer over so the FETD knows what to do */ 13517 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 13518 13519 /* 13520 * Use a custom move done callback, since we need to send completion 13521 * back to the other controller, not to the backend on this side. 13522 */ 13523 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13524 13525 fe_datamove = CTL_PORT(io)->fe_datamove; 13526 fe_datamove(io); 13527 } 13528 13529 static int 13530 ctl_datamove_remote_dm_read_cb(union ctl_io *io, bool samethr) 13531 { 13532 uint32_t i; 13533 13534 CTL_IO_ASSERT(io, SCSI); 13535 13536 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13537 free(CTL_LSGLT(io)[i].addr, M_CTL); 13538 free(CTL_RSGL(io), M_CTL); 13539 CTL_RSGL(io) = NULL; 13540 CTL_LSGL(io) = NULL; 13541 13542 /* 13543 * The read is done, now we need to send status (good or bad) back 13544 * to the other side. 13545 */ 13546 ctl_send_datamove_done(io, /*have_lock*/ 0); 13547 13548 return (0); 13549 } 13550 13551 static void 13552 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13553 { 13554 union ctl_io *io; 13555 void (*fe_datamove)(union ctl_io *io); 13556 13557 io = rq->context; 13558 CTL_IO_ASSERT(io, SCSI); 13559 13560 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13561 printf("%s: ISC DMA read failed with error %d\n", __func__, 13562 rq->ret); 13563 ctl_set_internal_failure(&io->scsiio, 13564 /*sks_valid*/ 1, 13565 /*retry_count*/ rq->ret); 13566 } 13567 13568 ctl_dt_req_free(rq); 13569 13570 /* Switch the pointer over so the FETD knows what to do */ 13571 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io); 13572 13573 /* 13574 * Use a custom move done callback, since we need to send completion 13575 * back to the other controller, not to the backend on this side. 13576 */ 13577 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13578 13579 /* XXX KDM add checks like the ones in ctl_datamove? */ 13580 13581 fe_datamove = CTL_PORT(io)->fe_datamove; 13582 fe_datamove(io); 13583 } 13584 13585 static int 13586 ctl_datamove_remote_sgl_setup(union ctl_io *io) 13587 { 13588 struct ctl_sg_entry *local_sglist; 13589 uint32_t len_to_go; 13590 int retval; 13591 int i; 13592 13593 CTL_IO_ASSERT(io, SCSI); 13594 13595 retval = 0; 13596 local_sglist = CTL_LSGL(io); 13597 len_to_go = io->scsiio.kern_data_len; 13598 13599 /* 13600 * The difficult thing here is that the size of the various 13601 * S/G segments may be different than the size from the 13602 * remote controller. That'll make it harder when DMAing 13603 * the data back to the other side. 13604 */ 13605 for (i = 0; len_to_go > 0; i++) { 13606 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 13607 local_sglist[i].addr = 13608 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 13609 13610 len_to_go -= local_sglist[i].len; 13611 } 13612 /* 13613 * Reset the number of S/G entries accordingly. The original 13614 * number of S/G entries is available in rem_sg_entries. 13615 */ 13616 io->scsiio.kern_sg_entries = i; 13617 13618 return (retval); 13619 } 13620 13621 static int 13622 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13623 ctl_ha_dt_cb callback) 13624 { 13625 struct ctl_ha_dt_req *rq; 13626 struct ctl_sg_entry *remote_sglist, *local_sglist; 13627 uint32_t local_used, remote_used, total_used; 13628 int i, j, isc_ret; 13629 13630 rq = ctl_dt_req_alloc(); 13631 13632 CTL_IO_ASSERT(io, SCSI); 13633 13634 /* 13635 * If we failed to allocate the request, and if the DMA didn't fail 13636 * anyway, set busy status. This is just a resource allocation 13637 * failure. 13638 */ 13639 if ((rq == NULL) 13640 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 13641 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 13642 ctl_set_busy(&io->scsiio); 13643 13644 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 13645 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 13646 if (rq != NULL) 13647 ctl_dt_req_free(rq); 13648 13649 /* 13650 * The data move failed. We need to return status back 13651 * to the other controller. No point in trying to DMA 13652 * data to the remote controller. 13653 */ 13654 13655 ctl_send_datamove_done(io, /*have_lock*/ 0); 13656 13657 return (1); 13658 } 13659 13660 local_sglist = CTL_LSGL(io); 13661 remote_sglist = CTL_RSGL(io); 13662 local_used = 0; 13663 remote_used = 0; 13664 total_used = 0; 13665 13666 /* 13667 * Pull/push the data over the wire from/to the other controller. 13668 * This takes into account the possibility that the local and 13669 * remote sglists may not be identical in terms of the size of 13670 * the elements and the number of elements. 13671 * 13672 * One fundamental assumption here is that the length allocated for 13673 * both the local and remote sglists is identical. Otherwise, we've 13674 * essentially got a coding error of some sort. 13675 */ 13676 isc_ret = CTL_HA_STATUS_SUCCESS; 13677 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13678 uint32_t cur_len; 13679 uint8_t *tmp_ptr; 13680 13681 rq->command = command; 13682 rq->context = io; 13683 13684 /* 13685 * Both pointers should be aligned. But it is possible 13686 * that the allocation length is not. They should both 13687 * also have enough slack left over at the end, though, 13688 * to round up to the next 8 byte boundary. 13689 */ 13690 cur_len = MIN(local_sglist[i].len - local_used, 13691 remote_sglist[j].len - remote_used); 13692 rq->size = cur_len; 13693 13694 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13695 tmp_ptr += local_used; 13696 13697 #if 0 13698 /* Use physical addresses when talking to ISC hardware */ 13699 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13700 /* XXX KDM use busdma */ 13701 rq->local = vtophys(tmp_ptr); 13702 } else 13703 rq->local = tmp_ptr; 13704 #else 13705 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 13706 ("HA does not support BUS_ADDR")); 13707 rq->local = tmp_ptr; 13708 #endif 13709 13710 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13711 tmp_ptr += remote_used; 13712 rq->remote = tmp_ptr; 13713 13714 rq->callback = NULL; 13715 13716 local_used += cur_len; 13717 if (local_used >= local_sglist[i].len) { 13718 i++; 13719 local_used = 0; 13720 } 13721 13722 remote_used += cur_len; 13723 if (remote_used >= remote_sglist[j].len) { 13724 j++; 13725 remote_used = 0; 13726 } 13727 total_used += cur_len; 13728 13729 if (total_used >= io->scsiio.kern_data_len) 13730 rq->callback = callback; 13731 13732 isc_ret = ctl_dt_single(rq); 13733 if (isc_ret > CTL_HA_STATUS_SUCCESS) 13734 break; 13735 } 13736 if (isc_ret != CTL_HA_STATUS_WAIT) { 13737 rq->ret = isc_ret; 13738 callback(rq); 13739 } 13740 13741 return (0); 13742 } 13743 13744 static void 13745 ctl_datamove_remote_read(union ctl_io *io) 13746 { 13747 int retval; 13748 uint32_t i; 13749 13750 /* 13751 * This will send an error to the other controller in the case of a 13752 * failure. 13753 */ 13754 retval = ctl_datamove_remote_sgl_setup(io); 13755 if (retval != 0) 13756 return; 13757 13758 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13759 ctl_datamove_remote_read_cb); 13760 if (retval != 0) { 13761 /* 13762 * Make sure we free memory if there was an error.. The 13763 * ctl_datamove_remote_xfer() function will send the 13764 * datamove done message, or call the callback with an 13765 * error if there is a problem. 13766 */ 13767 for (i = 0; i < ctl_kern_sg_entries(io); i++) 13768 free(CTL_LSGLT(io)[i].addr, M_CTL); 13769 free(CTL_RSGL(io), M_CTL); 13770 CTL_RSGL(io) = NULL; 13771 CTL_LSGL(io) = NULL; 13772 } 13773 } 13774 13775 /* 13776 * Process a datamove request from the other controller. This is used for 13777 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13778 * first. Once that is complete, the data gets DMAed into the remote 13779 * controller's memory. For reads, we DMA from the remote controller's 13780 * memory into our memory first, and then move it out to the FETD. 13781 */ 13782 static void 13783 ctl_datamove_remote(union ctl_io *io) 13784 { 13785 CTL_IO_ASSERT(io, SCSI); 13786 13787 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 13788 13789 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13790 ctl_failover_io(io, /*have_lock*/ 0); 13791 return; 13792 } 13793 13794 /* 13795 * Note that we look for an aborted I/O here, but don't do some of 13796 * the other checks that ctl_datamove() normally does. 13797 * We don't need to run the datamove delay code, since that should 13798 * have been done if need be on the other controller. 13799 */ 13800 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13801 printf("%s: tag 0x%jx on (%u:%u:%u) aborted\n", __func__, 13802 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13803 io->io_hdr.nexus.targ_port, 13804 io->io_hdr.nexus.targ_lun); 13805 io->io_hdr.port_status = 31338; 13806 ctl_send_datamove_done(io, /*have_lock*/ 0); 13807 return; 13808 } 13809 13810 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13811 ctl_datamove_remote_write(io); 13812 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13813 ctl_datamove_remote_read(io); 13814 else { 13815 io->io_hdr.port_status = 31339; 13816 ctl_send_datamove_done(io, /*have_lock*/ 0); 13817 } 13818 } 13819 13820 static void 13821 ctl_process_done(union ctl_io *io) 13822 { 13823 struct ctl_softc *softc = CTL_SOFTC(io); 13824 struct ctl_port *port = CTL_PORT(io); 13825 struct ctl_lun *lun = CTL_LUN(io); 13826 void (*fe_done)(union ctl_io *io); 13827 union ctl_ha_msg msg; 13828 13829 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13830 fe_done = port->fe_done; 13831 13832 #ifdef CTL_TIME_IO 13833 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13834 char str[256]; 13835 char path_str[64]; 13836 struct sbuf sb; 13837 13838 ctl_scsi_path_string(&io->io_hdr, path_str, sizeof(path_str)); 13839 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13840 13841 ctl_io_sbuf(io, &sb); 13842 sbuf_cat(&sb, path_str); 13843 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13844 (intmax_t)time_uptime - io->io_hdr.start_time); 13845 sbuf_finish(&sb); 13846 printf("%s", sbuf_data(&sb)); 13847 } 13848 #endif /* CTL_TIME_IO */ 13849 13850 switch (io->io_hdr.io_type) { 13851 case CTL_IO_SCSI: 13852 case CTL_IO_NVME: 13853 case CTL_IO_NVME_ADMIN: 13854 break; 13855 case CTL_IO_TASK: 13856 if (ctl_debug & CTL_DEBUG_INFO) 13857 ctl_io_error_print(io, NULL); 13858 fe_done(io); 13859 return; 13860 default: 13861 panic("%s: Invalid CTL I/O type %d\n", 13862 __func__, io->io_hdr.io_type); 13863 } 13864 13865 if (lun == NULL) { 13866 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13867 io->io_hdr.nexus.targ_mapped_lun)); 13868 goto bailout; 13869 } 13870 13871 mtx_lock(&lun->lun_lock); 13872 13873 /* 13874 * Check to see if we have any informational exception and status 13875 * of this command can be modified to report it in form of either 13876 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13877 */ 13878 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13879 io->io_hdr.status == CTL_SUCCESS && 13880 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13881 uint8_t mrie = lun->MODE_IE.mrie; 13882 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13883 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13884 13885 CTL_IO_ASSERT(io, SCSI); 13886 if (((mrie == SIEP_MRIE_REC_COND && per) || 13887 mrie == SIEP_MRIE_REC_UNCOND || 13888 mrie == SIEP_MRIE_NO_SENSE) && 13889 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13890 CTL_CMD_FLAG_NO_SENSE) == 0) { 13891 ctl_set_sense(&io->scsiio, 13892 /*current_error*/ 1, 13893 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13894 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13895 /*asc*/ lun->ie_asc, 13896 /*ascq*/ lun->ie_ascq, 13897 SSD_ELEM_NONE); 13898 lun->ie_reported = 1; 13899 } 13900 } else if (lun->ie_reported < 0) 13901 lun->ie_reported = 0; 13902 13903 /* 13904 * Check to see if we have any errors to inject here. We only 13905 * inject errors for commands that don't already have errors set. 13906 */ 13907 if (!STAILQ_EMPTY(&lun->error_list) && 13908 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13909 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13910 ctl_inject_error(lun, io); 13911 13912 /* 13913 * XXX KDM how do we treat commands that aren't completed 13914 * successfully? 13915 * 13916 * XXX KDM should we also track I/O latency? 13917 */ 13918 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13919 (io->io_hdr.io_type == CTL_IO_SCSI || 13920 io->io_hdr.io_type == CTL_IO_NVME || 13921 io->io_hdr.io_type == CTL_IO_NVME_ADMIN)) { 13922 int type; 13923 #ifdef CTL_TIME_IO 13924 struct bintime bt; 13925 13926 getbinuptime(&bt); 13927 bintime_sub(&bt, &io->io_hdr.start_bt); 13928 #endif 13929 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13930 CTL_FLAG_DATA_IN) 13931 type = CTL_STATS_READ; 13932 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13933 CTL_FLAG_DATA_OUT) 13934 type = CTL_STATS_WRITE; 13935 else 13936 type = CTL_STATS_NO_IO; 13937 13938 lun->stats.bytes[type] += ctl_kern_total_len(io); 13939 lun->stats.operations[type] ++; 13940 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13941 #ifdef CTL_TIME_IO 13942 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13943 bintime_add(&lun->stats.time[type], &bt); 13944 #endif 13945 13946 mtx_lock(&port->port_lock); 13947 port->stats.bytes[type] += ctl_kern_total_len(io); 13948 port->stats.operations[type] ++; 13949 port->stats.dmas[type] += io->io_hdr.num_dmas; 13950 #ifdef CTL_TIME_IO 13951 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13952 bintime_add(&port->stats.time[type], &bt); 13953 #endif 13954 mtx_unlock(&port->port_lock); 13955 } 13956 13957 /* 13958 * Run through the blocked queue of this I/O and see if anything 13959 * can be unblocked, now that this I/O is done and will be removed. 13960 * We need to do it before removal to have OOA position to start. 13961 */ 13962 ctl_try_unblock_others(lun, io, TRUE); 13963 13964 /* 13965 * Remove this from the OOA queue. 13966 */ 13967 LIST_REMOVE(&io->io_hdr, ooa_links); 13968 #ifdef CTL_TIME_IO 13969 if (LIST_EMPTY(&lun->ooa_queue)) 13970 lun->last_busy = getsbinuptime(); 13971 #endif 13972 13973 /* 13974 * If the LUN has been invalidated, free it if there is nothing 13975 * left on its OOA queue. 13976 */ 13977 if ((lun->flags & CTL_LUN_INVALID) 13978 && LIST_EMPTY(&lun->ooa_queue)) { 13979 mtx_unlock(&lun->lun_lock); 13980 ctl_free_lun(lun); 13981 } else 13982 mtx_unlock(&lun->lun_lock); 13983 13984 bailout: 13985 13986 /* 13987 * If this command has been aborted, make sure we set the status 13988 * properly. The FETD is responsible for freeing the I/O and doing 13989 * whatever it needs to do to clean up its state. 13990 */ 13991 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13992 switch (io->io_hdr.io_type) { 13993 case CTL_IO_SCSI: 13994 ctl_set_task_aborted(&io->scsiio); 13995 break; 13996 case CTL_IO_NVME: 13997 case CTL_IO_NVME_ADMIN: 13998 ctl_nvme_set_command_aborted(&io->nvmeio); 13999 break; 14000 default: 14001 __assert_unreachable(); 14002 } 14003 } 14004 14005 /* 14006 * If enabled, print command error status. 14007 */ 14008 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 14009 (ctl_debug & CTL_DEBUG_INFO) != 0) 14010 ctl_io_error_print(io, NULL); 14011 14012 /* 14013 * Tell the FETD or the other shelf controller we're done with this 14014 * command. Note that only SCSI commands get to this point. Task 14015 * management commands are completed above. 14016 */ 14017 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 14018 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 14019 memset(&msg, 0, sizeof(msg)); 14020 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 14021 msg.hdr.serializing_sc = io->io_hdr.remote_io; 14022 msg.hdr.nexus = io->io_hdr.nexus; 14023 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 14024 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 14025 M_WAITOK); 14026 } 14027 14028 fe_done(io); 14029 } 14030 14031 /* 14032 * Front end should call this if it doesn't do autosense. When the request 14033 * sense comes back in from the initiator, we'll dequeue this and send it. 14034 */ 14035 int 14036 ctl_queue_sense(union ctl_io *io) 14037 { 14038 struct ctl_softc *softc = CTL_SOFTC(io); 14039 struct ctl_port *port = CTL_PORT(io); 14040 struct ctl_lun *lun; 14041 struct scsi_sense_data *ps; 14042 uint32_t initidx, p, targ_lun; 14043 14044 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 14045 CTL_IO_ASSERT(io, SCSI); 14046 14047 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 14048 14049 /* 14050 * LUN lookup will likely move to the ctl_work_thread() once we 14051 * have our new queueing infrastructure (that doesn't put things on 14052 * a per-LUN queue initially). That is so that we can handle 14053 * things like an INQUIRY to a LUN that we don't have enabled. We 14054 * can't deal with that right now. 14055 * If we don't have a LUN for this, just toss the sense information. 14056 */ 14057 mtx_lock(&softc->ctl_lock); 14058 if (targ_lun >= ctl_max_luns || 14059 (lun = softc->ctl_luns[targ_lun]) == NULL) { 14060 mtx_unlock(&softc->ctl_lock); 14061 goto bailout; 14062 } 14063 mtx_lock(&lun->lun_lock); 14064 mtx_unlock(&softc->ctl_lock); 14065 14066 initidx = ctl_get_initindex(&io->io_hdr.nexus); 14067 p = initidx / CTL_MAX_INIT_PER_PORT; 14068 if (lun->pending_sense[p] == NULL) { 14069 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 14070 M_CTL, M_NOWAIT | M_ZERO); 14071 } 14072 if ((ps = lun->pending_sense[p]) != NULL) { 14073 ps += initidx % CTL_MAX_INIT_PER_PORT; 14074 memset(ps, 0, sizeof(*ps)); 14075 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 14076 } 14077 mtx_unlock(&lun->lun_lock); 14078 14079 bailout: 14080 ctl_free_io(io); 14081 return (CTL_RETVAL_COMPLETE); 14082 } 14083 14084 /* 14085 * Primary command inlet from frontend ports. All SCSI and task I/O 14086 * requests must go through this function. 14087 */ 14088 int 14089 ctl_queue(union ctl_io *io) 14090 { 14091 struct ctl_port *port = CTL_PORT(io); 14092 14093 switch (io->io_hdr.io_type) { 14094 case CTL_IO_SCSI: 14095 case CTL_IO_TASK: 14096 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 14097 break; 14098 case CTL_IO_NVME: 14099 CTL_DEBUG_PRINT(("ctl_queue nvme nvm cmd=%02X\n", 14100 io->nvmeio.cmd.opc)); 14101 break; 14102 case CTL_IO_NVME_ADMIN: 14103 CTL_DEBUG_PRINT(("ctl_queue nvme admin cmd=%02X\n", 14104 io->nvmeio.cmd.opc)); 14105 break; 14106 default: 14107 break; 14108 } 14109 14110 #ifdef CTL_TIME_IO 14111 io->io_hdr.start_time = time_uptime; 14112 getbinuptime(&io->io_hdr.start_bt); 14113 #endif /* CTL_TIME_IO */ 14114 14115 /* Map FE-specific LUN ID into global one. */ 14116 io->io_hdr.nexus.targ_mapped_lun = 14117 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 14118 14119 switch (io->io_hdr.io_type) { 14120 case CTL_IO_SCSI: 14121 case CTL_IO_TASK: 14122 case CTL_IO_NVME: 14123 case CTL_IO_NVME_ADMIN: 14124 if (ctl_debug & CTL_DEBUG_CDB) 14125 ctl_io_print(io); 14126 ctl_enqueue_incoming(io); 14127 break; 14128 default: 14129 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 14130 return (EINVAL); 14131 } 14132 14133 return (CTL_RETVAL_COMPLETE); 14134 } 14135 14136 int 14137 ctl_run(union ctl_io *io) 14138 { 14139 struct ctl_port *port = CTL_PORT(io); 14140 14141 CTL_DEBUG_PRINT(("ctl_run cdb[0]=%02X\n", io->scsiio.cdb[0])); 14142 14143 #ifdef CTL_TIME_IO 14144 io->io_hdr.start_time = time_uptime; 14145 getbinuptime(&io->io_hdr.start_bt); 14146 #endif /* CTL_TIME_IO */ 14147 14148 /* Map FE-specific LUN ID into global one. */ 14149 io->io_hdr.nexus.targ_mapped_lun = 14150 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 14151 14152 switch (io->io_hdr.io_type) { 14153 case CTL_IO_SCSI: 14154 if (ctl_debug & CTL_DEBUG_CDB) 14155 ctl_io_print(io); 14156 ctl_scsiio_precheck(&io->scsiio); 14157 break; 14158 case CTL_IO_TASK: 14159 if (ctl_debug & CTL_DEBUG_CDB) 14160 ctl_io_print(io); 14161 ctl_run_task(io); 14162 break; 14163 case CTL_IO_NVME: 14164 case CTL_IO_NVME_ADMIN: 14165 if (ctl_debug & CTL_DEBUG_CDB) 14166 ctl_io_print(io); 14167 ctl_nvmeio_precheck(&io->nvmeio); 14168 break; 14169 default: 14170 printf("ctl_run: unknown I/O type %d\n", io->io_hdr.io_type); 14171 return (EINVAL); 14172 } 14173 14174 return (CTL_RETVAL_COMPLETE); 14175 } 14176 14177 #ifdef CTL_IO_DELAY 14178 static void 14179 ctl_done_timer_wakeup(void *arg) 14180 { 14181 union ctl_io *io; 14182 14183 io = (union ctl_io *)arg; 14184 ctl_done(io); 14185 } 14186 #endif /* CTL_IO_DELAY */ 14187 14188 void 14189 ctl_serseq_done(union ctl_io *io) 14190 { 14191 struct ctl_lun *lun = CTL_LUN(io); 14192 14193 /* This is racy, but should not be a problem. */ 14194 if (!TAILQ_EMPTY(&io->io_hdr.blocked_queue)) { 14195 mtx_lock(&lun->lun_lock); 14196 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 14197 ctl_try_unblock_others(lun, io, FALSE); 14198 mtx_unlock(&lun->lun_lock); 14199 } else 14200 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 14201 } 14202 14203 void 14204 ctl_done(union ctl_io *io) 14205 { 14206 14207 /* 14208 * Enable this to catch duplicate completion issues. 14209 */ 14210 #if 0 14211 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 14212 switch (io->io_hdr.io_type) { 14213 case CTL_IO_SCSI: 14214 case CTL_IO_TASK: 14215 printf("%s: type %d msg %d cdb %x iptl: " 14216 "%u:%u:%u tag 0x%04lx " 14217 "flag %#x status %x\n", 14218 __func__, 14219 io->io_hdr.io_type, 14220 io->io_hdr.msg_type, 14221 io->scsiio.cdb[0], 14222 io->io_hdr.nexus.initid, 14223 io->io_hdr.nexus.targ_port, 14224 io->io_hdr.nexus.targ_lun, 14225 (io->io_hdr.io_type == CTL_IO_TASK) ? 14226 io->taskio.tag_num : 14227 io->scsiio.tag_num, 14228 io->io_hdr.flags, 14229 io->io_hdr.status); 14230 break; 14231 case CTL_IO_NVME: 14232 case CTL_IO_NVME_ADMIN: 14233 printf("%s: type %d msg %d opc %x iptl: " 14234 "%u:%u:%u cid 0x%04x " 14235 "flag %#x status %x\n", 14236 __func__, 14237 io->io_hdr.io_type, 14238 io->io_hdr.msg_type, 14239 io->nvmeio.cmd.opc, 14240 io->io_hdr.nexus.initid, 14241 io->io_hdr.nexus.targ_port, 14242 io->io_hdr.nexus.targ_lun, 14243 io->nvmeio.cmd.cid, 14244 io->io_hdr.flags, 14245 io->io_hdr.status); 14246 break; 14247 default: 14248 printf("%s: type %d msg %d iptl: " 14249 "%u:%u:%u flag %#x status %x\n", 14250 __func__, 14251 io->io_hdr.io_type, 14252 io->io_hdr.msg_type, 14253 io->io_hdr.nexus.initid, 14254 io->io_hdr.nexus.targ_port, 14255 io->io_hdr.nexus.targ_lun, 14256 io->io_hdr.flags, 14257 io->io_hdr.status); 14258 break; 14259 } 14260 } else 14261 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 14262 #endif 14263 14264 /* 14265 * This is an internal copy of an I/O, and should not go through 14266 * the normal done processing logic. 14267 */ 14268 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 14269 return; 14270 14271 #ifdef CTL_IO_DELAY 14272 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 14273 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14274 } else { 14275 struct ctl_lun *lun = CTL_LUN(io); 14276 14277 if ((lun != NULL) 14278 && (lun->delay_info.done_delay > 0)) { 14279 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 14280 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14281 callout_reset(&io->io_hdr.delay_callout, 14282 lun->delay_info.done_delay * hz, 14283 ctl_done_timer_wakeup, io); 14284 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14285 lun->delay_info.done_delay = 0; 14286 return; 14287 } 14288 } 14289 #endif /* CTL_IO_DELAY */ 14290 14291 ctl_enqueue_done(io); 14292 } 14293 14294 static void 14295 ctl_work_thread(void *arg) 14296 { 14297 struct ctl_thread *thr = (struct ctl_thread *)arg; 14298 struct ctl_softc *softc = thr->ctl_softc; 14299 union ctl_io *io; 14300 int retval; 14301 14302 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14303 thread_lock(curthread); 14304 sched_prio(curthread, PUSER - 1); 14305 thread_unlock(curthread); 14306 14307 while (!softc->shutdown) { 14308 /* 14309 * We handle the queues in this order: 14310 * - ISC 14311 * - done queue (to free up resources, unblock other commands) 14312 * - incoming queue 14313 * - RtR queue 14314 * 14315 * If those queues are empty, we break out of the loop and 14316 * go to sleep. 14317 */ 14318 mtx_lock(&thr->queue_lock); 14319 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14320 if (io != NULL) { 14321 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14322 mtx_unlock(&thr->queue_lock); 14323 ctl_handle_isc(io); 14324 continue; 14325 } 14326 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14327 if (io != NULL) { 14328 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14329 /* clear any blocked commands, call fe_done */ 14330 mtx_unlock(&thr->queue_lock); 14331 ctl_process_done(io); 14332 continue; 14333 } 14334 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14335 if (io != NULL) { 14336 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14337 mtx_unlock(&thr->queue_lock); 14338 switch (io->io_hdr.io_type) { 14339 case CTL_IO_TASK: 14340 ctl_run_task(io); 14341 break; 14342 case CTL_IO_SCSI: 14343 ctl_scsiio_precheck(&io->scsiio); 14344 break; 14345 case CTL_IO_NVME: 14346 case CTL_IO_NVME_ADMIN: 14347 ctl_nvmeio_precheck(&io->nvmeio); 14348 break; 14349 default: 14350 __assert_unreachable(); 14351 } 14352 continue; 14353 } 14354 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14355 if (io != NULL) { 14356 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14357 mtx_unlock(&thr->queue_lock); 14358 switch (io->io_hdr.io_type) { 14359 case CTL_IO_SCSI: 14360 retval = ctl_scsiio(&io->scsiio); 14361 if (retval != CTL_RETVAL_COMPLETE) 14362 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14363 break; 14364 case CTL_IO_NVME: 14365 case CTL_IO_NVME_ADMIN: 14366 retval = ctl_nvmeio(&io->nvmeio); 14367 if (retval != CTL_RETVAL_COMPLETE) 14368 CTL_DEBUG_PRINT(("ctl_nvmeio failed\n")); 14369 break; 14370 default: 14371 __assert_unreachable(); 14372 } 14373 continue; 14374 } 14375 14376 /* Sleep until we have something to do. */ 14377 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0); 14378 } 14379 thr->thread = NULL; 14380 kthread_exit(); 14381 } 14382 14383 static void 14384 ctl_thresh_thread(void *arg) 14385 { 14386 struct ctl_softc *softc = (struct ctl_softc *)arg; 14387 struct ctl_lun *lun; 14388 struct ctl_logical_block_provisioning_page *page; 14389 const char *attr; 14390 union ctl_ha_msg msg; 14391 uint64_t thres, val; 14392 int i, e, set; 14393 14394 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14395 thread_lock(curthread); 14396 sched_prio(curthread, PUSER - 1); 14397 thread_unlock(curthread); 14398 14399 while (!softc->shutdown) { 14400 mtx_lock(&softc->ctl_lock); 14401 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14402 if ((lun->flags & CTL_LUN_DISABLED) || 14403 (lun->flags & CTL_LUN_NO_MEDIA) || 14404 lun->backend->lun_attr == NULL) 14405 continue; 14406 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 14407 softc->ha_mode == CTL_HA_MODE_XFER) 14408 continue; 14409 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 14410 continue; 14411 e = 0; 14412 page = &lun->MODE_LBP; 14413 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14414 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14415 continue; 14416 thres = scsi_4btoul(page->descr[i].count); 14417 thres <<= CTL_LBP_EXPONENT; 14418 switch (page->descr[i].resource) { 14419 case 0x01: 14420 attr = "blocksavail"; 14421 break; 14422 case 0x02: 14423 attr = "blocksused"; 14424 break; 14425 case 0xf1: 14426 attr = "poolblocksavail"; 14427 break; 14428 case 0xf2: 14429 attr = "poolblocksused"; 14430 break; 14431 default: 14432 continue; 14433 } 14434 mtx_unlock(&softc->ctl_lock); // XXX 14435 val = lun->backend->lun_attr(lun->be_lun, attr); 14436 mtx_lock(&softc->ctl_lock); 14437 if (val == UINT64_MAX) 14438 continue; 14439 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14440 == SLBPPD_ARMING_INC) 14441 e = (val >= thres); 14442 else 14443 e = (val <= thres); 14444 if (e) 14445 break; 14446 } 14447 mtx_lock(&lun->lun_lock); 14448 if (e) { 14449 scsi_u64to8b((uint8_t *)&page->descr[i] - 14450 (uint8_t *)page, lun->ua_tpt_info); 14451 if (lun->lasttpt == 0 || 14452 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14453 lun->lasttpt = time_uptime; 14454 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14455 set = 1; 14456 } else 14457 set = 0; 14458 } else { 14459 lun->lasttpt = 0; 14460 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14461 set = -1; 14462 } 14463 mtx_unlock(&lun->lun_lock); 14464 if (set != 0 && 14465 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 14466 /* Send msg to other side. */ 14467 bzero(&msg.ua, sizeof(msg.ua)); 14468 msg.hdr.msg_type = CTL_MSG_UA; 14469 msg.hdr.nexus.initid = -1; 14470 msg.hdr.nexus.targ_port = -1; 14471 msg.hdr.nexus.targ_lun = lun->lun; 14472 msg.hdr.nexus.targ_mapped_lun = lun->lun; 14473 msg.ua.ua_all = 1; 14474 msg.ua.ua_set = (set > 0); 14475 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 14476 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 14477 mtx_unlock(&softc->ctl_lock); // XXX 14478 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 14479 sizeof(msg.ua), M_WAITOK); 14480 mtx_lock(&softc->ctl_lock); 14481 } 14482 } 14483 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 14484 PDROP, "-", CTL_LBP_PERIOD * hz); 14485 } 14486 softc->thresh_thread = NULL; 14487 kthread_exit(); 14488 } 14489 14490 static void 14491 ctl_enqueue_incoming(union ctl_io *io) 14492 { 14493 struct ctl_softc *softc = CTL_SOFTC(io); 14494 struct ctl_thread *thr; 14495 u_int idx; 14496 14497 idx = (io->io_hdr.nexus.targ_port * 127 + 14498 io->io_hdr.nexus.initid) % worker_threads; 14499 thr = &softc->threads[idx]; 14500 mtx_lock(&thr->queue_lock); 14501 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14502 mtx_unlock(&thr->queue_lock); 14503 wakeup(thr); 14504 } 14505 14506 static void 14507 ctl_enqueue_rtr(union ctl_io *io) 14508 { 14509 struct ctl_softc *softc = CTL_SOFTC(io); 14510 struct ctl_thread *thr; 14511 14512 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14513 mtx_lock(&thr->queue_lock); 14514 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14515 mtx_unlock(&thr->queue_lock); 14516 wakeup(thr); 14517 } 14518 14519 static void 14520 ctl_enqueue_done(union ctl_io *io) 14521 { 14522 struct ctl_softc *softc = CTL_SOFTC(io); 14523 struct ctl_thread *thr; 14524 14525 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14526 mtx_lock(&thr->queue_lock); 14527 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14528 mtx_unlock(&thr->queue_lock); 14529 wakeup(thr); 14530 } 14531 14532 static void 14533 ctl_enqueue_isc(union ctl_io *io) 14534 { 14535 struct ctl_softc *softc = CTL_SOFTC(io); 14536 struct ctl_thread *thr; 14537 14538 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14539 mtx_lock(&thr->queue_lock); 14540 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14541 mtx_unlock(&thr->queue_lock); 14542 wakeup(thr); 14543 } 14544 14545 /* 14546 * vim: ts=8 14547 */ 14548