ctl.c revision 312838
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37/* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43#define _CTL_C 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: stable/11/sys/cam/ctl/ctl.c 312838 2017-01-26 20:57:19Z 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; 405SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 406 &worker_threads, 1, "Number of worker threads"); 407static int ctl_debug = CTL_DEBUG_NONE; 408SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 409 &ctl_debug, 0, "Enabled debug flags"); 410static int ctl_lun_map_size = 1024; 411SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 412 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 413 414/* 415 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 416 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 417 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 418 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 419 */ 420#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 421 422static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 423 int param); 424static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 425static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 426static int ctl_init(void); 427void ctl_shutdown(void); 428static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 429static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 430static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 431static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 432 struct ctl_ooa *ooa_hdr, 433 struct ctl_ooa_entry *kern_entries); 434static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 435 struct thread *td); 436static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 437 struct ctl_be_lun *be_lun); 438static int ctl_free_lun(struct ctl_lun *lun); 439static void ctl_create_lun(struct ctl_be_lun *be_lun); 440 441static int ctl_do_mode_select(union ctl_io *io); 442static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 443 uint64_t res_key, uint64_t sa_res_key, 444 uint8_t type, uint32_t residx, 445 struct ctl_scsiio *ctsio, 446 struct scsi_per_res_out *cdb, 447 struct scsi_per_res_out_parms* param); 448static void ctl_pro_preempt_other(struct ctl_lun *lun, 449 union ctl_ha_msg *msg); 450static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io); 451static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 452static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 453static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 454static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 455static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 456static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 457 int alloc_len); 458static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 459 int alloc_len); 460static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 461static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 462static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 463static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 464static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 465static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 466 bool seq); 467static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 468static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 469 union ctl_io *pending_io, union ctl_io *ooa_io); 470static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 471 union ctl_io *starting_io); 472static int ctl_check_blocked(struct ctl_lun *lun); 473static int ctl_scsiio_lun_check(struct ctl_lun *lun, 474 const struct ctl_cmd_entry *entry, 475 struct ctl_scsiio *ctsio); 476static void ctl_failover_lun(union ctl_io *io); 477static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 478 struct ctl_scsiio *ctsio); 479static int ctl_scsiio(struct ctl_scsiio *ctsio); 480 481static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 482static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 483 ctl_ua_type ua_type); 484static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 485 ctl_ua_type ua_type); 486static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 487static int ctl_abort_task(union ctl_io *io); 488static int ctl_abort_task_set(union ctl_io *io); 489static int ctl_query_task(union ctl_io *io, int task_set); 490static int ctl_i_t_nexus_reset(union ctl_io *io); 491static int ctl_query_async_event(union ctl_io *io); 492static void ctl_run_task(union ctl_io *io); 493#ifdef CTL_IO_DELAY 494static void ctl_datamove_timer_wakeup(void *arg); 495static void ctl_done_timer_wakeup(void *arg); 496#endif /* CTL_IO_DELAY */ 497 498static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 499static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 500static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 501static void ctl_datamove_remote_write(union ctl_io *io); 502static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 503static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 504static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 505static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 506 ctl_ha_dt_cb callback); 507static void ctl_datamove_remote_read(union ctl_io *io); 508static void ctl_datamove_remote(union ctl_io *io); 509static void ctl_process_done(union ctl_io *io); 510static void ctl_lun_thread(void *arg); 511static void ctl_thresh_thread(void *arg); 512static void ctl_work_thread(void *arg); 513static void ctl_enqueue_incoming(union ctl_io *io); 514static void ctl_enqueue_rtr(union ctl_io *io); 515static void ctl_enqueue_done(union ctl_io *io); 516static void ctl_enqueue_isc(union ctl_io *io); 517static const struct ctl_cmd_entry * 518 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 519static const struct ctl_cmd_entry * 520 ctl_validate_command(struct ctl_scsiio *ctsio); 521static int ctl_cmd_applicable(uint8_t lun_type, 522 const struct ctl_cmd_entry *entry); 523 524static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 525static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 526static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 527static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 528 529/* 530 * Load the serialization table. This isn't very pretty, but is probably 531 * the easiest way to do it. 532 */ 533#include "ctl_ser_table.c" 534 535/* 536 * We only need to define open, close and ioctl routines for this driver. 537 */ 538static struct cdevsw ctl_cdevsw = { 539 .d_version = D_VERSION, 540 .d_flags = 0, 541 .d_open = ctl_open, 542 .d_close = ctl_close, 543 .d_ioctl = ctl_ioctl, 544 .d_name = "ctl", 545}; 546 547 548MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 549 550static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 551 552static moduledata_t ctl_moduledata = { 553 "ctl", 554 ctl_module_event_handler, 555 NULL 556}; 557 558DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 559MODULE_VERSION(ctl, 1); 560 561static struct ctl_frontend ha_frontend = 562{ 563 .name = "ha", 564}; 565 566static void 567ctl_ha_datamove(union ctl_io *io) 568{ 569 struct ctl_lun *lun = CTL_LUN(io); 570 struct ctl_sg_entry *sgl; 571 union ctl_ha_msg msg; 572 uint32_t sg_entries_sent; 573 int do_sg_copy, i, j; 574 575 memset(&msg.dt, 0, sizeof(msg.dt)); 576 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 577 msg.hdr.original_sc = io->io_hdr.original_sc; 578 msg.hdr.serializing_sc = io; 579 msg.hdr.nexus = io->io_hdr.nexus; 580 msg.hdr.status = io->io_hdr.status; 581 msg.dt.flags = io->io_hdr.flags; 582 583 /* 584 * We convert everything into a S/G list here. We can't 585 * pass by reference, only by value between controllers. 586 * So we can't pass a pointer to the S/G list, only as many 587 * S/G entries as we can fit in here. If it's possible for 588 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 589 * then we need to break this up into multiple transfers. 590 */ 591 if (io->scsiio.kern_sg_entries == 0) { 592 msg.dt.kern_sg_entries = 1; 593#if 0 594 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 595 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 596 } else { 597 /* XXX KDM use busdma here! */ 598 msg.dt.sg_list[0].addr = 599 (void *)vtophys(io->scsiio.kern_data_ptr); 600 } 601#else 602 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 603 ("HA does not support BUS_ADDR")); 604 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 605#endif 606 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 607 do_sg_copy = 0; 608 } else { 609 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 610 do_sg_copy = 1; 611 } 612 613 msg.dt.kern_data_len = io->scsiio.kern_data_len; 614 msg.dt.kern_total_len = io->scsiio.kern_total_len; 615 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 616 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 617 msg.dt.sg_sequence = 0; 618 619 /* 620 * Loop until we've sent all of the S/G entries. On the 621 * other end, we'll recompose these S/G entries into one 622 * contiguous list before processing. 623 */ 624 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 625 msg.dt.sg_sequence++) { 626 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 627 sizeof(msg.dt.sg_list[0])), 628 msg.dt.kern_sg_entries - sg_entries_sent); 629 if (do_sg_copy != 0) { 630 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 631 for (i = sg_entries_sent, j = 0; 632 i < msg.dt.cur_sg_entries; i++, j++) { 633#if 0 634 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 635 msg.dt.sg_list[j].addr = sgl[i].addr; 636 } else { 637 /* XXX KDM use busdma here! */ 638 msg.dt.sg_list[j].addr = 639 (void *)vtophys(sgl[i].addr); 640 } 641#else 642 KASSERT((io->io_hdr.flags & 643 CTL_FLAG_BUS_ADDR) == 0, 644 ("HA does not support BUS_ADDR")); 645 msg.dt.sg_list[j].addr = sgl[i].addr; 646#endif 647 msg.dt.sg_list[j].len = sgl[i].len; 648 } 649 } 650 651 sg_entries_sent += msg.dt.cur_sg_entries; 652 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 653 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 654 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 655 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 656 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 657 io->io_hdr.port_status = 31341; 658 io->scsiio.be_move_done(io); 659 return; 660 } 661 msg.dt.sent_sg_entries = sg_entries_sent; 662 } 663 664 /* 665 * Officially handover the request from us to peer. 666 * If failover has just happened, then we must return error. 667 * If failover happen just after, then it is not our problem. 668 */ 669 if (lun) 670 mtx_lock(&lun->lun_lock); 671 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 672 if (lun) 673 mtx_unlock(&lun->lun_lock); 674 io->io_hdr.port_status = 31342; 675 io->scsiio.be_move_done(io); 676 return; 677 } 678 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 679 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 680 if (lun) 681 mtx_unlock(&lun->lun_lock); 682} 683 684static void 685ctl_ha_done(union ctl_io *io) 686{ 687 union ctl_ha_msg msg; 688 689 if (io->io_hdr.io_type == CTL_IO_SCSI) { 690 memset(&msg, 0, sizeof(msg)); 691 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 692 msg.hdr.original_sc = io->io_hdr.original_sc; 693 msg.hdr.nexus = io->io_hdr.nexus; 694 msg.hdr.status = io->io_hdr.status; 695 msg.scsi.scsi_status = io->scsiio.scsi_status; 696 msg.scsi.tag_num = io->scsiio.tag_num; 697 msg.scsi.tag_type = io->scsiio.tag_type; 698 msg.scsi.sense_len = io->scsiio.sense_len; 699 msg.scsi.sense_residual = io->scsiio.sense_residual; 700 msg.scsi.residual = io->scsiio.residual; 701 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 702 io->scsiio.sense_len); 703 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 704 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 705 msg.scsi.sense_len, M_WAITOK); 706 } 707 ctl_free_io(io); 708} 709 710static void 711ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 712 union ctl_ha_msg *msg_info) 713{ 714 struct ctl_scsiio *ctsio; 715 716 if (msg_info->hdr.original_sc == NULL) { 717 printf("%s: original_sc == NULL!\n", __func__); 718 /* XXX KDM now what? */ 719 return; 720 } 721 722 ctsio = &msg_info->hdr.original_sc->scsiio; 723 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 724 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 725 ctsio->io_hdr.status = msg_info->hdr.status; 726 ctsio->scsi_status = msg_info->scsi.scsi_status; 727 ctsio->sense_len = msg_info->scsi.sense_len; 728 ctsio->sense_residual = msg_info->scsi.sense_residual; 729 ctsio->residual = msg_info->scsi.residual; 730 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 731 msg_info->scsi.sense_len); 732 ctl_enqueue_isc((union ctl_io *)ctsio); 733} 734 735static void 736ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 737 union ctl_ha_msg *msg_info) 738{ 739 struct ctl_scsiio *ctsio; 740 741 if (msg_info->hdr.serializing_sc == NULL) { 742 printf("%s: serializing_sc == NULL!\n", __func__); 743 /* XXX KDM now what? */ 744 return; 745 } 746 747 ctsio = &msg_info->hdr.serializing_sc->scsiio; 748 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 749 ctl_enqueue_isc((union ctl_io *)ctsio); 750} 751 752void 753ctl_isc_announce_lun(struct ctl_lun *lun) 754{ 755 struct ctl_softc *softc = lun->ctl_softc; 756 union ctl_ha_msg *msg; 757 struct ctl_ha_msg_lun_pr_key pr_key; 758 int i, k; 759 760 if (softc->ha_link != CTL_HA_LINK_ONLINE) 761 return; 762 mtx_lock(&lun->lun_lock); 763 i = sizeof(msg->lun); 764 if (lun->lun_devid) 765 i += lun->lun_devid->len; 766 i += sizeof(pr_key) * lun->pr_key_count; 767alloc: 768 mtx_unlock(&lun->lun_lock); 769 msg = malloc(i, M_CTL, M_WAITOK); 770 mtx_lock(&lun->lun_lock); 771 k = sizeof(msg->lun); 772 if (lun->lun_devid) 773 k += lun->lun_devid->len; 774 k += sizeof(pr_key) * lun->pr_key_count; 775 if (i < k) { 776 free(msg, M_CTL); 777 i = k; 778 goto alloc; 779 } 780 bzero(&msg->lun, sizeof(msg->lun)); 781 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 782 msg->hdr.nexus.targ_lun = lun->lun; 783 msg->hdr.nexus.targ_mapped_lun = lun->lun; 784 msg->lun.flags = lun->flags; 785 msg->lun.pr_generation = lun->pr_generation; 786 msg->lun.pr_res_idx = lun->pr_res_idx; 787 msg->lun.pr_res_type = lun->pr_res_type; 788 msg->lun.pr_key_count = lun->pr_key_count; 789 i = 0; 790 if (lun->lun_devid) { 791 msg->lun.lun_devid_len = lun->lun_devid->len; 792 memcpy(&msg->lun.data[i], lun->lun_devid->data, 793 msg->lun.lun_devid_len); 794 i += msg->lun.lun_devid_len; 795 } 796 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 797 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 798 continue; 799 pr_key.pr_iid = k; 800 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 801 i += sizeof(pr_key); 802 } 803 mtx_unlock(&lun->lun_lock); 804 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 805 M_WAITOK); 806 free(msg, M_CTL); 807 808 if (lun->flags & CTL_LUN_PRIMARY_SC) { 809 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 810 ctl_isc_announce_mode(lun, -1, 811 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 812 lun->mode_pages.index[i].subpage); 813 } 814 } 815} 816 817void 818ctl_isc_announce_port(struct ctl_port *port) 819{ 820 struct ctl_softc *softc = port->ctl_softc; 821 union ctl_ha_msg *msg; 822 int i; 823 824 if (port->targ_port < softc->port_min || 825 port->targ_port >= softc->port_max || 826 softc->ha_link != CTL_HA_LINK_ONLINE) 827 return; 828 i = sizeof(msg->port) + strlen(port->port_name) + 1; 829 if (port->lun_map) 830 i += port->lun_map_size * sizeof(uint32_t); 831 if (port->port_devid) 832 i += port->port_devid->len; 833 if (port->target_devid) 834 i += port->target_devid->len; 835 if (port->init_devid) 836 i += port->init_devid->len; 837 msg = malloc(i, M_CTL, M_WAITOK); 838 bzero(&msg->port, sizeof(msg->port)); 839 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 840 msg->hdr.nexus.targ_port = port->targ_port; 841 msg->port.port_type = port->port_type; 842 msg->port.physical_port = port->physical_port; 843 msg->port.virtual_port = port->virtual_port; 844 msg->port.status = port->status; 845 i = 0; 846 msg->port.name_len = sprintf(&msg->port.data[i], 847 "%d:%s", softc->ha_id, port->port_name) + 1; 848 i += msg->port.name_len; 849 if (port->lun_map) { 850 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 851 memcpy(&msg->port.data[i], port->lun_map, 852 msg->port.lun_map_len); 853 i += msg->port.lun_map_len; 854 } 855 if (port->port_devid) { 856 msg->port.port_devid_len = port->port_devid->len; 857 memcpy(&msg->port.data[i], port->port_devid->data, 858 msg->port.port_devid_len); 859 i += msg->port.port_devid_len; 860 } 861 if (port->target_devid) { 862 msg->port.target_devid_len = port->target_devid->len; 863 memcpy(&msg->port.data[i], port->target_devid->data, 864 msg->port.target_devid_len); 865 i += msg->port.target_devid_len; 866 } 867 if (port->init_devid) { 868 msg->port.init_devid_len = port->init_devid->len; 869 memcpy(&msg->port.data[i], port->init_devid->data, 870 msg->port.init_devid_len); 871 i += msg->port.init_devid_len; 872 } 873 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 874 M_WAITOK); 875 free(msg, M_CTL); 876} 877 878void 879ctl_isc_announce_iid(struct ctl_port *port, int iid) 880{ 881 struct ctl_softc *softc = port->ctl_softc; 882 union ctl_ha_msg *msg; 883 int i, l; 884 885 if (port->targ_port < softc->port_min || 886 port->targ_port >= softc->port_max || 887 softc->ha_link != CTL_HA_LINK_ONLINE) 888 return; 889 mtx_lock(&softc->ctl_lock); 890 i = sizeof(msg->iid); 891 l = 0; 892 if (port->wwpn_iid[iid].name) 893 l = strlen(port->wwpn_iid[iid].name) + 1; 894 i += l; 895 msg = malloc(i, M_CTL, M_NOWAIT); 896 if (msg == NULL) { 897 mtx_unlock(&softc->ctl_lock); 898 return; 899 } 900 bzero(&msg->iid, sizeof(msg->iid)); 901 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 902 msg->hdr.nexus.targ_port = port->targ_port; 903 msg->hdr.nexus.initid = iid; 904 msg->iid.in_use = port->wwpn_iid[iid].in_use; 905 msg->iid.name_len = l; 906 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 907 if (port->wwpn_iid[iid].name) 908 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 909 mtx_unlock(&softc->ctl_lock); 910 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 911 free(msg, M_CTL); 912} 913 914void 915ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 916 uint8_t page, uint8_t subpage) 917{ 918 struct ctl_softc *softc = lun->ctl_softc; 919 union ctl_ha_msg msg; 920 u_int i; 921 922 if (softc->ha_link != CTL_HA_LINK_ONLINE) 923 return; 924 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 925 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 926 page && lun->mode_pages.index[i].subpage == subpage) 927 break; 928 } 929 if (i == CTL_NUM_MODE_PAGES) 930 return; 931 932 /* Don't try to replicate pages not present on this device. */ 933 if (lun->mode_pages.index[i].page_data == NULL) 934 return; 935 936 bzero(&msg.mode, sizeof(msg.mode)); 937 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 938 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 939 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 940 msg.hdr.nexus.targ_lun = lun->lun; 941 msg.hdr.nexus.targ_mapped_lun = lun->lun; 942 msg.mode.page_code = page; 943 msg.mode.subpage = subpage; 944 msg.mode.page_len = lun->mode_pages.index[i].page_len; 945 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 946 msg.mode.page_len); 947 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 948 M_WAITOK); 949} 950 951static void 952ctl_isc_ha_link_up(struct ctl_softc *softc) 953{ 954 struct ctl_port *port; 955 struct ctl_lun *lun; 956 union ctl_ha_msg msg; 957 int i; 958 959 /* Announce this node parameters to peer for validation. */ 960 msg.login.msg_type = CTL_MSG_LOGIN; 961 msg.login.version = CTL_HA_VERSION; 962 msg.login.ha_mode = softc->ha_mode; 963 msg.login.ha_id = softc->ha_id; 964 msg.login.max_luns = CTL_MAX_LUNS; 965 msg.login.max_ports = CTL_MAX_PORTS; 966 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 967 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 968 M_WAITOK); 969 970 STAILQ_FOREACH(port, &softc->port_list, links) { 971 ctl_isc_announce_port(port); 972 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 973 if (port->wwpn_iid[i].in_use) 974 ctl_isc_announce_iid(port, i); 975 } 976 } 977 STAILQ_FOREACH(lun, &softc->lun_list, links) 978 ctl_isc_announce_lun(lun); 979} 980 981static void 982ctl_isc_ha_link_down(struct ctl_softc *softc) 983{ 984 struct ctl_port *port; 985 struct ctl_lun *lun; 986 union ctl_io *io; 987 int i; 988 989 mtx_lock(&softc->ctl_lock); 990 STAILQ_FOREACH(lun, &softc->lun_list, links) { 991 mtx_lock(&lun->lun_lock); 992 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 993 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 994 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 995 } 996 mtx_unlock(&lun->lun_lock); 997 998 mtx_unlock(&softc->ctl_lock); 999 io = ctl_alloc_io(softc->othersc_pool); 1000 mtx_lock(&softc->ctl_lock); 1001 ctl_zero_io(io); 1002 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1003 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1004 ctl_enqueue_isc(io); 1005 } 1006 1007 STAILQ_FOREACH(port, &softc->port_list, links) { 1008 if (port->targ_port >= softc->port_min && 1009 port->targ_port < softc->port_max) 1010 continue; 1011 port->status &= ~CTL_PORT_STATUS_ONLINE; 1012 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1013 port->wwpn_iid[i].in_use = 0; 1014 free(port->wwpn_iid[i].name, M_CTL); 1015 port->wwpn_iid[i].name = NULL; 1016 } 1017 } 1018 mtx_unlock(&softc->ctl_lock); 1019} 1020 1021static void 1022ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1023{ 1024 struct ctl_lun *lun; 1025 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1026 1027 mtx_lock(&softc->ctl_lock); 1028 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS || 1029 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1030 mtx_unlock(&softc->ctl_lock); 1031 return; 1032 } 1033 mtx_lock(&lun->lun_lock); 1034 mtx_unlock(&softc->ctl_lock); 1035 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1036 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1037 if (msg->ua.ua_all) { 1038 if (msg->ua.ua_set) 1039 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1040 else 1041 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1042 } else { 1043 if (msg->ua.ua_set) 1044 ctl_est_ua(lun, iid, msg->ua.ua_type); 1045 else 1046 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1047 } 1048 mtx_unlock(&lun->lun_lock); 1049} 1050 1051static void 1052ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1053{ 1054 struct ctl_lun *lun; 1055 struct ctl_ha_msg_lun_pr_key pr_key; 1056 int i, k; 1057 ctl_lun_flags oflags; 1058 uint32_t targ_lun; 1059 1060 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1061 mtx_lock(&softc->ctl_lock); 1062 if (targ_lun >= CTL_MAX_LUNS || 1063 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1064 mtx_unlock(&softc->ctl_lock); 1065 return; 1066 } 1067 mtx_lock(&lun->lun_lock); 1068 mtx_unlock(&softc->ctl_lock); 1069 if (lun->flags & CTL_LUN_DISABLED) { 1070 mtx_unlock(&lun->lun_lock); 1071 return; 1072 } 1073 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1074 if (msg->lun.lun_devid_len != i || (i > 0 && 1075 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1076 mtx_unlock(&lun->lun_lock); 1077 printf("%s: Received conflicting HA LUN %d\n", 1078 __func__, targ_lun); 1079 return; 1080 } else { 1081 /* Record whether peer is primary. */ 1082 oflags = lun->flags; 1083 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1084 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1085 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1086 else 1087 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1088 if (oflags != lun->flags) 1089 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1090 1091 /* If peer is primary and we are not -- use data */ 1092 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1093 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1094 lun->pr_generation = msg->lun.pr_generation; 1095 lun->pr_res_idx = msg->lun.pr_res_idx; 1096 lun->pr_res_type = msg->lun.pr_res_type; 1097 lun->pr_key_count = msg->lun.pr_key_count; 1098 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1099 ctl_clr_prkey(lun, k); 1100 for (k = 0; k < msg->lun.pr_key_count; k++) { 1101 memcpy(&pr_key, &msg->lun.data[i], 1102 sizeof(pr_key)); 1103 ctl_alloc_prkey(lun, pr_key.pr_iid); 1104 ctl_set_prkey(lun, pr_key.pr_iid, 1105 pr_key.pr_key); 1106 i += sizeof(pr_key); 1107 } 1108 } 1109 1110 mtx_unlock(&lun->lun_lock); 1111 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1112 __func__, targ_lun, 1113 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1114 "primary" : "secondary")); 1115 1116 /* If we are primary but peer doesn't know -- notify */ 1117 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1118 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1119 ctl_isc_announce_lun(lun); 1120 } 1121} 1122 1123static void 1124ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1125{ 1126 struct ctl_port *port; 1127 struct ctl_lun *lun; 1128 int i, new; 1129 1130 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1131 if (port == NULL) { 1132 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1133 msg->hdr.nexus.targ_port)); 1134 new = 1; 1135 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1136 port->frontend = &ha_frontend; 1137 port->targ_port = msg->hdr.nexus.targ_port; 1138 port->fe_datamove = ctl_ha_datamove; 1139 port->fe_done = ctl_ha_done; 1140 } else if (port->frontend == &ha_frontend) { 1141 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1142 msg->hdr.nexus.targ_port)); 1143 new = 0; 1144 } else { 1145 printf("%s: Received conflicting HA port %d\n", 1146 __func__, msg->hdr.nexus.targ_port); 1147 return; 1148 } 1149 port->port_type = msg->port.port_type; 1150 port->physical_port = msg->port.physical_port; 1151 port->virtual_port = msg->port.virtual_port; 1152 port->status = msg->port.status; 1153 i = 0; 1154 free(port->port_name, M_CTL); 1155 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1156 M_CTL); 1157 i += msg->port.name_len; 1158 if (msg->port.lun_map_len != 0) { 1159 if (port->lun_map == NULL || 1160 port->lun_map_size * sizeof(uint32_t) < 1161 msg->port.lun_map_len) { 1162 port->lun_map_size = 0; 1163 free(port->lun_map, M_CTL); 1164 port->lun_map = malloc(msg->port.lun_map_len, 1165 M_CTL, M_WAITOK); 1166 } 1167 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1168 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1169 i += msg->port.lun_map_len; 1170 } else { 1171 port->lun_map_size = 0; 1172 free(port->lun_map, M_CTL); 1173 port->lun_map = NULL; 1174 } 1175 if (msg->port.port_devid_len != 0) { 1176 if (port->port_devid == NULL || 1177 port->port_devid->len < msg->port.port_devid_len) { 1178 free(port->port_devid, M_CTL); 1179 port->port_devid = malloc(sizeof(struct ctl_devid) + 1180 msg->port.port_devid_len, M_CTL, M_WAITOK); 1181 } 1182 memcpy(port->port_devid->data, &msg->port.data[i], 1183 msg->port.port_devid_len); 1184 port->port_devid->len = msg->port.port_devid_len; 1185 i += msg->port.port_devid_len; 1186 } else { 1187 free(port->port_devid, M_CTL); 1188 port->port_devid = NULL; 1189 } 1190 if (msg->port.target_devid_len != 0) { 1191 if (port->target_devid == NULL || 1192 port->target_devid->len < msg->port.target_devid_len) { 1193 free(port->target_devid, M_CTL); 1194 port->target_devid = malloc(sizeof(struct ctl_devid) + 1195 msg->port.target_devid_len, M_CTL, M_WAITOK); 1196 } 1197 memcpy(port->target_devid->data, &msg->port.data[i], 1198 msg->port.target_devid_len); 1199 port->target_devid->len = msg->port.target_devid_len; 1200 i += msg->port.target_devid_len; 1201 } else { 1202 free(port->target_devid, M_CTL); 1203 port->target_devid = NULL; 1204 } 1205 if (msg->port.init_devid_len != 0) { 1206 if (port->init_devid == NULL || 1207 port->init_devid->len < msg->port.init_devid_len) { 1208 free(port->init_devid, M_CTL); 1209 port->init_devid = malloc(sizeof(struct ctl_devid) + 1210 msg->port.init_devid_len, M_CTL, M_WAITOK); 1211 } 1212 memcpy(port->init_devid->data, &msg->port.data[i], 1213 msg->port.init_devid_len); 1214 port->init_devid->len = msg->port.init_devid_len; 1215 i += msg->port.init_devid_len; 1216 } else { 1217 free(port->init_devid, M_CTL); 1218 port->init_devid = NULL; 1219 } 1220 if (new) { 1221 if (ctl_port_register(port) != 0) { 1222 printf("%s: ctl_port_register() failed with error\n", 1223 __func__); 1224 } 1225 } 1226 mtx_lock(&softc->ctl_lock); 1227 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1228 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1229 continue; 1230 mtx_lock(&lun->lun_lock); 1231 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1232 mtx_unlock(&lun->lun_lock); 1233 } 1234 mtx_unlock(&softc->ctl_lock); 1235} 1236 1237static void 1238ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1239{ 1240 struct ctl_port *port; 1241 int iid; 1242 1243 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1244 if (port == NULL) { 1245 printf("%s: Received IID for unknown port %d\n", 1246 __func__, msg->hdr.nexus.targ_port); 1247 return; 1248 } 1249 iid = msg->hdr.nexus.initid; 1250 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1251 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1252 free(port->wwpn_iid[iid].name, M_CTL); 1253 if (msg->iid.name_len) { 1254 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1255 msg->iid.name_len, M_CTL); 1256 } else 1257 port->wwpn_iid[iid].name = NULL; 1258} 1259 1260static void 1261ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1262{ 1263 1264 if (msg->login.version != CTL_HA_VERSION) { 1265 printf("CTL HA peers have different versions %d != %d\n", 1266 msg->login.version, CTL_HA_VERSION); 1267 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1268 return; 1269 } 1270 if (msg->login.ha_mode != softc->ha_mode) { 1271 printf("CTL HA peers have different ha_mode %d != %d\n", 1272 msg->login.ha_mode, softc->ha_mode); 1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1274 return; 1275 } 1276 if (msg->login.ha_id == softc->ha_id) { 1277 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1278 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1279 return; 1280 } 1281 if (msg->login.max_luns != CTL_MAX_LUNS || 1282 msg->login.max_ports != CTL_MAX_PORTS || 1283 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1284 printf("CTL HA peers have different limits\n"); 1285 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1286 return; 1287 } 1288} 1289 1290static void 1291ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1292{ 1293 struct ctl_lun *lun; 1294 u_int i; 1295 uint32_t initidx, targ_lun; 1296 1297 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1298 mtx_lock(&softc->ctl_lock); 1299 if (targ_lun >= CTL_MAX_LUNS || 1300 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1301 mtx_unlock(&softc->ctl_lock); 1302 return; 1303 } 1304 mtx_lock(&lun->lun_lock); 1305 mtx_unlock(&softc->ctl_lock); 1306 if (lun->flags & CTL_LUN_DISABLED) { 1307 mtx_unlock(&lun->lun_lock); 1308 return; 1309 } 1310 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1311 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1312 msg->mode.page_code && 1313 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1314 break; 1315 } 1316 if (i == CTL_NUM_MODE_PAGES) { 1317 mtx_unlock(&lun->lun_lock); 1318 return; 1319 } 1320 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1321 lun->mode_pages.index[i].page_len); 1322 initidx = ctl_get_initindex(&msg->hdr.nexus); 1323 if (initidx != -1) 1324 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1325 mtx_unlock(&lun->lun_lock); 1326} 1327 1328/* 1329 * ISC (Inter Shelf Communication) event handler. Events from the HA 1330 * subsystem come in here. 1331 */ 1332static void 1333ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1334{ 1335 struct ctl_softc *softc = control_softc; 1336 union ctl_io *io; 1337 struct ctl_prio *presio; 1338 ctl_ha_status isc_status; 1339 1340 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1341 if (event == CTL_HA_EVT_MSG_RECV) { 1342 union ctl_ha_msg *msg, msgbuf; 1343 1344 if (param > sizeof(msgbuf)) 1345 msg = malloc(param, M_CTL, M_WAITOK); 1346 else 1347 msg = &msgbuf; 1348 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1349 M_WAITOK); 1350 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1351 printf("%s: Error receiving message: %d\n", 1352 __func__, isc_status); 1353 if (msg != &msgbuf) 1354 free(msg, M_CTL); 1355 return; 1356 } 1357 1358 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1359 switch (msg->hdr.msg_type) { 1360 case CTL_MSG_SERIALIZE: 1361 io = ctl_alloc_io(softc->othersc_pool); 1362 ctl_zero_io(io); 1363 // populate ctsio from msg 1364 io->io_hdr.io_type = CTL_IO_SCSI; 1365 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1366 io->io_hdr.original_sc = msg->hdr.original_sc; 1367 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1368 CTL_FLAG_IO_ACTIVE; 1369 /* 1370 * If we're in serialization-only mode, we don't 1371 * want to go through full done processing. Thus 1372 * the COPY flag. 1373 * 1374 * XXX KDM add another flag that is more specific. 1375 */ 1376 if (softc->ha_mode != CTL_HA_MODE_XFER) 1377 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1378 io->io_hdr.nexus = msg->hdr.nexus; 1379#if 0 1380 printf("port %u, iid %u, lun %u\n", 1381 io->io_hdr.nexus.targ_port, 1382 io->io_hdr.nexus.initid, 1383 io->io_hdr.nexus.targ_lun); 1384#endif 1385 io->scsiio.tag_num = msg->scsi.tag_num; 1386 io->scsiio.tag_type = msg->scsi.tag_type; 1387#ifdef CTL_TIME_IO 1388 io->io_hdr.start_time = time_uptime; 1389 getbinuptime(&io->io_hdr.start_bt); 1390#endif /* CTL_TIME_IO */ 1391 io->scsiio.cdb_len = msg->scsi.cdb_len; 1392 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1393 CTL_MAX_CDBLEN); 1394 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1395 const struct ctl_cmd_entry *entry; 1396 1397 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1398 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1399 io->io_hdr.flags |= 1400 entry->flags & CTL_FLAG_DATA_MASK; 1401 } 1402 ctl_enqueue_isc(io); 1403 break; 1404 1405 /* Performed on the Originating SC, XFER mode only */ 1406 case CTL_MSG_DATAMOVE: { 1407 struct ctl_sg_entry *sgl; 1408 int i, j; 1409 1410 io = msg->hdr.original_sc; 1411 if (io == NULL) { 1412 printf("%s: original_sc == NULL!\n", __func__); 1413 /* XXX KDM do something here */ 1414 break; 1415 } 1416 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1417 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1418 /* 1419 * Keep track of this, we need to send it back over 1420 * when the datamove is complete. 1421 */ 1422 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1423 if (msg->hdr.status == CTL_SUCCESS) 1424 io->io_hdr.status = msg->hdr.status; 1425 1426 if (msg->dt.sg_sequence == 0) { 1427#ifdef CTL_TIME_IO 1428 getbinuptime(&io->io_hdr.dma_start_bt); 1429#endif 1430 i = msg->dt.kern_sg_entries + 1431 msg->dt.kern_data_len / 1432 CTL_HA_DATAMOVE_SEGMENT + 1; 1433 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1434 M_WAITOK | M_ZERO); 1435 io->io_hdr.remote_sglist = sgl; 1436 io->io_hdr.local_sglist = 1437 &sgl[msg->dt.kern_sg_entries]; 1438 1439 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1440 1441 io->scsiio.kern_sg_entries = 1442 msg->dt.kern_sg_entries; 1443 io->scsiio.rem_sg_entries = 1444 msg->dt.kern_sg_entries; 1445 io->scsiio.kern_data_len = 1446 msg->dt.kern_data_len; 1447 io->scsiio.kern_total_len = 1448 msg->dt.kern_total_len; 1449 io->scsiio.kern_data_resid = 1450 msg->dt.kern_data_resid; 1451 io->scsiio.kern_rel_offset = 1452 msg->dt.kern_rel_offset; 1453 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1454 io->io_hdr.flags |= msg->dt.flags & 1455 CTL_FLAG_BUS_ADDR; 1456 } else 1457 sgl = (struct ctl_sg_entry *) 1458 io->scsiio.kern_data_ptr; 1459 1460 for (i = msg->dt.sent_sg_entries, j = 0; 1461 i < (msg->dt.sent_sg_entries + 1462 msg->dt.cur_sg_entries); i++, j++) { 1463 sgl[i].addr = msg->dt.sg_list[j].addr; 1464 sgl[i].len = msg->dt.sg_list[j].len; 1465 1466#if 0 1467 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1468 __func__, sgl[i].addr, sgl[i].len, j, i); 1469#endif 1470 } 1471 1472 /* 1473 * If this is the last piece of the I/O, we've got 1474 * the full S/G list. Queue processing in the thread. 1475 * Otherwise wait for the next piece. 1476 */ 1477 if (msg->dt.sg_last != 0) 1478 ctl_enqueue_isc(io); 1479 break; 1480 } 1481 /* Performed on the Serializing (primary) SC, XFER mode only */ 1482 case CTL_MSG_DATAMOVE_DONE: { 1483 if (msg->hdr.serializing_sc == NULL) { 1484 printf("%s: serializing_sc == NULL!\n", 1485 __func__); 1486 /* XXX KDM now what? */ 1487 break; 1488 } 1489 /* 1490 * We grab the sense information here in case 1491 * there was a failure, so we can return status 1492 * back to the initiator. 1493 */ 1494 io = msg->hdr.serializing_sc; 1495 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1496 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1497 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1498 io->io_hdr.port_status = msg->scsi.fetd_status; 1499 io->scsiio.residual = msg->scsi.residual; 1500 if (msg->hdr.status != CTL_STATUS_NONE) { 1501 io->io_hdr.status = msg->hdr.status; 1502 io->scsiio.scsi_status = msg->scsi.scsi_status; 1503 io->scsiio.sense_len = msg->scsi.sense_len; 1504 io->scsiio.sense_residual =msg->scsi.sense_residual; 1505 memcpy(&io->scsiio.sense_data, 1506 &msg->scsi.sense_data, 1507 msg->scsi.sense_len); 1508 if (msg->hdr.status == CTL_SUCCESS) 1509 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1510 } 1511 ctl_enqueue_isc(io); 1512 break; 1513 } 1514 1515 /* Preformed on Originating SC, SER_ONLY mode */ 1516 case CTL_MSG_R2R: 1517 io = msg->hdr.original_sc; 1518 if (io == NULL) { 1519 printf("%s: original_sc == NULL!\n", 1520 __func__); 1521 break; 1522 } 1523 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1524 io->io_hdr.msg_type = CTL_MSG_R2R; 1525 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1526 ctl_enqueue_isc(io); 1527 break; 1528 1529 /* 1530 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1531 * mode. 1532 * Performed on the Originating (i.e. secondary) SC in XFER 1533 * mode 1534 */ 1535 case CTL_MSG_FINISH_IO: 1536 if (softc->ha_mode == CTL_HA_MODE_XFER) 1537 ctl_isc_handler_finish_xfer(softc, msg); 1538 else 1539 ctl_isc_handler_finish_ser_only(softc, msg); 1540 break; 1541 1542 /* Preformed on Originating SC */ 1543 case CTL_MSG_BAD_JUJU: 1544 io = msg->hdr.original_sc; 1545 if (io == NULL) { 1546 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1547 __func__); 1548 break; 1549 } 1550 ctl_copy_sense_data(msg, io); 1551 /* 1552 * IO should have already been cleaned up on other 1553 * SC so clear this flag so we won't send a message 1554 * back to finish the IO there. 1555 */ 1556 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1557 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1558 1559 /* io = msg->hdr.serializing_sc; */ 1560 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1561 ctl_enqueue_isc(io); 1562 break; 1563 1564 /* Handle resets sent from the other side */ 1565 case CTL_MSG_MANAGE_TASKS: { 1566 struct ctl_taskio *taskio; 1567 taskio = (struct ctl_taskio *)ctl_alloc_io( 1568 softc->othersc_pool); 1569 ctl_zero_io((union ctl_io *)taskio); 1570 taskio->io_hdr.io_type = CTL_IO_TASK; 1571 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1572 taskio->io_hdr.nexus = msg->hdr.nexus; 1573 taskio->task_action = msg->task.task_action; 1574 taskio->tag_num = msg->task.tag_num; 1575 taskio->tag_type = msg->task.tag_type; 1576#ifdef CTL_TIME_IO 1577 taskio->io_hdr.start_time = time_uptime; 1578 getbinuptime(&taskio->io_hdr.start_bt); 1579#endif /* CTL_TIME_IO */ 1580 ctl_run_task((union ctl_io *)taskio); 1581 break; 1582 } 1583 /* Persistent Reserve action which needs attention */ 1584 case CTL_MSG_PERS_ACTION: 1585 presio = (struct ctl_prio *)ctl_alloc_io( 1586 softc->othersc_pool); 1587 ctl_zero_io((union ctl_io *)presio); 1588 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1589 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1590 presio->io_hdr.nexus = msg->hdr.nexus; 1591 presio->pr_msg = msg->pr; 1592 ctl_enqueue_isc((union ctl_io *)presio); 1593 break; 1594 case CTL_MSG_UA: 1595 ctl_isc_ua(softc, msg, param); 1596 break; 1597 case CTL_MSG_PORT_SYNC: 1598 ctl_isc_port_sync(softc, msg, param); 1599 break; 1600 case CTL_MSG_LUN_SYNC: 1601 ctl_isc_lun_sync(softc, msg, param); 1602 break; 1603 case CTL_MSG_IID_SYNC: 1604 ctl_isc_iid_sync(softc, msg, param); 1605 break; 1606 case CTL_MSG_LOGIN: 1607 ctl_isc_login(softc, msg, param); 1608 break; 1609 case CTL_MSG_MODE_SYNC: 1610 ctl_isc_mode_sync(softc, msg, param); 1611 break; 1612 default: 1613 printf("Received HA message of unknown type %d\n", 1614 msg->hdr.msg_type); 1615 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1616 break; 1617 } 1618 if (msg != &msgbuf) 1619 free(msg, M_CTL); 1620 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1621 printf("CTL: HA link status changed from %d to %d\n", 1622 softc->ha_link, param); 1623 if (param == softc->ha_link) 1624 return; 1625 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1626 softc->ha_link = param; 1627 ctl_isc_ha_link_down(softc); 1628 } else { 1629 softc->ha_link = param; 1630 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1631 ctl_isc_ha_link_up(softc); 1632 } 1633 return; 1634 } else { 1635 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1636 return; 1637 } 1638} 1639 1640static void 1641ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1642{ 1643 1644 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1645 src->scsi.sense_len); 1646 dest->scsiio.scsi_status = src->scsi.scsi_status; 1647 dest->scsiio.sense_len = src->scsi.sense_len; 1648 dest->io_hdr.status = src->hdr.status; 1649} 1650 1651static void 1652ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1653{ 1654 1655 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1656 src->scsiio.sense_len); 1657 dest->scsi.scsi_status = src->scsiio.scsi_status; 1658 dest->scsi.sense_len = src->scsiio.sense_len; 1659 dest->hdr.status = src->io_hdr.status; 1660} 1661 1662void 1663ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1664{ 1665 struct ctl_softc *softc = lun->ctl_softc; 1666 ctl_ua_type *pu; 1667 1668 if (initidx < softc->init_min || initidx >= softc->init_max) 1669 return; 1670 mtx_assert(&lun->lun_lock, MA_OWNED); 1671 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1672 if (pu == NULL) 1673 return; 1674 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1675} 1676 1677void 1678ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1679{ 1680 int i; 1681 1682 mtx_assert(&lun->lun_lock, MA_OWNED); 1683 if (lun->pending_ua[port] == NULL) 1684 return; 1685 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1686 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1687 continue; 1688 lun->pending_ua[port][i] |= ua; 1689 } 1690} 1691 1692void 1693ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1694{ 1695 struct ctl_softc *softc = lun->ctl_softc; 1696 int i; 1697 1698 mtx_assert(&lun->lun_lock, MA_OWNED); 1699 for (i = softc->port_min; i < softc->port_max; i++) 1700 ctl_est_ua_port(lun, i, except, ua); 1701} 1702 1703void 1704ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1705{ 1706 struct ctl_softc *softc = lun->ctl_softc; 1707 ctl_ua_type *pu; 1708 1709 if (initidx < softc->init_min || initidx >= softc->init_max) 1710 return; 1711 mtx_assert(&lun->lun_lock, MA_OWNED); 1712 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1713 if (pu == NULL) 1714 return; 1715 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1716} 1717 1718void 1719ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1720{ 1721 struct ctl_softc *softc = lun->ctl_softc; 1722 int i, j; 1723 1724 mtx_assert(&lun->lun_lock, MA_OWNED); 1725 for (i = softc->port_min; i < softc->port_max; i++) { 1726 if (lun->pending_ua[i] == NULL) 1727 continue; 1728 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1729 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1730 continue; 1731 lun->pending_ua[i][j] &= ~ua; 1732 } 1733 } 1734} 1735 1736void 1737ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1738 ctl_ua_type ua_type) 1739{ 1740 struct ctl_lun *lun; 1741 1742 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1743 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1744 mtx_lock(&lun->lun_lock); 1745 ctl_clr_ua(lun, initidx, ua_type); 1746 mtx_unlock(&lun->lun_lock); 1747 } 1748} 1749 1750static int 1751ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1752{ 1753 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1754 struct ctl_lun *lun; 1755 struct ctl_lun_req ireq; 1756 int error, value; 1757 1758 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1759 error = sysctl_handle_int(oidp, &value, 0, req); 1760 if ((error != 0) || (req->newptr == NULL)) 1761 return (error); 1762 1763 mtx_lock(&softc->ctl_lock); 1764 if (value == 0) 1765 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1766 else 1767 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1768 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1769 mtx_unlock(&softc->ctl_lock); 1770 bzero(&ireq, sizeof(ireq)); 1771 ireq.reqtype = CTL_LUNREQ_MODIFY; 1772 ireq.reqdata.modify.lun_id = lun->lun; 1773 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1774 curthread); 1775 if (ireq.status != CTL_LUN_OK) { 1776 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1777 __func__, ireq.status, ireq.error_str); 1778 } 1779 mtx_lock(&softc->ctl_lock); 1780 } 1781 mtx_unlock(&softc->ctl_lock); 1782 return (0); 1783} 1784 1785static int 1786ctl_init(void) 1787{ 1788 struct make_dev_args args; 1789 struct ctl_softc *softc; 1790 void *other_pool; 1791 int i, error; 1792 1793 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1794 M_WAITOK | M_ZERO); 1795 1796 make_dev_args_init(&args); 1797 args.mda_devsw = &ctl_cdevsw; 1798 args.mda_uid = UID_ROOT; 1799 args.mda_gid = GID_OPERATOR; 1800 args.mda_mode = 0600; 1801 args.mda_si_drv1 = softc; 1802 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1803 if (error != 0) { 1804 free(softc, M_DEVBUF); 1805 control_softc = NULL; 1806 return (error); 1807 } 1808 1809 sysctl_ctx_init(&softc->sysctl_ctx); 1810 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1811 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1812 CTLFLAG_RD, 0, "CAM Target Layer"); 1813 1814 if (softc->sysctl_tree == NULL) { 1815 printf("%s: unable to allocate sysctl tree\n", __func__); 1816 destroy_dev(softc->dev); 1817 free(softc, M_DEVBUF); 1818 control_softc = NULL; 1819 return (ENOMEM); 1820 } 1821 1822 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1823 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1824 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1825 softc->flags = 0; 1826 1827 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1828 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1829 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1830 1831 /* 1832 * In Copan's HA scheme, the "master" and "slave" roles are 1833 * figured out through the slot the controller is in. Although it 1834 * is an active/active system, someone has to be in charge. 1835 */ 1836 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1837 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1838 "HA head ID (0 - no HA)"); 1839 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1840 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1841 softc->is_single = 1; 1842 softc->port_cnt = CTL_MAX_PORTS; 1843 softc->port_min = 0; 1844 } else { 1845 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES; 1846 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1847 } 1848 softc->port_max = softc->port_min + softc->port_cnt; 1849 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1850 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1851 1852 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1853 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1854 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1855 1856 STAILQ_INIT(&softc->lun_list); 1857 STAILQ_INIT(&softc->pending_lun_queue); 1858 STAILQ_INIT(&softc->fe_list); 1859 STAILQ_INIT(&softc->port_list); 1860 STAILQ_INIT(&softc->be_list); 1861 ctl_tpc_init(softc); 1862 1863 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1864 &other_pool) != 0) 1865 { 1866 printf("ctl: can't allocate %d entry other SC pool, " 1867 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1868 return (ENOMEM); 1869 } 1870 softc->othersc_pool = other_pool; 1871 1872 if (worker_threads <= 0) 1873 worker_threads = max(1, mp_ncpus / 4); 1874 if (worker_threads > CTL_MAX_THREADS) 1875 worker_threads = CTL_MAX_THREADS; 1876 1877 for (i = 0; i < worker_threads; i++) { 1878 struct ctl_thread *thr = &softc->threads[i]; 1879 1880 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1881 thr->ctl_softc = softc; 1882 STAILQ_INIT(&thr->incoming_queue); 1883 STAILQ_INIT(&thr->rtr_queue); 1884 STAILQ_INIT(&thr->done_queue); 1885 STAILQ_INIT(&thr->isc_queue); 1886 1887 error = kproc_kthread_add(ctl_work_thread, thr, 1888 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1889 if (error != 0) { 1890 printf("error creating CTL work thread!\n"); 1891 ctl_pool_free(other_pool); 1892 return (error); 1893 } 1894 } 1895 error = kproc_kthread_add(ctl_lun_thread, softc, 1896 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1897 if (error != 0) { 1898 printf("error creating CTL lun thread!\n"); 1899 ctl_pool_free(other_pool); 1900 return (error); 1901 } 1902 error = kproc_kthread_add(ctl_thresh_thread, softc, 1903 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1904 if (error != 0) { 1905 printf("error creating CTL threshold thread!\n"); 1906 ctl_pool_free(other_pool); 1907 return (error); 1908 } 1909 1910 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1911 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1912 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1913 1914 if (softc->is_single == 0) { 1915 ctl_frontend_register(&ha_frontend); 1916 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1917 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1918 softc->is_single = 1; 1919 } else 1920 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1921 != CTL_HA_STATUS_SUCCESS) { 1922 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1923 softc->is_single = 1; 1924 } 1925 } 1926 return (0); 1927} 1928 1929void 1930ctl_shutdown(void) 1931{ 1932 struct ctl_softc *softc = control_softc; 1933 struct ctl_lun *lun, *next_lun; 1934 1935 if (softc->is_single == 0) { 1936 ctl_ha_msg_shutdown(softc); 1937 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1938 != CTL_HA_STATUS_SUCCESS) 1939 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1940 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1941 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1942 ctl_frontend_deregister(&ha_frontend); 1943 } 1944 1945 mtx_lock(&softc->ctl_lock); 1946 1947 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1948 ctl_free_lun(lun); 1949 1950 mtx_unlock(&softc->ctl_lock); 1951 1952#if 0 1953 ctl_shutdown_thread(softc->work_thread); 1954 mtx_destroy(&softc->queue_lock); 1955#endif 1956 1957 ctl_tpc_shutdown(softc); 1958 uma_zdestroy(softc->io_zone); 1959 mtx_destroy(&softc->ctl_lock); 1960 1961 destroy_dev(softc->dev); 1962 1963 sysctl_ctx_free(&softc->sysctl_ctx); 1964 1965 free(softc, M_DEVBUF); 1966 control_softc = NULL; 1967} 1968 1969static int 1970ctl_module_event_handler(module_t mod, int what, void *arg) 1971{ 1972 1973 switch (what) { 1974 case MOD_LOAD: 1975 return (ctl_init()); 1976 case MOD_UNLOAD: 1977 return (EBUSY); 1978 default: 1979 return (EOPNOTSUPP); 1980 } 1981} 1982 1983/* 1984 * XXX KDM should we do some access checks here? Bump a reference count to 1985 * prevent a CTL module from being unloaded while someone has it open? 1986 */ 1987static int 1988ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1989{ 1990 return (0); 1991} 1992 1993static int 1994ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1995{ 1996 return (0); 1997} 1998 1999/* 2000 * Remove an initiator by port number and initiator ID. 2001 * Returns 0 for success, -1 for failure. 2002 */ 2003int 2004ctl_remove_initiator(struct ctl_port *port, int iid) 2005{ 2006 struct ctl_softc *softc = port->ctl_softc; 2007 2008 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2009 2010 if (iid > CTL_MAX_INIT_PER_PORT) { 2011 printf("%s: initiator ID %u > maximun %u!\n", 2012 __func__, iid, CTL_MAX_INIT_PER_PORT); 2013 return (-1); 2014 } 2015 2016 mtx_lock(&softc->ctl_lock); 2017 port->wwpn_iid[iid].in_use--; 2018 port->wwpn_iid[iid].last_use = time_uptime; 2019 mtx_unlock(&softc->ctl_lock); 2020 ctl_isc_announce_iid(port, iid); 2021 2022 return (0); 2023} 2024 2025/* 2026 * Add an initiator to the initiator map. 2027 * Returns iid for success, < 0 for failure. 2028 */ 2029int 2030ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2031{ 2032 struct ctl_softc *softc = port->ctl_softc; 2033 time_t best_time; 2034 int i, best; 2035 2036 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2037 2038 if (iid >= CTL_MAX_INIT_PER_PORT) { 2039 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2040 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2041 free(name, M_CTL); 2042 return (-1); 2043 } 2044 2045 mtx_lock(&softc->ctl_lock); 2046 2047 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2048 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2049 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2050 iid = i; 2051 break; 2052 } 2053 if (name != NULL && port->wwpn_iid[i].name != NULL && 2054 strcmp(name, port->wwpn_iid[i].name) == 0) { 2055 iid = i; 2056 break; 2057 } 2058 } 2059 } 2060 2061 if (iid < 0) { 2062 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2063 if (port->wwpn_iid[i].in_use == 0 && 2064 port->wwpn_iid[i].wwpn == 0 && 2065 port->wwpn_iid[i].name == NULL) { 2066 iid = i; 2067 break; 2068 } 2069 } 2070 } 2071 2072 if (iid < 0) { 2073 best = -1; 2074 best_time = INT32_MAX; 2075 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2076 if (port->wwpn_iid[i].in_use == 0) { 2077 if (port->wwpn_iid[i].last_use < best_time) { 2078 best = i; 2079 best_time = port->wwpn_iid[i].last_use; 2080 } 2081 } 2082 } 2083 iid = best; 2084 } 2085 2086 if (iid < 0) { 2087 mtx_unlock(&softc->ctl_lock); 2088 free(name, M_CTL); 2089 return (-2); 2090 } 2091 2092 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2093 /* 2094 * This is not an error yet. 2095 */ 2096 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2097#if 0 2098 printf("%s: port %d iid %u WWPN %#jx arrived" 2099 " again\n", __func__, port->targ_port, 2100 iid, (uintmax_t)wwpn); 2101#endif 2102 goto take; 2103 } 2104 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2105 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2106#if 0 2107 printf("%s: port %d iid %u name '%s' arrived" 2108 " again\n", __func__, port->targ_port, 2109 iid, name); 2110#endif 2111 goto take; 2112 } 2113 2114 /* 2115 * This is an error, but what do we do about it? The 2116 * driver is telling us we have a new WWPN for this 2117 * initiator ID, so we pretty much need to use it. 2118 */ 2119 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2120 " but WWPN %#jx '%s' is still at that address\n", 2121 __func__, port->targ_port, iid, wwpn, name, 2122 (uintmax_t)port->wwpn_iid[iid].wwpn, 2123 port->wwpn_iid[iid].name); 2124 2125 /* 2126 * XXX KDM clear have_ca and ua_pending on each LUN for 2127 * this initiator. 2128 */ 2129 } 2130take: 2131 free(port->wwpn_iid[iid].name, M_CTL); 2132 port->wwpn_iid[iid].name = name; 2133 port->wwpn_iid[iid].wwpn = wwpn; 2134 port->wwpn_iid[iid].in_use++; 2135 mtx_unlock(&softc->ctl_lock); 2136 ctl_isc_announce_iid(port, iid); 2137 2138 return (iid); 2139} 2140 2141static int 2142ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2143{ 2144 int len; 2145 2146 switch (port->port_type) { 2147 case CTL_PORT_FC: 2148 { 2149 struct scsi_transportid_fcp *id = 2150 (struct scsi_transportid_fcp *)buf; 2151 if (port->wwpn_iid[iid].wwpn == 0) 2152 return (0); 2153 memset(id, 0, sizeof(*id)); 2154 id->format_protocol = SCSI_PROTO_FC; 2155 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2156 return (sizeof(*id)); 2157 } 2158 case CTL_PORT_ISCSI: 2159 { 2160 struct scsi_transportid_iscsi_port *id = 2161 (struct scsi_transportid_iscsi_port *)buf; 2162 if (port->wwpn_iid[iid].name == NULL) 2163 return (0); 2164 memset(id, 0, 256); 2165 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2166 SCSI_PROTO_ISCSI; 2167 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2168 len = roundup2(min(len, 252), 4); 2169 scsi_ulto2b(len, id->additional_length); 2170 return (sizeof(*id) + len); 2171 } 2172 case CTL_PORT_SAS: 2173 { 2174 struct scsi_transportid_sas *id = 2175 (struct scsi_transportid_sas *)buf; 2176 if (port->wwpn_iid[iid].wwpn == 0) 2177 return (0); 2178 memset(id, 0, sizeof(*id)); 2179 id->format_protocol = SCSI_PROTO_SAS; 2180 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2181 return (sizeof(*id)); 2182 } 2183 default: 2184 { 2185 struct scsi_transportid_spi *id = 2186 (struct scsi_transportid_spi *)buf; 2187 memset(id, 0, sizeof(*id)); 2188 id->format_protocol = SCSI_PROTO_SPI; 2189 scsi_ulto2b(iid, id->scsi_addr); 2190 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2191 return (sizeof(*id)); 2192 } 2193 } 2194} 2195 2196/* 2197 * Serialize a command that went down the "wrong" side, and so was sent to 2198 * this controller for execution. The logic is a little different than the 2199 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2200 * sent back to the other side, but in the success case, we execute the 2201 * command on this side (XFER mode) or tell the other side to execute it 2202 * (SER_ONLY mode). 2203 */ 2204static void 2205ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2206{ 2207 struct ctl_softc *softc = CTL_SOFTC(ctsio); 2208 struct ctl_port *port = CTL_PORT(ctsio); 2209 union ctl_ha_msg msg_info; 2210 struct ctl_lun *lun; 2211 const struct ctl_cmd_entry *entry; 2212 uint32_t targ_lun; 2213 2214 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2215 2216 /* Make sure that we know about this port. */ 2217 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2218 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2219 /*retry_count*/ 1); 2220 goto badjuju; 2221 } 2222 2223 /* Make sure that we know about this LUN. */ 2224 mtx_lock(&softc->ctl_lock); 2225 if (targ_lun >= CTL_MAX_LUNS || 2226 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2227 mtx_unlock(&softc->ctl_lock); 2228 2229 /* 2230 * The other node would not send this request to us unless 2231 * received announce that we are primary node for this LUN. 2232 * If this LUN does not exist now, it is probably result of 2233 * a race, so respond to initiator in the most opaque way. 2234 */ 2235 ctl_set_busy(ctsio); 2236 goto badjuju; 2237 } 2238 mtx_lock(&lun->lun_lock); 2239 mtx_unlock(&softc->ctl_lock); 2240 2241 /* 2242 * If the LUN is invalid, pretend that it doesn't exist. 2243 * It will go away as soon as all pending I/Os completed. 2244 */ 2245 if (lun->flags & CTL_LUN_DISABLED) { 2246 mtx_unlock(&lun->lun_lock); 2247 ctl_set_busy(ctsio); 2248 goto badjuju; 2249 } 2250 2251 entry = ctl_get_cmd_entry(ctsio, NULL); 2252 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2253 mtx_unlock(&lun->lun_lock); 2254 goto badjuju; 2255 } 2256 2257 CTL_LUN(ctsio) = lun; 2258 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 2259 2260 /* 2261 * Every I/O goes into the OOA queue for a 2262 * particular LUN, and stays there until completion. 2263 */ 2264#ifdef CTL_TIME_IO 2265 if (TAILQ_EMPTY(&lun->ooa_queue)) 2266 lun->idle_time += getsbinuptime() - lun->last_busy; 2267#endif 2268 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2269 2270 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2271 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2272 ooa_links))) { 2273 case CTL_ACTION_BLOCK: 2274 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2275 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2276 blocked_links); 2277 mtx_unlock(&lun->lun_lock); 2278 break; 2279 case CTL_ACTION_PASS: 2280 case CTL_ACTION_SKIP: 2281 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2282 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2283 ctl_enqueue_rtr((union ctl_io *)ctsio); 2284 mtx_unlock(&lun->lun_lock); 2285 } else { 2286 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2287 mtx_unlock(&lun->lun_lock); 2288 2289 /* send msg back to other side */ 2290 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2291 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2292 msg_info.hdr.msg_type = CTL_MSG_R2R; 2293 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2294 sizeof(msg_info.hdr), M_WAITOK); 2295 } 2296 break; 2297 case CTL_ACTION_OVERLAP: 2298 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2299 mtx_unlock(&lun->lun_lock); 2300 ctl_set_overlapped_cmd(ctsio); 2301 goto badjuju; 2302 case CTL_ACTION_OVERLAP_TAG: 2303 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2304 mtx_unlock(&lun->lun_lock); 2305 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2306 goto badjuju; 2307 case CTL_ACTION_ERROR: 2308 default: 2309 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2310 mtx_unlock(&lun->lun_lock); 2311 2312 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2313 /*retry_count*/ 0); 2314badjuju: 2315 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2316 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2317 msg_info.hdr.serializing_sc = NULL; 2318 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2319 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2320 sizeof(msg_info.scsi), M_WAITOK); 2321 ctl_free_io((union ctl_io *)ctsio); 2322 break; 2323 } 2324} 2325 2326/* 2327 * Returns 0 for success, errno for failure. 2328 */ 2329static void 2330ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2331 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2332{ 2333 union ctl_io *io; 2334 2335 mtx_lock(&lun->lun_lock); 2336 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2337 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2338 ooa_links)) { 2339 struct ctl_ooa_entry *entry; 2340 2341 /* 2342 * If we've got more than we can fit, just count the 2343 * remaining entries. 2344 */ 2345 if (*cur_fill_num >= ooa_hdr->alloc_num) 2346 continue; 2347 2348 entry = &kern_entries[*cur_fill_num]; 2349 2350 entry->tag_num = io->scsiio.tag_num; 2351 entry->lun_num = lun->lun; 2352#ifdef CTL_TIME_IO 2353 entry->start_bt = io->io_hdr.start_bt; 2354#endif 2355 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2356 entry->cdb_len = io->scsiio.cdb_len; 2357 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2358 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2359 2360 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2361 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2362 2363 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2364 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2365 2366 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2367 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2368 2369 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2370 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2371 } 2372 mtx_unlock(&lun->lun_lock); 2373} 2374 2375static void * 2376ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str, 2377 size_t error_str_len) 2378{ 2379 void *kptr; 2380 2381 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2382 2383 if (copyin(user_addr, kptr, len) != 0) { 2384 snprintf(error_str, error_str_len, "Error copying %d bytes " 2385 "from user address %p to kernel address %p", len, 2386 user_addr, kptr); 2387 free(kptr, M_CTL); 2388 return (NULL); 2389 } 2390 2391 return (kptr); 2392} 2393 2394static void 2395ctl_free_args(int num_args, struct ctl_be_arg *args) 2396{ 2397 int i; 2398 2399 if (args == NULL) 2400 return; 2401 2402 for (i = 0; i < num_args; i++) { 2403 free(args[i].kname, M_CTL); 2404 free(args[i].kvalue, M_CTL); 2405 } 2406 2407 free(args, M_CTL); 2408} 2409 2410static struct ctl_be_arg * 2411ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2412 char *error_str, size_t error_str_len) 2413{ 2414 struct ctl_be_arg *args; 2415 int i; 2416 2417 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2418 error_str, error_str_len); 2419 2420 if (args == NULL) 2421 goto bailout; 2422 2423 for (i = 0; i < num_args; i++) { 2424 args[i].kname = NULL; 2425 args[i].kvalue = NULL; 2426 } 2427 2428 for (i = 0; i < num_args; i++) { 2429 uint8_t *tmpptr; 2430 2431 if (args[i].namelen == 0) { 2432 snprintf(error_str, error_str_len, "Argument %d " 2433 "name length is zero", i); 2434 goto bailout; 2435 } 2436 2437 args[i].kname = ctl_copyin_alloc(args[i].name, 2438 args[i].namelen, error_str, error_str_len); 2439 if (args[i].kname == NULL) 2440 goto bailout; 2441 2442 if (args[i].kname[args[i].namelen - 1] != '\0') { 2443 snprintf(error_str, error_str_len, "Argument %d " 2444 "name is not NUL-terminated", i); 2445 goto bailout; 2446 } 2447 2448 if (args[i].flags & CTL_BEARG_RD) { 2449 if (args[i].vallen == 0) { 2450 snprintf(error_str, error_str_len, "Argument %d " 2451 "value length is zero", i); 2452 goto bailout; 2453 } 2454 2455 tmpptr = ctl_copyin_alloc(args[i].value, 2456 args[i].vallen, error_str, error_str_len); 2457 if (tmpptr == NULL) 2458 goto bailout; 2459 2460 if ((args[i].flags & CTL_BEARG_ASCII) 2461 && (tmpptr[args[i].vallen - 1] != '\0')) { 2462 snprintf(error_str, error_str_len, "Argument " 2463 "%d value is not NUL-terminated", i); 2464 free(tmpptr, M_CTL); 2465 goto bailout; 2466 } 2467 args[i].kvalue = tmpptr; 2468 } else { 2469 args[i].kvalue = malloc(args[i].vallen, 2470 M_CTL, M_WAITOK | M_ZERO); 2471 } 2472 } 2473 2474 return (args); 2475bailout: 2476 2477 ctl_free_args(num_args, args); 2478 2479 return (NULL); 2480} 2481 2482static void 2483ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2484{ 2485 int i; 2486 2487 for (i = 0; i < num_args; i++) { 2488 if (args[i].flags & CTL_BEARG_WR) 2489 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2490 } 2491} 2492 2493/* 2494 * Escape characters that are illegal or not recommended in XML. 2495 */ 2496int 2497ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2498{ 2499 char *end = str + size; 2500 int retval; 2501 2502 retval = 0; 2503 2504 for (; *str && str < end; str++) { 2505 switch (*str) { 2506 case '&': 2507 retval = sbuf_printf(sb, "&"); 2508 break; 2509 case '>': 2510 retval = sbuf_printf(sb, ">"); 2511 break; 2512 case '<': 2513 retval = sbuf_printf(sb, "<"); 2514 break; 2515 default: 2516 retval = sbuf_putc(sb, *str); 2517 break; 2518 } 2519 2520 if (retval != 0) 2521 break; 2522 2523 } 2524 2525 return (retval); 2526} 2527 2528static void 2529ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2530{ 2531 struct scsi_vpd_id_descriptor *desc; 2532 int i; 2533 2534 if (id == NULL || id->len < 4) 2535 return; 2536 desc = (struct scsi_vpd_id_descriptor *)id->data; 2537 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2538 case SVPD_ID_TYPE_T10: 2539 sbuf_printf(sb, "t10."); 2540 break; 2541 case SVPD_ID_TYPE_EUI64: 2542 sbuf_printf(sb, "eui."); 2543 break; 2544 case SVPD_ID_TYPE_NAA: 2545 sbuf_printf(sb, "naa."); 2546 break; 2547 case SVPD_ID_TYPE_SCSI_NAME: 2548 break; 2549 } 2550 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2551 case SVPD_ID_CODESET_BINARY: 2552 for (i = 0; i < desc->length; i++) 2553 sbuf_printf(sb, "%02x", desc->identifier[i]); 2554 break; 2555 case SVPD_ID_CODESET_ASCII: 2556 sbuf_printf(sb, "%.*s", (int)desc->length, 2557 (char *)desc->identifier); 2558 break; 2559 case SVPD_ID_CODESET_UTF8: 2560 sbuf_printf(sb, "%s", (char *)desc->identifier); 2561 break; 2562 } 2563} 2564 2565static int 2566ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2567 struct thread *td) 2568{ 2569 struct ctl_softc *softc = dev->si_drv1; 2570 struct ctl_lun *lun; 2571 int retval; 2572 2573 retval = 0; 2574 2575 switch (cmd) { 2576 case CTL_IO: 2577 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2578 break; 2579 case CTL_ENABLE_PORT: 2580 case CTL_DISABLE_PORT: 2581 case CTL_SET_PORT_WWNS: { 2582 struct ctl_port *port; 2583 struct ctl_port_entry *entry; 2584 2585 entry = (struct ctl_port_entry *)addr; 2586 2587 mtx_lock(&softc->ctl_lock); 2588 STAILQ_FOREACH(port, &softc->port_list, links) { 2589 int action, done; 2590 2591 if (port->targ_port < softc->port_min || 2592 port->targ_port >= softc->port_max) 2593 continue; 2594 2595 action = 0; 2596 done = 0; 2597 if ((entry->port_type == CTL_PORT_NONE) 2598 && (entry->targ_port == port->targ_port)) { 2599 /* 2600 * If the user only wants to enable or 2601 * disable or set WWNs on a specific port, 2602 * do the operation and we're done. 2603 */ 2604 action = 1; 2605 done = 1; 2606 } else if (entry->port_type & port->port_type) { 2607 /* 2608 * Compare the user's type mask with the 2609 * particular frontend type to see if we 2610 * have a match. 2611 */ 2612 action = 1; 2613 done = 0; 2614 2615 /* 2616 * Make sure the user isn't trying to set 2617 * WWNs on multiple ports at the same time. 2618 */ 2619 if (cmd == CTL_SET_PORT_WWNS) { 2620 printf("%s: Can't set WWNs on " 2621 "multiple ports\n", __func__); 2622 retval = EINVAL; 2623 break; 2624 } 2625 } 2626 if (action == 0) 2627 continue; 2628 2629 /* 2630 * XXX KDM we have to drop the lock here, because 2631 * the online/offline operations can potentially 2632 * block. We need to reference count the frontends 2633 * so they can't go away, 2634 */ 2635 if (cmd == CTL_ENABLE_PORT) { 2636 mtx_unlock(&softc->ctl_lock); 2637 ctl_port_online(port); 2638 mtx_lock(&softc->ctl_lock); 2639 } else if (cmd == CTL_DISABLE_PORT) { 2640 mtx_unlock(&softc->ctl_lock); 2641 ctl_port_offline(port); 2642 mtx_lock(&softc->ctl_lock); 2643 } else if (cmd == CTL_SET_PORT_WWNS) { 2644 ctl_port_set_wwns(port, 2645 (entry->flags & CTL_PORT_WWNN_VALID) ? 2646 1 : 0, entry->wwnn, 2647 (entry->flags & CTL_PORT_WWPN_VALID) ? 2648 1 : 0, entry->wwpn); 2649 } 2650 if (done != 0) 2651 break; 2652 } 2653 mtx_unlock(&softc->ctl_lock); 2654 break; 2655 } 2656 case CTL_GET_OOA: { 2657 struct ctl_ooa *ooa_hdr; 2658 struct ctl_ooa_entry *entries; 2659 uint32_t cur_fill_num; 2660 2661 ooa_hdr = (struct ctl_ooa *)addr; 2662 2663 if ((ooa_hdr->alloc_len == 0) 2664 || (ooa_hdr->alloc_num == 0)) { 2665 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2666 "must be non-zero\n", __func__, 2667 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2668 retval = EINVAL; 2669 break; 2670 } 2671 2672 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2673 sizeof(struct ctl_ooa_entry))) { 2674 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2675 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2676 __func__, ooa_hdr->alloc_len, 2677 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2678 retval = EINVAL; 2679 break; 2680 } 2681 2682 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2683 if (entries == NULL) { 2684 printf("%s: could not allocate %d bytes for OOA " 2685 "dump\n", __func__, ooa_hdr->alloc_len); 2686 retval = ENOMEM; 2687 break; 2688 } 2689 2690 mtx_lock(&softc->ctl_lock); 2691 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2692 (ooa_hdr->lun_num >= CTL_MAX_LUNS || 2693 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2694 mtx_unlock(&softc->ctl_lock); 2695 free(entries, M_CTL); 2696 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2697 __func__, (uintmax_t)ooa_hdr->lun_num); 2698 retval = EINVAL; 2699 break; 2700 } 2701 2702 cur_fill_num = 0; 2703 2704 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2705 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2706 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2707 ooa_hdr, entries); 2708 } 2709 } else { 2710 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2711 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2712 entries); 2713 } 2714 mtx_unlock(&softc->ctl_lock); 2715 2716 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2717 ooa_hdr->fill_len = ooa_hdr->fill_num * 2718 sizeof(struct ctl_ooa_entry); 2719 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2720 if (retval != 0) { 2721 printf("%s: error copying out %d bytes for OOA dump\n", 2722 __func__, ooa_hdr->fill_len); 2723 } 2724 2725 getbinuptime(&ooa_hdr->cur_bt); 2726 2727 if (cur_fill_num > ooa_hdr->alloc_num) { 2728 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2729 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2730 } else { 2731 ooa_hdr->dropped_num = 0; 2732 ooa_hdr->status = CTL_OOA_OK; 2733 } 2734 2735 free(entries, M_CTL); 2736 break; 2737 } 2738 case CTL_DELAY_IO: { 2739 struct ctl_io_delay_info *delay_info; 2740 2741 delay_info = (struct ctl_io_delay_info *)addr; 2742 2743#ifdef CTL_IO_DELAY 2744 mtx_lock(&softc->ctl_lock); 2745 if (delay_info->lun_id >= CTL_MAX_LUNS || 2746 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2747 mtx_unlock(&softc->ctl_lock); 2748 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2749 break; 2750 } 2751 mtx_lock(&lun->lun_lock); 2752 mtx_unlock(&softc->ctl_lock); 2753 delay_info->status = CTL_DELAY_STATUS_OK; 2754 switch (delay_info->delay_type) { 2755 case CTL_DELAY_TYPE_CONT: 2756 case CTL_DELAY_TYPE_ONESHOT: 2757 break; 2758 default: 2759 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2760 break; 2761 } 2762 switch (delay_info->delay_loc) { 2763 case CTL_DELAY_LOC_DATAMOVE: 2764 lun->delay_info.datamove_type = delay_info->delay_type; 2765 lun->delay_info.datamove_delay = delay_info->delay_secs; 2766 break; 2767 case CTL_DELAY_LOC_DONE: 2768 lun->delay_info.done_type = delay_info->delay_type; 2769 lun->delay_info.done_delay = delay_info->delay_secs; 2770 break; 2771 default: 2772 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2773 break; 2774 } 2775 mtx_unlock(&lun->lun_lock); 2776#else 2777 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2778#endif /* CTL_IO_DELAY */ 2779 break; 2780 } 2781 case CTL_GETSTATS: { 2782 struct ctl_stats *stats = (struct ctl_stats *)addr; 2783 int i; 2784 2785 /* 2786 * XXX KDM no locking here. If the LUN list changes, 2787 * things can blow up. 2788 */ 2789 i = 0; 2790 stats->status = CTL_SS_OK; 2791 stats->fill_len = 0; 2792 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2793 if (stats->fill_len + sizeof(lun->stats) > 2794 stats->alloc_len) { 2795 stats->status = CTL_SS_NEED_MORE_SPACE; 2796 break; 2797 } 2798 retval = copyout(&lun->stats, &stats->lun_stats[i++], 2799 sizeof(lun->stats)); 2800 if (retval != 0) 2801 break; 2802 stats->fill_len += sizeof(lun->stats); 2803 } 2804 stats->num_luns = softc->num_luns; 2805#ifdef CTL_TIME_IO 2806 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2807#else 2808 stats->flags = CTL_STATS_FLAG_NONE; 2809#endif 2810 getnanouptime(&stats->timestamp); 2811 break; 2812 } 2813 case CTL_ERROR_INJECT: { 2814 struct ctl_error_desc *err_desc, *new_err_desc; 2815 2816 err_desc = (struct ctl_error_desc *)addr; 2817 2818 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2819 M_WAITOK | M_ZERO); 2820 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2821 2822 mtx_lock(&softc->ctl_lock); 2823 if (err_desc->lun_id >= CTL_MAX_LUNS || 2824 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2825 mtx_unlock(&softc->ctl_lock); 2826 free(new_err_desc, M_CTL); 2827 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2828 __func__, (uintmax_t)err_desc->lun_id); 2829 retval = EINVAL; 2830 break; 2831 } 2832 mtx_lock(&lun->lun_lock); 2833 mtx_unlock(&softc->ctl_lock); 2834 2835 /* 2836 * We could do some checking here to verify the validity 2837 * of the request, but given the complexity of error 2838 * injection requests, the checking logic would be fairly 2839 * complex. 2840 * 2841 * For now, if the request is invalid, it just won't get 2842 * executed and might get deleted. 2843 */ 2844 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2845 2846 /* 2847 * XXX KDM check to make sure the serial number is unique, 2848 * in case we somehow manage to wrap. That shouldn't 2849 * happen for a very long time, but it's the right thing to 2850 * do. 2851 */ 2852 new_err_desc->serial = lun->error_serial; 2853 err_desc->serial = lun->error_serial; 2854 lun->error_serial++; 2855 2856 mtx_unlock(&lun->lun_lock); 2857 break; 2858 } 2859 case CTL_ERROR_INJECT_DELETE: { 2860 struct ctl_error_desc *delete_desc, *desc, *desc2; 2861 int delete_done; 2862 2863 delete_desc = (struct ctl_error_desc *)addr; 2864 delete_done = 0; 2865 2866 mtx_lock(&softc->ctl_lock); 2867 if (delete_desc->lun_id >= CTL_MAX_LUNS || 2868 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2869 mtx_unlock(&softc->ctl_lock); 2870 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2871 __func__, (uintmax_t)delete_desc->lun_id); 2872 retval = EINVAL; 2873 break; 2874 } 2875 mtx_lock(&lun->lun_lock); 2876 mtx_unlock(&softc->ctl_lock); 2877 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2878 if (desc->serial != delete_desc->serial) 2879 continue; 2880 2881 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2882 links); 2883 free(desc, M_CTL); 2884 delete_done = 1; 2885 } 2886 mtx_unlock(&lun->lun_lock); 2887 if (delete_done == 0) { 2888 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2889 "error serial %ju on LUN %u\n", __func__, 2890 delete_desc->serial, delete_desc->lun_id); 2891 retval = EINVAL; 2892 break; 2893 } 2894 break; 2895 } 2896 case CTL_DUMP_STRUCTS: { 2897 int j, k; 2898 struct ctl_port *port; 2899 struct ctl_frontend *fe; 2900 2901 mtx_lock(&softc->ctl_lock); 2902 printf("CTL Persistent Reservation information start:\n"); 2903 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2904 mtx_lock(&lun->lun_lock); 2905 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2906 mtx_unlock(&lun->lun_lock); 2907 continue; 2908 } 2909 2910 for (j = 0; j < CTL_MAX_PORTS; j++) { 2911 if (lun->pr_keys[j] == NULL) 2912 continue; 2913 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2914 if (lun->pr_keys[j][k] == 0) 2915 continue; 2916 printf(" LUN %ju port %d iid %d key " 2917 "%#jx\n", lun->lun, j, k, 2918 (uintmax_t)lun->pr_keys[j][k]); 2919 } 2920 } 2921 mtx_unlock(&lun->lun_lock); 2922 } 2923 printf("CTL Persistent Reservation information end\n"); 2924 printf("CTL Ports:\n"); 2925 STAILQ_FOREACH(port, &softc->port_list, links) { 2926 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2927 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2928 port->frontend->name, port->port_type, 2929 port->physical_port, port->virtual_port, 2930 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2931 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2932 if (port->wwpn_iid[j].in_use == 0 && 2933 port->wwpn_iid[j].wwpn == 0 && 2934 port->wwpn_iid[j].name == NULL) 2935 continue; 2936 2937 printf(" iid %u use %d WWPN %#jx '%s'\n", 2938 j, port->wwpn_iid[j].in_use, 2939 (uintmax_t)port->wwpn_iid[j].wwpn, 2940 port->wwpn_iid[j].name); 2941 } 2942 } 2943 printf("CTL Port information end\n"); 2944 mtx_unlock(&softc->ctl_lock); 2945 /* 2946 * XXX KDM calling this without a lock. We'd likely want 2947 * to drop the lock before calling the frontend's dump 2948 * routine anyway. 2949 */ 2950 printf("CTL Frontends:\n"); 2951 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2952 printf(" Frontend '%s'\n", fe->name); 2953 if (fe->fe_dump != NULL) 2954 fe->fe_dump(); 2955 } 2956 printf("CTL Frontend information end\n"); 2957 break; 2958 } 2959 case CTL_LUN_REQ: { 2960 struct ctl_lun_req *lun_req; 2961 struct ctl_backend_driver *backend; 2962 2963 lun_req = (struct ctl_lun_req *)addr; 2964 2965 backend = ctl_backend_find(lun_req->backend); 2966 if (backend == NULL) { 2967 lun_req->status = CTL_LUN_ERROR; 2968 snprintf(lun_req->error_str, 2969 sizeof(lun_req->error_str), 2970 "Backend \"%s\" not found.", 2971 lun_req->backend); 2972 break; 2973 } 2974 if (lun_req->num_be_args > 0) { 2975 lun_req->kern_be_args = ctl_copyin_args( 2976 lun_req->num_be_args, 2977 lun_req->be_args, 2978 lun_req->error_str, 2979 sizeof(lun_req->error_str)); 2980 if (lun_req->kern_be_args == NULL) { 2981 lun_req->status = CTL_LUN_ERROR; 2982 break; 2983 } 2984 } 2985 2986 retval = backend->ioctl(dev, cmd, addr, flag, td); 2987 2988 if (lun_req->num_be_args > 0) { 2989 ctl_copyout_args(lun_req->num_be_args, 2990 lun_req->kern_be_args); 2991 ctl_free_args(lun_req->num_be_args, 2992 lun_req->kern_be_args); 2993 } 2994 break; 2995 } 2996 case CTL_LUN_LIST: { 2997 struct sbuf *sb; 2998 struct ctl_lun_list *list; 2999 struct ctl_option *opt; 3000 3001 list = (struct ctl_lun_list *)addr; 3002 3003 /* 3004 * Allocate a fixed length sbuf here, based on the length 3005 * of the user's buffer. We could allocate an auto-extending 3006 * buffer, and then tell the user how much larger our 3007 * amount of data is than his buffer, but that presents 3008 * some problems: 3009 * 3010 * 1. The sbuf(9) routines use a blocking malloc, and so 3011 * we can't hold a lock while calling them with an 3012 * auto-extending buffer. 3013 * 3014 * 2. There is not currently a LUN reference counting 3015 * mechanism, outside of outstanding transactions on 3016 * the LUN's OOA queue. So a LUN could go away on us 3017 * while we're getting the LUN number, backend-specific 3018 * information, etc. Thus, given the way things 3019 * currently work, we need to hold the CTL lock while 3020 * grabbing LUN information. 3021 * 3022 * So, from the user's standpoint, the best thing to do is 3023 * allocate what he thinks is a reasonable buffer length, 3024 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3025 * double the buffer length and try again. (And repeat 3026 * that until he succeeds.) 3027 */ 3028 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3029 if (sb == NULL) { 3030 list->status = CTL_LUN_LIST_ERROR; 3031 snprintf(list->error_str, sizeof(list->error_str), 3032 "Unable to allocate %d bytes for LUN list", 3033 list->alloc_len); 3034 break; 3035 } 3036 3037 sbuf_printf(sb, "<ctllunlist>\n"); 3038 3039 mtx_lock(&softc->ctl_lock); 3040 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3041 mtx_lock(&lun->lun_lock); 3042 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3043 (uintmax_t)lun->lun); 3044 3045 /* 3046 * Bail out as soon as we see that we've overfilled 3047 * the buffer. 3048 */ 3049 if (retval != 0) 3050 break; 3051 3052 retval = sbuf_printf(sb, "\t<backend_type>%s" 3053 "</backend_type>\n", 3054 (lun->backend == NULL) ? "none" : 3055 lun->backend->name); 3056 3057 if (retval != 0) 3058 break; 3059 3060 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3061 lun->be_lun->lun_type); 3062 3063 if (retval != 0) 3064 break; 3065 3066 if (lun->backend == NULL) { 3067 retval = sbuf_printf(sb, "</lun>\n"); 3068 if (retval != 0) 3069 break; 3070 continue; 3071 } 3072 3073 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3074 (lun->be_lun->maxlba > 0) ? 3075 lun->be_lun->maxlba + 1 : 0); 3076 3077 if (retval != 0) 3078 break; 3079 3080 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3081 lun->be_lun->blocksize); 3082 3083 if (retval != 0) 3084 break; 3085 3086 retval = sbuf_printf(sb, "\t<serial_number>"); 3087 3088 if (retval != 0) 3089 break; 3090 3091 retval = ctl_sbuf_printf_esc(sb, 3092 lun->be_lun->serial_num, 3093 sizeof(lun->be_lun->serial_num)); 3094 3095 if (retval != 0) 3096 break; 3097 3098 retval = sbuf_printf(sb, "</serial_number>\n"); 3099 3100 if (retval != 0) 3101 break; 3102 3103 retval = sbuf_printf(sb, "\t<device_id>"); 3104 3105 if (retval != 0) 3106 break; 3107 3108 retval = ctl_sbuf_printf_esc(sb, 3109 lun->be_lun->device_id, 3110 sizeof(lun->be_lun->device_id)); 3111 3112 if (retval != 0) 3113 break; 3114 3115 retval = sbuf_printf(sb, "</device_id>\n"); 3116 3117 if (retval != 0) 3118 break; 3119 3120 if (lun->backend->lun_info != NULL) { 3121 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3122 if (retval != 0) 3123 break; 3124 } 3125 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3126 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3127 opt->name, opt->value, opt->name); 3128 if (retval != 0) 3129 break; 3130 } 3131 3132 retval = sbuf_printf(sb, "</lun>\n"); 3133 3134 if (retval != 0) 3135 break; 3136 mtx_unlock(&lun->lun_lock); 3137 } 3138 if (lun != NULL) 3139 mtx_unlock(&lun->lun_lock); 3140 mtx_unlock(&softc->ctl_lock); 3141 3142 if ((retval != 0) 3143 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3144 retval = 0; 3145 sbuf_delete(sb); 3146 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3147 snprintf(list->error_str, sizeof(list->error_str), 3148 "Out of space, %d bytes is too small", 3149 list->alloc_len); 3150 break; 3151 } 3152 3153 sbuf_finish(sb); 3154 3155 retval = copyout(sbuf_data(sb), list->lun_xml, 3156 sbuf_len(sb) + 1); 3157 3158 list->fill_len = sbuf_len(sb) + 1; 3159 list->status = CTL_LUN_LIST_OK; 3160 sbuf_delete(sb); 3161 break; 3162 } 3163 case CTL_ISCSI: { 3164 struct ctl_iscsi *ci; 3165 struct ctl_frontend *fe; 3166 3167 ci = (struct ctl_iscsi *)addr; 3168 3169 fe = ctl_frontend_find("iscsi"); 3170 if (fe == NULL) { 3171 ci->status = CTL_ISCSI_ERROR; 3172 snprintf(ci->error_str, sizeof(ci->error_str), 3173 "Frontend \"iscsi\" not found."); 3174 break; 3175 } 3176 3177 retval = fe->ioctl(dev, cmd, addr, flag, td); 3178 break; 3179 } 3180 case CTL_PORT_REQ: { 3181 struct ctl_req *req; 3182 struct ctl_frontend *fe; 3183 3184 req = (struct ctl_req *)addr; 3185 3186 fe = ctl_frontend_find(req->driver); 3187 if (fe == NULL) { 3188 req->status = CTL_LUN_ERROR; 3189 snprintf(req->error_str, sizeof(req->error_str), 3190 "Frontend \"%s\" not found.", req->driver); 3191 break; 3192 } 3193 if (req->num_args > 0) { 3194 req->kern_args = ctl_copyin_args(req->num_args, 3195 req->args, req->error_str, sizeof(req->error_str)); 3196 if (req->kern_args == NULL) { 3197 req->status = CTL_LUN_ERROR; 3198 break; 3199 } 3200 } 3201 3202 if (fe->ioctl) 3203 retval = fe->ioctl(dev, cmd, addr, flag, td); 3204 else 3205 retval = ENODEV; 3206 3207 if (req->num_args > 0) { 3208 ctl_copyout_args(req->num_args, req->kern_args); 3209 ctl_free_args(req->num_args, req->kern_args); 3210 } 3211 break; 3212 } 3213 case CTL_PORT_LIST: { 3214 struct sbuf *sb; 3215 struct ctl_port *port; 3216 struct ctl_lun_list *list; 3217 struct ctl_option *opt; 3218 int j; 3219 uint32_t plun; 3220 3221 list = (struct ctl_lun_list *)addr; 3222 3223 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3224 if (sb == NULL) { 3225 list->status = CTL_LUN_LIST_ERROR; 3226 snprintf(list->error_str, sizeof(list->error_str), 3227 "Unable to allocate %d bytes for LUN list", 3228 list->alloc_len); 3229 break; 3230 } 3231 3232 sbuf_printf(sb, "<ctlportlist>\n"); 3233 3234 mtx_lock(&softc->ctl_lock); 3235 STAILQ_FOREACH(port, &softc->port_list, links) { 3236 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3237 (uintmax_t)port->targ_port); 3238 3239 /* 3240 * Bail out as soon as we see that we've overfilled 3241 * the buffer. 3242 */ 3243 if (retval != 0) 3244 break; 3245 3246 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3247 "</frontend_type>\n", port->frontend->name); 3248 if (retval != 0) 3249 break; 3250 3251 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3252 port->port_type); 3253 if (retval != 0) 3254 break; 3255 3256 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3257 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3258 if (retval != 0) 3259 break; 3260 3261 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3262 port->port_name); 3263 if (retval != 0) 3264 break; 3265 3266 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3267 port->physical_port); 3268 if (retval != 0) 3269 break; 3270 3271 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3272 port->virtual_port); 3273 if (retval != 0) 3274 break; 3275 3276 if (port->target_devid != NULL) { 3277 sbuf_printf(sb, "\t<target>"); 3278 ctl_id_sbuf(port->target_devid, sb); 3279 sbuf_printf(sb, "</target>\n"); 3280 } 3281 3282 if (port->port_devid != NULL) { 3283 sbuf_printf(sb, "\t<port>"); 3284 ctl_id_sbuf(port->port_devid, sb); 3285 sbuf_printf(sb, "</port>\n"); 3286 } 3287 3288 if (port->port_info != NULL) { 3289 retval = port->port_info(port->onoff_arg, sb); 3290 if (retval != 0) 3291 break; 3292 } 3293 STAILQ_FOREACH(opt, &port->options, links) { 3294 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3295 opt->name, opt->value, opt->name); 3296 if (retval != 0) 3297 break; 3298 } 3299 3300 if (port->lun_map != NULL) { 3301 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3302 for (j = 0; j < port->lun_map_size; j++) { 3303 plun = ctl_lun_map_from_port(port, j); 3304 if (plun == UINT32_MAX) 3305 continue; 3306 sbuf_printf(sb, 3307 "\t<lun id=\"%u\">%u</lun>\n", 3308 j, plun); 3309 } 3310 } 3311 3312 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3313 if (port->wwpn_iid[j].in_use == 0 || 3314 (port->wwpn_iid[j].wwpn == 0 && 3315 port->wwpn_iid[j].name == NULL)) 3316 continue; 3317 3318 if (port->wwpn_iid[j].name != NULL) 3319 retval = sbuf_printf(sb, 3320 "\t<initiator id=\"%u\">%s</initiator>\n", 3321 j, port->wwpn_iid[j].name); 3322 else 3323 retval = sbuf_printf(sb, 3324 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3325 j, port->wwpn_iid[j].wwpn); 3326 if (retval != 0) 3327 break; 3328 } 3329 if (retval != 0) 3330 break; 3331 3332 retval = sbuf_printf(sb, "</targ_port>\n"); 3333 if (retval != 0) 3334 break; 3335 } 3336 mtx_unlock(&softc->ctl_lock); 3337 3338 if ((retval != 0) 3339 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3340 retval = 0; 3341 sbuf_delete(sb); 3342 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3343 snprintf(list->error_str, sizeof(list->error_str), 3344 "Out of space, %d bytes is too small", 3345 list->alloc_len); 3346 break; 3347 } 3348 3349 sbuf_finish(sb); 3350 3351 retval = copyout(sbuf_data(sb), list->lun_xml, 3352 sbuf_len(sb) + 1); 3353 3354 list->fill_len = sbuf_len(sb) + 1; 3355 list->status = CTL_LUN_LIST_OK; 3356 sbuf_delete(sb); 3357 break; 3358 } 3359 case CTL_LUN_MAP: { 3360 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3361 struct ctl_port *port; 3362 3363 mtx_lock(&softc->ctl_lock); 3364 if (lm->port < softc->port_min || 3365 lm->port >= softc->port_max || 3366 (port = softc->ctl_ports[lm->port]) == NULL) { 3367 mtx_unlock(&softc->ctl_lock); 3368 return (ENXIO); 3369 } 3370 if (port->status & CTL_PORT_STATUS_ONLINE) { 3371 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3372 if (ctl_lun_map_to_port(port, lun->lun) == 3373 UINT32_MAX) 3374 continue; 3375 mtx_lock(&lun->lun_lock); 3376 ctl_est_ua_port(lun, lm->port, -1, 3377 CTL_UA_LUN_CHANGE); 3378 mtx_unlock(&lun->lun_lock); 3379 } 3380 } 3381 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3382 if (lm->plun != UINT32_MAX) { 3383 if (lm->lun == UINT32_MAX) 3384 retval = ctl_lun_map_unset(port, lm->plun); 3385 else if (lm->lun < CTL_MAX_LUNS && 3386 softc->ctl_luns[lm->lun] != NULL) 3387 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3388 else 3389 return (ENXIO); 3390 } else { 3391 if (lm->lun == UINT32_MAX) 3392 retval = ctl_lun_map_deinit(port); 3393 else 3394 retval = ctl_lun_map_init(port); 3395 } 3396 if (port->status & CTL_PORT_STATUS_ONLINE) 3397 ctl_isc_announce_port(port); 3398 break; 3399 } 3400 default: { 3401 /* XXX KDM should we fix this? */ 3402#if 0 3403 struct ctl_backend_driver *backend; 3404 unsigned int type; 3405 int found; 3406 3407 found = 0; 3408 3409 /* 3410 * We encode the backend type as the ioctl type for backend 3411 * ioctls. So parse it out here, and then search for a 3412 * backend of this type. 3413 */ 3414 type = _IOC_TYPE(cmd); 3415 3416 STAILQ_FOREACH(backend, &softc->be_list, links) { 3417 if (backend->type == type) { 3418 found = 1; 3419 break; 3420 } 3421 } 3422 if (found == 0) { 3423 printf("ctl: unknown ioctl command %#lx or backend " 3424 "%d\n", cmd, type); 3425 retval = EINVAL; 3426 break; 3427 } 3428 retval = backend->ioctl(dev, cmd, addr, flag, td); 3429#endif 3430 retval = ENOTTY; 3431 break; 3432 } 3433 } 3434 return (retval); 3435} 3436 3437uint32_t 3438ctl_get_initindex(struct ctl_nexus *nexus) 3439{ 3440 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3441} 3442 3443int 3444ctl_lun_map_init(struct ctl_port *port) 3445{ 3446 struct ctl_softc *softc = port->ctl_softc; 3447 struct ctl_lun *lun; 3448 int size = ctl_lun_map_size; 3449 uint32_t i; 3450 3451 if (port->lun_map == NULL || port->lun_map_size < size) { 3452 port->lun_map_size = 0; 3453 free(port->lun_map, M_CTL); 3454 port->lun_map = malloc(size * sizeof(uint32_t), 3455 M_CTL, M_NOWAIT); 3456 } 3457 if (port->lun_map == NULL) 3458 return (ENOMEM); 3459 for (i = 0; i < size; i++) 3460 port->lun_map[i] = UINT32_MAX; 3461 port->lun_map_size = size; 3462 if (port->status & CTL_PORT_STATUS_ONLINE) { 3463 if (port->lun_disable != NULL) { 3464 STAILQ_FOREACH(lun, &softc->lun_list, links) 3465 port->lun_disable(port->targ_lun_arg, lun->lun); 3466 } 3467 ctl_isc_announce_port(port); 3468 } 3469 return (0); 3470} 3471 3472int 3473ctl_lun_map_deinit(struct ctl_port *port) 3474{ 3475 struct ctl_softc *softc = port->ctl_softc; 3476 struct ctl_lun *lun; 3477 3478 if (port->lun_map == NULL) 3479 return (0); 3480 port->lun_map_size = 0; 3481 free(port->lun_map, M_CTL); 3482 port->lun_map = NULL; 3483 if (port->status & CTL_PORT_STATUS_ONLINE) { 3484 if (port->lun_enable != NULL) { 3485 STAILQ_FOREACH(lun, &softc->lun_list, links) 3486 port->lun_enable(port->targ_lun_arg, lun->lun); 3487 } 3488 ctl_isc_announce_port(port); 3489 } 3490 return (0); 3491} 3492 3493int 3494ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3495{ 3496 int status; 3497 uint32_t old; 3498 3499 if (port->lun_map == NULL) { 3500 status = ctl_lun_map_init(port); 3501 if (status != 0) 3502 return (status); 3503 } 3504 if (plun >= port->lun_map_size) 3505 return (EINVAL); 3506 old = port->lun_map[plun]; 3507 port->lun_map[plun] = glun; 3508 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3509 if (port->lun_enable != NULL) 3510 port->lun_enable(port->targ_lun_arg, plun); 3511 ctl_isc_announce_port(port); 3512 } 3513 return (0); 3514} 3515 3516int 3517ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3518{ 3519 uint32_t old; 3520 3521 if (port->lun_map == NULL || plun >= port->lun_map_size) 3522 return (0); 3523 old = port->lun_map[plun]; 3524 port->lun_map[plun] = UINT32_MAX; 3525 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3526 if (port->lun_disable != NULL) 3527 port->lun_disable(port->targ_lun_arg, plun); 3528 ctl_isc_announce_port(port); 3529 } 3530 return (0); 3531} 3532 3533uint32_t 3534ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3535{ 3536 3537 if (port == NULL) 3538 return (UINT32_MAX); 3539 if (port->lun_map == NULL) 3540 return (lun_id); 3541 if (lun_id > port->lun_map_size) 3542 return (UINT32_MAX); 3543 return (port->lun_map[lun_id]); 3544} 3545 3546uint32_t 3547ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3548{ 3549 uint32_t i; 3550 3551 if (port == NULL) 3552 return (UINT32_MAX); 3553 if (port->lun_map == NULL) 3554 return (lun_id); 3555 for (i = 0; i < port->lun_map_size; i++) { 3556 if (port->lun_map[i] == lun_id) 3557 return (i); 3558 } 3559 return (UINT32_MAX); 3560} 3561 3562uint32_t 3563ctl_decode_lun(uint64_t encoded) 3564{ 3565 uint8_t lun[8]; 3566 uint32_t result = 0xffffffff; 3567 3568 be64enc(lun, encoded); 3569 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3570 case RPL_LUNDATA_ATYP_PERIPH: 3571 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3572 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3573 result = lun[1]; 3574 break; 3575 case RPL_LUNDATA_ATYP_FLAT: 3576 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3577 lun[6] == 0 && lun[7] == 0) 3578 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3579 break; 3580 case RPL_LUNDATA_ATYP_EXTLUN: 3581 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3582 case 0x02: 3583 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3584 case 0x00: 3585 result = lun[1]; 3586 break; 3587 case 0x10: 3588 result = (lun[1] << 16) + (lun[2] << 8) + 3589 lun[3]; 3590 break; 3591 case 0x20: 3592 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3593 result = (lun[2] << 24) + 3594 (lun[3] << 16) + (lun[4] << 8) + 3595 lun[5]; 3596 break; 3597 } 3598 break; 3599 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3600 result = 0xffffffff; 3601 break; 3602 } 3603 break; 3604 } 3605 return (result); 3606} 3607 3608uint64_t 3609ctl_encode_lun(uint32_t decoded) 3610{ 3611 uint64_t l = decoded; 3612 3613 if (l <= 0xff) 3614 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3615 if (l <= 0x3fff) 3616 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3617 if (l <= 0xffffff) 3618 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3619 (l << 32)); 3620 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3621} 3622 3623int 3624ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3625{ 3626 int i; 3627 3628 for (i = first; i < last; i++) { 3629 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3630 return (i); 3631 } 3632 return (-1); 3633} 3634 3635int 3636ctl_set_mask(uint32_t *mask, uint32_t bit) 3637{ 3638 uint32_t chunk, piece; 3639 3640 chunk = bit >> 5; 3641 piece = bit % (sizeof(uint32_t) * 8); 3642 3643 if ((mask[chunk] & (1 << piece)) != 0) 3644 return (-1); 3645 else 3646 mask[chunk] |= (1 << piece); 3647 3648 return (0); 3649} 3650 3651int 3652ctl_clear_mask(uint32_t *mask, uint32_t bit) 3653{ 3654 uint32_t chunk, piece; 3655 3656 chunk = bit >> 5; 3657 piece = bit % (sizeof(uint32_t) * 8); 3658 3659 if ((mask[chunk] & (1 << piece)) == 0) 3660 return (-1); 3661 else 3662 mask[chunk] &= ~(1 << piece); 3663 3664 return (0); 3665} 3666 3667int 3668ctl_is_set(uint32_t *mask, uint32_t bit) 3669{ 3670 uint32_t chunk, piece; 3671 3672 chunk = bit >> 5; 3673 piece = bit % (sizeof(uint32_t) * 8); 3674 3675 if ((mask[chunk] & (1 << piece)) == 0) 3676 return (0); 3677 else 3678 return (1); 3679} 3680 3681static uint64_t 3682ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3683{ 3684 uint64_t *t; 3685 3686 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3687 if (t == NULL) 3688 return (0); 3689 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3690} 3691 3692static void 3693ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3694{ 3695 uint64_t *t; 3696 3697 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3698 if (t == NULL) 3699 return; 3700 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3701} 3702 3703static void 3704ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3705{ 3706 uint64_t *p; 3707 u_int i; 3708 3709 i = residx/CTL_MAX_INIT_PER_PORT; 3710 if (lun->pr_keys[i] != NULL) 3711 return; 3712 mtx_unlock(&lun->lun_lock); 3713 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3714 M_WAITOK | M_ZERO); 3715 mtx_lock(&lun->lun_lock); 3716 if (lun->pr_keys[i] == NULL) 3717 lun->pr_keys[i] = p; 3718 else 3719 free(p, M_CTL); 3720} 3721 3722static void 3723ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3724{ 3725 uint64_t *t; 3726 3727 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3728 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3729 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3730} 3731 3732/* 3733 * ctl_softc, pool_name, total_ctl_io are passed in. 3734 * npool is passed out. 3735 */ 3736int 3737ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3738 uint32_t total_ctl_io, void **npool) 3739{ 3740 struct ctl_io_pool *pool; 3741 3742 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3743 M_NOWAIT | M_ZERO); 3744 if (pool == NULL) 3745 return (ENOMEM); 3746 3747 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3748 pool->ctl_softc = ctl_softc; 3749#ifdef IO_POOLS 3750 pool->zone = uma_zsecond_create(pool->name, NULL, 3751 NULL, NULL, NULL, ctl_softc->io_zone); 3752 /* uma_prealloc(pool->zone, total_ctl_io); */ 3753#else 3754 pool->zone = ctl_softc->io_zone; 3755#endif 3756 3757 *npool = pool; 3758 return (0); 3759} 3760 3761void 3762ctl_pool_free(struct ctl_io_pool *pool) 3763{ 3764 3765 if (pool == NULL) 3766 return; 3767 3768#ifdef IO_POOLS 3769 uma_zdestroy(pool->zone); 3770#endif 3771 free(pool, M_CTL); 3772} 3773 3774union ctl_io * 3775ctl_alloc_io(void *pool_ref) 3776{ 3777 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3778 union ctl_io *io; 3779 3780 io = uma_zalloc(pool->zone, M_WAITOK); 3781 if (io != NULL) { 3782 io->io_hdr.pool = pool_ref; 3783 CTL_SOFTC(io) = pool->ctl_softc; 3784 } 3785 return (io); 3786} 3787 3788union ctl_io * 3789ctl_alloc_io_nowait(void *pool_ref) 3790{ 3791 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3792 union ctl_io *io; 3793 3794 io = uma_zalloc(pool->zone, M_NOWAIT); 3795 if (io != NULL) { 3796 io->io_hdr.pool = pool_ref; 3797 CTL_SOFTC(io) = pool->ctl_softc; 3798 } 3799 return (io); 3800} 3801 3802void 3803ctl_free_io(union ctl_io *io) 3804{ 3805 struct ctl_io_pool *pool; 3806 3807 if (io == NULL) 3808 return; 3809 3810 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3811 uma_zfree(pool->zone, io); 3812} 3813 3814void 3815ctl_zero_io(union ctl_io *io) 3816{ 3817 struct ctl_io_pool *pool; 3818 3819 if (io == NULL) 3820 return; 3821 3822 /* 3823 * May need to preserve linked list pointers at some point too. 3824 */ 3825 pool = io->io_hdr.pool; 3826 memset(io, 0, sizeof(*io)); 3827 io->io_hdr.pool = pool; 3828 CTL_SOFTC(io) = pool->ctl_softc; 3829} 3830 3831int 3832ctl_expand_number(const char *buf, uint64_t *num) 3833{ 3834 char *endptr; 3835 uint64_t number; 3836 unsigned shift; 3837 3838 number = strtoq(buf, &endptr, 0); 3839 3840 switch (tolower((unsigned char)*endptr)) { 3841 case 'e': 3842 shift = 60; 3843 break; 3844 case 'p': 3845 shift = 50; 3846 break; 3847 case 't': 3848 shift = 40; 3849 break; 3850 case 'g': 3851 shift = 30; 3852 break; 3853 case 'm': 3854 shift = 20; 3855 break; 3856 case 'k': 3857 shift = 10; 3858 break; 3859 case 'b': 3860 case '\0': /* No unit. */ 3861 *num = number; 3862 return (0); 3863 default: 3864 /* Unrecognized unit. */ 3865 return (-1); 3866 } 3867 3868 if ((number << shift) >> shift != number) { 3869 /* Overflow */ 3870 return (-1); 3871 } 3872 *num = number << shift; 3873 return (0); 3874} 3875 3876 3877/* 3878 * This routine could be used in the future to load default and/or saved 3879 * mode page parameters for a particuar lun. 3880 */ 3881static int 3882ctl_init_page_index(struct ctl_lun *lun) 3883{ 3884 int i, page_code; 3885 struct ctl_page_index *page_index; 3886 const char *value; 3887 uint64_t ival; 3888 3889 memcpy(&lun->mode_pages.index, page_index_template, 3890 sizeof(page_index_template)); 3891 3892 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3893 3894 page_index = &lun->mode_pages.index[i]; 3895 if (lun->be_lun->lun_type == T_DIRECT && 3896 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3897 continue; 3898 if (lun->be_lun->lun_type == T_PROCESSOR && 3899 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3900 continue; 3901 if (lun->be_lun->lun_type == T_CDROM && 3902 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3903 continue; 3904 3905 page_code = page_index->page_code & SMPH_PC_MASK; 3906 switch (page_code) { 3907 case SMS_RW_ERROR_RECOVERY_PAGE: { 3908 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3909 ("subpage %#x for page %#x is incorrect!", 3910 page_index->subpage, page_code)); 3911 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3912 &rw_er_page_default, 3913 sizeof(rw_er_page_default)); 3914 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3915 &rw_er_page_changeable, 3916 sizeof(rw_er_page_changeable)); 3917 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3918 &rw_er_page_default, 3919 sizeof(rw_er_page_default)); 3920 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3921 &rw_er_page_default, 3922 sizeof(rw_er_page_default)); 3923 page_index->page_data = 3924 (uint8_t *)lun->mode_pages.rw_er_page; 3925 break; 3926 } 3927 case SMS_FORMAT_DEVICE_PAGE: { 3928 struct scsi_format_page *format_page; 3929 3930 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3931 ("subpage %#x for page %#x is incorrect!", 3932 page_index->subpage, page_code)); 3933 3934 /* 3935 * Sectors per track are set above. Bytes per 3936 * sector need to be set here on a per-LUN basis. 3937 */ 3938 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3939 &format_page_default, 3940 sizeof(format_page_default)); 3941 memcpy(&lun->mode_pages.format_page[ 3942 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3943 sizeof(format_page_changeable)); 3944 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3945 &format_page_default, 3946 sizeof(format_page_default)); 3947 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3948 &format_page_default, 3949 sizeof(format_page_default)); 3950 3951 format_page = &lun->mode_pages.format_page[ 3952 CTL_PAGE_CURRENT]; 3953 scsi_ulto2b(lun->be_lun->blocksize, 3954 format_page->bytes_per_sector); 3955 3956 format_page = &lun->mode_pages.format_page[ 3957 CTL_PAGE_DEFAULT]; 3958 scsi_ulto2b(lun->be_lun->blocksize, 3959 format_page->bytes_per_sector); 3960 3961 format_page = &lun->mode_pages.format_page[ 3962 CTL_PAGE_SAVED]; 3963 scsi_ulto2b(lun->be_lun->blocksize, 3964 format_page->bytes_per_sector); 3965 3966 page_index->page_data = 3967 (uint8_t *)lun->mode_pages.format_page; 3968 break; 3969 } 3970 case SMS_RIGID_DISK_PAGE: { 3971 struct scsi_rigid_disk_page *rigid_disk_page; 3972 uint32_t sectors_per_cylinder; 3973 uint64_t cylinders; 3974#ifndef __XSCALE__ 3975 int shift; 3976#endif /* !__XSCALE__ */ 3977 3978 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3979 ("subpage %#x for page %#x is incorrect!", 3980 page_index->subpage, page_code)); 3981 3982 /* 3983 * Rotation rate and sectors per track are set 3984 * above. We calculate the cylinders here based on 3985 * capacity. Due to the number of heads and 3986 * sectors per track we're using, smaller arrays 3987 * may turn out to have 0 cylinders. Linux and 3988 * FreeBSD don't pay attention to these mode pages 3989 * to figure out capacity, but Solaris does. It 3990 * seems to deal with 0 cylinders just fine, and 3991 * works out a fake geometry based on the capacity. 3992 */ 3993 memcpy(&lun->mode_pages.rigid_disk_page[ 3994 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3995 sizeof(rigid_disk_page_default)); 3996 memcpy(&lun->mode_pages.rigid_disk_page[ 3997 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3998 sizeof(rigid_disk_page_changeable)); 3999 4000 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4001 CTL_DEFAULT_HEADS; 4002 4003 /* 4004 * The divide method here will be more accurate, 4005 * probably, but results in floating point being 4006 * used in the kernel on i386 (__udivdi3()). On the 4007 * XScale, though, __udivdi3() is implemented in 4008 * software. 4009 * 4010 * The shift method for cylinder calculation is 4011 * accurate if sectors_per_cylinder is a power of 4012 * 2. Otherwise it might be slightly off -- you 4013 * might have a bit of a truncation problem. 4014 */ 4015#ifdef __XSCALE__ 4016 cylinders = (lun->be_lun->maxlba + 1) / 4017 sectors_per_cylinder; 4018#else 4019 for (shift = 31; shift > 0; shift--) { 4020 if (sectors_per_cylinder & (1 << shift)) 4021 break; 4022 } 4023 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4024#endif 4025 4026 /* 4027 * We've basically got 3 bytes, or 24 bits for the 4028 * cylinder size in the mode page. If we're over, 4029 * just round down to 2^24. 4030 */ 4031 if (cylinders > 0xffffff) 4032 cylinders = 0xffffff; 4033 4034 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4035 CTL_PAGE_DEFAULT]; 4036 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4037 4038 if ((value = ctl_get_opt(&lun->be_lun->options, 4039 "rpm")) != NULL) { 4040 scsi_ulto2b(strtol(value, NULL, 0), 4041 rigid_disk_page->rotation_rate); 4042 } 4043 4044 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4045 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4046 sizeof(rigid_disk_page_default)); 4047 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4048 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4049 sizeof(rigid_disk_page_default)); 4050 4051 page_index->page_data = 4052 (uint8_t *)lun->mode_pages.rigid_disk_page; 4053 break; 4054 } 4055 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4056 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4057 ("subpage %#x for page %#x is incorrect!", 4058 page_index->subpage, page_code)); 4059 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4060 &verify_er_page_default, 4061 sizeof(verify_er_page_default)); 4062 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4063 &verify_er_page_changeable, 4064 sizeof(verify_er_page_changeable)); 4065 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4066 &verify_er_page_default, 4067 sizeof(verify_er_page_default)); 4068 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4069 &verify_er_page_default, 4070 sizeof(verify_er_page_default)); 4071 page_index->page_data = 4072 (uint8_t *)lun->mode_pages.verify_er_page; 4073 break; 4074 } 4075 case SMS_CACHING_PAGE: { 4076 struct scsi_caching_page *caching_page; 4077 4078 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4079 ("subpage %#x for page %#x is incorrect!", 4080 page_index->subpage, page_code)); 4081 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4082 &caching_page_default, 4083 sizeof(caching_page_default)); 4084 memcpy(&lun->mode_pages.caching_page[ 4085 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4086 sizeof(caching_page_changeable)); 4087 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4088 &caching_page_default, 4089 sizeof(caching_page_default)); 4090 caching_page = &lun->mode_pages.caching_page[ 4091 CTL_PAGE_SAVED]; 4092 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4093 if (value != NULL && strcmp(value, "off") == 0) 4094 caching_page->flags1 &= ~SCP_WCE; 4095 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4096 if (value != NULL && strcmp(value, "off") == 0) 4097 caching_page->flags1 |= SCP_RCD; 4098 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4099 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4100 sizeof(caching_page_default)); 4101 page_index->page_data = 4102 (uint8_t *)lun->mode_pages.caching_page; 4103 break; 4104 } 4105 case SMS_CONTROL_MODE_PAGE: { 4106 switch (page_index->subpage) { 4107 case SMS_SUBPAGE_PAGE_0: { 4108 struct scsi_control_page *control_page; 4109 4110 memcpy(&lun->mode_pages.control_page[ 4111 CTL_PAGE_DEFAULT], 4112 &control_page_default, 4113 sizeof(control_page_default)); 4114 memcpy(&lun->mode_pages.control_page[ 4115 CTL_PAGE_CHANGEABLE], 4116 &control_page_changeable, 4117 sizeof(control_page_changeable)); 4118 memcpy(&lun->mode_pages.control_page[ 4119 CTL_PAGE_SAVED], 4120 &control_page_default, 4121 sizeof(control_page_default)); 4122 control_page = &lun->mode_pages.control_page[ 4123 CTL_PAGE_SAVED]; 4124 value = ctl_get_opt(&lun->be_lun->options, 4125 "reordering"); 4126 if (value != NULL && 4127 strcmp(value, "unrestricted") == 0) { 4128 control_page->queue_flags &= 4129 ~SCP_QUEUE_ALG_MASK; 4130 control_page->queue_flags |= 4131 SCP_QUEUE_ALG_UNRESTRICTED; 4132 } 4133 memcpy(&lun->mode_pages.control_page[ 4134 CTL_PAGE_CURRENT], 4135 &lun->mode_pages.control_page[ 4136 CTL_PAGE_SAVED], 4137 sizeof(control_page_default)); 4138 page_index->page_data = 4139 (uint8_t *)lun->mode_pages.control_page; 4140 break; 4141 } 4142 case 0x01: 4143 memcpy(&lun->mode_pages.control_ext_page[ 4144 CTL_PAGE_DEFAULT], 4145 &control_ext_page_default, 4146 sizeof(control_ext_page_default)); 4147 memcpy(&lun->mode_pages.control_ext_page[ 4148 CTL_PAGE_CHANGEABLE], 4149 &control_ext_page_changeable, 4150 sizeof(control_ext_page_changeable)); 4151 memcpy(&lun->mode_pages.control_ext_page[ 4152 CTL_PAGE_SAVED], 4153 &control_ext_page_default, 4154 sizeof(control_ext_page_default)); 4155 memcpy(&lun->mode_pages.control_ext_page[ 4156 CTL_PAGE_CURRENT], 4157 &lun->mode_pages.control_ext_page[ 4158 CTL_PAGE_SAVED], 4159 sizeof(control_ext_page_default)); 4160 page_index->page_data = 4161 (uint8_t *)lun->mode_pages.control_ext_page; 4162 break; 4163 default: 4164 panic("subpage %#x for page %#x is incorrect!", 4165 page_index->subpage, page_code); 4166 } 4167 break; 4168 } 4169 case SMS_INFO_EXCEPTIONS_PAGE: { 4170 switch (page_index->subpage) { 4171 case SMS_SUBPAGE_PAGE_0: 4172 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4173 &ie_page_default, 4174 sizeof(ie_page_default)); 4175 memcpy(&lun->mode_pages.ie_page[ 4176 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4177 sizeof(ie_page_changeable)); 4178 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4179 &ie_page_default, 4180 sizeof(ie_page_default)); 4181 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4182 &ie_page_default, 4183 sizeof(ie_page_default)); 4184 page_index->page_data = 4185 (uint8_t *)lun->mode_pages.ie_page; 4186 break; 4187 case 0x02: { 4188 struct ctl_logical_block_provisioning_page *page; 4189 4190 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4191 &lbp_page_default, 4192 sizeof(lbp_page_default)); 4193 memcpy(&lun->mode_pages.lbp_page[ 4194 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4195 sizeof(lbp_page_changeable)); 4196 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4197 &lbp_page_default, 4198 sizeof(lbp_page_default)); 4199 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4200 value = ctl_get_opt(&lun->be_lun->options, 4201 "avail-threshold"); 4202 if (value != NULL && 4203 ctl_expand_number(value, &ival) == 0) { 4204 page->descr[0].flags |= SLBPPD_ENABLED | 4205 SLBPPD_ARMING_DEC; 4206 if (lun->be_lun->blocksize) 4207 ival /= lun->be_lun->blocksize; 4208 else 4209 ival /= 512; 4210 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4211 page->descr[0].count); 4212 } 4213 value = ctl_get_opt(&lun->be_lun->options, 4214 "used-threshold"); 4215 if (value != NULL && 4216 ctl_expand_number(value, &ival) == 0) { 4217 page->descr[1].flags |= SLBPPD_ENABLED | 4218 SLBPPD_ARMING_INC; 4219 if (lun->be_lun->blocksize) 4220 ival /= lun->be_lun->blocksize; 4221 else 4222 ival /= 512; 4223 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4224 page->descr[1].count); 4225 } 4226 value = ctl_get_opt(&lun->be_lun->options, 4227 "pool-avail-threshold"); 4228 if (value != NULL && 4229 ctl_expand_number(value, &ival) == 0) { 4230 page->descr[2].flags |= SLBPPD_ENABLED | 4231 SLBPPD_ARMING_DEC; 4232 if (lun->be_lun->blocksize) 4233 ival /= lun->be_lun->blocksize; 4234 else 4235 ival /= 512; 4236 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4237 page->descr[2].count); 4238 } 4239 value = ctl_get_opt(&lun->be_lun->options, 4240 "pool-used-threshold"); 4241 if (value != NULL && 4242 ctl_expand_number(value, &ival) == 0) { 4243 page->descr[3].flags |= SLBPPD_ENABLED | 4244 SLBPPD_ARMING_INC; 4245 if (lun->be_lun->blocksize) 4246 ival /= lun->be_lun->blocksize; 4247 else 4248 ival /= 512; 4249 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4250 page->descr[3].count); 4251 } 4252 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4253 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4254 sizeof(lbp_page_default)); 4255 page_index->page_data = 4256 (uint8_t *)lun->mode_pages.lbp_page; 4257 break; 4258 } 4259 default: 4260 panic("subpage %#x for page %#x is incorrect!", 4261 page_index->subpage, page_code); 4262 } 4263 break; 4264 } 4265 case SMS_CDDVD_CAPS_PAGE:{ 4266 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4267 ("subpage %#x for page %#x is incorrect!", 4268 page_index->subpage, page_code)); 4269 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4270 &cddvd_page_default, 4271 sizeof(cddvd_page_default)); 4272 memcpy(&lun->mode_pages.cddvd_page[ 4273 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4274 sizeof(cddvd_page_changeable)); 4275 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4276 &cddvd_page_default, 4277 sizeof(cddvd_page_default)); 4278 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4279 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4280 sizeof(cddvd_page_default)); 4281 page_index->page_data = 4282 (uint8_t *)lun->mode_pages.cddvd_page; 4283 break; 4284 } 4285 default: 4286 panic("invalid page code value %#x", page_code); 4287 } 4288 } 4289 4290 return (CTL_RETVAL_COMPLETE); 4291} 4292 4293static int 4294ctl_init_log_page_index(struct ctl_lun *lun) 4295{ 4296 struct ctl_page_index *page_index; 4297 int i, j, k, prev; 4298 4299 memcpy(&lun->log_pages.index, log_page_index_template, 4300 sizeof(log_page_index_template)); 4301 4302 prev = -1; 4303 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4304 4305 page_index = &lun->log_pages.index[i]; 4306 if (lun->be_lun->lun_type == T_DIRECT && 4307 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4308 continue; 4309 if (lun->be_lun->lun_type == T_PROCESSOR && 4310 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4311 continue; 4312 if (lun->be_lun->lun_type == T_CDROM && 4313 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4314 continue; 4315 4316 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4317 lun->backend->lun_attr == NULL) 4318 continue; 4319 4320 if (page_index->page_code != prev) { 4321 lun->log_pages.pages_page[j] = page_index->page_code; 4322 prev = page_index->page_code; 4323 j++; 4324 } 4325 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4326 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4327 k++; 4328 } 4329 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4330 lun->log_pages.index[0].page_len = j; 4331 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4332 lun->log_pages.index[1].page_len = k * 2; 4333 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4334 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4335 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4336 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4337 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4338 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4339 4340 return (CTL_RETVAL_COMPLETE); 4341} 4342 4343static int 4344hex2bin(const char *str, uint8_t *buf, int buf_size) 4345{ 4346 int i; 4347 u_char c; 4348 4349 memset(buf, 0, buf_size); 4350 while (isspace(str[0])) 4351 str++; 4352 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4353 str += 2; 4354 buf_size *= 2; 4355 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4356 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4357 str++; 4358 c = str[i]; 4359 if (isdigit(c)) 4360 c -= '0'; 4361 else if (isalpha(c)) 4362 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4363 else 4364 break; 4365 if (c >= 16) 4366 break; 4367 if ((i & 1) == 0) 4368 buf[i / 2] |= (c << 4); 4369 else 4370 buf[i / 2] |= c; 4371 } 4372 return ((i + 1) / 2); 4373} 4374 4375/* 4376 * LUN allocation. 4377 * 4378 * Requirements: 4379 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4380 * wants us to allocate the LUN and he can block. 4381 * - ctl_softc is always set 4382 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4383 * 4384 * Returns 0 for success, non-zero (errno) for failure. 4385 */ 4386static int 4387ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4388 struct ctl_be_lun *const be_lun) 4389{ 4390 struct ctl_lun *nlun, *lun; 4391 struct scsi_vpd_id_descriptor *desc; 4392 struct scsi_vpd_id_t10 *t10id; 4393 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4394 int lun_number, i, lun_malloced; 4395 int devidlen, idlen1, idlen2 = 0, len; 4396 4397 if (be_lun == NULL) 4398 return (EINVAL); 4399 4400 /* 4401 * We currently only support Direct Access or Processor LUN types. 4402 */ 4403 switch (be_lun->lun_type) { 4404 case T_DIRECT: 4405 case T_PROCESSOR: 4406 case T_CDROM: 4407 break; 4408 case T_SEQUENTIAL: 4409 case T_CHANGER: 4410 default: 4411 be_lun->lun_config_status(be_lun->be_lun, 4412 CTL_LUN_CONFIG_FAILURE); 4413 break; 4414 } 4415 if (ctl_lun == NULL) { 4416 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4417 lun_malloced = 1; 4418 } else { 4419 lun_malloced = 0; 4420 lun = ctl_lun; 4421 } 4422 4423 memset(lun, 0, sizeof(*lun)); 4424 if (lun_malloced) 4425 lun->flags = CTL_LUN_MALLOCED; 4426 4427 /* Generate LUN ID. */ 4428 devidlen = max(CTL_DEVID_MIN_LEN, 4429 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4430 idlen1 = sizeof(*t10id) + devidlen; 4431 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4432 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4433 if (scsiname != NULL) { 4434 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4435 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4436 } 4437 eui = ctl_get_opt(&be_lun->options, "eui"); 4438 if (eui != NULL) { 4439 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4440 } 4441 naa = ctl_get_opt(&be_lun->options, "naa"); 4442 if (naa != NULL) { 4443 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4444 } 4445 uuid = ctl_get_opt(&be_lun->options, "uuid"); 4446 if (uuid != NULL) { 4447 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4448 } 4449 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4450 M_CTL, M_WAITOK | M_ZERO); 4451 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4452 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4453 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4454 desc->length = idlen1; 4455 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4456 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4457 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4458 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4459 } else { 4460 strncpy(t10id->vendor, vendor, 4461 min(sizeof(t10id->vendor), strlen(vendor))); 4462 } 4463 strncpy((char *)t10id->vendor_spec_id, 4464 (char *)be_lun->device_id, devidlen); 4465 if (scsiname != NULL) { 4466 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4467 desc->length); 4468 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4469 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4470 SVPD_ID_TYPE_SCSI_NAME; 4471 desc->length = idlen2; 4472 strlcpy(desc->identifier, scsiname, idlen2); 4473 } 4474 if (eui != NULL) { 4475 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4476 desc->length); 4477 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4478 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4479 SVPD_ID_TYPE_EUI64; 4480 desc->length = hex2bin(eui, desc->identifier, 16); 4481 desc->length = desc->length > 12 ? 16 : 4482 (desc->length > 8 ? 12 : 8); 4483 len -= 16 - desc->length; 4484 } 4485 if (naa != NULL) { 4486 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4487 desc->length); 4488 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4489 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4490 SVPD_ID_TYPE_NAA; 4491 desc->length = hex2bin(naa, desc->identifier, 16); 4492 desc->length = desc->length > 8 ? 16 : 8; 4493 len -= 16 - desc->length; 4494 } 4495 if (uuid != NULL) { 4496 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4497 desc->length); 4498 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4499 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4500 SVPD_ID_TYPE_UUID; 4501 desc->identifier[0] = 0x10; 4502 hex2bin(uuid, &desc->identifier[2], 16); 4503 desc->length = 18; 4504 } 4505 lun->lun_devid->len = len; 4506 4507 mtx_lock(&ctl_softc->ctl_lock); 4508 /* 4509 * See if the caller requested a particular LUN number. If so, see 4510 * if it is available. Otherwise, allocate the first available LUN. 4511 */ 4512 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4513 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4514 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4515 mtx_unlock(&ctl_softc->ctl_lock); 4516 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4517 printf("ctl: requested LUN ID %d is higher " 4518 "than CTL_MAX_LUNS - 1 (%d)\n", 4519 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4520 } else { 4521 /* 4522 * XXX KDM return an error, or just assign 4523 * another LUN ID in this case?? 4524 */ 4525 printf("ctl: requested LUN ID %d is already " 4526 "in use\n", be_lun->req_lun_id); 4527 } 4528 if (lun->flags & CTL_LUN_MALLOCED) 4529 free(lun, M_CTL); 4530 be_lun->lun_config_status(be_lun->be_lun, 4531 CTL_LUN_CONFIG_FAILURE); 4532 return (ENOSPC); 4533 } 4534 lun_number = be_lun->req_lun_id; 4535 } else { 4536 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4537 if (lun_number == -1) { 4538 mtx_unlock(&ctl_softc->ctl_lock); 4539 printf("ctl: can't allocate LUN, out of LUNs\n"); 4540 if (lun->flags & CTL_LUN_MALLOCED) 4541 free(lun, M_CTL); 4542 be_lun->lun_config_status(be_lun->be_lun, 4543 CTL_LUN_CONFIG_FAILURE); 4544 return (ENOSPC); 4545 } 4546 } 4547 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4548 4549 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4550 lun->lun = lun_number; 4551 lun->be_lun = be_lun; 4552 /* 4553 * The processor LUN is always enabled. Disk LUNs come on line 4554 * disabled, and must be enabled by the backend. 4555 */ 4556 lun->flags |= CTL_LUN_DISABLED; 4557 lun->backend = be_lun->be; 4558 be_lun->ctl_lun = lun; 4559 be_lun->lun_id = lun_number; 4560 atomic_add_int(&be_lun->be->num_luns, 1); 4561 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4562 lun->flags |= CTL_LUN_EJECTED; 4563 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4564 lun->flags |= CTL_LUN_NO_MEDIA; 4565 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4566 lun->flags |= CTL_LUN_STOPPED; 4567 4568 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4569 lun->flags |= CTL_LUN_PRIMARY_SC; 4570 4571 value = ctl_get_opt(&be_lun->options, "removable"); 4572 if (value != NULL) { 4573 if (strcmp(value, "on") == 0) 4574 lun->flags |= CTL_LUN_REMOVABLE; 4575 } else if (be_lun->lun_type == T_CDROM) 4576 lun->flags |= CTL_LUN_REMOVABLE; 4577 4578 lun->ctl_softc = ctl_softc; 4579#ifdef CTL_TIME_IO 4580 lun->last_busy = getsbinuptime(); 4581#endif 4582 TAILQ_INIT(&lun->ooa_queue); 4583 TAILQ_INIT(&lun->blocked_queue); 4584 STAILQ_INIT(&lun->error_list); 4585 lun->ie_reported = 1; 4586 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4587 ctl_tpc_lun_init(lun); 4588 if (lun->flags & CTL_LUN_REMOVABLE) { 4589 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4, 4590 M_CTL, M_WAITOK); 4591 } 4592 4593 /* 4594 * Initialize the mode and log page index. 4595 */ 4596 ctl_init_page_index(lun); 4597 ctl_init_log_page_index(lun); 4598 4599 /* 4600 * Now, before we insert this lun on the lun list, set the lun 4601 * inventory changed UA for all other luns. 4602 */ 4603 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4604 mtx_lock(&nlun->lun_lock); 4605 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4606 mtx_unlock(&nlun->lun_lock); 4607 } 4608 4609 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4610 4611 ctl_softc->ctl_luns[lun_number] = lun; 4612 4613 ctl_softc->num_luns++; 4614 4615 /* Setup statistics gathering */ 4616 lun->stats.device_type = be_lun->lun_type; 4617 lun->stats.lun_number = lun_number; 4618 lun->stats.blocksize = be_lun->blocksize; 4619 if (be_lun->blocksize == 0) 4620 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4621 for (i = 0;i < CTL_MAX_PORTS;i++) 4622 lun->stats.ports[i].targ_port = i; 4623 4624 mtx_unlock(&ctl_softc->ctl_lock); 4625 4626 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4627 return (0); 4628} 4629 4630/* 4631 * Delete a LUN. 4632 * Assumptions: 4633 * - LUN has already been marked invalid and any pending I/O has been taken 4634 * care of. 4635 */ 4636static int 4637ctl_free_lun(struct ctl_lun *lun) 4638{ 4639 struct ctl_softc *softc = lun->ctl_softc; 4640 struct ctl_lun *nlun; 4641 int i; 4642 4643 mtx_assert(&softc->ctl_lock, MA_OWNED); 4644 4645 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4646 4647 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4648 4649 softc->ctl_luns[lun->lun] = NULL; 4650 4651 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4652 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4653 4654 softc->num_luns--; 4655 4656 /* 4657 * Tell the backend to free resources, if this LUN has a backend. 4658 */ 4659 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4660 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4661 4662 lun->ie_reportcnt = UINT32_MAX; 4663 callout_drain(&lun->ie_callout); 4664 4665 ctl_tpc_lun_shutdown(lun); 4666 mtx_destroy(&lun->lun_lock); 4667 free(lun->lun_devid, M_CTL); 4668 for (i = 0; i < CTL_MAX_PORTS; i++) 4669 free(lun->pending_ua[i], M_CTL); 4670 for (i = 0; i < CTL_MAX_PORTS; i++) 4671 free(lun->pr_keys[i], M_CTL); 4672 free(lun->write_buffer, M_CTL); 4673 free(lun->prevent, M_CTL); 4674 if (lun->flags & CTL_LUN_MALLOCED) 4675 free(lun, M_CTL); 4676 4677 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4678 mtx_lock(&nlun->lun_lock); 4679 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4680 mtx_unlock(&nlun->lun_lock); 4681 } 4682 4683 return (0); 4684} 4685 4686static void 4687ctl_create_lun(struct ctl_be_lun *be_lun) 4688{ 4689 4690 /* 4691 * ctl_alloc_lun() should handle all potential failure cases. 4692 */ 4693 ctl_alloc_lun(control_softc, NULL, be_lun); 4694} 4695 4696int 4697ctl_add_lun(struct ctl_be_lun *be_lun) 4698{ 4699 struct ctl_softc *softc = control_softc; 4700 4701 mtx_lock(&softc->ctl_lock); 4702 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4703 mtx_unlock(&softc->ctl_lock); 4704 wakeup(&softc->pending_lun_queue); 4705 4706 return (0); 4707} 4708 4709int 4710ctl_enable_lun(struct ctl_be_lun *be_lun) 4711{ 4712 struct ctl_softc *softc; 4713 struct ctl_port *port, *nport; 4714 struct ctl_lun *lun; 4715 int retval; 4716 4717 lun = (struct ctl_lun *)be_lun->ctl_lun; 4718 softc = lun->ctl_softc; 4719 4720 mtx_lock(&softc->ctl_lock); 4721 mtx_lock(&lun->lun_lock); 4722 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4723 /* 4724 * eh? Why did we get called if the LUN is already 4725 * enabled? 4726 */ 4727 mtx_unlock(&lun->lun_lock); 4728 mtx_unlock(&softc->ctl_lock); 4729 return (0); 4730 } 4731 lun->flags &= ~CTL_LUN_DISABLED; 4732 mtx_unlock(&lun->lun_lock); 4733 4734 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4735 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4736 port->lun_map != NULL || port->lun_enable == NULL) 4737 continue; 4738 4739 /* 4740 * Drop the lock while we call the FETD's enable routine. 4741 * This can lead to a callback into CTL (at least in the 4742 * case of the internal initiator frontend. 4743 */ 4744 mtx_unlock(&softc->ctl_lock); 4745 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4746 mtx_lock(&softc->ctl_lock); 4747 if (retval != 0) { 4748 printf("%s: FETD %s port %d returned error " 4749 "%d for lun_enable on lun %jd\n", 4750 __func__, port->port_name, port->targ_port, 4751 retval, (intmax_t)lun->lun); 4752 } 4753 } 4754 4755 mtx_unlock(&softc->ctl_lock); 4756 ctl_isc_announce_lun(lun); 4757 4758 return (0); 4759} 4760 4761int 4762ctl_disable_lun(struct ctl_be_lun *be_lun) 4763{ 4764 struct ctl_softc *softc; 4765 struct ctl_port *port; 4766 struct ctl_lun *lun; 4767 int retval; 4768 4769 lun = (struct ctl_lun *)be_lun->ctl_lun; 4770 softc = lun->ctl_softc; 4771 4772 mtx_lock(&softc->ctl_lock); 4773 mtx_lock(&lun->lun_lock); 4774 if (lun->flags & CTL_LUN_DISABLED) { 4775 mtx_unlock(&lun->lun_lock); 4776 mtx_unlock(&softc->ctl_lock); 4777 return (0); 4778 } 4779 lun->flags |= CTL_LUN_DISABLED; 4780 mtx_unlock(&lun->lun_lock); 4781 4782 STAILQ_FOREACH(port, &softc->port_list, links) { 4783 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4784 port->lun_map != NULL || port->lun_disable == NULL) 4785 continue; 4786 4787 /* 4788 * Drop the lock before we call the frontend's disable 4789 * routine, to avoid lock order reversals. 4790 * 4791 * XXX KDM what happens if the frontend list changes while 4792 * we're traversing it? It's unlikely, but should be handled. 4793 */ 4794 mtx_unlock(&softc->ctl_lock); 4795 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4796 mtx_lock(&softc->ctl_lock); 4797 if (retval != 0) { 4798 printf("%s: FETD %s port %d returned error " 4799 "%d for lun_disable on lun %jd\n", 4800 __func__, port->port_name, port->targ_port, 4801 retval, (intmax_t)lun->lun); 4802 } 4803 } 4804 4805 mtx_unlock(&softc->ctl_lock); 4806 ctl_isc_announce_lun(lun); 4807 4808 return (0); 4809} 4810 4811int 4812ctl_start_lun(struct ctl_be_lun *be_lun) 4813{ 4814 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4815 4816 mtx_lock(&lun->lun_lock); 4817 lun->flags &= ~CTL_LUN_STOPPED; 4818 mtx_unlock(&lun->lun_lock); 4819 return (0); 4820} 4821 4822int 4823ctl_stop_lun(struct ctl_be_lun *be_lun) 4824{ 4825 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4826 4827 mtx_lock(&lun->lun_lock); 4828 lun->flags |= CTL_LUN_STOPPED; 4829 mtx_unlock(&lun->lun_lock); 4830 return (0); 4831} 4832 4833int 4834ctl_lun_no_media(struct ctl_be_lun *be_lun) 4835{ 4836 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4837 4838 mtx_lock(&lun->lun_lock); 4839 lun->flags |= CTL_LUN_NO_MEDIA; 4840 mtx_unlock(&lun->lun_lock); 4841 return (0); 4842} 4843 4844int 4845ctl_lun_has_media(struct ctl_be_lun *be_lun) 4846{ 4847 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4848 union ctl_ha_msg msg; 4849 4850 mtx_lock(&lun->lun_lock); 4851 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4852 if (lun->flags & CTL_LUN_REMOVABLE) 4853 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4854 mtx_unlock(&lun->lun_lock); 4855 if ((lun->flags & CTL_LUN_REMOVABLE) && 4856 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4857 bzero(&msg.ua, sizeof(msg.ua)); 4858 msg.hdr.msg_type = CTL_MSG_UA; 4859 msg.hdr.nexus.initid = -1; 4860 msg.hdr.nexus.targ_port = -1; 4861 msg.hdr.nexus.targ_lun = lun->lun; 4862 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4863 msg.ua.ua_all = 1; 4864 msg.ua.ua_set = 1; 4865 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4866 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4867 M_WAITOK); 4868 } 4869 return (0); 4870} 4871 4872int 4873ctl_lun_ejected(struct ctl_be_lun *be_lun) 4874{ 4875 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4876 4877 mtx_lock(&lun->lun_lock); 4878 lun->flags |= CTL_LUN_EJECTED; 4879 mtx_unlock(&lun->lun_lock); 4880 return (0); 4881} 4882 4883int 4884ctl_lun_primary(struct ctl_be_lun *be_lun) 4885{ 4886 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4887 4888 mtx_lock(&lun->lun_lock); 4889 lun->flags |= CTL_LUN_PRIMARY_SC; 4890 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4891 mtx_unlock(&lun->lun_lock); 4892 ctl_isc_announce_lun(lun); 4893 return (0); 4894} 4895 4896int 4897ctl_lun_secondary(struct ctl_be_lun *be_lun) 4898{ 4899 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4900 4901 mtx_lock(&lun->lun_lock); 4902 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4903 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4904 mtx_unlock(&lun->lun_lock); 4905 ctl_isc_announce_lun(lun); 4906 return (0); 4907} 4908 4909int 4910ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4911{ 4912 struct ctl_softc *softc; 4913 struct ctl_lun *lun; 4914 4915 lun = (struct ctl_lun *)be_lun->ctl_lun; 4916 softc = lun->ctl_softc; 4917 4918 mtx_lock(&lun->lun_lock); 4919 4920 /* 4921 * The LUN needs to be disabled before it can be marked invalid. 4922 */ 4923 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4924 mtx_unlock(&lun->lun_lock); 4925 return (-1); 4926 } 4927 /* 4928 * Mark the LUN invalid. 4929 */ 4930 lun->flags |= CTL_LUN_INVALID; 4931 4932 /* 4933 * If there is nothing in the OOA queue, go ahead and free the LUN. 4934 * If we have something in the OOA queue, we'll free it when the 4935 * last I/O completes. 4936 */ 4937 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4938 mtx_unlock(&lun->lun_lock); 4939 mtx_lock(&softc->ctl_lock); 4940 ctl_free_lun(lun); 4941 mtx_unlock(&softc->ctl_lock); 4942 } else 4943 mtx_unlock(&lun->lun_lock); 4944 4945 return (0); 4946} 4947 4948void 4949ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4950{ 4951 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4952 union ctl_ha_msg msg; 4953 4954 mtx_lock(&lun->lun_lock); 4955 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4956 mtx_unlock(&lun->lun_lock); 4957 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4958 /* Send msg to other side. */ 4959 bzero(&msg.ua, sizeof(msg.ua)); 4960 msg.hdr.msg_type = CTL_MSG_UA; 4961 msg.hdr.nexus.initid = -1; 4962 msg.hdr.nexus.targ_port = -1; 4963 msg.hdr.nexus.targ_lun = lun->lun; 4964 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4965 msg.ua.ua_all = 1; 4966 msg.ua.ua_set = 1; 4967 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4968 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4969 M_WAITOK); 4970 } 4971} 4972 4973/* 4974 * Backend "memory move is complete" callback for requests that never 4975 * make it down to say RAIDCore's configuration code. 4976 */ 4977int 4978ctl_config_move_done(union ctl_io *io) 4979{ 4980 int retval; 4981 4982 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4983 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4984 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4985 4986 if ((io->io_hdr.port_status != 0) && 4987 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4988 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4989 /* 4990 * For hardware error sense keys, the sense key 4991 * specific value is defined to be a retry count, 4992 * but we use it to pass back an internal FETD 4993 * error code. XXX KDM Hopefully the FETD is only 4994 * using 16 bits for an error code, since that's 4995 * all the space we have in the sks field. 4996 */ 4997 ctl_set_internal_failure(&io->scsiio, 4998 /*sks_valid*/ 1, 4999 /*retry_count*/ 5000 io->io_hdr.port_status); 5001 } 5002 5003 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5004 ctl_data_print(io); 5005 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5006 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5007 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5008 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5009 /* 5010 * XXX KDM just assuming a single pointer here, and not a 5011 * S/G list. If we start using S/G lists for config data, 5012 * we'll need to know how to clean them up here as well. 5013 */ 5014 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5015 free(io->scsiio.kern_data_ptr, M_CTL); 5016 ctl_done(io); 5017 retval = CTL_RETVAL_COMPLETE; 5018 } else { 5019 /* 5020 * XXX KDM now we need to continue data movement. Some 5021 * options: 5022 * - call ctl_scsiio() again? We don't do this for data 5023 * writes, because for those at least we know ahead of 5024 * time where the write will go and how long it is. For 5025 * config writes, though, that information is largely 5026 * contained within the write itself, thus we need to 5027 * parse out the data again. 5028 * 5029 * - Call some other function once the data is in? 5030 */ 5031 5032 /* 5033 * XXX KDM call ctl_scsiio() again for now, and check flag 5034 * bits to see whether we're allocated or not. 5035 */ 5036 retval = ctl_scsiio(&io->scsiio); 5037 } 5038 return (retval); 5039} 5040 5041/* 5042 * This gets called by a backend driver when it is done with a 5043 * data_submit method. 5044 */ 5045void 5046ctl_data_submit_done(union ctl_io *io) 5047{ 5048 /* 5049 * If the IO_CONT flag is set, we need to call the supplied 5050 * function to continue processing the I/O, instead of completing 5051 * the I/O just yet. 5052 * 5053 * If there is an error, though, we don't want to keep processing. 5054 * Instead, just send status back to the initiator. 5055 */ 5056 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5057 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5058 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5059 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5060 io->scsiio.io_cont(io); 5061 return; 5062 } 5063 ctl_done(io); 5064} 5065 5066/* 5067 * This gets called by a backend driver when it is done with a 5068 * configuration write. 5069 */ 5070void 5071ctl_config_write_done(union ctl_io *io) 5072{ 5073 uint8_t *buf; 5074 5075 /* 5076 * If the IO_CONT flag is set, we need to call the supplied 5077 * function to continue processing the I/O, instead of completing 5078 * the I/O just yet. 5079 * 5080 * If there is an error, though, we don't want to keep processing. 5081 * Instead, just send status back to the initiator. 5082 */ 5083 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5084 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5085 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5086 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5087 io->scsiio.io_cont(io); 5088 return; 5089 } 5090 /* 5091 * Since a configuration write can be done for commands that actually 5092 * have data allocated, like write buffer, and commands that have 5093 * no data, like start/stop unit, we need to check here. 5094 */ 5095 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5096 buf = io->scsiio.kern_data_ptr; 5097 else 5098 buf = NULL; 5099 ctl_done(io); 5100 if (buf) 5101 free(buf, M_CTL); 5102} 5103 5104void 5105ctl_config_read_done(union ctl_io *io) 5106{ 5107 uint8_t *buf; 5108 5109 /* 5110 * If there is some error -- we are done, skip data transfer. 5111 */ 5112 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5113 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5114 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5115 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5116 buf = io->scsiio.kern_data_ptr; 5117 else 5118 buf = NULL; 5119 ctl_done(io); 5120 if (buf) 5121 free(buf, M_CTL); 5122 return; 5123 } 5124 5125 /* 5126 * If the IO_CONT flag is set, we need to call the supplied 5127 * function to continue processing the I/O, instead of completing 5128 * the I/O just yet. 5129 */ 5130 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5131 io->scsiio.io_cont(io); 5132 return; 5133 } 5134 5135 ctl_datamove(io); 5136} 5137 5138/* 5139 * SCSI release command. 5140 */ 5141int 5142ctl_scsi_release(struct ctl_scsiio *ctsio) 5143{ 5144 struct ctl_lun *lun = CTL_LUN(ctsio); 5145 uint32_t residx; 5146 5147 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5148 5149 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5150 5151 /* 5152 * XXX KDM right now, we only support LUN reservation. We don't 5153 * support 3rd party reservations, or extent reservations, which 5154 * might actually need the parameter list. If we've gotten this 5155 * far, we've got a LUN reservation. Anything else got kicked out 5156 * above. So, according to SPC, ignore the length. 5157 */ 5158 5159 mtx_lock(&lun->lun_lock); 5160 5161 /* 5162 * According to SPC, it is not an error for an intiator to attempt 5163 * to release a reservation on a LUN that isn't reserved, or that 5164 * is reserved by another initiator. The reservation can only be 5165 * released, though, by the initiator who made it or by one of 5166 * several reset type events. 5167 */ 5168 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5169 lun->flags &= ~CTL_LUN_RESERVED; 5170 5171 mtx_unlock(&lun->lun_lock); 5172 5173 ctl_set_success(ctsio); 5174 ctl_done((union ctl_io *)ctsio); 5175 return (CTL_RETVAL_COMPLETE); 5176} 5177 5178int 5179ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5180{ 5181 struct ctl_lun *lun = CTL_LUN(ctsio); 5182 uint32_t residx; 5183 5184 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5185 5186 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5187 5188 /* 5189 * XXX KDM right now, we only support LUN reservation. We don't 5190 * support 3rd party reservations, or extent reservations, which 5191 * might actually need the parameter list. If we've gotten this 5192 * far, we've got a LUN reservation. Anything else got kicked out 5193 * above. So, according to SPC, ignore the length. 5194 */ 5195 5196 mtx_lock(&lun->lun_lock); 5197 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5198 ctl_set_reservation_conflict(ctsio); 5199 goto bailout; 5200 } 5201 5202 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5203 if (lun->flags & CTL_LUN_PR_RESERVED) { 5204 ctl_set_success(ctsio); 5205 goto bailout; 5206 } 5207 5208 lun->flags |= CTL_LUN_RESERVED; 5209 lun->res_idx = residx; 5210 ctl_set_success(ctsio); 5211 5212bailout: 5213 mtx_unlock(&lun->lun_lock); 5214 ctl_done((union ctl_io *)ctsio); 5215 return (CTL_RETVAL_COMPLETE); 5216} 5217 5218int 5219ctl_start_stop(struct ctl_scsiio *ctsio) 5220{ 5221 struct ctl_lun *lun = CTL_LUN(ctsio); 5222 struct scsi_start_stop_unit *cdb; 5223 int retval; 5224 5225 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5226 5227 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5228 5229 if ((cdb->how & SSS_PC_MASK) == 0) { 5230 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5231 (cdb->how & SSS_START) == 0) { 5232 uint32_t residx; 5233 5234 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5235 if (ctl_get_prkey(lun, residx) == 0 || 5236 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5237 5238 ctl_set_reservation_conflict(ctsio); 5239 ctl_done((union ctl_io *)ctsio); 5240 return (CTL_RETVAL_COMPLETE); 5241 } 5242 } 5243 5244 if ((cdb->how & SSS_LOEJ) && 5245 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5246 ctl_set_invalid_field(ctsio, 5247 /*sks_valid*/ 1, 5248 /*command*/ 1, 5249 /*field*/ 4, 5250 /*bit_valid*/ 1, 5251 /*bit*/ 1); 5252 ctl_done((union ctl_io *)ctsio); 5253 return (CTL_RETVAL_COMPLETE); 5254 } 5255 5256 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5257 lun->prevent_count > 0) { 5258 /* "Medium removal prevented" */ 5259 ctl_set_sense(ctsio, /*current_error*/ 1, 5260 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5261 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5262 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5263 ctl_done((union ctl_io *)ctsio); 5264 return (CTL_RETVAL_COMPLETE); 5265 } 5266 } 5267 5268 retval = lun->backend->config_write((union ctl_io *)ctsio); 5269 return (retval); 5270} 5271 5272int 5273ctl_prevent_allow(struct ctl_scsiio *ctsio) 5274{ 5275 struct ctl_lun *lun = CTL_LUN(ctsio); 5276 struct scsi_prevent *cdb; 5277 int retval; 5278 uint32_t initidx; 5279 5280 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5281 5282 cdb = (struct scsi_prevent *)ctsio->cdb; 5283 5284 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) { 5285 ctl_set_invalid_opcode(ctsio); 5286 ctl_done((union ctl_io *)ctsio); 5287 return (CTL_RETVAL_COMPLETE); 5288 } 5289 5290 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5291 mtx_lock(&lun->lun_lock); 5292 if ((cdb->how & PR_PREVENT) && 5293 ctl_is_set(lun->prevent, initidx) == 0) { 5294 ctl_set_mask(lun->prevent, initidx); 5295 lun->prevent_count++; 5296 } else if ((cdb->how & PR_PREVENT) == 0 && 5297 ctl_is_set(lun->prevent, initidx)) { 5298 ctl_clear_mask(lun->prevent, initidx); 5299 lun->prevent_count--; 5300 } 5301 mtx_unlock(&lun->lun_lock); 5302 retval = lun->backend->config_write((union ctl_io *)ctsio); 5303 return (retval); 5304} 5305 5306/* 5307 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5308 * we don't really do anything with the LBA and length fields if the user 5309 * passes them in. Instead we'll just flush out the cache for the entire 5310 * LUN. 5311 */ 5312int 5313ctl_sync_cache(struct ctl_scsiio *ctsio) 5314{ 5315 struct ctl_lun *lun = CTL_LUN(ctsio); 5316 struct ctl_lba_len_flags *lbalen; 5317 uint64_t starting_lba; 5318 uint32_t block_count; 5319 int retval; 5320 uint8_t byte2; 5321 5322 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5323 5324 retval = 0; 5325 5326 switch (ctsio->cdb[0]) { 5327 case SYNCHRONIZE_CACHE: { 5328 struct scsi_sync_cache *cdb; 5329 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5330 5331 starting_lba = scsi_4btoul(cdb->begin_lba); 5332 block_count = scsi_2btoul(cdb->lb_count); 5333 byte2 = cdb->byte2; 5334 break; 5335 } 5336 case SYNCHRONIZE_CACHE_16: { 5337 struct scsi_sync_cache_16 *cdb; 5338 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5339 5340 starting_lba = scsi_8btou64(cdb->begin_lba); 5341 block_count = scsi_4btoul(cdb->lb_count); 5342 byte2 = cdb->byte2; 5343 break; 5344 } 5345 default: 5346 ctl_set_invalid_opcode(ctsio); 5347 ctl_done((union ctl_io *)ctsio); 5348 goto bailout; 5349 break; /* NOTREACHED */ 5350 } 5351 5352 /* 5353 * We check the LBA and length, but don't do anything with them. 5354 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5355 * get flushed. This check will just help satisfy anyone who wants 5356 * to see an error for an out of range LBA. 5357 */ 5358 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5359 ctl_set_lba_out_of_range(ctsio, 5360 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5361 ctl_done((union ctl_io *)ctsio); 5362 goto bailout; 5363 } 5364 5365 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5366 lbalen->lba = starting_lba; 5367 lbalen->len = block_count; 5368 lbalen->flags = byte2; 5369 retval = lun->backend->config_write((union ctl_io *)ctsio); 5370 5371bailout: 5372 return (retval); 5373} 5374 5375int 5376ctl_format(struct ctl_scsiio *ctsio) 5377{ 5378 struct scsi_format *cdb; 5379 int length, defect_list_len; 5380 5381 CTL_DEBUG_PRINT(("ctl_format\n")); 5382 5383 cdb = (struct scsi_format *)ctsio->cdb; 5384 5385 length = 0; 5386 if (cdb->byte2 & SF_FMTDATA) { 5387 if (cdb->byte2 & SF_LONGLIST) 5388 length = sizeof(struct scsi_format_header_long); 5389 else 5390 length = sizeof(struct scsi_format_header_short); 5391 } 5392 5393 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5394 && (length > 0)) { 5395 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5396 ctsio->kern_data_len = length; 5397 ctsio->kern_total_len = length; 5398 ctsio->kern_data_resid = 0; 5399 ctsio->kern_rel_offset = 0; 5400 ctsio->kern_sg_entries = 0; 5401 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5402 ctsio->be_move_done = ctl_config_move_done; 5403 ctl_datamove((union ctl_io *)ctsio); 5404 5405 return (CTL_RETVAL_COMPLETE); 5406 } 5407 5408 defect_list_len = 0; 5409 5410 if (cdb->byte2 & SF_FMTDATA) { 5411 if (cdb->byte2 & SF_LONGLIST) { 5412 struct scsi_format_header_long *header; 5413 5414 header = (struct scsi_format_header_long *) 5415 ctsio->kern_data_ptr; 5416 5417 defect_list_len = scsi_4btoul(header->defect_list_len); 5418 if (defect_list_len != 0) { 5419 ctl_set_invalid_field(ctsio, 5420 /*sks_valid*/ 1, 5421 /*command*/ 0, 5422 /*field*/ 2, 5423 /*bit_valid*/ 0, 5424 /*bit*/ 0); 5425 goto bailout; 5426 } 5427 } else { 5428 struct scsi_format_header_short *header; 5429 5430 header = (struct scsi_format_header_short *) 5431 ctsio->kern_data_ptr; 5432 5433 defect_list_len = scsi_2btoul(header->defect_list_len); 5434 if (defect_list_len != 0) { 5435 ctl_set_invalid_field(ctsio, 5436 /*sks_valid*/ 1, 5437 /*command*/ 0, 5438 /*field*/ 2, 5439 /*bit_valid*/ 0, 5440 /*bit*/ 0); 5441 goto bailout; 5442 } 5443 } 5444 } 5445 5446 ctl_set_success(ctsio); 5447bailout: 5448 5449 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5450 free(ctsio->kern_data_ptr, M_CTL); 5451 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5452 } 5453 5454 ctl_done((union ctl_io *)ctsio); 5455 return (CTL_RETVAL_COMPLETE); 5456} 5457 5458int 5459ctl_read_buffer(struct ctl_scsiio *ctsio) 5460{ 5461 struct ctl_lun *lun = CTL_LUN(ctsio); 5462 uint64_t buffer_offset; 5463 uint32_t len; 5464 uint8_t byte2; 5465 static uint8_t descr[4]; 5466 static uint8_t echo_descr[4] = { 0 }; 5467 5468 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5469 5470 switch (ctsio->cdb[0]) { 5471 case READ_BUFFER: { 5472 struct scsi_read_buffer *cdb; 5473 5474 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5475 buffer_offset = scsi_3btoul(cdb->offset); 5476 len = scsi_3btoul(cdb->length); 5477 byte2 = cdb->byte2; 5478 break; 5479 } 5480 case READ_BUFFER_16: { 5481 struct scsi_read_buffer_16 *cdb; 5482 5483 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5484 buffer_offset = scsi_8btou64(cdb->offset); 5485 len = scsi_4btoul(cdb->length); 5486 byte2 = cdb->byte2; 5487 break; 5488 } 5489 default: /* This shouldn't happen. */ 5490 ctl_set_invalid_opcode(ctsio); 5491 ctl_done((union ctl_io *)ctsio); 5492 return (CTL_RETVAL_COMPLETE); 5493 } 5494 5495 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5496 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5497 ctl_set_invalid_field(ctsio, 5498 /*sks_valid*/ 1, 5499 /*command*/ 1, 5500 /*field*/ 6, 5501 /*bit_valid*/ 0, 5502 /*bit*/ 0); 5503 ctl_done((union ctl_io *)ctsio); 5504 return (CTL_RETVAL_COMPLETE); 5505 } 5506 5507 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5508 descr[0] = 0; 5509 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5510 ctsio->kern_data_ptr = descr; 5511 len = min(len, sizeof(descr)); 5512 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5513 ctsio->kern_data_ptr = echo_descr; 5514 len = min(len, sizeof(echo_descr)); 5515 } else { 5516 if (lun->write_buffer == NULL) { 5517 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5518 M_CTL, M_WAITOK); 5519 } 5520 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5521 } 5522 ctsio->kern_data_len = len; 5523 ctsio->kern_total_len = len; 5524 ctsio->kern_data_resid = 0; 5525 ctsio->kern_rel_offset = 0; 5526 ctsio->kern_sg_entries = 0; 5527 ctl_set_success(ctsio); 5528 ctsio->be_move_done = ctl_config_move_done; 5529 ctl_datamove((union ctl_io *)ctsio); 5530 return (CTL_RETVAL_COMPLETE); 5531} 5532 5533int 5534ctl_write_buffer(struct ctl_scsiio *ctsio) 5535{ 5536 struct ctl_lun *lun = CTL_LUN(ctsio); 5537 struct scsi_write_buffer *cdb; 5538 int buffer_offset, len; 5539 5540 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5541 5542 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5543 5544 len = scsi_3btoul(cdb->length); 5545 buffer_offset = scsi_3btoul(cdb->offset); 5546 5547 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5548 ctl_set_invalid_field(ctsio, 5549 /*sks_valid*/ 1, 5550 /*command*/ 1, 5551 /*field*/ 6, 5552 /*bit_valid*/ 0, 5553 /*bit*/ 0); 5554 ctl_done((union ctl_io *)ctsio); 5555 return (CTL_RETVAL_COMPLETE); 5556 } 5557 5558 /* 5559 * If we've got a kernel request that hasn't been malloced yet, 5560 * malloc it and tell the caller the data buffer is here. 5561 */ 5562 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5563 if (lun->write_buffer == NULL) { 5564 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5565 M_CTL, M_WAITOK); 5566 } 5567 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5568 ctsio->kern_data_len = len; 5569 ctsio->kern_total_len = len; 5570 ctsio->kern_data_resid = 0; 5571 ctsio->kern_rel_offset = 0; 5572 ctsio->kern_sg_entries = 0; 5573 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5574 ctsio->be_move_done = ctl_config_move_done; 5575 ctl_datamove((union ctl_io *)ctsio); 5576 5577 return (CTL_RETVAL_COMPLETE); 5578 } 5579 5580 ctl_set_success(ctsio); 5581 ctl_done((union ctl_io *)ctsio); 5582 return (CTL_RETVAL_COMPLETE); 5583} 5584 5585int 5586ctl_write_same(struct ctl_scsiio *ctsio) 5587{ 5588 struct ctl_lun *lun = CTL_LUN(ctsio); 5589 struct ctl_lba_len_flags *lbalen; 5590 uint64_t lba; 5591 uint32_t num_blocks; 5592 int len, retval; 5593 uint8_t byte2; 5594 5595 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5596 5597 switch (ctsio->cdb[0]) { 5598 case WRITE_SAME_10: { 5599 struct scsi_write_same_10 *cdb; 5600 5601 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5602 5603 lba = scsi_4btoul(cdb->addr); 5604 num_blocks = scsi_2btoul(cdb->length); 5605 byte2 = cdb->byte2; 5606 break; 5607 } 5608 case WRITE_SAME_16: { 5609 struct scsi_write_same_16 *cdb; 5610 5611 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5612 5613 lba = scsi_8btou64(cdb->addr); 5614 num_blocks = scsi_4btoul(cdb->length); 5615 byte2 = cdb->byte2; 5616 break; 5617 } 5618 default: 5619 /* 5620 * We got a command we don't support. This shouldn't 5621 * happen, commands should be filtered out above us. 5622 */ 5623 ctl_set_invalid_opcode(ctsio); 5624 ctl_done((union ctl_io *)ctsio); 5625 5626 return (CTL_RETVAL_COMPLETE); 5627 break; /* NOTREACHED */ 5628 } 5629 5630 /* ANCHOR flag can be used only together with UNMAP */ 5631 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5632 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5633 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5634 ctl_done((union ctl_io *)ctsio); 5635 return (CTL_RETVAL_COMPLETE); 5636 } 5637 5638 /* 5639 * The first check is to make sure we're in bounds, the second 5640 * check is to catch wrap-around problems. If the lba + num blocks 5641 * is less than the lba, then we've wrapped around and the block 5642 * range is invalid anyway. 5643 */ 5644 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5645 || ((lba + num_blocks) < lba)) { 5646 ctl_set_lba_out_of_range(ctsio, 5647 MAX(lba, lun->be_lun->maxlba + 1)); 5648 ctl_done((union ctl_io *)ctsio); 5649 return (CTL_RETVAL_COMPLETE); 5650 } 5651 5652 /* Zero number of blocks means "to the last logical block" */ 5653 if (num_blocks == 0) { 5654 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5655 ctl_set_invalid_field(ctsio, 5656 /*sks_valid*/ 0, 5657 /*command*/ 1, 5658 /*field*/ 0, 5659 /*bit_valid*/ 0, 5660 /*bit*/ 0); 5661 ctl_done((union ctl_io *)ctsio); 5662 return (CTL_RETVAL_COMPLETE); 5663 } 5664 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5665 } 5666 5667 len = lun->be_lun->blocksize; 5668 5669 /* 5670 * If we've got a kernel request that hasn't been malloced yet, 5671 * malloc it and tell the caller the data buffer is here. 5672 */ 5673 if ((byte2 & SWS_NDOB) == 0 && 5674 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5675 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5676 ctsio->kern_data_len = len; 5677 ctsio->kern_total_len = len; 5678 ctsio->kern_data_resid = 0; 5679 ctsio->kern_rel_offset = 0; 5680 ctsio->kern_sg_entries = 0; 5681 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5682 ctsio->be_move_done = ctl_config_move_done; 5683 ctl_datamove((union ctl_io *)ctsio); 5684 5685 return (CTL_RETVAL_COMPLETE); 5686 } 5687 5688 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5689 lbalen->lba = lba; 5690 lbalen->len = num_blocks; 5691 lbalen->flags = byte2; 5692 retval = lun->backend->config_write((union ctl_io *)ctsio); 5693 5694 return (retval); 5695} 5696 5697int 5698ctl_unmap(struct ctl_scsiio *ctsio) 5699{ 5700 struct ctl_lun *lun = CTL_LUN(ctsio); 5701 struct scsi_unmap *cdb; 5702 struct ctl_ptr_len_flags *ptrlen; 5703 struct scsi_unmap_header *hdr; 5704 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5705 uint64_t lba; 5706 uint32_t num_blocks; 5707 int len, retval; 5708 uint8_t byte2; 5709 5710 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5711 5712 cdb = (struct scsi_unmap *)ctsio->cdb; 5713 len = scsi_2btoul(cdb->length); 5714 byte2 = cdb->byte2; 5715 5716 /* 5717 * If we've got a kernel request that hasn't been malloced yet, 5718 * malloc it and tell the caller the data buffer is here. 5719 */ 5720 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5721 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5722 ctsio->kern_data_len = len; 5723 ctsio->kern_total_len = len; 5724 ctsio->kern_data_resid = 0; 5725 ctsio->kern_rel_offset = 0; 5726 ctsio->kern_sg_entries = 0; 5727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5728 ctsio->be_move_done = ctl_config_move_done; 5729 ctl_datamove((union ctl_io *)ctsio); 5730 5731 return (CTL_RETVAL_COMPLETE); 5732 } 5733 5734 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5735 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5736 if (len < sizeof (*hdr) || 5737 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5738 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5739 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5740 ctl_set_invalid_field(ctsio, 5741 /*sks_valid*/ 0, 5742 /*command*/ 0, 5743 /*field*/ 0, 5744 /*bit_valid*/ 0, 5745 /*bit*/ 0); 5746 goto done; 5747 } 5748 len = scsi_2btoul(hdr->desc_length); 5749 buf = (struct scsi_unmap_desc *)(hdr + 1); 5750 end = buf + len / sizeof(*buf); 5751 5752 endnz = buf; 5753 for (range = buf; range < end; range++) { 5754 lba = scsi_8btou64(range->lba); 5755 num_blocks = scsi_4btoul(range->length); 5756 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5757 || ((lba + num_blocks) < lba)) { 5758 ctl_set_lba_out_of_range(ctsio, 5759 MAX(lba, lun->be_lun->maxlba + 1)); 5760 ctl_done((union ctl_io *)ctsio); 5761 return (CTL_RETVAL_COMPLETE); 5762 } 5763 if (num_blocks != 0) 5764 endnz = range + 1; 5765 } 5766 5767 /* 5768 * Block backend can not handle zero last range. 5769 * Filter it out and return if there is nothing left. 5770 */ 5771 len = (uint8_t *)endnz - (uint8_t *)buf; 5772 if (len == 0) { 5773 ctl_set_success(ctsio); 5774 goto done; 5775 } 5776 5777 mtx_lock(&lun->lun_lock); 5778 ptrlen = (struct ctl_ptr_len_flags *) 5779 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5780 ptrlen->ptr = (void *)buf; 5781 ptrlen->len = len; 5782 ptrlen->flags = byte2; 5783 ctl_check_blocked(lun); 5784 mtx_unlock(&lun->lun_lock); 5785 5786 retval = lun->backend->config_write((union ctl_io *)ctsio); 5787 return (retval); 5788 5789done: 5790 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5791 free(ctsio->kern_data_ptr, M_CTL); 5792 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5793 } 5794 ctl_done((union ctl_io *)ctsio); 5795 return (CTL_RETVAL_COMPLETE); 5796} 5797 5798int 5799ctl_default_page_handler(struct ctl_scsiio *ctsio, 5800 struct ctl_page_index *page_index, uint8_t *page_ptr) 5801{ 5802 struct ctl_lun *lun = CTL_LUN(ctsio); 5803 uint8_t *current_cp; 5804 int set_ua; 5805 uint32_t initidx; 5806 5807 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5808 set_ua = 0; 5809 5810 current_cp = (page_index->page_data + (page_index->page_len * 5811 CTL_PAGE_CURRENT)); 5812 5813 mtx_lock(&lun->lun_lock); 5814 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5815 memcpy(current_cp, page_ptr, page_index->page_len); 5816 set_ua = 1; 5817 } 5818 if (set_ua != 0) 5819 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5820 mtx_unlock(&lun->lun_lock); 5821 if (set_ua) { 5822 ctl_isc_announce_mode(lun, 5823 ctl_get_initindex(&ctsio->io_hdr.nexus), 5824 page_index->page_code, page_index->subpage); 5825 } 5826 return (CTL_RETVAL_COMPLETE); 5827} 5828 5829static void 5830ctl_ie_timer(void *arg) 5831{ 5832 struct ctl_lun *lun = arg; 5833 uint64_t t; 5834 5835 if (lun->ie_asc == 0) 5836 return; 5837 5838 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5839 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5840 else 5841 lun->ie_reported = 0; 5842 5843 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5844 lun->ie_reportcnt++; 5845 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5846 if (t == 0 || t == UINT32_MAX) 5847 t = 3000; /* 5 min */ 5848 callout_schedule(&lun->ie_callout, t * hz / 10); 5849 } 5850} 5851 5852int 5853ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5854 struct ctl_page_index *page_index, uint8_t *page_ptr) 5855{ 5856 struct ctl_lun *lun = CTL_LUN(ctsio); 5857 struct scsi_info_exceptions_page *pg; 5858 uint64_t t; 5859 5860 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5861 5862 pg = (struct scsi_info_exceptions_page *)page_ptr; 5863 mtx_lock(&lun->lun_lock); 5864 if (pg->info_flags & SIEP_FLAGS_TEST) { 5865 lun->ie_asc = 0x5d; 5866 lun->ie_ascq = 0xff; 5867 if (pg->mrie == SIEP_MRIE_UA) { 5868 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5869 lun->ie_reported = 1; 5870 } else { 5871 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5872 lun->ie_reported = -1; 5873 } 5874 lun->ie_reportcnt = 1; 5875 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5876 lun->ie_reportcnt++; 5877 t = scsi_4btoul(pg->interval_timer); 5878 if (t == 0 || t == UINT32_MAX) 5879 t = 3000; /* 5 min */ 5880 callout_reset(&lun->ie_callout, t * hz / 10, 5881 ctl_ie_timer, lun); 5882 } 5883 } else { 5884 lun->ie_asc = 0; 5885 lun->ie_ascq = 0; 5886 lun->ie_reported = 1; 5887 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5888 lun->ie_reportcnt = UINT32_MAX; 5889 callout_stop(&lun->ie_callout); 5890 } 5891 mtx_unlock(&lun->lun_lock); 5892 return (CTL_RETVAL_COMPLETE); 5893} 5894 5895static int 5896ctl_do_mode_select(union ctl_io *io) 5897{ 5898 struct ctl_lun *lun = CTL_LUN(io); 5899 struct scsi_mode_page_header *page_header; 5900 struct ctl_page_index *page_index; 5901 struct ctl_scsiio *ctsio; 5902 int page_len, page_len_offset, page_len_size; 5903 union ctl_modepage_info *modepage_info; 5904 uint16_t *len_left, *len_used; 5905 int retval, i; 5906 5907 ctsio = &io->scsiio; 5908 page_index = NULL; 5909 page_len = 0; 5910 5911 modepage_info = (union ctl_modepage_info *) 5912 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5913 len_left = &modepage_info->header.len_left; 5914 len_used = &modepage_info->header.len_used; 5915 5916do_next_page: 5917 5918 page_header = (struct scsi_mode_page_header *) 5919 (ctsio->kern_data_ptr + *len_used); 5920 5921 if (*len_left == 0) { 5922 free(ctsio->kern_data_ptr, M_CTL); 5923 ctl_set_success(ctsio); 5924 ctl_done((union ctl_io *)ctsio); 5925 return (CTL_RETVAL_COMPLETE); 5926 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5927 5928 free(ctsio->kern_data_ptr, M_CTL); 5929 ctl_set_param_len_error(ctsio); 5930 ctl_done((union ctl_io *)ctsio); 5931 return (CTL_RETVAL_COMPLETE); 5932 5933 } else if ((page_header->page_code & SMPH_SPF) 5934 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5935 5936 free(ctsio->kern_data_ptr, M_CTL); 5937 ctl_set_param_len_error(ctsio); 5938 ctl_done((union ctl_io *)ctsio); 5939 return (CTL_RETVAL_COMPLETE); 5940 } 5941 5942 5943 /* 5944 * XXX KDM should we do something with the block descriptor? 5945 */ 5946 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5947 page_index = &lun->mode_pages.index[i]; 5948 if (lun->be_lun->lun_type == T_DIRECT && 5949 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5950 continue; 5951 if (lun->be_lun->lun_type == T_PROCESSOR && 5952 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5953 continue; 5954 if (lun->be_lun->lun_type == T_CDROM && 5955 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5956 continue; 5957 5958 if ((page_index->page_code & SMPH_PC_MASK) != 5959 (page_header->page_code & SMPH_PC_MASK)) 5960 continue; 5961 5962 /* 5963 * If neither page has a subpage code, then we've got a 5964 * match. 5965 */ 5966 if (((page_index->page_code & SMPH_SPF) == 0) 5967 && ((page_header->page_code & SMPH_SPF) == 0)) { 5968 page_len = page_header->page_length; 5969 break; 5970 } 5971 5972 /* 5973 * If both pages have subpages, then the subpage numbers 5974 * have to match. 5975 */ 5976 if ((page_index->page_code & SMPH_SPF) 5977 && (page_header->page_code & SMPH_SPF)) { 5978 struct scsi_mode_page_header_sp *sph; 5979 5980 sph = (struct scsi_mode_page_header_sp *)page_header; 5981 if (page_index->subpage == sph->subpage) { 5982 page_len = scsi_2btoul(sph->page_length); 5983 break; 5984 } 5985 } 5986 } 5987 5988 /* 5989 * If we couldn't find the page, or if we don't have a mode select 5990 * handler for it, send back an error to the user. 5991 */ 5992 if ((i >= CTL_NUM_MODE_PAGES) 5993 || (page_index->select_handler == NULL)) { 5994 ctl_set_invalid_field(ctsio, 5995 /*sks_valid*/ 1, 5996 /*command*/ 0, 5997 /*field*/ *len_used, 5998 /*bit_valid*/ 0, 5999 /*bit*/ 0); 6000 free(ctsio->kern_data_ptr, M_CTL); 6001 ctl_done((union ctl_io *)ctsio); 6002 return (CTL_RETVAL_COMPLETE); 6003 } 6004 6005 if (page_index->page_code & SMPH_SPF) { 6006 page_len_offset = 2; 6007 page_len_size = 2; 6008 } else { 6009 page_len_size = 1; 6010 page_len_offset = 1; 6011 } 6012 6013 /* 6014 * If the length the initiator gives us isn't the one we specify in 6015 * the mode page header, or if they didn't specify enough data in 6016 * the CDB to avoid truncating this page, kick out the request. 6017 */ 6018 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6019 ctl_set_invalid_field(ctsio, 6020 /*sks_valid*/ 1, 6021 /*command*/ 0, 6022 /*field*/ *len_used + page_len_offset, 6023 /*bit_valid*/ 0, 6024 /*bit*/ 0); 6025 free(ctsio->kern_data_ptr, M_CTL); 6026 ctl_done((union ctl_io *)ctsio); 6027 return (CTL_RETVAL_COMPLETE); 6028 } 6029 if (*len_left < page_index->page_len) { 6030 free(ctsio->kern_data_ptr, M_CTL); 6031 ctl_set_param_len_error(ctsio); 6032 ctl_done((union ctl_io *)ctsio); 6033 return (CTL_RETVAL_COMPLETE); 6034 } 6035 6036 /* 6037 * Run through the mode page, checking to make sure that the bits 6038 * the user changed are actually legal for him to change. 6039 */ 6040 for (i = 0; i < page_index->page_len; i++) { 6041 uint8_t *user_byte, *change_mask, *current_byte; 6042 int bad_bit; 6043 int j; 6044 6045 user_byte = (uint8_t *)page_header + i; 6046 change_mask = page_index->page_data + 6047 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6048 current_byte = page_index->page_data + 6049 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6050 6051 /* 6052 * Check to see whether the user set any bits in this byte 6053 * that he is not allowed to set. 6054 */ 6055 if ((*user_byte & ~(*change_mask)) == 6056 (*current_byte & ~(*change_mask))) 6057 continue; 6058 6059 /* 6060 * Go through bit by bit to determine which one is illegal. 6061 */ 6062 bad_bit = 0; 6063 for (j = 7; j >= 0; j--) { 6064 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6065 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6066 bad_bit = i; 6067 break; 6068 } 6069 } 6070 ctl_set_invalid_field(ctsio, 6071 /*sks_valid*/ 1, 6072 /*command*/ 0, 6073 /*field*/ *len_used + i, 6074 /*bit_valid*/ 1, 6075 /*bit*/ bad_bit); 6076 free(ctsio->kern_data_ptr, M_CTL); 6077 ctl_done((union ctl_io *)ctsio); 6078 return (CTL_RETVAL_COMPLETE); 6079 } 6080 6081 /* 6082 * Decrement these before we call the page handler, since we may 6083 * end up getting called back one way or another before the handler 6084 * returns to this context. 6085 */ 6086 *len_left -= page_index->page_len; 6087 *len_used += page_index->page_len; 6088 6089 retval = page_index->select_handler(ctsio, page_index, 6090 (uint8_t *)page_header); 6091 6092 /* 6093 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6094 * wait until this queued command completes to finish processing 6095 * the mode page. If it returns anything other than 6096 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6097 * already set the sense information, freed the data pointer, and 6098 * completed the io for us. 6099 */ 6100 if (retval != CTL_RETVAL_COMPLETE) 6101 goto bailout_no_done; 6102 6103 /* 6104 * If the initiator sent us more than one page, parse the next one. 6105 */ 6106 if (*len_left > 0) 6107 goto do_next_page; 6108 6109 ctl_set_success(ctsio); 6110 free(ctsio->kern_data_ptr, M_CTL); 6111 ctl_done((union ctl_io *)ctsio); 6112 6113bailout_no_done: 6114 6115 return (CTL_RETVAL_COMPLETE); 6116 6117} 6118 6119int 6120ctl_mode_select(struct ctl_scsiio *ctsio) 6121{ 6122 struct ctl_lun *lun = CTL_LUN(ctsio); 6123 union ctl_modepage_info *modepage_info; 6124 int bd_len, i, header_size, param_len, pf, rtd, sp; 6125 uint32_t initidx; 6126 6127 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6128 switch (ctsio->cdb[0]) { 6129 case MODE_SELECT_6: { 6130 struct scsi_mode_select_6 *cdb; 6131 6132 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6133 6134 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6135 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6136 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6137 param_len = cdb->length; 6138 header_size = sizeof(struct scsi_mode_header_6); 6139 break; 6140 } 6141 case MODE_SELECT_10: { 6142 struct scsi_mode_select_10 *cdb; 6143 6144 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6145 6146 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6147 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6148 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6149 param_len = scsi_2btoul(cdb->length); 6150 header_size = sizeof(struct scsi_mode_header_10); 6151 break; 6152 } 6153 default: 6154 ctl_set_invalid_opcode(ctsio); 6155 ctl_done((union ctl_io *)ctsio); 6156 return (CTL_RETVAL_COMPLETE); 6157 } 6158 6159 if (rtd) { 6160 if (param_len != 0) { 6161 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6162 /*command*/ 1, /*field*/ 0, 6163 /*bit_valid*/ 0, /*bit*/ 0); 6164 ctl_done((union ctl_io *)ctsio); 6165 return (CTL_RETVAL_COMPLETE); 6166 } 6167 6168 /* Revert to defaults. */ 6169 ctl_init_page_index(lun); 6170 mtx_lock(&lun->lun_lock); 6171 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6172 mtx_unlock(&lun->lun_lock); 6173 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6174 ctl_isc_announce_mode(lun, -1, 6175 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6176 lun->mode_pages.index[i].subpage); 6177 } 6178 ctl_set_success(ctsio); 6179 ctl_done((union ctl_io *)ctsio); 6180 return (CTL_RETVAL_COMPLETE); 6181 } 6182 6183 /* 6184 * From SPC-3: 6185 * "A parameter list length of zero indicates that the Data-Out Buffer 6186 * shall be empty. This condition shall not be considered as an error." 6187 */ 6188 if (param_len == 0) { 6189 ctl_set_success(ctsio); 6190 ctl_done((union ctl_io *)ctsio); 6191 return (CTL_RETVAL_COMPLETE); 6192 } 6193 6194 /* 6195 * Since we'll hit this the first time through, prior to 6196 * allocation, we don't need to free a data buffer here. 6197 */ 6198 if (param_len < header_size) { 6199 ctl_set_param_len_error(ctsio); 6200 ctl_done((union ctl_io *)ctsio); 6201 return (CTL_RETVAL_COMPLETE); 6202 } 6203 6204 /* 6205 * Allocate the data buffer and grab the user's data. In theory, 6206 * we shouldn't have to sanity check the parameter list length here 6207 * because the maximum size is 64K. We should be able to malloc 6208 * that much without too many problems. 6209 */ 6210 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6211 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6212 ctsio->kern_data_len = param_len; 6213 ctsio->kern_total_len = param_len; 6214 ctsio->kern_data_resid = 0; 6215 ctsio->kern_rel_offset = 0; 6216 ctsio->kern_sg_entries = 0; 6217 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6218 ctsio->be_move_done = ctl_config_move_done; 6219 ctl_datamove((union ctl_io *)ctsio); 6220 6221 return (CTL_RETVAL_COMPLETE); 6222 } 6223 6224 switch (ctsio->cdb[0]) { 6225 case MODE_SELECT_6: { 6226 struct scsi_mode_header_6 *mh6; 6227 6228 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6229 bd_len = mh6->blk_desc_len; 6230 break; 6231 } 6232 case MODE_SELECT_10: { 6233 struct scsi_mode_header_10 *mh10; 6234 6235 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6236 bd_len = scsi_2btoul(mh10->blk_desc_len); 6237 break; 6238 } 6239 default: 6240 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6241 } 6242 6243 if (param_len < (header_size + bd_len)) { 6244 free(ctsio->kern_data_ptr, M_CTL); 6245 ctl_set_param_len_error(ctsio); 6246 ctl_done((union ctl_io *)ctsio); 6247 return (CTL_RETVAL_COMPLETE); 6248 } 6249 6250 /* 6251 * Set the IO_CONT flag, so that if this I/O gets passed to 6252 * ctl_config_write_done(), it'll get passed back to 6253 * ctl_do_mode_select() for further processing, or completion if 6254 * we're all done. 6255 */ 6256 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6257 ctsio->io_cont = ctl_do_mode_select; 6258 6259 modepage_info = (union ctl_modepage_info *) 6260 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6261 memset(modepage_info, 0, sizeof(*modepage_info)); 6262 modepage_info->header.len_left = param_len - header_size - bd_len; 6263 modepage_info->header.len_used = header_size + bd_len; 6264 6265 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6266} 6267 6268int 6269ctl_mode_sense(struct ctl_scsiio *ctsio) 6270{ 6271 struct ctl_lun *lun = CTL_LUN(ctsio); 6272 int pc, page_code, dbd, llba, subpage; 6273 int alloc_len, page_len, header_len, total_len; 6274 struct scsi_mode_block_descr *block_desc; 6275 struct ctl_page_index *page_index; 6276 6277 dbd = 0; 6278 llba = 0; 6279 block_desc = NULL; 6280 6281 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6282 6283 switch (ctsio->cdb[0]) { 6284 case MODE_SENSE_6: { 6285 struct scsi_mode_sense_6 *cdb; 6286 6287 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6288 6289 header_len = sizeof(struct scsi_mode_hdr_6); 6290 if (cdb->byte2 & SMS_DBD) 6291 dbd = 1; 6292 else 6293 header_len += sizeof(struct scsi_mode_block_descr); 6294 6295 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6296 page_code = cdb->page & SMS_PAGE_CODE; 6297 subpage = cdb->subpage; 6298 alloc_len = cdb->length; 6299 break; 6300 } 6301 case MODE_SENSE_10: { 6302 struct scsi_mode_sense_10 *cdb; 6303 6304 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6305 6306 header_len = sizeof(struct scsi_mode_hdr_10); 6307 6308 if (cdb->byte2 & SMS_DBD) 6309 dbd = 1; 6310 else 6311 header_len += sizeof(struct scsi_mode_block_descr); 6312 if (cdb->byte2 & SMS10_LLBAA) 6313 llba = 1; 6314 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6315 page_code = cdb->page & SMS_PAGE_CODE; 6316 subpage = cdb->subpage; 6317 alloc_len = scsi_2btoul(cdb->length); 6318 break; 6319 } 6320 default: 6321 ctl_set_invalid_opcode(ctsio); 6322 ctl_done((union ctl_io *)ctsio); 6323 return (CTL_RETVAL_COMPLETE); 6324 break; /* NOTREACHED */ 6325 } 6326 6327 /* 6328 * We have to make a first pass through to calculate the size of 6329 * the pages that match the user's query. Then we allocate enough 6330 * memory to hold it, and actually copy the data into the buffer. 6331 */ 6332 switch (page_code) { 6333 case SMS_ALL_PAGES_PAGE: { 6334 u_int i; 6335 6336 page_len = 0; 6337 6338 /* 6339 * At the moment, values other than 0 and 0xff here are 6340 * reserved according to SPC-3. 6341 */ 6342 if ((subpage != SMS_SUBPAGE_PAGE_0) 6343 && (subpage != SMS_SUBPAGE_ALL)) { 6344 ctl_set_invalid_field(ctsio, 6345 /*sks_valid*/ 1, 6346 /*command*/ 1, 6347 /*field*/ 3, 6348 /*bit_valid*/ 0, 6349 /*bit*/ 0); 6350 ctl_done((union ctl_io *)ctsio); 6351 return (CTL_RETVAL_COMPLETE); 6352 } 6353 6354 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6355 page_index = &lun->mode_pages.index[i]; 6356 6357 /* Make sure the page is supported for this dev type */ 6358 if (lun->be_lun->lun_type == T_DIRECT && 6359 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6360 continue; 6361 if (lun->be_lun->lun_type == T_PROCESSOR && 6362 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6363 continue; 6364 if (lun->be_lun->lun_type == T_CDROM && 6365 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6366 continue; 6367 6368 /* 6369 * We don't use this subpage if the user didn't 6370 * request all subpages. 6371 */ 6372 if ((page_index->subpage != 0) 6373 && (subpage == SMS_SUBPAGE_PAGE_0)) 6374 continue; 6375 6376#if 0 6377 printf("found page %#x len %d\n", 6378 page_index->page_code & SMPH_PC_MASK, 6379 page_index->page_len); 6380#endif 6381 page_len += page_index->page_len; 6382 } 6383 break; 6384 } 6385 default: { 6386 u_int i; 6387 6388 page_len = 0; 6389 6390 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6391 page_index = &lun->mode_pages.index[i]; 6392 6393 /* Make sure the page is supported for this dev type */ 6394 if (lun->be_lun->lun_type == T_DIRECT && 6395 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6396 continue; 6397 if (lun->be_lun->lun_type == T_PROCESSOR && 6398 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6399 continue; 6400 if (lun->be_lun->lun_type == T_CDROM && 6401 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6402 continue; 6403 6404 /* Look for the right page code */ 6405 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6406 continue; 6407 6408 /* Look for the right subpage or the subpage wildcard*/ 6409 if ((page_index->subpage != subpage) 6410 && (subpage != SMS_SUBPAGE_ALL)) 6411 continue; 6412 6413#if 0 6414 printf("found page %#x len %d\n", 6415 page_index->page_code & SMPH_PC_MASK, 6416 page_index->page_len); 6417#endif 6418 6419 page_len += page_index->page_len; 6420 } 6421 6422 if (page_len == 0) { 6423 ctl_set_invalid_field(ctsio, 6424 /*sks_valid*/ 1, 6425 /*command*/ 1, 6426 /*field*/ 2, 6427 /*bit_valid*/ 1, 6428 /*bit*/ 5); 6429 ctl_done((union ctl_io *)ctsio); 6430 return (CTL_RETVAL_COMPLETE); 6431 } 6432 break; 6433 } 6434 } 6435 6436 total_len = header_len + page_len; 6437#if 0 6438 printf("header_len = %d, page_len = %d, total_len = %d\n", 6439 header_len, page_len, total_len); 6440#endif 6441 6442 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6443 ctsio->kern_sg_entries = 0; 6444 ctsio->kern_data_resid = 0; 6445 ctsio->kern_rel_offset = 0; 6446 if (total_len < alloc_len) { 6447 ctsio->residual = alloc_len - total_len; 6448 ctsio->kern_data_len = total_len; 6449 ctsio->kern_total_len = total_len; 6450 } else { 6451 ctsio->residual = 0; 6452 ctsio->kern_data_len = alloc_len; 6453 ctsio->kern_total_len = alloc_len; 6454 } 6455 6456 switch (ctsio->cdb[0]) { 6457 case MODE_SENSE_6: { 6458 struct scsi_mode_hdr_6 *header; 6459 6460 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6461 6462 header->datalen = MIN(total_len - 1, 254); 6463 if (lun->be_lun->lun_type == T_DIRECT) { 6464 header->dev_specific = 0x10; /* DPOFUA */ 6465 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6466 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6467 header->dev_specific |= 0x80; /* WP */ 6468 } 6469 if (dbd) 6470 header->block_descr_len = 0; 6471 else 6472 header->block_descr_len = 6473 sizeof(struct scsi_mode_block_descr); 6474 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6475 break; 6476 } 6477 case MODE_SENSE_10: { 6478 struct scsi_mode_hdr_10 *header; 6479 int datalen; 6480 6481 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6482 6483 datalen = MIN(total_len - 2, 65533); 6484 scsi_ulto2b(datalen, header->datalen); 6485 if (lun->be_lun->lun_type == T_DIRECT) { 6486 header->dev_specific = 0x10; /* DPOFUA */ 6487 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6488 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6489 header->dev_specific |= 0x80; /* WP */ 6490 } 6491 if (dbd) 6492 scsi_ulto2b(0, header->block_descr_len); 6493 else 6494 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6495 header->block_descr_len); 6496 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6497 break; 6498 } 6499 default: 6500 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6501 } 6502 6503 /* 6504 * If we've got a disk, use its blocksize in the block 6505 * descriptor. Otherwise, just set it to 0. 6506 */ 6507 if (dbd == 0) { 6508 if (lun->be_lun->lun_type == T_DIRECT) 6509 scsi_ulto3b(lun->be_lun->blocksize, 6510 block_desc->block_len); 6511 else 6512 scsi_ulto3b(0, block_desc->block_len); 6513 } 6514 6515 switch (page_code) { 6516 case SMS_ALL_PAGES_PAGE: { 6517 int i, data_used; 6518 6519 data_used = header_len; 6520 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6521 struct ctl_page_index *page_index; 6522 6523 page_index = &lun->mode_pages.index[i]; 6524 if (lun->be_lun->lun_type == T_DIRECT && 6525 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6526 continue; 6527 if (lun->be_lun->lun_type == T_PROCESSOR && 6528 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6529 continue; 6530 if (lun->be_lun->lun_type == T_CDROM && 6531 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6532 continue; 6533 6534 /* 6535 * We don't use this subpage if the user didn't 6536 * request all subpages. We already checked (above) 6537 * to make sure the user only specified a subpage 6538 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6539 */ 6540 if ((page_index->subpage != 0) 6541 && (subpage == SMS_SUBPAGE_PAGE_0)) 6542 continue; 6543 6544 /* 6545 * Call the handler, if it exists, to update the 6546 * page to the latest values. 6547 */ 6548 if (page_index->sense_handler != NULL) 6549 page_index->sense_handler(ctsio, page_index,pc); 6550 6551 memcpy(ctsio->kern_data_ptr + data_used, 6552 page_index->page_data + 6553 (page_index->page_len * pc), 6554 page_index->page_len); 6555 data_used += page_index->page_len; 6556 } 6557 break; 6558 } 6559 default: { 6560 int i, data_used; 6561 6562 data_used = header_len; 6563 6564 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6565 struct ctl_page_index *page_index; 6566 6567 page_index = &lun->mode_pages.index[i]; 6568 6569 /* Look for the right page code */ 6570 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6571 continue; 6572 6573 /* Look for the right subpage or the subpage wildcard*/ 6574 if ((page_index->subpage != subpage) 6575 && (subpage != SMS_SUBPAGE_ALL)) 6576 continue; 6577 6578 /* Make sure the page is supported for this dev type */ 6579 if (lun->be_lun->lun_type == T_DIRECT && 6580 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6581 continue; 6582 if (lun->be_lun->lun_type == T_PROCESSOR && 6583 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6584 continue; 6585 if (lun->be_lun->lun_type == T_CDROM && 6586 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6587 continue; 6588 6589 /* 6590 * Call the handler, if it exists, to update the 6591 * page to the latest values. 6592 */ 6593 if (page_index->sense_handler != NULL) 6594 page_index->sense_handler(ctsio, page_index,pc); 6595 6596 memcpy(ctsio->kern_data_ptr + data_used, 6597 page_index->page_data + 6598 (page_index->page_len * pc), 6599 page_index->page_len); 6600 data_used += page_index->page_len; 6601 } 6602 break; 6603 } 6604 } 6605 6606 ctl_set_success(ctsio); 6607 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6608 ctsio->be_move_done = ctl_config_move_done; 6609 ctl_datamove((union ctl_io *)ctsio); 6610 return (CTL_RETVAL_COMPLETE); 6611} 6612 6613int 6614ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6615 struct ctl_page_index *page_index, 6616 int pc) 6617{ 6618 struct ctl_lun *lun = CTL_LUN(ctsio); 6619 struct scsi_log_param_header *phdr; 6620 uint8_t *data; 6621 uint64_t val; 6622 6623 data = page_index->page_data; 6624 6625 if (lun->backend->lun_attr != NULL && 6626 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6627 != UINT64_MAX) { 6628 phdr = (struct scsi_log_param_header *)data; 6629 scsi_ulto2b(0x0001, phdr->param_code); 6630 phdr->param_control = SLP_LBIN | SLP_LP; 6631 phdr->param_len = 8; 6632 data = (uint8_t *)(phdr + 1); 6633 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6634 data[4] = 0x02; /* per-pool */ 6635 data += phdr->param_len; 6636 } 6637 6638 if (lun->backend->lun_attr != NULL && 6639 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6640 != UINT64_MAX) { 6641 phdr = (struct scsi_log_param_header *)data; 6642 scsi_ulto2b(0x0002, phdr->param_code); 6643 phdr->param_control = SLP_LBIN | SLP_LP; 6644 phdr->param_len = 8; 6645 data = (uint8_t *)(phdr + 1); 6646 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6647 data[4] = 0x01; /* per-LUN */ 6648 data += phdr->param_len; 6649 } 6650 6651 if (lun->backend->lun_attr != NULL && 6652 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6653 != UINT64_MAX) { 6654 phdr = (struct scsi_log_param_header *)data; 6655 scsi_ulto2b(0x00f1, phdr->param_code); 6656 phdr->param_control = SLP_LBIN | SLP_LP; 6657 phdr->param_len = 8; 6658 data = (uint8_t *)(phdr + 1); 6659 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6660 data[4] = 0x02; /* per-pool */ 6661 data += phdr->param_len; 6662 } 6663 6664 if (lun->backend->lun_attr != NULL && 6665 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6666 != UINT64_MAX) { 6667 phdr = (struct scsi_log_param_header *)data; 6668 scsi_ulto2b(0x00f2, phdr->param_code); 6669 phdr->param_control = SLP_LBIN | SLP_LP; 6670 phdr->param_len = 8; 6671 data = (uint8_t *)(phdr + 1); 6672 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6673 data[4] = 0x02; /* per-pool */ 6674 data += phdr->param_len; 6675 } 6676 6677 page_index->page_len = data - page_index->page_data; 6678 return (0); 6679} 6680 6681int 6682ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6683 struct ctl_page_index *page_index, 6684 int pc) 6685{ 6686 struct ctl_lun *lun = CTL_LUN(ctsio); 6687 struct stat_page *data; 6688 uint64_t rn, wn, rb, wb; 6689 struct bintime rt, wt; 6690 int i; 6691 6692 data = (struct stat_page *)page_index->page_data; 6693 6694 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6695 data->sap.hdr.param_control = SLP_LBIN; 6696 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6697 sizeof(struct scsi_log_param_header); 6698 rn = wn = rb = wb = 0; 6699 bintime_clear(&rt); 6700 bintime_clear(&wt); 6701 for (i = 0; i < CTL_MAX_PORTS; i++) { 6702 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6703 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6704 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6705 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6706 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6707 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6708 } 6709 scsi_u64to8b(rn, data->sap.read_num); 6710 scsi_u64to8b(wn, data->sap.write_num); 6711 if (lun->stats.blocksize > 0) { 6712 scsi_u64to8b(wb / lun->stats.blocksize, 6713 data->sap.recvieved_lba); 6714 scsi_u64to8b(rb / lun->stats.blocksize, 6715 data->sap.transmitted_lba); 6716 } 6717 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6718 data->sap.read_int); 6719 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6720 data->sap.write_int); 6721 scsi_u64to8b(0, data->sap.weighted_num); 6722 scsi_u64to8b(0, data->sap.weighted_int); 6723 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6724 data->it.hdr.param_control = SLP_LBIN; 6725 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6726 sizeof(struct scsi_log_param_header); 6727#ifdef CTL_TIME_IO 6728 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6729#endif 6730 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6731 data->it.hdr.param_control = SLP_LBIN; 6732 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6733 sizeof(struct scsi_log_param_header); 6734 scsi_ulto4b(3, data->ti.exponent); 6735 scsi_ulto4b(1, data->ti.integer); 6736 return (0); 6737} 6738 6739int 6740ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6741 struct ctl_page_index *page_index, 6742 int pc) 6743{ 6744 struct ctl_lun *lun = CTL_LUN(ctsio); 6745 struct scsi_log_informational_exceptions *data; 6746 6747 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6748 6749 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6750 data->hdr.param_control = SLP_LBIN; 6751 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6752 sizeof(struct scsi_log_param_header); 6753 data->ie_asc = lun->ie_asc; 6754 data->ie_ascq = lun->ie_ascq; 6755 data->temperature = 0xff; 6756 return (0); 6757} 6758 6759int 6760ctl_log_sense(struct ctl_scsiio *ctsio) 6761{ 6762 struct ctl_lun *lun = CTL_LUN(ctsio); 6763 int i, pc, page_code, subpage; 6764 int alloc_len, total_len; 6765 struct ctl_page_index *page_index; 6766 struct scsi_log_sense *cdb; 6767 struct scsi_log_header *header; 6768 6769 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6770 6771 cdb = (struct scsi_log_sense *)ctsio->cdb; 6772 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6773 page_code = cdb->page & SLS_PAGE_CODE; 6774 subpage = cdb->subpage; 6775 alloc_len = scsi_2btoul(cdb->length); 6776 6777 page_index = NULL; 6778 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6779 page_index = &lun->log_pages.index[i]; 6780 6781 /* Look for the right page code */ 6782 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6783 continue; 6784 6785 /* Look for the right subpage or the subpage wildcard*/ 6786 if (page_index->subpage != subpage) 6787 continue; 6788 6789 break; 6790 } 6791 if (i >= CTL_NUM_LOG_PAGES) { 6792 ctl_set_invalid_field(ctsio, 6793 /*sks_valid*/ 1, 6794 /*command*/ 1, 6795 /*field*/ 2, 6796 /*bit_valid*/ 0, 6797 /*bit*/ 0); 6798 ctl_done((union ctl_io *)ctsio); 6799 return (CTL_RETVAL_COMPLETE); 6800 } 6801 6802 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6803 6804 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6805 ctsio->kern_sg_entries = 0; 6806 ctsio->kern_data_resid = 0; 6807 ctsio->kern_rel_offset = 0; 6808 if (total_len < alloc_len) { 6809 ctsio->residual = alloc_len - total_len; 6810 ctsio->kern_data_len = total_len; 6811 ctsio->kern_total_len = total_len; 6812 } else { 6813 ctsio->residual = 0; 6814 ctsio->kern_data_len = alloc_len; 6815 ctsio->kern_total_len = alloc_len; 6816 } 6817 6818 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6819 header->page = page_index->page_code; 6820 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6821 header->page |= SL_DS; 6822 if (page_index->subpage) { 6823 header->page |= SL_SPF; 6824 header->subpage = page_index->subpage; 6825 } 6826 scsi_ulto2b(page_index->page_len, header->datalen); 6827 6828 /* 6829 * Call the handler, if it exists, to update the 6830 * page to the latest values. 6831 */ 6832 if (page_index->sense_handler != NULL) 6833 page_index->sense_handler(ctsio, page_index, pc); 6834 6835 memcpy(header + 1, page_index->page_data, page_index->page_len); 6836 6837 ctl_set_success(ctsio); 6838 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6839 ctsio->be_move_done = ctl_config_move_done; 6840 ctl_datamove((union ctl_io *)ctsio); 6841 return (CTL_RETVAL_COMPLETE); 6842} 6843 6844int 6845ctl_read_capacity(struct ctl_scsiio *ctsio) 6846{ 6847 struct ctl_lun *lun = CTL_LUN(ctsio); 6848 struct scsi_read_capacity *cdb; 6849 struct scsi_read_capacity_data *data; 6850 uint32_t lba; 6851 6852 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6853 6854 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6855 6856 lba = scsi_4btoul(cdb->addr); 6857 if (((cdb->pmi & SRC_PMI) == 0) 6858 && (lba != 0)) { 6859 ctl_set_invalid_field(/*ctsio*/ ctsio, 6860 /*sks_valid*/ 1, 6861 /*command*/ 1, 6862 /*field*/ 2, 6863 /*bit_valid*/ 0, 6864 /*bit*/ 0); 6865 ctl_done((union ctl_io *)ctsio); 6866 return (CTL_RETVAL_COMPLETE); 6867 } 6868 6869 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6870 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6871 ctsio->residual = 0; 6872 ctsio->kern_data_len = sizeof(*data); 6873 ctsio->kern_total_len = sizeof(*data); 6874 ctsio->kern_data_resid = 0; 6875 ctsio->kern_rel_offset = 0; 6876 ctsio->kern_sg_entries = 0; 6877 6878 /* 6879 * If the maximum LBA is greater than 0xfffffffe, the user must 6880 * issue a SERVICE ACTION IN (16) command, with the read capacity 6881 * serivce action set. 6882 */ 6883 if (lun->be_lun->maxlba > 0xfffffffe) 6884 scsi_ulto4b(0xffffffff, data->addr); 6885 else 6886 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6887 6888 /* 6889 * XXX KDM this may not be 512 bytes... 6890 */ 6891 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6892 6893 ctl_set_success(ctsio); 6894 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6895 ctsio->be_move_done = ctl_config_move_done; 6896 ctl_datamove((union ctl_io *)ctsio); 6897 return (CTL_RETVAL_COMPLETE); 6898} 6899 6900int 6901ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6902{ 6903 struct ctl_lun *lun = CTL_LUN(ctsio); 6904 struct scsi_read_capacity_16 *cdb; 6905 struct scsi_read_capacity_data_long *data; 6906 uint64_t lba; 6907 uint32_t alloc_len; 6908 6909 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6910 6911 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6912 6913 alloc_len = scsi_4btoul(cdb->alloc_len); 6914 lba = scsi_8btou64(cdb->addr); 6915 6916 if ((cdb->reladr & SRC16_PMI) 6917 && (lba != 0)) { 6918 ctl_set_invalid_field(/*ctsio*/ ctsio, 6919 /*sks_valid*/ 1, 6920 /*command*/ 1, 6921 /*field*/ 2, 6922 /*bit_valid*/ 0, 6923 /*bit*/ 0); 6924 ctl_done((union ctl_io *)ctsio); 6925 return (CTL_RETVAL_COMPLETE); 6926 } 6927 6928 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6929 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6930 6931 if (sizeof(*data) < alloc_len) { 6932 ctsio->residual = alloc_len - sizeof(*data); 6933 ctsio->kern_data_len = sizeof(*data); 6934 ctsio->kern_total_len = sizeof(*data); 6935 } else { 6936 ctsio->residual = 0; 6937 ctsio->kern_data_len = alloc_len; 6938 ctsio->kern_total_len = alloc_len; 6939 } 6940 ctsio->kern_data_resid = 0; 6941 ctsio->kern_rel_offset = 0; 6942 ctsio->kern_sg_entries = 0; 6943 6944 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6945 /* XXX KDM this may not be 512 bytes... */ 6946 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6947 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6948 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6949 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6950 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6951 6952 ctl_set_success(ctsio); 6953 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6954 ctsio->be_move_done = ctl_config_move_done; 6955 ctl_datamove((union ctl_io *)ctsio); 6956 return (CTL_RETVAL_COMPLETE); 6957} 6958 6959int 6960ctl_get_lba_status(struct ctl_scsiio *ctsio) 6961{ 6962 struct ctl_lun *lun = CTL_LUN(ctsio); 6963 struct scsi_get_lba_status *cdb; 6964 struct scsi_get_lba_status_data *data; 6965 struct ctl_lba_len_flags *lbalen; 6966 uint64_t lba; 6967 uint32_t alloc_len, total_len; 6968 int retval; 6969 6970 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6971 6972 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6973 lba = scsi_8btou64(cdb->addr); 6974 alloc_len = scsi_4btoul(cdb->alloc_len); 6975 6976 if (lba > lun->be_lun->maxlba) { 6977 ctl_set_lba_out_of_range(ctsio, lba); 6978 ctl_done((union ctl_io *)ctsio); 6979 return (CTL_RETVAL_COMPLETE); 6980 } 6981 6982 total_len = sizeof(*data) + sizeof(data->descr[0]); 6983 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6984 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6985 6986 if (total_len < alloc_len) { 6987 ctsio->residual = alloc_len - total_len; 6988 ctsio->kern_data_len = total_len; 6989 ctsio->kern_total_len = total_len; 6990 } else { 6991 ctsio->residual = 0; 6992 ctsio->kern_data_len = alloc_len; 6993 ctsio->kern_total_len = alloc_len; 6994 } 6995 ctsio->kern_data_resid = 0; 6996 ctsio->kern_rel_offset = 0; 6997 ctsio->kern_sg_entries = 0; 6998 6999 /* Fill dummy data in case backend can't tell anything. */ 7000 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7001 scsi_u64to8b(lba, data->descr[0].addr); 7002 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7003 data->descr[0].length); 7004 data->descr[0].status = 0; /* Mapped or unknown. */ 7005 7006 ctl_set_success(ctsio); 7007 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7008 ctsio->be_move_done = ctl_config_move_done; 7009 7010 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7011 lbalen->lba = lba; 7012 lbalen->len = total_len; 7013 lbalen->flags = 0; 7014 retval = lun->backend->config_read((union ctl_io *)ctsio); 7015 return (CTL_RETVAL_COMPLETE); 7016} 7017 7018int 7019ctl_read_defect(struct ctl_scsiio *ctsio) 7020{ 7021 struct scsi_read_defect_data_10 *ccb10; 7022 struct scsi_read_defect_data_12 *ccb12; 7023 struct scsi_read_defect_data_hdr_10 *data10; 7024 struct scsi_read_defect_data_hdr_12 *data12; 7025 uint32_t alloc_len, data_len; 7026 uint8_t format; 7027 7028 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7029 7030 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7031 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7032 format = ccb10->format; 7033 alloc_len = scsi_2btoul(ccb10->alloc_length); 7034 data_len = sizeof(*data10); 7035 } else { 7036 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7037 format = ccb12->format; 7038 alloc_len = scsi_4btoul(ccb12->alloc_length); 7039 data_len = sizeof(*data12); 7040 } 7041 if (alloc_len == 0) { 7042 ctl_set_success(ctsio); 7043 ctl_done((union ctl_io *)ctsio); 7044 return (CTL_RETVAL_COMPLETE); 7045 } 7046 7047 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7048 if (data_len < alloc_len) { 7049 ctsio->residual = alloc_len - data_len; 7050 ctsio->kern_data_len = data_len; 7051 ctsio->kern_total_len = data_len; 7052 } else { 7053 ctsio->residual = 0; 7054 ctsio->kern_data_len = alloc_len; 7055 ctsio->kern_total_len = alloc_len; 7056 } 7057 ctsio->kern_data_resid = 0; 7058 ctsio->kern_rel_offset = 0; 7059 ctsio->kern_sg_entries = 0; 7060 7061 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7062 data10 = (struct scsi_read_defect_data_hdr_10 *) 7063 ctsio->kern_data_ptr; 7064 data10->format = format; 7065 scsi_ulto2b(0, data10->length); 7066 } else { 7067 data12 = (struct scsi_read_defect_data_hdr_12 *) 7068 ctsio->kern_data_ptr; 7069 data12->format = format; 7070 scsi_ulto2b(0, data12->generation); 7071 scsi_ulto4b(0, data12->length); 7072 } 7073 7074 ctl_set_success(ctsio); 7075 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7076 ctsio->be_move_done = ctl_config_move_done; 7077 ctl_datamove((union ctl_io *)ctsio); 7078 return (CTL_RETVAL_COMPLETE); 7079} 7080 7081int 7082ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7083{ 7084 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7085 struct ctl_lun *lun = CTL_LUN(ctsio); 7086 struct scsi_maintenance_in *cdb; 7087 int retval; 7088 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7089 int num_ha_groups, num_target_ports, shared_group; 7090 struct ctl_port *port; 7091 struct scsi_target_group_data *rtg_ptr; 7092 struct scsi_target_group_data_extended *rtg_ext_ptr; 7093 struct scsi_target_port_group_descriptor *tpg_desc; 7094 7095 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7096 7097 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7098 retval = CTL_RETVAL_COMPLETE; 7099 7100 switch (cdb->byte2 & STG_PDF_MASK) { 7101 case STG_PDF_LENGTH: 7102 ext = 0; 7103 break; 7104 case STG_PDF_EXTENDED: 7105 ext = 1; 7106 break; 7107 default: 7108 ctl_set_invalid_field(/*ctsio*/ ctsio, 7109 /*sks_valid*/ 1, 7110 /*command*/ 1, 7111 /*field*/ 2, 7112 /*bit_valid*/ 1, 7113 /*bit*/ 5); 7114 ctl_done((union ctl_io *)ctsio); 7115 return(retval); 7116 } 7117 7118 num_target_ports = 0; 7119 shared_group = (softc->is_single != 0); 7120 mtx_lock(&softc->ctl_lock); 7121 STAILQ_FOREACH(port, &softc->port_list, links) { 7122 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7123 continue; 7124 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7125 continue; 7126 num_target_ports++; 7127 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7128 shared_group = 1; 7129 } 7130 mtx_unlock(&softc->ctl_lock); 7131 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7132 7133 if (ext) 7134 total_len = sizeof(struct scsi_target_group_data_extended); 7135 else 7136 total_len = sizeof(struct scsi_target_group_data); 7137 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7138 (shared_group + num_ha_groups) + 7139 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7140 7141 alloc_len = scsi_4btoul(cdb->length); 7142 7143 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7144 7145 ctsio->kern_sg_entries = 0; 7146 7147 if (total_len < alloc_len) { 7148 ctsio->residual = alloc_len - total_len; 7149 ctsio->kern_data_len = total_len; 7150 ctsio->kern_total_len = total_len; 7151 } else { 7152 ctsio->residual = 0; 7153 ctsio->kern_data_len = alloc_len; 7154 ctsio->kern_total_len = alloc_len; 7155 } 7156 ctsio->kern_data_resid = 0; 7157 ctsio->kern_rel_offset = 0; 7158 7159 if (ext) { 7160 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7161 ctsio->kern_data_ptr; 7162 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7163 rtg_ext_ptr->format_type = 0x10; 7164 rtg_ext_ptr->implicit_transition_time = 0; 7165 tpg_desc = &rtg_ext_ptr->groups[0]; 7166 } else { 7167 rtg_ptr = (struct scsi_target_group_data *) 7168 ctsio->kern_data_ptr; 7169 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7170 tpg_desc = &rtg_ptr->groups[0]; 7171 } 7172 7173 mtx_lock(&softc->ctl_lock); 7174 pg = softc->port_min / softc->port_cnt; 7175 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7176 /* Some shelf is known to be primary. */ 7177 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7178 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7179 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7180 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7181 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7182 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7183 else 7184 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7185 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7186 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7187 } else { 7188 ts = os; 7189 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7190 } 7191 } else { 7192 /* No known primary shelf. */ 7193 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7194 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7195 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7196 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7197 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7198 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7199 } else { 7200 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7201 } 7202 } 7203 if (shared_group) { 7204 tpg_desc->pref_state = ts; 7205 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7206 TPG_U_SUP | TPG_T_SUP; 7207 scsi_ulto2b(1, tpg_desc->target_port_group); 7208 tpg_desc->status = TPG_IMPLICIT; 7209 pc = 0; 7210 STAILQ_FOREACH(port, &softc->port_list, links) { 7211 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7212 continue; 7213 if (!softc->is_single && 7214 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7215 continue; 7216 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7217 continue; 7218 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7219 relative_target_port_identifier); 7220 pc++; 7221 } 7222 tpg_desc->target_port_count = pc; 7223 tpg_desc = (struct scsi_target_port_group_descriptor *) 7224 &tpg_desc->descriptors[pc]; 7225 } 7226 for (g = 0; g < num_ha_groups; g++) { 7227 tpg_desc->pref_state = (g == pg) ? ts : os; 7228 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7229 TPG_U_SUP | TPG_T_SUP; 7230 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7231 tpg_desc->status = TPG_IMPLICIT; 7232 pc = 0; 7233 STAILQ_FOREACH(port, &softc->port_list, links) { 7234 if (port->targ_port < g * softc->port_cnt || 7235 port->targ_port >= (g + 1) * softc->port_cnt) 7236 continue; 7237 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7238 continue; 7239 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7240 continue; 7241 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7242 continue; 7243 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7244 relative_target_port_identifier); 7245 pc++; 7246 } 7247 tpg_desc->target_port_count = pc; 7248 tpg_desc = (struct scsi_target_port_group_descriptor *) 7249 &tpg_desc->descriptors[pc]; 7250 } 7251 mtx_unlock(&softc->ctl_lock); 7252 7253 ctl_set_success(ctsio); 7254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7255 ctsio->be_move_done = ctl_config_move_done; 7256 ctl_datamove((union ctl_io *)ctsio); 7257 return(retval); 7258} 7259 7260int 7261ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7262{ 7263 struct ctl_lun *lun = CTL_LUN(ctsio); 7264 struct scsi_report_supported_opcodes *cdb; 7265 const struct ctl_cmd_entry *entry, *sentry; 7266 struct scsi_report_supported_opcodes_all *all; 7267 struct scsi_report_supported_opcodes_descr *descr; 7268 struct scsi_report_supported_opcodes_one *one; 7269 int retval; 7270 int alloc_len, total_len; 7271 int opcode, service_action, i, j, num; 7272 7273 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7274 7275 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7276 retval = CTL_RETVAL_COMPLETE; 7277 7278 opcode = cdb->requested_opcode; 7279 service_action = scsi_2btoul(cdb->requested_service_action); 7280 switch (cdb->options & RSO_OPTIONS_MASK) { 7281 case RSO_OPTIONS_ALL: 7282 num = 0; 7283 for (i = 0; i < 256; i++) { 7284 entry = &ctl_cmd_table[i]; 7285 if (entry->flags & CTL_CMD_FLAG_SA5) { 7286 for (j = 0; j < 32; j++) { 7287 sentry = &((const struct ctl_cmd_entry *) 7288 entry->execute)[j]; 7289 if (ctl_cmd_applicable( 7290 lun->be_lun->lun_type, sentry)) 7291 num++; 7292 } 7293 } else { 7294 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7295 entry)) 7296 num++; 7297 } 7298 } 7299 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7300 num * sizeof(struct scsi_report_supported_opcodes_descr); 7301 break; 7302 case RSO_OPTIONS_OC: 7303 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7304 ctl_set_invalid_field(/*ctsio*/ ctsio, 7305 /*sks_valid*/ 1, 7306 /*command*/ 1, 7307 /*field*/ 2, 7308 /*bit_valid*/ 1, 7309 /*bit*/ 2); 7310 ctl_done((union ctl_io *)ctsio); 7311 return (CTL_RETVAL_COMPLETE); 7312 } 7313 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7314 break; 7315 case RSO_OPTIONS_OC_SA: 7316 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7317 service_action >= 32) { 7318 ctl_set_invalid_field(/*ctsio*/ ctsio, 7319 /*sks_valid*/ 1, 7320 /*command*/ 1, 7321 /*field*/ 2, 7322 /*bit_valid*/ 1, 7323 /*bit*/ 2); 7324 ctl_done((union ctl_io *)ctsio); 7325 return (CTL_RETVAL_COMPLETE); 7326 } 7327 /* FALLTHROUGH */ 7328 case RSO_OPTIONS_OC_ASA: 7329 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7330 break; 7331 default: 7332 ctl_set_invalid_field(/*ctsio*/ ctsio, 7333 /*sks_valid*/ 1, 7334 /*command*/ 1, 7335 /*field*/ 2, 7336 /*bit_valid*/ 1, 7337 /*bit*/ 2); 7338 ctl_done((union ctl_io *)ctsio); 7339 return (CTL_RETVAL_COMPLETE); 7340 } 7341 7342 alloc_len = scsi_4btoul(cdb->length); 7343 7344 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7345 7346 ctsio->kern_sg_entries = 0; 7347 7348 if (total_len < alloc_len) { 7349 ctsio->residual = alloc_len - total_len; 7350 ctsio->kern_data_len = total_len; 7351 ctsio->kern_total_len = total_len; 7352 } else { 7353 ctsio->residual = 0; 7354 ctsio->kern_data_len = alloc_len; 7355 ctsio->kern_total_len = alloc_len; 7356 } 7357 ctsio->kern_data_resid = 0; 7358 ctsio->kern_rel_offset = 0; 7359 7360 switch (cdb->options & RSO_OPTIONS_MASK) { 7361 case RSO_OPTIONS_ALL: 7362 all = (struct scsi_report_supported_opcodes_all *) 7363 ctsio->kern_data_ptr; 7364 num = 0; 7365 for (i = 0; i < 256; i++) { 7366 entry = &ctl_cmd_table[i]; 7367 if (entry->flags & CTL_CMD_FLAG_SA5) { 7368 for (j = 0; j < 32; j++) { 7369 sentry = &((const struct ctl_cmd_entry *) 7370 entry->execute)[j]; 7371 if (!ctl_cmd_applicable( 7372 lun->be_lun->lun_type, sentry)) 7373 continue; 7374 descr = &all->descr[num++]; 7375 descr->opcode = i; 7376 scsi_ulto2b(j, descr->service_action); 7377 descr->flags = RSO_SERVACTV; 7378 scsi_ulto2b(sentry->length, 7379 descr->cdb_length); 7380 } 7381 } else { 7382 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7383 entry)) 7384 continue; 7385 descr = &all->descr[num++]; 7386 descr->opcode = i; 7387 scsi_ulto2b(0, descr->service_action); 7388 descr->flags = 0; 7389 scsi_ulto2b(entry->length, descr->cdb_length); 7390 } 7391 } 7392 scsi_ulto4b( 7393 num * sizeof(struct scsi_report_supported_opcodes_descr), 7394 all->length); 7395 break; 7396 case RSO_OPTIONS_OC: 7397 one = (struct scsi_report_supported_opcodes_one *) 7398 ctsio->kern_data_ptr; 7399 entry = &ctl_cmd_table[opcode]; 7400 goto fill_one; 7401 case RSO_OPTIONS_OC_SA: 7402 one = (struct scsi_report_supported_opcodes_one *) 7403 ctsio->kern_data_ptr; 7404 entry = &ctl_cmd_table[opcode]; 7405 entry = &((const struct ctl_cmd_entry *) 7406 entry->execute)[service_action]; 7407fill_one: 7408 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7409 one->support = 3; 7410 scsi_ulto2b(entry->length, one->cdb_length); 7411 one->cdb_usage[0] = opcode; 7412 memcpy(&one->cdb_usage[1], entry->usage, 7413 entry->length - 1); 7414 } else 7415 one->support = 1; 7416 break; 7417 case RSO_OPTIONS_OC_ASA: 7418 one = (struct scsi_report_supported_opcodes_one *) 7419 ctsio->kern_data_ptr; 7420 entry = &ctl_cmd_table[opcode]; 7421 if (entry->flags & CTL_CMD_FLAG_SA5) { 7422 entry = &((const struct ctl_cmd_entry *) 7423 entry->execute)[service_action]; 7424 } else if (service_action != 0) { 7425 one->support = 1; 7426 break; 7427 } 7428 goto fill_one; 7429 } 7430 7431 ctl_set_success(ctsio); 7432 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7433 ctsio->be_move_done = ctl_config_move_done; 7434 ctl_datamove((union ctl_io *)ctsio); 7435 return(retval); 7436} 7437 7438int 7439ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7440{ 7441 struct scsi_report_supported_tmf *cdb; 7442 struct scsi_report_supported_tmf_ext_data *data; 7443 int retval; 7444 int alloc_len, total_len; 7445 7446 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7447 7448 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7449 7450 retval = CTL_RETVAL_COMPLETE; 7451 7452 if (cdb->options & RST_REPD) 7453 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7454 else 7455 total_len = sizeof(struct scsi_report_supported_tmf_data); 7456 alloc_len = scsi_4btoul(cdb->length); 7457 7458 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7459 7460 ctsio->kern_sg_entries = 0; 7461 7462 if (total_len < alloc_len) { 7463 ctsio->residual = alloc_len - total_len; 7464 ctsio->kern_data_len = total_len; 7465 ctsio->kern_total_len = total_len; 7466 } else { 7467 ctsio->residual = 0; 7468 ctsio->kern_data_len = alloc_len; 7469 ctsio->kern_total_len = alloc_len; 7470 } 7471 ctsio->kern_data_resid = 0; 7472 ctsio->kern_rel_offset = 0; 7473 7474 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7475 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7476 RST_TRS; 7477 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7478 data->length = total_len - 4; 7479 7480 ctl_set_success(ctsio); 7481 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7482 ctsio->be_move_done = ctl_config_move_done; 7483 ctl_datamove((union ctl_io *)ctsio); 7484 return (retval); 7485} 7486 7487int 7488ctl_report_timestamp(struct ctl_scsiio *ctsio) 7489{ 7490 struct scsi_report_timestamp *cdb; 7491 struct scsi_report_timestamp_data *data; 7492 struct timeval tv; 7493 int64_t timestamp; 7494 int retval; 7495 int alloc_len, total_len; 7496 7497 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7498 7499 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7500 7501 retval = CTL_RETVAL_COMPLETE; 7502 7503 total_len = sizeof(struct scsi_report_timestamp_data); 7504 alloc_len = scsi_4btoul(cdb->length); 7505 7506 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7507 7508 ctsio->kern_sg_entries = 0; 7509 7510 if (total_len < alloc_len) { 7511 ctsio->residual = alloc_len - total_len; 7512 ctsio->kern_data_len = total_len; 7513 ctsio->kern_total_len = total_len; 7514 } else { 7515 ctsio->residual = 0; 7516 ctsio->kern_data_len = alloc_len; 7517 ctsio->kern_total_len = alloc_len; 7518 } 7519 ctsio->kern_data_resid = 0; 7520 ctsio->kern_rel_offset = 0; 7521 7522 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7523 scsi_ulto2b(sizeof(*data) - 2, data->length); 7524 data->origin = RTS_ORIG_OUTSIDE; 7525 getmicrotime(&tv); 7526 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7527 scsi_ulto4b(timestamp >> 16, data->timestamp); 7528 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7529 7530 ctl_set_success(ctsio); 7531 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7532 ctsio->be_move_done = ctl_config_move_done; 7533 ctl_datamove((union ctl_io *)ctsio); 7534 return (retval); 7535} 7536 7537int 7538ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7539{ 7540 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7541 struct ctl_lun *lun = CTL_LUN(ctsio); 7542 struct scsi_per_res_in *cdb; 7543 int alloc_len, total_len = 0; 7544 /* struct scsi_per_res_in_rsrv in_data; */ 7545 uint64_t key; 7546 7547 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7548 7549 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7550 7551 alloc_len = scsi_2btoul(cdb->length); 7552 7553retry: 7554 mtx_lock(&lun->lun_lock); 7555 switch (cdb->action) { 7556 case SPRI_RK: /* read keys */ 7557 total_len = sizeof(struct scsi_per_res_in_keys) + 7558 lun->pr_key_count * 7559 sizeof(struct scsi_per_res_key); 7560 break; 7561 case SPRI_RR: /* read reservation */ 7562 if (lun->flags & CTL_LUN_PR_RESERVED) 7563 total_len = sizeof(struct scsi_per_res_in_rsrv); 7564 else 7565 total_len = sizeof(struct scsi_per_res_in_header); 7566 break; 7567 case SPRI_RC: /* report capabilities */ 7568 total_len = sizeof(struct scsi_per_res_cap); 7569 break; 7570 case SPRI_RS: /* read full status */ 7571 total_len = sizeof(struct scsi_per_res_in_header) + 7572 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7573 lun->pr_key_count; 7574 break; 7575 default: 7576 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7577 } 7578 mtx_unlock(&lun->lun_lock); 7579 7580 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7581 7582 if (total_len < alloc_len) { 7583 ctsio->residual = alloc_len - total_len; 7584 ctsio->kern_data_len = total_len; 7585 ctsio->kern_total_len = total_len; 7586 } else { 7587 ctsio->residual = 0; 7588 ctsio->kern_data_len = alloc_len; 7589 ctsio->kern_total_len = alloc_len; 7590 } 7591 7592 ctsio->kern_data_resid = 0; 7593 ctsio->kern_rel_offset = 0; 7594 ctsio->kern_sg_entries = 0; 7595 7596 mtx_lock(&lun->lun_lock); 7597 switch (cdb->action) { 7598 case SPRI_RK: { // read keys 7599 struct scsi_per_res_in_keys *res_keys; 7600 int i, key_count; 7601 7602 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7603 7604 /* 7605 * We had to drop the lock to allocate our buffer, which 7606 * leaves time for someone to come in with another 7607 * persistent reservation. (That is unlikely, though, 7608 * since this should be the only persistent reservation 7609 * command active right now.) 7610 */ 7611 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7612 (lun->pr_key_count * 7613 sizeof(struct scsi_per_res_key)))){ 7614 mtx_unlock(&lun->lun_lock); 7615 free(ctsio->kern_data_ptr, M_CTL); 7616 printf("%s: reservation length changed, retrying\n", 7617 __func__); 7618 goto retry; 7619 } 7620 7621 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7622 7623 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7624 lun->pr_key_count, res_keys->header.length); 7625 7626 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7627 if ((key = ctl_get_prkey(lun, i)) == 0) 7628 continue; 7629 7630 /* 7631 * We used lun->pr_key_count to calculate the 7632 * size to allocate. If it turns out the number of 7633 * initiators with the registered flag set is 7634 * larger than that (i.e. they haven't been kept in 7635 * sync), we've got a problem. 7636 */ 7637 if (key_count >= lun->pr_key_count) { 7638 key_count++; 7639 continue; 7640 } 7641 scsi_u64to8b(key, res_keys->keys[key_count].key); 7642 key_count++; 7643 } 7644 break; 7645 } 7646 case SPRI_RR: { // read reservation 7647 struct scsi_per_res_in_rsrv *res; 7648 int tmp_len, header_only; 7649 7650 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7651 7652 scsi_ulto4b(lun->pr_generation, res->header.generation); 7653 7654 if (lun->flags & CTL_LUN_PR_RESERVED) 7655 { 7656 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7657 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7658 res->header.length); 7659 header_only = 0; 7660 } else { 7661 tmp_len = sizeof(struct scsi_per_res_in_header); 7662 scsi_ulto4b(0, res->header.length); 7663 header_only = 1; 7664 } 7665 7666 /* 7667 * We had to drop the lock to allocate our buffer, which 7668 * leaves time for someone to come in with another 7669 * persistent reservation. (That is unlikely, though, 7670 * since this should be the only persistent reservation 7671 * command active right now.) 7672 */ 7673 if (tmp_len != total_len) { 7674 mtx_unlock(&lun->lun_lock); 7675 free(ctsio->kern_data_ptr, M_CTL); 7676 printf("%s: reservation status changed, retrying\n", 7677 __func__); 7678 goto retry; 7679 } 7680 7681 /* 7682 * No reservation held, so we're done. 7683 */ 7684 if (header_only != 0) 7685 break; 7686 7687 /* 7688 * If the registration is an All Registrants type, the key 7689 * is 0, since it doesn't really matter. 7690 */ 7691 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7692 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7693 res->data.reservation); 7694 } 7695 res->data.scopetype = lun->pr_res_type; 7696 break; 7697 } 7698 case SPRI_RC: //report capabilities 7699 { 7700 struct scsi_per_res_cap *res_cap; 7701 uint16_t type_mask; 7702 7703 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7704 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7705 res_cap->flags1 = SPRI_CRH; 7706 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7707 type_mask = SPRI_TM_WR_EX_AR | 7708 SPRI_TM_EX_AC_RO | 7709 SPRI_TM_WR_EX_RO | 7710 SPRI_TM_EX_AC | 7711 SPRI_TM_WR_EX | 7712 SPRI_TM_EX_AC_AR; 7713 scsi_ulto2b(type_mask, res_cap->type_mask); 7714 break; 7715 } 7716 case SPRI_RS: { // read full status 7717 struct scsi_per_res_in_full *res_status; 7718 struct scsi_per_res_in_full_desc *res_desc; 7719 struct ctl_port *port; 7720 int i, len; 7721 7722 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7723 7724 /* 7725 * We had to drop the lock to allocate our buffer, which 7726 * leaves time for someone to come in with another 7727 * persistent reservation. (That is unlikely, though, 7728 * since this should be the only persistent reservation 7729 * command active right now.) 7730 */ 7731 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7732 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7733 lun->pr_key_count)){ 7734 mtx_unlock(&lun->lun_lock); 7735 free(ctsio->kern_data_ptr, M_CTL); 7736 printf("%s: reservation length changed, retrying\n", 7737 __func__); 7738 goto retry; 7739 } 7740 7741 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7742 7743 res_desc = &res_status->desc[0]; 7744 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7745 if ((key = ctl_get_prkey(lun, i)) == 0) 7746 continue; 7747 7748 scsi_u64to8b(key, res_desc->res_key.key); 7749 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7750 (lun->pr_res_idx == i || 7751 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7752 res_desc->flags = SPRI_FULL_R_HOLDER; 7753 res_desc->scopetype = lun->pr_res_type; 7754 } 7755 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7756 res_desc->rel_trgt_port_id); 7757 len = 0; 7758 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7759 if (port != NULL) 7760 len = ctl_create_iid(port, 7761 i % CTL_MAX_INIT_PER_PORT, 7762 res_desc->transport_id); 7763 scsi_ulto4b(len, res_desc->additional_length); 7764 res_desc = (struct scsi_per_res_in_full_desc *) 7765 &res_desc->transport_id[len]; 7766 } 7767 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7768 res_status->header.length); 7769 break; 7770 } 7771 default: 7772 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7773 } 7774 mtx_unlock(&lun->lun_lock); 7775 7776 ctl_set_success(ctsio); 7777 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7778 ctsio->be_move_done = ctl_config_move_done; 7779 ctl_datamove((union ctl_io *)ctsio); 7780 return (CTL_RETVAL_COMPLETE); 7781} 7782 7783/* 7784 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7785 * it should return. 7786 */ 7787static int 7788ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7789 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7790 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7791 struct scsi_per_res_out_parms* param) 7792{ 7793 union ctl_ha_msg persis_io; 7794 int i; 7795 7796 mtx_lock(&lun->lun_lock); 7797 if (sa_res_key == 0) { 7798 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7799 /* validate scope and type */ 7800 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7801 SPR_LU_SCOPE) { 7802 mtx_unlock(&lun->lun_lock); 7803 ctl_set_invalid_field(/*ctsio*/ ctsio, 7804 /*sks_valid*/ 1, 7805 /*command*/ 1, 7806 /*field*/ 2, 7807 /*bit_valid*/ 1, 7808 /*bit*/ 4); 7809 ctl_done((union ctl_io *)ctsio); 7810 return (1); 7811 } 7812 7813 if (type>8 || type==2 || type==4 || type==0) { 7814 mtx_unlock(&lun->lun_lock); 7815 ctl_set_invalid_field(/*ctsio*/ ctsio, 7816 /*sks_valid*/ 1, 7817 /*command*/ 1, 7818 /*field*/ 2, 7819 /*bit_valid*/ 1, 7820 /*bit*/ 0); 7821 ctl_done((union ctl_io *)ctsio); 7822 return (1); 7823 } 7824 7825 /* 7826 * Unregister everybody else and build UA for 7827 * them 7828 */ 7829 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7830 if (i == residx || ctl_get_prkey(lun, i) == 0) 7831 continue; 7832 7833 ctl_clr_prkey(lun, i); 7834 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7835 } 7836 lun->pr_key_count = 1; 7837 lun->pr_res_type = type; 7838 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7839 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7840 lun->pr_res_idx = residx; 7841 lun->pr_generation++; 7842 mtx_unlock(&lun->lun_lock); 7843 7844 /* send msg to other side */ 7845 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7846 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7847 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7848 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7849 persis_io.pr.pr_info.res_type = type; 7850 memcpy(persis_io.pr.pr_info.sa_res_key, 7851 param->serv_act_res_key, 7852 sizeof(param->serv_act_res_key)); 7853 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7854 sizeof(persis_io.pr), M_WAITOK); 7855 } else { 7856 /* not all registrants */ 7857 mtx_unlock(&lun->lun_lock); 7858 free(ctsio->kern_data_ptr, M_CTL); 7859 ctl_set_invalid_field(ctsio, 7860 /*sks_valid*/ 1, 7861 /*command*/ 0, 7862 /*field*/ 8, 7863 /*bit_valid*/ 0, 7864 /*bit*/ 0); 7865 ctl_done((union ctl_io *)ctsio); 7866 return (1); 7867 } 7868 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7869 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7870 int found = 0; 7871 7872 if (res_key == sa_res_key) { 7873 /* special case */ 7874 /* 7875 * The spec implies this is not good but doesn't 7876 * say what to do. There are two choices either 7877 * generate a res conflict or check condition 7878 * with illegal field in parameter data. Since 7879 * that is what is done when the sa_res_key is 7880 * zero I'll take that approach since this has 7881 * to do with the sa_res_key. 7882 */ 7883 mtx_unlock(&lun->lun_lock); 7884 free(ctsio->kern_data_ptr, M_CTL); 7885 ctl_set_invalid_field(ctsio, 7886 /*sks_valid*/ 1, 7887 /*command*/ 0, 7888 /*field*/ 8, 7889 /*bit_valid*/ 0, 7890 /*bit*/ 0); 7891 ctl_done((union ctl_io *)ctsio); 7892 return (1); 7893 } 7894 7895 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7896 if (ctl_get_prkey(lun, i) != sa_res_key) 7897 continue; 7898 7899 found = 1; 7900 ctl_clr_prkey(lun, i); 7901 lun->pr_key_count--; 7902 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7903 } 7904 if (!found) { 7905 mtx_unlock(&lun->lun_lock); 7906 free(ctsio->kern_data_ptr, M_CTL); 7907 ctl_set_reservation_conflict(ctsio); 7908 ctl_done((union ctl_io *)ctsio); 7909 return (CTL_RETVAL_COMPLETE); 7910 } 7911 lun->pr_generation++; 7912 mtx_unlock(&lun->lun_lock); 7913 7914 /* send msg to other side */ 7915 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7916 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7917 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7918 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7919 persis_io.pr.pr_info.res_type = type; 7920 memcpy(persis_io.pr.pr_info.sa_res_key, 7921 param->serv_act_res_key, 7922 sizeof(param->serv_act_res_key)); 7923 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7924 sizeof(persis_io.pr), M_WAITOK); 7925 } else { 7926 /* Reserved but not all registrants */ 7927 /* sa_res_key is res holder */ 7928 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7929 /* validate scope and type */ 7930 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7931 SPR_LU_SCOPE) { 7932 mtx_unlock(&lun->lun_lock); 7933 ctl_set_invalid_field(/*ctsio*/ ctsio, 7934 /*sks_valid*/ 1, 7935 /*command*/ 1, 7936 /*field*/ 2, 7937 /*bit_valid*/ 1, 7938 /*bit*/ 4); 7939 ctl_done((union ctl_io *)ctsio); 7940 return (1); 7941 } 7942 7943 if (type>8 || type==2 || type==4 || type==0) { 7944 mtx_unlock(&lun->lun_lock); 7945 ctl_set_invalid_field(/*ctsio*/ ctsio, 7946 /*sks_valid*/ 1, 7947 /*command*/ 1, 7948 /*field*/ 2, 7949 /*bit_valid*/ 1, 7950 /*bit*/ 0); 7951 ctl_done((union ctl_io *)ctsio); 7952 return (1); 7953 } 7954 7955 /* 7956 * Do the following: 7957 * if sa_res_key != res_key remove all 7958 * registrants w/sa_res_key and generate UA 7959 * for these registrants(Registrations 7960 * Preempted) if it wasn't an exclusive 7961 * reservation generate UA(Reservations 7962 * Preempted) for all other registered nexuses 7963 * if the type has changed. Establish the new 7964 * reservation and holder. If res_key and 7965 * sa_res_key are the same do the above 7966 * except don't unregister the res holder. 7967 */ 7968 7969 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7970 if (i == residx || ctl_get_prkey(lun, i) == 0) 7971 continue; 7972 7973 if (sa_res_key == ctl_get_prkey(lun, i)) { 7974 ctl_clr_prkey(lun, i); 7975 lun->pr_key_count--; 7976 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7977 } else if (type != lun->pr_res_type && 7978 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 7979 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 7980 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7981 } 7982 } 7983 lun->pr_res_type = type; 7984 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7985 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7986 lun->pr_res_idx = residx; 7987 else 7988 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7989 lun->pr_generation++; 7990 mtx_unlock(&lun->lun_lock); 7991 7992 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7993 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7994 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7995 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7996 persis_io.pr.pr_info.res_type = type; 7997 memcpy(persis_io.pr.pr_info.sa_res_key, 7998 param->serv_act_res_key, 7999 sizeof(param->serv_act_res_key)); 8000 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8001 sizeof(persis_io.pr), M_WAITOK); 8002 } else { 8003 /* 8004 * sa_res_key is not the res holder just 8005 * remove registrants 8006 */ 8007 int found=0; 8008 8009 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8010 if (sa_res_key != ctl_get_prkey(lun, i)) 8011 continue; 8012 8013 found = 1; 8014 ctl_clr_prkey(lun, i); 8015 lun->pr_key_count--; 8016 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8017 } 8018 8019 if (!found) { 8020 mtx_unlock(&lun->lun_lock); 8021 free(ctsio->kern_data_ptr, M_CTL); 8022 ctl_set_reservation_conflict(ctsio); 8023 ctl_done((union ctl_io *)ctsio); 8024 return (1); 8025 } 8026 lun->pr_generation++; 8027 mtx_unlock(&lun->lun_lock); 8028 8029 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8030 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8031 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8032 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8033 persis_io.pr.pr_info.res_type = type; 8034 memcpy(persis_io.pr.pr_info.sa_res_key, 8035 param->serv_act_res_key, 8036 sizeof(param->serv_act_res_key)); 8037 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8038 sizeof(persis_io.pr), M_WAITOK); 8039 } 8040 } 8041 return (0); 8042} 8043 8044static void 8045ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8046{ 8047 uint64_t sa_res_key; 8048 int i; 8049 8050 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8051 8052 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8053 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8054 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8055 if (sa_res_key == 0) { 8056 /* 8057 * Unregister everybody else and build UA for 8058 * them 8059 */ 8060 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8061 if (i == msg->pr.pr_info.residx || 8062 ctl_get_prkey(lun, i) == 0) 8063 continue; 8064 8065 ctl_clr_prkey(lun, i); 8066 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8067 } 8068 8069 lun->pr_key_count = 1; 8070 lun->pr_res_type = msg->pr.pr_info.res_type; 8071 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8072 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8073 lun->pr_res_idx = msg->pr.pr_info.residx; 8074 } else { 8075 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8076 if (sa_res_key == ctl_get_prkey(lun, i)) 8077 continue; 8078 8079 ctl_clr_prkey(lun, i); 8080 lun->pr_key_count--; 8081 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8082 } 8083 } 8084 } else { 8085 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8086 if (i == msg->pr.pr_info.residx || 8087 ctl_get_prkey(lun, i) == 0) 8088 continue; 8089 8090 if (sa_res_key == ctl_get_prkey(lun, i)) { 8091 ctl_clr_prkey(lun, i); 8092 lun->pr_key_count--; 8093 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8094 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8095 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8096 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8097 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8098 } 8099 } 8100 lun->pr_res_type = msg->pr.pr_info.res_type; 8101 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8102 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8103 lun->pr_res_idx = msg->pr.pr_info.residx; 8104 else 8105 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8106 } 8107 lun->pr_generation++; 8108 8109} 8110 8111 8112int 8113ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8114{ 8115 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8116 struct ctl_lun *lun = CTL_LUN(ctsio); 8117 int retval; 8118 u_int32_t param_len; 8119 struct scsi_per_res_out *cdb; 8120 struct scsi_per_res_out_parms* param; 8121 uint32_t residx; 8122 uint64_t res_key, sa_res_key, key; 8123 uint8_t type; 8124 union ctl_ha_msg persis_io; 8125 int i; 8126 8127 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8128 8129 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8130 retval = CTL_RETVAL_COMPLETE; 8131 8132 /* 8133 * We only support whole-LUN scope. The scope & type are ignored for 8134 * register, register and ignore existing key and clear. 8135 * We sometimes ignore scope and type on preempts too!! 8136 * Verify reservation type here as well. 8137 */ 8138 type = cdb->scope_type & SPR_TYPE_MASK; 8139 if ((cdb->action == SPRO_RESERVE) 8140 || (cdb->action == SPRO_RELEASE)) { 8141 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8142 ctl_set_invalid_field(/*ctsio*/ ctsio, 8143 /*sks_valid*/ 1, 8144 /*command*/ 1, 8145 /*field*/ 2, 8146 /*bit_valid*/ 1, 8147 /*bit*/ 4); 8148 ctl_done((union ctl_io *)ctsio); 8149 return (CTL_RETVAL_COMPLETE); 8150 } 8151 8152 if (type>8 || type==2 || type==4 || type==0) { 8153 ctl_set_invalid_field(/*ctsio*/ ctsio, 8154 /*sks_valid*/ 1, 8155 /*command*/ 1, 8156 /*field*/ 2, 8157 /*bit_valid*/ 1, 8158 /*bit*/ 0); 8159 ctl_done((union ctl_io *)ctsio); 8160 return (CTL_RETVAL_COMPLETE); 8161 } 8162 } 8163 8164 param_len = scsi_4btoul(cdb->length); 8165 8166 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8167 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8168 ctsio->kern_data_len = param_len; 8169 ctsio->kern_total_len = param_len; 8170 ctsio->kern_data_resid = 0; 8171 ctsio->kern_rel_offset = 0; 8172 ctsio->kern_sg_entries = 0; 8173 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8174 ctsio->be_move_done = ctl_config_move_done; 8175 ctl_datamove((union ctl_io *)ctsio); 8176 8177 return (CTL_RETVAL_COMPLETE); 8178 } 8179 8180 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8181 8182 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8183 res_key = scsi_8btou64(param->res_key.key); 8184 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8185 8186 /* 8187 * Validate the reservation key here except for SPRO_REG_IGNO 8188 * This must be done for all other service actions 8189 */ 8190 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8191 mtx_lock(&lun->lun_lock); 8192 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8193 if (res_key != key) { 8194 /* 8195 * The current key passed in doesn't match 8196 * the one the initiator previously 8197 * registered. 8198 */ 8199 mtx_unlock(&lun->lun_lock); 8200 free(ctsio->kern_data_ptr, M_CTL); 8201 ctl_set_reservation_conflict(ctsio); 8202 ctl_done((union ctl_io *)ctsio); 8203 return (CTL_RETVAL_COMPLETE); 8204 } 8205 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8206 /* 8207 * We are not registered 8208 */ 8209 mtx_unlock(&lun->lun_lock); 8210 free(ctsio->kern_data_ptr, M_CTL); 8211 ctl_set_reservation_conflict(ctsio); 8212 ctl_done((union ctl_io *)ctsio); 8213 return (CTL_RETVAL_COMPLETE); 8214 } else if (res_key != 0) { 8215 /* 8216 * We are not registered and trying to register but 8217 * the register key isn't zero. 8218 */ 8219 mtx_unlock(&lun->lun_lock); 8220 free(ctsio->kern_data_ptr, M_CTL); 8221 ctl_set_reservation_conflict(ctsio); 8222 ctl_done((union ctl_io *)ctsio); 8223 return (CTL_RETVAL_COMPLETE); 8224 } 8225 mtx_unlock(&lun->lun_lock); 8226 } 8227 8228 switch (cdb->action & SPRO_ACTION_MASK) { 8229 case SPRO_REGISTER: 8230 case SPRO_REG_IGNO: { 8231 8232#if 0 8233 printf("Registration received\n"); 8234#endif 8235 8236 /* 8237 * We don't support any of these options, as we report in 8238 * the read capabilities request (see 8239 * ctl_persistent_reserve_in(), above). 8240 */ 8241 if ((param->flags & SPR_SPEC_I_PT) 8242 || (param->flags & SPR_ALL_TG_PT) 8243 || (param->flags & SPR_APTPL)) { 8244 int bit_ptr; 8245 8246 if (param->flags & SPR_APTPL) 8247 bit_ptr = 0; 8248 else if (param->flags & SPR_ALL_TG_PT) 8249 bit_ptr = 2; 8250 else /* SPR_SPEC_I_PT */ 8251 bit_ptr = 3; 8252 8253 free(ctsio->kern_data_ptr, M_CTL); 8254 ctl_set_invalid_field(ctsio, 8255 /*sks_valid*/ 1, 8256 /*command*/ 0, 8257 /*field*/ 20, 8258 /*bit_valid*/ 1, 8259 /*bit*/ bit_ptr); 8260 ctl_done((union ctl_io *)ctsio); 8261 return (CTL_RETVAL_COMPLETE); 8262 } 8263 8264 mtx_lock(&lun->lun_lock); 8265 8266 /* 8267 * The initiator wants to clear the 8268 * key/unregister. 8269 */ 8270 if (sa_res_key == 0) { 8271 if ((res_key == 0 8272 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8273 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8274 && ctl_get_prkey(lun, residx) == 0)) { 8275 mtx_unlock(&lun->lun_lock); 8276 goto done; 8277 } 8278 8279 ctl_clr_prkey(lun, residx); 8280 lun->pr_key_count--; 8281 8282 if (residx == lun->pr_res_idx) { 8283 lun->flags &= ~CTL_LUN_PR_RESERVED; 8284 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8285 8286 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8287 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8288 lun->pr_key_count) { 8289 /* 8290 * If the reservation is a registrants 8291 * only type we need to generate a UA 8292 * for other registered inits. The 8293 * sense code should be RESERVATIONS 8294 * RELEASED 8295 */ 8296 8297 for (i = softc->init_min; i < softc->init_max; i++){ 8298 if (ctl_get_prkey(lun, i) == 0) 8299 continue; 8300 ctl_est_ua(lun, i, 8301 CTL_UA_RES_RELEASE); 8302 } 8303 } 8304 lun->pr_res_type = 0; 8305 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8306 if (lun->pr_key_count==0) { 8307 lun->flags &= ~CTL_LUN_PR_RESERVED; 8308 lun->pr_res_type = 0; 8309 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8310 } 8311 } 8312 lun->pr_generation++; 8313 mtx_unlock(&lun->lun_lock); 8314 8315 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8316 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8317 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8318 persis_io.pr.pr_info.residx = residx; 8319 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8320 sizeof(persis_io.pr), M_WAITOK); 8321 } else /* sa_res_key != 0 */ { 8322 8323 /* 8324 * If we aren't registered currently then increment 8325 * the key count and set the registered flag. 8326 */ 8327 ctl_alloc_prkey(lun, residx); 8328 if (ctl_get_prkey(lun, residx) == 0) 8329 lun->pr_key_count++; 8330 ctl_set_prkey(lun, residx, sa_res_key); 8331 lun->pr_generation++; 8332 mtx_unlock(&lun->lun_lock); 8333 8334 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8335 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8336 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8337 persis_io.pr.pr_info.residx = residx; 8338 memcpy(persis_io.pr.pr_info.sa_res_key, 8339 param->serv_act_res_key, 8340 sizeof(param->serv_act_res_key)); 8341 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8342 sizeof(persis_io.pr), M_WAITOK); 8343 } 8344 8345 break; 8346 } 8347 case SPRO_RESERVE: 8348#if 0 8349 printf("Reserve executed type %d\n", type); 8350#endif 8351 mtx_lock(&lun->lun_lock); 8352 if (lun->flags & CTL_LUN_PR_RESERVED) { 8353 /* 8354 * if this isn't the reservation holder and it's 8355 * not a "all registrants" type or if the type is 8356 * different then we have a conflict 8357 */ 8358 if ((lun->pr_res_idx != residx 8359 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8360 || lun->pr_res_type != type) { 8361 mtx_unlock(&lun->lun_lock); 8362 free(ctsio->kern_data_ptr, M_CTL); 8363 ctl_set_reservation_conflict(ctsio); 8364 ctl_done((union ctl_io *)ctsio); 8365 return (CTL_RETVAL_COMPLETE); 8366 } 8367 mtx_unlock(&lun->lun_lock); 8368 } else /* create a reservation */ { 8369 /* 8370 * If it's not an "all registrants" type record 8371 * reservation holder 8372 */ 8373 if (type != SPR_TYPE_WR_EX_AR 8374 && type != SPR_TYPE_EX_AC_AR) 8375 lun->pr_res_idx = residx; /* Res holder */ 8376 else 8377 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8378 8379 lun->flags |= CTL_LUN_PR_RESERVED; 8380 lun->pr_res_type = type; 8381 8382 mtx_unlock(&lun->lun_lock); 8383 8384 /* send msg to other side */ 8385 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8386 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8387 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8388 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8389 persis_io.pr.pr_info.res_type = type; 8390 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8391 sizeof(persis_io.pr), M_WAITOK); 8392 } 8393 break; 8394 8395 case SPRO_RELEASE: 8396 mtx_lock(&lun->lun_lock); 8397 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8398 /* No reservation exists return good status */ 8399 mtx_unlock(&lun->lun_lock); 8400 goto done; 8401 } 8402 /* 8403 * Is this nexus a reservation holder? 8404 */ 8405 if (lun->pr_res_idx != residx 8406 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8407 /* 8408 * not a res holder return good status but 8409 * do nothing 8410 */ 8411 mtx_unlock(&lun->lun_lock); 8412 goto done; 8413 } 8414 8415 if (lun->pr_res_type != type) { 8416 mtx_unlock(&lun->lun_lock); 8417 free(ctsio->kern_data_ptr, M_CTL); 8418 ctl_set_illegal_pr_release(ctsio); 8419 ctl_done((union ctl_io *)ctsio); 8420 return (CTL_RETVAL_COMPLETE); 8421 } 8422 8423 /* okay to release */ 8424 lun->flags &= ~CTL_LUN_PR_RESERVED; 8425 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8426 lun->pr_res_type = 0; 8427 8428 /* 8429 * If this isn't an exclusive access reservation and NUAR 8430 * is not set, generate UA for all other registrants. 8431 */ 8432 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8433 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8434 for (i = softc->init_min; i < softc->init_max; i++) { 8435 if (i == residx || ctl_get_prkey(lun, i) == 0) 8436 continue; 8437 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8438 } 8439 } 8440 mtx_unlock(&lun->lun_lock); 8441 8442 /* Send msg to other side */ 8443 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8444 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8445 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8446 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8447 sizeof(persis_io.pr), M_WAITOK); 8448 break; 8449 8450 case SPRO_CLEAR: 8451 /* send msg to other side */ 8452 8453 mtx_lock(&lun->lun_lock); 8454 lun->flags &= ~CTL_LUN_PR_RESERVED; 8455 lun->pr_res_type = 0; 8456 lun->pr_key_count = 0; 8457 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8458 8459 ctl_clr_prkey(lun, residx); 8460 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8461 if (ctl_get_prkey(lun, i) != 0) { 8462 ctl_clr_prkey(lun, i); 8463 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8464 } 8465 lun->pr_generation++; 8466 mtx_unlock(&lun->lun_lock); 8467 8468 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8469 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8470 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8471 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8472 sizeof(persis_io.pr), M_WAITOK); 8473 break; 8474 8475 case SPRO_PREEMPT: 8476 case SPRO_PRE_ABO: { 8477 int nretval; 8478 8479 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8480 residx, ctsio, cdb, param); 8481 if (nretval != 0) 8482 return (CTL_RETVAL_COMPLETE); 8483 break; 8484 } 8485 default: 8486 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8487 } 8488 8489done: 8490 free(ctsio->kern_data_ptr, M_CTL); 8491 ctl_set_success(ctsio); 8492 ctl_done((union ctl_io *)ctsio); 8493 8494 return (retval); 8495} 8496 8497/* 8498 * This routine is for handling a message from the other SC pertaining to 8499 * persistent reserve out. All the error checking will have been done 8500 * so only perorming the action need be done here to keep the two 8501 * in sync. 8502 */ 8503static void 8504ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8505{ 8506 struct ctl_softc *softc = CTL_SOFTC(io); 8507 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8508 struct ctl_lun *lun; 8509 int i; 8510 uint32_t residx, targ_lun; 8511 8512 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8513 mtx_lock(&softc->ctl_lock); 8514 if (targ_lun >= CTL_MAX_LUNS || 8515 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8516 mtx_unlock(&softc->ctl_lock); 8517 return; 8518 } 8519 mtx_lock(&lun->lun_lock); 8520 mtx_unlock(&softc->ctl_lock); 8521 if (lun->flags & CTL_LUN_DISABLED) { 8522 mtx_unlock(&lun->lun_lock); 8523 return; 8524 } 8525 residx = ctl_get_initindex(&msg->hdr.nexus); 8526 switch(msg->pr.pr_info.action) { 8527 case CTL_PR_REG_KEY: 8528 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8529 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8530 lun->pr_key_count++; 8531 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8532 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8533 lun->pr_generation++; 8534 break; 8535 8536 case CTL_PR_UNREG_KEY: 8537 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8538 lun->pr_key_count--; 8539 8540 /* XXX Need to see if the reservation has been released */ 8541 /* if so do we need to generate UA? */ 8542 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8543 lun->flags &= ~CTL_LUN_PR_RESERVED; 8544 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8545 8546 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8547 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8548 lun->pr_key_count) { 8549 /* 8550 * If the reservation is a registrants 8551 * only type we need to generate a UA 8552 * for other registered inits. The 8553 * sense code should be RESERVATIONS 8554 * RELEASED 8555 */ 8556 8557 for (i = softc->init_min; i < softc->init_max; i++) { 8558 if (ctl_get_prkey(lun, i) == 0) 8559 continue; 8560 8561 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8562 } 8563 } 8564 lun->pr_res_type = 0; 8565 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8566 if (lun->pr_key_count==0) { 8567 lun->flags &= ~CTL_LUN_PR_RESERVED; 8568 lun->pr_res_type = 0; 8569 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8570 } 8571 } 8572 lun->pr_generation++; 8573 break; 8574 8575 case CTL_PR_RESERVE: 8576 lun->flags |= CTL_LUN_PR_RESERVED; 8577 lun->pr_res_type = msg->pr.pr_info.res_type; 8578 lun->pr_res_idx = msg->pr.pr_info.residx; 8579 8580 break; 8581 8582 case CTL_PR_RELEASE: 8583 /* 8584 * If this isn't an exclusive access reservation and NUAR 8585 * is not set, generate UA for all other registrants. 8586 */ 8587 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8588 lun->pr_res_type != SPR_TYPE_WR_EX && 8589 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8590 for (i = softc->init_min; i < softc->init_max; i++) 8591 if (i == residx || ctl_get_prkey(lun, i) == 0) 8592 continue; 8593 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8594 } 8595 8596 lun->flags &= ~CTL_LUN_PR_RESERVED; 8597 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8598 lun->pr_res_type = 0; 8599 break; 8600 8601 case CTL_PR_PREEMPT: 8602 ctl_pro_preempt_other(lun, msg); 8603 break; 8604 case CTL_PR_CLEAR: 8605 lun->flags &= ~CTL_LUN_PR_RESERVED; 8606 lun->pr_res_type = 0; 8607 lun->pr_key_count = 0; 8608 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8609 8610 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8611 if (ctl_get_prkey(lun, i) == 0) 8612 continue; 8613 ctl_clr_prkey(lun, i); 8614 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8615 } 8616 lun->pr_generation++; 8617 break; 8618 } 8619 8620 mtx_unlock(&lun->lun_lock); 8621} 8622 8623int 8624ctl_read_write(struct ctl_scsiio *ctsio) 8625{ 8626 struct ctl_lun *lun = CTL_LUN(ctsio); 8627 struct ctl_lba_len_flags *lbalen; 8628 uint64_t lba; 8629 uint32_t num_blocks; 8630 int flags, retval; 8631 int isread; 8632 8633 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8634 8635 flags = 0; 8636 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8637 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8638 switch (ctsio->cdb[0]) { 8639 case READ_6: 8640 case WRITE_6: { 8641 struct scsi_rw_6 *cdb; 8642 8643 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8644 8645 lba = scsi_3btoul(cdb->addr); 8646 /* only 5 bits are valid in the most significant address byte */ 8647 lba &= 0x1fffff; 8648 num_blocks = cdb->length; 8649 /* 8650 * This is correct according to SBC-2. 8651 */ 8652 if (num_blocks == 0) 8653 num_blocks = 256; 8654 break; 8655 } 8656 case READ_10: 8657 case WRITE_10: { 8658 struct scsi_rw_10 *cdb; 8659 8660 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8661 if (cdb->byte2 & SRW10_FUA) 8662 flags |= CTL_LLF_FUA; 8663 if (cdb->byte2 & SRW10_DPO) 8664 flags |= CTL_LLF_DPO; 8665 lba = scsi_4btoul(cdb->addr); 8666 num_blocks = scsi_2btoul(cdb->length); 8667 break; 8668 } 8669 case WRITE_VERIFY_10: { 8670 struct scsi_write_verify_10 *cdb; 8671 8672 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8673 flags |= CTL_LLF_FUA; 8674 if (cdb->byte2 & SWV_DPO) 8675 flags |= CTL_LLF_DPO; 8676 lba = scsi_4btoul(cdb->addr); 8677 num_blocks = scsi_2btoul(cdb->length); 8678 break; 8679 } 8680 case READ_12: 8681 case WRITE_12: { 8682 struct scsi_rw_12 *cdb; 8683 8684 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8685 if (cdb->byte2 & SRW12_FUA) 8686 flags |= CTL_LLF_FUA; 8687 if (cdb->byte2 & SRW12_DPO) 8688 flags |= CTL_LLF_DPO; 8689 lba = scsi_4btoul(cdb->addr); 8690 num_blocks = scsi_4btoul(cdb->length); 8691 break; 8692 } 8693 case WRITE_VERIFY_12: { 8694 struct scsi_write_verify_12 *cdb; 8695 8696 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8697 flags |= CTL_LLF_FUA; 8698 if (cdb->byte2 & SWV_DPO) 8699 flags |= CTL_LLF_DPO; 8700 lba = scsi_4btoul(cdb->addr); 8701 num_blocks = scsi_4btoul(cdb->length); 8702 break; 8703 } 8704 case READ_16: 8705 case WRITE_16: { 8706 struct scsi_rw_16 *cdb; 8707 8708 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8709 if (cdb->byte2 & SRW12_FUA) 8710 flags |= CTL_LLF_FUA; 8711 if (cdb->byte2 & SRW12_DPO) 8712 flags |= CTL_LLF_DPO; 8713 lba = scsi_8btou64(cdb->addr); 8714 num_blocks = scsi_4btoul(cdb->length); 8715 break; 8716 } 8717 case WRITE_ATOMIC_16: { 8718 struct scsi_write_atomic_16 *cdb; 8719 8720 if (lun->be_lun->atomicblock == 0) { 8721 ctl_set_invalid_opcode(ctsio); 8722 ctl_done((union ctl_io *)ctsio); 8723 return (CTL_RETVAL_COMPLETE); 8724 } 8725 8726 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8727 if (cdb->byte2 & SRW12_FUA) 8728 flags |= CTL_LLF_FUA; 8729 if (cdb->byte2 & SRW12_DPO) 8730 flags |= CTL_LLF_DPO; 8731 lba = scsi_8btou64(cdb->addr); 8732 num_blocks = scsi_2btoul(cdb->length); 8733 if (num_blocks > lun->be_lun->atomicblock) { 8734 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8735 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8736 /*bit*/ 0); 8737 ctl_done((union ctl_io *)ctsio); 8738 return (CTL_RETVAL_COMPLETE); 8739 } 8740 break; 8741 } 8742 case WRITE_VERIFY_16: { 8743 struct scsi_write_verify_16 *cdb; 8744 8745 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8746 flags |= CTL_LLF_FUA; 8747 if (cdb->byte2 & SWV_DPO) 8748 flags |= CTL_LLF_DPO; 8749 lba = scsi_8btou64(cdb->addr); 8750 num_blocks = scsi_4btoul(cdb->length); 8751 break; 8752 } 8753 default: 8754 /* 8755 * We got a command we don't support. This shouldn't 8756 * happen, commands should be filtered out above us. 8757 */ 8758 ctl_set_invalid_opcode(ctsio); 8759 ctl_done((union ctl_io *)ctsio); 8760 8761 return (CTL_RETVAL_COMPLETE); 8762 break; /* NOTREACHED */ 8763 } 8764 8765 /* 8766 * The first check is to make sure we're in bounds, the second 8767 * check is to catch wrap-around problems. If the lba + num blocks 8768 * is less than the lba, then we've wrapped around and the block 8769 * range is invalid anyway. 8770 */ 8771 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8772 || ((lba + num_blocks) < lba)) { 8773 ctl_set_lba_out_of_range(ctsio, 8774 MAX(lba, lun->be_lun->maxlba + 1)); 8775 ctl_done((union ctl_io *)ctsio); 8776 return (CTL_RETVAL_COMPLETE); 8777 } 8778 8779 /* 8780 * According to SBC-3, a transfer length of 0 is not an error. 8781 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8782 * translates to 256 blocks for those commands. 8783 */ 8784 if (num_blocks == 0) { 8785 ctl_set_success(ctsio); 8786 ctl_done((union ctl_io *)ctsio); 8787 return (CTL_RETVAL_COMPLETE); 8788 } 8789 8790 /* Set FUA and/or DPO if caches are disabled. */ 8791 if (isread) { 8792 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8793 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8794 } else { 8795 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8796 flags |= CTL_LLF_FUA; 8797 } 8798 8799 lbalen = (struct ctl_lba_len_flags *) 8800 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8801 lbalen->lba = lba; 8802 lbalen->len = num_blocks; 8803 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8804 8805 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8806 ctsio->kern_rel_offset = 0; 8807 8808 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8809 8810 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8811 return (retval); 8812} 8813 8814static int 8815ctl_cnw_cont(union ctl_io *io) 8816{ 8817 struct ctl_lun *lun = CTL_LUN(io); 8818 struct ctl_scsiio *ctsio; 8819 struct ctl_lba_len_flags *lbalen; 8820 int retval; 8821 8822 ctsio = &io->scsiio; 8823 ctsio->io_hdr.status = CTL_STATUS_NONE; 8824 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8825 lbalen = (struct ctl_lba_len_flags *) 8826 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8827 lbalen->flags &= ~CTL_LLF_COMPARE; 8828 lbalen->flags |= CTL_LLF_WRITE; 8829 8830 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8831 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8832 return (retval); 8833} 8834 8835int 8836ctl_cnw(struct ctl_scsiio *ctsio) 8837{ 8838 struct ctl_lun *lun = CTL_LUN(ctsio); 8839 struct ctl_lba_len_flags *lbalen; 8840 uint64_t lba; 8841 uint32_t num_blocks; 8842 int flags, retval; 8843 8844 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8845 8846 flags = 0; 8847 switch (ctsio->cdb[0]) { 8848 case COMPARE_AND_WRITE: { 8849 struct scsi_compare_and_write *cdb; 8850 8851 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8852 if (cdb->byte2 & SRW10_FUA) 8853 flags |= CTL_LLF_FUA; 8854 if (cdb->byte2 & SRW10_DPO) 8855 flags |= CTL_LLF_DPO; 8856 lba = scsi_8btou64(cdb->addr); 8857 num_blocks = cdb->length; 8858 break; 8859 } 8860 default: 8861 /* 8862 * We got a command we don't support. This shouldn't 8863 * happen, commands should be filtered out above us. 8864 */ 8865 ctl_set_invalid_opcode(ctsio); 8866 ctl_done((union ctl_io *)ctsio); 8867 8868 return (CTL_RETVAL_COMPLETE); 8869 break; /* NOTREACHED */ 8870 } 8871 8872 /* 8873 * The first check is to make sure we're in bounds, the second 8874 * check is to catch wrap-around problems. If the lba + num blocks 8875 * is less than the lba, then we've wrapped around and the block 8876 * range is invalid anyway. 8877 */ 8878 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8879 || ((lba + num_blocks) < lba)) { 8880 ctl_set_lba_out_of_range(ctsio, 8881 MAX(lba, lun->be_lun->maxlba + 1)); 8882 ctl_done((union ctl_io *)ctsio); 8883 return (CTL_RETVAL_COMPLETE); 8884 } 8885 8886 /* 8887 * According to SBC-3, a transfer length of 0 is not an error. 8888 */ 8889 if (num_blocks == 0) { 8890 ctl_set_success(ctsio); 8891 ctl_done((union ctl_io *)ctsio); 8892 return (CTL_RETVAL_COMPLETE); 8893 } 8894 8895 /* Set FUA if write cache is disabled. */ 8896 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8897 flags |= CTL_LLF_FUA; 8898 8899 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8900 ctsio->kern_rel_offset = 0; 8901 8902 /* 8903 * Set the IO_CONT flag, so that if this I/O gets passed to 8904 * ctl_data_submit_done(), it'll get passed back to 8905 * ctl_ctl_cnw_cont() for further processing. 8906 */ 8907 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8908 ctsio->io_cont = ctl_cnw_cont; 8909 8910 lbalen = (struct ctl_lba_len_flags *) 8911 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8912 lbalen->lba = lba; 8913 lbalen->len = num_blocks; 8914 lbalen->flags = CTL_LLF_COMPARE | flags; 8915 8916 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8917 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8918 return (retval); 8919} 8920 8921int 8922ctl_verify(struct ctl_scsiio *ctsio) 8923{ 8924 struct ctl_lun *lun = CTL_LUN(ctsio); 8925 struct ctl_lba_len_flags *lbalen; 8926 uint64_t lba; 8927 uint32_t num_blocks; 8928 int bytchk, flags; 8929 int retval; 8930 8931 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8932 8933 bytchk = 0; 8934 flags = CTL_LLF_FUA; 8935 switch (ctsio->cdb[0]) { 8936 case VERIFY_10: { 8937 struct scsi_verify_10 *cdb; 8938 8939 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8940 if (cdb->byte2 & SVFY_BYTCHK) 8941 bytchk = 1; 8942 if (cdb->byte2 & SVFY_DPO) 8943 flags |= CTL_LLF_DPO; 8944 lba = scsi_4btoul(cdb->addr); 8945 num_blocks = scsi_2btoul(cdb->length); 8946 break; 8947 } 8948 case VERIFY_12: { 8949 struct scsi_verify_12 *cdb; 8950 8951 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8952 if (cdb->byte2 & SVFY_BYTCHK) 8953 bytchk = 1; 8954 if (cdb->byte2 & SVFY_DPO) 8955 flags |= CTL_LLF_DPO; 8956 lba = scsi_4btoul(cdb->addr); 8957 num_blocks = scsi_4btoul(cdb->length); 8958 break; 8959 } 8960 case VERIFY_16: { 8961 struct scsi_rw_16 *cdb; 8962 8963 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8964 if (cdb->byte2 & SVFY_BYTCHK) 8965 bytchk = 1; 8966 if (cdb->byte2 & SVFY_DPO) 8967 flags |= CTL_LLF_DPO; 8968 lba = scsi_8btou64(cdb->addr); 8969 num_blocks = scsi_4btoul(cdb->length); 8970 break; 8971 } 8972 default: 8973 /* 8974 * We got a command we don't support. This shouldn't 8975 * happen, commands should be filtered out above us. 8976 */ 8977 ctl_set_invalid_opcode(ctsio); 8978 ctl_done((union ctl_io *)ctsio); 8979 return (CTL_RETVAL_COMPLETE); 8980 } 8981 8982 /* 8983 * The first check is to make sure we're in bounds, the second 8984 * check is to catch wrap-around problems. If the lba + num blocks 8985 * is less than the lba, then we've wrapped around and the block 8986 * range is invalid anyway. 8987 */ 8988 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8989 || ((lba + num_blocks) < lba)) { 8990 ctl_set_lba_out_of_range(ctsio, 8991 MAX(lba, lun->be_lun->maxlba + 1)); 8992 ctl_done((union ctl_io *)ctsio); 8993 return (CTL_RETVAL_COMPLETE); 8994 } 8995 8996 /* 8997 * According to SBC-3, a transfer length of 0 is not an error. 8998 */ 8999 if (num_blocks == 0) { 9000 ctl_set_success(ctsio); 9001 ctl_done((union ctl_io *)ctsio); 9002 return (CTL_RETVAL_COMPLETE); 9003 } 9004 9005 lbalen = (struct ctl_lba_len_flags *) 9006 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9007 lbalen->lba = lba; 9008 lbalen->len = num_blocks; 9009 if (bytchk) { 9010 lbalen->flags = CTL_LLF_COMPARE | flags; 9011 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9012 } else { 9013 lbalen->flags = CTL_LLF_VERIFY | flags; 9014 ctsio->kern_total_len = 0; 9015 } 9016 ctsio->kern_rel_offset = 0; 9017 9018 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9019 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9020 return (retval); 9021} 9022 9023int 9024ctl_report_luns(struct ctl_scsiio *ctsio) 9025{ 9026 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9027 struct ctl_port *port = CTL_PORT(ctsio); 9028 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9029 struct scsi_report_luns *cdb; 9030 struct scsi_report_luns_data *lun_data; 9031 int num_filled, num_luns, num_port_luns, retval; 9032 uint32_t alloc_len, lun_datalen; 9033 uint32_t initidx, targ_lun_id, lun_id; 9034 9035 retval = CTL_RETVAL_COMPLETE; 9036 cdb = (struct scsi_report_luns *)ctsio->cdb; 9037 9038 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9039 9040 num_luns = 0; 9041 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS; 9042 mtx_lock(&softc->ctl_lock); 9043 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9044 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9045 num_luns++; 9046 } 9047 mtx_unlock(&softc->ctl_lock); 9048 9049 switch (cdb->select_report) { 9050 case RPL_REPORT_DEFAULT: 9051 case RPL_REPORT_ALL: 9052 case RPL_REPORT_NONSUBSID: 9053 break; 9054 case RPL_REPORT_WELLKNOWN: 9055 case RPL_REPORT_ADMIN: 9056 case RPL_REPORT_CONGLOM: 9057 num_luns = 0; 9058 break; 9059 default: 9060 ctl_set_invalid_field(ctsio, 9061 /*sks_valid*/ 1, 9062 /*command*/ 1, 9063 /*field*/ 2, 9064 /*bit_valid*/ 0, 9065 /*bit*/ 0); 9066 ctl_done((union ctl_io *)ctsio); 9067 return (retval); 9068 break; /* NOTREACHED */ 9069 } 9070 9071 alloc_len = scsi_4btoul(cdb->length); 9072 /* 9073 * The initiator has to allocate at least 16 bytes for this request, 9074 * so he can at least get the header and the first LUN. Otherwise 9075 * we reject the request (per SPC-3 rev 14, section 6.21). 9076 */ 9077 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9078 sizeof(struct scsi_report_luns_lundata))) { 9079 ctl_set_invalid_field(ctsio, 9080 /*sks_valid*/ 1, 9081 /*command*/ 1, 9082 /*field*/ 6, 9083 /*bit_valid*/ 0, 9084 /*bit*/ 0); 9085 ctl_done((union ctl_io *)ctsio); 9086 return (retval); 9087 } 9088 9089 lun_datalen = sizeof(*lun_data) + 9090 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9091 9092 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9093 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9094 ctsio->kern_sg_entries = 0; 9095 9096 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9097 9098 mtx_lock(&softc->ctl_lock); 9099 for (targ_lun_id = 0, num_filled = 0; 9100 targ_lun_id < num_port_luns && num_filled < num_luns; 9101 targ_lun_id++) { 9102 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9103 if (lun_id == UINT32_MAX) 9104 continue; 9105 lun = softc->ctl_luns[lun_id]; 9106 if (lun == NULL) 9107 continue; 9108 9109 be64enc(lun_data->luns[num_filled++].lundata, 9110 ctl_encode_lun(targ_lun_id)); 9111 9112 /* 9113 * According to SPC-3, rev 14 section 6.21: 9114 * 9115 * "The execution of a REPORT LUNS command to any valid and 9116 * installed logical unit shall clear the REPORTED LUNS DATA 9117 * HAS CHANGED unit attention condition for all logical 9118 * units of that target with respect to the requesting 9119 * initiator. A valid and installed logical unit is one 9120 * having a PERIPHERAL QUALIFIER of 000b in the standard 9121 * INQUIRY data (see 6.4.2)." 9122 * 9123 * If request_lun is NULL, the LUN this report luns command 9124 * was issued to is either disabled or doesn't exist. In that 9125 * case, we shouldn't clear any pending lun change unit 9126 * attention. 9127 */ 9128 if (request_lun != NULL) { 9129 mtx_lock(&lun->lun_lock); 9130 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9131 mtx_unlock(&lun->lun_lock); 9132 } 9133 } 9134 mtx_unlock(&softc->ctl_lock); 9135 9136 /* 9137 * It's quite possible that we've returned fewer LUNs than we allocated 9138 * space for. Trim it. 9139 */ 9140 lun_datalen = sizeof(*lun_data) + 9141 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9142 9143 if (lun_datalen < alloc_len) { 9144 ctsio->residual = alloc_len - lun_datalen; 9145 ctsio->kern_data_len = lun_datalen; 9146 ctsio->kern_total_len = lun_datalen; 9147 } else { 9148 ctsio->residual = 0; 9149 ctsio->kern_data_len = alloc_len; 9150 ctsio->kern_total_len = alloc_len; 9151 } 9152 ctsio->kern_data_resid = 0; 9153 ctsio->kern_rel_offset = 0; 9154 ctsio->kern_sg_entries = 0; 9155 9156 /* 9157 * We set this to the actual data length, regardless of how much 9158 * space we actually have to return results. If the user looks at 9159 * this value, he'll know whether or not he allocated enough space 9160 * and reissue the command if necessary. We don't support well 9161 * known logical units, so if the user asks for that, return none. 9162 */ 9163 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9164 9165 /* 9166 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9167 * this request. 9168 */ 9169 ctl_set_success(ctsio); 9170 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9171 ctsio->be_move_done = ctl_config_move_done; 9172 ctl_datamove((union ctl_io *)ctsio); 9173 return (retval); 9174} 9175 9176int 9177ctl_request_sense(struct ctl_scsiio *ctsio) 9178{ 9179 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9180 struct ctl_lun *lun = CTL_LUN(ctsio); 9181 struct scsi_request_sense *cdb; 9182 struct scsi_sense_data *sense_ptr; 9183 uint32_t initidx; 9184 int have_error; 9185 u_int sense_len = SSD_FULL_SIZE; 9186 scsi_sense_data_type sense_format; 9187 ctl_ua_type ua_type; 9188 uint8_t asc = 0, ascq = 0; 9189 9190 cdb = (struct scsi_request_sense *)ctsio->cdb; 9191 9192 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9193 9194 /* 9195 * Determine which sense format the user wants. 9196 */ 9197 if (cdb->byte2 & SRS_DESC) 9198 sense_format = SSD_TYPE_DESC; 9199 else 9200 sense_format = SSD_TYPE_FIXED; 9201 9202 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9203 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9204 ctsio->kern_sg_entries = 0; 9205 9206 /* 9207 * struct scsi_sense_data, which is currently set to 256 bytes, is 9208 * larger than the largest allowed value for the length field in the 9209 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9210 */ 9211 ctsio->residual = 0; 9212 ctsio->kern_data_len = cdb->length; 9213 ctsio->kern_total_len = cdb->length; 9214 9215 ctsio->kern_data_resid = 0; 9216 ctsio->kern_rel_offset = 0; 9217 ctsio->kern_sg_entries = 0; 9218 9219 /* 9220 * If we don't have a LUN, we don't have any pending sense. 9221 */ 9222 if (lun == NULL || 9223 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9224 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9225 /* "Logical unit not supported" */ 9226 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9227 /*current_error*/ 1, 9228 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9229 /*asc*/ 0x25, 9230 /*ascq*/ 0x00, 9231 SSD_ELEM_NONE); 9232 goto send; 9233 } 9234 9235 have_error = 0; 9236 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9237 /* 9238 * Check for pending sense, and then for pending unit attentions. 9239 * Pending sense gets returned first, then pending unit attentions. 9240 */ 9241 mtx_lock(&lun->lun_lock); 9242#ifdef CTL_WITH_CA 9243 if (ctl_is_set(lun->have_ca, initidx)) { 9244 scsi_sense_data_type stored_format; 9245 9246 /* 9247 * Check to see which sense format was used for the stored 9248 * sense data. 9249 */ 9250 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9251 9252 /* 9253 * If the user requested a different sense format than the 9254 * one we stored, then we need to convert it to the other 9255 * format. If we're going from descriptor to fixed format 9256 * sense data, we may lose things in translation, depending 9257 * on what options were used. 9258 * 9259 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9260 * for some reason we'll just copy it out as-is. 9261 */ 9262 if ((stored_format == SSD_TYPE_FIXED) 9263 && (sense_format == SSD_TYPE_DESC)) 9264 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9265 &lun->pending_sense[initidx], 9266 (struct scsi_sense_data_desc *)sense_ptr); 9267 else if ((stored_format == SSD_TYPE_DESC) 9268 && (sense_format == SSD_TYPE_FIXED)) 9269 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9270 &lun->pending_sense[initidx], 9271 (struct scsi_sense_data_fixed *)sense_ptr); 9272 else 9273 memcpy(sense_ptr, &lun->pending_sense[initidx], 9274 MIN(sizeof(*sense_ptr), 9275 sizeof(lun->pending_sense[initidx]))); 9276 9277 ctl_clear_mask(lun->have_ca, initidx); 9278 have_error = 1; 9279 } else 9280#endif 9281 if (have_error == 0) { 9282 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9283 sense_format); 9284 if (ua_type != CTL_UA_NONE) 9285 have_error = 1; 9286 } 9287 if (have_error == 0) { 9288 /* 9289 * Report informational exception if have one and allowed. 9290 */ 9291 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9292 asc = lun->ie_asc; 9293 ascq = lun->ie_ascq; 9294 } 9295 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9296 /*current_error*/ 1, 9297 /*sense_key*/ SSD_KEY_NO_SENSE, 9298 /*asc*/ asc, 9299 /*ascq*/ ascq, 9300 SSD_ELEM_NONE); 9301 } 9302 mtx_unlock(&lun->lun_lock); 9303 9304send: 9305 /* 9306 * We report the SCSI status as OK, since the status of the command 9307 * itself is OK. We're reporting sense as parameter data. 9308 */ 9309 ctl_set_success(ctsio); 9310 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9311 ctsio->be_move_done = ctl_config_move_done; 9312 ctl_datamove((union ctl_io *)ctsio); 9313 return (CTL_RETVAL_COMPLETE); 9314} 9315 9316int 9317ctl_tur(struct ctl_scsiio *ctsio) 9318{ 9319 9320 CTL_DEBUG_PRINT(("ctl_tur\n")); 9321 9322 ctl_set_success(ctsio); 9323 ctl_done((union ctl_io *)ctsio); 9324 9325 return (CTL_RETVAL_COMPLETE); 9326} 9327 9328/* 9329 * SCSI VPD page 0x00, the Supported VPD Pages page. 9330 */ 9331static int 9332ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9333{ 9334 struct ctl_lun *lun = CTL_LUN(ctsio); 9335 struct scsi_vpd_supported_pages *pages; 9336 int sup_page_size; 9337 int p; 9338 9339 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9340 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9341 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9342 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9343 ctsio->kern_sg_entries = 0; 9344 9345 if (sup_page_size < alloc_len) { 9346 ctsio->residual = alloc_len - sup_page_size; 9347 ctsio->kern_data_len = sup_page_size; 9348 ctsio->kern_total_len = sup_page_size; 9349 } else { 9350 ctsio->residual = 0; 9351 ctsio->kern_data_len = alloc_len; 9352 ctsio->kern_total_len = alloc_len; 9353 } 9354 ctsio->kern_data_resid = 0; 9355 ctsio->kern_rel_offset = 0; 9356 ctsio->kern_sg_entries = 0; 9357 9358 /* 9359 * The control device is always connected. The disk device, on the 9360 * other hand, may not be online all the time. Need to change this 9361 * to figure out whether the disk device is actually online or not. 9362 */ 9363 if (lun != NULL) 9364 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9365 lun->be_lun->lun_type; 9366 else 9367 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9368 9369 p = 0; 9370 /* Supported VPD pages */ 9371 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9372 /* Serial Number */ 9373 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9374 /* Device Identification */ 9375 pages->page_list[p++] = SVPD_DEVICE_ID; 9376 /* Extended INQUIRY Data */ 9377 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9378 /* Mode Page Policy */ 9379 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9380 /* SCSI Ports */ 9381 pages->page_list[p++] = SVPD_SCSI_PORTS; 9382 /* Third-party Copy */ 9383 pages->page_list[p++] = SVPD_SCSI_TPC; 9384 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9385 /* Block limits */ 9386 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9387 /* Block Device Characteristics */ 9388 pages->page_list[p++] = SVPD_BDC; 9389 /* Logical Block Provisioning */ 9390 pages->page_list[p++] = SVPD_LBP; 9391 } 9392 pages->length = p; 9393 9394 ctl_set_success(ctsio); 9395 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9396 ctsio->be_move_done = ctl_config_move_done; 9397 ctl_datamove((union ctl_io *)ctsio); 9398 return (CTL_RETVAL_COMPLETE); 9399} 9400 9401/* 9402 * SCSI VPD page 0x80, the Unit Serial Number page. 9403 */ 9404static int 9405ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9406{ 9407 struct ctl_lun *lun = CTL_LUN(ctsio); 9408 struct scsi_vpd_unit_serial_number *sn_ptr; 9409 int data_len; 9410 9411 data_len = 4 + CTL_SN_LEN; 9412 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9413 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9414 if (data_len < alloc_len) { 9415 ctsio->residual = alloc_len - data_len; 9416 ctsio->kern_data_len = data_len; 9417 ctsio->kern_total_len = data_len; 9418 } else { 9419 ctsio->residual = 0; 9420 ctsio->kern_data_len = alloc_len; 9421 ctsio->kern_total_len = alloc_len; 9422 } 9423 ctsio->kern_data_resid = 0; 9424 ctsio->kern_rel_offset = 0; 9425 ctsio->kern_sg_entries = 0; 9426 9427 /* 9428 * The control device is always connected. The disk device, on the 9429 * other hand, may not be online all the time. Need to change this 9430 * to figure out whether the disk device is actually online or not. 9431 */ 9432 if (lun != NULL) 9433 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9434 lun->be_lun->lun_type; 9435 else 9436 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9437 9438 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9439 sn_ptr->length = CTL_SN_LEN; 9440 /* 9441 * If we don't have a LUN, we just leave the serial number as 9442 * all spaces. 9443 */ 9444 if (lun != NULL) { 9445 strncpy((char *)sn_ptr->serial_num, 9446 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9447 } else 9448 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9449 9450 ctl_set_success(ctsio); 9451 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9452 ctsio->be_move_done = ctl_config_move_done; 9453 ctl_datamove((union ctl_io *)ctsio); 9454 return (CTL_RETVAL_COMPLETE); 9455} 9456 9457 9458/* 9459 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9460 */ 9461static int 9462ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9463{ 9464 struct ctl_lun *lun = CTL_LUN(ctsio); 9465 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9466 int data_len; 9467 9468 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9469 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9470 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9471 ctsio->kern_sg_entries = 0; 9472 9473 if (data_len < alloc_len) { 9474 ctsio->residual = alloc_len - data_len; 9475 ctsio->kern_data_len = data_len; 9476 ctsio->kern_total_len = data_len; 9477 } else { 9478 ctsio->residual = 0; 9479 ctsio->kern_data_len = alloc_len; 9480 ctsio->kern_total_len = alloc_len; 9481 } 9482 ctsio->kern_data_resid = 0; 9483 ctsio->kern_rel_offset = 0; 9484 ctsio->kern_sg_entries = 0; 9485 9486 /* 9487 * The control device is always connected. The disk device, on the 9488 * other hand, may not be online all the time. 9489 */ 9490 if (lun != NULL) 9491 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9492 lun->be_lun->lun_type; 9493 else 9494 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9495 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9496 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9497 /* 9498 * We support head of queue, ordered and simple tags. 9499 */ 9500 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9501 /* 9502 * Volatile cache supported. 9503 */ 9504 eid_ptr->flags3 = SVPD_EID_V_SUP; 9505 9506 /* 9507 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9508 * attention for a particular IT nexus on all LUNs once we report 9509 * it to that nexus once. This bit is required as of SPC-4. 9510 */ 9511 eid_ptr->flags4 = SVPD_EID_LUICLR; 9512 9513 /* 9514 * We support revert to defaults (RTD) bit in MODE SELECT. 9515 */ 9516 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9517 9518 /* 9519 * XXX KDM in order to correctly answer this, we would need 9520 * information from the SIM to determine how much sense data it 9521 * can send. So this would really be a path inquiry field, most 9522 * likely. This can be set to a maximum of 252 according to SPC-4, 9523 * but the hardware may or may not be able to support that much. 9524 * 0 just means that the maximum sense data length is not reported. 9525 */ 9526 eid_ptr->max_sense_length = 0; 9527 9528 ctl_set_success(ctsio); 9529 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9530 ctsio->be_move_done = ctl_config_move_done; 9531 ctl_datamove((union ctl_io *)ctsio); 9532 return (CTL_RETVAL_COMPLETE); 9533} 9534 9535static int 9536ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9537{ 9538 struct ctl_lun *lun = CTL_LUN(ctsio); 9539 struct scsi_vpd_mode_page_policy *mpp_ptr; 9540 int data_len; 9541 9542 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9543 sizeof(struct scsi_vpd_mode_page_policy_descr); 9544 9545 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9546 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9547 ctsio->kern_sg_entries = 0; 9548 9549 if (data_len < alloc_len) { 9550 ctsio->residual = alloc_len - data_len; 9551 ctsio->kern_data_len = data_len; 9552 ctsio->kern_total_len = data_len; 9553 } else { 9554 ctsio->residual = 0; 9555 ctsio->kern_data_len = alloc_len; 9556 ctsio->kern_total_len = alloc_len; 9557 } 9558 ctsio->kern_data_resid = 0; 9559 ctsio->kern_rel_offset = 0; 9560 ctsio->kern_sg_entries = 0; 9561 9562 /* 9563 * The control device is always connected. The disk device, on the 9564 * other hand, may not be online all the time. 9565 */ 9566 if (lun != NULL) 9567 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9568 lun->be_lun->lun_type; 9569 else 9570 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9571 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9572 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9573 mpp_ptr->descr[0].page_code = 0x3f; 9574 mpp_ptr->descr[0].subpage_code = 0xff; 9575 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9576 9577 ctl_set_success(ctsio); 9578 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9579 ctsio->be_move_done = ctl_config_move_done; 9580 ctl_datamove((union ctl_io *)ctsio); 9581 return (CTL_RETVAL_COMPLETE); 9582} 9583 9584/* 9585 * SCSI VPD page 0x83, the Device Identification page. 9586 */ 9587static int 9588ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9589{ 9590 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9591 struct ctl_port *port = CTL_PORT(ctsio); 9592 struct ctl_lun *lun = CTL_LUN(ctsio); 9593 struct scsi_vpd_device_id *devid_ptr; 9594 struct scsi_vpd_id_descriptor *desc; 9595 int data_len, g; 9596 uint8_t proto; 9597 9598 data_len = sizeof(struct scsi_vpd_device_id) + 9599 sizeof(struct scsi_vpd_id_descriptor) + 9600 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9601 sizeof(struct scsi_vpd_id_descriptor) + 9602 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9603 if (lun && lun->lun_devid) 9604 data_len += lun->lun_devid->len; 9605 if (port && port->port_devid) 9606 data_len += port->port_devid->len; 9607 if (port && port->target_devid) 9608 data_len += port->target_devid->len; 9609 9610 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9611 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9612 ctsio->kern_sg_entries = 0; 9613 9614 if (data_len < alloc_len) { 9615 ctsio->residual = alloc_len - data_len; 9616 ctsio->kern_data_len = data_len; 9617 ctsio->kern_total_len = data_len; 9618 } else { 9619 ctsio->residual = 0; 9620 ctsio->kern_data_len = alloc_len; 9621 ctsio->kern_total_len = alloc_len; 9622 } 9623 ctsio->kern_data_resid = 0; 9624 ctsio->kern_rel_offset = 0; 9625 ctsio->kern_sg_entries = 0; 9626 9627 /* 9628 * The control device is always connected. The disk device, on the 9629 * other hand, may not be online all the time. 9630 */ 9631 if (lun != NULL) 9632 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9633 lun->be_lun->lun_type; 9634 else 9635 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9636 devid_ptr->page_code = SVPD_DEVICE_ID; 9637 scsi_ulto2b(data_len - 4, devid_ptr->length); 9638 9639 if (port && port->port_type == CTL_PORT_FC) 9640 proto = SCSI_PROTO_FC << 4; 9641 else if (port && port->port_type == CTL_PORT_ISCSI) 9642 proto = SCSI_PROTO_ISCSI << 4; 9643 else 9644 proto = SCSI_PROTO_SPI << 4; 9645 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9646 9647 /* 9648 * We're using a LUN association here. i.e., this device ID is a 9649 * per-LUN identifier. 9650 */ 9651 if (lun && lun->lun_devid) { 9652 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9653 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9654 lun->lun_devid->len); 9655 } 9656 9657 /* 9658 * This is for the WWPN which is a port association. 9659 */ 9660 if (port && port->port_devid) { 9661 memcpy(desc, port->port_devid->data, port->port_devid->len); 9662 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9663 port->port_devid->len); 9664 } 9665 9666 /* 9667 * This is for the Relative Target Port(type 4h) identifier 9668 */ 9669 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9670 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9671 SVPD_ID_TYPE_RELTARG; 9672 desc->length = 4; 9673 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9674 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9675 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9676 9677 /* 9678 * This is for the Target Port Group(type 5h) identifier 9679 */ 9680 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9681 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9682 SVPD_ID_TYPE_TPORTGRP; 9683 desc->length = 4; 9684 if (softc->is_single || 9685 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9686 g = 1; 9687 else 9688 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9689 scsi_ulto2b(g, &desc->identifier[2]); 9690 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9691 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9692 9693 /* 9694 * This is for the Target identifier 9695 */ 9696 if (port && port->target_devid) { 9697 memcpy(desc, port->target_devid->data, port->target_devid->len); 9698 } 9699 9700 ctl_set_success(ctsio); 9701 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9702 ctsio->be_move_done = ctl_config_move_done; 9703 ctl_datamove((union ctl_io *)ctsio); 9704 return (CTL_RETVAL_COMPLETE); 9705} 9706 9707static int 9708ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9709{ 9710 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9711 struct ctl_lun *lun = CTL_LUN(ctsio); 9712 struct scsi_vpd_scsi_ports *sp; 9713 struct scsi_vpd_port_designation *pd; 9714 struct scsi_vpd_port_designation_cont *pdc; 9715 struct ctl_port *port; 9716 int data_len, num_target_ports, iid_len, id_len; 9717 9718 num_target_ports = 0; 9719 iid_len = 0; 9720 id_len = 0; 9721 mtx_lock(&softc->ctl_lock); 9722 STAILQ_FOREACH(port, &softc->port_list, links) { 9723 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9724 continue; 9725 if (lun != NULL && 9726 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9727 continue; 9728 num_target_ports++; 9729 if (port->init_devid) 9730 iid_len += port->init_devid->len; 9731 if (port->port_devid) 9732 id_len += port->port_devid->len; 9733 } 9734 mtx_unlock(&softc->ctl_lock); 9735 9736 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9737 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9738 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9739 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9740 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9741 ctsio->kern_sg_entries = 0; 9742 9743 if (data_len < alloc_len) { 9744 ctsio->residual = alloc_len - data_len; 9745 ctsio->kern_data_len = data_len; 9746 ctsio->kern_total_len = data_len; 9747 } else { 9748 ctsio->residual = 0; 9749 ctsio->kern_data_len = alloc_len; 9750 ctsio->kern_total_len = alloc_len; 9751 } 9752 ctsio->kern_data_resid = 0; 9753 ctsio->kern_rel_offset = 0; 9754 ctsio->kern_sg_entries = 0; 9755 9756 /* 9757 * The control device is always connected. The disk device, on the 9758 * other hand, may not be online all the time. Need to change this 9759 * to figure out whether the disk device is actually online or not. 9760 */ 9761 if (lun != NULL) 9762 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9763 lun->be_lun->lun_type; 9764 else 9765 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9766 9767 sp->page_code = SVPD_SCSI_PORTS; 9768 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9769 sp->page_length); 9770 pd = &sp->design[0]; 9771 9772 mtx_lock(&softc->ctl_lock); 9773 STAILQ_FOREACH(port, &softc->port_list, links) { 9774 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9775 continue; 9776 if (lun != NULL && 9777 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9778 continue; 9779 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9780 if (port->init_devid) { 9781 iid_len = port->init_devid->len; 9782 memcpy(pd->initiator_transportid, 9783 port->init_devid->data, port->init_devid->len); 9784 } else 9785 iid_len = 0; 9786 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9787 pdc = (struct scsi_vpd_port_designation_cont *) 9788 (&pd->initiator_transportid[iid_len]); 9789 if (port->port_devid) { 9790 id_len = port->port_devid->len; 9791 memcpy(pdc->target_port_descriptors, 9792 port->port_devid->data, port->port_devid->len); 9793 } else 9794 id_len = 0; 9795 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9796 pd = (struct scsi_vpd_port_designation *) 9797 ((uint8_t *)pdc->target_port_descriptors + id_len); 9798 } 9799 mtx_unlock(&softc->ctl_lock); 9800 9801 ctl_set_success(ctsio); 9802 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9803 ctsio->be_move_done = ctl_config_move_done; 9804 ctl_datamove((union ctl_io *)ctsio); 9805 return (CTL_RETVAL_COMPLETE); 9806} 9807 9808static int 9809ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9810{ 9811 struct ctl_lun *lun = CTL_LUN(ctsio); 9812 struct scsi_vpd_block_limits *bl_ptr; 9813 uint64_t ival; 9814 9815 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9816 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9817 ctsio->kern_sg_entries = 0; 9818 9819 if (sizeof(*bl_ptr) < alloc_len) { 9820 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9821 ctsio->kern_data_len = sizeof(*bl_ptr); 9822 ctsio->kern_total_len = sizeof(*bl_ptr); 9823 } else { 9824 ctsio->residual = 0; 9825 ctsio->kern_data_len = alloc_len; 9826 ctsio->kern_total_len = alloc_len; 9827 } 9828 ctsio->kern_data_resid = 0; 9829 ctsio->kern_rel_offset = 0; 9830 ctsio->kern_sg_entries = 0; 9831 9832 /* 9833 * The control device is always connected. The disk device, on the 9834 * other hand, may not be online all the time. Need to change this 9835 * to figure out whether the disk device is actually online or not. 9836 */ 9837 if (lun != NULL) 9838 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9839 lun->be_lun->lun_type; 9840 else 9841 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9842 9843 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9844 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9845 bl_ptr->max_cmp_write_len = 0xff; 9846 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9847 if (lun != NULL) { 9848 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9849 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9850 ival = 0xffffffff; 9851 ctl_get_opt_number(&lun->be_lun->options, 9852 "unmap_max_lba", &ival); 9853 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9854 ival = 0xffffffff; 9855 ctl_get_opt_number(&lun->be_lun->options, 9856 "unmap_max_descr", &ival); 9857 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9858 if (lun->be_lun->ublockexp != 0) { 9859 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9860 bl_ptr->opt_unmap_grain); 9861 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9862 bl_ptr->unmap_grain_align); 9863 } 9864 } 9865 scsi_ulto4b(lun->be_lun->atomicblock, 9866 bl_ptr->max_atomic_transfer_length); 9867 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9868 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9869 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9870 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9871 ival = UINT64_MAX; 9872 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival); 9873 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9874 } 9875 9876 ctl_set_success(ctsio); 9877 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9878 ctsio->be_move_done = ctl_config_move_done; 9879 ctl_datamove((union ctl_io *)ctsio); 9880 return (CTL_RETVAL_COMPLETE); 9881} 9882 9883static int 9884ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9885{ 9886 struct ctl_lun *lun = CTL_LUN(ctsio); 9887 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9888 const char *value; 9889 u_int i; 9890 9891 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9892 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9893 ctsio->kern_sg_entries = 0; 9894 9895 if (sizeof(*bdc_ptr) < alloc_len) { 9896 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9897 ctsio->kern_data_len = sizeof(*bdc_ptr); 9898 ctsio->kern_total_len = sizeof(*bdc_ptr); 9899 } else { 9900 ctsio->residual = 0; 9901 ctsio->kern_data_len = alloc_len; 9902 ctsio->kern_total_len = alloc_len; 9903 } 9904 ctsio->kern_data_resid = 0; 9905 ctsio->kern_rel_offset = 0; 9906 ctsio->kern_sg_entries = 0; 9907 9908 /* 9909 * The control device is always connected. The disk device, on the 9910 * other hand, may not be online all the time. Need to change this 9911 * to figure out whether the disk device is actually online or not. 9912 */ 9913 if (lun != NULL) 9914 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9915 lun->be_lun->lun_type; 9916 else 9917 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9918 bdc_ptr->page_code = SVPD_BDC; 9919 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9920 if (lun != NULL && 9921 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9922 i = strtol(value, NULL, 0); 9923 else 9924 i = CTL_DEFAULT_ROTATION_RATE; 9925 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9926 if (lun != NULL && 9927 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9928 i = strtol(value, NULL, 0); 9929 else 9930 i = 0; 9931 bdc_ptr->wab_wac_ff = (i & 0x0f); 9932 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9933 9934 ctl_set_success(ctsio); 9935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9936 ctsio->be_move_done = ctl_config_move_done; 9937 ctl_datamove((union ctl_io *)ctsio); 9938 return (CTL_RETVAL_COMPLETE); 9939} 9940 9941static int 9942ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9943{ 9944 struct ctl_lun *lun = CTL_LUN(ctsio); 9945 struct scsi_vpd_logical_block_prov *lbp_ptr; 9946 const char *value; 9947 9948 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9949 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9950 ctsio->kern_sg_entries = 0; 9951 9952 if (sizeof(*lbp_ptr) < alloc_len) { 9953 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9954 ctsio->kern_data_len = sizeof(*lbp_ptr); 9955 ctsio->kern_total_len = sizeof(*lbp_ptr); 9956 } else { 9957 ctsio->residual = 0; 9958 ctsio->kern_data_len = alloc_len; 9959 ctsio->kern_total_len = alloc_len; 9960 } 9961 ctsio->kern_data_resid = 0; 9962 ctsio->kern_rel_offset = 0; 9963 ctsio->kern_sg_entries = 0; 9964 9965 /* 9966 * The control device is always connected. The disk device, on the 9967 * other hand, may not be online all the time. Need to change this 9968 * to figure out whether the disk device is actually online or not. 9969 */ 9970 if (lun != NULL) 9971 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9972 lun->be_lun->lun_type; 9973 else 9974 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9975 9976 lbp_ptr->page_code = SVPD_LBP; 9977 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9978 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9979 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9980 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9981 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9982 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type"); 9983 if (value != NULL) { 9984 if (strcmp(value, "resource") == 0) 9985 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 9986 else if (strcmp(value, "thin") == 0) 9987 lbp_ptr->prov_type = SVPD_LBP_THIN; 9988 } else 9989 lbp_ptr->prov_type = SVPD_LBP_THIN; 9990 } 9991 9992 ctl_set_success(ctsio); 9993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9994 ctsio->be_move_done = ctl_config_move_done; 9995 ctl_datamove((union ctl_io *)ctsio); 9996 return (CTL_RETVAL_COMPLETE); 9997} 9998 9999/* 10000 * INQUIRY with the EVPD bit set. 10001 */ 10002static int 10003ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10004{ 10005 struct ctl_lun *lun = CTL_LUN(ctsio); 10006 struct scsi_inquiry *cdb; 10007 int alloc_len, retval; 10008 10009 cdb = (struct scsi_inquiry *)ctsio->cdb; 10010 alloc_len = scsi_2btoul(cdb->length); 10011 10012 switch (cdb->page_code) { 10013 case SVPD_SUPPORTED_PAGES: 10014 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10015 break; 10016 case SVPD_UNIT_SERIAL_NUMBER: 10017 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10018 break; 10019 case SVPD_DEVICE_ID: 10020 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10021 break; 10022 case SVPD_EXTENDED_INQUIRY_DATA: 10023 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10024 break; 10025 case SVPD_MODE_PAGE_POLICY: 10026 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10027 break; 10028 case SVPD_SCSI_PORTS: 10029 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10030 break; 10031 case SVPD_SCSI_TPC: 10032 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10033 break; 10034 case SVPD_BLOCK_LIMITS: 10035 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10036 goto err; 10037 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10038 break; 10039 case SVPD_BDC: 10040 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10041 goto err; 10042 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10043 break; 10044 case SVPD_LBP: 10045 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10046 goto err; 10047 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10048 break; 10049 default: 10050err: 10051 ctl_set_invalid_field(ctsio, 10052 /*sks_valid*/ 1, 10053 /*command*/ 1, 10054 /*field*/ 2, 10055 /*bit_valid*/ 0, 10056 /*bit*/ 0); 10057 ctl_done((union ctl_io *)ctsio); 10058 retval = CTL_RETVAL_COMPLETE; 10059 break; 10060 } 10061 10062 return (retval); 10063} 10064 10065/* 10066 * Standard INQUIRY data. 10067 */ 10068static int 10069ctl_inquiry_std(struct ctl_scsiio *ctsio) 10070{ 10071 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10072 struct ctl_port *port = CTL_PORT(ctsio); 10073 struct ctl_lun *lun = CTL_LUN(ctsio); 10074 struct scsi_inquiry_data *inq_ptr; 10075 struct scsi_inquiry *cdb; 10076 char *val; 10077 uint32_t alloc_len, data_len; 10078 ctl_port_type port_type; 10079 10080 port_type = port->port_type; 10081 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10082 port_type = CTL_PORT_SCSI; 10083 10084 cdb = (struct scsi_inquiry *)ctsio->cdb; 10085 alloc_len = scsi_2btoul(cdb->length); 10086 10087 /* 10088 * We malloc the full inquiry data size here and fill it 10089 * in. If the user only asks for less, we'll give him 10090 * that much. 10091 */ 10092 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10093 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10094 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10095 ctsio->kern_sg_entries = 0; 10096 ctsio->kern_data_resid = 0; 10097 ctsio->kern_rel_offset = 0; 10098 10099 if (data_len < alloc_len) { 10100 ctsio->residual = alloc_len - data_len; 10101 ctsio->kern_data_len = data_len; 10102 ctsio->kern_total_len = data_len; 10103 } else { 10104 ctsio->residual = 0; 10105 ctsio->kern_data_len = alloc_len; 10106 ctsio->kern_total_len = alloc_len; 10107 } 10108 10109 if (lun != NULL) { 10110 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10111 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10112 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10113 lun->be_lun->lun_type; 10114 } else { 10115 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10116 lun->be_lun->lun_type; 10117 } 10118 if (lun->flags & CTL_LUN_REMOVABLE) 10119 inq_ptr->dev_qual2 |= SID_RMB; 10120 } else 10121 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10122 10123 /* RMB in byte 2 is 0 */ 10124 inq_ptr->version = SCSI_REV_SPC5; 10125 10126 /* 10127 * According to SAM-3, even if a device only supports a single 10128 * level of LUN addressing, it should still set the HISUP bit: 10129 * 10130 * 4.9.1 Logical unit numbers overview 10131 * 10132 * All logical unit number formats described in this standard are 10133 * hierarchical in structure even when only a single level in that 10134 * hierarchy is used. The HISUP bit shall be set to one in the 10135 * standard INQUIRY data (see SPC-2) when any logical unit number 10136 * format described in this standard is used. Non-hierarchical 10137 * formats are outside the scope of this standard. 10138 * 10139 * Therefore we set the HiSup bit here. 10140 * 10141 * The response format is 2, per SPC-3. 10142 */ 10143 inq_ptr->response_format = SID_HiSup | 2; 10144 10145 inq_ptr->additional_length = data_len - 10146 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10147 CTL_DEBUG_PRINT(("additional_length = %d\n", 10148 inq_ptr->additional_length)); 10149 10150 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10151 if (port_type == CTL_PORT_SCSI) 10152 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10153 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10154 inq_ptr->flags = SID_CmdQue; 10155 if (port_type == CTL_PORT_SCSI) 10156 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10157 10158 /* 10159 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10160 * We have 8 bytes for the vendor name, and 16 bytes for the device 10161 * name and 4 bytes for the revision. 10162 */ 10163 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10164 "vendor")) == NULL) { 10165 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10166 } else { 10167 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10168 strncpy(inq_ptr->vendor, val, 10169 min(sizeof(inq_ptr->vendor), strlen(val))); 10170 } 10171 if (lun == NULL) { 10172 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10173 sizeof(inq_ptr->product)); 10174 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10175 switch (lun->be_lun->lun_type) { 10176 case T_DIRECT: 10177 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10178 sizeof(inq_ptr->product)); 10179 break; 10180 case T_PROCESSOR: 10181 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10182 sizeof(inq_ptr->product)); 10183 break; 10184 case T_CDROM: 10185 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10186 sizeof(inq_ptr->product)); 10187 break; 10188 default: 10189 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10190 sizeof(inq_ptr->product)); 10191 break; 10192 } 10193 } else { 10194 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10195 strncpy(inq_ptr->product, val, 10196 min(sizeof(inq_ptr->product), strlen(val))); 10197 } 10198 10199 /* 10200 * XXX make this a macro somewhere so it automatically gets 10201 * incremented when we make changes. 10202 */ 10203 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10204 "revision")) == NULL) { 10205 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10206 } else { 10207 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10208 strncpy(inq_ptr->revision, val, 10209 min(sizeof(inq_ptr->revision), strlen(val))); 10210 } 10211 10212 /* 10213 * For parallel SCSI, we support double transition and single 10214 * transition clocking. We also support QAS (Quick Arbitration 10215 * and Selection) and Information Unit transfers on both the 10216 * control and array devices. 10217 */ 10218 if (port_type == CTL_PORT_SCSI) 10219 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10220 SID_SPI_IUS; 10221 10222 /* SAM-6 (no version claimed) */ 10223 scsi_ulto2b(0x00C0, inq_ptr->version1); 10224 /* SPC-5 (no version claimed) */ 10225 scsi_ulto2b(0x05C0, inq_ptr->version2); 10226 if (port_type == CTL_PORT_FC) { 10227 /* FCP-2 ANSI INCITS.350:2003 */ 10228 scsi_ulto2b(0x0917, inq_ptr->version3); 10229 } else if (port_type == CTL_PORT_SCSI) { 10230 /* SPI-4 ANSI INCITS.362:200x */ 10231 scsi_ulto2b(0x0B56, inq_ptr->version3); 10232 } else if (port_type == CTL_PORT_ISCSI) { 10233 /* iSCSI (no version claimed) */ 10234 scsi_ulto2b(0x0960, inq_ptr->version3); 10235 } else if (port_type == CTL_PORT_SAS) { 10236 /* SAS (no version claimed) */ 10237 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10238 } 10239 10240 if (lun == NULL) { 10241 /* SBC-4 (no version claimed) */ 10242 scsi_ulto2b(0x0600, inq_ptr->version4); 10243 } else { 10244 switch (lun->be_lun->lun_type) { 10245 case T_DIRECT: 10246 /* SBC-4 (no version claimed) */ 10247 scsi_ulto2b(0x0600, inq_ptr->version4); 10248 break; 10249 case T_PROCESSOR: 10250 break; 10251 case T_CDROM: 10252 /* MMC-6 (no version claimed) */ 10253 scsi_ulto2b(0x04E0, inq_ptr->version4); 10254 break; 10255 default: 10256 break; 10257 } 10258 } 10259 10260 ctl_set_success(ctsio); 10261 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10262 ctsio->be_move_done = ctl_config_move_done; 10263 ctl_datamove((union ctl_io *)ctsio); 10264 return (CTL_RETVAL_COMPLETE); 10265} 10266 10267int 10268ctl_inquiry(struct ctl_scsiio *ctsio) 10269{ 10270 struct scsi_inquiry *cdb; 10271 int retval; 10272 10273 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10274 10275 cdb = (struct scsi_inquiry *)ctsio->cdb; 10276 if (cdb->byte2 & SI_EVPD) 10277 retval = ctl_inquiry_evpd(ctsio); 10278 else if (cdb->page_code == 0) 10279 retval = ctl_inquiry_std(ctsio); 10280 else { 10281 ctl_set_invalid_field(ctsio, 10282 /*sks_valid*/ 1, 10283 /*command*/ 1, 10284 /*field*/ 2, 10285 /*bit_valid*/ 0, 10286 /*bit*/ 0); 10287 ctl_done((union ctl_io *)ctsio); 10288 return (CTL_RETVAL_COMPLETE); 10289 } 10290 10291 return (retval); 10292} 10293 10294int 10295ctl_get_config(struct ctl_scsiio *ctsio) 10296{ 10297 struct ctl_lun *lun = CTL_LUN(ctsio); 10298 struct scsi_get_config_header *hdr; 10299 struct scsi_get_config_feature *feature; 10300 struct scsi_get_config *cdb; 10301 uint32_t alloc_len, data_len; 10302 int rt, starting; 10303 10304 cdb = (struct scsi_get_config *)ctsio->cdb; 10305 rt = (cdb->rt & SGC_RT_MASK); 10306 starting = scsi_2btoul(cdb->starting_feature); 10307 alloc_len = scsi_2btoul(cdb->length); 10308 10309 data_len = sizeof(struct scsi_get_config_header) + 10310 sizeof(struct scsi_get_config_feature) + 8 + 10311 sizeof(struct scsi_get_config_feature) + 8 + 10312 sizeof(struct scsi_get_config_feature) + 4 + 10313 sizeof(struct scsi_get_config_feature) + 4 + 10314 sizeof(struct scsi_get_config_feature) + 8 + 10315 sizeof(struct scsi_get_config_feature) + 10316 sizeof(struct scsi_get_config_feature) + 4 + 10317 sizeof(struct scsi_get_config_feature) + 4 + 10318 sizeof(struct scsi_get_config_feature) + 4 + 10319 sizeof(struct scsi_get_config_feature) + 4 + 10320 sizeof(struct scsi_get_config_feature) + 4 + 10321 sizeof(struct scsi_get_config_feature) + 4; 10322 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10323 ctsio->kern_sg_entries = 0; 10324 ctsio->kern_data_resid = 0; 10325 ctsio->kern_rel_offset = 0; 10326 10327 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10328 if (lun->flags & CTL_LUN_NO_MEDIA) 10329 scsi_ulto2b(0x0000, hdr->current_profile); 10330 else 10331 scsi_ulto2b(0x0010, hdr->current_profile); 10332 feature = (struct scsi_get_config_feature *)(hdr + 1); 10333 10334 if (starting > 0x003b) 10335 goto done; 10336 if (starting > 0x003a) 10337 goto f3b; 10338 if (starting > 0x002b) 10339 goto f3a; 10340 if (starting > 0x002a) 10341 goto f2b; 10342 if (starting > 0x001f) 10343 goto f2a; 10344 if (starting > 0x001e) 10345 goto f1f; 10346 if (starting > 0x001d) 10347 goto f1e; 10348 if (starting > 0x0010) 10349 goto f1d; 10350 if (starting > 0x0003) 10351 goto f10; 10352 if (starting > 0x0002) 10353 goto f3; 10354 if (starting > 0x0001) 10355 goto f2; 10356 if (starting > 0x0000) 10357 goto f1; 10358 10359 /* Profile List */ 10360 scsi_ulto2b(0x0000, feature->feature_code); 10361 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10362 feature->add_length = 8; 10363 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10364 feature->feature_data[2] = 0x00; 10365 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10366 feature->feature_data[6] = 0x01; 10367 feature = (struct scsi_get_config_feature *) 10368 &feature->feature_data[feature->add_length]; 10369 10370f1: /* Core */ 10371 scsi_ulto2b(0x0001, feature->feature_code); 10372 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10373 feature->add_length = 8; 10374 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10375 feature->feature_data[4] = 0x03; 10376 feature = (struct scsi_get_config_feature *) 10377 &feature->feature_data[feature->add_length]; 10378 10379f2: /* Morphing */ 10380 scsi_ulto2b(0x0002, feature->feature_code); 10381 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10382 feature->add_length = 4; 10383 feature->feature_data[0] = 0x02; 10384 feature = (struct scsi_get_config_feature *) 10385 &feature->feature_data[feature->add_length]; 10386 10387f3: /* Removable Medium */ 10388 scsi_ulto2b(0x0003, feature->feature_code); 10389 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10390 feature->add_length = 4; 10391 feature->feature_data[0] = 0x39; 10392 feature = (struct scsi_get_config_feature *) 10393 &feature->feature_data[feature->add_length]; 10394 10395 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10396 goto done; 10397 10398f10: /* Random Read */ 10399 scsi_ulto2b(0x0010, feature->feature_code); 10400 feature->flags = 0x00; 10401 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10402 feature->flags |= SGC_F_CURRENT; 10403 feature->add_length = 8; 10404 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10405 scsi_ulto2b(1, &feature->feature_data[4]); 10406 feature->feature_data[6] = 0x00; 10407 feature = (struct scsi_get_config_feature *) 10408 &feature->feature_data[feature->add_length]; 10409 10410f1d: /* Multi-Read */ 10411 scsi_ulto2b(0x001D, feature->feature_code); 10412 feature->flags = 0x00; 10413 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10414 feature->flags |= SGC_F_CURRENT; 10415 feature->add_length = 0; 10416 feature = (struct scsi_get_config_feature *) 10417 &feature->feature_data[feature->add_length]; 10418 10419f1e: /* CD Read */ 10420 scsi_ulto2b(0x001E, feature->feature_code); 10421 feature->flags = 0x00; 10422 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10423 feature->flags |= SGC_F_CURRENT; 10424 feature->add_length = 4; 10425 feature->feature_data[0] = 0x00; 10426 feature = (struct scsi_get_config_feature *) 10427 &feature->feature_data[feature->add_length]; 10428 10429f1f: /* DVD Read */ 10430 scsi_ulto2b(0x001F, feature->feature_code); 10431 feature->flags = 0x08; 10432 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10433 feature->flags |= SGC_F_CURRENT; 10434 feature->add_length = 4; 10435 feature->feature_data[0] = 0x01; 10436 feature->feature_data[2] = 0x03; 10437 feature = (struct scsi_get_config_feature *) 10438 &feature->feature_data[feature->add_length]; 10439 10440f2a: /* DVD+RW */ 10441 scsi_ulto2b(0x002A, feature->feature_code); 10442 feature->flags = 0x04; 10443 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10444 feature->flags |= SGC_F_CURRENT; 10445 feature->add_length = 4; 10446 feature->feature_data[0] = 0x00; 10447 feature->feature_data[1] = 0x00; 10448 feature = (struct scsi_get_config_feature *) 10449 &feature->feature_data[feature->add_length]; 10450 10451f2b: /* DVD+R */ 10452 scsi_ulto2b(0x002B, 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 = 4; 10457 feature->feature_data[0] = 0x00; 10458 feature = (struct scsi_get_config_feature *) 10459 &feature->feature_data[feature->add_length]; 10460 10461f3a: /* DVD+RW Dual Layer */ 10462 scsi_ulto2b(0x003A, feature->feature_code); 10463 feature->flags = 0x00; 10464 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10465 feature->flags |= SGC_F_CURRENT; 10466 feature->add_length = 4; 10467 feature->feature_data[0] = 0x00; 10468 feature->feature_data[1] = 0x00; 10469 feature = (struct scsi_get_config_feature *) 10470 &feature->feature_data[feature->add_length]; 10471 10472f3b: /* DVD+R Dual Layer */ 10473 scsi_ulto2b(0x003B, 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 10482done: 10483 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10484 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10485 feature = (struct scsi_get_config_feature *)(hdr + 1); 10486 if (scsi_2btoul(feature->feature_code) == starting) 10487 feature = (struct scsi_get_config_feature *) 10488 &feature->feature_data[feature->add_length]; 10489 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10490 } 10491 scsi_ulto4b(data_len - 4, hdr->data_length); 10492 if (data_len < alloc_len) { 10493 ctsio->residual = alloc_len - data_len; 10494 ctsio->kern_data_len = data_len; 10495 ctsio->kern_total_len = data_len; 10496 } else { 10497 ctsio->residual = 0; 10498 ctsio->kern_data_len = alloc_len; 10499 ctsio->kern_total_len = alloc_len; 10500 } 10501 10502 ctl_set_success(ctsio); 10503 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10504 ctsio->be_move_done = ctl_config_move_done; 10505 ctl_datamove((union ctl_io *)ctsio); 10506 return (CTL_RETVAL_COMPLETE); 10507} 10508 10509int 10510ctl_get_event_status(struct ctl_scsiio *ctsio) 10511{ 10512 struct scsi_get_event_status_header *hdr; 10513 struct scsi_get_event_status *cdb; 10514 uint32_t alloc_len, data_len; 10515 int notif_class; 10516 10517 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10518 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10519 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10520 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10521 ctl_done((union ctl_io *)ctsio); 10522 return (CTL_RETVAL_COMPLETE); 10523 } 10524 notif_class = cdb->notif_class; 10525 alloc_len = scsi_2btoul(cdb->length); 10526 10527 data_len = sizeof(struct scsi_get_event_status_header); 10528 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10529 ctsio->kern_sg_entries = 0; 10530 ctsio->kern_data_resid = 0; 10531 ctsio->kern_rel_offset = 0; 10532 10533 if (data_len < alloc_len) { 10534 ctsio->residual = alloc_len - data_len; 10535 ctsio->kern_data_len = data_len; 10536 ctsio->kern_total_len = data_len; 10537 } else { 10538 ctsio->residual = 0; 10539 ctsio->kern_data_len = alloc_len; 10540 ctsio->kern_total_len = alloc_len; 10541 } 10542 10543 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10544 scsi_ulto2b(0, hdr->descr_length); 10545 hdr->nea_class = SGESN_NEA; 10546 hdr->supported_class = 0; 10547 10548 ctl_set_success(ctsio); 10549 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10550 ctsio->be_move_done = ctl_config_move_done; 10551 ctl_datamove((union ctl_io *)ctsio); 10552 return (CTL_RETVAL_COMPLETE); 10553} 10554 10555int 10556ctl_mechanism_status(struct ctl_scsiio *ctsio) 10557{ 10558 struct scsi_mechanism_status_header *hdr; 10559 struct scsi_mechanism_status *cdb; 10560 uint32_t alloc_len, data_len; 10561 10562 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10563 alloc_len = scsi_2btoul(cdb->length); 10564 10565 data_len = sizeof(struct scsi_mechanism_status_header); 10566 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10567 ctsio->kern_sg_entries = 0; 10568 ctsio->kern_data_resid = 0; 10569 ctsio->kern_rel_offset = 0; 10570 10571 if (data_len < alloc_len) { 10572 ctsio->residual = alloc_len - data_len; 10573 ctsio->kern_data_len = data_len; 10574 ctsio->kern_total_len = data_len; 10575 } else { 10576 ctsio->residual = 0; 10577 ctsio->kern_data_len = alloc_len; 10578 ctsio->kern_total_len = alloc_len; 10579 } 10580 10581 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10582 hdr->state1 = 0x00; 10583 hdr->state2 = 0xe0; 10584 scsi_ulto3b(0, hdr->lba); 10585 hdr->slots_num = 0; 10586 scsi_ulto2b(0, hdr->slots_length); 10587 10588 ctl_set_success(ctsio); 10589 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10590 ctsio->be_move_done = ctl_config_move_done; 10591 ctl_datamove((union ctl_io *)ctsio); 10592 return (CTL_RETVAL_COMPLETE); 10593} 10594 10595static void 10596ctl_ultomsf(uint32_t lba, uint8_t *buf) 10597{ 10598 10599 lba += 150; 10600 buf[0] = 0; 10601 buf[1] = bin2bcd((lba / 75) / 60); 10602 buf[2] = bin2bcd((lba / 75) % 60); 10603 buf[3] = bin2bcd(lba % 75); 10604} 10605 10606int 10607ctl_read_toc(struct ctl_scsiio *ctsio) 10608{ 10609 struct ctl_lun *lun = CTL_LUN(ctsio); 10610 struct scsi_read_toc_hdr *hdr; 10611 struct scsi_read_toc_type01_descr *descr; 10612 struct scsi_read_toc *cdb; 10613 uint32_t alloc_len, data_len; 10614 int format, msf; 10615 10616 cdb = (struct scsi_read_toc *)ctsio->cdb; 10617 msf = (cdb->byte2 & CD_MSF) != 0; 10618 format = cdb->format; 10619 alloc_len = scsi_2btoul(cdb->data_len); 10620 10621 data_len = sizeof(struct scsi_read_toc_hdr); 10622 if (format == 0) 10623 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10624 else 10625 data_len += sizeof(struct scsi_read_toc_type01_descr); 10626 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10627 ctsio->kern_sg_entries = 0; 10628 ctsio->kern_data_resid = 0; 10629 ctsio->kern_rel_offset = 0; 10630 10631 if (data_len < alloc_len) { 10632 ctsio->residual = alloc_len - data_len; 10633 ctsio->kern_data_len = data_len; 10634 ctsio->kern_total_len = data_len; 10635 } else { 10636 ctsio->residual = 0; 10637 ctsio->kern_data_len = alloc_len; 10638 ctsio->kern_total_len = alloc_len; 10639 } 10640 10641 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10642 if (format == 0) { 10643 scsi_ulto2b(0x12, hdr->data_length); 10644 hdr->first = 1; 10645 hdr->last = 1; 10646 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10647 descr->addr_ctl = 0x14; 10648 descr->track_number = 1; 10649 if (msf) 10650 ctl_ultomsf(0, descr->track_start); 10651 else 10652 scsi_ulto4b(0, descr->track_start); 10653 descr++; 10654 descr->addr_ctl = 0x14; 10655 descr->track_number = 0xaa; 10656 if (msf) 10657 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10658 else 10659 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10660 } else { 10661 scsi_ulto2b(0x0a, hdr->data_length); 10662 hdr->first = 1; 10663 hdr->last = 1; 10664 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10665 descr->addr_ctl = 0x14; 10666 descr->track_number = 1; 10667 if (msf) 10668 ctl_ultomsf(0, descr->track_start); 10669 else 10670 scsi_ulto4b(0, descr->track_start); 10671 } 10672 10673 ctl_set_success(ctsio); 10674 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10675 ctsio->be_move_done = ctl_config_move_done; 10676 ctl_datamove((union ctl_io *)ctsio); 10677 return (CTL_RETVAL_COMPLETE); 10678} 10679 10680/* 10681 * For known CDB types, parse the LBA and length. 10682 */ 10683static int 10684ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10685{ 10686 if (io->io_hdr.io_type != CTL_IO_SCSI) 10687 return (1); 10688 10689 switch (io->scsiio.cdb[0]) { 10690 case COMPARE_AND_WRITE: { 10691 struct scsi_compare_and_write *cdb; 10692 10693 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10694 10695 *lba = scsi_8btou64(cdb->addr); 10696 *len = cdb->length; 10697 break; 10698 } 10699 case READ_6: 10700 case WRITE_6: { 10701 struct scsi_rw_6 *cdb; 10702 10703 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10704 10705 *lba = scsi_3btoul(cdb->addr); 10706 /* only 5 bits are valid in the most significant address byte */ 10707 *lba &= 0x1fffff; 10708 *len = cdb->length; 10709 break; 10710 } 10711 case READ_10: 10712 case WRITE_10: { 10713 struct scsi_rw_10 *cdb; 10714 10715 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10716 10717 *lba = scsi_4btoul(cdb->addr); 10718 *len = scsi_2btoul(cdb->length); 10719 break; 10720 } 10721 case WRITE_VERIFY_10: { 10722 struct scsi_write_verify_10 *cdb; 10723 10724 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10725 10726 *lba = scsi_4btoul(cdb->addr); 10727 *len = scsi_2btoul(cdb->length); 10728 break; 10729 } 10730 case READ_12: 10731 case WRITE_12: { 10732 struct scsi_rw_12 *cdb; 10733 10734 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10735 10736 *lba = scsi_4btoul(cdb->addr); 10737 *len = scsi_4btoul(cdb->length); 10738 break; 10739 } 10740 case WRITE_VERIFY_12: { 10741 struct scsi_write_verify_12 *cdb; 10742 10743 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10744 10745 *lba = scsi_4btoul(cdb->addr); 10746 *len = scsi_4btoul(cdb->length); 10747 break; 10748 } 10749 case READ_16: 10750 case WRITE_16: { 10751 struct scsi_rw_16 *cdb; 10752 10753 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10754 10755 *lba = scsi_8btou64(cdb->addr); 10756 *len = scsi_4btoul(cdb->length); 10757 break; 10758 } 10759 case WRITE_ATOMIC_16: { 10760 struct scsi_write_atomic_16 *cdb; 10761 10762 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10763 10764 *lba = scsi_8btou64(cdb->addr); 10765 *len = scsi_2btoul(cdb->length); 10766 break; 10767 } 10768 case WRITE_VERIFY_16: { 10769 struct scsi_write_verify_16 *cdb; 10770 10771 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10772 10773 *lba = scsi_8btou64(cdb->addr); 10774 *len = scsi_4btoul(cdb->length); 10775 break; 10776 } 10777 case WRITE_SAME_10: { 10778 struct scsi_write_same_10 *cdb; 10779 10780 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10781 10782 *lba = scsi_4btoul(cdb->addr); 10783 *len = scsi_2btoul(cdb->length); 10784 break; 10785 } 10786 case WRITE_SAME_16: { 10787 struct scsi_write_same_16 *cdb; 10788 10789 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10790 10791 *lba = scsi_8btou64(cdb->addr); 10792 *len = scsi_4btoul(cdb->length); 10793 break; 10794 } 10795 case VERIFY_10: { 10796 struct scsi_verify_10 *cdb; 10797 10798 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10799 10800 *lba = scsi_4btoul(cdb->addr); 10801 *len = scsi_2btoul(cdb->length); 10802 break; 10803 } 10804 case VERIFY_12: { 10805 struct scsi_verify_12 *cdb; 10806 10807 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10808 10809 *lba = scsi_4btoul(cdb->addr); 10810 *len = scsi_4btoul(cdb->length); 10811 break; 10812 } 10813 case VERIFY_16: { 10814 struct scsi_verify_16 *cdb; 10815 10816 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10817 10818 *lba = scsi_8btou64(cdb->addr); 10819 *len = scsi_4btoul(cdb->length); 10820 break; 10821 } 10822 case UNMAP: { 10823 *lba = 0; 10824 *len = UINT64_MAX; 10825 break; 10826 } 10827 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10828 struct scsi_get_lba_status *cdb; 10829 10830 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10831 *lba = scsi_8btou64(cdb->addr); 10832 *len = UINT32_MAX; 10833 break; 10834 } 10835 default: 10836 return (1); 10837 break; /* NOTREACHED */ 10838 } 10839 10840 return (0); 10841} 10842 10843static ctl_action 10844ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10845 bool seq) 10846{ 10847 uint64_t endlba1, endlba2; 10848 10849 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10850 endlba2 = lba2 + len2 - 1; 10851 10852 if ((endlba1 < lba2) || (endlba2 < lba1)) 10853 return (CTL_ACTION_PASS); 10854 else 10855 return (CTL_ACTION_BLOCK); 10856} 10857 10858static int 10859ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10860{ 10861 struct ctl_ptr_len_flags *ptrlen; 10862 struct scsi_unmap_desc *buf, *end, *range; 10863 uint64_t lba; 10864 uint32_t len; 10865 10866 /* If not UNMAP -- go other way. */ 10867 if (io->io_hdr.io_type != CTL_IO_SCSI || 10868 io->scsiio.cdb[0] != UNMAP) 10869 return (CTL_ACTION_ERROR); 10870 10871 /* If UNMAP without data -- block and wait for data. */ 10872 ptrlen = (struct ctl_ptr_len_flags *) 10873 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10874 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10875 ptrlen->ptr == NULL) 10876 return (CTL_ACTION_BLOCK); 10877 10878 /* UNMAP with data -- check for collision. */ 10879 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10880 end = buf + ptrlen->len / sizeof(*buf); 10881 for (range = buf; range < end; range++) { 10882 lba = scsi_8btou64(range->lba); 10883 len = scsi_4btoul(range->length); 10884 if ((lba < lba2 + len2) && (lba + len > lba2)) 10885 return (CTL_ACTION_BLOCK); 10886 } 10887 return (CTL_ACTION_PASS); 10888} 10889 10890static ctl_action 10891ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10892{ 10893 uint64_t lba1, lba2; 10894 uint64_t len1, len2; 10895 int retval; 10896 10897 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10898 return (CTL_ACTION_ERROR); 10899 10900 retval = ctl_extent_check_unmap(io1, lba2, len2); 10901 if (retval != CTL_ACTION_ERROR) 10902 return (retval); 10903 10904 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10905 return (CTL_ACTION_ERROR); 10906 10907 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10908 seq = FALSE; 10909 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10910} 10911 10912static ctl_action 10913ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10914{ 10915 uint64_t lba1, lba2; 10916 uint64_t len1, len2; 10917 10918 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10919 return (CTL_ACTION_PASS); 10920 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10921 return (CTL_ACTION_ERROR); 10922 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10923 return (CTL_ACTION_ERROR); 10924 10925 if (lba1 + len1 == lba2) 10926 return (CTL_ACTION_BLOCK); 10927 return (CTL_ACTION_PASS); 10928} 10929 10930static ctl_action 10931ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10932 union ctl_io *ooa_io) 10933{ 10934 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10935 const ctl_serialize_action *serialize_row; 10936 10937 /* 10938 * The initiator attempted multiple untagged commands at the same 10939 * time. Can't do that. 10940 */ 10941 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10942 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10943 && ((pending_io->io_hdr.nexus.targ_port == 10944 ooa_io->io_hdr.nexus.targ_port) 10945 && (pending_io->io_hdr.nexus.initid == 10946 ooa_io->io_hdr.nexus.initid)) 10947 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10948 CTL_FLAG_STATUS_SENT)) == 0)) 10949 return (CTL_ACTION_OVERLAP); 10950 10951 /* 10952 * The initiator attempted to send multiple tagged commands with 10953 * the same ID. (It's fine if different initiators have the same 10954 * tag ID.) 10955 * 10956 * Even if all of those conditions are true, we don't kill the I/O 10957 * if the command ahead of us has been aborted. We won't end up 10958 * sending it to the FETD, and it's perfectly legal to resend a 10959 * command with the same tag number as long as the previous 10960 * instance of this tag number has been aborted somehow. 10961 */ 10962 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10963 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10964 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10965 && ((pending_io->io_hdr.nexus.targ_port == 10966 ooa_io->io_hdr.nexus.targ_port) 10967 && (pending_io->io_hdr.nexus.initid == 10968 ooa_io->io_hdr.nexus.initid)) 10969 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10970 CTL_FLAG_STATUS_SENT)) == 0)) 10971 return (CTL_ACTION_OVERLAP_TAG); 10972 10973 /* 10974 * If we get a head of queue tag, SAM-3 says that we should 10975 * immediately execute it. 10976 * 10977 * What happens if this command would normally block for some other 10978 * reason? e.g. a request sense with a head of queue tag 10979 * immediately after a write. Normally that would block, but this 10980 * will result in its getting executed immediately... 10981 * 10982 * We currently return "pass" instead of "skip", so we'll end up 10983 * going through the rest of the queue to check for overlapped tags. 10984 * 10985 * XXX KDM check for other types of blockage first?? 10986 */ 10987 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10988 return (CTL_ACTION_PASS); 10989 10990 /* 10991 * Ordered tags have to block until all items ahead of them 10992 * have completed. If we get called with an ordered tag, we always 10993 * block, if something else is ahead of us in the queue. 10994 */ 10995 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10996 return (CTL_ACTION_BLOCK); 10997 10998 /* 10999 * Simple tags get blocked until all head of queue and ordered tags 11000 * ahead of them have completed. I'm lumping untagged commands in 11001 * with simple tags here. XXX KDM is that the right thing to do? 11002 */ 11003 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11004 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11005 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11006 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11007 return (CTL_ACTION_BLOCK); 11008 11009 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11010 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 11011 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 11012 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 11013 pending_io->scsiio.cdb[1], pending_io)); 11014 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11015 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 11016 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 11017 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 11018 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 11019 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 11020 ooa_io->scsiio.cdb[1], ooa_io)); 11021 11022 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11023 11024 switch (serialize_row[pending_entry->seridx]) { 11025 case CTL_SER_BLOCK: 11026 return (CTL_ACTION_BLOCK); 11027 case CTL_SER_EXTENT: 11028 return (ctl_extent_check(ooa_io, pending_io, 11029 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11030 case CTL_SER_EXTENTOPT: 11031 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11032 SCP_QUEUE_ALG_UNRESTRICTED) 11033 return (ctl_extent_check(ooa_io, pending_io, 11034 (lun->be_lun && 11035 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11036 return (CTL_ACTION_PASS); 11037 case CTL_SER_EXTENTSEQ: 11038 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11039 return (ctl_extent_check_seq(ooa_io, pending_io)); 11040 return (CTL_ACTION_PASS); 11041 case CTL_SER_PASS: 11042 return (CTL_ACTION_PASS); 11043 case CTL_SER_BLOCKOPT: 11044 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11045 SCP_QUEUE_ALG_UNRESTRICTED) 11046 return (CTL_ACTION_BLOCK); 11047 return (CTL_ACTION_PASS); 11048 case CTL_SER_SKIP: 11049 return (CTL_ACTION_SKIP); 11050 default: 11051 panic("%s: Invalid serialization value %d for %d => %d", 11052 __func__, serialize_row[pending_entry->seridx], 11053 pending_entry->seridx, ooa_entry->seridx); 11054 } 11055 11056 return (CTL_ACTION_ERROR); 11057} 11058 11059/* 11060 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11061 * Assumptions: 11062 * - pending_io is generally either incoming, or on the blocked queue 11063 * - starting I/O is the I/O we want to start the check with. 11064 */ 11065static ctl_action 11066ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11067 union ctl_io *starting_io) 11068{ 11069 union ctl_io *ooa_io; 11070 ctl_action action; 11071 11072 mtx_assert(&lun->lun_lock, MA_OWNED); 11073 11074 /* 11075 * Run back along the OOA queue, starting with the current 11076 * blocked I/O and going through every I/O before it on the 11077 * queue. If starting_io is NULL, we'll just end up returning 11078 * CTL_ACTION_PASS. 11079 */ 11080 for (ooa_io = starting_io; ooa_io != NULL; 11081 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11082 ooa_links)){ 11083 11084 /* 11085 * This routine just checks to see whether 11086 * cur_blocked is blocked by ooa_io, which is ahead 11087 * of it in the queue. It doesn't queue/dequeue 11088 * cur_blocked. 11089 */ 11090 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11091 switch (action) { 11092 case CTL_ACTION_BLOCK: 11093 case CTL_ACTION_OVERLAP: 11094 case CTL_ACTION_OVERLAP_TAG: 11095 case CTL_ACTION_SKIP: 11096 case CTL_ACTION_ERROR: 11097 return (action); 11098 break; /* NOTREACHED */ 11099 case CTL_ACTION_PASS: 11100 break; 11101 default: 11102 panic("%s: Invalid action %d\n", __func__, action); 11103 } 11104 } 11105 11106 return (CTL_ACTION_PASS); 11107} 11108 11109/* 11110 * Assumptions: 11111 * - An I/O has just completed, and has been removed from the per-LUN OOA 11112 * queue, so some items on the blocked queue may now be unblocked. 11113 */ 11114static int 11115ctl_check_blocked(struct ctl_lun *lun) 11116{ 11117 struct ctl_softc *softc = lun->ctl_softc; 11118 union ctl_io *cur_blocked, *next_blocked; 11119 11120 mtx_assert(&lun->lun_lock, MA_OWNED); 11121 11122 /* 11123 * Run forward from the head of the blocked queue, checking each 11124 * entry against the I/Os prior to it on the OOA queue to see if 11125 * there is still any blockage. 11126 * 11127 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11128 * with our removing a variable on it while it is traversing the 11129 * list. 11130 */ 11131 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11132 cur_blocked != NULL; cur_blocked = next_blocked) { 11133 union ctl_io *prev_ooa; 11134 ctl_action action; 11135 11136 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11137 blocked_links); 11138 11139 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11140 ctl_ooaq, ooa_links); 11141 11142 /* 11143 * If cur_blocked happens to be the first item in the OOA 11144 * queue now, prev_ooa will be NULL, and the action 11145 * returned will just be CTL_ACTION_PASS. 11146 */ 11147 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11148 11149 switch (action) { 11150 case CTL_ACTION_BLOCK: 11151 /* Nothing to do here, still blocked */ 11152 break; 11153 case CTL_ACTION_OVERLAP: 11154 case CTL_ACTION_OVERLAP_TAG: 11155 /* 11156 * This shouldn't happen! In theory we've already 11157 * checked this command for overlap... 11158 */ 11159 break; 11160 case CTL_ACTION_PASS: 11161 case CTL_ACTION_SKIP: { 11162 const struct ctl_cmd_entry *entry; 11163 11164 /* 11165 * The skip case shouldn't happen, this transaction 11166 * should have never made it onto the blocked queue. 11167 */ 11168 /* 11169 * This I/O is no longer blocked, we can remove it 11170 * from the blocked queue. Since this is a TAILQ 11171 * (doubly linked list), we can do O(1) removals 11172 * from any place on the list. 11173 */ 11174 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11175 blocked_links); 11176 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11177 11178 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11179 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11180 /* 11181 * Need to send IO back to original side to 11182 * run 11183 */ 11184 union ctl_ha_msg msg_info; 11185 11186 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11187 msg_info.hdr.original_sc = 11188 cur_blocked->io_hdr.original_sc; 11189 msg_info.hdr.serializing_sc = cur_blocked; 11190 msg_info.hdr.msg_type = CTL_MSG_R2R; 11191 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11192 sizeof(msg_info.hdr), M_NOWAIT); 11193 break; 11194 } 11195 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11196 11197 /* 11198 * Check this I/O for LUN state changes that may 11199 * have happened while this command was blocked. 11200 * The LUN state may have been changed by a command 11201 * ahead of us in the queue, so we need to re-check 11202 * for any states that can be caused by SCSI 11203 * commands. 11204 */ 11205 if (ctl_scsiio_lun_check(lun, entry, 11206 &cur_blocked->scsiio) == 0) { 11207 cur_blocked->io_hdr.flags |= 11208 CTL_FLAG_IS_WAS_ON_RTR; 11209 ctl_enqueue_rtr(cur_blocked); 11210 } else 11211 ctl_done(cur_blocked); 11212 break; 11213 } 11214 default: 11215 /* 11216 * This probably shouldn't happen -- we shouldn't 11217 * get CTL_ACTION_ERROR, or anything else. 11218 */ 11219 break; 11220 } 11221 } 11222 11223 return (CTL_RETVAL_COMPLETE); 11224} 11225 11226/* 11227 * This routine (with one exception) checks LUN flags that can be set by 11228 * commands ahead of us in the OOA queue. These flags have to be checked 11229 * when a command initially comes in, and when we pull a command off the 11230 * blocked queue and are preparing to execute it. The reason we have to 11231 * check these flags for commands on the blocked queue is that the LUN 11232 * state may have been changed by a command ahead of us while we're on the 11233 * blocked queue. 11234 * 11235 * Ordering is somewhat important with these checks, so please pay 11236 * careful attention to the placement of any new checks. 11237 */ 11238static int 11239ctl_scsiio_lun_check(struct ctl_lun *lun, 11240 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11241{ 11242 struct ctl_softc *softc = lun->ctl_softc; 11243 int retval; 11244 uint32_t residx; 11245 11246 retval = 0; 11247 11248 mtx_assert(&lun->lun_lock, MA_OWNED); 11249 11250 /* 11251 * If this shelf is a secondary shelf controller, we may have to 11252 * reject some commands disallowed by HA mode and link state. 11253 */ 11254 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11255 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11256 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11257 ctl_set_lun_unavail(ctsio); 11258 retval = 1; 11259 goto bailout; 11260 } 11261 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11262 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11263 ctl_set_lun_transit(ctsio); 11264 retval = 1; 11265 goto bailout; 11266 } 11267 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11268 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11269 ctl_set_lun_standby(ctsio); 11270 retval = 1; 11271 goto bailout; 11272 } 11273 11274 /* The rest of checks are only done on executing side */ 11275 if (softc->ha_mode == CTL_HA_MODE_XFER) 11276 goto bailout; 11277 } 11278 11279 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11280 if (lun->be_lun && 11281 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11282 ctl_set_hw_write_protected(ctsio); 11283 retval = 1; 11284 goto bailout; 11285 } 11286 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11287 ctl_set_sense(ctsio, /*current_error*/ 1, 11288 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11289 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11290 retval = 1; 11291 goto bailout; 11292 } 11293 } 11294 11295 /* 11296 * Check for a reservation conflict. If this command isn't allowed 11297 * even on reserved LUNs, and if this initiator isn't the one who 11298 * reserved us, reject the command with a reservation conflict. 11299 */ 11300 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11301 if ((lun->flags & CTL_LUN_RESERVED) 11302 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11303 if (lun->res_idx != residx) { 11304 ctl_set_reservation_conflict(ctsio); 11305 retval = 1; 11306 goto bailout; 11307 } 11308 } 11309 11310 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11311 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11312 /* No reservation or command is allowed. */; 11313 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11314 (lun->pr_res_type == SPR_TYPE_WR_EX || 11315 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11316 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11317 /* The command is allowed for Write Exclusive resv. */; 11318 } else { 11319 /* 11320 * if we aren't registered or it's a res holder type 11321 * reservation and this isn't the res holder then set a 11322 * conflict. 11323 */ 11324 if (ctl_get_prkey(lun, residx) == 0 || 11325 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11326 ctl_set_reservation_conflict(ctsio); 11327 retval = 1; 11328 goto bailout; 11329 } 11330 } 11331 11332 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11333 if (lun->flags & CTL_LUN_EJECTED) 11334 ctl_set_lun_ejected(ctsio); 11335 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11336 if (lun->flags & CTL_LUN_REMOVABLE) 11337 ctl_set_lun_no_media(ctsio); 11338 else 11339 ctl_set_lun_int_reqd(ctsio); 11340 } else if (lun->flags & CTL_LUN_STOPPED) 11341 ctl_set_lun_stopped(ctsio); 11342 else 11343 goto bailout; 11344 retval = 1; 11345 goto bailout; 11346 } 11347 11348bailout: 11349 return (retval); 11350} 11351 11352static void 11353ctl_failover_io(union ctl_io *io, int have_lock) 11354{ 11355 ctl_set_busy(&io->scsiio); 11356 ctl_done(io); 11357} 11358 11359static void 11360ctl_failover_lun(union ctl_io *rio) 11361{ 11362 struct ctl_softc *softc = CTL_SOFTC(rio); 11363 struct ctl_lun *lun; 11364 struct ctl_io_hdr *io, *next_io; 11365 uint32_t targ_lun; 11366 11367 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11368 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11369 11370 /* Find and lock the LUN. */ 11371 mtx_lock(&softc->ctl_lock); 11372 if (targ_lun > CTL_MAX_LUNS || 11373 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11374 mtx_unlock(&softc->ctl_lock); 11375 return; 11376 } 11377 mtx_lock(&lun->lun_lock); 11378 mtx_unlock(&softc->ctl_lock); 11379 if (lun->flags & CTL_LUN_DISABLED) { 11380 mtx_unlock(&lun->lun_lock); 11381 return; 11382 } 11383 11384 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11385 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11386 /* We are master */ 11387 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11388 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11389 io->flags |= CTL_FLAG_ABORT; 11390 io->flags |= CTL_FLAG_FAILOVER; 11391 } else { /* This can be only due to DATAMOVE */ 11392 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11393 io->flags &= ~CTL_FLAG_DMA_INPROG; 11394 io->flags |= CTL_FLAG_IO_ACTIVE; 11395 io->port_status = 31340; 11396 ctl_enqueue_isc((union ctl_io *)io); 11397 } 11398 } 11399 /* We are slave */ 11400 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11401 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11402 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11403 io->flags |= CTL_FLAG_FAILOVER; 11404 } else { 11405 ctl_set_busy(&((union ctl_io *)io)-> 11406 scsiio); 11407 ctl_done((union ctl_io *)io); 11408 } 11409 } 11410 } 11411 } else { /* SERIALIZE modes */ 11412 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11413 next_io) { 11414 /* We are master */ 11415 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11416 TAILQ_REMOVE(&lun->blocked_queue, io, 11417 blocked_links); 11418 io->flags &= ~CTL_FLAG_BLOCKED; 11419 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11420 ctl_free_io((union ctl_io *)io); 11421 } 11422 } 11423 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11424 /* We are master */ 11425 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11426 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11427 ctl_free_io((union ctl_io *)io); 11428 } 11429 /* We are slave */ 11430 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11431 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11432 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11433 ctl_set_busy(&((union ctl_io *)io)-> 11434 scsiio); 11435 ctl_done((union ctl_io *)io); 11436 } 11437 } 11438 } 11439 ctl_check_blocked(lun); 11440 } 11441 mtx_unlock(&lun->lun_lock); 11442} 11443 11444static int 11445ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11446{ 11447 struct ctl_lun *lun; 11448 const struct ctl_cmd_entry *entry; 11449 uint32_t initidx, targ_lun; 11450 int retval = 0; 11451 11452 lun = NULL; 11453 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11454 if (targ_lun < CTL_MAX_LUNS) 11455 lun = softc->ctl_luns[targ_lun]; 11456 if (lun) { 11457 /* 11458 * If the LUN is invalid, pretend that it doesn't exist. 11459 * It will go away as soon as all pending I/O has been 11460 * completed. 11461 */ 11462 mtx_lock(&lun->lun_lock); 11463 if (lun->flags & CTL_LUN_DISABLED) { 11464 mtx_unlock(&lun->lun_lock); 11465 lun = NULL; 11466 } 11467 } 11468 CTL_LUN(ctsio) = lun; 11469 if (lun) { 11470 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11471 11472 /* 11473 * Every I/O goes into the OOA queue for a particular LUN, 11474 * and stays there until completion. 11475 */ 11476#ifdef CTL_TIME_IO 11477 if (TAILQ_EMPTY(&lun->ooa_queue)) 11478 lun->idle_time += getsbinuptime() - lun->last_busy; 11479#endif 11480 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11481 } 11482 11483 /* Get command entry and return error if it is unsuppotyed. */ 11484 entry = ctl_validate_command(ctsio); 11485 if (entry == NULL) { 11486 if (lun) 11487 mtx_unlock(&lun->lun_lock); 11488 return (retval); 11489 } 11490 11491 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11492 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11493 11494 /* 11495 * Check to see whether we can send this command to LUNs that don't 11496 * exist. This should pretty much only be the case for inquiry 11497 * and request sense. Further checks, below, really require having 11498 * a LUN, so we can't really check the command anymore. Just put 11499 * it on the rtr queue. 11500 */ 11501 if (lun == NULL) { 11502 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11503 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11504 ctl_enqueue_rtr((union ctl_io *)ctsio); 11505 return (retval); 11506 } 11507 11508 ctl_set_unsupported_lun(ctsio); 11509 ctl_done((union ctl_io *)ctsio); 11510 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11511 return (retval); 11512 } else { 11513 /* 11514 * Make sure we support this particular command on this LUN. 11515 * e.g., we don't support writes to the control LUN. 11516 */ 11517 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11518 mtx_unlock(&lun->lun_lock); 11519 ctl_set_invalid_opcode(ctsio); 11520 ctl_done((union ctl_io *)ctsio); 11521 return (retval); 11522 } 11523 } 11524 11525 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11526 11527#ifdef CTL_WITH_CA 11528 /* 11529 * If we've got a request sense, it'll clear the contingent 11530 * allegiance condition. Otherwise, if we have a CA condition for 11531 * this initiator, clear it, because it sent down a command other 11532 * than request sense. 11533 */ 11534 if ((ctsio->cdb[0] != REQUEST_SENSE) 11535 && (ctl_is_set(lun->have_ca, initidx))) 11536 ctl_clear_mask(lun->have_ca, initidx); 11537#endif 11538 11539 /* 11540 * If the command has this flag set, it handles its own unit 11541 * attention reporting, we shouldn't do anything. Otherwise we 11542 * check for any pending unit attentions, and send them back to the 11543 * initiator. We only do this when a command initially comes in, 11544 * not when we pull it off the blocked queue. 11545 * 11546 * According to SAM-3, section 5.3.2, the order that things get 11547 * presented back to the host is basically unit attentions caused 11548 * by some sort of reset event, busy status, reservation conflicts 11549 * or task set full, and finally any other status. 11550 * 11551 * One issue here is that some of the unit attentions we report 11552 * don't fall into the "reset" category (e.g. "reported luns data 11553 * has changed"). So reporting it here, before the reservation 11554 * check, may be technically wrong. I guess the only thing to do 11555 * would be to check for and report the reset events here, and then 11556 * check for the other unit attention types after we check for a 11557 * reservation conflict. 11558 * 11559 * XXX KDM need to fix this 11560 */ 11561 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11562 ctl_ua_type ua_type; 11563 u_int sense_len = 0; 11564 11565 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11566 &sense_len, SSD_TYPE_NONE); 11567 if (ua_type != CTL_UA_NONE) { 11568 mtx_unlock(&lun->lun_lock); 11569 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11570 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11571 ctsio->sense_len = sense_len; 11572 ctl_done((union ctl_io *)ctsio); 11573 return (retval); 11574 } 11575 } 11576 11577 11578 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11579 mtx_unlock(&lun->lun_lock); 11580 ctl_done((union ctl_io *)ctsio); 11581 return (retval); 11582 } 11583 11584 /* 11585 * XXX CHD this is where we want to send IO to other side if 11586 * this LUN is secondary on this SC. We will need to make a copy 11587 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11588 * the copy we send as FROM_OTHER. 11589 * We also need to stuff the address of the original IO so we can 11590 * find it easily. Something similar will need be done on the other 11591 * side so when we are done we can find the copy. 11592 */ 11593 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11594 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11595 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11596 union ctl_ha_msg msg_info; 11597 int isc_retval; 11598 11599 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11600 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11601 mtx_unlock(&lun->lun_lock); 11602 11603 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11604 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11605 msg_info.hdr.serializing_sc = NULL; 11606 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11607 msg_info.scsi.tag_num = ctsio->tag_num; 11608 msg_info.scsi.tag_type = ctsio->tag_type; 11609 msg_info.scsi.cdb_len = ctsio->cdb_len; 11610 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11611 11612 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11613 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11614 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11615 ctl_set_busy(ctsio); 11616 ctl_done((union ctl_io *)ctsio); 11617 return (retval); 11618 } 11619 return (retval); 11620 } 11621 11622 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11623 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11624 ctl_ooaq, ooa_links))) { 11625 case CTL_ACTION_BLOCK: 11626 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11627 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11628 blocked_links); 11629 mtx_unlock(&lun->lun_lock); 11630 return (retval); 11631 case CTL_ACTION_PASS: 11632 case CTL_ACTION_SKIP: 11633 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11634 mtx_unlock(&lun->lun_lock); 11635 ctl_enqueue_rtr((union ctl_io *)ctsio); 11636 break; 11637 case CTL_ACTION_OVERLAP: 11638 mtx_unlock(&lun->lun_lock); 11639 ctl_set_overlapped_cmd(ctsio); 11640 ctl_done((union ctl_io *)ctsio); 11641 break; 11642 case CTL_ACTION_OVERLAP_TAG: 11643 mtx_unlock(&lun->lun_lock); 11644 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11645 ctl_done((union ctl_io *)ctsio); 11646 break; 11647 case CTL_ACTION_ERROR: 11648 default: 11649 mtx_unlock(&lun->lun_lock); 11650 ctl_set_internal_failure(ctsio, 11651 /*sks_valid*/ 0, 11652 /*retry_count*/ 0); 11653 ctl_done((union ctl_io *)ctsio); 11654 break; 11655 } 11656 return (retval); 11657} 11658 11659const struct ctl_cmd_entry * 11660ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11661{ 11662 const struct ctl_cmd_entry *entry; 11663 int service_action; 11664 11665 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11666 if (sa) 11667 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11668 if (entry->flags & CTL_CMD_FLAG_SA5) { 11669 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11670 entry = &((const struct ctl_cmd_entry *) 11671 entry->execute)[service_action]; 11672 } 11673 return (entry); 11674} 11675 11676const struct ctl_cmd_entry * 11677ctl_validate_command(struct ctl_scsiio *ctsio) 11678{ 11679 const struct ctl_cmd_entry *entry; 11680 int i, sa; 11681 uint8_t diff; 11682 11683 entry = ctl_get_cmd_entry(ctsio, &sa); 11684 if (entry->execute == NULL) { 11685 if (sa) 11686 ctl_set_invalid_field(ctsio, 11687 /*sks_valid*/ 1, 11688 /*command*/ 1, 11689 /*field*/ 1, 11690 /*bit_valid*/ 1, 11691 /*bit*/ 4); 11692 else 11693 ctl_set_invalid_opcode(ctsio); 11694 ctl_done((union ctl_io *)ctsio); 11695 return (NULL); 11696 } 11697 KASSERT(entry->length > 0, 11698 ("Not defined length for command 0x%02x/0x%02x", 11699 ctsio->cdb[0], ctsio->cdb[1])); 11700 for (i = 1; i < entry->length; i++) { 11701 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11702 if (diff == 0) 11703 continue; 11704 ctl_set_invalid_field(ctsio, 11705 /*sks_valid*/ 1, 11706 /*command*/ 1, 11707 /*field*/ i, 11708 /*bit_valid*/ 1, 11709 /*bit*/ fls(diff) - 1); 11710 ctl_done((union ctl_io *)ctsio); 11711 return (NULL); 11712 } 11713 return (entry); 11714} 11715 11716static int 11717ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11718{ 11719 11720 switch (lun_type) { 11721 case T_DIRECT: 11722 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11723 return (0); 11724 break; 11725 case T_PROCESSOR: 11726 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11727 return (0); 11728 break; 11729 case T_CDROM: 11730 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11731 return (0); 11732 break; 11733 default: 11734 return (0); 11735 } 11736 return (1); 11737} 11738 11739static int 11740ctl_scsiio(struct ctl_scsiio *ctsio) 11741{ 11742 int retval; 11743 const struct ctl_cmd_entry *entry; 11744 11745 retval = CTL_RETVAL_COMPLETE; 11746 11747 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11748 11749 entry = ctl_get_cmd_entry(ctsio, NULL); 11750 11751 /* 11752 * If this I/O has been aborted, just send it straight to 11753 * ctl_done() without executing it. 11754 */ 11755 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11756 ctl_done((union ctl_io *)ctsio); 11757 goto bailout; 11758 } 11759 11760 /* 11761 * All the checks should have been handled by ctl_scsiio_precheck(). 11762 * We should be clear now to just execute the I/O. 11763 */ 11764 retval = entry->execute(ctsio); 11765 11766bailout: 11767 return (retval); 11768} 11769 11770/* 11771 * Since we only implement one target right now, a bus reset simply resets 11772 * our single target. 11773 */ 11774static int 11775ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11776{ 11777 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11778} 11779 11780static int 11781ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11782 ctl_ua_type ua_type) 11783{ 11784 struct ctl_port *port = CTL_PORT(io); 11785 struct ctl_lun *lun; 11786 int retval; 11787 11788 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11789 union ctl_ha_msg msg_info; 11790 11791 msg_info.hdr.nexus = io->io_hdr.nexus; 11792 if (ua_type==CTL_UA_TARG_RESET) 11793 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11794 else 11795 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11796 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11797 msg_info.hdr.original_sc = NULL; 11798 msg_info.hdr.serializing_sc = NULL; 11799 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11800 sizeof(msg_info.task), M_WAITOK); 11801 } 11802 retval = 0; 11803 11804 mtx_lock(&softc->ctl_lock); 11805 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11806 if (port != NULL && 11807 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11808 continue; 11809 retval += ctl_do_lun_reset(lun, io, ua_type); 11810 } 11811 mtx_unlock(&softc->ctl_lock); 11812 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11813 return (retval); 11814} 11815 11816/* 11817 * The LUN should always be set. The I/O is optional, and is used to 11818 * distinguish between I/Os sent by this initiator, and by other 11819 * initiators. We set unit attention for initiators other than this one. 11820 * SAM-3 is vague on this point. It does say that a unit attention should 11821 * be established for other initiators when a LUN is reset (see section 11822 * 5.7.3), but it doesn't specifically say that the unit attention should 11823 * be established for this particular initiator when a LUN is reset. Here 11824 * is the relevant text, from SAM-3 rev 8: 11825 * 11826 * 5.7.2 When a SCSI initiator port aborts its own tasks 11827 * 11828 * When a SCSI initiator port causes its own task(s) to be aborted, no 11829 * notification that the task(s) have been aborted shall be returned to 11830 * the SCSI initiator port other than the completion response for the 11831 * command or task management function action that caused the task(s) to 11832 * be aborted and notification(s) associated with related effects of the 11833 * action (e.g., a reset unit attention condition). 11834 * 11835 * XXX KDM for now, we're setting unit attention for all initiators. 11836 */ 11837static int 11838ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11839{ 11840 union ctl_io *xio; 11841#if 0 11842 uint32_t initidx; 11843#endif 11844 int i; 11845 11846 mtx_lock(&lun->lun_lock); 11847 /* 11848 * Run through the OOA queue and abort each I/O. 11849 */ 11850 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11851 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11852 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11853 } 11854 11855 /* 11856 * This version sets unit attention for every 11857 */ 11858#if 0 11859 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11860 ctl_est_ua_all(lun, initidx, ua_type); 11861#else 11862 ctl_est_ua_all(lun, -1, ua_type); 11863#endif 11864 11865 /* 11866 * A reset (any kind, really) clears reservations established with 11867 * RESERVE/RELEASE. It does not clear reservations established 11868 * with PERSISTENT RESERVE OUT, but we don't support that at the 11869 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11870 * reservations made with the RESERVE/RELEASE commands, because 11871 * those commands are obsolete in SPC-3. 11872 */ 11873 lun->flags &= ~CTL_LUN_RESERVED; 11874 11875#ifdef CTL_WITH_CA 11876 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11877 ctl_clear_mask(lun->have_ca, i); 11878#endif 11879 lun->prevent_count = 0; 11880 if (lun->prevent) { 11881 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11882 ctl_clear_mask(lun->prevent, i); 11883 } 11884 mtx_unlock(&lun->lun_lock); 11885 11886 return (0); 11887} 11888 11889static int 11890ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11891{ 11892 struct ctl_lun *lun; 11893 uint32_t targ_lun; 11894 int retval; 11895 11896 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11897 mtx_lock(&softc->ctl_lock); 11898 if (targ_lun >= CTL_MAX_LUNS || 11899 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11900 mtx_unlock(&softc->ctl_lock); 11901 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11902 return (1); 11903 } 11904 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11905 mtx_unlock(&softc->ctl_lock); 11906 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11907 11908 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11909 union ctl_ha_msg msg_info; 11910 11911 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11912 msg_info.hdr.nexus = io->io_hdr.nexus; 11913 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11914 msg_info.hdr.original_sc = NULL; 11915 msg_info.hdr.serializing_sc = NULL; 11916 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11917 sizeof(msg_info.task), M_WAITOK); 11918 } 11919 return (retval); 11920} 11921 11922static void 11923ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11924 int other_sc) 11925{ 11926 union ctl_io *xio; 11927 11928 mtx_assert(&lun->lun_lock, MA_OWNED); 11929 11930 /* 11931 * Run through the OOA queue and attempt to find the given I/O. 11932 * The target port, initiator ID, tag type and tag number have to 11933 * match the values that we got from the initiator. If we have an 11934 * untagged command to abort, simply abort the first untagged command 11935 * we come to. We only allow one untagged command at a time of course. 11936 */ 11937 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11938 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11939 11940 if ((targ_port == UINT32_MAX || 11941 targ_port == xio->io_hdr.nexus.targ_port) && 11942 (init_id == UINT32_MAX || 11943 init_id == xio->io_hdr.nexus.initid)) { 11944 if (targ_port != xio->io_hdr.nexus.targ_port || 11945 init_id != xio->io_hdr.nexus.initid) 11946 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11947 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11948 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11949 union ctl_ha_msg msg_info; 11950 11951 msg_info.hdr.nexus = xio->io_hdr.nexus; 11952 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11953 msg_info.task.tag_num = xio->scsiio.tag_num; 11954 msg_info.task.tag_type = xio->scsiio.tag_type; 11955 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11956 msg_info.hdr.original_sc = NULL; 11957 msg_info.hdr.serializing_sc = NULL; 11958 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11959 sizeof(msg_info.task), M_NOWAIT); 11960 } 11961 } 11962 } 11963} 11964 11965static int 11966ctl_abort_task_set(union ctl_io *io) 11967{ 11968 struct ctl_softc *softc = CTL_SOFTC(io); 11969 struct ctl_lun *lun; 11970 uint32_t targ_lun; 11971 11972 /* 11973 * Look up the LUN. 11974 */ 11975 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11976 mtx_lock(&softc->ctl_lock); 11977 if (targ_lun >= CTL_MAX_LUNS || 11978 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11979 mtx_unlock(&softc->ctl_lock); 11980 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11981 return (1); 11982 } 11983 11984 mtx_lock(&lun->lun_lock); 11985 mtx_unlock(&softc->ctl_lock); 11986 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11987 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11988 io->io_hdr.nexus.initid, 11989 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11990 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11991 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11992 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11993 } 11994 mtx_unlock(&lun->lun_lock); 11995 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11996 return (0); 11997} 11998 11999static int 12000ctl_i_t_nexus_reset(union ctl_io *io) 12001{ 12002 struct ctl_softc *softc = CTL_SOFTC(io); 12003 struct ctl_lun *lun; 12004 uint32_t initidx; 12005 12006 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12007 union ctl_ha_msg msg_info; 12008 12009 msg_info.hdr.nexus = io->io_hdr.nexus; 12010 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12011 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12012 msg_info.hdr.original_sc = NULL; 12013 msg_info.hdr.serializing_sc = NULL; 12014 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12015 sizeof(msg_info.task), M_WAITOK); 12016 } 12017 12018 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12019 mtx_lock(&softc->ctl_lock); 12020 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12021 mtx_lock(&lun->lun_lock); 12022 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12023 io->io_hdr.nexus.initid, 1); 12024#ifdef CTL_WITH_CA 12025 ctl_clear_mask(lun->have_ca, initidx); 12026#endif 12027 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12028 lun->flags &= ~CTL_LUN_RESERVED; 12029 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) { 12030 ctl_clear_mask(lun->prevent, initidx); 12031 lun->prevent_count--; 12032 } 12033 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12034 mtx_unlock(&lun->lun_lock); 12035 } 12036 mtx_unlock(&softc->ctl_lock); 12037 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12038 return (0); 12039} 12040 12041static int 12042ctl_abort_task(union ctl_io *io) 12043{ 12044 struct ctl_softc *softc = CTL_SOFTC(io); 12045 union ctl_io *xio; 12046 struct ctl_lun *lun; 12047#if 0 12048 struct sbuf sb; 12049 char printbuf[128]; 12050#endif 12051 int found; 12052 uint32_t targ_lun; 12053 12054 found = 0; 12055 12056 /* 12057 * Look up the LUN. 12058 */ 12059 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12060 mtx_lock(&softc->ctl_lock); 12061 if (targ_lun >= CTL_MAX_LUNS || 12062 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12063 mtx_unlock(&softc->ctl_lock); 12064 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12065 return (1); 12066 } 12067 12068#if 0 12069 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12070 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12071#endif 12072 12073 mtx_lock(&lun->lun_lock); 12074 mtx_unlock(&softc->ctl_lock); 12075 /* 12076 * Run through the OOA queue and attempt to find the given I/O. 12077 * The target port, initiator ID, tag type and tag number have to 12078 * match the values that we got from the initiator. If we have an 12079 * untagged command to abort, simply abort the first untagged command 12080 * we come to. We only allow one untagged command at a time of course. 12081 */ 12082 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12083 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12084#if 0 12085 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12086 12087 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12088 lun->lun, xio->scsiio.tag_num, 12089 xio->scsiio.tag_type, 12090 (xio->io_hdr.blocked_links.tqe_prev 12091 == NULL) ? "" : " BLOCKED", 12092 (xio->io_hdr.flags & 12093 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12094 (xio->io_hdr.flags & 12095 CTL_FLAG_ABORT) ? " ABORT" : "", 12096 (xio->io_hdr.flags & 12097 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12098 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12099 sbuf_finish(&sb); 12100 printf("%s\n", sbuf_data(&sb)); 12101#endif 12102 12103 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12104 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12105 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12106 continue; 12107 12108 /* 12109 * If the abort says that the task is untagged, the 12110 * task in the queue must be untagged. Otherwise, 12111 * we just check to see whether the tag numbers 12112 * match. This is because the QLogic firmware 12113 * doesn't pass back the tag type in an abort 12114 * request. 12115 */ 12116#if 0 12117 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12118 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12119 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12120#endif 12121 /* 12122 * XXX KDM we've got problems with FC, because it 12123 * doesn't send down a tag type with aborts. So we 12124 * can only really go by the tag number... 12125 * This may cause problems with parallel SCSI. 12126 * Need to figure that out!! 12127 */ 12128 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12129 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12130 found = 1; 12131 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12132 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12133 union ctl_ha_msg msg_info; 12134 12135 msg_info.hdr.nexus = io->io_hdr.nexus; 12136 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12137 msg_info.task.tag_num = io->taskio.tag_num; 12138 msg_info.task.tag_type = io->taskio.tag_type; 12139 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12140 msg_info.hdr.original_sc = NULL; 12141 msg_info.hdr.serializing_sc = NULL; 12142#if 0 12143 printf("Sent Abort to other side\n"); 12144#endif 12145 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12146 sizeof(msg_info.task), M_NOWAIT); 12147 } 12148#if 0 12149 printf("ctl_abort_task: found I/O to abort\n"); 12150#endif 12151 } 12152 } 12153 mtx_unlock(&lun->lun_lock); 12154 12155 if (found == 0) { 12156 /* 12157 * This isn't really an error. It's entirely possible for 12158 * the abort and command completion to cross on the wire. 12159 * This is more of an informative/diagnostic error. 12160 */ 12161#if 0 12162 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12163 "%u:%u:%u tag %d type %d\n", 12164 io->io_hdr.nexus.initid, 12165 io->io_hdr.nexus.targ_port, 12166 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12167 io->taskio.tag_type); 12168#endif 12169 } 12170 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12171 return (0); 12172} 12173 12174static int 12175ctl_query_task(union ctl_io *io, int task_set) 12176{ 12177 struct ctl_softc *softc = CTL_SOFTC(io); 12178 union ctl_io *xio; 12179 struct ctl_lun *lun; 12180 int found = 0; 12181 uint32_t targ_lun; 12182 12183 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12184 mtx_lock(&softc->ctl_lock); 12185 if (targ_lun >= CTL_MAX_LUNS || 12186 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12187 mtx_unlock(&softc->ctl_lock); 12188 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12189 return (1); 12190 } 12191 mtx_lock(&lun->lun_lock); 12192 mtx_unlock(&softc->ctl_lock); 12193 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12194 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12195 12196 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12197 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12198 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12199 continue; 12200 12201 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12202 found = 1; 12203 break; 12204 } 12205 } 12206 mtx_unlock(&lun->lun_lock); 12207 if (found) 12208 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12209 else 12210 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12211 return (0); 12212} 12213 12214static int 12215ctl_query_async_event(union ctl_io *io) 12216{ 12217 struct ctl_softc *softc = CTL_SOFTC(io); 12218 struct ctl_lun *lun; 12219 ctl_ua_type ua; 12220 uint32_t targ_lun, initidx; 12221 12222 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12223 mtx_lock(&softc->ctl_lock); 12224 if (targ_lun >= CTL_MAX_LUNS || 12225 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12226 mtx_unlock(&softc->ctl_lock); 12227 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12228 return (1); 12229 } 12230 mtx_lock(&lun->lun_lock); 12231 mtx_unlock(&softc->ctl_lock); 12232 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12233 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12234 mtx_unlock(&lun->lun_lock); 12235 if (ua != CTL_UA_NONE) 12236 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12237 else 12238 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12239 return (0); 12240} 12241 12242static void 12243ctl_run_task(union ctl_io *io) 12244{ 12245 struct ctl_softc *softc = CTL_SOFTC(io); 12246 int retval = 1; 12247 12248 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12249 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12250 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12251 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12252 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12253 switch (io->taskio.task_action) { 12254 case CTL_TASK_ABORT_TASK: 12255 retval = ctl_abort_task(io); 12256 break; 12257 case CTL_TASK_ABORT_TASK_SET: 12258 case CTL_TASK_CLEAR_TASK_SET: 12259 retval = ctl_abort_task_set(io); 12260 break; 12261 case CTL_TASK_CLEAR_ACA: 12262 break; 12263 case CTL_TASK_I_T_NEXUS_RESET: 12264 retval = ctl_i_t_nexus_reset(io); 12265 break; 12266 case CTL_TASK_LUN_RESET: 12267 retval = ctl_lun_reset(softc, io); 12268 break; 12269 case CTL_TASK_TARGET_RESET: 12270 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12271 break; 12272 case CTL_TASK_BUS_RESET: 12273 retval = ctl_bus_reset(softc, io); 12274 break; 12275 case CTL_TASK_PORT_LOGIN: 12276 break; 12277 case CTL_TASK_PORT_LOGOUT: 12278 break; 12279 case CTL_TASK_QUERY_TASK: 12280 retval = ctl_query_task(io, 0); 12281 break; 12282 case CTL_TASK_QUERY_TASK_SET: 12283 retval = ctl_query_task(io, 1); 12284 break; 12285 case CTL_TASK_QUERY_ASYNC_EVENT: 12286 retval = ctl_query_async_event(io); 12287 break; 12288 default: 12289 printf("%s: got unknown task management event %d\n", 12290 __func__, io->taskio.task_action); 12291 break; 12292 } 12293 if (retval == 0) 12294 io->io_hdr.status = CTL_SUCCESS; 12295 else 12296 io->io_hdr.status = CTL_ERROR; 12297 ctl_done(io); 12298} 12299 12300/* 12301 * For HA operation. Handle commands that come in from the other 12302 * controller. 12303 */ 12304static void 12305ctl_handle_isc(union ctl_io *io) 12306{ 12307 struct ctl_softc *softc = CTL_SOFTC(io); 12308 struct ctl_lun *lun; 12309 const struct ctl_cmd_entry *entry; 12310 uint32_t targ_lun; 12311 12312 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12313 switch (io->io_hdr.msg_type) { 12314 case CTL_MSG_SERIALIZE: 12315 ctl_serialize_other_sc_cmd(&io->scsiio); 12316 break; 12317 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12318 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12319 if (targ_lun >= CTL_MAX_LUNS || 12320 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12321 ctl_done(io); 12322 break; 12323 } 12324 mtx_lock(&lun->lun_lock); 12325 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12326 mtx_unlock(&lun->lun_lock); 12327 ctl_done(io); 12328 break; 12329 } 12330 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12331 mtx_unlock(&lun->lun_lock); 12332 ctl_enqueue_rtr(io); 12333 break; 12334 case CTL_MSG_FINISH_IO: 12335 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12336 ctl_done(io); 12337 break; 12338 } 12339 if (targ_lun >= CTL_MAX_LUNS || 12340 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12341 ctl_free_io(io); 12342 break; 12343 } 12344 mtx_lock(&lun->lun_lock); 12345 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12346 ctl_check_blocked(lun); 12347 mtx_unlock(&lun->lun_lock); 12348 ctl_free_io(io); 12349 break; 12350 case CTL_MSG_PERS_ACTION: 12351 ctl_hndl_per_res_out_on_other_sc(io); 12352 ctl_free_io(io); 12353 break; 12354 case CTL_MSG_BAD_JUJU: 12355 ctl_done(io); 12356 break; 12357 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12358 ctl_datamove_remote(io); 12359 break; 12360 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12361 io->scsiio.be_move_done(io); 12362 break; 12363 case CTL_MSG_FAILOVER: 12364 ctl_failover_lun(io); 12365 ctl_free_io(io); 12366 break; 12367 default: 12368 printf("%s: Invalid message type %d\n", 12369 __func__, io->io_hdr.msg_type); 12370 ctl_free_io(io); 12371 break; 12372 } 12373 12374} 12375 12376 12377/* 12378 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12379 * there is no match. 12380 */ 12381static ctl_lun_error_pattern 12382ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12383{ 12384 const struct ctl_cmd_entry *entry; 12385 ctl_lun_error_pattern filtered_pattern, pattern; 12386 12387 pattern = desc->error_pattern; 12388 12389 /* 12390 * XXX KDM we need more data passed into this function to match a 12391 * custom pattern, and we actually need to implement custom pattern 12392 * matching. 12393 */ 12394 if (pattern & CTL_LUN_PAT_CMD) 12395 return (CTL_LUN_PAT_CMD); 12396 12397 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12398 return (CTL_LUN_PAT_ANY); 12399 12400 entry = ctl_get_cmd_entry(ctsio, NULL); 12401 12402 filtered_pattern = entry->pattern & pattern; 12403 12404 /* 12405 * If the user requested specific flags in the pattern (e.g. 12406 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12407 * flags. 12408 * 12409 * If the user did not specify any flags, it doesn't matter whether 12410 * or not the command supports the flags. 12411 */ 12412 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12413 (pattern & ~CTL_LUN_PAT_MASK)) 12414 return (CTL_LUN_PAT_NONE); 12415 12416 /* 12417 * If the user asked for a range check, see if the requested LBA 12418 * range overlaps with this command's LBA range. 12419 */ 12420 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12421 uint64_t lba1; 12422 uint64_t len1; 12423 ctl_action action; 12424 int retval; 12425 12426 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12427 if (retval != 0) 12428 return (CTL_LUN_PAT_NONE); 12429 12430 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12431 desc->lba_range.len, FALSE); 12432 /* 12433 * A "pass" means that the LBA ranges don't overlap, so 12434 * this doesn't match the user's range criteria. 12435 */ 12436 if (action == CTL_ACTION_PASS) 12437 return (CTL_LUN_PAT_NONE); 12438 } 12439 12440 return (filtered_pattern); 12441} 12442 12443static void 12444ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12445{ 12446 struct ctl_error_desc *desc, *desc2; 12447 12448 mtx_assert(&lun->lun_lock, MA_OWNED); 12449 12450 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12451 ctl_lun_error_pattern pattern; 12452 /* 12453 * Check to see whether this particular command matches 12454 * the pattern in the descriptor. 12455 */ 12456 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12457 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12458 continue; 12459 12460 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12461 case CTL_LUN_INJ_ABORTED: 12462 ctl_set_aborted(&io->scsiio); 12463 break; 12464 case CTL_LUN_INJ_MEDIUM_ERR: 12465 ctl_set_medium_error(&io->scsiio, 12466 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12467 CTL_FLAG_DATA_OUT); 12468 break; 12469 case CTL_LUN_INJ_UA: 12470 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12471 * OCCURRED */ 12472 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12473 break; 12474 case CTL_LUN_INJ_CUSTOM: 12475 /* 12476 * We're assuming the user knows what he is doing. 12477 * Just copy the sense information without doing 12478 * checks. 12479 */ 12480 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12481 MIN(sizeof(desc->custom_sense), 12482 sizeof(io->scsiio.sense_data))); 12483 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12484 io->scsiio.sense_len = SSD_FULL_SIZE; 12485 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12486 break; 12487 case CTL_LUN_INJ_NONE: 12488 default: 12489 /* 12490 * If this is an error injection type we don't know 12491 * about, clear the continuous flag (if it is set) 12492 * so it will get deleted below. 12493 */ 12494 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12495 break; 12496 } 12497 /* 12498 * By default, each error injection action is a one-shot 12499 */ 12500 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12501 continue; 12502 12503 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12504 12505 free(desc, M_CTL); 12506 } 12507} 12508 12509#ifdef CTL_IO_DELAY 12510static void 12511ctl_datamove_timer_wakeup(void *arg) 12512{ 12513 union ctl_io *io; 12514 12515 io = (union ctl_io *)arg; 12516 12517 ctl_datamove(io); 12518} 12519#endif /* CTL_IO_DELAY */ 12520 12521void 12522ctl_datamove(union ctl_io *io) 12523{ 12524 void (*fe_datamove)(union ctl_io *io); 12525 12526 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12527 12528 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12529 12530#ifdef CTL_TIME_IO 12531 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12532 char str[256]; 12533 char path_str[64]; 12534 struct sbuf sb; 12535 12536 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12537 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12538 12539 sbuf_cat(&sb, path_str); 12540 switch (io->io_hdr.io_type) { 12541 case CTL_IO_SCSI: 12542 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12543 sbuf_printf(&sb, "\n"); 12544 sbuf_cat(&sb, path_str); 12545 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12546 io->scsiio.tag_num, io->scsiio.tag_type); 12547 break; 12548 case CTL_IO_TASK: 12549 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12550 "Tag Type: %d\n", io->taskio.task_action, 12551 io->taskio.tag_num, io->taskio.tag_type); 12552 break; 12553 default: 12554 panic("%s: Invalid CTL I/O type %d\n", 12555 __func__, io->io_hdr.io_type); 12556 } 12557 sbuf_cat(&sb, path_str); 12558 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12559 (intmax_t)time_uptime - io->io_hdr.start_time); 12560 sbuf_finish(&sb); 12561 printf("%s", sbuf_data(&sb)); 12562 } 12563#endif /* CTL_TIME_IO */ 12564 12565#ifdef CTL_IO_DELAY 12566 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12567 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12568 } else { 12569 if ((lun != NULL) 12570 && (lun->delay_info.datamove_delay > 0)) { 12571 12572 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12573 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12574 callout_reset(&io->io_hdr.delay_callout, 12575 lun->delay_info.datamove_delay * hz, 12576 ctl_datamove_timer_wakeup, io); 12577 if (lun->delay_info.datamove_type == 12578 CTL_DELAY_TYPE_ONESHOT) 12579 lun->delay_info.datamove_delay = 0; 12580 return; 12581 } 12582 } 12583#endif 12584 12585 /* 12586 * This command has been aborted. Set the port status, so we fail 12587 * the data move. 12588 */ 12589 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12590 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12591 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12592 io->io_hdr.nexus.targ_port, 12593 io->io_hdr.nexus.targ_lun); 12594 io->io_hdr.port_status = 31337; 12595 /* 12596 * Note that the backend, in this case, will get the 12597 * callback in its context. In other cases it may get 12598 * called in the frontend's interrupt thread context. 12599 */ 12600 io->scsiio.be_move_done(io); 12601 return; 12602 } 12603 12604 /* Don't confuse frontend with zero length data move. */ 12605 if (io->scsiio.kern_data_len == 0) { 12606 io->scsiio.be_move_done(io); 12607 return; 12608 } 12609 12610 fe_datamove = CTL_PORT(io)->fe_datamove; 12611 fe_datamove(io); 12612} 12613 12614static void 12615ctl_send_datamove_done(union ctl_io *io, int have_lock) 12616{ 12617 union ctl_ha_msg msg; 12618#ifdef CTL_TIME_IO 12619 struct bintime cur_bt; 12620#endif 12621 12622 memset(&msg, 0, sizeof(msg)); 12623 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12624 msg.hdr.original_sc = io; 12625 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12626 msg.hdr.nexus = io->io_hdr.nexus; 12627 msg.hdr.status = io->io_hdr.status; 12628 msg.scsi.tag_num = io->scsiio.tag_num; 12629 msg.scsi.tag_type = io->scsiio.tag_type; 12630 msg.scsi.scsi_status = io->scsiio.scsi_status; 12631 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12632 io->scsiio.sense_len); 12633 msg.scsi.sense_len = io->scsiio.sense_len; 12634 msg.scsi.sense_residual = io->scsiio.sense_residual; 12635 msg.scsi.fetd_status = io->io_hdr.port_status; 12636 msg.scsi.residual = io->scsiio.residual; 12637 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12638 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12639 ctl_failover_io(io, /*have_lock*/ have_lock); 12640 return; 12641 } 12642 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12643 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12644 msg.scsi.sense_len, M_WAITOK); 12645 12646#ifdef CTL_TIME_IO 12647 getbinuptime(&cur_bt); 12648 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12649 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12650#endif 12651 io->io_hdr.num_dmas++; 12652} 12653 12654/* 12655 * The DMA to the remote side is done, now we need to tell the other side 12656 * we're done so it can continue with its data movement. 12657 */ 12658static void 12659ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12660{ 12661 union ctl_io *io; 12662 uint32_t i; 12663 12664 io = rq->context; 12665 12666 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12667 printf("%s: ISC DMA write failed with error %d", __func__, 12668 rq->ret); 12669 ctl_set_internal_failure(&io->scsiio, 12670 /*sks_valid*/ 1, 12671 /*retry_count*/ rq->ret); 12672 } 12673 12674 ctl_dt_req_free(rq); 12675 12676 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12677 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12678 free(io->io_hdr.remote_sglist, M_CTL); 12679 io->io_hdr.remote_sglist = NULL; 12680 io->io_hdr.local_sglist = NULL; 12681 12682 /* 12683 * The data is in local and remote memory, so now we need to send 12684 * status (good or back) back to the other side. 12685 */ 12686 ctl_send_datamove_done(io, /*have_lock*/ 0); 12687} 12688 12689/* 12690 * We've moved the data from the host/controller into local memory. Now we 12691 * need to push it over to the remote controller's memory. 12692 */ 12693static int 12694ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12695{ 12696 int retval; 12697 12698 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12699 ctl_datamove_remote_write_cb); 12700 return (retval); 12701} 12702 12703static void 12704ctl_datamove_remote_write(union ctl_io *io) 12705{ 12706 int retval; 12707 void (*fe_datamove)(union ctl_io *io); 12708 12709 /* 12710 * - Get the data from the host/HBA into local memory. 12711 * - DMA memory from the local controller to the remote controller. 12712 * - Send status back to the remote controller. 12713 */ 12714 12715 retval = ctl_datamove_remote_sgl_setup(io); 12716 if (retval != 0) 12717 return; 12718 12719 /* Switch the pointer over so the FETD knows what to do */ 12720 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12721 12722 /* 12723 * Use a custom move done callback, since we need to send completion 12724 * back to the other controller, not to the backend on this side. 12725 */ 12726 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12727 12728 fe_datamove = CTL_PORT(io)->fe_datamove; 12729 fe_datamove(io); 12730} 12731 12732static int 12733ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12734{ 12735#if 0 12736 char str[256]; 12737 char path_str[64]; 12738 struct sbuf sb; 12739#endif 12740 uint32_t i; 12741 12742 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12743 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12744 free(io->io_hdr.remote_sglist, M_CTL); 12745 io->io_hdr.remote_sglist = NULL; 12746 io->io_hdr.local_sglist = NULL; 12747 12748#if 0 12749 scsi_path_string(io, path_str, sizeof(path_str)); 12750 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12751 sbuf_cat(&sb, path_str); 12752 scsi_command_string(&io->scsiio, NULL, &sb); 12753 sbuf_printf(&sb, "\n"); 12754 sbuf_cat(&sb, path_str); 12755 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12756 io->scsiio.tag_num, io->scsiio.tag_type); 12757 sbuf_cat(&sb, path_str); 12758 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12759 io->io_hdr.flags, io->io_hdr.status); 12760 sbuf_finish(&sb); 12761 printk("%s", sbuf_data(&sb)); 12762#endif 12763 12764 12765 /* 12766 * The read is done, now we need to send status (good or bad) back 12767 * to the other side. 12768 */ 12769 ctl_send_datamove_done(io, /*have_lock*/ 0); 12770 12771 return (0); 12772} 12773 12774static void 12775ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12776{ 12777 union ctl_io *io; 12778 void (*fe_datamove)(union ctl_io *io); 12779 12780 io = rq->context; 12781 12782 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12783 printf("%s: ISC DMA read failed with error %d\n", __func__, 12784 rq->ret); 12785 ctl_set_internal_failure(&io->scsiio, 12786 /*sks_valid*/ 1, 12787 /*retry_count*/ rq->ret); 12788 } 12789 12790 ctl_dt_req_free(rq); 12791 12792 /* Switch the pointer over so the FETD knows what to do */ 12793 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12794 12795 /* 12796 * Use a custom move done callback, since we need to send completion 12797 * back to the other controller, not to the backend on this side. 12798 */ 12799 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12800 12801 /* XXX KDM add checks like the ones in ctl_datamove? */ 12802 12803 fe_datamove = CTL_PORT(io)->fe_datamove; 12804 fe_datamove(io); 12805} 12806 12807static int 12808ctl_datamove_remote_sgl_setup(union ctl_io *io) 12809{ 12810 struct ctl_sg_entry *local_sglist; 12811 uint32_t len_to_go; 12812 int retval; 12813 int i; 12814 12815 retval = 0; 12816 local_sglist = io->io_hdr.local_sglist; 12817 len_to_go = io->scsiio.kern_data_len; 12818 12819 /* 12820 * The difficult thing here is that the size of the various 12821 * S/G segments may be different than the size from the 12822 * remote controller. That'll make it harder when DMAing 12823 * the data back to the other side. 12824 */ 12825 for (i = 0; len_to_go > 0; i++) { 12826 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12827 local_sglist[i].addr = 12828 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12829 12830 len_to_go -= local_sglist[i].len; 12831 } 12832 /* 12833 * Reset the number of S/G entries accordingly. The original 12834 * number of S/G entries is available in rem_sg_entries. 12835 */ 12836 io->scsiio.kern_sg_entries = i; 12837 12838#if 0 12839 printf("%s: kern_sg_entries = %d\n", __func__, 12840 io->scsiio.kern_sg_entries); 12841 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12842 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12843 local_sglist[i].addr, local_sglist[i].len); 12844#endif 12845 12846 return (retval); 12847} 12848 12849static int 12850ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12851 ctl_ha_dt_cb callback) 12852{ 12853 struct ctl_ha_dt_req *rq; 12854 struct ctl_sg_entry *remote_sglist, *local_sglist; 12855 uint32_t local_used, remote_used, total_used; 12856 int i, j, isc_ret; 12857 12858 rq = ctl_dt_req_alloc(); 12859 12860 /* 12861 * If we failed to allocate the request, and if the DMA didn't fail 12862 * anyway, set busy status. This is just a resource allocation 12863 * failure. 12864 */ 12865 if ((rq == NULL) 12866 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12867 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12868 ctl_set_busy(&io->scsiio); 12869 12870 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12871 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12872 12873 if (rq != NULL) 12874 ctl_dt_req_free(rq); 12875 12876 /* 12877 * The data move failed. We need to return status back 12878 * to the other controller. No point in trying to DMA 12879 * data to the remote controller. 12880 */ 12881 12882 ctl_send_datamove_done(io, /*have_lock*/ 0); 12883 12884 return (1); 12885 } 12886 12887 local_sglist = io->io_hdr.local_sglist; 12888 remote_sglist = io->io_hdr.remote_sglist; 12889 local_used = 0; 12890 remote_used = 0; 12891 total_used = 0; 12892 12893 /* 12894 * Pull/push the data over the wire from/to the other controller. 12895 * This takes into account the possibility that the local and 12896 * remote sglists may not be identical in terms of the size of 12897 * the elements and the number of elements. 12898 * 12899 * One fundamental assumption here is that the length allocated for 12900 * both the local and remote sglists is identical. Otherwise, we've 12901 * essentially got a coding error of some sort. 12902 */ 12903 isc_ret = CTL_HA_STATUS_SUCCESS; 12904 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12905 uint32_t cur_len; 12906 uint8_t *tmp_ptr; 12907 12908 rq->command = command; 12909 rq->context = io; 12910 12911 /* 12912 * Both pointers should be aligned. But it is possible 12913 * that the allocation length is not. They should both 12914 * also have enough slack left over at the end, though, 12915 * to round up to the next 8 byte boundary. 12916 */ 12917 cur_len = MIN(local_sglist[i].len - local_used, 12918 remote_sglist[j].len - remote_used); 12919 rq->size = cur_len; 12920 12921 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12922 tmp_ptr += local_used; 12923 12924#if 0 12925 /* Use physical addresses when talking to ISC hardware */ 12926 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12927 /* XXX KDM use busdma */ 12928 rq->local = vtophys(tmp_ptr); 12929 } else 12930 rq->local = tmp_ptr; 12931#else 12932 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12933 ("HA does not support BUS_ADDR")); 12934 rq->local = tmp_ptr; 12935#endif 12936 12937 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12938 tmp_ptr += remote_used; 12939 rq->remote = tmp_ptr; 12940 12941 rq->callback = NULL; 12942 12943 local_used += cur_len; 12944 if (local_used >= local_sglist[i].len) { 12945 i++; 12946 local_used = 0; 12947 } 12948 12949 remote_used += cur_len; 12950 if (remote_used >= remote_sglist[j].len) { 12951 j++; 12952 remote_used = 0; 12953 } 12954 total_used += cur_len; 12955 12956 if (total_used >= io->scsiio.kern_data_len) 12957 rq->callback = callback; 12958 12959#if 0 12960 printf("%s: %s: local %p remote %p size %d\n", __func__, 12961 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12962 rq->local, rq->remote, rq->size); 12963#endif 12964 12965 isc_ret = ctl_dt_single(rq); 12966 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12967 break; 12968 } 12969 if (isc_ret != CTL_HA_STATUS_WAIT) { 12970 rq->ret = isc_ret; 12971 callback(rq); 12972 } 12973 12974 return (0); 12975} 12976 12977static void 12978ctl_datamove_remote_read(union ctl_io *io) 12979{ 12980 int retval; 12981 uint32_t i; 12982 12983 /* 12984 * This will send an error to the other controller in the case of a 12985 * failure. 12986 */ 12987 retval = ctl_datamove_remote_sgl_setup(io); 12988 if (retval != 0) 12989 return; 12990 12991 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12992 ctl_datamove_remote_read_cb); 12993 if (retval != 0) { 12994 /* 12995 * Make sure we free memory if there was an error.. The 12996 * ctl_datamove_remote_xfer() function will send the 12997 * datamove done message, or call the callback with an 12998 * error if there is a problem. 12999 */ 13000 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13001 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13002 free(io->io_hdr.remote_sglist, M_CTL); 13003 io->io_hdr.remote_sglist = NULL; 13004 io->io_hdr.local_sglist = NULL; 13005 } 13006} 13007 13008/* 13009 * Process a datamove request from the other controller. This is used for 13010 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13011 * first. Once that is complete, the data gets DMAed into the remote 13012 * controller's memory. For reads, we DMA from the remote controller's 13013 * memory into our memory first, and then move it out to the FETD. 13014 */ 13015static void 13016ctl_datamove_remote(union ctl_io *io) 13017{ 13018 13019 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 13020 13021 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13022 ctl_failover_io(io, /*have_lock*/ 0); 13023 return; 13024 } 13025 13026 /* 13027 * Note that we look for an aborted I/O here, but don't do some of 13028 * the other checks that ctl_datamove() normally does. 13029 * We don't need to run the datamove delay code, since that should 13030 * have been done if need be on the other controller. 13031 */ 13032 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13033 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13034 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13035 io->io_hdr.nexus.targ_port, 13036 io->io_hdr.nexus.targ_lun); 13037 io->io_hdr.port_status = 31338; 13038 ctl_send_datamove_done(io, /*have_lock*/ 0); 13039 return; 13040 } 13041 13042 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13043 ctl_datamove_remote_write(io); 13044 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13045 ctl_datamove_remote_read(io); 13046 else { 13047 io->io_hdr.port_status = 31339; 13048 ctl_send_datamove_done(io, /*have_lock*/ 0); 13049 } 13050} 13051 13052static void 13053ctl_process_done(union ctl_io *io) 13054{ 13055 struct ctl_softc *softc = CTL_SOFTC(io); 13056 struct ctl_lun *lun = CTL_LUN(io); 13057 void (*fe_done)(union ctl_io *io); 13058 union ctl_ha_msg msg; 13059 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13060 13061 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13062 fe_done = softc->ctl_ports[targ_port]->fe_done; 13063 13064#ifdef CTL_TIME_IO 13065 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13066 char str[256]; 13067 char path_str[64]; 13068 struct sbuf sb; 13069 13070 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13071 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13072 13073 sbuf_cat(&sb, path_str); 13074 switch (io->io_hdr.io_type) { 13075 case CTL_IO_SCSI: 13076 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13077 sbuf_printf(&sb, "\n"); 13078 sbuf_cat(&sb, path_str); 13079 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13080 io->scsiio.tag_num, io->scsiio.tag_type); 13081 break; 13082 case CTL_IO_TASK: 13083 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13084 "Tag Type: %d\n", io->taskio.task_action, 13085 io->taskio.tag_num, io->taskio.tag_type); 13086 break; 13087 default: 13088 panic("%s: Invalid CTL I/O type %d\n", 13089 __func__, io->io_hdr.io_type); 13090 } 13091 sbuf_cat(&sb, path_str); 13092 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13093 (intmax_t)time_uptime - io->io_hdr.start_time); 13094 sbuf_finish(&sb); 13095 printf("%s", sbuf_data(&sb)); 13096 } 13097#endif /* CTL_TIME_IO */ 13098 13099 switch (io->io_hdr.io_type) { 13100 case CTL_IO_SCSI: 13101 break; 13102 case CTL_IO_TASK: 13103 if (ctl_debug & CTL_DEBUG_INFO) 13104 ctl_io_error_print(io, NULL); 13105 fe_done(io); 13106 return; 13107 default: 13108 panic("%s: Invalid CTL I/O type %d\n", 13109 __func__, io->io_hdr.io_type); 13110 } 13111 13112 if (lun == NULL) { 13113 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13114 io->io_hdr.nexus.targ_mapped_lun)); 13115 goto bailout; 13116 } 13117 13118 mtx_lock(&lun->lun_lock); 13119 13120 /* 13121 * Check to see if we have any informational exception and status 13122 * of this command can be modified to report it in form of either 13123 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13124 */ 13125 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13126 io->io_hdr.status == CTL_SUCCESS && 13127 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13128 uint8_t mrie = lun->MODE_IE.mrie; 13129 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13130 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13131 if (((mrie == SIEP_MRIE_REC_COND && per) || 13132 mrie == SIEP_MRIE_REC_UNCOND || 13133 mrie == SIEP_MRIE_NO_SENSE) && 13134 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13135 CTL_CMD_FLAG_NO_SENSE) == 0) { 13136 ctl_set_sense(&io->scsiio, 13137 /*current_error*/ 1, 13138 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13139 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13140 /*asc*/ lun->ie_asc, 13141 /*ascq*/ lun->ie_ascq, 13142 SSD_ELEM_NONE); 13143 lun->ie_reported = 1; 13144 } 13145 } else if (lun->ie_reported < 0) 13146 lun->ie_reported = 0; 13147 13148 /* 13149 * Check to see if we have any errors to inject here. We only 13150 * inject errors for commands that don't already have errors set. 13151 */ 13152 if (!STAILQ_EMPTY(&lun->error_list) && 13153 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13154 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13155 ctl_inject_error(lun, io); 13156 13157 /* 13158 * XXX KDM how do we treat commands that aren't completed 13159 * successfully? 13160 * 13161 * XXX KDM should we also track I/O latency? 13162 */ 13163 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13164 io->io_hdr.io_type == CTL_IO_SCSI) { 13165#ifdef CTL_TIME_IO 13166 struct bintime cur_bt; 13167#endif 13168 int type; 13169 13170 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13171 CTL_FLAG_DATA_IN) 13172 type = CTL_STATS_READ; 13173 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13174 CTL_FLAG_DATA_OUT) 13175 type = CTL_STATS_WRITE; 13176 else 13177 type = CTL_STATS_NO_IO; 13178 13179 lun->stats.ports[targ_port].bytes[type] += 13180 io->scsiio.kern_total_len; 13181 lun->stats.ports[targ_port].operations[type]++; 13182#ifdef CTL_TIME_IO 13183 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13184 &io->io_hdr.dma_bt); 13185 getbinuptime(&cur_bt); 13186 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13187 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13188#endif 13189 lun->stats.ports[targ_port].num_dmas[type] += 13190 io->io_hdr.num_dmas; 13191 } 13192 13193 /* 13194 * Remove this from the OOA queue. 13195 */ 13196 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13197#ifdef CTL_TIME_IO 13198 if (TAILQ_EMPTY(&lun->ooa_queue)) 13199 lun->last_busy = getsbinuptime(); 13200#endif 13201 13202 /* 13203 * Run through the blocked queue on this LUN and see if anything 13204 * has become unblocked, now that this transaction is done. 13205 */ 13206 ctl_check_blocked(lun); 13207 13208 /* 13209 * If the LUN has been invalidated, free it if there is nothing 13210 * left on its OOA queue. 13211 */ 13212 if ((lun->flags & CTL_LUN_INVALID) 13213 && TAILQ_EMPTY(&lun->ooa_queue)) { 13214 mtx_unlock(&lun->lun_lock); 13215 mtx_lock(&softc->ctl_lock); 13216 ctl_free_lun(lun); 13217 mtx_unlock(&softc->ctl_lock); 13218 } else 13219 mtx_unlock(&lun->lun_lock); 13220 13221bailout: 13222 13223 /* 13224 * If this command has been aborted, make sure we set the status 13225 * properly. The FETD is responsible for freeing the I/O and doing 13226 * whatever it needs to do to clean up its state. 13227 */ 13228 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13229 ctl_set_task_aborted(&io->scsiio); 13230 13231 /* 13232 * If enabled, print command error status. 13233 */ 13234 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13235 (ctl_debug & CTL_DEBUG_INFO) != 0) 13236 ctl_io_error_print(io, NULL); 13237 13238 /* 13239 * Tell the FETD or the other shelf controller we're done with this 13240 * command. Note that only SCSI commands get to this point. Task 13241 * management commands are completed above. 13242 */ 13243 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13244 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13245 memset(&msg, 0, sizeof(msg)); 13246 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13247 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13248 msg.hdr.nexus = io->io_hdr.nexus; 13249 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13250 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13251 M_WAITOK); 13252 } 13253 13254 fe_done(io); 13255} 13256 13257#ifdef CTL_WITH_CA 13258/* 13259 * Front end should call this if it doesn't do autosense. When the request 13260 * sense comes back in from the initiator, we'll dequeue this and send it. 13261 */ 13262int 13263ctl_queue_sense(union ctl_io *io) 13264{ 13265 struct ctl_softc *softc = CTL_SOFTC(io); 13266 struct ctl_port *port = CTL_PORT(io); 13267 struct ctl_lun *lun; 13268 uint32_t initidx, targ_lun; 13269 13270 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13271 13272 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13273 13274 /* 13275 * LUN lookup will likely move to the ctl_work_thread() once we 13276 * have our new queueing infrastructure (that doesn't put things on 13277 * a per-LUN queue initially). That is so that we can handle 13278 * things like an INQUIRY to a LUN that we don't have enabled. We 13279 * can't deal with that right now. 13280 * If we don't have a LUN for this, just toss the sense information. 13281 */ 13282 mtx_lock(&softc->ctl_lock); 13283 if (targ_lun >= CTL_MAX_LUNS || 13284 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13285 mtx_unlock(&softc->ctl_lock); 13286 goto bailout; 13287 } 13288 mtx_lock(&lun->lun_lock); 13289 mtx_unlock(&softc->ctl_lock); 13290 13291 /* 13292 * Already have CA set for this LUN...toss the sense information. 13293 */ 13294 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13295 if (ctl_is_set(lun->have_ca, initidx)) { 13296 mtx_unlock(&lun->lun_lock); 13297 goto bailout; 13298 } 13299 13300 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13301 MIN(sizeof(lun->pending_sense[initidx]), 13302 sizeof(io->scsiio.sense_data))); 13303 ctl_set_mask(lun->have_ca, initidx); 13304 mtx_unlock(&lun->lun_lock); 13305 13306bailout: 13307 ctl_free_io(io); 13308 return (CTL_RETVAL_COMPLETE); 13309} 13310#endif 13311 13312/* 13313 * Primary command inlet from frontend ports. All SCSI and task I/O 13314 * requests must go through this function. 13315 */ 13316int 13317ctl_queue(union ctl_io *io) 13318{ 13319 struct ctl_port *port = CTL_PORT(io); 13320 13321 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13322 13323#ifdef CTL_TIME_IO 13324 io->io_hdr.start_time = time_uptime; 13325 getbinuptime(&io->io_hdr.start_bt); 13326#endif /* CTL_TIME_IO */ 13327 13328 /* Map FE-specific LUN ID into global one. */ 13329 io->io_hdr.nexus.targ_mapped_lun = 13330 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13331 13332 switch (io->io_hdr.io_type) { 13333 case CTL_IO_SCSI: 13334 case CTL_IO_TASK: 13335 if (ctl_debug & CTL_DEBUG_CDB) 13336 ctl_io_print(io); 13337 ctl_enqueue_incoming(io); 13338 break; 13339 default: 13340 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13341 return (EINVAL); 13342 } 13343 13344 return (CTL_RETVAL_COMPLETE); 13345} 13346 13347#ifdef CTL_IO_DELAY 13348static void 13349ctl_done_timer_wakeup(void *arg) 13350{ 13351 union ctl_io *io; 13352 13353 io = (union ctl_io *)arg; 13354 ctl_done(io); 13355} 13356#endif /* CTL_IO_DELAY */ 13357 13358void 13359ctl_serseq_done(union ctl_io *io) 13360{ 13361 struct ctl_lun *lun = CTL_LUN(io);; 13362 13363 if (lun->be_lun == NULL || 13364 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13365 return; 13366 mtx_lock(&lun->lun_lock); 13367 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13368 ctl_check_blocked(lun); 13369 mtx_unlock(&lun->lun_lock); 13370} 13371 13372void 13373ctl_done(union ctl_io *io) 13374{ 13375 13376 /* 13377 * Enable this to catch duplicate completion issues. 13378 */ 13379#if 0 13380 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13381 printf("%s: type %d msg %d cdb %x iptl: " 13382 "%u:%u:%u tag 0x%04x " 13383 "flag %#x status %x\n", 13384 __func__, 13385 io->io_hdr.io_type, 13386 io->io_hdr.msg_type, 13387 io->scsiio.cdb[0], 13388 io->io_hdr.nexus.initid, 13389 io->io_hdr.nexus.targ_port, 13390 io->io_hdr.nexus.targ_lun, 13391 (io->io_hdr.io_type == 13392 CTL_IO_TASK) ? 13393 io->taskio.tag_num : 13394 io->scsiio.tag_num, 13395 io->io_hdr.flags, 13396 io->io_hdr.status); 13397 } else 13398 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13399#endif 13400 13401 /* 13402 * This is an internal copy of an I/O, and should not go through 13403 * the normal done processing logic. 13404 */ 13405 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13406 return; 13407 13408#ifdef CTL_IO_DELAY 13409 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13410 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13411 } else { 13412 struct ctl_lun *lun = CTL_LUN(io); 13413 13414 if ((lun != NULL) 13415 && (lun->delay_info.done_delay > 0)) { 13416 13417 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13418 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13419 callout_reset(&io->io_hdr.delay_callout, 13420 lun->delay_info.done_delay * hz, 13421 ctl_done_timer_wakeup, io); 13422 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13423 lun->delay_info.done_delay = 0; 13424 return; 13425 } 13426 } 13427#endif /* CTL_IO_DELAY */ 13428 13429 ctl_enqueue_done(io); 13430} 13431 13432static void 13433ctl_work_thread(void *arg) 13434{ 13435 struct ctl_thread *thr = (struct ctl_thread *)arg; 13436 struct ctl_softc *softc = thr->ctl_softc; 13437 union ctl_io *io; 13438 int retval; 13439 13440 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13441 13442 for (;;) { 13443 /* 13444 * We handle the queues in this order: 13445 * - ISC 13446 * - done queue (to free up resources, unblock other commands) 13447 * - RtR queue 13448 * - incoming queue 13449 * 13450 * If those queues are empty, we break out of the loop and 13451 * go to sleep. 13452 */ 13453 mtx_lock(&thr->queue_lock); 13454 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13455 if (io != NULL) { 13456 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13457 mtx_unlock(&thr->queue_lock); 13458 ctl_handle_isc(io); 13459 continue; 13460 } 13461 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13462 if (io != NULL) { 13463 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13464 /* clear any blocked commands, call fe_done */ 13465 mtx_unlock(&thr->queue_lock); 13466 ctl_process_done(io); 13467 continue; 13468 } 13469 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13470 if (io != NULL) { 13471 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13472 mtx_unlock(&thr->queue_lock); 13473 if (io->io_hdr.io_type == CTL_IO_TASK) 13474 ctl_run_task(io); 13475 else 13476 ctl_scsiio_precheck(softc, &io->scsiio); 13477 continue; 13478 } 13479 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13480 if (io != NULL) { 13481 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13482 mtx_unlock(&thr->queue_lock); 13483 retval = ctl_scsiio(&io->scsiio); 13484 if (retval != CTL_RETVAL_COMPLETE) 13485 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13486 continue; 13487 } 13488 13489 /* Sleep until we have something to do. */ 13490 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13491 } 13492} 13493 13494static void 13495ctl_lun_thread(void *arg) 13496{ 13497 struct ctl_softc *softc = (struct ctl_softc *)arg; 13498 struct ctl_be_lun *be_lun; 13499 13500 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13501 13502 for (;;) { 13503 mtx_lock(&softc->ctl_lock); 13504 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13505 if (be_lun != NULL) { 13506 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13507 mtx_unlock(&softc->ctl_lock); 13508 ctl_create_lun(be_lun); 13509 continue; 13510 } 13511 13512 /* Sleep until we have something to do. */ 13513 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13514 PDROP | PRIBIO, "-", 0); 13515 } 13516} 13517 13518static void 13519ctl_thresh_thread(void *arg) 13520{ 13521 struct ctl_softc *softc = (struct ctl_softc *)arg; 13522 struct ctl_lun *lun; 13523 struct ctl_logical_block_provisioning_page *page; 13524 const char *attr; 13525 union ctl_ha_msg msg; 13526 uint64_t thres, val; 13527 int i, e, set; 13528 13529 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13530 13531 for (;;) { 13532 mtx_lock(&softc->ctl_lock); 13533 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13534 if ((lun->flags & CTL_LUN_DISABLED) || 13535 (lun->flags & CTL_LUN_NO_MEDIA) || 13536 lun->backend->lun_attr == NULL) 13537 continue; 13538 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13539 softc->ha_mode == CTL_HA_MODE_XFER) 13540 continue; 13541 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13542 continue; 13543 e = 0; 13544 page = &lun->MODE_LBP; 13545 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13546 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13547 continue; 13548 thres = scsi_4btoul(page->descr[i].count); 13549 thres <<= CTL_LBP_EXPONENT; 13550 switch (page->descr[i].resource) { 13551 case 0x01: 13552 attr = "blocksavail"; 13553 break; 13554 case 0x02: 13555 attr = "blocksused"; 13556 break; 13557 case 0xf1: 13558 attr = "poolblocksavail"; 13559 break; 13560 case 0xf2: 13561 attr = "poolblocksused"; 13562 break; 13563 default: 13564 continue; 13565 } 13566 mtx_unlock(&softc->ctl_lock); // XXX 13567 val = lun->backend->lun_attr( 13568 lun->be_lun->be_lun, attr); 13569 mtx_lock(&softc->ctl_lock); 13570 if (val == UINT64_MAX) 13571 continue; 13572 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13573 == SLBPPD_ARMING_INC) 13574 e = (val >= thres); 13575 else 13576 e = (val <= thres); 13577 if (e) 13578 break; 13579 } 13580 mtx_lock(&lun->lun_lock); 13581 if (e) { 13582 scsi_u64to8b((uint8_t *)&page->descr[i] - 13583 (uint8_t *)page, lun->ua_tpt_info); 13584 if (lun->lasttpt == 0 || 13585 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13586 lun->lasttpt = time_uptime; 13587 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13588 set = 1; 13589 } else 13590 set = 0; 13591 } else { 13592 lun->lasttpt = 0; 13593 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13594 set = -1; 13595 } 13596 mtx_unlock(&lun->lun_lock); 13597 if (set != 0 && 13598 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13599 /* Send msg to other side. */ 13600 bzero(&msg.ua, sizeof(msg.ua)); 13601 msg.hdr.msg_type = CTL_MSG_UA; 13602 msg.hdr.nexus.initid = -1; 13603 msg.hdr.nexus.targ_port = -1; 13604 msg.hdr.nexus.targ_lun = lun->lun; 13605 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13606 msg.ua.ua_all = 1; 13607 msg.ua.ua_set = (set > 0); 13608 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13609 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13610 mtx_unlock(&softc->ctl_lock); // XXX 13611 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13612 sizeof(msg.ua), M_WAITOK); 13613 mtx_lock(&softc->ctl_lock); 13614 } 13615 } 13616 mtx_unlock(&softc->ctl_lock); 13617 pause("-", CTL_LBP_PERIOD * hz); 13618 } 13619} 13620 13621static void 13622ctl_enqueue_incoming(union ctl_io *io) 13623{ 13624 struct ctl_softc *softc = CTL_SOFTC(io); 13625 struct ctl_thread *thr; 13626 u_int idx; 13627 13628 idx = (io->io_hdr.nexus.targ_port * 127 + 13629 io->io_hdr.nexus.initid) % worker_threads; 13630 thr = &softc->threads[idx]; 13631 mtx_lock(&thr->queue_lock); 13632 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13633 mtx_unlock(&thr->queue_lock); 13634 wakeup(thr); 13635} 13636 13637static void 13638ctl_enqueue_rtr(union ctl_io *io) 13639{ 13640 struct ctl_softc *softc = CTL_SOFTC(io); 13641 struct ctl_thread *thr; 13642 13643 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13644 mtx_lock(&thr->queue_lock); 13645 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13646 mtx_unlock(&thr->queue_lock); 13647 wakeup(thr); 13648} 13649 13650static void 13651ctl_enqueue_done(union ctl_io *io) 13652{ 13653 struct ctl_softc *softc = CTL_SOFTC(io); 13654 struct ctl_thread *thr; 13655 13656 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13657 mtx_lock(&thr->queue_lock); 13658 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13659 mtx_unlock(&thr->queue_lock); 13660 wakeup(thr); 13661} 13662 13663static void 13664ctl_enqueue_isc(union ctl_io *io) 13665{ 13666 struct ctl_softc *softc = CTL_SOFTC(io); 13667 struct ctl_thread *thr; 13668 13669 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13670 mtx_lock(&thr->queue_lock); 13671 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13672 mtx_unlock(&thr->queue_lock); 13673 wakeup(thr); 13674} 13675 13676/* 13677 * vim: ts=8 13678 */ 13679