ctl.c revision 312841
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37/* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43#define _CTL_C 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 312841 2017-01-26 21:00:49Z 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); 442 443static int ctl_do_mode_select(union ctl_io *io); 444static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 445 uint64_t res_key, uint64_t sa_res_key, 446 uint8_t type, uint32_t residx, 447 struct ctl_scsiio *ctsio, 448 struct scsi_per_res_out *cdb, 449 struct scsi_per_res_out_parms* param); 450static void ctl_pro_preempt_other(struct ctl_lun *lun, 451 union ctl_ha_msg *msg); 452static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 453static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 454static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 455static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 456static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 457static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 458static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 459 int alloc_len); 460static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 461 int alloc_len); 462static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 463static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 464static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 465static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 466static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 467static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 468 bool seq); 469static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 470static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 471 union ctl_io *pending_io, union ctl_io *ooa_io); 472static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 473 union ctl_io *starting_io); 474static int ctl_check_blocked(struct ctl_lun *lun); 475static int ctl_scsiio_lun_check(struct ctl_lun *lun, 476 const struct ctl_cmd_entry *entry, 477 struct ctl_scsiio *ctsio); 478static void ctl_failover_lun(union ctl_io *io); 479static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 480 struct ctl_scsiio *ctsio); 481static int ctl_scsiio(struct ctl_scsiio *ctsio); 482 483static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 484static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 485 ctl_ua_type ua_type); 486static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 487 ctl_ua_type ua_type); 488static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 489static int ctl_abort_task(union ctl_io *io); 490static int ctl_abort_task_set(union ctl_io *io); 491static int ctl_query_task(union ctl_io *io, int task_set); 492static int ctl_i_t_nexus_reset(union ctl_io *io); 493static int ctl_query_async_event(union ctl_io *io); 494static void ctl_run_task(union ctl_io *io); 495#ifdef CTL_IO_DELAY 496static void ctl_datamove_timer_wakeup(void *arg); 497static void ctl_done_timer_wakeup(void *arg); 498#endif /* CTL_IO_DELAY */ 499 500static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 501static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 502static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 503static void ctl_datamove_remote_write(union ctl_io *io); 504static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 505static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 506static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 507static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 508 ctl_ha_dt_cb callback); 509static void ctl_datamove_remote_read(union ctl_io *io); 510static void ctl_datamove_remote(union ctl_io *io); 511static void ctl_process_done(union ctl_io *io); 512static void ctl_lun_thread(void *arg); 513static void ctl_thresh_thread(void *arg); 514static void ctl_work_thread(void *arg); 515static void ctl_enqueue_incoming(union ctl_io *io); 516static void ctl_enqueue_rtr(union ctl_io *io); 517static void ctl_enqueue_done(union ctl_io *io); 518static void ctl_enqueue_isc(union ctl_io *io); 519static const struct ctl_cmd_entry * 520 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 521static const struct ctl_cmd_entry * 522 ctl_validate_command(struct ctl_scsiio *ctsio); 523static int ctl_cmd_applicable(uint8_t lun_type, 524 const struct ctl_cmd_entry *entry); 525 526static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 527static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 528static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 529static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 530 531/* 532 * Load the serialization table. This isn't very pretty, but is probably 533 * the easiest way to do it. 534 */ 535#include "ctl_ser_table.c" 536 537/* 538 * We only need to define open, close and ioctl routines for this driver. 539 */ 540static struct cdevsw ctl_cdevsw = { 541 .d_version = D_VERSION, 542 .d_flags = 0, 543 .d_open = ctl_open, 544 .d_close = ctl_close, 545 .d_ioctl = ctl_ioctl, 546 .d_name = "ctl", 547}; 548 549 550MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 551 552static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 553 554static moduledata_t ctl_moduledata = { 555 "ctl", 556 ctl_module_event_handler, 557 NULL 558}; 559 560DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 561MODULE_VERSION(ctl, 1); 562 563static struct ctl_frontend ha_frontend = 564{ 565 .name = "ha", 566}; 567 568static void 569ctl_ha_datamove(union ctl_io *io) 570{ 571 struct ctl_lun *lun = CTL_LUN(io); 572 struct ctl_sg_entry *sgl; 573 union ctl_ha_msg msg; 574 uint32_t sg_entries_sent; 575 int do_sg_copy, i, j; 576 577 memset(&msg.dt, 0, sizeof(msg.dt)); 578 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 579 msg.hdr.original_sc = io->io_hdr.original_sc; 580 msg.hdr.serializing_sc = io; 581 msg.hdr.nexus = io->io_hdr.nexus; 582 msg.hdr.status = io->io_hdr.status; 583 msg.dt.flags = io->io_hdr.flags; 584 585 /* 586 * We convert everything into a S/G list here. We can't 587 * pass by reference, only by value between controllers. 588 * So we can't pass a pointer to the S/G list, only as many 589 * S/G entries as we can fit in here. If it's possible for 590 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 591 * then we need to break this up into multiple transfers. 592 */ 593 if (io->scsiio.kern_sg_entries == 0) { 594 msg.dt.kern_sg_entries = 1; 595#if 0 596 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 597 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 598 } else { 599 /* XXX KDM use busdma here! */ 600 msg.dt.sg_list[0].addr = 601 (void *)vtophys(io->scsiio.kern_data_ptr); 602 } 603#else 604 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 605 ("HA does not support BUS_ADDR")); 606 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 607#endif 608 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 609 do_sg_copy = 0; 610 } else { 611 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 612 do_sg_copy = 1; 613 } 614 615 msg.dt.kern_data_len = io->scsiio.kern_data_len; 616 msg.dt.kern_total_len = io->scsiio.kern_total_len; 617 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 618 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 619 msg.dt.sg_sequence = 0; 620 621 /* 622 * Loop until we've sent all of the S/G entries. On the 623 * other end, we'll recompose these S/G entries into one 624 * contiguous list before processing. 625 */ 626 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 627 msg.dt.sg_sequence++) { 628 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 629 sizeof(msg.dt.sg_list[0])), 630 msg.dt.kern_sg_entries - sg_entries_sent); 631 if (do_sg_copy != 0) { 632 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 633 for (i = sg_entries_sent, j = 0; 634 i < msg.dt.cur_sg_entries; i++, j++) { 635#if 0 636 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 637 msg.dt.sg_list[j].addr = sgl[i].addr; 638 } else { 639 /* XXX KDM use busdma here! */ 640 msg.dt.sg_list[j].addr = 641 (void *)vtophys(sgl[i].addr); 642 } 643#else 644 KASSERT((io->io_hdr.flags & 645 CTL_FLAG_BUS_ADDR) == 0, 646 ("HA does not support BUS_ADDR")); 647 msg.dt.sg_list[j].addr = sgl[i].addr; 648#endif 649 msg.dt.sg_list[j].len = sgl[i].len; 650 } 651 } 652 653 sg_entries_sent += msg.dt.cur_sg_entries; 654 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 656 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 657 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 658 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 659 io->io_hdr.port_status = 31341; 660 io->scsiio.be_move_done(io); 661 return; 662 } 663 msg.dt.sent_sg_entries = sg_entries_sent; 664 } 665 666 /* 667 * Officially handover the request from us to peer. 668 * If failover has just happened, then we must return error. 669 * If failover happen just after, then it is not our problem. 670 */ 671 if (lun) 672 mtx_lock(&lun->lun_lock); 673 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 674 if (lun) 675 mtx_unlock(&lun->lun_lock); 676 io->io_hdr.port_status = 31342; 677 io->scsiio.be_move_done(io); 678 return; 679 } 680 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 681 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 682 if (lun) 683 mtx_unlock(&lun->lun_lock); 684} 685 686static void 687ctl_ha_done(union ctl_io *io) 688{ 689 union ctl_ha_msg msg; 690 691 if (io->io_hdr.io_type == CTL_IO_SCSI) { 692 memset(&msg, 0, sizeof(msg)); 693 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 694 msg.hdr.original_sc = io->io_hdr.original_sc; 695 msg.hdr.nexus = io->io_hdr.nexus; 696 msg.hdr.status = io->io_hdr.status; 697 msg.scsi.scsi_status = io->scsiio.scsi_status; 698 msg.scsi.tag_num = io->scsiio.tag_num; 699 msg.scsi.tag_type = io->scsiio.tag_type; 700 msg.scsi.sense_len = io->scsiio.sense_len; 701 msg.scsi.sense_residual = io->scsiio.sense_residual; 702 msg.scsi.residual = io->scsiio.residual; 703 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 704 io->scsiio.sense_len); 705 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 706 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 707 msg.scsi.sense_len, M_WAITOK); 708 } 709 ctl_free_io(io); 710} 711 712static void 713ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 714 union ctl_ha_msg *msg_info) 715{ 716 struct ctl_scsiio *ctsio; 717 718 if (msg_info->hdr.original_sc == NULL) { 719 printf("%s: original_sc == NULL!\n", __func__); 720 /* XXX KDM now what? */ 721 return; 722 } 723 724 ctsio = &msg_info->hdr.original_sc->scsiio; 725 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 726 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 727 ctsio->io_hdr.status = msg_info->hdr.status; 728 ctsio->scsi_status = msg_info->scsi.scsi_status; 729 ctsio->sense_len = msg_info->scsi.sense_len; 730 ctsio->sense_residual = msg_info->scsi.sense_residual; 731 ctsio->residual = msg_info->scsi.residual; 732 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 733 msg_info->scsi.sense_len); 734 ctl_enqueue_isc((union ctl_io *)ctsio); 735} 736 737static void 738ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 739 union ctl_ha_msg *msg_info) 740{ 741 struct ctl_scsiio *ctsio; 742 743 if (msg_info->hdr.serializing_sc == NULL) { 744 printf("%s: serializing_sc == NULL!\n", __func__); 745 /* XXX KDM now what? */ 746 return; 747 } 748 749 ctsio = &msg_info->hdr.serializing_sc->scsiio; 750 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 751 ctl_enqueue_isc((union ctl_io *)ctsio); 752} 753 754void 755ctl_isc_announce_lun(struct ctl_lun *lun) 756{ 757 struct ctl_softc *softc = lun->ctl_softc; 758 union ctl_ha_msg *msg; 759 struct ctl_ha_msg_lun_pr_key pr_key; 760 int i, k; 761 762 if (softc->ha_link != CTL_HA_LINK_ONLINE) 763 return; 764 mtx_lock(&lun->lun_lock); 765 i = sizeof(msg->lun); 766 if (lun->lun_devid) 767 i += lun->lun_devid->len; 768 i += sizeof(pr_key) * lun->pr_key_count; 769alloc: 770 mtx_unlock(&lun->lun_lock); 771 msg = malloc(i, M_CTL, M_WAITOK); 772 mtx_lock(&lun->lun_lock); 773 k = sizeof(msg->lun); 774 if (lun->lun_devid) 775 k += lun->lun_devid->len; 776 k += sizeof(pr_key) * lun->pr_key_count; 777 if (i < k) { 778 free(msg, M_CTL); 779 i = k; 780 goto alloc; 781 } 782 bzero(&msg->lun, sizeof(msg->lun)); 783 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 784 msg->hdr.nexus.targ_lun = lun->lun; 785 msg->hdr.nexus.targ_mapped_lun = lun->lun; 786 msg->lun.flags = lun->flags; 787 msg->lun.pr_generation = lun->pr_generation; 788 msg->lun.pr_res_idx = lun->pr_res_idx; 789 msg->lun.pr_res_type = lun->pr_res_type; 790 msg->lun.pr_key_count = lun->pr_key_count; 791 i = 0; 792 if (lun->lun_devid) { 793 msg->lun.lun_devid_len = lun->lun_devid->len; 794 memcpy(&msg->lun.data[i], lun->lun_devid->data, 795 msg->lun.lun_devid_len); 796 i += msg->lun.lun_devid_len; 797 } 798 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 799 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 800 continue; 801 pr_key.pr_iid = k; 802 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 803 i += sizeof(pr_key); 804 } 805 mtx_unlock(&lun->lun_lock); 806 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 807 M_WAITOK); 808 free(msg, M_CTL); 809 810 if (lun->flags & CTL_LUN_PRIMARY_SC) { 811 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 812 ctl_isc_announce_mode(lun, -1, 813 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 814 lun->mode_pages.index[i].subpage); 815 } 816 } 817} 818 819void 820ctl_isc_announce_port(struct ctl_port *port) 821{ 822 struct ctl_softc *softc = port->ctl_softc; 823 union ctl_ha_msg *msg; 824 int i; 825 826 if (port->targ_port < softc->port_min || 827 port->targ_port >= softc->port_max || 828 softc->ha_link != CTL_HA_LINK_ONLINE) 829 return; 830 i = sizeof(msg->port) + strlen(port->port_name) + 1; 831 if (port->lun_map) 832 i += port->lun_map_size * sizeof(uint32_t); 833 if (port->port_devid) 834 i += port->port_devid->len; 835 if (port->target_devid) 836 i += port->target_devid->len; 837 if (port->init_devid) 838 i += port->init_devid->len; 839 msg = malloc(i, M_CTL, M_WAITOK); 840 bzero(&msg->port, sizeof(msg->port)); 841 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 842 msg->hdr.nexus.targ_port = port->targ_port; 843 msg->port.port_type = port->port_type; 844 msg->port.physical_port = port->physical_port; 845 msg->port.virtual_port = port->virtual_port; 846 msg->port.status = port->status; 847 i = 0; 848 msg->port.name_len = sprintf(&msg->port.data[i], 849 "%d:%s", softc->ha_id, port->port_name) + 1; 850 i += msg->port.name_len; 851 if (port->lun_map) { 852 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 853 memcpy(&msg->port.data[i], port->lun_map, 854 msg->port.lun_map_len); 855 i += msg->port.lun_map_len; 856 } 857 if (port->port_devid) { 858 msg->port.port_devid_len = port->port_devid->len; 859 memcpy(&msg->port.data[i], port->port_devid->data, 860 msg->port.port_devid_len); 861 i += msg->port.port_devid_len; 862 } 863 if (port->target_devid) { 864 msg->port.target_devid_len = port->target_devid->len; 865 memcpy(&msg->port.data[i], port->target_devid->data, 866 msg->port.target_devid_len); 867 i += msg->port.target_devid_len; 868 } 869 if (port->init_devid) { 870 msg->port.init_devid_len = port->init_devid->len; 871 memcpy(&msg->port.data[i], port->init_devid->data, 872 msg->port.init_devid_len); 873 i += msg->port.init_devid_len; 874 } 875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 876 M_WAITOK); 877 free(msg, M_CTL); 878} 879 880void 881ctl_isc_announce_iid(struct ctl_port *port, int iid) 882{ 883 struct ctl_softc *softc = port->ctl_softc; 884 union ctl_ha_msg *msg; 885 int i, l; 886 887 if (port->targ_port < softc->port_min || 888 port->targ_port >= softc->port_max || 889 softc->ha_link != CTL_HA_LINK_ONLINE) 890 return; 891 mtx_lock(&softc->ctl_lock); 892 i = sizeof(msg->iid); 893 l = 0; 894 if (port->wwpn_iid[iid].name) 895 l = strlen(port->wwpn_iid[iid].name) + 1; 896 i += l; 897 msg = malloc(i, M_CTL, M_NOWAIT); 898 if (msg == NULL) { 899 mtx_unlock(&softc->ctl_lock); 900 return; 901 } 902 bzero(&msg->iid, sizeof(msg->iid)); 903 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 904 msg->hdr.nexus.targ_port = port->targ_port; 905 msg->hdr.nexus.initid = iid; 906 msg->iid.in_use = port->wwpn_iid[iid].in_use; 907 msg->iid.name_len = l; 908 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 909 if (port->wwpn_iid[iid].name) 910 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 911 mtx_unlock(&softc->ctl_lock); 912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 913 free(msg, M_CTL); 914} 915 916void 917ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 918 uint8_t page, uint8_t subpage) 919{ 920 struct ctl_softc *softc = lun->ctl_softc; 921 union ctl_ha_msg msg; 922 u_int i; 923 924 if (softc->ha_link != CTL_HA_LINK_ONLINE) 925 return; 926 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 927 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 928 page && lun->mode_pages.index[i].subpage == subpage) 929 break; 930 } 931 if (i == CTL_NUM_MODE_PAGES) 932 return; 933 934 /* Don't try to replicate pages not present on this device. */ 935 if (lun->mode_pages.index[i].page_data == NULL) 936 return; 937 938 bzero(&msg.mode, sizeof(msg.mode)); 939 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 940 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 941 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 942 msg.hdr.nexus.targ_lun = lun->lun; 943 msg.hdr.nexus.targ_mapped_lun = lun->lun; 944 msg.mode.page_code = page; 945 msg.mode.subpage = subpage; 946 msg.mode.page_len = lun->mode_pages.index[i].page_len; 947 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 948 msg.mode.page_len); 949 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 950 M_WAITOK); 951} 952 953static void 954ctl_isc_ha_link_up(struct ctl_softc *softc) 955{ 956 struct ctl_port *port; 957 struct ctl_lun *lun; 958 union ctl_ha_msg msg; 959 int i; 960 961 /* Announce this node parameters to peer for validation. */ 962 msg.login.msg_type = CTL_MSG_LOGIN; 963 msg.login.version = CTL_HA_VERSION; 964 msg.login.ha_mode = softc->ha_mode; 965 msg.login.ha_id = softc->ha_id; 966 msg.login.max_luns = CTL_MAX_LUNS; 967 msg.login.max_ports = CTL_MAX_PORTS; 968 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 969 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 970 M_WAITOK); 971 972 STAILQ_FOREACH(port, &softc->port_list, links) { 973 ctl_isc_announce_port(port); 974 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 975 if (port->wwpn_iid[i].in_use) 976 ctl_isc_announce_iid(port, i); 977 } 978 } 979 STAILQ_FOREACH(lun, &softc->lun_list, links) 980 ctl_isc_announce_lun(lun); 981} 982 983static void 984ctl_isc_ha_link_down(struct ctl_softc *softc) 985{ 986 struct ctl_port *port; 987 struct ctl_lun *lun; 988 union ctl_io *io; 989 int i; 990 991 mtx_lock(&softc->ctl_lock); 992 STAILQ_FOREACH(lun, &softc->lun_list, links) { 993 mtx_lock(&lun->lun_lock); 994 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 995 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 996 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 997 } 998 mtx_unlock(&lun->lun_lock); 999 1000 mtx_unlock(&softc->ctl_lock); 1001 io = ctl_alloc_io(softc->othersc_pool); 1002 mtx_lock(&softc->ctl_lock); 1003 ctl_zero_io(io); 1004 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1005 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1006 ctl_enqueue_isc(io); 1007 } 1008 1009 STAILQ_FOREACH(port, &softc->port_list, links) { 1010 if (port->targ_port >= softc->port_min && 1011 port->targ_port < softc->port_max) 1012 continue; 1013 port->status &= ~CTL_PORT_STATUS_ONLINE; 1014 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1015 port->wwpn_iid[i].in_use = 0; 1016 free(port->wwpn_iid[i].name, M_CTL); 1017 port->wwpn_iid[i].name = NULL; 1018 } 1019 } 1020 mtx_unlock(&softc->ctl_lock); 1021} 1022 1023static void 1024ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1025{ 1026 struct ctl_lun *lun; 1027 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1028 1029 mtx_lock(&softc->ctl_lock); 1030 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS || 1031 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1032 mtx_unlock(&softc->ctl_lock); 1033 return; 1034 } 1035 mtx_lock(&lun->lun_lock); 1036 mtx_unlock(&softc->ctl_lock); 1037 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1038 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1039 if (msg->ua.ua_all) { 1040 if (msg->ua.ua_set) 1041 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1042 else 1043 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1044 } else { 1045 if (msg->ua.ua_set) 1046 ctl_est_ua(lun, iid, msg->ua.ua_type); 1047 else 1048 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1049 } 1050 mtx_unlock(&lun->lun_lock); 1051} 1052 1053static void 1054ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1055{ 1056 struct ctl_lun *lun; 1057 struct ctl_ha_msg_lun_pr_key pr_key; 1058 int i, k; 1059 ctl_lun_flags oflags; 1060 uint32_t targ_lun; 1061 1062 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1063 mtx_lock(&softc->ctl_lock); 1064 if (targ_lun >= CTL_MAX_LUNS || 1065 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1066 mtx_unlock(&softc->ctl_lock); 1067 return; 1068 } 1069 mtx_lock(&lun->lun_lock); 1070 mtx_unlock(&softc->ctl_lock); 1071 if (lun->flags & CTL_LUN_DISABLED) { 1072 mtx_unlock(&lun->lun_lock); 1073 return; 1074 } 1075 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1076 if (msg->lun.lun_devid_len != i || (i > 0 && 1077 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1078 mtx_unlock(&lun->lun_lock); 1079 printf("%s: Received conflicting HA LUN %d\n", 1080 __func__, targ_lun); 1081 return; 1082 } else { 1083 /* Record whether peer is primary. */ 1084 oflags = lun->flags; 1085 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1086 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1087 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1088 else 1089 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1090 if (oflags != lun->flags) 1091 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1092 1093 /* If peer is primary and we are not -- use data */ 1094 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1095 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1096 lun->pr_generation = msg->lun.pr_generation; 1097 lun->pr_res_idx = msg->lun.pr_res_idx; 1098 lun->pr_res_type = msg->lun.pr_res_type; 1099 lun->pr_key_count = msg->lun.pr_key_count; 1100 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1101 ctl_clr_prkey(lun, k); 1102 for (k = 0; k < msg->lun.pr_key_count; k++) { 1103 memcpy(&pr_key, &msg->lun.data[i], 1104 sizeof(pr_key)); 1105 ctl_alloc_prkey(lun, pr_key.pr_iid); 1106 ctl_set_prkey(lun, pr_key.pr_iid, 1107 pr_key.pr_key); 1108 i += sizeof(pr_key); 1109 } 1110 } 1111 1112 mtx_unlock(&lun->lun_lock); 1113 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1114 __func__, targ_lun, 1115 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1116 "primary" : "secondary")); 1117 1118 /* If we are primary but peer doesn't know -- notify */ 1119 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1120 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1121 ctl_isc_announce_lun(lun); 1122 } 1123} 1124 1125static void 1126ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1127{ 1128 struct ctl_port *port; 1129 struct ctl_lun *lun; 1130 int i, new; 1131 1132 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1133 if (port == NULL) { 1134 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1135 msg->hdr.nexus.targ_port)); 1136 new = 1; 1137 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1138 port->frontend = &ha_frontend; 1139 port->targ_port = msg->hdr.nexus.targ_port; 1140 port->fe_datamove = ctl_ha_datamove; 1141 port->fe_done = ctl_ha_done; 1142 } else if (port->frontend == &ha_frontend) { 1143 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1144 msg->hdr.nexus.targ_port)); 1145 new = 0; 1146 } else { 1147 printf("%s: Received conflicting HA port %d\n", 1148 __func__, msg->hdr.nexus.targ_port); 1149 return; 1150 } 1151 port->port_type = msg->port.port_type; 1152 port->physical_port = msg->port.physical_port; 1153 port->virtual_port = msg->port.virtual_port; 1154 port->status = msg->port.status; 1155 i = 0; 1156 free(port->port_name, M_CTL); 1157 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1158 M_CTL); 1159 i += msg->port.name_len; 1160 if (msg->port.lun_map_len != 0) { 1161 if (port->lun_map == NULL || 1162 port->lun_map_size * sizeof(uint32_t) < 1163 msg->port.lun_map_len) { 1164 port->lun_map_size = 0; 1165 free(port->lun_map, M_CTL); 1166 port->lun_map = malloc(msg->port.lun_map_len, 1167 M_CTL, M_WAITOK); 1168 } 1169 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1170 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1171 i += msg->port.lun_map_len; 1172 } else { 1173 port->lun_map_size = 0; 1174 free(port->lun_map, M_CTL); 1175 port->lun_map = NULL; 1176 } 1177 if (msg->port.port_devid_len != 0) { 1178 if (port->port_devid == NULL || 1179 port->port_devid->len < msg->port.port_devid_len) { 1180 free(port->port_devid, M_CTL); 1181 port->port_devid = malloc(sizeof(struct ctl_devid) + 1182 msg->port.port_devid_len, M_CTL, M_WAITOK); 1183 } 1184 memcpy(port->port_devid->data, &msg->port.data[i], 1185 msg->port.port_devid_len); 1186 port->port_devid->len = msg->port.port_devid_len; 1187 i += msg->port.port_devid_len; 1188 } else { 1189 free(port->port_devid, M_CTL); 1190 port->port_devid = NULL; 1191 } 1192 if (msg->port.target_devid_len != 0) { 1193 if (port->target_devid == NULL || 1194 port->target_devid->len < msg->port.target_devid_len) { 1195 free(port->target_devid, M_CTL); 1196 port->target_devid = malloc(sizeof(struct ctl_devid) + 1197 msg->port.target_devid_len, M_CTL, M_WAITOK); 1198 } 1199 memcpy(port->target_devid->data, &msg->port.data[i], 1200 msg->port.target_devid_len); 1201 port->target_devid->len = msg->port.target_devid_len; 1202 i += msg->port.target_devid_len; 1203 } else { 1204 free(port->target_devid, M_CTL); 1205 port->target_devid = NULL; 1206 } 1207 if (msg->port.init_devid_len != 0) { 1208 if (port->init_devid == NULL || 1209 port->init_devid->len < msg->port.init_devid_len) { 1210 free(port->init_devid, M_CTL); 1211 port->init_devid = malloc(sizeof(struct ctl_devid) + 1212 msg->port.init_devid_len, M_CTL, M_WAITOK); 1213 } 1214 memcpy(port->init_devid->data, &msg->port.data[i], 1215 msg->port.init_devid_len); 1216 port->init_devid->len = msg->port.init_devid_len; 1217 i += msg->port.init_devid_len; 1218 } else { 1219 free(port->init_devid, M_CTL); 1220 port->init_devid = NULL; 1221 } 1222 if (new) { 1223 if (ctl_port_register(port) != 0) { 1224 printf("%s: ctl_port_register() failed with error\n", 1225 __func__); 1226 } 1227 } 1228 mtx_lock(&softc->ctl_lock); 1229 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1230 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1231 continue; 1232 mtx_lock(&lun->lun_lock); 1233 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1234 mtx_unlock(&lun->lun_lock); 1235 } 1236 mtx_unlock(&softc->ctl_lock); 1237} 1238 1239static void 1240ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1241{ 1242 struct ctl_port *port; 1243 int iid; 1244 1245 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1246 if (port == NULL) { 1247 printf("%s: Received IID for unknown port %d\n", 1248 __func__, msg->hdr.nexus.targ_port); 1249 return; 1250 } 1251 iid = msg->hdr.nexus.initid; 1252 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1253 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1254 free(port->wwpn_iid[iid].name, M_CTL); 1255 if (msg->iid.name_len) { 1256 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1257 msg->iid.name_len, M_CTL); 1258 } else 1259 port->wwpn_iid[iid].name = NULL; 1260} 1261 1262static void 1263ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1264{ 1265 1266 if (msg->login.version != CTL_HA_VERSION) { 1267 printf("CTL HA peers have different versions %d != %d\n", 1268 msg->login.version, CTL_HA_VERSION); 1269 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1270 return; 1271 } 1272 if (msg->login.ha_mode != softc->ha_mode) { 1273 printf("CTL HA peers have different ha_mode %d != %d\n", 1274 msg->login.ha_mode, softc->ha_mode); 1275 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1276 return; 1277 } 1278 if (msg->login.ha_id == softc->ha_id) { 1279 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1280 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1281 return; 1282 } 1283 if (msg->login.max_luns != CTL_MAX_LUNS || 1284 msg->login.max_ports != CTL_MAX_PORTS || 1285 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1286 printf("CTL HA peers have different limits\n"); 1287 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1288 return; 1289 } 1290} 1291 1292static void 1293ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1294{ 1295 struct ctl_lun *lun; 1296 u_int i; 1297 uint32_t initidx, targ_lun; 1298 1299 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1300 mtx_lock(&softc->ctl_lock); 1301 if (targ_lun >= CTL_MAX_LUNS || 1302 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1303 mtx_unlock(&softc->ctl_lock); 1304 return; 1305 } 1306 mtx_lock(&lun->lun_lock); 1307 mtx_unlock(&softc->ctl_lock); 1308 if (lun->flags & CTL_LUN_DISABLED) { 1309 mtx_unlock(&lun->lun_lock); 1310 return; 1311 } 1312 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1313 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1314 msg->mode.page_code && 1315 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1316 break; 1317 } 1318 if (i == CTL_NUM_MODE_PAGES) { 1319 mtx_unlock(&lun->lun_lock); 1320 return; 1321 } 1322 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1323 lun->mode_pages.index[i].page_len); 1324 initidx = ctl_get_initindex(&msg->hdr.nexus); 1325 if (initidx != -1) 1326 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1327 mtx_unlock(&lun->lun_lock); 1328} 1329 1330/* 1331 * ISC (Inter Shelf Communication) event handler. Events from the HA 1332 * subsystem come in here. 1333 */ 1334static void 1335ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1336{ 1337 struct ctl_softc *softc = control_softc; 1338 union ctl_io *io; 1339 struct ctl_prio *presio; 1340 ctl_ha_status isc_status; 1341 1342 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1343 if (event == CTL_HA_EVT_MSG_RECV) { 1344 union ctl_ha_msg *msg, msgbuf; 1345 1346 if (param > sizeof(msgbuf)) 1347 msg = malloc(param, M_CTL, M_WAITOK); 1348 else 1349 msg = &msgbuf; 1350 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1351 M_WAITOK); 1352 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1353 printf("%s: Error receiving message: %d\n", 1354 __func__, isc_status); 1355 if (msg != &msgbuf) 1356 free(msg, M_CTL); 1357 return; 1358 } 1359 1360 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1361 switch (msg->hdr.msg_type) { 1362 case CTL_MSG_SERIALIZE: 1363 io = ctl_alloc_io(softc->othersc_pool); 1364 ctl_zero_io(io); 1365 // populate ctsio from msg 1366 io->io_hdr.io_type = CTL_IO_SCSI; 1367 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1368 io->io_hdr.original_sc = msg->hdr.original_sc; 1369 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1370 CTL_FLAG_IO_ACTIVE; 1371 /* 1372 * If we're in serialization-only mode, we don't 1373 * want to go through full done processing. Thus 1374 * the COPY flag. 1375 * 1376 * XXX KDM add another flag that is more specific. 1377 */ 1378 if (softc->ha_mode != CTL_HA_MODE_XFER) 1379 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1380 io->io_hdr.nexus = msg->hdr.nexus; 1381#if 0 1382 printf("port %u, iid %u, lun %u\n", 1383 io->io_hdr.nexus.targ_port, 1384 io->io_hdr.nexus.initid, 1385 io->io_hdr.nexus.targ_lun); 1386#endif 1387 io->scsiio.tag_num = msg->scsi.tag_num; 1388 io->scsiio.tag_type = msg->scsi.tag_type; 1389#ifdef CTL_TIME_IO 1390 io->io_hdr.start_time = time_uptime; 1391 getbinuptime(&io->io_hdr.start_bt); 1392#endif /* CTL_TIME_IO */ 1393 io->scsiio.cdb_len = msg->scsi.cdb_len; 1394 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1395 CTL_MAX_CDBLEN); 1396 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1397 const struct ctl_cmd_entry *entry; 1398 1399 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1400 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1401 io->io_hdr.flags |= 1402 entry->flags & CTL_FLAG_DATA_MASK; 1403 } 1404 ctl_enqueue_isc(io); 1405 break; 1406 1407 /* Performed on the Originating SC, XFER mode only */ 1408 case CTL_MSG_DATAMOVE: { 1409 struct ctl_sg_entry *sgl; 1410 int i, j; 1411 1412 io = msg->hdr.original_sc; 1413 if (io == NULL) { 1414 printf("%s: original_sc == NULL!\n", __func__); 1415 /* XXX KDM do something here */ 1416 break; 1417 } 1418 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1419 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1420 /* 1421 * Keep track of this, we need to send it back over 1422 * when the datamove is complete. 1423 */ 1424 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1425 if (msg->hdr.status == CTL_SUCCESS) 1426 io->io_hdr.status = msg->hdr.status; 1427 1428 if (msg->dt.sg_sequence == 0) { 1429#ifdef CTL_TIME_IO 1430 getbinuptime(&io->io_hdr.dma_start_bt); 1431#endif 1432 i = msg->dt.kern_sg_entries + 1433 msg->dt.kern_data_len / 1434 CTL_HA_DATAMOVE_SEGMENT + 1; 1435 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1436 M_WAITOK | M_ZERO); 1437 io->io_hdr.remote_sglist = sgl; 1438 io->io_hdr.local_sglist = 1439 &sgl[msg->dt.kern_sg_entries]; 1440 1441 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1442 1443 io->scsiio.kern_sg_entries = 1444 msg->dt.kern_sg_entries; 1445 io->scsiio.rem_sg_entries = 1446 msg->dt.kern_sg_entries; 1447 io->scsiio.kern_data_len = 1448 msg->dt.kern_data_len; 1449 io->scsiio.kern_total_len = 1450 msg->dt.kern_total_len; 1451 io->scsiio.kern_data_resid = 1452 msg->dt.kern_data_resid; 1453 io->scsiio.kern_rel_offset = 1454 msg->dt.kern_rel_offset; 1455 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1456 io->io_hdr.flags |= msg->dt.flags & 1457 CTL_FLAG_BUS_ADDR; 1458 } else 1459 sgl = (struct ctl_sg_entry *) 1460 io->scsiio.kern_data_ptr; 1461 1462 for (i = msg->dt.sent_sg_entries, j = 0; 1463 i < (msg->dt.sent_sg_entries + 1464 msg->dt.cur_sg_entries); i++, j++) { 1465 sgl[i].addr = msg->dt.sg_list[j].addr; 1466 sgl[i].len = msg->dt.sg_list[j].len; 1467 1468#if 0 1469 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1470 __func__, sgl[i].addr, sgl[i].len, j, i); 1471#endif 1472 } 1473 1474 /* 1475 * If this is the last piece of the I/O, we've got 1476 * the full S/G list. Queue processing in the thread. 1477 * Otherwise wait for the next piece. 1478 */ 1479 if (msg->dt.sg_last != 0) 1480 ctl_enqueue_isc(io); 1481 break; 1482 } 1483 /* Performed on the Serializing (primary) SC, XFER mode only */ 1484 case CTL_MSG_DATAMOVE_DONE: { 1485 if (msg->hdr.serializing_sc == NULL) { 1486 printf("%s: serializing_sc == NULL!\n", 1487 __func__); 1488 /* XXX KDM now what? */ 1489 break; 1490 } 1491 /* 1492 * We grab the sense information here in case 1493 * there was a failure, so we can return status 1494 * back to the initiator. 1495 */ 1496 io = msg->hdr.serializing_sc; 1497 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1498 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1499 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1500 io->io_hdr.port_status = msg->scsi.fetd_status; 1501 io->scsiio.residual = msg->scsi.residual; 1502 if (msg->hdr.status != CTL_STATUS_NONE) { 1503 io->io_hdr.status = msg->hdr.status; 1504 io->scsiio.scsi_status = msg->scsi.scsi_status; 1505 io->scsiio.sense_len = msg->scsi.sense_len; 1506 io->scsiio.sense_residual =msg->scsi.sense_residual; 1507 memcpy(&io->scsiio.sense_data, 1508 &msg->scsi.sense_data, 1509 msg->scsi.sense_len); 1510 if (msg->hdr.status == CTL_SUCCESS) 1511 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1512 } 1513 ctl_enqueue_isc(io); 1514 break; 1515 } 1516 1517 /* Preformed on Originating SC, SER_ONLY mode */ 1518 case CTL_MSG_R2R: 1519 io = msg->hdr.original_sc; 1520 if (io == NULL) { 1521 printf("%s: original_sc == NULL!\n", 1522 __func__); 1523 break; 1524 } 1525 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1526 io->io_hdr.msg_type = CTL_MSG_R2R; 1527 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1528 ctl_enqueue_isc(io); 1529 break; 1530 1531 /* 1532 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1533 * mode. 1534 * Performed on the Originating (i.e. secondary) SC in XFER 1535 * mode 1536 */ 1537 case CTL_MSG_FINISH_IO: 1538 if (softc->ha_mode == CTL_HA_MODE_XFER) 1539 ctl_isc_handler_finish_xfer(softc, msg); 1540 else 1541 ctl_isc_handler_finish_ser_only(softc, msg); 1542 break; 1543 1544 /* Preformed on Originating SC */ 1545 case CTL_MSG_BAD_JUJU: 1546 io = msg->hdr.original_sc; 1547 if (io == NULL) { 1548 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1549 __func__); 1550 break; 1551 } 1552 ctl_copy_sense_data(msg, io); 1553 /* 1554 * IO should have already been cleaned up on other 1555 * SC so clear this flag so we won't send a message 1556 * back to finish the IO there. 1557 */ 1558 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1559 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1560 1561 /* io = msg->hdr.serializing_sc; */ 1562 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1563 ctl_enqueue_isc(io); 1564 break; 1565 1566 /* Handle resets sent from the other side */ 1567 case CTL_MSG_MANAGE_TASKS: { 1568 struct ctl_taskio *taskio; 1569 taskio = (struct ctl_taskio *)ctl_alloc_io( 1570 softc->othersc_pool); 1571 ctl_zero_io((union ctl_io *)taskio); 1572 taskio->io_hdr.io_type = CTL_IO_TASK; 1573 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1574 taskio->io_hdr.nexus = msg->hdr.nexus; 1575 taskio->task_action = msg->task.task_action; 1576 taskio->tag_num = msg->task.tag_num; 1577 taskio->tag_type = msg->task.tag_type; 1578#ifdef CTL_TIME_IO 1579 taskio->io_hdr.start_time = time_uptime; 1580 getbinuptime(&taskio->io_hdr.start_bt); 1581#endif /* CTL_TIME_IO */ 1582 ctl_run_task((union ctl_io *)taskio); 1583 break; 1584 } 1585 /* Persistent Reserve action which needs attention */ 1586 case CTL_MSG_PERS_ACTION: 1587 presio = (struct ctl_prio *)ctl_alloc_io( 1588 softc->othersc_pool); 1589 ctl_zero_io((union ctl_io *)presio); 1590 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1591 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1592 presio->io_hdr.nexus = msg->hdr.nexus; 1593 presio->pr_msg = msg->pr; 1594 ctl_enqueue_isc((union ctl_io *)presio); 1595 break; 1596 case CTL_MSG_UA: 1597 ctl_isc_ua(softc, msg, param); 1598 break; 1599 case CTL_MSG_PORT_SYNC: 1600 ctl_isc_port_sync(softc, msg, param); 1601 break; 1602 case CTL_MSG_LUN_SYNC: 1603 ctl_isc_lun_sync(softc, msg, param); 1604 break; 1605 case CTL_MSG_IID_SYNC: 1606 ctl_isc_iid_sync(softc, msg, param); 1607 break; 1608 case CTL_MSG_LOGIN: 1609 ctl_isc_login(softc, msg, param); 1610 break; 1611 case CTL_MSG_MODE_SYNC: 1612 ctl_isc_mode_sync(softc, msg, param); 1613 break; 1614 default: 1615 printf("Received HA message of unknown type %d\n", 1616 msg->hdr.msg_type); 1617 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1618 break; 1619 } 1620 if (msg != &msgbuf) 1621 free(msg, M_CTL); 1622 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1623 printf("CTL: HA link status changed from %d to %d\n", 1624 softc->ha_link, param); 1625 if (param == softc->ha_link) 1626 return; 1627 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1628 softc->ha_link = param; 1629 ctl_isc_ha_link_down(softc); 1630 } else { 1631 softc->ha_link = param; 1632 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1633 ctl_isc_ha_link_up(softc); 1634 } 1635 return; 1636 } else { 1637 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1638 return; 1639 } 1640} 1641 1642static void 1643ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1644{ 1645 1646 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1647 src->scsi.sense_len); 1648 dest->scsiio.scsi_status = src->scsi.scsi_status; 1649 dest->scsiio.sense_len = src->scsi.sense_len; 1650 dest->io_hdr.status = src->hdr.status; 1651} 1652 1653static void 1654ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1655{ 1656 1657 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1658 src->scsiio.sense_len); 1659 dest->scsi.scsi_status = src->scsiio.scsi_status; 1660 dest->scsi.sense_len = src->scsiio.sense_len; 1661 dest->hdr.status = src->io_hdr.status; 1662} 1663 1664void 1665ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1666{ 1667 struct ctl_softc *softc = lun->ctl_softc; 1668 ctl_ua_type *pu; 1669 1670 if (initidx < softc->init_min || initidx >= softc->init_max) 1671 return; 1672 mtx_assert(&lun->lun_lock, MA_OWNED); 1673 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1674 if (pu == NULL) 1675 return; 1676 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1677} 1678 1679void 1680ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1681{ 1682 int i; 1683 1684 mtx_assert(&lun->lun_lock, MA_OWNED); 1685 if (lun->pending_ua[port] == NULL) 1686 return; 1687 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1688 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1689 continue; 1690 lun->pending_ua[port][i] |= ua; 1691 } 1692} 1693 1694void 1695ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1696{ 1697 struct ctl_softc *softc = lun->ctl_softc; 1698 int i; 1699 1700 mtx_assert(&lun->lun_lock, MA_OWNED); 1701 for (i = softc->port_min; i < softc->port_max; i++) 1702 ctl_est_ua_port(lun, i, except, ua); 1703} 1704 1705void 1706ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1707{ 1708 struct ctl_softc *softc = lun->ctl_softc; 1709 ctl_ua_type *pu; 1710 1711 if (initidx < softc->init_min || initidx >= softc->init_max) 1712 return; 1713 mtx_assert(&lun->lun_lock, MA_OWNED); 1714 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1715 if (pu == NULL) 1716 return; 1717 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1718} 1719 1720void 1721ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1722{ 1723 struct ctl_softc *softc = lun->ctl_softc; 1724 int i, j; 1725 1726 mtx_assert(&lun->lun_lock, MA_OWNED); 1727 for (i = softc->port_min; i < softc->port_max; i++) { 1728 if (lun->pending_ua[i] == NULL) 1729 continue; 1730 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1731 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1732 continue; 1733 lun->pending_ua[i][j] &= ~ua; 1734 } 1735 } 1736} 1737 1738void 1739ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1740 ctl_ua_type ua_type) 1741{ 1742 struct ctl_lun *lun; 1743 1744 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1745 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1746 mtx_lock(&lun->lun_lock); 1747 ctl_clr_ua(lun, initidx, ua_type); 1748 mtx_unlock(&lun->lun_lock); 1749 } 1750} 1751 1752static int 1753ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1754{ 1755 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1756 struct ctl_lun *lun; 1757 struct ctl_lun_req ireq; 1758 int error, value; 1759 1760 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1761 error = sysctl_handle_int(oidp, &value, 0, req); 1762 if ((error != 0) || (req->newptr == NULL)) 1763 return (error); 1764 1765 mtx_lock(&softc->ctl_lock); 1766 if (value == 0) 1767 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1768 else 1769 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1770 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1771 mtx_unlock(&softc->ctl_lock); 1772 bzero(&ireq, sizeof(ireq)); 1773 ireq.reqtype = CTL_LUNREQ_MODIFY; 1774 ireq.reqdata.modify.lun_id = lun->lun; 1775 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1776 curthread); 1777 if (ireq.status != CTL_LUN_OK) { 1778 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1779 __func__, ireq.status, ireq.error_str); 1780 } 1781 mtx_lock(&softc->ctl_lock); 1782 } 1783 mtx_unlock(&softc->ctl_lock); 1784 return (0); 1785} 1786 1787static int 1788ctl_init(void) 1789{ 1790 struct make_dev_args args; 1791 struct ctl_softc *softc; 1792 void *other_pool; 1793 int i, error; 1794 1795 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1796 M_WAITOK | M_ZERO); 1797 1798 make_dev_args_init(&args); 1799 args.mda_devsw = &ctl_cdevsw; 1800 args.mda_uid = UID_ROOT; 1801 args.mda_gid = GID_OPERATOR; 1802 args.mda_mode = 0600; 1803 args.mda_si_drv1 = softc; 1804 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1805 if (error != 0) { 1806 free(softc, M_DEVBUF); 1807 control_softc = NULL; 1808 return (error); 1809 } 1810 1811 sysctl_ctx_init(&softc->sysctl_ctx); 1812 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1813 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1814 CTLFLAG_RD, 0, "CAM Target Layer"); 1815 1816 if (softc->sysctl_tree == NULL) { 1817 printf("%s: unable to allocate sysctl tree\n", __func__); 1818 destroy_dev(softc->dev); 1819 free(softc, M_DEVBUF); 1820 control_softc = NULL; 1821 return (ENOMEM); 1822 } 1823 1824 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1825 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1826 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1827 softc->flags = 0; 1828 1829 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode); 1830 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1831 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1832 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1833 1834 /* 1835 * In Copan's HA scheme, the "master" and "slave" roles are 1836 * figured out through the slot the controller is in. Although it 1837 * is an active/active system, someone has to be in charge. 1838 */ 1839 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id); 1840 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1841 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1842 "HA head ID (0 - no HA)"); 1843 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1844 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1845 softc->is_single = 1; 1846 softc->port_cnt = CTL_MAX_PORTS; 1847 softc->port_min = 0; 1848 } else { 1849 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES; 1850 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1851 } 1852 softc->port_max = softc->port_min + softc->port_cnt; 1853 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1854 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1855 1856 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1857 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1858 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1859 1860 STAILQ_INIT(&softc->lun_list); 1861 STAILQ_INIT(&softc->pending_lun_queue); 1862 STAILQ_INIT(&softc->fe_list); 1863 STAILQ_INIT(&softc->port_list); 1864 STAILQ_INIT(&softc->be_list); 1865 ctl_tpc_init(softc); 1866 1867 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1868 &other_pool) != 0) 1869 { 1870 printf("ctl: can't allocate %d entry other SC pool, " 1871 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1872 return (ENOMEM); 1873 } 1874 softc->othersc_pool = other_pool; 1875 1876 if (worker_threads <= 0) 1877 worker_threads = max(1, mp_ncpus / 4); 1878 if (worker_threads > CTL_MAX_THREADS) 1879 worker_threads = CTL_MAX_THREADS; 1880 1881 for (i = 0; i < worker_threads; i++) { 1882 struct ctl_thread *thr = &softc->threads[i]; 1883 1884 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1885 thr->ctl_softc = softc; 1886 STAILQ_INIT(&thr->incoming_queue); 1887 STAILQ_INIT(&thr->rtr_queue); 1888 STAILQ_INIT(&thr->done_queue); 1889 STAILQ_INIT(&thr->isc_queue); 1890 1891 error = kproc_kthread_add(ctl_work_thread, thr, 1892 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1893 if (error != 0) { 1894 printf("error creating CTL work thread!\n"); 1895 ctl_pool_free(other_pool); 1896 return (error); 1897 } 1898 } 1899 error = kproc_kthread_add(ctl_lun_thread, softc, 1900 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1901 if (error != 0) { 1902 printf("error creating CTL lun thread!\n"); 1903 ctl_pool_free(other_pool); 1904 return (error); 1905 } 1906 error = kproc_kthread_add(ctl_thresh_thread, softc, 1907 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1908 if (error != 0) { 1909 printf("error creating CTL threshold thread!\n"); 1910 ctl_pool_free(other_pool); 1911 return (error); 1912 } 1913 1914 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1915 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1916 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1917 1918 if (softc->is_single == 0) { 1919 ctl_frontend_register(&ha_frontend); 1920 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1921 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1922 softc->is_single = 1; 1923 } else 1924 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1925 != CTL_HA_STATUS_SUCCESS) { 1926 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1927 softc->is_single = 1; 1928 } 1929 } 1930 return (0); 1931} 1932 1933void 1934ctl_shutdown(void) 1935{ 1936 struct ctl_softc *softc = control_softc; 1937 struct ctl_lun *lun, *next_lun; 1938 1939 if (softc->is_single == 0) { 1940 ctl_ha_msg_shutdown(softc); 1941 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1942 != CTL_HA_STATUS_SUCCESS) 1943 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1944 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1945 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1946 ctl_frontend_deregister(&ha_frontend); 1947 } 1948 1949 mtx_lock(&softc->ctl_lock); 1950 1951 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1952 ctl_free_lun(lun); 1953 1954 mtx_unlock(&softc->ctl_lock); 1955 1956#if 0 1957 ctl_shutdown_thread(softc->work_thread); 1958 mtx_destroy(&softc->queue_lock); 1959#endif 1960 1961 ctl_tpc_shutdown(softc); 1962 uma_zdestroy(softc->io_zone); 1963 mtx_destroy(&softc->ctl_lock); 1964 1965 destroy_dev(softc->dev); 1966 1967 sysctl_ctx_free(&softc->sysctl_ctx); 1968 1969 free(softc, M_DEVBUF); 1970 control_softc = NULL; 1971} 1972 1973static int 1974ctl_module_event_handler(module_t mod, int what, void *arg) 1975{ 1976 1977 switch (what) { 1978 case MOD_LOAD: 1979 return (ctl_init()); 1980 case MOD_UNLOAD: 1981 return (EBUSY); 1982 default: 1983 return (EOPNOTSUPP); 1984 } 1985} 1986 1987/* 1988 * XXX KDM should we do some access checks here? Bump a reference count to 1989 * prevent a CTL module from being unloaded while someone has it open? 1990 */ 1991static int 1992ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1993{ 1994 return (0); 1995} 1996 1997static int 1998ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1999{ 2000 return (0); 2001} 2002 2003/* 2004 * Remove an initiator by port number and initiator ID. 2005 * Returns 0 for success, -1 for failure. 2006 */ 2007int 2008ctl_remove_initiator(struct ctl_port *port, int iid) 2009{ 2010 struct ctl_softc *softc = port->ctl_softc; 2011 2012 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2013 2014 if (iid > CTL_MAX_INIT_PER_PORT) { 2015 printf("%s: initiator ID %u > maximun %u!\n", 2016 __func__, iid, CTL_MAX_INIT_PER_PORT); 2017 return (-1); 2018 } 2019 2020 mtx_lock(&softc->ctl_lock); 2021 port->wwpn_iid[iid].in_use--; 2022 port->wwpn_iid[iid].last_use = time_uptime; 2023 mtx_unlock(&softc->ctl_lock); 2024 ctl_isc_announce_iid(port, iid); 2025 2026 return (0); 2027} 2028 2029/* 2030 * Add an initiator to the initiator map. 2031 * Returns iid for success, < 0 for failure. 2032 */ 2033int 2034ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2035{ 2036 struct ctl_softc *softc = port->ctl_softc; 2037 time_t best_time; 2038 int i, best; 2039 2040 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2041 2042 if (iid >= CTL_MAX_INIT_PER_PORT) { 2043 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2044 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2045 free(name, M_CTL); 2046 return (-1); 2047 } 2048 2049 mtx_lock(&softc->ctl_lock); 2050 2051 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2052 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2053 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2054 iid = i; 2055 break; 2056 } 2057 if (name != NULL && port->wwpn_iid[i].name != NULL && 2058 strcmp(name, port->wwpn_iid[i].name) == 0) { 2059 iid = i; 2060 break; 2061 } 2062 } 2063 } 2064 2065 if (iid < 0) { 2066 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2067 if (port->wwpn_iid[i].in_use == 0 && 2068 port->wwpn_iid[i].wwpn == 0 && 2069 port->wwpn_iid[i].name == NULL) { 2070 iid = i; 2071 break; 2072 } 2073 } 2074 } 2075 2076 if (iid < 0) { 2077 best = -1; 2078 best_time = INT32_MAX; 2079 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2080 if (port->wwpn_iid[i].in_use == 0) { 2081 if (port->wwpn_iid[i].last_use < best_time) { 2082 best = i; 2083 best_time = port->wwpn_iid[i].last_use; 2084 } 2085 } 2086 } 2087 iid = best; 2088 } 2089 2090 if (iid < 0) { 2091 mtx_unlock(&softc->ctl_lock); 2092 free(name, M_CTL); 2093 return (-2); 2094 } 2095 2096 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2097 /* 2098 * This is not an error yet. 2099 */ 2100 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2101#if 0 2102 printf("%s: port %d iid %u WWPN %#jx arrived" 2103 " again\n", __func__, port->targ_port, 2104 iid, (uintmax_t)wwpn); 2105#endif 2106 goto take; 2107 } 2108 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2109 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2110#if 0 2111 printf("%s: port %d iid %u name '%s' arrived" 2112 " again\n", __func__, port->targ_port, 2113 iid, name); 2114#endif 2115 goto take; 2116 } 2117 2118 /* 2119 * This is an error, but what do we do about it? The 2120 * driver is telling us we have a new WWPN for this 2121 * initiator ID, so we pretty much need to use it. 2122 */ 2123 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2124 " but WWPN %#jx '%s' is still at that address\n", 2125 __func__, port->targ_port, iid, wwpn, name, 2126 (uintmax_t)port->wwpn_iid[iid].wwpn, 2127 port->wwpn_iid[iid].name); 2128 2129 /* 2130 * XXX KDM clear have_ca and ua_pending on each LUN for 2131 * this initiator. 2132 */ 2133 } 2134take: 2135 free(port->wwpn_iid[iid].name, M_CTL); 2136 port->wwpn_iid[iid].name = name; 2137 port->wwpn_iid[iid].wwpn = wwpn; 2138 port->wwpn_iid[iid].in_use++; 2139 mtx_unlock(&softc->ctl_lock); 2140 ctl_isc_announce_iid(port, iid); 2141 2142 return (iid); 2143} 2144 2145static int 2146ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2147{ 2148 int len; 2149 2150 switch (port->port_type) { 2151 case CTL_PORT_FC: 2152 { 2153 struct scsi_transportid_fcp *id = 2154 (struct scsi_transportid_fcp *)buf; 2155 if (port->wwpn_iid[iid].wwpn == 0) 2156 return (0); 2157 memset(id, 0, sizeof(*id)); 2158 id->format_protocol = SCSI_PROTO_FC; 2159 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2160 return (sizeof(*id)); 2161 } 2162 case CTL_PORT_ISCSI: 2163 { 2164 struct scsi_transportid_iscsi_port *id = 2165 (struct scsi_transportid_iscsi_port *)buf; 2166 if (port->wwpn_iid[iid].name == NULL) 2167 return (0); 2168 memset(id, 0, 256); 2169 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2170 SCSI_PROTO_ISCSI; 2171 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2172 len = roundup2(min(len, 252), 4); 2173 scsi_ulto2b(len, id->additional_length); 2174 return (sizeof(*id) + len); 2175 } 2176 case CTL_PORT_SAS: 2177 { 2178 struct scsi_transportid_sas *id = 2179 (struct scsi_transportid_sas *)buf; 2180 if (port->wwpn_iid[iid].wwpn == 0) 2181 return (0); 2182 memset(id, 0, sizeof(*id)); 2183 id->format_protocol = SCSI_PROTO_SAS; 2184 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2185 return (sizeof(*id)); 2186 } 2187 default: 2188 { 2189 struct scsi_transportid_spi *id = 2190 (struct scsi_transportid_spi *)buf; 2191 memset(id, 0, sizeof(*id)); 2192 id->format_protocol = SCSI_PROTO_SPI; 2193 scsi_ulto2b(iid, id->scsi_addr); 2194 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2195 return (sizeof(*id)); 2196 } 2197 } 2198} 2199 2200/* 2201 * Serialize a command that went down the "wrong" side, and so was sent to 2202 * this controller for execution. The logic is a little different than the 2203 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2204 * sent back to the other side, but in the success case, we execute the 2205 * command on this side (XFER mode) or tell the other side to execute it 2206 * (SER_ONLY mode). 2207 */ 2208static void 2209ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2210{ 2211 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2212 struct ctl_port *port = CTL_PORT(ctsio); 2213 union ctl_ha_msg msg_info; 2214 struct ctl_lun *lun; 2215 const struct ctl_cmd_entry *entry; 2216 uint32_t targ_lun; 2217 2218 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2219 2220 /* Make sure that we know about this port. */ 2221 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2222 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2223 /*retry_count*/ 1); 2224 goto badjuju; 2225 } 2226 2227 /* Make sure that we know about this LUN. */ 2228 mtx_lock(&softc->ctl_lock); 2229 if (targ_lun >= CTL_MAX_LUNS || 2230 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2231 mtx_unlock(&softc->ctl_lock); 2232 2233 /* 2234 * The other node would not send this request to us unless 2235 * received announce that we are primary node for this LUN. 2236 * If this LUN does not exist now, it is probably result of 2237 * a race, so respond to initiator in the most opaque way. 2238 */ 2239 ctl_set_busy(ctsio); 2240 goto badjuju; 2241 } 2242 mtx_lock(&lun->lun_lock); 2243 mtx_unlock(&softc->ctl_lock); 2244 2245 /* 2246 * If the LUN is invalid, pretend that it doesn't exist. 2247 * It will go away as soon as all pending I/Os completed. 2248 */ 2249 if (lun->flags & CTL_LUN_DISABLED) { 2250 mtx_unlock(&lun->lun_lock); 2251 ctl_set_busy(ctsio); 2252 goto badjuju; 2253 } 2254 2255 entry = ctl_get_cmd_entry(ctsio, NULL); 2256 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2257 mtx_unlock(&lun->lun_lock); 2258 goto badjuju; 2259 } 2260 2261 CTL_LUN(ctsio) = lun; 2262 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2263 2264 /* 2265 * Every I/O goes into the OOA queue for a 2266 * particular LUN, and stays there until completion. 2267 */ 2268#ifdef CTL_TIME_IO 2269 if (TAILQ_EMPTY(&lun->ooa_queue)) 2270 lun->idle_time += getsbinuptime() - lun->last_busy; 2271#endif 2272 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2273 2274 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2275 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2276 ooa_links))) { 2277 case CTL_ACTION_BLOCK: 2278 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2279 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2280 blocked_links); 2281 mtx_unlock(&lun->lun_lock); 2282 break; 2283 case CTL_ACTION_PASS: 2284 case CTL_ACTION_SKIP: 2285 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2286 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2287 ctl_enqueue_rtr((union ctl_io *)ctsio); 2288 mtx_unlock(&lun->lun_lock); 2289 } else { 2290 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2291 mtx_unlock(&lun->lun_lock); 2292 2293 /* send msg back to other side */ 2294 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2295 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2296 msg_info.hdr.msg_type = CTL_MSG_R2R; 2297 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2298 sizeof(msg_info.hdr), M_WAITOK); 2299 } 2300 break; 2301 case CTL_ACTION_OVERLAP: 2302 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2303 mtx_unlock(&lun->lun_lock); 2304 ctl_set_overlapped_cmd(ctsio); 2305 goto badjuju; 2306 case CTL_ACTION_OVERLAP_TAG: 2307 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2308 mtx_unlock(&lun->lun_lock); 2309 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2310 goto badjuju; 2311 case CTL_ACTION_ERROR: 2312 default: 2313 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2314 mtx_unlock(&lun->lun_lock); 2315 2316 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2317 /*retry_count*/ 0); 2318badjuju: 2319 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2320 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2321 msg_info.hdr.serializing_sc = NULL; 2322 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2323 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2324 sizeof(msg_info.scsi), M_WAITOK); 2325 ctl_free_io((union ctl_io *)ctsio); 2326 break; 2327 } 2328} 2329 2330/* 2331 * Returns 0 for success, errno for failure. 2332 */ 2333static void 2334ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2335 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2336{ 2337 union ctl_io *io; 2338 2339 mtx_lock(&lun->lun_lock); 2340 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2341 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2342 ooa_links)) { 2343 struct ctl_ooa_entry *entry; 2344 2345 /* 2346 * If we've got more than we can fit, just count the 2347 * remaining entries. 2348 */ 2349 if (*cur_fill_num >= ooa_hdr->alloc_num) 2350 continue; 2351 2352 entry = &kern_entries[*cur_fill_num]; 2353 2354 entry->tag_num = io->scsiio.tag_num; 2355 entry->lun_num = lun->lun; 2356#ifdef CTL_TIME_IO 2357 entry->start_bt = io->io_hdr.start_bt; 2358#endif 2359 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2360 entry->cdb_len = io->scsiio.cdb_len; 2361 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2362 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2363 2364 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2365 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2366 2367 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2368 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2369 2370 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2371 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2372 2373 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2374 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2375 } 2376 mtx_unlock(&lun->lun_lock); 2377} 2378 2379static void * 2380ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2381 size_t error_str_len) 2382{ 2383 void *kptr; 2384 2385 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2386 2387 if (copyin(user_addr, kptr, len) != 0) { 2388 snprintf(error_str, error_str_len, "Error copying %d bytes " 2389 "from user address %p to kernel address %p", len, 2390 user_addr, kptr); 2391 free(kptr, M_CTL); 2392 return (NULL); 2393 } 2394 2395 return (kptr); 2396} 2397 2398static void 2399ctl_free_args(int num_args, struct ctl_be_arg *args) 2400{ 2401 int i; 2402 2403 if (args == NULL) 2404 return; 2405 2406 for (i = 0; i < num_args; i++) { 2407 free(args[i].kname, M_CTL); 2408 free(args[i].kvalue, M_CTL); 2409 } 2410 2411 free(args, M_CTL); 2412} 2413 2414static struct ctl_be_arg * 2415ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2416 char *error_str, size_t error_str_len) 2417{ 2418 struct ctl_be_arg *args; 2419 int i; 2420 2421 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2422 error_str, error_str_len); 2423 2424 if (args == NULL) 2425 goto bailout; 2426 2427 for (i = 0; i < num_args; i++) { 2428 args[i].kname = NULL; 2429 args[i].kvalue = NULL; 2430 } 2431 2432 for (i = 0; i < num_args; i++) { 2433 uint8_t *tmpptr; 2434 2435 args[i].kname = ctl_copyin_alloc(args[i].name, 2436 args[i].namelen, error_str, error_str_len); 2437 if (args[i].kname == NULL) 2438 goto bailout; 2439 2440 if (args[i].kname[args[i].namelen - 1] != '\0') { 2441 snprintf(error_str, error_str_len, "Argument %d " 2442 "name is not NUL-terminated", i); 2443 goto bailout; 2444 } 2445 2446 if (args[i].flags & CTL_BEARG_RD) { 2447 tmpptr = ctl_copyin_alloc(args[i].value, 2448 args[i].vallen, error_str, error_str_len); 2449 if (tmpptr == NULL) 2450 goto bailout; 2451 if ((args[i].flags & CTL_BEARG_ASCII) 2452 && (tmpptr[args[i].vallen - 1] != '\0')) { 2453 snprintf(error_str, error_str_len, "Argument " 2454 "%d value is not NUL-terminated", i); 2455 free(tmpptr, M_CTL); 2456 goto bailout; 2457 } 2458 args[i].kvalue = tmpptr; 2459 } else { 2460 args[i].kvalue = malloc(args[i].vallen, 2461 M_CTL, M_WAITOK | M_ZERO); 2462 } 2463 } 2464 2465 return (args); 2466bailout: 2467 2468 ctl_free_args(num_args, args); 2469 2470 return (NULL); 2471} 2472 2473static void 2474ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2475{ 2476 int i; 2477 2478 for (i = 0; i < num_args; i++) { 2479 if (args[i].flags & CTL_BEARG_WR) 2480 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2481 } 2482} 2483 2484/* 2485 * Escape characters that are illegal or not recommended in XML. 2486 */ 2487int 2488ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2489{ 2490 char *end = str + size; 2491 int retval; 2492 2493 retval = 0; 2494 2495 for (; *str && str < end; str++) { 2496 switch (*str) { 2497 case '&': 2498 retval = sbuf_printf(sb, "&"); 2499 break; 2500 case '>': 2501 retval = sbuf_printf(sb, ">"); 2502 break; 2503 case '<': 2504 retval = sbuf_printf(sb, "<"); 2505 break; 2506 default: 2507 retval = sbuf_putc(sb, *str); 2508 break; 2509 } 2510 2511 if (retval != 0) 2512 break; 2513 2514 } 2515 2516 return (retval); 2517} 2518 2519static void 2520ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2521{ 2522 struct scsi_vpd_id_descriptor *desc; 2523 int i; 2524 2525 if (id == NULL || id->len < 4) 2526 return; 2527 desc = (struct scsi_vpd_id_descriptor *)id->data; 2528 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2529 case SVPD_ID_TYPE_T10: 2530 sbuf_printf(sb, "t10."); 2531 break; 2532 case SVPD_ID_TYPE_EUI64: 2533 sbuf_printf(sb, "eui."); 2534 break; 2535 case SVPD_ID_TYPE_NAA: 2536 sbuf_printf(sb, "naa."); 2537 break; 2538 case SVPD_ID_TYPE_SCSI_NAME: 2539 break; 2540 } 2541 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2542 case SVPD_ID_CODESET_BINARY: 2543 for (i = 0; i < desc->length; i++) 2544 sbuf_printf(sb, "%02x", desc->identifier[i]); 2545 break; 2546 case SVPD_ID_CODESET_ASCII: 2547 sbuf_printf(sb, "%.*s", (int)desc->length, 2548 (char *)desc->identifier); 2549 break; 2550 case SVPD_ID_CODESET_UTF8: 2551 sbuf_printf(sb, "%s", (char *)desc->identifier); 2552 break; 2553 } 2554} 2555 2556static int 2557ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2558 struct thread *td) 2559{ 2560 struct ctl_softc *softc = dev->si_drv1; 2561 struct ctl_port *port; 2562 struct ctl_lun *lun; 2563 int retval; 2564 2565 retval = 0; 2566 2567 switch (cmd) { 2568 case CTL_IO: 2569 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2570 break; 2571 case CTL_ENABLE_PORT: 2572 case CTL_DISABLE_PORT: 2573 case CTL_SET_PORT_WWNS: { 2574 struct ctl_port *port; 2575 struct ctl_port_entry *entry; 2576 2577 entry = (struct ctl_port_entry *)addr; 2578 2579 mtx_lock(&softc->ctl_lock); 2580 STAILQ_FOREACH(port, &softc->port_list, links) { 2581 int action, done; 2582 2583 if (port->targ_port < softc->port_min || 2584 port->targ_port >= softc->port_max) 2585 continue; 2586 2587 action = 0; 2588 done = 0; 2589 if ((entry->port_type == CTL_PORT_NONE) 2590 && (entry->targ_port == port->targ_port)) { 2591 /* 2592 * If the user only wants to enable or 2593 * disable or set WWNs on a specific port, 2594 * do the operation and we're done. 2595 */ 2596 action = 1; 2597 done = 1; 2598 } else if (entry->port_type & port->port_type) { 2599 /* 2600 * Compare the user's type mask with the 2601 * particular frontend type to see if we 2602 * have a match. 2603 */ 2604 action = 1; 2605 done = 0; 2606 2607 /* 2608 * Make sure the user isn't trying to set 2609 * WWNs on multiple ports at the same time. 2610 */ 2611 if (cmd == CTL_SET_PORT_WWNS) { 2612 printf("%s: Can't set WWNs on " 2613 "multiple ports\n", __func__); 2614 retval = EINVAL; 2615 break; 2616 } 2617 } 2618 if (action == 0) 2619 continue; 2620 2621 /* 2622 * XXX KDM we have to drop the lock here, because 2623 * the online/offline operations can potentially 2624 * block. We need to reference count the frontends 2625 * so they can't go away, 2626 */ 2627 if (cmd == CTL_ENABLE_PORT) { 2628 mtx_unlock(&softc->ctl_lock); 2629 ctl_port_online(port); 2630 mtx_lock(&softc->ctl_lock); 2631 } else if (cmd == CTL_DISABLE_PORT) { 2632 mtx_unlock(&softc->ctl_lock); 2633 ctl_port_offline(port); 2634 mtx_lock(&softc->ctl_lock); 2635 } else if (cmd == CTL_SET_PORT_WWNS) { 2636 ctl_port_set_wwns(port, 2637 (entry->flags & CTL_PORT_WWNN_VALID) ? 2638 1 : 0, entry->wwnn, 2639 (entry->flags & CTL_PORT_WWPN_VALID) ? 2640 1 : 0, entry->wwpn); 2641 } 2642 if (done != 0) 2643 break; 2644 } 2645 mtx_unlock(&softc->ctl_lock); 2646 break; 2647 } 2648 case CTL_GET_OOA: { 2649 struct ctl_ooa *ooa_hdr; 2650 struct ctl_ooa_entry *entries; 2651 uint32_t cur_fill_num; 2652 2653 ooa_hdr = (struct ctl_ooa *)addr; 2654 2655 if ((ooa_hdr->alloc_len == 0) 2656 || (ooa_hdr->alloc_num == 0)) { 2657 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2658 "must be non-zero\n", __func__, 2659 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2660 retval = EINVAL; 2661 break; 2662 } 2663 2664 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2665 sizeof(struct ctl_ooa_entry))) { 2666 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2667 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2668 __func__, ooa_hdr->alloc_len, 2669 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2670 retval = EINVAL; 2671 break; 2672 } 2673 2674 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2675 if (entries == NULL) { 2676 printf("%s: could not allocate %d bytes for OOA " 2677 "dump\n", __func__, ooa_hdr->alloc_len); 2678 retval = ENOMEM; 2679 break; 2680 } 2681 2682 mtx_lock(&softc->ctl_lock); 2683 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2684 (ooa_hdr->lun_num >= CTL_MAX_LUNS || 2685 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2686 mtx_unlock(&softc->ctl_lock); 2687 free(entries, M_CTL); 2688 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2689 __func__, (uintmax_t)ooa_hdr->lun_num); 2690 retval = EINVAL; 2691 break; 2692 } 2693 2694 cur_fill_num = 0; 2695 2696 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2697 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2698 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2699 ooa_hdr, entries); 2700 } 2701 } else { 2702 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2703 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2704 entries); 2705 } 2706 mtx_unlock(&softc->ctl_lock); 2707 2708 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2709 ooa_hdr->fill_len = ooa_hdr->fill_num * 2710 sizeof(struct ctl_ooa_entry); 2711 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2712 if (retval != 0) { 2713 printf("%s: error copying out %d bytes for OOA dump\n", 2714 __func__, ooa_hdr->fill_len); 2715 } 2716 2717 getbinuptime(&ooa_hdr->cur_bt); 2718 2719 if (cur_fill_num > ooa_hdr->alloc_num) { 2720 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2721 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2722 } else { 2723 ooa_hdr->dropped_num = 0; 2724 ooa_hdr->status = CTL_OOA_OK; 2725 } 2726 2727 free(entries, M_CTL); 2728 break; 2729 } 2730 case CTL_DELAY_IO: { 2731 struct ctl_io_delay_info *delay_info; 2732 2733 delay_info = (struct ctl_io_delay_info *)addr; 2734 2735#ifdef CTL_IO_DELAY 2736 mtx_lock(&softc->ctl_lock); 2737 if (delay_info->lun_id >= CTL_MAX_LUNS || 2738 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2739 mtx_unlock(&softc->ctl_lock); 2740 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2741 break; 2742 } 2743 mtx_lock(&lun->lun_lock); 2744 mtx_unlock(&softc->ctl_lock); 2745 delay_info->status = CTL_DELAY_STATUS_OK; 2746 switch (delay_info->delay_type) { 2747 case CTL_DELAY_TYPE_CONT: 2748 case CTL_DELAY_TYPE_ONESHOT: 2749 break; 2750 default: 2751 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2752 break; 2753 } 2754 switch (delay_info->delay_loc) { 2755 case CTL_DELAY_LOC_DATAMOVE: 2756 lun->delay_info.datamove_type = delay_info->delay_type; 2757 lun->delay_info.datamove_delay = delay_info->delay_secs; 2758 break; 2759 case CTL_DELAY_LOC_DONE: 2760 lun->delay_info.done_type = delay_info->delay_type; 2761 lun->delay_info.done_delay = delay_info->delay_secs; 2762 break; 2763 default: 2764 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2765 break; 2766 } 2767 mtx_unlock(&lun->lun_lock); 2768#else 2769 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2770#endif /* CTL_IO_DELAY */ 2771 break; 2772 } 2773#ifdef CTL_LEGACY_STATS 2774 case CTL_GETSTATS: { 2775 struct ctl_stats *stats = (struct ctl_stats *)addr; 2776 int i; 2777 2778 /* 2779 * XXX KDM no locking here. If the LUN list changes, 2780 * things can blow up. 2781 */ 2782 i = 0; 2783 stats->status = CTL_SS_OK; 2784 stats->fill_len = 0; 2785 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2786 if (stats->fill_len + sizeof(lun->legacy_stats) > 2787 stats->alloc_len) { 2788 stats->status = CTL_SS_NEED_MORE_SPACE; 2789 break; 2790 } 2791 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++], 2792 sizeof(lun->legacy_stats)); 2793 if (retval != 0) 2794 break; 2795 stats->fill_len += sizeof(lun->legacy_stats); 2796 } 2797 stats->num_luns = softc->num_luns; 2798 stats->flags = CTL_STATS_FLAG_NONE; 2799#ifdef CTL_TIME_IO 2800 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 2801#endif 2802 getnanouptime(&stats->timestamp); 2803 break; 2804 } 2805#endif /* CTL_LEGACY_STATS */ 2806 case CTL_ERROR_INJECT: { 2807 struct ctl_error_desc *err_desc, *new_err_desc; 2808 2809 err_desc = (struct ctl_error_desc *)addr; 2810 2811 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2812 M_WAITOK | M_ZERO); 2813 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2814 2815 mtx_lock(&softc->ctl_lock); 2816 if (err_desc->lun_id >= CTL_MAX_LUNS || 2817 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2818 mtx_unlock(&softc->ctl_lock); 2819 free(new_err_desc, M_CTL); 2820 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2821 __func__, (uintmax_t)err_desc->lun_id); 2822 retval = EINVAL; 2823 break; 2824 } 2825 mtx_lock(&lun->lun_lock); 2826 mtx_unlock(&softc->ctl_lock); 2827 2828 /* 2829 * We could do some checking here to verify the validity 2830 * of the request, but given the complexity of error 2831 * injection requests, the checking logic would be fairly 2832 * complex. 2833 * 2834 * For now, if the request is invalid, it just won't get 2835 * executed and might get deleted. 2836 */ 2837 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2838 2839 /* 2840 * XXX KDM check to make sure the serial number is unique, 2841 * in case we somehow manage to wrap. That shouldn't 2842 * happen for a very long time, but it's the right thing to 2843 * do. 2844 */ 2845 new_err_desc->serial = lun->error_serial; 2846 err_desc->serial = lun->error_serial; 2847 lun->error_serial++; 2848 2849 mtx_unlock(&lun->lun_lock); 2850 break; 2851 } 2852 case CTL_ERROR_INJECT_DELETE: { 2853 struct ctl_error_desc *delete_desc, *desc, *desc2; 2854 int delete_done; 2855 2856 delete_desc = (struct ctl_error_desc *)addr; 2857 delete_done = 0; 2858 2859 mtx_lock(&softc->ctl_lock); 2860 if (delete_desc->lun_id >= CTL_MAX_LUNS || 2861 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2862 mtx_unlock(&softc->ctl_lock); 2863 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2864 __func__, (uintmax_t)delete_desc->lun_id); 2865 retval = EINVAL; 2866 break; 2867 } 2868 mtx_lock(&lun->lun_lock); 2869 mtx_unlock(&softc->ctl_lock); 2870 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2871 if (desc->serial != delete_desc->serial) 2872 continue; 2873 2874 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2875 links); 2876 free(desc, M_CTL); 2877 delete_done = 1; 2878 } 2879 mtx_unlock(&lun->lun_lock); 2880 if (delete_done == 0) { 2881 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2882 "error serial %ju on LUN %u\n", __func__, 2883 delete_desc->serial, delete_desc->lun_id); 2884 retval = EINVAL; 2885 break; 2886 } 2887 break; 2888 } 2889 case CTL_DUMP_STRUCTS: { 2890 int j, k; 2891 struct ctl_port *port; 2892 struct ctl_frontend *fe; 2893 2894 mtx_lock(&softc->ctl_lock); 2895 printf("CTL Persistent Reservation information start:\n"); 2896 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2897 mtx_lock(&lun->lun_lock); 2898 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2899 mtx_unlock(&lun->lun_lock); 2900 continue; 2901 } 2902 2903 for (j = 0; j < CTL_MAX_PORTS; j++) { 2904 if (lun->pr_keys[j] == NULL) 2905 continue; 2906 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2907 if (lun->pr_keys[j][k] == 0) 2908 continue; 2909 printf(" LUN %ju port %d iid %d key " 2910 "%#jx\n", lun->lun, j, k, 2911 (uintmax_t)lun->pr_keys[j][k]); 2912 } 2913 } 2914 mtx_unlock(&lun->lun_lock); 2915 } 2916 printf("CTL Persistent Reservation information end\n"); 2917 printf("CTL Ports:\n"); 2918 STAILQ_FOREACH(port, &softc->port_list, links) { 2919 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2920 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2921 port->frontend->name, port->port_type, 2922 port->physical_port, port->virtual_port, 2923 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2924 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2925 if (port->wwpn_iid[j].in_use == 0 && 2926 port->wwpn_iid[j].wwpn == 0 && 2927 port->wwpn_iid[j].name == NULL) 2928 continue; 2929 2930 printf(" iid %u use %d WWPN %#jx '%s'\n", 2931 j, port->wwpn_iid[j].in_use, 2932 (uintmax_t)port->wwpn_iid[j].wwpn, 2933 port->wwpn_iid[j].name); 2934 } 2935 } 2936 printf("CTL Port information end\n"); 2937 mtx_unlock(&softc->ctl_lock); 2938 /* 2939 * XXX KDM calling this without a lock. We'd likely want 2940 * to drop the lock before calling the frontend's dump 2941 * routine anyway. 2942 */ 2943 printf("CTL Frontends:\n"); 2944 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2945 printf(" Frontend '%s'\n", fe->name); 2946 if (fe->fe_dump != NULL) 2947 fe->fe_dump(); 2948 } 2949 printf("CTL Frontend information end\n"); 2950 break; 2951 } 2952 case CTL_LUN_REQ: { 2953 struct ctl_lun_req *lun_req; 2954 struct ctl_backend_driver *backend; 2955 2956 lun_req = (struct ctl_lun_req *)addr; 2957 2958 backend = ctl_backend_find(lun_req->backend); 2959 if (backend == NULL) { 2960 lun_req->status = CTL_LUN_ERROR; 2961 snprintf(lun_req->error_str, 2962 sizeof(lun_req->error_str), 2963 "Backend \"%s\" not found.", 2964 lun_req->backend); 2965 break; 2966 } 2967 if (lun_req->num_be_args > 0) { 2968 lun_req->kern_be_args = ctl_copyin_args( 2969 lun_req->num_be_args, 2970 lun_req->be_args, 2971 lun_req->error_str, 2972 sizeof(lun_req->error_str)); 2973 if (lun_req->kern_be_args == NULL) { 2974 lun_req->status = CTL_LUN_ERROR; 2975 break; 2976 } 2977 } 2978 2979 retval = backend->ioctl(dev, cmd, addr, flag, td); 2980 2981 if (lun_req->num_be_args > 0) { 2982 ctl_copyout_args(lun_req->num_be_args, 2983 lun_req->kern_be_args); 2984 ctl_free_args(lun_req->num_be_args, 2985 lun_req->kern_be_args); 2986 } 2987 break; 2988 } 2989 case CTL_LUN_LIST: { 2990 struct sbuf *sb; 2991 struct ctl_lun_list *list; 2992 struct ctl_option *opt; 2993 2994 list = (struct ctl_lun_list *)addr; 2995 2996 /* 2997 * Allocate a fixed length sbuf here, based on the length 2998 * of the user's buffer. We could allocate an auto-extending 2999 * buffer, and then tell the user how much larger our 3000 * amount of data is than his buffer, but that presents 3001 * some problems: 3002 * 3003 * 1. The sbuf(9) routines use a blocking malloc, and so 3004 * we can't hold a lock while calling them with an 3005 * auto-extending buffer. 3006 * 3007 * 2. There is not currently a LUN reference counting 3008 * mechanism, outside of outstanding transactions on 3009 * the LUN's OOA queue. So a LUN could go away on us 3010 * while we're getting the LUN number, backend-specific 3011 * information, etc. Thus, given the way things 3012 * currently work, we need to hold the CTL lock while 3013 * grabbing LUN information. 3014 * 3015 * So, from the user's standpoint, the best thing to do is 3016 * allocate what he thinks is a reasonable buffer length, 3017 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3018 * double the buffer length and try again. (And repeat 3019 * that until he succeeds.) 3020 */ 3021 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3022 if (sb == NULL) { 3023 list->status = CTL_LUN_LIST_ERROR; 3024 snprintf(list->error_str, sizeof(list->error_str), 3025 "Unable to allocate %d bytes for LUN list", 3026 list->alloc_len); 3027 break; 3028 } 3029 3030 sbuf_printf(sb, "<ctllunlist>\n"); 3031 3032 mtx_lock(&softc->ctl_lock); 3033 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3034 mtx_lock(&lun->lun_lock); 3035 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3036 (uintmax_t)lun->lun); 3037 3038 /* 3039 * Bail out as soon as we see that we've overfilled 3040 * the buffer. 3041 */ 3042 if (retval != 0) 3043 break; 3044 3045 retval = sbuf_printf(sb, "\t<backend_type>%s" 3046 "</backend_type>\n", 3047 (lun->backend == NULL) ? "none" : 3048 lun->backend->name); 3049 3050 if (retval != 0) 3051 break; 3052 3053 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3054 lun->be_lun->lun_type); 3055 3056 if (retval != 0) 3057 break; 3058 3059 if (lun->backend == NULL) { 3060 retval = sbuf_printf(sb, "</lun>\n"); 3061 if (retval != 0) 3062 break; 3063 continue; 3064 } 3065 3066 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3067 (lun->be_lun->maxlba > 0) ? 3068 lun->be_lun->maxlba + 1 : 0); 3069 3070 if (retval != 0) 3071 break; 3072 3073 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3074 lun->be_lun->blocksize); 3075 3076 if (retval != 0) 3077 break; 3078 3079 retval = sbuf_printf(sb, "\t<serial_number>"); 3080 3081 if (retval != 0) 3082 break; 3083 3084 retval = ctl_sbuf_printf_esc(sb, 3085 lun->be_lun->serial_num, 3086 sizeof(lun->be_lun->serial_num)); 3087 3088 if (retval != 0) 3089 break; 3090 3091 retval = sbuf_printf(sb, "</serial_number>\n"); 3092 3093 if (retval != 0) 3094 break; 3095 3096 retval = sbuf_printf(sb, "\t<device_id>"); 3097 3098 if (retval != 0) 3099 break; 3100 3101 retval = ctl_sbuf_printf_esc(sb, 3102 lun->be_lun->device_id, 3103 sizeof(lun->be_lun->device_id)); 3104 3105 if (retval != 0) 3106 break; 3107 3108 retval = sbuf_printf(sb, "</device_id>\n"); 3109 3110 if (retval != 0) 3111 break; 3112 3113 if (lun->backend->lun_info != NULL) { 3114 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3115 if (retval != 0) 3116 break; 3117 } 3118 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3119 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3120 opt->name, opt->value, opt->name); 3121 if (retval != 0) 3122 break; 3123 } 3124 3125 retval = sbuf_printf(sb, "</lun>\n"); 3126 3127 if (retval != 0) 3128 break; 3129 mtx_unlock(&lun->lun_lock); 3130 } 3131 if (lun != NULL) 3132 mtx_unlock(&lun->lun_lock); 3133 mtx_unlock(&softc->ctl_lock); 3134 3135 if ((retval != 0) 3136 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3137 retval = 0; 3138 sbuf_delete(sb); 3139 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3140 snprintf(list->error_str, sizeof(list->error_str), 3141 "Out of space, %d bytes is too small", 3142 list->alloc_len); 3143 break; 3144 } 3145 3146 sbuf_finish(sb); 3147 3148 retval = copyout(sbuf_data(sb), list->lun_xml, 3149 sbuf_len(sb) + 1); 3150 3151 list->fill_len = sbuf_len(sb) + 1; 3152 list->status = CTL_LUN_LIST_OK; 3153 sbuf_delete(sb); 3154 break; 3155 } 3156 case CTL_ISCSI: { 3157 struct ctl_iscsi *ci; 3158 struct ctl_frontend *fe; 3159 3160 ci = (struct ctl_iscsi *)addr; 3161 3162 fe = ctl_frontend_find("iscsi"); 3163 if (fe == NULL) { 3164 ci->status = CTL_ISCSI_ERROR; 3165 snprintf(ci->error_str, sizeof(ci->error_str), 3166 "Frontend \"iscsi\" not found."); 3167 break; 3168 } 3169 3170 retval = fe->ioctl(dev, cmd, addr, flag, td); 3171 break; 3172 } 3173 case CTL_PORT_REQ: { 3174 struct ctl_req *req; 3175 struct ctl_frontend *fe; 3176 3177 req = (struct ctl_req *)addr; 3178 3179 fe = ctl_frontend_find(req->driver); 3180 if (fe == NULL) { 3181 req->status = CTL_LUN_ERROR; 3182 snprintf(req->error_str, sizeof(req->error_str), 3183 "Frontend \"%s\" not found.", req->driver); 3184 break; 3185 } 3186 if (req->num_args > 0) { 3187 req->kern_args = ctl_copyin_args(req->num_args, 3188 req->args, req->error_str, sizeof(req->error_str)); 3189 if (req->kern_args == NULL) { 3190 req->status = CTL_LUN_ERROR; 3191 break; 3192 } 3193 } 3194 3195 if (fe->ioctl) 3196 retval = fe->ioctl(dev, cmd, addr, flag, td); 3197 else 3198 retval = ENODEV; 3199 3200 if (req->num_args > 0) { 3201 ctl_copyout_args(req->num_args, req->kern_args); 3202 ctl_free_args(req->num_args, req->kern_args); 3203 } 3204 break; 3205 } 3206 case CTL_PORT_LIST: { 3207 struct sbuf *sb; 3208 struct ctl_port *port; 3209 struct ctl_lun_list *list; 3210 struct ctl_option *opt; 3211 int j; 3212 uint32_t plun; 3213 3214 list = (struct ctl_lun_list *)addr; 3215 3216 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3217 if (sb == NULL) { 3218 list->status = CTL_LUN_LIST_ERROR; 3219 snprintf(list->error_str, sizeof(list->error_str), 3220 "Unable to allocate %d bytes for LUN list", 3221 list->alloc_len); 3222 break; 3223 } 3224 3225 sbuf_printf(sb, "<ctlportlist>\n"); 3226 3227 mtx_lock(&softc->ctl_lock); 3228 STAILQ_FOREACH(port, &softc->port_list, links) { 3229 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3230 (uintmax_t)port->targ_port); 3231 3232 /* 3233 * Bail out as soon as we see that we've overfilled 3234 * the buffer. 3235 */ 3236 if (retval != 0) 3237 break; 3238 3239 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3240 "</frontend_type>\n", port->frontend->name); 3241 if (retval != 0) 3242 break; 3243 3244 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3245 port->port_type); 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3250 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3251 if (retval != 0) 3252 break; 3253 3254 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3255 port->port_name); 3256 if (retval != 0) 3257 break; 3258 3259 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3260 port->physical_port); 3261 if (retval != 0) 3262 break; 3263 3264 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3265 port->virtual_port); 3266 if (retval != 0) 3267 break; 3268 3269 if (port->target_devid != NULL) { 3270 sbuf_printf(sb, "\t<target>"); 3271 ctl_id_sbuf(port->target_devid, sb); 3272 sbuf_printf(sb, "</target>\n"); 3273 } 3274 3275 if (port->port_devid != NULL) { 3276 sbuf_printf(sb, "\t<port>"); 3277 ctl_id_sbuf(port->port_devid, sb); 3278 sbuf_printf(sb, "</port>\n"); 3279 } 3280 3281 if (port->port_info != NULL) { 3282 retval = port->port_info(port->onoff_arg, sb); 3283 if (retval != 0) 3284 break; 3285 } 3286 STAILQ_FOREACH(opt, &port->options, links) { 3287 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3288 opt->name, opt->value, opt->name); 3289 if (retval != 0) 3290 break; 3291 } 3292 3293 if (port->lun_map != NULL) { 3294 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3295 for (j = 0; j < port->lun_map_size; j++) { 3296 plun = ctl_lun_map_from_port(port, j); 3297 if (plun == UINT32_MAX) 3298 continue; 3299 sbuf_printf(sb, 3300 "\t<lun id=\"%u\">%u</lun>\n", 3301 j, plun); 3302 } 3303 } 3304 3305 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3306 if (port->wwpn_iid[j].in_use == 0 || 3307 (port->wwpn_iid[j].wwpn == 0 && 3308 port->wwpn_iid[j].name == NULL)) 3309 continue; 3310 3311 if (port->wwpn_iid[j].name != NULL) 3312 retval = sbuf_printf(sb, 3313 "\t<initiator id=\"%u\">%s</initiator>\n", 3314 j, port->wwpn_iid[j].name); 3315 else 3316 retval = sbuf_printf(sb, 3317 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3318 j, port->wwpn_iid[j].wwpn); 3319 if (retval != 0) 3320 break; 3321 } 3322 if (retval != 0) 3323 break; 3324 3325 retval = sbuf_printf(sb, "</targ_port>\n"); 3326 if (retval != 0) 3327 break; 3328 } 3329 mtx_unlock(&softc->ctl_lock); 3330 3331 if ((retval != 0) 3332 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3333 retval = 0; 3334 sbuf_delete(sb); 3335 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3336 snprintf(list->error_str, sizeof(list->error_str), 3337 "Out of space, %d bytes is too small", 3338 list->alloc_len); 3339 break; 3340 } 3341 3342 sbuf_finish(sb); 3343 3344 retval = copyout(sbuf_data(sb), list->lun_xml, 3345 sbuf_len(sb) + 1); 3346 3347 list->fill_len = sbuf_len(sb) + 1; 3348 list->status = CTL_LUN_LIST_OK; 3349 sbuf_delete(sb); 3350 break; 3351 } 3352 case CTL_LUN_MAP: { 3353 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3354 struct ctl_port *port; 3355 3356 mtx_lock(&softc->ctl_lock); 3357 if (lm->port < softc->port_min || 3358 lm->port >= softc->port_max || 3359 (port = softc->ctl_ports[lm->port]) == NULL) { 3360 mtx_unlock(&softc->ctl_lock); 3361 return (ENXIO); 3362 } 3363 if (port->status & CTL_PORT_STATUS_ONLINE) { 3364 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3365 if (ctl_lun_map_to_port(port, lun->lun) == 3366 UINT32_MAX) 3367 continue; 3368 mtx_lock(&lun->lun_lock); 3369 ctl_est_ua_port(lun, lm->port, -1, 3370 CTL_UA_LUN_CHANGE); 3371 mtx_unlock(&lun->lun_lock); 3372 } 3373 } 3374 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3375 if (lm->plun != UINT32_MAX) { 3376 if (lm->lun == UINT32_MAX) 3377 retval = ctl_lun_map_unset(port, lm->plun); 3378 else if (lm->lun < CTL_MAX_LUNS && 3379 softc->ctl_luns[lm->lun] != NULL) 3380 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3381 else 3382 return (ENXIO); 3383 } else { 3384 if (lm->lun == UINT32_MAX) 3385 retval = ctl_lun_map_deinit(port); 3386 else 3387 retval = ctl_lun_map_init(port); 3388 } 3389 if (port->status & CTL_PORT_STATUS_ONLINE) 3390 ctl_isc_announce_port(port); 3391 break; 3392 } 3393 case CTL_GET_LUN_STATS: { 3394 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3395 int i; 3396 3397 /* 3398 * XXX KDM no locking here. If the LUN list changes, 3399 * things can blow up. 3400 */ 3401 i = 0; 3402 stats->status = CTL_SS_OK; 3403 stats->fill_len = 0; 3404 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3405 if (lun->lun < stats->first_item) 3406 continue; 3407 if (stats->fill_len + sizeof(lun->stats) > 3408 stats->alloc_len) { 3409 stats->status = CTL_SS_NEED_MORE_SPACE; 3410 break; 3411 } 3412 retval = copyout(&lun->stats, &stats->stats[i++], 3413 sizeof(lun->stats)); 3414 if (retval != 0) 3415 break; 3416 stats->fill_len += sizeof(lun->stats); 3417 } 3418 stats->num_items = softc->num_luns; 3419 stats->flags = CTL_STATS_FLAG_NONE; 3420#ifdef CTL_TIME_IO 3421 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3422#endif 3423 getnanouptime(&stats->timestamp); 3424 break; 3425 } 3426 case CTL_GET_PORT_STATS: { 3427 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr; 3428 int i; 3429 3430 /* 3431 * XXX KDM no locking here. If the LUN list changes, 3432 * things can blow up. 3433 */ 3434 i = 0; 3435 stats->status = CTL_SS_OK; 3436 stats->fill_len = 0; 3437 STAILQ_FOREACH(port, &softc->port_list, links) { 3438 if (port->targ_port < stats->first_item) 3439 continue; 3440 if (stats->fill_len + sizeof(port->stats) > 3441 stats->alloc_len) { 3442 stats->status = CTL_SS_NEED_MORE_SPACE; 3443 break; 3444 } 3445 retval = copyout(&port->stats, &stats->stats[i++], 3446 sizeof(port->stats)); 3447 if (retval != 0) 3448 break; 3449 stats->fill_len += sizeof(port->stats); 3450 } 3451 stats->num_items = softc->num_ports; 3452 stats->flags = CTL_STATS_FLAG_NONE; 3453#ifdef CTL_TIME_IO 3454 stats->flags |= CTL_STATS_FLAG_TIME_VALID; 3455#endif 3456 getnanouptime(&stats->timestamp); 3457 break; 3458 } 3459 default: { 3460 /* XXX KDM should we fix this? */ 3461#if 0 3462 struct ctl_backend_driver *backend; 3463 unsigned int type; 3464 int found; 3465 3466 found = 0; 3467 3468 /* 3469 * We encode the backend type as the ioctl type for backend 3470 * ioctls. So parse it out here, and then search for a 3471 * backend of this type. 3472 */ 3473 type = _IOC_TYPE(cmd); 3474 3475 STAILQ_FOREACH(backend, &softc->be_list, links) { 3476 if (backend->type == type) { 3477 found = 1; 3478 break; 3479 } 3480 } 3481 if (found == 0) { 3482 printf("ctl: unknown ioctl command %#lx or backend " 3483 "%d\n", cmd, type); 3484 retval = EINVAL; 3485 break; 3486 } 3487 retval = backend->ioctl(dev, cmd, addr, flag, td); 3488#endif 3489 retval = ENOTTY; 3490 break; 3491 } 3492 } 3493 return (retval); 3494} 3495 3496uint32_t 3497ctl_get_initindex(struct ctl_nexus *nexus) 3498{ 3499 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3500} 3501 3502int 3503ctl_lun_map_init(struct ctl_port *port) 3504{ 3505 struct ctl_softc *softc = port->ctl_softc; 3506 struct ctl_lun *lun; 3507 int size = ctl_lun_map_size; 3508 uint32_t i; 3509 3510 if (port->lun_map == NULL || port->lun_map_size < size) { 3511 port->lun_map_size = 0; 3512 free(port->lun_map, M_CTL); 3513 port->lun_map = malloc(size * sizeof(uint32_t), 3514 M_CTL, M_NOWAIT); 3515 } 3516 if (port->lun_map == NULL) 3517 return (ENOMEM); 3518 for (i = 0; i < size; i++) 3519 port->lun_map[i] = UINT32_MAX; 3520 port->lun_map_size = size; 3521 if (port->status & CTL_PORT_STATUS_ONLINE) { 3522 if (port->lun_disable != NULL) { 3523 STAILQ_FOREACH(lun, &softc->lun_list, links) 3524 port->lun_disable(port->targ_lun_arg, lun->lun); 3525 } 3526 ctl_isc_announce_port(port); 3527 } 3528 return (0); 3529} 3530 3531int 3532ctl_lun_map_deinit(struct ctl_port *port) 3533{ 3534 struct ctl_softc *softc = port->ctl_softc; 3535 struct ctl_lun *lun; 3536 3537 if (port->lun_map == NULL) 3538 return (0); 3539 port->lun_map_size = 0; 3540 free(port->lun_map, M_CTL); 3541 port->lun_map = NULL; 3542 if (port->status & CTL_PORT_STATUS_ONLINE) { 3543 if (port->lun_enable != NULL) { 3544 STAILQ_FOREACH(lun, &softc->lun_list, links) 3545 port->lun_enable(port->targ_lun_arg, lun->lun); 3546 } 3547 ctl_isc_announce_port(port); 3548 } 3549 return (0); 3550} 3551 3552int 3553ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3554{ 3555 int status; 3556 uint32_t old; 3557 3558 if (port->lun_map == NULL) { 3559 status = ctl_lun_map_init(port); 3560 if (status != 0) 3561 return (status); 3562 } 3563 if (plun >= port->lun_map_size) 3564 return (EINVAL); 3565 old = port->lun_map[plun]; 3566 port->lun_map[plun] = glun; 3567 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3568 if (port->lun_enable != NULL) 3569 port->lun_enable(port->targ_lun_arg, plun); 3570 ctl_isc_announce_port(port); 3571 } 3572 return (0); 3573} 3574 3575int 3576ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3577{ 3578 uint32_t old; 3579 3580 if (port->lun_map == NULL || plun >= port->lun_map_size) 3581 return (0); 3582 old = port->lun_map[plun]; 3583 port->lun_map[plun] = UINT32_MAX; 3584 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3585 if (port->lun_disable != NULL) 3586 port->lun_disable(port->targ_lun_arg, plun); 3587 ctl_isc_announce_port(port); 3588 } 3589 return (0); 3590} 3591 3592uint32_t 3593ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3594{ 3595 3596 if (port == NULL) 3597 return (UINT32_MAX); 3598 if (port->lun_map == NULL) 3599 return (lun_id); 3600 if (lun_id > port->lun_map_size) 3601 return (UINT32_MAX); 3602 return (port->lun_map[lun_id]); 3603} 3604 3605uint32_t 3606ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3607{ 3608 uint32_t i; 3609 3610 if (port == NULL) 3611 return (UINT32_MAX); 3612 if (port->lun_map == NULL) 3613 return (lun_id); 3614 for (i = 0; i < port->lun_map_size; i++) { 3615 if (port->lun_map[i] == lun_id) 3616 return (i); 3617 } 3618 return (UINT32_MAX); 3619} 3620 3621uint32_t 3622ctl_decode_lun(uint64_t encoded) 3623{ 3624 uint8_t lun[8]; 3625 uint32_t result = 0xffffffff; 3626 3627 be64enc(lun, encoded); 3628 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3629 case RPL_LUNDATA_ATYP_PERIPH: 3630 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3631 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3632 result = lun[1]; 3633 break; 3634 case RPL_LUNDATA_ATYP_FLAT: 3635 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3636 lun[6] == 0 && lun[7] == 0) 3637 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3638 break; 3639 case RPL_LUNDATA_ATYP_EXTLUN: 3640 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3641 case 0x02: 3642 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3643 case 0x00: 3644 result = lun[1]; 3645 break; 3646 case 0x10: 3647 result = (lun[1] << 16) + (lun[2] << 8) + 3648 lun[3]; 3649 break; 3650 case 0x20: 3651 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3652 result = (lun[2] << 24) + 3653 (lun[3] << 16) + (lun[4] << 8) + 3654 lun[5]; 3655 break; 3656 } 3657 break; 3658 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3659 result = 0xffffffff; 3660 break; 3661 } 3662 break; 3663 } 3664 return (result); 3665} 3666 3667uint64_t 3668ctl_encode_lun(uint32_t decoded) 3669{ 3670 uint64_t l = decoded; 3671 3672 if (l <= 0xff) 3673 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3674 if (l <= 0x3fff) 3675 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3676 if (l <= 0xffffff) 3677 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3678 (l << 32)); 3679 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3680} 3681 3682int 3683ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3684{ 3685 int i; 3686 3687 for (i = first; i < last; i++) { 3688 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3689 return (i); 3690 } 3691 return (-1); 3692} 3693 3694int 3695ctl_set_mask(uint32_t *mask, uint32_t bit) 3696{ 3697 uint32_t chunk, piece; 3698 3699 chunk = bit >> 5; 3700 piece = bit % (sizeof(uint32_t) * 8); 3701 3702 if ((mask[chunk] & (1 << piece)) != 0) 3703 return (-1); 3704 else 3705 mask[chunk] |= (1 << piece); 3706 3707 return (0); 3708} 3709 3710int 3711ctl_clear_mask(uint32_t *mask, uint32_t bit) 3712{ 3713 uint32_t chunk, piece; 3714 3715 chunk = bit >> 5; 3716 piece = bit % (sizeof(uint32_t) * 8); 3717 3718 if ((mask[chunk] & (1 << piece)) == 0) 3719 return (-1); 3720 else 3721 mask[chunk] &= ~(1 << piece); 3722 3723 return (0); 3724} 3725 3726int 3727ctl_is_set(uint32_t *mask, uint32_t bit) 3728{ 3729 uint32_t chunk, piece; 3730 3731 chunk = bit >> 5; 3732 piece = bit % (sizeof(uint32_t) * 8); 3733 3734 if ((mask[chunk] & (1 << piece)) == 0) 3735 return (0); 3736 else 3737 return (1); 3738} 3739 3740static uint64_t 3741ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3742{ 3743 uint64_t *t; 3744 3745 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3746 if (t == NULL) 3747 return (0); 3748 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3749} 3750 3751static void 3752ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3753{ 3754 uint64_t *t; 3755 3756 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3757 if (t == NULL) 3758 return; 3759 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3760} 3761 3762static void 3763ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3764{ 3765 uint64_t *p; 3766 u_int i; 3767 3768 i = residx/CTL_MAX_INIT_PER_PORT; 3769 if (lun->pr_keys[i] != NULL) 3770 return; 3771 mtx_unlock(&lun->lun_lock); 3772 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3773 M_WAITOK | M_ZERO); 3774 mtx_lock(&lun->lun_lock); 3775 if (lun->pr_keys[i] == NULL) 3776 lun->pr_keys[i] = p; 3777 else 3778 free(p, M_CTL); 3779} 3780 3781static void 3782ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3783{ 3784 uint64_t *t; 3785 3786 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3787 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3788 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3789} 3790 3791/* 3792 * ctl_softc, pool_name, total_ctl_io are passed in. 3793 * npool is passed out. 3794 */ 3795int 3796ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3797 uint32_t total_ctl_io, void **npool) 3798{ 3799 struct ctl_io_pool *pool; 3800 3801 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3802 M_NOWAIT | M_ZERO); 3803 if (pool == NULL) 3804 return (ENOMEM); 3805 3806 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3807 pool->ctl_softc = ctl_softc; 3808#ifdef IO_POOLS 3809 pool->zone = uma_zsecond_create(pool->name, NULL, 3810 NULL, NULL, NULL, ctl_softc->io_zone); 3811 /* uma_prealloc(pool->zone, total_ctl_io); */ 3812#else 3813 pool->zone = ctl_softc->io_zone; 3814#endif 3815 3816 *npool = pool; 3817 return (0); 3818} 3819 3820void 3821ctl_pool_free(struct ctl_io_pool *pool) 3822{ 3823 3824 if (pool == NULL) 3825 return; 3826 3827#ifdef IO_POOLS 3828 uma_zdestroy(pool->zone); 3829#endif 3830 free(pool, M_CTL); 3831} 3832 3833union ctl_io * 3834ctl_alloc_io(void *pool_ref) 3835{ 3836 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3837 union ctl_io *io; 3838 3839 io = uma_zalloc(pool->zone, M_WAITOK); 3840 if (io != NULL) { 3841 io->io_hdr.pool = pool_ref; 3842 CTL_SOFTC(io) = pool->ctl_softc; 3843 } 3844 return (io); 3845} 3846 3847union ctl_io * 3848ctl_alloc_io_nowait(void *pool_ref) 3849{ 3850 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3851 union ctl_io *io; 3852 3853 io = uma_zalloc(pool->zone, M_NOWAIT); 3854 if (io != NULL) { 3855 io->io_hdr.pool = pool_ref; 3856 CTL_SOFTC(io) = pool->ctl_softc; 3857 } 3858 return (io); 3859} 3860 3861void 3862ctl_free_io(union ctl_io *io) 3863{ 3864 struct ctl_io_pool *pool; 3865 3866 if (io == NULL) 3867 return; 3868 3869 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3870 uma_zfree(pool->zone, io); 3871} 3872 3873void 3874ctl_zero_io(union ctl_io *io) 3875{ 3876 struct ctl_io_pool *pool; 3877 3878 if (io == NULL) 3879 return; 3880 3881 /* 3882 * May need to preserve linked list pointers at some point too. 3883 */ 3884 pool = io->io_hdr.pool; 3885 memset(io, 0, sizeof(*io)); 3886 io->io_hdr.pool = pool; 3887 CTL_SOFTC(io) = pool->ctl_softc; 3888} 3889 3890int 3891ctl_expand_number(const char *buf, uint64_t *num) 3892{ 3893 char *endptr; 3894 uint64_t number; 3895 unsigned shift; 3896 3897 number = strtoq(buf, &endptr, 0); 3898 3899 switch (tolower((unsigned char)*endptr)) { 3900 case 'e': 3901 shift = 60; 3902 break; 3903 case 'p': 3904 shift = 50; 3905 break; 3906 case 't': 3907 shift = 40; 3908 break; 3909 case 'g': 3910 shift = 30; 3911 break; 3912 case 'm': 3913 shift = 20; 3914 break; 3915 case 'k': 3916 shift = 10; 3917 break; 3918 case 'b': 3919 case '\0': /* No unit. */ 3920 *num = number; 3921 return (0); 3922 default: 3923 /* Unrecognized unit. */ 3924 return (-1); 3925 } 3926 3927 if ((number << shift) >> shift != number) { 3928 /* Overflow */ 3929 return (-1); 3930 } 3931 *num = number << shift; 3932 return (0); 3933} 3934 3935 3936/* 3937 * This routine could be used in the future to load default and/or saved 3938 * mode page parameters for a particuar lun. 3939 */ 3940static int 3941ctl_init_page_index(struct ctl_lun *lun) 3942{ 3943 int i, page_code; 3944 struct ctl_page_index *page_index; 3945 const char *value; 3946 uint64_t ival; 3947 3948 memcpy(&lun->mode_pages.index, page_index_template, 3949 sizeof(page_index_template)); 3950 3951 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3952 3953 page_index = &lun->mode_pages.index[i]; 3954 if (lun->be_lun->lun_type == T_DIRECT && 3955 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3956 continue; 3957 if (lun->be_lun->lun_type == T_PROCESSOR && 3958 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3959 continue; 3960 if (lun->be_lun->lun_type == T_CDROM && 3961 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3962 continue; 3963 3964 page_code = page_index->page_code & SMPH_PC_MASK; 3965 switch (page_code) { 3966 case SMS_RW_ERROR_RECOVERY_PAGE: { 3967 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3968 ("subpage %#x for page %#x is incorrect!", 3969 page_index->subpage, page_code)); 3970 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3971 &rw_er_page_default, 3972 sizeof(rw_er_page_default)); 3973 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3974 &rw_er_page_changeable, 3975 sizeof(rw_er_page_changeable)); 3976 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3977 &rw_er_page_default, 3978 sizeof(rw_er_page_default)); 3979 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3980 &rw_er_page_default, 3981 sizeof(rw_er_page_default)); 3982 page_index->page_data = 3983 (uint8_t *)lun->mode_pages.rw_er_page; 3984 break; 3985 } 3986 case SMS_FORMAT_DEVICE_PAGE: { 3987 struct scsi_format_page *format_page; 3988 3989 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3990 ("subpage %#x for page %#x is incorrect!", 3991 page_index->subpage, page_code)); 3992 3993 /* 3994 * Sectors per track are set above. Bytes per 3995 * sector need to be set here on a per-LUN basis. 3996 */ 3997 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3998 &format_page_default, 3999 sizeof(format_page_default)); 4000 memcpy(&lun->mode_pages.format_page[ 4001 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4002 sizeof(format_page_changeable)); 4003 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4004 &format_page_default, 4005 sizeof(format_page_default)); 4006 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4007 &format_page_default, 4008 sizeof(format_page_default)); 4009 4010 format_page = &lun->mode_pages.format_page[ 4011 CTL_PAGE_CURRENT]; 4012 scsi_ulto2b(lun->be_lun->blocksize, 4013 format_page->bytes_per_sector); 4014 4015 format_page = &lun->mode_pages.format_page[ 4016 CTL_PAGE_DEFAULT]; 4017 scsi_ulto2b(lun->be_lun->blocksize, 4018 format_page->bytes_per_sector); 4019 4020 format_page = &lun->mode_pages.format_page[ 4021 CTL_PAGE_SAVED]; 4022 scsi_ulto2b(lun->be_lun->blocksize, 4023 format_page->bytes_per_sector); 4024 4025 page_index->page_data = 4026 (uint8_t *)lun->mode_pages.format_page; 4027 break; 4028 } 4029 case SMS_RIGID_DISK_PAGE: { 4030 struct scsi_rigid_disk_page *rigid_disk_page; 4031 uint32_t sectors_per_cylinder; 4032 uint64_t cylinders; 4033#ifndef __XSCALE__ 4034 int shift; 4035#endif /* !__XSCALE__ */ 4036 4037 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4038 ("subpage %#x for page %#x is incorrect!", 4039 page_index->subpage, page_code)); 4040 4041 /* 4042 * Rotation rate and sectors per track are set 4043 * above. We calculate the cylinders here based on 4044 * capacity. Due to the number of heads and 4045 * sectors per track we're using, smaller arrays 4046 * may turn out to have 0 cylinders. Linux and 4047 * FreeBSD don't pay attention to these mode pages 4048 * to figure out capacity, but Solaris does. It 4049 * seems to deal with 0 cylinders just fine, and 4050 * works out a fake geometry based on the capacity. 4051 */ 4052 memcpy(&lun->mode_pages.rigid_disk_page[ 4053 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4054 sizeof(rigid_disk_page_default)); 4055 memcpy(&lun->mode_pages.rigid_disk_page[ 4056 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4057 sizeof(rigid_disk_page_changeable)); 4058 4059 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4060 CTL_DEFAULT_HEADS; 4061 4062 /* 4063 * The divide method here will be more accurate, 4064 * probably, but results in floating point being 4065 * used in the kernel on i386 (__udivdi3()). On the 4066 * XScale, though, __udivdi3() is implemented in 4067 * software. 4068 * 4069 * The shift method for cylinder calculation is 4070 * accurate if sectors_per_cylinder is a power of 4071 * 2. Otherwise it might be slightly off -- you 4072 * might have a bit of a truncation problem. 4073 */ 4074#ifdef __XSCALE__ 4075 cylinders = (lun->be_lun->maxlba + 1) / 4076 sectors_per_cylinder; 4077#else 4078 for (shift = 31; shift > 0; shift--) { 4079 if (sectors_per_cylinder & (1 << shift)) 4080 break; 4081 } 4082 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4083#endif 4084 4085 /* 4086 * We've basically got 3 bytes, or 24 bits for the 4087 * cylinder size in the mode page. If we're over, 4088 * just round down to 2^24. 4089 */ 4090 if (cylinders > 0xffffff) 4091 cylinders = 0xffffff; 4092 4093 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4094 CTL_PAGE_DEFAULT]; 4095 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4096 4097 if ((value = ctl_get_opt(&lun->be_lun->options, 4098 "rpm")) != NULL) { 4099 scsi_ulto2b(strtol(value, NULL, 0), 4100 rigid_disk_page->rotation_rate); 4101 } 4102 4103 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4104 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4105 sizeof(rigid_disk_page_default)); 4106 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4107 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4108 sizeof(rigid_disk_page_default)); 4109 4110 page_index->page_data = 4111 (uint8_t *)lun->mode_pages.rigid_disk_page; 4112 break; 4113 } 4114 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4115 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4116 ("subpage %#x for page %#x is incorrect!", 4117 page_index->subpage, page_code)); 4118 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4119 &verify_er_page_default, 4120 sizeof(verify_er_page_default)); 4121 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4122 &verify_er_page_changeable, 4123 sizeof(verify_er_page_changeable)); 4124 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4125 &verify_er_page_default, 4126 sizeof(verify_er_page_default)); 4127 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4128 &verify_er_page_default, 4129 sizeof(verify_er_page_default)); 4130 page_index->page_data = 4131 (uint8_t *)lun->mode_pages.verify_er_page; 4132 break; 4133 } 4134 case SMS_CACHING_PAGE: { 4135 struct scsi_caching_page *caching_page; 4136 4137 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4138 ("subpage %#x for page %#x is incorrect!", 4139 page_index->subpage, page_code)); 4140 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4141 &caching_page_default, 4142 sizeof(caching_page_default)); 4143 memcpy(&lun->mode_pages.caching_page[ 4144 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4145 sizeof(caching_page_changeable)); 4146 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4147 &caching_page_default, 4148 sizeof(caching_page_default)); 4149 caching_page = &lun->mode_pages.caching_page[ 4150 CTL_PAGE_SAVED]; 4151 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4152 if (value != NULL && strcmp(value, "off") == 0) 4153 caching_page->flags1 &= ~SCP_WCE; 4154 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4155 if (value != NULL && strcmp(value, "off") == 0) 4156 caching_page->flags1 |= SCP_RCD; 4157 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4158 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4159 sizeof(caching_page_default)); 4160 page_index->page_data = 4161 (uint8_t *)lun->mode_pages.caching_page; 4162 break; 4163 } 4164 case SMS_CONTROL_MODE_PAGE: { 4165 switch (page_index->subpage) { 4166 case SMS_SUBPAGE_PAGE_0: { 4167 struct scsi_control_page *control_page; 4168 4169 memcpy(&lun->mode_pages.control_page[ 4170 CTL_PAGE_DEFAULT], 4171 &control_page_default, 4172 sizeof(control_page_default)); 4173 memcpy(&lun->mode_pages.control_page[ 4174 CTL_PAGE_CHANGEABLE], 4175 &control_page_changeable, 4176 sizeof(control_page_changeable)); 4177 memcpy(&lun->mode_pages.control_page[ 4178 CTL_PAGE_SAVED], 4179 &control_page_default, 4180 sizeof(control_page_default)); 4181 control_page = &lun->mode_pages.control_page[ 4182 CTL_PAGE_SAVED]; 4183 value = ctl_get_opt(&lun->be_lun->options, 4184 "reordering"); 4185 if (value != NULL && 4186 strcmp(value, "unrestricted") == 0) { 4187 control_page->queue_flags &= 4188 ~SCP_QUEUE_ALG_MASK; 4189 control_page->queue_flags |= 4190 SCP_QUEUE_ALG_UNRESTRICTED; 4191 } 4192 memcpy(&lun->mode_pages.control_page[ 4193 CTL_PAGE_CURRENT], 4194 &lun->mode_pages.control_page[ 4195 CTL_PAGE_SAVED], 4196 sizeof(control_page_default)); 4197 page_index->page_data = 4198 (uint8_t *)lun->mode_pages.control_page; 4199 break; 4200 } 4201 case 0x01: 4202 memcpy(&lun->mode_pages.control_ext_page[ 4203 CTL_PAGE_DEFAULT], 4204 &control_ext_page_default, 4205 sizeof(control_ext_page_default)); 4206 memcpy(&lun->mode_pages.control_ext_page[ 4207 CTL_PAGE_CHANGEABLE], 4208 &control_ext_page_changeable, 4209 sizeof(control_ext_page_changeable)); 4210 memcpy(&lun->mode_pages.control_ext_page[ 4211 CTL_PAGE_SAVED], 4212 &control_ext_page_default, 4213 sizeof(control_ext_page_default)); 4214 memcpy(&lun->mode_pages.control_ext_page[ 4215 CTL_PAGE_CURRENT], 4216 &lun->mode_pages.control_ext_page[ 4217 CTL_PAGE_SAVED], 4218 sizeof(control_ext_page_default)); 4219 page_index->page_data = 4220 (uint8_t *)lun->mode_pages.control_ext_page; 4221 break; 4222 default: 4223 panic("subpage %#x for page %#x is incorrect!", 4224 page_index->subpage, page_code); 4225 } 4226 break; 4227 } 4228 case SMS_INFO_EXCEPTIONS_PAGE: { 4229 switch (page_index->subpage) { 4230 case SMS_SUBPAGE_PAGE_0: 4231 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4232 &ie_page_default, 4233 sizeof(ie_page_default)); 4234 memcpy(&lun->mode_pages.ie_page[ 4235 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4236 sizeof(ie_page_changeable)); 4237 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4238 &ie_page_default, 4239 sizeof(ie_page_default)); 4240 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4241 &ie_page_default, 4242 sizeof(ie_page_default)); 4243 page_index->page_data = 4244 (uint8_t *)lun->mode_pages.ie_page; 4245 break; 4246 case 0x02: { 4247 struct ctl_logical_block_provisioning_page *page; 4248 4249 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4250 &lbp_page_default, 4251 sizeof(lbp_page_default)); 4252 memcpy(&lun->mode_pages.lbp_page[ 4253 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4254 sizeof(lbp_page_changeable)); 4255 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4256 &lbp_page_default, 4257 sizeof(lbp_page_default)); 4258 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4259 value = ctl_get_opt(&lun->be_lun->options, 4260 "avail-threshold"); 4261 if (value != NULL && 4262 ctl_expand_number(value, &ival) == 0) { 4263 page->descr[0].flags |= SLBPPD_ENABLED | 4264 SLBPPD_ARMING_DEC; 4265 if (lun->be_lun->blocksize) 4266 ival /= lun->be_lun->blocksize; 4267 else 4268 ival /= 512; 4269 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4270 page->descr[0].count); 4271 } 4272 value = ctl_get_opt(&lun->be_lun->options, 4273 "used-threshold"); 4274 if (value != NULL && 4275 ctl_expand_number(value, &ival) == 0) { 4276 page->descr[1].flags |= SLBPPD_ENABLED | 4277 SLBPPD_ARMING_INC; 4278 if (lun->be_lun->blocksize) 4279 ival /= lun->be_lun->blocksize; 4280 else 4281 ival /= 512; 4282 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4283 page->descr[1].count); 4284 } 4285 value = ctl_get_opt(&lun->be_lun->options, 4286 "pool-avail-threshold"); 4287 if (value != NULL && 4288 ctl_expand_number(value, &ival) == 0) { 4289 page->descr[2].flags |= SLBPPD_ENABLED | 4290 SLBPPD_ARMING_DEC; 4291 if (lun->be_lun->blocksize) 4292 ival /= lun->be_lun->blocksize; 4293 else 4294 ival /= 512; 4295 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4296 page->descr[2].count); 4297 } 4298 value = ctl_get_opt(&lun->be_lun->options, 4299 "pool-used-threshold"); 4300 if (value != NULL && 4301 ctl_expand_number(value, &ival) == 0) { 4302 page->descr[3].flags |= SLBPPD_ENABLED | 4303 SLBPPD_ARMING_INC; 4304 if (lun->be_lun->blocksize) 4305 ival /= lun->be_lun->blocksize; 4306 else 4307 ival /= 512; 4308 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4309 page->descr[3].count); 4310 } 4311 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4312 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4313 sizeof(lbp_page_default)); 4314 page_index->page_data = 4315 (uint8_t *)lun->mode_pages.lbp_page; 4316 break; 4317 } 4318 default: 4319 panic("subpage %#x for page %#x is incorrect!", 4320 page_index->subpage, page_code); 4321 } 4322 break; 4323 } 4324 case SMS_CDDVD_CAPS_PAGE:{ 4325 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4326 ("subpage %#x for page %#x is incorrect!", 4327 page_index->subpage, page_code)); 4328 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4329 &cddvd_page_default, 4330 sizeof(cddvd_page_default)); 4331 memcpy(&lun->mode_pages.cddvd_page[ 4332 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4333 sizeof(cddvd_page_changeable)); 4334 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4335 &cddvd_page_default, 4336 sizeof(cddvd_page_default)); 4337 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4338 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4339 sizeof(cddvd_page_default)); 4340 page_index->page_data = 4341 (uint8_t *)lun->mode_pages.cddvd_page; 4342 break; 4343 } 4344 default: 4345 panic("invalid page code value %#x", page_code); 4346 } 4347 } 4348 4349 return (CTL_RETVAL_COMPLETE); 4350} 4351 4352static int 4353ctl_init_log_page_index(struct ctl_lun *lun) 4354{ 4355 struct ctl_page_index *page_index; 4356 int i, j, k, prev; 4357 4358 memcpy(&lun->log_pages.index, log_page_index_template, 4359 sizeof(log_page_index_template)); 4360 4361 prev = -1; 4362 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4363 4364 page_index = &lun->log_pages.index[i]; 4365 if (lun->be_lun->lun_type == T_DIRECT && 4366 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4367 continue; 4368 if (lun->be_lun->lun_type == T_PROCESSOR && 4369 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4370 continue; 4371 if (lun->be_lun->lun_type == T_CDROM && 4372 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4373 continue; 4374 4375 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4376 lun->backend->lun_attr == NULL) 4377 continue; 4378 4379 if (page_index->page_code != prev) { 4380 lun->log_pages.pages_page[j] = page_index->page_code; 4381 prev = page_index->page_code; 4382 j++; 4383 } 4384 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4385 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4386 k++; 4387 } 4388 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4389 lun->log_pages.index[0].page_len = j; 4390 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4391 lun->log_pages.index[1].page_len = k * 2; 4392 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4393 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4394 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4395 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4396 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4397 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4398 4399 return (CTL_RETVAL_COMPLETE); 4400} 4401 4402static int 4403hex2bin(const char *str, uint8_t *buf, int buf_size) 4404{ 4405 int i; 4406 u_char c; 4407 4408 memset(buf, 0, buf_size); 4409 while (isspace(str[0])) 4410 str++; 4411 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4412 str += 2; 4413 buf_size *= 2; 4414 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4415 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4416 str++; 4417 c = str[i]; 4418 if (isdigit(c)) 4419 c -= '0'; 4420 else if (isalpha(c)) 4421 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4422 else 4423 break; 4424 if (c >= 16) 4425 break; 4426 if ((i & 1) == 0) 4427 buf[i / 2] |= (c << 4); 4428 else 4429 buf[i / 2] |= c; 4430 } 4431 return ((i + 1) / 2); 4432} 4433 4434/* 4435 * LUN allocation. 4436 * 4437 * Requirements: 4438 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4439 * wants us to allocate the LUN and he can block. 4440 * - ctl_softc is always set 4441 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4442 * 4443 * Returns 0 for success, non-zero (errno) for failure. 4444 */ 4445static int 4446ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4447 struct ctl_be_lun *const be_lun) 4448{ 4449 struct ctl_lun *nlun, *lun; 4450 struct scsi_vpd_id_descriptor *desc; 4451 struct scsi_vpd_id_t10 *t10id; 4452 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4453 int lun_number, lun_malloced; 4454 int devidlen, idlen1, idlen2 = 0, len; 4455 4456 if (be_lun == NULL) 4457 return (EINVAL); 4458 4459 /* 4460 * We currently only support Direct Access or Processor LUN types. 4461 */ 4462 switch (be_lun->lun_type) { 4463 case T_DIRECT: 4464 case T_PROCESSOR: 4465 case T_CDROM: 4466 break; 4467 case T_SEQUENTIAL: 4468 case T_CHANGER: 4469 default: 4470 be_lun->lun_config_status(be_lun->be_lun, 4471 CTL_LUN_CONFIG_FAILURE); 4472 break; 4473 } 4474 if (ctl_lun == NULL) { 4475 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4476 lun_malloced = 1; 4477 } else { 4478 lun_malloced = 0; 4479 lun = ctl_lun; 4480 } 4481 4482 memset(lun, 0, sizeof(*lun)); 4483 if (lun_malloced) 4484 lun->flags = CTL_LUN_MALLOCED; 4485 4486 /* Generate LUN ID. */ 4487 devidlen = max(CTL_DEVID_MIN_LEN, 4488 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4489 idlen1 = sizeof(*t10id) + devidlen; 4490 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4491 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4492 if (scsiname != NULL) { 4493 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4494 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4495 } 4496 eui = ctl_get_opt(&be_lun->options, "eui"); 4497 if (eui != NULL) { 4498 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4499 } 4500 naa = ctl_get_opt(&be_lun->options, "naa"); 4501 if (naa != NULL) { 4502 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4503 } 4504 uuid = ctl_get_opt(&be_lun->options, "uuid"); 4505 if (uuid != NULL) { 4506 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4507 } 4508 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4509 M_CTL, M_WAITOK | M_ZERO); 4510 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4511 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4512 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4513 desc->length = idlen1; 4514 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4515 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4516 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4517 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4518 } else { 4519 strncpy(t10id->vendor, vendor, 4520 min(sizeof(t10id->vendor), strlen(vendor))); 4521 } 4522 strncpy((char *)t10id->vendor_spec_id, 4523 (char *)be_lun->device_id, devidlen); 4524 if (scsiname != NULL) { 4525 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4526 desc->length); 4527 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4528 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4529 SVPD_ID_TYPE_SCSI_NAME; 4530 desc->length = idlen2; 4531 strlcpy(desc->identifier, scsiname, idlen2); 4532 } 4533 if (eui != NULL) { 4534 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4535 desc->length); 4536 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4537 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4538 SVPD_ID_TYPE_EUI64; 4539 desc->length = hex2bin(eui, desc->identifier, 16); 4540 desc->length = desc->length > 12 ? 16 : 4541 (desc->length > 8 ? 12 : 8); 4542 len -= 16 - desc->length; 4543 } 4544 if (naa != NULL) { 4545 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4546 desc->length); 4547 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4548 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4549 SVPD_ID_TYPE_NAA; 4550 desc->length = hex2bin(naa, desc->identifier, 16); 4551 desc->length = desc->length > 8 ? 16 : 8; 4552 len -= 16 - desc->length; 4553 } 4554 if (uuid != NULL) { 4555 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4556 desc->length); 4557 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4558 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4559 SVPD_ID_TYPE_UUID; 4560 desc->identifier[0] = 0x10; 4561 hex2bin(uuid, &desc->identifier[2], 16); 4562 desc->length = 18; 4563 } 4564 lun->lun_devid->len = len; 4565 4566 mtx_lock(&ctl_softc->ctl_lock); 4567 /* 4568 * See if the caller requested a particular LUN number. If so, see 4569 * if it is available. Otherwise, allocate the first available LUN. 4570 */ 4571 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4572 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4573 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4574 mtx_unlock(&ctl_softc->ctl_lock); 4575 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4576 printf("ctl: requested LUN ID %d is higher " 4577 "than CTL_MAX_LUNS - 1 (%d)\n", 4578 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4579 } else { 4580 /* 4581 * XXX KDM return an error, or just assign 4582 * another LUN ID in this case?? 4583 */ 4584 printf("ctl: requested LUN ID %d is already " 4585 "in use\n", be_lun->req_lun_id); 4586 } 4587 if (lun->flags & CTL_LUN_MALLOCED) 4588 free(lun, M_CTL); 4589 be_lun->lun_config_status(be_lun->be_lun, 4590 CTL_LUN_CONFIG_FAILURE); 4591 return (ENOSPC); 4592 } 4593 lun_number = be_lun->req_lun_id; 4594 } else { 4595 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4596 if (lun_number == -1) { 4597 mtx_unlock(&ctl_softc->ctl_lock); 4598 printf("ctl: can't allocate LUN, out of LUNs\n"); 4599 if (lun->flags & CTL_LUN_MALLOCED) 4600 free(lun, M_CTL); 4601 be_lun->lun_config_status(be_lun->be_lun, 4602 CTL_LUN_CONFIG_FAILURE); 4603 return (ENOSPC); 4604 } 4605 } 4606 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4607 4608 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4609 lun->lun = lun_number; 4610 lun->be_lun = be_lun; 4611 /* 4612 * The processor LUN is always enabled. Disk LUNs come on line 4613 * disabled, and must be enabled by the backend. 4614 */ 4615 lun->flags |= CTL_LUN_DISABLED; 4616 lun->backend = be_lun->be; 4617 be_lun->ctl_lun = lun; 4618 be_lun->lun_id = lun_number; 4619 atomic_add_int(&be_lun->be->num_luns, 1); 4620 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4621 lun->flags |= CTL_LUN_EJECTED; 4622 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4623 lun->flags |= CTL_LUN_NO_MEDIA; 4624 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4625 lun->flags |= CTL_LUN_STOPPED; 4626 4627 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4628 lun->flags |= CTL_LUN_PRIMARY_SC; 4629 4630 value = ctl_get_opt(&be_lun->options, "removable"); 4631 if (value != NULL) { 4632 if (strcmp(value, "on") == 0) 4633 lun->flags |= CTL_LUN_REMOVABLE; 4634 } else if (be_lun->lun_type == T_CDROM) 4635 lun->flags |= CTL_LUN_REMOVABLE; 4636 4637 lun->ctl_softc = ctl_softc; 4638#ifdef CTL_TIME_IO 4639 lun->last_busy = getsbinuptime(); 4640#endif 4641 TAILQ_INIT(&lun->ooa_queue); 4642 TAILQ_INIT(&lun->blocked_queue); 4643 STAILQ_INIT(&lun->error_list); 4644 lun->ie_reported = 1; 4645 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4646 ctl_tpc_lun_init(lun); 4647 if (lun->flags & CTL_LUN_REMOVABLE) { 4648 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4649 M_CTL, M_WAITOK); 4650 } 4651 4652 /* 4653 * Initialize the mode and log page index. 4654 */ 4655 ctl_init_page_index(lun); 4656 ctl_init_log_page_index(lun); 4657 4658 /* 4659 * Now, before we insert this lun on the lun list, set the lun 4660 * inventory changed UA for all other luns. 4661 */ 4662 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4663 mtx_lock(&nlun->lun_lock); 4664 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4665 mtx_unlock(&nlun->lun_lock); 4666 } 4667 4668 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4669 4670 ctl_softc->ctl_luns[lun_number] = lun; 4671 4672 ctl_softc->num_luns++; 4673 4674 /* Setup statistics gathering */ 4675#ifdef CTL_LEGACY_STATS 4676 lun->legacy_stats.device_type = be_lun->lun_type; 4677 lun->legacy_stats.lun_number = lun_number; 4678 lun->legacy_stats.blocksize = be_lun->blocksize; 4679 if (be_lun->blocksize == 0) 4680 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4681 for (len = 0; len < CTL_MAX_PORTS; len++) 4682 lun->legacy_stats.ports[len].targ_port = len; 4683#endif /* CTL_LEGACY_STATS */ 4684 lun->stats.item = lun_number; 4685 4686 mtx_unlock(&ctl_softc->ctl_lock); 4687 4688 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4689 return (0); 4690} 4691 4692/* 4693 * Delete a LUN. 4694 * Assumptions: 4695 * - LUN has already been marked invalid and any pending I/O has been taken 4696 * care of. 4697 */ 4698static int 4699ctl_free_lun(struct ctl_lun *lun) 4700{ 4701 struct ctl_softc *softc = lun->ctl_softc; 4702 struct ctl_lun *nlun; 4703 int i; 4704 4705 mtx_assert(&softc->ctl_lock, MA_OWNED); 4706 4707 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4708 4709 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4710 4711 softc->ctl_luns[lun->lun] = NULL; 4712 4713 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4714 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4715 4716 softc->num_luns--; 4717 4718 /* 4719 * Tell the backend to free resources, if this LUN has a backend. 4720 */ 4721 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4722 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4723 4724 lun->ie_reportcnt = UINT32_MAX; 4725 callout_drain(&lun->ie_callout); 4726 4727 ctl_tpc_lun_shutdown(lun); 4728 mtx_destroy(&lun->lun_lock); 4729 free(lun->lun_devid, M_CTL); 4730 for (i = 0; i < CTL_MAX_PORTS; i++) 4731 free(lun->pending_ua[i], M_CTL); 4732 for (i = 0; i < CTL_MAX_PORTS; i++) 4733 free(lun->pr_keys[i], M_CTL); 4734 free(lun->write_buffer, M_CTL); 4735 free(lun->prevent, M_CTL); 4736 if (lun->flags & CTL_LUN_MALLOCED) 4737 free(lun, M_CTL); 4738 4739 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4740 mtx_lock(&nlun->lun_lock); 4741 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4742 mtx_unlock(&nlun->lun_lock); 4743 } 4744 4745 return (0); 4746} 4747 4748static void 4749ctl_create_lun(struct ctl_be_lun *be_lun) 4750{ 4751 4752 /* 4753 * ctl_alloc_lun() should handle all potential failure cases. 4754 */ 4755 ctl_alloc_lun(control_softc, NULL, be_lun); 4756} 4757 4758int 4759ctl_add_lun(struct ctl_be_lun *be_lun) 4760{ 4761 struct ctl_softc *softc = control_softc; 4762 4763 mtx_lock(&softc->ctl_lock); 4764 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4765 mtx_unlock(&softc->ctl_lock); 4766 wakeup(&softc->pending_lun_queue); 4767 4768 return (0); 4769} 4770 4771int 4772ctl_enable_lun(struct ctl_be_lun *be_lun) 4773{ 4774 struct ctl_softc *softc; 4775 struct ctl_port *port, *nport; 4776 struct ctl_lun *lun; 4777 int retval; 4778 4779 lun = (struct ctl_lun *)be_lun->ctl_lun; 4780 softc = lun->ctl_softc; 4781 4782 mtx_lock(&softc->ctl_lock); 4783 mtx_lock(&lun->lun_lock); 4784 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4785 /* 4786 * eh? Why did we get called if the LUN is already 4787 * enabled? 4788 */ 4789 mtx_unlock(&lun->lun_lock); 4790 mtx_unlock(&softc->ctl_lock); 4791 return (0); 4792 } 4793 lun->flags &= ~CTL_LUN_DISABLED; 4794 mtx_unlock(&lun->lun_lock); 4795 4796 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4797 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4798 port->lun_map != NULL || port->lun_enable == NULL) 4799 continue; 4800 4801 /* 4802 * Drop the lock while we call the FETD's enable routine. 4803 * This can lead to a callback into CTL (at least in the 4804 * case of the internal initiator frontend. 4805 */ 4806 mtx_unlock(&softc->ctl_lock); 4807 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4808 mtx_lock(&softc->ctl_lock); 4809 if (retval != 0) { 4810 printf("%s: FETD %s port %d returned error " 4811 "%d for lun_enable on lun %jd\n", 4812 __func__, port->port_name, port->targ_port, 4813 retval, (intmax_t)lun->lun); 4814 } 4815 } 4816 4817 mtx_unlock(&softc->ctl_lock); 4818 ctl_isc_announce_lun(lun); 4819 4820 return (0); 4821} 4822 4823int 4824ctl_disable_lun(struct ctl_be_lun *be_lun) 4825{ 4826 struct ctl_softc *softc; 4827 struct ctl_port *port; 4828 struct ctl_lun *lun; 4829 int retval; 4830 4831 lun = (struct ctl_lun *)be_lun->ctl_lun; 4832 softc = lun->ctl_softc; 4833 4834 mtx_lock(&softc->ctl_lock); 4835 mtx_lock(&lun->lun_lock); 4836 if (lun->flags & CTL_LUN_DISABLED) { 4837 mtx_unlock(&lun->lun_lock); 4838 mtx_unlock(&softc->ctl_lock); 4839 return (0); 4840 } 4841 lun->flags |= CTL_LUN_DISABLED; 4842 mtx_unlock(&lun->lun_lock); 4843 4844 STAILQ_FOREACH(port, &softc->port_list, links) { 4845 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4846 port->lun_map != NULL || port->lun_disable == NULL) 4847 continue; 4848 4849 /* 4850 * Drop the lock before we call the frontend's disable 4851 * routine, to avoid lock order reversals. 4852 * 4853 * XXX KDM what happens if the frontend list changes while 4854 * we're traversing it? It's unlikely, but should be handled. 4855 */ 4856 mtx_unlock(&softc->ctl_lock); 4857 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4858 mtx_lock(&softc->ctl_lock); 4859 if (retval != 0) { 4860 printf("%s: FETD %s port %d returned error " 4861 "%d for lun_disable on lun %jd\n", 4862 __func__, port->port_name, port->targ_port, 4863 retval, (intmax_t)lun->lun); 4864 } 4865 } 4866 4867 mtx_unlock(&softc->ctl_lock); 4868 ctl_isc_announce_lun(lun); 4869 4870 return (0); 4871} 4872 4873int 4874ctl_start_lun(struct ctl_be_lun *be_lun) 4875{ 4876 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4877 4878 mtx_lock(&lun->lun_lock); 4879 lun->flags &= ~CTL_LUN_STOPPED; 4880 mtx_unlock(&lun->lun_lock); 4881 return (0); 4882} 4883 4884int 4885ctl_stop_lun(struct ctl_be_lun *be_lun) 4886{ 4887 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4888 4889 mtx_lock(&lun->lun_lock); 4890 lun->flags |= CTL_LUN_STOPPED; 4891 mtx_unlock(&lun->lun_lock); 4892 return (0); 4893} 4894 4895int 4896ctl_lun_no_media(struct ctl_be_lun *be_lun) 4897{ 4898 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4899 4900 mtx_lock(&lun->lun_lock); 4901 lun->flags |= CTL_LUN_NO_MEDIA; 4902 mtx_unlock(&lun->lun_lock); 4903 return (0); 4904} 4905 4906int 4907ctl_lun_has_media(struct ctl_be_lun *be_lun) 4908{ 4909 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4910 union ctl_ha_msg msg; 4911 4912 mtx_lock(&lun->lun_lock); 4913 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4914 if (lun->flags & CTL_LUN_REMOVABLE) 4915 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4916 mtx_unlock(&lun->lun_lock); 4917 if ((lun->flags & CTL_LUN_REMOVABLE) && 4918 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4919 bzero(&msg.ua, sizeof(msg.ua)); 4920 msg.hdr.msg_type = CTL_MSG_UA; 4921 msg.hdr.nexus.initid = -1; 4922 msg.hdr.nexus.targ_port = -1; 4923 msg.hdr.nexus.targ_lun = lun->lun; 4924 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4925 msg.ua.ua_all = 1; 4926 msg.ua.ua_set = 1; 4927 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4928 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4929 M_WAITOK); 4930 } 4931 return (0); 4932} 4933 4934int 4935ctl_lun_ejected(struct ctl_be_lun *be_lun) 4936{ 4937 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4938 4939 mtx_lock(&lun->lun_lock); 4940 lun->flags |= CTL_LUN_EJECTED; 4941 mtx_unlock(&lun->lun_lock); 4942 return (0); 4943} 4944 4945int 4946ctl_lun_primary(struct ctl_be_lun *be_lun) 4947{ 4948 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4949 4950 mtx_lock(&lun->lun_lock); 4951 lun->flags |= CTL_LUN_PRIMARY_SC; 4952 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4953 mtx_unlock(&lun->lun_lock); 4954 ctl_isc_announce_lun(lun); 4955 return (0); 4956} 4957 4958int 4959ctl_lun_secondary(struct ctl_be_lun *be_lun) 4960{ 4961 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4962 4963 mtx_lock(&lun->lun_lock); 4964 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4965 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4966 mtx_unlock(&lun->lun_lock); 4967 ctl_isc_announce_lun(lun); 4968 return (0); 4969} 4970 4971int 4972ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4973{ 4974 struct ctl_softc *softc; 4975 struct ctl_lun *lun; 4976 4977 lun = (struct ctl_lun *)be_lun->ctl_lun; 4978 softc = lun->ctl_softc; 4979 4980 mtx_lock(&lun->lun_lock); 4981 4982 /* 4983 * The LUN needs to be disabled before it can be marked invalid. 4984 */ 4985 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4986 mtx_unlock(&lun->lun_lock); 4987 return (-1); 4988 } 4989 /* 4990 * Mark the LUN invalid. 4991 */ 4992 lun->flags |= CTL_LUN_INVALID; 4993 4994 /* 4995 * If there is nothing in the OOA queue, go ahead and free the LUN. 4996 * If we have something in the OOA queue, we'll free it when the 4997 * last I/O completes. 4998 */ 4999 if (TAILQ_EMPTY(&lun->ooa_queue)) { 5000 mtx_unlock(&lun->lun_lock); 5001 mtx_lock(&softc->ctl_lock); 5002 ctl_free_lun(lun); 5003 mtx_unlock(&softc->ctl_lock); 5004 } else 5005 mtx_unlock(&lun->lun_lock); 5006 5007 return (0); 5008} 5009 5010void 5011ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5012{ 5013 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5014 union ctl_ha_msg msg; 5015 5016 mtx_lock(&lun->lun_lock); 5017 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 5018 mtx_unlock(&lun->lun_lock); 5019 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 5020 /* Send msg to other side. */ 5021 bzero(&msg.ua, sizeof(msg.ua)); 5022 msg.hdr.msg_type = CTL_MSG_UA; 5023 msg.hdr.nexus.initid = -1; 5024 msg.hdr.nexus.targ_port = -1; 5025 msg.hdr.nexus.targ_lun = lun->lun; 5026 msg.hdr.nexus.targ_mapped_lun = lun->lun; 5027 msg.ua.ua_all = 1; 5028 msg.ua.ua_set = 1; 5029 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 5030 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 5031 M_WAITOK); 5032 } 5033} 5034 5035/* 5036 * Backend "memory move is complete" callback for requests that never 5037 * make it down to say RAIDCore's configuration code. 5038 */ 5039int 5040ctl_config_move_done(union ctl_io *io) 5041{ 5042 int retval; 5043 5044 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5045 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5046 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5047 5048 if ((io->io_hdr.port_status != 0) && 5049 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5050 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5051 /* 5052 * For hardware error sense keys, the sense key 5053 * specific value is defined to be a retry count, 5054 * but we use it to pass back an internal FETD 5055 * error code. XXX KDM Hopefully the FETD is only 5056 * using 16 bits for an error code, since that's 5057 * all the space we have in the sks field. 5058 */ 5059 ctl_set_internal_failure(&io->scsiio, 5060 /*sks_valid*/ 1, 5061 /*retry_count*/ 5062 io->io_hdr.port_status); 5063 } 5064 5065 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5066 ctl_data_print(io); 5067 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5068 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5069 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5070 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5071 /* 5072 * XXX KDM just assuming a single pointer here, and not a 5073 * S/G list. If we start using S/G lists for config data, 5074 * we'll need to know how to clean them up here as well. 5075 */ 5076 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5077 free(io->scsiio.kern_data_ptr, M_CTL); 5078 ctl_done(io); 5079 retval = CTL_RETVAL_COMPLETE; 5080 } else { 5081 /* 5082 * XXX KDM now we need to continue data movement. Some 5083 * options: 5084 * - call ctl_scsiio() again? We don't do this for data 5085 * writes, because for those at least we know ahead of 5086 * time where the write will go and how long it is. For 5087 * config writes, though, that information is largely 5088 * contained within the write itself, thus we need to 5089 * parse out the data again. 5090 * 5091 * - Call some other function once the data is in? 5092 */ 5093 5094 /* 5095 * XXX KDM call ctl_scsiio() again for now, and check flag 5096 * bits to see whether we're allocated or not. 5097 */ 5098 retval = ctl_scsiio(&io->scsiio); 5099 } 5100 return (retval); 5101} 5102 5103/* 5104 * This gets called by a backend driver when it is done with a 5105 * data_submit method. 5106 */ 5107void 5108ctl_data_submit_done(union ctl_io *io) 5109{ 5110 /* 5111 * If the IO_CONT flag is set, we need to call the supplied 5112 * function to continue processing the I/O, instead of completing 5113 * the I/O just yet. 5114 * 5115 * If there is an error, though, we don't want to keep processing. 5116 * Instead, just send status back to the initiator. 5117 */ 5118 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5119 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5120 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5121 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5122 io->scsiio.io_cont(io); 5123 return; 5124 } 5125 ctl_done(io); 5126} 5127 5128/* 5129 * This gets called by a backend driver when it is done with a 5130 * configuration write. 5131 */ 5132void 5133ctl_config_write_done(union ctl_io *io) 5134{ 5135 uint8_t *buf; 5136 5137 /* 5138 * If the IO_CONT flag is set, we need to call the supplied 5139 * function to continue processing the I/O, instead of completing 5140 * the I/O just yet. 5141 * 5142 * If there is an error, though, we don't want to keep processing. 5143 * Instead, just send status back to the initiator. 5144 */ 5145 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5146 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5147 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5148 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5149 io->scsiio.io_cont(io); 5150 return; 5151 } 5152 /* 5153 * Since a configuration write can be done for commands that actually 5154 * have data allocated, like write buffer, and commands that have 5155 * no data, like start/stop unit, we need to check here. 5156 */ 5157 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5158 buf = io->scsiio.kern_data_ptr; 5159 else 5160 buf = NULL; 5161 ctl_done(io); 5162 if (buf) 5163 free(buf, M_CTL); 5164} 5165 5166void 5167ctl_config_read_done(union ctl_io *io) 5168{ 5169 uint8_t *buf; 5170 5171 /* 5172 * If there is some error -- we are done, skip data transfer. 5173 */ 5174 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5175 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5176 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5177 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5178 buf = io->scsiio.kern_data_ptr; 5179 else 5180 buf = NULL; 5181 ctl_done(io); 5182 if (buf) 5183 free(buf, M_CTL); 5184 return; 5185 } 5186 5187 /* 5188 * If the IO_CONT flag is set, we need to call the supplied 5189 * function to continue processing the I/O, instead of completing 5190 * the I/O just yet. 5191 */ 5192 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5193 io->scsiio.io_cont(io); 5194 return; 5195 } 5196 5197 ctl_datamove(io); 5198} 5199 5200/* 5201 * SCSI release command. 5202 */ 5203int 5204ctl_scsi_release(struct ctl_scsiio *ctsio) 5205{ 5206 struct ctl_lun *lun = CTL_LUN(ctsio); 5207 uint32_t residx; 5208 5209 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5210 5211 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5212 5213 /* 5214 * XXX KDM right now, we only support LUN reservation. We don't 5215 * support 3rd party reservations, or extent reservations, which 5216 * might actually need the parameter list. If we've gotten this 5217 * far, we've got a LUN reservation. Anything else got kicked out 5218 * above. So, according to SPC, ignore the length. 5219 */ 5220 5221 mtx_lock(&lun->lun_lock); 5222 5223 /* 5224 * According to SPC, it is not an error for an intiator to attempt 5225 * to release a reservation on a LUN that isn't reserved, or that 5226 * is reserved by another initiator. The reservation can only be 5227 * released, though, by the initiator who made it or by one of 5228 * several reset type events. 5229 */ 5230 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5231 lun->flags &= ~CTL_LUN_RESERVED; 5232 5233 mtx_unlock(&lun->lun_lock); 5234 5235 ctl_set_success(ctsio); 5236 ctl_done((union ctl_io *)ctsio); 5237 return (CTL_RETVAL_COMPLETE); 5238} 5239 5240int 5241ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5242{ 5243 struct ctl_lun *lun = CTL_LUN(ctsio); 5244 uint32_t residx; 5245 5246 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5247 5248 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5249 5250 /* 5251 * XXX KDM right now, we only support LUN reservation. We don't 5252 * support 3rd party reservations, or extent reservations, which 5253 * might actually need the parameter list. If we've gotten this 5254 * far, we've got a LUN reservation. Anything else got kicked out 5255 * above. So, according to SPC, ignore the length. 5256 */ 5257 5258 mtx_lock(&lun->lun_lock); 5259 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5260 ctl_set_reservation_conflict(ctsio); 5261 goto bailout; 5262 } 5263 5264 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5265 if (lun->flags & CTL_LUN_PR_RESERVED) { 5266 ctl_set_success(ctsio); 5267 goto bailout; 5268 } 5269 5270 lun->flags |= CTL_LUN_RESERVED; 5271 lun->res_idx = residx; 5272 ctl_set_success(ctsio); 5273 5274bailout: 5275 mtx_unlock(&lun->lun_lock); 5276 ctl_done((union ctl_io *)ctsio); 5277 return (CTL_RETVAL_COMPLETE); 5278} 5279 5280int 5281ctl_start_stop(struct ctl_scsiio *ctsio) 5282{ 5283 struct ctl_lun *lun = CTL_LUN(ctsio); 5284 struct scsi_start_stop_unit *cdb; 5285 int retval; 5286 5287 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5288 5289 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5290 5291 if ((cdb->how & SSS_PC_MASK) == 0) { 5292 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5293 (cdb->how & SSS_START) == 0) { 5294 uint32_t residx; 5295 5296 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5297 if (ctl_get_prkey(lun, residx) == 0 || 5298 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5299 5300 ctl_set_reservation_conflict(ctsio); 5301 ctl_done((union ctl_io *)ctsio); 5302 return (CTL_RETVAL_COMPLETE); 5303 } 5304 } 5305 5306 if ((cdb->how & SSS_LOEJ) && 5307 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5308 ctl_set_invalid_field(ctsio, 5309 /*sks_valid*/ 1, 5310 /*command*/ 1, 5311 /*field*/ 4, 5312 /*bit_valid*/ 1, 5313 /*bit*/ 1); 5314 ctl_done((union ctl_io *)ctsio); 5315 return (CTL_RETVAL_COMPLETE); 5316 } 5317 5318 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5319 lun->prevent_count > 0) { 5320 /* "Medium removal prevented" */ 5321 ctl_set_sense(ctsio, /*current_error*/ 1, 5322 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5323 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5324 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5325 ctl_done((union ctl_io *)ctsio); 5326 return (CTL_RETVAL_COMPLETE); 5327 } 5328 } 5329 5330 retval = lun->backend->config_write((union ctl_io *)ctsio); 5331 return (retval); 5332} 5333 5334int 5335ctl_prevent_allow(struct ctl_scsiio *ctsio) 5336{ 5337 struct ctl_lun *lun = CTL_LUN(ctsio); 5338 struct scsi_prevent *cdb; 5339 int retval; 5340 uint32_t initidx; 5341 5342 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5343 5344 cdb = (struct scsi_prevent *)ctsio->cdb; 5345 5346 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5347 ctl_set_invalid_opcode(ctsio); 5348 ctl_done((union ctl_io *)ctsio); 5349 return (CTL_RETVAL_COMPLETE); 5350 } 5351 5352 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5353 mtx_lock(&lun->lun_lock); 5354 if ((cdb->how & PR_PREVENT) && 5355 ctl_is_set(lun->prevent, initidx) == 0) { 5356 ctl_set_mask(lun->prevent, initidx); 5357 lun->prevent_count++; 5358 } else if ((cdb->how & PR_PREVENT) == 0 && 5359 ctl_is_set(lun->prevent, initidx)) { 5360 ctl_clear_mask(lun->prevent, initidx); 5361 lun->prevent_count--; 5362 } 5363 mtx_unlock(&lun->lun_lock); 5364 retval = lun->backend->config_write((union ctl_io *)ctsio); 5365 return (retval); 5366} 5367 5368/* 5369 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5370 * we don't really do anything with the LBA and length fields if the user 5371 * passes them in. Instead we'll just flush out the cache for the entire 5372 * LUN. 5373 */ 5374int 5375ctl_sync_cache(struct ctl_scsiio *ctsio) 5376{ 5377 struct ctl_lun *lun = CTL_LUN(ctsio); 5378 struct ctl_lba_len_flags *lbalen; 5379 uint64_t starting_lba; 5380 uint32_t block_count; 5381 int retval; 5382 uint8_t byte2; 5383 5384 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5385 5386 retval = 0; 5387 5388 switch (ctsio->cdb[0]) { 5389 case SYNCHRONIZE_CACHE: { 5390 struct scsi_sync_cache *cdb; 5391 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5392 5393 starting_lba = scsi_4btoul(cdb->begin_lba); 5394 block_count = scsi_2btoul(cdb->lb_count); 5395 byte2 = cdb->byte2; 5396 break; 5397 } 5398 case SYNCHRONIZE_CACHE_16: { 5399 struct scsi_sync_cache_16 *cdb; 5400 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5401 5402 starting_lba = scsi_8btou64(cdb->begin_lba); 5403 block_count = scsi_4btoul(cdb->lb_count); 5404 byte2 = cdb->byte2; 5405 break; 5406 } 5407 default: 5408 ctl_set_invalid_opcode(ctsio); 5409 ctl_done((union ctl_io *)ctsio); 5410 goto bailout; 5411 break; /* NOTREACHED */ 5412 } 5413 5414 /* 5415 * We check the LBA and length, but don't do anything with them. 5416 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5417 * get flushed. This check will just help satisfy anyone who wants 5418 * to see an error for an out of range LBA. 5419 */ 5420 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5421 ctl_set_lba_out_of_range(ctsio, 5422 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5423 ctl_done((union ctl_io *)ctsio); 5424 goto bailout; 5425 } 5426 5427 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5428 lbalen->lba = starting_lba; 5429 lbalen->len = block_count; 5430 lbalen->flags = byte2; 5431 retval = lun->backend->config_write((union ctl_io *)ctsio); 5432 5433bailout: 5434 return (retval); 5435} 5436 5437int 5438ctl_format(struct ctl_scsiio *ctsio) 5439{ 5440 struct scsi_format *cdb; 5441 int length, defect_list_len; 5442 5443 CTL_DEBUG_PRINT(("ctl_format\n")); 5444 5445 cdb = (struct scsi_format *)ctsio->cdb; 5446 5447 length = 0; 5448 if (cdb->byte2 & SF_FMTDATA) { 5449 if (cdb->byte2 & SF_LONGLIST) 5450 length = sizeof(struct scsi_format_header_long); 5451 else 5452 length = sizeof(struct scsi_format_header_short); 5453 } 5454 5455 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5456 && (length > 0)) { 5457 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5458 ctsio->kern_data_len = length; 5459 ctsio->kern_total_len = length; 5460 ctsio->kern_data_resid = 0; 5461 ctsio->kern_rel_offset = 0; 5462 ctsio->kern_sg_entries = 0; 5463 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5464 ctsio->be_move_done = ctl_config_move_done; 5465 ctl_datamove((union ctl_io *)ctsio); 5466 5467 return (CTL_RETVAL_COMPLETE); 5468 } 5469 5470 defect_list_len = 0; 5471 5472 if (cdb->byte2 & SF_FMTDATA) { 5473 if (cdb->byte2 & SF_LONGLIST) { 5474 struct scsi_format_header_long *header; 5475 5476 header = (struct scsi_format_header_long *) 5477 ctsio->kern_data_ptr; 5478 5479 defect_list_len = scsi_4btoul(header->defect_list_len); 5480 if (defect_list_len != 0) { 5481 ctl_set_invalid_field(ctsio, 5482 /*sks_valid*/ 1, 5483 /*command*/ 0, 5484 /*field*/ 2, 5485 /*bit_valid*/ 0, 5486 /*bit*/ 0); 5487 goto bailout; 5488 } 5489 } else { 5490 struct scsi_format_header_short *header; 5491 5492 header = (struct scsi_format_header_short *) 5493 ctsio->kern_data_ptr; 5494 5495 defect_list_len = scsi_2btoul(header->defect_list_len); 5496 if (defect_list_len != 0) { 5497 ctl_set_invalid_field(ctsio, 5498 /*sks_valid*/ 1, 5499 /*command*/ 0, 5500 /*field*/ 2, 5501 /*bit_valid*/ 0, 5502 /*bit*/ 0); 5503 goto bailout; 5504 } 5505 } 5506 } 5507 5508 ctl_set_success(ctsio); 5509bailout: 5510 5511 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5512 free(ctsio->kern_data_ptr, M_CTL); 5513 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5514 } 5515 5516 ctl_done((union ctl_io *)ctsio); 5517 return (CTL_RETVAL_COMPLETE); 5518} 5519 5520int 5521ctl_read_buffer(struct ctl_scsiio *ctsio) 5522{ 5523 struct ctl_lun *lun = CTL_LUN(ctsio); 5524 uint64_t buffer_offset; 5525 uint32_t len; 5526 uint8_t byte2; 5527 static uint8_t descr[4]; 5528 static uint8_t echo_descr[4] = { 0 }; 5529 5530 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5531 5532 switch (ctsio->cdb[0]) { 5533 case READ_BUFFER: { 5534 struct scsi_read_buffer *cdb; 5535 5536 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5537 buffer_offset = scsi_3btoul(cdb->offset); 5538 len = scsi_3btoul(cdb->length); 5539 byte2 = cdb->byte2; 5540 break; 5541 } 5542 case READ_BUFFER_16: { 5543 struct scsi_read_buffer_16 *cdb; 5544 5545 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5546 buffer_offset = scsi_8btou64(cdb->offset); 5547 len = scsi_4btoul(cdb->length); 5548 byte2 = cdb->byte2; 5549 break; 5550 } 5551 default: /* This shouldn't happen. */ 5552 ctl_set_invalid_opcode(ctsio); 5553 ctl_done((union ctl_io *)ctsio); 5554 return (CTL_RETVAL_COMPLETE); 5555 } 5556 5557 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5558 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5559 ctl_set_invalid_field(ctsio, 5560 /*sks_valid*/ 1, 5561 /*command*/ 1, 5562 /*field*/ 6, 5563 /*bit_valid*/ 0, 5564 /*bit*/ 0); 5565 ctl_done((union ctl_io *)ctsio); 5566 return (CTL_RETVAL_COMPLETE); 5567 } 5568 5569 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5570 descr[0] = 0; 5571 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5572 ctsio->kern_data_ptr = descr; 5573 len = min(len, sizeof(descr)); 5574 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5575 ctsio->kern_data_ptr = echo_descr; 5576 len = min(len, sizeof(echo_descr)); 5577 } else { 5578 if (lun->write_buffer == NULL) { 5579 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5580 M_CTL, M_WAITOK); 5581 } 5582 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5583 } 5584 ctsio->kern_data_len = len; 5585 ctsio->kern_total_len = len; 5586 ctsio->kern_data_resid = 0; 5587 ctsio->kern_rel_offset = 0; 5588 ctsio->kern_sg_entries = 0; 5589 ctl_set_success(ctsio); 5590 ctsio->be_move_done = ctl_config_move_done; 5591 ctl_datamove((union ctl_io *)ctsio); 5592 return (CTL_RETVAL_COMPLETE); 5593} 5594 5595int 5596ctl_write_buffer(struct ctl_scsiio *ctsio) 5597{ 5598 struct ctl_lun *lun = CTL_LUN(ctsio); 5599 struct scsi_write_buffer *cdb; 5600 int buffer_offset, len; 5601 5602 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5603 5604 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5605 5606 len = scsi_3btoul(cdb->length); 5607 buffer_offset = scsi_3btoul(cdb->offset); 5608 5609 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5610 ctl_set_invalid_field(ctsio, 5611 /*sks_valid*/ 1, 5612 /*command*/ 1, 5613 /*field*/ 6, 5614 /*bit_valid*/ 0, 5615 /*bit*/ 0); 5616 ctl_done((union ctl_io *)ctsio); 5617 return (CTL_RETVAL_COMPLETE); 5618 } 5619 5620 /* 5621 * If we've got a kernel request that hasn't been malloced yet, 5622 * malloc it and tell the caller the data buffer is here. 5623 */ 5624 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5625 if (lun->write_buffer == NULL) { 5626 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5627 M_CTL, M_WAITOK); 5628 } 5629 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5630 ctsio->kern_data_len = len; 5631 ctsio->kern_total_len = len; 5632 ctsio->kern_data_resid = 0; 5633 ctsio->kern_rel_offset = 0; 5634 ctsio->kern_sg_entries = 0; 5635 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5636 ctsio->be_move_done = ctl_config_move_done; 5637 ctl_datamove((union ctl_io *)ctsio); 5638 5639 return (CTL_RETVAL_COMPLETE); 5640 } 5641 5642 ctl_set_success(ctsio); 5643 ctl_done((union ctl_io *)ctsio); 5644 return (CTL_RETVAL_COMPLETE); 5645} 5646 5647int 5648ctl_write_same(struct ctl_scsiio *ctsio) 5649{ 5650 struct ctl_lun *lun = CTL_LUN(ctsio); 5651 struct ctl_lba_len_flags *lbalen; 5652 uint64_t lba; 5653 uint32_t num_blocks; 5654 int len, retval; 5655 uint8_t byte2; 5656 5657 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5658 5659 switch (ctsio->cdb[0]) { 5660 case WRITE_SAME_10: { 5661 struct scsi_write_same_10 *cdb; 5662 5663 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5664 5665 lba = scsi_4btoul(cdb->addr); 5666 num_blocks = scsi_2btoul(cdb->length); 5667 byte2 = cdb->byte2; 5668 break; 5669 } 5670 case WRITE_SAME_16: { 5671 struct scsi_write_same_16 *cdb; 5672 5673 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5674 5675 lba = scsi_8btou64(cdb->addr); 5676 num_blocks = scsi_4btoul(cdb->length); 5677 byte2 = cdb->byte2; 5678 break; 5679 } 5680 default: 5681 /* 5682 * We got a command we don't support. This shouldn't 5683 * happen, commands should be filtered out above us. 5684 */ 5685 ctl_set_invalid_opcode(ctsio); 5686 ctl_done((union ctl_io *)ctsio); 5687 5688 return (CTL_RETVAL_COMPLETE); 5689 break; /* NOTREACHED */ 5690 } 5691 5692 /* ANCHOR flag can be used only together with UNMAP */ 5693 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5694 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5695 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5696 ctl_done((union ctl_io *)ctsio); 5697 return (CTL_RETVAL_COMPLETE); 5698 } 5699 5700 /* 5701 * The first check is to make sure we're in bounds, the second 5702 * check is to catch wrap-around problems. If the lba + num blocks 5703 * is less than the lba, then we've wrapped around and the block 5704 * range is invalid anyway. 5705 */ 5706 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5707 || ((lba + num_blocks) < lba)) { 5708 ctl_set_lba_out_of_range(ctsio, 5709 MAX(lba, lun->be_lun->maxlba + 1)); 5710 ctl_done((union ctl_io *)ctsio); 5711 return (CTL_RETVAL_COMPLETE); 5712 } 5713 5714 /* Zero number of blocks means "to the last logical block" */ 5715 if (num_blocks == 0) { 5716 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5717 ctl_set_invalid_field(ctsio, 5718 /*sks_valid*/ 0, 5719 /*command*/ 1, 5720 /*field*/ 0, 5721 /*bit_valid*/ 0, 5722 /*bit*/ 0); 5723 ctl_done((union ctl_io *)ctsio); 5724 return (CTL_RETVAL_COMPLETE); 5725 } 5726 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5727 } 5728 5729 len = lun->be_lun->blocksize; 5730 5731 /* 5732 * If we've got a kernel request that hasn't been malloced yet, 5733 * malloc it and tell the caller the data buffer is here. 5734 */ 5735 if ((byte2 & SWS_NDOB) == 0 && 5736 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5737 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5738 ctsio->kern_data_len = len; 5739 ctsio->kern_total_len = len; 5740 ctsio->kern_data_resid = 0; 5741 ctsio->kern_rel_offset = 0; 5742 ctsio->kern_sg_entries = 0; 5743 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5744 ctsio->be_move_done = ctl_config_move_done; 5745 ctl_datamove((union ctl_io *)ctsio); 5746 5747 return (CTL_RETVAL_COMPLETE); 5748 } 5749 5750 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5751 lbalen->lba = lba; 5752 lbalen->len = num_blocks; 5753 lbalen->flags = byte2; 5754 retval = lun->backend->config_write((union ctl_io *)ctsio); 5755 5756 return (retval); 5757} 5758 5759int 5760ctl_unmap(struct ctl_scsiio *ctsio) 5761{ 5762 struct ctl_lun *lun = CTL_LUN(ctsio); 5763 struct scsi_unmap *cdb; 5764 struct ctl_ptr_len_flags *ptrlen; 5765 struct scsi_unmap_header *hdr; 5766 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5767 uint64_t lba; 5768 uint32_t num_blocks; 5769 int len, retval; 5770 uint8_t byte2; 5771 5772 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5773 5774 cdb = (struct scsi_unmap *)ctsio->cdb; 5775 len = scsi_2btoul(cdb->length); 5776 byte2 = cdb->byte2; 5777 5778 /* 5779 * If we've got a kernel request that hasn't been malloced yet, 5780 * malloc it and tell the caller the data buffer is here. 5781 */ 5782 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5783 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5784 ctsio->kern_data_len = len; 5785 ctsio->kern_total_len = len; 5786 ctsio->kern_data_resid = 0; 5787 ctsio->kern_rel_offset = 0; 5788 ctsio->kern_sg_entries = 0; 5789 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5790 ctsio->be_move_done = ctl_config_move_done; 5791 ctl_datamove((union ctl_io *)ctsio); 5792 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 5796 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5797 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5798 if (len < sizeof (*hdr) || 5799 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5800 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5801 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5802 ctl_set_invalid_field(ctsio, 5803 /*sks_valid*/ 0, 5804 /*command*/ 0, 5805 /*field*/ 0, 5806 /*bit_valid*/ 0, 5807 /*bit*/ 0); 5808 goto done; 5809 } 5810 len = scsi_2btoul(hdr->desc_length); 5811 buf = (struct scsi_unmap_desc *)(hdr + 1); 5812 end = buf + len / sizeof(*buf); 5813 5814 endnz = buf; 5815 for (range = buf; range < end; range++) { 5816 lba = scsi_8btou64(range->lba); 5817 num_blocks = scsi_4btoul(range->length); 5818 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5819 || ((lba + num_blocks) < lba)) { 5820 ctl_set_lba_out_of_range(ctsio, 5821 MAX(lba, lun->be_lun->maxlba + 1)); 5822 ctl_done((union ctl_io *)ctsio); 5823 return (CTL_RETVAL_COMPLETE); 5824 } 5825 if (num_blocks != 0) 5826 endnz = range + 1; 5827 } 5828 5829 /* 5830 * Block backend can not handle zero last range. 5831 * Filter it out and return if there is nothing left. 5832 */ 5833 len = (uint8_t *)endnz - (uint8_t *)buf; 5834 if (len == 0) { 5835 ctl_set_success(ctsio); 5836 goto done; 5837 } 5838 5839 mtx_lock(&lun->lun_lock); 5840 ptrlen = (struct ctl_ptr_len_flags *) 5841 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5842 ptrlen->ptr = (void *)buf; 5843 ptrlen->len = len; 5844 ptrlen->flags = byte2; 5845 ctl_check_blocked(lun); 5846 mtx_unlock(&lun->lun_lock); 5847 5848 retval = lun->backend->config_write((union ctl_io *)ctsio); 5849 return (retval); 5850 5851done: 5852 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5853 free(ctsio->kern_data_ptr, M_CTL); 5854 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5855 } 5856 ctl_done((union ctl_io *)ctsio); 5857 return (CTL_RETVAL_COMPLETE); 5858} 5859 5860int 5861ctl_default_page_handler(struct ctl_scsiio *ctsio, 5862 struct ctl_page_index *page_index, uint8_t *page_ptr) 5863{ 5864 struct ctl_lun *lun = CTL_LUN(ctsio); 5865 uint8_t *current_cp; 5866 int set_ua; 5867 uint32_t initidx; 5868 5869 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5870 set_ua = 0; 5871 5872 current_cp = (page_index->page_data + (page_index->page_len * 5873 CTL_PAGE_CURRENT)); 5874 5875 mtx_lock(&lun->lun_lock); 5876 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5877 memcpy(current_cp, page_ptr, page_index->page_len); 5878 set_ua = 1; 5879 } 5880 if (set_ua != 0) 5881 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5882 mtx_unlock(&lun->lun_lock); 5883 if (set_ua) { 5884 ctl_isc_announce_mode(lun, 5885 ctl_get_initindex(&ctsio->io_hdr.nexus), 5886 page_index->page_code, page_index->subpage); 5887 } 5888 return (CTL_RETVAL_COMPLETE); 5889} 5890 5891static void 5892ctl_ie_timer(void *arg) 5893{ 5894 struct ctl_lun *lun = arg; 5895 uint64_t t; 5896 5897 if (lun->ie_asc == 0) 5898 return; 5899 5900 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5901 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5902 else 5903 lun->ie_reported = 0; 5904 5905 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5906 lun->ie_reportcnt++; 5907 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5908 if (t == 0 || t == UINT32_MAX) 5909 t = 3000; /* 5 min */ 5910 callout_schedule(&lun->ie_callout, t * hz / 10); 5911 } 5912} 5913 5914int 5915ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5916 struct ctl_page_index *page_index, uint8_t *page_ptr) 5917{ 5918 struct ctl_lun *lun = CTL_LUN(ctsio); 5919 struct scsi_info_exceptions_page *pg; 5920 uint64_t t; 5921 5922 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5923 5924 pg = (struct scsi_info_exceptions_page *)page_ptr; 5925 mtx_lock(&lun->lun_lock); 5926 if (pg->info_flags & SIEP_FLAGS_TEST) { 5927 lun->ie_asc = 0x5d; 5928 lun->ie_ascq = 0xff; 5929 if (pg->mrie == SIEP_MRIE_UA) { 5930 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5931 lun->ie_reported = 1; 5932 } else { 5933 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5934 lun->ie_reported = -1; 5935 } 5936 lun->ie_reportcnt = 1; 5937 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5938 lun->ie_reportcnt++; 5939 t = scsi_4btoul(pg->interval_timer); 5940 if (t == 0 || t == UINT32_MAX) 5941 t = 3000; /* 5 min */ 5942 callout_reset(&lun->ie_callout, t * hz / 10, 5943 ctl_ie_timer, lun); 5944 } 5945 } else { 5946 lun->ie_asc = 0; 5947 lun->ie_ascq = 0; 5948 lun->ie_reported = 1; 5949 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5950 lun->ie_reportcnt = UINT32_MAX; 5951 callout_stop(&lun->ie_callout); 5952 } 5953 mtx_unlock(&lun->lun_lock); 5954 return (CTL_RETVAL_COMPLETE); 5955} 5956 5957static int 5958ctl_do_mode_select(union ctl_io *io) 5959{ 5960 struct ctl_lun *lun = CTL_LUN(io); 5961 struct scsi_mode_page_header *page_header; 5962 struct ctl_page_index *page_index; 5963 struct ctl_scsiio *ctsio; 5964 int page_len, page_len_offset, page_len_size; 5965 union ctl_modepage_info *modepage_info; 5966 uint16_t *len_left, *len_used; 5967 int retval, i; 5968 5969 ctsio = &io->scsiio; 5970 page_index = NULL; 5971 page_len = 0; 5972 5973 modepage_info = (union ctl_modepage_info *) 5974 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5975 len_left = &modepage_info->header.len_left; 5976 len_used = &modepage_info->header.len_used; 5977 5978do_next_page: 5979 5980 page_header = (struct scsi_mode_page_header *) 5981 (ctsio->kern_data_ptr + *len_used); 5982 5983 if (*len_left == 0) { 5984 free(ctsio->kern_data_ptr, M_CTL); 5985 ctl_set_success(ctsio); 5986 ctl_done((union ctl_io *)ctsio); 5987 return (CTL_RETVAL_COMPLETE); 5988 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5989 5990 free(ctsio->kern_data_ptr, M_CTL); 5991 ctl_set_param_len_error(ctsio); 5992 ctl_done((union ctl_io *)ctsio); 5993 return (CTL_RETVAL_COMPLETE); 5994 5995 } else if ((page_header->page_code & SMPH_SPF) 5996 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5997 5998 free(ctsio->kern_data_ptr, M_CTL); 5999 ctl_set_param_len_error(ctsio); 6000 ctl_done((union ctl_io *)ctsio); 6001 return (CTL_RETVAL_COMPLETE); 6002 } 6003 6004 6005 /* 6006 * XXX KDM should we do something with the block descriptor? 6007 */ 6008 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6009 page_index = &lun->mode_pages.index[i]; 6010 if (lun->be_lun->lun_type == T_DIRECT && 6011 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6012 continue; 6013 if (lun->be_lun->lun_type == T_PROCESSOR && 6014 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6015 continue; 6016 if (lun->be_lun->lun_type == T_CDROM && 6017 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6018 continue; 6019 6020 if ((page_index->page_code & SMPH_PC_MASK) != 6021 (page_header->page_code & SMPH_PC_MASK)) 6022 continue; 6023 6024 /* 6025 * If neither page has a subpage code, then we've got a 6026 * match. 6027 */ 6028 if (((page_index->page_code & SMPH_SPF) == 0) 6029 && ((page_header->page_code & SMPH_SPF) == 0)) { 6030 page_len = page_header->page_length; 6031 break; 6032 } 6033 6034 /* 6035 * If both pages have subpages, then the subpage numbers 6036 * have to match. 6037 */ 6038 if ((page_index->page_code & SMPH_SPF) 6039 && (page_header->page_code & SMPH_SPF)) { 6040 struct scsi_mode_page_header_sp *sph; 6041 6042 sph = (struct scsi_mode_page_header_sp *)page_header; 6043 if (page_index->subpage == sph->subpage) { 6044 page_len = scsi_2btoul(sph->page_length); 6045 break; 6046 } 6047 } 6048 } 6049 6050 /* 6051 * If we couldn't find the page, or if we don't have a mode select 6052 * handler for it, send back an error to the user. 6053 */ 6054 if ((i >= CTL_NUM_MODE_PAGES) 6055 || (page_index->select_handler == NULL)) { 6056 ctl_set_invalid_field(ctsio, 6057 /*sks_valid*/ 1, 6058 /*command*/ 0, 6059 /*field*/ *len_used, 6060 /*bit_valid*/ 0, 6061 /*bit*/ 0); 6062 free(ctsio->kern_data_ptr, M_CTL); 6063 ctl_done((union ctl_io *)ctsio); 6064 return (CTL_RETVAL_COMPLETE); 6065 } 6066 6067 if (page_index->page_code & SMPH_SPF) { 6068 page_len_offset = 2; 6069 page_len_size = 2; 6070 } else { 6071 page_len_size = 1; 6072 page_len_offset = 1; 6073 } 6074 6075 /* 6076 * If the length the initiator gives us isn't the one we specify in 6077 * the mode page header, or if they didn't specify enough data in 6078 * the CDB to avoid truncating this page, kick out the request. 6079 */ 6080 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6081 ctl_set_invalid_field(ctsio, 6082 /*sks_valid*/ 1, 6083 /*command*/ 0, 6084 /*field*/ *len_used + page_len_offset, 6085 /*bit_valid*/ 0, 6086 /*bit*/ 0); 6087 free(ctsio->kern_data_ptr, M_CTL); 6088 ctl_done((union ctl_io *)ctsio); 6089 return (CTL_RETVAL_COMPLETE); 6090 } 6091 if (*len_left < page_index->page_len) { 6092 free(ctsio->kern_data_ptr, M_CTL); 6093 ctl_set_param_len_error(ctsio); 6094 ctl_done((union ctl_io *)ctsio); 6095 return (CTL_RETVAL_COMPLETE); 6096 } 6097 6098 /* 6099 * Run through the mode page, checking to make sure that the bits 6100 * the user changed are actually legal for him to change. 6101 */ 6102 for (i = 0; i < page_index->page_len; i++) { 6103 uint8_t *user_byte, *change_mask, *current_byte; 6104 int bad_bit; 6105 int j; 6106 6107 user_byte = (uint8_t *)page_header + i; 6108 change_mask = page_index->page_data + 6109 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6110 current_byte = page_index->page_data + 6111 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6112 6113 /* 6114 * Check to see whether the user set any bits in this byte 6115 * that he is not allowed to set. 6116 */ 6117 if ((*user_byte & ~(*change_mask)) == 6118 (*current_byte & ~(*change_mask))) 6119 continue; 6120 6121 /* 6122 * Go through bit by bit to determine which one is illegal. 6123 */ 6124 bad_bit = 0; 6125 for (j = 7; j >= 0; j--) { 6126 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6127 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6128 bad_bit = i; 6129 break; 6130 } 6131 } 6132 ctl_set_invalid_field(ctsio, 6133 /*sks_valid*/ 1, 6134 /*command*/ 0, 6135 /*field*/ *len_used + i, 6136 /*bit_valid*/ 1, 6137 /*bit*/ bad_bit); 6138 free(ctsio->kern_data_ptr, M_CTL); 6139 ctl_done((union ctl_io *)ctsio); 6140 return (CTL_RETVAL_COMPLETE); 6141 } 6142 6143 /* 6144 * Decrement these before we call the page handler, since we may 6145 * end up getting called back one way or another before the handler 6146 * returns to this context. 6147 */ 6148 *len_left -= page_index->page_len; 6149 *len_used += page_index->page_len; 6150 6151 retval = page_index->select_handler(ctsio, page_index, 6152 (uint8_t *)page_header); 6153 6154 /* 6155 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6156 * wait until this queued command completes to finish processing 6157 * the mode page. If it returns anything other than 6158 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6159 * already set the sense information, freed the data pointer, and 6160 * completed the io for us. 6161 */ 6162 if (retval != CTL_RETVAL_COMPLETE) 6163 goto bailout_no_done; 6164 6165 /* 6166 * If the initiator sent us more than one page, parse the next one. 6167 */ 6168 if (*len_left > 0) 6169 goto do_next_page; 6170 6171 ctl_set_success(ctsio); 6172 free(ctsio->kern_data_ptr, M_CTL); 6173 ctl_done((union ctl_io *)ctsio); 6174 6175bailout_no_done: 6176 6177 return (CTL_RETVAL_COMPLETE); 6178 6179} 6180 6181int 6182ctl_mode_select(struct ctl_scsiio *ctsio) 6183{ 6184 struct ctl_lun *lun = CTL_LUN(ctsio); 6185 union ctl_modepage_info *modepage_info; 6186 int bd_len, i, header_size, param_len, pf, rtd, sp; 6187 uint32_t initidx; 6188 6189 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6190 switch (ctsio->cdb[0]) { 6191 case MODE_SELECT_6: { 6192 struct scsi_mode_select_6 *cdb; 6193 6194 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6195 6196 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6197 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6198 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6199 param_len = cdb->length; 6200 header_size = sizeof(struct scsi_mode_header_6); 6201 break; 6202 } 6203 case MODE_SELECT_10: { 6204 struct scsi_mode_select_10 *cdb; 6205 6206 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6207 6208 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6209 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6210 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6211 param_len = scsi_2btoul(cdb->length); 6212 header_size = sizeof(struct scsi_mode_header_10); 6213 break; 6214 } 6215 default: 6216 ctl_set_invalid_opcode(ctsio); 6217 ctl_done((union ctl_io *)ctsio); 6218 return (CTL_RETVAL_COMPLETE); 6219 } 6220 6221 if (rtd) { 6222 if (param_len != 0) { 6223 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6224 /*command*/ 1, /*field*/ 0, 6225 /*bit_valid*/ 0, /*bit*/ 0); 6226 ctl_done((union ctl_io *)ctsio); 6227 return (CTL_RETVAL_COMPLETE); 6228 } 6229 6230 /* Revert to defaults. */ 6231 ctl_init_page_index(lun); 6232 mtx_lock(&lun->lun_lock); 6233 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6234 mtx_unlock(&lun->lun_lock); 6235 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6236 ctl_isc_announce_mode(lun, -1, 6237 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6238 lun->mode_pages.index[i].subpage); 6239 } 6240 ctl_set_success(ctsio); 6241 ctl_done((union ctl_io *)ctsio); 6242 return (CTL_RETVAL_COMPLETE); 6243 } 6244 6245 /* 6246 * From SPC-3: 6247 * "A parameter list length of zero indicates that the Data-Out Buffer 6248 * shall be empty. This condition shall not be considered as an error." 6249 */ 6250 if (param_len == 0) { 6251 ctl_set_success(ctsio); 6252 ctl_done((union ctl_io *)ctsio); 6253 return (CTL_RETVAL_COMPLETE); 6254 } 6255 6256 /* 6257 * Since we'll hit this the first time through, prior to 6258 * allocation, we don't need to free a data buffer here. 6259 */ 6260 if (param_len < header_size) { 6261 ctl_set_param_len_error(ctsio); 6262 ctl_done((union ctl_io *)ctsio); 6263 return (CTL_RETVAL_COMPLETE); 6264 } 6265 6266 /* 6267 * Allocate the data buffer and grab the user's data. In theory, 6268 * we shouldn't have to sanity check the parameter list length here 6269 * because the maximum size is 64K. We should be able to malloc 6270 * that much without too many problems. 6271 */ 6272 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6273 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6274 ctsio->kern_data_len = param_len; 6275 ctsio->kern_total_len = param_len; 6276 ctsio->kern_data_resid = 0; 6277 ctsio->kern_rel_offset = 0; 6278 ctsio->kern_sg_entries = 0; 6279 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6280 ctsio->be_move_done = ctl_config_move_done; 6281 ctl_datamove((union ctl_io *)ctsio); 6282 6283 return (CTL_RETVAL_COMPLETE); 6284 } 6285 6286 switch (ctsio->cdb[0]) { 6287 case MODE_SELECT_6: { 6288 struct scsi_mode_header_6 *mh6; 6289 6290 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6291 bd_len = mh6->blk_desc_len; 6292 break; 6293 } 6294 case MODE_SELECT_10: { 6295 struct scsi_mode_header_10 *mh10; 6296 6297 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6298 bd_len = scsi_2btoul(mh10->blk_desc_len); 6299 break; 6300 } 6301 default: 6302 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6303 } 6304 6305 if (param_len < (header_size + bd_len)) { 6306 free(ctsio->kern_data_ptr, M_CTL); 6307 ctl_set_param_len_error(ctsio); 6308 ctl_done((union ctl_io *)ctsio); 6309 return (CTL_RETVAL_COMPLETE); 6310 } 6311 6312 /* 6313 * Set the IO_CONT flag, so that if this I/O gets passed to 6314 * ctl_config_write_done(), it'll get passed back to 6315 * ctl_do_mode_select() for further processing, or completion if 6316 * we're all done. 6317 */ 6318 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6319 ctsio->io_cont = ctl_do_mode_select; 6320 6321 modepage_info = (union ctl_modepage_info *) 6322 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6323 memset(modepage_info, 0, sizeof(*modepage_info)); 6324 modepage_info->header.len_left = param_len - header_size - bd_len; 6325 modepage_info->header.len_used = header_size + bd_len; 6326 6327 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6328} 6329 6330int 6331ctl_mode_sense(struct ctl_scsiio *ctsio) 6332{ 6333 struct ctl_lun *lun = CTL_LUN(ctsio); 6334 int pc, page_code, dbd, llba, subpage; 6335 int alloc_len, page_len, header_len, total_len; 6336 struct scsi_mode_block_descr *block_desc; 6337 struct ctl_page_index *page_index; 6338 6339 dbd = 0; 6340 llba = 0; 6341 block_desc = NULL; 6342 6343 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6344 6345 switch (ctsio->cdb[0]) { 6346 case MODE_SENSE_6: { 6347 struct scsi_mode_sense_6 *cdb; 6348 6349 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6350 6351 header_len = sizeof(struct scsi_mode_hdr_6); 6352 if (cdb->byte2 & SMS_DBD) 6353 dbd = 1; 6354 else 6355 header_len += sizeof(struct scsi_mode_block_descr); 6356 6357 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6358 page_code = cdb->page & SMS_PAGE_CODE; 6359 subpage = cdb->subpage; 6360 alloc_len = cdb->length; 6361 break; 6362 } 6363 case MODE_SENSE_10: { 6364 struct scsi_mode_sense_10 *cdb; 6365 6366 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6367 6368 header_len = sizeof(struct scsi_mode_hdr_10); 6369 6370 if (cdb->byte2 & SMS_DBD) 6371 dbd = 1; 6372 else 6373 header_len += sizeof(struct scsi_mode_block_descr); 6374 if (cdb->byte2 & SMS10_LLBAA) 6375 llba = 1; 6376 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6377 page_code = cdb->page & SMS_PAGE_CODE; 6378 subpage = cdb->subpage; 6379 alloc_len = scsi_2btoul(cdb->length); 6380 break; 6381 } 6382 default: 6383 ctl_set_invalid_opcode(ctsio); 6384 ctl_done((union ctl_io *)ctsio); 6385 return (CTL_RETVAL_COMPLETE); 6386 break; /* NOTREACHED */ 6387 } 6388 6389 /* 6390 * We have to make a first pass through to calculate the size of 6391 * the pages that match the user's query. Then we allocate enough 6392 * memory to hold it, and actually copy the data into the buffer. 6393 */ 6394 switch (page_code) { 6395 case SMS_ALL_PAGES_PAGE: { 6396 u_int i; 6397 6398 page_len = 0; 6399 6400 /* 6401 * At the moment, values other than 0 and 0xff here are 6402 * reserved according to SPC-3. 6403 */ 6404 if ((subpage != SMS_SUBPAGE_PAGE_0) 6405 && (subpage != SMS_SUBPAGE_ALL)) { 6406 ctl_set_invalid_field(ctsio, 6407 /*sks_valid*/ 1, 6408 /*command*/ 1, 6409 /*field*/ 3, 6410 /*bit_valid*/ 0, 6411 /*bit*/ 0); 6412 ctl_done((union ctl_io *)ctsio); 6413 return (CTL_RETVAL_COMPLETE); 6414 } 6415 6416 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6417 page_index = &lun->mode_pages.index[i]; 6418 6419 /* Make sure the page is supported for this dev type */ 6420 if (lun->be_lun->lun_type == T_DIRECT && 6421 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6422 continue; 6423 if (lun->be_lun->lun_type == T_PROCESSOR && 6424 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6425 continue; 6426 if (lun->be_lun->lun_type == T_CDROM && 6427 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6428 continue; 6429 6430 /* 6431 * We don't use this subpage if the user didn't 6432 * request all subpages. 6433 */ 6434 if ((page_index->subpage != 0) 6435 && (subpage == SMS_SUBPAGE_PAGE_0)) 6436 continue; 6437 6438#if 0 6439 printf("found page %#x len %d\n", 6440 page_index->page_code & SMPH_PC_MASK, 6441 page_index->page_len); 6442#endif 6443 page_len += page_index->page_len; 6444 } 6445 break; 6446 } 6447 default: { 6448 u_int i; 6449 6450 page_len = 0; 6451 6452 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6453 page_index = &lun->mode_pages.index[i]; 6454 6455 /* Make sure the page is supported for this dev type */ 6456 if (lun->be_lun->lun_type == T_DIRECT && 6457 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6458 continue; 6459 if (lun->be_lun->lun_type == T_PROCESSOR && 6460 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6461 continue; 6462 if (lun->be_lun->lun_type == T_CDROM && 6463 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6464 continue; 6465 6466 /* Look for the right page code */ 6467 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6468 continue; 6469 6470 /* Look for the right subpage or the subpage wildcard*/ 6471 if ((page_index->subpage != subpage) 6472 && (subpage != SMS_SUBPAGE_ALL)) 6473 continue; 6474 6475#if 0 6476 printf("found page %#x len %d\n", 6477 page_index->page_code & SMPH_PC_MASK, 6478 page_index->page_len); 6479#endif 6480 6481 page_len += page_index->page_len; 6482 } 6483 6484 if (page_len == 0) { 6485 ctl_set_invalid_field(ctsio, 6486 /*sks_valid*/ 1, 6487 /*command*/ 1, 6488 /*field*/ 2, 6489 /*bit_valid*/ 1, 6490 /*bit*/ 5); 6491 ctl_done((union ctl_io *)ctsio); 6492 return (CTL_RETVAL_COMPLETE); 6493 } 6494 break; 6495 } 6496 } 6497 6498 total_len = header_len + page_len; 6499#if 0 6500 printf("header_len = %d, page_len = %d, total_len = %d\n", 6501 header_len, page_len, total_len); 6502#endif 6503 6504 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6505 ctsio->kern_sg_entries = 0; 6506 ctsio->kern_data_resid = 0; 6507 ctsio->kern_rel_offset = 0; 6508 if (total_len < alloc_len) { 6509 ctsio->residual = alloc_len - total_len; 6510 ctsio->kern_data_len = total_len; 6511 ctsio->kern_total_len = total_len; 6512 } else { 6513 ctsio->residual = 0; 6514 ctsio->kern_data_len = alloc_len; 6515 ctsio->kern_total_len = alloc_len; 6516 } 6517 6518 switch (ctsio->cdb[0]) { 6519 case MODE_SENSE_6: { 6520 struct scsi_mode_hdr_6 *header; 6521 6522 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6523 6524 header->datalen = MIN(total_len - 1, 254); 6525 if (lun->be_lun->lun_type == T_DIRECT) { 6526 header->dev_specific = 0x10; /* DPOFUA */ 6527 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6528 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6529 header->dev_specific |= 0x80; /* WP */ 6530 } 6531 if (dbd) 6532 header->block_descr_len = 0; 6533 else 6534 header->block_descr_len = 6535 sizeof(struct scsi_mode_block_descr); 6536 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6537 break; 6538 } 6539 case MODE_SENSE_10: { 6540 struct scsi_mode_hdr_10 *header; 6541 int datalen; 6542 6543 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6544 6545 datalen = MIN(total_len - 2, 65533); 6546 scsi_ulto2b(datalen, header->datalen); 6547 if (lun->be_lun->lun_type == T_DIRECT) { 6548 header->dev_specific = 0x10; /* DPOFUA */ 6549 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6550 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6551 header->dev_specific |= 0x80; /* WP */ 6552 } 6553 if (dbd) 6554 scsi_ulto2b(0, header->block_descr_len); 6555 else 6556 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6557 header->block_descr_len); 6558 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6559 break; 6560 } 6561 default: 6562 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6563 } 6564 6565 /* 6566 * If we've got a disk, use its blocksize in the block 6567 * descriptor. Otherwise, just set it to 0. 6568 */ 6569 if (dbd == 0) { 6570 if (lun->be_lun->lun_type == T_DIRECT) 6571 scsi_ulto3b(lun->be_lun->blocksize, 6572 block_desc->block_len); 6573 else 6574 scsi_ulto3b(0, block_desc->block_len); 6575 } 6576 6577 switch (page_code) { 6578 case SMS_ALL_PAGES_PAGE: { 6579 int i, data_used; 6580 6581 data_used = header_len; 6582 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6583 struct ctl_page_index *page_index; 6584 6585 page_index = &lun->mode_pages.index[i]; 6586 if (lun->be_lun->lun_type == T_DIRECT && 6587 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6588 continue; 6589 if (lun->be_lun->lun_type == T_PROCESSOR && 6590 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6591 continue; 6592 if (lun->be_lun->lun_type == T_CDROM && 6593 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6594 continue; 6595 6596 /* 6597 * We don't use this subpage if the user didn't 6598 * request all subpages. We already checked (above) 6599 * to make sure the user only specified a subpage 6600 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6601 */ 6602 if ((page_index->subpage != 0) 6603 && (subpage == SMS_SUBPAGE_PAGE_0)) 6604 continue; 6605 6606 /* 6607 * Call the handler, if it exists, to update the 6608 * page to the latest values. 6609 */ 6610 if (page_index->sense_handler != NULL) 6611 page_index->sense_handler(ctsio, page_index,pc); 6612 6613 memcpy(ctsio->kern_data_ptr + data_used, 6614 page_index->page_data + 6615 (page_index->page_len * pc), 6616 page_index->page_len); 6617 data_used += page_index->page_len; 6618 } 6619 break; 6620 } 6621 default: { 6622 int i, data_used; 6623 6624 data_used = header_len; 6625 6626 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6627 struct ctl_page_index *page_index; 6628 6629 page_index = &lun->mode_pages.index[i]; 6630 6631 /* Look for the right page code */ 6632 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6633 continue; 6634 6635 /* Look for the right subpage or the subpage wildcard*/ 6636 if ((page_index->subpage != subpage) 6637 && (subpage != SMS_SUBPAGE_ALL)) 6638 continue; 6639 6640 /* Make sure the page is supported for this dev type */ 6641 if (lun->be_lun->lun_type == T_DIRECT && 6642 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6643 continue; 6644 if (lun->be_lun->lun_type == T_PROCESSOR && 6645 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6646 continue; 6647 if (lun->be_lun->lun_type == T_CDROM && 6648 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6649 continue; 6650 6651 /* 6652 * Call the handler, if it exists, to update the 6653 * page to the latest values. 6654 */ 6655 if (page_index->sense_handler != NULL) 6656 page_index->sense_handler(ctsio, page_index,pc); 6657 6658 memcpy(ctsio->kern_data_ptr + data_used, 6659 page_index->page_data + 6660 (page_index->page_len * pc), 6661 page_index->page_len); 6662 data_used += page_index->page_len; 6663 } 6664 break; 6665 } 6666 } 6667 6668 ctl_set_success(ctsio); 6669 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6670 ctsio->be_move_done = ctl_config_move_done; 6671 ctl_datamove((union ctl_io *)ctsio); 6672 return (CTL_RETVAL_COMPLETE); 6673} 6674 6675int 6676ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6677 struct ctl_page_index *page_index, 6678 int pc) 6679{ 6680 struct ctl_lun *lun = CTL_LUN(ctsio); 6681 struct scsi_log_param_header *phdr; 6682 uint8_t *data; 6683 uint64_t val; 6684 6685 data = page_index->page_data; 6686 6687 if (lun->backend->lun_attr != NULL && 6688 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6689 != UINT64_MAX) { 6690 phdr = (struct scsi_log_param_header *)data; 6691 scsi_ulto2b(0x0001, phdr->param_code); 6692 phdr->param_control = SLP_LBIN | SLP_LP; 6693 phdr->param_len = 8; 6694 data = (uint8_t *)(phdr + 1); 6695 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6696 data[4] = 0x02; /* per-pool */ 6697 data += phdr->param_len; 6698 } 6699 6700 if (lun->backend->lun_attr != NULL && 6701 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6702 != UINT64_MAX) { 6703 phdr = (struct scsi_log_param_header *)data; 6704 scsi_ulto2b(0x0002, phdr->param_code); 6705 phdr->param_control = SLP_LBIN | SLP_LP; 6706 phdr->param_len = 8; 6707 data = (uint8_t *)(phdr + 1); 6708 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6709 data[4] = 0x01; /* per-LUN */ 6710 data += phdr->param_len; 6711 } 6712 6713 if (lun->backend->lun_attr != NULL && 6714 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6715 != UINT64_MAX) { 6716 phdr = (struct scsi_log_param_header *)data; 6717 scsi_ulto2b(0x00f1, phdr->param_code); 6718 phdr->param_control = SLP_LBIN | SLP_LP; 6719 phdr->param_len = 8; 6720 data = (uint8_t *)(phdr + 1); 6721 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6722 data[4] = 0x02; /* per-pool */ 6723 data += phdr->param_len; 6724 } 6725 6726 if (lun->backend->lun_attr != NULL && 6727 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6728 != UINT64_MAX) { 6729 phdr = (struct scsi_log_param_header *)data; 6730 scsi_ulto2b(0x00f2, phdr->param_code); 6731 phdr->param_control = SLP_LBIN | SLP_LP; 6732 phdr->param_len = 8; 6733 data = (uint8_t *)(phdr + 1); 6734 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6735 data[4] = 0x02; /* per-pool */ 6736 data += phdr->param_len; 6737 } 6738 6739 page_index->page_len = data - page_index->page_data; 6740 return (0); 6741} 6742 6743int 6744ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6745 struct ctl_page_index *page_index, 6746 int pc) 6747{ 6748 struct ctl_lun *lun = CTL_LUN(ctsio); 6749 struct stat_page *data; 6750 struct bintime *t; 6751 6752 data = (struct stat_page *)page_index->page_data; 6753 6754 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6755 data->sap.hdr.param_control = SLP_LBIN; 6756 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6757 sizeof(struct scsi_log_param_header); 6758 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ], 6759 data->sap.read_num); 6760 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE], 6761 data->sap.write_num); 6762 if (lun->be_lun->blocksize > 0) { 6763 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] / 6764 lun->be_lun->blocksize, data->sap.recvieved_lba); 6765 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] / 6766 lun->be_lun->blocksize, data->sap.transmitted_lba); 6767 } 6768 t = &lun->stats.time[CTL_STATS_READ]; 6769 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6770 data->sap.read_int); 6771 t = &lun->stats.time[CTL_STATS_WRITE]; 6772 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000), 6773 data->sap.write_int); 6774 scsi_u64to8b(0, data->sap.weighted_num); 6775 scsi_u64to8b(0, data->sap.weighted_int); 6776 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6777 data->it.hdr.param_control = SLP_LBIN; 6778 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6779 sizeof(struct scsi_log_param_header); 6780#ifdef CTL_TIME_IO 6781 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6782#endif 6783 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6784 data->it.hdr.param_control = SLP_LBIN; 6785 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6786 sizeof(struct scsi_log_param_header); 6787 scsi_ulto4b(3, data->ti.exponent); 6788 scsi_ulto4b(1, data->ti.integer); 6789 return (0); 6790} 6791 6792int 6793ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6794 struct ctl_page_index *page_index, 6795 int pc) 6796{ 6797 struct ctl_lun *lun = CTL_LUN(ctsio); 6798 struct scsi_log_informational_exceptions *data; 6799 6800 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6801 6802 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6803 data->hdr.param_control = SLP_LBIN; 6804 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6805 sizeof(struct scsi_log_param_header); 6806 data->ie_asc = lun->ie_asc; 6807 data->ie_ascq = lun->ie_ascq; 6808 data->temperature = 0xff; 6809 return (0); 6810} 6811 6812int 6813ctl_log_sense(struct ctl_scsiio *ctsio) 6814{ 6815 struct ctl_lun *lun = CTL_LUN(ctsio); 6816 int i, pc, page_code, subpage; 6817 int alloc_len, total_len; 6818 struct ctl_page_index *page_index; 6819 struct scsi_log_sense *cdb; 6820 struct scsi_log_header *header; 6821 6822 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6823 6824 cdb = (struct scsi_log_sense *)ctsio->cdb; 6825 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6826 page_code = cdb->page & SLS_PAGE_CODE; 6827 subpage = cdb->subpage; 6828 alloc_len = scsi_2btoul(cdb->length); 6829 6830 page_index = NULL; 6831 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6832 page_index = &lun->log_pages.index[i]; 6833 6834 /* Look for the right page code */ 6835 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6836 continue; 6837 6838 /* Look for the right subpage or the subpage wildcard*/ 6839 if (page_index->subpage != subpage) 6840 continue; 6841 6842 break; 6843 } 6844 if (i >= CTL_NUM_LOG_PAGES) { 6845 ctl_set_invalid_field(ctsio, 6846 /*sks_valid*/ 1, 6847 /*command*/ 1, 6848 /*field*/ 2, 6849 /*bit_valid*/ 0, 6850 /*bit*/ 0); 6851 ctl_done((union ctl_io *)ctsio); 6852 return (CTL_RETVAL_COMPLETE); 6853 } 6854 6855 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6856 6857 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6858 ctsio->kern_sg_entries = 0; 6859 ctsio->kern_data_resid = 0; 6860 ctsio->kern_rel_offset = 0; 6861 if (total_len < alloc_len) { 6862 ctsio->residual = alloc_len - total_len; 6863 ctsio->kern_data_len = total_len; 6864 ctsio->kern_total_len = total_len; 6865 } else { 6866 ctsio->residual = 0; 6867 ctsio->kern_data_len = alloc_len; 6868 ctsio->kern_total_len = alloc_len; 6869 } 6870 6871 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6872 header->page = page_index->page_code; 6873 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6874 header->page |= SL_DS; 6875 if (page_index->subpage) { 6876 header->page |= SL_SPF; 6877 header->subpage = page_index->subpage; 6878 } 6879 scsi_ulto2b(page_index->page_len, header->datalen); 6880 6881 /* 6882 * Call the handler, if it exists, to update the 6883 * page to the latest values. 6884 */ 6885 if (page_index->sense_handler != NULL) 6886 page_index->sense_handler(ctsio, page_index, pc); 6887 6888 memcpy(header + 1, page_index->page_data, page_index->page_len); 6889 6890 ctl_set_success(ctsio); 6891 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6892 ctsio->be_move_done = ctl_config_move_done; 6893 ctl_datamove((union ctl_io *)ctsio); 6894 return (CTL_RETVAL_COMPLETE); 6895} 6896 6897int 6898ctl_read_capacity(struct ctl_scsiio *ctsio) 6899{ 6900 struct ctl_lun *lun = CTL_LUN(ctsio); 6901 struct scsi_read_capacity *cdb; 6902 struct scsi_read_capacity_data *data; 6903 uint32_t lba; 6904 6905 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6906 6907 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6908 6909 lba = scsi_4btoul(cdb->addr); 6910 if (((cdb->pmi & SRC_PMI) == 0) 6911 && (lba != 0)) { 6912 ctl_set_invalid_field(/*ctsio*/ ctsio, 6913 /*sks_valid*/ 1, 6914 /*command*/ 1, 6915 /*field*/ 2, 6916 /*bit_valid*/ 0, 6917 /*bit*/ 0); 6918 ctl_done((union ctl_io *)ctsio); 6919 return (CTL_RETVAL_COMPLETE); 6920 } 6921 6922 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6923 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6924 ctsio->residual = 0; 6925 ctsio->kern_data_len = sizeof(*data); 6926 ctsio->kern_total_len = sizeof(*data); 6927 ctsio->kern_data_resid = 0; 6928 ctsio->kern_rel_offset = 0; 6929 ctsio->kern_sg_entries = 0; 6930 6931 /* 6932 * If the maximum LBA is greater than 0xfffffffe, the user must 6933 * issue a SERVICE ACTION IN (16) command, with the read capacity 6934 * serivce action set. 6935 */ 6936 if (lun->be_lun->maxlba > 0xfffffffe) 6937 scsi_ulto4b(0xffffffff, data->addr); 6938 else 6939 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6940 6941 /* 6942 * XXX KDM this may not be 512 bytes... 6943 */ 6944 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6945 6946 ctl_set_success(ctsio); 6947 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6948 ctsio->be_move_done = ctl_config_move_done; 6949 ctl_datamove((union ctl_io *)ctsio); 6950 return (CTL_RETVAL_COMPLETE); 6951} 6952 6953int 6954ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6955{ 6956 struct ctl_lun *lun = CTL_LUN(ctsio); 6957 struct scsi_read_capacity_16 *cdb; 6958 struct scsi_read_capacity_data_long *data; 6959 uint64_t lba; 6960 uint32_t alloc_len; 6961 6962 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6963 6964 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6965 6966 alloc_len = scsi_4btoul(cdb->alloc_len); 6967 lba = scsi_8btou64(cdb->addr); 6968 6969 if ((cdb->reladr & SRC16_PMI) 6970 && (lba != 0)) { 6971 ctl_set_invalid_field(/*ctsio*/ ctsio, 6972 /*sks_valid*/ 1, 6973 /*command*/ 1, 6974 /*field*/ 2, 6975 /*bit_valid*/ 0, 6976 /*bit*/ 0); 6977 ctl_done((union ctl_io *)ctsio); 6978 return (CTL_RETVAL_COMPLETE); 6979 } 6980 6981 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6982 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6983 6984 if (sizeof(*data) < alloc_len) { 6985 ctsio->residual = alloc_len - sizeof(*data); 6986 ctsio->kern_data_len = sizeof(*data); 6987 ctsio->kern_total_len = sizeof(*data); 6988 } else { 6989 ctsio->residual = 0; 6990 ctsio->kern_data_len = alloc_len; 6991 ctsio->kern_total_len = alloc_len; 6992 } 6993 ctsio->kern_data_resid = 0; 6994 ctsio->kern_rel_offset = 0; 6995 ctsio->kern_sg_entries = 0; 6996 6997 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6998 /* XXX KDM this may not be 512 bytes... */ 6999 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7000 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7001 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7002 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7003 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7004 7005 ctl_set_success(ctsio); 7006 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7007 ctsio->be_move_done = ctl_config_move_done; 7008 ctl_datamove((union ctl_io *)ctsio); 7009 return (CTL_RETVAL_COMPLETE); 7010} 7011 7012int 7013ctl_get_lba_status(struct ctl_scsiio *ctsio) 7014{ 7015 struct ctl_lun *lun = CTL_LUN(ctsio); 7016 struct scsi_get_lba_status *cdb; 7017 struct scsi_get_lba_status_data *data; 7018 struct ctl_lba_len_flags *lbalen; 7019 uint64_t lba; 7020 uint32_t alloc_len, total_len; 7021 int retval; 7022 7023 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7024 7025 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7026 lba = scsi_8btou64(cdb->addr); 7027 alloc_len = scsi_4btoul(cdb->alloc_len); 7028 7029 if (lba > lun->be_lun->maxlba) { 7030 ctl_set_lba_out_of_range(ctsio, lba); 7031 ctl_done((union ctl_io *)ctsio); 7032 return (CTL_RETVAL_COMPLETE); 7033 } 7034 7035 total_len = sizeof(*data) + sizeof(data->descr[0]); 7036 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7037 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7038 7039 if (total_len < alloc_len) { 7040 ctsio->residual = alloc_len - total_len; 7041 ctsio->kern_data_len = total_len; 7042 ctsio->kern_total_len = total_len; 7043 } else { 7044 ctsio->residual = 0; 7045 ctsio->kern_data_len = alloc_len; 7046 ctsio->kern_total_len = alloc_len; 7047 } 7048 ctsio->kern_data_resid = 0; 7049 ctsio->kern_rel_offset = 0; 7050 ctsio->kern_sg_entries = 0; 7051 7052 /* Fill dummy data in case backend can't tell anything. */ 7053 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7054 scsi_u64to8b(lba, data->descr[0].addr); 7055 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7056 data->descr[0].length); 7057 data->descr[0].status = 0; /* Mapped or unknown. */ 7058 7059 ctl_set_success(ctsio); 7060 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7061 ctsio->be_move_done = ctl_config_move_done; 7062 7063 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7064 lbalen->lba = lba; 7065 lbalen->len = total_len; 7066 lbalen->flags = 0; 7067 retval = lun->backend->config_read((union ctl_io *)ctsio); 7068 return (CTL_RETVAL_COMPLETE); 7069} 7070 7071int 7072ctl_read_defect(struct ctl_scsiio *ctsio) 7073{ 7074 struct scsi_read_defect_data_10 *ccb10; 7075 struct scsi_read_defect_data_12 *ccb12; 7076 struct scsi_read_defect_data_hdr_10 *data10; 7077 struct scsi_read_defect_data_hdr_12 *data12; 7078 uint32_t alloc_len, data_len; 7079 uint8_t format; 7080 7081 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7082 7083 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7084 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7085 format = ccb10->format; 7086 alloc_len = scsi_2btoul(ccb10->alloc_length); 7087 data_len = sizeof(*data10); 7088 } else { 7089 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7090 format = ccb12->format; 7091 alloc_len = scsi_4btoul(ccb12->alloc_length); 7092 data_len = sizeof(*data12); 7093 } 7094 if (alloc_len == 0) { 7095 ctl_set_success(ctsio); 7096 ctl_done((union ctl_io *)ctsio); 7097 return (CTL_RETVAL_COMPLETE); 7098 } 7099 7100 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7101 if (data_len < alloc_len) { 7102 ctsio->residual = alloc_len - data_len; 7103 ctsio->kern_data_len = data_len; 7104 ctsio->kern_total_len = data_len; 7105 } else { 7106 ctsio->residual = 0; 7107 ctsio->kern_data_len = alloc_len; 7108 ctsio->kern_total_len = alloc_len; 7109 } 7110 ctsio->kern_data_resid = 0; 7111 ctsio->kern_rel_offset = 0; 7112 ctsio->kern_sg_entries = 0; 7113 7114 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7115 data10 = (struct scsi_read_defect_data_hdr_10 *) 7116 ctsio->kern_data_ptr; 7117 data10->format = format; 7118 scsi_ulto2b(0, data10->length); 7119 } else { 7120 data12 = (struct scsi_read_defect_data_hdr_12 *) 7121 ctsio->kern_data_ptr; 7122 data12->format = format; 7123 scsi_ulto2b(0, data12->generation); 7124 scsi_ulto4b(0, data12->length); 7125 } 7126 7127 ctl_set_success(ctsio); 7128 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7129 ctsio->be_move_done = ctl_config_move_done; 7130 ctl_datamove((union ctl_io *)ctsio); 7131 return (CTL_RETVAL_COMPLETE); 7132} 7133 7134int 7135ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7136{ 7137 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7138 struct ctl_lun *lun = CTL_LUN(ctsio); 7139 struct scsi_maintenance_in *cdb; 7140 int retval; 7141 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7142 int num_ha_groups, num_target_ports, shared_group; 7143 struct ctl_port *port; 7144 struct scsi_target_group_data *rtg_ptr; 7145 struct scsi_target_group_data_extended *rtg_ext_ptr; 7146 struct scsi_target_port_group_descriptor *tpg_desc; 7147 7148 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7149 7150 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7151 retval = CTL_RETVAL_COMPLETE; 7152 7153 switch (cdb->byte2 & STG_PDF_MASK) { 7154 case STG_PDF_LENGTH: 7155 ext = 0; 7156 break; 7157 case STG_PDF_EXTENDED: 7158 ext = 1; 7159 break; 7160 default: 7161 ctl_set_invalid_field(/*ctsio*/ ctsio, 7162 /*sks_valid*/ 1, 7163 /*command*/ 1, 7164 /*field*/ 2, 7165 /*bit_valid*/ 1, 7166 /*bit*/ 5); 7167 ctl_done((union ctl_io *)ctsio); 7168 return(retval); 7169 } 7170 7171 num_target_ports = 0; 7172 shared_group = (softc->is_single != 0); 7173 mtx_lock(&softc->ctl_lock); 7174 STAILQ_FOREACH(port, &softc->port_list, links) { 7175 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7176 continue; 7177 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7178 continue; 7179 num_target_ports++; 7180 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7181 shared_group = 1; 7182 } 7183 mtx_unlock(&softc->ctl_lock); 7184 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7185 7186 if (ext) 7187 total_len = sizeof(struct scsi_target_group_data_extended); 7188 else 7189 total_len = sizeof(struct scsi_target_group_data); 7190 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7191 (shared_group + num_ha_groups) + 7192 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7193 7194 alloc_len = scsi_4btoul(cdb->length); 7195 7196 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7197 7198 ctsio->kern_sg_entries = 0; 7199 7200 if (total_len < alloc_len) { 7201 ctsio->residual = alloc_len - total_len; 7202 ctsio->kern_data_len = total_len; 7203 ctsio->kern_total_len = total_len; 7204 } else { 7205 ctsio->residual = 0; 7206 ctsio->kern_data_len = alloc_len; 7207 ctsio->kern_total_len = alloc_len; 7208 } 7209 ctsio->kern_data_resid = 0; 7210 ctsio->kern_rel_offset = 0; 7211 7212 if (ext) { 7213 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7214 ctsio->kern_data_ptr; 7215 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7216 rtg_ext_ptr->format_type = 0x10; 7217 rtg_ext_ptr->implicit_transition_time = 0; 7218 tpg_desc = &rtg_ext_ptr->groups[0]; 7219 } else { 7220 rtg_ptr = (struct scsi_target_group_data *) 7221 ctsio->kern_data_ptr; 7222 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7223 tpg_desc = &rtg_ptr->groups[0]; 7224 } 7225 7226 mtx_lock(&softc->ctl_lock); 7227 pg = softc->port_min / softc->port_cnt; 7228 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7229 /* Some shelf is known to be primary. */ 7230 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7231 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7232 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7233 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7234 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7235 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7236 else 7237 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7238 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7239 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7240 } else { 7241 ts = os; 7242 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7243 } 7244 } else { 7245 /* No known primary shelf. */ 7246 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7247 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7248 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7249 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7250 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7251 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7252 } else { 7253 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7254 } 7255 } 7256 if (shared_group) { 7257 tpg_desc->pref_state = ts; 7258 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7259 TPG_U_SUP | TPG_T_SUP; 7260 scsi_ulto2b(1, tpg_desc->target_port_group); 7261 tpg_desc->status = TPG_IMPLICIT; 7262 pc = 0; 7263 STAILQ_FOREACH(port, &softc->port_list, links) { 7264 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7265 continue; 7266 if (!softc->is_single && 7267 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7268 continue; 7269 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7270 continue; 7271 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7272 relative_target_port_identifier); 7273 pc++; 7274 } 7275 tpg_desc->target_port_count = pc; 7276 tpg_desc = (struct scsi_target_port_group_descriptor *) 7277 &tpg_desc->descriptors[pc]; 7278 } 7279 for (g = 0; g < num_ha_groups; g++) { 7280 tpg_desc->pref_state = (g == pg) ? ts : os; 7281 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7282 TPG_U_SUP | TPG_T_SUP; 7283 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7284 tpg_desc->status = TPG_IMPLICIT; 7285 pc = 0; 7286 STAILQ_FOREACH(port, &softc->port_list, links) { 7287 if (port->targ_port < g * softc->port_cnt || 7288 port->targ_port >= (g + 1) * softc->port_cnt) 7289 continue; 7290 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7291 continue; 7292 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7293 continue; 7294 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7295 continue; 7296 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7297 relative_target_port_identifier); 7298 pc++; 7299 } 7300 tpg_desc->target_port_count = pc; 7301 tpg_desc = (struct scsi_target_port_group_descriptor *) 7302 &tpg_desc->descriptors[pc]; 7303 } 7304 mtx_unlock(&softc->ctl_lock); 7305 7306 ctl_set_success(ctsio); 7307 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7308 ctsio->be_move_done = ctl_config_move_done; 7309 ctl_datamove((union ctl_io *)ctsio); 7310 return(retval); 7311} 7312 7313int 7314ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7315{ 7316 struct ctl_lun *lun = CTL_LUN(ctsio); 7317 struct scsi_report_supported_opcodes *cdb; 7318 const struct ctl_cmd_entry *entry, *sentry; 7319 struct scsi_report_supported_opcodes_all *all; 7320 struct scsi_report_supported_opcodes_descr *descr; 7321 struct scsi_report_supported_opcodes_one *one; 7322 int retval; 7323 int alloc_len, total_len; 7324 int opcode, service_action, i, j, num; 7325 7326 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7327 7328 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7329 retval = CTL_RETVAL_COMPLETE; 7330 7331 opcode = cdb->requested_opcode; 7332 service_action = scsi_2btoul(cdb->requested_service_action); 7333 switch (cdb->options & RSO_OPTIONS_MASK) { 7334 case RSO_OPTIONS_ALL: 7335 num = 0; 7336 for (i = 0; i < 256; i++) { 7337 entry = &ctl_cmd_table[i]; 7338 if (entry->flags & CTL_CMD_FLAG_SA5) { 7339 for (j = 0; j < 32; j++) { 7340 sentry = &((const struct ctl_cmd_entry *) 7341 entry->execute)[j]; 7342 if (ctl_cmd_applicable( 7343 lun->be_lun->lun_type, sentry)) 7344 num++; 7345 } 7346 } else { 7347 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7348 entry)) 7349 num++; 7350 } 7351 } 7352 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7353 num * sizeof(struct scsi_report_supported_opcodes_descr); 7354 break; 7355 case RSO_OPTIONS_OC: 7356 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7357 ctl_set_invalid_field(/*ctsio*/ ctsio, 7358 /*sks_valid*/ 1, 7359 /*command*/ 1, 7360 /*field*/ 2, 7361 /*bit_valid*/ 1, 7362 /*bit*/ 2); 7363 ctl_done((union ctl_io *)ctsio); 7364 return (CTL_RETVAL_COMPLETE); 7365 } 7366 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7367 break; 7368 case RSO_OPTIONS_OC_SA: 7369 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7370 service_action >= 32) { 7371 ctl_set_invalid_field(/*ctsio*/ ctsio, 7372 /*sks_valid*/ 1, 7373 /*command*/ 1, 7374 /*field*/ 2, 7375 /*bit_valid*/ 1, 7376 /*bit*/ 2); 7377 ctl_done((union ctl_io *)ctsio); 7378 return (CTL_RETVAL_COMPLETE); 7379 } 7380 /* FALLTHROUGH */ 7381 case RSO_OPTIONS_OC_ASA: 7382 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7383 break; 7384 default: 7385 ctl_set_invalid_field(/*ctsio*/ ctsio, 7386 /*sks_valid*/ 1, 7387 /*command*/ 1, 7388 /*field*/ 2, 7389 /*bit_valid*/ 1, 7390 /*bit*/ 2); 7391 ctl_done((union ctl_io *)ctsio); 7392 return (CTL_RETVAL_COMPLETE); 7393 } 7394 7395 alloc_len = scsi_4btoul(cdb->length); 7396 7397 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7398 7399 ctsio->kern_sg_entries = 0; 7400 7401 if (total_len < alloc_len) { 7402 ctsio->residual = alloc_len - total_len; 7403 ctsio->kern_data_len = total_len; 7404 ctsio->kern_total_len = total_len; 7405 } else { 7406 ctsio->residual = 0; 7407 ctsio->kern_data_len = alloc_len; 7408 ctsio->kern_total_len = alloc_len; 7409 } 7410 ctsio->kern_data_resid = 0; 7411 ctsio->kern_rel_offset = 0; 7412 7413 switch (cdb->options & RSO_OPTIONS_MASK) { 7414 case RSO_OPTIONS_ALL: 7415 all = (struct scsi_report_supported_opcodes_all *) 7416 ctsio->kern_data_ptr; 7417 num = 0; 7418 for (i = 0; i < 256; i++) { 7419 entry = &ctl_cmd_table[i]; 7420 if (entry->flags & CTL_CMD_FLAG_SA5) { 7421 for (j = 0; j < 32; j++) { 7422 sentry = &((const struct ctl_cmd_entry *) 7423 entry->execute)[j]; 7424 if (!ctl_cmd_applicable( 7425 lun->be_lun->lun_type, sentry)) 7426 continue; 7427 descr = &all->descr[num++]; 7428 descr->opcode = i; 7429 scsi_ulto2b(j, descr->service_action); 7430 descr->flags = RSO_SERVACTV; 7431 scsi_ulto2b(sentry->length, 7432 descr->cdb_length); 7433 } 7434 } else { 7435 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7436 entry)) 7437 continue; 7438 descr = &all->descr[num++]; 7439 descr->opcode = i; 7440 scsi_ulto2b(0, descr->service_action); 7441 descr->flags = 0; 7442 scsi_ulto2b(entry->length, descr->cdb_length); 7443 } 7444 } 7445 scsi_ulto4b( 7446 num * sizeof(struct scsi_report_supported_opcodes_descr), 7447 all->length); 7448 break; 7449 case RSO_OPTIONS_OC: 7450 one = (struct scsi_report_supported_opcodes_one *) 7451 ctsio->kern_data_ptr; 7452 entry = &ctl_cmd_table[opcode]; 7453 goto fill_one; 7454 case RSO_OPTIONS_OC_SA: 7455 one = (struct scsi_report_supported_opcodes_one *) 7456 ctsio->kern_data_ptr; 7457 entry = &ctl_cmd_table[opcode]; 7458 entry = &((const struct ctl_cmd_entry *) 7459 entry->execute)[service_action]; 7460fill_one: 7461 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7462 one->support = 3; 7463 scsi_ulto2b(entry->length, one->cdb_length); 7464 one->cdb_usage[0] = opcode; 7465 memcpy(&one->cdb_usage[1], entry->usage, 7466 entry->length - 1); 7467 } else 7468 one->support = 1; 7469 break; 7470 case RSO_OPTIONS_OC_ASA: 7471 one = (struct scsi_report_supported_opcodes_one *) 7472 ctsio->kern_data_ptr; 7473 entry = &ctl_cmd_table[opcode]; 7474 if (entry->flags & CTL_CMD_FLAG_SA5) { 7475 entry = &((const struct ctl_cmd_entry *) 7476 entry->execute)[service_action]; 7477 } else if (service_action != 0) { 7478 one->support = 1; 7479 break; 7480 } 7481 goto fill_one; 7482 } 7483 7484 ctl_set_success(ctsio); 7485 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7486 ctsio->be_move_done = ctl_config_move_done; 7487 ctl_datamove((union ctl_io *)ctsio); 7488 return(retval); 7489} 7490 7491int 7492ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7493{ 7494 struct scsi_report_supported_tmf *cdb; 7495 struct scsi_report_supported_tmf_ext_data *data; 7496 int retval; 7497 int alloc_len, total_len; 7498 7499 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7500 7501 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7502 7503 retval = CTL_RETVAL_COMPLETE; 7504 7505 if (cdb->options & RST_REPD) 7506 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7507 else 7508 total_len = sizeof(struct scsi_report_supported_tmf_data); 7509 alloc_len = scsi_4btoul(cdb->length); 7510 7511 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7512 7513 ctsio->kern_sg_entries = 0; 7514 7515 if (total_len < alloc_len) { 7516 ctsio->residual = alloc_len - total_len; 7517 ctsio->kern_data_len = total_len; 7518 ctsio->kern_total_len = total_len; 7519 } else { 7520 ctsio->residual = 0; 7521 ctsio->kern_data_len = alloc_len; 7522 ctsio->kern_total_len = alloc_len; 7523 } 7524 ctsio->kern_data_resid = 0; 7525 ctsio->kern_rel_offset = 0; 7526 7527 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7528 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7529 RST_TRS; 7530 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7531 data->length = total_len - 4; 7532 7533 ctl_set_success(ctsio); 7534 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7535 ctsio->be_move_done = ctl_config_move_done; 7536 ctl_datamove((union ctl_io *)ctsio); 7537 return (retval); 7538} 7539 7540int 7541ctl_report_timestamp(struct ctl_scsiio *ctsio) 7542{ 7543 struct scsi_report_timestamp *cdb; 7544 struct scsi_report_timestamp_data *data; 7545 struct timeval tv; 7546 int64_t timestamp; 7547 int retval; 7548 int alloc_len, total_len; 7549 7550 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7551 7552 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7553 7554 retval = CTL_RETVAL_COMPLETE; 7555 7556 total_len = sizeof(struct scsi_report_timestamp_data); 7557 alloc_len = scsi_4btoul(cdb->length); 7558 7559 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7560 7561 ctsio->kern_sg_entries = 0; 7562 7563 if (total_len < alloc_len) { 7564 ctsio->residual = alloc_len - total_len; 7565 ctsio->kern_data_len = total_len; 7566 ctsio->kern_total_len = total_len; 7567 } else { 7568 ctsio->residual = 0; 7569 ctsio->kern_data_len = alloc_len; 7570 ctsio->kern_total_len = alloc_len; 7571 } 7572 ctsio->kern_data_resid = 0; 7573 ctsio->kern_rel_offset = 0; 7574 7575 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7576 scsi_ulto2b(sizeof(*data) - 2, data->length); 7577 data->origin = RTS_ORIG_OUTSIDE; 7578 getmicrotime(&tv); 7579 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7580 scsi_ulto4b(timestamp >> 16, data->timestamp); 7581 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7582 7583 ctl_set_success(ctsio); 7584 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7585 ctsio->be_move_done = ctl_config_move_done; 7586 ctl_datamove((union ctl_io *)ctsio); 7587 return (retval); 7588} 7589 7590int 7591ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7592{ 7593 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7594 struct ctl_lun *lun = CTL_LUN(ctsio); 7595 struct scsi_per_res_in *cdb; 7596 int alloc_len, total_len = 0; 7597 /* struct scsi_per_res_in_rsrv in_data; */ 7598 uint64_t key; 7599 7600 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7601 7602 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7603 7604 alloc_len = scsi_2btoul(cdb->length); 7605 7606retry: 7607 mtx_lock(&lun->lun_lock); 7608 switch (cdb->action) { 7609 case SPRI_RK: /* read keys */ 7610 total_len = sizeof(struct scsi_per_res_in_keys) + 7611 lun->pr_key_count * 7612 sizeof(struct scsi_per_res_key); 7613 break; 7614 case SPRI_RR: /* read reservation */ 7615 if (lun->flags & CTL_LUN_PR_RESERVED) 7616 total_len = sizeof(struct scsi_per_res_in_rsrv); 7617 else 7618 total_len = sizeof(struct scsi_per_res_in_header); 7619 break; 7620 case SPRI_RC: /* report capabilities */ 7621 total_len = sizeof(struct scsi_per_res_cap); 7622 break; 7623 case SPRI_RS: /* read full status */ 7624 total_len = sizeof(struct scsi_per_res_in_header) + 7625 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7626 lun->pr_key_count; 7627 break; 7628 default: 7629 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7630 } 7631 mtx_unlock(&lun->lun_lock); 7632 7633 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7634 7635 if (total_len < alloc_len) { 7636 ctsio->residual = alloc_len - total_len; 7637 ctsio->kern_data_len = total_len; 7638 ctsio->kern_total_len = total_len; 7639 } else { 7640 ctsio->residual = 0; 7641 ctsio->kern_data_len = alloc_len; 7642 ctsio->kern_total_len = alloc_len; 7643 } 7644 7645 ctsio->kern_data_resid = 0; 7646 ctsio->kern_rel_offset = 0; 7647 ctsio->kern_sg_entries = 0; 7648 7649 mtx_lock(&lun->lun_lock); 7650 switch (cdb->action) { 7651 case SPRI_RK: { // read keys 7652 struct scsi_per_res_in_keys *res_keys; 7653 int i, key_count; 7654 7655 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7656 7657 /* 7658 * We had to drop the lock to allocate our buffer, which 7659 * leaves time for someone to come in with another 7660 * persistent reservation. (That is unlikely, though, 7661 * since this should be the only persistent reservation 7662 * command active right now.) 7663 */ 7664 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7665 (lun->pr_key_count * 7666 sizeof(struct scsi_per_res_key)))){ 7667 mtx_unlock(&lun->lun_lock); 7668 free(ctsio->kern_data_ptr, M_CTL); 7669 printf("%s: reservation length changed, retrying\n", 7670 __func__); 7671 goto retry; 7672 } 7673 7674 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7675 7676 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7677 lun->pr_key_count, res_keys->header.length); 7678 7679 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7680 if ((key = ctl_get_prkey(lun, i)) == 0) 7681 continue; 7682 7683 /* 7684 * We used lun->pr_key_count to calculate the 7685 * size to allocate. If it turns out the number of 7686 * initiators with the registered flag set is 7687 * larger than that (i.e. they haven't been kept in 7688 * sync), we've got a problem. 7689 */ 7690 if (key_count >= lun->pr_key_count) { 7691 key_count++; 7692 continue; 7693 } 7694 scsi_u64to8b(key, res_keys->keys[key_count].key); 7695 key_count++; 7696 } 7697 break; 7698 } 7699 case SPRI_RR: { // read reservation 7700 struct scsi_per_res_in_rsrv *res; 7701 int tmp_len, header_only; 7702 7703 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7704 7705 scsi_ulto4b(lun->pr_generation, res->header.generation); 7706 7707 if (lun->flags & CTL_LUN_PR_RESERVED) 7708 { 7709 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7710 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7711 res->header.length); 7712 header_only = 0; 7713 } else { 7714 tmp_len = sizeof(struct scsi_per_res_in_header); 7715 scsi_ulto4b(0, res->header.length); 7716 header_only = 1; 7717 } 7718 7719 /* 7720 * We had to drop the lock to allocate our buffer, which 7721 * leaves time for someone to come in with another 7722 * persistent reservation. (That is unlikely, though, 7723 * since this should be the only persistent reservation 7724 * command active right now.) 7725 */ 7726 if (tmp_len != total_len) { 7727 mtx_unlock(&lun->lun_lock); 7728 free(ctsio->kern_data_ptr, M_CTL); 7729 printf("%s: reservation status changed, retrying\n", 7730 __func__); 7731 goto retry; 7732 } 7733 7734 /* 7735 * No reservation held, so we're done. 7736 */ 7737 if (header_only != 0) 7738 break; 7739 7740 /* 7741 * If the registration is an All Registrants type, the key 7742 * is 0, since it doesn't really matter. 7743 */ 7744 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7745 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7746 res->data.reservation); 7747 } 7748 res->data.scopetype = lun->pr_res_type; 7749 break; 7750 } 7751 case SPRI_RC: //report capabilities 7752 { 7753 struct scsi_per_res_cap *res_cap; 7754 uint16_t type_mask; 7755 7756 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7757 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7758 res_cap->flags1 = SPRI_CRH; 7759 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7760 type_mask = SPRI_TM_WR_EX_AR | 7761 SPRI_TM_EX_AC_RO | 7762 SPRI_TM_WR_EX_RO | 7763 SPRI_TM_EX_AC | 7764 SPRI_TM_WR_EX | 7765 SPRI_TM_EX_AC_AR; 7766 scsi_ulto2b(type_mask, res_cap->type_mask); 7767 break; 7768 } 7769 case SPRI_RS: { // read full status 7770 struct scsi_per_res_in_full *res_status; 7771 struct scsi_per_res_in_full_desc *res_desc; 7772 struct ctl_port *port; 7773 int i, len; 7774 7775 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7776 7777 /* 7778 * We had to drop the lock to allocate our buffer, which 7779 * leaves time for someone to come in with another 7780 * persistent reservation. (That is unlikely, though, 7781 * since this should be the only persistent reservation 7782 * command active right now.) 7783 */ 7784 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7785 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7786 lun->pr_key_count)){ 7787 mtx_unlock(&lun->lun_lock); 7788 free(ctsio->kern_data_ptr, M_CTL); 7789 printf("%s: reservation length changed, retrying\n", 7790 __func__); 7791 goto retry; 7792 } 7793 7794 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7795 7796 res_desc = &res_status->desc[0]; 7797 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7798 if ((key = ctl_get_prkey(lun, i)) == 0) 7799 continue; 7800 7801 scsi_u64to8b(key, res_desc->res_key.key); 7802 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7803 (lun->pr_res_idx == i || 7804 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7805 res_desc->flags = SPRI_FULL_R_HOLDER; 7806 res_desc->scopetype = lun->pr_res_type; 7807 } 7808 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7809 res_desc->rel_trgt_port_id); 7810 len = 0; 7811 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7812 if (port != NULL) 7813 len = ctl_create_iid(port, 7814 i % CTL_MAX_INIT_PER_PORT, 7815 res_desc->transport_id); 7816 scsi_ulto4b(len, res_desc->additional_length); 7817 res_desc = (struct scsi_per_res_in_full_desc *) 7818 &res_desc->transport_id[len]; 7819 } 7820 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7821 res_status->header.length); 7822 break; 7823 } 7824 default: 7825 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7826 } 7827 mtx_unlock(&lun->lun_lock); 7828 7829 ctl_set_success(ctsio); 7830 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7831 ctsio->be_move_done = ctl_config_move_done; 7832 ctl_datamove((union ctl_io *)ctsio); 7833 return (CTL_RETVAL_COMPLETE); 7834} 7835 7836/* 7837 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7838 * it should return. 7839 */ 7840static int 7841ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7842 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7843 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7844 struct scsi_per_res_out_parms* param) 7845{ 7846 union ctl_ha_msg persis_io; 7847 int i; 7848 7849 mtx_lock(&lun->lun_lock); 7850 if (sa_res_key == 0) { 7851 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7852 /* validate scope and type */ 7853 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7854 SPR_LU_SCOPE) { 7855 mtx_unlock(&lun->lun_lock); 7856 ctl_set_invalid_field(/*ctsio*/ ctsio, 7857 /*sks_valid*/ 1, 7858 /*command*/ 1, 7859 /*field*/ 2, 7860 /*bit_valid*/ 1, 7861 /*bit*/ 4); 7862 ctl_done((union ctl_io *)ctsio); 7863 return (1); 7864 } 7865 7866 if (type>8 || type==2 || type==4 || type==0) { 7867 mtx_unlock(&lun->lun_lock); 7868 ctl_set_invalid_field(/*ctsio*/ ctsio, 7869 /*sks_valid*/ 1, 7870 /*command*/ 1, 7871 /*field*/ 2, 7872 /*bit_valid*/ 1, 7873 /*bit*/ 0); 7874 ctl_done((union ctl_io *)ctsio); 7875 return (1); 7876 } 7877 7878 /* 7879 * Unregister everybody else and build UA for 7880 * them 7881 */ 7882 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7883 if (i == residx || ctl_get_prkey(lun, i) == 0) 7884 continue; 7885 7886 ctl_clr_prkey(lun, i); 7887 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7888 } 7889 lun->pr_key_count = 1; 7890 lun->pr_res_type = type; 7891 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7892 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7893 lun->pr_res_idx = residx; 7894 lun->pr_generation++; 7895 mtx_unlock(&lun->lun_lock); 7896 7897 /* send msg to other side */ 7898 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7899 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7900 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7901 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7902 persis_io.pr.pr_info.res_type = type; 7903 memcpy(persis_io.pr.pr_info.sa_res_key, 7904 param->serv_act_res_key, 7905 sizeof(param->serv_act_res_key)); 7906 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7907 sizeof(persis_io.pr), M_WAITOK); 7908 } else { 7909 /* not all registrants */ 7910 mtx_unlock(&lun->lun_lock); 7911 free(ctsio->kern_data_ptr, M_CTL); 7912 ctl_set_invalid_field(ctsio, 7913 /*sks_valid*/ 1, 7914 /*command*/ 0, 7915 /*field*/ 8, 7916 /*bit_valid*/ 0, 7917 /*bit*/ 0); 7918 ctl_done((union ctl_io *)ctsio); 7919 return (1); 7920 } 7921 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7922 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7923 int found = 0; 7924 7925 if (res_key == sa_res_key) { 7926 /* special case */ 7927 /* 7928 * The spec implies this is not good but doesn't 7929 * say what to do. There are two choices either 7930 * generate a res conflict or check condition 7931 * with illegal field in parameter data. Since 7932 * that is what is done when the sa_res_key is 7933 * zero I'll take that approach since this has 7934 * to do with the sa_res_key. 7935 */ 7936 mtx_unlock(&lun->lun_lock); 7937 free(ctsio->kern_data_ptr, M_CTL); 7938 ctl_set_invalid_field(ctsio, 7939 /*sks_valid*/ 1, 7940 /*command*/ 0, 7941 /*field*/ 8, 7942 /*bit_valid*/ 0, 7943 /*bit*/ 0); 7944 ctl_done((union ctl_io *)ctsio); 7945 return (1); 7946 } 7947 7948 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7949 if (ctl_get_prkey(lun, i) != sa_res_key) 7950 continue; 7951 7952 found = 1; 7953 ctl_clr_prkey(lun, i); 7954 lun->pr_key_count--; 7955 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7956 } 7957 if (!found) { 7958 mtx_unlock(&lun->lun_lock); 7959 free(ctsio->kern_data_ptr, M_CTL); 7960 ctl_set_reservation_conflict(ctsio); 7961 ctl_done((union ctl_io *)ctsio); 7962 return (CTL_RETVAL_COMPLETE); 7963 } 7964 lun->pr_generation++; 7965 mtx_unlock(&lun->lun_lock); 7966 7967 /* send msg to other side */ 7968 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7969 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7970 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7971 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7972 persis_io.pr.pr_info.res_type = type; 7973 memcpy(persis_io.pr.pr_info.sa_res_key, 7974 param->serv_act_res_key, 7975 sizeof(param->serv_act_res_key)); 7976 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7977 sizeof(persis_io.pr), M_WAITOK); 7978 } else { 7979 /* Reserved but not all registrants */ 7980 /* sa_res_key is res holder */ 7981 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7982 /* validate scope and type */ 7983 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7984 SPR_LU_SCOPE) { 7985 mtx_unlock(&lun->lun_lock); 7986 ctl_set_invalid_field(/*ctsio*/ ctsio, 7987 /*sks_valid*/ 1, 7988 /*command*/ 1, 7989 /*field*/ 2, 7990 /*bit_valid*/ 1, 7991 /*bit*/ 4); 7992 ctl_done((union ctl_io *)ctsio); 7993 return (1); 7994 } 7995 7996 if (type>8 || type==2 || type==4 || type==0) { 7997 mtx_unlock(&lun->lun_lock); 7998 ctl_set_invalid_field(/*ctsio*/ ctsio, 7999 /*sks_valid*/ 1, 8000 /*command*/ 1, 8001 /*field*/ 2, 8002 /*bit_valid*/ 1, 8003 /*bit*/ 0); 8004 ctl_done((union ctl_io *)ctsio); 8005 return (1); 8006 } 8007 8008 /* 8009 * Do the following: 8010 * if sa_res_key != res_key remove all 8011 * registrants w/sa_res_key and generate UA 8012 * for these registrants(Registrations 8013 * Preempted) if it wasn't an exclusive 8014 * reservation generate UA(Reservations 8015 * Preempted) for all other registered nexuses 8016 * if the type has changed. Establish the new 8017 * reservation and holder. If res_key and 8018 * sa_res_key are the same do the above 8019 * except don't unregister the res holder. 8020 */ 8021 8022 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8023 if (i == residx || ctl_get_prkey(lun, i) == 0) 8024 continue; 8025 8026 if (sa_res_key == ctl_get_prkey(lun, i)) { 8027 ctl_clr_prkey(lun, i); 8028 lun->pr_key_count--; 8029 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8030 } else if (type != lun->pr_res_type && 8031 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8032 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8033 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8034 } 8035 } 8036 lun->pr_res_type = type; 8037 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8038 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8039 lun->pr_res_idx = residx; 8040 else 8041 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8042 lun->pr_generation++; 8043 mtx_unlock(&lun->lun_lock); 8044 8045 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8046 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8047 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8048 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8049 persis_io.pr.pr_info.res_type = type; 8050 memcpy(persis_io.pr.pr_info.sa_res_key, 8051 param->serv_act_res_key, 8052 sizeof(param->serv_act_res_key)); 8053 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8054 sizeof(persis_io.pr), M_WAITOK); 8055 } else { 8056 /* 8057 * sa_res_key is not the res holder just 8058 * remove registrants 8059 */ 8060 int found=0; 8061 8062 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8063 if (sa_res_key != ctl_get_prkey(lun, i)) 8064 continue; 8065 8066 found = 1; 8067 ctl_clr_prkey(lun, i); 8068 lun->pr_key_count--; 8069 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8070 } 8071 8072 if (!found) { 8073 mtx_unlock(&lun->lun_lock); 8074 free(ctsio->kern_data_ptr, M_CTL); 8075 ctl_set_reservation_conflict(ctsio); 8076 ctl_done((union ctl_io *)ctsio); 8077 return (1); 8078 } 8079 lun->pr_generation++; 8080 mtx_unlock(&lun->lun_lock); 8081 8082 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8083 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8084 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8085 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8086 persis_io.pr.pr_info.res_type = type; 8087 memcpy(persis_io.pr.pr_info.sa_res_key, 8088 param->serv_act_res_key, 8089 sizeof(param->serv_act_res_key)); 8090 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8091 sizeof(persis_io.pr), M_WAITOK); 8092 } 8093 } 8094 return (0); 8095} 8096 8097static void 8098ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8099{ 8100 uint64_t sa_res_key; 8101 int i; 8102 8103 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8104 8105 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8106 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8107 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8108 if (sa_res_key == 0) { 8109 /* 8110 * Unregister everybody else and build UA for 8111 * them 8112 */ 8113 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8114 if (i == msg->pr.pr_info.residx || 8115 ctl_get_prkey(lun, i) == 0) 8116 continue; 8117 8118 ctl_clr_prkey(lun, i); 8119 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8120 } 8121 8122 lun->pr_key_count = 1; 8123 lun->pr_res_type = msg->pr.pr_info.res_type; 8124 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8125 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8126 lun->pr_res_idx = msg->pr.pr_info.residx; 8127 } else { 8128 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8129 if (sa_res_key == ctl_get_prkey(lun, i)) 8130 continue; 8131 8132 ctl_clr_prkey(lun, i); 8133 lun->pr_key_count--; 8134 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8135 } 8136 } 8137 } else { 8138 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8139 if (i == msg->pr.pr_info.residx || 8140 ctl_get_prkey(lun, i) == 0) 8141 continue; 8142 8143 if (sa_res_key == ctl_get_prkey(lun, i)) { 8144 ctl_clr_prkey(lun, i); 8145 lun->pr_key_count--; 8146 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8147 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8148 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8149 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8150 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8151 } 8152 } 8153 lun->pr_res_type = msg->pr.pr_info.res_type; 8154 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8155 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8156 lun->pr_res_idx = msg->pr.pr_info.residx; 8157 else 8158 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8159 } 8160 lun->pr_generation++; 8161 8162} 8163 8164 8165int 8166ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8167{ 8168 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8169 struct ctl_lun *lun = CTL_LUN(ctsio); 8170 int retval; 8171 u_int32_t param_len; 8172 struct scsi_per_res_out *cdb; 8173 struct scsi_per_res_out_parms* param; 8174 uint32_t residx; 8175 uint64_t res_key, sa_res_key, key; 8176 uint8_t type; 8177 union ctl_ha_msg persis_io; 8178 int i; 8179 8180 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8181 8182 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8183 retval = CTL_RETVAL_COMPLETE; 8184 8185 /* 8186 * We only support whole-LUN scope. The scope & type are ignored for 8187 * register, register and ignore existing key and clear. 8188 * We sometimes ignore scope and type on preempts too!! 8189 * Verify reservation type here as well. 8190 */ 8191 type = cdb->scope_type & SPR_TYPE_MASK; 8192 if ((cdb->action == SPRO_RESERVE) 8193 || (cdb->action == SPRO_RELEASE)) { 8194 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8195 ctl_set_invalid_field(/*ctsio*/ ctsio, 8196 /*sks_valid*/ 1, 8197 /*command*/ 1, 8198 /*field*/ 2, 8199 /*bit_valid*/ 1, 8200 /*bit*/ 4); 8201 ctl_done((union ctl_io *)ctsio); 8202 return (CTL_RETVAL_COMPLETE); 8203 } 8204 8205 if (type>8 || type==2 || type==4 || type==0) { 8206 ctl_set_invalid_field(/*ctsio*/ ctsio, 8207 /*sks_valid*/ 1, 8208 /*command*/ 1, 8209 /*field*/ 2, 8210 /*bit_valid*/ 1, 8211 /*bit*/ 0); 8212 ctl_done((union ctl_io *)ctsio); 8213 return (CTL_RETVAL_COMPLETE); 8214 } 8215 } 8216 8217 param_len = scsi_4btoul(cdb->length); 8218 8219 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8220 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8221 ctsio->kern_data_len = param_len; 8222 ctsio->kern_total_len = param_len; 8223 ctsio->kern_data_resid = 0; 8224 ctsio->kern_rel_offset = 0; 8225 ctsio->kern_sg_entries = 0; 8226 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8227 ctsio->be_move_done = ctl_config_move_done; 8228 ctl_datamove((union ctl_io *)ctsio); 8229 8230 return (CTL_RETVAL_COMPLETE); 8231 } 8232 8233 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8234 8235 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8236 res_key = scsi_8btou64(param->res_key.key); 8237 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8238 8239 /* 8240 * Validate the reservation key here except for SPRO_REG_IGNO 8241 * This must be done for all other service actions 8242 */ 8243 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8244 mtx_lock(&lun->lun_lock); 8245 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8246 if (res_key != key) { 8247 /* 8248 * The current key passed in doesn't match 8249 * the one the initiator previously 8250 * registered. 8251 */ 8252 mtx_unlock(&lun->lun_lock); 8253 free(ctsio->kern_data_ptr, M_CTL); 8254 ctl_set_reservation_conflict(ctsio); 8255 ctl_done((union ctl_io *)ctsio); 8256 return (CTL_RETVAL_COMPLETE); 8257 } 8258 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8259 /* 8260 * We are not registered 8261 */ 8262 mtx_unlock(&lun->lun_lock); 8263 free(ctsio->kern_data_ptr, M_CTL); 8264 ctl_set_reservation_conflict(ctsio); 8265 ctl_done((union ctl_io *)ctsio); 8266 return (CTL_RETVAL_COMPLETE); 8267 } else if (res_key != 0) { 8268 /* 8269 * We are not registered and trying to register but 8270 * the register key isn't zero. 8271 */ 8272 mtx_unlock(&lun->lun_lock); 8273 free(ctsio->kern_data_ptr, M_CTL); 8274 ctl_set_reservation_conflict(ctsio); 8275 ctl_done((union ctl_io *)ctsio); 8276 return (CTL_RETVAL_COMPLETE); 8277 } 8278 mtx_unlock(&lun->lun_lock); 8279 } 8280 8281 switch (cdb->action & SPRO_ACTION_MASK) { 8282 case SPRO_REGISTER: 8283 case SPRO_REG_IGNO: { 8284 8285#if 0 8286 printf("Registration received\n"); 8287#endif 8288 8289 /* 8290 * We don't support any of these options, as we report in 8291 * the read capabilities request (see 8292 * ctl_persistent_reserve_in(), above). 8293 */ 8294 if ((param->flags & SPR_SPEC_I_PT) 8295 || (param->flags & SPR_ALL_TG_PT) 8296 || (param->flags & SPR_APTPL)) { 8297 int bit_ptr; 8298 8299 if (param->flags & SPR_APTPL) 8300 bit_ptr = 0; 8301 else if (param->flags & SPR_ALL_TG_PT) 8302 bit_ptr = 2; 8303 else /* SPR_SPEC_I_PT */ 8304 bit_ptr = 3; 8305 8306 free(ctsio->kern_data_ptr, M_CTL); 8307 ctl_set_invalid_field(ctsio, 8308 /*sks_valid*/ 1, 8309 /*command*/ 0, 8310 /*field*/ 20, 8311 /*bit_valid*/ 1, 8312 /*bit*/ bit_ptr); 8313 ctl_done((union ctl_io *)ctsio); 8314 return (CTL_RETVAL_COMPLETE); 8315 } 8316 8317 mtx_lock(&lun->lun_lock); 8318 8319 /* 8320 * The initiator wants to clear the 8321 * key/unregister. 8322 */ 8323 if (sa_res_key == 0) { 8324 if ((res_key == 0 8325 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8326 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8327 && ctl_get_prkey(lun, residx) == 0)) { 8328 mtx_unlock(&lun->lun_lock); 8329 goto done; 8330 } 8331 8332 ctl_clr_prkey(lun, residx); 8333 lun->pr_key_count--; 8334 8335 if (residx == lun->pr_res_idx) { 8336 lun->flags &= ~CTL_LUN_PR_RESERVED; 8337 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8338 8339 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8340 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8341 lun->pr_key_count) { 8342 /* 8343 * If the reservation is a registrants 8344 * only type we need to generate a UA 8345 * for other registered inits. The 8346 * sense code should be RESERVATIONS 8347 * RELEASED 8348 */ 8349 8350 for (i = softc->init_min; i < softc->init_max; i++){ 8351 if (ctl_get_prkey(lun, i) == 0) 8352 continue; 8353 ctl_est_ua(lun, i, 8354 CTL_UA_RES_RELEASE); 8355 } 8356 } 8357 lun->pr_res_type = 0; 8358 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8359 if (lun->pr_key_count==0) { 8360 lun->flags &= ~CTL_LUN_PR_RESERVED; 8361 lun->pr_res_type = 0; 8362 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8363 } 8364 } 8365 lun->pr_generation++; 8366 mtx_unlock(&lun->lun_lock); 8367 8368 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8369 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8370 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8371 persis_io.pr.pr_info.residx = residx; 8372 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8373 sizeof(persis_io.pr), M_WAITOK); 8374 } else /* sa_res_key != 0 */ { 8375 8376 /* 8377 * If we aren't registered currently then increment 8378 * the key count and set the registered flag. 8379 */ 8380 ctl_alloc_prkey(lun, residx); 8381 if (ctl_get_prkey(lun, residx) == 0) 8382 lun->pr_key_count++; 8383 ctl_set_prkey(lun, residx, sa_res_key); 8384 lun->pr_generation++; 8385 mtx_unlock(&lun->lun_lock); 8386 8387 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8388 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8389 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8390 persis_io.pr.pr_info.residx = residx; 8391 memcpy(persis_io.pr.pr_info.sa_res_key, 8392 param->serv_act_res_key, 8393 sizeof(param->serv_act_res_key)); 8394 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8395 sizeof(persis_io.pr), M_WAITOK); 8396 } 8397 8398 break; 8399 } 8400 case SPRO_RESERVE: 8401#if 0 8402 printf("Reserve executed type %d\n", type); 8403#endif 8404 mtx_lock(&lun->lun_lock); 8405 if (lun->flags & CTL_LUN_PR_RESERVED) { 8406 /* 8407 * if this isn't the reservation holder and it's 8408 * not a "all registrants" type or if the type is 8409 * different then we have a conflict 8410 */ 8411 if ((lun->pr_res_idx != residx 8412 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8413 || lun->pr_res_type != type) { 8414 mtx_unlock(&lun->lun_lock); 8415 free(ctsio->kern_data_ptr, M_CTL); 8416 ctl_set_reservation_conflict(ctsio); 8417 ctl_done((union ctl_io *)ctsio); 8418 return (CTL_RETVAL_COMPLETE); 8419 } 8420 mtx_unlock(&lun->lun_lock); 8421 } else /* create a reservation */ { 8422 /* 8423 * If it's not an "all registrants" type record 8424 * reservation holder 8425 */ 8426 if (type != SPR_TYPE_WR_EX_AR 8427 && type != SPR_TYPE_EX_AC_AR) 8428 lun->pr_res_idx = residx; /* Res holder */ 8429 else 8430 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8431 8432 lun->flags |= CTL_LUN_PR_RESERVED; 8433 lun->pr_res_type = type; 8434 8435 mtx_unlock(&lun->lun_lock); 8436 8437 /* send msg to other side */ 8438 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8439 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8440 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8441 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8442 persis_io.pr.pr_info.res_type = type; 8443 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8444 sizeof(persis_io.pr), M_WAITOK); 8445 } 8446 break; 8447 8448 case SPRO_RELEASE: 8449 mtx_lock(&lun->lun_lock); 8450 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8451 /* No reservation exists return good status */ 8452 mtx_unlock(&lun->lun_lock); 8453 goto done; 8454 } 8455 /* 8456 * Is this nexus a reservation holder? 8457 */ 8458 if (lun->pr_res_idx != residx 8459 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8460 /* 8461 * not a res holder return good status but 8462 * do nothing 8463 */ 8464 mtx_unlock(&lun->lun_lock); 8465 goto done; 8466 } 8467 8468 if (lun->pr_res_type != type) { 8469 mtx_unlock(&lun->lun_lock); 8470 free(ctsio->kern_data_ptr, M_CTL); 8471 ctl_set_illegal_pr_release(ctsio); 8472 ctl_done((union ctl_io *)ctsio); 8473 return (CTL_RETVAL_COMPLETE); 8474 } 8475 8476 /* okay to release */ 8477 lun->flags &= ~CTL_LUN_PR_RESERVED; 8478 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8479 lun->pr_res_type = 0; 8480 8481 /* 8482 * If this isn't an exclusive access reservation and NUAR 8483 * is not set, generate UA for all other registrants. 8484 */ 8485 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8486 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8487 for (i = softc->init_min; i < softc->init_max; i++) { 8488 if (i == residx || ctl_get_prkey(lun, i) == 0) 8489 continue; 8490 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8491 } 8492 } 8493 mtx_unlock(&lun->lun_lock); 8494 8495 /* Send msg to other side */ 8496 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8497 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8498 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8499 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8500 sizeof(persis_io.pr), M_WAITOK); 8501 break; 8502 8503 case SPRO_CLEAR: 8504 /* send msg to other side */ 8505 8506 mtx_lock(&lun->lun_lock); 8507 lun->flags &= ~CTL_LUN_PR_RESERVED; 8508 lun->pr_res_type = 0; 8509 lun->pr_key_count = 0; 8510 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8511 8512 ctl_clr_prkey(lun, residx); 8513 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8514 if (ctl_get_prkey(lun, i) != 0) { 8515 ctl_clr_prkey(lun, i); 8516 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8517 } 8518 lun->pr_generation++; 8519 mtx_unlock(&lun->lun_lock); 8520 8521 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8522 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8523 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8524 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8525 sizeof(persis_io.pr), M_WAITOK); 8526 break; 8527 8528 case SPRO_PREEMPT: 8529 case SPRO_PRE_ABO: { 8530 int nretval; 8531 8532 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8533 residx, ctsio, cdb, param); 8534 if (nretval != 0) 8535 return (CTL_RETVAL_COMPLETE); 8536 break; 8537 } 8538 default: 8539 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8540 } 8541 8542done: 8543 free(ctsio->kern_data_ptr, M_CTL); 8544 ctl_set_success(ctsio); 8545 ctl_done((union ctl_io *)ctsio); 8546 8547 return (retval); 8548} 8549 8550/* 8551 * This routine is for handling a message from the other SC pertaining to 8552 * persistent reserve out. All the error checking will have been done 8553 * so only perorming the action need be done here to keep the two 8554 * in sync. 8555 */ 8556static void 8557ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8558{ 8559 struct ctl_softc *softc = CTL_SOFTC(io); 8560 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8561 struct ctl_lun *lun; 8562 int i; 8563 uint32_t residx, targ_lun; 8564 8565 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8566 mtx_lock(&softc->ctl_lock); 8567 if (targ_lun >= CTL_MAX_LUNS || 8568 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8569 mtx_unlock(&softc->ctl_lock); 8570 return; 8571 } 8572 mtx_lock(&lun->lun_lock); 8573 mtx_unlock(&softc->ctl_lock); 8574 if (lun->flags & CTL_LUN_DISABLED) { 8575 mtx_unlock(&lun->lun_lock); 8576 return; 8577 } 8578 residx = ctl_get_initindex(&msg->hdr.nexus); 8579 switch(msg->pr.pr_info.action) { 8580 case CTL_PR_REG_KEY: 8581 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8582 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8583 lun->pr_key_count++; 8584 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8585 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8586 lun->pr_generation++; 8587 break; 8588 8589 case CTL_PR_UNREG_KEY: 8590 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8591 lun->pr_key_count--; 8592 8593 /* XXX Need to see if the reservation has been released */ 8594 /* if so do we need to generate UA? */ 8595 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8596 lun->flags &= ~CTL_LUN_PR_RESERVED; 8597 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8598 8599 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8600 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8601 lun->pr_key_count) { 8602 /* 8603 * If the reservation is a registrants 8604 * only type we need to generate a UA 8605 * for other registered inits. The 8606 * sense code should be RESERVATIONS 8607 * RELEASED 8608 */ 8609 8610 for (i = softc->init_min; i < softc->init_max; i++) { 8611 if (ctl_get_prkey(lun, i) == 0) 8612 continue; 8613 8614 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8615 } 8616 } 8617 lun->pr_res_type = 0; 8618 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8619 if (lun->pr_key_count==0) { 8620 lun->flags &= ~CTL_LUN_PR_RESERVED; 8621 lun->pr_res_type = 0; 8622 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8623 } 8624 } 8625 lun->pr_generation++; 8626 break; 8627 8628 case CTL_PR_RESERVE: 8629 lun->flags |= CTL_LUN_PR_RESERVED; 8630 lun->pr_res_type = msg->pr.pr_info.res_type; 8631 lun->pr_res_idx = msg->pr.pr_info.residx; 8632 8633 break; 8634 8635 case CTL_PR_RELEASE: 8636 /* 8637 * If this isn't an exclusive access reservation and NUAR 8638 * is not set, generate UA for all other registrants. 8639 */ 8640 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8641 lun->pr_res_type != SPR_TYPE_WR_EX && 8642 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8643 for (i = softc->init_min; i < softc->init_max; i++) 8644 if (i == residx || ctl_get_prkey(lun, i) == 0) 8645 continue; 8646 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8647 } 8648 8649 lun->flags &= ~CTL_LUN_PR_RESERVED; 8650 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8651 lun->pr_res_type = 0; 8652 break; 8653 8654 case CTL_PR_PREEMPT: 8655 ctl_pro_preempt_other(lun, msg); 8656 break; 8657 case CTL_PR_CLEAR: 8658 lun->flags &= ~CTL_LUN_PR_RESERVED; 8659 lun->pr_res_type = 0; 8660 lun->pr_key_count = 0; 8661 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8662 8663 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8664 if (ctl_get_prkey(lun, i) == 0) 8665 continue; 8666 ctl_clr_prkey(lun, i); 8667 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8668 } 8669 lun->pr_generation++; 8670 break; 8671 } 8672 8673 mtx_unlock(&lun->lun_lock); 8674} 8675 8676int 8677ctl_read_write(struct ctl_scsiio *ctsio) 8678{ 8679 struct ctl_lun *lun = CTL_LUN(ctsio); 8680 struct ctl_lba_len_flags *lbalen; 8681 uint64_t lba; 8682 uint32_t num_blocks; 8683 int flags, retval; 8684 int isread; 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_lun *lun = CTL_LUN(io); 8871 struct ctl_scsiio *ctsio; 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 lbalen = (struct ctl_lba_len_flags *) 8879 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8880 lbalen->flags &= ~CTL_LLF_COMPARE; 8881 lbalen->flags |= CTL_LLF_WRITE; 8882 8883 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8884 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8885 return (retval); 8886} 8887 8888int 8889ctl_cnw(struct ctl_scsiio *ctsio) 8890{ 8891 struct ctl_lun *lun = CTL_LUN(ctsio); 8892 struct ctl_lba_len_flags *lbalen; 8893 uint64_t lba; 8894 uint32_t num_blocks; 8895 int flags, retval; 8896 8897 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8898 8899 flags = 0; 8900 switch (ctsio->cdb[0]) { 8901 case COMPARE_AND_WRITE: { 8902 struct scsi_compare_and_write *cdb; 8903 8904 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8905 if (cdb->byte2 & SRW10_FUA) 8906 flags |= CTL_LLF_FUA; 8907 if (cdb->byte2 & SRW10_DPO) 8908 flags |= CTL_LLF_DPO; 8909 lba = scsi_8btou64(cdb->addr); 8910 num_blocks = cdb->length; 8911 break; 8912 } 8913 default: 8914 /* 8915 * We got a command we don't support. This shouldn't 8916 * happen, commands should be filtered out above us. 8917 */ 8918 ctl_set_invalid_opcode(ctsio); 8919 ctl_done((union ctl_io *)ctsio); 8920 8921 return (CTL_RETVAL_COMPLETE); 8922 break; /* NOTREACHED */ 8923 } 8924 8925 /* 8926 * The first check is to make sure we're in bounds, the second 8927 * check is to catch wrap-around problems. If the lba + num blocks 8928 * is less than the lba, then we've wrapped around and the block 8929 * range is invalid anyway. 8930 */ 8931 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8932 || ((lba + num_blocks) < lba)) { 8933 ctl_set_lba_out_of_range(ctsio, 8934 MAX(lba, lun->be_lun->maxlba + 1)); 8935 ctl_done((union ctl_io *)ctsio); 8936 return (CTL_RETVAL_COMPLETE); 8937 } 8938 8939 /* 8940 * According to SBC-3, a transfer length of 0 is not an error. 8941 */ 8942 if (num_blocks == 0) { 8943 ctl_set_success(ctsio); 8944 ctl_done((union ctl_io *)ctsio); 8945 return (CTL_RETVAL_COMPLETE); 8946 } 8947 8948 /* Set FUA if write cache is disabled. */ 8949 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8950 flags |= CTL_LLF_FUA; 8951 8952 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8953 ctsio->kern_rel_offset = 0; 8954 8955 /* 8956 * Set the IO_CONT flag, so that if this I/O gets passed to 8957 * ctl_data_submit_done(), it'll get passed back to 8958 * ctl_ctl_cnw_cont() for further processing. 8959 */ 8960 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8961 ctsio->io_cont = ctl_cnw_cont; 8962 8963 lbalen = (struct ctl_lba_len_flags *) 8964 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8965 lbalen->lba = lba; 8966 lbalen->len = num_blocks; 8967 lbalen->flags = CTL_LLF_COMPARE | flags; 8968 8969 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8970 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8971 return (retval); 8972} 8973 8974int 8975ctl_verify(struct ctl_scsiio *ctsio) 8976{ 8977 struct ctl_lun *lun = CTL_LUN(ctsio); 8978 struct ctl_lba_len_flags *lbalen; 8979 uint64_t lba; 8980 uint32_t num_blocks; 8981 int bytchk, flags; 8982 int retval; 8983 8984 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8985 8986 bytchk = 0; 8987 flags = CTL_LLF_FUA; 8988 switch (ctsio->cdb[0]) { 8989 case VERIFY_10: { 8990 struct scsi_verify_10 *cdb; 8991 8992 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8993 if (cdb->byte2 & SVFY_BYTCHK) 8994 bytchk = 1; 8995 if (cdb->byte2 & SVFY_DPO) 8996 flags |= CTL_LLF_DPO; 8997 lba = scsi_4btoul(cdb->addr); 8998 num_blocks = scsi_2btoul(cdb->length); 8999 break; 9000 } 9001 case VERIFY_12: { 9002 struct scsi_verify_12 *cdb; 9003 9004 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9005 if (cdb->byte2 & SVFY_BYTCHK) 9006 bytchk = 1; 9007 if (cdb->byte2 & SVFY_DPO) 9008 flags |= CTL_LLF_DPO; 9009 lba = scsi_4btoul(cdb->addr); 9010 num_blocks = scsi_4btoul(cdb->length); 9011 break; 9012 } 9013 case VERIFY_16: { 9014 struct scsi_rw_16 *cdb; 9015 9016 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9017 if (cdb->byte2 & SVFY_BYTCHK) 9018 bytchk = 1; 9019 if (cdb->byte2 & SVFY_DPO) 9020 flags |= CTL_LLF_DPO; 9021 lba = scsi_8btou64(cdb->addr); 9022 num_blocks = scsi_4btoul(cdb->length); 9023 break; 9024 } 9025 default: 9026 /* 9027 * We got a command we don't support. This shouldn't 9028 * happen, commands should be filtered out above us. 9029 */ 9030 ctl_set_invalid_opcode(ctsio); 9031 ctl_done((union ctl_io *)ctsio); 9032 return (CTL_RETVAL_COMPLETE); 9033 } 9034 9035 /* 9036 * The first check is to make sure we're in bounds, the second 9037 * check is to catch wrap-around problems. If the lba + num blocks 9038 * is less than the lba, then we've wrapped around and the block 9039 * range is invalid anyway. 9040 */ 9041 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9042 || ((lba + num_blocks) < lba)) { 9043 ctl_set_lba_out_of_range(ctsio, 9044 MAX(lba, lun->be_lun->maxlba + 1)); 9045 ctl_done((union ctl_io *)ctsio); 9046 return (CTL_RETVAL_COMPLETE); 9047 } 9048 9049 /* 9050 * According to SBC-3, a transfer length of 0 is not an error. 9051 */ 9052 if (num_blocks == 0) { 9053 ctl_set_success(ctsio); 9054 ctl_done((union ctl_io *)ctsio); 9055 return (CTL_RETVAL_COMPLETE); 9056 } 9057 9058 lbalen = (struct ctl_lba_len_flags *) 9059 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9060 lbalen->lba = lba; 9061 lbalen->len = num_blocks; 9062 if (bytchk) { 9063 lbalen->flags = CTL_LLF_COMPARE | flags; 9064 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9065 } else { 9066 lbalen->flags = CTL_LLF_VERIFY | flags; 9067 ctsio->kern_total_len = 0; 9068 } 9069 ctsio->kern_rel_offset = 0; 9070 9071 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9072 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9073 return (retval); 9074} 9075 9076int 9077ctl_report_luns(struct ctl_scsiio *ctsio) 9078{ 9079 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9080 struct ctl_port *port = CTL_PORT(ctsio); 9081 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9082 struct scsi_report_luns *cdb; 9083 struct scsi_report_luns_data *lun_data; 9084 int num_filled, num_luns, num_port_luns, retval; 9085 uint32_t alloc_len, lun_datalen; 9086 uint32_t initidx, targ_lun_id, lun_id; 9087 9088 retval = CTL_RETVAL_COMPLETE; 9089 cdb = (struct scsi_report_luns *)ctsio->cdb; 9090 9091 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9092 9093 num_luns = 0; 9094 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS; 9095 mtx_lock(&softc->ctl_lock); 9096 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9097 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9098 num_luns++; 9099 } 9100 mtx_unlock(&softc->ctl_lock); 9101 9102 switch (cdb->select_report) { 9103 case RPL_REPORT_DEFAULT: 9104 case RPL_REPORT_ALL: 9105 case RPL_REPORT_NONSUBSID: 9106 break; 9107 case RPL_REPORT_WELLKNOWN: 9108 case RPL_REPORT_ADMIN: 9109 case RPL_REPORT_CONGLOM: 9110 num_luns = 0; 9111 break; 9112 default: 9113 ctl_set_invalid_field(ctsio, 9114 /*sks_valid*/ 1, 9115 /*command*/ 1, 9116 /*field*/ 2, 9117 /*bit_valid*/ 0, 9118 /*bit*/ 0); 9119 ctl_done((union ctl_io *)ctsio); 9120 return (retval); 9121 break; /* NOTREACHED */ 9122 } 9123 9124 alloc_len = scsi_4btoul(cdb->length); 9125 /* 9126 * The initiator has to allocate at least 16 bytes for this request, 9127 * so he can at least get the header and the first LUN. Otherwise 9128 * we reject the request (per SPC-3 rev 14, section 6.21). 9129 */ 9130 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9131 sizeof(struct scsi_report_luns_lundata))) { 9132 ctl_set_invalid_field(ctsio, 9133 /*sks_valid*/ 1, 9134 /*command*/ 1, 9135 /*field*/ 6, 9136 /*bit_valid*/ 0, 9137 /*bit*/ 0); 9138 ctl_done((union ctl_io *)ctsio); 9139 return (retval); 9140 } 9141 9142 lun_datalen = sizeof(*lun_data) + 9143 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9144 9145 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9146 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9147 ctsio->kern_sg_entries = 0; 9148 9149 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9150 9151 mtx_lock(&softc->ctl_lock); 9152 for (targ_lun_id = 0, num_filled = 0; 9153 targ_lun_id < num_port_luns && num_filled < num_luns; 9154 targ_lun_id++) { 9155 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9156 if (lun_id == UINT32_MAX) 9157 continue; 9158 lun = softc->ctl_luns[lun_id]; 9159 if (lun == NULL) 9160 continue; 9161 9162 be64enc(lun_data->luns[num_filled++].lundata, 9163 ctl_encode_lun(targ_lun_id)); 9164 9165 /* 9166 * According to SPC-3, rev 14 section 6.21: 9167 * 9168 * "The execution of a REPORT LUNS command to any valid and 9169 * installed logical unit shall clear the REPORTED LUNS DATA 9170 * HAS CHANGED unit attention condition for all logical 9171 * units of that target with respect to the requesting 9172 * initiator. A valid and installed logical unit is one 9173 * having a PERIPHERAL QUALIFIER of 000b in the standard 9174 * INQUIRY data (see 6.4.2)." 9175 * 9176 * If request_lun is NULL, the LUN this report luns command 9177 * was issued to is either disabled or doesn't exist. In that 9178 * case, we shouldn't clear any pending lun change unit 9179 * attention. 9180 */ 9181 if (request_lun != NULL) { 9182 mtx_lock(&lun->lun_lock); 9183 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9184 mtx_unlock(&lun->lun_lock); 9185 } 9186 } 9187 mtx_unlock(&softc->ctl_lock); 9188 9189 /* 9190 * It's quite possible that we've returned fewer LUNs than we allocated 9191 * space for. Trim it. 9192 */ 9193 lun_datalen = sizeof(*lun_data) + 9194 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9195 9196 if (lun_datalen < alloc_len) { 9197 ctsio->residual = alloc_len - lun_datalen; 9198 ctsio->kern_data_len = lun_datalen; 9199 ctsio->kern_total_len = lun_datalen; 9200 } else { 9201 ctsio->residual = 0; 9202 ctsio->kern_data_len = alloc_len; 9203 ctsio->kern_total_len = alloc_len; 9204 } 9205 ctsio->kern_data_resid = 0; 9206 ctsio->kern_rel_offset = 0; 9207 ctsio->kern_sg_entries = 0; 9208 9209 /* 9210 * We set this to the actual data length, regardless of how much 9211 * space we actually have to return results. If the user looks at 9212 * this value, he'll know whether or not he allocated enough space 9213 * and reissue the command if necessary. We don't support well 9214 * known logical units, so if the user asks for that, return none. 9215 */ 9216 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9217 9218 /* 9219 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9220 * this request. 9221 */ 9222 ctl_set_success(ctsio); 9223 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9224 ctsio->be_move_done = ctl_config_move_done; 9225 ctl_datamove((union ctl_io *)ctsio); 9226 return (retval); 9227} 9228 9229int 9230ctl_request_sense(struct ctl_scsiio *ctsio) 9231{ 9232 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9233 struct ctl_lun *lun = CTL_LUN(ctsio); 9234 struct scsi_request_sense *cdb; 9235 struct scsi_sense_data *sense_ptr; 9236 uint32_t initidx; 9237 int have_error; 9238 u_int sense_len = SSD_FULL_SIZE; 9239 scsi_sense_data_type sense_format; 9240 ctl_ua_type ua_type; 9241 uint8_t asc = 0, ascq = 0; 9242 9243 cdb = (struct scsi_request_sense *)ctsio->cdb; 9244 9245 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9246 9247 /* 9248 * Determine which sense format the user wants. 9249 */ 9250 if (cdb->byte2 & SRS_DESC) 9251 sense_format = SSD_TYPE_DESC; 9252 else 9253 sense_format = SSD_TYPE_FIXED; 9254 9255 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9256 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9257 ctsio->kern_sg_entries = 0; 9258 9259 /* 9260 * struct scsi_sense_data, which is currently set to 256 bytes, is 9261 * larger than the largest allowed value for the length field in the 9262 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9263 */ 9264 ctsio->residual = 0; 9265 ctsio->kern_data_len = cdb->length; 9266 ctsio->kern_total_len = cdb->length; 9267 9268 ctsio->kern_data_resid = 0; 9269 ctsio->kern_rel_offset = 0; 9270 ctsio->kern_sg_entries = 0; 9271 9272 /* 9273 * If we don't have a LUN, we don't have any pending sense. 9274 */ 9275 if (lun == NULL || 9276 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9277 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9278 /* "Logical unit not supported" */ 9279 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9280 /*current_error*/ 1, 9281 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9282 /*asc*/ 0x25, 9283 /*ascq*/ 0x00, 9284 SSD_ELEM_NONE); 9285 goto send; 9286 } 9287 9288 have_error = 0; 9289 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9290 /* 9291 * Check for pending sense, and then for pending unit attentions. 9292 * Pending sense gets returned first, then pending unit attentions. 9293 */ 9294 mtx_lock(&lun->lun_lock); 9295#ifdef CTL_WITH_CA 9296 if (ctl_is_set(lun->have_ca, initidx)) { 9297 scsi_sense_data_type stored_format; 9298 9299 /* 9300 * Check to see which sense format was used for the stored 9301 * sense data. 9302 */ 9303 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9304 9305 /* 9306 * If the user requested a different sense format than the 9307 * one we stored, then we need to convert it to the other 9308 * format. If we're going from descriptor to fixed format 9309 * sense data, we may lose things in translation, depending 9310 * on what options were used. 9311 * 9312 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9313 * for some reason we'll just copy it out as-is. 9314 */ 9315 if ((stored_format == SSD_TYPE_FIXED) 9316 && (sense_format == SSD_TYPE_DESC)) 9317 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9318 &lun->pending_sense[initidx], 9319 (struct scsi_sense_data_desc *)sense_ptr); 9320 else if ((stored_format == SSD_TYPE_DESC) 9321 && (sense_format == SSD_TYPE_FIXED)) 9322 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9323 &lun->pending_sense[initidx], 9324 (struct scsi_sense_data_fixed *)sense_ptr); 9325 else 9326 memcpy(sense_ptr, &lun->pending_sense[initidx], 9327 MIN(sizeof(*sense_ptr), 9328 sizeof(lun->pending_sense[initidx]))); 9329 9330 ctl_clear_mask(lun->have_ca, initidx); 9331 have_error = 1; 9332 } else 9333#endif 9334 if (have_error == 0) { 9335 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9336 sense_format); 9337 if (ua_type != CTL_UA_NONE) 9338 have_error = 1; 9339 } 9340 if (have_error == 0) { 9341 /* 9342 * Report informational exception if have one and allowed. 9343 */ 9344 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9345 asc = lun->ie_asc; 9346 ascq = lun->ie_ascq; 9347 } 9348 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9349 /*current_error*/ 1, 9350 /*sense_key*/ SSD_KEY_NO_SENSE, 9351 /*asc*/ asc, 9352 /*ascq*/ ascq, 9353 SSD_ELEM_NONE); 9354 } 9355 mtx_unlock(&lun->lun_lock); 9356 9357send: 9358 /* 9359 * We report the SCSI status as OK, since the status of the command 9360 * itself is OK. We're reporting sense as parameter data. 9361 */ 9362 ctl_set_success(ctsio); 9363 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9364 ctsio->be_move_done = ctl_config_move_done; 9365 ctl_datamove((union ctl_io *)ctsio); 9366 return (CTL_RETVAL_COMPLETE); 9367} 9368 9369int 9370ctl_tur(struct ctl_scsiio *ctsio) 9371{ 9372 9373 CTL_DEBUG_PRINT(("ctl_tur\n")); 9374 9375 ctl_set_success(ctsio); 9376 ctl_done((union ctl_io *)ctsio); 9377 9378 return (CTL_RETVAL_COMPLETE); 9379} 9380 9381/* 9382 * SCSI VPD page 0x00, the Supported VPD Pages page. 9383 */ 9384static int 9385ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9386{ 9387 struct ctl_lun *lun = CTL_LUN(ctsio); 9388 struct scsi_vpd_supported_pages *pages; 9389 int sup_page_size; 9390 int p; 9391 9392 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9393 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9394 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9395 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9396 ctsio->kern_sg_entries = 0; 9397 9398 if (sup_page_size < alloc_len) { 9399 ctsio->residual = alloc_len - sup_page_size; 9400 ctsio->kern_data_len = sup_page_size; 9401 ctsio->kern_total_len = sup_page_size; 9402 } else { 9403 ctsio->residual = 0; 9404 ctsio->kern_data_len = alloc_len; 9405 ctsio->kern_total_len = alloc_len; 9406 } 9407 ctsio->kern_data_resid = 0; 9408 ctsio->kern_rel_offset = 0; 9409 ctsio->kern_sg_entries = 0; 9410 9411 /* 9412 * The control device is always connected. The disk device, on the 9413 * other hand, may not be online all the time. Need to change this 9414 * to figure out whether the disk device is actually online or not. 9415 */ 9416 if (lun != NULL) 9417 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9418 lun->be_lun->lun_type; 9419 else 9420 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9421 9422 p = 0; 9423 /* Supported VPD pages */ 9424 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9425 /* Serial Number */ 9426 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9427 /* Device Identification */ 9428 pages->page_list[p++] = SVPD_DEVICE_ID; 9429 /* Extended INQUIRY Data */ 9430 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9431 /* Mode Page Policy */ 9432 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9433 /* SCSI Ports */ 9434 pages->page_list[p++] = SVPD_SCSI_PORTS; 9435 /* Third-party Copy */ 9436 pages->page_list[p++] = SVPD_SCSI_TPC; 9437 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9438 /* Block limits */ 9439 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9440 /* Block Device Characteristics */ 9441 pages->page_list[p++] = SVPD_BDC; 9442 /* Logical Block Provisioning */ 9443 pages->page_list[p++] = SVPD_LBP; 9444 } 9445 pages->length = p; 9446 9447 ctl_set_success(ctsio); 9448 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9449 ctsio->be_move_done = ctl_config_move_done; 9450 ctl_datamove((union ctl_io *)ctsio); 9451 return (CTL_RETVAL_COMPLETE); 9452} 9453 9454/* 9455 * SCSI VPD page 0x80, the Unit Serial Number page. 9456 */ 9457static int 9458ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9459{ 9460 struct ctl_lun *lun = CTL_LUN(ctsio); 9461 struct scsi_vpd_unit_serial_number *sn_ptr; 9462 int data_len; 9463 9464 data_len = 4 + CTL_SN_LEN; 9465 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9466 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9467 if (data_len < alloc_len) { 9468 ctsio->residual = alloc_len - data_len; 9469 ctsio->kern_data_len = data_len; 9470 ctsio->kern_total_len = data_len; 9471 } else { 9472 ctsio->residual = 0; 9473 ctsio->kern_data_len = alloc_len; 9474 ctsio->kern_total_len = alloc_len; 9475 } 9476 ctsio->kern_data_resid = 0; 9477 ctsio->kern_rel_offset = 0; 9478 ctsio->kern_sg_entries = 0; 9479 9480 /* 9481 * The control device is always connected. The disk device, on the 9482 * other hand, may not be online all the time. Need to change this 9483 * to figure out whether the disk device is actually online or not. 9484 */ 9485 if (lun != NULL) 9486 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9487 lun->be_lun->lun_type; 9488 else 9489 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9490 9491 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9492 sn_ptr->length = CTL_SN_LEN; 9493 /* 9494 * If we don't have a LUN, we just leave the serial number as 9495 * all spaces. 9496 */ 9497 if (lun != NULL) { 9498 strncpy((char *)sn_ptr->serial_num, 9499 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9500 } else 9501 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9502 9503 ctl_set_success(ctsio); 9504 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9505 ctsio->be_move_done = ctl_config_move_done; 9506 ctl_datamove((union ctl_io *)ctsio); 9507 return (CTL_RETVAL_COMPLETE); 9508} 9509 9510 9511/* 9512 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9513 */ 9514static int 9515ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9516{ 9517 struct ctl_lun *lun = CTL_LUN(ctsio); 9518 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9519 int data_len; 9520 9521 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9522 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9523 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9524 ctsio->kern_sg_entries = 0; 9525 9526 if (data_len < alloc_len) { 9527 ctsio->residual = alloc_len - data_len; 9528 ctsio->kern_data_len = data_len; 9529 ctsio->kern_total_len = data_len; 9530 } else { 9531 ctsio->residual = 0; 9532 ctsio->kern_data_len = alloc_len; 9533 ctsio->kern_total_len = alloc_len; 9534 } 9535 ctsio->kern_data_resid = 0; 9536 ctsio->kern_rel_offset = 0; 9537 ctsio->kern_sg_entries = 0; 9538 9539 /* 9540 * The control device is always connected. The disk device, on the 9541 * other hand, may not be online all the time. 9542 */ 9543 if (lun != NULL) 9544 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9545 lun->be_lun->lun_type; 9546 else 9547 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9548 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9549 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9550 /* 9551 * We support head of queue, ordered and simple tags. 9552 */ 9553 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9554 /* 9555 * Volatile cache supported. 9556 */ 9557 eid_ptr->flags3 = SVPD_EID_V_SUP; 9558 9559 /* 9560 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9561 * attention for a particular IT nexus on all LUNs once we report 9562 * it to that nexus once. This bit is required as of SPC-4. 9563 */ 9564 eid_ptr->flags4 = SVPD_EID_LUICLR; 9565 9566 /* 9567 * We support revert to defaults (RTD) bit in MODE SELECT. 9568 */ 9569 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9570 9571 /* 9572 * XXX KDM in order to correctly answer this, we would need 9573 * information from the SIM to determine how much sense data it 9574 * can send. So this would really be a path inquiry field, most 9575 * likely. This can be set to a maximum of 252 according to SPC-4, 9576 * but the hardware may or may not be able to support that much. 9577 * 0 just means that the maximum sense data length is not reported. 9578 */ 9579 eid_ptr->max_sense_length = 0; 9580 9581 ctl_set_success(ctsio); 9582 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9583 ctsio->be_move_done = ctl_config_move_done; 9584 ctl_datamove((union ctl_io *)ctsio); 9585 return (CTL_RETVAL_COMPLETE); 9586} 9587 9588static int 9589ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9590{ 9591 struct ctl_lun *lun = CTL_LUN(ctsio); 9592 struct scsi_vpd_mode_page_policy *mpp_ptr; 9593 int data_len; 9594 9595 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9596 sizeof(struct scsi_vpd_mode_page_policy_descr); 9597 9598 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9599 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9600 ctsio->kern_sg_entries = 0; 9601 9602 if (data_len < alloc_len) { 9603 ctsio->residual = alloc_len - data_len; 9604 ctsio->kern_data_len = data_len; 9605 ctsio->kern_total_len = data_len; 9606 } else { 9607 ctsio->residual = 0; 9608 ctsio->kern_data_len = alloc_len; 9609 ctsio->kern_total_len = alloc_len; 9610 } 9611 ctsio->kern_data_resid = 0; 9612 ctsio->kern_rel_offset = 0; 9613 ctsio->kern_sg_entries = 0; 9614 9615 /* 9616 * The control device is always connected. The disk device, on the 9617 * other hand, may not be online all the time. 9618 */ 9619 if (lun != NULL) 9620 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9621 lun->be_lun->lun_type; 9622 else 9623 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9624 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9625 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9626 mpp_ptr->descr[0].page_code = 0x3f; 9627 mpp_ptr->descr[0].subpage_code = 0xff; 9628 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9629 9630 ctl_set_success(ctsio); 9631 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9632 ctsio->be_move_done = ctl_config_move_done; 9633 ctl_datamove((union ctl_io *)ctsio); 9634 return (CTL_RETVAL_COMPLETE); 9635} 9636 9637/* 9638 * SCSI VPD page 0x83, the Device Identification page. 9639 */ 9640static int 9641ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9642{ 9643 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9644 struct ctl_port *port = CTL_PORT(ctsio); 9645 struct ctl_lun *lun = CTL_LUN(ctsio); 9646 struct scsi_vpd_device_id *devid_ptr; 9647 struct scsi_vpd_id_descriptor *desc; 9648 int data_len, g; 9649 uint8_t proto; 9650 9651 data_len = sizeof(struct scsi_vpd_device_id) + 9652 sizeof(struct scsi_vpd_id_descriptor) + 9653 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9654 sizeof(struct scsi_vpd_id_descriptor) + 9655 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9656 if (lun && lun->lun_devid) 9657 data_len += lun->lun_devid->len; 9658 if (port && port->port_devid) 9659 data_len += port->port_devid->len; 9660 if (port && port->target_devid) 9661 data_len += port->target_devid->len; 9662 9663 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9664 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9665 ctsio->kern_sg_entries = 0; 9666 9667 if (data_len < alloc_len) { 9668 ctsio->residual = alloc_len - data_len; 9669 ctsio->kern_data_len = data_len; 9670 ctsio->kern_total_len = data_len; 9671 } else { 9672 ctsio->residual = 0; 9673 ctsio->kern_data_len = alloc_len; 9674 ctsio->kern_total_len = alloc_len; 9675 } 9676 ctsio->kern_data_resid = 0; 9677 ctsio->kern_rel_offset = 0; 9678 ctsio->kern_sg_entries = 0; 9679 9680 /* 9681 * The control device is always connected. The disk device, on the 9682 * other hand, may not be online all the time. 9683 */ 9684 if (lun != NULL) 9685 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9686 lun->be_lun->lun_type; 9687 else 9688 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9689 devid_ptr->page_code = SVPD_DEVICE_ID; 9690 scsi_ulto2b(data_len - 4, devid_ptr->length); 9691 9692 if (port && port->port_type == CTL_PORT_FC) 9693 proto = SCSI_PROTO_FC << 4; 9694 else if (port && port->port_type == CTL_PORT_ISCSI) 9695 proto = SCSI_PROTO_ISCSI << 4; 9696 else 9697 proto = SCSI_PROTO_SPI << 4; 9698 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9699 9700 /* 9701 * We're using a LUN association here. i.e., this device ID is a 9702 * per-LUN identifier. 9703 */ 9704 if (lun && lun->lun_devid) { 9705 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9706 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9707 lun->lun_devid->len); 9708 } 9709 9710 /* 9711 * This is for the WWPN which is a port association. 9712 */ 9713 if (port && port->port_devid) { 9714 memcpy(desc, port->port_devid->data, port->port_devid->len); 9715 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9716 port->port_devid->len); 9717 } 9718 9719 /* 9720 * This is for the Relative Target Port(type 4h) identifier 9721 */ 9722 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9723 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9724 SVPD_ID_TYPE_RELTARG; 9725 desc->length = 4; 9726 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9727 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9728 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9729 9730 /* 9731 * This is for the Target Port Group(type 5h) identifier 9732 */ 9733 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9734 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9735 SVPD_ID_TYPE_TPORTGRP; 9736 desc->length = 4; 9737 if (softc->is_single || 9738 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9739 g = 1; 9740 else 9741 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9742 scsi_ulto2b(g, &desc->identifier[2]); 9743 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9744 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9745 9746 /* 9747 * This is for the Target identifier 9748 */ 9749 if (port && port->target_devid) { 9750 memcpy(desc, port->target_devid->data, port->target_devid->len); 9751 } 9752 9753 ctl_set_success(ctsio); 9754 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9755 ctsio->be_move_done = ctl_config_move_done; 9756 ctl_datamove((union ctl_io *)ctsio); 9757 return (CTL_RETVAL_COMPLETE); 9758} 9759 9760static int 9761ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9762{ 9763 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9764 struct ctl_lun *lun = CTL_LUN(ctsio); 9765 struct scsi_vpd_scsi_ports *sp; 9766 struct scsi_vpd_port_designation *pd; 9767 struct scsi_vpd_port_designation_cont *pdc; 9768 struct ctl_port *port; 9769 int data_len, num_target_ports, iid_len, id_len; 9770 9771 num_target_ports = 0; 9772 iid_len = 0; 9773 id_len = 0; 9774 mtx_lock(&softc->ctl_lock); 9775 STAILQ_FOREACH(port, &softc->port_list, links) { 9776 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9777 continue; 9778 if (lun != NULL && 9779 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9780 continue; 9781 num_target_ports++; 9782 if (port->init_devid) 9783 iid_len += port->init_devid->len; 9784 if (port->port_devid) 9785 id_len += port->port_devid->len; 9786 } 9787 mtx_unlock(&softc->ctl_lock); 9788 9789 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9790 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9791 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9792 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9793 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9794 ctsio->kern_sg_entries = 0; 9795 9796 if (data_len < alloc_len) { 9797 ctsio->residual = alloc_len - data_len; 9798 ctsio->kern_data_len = data_len; 9799 ctsio->kern_total_len = data_len; 9800 } else { 9801 ctsio->residual = 0; 9802 ctsio->kern_data_len = alloc_len; 9803 ctsio->kern_total_len = alloc_len; 9804 } 9805 ctsio->kern_data_resid = 0; 9806 ctsio->kern_rel_offset = 0; 9807 ctsio->kern_sg_entries = 0; 9808 9809 /* 9810 * The control device is always connected. The disk device, on the 9811 * other hand, may not be online all the time. Need to change this 9812 * to figure out whether the disk device is actually online or not. 9813 */ 9814 if (lun != NULL) 9815 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9816 lun->be_lun->lun_type; 9817 else 9818 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9819 9820 sp->page_code = SVPD_SCSI_PORTS; 9821 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9822 sp->page_length); 9823 pd = &sp->design[0]; 9824 9825 mtx_lock(&softc->ctl_lock); 9826 STAILQ_FOREACH(port, &softc->port_list, links) { 9827 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9828 continue; 9829 if (lun != NULL && 9830 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9831 continue; 9832 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9833 if (port->init_devid) { 9834 iid_len = port->init_devid->len; 9835 memcpy(pd->initiator_transportid, 9836 port->init_devid->data, port->init_devid->len); 9837 } else 9838 iid_len = 0; 9839 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9840 pdc = (struct scsi_vpd_port_designation_cont *) 9841 (&pd->initiator_transportid[iid_len]); 9842 if (port->port_devid) { 9843 id_len = port->port_devid->len; 9844 memcpy(pdc->target_port_descriptors, 9845 port->port_devid->data, port->port_devid->len); 9846 } else 9847 id_len = 0; 9848 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9849 pd = (struct scsi_vpd_port_designation *) 9850 ((uint8_t *)pdc->target_port_descriptors + id_len); 9851 } 9852 mtx_unlock(&softc->ctl_lock); 9853 9854 ctl_set_success(ctsio); 9855 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9856 ctsio->be_move_done = ctl_config_move_done; 9857 ctl_datamove((union ctl_io *)ctsio); 9858 return (CTL_RETVAL_COMPLETE); 9859} 9860 9861static int 9862ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9863{ 9864 struct ctl_lun *lun = CTL_LUN(ctsio); 9865 struct scsi_vpd_block_limits *bl_ptr; 9866 uint64_t ival; 9867 9868 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9869 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9870 ctsio->kern_sg_entries = 0; 9871 9872 if (sizeof(*bl_ptr) < alloc_len) { 9873 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9874 ctsio->kern_data_len = sizeof(*bl_ptr); 9875 ctsio->kern_total_len = sizeof(*bl_ptr); 9876 } else { 9877 ctsio->residual = 0; 9878 ctsio->kern_data_len = alloc_len; 9879 ctsio->kern_total_len = alloc_len; 9880 } 9881 ctsio->kern_data_resid = 0; 9882 ctsio->kern_rel_offset = 0; 9883 ctsio->kern_sg_entries = 0; 9884 9885 /* 9886 * The control device is always connected. The disk device, on the 9887 * other hand, may not be online all the time. Need to change this 9888 * to figure out whether the disk device is actually online or not. 9889 */ 9890 if (lun != NULL) 9891 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9892 lun->be_lun->lun_type; 9893 else 9894 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9895 9896 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9897 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9898 bl_ptr->max_cmp_write_len = 0xff; 9899 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9900 if (lun != NULL) { 9901 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9902 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9903 ival = 0xffffffff; 9904 ctl_get_opt_number(&lun->be_lun->options, 9905 "unmap_max_lba", &ival); 9906 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9907 ival = 0xffffffff; 9908 ctl_get_opt_number(&lun->be_lun->options, 9909 "unmap_max_descr", &ival); 9910 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9911 if (lun->be_lun->ublockexp != 0) { 9912 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9913 bl_ptr->opt_unmap_grain); 9914 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9915 bl_ptr->unmap_grain_align); 9916 } 9917 } 9918 scsi_ulto4b(lun->be_lun->atomicblock, 9919 bl_ptr->max_atomic_transfer_length); 9920 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9921 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9922 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9923 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9924 ival = UINT64_MAX; 9925 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival); 9926 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9927 } 9928 9929 ctl_set_success(ctsio); 9930 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9931 ctsio->be_move_done = ctl_config_move_done; 9932 ctl_datamove((union ctl_io *)ctsio); 9933 return (CTL_RETVAL_COMPLETE); 9934} 9935 9936static int 9937ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9938{ 9939 struct ctl_lun *lun = CTL_LUN(ctsio); 9940 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9941 const char *value; 9942 u_int i; 9943 9944 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9945 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9946 ctsio->kern_sg_entries = 0; 9947 9948 if (sizeof(*bdc_ptr) < alloc_len) { 9949 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9950 ctsio->kern_data_len = sizeof(*bdc_ptr); 9951 ctsio->kern_total_len = sizeof(*bdc_ptr); 9952 } else { 9953 ctsio->residual = 0; 9954 ctsio->kern_data_len = alloc_len; 9955 ctsio->kern_total_len = alloc_len; 9956 } 9957 ctsio->kern_data_resid = 0; 9958 ctsio->kern_rel_offset = 0; 9959 ctsio->kern_sg_entries = 0; 9960 9961 /* 9962 * The control device is always connected. The disk device, on the 9963 * other hand, may not be online all the time. Need to change this 9964 * to figure out whether the disk device is actually online or not. 9965 */ 9966 if (lun != NULL) 9967 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9968 lun->be_lun->lun_type; 9969 else 9970 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9971 bdc_ptr->page_code = SVPD_BDC; 9972 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9973 if (lun != NULL && 9974 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9975 i = strtol(value, NULL, 0); 9976 else 9977 i = CTL_DEFAULT_ROTATION_RATE; 9978 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9979 if (lun != NULL && 9980 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9981 i = strtol(value, NULL, 0); 9982 else 9983 i = 0; 9984 bdc_ptr->wab_wac_ff = (i & 0x0f); 9985 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9986 9987 ctl_set_success(ctsio); 9988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9989 ctsio->be_move_done = ctl_config_move_done; 9990 ctl_datamove((union ctl_io *)ctsio); 9991 return (CTL_RETVAL_COMPLETE); 9992} 9993 9994static int 9995ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9996{ 9997 struct ctl_lun *lun = CTL_LUN(ctsio); 9998 struct scsi_vpd_logical_block_prov *lbp_ptr; 9999 const char *value; 10000 10001 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10002 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10003 ctsio->kern_sg_entries = 0; 10004 10005 if (sizeof(*lbp_ptr) < alloc_len) { 10006 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10007 ctsio->kern_data_len = sizeof(*lbp_ptr); 10008 ctsio->kern_total_len = sizeof(*lbp_ptr); 10009 } else { 10010 ctsio->residual = 0; 10011 ctsio->kern_data_len = alloc_len; 10012 ctsio->kern_total_len = alloc_len; 10013 } 10014 ctsio->kern_data_resid = 0; 10015 ctsio->kern_rel_offset = 0; 10016 ctsio->kern_sg_entries = 0; 10017 10018 /* 10019 * The control device is always connected. The disk device, on the 10020 * other hand, may not be online all the time. Need to change this 10021 * to figure out whether the disk device is actually online or not. 10022 */ 10023 if (lun != NULL) 10024 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10025 lun->be_lun->lun_type; 10026 else 10027 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10028 10029 lbp_ptr->page_code = SVPD_LBP; 10030 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10031 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10032 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10033 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10034 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10035 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type"); 10036 if (value != NULL) { 10037 if (strcmp(value, "resource") == 0) 10038 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10039 else if (strcmp(value, "thin") == 0) 10040 lbp_ptr->prov_type = SVPD_LBP_THIN; 10041 } else 10042 lbp_ptr->prov_type = SVPD_LBP_THIN; 10043 } 10044 10045 ctl_set_success(ctsio); 10046 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10047 ctsio->be_move_done = ctl_config_move_done; 10048 ctl_datamove((union ctl_io *)ctsio); 10049 return (CTL_RETVAL_COMPLETE); 10050} 10051 10052/* 10053 * INQUIRY with the EVPD bit set. 10054 */ 10055static int 10056ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10057{ 10058 struct ctl_lun *lun = CTL_LUN(ctsio); 10059 struct scsi_inquiry *cdb; 10060 int alloc_len, retval; 10061 10062 cdb = (struct scsi_inquiry *)ctsio->cdb; 10063 alloc_len = scsi_2btoul(cdb->length); 10064 10065 switch (cdb->page_code) { 10066 case SVPD_SUPPORTED_PAGES: 10067 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10068 break; 10069 case SVPD_UNIT_SERIAL_NUMBER: 10070 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10071 break; 10072 case SVPD_DEVICE_ID: 10073 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10074 break; 10075 case SVPD_EXTENDED_INQUIRY_DATA: 10076 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10077 break; 10078 case SVPD_MODE_PAGE_POLICY: 10079 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10080 break; 10081 case SVPD_SCSI_PORTS: 10082 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10083 break; 10084 case SVPD_SCSI_TPC: 10085 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10086 break; 10087 case SVPD_BLOCK_LIMITS: 10088 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10089 goto err; 10090 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10091 break; 10092 case SVPD_BDC: 10093 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10094 goto err; 10095 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10096 break; 10097 case SVPD_LBP: 10098 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10099 goto err; 10100 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10101 break; 10102 default: 10103err: 10104 ctl_set_invalid_field(ctsio, 10105 /*sks_valid*/ 1, 10106 /*command*/ 1, 10107 /*field*/ 2, 10108 /*bit_valid*/ 0, 10109 /*bit*/ 0); 10110 ctl_done((union ctl_io *)ctsio); 10111 retval = CTL_RETVAL_COMPLETE; 10112 break; 10113 } 10114 10115 return (retval); 10116} 10117 10118/* 10119 * Standard INQUIRY data. 10120 */ 10121static int 10122ctl_inquiry_std(struct ctl_scsiio *ctsio) 10123{ 10124 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10125 struct ctl_port *port = CTL_PORT(ctsio); 10126 struct ctl_lun *lun = CTL_LUN(ctsio); 10127 struct scsi_inquiry_data *inq_ptr; 10128 struct scsi_inquiry *cdb; 10129 char *val; 10130 uint32_t alloc_len, data_len; 10131 ctl_port_type port_type; 10132 10133 port_type = port->port_type; 10134 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10135 port_type = CTL_PORT_SCSI; 10136 10137 cdb = (struct scsi_inquiry *)ctsio->cdb; 10138 alloc_len = scsi_2btoul(cdb->length); 10139 10140 /* 10141 * We malloc the full inquiry data size here and fill it 10142 * in. If the user only asks for less, we'll give him 10143 * that much. 10144 */ 10145 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10146 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10147 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10148 ctsio->kern_sg_entries = 0; 10149 ctsio->kern_data_resid = 0; 10150 ctsio->kern_rel_offset = 0; 10151 10152 if (data_len < alloc_len) { 10153 ctsio->residual = alloc_len - data_len; 10154 ctsio->kern_data_len = data_len; 10155 ctsio->kern_total_len = data_len; 10156 } else { 10157 ctsio->residual = 0; 10158 ctsio->kern_data_len = alloc_len; 10159 ctsio->kern_total_len = alloc_len; 10160 } 10161 10162 if (lun != NULL) { 10163 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10164 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10165 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10166 lun->be_lun->lun_type; 10167 } else { 10168 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10169 lun->be_lun->lun_type; 10170 } 10171 if (lun->flags & CTL_LUN_REMOVABLE) 10172 inq_ptr->dev_qual2 |= SID_RMB; 10173 } else 10174 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10175 10176 /* RMB in byte 2 is 0 */ 10177 inq_ptr->version = SCSI_REV_SPC5; 10178 10179 /* 10180 * According to SAM-3, even if a device only supports a single 10181 * level of LUN addressing, it should still set the HISUP bit: 10182 * 10183 * 4.9.1 Logical unit numbers overview 10184 * 10185 * All logical unit number formats described in this standard are 10186 * hierarchical in structure even when only a single level in that 10187 * hierarchy is used. The HISUP bit shall be set to one in the 10188 * standard INQUIRY data (see SPC-2) when any logical unit number 10189 * format described in this standard is used. Non-hierarchical 10190 * formats are outside the scope of this standard. 10191 * 10192 * Therefore we set the HiSup bit here. 10193 * 10194 * The response format is 2, per SPC-3. 10195 */ 10196 inq_ptr->response_format = SID_HiSup | 2; 10197 10198 inq_ptr->additional_length = data_len - 10199 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10200 CTL_DEBUG_PRINT(("additional_length = %d\n", 10201 inq_ptr->additional_length)); 10202 10203 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10204 if (port_type == CTL_PORT_SCSI) 10205 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10206 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10207 inq_ptr->flags = SID_CmdQue; 10208 if (port_type == CTL_PORT_SCSI) 10209 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10210 10211 /* 10212 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10213 * We have 8 bytes for the vendor name, and 16 bytes for the device 10214 * name and 4 bytes for the revision. 10215 */ 10216 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10217 "vendor")) == NULL) { 10218 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10219 } else { 10220 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10221 strncpy(inq_ptr->vendor, val, 10222 min(sizeof(inq_ptr->vendor), strlen(val))); 10223 } 10224 if (lun == NULL) { 10225 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10226 sizeof(inq_ptr->product)); 10227 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10228 switch (lun->be_lun->lun_type) { 10229 case T_DIRECT: 10230 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10231 sizeof(inq_ptr->product)); 10232 break; 10233 case T_PROCESSOR: 10234 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10235 sizeof(inq_ptr->product)); 10236 break; 10237 case T_CDROM: 10238 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10239 sizeof(inq_ptr->product)); 10240 break; 10241 default: 10242 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10243 sizeof(inq_ptr->product)); 10244 break; 10245 } 10246 } else { 10247 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10248 strncpy(inq_ptr->product, val, 10249 min(sizeof(inq_ptr->product), strlen(val))); 10250 } 10251 10252 /* 10253 * XXX make this a macro somewhere so it automatically gets 10254 * incremented when we make changes. 10255 */ 10256 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10257 "revision")) == NULL) { 10258 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10259 } else { 10260 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10261 strncpy(inq_ptr->revision, val, 10262 min(sizeof(inq_ptr->revision), strlen(val))); 10263 } 10264 10265 /* 10266 * For parallel SCSI, we support double transition and single 10267 * transition clocking. We also support QAS (Quick Arbitration 10268 * and Selection) and Information Unit transfers on both the 10269 * control and array devices. 10270 */ 10271 if (port_type == CTL_PORT_SCSI) 10272 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10273 SID_SPI_IUS; 10274 10275 /* SAM-6 (no version claimed) */ 10276 scsi_ulto2b(0x00C0, inq_ptr->version1); 10277 /* SPC-5 (no version claimed) */ 10278 scsi_ulto2b(0x05C0, inq_ptr->version2); 10279 if (port_type == CTL_PORT_FC) { 10280 /* FCP-2 ANSI INCITS.350:2003 */ 10281 scsi_ulto2b(0x0917, inq_ptr->version3); 10282 } else if (port_type == CTL_PORT_SCSI) { 10283 /* SPI-4 ANSI INCITS.362:200x */ 10284 scsi_ulto2b(0x0B56, inq_ptr->version3); 10285 } else if (port_type == CTL_PORT_ISCSI) { 10286 /* iSCSI (no version claimed) */ 10287 scsi_ulto2b(0x0960, inq_ptr->version3); 10288 } else if (port_type == CTL_PORT_SAS) { 10289 /* SAS (no version claimed) */ 10290 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10291 } 10292 10293 if (lun == NULL) { 10294 /* SBC-4 (no version claimed) */ 10295 scsi_ulto2b(0x0600, inq_ptr->version4); 10296 } else { 10297 switch (lun->be_lun->lun_type) { 10298 case T_DIRECT: 10299 /* SBC-4 (no version claimed) */ 10300 scsi_ulto2b(0x0600, inq_ptr->version4); 10301 break; 10302 case T_PROCESSOR: 10303 break; 10304 case T_CDROM: 10305 /* MMC-6 (no version claimed) */ 10306 scsi_ulto2b(0x04E0, inq_ptr->version4); 10307 break; 10308 default: 10309 break; 10310 } 10311 } 10312 10313 ctl_set_success(ctsio); 10314 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10315 ctsio->be_move_done = ctl_config_move_done; 10316 ctl_datamove((union ctl_io *)ctsio); 10317 return (CTL_RETVAL_COMPLETE); 10318} 10319 10320int 10321ctl_inquiry(struct ctl_scsiio *ctsio) 10322{ 10323 struct scsi_inquiry *cdb; 10324 int retval; 10325 10326 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10327 10328 cdb = (struct scsi_inquiry *)ctsio->cdb; 10329 if (cdb->byte2 & SI_EVPD) 10330 retval = ctl_inquiry_evpd(ctsio); 10331 else if (cdb->page_code == 0) 10332 retval = ctl_inquiry_std(ctsio); 10333 else { 10334 ctl_set_invalid_field(ctsio, 10335 /*sks_valid*/ 1, 10336 /*command*/ 1, 10337 /*field*/ 2, 10338 /*bit_valid*/ 0, 10339 /*bit*/ 0); 10340 ctl_done((union ctl_io *)ctsio); 10341 return (CTL_RETVAL_COMPLETE); 10342 } 10343 10344 return (retval); 10345} 10346 10347int 10348ctl_get_config(struct ctl_scsiio *ctsio) 10349{ 10350 struct ctl_lun *lun = CTL_LUN(ctsio); 10351 struct scsi_get_config_header *hdr; 10352 struct scsi_get_config_feature *feature; 10353 struct scsi_get_config *cdb; 10354 uint32_t alloc_len, data_len; 10355 int rt, starting; 10356 10357 cdb = (struct scsi_get_config *)ctsio->cdb; 10358 rt = (cdb->rt & SGC_RT_MASK); 10359 starting = scsi_2btoul(cdb->starting_feature); 10360 alloc_len = scsi_2btoul(cdb->length); 10361 10362 data_len = sizeof(struct scsi_get_config_header) + 10363 sizeof(struct scsi_get_config_feature) + 8 + 10364 sizeof(struct scsi_get_config_feature) + 8 + 10365 sizeof(struct scsi_get_config_feature) + 4 + 10366 sizeof(struct scsi_get_config_feature) + 4 + 10367 sizeof(struct scsi_get_config_feature) + 8 + 10368 sizeof(struct scsi_get_config_feature) + 10369 sizeof(struct scsi_get_config_feature) + 4 + 10370 sizeof(struct scsi_get_config_feature) + 4 + 10371 sizeof(struct scsi_get_config_feature) + 4 + 10372 sizeof(struct scsi_get_config_feature) + 4 + 10373 sizeof(struct scsi_get_config_feature) + 4 + 10374 sizeof(struct scsi_get_config_feature) + 4; 10375 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10376 ctsio->kern_sg_entries = 0; 10377 ctsio->kern_data_resid = 0; 10378 ctsio->kern_rel_offset = 0; 10379 10380 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10381 if (lun->flags & CTL_LUN_NO_MEDIA) 10382 scsi_ulto2b(0x0000, hdr->current_profile); 10383 else 10384 scsi_ulto2b(0x0010, hdr->current_profile); 10385 feature = (struct scsi_get_config_feature *)(hdr + 1); 10386 10387 if (starting > 0x003b) 10388 goto done; 10389 if (starting > 0x003a) 10390 goto f3b; 10391 if (starting > 0x002b) 10392 goto f3a; 10393 if (starting > 0x002a) 10394 goto f2b; 10395 if (starting > 0x001f) 10396 goto f2a; 10397 if (starting > 0x001e) 10398 goto f1f; 10399 if (starting > 0x001d) 10400 goto f1e; 10401 if (starting > 0x0010) 10402 goto f1d; 10403 if (starting > 0x0003) 10404 goto f10; 10405 if (starting > 0x0002) 10406 goto f3; 10407 if (starting > 0x0001) 10408 goto f2; 10409 if (starting > 0x0000) 10410 goto f1; 10411 10412 /* Profile List */ 10413 scsi_ulto2b(0x0000, feature->feature_code); 10414 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10415 feature->add_length = 8; 10416 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10417 feature->feature_data[2] = 0x00; 10418 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10419 feature->feature_data[6] = 0x01; 10420 feature = (struct scsi_get_config_feature *) 10421 &feature->feature_data[feature->add_length]; 10422 10423f1: /* Core */ 10424 scsi_ulto2b(0x0001, feature->feature_code); 10425 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10426 feature->add_length = 8; 10427 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10428 feature->feature_data[4] = 0x03; 10429 feature = (struct scsi_get_config_feature *) 10430 &feature->feature_data[feature->add_length]; 10431 10432f2: /* Morphing */ 10433 scsi_ulto2b(0x0002, feature->feature_code); 10434 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10435 feature->add_length = 4; 10436 feature->feature_data[0] = 0x02; 10437 feature = (struct scsi_get_config_feature *) 10438 &feature->feature_data[feature->add_length]; 10439 10440f3: /* Removable Medium */ 10441 scsi_ulto2b(0x0003, feature->feature_code); 10442 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10443 feature->add_length = 4; 10444 feature->feature_data[0] = 0x39; 10445 feature = (struct scsi_get_config_feature *) 10446 &feature->feature_data[feature->add_length]; 10447 10448 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10449 goto done; 10450 10451f10: /* Random Read */ 10452 scsi_ulto2b(0x0010, feature->feature_code); 10453 feature->flags = 0x00; 10454 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10455 feature->flags |= SGC_F_CURRENT; 10456 feature->add_length = 8; 10457 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10458 scsi_ulto2b(1, &feature->feature_data[4]); 10459 feature->feature_data[6] = 0x00; 10460 feature = (struct scsi_get_config_feature *) 10461 &feature->feature_data[feature->add_length]; 10462 10463f1d: /* Multi-Read */ 10464 scsi_ulto2b(0x001D, feature->feature_code); 10465 feature->flags = 0x00; 10466 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10467 feature->flags |= SGC_F_CURRENT; 10468 feature->add_length = 0; 10469 feature = (struct scsi_get_config_feature *) 10470 &feature->feature_data[feature->add_length]; 10471 10472f1e: /* CD Read */ 10473 scsi_ulto2b(0x001E, feature->feature_code); 10474 feature->flags = 0x00; 10475 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10476 feature->flags |= SGC_F_CURRENT; 10477 feature->add_length = 4; 10478 feature->feature_data[0] = 0x00; 10479 feature = (struct scsi_get_config_feature *) 10480 &feature->feature_data[feature->add_length]; 10481 10482f1f: /* DVD Read */ 10483 scsi_ulto2b(0x001F, feature->feature_code); 10484 feature->flags = 0x08; 10485 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10486 feature->flags |= SGC_F_CURRENT; 10487 feature->add_length = 4; 10488 feature->feature_data[0] = 0x01; 10489 feature->feature_data[2] = 0x03; 10490 feature = (struct scsi_get_config_feature *) 10491 &feature->feature_data[feature->add_length]; 10492 10493f2a: /* DVD+RW */ 10494 scsi_ulto2b(0x002A, feature->feature_code); 10495 feature->flags = 0x04; 10496 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10497 feature->flags |= SGC_F_CURRENT; 10498 feature->add_length = 4; 10499 feature->feature_data[0] = 0x00; 10500 feature->feature_data[1] = 0x00; 10501 feature = (struct scsi_get_config_feature *) 10502 &feature->feature_data[feature->add_length]; 10503 10504f2b: /* DVD+R */ 10505 scsi_ulto2b(0x002B, feature->feature_code); 10506 feature->flags = 0x00; 10507 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10508 feature->flags |= SGC_F_CURRENT; 10509 feature->add_length = 4; 10510 feature->feature_data[0] = 0x00; 10511 feature = (struct scsi_get_config_feature *) 10512 &feature->feature_data[feature->add_length]; 10513 10514f3a: /* DVD+RW Dual Layer */ 10515 scsi_ulto2b(0x003A, feature->feature_code); 10516 feature->flags = 0x00; 10517 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10518 feature->flags |= SGC_F_CURRENT; 10519 feature->add_length = 4; 10520 feature->feature_data[0] = 0x00; 10521 feature->feature_data[1] = 0x00; 10522 feature = (struct scsi_get_config_feature *) 10523 &feature->feature_data[feature->add_length]; 10524 10525f3b: /* DVD+R Dual Layer */ 10526 scsi_ulto2b(0x003B, feature->feature_code); 10527 feature->flags = 0x00; 10528 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10529 feature->flags |= SGC_F_CURRENT; 10530 feature->add_length = 4; 10531 feature->feature_data[0] = 0x00; 10532 feature = (struct scsi_get_config_feature *) 10533 &feature->feature_data[feature->add_length]; 10534 10535done: 10536 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10537 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10538 feature = (struct scsi_get_config_feature *)(hdr + 1); 10539 if (scsi_2btoul(feature->feature_code) == starting) 10540 feature = (struct scsi_get_config_feature *) 10541 &feature->feature_data[feature->add_length]; 10542 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10543 } 10544 scsi_ulto4b(data_len - 4, hdr->data_length); 10545 if (data_len < alloc_len) { 10546 ctsio->residual = alloc_len - data_len; 10547 ctsio->kern_data_len = data_len; 10548 ctsio->kern_total_len = data_len; 10549 } else { 10550 ctsio->residual = 0; 10551 ctsio->kern_data_len = alloc_len; 10552 ctsio->kern_total_len = alloc_len; 10553 } 10554 10555 ctl_set_success(ctsio); 10556 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10557 ctsio->be_move_done = ctl_config_move_done; 10558 ctl_datamove((union ctl_io *)ctsio); 10559 return (CTL_RETVAL_COMPLETE); 10560} 10561 10562int 10563ctl_get_event_status(struct ctl_scsiio *ctsio) 10564{ 10565 struct scsi_get_event_status_header *hdr; 10566 struct scsi_get_event_status *cdb; 10567 uint32_t alloc_len, data_len; 10568 int notif_class; 10569 10570 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10571 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10572 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10573 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10574 ctl_done((union ctl_io *)ctsio); 10575 return (CTL_RETVAL_COMPLETE); 10576 } 10577 notif_class = cdb->notif_class; 10578 alloc_len = scsi_2btoul(cdb->length); 10579 10580 data_len = sizeof(struct scsi_get_event_status_header); 10581 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10582 ctsio->kern_sg_entries = 0; 10583 ctsio->kern_data_resid = 0; 10584 ctsio->kern_rel_offset = 0; 10585 10586 if (data_len < alloc_len) { 10587 ctsio->residual = alloc_len - data_len; 10588 ctsio->kern_data_len = data_len; 10589 ctsio->kern_total_len = data_len; 10590 } else { 10591 ctsio->residual = 0; 10592 ctsio->kern_data_len = alloc_len; 10593 ctsio->kern_total_len = alloc_len; 10594 } 10595 10596 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10597 scsi_ulto2b(0, hdr->descr_length); 10598 hdr->nea_class = SGESN_NEA; 10599 hdr->supported_class = 0; 10600 10601 ctl_set_success(ctsio); 10602 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10603 ctsio->be_move_done = ctl_config_move_done; 10604 ctl_datamove((union ctl_io *)ctsio); 10605 return (CTL_RETVAL_COMPLETE); 10606} 10607 10608int 10609ctl_mechanism_status(struct ctl_scsiio *ctsio) 10610{ 10611 struct scsi_mechanism_status_header *hdr; 10612 struct scsi_mechanism_status *cdb; 10613 uint32_t alloc_len, data_len; 10614 10615 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10616 alloc_len = scsi_2btoul(cdb->length); 10617 10618 data_len = sizeof(struct scsi_mechanism_status_header); 10619 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10620 ctsio->kern_sg_entries = 0; 10621 ctsio->kern_data_resid = 0; 10622 ctsio->kern_rel_offset = 0; 10623 10624 if (data_len < alloc_len) { 10625 ctsio->residual = alloc_len - data_len; 10626 ctsio->kern_data_len = data_len; 10627 ctsio->kern_total_len = data_len; 10628 } else { 10629 ctsio->residual = 0; 10630 ctsio->kern_data_len = alloc_len; 10631 ctsio->kern_total_len = alloc_len; 10632 } 10633 10634 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10635 hdr->state1 = 0x00; 10636 hdr->state2 = 0xe0; 10637 scsi_ulto3b(0, hdr->lba); 10638 hdr->slots_num = 0; 10639 scsi_ulto2b(0, hdr->slots_length); 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 10648static void 10649ctl_ultomsf(uint32_t lba, uint8_t *buf) 10650{ 10651 10652 lba += 150; 10653 buf[0] = 0; 10654 buf[1] = bin2bcd((lba / 75) / 60); 10655 buf[2] = bin2bcd((lba / 75) % 60); 10656 buf[3] = bin2bcd(lba % 75); 10657} 10658 10659int 10660ctl_read_toc(struct ctl_scsiio *ctsio) 10661{ 10662 struct ctl_lun *lun = CTL_LUN(ctsio); 10663 struct scsi_read_toc_hdr *hdr; 10664 struct scsi_read_toc_type01_descr *descr; 10665 struct scsi_read_toc *cdb; 10666 uint32_t alloc_len, data_len; 10667 int format, msf; 10668 10669 cdb = (struct scsi_read_toc *)ctsio->cdb; 10670 msf = (cdb->byte2 & CD_MSF) != 0; 10671 format = cdb->format; 10672 alloc_len = scsi_2btoul(cdb->data_len); 10673 10674 data_len = sizeof(struct scsi_read_toc_hdr); 10675 if (format == 0) 10676 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10677 else 10678 data_len += sizeof(struct scsi_read_toc_type01_descr); 10679 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10680 ctsio->kern_sg_entries = 0; 10681 ctsio->kern_data_resid = 0; 10682 ctsio->kern_rel_offset = 0; 10683 10684 if (data_len < alloc_len) { 10685 ctsio->residual = alloc_len - data_len; 10686 ctsio->kern_data_len = data_len; 10687 ctsio->kern_total_len = data_len; 10688 } else { 10689 ctsio->residual = 0; 10690 ctsio->kern_data_len = alloc_len; 10691 ctsio->kern_total_len = alloc_len; 10692 } 10693 10694 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10695 if (format == 0) { 10696 scsi_ulto2b(0x12, hdr->data_length); 10697 hdr->first = 1; 10698 hdr->last = 1; 10699 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10700 descr->addr_ctl = 0x14; 10701 descr->track_number = 1; 10702 if (msf) 10703 ctl_ultomsf(0, descr->track_start); 10704 else 10705 scsi_ulto4b(0, descr->track_start); 10706 descr++; 10707 descr->addr_ctl = 0x14; 10708 descr->track_number = 0xaa; 10709 if (msf) 10710 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10711 else 10712 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10713 } else { 10714 scsi_ulto2b(0x0a, hdr->data_length); 10715 hdr->first = 1; 10716 hdr->last = 1; 10717 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10718 descr->addr_ctl = 0x14; 10719 descr->track_number = 1; 10720 if (msf) 10721 ctl_ultomsf(0, descr->track_start); 10722 else 10723 scsi_ulto4b(0, descr->track_start); 10724 } 10725 10726 ctl_set_success(ctsio); 10727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10728 ctsio->be_move_done = ctl_config_move_done; 10729 ctl_datamove((union ctl_io *)ctsio); 10730 return (CTL_RETVAL_COMPLETE); 10731} 10732 10733/* 10734 * For known CDB types, parse the LBA and length. 10735 */ 10736static int 10737ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10738{ 10739 if (io->io_hdr.io_type != CTL_IO_SCSI) 10740 return (1); 10741 10742 switch (io->scsiio.cdb[0]) { 10743 case COMPARE_AND_WRITE: { 10744 struct scsi_compare_and_write *cdb; 10745 10746 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10747 10748 *lba = scsi_8btou64(cdb->addr); 10749 *len = cdb->length; 10750 break; 10751 } 10752 case READ_6: 10753 case WRITE_6: { 10754 struct scsi_rw_6 *cdb; 10755 10756 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10757 10758 *lba = scsi_3btoul(cdb->addr); 10759 /* only 5 bits are valid in the most significant address byte */ 10760 *lba &= 0x1fffff; 10761 *len = cdb->length; 10762 break; 10763 } 10764 case READ_10: 10765 case WRITE_10: { 10766 struct scsi_rw_10 *cdb; 10767 10768 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10769 10770 *lba = scsi_4btoul(cdb->addr); 10771 *len = scsi_2btoul(cdb->length); 10772 break; 10773 } 10774 case WRITE_VERIFY_10: { 10775 struct scsi_write_verify_10 *cdb; 10776 10777 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10778 10779 *lba = scsi_4btoul(cdb->addr); 10780 *len = scsi_2btoul(cdb->length); 10781 break; 10782 } 10783 case READ_12: 10784 case WRITE_12: { 10785 struct scsi_rw_12 *cdb; 10786 10787 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10788 10789 *lba = scsi_4btoul(cdb->addr); 10790 *len = scsi_4btoul(cdb->length); 10791 break; 10792 } 10793 case WRITE_VERIFY_12: { 10794 struct scsi_write_verify_12 *cdb; 10795 10796 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10797 10798 *lba = scsi_4btoul(cdb->addr); 10799 *len = scsi_4btoul(cdb->length); 10800 break; 10801 } 10802 case READ_16: 10803 case WRITE_16: { 10804 struct scsi_rw_16 *cdb; 10805 10806 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10807 10808 *lba = scsi_8btou64(cdb->addr); 10809 *len = scsi_4btoul(cdb->length); 10810 break; 10811 } 10812 case WRITE_ATOMIC_16: { 10813 struct scsi_write_atomic_16 *cdb; 10814 10815 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10816 10817 *lba = scsi_8btou64(cdb->addr); 10818 *len = scsi_2btoul(cdb->length); 10819 break; 10820 } 10821 case WRITE_VERIFY_16: { 10822 struct scsi_write_verify_16 *cdb; 10823 10824 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10825 10826 *lba = scsi_8btou64(cdb->addr); 10827 *len = scsi_4btoul(cdb->length); 10828 break; 10829 } 10830 case WRITE_SAME_10: { 10831 struct scsi_write_same_10 *cdb; 10832 10833 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10834 10835 *lba = scsi_4btoul(cdb->addr); 10836 *len = scsi_2btoul(cdb->length); 10837 break; 10838 } 10839 case WRITE_SAME_16: { 10840 struct scsi_write_same_16 *cdb; 10841 10842 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10843 10844 *lba = scsi_8btou64(cdb->addr); 10845 *len = scsi_4btoul(cdb->length); 10846 break; 10847 } 10848 case VERIFY_10: { 10849 struct scsi_verify_10 *cdb; 10850 10851 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10852 10853 *lba = scsi_4btoul(cdb->addr); 10854 *len = scsi_2btoul(cdb->length); 10855 break; 10856 } 10857 case VERIFY_12: { 10858 struct scsi_verify_12 *cdb; 10859 10860 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10861 10862 *lba = scsi_4btoul(cdb->addr); 10863 *len = scsi_4btoul(cdb->length); 10864 break; 10865 } 10866 case VERIFY_16: { 10867 struct scsi_verify_16 *cdb; 10868 10869 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10870 10871 *lba = scsi_8btou64(cdb->addr); 10872 *len = scsi_4btoul(cdb->length); 10873 break; 10874 } 10875 case UNMAP: { 10876 *lba = 0; 10877 *len = UINT64_MAX; 10878 break; 10879 } 10880 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10881 struct scsi_get_lba_status *cdb; 10882 10883 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10884 *lba = scsi_8btou64(cdb->addr); 10885 *len = UINT32_MAX; 10886 break; 10887 } 10888 default: 10889 return (1); 10890 break; /* NOTREACHED */ 10891 } 10892 10893 return (0); 10894} 10895 10896static ctl_action 10897ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10898 bool seq) 10899{ 10900 uint64_t endlba1, endlba2; 10901 10902 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10903 endlba2 = lba2 + len2 - 1; 10904 10905 if ((endlba1 < lba2) || (endlba2 < lba1)) 10906 return (CTL_ACTION_PASS); 10907 else 10908 return (CTL_ACTION_BLOCK); 10909} 10910 10911static int 10912ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10913{ 10914 struct ctl_ptr_len_flags *ptrlen; 10915 struct scsi_unmap_desc *buf, *end, *range; 10916 uint64_t lba; 10917 uint32_t len; 10918 10919 /* If not UNMAP -- go other way. */ 10920 if (io->io_hdr.io_type != CTL_IO_SCSI || 10921 io->scsiio.cdb[0] != UNMAP) 10922 return (CTL_ACTION_ERROR); 10923 10924 /* If UNMAP without data -- block and wait for data. */ 10925 ptrlen = (struct ctl_ptr_len_flags *) 10926 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10927 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10928 ptrlen->ptr == NULL) 10929 return (CTL_ACTION_BLOCK); 10930 10931 /* UNMAP with data -- check for collision. */ 10932 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10933 end = buf + ptrlen->len / sizeof(*buf); 10934 for (range = buf; range < end; range++) { 10935 lba = scsi_8btou64(range->lba); 10936 len = scsi_4btoul(range->length); 10937 if ((lba < lba2 + len2) && (lba + len > lba2)) 10938 return (CTL_ACTION_BLOCK); 10939 } 10940 return (CTL_ACTION_PASS); 10941} 10942 10943static ctl_action 10944ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10945{ 10946 uint64_t lba1, lba2; 10947 uint64_t len1, len2; 10948 int retval; 10949 10950 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10951 return (CTL_ACTION_ERROR); 10952 10953 retval = ctl_extent_check_unmap(io1, lba2, len2); 10954 if (retval != CTL_ACTION_ERROR) 10955 return (retval); 10956 10957 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10958 return (CTL_ACTION_ERROR); 10959 10960 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10961 seq = FALSE; 10962 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10963} 10964 10965static ctl_action 10966ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10967{ 10968 uint64_t lba1, lba2; 10969 uint64_t len1, len2; 10970 10971 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10972 return (CTL_ACTION_PASS); 10973 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10974 return (CTL_ACTION_ERROR); 10975 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10976 return (CTL_ACTION_ERROR); 10977 10978 if (lba1 + len1 == lba2) 10979 return (CTL_ACTION_BLOCK); 10980 return (CTL_ACTION_PASS); 10981} 10982 10983static ctl_action 10984ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10985 union ctl_io *ooa_io) 10986{ 10987 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10988 const ctl_serialize_action *serialize_row; 10989 10990 /* 10991 * The initiator attempted multiple untagged commands at the same 10992 * time. Can't do that. 10993 */ 10994 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10995 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10996 && ((pending_io->io_hdr.nexus.targ_port == 10997 ooa_io->io_hdr.nexus.targ_port) 10998 && (pending_io->io_hdr.nexus.initid == 10999 ooa_io->io_hdr.nexus.initid)) 11000 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11001 CTL_FLAG_STATUS_SENT)) == 0)) 11002 return (CTL_ACTION_OVERLAP); 11003 11004 /* 11005 * The initiator attempted to send multiple tagged commands with 11006 * the same ID. (It's fine if different initiators have the same 11007 * tag ID.) 11008 * 11009 * Even if all of those conditions are true, we don't kill the I/O 11010 * if the command ahead of us has been aborted. We won't end up 11011 * sending it to the FETD, and it's perfectly legal to resend a 11012 * command with the same tag number as long as the previous 11013 * instance of this tag number has been aborted somehow. 11014 */ 11015 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11016 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11017 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11018 && ((pending_io->io_hdr.nexus.targ_port == 11019 ooa_io->io_hdr.nexus.targ_port) 11020 && (pending_io->io_hdr.nexus.initid == 11021 ooa_io->io_hdr.nexus.initid)) 11022 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11023 CTL_FLAG_STATUS_SENT)) == 0)) 11024 return (CTL_ACTION_OVERLAP_TAG); 11025 11026 /* 11027 * If we get a head of queue tag, SAM-3 says that we should 11028 * immediately execute it. 11029 * 11030 * What happens if this command would normally block for some other 11031 * reason? e.g. a request sense with a head of queue tag 11032 * immediately after a write. Normally that would block, but this 11033 * will result in its getting executed immediately... 11034 * 11035 * We currently return "pass" instead of "skip", so we'll end up 11036 * going through the rest of the queue to check for overlapped tags. 11037 * 11038 * XXX KDM check for other types of blockage first?? 11039 */ 11040 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11041 return (CTL_ACTION_PASS); 11042 11043 /* 11044 * Ordered tags have to block until all items ahead of them 11045 * have completed. If we get called with an ordered tag, we always 11046 * block, if something else is ahead of us in the queue. 11047 */ 11048 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11049 return (CTL_ACTION_BLOCK); 11050 11051 /* 11052 * Simple tags get blocked until all head of queue and ordered tags 11053 * ahead of them have completed. I'm lumping untagged commands in 11054 * with simple tags here. XXX KDM is that the right thing to do? 11055 */ 11056 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11057 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11058 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11059 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11060 return (CTL_ACTION_BLOCK); 11061 11062 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11063 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 11064 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 11065 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 11066 pending_io->scsiio.cdb[1], pending_io)); 11067 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11068 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 11069 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 11070 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 11071 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 11072 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 11073 ooa_io->scsiio.cdb[1], ooa_io)); 11074 11075 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11076 11077 switch (serialize_row[pending_entry->seridx]) { 11078 case CTL_SER_BLOCK: 11079 return (CTL_ACTION_BLOCK); 11080 case CTL_SER_EXTENT: 11081 return (ctl_extent_check(ooa_io, pending_io, 11082 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11083 case CTL_SER_EXTENTOPT: 11084 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11085 SCP_QUEUE_ALG_UNRESTRICTED) 11086 return (ctl_extent_check(ooa_io, pending_io, 11087 (lun->be_lun && 11088 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11089 return (CTL_ACTION_PASS); 11090 case CTL_SER_EXTENTSEQ: 11091 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11092 return (ctl_extent_check_seq(ooa_io, pending_io)); 11093 return (CTL_ACTION_PASS); 11094 case CTL_SER_PASS: 11095 return (CTL_ACTION_PASS); 11096 case CTL_SER_BLOCKOPT: 11097 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11098 SCP_QUEUE_ALG_UNRESTRICTED) 11099 return (CTL_ACTION_BLOCK); 11100 return (CTL_ACTION_PASS); 11101 case CTL_SER_SKIP: 11102 return (CTL_ACTION_SKIP); 11103 default: 11104 panic("%s: Invalid serialization value %d for %d => %d", 11105 __func__, serialize_row[pending_entry->seridx], 11106 pending_entry->seridx, ooa_entry->seridx); 11107 } 11108 11109 return (CTL_ACTION_ERROR); 11110} 11111 11112/* 11113 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11114 * Assumptions: 11115 * - pending_io is generally either incoming, or on the blocked queue 11116 * - starting I/O is the I/O we want to start the check with. 11117 */ 11118static ctl_action 11119ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11120 union ctl_io *starting_io) 11121{ 11122 union ctl_io *ooa_io; 11123 ctl_action action; 11124 11125 mtx_assert(&lun->lun_lock, MA_OWNED); 11126 11127 /* 11128 * Run back along the OOA queue, starting with the current 11129 * blocked I/O and going through every I/O before it on the 11130 * queue. If starting_io is NULL, we'll just end up returning 11131 * CTL_ACTION_PASS. 11132 */ 11133 for (ooa_io = starting_io; ooa_io != NULL; 11134 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11135 ooa_links)){ 11136 11137 /* 11138 * This routine just checks to see whether 11139 * cur_blocked is blocked by ooa_io, which is ahead 11140 * of it in the queue. It doesn't queue/dequeue 11141 * cur_blocked. 11142 */ 11143 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11144 switch (action) { 11145 case CTL_ACTION_BLOCK: 11146 case CTL_ACTION_OVERLAP: 11147 case CTL_ACTION_OVERLAP_TAG: 11148 case CTL_ACTION_SKIP: 11149 case CTL_ACTION_ERROR: 11150 return (action); 11151 break; /* NOTREACHED */ 11152 case CTL_ACTION_PASS: 11153 break; 11154 default: 11155 panic("%s: Invalid action %d\n", __func__, action); 11156 } 11157 } 11158 11159 return (CTL_ACTION_PASS); 11160} 11161 11162/* 11163 * Assumptions: 11164 * - An I/O has just completed, and has been removed from the per-LUN OOA 11165 * queue, so some items on the blocked queue may now be unblocked. 11166 */ 11167static int 11168ctl_check_blocked(struct ctl_lun *lun) 11169{ 11170 struct ctl_softc *softc = lun->ctl_softc; 11171 union ctl_io *cur_blocked, *next_blocked; 11172 11173 mtx_assert(&lun->lun_lock, MA_OWNED); 11174 11175 /* 11176 * Run forward from the head of the blocked queue, checking each 11177 * entry against the I/Os prior to it on the OOA queue to see if 11178 * there is still any blockage. 11179 * 11180 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11181 * with our removing a variable on it while it is traversing the 11182 * list. 11183 */ 11184 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11185 cur_blocked != NULL; cur_blocked = next_blocked) { 11186 union ctl_io *prev_ooa; 11187 ctl_action action; 11188 11189 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11190 blocked_links); 11191 11192 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11193 ctl_ooaq, ooa_links); 11194 11195 /* 11196 * If cur_blocked happens to be the first item in the OOA 11197 * queue now, prev_ooa will be NULL, and the action 11198 * returned will just be CTL_ACTION_PASS. 11199 */ 11200 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11201 11202 switch (action) { 11203 case CTL_ACTION_BLOCK: 11204 /* Nothing to do here, still blocked */ 11205 break; 11206 case CTL_ACTION_OVERLAP: 11207 case CTL_ACTION_OVERLAP_TAG: 11208 /* 11209 * This shouldn't happen! In theory we've already 11210 * checked this command for overlap... 11211 */ 11212 break; 11213 case CTL_ACTION_PASS: 11214 case CTL_ACTION_SKIP: { 11215 const struct ctl_cmd_entry *entry; 11216 11217 /* 11218 * The skip case shouldn't happen, this transaction 11219 * should have never made it onto the blocked queue. 11220 */ 11221 /* 11222 * This I/O is no longer blocked, we can remove it 11223 * from the blocked queue. Since this is a TAILQ 11224 * (doubly linked list), we can do O(1) removals 11225 * from any place on the list. 11226 */ 11227 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11228 blocked_links); 11229 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11230 11231 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11232 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11233 /* 11234 * Need to send IO back to original side to 11235 * run 11236 */ 11237 union ctl_ha_msg msg_info; 11238 11239 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11240 msg_info.hdr.original_sc = 11241 cur_blocked->io_hdr.original_sc; 11242 msg_info.hdr.serializing_sc = cur_blocked; 11243 msg_info.hdr.msg_type = CTL_MSG_R2R; 11244 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11245 sizeof(msg_info.hdr), M_NOWAIT); 11246 break; 11247 } 11248 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11249 11250 /* 11251 * Check this I/O for LUN state changes that may 11252 * have happened while this command was blocked. 11253 * The LUN state may have been changed by a command 11254 * ahead of us in the queue, so we need to re-check 11255 * for any states that can be caused by SCSI 11256 * commands. 11257 */ 11258 if (ctl_scsiio_lun_check(lun, entry, 11259 &cur_blocked->scsiio) == 0) { 11260 cur_blocked->io_hdr.flags |= 11261 CTL_FLAG_IS_WAS_ON_RTR; 11262 ctl_enqueue_rtr(cur_blocked); 11263 } else 11264 ctl_done(cur_blocked); 11265 break; 11266 } 11267 default: 11268 /* 11269 * This probably shouldn't happen -- we shouldn't 11270 * get CTL_ACTION_ERROR, or anything else. 11271 */ 11272 break; 11273 } 11274 } 11275 11276 return (CTL_RETVAL_COMPLETE); 11277} 11278 11279/* 11280 * This routine (with one exception) checks LUN flags that can be set by 11281 * commands ahead of us in the OOA queue. These flags have to be checked 11282 * when a command initially comes in, and when we pull a command off the 11283 * blocked queue and are preparing to execute it. The reason we have to 11284 * check these flags for commands on the blocked queue is that the LUN 11285 * state may have been changed by a command ahead of us while we're on the 11286 * blocked queue. 11287 * 11288 * Ordering is somewhat important with these checks, so please pay 11289 * careful attention to the placement of any new checks. 11290 */ 11291static int 11292ctl_scsiio_lun_check(struct ctl_lun *lun, 11293 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11294{ 11295 struct ctl_softc *softc = lun->ctl_softc; 11296 int retval; 11297 uint32_t residx; 11298 11299 retval = 0; 11300 11301 mtx_assert(&lun->lun_lock, MA_OWNED); 11302 11303 /* 11304 * If this shelf is a secondary shelf controller, we may have to 11305 * reject some commands disallowed by HA mode and link state. 11306 */ 11307 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11308 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11309 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11310 ctl_set_lun_unavail(ctsio); 11311 retval = 1; 11312 goto bailout; 11313 } 11314 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11315 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11316 ctl_set_lun_transit(ctsio); 11317 retval = 1; 11318 goto bailout; 11319 } 11320 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11321 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11322 ctl_set_lun_standby(ctsio); 11323 retval = 1; 11324 goto bailout; 11325 } 11326 11327 /* The rest of checks are only done on executing side */ 11328 if (softc->ha_mode == CTL_HA_MODE_XFER) 11329 goto bailout; 11330 } 11331 11332 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11333 if (lun->be_lun && 11334 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11335 ctl_set_hw_write_protected(ctsio); 11336 retval = 1; 11337 goto bailout; 11338 } 11339 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11340 ctl_set_sense(ctsio, /*current_error*/ 1, 11341 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11342 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11343 retval = 1; 11344 goto bailout; 11345 } 11346 } 11347 11348 /* 11349 * Check for a reservation conflict. If this command isn't allowed 11350 * even on reserved LUNs, and if this initiator isn't the one who 11351 * reserved us, reject the command with a reservation conflict. 11352 */ 11353 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11354 if ((lun->flags & CTL_LUN_RESERVED) 11355 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11356 if (lun->res_idx != residx) { 11357 ctl_set_reservation_conflict(ctsio); 11358 retval = 1; 11359 goto bailout; 11360 } 11361 } 11362 11363 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11364 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11365 /* No reservation or command is allowed. */; 11366 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11367 (lun->pr_res_type == SPR_TYPE_WR_EX || 11368 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11369 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11370 /* The command is allowed for Write Exclusive resv. */; 11371 } else { 11372 /* 11373 * if we aren't registered or it's a res holder type 11374 * reservation and this isn't the res holder then set a 11375 * conflict. 11376 */ 11377 if (ctl_get_prkey(lun, residx) == 0 || 11378 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11379 ctl_set_reservation_conflict(ctsio); 11380 retval = 1; 11381 goto bailout; 11382 } 11383 } 11384 11385 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11386 if (lun->flags & CTL_LUN_EJECTED) 11387 ctl_set_lun_ejected(ctsio); 11388 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11389 if (lun->flags & CTL_LUN_REMOVABLE) 11390 ctl_set_lun_no_media(ctsio); 11391 else 11392 ctl_set_lun_int_reqd(ctsio); 11393 } else if (lun->flags & CTL_LUN_STOPPED) 11394 ctl_set_lun_stopped(ctsio); 11395 else 11396 goto bailout; 11397 retval = 1; 11398 goto bailout; 11399 } 11400 11401bailout: 11402 return (retval); 11403} 11404 11405static void 11406ctl_failover_io(union ctl_io *io, int have_lock) 11407{ 11408 ctl_set_busy(&io->scsiio); 11409 ctl_done(io); 11410} 11411 11412static void 11413ctl_failover_lun(union ctl_io *rio) 11414{ 11415 struct ctl_softc *softc = CTL_SOFTC(rio); 11416 struct ctl_lun *lun; 11417 struct ctl_io_hdr *io, *next_io; 11418 uint32_t targ_lun; 11419 11420 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11421 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11422 11423 /* Find and lock the LUN. */ 11424 mtx_lock(&softc->ctl_lock); 11425 if (targ_lun > CTL_MAX_LUNS || 11426 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11427 mtx_unlock(&softc->ctl_lock); 11428 return; 11429 } 11430 mtx_lock(&lun->lun_lock); 11431 mtx_unlock(&softc->ctl_lock); 11432 if (lun->flags & CTL_LUN_DISABLED) { 11433 mtx_unlock(&lun->lun_lock); 11434 return; 11435 } 11436 11437 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11438 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11439 /* We are master */ 11440 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11441 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11442 io->flags |= CTL_FLAG_ABORT; 11443 io->flags |= CTL_FLAG_FAILOVER; 11444 } else { /* This can be only due to DATAMOVE */ 11445 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11446 io->flags &= ~CTL_FLAG_DMA_INPROG; 11447 io->flags |= CTL_FLAG_IO_ACTIVE; 11448 io->port_status = 31340; 11449 ctl_enqueue_isc((union ctl_io *)io); 11450 } 11451 } 11452 /* We are slave */ 11453 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11454 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11455 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11456 io->flags |= CTL_FLAG_FAILOVER; 11457 } else { 11458 ctl_set_busy(&((union ctl_io *)io)-> 11459 scsiio); 11460 ctl_done((union ctl_io *)io); 11461 } 11462 } 11463 } 11464 } else { /* SERIALIZE modes */ 11465 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11466 next_io) { 11467 /* We are master */ 11468 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11469 TAILQ_REMOVE(&lun->blocked_queue, io, 11470 blocked_links); 11471 io->flags &= ~CTL_FLAG_BLOCKED; 11472 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11473 ctl_free_io((union ctl_io *)io); 11474 } 11475 } 11476 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11477 /* We are master */ 11478 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11479 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11480 ctl_free_io((union ctl_io *)io); 11481 } 11482 /* We are slave */ 11483 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11484 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11485 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11486 ctl_set_busy(&((union ctl_io *)io)-> 11487 scsiio); 11488 ctl_done((union ctl_io *)io); 11489 } 11490 } 11491 } 11492 ctl_check_blocked(lun); 11493 } 11494 mtx_unlock(&lun->lun_lock); 11495} 11496 11497static int 11498ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11499{ 11500 struct ctl_lun *lun; 11501 const struct ctl_cmd_entry *entry; 11502 uint32_t initidx, targ_lun; 11503 int retval = 0; 11504 11505 lun = NULL; 11506 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11507 if (targ_lun < CTL_MAX_LUNS) 11508 lun = softc->ctl_luns[targ_lun]; 11509 if (lun) { 11510 /* 11511 * If the LUN is invalid, pretend that it doesn't exist. 11512 * It will go away as soon as all pending I/O has been 11513 * completed. 11514 */ 11515 mtx_lock(&lun->lun_lock); 11516 if (lun->flags & CTL_LUN_DISABLED) { 11517 mtx_unlock(&lun->lun_lock); 11518 lun = NULL; 11519 } 11520 } 11521 CTL_LUN(ctsio) = lun; 11522 if (lun) { 11523 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11524 11525 /* 11526 * Every I/O goes into the OOA queue for a particular LUN, 11527 * and stays there until completion. 11528 */ 11529#ifdef CTL_TIME_IO 11530 if (TAILQ_EMPTY(&lun->ooa_queue)) 11531 lun->idle_time += getsbinuptime() - lun->last_busy; 11532#endif 11533 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11534 } 11535 11536 /* Get command entry and return error if it is unsuppotyed. */ 11537 entry = ctl_validate_command(ctsio); 11538 if (entry == NULL) { 11539 if (lun) 11540 mtx_unlock(&lun->lun_lock); 11541 return (retval); 11542 } 11543 11544 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11545 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11546 11547 /* 11548 * Check to see whether we can send this command to LUNs that don't 11549 * exist. This should pretty much only be the case for inquiry 11550 * and request sense. Further checks, below, really require having 11551 * a LUN, so we can't really check the command anymore. Just put 11552 * it on the rtr queue. 11553 */ 11554 if (lun == NULL) { 11555 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11556 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11557 ctl_enqueue_rtr((union ctl_io *)ctsio); 11558 return (retval); 11559 } 11560 11561 ctl_set_unsupported_lun(ctsio); 11562 ctl_done((union ctl_io *)ctsio); 11563 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11564 return (retval); 11565 } else { 11566 /* 11567 * Make sure we support this particular command on this LUN. 11568 * e.g., we don't support writes to the control LUN. 11569 */ 11570 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11571 mtx_unlock(&lun->lun_lock); 11572 ctl_set_invalid_opcode(ctsio); 11573 ctl_done((union ctl_io *)ctsio); 11574 return (retval); 11575 } 11576 } 11577 11578 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11579 11580#ifdef CTL_WITH_CA 11581 /* 11582 * If we've got a request sense, it'll clear the contingent 11583 * allegiance condition. Otherwise, if we have a CA condition for 11584 * this initiator, clear it, because it sent down a command other 11585 * than request sense. 11586 */ 11587 if ((ctsio->cdb[0] != REQUEST_SENSE) 11588 && (ctl_is_set(lun->have_ca, initidx))) 11589 ctl_clear_mask(lun->have_ca, initidx); 11590#endif 11591 11592 /* 11593 * If the command has this flag set, it handles its own unit 11594 * attention reporting, we shouldn't do anything. Otherwise we 11595 * check for any pending unit attentions, and send them back to the 11596 * initiator. We only do this when a command initially comes in, 11597 * not when we pull it off the blocked queue. 11598 * 11599 * According to SAM-3, section 5.3.2, the order that things get 11600 * presented back to the host is basically unit attentions caused 11601 * by some sort of reset event, busy status, reservation conflicts 11602 * or task set full, and finally any other status. 11603 * 11604 * One issue here is that some of the unit attentions we report 11605 * don't fall into the "reset" category (e.g. "reported luns data 11606 * has changed"). So reporting it here, before the reservation 11607 * check, may be technically wrong. I guess the only thing to do 11608 * would be to check for and report the reset events here, and then 11609 * check for the other unit attention types after we check for a 11610 * reservation conflict. 11611 * 11612 * XXX KDM need to fix this 11613 */ 11614 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11615 ctl_ua_type ua_type; 11616 u_int sense_len = 0; 11617 11618 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11619 &sense_len, SSD_TYPE_NONE); 11620 if (ua_type != CTL_UA_NONE) { 11621 mtx_unlock(&lun->lun_lock); 11622 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11623 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11624 ctsio->sense_len = sense_len; 11625 ctl_done((union ctl_io *)ctsio); 11626 return (retval); 11627 } 11628 } 11629 11630 11631 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11632 mtx_unlock(&lun->lun_lock); 11633 ctl_done((union ctl_io *)ctsio); 11634 return (retval); 11635 } 11636 11637 /* 11638 * XXX CHD this is where we want to send IO to other side if 11639 * this LUN is secondary on this SC. We will need to make a copy 11640 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11641 * the copy we send as FROM_OTHER. 11642 * We also need to stuff the address of the original IO so we can 11643 * find it easily. Something similar will need be done on the other 11644 * side so when we are done we can find the copy. 11645 */ 11646 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11647 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11648 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11649 union ctl_ha_msg msg_info; 11650 int isc_retval; 11651 11652 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11653 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11654 mtx_unlock(&lun->lun_lock); 11655 11656 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11657 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11658 msg_info.hdr.serializing_sc = NULL; 11659 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11660 msg_info.scsi.tag_num = ctsio->tag_num; 11661 msg_info.scsi.tag_type = ctsio->tag_type; 11662 msg_info.scsi.cdb_len = ctsio->cdb_len; 11663 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11664 11665 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11666 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11667 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11668 ctl_set_busy(ctsio); 11669 ctl_done((union ctl_io *)ctsio); 11670 return (retval); 11671 } 11672 return (retval); 11673 } 11674 11675 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11676 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11677 ctl_ooaq, ooa_links))) { 11678 case CTL_ACTION_BLOCK: 11679 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11680 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11681 blocked_links); 11682 mtx_unlock(&lun->lun_lock); 11683 return (retval); 11684 case CTL_ACTION_PASS: 11685 case CTL_ACTION_SKIP: 11686 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11687 mtx_unlock(&lun->lun_lock); 11688 ctl_enqueue_rtr((union ctl_io *)ctsio); 11689 break; 11690 case CTL_ACTION_OVERLAP: 11691 mtx_unlock(&lun->lun_lock); 11692 ctl_set_overlapped_cmd(ctsio); 11693 ctl_done((union ctl_io *)ctsio); 11694 break; 11695 case CTL_ACTION_OVERLAP_TAG: 11696 mtx_unlock(&lun->lun_lock); 11697 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11698 ctl_done((union ctl_io *)ctsio); 11699 break; 11700 case CTL_ACTION_ERROR: 11701 default: 11702 mtx_unlock(&lun->lun_lock); 11703 ctl_set_internal_failure(ctsio, 11704 /*sks_valid*/ 0, 11705 /*retry_count*/ 0); 11706 ctl_done((union ctl_io *)ctsio); 11707 break; 11708 } 11709 return (retval); 11710} 11711 11712const struct ctl_cmd_entry * 11713ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11714{ 11715 const struct ctl_cmd_entry *entry; 11716 int service_action; 11717 11718 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11719 if (sa) 11720 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11721 if (entry->flags & CTL_CMD_FLAG_SA5) { 11722 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11723 entry = &((const struct ctl_cmd_entry *) 11724 entry->execute)[service_action]; 11725 } 11726 return (entry); 11727} 11728 11729const struct ctl_cmd_entry * 11730ctl_validate_command(struct ctl_scsiio *ctsio) 11731{ 11732 const struct ctl_cmd_entry *entry; 11733 int i, sa; 11734 uint8_t diff; 11735 11736 entry = ctl_get_cmd_entry(ctsio, &sa); 11737 if (entry->execute == NULL) { 11738 if (sa) 11739 ctl_set_invalid_field(ctsio, 11740 /*sks_valid*/ 1, 11741 /*command*/ 1, 11742 /*field*/ 1, 11743 /*bit_valid*/ 1, 11744 /*bit*/ 4); 11745 else 11746 ctl_set_invalid_opcode(ctsio); 11747 ctl_done((union ctl_io *)ctsio); 11748 return (NULL); 11749 } 11750 KASSERT(entry->length > 0, 11751 ("Not defined length for command 0x%02x/0x%02x", 11752 ctsio->cdb[0], ctsio->cdb[1])); 11753 for (i = 1; i < entry->length; i++) { 11754 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11755 if (diff == 0) 11756 continue; 11757 ctl_set_invalid_field(ctsio, 11758 /*sks_valid*/ 1, 11759 /*command*/ 1, 11760 /*field*/ i, 11761 /*bit_valid*/ 1, 11762 /*bit*/ fls(diff) - 1); 11763 ctl_done((union ctl_io *)ctsio); 11764 return (NULL); 11765 } 11766 return (entry); 11767} 11768 11769static int 11770ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11771{ 11772 11773 switch (lun_type) { 11774 case T_DIRECT: 11775 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11776 return (0); 11777 break; 11778 case T_PROCESSOR: 11779 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11780 return (0); 11781 break; 11782 case T_CDROM: 11783 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11784 return (0); 11785 break; 11786 default: 11787 return (0); 11788 } 11789 return (1); 11790} 11791 11792static int 11793ctl_scsiio(struct ctl_scsiio *ctsio) 11794{ 11795 int retval; 11796 const struct ctl_cmd_entry *entry; 11797 11798 retval = CTL_RETVAL_COMPLETE; 11799 11800 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11801 11802 entry = ctl_get_cmd_entry(ctsio, NULL); 11803 11804 /* 11805 * If this I/O has been aborted, just send it straight to 11806 * ctl_done() without executing it. 11807 */ 11808 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11809 ctl_done((union ctl_io *)ctsio); 11810 goto bailout; 11811 } 11812 11813 /* 11814 * All the checks should have been handled by ctl_scsiio_precheck(). 11815 * We should be clear now to just execute the I/O. 11816 */ 11817 retval = entry->execute(ctsio); 11818 11819bailout: 11820 return (retval); 11821} 11822 11823/* 11824 * Since we only implement one target right now, a bus reset simply resets 11825 * our single target. 11826 */ 11827static int 11828ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11829{ 11830 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11831} 11832 11833static int 11834ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11835 ctl_ua_type ua_type) 11836{ 11837 struct ctl_port *port = CTL_PORT(io); 11838 struct ctl_lun *lun; 11839 int retval; 11840 11841 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11842 union ctl_ha_msg msg_info; 11843 11844 msg_info.hdr.nexus = io->io_hdr.nexus; 11845 if (ua_type==CTL_UA_TARG_RESET) 11846 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11847 else 11848 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11849 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11850 msg_info.hdr.original_sc = NULL; 11851 msg_info.hdr.serializing_sc = NULL; 11852 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11853 sizeof(msg_info.task), M_WAITOK); 11854 } 11855 retval = 0; 11856 11857 mtx_lock(&softc->ctl_lock); 11858 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11859 if (port != NULL && 11860 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11861 continue; 11862 retval += ctl_do_lun_reset(lun, io, ua_type); 11863 } 11864 mtx_unlock(&softc->ctl_lock); 11865 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11866 return (retval); 11867} 11868 11869/* 11870 * The LUN should always be set. The I/O is optional, and is used to 11871 * distinguish between I/Os sent by this initiator, and by other 11872 * initiators. We set unit attention for initiators other than this one. 11873 * SAM-3 is vague on this point. It does say that a unit attention should 11874 * be established for other initiators when a LUN is reset (see section 11875 * 5.7.3), but it doesn't specifically say that the unit attention should 11876 * be established for this particular initiator when a LUN is reset. Here 11877 * is the relevant text, from SAM-3 rev 8: 11878 * 11879 * 5.7.2 When a SCSI initiator port aborts its own tasks 11880 * 11881 * When a SCSI initiator port causes its own task(s) to be aborted, no 11882 * notification that the task(s) have been aborted shall be returned to 11883 * the SCSI initiator port other than the completion response for the 11884 * command or task management function action that caused the task(s) to 11885 * be aborted and notification(s) associated with related effects of the 11886 * action (e.g., a reset unit attention condition). 11887 * 11888 * XXX KDM for now, we're setting unit attention for all initiators. 11889 */ 11890static int 11891ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11892{ 11893 union ctl_io *xio; 11894#if 0 11895 uint32_t initidx; 11896#endif 11897 int i; 11898 11899 mtx_lock(&lun->lun_lock); 11900 /* 11901 * Run through the OOA queue and abort each I/O. 11902 */ 11903 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11904 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11905 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11906 } 11907 11908 /* 11909 * This version sets unit attention for every 11910 */ 11911#if 0 11912 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11913 ctl_est_ua_all(lun, initidx, ua_type); 11914#else 11915 ctl_est_ua_all(lun, -1, ua_type); 11916#endif 11917 11918 /* 11919 * A reset (any kind, really) clears reservations established with 11920 * RESERVE/RELEASE. It does not clear reservations established 11921 * with PERSISTENT RESERVE OUT, but we don't support that at the 11922 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11923 * reservations made with the RESERVE/RELEASE commands, because 11924 * those commands are obsolete in SPC-3. 11925 */ 11926 lun->flags &= ~CTL_LUN_RESERVED; 11927 11928#ifdef CTL_WITH_CA 11929 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11930 ctl_clear_mask(lun->have_ca, i); 11931#endif 11932 lun->prevent_count = 0; 11933 if (lun->prevent) { 11934 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11935 ctl_clear_mask(lun->prevent, i); 11936 } 11937 mtx_unlock(&lun->lun_lock); 11938 11939 return (0); 11940} 11941 11942static int 11943ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11944{ 11945 struct ctl_lun *lun; 11946 uint32_t targ_lun; 11947 int retval; 11948 11949 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11950 mtx_lock(&softc->ctl_lock); 11951 if (targ_lun >= CTL_MAX_LUNS || 11952 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11953 mtx_unlock(&softc->ctl_lock); 11954 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11955 return (1); 11956 } 11957 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11958 mtx_unlock(&softc->ctl_lock); 11959 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11960 11961 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11962 union ctl_ha_msg msg_info; 11963 11964 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11965 msg_info.hdr.nexus = io->io_hdr.nexus; 11966 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11967 msg_info.hdr.original_sc = NULL; 11968 msg_info.hdr.serializing_sc = NULL; 11969 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11970 sizeof(msg_info.task), M_WAITOK); 11971 } 11972 return (retval); 11973} 11974 11975static void 11976ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11977 int other_sc) 11978{ 11979 union ctl_io *xio; 11980 11981 mtx_assert(&lun->lun_lock, MA_OWNED); 11982 11983 /* 11984 * Run through the OOA queue and attempt to find the given I/O. 11985 * The target port, initiator ID, tag type and tag number have to 11986 * match the values that we got from the initiator. If we have an 11987 * untagged command to abort, simply abort the first untagged command 11988 * we come to. We only allow one untagged command at a time of course. 11989 */ 11990 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11991 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11992 11993 if ((targ_port == UINT32_MAX || 11994 targ_port == xio->io_hdr.nexus.targ_port) && 11995 (init_id == UINT32_MAX || 11996 init_id == xio->io_hdr.nexus.initid)) { 11997 if (targ_port != xio->io_hdr.nexus.targ_port || 11998 init_id != xio->io_hdr.nexus.initid) 11999 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12000 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12001 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12002 union ctl_ha_msg msg_info; 12003 12004 msg_info.hdr.nexus = xio->io_hdr.nexus; 12005 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12006 msg_info.task.tag_num = xio->scsiio.tag_num; 12007 msg_info.task.tag_type = xio->scsiio.tag_type; 12008 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12009 msg_info.hdr.original_sc = NULL; 12010 msg_info.hdr.serializing_sc = NULL; 12011 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12012 sizeof(msg_info.task), M_NOWAIT); 12013 } 12014 } 12015 } 12016} 12017 12018static int 12019ctl_abort_task_set(union ctl_io *io) 12020{ 12021 struct ctl_softc *softc = CTL_SOFTC(io); 12022 struct ctl_lun *lun; 12023 uint32_t targ_lun; 12024 12025 /* 12026 * Look up the LUN. 12027 */ 12028 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12029 mtx_lock(&softc->ctl_lock); 12030 if (targ_lun >= CTL_MAX_LUNS || 12031 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12032 mtx_unlock(&softc->ctl_lock); 12033 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12034 return (1); 12035 } 12036 12037 mtx_lock(&lun->lun_lock); 12038 mtx_unlock(&softc->ctl_lock); 12039 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12040 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12041 io->io_hdr.nexus.initid, 12042 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12043 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12044 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12045 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12046 } 12047 mtx_unlock(&lun->lun_lock); 12048 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12049 return (0); 12050} 12051 12052static int 12053ctl_i_t_nexus_reset(union ctl_io *io) 12054{ 12055 struct ctl_softc *softc = CTL_SOFTC(io); 12056 struct ctl_lun *lun; 12057 uint32_t initidx; 12058 12059 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12060 union ctl_ha_msg msg_info; 12061 12062 msg_info.hdr.nexus = io->io_hdr.nexus; 12063 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12064 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12065 msg_info.hdr.original_sc = NULL; 12066 msg_info.hdr.serializing_sc = NULL; 12067 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12068 sizeof(msg_info.task), M_WAITOK); 12069 } 12070 12071 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12072 mtx_lock(&softc->ctl_lock); 12073 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12074 mtx_lock(&lun->lun_lock); 12075 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12076 io->io_hdr.nexus.initid, 1); 12077#ifdef CTL_WITH_CA 12078 ctl_clear_mask(lun->have_ca, initidx); 12079#endif 12080 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12081 lun->flags &= ~CTL_LUN_RESERVED; 12082 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 12083 ctl_clear_mask(lun->prevent, initidx); 12084 lun->prevent_count--; 12085 } 12086 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12087 mtx_unlock(&lun->lun_lock); 12088 } 12089 mtx_unlock(&softc->ctl_lock); 12090 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12091 return (0); 12092} 12093 12094static int 12095ctl_abort_task(union ctl_io *io) 12096{ 12097 struct ctl_softc *softc = CTL_SOFTC(io); 12098 union ctl_io *xio; 12099 struct ctl_lun *lun; 12100#if 0 12101 struct sbuf sb; 12102 char printbuf[128]; 12103#endif 12104 int found; 12105 uint32_t targ_lun; 12106 12107 found = 0; 12108 12109 /* 12110 * Look up the LUN. 12111 */ 12112 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12113 mtx_lock(&softc->ctl_lock); 12114 if (targ_lun >= CTL_MAX_LUNS || 12115 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12116 mtx_unlock(&softc->ctl_lock); 12117 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12118 return (1); 12119 } 12120 12121#if 0 12122 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12123 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12124#endif 12125 12126 mtx_lock(&lun->lun_lock); 12127 mtx_unlock(&softc->ctl_lock); 12128 /* 12129 * Run through the OOA queue and attempt to find the given I/O. 12130 * The target port, initiator ID, tag type and tag number have to 12131 * match the values that we got from the initiator. If we have an 12132 * untagged command to abort, simply abort the first untagged command 12133 * we come to. We only allow one untagged command at a time of course. 12134 */ 12135 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12136 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12137#if 0 12138 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12139 12140 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12141 lun->lun, xio->scsiio.tag_num, 12142 xio->scsiio.tag_type, 12143 (xio->io_hdr.blocked_links.tqe_prev 12144 == NULL) ? "" : " BLOCKED", 12145 (xio->io_hdr.flags & 12146 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12147 (xio->io_hdr.flags & 12148 CTL_FLAG_ABORT) ? " ABORT" : "", 12149 (xio->io_hdr.flags & 12150 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12151 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12152 sbuf_finish(&sb); 12153 printf("%s\n", sbuf_data(&sb)); 12154#endif 12155 12156 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12157 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12158 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12159 continue; 12160 12161 /* 12162 * If the abort says that the task is untagged, the 12163 * task in the queue must be untagged. Otherwise, 12164 * we just check to see whether the tag numbers 12165 * match. This is because the QLogic firmware 12166 * doesn't pass back the tag type in an abort 12167 * request. 12168 */ 12169#if 0 12170 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12171 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12172 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12173#endif 12174 /* 12175 * XXX KDM we've got problems with FC, because it 12176 * doesn't send down a tag type with aborts. So we 12177 * can only really go by the tag number... 12178 * This may cause problems with parallel SCSI. 12179 * Need to figure that out!! 12180 */ 12181 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12182 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12183 found = 1; 12184 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12185 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12186 union ctl_ha_msg msg_info; 12187 12188 msg_info.hdr.nexus = io->io_hdr.nexus; 12189 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12190 msg_info.task.tag_num = io->taskio.tag_num; 12191 msg_info.task.tag_type = io->taskio.tag_type; 12192 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12193 msg_info.hdr.original_sc = NULL; 12194 msg_info.hdr.serializing_sc = NULL; 12195#if 0 12196 printf("Sent Abort to other side\n"); 12197#endif 12198 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12199 sizeof(msg_info.task), M_NOWAIT); 12200 } 12201#if 0 12202 printf("ctl_abort_task: found I/O to abort\n"); 12203#endif 12204 } 12205 } 12206 mtx_unlock(&lun->lun_lock); 12207 12208 if (found == 0) { 12209 /* 12210 * This isn't really an error. It's entirely possible for 12211 * the abort and command completion to cross on the wire. 12212 * This is more of an informative/diagnostic error. 12213 */ 12214#if 0 12215 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12216 "%u:%u:%u tag %d type %d\n", 12217 io->io_hdr.nexus.initid, 12218 io->io_hdr.nexus.targ_port, 12219 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12220 io->taskio.tag_type); 12221#endif 12222 } 12223 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12224 return (0); 12225} 12226 12227static int 12228ctl_query_task(union ctl_io *io, int task_set) 12229{ 12230 struct ctl_softc *softc = CTL_SOFTC(io); 12231 union ctl_io *xio; 12232 struct ctl_lun *lun; 12233 int found = 0; 12234 uint32_t targ_lun; 12235 12236 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12237 mtx_lock(&softc->ctl_lock); 12238 if (targ_lun >= CTL_MAX_LUNS || 12239 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12240 mtx_unlock(&softc->ctl_lock); 12241 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12242 return (1); 12243 } 12244 mtx_lock(&lun->lun_lock); 12245 mtx_unlock(&softc->ctl_lock); 12246 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12247 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12248 12249 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12250 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12251 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12252 continue; 12253 12254 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12255 found = 1; 12256 break; 12257 } 12258 } 12259 mtx_unlock(&lun->lun_lock); 12260 if (found) 12261 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12262 else 12263 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12264 return (0); 12265} 12266 12267static int 12268ctl_query_async_event(union ctl_io *io) 12269{ 12270 struct ctl_softc *softc = CTL_SOFTC(io); 12271 struct ctl_lun *lun; 12272 ctl_ua_type ua; 12273 uint32_t targ_lun, initidx; 12274 12275 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12276 mtx_lock(&softc->ctl_lock); 12277 if (targ_lun >= CTL_MAX_LUNS || 12278 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12279 mtx_unlock(&softc->ctl_lock); 12280 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12281 return (1); 12282 } 12283 mtx_lock(&lun->lun_lock); 12284 mtx_unlock(&softc->ctl_lock); 12285 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12286 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12287 mtx_unlock(&lun->lun_lock); 12288 if (ua != CTL_UA_NONE) 12289 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12290 else 12291 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12292 return (0); 12293} 12294 12295static void 12296ctl_run_task(union ctl_io *io) 12297{ 12298 struct ctl_softc *softc = CTL_SOFTC(io); 12299 int retval = 1; 12300 12301 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12302 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12303 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12304 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12305 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12306 switch (io->taskio.task_action) { 12307 case CTL_TASK_ABORT_TASK: 12308 retval = ctl_abort_task(io); 12309 break; 12310 case CTL_TASK_ABORT_TASK_SET: 12311 case CTL_TASK_CLEAR_TASK_SET: 12312 retval = ctl_abort_task_set(io); 12313 break; 12314 case CTL_TASK_CLEAR_ACA: 12315 break; 12316 case CTL_TASK_I_T_NEXUS_RESET: 12317 retval = ctl_i_t_nexus_reset(io); 12318 break; 12319 case CTL_TASK_LUN_RESET: 12320 retval = ctl_lun_reset(softc, io); 12321 break; 12322 case CTL_TASK_TARGET_RESET: 12323 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12324 break; 12325 case CTL_TASK_BUS_RESET: 12326 retval = ctl_bus_reset(softc, io); 12327 break; 12328 case CTL_TASK_PORT_LOGIN: 12329 break; 12330 case CTL_TASK_PORT_LOGOUT: 12331 break; 12332 case CTL_TASK_QUERY_TASK: 12333 retval = ctl_query_task(io, 0); 12334 break; 12335 case CTL_TASK_QUERY_TASK_SET: 12336 retval = ctl_query_task(io, 1); 12337 break; 12338 case CTL_TASK_QUERY_ASYNC_EVENT: 12339 retval = ctl_query_async_event(io); 12340 break; 12341 default: 12342 printf("%s: got unknown task management event %d\n", 12343 __func__, io->taskio.task_action); 12344 break; 12345 } 12346 if (retval == 0) 12347 io->io_hdr.status = CTL_SUCCESS; 12348 else 12349 io->io_hdr.status = CTL_ERROR; 12350 ctl_done(io); 12351} 12352 12353/* 12354 * For HA operation. Handle commands that come in from the other 12355 * controller. 12356 */ 12357static void 12358ctl_handle_isc(union ctl_io *io) 12359{ 12360 struct ctl_softc *softc = CTL_SOFTC(io); 12361 struct ctl_lun *lun; 12362 const struct ctl_cmd_entry *entry; 12363 uint32_t targ_lun; 12364 12365 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12366 switch (io->io_hdr.msg_type) { 12367 case CTL_MSG_SERIALIZE: 12368 ctl_serialize_other_sc_cmd(&io->scsiio); 12369 break; 12370 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12371 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12372 if (targ_lun >= CTL_MAX_LUNS || 12373 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12374 ctl_done(io); 12375 break; 12376 } 12377 mtx_lock(&lun->lun_lock); 12378 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12379 mtx_unlock(&lun->lun_lock); 12380 ctl_done(io); 12381 break; 12382 } 12383 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12384 mtx_unlock(&lun->lun_lock); 12385 ctl_enqueue_rtr(io); 12386 break; 12387 case CTL_MSG_FINISH_IO: 12388 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12389 ctl_done(io); 12390 break; 12391 } 12392 if (targ_lun >= CTL_MAX_LUNS || 12393 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12394 ctl_free_io(io); 12395 break; 12396 } 12397 mtx_lock(&lun->lun_lock); 12398 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12399 ctl_check_blocked(lun); 12400 mtx_unlock(&lun->lun_lock); 12401 ctl_free_io(io); 12402 break; 12403 case CTL_MSG_PERS_ACTION: 12404 ctl_hndl_per_res_out_on_other_sc(io); 12405 ctl_free_io(io); 12406 break; 12407 case CTL_MSG_BAD_JUJU: 12408 ctl_done(io); 12409 break; 12410 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12411 ctl_datamove_remote(io); 12412 break; 12413 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12414 io->scsiio.be_move_done(io); 12415 break; 12416 case CTL_MSG_FAILOVER: 12417 ctl_failover_lun(io); 12418 ctl_free_io(io); 12419 break; 12420 default: 12421 printf("%s: Invalid message type %d\n", 12422 __func__, io->io_hdr.msg_type); 12423 ctl_free_io(io); 12424 break; 12425 } 12426 12427} 12428 12429 12430/* 12431 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12432 * there is no match. 12433 */ 12434static ctl_lun_error_pattern 12435ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12436{ 12437 const struct ctl_cmd_entry *entry; 12438 ctl_lun_error_pattern filtered_pattern, pattern; 12439 12440 pattern = desc->error_pattern; 12441 12442 /* 12443 * XXX KDM we need more data passed into this function to match a 12444 * custom pattern, and we actually need to implement custom pattern 12445 * matching. 12446 */ 12447 if (pattern & CTL_LUN_PAT_CMD) 12448 return (CTL_LUN_PAT_CMD); 12449 12450 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12451 return (CTL_LUN_PAT_ANY); 12452 12453 entry = ctl_get_cmd_entry(ctsio, NULL); 12454 12455 filtered_pattern = entry->pattern & pattern; 12456 12457 /* 12458 * If the user requested specific flags in the pattern (e.g. 12459 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12460 * flags. 12461 * 12462 * If the user did not specify any flags, it doesn't matter whether 12463 * or not the command supports the flags. 12464 */ 12465 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12466 (pattern & ~CTL_LUN_PAT_MASK)) 12467 return (CTL_LUN_PAT_NONE); 12468 12469 /* 12470 * If the user asked for a range check, see if the requested LBA 12471 * range overlaps with this command's LBA range. 12472 */ 12473 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12474 uint64_t lba1; 12475 uint64_t len1; 12476 ctl_action action; 12477 int retval; 12478 12479 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12480 if (retval != 0) 12481 return (CTL_LUN_PAT_NONE); 12482 12483 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12484 desc->lba_range.len, FALSE); 12485 /* 12486 * A "pass" means that the LBA ranges don't overlap, so 12487 * this doesn't match the user's range criteria. 12488 */ 12489 if (action == CTL_ACTION_PASS) 12490 return (CTL_LUN_PAT_NONE); 12491 } 12492 12493 return (filtered_pattern); 12494} 12495 12496static void 12497ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12498{ 12499 struct ctl_error_desc *desc, *desc2; 12500 12501 mtx_assert(&lun->lun_lock, MA_OWNED); 12502 12503 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12504 ctl_lun_error_pattern pattern; 12505 /* 12506 * Check to see whether this particular command matches 12507 * the pattern in the descriptor. 12508 */ 12509 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12510 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12511 continue; 12512 12513 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12514 case CTL_LUN_INJ_ABORTED: 12515 ctl_set_aborted(&io->scsiio); 12516 break; 12517 case CTL_LUN_INJ_MEDIUM_ERR: 12518 ctl_set_medium_error(&io->scsiio, 12519 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12520 CTL_FLAG_DATA_OUT); 12521 break; 12522 case CTL_LUN_INJ_UA: 12523 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12524 * OCCURRED */ 12525 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12526 break; 12527 case CTL_LUN_INJ_CUSTOM: 12528 /* 12529 * We're assuming the user knows what he is doing. 12530 * Just copy the sense information without doing 12531 * checks. 12532 */ 12533 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12534 MIN(sizeof(desc->custom_sense), 12535 sizeof(io->scsiio.sense_data))); 12536 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12537 io->scsiio.sense_len = SSD_FULL_SIZE; 12538 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12539 break; 12540 case CTL_LUN_INJ_NONE: 12541 default: 12542 /* 12543 * If this is an error injection type we don't know 12544 * about, clear the continuous flag (if it is set) 12545 * so it will get deleted below. 12546 */ 12547 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12548 break; 12549 } 12550 /* 12551 * By default, each error injection action is a one-shot 12552 */ 12553 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12554 continue; 12555 12556 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12557 12558 free(desc, M_CTL); 12559 } 12560} 12561 12562#ifdef CTL_IO_DELAY 12563static void 12564ctl_datamove_timer_wakeup(void *arg) 12565{ 12566 union ctl_io *io; 12567 12568 io = (union ctl_io *)arg; 12569 12570 ctl_datamove(io); 12571} 12572#endif /* CTL_IO_DELAY */ 12573 12574void 12575ctl_datamove(union ctl_io *io) 12576{ 12577 void (*fe_datamove)(union ctl_io *io); 12578 12579 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12580 12581 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12582 12583#ifdef CTL_TIME_IO 12584 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12585 char str[256]; 12586 char path_str[64]; 12587 struct sbuf sb; 12588 12589 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12590 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12591 12592 sbuf_cat(&sb, path_str); 12593 switch (io->io_hdr.io_type) { 12594 case CTL_IO_SCSI: 12595 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12596 sbuf_printf(&sb, "\n"); 12597 sbuf_cat(&sb, path_str); 12598 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12599 io->scsiio.tag_num, io->scsiio.tag_type); 12600 break; 12601 case CTL_IO_TASK: 12602 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12603 "Tag Type: %d\n", io->taskio.task_action, 12604 io->taskio.tag_num, io->taskio.tag_type); 12605 break; 12606 default: 12607 panic("%s: Invalid CTL I/O type %d\n", 12608 __func__, io->io_hdr.io_type); 12609 } 12610 sbuf_cat(&sb, path_str); 12611 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12612 (intmax_t)time_uptime - io->io_hdr.start_time); 12613 sbuf_finish(&sb); 12614 printf("%s", sbuf_data(&sb)); 12615 } 12616#endif /* CTL_TIME_IO */ 12617 12618#ifdef CTL_IO_DELAY 12619 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12620 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12621 } else { 12622 if ((lun != NULL) 12623 && (lun->delay_info.datamove_delay > 0)) { 12624 12625 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12626 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12627 callout_reset(&io->io_hdr.delay_callout, 12628 lun->delay_info.datamove_delay * hz, 12629 ctl_datamove_timer_wakeup, io); 12630 if (lun->delay_info.datamove_type == 12631 CTL_DELAY_TYPE_ONESHOT) 12632 lun->delay_info.datamove_delay = 0; 12633 return; 12634 } 12635 } 12636#endif 12637 12638 /* 12639 * This command has been aborted. Set the port status, so we fail 12640 * the data move. 12641 */ 12642 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12643 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12644 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12645 io->io_hdr.nexus.targ_port, 12646 io->io_hdr.nexus.targ_lun); 12647 io->io_hdr.port_status = 31337; 12648 /* 12649 * Note that the backend, in this case, will get the 12650 * callback in its context. In other cases it may get 12651 * called in the frontend's interrupt thread context. 12652 */ 12653 io->scsiio.be_move_done(io); 12654 return; 12655 } 12656 12657 /* Don't confuse frontend with zero length data move. */ 12658 if (io->scsiio.kern_data_len == 0) { 12659 io->scsiio.be_move_done(io); 12660 return; 12661 } 12662 12663 fe_datamove = CTL_PORT(io)->fe_datamove; 12664 fe_datamove(io); 12665} 12666 12667static void 12668ctl_send_datamove_done(union ctl_io *io, int have_lock) 12669{ 12670 union ctl_ha_msg msg; 12671#ifdef CTL_TIME_IO 12672 struct bintime cur_bt; 12673#endif 12674 12675 memset(&msg, 0, sizeof(msg)); 12676 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12677 msg.hdr.original_sc = io; 12678 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12679 msg.hdr.nexus = io->io_hdr.nexus; 12680 msg.hdr.status = io->io_hdr.status; 12681 msg.scsi.tag_num = io->scsiio.tag_num; 12682 msg.scsi.tag_type = io->scsiio.tag_type; 12683 msg.scsi.scsi_status = io->scsiio.scsi_status; 12684 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12685 io->scsiio.sense_len); 12686 msg.scsi.sense_len = io->scsiio.sense_len; 12687 msg.scsi.sense_residual = io->scsiio.sense_residual; 12688 msg.scsi.fetd_status = io->io_hdr.port_status; 12689 msg.scsi.residual = io->scsiio.residual; 12690 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12691 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12692 ctl_failover_io(io, /*have_lock*/ have_lock); 12693 return; 12694 } 12695 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12696 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12697 msg.scsi.sense_len, M_WAITOK); 12698 12699#ifdef CTL_TIME_IO 12700 getbinuptime(&cur_bt); 12701 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12702 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12703#endif 12704 io->io_hdr.num_dmas++; 12705} 12706 12707/* 12708 * The DMA to the remote side is done, now we need to tell the other side 12709 * we're done so it can continue with its data movement. 12710 */ 12711static void 12712ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12713{ 12714 union ctl_io *io; 12715 uint32_t i; 12716 12717 io = rq->context; 12718 12719 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12720 printf("%s: ISC DMA write failed with error %d", __func__, 12721 rq->ret); 12722 ctl_set_internal_failure(&io->scsiio, 12723 /*sks_valid*/ 1, 12724 /*retry_count*/ rq->ret); 12725 } 12726 12727 ctl_dt_req_free(rq); 12728 12729 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12730 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12731 free(io->io_hdr.remote_sglist, M_CTL); 12732 io->io_hdr.remote_sglist = NULL; 12733 io->io_hdr.local_sglist = NULL; 12734 12735 /* 12736 * The data is in local and remote memory, so now we need to send 12737 * status (good or back) back to the other side. 12738 */ 12739 ctl_send_datamove_done(io, /*have_lock*/ 0); 12740} 12741 12742/* 12743 * We've moved the data from the host/controller into local memory. Now we 12744 * need to push it over to the remote controller's memory. 12745 */ 12746static int 12747ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12748{ 12749 int retval; 12750 12751 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12752 ctl_datamove_remote_write_cb); 12753 return (retval); 12754} 12755 12756static void 12757ctl_datamove_remote_write(union ctl_io *io) 12758{ 12759 int retval; 12760 void (*fe_datamove)(union ctl_io *io); 12761 12762 /* 12763 * - Get the data from the host/HBA into local memory. 12764 * - DMA memory from the local controller to the remote controller. 12765 * - Send status back to the remote controller. 12766 */ 12767 12768 retval = ctl_datamove_remote_sgl_setup(io); 12769 if (retval != 0) 12770 return; 12771 12772 /* Switch the pointer over so the FETD knows what to do */ 12773 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12774 12775 /* 12776 * Use a custom move done callback, since we need to send completion 12777 * back to the other controller, not to the backend on this side. 12778 */ 12779 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12780 12781 fe_datamove = CTL_PORT(io)->fe_datamove; 12782 fe_datamove(io); 12783} 12784 12785static int 12786ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12787{ 12788#if 0 12789 char str[256]; 12790 char path_str[64]; 12791 struct sbuf sb; 12792#endif 12793 uint32_t i; 12794 12795 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12796 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12797 free(io->io_hdr.remote_sglist, M_CTL); 12798 io->io_hdr.remote_sglist = NULL; 12799 io->io_hdr.local_sglist = NULL; 12800 12801#if 0 12802 scsi_path_string(io, path_str, sizeof(path_str)); 12803 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12804 sbuf_cat(&sb, path_str); 12805 scsi_command_string(&io->scsiio, NULL, &sb); 12806 sbuf_printf(&sb, "\n"); 12807 sbuf_cat(&sb, path_str); 12808 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12809 io->scsiio.tag_num, io->scsiio.tag_type); 12810 sbuf_cat(&sb, path_str); 12811 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12812 io->io_hdr.flags, io->io_hdr.status); 12813 sbuf_finish(&sb); 12814 printk("%s", sbuf_data(&sb)); 12815#endif 12816 12817 12818 /* 12819 * The read is done, now we need to send status (good or bad) back 12820 * to the other side. 12821 */ 12822 ctl_send_datamove_done(io, /*have_lock*/ 0); 12823 12824 return (0); 12825} 12826 12827static void 12828ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12829{ 12830 union ctl_io *io; 12831 void (*fe_datamove)(union ctl_io *io); 12832 12833 io = rq->context; 12834 12835 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12836 printf("%s: ISC DMA read failed with error %d\n", __func__, 12837 rq->ret); 12838 ctl_set_internal_failure(&io->scsiio, 12839 /*sks_valid*/ 1, 12840 /*retry_count*/ rq->ret); 12841 } 12842 12843 ctl_dt_req_free(rq); 12844 12845 /* Switch the pointer over so the FETD knows what to do */ 12846 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12847 12848 /* 12849 * Use a custom move done callback, since we need to send completion 12850 * back to the other controller, not to the backend on this side. 12851 */ 12852 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12853 12854 /* XXX KDM add checks like the ones in ctl_datamove? */ 12855 12856 fe_datamove = CTL_PORT(io)->fe_datamove; 12857 fe_datamove(io); 12858} 12859 12860static int 12861ctl_datamove_remote_sgl_setup(union ctl_io *io) 12862{ 12863 struct ctl_sg_entry *local_sglist; 12864 uint32_t len_to_go; 12865 int retval; 12866 int i; 12867 12868 retval = 0; 12869 local_sglist = io->io_hdr.local_sglist; 12870 len_to_go = io->scsiio.kern_data_len; 12871 12872 /* 12873 * The difficult thing here is that the size of the various 12874 * S/G segments may be different than the size from the 12875 * remote controller. That'll make it harder when DMAing 12876 * the data back to the other side. 12877 */ 12878 for (i = 0; len_to_go > 0; i++) { 12879 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12880 local_sglist[i].addr = 12881 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12882 12883 len_to_go -= local_sglist[i].len; 12884 } 12885 /* 12886 * Reset the number of S/G entries accordingly. The original 12887 * number of S/G entries is available in rem_sg_entries. 12888 */ 12889 io->scsiio.kern_sg_entries = i; 12890 12891#if 0 12892 printf("%s: kern_sg_entries = %d\n", __func__, 12893 io->scsiio.kern_sg_entries); 12894 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12895 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12896 local_sglist[i].addr, local_sglist[i].len); 12897#endif 12898 12899 return (retval); 12900} 12901 12902static int 12903ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12904 ctl_ha_dt_cb callback) 12905{ 12906 struct ctl_ha_dt_req *rq; 12907 struct ctl_sg_entry *remote_sglist, *local_sglist; 12908 uint32_t local_used, remote_used, total_used; 12909 int i, j, isc_ret; 12910 12911 rq = ctl_dt_req_alloc(); 12912 12913 /* 12914 * If we failed to allocate the request, and if the DMA didn't fail 12915 * anyway, set busy status. This is just a resource allocation 12916 * failure. 12917 */ 12918 if ((rq == NULL) 12919 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12920 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12921 ctl_set_busy(&io->scsiio); 12922 12923 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12924 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12925 12926 if (rq != NULL) 12927 ctl_dt_req_free(rq); 12928 12929 /* 12930 * The data move failed. We need to return status back 12931 * to the other controller. No point in trying to DMA 12932 * data to the remote controller. 12933 */ 12934 12935 ctl_send_datamove_done(io, /*have_lock*/ 0); 12936 12937 return (1); 12938 } 12939 12940 local_sglist = io->io_hdr.local_sglist; 12941 remote_sglist = io->io_hdr.remote_sglist; 12942 local_used = 0; 12943 remote_used = 0; 12944 total_used = 0; 12945 12946 /* 12947 * Pull/push the data over the wire from/to the other controller. 12948 * This takes into account the possibility that the local and 12949 * remote sglists may not be identical in terms of the size of 12950 * the elements and the number of elements. 12951 * 12952 * One fundamental assumption here is that the length allocated for 12953 * both the local and remote sglists is identical. Otherwise, we've 12954 * essentially got a coding error of some sort. 12955 */ 12956 isc_ret = CTL_HA_STATUS_SUCCESS; 12957 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12958 uint32_t cur_len; 12959 uint8_t *tmp_ptr; 12960 12961 rq->command = command; 12962 rq->context = io; 12963 12964 /* 12965 * Both pointers should be aligned. But it is possible 12966 * that the allocation length is not. They should both 12967 * also have enough slack left over at the end, though, 12968 * to round up to the next 8 byte boundary. 12969 */ 12970 cur_len = MIN(local_sglist[i].len - local_used, 12971 remote_sglist[j].len - remote_used); 12972 rq->size = cur_len; 12973 12974 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12975 tmp_ptr += local_used; 12976 12977#if 0 12978 /* Use physical addresses when talking to ISC hardware */ 12979 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12980 /* XXX KDM use busdma */ 12981 rq->local = vtophys(tmp_ptr); 12982 } else 12983 rq->local = tmp_ptr; 12984#else 12985 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12986 ("HA does not support BUS_ADDR")); 12987 rq->local = tmp_ptr; 12988#endif 12989 12990 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12991 tmp_ptr += remote_used; 12992 rq->remote = tmp_ptr; 12993 12994 rq->callback = NULL; 12995 12996 local_used += cur_len; 12997 if (local_used >= local_sglist[i].len) { 12998 i++; 12999 local_used = 0; 13000 } 13001 13002 remote_used += cur_len; 13003 if (remote_used >= remote_sglist[j].len) { 13004 j++; 13005 remote_used = 0; 13006 } 13007 total_used += cur_len; 13008 13009 if (total_used >= io->scsiio.kern_data_len) 13010 rq->callback = callback; 13011 13012#if 0 13013 printf("%s: %s: local %p remote %p size %d\n", __func__, 13014 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13015 rq->local, rq->remote, rq->size); 13016#endif 13017 13018 isc_ret = ctl_dt_single(rq); 13019 if (isc_ret > CTL_HA_STATUS_SUCCESS) 13020 break; 13021 } 13022 if (isc_ret != CTL_HA_STATUS_WAIT) { 13023 rq->ret = isc_ret; 13024 callback(rq); 13025 } 13026 13027 return (0); 13028} 13029 13030static void 13031ctl_datamove_remote_read(union ctl_io *io) 13032{ 13033 int retval; 13034 uint32_t i; 13035 13036 /* 13037 * This will send an error to the other controller in the case of a 13038 * failure. 13039 */ 13040 retval = ctl_datamove_remote_sgl_setup(io); 13041 if (retval != 0) 13042 return; 13043 13044 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13045 ctl_datamove_remote_read_cb); 13046 if (retval != 0) { 13047 /* 13048 * Make sure we free memory if there was an error.. The 13049 * ctl_datamove_remote_xfer() function will send the 13050 * datamove done message, or call the callback with an 13051 * error if there is a problem. 13052 */ 13053 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13054 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13055 free(io->io_hdr.remote_sglist, M_CTL); 13056 io->io_hdr.remote_sglist = NULL; 13057 io->io_hdr.local_sglist = NULL; 13058 } 13059} 13060 13061/* 13062 * Process a datamove request from the other controller. This is used for 13063 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13064 * first. Once that is complete, the data gets DMAed into the remote 13065 * controller's memory. For reads, we DMA from the remote controller's 13066 * memory into our memory first, and then move it out to the FETD. 13067 */ 13068static void 13069ctl_datamove_remote(union ctl_io *io) 13070{ 13071 13072 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 13073 13074 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13075 ctl_failover_io(io, /*have_lock*/ 0); 13076 return; 13077 } 13078 13079 /* 13080 * Note that we look for an aborted I/O here, but don't do some of 13081 * the other checks that ctl_datamove() normally does. 13082 * We don't need to run the datamove delay code, since that should 13083 * have been done if need be on the other controller. 13084 */ 13085 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13086 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13087 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13088 io->io_hdr.nexus.targ_port, 13089 io->io_hdr.nexus.targ_lun); 13090 io->io_hdr.port_status = 31338; 13091 ctl_send_datamove_done(io, /*have_lock*/ 0); 13092 return; 13093 } 13094 13095 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13096 ctl_datamove_remote_write(io); 13097 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13098 ctl_datamove_remote_read(io); 13099 else { 13100 io->io_hdr.port_status = 31339; 13101 ctl_send_datamove_done(io, /*have_lock*/ 0); 13102 } 13103} 13104 13105static void 13106ctl_process_done(union ctl_io *io) 13107{ 13108 struct ctl_softc *softc = CTL_SOFTC(io); 13109 struct ctl_port *port = CTL_PORT(io); 13110 struct ctl_lun *lun = CTL_LUN(io); 13111 void (*fe_done)(union ctl_io *io); 13112 union ctl_ha_msg msg; 13113 13114 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13115 fe_done = port->fe_done; 13116 13117#ifdef CTL_TIME_IO 13118 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13119 char str[256]; 13120 char path_str[64]; 13121 struct sbuf sb; 13122 13123 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13124 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13125 13126 sbuf_cat(&sb, path_str); 13127 switch (io->io_hdr.io_type) { 13128 case CTL_IO_SCSI: 13129 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13130 sbuf_printf(&sb, "\n"); 13131 sbuf_cat(&sb, path_str); 13132 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13133 io->scsiio.tag_num, io->scsiio.tag_type); 13134 break; 13135 case CTL_IO_TASK: 13136 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13137 "Tag Type: %d\n", io->taskio.task_action, 13138 io->taskio.tag_num, io->taskio.tag_type); 13139 break; 13140 default: 13141 panic("%s: Invalid CTL I/O type %d\n", 13142 __func__, io->io_hdr.io_type); 13143 } 13144 sbuf_cat(&sb, path_str); 13145 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13146 (intmax_t)time_uptime - io->io_hdr.start_time); 13147 sbuf_finish(&sb); 13148 printf("%s", sbuf_data(&sb)); 13149 } 13150#endif /* CTL_TIME_IO */ 13151 13152 switch (io->io_hdr.io_type) { 13153 case CTL_IO_SCSI: 13154 break; 13155 case CTL_IO_TASK: 13156 if (ctl_debug & CTL_DEBUG_INFO) 13157 ctl_io_error_print(io, NULL); 13158 fe_done(io); 13159 return; 13160 default: 13161 panic("%s: Invalid CTL I/O type %d\n", 13162 __func__, io->io_hdr.io_type); 13163 } 13164 13165 if (lun == NULL) { 13166 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13167 io->io_hdr.nexus.targ_mapped_lun)); 13168 goto bailout; 13169 } 13170 13171 mtx_lock(&lun->lun_lock); 13172 13173 /* 13174 * Check to see if we have any informational exception and status 13175 * of this command can be modified to report it in form of either 13176 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13177 */ 13178 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13179 io->io_hdr.status == CTL_SUCCESS && 13180 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13181 uint8_t mrie = lun->MODE_IE.mrie; 13182 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13183 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13184 if (((mrie == SIEP_MRIE_REC_COND && per) || 13185 mrie == SIEP_MRIE_REC_UNCOND || 13186 mrie == SIEP_MRIE_NO_SENSE) && 13187 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13188 CTL_CMD_FLAG_NO_SENSE) == 0) { 13189 ctl_set_sense(&io->scsiio, 13190 /*current_error*/ 1, 13191 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13192 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13193 /*asc*/ lun->ie_asc, 13194 /*ascq*/ lun->ie_ascq, 13195 SSD_ELEM_NONE); 13196 lun->ie_reported = 1; 13197 } 13198 } else if (lun->ie_reported < 0) 13199 lun->ie_reported = 0; 13200 13201 /* 13202 * Check to see if we have any errors to inject here. We only 13203 * inject errors for commands that don't already have errors set. 13204 */ 13205 if (!STAILQ_EMPTY(&lun->error_list) && 13206 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13207 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13208 ctl_inject_error(lun, io); 13209 13210 /* 13211 * XXX KDM how do we treat commands that aren't completed 13212 * successfully? 13213 * 13214 * XXX KDM should we also track I/O latency? 13215 */ 13216 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13217 io->io_hdr.io_type == CTL_IO_SCSI) { 13218 int type; 13219#ifdef CTL_TIME_IO 13220 struct bintime bt; 13221 13222 getbinuptime(&bt); 13223 bintime_sub(&bt, &io->io_hdr.start_bt); 13224#endif 13225 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13226 CTL_FLAG_DATA_IN) 13227 type = CTL_STATS_READ; 13228 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13229 CTL_FLAG_DATA_OUT) 13230 type = CTL_STATS_WRITE; 13231 else 13232 type = CTL_STATS_NO_IO; 13233 13234#ifdef CTL_LEGACY_STATS 13235 uint32_t targ_port = port->targ_port; 13236 lun->legacy_stats.ports[targ_port].bytes[type] += 13237 io->scsiio.kern_total_len; 13238 lun->legacy_stats.ports[targ_port].operations[type] ++; 13239 lun->legacy_stats.ports[targ_port].num_dmas[type] += 13240 io->io_hdr.num_dmas; 13241#ifdef CTL_TIME_IO 13242 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type], 13243 &io->io_hdr.dma_bt); 13244 bintime_add(&lun->legacy_stats.ports[targ_port].time[type], 13245 &bt); 13246#endif 13247#endif /* CTL_LEGACY_STATS */ 13248 13249 lun->stats.bytes[type] += io->scsiio.kern_total_len; 13250 lun->stats.operations[type] ++; 13251 lun->stats.dmas[type] += io->io_hdr.num_dmas; 13252#ifdef CTL_TIME_IO 13253 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt); 13254 bintime_add(&lun->stats.time[type], &bt); 13255#endif 13256 13257 mtx_lock(&port->port_lock); 13258 port->stats.bytes[type] += io->scsiio.kern_total_len; 13259 port->stats.operations[type] ++; 13260 port->stats.dmas[type] += io->io_hdr.num_dmas; 13261#ifdef CTL_TIME_IO 13262 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt); 13263 bintime_add(&port->stats.time[type], &bt); 13264#endif 13265 mtx_unlock(&port->port_lock); 13266 } 13267 13268 /* 13269 * Remove this from the OOA queue. 13270 */ 13271 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13272#ifdef CTL_TIME_IO 13273 if (TAILQ_EMPTY(&lun->ooa_queue)) 13274 lun->last_busy = getsbinuptime(); 13275#endif 13276 13277 /* 13278 * Run through the blocked queue on this LUN and see if anything 13279 * has become unblocked, now that this transaction is done. 13280 */ 13281 ctl_check_blocked(lun); 13282 13283 /* 13284 * If the LUN has been invalidated, free it if there is nothing 13285 * left on its OOA queue. 13286 */ 13287 if ((lun->flags & CTL_LUN_INVALID) 13288 && TAILQ_EMPTY(&lun->ooa_queue)) { 13289 mtx_unlock(&lun->lun_lock); 13290 mtx_lock(&softc->ctl_lock); 13291 ctl_free_lun(lun); 13292 mtx_unlock(&softc->ctl_lock); 13293 } else 13294 mtx_unlock(&lun->lun_lock); 13295 13296bailout: 13297 13298 /* 13299 * If this command has been aborted, make sure we set the status 13300 * properly. The FETD is responsible for freeing the I/O and doing 13301 * whatever it needs to do to clean up its state. 13302 */ 13303 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13304 ctl_set_task_aborted(&io->scsiio); 13305 13306 /* 13307 * If enabled, print command error status. 13308 */ 13309 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13310 (ctl_debug & CTL_DEBUG_INFO) != 0) 13311 ctl_io_error_print(io, NULL); 13312 13313 /* 13314 * Tell the FETD or the other shelf controller we're done with this 13315 * command. Note that only SCSI commands get to this point. Task 13316 * management commands are completed above. 13317 */ 13318 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13319 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13320 memset(&msg, 0, sizeof(msg)); 13321 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13322 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13323 msg.hdr.nexus = io->io_hdr.nexus; 13324 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13325 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13326 M_WAITOK); 13327 } 13328 13329 fe_done(io); 13330} 13331 13332#ifdef CTL_WITH_CA 13333/* 13334 * Front end should call this if it doesn't do autosense. When the request 13335 * sense comes back in from the initiator, we'll dequeue this and send it. 13336 */ 13337int 13338ctl_queue_sense(union ctl_io *io) 13339{ 13340 struct ctl_softc *softc = CTL_SOFTC(io); 13341 struct ctl_port *port = CTL_PORT(io); 13342 struct ctl_lun *lun; 13343 uint32_t initidx, targ_lun; 13344 13345 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13346 13347 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13348 13349 /* 13350 * LUN lookup will likely move to the ctl_work_thread() once we 13351 * have our new queueing infrastructure (that doesn't put things on 13352 * a per-LUN queue initially). That is so that we can handle 13353 * things like an INQUIRY to a LUN that we don't have enabled. We 13354 * can't deal with that right now. 13355 * If we don't have a LUN for this, just toss the sense information. 13356 */ 13357 mtx_lock(&softc->ctl_lock); 13358 if (targ_lun >= CTL_MAX_LUNS || 13359 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13360 mtx_unlock(&softc->ctl_lock); 13361 goto bailout; 13362 } 13363 mtx_lock(&lun->lun_lock); 13364 mtx_unlock(&softc->ctl_lock); 13365 13366 /* 13367 * Already have CA set for this LUN...toss the sense information. 13368 */ 13369 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13370 if (ctl_is_set(lun->have_ca, initidx)) { 13371 mtx_unlock(&lun->lun_lock); 13372 goto bailout; 13373 } 13374 13375 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13376 MIN(sizeof(lun->pending_sense[initidx]), 13377 sizeof(io->scsiio.sense_data))); 13378 ctl_set_mask(lun->have_ca, initidx); 13379 mtx_unlock(&lun->lun_lock); 13380 13381bailout: 13382 ctl_free_io(io); 13383 return (CTL_RETVAL_COMPLETE); 13384} 13385#endif 13386 13387/* 13388 * Primary command inlet from frontend ports. All SCSI and task I/O 13389 * requests must go through this function. 13390 */ 13391int 13392ctl_queue(union ctl_io *io) 13393{ 13394 struct ctl_port *port = CTL_PORT(io); 13395 13396 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13397 13398#ifdef CTL_TIME_IO 13399 io->io_hdr.start_time = time_uptime; 13400 getbinuptime(&io->io_hdr.start_bt); 13401#endif /* CTL_TIME_IO */ 13402 13403 /* Map FE-specific LUN ID into global one. */ 13404 io->io_hdr.nexus.targ_mapped_lun = 13405 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13406 13407 switch (io->io_hdr.io_type) { 13408 case CTL_IO_SCSI: 13409 case CTL_IO_TASK: 13410 if (ctl_debug & CTL_DEBUG_CDB) 13411 ctl_io_print(io); 13412 ctl_enqueue_incoming(io); 13413 break; 13414 default: 13415 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13416 return (EINVAL); 13417 } 13418 13419 return (CTL_RETVAL_COMPLETE); 13420} 13421 13422#ifdef CTL_IO_DELAY 13423static void 13424ctl_done_timer_wakeup(void *arg) 13425{ 13426 union ctl_io *io; 13427 13428 io = (union ctl_io *)arg; 13429 ctl_done(io); 13430} 13431#endif /* CTL_IO_DELAY */ 13432 13433void 13434ctl_serseq_done(union ctl_io *io) 13435{ 13436 struct ctl_lun *lun = CTL_LUN(io);; 13437 13438 if (lun->be_lun == NULL || 13439 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13440 return; 13441 mtx_lock(&lun->lun_lock); 13442 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13443 ctl_check_blocked(lun); 13444 mtx_unlock(&lun->lun_lock); 13445} 13446 13447void 13448ctl_done(union ctl_io *io) 13449{ 13450 13451 /* 13452 * Enable this to catch duplicate completion issues. 13453 */ 13454#if 0 13455 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13456 printf("%s: type %d msg %d cdb %x iptl: " 13457 "%u:%u:%u tag 0x%04x " 13458 "flag %#x status %x\n", 13459 __func__, 13460 io->io_hdr.io_type, 13461 io->io_hdr.msg_type, 13462 io->scsiio.cdb[0], 13463 io->io_hdr.nexus.initid, 13464 io->io_hdr.nexus.targ_port, 13465 io->io_hdr.nexus.targ_lun, 13466 (io->io_hdr.io_type == 13467 CTL_IO_TASK) ? 13468 io->taskio.tag_num : 13469 io->scsiio.tag_num, 13470 io->io_hdr.flags, 13471 io->io_hdr.status); 13472 } else 13473 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13474#endif 13475 13476 /* 13477 * This is an internal copy of an I/O, and should not go through 13478 * the normal done processing logic. 13479 */ 13480 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13481 return; 13482 13483#ifdef CTL_IO_DELAY 13484 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13485 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13486 } else { 13487 struct ctl_lun *lun = CTL_LUN(io); 13488 13489 if ((lun != NULL) 13490 && (lun->delay_info.done_delay > 0)) { 13491 13492 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13493 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13494 callout_reset(&io->io_hdr.delay_callout, 13495 lun->delay_info.done_delay * hz, 13496 ctl_done_timer_wakeup, io); 13497 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13498 lun->delay_info.done_delay = 0; 13499 return; 13500 } 13501 } 13502#endif /* CTL_IO_DELAY */ 13503 13504 ctl_enqueue_done(io); 13505} 13506 13507static void 13508ctl_work_thread(void *arg) 13509{ 13510 struct ctl_thread *thr = (struct ctl_thread *)arg; 13511 struct ctl_softc *softc = thr->ctl_softc; 13512 union ctl_io *io; 13513 int retval; 13514 13515 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13516 13517 for (;;) { 13518 /* 13519 * We handle the queues in this order: 13520 * - ISC 13521 * - done queue (to free up resources, unblock other commands) 13522 * - RtR queue 13523 * - incoming queue 13524 * 13525 * If those queues are empty, we break out of the loop and 13526 * go to sleep. 13527 */ 13528 mtx_lock(&thr->queue_lock); 13529 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13530 if (io != NULL) { 13531 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13532 mtx_unlock(&thr->queue_lock); 13533 ctl_handle_isc(io); 13534 continue; 13535 } 13536 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13537 if (io != NULL) { 13538 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13539 /* clear any blocked commands, call fe_done */ 13540 mtx_unlock(&thr->queue_lock); 13541 ctl_process_done(io); 13542 continue; 13543 } 13544 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13545 if (io != NULL) { 13546 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13547 mtx_unlock(&thr->queue_lock); 13548 if (io->io_hdr.io_type == CTL_IO_TASK) 13549 ctl_run_task(io); 13550 else 13551 ctl_scsiio_precheck(softc, &io->scsiio); 13552 continue; 13553 } 13554 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13555 if (io != NULL) { 13556 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13557 mtx_unlock(&thr->queue_lock); 13558 retval = ctl_scsiio(&io->scsiio); 13559 if (retval != CTL_RETVAL_COMPLETE) 13560 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13561 continue; 13562 } 13563 13564 /* Sleep until we have something to do. */ 13565 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13566 } 13567} 13568 13569static void 13570ctl_lun_thread(void *arg) 13571{ 13572 struct ctl_softc *softc = (struct ctl_softc *)arg; 13573 struct ctl_be_lun *be_lun; 13574 13575 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13576 13577 for (;;) { 13578 mtx_lock(&softc->ctl_lock); 13579 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13580 if (be_lun != NULL) { 13581 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13582 mtx_unlock(&softc->ctl_lock); 13583 ctl_create_lun(be_lun); 13584 continue; 13585 } 13586 13587 /* Sleep until we have something to do. */ 13588 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13589 PDROP | PRIBIO, "-", 0); 13590 } 13591} 13592 13593static void 13594ctl_thresh_thread(void *arg) 13595{ 13596 struct ctl_softc *softc = (struct ctl_softc *)arg; 13597 struct ctl_lun *lun; 13598 struct ctl_logical_block_provisioning_page *page; 13599 const char *attr; 13600 union ctl_ha_msg msg; 13601 uint64_t thres, val; 13602 int i, e, set; 13603 13604 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13605 13606 for (;;) { 13607 mtx_lock(&softc->ctl_lock); 13608 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13609 if ((lun->flags & CTL_LUN_DISABLED) || 13610 (lun->flags & CTL_LUN_NO_MEDIA) || 13611 lun->backend->lun_attr == NULL) 13612 continue; 13613 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13614 softc->ha_mode == CTL_HA_MODE_XFER) 13615 continue; 13616 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13617 continue; 13618 e = 0; 13619 page = &lun->MODE_LBP; 13620 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13621 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13622 continue; 13623 thres = scsi_4btoul(page->descr[i].count); 13624 thres <<= CTL_LBP_EXPONENT; 13625 switch (page->descr[i].resource) { 13626 case 0x01: 13627 attr = "blocksavail"; 13628 break; 13629 case 0x02: 13630 attr = "blocksused"; 13631 break; 13632 case 0xf1: 13633 attr = "poolblocksavail"; 13634 break; 13635 case 0xf2: 13636 attr = "poolblocksused"; 13637 break; 13638 default: 13639 continue; 13640 } 13641 mtx_unlock(&softc->ctl_lock); // XXX 13642 val = lun->backend->lun_attr( 13643 lun->be_lun->be_lun, attr); 13644 mtx_lock(&softc->ctl_lock); 13645 if (val == UINT64_MAX) 13646 continue; 13647 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13648 == SLBPPD_ARMING_INC) 13649 e = (val >= thres); 13650 else 13651 e = (val <= thres); 13652 if (e) 13653 break; 13654 } 13655 mtx_lock(&lun->lun_lock); 13656 if (e) { 13657 scsi_u64to8b((uint8_t *)&page->descr[i] - 13658 (uint8_t *)page, lun->ua_tpt_info); 13659 if (lun->lasttpt == 0 || 13660 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13661 lun->lasttpt = time_uptime; 13662 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13663 set = 1; 13664 } else 13665 set = 0; 13666 } else { 13667 lun->lasttpt = 0; 13668 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13669 set = -1; 13670 } 13671 mtx_unlock(&lun->lun_lock); 13672 if (set != 0 && 13673 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13674 /* Send msg to other side. */ 13675 bzero(&msg.ua, sizeof(msg.ua)); 13676 msg.hdr.msg_type = CTL_MSG_UA; 13677 msg.hdr.nexus.initid = -1; 13678 msg.hdr.nexus.targ_port = -1; 13679 msg.hdr.nexus.targ_lun = lun->lun; 13680 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13681 msg.ua.ua_all = 1; 13682 msg.ua.ua_set = (set > 0); 13683 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13684 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13685 mtx_unlock(&softc->ctl_lock); // XXX 13686 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13687 sizeof(msg.ua), M_WAITOK); 13688 mtx_lock(&softc->ctl_lock); 13689 } 13690 } 13691 mtx_unlock(&softc->ctl_lock); 13692 pause("-", CTL_LBP_PERIOD * hz); 13693 } 13694} 13695 13696static void 13697ctl_enqueue_incoming(union ctl_io *io) 13698{ 13699 struct ctl_softc *softc = CTL_SOFTC(io); 13700 struct ctl_thread *thr; 13701 u_int idx; 13702 13703 idx = (io->io_hdr.nexus.targ_port * 127 + 13704 io->io_hdr.nexus.initid) % worker_threads; 13705 thr = &softc->threads[idx]; 13706 mtx_lock(&thr->queue_lock); 13707 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13708 mtx_unlock(&thr->queue_lock); 13709 wakeup(thr); 13710} 13711 13712static void 13713ctl_enqueue_rtr(union ctl_io *io) 13714{ 13715 struct ctl_softc *softc = CTL_SOFTC(io); 13716 struct ctl_thread *thr; 13717 13718 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13719 mtx_lock(&thr->queue_lock); 13720 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13721 mtx_unlock(&thr->queue_lock); 13722 wakeup(thr); 13723} 13724 13725static void 13726ctl_enqueue_done(union ctl_io *io) 13727{ 13728 struct ctl_softc *softc = CTL_SOFTC(io); 13729 struct ctl_thread *thr; 13730 13731 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13732 mtx_lock(&thr->queue_lock); 13733 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13734 mtx_unlock(&thr->queue_lock); 13735 wakeup(thr); 13736} 13737 13738static void 13739ctl_enqueue_isc(union ctl_io *io) 13740{ 13741 struct ctl_softc *softc = CTL_SOFTC(io); 13742 struct ctl_thread *thr; 13743 13744 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13745 mtx_lock(&thr->queue_lock); 13746 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13747 mtx_unlock(&thr->queue_lock); 13748 wakeup(thr); 13749} 13750 13751/* 13752 * vim: ts=8 13753 */ 13754