1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37/* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43#define _CTL_C 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 317320 2017-04-23 07:35:51Z mav $"); 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/ctype.h> 51#include <sys/kernel.h> 52#include <sys/types.h> 53#include <sys/kthread.h> 54#include <sys/bio.h> 55#include <sys/fcntl.h> 56#include <sys/lock.h> 57#include <sys/module.h> 58#include <sys/mutex.h> 59#include <sys/condvar.h> 60#include <sys/malloc.h> 61#include <sys/conf.h> 62#include <sys/ioccom.h> 63#include <sys/queue.h> 64#include <sys/sbuf.h> 65#include <sys/smp.h> 66#include <sys/endian.h> 67#include <sys/sysctl.h> 68#include <vm/uma.h> 69 70#include <cam/cam.h> 71#include <cam/scsi/scsi_all.h> 72#include <cam/scsi/scsi_cd.h> 73#include <cam/scsi/scsi_da.h> 74#include <cam/ctl/ctl_io.h> 75#include <cam/ctl/ctl.h> 76#include <cam/ctl/ctl_frontend.h> 77#include <cam/ctl/ctl_util.h> 78#include <cam/ctl/ctl_backend.h> 79#include <cam/ctl/ctl_ioctl.h> 80#include <cam/ctl/ctl_ha.h> 81#include <cam/ctl/ctl_private.h> 82#include <cam/ctl/ctl_debug.h> 83#include <cam/ctl/ctl_scsi_all.h> 84#include <cam/ctl/ctl_error.h> 85 86struct ctl_softc *control_softc = NULL; 87 88/* 89 * Template mode pages. 90 */ 91 92/* 93 * Note that these are default values only. The actual values will be 94 * filled in when the user does a mode sense. 95 */ 96const static struct scsi_da_rw_recovery_page rw_er_page_default = { 97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 100 /*read_retry_count*/0, 101 /*correction_span*/0, 102 /*head_offset_count*/0, 103 /*data_strobe_offset_cnt*/0, 104 /*byte8*/SMS_RWER_LBPERE, 105 /*write_retry_count*/0, 106 /*reserved2*/0, 107 /*recovery_time_limit*/{0, 0}, 108}; 109 110const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 113 /*byte3*/SMS_RWER_PER, 114 /*read_retry_count*/0, 115 /*correction_span*/0, 116 /*head_offset_count*/0, 117 /*data_strobe_offset_cnt*/0, 118 /*byte8*/SMS_RWER_LBPERE, 119 /*write_retry_count*/0, 120 /*reserved2*/0, 121 /*recovery_time_limit*/{0, 0}, 122}; 123 124const static struct scsi_format_page format_page_default = { 125 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 126 /*page_length*/sizeof(struct scsi_format_page) - 2, 127 /*tracks_per_zone*/ {0, 0}, 128 /*alt_sectors_per_zone*/ {0, 0}, 129 /*alt_tracks_per_zone*/ {0, 0}, 130 /*alt_tracks_per_lun*/ {0, 0}, 131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 133 /*bytes_per_sector*/ {0, 0}, 134 /*interleave*/ {0, 0}, 135 /*track_skew*/ {0, 0}, 136 /*cylinder_skew*/ {0, 0}, 137 /*flags*/ SFP_HSEC, 138 /*reserved*/ {0, 0, 0} 139}; 140 141const static struct scsi_format_page format_page_changeable = { 142 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 143 /*page_length*/sizeof(struct scsi_format_page) - 2, 144 /*tracks_per_zone*/ {0, 0}, 145 /*alt_sectors_per_zone*/ {0, 0}, 146 /*alt_tracks_per_zone*/ {0, 0}, 147 /*alt_tracks_per_lun*/ {0, 0}, 148 /*sectors_per_track*/ {0, 0}, 149 /*bytes_per_sector*/ {0, 0}, 150 /*interleave*/ {0, 0}, 151 /*track_skew*/ {0, 0}, 152 /*cylinder_skew*/ {0, 0}, 153 /*flags*/ 0, 154 /*reserved*/ {0, 0, 0} 155}; 156 157const static struct scsi_rigid_disk_page rigid_disk_page_default = { 158 /*page_code*/SMS_RIGID_DISK_PAGE, 159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 160 /*cylinders*/ {0, 0, 0}, 161 /*heads*/ CTL_DEFAULT_HEADS, 162 /*start_write_precomp*/ {0, 0, 0}, 163 /*start_reduced_current*/ {0, 0, 0}, 164 /*step_rate*/ {0, 0}, 165 /*landing_zone_cylinder*/ {0, 0, 0}, 166 /*rpl*/ SRDP_RPL_DISABLED, 167 /*rotational_offset*/ 0, 168 /*reserved1*/ 0, 169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 170 CTL_DEFAULT_ROTATION_RATE & 0xff}, 171 /*reserved2*/ {0, 0} 172}; 173 174const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 175 /*page_code*/SMS_RIGID_DISK_PAGE, 176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 177 /*cylinders*/ {0, 0, 0}, 178 /*heads*/ 0, 179 /*start_write_precomp*/ {0, 0, 0}, 180 /*start_reduced_current*/ {0, 0, 0}, 181 /*step_rate*/ {0, 0}, 182 /*landing_zone_cylinder*/ {0, 0, 0}, 183 /*rpl*/ 0, 184 /*rotational_offset*/ 0, 185 /*reserved1*/ 0, 186 /*rotation_rate*/ {0, 0}, 187 /*reserved2*/ {0, 0} 188}; 189 190const static struct scsi_da_verify_recovery_page verify_er_page_default = { 191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 193 /*byte3*/0, 194 /*read_retry_count*/0, 195 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 196 /*recovery_time_limit*/{0, 0}, 197}; 198 199const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 202 /*byte3*/SMS_VER_PER, 203 /*read_retry_count*/0, 204 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 205 /*recovery_time_limit*/{0, 0}, 206}; 207 208const static struct scsi_caching_page caching_page_default = { 209 /*page_code*/SMS_CACHING_PAGE, 210 /*page_length*/sizeof(struct scsi_caching_page) - 2, 211 /*flags1*/ SCP_DISC | SCP_WCE, 212 /*ret_priority*/ 0, 213 /*disable_pf_transfer_len*/ {0xff, 0xff}, 214 /*min_prefetch*/ {0, 0}, 215 /*max_prefetch*/ {0xff, 0xff}, 216 /*max_pf_ceiling*/ {0xff, 0xff}, 217 /*flags2*/ 0, 218 /*cache_segments*/ 0, 219 /*cache_seg_size*/ {0, 0}, 220 /*reserved*/ 0, 221 /*non_cache_seg_size*/ {0, 0, 0} 222}; 223 224const static struct scsi_caching_page caching_page_changeable = { 225 /*page_code*/SMS_CACHING_PAGE, 226 /*page_length*/sizeof(struct scsi_caching_page) - 2, 227 /*flags1*/ SCP_WCE | SCP_RCD, 228 /*ret_priority*/ 0, 229 /*disable_pf_transfer_len*/ {0, 0}, 230 /*min_prefetch*/ {0, 0}, 231 /*max_prefetch*/ {0, 0}, 232 /*max_pf_ceiling*/ {0, 0}, 233 /*flags2*/ 0, 234 /*cache_segments*/ 0, 235 /*cache_seg_size*/ {0, 0}, 236 /*reserved*/ 0, 237 /*non_cache_seg_size*/ {0, 0, 0} 238}; 239 240const static struct scsi_control_page control_page_default = { 241 /*page_code*/SMS_CONTROL_MODE_PAGE, 242 /*page_length*/sizeof(struct scsi_control_page) - 2, 243 /*rlec*/0, 244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 245 /*eca_and_aen*/0, 246 /*flags4*/SCP_TAS, 247 /*aen_holdoff_period*/{0, 0}, 248 /*busy_timeout_period*/{0, 0}, 249 /*extended_selftest_completion_time*/{0, 0} 250}; 251 252const static struct scsi_control_page control_page_changeable = { 253 /*page_code*/SMS_CONTROL_MODE_PAGE, 254 /*page_length*/sizeof(struct scsi_control_page) - 2, 255 /*rlec*/SCP_DSENSE, 256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 257 /*eca_and_aen*/SCP_SWP, 258 /*flags4*/0, 259 /*aen_holdoff_period*/{0, 0}, 260 /*busy_timeout_period*/{0, 0}, 261 /*extended_selftest_completion_time*/{0, 0} 262}; 263 264#define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 265 266const static struct scsi_control_ext_page control_ext_page_default = { 267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 268 /*subpage_code*/0x01, 269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 270 /*flags*/0, 271 /*prio*/0, 272 /*max_sense*/0 273}; 274 275const static struct scsi_control_ext_page control_ext_page_changeable = { 276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 277 /*subpage_code*/0x01, 278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 279 /*flags*/0, 280 /*prio*/0, 281 /*max_sense*/0xff 282}; 283 284const static struct scsi_info_exceptions_page ie_page_default = { 285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 287 /*info_flags*/SIEP_FLAGS_EWASC, 288 /*mrie*/SIEP_MRIE_NO, 289 /*interval_timer*/{0, 0, 0, 0}, 290 /*report_count*/{0, 0, 0, 1} 291}; 292 293const static struct scsi_info_exceptions_page ie_page_changeable = { 294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 297 SIEP_FLAGS_LOGERR, 298 /*mrie*/0x0f, 299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 300 /*report_count*/{0xff, 0xff, 0xff, 0xff} 301}; 302 303#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 304 305const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 307 /*subpage_code*/0x02, 308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 309 /*flags*/0, 310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 311 /*descr*/{}}, 312 {{/*flags*/0, 313 /*resource*/0x01, 314 /*reserved*/{0, 0}, 315 /*count*/{0, 0, 0, 0}}, 316 {/*flags*/0, 317 /*resource*/0x02, 318 /*reserved*/{0, 0}, 319 /*count*/{0, 0, 0, 0}}, 320 {/*flags*/0, 321 /*resource*/0xf1, 322 /*reserved*/{0, 0}, 323 /*count*/{0, 0, 0, 0}}, 324 {/*flags*/0, 325 /*resource*/0xf2, 326 /*reserved*/{0, 0}, 327 /*count*/{0, 0, 0, 0}} 328 } 329}; 330 331const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 333 /*subpage_code*/0x02, 334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 335 /*flags*/SLBPP_SITUA, 336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 337 /*descr*/{}}, 338 {{/*flags*/0, 339 /*resource*/0, 340 /*reserved*/{0, 0}, 341 /*count*/{0, 0, 0, 0}}, 342 {/*flags*/0, 343 /*resource*/0, 344 /*reserved*/{0, 0}, 345 /*count*/{0, 0, 0, 0}}, 346 {/*flags*/0, 347 /*resource*/0, 348 /*reserved*/{0, 0}, 349 /*count*/{0, 0, 0, 0}}, 350 {/*flags*/0, 351 /*resource*/0, 352 /*reserved*/{0, 0}, 353 /*count*/{0, 0, 0, 0}} 354 } 355}; 356 357const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 358 /*page_code*/SMS_CDDVD_CAPS_PAGE, 359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 360 /*caps1*/0x3f, 361 /*caps2*/0x00, 362 /*caps3*/0xf0, 363 /*caps4*/0x00, 364 /*caps5*/0x29, 365 /*caps6*/0x00, 366 /*obsolete*/{0, 0}, 367 /*nvol_levels*/{0, 0}, 368 /*buffer_size*/{8, 0}, 369 /*obsolete2*/{0, 0}, 370 /*reserved*/0, 371 /*digital*/0, 372 /*obsolete3*/0, 373 /*copy_management*/0, 374 /*reserved2*/0, 375 /*rotation_control*/0, 376 /*cur_write_speed*/0, 377 /*num_speed_descr*/0, 378}; 379 380const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 381 /*page_code*/SMS_CDDVD_CAPS_PAGE, 382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 383 /*caps1*/0, 384 /*caps2*/0, 385 /*caps3*/0, 386 /*caps4*/0, 387 /*caps5*/0, 388 /*caps6*/0, 389 /*obsolete*/{0, 0}, 390 /*nvol_levels*/{0, 0}, 391 /*buffer_size*/{0, 0}, 392 /*obsolete2*/{0, 0}, 393 /*reserved*/0, 394 /*digital*/0, 395 /*obsolete3*/0, 396 /*copy_management*/0, 397 /*reserved2*/0, 398 /*rotation_control*/0, 399 /*cur_write_speed*/0, 400 /*num_speed_descr*/0, 401}; 402 403SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 404static int worker_threads = -1; 405TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 406SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 407 &worker_threads, 1, "Number of worker threads"); 408static int ctl_debug = CTL_DEBUG_NONE; 409TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 410SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 411 &ctl_debug, 0, "Enabled debug flags"); 412static int ctl_lun_map_size = 1024; 413SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 414 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 415 416/* 417 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 418 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 419 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 420 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 421 */ 422#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 423 424static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 425 int param); 426static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 427static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 428static int ctl_init(void); 429static int ctl_shutdown(void); 430static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 431static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 432static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 433static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 434 struct ctl_ooa *ooa_hdr, 435 struct ctl_ooa_entry *kern_entries); 436static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 437 struct thread *td); 438static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 439 struct ctl_be_lun *be_lun); 440static int ctl_free_lun(struct ctl_lun *lun); 441static void ctl_create_lun(struct ctl_be_lun *be_lun); 442 443static int ctl_do_mode_select(union ctl_io *io); 444static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 445 uint64_t res_key, uint64_t sa_res_key, 446 uint8_t type, uint32_t residx, 447 struct ctl_scsiio *ctsio, 448 struct scsi_per_res_out *cdb, 449 struct scsi_per_res_out_parms* param); 450static void ctl_pro_preempt_other(struct ctl_lun *lun, 451 union ctl_ha_msg *msg); 452static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 453static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 454static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 455static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 456static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 457static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 458static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 459 int alloc_len); 460static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 461 int alloc_len); 462static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 463static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 464static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 465static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 466static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 467static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 468 bool seq); 469static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 470static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 471 union ctl_io *pending_io, union ctl_io *ooa_io); 472static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 473 union ctl_io *starting_io); 474static int ctl_check_blocked(struct ctl_lun *lun); 475static int ctl_scsiio_lun_check(struct ctl_lun *lun, 476 const struct ctl_cmd_entry *entry, 477 struct ctl_scsiio *ctsio); 478static void ctl_failover_lun(union ctl_io *io); 479static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 480 struct ctl_scsiio *ctsio); 481static int ctl_scsiio(struct ctl_scsiio *ctsio); 482 483static int ctl_target_reset(union ctl_io *io); 484static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, 485 ctl_ua_type ua_type); 486static int ctl_lun_reset(union ctl_io *io); 487static int ctl_abort_task(union ctl_io *io); 488static int ctl_abort_task_set(union ctl_io *io); 489static int ctl_query_task(union ctl_io *io, int task_set); 490static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 491 ctl_ua_type ua_type); 492static int ctl_i_t_nexus_reset(union ctl_io *io); 493static int ctl_query_async_event(union ctl_io *io); 494static void ctl_run_task(union ctl_io *io); 495#ifdef CTL_IO_DELAY 496static void ctl_datamove_timer_wakeup(void *arg); 497static void ctl_done_timer_wakeup(void *arg); 498#endif /* CTL_IO_DELAY */ 499 500static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 501static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 502static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 503static void ctl_datamove_remote_write(union ctl_io *io); 504static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 505static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 506static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 507static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 508 ctl_ha_dt_cb callback); 509static void ctl_datamove_remote_read(union ctl_io *io); 510static void ctl_datamove_remote(union ctl_io *io); 511static void ctl_process_done(union ctl_io *io); 512static void ctl_lun_thread(void *arg); 513static void ctl_thresh_thread(void *arg); 514static void ctl_work_thread(void *arg); 515static void ctl_enqueue_incoming(union ctl_io *io); 516static void ctl_enqueue_rtr(union ctl_io *io); 517static void ctl_enqueue_done(union ctl_io *io); 518static void ctl_enqueue_isc(union ctl_io *io); 519static const struct ctl_cmd_entry * 520 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 521static const struct ctl_cmd_entry * 522 ctl_validate_command(struct ctl_scsiio *ctsio); 523static int ctl_cmd_applicable(uint8_t lun_type, 524 const struct ctl_cmd_entry *entry); 525static int ctl_ha_init(void); 526static int ctl_ha_shutdown(void); 527 528static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 529static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 530static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 531static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 532 533/* 534 * Load the serialization table. This isn't very pretty, but is probably 535 * the easiest way to do it. 536 */ 537#include "ctl_ser_table.c" 538 539/* 540 * We only need to define open, close and ioctl routines for this driver. 541 */ 542static struct cdevsw ctl_cdevsw = { 543 .d_version = D_VERSION, 544 .d_flags = 0, 545 .d_open = ctl_open, 546 .d_close = ctl_close, 547 .d_ioctl = ctl_ioctl, 548 .d_name = "ctl", 549}; 550 551 552MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 553 554static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 555 556static moduledata_t ctl_moduledata = { 557 "ctl", 558 ctl_module_event_handler, 559 NULL 560}; 561 562DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 563MODULE_VERSION(ctl, 1); 564 565static struct ctl_frontend ha_frontend = 566{ 567 .name = "ha", 568 .init = ctl_ha_init, 569 .shutdown = ctl_ha_shutdown, 570}; 571 572static int 573ctl_ha_init(void) 574{ 575 struct ctl_softc *softc = control_softc; 576 577 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 578 &softc->othersc_pool) != 0) 579 return (ENOMEM); 580 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 581 ctl_pool_free(softc->othersc_pool); 582 return (EIO); 583 } 584 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 585 != CTL_HA_STATUS_SUCCESS) { 586 ctl_ha_msg_destroy(softc); 587 ctl_pool_free(softc->othersc_pool); 588 return (EIO); 589 } 590 return (0); 591}; 592 593static int 594ctl_ha_shutdown(void) 595{ 596 struct ctl_softc *softc = control_softc; 597 struct ctl_port *port; 598 599 ctl_ha_msg_shutdown(softc); 600 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS) 601 return (EIO); 602 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 603 return (EIO); 604 ctl_pool_free(softc->othersc_pool); 605 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) { 606 ctl_port_deregister(port); 607 free(port->port_name, M_CTL); 608 free(port, M_CTL); 609 } 610 return (0); 611}; 612 613static void 614ctl_ha_datamove(union ctl_io *io) 615{ 616 struct ctl_lun *lun = CTL_LUN(io); 617 struct ctl_sg_entry *sgl; 618 union ctl_ha_msg msg; 619 uint32_t sg_entries_sent; 620 int do_sg_copy, i, j; 621 622 memset(&msg.dt, 0, sizeof(msg.dt)); 623 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 624 msg.hdr.original_sc = io->io_hdr.original_sc; 625 msg.hdr.serializing_sc = io; 626 msg.hdr.nexus = io->io_hdr.nexus; 627 msg.hdr.status = io->io_hdr.status; 628 msg.dt.flags = io->io_hdr.flags; 629 630 /* 631 * We convert everything into a S/G list here. We can't 632 * pass by reference, only by value between controllers. 633 * So we can't pass a pointer to the S/G list, only as many 634 * S/G entries as we can fit in here. If it's possible for 635 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 636 * then we need to break this up into multiple transfers. 637 */ 638 if (io->scsiio.kern_sg_entries == 0) { 639 msg.dt.kern_sg_entries = 1; 640#if 0 641 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 642 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 643 } else { 644 /* XXX KDM use busdma here! */ 645 msg.dt.sg_list[0].addr = 646 (void *)vtophys(io->scsiio.kern_data_ptr); 647 } 648#else 649 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 650 ("HA does not support BUS_ADDR")); 651 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 652#endif 653 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 654 do_sg_copy = 0; 655 } else { 656 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 657 do_sg_copy = 1; 658 } 659 660 msg.dt.kern_data_len = io->scsiio.kern_data_len; 661 msg.dt.kern_total_len = io->scsiio.kern_total_len; 662 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 663 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 664 msg.dt.sg_sequence = 0; 665 666 /* 667 * Loop until we've sent all of the S/G entries. On the 668 * other end, we'll recompose these S/G entries into one 669 * contiguous list before processing. 670 */ 671 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 672 msg.dt.sg_sequence++) { 673 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 674 sizeof(msg.dt.sg_list[0])), 675 msg.dt.kern_sg_entries - sg_entries_sent); 676 if (do_sg_copy != 0) { 677 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 678 for (i = sg_entries_sent, j = 0; 679 i < msg.dt.cur_sg_entries; i++, j++) { 680#if 0 681 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 682 msg.dt.sg_list[j].addr = sgl[i].addr; 683 } else { 684 /* XXX KDM use busdma here! */ 685 msg.dt.sg_list[j].addr = 686 (void *)vtophys(sgl[i].addr); 687 } 688#else 689 KASSERT((io->io_hdr.flags & 690 CTL_FLAG_BUS_ADDR) == 0, 691 ("HA does not support BUS_ADDR")); 692 msg.dt.sg_list[j].addr = sgl[i].addr; 693#endif 694 msg.dt.sg_list[j].len = sgl[i].len; 695 } 696 } 697 698 sg_entries_sent += msg.dt.cur_sg_entries; 699 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 700 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 701 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 702 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 703 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 704 io->io_hdr.port_status = 31341; 705 io->scsiio.be_move_done(io); 706 return; 707 } 708 msg.dt.sent_sg_entries = sg_entries_sent; 709 } 710 711 /* 712 * Officially handover the request from us to peer. 713 * If failover has just happened, then we must return error. 714 * If failover happen just after, then it is not our problem. 715 */ 716 if (lun) 717 mtx_lock(&lun->lun_lock); 718 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 719 if (lun) 720 mtx_unlock(&lun->lun_lock); 721 io->io_hdr.port_status = 31342; 722 io->scsiio.be_move_done(io); 723 return; 724 } 725 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 726 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 727 if (lun) 728 mtx_unlock(&lun->lun_lock); 729} 730 731static void 732ctl_ha_done(union ctl_io *io) 733{ 734 union ctl_ha_msg msg; 735 736 if (io->io_hdr.io_type == CTL_IO_SCSI) { 737 memset(&msg, 0, sizeof(msg)); 738 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 739 msg.hdr.original_sc = io->io_hdr.original_sc; 740 msg.hdr.nexus = io->io_hdr.nexus; 741 msg.hdr.status = io->io_hdr.status; 742 msg.scsi.scsi_status = io->scsiio.scsi_status; 743 msg.scsi.tag_num = io->scsiio.tag_num; 744 msg.scsi.tag_type = io->scsiio.tag_type; 745 msg.scsi.sense_len = io->scsiio.sense_len; 746 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 747 io->scsiio.sense_len); 748 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 749 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 750 msg.scsi.sense_len, M_WAITOK); 751 } 752 ctl_free_io(io); 753} 754 755static void 756ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 757 union ctl_ha_msg *msg_info) 758{ 759 struct ctl_scsiio *ctsio; 760 761 if (msg_info->hdr.original_sc == NULL) { 762 printf("%s: original_sc == NULL!\n", __func__); 763 /* XXX KDM now what? */ 764 return; 765 } 766 767 ctsio = &msg_info->hdr.original_sc->scsiio; 768 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 769 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 770 ctsio->io_hdr.status = msg_info->hdr.status; 771 ctsio->scsi_status = msg_info->scsi.scsi_status; 772 ctsio->sense_len = msg_info->scsi.sense_len; 773 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 774 msg_info->scsi.sense_len); 775 ctl_enqueue_isc((union ctl_io *)ctsio); 776} 777 778static void 779ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 780 union ctl_ha_msg *msg_info) 781{ 782 struct ctl_scsiio *ctsio; 783 784 if (msg_info->hdr.serializing_sc == NULL) { 785 printf("%s: serializing_sc == NULL!\n", __func__); 786 /* XXX KDM now what? */ 787 return; 788 } 789 790 ctsio = &msg_info->hdr.serializing_sc->scsiio; 791 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 792 ctl_enqueue_isc((union ctl_io *)ctsio); 793} 794 795void 796ctl_isc_announce_lun(struct ctl_lun *lun) 797{ 798 struct ctl_softc *softc = lun->ctl_softc; 799 union ctl_ha_msg *msg; 800 struct ctl_ha_msg_lun_pr_key pr_key; 801 int i, k; 802 803 if (softc->ha_link != CTL_HA_LINK_ONLINE) 804 return; 805 mtx_lock(&lun->lun_lock); 806 i = sizeof(msg->lun); 807 if (lun->lun_devid) 808 i += lun->lun_devid->len; 809 i += sizeof(pr_key) * lun->pr_key_count; 810alloc: 811 mtx_unlock(&lun->lun_lock); 812 msg = malloc(i, M_CTL, M_WAITOK); 813 mtx_lock(&lun->lun_lock); 814 k = sizeof(msg->lun); 815 if (lun->lun_devid) 816 k += lun->lun_devid->len; 817 k += sizeof(pr_key) * lun->pr_key_count; 818 if (i < k) { 819 free(msg, M_CTL); 820 i = k; 821 goto alloc; 822 } 823 bzero(&msg->lun, sizeof(msg->lun)); 824 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 825 msg->hdr.nexus.targ_lun = lun->lun; 826 msg->hdr.nexus.targ_mapped_lun = lun->lun; 827 msg->lun.flags = lun->flags; 828 msg->lun.pr_generation = lun->pr_generation; 829 msg->lun.pr_res_idx = lun->pr_res_idx; 830 msg->lun.pr_res_type = lun->pr_res_type; 831 msg->lun.pr_key_count = lun->pr_key_count; 832 i = 0; 833 if (lun->lun_devid) { 834 msg->lun.lun_devid_len = lun->lun_devid->len; 835 memcpy(&msg->lun.data[i], lun->lun_devid->data, 836 msg->lun.lun_devid_len); 837 i += msg->lun.lun_devid_len; 838 } 839 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 840 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 841 continue; 842 pr_key.pr_iid = k; 843 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 844 i += sizeof(pr_key); 845 } 846 mtx_unlock(&lun->lun_lock); 847 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 848 M_WAITOK); 849 free(msg, M_CTL); 850 851 if (lun->flags & CTL_LUN_PRIMARY_SC) { 852 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 853 ctl_isc_announce_mode(lun, -1, 854 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 855 lun->mode_pages.index[i].subpage); 856 } 857 } 858} 859 860void 861ctl_isc_announce_port(struct ctl_port *port) 862{ 863 struct ctl_softc *softc = port->ctl_softc; 864 union ctl_ha_msg *msg; 865 int i; 866 867 if (port->targ_port < softc->port_min || 868 port->targ_port >= softc->port_max || 869 softc->ha_link != CTL_HA_LINK_ONLINE) 870 return; 871 i = sizeof(msg->port) + strlen(port->port_name) + 1; 872 if (port->lun_map) 873 i += port->lun_map_size * sizeof(uint32_t); 874 if (port->port_devid) 875 i += port->port_devid->len; 876 if (port->target_devid) 877 i += port->target_devid->len; 878 if (port->init_devid) 879 i += port->init_devid->len; 880 msg = malloc(i, M_CTL, M_WAITOK); 881 bzero(&msg->port, sizeof(msg->port)); 882 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 883 msg->hdr.nexus.targ_port = port->targ_port; 884 msg->port.port_type = port->port_type; 885 msg->port.physical_port = port->physical_port; 886 msg->port.virtual_port = port->virtual_port; 887 msg->port.status = port->status; 888 i = 0; 889 msg->port.name_len = sprintf(&msg->port.data[i], 890 "%d:%s", softc->ha_id, port->port_name) + 1; 891 i += msg->port.name_len; 892 if (port->lun_map) { 893 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 894 memcpy(&msg->port.data[i], port->lun_map, 895 msg->port.lun_map_len); 896 i += msg->port.lun_map_len; 897 } 898 if (port->port_devid) { 899 msg->port.port_devid_len = port->port_devid->len; 900 memcpy(&msg->port.data[i], port->port_devid->data, 901 msg->port.port_devid_len); 902 i += msg->port.port_devid_len; 903 } 904 if (port->target_devid) { 905 msg->port.target_devid_len = port->target_devid->len; 906 memcpy(&msg->port.data[i], port->target_devid->data, 907 msg->port.target_devid_len); 908 i += msg->port.target_devid_len; 909 } 910 if (port->init_devid) { 911 msg->port.init_devid_len = port->init_devid->len; 912 memcpy(&msg->port.data[i], port->init_devid->data, 913 msg->port.init_devid_len); 914 i += msg->port.init_devid_len; 915 } 916 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 917 M_WAITOK); 918 free(msg, M_CTL); 919} 920 921void 922ctl_isc_announce_iid(struct ctl_port *port, int iid) 923{ 924 struct ctl_softc *softc = port->ctl_softc; 925 union ctl_ha_msg *msg; 926 int i, l; 927 928 if (port->targ_port < softc->port_min || 929 port->targ_port >= softc->port_max || 930 softc->ha_link != CTL_HA_LINK_ONLINE) 931 return; 932 mtx_lock(&softc->ctl_lock); 933 i = sizeof(msg->iid); 934 l = 0; 935 if (port->wwpn_iid[iid].name) 936 l = strlen(port->wwpn_iid[iid].name) + 1; 937 i += l; 938 msg = malloc(i, M_CTL, M_NOWAIT); 939 if (msg == NULL) { 940 mtx_unlock(&softc->ctl_lock); 941 return; 942 } 943 bzero(&msg->iid, sizeof(msg->iid)); 944 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 945 msg->hdr.nexus.targ_port = port->targ_port; 946 msg->hdr.nexus.initid = iid; 947 msg->iid.in_use = port->wwpn_iid[iid].in_use; 948 msg->iid.name_len = l; 949 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 950 if (port->wwpn_iid[iid].name) 951 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 952 mtx_unlock(&softc->ctl_lock); 953 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 954 free(msg, M_CTL); 955} 956 957void 958ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 959 uint8_t page, uint8_t subpage) 960{ 961 struct ctl_softc *softc = lun->ctl_softc; 962 union ctl_ha_msg msg; 963 u_int i; 964 965 if (softc->ha_link != CTL_HA_LINK_ONLINE) 966 return; 967 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 968 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 969 page && lun->mode_pages.index[i].subpage == subpage) 970 break; 971 } 972 if (i == CTL_NUM_MODE_PAGES) 973 return; 974 975 /* Don't try to replicate pages not present on this device. */ 976 if (lun->mode_pages.index[i].page_data == NULL) 977 return; 978 979 bzero(&msg.mode, sizeof(msg.mode)); 980 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 981 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 982 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 983 msg.hdr.nexus.targ_lun = lun->lun; 984 msg.hdr.nexus.targ_mapped_lun = lun->lun; 985 msg.mode.page_code = page; 986 msg.mode.subpage = subpage; 987 msg.mode.page_len = lun->mode_pages.index[i].page_len; 988 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 989 msg.mode.page_len); 990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 991 M_WAITOK); 992} 993 994static void 995ctl_isc_ha_link_up(struct ctl_softc *softc) 996{ 997 struct ctl_port *port; 998 struct ctl_lun *lun; 999 union ctl_ha_msg msg; 1000 int i; 1001 1002 /* Announce this node parameters to peer for validation. */ 1003 msg.login.msg_type = CTL_MSG_LOGIN; 1004 msg.login.version = CTL_HA_VERSION; 1005 msg.login.ha_mode = softc->ha_mode; 1006 msg.login.ha_id = softc->ha_id; 1007 msg.login.max_luns = CTL_MAX_LUNS; 1008 msg.login.max_ports = CTL_MAX_PORTS; 1009 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 1010 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 1011 M_WAITOK); 1012 1013 STAILQ_FOREACH(port, &softc->port_list, links) { 1014 ctl_isc_announce_port(port); 1015 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1016 if (port->wwpn_iid[i].in_use) 1017 ctl_isc_announce_iid(port, i); 1018 } 1019 } 1020 STAILQ_FOREACH(lun, &softc->lun_list, links) 1021 ctl_isc_announce_lun(lun); 1022} 1023 1024static void 1025ctl_isc_ha_link_down(struct ctl_softc *softc) 1026{ 1027 struct ctl_port *port; 1028 struct ctl_lun *lun; 1029 union ctl_io *io; 1030 int i; 1031 1032 mtx_lock(&softc->ctl_lock); 1033 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1034 mtx_lock(&lun->lun_lock); 1035 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 1036 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1037 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1038 } 1039 mtx_unlock(&lun->lun_lock); 1040 1041 mtx_unlock(&softc->ctl_lock); 1042 io = ctl_alloc_io(softc->othersc_pool); 1043 mtx_lock(&softc->ctl_lock); 1044 ctl_zero_io(io); 1045 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1046 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1047 ctl_enqueue_isc(io); 1048 } 1049 1050 STAILQ_FOREACH(port, &softc->port_list, links) { 1051 if (port->targ_port >= softc->port_min && 1052 port->targ_port < softc->port_max) 1053 continue; 1054 port->status &= ~CTL_PORT_STATUS_ONLINE; 1055 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1056 port->wwpn_iid[i].in_use = 0; 1057 free(port->wwpn_iid[i].name, M_CTL); 1058 port->wwpn_iid[i].name = NULL; 1059 } 1060 } 1061 mtx_unlock(&softc->ctl_lock); 1062} 1063 1064static void 1065ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1066{ 1067 struct ctl_lun *lun; 1068 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1069 1070 mtx_lock(&softc->ctl_lock); 1071 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS || 1072 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1073 mtx_unlock(&softc->ctl_lock); 1074 return; 1075 } 1076 mtx_lock(&lun->lun_lock); 1077 mtx_unlock(&softc->ctl_lock); 1078 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1079 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1080 if (msg->ua.ua_all) { 1081 if (msg->ua.ua_set) 1082 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1083 else 1084 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1085 } else { 1086 if (msg->ua.ua_set) 1087 ctl_est_ua(lun, iid, msg->ua.ua_type); 1088 else 1089 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1090 } 1091 mtx_unlock(&lun->lun_lock); 1092} 1093 1094static void 1095ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1096{ 1097 struct ctl_lun *lun; 1098 struct ctl_ha_msg_lun_pr_key pr_key; 1099 int i, k; 1100 ctl_lun_flags oflags; 1101 uint32_t targ_lun; 1102 1103 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1104 mtx_lock(&softc->ctl_lock); 1105 if (targ_lun >= CTL_MAX_LUNS || 1106 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1107 mtx_unlock(&softc->ctl_lock); 1108 return; 1109 } 1110 mtx_lock(&lun->lun_lock); 1111 mtx_unlock(&softc->ctl_lock); 1112 if (lun->flags & CTL_LUN_DISABLED) { 1113 mtx_unlock(&lun->lun_lock); 1114 return; 1115 } 1116 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1117 if (msg->lun.lun_devid_len != i || (i > 0 && 1118 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1119 mtx_unlock(&lun->lun_lock); 1120 printf("%s: Received conflicting HA LUN %d\n", 1121 __func__, targ_lun); 1122 return; 1123 } else { 1124 /* Record whether peer is primary. */ 1125 oflags = lun->flags; 1126 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1127 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1128 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1129 else 1130 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1131 if (oflags != lun->flags) 1132 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1133 1134 /* If peer is primary and we are not -- use data */ 1135 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1136 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1137 lun->pr_generation = msg->lun.pr_generation; 1138 lun->pr_res_idx = msg->lun.pr_res_idx; 1139 lun->pr_res_type = msg->lun.pr_res_type; 1140 lun->pr_key_count = msg->lun.pr_key_count; 1141 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1142 ctl_clr_prkey(lun, k); 1143 for (k = 0; k < msg->lun.pr_key_count; k++) { 1144 memcpy(&pr_key, &msg->lun.data[i], 1145 sizeof(pr_key)); 1146 ctl_alloc_prkey(lun, pr_key.pr_iid); 1147 ctl_set_prkey(lun, pr_key.pr_iid, 1148 pr_key.pr_key); 1149 i += sizeof(pr_key); 1150 } 1151 } 1152 1153 mtx_unlock(&lun->lun_lock); 1154 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1155 __func__, targ_lun, 1156 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1157 "primary" : "secondary")); 1158 1159 /* If we are primary but peer doesn't know -- notify */ 1160 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1161 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1162 ctl_isc_announce_lun(lun); 1163 } 1164} 1165 1166static void 1167ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1168{ 1169 struct ctl_port *port; 1170 struct ctl_lun *lun; 1171 int i, new; 1172 1173 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1174 if (port == NULL) { 1175 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1176 msg->hdr.nexus.targ_port)); 1177 new = 1; 1178 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1179 port->frontend = &ha_frontend; 1180 port->targ_port = msg->hdr.nexus.targ_port; 1181 port->fe_datamove = ctl_ha_datamove; 1182 port->fe_done = ctl_ha_done; 1183 } else if (port->frontend == &ha_frontend) { 1184 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1185 msg->hdr.nexus.targ_port)); 1186 new = 0; 1187 } else { 1188 printf("%s: Received conflicting HA port %d\n", 1189 __func__, msg->hdr.nexus.targ_port); 1190 return; 1191 } 1192 port->port_type = msg->port.port_type; 1193 port->physical_port = msg->port.physical_port; 1194 port->virtual_port = msg->port.virtual_port; 1195 port->status = msg->port.status; 1196 i = 0; 1197 free(port->port_name, M_CTL); 1198 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1199 M_CTL); 1200 i += msg->port.name_len; 1201 if (msg->port.lun_map_len != 0) { 1202 if (port->lun_map == NULL || 1203 port->lun_map_size * sizeof(uint32_t) < 1204 msg->port.lun_map_len) { 1205 port->lun_map_size = 0; 1206 free(port->lun_map, M_CTL); 1207 port->lun_map = malloc(msg->port.lun_map_len, 1208 M_CTL, M_WAITOK); 1209 } 1210 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1211 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1212 i += msg->port.lun_map_len; 1213 } else { 1214 port->lun_map_size = 0; 1215 free(port->lun_map, M_CTL); 1216 port->lun_map = NULL; 1217 } 1218 if (msg->port.port_devid_len != 0) { 1219 if (port->port_devid == NULL || 1220 port->port_devid->len < msg->port.port_devid_len) { 1221 free(port->port_devid, M_CTL); 1222 port->port_devid = malloc(sizeof(struct ctl_devid) + 1223 msg->port.port_devid_len, M_CTL, M_WAITOK); 1224 } 1225 memcpy(port->port_devid->data, &msg->port.data[i], 1226 msg->port.port_devid_len); 1227 port->port_devid->len = msg->port.port_devid_len; 1228 i += msg->port.port_devid_len; 1229 } else { 1230 free(port->port_devid, M_CTL); 1231 port->port_devid = NULL; 1232 } 1233 if (msg->port.target_devid_len != 0) { 1234 if (port->target_devid == NULL || 1235 port->target_devid->len < msg->port.target_devid_len) { 1236 free(port->target_devid, M_CTL); 1237 port->target_devid = malloc(sizeof(struct ctl_devid) + 1238 msg->port.target_devid_len, M_CTL, M_WAITOK); 1239 } 1240 memcpy(port->target_devid->data, &msg->port.data[i], 1241 msg->port.target_devid_len); 1242 port->target_devid->len = msg->port.target_devid_len; 1243 i += msg->port.target_devid_len; 1244 } else { 1245 free(port->target_devid, M_CTL); 1246 port->target_devid = NULL; 1247 } 1248 if (msg->port.init_devid_len != 0) { 1249 if (port->init_devid == NULL || 1250 port->init_devid->len < msg->port.init_devid_len) { 1251 free(port->init_devid, M_CTL); 1252 port->init_devid = malloc(sizeof(struct ctl_devid) + 1253 msg->port.init_devid_len, M_CTL, M_WAITOK); 1254 } 1255 memcpy(port->init_devid->data, &msg->port.data[i], 1256 msg->port.init_devid_len); 1257 port->init_devid->len = msg->port.init_devid_len; 1258 i += msg->port.init_devid_len; 1259 } else { 1260 free(port->init_devid, M_CTL); 1261 port->init_devid = NULL; 1262 } 1263 if (new) { 1264 if (ctl_port_register(port) != 0) { 1265 printf("%s: ctl_port_register() failed with error\n", 1266 __func__); 1267 } 1268 } 1269 mtx_lock(&softc->ctl_lock); 1270 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1271 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1272 continue; 1273 mtx_lock(&lun->lun_lock); 1274 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1275 mtx_unlock(&lun->lun_lock); 1276 } 1277 mtx_unlock(&softc->ctl_lock); 1278} 1279 1280static void 1281ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1282{ 1283 struct ctl_port *port; 1284 int iid; 1285 1286 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1287 if (port == NULL) { 1288 printf("%s: Received IID for unknown port %d\n", 1289 __func__, msg->hdr.nexus.targ_port); 1290 return; 1291 } 1292 iid = msg->hdr.nexus.initid; 1293 if (port->wwpn_iid[iid].in_use != 0 && 1294 msg->iid.in_use == 0) 1295 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 1296 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1297 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1298 free(port->wwpn_iid[iid].name, M_CTL); 1299 if (msg->iid.name_len) { 1300 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1301 msg->iid.name_len, M_CTL); 1302 } else 1303 port->wwpn_iid[iid].name = NULL; 1304} 1305 1306static void 1307ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1308{ 1309 1310 if (msg->login.version != CTL_HA_VERSION) { 1311 printf("CTL HA peers have different versions %d != %d\n", 1312 msg->login.version, CTL_HA_VERSION); 1313 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1314 return; 1315 } 1316 if (msg->login.ha_mode != softc->ha_mode) { 1317 printf("CTL HA peers have different ha_mode %d != %d\n", 1318 msg->login.ha_mode, softc->ha_mode); 1319 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1320 return; 1321 } 1322 if (msg->login.ha_id == softc->ha_id) { 1323 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1324 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1325 return; 1326 } 1327 if (msg->login.max_luns != CTL_MAX_LUNS || 1328 msg->login.max_ports != CTL_MAX_PORTS || 1329 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1330 printf("CTL HA peers have different limits\n"); 1331 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1332 return; 1333 } 1334} 1335 1336static void 1337ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1338{ 1339 struct ctl_lun *lun; 1340 u_int i; 1341 uint32_t initidx, targ_lun; 1342 1343 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1344 mtx_lock(&softc->ctl_lock); 1345 if (targ_lun >= CTL_MAX_LUNS || 1346 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1347 mtx_unlock(&softc->ctl_lock); 1348 return; 1349 } 1350 mtx_lock(&lun->lun_lock); 1351 mtx_unlock(&softc->ctl_lock); 1352 if (lun->flags & CTL_LUN_DISABLED) { 1353 mtx_unlock(&lun->lun_lock); 1354 return; 1355 } 1356 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1357 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1358 msg->mode.page_code && 1359 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1360 break; 1361 } 1362 if (i == CTL_NUM_MODE_PAGES) { 1363 mtx_unlock(&lun->lun_lock); 1364 return; 1365 } 1366 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1367 lun->mode_pages.index[i].page_len); 1368 initidx = ctl_get_initindex(&msg->hdr.nexus); 1369 if (initidx != -1) 1370 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1371 mtx_unlock(&lun->lun_lock); 1372} 1373 1374/* 1375 * ISC (Inter Shelf Communication) event handler. Events from the HA 1376 * subsystem come in here. 1377 */ 1378static void 1379ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1380{ 1381 struct ctl_softc *softc = control_softc; 1382 union ctl_io *io; 1383 struct ctl_prio *presio; 1384 ctl_ha_status isc_status; 1385 1386 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1387 if (event == CTL_HA_EVT_MSG_RECV) { 1388 union ctl_ha_msg *msg, msgbuf; 1389 1390 if (param > sizeof(msgbuf)) 1391 msg = malloc(param, M_CTL, M_WAITOK); 1392 else 1393 msg = &msgbuf; 1394 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1395 M_WAITOK); 1396 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1397 printf("%s: Error receiving message: %d\n", 1398 __func__, isc_status); 1399 if (msg != &msgbuf) 1400 free(msg, M_CTL); 1401 return; 1402 } 1403 1404 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1405 switch (msg->hdr.msg_type) { 1406 case CTL_MSG_SERIALIZE: 1407 io = ctl_alloc_io(softc->othersc_pool); 1408 ctl_zero_io(io); 1409 // populate ctsio from msg 1410 io->io_hdr.io_type = CTL_IO_SCSI; 1411 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1412 io->io_hdr.original_sc = msg->hdr.original_sc; 1413 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1414 CTL_FLAG_IO_ACTIVE; 1415 /* 1416 * If we're in serialization-only mode, we don't 1417 * want to go through full done processing. Thus 1418 * the COPY flag. 1419 * 1420 * XXX KDM add another flag that is more specific. 1421 */ 1422 if (softc->ha_mode != CTL_HA_MODE_XFER) 1423 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1424 io->io_hdr.nexus = msg->hdr.nexus; 1425#if 0 1426 printf("port %u, iid %u, lun %u\n", 1427 io->io_hdr.nexus.targ_port, 1428 io->io_hdr.nexus.initid, 1429 io->io_hdr.nexus.targ_lun); 1430#endif 1431 io->scsiio.tag_num = msg->scsi.tag_num; 1432 io->scsiio.tag_type = msg->scsi.tag_type; 1433#ifdef CTL_TIME_IO 1434 io->io_hdr.start_time = time_uptime; 1435 getbinuptime(&io->io_hdr.start_bt); 1436#endif /* CTL_TIME_IO */ 1437 io->scsiio.cdb_len = msg->scsi.cdb_len; 1438 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1439 CTL_MAX_CDBLEN); 1440 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1441 const struct ctl_cmd_entry *entry; 1442 1443 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1444 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1445 io->io_hdr.flags |= 1446 entry->flags & CTL_FLAG_DATA_MASK; 1447 } 1448 ctl_enqueue_isc(io); 1449 break; 1450 1451 /* Performed on the Originating SC, XFER mode only */ 1452 case CTL_MSG_DATAMOVE: { 1453 struct ctl_sg_entry *sgl; 1454 int i, j; 1455 1456 io = msg->hdr.original_sc; 1457 if (io == NULL) { 1458 printf("%s: original_sc == NULL!\n", __func__); 1459 /* XXX KDM do something here */ 1460 break; 1461 } 1462 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1463 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1464 /* 1465 * Keep track of this, we need to send it back over 1466 * when the datamove is complete. 1467 */ 1468 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1469 if (msg->hdr.status == CTL_SUCCESS) 1470 io->io_hdr.status = msg->hdr.status; 1471 1472 if (msg->dt.sg_sequence == 0) { 1473#ifdef CTL_TIME_IO 1474 getbinuptime(&io->io_hdr.dma_start_bt); 1475#endif 1476 i = msg->dt.kern_sg_entries + 1477 msg->dt.kern_data_len / 1478 CTL_HA_DATAMOVE_SEGMENT + 1; 1479 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1480 M_WAITOK | M_ZERO); 1481 io->io_hdr.remote_sglist = sgl; 1482 io->io_hdr.local_sglist = 1483 &sgl[msg->dt.kern_sg_entries]; 1484 1485 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1486 1487 io->scsiio.kern_sg_entries = 1488 msg->dt.kern_sg_entries; 1489 io->scsiio.rem_sg_entries = 1490 msg->dt.kern_sg_entries; 1491 io->scsiio.kern_data_len = 1492 msg->dt.kern_data_len; 1493 io->scsiio.kern_total_len = 1494 msg->dt.kern_total_len; 1495 io->scsiio.kern_data_resid = 1496 msg->dt.kern_data_resid; 1497 io->scsiio.kern_rel_offset = 1498 msg->dt.kern_rel_offset; 1499 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1500 io->io_hdr.flags |= msg->dt.flags & 1501 CTL_FLAG_BUS_ADDR; 1502 } else 1503 sgl = (struct ctl_sg_entry *) 1504 io->scsiio.kern_data_ptr; 1505 1506 for (i = msg->dt.sent_sg_entries, j = 0; 1507 i < (msg->dt.sent_sg_entries + 1508 msg->dt.cur_sg_entries); i++, j++) { 1509 sgl[i].addr = msg->dt.sg_list[j].addr; 1510 sgl[i].len = msg->dt.sg_list[j].len; 1511 1512#if 0 1513 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1514 __func__, sgl[i].addr, sgl[i].len, j, i); 1515#endif 1516 } 1517 1518 /* 1519 * If this is the last piece of the I/O, we've got 1520 * the full S/G list. Queue processing in the thread. 1521 * Otherwise wait for the next piece. 1522 */ 1523 if (msg->dt.sg_last != 0) 1524 ctl_enqueue_isc(io); 1525 break; 1526 } 1527 /* Performed on the Serializing (primary) SC, XFER mode only */ 1528 case CTL_MSG_DATAMOVE_DONE: { 1529 if (msg->hdr.serializing_sc == NULL) { 1530 printf("%s: serializing_sc == NULL!\n", 1531 __func__); 1532 /* XXX KDM now what? */ 1533 break; 1534 } 1535 /* 1536 * We grab the sense information here in case 1537 * there was a failure, so we can return status 1538 * back to the initiator. 1539 */ 1540 io = msg->hdr.serializing_sc; 1541 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1542 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1543 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1544 io->io_hdr.port_status = msg->scsi.port_status; 1545 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid; 1546 if (msg->hdr.status != CTL_STATUS_NONE) { 1547 io->io_hdr.status = msg->hdr.status; 1548 io->scsiio.scsi_status = msg->scsi.scsi_status; 1549 io->scsiio.sense_len = msg->scsi.sense_len; 1550 memcpy(&io->scsiio.sense_data, 1551 &msg->scsi.sense_data, 1552 msg->scsi.sense_len); 1553 if (msg->hdr.status == CTL_SUCCESS) 1554 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1555 } 1556 ctl_enqueue_isc(io); 1557 break; 1558 } 1559 1560 /* Preformed on Originating SC, SER_ONLY mode */ 1561 case CTL_MSG_R2R: 1562 io = msg->hdr.original_sc; 1563 if (io == NULL) { 1564 printf("%s: original_sc == NULL!\n", 1565 __func__); 1566 break; 1567 } 1568 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1569 io->io_hdr.msg_type = CTL_MSG_R2R; 1570 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1571 ctl_enqueue_isc(io); 1572 break; 1573 1574 /* 1575 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1576 * mode. 1577 * Performed on the Originating (i.e. secondary) SC in XFER 1578 * mode 1579 */ 1580 case CTL_MSG_FINISH_IO: 1581 if (softc->ha_mode == CTL_HA_MODE_XFER) 1582 ctl_isc_handler_finish_xfer(softc, msg); 1583 else 1584 ctl_isc_handler_finish_ser_only(softc, msg); 1585 break; 1586 1587 /* Preformed on Originating SC */ 1588 case CTL_MSG_BAD_JUJU: 1589 io = msg->hdr.original_sc; 1590 if (io == NULL) { 1591 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1592 __func__); 1593 break; 1594 } 1595 ctl_copy_sense_data(msg, io); 1596 /* 1597 * IO should have already been cleaned up on other 1598 * SC so clear this flag so we won't send a message 1599 * back to finish the IO there. 1600 */ 1601 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1602 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1603 1604 /* io = msg->hdr.serializing_sc; */ 1605 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1606 ctl_enqueue_isc(io); 1607 break; 1608 1609 /* Handle resets sent from the other side */ 1610 case CTL_MSG_MANAGE_TASKS: { 1611 struct ctl_taskio *taskio; 1612 taskio = (struct ctl_taskio *)ctl_alloc_io( 1613 softc->othersc_pool); 1614 ctl_zero_io((union ctl_io *)taskio); 1615 taskio->io_hdr.io_type = CTL_IO_TASK; 1616 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1617 taskio->io_hdr.nexus = msg->hdr.nexus; 1618 taskio->task_action = msg->task.task_action; 1619 taskio->tag_num = msg->task.tag_num; 1620 taskio->tag_type = msg->task.tag_type; 1621#ifdef CTL_TIME_IO 1622 taskio->io_hdr.start_time = time_uptime; 1623 getbinuptime(&taskio->io_hdr.start_bt); 1624#endif /* CTL_TIME_IO */ 1625 ctl_run_task((union ctl_io *)taskio); 1626 break; 1627 } 1628 /* Persistent Reserve action which needs attention */ 1629 case CTL_MSG_PERS_ACTION: 1630 presio = (struct ctl_prio *)ctl_alloc_io( 1631 softc->othersc_pool); 1632 ctl_zero_io((union ctl_io *)presio); 1633 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1634 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1635 presio->io_hdr.nexus = msg->hdr.nexus; 1636 presio->pr_msg = msg->pr; 1637 ctl_enqueue_isc((union ctl_io *)presio); 1638 break; 1639 case CTL_MSG_UA: 1640 ctl_isc_ua(softc, msg, param); 1641 break; 1642 case CTL_MSG_PORT_SYNC: 1643 ctl_isc_port_sync(softc, msg, param); 1644 break; 1645 case CTL_MSG_LUN_SYNC: 1646 ctl_isc_lun_sync(softc, msg, param); 1647 break; 1648 case CTL_MSG_IID_SYNC: 1649 ctl_isc_iid_sync(softc, msg, param); 1650 break; 1651 case CTL_MSG_LOGIN: 1652 ctl_isc_login(softc, msg, param); 1653 break; 1654 case CTL_MSG_MODE_SYNC: 1655 ctl_isc_mode_sync(softc, msg, param); 1656 break; 1657 default: 1658 printf("Received HA message of unknown type %d\n", 1659 msg->hdr.msg_type); 1660 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1661 break; 1662 } 1663 if (msg != &msgbuf) 1664 free(msg, M_CTL); 1665 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1666 printf("CTL: HA link status changed from %d to %d\n", 1667 softc->ha_link, param); 1668 if (param == softc->ha_link) 1669 return; 1670 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1671 softc->ha_link = param; 1672 ctl_isc_ha_link_down(softc); 1673 } else { 1674 softc->ha_link = param; 1675 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1676 ctl_isc_ha_link_up(softc); 1677 } 1678 return; 1679 } else { 1680 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1681 return; 1682 } 1683} 1684 1685static void 1686ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1687{ 1688 1689 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1690 src->scsi.sense_len); 1691 dest->scsiio.scsi_status = src->scsi.scsi_status; 1692 dest->scsiio.sense_len = src->scsi.sense_len; 1693 dest->io_hdr.status = src->hdr.status; 1694} 1695 1696static void 1697ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1698{ 1699 1700 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1701 src->scsiio.sense_len); 1702 dest->scsi.scsi_status = src->scsiio.scsi_status; 1703 dest->scsi.sense_len = src->scsiio.sense_len; 1704 dest->hdr.status = src->io_hdr.status; 1705} 1706 1707void 1708ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1709{ 1710 struct ctl_softc *softc = lun->ctl_softc; 1711 ctl_ua_type *pu; 1712 1713 if (initidx < softc->init_min || initidx >= softc->init_max) 1714 return; 1715 mtx_assert(&lun->lun_lock, MA_OWNED); 1716 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1717 if (pu == NULL) 1718 return; 1719 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1720} 1721 1722void 1723ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1724{ 1725 int i; 1726 1727 mtx_assert(&lun->lun_lock, MA_OWNED); 1728 if (lun->pending_ua[port] == NULL) 1729 return; 1730 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1731 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1732 continue; 1733 lun->pending_ua[port][i] |= ua; 1734 } 1735} 1736 1737void 1738ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1739{ 1740 struct ctl_softc *softc = lun->ctl_softc; 1741 int i; 1742 1743 mtx_assert(&lun->lun_lock, MA_OWNED); 1744 for (i = softc->port_min; i < softc->port_max; i++) 1745 ctl_est_ua_port(lun, i, except, ua); 1746} 1747 1748void 1749ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1750{ 1751 struct ctl_softc *softc = lun->ctl_softc; 1752 ctl_ua_type *pu; 1753 1754 if (initidx < softc->init_min || initidx >= softc->init_max) 1755 return; 1756 mtx_assert(&lun->lun_lock, MA_OWNED); 1757 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1758 if (pu == NULL) 1759 return; 1760 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1761} 1762 1763void 1764ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1765{ 1766 struct ctl_softc *softc = lun->ctl_softc; 1767 int i, j; 1768 1769 mtx_assert(&lun->lun_lock, MA_OWNED); 1770 for (i = softc->port_min; i < softc->port_max; i++) { 1771 if (lun->pending_ua[i] == NULL) 1772 continue; 1773 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1774 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1775 continue; 1776 lun->pending_ua[i][j] &= ~ua; 1777 } 1778 } 1779} 1780 1781void 1782ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1783 ctl_ua_type ua_type) 1784{ 1785 struct ctl_lun *lun; 1786 1787 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1788 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1789 mtx_lock(&lun->lun_lock); 1790 ctl_clr_ua(lun, initidx, ua_type); 1791 mtx_unlock(&lun->lun_lock); 1792 } 1793} 1794 1795static int 1796ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1797{ 1798 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1799 struct ctl_lun *lun; 1800 struct ctl_lun_req ireq; 1801 int error, value; 1802 1803 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1804 error = sysctl_handle_int(oidp, &value, 0, req); 1805 if ((error != 0) || (req->newptr == NULL)) 1806 return (error); 1807 1808 mtx_lock(&softc->ctl_lock); 1809 if (value == 0) 1810 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1811 else 1812 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1813 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1814 mtx_unlock(&softc->ctl_lock); 1815 bzero(&ireq, sizeof(ireq)); 1816 ireq.reqtype = CTL_LUNREQ_MODIFY; 1817 ireq.reqdata.modify.lun_id = lun->lun; 1818 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1819 curthread); 1820 if (ireq.status != CTL_LUN_OK) { 1821 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1822 __func__, ireq.status, ireq.error_str); 1823 } 1824 mtx_lock(&softc->ctl_lock); 1825 } 1826 mtx_unlock(&softc->ctl_lock); 1827 return (0); 1828} 1829 1830static int 1831ctl_init(void) 1832{ 1833 struct make_dev_args args; 1834 struct ctl_softc *softc; 1835 int i, error; 1836 1837 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1838 M_WAITOK | M_ZERO); 1839 1840 make_dev_args_init(&args); 1841 args.mda_devsw = &ctl_cdevsw; 1842 args.mda_uid = UID_ROOT; 1843 args.mda_gid = GID_OPERATOR; 1844 args.mda_mode = 0600; 1845 args.mda_si_drv1 = softc; 1846 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1847 if (error != 0) { 1848 free(softc, M_DEVBUF); 1849 control_softc = NULL; 1850 return (error); 1851 } 1852 1853 sysctl_ctx_init(&softc->sysctl_ctx); 1854 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1855 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1856 CTLFLAG_RD, 0, "CAM Target Layer"); 1857 1858 if (softc->sysctl_tree == NULL) { 1859 printf("%s: unable to allocate sysctl tree\n", __func__); 1860 destroy_dev(softc->dev); 1861 free(softc, M_DEVBUF); 1862 control_softc = NULL; 1863 return (ENOMEM); 1864 } 1865 1866 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1867 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1868 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1869 softc->flags = 0; 1870 1871 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode); 1872 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1873 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1874 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1875 1876 /* 1877 * In Copan's HA scheme, the "master" and "slave" roles are 1878 * figured out through the slot the controller is in. Although it 1879 * is an active/active system, someone has to be in charge. 1880 */ 1881 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id); 1882 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1883 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1884 "HA head ID (0 - no HA)"); 1885 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1886 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1887 softc->is_single = 1; 1888 softc->port_cnt = CTL_MAX_PORTS; 1889 softc->port_min = 0; 1890 } else { 1891 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES; 1892 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1893 } 1894 softc->port_max = softc->port_min + softc->port_cnt; 1895 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1896 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1897 1898 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1899 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1900 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1901 1902 STAILQ_INIT(&softc->lun_list); 1903 STAILQ_INIT(&softc->pending_lun_queue); 1904 STAILQ_INIT(&softc->fe_list); 1905 STAILQ_INIT(&softc->port_list); 1906 STAILQ_INIT(&softc->be_list); 1907 ctl_tpc_init(softc); 1908 1909 if (worker_threads <= 0) 1910 worker_threads = max(1, mp_ncpus / 4); 1911 if (worker_threads > CTL_MAX_THREADS) 1912 worker_threads = CTL_MAX_THREADS; 1913 1914 for (i = 0; i < worker_threads; i++) { 1915 struct ctl_thread *thr = &softc->threads[i]; 1916 1917 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1918 thr->ctl_softc = softc; 1919 STAILQ_INIT(&thr->incoming_queue); 1920 STAILQ_INIT(&thr->rtr_queue); 1921 STAILQ_INIT(&thr->done_queue); 1922 STAILQ_INIT(&thr->isc_queue); 1923 1924 error = kproc_kthread_add(ctl_work_thread, thr, 1925 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1926 if (error != 0) { 1927 printf("error creating CTL work thread!\n"); 1928 return (error); 1929 } 1930 } 1931 error = kproc_kthread_add(ctl_lun_thread, softc, 1932 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun"); 1933 if (error != 0) { 1934 printf("error creating CTL lun thread!\n"); 1935 return (error); 1936 } 1937 error = kproc_kthread_add(ctl_thresh_thread, softc, 1938 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh"); 1939 if (error != 0) { 1940 printf("error creating CTL threshold thread!\n"); 1941 return (error); 1942 } 1943 1944 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1945 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1946 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1947 1948 if (softc->is_single == 0) { 1949 if (ctl_frontend_register(&ha_frontend) != 0) 1950 softc->is_single = 1; 1951 } 1952 return (0); 1953} 1954 1955static int 1956ctl_shutdown(void) 1957{ 1958 struct ctl_softc *softc = control_softc; 1959 int i; 1960 1961 if (softc->is_single == 0) 1962 ctl_frontend_deregister(&ha_frontend); 1963 1964 destroy_dev(softc->dev); 1965 1966 /* Shutdown CTL threads. */ 1967 softc->shutdown = 1; 1968 for (i = 0; i < worker_threads; i++) { 1969 struct ctl_thread *thr = &softc->threads[i]; 1970 while (thr->thread != NULL) { 1971 wakeup(thr); 1972 if (thr->thread != NULL) 1973 pause("CTL thr shutdown", 1); 1974 } 1975 mtx_destroy(&thr->queue_lock); 1976 } 1977 while (softc->lun_thread != NULL) { 1978 wakeup(&softc->pending_lun_queue); 1979 if (softc->lun_thread != NULL) 1980 pause("CTL thr shutdown", 1); 1981 } 1982 while (softc->thresh_thread != NULL) { 1983 wakeup(softc->thresh_thread); 1984 if (softc->thresh_thread != NULL) 1985 pause("CTL thr shutdown", 1); 1986 } 1987 1988 ctl_tpc_shutdown(softc); 1989 uma_zdestroy(softc->io_zone); 1990 mtx_destroy(&softc->ctl_lock); 1991 1992 sysctl_ctx_free(&softc->sysctl_ctx); 1993 1994 free(softc, M_DEVBUF); 1995 control_softc = NULL; 1996 return (0); 1997} 1998 1999static int 2000ctl_module_event_handler(module_t mod, int what, void *arg) 2001{ 2002 2003 switch (what) { 2004 case MOD_LOAD: 2005 return (ctl_init()); 2006 case MOD_UNLOAD: 2007 return (ctl_shutdown()); 2008 default: 2009 return (EOPNOTSUPP); 2010 } 2011} 2012 2013/* 2014 * XXX KDM should we do some access checks here? Bump a reference count to 2015 * prevent a CTL module from being unloaded while someone has it open? 2016 */ 2017static int 2018ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 2019{ 2020 return (0); 2021} 2022 2023static int 2024ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2025{ 2026 return (0); 2027} 2028 2029/* 2030 * Remove an initiator by port number and initiator ID. 2031 * Returns 0 for success, -1 for failure. 2032 */ 2033int 2034ctl_remove_initiator(struct ctl_port *port, int iid) 2035{ 2036 struct ctl_softc *softc = port->ctl_softc; 2037 int last; 2038 2039 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2040 2041 if (iid > CTL_MAX_INIT_PER_PORT) { 2042 printf("%s: initiator ID %u > maximun %u!\n", 2043 __func__, iid, CTL_MAX_INIT_PER_PORT); 2044 return (-1); 2045 } 2046 2047 mtx_lock(&softc->ctl_lock); 2048 last = (--port->wwpn_iid[iid].in_use == 0); 2049 port->wwpn_iid[iid].last_use = time_uptime; 2050 mtx_unlock(&softc->ctl_lock); 2051 if (last) 2052 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON); 2053 ctl_isc_announce_iid(port, iid); 2054 2055 return (0); 2056} 2057 2058/* 2059 * Add an initiator to the initiator map. 2060 * Returns iid for success, < 0 for failure. 2061 */ 2062int 2063ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2064{ 2065 struct ctl_softc *softc = port->ctl_softc; 2066 time_t best_time; 2067 int i, best; 2068 2069 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2070 2071 if (iid >= CTL_MAX_INIT_PER_PORT) { 2072 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2073 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2074 free(name, M_CTL); 2075 return (-1); 2076 } 2077 2078 mtx_lock(&softc->ctl_lock); 2079 2080 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2081 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2082 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2083 iid = i; 2084 break; 2085 } 2086 if (name != NULL && port->wwpn_iid[i].name != NULL && 2087 strcmp(name, port->wwpn_iid[i].name) == 0) { 2088 iid = i; 2089 break; 2090 } 2091 } 2092 } 2093 2094 if (iid < 0) { 2095 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2096 if (port->wwpn_iid[i].in_use == 0 && 2097 port->wwpn_iid[i].wwpn == 0 && 2098 port->wwpn_iid[i].name == NULL) { 2099 iid = i; 2100 break; 2101 } 2102 } 2103 } 2104 2105 if (iid < 0) { 2106 best = -1; 2107 best_time = INT32_MAX; 2108 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2109 if (port->wwpn_iid[i].in_use == 0) { 2110 if (port->wwpn_iid[i].last_use < best_time) { 2111 best = i; 2112 best_time = port->wwpn_iid[i].last_use; 2113 } 2114 } 2115 } 2116 iid = best; 2117 } 2118 2119 if (iid < 0) { 2120 mtx_unlock(&softc->ctl_lock); 2121 free(name, M_CTL); 2122 return (-2); 2123 } 2124 2125 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2126 /* 2127 * This is not an error yet. 2128 */ 2129 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2130#if 0 2131 printf("%s: port %d iid %u WWPN %#jx arrived" 2132 " again\n", __func__, port->targ_port, 2133 iid, (uintmax_t)wwpn); 2134#endif 2135 goto take; 2136 } 2137 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2138 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2139#if 0 2140 printf("%s: port %d iid %u name '%s' arrived" 2141 " again\n", __func__, port->targ_port, 2142 iid, name); 2143#endif 2144 goto take; 2145 } 2146 2147 /* 2148 * This is an error, but what do we do about it? The 2149 * driver is telling us we have a new WWPN for this 2150 * initiator ID, so we pretty much need to use it. 2151 */ 2152 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2153 " but WWPN %#jx '%s' is still at that address\n", 2154 __func__, port->targ_port, iid, wwpn, name, 2155 (uintmax_t)port->wwpn_iid[iid].wwpn, 2156 port->wwpn_iid[iid].name); 2157 } 2158take: 2159 free(port->wwpn_iid[iid].name, M_CTL); 2160 port->wwpn_iid[iid].name = name; 2161 port->wwpn_iid[iid].wwpn = wwpn; 2162 port->wwpn_iid[iid].in_use++; 2163 mtx_unlock(&softc->ctl_lock); 2164 ctl_isc_announce_iid(port, iid); 2165 2166 return (iid); 2167} 2168 2169static int 2170ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2171{ 2172 int len; 2173 2174 switch (port->port_type) { 2175 case CTL_PORT_FC: 2176 { 2177 struct scsi_transportid_fcp *id = 2178 (struct scsi_transportid_fcp *)buf; 2179 if (port->wwpn_iid[iid].wwpn == 0) 2180 return (0); 2181 memset(id, 0, sizeof(*id)); 2182 id->format_protocol = SCSI_PROTO_FC; 2183 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2184 return (sizeof(*id)); 2185 } 2186 case CTL_PORT_ISCSI: 2187 { 2188 struct scsi_transportid_iscsi_port *id = 2189 (struct scsi_transportid_iscsi_port *)buf; 2190 if (port->wwpn_iid[iid].name == NULL) 2191 return (0); 2192 memset(id, 0, 256); 2193 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2194 SCSI_PROTO_ISCSI; 2195 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2196 len = roundup2(min(len, 252), 4); 2197 scsi_ulto2b(len, id->additional_length); 2198 return (sizeof(*id) + len); 2199 } 2200 case CTL_PORT_SAS: 2201 { 2202 struct scsi_transportid_sas *id = 2203 (struct scsi_transportid_sas *)buf; 2204 if (port->wwpn_iid[iid].wwpn == 0) 2205 return (0); 2206 memset(id, 0, sizeof(*id)); 2207 id->format_protocol = SCSI_PROTO_SAS; 2208 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2209 return (sizeof(*id)); 2210 } 2211 default: 2212 { 2213 struct scsi_transportid_spi *id = 2214 (struct scsi_transportid_spi *)buf; 2215 memset(id, 0, sizeof(*id)); 2216 id->format_protocol = SCSI_PROTO_SPI; 2217 scsi_ulto2b(iid, id->scsi_addr); 2218 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2219 return (sizeof(*id)); 2220 } 2221 } 2222} 2223 2224/* 2225 * Serialize a command that went down the "wrong" side, and so was sent to 2226 * this controller for execution. The logic is a little different than the 2227 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2228 * sent back to the other side, but in the success case, we execute the 2229 * command on this side (XFER mode) or tell the other side to execute it 2230 * (SER_ONLY mode). 2231 */ 2232static void 2233ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2234{ 2235 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2236 struct ctl_port *port = CTL_PORT(ctsio); 2237 union ctl_ha_msg msg_info; 2238 struct ctl_lun *lun; 2239 const struct ctl_cmd_entry *entry; 2240 uint32_t targ_lun; 2241 2242 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2243 2244 /* Make sure that we know about this port. */ 2245 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2246 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2247 /*retry_count*/ 1); 2248 goto badjuju; 2249 } 2250 2251 /* Make sure that we know about this LUN. */ 2252 mtx_lock(&softc->ctl_lock); 2253 if (targ_lun >= CTL_MAX_LUNS || 2254 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2255 mtx_unlock(&softc->ctl_lock); 2256 2257 /* 2258 * The other node would not send this request to us unless 2259 * received announce that we are primary node for this LUN. 2260 * If this LUN does not exist now, it is probably result of 2261 * a race, so respond to initiator in the most opaque way. 2262 */ 2263 ctl_set_busy(ctsio); 2264 goto badjuju; 2265 } 2266 mtx_lock(&lun->lun_lock); 2267 mtx_unlock(&softc->ctl_lock); 2268 2269 /* 2270 * If the LUN is invalid, pretend that it doesn't exist. 2271 * It will go away as soon as all pending I/Os completed. 2272 */ 2273 if (lun->flags & CTL_LUN_DISABLED) { 2274 mtx_unlock(&lun->lun_lock); 2275 ctl_set_busy(ctsio); 2276 goto badjuju; 2277 } 2278 2279 entry = ctl_get_cmd_entry(ctsio, NULL); 2280 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2281 mtx_unlock(&lun->lun_lock); 2282 goto badjuju; 2283 } 2284 2285 CTL_LUN(ctsio) = lun; 2286 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2287 2288 /* 2289 * Every I/O goes into the OOA queue for a 2290 * particular LUN, and stays there until completion. 2291 */ 2292#ifdef CTL_TIME_IO 2293 if (TAILQ_EMPTY(&lun->ooa_queue)) 2294 lun->idle_time += getsbinuptime() - lun->last_busy; 2295#endif 2296 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2297 2298 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2299 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2300 ooa_links))) { 2301 case CTL_ACTION_BLOCK: 2302 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2303 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2304 blocked_links); 2305 mtx_unlock(&lun->lun_lock); 2306 break; 2307 case CTL_ACTION_PASS: 2308 case CTL_ACTION_SKIP: 2309 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2310 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2311 ctl_enqueue_rtr((union ctl_io *)ctsio); 2312 mtx_unlock(&lun->lun_lock); 2313 } else { 2314 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2315 mtx_unlock(&lun->lun_lock); 2316 2317 /* send msg back to other side */ 2318 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2319 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2320 msg_info.hdr.msg_type = CTL_MSG_R2R; 2321 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2322 sizeof(msg_info.hdr), M_WAITOK); 2323 } 2324 break; 2325 case CTL_ACTION_OVERLAP: 2326 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2327 mtx_unlock(&lun->lun_lock); 2328 ctl_set_overlapped_cmd(ctsio); 2329 goto badjuju; 2330 case CTL_ACTION_OVERLAP_TAG: 2331 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2332 mtx_unlock(&lun->lun_lock); 2333 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2334 goto badjuju; 2335 case CTL_ACTION_ERROR: 2336 default: 2337 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2338 mtx_unlock(&lun->lun_lock); 2339 2340 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2341 /*retry_count*/ 0); 2342badjuju: 2343 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2344 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2345 msg_info.hdr.serializing_sc = NULL; 2346 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2347 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2348 sizeof(msg_info.scsi), M_WAITOK); 2349 ctl_free_io((union ctl_io *)ctsio); 2350 break; 2351 } 2352} 2353 2354/* 2355 * Returns 0 for success, errno for failure. 2356 */ 2357static void 2358ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2359 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2360{ 2361 union ctl_io *io; 2362 2363 mtx_lock(&lun->lun_lock); 2364 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2365 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2366 ooa_links)) { 2367 struct ctl_ooa_entry *entry; 2368 2369 /* 2370 * If we've got more than we can fit, just count the 2371 * remaining entries. 2372 */ 2373 if (*cur_fill_num >= ooa_hdr->alloc_num) 2374 continue; 2375 2376 entry = &kern_entries[*cur_fill_num]; 2377 2378 entry->tag_num = io->scsiio.tag_num; 2379 entry->lun_num = lun->lun; 2380#ifdef CTL_TIME_IO 2381 entry->start_bt = io->io_hdr.start_bt; 2382#endif 2383 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2384 entry->cdb_len = io->scsiio.cdb_len; 2385 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2386 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2387 2388 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2389 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2390 2391 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2392 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2393 2394 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2395 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2396 2397 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2398 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2399 } 2400 mtx_unlock(&lun->lun_lock); 2401} 2402 2403static void * 2404ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2405 size_t error_str_len) 2406{ 2407 void *kptr; 2408 2409 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2410 2411 if (copyin(user_addr, kptr, len) != 0) { 2412 snprintf(error_str, error_str_len, "Error copying %d bytes " 2413 "from user address %p to kernel address %p", len, 2414 user_addr, kptr); 2415 free(kptr, M_CTL); 2416 return (NULL); 2417 } 2418 2419 return (kptr); 2420} 2421 2422static void 2423ctl_free_args(int num_args, struct ctl_be_arg *args) 2424{ 2425 int i; 2426 2427 if (args == NULL) 2428 return; 2429 2430 for (i = 0; i < num_args; i++) { 2431 free(args[i].kname, M_CTL); 2432 free(args[i].kvalue, M_CTL); 2433 } 2434 2435 free(args, M_CTL); 2436} 2437 2438static struct ctl_be_arg * 2439ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2440 char *error_str, size_t error_str_len) 2441{ 2442 struct ctl_be_arg *args; 2443 int i; 2444 2445 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2446 error_str, error_str_len); 2447 2448 if (args == NULL) 2449 goto bailout; 2450 2451 for (i = 0; i < num_args; i++) { 2452 args[i].kname = NULL; 2453 args[i].kvalue = NULL; 2454 } 2455 2456 for (i = 0; i < num_args; i++) { 2457 uint8_t *tmpptr; 2458 2459 args[i].kname = ctl_copyin_alloc(args[i].name, 2460 args[i].namelen, error_str, error_str_len); 2461 if (args[i].kname == NULL) 2462 goto bailout; 2463 2464 if (args[i].kname[args[i].namelen - 1] != '\0') { 2465 snprintf(error_str, error_str_len, "Argument %d " 2466 "name is not NUL-terminated", i); 2467 goto bailout; 2468 } 2469 2470 if (args[i].flags & CTL_BEARG_RD) { 2471 tmpptr = ctl_copyin_alloc(args[i].value, 2472 args[i].vallen, error_str, error_str_len); 2473 if (tmpptr == NULL) 2474 goto bailout; 2475 if ((args[i].flags & CTL_BEARG_ASCII) 2476 && (tmpptr[args[i].vallen - 1] != '\0')) { 2477 snprintf(error_str, error_str_len, "Argument " 2478 "%d value is not NUL-terminated", i); 2479 free(tmpptr, M_CTL); 2480 goto bailout; 2481 } 2482 args[i].kvalue = tmpptr; 2483 } else { 2484 args[i].kvalue = malloc(args[i].vallen, 2485 M_CTL, M_WAITOK | M_ZERO); 2486 } 2487 } 2488 2489 return (args); 2490bailout: 2491 2492 ctl_free_args(num_args, args); 2493 2494 return (NULL); 2495} 2496 2497static void 2498ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2499{ 2500 int i; 2501 2502 for (i = 0; i < num_args; i++) { 2503 if (args[i].flags & CTL_BEARG_WR) 2504 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2505 } 2506} 2507 2508/* 2509 * Escape characters that are illegal or not recommended in XML. 2510 */ 2511int 2512ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2513{ 2514 char *end = str + size; 2515 int retval; 2516 2517 retval = 0; 2518 2519 for (; *str && str < end; str++) { 2520 switch (*str) { 2521 case '&': 2522 retval = sbuf_printf(sb, "&"); 2523 break; 2524 case '>': 2525 retval = sbuf_printf(sb, ">"); 2526 break; 2527 case '<': 2528 retval = sbuf_printf(sb, "<"); 2529 break; 2530 default: 2531 retval = sbuf_putc(sb, *str); 2532 break; 2533 } 2534 2535 if (retval != 0) 2536 break; 2537 2538 } 2539 2540 return (retval); 2541} 2542 2543static void 2544ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2545{ 2546 struct scsi_vpd_id_descriptor *desc; 2547 int i; 2548 2549 if (id == NULL || id->len < 4) 2550 return; 2551 desc = (struct scsi_vpd_id_descriptor *)id->data; 2552 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2553 case SVPD_ID_TYPE_T10: 2554 sbuf_printf(sb, "t10."); 2555 break; 2556 case SVPD_ID_TYPE_EUI64: 2557 sbuf_printf(sb, "eui."); 2558 break; 2559 case SVPD_ID_TYPE_NAA: 2560 sbuf_printf(sb, "naa."); 2561 break; 2562 case SVPD_ID_TYPE_SCSI_NAME: 2563 break; 2564 } 2565 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2566 case SVPD_ID_CODESET_BINARY: 2567 for (i = 0; i < desc->length; i++) 2568 sbuf_printf(sb, "%02x", desc->identifier[i]); 2569 break; 2570 case SVPD_ID_CODESET_ASCII: 2571 sbuf_printf(sb, "%.*s", (int)desc->length, 2572 (char *)desc->identifier); 2573 break; 2574 case SVPD_ID_CODESET_UTF8: 2575 sbuf_printf(sb, "%s", (char *)desc->identifier); 2576 break; 2577 } 2578} 2579 2580static int 2581ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2582 struct thread *td) 2583{ 2584 struct ctl_softc *softc = dev->si_drv1; 2585 struct ctl_port *port; 2586 struct ctl_lun *lun; 2587 int retval; 2588 2589 retval = 0; 2590 2591 switch (cmd) { 2592 case CTL_IO: 2593 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2594 break; 2595 case CTL_ENABLE_PORT: 2596 case CTL_DISABLE_PORT: 2597 case CTL_SET_PORT_WWNS: { 2598 struct ctl_port *port; 2599 struct ctl_port_entry *entry; 2600 2601 entry = (struct ctl_port_entry *)addr; 2602 2603 mtx_lock(&softc->ctl_lock); 2604 STAILQ_FOREACH(port, &softc->port_list, links) { 2605 int action, done; 2606 2607 if (port->targ_port < softc->port_min || 2608 port->targ_port >= softc->port_max) 2609 continue; 2610 2611 action = 0; 2612 done = 0; 2613 if ((entry->port_type == CTL_PORT_NONE) 2614 && (entry->targ_port == port->targ_port)) { 2615 /* 2616 * If the user only wants to enable or 2617 * disable or set WWNs on a specific port, 2618 * do the operation and we're done. 2619 */ 2620 action = 1; 2621 done = 1; 2622 } else if (entry->port_type & port->port_type) { 2623 /* 2624 * Compare the user's type mask with the 2625 * particular frontend type to see if we 2626 * have a match. 2627 */ 2628 action = 1; 2629 done = 0; 2630 2631 /* 2632 * Make sure the user isn't trying to set 2633 * WWNs on multiple ports at the same time. 2634 */ 2635 if (cmd == CTL_SET_PORT_WWNS) { 2636 printf("%s: Can't set WWNs on " 2637 "multiple ports\n", __func__); 2638 retval = EINVAL; 2639 break; 2640 } 2641 } 2642 if (action == 0) 2643 continue; 2644 2645 /* 2646 * XXX KDM we have to drop the lock here, because 2647 * the online/offline operations can potentially 2648 * block. We need to reference count the frontends 2649 * so they can't go away, 2650 */ 2651 if (cmd == CTL_ENABLE_PORT) { 2652 mtx_unlock(&softc->ctl_lock); 2653 ctl_port_online(port); 2654 mtx_lock(&softc->ctl_lock); 2655 } else if (cmd == CTL_DISABLE_PORT) { 2656 mtx_unlock(&softc->ctl_lock); 2657 ctl_port_offline(port); 2658 mtx_lock(&softc->ctl_lock); 2659 } else if (cmd == CTL_SET_PORT_WWNS) { 2660 ctl_port_set_wwns(port, 2661 (entry->flags & CTL_PORT_WWNN_VALID) ? 2662 1 : 0, entry->wwnn, 2663 (entry->flags & CTL_PORT_WWPN_VALID) ? 2664 1 : 0, entry->wwpn); 2665 } 2666 if (done != 0) 2667 break; 2668 } 2669 mtx_unlock(&softc->ctl_lock); 2670 break; 2671 } 2672 case CTL_GET_OOA: { 2673 struct ctl_ooa *ooa_hdr; 2674 struct ctl_ooa_entry *entries; 2675 uint32_t cur_fill_num; 2676 2677 ooa_hdr = (struct ctl_ooa *)addr; 2678 2679 if ((ooa_hdr->alloc_len == 0) 2680 || (ooa_hdr->alloc_num == 0)) { 2681 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2682 "must be non-zero\n", __func__, 2683 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2684 retval = EINVAL; 2685 break; 2686 } 2687 2688 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2689 sizeof(struct ctl_ooa_entry))) { 2690 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2691 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2692 __func__, ooa_hdr->alloc_len, 2693 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2694 retval = EINVAL; 2695 break; 2696 } 2697 2698 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2699 if (entries == NULL) { 2700 printf("%s: could not allocate %d bytes for OOA " 2701 "dump\n", __func__, ooa_hdr->alloc_len); 2702 retval = ENOMEM; 2703 break; 2704 } 2705 2706 mtx_lock(&softc->ctl_lock); 2707 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2708 (ooa_hdr->lun_num >= CTL_MAX_LUNS || 2709 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2710 mtx_unlock(&softc->ctl_lock); 2711 free(entries, M_CTL); 2712 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2713 __func__, (uintmax_t)ooa_hdr->lun_num); 2714 retval = EINVAL; 2715 break; 2716 } 2717 2718 cur_fill_num = 0; 2719 2720 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2721 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2722 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2723 ooa_hdr, entries); 2724 } 2725 } else { 2726 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2727 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2728 entries); 2729 } 2730 mtx_unlock(&softc->ctl_lock); 2731 2732 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2733 ooa_hdr->fill_len = ooa_hdr->fill_num * 2734 sizeof(struct ctl_ooa_entry); 2735 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2736 if (retval != 0) { 2737 printf("%s: error copying out %d bytes for OOA dump\n", 2738 __func__, ooa_hdr->fill_len); 2739 } 2740 2741 getbinuptime(&ooa_hdr->cur_bt); 2742 2743 if (cur_fill_num > ooa_hdr->alloc_num) { 2744 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2745 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2746 } else { 2747 ooa_hdr->dropped_num = 0; 2748 ooa_hdr->status = CTL_OOA_OK; 2749 } 2750 2751 free(entries, M_CTL); 2752 break; 2753 } 2754 case CTL_DELAY_IO: { 2755 struct ctl_io_delay_info *delay_info; 2756 2757 delay_info = (struct ctl_io_delay_info *)addr; 2758 2759#ifdef CTL_IO_DELAY 2760 mtx_lock(&softc->ctl_lock); 2761 if (delay_info->lun_id >= CTL_MAX_LUNS || 2762 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2763 mtx_unlock(&softc->ctl_lock); 2764 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2765 break; 2766 } 2767 mtx_lock(&lun->lun_lock); 2768 mtx_unlock(&softc->ctl_lock); 2769 delay_info->status = CTL_DELAY_STATUS_OK; 2770 switch (delay_info->delay_type) { 2771 case CTL_DELAY_TYPE_CONT: 2772 case CTL_DELAY_TYPE_ONESHOT: 2773 break; 2774 default: 2775 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2776 break; 2777 } 2778 switch (delay_info->delay_loc) { 2779 case CTL_DELAY_LOC_DATAMOVE: 2780 lun->delay_info.datamove_type = delay_info->delay_type; 2781 lun->delay_info.datamove_delay = delay_info->delay_secs; 2782 break; 2783 case CTL_DELAY_LOC_DONE: 2784 lun->delay_info.done_type = delay_info->delay_type; 2785 lun->delay_info.done_delay = delay_info->delay_secs; 2786 break; 2787 default: 2788 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2789 break; 2790 } 2791 mtx_unlock(&lun->lun_lock); 2792#else 2793 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2794#endif /* CTL_IO_DELAY */ 2795 break; 2796 } 2797#ifdef CTL_LEGACY_STATS 2798 case CTL_GETSTATS: { 2799 struct ctl_stats *stats = (struct ctl_stats *)addr; 2800 int i; 2801 2802 /* 2803 * XXX KDM no locking here. If the LUN list changes, 2804 * things can blow up. 2805 */ 2806 i = 0; 2807 stats->status = CTL_SS_OK; 2808 stats->fill_len = 0; 2809 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2810 if (stats->fill_len + sizeof(lun->legacy_stats) > 2811 stats->alloc_len) { 2812 stats->status = CTL_SS_NEED_MORE_SPACE; 2813 break; 2814 } 2815 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++], 2816 sizeof(lun->legacy_stats)); 2817 if (retval != 0) 2818 break; 2819 stats->fill_len += sizeof(lun->legacy_stats); 2820 } 2821 stats->num_luns = softc->num_luns; 2822 stats->flags = CTL_STATS_FLAG_NONE; 2823#ifdef CTL_TIME_IO 2824 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 2825#endif 2826 getnanouptime(&stats->timestamp); 2827 break; 2828 } 2829#endif /* CTL_LEGACY_STATS */ 2830 case CTL_ERROR_INJECT: { 2831 struct ctl_error_desc *err_desc, *new_err_desc; 2832 2833 err_desc = (struct ctl_error_desc *)addr; 2834 2835 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2836 M_WAITOK | M_ZERO); 2837 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2838 2839 mtx_lock(&softc->ctl_lock); 2840 if (err_desc->lun_id >= CTL_MAX_LUNS || 2841 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2842 mtx_unlock(&softc->ctl_lock); 2843 free(new_err_desc, M_CTL); 2844 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2845 __func__, (uintmax_t)err_desc->lun_id); 2846 retval = EINVAL; 2847 break; 2848 } 2849 mtx_lock(&lun->lun_lock); 2850 mtx_unlock(&softc->ctl_lock); 2851 2852 /* 2853 * We could do some checking here to verify the validity 2854 * of the request, but given the complexity of error 2855 * injection requests, the checking logic would be fairly 2856 * complex. 2857 * 2858 * For now, if the request is invalid, it just won't get 2859 * executed and might get deleted. 2860 */ 2861 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2862 2863 /* 2864 * XXX KDM check to make sure the serial number is unique, 2865 * in case we somehow manage to wrap. That shouldn't 2866 * happen for a very long time, but it's the right thing to 2867 * do. 2868 */ 2869 new_err_desc->serial = lun->error_serial; 2870 err_desc->serial = lun->error_serial; 2871 lun->error_serial++; 2872 2873 mtx_unlock(&lun->lun_lock); 2874 break; 2875 } 2876 case CTL_ERROR_INJECT_DELETE: { 2877 struct ctl_error_desc *delete_desc, *desc, *desc2; 2878 int delete_done; 2879 2880 delete_desc = (struct ctl_error_desc *)addr; 2881 delete_done = 0; 2882 2883 mtx_lock(&softc->ctl_lock); 2884 if (delete_desc->lun_id >= CTL_MAX_LUNS || 2885 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2886 mtx_unlock(&softc->ctl_lock); 2887 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2888 __func__, (uintmax_t)delete_desc->lun_id); 2889 retval = EINVAL; 2890 break; 2891 } 2892 mtx_lock(&lun->lun_lock); 2893 mtx_unlock(&softc->ctl_lock); 2894 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2895 if (desc->serial != delete_desc->serial) 2896 continue; 2897 2898 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2899 links); 2900 free(desc, M_CTL); 2901 delete_done = 1; 2902 } 2903 mtx_unlock(&lun->lun_lock); 2904 if (delete_done == 0) { 2905 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2906 "error serial %ju on LUN %u\n", __func__, 2907 delete_desc->serial, delete_desc->lun_id); 2908 retval = EINVAL; 2909 break; 2910 } 2911 break; 2912 } 2913 case CTL_DUMP_STRUCTS: { 2914 int j, k; 2915 struct ctl_port *port; 2916 struct ctl_frontend *fe; 2917 2918 mtx_lock(&softc->ctl_lock); 2919 printf("CTL Persistent Reservation information start:\n"); 2920 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2921 mtx_lock(&lun->lun_lock); 2922 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2923 mtx_unlock(&lun->lun_lock); 2924 continue; 2925 } 2926 2927 for (j = 0; j < CTL_MAX_PORTS; j++) { 2928 if (lun->pr_keys[j] == NULL) 2929 continue; 2930 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2931 if (lun->pr_keys[j][k] == 0) 2932 continue; 2933 printf(" LUN %ju port %d iid %d key " 2934 "%#jx\n", lun->lun, j, k, 2935 (uintmax_t)lun->pr_keys[j][k]); 2936 } 2937 } 2938 mtx_unlock(&lun->lun_lock); 2939 } 2940 printf("CTL Persistent Reservation information end\n"); 2941 printf("CTL Ports:\n"); 2942 STAILQ_FOREACH(port, &softc->port_list, links) { 2943 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2944 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2945 port->frontend->name, port->port_type, 2946 port->physical_port, port->virtual_port, 2947 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2948 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2949 if (port->wwpn_iid[j].in_use == 0 && 2950 port->wwpn_iid[j].wwpn == 0 && 2951 port->wwpn_iid[j].name == NULL) 2952 continue; 2953 2954 printf(" iid %u use %d WWPN %#jx '%s'\n", 2955 j, port->wwpn_iid[j].in_use, 2956 (uintmax_t)port->wwpn_iid[j].wwpn, 2957 port->wwpn_iid[j].name); 2958 } 2959 } 2960 printf("CTL Port information end\n"); 2961 mtx_unlock(&softc->ctl_lock); 2962 /* 2963 * XXX KDM calling this without a lock. We'd likely want 2964 * to drop the lock before calling the frontend's dump 2965 * routine anyway. 2966 */ 2967 printf("CTL Frontends:\n"); 2968 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2969 printf(" Frontend '%s'\n", fe->name); 2970 if (fe->fe_dump != NULL) 2971 fe->fe_dump(); 2972 } 2973 printf("CTL Frontend information end\n"); 2974 break; 2975 } 2976 case CTL_LUN_REQ: { 2977 struct ctl_lun_req *lun_req; 2978 struct ctl_backend_driver *backend; 2979 2980 lun_req = (struct ctl_lun_req *)addr; 2981 2982 backend = ctl_backend_find(lun_req->backend); 2983 if (backend == NULL) { 2984 lun_req->status = CTL_LUN_ERROR; 2985 snprintf(lun_req->error_str, 2986 sizeof(lun_req->error_str), 2987 "Backend \"%s\" not found.", 2988 lun_req->backend); 2989 break; 2990 } 2991 if (lun_req->num_be_args > 0) { 2992 lun_req->kern_be_args = ctl_copyin_args( 2993 lun_req->num_be_args, 2994 lun_req->be_args, 2995 lun_req->error_str, 2996 sizeof(lun_req->error_str)); 2997 if (lun_req->kern_be_args == NULL) { 2998 lun_req->status = CTL_LUN_ERROR; 2999 break; 3000 } 3001 } 3002 3003 retval = backend->ioctl(dev, cmd, addr, flag, td); 3004 3005 if (lun_req->num_be_args > 0) { 3006 ctl_copyout_args(lun_req->num_be_args, 3007 lun_req->kern_be_args); 3008 ctl_free_args(lun_req->num_be_args, 3009 lun_req->kern_be_args); 3010 } 3011 break; 3012 } 3013 case CTL_LUN_LIST: { 3014 struct sbuf *sb; 3015 struct ctl_lun_list *list; 3016 struct ctl_option *opt; 3017 3018 list = (struct ctl_lun_list *)addr; 3019 3020 /* 3021 * Allocate a fixed length sbuf here, based on the length 3022 * of the user's buffer. We could allocate an auto-extending 3023 * buffer, and then tell the user how much larger our 3024 * amount of data is than his buffer, but that presents 3025 * some problems: 3026 * 3027 * 1. The sbuf(9) routines use a blocking malloc, and so 3028 * we can't hold a lock while calling them with an 3029 * auto-extending buffer. 3030 * 3031 * 2. There is not currently a LUN reference counting 3032 * mechanism, outside of outstanding transactions on 3033 * the LUN's OOA queue. So a LUN could go away on us 3034 * while we're getting the LUN number, backend-specific 3035 * information, etc. Thus, given the way things 3036 * currently work, we need to hold the CTL lock while 3037 * grabbing LUN information. 3038 * 3039 * So, from the user's standpoint, the best thing to do is 3040 * allocate what he thinks is a reasonable buffer length, 3041 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3042 * double the buffer length and try again. (And repeat 3043 * that until he succeeds.) 3044 */ 3045 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3046 if (sb == NULL) { 3047 list->status = CTL_LUN_LIST_ERROR; 3048 snprintf(list->error_str, sizeof(list->error_str), 3049 "Unable to allocate %d bytes for LUN list", 3050 list->alloc_len); 3051 break; 3052 } 3053 3054 sbuf_printf(sb, "<ctllunlist>\n"); 3055 3056 mtx_lock(&softc->ctl_lock); 3057 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3058 mtx_lock(&lun->lun_lock); 3059 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3060 (uintmax_t)lun->lun); 3061 3062 /* 3063 * Bail out as soon as we see that we've overfilled 3064 * the buffer. 3065 */ 3066 if (retval != 0) 3067 break; 3068 3069 retval = sbuf_printf(sb, "\t<backend_type>%s" 3070 "</backend_type>\n", 3071 (lun->backend == NULL) ? "none" : 3072 lun->backend->name); 3073 3074 if (retval != 0) 3075 break; 3076 3077 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3078 lun->be_lun->lun_type); 3079 3080 if (retval != 0) 3081 break; 3082 3083 if (lun->backend == NULL) { 3084 retval = sbuf_printf(sb, "</lun>\n"); 3085 if (retval != 0) 3086 break; 3087 continue; 3088 } 3089 3090 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3091 (lun->be_lun->maxlba > 0) ? 3092 lun->be_lun->maxlba + 1 : 0); 3093 3094 if (retval != 0) 3095 break; 3096 3097 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3098 lun->be_lun->blocksize); 3099 3100 if (retval != 0) 3101 break; 3102 3103 retval = sbuf_printf(sb, "\t<serial_number>"); 3104 3105 if (retval != 0) 3106 break; 3107 3108 retval = ctl_sbuf_printf_esc(sb, 3109 lun->be_lun->serial_num, 3110 sizeof(lun->be_lun->serial_num)); 3111 3112 if (retval != 0) 3113 break; 3114 3115 retval = sbuf_printf(sb, "</serial_number>\n"); 3116 3117 if (retval != 0) 3118 break; 3119 3120 retval = sbuf_printf(sb, "\t<device_id>"); 3121 3122 if (retval != 0) 3123 break; 3124 3125 retval = ctl_sbuf_printf_esc(sb, 3126 lun->be_lun->device_id, 3127 sizeof(lun->be_lun->device_id)); 3128 3129 if (retval != 0) 3130 break; 3131 3132 retval = sbuf_printf(sb, "</device_id>\n"); 3133 3134 if (retval != 0) 3135 break; 3136 3137 if (lun->backend->lun_info != NULL) { 3138 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3139 if (retval != 0) 3140 break; 3141 } 3142 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3143 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3144 opt->name, opt->value, opt->name); 3145 if (retval != 0) 3146 break; 3147 } 3148 3149 retval = sbuf_printf(sb, "</lun>\n"); 3150 3151 if (retval != 0) 3152 break; 3153 mtx_unlock(&lun->lun_lock); 3154 } 3155 if (lun != NULL) 3156 mtx_unlock(&lun->lun_lock); 3157 mtx_unlock(&softc->ctl_lock); 3158 3159 if ((retval != 0) 3160 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3161 retval = 0; 3162 sbuf_delete(sb); 3163 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3164 snprintf(list->error_str, sizeof(list->error_str), 3165 "Out of space, %d bytes is too small", 3166 list->alloc_len); 3167 break; 3168 } 3169 3170 sbuf_finish(sb); 3171 3172 retval = copyout(sbuf_data(sb), list->lun_xml, 3173 sbuf_len(sb) + 1); 3174 3175 list->fill_len = sbuf_len(sb) + 1; 3176 list->status = CTL_LUN_LIST_OK; 3177 sbuf_delete(sb); 3178 break; 3179 } 3180 case CTL_ISCSI: { 3181 struct ctl_iscsi *ci; 3182 struct ctl_frontend *fe; 3183 3184 ci = (struct ctl_iscsi *)addr; 3185 3186 fe = ctl_frontend_find("iscsi"); 3187 if (fe == NULL) { 3188 ci->status = CTL_ISCSI_ERROR; 3189 snprintf(ci->error_str, sizeof(ci->error_str), 3190 "Frontend \"iscsi\" not found."); 3191 break; 3192 } 3193 3194 retval = fe->ioctl(dev, cmd, addr, flag, td); 3195 break; 3196 } 3197 case CTL_PORT_REQ: { 3198 struct ctl_req *req; 3199 struct ctl_frontend *fe; 3200 3201 req = (struct ctl_req *)addr; 3202 3203 fe = ctl_frontend_find(req->driver); 3204 if (fe == NULL) { 3205 req->status = CTL_LUN_ERROR; 3206 snprintf(req->error_str, sizeof(req->error_str), 3207 "Frontend \"%s\" not found.", req->driver); 3208 break; 3209 } 3210 if (req->num_args > 0) { 3211 req->kern_args = ctl_copyin_args(req->num_args, 3212 req->args, req->error_str, sizeof(req->error_str)); 3213 if (req->kern_args == NULL) { 3214 req->status = CTL_LUN_ERROR; 3215 break; 3216 } 3217 } 3218 3219 if (fe->ioctl) 3220 retval = fe->ioctl(dev, cmd, addr, flag, td); 3221 else 3222 retval = ENODEV; 3223 3224 if (req->num_args > 0) { 3225 ctl_copyout_args(req->num_args, req->kern_args); 3226 ctl_free_args(req->num_args, req->kern_args); 3227 } 3228 break; 3229 } 3230 case CTL_PORT_LIST: { 3231 struct sbuf *sb; 3232 struct ctl_port *port; 3233 struct ctl_lun_list *list; 3234 struct ctl_option *opt; 3235 int j; 3236 uint32_t plun; 3237 3238 list = (struct ctl_lun_list *)addr; 3239 3240 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3241 if (sb == NULL) { 3242 list->status = CTL_LUN_LIST_ERROR; 3243 snprintf(list->error_str, sizeof(list->error_str), 3244 "Unable to allocate %d bytes for LUN list", 3245 list->alloc_len); 3246 break; 3247 } 3248 3249 sbuf_printf(sb, "<ctlportlist>\n"); 3250 3251 mtx_lock(&softc->ctl_lock); 3252 STAILQ_FOREACH(port, &softc->port_list, links) { 3253 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3254 (uintmax_t)port->targ_port); 3255 3256 /* 3257 * Bail out as soon as we see that we've overfilled 3258 * the buffer. 3259 */ 3260 if (retval != 0) 3261 break; 3262 3263 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3264 "</frontend_type>\n", port->frontend->name); 3265 if (retval != 0) 3266 break; 3267 3268 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3269 port->port_type); 3270 if (retval != 0) 3271 break; 3272 3273 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3274 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3275 if (retval != 0) 3276 break; 3277 3278 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3279 port->port_name); 3280 if (retval != 0) 3281 break; 3282 3283 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3284 port->physical_port); 3285 if (retval != 0) 3286 break; 3287 3288 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3289 port->virtual_port); 3290 if (retval != 0) 3291 break; 3292 3293 if (port->target_devid != NULL) { 3294 sbuf_printf(sb, "\t<target>"); 3295 ctl_id_sbuf(port->target_devid, sb); 3296 sbuf_printf(sb, "</target>\n"); 3297 } 3298 3299 if (port->port_devid != NULL) { 3300 sbuf_printf(sb, "\t<port>"); 3301 ctl_id_sbuf(port->port_devid, sb); 3302 sbuf_printf(sb, "</port>\n"); 3303 } 3304 3305 if (port->port_info != NULL) { 3306 retval = port->port_info(port->onoff_arg, sb); 3307 if (retval != 0) 3308 break; 3309 } 3310 STAILQ_FOREACH(opt, &port->options, links) { 3311 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3312 opt->name, opt->value, opt->name); 3313 if (retval != 0) 3314 break; 3315 } 3316 3317 if (port->lun_map != NULL) { 3318 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3319 for (j = 0; j < port->lun_map_size; j++) { 3320 plun = ctl_lun_map_from_port(port, j); 3321 if (plun == UINT32_MAX) 3322 continue; 3323 sbuf_printf(sb, 3324 "\t<lun id=\"%u\">%u</lun>\n", 3325 j, plun); 3326 } 3327 } 3328 3329 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3330 if (port->wwpn_iid[j].in_use == 0 || 3331 (port->wwpn_iid[j].wwpn == 0 && 3332 port->wwpn_iid[j].name == NULL)) 3333 continue; 3334 3335 if (port->wwpn_iid[j].name != NULL) 3336 retval = sbuf_printf(sb, 3337 "\t<initiator id=\"%u\">%s</initiator>\n", 3338 j, port->wwpn_iid[j].name); 3339 else 3340 retval = sbuf_printf(sb, 3341 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3342 j, port->wwpn_iid[j].wwpn); 3343 if (retval != 0) 3344 break; 3345 } 3346 if (retval != 0) 3347 break; 3348 3349 retval = sbuf_printf(sb, "</targ_port>\n"); 3350 if (retval != 0) 3351 break; 3352 } 3353 mtx_unlock(&softc->ctl_lock); 3354 3355 if ((retval != 0) 3356 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3357 retval = 0; 3358 sbuf_delete(sb); 3359 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3360 snprintf(list->error_str, sizeof(list->error_str), 3361 "Out of space, %d bytes is too small", 3362 list->alloc_len); 3363 break; 3364 } 3365 3366 sbuf_finish(sb); 3367 3368 retval = copyout(sbuf_data(sb), list->lun_xml, 3369 sbuf_len(sb) + 1); 3370 3371 list->fill_len = sbuf_len(sb) + 1; 3372 list->status = CTL_LUN_LIST_OK; 3373 sbuf_delete(sb); 3374 break; 3375 } 3376 case CTL_LUN_MAP: { 3377 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3378 struct ctl_port *port; 3379 3380 mtx_lock(&softc->ctl_lock); 3381 if (lm->port < softc->port_min || 3382 lm->port >= softc->port_max || 3383 (port = softc->ctl_ports[lm->port]) == NULL) { 3384 mtx_unlock(&softc->ctl_lock); 3385 return (ENXIO); 3386 } 3387 if (port->status & CTL_PORT_STATUS_ONLINE) { 3388 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3389 if (ctl_lun_map_to_port(port, lun->lun) == 3390 UINT32_MAX) 3391 continue; 3392 mtx_lock(&lun->lun_lock); 3393 ctl_est_ua_port(lun, lm->port, -1, 3394 CTL_UA_LUN_CHANGE); 3395 mtx_unlock(&lun->lun_lock); 3396 } 3397 } 3398 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3399 if (lm->plun != UINT32_MAX) { 3400 if (lm->lun == UINT32_MAX) 3401 retval = ctl_lun_map_unset(port, lm->plun); 3402 else if (lm->lun < CTL_MAX_LUNS && 3403 softc->ctl_luns[lm->lun] != NULL) 3404 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3405 else 3406 return (ENXIO); 3407 } else { 3408 if (lm->lun == UINT32_MAX) 3409 retval = ctl_lun_map_deinit(port); 3410 else 3411 retval = ctl_lun_map_init(port); 3412 } 3413 if (port->status & CTL_PORT_STATUS_ONLINE) 3414 ctl_isc_announce_port(port); 3415 break; 3416 } 3417 case CTL_GET_LUN_STATS: { 3418 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3419 int i; 3420 3421 /* 3422 * XXX KDM no locking here. If the LUN list changes, 3423 * things can blow up. 3424 */ 3425 i = 0; 3426 stats->status = CTL_SS_OK; 3427 stats->fill_len = 0; 3428 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3429 if (lun->lun < stats->first_item) 3430 continue; 3431 if (stats->fill_len + sizeof(lun->stats) > 3432 stats->alloc_len) { 3433 stats->status = CTL_SS_NEED_MORE_SPACE; 3434 break; 3435 } 3436 retval = copyout(&lun->stats, &stats->stats[i++], 3437 sizeof(lun->stats)); 3438 if (retval != 0) 3439 break; 3440 stats->fill_len += sizeof(lun->stats); 3441 } 3442 stats->num_items = softc->num_luns; 3443 stats->flags = CTL_STATS_FLAG_NONE; 3444#ifdef CTL_TIME_IO 3445 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3446#endif 3447 getnanouptime(&stats->timestamp); 3448 break; 3449 } 3450 case CTL_GET_PORT_STATS: { 3451 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3452 int i; 3453 3454 /* 3455 * XXX KDM no locking here. If the LUN list changes, 3456 * things can blow up. 3457 */ 3458 i = 0; 3459 stats->status = CTL_SS_OK; 3460 stats->fill_len = 0; 3461 STAILQ_FOREACH(port, &softc->port_list, links) { 3462 if (port->targ_port < stats->first_item) 3463 continue; 3464 if (stats->fill_len + sizeof(port->stats) > 3465 stats->alloc_len) { 3466 stats->status = CTL_SS_NEED_MORE_SPACE; 3467 break; 3468 } 3469 retval = copyout(&port->stats, &stats->stats[i++], 3470 sizeof(port->stats)); 3471 if (retval != 0) 3472 break; 3473 stats->fill_len += sizeof(port->stats); 3474 } 3475 stats->num_items = softc->num_ports; 3476 stats->flags = CTL_STATS_FLAG_NONE; 3477#ifdef CTL_TIME_IO 3478 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3479#endif 3480 getnanouptime(&stats->timestamp); 3481 break; 3482 } 3483 default: { 3484 /* XXX KDM should we fix this? */ 3485#if 0 3486 struct ctl_backend_driver *backend; 3487 unsigned int type; 3488 int found; 3489 3490 found = 0; 3491 3492 /* 3493 * We encode the backend type as the ioctl type for backend 3494 * ioctls. So parse it out here, and then search for a 3495 * backend of this type. 3496 */ 3497 type = _IOC_TYPE(cmd); 3498 3499 STAILQ_FOREACH(backend, &softc->be_list, links) { 3500 if (backend->type == type) { 3501 found = 1; 3502 break; 3503 } 3504 } 3505 if (found == 0) { 3506 printf("ctl: unknown ioctl command %#lx or backend " 3507 "%d\n", cmd, type); 3508 retval = EINVAL; 3509 break; 3510 } 3511 retval = backend->ioctl(dev, cmd, addr, flag, td); 3512#endif 3513 retval = ENOTTY; 3514 break; 3515 } 3516 } 3517 return (retval); 3518} 3519 3520uint32_t 3521ctl_get_initindex(struct ctl_nexus *nexus) 3522{ 3523 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3524} 3525 3526int 3527ctl_lun_map_init(struct ctl_port *port) 3528{ 3529 struct ctl_softc *softc = port->ctl_softc; 3530 struct ctl_lun *lun; 3531 int size = ctl_lun_map_size; 3532 uint32_t i; 3533 3534 if (port->lun_map == NULL || port->lun_map_size < size) { 3535 port->lun_map_size = 0; 3536 free(port->lun_map, M_CTL); 3537 port->lun_map = malloc(size * sizeof(uint32_t), 3538 M_CTL, M_NOWAIT); 3539 } 3540 if (port->lun_map == NULL) 3541 return (ENOMEM); 3542 for (i = 0; i < size; i++) 3543 port->lun_map[i] = UINT32_MAX; 3544 port->lun_map_size = size; 3545 if (port->status & CTL_PORT_STATUS_ONLINE) { 3546 if (port->lun_disable != NULL) { 3547 STAILQ_FOREACH(lun, &softc->lun_list, links) 3548 port->lun_disable(port->targ_lun_arg, lun->lun); 3549 } 3550 ctl_isc_announce_port(port); 3551 } 3552 return (0); 3553} 3554 3555int 3556ctl_lun_map_deinit(struct ctl_port *port) 3557{ 3558 struct ctl_softc *softc = port->ctl_softc; 3559 struct ctl_lun *lun; 3560 3561 if (port->lun_map == NULL) 3562 return (0); 3563 port->lun_map_size = 0; 3564 free(port->lun_map, M_CTL); 3565 port->lun_map = NULL; 3566 if (port->status & CTL_PORT_STATUS_ONLINE) { 3567 if (port->lun_enable != NULL) { 3568 STAILQ_FOREACH(lun, &softc->lun_list, links) 3569 port->lun_enable(port->targ_lun_arg, lun->lun); 3570 } 3571 ctl_isc_announce_port(port); 3572 } 3573 return (0); 3574} 3575 3576int 3577ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3578{ 3579 int status; 3580 uint32_t old; 3581 3582 if (port->lun_map == NULL) { 3583 status = ctl_lun_map_init(port); 3584 if (status != 0) 3585 return (status); 3586 } 3587 if (plun >= port->lun_map_size) 3588 return (EINVAL); 3589 old = port->lun_map[plun]; 3590 port->lun_map[plun] = glun; 3591 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3592 if (port->lun_enable != NULL) 3593 port->lun_enable(port->targ_lun_arg, plun); 3594 ctl_isc_announce_port(port); 3595 } 3596 return (0); 3597} 3598 3599int 3600ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3601{ 3602 uint32_t old; 3603 3604 if (port->lun_map == NULL || plun >= port->lun_map_size) 3605 return (0); 3606 old = port->lun_map[plun]; 3607 port->lun_map[plun] = UINT32_MAX; 3608 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3609 if (port->lun_disable != NULL) 3610 port->lun_disable(port->targ_lun_arg, plun); 3611 ctl_isc_announce_port(port); 3612 } 3613 return (0); 3614} 3615 3616uint32_t 3617ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3618{ 3619 3620 if (port == NULL) 3621 return (UINT32_MAX); 3622 if (port->lun_map == NULL) 3623 return (lun_id); 3624 if (lun_id > port->lun_map_size) 3625 return (UINT32_MAX); 3626 return (port->lun_map[lun_id]); 3627} 3628 3629uint32_t 3630ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3631{ 3632 uint32_t i; 3633 3634 if (port == NULL) 3635 return (UINT32_MAX); 3636 if (port->lun_map == NULL) 3637 return (lun_id); 3638 for (i = 0; i < port->lun_map_size; i++) { 3639 if (port->lun_map[i] == lun_id) 3640 return (i); 3641 } 3642 return (UINT32_MAX); 3643} 3644 3645uint32_t 3646ctl_decode_lun(uint64_t encoded) 3647{ 3648 uint8_t lun[8]; 3649 uint32_t result = 0xffffffff; 3650 3651 be64enc(lun, encoded); 3652 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3653 case RPL_LUNDATA_ATYP_PERIPH: 3654 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3655 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3656 result = lun[1]; 3657 break; 3658 case RPL_LUNDATA_ATYP_FLAT: 3659 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3660 lun[6] == 0 && lun[7] == 0) 3661 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3662 break; 3663 case RPL_LUNDATA_ATYP_EXTLUN: 3664 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3665 case 0x02: 3666 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3667 case 0x00: 3668 result = lun[1]; 3669 break; 3670 case 0x10: 3671 result = (lun[1] << 16) + (lun[2] << 8) + 3672 lun[3]; 3673 break; 3674 case 0x20: 3675 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3676 result = (lun[2] << 24) + 3677 (lun[3] << 16) + (lun[4] << 8) + 3678 lun[5]; 3679 break; 3680 } 3681 break; 3682 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3683 result = 0xffffffff; 3684 break; 3685 } 3686 break; 3687 } 3688 return (result); 3689} 3690 3691uint64_t 3692ctl_encode_lun(uint32_t decoded) 3693{ 3694 uint64_t l = decoded; 3695 3696 if (l <= 0xff) 3697 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3698 if (l <= 0x3fff) 3699 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3700 if (l <= 0xffffff) 3701 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3702 (l << 32)); 3703 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3704} 3705 3706int 3707ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3708{ 3709 int i; 3710 3711 for (i = first; i < last; i++) { 3712 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3713 return (i); 3714 } 3715 return (-1); 3716} 3717 3718int 3719ctl_set_mask(uint32_t *mask, uint32_t bit) 3720{ 3721 uint32_t chunk, piece; 3722 3723 chunk = bit >> 5; 3724 piece = bit % (sizeof(uint32_t) * 8); 3725 3726 if ((mask[chunk] & (1 << piece)) != 0) 3727 return (-1); 3728 else 3729 mask[chunk] |= (1 << piece); 3730 3731 return (0); 3732} 3733 3734int 3735ctl_clear_mask(uint32_t *mask, uint32_t bit) 3736{ 3737 uint32_t chunk, piece; 3738 3739 chunk = bit >> 5; 3740 piece = bit % (sizeof(uint32_t) * 8); 3741 3742 if ((mask[chunk] & (1 << piece)) == 0) 3743 return (-1); 3744 else 3745 mask[chunk] &= ~(1 << piece); 3746 3747 return (0); 3748} 3749 3750int 3751ctl_is_set(uint32_t *mask, uint32_t bit) 3752{ 3753 uint32_t chunk, piece; 3754 3755 chunk = bit >> 5; 3756 piece = bit % (sizeof(uint32_t) * 8); 3757 3758 if ((mask[chunk] & (1 << piece)) == 0) 3759 return (0); 3760 else 3761 return (1); 3762} 3763 3764static uint64_t 3765ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3766{ 3767 uint64_t *t; 3768 3769 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3770 if (t == NULL) 3771 return (0); 3772 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3773} 3774 3775static void 3776ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3777{ 3778 uint64_t *t; 3779 3780 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3781 if (t == NULL) 3782 return; 3783 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3784} 3785 3786static void 3787ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3788{ 3789 uint64_t *p; 3790 u_int i; 3791 3792 i = residx/CTL_MAX_INIT_PER_PORT; 3793 if (lun->pr_keys[i] != NULL) 3794 return; 3795 mtx_unlock(&lun->lun_lock); 3796 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3797 M_WAITOK | M_ZERO); 3798 mtx_lock(&lun->lun_lock); 3799 if (lun->pr_keys[i] == NULL) 3800 lun->pr_keys[i] = p; 3801 else 3802 free(p, M_CTL); 3803} 3804 3805static void 3806ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3807{ 3808 uint64_t *t; 3809 3810 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3811 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3812 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3813} 3814 3815/* 3816 * ctl_softc, pool_name, total_ctl_io are passed in. 3817 * npool is passed out. 3818 */ 3819int 3820ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3821 uint32_t total_ctl_io, void **npool) 3822{ 3823 struct ctl_io_pool *pool; 3824 3825 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3826 M_NOWAIT | M_ZERO); 3827 if (pool == NULL) 3828 return (ENOMEM); 3829 3830 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3831 pool->ctl_softc = ctl_softc; 3832#ifdef IO_POOLS 3833 pool->zone = uma_zsecond_create(pool->name, NULL, 3834 NULL, NULL, NULL, ctl_softc->io_zone); 3835 /* uma_prealloc(pool->zone, total_ctl_io); */ 3836#else 3837 pool->zone = ctl_softc->io_zone; 3838#endif 3839 3840 *npool = pool; 3841 return (0); 3842} 3843 3844void 3845ctl_pool_free(struct ctl_io_pool *pool) 3846{ 3847 3848 if (pool == NULL) 3849 return; 3850 3851#ifdef IO_POOLS 3852 uma_zdestroy(pool->zone); 3853#endif 3854 free(pool, M_CTL); 3855} 3856 3857union ctl_io * 3858ctl_alloc_io(void *pool_ref) 3859{ 3860 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3861 union ctl_io *io; 3862 3863 io = uma_zalloc(pool->zone, M_WAITOK); 3864 if (io != NULL) { 3865 io->io_hdr.pool = pool_ref; 3866 CTL_SOFTC(io) = pool->ctl_softc; 3867 } 3868 return (io); 3869} 3870 3871union ctl_io * 3872ctl_alloc_io_nowait(void *pool_ref) 3873{ 3874 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3875 union ctl_io *io; 3876 3877 io = uma_zalloc(pool->zone, M_NOWAIT); 3878 if (io != NULL) { 3879 io->io_hdr.pool = pool_ref; 3880 CTL_SOFTC(io) = pool->ctl_softc; 3881 } 3882 return (io); 3883} 3884 3885void 3886ctl_free_io(union ctl_io *io) 3887{ 3888 struct ctl_io_pool *pool; 3889 3890 if (io == NULL) 3891 return; 3892 3893 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3894 uma_zfree(pool->zone, io); 3895} 3896 3897void 3898ctl_zero_io(union ctl_io *io) 3899{ 3900 struct ctl_io_pool *pool; 3901 3902 if (io == NULL) 3903 return; 3904 3905 /* 3906 * May need to preserve linked list pointers at some point too. 3907 */ 3908 pool = io->io_hdr.pool; 3909 memset(io, 0, sizeof(*io)); 3910 io->io_hdr.pool = pool; 3911 CTL_SOFTC(io) = pool->ctl_softc; 3912} 3913 3914int 3915ctl_expand_number(const char *buf, uint64_t *num) 3916{ 3917 char *endptr; 3918 uint64_t number; 3919 unsigned shift; 3920 3921 number = strtoq(buf, &endptr, 0); 3922 3923 switch (tolower((unsigned char)*endptr)) { 3924 case 'e': 3925 shift = 60; 3926 break; 3927 case 'p': 3928 shift = 50; 3929 break; 3930 case 't': 3931 shift = 40; 3932 break; 3933 case 'g': 3934 shift = 30; 3935 break; 3936 case 'm': 3937 shift = 20; 3938 break; 3939 case 'k': 3940 shift = 10; 3941 break; 3942 case 'b': 3943 case '\0': /* No unit. */ 3944 *num = number; 3945 return (0); 3946 default: 3947 /* Unrecognized unit. */ 3948 return (-1); 3949 } 3950 3951 if ((number << shift) >> shift != number) { 3952 /* Overflow */ 3953 return (-1); 3954 } 3955 *num = number << shift; 3956 return (0); 3957} 3958 3959 3960/* 3961 * This routine could be used in the future to load default and/or saved 3962 * mode page parameters for a particuar lun. 3963 */ 3964static int 3965ctl_init_page_index(struct ctl_lun *lun) 3966{ 3967 int i, page_code; 3968 struct ctl_page_index *page_index; 3969 const char *value; 3970 uint64_t ival; 3971 3972 memcpy(&lun->mode_pages.index, page_index_template, 3973 sizeof(page_index_template)); 3974 3975 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3976 3977 page_index = &lun->mode_pages.index[i]; 3978 if (lun->be_lun->lun_type == T_DIRECT && 3979 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3980 continue; 3981 if (lun->be_lun->lun_type == T_PROCESSOR && 3982 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3983 continue; 3984 if (lun->be_lun->lun_type == T_CDROM && 3985 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3986 continue; 3987 3988 page_code = page_index->page_code & SMPH_PC_MASK; 3989 switch (page_code) { 3990 case SMS_RW_ERROR_RECOVERY_PAGE: { 3991 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3992 ("subpage %#x for page %#x is incorrect!", 3993 page_index->subpage, page_code)); 3994 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3995 &rw_er_page_default, 3996 sizeof(rw_er_page_default)); 3997 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3998 &rw_er_page_changeable, 3999 sizeof(rw_er_page_changeable)); 4000 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4001 &rw_er_page_default, 4002 sizeof(rw_er_page_default)); 4003 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4004 &rw_er_page_default, 4005 sizeof(rw_er_page_default)); 4006 page_index->page_data = 4007 (uint8_t *)lun->mode_pages.rw_er_page; 4008 break; 4009 } 4010 case SMS_FORMAT_DEVICE_PAGE: { 4011 struct scsi_format_page *format_page; 4012 4013 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4014 ("subpage %#x for page %#x is incorrect!", 4015 page_index->subpage, page_code)); 4016 4017 /* 4018 * Sectors per track are set above. Bytes per 4019 * sector need to be set here on a per-LUN basis. 4020 */ 4021 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4022 &format_page_default, 4023 sizeof(format_page_default)); 4024 memcpy(&lun->mode_pages.format_page[ 4025 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4026 sizeof(format_page_changeable)); 4027 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4028 &format_page_default, 4029 sizeof(format_page_default)); 4030 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4031 &format_page_default, 4032 sizeof(format_page_default)); 4033 4034 format_page = &lun->mode_pages.format_page[ 4035 CTL_PAGE_CURRENT]; 4036 scsi_ulto2b(lun->be_lun->blocksize, 4037 format_page->bytes_per_sector); 4038 4039 format_page = &lun->mode_pages.format_page[ 4040 CTL_PAGE_DEFAULT]; 4041 scsi_ulto2b(lun->be_lun->blocksize, 4042 format_page->bytes_per_sector); 4043 4044 format_page = &lun->mode_pages.format_page[ 4045 CTL_PAGE_SAVED]; 4046 scsi_ulto2b(lun->be_lun->blocksize, 4047 format_page->bytes_per_sector); 4048 4049 page_index->page_data = 4050 (uint8_t *)lun->mode_pages.format_page; 4051 break; 4052 } 4053 case SMS_RIGID_DISK_PAGE: { 4054 struct scsi_rigid_disk_page *rigid_disk_page; 4055 uint32_t sectors_per_cylinder; 4056 uint64_t cylinders; 4057#ifndef __XSCALE__ 4058 int shift; 4059#endif /* !__XSCALE__ */ 4060 4061 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4062 ("subpage %#x for page %#x is incorrect!", 4063 page_index->subpage, page_code)); 4064 4065 /* 4066 * Rotation rate and sectors per track are set 4067 * above. We calculate the cylinders here based on 4068 * capacity. Due to the number of heads and 4069 * sectors per track we're using, smaller arrays 4070 * may turn out to have 0 cylinders. Linux and 4071 * FreeBSD don't pay attention to these mode pages 4072 * to figure out capacity, but Solaris does. It 4073 * seems to deal with 0 cylinders just fine, and 4074 * works out a fake geometry based on the capacity. 4075 */ 4076 memcpy(&lun->mode_pages.rigid_disk_page[ 4077 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4078 sizeof(rigid_disk_page_default)); 4079 memcpy(&lun->mode_pages.rigid_disk_page[ 4080 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4081 sizeof(rigid_disk_page_changeable)); 4082 4083 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4084 CTL_DEFAULT_HEADS; 4085 4086 /* 4087 * The divide method here will be more accurate, 4088 * probably, but results in floating point being 4089 * used in the kernel on i386 (__udivdi3()). On the 4090 * XScale, though, __udivdi3() is implemented in 4091 * software. 4092 * 4093 * The shift method for cylinder calculation is 4094 * accurate if sectors_per_cylinder is a power of 4095 * 2. Otherwise it might be slightly off -- you 4096 * might have a bit of a truncation problem. 4097 */ 4098#ifdef __XSCALE__ 4099 cylinders = (lun->be_lun->maxlba + 1) / 4100 sectors_per_cylinder; 4101#else 4102 for (shift = 31; shift > 0; shift--) { 4103 if (sectors_per_cylinder & (1 << shift)) 4104 break; 4105 } 4106 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4107#endif 4108 4109 /* 4110 * We've basically got 3 bytes, or 24 bits for the 4111 * cylinder size in the mode page. If we're over, 4112 * just round down to 2^24. 4113 */ 4114 if (cylinders > 0xffffff) 4115 cylinders = 0xffffff; 4116 4117 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4118 CTL_PAGE_DEFAULT]; 4119 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4120 4121 if ((value = ctl_get_opt(&lun->be_lun->options, 4122 "rpm")) != NULL) { 4123 scsi_ulto2b(strtol(value, NULL, 0), 4124 rigid_disk_page->rotation_rate); 4125 } 4126 4127 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4128 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4129 sizeof(rigid_disk_page_default)); 4130 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4131 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4132 sizeof(rigid_disk_page_default)); 4133 4134 page_index->page_data = 4135 (uint8_t *)lun->mode_pages.rigid_disk_page; 4136 break; 4137 } 4138 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4139 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4140 ("subpage %#x for page %#x is incorrect!", 4141 page_index->subpage, page_code)); 4142 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4143 &verify_er_page_default, 4144 sizeof(verify_er_page_default)); 4145 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4146 &verify_er_page_changeable, 4147 sizeof(verify_er_page_changeable)); 4148 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4149 &verify_er_page_default, 4150 sizeof(verify_er_page_default)); 4151 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4152 &verify_er_page_default, 4153 sizeof(verify_er_page_default)); 4154 page_index->page_data = 4155 (uint8_t *)lun->mode_pages.verify_er_page; 4156 break; 4157 } 4158 case SMS_CACHING_PAGE: { 4159 struct scsi_caching_page *caching_page; 4160 4161 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4162 ("subpage %#x for page %#x is incorrect!", 4163 page_index->subpage, page_code)); 4164 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4165 &caching_page_default, 4166 sizeof(caching_page_default)); 4167 memcpy(&lun->mode_pages.caching_page[ 4168 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4169 sizeof(caching_page_changeable)); 4170 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4171 &caching_page_default, 4172 sizeof(caching_page_default)); 4173 caching_page = &lun->mode_pages.caching_page[ 4174 CTL_PAGE_SAVED]; 4175 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4176 if (value != NULL && strcmp(value, "off") == 0) 4177 caching_page->flags1 &= ~SCP_WCE; 4178 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4179 if (value != NULL && strcmp(value, "off") == 0) 4180 caching_page->flags1 |= SCP_RCD; 4181 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4182 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4183 sizeof(caching_page_default)); 4184 page_index->page_data = 4185 (uint8_t *)lun->mode_pages.caching_page; 4186 break; 4187 } 4188 case SMS_CONTROL_MODE_PAGE: { 4189 switch (page_index->subpage) { 4190 case SMS_SUBPAGE_PAGE_0: { 4191 struct scsi_control_page *control_page; 4192 4193 memcpy(&lun->mode_pages.control_page[ 4194 CTL_PAGE_DEFAULT], 4195 &control_page_default, 4196 sizeof(control_page_default)); 4197 memcpy(&lun->mode_pages.control_page[ 4198 CTL_PAGE_CHANGEABLE], 4199 &control_page_changeable, 4200 sizeof(control_page_changeable)); 4201 memcpy(&lun->mode_pages.control_page[ 4202 CTL_PAGE_SAVED], 4203 &control_page_default, 4204 sizeof(control_page_default)); 4205 control_page = &lun->mode_pages.control_page[ 4206 CTL_PAGE_SAVED]; 4207 value = ctl_get_opt(&lun->be_lun->options, 4208 "reordering"); 4209 if (value != NULL && 4210 strcmp(value, "unrestricted") == 0) { 4211 control_page->queue_flags &= 4212 ~SCP_QUEUE_ALG_MASK; 4213 control_page->queue_flags |= 4214 SCP_QUEUE_ALG_UNRESTRICTED; 4215 } 4216 memcpy(&lun->mode_pages.control_page[ 4217 CTL_PAGE_CURRENT], 4218 &lun->mode_pages.control_page[ 4219 CTL_PAGE_SAVED], 4220 sizeof(control_page_default)); 4221 page_index->page_data = 4222 (uint8_t *)lun->mode_pages.control_page; 4223 break; 4224 } 4225 case 0x01: 4226 memcpy(&lun->mode_pages.control_ext_page[ 4227 CTL_PAGE_DEFAULT], 4228 &control_ext_page_default, 4229 sizeof(control_ext_page_default)); 4230 memcpy(&lun->mode_pages.control_ext_page[ 4231 CTL_PAGE_CHANGEABLE], 4232 &control_ext_page_changeable, 4233 sizeof(control_ext_page_changeable)); 4234 memcpy(&lun->mode_pages.control_ext_page[ 4235 CTL_PAGE_SAVED], 4236 &control_ext_page_default, 4237 sizeof(control_ext_page_default)); 4238 memcpy(&lun->mode_pages.control_ext_page[ 4239 CTL_PAGE_CURRENT], 4240 &lun->mode_pages.control_ext_page[ 4241 CTL_PAGE_SAVED], 4242 sizeof(control_ext_page_default)); 4243 page_index->page_data = 4244 (uint8_t *)lun->mode_pages.control_ext_page; 4245 break; 4246 default: 4247 panic("subpage %#x for page %#x is incorrect!", 4248 page_index->subpage, page_code); 4249 } 4250 break; 4251 } 4252 case SMS_INFO_EXCEPTIONS_PAGE: { 4253 switch (page_index->subpage) { 4254 case SMS_SUBPAGE_PAGE_0: 4255 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4256 &ie_page_default, 4257 sizeof(ie_page_default)); 4258 memcpy(&lun->mode_pages.ie_page[ 4259 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4260 sizeof(ie_page_changeable)); 4261 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4262 &ie_page_default, 4263 sizeof(ie_page_default)); 4264 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4265 &ie_page_default, 4266 sizeof(ie_page_default)); 4267 page_index->page_data = 4268 (uint8_t *)lun->mode_pages.ie_page; 4269 break; 4270 case 0x02: { 4271 struct ctl_logical_block_provisioning_page *page; 4272 4273 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4274 &lbp_page_default, 4275 sizeof(lbp_page_default)); 4276 memcpy(&lun->mode_pages.lbp_page[ 4277 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4278 sizeof(lbp_page_changeable)); 4279 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4280 &lbp_page_default, 4281 sizeof(lbp_page_default)); 4282 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4283 value = ctl_get_opt(&lun->be_lun->options, 4284 "avail-threshold"); 4285 if (value != NULL && 4286 ctl_expand_number(value, &ival) == 0) { 4287 page->descr[0].flags |= SLBPPD_ENABLED | 4288 SLBPPD_ARMING_DEC; 4289 if (lun->be_lun->blocksize) 4290 ival /= lun->be_lun->blocksize; 4291 else 4292 ival /= 512; 4293 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4294 page->descr[0].count); 4295 } 4296 value = ctl_get_opt(&lun->be_lun->options, 4297 "used-threshold"); 4298 if (value != NULL && 4299 ctl_expand_number(value, &ival) == 0) { 4300 page->descr[1].flags |= SLBPPD_ENABLED | 4301 SLBPPD_ARMING_INC; 4302 if (lun->be_lun->blocksize) 4303 ival /= lun->be_lun->blocksize; 4304 else 4305 ival /= 512; 4306 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4307 page->descr[1].count); 4308 } 4309 value = ctl_get_opt(&lun->be_lun->options, 4310 "pool-avail-threshold"); 4311 if (value != NULL && 4312 ctl_expand_number(value, &ival) == 0) { 4313 page->descr[2].flags |= SLBPPD_ENABLED | 4314 SLBPPD_ARMING_DEC; 4315 if (lun->be_lun->blocksize) 4316 ival /= lun->be_lun->blocksize; 4317 else 4318 ival /= 512; 4319 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4320 page->descr[2].count); 4321 } 4322 value = ctl_get_opt(&lun->be_lun->options, 4323 "pool-used-threshold"); 4324 if (value != NULL && 4325 ctl_expand_number(value, &ival) == 0) { 4326 page->descr[3].flags |= SLBPPD_ENABLED | 4327 SLBPPD_ARMING_INC; 4328 if (lun->be_lun->blocksize) 4329 ival /= lun->be_lun->blocksize; 4330 else 4331 ival /= 512; 4332 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4333 page->descr[3].count); 4334 } 4335 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4336 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4337 sizeof(lbp_page_default)); 4338 page_index->page_data = 4339 (uint8_t *)lun->mode_pages.lbp_page; 4340 break; 4341 } 4342 default: 4343 panic("subpage %#x for page %#x is incorrect!", 4344 page_index->subpage, page_code); 4345 } 4346 break; 4347 } 4348 case SMS_CDDVD_CAPS_PAGE:{ 4349 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4350 ("subpage %#x for page %#x is incorrect!", 4351 page_index->subpage, page_code)); 4352 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4353 &cddvd_page_default, 4354 sizeof(cddvd_page_default)); 4355 memcpy(&lun->mode_pages.cddvd_page[ 4356 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4357 sizeof(cddvd_page_changeable)); 4358 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4359 &cddvd_page_default, 4360 sizeof(cddvd_page_default)); 4361 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4362 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4363 sizeof(cddvd_page_default)); 4364 page_index->page_data = 4365 (uint8_t *)lun->mode_pages.cddvd_page; 4366 break; 4367 } 4368 default: 4369 panic("invalid page code value %#x", page_code); 4370 } 4371 } 4372 4373 return (CTL_RETVAL_COMPLETE); 4374} 4375 4376static int 4377ctl_init_log_page_index(struct ctl_lun *lun) 4378{ 4379 struct ctl_page_index *page_index; 4380 int i, j, k, prev; 4381 4382 memcpy(&lun->log_pages.index, log_page_index_template, 4383 sizeof(log_page_index_template)); 4384 4385 prev = -1; 4386 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4387 4388 page_index = &lun->log_pages.index[i]; 4389 if (lun->be_lun->lun_type == T_DIRECT && 4390 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4391 continue; 4392 if (lun->be_lun->lun_type == T_PROCESSOR && 4393 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4394 continue; 4395 if (lun->be_lun->lun_type == T_CDROM && 4396 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4397 continue; 4398 4399 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4400 lun->backend->lun_attr == NULL) 4401 continue; 4402 4403 if (page_index->page_code != prev) { 4404 lun->log_pages.pages_page[j] = page_index->page_code; 4405 prev = page_index->page_code; 4406 j++; 4407 } 4408 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4409 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4410 k++; 4411 } 4412 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4413 lun->log_pages.index[0].page_len = j; 4414 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4415 lun->log_pages.index[1].page_len = k * 2; 4416 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4417 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4418 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4419 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4420 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4421 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4422 4423 return (CTL_RETVAL_COMPLETE); 4424} 4425 4426static int 4427hex2bin(const char *str, uint8_t *buf, int buf_size) 4428{ 4429 int i; 4430 u_char c; 4431 4432 memset(buf, 0, buf_size); 4433 while (isspace(str[0])) 4434 str++; 4435 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4436 str += 2; 4437 buf_size *= 2; 4438 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4439 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4440 str++; 4441 c = str[i]; 4442 if (isdigit(c)) 4443 c -= '0'; 4444 else if (isalpha(c)) 4445 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4446 else 4447 break; 4448 if (c >= 16) 4449 break; 4450 if ((i & 1) == 0) 4451 buf[i / 2] |= (c << 4); 4452 else 4453 buf[i / 2] |= c; 4454 } 4455 return ((i + 1) / 2); 4456} 4457 4458/* 4459 * LUN allocation. 4460 * 4461 * Requirements: 4462 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4463 * wants us to allocate the LUN and he can block. 4464 * - ctl_softc is always set 4465 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4466 * 4467 * Returns 0 for success, non-zero (errno) for failure. 4468 */ 4469static int 4470ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4471 struct ctl_be_lun *const be_lun) 4472{ 4473 struct ctl_lun *nlun, *lun; 4474 struct scsi_vpd_id_descriptor *desc; 4475 struct scsi_vpd_id_t10 *t10id; 4476 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4477 int lun_number, lun_malloced; 4478 int devidlen, idlen1, idlen2 = 0, len; 4479 4480 if (be_lun == NULL) 4481 return (EINVAL); 4482 4483 /* 4484 * We currently only support Direct Access or Processor LUN types. 4485 */ 4486 switch (be_lun->lun_type) { 4487 case T_DIRECT: 4488 case T_PROCESSOR: 4489 case T_CDROM: 4490 break; 4491 case T_SEQUENTIAL: 4492 case T_CHANGER: 4493 default: 4494 be_lun->lun_config_status(be_lun->be_lun, 4495 CTL_LUN_CONFIG_FAILURE); 4496 break; 4497 } 4498 if (ctl_lun == NULL) { 4499 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4500 lun_malloced = 1; 4501 } else { 4502 lun_malloced = 0; 4503 lun = ctl_lun; 4504 } 4505 4506 memset(lun, 0, sizeof(*lun)); 4507 if (lun_malloced) 4508 lun->flags = CTL_LUN_MALLOCED; 4509 4510 /* Generate LUN ID. */ 4511 devidlen = max(CTL_DEVID_MIN_LEN, 4512 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4513 idlen1 = sizeof(*t10id) + devidlen; 4514 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4515 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4516 if (scsiname != NULL) { 4517 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4518 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4519 } 4520 eui = ctl_get_opt(&be_lun->options, "eui"); 4521 if (eui != NULL) { 4522 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4523 } 4524 naa = ctl_get_opt(&be_lun->options, "naa"); 4525 if (naa != NULL) { 4526 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4527 } 4528 uuid = ctl_get_opt(&be_lun->options, "uuid"); 4529 if (uuid != NULL) { 4530 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4531 } 4532 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4533 M_CTL, M_WAITOK | M_ZERO); 4534 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4535 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4536 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4537 desc->length = idlen1; 4538 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4539 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4540 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4541 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4542 } else { 4543 strncpy(t10id->vendor, vendor, 4544 min(sizeof(t10id->vendor), strlen(vendor))); 4545 } 4546 strncpy((char *)t10id->vendor_spec_id, 4547 (char *)be_lun->device_id, devidlen); 4548 if (scsiname != NULL) { 4549 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4550 desc->length); 4551 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4552 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4553 SVPD_ID_TYPE_SCSI_NAME; 4554 desc->length = idlen2; 4555 strlcpy(desc->identifier, scsiname, idlen2); 4556 } 4557 if (eui != NULL) { 4558 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4559 desc->length); 4560 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4561 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4562 SVPD_ID_TYPE_EUI64; 4563 desc->length = hex2bin(eui, desc->identifier, 16); 4564 desc->length = desc->length > 12 ? 16 : 4565 (desc->length > 8 ? 12 : 8); 4566 len -= 16 - desc->length; 4567 } 4568 if (naa != NULL) { 4569 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4570 desc->length); 4571 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4572 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4573 SVPD_ID_TYPE_NAA; 4574 desc->length = hex2bin(naa, desc->identifier, 16); 4575 desc->length = desc->length > 8 ? 16 : 8; 4576 len -= 16 - desc->length; 4577 } 4578 if (uuid != NULL) { 4579 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4580 desc->length); 4581 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4582 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4583 SVPD_ID_TYPE_UUID; 4584 desc->identifier[0] = 0x10; 4585 hex2bin(uuid, &desc->identifier[2], 16); 4586 desc->length = 18; 4587 } 4588 lun->lun_devid->len = len; 4589 4590 mtx_lock(&ctl_softc->ctl_lock); 4591 /* 4592 * See if the caller requested a particular LUN number. If so, see 4593 * if it is available. Otherwise, allocate the first available LUN. 4594 */ 4595 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4596 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4597 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4598 mtx_unlock(&ctl_softc->ctl_lock); 4599 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4600 printf("ctl: requested LUN ID %d is higher " 4601 "than CTL_MAX_LUNS - 1 (%d)\n", 4602 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4603 } else { 4604 /* 4605 * XXX KDM return an error, or just assign 4606 * another LUN ID in this case?? 4607 */ 4608 printf("ctl: requested LUN ID %d is already " 4609 "in use\n", be_lun->req_lun_id); 4610 } 4611fail: 4612 free(lun->lun_devid, M_CTL); 4613 if (lun->flags & CTL_LUN_MALLOCED) 4614 free(lun, M_CTL); 4615 be_lun->lun_config_status(be_lun->be_lun, 4616 CTL_LUN_CONFIG_FAILURE); 4617 return (ENOSPC); 4618 } 4619 lun_number = be_lun->req_lun_id; 4620 } else { 4621 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4622 if (lun_number == -1) { 4623 mtx_unlock(&ctl_softc->ctl_lock); 4624 printf("ctl: can't allocate LUN, out of LUNs\n"); 4625 goto fail; 4626 } 4627 } 4628 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4629 mtx_unlock(&ctl_softc->ctl_lock); 4630 4631 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4632 lun->lun = lun_number; 4633 lun->be_lun = be_lun; 4634 /* 4635 * The processor LUN is always enabled. Disk LUNs come on line 4636 * disabled, and must be enabled by the backend. 4637 */ 4638 lun->flags |= CTL_LUN_DISABLED; 4639 lun->backend = be_lun->be; 4640 be_lun->ctl_lun = lun; 4641 be_lun->lun_id = lun_number; 4642 atomic_add_int(&be_lun->be->num_luns, 1); 4643 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4644 lun->flags |= CTL_LUN_EJECTED; 4645 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4646 lun->flags |= CTL_LUN_NO_MEDIA; 4647 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4648 lun->flags |= CTL_LUN_STOPPED; 4649 4650 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4651 lun->flags |= CTL_LUN_PRIMARY_SC; 4652 4653 value = ctl_get_opt(&be_lun->options, "removable"); 4654 if (value != NULL) { 4655 if (strcmp(value, "on") == 0) 4656 lun->flags |= CTL_LUN_REMOVABLE; 4657 } else if (be_lun->lun_type == T_CDROM) 4658 lun->flags |= CTL_LUN_REMOVABLE; 4659 4660 lun->ctl_softc = ctl_softc; 4661#ifdef CTL_TIME_IO 4662 lun->last_busy = getsbinuptime(); 4663#endif 4664 TAILQ_INIT(&lun->ooa_queue); 4665 TAILQ_INIT(&lun->blocked_queue); 4666 STAILQ_INIT(&lun->error_list); 4667 lun->ie_reported = 1; 4668 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4669 ctl_tpc_lun_init(lun); 4670 if (lun->flags & CTL_LUN_REMOVABLE) { 4671 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4672 M_CTL, M_WAITOK); 4673 } 4674 4675 /* 4676 * Initialize the mode and log page index. 4677 */ 4678 ctl_init_page_index(lun); 4679 ctl_init_log_page_index(lun); 4680 4681 /* Setup statistics gathering */ 4682#ifdef CTL_LEGACY_STATS 4683 lun->legacy_stats.device_type = be_lun->lun_type; 4684 lun->legacy_stats.lun_number = lun_number; 4685 lun->legacy_stats.blocksize = be_lun->blocksize; 4686 if (be_lun->blocksize == 0) 4687 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4688 for (len = 0; len < CTL_MAX_PORTS; len++) 4689 lun->legacy_stats.ports[len].targ_port = len; 4690#endif /* CTL_LEGACY_STATS */ 4691 lun->stats.item = lun_number; 4692 4693 /* 4694 * Now, before we insert this lun on the lun list, set the lun 4695 * inventory changed UA for all other luns. 4696 */ 4697 mtx_lock(&ctl_softc->ctl_lock); 4698 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4699 mtx_lock(&nlun->lun_lock); 4700 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4701 mtx_unlock(&nlun->lun_lock); 4702 } 4703 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4704 ctl_softc->ctl_luns[lun_number] = lun; 4705 ctl_softc->num_luns++; 4706 mtx_unlock(&ctl_softc->ctl_lock); 4707 4708 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4709 return (0); 4710} 4711 4712/* 4713 * Delete a LUN. 4714 * Assumptions: 4715 * - LUN has already been marked invalid and any pending I/O has been taken 4716 * care of. 4717 */ 4718static int 4719ctl_free_lun(struct ctl_lun *lun) 4720{ 4721 struct ctl_softc *softc = lun->ctl_softc; 4722 struct ctl_lun *nlun; 4723 int i; 4724 4725 mtx_assert(&softc->ctl_lock, MA_OWNED); 4726 4727 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4728 4729 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4730 4731 softc->ctl_luns[lun->lun] = NULL; 4732 4733 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4734 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4735 4736 softc->num_luns--; 4737 4738 /* 4739 * Tell the backend to free resources, if this LUN has a backend. 4740 */ 4741 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4742 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4743 4744 lun->ie_reportcnt = UINT32_MAX; 4745 callout_drain(&lun->ie_callout); 4746 4747 ctl_tpc_lun_shutdown(lun); 4748 mtx_destroy(&lun->lun_lock); 4749 free(lun->lun_devid, M_CTL); 4750 for (i = 0; i < CTL_MAX_PORTS; i++) 4751 free(lun->pending_ua[i], M_CTL); 4752 for (i = 0; i < CTL_MAX_PORTS; i++) 4753 free(lun->pr_keys[i], M_CTL); 4754 free(lun->write_buffer, M_CTL); 4755 free(lun->prevent, M_CTL); 4756 if (lun->flags & CTL_LUN_MALLOCED) 4757 free(lun, M_CTL); 4758 4759 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4760 mtx_lock(&nlun->lun_lock); 4761 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4762 mtx_unlock(&nlun->lun_lock); 4763 } 4764 4765 return (0); 4766} 4767 4768static void 4769ctl_create_lun(struct ctl_be_lun *be_lun) 4770{ 4771 4772 /* 4773 * ctl_alloc_lun() should handle all potential failure cases. 4774 */ 4775 ctl_alloc_lun(control_softc, NULL, be_lun); 4776} 4777 4778int 4779ctl_add_lun(struct ctl_be_lun *be_lun) 4780{ 4781 struct ctl_softc *softc = control_softc; 4782 4783 mtx_lock(&softc->ctl_lock); 4784 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4785 mtx_unlock(&softc->ctl_lock); 4786 wakeup(&softc->pending_lun_queue); 4787 4788 return (0); 4789} 4790 4791int 4792ctl_enable_lun(struct ctl_be_lun *be_lun) 4793{ 4794 struct ctl_softc *softc; 4795 struct ctl_port *port, *nport; 4796 struct ctl_lun *lun; 4797 int retval; 4798 4799 lun = (struct ctl_lun *)be_lun->ctl_lun; 4800 softc = lun->ctl_softc; 4801 4802 mtx_lock(&softc->ctl_lock); 4803 mtx_lock(&lun->lun_lock); 4804 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4805 /* 4806 * eh? Why did we get called if the LUN is already 4807 * enabled? 4808 */ 4809 mtx_unlock(&lun->lun_lock); 4810 mtx_unlock(&softc->ctl_lock); 4811 return (0); 4812 } 4813 lun->flags &= ~CTL_LUN_DISABLED; 4814 mtx_unlock(&lun->lun_lock); 4815 4816 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4817 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4818 port->lun_map != NULL || port->lun_enable == NULL) 4819 continue; 4820 4821 /* 4822 * Drop the lock while we call the FETD's enable routine. 4823 * This can lead to a callback into CTL (at least in the 4824 * case of the internal initiator frontend. 4825 */ 4826 mtx_unlock(&softc->ctl_lock); 4827 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4828 mtx_lock(&softc->ctl_lock); 4829 if (retval != 0) { 4830 printf("%s: FETD %s port %d returned error " 4831 "%d for lun_enable on lun %jd\n", 4832 __func__, port->port_name, port->targ_port, 4833 retval, (intmax_t)lun->lun); 4834 } 4835 } 4836 4837 mtx_unlock(&softc->ctl_lock); 4838 ctl_isc_announce_lun(lun); 4839 4840 return (0); 4841} 4842 4843int 4844ctl_disable_lun(struct ctl_be_lun *be_lun) 4845{ 4846 struct ctl_softc *softc; 4847 struct ctl_port *port; 4848 struct ctl_lun *lun; 4849 int retval; 4850 4851 lun = (struct ctl_lun *)be_lun->ctl_lun; 4852 softc = lun->ctl_softc; 4853 4854 mtx_lock(&softc->ctl_lock); 4855 mtx_lock(&lun->lun_lock); 4856 if (lun->flags & CTL_LUN_DISABLED) { 4857 mtx_unlock(&lun->lun_lock); 4858 mtx_unlock(&softc->ctl_lock); 4859 return (0); 4860 } 4861 lun->flags |= CTL_LUN_DISABLED; 4862 mtx_unlock(&lun->lun_lock); 4863 4864 STAILQ_FOREACH(port, &softc->port_list, links) { 4865 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4866 port->lun_map != NULL || port->lun_disable == NULL) 4867 continue; 4868 4869 /* 4870 * Drop the lock before we call the frontend's disable 4871 * routine, to avoid lock order reversals. 4872 * 4873 * XXX KDM what happens if the frontend list changes while 4874 * we're traversing it? It's unlikely, but should be handled. 4875 */ 4876 mtx_unlock(&softc->ctl_lock); 4877 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4878 mtx_lock(&softc->ctl_lock); 4879 if (retval != 0) { 4880 printf("%s: FETD %s port %d returned error " 4881 "%d for lun_disable on lun %jd\n", 4882 __func__, port->port_name, port->targ_port, 4883 retval, (intmax_t)lun->lun); 4884 } 4885 } 4886 4887 mtx_unlock(&softc->ctl_lock); 4888 ctl_isc_announce_lun(lun); 4889 4890 return (0); 4891} 4892 4893int 4894ctl_start_lun(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_STOPPED; 4900 mtx_unlock(&lun->lun_lock); 4901 return (0); 4902} 4903 4904int 4905ctl_stop_lun(struct ctl_be_lun *be_lun) 4906{ 4907 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4908 4909 mtx_lock(&lun->lun_lock); 4910 lun->flags |= CTL_LUN_STOPPED; 4911 mtx_unlock(&lun->lun_lock); 4912 return (0); 4913} 4914 4915int 4916ctl_lun_no_media(struct ctl_be_lun *be_lun) 4917{ 4918 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4919 4920 mtx_lock(&lun->lun_lock); 4921 lun->flags |= CTL_LUN_NO_MEDIA; 4922 mtx_unlock(&lun->lun_lock); 4923 return (0); 4924} 4925 4926int 4927ctl_lun_has_media(struct ctl_be_lun *be_lun) 4928{ 4929 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4930 union ctl_ha_msg msg; 4931 4932 mtx_lock(&lun->lun_lock); 4933 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4934 if (lun->flags & CTL_LUN_REMOVABLE) 4935 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4936 mtx_unlock(&lun->lun_lock); 4937 if ((lun->flags & CTL_LUN_REMOVABLE) && 4938 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4939 bzero(&msg.ua, sizeof(msg.ua)); 4940 msg.hdr.msg_type = CTL_MSG_UA; 4941 msg.hdr.nexus.initid = -1; 4942 msg.hdr.nexus.targ_port = -1; 4943 msg.hdr.nexus.targ_lun = lun->lun; 4944 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4945 msg.ua.ua_all = 1; 4946 msg.ua.ua_set = 1; 4947 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4948 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4949 M_WAITOK); 4950 } 4951 return (0); 4952} 4953 4954int 4955ctl_lun_ejected(struct ctl_be_lun *be_lun) 4956{ 4957 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4958 4959 mtx_lock(&lun->lun_lock); 4960 lun->flags |= CTL_LUN_EJECTED; 4961 mtx_unlock(&lun->lun_lock); 4962 return (0); 4963} 4964 4965int 4966ctl_lun_primary(struct ctl_be_lun *be_lun) 4967{ 4968 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4969 4970 mtx_lock(&lun->lun_lock); 4971 lun->flags |= CTL_LUN_PRIMARY_SC; 4972 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4973 mtx_unlock(&lun->lun_lock); 4974 ctl_isc_announce_lun(lun); 4975 return (0); 4976} 4977 4978int 4979ctl_lun_secondary(struct ctl_be_lun *be_lun) 4980{ 4981 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4982 4983 mtx_lock(&lun->lun_lock); 4984 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4985 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4986 mtx_unlock(&lun->lun_lock); 4987 ctl_isc_announce_lun(lun); 4988 return (0); 4989} 4990 4991int 4992ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4993{ 4994 struct ctl_softc *softc; 4995 struct ctl_lun *lun; 4996 4997 lun = (struct ctl_lun *)be_lun->ctl_lun; 4998 softc = lun->ctl_softc; 4999 5000 mtx_lock(&lun->lun_lock); 5001 5002 /* 5003 * The LUN needs to be disabled before it can be marked invalid. 5004 */ 5005 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 5006 mtx_unlock(&lun->lun_lock); 5007 return (-1); 5008 } 5009 /* 5010 * Mark the LUN invalid. 5011 */ 5012 lun->flags |= CTL_LUN_INVALID; 5013 5014 /* 5015 * If there is nothing in the OOA queue, go ahead and free the LUN. 5016 * If we have something in the OOA queue, we'll free it when the 5017 * last I/O completes. 5018 */ 5019 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5020 mtx_unlock(&lun->lun_lock); 5021 mtx_lock(&softc->ctl_lock); 5022 ctl_free_lun(lun); 5023 mtx_unlock(&softc->ctl_lock); 5024 } else 5025 mtx_unlock(&lun->lun_lock); 5026 5027 return (0); 5028} 5029 5030void 5031ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5032{ 5033 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5034 union ctl_ha_msg msg; 5035 5036 mtx_lock(&lun->lun_lock); 5037 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5038 mtx_unlock(&lun->lun_lock); 5039 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5040 /* Send msg to other side. */ 5041 bzero(&msg.ua, sizeof(msg.ua)); 5042 msg.hdr.msg_type = CTL_MSG_UA; 5043 msg.hdr.nexus.initid = -1; 5044 msg.hdr.nexus.targ_port = -1; 5045 msg.hdr.nexus.targ_lun = lun->lun; 5046 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5047 msg.ua.ua_all = 1; 5048 msg.ua.ua_set = 1; 5049 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5050 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5051 M_WAITOK); 5052 } 5053} 5054 5055/* 5056 * Backend "memory move is complete" callback for requests that never 5057 * make it down to say RAIDCore's configuration code. 5058 */ 5059int 5060ctl_config_move_done(union ctl_io *io) 5061{ 5062 int retval; 5063 5064 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5065 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5066 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5067 5068 if ((io->io_hdr.port_status != 0) && 5069 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5070 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5071 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 5072 /*retry_count*/ io->io_hdr.port_status); 5073 } else if (io->scsiio.kern_data_resid != 0 && 5074 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 5075 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5076 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5077 ctl_set_invalid_field_ciu(&io->scsiio); 5078 } 5079 5080 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5081 ctl_data_print(io); 5082 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5083 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5084 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5085 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5086 /* 5087 * XXX KDM just assuming a single pointer here, and not a 5088 * S/G list. If we start using S/G lists for config data, 5089 * we'll need to know how to clean them up here as well. 5090 */ 5091 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5092 free(io->scsiio.kern_data_ptr, M_CTL); 5093 ctl_done(io); 5094 retval = CTL_RETVAL_COMPLETE; 5095 } else { 5096 /* 5097 * XXX KDM now we need to continue data movement. Some 5098 * options: 5099 * - call ctl_scsiio() again? We don't do this for data 5100 * writes, because for those at least we know ahead of 5101 * time where the write will go and how long it is. For 5102 * config writes, though, that information is largely 5103 * contained within the write itself, thus we need to 5104 * parse out the data again. 5105 * 5106 * - Call some other function once the data is in? 5107 */ 5108 5109 /* 5110 * XXX KDM call ctl_scsiio() again for now, and check flag 5111 * bits to see whether we're allocated or not. 5112 */ 5113 retval = ctl_scsiio(&io->scsiio); 5114 } 5115 return (retval); 5116} 5117 5118/* 5119 * This gets called by a backend driver when it is done with a 5120 * data_submit method. 5121 */ 5122void 5123ctl_data_submit_done(union ctl_io *io) 5124{ 5125 /* 5126 * If the IO_CONT flag is set, we need to call the supplied 5127 * function to continue processing the I/O, instead of completing 5128 * the I/O just yet. 5129 * 5130 * If there is an error, though, we don't want to keep processing. 5131 * Instead, just send status back to the initiator. 5132 */ 5133 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5134 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5135 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5136 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5137 io->scsiio.io_cont(io); 5138 return; 5139 } 5140 ctl_done(io); 5141} 5142 5143/* 5144 * This gets called by a backend driver when it is done with a 5145 * configuration write. 5146 */ 5147void 5148ctl_config_write_done(union ctl_io *io) 5149{ 5150 uint8_t *buf; 5151 5152 /* 5153 * If the IO_CONT flag is set, we need to call the supplied 5154 * function to continue processing the I/O, instead of completing 5155 * the I/O just yet. 5156 * 5157 * If there is an error, though, we don't want to keep processing. 5158 * Instead, just send status back to the initiator. 5159 */ 5160 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5161 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5162 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5163 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5164 io->scsiio.io_cont(io); 5165 return; 5166 } 5167 /* 5168 * Since a configuration write can be done for commands that actually 5169 * have data allocated, like write buffer, and commands that have 5170 * no data, like start/stop unit, we need to check here. 5171 */ 5172 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5173 buf = io->scsiio.kern_data_ptr; 5174 else 5175 buf = NULL; 5176 ctl_done(io); 5177 if (buf) 5178 free(buf, M_CTL); 5179} 5180 5181void 5182ctl_config_read_done(union ctl_io *io) 5183{ 5184 uint8_t *buf; 5185 5186 /* 5187 * If there is some error -- we are done, skip data transfer. 5188 */ 5189 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5190 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5191 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5192 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5193 buf = io->scsiio.kern_data_ptr; 5194 else 5195 buf = NULL; 5196 ctl_done(io); 5197 if (buf) 5198 free(buf, M_CTL); 5199 return; 5200 } 5201 5202 /* 5203 * If the IO_CONT flag is set, we need to call the supplied 5204 * function to continue processing the I/O, instead of completing 5205 * the I/O just yet. 5206 */ 5207 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5208 io->scsiio.io_cont(io); 5209 return; 5210 } 5211 5212 ctl_datamove(io); 5213} 5214 5215/* 5216 * SCSI release command. 5217 */ 5218int 5219ctl_scsi_release(struct ctl_scsiio *ctsio) 5220{ 5221 struct ctl_lun *lun = CTL_LUN(ctsio); 5222 uint32_t residx; 5223 5224 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5225 5226 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5227 5228 /* 5229 * XXX KDM right now, we only support LUN reservation. We don't 5230 * support 3rd party reservations, or extent reservations, which 5231 * might actually need the parameter list. If we've gotten this 5232 * far, we've got a LUN reservation. Anything else got kicked out 5233 * above. So, according to SPC, ignore the length. 5234 */ 5235 5236 mtx_lock(&lun->lun_lock); 5237 5238 /* 5239 * According to SPC, it is not an error for an intiator to attempt 5240 * to release a reservation on a LUN that isn't reserved, or that 5241 * is reserved by another initiator. The reservation can only be 5242 * released, though, by the initiator who made it or by one of 5243 * several reset type events. 5244 */ 5245 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5246 lun->flags &= ~CTL_LUN_RESERVED; 5247 5248 mtx_unlock(&lun->lun_lock); 5249 5250 ctl_set_success(ctsio); 5251 ctl_done((union ctl_io *)ctsio); 5252 return (CTL_RETVAL_COMPLETE); 5253} 5254 5255int 5256ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5257{ 5258 struct ctl_lun *lun = CTL_LUN(ctsio); 5259 uint32_t residx; 5260 5261 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5262 5263 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5264 5265 /* 5266 * XXX KDM right now, we only support LUN reservation. We don't 5267 * support 3rd party reservations, or extent reservations, which 5268 * might actually need the parameter list. If we've gotten this 5269 * far, we've got a LUN reservation. Anything else got kicked out 5270 * above. So, according to SPC, ignore the length. 5271 */ 5272 5273 mtx_lock(&lun->lun_lock); 5274 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5275 ctl_set_reservation_conflict(ctsio); 5276 goto bailout; 5277 } 5278 5279 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5280 if (lun->flags & CTL_LUN_PR_RESERVED) { 5281 ctl_set_success(ctsio); 5282 goto bailout; 5283 } 5284 5285 lun->flags |= CTL_LUN_RESERVED; 5286 lun->res_idx = residx; 5287 ctl_set_success(ctsio); 5288 5289bailout: 5290 mtx_unlock(&lun->lun_lock); 5291 ctl_done((union ctl_io *)ctsio); 5292 return (CTL_RETVAL_COMPLETE); 5293} 5294 5295int 5296ctl_start_stop(struct ctl_scsiio *ctsio) 5297{ 5298 struct ctl_lun *lun = CTL_LUN(ctsio); 5299 struct scsi_start_stop_unit *cdb; 5300 int retval; 5301 5302 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5303 5304 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5305 5306 if ((cdb->how & SSS_PC_MASK) == 0) { 5307 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5308 (cdb->how & SSS_START) == 0) { 5309 uint32_t residx; 5310 5311 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5312 if (ctl_get_prkey(lun, residx) == 0 || 5313 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5314 5315 ctl_set_reservation_conflict(ctsio); 5316 ctl_done((union ctl_io *)ctsio); 5317 return (CTL_RETVAL_COMPLETE); 5318 } 5319 } 5320 5321 if ((cdb->how & SSS_LOEJ) && 5322 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5323 ctl_set_invalid_field(ctsio, 5324 /*sks_valid*/ 1, 5325 /*command*/ 1, 5326 /*field*/ 4, 5327 /*bit_valid*/ 1, 5328 /*bit*/ 1); 5329 ctl_done((union ctl_io *)ctsio); 5330 return (CTL_RETVAL_COMPLETE); 5331 } 5332 5333 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5334 lun->prevent_count > 0) { 5335 /* "Medium removal prevented" */ 5336 ctl_set_sense(ctsio, /*current_error*/ 1, 5337 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5338 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5339 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5340 ctl_done((union ctl_io *)ctsio); 5341 return (CTL_RETVAL_COMPLETE); 5342 } 5343 } 5344 5345 retval = lun->backend->config_write((union ctl_io *)ctsio); 5346 return (retval); 5347} 5348 5349int 5350ctl_prevent_allow(struct ctl_scsiio *ctsio) 5351{ 5352 struct ctl_lun *lun = CTL_LUN(ctsio); 5353 struct scsi_prevent *cdb; 5354 int retval; 5355 uint32_t initidx; 5356 5357 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5358 5359 cdb = (struct scsi_prevent *)ctsio->cdb; 5360 5361 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5362 ctl_set_invalid_opcode(ctsio); 5363 ctl_done((union ctl_io *)ctsio); 5364 return (CTL_RETVAL_COMPLETE); 5365 } 5366 5367 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5368 mtx_lock(&lun->lun_lock); 5369 if ((cdb->how & PR_PREVENT) && 5370 ctl_is_set(lun->prevent, initidx) == 0) { 5371 ctl_set_mask(lun->prevent, initidx); 5372 lun->prevent_count++; 5373 } else if ((cdb->how & PR_PREVENT) == 0 && 5374 ctl_is_set(lun->prevent, initidx)) { 5375 ctl_clear_mask(lun->prevent, initidx); 5376 lun->prevent_count--; 5377 } 5378 mtx_unlock(&lun->lun_lock); 5379 retval = lun->backend->config_write((union ctl_io *)ctsio); 5380 return (retval); 5381} 5382 5383/* 5384 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5385 * we don't really do anything with the LBA and length fields if the user 5386 * passes them in. Instead we'll just flush out the cache for the entire 5387 * LUN. 5388 */ 5389int 5390ctl_sync_cache(struct ctl_scsiio *ctsio) 5391{ 5392 struct ctl_lun *lun = CTL_LUN(ctsio); 5393 struct ctl_lba_len_flags *lbalen; 5394 uint64_t starting_lba; 5395 uint32_t block_count; 5396 int retval; 5397 uint8_t byte2; 5398 5399 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5400 5401 retval = 0; 5402 5403 switch (ctsio->cdb[0]) { 5404 case SYNCHRONIZE_CACHE: { 5405 struct scsi_sync_cache *cdb; 5406 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5407 5408 starting_lba = scsi_4btoul(cdb->begin_lba); 5409 block_count = scsi_2btoul(cdb->lb_count); 5410 byte2 = cdb->byte2; 5411 break; 5412 } 5413 case SYNCHRONIZE_CACHE_16: { 5414 struct scsi_sync_cache_16 *cdb; 5415 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5416 5417 starting_lba = scsi_8btou64(cdb->begin_lba); 5418 block_count = scsi_4btoul(cdb->lb_count); 5419 byte2 = cdb->byte2; 5420 break; 5421 } 5422 default: 5423 ctl_set_invalid_opcode(ctsio); 5424 ctl_done((union ctl_io *)ctsio); 5425 goto bailout; 5426 break; /* NOTREACHED */ 5427 } 5428 5429 /* 5430 * We check the LBA and length, but don't do anything with them. 5431 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5432 * get flushed. This check will just help satisfy anyone who wants 5433 * to see an error for an out of range LBA. 5434 */ 5435 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5436 ctl_set_lba_out_of_range(ctsio, 5437 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5438 ctl_done((union ctl_io *)ctsio); 5439 goto bailout; 5440 } 5441 5442 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5443 lbalen->lba = starting_lba; 5444 lbalen->len = block_count; 5445 lbalen->flags = byte2; 5446 retval = lun->backend->config_write((union ctl_io *)ctsio); 5447 5448bailout: 5449 return (retval); 5450} 5451 5452int 5453ctl_format(struct ctl_scsiio *ctsio) 5454{ 5455 struct scsi_format *cdb; 5456 int length, defect_list_len; 5457 5458 CTL_DEBUG_PRINT(("ctl_format\n")); 5459 5460 cdb = (struct scsi_format *)ctsio->cdb; 5461 5462 length = 0; 5463 if (cdb->byte2 & SF_FMTDATA) { 5464 if (cdb->byte2 & SF_LONGLIST) 5465 length = sizeof(struct scsi_format_header_long); 5466 else 5467 length = sizeof(struct scsi_format_header_short); 5468 } 5469 5470 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5471 && (length > 0)) { 5472 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5473 ctsio->kern_data_len = length; 5474 ctsio->kern_total_len = length; 5475 ctsio->kern_rel_offset = 0; 5476 ctsio->kern_sg_entries = 0; 5477 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5478 ctsio->be_move_done = ctl_config_move_done; 5479 ctl_datamove((union ctl_io *)ctsio); 5480 5481 return (CTL_RETVAL_COMPLETE); 5482 } 5483 5484 defect_list_len = 0; 5485 5486 if (cdb->byte2 & SF_FMTDATA) { 5487 if (cdb->byte2 & SF_LONGLIST) { 5488 struct scsi_format_header_long *header; 5489 5490 header = (struct scsi_format_header_long *) 5491 ctsio->kern_data_ptr; 5492 5493 defect_list_len = scsi_4btoul(header->defect_list_len); 5494 if (defect_list_len != 0) { 5495 ctl_set_invalid_field(ctsio, 5496 /*sks_valid*/ 1, 5497 /*command*/ 0, 5498 /*field*/ 2, 5499 /*bit_valid*/ 0, 5500 /*bit*/ 0); 5501 goto bailout; 5502 } 5503 } else { 5504 struct scsi_format_header_short *header; 5505 5506 header = (struct scsi_format_header_short *) 5507 ctsio->kern_data_ptr; 5508 5509 defect_list_len = scsi_2btoul(header->defect_list_len); 5510 if (defect_list_len != 0) { 5511 ctl_set_invalid_field(ctsio, 5512 /*sks_valid*/ 1, 5513 /*command*/ 0, 5514 /*field*/ 2, 5515 /*bit_valid*/ 0, 5516 /*bit*/ 0); 5517 goto bailout; 5518 } 5519 } 5520 } 5521 5522 ctl_set_success(ctsio); 5523bailout: 5524 5525 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5526 free(ctsio->kern_data_ptr, M_CTL); 5527 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5528 } 5529 5530 ctl_done((union ctl_io *)ctsio); 5531 return (CTL_RETVAL_COMPLETE); 5532} 5533 5534int 5535ctl_read_buffer(struct ctl_scsiio *ctsio) 5536{ 5537 struct ctl_lun *lun = CTL_LUN(ctsio); 5538 uint64_t buffer_offset; 5539 uint32_t len; 5540 uint8_t byte2; 5541 static uint8_t descr[4]; 5542 static uint8_t echo_descr[4] = { 0 }; 5543 5544 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5545 5546 switch (ctsio->cdb[0]) { 5547 case READ_BUFFER: { 5548 struct scsi_read_buffer *cdb; 5549 5550 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5551 buffer_offset = scsi_3btoul(cdb->offset); 5552 len = scsi_3btoul(cdb->length); 5553 byte2 = cdb->byte2; 5554 break; 5555 } 5556 case READ_BUFFER_16: { 5557 struct scsi_read_buffer_16 *cdb; 5558 5559 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5560 buffer_offset = scsi_8btou64(cdb->offset); 5561 len = scsi_4btoul(cdb->length); 5562 byte2 = cdb->byte2; 5563 break; 5564 } 5565 default: /* This shouldn't happen. */ 5566 ctl_set_invalid_opcode(ctsio); 5567 ctl_done((union ctl_io *)ctsio); 5568 return (CTL_RETVAL_COMPLETE); 5569 } 5570 5571 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5572 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5573 ctl_set_invalid_field(ctsio, 5574 /*sks_valid*/ 1, 5575 /*command*/ 1, 5576 /*field*/ 6, 5577 /*bit_valid*/ 0, 5578 /*bit*/ 0); 5579 ctl_done((union ctl_io *)ctsio); 5580 return (CTL_RETVAL_COMPLETE); 5581 } 5582 5583 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5584 descr[0] = 0; 5585 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5586 ctsio->kern_data_ptr = descr; 5587 len = min(len, sizeof(descr)); 5588 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5589 ctsio->kern_data_ptr = echo_descr; 5590 len = min(len, sizeof(echo_descr)); 5591 } else { 5592 if (lun->write_buffer == NULL) { 5593 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5594 M_CTL, M_WAITOK); 5595 } 5596 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5597 } 5598 ctsio->kern_data_len = len; 5599 ctsio->kern_total_len = len; 5600 ctsio->kern_rel_offset = 0; 5601 ctsio->kern_sg_entries = 0; 5602 ctl_set_success(ctsio); 5603 ctsio->be_move_done = ctl_config_move_done; 5604 ctl_datamove((union ctl_io *)ctsio); 5605 return (CTL_RETVAL_COMPLETE); 5606} 5607 5608int 5609ctl_write_buffer(struct ctl_scsiio *ctsio) 5610{ 5611 struct ctl_lun *lun = CTL_LUN(ctsio); 5612 struct scsi_write_buffer *cdb; 5613 int buffer_offset, len; 5614 5615 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5616 5617 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5618 5619 len = scsi_3btoul(cdb->length); 5620 buffer_offset = scsi_3btoul(cdb->offset); 5621 5622 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5623 ctl_set_invalid_field(ctsio, 5624 /*sks_valid*/ 1, 5625 /*command*/ 1, 5626 /*field*/ 6, 5627 /*bit_valid*/ 0, 5628 /*bit*/ 0); 5629 ctl_done((union ctl_io *)ctsio); 5630 return (CTL_RETVAL_COMPLETE); 5631 } 5632 5633 /* 5634 * If we've got a kernel request that hasn't been malloced yet, 5635 * malloc it and tell the caller the data buffer is here. 5636 */ 5637 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5638 if (lun->write_buffer == NULL) { 5639 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5640 M_CTL, M_WAITOK); 5641 } 5642 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5643 ctsio->kern_data_len = len; 5644 ctsio->kern_total_len = len; 5645 ctsio->kern_rel_offset = 0; 5646 ctsio->kern_sg_entries = 0; 5647 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5648 ctsio->be_move_done = ctl_config_move_done; 5649 ctl_datamove((union ctl_io *)ctsio); 5650 5651 return (CTL_RETVAL_COMPLETE); 5652 } 5653 5654 ctl_set_success(ctsio); 5655 ctl_done((union ctl_io *)ctsio); 5656 return (CTL_RETVAL_COMPLETE); 5657} 5658 5659int 5660ctl_write_same(struct ctl_scsiio *ctsio) 5661{ 5662 struct ctl_lun *lun = CTL_LUN(ctsio); 5663 struct ctl_lba_len_flags *lbalen; 5664 uint64_t lba; 5665 uint32_t num_blocks; 5666 int len, retval; 5667 uint8_t byte2; 5668 5669 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5670 5671 switch (ctsio->cdb[0]) { 5672 case WRITE_SAME_10: { 5673 struct scsi_write_same_10 *cdb; 5674 5675 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5676 5677 lba = scsi_4btoul(cdb->addr); 5678 num_blocks = scsi_2btoul(cdb->length); 5679 byte2 = cdb->byte2; 5680 break; 5681 } 5682 case WRITE_SAME_16: { 5683 struct scsi_write_same_16 *cdb; 5684 5685 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5686 5687 lba = scsi_8btou64(cdb->addr); 5688 num_blocks = scsi_4btoul(cdb->length); 5689 byte2 = cdb->byte2; 5690 break; 5691 } 5692 default: 5693 /* 5694 * We got a command we don't support. This shouldn't 5695 * happen, commands should be filtered out above us. 5696 */ 5697 ctl_set_invalid_opcode(ctsio); 5698 ctl_done((union ctl_io *)ctsio); 5699 5700 return (CTL_RETVAL_COMPLETE); 5701 break; /* NOTREACHED */ 5702 } 5703 5704 /* ANCHOR flag can be used only together with UNMAP */ 5705 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5706 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5707 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5708 ctl_done((union ctl_io *)ctsio); 5709 return (CTL_RETVAL_COMPLETE); 5710 } 5711 5712 /* 5713 * The first check is to make sure we're in bounds, the second 5714 * check is to catch wrap-around problems. If the lba + num blocks 5715 * is less than the lba, then we've wrapped around and the block 5716 * range is invalid anyway. 5717 */ 5718 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5719 || ((lba + num_blocks) < lba)) { 5720 ctl_set_lba_out_of_range(ctsio, 5721 MAX(lba, lun->be_lun->maxlba + 1)); 5722 ctl_done((union ctl_io *)ctsio); 5723 return (CTL_RETVAL_COMPLETE); 5724 } 5725 5726 /* Zero number of blocks means "to the last logical block" */ 5727 if (num_blocks == 0) { 5728 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5729 ctl_set_invalid_field(ctsio, 5730 /*sks_valid*/ 0, 5731 /*command*/ 1, 5732 /*field*/ 0, 5733 /*bit_valid*/ 0, 5734 /*bit*/ 0); 5735 ctl_done((union ctl_io *)ctsio); 5736 return (CTL_RETVAL_COMPLETE); 5737 } 5738 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5739 } 5740 5741 len = lun->be_lun->blocksize; 5742 5743 /* 5744 * If we've got a kernel request that hasn't been malloced yet, 5745 * malloc it and tell the caller the data buffer is here. 5746 */ 5747 if ((byte2 & SWS_NDOB) == 0 && 5748 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5749 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5750 ctsio->kern_data_len = len; 5751 ctsio->kern_total_len = len; 5752 ctsio->kern_rel_offset = 0; 5753 ctsio->kern_sg_entries = 0; 5754 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5755 ctsio->be_move_done = ctl_config_move_done; 5756 ctl_datamove((union ctl_io *)ctsio); 5757 5758 return (CTL_RETVAL_COMPLETE); 5759 } 5760 5761 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5762 lbalen->lba = lba; 5763 lbalen->len = num_blocks; 5764 lbalen->flags = byte2; 5765 retval = lun->backend->config_write((union ctl_io *)ctsio); 5766 5767 return (retval); 5768} 5769 5770int 5771ctl_unmap(struct ctl_scsiio *ctsio) 5772{ 5773 struct ctl_lun *lun = CTL_LUN(ctsio); 5774 struct scsi_unmap *cdb; 5775 struct ctl_ptr_len_flags *ptrlen; 5776 struct scsi_unmap_header *hdr; 5777 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5778 uint64_t lba; 5779 uint32_t num_blocks; 5780 int len, retval; 5781 uint8_t byte2; 5782 5783 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5784 5785 cdb = (struct scsi_unmap *)ctsio->cdb; 5786 len = scsi_2btoul(cdb->length); 5787 byte2 = cdb->byte2; 5788 5789 /* 5790 * If we've got a kernel request that hasn't been malloced yet, 5791 * malloc it and tell the caller the data buffer is here. 5792 */ 5793 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5794 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5795 ctsio->kern_data_len = len; 5796 ctsio->kern_total_len = len; 5797 ctsio->kern_rel_offset = 0; 5798 ctsio->kern_sg_entries = 0; 5799 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5800 ctsio->be_move_done = ctl_config_move_done; 5801 ctl_datamove((union ctl_io *)ctsio); 5802 5803 return (CTL_RETVAL_COMPLETE); 5804 } 5805 5806 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5807 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5808 if (len < sizeof (*hdr) || 5809 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5810 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5811 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5812 ctl_set_invalid_field(ctsio, 5813 /*sks_valid*/ 0, 5814 /*command*/ 0, 5815 /*field*/ 0, 5816 /*bit_valid*/ 0, 5817 /*bit*/ 0); 5818 goto done; 5819 } 5820 len = scsi_2btoul(hdr->desc_length); 5821 buf = (struct scsi_unmap_desc *)(hdr + 1); 5822 end = buf + len / sizeof(*buf); 5823 5824 endnz = buf; 5825 for (range = buf; range < end; range++) { 5826 lba = scsi_8btou64(range->lba); 5827 num_blocks = scsi_4btoul(range->length); 5828 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5829 || ((lba + num_blocks) < lba)) { 5830 ctl_set_lba_out_of_range(ctsio, 5831 MAX(lba, lun->be_lun->maxlba + 1)); 5832 ctl_done((union ctl_io *)ctsio); 5833 return (CTL_RETVAL_COMPLETE); 5834 } 5835 if (num_blocks != 0) 5836 endnz = range + 1; 5837 } 5838 5839 /* 5840 * Block backend can not handle zero last range. 5841 * Filter it out and return if there is nothing left. 5842 */ 5843 len = (uint8_t *)endnz - (uint8_t *)buf; 5844 if (len == 0) { 5845 ctl_set_success(ctsio); 5846 goto done; 5847 } 5848 5849 mtx_lock(&lun->lun_lock); 5850 ptrlen = (struct ctl_ptr_len_flags *) 5851 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5852 ptrlen->ptr = (void *)buf; 5853 ptrlen->len = len; 5854 ptrlen->flags = byte2; 5855 ctl_check_blocked(lun); 5856 mtx_unlock(&lun->lun_lock); 5857 5858 retval = lun->backend->config_write((union ctl_io *)ctsio); 5859 return (retval); 5860 5861done: 5862 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5863 free(ctsio->kern_data_ptr, M_CTL); 5864 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5865 } 5866 ctl_done((union ctl_io *)ctsio); 5867 return (CTL_RETVAL_COMPLETE); 5868} 5869 5870int 5871ctl_default_page_handler(struct ctl_scsiio *ctsio, 5872 struct ctl_page_index *page_index, uint8_t *page_ptr) 5873{ 5874 struct ctl_lun *lun = CTL_LUN(ctsio); 5875 uint8_t *current_cp; 5876 int set_ua; 5877 uint32_t initidx; 5878 5879 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5880 set_ua = 0; 5881 5882 current_cp = (page_index->page_data + (page_index->page_len * 5883 CTL_PAGE_CURRENT)); 5884 5885 mtx_lock(&lun->lun_lock); 5886 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5887 memcpy(current_cp, page_ptr, page_index->page_len); 5888 set_ua = 1; 5889 } 5890 if (set_ua != 0) 5891 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5892 mtx_unlock(&lun->lun_lock); 5893 if (set_ua) { 5894 ctl_isc_announce_mode(lun, 5895 ctl_get_initindex(&ctsio->io_hdr.nexus), 5896 page_index->page_code, page_index->subpage); 5897 } 5898 return (CTL_RETVAL_COMPLETE); 5899} 5900 5901static void 5902ctl_ie_timer(void *arg) 5903{ 5904 struct ctl_lun *lun = arg; 5905 uint64_t t; 5906 5907 if (lun->ie_asc == 0) 5908 return; 5909 5910 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5911 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5912 else 5913 lun->ie_reported = 0; 5914 5915 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5916 lun->ie_reportcnt++; 5917 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5918 if (t == 0 || t == UINT32_MAX) 5919 t = 3000; /* 5 min */ 5920 callout_schedule(&lun->ie_callout, t * hz / 10); 5921 } 5922} 5923 5924int 5925ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5926 struct ctl_page_index *page_index, uint8_t *page_ptr) 5927{ 5928 struct ctl_lun *lun = CTL_LUN(ctsio); 5929 struct scsi_info_exceptions_page *pg; 5930 uint64_t t; 5931 5932 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5933 5934 pg = (struct scsi_info_exceptions_page *)page_ptr; 5935 mtx_lock(&lun->lun_lock); 5936 if (pg->info_flags & SIEP_FLAGS_TEST) { 5937 lun->ie_asc = 0x5d; 5938 lun->ie_ascq = 0xff; 5939 if (pg->mrie == SIEP_MRIE_UA) { 5940 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5941 lun->ie_reported = 1; 5942 } else { 5943 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5944 lun->ie_reported = -1; 5945 } 5946 lun->ie_reportcnt = 1; 5947 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5948 lun->ie_reportcnt++; 5949 t = scsi_4btoul(pg->interval_timer); 5950 if (t == 0 || t == UINT32_MAX) 5951 t = 3000; /* 5 min */ 5952 callout_reset(&lun->ie_callout, t * hz / 10, 5953 ctl_ie_timer, lun); 5954 } 5955 } else { 5956 lun->ie_asc = 0; 5957 lun->ie_ascq = 0; 5958 lun->ie_reported = 1; 5959 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5960 lun->ie_reportcnt = UINT32_MAX; 5961 callout_stop(&lun->ie_callout); 5962 } 5963 mtx_unlock(&lun->lun_lock); 5964 return (CTL_RETVAL_COMPLETE); 5965} 5966 5967static int 5968ctl_do_mode_select(union ctl_io *io) 5969{ 5970 struct ctl_lun *lun = CTL_LUN(io); 5971 struct scsi_mode_page_header *page_header; 5972 struct ctl_page_index *page_index; 5973 struct ctl_scsiio *ctsio; 5974 int page_len, page_len_offset, page_len_size; 5975 union ctl_modepage_info *modepage_info; 5976 uint16_t *len_left, *len_used; 5977 int retval, i; 5978 5979 ctsio = &io->scsiio; 5980 page_index = NULL; 5981 page_len = 0; 5982 5983 modepage_info = (union ctl_modepage_info *) 5984 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5985 len_left = &modepage_info->header.len_left; 5986 len_used = &modepage_info->header.len_used; 5987 5988do_next_page: 5989 5990 page_header = (struct scsi_mode_page_header *) 5991 (ctsio->kern_data_ptr + *len_used); 5992 5993 if (*len_left == 0) { 5994 free(ctsio->kern_data_ptr, M_CTL); 5995 ctl_set_success(ctsio); 5996 ctl_done((union ctl_io *)ctsio); 5997 return (CTL_RETVAL_COMPLETE); 5998 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5999 6000 free(ctsio->kern_data_ptr, M_CTL); 6001 ctl_set_param_len_error(ctsio); 6002 ctl_done((union ctl_io *)ctsio); 6003 return (CTL_RETVAL_COMPLETE); 6004 6005 } else if ((page_header->page_code & SMPH_SPF) 6006 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6007 6008 free(ctsio->kern_data_ptr, M_CTL); 6009 ctl_set_param_len_error(ctsio); 6010 ctl_done((union ctl_io *)ctsio); 6011 return (CTL_RETVAL_COMPLETE); 6012 } 6013 6014 6015 /* 6016 * XXX KDM should we do something with the block descriptor? 6017 */ 6018 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6019 page_index = &lun->mode_pages.index[i]; 6020 if (lun->be_lun->lun_type == T_DIRECT && 6021 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6022 continue; 6023 if (lun->be_lun->lun_type == T_PROCESSOR && 6024 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6025 continue; 6026 if (lun->be_lun->lun_type == T_CDROM && 6027 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6028 continue; 6029 6030 if ((page_index->page_code & SMPH_PC_MASK) != 6031 (page_header->page_code & SMPH_PC_MASK)) 6032 continue; 6033 6034 /* 6035 * If neither page has a subpage code, then we've got a 6036 * match. 6037 */ 6038 if (((page_index->page_code & SMPH_SPF) == 0) 6039 && ((page_header->page_code & SMPH_SPF) == 0)) { 6040 page_len = page_header->page_length; 6041 break; 6042 } 6043 6044 /* 6045 * If both pages have subpages, then the subpage numbers 6046 * have to match. 6047 */ 6048 if ((page_index->page_code & SMPH_SPF) 6049 && (page_header->page_code & SMPH_SPF)) { 6050 struct scsi_mode_page_header_sp *sph; 6051 6052 sph = (struct scsi_mode_page_header_sp *)page_header; 6053 if (page_index->subpage == sph->subpage) { 6054 page_len = scsi_2btoul(sph->page_length); 6055 break; 6056 } 6057 } 6058 } 6059 6060 /* 6061 * If we couldn't find the page, or if we don't have a mode select 6062 * handler for it, send back an error to the user. 6063 */ 6064 if ((i >= CTL_NUM_MODE_PAGES) 6065 || (page_index->select_handler == NULL)) { 6066 ctl_set_invalid_field(ctsio, 6067 /*sks_valid*/ 1, 6068 /*command*/ 0, 6069 /*field*/ *len_used, 6070 /*bit_valid*/ 0, 6071 /*bit*/ 0); 6072 free(ctsio->kern_data_ptr, M_CTL); 6073 ctl_done((union ctl_io *)ctsio); 6074 return (CTL_RETVAL_COMPLETE); 6075 } 6076 6077 if (page_index->page_code & SMPH_SPF) { 6078 page_len_offset = 2; 6079 page_len_size = 2; 6080 } else { 6081 page_len_size = 1; 6082 page_len_offset = 1; 6083 } 6084 6085 /* 6086 * If the length the initiator gives us isn't the one we specify in 6087 * the mode page header, or if they didn't specify enough data in 6088 * the CDB to avoid truncating this page, kick out the request. 6089 */ 6090 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6091 ctl_set_invalid_field(ctsio, 6092 /*sks_valid*/ 1, 6093 /*command*/ 0, 6094 /*field*/ *len_used + page_len_offset, 6095 /*bit_valid*/ 0, 6096 /*bit*/ 0); 6097 free(ctsio->kern_data_ptr, M_CTL); 6098 ctl_done((union ctl_io *)ctsio); 6099 return (CTL_RETVAL_COMPLETE); 6100 } 6101 if (*len_left < page_index->page_len) { 6102 free(ctsio->kern_data_ptr, M_CTL); 6103 ctl_set_param_len_error(ctsio); 6104 ctl_done((union ctl_io *)ctsio); 6105 return (CTL_RETVAL_COMPLETE); 6106 } 6107 6108 /* 6109 * Run through the mode page, checking to make sure that the bits 6110 * the user changed are actually legal for him to change. 6111 */ 6112 for (i = 0; i < page_index->page_len; i++) { 6113 uint8_t *user_byte, *change_mask, *current_byte; 6114 int bad_bit; 6115 int j; 6116 6117 user_byte = (uint8_t *)page_header + i; 6118 change_mask = page_index->page_data + 6119 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6120 current_byte = page_index->page_data + 6121 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6122 6123 /* 6124 * Check to see whether the user set any bits in this byte 6125 * that he is not allowed to set. 6126 */ 6127 if ((*user_byte & ~(*change_mask)) == 6128 (*current_byte & ~(*change_mask))) 6129 continue; 6130 6131 /* 6132 * Go through bit by bit to determine which one is illegal. 6133 */ 6134 bad_bit = 0; 6135 for (j = 7; j >= 0; j--) { 6136 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6137 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6138 bad_bit = i; 6139 break; 6140 } 6141 } 6142 ctl_set_invalid_field(ctsio, 6143 /*sks_valid*/ 1, 6144 /*command*/ 0, 6145 /*field*/ *len_used + i, 6146 /*bit_valid*/ 1, 6147 /*bit*/ bad_bit); 6148 free(ctsio->kern_data_ptr, M_CTL); 6149 ctl_done((union ctl_io *)ctsio); 6150 return (CTL_RETVAL_COMPLETE); 6151 } 6152 6153 /* 6154 * Decrement these before we call the page handler, since we may 6155 * end up getting called back one way or another before the handler 6156 * returns to this context. 6157 */ 6158 *len_left -= page_index->page_len; 6159 *len_used += page_index->page_len; 6160 6161 retval = page_index->select_handler(ctsio, page_index, 6162 (uint8_t *)page_header); 6163 6164 /* 6165 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6166 * wait until this queued command completes to finish processing 6167 * the mode page. If it returns anything other than 6168 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6169 * already set the sense information, freed the data pointer, and 6170 * completed the io for us. 6171 */ 6172 if (retval != CTL_RETVAL_COMPLETE) 6173 goto bailout_no_done; 6174 6175 /* 6176 * If the initiator sent us more than one page, parse the next one. 6177 */ 6178 if (*len_left > 0) 6179 goto do_next_page; 6180 6181 ctl_set_success(ctsio); 6182 free(ctsio->kern_data_ptr, M_CTL); 6183 ctl_done((union ctl_io *)ctsio); 6184 6185bailout_no_done: 6186 6187 return (CTL_RETVAL_COMPLETE); 6188 6189} 6190 6191int 6192ctl_mode_select(struct ctl_scsiio *ctsio) 6193{ 6194 struct ctl_lun *lun = CTL_LUN(ctsio); 6195 union ctl_modepage_info *modepage_info; 6196 int bd_len, i, header_size, param_len, pf, rtd, sp; 6197 uint32_t initidx; 6198 6199 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6200 switch (ctsio->cdb[0]) { 6201 case MODE_SELECT_6: { 6202 struct scsi_mode_select_6 *cdb; 6203 6204 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6205 6206 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6207 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6208 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6209 param_len = cdb->length; 6210 header_size = sizeof(struct scsi_mode_header_6); 6211 break; 6212 } 6213 case MODE_SELECT_10: { 6214 struct scsi_mode_select_10 *cdb; 6215 6216 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6217 6218 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6219 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6220 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6221 param_len = scsi_2btoul(cdb->length); 6222 header_size = sizeof(struct scsi_mode_header_10); 6223 break; 6224 } 6225 default: 6226 ctl_set_invalid_opcode(ctsio); 6227 ctl_done((union ctl_io *)ctsio); 6228 return (CTL_RETVAL_COMPLETE); 6229 } 6230 6231 if (rtd) { 6232 if (param_len != 0) { 6233 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6234 /*command*/ 1, /*field*/ 0, 6235 /*bit_valid*/ 0, /*bit*/ 0); 6236 ctl_done((union ctl_io *)ctsio); 6237 return (CTL_RETVAL_COMPLETE); 6238 } 6239 6240 /* Revert to defaults. */ 6241 ctl_init_page_index(lun); 6242 mtx_lock(&lun->lun_lock); 6243 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6244 mtx_unlock(&lun->lun_lock); 6245 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6246 ctl_isc_announce_mode(lun, -1, 6247 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6248 lun->mode_pages.index[i].subpage); 6249 } 6250 ctl_set_success(ctsio); 6251 ctl_done((union ctl_io *)ctsio); 6252 return (CTL_RETVAL_COMPLETE); 6253 } 6254 6255 /* 6256 * From SPC-3: 6257 * "A parameter list length of zero indicates that the Data-Out Buffer 6258 * shall be empty. This condition shall not be considered as an error." 6259 */ 6260 if (param_len == 0) { 6261 ctl_set_success(ctsio); 6262 ctl_done((union ctl_io *)ctsio); 6263 return (CTL_RETVAL_COMPLETE); 6264 } 6265 6266 /* 6267 * Since we'll hit this the first time through, prior to 6268 * allocation, we don't need to free a data buffer here. 6269 */ 6270 if (param_len < header_size) { 6271 ctl_set_param_len_error(ctsio); 6272 ctl_done((union ctl_io *)ctsio); 6273 return (CTL_RETVAL_COMPLETE); 6274 } 6275 6276 /* 6277 * Allocate the data buffer and grab the user's data. In theory, 6278 * we shouldn't have to sanity check the parameter list length here 6279 * because the maximum size is 64K. We should be able to malloc 6280 * that much without too many problems. 6281 */ 6282 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6283 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6284 ctsio->kern_data_len = param_len; 6285 ctsio->kern_total_len = param_len; 6286 ctsio->kern_rel_offset = 0; 6287 ctsio->kern_sg_entries = 0; 6288 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6289 ctsio->be_move_done = ctl_config_move_done; 6290 ctl_datamove((union ctl_io *)ctsio); 6291 6292 return (CTL_RETVAL_COMPLETE); 6293 } 6294 6295 switch (ctsio->cdb[0]) { 6296 case MODE_SELECT_6: { 6297 struct scsi_mode_header_6 *mh6; 6298 6299 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6300 bd_len = mh6->blk_desc_len; 6301 break; 6302 } 6303 case MODE_SELECT_10: { 6304 struct scsi_mode_header_10 *mh10; 6305 6306 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6307 bd_len = scsi_2btoul(mh10->blk_desc_len); 6308 break; 6309 } 6310 default: 6311 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6312 } 6313 6314 if (param_len < (header_size + bd_len)) { 6315 free(ctsio->kern_data_ptr, M_CTL); 6316 ctl_set_param_len_error(ctsio); 6317 ctl_done((union ctl_io *)ctsio); 6318 return (CTL_RETVAL_COMPLETE); 6319 } 6320 6321 /* 6322 * Set the IO_CONT flag, so that if this I/O gets passed to 6323 * ctl_config_write_done(), it'll get passed back to 6324 * ctl_do_mode_select() for further processing, or completion if 6325 * we're all done. 6326 */ 6327 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6328 ctsio->io_cont = ctl_do_mode_select; 6329 6330 modepage_info = (union ctl_modepage_info *) 6331 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6332 memset(modepage_info, 0, sizeof(*modepage_info)); 6333 modepage_info->header.len_left = param_len - header_size - bd_len; 6334 modepage_info->header.len_used = header_size + bd_len; 6335 6336 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6337} 6338 6339int 6340ctl_mode_sense(struct ctl_scsiio *ctsio) 6341{ 6342 struct ctl_lun *lun = CTL_LUN(ctsio); 6343 int pc, page_code, dbd, llba, subpage; 6344 int alloc_len, page_len, header_len, total_len; 6345 struct scsi_mode_block_descr *block_desc; 6346 struct ctl_page_index *page_index; 6347 6348 dbd = 0; 6349 llba = 0; 6350 block_desc = NULL; 6351 6352 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6353 6354 switch (ctsio->cdb[0]) { 6355 case MODE_SENSE_6: { 6356 struct scsi_mode_sense_6 *cdb; 6357 6358 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6359 6360 header_len = sizeof(struct scsi_mode_hdr_6); 6361 if (cdb->byte2 & SMS_DBD) 6362 dbd = 1; 6363 else 6364 header_len += sizeof(struct scsi_mode_block_descr); 6365 6366 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6367 page_code = cdb->page & SMS_PAGE_CODE; 6368 subpage = cdb->subpage; 6369 alloc_len = cdb->length; 6370 break; 6371 } 6372 case MODE_SENSE_10: { 6373 struct scsi_mode_sense_10 *cdb; 6374 6375 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6376 6377 header_len = sizeof(struct scsi_mode_hdr_10); 6378 6379 if (cdb->byte2 & SMS_DBD) 6380 dbd = 1; 6381 else 6382 header_len += sizeof(struct scsi_mode_block_descr); 6383 if (cdb->byte2 & SMS10_LLBAA) 6384 llba = 1; 6385 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6386 page_code = cdb->page & SMS_PAGE_CODE; 6387 subpage = cdb->subpage; 6388 alloc_len = scsi_2btoul(cdb->length); 6389 break; 6390 } 6391 default: 6392 ctl_set_invalid_opcode(ctsio); 6393 ctl_done((union ctl_io *)ctsio); 6394 return (CTL_RETVAL_COMPLETE); 6395 break; /* NOTREACHED */ 6396 } 6397 6398 /* 6399 * We have to make a first pass through to calculate the size of 6400 * the pages that match the user's query. Then we allocate enough 6401 * memory to hold it, and actually copy the data into the buffer. 6402 */ 6403 switch (page_code) { 6404 case SMS_ALL_PAGES_PAGE: { 6405 u_int i; 6406 6407 page_len = 0; 6408 6409 /* 6410 * At the moment, values other than 0 and 0xff here are 6411 * reserved according to SPC-3. 6412 */ 6413 if ((subpage != SMS_SUBPAGE_PAGE_0) 6414 && (subpage != SMS_SUBPAGE_ALL)) { 6415 ctl_set_invalid_field(ctsio, 6416 /*sks_valid*/ 1, 6417 /*command*/ 1, 6418 /*field*/ 3, 6419 /*bit_valid*/ 0, 6420 /*bit*/ 0); 6421 ctl_done((union ctl_io *)ctsio); 6422 return (CTL_RETVAL_COMPLETE); 6423 } 6424 6425 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6426 page_index = &lun->mode_pages.index[i]; 6427 6428 /* Make sure the page is supported for this dev type */ 6429 if (lun->be_lun->lun_type == T_DIRECT && 6430 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6431 continue; 6432 if (lun->be_lun->lun_type == T_PROCESSOR && 6433 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6434 continue; 6435 if (lun->be_lun->lun_type == T_CDROM && 6436 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6437 continue; 6438 6439 /* 6440 * We don't use this subpage if the user didn't 6441 * request all subpages. 6442 */ 6443 if ((page_index->subpage != 0) 6444 && (subpage == SMS_SUBPAGE_PAGE_0)) 6445 continue; 6446 6447#if 0 6448 printf("found page %#x len %d\n", 6449 page_index->page_code & SMPH_PC_MASK, 6450 page_index->page_len); 6451#endif 6452 page_len += page_index->page_len; 6453 } 6454 break; 6455 } 6456 default: { 6457 u_int i; 6458 6459 page_len = 0; 6460 6461 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6462 page_index = &lun->mode_pages.index[i]; 6463 6464 /* Make sure the page is supported for this dev type */ 6465 if (lun->be_lun->lun_type == T_DIRECT && 6466 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6467 continue; 6468 if (lun->be_lun->lun_type == T_PROCESSOR && 6469 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6470 continue; 6471 if (lun->be_lun->lun_type == T_CDROM && 6472 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6473 continue; 6474 6475 /* Look for the right page code */ 6476 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6477 continue; 6478 6479 /* Look for the right subpage or the subpage wildcard*/ 6480 if ((page_index->subpage != subpage) 6481 && (subpage != SMS_SUBPAGE_ALL)) 6482 continue; 6483 6484#if 0 6485 printf("found page %#x len %d\n", 6486 page_index->page_code & SMPH_PC_MASK, 6487 page_index->page_len); 6488#endif 6489 6490 page_len += page_index->page_len; 6491 } 6492 6493 if (page_len == 0) { 6494 ctl_set_invalid_field(ctsio, 6495 /*sks_valid*/ 1, 6496 /*command*/ 1, 6497 /*field*/ 2, 6498 /*bit_valid*/ 1, 6499 /*bit*/ 5); 6500 ctl_done((union ctl_io *)ctsio); 6501 return (CTL_RETVAL_COMPLETE); 6502 } 6503 break; 6504 } 6505 } 6506 6507 total_len = header_len + page_len; 6508#if 0 6509 printf("header_len = %d, page_len = %d, total_len = %d\n", 6510 header_len, page_len, total_len); 6511#endif 6512 6513 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6514 ctsio->kern_sg_entries = 0; 6515 ctsio->kern_rel_offset = 0; 6516 ctsio->kern_data_len = min(total_len, alloc_len); 6517 ctsio->kern_total_len = ctsio->kern_data_len; 6518 6519 switch (ctsio->cdb[0]) { 6520 case MODE_SENSE_6: { 6521 struct scsi_mode_hdr_6 *header; 6522 6523 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6524 6525 header->datalen = MIN(total_len - 1, 254); 6526 if (lun->be_lun->lun_type == T_DIRECT) { 6527 header->dev_specific = 0x10; /* DPOFUA */ 6528 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6529 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6530 header->dev_specific |= 0x80; /* WP */ 6531 } 6532 if (dbd) 6533 header->block_descr_len = 0; 6534 else 6535 header->block_descr_len = 6536 sizeof(struct scsi_mode_block_descr); 6537 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6538 break; 6539 } 6540 case MODE_SENSE_10: { 6541 struct scsi_mode_hdr_10 *header; 6542 int datalen; 6543 6544 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6545 6546 datalen = MIN(total_len - 2, 65533); 6547 scsi_ulto2b(datalen, header->datalen); 6548 if (lun->be_lun->lun_type == T_DIRECT) { 6549 header->dev_specific = 0x10; /* DPOFUA */ 6550 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6551 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6552 header->dev_specific |= 0x80; /* WP */ 6553 } 6554 if (dbd) 6555 scsi_ulto2b(0, header->block_descr_len); 6556 else 6557 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6558 header->block_descr_len); 6559 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6560 break; 6561 } 6562 default: 6563 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6564 } 6565 6566 /* 6567 * If we've got a disk, use its blocksize in the block 6568 * descriptor. Otherwise, just set it to 0. 6569 */ 6570 if (dbd == 0) { 6571 if (lun->be_lun->lun_type == T_DIRECT) 6572 scsi_ulto3b(lun->be_lun->blocksize, 6573 block_desc->block_len); 6574 else 6575 scsi_ulto3b(0, block_desc->block_len); 6576 } 6577 6578 switch (page_code) { 6579 case SMS_ALL_PAGES_PAGE: { 6580 int i, data_used; 6581 6582 data_used = header_len; 6583 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6584 struct ctl_page_index *page_index; 6585 6586 page_index = &lun->mode_pages.index[i]; 6587 if (lun->be_lun->lun_type == T_DIRECT && 6588 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6589 continue; 6590 if (lun->be_lun->lun_type == T_PROCESSOR && 6591 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6592 continue; 6593 if (lun->be_lun->lun_type == T_CDROM && 6594 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6595 continue; 6596 6597 /* 6598 * We don't use this subpage if the user didn't 6599 * request all subpages. We already checked (above) 6600 * to make sure the user only specified a subpage 6601 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6602 */ 6603 if ((page_index->subpage != 0) 6604 && (subpage == SMS_SUBPAGE_PAGE_0)) 6605 continue; 6606 6607 /* 6608 * Call the handler, if it exists, to update the 6609 * page to the latest values. 6610 */ 6611 if (page_index->sense_handler != NULL) 6612 page_index->sense_handler(ctsio, page_index,pc); 6613 6614 memcpy(ctsio->kern_data_ptr + data_used, 6615 page_index->page_data + 6616 (page_index->page_len * pc), 6617 page_index->page_len); 6618 data_used += page_index->page_len; 6619 } 6620 break; 6621 } 6622 default: { 6623 int i, data_used; 6624 6625 data_used = header_len; 6626 6627 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6628 struct ctl_page_index *page_index; 6629 6630 page_index = &lun->mode_pages.index[i]; 6631 6632 /* Look for the right page code */ 6633 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6634 continue; 6635 6636 /* Look for the right subpage or the subpage wildcard*/ 6637 if ((page_index->subpage != subpage) 6638 && (subpage != SMS_SUBPAGE_ALL)) 6639 continue; 6640 6641 /* Make sure the page is supported for this dev type */ 6642 if (lun->be_lun->lun_type == T_DIRECT && 6643 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6644 continue; 6645 if (lun->be_lun->lun_type == T_PROCESSOR && 6646 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6647 continue; 6648 if (lun->be_lun->lun_type == T_CDROM && 6649 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6650 continue; 6651 6652 /* 6653 * Call the handler, if it exists, to update the 6654 * page to the latest values. 6655 */ 6656 if (page_index->sense_handler != NULL) 6657 page_index->sense_handler(ctsio, page_index,pc); 6658 6659 memcpy(ctsio->kern_data_ptr + data_used, 6660 page_index->page_data + 6661 (page_index->page_len * pc), 6662 page_index->page_len); 6663 data_used += page_index->page_len; 6664 } 6665 break; 6666 } 6667 } 6668 6669 ctl_set_success(ctsio); 6670 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6671 ctsio->be_move_done = ctl_config_move_done; 6672 ctl_datamove((union ctl_io *)ctsio); 6673 return (CTL_RETVAL_COMPLETE); 6674} 6675 6676int 6677ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6678 struct ctl_page_index *page_index, 6679 int pc) 6680{ 6681 struct ctl_lun *lun = CTL_LUN(ctsio); 6682 struct scsi_log_param_header *phdr; 6683 uint8_t *data; 6684 uint64_t val; 6685 6686 data = page_index->page_data; 6687 6688 if (lun->backend->lun_attr != NULL && 6689 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6690 != UINT64_MAX) { 6691 phdr = (struct scsi_log_param_header *)data; 6692 scsi_ulto2b(0x0001, phdr->param_code); 6693 phdr->param_control = SLP_LBIN | SLP_LP; 6694 phdr->param_len = 8; 6695 data = (uint8_t *)(phdr + 1); 6696 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6697 data[4] = 0x02; /* per-pool */ 6698 data += phdr->param_len; 6699 } 6700 6701 if (lun->backend->lun_attr != NULL && 6702 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6703 != UINT64_MAX) { 6704 phdr = (struct scsi_log_param_header *)data; 6705 scsi_ulto2b(0x0002, phdr->param_code); 6706 phdr->param_control = SLP_LBIN | SLP_LP; 6707 phdr->param_len = 8; 6708 data = (uint8_t *)(phdr + 1); 6709 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6710 data[4] = 0x01; /* per-LUN */ 6711 data += phdr->param_len; 6712 } 6713 6714 if (lun->backend->lun_attr != NULL && 6715 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6716 != UINT64_MAX) { 6717 phdr = (struct scsi_log_param_header *)data; 6718 scsi_ulto2b(0x00f1, phdr->param_code); 6719 phdr->param_control = SLP_LBIN | SLP_LP; 6720 phdr->param_len = 8; 6721 data = (uint8_t *)(phdr + 1); 6722 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6723 data[4] = 0x02; /* per-pool */ 6724 data += phdr->param_len; 6725 } 6726 6727 if (lun->backend->lun_attr != NULL && 6728 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6729 != UINT64_MAX) { 6730 phdr = (struct scsi_log_param_header *)data; 6731 scsi_ulto2b(0x00f2, phdr->param_code); 6732 phdr->param_control = SLP_LBIN | SLP_LP; 6733 phdr->param_len = 8; 6734 data = (uint8_t *)(phdr + 1); 6735 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6736 data[4] = 0x02; /* per-pool */ 6737 data += phdr->param_len; 6738 } 6739 6740 page_index->page_len = data - page_index->page_data; 6741 return (0); 6742} 6743 6744int 6745ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6746 struct ctl_page_index *page_index, 6747 int pc) 6748{ 6749 struct ctl_lun *lun = CTL_LUN(ctsio); 6750 struct stat_page *data; 6751 struct bintime *t; 6752 6753 data = (struct stat_page *)page_index->page_data; 6754 6755 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6756 data->sap.hdr.param_control = SLP_LBIN; 6757 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6758 sizeof(struct scsi_log_param_header); 6759 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6760 data->sap.read_num); 6761 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6762 data->sap.write_num); 6763 if (lun->be_lun->blocksize > 0) { 6764 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6765 lun->be_lun->blocksize, data->sap.recvieved_lba); 6766 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6767 lun->be_lun->blocksize, data->sap.transmitted_lba); 6768 } 6769 t = &lun->stats.time[CTL_STATS_READ]; 6770 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6771 data->sap.read_int); 6772 t = &lun->stats.time[CTL_STATS_WRITE]; 6773 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6774 data->sap.write_int); 6775 scsi_u64to8b(0, data->sap.weighted_num); 6776 scsi_u64to8b(0, data->sap.weighted_int); 6777 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6778 data->it.hdr.param_control = SLP_LBIN; 6779 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6780 sizeof(struct scsi_log_param_header); 6781#ifdef CTL_TIME_IO 6782 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6783#endif 6784 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6785 data->it.hdr.param_control = SLP_LBIN; 6786 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6787 sizeof(struct scsi_log_param_header); 6788 scsi_ulto4b(3, data->ti.exponent); 6789 scsi_ulto4b(1, data->ti.integer); 6790 return (0); 6791} 6792 6793int 6794ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6795 struct ctl_page_index *page_index, 6796 int pc) 6797{ 6798 struct ctl_lun *lun = CTL_LUN(ctsio); 6799 struct scsi_log_informational_exceptions *data; 6800 6801 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6802 6803 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6804 data->hdr.param_control = SLP_LBIN; 6805 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6806 sizeof(struct scsi_log_param_header); 6807 data->ie_asc = lun->ie_asc; 6808 data->ie_ascq = lun->ie_ascq; 6809 data->temperature = 0xff; 6810 return (0); 6811} 6812 6813int 6814ctl_log_sense(struct ctl_scsiio *ctsio) 6815{ 6816 struct ctl_lun *lun = CTL_LUN(ctsio); 6817 int i, pc, page_code, subpage; 6818 int alloc_len, total_len; 6819 struct ctl_page_index *page_index; 6820 struct scsi_log_sense *cdb; 6821 struct scsi_log_header *header; 6822 6823 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6824 6825 cdb = (struct scsi_log_sense *)ctsio->cdb; 6826 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6827 page_code = cdb->page & SLS_PAGE_CODE; 6828 subpage = cdb->subpage; 6829 alloc_len = scsi_2btoul(cdb->length); 6830 6831 page_index = NULL; 6832 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6833 page_index = &lun->log_pages.index[i]; 6834 6835 /* Look for the right page code */ 6836 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6837 continue; 6838 6839 /* Look for the right subpage or the subpage wildcard*/ 6840 if (page_index->subpage != subpage) 6841 continue; 6842 6843 break; 6844 } 6845 if (i >= CTL_NUM_LOG_PAGES) { 6846 ctl_set_invalid_field(ctsio, 6847 /*sks_valid*/ 1, 6848 /*command*/ 1, 6849 /*field*/ 2, 6850 /*bit_valid*/ 0, 6851 /*bit*/ 0); 6852 ctl_done((union ctl_io *)ctsio); 6853 return (CTL_RETVAL_COMPLETE); 6854 } 6855 6856 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6857 6858 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6859 ctsio->kern_sg_entries = 0; 6860 ctsio->kern_rel_offset = 0; 6861 ctsio->kern_data_len = min(total_len, alloc_len); 6862 ctsio->kern_total_len = ctsio->kern_data_len; 6863 6864 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6865 header->page = page_index->page_code; 6866 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6867 header->page |= SL_DS; 6868 if (page_index->subpage) { 6869 header->page |= SL_SPF; 6870 header->subpage = page_index->subpage; 6871 } 6872 scsi_ulto2b(page_index->page_len, header->datalen); 6873 6874 /* 6875 * Call the handler, if it exists, to update the 6876 * page to the latest values. 6877 */ 6878 if (page_index->sense_handler != NULL) 6879 page_index->sense_handler(ctsio, page_index, pc); 6880 6881 memcpy(header + 1, page_index->page_data, page_index->page_len); 6882 6883 ctl_set_success(ctsio); 6884 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6885 ctsio->be_move_done = ctl_config_move_done; 6886 ctl_datamove((union ctl_io *)ctsio); 6887 return (CTL_RETVAL_COMPLETE); 6888} 6889 6890int 6891ctl_read_capacity(struct ctl_scsiio *ctsio) 6892{ 6893 struct ctl_lun *lun = CTL_LUN(ctsio); 6894 struct scsi_read_capacity *cdb; 6895 struct scsi_read_capacity_data *data; 6896 uint32_t lba; 6897 6898 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6899 6900 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6901 6902 lba = scsi_4btoul(cdb->addr); 6903 if (((cdb->pmi & SRC_PMI) == 0) 6904 && (lba != 0)) { 6905 ctl_set_invalid_field(/*ctsio*/ ctsio, 6906 /*sks_valid*/ 1, 6907 /*command*/ 1, 6908 /*field*/ 2, 6909 /*bit_valid*/ 0, 6910 /*bit*/ 0); 6911 ctl_done((union ctl_io *)ctsio); 6912 return (CTL_RETVAL_COMPLETE); 6913 } 6914 6915 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6916 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6917 ctsio->kern_data_len = sizeof(*data); 6918 ctsio->kern_total_len = sizeof(*data); 6919 ctsio->kern_rel_offset = 0; 6920 ctsio->kern_sg_entries = 0; 6921 6922 /* 6923 * If the maximum LBA is greater than 0xfffffffe, the user must 6924 * issue a SERVICE ACTION IN (16) command, with the read capacity 6925 * serivce action set. 6926 */ 6927 if (lun->be_lun->maxlba > 0xfffffffe) 6928 scsi_ulto4b(0xffffffff, data->addr); 6929 else 6930 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6931 6932 /* 6933 * XXX KDM this may not be 512 bytes... 6934 */ 6935 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6936 6937 ctl_set_success(ctsio); 6938 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6939 ctsio->be_move_done = ctl_config_move_done; 6940 ctl_datamove((union ctl_io *)ctsio); 6941 return (CTL_RETVAL_COMPLETE); 6942} 6943 6944int 6945ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6946{ 6947 struct ctl_lun *lun = CTL_LUN(ctsio); 6948 struct scsi_read_capacity_16 *cdb; 6949 struct scsi_read_capacity_data_long *data; 6950 uint64_t lba; 6951 uint32_t alloc_len; 6952 6953 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6954 6955 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6956 6957 alloc_len = scsi_4btoul(cdb->alloc_len); 6958 lba = scsi_8btou64(cdb->addr); 6959 6960 if ((cdb->reladr & SRC16_PMI) 6961 && (lba != 0)) { 6962 ctl_set_invalid_field(/*ctsio*/ ctsio, 6963 /*sks_valid*/ 1, 6964 /*command*/ 1, 6965 /*field*/ 2, 6966 /*bit_valid*/ 0, 6967 /*bit*/ 0); 6968 ctl_done((union ctl_io *)ctsio); 6969 return (CTL_RETVAL_COMPLETE); 6970 } 6971 6972 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6973 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6974 ctsio->kern_rel_offset = 0; 6975 ctsio->kern_sg_entries = 0; 6976 ctsio->kern_data_len = min(sizeof(*data), alloc_len); 6977 ctsio->kern_total_len = ctsio->kern_data_len; 6978 6979 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6980 /* XXX KDM this may not be 512 bytes... */ 6981 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6982 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6983 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6984 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6985 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6986 6987 ctl_set_success(ctsio); 6988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6989 ctsio->be_move_done = ctl_config_move_done; 6990 ctl_datamove((union ctl_io *)ctsio); 6991 return (CTL_RETVAL_COMPLETE); 6992} 6993 6994int 6995ctl_get_lba_status(struct ctl_scsiio *ctsio) 6996{ 6997 struct ctl_lun *lun = CTL_LUN(ctsio); 6998 struct scsi_get_lba_status *cdb; 6999 struct scsi_get_lba_status_data *data; 7000 struct ctl_lba_len_flags *lbalen; 7001 uint64_t lba; 7002 uint32_t alloc_len, total_len; 7003 int retval; 7004 7005 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7006 7007 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7008 lba = scsi_8btou64(cdb->addr); 7009 alloc_len = scsi_4btoul(cdb->alloc_len); 7010 7011 if (lba > lun->be_lun->maxlba) { 7012 ctl_set_lba_out_of_range(ctsio, lba); 7013 ctl_done((union ctl_io *)ctsio); 7014 return (CTL_RETVAL_COMPLETE); 7015 } 7016 7017 total_len = sizeof(*data) + sizeof(data->descr[0]); 7018 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7019 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7020 ctsio->kern_rel_offset = 0; 7021 ctsio->kern_sg_entries = 0; 7022 ctsio->kern_data_len = min(total_len, alloc_len); 7023 ctsio->kern_total_len = ctsio->kern_data_len; 7024 7025 /* Fill dummy data in case backend can't tell anything. */ 7026 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7027 scsi_u64to8b(lba, data->descr[0].addr); 7028 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7029 data->descr[0].length); 7030 data->descr[0].status = 0; /* Mapped or unknown. */ 7031 7032 ctl_set_success(ctsio); 7033 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7034 ctsio->be_move_done = ctl_config_move_done; 7035 7036 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7037 lbalen->lba = lba; 7038 lbalen->len = total_len; 7039 lbalen->flags = 0; 7040 retval = lun->backend->config_read((union ctl_io *)ctsio); 7041 return (retval); 7042} 7043 7044int 7045ctl_read_defect(struct ctl_scsiio *ctsio) 7046{ 7047 struct scsi_read_defect_data_10 *ccb10; 7048 struct scsi_read_defect_data_12 *ccb12; 7049 struct scsi_read_defect_data_hdr_10 *data10; 7050 struct scsi_read_defect_data_hdr_12 *data12; 7051 uint32_t alloc_len, data_len; 7052 uint8_t format; 7053 7054 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7055 7056 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7057 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7058 format = ccb10->format; 7059 alloc_len = scsi_2btoul(ccb10->alloc_length); 7060 data_len = sizeof(*data10); 7061 } else { 7062 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7063 format = ccb12->format; 7064 alloc_len = scsi_4btoul(ccb12->alloc_length); 7065 data_len = sizeof(*data12); 7066 } 7067 if (alloc_len == 0) { 7068 ctl_set_success(ctsio); 7069 ctl_done((union ctl_io *)ctsio); 7070 return (CTL_RETVAL_COMPLETE); 7071 } 7072 7073 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7074 ctsio->kern_rel_offset = 0; 7075 ctsio->kern_sg_entries = 0; 7076 ctsio->kern_data_len = min(data_len, alloc_len); 7077 ctsio->kern_total_len = ctsio->kern_data_len; 7078 7079 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7080 data10 = (struct scsi_read_defect_data_hdr_10 *) 7081 ctsio->kern_data_ptr; 7082 data10->format = format; 7083 scsi_ulto2b(0, data10->length); 7084 } else { 7085 data12 = (struct scsi_read_defect_data_hdr_12 *) 7086 ctsio->kern_data_ptr; 7087 data12->format = format; 7088 scsi_ulto2b(0, data12->generation); 7089 scsi_ulto4b(0, data12->length); 7090 } 7091 7092 ctl_set_success(ctsio); 7093 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7094 ctsio->be_move_done = ctl_config_move_done; 7095 ctl_datamove((union ctl_io *)ctsio); 7096 return (CTL_RETVAL_COMPLETE); 7097} 7098 7099int 7100ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7101{ 7102 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7103 struct ctl_lun *lun = CTL_LUN(ctsio); 7104 struct scsi_maintenance_in *cdb; 7105 int retval; 7106 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7107 int num_ha_groups, num_target_ports, shared_group; 7108 struct ctl_port *port; 7109 struct scsi_target_group_data *rtg_ptr; 7110 struct scsi_target_group_data_extended *rtg_ext_ptr; 7111 struct scsi_target_port_group_descriptor *tpg_desc; 7112 7113 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7114 7115 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7116 retval = CTL_RETVAL_COMPLETE; 7117 7118 switch (cdb->byte2 & STG_PDF_MASK) { 7119 case STG_PDF_LENGTH: 7120 ext = 0; 7121 break; 7122 case STG_PDF_EXTENDED: 7123 ext = 1; 7124 break; 7125 default: 7126 ctl_set_invalid_field(/*ctsio*/ ctsio, 7127 /*sks_valid*/ 1, 7128 /*command*/ 1, 7129 /*field*/ 2, 7130 /*bit_valid*/ 1, 7131 /*bit*/ 5); 7132 ctl_done((union ctl_io *)ctsio); 7133 return(retval); 7134 } 7135 7136 num_target_ports = 0; 7137 shared_group = (softc->is_single != 0); 7138 mtx_lock(&softc->ctl_lock); 7139 STAILQ_FOREACH(port, &softc->port_list, links) { 7140 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7141 continue; 7142 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7143 continue; 7144 num_target_ports++; 7145 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7146 shared_group = 1; 7147 } 7148 mtx_unlock(&softc->ctl_lock); 7149 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7150 7151 if (ext) 7152 total_len = sizeof(struct scsi_target_group_data_extended); 7153 else 7154 total_len = sizeof(struct scsi_target_group_data); 7155 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7156 (shared_group + num_ha_groups) + 7157 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7158 7159 alloc_len = scsi_4btoul(cdb->length); 7160 7161 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7162 ctsio->kern_sg_entries = 0; 7163 ctsio->kern_rel_offset = 0; 7164 ctsio->kern_data_len = min(total_len, alloc_len); 7165 ctsio->kern_total_len = ctsio->kern_data_len; 7166 7167 if (ext) { 7168 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7169 ctsio->kern_data_ptr; 7170 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7171 rtg_ext_ptr->format_type = 0x10; 7172 rtg_ext_ptr->implicit_transition_time = 0; 7173 tpg_desc = &rtg_ext_ptr->groups[0]; 7174 } else { 7175 rtg_ptr = (struct scsi_target_group_data *) 7176 ctsio->kern_data_ptr; 7177 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7178 tpg_desc = &rtg_ptr->groups[0]; 7179 } 7180 7181 mtx_lock(&softc->ctl_lock); 7182 pg = softc->port_min / softc->port_cnt; 7183 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7184 /* Some shelf is known to be primary. */ 7185 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7186 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7187 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7188 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7189 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7190 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7191 else 7192 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7193 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7194 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7195 } else { 7196 ts = os; 7197 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7198 } 7199 } else { 7200 /* No known primary shelf. */ 7201 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7202 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7203 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7204 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7205 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7206 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7207 } else { 7208 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7209 } 7210 } 7211 if (shared_group) { 7212 tpg_desc->pref_state = ts; 7213 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7214 TPG_U_SUP | TPG_T_SUP; 7215 scsi_ulto2b(1, tpg_desc->target_port_group); 7216 tpg_desc->status = TPG_IMPLICIT; 7217 pc = 0; 7218 STAILQ_FOREACH(port, &softc->port_list, links) { 7219 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7220 continue; 7221 if (!softc->is_single && 7222 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7223 continue; 7224 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7225 continue; 7226 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7227 relative_target_port_identifier); 7228 pc++; 7229 } 7230 tpg_desc->target_port_count = pc; 7231 tpg_desc = (struct scsi_target_port_group_descriptor *) 7232 &tpg_desc->descriptors[pc]; 7233 } 7234 for (g = 0; g < num_ha_groups; g++) { 7235 tpg_desc->pref_state = (g == pg) ? ts : os; 7236 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7237 TPG_U_SUP | TPG_T_SUP; 7238 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7239 tpg_desc->status = TPG_IMPLICIT; 7240 pc = 0; 7241 STAILQ_FOREACH(port, &softc->port_list, links) { 7242 if (port->targ_port < g * softc->port_cnt || 7243 port->targ_port >= (g + 1) * softc->port_cnt) 7244 continue; 7245 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7246 continue; 7247 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7248 continue; 7249 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7250 continue; 7251 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7252 relative_target_port_identifier); 7253 pc++; 7254 } 7255 tpg_desc->target_port_count = pc; 7256 tpg_desc = (struct scsi_target_port_group_descriptor *) 7257 &tpg_desc->descriptors[pc]; 7258 } 7259 mtx_unlock(&softc->ctl_lock); 7260 7261 ctl_set_success(ctsio); 7262 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7263 ctsio->be_move_done = ctl_config_move_done; 7264 ctl_datamove((union ctl_io *)ctsio); 7265 return(retval); 7266} 7267 7268int 7269ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7270{ 7271 struct ctl_lun *lun = CTL_LUN(ctsio); 7272 struct scsi_report_supported_opcodes *cdb; 7273 const struct ctl_cmd_entry *entry, *sentry; 7274 struct scsi_report_supported_opcodes_all *all; 7275 struct scsi_report_supported_opcodes_descr *descr; 7276 struct scsi_report_supported_opcodes_one *one; 7277 int retval; 7278 int alloc_len, total_len; 7279 int opcode, service_action, i, j, num; 7280 7281 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7282 7283 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7284 retval = CTL_RETVAL_COMPLETE; 7285 7286 opcode = cdb->requested_opcode; 7287 service_action = scsi_2btoul(cdb->requested_service_action); 7288 switch (cdb->options & RSO_OPTIONS_MASK) { 7289 case RSO_OPTIONS_ALL: 7290 num = 0; 7291 for (i = 0; i < 256; i++) { 7292 entry = &ctl_cmd_table[i]; 7293 if (entry->flags & CTL_CMD_FLAG_SA5) { 7294 for (j = 0; j < 32; j++) { 7295 sentry = &((const struct ctl_cmd_entry *) 7296 entry->execute)[j]; 7297 if (ctl_cmd_applicable( 7298 lun->be_lun->lun_type, sentry)) 7299 num++; 7300 } 7301 } else { 7302 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7303 entry)) 7304 num++; 7305 } 7306 } 7307 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7308 num * sizeof(struct scsi_report_supported_opcodes_descr); 7309 break; 7310 case RSO_OPTIONS_OC: 7311 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7312 ctl_set_invalid_field(/*ctsio*/ ctsio, 7313 /*sks_valid*/ 1, 7314 /*command*/ 1, 7315 /*field*/ 2, 7316 /*bit_valid*/ 1, 7317 /*bit*/ 2); 7318 ctl_done((union ctl_io *)ctsio); 7319 return (CTL_RETVAL_COMPLETE); 7320 } 7321 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7322 break; 7323 case RSO_OPTIONS_OC_SA: 7324 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7325 service_action >= 32) { 7326 ctl_set_invalid_field(/*ctsio*/ ctsio, 7327 /*sks_valid*/ 1, 7328 /*command*/ 1, 7329 /*field*/ 2, 7330 /*bit_valid*/ 1, 7331 /*bit*/ 2); 7332 ctl_done((union ctl_io *)ctsio); 7333 return (CTL_RETVAL_COMPLETE); 7334 } 7335 /* FALLTHROUGH */ 7336 case RSO_OPTIONS_OC_ASA: 7337 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7338 break; 7339 default: 7340 ctl_set_invalid_field(/*ctsio*/ ctsio, 7341 /*sks_valid*/ 1, 7342 /*command*/ 1, 7343 /*field*/ 2, 7344 /*bit_valid*/ 1, 7345 /*bit*/ 2); 7346 ctl_done((union ctl_io *)ctsio); 7347 return (CTL_RETVAL_COMPLETE); 7348 } 7349 7350 alloc_len = scsi_4btoul(cdb->length); 7351 7352 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7353 ctsio->kern_sg_entries = 0; 7354 ctsio->kern_rel_offset = 0; 7355 ctsio->kern_data_len = min(total_len, alloc_len); 7356 ctsio->kern_total_len = ctsio->kern_data_len; 7357 7358 switch (cdb->options & RSO_OPTIONS_MASK) { 7359 case RSO_OPTIONS_ALL: 7360 all = (struct scsi_report_supported_opcodes_all *) 7361 ctsio->kern_data_ptr; 7362 num = 0; 7363 for (i = 0; i < 256; i++) { 7364 entry = &ctl_cmd_table[i]; 7365 if (entry->flags & CTL_CMD_FLAG_SA5) { 7366 for (j = 0; j < 32; j++) { 7367 sentry = &((const struct ctl_cmd_entry *) 7368 entry->execute)[j]; 7369 if (!ctl_cmd_applicable( 7370 lun->be_lun->lun_type, sentry)) 7371 continue; 7372 descr = &all->descr[num++]; 7373 descr->opcode = i; 7374 scsi_ulto2b(j, descr->service_action); 7375 descr->flags = RSO_SERVACTV; 7376 scsi_ulto2b(sentry->length, 7377 descr->cdb_length); 7378 } 7379 } else { 7380 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7381 entry)) 7382 continue; 7383 descr = &all->descr[num++]; 7384 descr->opcode = i; 7385 scsi_ulto2b(0, descr->service_action); 7386 descr->flags = 0; 7387 scsi_ulto2b(entry->length, descr->cdb_length); 7388 } 7389 } 7390 scsi_ulto4b( 7391 num * sizeof(struct scsi_report_supported_opcodes_descr), 7392 all->length); 7393 break; 7394 case RSO_OPTIONS_OC: 7395 one = (struct scsi_report_supported_opcodes_one *) 7396 ctsio->kern_data_ptr; 7397 entry = &ctl_cmd_table[opcode]; 7398 goto fill_one; 7399 case RSO_OPTIONS_OC_SA: 7400 one = (struct scsi_report_supported_opcodes_one *) 7401 ctsio->kern_data_ptr; 7402 entry = &ctl_cmd_table[opcode]; 7403 entry = &((const struct ctl_cmd_entry *) 7404 entry->execute)[service_action]; 7405fill_one: 7406 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7407 one->support = 3; 7408 scsi_ulto2b(entry->length, one->cdb_length); 7409 one->cdb_usage[0] = opcode; 7410 memcpy(&one->cdb_usage[1], entry->usage, 7411 entry->length - 1); 7412 } else 7413 one->support = 1; 7414 break; 7415 case RSO_OPTIONS_OC_ASA: 7416 one = (struct scsi_report_supported_opcodes_one *) 7417 ctsio->kern_data_ptr; 7418 entry = &ctl_cmd_table[opcode]; 7419 if (entry->flags & CTL_CMD_FLAG_SA5) { 7420 entry = &((const struct ctl_cmd_entry *) 7421 entry->execute)[service_action]; 7422 } else if (service_action != 0) { 7423 one->support = 1; 7424 break; 7425 } 7426 goto fill_one; 7427 } 7428 7429 ctl_set_success(ctsio); 7430 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7431 ctsio->be_move_done = ctl_config_move_done; 7432 ctl_datamove((union ctl_io *)ctsio); 7433 return(retval); 7434} 7435 7436int 7437ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7438{ 7439 struct scsi_report_supported_tmf *cdb; 7440 struct scsi_report_supported_tmf_ext_data *data; 7441 int retval; 7442 int alloc_len, total_len; 7443 7444 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7445 7446 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7447 7448 retval = CTL_RETVAL_COMPLETE; 7449 7450 if (cdb->options & RST_REPD) 7451 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7452 else 7453 total_len = sizeof(struct scsi_report_supported_tmf_data); 7454 alloc_len = scsi_4btoul(cdb->length); 7455 7456 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7457 ctsio->kern_sg_entries = 0; 7458 ctsio->kern_rel_offset = 0; 7459 ctsio->kern_data_len = min(total_len, alloc_len); 7460 ctsio->kern_total_len = ctsio->kern_data_len; 7461 7462 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7463 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7464 RST_TRS; 7465 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7466 data->length = total_len - 4; 7467 7468 ctl_set_success(ctsio); 7469 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7470 ctsio->be_move_done = ctl_config_move_done; 7471 ctl_datamove((union ctl_io *)ctsio); 7472 return (retval); 7473} 7474 7475int 7476ctl_report_timestamp(struct ctl_scsiio *ctsio) 7477{ 7478 struct scsi_report_timestamp *cdb; 7479 struct scsi_report_timestamp_data *data; 7480 struct timeval tv; 7481 int64_t timestamp; 7482 int retval; 7483 int alloc_len, total_len; 7484 7485 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7486 7487 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7488 7489 retval = CTL_RETVAL_COMPLETE; 7490 7491 total_len = sizeof(struct scsi_report_timestamp_data); 7492 alloc_len = scsi_4btoul(cdb->length); 7493 7494 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7495 ctsio->kern_sg_entries = 0; 7496 ctsio->kern_rel_offset = 0; 7497 ctsio->kern_data_len = min(total_len, alloc_len); 7498 ctsio->kern_total_len = ctsio->kern_data_len; 7499 7500 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7501 scsi_ulto2b(sizeof(*data) - 2, data->length); 7502 data->origin = RTS_ORIG_OUTSIDE; 7503 getmicrotime(&tv); 7504 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7505 scsi_ulto4b(timestamp >> 16, data->timestamp); 7506 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7507 7508 ctl_set_success(ctsio); 7509 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7510 ctsio->be_move_done = ctl_config_move_done; 7511 ctl_datamove((union ctl_io *)ctsio); 7512 return (retval); 7513} 7514 7515int 7516ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7517{ 7518 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7519 struct ctl_lun *lun = CTL_LUN(ctsio); 7520 struct scsi_per_res_in *cdb; 7521 int alloc_len, total_len = 0; 7522 /* struct scsi_per_res_in_rsrv in_data; */ 7523 uint64_t key; 7524 7525 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7526 7527 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7528 7529 alloc_len = scsi_2btoul(cdb->length); 7530 7531retry: 7532 mtx_lock(&lun->lun_lock); 7533 switch (cdb->action) { 7534 case SPRI_RK: /* read keys */ 7535 total_len = sizeof(struct scsi_per_res_in_keys) + 7536 lun->pr_key_count * 7537 sizeof(struct scsi_per_res_key); 7538 break; 7539 case SPRI_RR: /* read reservation */ 7540 if (lun->flags & CTL_LUN_PR_RESERVED) 7541 total_len = sizeof(struct scsi_per_res_in_rsrv); 7542 else 7543 total_len = sizeof(struct scsi_per_res_in_header); 7544 break; 7545 case SPRI_RC: /* report capabilities */ 7546 total_len = sizeof(struct scsi_per_res_cap); 7547 break; 7548 case SPRI_RS: /* read full status */ 7549 total_len = sizeof(struct scsi_per_res_in_header) + 7550 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7551 lun->pr_key_count; 7552 break; 7553 default: 7554 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7555 } 7556 mtx_unlock(&lun->lun_lock); 7557 7558 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7559 ctsio->kern_rel_offset = 0; 7560 ctsio->kern_sg_entries = 0; 7561 ctsio->kern_data_len = min(total_len, alloc_len); 7562 ctsio->kern_total_len = ctsio->kern_data_len; 7563 7564 mtx_lock(&lun->lun_lock); 7565 switch (cdb->action) { 7566 case SPRI_RK: { // read keys 7567 struct scsi_per_res_in_keys *res_keys; 7568 int i, key_count; 7569 7570 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7571 7572 /* 7573 * We had to drop the lock to allocate our buffer, which 7574 * leaves time for someone to come in with another 7575 * persistent reservation. (That is unlikely, though, 7576 * since this should be the only persistent reservation 7577 * command active right now.) 7578 */ 7579 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7580 (lun->pr_key_count * 7581 sizeof(struct scsi_per_res_key)))){ 7582 mtx_unlock(&lun->lun_lock); 7583 free(ctsio->kern_data_ptr, M_CTL); 7584 printf("%s: reservation length changed, retrying\n", 7585 __func__); 7586 goto retry; 7587 } 7588 7589 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7590 7591 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7592 lun->pr_key_count, res_keys->header.length); 7593 7594 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7595 if ((key = ctl_get_prkey(lun, i)) == 0) 7596 continue; 7597 7598 /* 7599 * We used lun->pr_key_count to calculate the 7600 * size to allocate. If it turns out the number of 7601 * initiators with the registered flag set is 7602 * larger than that (i.e. they haven't been kept in 7603 * sync), we've got a problem. 7604 */ 7605 if (key_count >= lun->pr_key_count) { 7606 key_count++; 7607 continue; 7608 } 7609 scsi_u64to8b(key, res_keys->keys[key_count].key); 7610 key_count++; 7611 } 7612 break; 7613 } 7614 case SPRI_RR: { // read reservation 7615 struct scsi_per_res_in_rsrv *res; 7616 int tmp_len, header_only; 7617 7618 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7619 7620 scsi_ulto4b(lun->pr_generation, res->header.generation); 7621 7622 if (lun->flags & CTL_LUN_PR_RESERVED) 7623 { 7624 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7625 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7626 res->header.length); 7627 header_only = 0; 7628 } else { 7629 tmp_len = sizeof(struct scsi_per_res_in_header); 7630 scsi_ulto4b(0, res->header.length); 7631 header_only = 1; 7632 } 7633 7634 /* 7635 * We had to drop the lock to allocate our buffer, which 7636 * leaves time for someone to come in with another 7637 * persistent reservation. (That is unlikely, though, 7638 * since this should be the only persistent reservation 7639 * command active right now.) 7640 */ 7641 if (tmp_len != total_len) { 7642 mtx_unlock(&lun->lun_lock); 7643 free(ctsio->kern_data_ptr, M_CTL); 7644 printf("%s: reservation status changed, retrying\n", 7645 __func__); 7646 goto retry; 7647 } 7648 7649 /* 7650 * No reservation held, so we're done. 7651 */ 7652 if (header_only != 0) 7653 break; 7654 7655 /* 7656 * If the registration is an All Registrants type, the key 7657 * is 0, since it doesn't really matter. 7658 */ 7659 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7660 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7661 res->data.reservation); 7662 } 7663 res->data.scopetype = lun->pr_res_type; 7664 break; 7665 } 7666 case SPRI_RC: //report capabilities 7667 { 7668 struct scsi_per_res_cap *res_cap; 7669 uint16_t type_mask; 7670 7671 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7672 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7673 res_cap->flags1 = SPRI_CRH; 7674 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7675 type_mask = SPRI_TM_WR_EX_AR | 7676 SPRI_TM_EX_AC_RO | 7677 SPRI_TM_WR_EX_RO | 7678 SPRI_TM_EX_AC | 7679 SPRI_TM_WR_EX | 7680 SPRI_TM_EX_AC_AR; 7681 scsi_ulto2b(type_mask, res_cap->type_mask); 7682 break; 7683 } 7684 case SPRI_RS: { // read full status 7685 struct scsi_per_res_in_full *res_status; 7686 struct scsi_per_res_in_full_desc *res_desc; 7687 struct ctl_port *port; 7688 int i, len; 7689 7690 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7691 7692 /* 7693 * We had to drop the lock to allocate our buffer, which 7694 * leaves time for someone to come in with another 7695 * persistent reservation. (That is unlikely, though, 7696 * since this should be the only persistent reservation 7697 * command active right now.) 7698 */ 7699 if (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 mtx_unlock(&lun->lun_lock); 7703 free(ctsio->kern_data_ptr, M_CTL); 7704 printf("%s: reservation length changed, retrying\n", 7705 __func__); 7706 goto retry; 7707 } 7708 7709 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7710 7711 res_desc = &res_status->desc[0]; 7712 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7713 if ((key = ctl_get_prkey(lun, i)) == 0) 7714 continue; 7715 7716 scsi_u64to8b(key, res_desc->res_key.key); 7717 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7718 (lun->pr_res_idx == i || 7719 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7720 res_desc->flags = SPRI_FULL_R_HOLDER; 7721 res_desc->scopetype = lun->pr_res_type; 7722 } 7723 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7724 res_desc->rel_trgt_port_id); 7725 len = 0; 7726 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7727 if (port != NULL) 7728 len = ctl_create_iid(port, 7729 i % CTL_MAX_INIT_PER_PORT, 7730 res_desc->transport_id); 7731 scsi_ulto4b(len, res_desc->additional_length); 7732 res_desc = (struct scsi_per_res_in_full_desc *) 7733 &res_desc->transport_id[len]; 7734 } 7735 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7736 res_status->header.length); 7737 break; 7738 } 7739 default: 7740 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7741 } 7742 mtx_unlock(&lun->lun_lock); 7743 7744 ctl_set_success(ctsio); 7745 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7746 ctsio->be_move_done = ctl_config_move_done; 7747 ctl_datamove((union ctl_io *)ctsio); 7748 return (CTL_RETVAL_COMPLETE); 7749} 7750 7751/* 7752 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7753 * it should return. 7754 */ 7755static int 7756ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7757 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7758 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7759 struct scsi_per_res_out_parms* param) 7760{ 7761 union ctl_ha_msg persis_io; 7762 int i; 7763 7764 mtx_lock(&lun->lun_lock); 7765 if (sa_res_key == 0) { 7766 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7767 /* validate scope and type */ 7768 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7769 SPR_LU_SCOPE) { 7770 mtx_unlock(&lun->lun_lock); 7771 ctl_set_invalid_field(/*ctsio*/ ctsio, 7772 /*sks_valid*/ 1, 7773 /*command*/ 1, 7774 /*field*/ 2, 7775 /*bit_valid*/ 1, 7776 /*bit*/ 4); 7777 ctl_done((union ctl_io *)ctsio); 7778 return (1); 7779 } 7780 7781 if (type>8 || type==2 || type==4 || type==0) { 7782 mtx_unlock(&lun->lun_lock); 7783 ctl_set_invalid_field(/*ctsio*/ ctsio, 7784 /*sks_valid*/ 1, 7785 /*command*/ 1, 7786 /*field*/ 2, 7787 /*bit_valid*/ 1, 7788 /*bit*/ 0); 7789 ctl_done((union ctl_io *)ctsio); 7790 return (1); 7791 } 7792 7793 /* 7794 * Unregister everybody else and build UA for 7795 * them 7796 */ 7797 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7798 if (i == residx || ctl_get_prkey(lun, i) == 0) 7799 continue; 7800 7801 ctl_clr_prkey(lun, i); 7802 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7803 } 7804 lun->pr_key_count = 1; 7805 lun->pr_res_type = type; 7806 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7807 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7808 lun->pr_res_idx = residx; 7809 lun->pr_generation++; 7810 mtx_unlock(&lun->lun_lock); 7811 7812 /* send msg to other side */ 7813 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7814 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7815 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7816 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7817 persis_io.pr.pr_info.res_type = type; 7818 memcpy(persis_io.pr.pr_info.sa_res_key, 7819 param->serv_act_res_key, 7820 sizeof(param->serv_act_res_key)); 7821 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7822 sizeof(persis_io.pr), M_WAITOK); 7823 } else { 7824 /* not all registrants */ 7825 mtx_unlock(&lun->lun_lock); 7826 free(ctsio->kern_data_ptr, M_CTL); 7827 ctl_set_invalid_field(ctsio, 7828 /*sks_valid*/ 1, 7829 /*command*/ 0, 7830 /*field*/ 8, 7831 /*bit_valid*/ 0, 7832 /*bit*/ 0); 7833 ctl_done((union ctl_io *)ctsio); 7834 return (1); 7835 } 7836 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7837 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7838 int found = 0; 7839 7840 if (res_key == sa_res_key) { 7841 /* special case */ 7842 /* 7843 * The spec implies this is not good but doesn't 7844 * say what to do. There are two choices either 7845 * generate a res conflict or check condition 7846 * with illegal field in parameter data. Since 7847 * that is what is done when the sa_res_key is 7848 * zero I'll take that approach since this has 7849 * to do with the sa_res_key. 7850 */ 7851 mtx_unlock(&lun->lun_lock); 7852 free(ctsio->kern_data_ptr, M_CTL); 7853 ctl_set_invalid_field(ctsio, 7854 /*sks_valid*/ 1, 7855 /*command*/ 0, 7856 /*field*/ 8, 7857 /*bit_valid*/ 0, 7858 /*bit*/ 0); 7859 ctl_done((union ctl_io *)ctsio); 7860 return (1); 7861 } 7862 7863 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7864 if (ctl_get_prkey(lun, i) != sa_res_key) 7865 continue; 7866 7867 found = 1; 7868 ctl_clr_prkey(lun, i); 7869 lun->pr_key_count--; 7870 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7871 } 7872 if (!found) { 7873 mtx_unlock(&lun->lun_lock); 7874 free(ctsio->kern_data_ptr, M_CTL); 7875 ctl_set_reservation_conflict(ctsio); 7876 ctl_done((union ctl_io *)ctsio); 7877 return (CTL_RETVAL_COMPLETE); 7878 } 7879 lun->pr_generation++; 7880 mtx_unlock(&lun->lun_lock); 7881 7882 /* send msg to other side */ 7883 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7884 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7885 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7886 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7887 persis_io.pr.pr_info.res_type = type; 7888 memcpy(persis_io.pr.pr_info.sa_res_key, 7889 param->serv_act_res_key, 7890 sizeof(param->serv_act_res_key)); 7891 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7892 sizeof(persis_io.pr), M_WAITOK); 7893 } else { 7894 /* Reserved but not all registrants */ 7895 /* sa_res_key is res holder */ 7896 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7897 /* validate scope and type */ 7898 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7899 SPR_LU_SCOPE) { 7900 mtx_unlock(&lun->lun_lock); 7901 ctl_set_invalid_field(/*ctsio*/ ctsio, 7902 /*sks_valid*/ 1, 7903 /*command*/ 1, 7904 /*field*/ 2, 7905 /*bit_valid*/ 1, 7906 /*bit*/ 4); 7907 ctl_done((union ctl_io *)ctsio); 7908 return (1); 7909 } 7910 7911 if (type>8 || type==2 || type==4 || type==0) { 7912 mtx_unlock(&lun->lun_lock); 7913 ctl_set_invalid_field(/*ctsio*/ ctsio, 7914 /*sks_valid*/ 1, 7915 /*command*/ 1, 7916 /*field*/ 2, 7917 /*bit_valid*/ 1, 7918 /*bit*/ 0); 7919 ctl_done((union ctl_io *)ctsio); 7920 return (1); 7921 } 7922 7923 /* 7924 * Do the following: 7925 * if sa_res_key != res_key remove all 7926 * registrants w/sa_res_key and generate UA 7927 * for these registrants(Registrations 7928 * Preempted) if it wasn't an exclusive 7929 * reservation generate UA(Reservations 7930 * Preempted) for all other registered nexuses 7931 * if the type has changed. Establish the new 7932 * reservation and holder. If res_key and 7933 * sa_res_key are the same do the above 7934 * except don't unregister the res holder. 7935 */ 7936 7937 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7938 if (i == residx || ctl_get_prkey(lun, i) == 0) 7939 continue; 7940 7941 if (sa_res_key == ctl_get_prkey(lun, i)) { 7942 ctl_clr_prkey(lun, i); 7943 lun->pr_key_count--; 7944 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7945 } else if (type != lun->pr_res_type && 7946 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 7947 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 7948 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7949 } 7950 } 7951 lun->pr_res_type = type; 7952 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7953 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7954 lun->pr_res_idx = residx; 7955 else 7956 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7957 lun->pr_generation++; 7958 mtx_unlock(&lun->lun_lock); 7959 7960 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7961 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7962 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7963 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7964 persis_io.pr.pr_info.res_type = type; 7965 memcpy(persis_io.pr.pr_info.sa_res_key, 7966 param->serv_act_res_key, 7967 sizeof(param->serv_act_res_key)); 7968 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7969 sizeof(persis_io.pr), M_WAITOK); 7970 } else { 7971 /* 7972 * sa_res_key is not the res holder just 7973 * remove registrants 7974 */ 7975 int found=0; 7976 7977 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7978 if (sa_res_key != ctl_get_prkey(lun, i)) 7979 continue; 7980 7981 found = 1; 7982 ctl_clr_prkey(lun, i); 7983 lun->pr_key_count--; 7984 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7985 } 7986 7987 if (!found) { 7988 mtx_unlock(&lun->lun_lock); 7989 free(ctsio->kern_data_ptr, M_CTL); 7990 ctl_set_reservation_conflict(ctsio); 7991 ctl_done((union ctl_io *)ctsio); 7992 return (1); 7993 } 7994 lun->pr_generation++; 7995 mtx_unlock(&lun->lun_lock); 7996 7997 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7998 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7999 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8000 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8001 persis_io.pr.pr_info.res_type = type; 8002 memcpy(persis_io.pr.pr_info.sa_res_key, 8003 param->serv_act_res_key, 8004 sizeof(param->serv_act_res_key)); 8005 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8006 sizeof(persis_io.pr), M_WAITOK); 8007 } 8008 } 8009 return (0); 8010} 8011 8012static void 8013ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8014{ 8015 uint64_t sa_res_key; 8016 int i; 8017 8018 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8019 8020 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8021 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8022 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8023 if (sa_res_key == 0) { 8024 /* 8025 * Unregister everybody else and build UA for 8026 * them 8027 */ 8028 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8029 if (i == msg->pr.pr_info.residx || 8030 ctl_get_prkey(lun, i) == 0) 8031 continue; 8032 8033 ctl_clr_prkey(lun, i); 8034 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8035 } 8036 8037 lun->pr_key_count = 1; 8038 lun->pr_res_type = msg->pr.pr_info.res_type; 8039 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8040 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8041 lun->pr_res_idx = msg->pr.pr_info.residx; 8042 } else { 8043 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8044 if (sa_res_key == ctl_get_prkey(lun, i)) 8045 continue; 8046 8047 ctl_clr_prkey(lun, i); 8048 lun->pr_key_count--; 8049 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8050 } 8051 } 8052 } else { 8053 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8054 if (i == msg->pr.pr_info.residx || 8055 ctl_get_prkey(lun, i) == 0) 8056 continue; 8057 8058 if (sa_res_key == ctl_get_prkey(lun, i)) { 8059 ctl_clr_prkey(lun, i); 8060 lun->pr_key_count--; 8061 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8062 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8063 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8064 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8065 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8066 } 8067 } 8068 lun->pr_res_type = msg->pr.pr_info.res_type; 8069 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8070 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8071 lun->pr_res_idx = msg->pr.pr_info.residx; 8072 else 8073 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8074 } 8075 lun->pr_generation++; 8076 8077} 8078 8079 8080int 8081ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8082{ 8083 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8084 struct ctl_lun *lun = CTL_LUN(ctsio); 8085 int retval; 8086 u_int32_t param_len; 8087 struct scsi_per_res_out *cdb; 8088 struct scsi_per_res_out_parms* param; 8089 uint32_t residx; 8090 uint64_t res_key, sa_res_key, key; 8091 uint8_t type; 8092 union ctl_ha_msg persis_io; 8093 int i; 8094 8095 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8096 8097 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8098 retval = CTL_RETVAL_COMPLETE; 8099 8100 /* 8101 * We only support whole-LUN scope. The scope & type are ignored for 8102 * register, register and ignore existing key and clear. 8103 * We sometimes ignore scope and type on preempts too!! 8104 * Verify reservation type here as well. 8105 */ 8106 type = cdb->scope_type & SPR_TYPE_MASK; 8107 if ((cdb->action == SPRO_RESERVE) 8108 || (cdb->action == SPRO_RELEASE)) { 8109 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8110 ctl_set_invalid_field(/*ctsio*/ ctsio, 8111 /*sks_valid*/ 1, 8112 /*command*/ 1, 8113 /*field*/ 2, 8114 /*bit_valid*/ 1, 8115 /*bit*/ 4); 8116 ctl_done((union ctl_io *)ctsio); 8117 return (CTL_RETVAL_COMPLETE); 8118 } 8119 8120 if (type>8 || type==2 || type==4 || type==0) { 8121 ctl_set_invalid_field(/*ctsio*/ ctsio, 8122 /*sks_valid*/ 1, 8123 /*command*/ 1, 8124 /*field*/ 2, 8125 /*bit_valid*/ 1, 8126 /*bit*/ 0); 8127 ctl_done((union ctl_io *)ctsio); 8128 return (CTL_RETVAL_COMPLETE); 8129 } 8130 } 8131 8132 param_len = scsi_4btoul(cdb->length); 8133 8134 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8135 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8136 ctsio->kern_data_len = param_len; 8137 ctsio->kern_total_len = param_len; 8138 ctsio->kern_rel_offset = 0; 8139 ctsio->kern_sg_entries = 0; 8140 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8141 ctsio->be_move_done = ctl_config_move_done; 8142 ctl_datamove((union ctl_io *)ctsio); 8143 8144 return (CTL_RETVAL_COMPLETE); 8145 } 8146 8147 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8148 8149 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8150 res_key = scsi_8btou64(param->res_key.key); 8151 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8152 8153 /* 8154 * Validate the reservation key here except for SPRO_REG_IGNO 8155 * This must be done for all other service actions 8156 */ 8157 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8158 mtx_lock(&lun->lun_lock); 8159 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8160 if (res_key != key) { 8161 /* 8162 * The current key passed in doesn't match 8163 * the one the initiator previously 8164 * registered. 8165 */ 8166 mtx_unlock(&lun->lun_lock); 8167 free(ctsio->kern_data_ptr, M_CTL); 8168 ctl_set_reservation_conflict(ctsio); 8169 ctl_done((union ctl_io *)ctsio); 8170 return (CTL_RETVAL_COMPLETE); 8171 } 8172 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8173 /* 8174 * We are not registered 8175 */ 8176 mtx_unlock(&lun->lun_lock); 8177 free(ctsio->kern_data_ptr, M_CTL); 8178 ctl_set_reservation_conflict(ctsio); 8179 ctl_done((union ctl_io *)ctsio); 8180 return (CTL_RETVAL_COMPLETE); 8181 } else if (res_key != 0) { 8182 /* 8183 * We are not registered and trying to register but 8184 * the register key isn't zero. 8185 */ 8186 mtx_unlock(&lun->lun_lock); 8187 free(ctsio->kern_data_ptr, M_CTL); 8188 ctl_set_reservation_conflict(ctsio); 8189 ctl_done((union ctl_io *)ctsio); 8190 return (CTL_RETVAL_COMPLETE); 8191 } 8192 mtx_unlock(&lun->lun_lock); 8193 } 8194 8195 switch (cdb->action & SPRO_ACTION_MASK) { 8196 case SPRO_REGISTER: 8197 case SPRO_REG_IGNO: { 8198 8199#if 0 8200 printf("Registration received\n"); 8201#endif 8202 8203 /* 8204 * We don't support any of these options, as we report in 8205 * the read capabilities request (see 8206 * ctl_persistent_reserve_in(), above). 8207 */ 8208 if ((param->flags & SPR_SPEC_I_PT) 8209 || (param->flags & SPR_ALL_TG_PT) 8210 || (param->flags & SPR_APTPL)) { 8211 int bit_ptr; 8212 8213 if (param->flags & SPR_APTPL) 8214 bit_ptr = 0; 8215 else if (param->flags & SPR_ALL_TG_PT) 8216 bit_ptr = 2; 8217 else /* SPR_SPEC_I_PT */ 8218 bit_ptr = 3; 8219 8220 free(ctsio->kern_data_ptr, M_CTL); 8221 ctl_set_invalid_field(ctsio, 8222 /*sks_valid*/ 1, 8223 /*command*/ 0, 8224 /*field*/ 20, 8225 /*bit_valid*/ 1, 8226 /*bit*/ bit_ptr); 8227 ctl_done((union ctl_io *)ctsio); 8228 return (CTL_RETVAL_COMPLETE); 8229 } 8230 8231 mtx_lock(&lun->lun_lock); 8232 8233 /* 8234 * The initiator wants to clear the 8235 * key/unregister. 8236 */ 8237 if (sa_res_key == 0) { 8238 if ((res_key == 0 8239 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8240 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8241 && ctl_get_prkey(lun, residx) == 0)) { 8242 mtx_unlock(&lun->lun_lock); 8243 goto done; 8244 } 8245 8246 ctl_clr_prkey(lun, residx); 8247 lun->pr_key_count--; 8248 8249 if (residx == lun->pr_res_idx) { 8250 lun->flags &= ~CTL_LUN_PR_RESERVED; 8251 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8252 8253 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8254 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8255 lun->pr_key_count) { 8256 /* 8257 * If the reservation is a registrants 8258 * only type we need to generate a UA 8259 * for other registered inits. The 8260 * sense code should be RESERVATIONS 8261 * RELEASED 8262 */ 8263 8264 for (i = softc->init_min; i < softc->init_max; i++){ 8265 if (ctl_get_prkey(lun, i) == 0) 8266 continue; 8267 ctl_est_ua(lun, i, 8268 CTL_UA_RES_RELEASE); 8269 } 8270 } 8271 lun->pr_res_type = 0; 8272 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8273 if (lun->pr_key_count==0) { 8274 lun->flags &= ~CTL_LUN_PR_RESERVED; 8275 lun->pr_res_type = 0; 8276 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8277 } 8278 } 8279 lun->pr_generation++; 8280 mtx_unlock(&lun->lun_lock); 8281 8282 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8283 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8284 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8285 persis_io.pr.pr_info.residx = residx; 8286 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8287 sizeof(persis_io.pr), M_WAITOK); 8288 } else /* sa_res_key != 0 */ { 8289 8290 /* 8291 * If we aren't registered currently then increment 8292 * the key count and set the registered flag. 8293 */ 8294 ctl_alloc_prkey(lun, residx); 8295 if (ctl_get_prkey(lun, residx) == 0) 8296 lun->pr_key_count++; 8297 ctl_set_prkey(lun, residx, sa_res_key); 8298 lun->pr_generation++; 8299 mtx_unlock(&lun->lun_lock); 8300 8301 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8302 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8303 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8304 persis_io.pr.pr_info.residx = residx; 8305 memcpy(persis_io.pr.pr_info.sa_res_key, 8306 param->serv_act_res_key, 8307 sizeof(param->serv_act_res_key)); 8308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8309 sizeof(persis_io.pr), M_WAITOK); 8310 } 8311 8312 break; 8313 } 8314 case SPRO_RESERVE: 8315#if 0 8316 printf("Reserve executed type %d\n", type); 8317#endif 8318 mtx_lock(&lun->lun_lock); 8319 if (lun->flags & CTL_LUN_PR_RESERVED) { 8320 /* 8321 * if this isn't the reservation holder and it's 8322 * not a "all registrants" type or if the type is 8323 * different then we have a conflict 8324 */ 8325 if ((lun->pr_res_idx != residx 8326 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8327 || lun->pr_res_type != type) { 8328 mtx_unlock(&lun->lun_lock); 8329 free(ctsio->kern_data_ptr, M_CTL); 8330 ctl_set_reservation_conflict(ctsio); 8331 ctl_done((union ctl_io *)ctsio); 8332 return (CTL_RETVAL_COMPLETE); 8333 } 8334 mtx_unlock(&lun->lun_lock); 8335 } else /* create a reservation */ { 8336 /* 8337 * If it's not an "all registrants" type record 8338 * reservation holder 8339 */ 8340 if (type != SPR_TYPE_WR_EX_AR 8341 && type != SPR_TYPE_EX_AC_AR) 8342 lun->pr_res_idx = residx; /* Res holder */ 8343 else 8344 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8345 8346 lun->flags |= CTL_LUN_PR_RESERVED; 8347 lun->pr_res_type = type; 8348 8349 mtx_unlock(&lun->lun_lock); 8350 8351 /* send msg to other side */ 8352 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8353 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8354 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8355 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8356 persis_io.pr.pr_info.res_type = type; 8357 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8358 sizeof(persis_io.pr), M_WAITOK); 8359 } 8360 break; 8361 8362 case SPRO_RELEASE: 8363 mtx_lock(&lun->lun_lock); 8364 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8365 /* No reservation exists return good status */ 8366 mtx_unlock(&lun->lun_lock); 8367 goto done; 8368 } 8369 /* 8370 * Is this nexus a reservation holder? 8371 */ 8372 if (lun->pr_res_idx != residx 8373 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8374 /* 8375 * not a res holder return good status but 8376 * do nothing 8377 */ 8378 mtx_unlock(&lun->lun_lock); 8379 goto done; 8380 } 8381 8382 if (lun->pr_res_type != type) { 8383 mtx_unlock(&lun->lun_lock); 8384 free(ctsio->kern_data_ptr, M_CTL); 8385 ctl_set_illegal_pr_release(ctsio); 8386 ctl_done((union ctl_io *)ctsio); 8387 return (CTL_RETVAL_COMPLETE); 8388 } 8389 8390 /* okay to release */ 8391 lun->flags &= ~CTL_LUN_PR_RESERVED; 8392 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8393 lun->pr_res_type = 0; 8394 8395 /* 8396 * If this isn't an exclusive access reservation and NUAR 8397 * is not set, generate UA for all other registrants. 8398 */ 8399 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8400 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8401 for (i = softc->init_min; i < softc->init_max; i++) { 8402 if (i == residx || ctl_get_prkey(lun, i) == 0) 8403 continue; 8404 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8405 } 8406 } 8407 mtx_unlock(&lun->lun_lock); 8408 8409 /* Send msg to other side */ 8410 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8411 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8412 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8413 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8414 sizeof(persis_io.pr), M_WAITOK); 8415 break; 8416 8417 case SPRO_CLEAR: 8418 /* send msg to other side */ 8419 8420 mtx_lock(&lun->lun_lock); 8421 lun->flags &= ~CTL_LUN_PR_RESERVED; 8422 lun->pr_res_type = 0; 8423 lun->pr_key_count = 0; 8424 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8425 8426 ctl_clr_prkey(lun, residx); 8427 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8428 if (ctl_get_prkey(lun, i) != 0) { 8429 ctl_clr_prkey(lun, i); 8430 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8431 } 8432 lun->pr_generation++; 8433 mtx_unlock(&lun->lun_lock); 8434 8435 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8436 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8437 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8438 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8439 sizeof(persis_io.pr), M_WAITOK); 8440 break; 8441 8442 case SPRO_PREEMPT: 8443 case SPRO_PRE_ABO: { 8444 int nretval; 8445 8446 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8447 residx, ctsio, cdb, param); 8448 if (nretval != 0) 8449 return (CTL_RETVAL_COMPLETE); 8450 break; 8451 } 8452 default: 8453 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8454 } 8455 8456done: 8457 free(ctsio->kern_data_ptr, M_CTL); 8458 ctl_set_success(ctsio); 8459 ctl_done((union ctl_io *)ctsio); 8460 8461 return (retval); 8462} 8463 8464/* 8465 * This routine is for handling a message from the other SC pertaining to 8466 * persistent reserve out. All the error checking will have been done 8467 * so only perorming the action need be done here to keep the two 8468 * in sync. 8469 */ 8470static void 8471ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8472{ 8473 struct ctl_softc *softc = CTL_SOFTC(io); 8474 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8475 struct ctl_lun *lun; 8476 int i; 8477 uint32_t residx, targ_lun; 8478 8479 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8480 mtx_lock(&softc->ctl_lock); 8481 if (targ_lun >= CTL_MAX_LUNS || 8482 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8483 mtx_unlock(&softc->ctl_lock); 8484 return; 8485 } 8486 mtx_lock(&lun->lun_lock); 8487 mtx_unlock(&softc->ctl_lock); 8488 if (lun->flags & CTL_LUN_DISABLED) { 8489 mtx_unlock(&lun->lun_lock); 8490 return; 8491 } 8492 residx = ctl_get_initindex(&msg->hdr.nexus); 8493 switch(msg->pr.pr_info.action) { 8494 case CTL_PR_REG_KEY: 8495 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8496 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8497 lun->pr_key_count++; 8498 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8499 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8500 lun->pr_generation++; 8501 break; 8502 8503 case CTL_PR_UNREG_KEY: 8504 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8505 lun->pr_key_count--; 8506 8507 /* XXX Need to see if the reservation has been released */ 8508 /* if so do we need to generate UA? */ 8509 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8510 lun->flags &= ~CTL_LUN_PR_RESERVED; 8511 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8512 8513 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8514 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8515 lun->pr_key_count) { 8516 /* 8517 * If the reservation is a registrants 8518 * only type we need to generate a UA 8519 * for other registered inits. The 8520 * sense code should be RESERVATIONS 8521 * RELEASED 8522 */ 8523 8524 for (i = softc->init_min; i < softc->init_max; i++) { 8525 if (ctl_get_prkey(lun, i) == 0) 8526 continue; 8527 8528 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8529 } 8530 } 8531 lun->pr_res_type = 0; 8532 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8533 if (lun->pr_key_count==0) { 8534 lun->flags &= ~CTL_LUN_PR_RESERVED; 8535 lun->pr_res_type = 0; 8536 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8537 } 8538 } 8539 lun->pr_generation++; 8540 break; 8541 8542 case CTL_PR_RESERVE: 8543 lun->flags |= CTL_LUN_PR_RESERVED; 8544 lun->pr_res_type = msg->pr.pr_info.res_type; 8545 lun->pr_res_idx = msg->pr.pr_info.residx; 8546 8547 break; 8548 8549 case CTL_PR_RELEASE: 8550 /* 8551 * If this isn't an exclusive access reservation and NUAR 8552 * is not set, generate UA for all other registrants. 8553 */ 8554 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8555 lun->pr_res_type != SPR_TYPE_WR_EX && 8556 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8557 for (i = softc->init_min; i < softc->init_max; i++) 8558 if (i == residx || ctl_get_prkey(lun, i) == 0) 8559 continue; 8560 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8561 } 8562 8563 lun->flags &= ~CTL_LUN_PR_RESERVED; 8564 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8565 lun->pr_res_type = 0; 8566 break; 8567 8568 case CTL_PR_PREEMPT: 8569 ctl_pro_preempt_other(lun, msg); 8570 break; 8571 case CTL_PR_CLEAR: 8572 lun->flags &= ~CTL_LUN_PR_RESERVED; 8573 lun->pr_res_type = 0; 8574 lun->pr_key_count = 0; 8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8576 8577 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8578 if (ctl_get_prkey(lun, i) == 0) 8579 continue; 8580 ctl_clr_prkey(lun, i); 8581 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8582 } 8583 lun->pr_generation++; 8584 break; 8585 } 8586 8587 mtx_unlock(&lun->lun_lock); 8588} 8589 8590int 8591ctl_read_write(struct ctl_scsiio *ctsio) 8592{ 8593 struct ctl_lun *lun = CTL_LUN(ctsio); 8594 struct ctl_lba_len_flags *lbalen; 8595 uint64_t lba; 8596 uint32_t num_blocks; 8597 int flags, retval; 8598 int isread; 8599 8600 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8601 8602 flags = 0; 8603 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8604 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8605 switch (ctsio->cdb[0]) { 8606 case READ_6: 8607 case WRITE_6: { 8608 struct scsi_rw_6 *cdb; 8609 8610 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8611 8612 lba = scsi_3btoul(cdb->addr); 8613 /* only 5 bits are valid in the most significant address byte */ 8614 lba &= 0x1fffff; 8615 num_blocks = cdb->length; 8616 /* 8617 * This is correct according to SBC-2. 8618 */ 8619 if (num_blocks == 0) 8620 num_blocks = 256; 8621 break; 8622 } 8623 case READ_10: 8624 case WRITE_10: { 8625 struct scsi_rw_10 *cdb; 8626 8627 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8628 if (cdb->byte2 & SRW10_FUA) 8629 flags |= CTL_LLF_FUA; 8630 if (cdb->byte2 & SRW10_DPO) 8631 flags |= CTL_LLF_DPO; 8632 lba = scsi_4btoul(cdb->addr); 8633 num_blocks = scsi_2btoul(cdb->length); 8634 break; 8635 } 8636 case WRITE_VERIFY_10: { 8637 struct scsi_write_verify_10 *cdb; 8638 8639 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8640 flags |= CTL_LLF_FUA; 8641 if (cdb->byte2 & SWV_DPO) 8642 flags |= CTL_LLF_DPO; 8643 lba = scsi_4btoul(cdb->addr); 8644 num_blocks = scsi_2btoul(cdb->length); 8645 break; 8646 } 8647 case READ_12: 8648 case WRITE_12: { 8649 struct scsi_rw_12 *cdb; 8650 8651 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8652 if (cdb->byte2 & SRW12_FUA) 8653 flags |= CTL_LLF_FUA; 8654 if (cdb->byte2 & SRW12_DPO) 8655 flags |= CTL_LLF_DPO; 8656 lba = scsi_4btoul(cdb->addr); 8657 num_blocks = scsi_4btoul(cdb->length); 8658 break; 8659 } 8660 case WRITE_VERIFY_12: { 8661 struct scsi_write_verify_12 *cdb; 8662 8663 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8664 flags |= CTL_LLF_FUA; 8665 if (cdb->byte2 & SWV_DPO) 8666 flags |= CTL_LLF_DPO; 8667 lba = scsi_4btoul(cdb->addr); 8668 num_blocks = scsi_4btoul(cdb->length); 8669 break; 8670 } 8671 case READ_16: 8672 case WRITE_16: { 8673 struct scsi_rw_16 *cdb; 8674 8675 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8676 if (cdb->byte2 & SRW12_FUA) 8677 flags |= CTL_LLF_FUA; 8678 if (cdb->byte2 & SRW12_DPO) 8679 flags |= CTL_LLF_DPO; 8680 lba = scsi_8btou64(cdb->addr); 8681 num_blocks = scsi_4btoul(cdb->length); 8682 break; 8683 } 8684 case WRITE_ATOMIC_16: { 8685 struct scsi_write_atomic_16 *cdb; 8686 8687 if (lun->be_lun->atomicblock == 0) { 8688 ctl_set_invalid_opcode(ctsio); 8689 ctl_done((union ctl_io *)ctsio); 8690 return (CTL_RETVAL_COMPLETE); 8691 } 8692 8693 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8694 if (cdb->byte2 & SRW12_FUA) 8695 flags |= CTL_LLF_FUA; 8696 if (cdb->byte2 & SRW12_DPO) 8697 flags |= CTL_LLF_DPO; 8698 lba = scsi_8btou64(cdb->addr); 8699 num_blocks = scsi_2btoul(cdb->length); 8700 if (num_blocks > lun->be_lun->atomicblock) { 8701 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8702 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8703 /*bit*/ 0); 8704 ctl_done((union ctl_io *)ctsio); 8705 return (CTL_RETVAL_COMPLETE); 8706 } 8707 break; 8708 } 8709 case WRITE_VERIFY_16: { 8710 struct scsi_write_verify_16 *cdb; 8711 8712 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8713 flags |= CTL_LLF_FUA; 8714 if (cdb->byte2 & SWV_DPO) 8715 flags |= CTL_LLF_DPO; 8716 lba = scsi_8btou64(cdb->addr); 8717 num_blocks = scsi_4btoul(cdb->length); 8718 break; 8719 } 8720 default: 8721 /* 8722 * We got a command we don't support. This shouldn't 8723 * happen, commands should be filtered out above us. 8724 */ 8725 ctl_set_invalid_opcode(ctsio); 8726 ctl_done((union ctl_io *)ctsio); 8727 8728 return (CTL_RETVAL_COMPLETE); 8729 break; /* NOTREACHED */ 8730 } 8731 8732 /* 8733 * The first check is to make sure we're in bounds, the second 8734 * check is to catch wrap-around problems. If the lba + num blocks 8735 * is less than the lba, then we've wrapped around and the block 8736 * range is invalid anyway. 8737 */ 8738 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8739 || ((lba + num_blocks) < lba)) { 8740 ctl_set_lba_out_of_range(ctsio, 8741 MAX(lba, lun->be_lun->maxlba + 1)); 8742 ctl_done((union ctl_io *)ctsio); 8743 return (CTL_RETVAL_COMPLETE); 8744 } 8745 8746 /* 8747 * According to SBC-3, a transfer length of 0 is not an error. 8748 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8749 * translates to 256 blocks for those commands. 8750 */ 8751 if (num_blocks == 0) { 8752 ctl_set_success(ctsio); 8753 ctl_done((union ctl_io *)ctsio); 8754 return (CTL_RETVAL_COMPLETE); 8755 } 8756 8757 /* Set FUA and/or DPO if caches are disabled. */ 8758 if (isread) { 8759 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8760 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8761 } else { 8762 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8763 flags |= CTL_LLF_FUA; 8764 } 8765 8766 lbalen = (struct ctl_lba_len_flags *) 8767 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8768 lbalen->lba = lba; 8769 lbalen->len = num_blocks; 8770 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8771 8772 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8773 ctsio->kern_rel_offset = 0; 8774 8775 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8776 8777 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8778 return (retval); 8779} 8780 8781static int 8782ctl_cnw_cont(union ctl_io *io) 8783{ 8784 struct ctl_lun *lun = CTL_LUN(io); 8785 struct ctl_scsiio *ctsio; 8786 struct ctl_lba_len_flags *lbalen; 8787 int retval; 8788 8789 ctsio = &io->scsiio; 8790 ctsio->io_hdr.status = CTL_STATUS_NONE; 8791 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8792 lbalen = (struct ctl_lba_len_flags *) 8793 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8794 lbalen->flags &= ~CTL_LLF_COMPARE; 8795 lbalen->flags |= CTL_LLF_WRITE; 8796 8797 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8798 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8799 return (retval); 8800} 8801 8802int 8803ctl_cnw(struct ctl_scsiio *ctsio) 8804{ 8805 struct ctl_lun *lun = CTL_LUN(ctsio); 8806 struct ctl_lba_len_flags *lbalen; 8807 uint64_t lba; 8808 uint32_t num_blocks; 8809 int flags, retval; 8810 8811 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8812 8813 flags = 0; 8814 switch (ctsio->cdb[0]) { 8815 case COMPARE_AND_WRITE: { 8816 struct scsi_compare_and_write *cdb; 8817 8818 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8819 if (cdb->byte2 & SRW10_FUA) 8820 flags |= CTL_LLF_FUA; 8821 if (cdb->byte2 & SRW10_DPO) 8822 flags |= CTL_LLF_DPO; 8823 lba = scsi_8btou64(cdb->addr); 8824 num_blocks = cdb->length; 8825 break; 8826 } 8827 default: 8828 /* 8829 * We got a command we don't support. This shouldn't 8830 * happen, commands should be filtered out above us. 8831 */ 8832 ctl_set_invalid_opcode(ctsio); 8833 ctl_done((union ctl_io *)ctsio); 8834 8835 return (CTL_RETVAL_COMPLETE); 8836 break; /* NOTREACHED */ 8837 } 8838 8839 /* 8840 * The first check is to make sure we're in bounds, the second 8841 * check is to catch wrap-around problems. If the lba + num blocks 8842 * is less than the lba, then we've wrapped around and the block 8843 * range is invalid anyway. 8844 */ 8845 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8846 || ((lba + num_blocks) < lba)) { 8847 ctl_set_lba_out_of_range(ctsio, 8848 MAX(lba, lun->be_lun->maxlba + 1)); 8849 ctl_done((union ctl_io *)ctsio); 8850 return (CTL_RETVAL_COMPLETE); 8851 } 8852 8853 /* 8854 * According to SBC-3, a transfer length of 0 is not an error. 8855 */ 8856 if (num_blocks == 0) { 8857 ctl_set_success(ctsio); 8858 ctl_done((union ctl_io *)ctsio); 8859 return (CTL_RETVAL_COMPLETE); 8860 } 8861 8862 /* Set FUA if write cache is disabled. */ 8863 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8864 flags |= CTL_LLF_FUA; 8865 8866 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8867 ctsio->kern_rel_offset = 0; 8868 8869 /* 8870 * Set the IO_CONT flag, so that if this I/O gets passed to 8871 * ctl_data_submit_done(), it'll get passed back to 8872 * ctl_ctl_cnw_cont() for further processing. 8873 */ 8874 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8875 ctsio->io_cont = ctl_cnw_cont; 8876 8877 lbalen = (struct ctl_lba_len_flags *) 8878 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8879 lbalen->lba = lba; 8880 lbalen->len = num_blocks; 8881 lbalen->flags = CTL_LLF_COMPARE | flags; 8882 8883 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8884 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8885 return (retval); 8886} 8887 8888int 8889ctl_verify(struct ctl_scsiio *ctsio) 8890{ 8891 struct ctl_lun *lun = CTL_LUN(ctsio); 8892 struct ctl_lba_len_flags *lbalen; 8893 uint64_t lba; 8894 uint32_t num_blocks; 8895 int bytchk, flags; 8896 int retval; 8897 8898 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8899 8900 bytchk = 0; 8901 flags = CTL_LLF_FUA; 8902 switch (ctsio->cdb[0]) { 8903 case VERIFY_10: { 8904 struct scsi_verify_10 *cdb; 8905 8906 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8907 if (cdb->byte2 & SVFY_BYTCHK) 8908 bytchk = 1; 8909 if (cdb->byte2 & SVFY_DPO) 8910 flags |= CTL_LLF_DPO; 8911 lba = scsi_4btoul(cdb->addr); 8912 num_blocks = scsi_2btoul(cdb->length); 8913 break; 8914 } 8915 case VERIFY_12: { 8916 struct scsi_verify_12 *cdb; 8917 8918 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8919 if (cdb->byte2 & SVFY_BYTCHK) 8920 bytchk = 1; 8921 if (cdb->byte2 & SVFY_DPO) 8922 flags |= CTL_LLF_DPO; 8923 lba = scsi_4btoul(cdb->addr); 8924 num_blocks = scsi_4btoul(cdb->length); 8925 break; 8926 } 8927 case VERIFY_16: { 8928 struct scsi_rw_16 *cdb; 8929 8930 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8931 if (cdb->byte2 & SVFY_BYTCHK) 8932 bytchk = 1; 8933 if (cdb->byte2 & SVFY_DPO) 8934 flags |= CTL_LLF_DPO; 8935 lba = scsi_8btou64(cdb->addr); 8936 num_blocks = scsi_4btoul(cdb->length); 8937 break; 8938 } 8939 default: 8940 /* 8941 * We got a command we don't support. This shouldn't 8942 * happen, commands should be filtered out above us. 8943 */ 8944 ctl_set_invalid_opcode(ctsio); 8945 ctl_done((union ctl_io *)ctsio); 8946 return (CTL_RETVAL_COMPLETE); 8947 } 8948 8949 /* 8950 * The first check is to make sure we're in bounds, the second 8951 * check is to catch wrap-around problems. If the lba + num blocks 8952 * is less than the lba, then we've wrapped around and the block 8953 * range is invalid anyway. 8954 */ 8955 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8956 || ((lba + num_blocks) < lba)) { 8957 ctl_set_lba_out_of_range(ctsio, 8958 MAX(lba, lun->be_lun->maxlba + 1)); 8959 ctl_done((union ctl_io *)ctsio); 8960 return (CTL_RETVAL_COMPLETE); 8961 } 8962 8963 /* 8964 * According to SBC-3, a transfer length of 0 is not an error. 8965 */ 8966 if (num_blocks == 0) { 8967 ctl_set_success(ctsio); 8968 ctl_done((union ctl_io *)ctsio); 8969 return (CTL_RETVAL_COMPLETE); 8970 } 8971 8972 lbalen = (struct ctl_lba_len_flags *) 8973 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8974 lbalen->lba = lba; 8975 lbalen->len = num_blocks; 8976 if (bytchk) { 8977 lbalen->flags = CTL_LLF_COMPARE | flags; 8978 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8979 } else { 8980 lbalen->flags = CTL_LLF_VERIFY | flags; 8981 ctsio->kern_total_len = 0; 8982 } 8983 ctsio->kern_rel_offset = 0; 8984 8985 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8986 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8987 return (retval); 8988} 8989 8990int 8991ctl_report_luns(struct ctl_scsiio *ctsio) 8992{ 8993 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8994 struct ctl_port *port = CTL_PORT(ctsio); 8995 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 8996 struct scsi_report_luns *cdb; 8997 struct scsi_report_luns_data *lun_data; 8998 int num_filled, num_luns, num_port_luns, retval; 8999 uint32_t alloc_len, lun_datalen; 9000 uint32_t initidx, targ_lun_id, lun_id; 9001 9002 retval = CTL_RETVAL_COMPLETE; 9003 cdb = (struct scsi_report_luns *)ctsio->cdb; 9004 9005 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9006 9007 num_luns = 0; 9008 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS; 9009 mtx_lock(&softc->ctl_lock); 9010 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9011 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9012 num_luns++; 9013 } 9014 mtx_unlock(&softc->ctl_lock); 9015 9016 switch (cdb->select_report) { 9017 case RPL_REPORT_DEFAULT: 9018 case RPL_REPORT_ALL: 9019 case RPL_REPORT_NONSUBSID: 9020 break; 9021 case RPL_REPORT_WELLKNOWN: 9022 case RPL_REPORT_ADMIN: 9023 case RPL_REPORT_CONGLOM: 9024 num_luns = 0; 9025 break; 9026 default: 9027 ctl_set_invalid_field(ctsio, 9028 /*sks_valid*/ 1, 9029 /*command*/ 1, 9030 /*field*/ 2, 9031 /*bit_valid*/ 0, 9032 /*bit*/ 0); 9033 ctl_done((union ctl_io *)ctsio); 9034 return (retval); 9035 break; /* NOTREACHED */ 9036 } 9037 9038 alloc_len = scsi_4btoul(cdb->length); 9039 /* 9040 * The initiator has to allocate at least 16 bytes for this request, 9041 * so he can at least get the header and the first LUN. Otherwise 9042 * we reject the request (per SPC-3 rev 14, section 6.21). 9043 */ 9044 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9045 sizeof(struct scsi_report_luns_lundata))) { 9046 ctl_set_invalid_field(ctsio, 9047 /*sks_valid*/ 1, 9048 /*command*/ 1, 9049 /*field*/ 6, 9050 /*bit_valid*/ 0, 9051 /*bit*/ 0); 9052 ctl_done((union ctl_io *)ctsio); 9053 return (retval); 9054 } 9055 9056 lun_datalen = sizeof(*lun_data) + 9057 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9058 9059 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9060 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9061 ctsio->kern_sg_entries = 0; 9062 9063 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9064 9065 mtx_lock(&softc->ctl_lock); 9066 for (targ_lun_id = 0, num_filled = 0; 9067 targ_lun_id < num_port_luns && num_filled < num_luns; 9068 targ_lun_id++) { 9069 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9070 if (lun_id == UINT32_MAX) 9071 continue; 9072 lun = softc->ctl_luns[lun_id]; 9073 if (lun == NULL) 9074 continue; 9075 9076 be64enc(lun_data->luns[num_filled++].lundata, 9077 ctl_encode_lun(targ_lun_id)); 9078 9079 /* 9080 * According to SPC-3, rev 14 section 6.21: 9081 * 9082 * "The execution of a REPORT LUNS command to any valid and 9083 * installed logical unit shall clear the REPORTED LUNS DATA 9084 * HAS CHANGED unit attention condition for all logical 9085 * units of that target with respect to the requesting 9086 * initiator. A valid and installed logical unit is one 9087 * having a PERIPHERAL QUALIFIER of 000b in the standard 9088 * INQUIRY data (see 6.4.2)." 9089 * 9090 * If request_lun is NULL, the LUN this report luns command 9091 * was issued to is either disabled or doesn't exist. In that 9092 * case, we shouldn't clear any pending lun change unit 9093 * attention. 9094 */ 9095 if (request_lun != NULL) { 9096 mtx_lock(&lun->lun_lock); 9097 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9098 mtx_unlock(&lun->lun_lock); 9099 } 9100 } 9101 mtx_unlock(&softc->ctl_lock); 9102 9103 /* 9104 * It's quite possible that we've returned fewer LUNs than we allocated 9105 * space for. Trim it. 9106 */ 9107 lun_datalen = sizeof(*lun_data) + 9108 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9109 ctsio->kern_rel_offset = 0; 9110 ctsio->kern_sg_entries = 0; 9111 ctsio->kern_data_len = min(lun_datalen, alloc_len); 9112 ctsio->kern_total_len = ctsio->kern_data_len; 9113 9114 /* 9115 * We set this to the actual data length, regardless of how much 9116 * space we actually have to return results. If the user looks at 9117 * this value, he'll know whether or not he allocated enough space 9118 * and reissue the command if necessary. We don't support well 9119 * known logical units, so if the user asks for that, return none. 9120 */ 9121 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9122 9123 /* 9124 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9125 * this request. 9126 */ 9127 ctl_set_success(ctsio); 9128 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9129 ctsio->be_move_done = ctl_config_move_done; 9130 ctl_datamove((union ctl_io *)ctsio); 9131 return (retval); 9132} 9133 9134int 9135ctl_request_sense(struct ctl_scsiio *ctsio) 9136{ 9137 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9138 struct ctl_lun *lun = CTL_LUN(ctsio); 9139 struct scsi_request_sense *cdb; 9140 struct scsi_sense_data *sense_ptr, *ps; 9141 uint32_t initidx; 9142 int have_error; 9143 u_int sense_len = SSD_FULL_SIZE; 9144 scsi_sense_data_type sense_format; 9145 ctl_ua_type ua_type; 9146 uint8_t asc = 0, ascq = 0; 9147 9148 cdb = (struct scsi_request_sense *)ctsio->cdb; 9149 9150 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9151 9152 /* 9153 * Determine which sense format the user wants. 9154 */ 9155 if (cdb->byte2 & SRS_DESC) 9156 sense_format = SSD_TYPE_DESC; 9157 else 9158 sense_format = SSD_TYPE_FIXED; 9159 9160 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9161 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9162 ctsio->kern_sg_entries = 0; 9163 ctsio->kern_rel_offset = 0; 9164 9165 /* 9166 * struct scsi_sense_data, which is currently set to 256 bytes, is 9167 * larger than the largest allowed value for the length field in the 9168 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9169 */ 9170 ctsio->kern_data_len = cdb->length; 9171 ctsio->kern_total_len = cdb->length; 9172 9173 /* 9174 * If we don't have a LUN, we don't have any pending sense. 9175 */ 9176 if (lun == NULL || 9177 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9178 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9179 /* "Logical unit not supported" */ 9180 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9181 /*current_error*/ 1, 9182 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9183 /*asc*/ 0x25, 9184 /*ascq*/ 0x00, 9185 SSD_ELEM_NONE); 9186 goto send; 9187 } 9188 9189 have_error = 0; 9190 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9191 /* 9192 * Check for pending sense, and then for pending unit attentions. 9193 * Pending sense gets returned first, then pending unit attentions. 9194 */ 9195 mtx_lock(&lun->lun_lock); 9196 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 9197 if (ps != NULL) 9198 ps += initidx % CTL_MAX_INIT_PER_PORT; 9199 if (ps != NULL && ps->error_code != 0) { 9200 scsi_sense_data_type stored_format; 9201 9202 /* 9203 * Check to see which sense format was used for the stored 9204 * sense data. 9205 */ 9206 stored_format = scsi_sense_type(ps); 9207 9208 /* 9209 * If the user requested a different sense format than the 9210 * one we stored, then we need to convert it to the other 9211 * format. If we're going from descriptor to fixed format 9212 * sense data, we may lose things in translation, depending 9213 * on what options were used. 9214 * 9215 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9216 * for some reason we'll just copy it out as-is. 9217 */ 9218 if ((stored_format == SSD_TYPE_FIXED) 9219 && (sense_format == SSD_TYPE_DESC)) 9220 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9221 ps, (struct scsi_sense_data_desc *)sense_ptr); 9222 else if ((stored_format == SSD_TYPE_DESC) 9223 && (sense_format == SSD_TYPE_FIXED)) 9224 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9225 ps, (struct scsi_sense_data_fixed *)sense_ptr); 9226 else 9227 memcpy(sense_ptr, ps, sizeof(*sense_ptr)); 9228 9229 ps->error_code = 0; 9230 have_error = 1; 9231 } else { 9232 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9233 sense_format); 9234 if (ua_type != CTL_UA_NONE) 9235 have_error = 1; 9236 } 9237 if (have_error == 0) { 9238 /* 9239 * Report informational exception if have one and allowed. 9240 */ 9241 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9242 asc = lun->ie_asc; 9243 ascq = lun->ie_ascq; 9244 } 9245 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9246 /*current_error*/ 1, 9247 /*sense_key*/ SSD_KEY_NO_SENSE, 9248 /*asc*/ asc, 9249 /*ascq*/ ascq, 9250 SSD_ELEM_NONE); 9251 } 9252 mtx_unlock(&lun->lun_lock); 9253 9254send: 9255 /* 9256 * We report the SCSI status as OK, since the status of the command 9257 * itself is OK. We're reporting sense as parameter data. 9258 */ 9259 ctl_set_success(ctsio); 9260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9261 ctsio->be_move_done = ctl_config_move_done; 9262 ctl_datamove((union ctl_io *)ctsio); 9263 return (CTL_RETVAL_COMPLETE); 9264} 9265 9266int 9267ctl_tur(struct ctl_scsiio *ctsio) 9268{ 9269 9270 CTL_DEBUG_PRINT(("ctl_tur\n")); 9271 9272 ctl_set_success(ctsio); 9273 ctl_done((union ctl_io *)ctsio); 9274 9275 return (CTL_RETVAL_COMPLETE); 9276} 9277 9278/* 9279 * SCSI VPD page 0x00, the Supported VPD Pages page. 9280 */ 9281static int 9282ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9283{ 9284 struct ctl_lun *lun = CTL_LUN(ctsio); 9285 struct scsi_vpd_supported_pages *pages; 9286 int sup_page_size; 9287 int p; 9288 9289 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9290 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9291 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9292 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9293 ctsio->kern_rel_offset = 0; 9294 ctsio->kern_sg_entries = 0; 9295 ctsio->kern_data_len = min(sup_page_size, alloc_len); 9296 ctsio->kern_total_len = ctsio->kern_data_len; 9297 9298 /* 9299 * The control device is always connected. The disk device, on the 9300 * other hand, may not be online all the time. Need to change this 9301 * to figure out whether the disk device is actually online or not. 9302 */ 9303 if (lun != NULL) 9304 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9305 lun->be_lun->lun_type; 9306 else 9307 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9308 9309 p = 0; 9310 /* Supported VPD pages */ 9311 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9312 /* Serial Number */ 9313 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9314 /* Device Identification */ 9315 pages->page_list[p++] = SVPD_DEVICE_ID; 9316 /* Extended INQUIRY Data */ 9317 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9318 /* Mode Page Policy */ 9319 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9320 /* SCSI Ports */ 9321 pages->page_list[p++] = SVPD_SCSI_PORTS; 9322 /* Third-party Copy */ 9323 pages->page_list[p++] = SVPD_SCSI_TPC; 9324 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9325 /* Block limits */ 9326 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9327 /* Block Device Characteristics */ 9328 pages->page_list[p++] = SVPD_BDC; 9329 /* Logical Block Provisioning */ 9330 pages->page_list[p++] = SVPD_LBP; 9331 } 9332 pages->length = p; 9333 9334 ctl_set_success(ctsio); 9335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9336 ctsio->be_move_done = ctl_config_move_done; 9337 ctl_datamove((union ctl_io *)ctsio); 9338 return (CTL_RETVAL_COMPLETE); 9339} 9340 9341/* 9342 * SCSI VPD page 0x80, the Unit Serial Number page. 9343 */ 9344static int 9345ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9346{ 9347 struct ctl_lun *lun = CTL_LUN(ctsio); 9348 struct scsi_vpd_unit_serial_number *sn_ptr; 9349 int data_len; 9350 9351 data_len = 4 + CTL_SN_LEN; 9352 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9353 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9354 ctsio->kern_rel_offset = 0; 9355 ctsio->kern_sg_entries = 0; 9356 ctsio->kern_data_len = min(data_len, alloc_len); 9357 ctsio->kern_total_len = ctsio->kern_data_len; 9358 9359 /* 9360 * The control device is always connected. The disk device, on the 9361 * other hand, may not be online all the time. Need to change this 9362 * to figure out whether the disk device is actually online or not. 9363 */ 9364 if (lun != NULL) 9365 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9366 lun->be_lun->lun_type; 9367 else 9368 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9369 9370 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9371 sn_ptr->length = CTL_SN_LEN; 9372 /* 9373 * If we don't have a LUN, we just leave the serial number as 9374 * all spaces. 9375 */ 9376 if (lun != NULL) { 9377 strncpy((char *)sn_ptr->serial_num, 9378 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9379 } else 9380 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9381 9382 ctl_set_success(ctsio); 9383 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9384 ctsio->be_move_done = ctl_config_move_done; 9385 ctl_datamove((union ctl_io *)ctsio); 9386 return (CTL_RETVAL_COMPLETE); 9387} 9388 9389 9390/* 9391 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9392 */ 9393static int 9394ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9395{ 9396 struct ctl_lun *lun = CTL_LUN(ctsio); 9397 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9398 int data_len; 9399 9400 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9401 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9402 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9403 ctsio->kern_sg_entries = 0; 9404 ctsio->kern_rel_offset = 0; 9405 ctsio->kern_data_len = min(data_len, alloc_len); 9406 ctsio->kern_total_len = ctsio->kern_data_len; 9407 9408 /* 9409 * The control device is always connected. The disk device, on the 9410 * other hand, may not be online all the time. 9411 */ 9412 if (lun != NULL) 9413 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9414 lun->be_lun->lun_type; 9415 else 9416 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9417 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9418 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9419 /* 9420 * We support head of queue, ordered and simple tags. 9421 */ 9422 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9423 /* 9424 * Volatile cache supported. 9425 */ 9426 eid_ptr->flags3 = SVPD_EID_V_SUP; 9427 9428 /* 9429 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9430 * attention for a particular IT nexus on all LUNs once we report 9431 * it to that nexus once. This bit is required as of SPC-4. 9432 */ 9433 eid_ptr->flags4 = SVPD_EID_LUICLR; 9434 9435 /* 9436 * We support revert to defaults (RTD) bit in MODE SELECT. 9437 */ 9438 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9439 9440 /* 9441 * XXX KDM in order to correctly answer this, we would need 9442 * information from the SIM to determine how much sense data it 9443 * can send. So this would really be a path inquiry field, most 9444 * likely. This can be set to a maximum of 252 according to SPC-4, 9445 * but the hardware may or may not be able to support that much. 9446 * 0 just means that the maximum sense data length is not reported. 9447 */ 9448 eid_ptr->max_sense_length = 0; 9449 9450 ctl_set_success(ctsio); 9451 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9452 ctsio->be_move_done = ctl_config_move_done; 9453 ctl_datamove((union ctl_io *)ctsio); 9454 return (CTL_RETVAL_COMPLETE); 9455} 9456 9457static int 9458ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9459{ 9460 struct ctl_lun *lun = CTL_LUN(ctsio); 9461 struct scsi_vpd_mode_page_policy *mpp_ptr; 9462 int data_len; 9463 9464 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9465 sizeof(struct scsi_vpd_mode_page_policy_descr); 9466 9467 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9468 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9469 ctsio->kern_rel_offset = 0; 9470 ctsio->kern_sg_entries = 0; 9471 ctsio->kern_data_len = min(data_len, alloc_len); 9472 ctsio->kern_total_len = ctsio->kern_data_len; 9473 9474 /* 9475 * The control device is always connected. The disk device, on the 9476 * other hand, may not be online all the time. 9477 */ 9478 if (lun != NULL) 9479 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9480 lun->be_lun->lun_type; 9481 else 9482 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9483 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9484 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9485 mpp_ptr->descr[0].page_code = 0x3f; 9486 mpp_ptr->descr[0].subpage_code = 0xff; 9487 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9488 9489 ctl_set_success(ctsio); 9490 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9491 ctsio->be_move_done = ctl_config_move_done; 9492 ctl_datamove((union ctl_io *)ctsio); 9493 return (CTL_RETVAL_COMPLETE); 9494} 9495 9496/* 9497 * SCSI VPD page 0x83, the Device Identification page. 9498 */ 9499static int 9500ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9501{ 9502 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9503 struct ctl_port *port = CTL_PORT(ctsio); 9504 struct ctl_lun *lun = CTL_LUN(ctsio); 9505 struct scsi_vpd_device_id *devid_ptr; 9506 struct scsi_vpd_id_descriptor *desc; 9507 int data_len, g; 9508 uint8_t proto; 9509 9510 data_len = sizeof(struct scsi_vpd_device_id) + 9511 sizeof(struct scsi_vpd_id_descriptor) + 9512 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9513 sizeof(struct scsi_vpd_id_descriptor) + 9514 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9515 if (lun && lun->lun_devid) 9516 data_len += lun->lun_devid->len; 9517 if (port && port->port_devid) 9518 data_len += port->port_devid->len; 9519 if (port && port->target_devid) 9520 data_len += port->target_devid->len; 9521 9522 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9523 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9524 ctsio->kern_sg_entries = 0; 9525 ctsio->kern_rel_offset = 0; 9526 ctsio->kern_sg_entries = 0; 9527 ctsio->kern_data_len = min(data_len, alloc_len); 9528 ctsio->kern_total_len = ctsio->kern_data_len; 9529 9530 /* 9531 * The control device is always connected. The disk device, on the 9532 * other hand, may not be online all the time. 9533 */ 9534 if (lun != NULL) 9535 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9536 lun->be_lun->lun_type; 9537 else 9538 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9539 devid_ptr->page_code = SVPD_DEVICE_ID; 9540 scsi_ulto2b(data_len - 4, devid_ptr->length); 9541 9542 if (port && port->port_type == CTL_PORT_FC) 9543 proto = SCSI_PROTO_FC << 4; 9544 else if (port && port->port_type == CTL_PORT_SAS) 9545 proto = SCSI_PROTO_SAS << 4; 9546 else if (port && port->port_type == CTL_PORT_ISCSI) 9547 proto = SCSI_PROTO_ISCSI << 4; 9548 else 9549 proto = SCSI_PROTO_SPI << 4; 9550 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9551 9552 /* 9553 * We're using a LUN association here. i.e., this device ID is a 9554 * per-LUN identifier. 9555 */ 9556 if (lun && lun->lun_devid) { 9557 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9558 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9559 lun->lun_devid->len); 9560 } 9561 9562 /* 9563 * This is for the WWPN which is a port association. 9564 */ 9565 if (port && port->port_devid) { 9566 memcpy(desc, port->port_devid->data, port->port_devid->len); 9567 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9568 port->port_devid->len); 9569 } 9570 9571 /* 9572 * This is for the Relative Target Port(type 4h) identifier 9573 */ 9574 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9575 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9576 SVPD_ID_TYPE_RELTARG; 9577 desc->length = 4; 9578 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9579 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9580 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9581 9582 /* 9583 * This is for the Target Port Group(type 5h) identifier 9584 */ 9585 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9586 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9587 SVPD_ID_TYPE_TPORTGRP; 9588 desc->length = 4; 9589 if (softc->is_single || 9590 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9591 g = 1; 9592 else 9593 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9594 scsi_ulto2b(g, &desc->identifier[2]); 9595 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9596 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9597 9598 /* 9599 * This is for the Target identifier 9600 */ 9601 if (port && port->target_devid) { 9602 memcpy(desc, port->target_devid->data, port->target_devid->len); 9603 } 9604 9605 ctl_set_success(ctsio); 9606 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9607 ctsio->be_move_done = ctl_config_move_done; 9608 ctl_datamove((union ctl_io *)ctsio); 9609 return (CTL_RETVAL_COMPLETE); 9610} 9611 9612static int 9613ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9614{ 9615 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9616 struct ctl_lun *lun = CTL_LUN(ctsio); 9617 struct scsi_vpd_scsi_ports *sp; 9618 struct scsi_vpd_port_designation *pd; 9619 struct scsi_vpd_port_designation_cont *pdc; 9620 struct ctl_port *port; 9621 int data_len, num_target_ports, iid_len, id_len; 9622 9623 num_target_ports = 0; 9624 iid_len = 0; 9625 id_len = 0; 9626 mtx_lock(&softc->ctl_lock); 9627 STAILQ_FOREACH(port, &softc->port_list, links) { 9628 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9629 continue; 9630 if (lun != NULL && 9631 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9632 continue; 9633 num_target_ports++; 9634 if (port->init_devid) 9635 iid_len += port->init_devid->len; 9636 if (port->port_devid) 9637 id_len += port->port_devid->len; 9638 } 9639 mtx_unlock(&softc->ctl_lock); 9640 9641 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9642 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9643 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9644 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9645 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9646 ctsio->kern_sg_entries = 0; 9647 ctsio->kern_rel_offset = 0; 9648 ctsio->kern_sg_entries = 0; 9649 ctsio->kern_data_len = min(data_len, alloc_len); 9650 ctsio->kern_total_len = ctsio->kern_data_len; 9651 9652 /* 9653 * The control device is always connected. The disk device, on the 9654 * other hand, may not be online all the time. Need to change this 9655 * to figure out whether the disk device is actually online or not. 9656 */ 9657 if (lun != NULL) 9658 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9659 lun->be_lun->lun_type; 9660 else 9661 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9662 9663 sp->page_code = SVPD_SCSI_PORTS; 9664 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9665 sp->page_length); 9666 pd = &sp->design[0]; 9667 9668 mtx_lock(&softc->ctl_lock); 9669 STAILQ_FOREACH(port, &softc->port_list, links) { 9670 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9671 continue; 9672 if (lun != NULL && 9673 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9674 continue; 9675 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9676 if (port->init_devid) { 9677 iid_len = port->init_devid->len; 9678 memcpy(pd->initiator_transportid, 9679 port->init_devid->data, port->init_devid->len); 9680 } else 9681 iid_len = 0; 9682 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9683 pdc = (struct scsi_vpd_port_designation_cont *) 9684 (&pd->initiator_transportid[iid_len]); 9685 if (port->port_devid) { 9686 id_len = port->port_devid->len; 9687 memcpy(pdc->target_port_descriptors, 9688 port->port_devid->data, port->port_devid->len); 9689 } else 9690 id_len = 0; 9691 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9692 pd = (struct scsi_vpd_port_designation *) 9693 ((uint8_t *)pdc->target_port_descriptors + id_len); 9694 } 9695 mtx_unlock(&softc->ctl_lock); 9696 9697 ctl_set_success(ctsio); 9698 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9699 ctsio->be_move_done = ctl_config_move_done; 9700 ctl_datamove((union ctl_io *)ctsio); 9701 return (CTL_RETVAL_COMPLETE); 9702} 9703 9704static int 9705ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9706{ 9707 struct ctl_lun *lun = CTL_LUN(ctsio); 9708 struct scsi_vpd_block_limits *bl_ptr; 9709 uint64_t ival; 9710 9711 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9712 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9713 ctsio->kern_sg_entries = 0; 9714 ctsio->kern_rel_offset = 0; 9715 ctsio->kern_sg_entries = 0; 9716 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len); 9717 ctsio->kern_total_len = ctsio->kern_data_len; 9718 9719 /* 9720 * The control device is always connected. The disk device, on the 9721 * other hand, may not be online all the time. Need to change this 9722 * to figure out whether the disk device is actually online or not. 9723 */ 9724 if (lun != NULL) 9725 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9726 lun->be_lun->lun_type; 9727 else 9728 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9729 9730 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9731 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9732 bl_ptr->max_cmp_write_len = 0xff; 9733 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9734 if (lun != NULL) { 9735 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9736 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9737 ival = 0xffffffff; 9738 ctl_get_opt_number(&lun->be_lun->options, 9739 "unmap_max_lba", &ival); 9740 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9741 ival = 0xffffffff; 9742 ctl_get_opt_number(&lun->be_lun->options, 9743 "unmap_max_descr", &ival); 9744 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9745 if (lun->be_lun->ublockexp != 0) { 9746 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9747 bl_ptr->opt_unmap_grain); 9748 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9749 bl_ptr->unmap_grain_align); 9750 } 9751 } 9752 scsi_ulto4b(lun->be_lun->atomicblock, 9753 bl_ptr->max_atomic_transfer_length); 9754 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9755 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9756 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9757 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9758 ival = UINT64_MAX; 9759 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival); 9760 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9761 } 9762 9763 ctl_set_success(ctsio); 9764 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9765 ctsio->be_move_done = ctl_config_move_done; 9766 ctl_datamove((union ctl_io *)ctsio); 9767 return (CTL_RETVAL_COMPLETE); 9768} 9769 9770static int 9771ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9772{ 9773 struct ctl_lun *lun = CTL_LUN(ctsio); 9774 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9775 const char *value; 9776 u_int i; 9777 9778 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9779 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9780 ctsio->kern_sg_entries = 0; 9781 ctsio->kern_rel_offset = 0; 9782 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len); 9783 ctsio->kern_total_len = ctsio->kern_data_len; 9784 9785 /* 9786 * The control device is always connected. The disk device, on the 9787 * other hand, may not be online all the time. Need to change this 9788 * to figure out whether the disk device is actually online or not. 9789 */ 9790 if (lun != NULL) 9791 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9792 lun->be_lun->lun_type; 9793 else 9794 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9795 bdc_ptr->page_code = SVPD_BDC; 9796 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9797 if (lun != NULL && 9798 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9799 i = strtol(value, NULL, 0); 9800 else 9801 i = CTL_DEFAULT_ROTATION_RATE; 9802 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9803 if (lun != NULL && 9804 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9805 i = strtol(value, NULL, 0); 9806 else 9807 i = 0; 9808 bdc_ptr->wab_wac_ff = (i & 0x0f); 9809 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9810 9811 ctl_set_success(ctsio); 9812 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9813 ctsio->be_move_done = ctl_config_move_done; 9814 ctl_datamove((union ctl_io *)ctsio); 9815 return (CTL_RETVAL_COMPLETE); 9816} 9817 9818static int 9819ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9820{ 9821 struct ctl_lun *lun = CTL_LUN(ctsio); 9822 struct scsi_vpd_logical_block_prov *lbp_ptr; 9823 const char *value; 9824 9825 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9826 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9827 ctsio->kern_sg_entries = 0; 9828 ctsio->kern_rel_offset = 0; 9829 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len); 9830 ctsio->kern_total_len = ctsio->kern_data_len; 9831 9832 /* 9833 * The control device is always connected. The disk device, on the 9834 * other hand, may not be online all the time. Need to change this 9835 * to figure out whether the disk device is actually online or not. 9836 */ 9837 if (lun != NULL) 9838 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9839 lun->be_lun->lun_type; 9840 else 9841 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9842 9843 lbp_ptr->page_code = SVPD_LBP; 9844 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9845 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9846 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9847 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9848 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9849 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type"); 9850 if (value != NULL) { 9851 if (strcmp(value, "resource") == 0) 9852 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 9853 else if (strcmp(value, "thin") == 0) 9854 lbp_ptr->prov_type = SVPD_LBP_THIN; 9855 } else 9856 lbp_ptr->prov_type = SVPD_LBP_THIN; 9857 } 9858 9859 ctl_set_success(ctsio); 9860 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9861 ctsio->be_move_done = ctl_config_move_done; 9862 ctl_datamove((union ctl_io *)ctsio); 9863 return (CTL_RETVAL_COMPLETE); 9864} 9865 9866/* 9867 * INQUIRY with the EVPD bit set. 9868 */ 9869static int 9870ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9871{ 9872 struct ctl_lun *lun = CTL_LUN(ctsio); 9873 struct scsi_inquiry *cdb; 9874 int alloc_len, retval; 9875 9876 cdb = (struct scsi_inquiry *)ctsio->cdb; 9877 alloc_len = scsi_2btoul(cdb->length); 9878 9879 switch (cdb->page_code) { 9880 case SVPD_SUPPORTED_PAGES: 9881 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9882 break; 9883 case SVPD_UNIT_SERIAL_NUMBER: 9884 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9885 break; 9886 case SVPD_DEVICE_ID: 9887 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9888 break; 9889 case SVPD_EXTENDED_INQUIRY_DATA: 9890 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9891 break; 9892 case SVPD_MODE_PAGE_POLICY: 9893 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 9894 break; 9895 case SVPD_SCSI_PORTS: 9896 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 9897 break; 9898 case SVPD_SCSI_TPC: 9899 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 9900 break; 9901 case SVPD_BLOCK_LIMITS: 9902 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9903 goto err; 9904 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9905 break; 9906 case SVPD_BDC: 9907 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9908 goto err; 9909 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 9910 break; 9911 case SVPD_LBP: 9912 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9913 goto err; 9914 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9915 break; 9916 default: 9917err: 9918 ctl_set_invalid_field(ctsio, 9919 /*sks_valid*/ 1, 9920 /*command*/ 1, 9921 /*field*/ 2, 9922 /*bit_valid*/ 0, 9923 /*bit*/ 0); 9924 ctl_done((union ctl_io *)ctsio); 9925 retval = CTL_RETVAL_COMPLETE; 9926 break; 9927 } 9928 9929 return (retval); 9930} 9931 9932/* 9933 * Standard INQUIRY data. 9934 */ 9935static int 9936ctl_inquiry_std(struct ctl_scsiio *ctsio) 9937{ 9938 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9939 struct ctl_port *port = CTL_PORT(ctsio); 9940 struct ctl_lun *lun = CTL_LUN(ctsio); 9941 struct scsi_inquiry_data *inq_ptr; 9942 struct scsi_inquiry *cdb; 9943 char *val; 9944 uint32_t alloc_len, data_len; 9945 ctl_port_type port_type; 9946 9947 port_type = port->port_type; 9948 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 9949 port_type = CTL_PORT_SCSI; 9950 9951 cdb = (struct scsi_inquiry *)ctsio->cdb; 9952 alloc_len = scsi_2btoul(cdb->length); 9953 9954 /* 9955 * We malloc the full inquiry data size here and fill it 9956 * in. If the user only asks for less, we'll give him 9957 * that much. 9958 */ 9959 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 9960 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9961 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 9962 ctsio->kern_sg_entries = 0; 9963 ctsio->kern_rel_offset = 0; 9964 ctsio->kern_data_len = min(data_len, alloc_len); 9965 ctsio->kern_total_len = ctsio->kern_data_len; 9966 9967 if (lun != NULL) { 9968 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 9969 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 9970 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9971 lun->be_lun->lun_type; 9972 } else { 9973 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 9974 lun->be_lun->lun_type; 9975 } 9976 if (lun->flags & CTL_LUN_REMOVABLE) 9977 inq_ptr->dev_qual2 |= SID_RMB; 9978 } else 9979 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 9980 9981 /* RMB in byte 2 is 0 */ 9982 inq_ptr->version = SCSI_REV_SPC5; 9983 9984 /* 9985 * According to SAM-3, even if a device only supports a single 9986 * level of LUN addressing, it should still set the HISUP bit: 9987 * 9988 * 4.9.1 Logical unit numbers overview 9989 * 9990 * All logical unit number formats described in this standard are 9991 * hierarchical in structure even when only a single level in that 9992 * hierarchy is used. The HISUP bit shall be set to one in the 9993 * standard INQUIRY data (see SPC-2) when any logical unit number 9994 * format described in this standard is used. Non-hierarchical 9995 * formats are outside the scope of this standard. 9996 * 9997 * Therefore we set the HiSup bit here. 9998 * 9999 * The response format is 2, per SPC-3. 10000 */ 10001 inq_ptr->response_format = SID_HiSup | 2; 10002 10003 inq_ptr->additional_length = data_len - 10004 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10005 CTL_DEBUG_PRINT(("additional_length = %d\n", 10006 inq_ptr->additional_length)); 10007 10008 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10009 if (port_type == CTL_PORT_SCSI) 10010 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10011 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10012 inq_ptr->flags = SID_CmdQue; 10013 if (port_type == CTL_PORT_SCSI) 10014 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10015 10016 /* 10017 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10018 * We have 8 bytes for the vendor name, and 16 bytes for the device 10019 * name and 4 bytes for the revision. 10020 */ 10021 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10022 "vendor")) == NULL) { 10023 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10024 } else { 10025 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10026 strncpy(inq_ptr->vendor, val, 10027 min(sizeof(inq_ptr->vendor), strlen(val))); 10028 } 10029 if (lun == NULL) { 10030 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10031 sizeof(inq_ptr->product)); 10032 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10033 switch (lun->be_lun->lun_type) { 10034 case T_DIRECT: 10035 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10036 sizeof(inq_ptr->product)); 10037 break; 10038 case T_PROCESSOR: 10039 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10040 sizeof(inq_ptr->product)); 10041 break; 10042 case T_CDROM: 10043 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10044 sizeof(inq_ptr->product)); 10045 break; 10046 default: 10047 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10048 sizeof(inq_ptr->product)); 10049 break; 10050 } 10051 } else { 10052 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10053 strncpy(inq_ptr->product, val, 10054 min(sizeof(inq_ptr->product), strlen(val))); 10055 } 10056 10057 /* 10058 * XXX make this a macro somewhere so it automatically gets 10059 * incremented when we make changes. 10060 */ 10061 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10062 "revision")) == NULL) { 10063 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10064 } else { 10065 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10066 strncpy(inq_ptr->revision, val, 10067 min(sizeof(inq_ptr->revision), strlen(val))); 10068 } 10069 10070 /* 10071 * For parallel SCSI, we support double transition and single 10072 * transition clocking. We also support QAS (Quick Arbitration 10073 * and Selection) and Information Unit transfers on both the 10074 * control and array devices. 10075 */ 10076 if (port_type == CTL_PORT_SCSI) 10077 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10078 SID_SPI_IUS; 10079 10080 /* SAM-6 (no version claimed) */ 10081 scsi_ulto2b(0x00C0, inq_ptr->version1); 10082 /* SPC-5 (no version claimed) */ 10083 scsi_ulto2b(0x05C0, inq_ptr->version2); 10084 if (port_type == CTL_PORT_FC) { 10085 /* FCP-2 ANSI INCITS.350:2003 */ 10086 scsi_ulto2b(0x0917, inq_ptr->version3); 10087 } else if (port_type == CTL_PORT_SCSI) { 10088 /* SPI-4 ANSI INCITS.362:200x */ 10089 scsi_ulto2b(0x0B56, inq_ptr->version3); 10090 } else if (port_type == CTL_PORT_ISCSI) { 10091 /* iSCSI (no version claimed) */ 10092 scsi_ulto2b(0x0960, inq_ptr->version3); 10093 } else if (port_type == CTL_PORT_SAS) { 10094 /* SAS (no version claimed) */ 10095 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10096 } 10097 10098 if (lun == NULL) { 10099 /* SBC-4 (no version claimed) */ 10100 scsi_ulto2b(0x0600, inq_ptr->version4); 10101 } else { 10102 switch (lun->be_lun->lun_type) { 10103 case T_DIRECT: 10104 /* SBC-4 (no version claimed) */ 10105 scsi_ulto2b(0x0600, inq_ptr->version4); 10106 break; 10107 case T_PROCESSOR: 10108 break; 10109 case T_CDROM: 10110 /* MMC-6 (no version claimed) */ 10111 scsi_ulto2b(0x04E0, inq_ptr->version4); 10112 break; 10113 default: 10114 break; 10115 } 10116 } 10117 10118 ctl_set_success(ctsio); 10119 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10120 ctsio->be_move_done = ctl_config_move_done; 10121 ctl_datamove((union ctl_io *)ctsio); 10122 return (CTL_RETVAL_COMPLETE); 10123} 10124 10125int 10126ctl_inquiry(struct ctl_scsiio *ctsio) 10127{ 10128 struct scsi_inquiry *cdb; 10129 int retval; 10130 10131 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10132 10133 cdb = (struct scsi_inquiry *)ctsio->cdb; 10134 if (cdb->byte2 & SI_EVPD) 10135 retval = ctl_inquiry_evpd(ctsio); 10136 else if (cdb->page_code == 0) 10137 retval = ctl_inquiry_std(ctsio); 10138 else { 10139 ctl_set_invalid_field(ctsio, 10140 /*sks_valid*/ 1, 10141 /*command*/ 1, 10142 /*field*/ 2, 10143 /*bit_valid*/ 0, 10144 /*bit*/ 0); 10145 ctl_done((union ctl_io *)ctsio); 10146 return (CTL_RETVAL_COMPLETE); 10147 } 10148 10149 return (retval); 10150} 10151 10152int 10153ctl_get_config(struct ctl_scsiio *ctsio) 10154{ 10155 struct ctl_lun *lun = CTL_LUN(ctsio); 10156 struct scsi_get_config_header *hdr; 10157 struct scsi_get_config_feature *feature; 10158 struct scsi_get_config *cdb; 10159 uint32_t alloc_len, data_len; 10160 int rt, starting; 10161 10162 cdb = (struct scsi_get_config *)ctsio->cdb; 10163 rt = (cdb->rt & SGC_RT_MASK); 10164 starting = scsi_2btoul(cdb->starting_feature); 10165 alloc_len = scsi_2btoul(cdb->length); 10166 10167 data_len = sizeof(struct scsi_get_config_header) + 10168 sizeof(struct scsi_get_config_feature) + 8 + 10169 sizeof(struct scsi_get_config_feature) + 8 + 10170 sizeof(struct scsi_get_config_feature) + 4 + 10171 sizeof(struct scsi_get_config_feature) + 4 + 10172 sizeof(struct scsi_get_config_feature) + 8 + 10173 sizeof(struct scsi_get_config_feature) + 10174 sizeof(struct scsi_get_config_feature) + 4 + 10175 sizeof(struct scsi_get_config_feature) + 4 + 10176 sizeof(struct scsi_get_config_feature) + 4 + 10177 sizeof(struct scsi_get_config_feature) + 4 + 10178 sizeof(struct scsi_get_config_feature) + 4 + 10179 sizeof(struct scsi_get_config_feature) + 4; 10180 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10181 ctsio->kern_sg_entries = 0; 10182 ctsio->kern_rel_offset = 0; 10183 10184 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10185 if (lun->flags & CTL_LUN_NO_MEDIA) 10186 scsi_ulto2b(0x0000, hdr->current_profile); 10187 else 10188 scsi_ulto2b(0x0010, hdr->current_profile); 10189 feature = (struct scsi_get_config_feature *)(hdr + 1); 10190 10191 if (starting > 0x003b) 10192 goto done; 10193 if (starting > 0x003a) 10194 goto f3b; 10195 if (starting > 0x002b) 10196 goto f3a; 10197 if (starting > 0x002a) 10198 goto f2b; 10199 if (starting > 0x001f) 10200 goto f2a; 10201 if (starting > 0x001e) 10202 goto f1f; 10203 if (starting > 0x001d) 10204 goto f1e; 10205 if (starting > 0x0010) 10206 goto f1d; 10207 if (starting > 0x0003) 10208 goto f10; 10209 if (starting > 0x0002) 10210 goto f3; 10211 if (starting > 0x0001) 10212 goto f2; 10213 if (starting > 0x0000) 10214 goto f1; 10215 10216 /* Profile List */ 10217 scsi_ulto2b(0x0000, feature->feature_code); 10218 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10219 feature->add_length = 8; 10220 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10221 feature->feature_data[2] = 0x00; 10222 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10223 feature->feature_data[6] = 0x01; 10224 feature = (struct scsi_get_config_feature *) 10225 &feature->feature_data[feature->add_length]; 10226 10227f1: /* Core */ 10228 scsi_ulto2b(0x0001, feature->feature_code); 10229 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10230 feature->add_length = 8; 10231 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10232 feature->feature_data[4] = 0x03; 10233 feature = (struct scsi_get_config_feature *) 10234 &feature->feature_data[feature->add_length]; 10235 10236f2: /* Morphing */ 10237 scsi_ulto2b(0x0002, feature->feature_code); 10238 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10239 feature->add_length = 4; 10240 feature->feature_data[0] = 0x02; 10241 feature = (struct scsi_get_config_feature *) 10242 &feature->feature_data[feature->add_length]; 10243 10244f3: /* Removable Medium */ 10245 scsi_ulto2b(0x0003, feature->feature_code); 10246 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10247 feature->add_length = 4; 10248 feature->feature_data[0] = 0x39; 10249 feature = (struct scsi_get_config_feature *) 10250 &feature->feature_data[feature->add_length]; 10251 10252 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10253 goto done; 10254 10255f10: /* Random Read */ 10256 scsi_ulto2b(0x0010, feature->feature_code); 10257 feature->flags = 0x00; 10258 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10259 feature->flags |= SGC_F_CURRENT; 10260 feature->add_length = 8; 10261 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10262 scsi_ulto2b(1, &feature->feature_data[4]); 10263 feature->feature_data[6] = 0x00; 10264 feature = (struct scsi_get_config_feature *) 10265 &feature->feature_data[feature->add_length]; 10266 10267f1d: /* Multi-Read */ 10268 scsi_ulto2b(0x001D, feature->feature_code); 10269 feature->flags = 0x00; 10270 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10271 feature->flags |= SGC_F_CURRENT; 10272 feature->add_length = 0; 10273 feature = (struct scsi_get_config_feature *) 10274 &feature->feature_data[feature->add_length]; 10275 10276f1e: /* CD Read */ 10277 scsi_ulto2b(0x001E, feature->feature_code); 10278 feature->flags = 0x00; 10279 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10280 feature->flags |= SGC_F_CURRENT; 10281 feature->add_length = 4; 10282 feature->feature_data[0] = 0x00; 10283 feature = (struct scsi_get_config_feature *) 10284 &feature->feature_data[feature->add_length]; 10285 10286f1f: /* DVD Read */ 10287 scsi_ulto2b(0x001F, feature->feature_code); 10288 feature->flags = 0x08; 10289 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10290 feature->flags |= SGC_F_CURRENT; 10291 feature->add_length = 4; 10292 feature->feature_data[0] = 0x01; 10293 feature->feature_data[2] = 0x03; 10294 feature = (struct scsi_get_config_feature *) 10295 &feature->feature_data[feature->add_length]; 10296 10297f2a: /* DVD+RW */ 10298 scsi_ulto2b(0x002A, feature->feature_code); 10299 feature->flags = 0x04; 10300 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10301 feature->flags |= SGC_F_CURRENT; 10302 feature->add_length = 4; 10303 feature->feature_data[0] = 0x00; 10304 feature->feature_data[1] = 0x00; 10305 feature = (struct scsi_get_config_feature *) 10306 &feature->feature_data[feature->add_length]; 10307 10308f2b: /* DVD+R */ 10309 scsi_ulto2b(0x002B, feature->feature_code); 10310 feature->flags = 0x00; 10311 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10312 feature->flags |= SGC_F_CURRENT; 10313 feature->add_length = 4; 10314 feature->feature_data[0] = 0x00; 10315 feature = (struct scsi_get_config_feature *) 10316 &feature->feature_data[feature->add_length]; 10317 10318f3a: /* DVD+RW Dual Layer */ 10319 scsi_ulto2b(0x003A, feature->feature_code); 10320 feature->flags = 0x00; 10321 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10322 feature->flags |= SGC_F_CURRENT; 10323 feature->add_length = 4; 10324 feature->feature_data[0] = 0x00; 10325 feature->feature_data[1] = 0x00; 10326 feature = (struct scsi_get_config_feature *) 10327 &feature->feature_data[feature->add_length]; 10328 10329f3b: /* DVD+R Dual Layer */ 10330 scsi_ulto2b(0x003B, feature->feature_code); 10331 feature->flags = 0x00; 10332 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10333 feature->flags |= SGC_F_CURRENT; 10334 feature->add_length = 4; 10335 feature->feature_data[0] = 0x00; 10336 feature = (struct scsi_get_config_feature *) 10337 &feature->feature_data[feature->add_length]; 10338 10339done: 10340 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10341 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10342 feature = (struct scsi_get_config_feature *)(hdr + 1); 10343 if (scsi_2btoul(feature->feature_code) == starting) 10344 feature = (struct scsi_get_config_feature *) 10345 &feature->feature_data[feature->add_length]; 10346 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10347 } 10348 scsi_ulto4b(data_len - 4, hdr->data_length); 10349 ctsio->kern_data_len = min(data_len, alloc_len); 10350 ctsio->kern_total_len = ctsio->kern_data_len; 10351 10352 ctl_set_success(ctsio); 10353 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10354 ctsio->be_move_done = ctl_config_move_done; 10355 ctl_datamove((union ctl_io *)ctsio); 10356 return (CTL_RETVAL_COMPLETE); 10357} 10358 10359int 10360ctl_get_event_status(struct ctl_scsiio *ctsio) 10361{ 10362 struct scsi_get_event_status_header *hdr; 10363 struct scsi_get_event_status *cdb; 10364 uint32_t alloc_len, data_len; 10365 int notif_class; 10366 10367 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10368 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10369 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10370 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10371 ctl_done((union ctl_io *)ctsio); 10372 return (CTL_RETVAL_COMPLETE); 10373 } 10374 notif_class = cdb->notif_class; 10375 alloc_len = scsi_2btoul(cdb->length); 10376 10377 data_len = sizeof(struct scsi_get_event_status_header); 10378 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10379 ctsio->kern_sg_entries = 0; 10380 ctsio->kern_rel_offset = 0; 10381 ctsio->kern_data_len = min(data_len, alloc_len); 10382 ctsio->kern_total_len = ctsio->kern_data_len; 10383 10384 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10385 scsi_ulto2b(0, hdr->descr_length); 10386 hdr->nea_class = SGESN_NEA; 10387 hdr->supported_class = 0; 10388 10389 ctl_set_success(ctsio); 10390 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10391 ctsio->be_move_done = ctl_config_move_done; 10392 ctl_datamove((union ctl_io *)ctsio); 10393 return (CTL_RETVAL_COMPLETE); 10394} 10395 10396int 10397ctl_mechanism_status(struct ctl_scsiio *ctsio) 10398{ 10399 struct scsi_mechanism_status_header *hdr; 10400 struct scsi_mechanism_status *cdb; 10401 uint32_t alloc_len, data_len; 10402 10403 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10404 alloc_len = scsi_2btoul(cdb->length); 10405 10406 data_len = sizeof(struct scsi_mechanism_status_header); 10407 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10408 ctsio->kern_sg_entries = 0; 10409 ctsio->kern_rel_offset = 0; 10410 ctsio->kern_data_len = min(data_len, alloc_len); 10411 ctsio->kern_total_len = ctsio->kern_data_len; 10412 10413 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10414 hdr->state1 = 0x00; 10415 hdr->state2 = 0xe0; 10416 scsi_ulto3b(0, hdr->lba); 10417 hdr->slots_num = 0; 10418 scsi_ulto2b(0, hdr->slots_length); 10419 10420 ctl_set_success(ctsio); 10421 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10422 ctsio->be_move_done = ctl_config_move_done; 10423 ctl_datamove((union ctl_io *)ctsio); 10424 return (CTL_RETVAL_COMPLETE); 10425} 10426 10427static void 10428ctl_ultomsf(uint32_t lba, uint8_t *buf) 10429{ 10430 10431 lba += 150; 10432 buf[0] = 0; 10433 buf[1] = bin2bcd((lba / 75) / 60); 10434 buf[2] = bin2bcd((lba / 75) % 60); 10435 buf[3] = bin2bcd(lba % 75); 10436} 10437 10438int 10439ctl_read_toc(struct ctl_scsiio *ctsio) 10440{ 10441 struct ctl_lun *lun = CTL_LUN(ctsio); 10442 struct scsi_read_toc_hdr *hdr; 10443 struct scsi_read_toc_type01_descr *descr; 10444 struct scsi_read_toc *cdb; 10445 uint32_t alloc_len, data_len; 10446 int format, msf; 10447 10448 cdb = (struct scsi_read_toc *)ctsio->cdb; 10449 msf = (cdb->byte2 & CD_MSF) != 0; 10450 format = cdb->format; 10451 alloc_len = scsi_2btoul(cdb->data_len); 10452 10453 data_len = sizeof(struct scsi_read_toc_hdr); 10454 if (format == 0) 10455 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10456 else 10457 data_len += sizeof(struct scsi_read_toc_type01_descr); 10458 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10459 ctsio->kern_sg_entries = 0; 10460 ctsio->kern_rel_offset = 0; 10461 ctsio->kern_data_len = min(data_len, alloc_len); 10462 ctsio->kern_total_len = ctsio->kern_data_len; 10463 10464 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10465 if (format == 0) { 10466 scsi_ulto2b(0x12, hdr->data_length); 10467 hdr->first = 1; 10468 hdr->last = 1; 10469 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10470 descr->addr_ctl = 0x14; 10471 descr->track_number = 1; 10472 if (msf) 10473 ctl_ultomsf(0, descr->track_start); 10474 else 10475 scsi_ulto4b(0, descr->track_start); 10476 descr++; 10477 descr->addr_ctl = 0x14; 10478 descr->track_number = 0xaa; 10479 if (msf) 10480 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10481 else 10482 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10483 } else { 10484 scsi_ulto2b(0x0a, hdr->data_length); 10485 hdr->first = 1; 10486 hdr->last = 1; 10487 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10488 descr->addr_ctl = 0x14; 10489 descr->track_number = 1; 10490 if (msf) 10491 ctl_ultomsf(0, descr->track_start); 10492 else 10493 scsi_ulto4b(0, descr->track_start); 10494 } 10495 10496 ctl_set_success(ctsio); 10497 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10498 ctsio->be_move_done = ctl_config_move_done; 10499 ctl_datamove((union ctl_io *)ctsio); 10500 return (CTL_RETVAL_COMPLETE); 10501} 10502 10503/* 10504 * For known CDB types, parse the LBA and length. 10505 */ 10506static int 10507ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10508{ 10509 if (io->io_hdr.io_type != CTL_IO_SCSI) 10510 return (1); 10511 10512 switch (io->scsiio.cdb[0]) { 10513 case COMPARE_AND_WRITE: { 10514 struct scsi_compare_and_write *cdb; 10515 10516 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10517 10518 *lba = scsi_8btou64(cdb->addr); 10519 *len = cdb->length; 10520 break; 10521 } 10522 case READ_6: 10523 case WRITE_6: { 10524 struct scsi_rw_6 *cdb; 10525 10526 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10527 10528 *lba = scsi_3btoul(cdb->addr); 10529 /* only 5 bits are valid in the most significant address byte */ 10530 *lba &= 0x1fffff; 10531 *len = cdb->length; 10532 break; 10533 } 10534 case READ_10: 10535 case WRITE_10: { 10536 struct scsi_rw_10 *cdb; 10537 10538 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10539 10540 *lba = scsi_4btoul(cdb->addr); 10541 *len = scsi_2btoul(cdb->length); 10542 break; 10543 } 10544 case WRITE_VERIFY_10: { 10545 struct scsi_write_verify_10 *cdb; 10546 10547 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10548 10549 *lba = scsi_4btoul(cdb->addr); 10550 *len = scsi_2btoul(cdb->length); 10551 break; 10552 } 10553 case READ_12: 10554 case WRITE_12: { 10555 struct scsi_rw_12 *cdb; 10556 10557 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10558 10559 *lba = scsi_4btoul(cdb->addr); 10560 *len = scsi_4btoul(cdb->length); 10561 break; 10562 } 10563 case WRITE_VERIFY_12: { 10564 struct scsi_write_verify_12 *cdb; 10565 10566 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10567 10568 *lba = scsi_4btoul(cdb->addr); 10569 *len = scsi_4btoul(cdb->length); 10570 break; 10571 } 10572 case READ_16: 10573 case WRITE_16: { 10574 struct scsi_rw_16 *cdb; 10575 10576 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10577 10578 *lba = scsi_8btou64(cdb->addr); 10579 *len = scsi_4btoul(cdb->length); 10580 break; 10581 } 10582 case WRITE_ATOMIC_16: { 10583 struct scsi_write_atomic_16 *cdb; 10584 10585 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10586 10587 *lba = scsi_8btou64(cdb->addr); 10588 *len = scsi_2btoul(cdb->length); 10589 break; 10590 } 10591 case WRITE_VERIFY_16: { 10592 struct scsi_write_verify_16 *cdb; 10593 10594 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10595 10596 *lba = scsi_8btou64(cdb->addr); 10597 *len = scsi_4btoul(cdb->length); 10598 break; 10599 } 10600 case WRITE_SAME_10: { 10601 struct scsi_write_same_10 *cdb; 10602 10603 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10604 10605 *lba = scsi_4btoul(cdb->addr); 10606 *len = scsi_2btoul(cdb->length); 10607 break; 10608 } 10609 case WRITE_SAME_16: { 10610 struct scsi_write_same_16 *cdb; 10611 10612 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10613 10614 *lba = scsi_8btou64(cdb->addr); 10615 *len = scsi_4btoul(cdb->length); 10616 break; 10617 } 10618 case VERIFY_10: { 10619 struct scsi_verify_10 *cdb; 10620 10621 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10622 10623 *lba = scsi_4btoul(cdb->addr); 10624 *len = scsi_2btoul(cdb->length); 10625 break; 10626 } 10627 case VERIFY_12: { 10628 struct scsi_verify_12 *cdb; 10629 10630 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10631 10632 *lba = scsi_4btoul(cdb->addr); 10633 *len = scsi_4btoul(cdb->length); 10634 break; 10635 } 10636 case VERIFY_16: { 10637 struct scsi_verify_16 *cdb; 10638 10639 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10640 10641 *lba = scsi_8btou64(cdb->addr); 10642 *len = scsi_4btoul(cdb->length); 10643 break; 10644 } 10645 case UNMAP: { 10646 *lba = 0; 10647 *len = UINT64_MAX; 10648 break; 10649 } 10650 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10651 struct scsi_get_lba_status *cdb; 10652 10653 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10654 *lba = scsi_8btou64(cdb->addr); 10655 *len = UINT32_MAX; 10656 break; 10657 } 10658 default: 10659 return (1); 10660 break; /* NOTREACHED */ 10661 } 10662 10663 return (0); 10664} 10665 10666static ctl_action 10667ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10668 bool seq) 10669{ 10670 uint64_t endlba1, endlba2; 10671 10672 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10673 endlba2 = lba2 + len2 - 1; 10674 10675 if ((endlba1 < lba2) || (endlba2 < lba1)) 10676 return (CTL_ACTION_PASS); 10677 else 10678 return (CTL_ACTION_BLOCK); 10679} 10680 10681static int 10682ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10683{ 10684 struct ctl_ptr_len_flags *ptrlen; 10685 struct scsi_unmap_desc *buf, *end, *range; 10686 uint64_t lba; 10687 uint32_t len; 10688 10689 /* If not UNMAP -- go other way. */ 10690 if (io->io_hdr.io_type != CTL_IO_SCSI || 10691 io->scsiio.cdb[0] != UNMAP) 10692 return (CTL_ACTION_ERROR); 10693 10694 /* If UNMAP without data -- block and wait for data. */ 10695 ptrlen = (struct ctl_ptr_len_flags *) 10696 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10697 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10698 ptrlen->ptr == NULL) 10699 return (CTL_ACTION_BLOCK); 10700 10701 /* UNMAP with data -- check for collision. */ 10702 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10703 end = buf + ptrlen->len / sizeof(*buf); 10704 for (range = buf; range < end; range++) { 10705 lba = scsi_8btou64(range->lba); 10706 len = scsi_4btoul(range->length); 10707 if ((lba < lba2 + len2) && (lba + len > lba2)) 10708 return (CTL_ACTION_BLOCK); 10709 } 10710 return (CTL_ACTION_PASS); 10711} 10712 10713static ctl_action 10714ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10715{ 10716 uint64_t lba1, lba2; 10717 uint64_t len1, len2; 10718 int retval; 10719 10720 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10721 return (CTL_ACTION_ERROR); 10722 10723 retval = ctl_extent_check_unmap(io1, lba2, len2); 10724 if (retval != CTL_ACTION_ERROR) 10725 return (retval); 10726 10727 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10728 return (CTL_ACTION_ERROR); 10729 10730 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10731 seq = FALSE; 10732 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10733} 10734 10735static ctl_action 10736ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10737{ 10738 uint64_t lba1, lba2; 10739 uint64_t len1, len2; 10740 10741 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10742 return (CTL_ACTION_PASS); 10743 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10744 return (CTL_ACTION_ERROR); 10745 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10746 return (CTL_ACTION_ERROR); 10747 10748 if (lba1 + len1 == lba2) 10749 return (CTL_ACTION_BLOCK); 10750 return (CTL_ACTION_PASS); 10751} 10752 10753static ctl_action 10754ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10755 union ctl_io *ooa_io) 10756{ 10757 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10758 const ctl_serialize_action *serialize_row; 10759 10760 /* 10761 * The initiator attempted multiple untagged commands at the same 10762 * time. Can't do that. 10763 */ 10764 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10765 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10766 && ((pending_io->io_hdr.nexus.targ_port == 10767 ooa_io->io_hdr.nexus.targ_port) 10768 && (pending_io->io_hdr.nexus.initid == 10769 ooa_io->io_hdr.nexus.initid)) 10770 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10771 CTL_FLAG_STATUS_SENT)) == 0)) 10772 return (CTL_ACTION_OVERLAP); 10773 10774 /* 10775 * The initiator attempted to send multiple tagged commands with 10776 * the same ID. (It's fine if different initiators have the same 10777 * tag ID.) 10778 * 10779 * Even if all of those conditions are true, we don't kill the I/O 10780 * if the command ahead of us has been aborted. We won't end up 10781 * sending it to the FETD, and it's perfectly legal to resend a 10782 * command with the same tag number as long as the previous 10783 * instance of this tag number has been aborted somehow. 10784 */ 10785 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10786 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10787 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10788 && ((pending_io->io_hdr.nexus.targ_port == 10789 ooa_io->io_hdr.nexus.targ_port) 10790 && (pending_io->io_hdr.nexus.initid == 10791 ooa_io->io_hdr.nexus.initid)) 10792 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10793 CTL_FLAG_STATUS_SENT)) == 0)) 10794 return (CTL_ACTION_OVERLAP_TAG); 10795 10796 /* 10797 * If we get a head of queue tag, SAM-3 says that we should 10798 * immediately execute it. 10799 * 10800 * What happens if this command would normally block for some other 10801 * reason? e.g. a request sense with a head of queue tag 10802 * immediately after a write. Normally that would block, but this 10803 * will result in its getting executed immediately... 10804 * 10805 * We currently return "pass" instead of "skip", so we'll end up 10806 * going through the rest of the queue to check for overlapped tags. 10807 * 10808 * XXX KDM check for other types of blockage first?? 10809 */ 10810 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10811 return (CTL_ACTION_PASS); 10812 10813 /* 10814 * Ordered tags have to block until all items ahead of them 10815 * have completed. If we get called with an ordered tag, we always 10816 * block, if something else is ahead of us in the queue. 10817 */ 10818 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10819 return (CTL_ACTION_BLOCK); 10820 10821 /* 10822 * Simple tags get blocked until all head of queue and ordered tags 10823 * ahead of them have completed. I'm lumping untagged commands in 10824 * with simple tags here. XXX KDM is that the right thing to do? 10825 */ 10826 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10827 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10828 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10829 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10830 return (CTL_ACTION_BLOCK); 10831 10832 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10833 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 10834 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 10835 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 10836 pending_io->scsiio.cdb[1], pending_io)); 10837 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10838 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 10839 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 10840 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 10841 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 10842 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 10843 ooa_io->scsiio.cdb[1], ooa_io)); 10844 10845 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10846 10847 switch (serialize_row[pending_entry->seridx]) { 10848 case CTL_SER_BLOCK: 10849 return (CTL_ACTION_BLOCK); 10850 case CTL_SER_EXTENT: 10851 return (ctl_extent_check(ooa_io, pending_io, 10852 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10853 case CTL_SER_EXTENTOPT: 10854 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10855 SCP_QUEUE_ALG_UNRESTRICTED) 10856 return (ctl_extent_check(ooa_io, pending_io, 10857 (lun->be_lun && 10858 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10859 return (CTL_ACTION_PASS); 10860 case CTL_SER_EXTENTSEQ: 10861 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10862 return (ctl_extent_check_seq(ooa_io, pending_io)); 10863 return (CTL_ACTION_PASS); 10864 case CTL_SER_PASS: 10865 return (CTL_ACTION_PASS); 10866 case CTL_SER_BLOCKOPT: 10867 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 10868 SCP_QUEUE_ALG_UNRESTRICTED) 10869 return (CTL_ACTION_BLOCK); 10870 return (CTL_ACTION_PASS); 10871 case CTL_SER_SKIP: 10872 return (CTL_ACTION_SKIP); 10873 default: 10874 panic("%s: Invalid serialization value %d for %d => %d", 10875 __func__, serialize_row[pending_entry->seridx], 10876 pending_entry->seridx, ooa_entry->seridx); 10877 } 10878 10879 return (CTL_ACTION_ERROR); 10880} 10881 10882/* 10883 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10884 * Assumptions: 10885 * - pending_io is generally either incoming, or on the blocked queue 10886 * - starting I/O is the I/O we want to start the check with. 10887 */ 10888static ctl_action 10889ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10890 union ctl_io *starting_io) 10891{ 10892 union ctl_io *ooa_io; 10893 ctl_action action; 10894 10895 mtx_assert(&lun->lun_lock, MA_OWNED); 10896 10897 /* 10898 * Run back along the OOA queue, starting with the current 10899 * blocked I/O and going through every I/O before it on the 10900 * queue. If starting_io is NULL, we'll just end up returning 10901 * CTL_ACTION_PASS. 10902 */ 10903 for (ooa_io = starting_io; ooa_io != NULL; 10904 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10905 ooa_links)){ 10906 10907 /* 10908 * This routine just checks to see whether 10909 * cur_blocked is blocked by ooa_io, which is ahead 10910 * of it in the queue. It doesn't queue/dequeue 10911 * cur_blocked. 10912 */ 10913 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10914 switch (action) { 10915 case CTL_ACTION_BLOCK: 10916 case CTL_ACTION_OVERLAP: 10917 case CTL_ACTION_OVERLAP_TAG: 10918 case CTL_ACTION_SKIP: 10919 case CTL_ACTION_ERROR: 10920 return (action); 10921 break; /* NOTREACHED */ 10922 case CTL_ACTION_PASS: 10923 break; 10924 default: 10925 panic("%s: Invalid action %d\n", __func__, action); 10926 } 10927 } 10928 10929 return (CTL_ACTION_PASS); 10930} 10931 10932/* 10933 * Assumptions: 10934 * - An I/O has just completed, and has been removed from the per-LUN OOA 10935 * queue, so some items on the blocked queue may now be unblocked. 10936 */ 10937static int 10938ctl_check_blocked(struct ctl_lun *lun) 10939{ 10940 struct ctl_softc *softc = lun->ctl_softc; 10941 union ctl_io *cur_blocked, *next_blocked; 10942 10943 mtx_assert(&lun->lun_lock, MA_OWNED); 10944 10945 /* 10946 * Run forward from the head of the blocked queue, checking each 10947 * entry against the I/Os prior to it on the OOA queue to see if 10948 * there is still any blockage. 10949 * 10950 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10951 * with our removing a variable on it while it is traversing the 10952 * list. 10953 */ 10954 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10955 cur_blocked != NULL; cur_blocked = next_blocked) { 10956 union ctl_io *prev_ooa; 10957 ctl_action action; 10958 10959 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10960 blocked_links); 10961 10962 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10963 ctl_ooaq, ooa_links); 10964 10965 /* 10966 * If cur_blocked happens to be the first item in the OOA 10967 * queue now, prev_ooa will be NULL, and the action 10968 * returned will just be CTL_ACTION_PASS. 10969 */ 10970 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10971 10972 switch (action) { 10973 case CTL_ACTION_BLOCK: 10974 /* Nothing to do here, still blocked */ 10975 break; 10976 case CTL_ACTION_OVERLAP: 10977 case CTL_ACTION_OVERLAP_TAG: 10978 /* 10979 * This shouldn't happen! In theory we've already 10980 * checked this command for overlap... 10981 */ 10982 break; 10983 case CTL_ACTION_PASS: 10984 case CTL_ACTION_SKIP: { 10985 const struct ctl_cmd_entry *entry; 10986 10987 /* 10988 * The skip case shouldn't happen, this transaction 10989 * should have never made it onto the blocked queue. 10990 */ 10991 /* 10992 * This I/O is no longer blocked, we can remove it 10993 * from the blocked queue. Since this is a TAILQ 10994 * (doubly linked list), we can do O(1) removals 10995 * from any place on the list. 10996 */ 10997 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10998 blocked_links); 10999 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11000 11001 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11002 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11003 /* 11004 * Need to send IO back to original side to 11005 * run 11006 */ 11007 union ctl_ha_msg msg_info; 11008 11009 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11010 msg_info.hdr.original_sc = 11011 cur_blocked->io_hdr.original_sc; 11012 msg_info.hdr.serializing_sc = cur_blocked; 11013 msg_info.hdr.msg_type = CTL_MSG_R2R; 11014 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11015 sizeof(msg_info.hdr), M_NOWAIT); 11016 break; 11017 } 11018 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11019 11020 /* 11021 * Check this I/O for LUN state changes that may 11022 * have happened while this command was blocked. 11023 * The LUN state may have been changed by a command 11024 * ahead of us in the queue, so we need to re-check 11025 * for any states that can be caused by SCSI 11026 * commands. 11027 */ 11028 if (ctl_scsiio_lun_check(lun, entry, 11029 &cur_blocked->scsiio) == 0) { 11030 cur_blocked->io_hdr.flags |= 11031 CTL_FLAG_IS_WAS_ON_RTR; 11032 ctl_enqueue_rtr(cur_blocked); 11033 } else 11034 ctl_done(cur_blocked); 11035 break; 11036 } 11037 default: 11038 /* 11039 * This probably shouldn't happen -- we shouldn't 11040 * get CTL_ACTION_ERROR, or anything else. 11041 */ 11042 break; 11043 } 11044 } 11045 11046 return (CTL_RETVAL_COMPLETE); 11047} 11048 11049/* 11050 * This routine (with one exception) checks LUN flags that can be set by 11051 * commands ahead of us in the OOA queue. These flags have to be checked 11052 * when a command initially comes in, and when we pull a command off the 11053 * blocked queue and are preparing to execute it. The reason we have to 11054 * check these flags for commands on the blocked queue is that the LUN 11055 * state may have been changed by a command ahead of us while we're on the 11056 * blocked queue. 11057 * 11058 * Ordering is somewhat important with these checks, so please pay 11059 * careful attention to the placement of any new checks. 11060 */ 11061static int 11062ctl_scsiio_lun_check(struct ctl_lun *lun, 11063 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11064{ 11065 struct ctl_softc *softc = lun->ctl_softc; 11066 int retval; 11067 uint32_t residx; 11068 11069 retval = 0; 11070 11071 mtx_assert(&lun->lun_lock, MA_OWNED); 11072 11073 /* 11074 * If this shelf is a secondary shelf controller, we may have to 11075 * reject some commands disallowed by HA mode and link state. 11076 */ 11077 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11078 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11079 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11080 ctl_set_lun_unavail(ctsio); 11081 retval = 1; 11082 goto bailout; 11083 } 11084 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11085 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11086 ctl_set_lun_transit(ctsio); 11087 retval = 1; 11088 goto bailout; 11089 } 11090 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11091 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11092 ctl_set_lun_standby(ctsio); 11093 retval = 1; 11094 goto bailout; 11095 } 11096 11097 /* The rest of checks are only done on executing side */ 11098 if (softc->ha_mode == CTL_HA_MODE_XFER) 11099 goto bailout; 11100 } 11101 11102 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11103 if (lun->be_lun && 11104 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11105 ctl_set_hw_write_protected(ctsio); 11106 retval = 1; 11107 goto bailout; 11108 } 11109 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11110 ctl_set_sense(ctsio, /*current_error*/ 1, 11111 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11112 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11113 retval = 1; 11114 goto bailout; 11115 } 11116 } 11117 11118 /* 11119 * Check for a reservation conflict. If this command isn't allowed 11120 * even on reserved LUNs, and if this initiator isn't the one who 11121 * reserved us, reject the command with a reservation conflict. 11122 */ 11123 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11124 if ((lun->flags & CTL_LUN_RESERVED) 11125 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11126 if (lun->res_idx != residx) { 11127 ctl_set_reservation_conflict(ctsio); 11128 retval = 1; 11129 goto bailout; 11130 } 11131 } 11132 11133 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11134 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11135 /* No reservation or command is allowed. */; 11136 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11137 (lun->pr_res_type == SPR_TYPE_WR_EX || 11138 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11139 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11140 /* The command is allowed for Write Exclusive resv. */; 11141 } else { 11142 /* 11143 * if we aren't registered or it's a res holder type 11144 * reservation and this isn't the res holder then set a 11145 * conflict. 11146 */ 11147 if (ctl_get_prkey(lun, residx) == 0 || 11148 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11149 ctl_set_reservation_conflict(ctsio); 11150 retval = 1; 11151 goto bailout; 11152 } 11153 } 11154 11155 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11156 if (lun->flags & CTL_LUN_EJECTED) 11157 ctl_set_lun_ejected(ctsio); 11158 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11159 if (lun->flags & CTL_LUN_REMOVABLE) 11160 ctl_set_lun_no_media(ctsio); 11161 else 11162 ctl_set_lun_int_reqd(ctsio); 11163 } else if (lun->flags & CTL_LUN_STOPPED) 11164 ctl_set_lun_stopped(ctsio); 11165 else 11166 goto bailout; 11167 retval = 1; 11168 goto bailout; 11169 } 11170 11171bailout: 11172 return (retval); 11173} 11174 11175static void 11176ctl_failover_io(union ctl_io *io, int have_lock) 11177{ 11178 ctl_set_busy(&io->scsiio); 11179 ctl_done(io); 11180} 11181 11182static void 11183ctl_failover_lun(union ctl_io *rio) 11184{ 11185 struct ctl_softc *softc = CTL_SOFTC(rio); 11186 struct ctl_lun *lun; 11187 struct ctl_io_hdr *io, *next_io; 11188 uint32_t targ_lun; 11189 11190 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11191 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11192 11193 /* Find and lock the LUN. */ 11194 mtx_lock(&softc->ctl_lock); 11195 if (targ_lun > CTL_MAX_LUNS || 11196 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11197 mtx_unlock(&softc->ctl_lock); 11198 return; 11199 } 11200 mtx_lock(&lun->lun_lock); 11201 mtx_unlock(&softc->ctl_lock); 11202 if (lun->flags & CTL_LUN_DISABLED) { 11203 mtx_unlock(&lun->lun_lock); 11204 return; 11205 } 11206 11207 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11208 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11209 /* We are master */ 11210 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11211 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11212 io->flags |= CTL_FLAG_ABORT; 11213 io->flags |= CTL_FLAG_FAILOVER; 11214 } else { /* This can be only due to DATAMOVE */ 11215 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11216 io->flags &= ~CTL_FLAG_DMA_INPROG; 11217 io->flags |= CTL_FLAG_IO_ACTIVE; 11218 io->port_status = 31340; 11219 ctl_enqueue_isc((union ctl_io *)io); 11220 } 11221 } 11222 /* We are slave */ 11223 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11224 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11225 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11226 io->flags |= CTL_FLAG_FAILOVER; 11227 } else { 11228 ctl_set_busy(&((union ctl_io *)io)-> 11229 scsiio); 11230 ctl_done((union ctl_io *)io); 11231 } 11232 } 11233 } 11234 } else { /* SERIALIZE modes */ 11235 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11236 next_io) { 11237 /* We are master */ 11238 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11239 TAILQ_REMOVE(&lun->blocked_queue, io, 11240 blocked_links); 11241 io->flags &= ~CTL_FLAG_BLOCKED; 11242 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11243 ctl_free_io((union ctl_io *)io); 11244 } 11245 } 11246 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11247 /* We are master */ 11248 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11249 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11250 ctl_free_io((union ctl_io *)io); 11251 } 11252 /* We are slave */ 11253 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11254 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11255 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11256 ctl_set_busy(&((union ctl_io *)io)-> 11257 scsiio); 11258 ctl_done((union ctl_io *)io); 11259 } 11260 } 11261 } 11262 ctl_check_blocked(lun); 11263 } 11264 mtx_unlock(&lun->lun_lock); 11265} 11266 11267static int 11268ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11269{ 11270 struct ctl_lun *lun; 11271 const struct ctl_cmd_entry *entry; 11272 uint32_t initidx, targ_lun; 11273 int retval = 0; 11274 11275 lun = NULL; 11276 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11277 if (targ_lun < CTL_MAX_LUNS) 11278 lun = softc->ctl_luns[targ_lun]; 11279 if (lun) { 11280 /* 11281 * If the LUN is invalid, pretend that it doesn't exist. 11282 * It will go away as soon as all pending I/O has been 11283 * completed. 11284 */ 11285 mtx_lock(&lun->lun_lock); 11286 if (lun->flags & CTL_LUN_DISABLED) { 11287 mtx_unlock(&lun->lun_lock); 11288 lun = NULL; 11289 } 11290 } 11291 CTL_LUN(ctsio) = lun; 11292 if (lun) { 11293 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11294 11295 /* 11296 * Every I/O goes into the OOA queue for a particular LUN, 11297 * and stays there until completion. 11298 */ 11299#ifdef CTL_TIME_IO 11300 if (TAILQ_EMPTY(&lun->ooa_queue)) 11301 lun->idle_time += getsbinuptime() - lun->last_busy; 11302#endif 11303 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11304 } 11305 11306 /* Get command entry and return error if it is unsuppotyed. */ 11307 entry = ctl_validate_command(ctsio); 11308 if (entry == NULL) { 11309 if (lun) 11310 mtx_unlock(&lun->lun_lock); 11311 return (retval); 11312 } 11313 11314 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11315 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11316 11317 /* 11318 * Check to see whether we can send this command to LUNs that don't 11319 * exist. This should pretty much only be the case for inquiry 11320 * and request sense. Further checks, below, really require having 11321 * a LUN, so we can't really check the command anymore. Just put 11322 * it on the rtr queue. 11323 */ 11324 if (lun == NULL) { 11325 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11326 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11327 ctl_enqueue_rtr((union ctl_io *)ctsio); 11328 return (retval); 11329 } 11330 11331 ctl_set_unsupported_lun(ctsio); 11332 ctl_done((union ctl_io *)ctsio); 11333 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11334 return (retval); 11335 } else { 11336 /* 11337 * Make sure we support this particular command on this LUN. 11338 * e.g., we don't support writes to the control LUN. 11339 */ 11340 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11341 mtx_unlock(&lun->lun_lock); 11342 ctl_set_invalid_opcode(ctsio); 11343 ctl_done((union ctl_io *)ctsio); 11344 return (retval); 11345 } 11346 } 11347 11348 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11349 11350 /* 11351 * If we've got a request sense, it'll clear the contingent 11352 * allegiance condition. Otherwise, if we have a CA condition for 11353 * this initiator, clear it, because it sent down a command other 11354 * than request sense. 11355 */ 11356 if (ctsio->cdb[0] != REQUEST_SENSE) { 11357 struct scsi_sense_data *ps; 11358 11359 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT]; 11360 if (ps != NULL) 11361 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0; 11362 } 11363 11364 /* 11365 * If the command has this flag set, it handles its own unit 11366 * attention reporting, we shouldn't do anything. Otherwise we 11367 * check for any pending unit attentions, and send them back to the 11368 * initiator. We only do this when a command initially comes in, 11369 * not when we pull it off the blocked queue. 11370 * 11371 * According to SAM-3, section 5.3.2, the order that things get 11372 * presented back to the host is basically unit attentions caused 11373 * by some sort of reset event, busy status, reservation conflicts 11374 * or task set full, and finally any other status. 11375 * 11376 * One issue here is that some of the unit attentions we report 11377 * don't fall into the "reset" category (e.g. "reported luns data 11378 * has changed"). So reporting it here, before the reservation 11379 * check, may be technically wrong. I guess the only thing to do 11380 * would be to check for and report the reset events here, and then 11381 * check for the other unit attention types after we check for a 11382 * reservation conflict. 11383 * 11384 * XXX KDM need to fix this 11385 */ 11386 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11387 ctl_ua_type ua_type; 11388 u_int sense_len = 0; 11389 11390 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11391 &sense_len, SSD_TYPE_NONE); 11392 if (ua_type != CTL_UA_NONE) { 11393 mtx_unlock(&lun->lun_lock); 11394 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11395 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11396 ctsio->sense_len = sense_len; 11397 ctl_done((union ctl_io *)ctsio); 11398 return (retval); 11399 } 11400 } 11401 11402 11403 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11404 mtx_unlock(&lun->lun_lock); 11405 ctl_done((union ctl_io *)ctsio); 11406 return (retval); 11407 } 11408 11409 /* 11410 * XXX CHD this is where we want to send IO to other side if 11411 * this LUN is secondary on this SC. We will need to make a copy 11412 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11413 * the copy we send as FROM_OTHER. 11414 * We also need to stuff the address of the original IO so we can 11415 * find it easily. Something similar will need be done on the other 11416 * side so when we are done we can find the copy. 11417 */ 11418 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11419 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11420 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11421 union ctl_ha_msg msg_info; 11422 int isc_retval; 11423 11424 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11425 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11426 mtx_unlock(&lun->lun_lock); 11427 11428 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11429 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11430 msg_info.hdr.serializing_sc = NULL; 11431 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11432 msg_info.scsi.tag_num = ctsio->tag_num; 11433 msg_info.scsi.tag_type = ctsio->tag_type; 11434 msg_info.scsi.cdb_len = ctsio->cdb_len; 11435 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11436 11437 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11438 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11439 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11440 ctl_set_busy(ctsio); 11441 ctl_done((union ctl_io *)ctsio); 11442 return (retval); 11443 } 11444 return (retval); 11445 } 11446 11447 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11448 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11449 ctl_ooaq, ooa_links))) { 11450 case CTL_ACTION_BLOCK: 11451 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11452 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11453 blocked_links); 11454 mtx_unlock(&lun->lun_lock); 11455 return (retval); 11456 case CTL_ACTION_PASS: 11457 case CTL_ACTION_SKIP: 11458 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11459 mtx_unlock(&lun->lun_lock); 11460 ctl_enqueue_rtr((union ctl_io *)ctsio); 11461 break; 11462 case CTL_ACTION_OVERLAP: 11463 mtx_unlock(&lun->lun_lock); 11464 ctl_set_overlapped_cmd(ctsio); 11465 ctl_done((union ctl_io *)ctsio); 11466 break; 11467 case CTL_ACTION_OVERLAP_TAG: 11468 mtx_unlock(&lun->lun_lock); 11469 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11470 ctl_done((union ctl_io *)ctsio); 11471 break; 11472 case CTL_ACTION_ERROR: 11473 default: 11474 mtx_unlock(&lun->lun_lock); 11475 ctl_set_internal_failure(ctsio, 11476 /*sks_valid*/ 0, 11477 /*retry_count*/ 0); 11478 ctl_done((union ctl_io *)ctsio); 11479 break; 11480 } 11481 return (retval); 11482} 11483 11484const struct ctl_cmd_entry * 11485ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11486{ 11487 const struct ctl_cmd_entry *entry; 11488 int service_action; 11489 11490 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11491 if (sa) 11492 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11493 if (entry->flags & CTL_CMD_FLAG_SA5) { 11494 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11495 entry = &((const struct ctl_cmd_entry *) 11496 entry->execute)[service_action]; 11497 } 11498 return (entry); 11499} 11500 11501const struct ctl_cmd_entry * 11502ctl_validate_command(struct ctl_scsiio *ctsio) 11503{ 11504 const struct ctl_cmd_entry *entry; 11505 int i, sa; 11506 uint8_t diff; 11507 11508 entry = ctl_get_cmd_entry(ctsio, &sa); 11509 if (entry->execute == NULL) { 11510 if (sa) 11511 ctl_set_invalid_field(ctsio, 11512 /*sks_valid*/ 1, 11513 /*command*/ 1, 11514 /*field*/ 1, 11515 /*bit_valid*/ 1, 11516 /*bit*/ 4); 11517 else 11518 ctl_set_invalid_opcode(ctsio); 11519 ctl_done((union ctl_io *)ctsio); 11520 return (NULL); 11521 } 11522 KASSERT(entry->length > 0, 11523 ("Not defined length for command 0x%02x/0x%02x", 11524 ctsio->cdb[0], ctsio->cdb[1])); 11525 for (i = 1; i < entry->length; i++) { 11526 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11527 if (diff == 0) 11528 continue; 11529 ctl_set_invalid_field(ctsio, 11530 /*sks_valid*/ 1, 11531 /*command*/ 1, 11532 /*field*/ i, 11533 /*bit_valid*/ 1, 11534 /*bit*/ fls(diff) - 1); 11535 ctl_done((union ctl_io *)ctsio); 11536 return (NULL); 11537 } 11538 return (entry); 11539} 11540 11541static int 11542ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11543{ 11544 11545 switch (lun_type) { 11546 case T_DIRECT: 11547 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11548 return (0); 11549 break; 11550 case T_PROCESSOR: 11551 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11552 return (0); 11553 break; 11554 case T_CDROM: 11555 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11556 return (0); 11557 break; 11558 default: 11559 return (0); 11560 } 11561 return (1); 11562} 11563 11564static int 11565ctl_scsiio(struct ctl_scsiio *ctsio) 11566{ 11567 int retval; 11568 const struct ctl_cmd_entry *entry; 11569 11570 retval = CTL_RETVAL_COMPLETE; 11571 11572 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11573 11574 entry = ctl_get_cmd_entry(ctsio, NULL); 11575 11576 /* 11577 * If this I/O has been aborted, just send it straight to 11578 * ctl_done() without executing it. 11579 */ 11580 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11581 ctl_done((union ctl_io *)ctsio); 11582 goto bailout; 11583 } 11584 11585 /* 11586 * All the checks should have been handled by ctl_scsiio_precheck(). 11587 * We should be clear now to just execute the I/O. 11588 */ 11589 retval = entry->execute(ctsio); 11590 11591bailout: 11592 return (retval); 11593} 11594 11595static int 11596ctl_target_reset(union ctl_io *io) 11597{ 11598 struct ctl_softc *softc = CTL_SOFTC(io); 11599 struct ctl_port *port = CTL_PORT(io); 11600 struct ctl_lun *lun; 11601 uint32_t initidx; 11602 ctl_ua_type ua_type; 11603 11604 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11605 union ctl_ha_msg msg_info; 11606 11607 msg_info.hdr.nexus = io->io_hdr.nexus; 11608 msg_info.task.task_action = io->taskio.task_action; 11609 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11610 msg_info.hdr.original_sc = NULL; 11611 msg_info.hdr.serializing_sc = NULL; 11612 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11613 sizeof(msg_info.task), M_WAITOK); 11614 } 11615 11616 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11617 if (io->taskio.task_action == CTL_TASK_TARGET_RESET) 11618 ua_type = CTL_UA_TARG_RESET; 11619 else 11620 ua_type = CTL_UA_BUS_RESET; 11621 mtx_lock(&softc->ctl_lock); 11622 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11623 if (port != NULL && 11624 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11625 continue; 11626 ctl_do_lun_reset(lun, initidx, ua_type); 11627 } 11628 mtx_unlock(&softc->ctl_lock); 11629 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11630 return (0); 11631} 11632 11633/* 11634 * The LUN should always be set. The I/O is optional, and is used to 11635 * distinguish between I/Os sent by this initiator, and by other 11636 * initiators. We set unit attention for initiators other than this one. 11637 * SAM-3 is vague on this point. It does say that a unit attention should 11638 * be established for other initiators when a LUN is reset (see section 11639 * 5.7.3), but it doesn't specifically say that the unit attention should 11640 * be established for this particular initiator when a LUN is reset. Here 11641 * is the relevant text, from SAM-3 rev 8: 11642 * 11643 * 5.7.2 When a SCSI initiator port aborts its own tasks 11644 * 11645 * When a SCSI initiator port causes its own task(s) to be aborted, no 11646 * notification that the task(s) have been aborted shall be returned to 11647 * the SCSI initiator port other than the completion response for the 11648 * command or task management function action that caused the task(s) to 11649 * be aborted and notification(s) associated with related effects of the 11650 * action (e.g., a reset unit attention condition). 11651 * 11652 * XXX KDM for now, we're setting unit attention for all initiators. 11653 */ 11654static void 11655ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type) 11656{ 11657 union ctl_io *xio; 11658 int i; 11659 11660 mtx_lock(&lun->lun_lock); 11661 /* Abort tasks. */ 11662 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11663 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11664 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11665 } 11666 /* Clear CA. */ 11667 for (i = 0; i < CTL_MAX_PORTS; i++) { 11668 free(lun->pending_sense[i], M_CTL); 11669 lun->pending_sense[i] = NULL; 11670 } 11671 /* Clear reservation. */ 11672 lun->flags &= ~CTL_LUN_RESERVED; 11673 /* Clear prevent media removal. */ 11674 if (lun->prevent) { 11675 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11676 ctl_clear_mask(lun->prevent, i); 11677 lun->prevent_count = 0; 11678 } 11679 /* Clear TPC status */ 11680 ctl_tpc_lun_clear(lun, -1); 11681 /* Establish UA. */ 11682#if 0 11683 ctl_est_ua_all(lun, initidx, ua_type); 11684#else 11685 ctl_est_ua_all(lun, -1, ua_type); 11686#endif 11687 mtx_unlock(&lun->lun_lock); 11688} 11689 11690static int 11691ctl_lun_reset(union ctl_io *io) 11692{ 11693 struct ctl_softc *softc = CTL_SOFTC(io); 11694 struct ctl_lun *lun; 11695 uint32_t targ_lun, initidx; 11696 11697 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11698 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11699 mtx_lock(&softc->ctl_lock); 11700 if (targ_lun >= CTL_MAX_LUNS || 11701 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11702 mtx_unlock(&softc->ctl_lock); 11703 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11704 return (1); 11705 } 11706 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET); 11707 mtx_unlock(&softc->ctl_lock); 11708 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11709 11710 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11711 union ctl_ha_msg msg_info; 11712 11713 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11714 msg_info.hdr.nexus = io->io_hdr.nexus; 11715 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11716 msg_info.hdr.original_sc = NULL; 11717 msg_info.hdr.serializing_sc = NULL; 11718 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11719 sizeof(msg_info.task), M_WAITOK); 11720 } 11721 return (0); 11722} 11723 11724static void 11725ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11726 int other_sc) 11727{ 11728 union ctl_io *xio; 11729 11730 mtx_assert(&lun->lun_lock, MA_OWNED); 11731 11732 /* 11733 * Run through the OOA queue and attempt to find the given I/O. 11734 * The target port, initiator ID, tag type and tag number have to 11735 * match the values that we got from the initiator. If we have an 11736 * untagged command to abort, simply abort the first untagged command 11737 * we come to. We only allow one untagged command at a time of course. 11738 */ 11739 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11740 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11741 11742 if ((targ_port == UINT32_MAX || 11743 targ_port == xio->io_hdr.nexus.targ_port) && 11744 (init_id == UINT32_MAX || 11745 init_id == xio->io_hdr.nexus.initid)) { 11746 if (targ_port != xio->io_hdr.nexus.targ_port || 11747 init_id != xio->io_hdr.nexus.initid) 11748 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11749 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11750 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11751 union ctl_ha_msg msg_info; 11752 11753 msg_info.hdr.nexus = xio->io_hdr.nexus; 11754 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11755 msg_info.task.tag_num = xio->scsiio.tag_num; 11756 msg_info.task.tag_type = xio->scsiio.tag_type; 11757 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11758 msg_info.hdr.original_sc = NULL; 11759 msg_info.hdr.serializing_sc = NULL; 11760 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11761 sizeof(msg_info.task), M_NOWAIT); 11762 } 11763 } 11764 } 11765} 11766 11767static int 11768ctl_abort_task_set(union ctl_io *io) 11769{ 11770 struct ctl_softc *softc = CTL_SOFTC(io); 11771 struct ctl_lun *lun; 11772 uint32_t targ_lun; 11773 11774 /* 11775 * Look up the LUN. 11776 */ 11777 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11778 mtx_lock(&softc->ctl_lock); 11779 if (targ_lun >= CTL_MAX_LUNS || 11780 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11781 mtx_unlock(&softc->ctl_lock); 11782 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11783 return (1); 11784 } 11785 11786 mtx_lock(&lun->lun_lock); 11787 mtx_unlock(&softc->ctl_lock); 11788 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11789 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11790 io->io_hdr.nexus.initid, 11791 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11792 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11793 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11794 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11795 } 11796 mtx_unlock(&lun->lun_lock); 11797 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11798 return (0); 11799} 11800 11801static void 11802ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx, 11803 ctl_ua_type ua_type) 11804{ 11805 struct ctl_lun *lun; 11806 struct scsi_sense_data *ps; 11807 uint32_t p, i; 11808 11809 p = initidx / CTL_MAX_INIT_PER_PORT; 11810 i = initidx % CTL_MAX_INIT_PER_PORT; 11811 mtx_lock(&softc->ctl_lock); 11812 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11813 mtx_lock(&lun->lun_lock); 11814 /* Abort tasks. */ 11815 ctl_abort_tasks_lun(lun, p, i, 1); 11816 /* Clear CA. */ 11817 ps = lun->pending_sense[p]; 11818 if (ps != NULL) 11819 ps[i].error_code = 0; 11820 /* Clear reservation. */ 11821 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11822 lun->flags &= ~CTL_LUN_RESERVED; 11823 /* Clear prevent media removal. */ 11824 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 11825 ctl_clear_mask(lun->prevent, initidx); 11826 lun->prevent_count--; 11827 } 11828 /* Clear TPC status */ 11829 ctl_tpc_lun_clear(lun, initidx); 11830 /* Establish UA. */ 11831 ctl_est_ua(lun, initidx, ua_type); 11832 mtx_unlock(&lun->lun_lock); 11833 } 11834 mtx_unlock(&softc->ctl_lock); 11835} 11836 11837static int 11838ctl_i_t_nexus_reset(union ctl_io *io) 11839{ 11840 struct ctl_softc *softc = CTL_SOFTC(io); 11841 uint32_t initidx; 11842 11843 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11844 union ctl_ha_msg msg_info; 11845 11846 msg_info.hdr.nexus = io->io_hdr.nexus; 11847 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11848 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11849 msg_info.hdr.original_sc = NULL; 11850 msg_info.hdr.serializing_sc = NULL; 11851 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11852 sizeof(msg_info.task), M_WAITOK); 11853 } 11854 11855 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11856 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS); 11857 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11858 return (0); 11859} 11860 11861static int 11862ctl_abort_task(union ctl_io *io) 11863{ 11864 struct ctl_softc *softc = CTL_SOFTC(io); 11865 union ctl_io *xio; 11866 struct ctl_lun *lun; 11867#if 0 11868 struct sbuf sb; 11869 char printbuf[128]; 11870#endif 11871 int found; 11872 uint32_t targ_lun; 11873 11874 found = 0; 11875 11876 /* 11877 * Look up the LUN. 11878 */ 11879 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11880 mtx_lock(&softc->ctl_lock); 11881 if (targ_lun >= CTL_MAX_LUNS || 11882 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11883 mtx_unlock(&softc->ctl_lock); 11884 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11885 return (1); 11886 } 11887 11888#if 0 11889 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11890 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11891#endif 11892 11893 mtx_lock(&lun->lun_lock); 11894 mtx_unlock(&softc->ctl_lock); 11895 /* 11896 * Run through the OOA queue and attempt to find the given I/O. 11897 * The target port, initiator ID, tag type and tag number have to 11898 * match the values that we got from the initiator. If we have an 11899 * untagged command to abort, simply abort the first untagged command 11900 * we come to. We only allow one untagged command at a time of course. 11901 */ 11902 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11903 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11904#if 0 11905 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11906 11907 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11908 lun->lun, xio->scsiio.tag_num, 11909 xio->scsiio.tag_type, 11910 (xio->io_hdr.blocked_links.tqe_prev 11911 == NULL) ? "" : " BLOCKED", 11912 (xio->io_hdr.flags & 11913 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11914 (xio->io_hdr.flags & 11915 CTL_FLAG_ABORT) ? " ABORT" : "", 11916 (xio->io_hdr.flags & 11917 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11918 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11919 sbuf_finish(&sb); 11920 printf("%s\n", sbuf_data(&sb)); 11921#endif 11922 11923 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 11924 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 11925 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 11926 continue; 11927 11928 /* 11929 * If the abort says that the task is untagged, the 11930 * task in the queue must be untagged. Otherwise, 11931 * we just check to see whether the tag numbers 11932 * match. This is because the QLogic firmware 11933 * doesn't pass back the tag type in an abort 11934 * request. 11935 */ 11936#if 0 11937 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11938 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11939 || (xio->scsiio.tag_num == io->taskio.tag_num)) 11940#endif 11941 /* 11942 * XXX KDM we've got problems with FC, because it 11943 * doesn't send down a tag type with aborts. So we 11944 * can only really go by the tag number... 11945 * This may cause problems with parallel SCSI. 11946 * Need to figure that out!! 11947 */ 11948 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11949 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11950 found = 1; 11951 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 11952 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11953 union ctl_ha_msg msg_info; 11954 11955 msg_info.hdr.nexus = io->io_hdr.nexus; 11956 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11957 msg_info.task.tag_num = io->taskio.tag_num; 11958 msg_info.task.tag_type = io->taskio.tag_type; 11959 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11960 msg_info.hdr.original_sc = NULL; 11961 msg_info.hdr.serializing_sc = NULL; 11962#if 0 11963 printf("Sent Abort to other side\n"); 11964#endif 11965 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11966 sizeof(msg_info.task), M_NOWAIT); 11967 } 11968#if 0 11969 printf("ctl_abort_task: found I/O to abort\n"); 11970#endif 11971 } 11972 } 11973 mtx_unlock(&lun->lun_lock); 11974 11975 if (found == 0) { 11976 /* 11977 * This isn't really an error. It's entirely possible for 11978 * the abort and command completion to cross on the wire. 11979 * This is more of an informative/diagnostic error. 11980 */ 11981#if 0 11982 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11983 "%u:%u:%u tag %d type %d\n", 11984 io->io_hdr.nexus.initid, 11985 io->io_hdr.nexus.targ_port, 11986 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11987 io->taskio.tag_type); 11988#endif 11989 } 11990 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11991 return (0); 11992} 11993 11994static int 11995ctl_query_task(union ctl_io *io, int task_set) 11996{ 11997 struct ctl_softc *softc = CTL_SOFTC(io); 11998 union ctl_io *xio; 11999 struct ctl_lun *lun; 12000 int found = 0; 12001 uint32_t targ_lun; 12002 12003 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12004 mtx_lock(&softc->ctl_lock); 12005 if (targ_lun >= CTL_MAX_LUNS || 12006 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12007 mtx_unlock(&softc->ctl_lock); 12008 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12009 return (1); 12010 } 12011 mtx_lock(&lun->lun_lock); 12012 mtx_unlock(&softc->ctl_lock); 12013 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12014 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12015 12016 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12017 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12018 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12019 continue; 12020 12021 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12022 found = 1; 12023 break; 12024 } 12025 } 12026 mtx_unlock(&lun->lun_lock); 12027 if (found) 12028 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12029 else 12030 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12031 return (0); 12032} 12033 12034static int 12035ctl_query_async_event(union ctl_io *io) 12036{ 12037 struct ctl_softc *softc = CTL_SOFTC(io); 12038 struct ctl_lun *lun; 12039 ctl_ua_type ua; 12040 uint32_t targ_lun, initidx; 12041 12042 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12043 mtx_lock(&softc->ctl_lock); 12044 if (targ_lun >= CTL_MAX_LUNS || 12045 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12046 mtx_unlock(&softc->ctl_lock); 12047 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12048 return (1); 12049 } 12050 mtx_lock(&lun->lun_lock); 12051 mtx_unlock(&softc->ctl_lock); 12052 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12053 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12054 mtx_unlock(&lun->lun_lock); 12055 if (ua != CTL_UA_NONE) 12056 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12057 else 12058 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12059 return (0); 12060} 12061 12062static void 12063ctl_run_task(union ctl_io *io) 12064{ 12065 int retval = 1; 12066 12067 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12068 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12069 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12070 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12071 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12072 switch (io->taskio.task_action) { 12073 case CTL_TASK_ABORT_TASK: 12074 retval = ctl_abort_task(io); 12075 break; 12076 case CTL_TASK_ABORT_TASK_SET: 12077 case CTL_TASK_CLEAR_TASK_SET: 12078 retval = ctl_abort_task_set(io); 12079 break; 12080 case CTL_TASK_CLEAR_ACA: 12081 break; 12082 case CTL_TASK_I_T_NEXUS_RESET: 12083 retval = ctl_i_t_nexus_reset(io); 12084 break; 12085 case CTL_TASK_LUN_RESET: 12086 retval = ctl_lun_reset(io); 12087 break; 12088 case CTL_TASK_TARGET_RESET: 12089 case CTL_TASK_BUS_RESET: 12090 retval = ctl_target_reset(io); 12091 break; 12092 case CTL_TASK_PORT_LOGIN: 12093 break; 12094 case CTL_TASK_PORT_LOGOUT: 12095 break; 12096 case CTL_TASK_QUERY_TASK: 12097 retval = ctl_query_task(io, 0); 12098 break; 12099 case CTL_TASK_QUERY_TASK_SET: 12100 retval = ctl_query_task(io, 1); 12101 break; 12102 case CTL_TASK_QUERY_ASYNC_EVENT: 12103 retval = ctl_query_async_event(io); 12104 break; 12105 default: 12106 printf("%s: got unknown task management event %d\n", 12107 __func__, io->taskio.task_action); 12108 break; 12109 } 12110 if (retval == 0) 12111 io->io_hdr.status = CTL_SUCCESS; 12112 else 12113 io->io_hdr.status = CTL_ERROR; 12114 ctl_done(io); 12115} 12116 12117/* 12118 * For HA operation. Handle commands that come in from the other 12119 * controller. 12120 */ 12121static void 12122ctl_handle_isc(union ctl_io *io) 12123{ 12124 struct ctl_softc *softc = CTL_SOFTC(io); 12125 struct ctl_lun *lun; 12126 const struct ctl_cmd_entry *entry; 12127 uint32_t targ_lun; 12128 12129 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12130 switch (io->io_hdr.msg_type) { 12131 case CTL_MSG_SERIALIZE: 12132 ctl_serialize_other_sc_cmd(&io->scsiio); 12133 break; 12134 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12135 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12136 if (targ_lun >= CTL_MAX_LUNS || 12137 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12138 ctl_done(io); 12139 break; 12140 } 12141 mtx_lock(&lun->lun_lock); 12142 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12143 mtx_unlock(&lun->lun_lock); 12144 ctl_done(io); 12145 break; 12146 } 12147 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12148 mtx_unlock(&lun->lun_lock); 12149 ctl_enqueue_rtr(io); 12150 break; 12151 case CTL_MSG_FINISH_IO: 12152 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12153 ctl_done(io); 12154 break; 12155 } 12156 if (targ_lun >= CTL_MAX_LUNS || 12157 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12158 ctl_free_io(io); 12159 break; 12160 } 12161 mtx_lock(&lun->lun_lock); 12162 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12163 ctl_check_blocked(lun); 12164 mtx_unlock(&lun->lun_lock); 12165 ctl_free_io(io); 12166 break; 12167 case CTL_MSG_PERS_ACTION: 12168 ctl_hndl_per_res_out_on_other_sc(io); 12169 ctl_free_io(io); 12170 break; 12171 case CTL_MSG_BAD_JUJU: 12172 ctl_done(io); 12173 break; 12174 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12175 ctl_datamove_remote(io); 12176 break; 12177 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12178 io->scsiio.be_move_done(io); 12179 break; 12180 case CTL_MSG_FAILOVER: 12181 ctl_failover_lun(io); 12182 ctl_free_io(io); 12183 break; 12184 default: 12185 printf("%s: Invalid message type %d\n", 12186 __func__, io->io_hdr.msg_type); 12187 ctl_free_io(io); 12188 break; 12189 } 12190 12191} 12192 12193 12194/* 12195 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12196 * there is no match. 12197 */ 12198static ctl_lun_error_pattern 12199ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12200{ 12201 const struct ctl_cmd_entry *entry; 12202 ctl_lun_error_pattern filtered_pattern, pattern; 12203 12204 pattern = desc->error_pattern; 12205 12206 /* 12207 * XXX KDM we need more data passed into this function to match a 12208 * custom pattern, and we actually need to implement custom pattern 12209 * matching. 12210 */ 12211 if (pattern & CTL_LUN_PAT_CMD) 12212 return (CTL_LUN_PAT_CMD); 12213 12214 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12215 return (CTL_LUN_PAT_ANY); 12216 12217 entry = ctl_get_cmd_entry(ctsio, NULL); 12218 12219 filtered_pattern = entry->pattern & pattern; 12220 12221 /* 12222 * If the user requested specific flags in the pattern (e.g. 12223 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12224 * flags. 12225 * 12226 * If the user did not specify any flags, it doesn't matter whether 12227 * or not the command supports the flags. 12228 */ 12229 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12230 (pattern & ~CTL_LUN_PAT_MASK)) 12231 return (CTL_LUN_PAT_NONE); 12232 12233 /* 12234 * If the user asked for a range check, see if the requested LBA 12235 * range overlaps with this command's LBA range. 12236 */ 12237 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12238 uint64_t lba1; 12239 uint64_t len1; 12240 ctl_action action; 12241 int retval; 12242 12243 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12244 if (retval != 0) 12245 return (CTL_LUN_PAT_NONE); 12246 12247 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12248 desc->lba_range.len, FALSE); 12249 /* 12250 * A "pass" means that the LBA ranges don't overlap, so 12251 * this doesn't match the user's range criteria. 12252 */ 12253 if (action == CTL_ACTION_PASS) 12254 return (CTL_LUN_PAT_NONE); 12255 } 12256 12257 return (filtered_pattern); 12258} 12259 12260static void 12261ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12262{ 12263 struct ctl_error_desc *desc, *desc2; 12264 12265 mtx_assert(&lun->lun_lock, MA_OWNED); 12266 12267 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12268 ctl_lun_error_pattern pattern; 12269 /* 12270 * Check to see whether this particular command matches 12271 * the pattern in the descriptor. 12272 */ 12273 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12274 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12275 continue; 12276 12277 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12278 case CTL_LUN_INJ_ABORTED: 12279 ctl_set_aborted(&io->scsiio); 12280 break; 12281 case CTL_LUN_INJ_MEDIUM_ERR: 12282 ctl_set_medium_error(&io->scsiio, 12283 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12284 CTL_FLAG_DATA_OUT); 12285 break; 12286 case CTL_LUN_INJ_UA: 12287 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12288 * OCCURRED */ 12289 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12290 break; 12291 case CTL_LUN_INJ_CUSTOM: 12292 /* 12293 * We're assuming the user knows what he is doing. 12294 * Just copy the sense information without doing 12295 * checks. 12296 */ 12297 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12298 MIN(sizeof(desc->custom_sense), 12299 sizeof(io->scsiio.sense_data))); 12300 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12301 io->scsiio.sense_len = SSD_FULL_SIZE; 12302 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12303 break; 12304 case CTL_LUN_INJ_NONE: 12305 default: 12306 /* 12307 * If this is an error injection type we don't know 12308 * about, clear the continuous flag (if it is set) 12309 * so it will get deleted below. 12310 */ 12311 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12312 break; 12313 } 12314 /* 12315 * By default, each error injection action is a one-shot 12316 */ 12317 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12318 continue; 12319 12320 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12321 12322 free(desc, M_CTL); 12323 } 12324} 12325 12326#ifdef CTL_IO_DELAY 12327static void 12328ctl_datamove_timer_wakeup(void *arg) 12329{ 12330 union ctl_io *io; 12331 12332 io = (union ctl_io *)arg; 12333 12334 ctl_datamove(io); 12335} 12336#endif /* CTL_IO_DELAY */ 12337 12338void 12339ctl_datamove(union ctl_io *io) 12340{ 12341 void (*fe_datamove)(union ctl_io *io); 12342 12343 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12344 12345 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12346 12347 /* No data transferred yet. Frontend must update this when done. */ 12348 io->scsiio.kern_data_resid = io->scsiio.kern_data_len; 12349 12350#ifdef CTL_TIME_IO 12351 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12352 char str[256]; 12353 char path_str[64]; 12354 struct sbuf sb; 12355 12356 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12357 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12358 12359 sbuf_cat(&sb, path_str); 12360 switch (io->io_hdr.io_type) { 12361 case CTL_IO_SCSI: 12362 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12363 sbuf_printf(&sb, "\n"); 12364 sbuf_cat(&sb, path_str); 12365 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12366 io->scsiio.tag_num, io->scsiio.tag_type); 12367 break; 12368 case CTL_IO_TASK: 12369 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12370 "Tag Type: %d\n", io->taskio.task_action, 12371 io->taskio.tag_num, io->taskio.tag_type); 12372 break; 12373 default: 12374 panic("%s: Invalid CTL I/O type %d\n", 12375 __func__, io->io_hdr.io_type); 12376 } 12377 sbuf_cat(&sb, path_str); 12378 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12379 (intmax_t)time_uptime - io->io_hdr.start_time); 12380 sbuf_finish(&sb); 12381 printf("%s", sbuf_data(&sb)); 12382 } 12383#endif /* CTL_TIME_IO */ 12384 12385#ifdef CTL_IO_DELAY 12386 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12387 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12388 } else { 12389 struct ctl_lun *lun; 12390 12391 lun = CTL_LUN(io); 12392 if ((lun != NULL) 12393 && (lun->delay_info.datamove_delay > 0)) { 12394 12395 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12396 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12397 callout_reset(&io->io_hdr.delay_callout, 12398 lun->delay_info.datamove_delay * hz, 12399 ctl_datamove_timer_wakeup, io); 12400 if (lun->delay_info.datamove_type == 12401 CTL_DELAY_TYPE_ONESHOT) 12402 lun->delay_info.datamove_delay = 0; 12403 return; 12404 } 12405 } 12406#endif 12407 12408 /* 12409 * This command has been aborted. Set the port status, so we fail 12410 * the data move. 12411 */ 12412 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12413 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12414 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12415 io->io_hdr.nexus.targ_port, 12416 io->io_hdr.nexus.targ_lun); 12417 io->io_hdr.port_status = 31337; 12418 /* 12419 * Note that the backend, in this case, will get the 12420 * callback in its context. In other cases it may get 12421 * called in the frontend's interrupt thread context. 12422 */ 12423 io->scsiio.be_move_done(io); 12424 return; 12425 } 12426 12427 /* Don't confuse frontend with zero length data move. */ 12428 if (io->scsiio.kern_data_len == 0) { 12429 io->scsiio.be_move_done(io); 12430 return; 12431 } 12432 12433 fe_datamove = CTL_PORT(io)->fe_datamove; 12434 fe_datamove(io); 12435} 12436 12437static void 12438ctl_send_datamove_done(union ctl_io *io, int have_lock) 12439{ 12440 union ctl_ha_msg msg; 12441#ifdef CTL_TIME_IO 12442 struct bintime cur_bt; 12443#endif 12444 12445 memset(&msg, 0, sizeof(msg)); 12446 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12447 msg.hdr.original_sc = io; 12448 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12449 msg.hdr.nexus = io->io_hdr.nexus; 12450 msg.hdr.status = io->io_hdr.status; 12451 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid; 12452 msg.scsi.tag_num = io->scsiio.tag_num; 12453 msg.scsi.tag_type = io->scsiio.tag_type; 12454 msg.scsi.scsi_status = io->scsiio.scsi_status; 12455 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12456 io->scsiio.sense_len); 12457 msg.scsi.sense_len = io->scsiio.sense_len; 12458 msg.scsi.port_status = io->io_hdr.port_status; 12459 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12460 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12461 ctl_failover_io(io, /*have_lock*/ have_lock); 12462 return; 12463 } 12464 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12465 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12466 msg.scsi.sense_len, M_WAITOK); 12467 12468#ifdef CTL_TIME_IO 12469 getbinuptime(&cur_bt); 12470 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12471 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12472#endif 12473 io->io_hdr.num_dmas++; 12474} 12475 12476/* 12477 * The DMA to the remote side is done, now we need to tell the other side 12478 * we're done so it can continue with its data movement. 12479 */ 12480static void 12481ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12482{ 12483 union ctl_io *io; 12484 uint32_t i; 12485 12486 io = rq->context; 12487 12488 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12489 printf("%s: ISC DMA write failed with error %d", __func__, 12490 rq->ret); 12491 ctl_set_internal_failure(&io->scsiio, 12492 /*sks_valid*/ 1, 12493 /*retry_count*/ rq->ret); 12494 } 12495 12496 ctl_dt_req_free(rq); 12497 12498 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12499 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12500 free(io->io_hdr.remote_sglist, M_CTL); 12501 io->io_hdr.remote_sglist = NULL; 12502 io->io_hdr.local_sglist = NULL; 12503 12504 /* 12505 * The data is in local and remote memory, so now we need to send 12506 * status (good or back) back to the other side. 12507 */ 12508 ctl_send_datamove_done(io, /*have_lock*/ 0); 12509} 12510 12511/* 12512 * We've moved the data from the host/controller into local memory. Now we 12513 * need to push it over to the remote controller's memory. 12514 */ 12515static int 12516ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12517{ 12518 int retval; 12519 12520 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12521 ctl_datamove_remote_write_cb); 12522 return (retval); 12523} 12524 12525static void 12526ctl_datamove_remote_write(union ctl_io *io) 12527{ 12528 int retval; 12529 void (*fe_datamove)(union ctl_io *io); 12530 12531 /* 12532 * - Get the data from the host/HBA into local memory. 12533 * - DMA memory from the local controller to the remote controller. 12534 * - Send status back to the remote controller. 12535 */ 12536 12537 retval = ctl_datamove_remote_sgl_setup(io); 12538 if (retval != 0) 12539 return; 12540 12541 /* Switch the pointer over so the FETD knows what to do */ 12542 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12543 12544 /* 12545 * Use a custom move done callback, since we need to send completion 12546 * back to the other controller, not to the backend on this side. 12547 */ 12548 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12549 12550 fe_datamove = CTL_PORT(io)->fe_datamove; 12551 fe_datamove(io); 12552} 12553 12554static int 12555ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12556{ 12557#if 0 12558 char str[256]; 12559 char path_str[64]; 12560 struct sbuf sb; 12561#endif 12562 uint32_t i; 12563 12564 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12565 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12566 free(io->io_hdr.remote_sglist, M_CTL); 12567 io->io_hdr.remote_sglist = NULL; 12568 io->io_hdr.local_sglist = NULL; 12569 12570#if 0 12571 scsi_path_string(io, path_str, sizeof(path_str)); 12572 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12573 sbuf_cat(&sb, path_str); 12574 scsi_command_string(&io->scsiio, NULL, &sb); 12575 sbuf_printf(&sb, "\n"); 12576 sbuf_cat(&sb, path_str); 12577 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12578 io->scsiio.tag_num, io->scsiio.tag_type); 12579 sbuf_cat(&sb, path_str); 12580 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12581 io->io_hdr.flags, io->io_hdr.status); 12582 sbuf_finish(&sb); 12583 printk("%s", sbuf_data(&sb)); 12584#endif 12585 12586 12587 /* 12588 * The read is done, now we need to send status (good or bad) back 12589 * to the other side. 12590 */ 12591 ctl_send_datamove_done(io, /*have_lock*/ 0); 12592 12593 return (0); 12594} 12595 12596static void 12597ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12598{ 12599 union ctl_io *io; 12600 void (*fe_datamove)(union ctl_io *io); 12601 12602 io = rq->context; 12603 12604 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12605 printf("%s: ISC DMA read failed with error %d\n", __func__, 12606 rq->ret); 12607 ctl_set_internal_failure(&io->scsiio, 12608 /*sks_valid*/ 1, 12609 /*retry_count*/ rq->ret); 12610 } 12611 12612 ctl_dt_req_free(rq); 12613 12614 /* Switch the pointer over so the FETD knows what to do */ 12615 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12616 12617 /* 12618 * Use a custom move done callback, since we need to send completion 12619 * back to the other controller, not to the backend on this side. 12620 */ 12621 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12622 12623 /* XXX KDM add checks like the ones in ctl_datamove? */ 12624 12625 fe_datamove = CTL_PORT(io)->fe_datamove; 12626 fe_datamove(io); 12627} 12628 12629static int 12630ctl_datamove_remote_sgl_setup(union ctl_io *io) 12631{ 12632 struct ctl_sg_entry *local_sglist; 12633 uint32_t len_to_go; 12634 int retval; 12635 int i; 12636 12637 retval = 0; 12638 local_sglist = io->io_hdr.local_sglist; 12639 len_to_go = io->scsiio.kern_data_len; 12640 12641 /* 12642 * The difficult thing here is that the size of the various 12643 * S/G segments may be different than the size from the 12644 * remote controller. That'll make it harder when DMAing 12645 * the data back to the other side. 12646 */ 12647 for (i = 0; len_to_go > 0; i++) { 12648 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12649 local_sglist[i].addr = 12650 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12651 12652 len_to_go -= local_sglist[i].len; 12653 } 12654 /* 12655 * Reset the number of S/G entries accordingly. The original 12656 * number of S/G entries is available in rem_sg_entries. 12657 */ 12658 io->scsiio.kern_sg_entries = i; 12659 12660#if 0 12661 printf("%s: kern_sg_entries = %d\n", __func__, 12662 io->scsiio.kern_sg_entries); 12663 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12664 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12665 local_sglist[i].addr, local_sglist[i].len); 12666#endif 12667 12668 return (retval); 12669} 12670 12671static int 12672ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12673 ctl_ha_dt_cb callback) 12674{ 12675 struct ctl_ha_dt_req *rq; 12676 struct ctl_sg_entry *remote_sglist, *local_sglist; 12677 uint32_t local_used, remote_used, total_used; 12678 int i, j, isc_ret; 12679 12680 rq = ctl_dt_req_alloc(); 12681 12682 /* 12683 * If we failed to allocate the request, and if the DMA didn't fail 12684 * anyway, set busy status. This is just a resource allocation 12685 * failure. 12686 */ 12687 if ((rq == NULL) 12688 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12689 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12690 ctl_set_busy(&io->scsiio); 12691 12692 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12693 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12694 12695 if (rq != NULL) 12696 ctl_dt_req_free(rq); 12697 12698 /* 12699 * The data move failed. We need to return status back 12700 * to the other controller. No point in trying to DMA 12701 * data to the remote controller. 12702 */ 12703 12704 ctl_send_datamove_done(io, /*have_lock*/ 0); 12705 12706 return (1); 12707 } 12708 12709 local_sglist = io->io_hdr.local_sglist; 12710 remote_sglist = io->io_hdr.remote_sglist; 12711 local_used = 0; 12712 remote_used = 0; 12713 total_used = 0; 12714 12715 /* 12716 * Pull/push the data over the wire from/to the other controller. 12717 * This takes into account the possibility that the local and 12718 * remote sglists may not be identical in terms of the size of 12719 * the elements and the number of elements. 12720 * 12721 * One fundamental assumption here is that the length allocated for 12722 * both the local and remote sglists is identical. Otherwise, we've 12723 * essentially got a coding error of some sort. 12724 */ 12725 isc_ret = CTL_HA_STATUS_SUCCESS; 12726 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12727 uint32_t cur_len; 12728 uint8_t *tmp_ptr; 12729 12730 rq->command = command; 12731 rq->context = io; 12732 12733 /* 12734 * Both pointers should be aligned. But it is possible 12735 * that the allocation length is not. They should both 12736 * also have enough slack left over at the end, though, 12737 * to round up to the next 8 byte boundary. 12738 */ 12739 cur_len = MIN(local_sglist[i].len - local_used, 12740 remote_sglist[j].len - remote_used); 12741 rq->size = cur_len; 12742 12743 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12744 tmp_ptr += local_used; 12745 12746#if 0 12747 /* Use physical addresses when talking to ISC hardware */ 12748 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12749 /* XXX KDM use busdma */ 12750 rq->local = vtophys(tmp_ptr); 12751 } else 12752 rq->local = tmp_ptr; 12753#else 12754 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12755 ("HA does not support BUS_ADDR")); 12756 rq->local = tmp_ptr; 12757#endif 12758 12759 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12760 tmp_ptr += remote_used; 12761 rq->remote = tmp_ptr; 12762 12763 rq->callback = NULL; 12764 12765 local_used += cur_len; 12766 if (local_used >= local_sglist[i].len) { 12767 i++; 12768 local_used = 0; 12769 } 12770 12771 remote_used += cur_len; 12772 if (remote_used >= remote_sglist[j].len) { 12773 j++; 12774 remote_used = 0; 12775 } 12776 total_used += cur_len; 12777 12778 if (total_used >= io->scsiio.kern_data_len) 12779 rq->callback = callback; 12780 12781#if 0 12782 printf("%s: %s: local %p remote %p size %d\n", __func__, 12783 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12784 rq->local, rq->remote, rq->size); 12785#endif 12786 12787 isc_ret = ctl_dt_single(rq); 12788 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12789 break; 12790 } 12791 if (isc_ret != CTL_HA_STATUS_WAIT) { 12792 rq->ret = isc_ret; 12793 callback(rq); 12794 } 12795 12796 return (0); 12797} 12798 12799static void 12800ctl_datamove_remote_read(union ctl_io *io) 12801{ 12802 int retval; 12803 uint32_t i; 12804 12805 /* 12806 * This will send an error to the other controller in the case of a 12807 * failure. 12808 */ 12809 retval = ctl_datamove_remote_sgl_setup(io); 12810 if (retval != 0) 12811 return; 12812 12813 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12814 ctl_datamove_remote_read_cb); 12815 if (retval != 0) { 12816 /* 12817 * Make sure we free memory if there was an error.. The 12818 * ctl_datamove_remote_xfer() function will send the 12819 * datamove done message, or call the callback with an 12820 * error if there is a problem. 12821 */ 12822 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12823 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12824 free(io->io_hdr.remote_sglist, M_CTL); 12825 io->io_hdr.remote_sglist = NULL; 12826 io->io_hdr.local_sglist = NULL; 12827 } 12828} 12829 12830/* 12831 * Process a datamove request from the other controller. This is used for 12832 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12833 * first. Once that is complete, the data gets DMAed into the remote 12834 * controller's memory. For reads, we DMA from the remote controller's 12835 * memory into our memory first, and then move it out to the FETD. 12836 */ 12837static void 12838ctl_datamove_remote(union ctl_io *io) 12839{ 12840 12841 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12842 12843 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12844 ctl_failover_io(io, /*have_lock*/ 0); 12845 return; 12846 } 12847 12848 /* 12849 * Note that we look for an aborted I/O here, but don't do some of 12850 * the other checks that ctl_datamove() normally does. 12851 * We don't need to run the datamove delay code, since that should 12852 * have been done if need be on the other controller. 12853 */ 12854 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12855 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12856 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12857 io->io_hdr.nexus.targ_port, 12858 io->io_hdr.nexus.targ_lun); 12859 io->io_hdr.port_status = 31338; 12860 ctl_send_datamove_done(io, /*have_lock*/ 0); 12861 return; 12862 } 12863 12864 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12865 ctl_datamove_remote_write(io); 12866 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12867 ctl_datamove_remote_read(io); 12868 else { 12869 io->io_hdr.port_status = 31339; 12870 ctl_send_datamove_done(io, /*have_lock*/ 0); 12871 } 12872} 12873 12874static void 12875ctl_process_done(union ctl_io *io) 12876{ 12877 struct ctl_softc *softc = CTL_SOFTC(io); 12878 struct ctl_port *port = CTL_PORT(io); 12879 struct ctl_lun *lun = CTL_LUN(io); 12880 void (*fe_done)(union ctl_io *io); 12881 union ctl_ha_msg msg; 12882 12883 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12884 fe_done = port->fe_done; 12885 12886#ifdef CTL_TIME_IO 12887 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12888 char str[256]; 12889 char path_str[64]; 12890 struct sbuf sb; 12891 12892 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12893 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12894 12895 sbuf_cat(&sb, path_str); 12896 switch (io->io_hdr.io_type) { 12897 case CTL_IO_SCSI: 12898 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12899 sbuf_printf(&sb, "\n"); 12900 sbuf_cat(&sb, path_str); 12901 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12902 io->scsiio.tag_num, io->scsiio.tag_type); 12903 break; 12904 case CTL_IO_TASK: 12905 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12906 "Tag Type: %d\n", io->taskio.task_action, 12907 io->taskio.tag_num, io->taskio.tag_type); 12908 break; 12909 default: 12910 panic("%s: Invalid CTL I/O type %d\n", 12911 __func__, io->io_hdr.io_type); 12912 } 12913 sbuf_cat(&sb, path_str); 12914 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12915 (intmax_t)time_uptime - io->io_hdr.start_time); 12916 sbuf_finish(&sb); 12917 printf("%s", sbuf_data(&sb)); 12918 } 12919#endif /* CTL_TIME_IO */ 12920 12921 switch (io->io_hdr.io_type) { 12922 case CTL_IO_SCSI: 12923 break; 12924 case CTL_IO_TASK: 12925 if (ctl_debug & CTL_DEBUG_INFO) 12926 ctl_io_error_print(io, NULL); 12927 fe_done(io); 12928 return; 12929 default: 12930 panic("%s: Invalid CTL I/O type %d\n", 12931 __func__, io->io_hdr.io_type); 12932 } 12933 12934 if (lun == NULL) { 12935 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12936 io->io_hdr.nexus.targ_mapped_lun)); 12937 goto bailout; 12938 } 12939 12940 mtx_lock(&lun->lun_lock); 12941 12942 /* 12943 * Check to see if we have any informational exception and status 12944 * of this command can be modified to report it in form of either 12945 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 12946 */ 12947 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 12948 io->io_hdr.status == CTL_SUCCESS && 12949 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 12950 uint8_t mrie = lun->MODE_IE.mrie; 12951 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 12952 (lun->MODE_VER.byte3 & SMS_VER_PER)); 12953 if (((mrie == SIEP_MRIE_REC_COND && per) || 12954 mrie == SIEP_MRIE_REC_UNCOND || 12955 mrie == SIEP_MRIE_NO_SENSE) && 12956 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 12957 CTL_CMD_FLAG_NO_SENSE) == 0) { 12958 ctl_set_sense(&io->scsiio, 12959 /*current_error*/ 1, 12960 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 12961 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 12962 /*asc*/ lun->ie_asc, 12963 /*ascq*/ lun->ie_ascq, 12964 SSD_ELEM_NONE); 12965 lun->ie_reported = 1; 12966 } 12967 } else if (lun->ie_reported < 0) 12968 lun->ie_reported = 0; 12969 12970 /* 12971 * Check to see if we have any errors to inject here. We only 12972 * inject errors for commands that don't already have errors set. 12973 */ 12974 if (!STAILQ_EMPTY(&lun->error_list) && 12975 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 12976 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 12977 ctl_inject_error(lun, io); 12978 12979 /* 12980 * XXX KDM how do we treat commands that aren't completed 12981 * successfully? 12982 * 12983 * XXX KDM should we also track I/O latency? 12984 */ 12985 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 12986 io->io_hdr.io_type == CTL_IO_SCSI) { 12987 int type; 12988#ifdef CTL_TIME_IO 12989 struct bintime bt; 12990 12991 getbinuptime(&bt); 12992 bintime_sub(&bt, &io->io_hdr.start_bt); 12993#endif 12994 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12995 CTL_FLAG_DATA_IN) 12996 type = CTL_STATS_READ; 12997 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12998 CTL_FLAG_DATA_OUT) 12999 type = CTL_STATS_WRITE; 13000 else 13001 type = CTL_STATS_NO_IO; 13002 13003#ifdef CTL_LEGACY_STATS 13004 uint32_t targ_port = port->targ_port; 13005 lun->legacy_stats.ports[targ_port].bytes[type] += 13006 io->scsiio.kern_total_len; 13007 lun->legacy_stats.ports[targ_port].operations[type] ++; 13008 lun->legacy_stats.ports[targ_port].num_dmas[type] += 13009 io->io_hdr.num_dmas; 13010#ifdef CTL_TIME_IO 13011 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type], 13012 &io->io_hdr.dma_bt); 13013 bintime_add(&lun->legacy_stats.ports[targ_port].time[type], 13014 &bt); 13015#endif 13016#endif /* CTL_LEGACY_STATS */ 13017 13018 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13019 lun->stats.operations[type] ++; 13020 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13021#ifdef CTL_TIME_IO 13022 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13023 bintime_add(&lun->stats.time[type], &bt); 13024#endif 13025 13026 mtx_lock(&port->port_lock); 13027 port->stats.bytes[type] += io->scsiio.kern_total_len; 13028 port->stats.operations[type] ++; 13029 port->stats.dmas[type] += io->io_hdr.num_dmas; 13030#ifdef CTL_TIME_IO 13031 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13032 bintime_add(&port->stats.time[type], &bt); 13033#endif 13034 mtx_unlock(&port->port_lock); 13035 } 13036 13037 /* 13038 * Remove this from the OOA queue. 13039 */ 13040 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13041#ifdef CTL_TIME_IO 13042 if (TAILQ_EMPTY(&lun->ooa_queue)) 13043 lun->last_busy = getsbinuptime(); 13044#endif 13045 13046 /* 13047 * Run through the blocked queue on this LUN and see if anything 13048 * has become unblocked, now that this transaction is done. 13049 */ 13050 ctl_check_blocked(lun); 13051 13052 /* 13053 * If the LUN has been invalidated, free it if there is nothing 13054 * left on its OOA queue. 13055 */ 13056 if ((lun->flags & CTL_LUN_INVALID) 13057 && TAILQ_EMPTY(&lun->ooa_queue)) { 13058 mtx_unlock(&lun->lun_lock); 13059 mtx_lock(&softc->ctl_lock); 13060 ctl_free_lun(lun); 13061 mtx_unlock(&softc->ctl_lock); 13062 } else 13063 mtx_unlock(&lun->lun_lock); 13064 13065bailout: 13066 13067 /* 13068 * If this command has been aborted, make sure we set the status 13069 * properly. The FETD is responsible for freeing the I/O and doing 13070 * whatever it needs to do to clean up its state. 13071 */ 13072 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13073 ctl_set_task_aborted(&io->scsiio); 13074 13075 /* 13076 * If enabled, print command error status. 13077 */ 13078 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13079 (ctl_debug & CTL_DEBUG_INFO) != 0) 13080 ctl_io_error_print(io, NULL); 13081 13082 /* 13083 * Tell the FETD or the other shelf controller we're done with this 13084 * command. Note that only SCSI commands get to this point. Task 13085 * management commands are completed above. 13086 */ 13087 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13088 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13089 memset(&msg, 0, sizeof(msg)); 13090 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13091 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13092 msg.hdr.nexus = io->io_hdr.nexus; 13093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13094 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13095 M_WAITOK); 13096 } 13097 13098 fe_done(io); 13099} 13100 13101/* 13102 * Front end should call this if it doesn't do autosense. When the request 13103 * sense comes back in from the initiator, we'll dequeue this and send it. 13104 */ 13105int 13106ctl_queue_sense(union ctl_io *io) 13107{ 13108 struct ctl_softc *softc = CTL_SOFTC(io); 13109 struct ctl_port *port = CTL_PORT(io); 13110 struct ctl_lun *lun; 13111 struct scsi_sense_data *ps; 13112 uint32_t initidx, p, targ_lun; 13113 13114 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13115 13116 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13117 13118 /* 13119 * LUN lookup will likely move to the ctl_work_thread() once we 13120 * have our new queueing infrastructure (that doesn't put things on 13121 * a per-LUN queue initially). That is so that we can handle 13122 * things like an INQUIRY to a LUN that we don't have enabled. We 13123 * can't deal with that right now. 13124 * If we don't have a LUN for this, just toss the sense information. 13125 */ 13126 mtx_lock(&softc->ctl_lock); 13127 if (targ_lun >= CTL_MAX_LUNS || 13128 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13129 mtx_unlock(&softc->ctl_lock); 13130 goto bailout; 13131 } 13132 mtx_lock(&lun->lun_lock); 13133 mtx_unlock(&softc->ctl_lock); 13134 13135 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13136 p = initidx / CTL_MAX_INIT_PER_PORT; 13137 if (lun->pending_sense[p] == NULL) { 13138 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT, 13139 M_CTL, M_NOWAIT | M_ZERO); 13140 } 13141 if ((ps = lun->pending_sense[p]) != NULL) { 13142 ps += initidx % CTL_MAX_INIT_PER_PORT; 13143 memset(ps, 0, sizeof(*ps)); 13144 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len); 13145 } 13146 mtx_unlock(&lun->lun_lock); 13147 13148bailout: 13149 ctl_free_io(io); 13150 return (CTL_RETVAL_COMPLETE); 13151} 13152 13153/* 13154 * Primary command inlet from frontend ports. All SCSI and task I/O 13155 * requests must go through this function. 13156 */ 13157int 13158ctl_queue(union ctl_io *io) 13159{ 13160 struct ctl_port *port = CTL_PORT(io); 13161 13162 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13163 13164#ifdef CTL_TIME_IO 13165 io->io_hdr.start_time = time_uptime; 13166 getbinuptime(&io->io_hdr.start_bt); 13167#endif /* CTL_TIME_IO */ 13168 13169 /* Map FE-specific LUN ID into global one. */ 13170 io->io_hdr.nexus.targ_mapped_lun = 13171 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13172 13173 switch (io->io_hdr.io_type) { 13174 case CTL_IO_SCSI: 13175 case CTL_IO_TASK: 13176 if (ctl_debug & CTL_DEBUG_CDB) 13177 ctl_io_print(io); 13178 ctl_enqueue_incoming(io); 13179 break; 13180 default: 13181 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13182 return (EINVAL); 13183 } 13184 13185 return (CTL_RETVAL_COMPLETE); 13186} 13187 13188#ifdef CTL_IO_DELAY 13189static void 13190ctl_done_timer_wakeup(void *arg) 13191{ 13192 union ctl_io *io; 13193 13194 io = (union ctl_io *)arg; 13195 ctl_done(io); 13196} 13197#endif /* CTL_IO_DELAY */ 13198 13199void 13200ctl_serseq_done(union ctl_io *io) 13201{ 13202 struct ctl_lun *lun = CTL_LUN(io);; 13203 13204 if (lun->be_lun == NULL || 13205 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13206 return; 13207 mtx_lock(&lun->lun_lock); 13208 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13209 ctl_check_blocked(lun); 13210 mtx_unlock(&lun->lun_lock); 13211} 13212 13213void 13214ctl_done(union ctl_io *io) 13215{ 13216 13217 /* 13218 * Enable this to catch duplicate completion issues. 13219 */ 13220#if 0 13221 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13222 printf("%s: type %d msg %d cdb %x iptl: " 13223 "%u:%u:%u tag 0x%04x " 13224 "flag %#x status %x\n", 13225 __func__, 13226 io->io_hdr.io_type, 13227 io->io_hdr.msg_type, 13228 io->scsiio.cdb[0], 13229 io->io_hdr.nexus.initid, 13230 io->io_hdr.nexus.targ_port, 13231 io->io_hdr.nexus.targ_lun, 13232 (io->io_hdr.io_type == 13233 CTL_IO_TASK) ? 13234 io->taskio.tag_num : 13235 io->scsiio.tag_num, 13236 io->io_hdr.flags, 13237 io->io_hdr.status); 13238 } else 13239 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13240#endif 13241 13242 /* 13243 * This is an internal copy of an I/O, and should not go through 13244 * the normal done processing logic. 13245 */ 13246 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13247 return; 13248 13249#ifdef CTL_IO_DELAY 13250 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13251 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13252 } else { 13253 struct ctl_lun *lun = CTL_LUN(io); 13254 13255 if ((lun != NULL) 13256 && (lun->delay_info.done_delay > 0)) { 13257 13258 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13259 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13260 callout_reset(&io->io_hdr.delay_callout, 13261 lun->delay_info.done_delay * hz, 13262 ctl_done_timer_wakeup, io); 13263 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13264 lun->delay_info.done_delay = 0; 13265 return; 13266 } 13267 } 13268#endif /* CTL_IO_DELAY */ 13269 13270 ctl_enqueue_done(io); 13271} 13272 13273static void 13274ctl_work_thread(void *arg) 13275{ 13276 struct ctl_thread *thr = (struct ctl_thread *)arg; 13277 struct ctl_softc *softc = thr->ctl_softc; 13278 union ctl_io *io; 13279 int retval; 13280 13281 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13282 13283 while (!softc->shutdown) { 13284 /* 13285 * We handle the queues in this order: 13286 * - ISC 13287 * - done queue (to free up resources, unblock other commands) 13288 * - RtR queue 13289 * - incoming queue 13290 * 13291 * If those queues are empty, we break out of the loop and 13292 * go to sleep. 13293 */ 13294 mtx_lock(&thr->queue_lock); 13295 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13296 if (io != NULL) { 13297 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13298 mtx_unlock(&thr->queue_lock); 13299 ctl_handle_isc(io); 13300 continue; 13301 } 13302 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13303 if (io != NULL) { 13304 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13305 /* clear any blocked commands, call fe_done */ 13306 mtx_unlock(&thr->queue_lock); 13307 ctl_process_done(io); 13308 continue; 13309 } 13310 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13311 if (io != NULL) { 13312 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13313 mtx_unlock(&thr->queue_lock); 13314 if (io->io_hdr.io_type == CTL_IO_TASK) 13315 ctl_run_task(io); 13316 else 13317 ctl_scsiio_precheck(softc, &io->scsiio); 13318 continue; 13319 } 13320 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13321 if (io != NULL) { 13322 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13323 mtx_unlock(&thr->queue_lock); 13324 retval = ctl_scsiio(&io->scsiio); 13325 if (retval != CTL_RETVAL_COMPLETE) 13326 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13327 continue; 13328 } 13329 13330 /* Sleep until we have something to do. */ 13331 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13332 } 13333 thr->thread = NULL; 13334 kthread_exit(); 13335} 13336 13337static void 13338ctl_lun_thread(void *arg) 13339{ 13340 struct ctl_softc *softc = (struct ctl_softc *)arg; 13341 struct ctl_be_lun *be_lun; 13342 13343 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13344 13345 while (!softc->shutdown) { 13346 mtx_lock(&softc->ctl_lock); 13347 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13348 if (be_lun != NULL) { 13349 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13350 mtx_unlock(&softc->ctl_lock); 13351 ctl_create_lun(be_lun); 13352 continue; 13353 } 13354 13355 /* Sleep until we have something to do. */ 13356 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13357 PDROP | PRIBIO, "-", 0); 13358 } 13359 softc->lun_thread = NULL; 13360 kthread_exit(); 13361} 13362 13363static void 13364ctl_thresh_thread(void *arg) 13365{ 13366 struct ctl_softc *softc = (struct ctl_softc *)arg; 13367 struct ctl_lun *lun; 13368 struct ctl_logical_block_provisioning_page *page; 13369 const char *attr; 13370 union ctl_ha_msg msg; 13371 uint64_t thres, val; 13372 int i, e, set; 13373 13374 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13375 13376 while (!softc->shutdown) { 13377 mtx_lock(&softc->ctl_lock); 13378 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13379 if ((lun->flags & CTL_LUN_DISABLED) || 13380 (lun->flags & CTL_LUN_NO_MEDIA) || 13381 lun->backend->lun_attr == NULL) 13382 continue; 13383 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13384 softc->ha_mode == CTL_HA_MODE_XFER) 13385 continue; 13386 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13387 continue; 13388 e = 0; 13389 page = &lun->MODE_LBP; 13390 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13391 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13392 continue; 13393 thres = scsi_4btoul(page->descr[i].count); 13394 thres <<= CTL_LBP_EXPONENT; 13395 switch (page->descr[i].resource) { 13396 case 0x01: 13397 attr = "blocksavail"; 13398 break; 13399 case 0x02: 13400 attr = "blocksused"; 13401 break; 13402 case 0xf1: 13403 attr = "poolblocksavail"; 13404 break; 13405 case 0xf2: 13406 attr = "poolblocksused"; 13407 break; 13408 default: 13409 continue; 13410 } 13411 mtx_unlock(&softc->ctl_lock); // XXX 13412 val = lun->backend->lun_attr( 13413 lun->be_lun->be_lun, attr); 13414 mtx_lock(&softc->ctl_lock); 13415 if (val == UINT64_MAX) 13416 continue; 13417 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13418 == SLBPPD_ARMING_INC) 13419 e = (val >= thres); 13420 else 13421 e = (val <= thres); 13422 if (e) 13423 break; 13424 } 13425 mtx_lock(&lun->lun_lock); 13426 if (e) { 13427 scsi_u64to8b((uint8_t *)&page->descr[i] - 13428 (uint8_t *)page, lun->ua_tpt_info); 13429 if (lun->lasttpt == 0 || 13430 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13431 lun->lasttpt = time_uptime; 13432 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13433 set = 1; 13434 } else 13435 set = 0; 13436 } else { 13437 lun->lasttpt = 0; 13438 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13439 set = -1; 13440 } 13441 mtx_unlock(&lun->lun_lock); 13442 if (set != 0 && 13443 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13444 /* Send msg to other side. */ 13445 bzero(&msg.ua, sizeof(msg.ua)); 13446 msg.hdr.msg_type = CTL_MSG_UA; 13447 msg.hdr.nexus.initid = -1; 13448 msg.hdr.nexus.targ_port = -1; 13449 msg.hdr.nexus.targ_lun = lun->lun; 13450 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13451 msg.ua.ua_all = 1; 13452 msg.ua.ua_set = (set > 0); 13453 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13454 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13455 mtx_unlock(&softc->ctl_lock); // XXX 13456 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13457 sizeof(msg.ua), M_WAITOK); 13458 mtx_lock(&softc->ctl_lock); 13459 } 13460 } 13461 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock, 13462 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz); 13463 } 13464 softc->thresh_thread = NULL; 13465 kthread_exit(); 13466} 13467 13468static void 13469ctl_enqueue_incoming(union ctl_io *io) 13470{ 13471 struct ctl_softc *softc = CTL_SOFTC(io); 13472 struct ctl_thread *thr; 13473 u_int idx; 13474 13475 idx = (io->io_hdr.nexus.targ_port * 127 + 13476 io->io_hdr.nexus.initid) % worker_threads; 13477 thr = &softc->threads[idx]; 13478 mtx_lock(&thr->queue_lock); 13479 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13480 mtx_unlock(&thr->queue_lock); 13481 wakeup(thr); 13482} 13483 13484static void 13485ctl_enqueue_rtr(union ctl_io *io) 13486{ 13487 struct ctl_softc *softc = CTL_SOFTC(io); 13488 struct ctl_thread *thr; 13489 13490 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13491 mtx_lock(&thr->queue_lock); 13492 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13493 mtx_unlock(&thr->queue_lock); 13494 wakeup(thr); 13495} 13496 13497static void 13498ctl_enqueue_done(union ctl_io *io) 13499{ 13500 struct ctl_softc *softc = CTL_SOFTC(io); 13501 struct ctl_thread *thr; 13502 13503 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13504 mtx_lock(&thr->queue_lock); 13505 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13506 mtx_unlock(&thr->queue_lock); 13507 wakeup(thr); 13508} 13509 13510static void 13511ctl_enqueue_isc(union ctl_io *io) 13512{ 13513 struct ctl_softc *softc = CTL_SOFTC(io); 13514 struct ctl_thread *thr; 13515 13516 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13517 mtx_lock(&thr->queue_lock); 13518 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13519 mtx_unlock(&thr->queue_lock); 13520 wakeup(thr); 13521} 13522 13523/* 13524 * vim: ts=8 13525 */ 13526