ctl.c revision 312581
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 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 312581 2017-01-21 08:37:53Z 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); 429void 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); 442static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 443 444static int ctl_do_mode_select(union ctl_io *io); 445static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 446 uint64_t res_key, uint64_t sa_res_key, 447 uint8_t type, uint32_t residx, 448 struct ctl_scsiio *ctsio, 449 struct scsi_per_res_out *cdb, 450 struct scsi_per_res_out_parms* param); 451static void ctl_pro_preempt_other(struct ctl_lun *lun, 452 union ctl_ha_msg *msg); 453static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 454static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 455static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 456static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 457static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 458static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 459static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 460 int alloc_len); 461static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 462 int alloc_len); 463static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 464static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 465static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 466static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 467static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 468static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 469 bool seq); 470static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 471static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 472 union ctl_io *pending_io, union ctl_io *ooa_io); 473static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 474 union ctl_io *starting_io); 475static int ctl_check_blocked(struct ctl_lun *lun); 476static int ctl_scsiio_lun_check(struct ctl_lun *lun, 477 const struct ctl_cmd_entry *entry, 478 struct ctl_scsiio *ctsio); 479static void ctl_failover_lun(union ctl_io *io); 480static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 481 struct ctl_scsiio *ctsio); 482static int ctl_scsiio(struct ctl_scsiio *ctsio); 483 484static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 485static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 486 ctl_ua_type ua_type); 487static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 488 ctl_ua_type ua_type); 489static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 490static int ctl_abort_task(union ctl_io *io); 491static int ctl_abort_task_set(union ctl_io *io); 492static int ctl_query_task(union ctl_io *io, int task_set); 493static int ctl_i_t_nexus_reset(union ctl_io *io); 494static int ctl_query_async_event(union ctl_io *io); 495static void ctl_run_task(union ctl_io *io); 496#ifdef CTL_IO_DELAY 497static void ctl_datamove_timer_wakeup(void *arg); 498static void ctl_done_timer_wakeup(void *arg); 499#endif /* CTL_IO_DELAY */ 500 501static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 502static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 503static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 504static void ctl_datamove_remote_write(union ctl_io *io); 505static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 506static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 507static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 508static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 509 ctl_ha_dt_cb callback); 510static void ctl_datamove_remote_read(union ctl_io *io); 511static void ctl_datamove_remote(union ctl_io *io); 512static void ctl_process_done(union ctl_io *io); 513static void ctl_lun_thread(void *arg); 514static void ctl_thresh_thread(void *arg); 515static void ctl_work_thread(void *arg); 516static void ctl_enqueue_incoming(union ctl_io *io); 517static void ctl_enqueue_rtr(union ctl_io *io); 518static void ctl_enqueue_done(union ctl_io *io); 519static void ctl_enqueue_isc(union ctl_io *io); 520static const struct ctl_cmd_entry * 521 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 522static const struct ctl_cmd_entry * 523 ctl_validate_command(struct ctl_scsiio *ctsio); 524static int ctl_cmd_applicable(uint8_t lun_type, 525 const struct ctl_cmd_entry *entry); 526 527static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 528static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 529static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 530static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 531 532/* 533 * Load the serialization table. This isn't very pretty, but is probably 534 * the easiest way to do it. 535 */ 536#include "ctl_ser_table.c" 537 538/* 539 * We only need to define open, close and ioctl routines for this driver. 540 */ 541static struct cdevsw ctl_cdevsw = { 542 .d_version = D_VERSION, 543 .d_flags = 0, 544 .d_open = ctl_open, 545 .d_close = ctl_close, 546 .d_ioctl = ctl_ioctl, 547 .d_name = "ctl", 548}; 549 550 551MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 552 553static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 554 555static moduledata_t ctl_moduledata = { 556 "ctl", 557 ctl_module_event_handler, 558 NULL 559}; 560 561DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 562MODULE_VERSION(ctl, 1); 563 564static struct ctl_frontend ha_frontend = 565{ 566 .name = "ha", 567}; 568 569static void 570ctl_ha_datamove(union ctl_io *io) 571{ 572 struct ctl_lun *lun; 573 struct ctl_sg_entry *sgl; 574 union ctl_ha_msg msg; 575 uint32_t sg_entries_sent; 576 int do_sg_copy, i, j; 577 578 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 579 memset(&msg.dt, 0, sizeof(msg.dt)); 580 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 581 msg.hdr.original_sc = io->io_hdr.original_sc; 582 msg.hdr.serializing_sc = io; 583 msg.hdr.nexus = io->io_hdr.nexus; 584 msg.hdr.status = io->io_hdr.status; 585 msg.dt.flags = io->io_hdr.flags; 586 587 /* 588 * We convert everything into a S/G list here. We can't 589 * pass by reference, only by value between controllers. 590 * So we can't pass a pointer to the S/G list, only as many 591 * S/G entries as we can fit in here. If it's possible for 592 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 593 * then we need to break this up into multiple transfers. 594 */ 595 if (io->scsiio.kern_sg_entries == 0) { 596 msg.dt.kern_sg_entries = 1; 597#if 0 598 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 599 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 600 } else { 601 /* XXX KDM use busdma here! */ 602 msg.dt.sg_list[0].addr = 603 (void *)vtophys(io->scsiio.kern_data_ptr); 604 } 605#else 606 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 607 ("HA does not support BUS_ADDR")); 608 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 609#endif 610 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 611 do_sg_copy = 0; 612 } else { 613 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 614 do_sg_copy = 1; 615 } 616 617 msg.dt.kern_data_len = io->scsiio.kern_data_len; 618 msg.dt.kern_total_len = io->scsiio.kern_total_len; 619 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 620 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 621 msg.dt.sg_sequence = 0; 622 623 /* 624 * Loop until we've sent all of the S/G entries. On the 625 * other end, we'll recompose these S/G entries into one 626 * contiguous list before processing. 627 */ 628 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 629 msg.dt.sg_sequence++) { 630 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 631 sizeof(msg.dt.sg_list[0])), 632 msg.dt.kern_sg_entries - sg_entries_sent); 633 if (do_sg_copy != 0) { 634 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 635 for (i = sg_entries_sent, j = 0; 636 i < msg.dt.cur_sg_entries; i++, j++) { 637#if 0 638 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 639 msg.dt.sg_list[j].addr = sgl[i].addr; 640 } else { 641 /* XXX KDM use busdma here! */ 642 msg.dt.sg_list[j].addr = 643 (void *)vtophys(sgl[i].addr); 644 } 645#else 646 KASSERT((io->io_hdr.flags & 647 CTL_FLAG_BUS_ADDR) == 0, 648 ("HA does not support BUS_ADDR")); 649 msg.dt.sg_list[j].addr = sgl[i].addr; 650#endif 651 msg.dt.sg_list[j].len = sgl[i].len; 652 } 653 } 654 655 sg_entries_sent += msg.dt.cur_sg_entries; 656 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 657 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 658 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 659 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 660 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 661 io->io_hdr.port_status = 31341; 662 io->scsiio.be_move_done(io); 663 return; 664 } 665 msg.dt.sent_sg_entries = sg_entries_sent; 666 } 667 668 /* 669 * Officially handover the request from us to peer. 670 * If failover has just happened, then we must return error. 671 * If failover happen just after, then it is not our problem. 672 */ 673 if (lun) 674 mtx_lock(&lun->lun_lock); 675 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 676 if (lun) 677 mtx_unlock(&lun->lun_lock); 678 io->io_hdr.port_status = 31342; 679 io->scsiio.be_move_done(io); 680 return; 681 } 682 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 683 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 684 if (lun) 685 mtx_unlock(&lun->lun_lock); 686} 687 688static void 689ctl_ha_done(union ctl_io *io) 690{ 691 union ctl_ha_msg msg; 692 693 if (io->io_hdr.io_type == CTL_IO_SCSI) { 694 memset(&msg, 0, sizeof(msg)); 695 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 696 msg.hdr.original_sc = io->io_hdr.original_sc; 697 msg.hdr.nexus = io->io_hdr.nexus; 698 msg.hdr.status = io->io_hdr.status; 699 msg.scsi.scsi_status = io->scsiio.scsi_status; 700 msg.scsi.tag_num = io->scsiio.tag_num; 701 msg.scsi.tag_type = io->scsiio.tag_type; 702 msg.scsi.sense_len = io->scsiio.sense_len; 703 msg.scsi.sense_residual = io->scsiio.sense_residual; 704 msg.scsi.residual = io->scsiio.residual; 705 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 706 io->scsiio.sense_len); 707 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 708 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 709 msg.scsi.sense_len, M_WAITOK); 710 } 711 ctl_free_io(io); 712} 713 714static void 715ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 716 union ctl_ha_msg *msg_info) 717{ 718 struct ctl_scsiio *ctsio; 719 720 if (msg_info->hdr.original_sc == NULL) { 721 printf("%s: original_sc == NULL!\n", __func__); 722 /* XXX KDM now what? */ 723 return; 724 } 725 726 ctsio = &msg_info->hdr.original_sc->scsiio; 727 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 728 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 729 ctsio->io_hdr.status = msg_info->hdr.status; 730 ctsio->scsi_status = msg_info->scsi.scsi_status; 731 ctsio->sense_len = msg_info->scsi.sense_len; 732 ctsio->sense_residual = msg_info->scsi.sense_residual; 733 ctsio->residual = msg_info->scsi.residual; 734 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 735 msg_info->scsi.sense_len); 736 ctl_enqueue_isc((union ctl_io *)ctsio); 737} 738 739static void 740ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 741 union ctl_ha_msg *msg_info) 742{ 743 struct ctl_scsiio *ctsio; 744 745 if (msg_info->hdr.serializing_sc == NULL) { 746 printf("%s: serializing_sc == NULL!\n", __func__); 747 /* XXX KDM now what? */ 748 return; 749 } 750 751 ctsio = &msg_info->hdr.serializing_sc->scsiio; 752 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 753 ctl_enqueue_isc((union ctl_io *)ctsio); 754} 755 756void 757ctl_isc_announce_lun(struct ctl_lun *lun) 758{ 759 struct ctl_softc *softc = lun->ctl_softc; 760 union ctl_ha_msg *msg; 761 struct ctl_ha_msg_lun_pr_key pr_key; 762 int i, k; 763 764 if (softc->ha_link != CTL_HA_LINK_ONLINE) 765 return; 766 mtx_lock(&lun->lun_lock); 767 i = sizeof(msg->lun); 768 if (lun->lun_devid) 769 i += lun->lun_devid->len; 770 i += sizeof(pr_key) * lun->pr_key_count; 771alloc: 772 mtx_unlock(&lun->lun_lock); 773 msg = malloc(i, M_CTL, M_WAITOK); 774 mtx_lock(&lun->lun_lock); 775 k = sizeof(msg->lun); 776 if (lun->lun_devid) 777 k += lun->lun_devid->len; 778 k += sizeof(pr_key) * lun->pr_key_count; 779 if (i < k) { 780 free(msg, M_CTL); 781 i = k; 782 goto alloc; 783 } 784 bzero(&msg->lun, sizeof(msg->lun)); 785 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 786 msg->hdr.nexus.targ_lun = lun->lun; 787 msg->hdr.nexus.targ_mapped_lun = lun->lun; 788 msg->lun.flags = lun->flags; 789 msg->lun.pr_generation = lun->pr_generation; 790 msg->lun.pr_res_idx = lun->pr_res_idx; 791 msg->lun.pr_res_type = lun->pr_res_type; 792 msg->lun.pr_key_count = lun->pr_key_count; 793 i = 0; 794 if (lun->lun_devid) { 795 msg->lun.lun_devid_len = lun->lun_devid->len; 796 memcpy(&msg->lun.data[i], lun->lun_devid->data, 797 msg->lun.lun_devid_len); 798 i += msg->lun.lun_devid_len; 799 } 800 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 801 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 802 continue; 803 pr_key.pr_iid = k; 804 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 805 i += sizeof(pr_key); 806 } 807 mtx_unlock(&lun->lun_lock); 808 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 809 M_WAITOK); 810 free(msg, M_CTL); 811 812 if (lun->flags & CTL_LUN_PRIMARY_SC) { 813 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 814 ctl_isc_announce_mode(lun, -1, 815 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 816 lun->mode_pages.index[i].subpage); 817 } 818 } 819} 820 821void 822ctl_isc_announce_port(struct ctl_port *port) 823{ 824 struct ctl_softc *softc = port->ctl_softc; 825 union ctl_ha_msg *msg; 826 int i; 827 828 if (port->targ_port < softc->port_min || 829 port->targ_port >= softc->port_max || 830 softc->ha_link != CTL_HA_LINK_ONLINE) 831 return; 832 i = sizeof(msg->port) + strlen(port->port_name) + 1; 833 if (port->lun_map) 834 i += port->lun_map_size * sizeof(uint32_t); 835 if (port->port_devid) 836 i += port->port_devid->len; 837 if (port->target_devid) 838 i += port->target_devid->len; 839 if (port->init_devid) 840 i += port->init_devid->len; 841 msg = malloc(i, M_CTL, M_WAITOK); 842 bzero(&msg->port, sizeof(msg->port)); 843 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 844 msg->hdr.nexus.targ_port = port->targ_port; 845 msg->port.port_type = port->port_type; 846 msg->port.physical_port = port->physical_port; 847 msg->port.virtual_port = port->virtual_port; 848 msg->port.status = port->status; 849 i = 0; 850 msg->port.name_len = sprintf(&msg->port.data[i], 851 "%d:%s", softc->ha_id, port->port_name) + 1; 852 i += msg->port.name_len; 853 if (port->lun_map) { 854 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 855 memcpy(&msg->port.data[i], port->lun_map, 856 msg->port.lun_map_len); 857 i += msg->port.lun_map_len; 858 } 859 if (port->port_devid) { 860 msg->port.port_devid_len = port->port_devid->len; 861 memcpy(&msg->port.data[i], port->port_devid->data, 862 msg->port.port_devid_len); 863 i += msg->port.port_devid_len; 864 } 865 if (port->target_devid) { 866 msg->port.target_devid_len = port->target_devid->len; 867 memcpy(&msg->port.data[i], port->target_devid->data, 868 msg->port.target_devid_len); 869 i += msg->port.target_devid_len; 870 } 871 if (port->init_devid) { 872 msg->port.init_devid_len = port->init_devid->len; 873 memcpy(&msg->port.data[i], port->init_devid->data, 874 msg->port.init_devid_len); 875 i += msg->port.init_devid_len; 876 } 877 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 878 M_WAITOK); 879 free(msg, M_CTL); 880} 881 882void 883ctl_isc_announce_iid(struct ctl_port *port, int iid) 884{ 885 struct ctl_softc *softc = port->ctl_softc; 886 union ctl_ha_msg *msg; 887 int i, l; 888 889 if (port->targ_port < softc->port_min || 890 port->targ_port >= softc->port_max || 891 softc->ha_link != CTL_HA_LINK_ONLINE) 892 return; 893 mtx_lock(&softc->ctl_lock); 894 i = sizeof(msg->iid); 895 l = 0; 896 if (port->wwpn_iid[iid].name) 897 l = strlen(port->wwpn_iid[iid].name) + 1; 898 i += l; 899 msg = malloc(i, M_CTL, M_NOWAIT); 900 if (msg == NULL) { 901 mtx_unlock(&softc->ctl_lock); 902 return; 903 } 904 bzero(&msg->iid, sizeof(msg->iid)); 905 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 906 msg->hdr.nexus.targ_port = port->targ_port; 907 msg->hdr.nexus.initid = iid; 908 msg->iid.in_use = port->wwpn_iid[iid].in_use; 909 msg->iid.name_len = l; 910 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 911 if (port->wwpn_iid[iid].name) 912 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 913 mtx_unlock(&softc->ctl_lock); 914 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 915 free(msg, M_CTL); 916} 917 918void 919ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 920 uint8_t page, uint8_t subpage) 921{ 922 struct ctl_softc *softc = lun->ctl_softc; 923 union ctl_ha_msg msg; 924 u_int i; 925 926 if (softc->ha_link != CTL_HA_LINK_ONLINE) 927 return; 928 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 929 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 930 page && lun->mode_pages.index[i].subpage == subpage) 931 break; 932 } 933 if (i == CTL_NUM_MODE_PAGES) 934 return; 935 936 /* Don't try to replicate pages not present on this device. */ 937 if (lun->mode_pages.index[i].page_data == NULL) 938 return; 939 940 bzero(&msg.mode, sizeof(msg.mode)); 941 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 942 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 943 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 944 msg.hdr.nexus.targ_lun = lun->lun; 945 msg.hdr.nexus.targ_mapped_lun = lun->lun; 946 msg.mode.page_code = page; 947 msg.mode.subpage = subpage; 948 msg.mode.page_len = lun->mode_pages.index[i].page_len; 949 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 950 msg.mode.page_len); 951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 952 M_WAITOK); 953} 954 955static void 956ctl_isc_ha_link_up(struct ctl_softc *softc) 957{ 958 struct ctl_port *port; 959 struct ctl_lun *lun; 960 union ctl_ha_msg msg; 961 int i; 962 963 /* Announce this node parameters to peer for validation. */ 964 msg.login.msg_type = CTL_MSG_LOGIN; 965 msg.login.version = CTL_HA_VERSION; 966 msg.login.ha_mode = softc->ha_mode; 967 msg.login.ha_id = softc->ha_id; 968 msg.login.max_luns = CTL_MAX_LUNS; 969 msg.login.max_ports = CTL_MAX_PORTS; 970 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 971 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 972 M_WAITOK); 973 974 STAILQ_FOREACH(port, &softc->port_list, links) { 975 ctl_isc_announce_port(port); 976 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 977 if (port->wwpn_iid[i].in_use) 978 ctl_isc_announce_iid(port, i); 979 } 980 } 981 STAILQ_FOREACH(lun, &softc->lun_list, links) 982 ctl_isc_announce_lun(lun); 983} 984 985static void 986ctl_isc_ha_link_down(struct ctl_softc *softc) 987{ 988 struct ctl_port *port; 989 struct ctl_lun *lun; 990 union ctl_io *io; 991 int i; 992 993 mtx_lock(&softc->ctl_lock); 994 STAILQ_FOREACH(lun, &softc->lun_list, links) { 995 mtx_lock(&lun->lun_lock); 996 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 997 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 998 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 999 } 1000 mtx_unlock(&lun->lun_lock); 1001 1002 mtx_unlock(&softc->ctl_lock); 1003 io = ctl_alloc_io(softc->othersc_pool); 1004 mtx_lock(&softc->ctl_lock); 1005 ctl_zero_io(io); 1006 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1007 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1008 ctl_enqueue_isc(io); 1009 } 1010 1011 STAILQ_FOREACH(port, &softc->port_list, links) { 1012 if (port->targ_port >= softc->port_min && 1013 port->targ_port < softc->port_max) 1014 continue; 1015 port->status &= ~CTL_PORT_STATUS_ONLINE; 1016 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1017 port->wwpn_iid[i].in_use = 0; 1018 free(port->wwpn_iid[i].name, M_CTL); 1019 port->wwpn_iid[i].name = NULL; 1020 } 1021 } 1022 mtx_unlock(&softc->ctl_lock); 1023} 1024 1025static void 1026ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1027{ 1028 struct ctl_lun *lun; 1029 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1030 1031 mtx_lock(&softc->ctl_lock); 1032 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS || 1033 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1034 mtx_unlock(&softc->ctl_lock); 1035 return; 1036 } 1037 mtx_lock(&lun->lun_lock); 1038 mtx_unlock(&softc->ctl_lock); 1039 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1040 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1041 if (msg->ua.ua_all) { 1042 if (msg->ua.ua_set) 1043 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1044 else 1045 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1046 } else { 1047 if (msg->ua.ua_set) 1048 ctl_est_ua(lun, iid, msg->ua.ua_type); 1049 else 1050 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1051 } 1052 mtx_unlock(&lun->lun_lock); 1053} 1054 1055static void 1056ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1057{ 1058 struct ctl_lun *lun; 1059 struct ctl_ha_msg_lun_pr_key pr_key; 1060 int i, k; 1061 ctl_lun_flags oflags; 1062 uint32_t targ_lun; 1063 1064 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1065 mtx_lock(&softc->ctl_lock); 1066 if (targ_lun >= CTL_MAX_LUNS || 1067 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1068 mtx_unlock(&softc->ctl_lock); 1069 return; 1070 } 1071 mtx_lock(&lun->lun_lock); 1072 mtx_unlock(&softc->ctl_lock); 1073 if (lun->flags & CTL_LUN_DISABLED) { 1074 mtx_unlock(&lun->lun_lock); 1075 return; 1076 } 1077 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1078 if (msg->lun.lun_devid_len != i || (i > 0 && 1079 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1080 mtx_unlock(&lun->lun_lock); 1081 printf("%s: Received conflicting HA LUN %d\n", 1082 __func__, targ_lun); 1083 return; 1084 } else { 1085 /* Record whether peer is primary. */ 1086 oflags = lun->flags; 1087 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1088 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1089 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1090 else 1091 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1092 if (oflags != lun->flags) 1093 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1094 1095 /* If peer is primary and we are not -- use data */ 1096 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1097 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1098 lun->pr_generation = msg->lun.pr_generation; 1099 lun->pr_res_idx = msg->lun.pr_res_idx; 1100 lun->pr_res_type = msg->lun.pr_res_type; 1101 lun->pr_key_count = msg->lun.pr_key_count; 1102 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1103 ctl_clr_prkey(lun, k); 1104 for (k = 0; k < msg->lun.pr_key_count; k++) { 1105 memcpy(&pr_key, &msg->lun.data[i], 1106 sizeof(pr_key)); 1107 ctl_alloc_prkey(lun, pr_key.pr_iid); 1108 ctl_set_prkey(lun, pr_key.pr_iid, 1109 pr_key.pr_key); 1110 i += sizeof(pr_key); 1111 } 1112 } 1113 1114 mtx_unlock(&lun->lun_lock); 1115 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1116 __func__, targ_lun, 1117 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1118 "primary" : "secondary")); 1119 1120 /* If we are primary but peer doesn't know -- notify */ 1121 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1122 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1123 ctl_isc_announce_lun(lun); 1124 } 1125} 1126 1127static void 1128ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1129{ 1130 struct ctl_port *port; 1131 struct ctl_lun *lun; 1132 int i, new; 1133 1134 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1135 if (port == NULL) { 1136 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1137 msg->hdr.nexus.targ_port)); 1138 new = 1; 1139 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1140 port->frontend = &ha_frontend; 1141 port->targ_port = msg->hdr.nexus.targ_port; 1142 port->fe_datamove = ctl_ha_datamove; 1143 port->fe_done = ctl_ha_done; 1144 } else if (port->frontend == &ha_frontend) { 1145 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1146 msg->hdr.nexus.targ_port)); 1147 new = 0; 1148 } else { 1149 printf("%s: Received conflicting HA port %d\n", 1150 __func__, msg->hdr.nexus.targ_port); 1151 return; 1152 } 1153 port->port_type = msg->port.port_type; 1154 port->physical_port = msg->port.physical_port; 1155 port->virtual_port = msg->port.virtual_port; 1156 port->status = msg->port.status; 1157 i = 0; 1158 free(port->port_name, M_CTL); 1159 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1160 M_CTL); 1161 i += msg->port.name_len; 1162 if (msg->port.lun_map_len != 0) { 1163 if (port->lun_map == NULL || 1164 port->lun_map_size * sizeof(uint32_t) < 1165 msg->port.lun_map_len) { 1166 port->lun_map_size = 0; 1167 free(port->lun_map, M_CTL); 1168 port->lun_map = malloc(msg->port.lun_map_len, 1169 M_CTL, M_WAITOK); 1170 } 1171 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1172 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1173 i += msg->port.lun_map_len; 1174 } else { 1175 port->lun_map_size = 0; 1176 free(port->lun_map, M_CTL); 1177 port->lun_map = NULL; 1178 } 1179 if (msg->port.port_devid_len != 0) { 1180 if (port->port_devid == NULL || 1181 port->port_devid->len < msg->port.port_devid_len) { 1182 free(port->port_devid, M_CTL); 1183 port->port_devid = malloc(sizeof(struct ctl_devid) + 1184 msg->port.port_devid_len, M_CTL, M_WAITOK); 1185 } 1186 memcpy(port->port_devid->data, &msg->port.data[i], 1187 msg->port.port_devid_len); 1188 port->port_devid->len = msg->port.port_devid_len; 1189 i += msg->port.port_devid_len; 1190 } else { 1191 free(port->port_devid, M_CTL); 1192 port->port_devid = NULL; 1193 } 1194 if (msg->port.target_devid_len != 0) { 1195 if (port->target_devid == NULL || 1196 port->target_devid->len < msg->port.target_devid_len) { 1197 free(port->target_devid, M_CTL); 1198 port->target_devid = malloc(sizeof(struct ctl_devid) + 1199 msg->port.target_devid_len, M_CTL, M_WAITOK); 1200 } 1201 memcpy(port->target_devid->data, &msg->port.data[i], 1202 msg->port.target_devid_len); 1203 port->target_devid->len = msg->port.target_devid_len; 1204 i += msg->port.target_devid_len; 1205 } else { 1206 free(port->target_devid, M_CTL); 1207 port->target_devid = NULL; 1208 } 1209 if (msg->port.init_devid_len != 0) { 1210 if (port->init_devid == NULL || 1211 port->init_devid->len < msg->port.init_devid_len) { 1212 free(port->init_devid, M_CTL); 1213 port->init_devid = malloc(sizeof(struct ctl_devid) + 1214 msg->port.init_devid_len, M_CTL, M_WAITOK); 1215 } 1216 memcpy(port->init_devid->data, &msg->port.data[i], 1217 msg->port.init_devid_len); 1218 port->init_devid->len = msg->port.init_devid_len; 1219 i += msg->port.init_devid_len; 1220 } else { 1221 free(port->init_devid, M_CTL); 1222 port->init_devid = NULL; 1223 } 1224 if (new) { 1225 if (ctl_port_register(port) != 0) { 1226 printf("%s: ctl_port_register() failed with error\n", 1227 __func__); 1228 } 1229 } 1230 mtx_lock(&softc->ctl_lock); 1231 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1232 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1233 continue; 1234 mtx_lock(&lun->lun_lock); 1235 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1236 mtx_unlock(&lun->lun_lock); 1237 } 1238 mtx_unlock(&softc->ctl_lock); 1239} 1240 1241static void 1242ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1243{ 1244 struct ctl_port *port; 1245 int iid; 1246 1247 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1248 if (port == NULL) { 1249 printf("%s: Received IID for unknown port %d\n", 1250 __func__, msg->hdr.nexus.targ_port); 1251 return; 1252 } 1253 iid = msg->hdr.nexus.initid; 1254 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1255 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1256 free(port->wwpn_iid[iid].name, M_CTL); 1257 if (msg->iid.name_len) { 1258 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1259 msg->iid.name_len, M_CTL); 1260 } else 1261 port->wwpn_iid[iid].name = NULL; 1262} 1263 1264static void 1265ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1266{ 1267 1268 if (msg->login.version != CTL_HA_VERSION) { 1269 printf("CTL HA peers have different versions %d != %d\n", 1270 msg->login.version, CTL_HA_VERSION); 1271 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1272 return; 1273 } 1274 if (msg->login.ha_mode != softc->ha_mode) { 1275 printf("CTL HA peers have different ha_mode %d != %d\n", 1276 msg->login.ha_mode, softc->ha_mode); 1277 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1278 return; 1279 } 1280 if (msg->login.ha_id == softc->ha_id) { 1281 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1282 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1283 return; 1284 } 1285 if (msg->login.max_luns != CTL_MAX_LUNS || 1286 msg->login.max_ports != CTL_MAX_PORTS || 1287 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1288 printf("CTL HA peers have different limits\n"); 1289 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1290 return; 1291 } 1292} 1293 1294static void 1295ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1296{ 1297 struct ctl_lun *lun; 1298 u_int i; 1299 uint32_t initidx, targ_lun; 1300 1301 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1302 mtx_lock(&softc->ctl_lock); 1303 if (targ_lun >= CTL_MAX_LUNS || 1304 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1305 mtx_unlock(&softc->ctl_lock); 1306 return; 1307 } 1308 mtx_lock(&lun->lun_lock); 1309 mtx_unlock(&softc->ctl_lock); 1310 if (lun->flags & CTL_LUN_DISABLED) { 1311 mtx_unlock(&lun->lun_lock); 1312 return; 1313 } 1314 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1315 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1316 msg->mode.page_code && 1317 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1318 break; 1319 } 1320 if (i == CTL_NUM_MODE_PAGES) { 1321 mtx_unlock(&lun->lun_lock); 1322 return; 1323 } 1324 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1325 lun->mode_pages.index[i].page_len); 1326 initidx = ctl_get_initindex(&msg->hdr.nexus); 1327 if (initidx != -1) 1328 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1329 mtx_unlock(&lun->lun_lock); 1330} 1331 1332/* 1333 * ISC (Inter Shelf Communication) event handler. Events from the HA 1334 * subsystem come in here. 1335 */ 1336static void 1337ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1338{ 1339 struct ctl_softc *softc = control_softc; 1340 union ctl_io *io; 1341 struct ctl_prio *presio; 1342 ctl_ha_status isc_status; 1343 1344 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1345 if (event == CTL_HA_EVT_MSG_RECV) { 1346 union ctl_ha_msg *msg, msgbuf; 1347 1348 if (param > sizeof(msgbuf)) 1349 msg = malloc(param, M_CTL, M_WAITOK); 1350 else 1351 msg = &msgbuf; 1352 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1353 M_WAITOK); 1354 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1355 printf("%s: Error receiving message: %d\n", 1356 __func__, isc_status); 1357 if (msg != &msgbuf) 1358 free(msg, M_CTL); 1359 return; 1360 } 1361 1362 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1363 switch (msg->hdr.msg_type) { 1364 case CTL_MSG_SERIALIZE: 1365 io = ctl_alloc_io(softc->othersc_pool); 1366 ctl_zero_io(io); 1367 // populate ctsio from msg 1368 io->io_hdr.io_type = CTL_IO_SCSI; 1369 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1370 io->io_hdr.original_sc = msg->hdr.original_sc; 1371 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1372 CTL_FLAG_IO_ACTIVE; 1373 /* 1374 * If we're in serialization-only mode, we don't 1375 * want to go through full done processing. Thus 1376 * the COPY flag. 1377 * 1378 * XXX KDM add another flag that is more specific. 1379 */ 1380 if (softc->ha_mode != CTL_HA_MODE_XFER) 1381 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1382 io->io_hdr.nexus = msg->hdr.nexus; 1383#if 0 1384 printf("port %u, iid %u, lun %u\n", 1385 io->io_hdr.nexus.targ_port, 1386 io->io_hdr.nexus.initid, 1387 io->io_hdr.nexus.targ_lun); 1388#endif 1389 io->scsiio.tag_num = msg->scsi.tag_num; 1390 io->scsiio.tag_type = msg->scsi.tag_type; 1391#ifdef CTL_TIME_IO 1392 io->io_hdr.start_time = time_uptime; 1393 getbinuptime(&io->io_hdr.start_bt); 1394#endif /* CTL_TIME_IO */ 1395 io->scsiio.cdb_len = msg->scsi.cdb_len; 1396 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1397 CTL_MAX_CDBLEN); 1398 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1399 const struct ctl_cmd_entry *entry; 1400 1401 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1402 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1403 io->io_hdr.flags |= 1404 entry->flags & CTL_FLAG_DATA_MASK; 1405 } 1406 ctl_enqueue_isc(io); 1407 break; 1408 1409 /* Performed on the Originating SC, XFER mode only */ 1410 case CTL_MSG_DATAMOVE: { 1411 struct ctl_sg_entry *sgl; 1412 int i, j; 1413 1414 io = msg->hdr.original_sc; 1415 if (io == NULL) { 1416 printf("%s: original_sc == NULL!\n", __func__); 1417 /* XXX KDM do something here */ 1418 break; 1419 } 1420 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1421 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1422 /* 1423 * Keep track of this, we need to send it back over 1424 * when the datamove is complete. 1425 */ 1426 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1427 if (msg->hdr.status == CTL_SUCCESS) 1428 io->io_hdr.status = msg->hdr.status; 1429 1430 if (msg->dt.sg_sequence == 0) { 1431#ifdef CTL_TIME_IO 1432 getbinuptime(&io->io_hdr.dma_start_bt); 1433#endif 1434 i = msg->dt.kern_sg_entries + 1435 msg->dt.kern_data_len / 1436 CTL_HA_DATAMOVE_SEGMENT + 1; 1437 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1438 M_WAITOK | M_ZERO); 1439 io->io_hdr.remote_sglist = sgl; 1440 io->io_hdr.local_sglist = 1441 &sgl[msg->dt.kern_sg_entries]; 1442 1443 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1444 1445 io->scsiio.kern_sg_entries = 1446 msg->dt.kern_sg_entries; 1447 io->scsiio.rem_sg_entries = 1448 msg->dt.kern_sg_entries; 1449 io->scsiio.kern_data_len = 1450 msg->dt.kern_data_len; 1451 io->scsiio.kern_total_len = 1452 msg->dt.kern_total_len; 1453 io->scsiio.kern_data_resid = 1454 msg->dt.kern_data_resid; 1455 io->scsiio.kern_rel_offset = 1456 msg->dt.kern_rel_offset; 1457 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1458 io->io_hdr.flags |= msg->dt.flags & 1459 CTL_FLAG_BUS_ADDR; 1460 } else 1461 sgl = (struct ctl_sg_entry *) 1462 io->scsiio.kern_data_ptr; 1463 1464 for (i = msg->dt.sent_sg_entries, j = 0; 1465 i < (msg->dt.sent_sg_entries + 1466 msg->dt.cur_sg_entries); i++, j++) { 1467 sgl[i].addr = msg->dt.sg_list[j].addr; 1468 sgl[i].len = msg->dt.sg_list[j].len; 1469 1470#if 0 1471 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1472 __func__, sgl[i].addr, sgl[i].len, j, i); 1473#endif 1474 } 1475 1476 /* 1477 * If this is the last piece of the I/O, we've got 1478 * the full S/G list. Queue processing in the thread. 1479 * Otherwise wait for the next piece. 1480 */ 1481 if (msg->dt.sg_last != 0) 1482 ctl_enqueue_isc(io); 1483 break; 1484 } 1485 /* Performed on the Serializing (primary) SC, XFER mode only */ 1486 case CTL_MSG_DATAMOVE_DONE: { 1487 if (msg->hdr.serializing_sc == NULL) { 1488 printf("%s: serializing_sc == NULL!\n", 1489 __func__); 1490 /* XXX KDM now what? */ 1491 break; 1492 } 1493 /* 1494 * We grab the sense information here in case 1495 * there was a failure, so we can return status 1496 * back to the initiator. 1497 */ 1498 io = msg->hdr.serializing_sc; 1499 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1500 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1501 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1502 io->io_hdr.port_status = msg->scsi.fetd_status; 1503 io->scsiio.residual = msg->scsi.residual; 1504 if (msg->hdr.status != CTL_STATUS_NONE) { 1505 io->io_hdr.status = msg->hdr.status; 1506 io->scsiio.scsi_status = msg->scsi.scsi_status; 1507 io->scsiio.sense_len = msg->scsi.sense_len; 1508 io->scsiio.sense_residual =msg->scsi.sense_residual; 1509 memcpy(&io->scsiio.sense_data, 1510 &msg->scsi.sense_data, 1511 msg->scsi.sense_len); 1512 if (msg->hdr.status == CTL_SUCCESS) 1513 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1514 } 1515 ctl_enqueue_isc(io); 1516 break; 1517 } 1518 1519 /* Preformed on Originating SC, SER_ONLY mode */ 1520 case CTL_MSG_R2R: 1521 io = msg->hdr.original_sc; 1522 if (io == NULL) { 1523 printf("%s: original_sc == NULL!\n", 1524 __func__); 1525 break; 1526 } 1527 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1528 io->io_hdr.msg_type = CTL_MSG_R2R; 1529 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1530 ctl_enqueue_isc(io); 1531 break; 1532 1533 /* 1534 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1535 * mode. 1536 * Performed on the Originating (i.e. secondary) SC in XFER 1537 * mode 1538 */ 1539 case CTL_MSG_FINISH_IO: 1540 if (softc->ha_mode == CTL_HA_MODE_XFER) 1541 ctl_isc_handler_finish_xfer(softc, msg); 1542 else 1543 ctl_isc_handler_finish_ser_only(softc, msg); 1544 break; 1545 1546 /* Preformed on Originating SC */ 1547 case CTL_MSG_BAD_JUJU: 1548 io = msg->hdr.original_sc; 1549 if (io == NULL) { 1550 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1551 __func__); 1552 break; 1553 } 1554 ctl_copy_sense_data(msg, io); 1555 /* 1556 * IO should have already been cleaned up on other 1557 * SC so clear this flag so we won't send a message 1558 * back to finish the IO there. 1559 */ 1560 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1561 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1562 1563 /* io = msg->hdr.serializing_sc; */ 1564 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1565 ctl_enqueue_isc(io); 1566 break; 1567 1568 /* Handle resets sent from the other side */ 1569 case CTL_MSG_MANAGE_TASKS: { 1570 struct ctl_taskio *taskio; 1571 taskio = (struct ctl_taskio *)ctl_alloc_io( 1572 softc->othersc_pool); 1573 ctl_zero_io((union ctl_io *)taskio); 1574 taskio->io_hdr.io_type = CTL_IO_TASK; 1575 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1576 taskio->io_hdr.nexus = msg->hdr.nexus; 1577 taskio->task_action = msg->task.task_action; 1578 taskio->tag_num = msg->task.tag_num; 1579 taskio->tag_type = msg->task.tag_type; 1580#ifdef CTL_TIME_IO 1581 taskio->io_hdr.start_time = time_uptime; 1582 getbinuptime(&taskio->io_hdr.start_bt); 1583#endif /* CTL_TIME_IO */ 1584 ctl_run_task((union ctl_io *)taskio); 1585 break; 1586 } 1587 /* Persistent Reserve action which needs attention */ 1588 case CTL_MSG_PERS_ACTION: 1589 presio = (struct ctl_prio *)ctl_alloc_io( 1590 softc->othersc_pool); 1591 ctl_zero_io((union ctl_io *)presio); 1592 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1593 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1594 presio->io_hdr.nexus = msg->hdr.nexus; 1595 presio->pr_msg = msg->pr; 1596 ctl_enqueue_isc((union ctl_io *)presio); 1597 break; 1598 case CTL_MSG_UA: 1599 ctl_isc_ua(softc, msg, param); 1600 break; 1601 case CTL_MSG_PORT_SYNC: 1602 ctl_isc_port_sync(softc, msg, param); 1603 break; 1604 case CTL_MSG_LUN_SYNC: 1605 ctl_isc_lun_sync(softc, msg, param); 1606 break; 1607 case CTL_MSG_IID_SYNC: 1608 ctl_isc_iid_sync(softc, msg, param); 1609 break; 1610 case CTL_MSG_LOGIN: 1611 ctl_isc_login(softc, msg, param); 1612 break; 1613 case CTL_MSG_MODE_SYNC: 1614 ctl_isc_mode_sync(softc, msg, param); 1615 break; 1616 default: 1617 printf("Received HA message of unknown type %d\n", 1618 msg->hdr.msg_type); 1619 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1620 break; 1621 } 1622 if (msg != &msgbuf) 1623 free(msg, M_CTL); 1624 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1625 printf("CTL: HA link status changed from %d to %d\n", 1626 softc->ha_link, param); 1627 if (param == softc->ha_link) 1628 return; 1629 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1630 softc->ha_link = param; 1631 ctl_isc_ha_link_down(softc); 1632 } else { 1633 softc->ha_link = param; 1634 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1635 ctl_isc_ha_link_up(softc); 1636 } 1637 return; 1638 } else { 1639 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1640 return; 1641 } 1642} 1643 1644static void 1645ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1646{ 1647 1648 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1649 src->scsi.sense_len); 1650 dest->scsiio.scsi_status = src->scsi.scsi_status; 1651 dest->scsiio.sense_len = src->scsi.sense_len; 1652 dest->io_hdr.status = src->hdr.status; 1653} 1654 1655static void 1656ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1657{ 1658 1659 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1660 src->scsiio.sense_len); 1661 dest->scsi.scsi_status = src->scsiio.scsi_status; 1662 dest->scsi.sense_len = src->scsiio.sense_len; 1663 dest->hdr.status = src->io_hdr.status; 1664} 1665 1666void 1667ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1668{ 1669 struct ctl_softc *softc = lun->ctl_softc; 1670 ctl_ua_type *pu; 1671 1672 if (initidx < softc->init_min || initidx >= softc->init_max) 1673 return; 1674 mtx_assert(&lun->lun_lock, MA_OWNED); 1675 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1676 if (pu == NULL) 1677 return; 1678 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1679} 1680 1681void 1682ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1683{ 1684 int i; 1685 1686 mtx_assert(&lun->lun_lock, MA_OWNED); 1687 if (lun->pending_ua[port] == NULL) 1688 return; 1689 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1690 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1691 continue; 1692 lun->pending_ua[port][i] |= ua; 1693 } 1694} 1695 1696void 1697ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1698{ 1699 struct ctl_softc *softc = lun->ctl_softc; 1700 int i; 1701 1702 mtx_assert(&lun->lun_lock, MA_OWNED); 1703 for (i = softc->port_min; i < softc->port_max; i++) 1704 ctl_est_ua_port(lun, i, except, ua); 1705} 1706 1707void 1708ctl_clr_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_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1724{ 1725 struct ctl_softc *softc = lun->ctl_softc; 1726 int i, j; 1727 1728 mtx_assert(&lun->lun_lock, MA_OWNED); 1729 for (i = softc->port_min; i < softc->port_max; i++) { 1730 if (lun->pending_ua[i] == NULL) 1731 continue; 1732 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1733 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1734 continue; 1735 lun->pending_ua[i][j] &= ~ua; 1736 } 1737 } 1738} 1739 1740void 1741ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1742 ctl_ua_type ua_type) 1743{ 1744 struct ctl_lun *lun; 1745 1746 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1747 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1748 mtx_lock(&lun->lun_lock); 1749 ctl_clr_ua(lun, initidx, ua_type); 1750 mtx_unlock(&lun->lun_lock); 1751 } 1752} 1753 1754static int 1755ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1756{ 1757 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1758 struct ctl_lun *lun; 1759 struct ctl_lun_req ireq; 1760 int error, value; 1761 1762 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1763 error = sysctl_handle_int(oidp, &value, 0, req); 1764 if ((error != 0) || (req->newptr == NULL)) 1765 return (error); 1766 1767 mtx_lock(&softc->ctl_lock); 1768 if (value == 0) 1769 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1770 else 1771 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1772 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1773 mtx_unlock(&softc->ctl_lock); 1774 bzero(&ireq, sizeof(ireq)); 1775 ireq.reqtype = CTL_LUNREQ_MODIFY; 1776 ireq.reqdata.modify.lun_id = lun->lun; 1777 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1778 curthread); 1779 if (ireq.status != CTL_LUN_OK) { 1780 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1781 __func__, ireq.status, ireq.error_str); 1782 } 1783 mtx_lock(&softc->ctl_lock); 1784 } 1785 mtx_unlock(&softc->ctl_lock); 1786 return (0); 1787} 1788 1789static int 1790ctl_init(void) 1791{ 1792 struct make_dev_args args; 1793 struct ctl_softc *softc; 1794 void *other_pool; 1795 int i, error; 1796 1797 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1798 M_WAITOK | M_ZERO); 1799 1800 make_dev_args_init(&args); 1801 args.mda_devsw = &ctl_cdevsw; 1802 args.mda_uid = UID_ROOT; 1803 args.mda_gid = GID_OPERATOR; 1804 args.mda_mode = 0600; 1805 args.mda_si_drv1 = softc; 1806 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1807 if (error != 0) { 1808 free(control_softc, M_DEVBUF); 1809 return (error); 1810 } 1811 1812 sysctl_ctx_init(&softc->sysctl_ctx); 1813 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1814 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1815 CTLFLAG_RD, 0, "CAM Target Layer"); 1816 1817 if (softc->sysctl_tree == NULL) { 1818 printf("%s: unable to allocate sysctl tree\n", __func__); 1819 destroy_dev(softc->dev); 1820 free(control_softc, M_DEVBUF); 1821 control_softc = NULL; 1822 return (ENOMEM); 1823 } 1824 1825 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1826 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1827 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1828 softc->flags = 0; 1829 1830 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode); 1831 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1832 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1833 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1834 1835 /* 1836 * In Copan's HA scheme, the "master" and "slave" roles are 1837 * figured out through the slot the controller is in. Although it 1838 * is an active/active system, someone has to be in charge. 1839 */ 1840 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id); 1841 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1842 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1843 "HA head ID (0 - no HA)"); 1844 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1845 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1846 softc->is_single = 1; 1847 softc->port_cnt = CTL_MAX_PORTS; 1848 softc->port_min = 0; 1849 } else { 1850 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES; 1851 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1852 } 1853 softc->port_max = softc->port_min + softc->port_cnt; 1854 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1855 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1856 1857 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1858 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1859 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1860 1861 STAILQ_INIT(&softc->lun_list); 1862 STAILQ_INIT(&softc->pending_lun_queue); 1863 STAILQ_INIT(&softc->fe_list); 1864 STAILQ_INIT(&softc->port_list); 1865 STAILQ_INIT(&softc->be_list); 1866 ctl_tpc_init(softc); 1867 1868 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1869 &other_pool) != 0) 1870 { 1871 printf("ctl: can't allocate %d entry other SC pool, " 1872 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1873 return (ENOMEM); 1874 } 1875 softc->othersc_pool = other_pool; 1876 1877 if (worker_threads <= 0) 1878 worker_threads = max(1, mp_ncpus / 4); 1879 if (worker_threads > CTL_MAX_THREADS) 1880 worker_threads = CTL_MAX_THREADS; 1881 1882 for (i = 0; i < worker_threads; i++) { 1883 struct ctl_thread *thr = &softc->threads[i]; 1884 1885 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1886 thr->ctl_softc = softc; 1887 STAILQ_INIT(&thr->incoming_queue); 1888 STAILQ_INIT(&thr->rtr_queue); 1889 STAILQ_INIT(&thr->done_queue); 1890 STAILQ_INIT(&thr->isc_queue); 1891 1892 error = kproc_kthread_add(ctl_work_thread, thr, 1893 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1894 if (error != 0) { 1895 printf("error creating CTL work thread!\n"); 1896 ctl_pool_free(other_pool); 1897 return (error); 1898 } 1899 } 1900 error = kproc_kthread_add(ctl_lun_thread, softc, 1901 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1902 if (error != 0) { 1903 printf("error creating CTL lun thread!\n"); 1904 ctl_pool_free(other_pool); 1905 return (error); 1906 } 1907 error = kproc_kthread_add(ctl_thresh_thread, softc, 1908 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1909 if (error != 0) { 1910 printf("error creating CTL threshold thread!\n"); 1911 ctl_pool_free(other_pool); 1912 return (error); 1913 } 1914 1915 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1916 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1917 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1918 1919 if (softc->is_single == 0) { 1920 ctl_frontend_register(&ha_frontend); 1921 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1922 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1923 softc->is_single = 1; 1924 } else 1925 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1926 != CTL_HA_STATUS_SUCCESS) { 1927 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1928 softc->is_single = 1; 1929 } 1930 } 1931 return (0); 1932} 1933 1934void 1935ctl_shutdown(void) 1936{ 1937 struct ctl_softc *softc = control_softc; 1938 struct ctl_lun *lun, *next_lun; 1939 1940 if (softc->is_single == 0) { 1941 ctl_ha_msg_shutdown(softc); 1942 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1943 != CTL_HA_STATUS_SUCCESS) 1944 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1945 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1946 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1947 ctl_frontend_deregister(&ha_frontend); 1948 } 1949 1950 mtx_lock(&softc->ctl_lock); 1951 1952 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1953 ctl_free_lun(lun); 1954 1955 mtx_unlock(&softc->ctl_lock); 1956 1957#if 0 1958 ctl_shutdown_thread(softc->work_thread); 1959 mtx_destroy(&softc->queue_lock); 1960#endif 1961 1962 ctl_tpc_shutdown(softc); 1963 uma_zdestroy(softc->io_zone); 1964 mtx_destroy(&softc->ctl_lock); 1965 1966 destroy_dev(softc->dev); 1967 1968 sysctl_ctx_free(&softc->sysctl_ctx); 1969 1970 free(control_softc, M_DEVBUF); 1971 control_softc = NULL; 1972} 1973 1974static int 1975ctl_module_event_handler(module_t mod, int what, void *arg) 1976{ 1977 1978 switch (what) { 1979 case MOD_LOAD: 1980 return (ctl_init()); 1981 case MOD_UNLOAD: 1982 return (EBUSY); 1983 default: 1984 return (EOPNOTSUPP); 1985 } 1986} 1987 1988/* 1989 * XXX KDM should we do some access checks here? Bump a reference count to 1990 * prevent a CTL module from being unloaded while someone has it open? 1991 */ 1992static int 1993ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1994{ 1995 return (0); 1996} 1997 1998static int 1999ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 2000{ 2001 return (0); 2002} 2003 2004/* 2005 * Remove an initiator by port number and initiator ID. 2006 * Returns 0 for success, -1 for failure. 2007 */ 2008int 2009ctl_remove_initiator(struct ctl_port *port, int iid) 2010{ 2011 struct ctl_softc *softc = port->ctl_softc; 2012 2013 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2014 2015 if (iid > CTL_MAX_INIT_PER_PORT) { 2016 printf("%s: initiator ID %u > maximun %u!\n", 2017 __func__, iid, CTL_MAX_INIT_PER_PORT); 2018 return (-1); 2019 } 2020 2021 mtx_lock(&softc->ctl_lock); 2022 port->wwpn_iid[iid].in_use--; 2023 port->wwpn_iid[iid].last_use = time_uptime; 2024 mtx_unlock(&softc->ctl_lock); 2025 ctl_isc_announce_iid(port, iid); 2026 2027 return (0); 2028} 2029 2030/* 2031 * Add an initiator to the initiator map. 2032 * Returns iid for success, < 0 for failure. 2033 */ 2034int 2035ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2036{ 2037 struct ctl_softc *softc = port->ctl_softc; 2038 time_t best_time; 2039 int i, best; 2040 2041 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2042 2043 if (iid >= CTL_MAX_INIT_PER_PORT) { 2044 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2045 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2046 free(name, M_CTL); 2047 return (-1); 2048 } 2049 2050 mtx_lock(&softc->ctl_lock); 2051 2052 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2053 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2054 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2055 iid = i; 2056 break; 2057 } 2058 if (name != NULL && port->wwpn_iid[i].name != NULL && 2059 strcmp(name, port->wwpn_iid[i].name) == 0) { 2060 iid = i; 2061 break; 2062 } 2063 } 2064 } 2065 2066 if (iid < 0) { 2067 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2068 if (port->wwpn_iid[i].in_use == 0 && 2069 port->wwpn_iid[i].wwpn == 0 && 2070 port->wwpn_iid[i].name == NULL) { 2071 iid = i; 2072 break; 2073 } 2074 } 2075 } 2076 2077 if (iid < 0) { 2078 best = -1; 2079 best_time = INT32_MAX; 2080 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2081 if (port->wwpn_iid[i].in_use == 0) { 2082 if (port->wwpn_iid[i].last_use < best_time) { 2083 best = i; 2084 best_time = port->wwpn_iid[i].last_use; 2085 } 2086 } 2087 } 2088 iid = best; 2089 } 2090 2091 if (iid < 0) { 2092 mtx_unlock(&softc->ctl_lock); 2093 free(name, M_CTL); 2094 return (-2); 2095 } 2096 2097 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2098 /* 2099 * This is not an error yet. 2100 */ 2101 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2102#if 0 2103 printf("%s: port %d iid %u WWPN %#jx arrived" 2104 " again\n", __func__, port->targ_port, 2105 iid, (uintmax_t)wwpn); 2106#endif 2107 goto take; 2108 } 2109 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2110 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2111#if 0 2112 printf("%s: port %d iid %u name '%s' arrived" 2113 " again\n", __func__, port->targ_port, 2114 iid, name); 2115#endif 2116 goto take; 2117 } 2118 2119 /* 2120 * This is an error, but what do we do about it? The 2121 * driver is telling us we have a new WWPN for this 2122 * initiator ID, so we pretty much need to use it. 2123 */ 2124 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2125 " but WWPN %#jx '%s' is still at that address\n", 2126 __func__, port->targ_port, iid, wwpn, name, 2127 (uintmax_t)port->wwpn_iid[iid].wwpn, 2128 port->wwpn_iid[iid].name); 2129 2130 /* 2131 * XXX KDM clear have_ca and ua_pending on each LUN for 2132 * this initiator. 2133 */ 2134 } 2135take: 2136 free(port->wwpn_iid[iid].name, M_CTL); 2137 port->wwpn_iid[iid].name = name; 2138 port->wwpn_iid[iid].wwpn = wwpn; 2139 port->wwpn_iid[iid].in_use++; 2140 mtx_unlock(&softc->ctl_lock); 2141 ctl_isc_announce_iid(port, iid); 2142 2143 return (iid); 2144} 2145 2146static int 2147ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2148{ 2149 int len; 2150 2151 switch (port->port_type) { 2152 case CTL_PORT_FC: 2153 { 2154 struct scsi_transportid_fcp *id = 2155 (struct scsi_transportid_fcp *)buf; 2156 if (port->wwpn_iid[iid].wwpn == 0) 2157 return (0); 2158 memset(id, 0, sizeof(*id)); 2159 id->format_protocol = SCSI_PROTO_FC; 2160 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2161 return (sizeof(*id)); 2162 } 2163 case CTL_PORT_ISCSI: 2164 { 2165 struct scsi_transportid_iscsi_port *id = 2166 (struct scsi_transportid_iscsi_port *)buf; 2167 if (port->wwpn_iid[iid].name == NULL) 2168 return (0); 2169 memset(id, 0, 256); 2170 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2171 SCSI_PROTO_ISCSI; 2172 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2173 len = roundup2(min(len, 252), 4); 2174 scsi_ulto2b(len, id->additional_length); 2175 return (sizeof(*id) + len); 2176 } 2177 case CTL_PORT_SAS: 2178 { 2179 struct scsi_transportid_sas *id = 2180 (struct scsi_transportid_sas *)buf; 2181 if (port->wwpn_iid[iid].wwpn == 0) 2182 return (0); 2183 memset(id, 0, sizeof(*id)); 2184 id->format_protocol = SCSI_PROTO_SAS; 2185 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2186 return (sizeof(*id)); 2187 } 2188 default: 2189 { 2190 struct scsi_transportid_spi *id = 2191 (struct scsi_transportid_spi *)buf; 2192 memset(id, 0, sizeof(*id)); 2193 id->format_protocol = SCSI_PROTO_SPI; 2194 scsi_ulto2b(iid, id->scsi_addr); 2195 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2196 return (sizeof(*id)); 2197 } 2198 } 2199} 2200 2201/* 2202 * Serialize a command that went down the "wrong" side, and so was sent to 2203 * this controller for execution. The logic is a little different than the 2204 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2205 * sent back to the other side, but in the success case, we execute the 2206 * command on this side (XFER mode) or tell the other side to execute it 2207 * (SER_ONLY mode). 2208 */ 2209static void 2210ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2211{ 2212 struct ctl_softc *softc = control_softc; 2213 union ctl_ha_msg msg_info; 2214 struct ctl_port *port; 2215 struct ctl_lun *lun; 2216 const struct ctl_cmd_entry *entry; 2217 uint32_t targ_lun; 2218 2219 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2220 mtx_lock(&softc->ctl_lock); 2221 2222 /* Make sure that we know about this port. */ 2223 port = ctl_io_port(&ctsio->io_hdr); 2224 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2225 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2226 /*retry_count*/ 1); 2227 goto badjuju; 2228 } 2229 2230 /* Make sure that we know about this LUN. */ 2231 if (targ_lun >= CTL_MAX_LUNS || 2232 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2233 mtx_unlock(&softc->ctl_lock); 2234 2235 /* 2236 * The other node would not send this request to us unless 2237 * received announce that we are primary node for this LUN. 2238 * If this LUN does not exist now, it is probably result of 2239 * a race, so respond to initiator in the most opaque way. 2240 */ 2241 ctl_set_busy(ctsio); 2242 goto badjuju; 2243 } 2244 mtx_lock(&lun->lun_lock); 2245 mtx_unlock(&softc->ctl_lock); 2246 2247 /* 2248 * If the LUN is invalid, pretend that it doesn't exist. 2249 * It will go away as soon as all pending I/Os completed. 2250 */ 2251 if (lun->flags & CTL_LUN_DISABLED) { 2252 mtx_unlock(&lun->lun_lock); 2253 ctl_set_busy(ctsio); 2254 goto badjuju; 2255 } 2256 2257 entry = ctl_get_cmd_entry(ctsio, NULL); 2258 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2259 mtx_unlock(&lun->lun_lock); 2260 goto badjuju; 2261 } 2262 2263 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2264 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2265 2266 /* 2267 * Every I/O goes into the OOA queue for a 2268 * particular LUN, and stays there until completion. 2269 */ 2270#ifdef CTL_TIME_IO 2271 if (TAILQ_EMPTY(&lun->ooa_queue)) 2272 lun->idle_time += getsbinuptime() - lun->last_busy; 2273#endif 2274 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2275 2276 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2277 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2278 ooa_links))) { 2279 case CTL_ACTION_BLOCK: 2280 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2281 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2282 blocked_links); 2283 mtx_unlock(&lun->lun_lock); 2284 break; 2285 case CTL_ACTION_PASS: 2286 case CTL_ACTION_SKIP: 2287 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2288 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2289 ctl_enqueue_rtr((union ctl_io *)ctsio); 2290 mtx_unlock(&lun->lun_lock); 2291 } else { 2292 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2293 mtx_unlock(&lun->lun_lock); 2294 2295 /* send msg back to other side */ 2296 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2297 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2298 msg_info.hdr.msg_type = CTL_MSG_R2R; 2299 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2300 sizeof(msg_info.hdr), M_WAITOK); 2301 } 2302 break; 2303 case CTL_ACTION_OVERLAP: 2304 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2305 mtx_unlock(&lun->lun_lock); 2306 ctl_set_overlapped_cmd(ctsio); 2307 goto badjuju; 2308 case CTL_ACTION_OVERLAP_TAG: 2309 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2310 mtx_unlock(&lun->lun_lock); 2311 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2312 goto badjuju; 2313 case CTL_ACTION_ERROR: 2314 default: 2315 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2316 mtx_unlock(&lun->lun_lock); 2317 2318 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2319 /*retry_count*/ 0); 2320badjuju: 2321 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2322 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2323 msg_info.hdr.serializing_sc = NULL; 2324 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2325 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2326 sizeof(msg_info.scsi), M_WAITOK); 2327 ctl_free_io((union ctl_io *)ctsio); 2328 break; 2329 } 2330} 2331 2332/* 2333 * Returns 0 for success, errno for failure. 2334 */ 2335static void 2336ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2337 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2338{ 2339 union ctl_io *io; 2340 2341 mtx_lock(&lun->lun_lock); 2342 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2343 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2344 ooa_links)) { 2345 struct ctl_ooa_entry *entry; 2346 2347 /* 2348 * If we've got more than we can fit, just count the 2349 * remaining entries. 2350 */ 2351 if (*cur_fill_num >= ooa_hdr->alloc_num) 2352 continue; 2353 2354 entry = &kern_entries[*cur_fill_num]; 2355 2356 entry->tag_num = io->scsiio.tag_num; 2357 entry->lun_num = lun->lun; 2358#ifdef CTL_TIME_IO 2359 entry->start_bt = io->io_hdr.start_bt; 2360#endif 2361 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2362 entry->cdb_len = io->scsiio.cdb_len; 2363 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2364 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2365 2366 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2367 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2368 2369 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2370 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2371 2372 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2373 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2374 2375 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2376 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2377 } 2378 mtx_unlock(&lun->lun_lock); 2379} 2380 2381static void * 2382ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2383 size_t error_str_len) 2384{ 2385 void *kptr; 2386 2387 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2388 2389 if (copyin(user_addr, kptr, len) != 0) { 2390 snprintf(error_str, error_str_len, "Error copying %d bytes " 2391 "from user address %p to kernel address %p", len, 2392 user_addr, kptr); 2393 free(kptr, M_CTL); 2394 return (NULL); 2395 } 2396 2397 return (kptr); 2398} 2399 2400static void 2401ctl_free_args(int num_args, struct ctl_be_arg *args) 2402{ 2403 int i; 2404 2405 if (args == NULL) 2406 return; 2407 2408 for (i = 0; i < num_args; i++) { 2409 free(args[i].kname, M_CTL); 2410 free(args[i].kvalue, M_CTL); 2411 } 2412 2413 free(args, M_CTL); 2414} 2415 2416static struct ctl_be_arg * 2417ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2418 char *error_str, size_t error_str_len) 2419{ 2420 struct ctl_be_arg *args; 2421 int i; 2422 2423 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2424 error_str, error_str_len); 2425 2426 if (args == NULL) 2427 goto bailout; 2428 2429 for (i = 0; i < num_args; i++) { 2430 args[i].kname = NULL; 2431 args[i].kvalue = NULL; 2432 } 2433 2434 for (i = 0; i < num_args; i++) { 2435 uint8_t *tmpptr; 2436 2437 args[i].kname = ctl_copyin_alloc(args[i].name, 2438 args[i].namelen, error_str, error_str_len); 2439 if (args[i].kname == NULL) 2440 goto bailout; 2441 2442 if (args[i].kname[args[i].namelen - 1] != '\0') { 2443 snprintf(error_str, error_str_len, "Argument %d " 2444 "name is not NUL-terminated", i); 2445 goto bailout; 2446 } 2447 2448 if (args[i].flags & CTL_BEARG_RD) { 2449 tmpptr = ctl_copyin_alloc(args[i].value, 2450 args[i].vallen, error_str, error_str_len); 2451 if (tmpptr == NULL) 2452 goto bailout; 2453 if ((args[i].flags & CTL_BEARG_ASCII) 2454 && (tmpptr[args[i].vallen - 1] != '\0')) { 2455 snprintf(error_str, error_str_len, "Argument " 2456 "%d value is not NUL-terminated", i); 2457 free(tmpptr, M_CTL); 2458 goto bailout; 2459 } 2460 args[i].kvalue = tmpptr; 2461 } else { 2462 args[i].kvalue = malloc(args[i].vallen, 2463 M_CTL, M_WAITOK | M_ZERO); 2464 } 2465 } 2466 2467 return (args); 2468bailout: 2469 2470 ctl_free_args(num_args, args); 2471 2472 return (NULL); 2473} 2474 2475static void 2476ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2477{ 2478 int i; 2479 2480 for (i = 0; i < num_args; i++) { 2481 if (args[i].flags & CTL_BEARG_WR) 2482 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2483 } 2484} 2485 2486/* 2487 * Escape characters that are illegal or not recommended in XML. 2488 */ 2489int 2490ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2491{ 2492 char *end = str + size; 2493 int retval; 2494 2495 retval = 0; 2496 2497 for (; *str && str < end; str++) { 2498 switch (*str) { 2499 case '&': 2500 retval = sbuf_printf(sb, "&"); 2501 break; 2502 case '>': 2503 retval = sbuf_printf(sb, ">"); 2504 break; 2505 case '<': 2506 retval = sbuf_printf(sb, "<"); 2507 break; 2508 default: 2509 retval = sbuf_putc(sb, *str); 2510 break; 2511 } 2512 2513 if (retval != 0) 2514 break; 2515 2516 } 2517 2518 return (retval); 2519} 2520 2521static void 2522ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2523{ 2524 struct scsi_vpd_id_descriptor *desc; 2525 int i; 2526 2527 if (id == NULL || id->len < 4) 2528 return; 2529 desc = (struct scsi_vpd_id_descriptor *)id->data; 2530 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2531 case SVPD_ID_TYPE_T10: 2532 sbuf_printf(sb, "t10."); 2533 break; 2534 case SVPD_ID_TYPE_EUI64: 2535 sbuf_printf(sb, "eui."); 2536 break; 2537 case SVPD_ID_TYPE_NAA: 2538 sbuf_printf(sb, "naa."); 2539 break; 2540 case SVPD_ID_TYPE_SCSI_NAME: 2541 break; 2542 } 2543 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2544 case SVPD_ID_CODESET_BINARY: 2545 for (i = 0; i < desc->length; i++) 2546 sbuf_printf(sb, "%02x", desc->identifier[i]); 2547 break; 2548 case SVPD_ID_CODESET_ASCII: 2549 sbuf_printf(sb, "%.*s", (int)desc->length, 2550 (char *)desc->identifier); 2551 break; 2552 case SVPD_ID_CODESET_UTF8: 2553 sbuf_printf(sb, "%s", (char *)desc->identifier); 2554 break; 2555 } 2556} 2557 2558static int 2559ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2560 struct thread *td) 2561{ 2562 struct ctl_softc *softc = dev->si_drv1; 2563 struct ctl_lun *lun; 2564 int retval; 2565 2566 retval = 0; 2567 2568 switch (cmd) { 2569 case CTL_IO: 2570 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2571 break; 2572 case CTL_ENABLE_PORT: 2573 case CTL_DISABLE_PORT: 2574 case CTL_SET_PORT_WWNS: { 2575 struct ctl_port *port; 2576 struct ctl_port_entry *entry; 2577 2578 entry = (struct ctl_port_entry *)addr; 2579 2580 mtx_lock(&softc->ctl_lock); 2581 STAILQ_FOREACH(port, &softc->port_list, links) { 2582 int action, done; 2583 2584 if (port->targ_port < softc->port_min || 2585 port->targ_port >= softc->port_max) 2586 continue; 2587 2588 action = 0; 2589 done = 0; 2590 if ((entry->port_type == CTL_PORT_NONE) 2591 && (entry->targ_port == port->targ_port)) { 2592 /* 2593 * If the user only wants to enable or 2594 * disable or set WWNs on a specific port, 2595 * do the operation and we're done. 2596 */ 2597 action = 1; 2598 done = 1; 2599 } else if (entry->port_type & port->port_type) { 2600 /* 2601 * Compare the user's type mask with the 2602 * particular frontend type to see if we 2603 * have a match. 2604 */ 2605 action = 1; 2606 done = 0; 2607 2608 /* 2609 * Make sure the user isn't trying to set 2610 * WWNs on multiple ports at the same time. 2611 */ 2612 if (cmd == CTL_SET_PORT_WWNS) { 2613 printf("%s: Can't set WWNs on " 2614 "multiple ports\n", __func__); 2615 retval = EINVAL; 2616 break; 2617 } 2618 } 2619 if (action == 0) 2620 continue; 2621 2622 /* 2623 * XXX KDM we have to drop the lock here, because 2624 * the online/offline operations can potentially 2625 * block. We need to reference count the frontends 2626 * so they can't go away, 2627 */ 2628 if (cmd == CTL_ENABLE_PORT) { 2629 mtx_unlock(&softc->ctl_lock); 2630 ctl_port_online(port); 2631 mtx_lock(&softc->ctl_lock); 2632 } else if (cmd == CTL_DISABLE_PORT) { 2633 mtx_unlock(&softc->ctl_lock); 2634 ctl_port_offline(port); 2635 mtx_lock(&softc->ctl_lock); 2636 } else if (cmd == CTL_SET_PORT_WWNS) { 2637 ctl_port_set_wwns(port, 2638 (entry->flags & CTL_PORT_WWNN_VALID) ? 2639 1 : 0, entry->wwnn, 2640 (entry->flags & CTL_PORT_WWPN_VALID) ? 2641 1 : 0, entry->wwpn); 2642 } 2643 if (done != 0) 2644 break; 2645 } 2646 mtx_unlock(&softc->ctl_lock); 2647 break; 2648 } 2649 case CTL_GET_OOA: { 2650 struct ctl_ooa *ooa_hdr; 2651 struct ctl_ooa_entry *entries; 2652 uint32_t cur_fill_num; 2653 2654 ooa_hdr = (struct ctl_ooa *)addr; 2655 2656 if ((ooa_hdr->alloc_len == 0) 2657 || (ooa_hdr->alloc_num == 0)) { 2658 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2659 "must be non-zero\n", __func__, 2660 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2661 retval = EINVAL; 2662 break; 2663 } 2664 2665 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2666 sizeof(struct ctl_ooa_entry))) { 2667 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2668 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2669 __func__, ooa_hdr->alloc_len, 2670 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2671 retval = EINVAL; 2672 break; 2673 } 2674 2675 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2676 if (entries == NULL) { 2677 printf("%s: could not allocate %d bytes for OOA " 2678 "dump\n", __func__, ooa_hdr->alloc_len); 2679 retval = ENOMEM; 2680 break; 2681 } 2682 2683 mtx_lock(&softc->ctl_lock); 2684 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2685 (ooa_hdr->lun_num >= CTL_MAX_LUNS || 2686 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2687 mtx_unlock(&softc->ctl_lock); 2688 free(entries, M_CTL); 2689 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2690 __func__, (uintmax_t)ooa_hdr->lun_num); 2691 retval = EINVAL; 2692 break; 2693 } 2694 2695 cur_fill_num = 0; 2696 2697 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2698 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2699 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2700 ooa_hdr, entries); 2701 } 2702 } else { 2703 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2704 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2705 entries); 2706 } 2707 mtx_unlock(&softc->ctl_lock); 2708 2709 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2710 ooa_hdr->fill_len = ooa_hdr->fill_num * 2711 sizeof(struct ctl_ooa_entry); 2712 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2713 if (retval != 0) { 2714 printf("%s: error copying out %d bytes for OOA dump\n", 2715 __func__, ooa_hdr->fill_len); 2716 } 2717 2718 getbinuptime(&ooa_hdr->cur_bt); 2719 2720 if (cur_fill_num > ooa_hdr->alloc_num) { 2721 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2722 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2723 } else { 2724 ooa_hdr->dropped_num = 0; 2725 ooa_hdr->status = CTL_OOA_OK; 2726 } 2727 2728 free(entries, M_CTL); 2729 break; 2730 } 2731 case CTL_DELAY_IO: { 2732 struct ctl_io_delay_info *delay_info; 2733 2734 delay_info = (struct ctl_io_delay_info *)addr; 2735 2736#ifdef CTL_IO_DELAY 2737 mtx_lock(&softc->ctl_lock); 2738 if (delay_info->lun_id >= CTL_MAX_LUNS || 2739 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2740 mtx_unlock(&softc->ctl_lock); 2741 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2742 break; 2743 } 2744 mtx_lock(&lun->lun_lock); 2745 mtx_unlock(&softc->ctl_lock); 2746 delay_info->status = CTL_DELAY_STATUS_OK; 2747 switch (delay_info->delay_type) { 2748 case CTL_DELAY_TYPE_CONT: 2749 case CTL_DELAY_TYPE_ONESHOT: 2750 break; 2751 default: 2752 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2753 break; 2754 } 2755 switch (delay_info->delay_loc) { 2756 case CTL_DELAY_LOC_DATAMOVE: 2757 lun->delay_info.datamove_type = delay_info->delay_type; 2758 lun->delay_info.datamove_delay = delay_info->delay_secs; 2759 break; 2760 case CTL_DELAY_LOC_DONE: 2761 lun->delay_info.done_type = delay_info->delay_type; 2762 lun->delay_info.done_delay = delay_info->delay_secs; 2763 break; 2764 default: 2765 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2766 break; 2767 } 2768 mtx_unlock(&lun->lun_lock); 2769#else 2770 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2771#endif /* CTL_IO_DELAY */ 2772 break; 2773 } 2774 case CTL_GETSTATS: { 2775 struct ctl_stats *stats; 2776 int i; 2777 2778 stats = (struct ctl_stats *)addr; 2779 2780 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2781 stats->alloc_len) { 2782 stats->status = CTL_SS_NEED_MORE_SPACE; 2783 stats->num_luns = softc->num_luns; 2784 break; 2785 } 2786 /* 2787 * XXX KDM no locking here. If the LUN list changes, 2788 * things can blow up. 2789 */ 2790 i = 0; 2791 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2792 retval = copyout(&lun->stats, &stats->lun_stats[i++], 2793 sizeof(lun->stats)); 2794 if (retval != 0) 2795 break; 2796 } 2797 stats->num_luns = softc->num_luns; 2798 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2799 softc->num_luns; 2800 stats->status = CTL_SS_OK; 2801#ifdef CTL_TIME_IO 2802 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2803#else 2804 stats->flags = CTL_STATS_FLAG_NONE; 2805#endif 2806 getnanouptime(&stats->timestamp); 2807 break; 2808 } 2809 case CTL_ERROR_INJECT: { 2810 struct ctl_error_desc *err_desc, *new_err_desc; 2811 2812 err_desc = (struct ctl_error_desc *)addr; 2813 2814 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2815 M_WAITOK | M_ZERO); 2816 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2817 2818 mtx_lock(&softc->ctl_lock); 2819 if (err_desc->lun_id >= CTL_MAX_LUNS || 2820 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2821 mtx_unlock(&softc->ctl_lock); 2822 free(new_err_desc, M_CTL); 2823 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2824 __func__, (uintmax_t)err_desc->lun_id); 2825 retval = EINVAL; 2826 break; 2827 } 2828 mtx_lock(&lun->lun_lock); 2829 mtx_unlock(&softc->ctl_lock); 2830 2831 /* 2832 * We could do some checking here to verify the validity 2833 * of the request, but given the complexity of error 2834 * injection requests, the checking logic would be fairly 2835 * complex. 2836 * 2837 * For now, if the request is invalid, it just won't get 2838 * executed and might get deleted. 2839 */ 2840 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2841 2842 /* 2843 * XXX KDM check to make sure the serial number is unique, 2844 * in case we somehow manage to wrap. That shouldn't 2845 * happen for a very long time, but it's the right thing to 2846 * do. 2847 */ 2848 new_err_desc->serial = lun->error_serial; 2849 err_desc->serial = lun->error_serial; 2850 lun->error_serial++; 2851 2852 mtx_unlock(&lun->lun_lock); 2853 break; 2854 } 2855 case CTL_ERROR_INJECT_DELETE: { 2856 struct ctl_error_desc *delete_desc, *desc, *desc2; 2857 int delete_done; 2858 2859 delete_desc = (struct ctl_error_desc *)addr; 2860 delete_done = 0; 2861 2862 mtx_lock(&softc->ctl_lock); 2863 if (delete_desc->lun_id >= CTL_MAX_LUNS || 2864 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2865 mtx_unlock(&softc->ctl_lock); 2866 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2867 __func__, (uintmax_t)delete_desc->lun_id); 2868 retval = EINVAL; 2869 break; 2870 } 2871 mtx_lock(&lun->lun_lock); 2872 mtx_unlock(&softc->ctl_lock); 2873 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2874 if (desc->serial != delete_desc->serial) 2875 continue; 2876 2877 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2878 links); 2879 free(desc, M_CTL); 2880 delete_done = 1; 2881 } 2882 mtx_unlock(&lun->lun_lock); 2883 if (delete_done == 0) { 2884 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2885 "error serial %ju on LUN %u\n", __func__, 2886 delete_desc->serial, delete_desc->lun_id); 2887 retval = EINVAL; 2888 break; 2889 } 2890 break; 2891 } 2892 case CTL_DUMP_STRUCTS: { 2893 int j, k; 2894 struct ctl_port *port; 2895 struct ctl_frontend *fe; 2896 2897 mtx_lock(&softc->ctl_lock); 2898 printf("CTL Persistent Reservation information start:\n"); 2899 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2900 mtx_lock(&lun->lun_lock); 2901 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2902 mtx_unlock(&lun->lun_lock); 2903 continue; 2904 } 2905 2906 for (j = 0; j < CTL_MAX_PORTS; j++) { 2907 if (lun->pr_keys[j] == NULL) 2908 continue; 2909 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2910 if (lun->pr_keys[j][k] == 0) 2911 continue; 2912 printf(" LUN %ju port %d iid %d key " 2913 "%#jx\n", lun->lun, j, k, 2914 (uintmax_t)lun->pr_keys[j][k]); 2915 } 2916 } 2917 mtx_unlock(&lun->lun_lock); 2918 } 2919 printf("CTL Persistent Reservation information end\n"); 2920 printf("CTL Ports:\n"); 2921 STAILQ_FOREACH(port, &softc->port_list, links) { 2922 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2923 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2924 port->frontend->name, port->port_type, 2925 port->physical_port, port->virtual_port, 2926 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2927 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2928 if (port->wwpn_iid[j].in_use == 0 && 2929 port->wwpn_iid[j].wwpn == 0 && 2930 port->wwpn_iid[j].name == NULL) 2931 continue; 2932 2933 printf(" iid %u use %d WWPN %#jx '%s'\n", 2934 j, port->wwpn_iid[j].in_use, 2935 (uintmax_t)port->wwpn_iid[j].wwpn, 2936 port->wwpn_iid[j].name); 2937 } 2938 } 2939 printf("CTL Port information end\n"); 2940 mtx_unlock(&softc->ctl_lock); 2941 /* 2942 * XXX KDM calling this without a lock. We'd likely want 2943 * to drop the lock before calling the frontend's dump 2944 * routine anyway. 2945 */ 2946 printf("CTL Frontends:\n"); 2947 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2948 printf(" Frontend '%s'\n", fe->name); 2949 if (fe->fe_dump != NULL) 2950 fe->fe_dump(); 2951 } 2952 printf("CTL Frontend information end\n"); 2953 break; 2954 } 2955 case CTL_LUN_REQ: { 2956 struct ctl_lun_req *lun_req; 2957 struct ctl_backend_driver *backend; 2958 2959 lun_req = (struct ctl_lun_req *)addr; 2960 2961 backend = ctl_backend_find(lun_req->backend); 2962 if (backend == NULL) { 2963 lun_req->status = CTL_LUN_ERROR; 2964 snprintf(lun_req->error_str, 2965 sizeof(lun_req->error_str), 2966 "Backend \"%s\" not found.", 2967 lun_req->backend); 2968 break; 2969 } 2970 if (lun_req->num_be_args > 0) { 2971 lun_req->kern_be_args = ctl_copyin_args( 2972 lun_req->num_be_args, 2973 lun_req->be_args, 2974 lun_req->error_str, 2975 sizeof(lun_req->error_str)); 2976 if (lun_req->kern_be_args == NULL) { 2977 lun_req->status = CTL_LUN_ERROR; 2978 break; 2979 } 2980 } 2981 2982 retval = backend->ioctl(dev, cmd, addr, flag, td); 2983 2984 if (lun_req->num_be_args > 0) { 2985 ctl_copyout_args(lun_req->num_be_args, 2986 lun_req->kern_be_args); 2987 ctl_free_args(lun_req->num_be_args, 2988 lun_req->kern_be_args); 2989 } 2990 break; 2991 } 2992 case CTL_LUN_LIST: { 2993 struct sbuf *sb; 2994 struct ctl_lun_list *list; 2995 struct ctl_option *opt; 2996 2997 list = (struct ctl_lun_list *)addr; 2998 2999 /* 3000 * Allocate a fixed length sbuf here, based on the length 3001 * of the user's buffer. We could allocate an auto-extending 3002 * buffer, and then tell the user how much larger our 3003 * amount of data is than his buffer, but that presents 3004 * some problems: 3005 * 3006 * 1. The sbuf(9) routines use a blocking malloc, and so 3007 * we can't hold a lock while calling them with an 3008 * auto-extending buffer. 3009 * 3010 * 2. There is not currently a LUN reference counting 3011 * mechanism, outside of outstanding transactions on 3012 * the LUN's OOA queue. So a LUN could go away on us 3013 * while we're getting the LUN number, backend-specific 3014 * information, etc. Thus, given the way things 3015 * currently work, we need to hold the CTL lock while 3016 * grabbing LUN information. 3017 * 3018 * So, from the user's standpoint, the best thing to do is 3019 * allocate what he thinks is a reasonable buffer length, 3020 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3021 * double the buffer length and try again. (And repeat 3022 * that until he succeeds.) 3023 */ 3024 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3025 if (sb == NULL) { 3026 list->status = CTL_LUN_LIST_ERROR; 3027 snprintf(list->error_str, sizeof(list->error_str), 3028 "Unable to allocate %d bytes for LUN list", 3029 list->alloc_len); 3030 break; 3031 } 3032 3033 sbuf_printf(sb, "<ctllunlist>\n"); 3034 3035 mtx_lock(&softc->ctl_lock); 3036 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3037 mtx_lock(&lun->lun_lock); 3038 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3039 (uintmax_t)lun->lun); 3040 3041 /* 3042 * Bail out as soon as we see that we've overfilled 3043 * the buffer. 3044 */ 3045 if (retval != 0) 3046 break; 3047 3048 retval = sbuf_printf(sb, "\t<backend_type>%s" 3049 "</backend_type>\n", 3050 (lun->backend == NULL) ? "none" : 3051 lun->backend->name); 3052 3053 if (retval != 0) 3054 break; 3055 3056 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3057 lun->be_lun->lun_type); 3058 3059 if (retval != 0) 3060 break; 3061 3062 if (lun->backend == NULL) { 3063 retval = sbuf_printf(sb, "</lun>\n"); 3064 if (retval != 0) 3065 break; 3066 continue; 3067 } 3068 3069 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3070 (lun->be_lun->maxlba > 0) ? 3071 lun->be_lun->maxlba + 1 : 0); 3072 3073 if (retval != 0) 3074 break; 3075 3076 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3077 lun->be_lun->blocksize); 3078 3079 if (retval != 0) 3080 break; 3081 3082 retval = sbuf_printf(sb, "\t<serial_number>"); 3083 3084 if (retval != 0) 3085 break; 3086 3087 retval = ctl_sbuf_printf_esc(sb, 3088 lun->be_lun->serial_num, 3089 sizeof(lun->be_lun->serial_num)); 3090 3091 if (retval != 0) 3092 break; 3093 3094 retval = sbuf_printf(sb, "</serial_number>\n"); 3095 3096 if (retval != 0) 3097 break; 3098 3099 retval = sbuf_printf(sb, "\t<device_id>"); 3100 3101 if (retval != 0) 3102 break; 3103 3104 retval = ctl_sbuf_printf_esc(sb, 3105 lun->be_lun->device_id, 3106 sizeof(lun->be_lun->device_id)); 3107 3108 if (retval != 0) 3109 break; 3110 3111 retval = sbuf_printf(sb, "</device_id>\n"); 3112 3113 if (retval != 0) 3114 break; 3115 3116 if (lun->backend->lun_info != NULL) { 3117 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3118 if (retval != 0) 3119 break; 3120 } 3121 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3122 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3123 opt->name, opt->value, opt->name); 3124 if (retval != 0) 3125 break; 3126 } 3127 3128 retval = sbuf_printf(sb, "</lun>\n"); 3129 3130 if (retval != 0) 3131 break; 3132 mtx_unlock(&lun->lun_lock); 3133 } 3134 if (lun != NULL) 3135 mtx_unlock(&lun->lun_lock); 3136 mtx_unlock(&softc->ctl_lock); 3137 3138 if ((retval != 0) 3139 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3140 retval = 0; 3141 sbuf_delete(sb); 3142 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3143 snprintf(list->error_str, sizeof(list->error_str), 3144 "Out of space, %d bytes is too small", 3145 list->alloc_len); 3146 break; 3147 } 3148 3149 sbuf_finish(sb); 3150 3151 retval = copyout(sbuf_data(sb), list->lun_xml, 3152 sbuf_len(sb) + 1); 3153 3154 list->fill_len = sbuf_len(sb) + 1; 3155 list->status = CTL_LUN_LIST_OK; 3156 sbuf_delete(sb); 3157 break; 3158 } 3159 case CTL_ISCSI: { 3160 struct ctl_iscsi *ci; 3161 struct ctl_frontend *fe; 3162 3163 ci = (struct ctl_iscsi *)addr; 3164 3165 fe = ctl_frontend_find("iscsi"); 3166 if (fe == NULL) { 3167 ci->status = CTL_ISCSI_ERROR; 3168 snprintf(ci->error_str, sizeof(ci->error_str), 3169 "Frontend \"iscsi\" not found."); 3170 break; 3171 } 3172 3173 retval = fe->ioctl(dev, cmd, addr, flag, td); 3174 break; 3175 } 3176 case CTL_PORT_REQ: { 3177 struct ctl_req *req; 3178 struct ctl_frontend *fe; 3179 3180 req = (struct ctl_req *)addr; 3181 3182 fe = ctl_frontend_find(req->driver); 3183 if (fe == NULL) { 3184 req->status = CTL_LUN_ERROR; 3185 snprintf(req->error_str, sizeof(req->error_str), 3186 "Frontend \"%s\" not found.", req->driver); 3187 break; 3188 } 3189 if (req->num_args > 0) { 3190 req->kern_args = ctl_copyin_args(req->num_args, 3191 req->args, req->error_str, sizeof(req->error_str)); 3192 if (req->kern_args == NULL) { 3193 req->status = CTL_LUN_ERROR; 3194 break; 3195 } 3196 } 3197 3198 if (fe->ioctl) 3199 retval = fe->ioctl(dev, cmd, addr, flag, td); 3200 else 3201 retval = ENODEV; 3202 3203 if (req->num_args > 0) { 3204 ctl_copyout_args(req->num_args, req->kern_args); 3205 ctl_free_args(req->num_args, req->kern_args); 3206 } 3207 break; 3208 } 3209 case CTL_PORT_LIST: { 3210 struct sbuf *sb; 3211 struct ctl_port *port; 3212 struct ctl_lun_list *list; 3213 struct ctl_option *opt; 3214 int j; 3215 uint32_t plun; 3216 3217 list = (struct ctl_lun_list *)addr; 3218 3219 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3220 if (sb == NULL) { 3221 list->status = CTL_LUN_LIST_ERROR; 3222 snprintf(list->error_str, sizeof(list->error_str), 3223 "Unable to allocate %d bytes for LUN list", 3224 list->alloc_len); 3225 break; 3226 } 3227 3228 sbuf_printf(sb, "<ctlportlist>\n"); 3229 3230 mtx_lock(&softc->ctl_lock); 3231 STAILQ_FOREACH(port, &softc->port_list, links) { 3232 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3233 (uintmax_t)port->targ_port); 3234 3235 /* 3236 * Bail out as soon as we see that we've overfilled 3237 * the buffer. 3238 */ 3239 if (retval != 0) 3240 break; 3241 3242 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3243 "</frontend_type>\n", port->frontend->name); 3244 if (retval != 0) 3245 break; 3246 3247 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3248 port->port_type); 3249 if (retval != 0) 3250 break; 3251 3252 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3253 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3254 if (retval != 0) 3255 break; 3256 3257 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3258 port->port_name); 3259 if (retval != 0) 3260 break; 3261 3262 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3263 port->physical_port); 3264 if (retval != 0) 3265 break; 3266 3267 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3268 port->virtual_port); 3269 if (retval != 0) 3270 break; 3271 3272 if (port->target_devid != NULL) { 3273 sbuf_printf(sb, "\t<target>"); 3274 ctl_id_sbuf(port->target_devid, sb); 3275 sbuf_printf(sb, "</target>\n"); 3276 } 3277 3278 if (port->port_devid != NULL) { 3279 sbuf_printf(sb, "\t<port>"); 3280 ctl_id_sbuf(port->port_devid, sb); 3281 sbuf_printf(sb, "</port>\n"); 3282 } 3283 3284 if (port->port_info != NULL) { 3285 retval = port->port_info(port->onoff_arg, sb); 3286 if (retval != 0) 3287 break; 3288 } 3289 STAILQ_FOREACH(opt, &port->options, links) { 3290 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3291 opt->name, opt->value, opt->name); 3292 if (retval != 0) 3293 break; 3294 } 3295 3296 if (port->lun_map != NULL) { 3297 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3298 for (j = 0; j < port->lun_map_size; j++) { 3299 plun = ctl_lun_map_from_port(port, j); 3300 if (plun == UINT32_MAX) 3301 continue; 3302 sbuf_printf(sb, 3303 "\t<lun id=\"%u\">%u</lun>\n", 3304 j, plun); 3305 } 3306 } 3307 3308 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3309 if (port->wwpn_iid[j].in_use == 0 || 3310 (port->wwpn_iid[j].wwpn == 0 && 3311 port->wwpn_iid[j].name == NULL)) 3312 continue; 3313 3314 if (port->wwpn_iid[j].name != NULL) 3315 retval = sbuf_printf(sb, 3316 "\t<initiator id=\"%u\">%s</initiator>\n", 3317 j, port->wwpn_iid[j].name); 3318 else 3319 retval = sbuf_printf(sb, 3320 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3321 j, port->wwpn_iid[j].wwpn); 3322 if (retval != 0) 3323 break; 3324 } 3325 if (retval != 0) 3326 break; 3327 3328 retval = sbuf_printf(sb, "</targ_port>\n"); 3329 if (retval != 0) 3330 break; 3331 } 3332 mtx_unlock(&softc->ctl_lock); 3333 3334 if ((retval != 0) 3335 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3336 retval = 0; 3337 sbuf_delete(sb); 3338 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3339 snprintf(list->error_str, sizeof(list->error_str), 3340 "Out of space, %d bytes is too small", 3341 list->alloc_len); 3342 break; 3343 } 3344 3345 sbuf_finish(sb); 3346 3347 retval = copyout(sbuf_data(sb), list->lun_xml, 3348 sbuf_len(sb) + 1); 3349 3350 list->fill_len = sbuf_len(sb) + 1; 3351 list->status = CTL_LUN_LIST_OK; 3352 sbuf_delete(sb); 3353 break; 3354 } 3355 case CTL_LUN_MAP: { 3356 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3357 struct ctl_port *port; 3358 3359 mtx_lock(&softc->ctl_lock); 3360 if (lm->port < softc->port_min || 3361 lm->port >= softc->port_max || 3362 (port = softc->ctl_ports[lm->port]) == NULL) { 3363 mtx_unlock(&softc->ctl_lock); 3364 return (ENXIO); 3365 } 3366 if (port->status & CTL_PORT_STATUS_ONLINE) { 3367 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3368 if (ctl_lun_map_to_port(port, lun->lun) == 3369 UINT32_MAX) 3370 continue; 3371 mtx_lock(&lun->lun_lock); 3372 ctl_est_ua_port(lun, lm->port, -1, 3373 CTL_UA_LUN_CHANGE); 3374 mtx_unlock(&lun->lun_lock); 3375 } 3376 } 3377 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3378 if (lm->plun != UINT32_MAX) { 3379 if (lm->lun == UINT32_MAX) 3380 retval = ctl_lun_map_unset(port, lm->plun); 3381 else if (lm->lun < CTL_MAX_LUNS && 3382 softc->ctl_luns[lm->lun] != NULL) 3383 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3384 else 3385 return (ENXIO); 3386 } else { 3387 if (lm->lun == UINT32_MAX) 3388 retval = ctl_lun_map_deinit(port); 3389 else 3390 retval = ctl_lun_map_init(port); 3391 } 3392 if (port->status & CTL_PORT_STATUS_ONLINE) 3393 ctl_isc_announce_port(port); 3394 break; 3395 } 3396 default: { 3397 /* XXX KDM should we fix this? */ 3398#if 0 3399 struct ctl_backend_driver *backend; 3400 unsigned int type; 3401 int found; 3402 3403 found = 0; 3404 3405 /* 3406 * We encode the backend type as the ioctl type for backend 3407 * ioctls. So parse it out here, and then search for a 3408 * backend of this type. 3409 */ 3410 type = _IOC_TYPE(cmd); 3411 3412 STAILQ_FOREACH(backend, &softc->be_list, links) { 3413 if (backend->type == type) { 3414 found = 1; 3415 break; 3416 } 3417 } 3418 if (found == 0) { 3419 printf("ctl: unknown ioctl command %#lx or backend " 3420 "%d\n", cmd, type); 3421 retval = EINVAL; 3422 break; 3423 } 3424 retval = backend->ioctl(dev, cmd, addr, flag, td); 3425#endif 3426 retval = ENOTTY; 3427 break; 3428 } 3429 } 3430 return (retval); 3431} 3432 3433uint32_t 3434ctl_get_initindex(struct ctl_nexus *nexus) 3435{ 3436 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3437} 3438 3439int 3440ctl_lun_map_init(struct ctl_port *port) 3441{ 3442 struct ctl_softc *softc = port->ctl_softc; 3443 struct ctl_lun *lun; 3444 int size = ctl_lun_map_size; 3445 uint32_t i; 3446 3447 if (port->lun_map == NULL || port->lun_map_size < size) { 3448 port->lun_map_size = 0; 3449 free(port->lun_map, M_CTL); 3450 port->lun_map = malloc(size * sizeof(uint32_t), 3451 M_CTL, M_NOWAIT); 3452 } 3453 if (port->lun_map == NULL) 3454 return (ENOMEM); 3455 for (i = 0; i < size; i++) 3456 port->lun_map[i] = UINT32_MAX; 3457 port->lun_map_size = size; 3458 if (port->status & CTL_PORT_STATUS_ONLINE) { 3459 if (port->lun_disable != NULL) { 3460 STAILQ_FOREACH(lun, &softc->lun_list, links) 3461 port->lun_disable(port->targ_lun_arg, lun->lun); 3462 } 3463 ctl_isc_announce_port(port); 3464 } 3465 return (0); 3466} 3467 3468int 3469ctl_lun_map_deinit(struct ctl_port *port) 3470{ 3471 struct ctl_softc *softc = port->ctl_softc; 3472 struct ctl_lun *lun; 3473 3474 if (port->lun_map == NULL) 3475 return (0); 3476 port->lun_map_size = 0; 3477 free(port->lun_map, M_CTL); 3478 port->lun_map = NULL; 3479 if (port->status & CTL_PORT_STATUS_ONLINE) { 3480 if (port->lun_enable != NULL) { 3481 STAILQ_FOREACH(lun, &softc->lun_list, links) 3482 port->lun_enable(port->targ_lun_arg, lun->lun); 3483 } 3484 ctl_isc_announce_port(port); 3485 } 3486 return (0); 3487} 3488 3489int 3490ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3491{ 3492 int status; 3493 uint32_t old; 3494 3495 if (port->lun_map == NULL) { 3496 status = ctl_lun_map_init(port); 3497 if (status != 0) 3498 return (status); 3499 } 3500 if (plun >= port->lun_map_size) 3501 return (EINVAL); 3502 old = port->lun_map[plun]; 3503 port->lun_map[plun] = glun; 3504 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3505 if (port->lun_enable != NULL) 3506 port->lun_enable(port->targ_lun_arg, plun); 3507 ctl_isc_announce_port(port); 3508 } 3509 return (0); 3510} 3511 3512int 3513ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3514{ 3515 uint32_t old; 3516 3517 if (port->lun_map == NULL || plun >= port->lun_map_size) 3518 return (0); 3519 old = port->lun_map[plun]; 3520 port->lun_map[plun] = UINT32_MAX; 3521 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3522 if (port->lun_disable != NULL) 3523 port->lun_disable(port->targ_lun_arg, plun); 3524 ctl_isc_announce_port(port); 3525 } 3526 return (0); 3527} 3528 3529uint32_t 3530ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3531{ 3532 3533 if (port == NULL) 3534 return (UINT32_MAX); 3535 if (port->lun_map == NULL) 3536 return (lun_id); 3537 if (lun_id > port->lun_map_size) 3538 return (UINT32_MAX); 3539 return (port->lun_map[lun_id]); 3540} 3541 3542uint32_t 3543ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3544{ 3545 uint32_t i; 3546 3547 if (port == NULL) 3548 return (UINT32_MAX); 3549 if (port->lun_map == NULL) 3550 return (lun_id); 3551 for (i = 0; i < port->lun_map_size; i++) { 3552 if (port->lun_map[i] == lun_id) 3553 return (i); 3554 } 3555 return (UINT32_MAX); 3556} 3557 3558uint32_t 3559ctl_decode_lun(uint64_t encoded) 3560{ 3561 uint8_t lun[8]; 3562 uint32_t result = 0xffffffff; 3563 3564 be64enc(lun, encoded); 3565 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3566 case RPL_LUNDATA_ATYP_PERIPH: 3567 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3568 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3569 result = lun[1]; 3570 break; 3571 case RPL_LUNDATA_ATYP_FLAT: 3572 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3573 lun[6] == 0 && lun[7] == 0) 3574 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3575 break; 3576 case RPL_LUNDATA_ATYP_EXTLUN: 3577 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3578 case 0x02: 3579 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3580 case 0x00: 3581 result = lun[1]; 3582 break; 3583 case 0x10: 3584 result = (lun[1] << 16) + (lun[2] << 8) + 3585 lun[3]; 3586 break; 3587 case 0x20: 3588 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3589 result = (lun[2] << 24) + 3590 (lun[3] << 16) + (lun[4] << 8) + 3591 lun[5]; 3592 break; 3593 } 3594 break; 3595 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3596 result = 0xffffffff; 3597 break; 3598 } 3599 break; 3600 } 3601 return (result); 3602} 3603 3604uint64_t 3605ctl_encode_lun(uint32_t decoded) 3606{ 3607 uint64_t l = decoded; 3608 3609 if (l <= 0xff) 3610 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3611 if (l <= 0x3fff) 3612 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3613 if (l <= 0xffffff) 3614 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3615 (l << 32)); 3616 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3617} 3618 3619static struct ctl_port * 3620ctl_io_port(struct ctl_io_hdr *io_hdr) 3621{ 3622 3623 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3624} 3625 3626int 3627ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3628{ 3629 int i; 3630 3631 for (i = first; i < last; i++) { 3632 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3633 return (i); 3634 } 3635 return (-1); 3636} 3637 3638int 3639ctl_set_mask(uint32_t *mask, uint32_t bit) 3640{ 3641 uint32_t chunk, piece; 3642 3643 chunk = bit >> 5; 3644 piece = bit % (sizeof(uint32_t) * 8); 3645 3646 if ((mask[chunk] & (1 << piece)) != 0) 3647 return (-1); 3648 else 3649 mask[chunk] |= (1 << piece); 3650 3651 return (0); 3652} 3653 3654int 3655ctl_clear_mask(uint32_t *mask, uint32_t bit) 3656{ 3657 uint32_t chunk, piece; 3658 3659 chunk = bit >> 5; 3660 piece = bit % (sizeof(uint32_t) * 8); 3661 3662 if ((mask[chunk] & (1 << piece)) == 0) 3663 return (-1); 3664 else 3665 mask[chunk] &= ~(1 << piece); 3666 3667 return (0); 3668} 3669 3670int 3671ctl_is_set(uint32_t *mask, uint32_t bit) 3672{ 3673 uint32_t chunk, piece; 3674 3675 chunk = bit >> 5; 3676 piece = bit % (sizeof(uint32_t) * 8); 3677 3678 if ((mask[chunk] & (1 << piece)) == 0) 3679 return (0); 3680 else 3681 return (1); 3682} 3683 3684static uint64_t 3685ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3686{ 3687 uint64_t *t; 3688 3689 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3690 if (t == NULL) 3691 return (0); 3692 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3693} 3694 3695static void 3696ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3697{ 3698 uint64_t *t; 3699 3700 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3701 if (t == NULL) 3702 return; 3703 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3704} 3705 3706static void 3707ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3708{ 3709 uint64_t *p; 3710 u_int i; 3711 3712 i = residx/CTL_MAX_INIT_PER_PORT; 3713 if (lun->pr_keys[i] != NULL) 3714 return; 3715 mtx_unlock(&lun->lun_lock); 3716 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3717 M_WAITOK | M_ZERO); 3718 mtx_lock(&lun->lun_lock); 3719 if (lun->pr_keys[i] == NULL) 3720 lun->pr_keys[i] = p; 3721 else 3722 free(p, M_CTL); 3723} 3724 3725static void 3726ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3727{ 3728 uint64_t *t; 3729 3730 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3731 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3732 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3733} 3734 3735/* 3736 * ctl_softc, pool_name, total_ctl_io are passed in. 3737 * npool is passed out. 3738 */ 3739int 3740ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3741 uint32_t total_ctl_io, void **npool) 3742{ 3743#ifdef IO_POOLS 3744 struct ctl_io_pool *pool; 3745 3746 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3747 M_NOWAIT | M_ZERO); 3748 if (pool == NULL) 3749 return (ENOMEM); 3750 3751 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3752 pool->ctl_softc = ctl_softc; 3753 pool->zone = uma_zsecond_create(pool->name, NULL, 3754 NULL, NULL, NULL, ctl_softc->io_zone); 3755 /* uma_prealloc(pool->zone, total_ctl_io); */ 3756 3757 *npool = pool; 3758#else 3759 *npool = ctl_softc->io_zone; 3760#endif 3761 return (0); 3762} 3763 3764void 3765ctl_pool_free(struct ctl_io_pool *pool) 3766{ 3767 3768 if (pool == NULL) 3769 return; 3770 3771#ifdef IO_POOLS 3772 uma_zdestroy(pool->zone); 3773 free(pool, M_CTL); 3774#endif 3775} 3776 3777union ctl_io * 3778ctl_alloc_io(void *pool_ref) 3779{ 3780 union ctl_io *io; 3781#ifdef IO_POOLS 3782 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3783 3784 io = uma_zalloc(pool->zone, M_WAITOK); 3785#else 3786 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3787#endif 3788 if (io != NULL) 3789 io->io_hdr.pool = pool_ref; 3790 return (io); 3791} 3792 3793union ctl_io * 3794ctl_alloc_io_nowait(void *pool_ref) 3795{ 3796 union ctl_io *io; 3797#ifdef IO_POOLS 3798 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3799 3800 io = uma_zalloc(pool->zone, M_NOWAIT); 3801#else 3802 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3803#endif 3804 if (io != NULL) 3805 io->io_hdr.pool = pool_ref; 3806 return (io); 3807} 3808 3809void 3810ctl_free_io(union ctl_io *io) 3811{ 3812#ifdef IO_POOLS 3813 struct ctl_io_pool *pool; 3814#endif 3815 3816 if (io == NULL) 3817 return; 3818 3819#ifdef IO_POOLS 3820 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3821 uma_zfree(pool->zone, io); 3822#else 3823 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3824#endif 3825} 3826 3827void 3828ctl_zero_io(union ctl_io *io) 3829{ 3830 void *pool_ref; 3831 3832 if (io == NULL) 3833 return; 3834 3835 /* 3836 * May need to preserve linked list pointers at some point too. 3837 */ 3838 pool_ref = io->io_hdr.pool; 3839 memset(io, 0, sizeof(*io)); 3840 io->io_hdr.pool = pool_ref; 3841} 3842 3843int 3844ctl_expand_number(const char *buf, uint64_t *num) 3845{ 3846 char *endptr; 3847 uint64_t number; 3848 unsigned shift; 3849 3850 number = strtoq(buf, &endptr, 0); 3851 3852 switch (tolower((unsigned char)*endptr)) { 3853 case 'e': 3854 shift = 60; 3855 break; 3856 case 'p': 3857 shift = 50; 3858 break; 3859 case 't': 3860 shift = 40; 3861 break; 3862 case 'g': 3863 shift = 30; 3864 break; 3865 case 'm': 3866 shift = 20; 3867 break; 3868 case 'k': 3869 shift = 10; 3870 break; 3871 case 'b': 3872 case '\0': /* No unit. */ 3873 *num = number; 3874 return (0); 3875 default: 3876 /* Unrecognized unit. */ 3877 return (-1); 3878 } 3879 3880 if ((number << shift) >> shift != number) { 3881 /* Overflow */ 3882 return (-1); 3883 } 3884 *num = number << shift; 3885 return (0); 3886} 3887 3888 3889/* 3890 * This routine could be used in the future to load default and/or saved 3891 * mode page parameters for a particuar lun. 3892 */ 3893static int 3894ctl_init_page_index(struct ctl_lun *lun) 3895{ 3896 int i, page_code; 3897 struct ctl_page_index *page_index; 3898 const char *value; 3899 uint64_t ival; 3900 3901 memcpy(&lun->mode_pages.index, page_index_template, 3902 sizeof(page_index_template)); 3903 3904 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3905 3906 page_index = &lun->mode_pages.index[i]; 3907 if (lun->be_lun->lun_type == T_DIRECT && 3908 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3909 continue; 3910 if (lun->be_lun->lun_type == T_PROCESSOR && 3911 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3912 continue; 3913 if (lun->be_lun->lun_type == T_CDROM && 3914 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3915 continue; 3916 3917 page_code = page_index->page_code & SMPH_PC_MASK; 3918 switch (page_code) { 3919 case SMS_RW_ERROR_RECOVERY_PAGE: { 3920 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3921 ("subpage %#x for page %#x is incorrect!", 3922 page_index->subpage, page_code)); 3923 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3924 &rw_er_page_default, 3925 sizeof(rw_er_page_default)); 3926 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3927 &rw_er_page_changeable, 3928 sizeof(rw_er_page_changeable)); 3929 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3930 &rw_er_page_default, 3931 sizeof(rw_er_page_default)); 3932 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3933 &rw_er_page_default, 3934 sizeof(rw_er_page_default)); 3935 page_index->page_data = 3936 (uint8_t *)lun->mode_pages.rw_er_page; 3937 break; 3938 } 3939 case SMS_FORMAT_DEVICE_PAGE: { 3940 struct scsi_format_page *format_page; 3941 3942 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3943 ("subpage %#x for page %#x is incorrect!", 3944 page_index->subpage, page_code)); 3945 3946 /* 3947 * Sectors per track are set above. Bytes per 3948 * sector need to be set here on a per-LUN basis. 3949 */ 3950 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3951 &format_page_default, 3952 sizeof(format_page_default)); 3953 memcpy(&lun->mode_pages.format_page[ 3954 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3955 sizeof(format_page_changeable)); 3956 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3957 &format_page_default, 3958 sizeof(format_page_default)); 3959 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3960 &format_page_default, 3961 sizeof(format_page_default)); 3962 3963 format_page = &lun->mode_pages.format_page[ 3964 CTL_PAGE_CURRENT]; 3965 scsi_ulto2b(lun->be_lun->blocksize, 3966 format_page->bytes_per_sector); 3967 3968 format_page = &lun->mode_pages.format_page[ 3969 CTL_PAGE_DEFAULT]; 3970 scsi_ulto2b(lun->be_lun->blocksize, 3971 format_page->bytes_per_sector); 3972 3973 format_page = &lun->mode_pages.format_page[ 3974 CTL_PAGE_SAVED]; 3975 scsi_ulto2b(lun->be_lun->blocksize, 3976 format_page->bytes_per_sector); 3977 3978 page_index->page_data = 3979 (uint8_t *)lun->mode_pages.format_page; 3980 break; 3981 } 3982 case SMS_RIGID_DISK_PAGE: { 3983 struct scsi_rigid_disk_page *rigid_disk_page; 3984 uint32_t sectors_per_cylinder; 3985 uint64_t cylinders; 3986#ifndef __XSCALE__ 3987 int shift; 3988#endif /* !__XSCALE__ */ 3989 3990 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3991 ("subpage %#x for page %#x is incorrect!", 3992 page_index->subpage, page_code)); 3993 3994 /* 3995 * Rotation rate and sectors per track are set 3996 * above. We calculate the cylinders here based on 3997 * capacity. Due to the number of heads and 3998 * sectors per track we're using, smaller arrays 3999 * may turn out to have 0 cylinders. Linux and 4000 * FreeBSD don't pay attention to these mode pages 4001 * to figure out capacity, but Solaris does. It 4002 * seems to deal with 0 cylinders just fine, and 4003 * works out a fake geometry based on the capacity. 4004 */ 4005 memcpy(&lun->mode_pages.rigid_disk_page[ 4006 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4007 sizeof(rigid_disk_page_default)); 4008 memcpy(&lun->mode_pages.rigid_disk_page[ 4009 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4010 sizeof(rigid_disk_page_changeable)); 4011 4012 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4013 CTL_DEFAULT_HEADS; 4014 4015 /* 4016 * The divide method here will be more accurate, 4017 * probably, but results in floating point being 4018 * used in the kernel on i386 (__udivdi3()). On the 4019 * XScale, though, __udivdi3() is implemented in 4020 * software. 4021 * 4022 * The shift method for cylinder calculation is 4023 * accurate if sectors_per_cylinder is a power of 4024 * 2. Otherwise it might be slightly off -- you 4025 * might have a bit of a truncation problem. 4026 */ 4027#ifdef __XSCALE__ 4028 cylinders = (lun->be_lun->maxlba + 1) / 4029 sectors_per_cylinder; 4030#else 4031 for (shift = 31; shift > 0; shift--) { 4032 if (sectors_per_cylinder & (1 << shift)) 4033 break; 4034 } 4035 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4036#endif 4037 4038 /* 4039 * We've basically got 3 bytes, or 24 bits for the 4040 * cylinder size in the mode page. If we're over, 4041 * just round down to 2^24. 4042 */ 4043 if (cylinders > 0xffffff) 4044 cylinders = 0xffffff; 4045 4046 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4047 CTL_PAGE_DEFAULT]; 4048 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4049 4050 if ((value = ctl_get_opt(&lun->be_lun->options, 4051 "rpm")) != NULL) { 4052 scsi_ulto2b(strtol(value, NULL, 0), 4053 rigid_disk_page->rotation_rate); 4054 } 4055 4056 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4057 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4058 sizeof(rigid_disk_page_default)); 4059 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4060 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4061 sizeof(rigid_disk_page_default)); 4062 4063 page_index->page_data = 4064 (uint8_t *)lun->mode_pages.rigid_disk_page; 4065 break; 4066 } 4067 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4068 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4069 ("subpage %#x for page %#x is incorrect!", 4070 page_index->subpage, page_code)); 4071 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4072 &verify_er_page_default, 4073 sizeof(verify_er_page_default)); 4074 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4075 &verify_er_page_changeable, 4076 sizeof(verify_er_page_changeable)); 4077 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4078 &verify_er_page_default, 4079 sizeof(verify_er_page_default)); 4080 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4081 &verify_er_page_default, 4082 sizeof(verify_er_page_default)); 4083 page_index->page_data = 4084 (uint8_t *)lun->mode_pages.verify_er_page; 4085 break; 4086 } 4087 case SMS_CACHING_PAGE: { 4088 struct scsi_caching_page *caching_page; 4089 4090 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4091 ("subpage %#x for page %#x is incorrect!", 4092 page_index->subpage, page_code)); 4093 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4094 &caching_page_default, 4095 sizeof(caching_page_default)); 4096 memcpy(&lun->mode_pages.caching_page[ 4097 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4098 sizeof(caching_page_changeable)); 4099 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4100 &caching_page_default, 4101 sizeof(caching_page_default)); 4102 caching_page = &lun->mode_pages.caching_page[ 4103 CTL_PAGE_SAVED]; 4104 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4105 if (value != NULL && strcmp(value, "off") == 0) 4106 caching_page->flags1 &= ~SCP_WCE; 4107 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4108 if (value != NULL && strcmp(value, "off") == 0) 4109 caching_page->flags1 |= SCP_RCD; 4110 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4111 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4112 sizeof(caching_page_default)); 4113 page_index->page_data = 4114 (uint8_t *)lun->mode_pages.caching_page; 4115 break; 4116 } 4117 case SMS_CONTROL_MODE_PAGE: { 4118 switch (page_index->subpage) { 4119 case SMS_SUBPAGE_PAGE_0: { 4120 struct scsi_control_page *control_page; 4121 4122 memcpy(&lun->mode_pages.control_page[ 4123 CTL_PAGE_DEFAULT], 4124 &control_page_default, 4125 sizeof(control_page_default)); 4126 memcpy(&lun->mode_pages.control_page[ 4127 CTL_PAGE_CHANGEABLE], 4128 &control_page_changeable, 4129 sizeof(control_page_changeable)); 4130 memcpy(&lun->mode_pages.control_page[ 4131 CTL_PAGE_SAVED], 4132 &control_page_default, 4133 sizeof(control_page_default)); 4134 control_page = &lun->mode_pages.control_page[ 4135 CTL_PAGE_SAVED]; 4136 value = ctl_get_opt(&lun->be_lun->options, 4137 "reordering"); 4138 if (value != NULL && 4139 strcmp(value, "unrestricted") == 0) { 4140 control_page->queue_flags &= 4141 ~SCP_QUEUE_ALG_MASK; 4142 control_page->queue_flags |= 4143 SCP_QUEUE_ALG_UNRESTRICTED; 4144 } 4145 memcpy(&lun->mode_pages.control_page[ 4146 CTL_PAGE_CURRENT], 4147 &lun->mode_pages.control_page[ 4148 CTL_PAGE_SAVED], 4149 sizeof(control_page_default)); 4150 page_index->page_data = 4151 (uint8_t *)lun->mode_pages.control_page; 4152 break; 4153 } 4154 case 0x01: 4155 memcpy(&lun->mode_pages.control_ext_page[ 4156 CTL_PAGE_DEFAULT], 4157 &control_ext_page_default, 4158 sizeof(control_ext_page_default)); 4159 memcpy(&lun->mode_pages.control_ext_page[ 4160 CTL_PAGE_CHANGEABLE], 4161 &control_ext_page_changeable, 4162 sizeof(control_ext_page_changeable)); 4163 memcpy(&lun->mode_pages.control_ext_page[ 4164 CTL_PAGE_SAVED], 4165 &control_ext_page_default, 4166 sizeof(control_ext_page_default)); 4167 memcpy(&lun->mode_pages.control_ext_page[ 4168 CTL_PAGE_CURRENT], 4169 &lun->mode_pages.control_ext_page[ 4170 CTL_PAGE_SAVED], 4171 sizeof(control_ext_page_default)); 4172 page_index->page_data = 4173 (uint8_t *)lun->mode_pages.control_ext_page; 4174 break; 4175 default: 4176 panic("subpage %#x for page %#x is incorrect!", 4177 page_index->subpage, page_code); 4178 } 4179 break; 4180 } 4181 case SMS_INFO_EXCEPTIONS_PAGE: { 4182 switch (page_index->subpage) { 4183 case SMS_SUBPAGE_PAGE_0: 4184 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4185 &ie_page_default, 4186 sizeof(ie_page_default)); 4187 memcpy(&lun->mode_pages.ie_page[ 4188 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4189 sizeof(ie_page_changeable)); 4190 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4191 &ie_page_default, 4192 sizeof(ie_page_default)); 4193 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4194 &ie_page_default, 4195 sizeof(ie_page_default)); 4196 page_index->page_data = 4197 (uint8_t *)lun->mode_pages.ie_page; 4198 break; 4199 case 0x02: { 4200 struct ctl_logical_block_provisioning_page *page; 4201 4202 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4203 &lbp_page_default, 4204 sizeof(lbp_page_default)); 4205 memcpy(&lun->mode_pages.lbp_page[ 4206 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4207 sizeof(lbp_page_changeable)); 4208 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4209 &lbp_page_default, 4210 sizeof(lbp_page_default)); 4211 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4212 value = ctl_get_opt(&lun->be_lun->options, 4213 "avail-threshold"); 4214 if (value != NULL && 4215 ctl_expand_number(value, &ival) == 0) { 4216 page->descr[0].flags |= SLBPPD_ENABLED | 4217 SLBPPD_ARMING_DEC; 4218 if (lun->be_lun->blocksize) 4219 ival /= lun->be_lun->blocksize; 4220 else 4221 ival /= 512; 4222 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4223 page->descr[0].count); 4224 } 4225 value = ctl_get_opt(&lun->be_lun->options, 4226 "used-threshold"); 4227 if (value != NULL && 4228 ctl_expand_number(value, &ival) == 0) { 4229 page->descr[1].flags |= SLBPPD_ENABLED | 4230 SLBPPD_ARMING_INC; 4231 if (lun->be_lun->blocksize) 4232 ival /= lun->be_lun->blocksize; 4233 else 4234 ival /= 512; 4235 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4236 page->descr[1].count); 4237 } 4238 value = ctl_get_opt(&lun->be_lun->options, 4239 "pool-avail-threshold"); 4240 if (value != NULL && 4241 ctl_expand_number(value, &ival) == 0) { 4242 page->descr[2].flags |= SLBPPD_ENABLED | 4243 SLBPPD_ARMING_DEC; 4244 if (lun->be_lun->blocksize) 4245 ival /= lun->be_lun->blocksize; 4246 else 4247 ival /= 512; 4248 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4249 page->descr[2].count); 4250 } 4251 value = ctl_get_opt(&lun->be_lun->options, 4252 "pool-used-threshold"); 4253 if (value != NULL && 4254 ctl_expand_number(value, &ival) == 0) { 4255 page->descr[3].flags |= SLBPPD_ENABLED | 4256 SLBPPD_ARMING_INC; 4257 if (lun->be_lun->blocksize) 4258 ival /= lun->be_lun->blocksize; 4259 else 4260 ival /= 512; 4261 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4262 page->descr[3].count); 4263 } 4264 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4265 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4266 sizeof(lbp_page_default)); 4267 page_index->page_data = 4268 (uint8_t *)lun->mode_pages.lbp_page; 4269 break; 4270 } 4271 default: 4272 panic("subpage %#x for page %#x is incorrect!", 4273 page_index->subpage, page_code); 4274 } 4275 break; 4276 } 4277 case SMS_CDDVD_CAPS_PAGE:{ 4278 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4279 ("subpage %#x for page %#x is incorrect!", 4280 page_index->subpage, page_code)); 4281 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4282 &cddvd_page_default, 4283 sizeof(cddvd_page_default)); 4284 memcpy(&lun->mode_pages.cddvd_page[ 4285 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4286 sizeof(cddvd_page_changeable)); 4287 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4288 &cddvd_page_default, 4289 sizeof(cddvd_page_default)); 4290 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4291 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4292 sizeof(cddvd_page_default)); 4293 page_index->page_data = 4294 (uint8_t *)lun->mode_pages.cddvd_page; 4295 break; 4296 } 4297 default: 4298 panic("invalid page code value %#x", page_code); 4299 } 4300 } 4301 4302 return (CTL_RETVAL_COMPLETE); 4303} 4304 4305static int 4306ctl_init_log_page_index(struct ctl_lun *lun) 4307{ 4308 struct ctl_page_index *page_index; 4309 int i, j, k, prev; 4310 4311 memcpy(&lun->log_pages.index, log_page_index_template, 4312 sizeof(log_page_index_template)); 4313 4314 prev = -1; 4315 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4316 4317 page_index = &lun->log_pages.index[i]; 4318 if (lun->be_lun->lun_type == T_DIRECT && 4319 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4320 continue; 4321 if (lun->be_lun->lun_type == T_PROCESSOR && 4322 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4323 continue; 4324 if (lun->be_lun->lun_type == T_CDROM && 4325 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4326 continue; 4327 4328 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4329 lun->backend->lun_attr == NULL) 4330 continue; 4331 4332 if (page_index->page_code != prev) { 4333 lun->log_pages.pages_page[j] = page_index->page_code; 4334 prev = page_index->page_code; 4335 j++; 4336 } 4337 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4338 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4339 k++; 4340 } 4341 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4342 lun->log_pages.index[0].page_len = j; 4343 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4344 lun->log_pages.index[1].page_len = k * 2; 4345 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4346 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4347 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4348 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4349 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4350 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4351 4352 return (CTL_RETVAL_COMPLETE); 4353} 4354 4355static int 4356hex2bin(const char *str, uint8_t *buf, int buf_size) 4357{ 4358 int i; 4359 u_char c; 4360 4361 memset(buf, 0, buf_size); 4362 while (isspace(str[0])) 4363 str++; 4364 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4365 str += 2; 4366 buf_size *= 2; 4367 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4368 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4369 str++; 4370 c = str[i]; 4371 if (isdigit(c)) 4372 c -= '0'; 4373 else if (isalpha(c)) 4374 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4375 else 4376 break; 4377 if (c >= 16) 4378 break; 4379 if ((i & 1) == 0) 4380 buf[i / 2] |= (c << 4); 4381 else 4382 buf[i / 2] |= c; 4383 } 4384 return ((i + 1) / 2); 4385} 4386 4387/* 4388 * LUN allocation. 4389 * 4390 * Requirements: 4391 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4392 * wants us to allocate the LUN and he can block. 4393 * - ctl_softc is always set 4394 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4395 * 4396 * Returns 0 for success, non-zero (errno) for failure. 4397 */ 4398static int 4399ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4400 struct ctl_be_lun *const be_lun) 4401{ 4402 struct ctl_lun *nlun, *lun; 4403 struct scsi_vpd_id_descriptor *desc; 4404 struct scsi_vpd_id_t10 *t10id; 4405 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4406 int lun_number, i, lun_malloced; 4407 int devidlen, idlen1, idlen2 = 0, len; 4408 4409 if (be_lun == NULL) 4410 return (EINVAL); 4411 4412 /* 4413 * We currently only support Direct Access or Processor LUN types. 4414 */ 4415 switch (be_lun->lun_type) { 4416 case T_DIRECT: 4417 case T_PROCESSOR: 4418 case T_CDROM: 4419 break; 4420 case T_SEQUENTIAL: 4421 case T_CHANGER: 4422 default: 4423 be_lun->lun_config_status(be_lun->be_lun, 4424 CTL_LUN_CONFIG_FAILURE); 4425 break; 4426 } 4427 if (ctl_lun == NULL) { 4428 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4429 lun_malloced = 1; 4430 } else { 4431 lun_malloced = 0; 4432 lun = ctl_lun; 4433 } 4434 4435 memset(lun, 0, sizeof(*lun)); 4436 if (lun_malloced) 4437 lun->flags = CTL_LUN_MALLOCED; 4438 4439 /* Generate LUN ID. */ 4440 devidlen = max(CTL_DEVID_MIN_LEN, 4441 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4442 idlen1 = sizeof(*t10id) + devidlen; 4443 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4444 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4445 if (scsiname != NULL) { 4446 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4447 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4448 } 4449 eui = ctl_get_opt(&be_lun->options, "eui"); 4450 if (eui != NULL) { 4451 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4452 } 4453 naa = ctl_get_opt(&be_lun->options, "naa"); 4454 if (naa != NULL) { 4455 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4456 } 4457 uuid = ctl_get_opt(&be_lun->options, "uuid"); 4458 if (uuid != NULL) { 4459 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4460 } 4461 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4462 M_CTL, M_WAITOK | M_ZERO); 4463 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4464 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4465 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4466 desc->length = idlen1; 4467 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4468 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4469 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4470 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4471 } else { 4472 strncpy(t10id->vendor, vendor, 4473 min(sizeof(t10id->vendor), strlen(vendor))); 4474 } 4475 strncpy((char *)t10id->vendor_spec_id, 4476 (char *)be_lun->device_id, devidlen); 4477 if (scsiname != NULL) { 4478 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4479 desc->length); 4480 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4481 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4482 SVPD_ID_TYPE_SCSI_NAME; 4483 desc->length = idlen2; 4484 strlcpy(desc->identifier, scsiname, idlen2); 4485 } 4486 if (eui != NULL) { 4487 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4488 desc->length); 4489 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4490 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4491 SVPD_ID_TYPE_EUI64; 4492 desc->length = hex2bin(eui, desc->identifier, 16); 4493 desc->length = desc->length > 12 ? 16 : 4494 (desc->length > 8 ? 12 : 8); 4495 len -= 16 - desc->length; 4496 } 4497 if (naa != NULL) { 4498 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4499 desc->length); 4500 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4501 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4502 SVPD_ID_TYPE_NAA; 4503 desc->length = hex2bin(naa, desc->identifier, 16); 4504 desc->length = desc->length > 8 ? 16 : 8; 4505 len -= 16 - desc->length; 4506 } 4507 if (uuid != NULL) { 4508 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4509 desc->length); 4510 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4511 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4512 SVPD_ID_TYPE_UUID; 4513 desc->identifier[0] = 0x10; 4514 hex2bin(uuid, &desc->identifier[2], 16); 4515 desc->length = 18; 4516 } 4517 lun->lun_devid->len = len; 4518 4519 mtx_lock(&ctl_softc->ctl_lock); 4520 /* 4521 * See if the caller requested a particular LUN number. If so, see 4522 * if it is available. Otherwise, allocate the first available LUN. 4523 */ 4524 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4525 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4526 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4527 mtx_unlock(&ctl_softc->ctl_lock); 4528 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4529 printf("ctl: requested LUN ID %d is higher " 4530 "than CTL_MAX_LUNS - 1 (%d)\n", 4531 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4532 } else { 4533 /* 4534 * XXX KDM return an error, or just assign 4535 * another LUN ID in this case?? 4536 */ 4537 printf("ctl: requested LUN ID %d is already " 4538 "in use\n", be_lun->req_lun_id); 4539 } 4540 if (lun->flags & CTL_LUN_MALLOCED) 4541 free(lun, M_CTL); 4542 be_lun->lun_config_status(be_lun->be_lun, 4543 CTL_LUN_CONFIG_FAILURE); 4544 return (ENOSPC); 4545 } 4546 lun_number = be_lun->req_lun_id; 4547 } else { 4548 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4549 if (lun_number == -1) { 4550 mtx_unlock(&ctl_softc->ctl_lock); 4551 printf("ctl: can't allocate LUN, out of LUNs\n"); 4552 if (lun->flags & CTL_LUN_MALLOCED) 4553 free(lun, M_CTL); 4554 be_lun->lun_config_status(be_lun->be_lun, 4555 CTL_LUN_CONFIG_FAILURE); 4556 return (ENOSPC); 4557 } 4558 } 4559 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4560 4561 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4562 lun->lun = lun_number; 4563 lun->be_lun = be_lun; 4564 /* 4565 * The processor LUN is always enabled. Disk LUNs come on line 4566 * disabled, and must be enabled by the backend. 4567 */ 4568 lun->flags |= CTL_LUN_DISABLED; 4569 lun->backend = be_lun->be; 4570 be_lun->ctl_lun = lun; 4571 be_lun->lun_id = lun_number; 4572 atomic_add_int(&be_lun->be->num_luns, 1); 4573 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4574 lun->flags |= CTL_LUN_EJECTED; 4575 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4576 lun->flags |= CTL_LUN_NO_MEDIA; 4577 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4578 lun->flags |= CTL_LUN_STOPPED; 4579 4580 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4581 lun->flags |= CTL_LUN_PRIMARY_SC; 4582 4583 value = ctl_get_opt(&be_lun->options, "removable"); 4584 if (value != NULL) { 4585 if (strcmp(value, "on") == 0) 4586 lun->flags |= CTL_LUN_REMOVABLE; 4587 } else if (be_lun->lun_type == T_CDROM) 4588 lun->flags |= CTL_LUN_REMOVABLE; 4589 4590 lun->ctl_softc = ctl_softc; 4591#ifdef CTL_TIME_IO 4592 lun->last_busy = getsbinuptime(); 4593#endif 4594 TAILQ_INIT(&lun->ooa_queue); 4595 TAILQ_INIT(&lun->blocked_queue); 4596 STAILQ_INIT(&lun->error_list); 4597 lun->ie_reported = 1; 4598 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4599 ctl_tpc_lun_init(lun); 4600 4601 /* 4602 * Initialize the mode and log page index. 4603 */ 4604 ctl_init_page_index(lun); 4605 ctl_init_log_page_index(lun); 4606 4607 /* 4608 * Now, before we insert this lun on the lun list, set the lun 4609 * inventory changed UA for all other luns. 4610 */ 4611 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4612 mtx_lock(&nlun->lun_lock); 4613 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4614 mtx_unlock(&nlun->lun_lock); 4615 } 4616 4617 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4618 4619 ctl_softc->ctl_luns[lun_number] = lun; 4620 4621 ctl_softc->num_luns++; 4622 4623 /* Setup statistics gathering */ 4624 lun->stats.device_type = be_lun->lun_type; 4625 lun->stats.lun_number = lun_number; 4626 lun->stats.blocksize = be_lun->blocksize; 4627 if (be_lun->blocksize == 0) 4628 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4629 for (i = 0;i < CTL_MAX_PORTS;i++) 4630 lun->stats.ports[i].targ_port = i; 4631 4632 mtx_unlock(&ctl_softc->ctl_lock); 4633 4634 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4635 return (0); 4636} 4637 4638/* 4639 * Delete a LUN. 4640 * Assumptions: 4641 * - LUN has already been marked invalid and any pending I/O has been taken 4642 * care of. 4643 */ 4644static int 4645ctl_free_lun(struct ctl_lun *lun) 4646{ 4647 struct ctl_softc *softc; 4648 struct ctl_lun *nlun; 4649 int i; 4650 4651 softc = lun->ctl_softc; 4652 4653 mtx_assert(&softc->ctl_lock, MA_OWNED); 4654 4655 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4656 4657 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4658 4659 softc->ctl_luns[lun->lun] = NULL; 4660 4661 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4662 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4663 4664 softc->num_luns--; 4665 4666 /* 4667 * Tell the backend to free resources, if this LUN has a backend. 4668 */ 4669 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4670 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4671 4672 lun->ie_reportcnt = UINT32_MAX; 4673 callout_drain(&lun->ie_callout); 4674 4675 ctl_tpc_lun_shutdown(lun); 4676 mtx_destroy(&lun->lun_lock); 4677 free(lun->lun_devid, M_CTL); 4678 for (i = 0; i < CTL_MAX_PORTS; i++) 4679 free(lun->pending_ua[i], M_CTL); 4680 for (i = 0; i < CTL_MAX_PORTS; i++) 4681 free(lun->pr_keys[i], M_CTL); 4682 free(lun->write_buffer, M_CTL); 4683 if (lun->flags & CTL_LUN_MALLOCED) 4684 free(lun, M_CTL); 4685 4686 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4687 mtx_lock(&nlun->lun_lock); 4688 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4689 mtx_unlock(&nlun->lun_lock); 4690 } 4691 4692 return (0); 4693} 4694 4695static void 4696ctl_create_lun(struct ctl_be_lun *be_lun) 4697{ 4698 4699 /* 4700 * ctl_alloc_lun() should handle all potential failure cases. 4701 */ 4702 ctl_alloc_lun(control_softc, NULL, be_lun); 4703} 4704 4705int 4706ctl_add_lun(struct ctl_be_lun *be_lun) 4707{ 4708 struct ctl_softc *softc = control_softc; 4709 4710 mtx_lock(&softc->ctl_lock); 4711 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4712 mtx_unlock(&softc->ctl_lock); 4713 wakeup(&softc->pending_lun_queue); 4714 4715 return (0); 4716} 4717 4718int 4719ctl_enable_lun(struct ctl_be_lun *be_lun) 4720{ 4721 struct ctl_softc *softc; 4722 struct ctl_port *port, *nport; 4723 struct ctl_lun *lun; 4724 int retval; 4725 4726 lun = (struct ctl_lun *)be_lun->ctl_lun; 4727 softc = lun->ctl_softc; 4728 4729 mtx_lock(&softc->ctl_lock); 4730 mtx_lock(&lun->lun_lock); 4731 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4732 /* 4733 * eh? Why did we get called if the LUN is already 4734 * enabled? 4735 */ 4736 mtx_unlock(&lun->lun_lock); 4737 mtx_unlock(&softc->ctl_lock); 4738 return (0); 4739 } 4740 lun->flags &= ~CTL_LUN_DISABLED; 4741 mtx_unlock(&lun->lun_lock); 4742 4743 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4744 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4745 port->lun_map != NULL || port->lun_enable == NULL) 4746 continue; 4747 4748 /* 4749 * Drop the lock while we call the FETD's enable routine. 4750 * This can lead to a callback into CTL (at least in the 4751 * case of the internal initiator frontend. 4752 */ 4753 mtx_unlock(&softc->ctl_lock); 4754 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4755 mtx_lock(&softc->ctl_lock); 4756 if (retval != 0) { 4757 printf("%s: FETD %s port %d returned error " 4758 "%d for lun_enable on lun %jd\n", 4759 __func__, port->port_name, port->targ_port, 4760 retval, (intmax_t)lun->lun); 4761 } 4762 } 4763 4764 mtx_unlock(&softc->ctl_lock); 4765 ctl_isc_announce_lun(lun); 4766 4767 return (0); 4768} 4769 4770int 4771ctl_disable_lun(struct ctl_be_lun *be_lun) 4772{ 4773 struct ctl_softc *softc; 4774 struct ctl_port *port; 4775 struct ctl_lun *lun; 4776 int retval; 4777 4778 lun = (struct ctl_lun *)be_lun->ctl_lun; 4779 softc = lun->ctl_softc; 4780 4781 mtx_lock(&softc->ctl_lock); 4782 mtx_lock(&lun->lun_lock); 4783 if (lun->flags & CTL_LUN_DISABLED) { 4784 mtx_unlock(&lun->lun_lock); 4785 mtx_unlock(&softc->ctl_lock); 4786 return (0); 4787 } 4788 lun->flags |= CTL_LUN_DISABLED; 4789 mtx_unlock(&lun->lun_lock); 4790 4791 STAILQ_FOREACH(port, &softc->port_list, links) { 4792 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4793 port->lun_map != NULL || port->lun_disable == NULL) 4794 continue; 4795 4796 /* 4797 * Drop the lock before we call the frontend's disable 4798 * routine, to avoid lock order reversals. 4799 * 4800 * XXX KDM what happens if the frontend list changes while 4801 * we're traversing it? It's unlikely, but should be handled. 4802 */ 4803 mtx_unlock(&softc->ctl_lock); 4804 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4805 mtx_lock(&softc->ctl_lock); 4806 if (retval != 0) { 4807 printf("%s: FETD %s port %d returned error " 4808 "%d for lun_disable on lun %jd\n", 4809 __func__, port->port_name, port->targ_port, 4810 retval, (intmax_t)lun->lun); 4811 } 4812 } 4813 4814 mtx_unlock(&softc->ctl_lock); 4815 ctl_isc_announce_lun(lun); 4816 4817 return (0); 4818} 4819 4820int 4821ctl_start_lun(struct ctl_be_lun *be_lun) 4822{ 4823 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4824 4825 mtx_lock(&lun->lun_lock); 4826 lun->flags &= ~CTL_LUN_STOPPED; 4827 mtx_unlock(&lun->lun_lock); 4828 return (0); 4829} 4830 4831int 4832ctl_stop_lun(struct ctl_be_lun *be_lun) 4833{ 4834 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4835 4836 mtx_lock(&lun->lun_lock); 4837 lun->flags |= CTL_LUN_STOPPED; 4838 mtx_unlock(&lun->lun_lock); 4839 return (0); 4840} 4841 4842int 4843ctl_lun_no_media(struct ctl_be_lun *be_lun) 4844{ 4845 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4846 4847 mtx_lock(&lun->lun_lock); 4848 lun->flags |= CTL_LUN_NO_MEDIA; 4849 mtx_unlock(&lun->lun_lock); 4850 return (0); 4851} 4852 4853int 4854ctl_lun_has_media(struct ctl_be_lun *be_lun) 4855{ 4856 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4857 union ctl_ha_msg msg; 4858 4859 mtx_lock(&lun->lun_lock); 4860 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4861 if (lun->flags & CTL_LUN_REMOVABLE) 4862 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4863 mtx_unlock(&lun->lun_lock); 4864 if ((lun->flags & CTL_LUN_REMOVABLE) && 4865 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4866 bzero(&msg.ua, sizeof(msg.ua)); 4867 msg.hdr.msg_type = CTL_MSG_UA; 4868 msg.hdr.nexus.initid = -1; 4869 msg.hdr.nexus.targ_port = -1; 4870 msg.hdr.nexus.targ_lun = lun->lun; 4871 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4872 msg.ua.ua_all = 1; 4873 msg.ua.ua_set = 1; 4874 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4876 M_WAITOK); 4877 } 4878 return (0); 4879} 4880 4881int 4882ctl_lun_ejected(struct ctl_be_lun *be_lun) 4883{ 4884 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4885 4886 mtx_lock(&lun->lun_lock); 4887 lun->flags |= CTL_LUN_EJECTED; 4888 mtx_unlock(&lun->lun_lock); 4889 return (0); 4890} 4891 4892int 4893ctl_lun_primary(struct ctl_be_lun *be_lun) 4894{ 4895 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4896 4897 mtx_lock(&lun->lun_lock); 4898 lun->flags |= CTL_LUN_PRIMARY_SC; 4899 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4900 mtx_unlock(&lun->lun_lock); 4901 ctl_isc_announce_lun(lun); 4902 return (0); 4903} 4904 4905int 4906ctl_lun_secondary(struct ctl_be_lun *be_lun) 4907{ 4908 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4909 4910 mtx_lock(&lun->lun_lock); 4911 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4912 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4913 mtx_unlock(&lun->lun_lock); 4914 ctl_isc_announce_lun(lun); 4915 return (0); 4916} 4917 4918int 4919ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4920{ 4921 struct ctl_softc *softc; 4922 struct ctl_lun *lun; 4923 4924 lun = (struct ctl_lun *)be_lun->ctl_lun; 4925 softc = lun->ctl_softc; 4926 4927 mtx_lock(&lun->lun_lock); 4928 4929 /* 4930 * The LUN needs to be disabled before it can be marked invalid. 4931 */ 4932 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4933 mtx_unlock(&lun->lun_lock); 4934 return (-1); 4935 } 4936 /* 4937 * Mark the LUN invalid. 4938 */ 4939 lun->flags |= CTL_LUN_INVALID; 4940 4941 /* 4942 * If there is nothing in the OOA queue, go ahead and free the LUN. 4943 * If we have something in the OOA queue, we'll free it when the 4944 * last I/O completes. 4945 */ 4946 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4947 mtx_unlock(&lun->lun_lock); 4948 mtx_lock(&softc->ctl_lock); 4949 ctl_free_lun(lun); 4950 mtx_unlock(&softc->ctl_lock); 4951 } else 4952 mtx_unlock(&lun->lun_lock); 4953 4954 return (0); 4955} 4956 4957void 4958ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4959{ 4960 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4961 union ctl_ha_msg msg; 4962 4963 mtx_lock(&lun->lun_lock); 4964 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4965 mtx_unlock(&lun->lun_lock); 4966 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4967 /* Send msg to other side. */ 4968 bzero(&msg.ua, sizeof(msg.ua)); 4969 msg.hdr.msg_type = CTL_MSG_UA; 4970 msg.hdr.nexus.initid = -1; 4971 msg.hdr.nexus.targ_port = -1; 4972 msg.hdr.nexus.targ_lun = lun->lun; 4973 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4974 msg.ua.ua_all = 1; 4975 msg.ua.ua_set = 1; 4976 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4977 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4978 M_WAITOK); 4979 } 4980} 4981 4982/* 4983 * Backend "memory move is complete" callback for requests that never 4984 * make it down to say RAIDCore's configuration code. 4985 */ 4986int 4987ctl_config_move_done(union ctl_io *io) 4988{ 4989 int retval; 4990 4991 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4992 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4993 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4994 4995 if ((io->io_hdr.port_status != 0) && 4996 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4997 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4998 /* 4999 * For hardware error sense keys, the sense key 5000 * specific value is defined to be a retry count, 5001 * but we use it to pass back an internal FETD 5002 * error code. XXX KDM Hopefully the FETD is only 5003 * using 16 bits for an error code, since that's 5004 * all the space we have in the sks field. 5005 */ 5006 ctl_set_internal_failure(&io->scsiio, 5007 /*sks_valid*/ 1, 5008 /*retry_count*/ 5009 io->io_hdr.port_status); 5010 } 5011 5012 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5013 ctl_data_print(io); 5014 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5015 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5016 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5017 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5018 /* 5019 * XXX KDM just assuming a single pointer here, and not a 5020 * S/G list. If we start using S/G lists for config data, 5021 * we'll need to know how to clean them up here as well. 5022 */ 5023 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5024 free(io->scsiio.kern_data_ptr, M_CTL); 5025 ctl_done(io); 5026 retval = CTL_RETVAL_COMPLETE; 5027 } else { 5028 /* 5029 * XXX KDM now we need to continue data movement. Some 5030 * options: 5031 * - call ctl_scsiio() again? We don't do this for data 5032 * writes, because for those at least we know ahead of 5033 * time where the write will go and how long it is. For 5034 * config writes, though, that information is largely 5035 * contained within the write itself, thus we need to 5036 * parse out the data again. 5037 * 5038 * - Call some other function once the data is in? 5039 */ 5040 5041 /* 5042 * XXX KDM call ctl_scsiio() again for now, and check flag 5043 * bits to see whether we're allocated or not. 5044 */ 5045 retval = ctl_scsiio(&io->scsiio); 5046 } 5047 return (retval); 5048} 5049 5050/* 5051 * This gets called by a backend driver when it is done with a 5052 * data_submit method. 5053 */ 5054void 5055ctl_data_submit_done(union ctl_io *io) 5056{ 5057 /* 5058 * If the IO_CONT flag is set, we need to call the supplied 5059 * function to continue processing the I/O, instead of completing 5060 * the I/O just yet. 5061 * 5062 * If there is an error, though, we don't want to keep processing. 5063 * Instead, just send status back to the initiator. 5064 */ 5065 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5066 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5067 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5068 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5069 io->scsiio.io_cont(io); 5070 return; 5071 } 5072 ctl_done(io); 5073} 5074 5075/* 5076 * This gets called by a backend driver when it is done with a 5077 * configuration write. 5078 */ 5079void 5080ctl_config_write_done(union ctl_io *io) 5081{ 5082 uint8_t *buf; 5083 5084 /* 5085 * If the IO_CONT flag is set, we need to call the supplied 5086 * function to continue processing the I/O, instead of completing 5087 * the I/O just yet. 5088 * 5089 * If there is an error, though, we don't want to keep processing. 5090 * Instead, just send status back to the initiator. 5091 */ 5092 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5093 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5094 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5095 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5096 io->scsiio.io_cont(io); 5097 return; 5098 } 5099 /* 5100 * Since a configuration write can be done for commands that actually 5101 * have data allocated, like write buffer, and commands that have 5102 * no data, like start/stop unit, we need to check here. 5103 */ 5104 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5105 buf = io->scsiio.kern_data_ptr; 5106 else 5107 buf = NULL; 5108 ctl_done(io); 5109 if (buf) 5110 free(buf, M_CTL); 5111} 5112 5113void 5114ctl_config_read_done(union ctl_io *io) 5115{ 5116 uint8_t *buf; 5117 5118 /* 5119 * If there is some error -- we are done, skip data transfer. 5120 */ 5121 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5122 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5123 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5124 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5125 buf = io->scsiio.kern_data_ptr; 5126 else 5127 buf = NULL; 5128 ctl_done(io); 5129 if (buf) 5130 free(buf, M_CTL); 5131 return; 5132 } 5133 5134 /* 5135 * If the IO_CONT flag is set, we need to call the supplied 5136 * function to continue processing the I/O, instead of completing 5137 * the I/O just yet. 5138 */ 5139 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5140 io->scsiio.io_cont(io); 5141 return; 5142 } 5143 5144 ctl_datamove(io); 5145} 5146 5147/* 5148 * SCSI release command. 5149 */ 5150int 5151ctl_scsi_release(struct ctl_scsiio *ctsio) 5152{ 5153 struct ctl_lun *lun; 5154 uint32_t residx; 5155 5156 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5157 5158 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5159 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5160 5161 /* 5162 * XXX KDM right now, we only support LUN reservation. We don't 5163 * support 3rd party reservations, or extent reservations, which 5164 * might actually need the parameter list. If we've gotten this 5165 * far, we've got a LUN reservation. Anything else got kicked out 5166 * above. So, according to SPC, ignore the length. 5167 */ 5168 5169 mtx_lock(&lun->lun_lock); 5170 5171 /* 5172 * According to SPC, it is not an error for an intiator to attempt 5173 * to release a reservation on a LUN that isn't reserved, or that 5174 * is reserved by another initiator. The reservation can only be 5175 * released, though, by the initiator who made it or by one of 5176 * several reset type events. 5177 */ 5178 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5179 lun->flags &= ~CTL_LUN_RESERVED; 5180 5181 mtx_unlock(&lun->lun_lock); 5182 5183 ctl_set_success(ctsio); 5184 ctl_done((union ctl_io *)ctsio); 5185 return (CTL_RETVAL_COMPLETE); 5186} 5187 5188int 5189ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5190{ 5191 struct ctl_lun *lun; 5192 uint32_t residx; 5193 5194 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5195 5196 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5197 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5198 5199 /* 5200 * XXX KDM right now, we only support LUN reservation. We don't 5201 * support 3rd party reservations, or extent reservations, which 5202 * might actually need the parameter list. If we've gotten this 5203 * far, we've got a LUN reservation. Anything else got kicked out 5204 * above. So, according to SPC, ignore the length. 5205 */ 5206 5207 mtx_lock(&lun->lun_lock); 5208 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5209 ctl_set_reservation_conflict(ctsio); 5210 goto bailout; 5211 } 5212 5213 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5214 if (lun->flags & CTL_LUN_PR_RESERVED) { 5215 ctl_set_success(ctsio); 5216 goto bailout; 5217 } 5218 5219 lun->flags |= CTL_LUN_RESERVED; 5220 lun->res_idx = residx; 5221 ctl_set_success(ctsio); 5222 5223bailout: 5224 mtx_unlock(&lun->lun_lock); 5225 ctl_done((union ctl_io *)ctsio); 5226 return (CTL_RETVAL_COMPLETE); 5227} 5228 5229int 5230ctl_start_stop(struct ctl_scsiio *ctsio) 5231{ 5232 struct scsi_start_stop_unit *cdb; 5233 struct ctl_lun *lun; 5234 int retval; 5235 5236 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5237 5238 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5239 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5240 5241 if ((cdb->how & SSS_PC_MASK) == 0) { 5242 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5243 (cdb->how & SSS_START) == 0) { 5244 uint32_t residx; 5245 5246 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5247 if (ctl_get_prkey(lun, residx) == 0 || 5248 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5249 5250 ctl_set_reservation_conflict(ctsio); 5251 ctl_done((union ctl_io *)ctsio); 5252 return (CTL_RETVAL_COMPLETE); 5253 } 5254 } 5255 5256 if ((cdb->how & SSS_LOEJ) && 5257 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5258 ctl_set_invalid_field(ctsio, 5259 /*sks_valid*/ 1, 5260 /*command*/ 1, 5261 /*field*/ 4, 5262 /*bit_valid*/ 1, 5263 /*bit*/ 1); 5264 ctl_done((union ctl_io *)ctsio); 5265 return (CTL_RETVAL_COMPLETE); 5266 } 5267 5268 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5269 lun->prevent_count > 0) { 5270 /* "Medium removal prevented" */ 5271 ctl_set_sense(ctsio, /*current_error*/ 1, 5272 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5273 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5274 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5275 ctl_done((union ctl_io *)ctsio); 5276 return (CTL_RETVAL_COMPLETE); 5277 } 5278 } 5279 5280 retval = lun->backend->config_write((union ctl_io *)ctsio); 5281 return (retval); 5282} 5283 5284int 5285ctl_prevent_allow(struct ctl_scsiio *ctsio) 5286{ 5287 struct ctl_lun *lun; 5288 struct scsi_prevent *cdb; 5289 int retval; 5290 uint32_t initidx; 5291 5292 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5293 5294 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5295 cdb = (struct scsi_prevent *)ctsio->cdb; 5296 5297 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5298 ctl_set_invalid_opcode(ctsio); 5299 ctl_done((union ctl_io *)ctsio); 5300 return (CTL_RETVAL_COMPLETE); 5301 } 5302 5303 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5304 mtx_lock(&lun->lun_lock); 5305 if ((cdb->how & PR_PREVENT) && 5306 ctl_is_set(lun->prevent, initidx) == 0) { 5307 ctl_set_mask(lun->prevent, initidx); 5308 lun->prevent_count++; 5309 } else if ((cdb->how & PR_PREVENT) == 0 && 5310 ctl_is_set(lun->prevent, initidx)) { 5311 ctl_clear_mask(lun->prevent, initidx); 5312 lun->prevent_count--; 5313 } 5314 mtx_unlock(&lun->lun_lock); 5315 retval = lun->backend->config_write((union ctl_io *)ctsio); 5316 return (retval); 5317} 5318 5319/* 5320 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5321 * we don't really do anything with the LBA and length fields if the user 5322 * passes them in. Instead we'll just flush out the cache for the entire 5323 * LUN. 5324 */ 5325int 5326ctl_sync_cache(struct ctl_scsiio *ctsio) 5327{ 5328 struct ctl_lun *lun; 5329 struct ctl_softc *softc; 5330 struct ctl_lba_len_flags *lbalen; 5331 uint64_t starting_lba; 5332 uint32_t block_count; 5333 int retval; 5334 uint8_t byte2; 5335 5336 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5337 5338 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5339 softc = lun->ctl_softc; 5340 retval = 0; 5341 5342 switch (ctsio->cdb[0]) { 5343 case SYNCHRONIZE_CACHE: { 5344 struct scsi_sync_cache *cdb; 5345 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5346 5347 starting_lba = scsi_4btoul(cdb->begin_lba); 5348 block_count = scsi_2btoul(cdb->lb_count); 5349 byte2 = cdb->byte2; 5350 break; 5351 } 5352 case SYNCHRONIZE_CACHE_16: { 5353 struct scsi_sync_cache_16 *cdb; 5354 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5355 5356 starting_lba = scsi_8btou64(cdb->begin_lba); 5357 block_count = scsi_4btoul(cdb->lb_count); 5358 byte2 = cdb->byte2; 5359 break; 5360 } 5361 default: 5362 ctl_set_invalid_opcode(ctsio); 5363 ctl_done((union ctl_io *)ctsio); 5364 goto bailout; 5365 break; /* NOTREACHED */ 5366 } 5367 5368 /* 5369 * We check the LBA and length, but don't do anything with them. 5370 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5371 * get flushed. This check will just help satisfy anyone who wants 5372 * to see an error for an out of range LBA. 5373 */ 5374 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5375 ctl_set_lba_out_of_range(ctsio, 5376 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5377 ctl_done((union ctl_io *)ctsio); 5378 goto bailout; 5379 } 5380 5381 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5382 lbalen->lba = starting_lba; 5383 lbalen->len = block_count; 5384 lbalen->flags = byte2; 5385 retval = lun->backend->config_write((union ctl_io *)ctsio); 5386 5387bailout: 5388 return (retval); 5389} 5390 5391int 5392ctl_format(struct ctl_scsiio *ctsio) 5393{ 5394 struct scsi_format *cdb; 5395 struct ctl_lun *lun; 5396 int length, defect_list_len; 5397 5398 CTL_DEBUG_PRINT(("ctl_format\n")); 5399 5400 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5401 5402 cdb = (struct scsi_format *)ctsio->cdb; 5403 5404 length = 0; 5405 if (cdb->byte2 & SF_FMTDATA) { 5406 if (cdb->byte2 & SF_LONGLIST) 5407 length = sizeof(struct scsi_format_header_long); 5408 else 5409 length = sizeof(struct scsi_format_header_short); 5410 } 5411 5412 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5413 && (length > 0)) { 5414 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5415 ctsio->kern_data_len = length; 5416 ctsio->kern_total_len = length; 5417 ctsio->kern_data_resid = 0; 5418 ctsio->kern_rel_offset = 0; 5419 ctsio->kern_sg_entries = 0; 5420 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5421 ctsio->be_move_done = ctl_config_move_done; 5422 ctl_datamove((union ctl_io *)ctsio); 5423 5424 return (CTL_RETVAL_COMPLETE); 5425 } 5426 5427 defect_list_len = 0; 5428 5429 if (cdb->byte2 & SF_FMTDATA) { 5430 if (cdb->byte2 & SF_LONGLIST) { 5431 struct scsi_format_header_long *header; 5432 5433 header = (struct scsi_format_header_long *) 5434 ctsio->kern_data_ptr; 5435 5436 defect_list_len = scsi_4btoul(header->defect_list_len); 5437 if (defect_list_len != 0) { 5438 ctl_set_invalid_field(ctsio, 5439 /*sks_valid*/ 1, 5440 /*command*/ 0, 5441 /*field*/ 2, 5442 /*bit_valid*/ 0, 5443 /*bit*/ 0); 5444 goto bailout; 5445 } 5446 } else { 5447 struct scsi_format_header_short *header; 5448 5449 header = (struct scsi_format_header_short *) 5450 ctsio->kern_data_ptr; 5451 5452 defect_list_len = scsi_2btoul(header->defect_list_len); 5453 if (defect_list_len != 0) { 5454 ctl_set_invalid_field(ctsio, 5455 /*sks_valid*/ 1, 5456 /*command*/ 0, 5457 /*field*/ 2, 5458 /*bit_valid*/ 0, 5459 /*bit*/ 0); 5460 goto bailout; 5461 } 5462 } 5463 } 5464 5465 ctl_set_success(ctsio); 5466bailout: 5467 5468 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5469 free(ctsio->kern_data_ptr, M_CTL); 5470 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5471 } 5472 5473 ctl_done((union ctl_io *)ctsio); 5474 return (CTL_RETVAL_COMPLETE); 5475} 5476 5477int 5478ctl_read_buffer(struct ctl_scsiio *ctsio) 5479{ 5480 struct ctl_lun *lun; 5481 uint64_t buffer_offset; 5482 uint32_t len; 5483 uint8_t byte2; 5484 static uint8_t descr[4]; 5485 static uint8_t echo_descr[4] = { 0 }; 5486 5487 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5489 switch (ctsio->cdb[0]) { 5490 case READ_BUFFER: { 5491 struct scsi_read_buffer *cdb; 5492 5493 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5494 buffer_offset = scsi_3btoul(cdb->offset); 5495 len = scsi_3btoul(cdb->length); 5496 byte2 = cdb->byte2; 5497 break; 5498 } 5499 case READ_BUFFER_16: { 5500 struct scsi_read_buffer_16 *cdb; 5501 5502 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5503 buffer_offset = scsi_8btou64(cdb->offset); 5504 len = scsi_4btoul(cdb->length); 5505 byte2 = cdb->byte2; 5506 break; 5507 } 5508 default: /* This shouldn't happen. */ 5509 ctl_set_invalid_opcode(ctsio); 5510 ctl_done((union ctl_io *)ctsio); 5511 return (CTL_RETVAL_COMPLETE); 5512 } 5513 5514 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5515 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5516 ctl_set_invalid_field(ctsio, 5517 /*sks_valid*/ 1, 5518 /*command*/ 1, 5519 /*field*/ 6, 5520 /*bit_valid*/ 0, 5521 /*bit*/ 0); 5522 ctl_done((union ctl_io *)ctsio); 5523 return (CTL_RETVAL_COMPLETE); 5524 } 5525 5526 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5527 descr[0] = 0; 5528 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5529 ctsio->kern_data_ptr = descr; 5530 len = min(len, sizeof(descr)); 5531 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5532 ctsio->kern_data_ptr = echo_descr; 5533 len = min(len, sizeof(echo_descr)); 5534 } else { 5535 if (lun->write_buffer == NULL) { 5536 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5537 M_CTL, M_WAITOK); 5538 } 5539 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5540 } 5541 ctsio->kern_data_len = len; 5542 ctsio->kern_total_len = len; 5543 ctsio->kern_data_resid = 0; 5544 ctsio->kern_rel_offset = 0; 5545 ctsio->kern_sg_entries = 0; 5546 ctl_set_success(ctsio); 5547 ctsio->be_move_done = ctl_config_move_done; 5548 ctl_datamove((union ctl_io *)ctsio); 5549 return (CTL_RETVAL_COMPLETE); 5550} 5551 5552int 5553ctl_write_buffer(struct ctl_scsiio *ctsio) 5554{ 5555 struct scsi_write_buffer *cdb; 5556 struct ctl_lun *lun; 5557 int buffer_offset, len; 5558 5559 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5560 5561 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5562 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5563 5564 len = scsi_3btoul(cdb->length); 5565 buffer_offset = scsi_3btoul(cdb->offset); 5566 5567 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5568 ctl_set_invalid_field(ctsio, 5569 /*sks_valid*/ 1, 5570 /*command*/ 1, 5571 /*field*/ 6, 5572 /*bit_valid*/ 0, 5573 /*bit*/ 0); 5574 ctl_done((union ctl_io *)ctsio); 5575 return (CTL_RETVAL_COMPLETE); 5576 } 5577 5578 /* 5579 * If we've got a kernel request that hasn't been malloced yet, 5580 * malloc it and tell the caller the data buffer is here. 5581 */ 5582 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5583 if (lun->write_buffer == NULL) { 5584 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5585 M_CTL, M_WAITOK); 5586 } 5587 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5588 ctsio->kern_data_len = len; 5589 ctsio->kern_total_len = len; 5590 ctsio->kern_data_resid = 0; 5591 ctsio->kern_rel_offset = 0; 5592 ctsio->kern_sg_entries = 0; 5593 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5594 ctsio->be_move_done = ctl_config_move_done; 5595 ctl_datamove((union ctl_io *)ctsio); 5596 5597 return (CTL_RETVAL_COMPLETE); 5598 } 5599 5600 ctl_set_success(ctsio); 5601 ctl_done((union ctl_io *)ctsio); 5602 return (CTL_RETVAL_COMPLETE); 5603} 5604 5605int 5606ctl_write_same(struct ctl_scsiio *ctsio) 5607{ 5608 struct ctl_lun *lun; 5609 struct ctl_lba_len_flags *lbalen; 5610 uint64_t lba; 5611 uint32_t num_blocks; 5612 int len, retval; 5613 uint8_t byte2; 5614 5615 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5616 5617 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5618 5619 switch (ctsio->cdb[0]) { 5620 case WRITE_SAME_10: { 5621 struct scsi_write_same_10 *cdb; 5622 5623 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5624 5625 lba = scsi_4btoul(cdb->addr); 5626 num_blocks = scsi_2btoul(cdb->length); 5627 byte2 = cdb->byte2; 5628 break; 5629 } 5630 case WRITE_SAME_16: { 5631 struct scsi_write_same_16 *cdb; 5632 5633 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5634 5635 lba = scsi_8btou64(cdb->addr); 5636 num_blocks = scsi_4btoul(cdb->length); 5637 byte2 = cdb->byte2; 5638 break; 5639 } 5640 default: 5641 /* 5642 * We got a command we don't support. This shouldn't 5643 * happen, commands should be filtered out above us. 5644 */ 5645 ctl_set_invalid_opcode(ctsio); 5646 ctl_done((union ctl_io *)ctsio); 5647 5648 return (CTL_RETVAL_COMPLETE); 5649 break; /* NOTREACHED */ 5650 } 5651 5652 /* ANCHOR flag can be used only together with UNMAP */ 5653 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5654 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5655 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5656 ctl_done((union ctl_io *)ctsio); 5657 return (CTL_RETVAL_COMPLETE); 5658 } 5659 5660 /* 5661 * The first check is to make sure we're in bounds, the second 5662 * check is to catch wrap-around problems. If the lba + num blocks 5663 * is less than the lba, then we've wrapped around and the block 5664 * range is invalid anyway. 5665 */ 5666 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5667 || ((lba + num_blocks) < lba)) { 5668 ctl_set_lba_out_of_range(ctsio, 5669 MAX(lba, lun->be_lun->maxlba + 1)); 5670 ctl_done((union ctl_io *)ctsio); 5671 return (CTL_RETVAL_COMPLETE); 5672 } 5673 5674 /* Zero number of blocks means "to the last logical block" */ 5675 if (num_blocks == 0) { 5676 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5677 ctl_set_invalid_field(ctsio, 5678 /*sks_valid*/ 0, 5679 /*command*/ 1, 5680 /*field*/ 0, 5681 /*bit_valid*/ 0, 5682 /*bit*/ 0); 5683 ctl_done((union ctl_io *)ctsio); 5684 return (CTL_RETVAL_COMPLETE); 5685 } 5686 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5687 } 5688 5689 len = lun->be_lun->blocksize; 5690 5691 /* 5692 * If we've got a kernel request that hasn't been malloced yet, 5693 * malloc it and tell the caller the data buffer is here. 5694 */ 5695 if ((byte2 & SWS_NDOB) == 0 && 5696 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5697 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5698 ctsio->kern_data_len = len; 5699 ctsio->kern_total_len = len; 5700 ctsio->kern_data_resid = 0; 5701 ctsio->kern_rel_offset = 0; 5702 ctsio->kern_sg_entries = 0; 5703 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5704 ctsio->be_move_done = ctl_config_move_done; 5705 ctl_datamove((union ctl_io *)ctsio); 5706 5707 return (CTL_RETVAL_COMPLETE); 5708 } 5709 5710 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5711 lbalen->lba = lba; 5712 lbalen->len = num_blocks; 5713 lbalen->flags = byte2; 5714 retval = lun->backend->config_write((union ctl_io *)ctsio); 5715 5716 return (retval); 5717} 5718 5719int 5720ctl_unmap(struct ctl_scsiio *ctsio) 5721{ 5722 struct ctl_lun *lun; 5723 struct scsi_unmap *cdb; 5724 struct ctl_ptr_len_flags *ptrlen; 5725 struct scsi_unmap_header *hdr; 5726 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5727 uint64_t lba; 5728 uint32_t num_blocks; 5729 int len, retval; 5730 uint8_t byte2; 5731 5732 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5733 5734 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5735 cdb = (struct scsi_unmap *)ctsio->cdb; 5736 5737 len = scsi_2btoul(cdb->length); 5738 byte2 = cdb->byte2; 5739 5740 /* 5741 * If we've got a kernel request that hasn't been malloced yet, 5742 * malloc it and tell the caller the data buffer is here. 5743 */ 5744 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5745 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5746 ctsio->kern_data_len = len; 5747 ctsio->kern_total_len = len; 5748 ctsio->kern_data_resid = 0; 5749 ctsio->kern_rel_offset = 0; 5750 ctsio->kern_sg_entries = 0; 5751 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5752 ctsio->be_move_done = ctl_config_move_done; 5753 ctl_datamove((union ctl_io *)ctsio); 5754 5755 return (CTL_RETVAL_COMPLETE); 5756 } 5757 5758 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5759 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5760 if (len < sizeof (*hdr) || 5761 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5762 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5763 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5764 ctl_set_invalid_field(ctsio, 5765 /*sks_valid*/ 0, 5766 /*command*/ 0, 5767 /*field*/ 0, 5768 /*bit_valid*/ 0, 5769 /*bit*/ 0); 5770 goto done; 5771 } 5772 len = scsi_2btoul(hdr->desc_length); 5773 buf = (struct scsi_unmap_desc *)(hdr + 1); 5774 end = buf + len / sizeof(*buf); 5775 5776 endnz = buf; 5777 for (range = buf; range < end; range++) { 5778 lba = scsi_8btou64(range->lba); 5779 num_blocks = scsi_4btoul(range->length); 5780 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5781 || ((lba + num_blocks) < lba)) { 5782 ctl_set_lba_out_of_range(ctsio, 5783 MAX(lba, lun->be_lun->maxlba + 1)); 5784 ctl_done((union ctl_io *)ctsio); 5785 return (CTL_RETVAL_COMPLETE); 5786 } 5787 if (num_blocks != 0) 5788 endnz = range + 1; 5789 } 5790 5791 /* 5792 * Block backend can not handle zero last range. 5793 * Filter it out and return if there is nothing left. 5794 */ 5795 len = (uint8_t *)endnz - (uint8_t *)buf; 5796 if (len == 0) { 5797 ctl_set_success(ctsio); 5798 goto done; 5799 } 5800 5801 mtx_lock(&lun->lun_lock); 5802 ptrlen = (struct ctl_ptr_len_flags *) 5803 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5804 ptrlen->ptr = (void *)buf; 5805 ptrlen->len = len; 5806 ptrlen->flags = byte2; 5807 ctl_check_blocked(lun); 5808 mtx_unlock(&lun->lun_lock); 5809 5810 retval = lun->backend->config_write((union ctl_io *)ctsio); 5811 return (retval); 5812 5813done: 5814 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5815 free(ctsio->kern_data_ptr, M_CTL); 5816 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5817 } 5818 ctl_done((union ctl_io *)ctsio); 5819 return (CTL_RETVAL_COMPLETE); 5820} 5821 5822int 5823ctl_default_page_handler(struct ctl_scsiio *ctsio, 5824 struct ctl_page_index *page_index, uint8_t *page_ptr) 5825{ 5826 struct ctl_lun *lun; 5827 uint8_t *current_cp, *saved_cp; 5828 int set_ua; 5829 uint32_t initidx; 5830 5831 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5832 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5833 set_ua = 0; 5834 5835 current_cp = (page_index->page_data + (page_index->page_len * 5836 CTL_PAGE_CURRENT)); 5837 saved_cp = (page_index->page_data + (page_index->page_len * 5838 CTL_PAGE_SAVED)); 5839 5840 mtx_lock(&lun->lun_lock); 5841 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5842 memcpy(current_cp, page_ptr, page_index->page_len); 5843 memcpy(saved_cp, page_ptr, page_index->page_len); 5844 set_ua = 1; 5845 } 5846 if (set_ua != 0) 5847 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5848 mtx_unlock(&lun->lun_lock); 5849 if (set_ua) { 5850 ctl_isc_announce_mode(lun, 5851 ctl_get_initindex(&ctsio->io_hdr.nexus), 5852 page_index->page_code, page_index->subpage); 5853 } 5854 return (CTL_RETVAL_COMPLETE); 5855} 5856 5857static void 5858ctl_ie_timer(void *arg) 5859{ 5860 struct ctl_lun *lun = arg; 5861 uint64_t t; 5862 5863 if (lun->ie_asc == 0) 5864 return; 5865 5866 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5867 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5868 else 5869 lun->ie_reported = 0; 5870 5871 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5872 lun->ie_reportcnt++; 5873 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5874 if (t == 0 || t == UINT32_MAX) 5875 t = 3000; /* 5 min */ 5876 callout_schedule(&lun->ie_callout, t * hz / 10); 5877 } 5878} 5879 5880int 5881ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5882 struct ctl_page_index *page_index, uint8_t *page_ptr) 5883{ 5884 struct scsi_info_exceptions_page *pg; 5885 struct ctl_lun *lun; 5886 uint64_t t; 5887 5888 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5889 5890 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5891 pg = (struct scsi_info_exceptions_page *)page_ptr; 5892 mtx_lock(&lun->lun_lock); 5893 if (pg->info_flags & SIEP_FLAGS_TEST) { 5894 lun->ie_asc = 0x5d; 5895 lun->ie_ascq = 0xff; 5896 if (pg->mrie == SIEP_MRIE_UA) { 5897 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5898 lun->ie_reported = 1; 5899 } else { 5900 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5901 lun->ie_reported = -1; 5902 } 5903 lun->ie_reportcnt = 1; 5904 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5905 lun->ie_reportcnt++; 5906 t = scsi_4btoul(pg->interval_timer); 5907 if (t == 0 || t == UINT32_MAX) 5908 t = 3000; /* 5 min */ 5909 callout_reset(&lun->ie_callout, t * hz / 10, 5910 ctl_ie_timer, lun); 5911 } 5912 } else { 5913 lun->ie_asc = 0; 5914 lun->ie_ascq = 0; 5915 lun->ie_reported = 1; 5916 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5917 lun->ie_reportcnt = UINT32_MAX; 5918 callout_stop(&lun->ie_callout); 5919 } 5920 mtx_unlock(&lun->lun_lock); 5921 return (CTL_RETVAL_COMPLETE); 5922} 5923 5924static int 5925ctl_do_mode_select(union ctl_io *io) 5926{ 5927 struct scsi_mode_page_header *page_header; 5928 struct ctl_page_index *page_index; 5929 struct ctl_scsiio *ctsio; 5930 int page_len, page_len_offset, page_len_size; 5931 union ctl_modepage_info *modepage_info; 5932 struct ctl_lun *lun; 5933 uint16_t *len_left, *len_used; 5934 int retval, i; 5935 5936 ctsio = &io->scsiio; 5937 page_index = NULL; 5938 page_len = 0; 5939 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5940 5941 modepage_info = (union ctl_modepage_info *) 5942 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5943 len_left = &modepage_info->header.len_left; 5944 len_used = &modepage_info->header.len_used; 5945 5946do_next_page: 5947 5948 page_header = (struct scsi_mode_page_header *) 5949 (ctsio->kern_data_ptr + *len_used); 5950 5951 if (*len_left == 0) { 5952 free(ctsio->kern_data_ptr, M_CTL); 5953 ctl_set_success(ctsio); 5954 ctl_done((union ctl_io *)ctsio); 5955 return (CTL_RETVAL_COMPLETE); 5956 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5957 5958 free(ctsio->kern_data_ptr, M_CTL); 5959 ctl_set_param_len_error(ctsio); 5960 ctl_done((union ctl_io *)ctsio); 5961 return (CTL_RETVAL_COMPLETE); 5962 5963 } else if ((page_header->page_code & SMPH_SPF) 5964 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5965 5966 free(ctsio->kern_data_ptr, M_CTL); 5967 ctl_set_param_len_error(ctsio); 5968 ctl_done((union ctl_io *)ctsio); 5969 return (CTL_RETVAL_COMPLETE); 5970 } 5971 5972 5973 /* 5974 * XXX KDM should we do something with the block descriptor? 5975 */ 5976 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5977 page_index = &lun->mode_pages.index[i]; 5978 if (lun->be_lun->lun_type == T_DIRECT && 5979 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5980 continue; 5981 if (lun->be_lun->lun_type == T_PROCESSOR && 5982 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5983 continue; 5984 if (lun->be_lun->lun_type == T_CDROM && 5985 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5986 continue; 5987 5988 if ((page_index->page_code & SMPH_PC_MASK) != 5989 (page_header->page_code & SMPH_PC_MASK)) 5990 continue; 5991 5992 /* 5993 * If neither page has a subpage code, then we've got a 5994 * match. 5995 */ 5996 if (((page_index->page_code & SMPH_SPF) == 0) 5997 && ((page_header->page_code & SMPH_SPF) == 0)) { 5998 page_len = page_header->page_length; 5999 break; 6000 } 6001 6002 /* 6003 * If both pages have subpages, then the subpage numbers 6004 * have to match. 6005 */ 6006 if ((page_index->page_code & SMPH_SPF) 6007 && (page_header->page_code & SMPH_SPF)) { 6008 struct scsi_mode_page_header_sp *sph; 6009 6010 sph = (struct scsi_mode_page_header_sp *)page_header; 6011 if (page_index->subpage == sph->subpage) { 6012 page_len = scsi_2btoul(sph->page_length); 6013 break; 6014 } 6015 } 6016 } 6017 6018 /* 6019 * If we couldn't find the page, or if we don't have a mode select 6020 * handler for it, send back an error to the user. 6021 */ 6022 if ((i >= CTL_NUM_MODE_PAGES) 6023 || (page_index->select_handler == NULL)) { 6024 ctl_set_invalid_field(ctsio, 6025 /*sks_valid*/ 1, 6026 /*command*/ 0, 6027 /*field*/ *len_used, 6028 /*bit_valid*/ 0, 6029 /*bit*/ 0); 6030 free(ctsio->kern_data_ptr, M_CTL); 6031 ctl_done((union ctl_io *)ctsio); 6032 return (CTL_RETVAL_COMPLETE); 6033 } 6034 6035 if (page_index->page_code & SMPH_SPF) { 6036 page_len_offset = 2; 6037 page_len_size = 2; 6038 } else { 6039 page_len_size = 1; 6040 page_len_offset = 1; 6041 } 6042 6043 /* 6044 * If the length the initiator gives us isn't the one we specify in 6045 * the mode page header, or if they didn't specify enough data in 6046 * the CDB to avoid truncating this page, kick out the request. 6047 */ 6048 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6049 ctl_set_invalid_field(ctsio, 6050 /*sks_valid*/ 1, 6051 /*command*/ 0, 6052 /*field*/ *len_used + page_len_offset, 6053 /*bit_valid*/ 0, 6054 /*bit*/ 0); 6055 free(ctsio->kern_data_ptr, M_CTL); 6056 ctl_done((union ctl_io *)ctsio); 6057 return (CTL_RETVAL_COMPLETE); 6058 } 6059 if (*len_left < page_index->page_len) { 6060 free(ctsio->kern_data_ptr, M_CTL); 6061 ctl_set_param_len_error(ctsio); 6062 ctl_done((union ctl_io *)ctsio); 6063 return (CTL_RETVAL_COMPLETE); 6064 } 6065 6066 /* 6067 * Run through the mode page, checking to make sure that the bits 6068 * the user changed are actually legal for him to change. 6069 */ 6070 for (i = 0; i < page_index->page_len; i++) { 6071 uint8_t *user_byte, *change_mask, *current_byte; 6072 int bad_bit; 6073 int j; 6074 6075 user_byte = (uint8_t *)page_header + i; 6076 change_mask = page_index->page_data + 6077 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6078 current_byte = page_index->page_data + 6079 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6080 6081 /* 6082 * Check to see whether the user set any bits in this byte 6083 * that he is not allowed to set. 6084 */ 6085 if ((*user_byte & ~(*change_mask)) == 6086 (*current_byte & ~(*change_mask))) 6087 continue; 6088 6089 /* 6090 * Go through bit by bit to determine which one is illegal. 6091 */ 6092 bad_bit = 0; 6093 for (j = 7; j >= 0; j--) { 6094 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6095 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6096 bad_bit = i; 6097 break; 6098 } 6099 } 6100 ctl_set_invalid_field(ctsio, 6101 /*sks_valid*/ 1, 6102 /*command*/ 0, 6103 /*field*/ *len_used + i, 6104 /*bit_valid*/ 1, 6105 /*bit*/ bad_bit); 6106 free(ctsio->kern_data_ptr, M_CTL); 6107 ctl_done((union ctl_io *)ctsio); 6108 return (CTL_RETVAL_COMPLETE); 6109 } 6110 6111 /* 6112 * Decrement these before we call the page handler, since we may 6113 * end up getting called back one way or another before the handler 6114 * returns to this context. 6115 */ 6116 *len_left -= page_index->page_len; 6117 *len_used += page_index->page_len; 6118 6119 retval = page_index->select_handler(ctsio, page_index, 6120 (uint8_t *)page_header); 6121 6122 /* 6123 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6124 * wait until this queued command completes to finish processing 6125 * the mode page. If it returns anything other than 6126 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6127 * already set the sense information, freed the data pointer, and 6128 * completed the io for us. 6129 */ 6130 if (retval != CTL_RETVAL_COMPLETE) 6131 goto bailout_no_done; 6132 6133 /* 6134 * If the initiator sent us more than one page, parse the next one. 6135 */ 6136 if (*len_left > 0) 6137 goto do_next_page; 6138 6139 ctl_set_success(ctsio); 6140 free(ctsio->kern_data_ptr, M_CTL); 6141 ctl_done((union ctl_io *)ctsio); 6142 6143bailout_no_done: 6144 6145 return (CTL_RETVAL_COMPLETE); 6146 6147} 6148 6149int 6150ctl_mode_select(struct ctl_scsiio *ctsio) 6151{ 6152 struct ctl_lun *lun; 6153 union ctl_modepage_info *modepage_info; 6154 int bd_len, i, header_size, param_len, pf, rtd, sp; 6155 uint32_t initidx; 6156 6157 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6158 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6159 switch (ctsio->cdb[0]) { 6160 case MODE_SELECT_6: { 6161 struct scsi_mode_select_6 *cdb; 6162 6163 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6164 6165 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6166 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6167 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6168 param_len = cdb->length; 6169 header_size = sizeof(struct scsi_mode_header_6); 6170 break; 6171 } 6172 case MODE_SELECT_10: { 6173 struct scsi_mode_select_10 *cdb; 6174 6175 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6176 6177 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6178 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6179 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6180 param_len = scsi_2btoul(cdb->length); 6181 header_size = sizeof(struct scsi_mode_header_10); 6182 break; 6183 } 6184 default: 6185 ctl_set_invalid_opcode(ctsio); 6186 ctl_done((union ctl_io *)ctsio); 6187 return (CTL_RETVAL_COMPLETE); 6188 } 6189 6190 if (rtd) { 6191 if (param_len != 0) { 6192 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6193 /*command*/ 1, /*field*/ 0, 6194 /*bit_valid*/ 0, /*bit*/ 0); 6195 ctl_done((union ctl_io *)ctsio); 6196 return (CTL_RETVAL_COMPLETE); 6197 } 6198 6199 /* Revert to defaults. */ 6200 ctl_init_page_index(lun); 6201 mtx_lock(&lun->lun_lock); 6202 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6203 mtx_unlock(&lun->lun_lock); 6204 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6205 ctl_isc_announce_mode(lun, -1, 6206 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6207 lun->mode_pages.index[i].subpage); 6208 } 6209 ctl_set_success(ctsio); 6210 ctl_done((union ctl_io *)ctsio); 6211 return (CTL_RETVAL_COMPLETE); 6212 } 6213 6214 /* 6215 * From SPC-3: 6216 * "A parameter list length of zero indicates that the Data-Out Buffer 6217 * shall be empty. This condition shall not be considered as an error." 6218 */ 6219 if (param_len == 0) { 6220 ctl_set_success(ctsio); 6221 ctl_done((union ctl_io *)ctsio); 6222 return (CTL_RETVAL_COMPLETE); 6223 } 6224 6225 /* 6226 * Since we'll hit this the first time through, prior to 6227 * allocation, we don't need to free a data buffer here. 6228 */ 6229 if (param_len < header_size) { 6230 ctl_set_param_len_error(ctsio); 6231 ctl_done((union ctl_io *)ctsio); 6232 return (CTL_RETVAL_COMPLETE); 6233 } 6234 6235 /* 6236 * Allocate the data buffer and grab the user's data. In theory, 6237 * we shouldn't have to sanity check the parameter list length here 6238 * because the maximum size is 64K. We should be able to malloc 6239 * that much without too many problems. 6240 */ 6241 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6242 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6243 ctsio->kern_data_len = param_len; 6244 ctsio->kern_total_len = param_len; 6245 ctsio->kern_data_resid = 0; 6246 ctsio->kern_rel_offset = 0; 6247 ctsio->kern_sg_entries = 0; 6248 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6249 ctsio->be_move_done = ctl_config_move_done; 6250 ctl_datamove((union ctl_io *)ctsio); 6251 6252 return (CTL_RETVAL_COMPLETE); 6253 } 6254 6255 switch (ctsio->cdb[0]) { 6256 case MODE_SELECT_6: { 6257 struct scsi_mode_header_6 *mh6; 6258 6259 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6260 bd_len = mh6->blk_desc_len; 6261 break; 6262 } 6263 case MODE_SELECT_10: { 6264 struct scsi_mode_header_10 *mh10; 6265 6266 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6267 bd_len = scsi_2btoul(mh10->blk_desc_len); 6268 break; 6269 } 6270 default: 6271 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6272 } 6273 6274 if (param_len < (header_size + bd_len)) { 6275 free(ctsio->kern_data_ptr, M_CTL); 6276 ctl_set_param_len_error(ctsio); 6277 ctl_done((union ctl_io *)ctsio); 6278 return (CTL_RETVAL_COMPLETE); 6279 } 6280 6281 /* 6282 * Set the IO_CONT flag, so that if this I/O gets passed to 6283 * ctl_config_write_done(), it'll get passed back to 6284 * ctl_do_mode_select() for further processing, or completion if 6285 * we're all done. 6286 */ 6287 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6288 ctsio->io_cont = ctl_do_mode_select; 6289 6290 modepage_info = (union ctl_modepage_info *) 6291 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6292 memset(modepage_info, 0, sizeof(*modepage_info)); 6293 modepage_info->header.len_left = param_len - header_size - bd_len; 6294 modepage_info->header.len_used = header_size + bd_len; 6295 6296 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6297} 6298 6299int 6300ctl_mode_sense(struct ctl_scsiio *ctsio) 6301{ 6302 struct ctl_lun *lun; 6303 int pc, page_code, dbd, llba, subpage; 6304 int alloc_len, page_len, header_len, total_len; 6305 struct scsi_mode_block_descr *block_desc; 6306 struct ctl_page_index *page_index; 6307 6308 dbd = 0; 6309 llba = 0; 6310 block_desc = NULL; 6311 6312 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6313 6314 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6315 switch (ctsio->cdb[0]) { 6316 case MODE_SENSE_6: { 6317 struct scsi_mode_sense_6 *cdb; 6318 6319 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6320 6321 header_len = sizeof(struct scsi_mode_hdr_6); 6322 if (cdb->byte2 & SMS_DBD) 6323 dbd = 1; 6324 else 6325 header_len += sizeof(struct scsi_mode_block_descr); 6326 6327 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6328 page_code = cdb->page & SMS_PAGE_CODE; 6329 subpage = cdb->subpage; 6330 alloc_len = cdb->length; 6331 break; 6332 } 6333 case MODE_SENSE_10: { 6334 struct scsi_mode_sense_10 *cdb; 6335 6336 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6337 6338 header_len = sizeof(struct scsi_mode_hdr_10); 6339 6340 if (cdb->byte2 & SMS_DBD) 6341 dbd = 1; 6342 else 6343 header_len += sizeof(struct scsi_mode_block_descr); 6344 if (cdb->byte2 & SMS10_LLBAA) 6345 llba = 1; 6346 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6347 page_code = cdb->page & SMS_PAGE_CODE; 6348 subpage = cdb->subpage; 6349 alloc_len = scsi_2btoul(cdb->length); 6350 break; 6351 } 6352 default: 6353 ctl_set_invalid_opcode(ctsio); 6354 ctl_done((union ctl_io *)ctsio); 6355 return (CTL_RETVAL_COMPLETE); 6356 break; /* NOTREACHED */ 6357 } 6358 6359 /* 6360 * We have to make a first pass through to calculate the size of 6361 * the pages that match the user's query. Then we allocate enough 6362 * memory to hold it, and actually copy the data into the buffer. 6363 */ 6364 switch (page_code) { 6365 case SMS_ALL_PAGES_PAGE: { 6366 u_int i; 6367 6368 page_len = 0; 6369 6370 /* 6371 * At the moment, values other than 0 and 0xff here are 6372 * reserved according to SPC-3. 6373 */ 6374 if ((subpage != SMS_SUBPAGE_PAGE_0) 6375 && (subpage != SMS_SUBPAGE_ALL)) { 6376 ctl_set_invalid_field(ctsio, 6377 /*sks_valid*/ 1, 6378 /*command*/ 1, 6379 /*field*/ 3, 6380 /*bit_valid*/ 0, 6381 /*bit*/ 0); 6382 ctl_done((union ctl_io *)ctsio); 6383 return (CTL_RETVAL_COMPLETE); 6384 } 6385 6386 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6387 page_index = &lun->mode_pages.index[i]; 6388 6389 /* Make sure the page is supported for this dev type */ 6390 if (lun->be_lun->lun_type == T_DIRECT && 6391 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6392 continue; 6393 if (lun->be_lun->lun_type == T_PROCESSOR && 6394 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6395 continue; 6396 if (lun->be_lun->lun_type == T_CDROM && 6397 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6398 continue; 6399 6400 /* 6401 * We don't use this subpage if the user didn't 6402 * request all subpages. 6403 */ 6404 if ((page_index->subpage != 0) 6405 && (subpage == SMS_SUBPAGE_PAGE_0)) 6406 continue; 6407 6408#if 0 6409 printf("found page %#x len %d\n", 6410 page_index->page_code & SMPH_PC_MASK, 6411 page_index->page_len); 6412#endif 6413 page_len += page_index->page_len; 6414 } 6415 break; 6416 } 6417 default: { 6418 u_int i; 6419 6420 page_len = 0; 6421 6422 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6423 page_index = &lun->mode_pages.index[i]; 6424 6425 /* Make sure the page is supported for this dev type */ 6426 if (lun->be_lun->lun_type == T_DIRECT && 6427 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6428 continue; 6429 if (lun->be_lun->lun_type == T_PROCESSOR && 6430 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6431 continue; 6432 if (lun->be_lun->lun_type == T_CDROM && 6433 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6434 continue; 6435 6436 /* Look for the right page code */ 6437 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6438 continue; 6439 6440 /* Look for the right subpage or the subpage wildcard*/ 6441 if ((page_index->subpage != subpage) 6442 && (subpage != SMS_SUBPAGE_ALL)) 6443 continue; 6444 6445#if 0 6446 printf("found page %#x len %d\n", 6447 page_index->page_code & SMPH_PC_MASK, 6448 page_index->page_len); 6449#endif 6450 6451 page_len += page_index->page_len; 6452 } 6453 6454 if (page_len == 0) { 6455 ctl_set_invalid_field(ctsio, 6456 /*sks_valid*/ 1, 6457 /*command*/ 1, 6458 /*field*/ 2, 6459 /*bit_valid*/ 1, 6460 /*bit*/ 5); 6461 ctl_done((union ctl_io *)ctsio); 6462 return (CTL_RETVAL_COMPLETE); 6463 } 6464 break; 6465 } 6466 } 6467 6468 total_len = header_len + page_len; 6469#if 0 6470 printf("header_len = %d, page_len = %d, total_len = %d\n", 6471 header_len, page_len, total_len); 6472#endif 6473 6474 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6475 ctsio->kern_sg_entries = 0; 6476 ctsio->kern_data_resid = 0; 6477 ctsio->kern_rel_offset = 0; 6478 if (total_len < alloc_len) { 6479 ctsio->residual = alloc_len - total_len; 6480 ctsio->kern_data_len = total_len; 6481 ctsio->kern_total_len = total_len; 6482 } else { 6483 ctsio->residual = 0; 6484 ctsio->kern_data_len = alloc_len; 6485 ctsio->kern_total_len = alloc_len; 6486 } 6487 6488 switch (ctsio->cdb[0]) { 6489 case MODE_SENSE_6: { 6490 struct scsi_mode_hdr_6 *header; 6491 6492 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6493 6494 header->datalen = MIN(total_len - 1, 254); 6495 if (lun->be_lun->lun_type == T_DIRECT) { 6496 header->dev_specific = 0x10; /* DPOFUA */ 6497 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6498 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6499 header->dev_specific |= 0x80; /* WP */ 6500 } 6501 if (dbd) 6502 header->block_descr_len = 0; 6503 else 6504 header->block_descr_len = 6505 sizeof(struct scsi_mode_block_descr); 6506 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6507 break; 6508 } 6509 case MODE_SENSE_10: { 6510 struct scsi_mode_hdr_10 *header; 6511 int datalen; 6512 6513 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6514 6515 datalen = MIN(total_len - 2, 65533); 6516 scsi_ulto2b(datalen, header->datalen); 6517 if (lun->be_lun->lun_type == T_DIRECT) { 6518 header->dev_specific = 0x10; /* DPOFUA */ 6519 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6520 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6521 header->dev_specific |= 0x80; /* WP */ 6522 } 6523 if (dbd) 6524 scsi_ulto2b(0, header->block_descr_len); 6525 else 6526 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6527 header->block_descr_len); 6528 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6529 break; 6530 } 6531 default: 6532 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6533 } 6534 6535 /* 6536 * If we've got a disk, use its blocksize in the block 6537 * descriptor. Otherwise, just set it to 0. 6538 */ 6539 if (dbd == 0) { 6540 if (lun->be_lun->lun_type == T_DIRECT) 6541 scsi_ulto3b(lun->be_lun->blocksize, 6542 block_desc->block_len); 6543 else 6544 scsi_ulto3b(0, block_desc->block_len); 6545 } 6546 6547 switch (page_code) { 6548 case SMS_ALL_PAGES_PAGE: { 6549 int i, data_used; 6550 6551 data_used = header_len; 6552 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6553 struct ctl_page_index *page_index; 6554 6555 page_index = &lun->mode_pages.index[i]; 6556 if (lun->be_lun->lun_type == T_DIRECT && 6557 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6558 continue; 6559 if (lun->be_lun->lun_type == T_PROCESSOR && 6560 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6561 continue; 6562 if (lun->be_lun->lun_type == T_CDROM && 6563 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6564 continue; 6565 6566 /* 6567 * We don't use this subpage if the user didn't 6568 * request all subpages. We already checked (above) 6569 * to make sure the user only specified a subpage 6570 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6571 */ 6572 if ((page_index->subpage != 0) 6573 && (subpage == SMS_SUBPAGE_PAGE_0)) 6574 continue; 6575 6576 /* 6577 * Call the handler, if it exists, to update the 6578 * page to the latest values. 6579 */ 6580 if (page_index->sense_handler != NULL) 6581 page_index->sense_handler(ctsio, page_index,pc); 6582 6583 memcpy(ctsio->kern_data_ptr + data_used, 6584 page_index->page_data + 6585 (page_index->page_len * pc), 6586 page_index->page_len); 6587 data_used += page_index->page_len; 6588 } 6589 break; 6590 } 6591 default: { 6592 int i, data_used; 6593 6594 data_used = header_len; 6595 6596 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6597 struct ctl_page_index *page_index; 6598 6599 page_index = &lun->mode_pages.index[i]; 6600 6601 /* Look for the right page code */ 6602 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6603 continue; 6604 6605 /* Look for the right subpage or the subpage wildcard*/ 6606 if ((page_index->subpage != subpage) 6607 && (subpage != SMS_SUBPAGE_ALL)) 6608 continue; 6609 6610 /* Make sure the page is supported for this dev type */ 6611 if (lun->be_lun->lun_type == T_DIRECT && 6612 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6613 continue; 6614 if (lun->be_lun->lun_type == T_PROCESSOR && 6615 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6616 continue; 6617 if (lun->be_lun->lun_type == T_CDROM && 6618 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6619 continue; 6620 6621 /* 6622 * Call the handler, if it exists, to update the 6623 * page to the latest values. 6624 */ 6625 if (page_index->sense_handler != NULL) 6626 page_index->sense_handler(ctsio, page_index,pc); 6627 6628 memcpy(ctsio->kern_data_ptr + data_used, 6629 page_index->page_data + 6630 (page_index->page_len * pc), 6631 page_index->page_len); 6632 data_used += page_index->page_len; 6633 } 6634 break; 6635 } 6636 } 6637 6638 ctl_set_success(ctsio); 6639 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6640 ctsio->be_move_done = ctl_config_move_done; 6641 ctl_datamove((union ctl_io *)ctsio); 6642 return (CTL_RETVAL_COMPLETE); 6643} 6644 6645int 6646ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6647 struct ctl_page_index *page_index, 6648 int pc) 6649{ 6650 struct ctl_lun *lun; 6651 struct scsi_log_param_header *phdr; 6652 uint8_t *data; 6653 uint64_t val; 6654 6655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6656 data = page_index->page_data; 6657 6658 if (lun->backend->lun_attr != NULL && 6659 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6660 != UINT64_MAX) { 6661 phdr = (struct scsi_log_param_header *)data; 6662 scsi_ulto2b(0x0001, phdr->param_code); 6663 phdr->param_control = SLP_LBIN | SLP_LP; 6664 phdr->param_len = 8; 6665 data = (uint8_t *)(phdr + 1); 6666 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6667 data[4] = 0x02; /* per-pool */ 6668 data += phdr->param_len; 6669 } 6670 6671 if (lun->backend->lun_attr != NULL && 6672 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6673 != UINT64_MAX) { 6674 phdr = (struct scsi_log_param_header *)data; 6675 scsi_ulto2b(0x0002, phdr->param_code); 6676 phdr->param_control = SLP_LBIN | SLP_LP; 6677 phdr->param_len = 8; 6678 data = (uint8_t *)(phdr + 1); 6679 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6680 data[4] = 0x01; /* per-LUN */ 6681 data += phdr->param_len; 6682 } 6683 6684 if (lun->backend->lun_attr != NULL && 6685 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6686 != UINT64_MAX) { 6687 phdr = (struct scsi_log_param_header *)data; 6688 scsi_ulto2b(0x00f1, phdr->param_code); 6689 phdr->param_control = SLP_LBIN | SLP_LP; 6690 phdr->param_len = 8; 6691 data = (uint8_t *)(phdr + 1); 6692 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6693 data[4] = 0x02; /* per-pool */ 6694 data += phdr->param_len; 6695 } 6696 6697 if (lun->backend->lun_attr != NULL && 6698 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6699 != UINT64_MAX) { 6700 phdr = (struct scsi_log_param_header *)data; 6701 scsi_ulto2b(0x00f2, phdr->param_code); 6702 phdr->param_control = SLP_LBIN | SLP_LP; 6703 phdr->param_len = 8; 6704 data = (uint8_t *)(phdr + 1); 6705 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6706 data[4] = 0x02; /* per-pool */ 6707 data += phdr->param_len; 6708 } 6709 6710 page_index->page_len = data - page_index->page_data; 6711 return (0); 6712} 6713 6714int 6715ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6716 struct ctl_page_index *page_index, 6717 int pc) 6718{ 6719 struct ctl_lun *lun; 6720 struct stat_page *data; 6721 uint64_t rn, wn, rb, wb; 6722 struct bintime rt, wt; 6723 int i; 6724 6725 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6726 data = (struct stat_page *)page_index->page_data; 6727 6728 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6729 data->sap.hdr.param_control = SLP_LBIN; 6730 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6731 sizeof(struct scsi_log_param_header); 6732 rn = wn = rb = wb = 0; 6733 bintime_clear(&rt); 6734 bintime_clear(&wt); 6735 for (i = 0; i < CTL_MAX_PORTS; i++) { 6736 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6737 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6738 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6739 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6740 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6741 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6742 } 6743 scsi_u64to8b(rn, data->sap.read_num); 6744 scsi_u64to8b(wn, data->sap.write_num); 6745 if (lun->stats.blocksize > 0) { 6746 scsi_u64to8b(wb / lun->stats.blocksize, 6747 data->sap.recvieved_lba); 6748 scsi_u64to8b(rb / lun->stats.blocksize, 6749 data->sap.transmitted_lba); 6750 } 6751 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6752 data->sap.read_int); 6753 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6754 data->sap.write_int); 6755 scsi_u64to8b(0, data->sap.weighted_num); 6756 scsi_u64to8b(0, data->sap.weighted_int); 6757 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6758 data->it.hdr.param_control = SLP_LBIN; 6759 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6760 sizeof(struct scsi_log_param_header); 6761#ifdef CTL_TIME_IO 6762 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6763#endif 6764 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6765 data->it.hdr.param_control = SLP_LBIN; 6766 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6767 sizeof(struct scsi_log_param_header); 6768 scsi_ulto4b(3, data->ti.exponent); 6769 scsi_ulto4b(1, data->ti.integer); 6770 return (0); 6771} 6772 6773int 6774ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6775 struct ctl_page_index *page_index, 6776 int pc) 6777{ 6778 struct ctl_lun *lun; 6779 struct scsi_log_informational_exceptions *data; 6780 6781 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6782 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6783 6784 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6785 data->hdr.param_control = SLP_LBIN; 6786 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6787 sizeof(struct scsi_log_param_header); 6788 data->ie_asc = lun->ie_asc; 6789 data->ie_ascq = lun->ie_ascq; 6790 data->temperature = 0xff; 6791 return (0); 6792} 6793 6794int 6795ctl_log_sense(struct ctl_scsiio *ctsio) 6796{ 6797 struct ctl_lun *lun; 6798 int i, pc, page_code, subpage; 6799 int alloc_len, total_len; 6800 struct ctl_page_index *page_index; 6801 struct scsi_log_sense *cdb; 6802 struct scsi_log_header *header; 6803 6804 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6805 6806 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6807 cdb = (struct scsi_log_sense *)ctsio->cdb; 6808 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6809 page_code = cdb->page & SLS_PAGE_CODE; 6810 subpage = cdb->subpage; 6811 alloc_len = scsi_2btoul(cdb->length); 6812 6813 page_index = NULL; 6814 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6815 page_index = &lun->log_pages.index[i]; 6816 6817 /* Look for the right page code */ 6818 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6819 continue; 6820 6821 /* Look for the right subpage or the subpage wildcard*/ 6822 if (page_index->subpage != subpage) 6823 continue; 6824 6825 break; 6826 } 6827 if (i >= CTL_NUM_LOG_PAGES) { 6828 ctl_set_invalid_field(ctsio, 6829 /*sks_valid*/ 1, 6830 /*command*/ 1, 6831 /*field*/ 2, 6832 /*bit_valid*/ 0, 6833 /*bit*/ 0); 6834 ctl_done((union ctl_io *)ctsio); 6835 return (CTL_RETVAL_COMPLETE); 6836 } 6837 6838 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6839 6840 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6841 ctsio->kern_sg_entries = 0; 6842 ctsio->kern_data_resid = 0; 6843 ctsio->kern_rel_offset = 0; 6844 if (total_len < alloc_len) { 6845 ctsio->residual = alloc_len - total_len; 6846 ctsio->kern_data_len = total_len; 6847 ctsio->kern_total_len = total_len; 6848 } else { 6849 ctsio->residual = 0; 6850 ctsio->kern_data_len = alloc_len; 6851 ctsio->kern_total_len = alloc_len; 6852 } 6853 6854 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6855 header->page = page_index->page_code; 6856 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6857 header->page |= SL_DS; 6858 if (page_index->subpage) { 6859 header->page |= SL_SPF; 6860 header->subpage = page_index->subpage; 6861 } 6862 scsi_ulto2b(page_index->page_len, header->datalen); 6863 6864 /* 6865 * Call the handler, if it exists, to update the 6866 * page to the latest values. 6867 */ 6868 if (page_index->sense_handler != NULL) 6869 page_index->sense_handler(ctsio, page_index, pc); 6870 6871 memcpy(header + 1, page_index->page_data, page_index->page_len); 6872 6873 ctl_set_success(ctsio); 6874 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6875 ctsio->be_move_done = ctl_config_move_done; 6876 ctl_datamove((union ctl_io *)ctsio); 6877 return (CTL_RETVAL_COMPLETE); 6878} 6879 6880int 6881ctl_read_capacity(struct ctl_scsiio *ctsio) 6882{ 6883 struct scsi_read_capacity *cdb; 6884 struct scsi_read_capacity_data *data; 6885 struct ctl_lun *lun; 6886 uint32_t lba; 6887 6888 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6889 6890 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6891 6892 lba = scsi_4btoul(cdb->addr); 6893 if (((cdb->pmi & SRC_PMI) == 0) 6894 && (lba != 0)) { 6895 ctl_set_invalid_field(/*ctsio*/ ctsio, 6896 /*sks_valid*/ 1, 6897 /*command*/ 1, 6898 /*field*/ 2, 6899 /*bit_valid*/ 0, 6900 /*bit*/ 0); 6901 ctl_done((union ctl_io *)ctsio); 6902 return (CTL_RETVAL_COMPLETE); 6903 } 6904 6905 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6906 6907 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6908 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6909 ctsio->residual = 0; 6910 ctsio->kern_data_len = sizeof(*data); 6911 ctsio->kern_total_len = sizeof(*data); 6912 ctsio->kern_data_resid = 0; 6913 ctsio->kern_rel_offset = 0; 6914 ctsio->kern_sg_entries = 0; 6915 6916 /* 6917 * If the maximum LBA is greater than 0xfffffffe, the user must 6918 * issue a SERVICE ACTION IN (16) command, with the read capacity 6919 * serivce action set. 6920 */ 6921 if (lun->be_lun->maxlba > 0xfffffffe) 6922 scsi_ulto4b(0xffffffff, data->addr); 6923 else 6924 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6925 6926 /* 6927 * XXX KDM this may not be 512 bytes... 6928 */ 6929 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6930 6931 ctl_set_success(ctsio); 6932 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6933 ctsio->be_move_done = ctl_config_move_done; 6934 ctl_datamove((union ctl_io *)ctsio); 6935 return (CTL_RETVAL_COMPLETE); 6936} 6937 6938int 6939ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6940{ 6941 struct scsi_read_capacity_16 *cdb; 6942 struct scsi_read_capacity_data_long *data; 6943 struct ctl_lun *lun; 6944 uint64_t lba; 6945 uint32_t alloc_len; 6946 6947 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6948 6949 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6950 6951 alloc_len = scsi_4btoul(cdb->alloc_len); 6952 lba = scsi_8btou64(cdb->addr); 6953 6954 if ((cdb->reladr & SRC16_PMI) 6955 && (lba != 0)) { 6956 ctl_set_invalid_field(/*ctsio*/ ctsio, 6957 /*sks_valid*/ 1, 6958 /*command*/ 1, 6959 /*field*/ 2, 6960 /*bit_valid*/ 0, 6961 /*bit*/ 0); 6962 ctl_done((union ctl_io *)ctsio); 6963 return (CTL_RETVAL_COMPLETE); 6964 } 6965 6966 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6967 6968 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6969 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6970 6971 if (sizeof(*data) < alloc_len) { 6972 ctsio->residual = alloc_len - sizeof(*data); 6973 ctsio->kern_data_len = sizeof(*data); 6974 ctsio->kern_total_len = sizeof(*data); 6975 } else { 6976 ctsio->residual = 0; 6977 ctsio->kern_data_len = alloc_len; 6978 ctsio->kern_total_len = alloc_len; 6979 } 6980 ctsio->kern_data_resid = 0; 6981 ctsio->kern_rel_offset = 0; 6982 ctsio->kern_sg_entries = 0; 6983 6984 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6985 /* XXX KDM this may not be 512 bytes... */ 6986 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6987 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6988 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6989 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6990 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6991 6992 ctl_set_success(ctsio); 6993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6994 ctsio->be_move_done = ctl_config_move_done; 6995 ctl_datamove((union ctl_io *)ctsio); 6996 return (CTL_RETVAL_COMPLETE); 6997} 6998 6999int 7000ctl_get_lba_status(struct ctl_scsiio *ctsio) 7001{ 7002 struct scsi_get_lba_status *cdb; 7003 struct scsi_get_lba_status_data *data; 7004 struct ctl_lun *lun; 7005 struct ctl_lba_len_flags *lbalen; 7006 uint64_t lba; 7007 uint32_t alloc_len, total_len; 7008 int retval; 7009 7010 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7011 7012 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7013 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7014 lba = scsi_8btou64(cdb->addr); 7015 alloc_len = scsi_4btoul(cdb->alloc_len); 7016 7017 if (lba > lun->be_lun->maxlba) { 7018 ctl_set_lba_out_of_range(ctsio, lba); 7019 ctl_done((union ctl_io *)ctsio); 7020 return (CTL_RETVAL_COMPLETE); 7021 } 7022 7023 total_len = sizeof(*data) + sizeof(data->descr[0]); 7024 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7025 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7026 7027 if (total_len < alloc_len) { 7028 ctsio->residual = alloc_len - total_len; 7029 ctsio->kern_data_len = total_len; 7030 ctsio->kern_total_len = total_len; 7031 } else { 7032 ctsio->residual = 0; 7033 ctsio->kern_data_len = alloc_len; 7034 ctsio->kern_total_len = alloc_len; 7035 } 7036 ctsio->kern_data_resid = 0; 7037 ctsio->kern_rel_offset = 0; 7038 ctsio->kern_sg_entries = 0; 7039 7040 /* Fill dummy data in case backend can't tell anything. */ 7041 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7042 scsi_u64to8b(lba, data->descr[0].addr); 7043 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7044 data->descr[0].length); 7045 data->descr[0].status = 0; /* Mapped or unknown. */ 7046 7047 ctl_set_success(ctsio); 7048 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7049 ctsio->be_move_done = ctl_config_move_done; 7050 7051 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7052 lbalen->lba = lba; 7053 lbalen->len = total_len; 7054 lbalen->flags = 0; 7055 retval = lun->backend->config_read((union ctl_io *)ctsio); 7056 return (CTL_RETVAL_COMPLETE); 7057} 7058 7059int 7060ctl_read_defect(struct ctl_scsiio *ctsio) 7061{ 7062 struct scsi_read_defect_data_10 *ccb10; 7063 struct scsi_read_defect_data_12 *ccb12; 7064 struct scsi_read_defect_data_hdr_10 *data10; 7065 struct scsi_read_defect_data_hdr_12 *data12; 7066 uint32_t alloc_len, data_len; 7067 uint8_t format; 7068 7069 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7070 7071 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7072 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7073 format = ccb10->format; 7074 alloc_len = scsi_2btoul(ccb10->alloc_length); 7075 data_len = sizeof(*data10); 7076 } else { 7077 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7078 format = ccb12->format; 7079 alloc_len = scsi_4btoul(ccb12->alloc_length); 7080 data_len = sizeof(*data12); 7081 } 7082 if (alloc_len == 0) { 7083 ctl_set_success(ctsio); 7084 ctl_done((union ctl_io *)ctsio); 7085 return (CTL_RETVAL_COMPLETE); 7086 } 7087 7088 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7089 if (data_len < alloc_len) { 7090 ctsio->residual = alloc_len - data_len; 7091 ctsio->kern_data_len = data_len; 7092 ctsio->kern_total_len = data_len; 7093 } else { 7094 ctsio->residual = 0; 7095 ctsio->kern_data_len = alloc_len; 7096 ctsio->kern_total_len = alloc_len; 7097 } 7098 ctsio->kern_data_resid = 0; 7099 ctsio->kern_rel_offset = 0; 7100 ctsio->kern_sg_entries = 0; 7101 7102 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7103 data10 = (struct scsi_read_defect_data_hdr_10 *) 7104 ctsio->kern_data_ptr; 7105 data10->format = format; 7106 scsi_ulto2b(0, data10->length); 7107 } else { 7108 data12 = (struct scsi_read_defect_data_hdr_12 *) 7109 ctsio->kern_data_ptr; 7110 data12->format = format; 7111 scsi_ulto2b(0, data12->generation); 7112 scsi_ulto4b(0, data12->length); 7113 } 7114 7115 ctl_set_success(ctsio); 7116 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7117 ctsio->be_move_done = ctl_config_move_done; 7118 ctl_datamove((union ctl_io *)ctsio); 7119 return (CTL_RETVAL_COMPLETE); 7120} 7121 7122int 7123ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7124{ 7125 struct scsi_maintenance_in *cdb; 7126 int retval; 7127 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7128 int num_ha_groups, num_target_ports, shared_group; 7129 struct ctl_lun *lun; 7130 struct ctl_softc *softc; 7131 struct ctl_port *port; 7132 struct scsi_target_group_data *rtg_ptr; 7133 struct scsi_target_group_data_extended *rtg_ext_ptr; 7134 struct scsi_target_port_group_descriptor *tpg_desc; 7135 7136 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7137 7138 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7139 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7140 softc = lun->ctl_softc; 7141 7142 retval = CTL_RETVAL_COMPLETE; 7143 7144 switch (cdb->byte2 & STG_PDF_MASK) { 7145 case STG_PDF_LENGTH: 7146 ext = 0; 7147 break; 7148 case STG_PDF_EXTENDED: 7149 ext = 1; 7150 break; 7151 default: 7152 ctl_set_invalid_field(/*ctsio*/ ctsio, 7153 /*sks_valid*/ 1, 7154 /*command*/ 1, 7155 /*field*/ 2, 7156 /*bit_valid*/ 1, 7157 /*bit*/ 5); 7158 ctl_done((union ctl_io *)ctsio); 7159 return(retval); 7160 } 7161 7162 num_target_ports = 0; 7163 shared_group = (softc->is_single != 0); 7164 mtx_lock(&softc->ctl_lock); 7165 STAILQ_FOREACH(port, &softc->port_list, links) { 7166 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7167 continue; 7168 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7169 continue; 7170 num_target_ports++; 7171 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7172 shared_group = 1; 7173 } 7174 mtx_unlock(&softc->ctl_lock); 7175 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7176 7177 if (ext) 7178 total_len = sizeof(struct scsi_target_group_data_extended); 7179 else 7180 total_len = sizeof(struct scsi_target_group_data); 7181 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7182 (shared_group + num_ha_groups) + 7183 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7184 7185 alloc_len = scsi_4btoul(cdb->length); 7186 7187 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7188 7189 ctsio->kern_sg_entries = 0; 7190 7191 if (total_len < alloc_len) { 7192 ctsio->residual = alloc_len - total_len; 7193 ctsio->kern_data_len = total_len; 7194 ctsio->kern_total_len = total_len; 7195 } else { 7196 ctsio->residual = 0; 7197 ctsio->kern_data_len = alloc_len; 7198 ctsio->kern_total_len = alloc_len; 7199 } 7200 ctsio->kern_data_resid = 0; 7201 ctsio->kern_rel_offset = 0; 7202 7203 if (ext) { 7204 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7205 ctsio->kern_data_ptr; 7206 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7207 rtg_ext_ptr->format_type = 0x10; 7208 rtg_ext_ptr->implicit_transition_time = 0; 7209 tpg_desc = &rtg_ext_ptr->groups[0]; 7210 } else { 7211 rtg_ptr = (struct scsi_target_group_data *) 7212 ctsio->kern_data_ptr; 7213 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7214 tpg_desc = &rtg_ptr->groups[0]; 7215 } 7216 7217 mtx_lock(&softc->ctl_lock); 7218 pg = softc->port_min / softc->port_cnt; 7219 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7220 /* Some shelf is known to be primary. */ 7221 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7222 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7223 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7224 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7225 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7226 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7227 else 7228 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7229 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7230 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7231 } else { 7232 ts = os; 7233 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7234 } 7235 } else { 7236 /* No known primary shelf. */ 7237 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7238 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7239 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7240 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7241 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7242 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7243 } else { 7244 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7245 } 7246 } 7247 if (shared_group) { 7248 tpg_desc->pref_state = ts; 7249 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7250 TPG_U_SUP | TPG_T_SUP; 7251 scsi_ulto2b(1, tpg_desc->target_port_group); 7252 tpg_desc->status = TPG_IMPLICIT; 7253 pc = 0; 7254 STAILQ_FOREACH(port, &softc->port_list, links) { 7255 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7256 continue; 7257 if (!softc->is_single && 7258 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7259 continue; 7260 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7261 continue; 7262 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7263 relative_target_port_identifier); 7264 pc++; 7265 } 7266 tpg_desc->target_port_count = pc; 7267 tpg_desc = (struct scsi_target_port_group_descriptor *) 7268 &tpg_desc->descriptors[pc]; 7269 } 7270 for (g = 0; g < num_ha_groups; g++) { 7271 tpg_desc->pref_state = (g == pg) ? ts : os; 7272 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7273 TPG_U_SUP | TPG_T_SUP; 7274 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7275 tpg_desc->status = TPG_IMPLICIT; 7276 pc = 0; 7277 STAILQ_FOREACH(port, &softc->port_list, links) { 7278 if (port->targ_port < g * softc->port_cnt || 7279 port->targ_port >= (g + 1) * softc->port_cnt) 7280 continue; 7281 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7282 continue; 7283 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7284 continue; 7285 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7286 continue; 7287 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7288 relative_target_port_identifier); 7289 pc++; 7290 } 7291 tpg_desc->target_port_count = pc; 7292 tpg_desc = (struct scsi_target_port_group_descriptor *) 7293 &tpg_desc->descriptors[pc]; 7294 } 7295 mtx_unlock(&softc->ctl_lock); 7296 7297 ctl_set_success(ctsio); 7298 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7299 ctsio->be_move_done = ctl_config_move_done; 7300 ctl_datamove((union ctl_io *)ctsio); 7301 return(retval); 7302} 7303 7304int 7305ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7306{ 7307 struct ctl_lun *lun; 7308 struct scsi_report_supported_opcodes *cdb; 7309 const struct ctl_cmd_entry *entry, *sentry; 7310 struct scsi_report_supported_opcodes_all *all; 7311 struct scsi_report_supported_opcodes_descr *descr; 7312 struct scsi_report_supported_opcodes_one *one; 7313 int retval; 7314 int alloc_len, total_len; 7315 int opcode, service_action, i, j, num; 7316 7317 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7318 7319 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7320 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7321 7322 retval = CTL_RETVAL_COMPLETE; 7323 7324 opcode = cdb->requested_opcode; 7325 service_action = scsi_2btoul(cdb->requested_service_action); 7326 switch (cdb->options & RSO_OPTIONS_MASK) { 7327 case RSO_OPTIONS_ALL: 7328 num = 0; 7329 for (i = 0; i < 256; i++) { 7330 entry = &ctl_cmd_table[i]; 7331 if (entry->flags & CTL_CMD_FLAG_SA5) { 7332 for (j = 0; j < 32; j++) { 7333 sentry = &((const struct ctl_cmd_entry *) 7334 entry->execute)[j]; 7335 if (ctl_cmd_applicable( 7336 lun->be_lun->lun_type, sentry)) 7337 num++; 7338 } 7339 } else { 7340 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7341 entry)) 7342 num++; 7343 } 7344 } 7345 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7346 num * sizeof(struct scsi_report_supported_opcodes_descr); 7347 break; 7348 case RSO_OPTIONS_OC: 7349 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7350 ctl_set_invalid_field(/*ctsio*/ ctsio, 7351 /*sks_valid*/ 1, 7352 /*command*/ 1, 7353 /*field*/ 2, 7354 /*bit_valid*/ 1, 7355 /*bit*/ 2); 7356 ctl_done((union ctl_io *)ctsio); 7357 return (CTL_RETVAL_COMPLETE); 7358 } 7359 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7360 break; 7361 case RSO_OPTIONS_OC_SA: 7362 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7363 service_action >= 32) { 7364 ctl_set_invalid_field(/*ctsio*/ ctsio, 7365 /*sks_valid*/ 1, 7366 /*command*/ 1, 7367 /*field*/ 2, 7368 /*bit_valid*/ 1, 7369 /*bit*/ 2); 7370 ctl_done((union ctl_io *)ctsio); 7371 return (CTL_RETVAL_COMPLETE); 7372 } 7373 /* FALLTHROUGH */ 7374 case RSO_OPTIONS_OC_ASA: 7375 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7376 break; 7377 default: 7378 ctl_set_invalid_field(/*ctsio*/ ctsio, 7379 /*sks_valid*/ 1, 7380 /*command*/ 1, 7381 /*field*/ 2, 7382 /*bit_valid*/ 1, 7383 /*bit*/ 2); 7384 ctl_done((union ctl_io *)ctsio); 7385 return (CTL_RETVAL_COMPLETE); 7386 } 7387 7388 alloc_len = scsi_4btoul(cdb->length); 7389 7390 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7391 7392 ctsio->kern_sg_entries = 0; 7393 7394 if (total_len < alloc_len) { 7395 ctsio->residual = alloc_len - total_len; 7396 ctsio->kern_data_len = total_len; 7397 ctsio->kern_total_len = total_len; 7398 } else { 7399 ctsio->residual = 0; 7400 ctsio->kern_data_len = alloc_len; 7401 ctsio->kern_total_len = alloc_len; 7402 } 7403 ctsio->kern_data_resid = 0; 7404 ctsio->kern_rel_offset = 0; 7405 7406 switch (cdb->options & RSO_OPTIONS_MASK) { 7407 case RSO_OPTIONS_ALL: 7408 all = (struct scsi_report_supported_opcodes_all *) 7409 ctsio->kern_data_ptr; 7410 num = 0; 7411 for (i = 0; i < 256; i++) { 7412 entry = &ctl_cmd_table[i]; 7413 if (entry->flags & CTL_CMD_FLAG_SA5) { 7414 for (j = 0; j < 32; j++) { 7415 sentry = &((const struct ctl_cmd_entry *) 7416 entry->execute)[j]; 7417 if (!ctl_cmd_applicable( 7418 lun->be_lun->lun_type, sentry)) 7419 continue; 7420 descr = &all->descr[num++]; 7421 descr->opcode = i; 7422 scsi_ulto2b(j, descr->service_action); 7423 descr->flags = RSO_SERVACTV; 7424 scsi_ulto2b(sentry->length, 7425 descr->cdb_length); 7426 } 7427 } else { 7428 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7429 entry)) 7430 continue; 7431 descr = &all->descr[num++]; 7432 descr->opcode = i; 7433 scsi_ulto2b(0, descr->service_action); 7434 descr->flags = 0; 7435 scsi_ulto2b(entry->length, descr->cdb_length); 7436 } 7437 } 7438 scsi_ulto4b( 7439 num * sizeof(struct scsi_report_supported_opcodes_descr), 7440 all->length); 7441 break; 7442 case RSO_OPTIONS_OC: 7443 one = (struct scsi_report_supported_opcodes_one *) 7444 ctsio->kern_data_ptr; 7445 entry = &ctl_cmd_table[opcode]; 7446 goto fill_one; 7447 case RSO_OPTIONS_OC_SA: 7448 one = (struct scsi_report_supported_opcodes_one *) 7449 ctsio->kern_data_ptr; 7450 entry = &ctl_cmd_table[opcode]; 7451 entry = &((const struct ctl_cmd_entry *) 7452 entry->execute)[service_action]; 7453fill_one: 7454 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7455 one->support = 3; 7456 scsi_ulto2b(entry->length, one->cdb_length); 7457 one->cdb_usage[0] = opcode; 7458 memcpy(&one->cdb_usage[1], entry->usage, 7459 entry->length - 1); 7460 } else 7461 one->support = 1; 7462 break; 7463 case RSO_OPTIONS_OC_ASA: 7464 one = (struct scsi_report_supported_opcodes_one *) 7465 ctsio->kern_data_ptr; 7466 entry = &ctl_cmd_table[opcode]; 7467 if (entry->flags & CTL_CMD_FLAG_SA5) { 7468 entry = &((const struct ctl_cmd_entry *) 7469 entry->execute)[service_action]; 7470 } else if (service_action != 0) { 7471 one->support = 1; 7472 break; 7473 } 7474 goto fill_one; 7475 } 7476 7477 ctl_set_success(ctsio); 7478 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7479 ctsio->be_move_done = ctl_config_move_done; 7480 ctl_datamove((union ctl_io *)ctsio); 7481 return(retval); 7482} 7483 7484int 7485ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7486{ 7487 struct scsi_report_supported_tmf *cdb; 7488 struct scsi_report_supported_tmf_ext_data *data; 7489 int retval; 7490 int alloc_len, total_len; 7491 7492 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7493 7494 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7495 7496 retval = CTL_RETVAL_COMPLETE; 7497 7498 if (cdb->options & RST_REPD) 7499 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7500 else 7501 total_len = sizeof(struct scsi_report_supported_tmf_data); 7502 alloc_len = scsi_4btoul(cdb->length); 7503 7504 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7505 7506 ctsio->kern_sg_entries = 0; 7507 7508 if (total_len < alloc_len) { 7509 ctsio->residual = alloc_len - total_len; 7510 ctsio->kern_data_len = total_len; 7511 ctsio->kern_total_len = total_len; 7512 } else { 7513 ctsio->residual = 0; 7514 ctsio->kern_data_len = alloc_len; 7515 ctsio->kern_total_len = alloc_len; 7516 } 7517 ctsio->kern_data_resid = 0; 7518 ctsio->kern_rel_offset = 0; 7519 7520 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7521 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7522 RST_TRS; 7523 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7524 data->length = total_len - 4; 7525 7526 ctl_set_success(ctsio); 7527 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7528 ctsio->be_move_done = ctl_config_move_done; 7529 ctl_datamove((union ctl_io *)ctsio); 7530 return (retval); 7531} 7532 7533int 7534ctl_report_timestamp(struct ctl_scsiio *ctsio) 7535{ 7536 struct scsi_report_timestamp *cdb; 7537 struct scsi_report_timestamp_data *data; 7538 struct timeval tv; 7539 int64_t timestamp; 7540 int retval; 7541 int alloc_len, total_len; 7542 7543 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7544 7545 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7546 7547 retval = CTL_RETVAL_COMPLETE; 7548 7549 total_len = sizeof(struct scsi_report_timestamp_data); 7550 alloc_len = scsi_4btoul(cdb->length); 7551 7552 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7553 7554 ctsio->kern_sg_entries = 0; 7555 7556 if (total_len < alloc_len) { 7557 ctsio->residual = alloc_len - total_len; 7558 ctsio->kern_data_len = total_len; 7559 ctsio->kern_total_len = total_len; 7560 } else { 7561 ctsio->residual = 0; 7562 ctsio->kern_data_len = alloc_len; 7563 ctsio->kern_total_len = alloc_len; 7564 } 7565 ctsio->kern_data_resid = 0; 7566 ctsio->kern_rel_offset = 0; 7567 7568 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7569 scsi_ulto2b(sizeof(*data) - 2, data->length); 7570 data->origin = RTS_ORIG_OUTSIDE; 7571 getmicrotime(&tv); 7572 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7573 scsi_ulto4b(timestamp >> 16, data->timestamp); 7574 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7575 7576 ctl_set_success(ctsio); 7577 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7578 ctsio->be_move_done = ctl_config_move_done; 7579 ctl_datamove((union ctl_io *)ctsio); 7580 return (retval); 7581} 7582 7583int 7584ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7585{ 7586 struct scsi_per_res_in *cdb; 7587 int alloc_len, total_len = 0; 7588 /* struct scsi_per_res_in_rsrv in_data; */ 7589 struct ctl_lun *lun; 7590 struct ctl_softc *softc; 7591 uint64_t key; 7592 7593 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7594 7595 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7596 7597 alloc_len = scsi_2btoul(cdb->length); 7598 7599 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7600 softc = lun->ctl_softc; 7601 7602retry: 7603 mtx_lock(&lun->lun_lock); 7604 switch (cdb->action) { 7605 case SPRI_RK: /* read keys */ 7606 total_len = sizeof(struct scsi_per_res_in_keys) + 7607 lun->pr_key_count * 7608 sizeof(struct scsi_per_res_key); 7609 break; 7610 case SPRI_RR: /* read reservation */ 7611 if (lun->flags & CTL_LUN_PR_RESERVED) 7612 total_len = sizeof(struct scsi_per_res_in_rsrv); 7613 else 7614 total_len = sizeof(struct scsi_per_res_in_header); 7615 break; 7616 case SPRI_RC: /* report capabilities */ 7617 total_len = sizeof(struct scsi_per_res_cap); 7618 break; 7619 case SPRI_RS: /* read full status */ 7620 total_len = sizeof(struct scsi_per_res_in_header) + 7621 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7622 lun->pr_key_count; 7623 break; 7624 default: 7625 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7626 } 7627 mtx_unlock(&lun->lun_lock); 7628 7629 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7630 7631 if (total_len < alloc_len) { 7632 ctsio->residual = alloc_len - total_len; 7633 ctsio->kern_data_len = total_len; 7634 ctsio->kern_total_len = total_len; 7635 } else { 7636 ctsio->residual = 0; 7637 ctsio->kern_data_len = alloc_len; 7638 ctsio->kern_total_len = alloc_len; 7639 } 7640 7641 ctsio->kern_data_resid = 0; 7642 ctsio->kern_rel_offset = 0; 7643 ctsio->kern_sg_entries = 0; 7644 7645 mtx_lock(&lun->lun_lock); 7646 switch (cdb->action) { 7647 case SPRI_RK: { // read keys 7648 struct scsi_per_res_in_keys *res_keys; 7649 int i, key_count; 7650 7651 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7652 7653 /* 7654 * We had to drop the lock to allocate our buffer, which 7655 * leaves time for someone to come in with another 7656 * persistent reservation. (That is unlikely, though, 7657 * since this should be the only persistent reservation 7658 * command active right now.) 7659 */ 7660 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7661 (lun->pr_key_count * 7662 sizeof(struct scsi_per_res_key)))){ 7663 mtx_unlock(&lun->lun_lock); 7664 free(ctsio->kern_data_ptr, M_CTL); 7665 printf("%s: reservation length changed, retrying\n", 7666 __func__); 7667 goto retry; 7668 } 7669 7670 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7671 7672 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7673 lun->pr_key_count, res_keys->header.length); 7674 7675 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7676 if ((key = ctl_get_prkey(lun, i)) == 0) 7677 continue; 7678 7679 /* 7680 * We used lun->pr_key_count to calculate the 7681 * size to allocate. If it turns out the number of 7682 * initiators with the registered flag set is 7683 * larger than that (i.e. they haven't been kept in 7684 * sync), we've got a problem. 7685 */ 7686 if (key_count >= lun->pr_key_count) { 7687 key_count++; 7688 continue; 7689 } 7690 scsi_u64to8b(key, res_keys->keys[key_count].key); 7691 key_count++; 7692 } 7693 break; 7694 } 7695 case SPRI_RR: { // read reservation 7696 struct scsi_per_res_in_rsrv *res; 7697 int tmp_len, header_only; 7698 7699 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7700 7701 scsi_ulto4b(lun->pr_generation, res->header.generation); 7702 7703 if (lun->flags & CTL_LUN_PR_RESERVED) 7704 { 7705 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7706 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7707 res->header.length); 7708 header_only = 0; 7709 } else { 7710 tmp_len = sizeof(struct scsi_per_res_in_header); 7711 scsi_ulto4b(0, res->header.length); 7712 header_only = 1; 7713 } 7714 7715 /* 7716 * We had to drop the lock to allocate our buffer, which 7717 * leaves time for someone to come in with another 7718 * persistent reservation. (That is unlikely, though, 7719 * since this should be the only persistent reservation 7720 * command active right now.) 7721 */ 7722 if (tmp_len != total_len) { 7723 mtx_unlock(&lun->lun_lock); 7724 free(ctsio->kern_data_ptr, M_CTL); 7725 printf("%s: reservation status changed, retrying\n", 7726 __func__); 7727 goto retry; 7728 } 7729 7730 /* 7731 * No reservation held, so we're done. 7732 */ 7733 if (header_only != 0) 7734 break; 7735 7736 /* 7737 * If the registration is an All Registrants type, the key 7738 * is 0, since it doesn't really matter. 7739 */ 7740 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7741 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7742 res->data.reservation); 7743 } 7744 res->data.scopetype = lun->pr_res_type; 7745 break; 7746 } 7747 case SPRI_RC: //report capabilities 7748 { 7749 struct scsi_per_res_cap *res_cap; 7750 uint16_t type_mask; 7751 7752 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7753 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7754 res_cap->flags1 = SPRI_CRH; 7755 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7756 type_mask = SPRI_TM_WR_EX_AR | 7757 SPRI_TM_EX_AC_RO | 7758 SPRI_TM_WR_EX_RO | 7759 SPRI_TM_EX_AC | 7760 SPRI_TM_WR_EX | 7761 SPRI_TM_EX_AC_AR; 7762 scsi_ulto2b(type_mask, res_cap->type_mask); 7763 break; 7764 } 7765 case SPRI_RS: { // read full status 7766 struct scsi_per_res_in_full *res_status; 7767 struct scsi_per_res_in_full_desc *res_desc; 7768 struct ctl_port *port; 7769 int i, len; 7770 7771 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7772 7773 /* 7774 * We had to drop the lock to allocate our buffer, which 7775 * leaves time for someone to come in with another 7776 * persistent reservation. (That is unlikely, though, 7777 * since this should be the only persistent reservation 7778 * command active right now.) 7779 */ 7780 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7781 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7782 lun->pr_key_count)){ 7783 mtx_unlock(&lun->lun_lock); 7784 free(ctsio->kern_data_ptr, M_CTL); 7785 printf("%s: reservation length changed, retrying\n", 7786 __func__); 7787 goto retry; 7788 } 7789 7790 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7791 7792 res_desc = &res_status->desc[0]; 7793 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7794 if ((key = ctl_get_prkey(lun, i)) == 0) 7795 continue; 7796 7797 scsi_u64to8b(key, res_desc->res_key.key); 7798 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7799 (lun->pr_res_idx == i || 7800 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7801 res_desc->flags = SPRI_FULL_R_HOLDER; 7802 res_desc->scopetype = lun->pr_res_type; 7803 } 7804 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7805 res_desc->rel_trgt_port_id); 7806 len = 0; 7807 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7808 if (port != NULL) 7809 len = ctl_create_iid(port, 7810 i % CTL_MAX_INIT_PER_PORT, 7811 res_desc->transport_id); 7812 scsi_ulto4b(len, res_desc->additional_length); 7813 res_desc = (struct scsi_per_res_in_full_desc *) 7814 &res_desc->transport_id[len]; 7815 } 7816 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7817 res_status->header.length); 7818 break; 7819 } 7820 default: 7821 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7822 } 7823 mtx_unlock(&lun->lun_lock); 7824 7825 ctl_set_success(ctsio); 7826 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7827 ctsio->be_move_done = ctl_config_move_done; 7828 ctl_datamove((union ctl_io *)ctsio); 7829 return (CTL_RETVAL_COMPLETE); 7830} 7831 7832/* 7833 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7834 * it should return. 7835 */ 7836static int 7837ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7838 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7839 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7840 struct scsi_per_res_out_parms* param) 7841{ 7842 union ctl_ha_msg persis_io; 7843 int i; 7844 7845 mtx_lock(&lun->lun_lock); 7846 if (sa_res_key == 0) { 7847 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7848 /* validate scope and type */ 7849 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7850 SPR_LU_SCOPE) { 7851 mtx_unlock(&lun->lun_lock); 7852 ctl_set_invalid_field(/*ctsio*/ ctsio, 7853 /*sks_valid*/ 1, 7854 /*command*/ 1, 7855 /*field*/ 2, 7856 /*bit_valid*/ 1, 7857 /*bit*/ 4); 7858 ctl_done((union ctl_io *)ctsio); 7859 return (1); 7860 } 7861 7862 if (type>8 || type==2 || type==4 || type==0) { 7863 mtx_unlock(&lun->lun_lock); 7864 ctl_set_invalid_field(/*ctsio*/ ctsio, 7865 /*sks_valid*/ 1, 7866 /*command*/ 1, 7867 /*field*/ 2, 7868 /*bit_valid*/ 1, 7869 /*bit*/ 0); 7870 ctl_done((union ctl_io *)ctsio); 7871 return (1); 7872 } 7873 7874 /* 7875 * Unregister everybody else and build UA for 7876 * them 7877 */ 7878 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7879 if (i == residx || ctl_get_prkey(lun, i) == 0) 7880 continue; 7881 7882 ctl_clr_prkey(lun, i); 7883 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7884 } 7885 lun->pr_key_count = 1; 7886 lun->pr_res_type = type; 7887 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7888 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7889 lun->pr_res_idx = residx; 7890 lun->pr_generation++; 7891 mtx_unlock(&lun->lun_lock); 7892 7893 /* send msg to other side */ 7894 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7895 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7896 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7897 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7898 persis_io.pr.pr_info.res_type = type; 7899 memcpy(persis_io.pr.pr_info.sa_res_key, 7900 param->serv_act_res_key, 7901 sizeof(param->serv_act_res_key)); 7902 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7903 sizeof(persis_io.pr), M_WAITOK); 7904 } else { 7905 /* not all registrants */ 7906 mtx_unlock(&lun->lun_lock); 7907 free(ctsio->kern_data_ptr, M_CTL); 7908 ctl_set_invalid_field(ctsio, 7909 /*sks_valid*/ 1, 7910 /*command*/ 0, 7911 /*field*/ 8, 7912 /*bit_valid*/ 0, 7913 /*bit*/ 0); 7914 ctl_done((union ctl_io *)ctsio); 7915 return (1); 7916 } 7917 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7918 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7919 int found = 0; 7920 7921 if (res_key == sa_res_key) { 7922 /* special case */ 7923 /* 7924 * The spec implies this is not good but doesn't 7925 * say what to do. There are two choices either 7926 * generate a res conflict or check condition 7927 * with illegal field in parameter data. Since 7928 * that is what is done when the sa_res_key is 7929 * zero I'll take that approach since this has 7930 * to do with the sa_res_key. 7931 */ 7932 mtx_unlock(&lun->lun_lock); 7933 free(ctsio->kern_data_ptr, M_CTL); 7934 ctl_set_invalid_field(ctsio, 7935 /*sks_valid*/ 1, 7936 /*command*/ 0, 7937 /*field*/ 8, 7938 /*bit_valid*/ 0, 7939 /*bit*/ 0); 7940 ctl_done((union ctl_io *)ctsio); 7941 return (1); 7942 } 7943 7944 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7945 if (ctl_get_prkey(lun, i) != sa_res_key) 7946 continue; 7947 7948 found = 1; 7949 ctl_clr_prkey(lun, i); 7950 lun->pr_key_count--; 7951 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7952 } 7953 if (!found) { 7954 mtx_unlock(&lun->lun_lock); 7955 free(ctsio->kern_data_ptr, M_CTL); 7956 ctl_set_reservation_conflict(ctsio); 7957 ctl_done((union ctl_io *)ctsio); 7958 return (CTL_RETVAL_COMPLETE); 7959 } 7960 lun->pr_generation++; 7961 mtx_unlock(&lun->lun_lock); 7962 7963 /* send msg to other side */ 7964 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7965 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7966 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7967 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7968 persis_io.pr.pr_info.res_type = type; 7969 memcpy(persis_io.pr.pr_info.sa_res_key, 7970 param->serv_act_res_key, 7971 sizeof(param->serv_act_res_key)); 7972 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7973 sizeof(persis_io.pr), M_WAITOK); 7974 } else { 7975 /* Reserved but not all registrants */ 7976 /* sa_res_key is res holder */ 7977 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7978 /* validate scope and type */ 7979 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7980 SPR_LU_SCOPE) { 7981 mtx_unlock(&lun->lun_lock); 7982 ctl_set_invalid_field(/*ctsio*/ ctsio, 7983 /*sks_valid*/ 1, 7984 /*command*/ 1, 7985 /*field*/ 2, 7986 /*bit_valid*/ 1, 7987 /*bit*/ 4); 7988 ctl_done((union ctl_io *)ctsio); 7989 return (1); 7990 } 7991 7992 if (type>8 || type==2 || type==4 || type==0) { 7993 mtx_unlock(&lun->lun_lock); 7994 ctl_set_invalid_field(/*ctsio*/ ctsio, 7995 /*sks_valid*/ 1, 7996 /*command*/ 1, 7997 /*field*/ 2, 7998 /*bit_valid*/ 1, 7999 /*bit*/ 0); 8000 ctl_done((union ctl_io *)ctsio); 8001 return (1); 8002 } 8003 8004 /* 8005 * Do the following: 8006 * if sa_res_key != res_key remove all 8007 * registrants w/sa_res_key and generate UA 8008 * for these registrants(Registrations 8009 * Preempted) if it wasn't an exclusive 8010 * reservation generate UA(Reservations 8011 * Preempted) for all other registered nexuses 8012 * if the type has changed. Establish the new 8013 * reservation and holder. If res_key and 8014 * sa_res_key are the same do the above 8015 * except don't unregister the res holder. 8016 */ 8017 8018 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8019 if (i == residx || ctl_get_prkey(lun, i) == 0) 8020 continue; 8021 8022 if (sa_res_key == ctl_get_prkey(lun, i)) { 8023 ctl_clr_prkey(lun, i); 8024 lun->pr_key_count--; 8025 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8026 } else if (type != lun->pr_res_type && 8027 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8028 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8029 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8030 } 8031 } 8032 lun->pr_res_type = type; 8033 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8034 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8035 lun->pr_res_idx = residx; 8036 else 8037 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8038 lun->pr_generation++; 8039 mtx_unlock(&lun->lun_lock); 8040 8041 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8042 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8043 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8044 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8045 persis_io.pr.pr_info.res_type = type; 8046 memcpy(persis_io.pr.pr_info.sa_res_key, 8047 param->serv_act_res_key, 8048 sizeof(param->serv_act_res_key)); 8049 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8050 sizeof(persis_io.pr), M_WAITOK); 8051 } else { 8052 /* 8053 * sa_res_key is not the res holder just 8054 * remove registrants 8055 */ 8056 int found=0; 8057 8058 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8059 if (sa_res_key != ctl_get_prkey(lun, i)) 8060 continue; 8061 8062 found = 1; 8063 ctl_clr_prkey(lun, i); 8064 lun->pr_key_count--; 8065 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8066 } 8067 8068 if (!found) { 8069 mtx_unlock(&lun->lun_lock); 8070 free(ctsio->kern_data_ptr, M_CTL); 8071 ctl_set_reservation_conflict(ctsio); 8072 ctl_done((union ctl_io *)ctsio); 8073 return (1); 8074 } 8075 lun->pr_generation++; 8076 mtx_unlock(&lun->lun_lock); 8077 8078 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8079 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8080 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8081 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8082 persis_io.pr.pr_info.res_type = type; 8083 memcpy(persis_io.pr.pr_info.sa_res_key, 8084 param->serv_act_res_key, 8085 sizeof(param->serv_act_res_key)); 8086 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8087 sizeof(persis_io.pr), M_WAITOK); 8088 } 8089 } 8090 return (0); 8091} 8092 8093static void 8094ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8095{ 8096 uint64_t sa_res_key; 8097 int i; 8098 8099 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8100 8101 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8102 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8103 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8104 if (sa_res_key == 0) { 8105 /* 8106 * Unregister everybody else and build UA for 8107 * them 8108 */ 8109 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8110 if (i == msg->pr.pr_info.residx || 8111 ctl_get_prkey(lun, i) == 0) 8112 continue; 8113 8114 ctl_clr_prkey(lun, i); 8115 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8116 } 8117 8118 lun->pr_key_count = 1; 8119 lun->pr_res_type = msg->pr.pr_info.res_type; 8120 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8121 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8122 lun->pr_res_idx = msg->pr.pr_info.residx; 8123 } else { 8124 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8125 if (sa_res_key == ctl_get_prkey(lun, i)) 8126 continue; 8127 8128 ctl_clr_prkey(lun, i); 8129 lun->pr_key_count--; 8130 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8131 } 8132 } 8133 } else { 8134 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8135 if (i == msg->pr.pr_info.residx || 8136 ctl_get_prkey(lun, i) == 0) 8137 continue; 8138 8139 if (sa_res_key == ctl_get_prkey(lun, i)) { 8140 ctl_clr_prkey(lun, i); 8141 lun->pr_key_count--; 8142 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8143 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8144 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8145 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8146 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8147 } 8148 } 8149 lun->pr_res_type = msg->pr.pr_info.res_type; 8150 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8151 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8152 lun->pr_res_idx = msg->pr.pr_info.residx; 8153 else 8154 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8155 } 8156 lun->pr_generation++; 8157 8158} 8159 8160 8161int 8162ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8163{ 8164 int retval; 8165 u_int32_t param_len; 8166 struct scsi_per_res_out *cdb; 8167 struct ctl_lun *lun; 8168 struct scsi_per_res_out_parms* param; 8169 struct ctl_softc *softc; 8170 uint32_t residx; 8171 uint64_t res_key, sa_res_key, key; 8172 uint8_t type; 8173 union ctl_ha_msg persis_io; 8174 int i; 8175 8176 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8177 8178 retval = CTL_RETVAL_COMPLETE; 8179 8180 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8181 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8182 softc = lun->ctl_softc; 8183 8184 /* 8185 * We only support whole-LUN scope. The scope & type are ignored for 8186 * register, register and ignore existing key and clear. 8187 * We sometimes ignore scope and type on preempts too!! 8188 * Verify reservation type here as well. 8189 */ 8190 type = cdb->scope_type & SPR_TYPE_MASK; 8191 if ((cdb->action == SPRO_RESERVE) 8192 || (cdb->action == SPRO_RELEASE)) { 8193 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8194 ctl_set_invalid_field(/*ctsio*/ ctsio, 8195 /*sks_valid*/ 1, 8196 /*command*/ 1, 8197 /*field*/ 2, 8198 /*bit_valid*/ 1, 8199 /*bit*/ 4); 8200 ctl_done((union ctl_io *)ctsio); 8201 return (CTL_RETVAL_COMPLETE); 8202 } 8203 8204 if (type>8 || type==2 || type==4 || type==0) { 8205 ctl_set_invalid_field(/*ctsio*/ ctsio, 8206 /*sks_valid*/ 1, 8207 /*command*/ 1, 8208 /*field*/ 2, 8209 /*bit_valid*/ 1, 8210 /*bit*/ 0); 8211 ctl_done((union ctl_io *)ctsio); 8212 return (CTL_RETVAL_COMPLETE); 8213 } 8214 } 8215 8216 param_len = scsi_4btoul(cdb->length); 8217 8218 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8219 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8220 ctsio->kern_data_len = param_len; 8221 ctsio->kern_total_len = param_len; 8222 ctsio->kern_data_resid = 0; 8223 ctsio->kern_rel_offset = 0; 8224 ctsio->kern_sg_entries = 0; 8225 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8226 ctsio->be_move_done = ctl_config_move_done; 8227 ctl_datamove((union ctl_io *)ctsio); 8228 8229 return (CTL_RETVAL_COMPLETE); 8230 } 8231 8232 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8233 8234 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8235 res_key = scsi_8btou64(param->res_key.key); 8236 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8237 8238 /* 8239 * Validate the reservation key here except for SPRO_REG_IGNO 8240 * This must be done for all other service actions 8241 */ 8242 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8243 mtx_lock(&lun->lun_lock); 8244 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8245 if (res_key != key) { 8246 /* 8247 * The current key passed in doesn't match 8248 * the one the initiator previously 8249 * registered. 8250 */ 8251 mtx_unlock(&lun->lun_lock); 8252 free(ctsio->kern_data_ptr, M_CTL); 8253 ctl_set_reservation_conflict(ctsio); 8254 ctl_done((union ctl_io *)ctsio); 8255 return (CTL_RETVAL_COMPLETE); 8256 } 8257 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8258 /* 8259 * We are not registered 8260 */ 8261 mtx_unlock(&lun->lun_lock); 8262 free(ctsio->kern_data_ptr, M_CTL); 8263 ctl_set_reservation_conflict(ctsio); 8264 ctl_done((union ctl_io *)ctsio); 8265 return (CTL_RETVAL_COMPLETE); 8266 } else if (res_key != 0) { 8267 /* 8268 * We are not registered and trying to register but 8269 * the register key isn't zero. 8270 */ 8271 mtx_unlock(&lun->lun_lock); 8272 free(ctsio->kern_data_ptr, M_CTL); 8273 ctl_set_reservation_conflict(ctsio); 8274 ctl_done((union ctl_io *)ctsio); 8275 return (CTL_RETVAL_COMPLETE); 8276 } 8277 mtx_unlock(&lun->lun_lock); 8278 } 8279 8280 switch (cdb->action & SPRO_ACTION_MASK) { 8281 case SPRO_REGISTER: 8282 case SPRO_REG_IGNO: { 8283 8284#if 0 8285 printf("Registration received\n"); 8286#endif 8287 8288 /* 8289 * We don't support any of these options, as we report in 8290 * the read capabilities request (see 8291 * ctl_persistent_reserve_in(), above). 8292 */ 8293 if ((param->flags & SPR_SPEC_I_PT) 8294 || (param->flags & SPR_ALL_TG_PT) 8295 || (param->flags & SPR_APTPL)) { 8296 int bit_ptr; 8297 8298 if (param->flags & SPR_APTPL) 8299 bit_ptr = 0; 8300 else if (param->flags & SPR_ALL_TG_PT) 8301 bit_ptr = 2; 8302 else /* SPR_SPEC_I_PT */ 8303 bit_ptr = 3; 8304 8305 free(ctsio->kern_data_ptr, M_CTL); 8306 ctl_set_invalid_field(ctsio, 8307 /*sks_valid*/ 1, 8308 /*command*/ 0, 8309 /*field*/ 20, 8310 /*bit_valid*/ 1, 8311 /*bit*/ bit_ptr); 8312 ctl_done((union ctl_io *)ctsio); 8313 return (CTL_RETVAL_COMPLETE); 8314 } 8315 8316 mtx_lock(&lun->lun_lock); 8317 8318 /* 8319 * The initiator wants to clear the 8320 * key/unregister. 8321 */ 8322 if (sa_res_key == 0) { 8323 if ((res_key == 0 8324 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8325 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8326 && ctl_get_prkey(lun, residx) == 0)) { 8327 mtx_unlock(&lun->lun_lock); 8328 goto done; 8329 } 8330 8331 ctl_clr_prkey(lun, residx); 8332 lun->pr_key_count--; 8333 8334 if (residx == lun->pr_res_idx) { 8335 lun->flags &= ~CTL_LUN_PR_RESERVED; 8336 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8337 8338 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8339 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8340 lun->pr_key_count) { 8341 /* 8342 * If the reservation is a registrants 8343 * only type we need to generate a UA 8344 * for other registered inits. The 8345 * sense code should be RESERVATIONS 8346 * RELEASED 8347 */ 8348 8349 for (i = softc->init_min; i < softc->init_max; i++){ 8350 if (ctl_get_prkey(lun, i) == 0) 8351 continue; 8352 ctl_est_ua(lun, i, 8353 CTL_UA_RES_RELEASE); 8354 } 8355 } 8356 lun->pr_res_type = 0; 8357 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8358 if (lun->pr_key_count==0) { 8359 lun->flags &= ~CTL_LUN_PR_RESERVED; 8360 lun->pr_res_type = 0; 8361 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8362 } 8363 } 8364 lun->pr_generation++; 8365 mtx_unlock(&lun->lun_lock); 8366 8367 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8368 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8369 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8370 persis_io.pr.pr_info.residx = residx; 8371 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8372 sizeof(persis_io.pr), M_WAITOK); 8373 } else /* sa_res_key != 0 */ { 8374 8375 /* 8376 * If we aren't registered currently then increment 8377 * the key count and set the registered flag. 8378 */ 8379 ctl_alloc_prkey(lun, residx); 8380 if (ctl_get_prkey(lun, residx) == 0) 8381 lun->pr_key_count++; 8382 ctl_set_prkey(lun, residx, sa_res_key); 8383 lun->pr_generation++; 8384 mtx_unlock(&lun->lun_lock); 8385 8386 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8387 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8388 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8389 persis_io.pr.pr_info.residx = residx; 8390 memcpy(persis_io.pr.pr_info.sa_res_key, 8391 param->serv_act_res_key, 8392 sizeof(param->serv_act_res_key)); 8393 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8394 sizeof(persis_io.pr), M_WAITOK); 8395 } 8396 8397 break; 8398 } 8399 case SPRO_RESERVE: 8400#if 0 8401 printf("Reserve executed type %d\n", type); 8402#endif 8403 mtx_lock(&lun->lun_lock); 8404 if (lun->flags & CTL_LUN_PR_RESERVED) { 8405 /* 8406 * if this isn't the reservation holder and it's 8407 * not a "all registrants" type or if the type is 8408 * different then we have a conflict 8409 */ 8410 if ((lun->pr_res_idx != residx 8411 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8412 || lun->pr_res_type != type) { 8413 mtx_unlock(&lun->lun_lock); 8414 free(ctsio->kern_data_ptr, M_CTL); 8415 ctl_set_reservation_conflict(ctsio); 8416 ctl_done((union ctl_io *)ctsio); 8417 return (CTL_RETVAL_COMPLETE); 8418 } 8419 mtx_unlock(&lun->lun_lock); 8420 } else /* create a reservation */ { 8421 /* 8422 * If it's not an "all registrants" type record 8423 * reservation holder 8424 */ 8425 if (type != SPR_TYPE_WR_EX_AR 8426 && type != SPR_TYPE_EX_AC_AR) 8427 lun->pr_res_idx = residx; /* Res holder */ 8428 else 8429 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8430 8431 lun->flags |= CTL_LUN_PR_RESERVED; 8432 lun->pr_res_type = type; 8433 8434 mtx_unlock(&lun->lun_lock); 8435 8436 /* send msg to other side */ 8437 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8438 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8439 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8440 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8441 persis_io.pr.pr_info.res_type = type; 8442 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8443 sizeof(persis_io.pr), M_WAITOK); 8444 } 8445 break; 8446 8447 case SPRO_RELEASE: 8448 mtx_lock(&lun->lun_lock); 8449 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8450 /* No reservation exists return good status */ 8451 mtx_unlock(&lun->lun_lock); 8452 goto done; 8453 } 8454 /* 8455 * Is this nexus a reservation holder? 8456 */ 8457 if (lun->pr_res_idx != residx 8458 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8459 /* 8460 * not a res holder return good status but 8461 * do nothing 8462 */ 8463 mtx_unlock(&lun->lun_lock); 8464 goto done; 8465 } 8466 8467 if (lun->pr_res_type != type) { 8468 mtx_unlock(&lun->lun_lock); 8469 free(ctsio->kern_data_ptr, M_CTL); 8470 ctl_set_illegal_pr_release(ctsio); 8471 ctl_done((union ctl_io *)ctsio); 8472 return (CTL_RETVAL_COMPLETE); 8473 } 8474 8475 /* okay to release */ 8476 lun->flags &= ~CTL_LUN_PR_RESERVED; 8477 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8478 lun->pr_res_type = 0; 8479 8480 /* 8481 * If this isn't an exclusive access reservation and NUAR 8482 * is not set, generate UA for all other registrants. 8483 */ 8484 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8485 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8486 for (i = softc->init_min; i < softc->init_max; i++) { 8487 if (i == residx || ctl_get_prkey(lun, i) == 0) 8488 continue; 8489 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8490 } 8491 } 8492 mtx_unlock(&lun->lun_lock); 8493 8494 /* Send msg to other side */ 8495 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8496 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8497 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8498 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8499 sizeof(persis_io.pr), M_WAITOK); 8500 break; 8501 8502 case SPRO_CLEAR: 8503 /* send msg to other side */ 8504 8505 mtx_lock(&lun->lun_lock); 8506 lun->flags &= ~CTL_LUN_PR_RESERVED; 8507 lun->pr_res_type = 0; 8508 lun->pr_key_count = 0; 8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8510 8511 ctl_clr_prkey(lun, residx); 8512 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8513 if (ctl_get_prkey(lun, i) != 0) { 8514 ctl_clr_prkey(lun, i); 8515 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8516 } 8517 lun->pr_generation++; 8518 mtx_unlock(&lun->lun_lock); 8519 8520 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8521 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8522 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8523 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8524 sizeof(persis_io.pr), M_WAITOK); 8525 break; 8526 8527 case SPRO_PREEMPT: 8528 case SPRO_PRE_ABO: { 8529 int nretval; 8530 8531 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8532 residx, ctsio, cdb, param); 8533 if (nretval != 0) 8534 return (CTL_RETVAL_COMPLETE); 8535 break; 8536 } 8537 default: 8538 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8539 } 8540 8541done: 8542 free(ctsio->kern_data_ptr, M_CTL); 8543 ctl_set_success(ctsio); 8544 ctl_done((union ctl_io *)ctsio); 8545 8546 return (retval); 8547} 8548 8549/* 8550 * This routine is for handling a message from the other SC pertaining to 8551 * persistent reserve out. All the error checking will have been done 8552 * so only perorming the action need be done here to keep the two 8553 * in sync. 8554 */ 8555static void 8556ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8557{ 8558 struct ctl_softc *softc = control_softc; 8559 struct ctl_lun *lun; 8560 int i; 8561 uint32_t residx, targ_lun; 8562 8563 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8564 mtx_lock(&softc->ctl_lock); 8565 if (targ_lun >= CTL_MAX_LUNS || 8566 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8567 mtx_unlock(&softc->ctl_lock); 8568 return; 8569 } 8570 mtx_lock(&lun->lun_lock); 8571 mtx_unlock(&softc->ctl_lock); 8572 if (lun->flags & CTL_LUN_DISABLED) { 8573 mtx_unlock(&lun->lun_lock); 8574 return; 8575 } 8576 residx = ctl_get_initindex(&msg->hdr.nexus); 8577 switch(msg->pr.pr_info.action) { 8578 case CTL_PR_REG_KEY: 8579 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8580 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8581 lun->pr_key_count++; 8582 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8583 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8584 lun->pr_generation++; 8585 break; 8586 8587 case CTL_PR_UNREG_KEY: 8588 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8589 lun->pr_key_count--; 8590 8591 /* XXX Need to see if the reservation has been released */ 8592 /* if so do we need to generate UA? */ 8593 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8594 lun->flags &= ~CTL_LUN_PR_RESERVED; 8595 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8596 8597 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8598 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8599 lun->pr_key_count) { 8600 /* 8601 * If the reservation is a registrants 8602 * only type we need to generate a UA 8603 * for other registered inits. The 8604 * sense code should be RESERVATIONS 8605 * RELEASED 8606 */ 8607 8608 for (i = softc->init_min; i < softc->init_max; i++) { 8609 if (ctl_get_prkey(lun, i) == 0) 8610 continue; 8611 8612 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8613 } 8614 } 8615 lun->pr_res_type = 0; 8616 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8617 if (lun->pr_key_count==0) { 8618 lun->flags &= ~CTL_LUN_PR_RESERVED; 8619 lun->pr_res_type = 0; 8620 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8621 } 8622 } 8623 lun->pr_generation++; 8624 break; 8625 8626 case CTL_PR_RESERVE: 8627 lun->flags |= CTL_LUN_PR_RESERVED; 8628 lun->pr_res_type = msg->pr.pr_info.res_type; 8629 lun->pr_res_idx = msg->pr.pr_info.residx; 8630 8631 break; 8632 8633 case CTL_PR_RELEASE: 8634 /* 8635 * If this isn't an exclusive access reservation and NUAR 8636 * is not set, generate UA for all other registrants. 8637 */ 8638 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8639 lun->pr_res_type != SPR_TYPE_WR_EX && 8640 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8641 for (i = softc->init_min; i < softc->init_max; i++) 8642 if (i == residx || ctl_get_prkey(lun, i) == 0) 8643 continue; 8644 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8645 } 8646 8647 lun->flags &= ~CTL_LUN_PR_RESERVED; 8648 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8649 lun->pr_res_type = 0; 8650 break; 8651 8652 case CTL_PR_PREEMPT: 8653 ctl_pro_preempt_other(lun, msg); 8654 break; 8655 case CTL_PR_CLEAR: 8656 lun->flags &= ~CTL_LUN_PR_RESERVED; 8657 lun->pr_res_type = 0; 8658 lun->pr_key_count = 0; 8659 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8660 8661 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8662 if (ctl_get_prkey(lun, i) == 0) 8663 continue; 8664 ctl_clr_prkey(lun, i); 8665 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8666 } 8667 lun->pr_generation++; 8668 break; 8669 } 8670 8671 mtx_unlock(&lun->lun_lock); 8672} 8673 8674int 8675ctl_read_write(struct ctl_scsiio *ctsio) 8676{ 8677 struct ctl_lun *lun; 8678 struct ctl_lba_len_flags *lbalen; 8679 uint64_t lba; 8680 uint32_t num_blocks; 8681 int flags, retval; 8682 int isread; 8683 8684 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8685 8686 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8687 8688 flags = 0; 8689 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8690 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8691 switch (ctsio->cdb[0]) { 8692 case READ_6: 8693 case WRITE_6: { 8694 struct scsi_rw_6 *cdb; 8695 8696 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8697 8698 lba = scsi_3btoul(cdb->addr); 8699 /* only 5 bits are valid in the most significant address byte */ 8700 lba &= 0x1fffff; 8701 num_blocks = cdb->length; 8702 /* 8703 * This is correct according to SBC-2. 8704 */ 8705 if (num_blocks == 0) 8706 num_blocks = 256; 8707 break; 8708 } 8709 case READ_10: 8710 case WRITE_10: { 8711 struct scsi_rw_10 *cdb; 8712 8713 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8714 if (cdb->byte2 & SRW10_FUA) 8715 flags |= CTL_LLF_FUA; 8716 if (cdb->byte2 & SRW10_DPO) 8717 flags |= CTL_LLF_DPO; 8718 lba = scsi_4btoul(cdb->addr); 8719 num_blocks = scsi_2btoul(cdb->length); 8720 break; 8721 } 8722 case WRITE_VERIFY_10: { 8723 struct scsi_write_verify_10 *cdb; 8724 8725 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8726 flags |= CTL_LLF_FUA; 8727 if (cdb->byte2 & SWV_DPO) 8728 flags |= CTL_LLF_DPO; 8729 lba = scsi_4btoul(cdb->addr); 8730 num_blocks = scsi_2btoul(cdb->length); 8731 break; 8732 } 8733 case READ_12: 8734 case WRITE_12: { 8735 struct scsi_rw_12 *cdb; 8736 8737 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8738 if (cdb->byte2 & SRW12_FUA) 8739 flags |= CTL_LLF_FUA; 8740 if (cdb->byte2 & SRW12_DPO) 8741 flags |= CTL_LLF_DPO; 8742 lba = scsi_4btoul(cdb->addr); 8743 num_blocks = scsi_4btoul(cdb->length); 8744 break; 8745 } 8746 case WRITE_VERIFY_12: { 8747 struct scsi_write_verify_12 *cdb; 8748 8749 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8750 flags |= CTL_LLF_FUA; 8751 if (cdb->byte2 & SWV_DPO) 8752 flags |= CTL_LLF_DPO; 8753 lba = scsi_4btoul(cdb->addr); 8754 num_blocks = scsi_4btoul(cdb->length); 8755 break; 8756 } 8757 case READ_16: 8758 case WRITE_16: { 8759 struct scsi_rw_16 *cdb; 8760 8761 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8762 if (cdb->byte2 & SRW12_FUA) 8763 flags |= CTL_LLF_FUA; 8764 if (cdb->byte2 & SRW12_DPO) 8765 flags |= CTL_LLF_DPO; 8766 lba = scsi_8btou64(cdb->addr); 8767 num_blocks = scsi_4btoul(cdb->length); 8768 break; 8769 } 8770 case WRITE_ATOMIC_16: { 8771 struct scsi_write_atomic_16 *cdb; 8772 8773 if (lun->be_lun->atomicblock == 0) { 8774 ctl_set_invalid_opcode(ctsio); 8775 ctl_done((union ctl_io *)ctsio); 8776 return (CTL_RETVAL_COMPLETE); 8777 } 8778 8779 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8780 if (cdb->byte2 & SRW12_FUA) 8781 flags |= CTL_LLF_FUA; 8782 if (cdb->byte2 & SRW12_DPO) 8783 flags |= CTL_LLF_DPO; 8784 lba = scsi_8btou64(cdb->addr); 8785 num_blocks = scsi_2btoul(cdb->length); 8786 if (num_blocks > lun->be_lun->atomicblock) { 8787 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8788 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8789 /*bit*/ 0); 8790 ctl_done((union ctl_io *)ctsio); 8791 return (CTL_RETVAL_COMPLETE); 8792 } 8793 break; 8794 } 8795 case WRITE_VERIFY_16: { 8796 struct scsi_write_verify_16 *cdb; 8797 8798 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8799 flags |= CTL_LLF_FUA; 8800 if (cdb->byte2 & SWV_DPO) 8801 flags |= CTL_LLF_DPO; 8802 lba = scsi_8btou64(cdb->addr); 8803 num_blocks = scsi_4btoul(cdb->length); 8804 break; 8805 } 8806 default: 8807 /* 8808 * We got a command we don't support. This shouldn't 8809 * happen, commands should be filtered out above us. 8810 */ 8811 ctl_set_invalid_opcode(ctsio); 8812 ctl_done((union ctl_io *)ctsio); 8813 8814 return (CTL_RETVAL_COMPLETE); 8815 break; /* NOTREACHED */ 8816 } 8817 8818 /* 8819 * The first check is to make sure we're in bounds, the second 8820 * check is to catch wrap-around problems. If the lba + num blocks 8821 * is less than the lba, then we've wrapped around and the block 8822 * range is invalid anyway. 8823 */ 8824 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8825 || ((lba + num_blocks) < lba)) { 8826 ctl_set_lba_out_of_range(ctsio, 8827 MAX(lba, lun->be_lun->maxlba + 1)); 8828 ctl_done((union ctl_io *)ctsio); 8829 return (CTL_RETVAL_COMPLETE); 8830 } 8831 8832 /* 8833 * According to SBC-3, a transfer length of 0 is not an error. 8834 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8835 * translates to 256 blocks for those commands. 8836 */ 8837 if (num_blocks == 0) { 8838 ctl_set_success(ctsio); 8839 ctl_done((union ctl_io *)ctsio); 8840 return (CTL_RETVAL_COMPLETE); 8841 } 8842 8843 /* Set FUA and/or DPO if caches are disabled. */ 8844 if (isread) { 8845 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8846 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8847 } else { 8848 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8849 flags |= CTL_LLF_FUA; 8850 } 8851 8852 lbalen = (struct ctl_lba_len_flags *) 8853 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8854 lbalen->lba = lba; 8855 lbalen->len = num_blocks; 8856 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8857 8858 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8859 ctsio->kern_rel_offset = 0; 8860 8861 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8862 8863 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8864 return (retval); 8865} 8866 8867static int 8868ctl_cnw_cont(union ctl_io *io) 8869{ 8870 struct ctl_scsiio *ctsio; 8871 struct ctl_lun *lun; 8872 struct ctl_lba_len_flags *lbalen; 8873 int retval; 8874 8875 ctsio = &io->scsiio; 8876 ctsio->io_hdr.status = CTL_STATUS_NONE; 8877 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8879 lbalen = (struct ctl_lba_len_flags *) 8880 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8881 lbalen->flags &= ~CTL_LLF_COMPARE; 8882 lbalen->flags |= CTL_LLF_WRITE; 8883 8884 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8885 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8886 return (retval); 8887} 8888 8889int 8890ctl_cnw(struct ctl_scsiio *ctsio) 8891{ 8892 struct ctl_lun *lun; 8893 struct ctl_lba_len_flags *lbalen; 8894 uint64_t lba; 8895 uint32_t num_blocks; 8896 int flags, retval; 8897 8898 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8899 8900 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8901 8902 flags = 0; 8903 switch (ctsio->cdb[0]) { 8904 case COMPARE_AND_WRITE: { 8905 struct scsi_compare_and_write *cdb; 8906 8907 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8908 if (cdb->byte2 & SRW10_FUA) 8909 flags |= CTL_LLF_FUA; 8910 if (cdb->byte2 & SRW10_DPO) 8911 flags |= CTL_LLF_DPO; 8912 lba = scsi_8btou64(cdb->addr); 8913 num_blocks = cdb->length; 8914 break; 8915 } 8916 default: 8917 /* 8918 * We got a command we don't support. This shouldn't 8919 * happen, commands should be filtered out above us. 8920 */ 8921 ctl_set_invalid_opcode(ctsio); 8922 ctl_done((union ctl_io *)ctsio); 8923 8924 return (CTL_RETVAL_COMPLETE); 8925 break; /* NOTREACHED */ 8926 } 8927 8928 /* 8929 * The first check is to make sure we're in bounds, the second 8930 * check is to catch wrap-around problems. If the lba + num blocks 8931 * is less than the lba, then we've wrapped around and the block 8932 * range is invalid anyway. 8933 */ 8934 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8935 || ((lba + num_blocks) < lba)) { 8936 ctl_set_lba_out_of_range(ctsio, 8937 MAX(lba, lun->be_lun->maxlba + 1)); 8938 ctl_done((union ctl_io *)ctsio); 8939 return (CTL_RETVAL_COMPLETE); 8940 } 8941 8942 /* 8943 * According to SBC-3, a transfer length of 0 is not an error. 8944 */ 8945 if (num_blocks == 0) { 8946 ctl_set_success(ctsio); 8947 ctl_done((union ctl_io *)ctsio); 8948 return (CTL_RETVAL_COMPLETE); 8949 } 8950 8951 /* Set FUA if write cache is disabled. */ 8952 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8953 flags |= CTL_LLF_FUA; 8954 8955 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8956 ctsio->kern_rel_offset = 0; 8957 8958 /* 8959 * Set the IO_CONT flag, so that if this I/O gets passed to 8960 * ctl_data_submit_done(), it'll get passed back to 8961 * ctl_ctl_cnw_cont() for further processing. 8962 */ 8963 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8964 ctsio->io_cont = ctl_cnw_cont; 8965 8966 lbalen = (struct ctl_lba_len_flags *) 8967 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8968 lbalen->lba = lba; 8969 lbalen->len = num_blocks; 8970 lbalen->flags = CTL_LLF_COMPARE | flags; 8971 8972 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8973 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8974 return (retval); 8975} 8976 8977int 8978ctl_verify(struct ctl_scsiio *ctsio) 8979{ 8980 struct ctl_lun *lun; 8981 struct ctl_lba_len_flags *lbalen; 8982 uint64_t lba; 8983 uint32_t num_blocks; 8984 int bytchk, flags; 8985 int retval; 8986 8987 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8988 8989 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8990 8991 bytchk = 0; 8992 flags = CTL_LLF_FUA; 8993 switch (ctsio->cdb[0]) { 8994 case VERIFY_10: { 8995 struct scsi_verify_10 *cdb; 8996 8997 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8998 if (cdb->byte2 & SVFY_BYTCHK) 8999 bytchk = 1; 9000 if (cdb->byte2 & SVFY_DPO) 9001 flags |= CTL_LLF_DPO; 9002 lba = scsi_4btoul(cdb->addr); 9003 num_blocks = scsi_2btoul(cdb->length); 9004 break; 9005 } 9006 case VERIFY_12: { 9007 struct scsi_verify_12 *cdb; 9008 9009 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9010 if (cdb->byte2 & SVFY_BYTCHK) 9011 bytchk = 1; 9012 if (cdb->byte2 & SVFY_DPO) 9013 flags |= CTL_LLF_DPO; 9014 lba = scsi_4btoul(cdb->addr); 9015 num_blocks = scsi_4btoul(cdb->length); 9016 break; 9017 } 9018 case VERIFY_16: { 9019 struct scsi_rw_16 *cdb; 9020 9021 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9022 if (cdb->byte2 & SVFY_BYTCHK) 9023 bytchk = 1; 9024 if (cdb->byte2 & SVFY_DPO) 9025 flags |= CTL_LLF_DPO; 9026 lba = scsi_8btou64(cdb->addr); 9027 num_blocks = scsi_4btoul(cdb->length); 9028 break; 9029 } 9030 default: 9031 /* 9032 * We got a command we don't support. This shouldn't 9033 * happen, commands should be filtered out above us. 9034 */ 9035 ctl_set_invalid_opcode(ctsio); 9036 ctl_done((union ctl_io *)ctsio); 9037 return (CTL_RETVAL_COMPLETE); 9038 } 9039 9040 /* 9041 * The first check is to make sure we're in bounds, the second 9042 * check is to catch wrap-around problems. If the lba + num blocks 9043 * is less than the lba, then we've wrapped around and the block 9044 * range is invalid anyway. 9045 */ 9046 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9047 || ((lba + num_blocks) < lba)) { 9048 ctl_set_lba_out_of_range(ctsio, 9049 MAX(lba, lun->be_lun->maxlba + 1)); 9050 ctl_done((union ctl_io *)ctsio); 9051 return (CTL_RETVAL_COMPLETE); 9052 } 9053 9054 /* 9055 * According to SBC-3, a transfer length of 0 is not an error. 9056 */ 9057 if (num_blocks == 0) { 9058 ctl_set_success(ctsio); 9059 ctl_done((union ctl_io *)ctsio); 9060 return (CTL_RETVAL_COMPLETE); 9061 } 9062 9063 lbalen = (struct ctl_lba_len_flags *) 9064 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9065 lbalen->lba = lba; 9066 lbalen->len = num_blocks; 9067 if (bytchk) { 9068 lbalen->flags = CTL_LLF_COMPARE | flags; 9069 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9070 } else { 9071 lbalen->flags = CTL_LLF_VERIFY | flags; 9072 ctsio->kern_total_len = 0; 9073 } 9074 ctsio->kern_rel_offset = 0; 9075 9076 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9077 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9078 return (retval); 9079} 9080 9081int 9082ctl_report_luns(struct ctl_scsiio *ctsio) 9083{ 9084 struct ctl_softc *softc; 9085 struct scsi_report_luns *cdb; 9086 struct scsi_report_luns_data *lun_data; 9087 struct ctl_lun *lun, *request_lun; 9088 struct ctl_port *port; 9089 int num_filled, num_luns, num_port_luns, retval; 9090 uint32_t alloc_len, lun_datalen; 9091 uint32_t initidx, targ_lun_id, lun_id; 9092 9093 retval = CTL_RETVAL_COMPLETE; 9094 cdb = (struct scsi_report_luns *)ctsio->cdb; 9095 port = ctl_io_port(&ctsio->io_hdr); 9096 softc = port->ctl_softc; 9097 9098 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9099 9100 num_luns = 0; 9101 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS; 9102 mtx_lock(&softc->ctl_lock); 9103 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9104 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9105 num_luns++; 9106 } 9107 mtx_unlock(&softc->ctl_lock); 9108 9109 switch (cdb->select_report) { 9110 case RPL_REPORT_DEFAULT: 9111 case RPL_REPORT_ALL: 9112 case RPL_REPORT_NONSUBSID: 9113 break; 9114 case RPL_REPORT_WELLKNOWN: 9115 case RPL_REPORT_ADMIN: 9116 case RPL_REPORT_CONGLOM: 9117 num_luns = 0; 9118 break; 9119 default: 9120 ctl_set_invalid_field(ctsio, 9121 /*sks_valid*/ 1, 9122 /*command*/ 1, 9123 /*field*/ 2, 9124 /*bit_valid*/ 0, 9125 /*bit*/ 0); 9126 ctl_done((union ctl_io *)ctsio); 9127 return (retval); 9128 break; /* NOTREACHED */ 9129 } 9130 9131 alloc_len = scsi_4btoul(cdb->length); 9132 /* 9133 * The initiator has to allocate at least 16 bytes for this request, 9134 * so he can at least get the header and the first LUN. Otherwise 9135 * we reject the request (per SPC-3 rev 14, section 6.21). 9136 */ 9137 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9138 sizeof(struct scsi_report_luns_lundata))) { 9139 ctl_set_invalid_field(ctsio, 9140 /*sks_valid*/ 1, 9141 /*command*/ 1, 9142 /*field*/ 6, 9143 /*bit_valid*/ 0, 9144 /*bit*/ 0); 9145 ctl_done((union ctl_io *)ctsio); 9146 return (retval); 9147 } 9148 9149 request_lun = (struct ctl_lun *) 9150 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9151 9152 lun_datalen = sizeof(*lun_data) + 9153 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9154 9155 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9156 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9157 ctsio->kern_sg_entries = 0; 9158 9159 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9160 9161 mtx_lock(&softc->ctl_lock); 9162 for (targ_lun_id = 0, num_filled = 0; 9163 targ_lun_id < num_port_luns && num_filled < num_luns; 9164 targ_lun_id++) { 9165 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9166 if (lun_id == UINT32_MAX) 9167 continue; 9168 lun = softc->ctl_luns[lun_id]; 9169 if (lun == NULL) 9170 continue; 9171 9172 be64enc(lun_data->luns[num_filled++].lundata, 9173 ctl_encode_lun(targ_lun_id)); 9174 9175 /* 9176 * According to SPC-3, rev 14 section 6.21: 9177 * 9178 * "The execution of a REPORT LUNS command to any valid and 9179 * installed logical unit shall clear the REPORTED LUNS DATA 9180 * HAS CHANGED unit attention condition for all logical 9181 * units of that target with respect to the requesting 9182 * initiator. A valid and installed logical unit is one 9183 * having a PERIPHERAL QUALIFIER of 000b in the standard 9184 * INQUIRY data (see 6.4.2)." 9185 * 9186 * If request_lun is NULL, the LUN this report luns command 9187 * was issued to is either disabled or doesn't exist. In that 9188 * case, we shouldn't clear any pending lun change unit 9189 * attention. 9190 */ 9191 if (request_lun != NULL) { 9192 mtx_lock(&lun->lun_lock); 9193 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9194 mtx_unlock(&lun->lun_lock); 9195 } 9196 } 9197 mtx_unlock(&softc->ctl_lock); 9198 9199 /* 9200 * It's quite possible that we've returned fewer LUNs than we allocated 9201 * space for. Trim it. 9202 */ 9203 lun_datalen = sizeof(*lun_data) + 9204 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9205 9206 if (lun_datalen < alloc_len) { 9207 ctsio->residual = alloc_len - lun_datalen; 9208 ctsio->kern_data_len = lun_datalen; 9209 ctsio->kern_total_len = lun_datalen; 9210 } else { 9211 ctsio->residual = 0; 9212 ctsio->kern_data_len = alloc_len; 9213 ctsio->kern_total_len = alloc_len; 9214 } 9215 ctsio->kern_data_resid = 0; 9216 ctsio->kern_rel_offset = 0; 9217 ctsio->kern_sg_entries = 0; 9218 9219 /* 9220 * We set this to the actual data length, regardless of how much 9221 * space we actually have to return results. If the user looks at 9222 * this value, he'll know whether or not he allocated enough space 9223 * and reissue the command if necessary. We don't support well 9224 * known logical units, so if the user asks for that, return none. 9225 */ 9226 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9227 9228 /* 9229 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9230 * this request. 9231 */ 9232 ctl_set_success(ctsio); 9233 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9234 ctsio->be_move_done = ctl_config_move_done; 9235 ctl_datamove((union ctl_io *)ctsio); 9236 return (retval); 9237} 9238 9239int 9240ctl_request_sense(struct ctl_scsiio *ctsio) 9241{ 9242 struct scsi_request_sense *cdb; 9243 struct scsi_sense_data *sense_ptr; 9244 struct ctl_softc *softc; 9245 struct ctl_lun *lun; 9246 uint32_t initidx; 9247 int have_error; 9248 u_int sense_len = SSD_FULL_SIZE; 9249 scsi_sense_data_type sense_format; 9250 ctl_ua_type ua_type; 9251 uint8_t asc = 0, ascq = 0; 9252 9253 cdb = (struct scsi_request_sense *)ctsio->cdb; 9254 9255 softc = control_softc; 9256 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9257 9258 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9259 9260 /* 9261 * Determine which sense format the user wants. 9262 */ 9263 if (cdb->byte2 & SRS_DESC) 9264 sense_format = SSD_TYPE_DESC; 9265 else 9266 sense_format = SSD_TYPE_FIXED; 9267 9268 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9269 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9270 ctsio->kern_sg_entries = 0; 9271 9272 /* 9273 * struct scsi_sense_data, which is currently set to 256 bytes, is 9274 * larger than the largest allowed value for the length field in the 9275 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9276 */ 9277 ctsio->residual = 0; 9278 ctsio->kern_data_len = cdb->length; 9279 ctsio->kern_total_len = cdb->length; 9280 9281 ctsio->kern_data_resid = 0; 9282 ctsio->kern_rel_offset = 0; 9283 ctsio->kern_sg_entries = 0; 9284 9285 /* 9286 * If we don't have a LUN, we don't have any pending sense. 9287 */ 9288 if (lun == NULL || 9289 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9290 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9291 /* "Logical unit not supported" */ 9292 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9293 /*current_error*/ 1, 9294 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9295 /*asc*/ 0x25, 9296 /*ascq*/ 0x00, 9297 SSD_ELEM_NONE); 9298 goto send; 9299 } 9300 9301 have_error = 0; 9302 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9303 /* 9304 * Check for pending sense, and then for pending unit attentions. 9305 * Pending sense gets returned first, then pending unit attentions. 9306 */ 9307 mtx_lock(&lun->lun_lock); 9308#ifdef CTL_WITH_CA 9309 if (ctl_is_set(lun->have_ca, initidx)) { 9310 scsi_sense_data_type stored_format; 9311 9312 /* 9313 * Check to see which sense format was used for the stored 9314 * sense data. 9315 */ 9316 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9317 9318 /* 9319 * If the user requested a different sense format than the 9320 * one we stored, then we need to convert it to the other 9321 * format. If we're going from descriptor to fixed format 9322 * sense data, we may lose things in translation, depending 9323 * on what options were used. 9324 * 9325 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9326 * for some reason we'll just copy it out as-is. 9327 */ 9328 if ((stored_format == SSD_TYPE_FIXED) 9329 && (sense_format == SSD_TYPE_DESC)) 9330 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9331 &lun->pending_sense[initidx], 9332 (struct scsi_sense_data_desc *)sense_ptr); 9333 else if ((stored_format == SSD_TYPE_DESC) 9334 && (sense_format == SSD_TYPE_FIXED)) 9335 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9336 &lun->pending_sense[initidx], 9337 (struct scsi_sense_data_fixed *)sense_ptr); 9338 else 9339 memcpy(sense_ptr, &lun->pending_sense[initidx], 9340 MIN(sizeof(*sense_ptr), 9341 sizeof(lun->pending_sense[initidx]))); 9342 9343 ctl_clear_mask(lun->have_ca, initidx); 9344 have_error = 1; 9345 } else 9346#endif 9347 if (have_error == 0) { 9348 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9349 sense_format); 9350 if (ua_type != CTL_UA_NONE) 9351 have_error = 1; 9352 } 9353 if (have_error == 0) { 9354 /* 9355 * Report informational exception if have one and allowed. 9356 */ 9357 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9358 asc = lun->ie_asc; 9359 ascq = lun->ie_ascq; 9360 } 9361 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9362 /*current_error*/ 1, 9363 /*sense_key*/ SSD_KEY_NO_SENSE, 9364 /*asc*/ asc, 9365 /*ascq*/ ascq, 9366 SSD_ELEM_NONE); 9367 } 9368 mtx_unlock(&lun->lun_lock); 9369 9370send: 9371 /* 9372 * We report the SCSI status as OK, since the status of the command 9373 * itself is OK. We're reporting sense as parameter data. 9374 */ 9375 ctl_set_success(ctsio); 9376 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9377 ctsio->be_move_done = ctl_config_move_done; 9378 ctl_datamove((union ctl_io *)ctsio); 9379 return (CTL_RETVAL_COMPLETE); 9380} 9381 9382int 9383ctl_tur(struct ctl_scsiio *ctsio) 9384{ 9385 9386 CTL_DEBUG_PRINT(("ctl_tur\n")); 9387 9388 ctl_set_success(ctsio); 9389 ctl_done((union ctl_io *)ctsio); 9390 9391 return (CTL_RETVAL_COMPLETE); 9392} 9393 9394/* 9395 * SCSI VPD page 0x00, the Supported VPD Pages page. 9396 */ 9397static int 9398ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9399{ 9400 struct scsi_vpd_supported_pages *pages; 9401 int sup_page_size; 9402 struct ctl_lun *lun; 9403 int p; 9404 9405 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9406 9407 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9408 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9409 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9410 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9411 ctsio->kern_sg_entries = 0; 9412 9413 if (sup_page_size < alloc_len) { 9414 ctsio->residual = alloc_len - sup_page_size; 9415 ctsio->kern_data_len = sup_page_size; 9416 ctsio->kern_total_len = sup_page_size; 9417 } else { 9418 ctsio->residual = 0; 9419 ctsio->kern_data_len = alloc_len; 9420 ctsio->kern_total_len = alloc_len; 9421 } 9422 ctsio->kern_data_resid = 0; 9423 ctsio->kern_rel_offset = 0; 9424 ctsio->kern_sg_entries = 0; 9425 9426 /* 9427 * The control device is always connected. The disk device, on the 9428 * other hand, may not be online all the time. Need to change this 9429 * to figure out whether the disk device is actually online or not. 9430 */ 9431 if (lun != NULL) 9432 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9433 lun->be_lun->lun_type; 9434 else 9435 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9436 9437 p = 0; 9438 /* Supported VPD pages */ 9439 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9440 /* Serial Number */ 9441 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9442 /* Device Identification */ 9443 pages->page_list[p++] = SVPD_DEVICE_ID; 9444 /* Extended INQUIRY Data */ 9445 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9446 /* Mode Page Policy */ 9447 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9448 /* SCSI Ports */ 9449 pages->page_list[p++] = SVPD_SCSI_PORTS; 9450 /* Third-party Copy */ 9451 pages->page_list[p++] = SVPD_SCSI_TPC; 9452 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9453 /* Block limits */ 9454 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9455 /* Block Device Characteristics */ 9456 pages->page_list[p++] = SVPD_BDC; 9457 /* Logical Block Provisioning */ 9458 pages->page_list[p++] = SVPD_LBP; 9459 } 9460 pages->length = p; 9461 9462 ctl_set_success(ctsio); 9463 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9464 ctsio->be_move_done = ctl_config_move_done; 9465 ctl_datamove((union ctl_io *)ctsio); 9466 return (CTL_RETVAL_COMPLETE); 9467} 9468 9469/* 9470 * SCSI VPD page 0x80, the Unit Serial Number page. 9471 */ 9472static int 9473ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9474{ 9475 struct scsi_vpd_unit_serial_number *sn_ptr; 9476 struct ctl_lun *lun; 9477 int data_len; 9478 9479 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9480 9481 data_len = 4 + CTL_SN_LEN; 9482 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9483 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9484 if (data_len < alloc_len) { 9485 ctsio->residual = alloc_len - data_len; 9486 ctsio->kern_data_len = data_len; 9487 ctsio->kern_total_len = data_len; 9488 } else { 9489 ctsio->residual = 0; 9490 ctsio->kern_data_len = alloc_len; 9491 ctsio->kern_total_len = alloc_len; 9492 } 9493 ctsio->kern_data_resid = 0; 9494 ctsio->kern_rel_offset = 0; 9495 ctsio->kern_sg_entries = 0; 9496 9497 /* 9498 * The control device is always connected. The disk device, on the 9499 * other hand, may not be online all the time. Need to change this 9500 * to figure out whether the disk device is actually online or not. 9501 */ 9502 if (lun != NULL) 9503 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9504 lun->be_lun->lun_type; 9505 else 9506 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9507 9508 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9509 sn_ptr->length = CTL_SN_LEN; 9510 /* 9511 * If we don't have a LUN, we just leave the serial number as 9512 * all spaces. 9513 */ 9514 if (lun != NULL) { 9515 strncpy((char *)sn_ptr->serial_num, 9516 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9517 } else 9518 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9519 9520 ctl_set_success(ctsio); 9521 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9522 ctsio->be_move_done = ctl_config_move_done; 9523 ctl_datamove((union ctl_io *)ctsio); 9524 return (CTL_RETVAL_COMPLETE); 9525} 9526 9527 9528/* 9529 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9530 */ 9531static int 9532ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9533{ 9534 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9535 struct ctl_lun *lun; 9536 int data_len; 9537 9538 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9539 9540 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9541 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9542 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9543 ctsio->kern_sg_entries = 0; 9544 9545 if (data_len < alloc_len) { 9546 ctsio->residual = alloc_len - data_len; 9547 ctsio->kern_data_len = data_len; 9548 ctsio->kern_total_len = data_len; 9549 } else { 9550 ctsio->residual = 0; 9551 ctsio->kern_data_len = alloc_len; 9552 ctsio->kern_total_len = alloc_len; 9553 } 9554 ctsio->kern_data_resid = 0; 9555 ctsio->kern_rel_offset = 0; 9556 ctsio->kern_sg_entries = 0; 9557 9558 /* 9559 * The control device is always connected. The disk device, on the 9560 * other hand, may not be online all the time. 9561 */ 9562 if (lun != NULL) 9563 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9564 lun->be_lun->lun_type; 9565 else 9566 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9567 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9568 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9569 /* 9570 * We support head of queue, ordered and simple tags. 9571 */ 9572 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9573 /* 9574 * Volatile cache supported. 9575 */ 9576 eid_ptr->flags3 = SVPD_EID_V_SUP; 9577 9578 /* 9579 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9580 * attention for a particular IT nexus on all LUNs once we report 9581 * it to that nexus once. This bit is required as of SPC-4. 9582 */ 9583 eid_ptr->flags4 = SVPD_EID_LUICLR; 9584 9585 /* 9586 * We support revert to defaults (RTD) bit in MODE SELECT. 9587 */ 9588 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9589 9590 /* 9591 * XXX KDM in order to correctly answer this, we would need 9592 * information from the SIM to determine how much sense data it 9593 * can send. So this would really be a path inquiry field, most 9594 * likely. This can be set to a maximum of 252 according to SPC-4, 9595 * but the hardware may or may not be able to support that much. 9596 * 0 just means that the maximum sense data length is not reported. 9597 */ 9598 eid_ptr->max_sense_length = 0; 9599 9600 ctl_set_success(ctsio); 9601 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9602 ctsio->be_move_done = ctl_config_move_done; 9603 ctl_datamove((union ctl_io *)ctsio); 9604 return (CTL_RETVAL_COMPLETE); 9605} 9606 9607static int 9608ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9609{ 9610 struct scsi_vpd_mode_page_policy *mpp_ptr; 9611 struct ctl_lun *lun; 9612 int data_len; 9613 9614 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9615 9616 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9617 sizeof(struct scsi_vpd_mode_page_policy_descr); 9618 9619 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9620 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9621 ctsio->kern_sg_entries = 0; 9622 9623 if (data_len < alloc_len) { 9624 ctsio->residual = alloc_len - data_len; 9625 ctsio->kern_data_len = data_len; 9626 ctsio->kern_total_len = data_len; 9627 } else { 9628 ctsio->residual = 0; 9629 ctsio->kern_data_len = alloc_len; 9630 ctsio->kern_total_len = alloc_len; 9631 } 9632 ctsio->kern_data_resid = 0; 9633 ctsio->kern_rel_offset = 0; 9634 ctsio->kern_sg_entries = 0; 9635 9636 /* 9637 * The control device is always connected. The disk device, on the 9638 * other hand, may not be online all the time. 9639 */ 9640 if (lun != NULL) 9641 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9642 lun->be_lun->lun_type; 9643 else 9644 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9645 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9646 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9647 mpp_ptr->descr[0].page_code = 0x3f; 9648 mpp_ptr->descr[0].subpage_code = 0xff; 9649 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9650 9651 ctl_set_success(ctsio); 9652 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9653 ctsio->be_move_done = ctl_config_move_done; 9654 ctl_datamove((union ctl_io *)ctsio); 9655 return (CTL_RETVAL_COMPLETE); 9656} 9657 9658/* 9659 * SCSI VPD page 0x83, the Device Identification page. 9660 */ 9661static int 9662ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9663{ 9664 struct scsi_vpd_device_id *devid_ptr; 9665 struct scsi_vpd_id_descriptor *desc; 9666 struct ctl_softc *softc; 9667 struct ctl_lun *lun; 9668 struct ctl_port *port; 9669 int data_len, g; 9670 uint8_t proto; 9671 9672 softc = control_softc; 9673 9674 port = ctl_io_port(&ctsio->io_hdr); 9675 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9676 9677 data_len = sizeof(struct scsi_vpd_device_id) + 9678 sizeof(struct scsi_vpd_id_descriptor) + 9679 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9680 sizeof(struct scsi_vpd_id_descriptor) + 9681 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9682 if (lun && lun->lun_devid) 9683 data_len += lun->lun_devid->len; 9684 if (port && port->port_devid) 9685 data_len += port->port_devid->len; 9686 if (port && port->target_devid) 9687 data_len += port->target_devid->len; 9688 9689 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9690 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9691 ctsio->kern_sg_entries = 0; 9692 9693 if (data_len < alloc_len) { 9694 ctsio->residual = alloc_len - data_len; 9695 ctsio->kern_data_len = data_len; 9696 ctsio->kern_total_len = data_len; 9697 } else { 9698 ctsio->residual = 0; 9699 ctsio->kern_data_len = alloc_len; 9700 ctsio->kern_total_len = alloc_len; 9701 } 9702 ctsio->kern_data_resid = 0; 9703 ctsio->kern_rel_offset = 0; 9704 ctsio->kern_sg_entries = 0; 9705 9706 /* 9707 * The control device is always connected. The disk device, on the 9708 * other hand, may not be online all the time. 9709 */ 9710 if (lun != NULL) 9711 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9712 lun->be_lun->lun_type; 9713 else 9714 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9715 devid_ptr->page_code = SVPD_DEVICE_ID; 9716 scsi_ulto2b(data_len - 4, devid_ptr->length); 9717 9718 if (port && port->port_type == CTL_PORT_FC) 9719 proto = SCSI_PROTO_FC << 4; 9720 else if (port && port->port_type == CTL_PORT_ISCSI) 9721 proto = SCSI_PROTO_ISCSI << 4; 9722 else 9723 proto = SCSI_PROTO_SPI << 4; 9724 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9725 9726 /* 9727 * We're using a LUN association here. i.e., this device ID is a 9728 * per-LUN identifier. 9729 */ 9730 if (lun && lun->lun_devid) { 9731 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9732 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9733 lun->lun_devid->len); 9734 } 9735 9736 /* 9737 * This is for the WWPN which is a port association. 9738 */ 9739 if (port && port->port_devid) { 9740 memcpy(desc, port->port_devid->data, port->port_devid->len); 9741 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9742 port->port_devid->len); 9743 } 9744 9745 /* 9746 * This is for the Relative Target Port(type 4h) identifier 9747 */ 9748 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9749 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9750 SVPD_ID_TYPE_RELTARG; 9751 desc->length = 4; 9752 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9753 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9754 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9755 9756 /* 9757 * This is for the Target Port Group(type 5h) identifier 9758 */ 9759 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9760 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9761 SVPD_ID_TYPE_TPORTGRP; 9762 desc->length = 4; 9763 if (softc->is_single || 9764 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9765 g = 1; 9766 else 9767 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9768 scsi_ulto2b(g, &desc->identifier[2]); 9769 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9770 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9771 9772 /* 9773 * This is for the Target identifier 9774 */ 9775 if (port && port->target_devid) { 9776 memcpy(desc, port->target_devid->data, port->target_devid->len); 9777 } 9778 9779 ctl_set_success(ctsio); 9780 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9781 ctsio->be_move_done = ctl_config_move_done; 9782 ctl_datamove((union ctl_io *)ctsio); 9783 return (CTL_RETVAL_COMPLETE); 9784} 9785 9786static int 9787ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9788{ 9789 struct ctl_softc *softc = control_softc; 9790 struct scsi_vpd_scsi_ports *sp; 9791 struct scsi_vpd_port_designation *pd; 9792 struct scsi_vpd_port_designation_cont *pdc; 9793 struct ctl_lun *lun; 9794 struct ctl_port *port; 9795 int data_len, num_target_ports, iid_len, id_len; 9796 9797 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9798 9799 num_target_ports = 0; 9800 iid_len = 0; 9801 id_len = 0; 9802 mtx_lock(&softc->ctl_lock); 9803 STAILQ_FOREACH(port, &softc->port_list, links) { 9804 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9805 continue; 9806 if (lun != NULL && 9807 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9808 continue; 9809 num_target_ports++; 9810 if (port->init_devid) 9811 iid_len += port->init_devid->len; 9812 if (port->port_devid) 9813 id_len += port->port_devid->len; 9814 } 9815 mtx_unlock(&softc->ctl_lock); 9816 9817 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9818 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9819 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9820 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9821 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9822 ctsio->kern_sg_entries = 0; 9823 9824 if (data_len < alloc_len) { 9825 ctsio->residual = alloc_len - data_len; 9826 ctsio->kern_data_len = data_len; 9827 ctsio->kern_total_len = data_len; 9828 } else { 9829 ctsio->residual = 0; 9830 ctsio->kern_data_len = alloc_len; 9831 ctsio->kern_total_len = alloc_len; 9832 } 9833 ctsio->kern_data_resid = 0; 9834 ctsio->kern_rel_offset = 0; 9835 ctsio->kern_sg_entries = 0; 9836 9837 /* 9838 * The control device is always connected. The disk device, on the 9839 * other hand, may not be online all the time. Need to change this 9840 * to figure out whether the disk device is actually online or not. 9841 */ 9842 if (lun != NULL) 9843 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9844 lun->be_lun->lun_type; 9845 else 9846 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9847 9848 sp->page_code = SVPD_SCSI_PORTS; 9849 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9850 sp->page_length); 9851 pd = &sp->design[0]; 9852 9853 mtx_lock(&softc->ctl_lock); 9854 STAILQ_FOREACH(port, &softc->port_list, links) { 9855 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9856 continue; 9857 if (lun != NULL && 9858 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9859 continue; 9860 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9861 if (port->init_devid) { 9862 iid_len = port->init_devid->len; 9863 memcpy(pd->initiator_transportid, 9864 port->init_devid->data, port->init_devid->len); 9865 } else 9866 iid_len = 0; 9867 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9868 pdc = (struct scsi_vpd_port_designation_cont *) 9869 (&pd->initiator_transportid[iid_len]); 9870 if (port->port_devid) { 9871 id_len = port->port_devid->len; 9872 memcpy(pdc->target_port_descriptors, 9873 port->port_devid->data, port->port_devid->len); 9874 } else 9875 id_len = 0; 9876 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9877 pd = (struct scsi_vpd_port_designation *) 9878 ((uint8_t *)pdc->target_port_descriptors + id_len); 9879 } 9880 mtx_unlock(&softc->ctl_lock); 9881 9882 ctl_set_success(ctsio); 9883 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9884 ctsio->be_move_done = ctl_config_move_done; 9885 ctl_datamove((union ctl_io *)ctsio); 9886 return (CTL_RETVAL_COMPLETE); 9887} 9888 9889static int 9890ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9891{ 9892 struct scsi_vpd_block_limits *bl_ptr; 9893 struct ctl_lun *lun; 9894 uint64_t ival; 9895 9896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9897 9898 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9899 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9900 ctsio->kern_sg_entries = 0; 9901 9902 if (sizeof(*bl_ptr) < alloc_len) { 9903 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9904 ctsio->kern_data_len = sizeof(*bl_ptr); 9905 ctsio->kern_total_len = sizeof(*bl_ptr); 9906 } else { 9907 ctsio->residual = 0; 9908 ctsio->kern_data_len = alloc_len; 9909 ctsio->kern_total_len = alloc_len; 9910 } 9911 ctsio->kern_data_resid = 0; 9912 ctsio->kern_rel_offset = 0; 9913 ctsio->kern_sg_entries = 0; 9914 9915 /* 9916 * The control device is always connected. The disk device, on the 9917 * other hand, may not be online all the time. Need to change this 9918 * to figure out whether the disk device is actually online or not. 9919 */ 9920 if (lun != NULL) 9921 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9922 lun->be_lun->lun_type; 9923 else 9924 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9925 9926 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9927 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9928 bl_ptr->max_cmp_write_len = 0xff; 9929 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9930 if (lun != NULL) { 9931 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9932 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9933 ival = 0xffffffff; 9934 ctl_get_opt_number(&lun->be_lun->options, 9935 "unmap_max_lba", &ival); 9936 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9937 ival = 0xffffffff; 9938 ctl_get_opt_number(&lun->be_lun->options, 9939 "unmap_max_descr", &ival); 9940 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9941 if (lun->be_lun->ublockexp != 0) { 9942 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9943 bl_ptr->opt_unmap_grain); 9944 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9945 bl_ptr->unmap_grain_align); 9946 } 9947 } 9948 scsi_ulto4b(lun->be_lun->atomicblock, 9949 bl_ptr->max_atomic_transfer_length); 9950 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9951 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9952 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9953 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9954 ival = UINT64_MAX; 9955 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival); 9956 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9957 } 9958 9959 ctl_set_success(ctsio); 9960 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9961 ctsio->be_move_done = ctl_config_move_done; 9962 ctl_datamove((union ctl_io *)ctsio); 9963 return (CTL_RETVAL_COMPLETE); 9964} 9965 9966static int 9967ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9968{ 9969 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9970 struct ctl_lun *lun; 9971 const char *value; 9972 u_int i; 9973 9974 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9975 9976 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9977 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9978 ctsio->kern_sg_entries = 0; 9979 9980 if (sizeof(*bdc_ptr) < alloc_len) { 9981 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9982 ctsio->kern_data_len = sizeof(*bdc_ptr); 9983 ctsio->kern_total_len = sizeof(*bdc_ptr); 9984 } else { 9985 ctsio->residual = 0; 9986 ctsio->kern_data_len = alloc_len; 9987 ctsio->kern_total_len = alloc_len; 9988 } 9989 ctsio->kern_data_resid = 0; 9990 ctsio->kern_rel_offset = 0; 9991 ctsio->kern_sg_entries = 0; 9992 9993 /* 9994 * The control device is always connected. The disk device, on the 9995 * other hand, may not be online all the time. Need to change this 9996 * to figure out whether the disk device is actually online or not. 9997 */ 9998 if (lun != NULL) 9999 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10000 lun->be_lun->lun_type; 10001 else 10002 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10003 bdc_ptr->page_code = SVPD_BDC; 10004 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10005 if (lun != NULL && 10006 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10007 i = strtol(value, NULL, 0); 10008 else 10009 i = CTL_DEFAULT_ROTATION_RATE; 10010 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10011 if (lun != NULL && 10012 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10013 i = strtol(value, NULL, 0); 10014 else 10015 i = 0; 10016 bdc_ptr->wab_wac_ff = (i & 0x0f); 10017 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10018 10019 ctl_set_success(ctsio); 10020 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10021 ctsio->be_move_done = ctl_config_move_done; 10022 ctl_datamove((union ctl_io *)ctsio); 10023 return (CTL_RETVAL_COMPLETE); 10024} 10025 10026static int 10027ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10028{ 10029 struct scsi_vpd_logical_block_prov *lbp_ptr; 10030 struct ctl_lun *lun; 10031 const char *value; 10032 10033 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10034 10035 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10036 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10037 ctsio->kern_sg_entries = 0; 10038 10039 if (sizeof(*lbp_ptr) < alloc_len) { 10040 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10041 ctsio->kern_data_len = sizeof(*lbp_ptr); 10042 ctsio->kern_total_len = sizeof(*lbp_ptr); 10043 } else { 10044 ctsio->residual = 0; 10045 ctsio->kern_data_len = alloc_len; 10046 ctsio->kern_total_len = alloc_len; 10047 } 10048 ctsio->kern_data_resid = 0; 10049 ctsio->kern_rel_offset = 0; 10050 ctsio->kern_sg_entries = 0; 10051 10052 /* 10053 * The control device is always connected. The disk device, on the 10054 * other hand, may not be online all the time. Need to change this 10055 * to figure out whether the disk device is actually online or not. 10056 */ 10057 if (lun != NULL) 10058 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10059 lun->be_lun->lun_type; 10060 else 10061 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10062 10063 lbp_ptr->page_code = SVPD_LBP; 10064 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10065 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10066 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10067 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10068 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10069 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type"); 10070 if (value != NULL) { 10071 if (strcmp(value, "resource") == 0) 10072 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10073 else if (strcmp(value, "thin") == 0) 10074 lbp_ptr->prov_type = SVPD_LBP_THIN; 10075 } else 10076 lbp_ptr->prov_type = SVPD_LBP_THIN; 10077 } 10078 10079 ctl_set_success(ctsio); 10080 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10081 ctsio->be_move_done = ctl_config_move_done; 10082 ctl_datamove((union ctl_io *)ctsio); 10083 return (CTL_RETVAL_COMPLETE); 10084} 10085 10086/* 10087 * INQUIRY with the EVPD bit set. 10088 */ 10089static int 10090ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10091{ 10092 struct ctl_lun *lun; 10093 struct scsi_inquiry *cdb; 10094 int alloc_len, retval; 10095 10096 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10097 cdb = (struct scsi_inquiry *)ctsio->cdb; 10098 alloc_len = scsi_2btoul(cdb->length); 10099 10100 switch (cdb->page_code) { 10101 case SVPD_SUPPORTED_PAGES: 10102 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10103 break; 10104 case SVPD_UNIT_SERIAL_NUMBER: 10105 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10106 break; 10107 case SVPD_DEVICE_ID: 10108 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10109 break; 10110 case SVPD_EXTENDED_INQUIRY_DATA: 10111 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10112 break; 10113 case SVPD_MODE_PAGE_POLICY: 10114 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10115 break; 10116 case SVPD_SCSI_PORTS: 10117 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10118 break; 10119 case SVPD_SCSI_TPC: 10120 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10121 break; 10122 case SVPD_BLOCK_LIMITS: 10123 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10124 goto err; 10125 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10126 break; 10127 case SVPD_BDC: 10128 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10129 goto err; 10130 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10131 break; 10132 case SVPD_LBP: 10133 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10134 goto err; 10135 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10136 break; 10137 default: 10138err: 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 retval = CTL_RETVAL_COMPLETE; 10147 break; 10148 } 10149 10150 return (retval); 10151} 10152 10153/* 10154 * Standard INQUIRY data. 10155 */ 10156static int 10157ctl_inquiry_std(struct ctl_scsiio *ctsio) 10158{ 10159 struct scsi_inquiry_data *inq_ptr; 10160 struct scsi_inquiry *cdb; 10161 struct ctl_softc *softc = control_softc; 10162 struct ctl_port *port; 10163 struct ctl_lun *lun; 10164 char *val; 10165 uint32_t alloc_len, data_len; 10166 ctl_port_type port_type; 10167 10168 port = ctl_io_port(&ctsio->io_hdr); 10169 port_type = port->port_type; 10170 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10171 port_type = CTL_PORT_SCSI; 10172 10173 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10174 cdb = (struct scsi_inquiry *)ctsio->cdb; 10175 alloc_len = scsi_2btoul(cdb->length); 10176 10177 /* 10178 * We malloc the full inquiry data size here and fill it 10179 * in. If the user only asks for less, we'll give him 10180 * that much. 10181 */ 10182 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10183 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10184 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10185 ctsio->kern_sg_entries = 0; 10186 ctsio->kern_data_resid = 0; 10187 ctsio->kern_rel_offset = 0; 10188 10189 if (data_len < alloc_len) { 10190 ctsio->residual = alloc_len - data_len; 10191 ctsio->kern_data_len = data_len; 10192 ctsio->kern_total_len = data_len; 10193 } else { 10194 ctsio->residual = 0; 10195 ctsio->kern_data_len = alloc_len; 10196 ctsio->kern_total_len = alloc_len; 10197 } 10198 10199 if (lun != NULL) { 10200 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10201 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10202 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10203 lun->be_lun->lun_type; 10204 } else { 10205 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10206 lun->be_lun->lun_type; 10207 } 10208 if (lun->flags & CTL_LUN_REMOVABLE) 10209 inq_ptr->dev_qual2 |= SID_RMB; 10210 } else 10211 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10212 10213 /* RMB in byte 2 is 0 */ 10214 inq_ptr->version = SCSI_REV_SPC5; 10215 10216 /* 10217 * According to SAM-3, even if a device only supports a single 10218 * level of LUN addressing, it should still set the HISUP bit: 10219 * 10220 * 4.9.1 Logical unit numbers overview 10221 * 10222 * All logical unit number formats described in this standard are 10223 * hierarchical in structure even when only a single level in that 10224 * hierarchy is used. The HISUP bit shall be set to one in the 10225 * standard INQUIRY data (see SPC-2) when any logical unit number 10226 * format described in this standard is used. Non-hierarchical 10227 * formats are outside the scope of this standard. 10228 * 10229 * Therefore we set the HiSup bit here. 10230 * 10231 * The response format is 2, per SPC-3. 10232 */ 10233 inq_ptr->response_format = SID_HiSup | 2; 10234 10235 inq_ptr->additional_length = data_len - 10236 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10237 CTL_DEBUG_PRINT(("additional_length = %d\n", 10238 inq_ptr->additional_length)); 10239 10240 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10241 if (port_type == CTL_PORT_SCSI) 10242 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10243 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10244 inq_ptr->flags = SID_CmdQue; 10245 if (port_type == CTL_PORT_SCSI) 10246 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10247 10248 /* 10249 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10250 * We have 8 bytes for the vendor name, and 16 bytes for the device 10251 * name and 4 bytes for the revision. 10252 */ 10253 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10254 "vendor")) == NULL) { 10255 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10256 } else { 10257 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10258 strncpy(inq_ptr->vendor, val, 10259 min(sizeof(inq_ptr->vendor), strlen(val))); 10260 } 10261 if (lun == NULL) { 10262 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10263 sizeof(inq_ptr->product)); 10264 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10265 switch (lun->be_lun->lun_type) { 10266 case T_DIRECT: 10267 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10268 sizeof(inq_ptr->product)); 10269 break; 10270 case T_PROCESSOR: 10271 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10272 sizeof(inq_ptr->product)); 10273 break; 10274 case T_CDROM: 10275 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10276 sizeof(inq_ptr->product)); 10277 break; 10278 default: 10279 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10280 sizeof(inq_ptr->product)); 10281 break; 10282 } 10283 } else { 10284 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10285 strncpy(inq_ptr->product, val, 10286 min(sizeof(inq_ptr->product), strlen(val))); 10287 } 10288 10289 /* 10290 * XXX make this a macro somewhere so it automatically gets 10291 * incremented when we make changes. 10292 */ 10293 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10294 "revision")) == NULL) { 10295 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10296 } else { 10297 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10298 strncpy(inq_ptr->revision, val, 10299 min(sizeof(inq_ptr->revision), strlen(val))); 10300 } 10301 10302 /* 10303 * For parallel SCSI, we support double transition and single 10304 * transition clocking. We also support QAS (Quick Arbitration 10305 * and Selection) and Information Unit transfers on both the 10306 * control and array devices. 10307 */ 10308 if (port_type == CTL_PORT_SCSI) 10309 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10310 SID_SPI_IUS; 10311 10312 /* SAM-6 (no version claimed) */ 10313 scsi_ulto2b(0x00C0, inq_ptr->version1); 10314 /* SPC-5 (no version claimed) */ 10315 scsi_ulto2b(0x05C0, inq_ptr->version2); 10316 if (port_type == CTL_PORT_FC) { 10317 /* FCP-2 ANSI INCITS.350:2003 */ 10318 scsi_ulto2b(0x0917, inq_ptr->version3); 10319 } else if (port_type == CTL_PORT_SCSI) { 10320 /* SPI-4 ANSI INCITS.362:200x */ 10321 scsi_ulto2b(0x0B56, inq_ptr->version3); 10322 } else if (port_type == CTL_PORT_ISCSI) { 10323 /* iSCSI (no version claimed) */ 10324 scsi_ulto2b(0x0960, inq_ptr->version3); 10325 } else if (port_type == CTL_PORT_SAS) { 10326 /* SAS (no version claimed) */ 10327 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10328 } 10329 10330 if (lun == NULL) { 10331 /* SBC-4 (no version claimed) */ 10332 scsi_ulto2b(0x0600, inq_ptr->version4); 10333 } else { 10334 switch (lun->be_lun->lun_type) { 10335 case T_DIRECT: 10336 /* SBC-4 (no version claimed) */ 10337 scsi_ulto2b(0x0600, inq_ptr->version4); 10338 break; 10339 case T_PROCESSOR: 10340 break; 10341 case T_CDROM: 10342 /* MMC-6 (no version claimed) */ 10343 scsi_ulto2b(0x04E0, inq_ptr->version4); 10344 break; 10345 default: 10346 break; 10347 } 10348 } 10349 10350 ctl_set_success(ctsio); 10351 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10352 ctsio->be_move_done = ctl_config_move_done; 10353 ctl_datamove((union ctl_io *)ctsio); 10354 return (CTL_RETVAL_COMPLETE); 10355} 10356 10357int 10358ctl_inquiry(struct ctl_scsiio *ctsio) 10359{ 10360 struct scsi_inquiry *cdb; 10361 int retval; 10362 10363 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10364 10365 cdb = (struct scsi_inquiry *)ctsio->cdb; 10366 if (cdb->byte2 & SI_EVPD) 10367 retval = ctl_inquiry_evpd(ctsio); 10368 else if (cdb->page_code == 0) 10369 retval = ctl_inquiry_std(ctsio); 10370 else { 10371 ctl_set_invalid_field(ctsio, 10372 /*sks_valid*/ 1, 10373 /*command*/ 1, 10374 /*field*/ 2, 10375 /*bit_valid*/ 0, 10376 /*bit*/ 0); 10377 ctl_done((union ctl_io *)ctsio); 10378 return (CTL_RETVAL_COMPLETE); 10379 } 10380 10381 return (retval); 10382} 10383 10384int 10385ctl_get_config(struct ctl_scsiio *ctsio) 10386{ 10387 struct scsi_get_config_header *hdr; 10388 struct scsi_get_config_feature *feature; 10389 struct scsi_get_config *cdb; 10390 struct ctl_lun *lun; 10391 uint32_t alloc_len, data_len; 10392 int rt, starting; 10393 10394 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10395 cdb = (struct scsi_get_config *)ctsio->cdb; 10396 rt = (cdb->rt & SGC_RT_MASK); 10397 starting = scsi_2btoul(cdb->starting_feature); 10398 alloc_len = scsi_2btoul(cdb->length); 10399 10400 data_len = sizeof(struct scsi_get_config_header) + 10401 sizeof(struct scsi_get_config_feature) + 8 + 10402 sizeof(struct scsi_get_config_feature) + 8 + 10403 sizeof(struct scsi_get_config_feature) + 4 + 10404 sizeof(struct scsi_get_config_feature) + 4 + 10405 sizeof(struct scsi_get_config_feature) + 8 + 10406 sizeof(struct scsi_get_config_feature) + 10407 sizeof(struct scsi_get_config_feature) + 4 + 10408 sizeof(struct scsi_get_config_feature) + 4 + 10409 sizeof(struct scsi_get_config_feature) + 4 + 10410 sizeof(struct scsi_get_config_feature) + 4 + 10411 sizeof(struct scsi_get_config_feature) + 4 + 10412 sizeof(struct scsi_get_config_feature) + 4; 10413 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10414 ctsio->kern_sg_entries = 0; 10415 ctsio->kern_data_resid = 0; 10416 ctsio->kern_rel_offset = 0; 10417 10418 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10419 if (lun->flags & CTL_LUN_NO_MEDIA) 10420 scsi_ulto2b(0x0000, hdr->current_profile); 10421 else 10422 scsi_ulto2b(0x0010, hdr->current_profile); 10423 feature = (struct scsi_get_config_feature *)(hdr + 1); 10424 10425 if (starting > 0x003b) 10426 goto done; 10427 if (starting > 0x003a) 10428 goto f3b; 10429 if (starting > 0x002b) 10430 goto f3a; 10431 if (starting > 0x002a) 10432 goto f2b; 10433 if (starting > 0x001f) 10434 goto f2a; 10435 if (starting > 0x001e) 10436 goto f1f; 10437 if (starting > 0x001d) 10438 goto f1e; 10439 if (starting > 0x0010) 10440 goto f1d; 10441 if (starting > 0x0003) 10442 goto f10; 10443 if (starting > 0x0002) 10444 goto f3; 10445 if (starting > 0x0001) 10446 goto f2; 10447 if (starting > 0x0000) 10448 goto f1; 10449 10450 /* Profile List */ 10451 scsi_ulto2b(0x0000, feature->feature_code); 10452 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10453 feature->add_length = 8; 10454 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10455 feature->feature_data[2] = 0x00; 10456 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10457 feature->feature_data[6] = 0x01; 10458 feature = (struct scsi_get_config_feature *) 10459 &feature->feature_data[feature->add_length]; 10460 10461f1: /* Core */ 10462 scsi_ulto2b(0x0001, feature->feature_code); 10463 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10464 feature->add_length = 8; 10465 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10466 feature->feature_data[4] = 0x03; 10467 feature = (struct scsi_get_config_feature *) 10468 &feature->feature_data[feature->add_length]; 10469 10470f2: /* Morphing */ 10471 scsi_ulto2b(0x0002, feature->feature_code); 10472 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10473 feature->add_length = 4; 10474 feature->feature_data[0] = 0x02; 10475 feature = (struct scsi_get_config_feature *) 10476 &feature->feature_data[feature->add_length]; 10477 10478f3: /* Removable Medium */ 10479 scsi_ulto2b(0x0003, feature->feature_code); 10480 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10481 feature->add_length = 4; 10482 feature->feature_data[0] = 0x39; 10483 feature = (struct scsi_get_config_feature *) 10484 &feature->feature_data[feature->add_length]; 10485 10486 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10487 goto done; 10488 10489f10: /* Random Read */ 10490 scsi_ulto2b(0x0010, feature->feature_code); 10491 feature->flags = 0x00; 10492 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10493 feature->flags |= SGC_F_CURRENT; 10494 feature->add_length = 8; 10495 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10496 scsi_ulto2b(1, &feature->feature_data[4]); 10497 feature->feature_data[6] = 0x00; 10498 feature = (struct scsi_get_config_feature *) 10499 &feature->feature_data[feature->add_length]; 10500 10501f1d: /* Multi-Read */ 10502 scsi_ulto2b(0x001D, feature->feature_code); 10503 feature->flags = 0x00; 10504 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10505 feature->flags |= SGC_F_CURRENT; 10506 feature->add_length = 0; 10507 feature = (struct scsi_get_config_feature *) 10508 &feature->feature_data[feature->add_length]; 10509 10510f1e: /* CD Read */ 10511 scsi_ulto2b(0x001E, feature->feature_code); 10512 feature->flags = 0x00; 10513 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10514 feature->flags |= SGC_F_CURRENT; 10515 feature->add_length = 4; 10516 feature->feature_data[0] = 0x00; 10517 feature = (struct scsi_get_config_feature *) 10518 &feature->feature_data[feature->add_length]; 10519 10520f1f: /* DVD Read */ 10521 scsi_ulto2b(0x001F, feature->feature_code); 10522 feature->flags = 0x08; 10523 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10524 feature->flags |= SGC_F_CURRENT; 10525 feature->add_length = 4; 10526 feature->feature_data[0] = 0x01; 10527 feature->feature_data[2] = 0x03; 10528 feature = (struct scsi_get_config_feature *) 10529 &feature->feature_data[feature->add_length]; 10530 10531f2a: /* DVD+RW */ 10532 scsi_ulto2b(0x002A, feature->feature_code); 10533 feature->flags = 0x04; 10534 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10535 feature->flags |= SGC_F_CURRENT; 10536 feature->add_length = 4; 10537 feature->feature_data[0] = 0x00; 10538 feature->feature_data[1] = 0x00; 10539 feature = (struct scsi_get_config_feature *) 10540 &feature->feature_data[feature->add_length]; 10541 10542f2b: /* DVD+R */ 10543 scsi_ulto2b(0x002B, feature->feature_code); 10544 feature->flags = 0x00; 10545 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10546 feature->flags |= SGC_F_CURRENT; 10547 feature->add_length = 4; 10548 feature->feature_data[0] = 0x00; 10549 feature = (struct scsi_get_config_feature *) 10550 &feature->feature_data[feature->add_length]; 10551 10552f3a: /* DVD+RW Dual Layer */ 10553 scsi_ulto2b(0x003A, feature->feature_code); 10554 feature->flags = 0x00; 10555 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10556 feature->flags |= SGC_F_CURRENT; 10557 feature->add_length = 4; 10558 feature->feature_data[0] = 0x00; 10559 feature->feature_data[1] = 0x00; 10560 feature = (struct scsi_get_config_feature *) 10561 &feature->feature_data[feature->add_length]; 10562 10563f3b: /* DVD+R Dual Layer */ 10564 scsi_ulto2b(0x003B, feature->feature_code); 10565 feature->flags = 0x00; 10566 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10567 feature->flags |= SGC_F_CURRENT; 10568 feature->add_length = 4; 10569 feature->feature_data[0] = 0x00; 10570 feature = (struct scsi_get_config_feature *) 10571 &feature->feature_data[feature->add_length]; 10572 10573done: 10574 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10575 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10576 feature = (struct scsi_get_config_feature *)(hdr + 1); 10577 if (scsi_2btoul(feature->feature_code) == starting) 10578 feature = (struct scsi_get_config_feature *) 10579 &feature->feature_data[feature->add_length]; 10580 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10581 } 10582 scsi_ulto4b(data_len - 4, hdr->data_length); 10583 if (data_len < alloc_len) { 10584 ctsio->residual = alloc_len - data_len; 10585 ctsio->kern_data_len = data_len; 10586 ctsio->kern_total_len = data_len; 10587 } else { 10588 ctsio->residual = 0; 10589 ctsio->kern_data_len = alloc_len; 10590 ctsio->kern_total_len = alloc_len; 10591 } 10592 10593 ctl_set_success(ctsio); 10594 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10595 ctsio->be_move_done = ctl_config_move_done; 10596 ctl_datamove((union ctl_io *)ctsio); 10597 return (CTL_RETVAL_COMPLETE); 10598} 10599 10600int 10601ctl_get_event_status(struct ctl_scsiio *ctsio) 10602{ 10603 struct scsi_get_event_status_header *hdr; 10604 struct scsi_get_event_status *cdb; 10605 struct ctl_lun *lun; 10606 uint32_t alloc_len, data_len; 10607 int notif_class; 10608 10609 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10610 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10611 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10612 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10613 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10614 ctl_done((union ctl_io *)ctsio); 10615 return (CTL_RETVAL_COMPLETE); 10616 } 10617 notif_class = cdb->notif_class; 10618 alloc_len = scsi_2btoul(cdb->length); 10619 10620 data_len = sizeof(struct scsi_get_event_status_header); 10621 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10622 ctsio->kern_sg_entries = 0; 10623 ctsio->kern_data_resid = 0; 10624 ctsio->kern_rel_offset = 0; 10625 10626 if (data_len < alloc_len) { 10627 ctsio->residual = alloc_len - data_len; 10628 ctsio->kern_data_len = data_len; 10629 ctsio->kern_total_len = data_len; 10630 } else { 10631 ctsio->residual = 0; 10632 ctsio->kern_data_len = alloc_len; 10633 ctsio->kern_total_len = alloc_len; 10634 } 10635 10636 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10637 scsi_ulto2b(0, hdr->descr_length); 10638 hdr->nea_class = SGESN_NEA; 10639 hdr->supported_class = 0; 10640 10641 ctl_set_success(ctsio); 10642 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10643 ctsio->be_move_done = ctl_config_move_done; 10644 ctl_datamove((union ctl_io *)ctsio); 10645 return (CTL_RETVAL_COMPLETE); 10646} 10647 10648int 10649ctl_mechanism_status(struct ctl_scsiio *ctsio) 10650{ 10651 struct scsi_mechanism_status_header *hdr; 10652 struct scsi_mechanism_status *cdb; 10653 struct ctl_lun *lun; 10654 uint32_t alloc_len, data_len; 10655 10656 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10657 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10658 alloc_len = scsi_2btoul(cdb->length); 10659 10660 data_len = sizeof(struct scsi_mechanism_status_header); 10661 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10662 ctsio->kern_sg_entries = 0; 10663 ctsio->kern_data_resid = 0; 10664 ctsio->kern_rel_offset = 0; 10665 10666 if (data_len < alloc_len) { 10667 ctsio->residual = alloc_len - data_len; 10668 ctsio->kern_data_len = data_len; 10669 ctsio->kern_total_len = data_len; 10670 } else { 10671 ctsio->residual = 0; 10672 ctsio->kern_data_len = alloc_len; 10673 ctsio->kern_total_len = alloc_len; 10674 } 10675 10676 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10677 hdr->state1 = 0x00; 10678 hdr->state2 = 0xe0; 10679 scsi_ulto3b(0, hdr->lba); 10680 hdr->slots_num = 0; 10681 scsi_ulto2b(0, hdr->slots_length); 10682 10683 ctl_set_success(ctsio); 10684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10685 ctsio->be_move_done = ctl_config_move_done; 10686 ctl_datamove((union ctl_io *)ctsio); 10687 return (CTL_RETVAL_COMPLETE); 10688} 10689 10690static void 10691ctl_ultomsf(uint32_t lba, uint8_t *buf) 10692{ 10693 10694 lba += 150; 10695 buf[0] = 0; 10696 buf[1] = bin2bcd((lba / 75) / 60); 10697 buf[2] = bin2bcd((lba / 75) % 60); 10698 buf[3] = bin2bcd(lba % 75); 10699} 10700 10701int 10702ctl_read_toc(struct ctl_scsiio *ctsio) 10703{ 10704 struct scsi_read_toc_hdr *hdr; 10705 struct scsi_read_toc_type01_descr *descr; 10706 struct scsi_read_toc *cdb; 10707 struct ctl_lun *lun; 10708 uint32_t alloc_len, data_len; 10709 int format, msf; 10710 10711 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10712 cdb = (struct scsi_read_toc *)ctsio->cdb; 10713 msf = (cdb->byte2 & CD_MSF) != 0; 10714 format = cdb->format; 10715 alloc_len = scsi_2btoul(cdb->data_len); 10716 10717 data_len = sizeof(struct scsi_read_toc_hdr); 10718 if (format == 0) 10719 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10720 else 10721 data_len += sizeof(struct scsi_read_toc_type01_descr); 10722 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10723 ctsio->kern_sg_entries = 0; 10724 ctsio->kern_data_resid = 0; 10725 ctsio->kern_rel_offset = 0; 10726 10727 if (data_len < alloc_len) { 10728 ctsio->residual = alloc_len - data_len; 10729 ctsio->kern_data_len = data_len; 10730 ctsio->kern_total_len = data_len; 10731 } else { 10732 ctsio->residual = 0; 10733 ctsio->kern_data_len = alloc_len; 10734 ctsio->kern_total_len = alloc_len; 10735 } 10736 10737 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10738 if (format == 0) { 10739 scsi_ulto2b(0x12, hdr->data_length); 10740 hdr->first = 1; 10741 hdr->last = 1; 10742 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10743 descr->addr_ctl = 0x14; 10744 descr->track_number = 1; 10745 if (msf) 10746 ctl_ultomsf(0, descr->track_start); 10747 else 10748 scsi_ulto4b(0, descr->track_start); 10749 descr++; 10750 descr->addr_ctl = 0x14; 10751 descr->track_number = 0xaa; 10752 if (msf) 10753 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10754 else 10755 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10756 } else { 10757 scsi_ulto2b(0x0a, hdr->data_length); 10758 hdr->first = 1; 10759 hdr->last = 1; 10760 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10761 descr->addr_ctl = 0x14; 10762 descr->track_number = 1; 10763 if (msf) 10764 ctl_ultomsf(0, descr->track_start); 10765 else 10766 scsi_ulto4b(0, descr->track_start); 10767 } 10768 10769 ctl_set_success(ctsio); 10770 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10771 ctsio->be_move_done = ctl_config_move_done; 10772 ctl_datamove((union ctl_io *)ctsio); 10773 return (CTL_RETVAL_COMPLETE); 10774} 10775 10776/* 10777 * For known CDB types, parse the LBA and length. 10778 */ 10779static int 10780ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10781{ 10782 if (io->io_hdr.io_type != CTL_IO_SCSI) 10783 return (1); 10784 10785 switch (io->scsiio.cdb[0]) { 10786 case COMPARE_AND_WRITE: { 10787 struct scsi_compare_and_write *cdb; 10788 10789 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10790 10791 *lba = scsi_8btou64(cdb->addr); 10792 *len = cdb->length; 10793 break; 10794 } 10795 case READ_6: 10796 case WRITE_6: { 10797 struct scsi_rw_6 *cdb; 10798 10799 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10800 10801 *lba = scsi_3btoul(cdb->addr); 10802 /* only 5 bits are valid in the most significant address byte */ 10803 *lba &= 0x1fffff; 10804 *len = cdb->length; 10805 break; 10806 } 10807 case READ_10: 10808 case WRITE_10: { 10809 struct scsi_rw_10 *cdb; 10810 10811 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10812 10813 *lba = scsi_4btoul(cdb->addr); 10814 *len = scsi_2btoul(cdb->length); 10815 break; 10816 } 10817 case WRITE_VERIFY_10: { 10818 struct scsi_write_verify_10 *cdb; 10819 10820 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10821 10822 *lba = scsi_4btoul(cdb->addr); 10823 *len = scsi_2btoul(cdb->length); 10824 break; 10825 } 10826 case READ_12: 10827 case WRITE_12: { 10828 struct scsi_rw_12 *cdb; 10829 10830 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10831 10832 *lba = scsi_4btoul(cdb->addr); 10833 *len = scsi_4btoul(cdb->length); 10834 break; 10835 } 10836 case WRITE_VERIFY_12: { 10837 struct scsi_write_verify_12 *cdb; 10838 10839 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10840 10841 *lba = scsi_4btoul(cdb->addr); 10842 *len = scsi_4btoul(cdb->length); 10843 break; 10844 } 10845 case READ_16: 10846 case WRITE_16: { 10847 struct scsi_rw_16 *cdb; 10848 10849 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10850 10851 *lba = scsi_8btou64(cdb->addr); 10852 *len = scsi_4btoul(cdb->length); 10853 break; 10854 } 10855 case WRITE_ATOMIC_16: { 10856 struct scsi_write_atomic_16 *cdb; 10857 10858 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10859 10860 *lba = scsi_8btou64(cdb->addr); 10861 *len = scsi_2btoul(cdb->length); 10862 break; 10863 } 10864 case WRITE_VERIFY_16: { 10865 struct scsi_write_verify_16 *cdb; 10866 10867 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10868 10869 *lba = scsi_8btou64(cdb->addr); 10870 *len = scsi_4btoul(cdb->length); 10871 break; 10872 } 10873 case WRITE_SAME_10: { 10874 struct scsi_write_same_10 *cdb; 10875 10876 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10877 10878 *lba = scsi_4btoul(cdb->addr); 10879 *len = scsi_2btoul(cdb->length); 10880 break; 10881 } 10882 case WRITE_SAME_16: { 10883 struct scsi_write_same_16 *cdb; 10884 10885 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10886 10887 *lba = scsi_8btou64(cdb->addr); 10888 *len = scsi_4btoul(cdb->length); 10889 break; 10890 } 10891 case VERIFY_10: { 10892 struct scsi_verify_10 *cdb; 10893 10894 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10895 10896 *lba = scsi_4btoul(cdb->addr); 10897 *len = scsi_2btoul(cdb->length); 10898 break; 10899 } 10900 case VERIFY_12: { 10901 struct scsi_verify_12 *cdb; 10902 10903 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10904 10905 *lba = scsi_4btoul(cdb->addr); 10906 *len = scsi_4btoul(cdb->length); 10907 break; 10908 } 10909 case VERIFY_16: { 10910 struct scsi_verify_16 *cdb; 10911 10912 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10913 10914 *lba = scsi_8btou64(cdb->addr); 10915 *len = scsi_4btoul(cdb->length); 10916 break; 10917 } 10918 case UNMAP: { 10919 *lba = 0; 10920 *len = UINT64_MAX; 10921 break; 10922 } 10923 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10924 struct scsi_get_lba_status *cdb; 10925 10926 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10927 *lba = scsi_8btou64(cdb->addr); 10928 *len = UINT32_MAX; 10929 break; 10930 } 10931 default: 10932 return (1); 10933 break; /* NOTREACHED */ 10934 } 10935 10936 return (0); 10937} 10938 10939static ctl_action 10940ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10941 bool seq) 10942{ 10943 uint64_t endlba1, endlba2; 10944 10945 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10946 endlba2 = lba2 + len2 - 1; 10947 10948 if ((endlba1 < lba2) || (endlba2 < lba1)) 10949 return (CTL_ACTION_PASS); 10950 else 10951 return (CTL_ACTION_BLOCK); 10952} 10953 10954static int 10955ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10956{ 10957 struct ctl_ptr_len_flags *ptrlen; 10958 struct scsi_unmap_desc *buf, *end, *range; 10959 uint64_t lba; 10960 uint32_t len; 10961 10962 /* If not UNMAP -- go other way. */ 10963 if (io->io_hdr.io_type != CTL_IO_SCSI || 10964 io->scsiio.cdb[0] != UNMAP) 10965 return (CTL_ACTION_ERROR); 10966 10967 /* If UNMAP without data -- block and wait for data. */ 10968 ptrlen = (struct ctl_ptr_len_flags *) 10969 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10970 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10971 ptrlen->ptr == NULL) 10972 return (CTL_ACTION_BLOCK); 10973 10974 /* UNMAP with data -- check for collision. */ 10975 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10976 end = buf + ptrlen->len / sizeof(*buf); 10977 for (range = buf; range < end; range++) { 10978 lba = scsi_8btou64(range->lba); 10979 len = scsi_4btoul(range->length); 10980 if ((lba < lba2 + len2) && (lba + len > lba2)) 10981 return (CTL_ACTION_BLOCK); 10982 } 10983 return (CTL_ACTION_PASS); 10984} 10985 10986static ctl_action 10987ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10988{ 10989 uint64_t lba1, lba2; 10990 uint64_t len1, len2; 10991 int retval; 10992 10993 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10994 return (CTL_ACTION_ERROR); 10995 10996 retval = ctl_extent_check_unmap(io1, lba2, len2); 10997 if (retval != CTL_ACTION_ERROR) 10998 return (retval); 10999 11000 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 11001 return (CTL_ACTION_ERROR); 11002 11003 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 11004 seq = FALSE; 11005 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 11006} 11007 11008static ctl_action 11009ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 11010{ 11011 uint64_t lba1, lba2; 11012 uint64_t len1, len2; 11013 11014 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 11015 return (CTL_ACTION_PASS); 11016 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 11017 return (CTL_ACTION_ERROR); 11018 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 11019 return (CTL_ACTION_ERROR); 11020 11021 if (lba1 + len1 == lba2) 11022 return (CTL_ACTION_BLOCK); 11023 return (CTL_ACTION_PASS); 11024} 11025 11026static ctl_action 11027ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 11028 union ctl_io *ooa_io) 11029{ 11030 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 11031 const ctl_serialize_action *serialize_row; 11032 11033 /* 11034 * The initiator attempted multiple untagged commands at the same 11035 * time. Can't do that. 11036 */ 11037 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11038 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11039 && ((pending_io->io_hdr.nexus.targ_port == 11040 ooa_io->io_hdr.nexus.targ_port) 11041 && (pending_io->io_hdr.nexus.initid == 11042 ooa_io->io_hdr.nexus.initid)) 11043 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11044 CTL_FLAG_STATUS_SENT)) == 0)) 11045 return (CTL_ACTION_OVERLAP); 11046 11047 /* 11048 * The initiator attempted to send multiple tagged commands with 11049 * the same ID. (It's fine if different initiators have the same 11050 * tag ID.) 11051 * 11052 * Even if all of those conditions are true, we don't kill the I/O 11053 * if the command ahead of us has been aborted. We won't end up 11054 * sending it to the FETD, and it's perfectly legal to resend a 11055 * command with the same tag number as long as the previous 11056 * instance of this tag number has been aborted somehow. 11057 */ 11058 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11059 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11060 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11061 && ((pending_io->io_hdr.nexus.targ_port == 11062 ooa_io->io_hdr.nexus.targ_port) 11063 && (pending_io->io_hdr.nexus.initid == 11064 ooa_io->io_hdr.nexus.initid)) 11065 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11066 CTL_FLAG_STATUS_SENT)) == 0)) 11067 return (CTL_ACTION_OVERLAP_TAG); 11068 11069 /* 11070 * If we get a head of queue tag, SAM-3 says that we should 11071 * immediately execute it. 11072 * 11073 * What happens if this command would normally block for some other 11074 * reason? e.g. a request sense with a head of queue tag 11075 * immediately after a write. Normally that would block, but this 11076 * will result in its getting executed immediately... 11077 * 11078 * We currently return "pass" instead of "skip", so we'll end up 11079 * going through the rest of the queue to check for overlapped tags. 11080 * 11081 * XXX KDM check for other types of blockage first?? 11082 */ 11083 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11084 return (CTL_ACTION_PASS); 11085 11086 /* 11087 * Ordered tags have to block until all items ahead of them 11088 * have completed. If we get called with an ordered tag, we always 11089 * block, if something else is ahead of us in the queue. 11090 */ 11091 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11092 return (CTL_ACTION_BLOCK); 11093 11094 /* 11095 * Simple tags get blocked until all head of queue and ordered tags 11096 * ahead of them have completed. I'm lumping untagged commands in 11097 * with simple tags here. XXX KDM is that the right thing to do? 11098 */ 11099 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11100 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11101 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11102 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11103 return (CTL_ACTION_BLOCK); 11104 11105 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11106 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 11107 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 11108 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 11109 pending_io->scsiio.cdb[1], pending_io)); 11110 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11111 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 11112 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 11113 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 11114 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 11115 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 11116 ooa_io->scsiio.cdb[1], ooa_io)); 11117 11118 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11119 11120 switch (serialize_row[pending_entry->seridx]) { 11121 case CTL_SER_BLOCK: 11122 return (CTL_ACTION_BLOCK); 11123 case CTL_SER_EXTENT: 11124 return (ctl_extent_check(ooa_io, pending_io, 11125 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11126 case CTL_SER_EXTENTOPT: 11127 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11128 SCP_QUEUE_ALG_UNRESTRICTED) 11129 return (ctl_extent_check(ooa_io, pending_io, 11130 (lun->be_lun && 11131 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11132 return (CTL_ACTION_PASS); 11133 case CTL_SER_EXTENTSEQ: 11134 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11135 return (ctl_extent_check_seq(ooa_io, pending_io)); 11136 return (CTL_ACTION_PASS); 11137 case CTL_SER_PASS: 11138 return (CTL_ACTION_PASS); 11139 case CTL_SER_BLOCKOPT: 11140 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11141 SCP_QUEUE_ALG_UNRESTRICTED) 11142 return (CTL_ACTION_BLOCK); 11143 return (CTL_ACTION_PASS); 11144 case CTL_SER_SKIP: 11145 return (CTL_ACTION_SKIP); 11146 default: 11147 panic("%s: Invalid serialization value %d for %d => %d", 11148 __func__, serialize_row[pending_entry->seridx], 11149 pending_entry->seridx, ooa_entry->seridx); 11150 } 11151 11152 return (CTL_ACTION_ERROR); 11153} 11154 11155/* 11156 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11157 * Assumptions: 11158 * - pending_io is generally either incoming, or on the blocked queue 11159 * - starting I/O is the I/O we want to start the check with. 11160 */ 11161static ctl_action 11162ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11163 union ctl_io *starting_io) 11164{ 11165 union ctl_io *ooa_io; 11166 ctl_action action; 11167 11168 mtx_assert(&lun->lun_lock, MA_OWNED); 11169 11170 /* 11171 * Run back along the OOA queue, starting with the current 11172 * blocked I/O and going through every I/O before it on the 11173 * queue. If starting_io is NULL, we'll just end up returning 11174 * CTL_ACTION_PASS. 11175 */ 11176 for (ooa_io = starting_io; ooa_io != NULL; 11177 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11178 ooa_links)){ 11179 11180 /* 11181 * This routine just checks to see whether 11182 * cur_blocked is blocked by ooa_io, which is ahead 11183 * of it in the queue. It doesn't queue/dequeue 11184 * cur_blocked. 11185 */ 11186 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11187 switch (action) { 11188 case CTL_ACTION_BLOCK: 11189 case CTL_ACTION_OVERLAP: 11190 case CTL_ACTION_OVERLAP_TAG: 11191 case CTL_ACTION_SKIP: 11192 case CTL_ACTION_ERROR: 11193 return (action); 11194 break; /* NOTREACHED */ 11195 case CTL_ACTION_PASS: 11196 break; 11197 default: 11198 panic("%s: Invalid action %d\n", __func__, action); 11199 } 11200 } 11201 11202 return (CTL_ACTION_PASS); 11203} 11204 11205/* 11206 * Assumptions: 11207 * - An I/O has just completed, and has been removed from the per-LUN OOA 11208 * queue, so some items on the blocked queue may now be unblocked. 11209 */ 11210static int 11211ctl_check_blocked(struct ctl_lun *lun) 11212{ 11213 struct ctl_softc *softc = lun->ctl_softc; 11214 union ctl_io *cur_blocked, *next_blocked; 11215 11216 mtx_assert(&lun->lun_lock, MA_OWNED); 11217 11218 /* 11219 * Run forward from the head of the blocked queue, checking each 11220 * entry against the I/Os prior to it on the OOA queue to see if 11221 * there is still any blockage. 11222 * 11223 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11224 * with our removing a variable on it while it is traversing the 11225 * list. 11226 */ 11227 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11228 cur_blocked != NULL; cur_blocked = next_blocked) { 11229 union ctl_io *prev_ooa; 11230 ctl_action action; 11231 11232 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11233 blocked_links); 11234 11235 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11236 ctl_ooaq, ooa_links); 11237 11238 /* 11239 * If cur_blocked happens to be the first item in the OOA 11240 * queue now, prev_ooa will be NULL, and the action 11241 * returned will just be CTL_ACTION_PASS. 11242 */ 11243 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11244 11245 switch (action) { 11246 case CTL_ACTION_BLOCK: 11247 /* Nothing to do here, still blocked */ 11248 break; 11249 case CTL_ACTION_OVERLAP: 11250 case CTL_ACTION_OVERLAP_TAG: 11251 /* 11252 * This shouldn't happen! In theory we've already 11253 * checked this command for overlap... 11254 */ 11255 break; 11256 case CTL_ACTION_PASS: 11257 case CTL_ACTION_SKIP: { 11258 const struct ctl_cmd_entry *entry; 11259 11260 /* 11261 * The skip case shouldn't happen, this transaction 11262 * should have never made it onto the blocked queue. 11263 */ 11264 /* 11265 * This I/O is no longer blocked, we can remove it 11266 * from the blocked queue. Since this is a TAILQ 11267 * (doubly linked list), we can do O(1) removals 11268 * from any place on the list. 11269 */ 11270 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11271 blocked_links); 11272 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11273 11274 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11275 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11276 /* 11277 * Need to send IO back to original side to 11278 * run 11279 */ 11280 union ctl_ha_msg msg_info; 11281 11282 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11283 msg_info.hdr.original_sc = 11284 cur_blocked->io_hdr.original_sc; 11285 msg_info.hdr.serializing_sc = cur_blocked; 11286 msg_info.hdr.msg_type = CTL_MSG_R2R; 11287 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11288 sizeof(msg_info.hdr), M_NOWAIT); 11289 break; 11290 } 11291 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11292 11293 /* 11294 * Check this I/O for LUN state changes that may 11295 * have happened while this command was blocked. 11296 * The LUN state may have been changed by a command 11297 * ahead of us in the queue, so we need to re-check 11298 * for any states that can be caused by SCSI 11299 * commands. 11300 */ 11301 if (ctl_scsiio_lun_check(lun, entry, 11302 &cur_blocked->scsiio) == 0) { 11303 cur_blocked->io_hdr.flags |= 11304 CTL_FLAG_IS_WAS_ON_RTR; 11305 ctl_enqueue_rtr(cur_blocked); 11306 } else 11307 ctl_done(cur_blocked); 11308 break; 11309 } 11310 default: 11311 /* 11312 * This probably shouldn't happen -- we shouldn't 11313 * get CTL_ACTION_ERROR, or anything else. 11314 */ 11315 break; 11316 } 11317 } 11318 11319 return (CTL_RETVAL_COMPLETE); 11320} 11321 11322/* 11323 * This routine (with one exception) checks LUN flags that can be set by 11324 * commands ahead of us in the OOA queue. These flags have to be checked 11325 * when a command initially comes in, and when we pull a command off the 11326 * blocked queue and are preparing to execute it. The reason we have to 11327 * check these flags for commands on the blocked queue is that the LUN 11328 * state may have been changed by a command ahead of us while we're on the 11329 * blocked queue. 11330 * 11331 * Ordering is somewhat important with these checks, so please pay 11332 * careful attention to the placement of any new checks. 11333 */ 11334static int 11335ctl_scsiio_lun_check(struct ctl_lun *lun, 11336 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11337{ 11338 struct ctl_softc *softc = lun->ctl_softc; 11339 int retval; 11340 uint32_t residx; 11341 11342 retval = 0; 11343 11344 mtx_assert(&lun->lun_lock, MA_OWNED); 11345 11346 /* 11347 * If this shelf is a secondary shelf controller, we may have to 11348 * reject some commands disallowed by HA mode and link state. 11349 */ 11350 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11351 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11352 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11353 ctl_set_lun_unavail(ctsio); 11354 retval = 1; 11355 goto bailout; 11356 } 11357 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11358 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11359 ctl_set_lun_transit(ctsio); 11360 retval = 1; 11361 goto bailout; 11362 } 11363 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11364 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11365 ctl_set_lun_standby(ctsio); 11366 retval = 1; 11367 goto bailout; 11368 } 11369 11370 /* The rest of checks are only done on executing side */ 11371 if (softc->ha_mode == CTL_HA_MODE_XFER) 11372 goto bailout; 11373 } 11374 11375 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11376 if (lun->be_lun && 11377 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11378 ctl_set_hw_write_protected(ctsio); 11379 retval = 1; 11380 goto bailout; 11381 } 11382 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11383 ctl_set_sense(ctsio, /*current_error*/ 1, 11384 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11385 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11386 retval = 1; 11387 goto bailout; 11388 } 11389 } 11390 11391 /* 11392 * Check for a reservation conflict. If this command isn't allowed 11393 * even on reserved LUNs, and if this initiator isn't the one who 11394 * reserved us, reject the command with a reservation conflict. 11395 */ 11396 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11397 if ((lun->flags & CTL_LUN_RESERVED) 11398 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11399 if (lun->res_idx != residx) { 11400 ctl_set_reservation_conflict(ctsio); 11401 retval = 1; 11402 goto bailout; 11403 } 11404 } 11405 11406 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11407 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11408 /* No reservation or command is allowed. */; 11409 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11410 (lun->pr_res_type == SPR_TYPE_WR_EX || 11411 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11412 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11413 /* The command is allowed for Write Exclusive resv. */; 11414 } else { 11415 /* 11416 * if we aren't registered or it's a res holder type 11417 * reservation and this isn't the res holder then set a 11418 * conflict. 11419 */ 11420 if (ctl_get_prkey(lun, residx) == 0 || 11421 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11422 ctl_set_reservation_conflict(ctsio); 11423 retval = 1; 11424 goto bailout; 11425 } 11426 } 11427 11428 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11429 if (lun->flags & CTL_LUN_EJECTED) 11430 ctl_set_lun_ejected(ctsio); 11431 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11432 if (lun->flags & CTL_LUN_REMOVABLE) 11433 ctl_set_lun_no_media(ctsio); 11434 else 11435 ctl_set_lun_int_reqd(ctsio); 11436 } else if (lun->flags & CTL_LUN_STOPPED) 11437 ctl_set_lun_stopped(ctsio); 11438 else 11439 goto bailout; 11440 retval = 1; 11441 goto bailout; 11442 } 11443 11444bailout: 11445 return (retval); 11446} 11447 11448static void 11449ctl_failover_io(union ctl_io *io, int have_lock) 11450{ 11451 ctl_set_busy(&io->scsiio); 11452 ctl_done(io); 11453} 11454 11455static void 11456ctl_failover_lun(union ctl_io *rio) 11457{ 11458 struct ctl_softc *softc = control_softc; 11459 struct ctl_lun *lun; 11460 struct ctl_io_hdr *io, *next_io; 11461 uint32_t targ_lun; 11462 11463 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11464 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11465 11466 /* Find and lock the LUN. */ 11467 mtx_lock(&softc->ctl_lock); 11468 if (targ_lun > CTL_MAX_LUNS || 11469 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11470 mtx_unlock(&softc->ctl_lock); 11471 return; 11472 } 11473 mtx_lock(&lun->lun_lock); 11474 mtx_unlock(&softc->ctl_lock); 11475 if (lun->flags & CTL_LUN_DISABLED) { 11476 mtx_unlock(&lun->lun_lock); 11477 return; 11478 } 11479 11480 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11481 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11482 /* We are master */ 11483 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11484 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11485 io->flags |= CTL_FLAG_ABORT; 11486 io->flags |= CTL_FLAG_FAILOVER; 11487 } else { /* This can be only due to DATAMOVE */ 11488 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11489 io->flags &= ~CTL_FLAG_DMA_INPROG; 11490 io->flags |= CTL_FLAG_IO_ACTIVE; 11491 io->port_status = 31340; 11492 ctl_enqueue_isc((union ctl_io *)io); 11493 } 11494 } 11495 /* We are slave */ 11496 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11497 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11498 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11499 io->flags |= CTL_FLAG_FAILOVER; 11500 } else { 11501 ctl_set_busy(&((union ctl_io *)io)-> 11502 scsiio); 11503 ctl_done((union ctl_io *)io); 11504 } 11505 } 11506 } 11507 } else { /* SERIALIZE modes */ 11508 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11509 next_io) { 11510 /* We are master */ 11511 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11512 TAILQ_REMOVE(&lun->blocked_queue, io, 11513 blocked_links); 11514 io->flags &= ~CTL_FLAG_BLOCKED; 11515 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11516 ctl_free_io((union ctl_io *)io); 11517 } 11518 } 11519 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11520 /* We are master */ 11521 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11522 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11523 ctl_free_io((union ctl_io *)io); 11524 } 11525 /* We are slave */ 11526 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11527 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11528 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11529 ctl_set_busy(&((union ctl_io *)io)-> 11530 scsiio); 11531 ctl_done((union ctl_io *)io); 11532 } 11533 } 11534 } 11535 ctl_check_blocked(lun); 11536 } 11537 mtx_unlock(&lun->lun_lock); 11538} 11539 11540static int 11541ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11542{ 11543 struct ctl_lun *lun; 11544 const struct ctl_cmd_entry *entry; 11545 uint32_t initidx, targ_lun; 11546 int retval = 0; 11547 11548 lun = NULL; 11549 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11550 if (targ_lun < CTL_MAX_LUNS) 11551 lun = softc->ctl_luns[targ_lun]; 11552 if (lun) { 11553 /* 11554 * If the LUN is invalid, pretend that it doesn't exist. 11555 * It will go away as soon as all pending I/O has been 11556 * completed. 11557 */ 11558 mtx_lock(&lun->lun_lock); 11559 if (lun->flags & CTL_LUN_DISABLED) { 11560 mtx_unlock(&lun->lun_lock); 11561 lun = NULL; 11562 } 11563 } 11564 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11565 if (lun) { 11566 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11567 lun->be_lun; 11568 11569 /* 11570 * Every I/O goes into the OOA queue for a particular LUN, 11571 * and stays there until completion. 11572 */ 11573#ifdef CTL_TIME_IO 11574 if (TAILQ_EMPTY(&lun->ooa_queue)) 11575 lun->idle_time += getsbinuptime() - lun->last_busy; 11576#endif 11577 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11578 } 11579 11580 /* Get command entry and return error if it is unsuppotyed. */ 11581 entry = ctl_validate_command(ctsio); 11582 if (entry == NULL) { 11583 if (lun) 11584 mtx_unlock(&lun->lun_lock); 11585 return (retval); 11586 } 11587 11588 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11589 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11590 11591 /* 11592 * Check to see whether we can send this command to LUNs that don't 11593 * exist. This should pretty much only be the case for inquiry 11594 * and request sense. Further checks, below, really require having 11595 * a LUN, so we can't really check the command anymore. Just put 11596 * it on the rtr queue. 11597 */ 11598 if (lun == NULL) { 11599 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11600 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11601 ctl_enqueue_rtr((union ctl_io *)ctsio); 11602 return (retval); 11603 } 11604 11605 ctl_set_unsupported_lun(ctsio); 11606 ctl_done((union ctl_io *)ctsio); 11607 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11608 return (retval); 11609 } else { 11610 /* 11611 * Make sure we support this particular command on this LUN. 11612 * e.g., we don't support writes to the control LUN. 11613 */ 11614 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11615 mtx_unlock(&lun->lun_lock); 11616 ctl_set_invalid_opcode(ctsio); 11617 ctl_done((union ctl_io *)ctsio); 11618 return (retval); 11619 } 11620 } 11621 11622 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11623 11624#ifdef CTL_WITH_CA 11625 /* 11626 * If we've got a request sense, it'll clear the contingent 11627 * allegiance condition. Otherwise, if we have a CA condition for 11628 * this initiator, clear it, because it sent down a command other 11629 * than request sense. 11630 */ 11631 if ((ctsio->cdb[0] != REQUEST_SENSE) 11632 && (ctl_is_set(lun->have_ca, initidx))) 11633 ctl_clear_mask(lun->have_ca, initidx); 11634#endif 11635 11636 /* 11637 * If the command has this flag set, it handles its own unit 11638 * attention reporting, we shouldn't do anything. Otherwise we 11639 * check for any pending unit attentions, and send them back to the 11640 * initiator. We only do this when a command initially comes in, 11641 * not when we pull it off the blocked queue. 11642 * 11643 * According to SAM-3, section 5.3.2, the order that things get 11644 * presented back to the host is basically unit attentions caused 11645 * by some sort of reset event, busy status, reservation conflicts 11646 * or task set full, and finally any other status. 11647 * 11648 * One issue here is that some of the unit attentions we report 11649 * don't fall into the "reset" category (e.g. "reported luns data 11650 * has changed"). So reporting it here, before the reservation 11651 * check, may be technically wrong. I guess the only thing to do 11652 * would be to check for and report the reset events here, and then 11653 * check for the other unit attention types after we check for a 11654 * reservation conflict. 11655 * 11656 * XXX KDM need to fix this 11657 */ 11658 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11659 ctl_ua_type ua_type; 11660 u_int sense_len = 0; 11661 11662 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11663 &sense_len, SSD_TYPE_NONE); 11664 if (ua_type != CTL_UA_NONE) { 11665 mtx_unlock(&lun->lun_lock); 11666 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11667 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11668 ctsio->sense_len = sense_len; 11669 ctl_done((union ctl_io *)ctsio); 11670 return (retval); 11671 } 11672 } 11673 11674 11675 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11676 mtx_unlock(&lun->lun_lock); 11677 ctl_done((union ctl_io *)ctsio); 11678 return (retval); 11679 } 11680 11681 /* 11682 * XXX CHD this is where we want to send IO to other side if 11683 * this LUN is secondary on this SC. We will need to make a copy 11684 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11685 * the copy we send as FROM_OTHER. 11686 * We also need to stuff the address of the original IO so we can 11687 * find it easily. Something similar will need be done on the other 11688 * side so when we are done we can find the copy. 11689 */ 11690 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11691 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11692 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11693 union ctl_ha_msg msg_info; 11694 int isc_retval; 11695 11696 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11697 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11698 mtx_unlock(&lun->lun_lock); 11699 11700 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11701 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11702 msg_info.hdr.serializing_sc = NULL; 11703 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11704 msg_info.scsi.tag_num = ctsio->tag_num; 11705 msg_info.scsi.tag_type = ctsio->tag_type; 11706 msg_info.scsi.cdb_len = ctsio->cdb_len; 11707 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11708 11709 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11710 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11711 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11712 ctl_set_busy(ctsio); 11713 ctl_done((union ctl_io *)ctsio); 11714 return (retval); 11715 } 11716 return (retval); 11717 } 11718 11719 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11720 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11721 ctl_ooaq, ooa_links))) { 11722 case CTL_ACTION_BLOCK: 11723 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11724 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11725 blocked_links); 11726 mtx_unlock(&lun->lun_lock); 11727 return (retval); 11728 case CTL_ACTION_PASS: 11729 case CTL_ACTION_SKIP: 11730 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11731 mtx_unlock(&lun->lun_lock); 11732 ctl_enqueue_rtr((union ctl_io *)ctsio); 11733 break; 11734 case CTL_ACTION_OVERLAP: 11735 mtx_unlock(&lun->lun_lock); 11736 ctl_set_overlapped_cmd(ctsio); 11737 ctl_done((union ctl_io *)ctsio); 11738 break; 11739 case CTL_ACTION_OVERLAP_TAG: 11740 mtx_unlock(&lun->lun_lock); 11741 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11742 ctl_done((union ctl_io *)ctsio); 11743 break; 11744 case CTL_ACTION_ERROR: 11745 default: 11746 mtx_unlock(&lun->lun_lock); 11747 ctl_set_internal_failure(ctsio, 11748 /*sks_valid*/ 0, 11749 /*retry_count*/ 0); 11750 ctl_done((union ctl_io *)ctsio); 11751 break; 11752 } 11753 return (retval); 11754} 11755 11756const struct ctl_cmd_entry * 11757ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11758{ 11759 const struct ctl_cmd_entry *entry; 11760 int service_action; 11761 11762 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11763 if (sa) 11764 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11765 if (entry->flags & CTL_CMD_FLAG_SA5) { 11766 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11767 entry = &((const struct ctl_cmd_entry *) 11768 entry->execute)[service_action]; 11769 } 11770 return (entry); 11771} 11772 11773const struct ctl_cmd_entry * 11774ctl_validate_command(struct ctl_scsiio *ctsio) 11775{ 11776 const struct ctl_cmd_entry *entry; 11777 int i, sa; 11778 uint8_t diff; 11779 11780 entry = ctl_get_cmd_entry(ctsio, &sa); 11781 if (entry->execute == NULL) { 11782 if (sa) 11783 ctl_set_invalid_field(ctsio, 11784 /*sks_valid*/ 1, 11785 /*command*/ 1, 11786 /*field*/ 1, 11787 /*bit_valid*/ 1, 11788 /*bit*/ 4); 11789 else 11790 ctl_set_invalid_opcode(ctsio); 11791 ctl_done((union ctl_io *)ctsio); 11792 return (NULL); 11793 } 11794 KASSERT(entry->length > 0, 11795 ("Not defined length for command 0x%02x/0x%02x", 11796 ctsio->cdb[0], ctsio->cdb[1])); 11797 for (i = 1; i < entry->length; i++) { 11798 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11799 if (diff == 0) 11800 continue; 11801 ctl_set_invalid_field(ctsio, 11802 /*sks_valid*/ 1, 11803 /*command*/ 1, 11804 /*field*/ i, 11805 /*bit_valid*/ 1, 11806 /*bit*/ fls(diff) - 1); 11807 ctl_done((union ctl_io *)ctsio); 11808 return (NULL); 11809 } 11810 return (entry); 11811} 11812 11813static int 11814ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11815{ 11816 11817 switch (lun_type) { 11818 case T_DIRECT: 11819 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11820 return (0); 11821 break; 11822 case T_PROCESSOR: 11823 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11824 return (0); 11825 break; 11826 case T_CDROM: 11827 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11828 return (0); 11829 break; 11830 default: 11831 return (0); 11832 } 11833 return (1); 11834} 11835 11836static int 11837ctl_scsiio(struct ctl_scsiio *ctsio) 11838{ 11839 int retval; 11840 const struct ctl_cmd_entry *entry; 11841 11842 retval = CTL_RETVAL_COMPLETE; 11843 11844 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11845 11846 entry = ctl_get_cmd_entry(ctsio, NULL); 11847 11848 /* 11849 * If this I/O has been aborted, just send it straight to 11850 * ctl_done() without executing it. 11851 */ 11852 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11853 ctl_done((union ctl_io *)ctsio); 11854 goto bailout; 11855 } 11856 11857 /* 11858 * All the checks should have been handled by ctl_scsiio_precheck(). 11859 * We should be clear now to just execute the I/O. 11860 */ 11861 retval = entry->execute(ctsio); 11862 11863bailout: 11864 return (retval); 11865} 11866 11867/* 11868 * Since we only implement one target right now, a bus reset simply resets 11869 * our single target. 11870 */ 11871static int 11872ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11873{ 11874 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11875} 11876 11877static int 11878ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11879 ctl_ua_type ua_type) 11880{ 11881 struct ctl_port *port; 11882 struct ctl_lun *lun; 11883 int retval; 11884 11885 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11886 union ctl_ha_msg msg_info; 11887 11888 msg_info.hdr.nexus = io->io_hdr.nexus; 11889 if (ua_type==CTL_UA_TARG_RESET) 11890 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11891 else 11892 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11893 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11894 msg_info.hdr.original_sc = NULL; 11895 msg_info.hdr.serializing_sc = NULL; 11896 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11897 sizeof(msg_info.task), M_WAITOK); 11898 } 11899 retval = 0; 11900 11901 mtx_lock(&softc->ctl_lock); 11902 port = ctl_io_port(&io->io_hdr); 11903 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11904 if (port != NULL && 11905 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11906 continue; 11907 retval += ctl_do_lun_reset(lun, io, ua_type); 11908 } 11909 mtx_unlock(&softc->ctl_lock); 11910 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11911 return (retval); 11912} 11913 11914/* 11915 * The LUN should always be set. The I/O is optional, and is used to 11916 * distinguish between I/Os sent by this initiator, and by other 11917 * initiators. We set unit attention for initiators other than this one. 11918 * SAM-3 is vague on this point. It does say that a unit attention should 11919 * be established for other initiators when a LUN is reset (see section 11920 * 5.7.3), but it doesn't specifically say that the unit attention should 11921 * be established for this particular initiator when a LUN is reset. Here 11922 * is the relevant text, from SAM-3 rev 8: 11923 * 11924 * 5.7.2 When a SCSI initiator port aborts its own tasks 11925 * 11926 * When a SCSI initiator port causes its own task(s) to be aborted, no 11927 * notification that the task(s) have been aborted shall be returned to 11928 * the SCSI initiator port other than the completion response for the 11929 * command or task management function action that caused the task(s) to 11930 * be aborted and notification(s) associated with related effects of the 11931 * action (e.g., a reset unit attention condition). 11932 * 11933 * XXX KDM for now, we're setting unit attention for all initiators. 11934 */ 11935static int 11936ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11937{ 11938 union ctl_io *xio; 11939#if 0 11940 uint32_t initidx; 11941#endif 11942 int i; 11943 11944 mtx_lock(&lun->lun_lock); 11945 /* 11946 * Run through the OOA queue and abort each I/O. 11947 */ 11948 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11949 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11950 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11951 } 11952 11953 /* 11954 * This version sets unit attention for every 11955 */ 11956#if 0 11957 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11958 ctl_est_ua_all(lun, initidx, ua_type); 11959#else 11960 ctl_est_ua_all(lun, -1, ua_type); 11961#endif 11962 11963 /* 11964 * A reset (any kind, really) clears reservations established with 11965 * RESERVE/RELEASE. It does not clear reservations established 11966 * with PERSISTENT RESERVE OUT, but we don't support that at the 11967 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11968 * reservations made with the RESERVE/RELEASE commands, because 11969 * those commands are obsolete in SPC-3. 11970 */ 11971 lun->flags &= ~CTL_LUN_RESERVED; 11972 11973#ifdef CTL_WITH_CA 11974 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11975 ctl_clear_mask(lun->have_ca, i); 11976#endif 11977 lun->prevent_count = 0; 11978 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11979 ctl_clear_mask(lun->prevent, i); 11980 mtx_unlock(&lun->lun_lock); 11981 11982 return (0); 11983} 11984 11985static int 11986ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11987{ 11988 struct ctl_lun *lun; 11989 uint32_t targ_lun; 11990 int retval; 11991 11992 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11993 mtx_lock(&softc->ctl_lock); 11994 if (targ_lun >= CTL_MAX_LUNS || 11995 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11996 mtx_unlock(&softc->ctl_lock); 11997 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11998 return (1); 11999 } 12000 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 12001 mtx_unlock(&softc->ctl_lock); 12002 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12003 12004 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 12005 union ctl_ha_msg msg_info; 12006 12007 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12008 msg_info.hdr.nexus = io->io_hdr.nexus; 12009 msg_info.task.task_action = CTL_TASK_LUN_RESET; 12010 msg_info.hdr.original_sc = NULL; 12011 msg_info.hdr.serializing_sc = NULL; 12012 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12013 sizeof(msg_info.task), M_WAITOK); 12014 } 12015 return (retval); 12016} 12017 12018static void 12019ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12020 int other_sc) 12021{ 12022 union ctl_io *xio; 12023 12024 mtx_assert(&lun->lun_lock, MA_OWNED); 12025 12026 /* 12027 * Run through the OOA queue and attempt to find the given I/O. 12028 * The target port, initiator ID, tag type and tag number have to 12029 * match the values that we got from the initiator. If we have an 12030 * untagged command to abort, simply abort the first untagged command 12031 * we come to. We only allow one untagged command at a time of course. 12032 */ 12033 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12034 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12035 12036 if ((targ_port == UINT32_MAX || 12037 targ_port == xio->io_hdr.nexus.targ_port) && 12038 (init_id == UINT32_MAX || 12039 init_id == xio->io_hdr.nexus.initid)) { 12040 if (targ_port != xio->io_hdr.nexus.targ_port || 12041 init_id != xio->io_hdr.nexus.initid) 12042 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12043 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12044 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12045 union ctl_ha_msg msg_info; 12046 12047 msg_info.hdr.nexus = xio->io_hdr.nexus; 12048 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12049 msg_info.task.tag_num = xio->scsiio.tag_num; 12050 msg_info.task.tag_type = xio->scsiio.tag_type; 12051 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12052 msg_info.hdr.original_sc = NULL; 12053 msg_info.hdr.serializing_sc = NULL; 12054 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12055 sizeof(msg_info.task), M_NOWAIT); 12056 } 12057 } 12058 } 12059} 12060 12061static int 12062ctl_abort_task_set(union ctl_io *io) 12063{ 12064 struct ctl_softc *softc = control_softc; 12065 struct ctl_lun *lun; 12066 uint32_t targ_lun; 12067 12068 /* 12069 * Look up the LUN. 12070 */ 12071 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12072 mtx_lock(&softc->ctl_lock); 12073 if (targ_lun >= CTL_MAX_LUNS || 12074 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12075 mtx_unlock(&softc->ctl_lock); 12076 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12077 return (1); 12078 } 12079 12080 mtx_lock(&lun->lun_lock); 12081 mtx_unlock(&softc->ctl_lock); 12082 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12083 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12084 io->io_hdr.nexus.initid, 12085 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12086 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12087 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12088 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12089 } 12090 mtx_unlock(&lun->lun_lock); 12091 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12092 return (0); 12093} 12094 12095static int 12096ctl_i_t_nexus_reset(union ctl_io *io) 12097{ 12098 struct ctl_softc *softc = control_softc; 12099 struct ctl_lun *lun; 12100 uint32_t initidx; 12101 12102 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12103 union ctl_ha_msg msg_info; 12104 12105 msg_info.hdr.nexus = io->io_hdr.nexus; 12106 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12107 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12108 msg_info.hdr.original_sc = NULL; 12109 msg_info.hdr.serializing_sc = NULL; 12110 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12111 sizeof(msg_info.task), M_WAITOK); 12112 } 12113 12114 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12115 mtx_lock(&softc->ctl_lock); 12116 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12117 mtx_lock(&lun->lun_lock); 12118 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12119 io->io_hdr.nexus.initid, 1); 12120#ifdef CTL_WITH_CA 12121 ctl_clear_mask(lun->have_ca, initidx); 12122#endif 12123 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12124 lun->flags &= ~CTL_LUN_RESERVED; 12125 if (ctl_is_set(lun->prevent, initidx)) { 12126 ctl_clear_mask(lun->prevent, initidx); 12127 lun->prevent_count--; 12128 } 12129 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12130 mtx_unlock(&lun->lun_lock); 12131 } 12132 mtx_unlock(&softc->ctl_lock); 12133 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12134 return (0); 12135} 12136 12137static int 12138ctl_abort_task(union ctl_io *io) 12139{ 12140 union ctl_io *xio; 12141 struct ctl_lun *lun; 12142 struct ctl_softc *softc; 12143#if 0 12144 struct sbuf sb; 12145 char printbuf[128]; 12146#endif 12147 int found; 12148 uint32_t targ_lun; 12149 12150 softc = control_softc; 12151 found = 0; 12152 12153 /* 12154 * Look up the LUN. 12155 */ 12156 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12157 mtx_lock(&softc->ctl_lock); 12158 if (targ_lun >= CTL_MAX_LUNS || 12159 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12160 mtx_unlock(&softc->ctl_lock); 12161 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12162 return (1); 12163 } 12164 12165#if 0 12166 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12167 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12168#endif 12169 12170 mtx_lock(&lun->lun_lock); 12171 mtx_unlock(&softc->ctl_lock); 12172 /* 12173 * Run through the OOA queue and attempt to find the given I/O. 12174 * The target port, initiator ID, tag type and tag number have to 12175 * match the values that we got from the initiator. If we have an 12176 * untagged command to abort, simply abort the first untagged command 12177 * we come to. We only allow one untagged command at a time of course. 12178 */ 12179 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12180 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12181#if 0 12182 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12183 12184 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12185 lun->lun, xio->scsiio.tag_num, 12186 xio->scsiio.tag_type, 12187 (xio->io_hdr.blocked_links.tqe_prev 12188 == NULL) ? "" : " BLOCKED", 12189 (xio->io_hdr.flags & 12190 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12191 (xio->io_hdr.flags & 12192 CTL_FLAG_ABORT) ? " ABORT" : "", 12193 (xio->io_hdr.flags & 12194 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12195 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12196 sbuf_finish(&sb); 12197 printf("%s\n", sbuf_data(&sb)); 12198#endif 12199 12200 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12201 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12202 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12203 continue; 12204 12205 /* 12206 * If the abort says that the task is untagged, the 12207 * task in the queue must be untagged. Otherwise, 12208 * we just check to see whether the tag numbers 12209 * match. This is because the QLogic firmware 12210 * doesn't pass back the tag type in an abort 12211 * request. 12212 */ 12213#if 0 12214 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12215 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12216 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12217#endif 12218 /* 12219 * XXX KDM we've got problems with FC, because it 12220 * doesn't send down a tag type with aborts. So we 12221 * can only really go by the tag number... 12222 * This may cause problems with parallel SCSI. 12223 * Need to figure that out!! 12224 */ 12225 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12226 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12227 found = 1; 12228 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12229 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12230 union ctl_ha_msg msg_info; 12231 12232 msg_info.hdr.nexus = io->io_hdr.nexus; 12233 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12234 msg_info.task.tag_num = io->taskio.tag_num; 12235 msg_info.task.tag_type = io->taskio.tag_type; 12236 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12237 msg_info.hdr.original_sc = NULL; 12238 msg_info.hdr.serializing_sc = NULL; 12239#if 0 12240 printf("Sent Abort to other side\n"); 12241#endif 12242 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12243 sizeof(msg_info.task), M_NOWAIT); 12244 } 12245#if 0 12246 printf("ctl_abort_task: found I/O to abort\n"); 12247#endif 12248 } 12249 } 12250 mtx_unlock(&lun->lun_lock); 12251 12252 if (found == 0) { 12253 /* 12254 * This isn't really an error. It's entirely possible for 12255 * the abort and command completion to cross on the wire. 12256 * This is more of an informative/diagnostic error. 12257 */ 12258#if 0 12259 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12260 "%u:%u:%u tag %d type %d\n", 12261 io->io_hdr.nexus.initid, 12262 io->io_hdr.nexus.targ_port, 12263 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12264 io->taskio.tag_type); 12265#endif 12266 } 12267 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12268 return (0); 12269} 12270 12271static int 12272ctl_query_task(union ctl_io *io, int task_set) 12273{ 12274 union ctl_io *xio; 12275 struct ctl_lun *lun; 12276 struct ctl_softc *softc; 12277 int found = 0; 12278 uint32_t targ_lun; 12279 12280 softc = control_softc; 12281 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12282 mtx_lock(&softc->ctl_lock); 12283 if (targ_lun >= CTL_MAX_LUNS || 12284 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12285 mtx_unlock(&softc->ctl_lock); 12286 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12287 return (1); 12288 } 12289 mtx_lock(&lun->lun_lock); 12290 mtx_unlock(&softc->ctl_lock); 12291 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12292 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12293 12294 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12295 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12296 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12297 continue; 12298 12299 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12300 found = 1; 12301 break; 12302 } 12303 } 12304 mtx_unlock(&lun->lun_lock); 12305 if (found) 12306 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12307 else 12308 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12309 return (0); 12310} 12311 12312static int 12313ctl_query_async_event(union ctl_io *io) 12314{ 12315 struct ctl_lun *lun; 12316 struct ctl_softc *softc; 12317 ctl_ua_type ua; 12318 uint32_t targ_lun, initidx; 12319 12320 softc = control_softc; 12321 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12322 mtx_lock(&softc->ctl_lock); 12323 if (targ_lun >= CTL_MAX_LUNS || 12324 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12325 mtx_unlock(&softc->ctl_lock); 12326 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12327 return (1); 12328 } 12329 mtx_lock(&lun->lun_lock); 12330 mtx_unlock(&softc->ctl_lock); 12331 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12332 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12333 mtx_unlock(&lun->lun_lock); 12334 if (ua != CTL_UA_NONE) 12335 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12336 else 12337 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12338 return (0); 12339} 12340 12341static void 12342ctl_run_task(union ctl_io *io) 12343{ 12344 struct ctl_softc *softc = control_softc; 12345 int retval = 1; 12346 12347 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12348 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12349 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12350 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12351 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12352 switch (io->taskio.task_action) { 12353 case CTL_TASK_ABORT_TASK: 12354 retval = ctl_abort_task(io); 12355 break; 12356 case CTL_TASK_ABORT_TASK_SET: 12357 case CTL_TASK_CLEAR_TASK_SET: 12358 retval = ctl_abort_task_set(io); 12359 break; 12360 case CTL_TASK_CLEAR_ACA: 12361 break; 12362 case CTL_TASK_I_T_NEXUS_RESET: 12363 retval = ctl_i_t_nexus_reset(io); 12364 break; 12365 case CTL_TASK_LUN_RESET: 12366 retval = ctl_lun_reset(softc, io); 12367 break; 12368 case CTL_TASK_TARGET_RESET: 12369 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12370 break; 12371 case CTL_TASK_BUS_RESET: 12372 retval = ctl_bus_reset(softc, io); 12373 break; 12374 case CTL_TASK_PORT_LOGIN: 12375 break; 12376 case CTL_TASK_PORT_LOGOUT: 12377 break; 12378 case CTL_TASK_QUERY_TASK: 12379 retval = ctl_query_task(io, 0); 12380 break; 12381 case CTL_TASK_QUERY_TASK_SET: 12382 retval = ctl_query_task(io, 1); 12383 break; 12384 case CTL_TASK_QUERY_ASYNC_EVENT: 12385 retval = ctl_query_async_event(io); 12386 break; 12387 default: 12388 printf("%s: got unknown task management event %d\n", 12389 __func__, io->taskio.task_action); 12390 break; 12391 } 12392 if (retval == 0) 12393 io->io_hdr.status = CTL_SUCCESS; 12394 else 12395 io->io_hdr.status = CTL_ERROR; 12396 ctl_done(io); 12397} 12398 12399/* 12400 * For HA operation. Handle commands that come in from the other 12401 * controller. 12402 */ 12403static void 12404ctl_handle_isc(union ctl_io *io) 12405{ 12406 struct ctl_softc *softc = control_softc; 12407 struct ctl_lun *lun; 12408 const struct ctl_cmd_entry *entry; 12409 uint32_t targ_lun; 12410 12411 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12412 switch (io->io_hdr.msg_type) { 12413 case CTL_MSG_SERIALIZE: 12414 ctl_serialize_other_sc_cmd(&io->scsiio); 12415 break; 12416 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12417 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12418 if (targ_lun >= CTL_MAX_LUNS || 12419 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12420 ctl_done(io); 12421 break; 12422 } 12423 mtx_lock(&lun->lun_lock); 12424 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12425 mtx_unlock(&lun->lun_lock); 12426 ctl_done(io); 12427 break; 12428 } 12429 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12430 mtx_unlock(&lun->lun_lock); 12431 ctl_enqueue_rtr(io); 12432 break; 12433 case CTL_MSG_FINISH_IO: 12434 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12435 ctl_done(io); 12436 break; 12437 } 12438 if (targ_lun >= CTL_MAX_LUNS || 12439 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12440 ctl_free_io(io); 12441 break; 12442 } 12443 mtx_lock(&lun->lun_lock); 12444 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12445 ctl_check_blocked(lun); 12446 mtx_unlock(&lun->lun_lock); 12447 ctl_free_io(io); 12448 break; 12449 case CTL_MSG_PERS_ACTION: 12450 ctl_hndl_per_res_out_on_other_sc( 12451 (union ctl_ha_msg *)&io->presio.pr_msg); 12452 ctl_free_io(io); 12453 break; 12454 case CTL_MSG_BAD_JUJU: 12455 ctl_done(io); 12456 break; 12457 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12458 ctl_datamove_remote(io); 12459 break; 12460 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12461 io->scsiio.be_move_done(io); 12462 break; 12463 case CTL_MSG_FAILOVER: 12464 ctl_failover_lun(io); 12465 ctl_free_io(io); 12466 break; 12467 default: 12468 printf("%s: Invalid message type %d\n", 12469 __func__, io->io_hdr.msg_type); 12470 ctl_free_io(io); 12471 break; 12472 } 12473 12474} 12475 12476 12477/* 12478 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12479 * there is no match. 12480 */ 12481static ctl_lun_error_pattern 12482ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12483{ 12484 const struct ctl_cmd_entry *entry; 12485 ctl_lun_error_pattern filtered_pattern, pattern; 12486 12487 pattern = desc->error_pattern; 12488 12489 /* 12490 * XXX KDM we need more data passed into this function to match a 12491 * custom pattern, and we actually need to implement custom pattern 12492 * matching. 12493 */ 12494 if (pattern & CTL_LUN_PAT_CMD) 12495 return (CTL_LUN_PAT_CMD); 12496 12497 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12498 return (CTL_LUN_PAT_ANY); 12499 12500 entry = ctl_get_cmd_entry(ctsio, NULL); 12501 12502 filtered_pattern = entry->pattern & pattern; 12503 12504 /* 12505 * If the user requested specific flags in the pattern (e.g. 12506 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12507 * flags. 12508 * 12509 * If the user did not specify any flags, it doesn't matter whether 12510 * or not the command supports the flags. 12511 */ 12512 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12513 (pattern & ~CTL_LUN_PAT_MASK)) 12514 return (CTL_LUN_PAT_NONE); 12515 12516 /* 12517 * If the user asked for a range check, see if the requested LBA 12518 * range overlaps with this command's LBA range. 12519 */ 12520 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12521 uint64_t lba1; 12522 uint64_t len1; 12523 ctl_action action; 12524 int retval; 12525 12526 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12527 if (retval != 0) 12528 return (CTL_LUN_PAT_NONE); 12529 12530 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12531 desc->lba_range.len, FALSE); 12532 /* 12533 * A "pass" means that the LBA ranges don't overlap, so 12534 * this doesn't match the user's range criteria. 12535 */ 12536 if (action == CTL_ACTION_PASS) 12537 return (CTL_LUN_PAT_NONE); 12538 } 12539 12540 return (filtered_pattern); 12541} 12542 12543static void 12544ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12545{ 12546 struct ctl_error_desc *desc, *desc2; 12547 12548 mtx_assert(&lun->lun_lock, MA_OWNED); 12549 12550 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12551 ctl_lun_error_pattern pattern; 12552 /* 12553 * Check to see whether this particular command matches 12554 * the pattern in the descriptor. 12555 */ 12556 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12557 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12558 continue; 12559 12560 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12561 case CTL_LUN_INJ_ABORTED: 12562 ctl_set_aborted(&io->scsiio); 12563 break; 12564 case CTL_LUN_INJ_MEDIUM_ERR: 12565 ctl_set_medium_error(&io->scsiio, 12566 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12567 CTL_FLAG_DATA_OUT); 12568 break; 12569 case CTL_LUN_INJ_UA: 12570 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12571 * OCCURRED */ 12572 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12573 break; 12574 case CTL_LUN_INJ_CUSTOM: 12575 /* 12576 * We're assuming the user knows what he is doing. 12577 * Just copy the sense information without doing 12578 * checks. 12579 */ 12580 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12581 MIN(sizeof(desc->custom_sense), 12582 sizeof(io->scsiio.sense_data))); 12583 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12584 io->scsiio.sense_len = SSD_FULL_SIZE; 12585 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12586 break; 12587 case CTL_LUN_INJ_NONE: 12588 default: 12589 /* 12590 * If this is an error injection type we don't know 12591 * about, clear the continuous flag (if it is set) 12592 * so it will get deleted below. 12593 */ 12594 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12595 break; 12596 } 12597 /* 12598 * By default, each error injection action is a one-shot 12599 */ 12600 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12601 continue; 12602 12603 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12604 12605 free(desc, M_CTL); 12606 } 12607} 12608 12609#ifdef CTL_IO_DELAY 12610static void 12611ctl_datamove_timer_wakeup(void *arg) 12612{ 12613 union ctl_io *io; 12614 12615 io = (union ctl_io *)arg; 12616 12617 ctl_datamove(io); 12618} 12619#endif /* CTL_IO_DELAY */ 12620 12621void 12622ctl_datamove(union ctl_io *io) 12623{ 12624 struct ctl_lun *lun; 12625 void (*fe_datamove)(union ctl_io *io); 12626 12627 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12628 12629 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12630 12631 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12632#ifdef CTL_TIME_IO 12633 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12634 char str[256]; 12635 char path_str[64]; 12636 struct sbuf sb; 12637 12638 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12639 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12640 12641 sbuf_cat(&sb, path_str); 12642 switch (io->io_hdr.io_type) { 12643 case CTL_IO_SCSI: 12644 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12645 sbuf_printf(&sb, "\n"); 12646 sbuf_cat(&sb, path_str); 12647 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12648 io->scsiio.tag_num, io->scsiio.tag_type); 12649 break; 12650 case CTL_IO_TASK: 12651 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12652 "Tag Type: %d\n", io->taskio.task_action, 12653 io->taskio.tag_num, io->taskio.tag_type); 12654 break; 12655 default: 12656 panic("%s: Invalid CTL I/O type %d\n", 12657 __func__, io->io_hdr.io_type); 12658 } 12659 sbuf_cat(&sb, path_str); 12660 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12661 (intmax_t)time_uptime - io->io_hdr.start_time); 12662 sbuf_finish(&sb); 12663 printf("%s", sbuf_data(&sb)); 12664 } 12665#endif /* CTL_TIME_IO */ 12666 12667#ifdef CTL_IO_DELAY 12668 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12669 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12670 } else { 12671 if ((lun != NULL) 12672 && (lun->delay_info.datamove_delay > 0)) { 12673 12674 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12675 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12676 callout_reset(&io->io_hdr.delay_callout, 12677 lun->delay_info.datamove_delay * hz, 12678 ctl_datamove_timer_wakeup, io); 12679 if (lun->delay_info.datamove_type == 12680 CTL_DELAY_TYPE_ONESHOT) 12681 lun->delay_info.datamove_delay = 0; 12682 return; 12683 } 12684 } 12685#endif 12686 12687 /* 12688 * This command has been aborted. Set the port status, so we fail 12689 * the data move. 12690 */ 12691 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12692 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12693 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12694 io->io_hdr.nexus.targ_port, 12695 io->io_hdr.nexus.targ_lun); 12696 io->io_hdr.port_status = 31337; 12697 /* 12698 * Note that the backend, in this case, will get the 12699 * callback in its context. In other cases it may get 12700 * called in the frontend's interrupt thread context. 12701 */ 12702 io->scsiio.be_move_done(io); 12703 return; 12704 } 12705 12706 /* Don't confuse frontend with zero length data move. */ 12707 if (io->scsiio.kern_data_len == 0) { 12708 io->scsiio.be_move_done(io); 12709 return; 12710 } 12711 12712 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12713 fe_datamove(io); 12714} 12715 12716static void 12717ctl_send_datamove_done(union ctl_io *io, int have_lock) 12718{ 12719 union ctl_ha_msg msg; 12720#ifdef CTL_TIME_IO 12721 struct bintime cur_bt; 12722#endif 12723 12724 memset(&msg, 0, sizeof(msg)); 12725 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12726 msg.hdr.original_sc = io; 12727 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12728 msg.hdr.nexus = io->io_hdr.nexus; 12729 msg.hdr.status = io->io_hdr.status; 12730 msg.scsi.tag_num = io->scsiio.tag_num; 12731 msg.scsi.tag_type = io->scsiio.tag_type; 12732 msg.scsi.scsi_status = io->scsiio.scsi_status; 12733 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12734 io->scsiio.sense_len); 12735 msg.scsi.sense_len = io->scsiio.sense_len; 12736 msg.scsi.sense_residual = io->scsiio.sense_residual; 12737 msg.scsi.fetd_status = io->io_hdr.port_status; 12738 msg.scsi.residual = io->scsiio.residual; 12739 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12740 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12741 ctl_failover_io(io, /*have_lock*/ have_lock); 12742 return; 12743 } 12744 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12745 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12746 msg.scsi.sense_len, M_WAITOK); 12747 12748#ifdef CTL_TIME_IO 12749 getbinuptime(&cur_bt); 12750 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12751 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12752#endif 12753 io->io_hdr.num_dmas++; 12754} 12755 12756/* 12757 * The DMA to the remote side is done, now we need to tell the other side 12758 * we're done so it can continue with its data movement. 12759 */ 12760static void 12761ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12762{ 12763 union ctl_io *io; 12764 uint32_t i; 12765 12766 io = rq->context; 12767 12768 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12769 printf("%s: ISC DMA write failed with error %d", __func__, 12770 rq->ret); 12771 ctl_set_internal_failure(&io->scsiio, 12772 /*sks_valid*/ 1, 12773 /*retry_count*/ rq->ret); 12774 } 12775 12776 ctl_dt_req_free(rq); 12777 12778 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12779 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12780 free(io->io_hdr.remote_sglist, M_CTL); 12781 io->io_hdr.remote_sglist = NULL; 12782 io->io_hdr.local_sglist = NULL; 12783 12784 /* 12785 * The data is in local and remote memory, so now we need to send 12786 * status (good or back) back to the other side. 12787 */ 12788 ctl_send_datamove_done(io, /*have_lock*/ 0); 12789} 12790 12791/* 12792 * We've moved the data from the host/controller into local memory. Now we 12793 * need to push it over to the remote controller's memory. 12794 */ 12795static int 12796ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12797{ 12798 int retval; 12799 12800 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12801 ctl_datamove_remote_write_cb); 12802 return (retval); 12803} 12804 12805static void 12806ctl_datamove_remote_write(union ctl_io *io) 12807{ 12808 int retval; 12809 void (*fe_datamove)(union ctl_io *io); 12810 12811 /* 12812 * - Get the data from the host/HBA into local memory. 12813 * - DMA memory from the local controller to the remote controller. 12814 * - Send status back to the remote controller. 12815 */ 12816 12817 retval = ctl_datamove_remote_sgl_setup(io); 12818 if (retval != 0) 12819 return; 12820 12821 /* Switch the pointer over so the FETD knows what to do */ 12822 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12823 12824 /* 12825 * Use a custom move done callback, since we need to send completion 12826 * back to the other controller, not to the backend on this side. 12827 */ 12828 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12829 12830 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12831 fe_datamove(io); 12832} 12833 12834static int 12835ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12836{ 12837#if 0 12838 char str[256]; 12839 char path_str[64]; 12840 struct sbuf sb; 12841#endif 12842 uint32_t i; 12843 12844 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12845 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12846 free(io->io_hdr.remote_sglist, M_CTL); 12847 io->io_hdr.remote_sglist = NULL; 12848 io->io_hdr.local_sglist = NULL; 12849 12850#if 0 12851 scsi_path_string(io, path_str, sizeof(path_str)); 12852 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12853 sbuf_cat(&sb, path_str); 12854 scsi_command_string(&io->scsiio, NULL, &sb); 12855 sbuf_printf(&sb, "\n"); 12856 sbuf_cat(&sb, path_str); 12857 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12858 io->scsiio.tag_num, io->scsiio.tag_type); 12859 sbuf_cat(&sb, path_str); 12860 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12861 io->io_hdr.flags, io->io_hdr.status); 12862 sbuf_finish(&sb); 12863 printk("%s", sbuf_data(&sb)); 12864#endif 12865 12866 12867 /* 12868 * The read is done, now we need to send status (good or bad) back 12869 * to the other side. 12870 */ 12871 ctl_send_datamove_done(io, /*have_lock*/ 0); 12872 12873 return (0); 12874} 12875 12876static void 12877ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12878{ 12879 union ctl_io *io; 12880 void (*fe_datamove)(union ctl_io *io); 12881 12882 io = rq->context; 12883 12884 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12885 printf("%s: ISC DMA read failed with error %d\n", __func__, 12886 rq->ret); 12887 ctl_set_internal_failure(&io->scsiio, 12888 /*sks_valid*/ 1, 12889 /*retry_count*/ rq->ret); 12890 } 12891 12892 ctl_dt_req_free(rq); 12893 12894 /* Switch the pointer over so the FETD knows what to do */ 12895 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12896 12897 /* 12898 * Use a custom move done callback, since we need to send completion 12899 * back to the other controller, not to the backend on this side. 12900 */ 12901 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12902 12903 /* XXX KDM add checks like the ones in ctl_datamove? */ 12904 12905 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12906 fe_datamove(io); 12907} 12908 12909static int 12910ctl_datamove_remote_sgl_setup(union ctl_io *io) 12911{ 12912 struct ctl_sg_entry *local_sglist; 12913 uint32_t len_to_go; 12914 int retval; 12915 int i; 12916 12917 retval = 0; 12918 local_sglist = io->io_hdr.local_sglist; 12919 len_to_go = io->scsiio.kern_data_len; 12920 12921 /* 12922 * The difficult thing here is that the size of the various 12923 * S/G segments may be different than the size from the 12924 * remote controller. That'll make it harder when DMAing 12925 * the data back to the other side. 12926 */ 12927 for (i = 0; len_to_go > 0; i++) { 12928 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12929 local_sglist[i].addr = 12930 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12931 12932 len_to_go -= local_sglist[i].len; 12933 } 12934 /* 12935 * Reset the number of S/G entries accordingly. The original 12936 * number of S/G entries is available in rem_sg_entries. 12937 */ 12938 io->scsiio.kern_sg_entries = i; 12939 12940#if 0 12941 printf("%s: kern_sg_entries = %d\n", __func__, 12942 io->scsiio.kern_sg_entries); 12943 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12944 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12945 local_sglist[i].addr, local_sglist[i].len); 12946#endif 12947 12948 return (retval); 12949} 12950 12951static int 12952ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12953 ctl_ha_dt_cb callback) 12954{ 12955 struct ctl_ha_dt_req *rq; 12956 struct ctl_sg_entry *remote_sglist, *local_sglist; 12957 uint32_t local_used, remote_used, total_used; 12958 int i, j, isc_ret; 12959 12960 rq = ctl_dt_req_alloc(); 12961 12962 /* 12963 * If we failed to allocate the request, and if the DMA didn't fail 12964 * anyway, set busy status. This is just a resource allocation 12965 * failure. 12966 */ 12967 if ((rq == NULL) 12968 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12969 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12970 ctl_set_busy(&io->scsiio); 12971 12972 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12973 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12974 12975 if (rq != NULL) 12976 ctl_dt_req_free(rq); 12977 12978 /* 12979 * The data move failed. We need to return status back 12980 * to the other controller. No point in trying to DMA 12981 * data to the remote controller. 12982 */ 12983 12984 ctl_send_datamove_done(io, /*have_lock*/ 0); 12985 12986 return (1); 12987 } 12988 12989 local_sglist = io->io_hdr.local_sglist; 12990 remote_sglist = io->io_hdr.remote_sglist; 12991 local_used = 0; 12992 remote_used = 0; 12993 total_used = 0; 12994 12995 /* 12996 * Pull/push the data over the wire from/to the other controller. 12997 * This takes into account the possibility that the local and 12998 * remote sglists may not be identical in terms of the size of 12999 * the elements and the number of elements. 13000 * 13001 * One fundamental assumption here is that the length allocated for 13002 * both the local and remote sglists is identical. Otherwise, we've 13003 * essentially got a coding error of some sort. 13004 */ 13005 isc_ret = CTL_HA_STATUS_SUCCESS; 13006 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13007 uint32_t cur_len; 13008 uint8_t *tmp_ptr; 13009 13010 rq->command = command; 13011 rq->context = io; 13012 13013 /* 13014 * Both pointers should be aligned. But it is possible 13015 * that the allocation length is not. They should both 13016 * also have enough slack left over at the end, though, 13017 * to round up to the next 8 byte boundary. 13018 */ 13019 cur_len = MIN(local_sglist[i].len - local_used, 13020 remote_sglist[j].len - remote_used); 13021 rq->size = cur_len; 13022 13023 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13024 tmp_ptr += local_used; 13025 13026#if 0 13027 /* Use physical addresses when talking to ISC hardware */ 13028 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13029 /* XXX KDM use busdma */ 13030 rq->local = vtophys(tmp_ptr); 13031 } else 13032 rq->local = tmp_ptr; 13033#else 13034 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 13035 ("HA does not support BUS_ADDR")); 13036 rq->local = tmp_ptr; 13037#endif 13038 13039 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13040 tmp_ptr += remote_used; 13041 rq->remote = tmp_ptr; 13042 13043 rq->callback = NULL; 13044 13045 local_used += cur_len; 13046 if (local_used >= local_sglist[i].len) { 13047 i++; 13048 local_used = 0; 13049 } 13050 13051 remote_used += cur_len; 13052 if (remote_used >= remote_sglist[j].len) { 13053 j++; 13054 remote_used = 0; 13055 } 13056 total_used += cur_len; 13057 13058 if (total_used >= io->scsiio.kern_data_len) 13059 rq->callback = callback; 13060 13061#if 0 13062 printf("%s: %s: local %p remote %p size %d\n", __func__, 13063 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13064 rq->local, rq->remote, rq->size); 13065#endif 13066 13067 isc_ret = ctl_dt_single(rq); 13068 if (isc_ret > CTL_HA_STATUS_SUCCESS) 13069 break; 13070 } 13071 if (isc_ret != CTL_HA_STATUS_WAIT) { 13072 rq->ret = isc_ret; 13073 callback(rq); 13074 } 13075 13076 return (0); 13077} 13078 13079static void 13080ctl_datamove_remote_read(union ctl_io *io) 13081{ 13082 int retval; 13083 uint32_t i; 13084 13085 /* 13086 * This will send an error to the other controller in the case of a 13087 * failure. 13088 */ 13089 retval = ctl_datamove_remote_sgl_setup(io); 13090 if (retval != 0) 13091 return; 13092 13093 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13094 ctl_datamove_remote_read_cb); 13095 if (retval != 0) { 13096 /* 13097 * Make sure we free memory if there was an error.. The 13098 * ctl_datamove_remote_xfer() function will send the 13099 * datamove done message, or call the callback with an 13100 * error if there is a problem. 13101 */ 13102 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13103 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13104 free(io->io_hdr.remote_sglist, M_CTL); 13105 io->io_hdr.remote_sglist = NULL; 13106 io->io_hdr.local_sglist = NULL; 13107 } 13108} 13109 13110/* 13111 * Process a datamove request from the other controller. This is used for 13112 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13113 * first. Once that is complete, the data gets DMAed into the remote 13114 * controller's memory. For reads, we DMA from the remote controller's 13115 * memory into our memory first, and then move it out to the FETD. 13116 */ 13117static void 13118ctl_datamove_remote(union ctl_io *io) 13119{ 13120 13121 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13122 13123 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13124 ctl_failover_io(io, /*have_lock*/ 0); 13125 return; 13126 } 13127 13128 /* 13129 * Note that we look for an aborted I/O here, but don't do some of 13130 * the other checks that ctl_datamove() normally does. 13131 * We don't need to run the datamove delay code, since that should 13132 * have been done if need be on the other controller. 13133 */ 13134 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13135 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13136 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13137 io->io_hdr.nexus.targ_port, 13138 io->io_hdr.nexus.targ_lun); 13139 io->io_hdr.port_status = 31338; 13140 ctl_send_datamove_done(io, /*have_lock*/ 0); 13141 return; 13142 } 13143 13144 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13145 ctl_datamove_remote_write(io); 13146 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13147 ctl_datamove_remote_read(io); 13148 else { 13149 io->io_hdr.port_status = 31339; 13150 ctl_send_datamove_done(io, /*have_lock*/ 0); 13151 } 13152} 13153 13154static void 13155ctl_process_done(union ctl_io *io) 13156{ 13157 struct ctl_lun *lun; 13158 struct ctl_softc *softc = control_softc; 13159 void (*fe_done)(union ctl_io *io); 13160 union ctl_ha_msg msg; 13161 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13162 13163 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13164 fe_done = softc->ctl_ports[targ_port]->fe_done; 13165 13166#ifdef CTL_TIME_IO 13167 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13168 char str[256]; 13169 char path_str[64]; 13170 struct sbuf sb; 13171 13172 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13173 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13174 13175 sbuf_cat(&sb, path_str); 13176 switch (io->io_hdr.io_type) { 13177 case CTL_IO_SCSI: 13178 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13179 sbuf_printf(&sb, "\n"); 13180 sbuf_cat(&sb, path_str); 13181 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13182 io->scsiio.tag_num, io->scsiio.tag_type); 13183 break; 13184 case CTL_IO_TASK: 13185 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13186 "Tag Type: %d\n", io->taskio.task_action, 13187 io->taskio.tag_num, io->taskio.tag_type); 13188 break; 13189 default: 13190 panic("%s: Invalid CTL I/O type %d\n", 13191 __func__, io->io_hdr.io_type); 13192 } 13193 sbuf_cat(&sb, path_str); 13194 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13195 (intmax_t)time_uptime - io->io_hdr.start_time); 13196 sbuf_finish(&sb); 13197 printf("%s", sbuf_data(&sb)); 13198 } 13199#endif /* CTL_TIME_IO */ 13200 13201 switch (io->io_hdr.io_type) { 13202 case CTL_IO_SCSI: 13203 break; 13204 case CTL_IO_TASK: 13205 if (ctl_debug & CTL_DEBUG_INFO) 13206 ctl_io_error_print(io, NULL); 13207 fe_done(io); 13208 return; 13209 default: 13210 panic("%s: Invalid CTL I/O type %d\n", 13211 __func__, io->io_hdr.io_type); 13212 } 13213 13214 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13215 if (lun == NULL) { 13216 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13217 io->io_hdr.nexus.targ_mapped_lun)); 13218 goto bailout; 13219 } 13220 13221 mtx_lock(&lun->lun_lock); 13222 13223 /* 13224 * Check to see if we have any informational exception and status 13225 * of this command can be modified to report it in form of either 13226 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13227 */ 13228 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13229 io->io_hdr.status == CTL_SUCCESS && 13230 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13231 uint8_t mrie = lun->MODE_IE.mrie; 13232 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13233 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13234 if (((mrie == SIEP_MRIE_REC_COND && per) || 13235 mrie == SIEP_MRIE_REC_UNCOND || 13236 mrie == SIEP_MRIE_NO_SENSE) && 13237 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13238 CTL_CMD_FLAG_NO_SENSE) == 0) { 13239 ctl_set_sense(&io->scsiio, 13240 /*current_error*/ 1, 13241 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13242 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13243 /*asc*/ lun->ie_asc, 13244 /*ascq*/ lun->ie_ascq, 13245 SSD_ELEM_NONE); 13246 lun->ie_reported = 1; 13247 } 13248 } else if (lun->ie_reported < 0) 13249 lun->ie_reported = 0; 13250 13251 /* 13252 * Check to see if we have any errors to inject here. We only 13253 * inject errors for commands that don't already have errors set. 13254 */ 13255 if (!STAILQ_EMPTY(&lun->error_list) && 13256 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13257 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13258 ctl_inject_error(lun, io); 13259 13260 /* 13261 * XXX KDM how do we treat commands that aren't completed 13262 * successfully? 13263 * 13264 * XXX KDM should we also track I/O latency? 13265 */ 13266 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13267 io->io_hdr.io_type == CTL_IO_SCSI) { 13268#ifdef CTL_TIME_IO 13269 struct bintime cur_bt; 13270#endif 13271 int type; 13272 13273 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13274 CTL_FLAG_DATA_IN) 13275 type = CTL_STATS_READ; 13276 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13277 CTL_FLAG_DATA_OUT) 13278 type = CTL_STATS_WRITE; 13279 else 13280 type = CTL_STATS_NO_IO; 13281 13282 lun->stats.ports[targ_port].bytes[type] += 13283 io->scsiio.kern_total_len; 13284 lun->stats.ports[targ_port].operations[type]++; 13285#ifdef CTL_TIME_IO 13286 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13287 &io->io_hdr.dma_bt); 13288 getbinuptime(&cur_bt); 13289 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13290 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13291#endif 13292 lun->stats.ports[targ_port].num_dmas[type] += 13293 io->io_hdr.num_dmas; 13294 } 13295 13296 /* 13297 * Remove this from the OOA queue. 13298 */ 13299 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13300#ifdef CTL_TIME_IO 13301 if (TAILQ_EMPTY(&lun->ooa_queue)) 13302 lun->last_busy = getsbinuptime(); 13303#endif 13304 13305 /* 13306 * Run through the blocked queue on this LUN and see if anything 13307 * has become unblocked, now that this transaction is done. 13308 */ 13309 ctl_check_blocked(lun); 13310 13311 /* 13312 * If the LUN has been invalidated, free it if there is nothing 13313 * left on its OOA queue. 13314 */ 13315 if ((lun->flags & CTL_LUN_INVALID) 13316 && TAILQ_EMPTY(&lun->ooa_queue)) { 13317 mtx_unlock(&lun->lun_lock); 13318 mtx_lock(&softc->ctl_lock); 13319 ctl_free_lun(lun); 13320 mtx_unlock(&softc->ctl_lock); 13321 } else 13322 mtx_unlock(&lun->lun_lock); 13323 13324bailout: 13325 13326 /* 13327 * If this command has been aborted, make sure we set the status 13328 * properly. The FETD is responsible for freeing the I/O and doing 13329 * whatever it needs to do to clean up its state. 13330 */ 13331 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13332 ctl_set_task_aborted(&io->scsiio); 13333 13334 /* 13335 * If enabled, print command error status. 13336 */ 13337 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13338 (ctl_debug & CTL_DEBUG_INFO) != 0) 13339 ctl_io_error_print(io, NULL); 13340 13341 /* 13342 * Tell the FETD or the other shelf controller we're done with this 13343 * command. Note that only SCSI commands get to this point. Task 13344 * management commands are completed above. 13345 */ 13346 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13347 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13348 memset(&msg, 0, sizeof(msg)); 13349 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13350 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13351 msg.hdr.nexus = io->io_hdr.nexus; 13352 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13353 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13354 M_WAITOK); 13355 } 13356 13357 fe_done(io); 13358} 13359 13360#ifdef CTL_WITH_CA 13361/* 13362 * Front end should call this if it doesn't do autosense. When the request 13363 * sense comes back in from the initiator, we'll dequeue this and send it. 13364 */ 13365int 13366ctl_queue_sense(union ctl_io *io) 13367{ 13368 struct ctl_lun *lun; 13369 struct ctl_port *port; 13370 struct ctl_softc *softc; 13371 uint32_t initidx, targ_lun; 13372 13373 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13374 13375 softc = control_softc; 13376 port = ctl_io_port(&ctsio->io_hdr); 13377 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13378 13379 /* 13380 * LUN lookup will likely move to the ctl_work_thread() once we 13381 * have our new queueing infrastructure (that doesn't put things on 13382 * a per-LUN queue initially). That is so that we can handle 13383 * things like an INQUIRY to a LUN that we don't have enabled. We 13384 * can't deal with that right now. 13385 * If we don't have a LUN for this, just toss the sense information. 13386 */ 13387 mtx_lock(&softc->ctl_lock); 13388 if (targ_lun >= CTL_MAX_LUNS || 13389 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13390 mtx_unlock(&softc->ctl_lock); 13391 goto bailout; 13392 } 13393 mtx_lock(&lun->lun_lock); 13394 mtx_unlock(&softc->ctl_lock); 13395 13396 /* 13397 * Already have CA set for this LUN...toss the sense information. 13398 */ 13399 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13400 if (ctl_is_set(lun->have_ca, initidx)) { 13401 mtx_unlock(&lun->lun_lock); 13402 goto bailout; 13403 } 13404 13405 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13406 MIN(sizeof(lun->pending_sense[initidx]), 13407 sizeof(io->scsiio.sense_data))); 13408 ctl_set_mask(lun->have_ca, initidx); 13409 mtx_unlock(&lun->lun_lock); 13410 13411bailout: 13412 ctl_free_io(io); 13413 return (CTL_RETVAL_COMPLETE); 13414} 13415#endif 13416 13417/* 13418 * Primary command inlet from frontend ports. All SCSI and task I/O 13419 * requests must go through this function. 13420 */ 13421int 13422ctl_queue(union ctl_io *io) 13423{ 13424 struct ctl_port *port; 13425 13426 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13427 13428#ifdef CTL_TIME_IO 13429 io->io_hdr.start_time = time_uptime; 13430 getbinuptime(&io->io_hdr.start_bt); 13431#endif /* CTL_TIME_IO */ 13432 13433 /* Map FE-specific LUN ID into global one. */ 13434 port = ctl_io_port(&io->io_hdr); 13435 io->io_hdr.nexus.targ_mapped_lun = 13436 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13437 13438 switch (io->io_hdr.io_type) { 13439 case CTL_IO_SCSI: 13440 case CTL_IO_TASK: 13441 if (ctl_debug & CTL_DEBUG_CDB) 13442 ctl_io_print(io); 13443 ctl_enqueue_incoming(io); 13444 break; 13445 default: 13446 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13447 return (EINVAL); 13448 } 13449 13450 return (CTL_RETVAL_COMPLETE); 13451} 13452 13453#ifdef CTL_IO_DELAY 13454static void 13455ctl_done_timer_wakeup(void *arg) 13456{ 13457 union ctl_io *io; 13458 13459 io = (union ctl_io *)arg; 13460 ctl_done(io); 13461} 13462#endif /* CTL_IO_DELAY */ 13463 13464void 13465ctl_serseq_done(union ctl_io *io) 13466{ 13467 struct ctl_lun *lun; 13468 13469 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13470 if (lun->be_lun == NULL || 13471 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13472 return; 13473 mtx_lock(&lun->lun_lock); 13474 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13475 ctl_check_blocked(lun); 13476 mtx_unlock(&lun->lun_lock); 13477} 13478 13479void 13480ctl_done(union ctl_io *io) 13481{ 13482 13483 /* 13484 * Enable this to catch duplicate completion issues. 13485 */ 13486#if 0 13487 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13488 printf("%s: type %d msg %d cdb %x iptl: " 13489 "%u:%u:%u tag 0x%04x " 13490 "flag %#x status %x\n", 13491 __func__, 13492 io->io_hdr.io_type, 13493 io->io_hdr.msg_type, 13494 io->scsiio.cdb[0], 13495 io->io_hdr.nexus.initid, 13496 io->io_hdr.nexus.targ_port, 13497 io->io_hdr.nexus.targ_lun, 13498 (io->io_hdr.io_type == 13499 CTL_IO_TASK) ? 13500 io->taskio.tag_num : 13501 io->scsiio.tag_num, 13502 io->io_hdr.flags, 13503 io->io_hdr.status); 13504 } else 13505 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13506#endif 13507 13508 /* 13509 * This is an internal copy of an I/O, and should not go through 13510 * the normal done processing logic. 13511 */ 13512 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13513 return; 13514 13515#ifdef CTL_IO_DELAY 13516 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13517 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13518 } else { 13519 struct ctl_lun *lun; 13520 13521 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13522 13523 if ((lun != NULL) 13524 && (lun->delay_info.done_delay > 0)) { 13525 13526 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13527 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13528 callout_reset(&io->io_hdr.delay_callout, 13529 lun->delay_info.done_delay * hz, 13530 ctl_done_timer_wakeup, io); 13531 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13532 lun->delay_info.done_delay = 0; 13533 return; 13534 } 13535 } 13536#endif /* CTL_IO_DELAY */ 13537 13538 ctl_enqueue_done(io); 13539} 13540 13541static void 13542ctl_work_thread(void *arg) 13543{ 13544 struct ctl_thread *thr = (struct ctl_thread *)arg; 13545 struct ctl_softc *softc = thr->ctl_softc; 13546 union ctl_io *io; 13547 int retval; 13548 13549 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13550 13551 for (;;) { 13552 /* 13553 * We handle the queues in this order: 13554 * - ISC 13555 * - done queue (to free up resources, unblock other commands) 13556 * - RtR queue 13557 * - incoming queue 13558 * 13559 * If those queues are empty, we break out of the loop and 13560 * go to sleep. 13561 */ 13562 mtx_lock(&thr->queue_lock); 13563 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13564 if (io != NULL) { 13565 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13566 mtx_unlock(&thr->queue_lock); 13567 ctl_handle_isc(io); 13568 continue; 13569 } 13570 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13571 if (io != NULL) { 13572 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13573 /* clear any blocked commands, call fe_done */ 13574 mtx_unlock(&thr->queue_lock); 13575 ctl_process_done(io); 13576 continue; 13577 } 13578 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13579 if (io != NULL) { 13580 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13581 mtx_unlock(&thr->queue_lock); 13582 if (io->io_hdr.io_type == CTL_IO_TASK) 13583 ctl_run_task(io); 13584 else 13585 ctl_scsiio_precheck(softc, &io->scsiio); 13586 continue; 13587 } 13588 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13589 if (io != NULL) { 13590 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13591 mtx_unlock(&thr->queue_lock); 13592 retval = ctl_scsiio(&io->scsiio); 13593 if (retval != CTL_RETVAL_COMPLETE) 13594 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13595 continue; 13596 } 13597 13598 /* Sleep until we have something to do. */ 13599 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13600 } 13601} 13602 13603static void 13604ctl_lun_thread(void *arg) 13605{ 13606 struct ctl_softc *softc = (struct ctl_softc *)arg; 13607 struct ctl_be_lun *be_lun; 13608 13609 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13610 13611 for (;;) { 13612 mtx_lock(&softc->ctl_lock); 13613 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13614 if (be_lun != NULL) { 13615 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13616 mtx_unlock(&softc->ctl_lock); 13617 ctl_create_lun(be_lun); 13618 continue; 13619 } 13620 13621 /* Sleep until we have something to do. */ 13622 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13623 PDROP | PRIBIO, "-", 0); 13624 } 13625} 13626 13627static void 13628ctl_thresh_thread(void *arg) 13629{ 13630 struct ctl_softc *softc = (struct ctl_softc *)arg; 13631 struct ctl_lun *lun; 13632 struct ctl_logical_block_provisioning_page *page; 13633 const char *attr; 13634 union ctl_ha_msg msg; 13635 uint64_t thres, val; 13636 int i, e, set; 13637 13638 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13639 13640 for (;;) { 13641 mtx_lock(&softc->ctl_lock); 13642 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13643 if ((lun->flags & CTL_LUN_DISABLED) || 13644 (lun->flags & CTL_LUN_NO_MEDIA) || 13645 lun->backend->lun_attr == NULL) 13646 continue; 13647 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13648 softc->ha_mode == CTL_HA_MODE_XFER) 13649 continue; 13650 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13651 continue; 13652 e = 0; 13653 page = &lun->MODE_LBP; 13654 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13655 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13656 continue; 13657 thres = scsi_4btoul(page->descr[i].count); 13658 thres <<= CTL_LBP_EXPONENT; 13659 switch (page->descr[i].resource) { 13660 case 0x01: 13661 attr = "blocksavail"; 13662 break; 13663 case 0x02: 13664 attr = "blocksused"; 13665 break; 13666 case 0xf1: 13667 attr = "poolblocksavail"; 13668 break; 13669 case 0xf2: 13670 attr = "poolblocksused"; 13671 break; 13672 default: 13673 continue; 13674 } 13675 mtx_unlock(&softc->ctl_lock); // XXX 13676 val = lun->backend->lun_attr( 13677 lun->be_lun->be_lun, attr); 13678 mtx_lock(&softc->ctl_lock); 13679 if (val == UINT64_MAX) 13680 continue; 13681 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13682 == SLBPPD_ARMING_INC) 13683 e = (val >= thres); 13684 else 13685 e = (val <= thres); 13686 if (e) 13687 break; 13688 } 13689 mtx_lock(&lun->lun_lock); 13690 if (e) { 13691 scsi_u64to8b((uint8_t *)&page->descr[i] - 13692 (uint8_t *)page, lun->ua_tpt_info); 13693 if (lun->lasttpt == 0 || 13694 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13695 lun->lasttpt = time_uptime; 13696 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13697 set = 1; 13698 } else 13699 set = 0; 13700 } else { 13701 lun->lasttpt = 0; 13702 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13703 set = -1; 13704 } 13705 mtx_unlock(&lun->lun_lock); 13706 if (set != 0 && 13707 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13708 /* Send msg to other side. */ 13709 bzero(&msg.ua, sizeof(msg.ua)); 13710 msg.hdr.msg_type = CTL_MSG_UA; 13711 msg.hdr.nexus.initid = -1; 13712 msg.hdr.nexus.targ_port = -1; 13713 msg.hdr.nexus.targ_lun = lun->lun; 13714 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13715 msg.ua.ua_all = 1; 13716 msg.ua.ua_set = (set > 0); 13717 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13718 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13719 mtx_unlock(&softc->ctl_lock); // XXX 13720 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13721 sizeof(msg.ua), M_WAITOK); 13722 mtx_lock(&softc->ctl_lock); 13723 } 13724 } 13725 mtx_unlock(&softc->ctl_lock); 13726 pause("-", CTL_LBP_PERIOD * hz); 13727 } 13728} 13729 13730static void 13731ctl_enqueue_incoming(union ctl_io *io) 13732{ 13733 struct ctl_softc *softc = control_softc; 13734 struct ctl_thread *thr; 13735 u_int idx; 13736 13737 idx = (io->io_hdr.nexus.targ_port * 127 + 13738 io->io_hdr.nexus.initid) % worker_threads; 13739 thr = &softc->threads[idx]; 13740 mtx_lock(&thr->queue_lock); 13741 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13742 mtx_unlock(&thr->queue_lock); 13743 wakeup(thr); 13744} 13745 13746static void 13747ctl_enqueue_rtr(union ctl_io *io) 13748{ 13749 struct ctl_softc *softc = control_softc; 13750 struct ctl_thread *thr; 13751 13752 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13753 mtx_lock(&thr->queue_lock); 13754 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13755 mtx_unlock(&thr->queue_lock); 13756 wakeup(thr); 13757} 13758 13759static void 13760ctl_enqueue_done(union ctl_io *io) 13761{ 13762 struct ctl_softc *softc = control_softc; 13763 struct ctl_thread *thr; 13764 13765 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13766 mtx_lock(&thr->queue_lock); 13767 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13768 mtx_unlock(&thr->queue_lock); 13769 wakeup(thr); 13770} 13771 13772static void 13773ctl_enqueue_isc(union ctl_io *io) 13774{ 13775 struct ctl_softc *softc = control_softc; 13776 struct ctl_thread *thr; 13777 13778 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13779 mtx_lock(&thr->queue_lock); 13780 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13781 mtx_unlock(&thr->queue_lock); 13782 wakeup(thr); 13783} 13784 13785/* 13786 * vim: ts=8 13787 */ 13788