ctl.c revision 312836
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 312836 2017-01-26 20:51:18Z 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 4589 /* 4590 * Initialize the mode and log page index. 4591 */ 4592 ctl_init_page_index(lun); 4593 ctl_init_log_page_index(lun); 4594 4595 /* 4596 * Now, before we insert this lun on the lun list, set the lun 4597 * inventory changed UA for all other luns. 4598 */ 4599 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4600 mtx_lock(&nlun->lun_lock); 4601 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4602 mtx_unlock(&nlun->lun_lock); 4603 } 4604 4605 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4606 4607 ctl_softc->ctl_luns[lun_number] = lun; 4608 4609 ctl_softc->num_luns++; 4610 4611 /* Setup statistics gathering */ 4612 lun->stats.device_type = be_lun->lun_type; 4613 lun->stats.lun_number = lun_number; 4614 lun->stats.blocksize = be_lun->blocksize; 4615 if (be_lun->blocksize == 0) 4616 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4617 for (i = 0;i < CTL_MAX_PORTS;i++) 4618 lun->stats.ports[i].targ_port = i; 4619 4620 mtx_unlock(&ctl_softc->ctl_lock); 4621 4622 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4623 return (0); 4624} 4625 4626/* 4627 * Delete a LUN. 4628 * Assumptions: 4629 * - LUN has already been marked invalid and any pending I/O has been taken 4630 * care of. 4631 */ 4632static int 4633ctl_free_lun(struct ctl_lun *lun) 4634{ 4635 struct ctl_softc *softc = lun->ctl_softc; 4636 struct ctl_lun *nlun; 4637 int i; 4638 4639 mtx_assert(&softc->ctl_lock, MA_OWNED); 4640 4641 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4642 4643 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4644 4645 softc->ctl_luns[lun->lun] = NULL; 4646 4647 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4648 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4649 4650 softc->num_luns--; 4651 4652 /* 4653 * Tell the backend to free resources, if this LUN has a backend. 4654 */ 4655 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4656 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4657 4658 lun->ie_reportcnt = UINT32_MAX; 4659 callout_drain(&lun->ie_callout); 4660 4661 ctl_tpc_lun_shutdown(lun); 4662 mtx_destroy(&lun->lun_lock); 4663 free(lun->lun_devid, M_CTL); 4664 for (i = 0; i < CTL_MAX_PORTS; i++) 4665 free(lun->pending_ua[i], M_CTL); 4666 for (i = 0; i < CTL_MAX_PORTS; i++) 4667 free(lun->pr_keys[i], M_CTL); 4668 free(lun->write_buffer, M_CTL); 4669 if (lun->flags & CTL_LUN_MALLOCED) 4670 free(lun, M_CTL); 4671 4672 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4673 mtx_lock(&nlun->lun_lock); 4674 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4675 mtx_unlock(&nlun->lun_lock); 4676 } 4677 4678 return (0); 4679} 4680 4681static void 4682ctl_create_lun(struct ctl_be_lun *be_lun) 4683{ 4684 4685 /* 4686 * ctl_alloc_lun() should handle all potential failure cases. 4687 */ 4688 ctl_alloc_lun(control_softc, NULL, be_lun); 4689} 4690 4691int 4692ctl_add_lun(struct ctl_be_lun *be_lun) 4693{ 4694 struct ctl_softc *softc = control_softc; 4695 4696 mtx_lock(&softc->ctl_lock); 4697 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4698 mtx_unlock(&softc->ctl_lock); 4699 wakeup(&softc->pending_lun_queue); 4700 4701 return (0); 4702} 4703 4704int 4705ctl_enable_lun(struct ctl_be_lun *be_lun) 4706{ 4707 struct ctl_softc *softc; 4708 struct ctl_port *port, *nport; 4709 struct ctl_lun *lun; 4710 int retval; 4711 4712 lun = (struct ctl_lun *)be_lun->ctl_lun; 4713 softc = lun->ctl_softc; 4714 4715 mtx_lock(&softc->ctl_lock); 4716 mtx_lock(&lun->lun_lock); 4717 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4718 /* 4719 * eh? Why did we get called if the LUN is already 4720 * enabled? 4721 */ 4722 mtx_unlock(&lun->lun_lock); 4723 mtx_unlock(&softc->ctl_lock); 4724 return (0); 4725 } 4726 lun->flags &= ~CTL_LUN_DISABLED; 4727 mtx_unlock(&lun->lun_lock); 4728 4729 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4730 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4731 port->lun_map != NULL || port->lun_enable == NULL) 4732 continue; 4733 4734 /* 4735 * Drop the lock while we call the FETD's enable routine. 4736 * This can lead to a callback into CTL (at least in the 4737 * case of the internal initiator frontend. 4738 */ 4739 mtx_unlock(&softc->ctl_lock); 4740 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4741 mtx_lock(&softc->ctl_lock); 4742 if (retval != 0) { 4743 printf("%s: FETD %s port %d returned error " 4744 "%d for lun_enable on lun %jd\n", 4745 __func__, port->port_name, port->targ_port, 4746 retval, (intmax_t)lun->lun); 4747 } 4748 } 4749 4750 mtx_unlock(&softc->ctl_lock); 4751 ctl_isc_announce_lun(lun); 4752 4753 return (0); 4754} 4755 4756int 4757ctl_disable_lun(struct ctl_be_lun *be_lun) 4758{ 4759 struct ctl_softc *softc; 4760 struct ctl_port *port; 4761 struct ctl_lun *lun; 4762 int retval; 4763 4764 lun = (struct ctl_lun *)be_lun->ctl_lun; 4765 softc = lun->ctl_softc; 4766 4767 mtx_lock(&softc->ctl_lock); 4768 mtx_lock(&lun->lun_lock); 4769 if (lun->flags & CTL_LUN_DISABLED) { 4770 mtx_unlock(&lun->lun_lock); 4771 mtx_unlock(&softc->ctl_lock); 4772 return (0); 4773 } 4774 lun->flags |= CTL_LUN_DISABLED; 4775 mtx_unlock(&lun->lun_lock); 4776 4777 STAILQ_FOREACH(port, &softc->port_list, links) { 4778 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4779 port->lun_map != NULL || port->lun_disable == NULL) 4780 continue; 4781 4782 /* 4783 * Drop the lock before we call the frontend's disable 4784 * routine, to avoid lock order reversals. 4785 * 4786 * XXX KDM what happens if the frontend list changes while 4787 * we're traversing it? It's unlikely, but should be handled. 4788 */ 4789 mtx_unlock(&softc->ctl_lock); 4790 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4791 mtx_lock(&softc->ctl_lock); 4792 if (retval != 0) { 4793 printf("%s: FETD %s port %d returned error " 4794 "%d for lun_disable on lun %jd\n", 4795 __func__, port->port_name, port->targ_port, 4796 retval, (intmax_t)lun->lun); 4797 } 4798 } 4799 4800 mtx_unlock(&softc->ctl_lock); 4801 ctl_isc_announce_lun(lun); 4802 4803 return (0); 4804} 4805 4806int 4807ctl_start_lun(struct ctl_be_lun *be_lun) 4808{ 4809 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4810 4811 mtx_lock(&lun->lun_lock); 4812 lun->flags &= ~CTL_LUN_STOPPED; 4813 mtx_unlock(&lun->lun_lock); 4814 return (0); 4815} 4816 4817int 4818ctl_stop_lun(struct ctl_be_lun *be_lun) 4819{ 4820 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4821 4822 mtx_lock(&lun->lun_lock); 4823 lun->flags |= CTL_LUN_STOPPED; 4824 mtx_unlock(&lun->lun_lock); 4825 return (0); 4826} 4827 4828int 4829ctl_lun_no_media(struct ctl_be_lun *be_lun) 4830{ 4831 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4832 4833 mtx_lock(&lun->lun_lock); 4834 lun->flags |= CTL_LUN_NO_MEDIA; 4835 mtx_unlock(&lun->lun_lock); 4836 return (0); 4837} 4838 4839int 4840ctl_lun_has_media(struct ctl_be_lun *be_lun) 4841{ 4842 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4843 union ctl_ha_msg msg; 4844 4845 mtx_lock(&lun->lun_lock); 4846 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4847 if (lun->flags & CTL_LUN_REMOVABLE) 4848 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4849 mtx_unlock(&lun->lun_lock); 4850 if ((lun->flags & CTL_LUN_REMOVABLE) && 4851 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4852 bzero(&msg.ua, sizeof(msg.ua)); 4853 msg.hdr.msg_type = CTL_MSG_UA; 4854 msg.hdr.nexus.initid = -1; 4855 msg.hdr.nexus.targ_port = -1; 4856 msg.hdr.nexus.targ_lun = lun->lun; 4857 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4858 msg.ua.ua_all = 1; 4859 msg.ua.ua_set = 1; 4860 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4861 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4862 M_WAITOK); 4863 } 4864 return (0); 4865} 4866 4867int 4868ctl_lun_ejected(struct ctl_be_lun *be_lun) 4869{ 4870 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4871 4872 mtx_lock(&lun->lun_lock); 4873 lun->flags |= CTL_LUN_EJECTED; 4874 mtx_unlock(&lun->lun_lock); 4875 return (0); 4876} 4877 4878int 4879ctl_lun_primary(struct ctl_be_lun *be_lun) 4880{ 4881 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4882 4883 mtx_lock(&lun->lun_lock); 4884 lun->flags |= CTL_LUN_PRIMARY_SC; 4885 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4886 mtx_unlock(&lun->lun_lock); 4887 ctl_isc_announce_lun(lun); 4888 return (0); 4889} 4890 4891int 4892ctl_lun_secondary(struct ctl_be_lun *be_lun) 4893{ 4894 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4895 4896 mtx_lock(&lun->lun_lock); 4897 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4898 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4899 mtx_unlock(&lun->lun_lock); 4900 ctl_isc_announce_lun(lun); 4901 return (0); 4902} 4903 4904int 4905ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4906{ 4907 struct ctl_softc *softc; 4908 struct ctl_lun *lun; 4909 4910 lun = (struct ctl_lun *)be_lun->ctl_lun; 4911 softc = lun->ctl_softc; 4912 4913 mtx_lock(&lun->lun_lock); 4914 4915 /* 4916 * The LUN needs to be disabled before it can be marked invalid. 4917 */ 4918 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4919 mtx_unlock(&lun->lun_lock); 4920 return (-1); 4921 } 4922 /* 4923 * Mark the LUN invalid. 4924 */ 4925 lun->flags |= CTL_LUN_INVALID; 4926 4927 /* 4928 * If there is nothing in the OOA queue, go ahead and free the LUN. 4929 * If we have something in the OOA queue, we'll free it when the 4930 * last I/O completes. 4931 */ 4932 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4933 mtx_unlock(&lun->lun_lock); 4934 mtx_lock(&softc->ctl_lock); 4935 ctl_free_lun(lun); 4936 mtx_unlock(&softc->ctl_lock); 4937 } else 4938 mtx_unlock(&lun->lun_lock); 4939 4940 return (0); 4941} 4942 4943void 4944ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4945{ 4946 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4947 union ctl_ha_msg msg; 4948 4949 mtx_lock(&lun->lun_lock); 4950 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4951 mtx_unlock(&lun->lun_lock); 4952 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4953 /* Send msg to other side. */ 4954 bzero(&msg.ua, sizeof(msg.ua)); 4955 msg.hdr.msg_type = CTL_MSG_UA; 4956 msg.hdr.nexus.initid = -1; 4957 msg.hdr.nexus.targ_port = -1; 4958 msg.hdr.nexus.targ_lun = lun->lun; 4959 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4960 msg.ua.ua_all = 1; 4961 msg.ua.ua_set = 1; 4962 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4963 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4964 M_WAITOK); 4965 } 4966} 4967 4968/* 4969 * Backend "memory move is complete" callback for requests that never 4970 * make it down to say RAIDCore's configuration code. 4971 */ 4972int 4973ctl_config_move_done(union ctl_io *io) 4974{ 4975 int retval; 4976 4977 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4978 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4979 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4980 4981 if ((io->io_hdr.port_status != 0) && 4982 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4983 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4984 /* 4985 * For hardware error sense keys, the sense key 4986 * specific value is defined to be a retry count, 4987 * but we use it to pass back an internal FETD 4988 * error code. XXX KDM Hopefully the FETD is only 4989 * using 16 bits for an error code, since that's 4990 * all the space we have in the sks field. 4991 */ 4992 ctl_set_internal_failure(&io->scsiio, 4993 /*sks_valid*/ 1, 4994 /*retry_count*/ 4995 io->io_hdr.port_status); 4996 } 4997 4998 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4999 ctl_data_print(io); 5000 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5001 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5002 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5003 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5004 /* 5005 * XXX KDM just assuming a single pointer here, and not a 5006 * S/G list. If we start using S/G lists for config data, 5007 * we'll need to know how to clean them up here as well. 5008 */ 5009 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5010 free(io->scsiio.kern_data_ptr, M_CTL); 5011 ctl_done(io); 5012 retval = CTL_RETVAL_COMPLETE; 5013 } else { 5014 /* 5015 * XXX KDM now we need to continue data movement. Some 5016 * options: 5017 * - call ctl_scsiio() again? We don't do this for data 5018 * writes, because for those at least we know ahead of 5019 * time where the write will go and how long it is. For 5020 * config writes, though, that information is largely 5021 * contained within the write itself, thus we need to 5022 * parse out the data again. 5023 * 5024 * - Call some other function once the data is in? 5025 */ 5026 5027 /* 5028 * XXX KDM call ctl_scsiio() again for now, and check flag 5029 * bits to see whether we're allocated or not. 5030 */ 5031 retval = ctl_scsiio(&io->scsiio); 5032 } 5033 return (retval); 5034} 5035 5036/* 5037 * This gets called by a backend driver when it is done with a 5038 * data_submit method. 5039 */ 5040void 5041ctl_data_submit_done(union ctl_io *io) 5042{ 5043 /* 5044 * If the IO_CONT flag is set, we need to call the supplied 5045 * function to continue processing the I/O, instead of completing 5046 * the I/O just yet. 5047 * 5048 * If there is an error, though, we don't want to keep processing. 5049 * Instead, just send status back to the initiator. 5050 */ 5051 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5052 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5053 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5054 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5055 io->scsiio.io_cont(io); 5056 return; 5057 } 5058 ctl_done(io); 5059} 5060 5061/* 5062 * This gets called by a backend driver when it is done with a 5063 * configuration write. 5064 */ 5065void 5066ctl_config_write_done(union ctl_io *io) 5067{ 5068 uint8_t *buf; 5069 5070 /* 5071 * If the IO_CONT flag is set, we need to call the supplied 5072 * function to continue processing the I/O, instead of completing 5073 * the I/O just yet. 5074 * 5075 * If there is an error, though, we don't want to keep processing. 5076 * Instead, just send status back to the initiator. 5077 */ 5078 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5079 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5080 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5081 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5082 io->scsiio.io_cont(io); 5083 return; 5084 } 5085 /* 5086 * Since a configuration write can be done for commands that actually 5087 * have data allocated, like write buffer, and commands that have 5088 * no data, like start/stop unit, we need to check here. 5089 */ 5090 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5091 buf = io->scsiio.kern_data_ptr; 5092 else 5093 buf = NULL; 5094 ctl_done(io); 5095 if (buf) 5096 free(buf, M_CTL); 5097} 5098 5099void 5100ctl_config_read_done(union ctl_io *io) 5101{ 5102 uint8_t *buf; 5103 5104 /* 5105 * If there is some error -- we are done, skip data transfer. 5106 */ 5107 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5108 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5109 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5110 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5111 buf = io->scsiio.kern_data_ptr; 5112 else 5113 buf = NULL; 5114 ctl_done(io); 5115 if (buf) 5116 free(buf, M_CTL); 5117 return; 5118 } 5119 5120 /* 5121 * If the IO_CONT flag is set, we need to call the supplied 5122 * function to continue processing the I/O, instead of completing 5123 * the I/O just yet. 5124 */ 5125 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5126 io->scsiio.io_cont(io); 5127 return; 5128 } 5129 5130 ctl_datamove(io); 5131} 5132 5133/* 5134 * SCSI release command. 5135 */ 5136int 5137ctl_scsi_release(struct ctl_scsiio *ctsio) 5138{ 5139 struct ctl_lun *lun = CTL_LUN(ctsio); 5140 uint32_t residx; 5141 5142 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5143 5144 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5145 5146 /* 5147 * XXX KDM right now, we only support LUN reservation. We don't 5148 * support 3rd party reservations, or extent reservations, which 5149 * might actually need the parameter list. If we've gotten this 5150 * far, we've got a LUN reservation. Anything else got kicked out 5151 * above. So, according to SPC, ignore the length. 5152 */ 5153 5154 mtx_lock(&lun->lun_lock); 5155 5156 /* 5157 * According to SPC, it is not an error for an intiator to attempt 5158 * to release a reservation on a LUN that isn't reserved, or that 5159 * is reserved by another initiator. The reservation can only be 5160 * released, though, by the initiator who made it or by one of 5161 * several reset type events. 5162 */ 5163 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5164 lun->flags &= ~CTL_LUN_RESERVED; 5165 5166 mtx_unlock(&lun->lun_lock); 5167 5168 ctl_set_success(ctsio); 5169 ctl_done((union ctl_io *)ctsio); 5170 return (CTL_RETVAL_COMPLETE); 5171} 5172 5173int 5174ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5175{ 5176 struct ctl_lun *lun = CTL_LUN(ctsio); 5177 uint32_t residx; 5178 5179 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5180 5181 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5182 5183 /* 5184 * XXX KDM right now, we only support LUN reservation. We don't 5185 * support 3rd party reservations, or extent reservations, which 5186 * might actually need the parameter list. If we've gotten this 5187 * far, we've got a LUN reservation. Anything else got kicked out 5188 * above. So, according to SPC, ignore the length. 5189 */ 5190 5191 mtx_lock(&lun->lun_lock); 5192 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5193 ctl_set_reservation_conflict(ctsio); 5194 goto bailout; 5195 } 5196 5197 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5198 if (lun->flags & CTL_LUN_PR_RESERVED) { 5199 ctl_set_success(ctsio); 5200 goto bailout; 5201 } 5202 5203 lun->flags |= CTL_LUN_RESERVED; 5204 lun->res_idx = residx; 5205 ctl_set_success(ctsio); 5206 5207bailout: 5208 mtx_unlock(&lun->lun_lock); 5209 ctl_done((union ctl_io *)ctsio); 5210 return (CTL_RETVAL_COMPLETE); 5211} 5212 5213int 5214ctl_start_stop(struct ctl_scsiio *ctsio) 5215{ 5216 struct ctl_lun *lun = CTL_LUN(ctsio); 5217 struct scsi_start_stop_unit *cdb; 5218 int retval; 5219 5220 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5221 5222 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5223 5224 if ((cdb->how & SSS_PC_MASK) == 0) { 5225 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5226 (cdb->how & SSS_START) == 0) { 5227 uint32_t residx; 5228 5229 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5230 if (ctl_get_prkey(lun, residx) == 0 || 5231 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5232 5233 ctl_set_reservation_conflict(ctsio); 5234 ctl_done((union ctl_io *)ctsio); 5235 return (CTL_RETVAL_COMPLETE); 5236 } 5237 } 5238 5239 if ((cdb->how & SSS_LOEJ) && 5240 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5241 ctl_set_invalid_field(ctsio, 5242 /*sks_valid*/ 1, 5243 /*command*/ 1, 5244 /*field*/ 4, 5245 /*bit_valid*/ 1, 5246 /*bit*/ 1); 5247 ctl_done((union ctl_io *)ctsio); 5248 return (CTL_RETVAL_COMPLETE); 5249 } 5250 5251 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5252 lun->prevent_count > 0) { 5253 /* "Medium removal prevented" */ 5254 ctl_set_sense(ctsio, /*current_error*/ 1, 5255 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5256 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5257 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5258 ctl_done((union ctl_io *)ctsio); 5259 return (CTL_RETVAL_COMPLETE); 5260 } 5261 } 5262 5263 retval = lun->backend->config_write((union ctl_io *)ctsio); 5264 return (retval); 5265} 5266 5267int 5268ctl_prevent_allow(struct ctl_scsiio *ctsio) 5269{ 5270 struct ctl_lun *lun = CTL_LUN(ctsio); 5271 struct scsi_prevent *cdb; 5272 int retval; 5273 uint32_t initidx; 5274 5275 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5276 5277 cdb = (struct scsi_prevent *)ctsio->cdb; 5278 5279 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5280 ctl_set_invalid_opcode(ctsio); 5281 ctl_done((union ctl_io *)ctsio); 5282 return (CTL_RETVAL_COMPLETE); 5283 } 5284 5285 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5286 mtx_lock(&lun->lun_lock); 5287 if ((cdb->how & PR_PREVENT) && 5288 ctl_is_set(lun->prevent, initidx) == 0) { 5289 ctl_set_mask(lun->prevent, initidx); 5290 lun->prevent_count++; 5291 } else if ((cdb->how & PR_PREVENT) == 0 && 5292 ctl_is_set(lun->prevent, initidx)) { 5293 ctl_clear_mask(lun->prevent, initidx); 5294 lun->prevent_count--; 5295 } 5296 mtx_unlock(&lun->lun_lock); 5297 retval = lun->backend->config_write((union ctl_io *)ctsio); 5298 return (retval); 5299} 5300 5301/* 5302 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5303 * we don't really do anything with the LBA and length fields if the user 5304 * passes them in. Instead we'll just flush out the cache for the entire 5305 * LUN. 5306 */ 5307int 5308ctl_sync_cache(struct ctl_scsiio *ctsio) 5309{ 5310 struct ctl_lun *lun = CTL_LUN(ctsio); 5311 struct ctl_lba_len_flags *lbalen; 5312 uint64_t starting_lba; 5313 uint32_t block_count; 5314 int retval; 5315 uint8_t byte2; 5316 5317 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5318 5319 retval = 0; 5320 5321 switch (ctsio->cdb[0]) { 5322 case SYNCHRONIZE_CACHE: { 5323 struct scsi_sync_cache *cdb; 5324 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5325 5326 starting_lba = scsi_4btoul(cdb->begin_lba); 5327 block_count = scsi_2btoul(cdb->lb_count); 5328 byte2 = cdb->byte2; 5329 break; 5330 } 5331 case SYNCHRONIZE_CACHE_16: { 5332 struct scsi_sync_cache_16 *cdb; 5333 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5334 5335 starting_lba = scsi_8btou64(cdb->begin_lba); 5336 block_count = scsi_4btoul(cdb->lb_count); 5337 byte2 = cdb->byte2; 5338 break; 5339 } 5340 default: 5341 ctl_set_invalid_opcode(ctsio); 5342 ctl_done((union ctl_io *)ctsio); 5343 goto bailout; 5344 break; /* NOTREACHED */ 5345 } 5346 5347 /* 5348 * We check the LBA and length, but don't do anything with them. 5349 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5350 * get flushed. This check will just help satisfy anyone who wants 5351 * to see an error for an out of range LBA. 5352 */ 5353 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5354 ctl_set_lba_out_of_range(ctsio, 5355 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5356 ctl_done((union ctl_io *)ctsio); 5357 goto bailout; 5358 } 5359 5360 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5361 lbalen->lba = starting_lba; 5362 lbalen->len = block_count; 5363 lbalen->flags = byte2; 5364 retval = lun->backend->config_write((union ctl_io *)ctsio); 5365 5366bailout: 5367 return (retval); 5368} 5369 5370int 5371ctl_format(struct ctl_scsiio *ctsio) 5372{ 5373 struct scsi_format *cdb; 5374 int length, defect_list_len; 5375 5376 CTL_DEBUG_PRINT(("ctl_format\n")); 5377 5378 cdb = (struct scsi_format *)ctsio->cdb; 5379 5380 length = 0; 5381 if (cdb->byte2 & SF_FMTDATA) { 5382 if (cdb->byte2 & SF_LONGLIST) 5383 length = sizeof(struct scsi_format_header_long); 5384 else 5385 length = sizeof(struct scsi_format_header_short); 5386 } 5387 5388 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5389 && (length > 0)) { 5390 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5391 ctsio->kern_data_len = length; 5392 ctsio->kern_total_len = length; 5393 ctsio->kern_data_resid = 0; 5394 ctsio->kern_rel_offset = 0; 5395 ctsio->kern_sg_entries = 0; 5396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5397 ctsio->be_move_done = ctl_config_move_done; 5398 ctl_datamove((union ctl_io *)ctsio); 5399 5400 return (CTL_RETVAL_COMPLETE); 5401 } 5402 5403 defect_list_len = 0; 5404 5405 if (cdb->byte2 & SF_FMTDATA) { 5406 if (cdb->byte2 & SF_LONGLIST) { 5407 struct scsi_format_header_long *header; 5408 5409 header = (struct scsi_format_header_long *) 5410 ctsio->kern_data_ptr; 5411 5412 defect_list_len = scsi_4btoul(header->defect_list_len); 5413 if (defect_list_len != 0) { 5414 ctl_set_invalid_field(ctsio, 5415 /*sks_valid*/ 1, 5416 /*command*/ 0, 5417 /*field*/ 2, 5418 /*bit_valid*/ 0, 5419 /*bit*/ 0); 5420 goto bailout; 5421 } 5422 } else { 5423 struct scsi_format_header_short *header; 5424 5425 header = (struct scsi_format_header_short *) 5426 ctsio->kern_data_ptr; 5427 5428 defect_list_len = scsi_2btoul(header->defect_list_len); 5429 if (defect_list_len != 0) { 5430 ctl_set_invalid_field(ctsio, 5431 /*sks_valid*/ 1, 5432 /*command*/ 0, 5433 /*field*/ 2, 5434 /*bit_valid*/ 0, 5435 /*bit*/ 0); 5436 goto bailout; 5437 } 5438 } 5439 } 5440 5441 ctl_set_success(ctsio); 5442bailout: 5443 5444 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5445 free(ctsio->kern_data_ptr, M_CTL); 5446 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5447 } 5448 5449 ctl_done((union ctl_io *)ctsio); 5450 return (CTL_RETVAL_COMPLETE); 5451} 5452 5453int 5454ctl_read_buffer(struct ctl_scsiio *ctsio) 5455{ 5456 struct ctl_lun *lun = CTL_LUN(ctsio); 5457 uint64_t buffer_offset; 5458 uint32_t len; 5459 uint8_t byte2; 5460 static uint8_t descr[4]; 5461 static uint8_t echo_descr[4] = { 0 }; 5462 5463 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5464 5465 switch (ctsio->cdb[0]) { 5466 case READ_BUFFER: { 5467 struct scsi_read_buffer *cdb; 5468 5469 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5470 buffer_offset = scsi_3btoul(cdb->offset); 5471 len = scsi_3btoul(cdb->length); 5472 byte2 = cdb->byte2; 5473 break; 5474 } 5475 case READ_BUFFER_16: { 5476 struct scsi_read_buffer_16 *cdb; 5477 5478 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5479 buffer_offset = scsi_8btou64(cdb->offset); 5480 len = scsi_4btoul(cdb->length); 5481 byte2 = cdb->byte2; 5482 break; 5483 } 5484 default: /* This shouldn't happen. */ 5485 ctl_set_invalid_opcode(ctsio); 5486 ctl_done((union ctl_io *)ctsio); 5487 return (CTL_RETVAL_COMPLETE); 5488 } 5489 5490 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5491 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5492 ctl_set_invalid_field(ctsio, 5493 /*sks_valid*/ 1, 5494 /*command*/ 1, 5495 /*field*/ 6, 5496 /*bit_valid*/ 0, 5497 /*bit*/ 0); 5498 ctl_done((union ctl_io *)ctsio); 5499 return (CTL_RETVAL_COMPLETE); 5500 } 5501 5502 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5503 descr[0] = 0; 5504 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5505 ctsio->kern_data_ptr = descr; 5506 len = min(len, sizeof(descr)); 5507 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5508 ctsio->kern_data_ptr = echo_descr; 5509 len = min(len, sizeof(echo_descr)); 5510 } else { 5511 if (lun->write_buffer == NULL) { 5512 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5513 M_CTL, M_WAITOK); 5514 } 5515 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5516 } 5517 ctsio->kern_data_len = len; 5518 ctsio->kern_total_len = len; 5519 ctsio->kern_data_resid = 0; 5520 ctsio->kern_rel_offset = 0; 5521 ctsio->kern_sg_entries = 0; 5522 ctl_set_success(ctsio); 5523 ctsio->be_move_done = ctl_config_move_done; 5524 ctl_datamove((union ctl_io *)ctsio); 5525 return (CTL_RETVAL_COMPLETE); 5526} 5527 5528int 5529ctl_write_buffer(struct ctl_scsiio *ctsio) 5530{ 5531 struct ctl_lun *lun = CTL_LUN(ctsio); 5532 struct scsi_write_buffer *cdb; 5533 int buffer_offset, len; 5534 5535 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5536 5537 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5538 5539 len = scsi_3btoul(cdb->length); 5540 buffer_offset = scsi_3btoul(cdb->offset); 5541 5542 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5543 ctl_set_invalid_field(ctsio, 5544 /*sks_valid*/ 1, 5545 /*command*/ 1, 5546 /*field*/ 6, 5547 /*bit_valid*/ 0, 5548 /*bit*/ 0); 5549 ctl_done((union ctl_io *)ctsio); 5550 return (CTL_RETVAL_COMPLETE); 5551 } 5552 5553 /* 5554 * If we've got a kernel request that hasn't been malloced yet, 5555 * malloc it and tell the caller the data buffer is here. 5556 */ 5557 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5558 if (lun->write_buffer == NULL) { 5559 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5560 M_CTL, M_WAITOK); 5561 } 5562 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5563 ctsio->kern_data_len = len; 5564 ctsio->kern_total_len = len; 5565 ctsio->kern_data_resid = 0; 5566 ctsio->kern_rel_offset = 0; 5567 ctsio->kern_sg_entries = 0; 5568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5569 ctsio->be_move_done = ctl_config_move_done; 5570 ctl_datamove((union ctl_io *)ctsio); 5571 5572 return (CTL_RETVAL_COMPLETE); 5573 } 5574 5575 ctl_set_success(ctsio); 5576 ctl_done((union ctl_io *)ctsio); 5577 return (CTL_RETVAL_COMPLETE); 5578} 5579 5580int 5581ctl_write_same(struct ctl_scsiio *ctsio) 5582{ 5583 struct ctl_lun *lun = CTL_LUN(ctsio); 5584 struct ctl_lba_len_flags *lbalen; 5585 uint64_t lba; 5586 uint32_t num_blocks; 5587 int len, retval; 5588 uint8_t byte2; 5589 5590 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5591 5592 switch (ctsio->cdb[0]) { 5593 case WRITE_SAME_10: { 5594 struct scsi_write_same_10 *cdb; 5595 5596 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5597 5598 lba = scsi_4btoul(cdb->addr); 5599 num_blocks = scsi_2btoul(cdb->length); 5600 byte2 = cdb->byte2; 5601 break; 5602 } 5603 case WRITE_SAME_16: { 5604 struct scsi_write_same_16 *cdb; 5605 5606 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5607 5608 lba = scsi_8btou64(cdb->addr); 5609 num_blocks = scsi_4btoul(cdb->length); 5610 byte2 = cdb->byte2; 5611 break; 5612 } 5613 default: 5614 /* 5615 * We got a command we don't support. This shouldn't 5616 * happen, commands should be filtered out above us. 5617 */ 5618 ctl_set_invalid_opcode(ctsio); 5619 ctl_done((union ctl_io *)ctsio); 5620 5621 return (CTL_RETVAL_COMPLETE); 5622 break; /* NOTREACHED */ 5623 } 5624 5625 /* ANCHOR flag can be used only together with UNMAP */ 5626 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5627 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5628 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5629 ctl_done((union ctl_io *)ctsio); 5630 return (CTL_RETVAL_COMPLETE); 5631 } 5632 5633 /* 5634 * The first check is to make sure we're in bounds, the second 5635 * check is to catch wrap-around problems. If the lba + num blocks 5636 * is less than the lba, then we've wrapped around and the block 5637 * range is invalid anyway. 5638 */ 5639 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5640 || ((lba + num_blocks) < lba)) { 5641 ctl_set_lba_out_of_range(ctsio, 5642 MAX(lba, lun->be_lun->maxlba + 1)); 5643 ctl_done((union ctl_io *)ctsio); 5644 return (CTL_RETVAL_COMPLETE); 5645 } 5646 5647 /* Zero number of blocks means "to the last logical block" */ 5648 if (num_blocks == 0) { 5649 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5650 ctl_set_invalid_field(ctsio, 5651 /*sks_valid*/ 0, 5652 /*command*/ 1, 5653 /*field*/ 0, 5654 /*bit_valid*/ 0, 5655 /*bit*/ 0); 5656 ctl_done((union ctl_io *)ctsio); 5657 return (CTL_RETVAL_COMPLETE); 5658 } 5659 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5660 } 5661 5662 len = lun->be_lun->blocksize; 5663 5664 /* 5665 * If we've got a kernel request that hasn't been malloced yet, 5666 * malloc it and tell the caller the data buffer is here. 5667 */ 5668 if ((byte2 & SWS_NDOB) == 0 && 5669 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5670 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5671 ctsio->kern_data_len = len; 5672 ctsio->kern_total_len = len; 5673 ctsio->kern_data_resid = 0; 5674 ctsio->kern_rel_offset = 0; 5675 ctsio->kern_sg_entries = 0; 5676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5677 ctsio->be_move_done = ctl_config_move_done; 5678 ctl_datamove((union ctl_io *)ctsio); 5679 5680 return (CTL_RETVAL_COMPLETE); 5681 } 5682 5683 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5684 lbalen->lba = lba; 5685 lbalen->len = num_blocks; 5686 lbalen->flags = byte2; 5687 retval = lun->backend->config_write((union ctl_io *)ctsio); 5688 5689 return (retval); 5690} 5691 5692int 5693ctl_unmap(struct ctl_scsiio *ctsio) 5694{ 5695 struct ctl_lun *lun = CTL_LUN(ctsio); 5696 struct scsi_unmap *cdb; 5697 struct ctl_ptr_len_flags *ptrlen; 5698 struct scsi_unmap_header *hdr; 5699 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5700 uint64_t lba; 5701 uint32_t num_blocks; 5702 int len, retval; 5703 uint8_t byte2; 5704 5705 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5706 5707 cdb = (struct scsi_unmap *)ctsio->cdb; 5708 len = scsi_2btoul(cdb->length); 5709 byte2 = cdb->byte2; 5710 5711 /* 5712 * If we've got a kernel request that hasn't been malloced yet, 5713 * malloc it and tell the caller the data buffer is here. 5714 */ 5715 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5716 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5717 ctsio->kern_data_len = len; 5718 ctsio->kern_total_len = len; 5719 ctsio->kern_data_resid = 0; 5720 ctsio->kern_rel_offset = 0; 5721 ctsio->kern_sg_entries = 0; 5722 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5723 ctsio->be_move_done = ctl_config_move_done; 5724 ctl_datamove((union ctl_io *)ctsio); 5725 5726 return (CTL_RETVAL_COMPLETE); 5727 } 5728 5729 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5730 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5731 if (len < sizeof (*hdr) || 5732 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5733 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5734 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5735 ctl_set_invalid_field(ctsio, 5736 /*sks_valid*/ 0, 5737 /*command*/ 0, 5738 /*field*/ 0, 5739 /*bit_valid*/ 0, 5740 /*bit*/ 0); 5741 goto done; 5742 } 5743 len = scsi_2btoul(hdr->desc_length); 5744 buf = (struct scsi_unmap_desc *)(hdr + 1); 5745 end = buf + len / sizeof(*buf); 5746 5747 endnz = buf; 5748 for (range = buf; range < end; range++) { 5749 lba = scsi_8btou64(range->lba); 5750 num_blocks = scsi_4btoul(range->length); 5751 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5752 || ((lba + num_blocks) < lba)) { 5753 ctl_set_lba_out_of_range(ctsio, 5754 MAX(lba, lun->be_lun->maxlba + 1)); 5755 ctl_done((union ctl_io *)ctsio); 5756 return (CTL_RETVAL_COMPLETE); 5757 } 5758 if (num_blocks != 0) 5759 endnz = range + 1; 5760 } 5761 5762 /* 5763 * Block backend can not handle zero last range. 5764 * Filter it out and return if there is nothing left. 5765 */ 5766 len = (uint8_t *)endnz - (uint8_t *)buf; 5767 if (len == 0) { 5768 ctl_set_success(ctsio); 5769 goto done; 5770 } 5771 5772 mtx_lock(&lun->lun_lock); 5773 ptrlen = (struct ctl_ptr_len_flags *) 5774 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5775 ptrlen->ptr = (void *)buf; 5776 ptrlen->len = len; 5777 ptrlen->flags = byte2; 5778 ctl_check_blocked(lun); 5779 mtx_unlock(&lun->lun_lock); 5780 5781 retval = lun->backend->config_write((union ctl_io *)ctsio); 5782 return (retval); 5783 5784done: 5785 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5786 free(ctsio->kern_data_ptr, M_CTL); 5787 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5788 } 5789 ctl_done((union ctl_io *)ctsio); 5790 return (CTL_RETVAL_COMPLETE); 5791} 5792 5793int 5794ctl_default_page_handler(struct ctl_scsiio *ctsio, 5795 struct ctl_page_index *page_index, uint8_t *page_ptr) 5796{ 5797 struct ctl_lun *lun = CTL_LUN(ctsio); 5798 uint8_t *current_cp; 5799 int set_ua; 5800 uint32_t initidx; 5801 5802 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5803 set_ua = 0; 5804 5805 current_cp = (page_index->page_data + (page_index->page_len * 5806 CTL_PAGE_CURRENT)); 5807 5808 mtx_lock(&lun->lun_lock); 5809 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5810 memcpy(current_cp, page_ptr, page_index->page_len); 5811 set_ua = 1; 5812 } 5813 if (set_ua != 0) 5814 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5815 mtx_unlock(&lun->lun_lock); 5816 if (set_ua) { 5817 ctl_isc_announce_mode(lun, 5818 ctl_get_initindex(&ctsio->io_hdr.nexus), 5819 page_index->page_code, page_index->subpage); 5820 } 5821 return (CTL_RETVAL_COMPLETE); 5822} 5823 5824static void 5825ctl_ie_timer(void *arg) 5826{ 5827 struct ctl_lun *lun = arg; 5828 uint64_t t; 5829 5830 if (lun->ie_asc == 0) 5831 return; 5832 5833 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5834 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5835 else 5836 lun->ie_reported = 0; 5837 5838 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5839 lun->ie_reportcnt++; 5840 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5841 if (t == 0 || t == UINT32_MAX) 5842 t = 3000; /* 5 min */ 5843 callout_schedule(&lun->ie_callout, t * hz / 10); 5844 } 5845} 5846 5847int 5848ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5849 struct ctl_page_index *page_index, uint8_t *page_ptr) 5850{ 5851 struct ctl_lun *lun = CTL_LUN(ctsio); 5852 struct scsi_info_exceptions_page *pg; 5853 uint64_t t; 5854 5855 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5856 5857 pg = (struct scsi_info_exceptions_page *)page_ptr; 5858 mtx_lock(&lun->lun_lock); 5859 if (pg->info_flags & SIEP_FLAGS_TEST) { 5860 lun->ie_asc = 0x5d; 5861 lun->ie_ascq = 0xff; 5862 if (pg->mrie == SIEP_MRIE_UA) { 5863 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5864 lun->ie_reported = 1; 5865 } else { 5866 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5867 lun->ie_reported = -1; 5868 } 5869 lun->ie_reportcnt = 1; 5870 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5871 lun->ie_reportcnt++; 5872 t = scsi_4btoul(pg->interval_timer); 5873 if (t == 0 || t == UINT32_MAX) 5874 t = 3000; /* 5 min */ 5875 callout_reset(&lun->ie_callout, t * hz / 10, 5876 ctl_ie_timer, lun); 5877 } 5878 } else { 5879 lun->ie_asc = 0; 5880 lun->ie_ascq = 0; 5881 lun->ie_reported = 1; 5882 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5883 lun->ie_reportcnt = UINT32_MAX; 5884 callout_stop(&lun->ie_callout); 5885 } 5886 mtx_unlock(&lun->lun_lock); 5887 return (CTL_RETVAL_COMPLETE); 5888} 5889 5890static int 5891ctl_do_mode_select(union ctl_io *io) 5892{ 5893 struct ctl_lun *lun = CTL_LUN(io); 5894 struct scsi_mode_page_header *page_header; 5895 struct ctl_page_index *page_index; 5896 struct ctl_scsiio *ctsio; 5897 int page_len, page_len_offset, page_len_size; 5898 union ctl_modepage_info *modepage_info; 5899 uint16_t *len_left, *len_used; 5900 int retval, i; 5901 5902 ctsio = &io->scsiio; 5903 page_index = NULL; 5904 page_len = 0; 5905 5906 modepage_info = (union ctl_modepage_info *) 5907 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5908 len_left = &modepage_info->header.len_left; 5909 len_used = &modepage_info->header.len_used; 5910 5911do_next_page: 5912 5913 page_header = (struct scsi_mode_page_header *) 5914 (ctsio->kern_data_ptr + *len_used); 5915 5916 if (*len_left == 0) { 5917 free(ctsio->kern_data_ptr, M_CTL); 5918 ctl_set_success(ctsio); 5919 ctl_done((union ctl_io *)ctsio); 5920 return (CTL_RETVAL_COMPLETE); 5921 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5922 5923 free(ctsio->kern_data_ptr, M_CTL); 5924 ctl_set_param_len_error(ctsio); 5925 ctl_done((union ctl_io *)ctsio); 5926 return (CTL_RETVAL_COMPLETE); 5927 5928 } else if ((page_header->page_code & SMPH_SPF) 5929 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5930 5931 free(ctsio->kern_data_ptr, M_CTL); 5932 ctl_set_param_len_error(ctsio); 5933 ctl_done((union ctl_io *)ctsio); 5934 return (CTL_RETVAL_COMPLETE); 5935 } 5936 5937 5938 /* 5939 * XXX KDM should we do something with the block descriptor? 5940 */ 5941 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5942 page_index = &lun->mode_pages.index[i]; 5943 if (lun->be_lun->lun_type == T_DIRECT && 5944 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5945 continue; 5946 if (lun->be_lun->lun_type == T_PROCESSOR && 5947 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5948 continue; 5949 if (lun->be_lun->lun_type == T_CDROM && 5950 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5951 continue; 5952 5953 if ((page_index->page_code & SMPH_PC_MASK) != 5954 (page_header->page_code & SMPH_PC_MASK)) 5955 continue; 5956 5957 /* 5958 * If neither page has a subpage code, then we've got a 5959 * match. 5960 */ 5961 if (((page_index->page_code & SMPH_SPF) == 0) 5962 && ((page_header->page_code & SMPH_SPF) == 0)) { 5963 page_len = page_header->page_length; 5964 break; 5965 } 5966 5967 /* 5968 * If both pages have subpages, then the subpage numbers 5969 * have to match. 5970 */ 5971 if ((page_index->page_code & SMPH_SPF) 5972 && (page_header->page_code & SMPH_SPF)) { 5973 struct scsi_mode_page_header_sp *sph; 5974 5975 sph = (struct scsi_mode_page_header_sp *)page_header; 5976 if (page_index->subpage == sph->subpage) { 5977 page_len = scsi_2btoul(sph->page_length); 5978 break; 5979 } 5980 } 5981 } 5982 5983 /* 5984 * If we couldn't find the page, or if we don't have a mode select 5985 * handler for it, send back an error to the user. 5986 */ 5987 if ((i >= CTL_NUM_MODE_PAGES) 5988 || (page_index->select_handler == NULL)) { 5989 ctl_set_invalid_field(ctsio, 5990 /*sks_valid*/ 1, 5991 /*command*/ 0, 5992 /*field*/ *len_used, 5993 /*bit_valid*/ 0, 5994 /*bit*/ 0); 5995 free(ctsio->kern_data_ptr, M_CTL); 5996 ctl_done((union ctl_io *)ctsio); 5997 return (CTL_RETVAL_COMPLETE); 5998 } 5999 6000 if (page_index->page_code & SMPH_SPF) { 6001 page_len_offset = 2; 6002 page_len_size = 2; 6003 } else { 6004 page_len_size = 1; 6005 page_len_offset = 1; 6006 } 6007 6008 /* 6009 * If the length the initiator gives us isn't the one we specify in 6010 * the mode page header, or if they didn't specify enough data in 6011 * the CDB to avoid truncating this page, kick out the request. 6012 */ 6013 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6014 ctl_set_invalid_field(ctsio, 6015 /*sks_valid*/ 1, 6016 /*command*/ 0, 6017 /*field*/ *len_used + page_len_offset, 6018 /*bit_valid*/ 0, 6019 /*bit*/ 0); 6020 free(ctsio->kern_data_ptr, M_CTL); 6021 ctl_done((union ctl_io *)ctsio); 6022 return (CTL_RETVAL_COMPLETE); 6023 } 6024 if (*len_left < page_index->page_len) { 6025 free(ctsio->kern_data_ptr, M_CTL); 6026 ctl_set_param_len_error(ctsio); 6027 ctl_done((union ctl_io *)ctsio); 6028 return (CTL_RETVAL_COMPLETE); 6029 } 6030 6031 /* 6032 * Run through the mode page, checking to make sure that the bits 6033 * the user changed are actually legal for him to change. 6034 */ 6035 for (i = 0; i < page_index->page_len; i++) { 6036 uint8_t *user_byte, *change_mask, *current_byte; 6037 int bad_bit; 6038 int j; 6039 6040 user_byte = (uint8_t *)page_header + i; 6041 change_mask = page_index->page_data + 6042 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6043 current_byte = page_index->page_data + 6044 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6045 6046 /* 6047 * Check to see whether the user set any bits in this byte 6048 * that he is not allowed to set. 6049 */ 6050 if ((*user_byte & ~(*change_mask)) == 6051 (*current_byte & ~(*change_mask))) 6052 continue; 6053 6054 /* 6055 * Go through bit by bit to determine which one is illegal. 6056 */ 6057 bad_bit = 0; 6058 for (j = 7; j >= 0; j--) { 6059 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6060 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6061 bad_bit = i; 6062 break; 6063 } 6064 } 6065 ctl_set_invalid_field(ctsio, 6066 /*sks_valid*/ 1, 6067 /*command*/ 0, 6068 /*field*/ *len_used + i, 6069 /*bit_valid*/ 1, 6070 /*bit*/ bad_bit); 6071 free(ctsio->kern_data_ptr, M_CTL); 6072 ctl_done((union ctl_io *)ctsio); 6073 return (CTL_RETVAL_COMPLETE); 6074 } 6075 6076 /* 6077 * Decrement these before we call the page handler, since we may 6078 * end up getting called back one way or another before the handler 6079 * returns to this context. 6080 */ 6081 *len_left -= page_index->page_len; 6082 *len_used += page_index->page_len; 6083 6084 retval = page_index->select_handler(ctsio, page_index, 6085 (uint8_t *)page_header); 6086 6087 /* 6088 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6089 * wait until this queued command completes to finish processing 6090 * the mode page. If it returns anything other than 6091 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6092 * already set the sense information, freed the data pointer, and 6093 * completed the io for us. 6094 */ 6095 if (retval != CTL_RETVAL_COMPLETE) 6096 goto bailout_no_done; 6097 6098 /* 6099 * If the initiator sent us more than one page, parse the next one. 6100 */ 6101 if (*len_left > 0) 6102 goto do_next_page; 6103 6104 ctl_set_success(ctsio); 6105 free(ctsio->kern_data_ptr, M_CTL); 6106 ctl_done((union ctl_io *)ctsio); 6107 6108bailout_no_done: 6109 6110 return (CTL_RETVAL_COMPLETE); 6111 6112} 6113 6114int 6115ctl_mode_select(struct ctl_scsiio *ctsio) 6116{ 6117 struct ctl_lun *lun = CTL_LUN(ctsio); 6118 union ctl_modepage_info *modepage_info; 6119 int bd_len, i, header_size, param_len, pf, rtd, sp; 6120 uint32_t initidx; 6121 6122 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6123 switch (ctsio->cdb[0]) { 6124 case MODE_SELECT_6: { 6125 struct scsi_mode_select_6 *cdb; 6126 6127 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6128 6129 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6130 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6131 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6132 param_len = cdb->length; 6133 header_size = sizeof(struct scsi_mode_header_6); 6134 break; 6135 } 6136 case MODE_SELECT_10: { 6137 struct scsi_mode_select_10 *cdb; 6138 6139 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6140 6141 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6142 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0; 6143 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6144 param_len = scsi_2btoul(cdb->length); 6145 header_size = sizeof(struct scsi_mode_header_10); 6146 break; 6147 } 6148 default: 6149 ctl_set_invalid_opcode(ctsio); 6150 ctl_done((union ctl_io *)ctsio); 6151 return (CTL_RETVAL_COMPLETE); 6152 } 6153 6154 if (rtd) { 6155 if (param_len != 0) { 6156 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0, 6157 /*command*/ 1, /*field*/ 0, 6158 /*bit_valid*/ 0, /*bit*/ 0); 6159 ctl_done((union ctl_io *)ctsio); 6160 return (CTL_RETVAL_COMPLETE); 6161 } 6162 6163 /* Revert to defaults. */ 6164 ctl_init_page_index(lun); 6165 mtx_lock(&lun->lun_lock); 6166 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6167 mtx_unlock(&lun->lun_lock); 6168 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6169 ctl_isc_announce_mode(lun, -1, 6170 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 6171 lun->mode_pages.index[i].subpage); 6172 } 6173 ctl_set_success(ctsio); 6174 ctl_done((union ctl_io *)ctsio); 6175 return (CTL_RETVAL_COMPLETE); 6176 } 6177 6178 /* 6179 * From SPC-3: 6180 * "A parameter list length of zero indicates that the Data-Out Buffer 6181 * shall be empty. This condition shall not be considered as an error." 6182 */ 6183 if (param_len == 0) { 6184 ctl_set_success(ctsio); 6185 ctl_done((union ctl_io *)ctsio); 6186 return (CTL_RETVAL_COMPLETE); 6187 } 6188 6189 /* 6190 * Since we'll hit this the first time through, prior to 6191 * allocation, we don't need to free a data buffer here. 6192 */ 6193 if (param_len < header_size) { 6194 ctl_set_param_len_error(ctsio); 6195 ctl_done((union ctl_io *)ctsio); 6196 return (CTL_RETVAL_COMPLETE); 6197 } 6198 6199 /* 6200 * Allocate the data buffer and grab the user's data. In theory, 6201 * we shouldn't have to sanity check the parameter list length here 6202 * because the maximum size is 64K. We should be able to malloc 6203 * that much without too many problems. 6204 */ 6205 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6206 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6207 ctsio->kern_data_len = param_len; 6208 ctsio->kern_total_len = param_len; 6209 ctsio->kern_data_resid = 0; 6210 ctsio->kern_rel_offset = 0; 6211 ctsio->kern_sg_entries = 0; 6212 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6213 ctsio->be_move_done = ctl_config_move_done; 6214 ctl_datamove((union ctl_io *)ctsio); 6215 6216 return (CTL_RETVAL_COMPLETE); 6217 } 6218 6219 switch (ctsio->cdb[0]) { 6220 case MODE_SELECT_6: { 6221 struct scsi_mode_header_6 *mh6; 6222 6223 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6224 bd_len = mh6->blk_desc_len; 6225 break; 6226 } 6227 case MODE_SELECT_10: { 6228 struct scsi_mode_header_10 *mh10; 6229 6230 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6231 bd_len = scsi_2btoul(mh10->blk_desc_len); 6232 break; 6233 } 6234 default: 6235 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6236 } 6237 6238 if (param_len < (header_size + bd_len)) { 6239 free(ctsio->kern_data_ptr, M_CTL); 6240 ctl_set_param_len_error(ctsio); 6241 ctl_done((union ctl_io *)ctsio); 6242 return (CTL_RETVAL_COMPLETE); 6243 } 6244 6245 /* 6246 * Set the IO_CONT flag, so that if this I/O gets passed to 6247 * ctl_config_write_done(), it'll get passed back to 6248 * ctl_do_mode_select() for further processing, or completion if 6249 * we're all done. 6250 */ 6251 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6252 ctsio->io_cont = ctl_do_mode_select; 6253 6254 modepage_info = (union ctl_modepage_info *) 6255 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6256 memset(modepage_info, 0, sizeof(*modepage_info)); 6257 modepage_info->header.len_left = param_len - header_size - bd_len; 6258 modepage_info->header.len_used = header_size + bd_len; 6259 6260 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6261} 6262 6263int 6264ctl_mode_sense(struct ctl_scsiio *ctsio) 6265{ 6266 struct ctl_lun *lun = CTL_LUN(ctsio); 6267 int pc, page_code, dbd, llba, subpage; 6268 int alloc_len, page_len, header_len, total_len; 6269 struct scsi_mode_block_descr *block_desc; 6270 struct ctl_page_index *page_index; 6271 6272 dbd = 0; 6273 llba = 0; 6274 block_desc = NULL; 6275 6276 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6277 6278 switch (ctsio->cdb[0]) { 6279 case MODE_SENSE_6: { 6280 struct scsi_mode_sense_6 *cdb; 6281 6282 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6283 6284 header_len = sizeof(struct scsi_mode_hdr_6); 6285 if (cdb->byte2 & SMS_DBD) 6286 dbd = 1; 6287 else 6288 header_len += sizeof(struct scsi_mode_block_descr); 6289 6290 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6291 page_code = cdb->page & SMS_PAGE_CODE; 6292 subpage = cdb->subpage; 6293 alloc_len = cdb->length; 6294 break; 6295 } 6296 case MODE_SENSE_10: { 6297 struct scsi_mode_sense_10 *cdb; 6298 6299 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6300 6301 header_len = sizeof(struct scsi_mode_hdr_10); 6302 6303 if (cdb->byte2 & SMS_DBD) 6304 dbd = 1; 6305 else 6306 header_len += sizeof(struct scsi_mode_block_descr); 6307 if (cdb->byte2 & SMS10_LLBAA) 6308 llba = 1; 6309 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6310 page_code = cdb->page & SMS_PAGE_CODE; 6311 subpage = cdb->subpage; 6312 alloc_len = scsi_2btoul(cdb->length); 6313 break; 6314 } 6315 default: 6316 ctl_set_invalid_opcode(ctsio); 6317 ctl_done((union ctl_io *)ctsio); 6318 return (CTL_RETVAL_COMPLETE); 6319 break; /* NOTREACHED */ 6320 } 6321 6322 /* 6323 * We have to make a first pass through to calculate the size of 6324 * the pages that match the user's query. Then we allocate enough 6325 * memory to hold it, and actually copy the data into the buffer. 6326 */ 6327 switch (page_code) { 6328 case SMS_ALL_PAGES_PAGE: { 6329 u_int i; 6330 6331 page_len = 0; 6332 6333 /* 6334 * At the moment, values other than 0 and 0xff here are 6335 * reserved according to SPC-3. 6336 */ 6337 if ((subpage != SMS_SUBPAGE_PAGE_0) 6338 && (subpage != SMS_SUBPAGE_ALL)) { 6339 ctl_set_invalid_field(ctsio, 6340 /*sks_valid*/ 1, 6341 /*command*/ 1, 6342 /*field*/ 3, 6343 /*bit_valid*/ 0, 6344 /*bit*/ 0); 6345 ctl_done((union ctl_io *)ctsio); 6346 return (CTL_RETVAL_COMPLETE); 6347 } 6348 6349 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6350 page_index = &lun->mode_pages.index[i]; 6351 6352 /* Make sure the page is supported for this dev type */ 6353 if (lun->be_lun->lun_type == T_DIRECT && 6354 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6355 continue; 6356 if (lun->be_lun->lun_type == T_PROCESSOR && 6357 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6358 continue; 6359 if (lun->be_lun->lun_type == T_CDROM && 6360 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6361 continue; 6362 6363 /* 6364 * We don't use this subpage if the user didn't 6365 * request all subpages. 6366 */ 6367 if ((page_index->subpage != 0) 6368 && (subpage == SMS_SUBPAGE_PAGE_0)) 6369 continue; 6370 6371#if 0 6372 printf("found page %#x len %d\n", 6373 page_index->page_code & SMPH_PC_MASK, 6374 page_index->page_len); 6375#endif 6376 page_len += page_index->page_len; 6377 } 6378 break; 6379 } 6380 default: { 6381 u_int i; 6382 6383 page_len = 0; 6384 6385 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6386 page_index = &lun->mode_pages.index[i]; 6387 6388 /* Make sure the page is supported for this dev type */ 6389 if (lun->be_lun->lun_type == T_DIRECT && 6390 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6391 continue; 6392 if (lun->be_lun->lun_type == T_PROCESSOR && 6393 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6394 continue; 6395 if (lun->be_lun->lun_type == T_CDROM && 6396 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6397 continue; 6398 6399 /* Look for the right page code */ 6400 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6401 continue; 6402 6403 /* Look for the right subpage or the subpage wildcard*/ 6404 if ((page_index->subpage != subpage) 6405 && (subpage != SMS_SUBPAGE_ALL)) 6406 continue; 6407 6408#if 0 6409 printf("found page %#x len %d\n", 6410 page_index->page_code & SMPH_PC_MASK, 6411 page_index->page_len); 6412#endif 6413 6414 page_len += page_index->page_len; 6415 } 6416 6417 if (page_len == 0) { 6418 ctl_set_invalid_field(ctsio, 6419 /*sks_valid*/ 1, 6420 /*command*/ 1, 6421 /*field*/ 2, 6422 /*bit_valid*/ 1, 6423 /*bit*/ 5); 6424 ctl_done((union ctl_io *)ctsio); 6425 return (CTL_RETVAL_COMPLETE); 6426 } 6427 break; 6428 } 6429 } 6430 6431 total_len = header_len + page_len; 6432#if 0 6433 printf("header_len = %d, page_len = %d, total_len = %d\n", 6434 header_len, page_len, total_len); 6435#endif 6436 6437 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6438 ctsio->kern_sg_entries = 0; 6439 ctsio->kern_data_resid = 0; 6440 ctsio->kern_rel_offset = 0; 6441 if (total_len < alloc_len) { 6442 ctsio->residual = alloc_len - total_len; 6443 ctsio->kern_data_len = total_len; 6444 ctsio->kern_total_len = total_len; 6445 } else { 6446 ctsio->residual = 0; 6447 ctsio->kern_data_len = alloc_len; 6448 ctsio->kern_total_len = alloc_len; 6449 } 6450 6451 switch (ctsio->cdb[0]) { 6452 case MODE_SENSE_6: { 6453 struct scsi_mode_hdr_6 *header; 6454 6455 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6456 6457 header->datalen = MIN(total_len - 1, 254); 6458 if (lun->be_lun->lun_type == T_DIRECT) { 6459 header->dev_specific = 0x10; /* DPOFUA */ 6460 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6461 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6462 header->dev_specific |= 0x80; /* WP */ 6463 } 6464 if (dbd) 6465 header->block_descr_len = 0; 6466 else 6467 header->block_descr_len = 6468 sizeof(struct scsi_mode_block_descr); 6469 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6470 break; 6471 } 6472 case MODE_SENSE_10: { 6473 struct scsi_mode_hdr_10 *header; 6474 int datalen; 6475 6476 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6477 6478 datalen = MIN(total_len - 2, 65533); 6479 scsi_ulto2b(datalen, header->datalen); 6480 if (lun->be_lun->lun_type == T_DIRECT) { 6481 header->dev_specific = 0x10; /* DPOFUA */ 6482 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6483 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6484 header->dev_specific |= 0x80; /* WP */ 6485 } 6486 if (dbd) 6487 scsi_ulto2b(0, header->block_descr_len); 6488 else 6489 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6490 header->block_descr_len); 6491 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6492 break; 6493 } 6494 default: 6495 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6496 } 6497 6498 /* 6499 * If we've got a disk, use its blocksize in the block 6500 * descriptor. Otherwise, just set it to 0. 6501 */ 6502 if (dbd == 0) { 6503 if (lun->be_lun->lun_type == T_DIRECT) 6504 scsi_ulto3b(lun->be_lun->blocksize, 6505 block_desc->block_len); 6506 else 6507 scsi_ulto3b(0, block_desc->block_len); 6508 } 6509 6510 switch (page_code) { 6511 case SMS_ALL_PAGES_PAGE: { 6512 int i, data_used; 6513 6514 data_used = header_len; 6515 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6516 struct ctl_page_index *page_index; 6517 6518 page_index = &lun->mode_pages.index[i]; 6519 if (lun->be_lun->lun_type == T_DIRECT && 6520 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6521 continue; 6522 if (lun->be_lun->lun_type == T_PROCESSOR && 6523 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6524 continue; 6525 if (lun->be_lun->lun_type == T_CDROM && 6526 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6527 continue; 6528 6529 /* 6530 * We don't use this subpage if the user didn't 6531 * request all subpages. We already checked (above) 6532 * to make sure the user only specified a subpage 6533 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6534 */ 6535 if ((page_index->subpage != 0) 6536 && (subpage == SMS_SUBPAGE_PAGE_0)) 6537 continue; 6538 6539 /* 6540 * Call the handler, if it exists, to update the 6541 * page to the latest values. 6542 */ 6543 if (page_index->sense_handler != NULL) 6544 page_index->sense_handler(ctsio, page_index,pc); 6545 6546 memcpy(ctsio->kern_data_ptr + data_used, 6547 page_index->page_data + 6548 (page_index->page_len * pc), 6549 page_index->page_len); 6550 data_used += page_index->page_len; 6551 } 6552 break; 6553 } 6554 default: { 6555 int i, data_used; 6556 6557 data_used = header_len; 6558 6559 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6560 struct ctl_page_index *page_index; 6561 6562 page_index = &lun->mode_pages.index[i]; 6563 6564 /* Look for the right page code */ 6565 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6566 continue; 6567 6568 /* Look for the right subpage or the subpage wildcard*/ 6569 if ((page_index->subpage != subpage) 6570 && (subpage != SMS_SUBPAGE_ALL)) 6571 continue; 6572 6573 /* Make sure the page is supported for this dev type */ 6574 if (lun->be_lun->lun_type == T_DIRECT && 6575 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6576 continue; 6577 if (lun->be_lun->lun_type == T_PROCESSOR && 6578 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6579 continue; 6580 if (lun->be_lun->lun_type == T_CDROM && 6581 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6582 continue; 6583 6584 /* 6585 * Call the handler, if it exists, to update the 6586 * page to the latest values. 6587 */ 6588 if (page_index->sense_handler != NULL) 6589 page_index->sense_handler(ctsio, page_index,pc); 6590 6591 memcpy(ctsio->kern_data_ptr + data_used, 6592 page_index->page_data + 6593 (page_index->page_len * pc), 6594 page_index->page_len); 6595 data_used += page_index->page_len; 6596 } 6597 break; 6598 } 6599 } 6600 6601 ctl_set_success(ctsio); 6602 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6603 ctsio->be_move_done = ctl_config_move_done; 6604 ctl_datamove((union ctl_io *)ctsio); 6605 return (CTL_RETVAL_COMPLETE); 6606} 6607 6608int 6609ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6610 struct ctl_page_index *page_index, 6611 int pc) 6612{ 6613 struct ctl_lun *lun = CTL_LUN(ctsio); 6614 struct scsi_log_param_header *phdr; 6615 uint8_t *data; 6616 uint64_t val; 6617 6618 data = page_index->page_data; 6619 6620 if (lun->backend->lun_attr != NULL && 6621 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6622 != UINT64_MAX) { 6623 phdr = (struct scsi_log_param_header *)data; 6624 scsi_ulto2b(0x0001, phdr->param_code); 6625 phdr->param_control = SLP_LBIN | SLP_LP; 6626 phdr->param_len = 8; 6627 data = (uint8_t *)(phdr + 1); 6628 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6629 data[4] = 0x02; /* per-pool */ 6630 data += phdr->param_len; 6631 } 6632 6633 if (lun->backend->lun_attr != NULL && 6634 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6635 != UINT64_MAX) { 6636 phdr = (struct scsi_log_param_header *)data; 6637 scsi_ulto2b(0x0002, phdr->param_code); 6638 phdr->param_control = SLP_LBIN | SLP_LP; 6639 phdr->param_len = 8; 6640 data = (uint8_t *)(phdr + 1); 6641 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6642 data[4] = 0x01; /* per-LUN */ 6643 data += phdr->param_len; 6644 } 6645 6646 if (lun->backend->lun_attr != NULL && 6647 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6648 != UINT64_MAX) { 6649 phdr = (struct scsi_log_param_header *)data; 6650 scsi_ulto2b(0x00f1, phdr->param_code); 6651 phdr->param_control = SLP_LBIN | SLP_LP; 6652 phdr->param_len = 8; 6653 data = (uint8_t *)(phdr + 1); 6654 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6655 data[4] = 0x02; /* per-pool */ 6656 data += phdr->param_len; 6657 } 6658 6659 if (lun->backend->lun_attr != NULL && 6660 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6661 != UINT64_MAX) { 6662 phdr = (struct scsi_log_param_header *)data; 6663 scsi_ulto2b(0x00f2, phdr->param_code); 6664 phdr->param_control = SLP_LBIN | SLP_LP; 6665 phdr->param_len = 8; 6666 data = (uint8_t *)(phdr + 1); 6667 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6668 data[4] = 0x02; /* per-pool */ 6669 data += phdr->param_len; 6670 } 6671 6672 page_index->page_len = data - page_index->page_data; 6673 return (0); 6674} 6675 6676int 6677ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6678 struct ctl_page_index *page_index, 6679 int pc) 6680{ 6681 struct ctl_lun *lun = CTL_LUN(ctsio); 6682 struct stat_page *data; 6683 uint64_t rn, wn, rb, wb; 6684 struct bintime rt, wt; 6685 int i; 6686 6687 data = (struct stat_page *)page_index->page_data; 6688 6689 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6690 data->sap.hdr.param_control = SLP_LBIN; 6691 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6692 sizeof(struct scsi_log_param_header); 6693 rn = wn = rb = wb = 0; 6694 bintime_clear(&rt); 6695 bintime_clear(&wt); 6696 for (i = 0; i < CTL_MAX_PORTS; i++) { 6697 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6698 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6699 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6700 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6701 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6702 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6703 } 6704 scsi_u64to8b(rn, data->sap.read_num); 6705 scsi_u64to8b(wn, data->sap.write_num); 6706 if (lun->stats.blocksize > 0) { 6707 scsi_u64to8b(wb / lun->stats.blocksize, 6708 data->sap.recvieved_lba); 6709 scsi_u64to8b(rb / lun->stats.blocksize, 6710 data->sap.transmitted_lba); 6711 } 6712 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6713 data->sap.read_int); 6714 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6715 data->sap.write_int); 6716 scsi_u64to8b(0, data->sap.weighted_num); 6717 scsi_u64to8b(0, data->sap.weighted_int); 6718 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6719 data->it.hdr.param_control = SLP_LBIN; 6720 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6721 sizeof(struct scsi_log_param_header); 6722#ifdef CTL_TIME_IO 6723 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6724#endif 6725 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6726 data->it.hdr.param_control = SLP_LBIN; 6727 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6728 sizeof(struct scsi_log_param_header); 6729 scsi_ulto4b(3, data->ti.exponent); 6730 scsi_ulto4b(1, data->ti.integer); 6731 return (0); 6732} 6733 6734int 6735ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6736 struct ctl_page_index *page_index, 6737 int pc) 6738{ 6739 struct ctl_lun *lun = CTL_LUN(ctsio); 6740 struct scsi_log_informational_exceptions *data; 6741 6742 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6743 6744 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6745 data->hdr.param_control = SLP_LBIN; 6746 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6747 sizeof(struct scsi_log_param_header); 6748 data->ie_asc = lun->ie_asc; 6749 data->ie_ascq = lun->ie_ascq; 6750 data->temperature = 0xff; 6751 return (0); 6752} 6753 6754int 6755ctl_log_sense(struct ctl_scsiio *ctsio) 6756{ 6757 struct ctl_lun *lun = CTL_LUN(ctsio); 6758 int i, pc, page_code, subpage; 6759 int alloc_len, total_len; 6760 struct ctl_page_index *page_index; 6761 struct scsi_log_sense *cdb; 6762 struct scsi_log_header *header; 6763 6764 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6765 6766 cdb = (struct scsi_log_sense *)ctsio->cdb; 6767 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6768 page_code = cdb->page & SLS_PAGE_CODE; 6769 subpage = cdb->subpage; 6770 alloc_len = scsi_2btoul(cdb->length); 6771 6772 page_index = NULL; 6773 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6774 page_index = &lun->log_pages.index[i]; 6775 6776 /* Look for the right page code */ 6777 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6778 continue; 6779 6780 /* Look for the right subpage or the subpage wildcard*/ 6781 if (page_index->subpage != subpage) 6782 continue; 6783 6784 break; 6785 } 6786 if (i >= CTL_NUM_LOG_PAGES) { 6787 ctl_set_invalid_field(ctsio, 6788 /*sks_valid*/ 1, 6789 /*command*/ 1, 6790 /*field*/ 2, 6791 /*bit_valid*/ 0, 6792 /*bit*/ 0); 6793 ctl_done((union ctl_io *)ctsio); 6794 return (CTL_RETVAL_COMPLETE); 6795 } 6796 6797 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6798 6799 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6800 ctsio->kern_sg_entries = 0; 6801 ctsio->kern_data_resid = 0; 6802 ctsio->kern_rel_offset = 0; 6803 if (total_len < alloc_len) { 6804 ctsio->residual = alloc_len - total_len; 6805 ctsio->kern_data_len = total_len; 6806 ctsio->kern_total_len = total_len; 6807 } else { 6808 ctsio->residual = 0; 6809 ctsio->kern_data_len = alloc_len; 6810 ctsio->kern_total_len = alloc_len; 6811 } 6812 6813 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6814 header->page = page_index->page_code; 6815 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6816 header->page |= SL_DS; 6817 if (page_index->subpage) { 6818 header->page |= SL_SPF; 6819 header->subpage = page_index->subpage; 6820 } 6821 scsi_ulto2b(page_index->page_len, header->datalen); 6822 6823 /* 6824 * Call the handler, if it exists, to update the 6825 * page to the latest values. 6826 */ 6827 if (page_index->sense_handler != NULL) 6828 page_index->sense_handler(ctsio, page_index, pc); 6829 6830 memcpy(header + 1, page_index->page_data, page_index->page_len); 6831 6832 ctl_set_success(ctsio); 6833 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6834 ctsio->be_move_done = ctl_config_move_done; 6835 ctl_datamove((union ctl_io *)ctsio); 6836 return (CTL_RETVAL_COMPLETE); 6837} 6838 6839int 6840ctl_read_capacity(struct ctl_scsiio *ctsio) 6841{ 6842 struct ctl_lun *lun = CTL_LUN(ctsio); 6843 struct scsi_read_capacity *cdb; 6844 struct scsi_read_capacity_data *data; 6845 uint32_t lba; 6846 6847 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6848 6849 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6850 6851 lba = scsi_4btoul(cdb->addr); 6852 if (((cdb->pmi & SRC_PMI) == 0) 6853 && (lba != 0)) { 6854 ctl_set_invalid_field(/*ctsio*/ ctsio, 6855 /*sks_valid*/ 1, 6856 /*command*/ 1, 6857 /*field*/ 2, 6858 /*bit_valid*/ 0, 6859 /*bit*/ 0); 6860 ctl_done((union ctl_io *)ctsio); 6861 return (CTL_RETVAL_COMPLETE); 6862 } 6863 6864 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6865 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6866 ctsio->residual = 0; 6867 ctsio->kern_data_len = sizeof(*data); 6868 ctsio->kern_total_len = sizeof(*data); 6869 ctsio->kern_data_resid = 0; 6870 ctsio->kern_rel_offset = 0; 6871 ctsio->kern_sg_entries = 0; 6872 6873 /* 6874 * If the maximum LBA is greater than 0xfffffffe, the user must 6875 * issue a SERVICE ACTION IN (16) command, with the read capacity 6876 * serivce action set. 6877 */ 6878 if (lun->be_lun->maxlba > 0xfffffffe) 6879 scsi_ulto4b(0xffffffff, data->addr); 6880 else 6881 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6882 6883 /* 6884 * XXX KDM this may not be 512 bytes... 6885 */ 6886 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6887 6888 ctl_set_success(ctsio); 6889 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6890 ctsio->be_move_done = ctl_config_move_done; 6891 ctl_datamove((union ctl_io *)ctsio); 6892 return (CTL_RETVAL_COMPLETE); 6893} 6894 6895int 6896ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6897{ 6898 struct ctl_lun *lun = CTL_LUN(ctsio); 6899 struct scsi_read_capacity_16 *cdb; 6900 struct scsi_read_capacity_data_long *data; 6901 uint64_t lba; 6902 uint32_t alloc_len; 6903 6904 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6905 6906 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6907 6908 alloc_len = scsi_4btoul(cdb->alloc_len); 6909 lba = scsi_8btou64(cdb->addr); 6910 6911 if ((cdb->reladr & SRC16_PMI) 6912 && (lba != 0)) { 6913 ctl_set_invalid_field(/*ctsio*/ ctsio, 6914 /*sks_valid*/ 1, 6915 /*command*/ 1, 6916 /*field*/ 2, 6917 /*bit_valid*/ 0, 6918 /*bit*/ 0); 6919 ctl_done((union ctl_io *)ctsio); 6920 return (CTL_RETVAL_COMPLETE); 6921 } 6922 6923 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6924 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6925 6926 if (sizeof(*data) < alloc_len) { 6927 ctsio->residual = alloc_len - sizeof(*data); 6928 ctsio->kern_data_len = sizeof(*data); 6929 ctsio->kern_total_len = sizeof(*data); 6930 } else { 6931 ctsio->residual = 0; 6932 ctsio->kern_data_len = alloc_len; 6933 ctsio->kern_total_len = alloc_len; 6934 } 6935 ctsio->kern_data_resid = 0; 6936 ctsio->kern_rel_offset = 0; 6937 ctsio->kern_sg_entries = 0; 6938 6939 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6940 /* XXX KDM this may not be 512 bytes... */ 6941 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6942 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6943 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6944 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6945 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6946 6947 ctl_set_success(ctsio); 6948 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6949 ctsio->be_move_done = ctl_config_move_done; 6950 ctl_datamove((union ctl_io *)ctsio); 6951 return (CTL_RETVAL_COMPLETE); 6952} 6953 6954int 6955ctl_get_lba_status(struct ctl_scsiio *ctsio) 6956{ 6957 struct ctl_lun *lun = CTL_LUN(ctsio); 6958 struct scsi_get_lba_status *cdb; 6959 struct scsi_get_lba_status_data *data; 6960 struct ctl_lba_len_flags *lbalen; 6961 uint64_t lba; 6962 uint32_t alloc_len, total_len; 6963 int retval; 6964 6965 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6966 6967 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6968 lba = scsi_8btou64(cdb->addr); 6969 alloc_len = scsi_4btoul(cdb->alloc_len); 6970 6971 if (lba > lun->be_lun->maxlba) { 6972 ctl_set_lba_out_of_range(ctsio, lba); 6973 ctl_done((union ctl_io *)ctsio); 6974 return (CTL_RETVAL_COMPLETE); 6975 } 6976 6977 total_len = sizeof(*data) + sizeof(data->descr[0]); 6978 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6979 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6980 6981 if (total_len < alloc_len) { 6982 ctsio->residual = alloc_len - total_len; 6983 ctsio->kern_data_len = total_len; 6984 ctsio->kern_total_len = total_len; 6985 } else { 6986 ctsio->residual = 0; 6987 ctsio->kern_data_len = alloc_len; 6988 ctsio->kern_total_len = alloc_len; 6989 } 6990 ctsio->kern_data_resid = 0; 6991 ctsio->kern_rel_offset = 0; 6992 ctsio->kern_sg_entries = 0; 6993 6994 /* Fill dummy data in case backend can't tell anything. */ 6995 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6996 scsi_u64to8b(lba, data->descr[0].addr); 6997 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6998 data->descr[0].length); 6999 data->descr[0].status = 0; /* Mapped or unknown. */ 7000 7001 ctl_set_success(ctsio); 7002 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7003 ctsio->be_move_done = ctl_config_move_done; 7004 7005 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7006 lbalen->lba = lba; 7007 lbalen->len = total_len; 7008 lbalen->flags = 0; 7009 retval = lun->backend->config_read((union ctl_io *)ctsio); 7010 return (CTL_RETVAL_COMPLETE); 7011} 7012 7013int 7014ctl_read_defect(struct ctl_scsiio *ctsio) 7015{ 7016 struct scsi_read_defect_data_10 *ccb10; 7017 struct scsi_read_defect_data_12 *ccb12; 7018 struct scsi_read_defect_data_hdr_10 *data10; 7019 struct scsi_read_defect_data_hdr_12 *data12; 7020 uint32_t alloc_len, data_len; 7021 uint8_t format; 7022 7023 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7024 7025 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7026 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7027 format = ccb10->format; 7028 alloc_len = scsi_2btoul(ccb10->alloc_length); 7029 data_len = sizeof(*data10); 7030 } else { 7031 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7032 format = ccb12->format; 7033 alloc_len = scsi_4btoul(ccb12->alloc_length); 7034 data_len = sizeof(*data12); 7035 } 7036 if (alloc_len == 0) { 7037 ctl_set_success(ctsio); 7038 ctl_done((union ctl_io *)ctsio); 7039 return (CTL_RETVAL_COMPLETE); 7040 } 7041 7042 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7043 if (data_len < alloc_len) { 7044 ctsio->residual = alloc_len - data_len; 7045 ctsio->kern_data_len = data_len; 7046 ctsio->kern_total_len = data_len; 7047 } else { 7048 ctsio->residual = 0; 7049 ctsio->kern_data_len = alloc_len; 7050 ctsio->kern_total_len = alloc_len; 7051 } 7052 ctsio->kern_data_resid = 0; 7053 ctsio->kern_rel_offset = 0; 7054 ctsio->kern_sg_entries = 0; 7055 7056 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7057 data10 = (struct scsi_read_defect_data_hdr_10 *) 7058 ctsio->kern_data_ptr; 7059 data10->format = format; 7060 scsi_ulto2b(0, data10->length); 7061 } else { 7062 data12 = (struct scsi_read_defect_data_hdr_12 *) 7063 ctsio->kern_data_ptr; 7064 data12->format = format; 7065 scsi_ulto2b(0, data12->generation); 7066 scsi_ulto4b(0, data12->length); 7067 } 7068 7069 ctl_set_success(ctsio); 7070 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7071 ctsio->be_move_done = ctl_config_move_done; 7072 ctl_datamove((union ctl_io *)ctsio); 7073 return (CTL_RETVAL_COMPLETE); 7074} 7075 7076int 7077ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7078{ 7079 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7080 struct ctl_lun *lun = CTL_LUN(ctsio); 7081 struct scsi_maintenance_in *cdb; 7082 int retval; 7083 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7084 int num_ha_groups, num_target_ports, shared_group; 7085 struct ctl_port *port; 7086 struct scsi_target_group_data *rtg_ptr; 7087 struct scsi_target_group_data_extended *rtg_ext_ptr; 7088 struct scsi_target_port_group_descriptor *tpg_desc; 7089 7090 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7091 7092 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7093 retval = CTL_RETVAL_COMPLETE; 7094 7095 switch (cdb->byte2 & STG_PDF_MASK) { 7096 case STG_PDF_LENGTH: 7097 ext = 0; 7098 break; 7099 case STG_PDF_EXTENDED: 7100 ext = 1; 7101 break; 7102 default: 7103 ctl_set_invalid_field(/*ctsio*/ ctsio, 7104 /*sks_valid*/ 1, 7105 /*command*/ 1, 7106 /*field*/ 2, 7107 /*bit_valid*/ 1, 7108 /*bit*/ 5); 7109 ctl_done((union ctl_io *)ctsio); 7110 return(retval); 7111 } 7112 7113 num_target_ports = 0; 7114 shared_group = (softc->is_single != 0); 7115 mtx_lock(&softc->ctl_lock); 7116 STAILQ_FOREACH(port, &softc->port_list, links) { 7117 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7118 continue; 7119 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7120 continue; 7121 num_target_ports++; 7122 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7123 shared_group = 1; 7124 } 7125 mtx_unlock(&softc->ctl_lock); 7126 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7127 7128 if (ext) 7129 total_len = sizeof(struct scsi_target_group_data_extended); 7130 else 7131 total_len = sizeof(struct scsi_target_group_data); 7132 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7133 (shared_group + num_ha_groups) + 7134 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7135 7136 alloc_len = scsi_4btoul(cdb->length); 7137 7138 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7139 7140 ctsio->kern_sg_entries = 0; 7141 7142 if (total_len < alloc_len) { 7143 ctsio->residual = alloc_len - total_len; 7144 ctsio->kern_data_len = total_len; 7145 ctsio->kern_total_len = total_len; 7146 } else { 7147 ctsio->residual = 0; 7148 ctsio->kern_data_len = alloc_len; 7149 ctsio->kern_total_len = alloc_len; 7150 } 7151 ctsio->kern_data_resid = 0; 7152 ctsio->kern_rel_offset = 0; 7153 7154 if (ext) { 7155 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7156 ctsio->kern_data_ptr; 7157 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7158 rtg_ext_ptr->format_type = 0x10; 7159 rtg_ext_ptr->implicit_transition_time = 0; 7160 tpg_desc = &rtg_ext_ptr->groups[0]; 7161 } else { 7162 rtg_ptr = (struct scsi_target_group_data *) 7163 ctsio->kern_data_ptr; 7164 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7165 tpg_desc = &rtg_ptr->groups[0]; 7166 } 7167 7168 mtx_lock(&softc->ctl_lock); 7169 pg = softc->port_min / softc->port_cnt; 7170 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7171 /* Some shelf is known to be primary. */ 7172 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7173 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7174 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7175 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7176 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7177 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7178 else 7179 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7180 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7181 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7182 } else { 7183 ts = os; 7184 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7185 } 7186 } else { 7187 /* No known primary shelf. */ 7188 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7189 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7190 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7191 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7192 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7193 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7194 } else { 7195 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7196 } 7197 } 7198 if (shared_group) { 7199 tpg_desc->pref_state = ts; 7200 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7201 TPG_U_SUP | TPG_T_SUP; 7202 scsi_ulto2b(1, tpg_desc->target_port_group); 7203 tpg_desc->status = TPG_IMPLICIT; 7204 pc = 0; 7205 STAILQ_FOREACH(port, &softc->port_list, links) { 7206 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7207 continue; 7208 if (!softc->is_single && 7209 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7210 continue; 7211 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7212 continue; 7213 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7214 relative_target_port_identifier); 7215 pc++; 7216 } 7217 tpg_desc->target_port_count = pc; 7218 tpg_desc = (struct scsi_target_port_group_descriptor *) 7219 &tpg_desc->descriptors[pc]; 7220 } 7221 for (g = 0; g < num_ha_groups; g++) { 7222 tpg_desc->pref_state = (g == pg) ? ts : os; 7223 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7224 TPG_U_SUP | TPG_T_SUP; 7225 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7226 tpg_desc->status = TPG_IMPLICIT; 7227 pc = 0; 7228 STAILQ_FOREACH(port, &softc->port_list, links) { 7229 if (port->targ_port < g * softc->port_cnt || 7230 port->targ_port >= (g + 1) * softc->port_cnt) 7231 continue; 7232 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7233 continue; 7234 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7235 continue; 7236 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7237 continue; 7238 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7239 relative_target_port_identifier); 7240 pc++; 7241 } 7242 tpg_desc->target_port_count = pc; 7243 tpg_desc = (struct scsi_target_port_group_descriptor *) 7244 &tpg_desc->descriptors[pc]; 7245 } 7246 mtx_unlock(&softc->ctl_lock); 7247 7248 ctl_set_success(ctsio); 7249 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7250 ctsio->be_move_done = ctl_config_move_done; 7251 ctl_datamove((union ctl_io *)ctsio); 7252 return(retval); 7253} 7254 7255int 7256ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7257{ 7258 struct ctl_lun *lun = CTL_LUN(ctsio); 7259 struct scsi_report_supported_opcodes *cdb; 7260 const struct ctl_cmd_entry *entry, *sentry; 7261 struct scsi_report_supported_opcodes_all *all; 7262 struct scsi_report_supported_opcodes_descr *descr; 7263 struct scsi_report_supported_opcodes_one *one; 7264 int retval; 7265 int alloc_len, total_len; 7266 int opcode, service_action, i, j, num; 7267 7268 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7269 7270 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7271 retval = CTL_RETVAL_COMPLETE; 7272 7273 opcode = cdb->requested_opcode; 7274 service_action = scsi_2btoul(cdb->requested_service_action); 7275 switch (cdb->options & RSO_OPTIONS_MASK) { 7276 case RSO_OPTIONS_ALL: 7277 num = 0; 7278 for (i = 0; i < 256; i++) { 7279 entry = &ctl_cmd_table[i]; 7280 if (entry->flags & CTL_CMD_FLAG_SA5) { 7281 for (j = 0; j < 32; j++) { 7282 sentry = &((const struct ctl_cmd_entry *) 7283 entry->execute)[j]; 7284 if (ctl_cmd_applicable( 7285 lun->be_lun->lun_type, sentry)) 7286 num++; 7287 } 7288 } else { 7289 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7290 entry)) 7291 num++; 7292 } 7293 } 7294 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7295 num * sizeof(struct scsi_report_supported_opcodes_descr); 7296 break; 7297 case RSO_OPTIONS_OC: 7298 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7299 ctl_set_invalid_field(/*ctsio*/ ctsio, 7300 /*sks_valid*/ 1, 7301 /*command*/ 1, 7302 /*field*/ 2, 7303 /*bit_valid*/ 1, 7304 /*bit*/ 2); 7305 ctl_done((union ctl_io *)ctsio); 7306 return (CTL_RETVAL_COMPLETE); 7307 } 7308 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7309 break; 7310 case RSO_OPTIONS_OC_SA: 7311 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7312 service_action >= 32) { 7313 ctl_set_invalid_field(/*ctsio*/ ctsio, 7314 /*sks_valid*/ 1, 7315 /*command*/ 1, 7316 /*field*/ 2, 7317 /*bit_valid*/ 1, 7318 /*bit*/ 2); 7319 ctl_done((union ctl_io *)ctsio); 7320 return (CTL_RETVAL_COMPLETE); 7321 } 7322 /* FALLTHROUGH */ 7323 case RSO_OPTIONS_OC_ASA: 7324 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7325 break; 7326 default: 7327 ctl_set_invalid_field(/*ctsio*/ ctsio, 7328 /*sks_valid*/ 1, 7329 /*command*/ 1, 7330 /*field*/ 2, 7331 /*bit_valid*/ 1, 7332 /*bit*/ 2); 7333 ctl_done((union ctl_io *)ctsio); 7334 return (CTL_RETVAL_COMPLETE); 7335 } 7336 7337 alloc_len = scsi_4btoul(cdb->length); 7338 7339 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7340 7341 ctsio->kern_sg_entries = 0; 7342 7343 if (total_len < alloc_len) { 7344 ctsio->residual = alloc_len - total_len; 7345 ctsio->kern_data_len = total_len; 7346 ctsio->kern_total_len = total_len; 7347 } else { 7348 ctsio->residual = 0; 7349 ctsio->kern_data_len = alloc_len; 7350 ctsio->kern_total_len = alloc_len; 7351 } 7352 ctsio->kern_data_resid = 0; 7353 ctsio->kern_rel_offset = 0; 7354 7355 switch (cdb->options & RSO_OPTIONS_MASK) { 7356 case RSO_OPTIONS_ALL: 7357 all = (struct scsi_report_supported_opcodes_all *) 7358 ctsio->kern_data_ptr; 7359 num = 0; 7360 for (i = 0; i < 256; i++) { 7361 entry = &ctl_cmd_table[i]; 7362 if (entry->flags & CTL_CMD_FLAG_SA5) { 7363 for (j = 0; j < 32; j++) { 7364 sentry = &((const struct ctl_cmd_entry *) 7365 entry->execute)[j]; 7366 if (!ctl_cmd_applicable( 7367 lun->be_lun->lun_type, sentry)) 7368 continue; 7369 descr = &all->descr[num++]; 7370 descr->opcode = i; 7371 scsi_ulto2b(j, descr->service_action); 7372 descr->flags = RSO_SERVACTV; 7373 scsi_ulto2b(sentry->length, 7374 descr->cdb_length); 7375 } 7376 } else { 7377 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7378 entry)) 7379 continue; 7380 descr = &all->descr[num++]; 7381 descr->opcode = i; 7382 scsi_ulto2b(0, descr->service_action); 7383 descr->flags = 0; 7384 scsi_ulto2b(entry->length, descr->cdb_length); 7385 } 7386 } 7387 scsi_ulto4b( 7388 num * sizeof(struct scsi_report_supported_opcodes_descr), 7389 all->length); 7390 break; 7391 case RSO_OPTIONS_OC: 7392 one = (struct scsi_report_supported_opcodes_one *) 7393 ctsio->kern_data_ptr; 7394 entry = &ctl_cmd_table[opcode]; 7395 goto fill_one; 7396 case RSO_OPTIONS_OC_SA: 7397 one = (struct scsi_report_supported_opcodes_one *) 7398 ctsio->kern_data_ptr; 7399 entry = &ctl_cmd_table[opcode]; 7400 entry = &((const struct ctl_cmd_entry *) 7401 entry->execute)[service_action]; 7402fill_one: 7403 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7404 one->support = 3; 7405 scsi_ulto2b(entry->length, one->cdb_length); 7406 one->cdb_usage[0] = opcode; 7407 memcpy(&one->cdb_usage[1], entry->usage, 7408 entry->length - 1); 7409 } else 7410 one->support = 1; 7411 break; 7412 case RSO_OPTIONS_OC_ASA: 7413 one = (struct scsi_report_supported_opcodes_one *) 7414 ctsio->kern_data_ptr; 7415 entry = &ctl_cmd_table[opcode]; 7416 if (entry->flags & CTL_CMD_FLAG_SA5) { 7417 entry = &((const struct ctl_cmd_entry *) 7418 entry->execute)[service_action]; 7419 } else if (service_action != 0) { 7420 one->support = 1; 7421 break; 7422 } 7423 goto fill_one; 7424 } 7425 7426 ctl_set_success(ctsio); 7427 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7428 ctsio->be_move_done = ctl_config_move_done; 7429 ctl_datamove((union ctl_io *)ctsio); 7430 return(retval); 7431} 7432 7433int 7434ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7435{ 7436 struct scsi_report_supported_tmf *cdb; 7437 struct scsi_report_supported_tmf_ext_data *data; 7438 int retval; 7439 int alloc_len, total_len; 7440 7441 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7442 7443 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7444 7445 retval = CTL_RETVAL_COMPLETE; 7446 7447 if (cdb->options & RST_REPD) 7448 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7449 else 7450 total_len = sizeof(struct scsi_report_supported_tmf_data); 7451 alloc_len = scsi_4btoul(cdb->length); 7452 7453 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7454 7455 ctsio->kern_sg_entries = 0; 7456 7457 if (total_len < alloc_len) { 7458 ctsio->residual = alloc_len - total_len; 7459 ctsio->kern_data_len = total_len; 7460 ctsio->kern_total_len = total_len; 7461 } else { 7462 ctsio->residual = 0; 7463 ctsio->kern_data_len = alloc_len; 7464 ctsio->kern_total_len = alloc_len; 7465 } 7466 ctsio->kern_data_resid = 0; 7467 ctsio->kern_rel_offset = 0; 7468 7469 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7470 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7471 RST_TRS; 7472 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7473 data->length = total_len - 4; 7474 7475 ctl_set_success(ctsio); 7476 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7477 ctsio->be_move_done = ctl_config_move_done; 7478 ctl_datamove((union ctl_io *)ctsio); 7479 return (retval); 7480} 7481 7482int 7483ctl_report_timestamp(struct ctl_scsiio *ctsio) 7484{ 7485 struct scsi_report_timestamp *cdb; 7486 struct scsi_report_timestamp_data *data; 7487 struct timeval tv; 7488 int64_t timestamp; 7489 int retval; 7490 int alloc_len, total_len; 7491 7492 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7493 7494 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7495 7496 retval = CTL_RETVAL_COMPLETE; 7497 7498 total_len = sizeof(struct scsi_report_timestamp_data); 7499 alloc_len = scsi_4btoul(cdb->length); 7500 7501 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7502 7503 ctsio->kern_sg_entries = 0; 7504 7505 if (total_len < alloc_len) { 7506 ctsio->residual = alloc_len - total_len; 7507 ctsio->kern_data_len = total_len; 7508 ctsio->kern_total_len = total_len; 7509 } else { 7510 ctsio->residual = 0; 7511 ctsio->kern_data_len = alloc_len; 7512 ctsio->kern_total_len = alloc_len; 7513 } 7514 ctsio->kern_data_resid = 0; 7515 ctsio->kern_rel_offset = 0; 7516 7517 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7518 scsi_ulto2b(sizeof(*data) - 2, data->length); 7519 data->origin = RTS_ORIG_OUTSIDE; 7520 getmicrotime(&tv); 7521 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7522 scsi_ulto4b(timestamp >> 16, data->timestamp); 7523 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7524 7525 ctl_set_success(ctsio); 7526 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7527 ctsio->be_move_done = ctl_config_move_done; 7528 ctl_datamove((union ctl_io *)ctsio); 7529 return (retval); 7530} 7531 7532int 7533ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7534{ 7535 struct ctl_softc *softc = CTL_SOFTC(ctsio); 7536 struct ctl_lun *lun = CTL_LUN(ctsio); 7537 struct scsi_per_res_in *cdb; 7538 int alloc_len, total_len = 0; 7539 /* struct scsi_per_res_in_rsrv in_data; */ 7540 uint64_t key; 7541 7542 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7543 7544 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7545 7546 alloc_len = scsi_2btoul(cdb->length); 7547 7548retry: 7549 mtx_lock(&lun->lun_lock); 7550 switch (cdb->action) { 7551 case SPRI_RK: /* read keys */ 7552 total_len = sizeof(struct scsi_per_res_in_keys) + 7553 lun->pr_key_count * 7554 sizeof(struct scsi_per_res_key); 7555 break; 7556 case SPRI_RR: /* read reservation */ 7557 if (lun->flags & CTL_LUN_PR_RESERVED) 7558 total_len = sizeof(struct scsi_per_res_in_rsrv); 7559 else 7560 total_len = sizeof(struct scsi_per_res_in_header); 7561 break; 7562 case SPRI_RC: /* report capabilities */ 7563 total_len = sizeof(struct scsi_per_res_cap); 7564 break; 7565 case SPRI_RS: /* read full status */ 7566 total_len = sizeof(struct scsi_per_res_in_header) + 7567 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7568 lun->pr_key_count; 7569 break; 7570 default: 7571 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7572 } 7573 mtx_unlock(&lun->lun_lock); 7574 7575 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7576 7577 if (total_len < alloc_len) { 7578 ctsio->residual = alloc_len - total_len; 7579 ctsio->kern_data_len = total_len; 7580 ctsio->kern_total_len = total_len; 7581 } else { 7582 ctsio->residual = 0; 7583 ctsio->kern_data_len = alloc_len; 7584 ctsio->kern_total_len = alloc_len; 7585 } 7586 7587 ctsio->kern_data_resid = 0; 7588 ctsio->kern_rel_offset = 0; 7589 ctsio->kern_sg_entries = 0; 7590 7591 mtx_lock(&lun->lun_lock); 7592 switch (cdb->action) { 7593 case SPRI_RK: { // read keys 7594 struct scsi_per_res_in_keys *res_keys; 7595 int i, key_count; 7596 7597 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7598 7599 /* 7600 * We had to drop the lock to allocate our buffer, which 7601 * leaves time for someone to come in with another 7602 * persistent reservation. (That is unlikely, though, 7603 * since this should be the only persistent reservation 7604 * command active right now.) 7605 */ 7606 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7607 (lun->pr_key_count * 7608 sizeof(struct scsi_per_res_key)))){ 7609 mtx_unlock(&lun->lun_lock); 7610 free(ctsio->kern_data_ptr, M_CTL); 7611 printf("%s: reservation length changed, retrying\n", 7612 __func__); 7613 goto retry; 7614 } 7615 7616 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7617 7618 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7619 lun->pr_key_count, res_keys->header.length); 7620 7621 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7622 if ((key = ctl_get_prkey(lun, i)) == 0) 7623 continue; 7624 7625 /* 7626 * We used lun->pr_key_count to calculate the 7627 * size to allocate. If it turns out the number of 7628 * initiators with the registered flag set is 7629 * larger than that (i.e. they haven't been kept in 7630 * sync), we've got a problem. 7631 */ 7632 if (key_count >= lun->pr_key_count) { 7633 key_count++; 7634 continue; 7635 } 7636 scsi_u64to8b(key, res_keys->keys[key_count].key); 7637 key_count++; 7638 } 7639 break; 7640 } 7641 case SPRI_RR: { // read reservation 7642 struct scsi_per_res_in_rsrv *res; 7643 int tmp_len, header_only; 7644 7645 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7646 7647 scsi_ulto4b(lun->pr_generation, res->header.generation); 7648 7649 if (lun->flags & CTL_LUN_PR_RESERVED) 7650 { 7651 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7652 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7653 res->header.length); 7654 header_only = 0; 7655 } else { 7656 tmp_len = sizeof(struct scsi_per_res_in_header); 7657 scsi_ulto4b(0, res->header.length); 7658 header_only = 1; 7659 } 7660 7661 /* 7662 * We had to drop the lock to allocate our buffer, which 7663 * leaves time for someone to come in with another 7664 * persistent reservation. (That is unlikely, though, 7665 * since this should be the only persistent reservation 7666 * command active right now.) 7667 */ 7668 if (tmp_len != total_len) { 7669 mtx_unlock(&lun->lun_lock); 7670 free(ctsio->kern_data_ptr, M_CTL); 7671 printf("%s: reservation status changed, retrying\n", 7672 __func__); 7673 goto retry; 7674 } 7675 7676 /* 7677 * No reservation held, so we're done. 7678 */ 7679 if (header_only != 0) 7680 break; 7681 7682 /* 7683 * If the registration is an All Registrants type, the key 7684 * is 0, since it doesn't really matter. 7685 */ 7686 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7687 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7688 res->data.reservation); 7689 } 7690 res->data.scopetype = lun->pr_res_type; 7691 break; 7692 } 7693 case SPRI_RC: //report capabilities 7694 { 7695 struct scsi_per_res_cap *res_cap; 7696 uint16_t type_mask; 7697 7698 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7699 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7700 res_cap->flags1 = SPRI_CRH; 7701 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7702 type_mask = SPRI_TM_WR_EX_AR | 7703 SPRI_TM_EX_AC_RO | 7704 SPRI_TM_WR_EX_RO | 7705 SPRI_TM_EX_AC | 7706 SPRI_TM_WR_EX | 7707 SPRI_TM_EX_AC_AR; 7708 scsi_ulto2b(type_mask, res_cap->type_mask); 7709 break; 7710 } 7711 case SPRI_RS: { // read full status 7712 struct scsi_per_res_in_full *res_status; 7713 struct scsi_per_res_in_full_desc *res_desc; 7714 struct ctl_port *port; 7715 int i, len; 7716 7717 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7718 7719 /* 7720 * We had to drop the lock to allocate our buffer, which 7721 * leaves time for someone to come in with another 7722 * persistent reservation. (That is unlikely, though, 7723 * since this should be the only persistent reservation 7724 * command active right now.) 7725 */ 7726 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7727 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7728 lun->pr_key_count)){ 7729 mtx_unlock(&lun->lun_lock); 7730 free(ctsio->kern_data_ptr, M_CTL); 7731 printf("%s: reservation length changed, retrying\n", 7732 __func__); 7733 goto retry; 7734 } 7735 7736 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7737 7738 res_desc = &res_status->desc[0]; 7739 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7740 if ((key = ctl_get_prkey(lun, i)) == 0) 7741 continue; 7742 7743 scsi_u64to8b(key, res_desc->res_key.key); 7744 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7745 (lun->pr_res_idx == i || 7746 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7747 res_desc->flags = SPRI_FULL_R_HOLDER; 7748 res_desc->scopetype = lun->pr_res_type; 7749 } 7750 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7751 res_desc->rel_trgt_port_id); 7752 len = 0; 7753 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7754 if (port != NULL) 7755 len = ctl_create_iid(port, 7756 i % CTL_MAX_INIT_PER_PORT, 7757 res_desc->transport_id); 7758 scsi_ulto4b(len, res_desc->additional_length); 7759 res_desc = (struct scsi_per_res_in_full_desc *) 7760 &res_desc->transport_id[len]; 7761 } 7762 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7763 res_status->header.length); 7764 break; 7765 } 7766 default: 7767 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7768 } 7769 mtx_unlock(&lun->lun_lock); 7770 7771 ctl_set_success(ctsio); 7772 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7773 ctsio->be_move_done = ctl_config_move_done; 7774 ctl_datamove((union ctl_io *)ctsio); 7775 return (CTL_RETVAL_COMPLETE); 7776} 7777 7778/* 7779 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7780 * it should return. 7781 */ 7782static int 7783ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7784 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7785 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7786 struct scsi_per_res_out_parms* param) 7787{ 7788 union ctl_ha_msg persis_io; 7789 int i; 7790 7791 mtx_lock(&lun->lun_lock); 7792 if (sa_res_key == 0) { 7793 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7794 /* validate scope and type */ 7795 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7796 SPR_LU_SCOPE) { 7797 mtx_unlock(&lun->lun_lock); 7798 ctl_set_invalid_field(/*ctsio*/ ctsio, 7799 /*sks_valid*/ 1, 7800 /*command*/ 1, 7801 /*field*/ 2, 7802 /*bit_valid*/ 1, 7803 /*bit*/ 4); 7804 ctl_done((union ctl_io *)ctsio); 7805 return (1); 7806 } 7807 7808 if (type>8 || type==2 || type==4 || type==0) { 7809 mtx_unlock(&lun->lun_lock); 7810 ctl_set_invalid_field(/*ctsio*/ ctsio, 7811 /*sks_valid*/ 1, 7812 /*command*/ 1, 7813 /*field*/ 2, 7814 /*bit_valid*/ 1, 7815 /*bit*/ 0); 7816 ctl_done((union ctl_io *)ctsio); 7817 return (1); 7818 } 7819 7820 /* 7821 * Unregister everybody else and build UA for 7822 * them 7823 */ 7824 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7825 if (i == residx || ctl_get_prkey(lun, i) == 0) 7826 continue; 7827 7828 ctl_clr_prkey(lun, i); 7829 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7830 } 7831 lun->pr_key_count = 1; 7832 lun->pr_res_type = type; 7833 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7834 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7835 lun->pr_res_idx = residx; 7836 lun->pr_generation++; 7837 mtx_unlock(&lun->lun_lock); 7838 7839 /* send msg to other side */ 7840 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7841 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7842 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7843 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7844 persis_io.pr.pr_info.res_type = type; 7845 memcpy(persis_io.pr.pr_info.sa_res_key, 7846 param->serv_act_res_key, 7847 sizeof(param->serv_act_res_key)); 7848 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7849 sizeof(persis_io.pr), M_WAITOK); 7850 } else { 7851 /* not all registrants */ 7852 mtx_unlock(&lun->lun_lock); 7853 free(ctsio->kern_data_ptr, M_CTL); 7854 ctl_set_invalid_field(ctsio, 7855 /*sks_valid*/ 1, 7856 /*command*/ 0, 7857 /*field*/ 8, 7858 /*bit_valid*/ 0, 7859 /*bit*/ 0); 7860 ctl_done((union ctl_io *)ctsio); 7861 return (1); 7862 } 7863 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7864 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7865 int found = 0; 7866 7867 if (res_key == sa_res_key) { 7868 /* special case */ 7869 /* 7870 * The spec implies this is not good but doesn't 7871 * say what to do. There are two choices either 7872 * generate a res conflict or check condition 7873 * with illegal field in parameter data. Since 7874 * that is what is done when the sa_res_key is 7875 * zero I'll take that approach since this has 7876 * to do with the sa_res_key. 7877 */ 7878 mtx_unlock(&lun->lun_lock); 7879 free(ctsio->kern_data_ptr, M_CTL); 7880 ctl_set_invalid_field(ctsio, 7881 /*sks_valid*/ 1, 7882 /*command*/ 0, 7883 /*field*/ 8, 7884 /*bit_valid*/ 0, 7885 /*bit*/ 0); 7886 ctl_done((union ctl_io *)ctsio); 7887 return (1); 7888 } 7889 7890 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7891 if (ctl_get_prkey(lun, i) != sa_res_key) 7892 continue; 7893 7894 found = 1; 7895 ctl_clr_prkey(lun, i); 7896 lun->pr_key_count--; 7897 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7898 } 7899 if (!found) { 7900 mtx_unlock(&lun->lun_lock); 7901 free(ctsio->kern_data_ptr, M_CTL); 7902 ctl_set_reservation_conflict(ctsio); 7903 ctl_done((union ctl_io *)ctsio); 7904 return (CTL_RETVAL_COMPLETE); 7905 } 7906 lun->pr_generation++; 7907 mtx_unlock(&lun->lun_lock); 7908 7909 /* send msg to other side */ 7910 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7911 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7912 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7913 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7914 persis_io.pr.pr_info.res_type = type; 7915 memcpy(persis_io.pr.pr_info.sa_res_key, 7916 param->serv_act_res_key, 7917 sizeof(param->serv_act_res_key)); 7918 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7919 sizeof(persis_io.pr), M_WAITOK); 7920 } else { 7921 /* Reserved but not all registrants */ 7922 /* sa_res_key is res holder */ 7923 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7924 /* validate scope and type */ 7925 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7926 SPR_LU_SCOPE) { 7927 mtx_unlock(&lun->lun_lock); 7928 ctl_set_invalid_field(/*ctsio*/ ctsio, 7929 /*sks_valid*/ 1, 7930 /*command*/ 1, 7931 /*field*/ 2, 7932 /*bit_valid*/ 1, 7933 /*bit*/ 4); 7934 ctl_done((union ctl_io *)ctsio); 7935 return (1); 7936 } 7937 7938 if (type>8 || type==2 || type==4 || type==0) { 7939 mtx_unlock(&lun->lun_lock); 7940 ctl_set_invalid_field(/*ctsio*/ ctsio, 7941 /*sks_valid*/ 1, 7942 /*command*/ 1, 7943 /*field*/ 2, 7944 /*bit_valid*/ 1, 7945 /*bit*/ 0); 7946 ctl_done((union ctl_io *)ctsio); 7947 return (1); 7948 } 7949 7950 /* 7951 * Do the following: 7952 * if sa_res_key != res_key remove all 7953 * registrants w/sa_res_key and generate UA 7954 * for these registrants(Registrations 7955 * Preempted) if it wasn't an exclusive 7956 * reservation generate UA(Reservations 7957 * Preempted) for all other registered nexuses 7958 * if the type has changed. Establish the new 7959 * reservation and holder. If res_key and 7960 * sa_res_key are the same do the above 7961 * except don't unregister the res holder. 7962 */ 7963 7964 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7965 if (i == residx || ctl_get_prkey(lun, i) == 0) 7966 continue; 7967 7968 if (sa_res_key == ctl_get_prkey(lun, i)) { 7969 ctl_clr_prkey(lun, i); 7970 lun->pr_key_count--; 7971 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7972 } else if (type != lun->pr_res_type && 7973 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 7974 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 7975 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7976 } 7977 } 7978 lun->pr_res_type = type; 7979 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7980 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7981 lun->pr_res_idx = residx; 7982 else 7983 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7984 lun->pr_generation++; 7985 mtx_unlock(&lun->lun_lock); 7986 7987 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7988 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7989 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7990 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7991 persis_io.pr.pr_info.res_type = type; 7992 memcpy(persis_io.pr.pr_info.sa_res_key, 7993 param->serv_act_res_key, 7994 sizeof(param->serv_act_res_key)); 7995 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7996 sizeof(persis_io.pr), M_WAITOK); 7997 } else { 7998 /* 7999 * sa_res_key is not the res holder just 8000 * remove registrants 8001 */ 8002 int found=0; 8003 8004 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8005 if (sa_res_key != ctl_get_prkey(lun, i)) 8006 continue; 8007 8008 found = 1; 8009 ctl_clr_prkey(lun, i); 8010 lun->pr_key_count--; 8011 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8012 } 8013 8014 if (!found) { 8015 mtx_unlock(&lun->lun_lock); 8016 free(ctsio->kern_data_ptr, M_CTL); 8017 ctl_set_reservation_conflict(ctsio); 8018 ctl_done((union ctl_io *)ctsio); 8019 return (1); 8020 } 8021 lun->pr_generation++; 8022 mtx_unlock(&lun->lun_lock); 8023 8024 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8025 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8026 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8027 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8028 persis_io.pr.pr_info.res_type = type; 8029 memcpy(persis_io.pr.pr_info.sa_res_key, 8030 param->serv_act_res_key, 8031 sizeof(param->serv_act_res_key)); 8032 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8033 sizeof(persis_io.pr), M_WAITOK); 8034 } 8035 } 8036 return (0); 8037} 8038 8039static void 8040ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8041{ 8042 uint64_t sa_res_key; 8043 int i; 8044 8045 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8046 8047 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8048 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8049 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8050 if (sa_res_key == 0) { 8051 /* 8052 * Unregister everybody else and build UA for 8053 * them 8054 */ 8055 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8056 if (i == msg->pr.pr_info.residx || 8057 ctl_get_prkey(lun, i) == 0) 8058 continue; 8059 8060 ctl_clr_prkey(lun, i); 8061 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8062 } 8063 8064 lun->pr_key_count = 1; 8065 lun->pr_res_type = msg->pr.pr_info.res_type; 8066 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8067 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8068 lun->pr_res_idx = msg->pr.pr_info.residx; 8069 } else { 8070 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8071 if (sa_res_key == ctl_get_prkey(lun, i)) 8072 continue; 8073 8074 ctl_clr_prkey(lun, i); 8075 lun->pr_key_count--; 8076 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8077 } 8078 } 8079 } else { 8080 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8081 if (i == msg->pr.pr_info.residx || 8082 ctl_get_prkey(lun, i) == 0) 8083 continue; 8084 8085 if (sa_res_key == ctl_get_prkey(lun, i)) { 8086 ctl_clr_prkey(lun, i); 8087 lun->pr_key_count--; 8088 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8089 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8090 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8091 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8092 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8093 } 8094 } 8095 lun->pr_res_type = msg->pr.pr_info.res_type; 8096 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8097 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8098 lun->pr_res_idx = msg->pr.pr_info.residx; 8099 else 8100 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8101 } 8102 lun->pr_generation++; 8103 8104} 8105 8106 8107int 8108ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8109{ 8110 struct ctl_softc *softc = CTL_SOFTC(ctsio); 8111 struct ctl_lun *lun = CTL_LUN(ctsio); 8112 int retval; 8113 u_int32_t param_len; 8114 struct scsi_per_res_out *cdb; 8115 struct scsi_per_res_out_parms* param; 8116 uint32_t residx; 8117 uint64_t res_key, sa_res_key, key; 8118 uint8_t type; 8119 union ctl_ha_msg persis_io; 8120 int i; 8121 8122 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8123 8124 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8125 retval = CTL_RETVAL_COMPLETE; 8126 8127 /* 8128 * We only support whole-LUN scope. The scope & type are ignored for 8129 * register, register and ignore existing key and clear. 8130 * We sometimes ignore scope and type on preempts too!! 8131 * Verify reservation type here as well. 8132 */ 8133 type = cdb->scope_type & SPR_TYPE_MASK; 8134 if ((cdb->action == SPRO_RESERVE) 8135 || (cdb->action == SPRO_RELEASE)) { 8136 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8137 ctl_set_invalid_field(/*ctsio*/ ctsio, 8138 /*sks_valid*/ 1, 8139 /*command*/ 1, 8140 /*field*/ 2, 8141 /*bit_valid*/ 1, 8142 /*bit*/ 4); 8143 ctl_done((union ctl_io *)ctsio); 8144 return (CTL_RETVAL_COMPLETE); 8145 } 8146 8147 if (type>8 || type==2 || type==4 || type==0) { 8148 ctl_set_invalid_field(/*ctsio*/ ctsio, 8149 /*sks_valid*/ 1, 8150 /*command*/ 1, 8151 /*field*/ 2, 8152 /*bit_valid*/ 1, 8153 /*bit*/ 0); 8154 ctl_done((union ctl_io *)ctsio); 8155 return (CTL_RETVAL_COMPLETE); 8156 } 8157 } 8158 8159 param_len = scsi_4btoul(cdb->length); 8160 8161 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8162 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8163 ctsio->kern_data_len = param_len; 8164 ctsio->kern_total_len = param_len; 8165 ctsio->kern_data_resid = 0; 8166 ctsio->kern_rel_offset = 0; 8167 ctsio->kern_sg_entries = 0; 8168 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8169 ctsio->be_move_done = ctl_config_move_done; 8170 ctl_datamove((union ctl_io *)ctsio); 8171 8172 return (CTL_RETVAL_COMPLETE); 8173 } 8174 8175 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8176 8177 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8178 res_key = scsi_8btou64(param->res_key.key); 8179 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8180 8181 /* 8182 * Validate the reservation key here except for SPRO_REG_IGNO 8183 * This must be done for all other service actions 8184 */ 8185 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8186 mtx_lock(&lun->lun_lock); 8187 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8188 if (res_key != key) { 8189 /* 8190 * The current key passed in doesn't match 8191 * the one the initiator previously 8192 * registered. 8193 */ 8194 mtx_unlock(&lun->lun_lock); 8195 free(ctsio->kern_data_ptr, M_CTL); 8196 ctl_set_reservation_conflict(ctsio); 8197 ctl_done((union ctl_io *)ctsio); 8198 return (CTL_RETVAL_COMPLETE); 8199 } 8200 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8201 /* 8202 * We are not registered 8203 */ 8204 mtx_unlock(&lun->lun_lock); 8205 free(ctsio->kern_data_ptr, M_CTL); 8206 ctl_set_reservation_conflict(ctsio); 8207 ctl_done((union ctl_io *)ctsio); 8208 return (CTL_RETVAL_COMPLETE); 8209 } else if (res_key != 0) { 8210 /* 8211 * We are not registered and trying to register but 8212 * the register key isn't zero. 8213 */ 8214 mtx_unlock(&lun->lun_lock); 8215 free(ctsio->kern_data_ptr, M_CTL); 8216 ctl_set_reservation_conflict(ctsio); 8217 ctl_done((union ctl_io *)ctsio); 8218 return (CTL_RETVAL_COMPLETE); 8219 } 8220 mtx_unlock(&lun->lun_lock); 8221 } 8222 8223 switch (cdb->action & SPRO_ACTION_MASK) { 8224 case SPRO_REGISTER: 8225 case SPRO_REG_IGNO: { 8226 8227#if 0 8228 printf("Registration received\n"); 8229#endif 8230 8231 /* 8232 * We don't support any of these options, as we report in 8233 * the read capabilities request (see 8234 * ctl_persistent_reserve_in(), above). 8235 */ 8236 if ((param->flags & SPR_SPEC_I_PT) 8237 || (param->flags & SPR_ALL_TG_PT) 8238 || (param->flags & SPR_APTPL)) { 8239 int bit_ptr; 8240 8241 if (param->flags & SPR_APTPL) 8242 bit_ptr = 0; 8243 else if (param->flags & SPR_ALL_TG_PT) 8244 bit_ptr = 2; 8245 else /* SPR_SPEC_I_PT */ 8246 bit_ptr = 3; 8247 8248 free(ctsio->kern_data_ptr, M_CTL); 8249 ctl_set_invalid_field(ctsio, 8250 /*sks_valid*/ 1, 8251 /*command*/ 0, 8252 /*field*/ 20, 8253 /*bit_valid*/ 1, 8254 /*bit*/ bit_ptr); 8255 ctl_done((union ctl_io *)ctsio); 8256 return (CTL_RETVAL_COMPLETE); 8257 } 8258 8259 mtx_lock(&lun->lun_lock); 8260 8261 /* 8262 * The initiator wants to clear the 8263 * key/unregister. 8264 */ 8265 if (sa_res_key == 0) { 8266 if ((res_key == 0 8267 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8268 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8269 && ctl_get_prkey(lun, residx) == 0)) { 8270 mtx_unlock(&lun->lun_lock); 8271 goto done; 8272 } 8273 8274 ctl_clr_prkey(lun, residx); 8275 lun->pr_key_count--; 8276 8277 if (residx == lun->pr_res_idx) { 8278 lun->flags &= ~CTL_LUN_PR_RESERVED; 8279 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8280 8281 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8282 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8283 lun->pr_key_count) { 8284 /* 8285 * If the reservation is a registrants 8286 * only type we need to generate a UA 8287 * for other registered inits. The 8288 * sense code should be RESERVATIONS 8289 * RELEASED 8290 */ 8291 8292 for (i = softc->init_min; i < softc->init_max; i++){ 8293 if (ctl_get_prkey(lun, i) == 0) 8294 continue; 8295 ctl_est_ua(lun, i, 8296 CTL_UA_RES_RELEASE); 8297 } 8298 } 8299 lun->pr_res_type = 0; 8300 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8301 if (lun->pr_key_count==0) { 8302 lun->flags &= ~CTL_LUN_PR_RESERVED; 8303 lun->pr_res_type = 0; 8304 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8305 } 8306 } 8307 lun->pr_generation++; 8308 mtx_unlock(&lun->lun_lock); 8309 8310 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8311 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8312 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8313 persis_io.pr.pr_info.residx = residx; 8314 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8315 sizeof(persis_io.pr), M_WAITOK); 8316 } else /* sa_res_key != 0 */ { 8317 8318 /* 8319 * If we aren't registered currently then increment 8320 * the key count and set the registered flag. 8321 */ 8322 ctl_alloc_prkey(lun, residx); 8323 if (ctl_get_prkey(lun, residx) == 0) 8324 lun->pr_key_count++; 8325 ctl_set_prkey(lun, residx, sa_res_key); 8326 lun->pr_generation++; 8327 mtx_unlock(&lun->lun_lock); 8328 8329 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8330 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8331 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8332 persis_io.pr.pr_info.residx = residx; 8333 memcpy(persis_io.pr.pr_info.sa_res_key, 8334 param->serv_act_res_key, 8335 sizeof(param->serv_act_res_key)); 8336 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8337 sizeof(persis_io.pr), M_WAITOK); 8338 } 8339 8340 break; 8341 } 8342 case SPRO_RESERVE: 8343#if 0 8344 printf("Reserve executed type %d\n", type); 8345#endif 8346 mtx_lock(&lun->lun_lock); 8347 if (lun->flags & CTL_LUN_PR_RESERVED) { 8348 /* 8349 * if this isn't the reservation holder and it's 8350 * not a "all registrants" type or if the type is 8351 * different then we have a conflict 8352 */ 8353 if ((lun->pr_res_idx != residx 8354 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8355 || lun->pr_res_type != type) { 8356 mtx_unlock(&lun->lun_lock); 8357 free(ctsio->kern_data_ptr, M_CTL); 8358 ctl_set_reservation_conflict(ctsio); 8359 ctl_done((union ctl_io *)ctsio); 8360 return (CTL_RETVAL_COMPLETE); 8361 } 8362 mtx_unlock(&lun->lun_lock); 8363 } else /* create a reservation */ { 8364 /* 8365 * If it's not an "all registrants" type record 8366 * reservation holder 8367 */ 8368 if (type != SPR_TYPE_WR_EX_AR 8369 && type != SPR_TYPE_EX_AC_AR) 8370 lun->pr_res_idx = residx; /* Res holder */ 8371 else 8372 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8373 8374 lun->flags |= CTL_LUN_PR_RESERVED; 8375 lun->pr_res_type = type; 8376 8377 mtx_unlock(&lun->lun_lock); 8378 8379 /* send msg to other side */ 8380 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8381 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8382 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8383 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8384 persis_io.pr.pr_info.res_type = type; 8385 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8386 sizeof(persis_io.pr), M_WAITOK); 8387 } 8388 break; 8389 8390 case SPRO_RELEASE: 8391 mtx_lock(&lun->lun_lock); 8392 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8393 /* No reservation exists return good status */ 8394 mtx_unlock(&lun->lun_lock); 8395 goto done; 8396 } 8397 /* 8398 * Is this nexus a reservation holder? 8399 */ 8400 if (lun->pr_res_idx != residx 8401 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8402 /* 8403 * not a res holder return good status but 8404 * do nothing 8405 */ 8406 mtx_unlock(&lun->lun_lock); 8407 goto done; 8408 } 8409 8410 if (lun->pr_res_type != type) { 8411 mtx_unlock(&lun->lun_lock); 8412 free(ctsio->kern_data_ptr, M_CTL); 8413 ctl_set_illegal_pr_release(ctsio); 8414 ctl_done((union ctl_io *)ctsio); 8415 return (CTL_RETVAL_COMPLETE); 8416 } 8417 8418 /* okay to release */ 8419 lun->flags &= ~CTL_LUN_PR_RESERVED; 8420 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8421 lun->pr_res_type = 0; 8422 8423 /* 8424 * If this isn't an exclusive access reservation and NUAR 8425 * is not set, generate UA for all other registrants. 8426 */ 8427 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8428 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8429 for (i = softc->init_min; i < softc->init_max; i++) { 8430 if (i == residx || ctl_get_prkey(lun, i) == 0) 8431 continue; 8432 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8433 } 8434 } 8435 mtx_unlock(&lun->lun_lock); 8436 8437 /* Send msg to other side */ 8438 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8439 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8440 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8441 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8442 sizeof(persis_io.pr), M_WAITOK); 8443 break; 8444 8445 case SPRO_CLEAR: 8446 /* send msg to other side */ 8447 8448 mtx_lock(&lun->lun_lock); 8449 lun->flags &= ~CTL_LUN_PR_RESERVED; 8450 lun->pr_res_type = 0; 8451 lun->pr_key_count = 0; 8452 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8453 8454 ctl_clr_prkey(lun, residx); 8455 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8456 if (ctl_get_prkey(lun, i) != 0) { 8457 ctl_clr_prkey(lun, i); 8458 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8459 } 8460 lun->pr_generation++; 8461 mtx_unlock(&lun->lun_lock); 8462 8463 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8464 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8465 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8466 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8467 sizeof(persis_io.pr), M_WAITOK); 8468 break; 8469 8470 case SPRO_PREEMPT: 8471 case SPRO_PRE_ABO: { 8472 int nretval; 8473 8474 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8475 residx, ctsio, cdb, param); 8476 if (nretval != 0) 8477 return (CTL_RETVAL_COMPLETE); 8478 break; 8479 } 8480 default: 8481 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8482 } 8483 8484done: 8485 free(ctsio->kern_data_ptr, M_CTL); 8486 ctl_set_success(ctsio); 8487 ctl_done((union ctl_io *)ctsio); 8488 8489 return (retval); 8490} 8491 8492/* 8493 * This routine is for handling a message from the other SC pertaining to 8494 * persistent reserve out. All the error checking will have been done 8495 * so only perorming the action need be done here to keep the two 8496 * in sync. 8497 */ 8498static void 8499ctl_hndl_per_res_out_on_other_sc(union ctl_io *io) 8500{ 8501 struct ctl_softc *softc = CTL_SOFTC(io); 8502 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg; 8503 struct ctl_lun *lun; 8504 int i; 8505 uint32_t residx, targ_lun; 8506 8507 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8508 mtx_lock(&softc->ctl_lock); 8509 if (targ_lun >= CTL_MAX_LUNS || 8510 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8511 mtx_unlock(&softc->ctl_lock); 8512 return; 8513 } 8514 mtx_lock(&lun->lun_lock); 8515 mtx_unlock(&softc->ctl_lock); 8516 if (lun->flags & CTL_LUN_DISABLED) { 8517 mtx_unlock(&lun->lun_lock); 8518 return; 8519 } 8520 residx = ctl_get_initindex(&msg->hdr.nexus); 8521 switch(msg->pr.pr_info.action) { 8522 case CTL_PR_REG_KEY: 8523 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8524 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8525 lun->pr_key_count++; 8526 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8527 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8528 lun->pr_generation++; 8529 break; 8530 8531 case CTL_PR_UNREG_KEY: 8532 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8533 lun->pr_key_count--; 8534 8535 /* XXX Need to see if the reservation has been released */ 8536 /* if so do we need to generate UA? */ 8537 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8538 lun->flags &= ~CTL_LUN_PR_RESERVED; 8539 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8540 8541 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8542 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8543 lun->pr_key_count) { 8544 /* 8545 * If the reservation is a registrants 8546 * only type we need to generate a UA 8547 * for other registered inits. The 8548 * sense code should be RESERVATIONS 8549 * RELEASED 8550 */ 8551 8552 for (i = softc->init_min; i < softc->init_max; i++) { 8553 if (ctl_get_prkey(lun, i) == 0) 8554 continue; 8555 8556 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8557 } 8558 } 8559 lun->pr_res_type = 0; 8560 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8561 if (lun->pr_key_count==0) { 8562 lun->flags &= ~CTL_LUN_PR_RESERVED; 8563 lun->pr_res_type = 0; 8564 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8565 } 8566 } 8567 lun->pr_generation++; 8568 break; 8569 8570 case CTL_PR_RESERVE: 8571 lun->flags |= CTL_LUN_PR_RESERVED; 8572 lun->pr_res_type = msg->pr.pr_info.res_type; 8573 lun->pr_res_idx = msg->pr.pr_info.residx; 8574 8575 break; 8576 8577 case CTL_PR_RELEASE: 8578 /* 8579 * If this isn't an exclusive access reservation and NUAR 8580 * is not set, generate UA for all other registrants. 8581 */ 8582 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8583 lun->pr_res_type != SPR_TYPE_WR_EX && 8584 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8585 for (i = softc->init_min; i < softc->init_max; i++) 8586 if (i == residx || ctl_get_prkey(lun, i) == 0) 8587 continue; 8588 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8589 } 8590 8591 lun->flags &= ~CTL_LUN_PR_RESERVED; 8592 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8593 lun->pr_res_type = 0; 8594 break; 8595 8596 case CTL_PR_PREEMPT: 8597 ctl_pro_preempt_other(lun, msg); 8598 break; 8599 case CTL_PR_CLEAR: 8600 lun->flags &= ~CTL_LUN_PR_RESERVED; 8601 lun->pr_res_type = 0; 8602 lun->pr_key_count = 0; 8603 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8604 8605 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8606 if (ctl_get_prkey(lun, i) == 0) 8607 continue; 8608 ctl_clr_prkey(lun, i); 8609 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8610 } 8611 lun->pr_generation++; 8612 break; 8613 } 8614 8615 mtx_unlock(&lun->lun_lock); 8616} 8617 8618int 8619ctl_read_write(struct ctl_scsiio *ctsio) 8620{ 8621 struct ctl_lun *lun = CTL_LUN(ctsio); 8622 struct ctl_lba_len_flags *lbalen; 8623 uint64_t lba; 8624 uint32_t num_blocks; 8625 int flags, retval; 8626 int isread; 8627 8628 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8629 8630 flags = 0; 8631 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8632 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8633 switch (ctsio->cdb[0]) { 8634 case READ_6: 8635 case WRITE_6: { 8636 struct scsi_rw_6 *cdb; 8637 8638 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8639 8640 lba = scsi_3btoul(cdb->addr); 8641 /* only 5 bits are valid in the most significant address byte */ 8642 lba &= 0x1fffff; 8643 num_blocks = cdb->length; 8644 /* 8645 * This is correct according to SBC-2. 8646 */ 8647 if (num_blocks == 0) 8648 num_blocks = 256; 8649 break; 8650 } 8651 case READ_10: 8652 case WRITE_10: { 8653 struct scsi_rw_10 *cdb; 8654 8655 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8656 if (cdb->byte2 & SRW10_FUA) 8657 flags |= CTL_LLF_FUA; 8658 if (cdb->byte2 & SRW10_DPO) 8659 flags |= CTL_LLF_DPO; 8660 lba = scsi_4btoul(cdb->addr); 8661 num_blocks = scsi_2btoul(cdb->length); 8662 break; 8663 } 8664 case WRITE_VERIFY_10: { 8665 struct scsi_write_verify_10 *cdb; 8666 8667 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8668 flags |= CTL_LLF_FUA; 8669 if (cdb->byte2 & SWV_DPO) 8670 flags |= CTL_LLF_DPO; 8671 lba = scsi_4btoul(cdb->addr); 8672 num_blocks = scsi_2btoul(cdb->length); 8673 break; 8674 } 8675 case READ_12: 8676 case WRITE_12: { 8677 struct scsi_rw_12 *cdb; 8678 8679 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8680 if (cdb->byte2 & SRW12_FUA) 8681 flags |= CTL_LLF_FUA; 8682 if (cdb->byte2 & SRW12_DPO) 8683 flags |= CTL_LLF_DPO; 8684 lba = scsi_4btoul(cdb->addr); 8685 num_blocks = scsi_4btoul(cdb->length); 8686 break; 8687 } 8688 case WRITE_VERIFY_12: { 8689 struct scsi_write_verify_12 *cdb; 8690 8691 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8692 flags |= CTL_LLF_FUA; 8693 if (cdb->byte2 & SWV_DPO) 8694 flags |= CTL_LLF_DPO; 8695 lba = scsi_4btoul(cdb->addr); 8696 num_blocks = scsi_4btoul(cdb->length); 8697 break; 8698 } 8699 case READ_16: 8700 case WRITE_16: { 8701 struct scsi_rw_16 *cdb; 8702 8703 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8704 if (cdb->byte2 & SRW12_FUA) 8705 flags |= CTL_LLF_FUA; 8706 if (cdb->byte2 & SRW12_DPO) 8707 flags |= CTL_LLF_DPO; 8708 lba = scsi_8btou64(cdb->addr); 8709 num_blocks = scsi_4btoul(cdb->length); 8710 break; 8711 } 8712 case WRITE_ATOMIC_16: { 8713 struct scsi_write_atomic_16 *cdb; 8714 8715 if (lun->be_lun->atomicblock == 0) { 8716 ctl_set_invalid_opcode(ctsio); 8717 ctl_done((union ctl_io *)ctsio); 8718 return (CTL_RETVAL_COMPLETE); 8719 } 8720 8721 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8722 if (cdb->byte2 & SRW12_FUA) 8723 flags |= CTL_LLF_FUA; 8724 if (cdb->byte2 & SRW12_DPO) 8725 flags |= CTL_LLF_DPO; 8726 lba = scsi_8btou64(cdb->addr); 8727 num_blocks = scsi_2btoul(cdb->length); 8728 if (num_blocks > lun->be_lun->atomicblock) { 8729 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8730 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8731 /*bit*/ 0); 8732 ctl_done((union ctl_io *)ctsio); 8733 return (CTL_RETVAL_COMPLETE); 8734 } 8735 break; 8736 } 8737 case WRITE_VERIFY_16: { 8738 struct scsi_write_verify_16 *cdb; 8739 8740 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8741 flags |= CTL_LLF_FUA; 8742 if (cdb->byte2 & SWV_DPO) 8743 flags |= CTL_LLF_DPO; 8744 lba = scsi_8btou64(cdb->addr); 8745 num_blocks = scsi_4btoul(cdb->length); 8746 break; 8747 } 8748 default: 8749 /* 8750 * We got a command we don't support. This shouldn't 8751 * happen, commands should be filtered out above us. 8752 */ 8753 ctl_set_invalid_opcode(ctsio); 8754 ctl_done((union ctl_io *)ctsio); 8755 8756 return (CTL_RETVAL_COMPLETE); 8757 break; /* NOTREACHED */ 8758 } 8759 8760 /* 8761 * The first check is to make sure we're in bounds, the second 8762 * check is to catch wrap-around problems. If the lba + num blocks 8763 * is less than the lba, then we've wrapped around and the block 8764 * range is invalid anyway. 8765 */ 8766 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8767 || ((lba + num_blocks) < lba)) { 8768 ctl_set_lba_out_of_range(ctsio, 8769 MAX(lba, lun->be_lun->maxlba + 1)); 8770 ctl_done((union ctl_io *)ctsio); 8771 return (CTL_RETVAL_COMPLETE); 8772 } 8773 8774 /* 8775 * According to SBC-3, a transfer length of 0 is not an error. 8776 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8777 * translates to 256 blocks for those commands. 8778 */ 8779 if (num_blocks == 0) { 8780 ctl_set_success(ctsio); 8781 ctl_done((union ctl_io *)ctsio); 8782 return (CTL_RETVAL_COMPLETE); 8783 } 8784 8785 /* Set FUA and/or DPO if caches are disabled. */ 8786 if (isread) { 8787 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8788 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8789 } else { 8790 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8791 flags |= CTL_LLF_FUA; 8792 } 8793 8794 lbalen = (struct ctl_lba_len_flags *) 8795 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8796 lbalen->lba = lba; 8797 lbalen->len = num_blocks; 8798 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8799 8800 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8801 ctsio->kern_rel_offset = 0; 8802 8803 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8804 8805 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8806 return (retval); 8807} 8808 8809static int 8810ctl_cnw_cont(union ctl_io *io) 8811{ 8812 struct ctl_lun *lun = CTL_LUN(io); 8813 struct ctl_scsiio *ctsio; 8814 struct ctl_lba_len_flags *lbalen; 8815 int retval; 8816 8817 ctsio = &io->scsiio; 8818 ctsio->io_hdr.status = CTL_STATUS_NONE; 8819 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8820 lbalen = (struct ctl_lba_len_flags *) 8821 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8822 lbalen->flags &= ~CTL_LLF_COMPARE; 8823 lbalen->flags |= CTL_LLF_WRITE; 8824 8825 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8826 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8827 return (retval); 8828} 8829 8830int 8831ctl_cnw(struct ctl_scsiio *ctsio) 8832{ 8833 struct ctl_lun *lun = CTL_LUN(ctsio); 8834 struct ctl_lba_len_flags *lbalen; 8835 uint64_t lba; 8836 uint32_t num_blocks; 8837 int flags, retval; 8838 8839 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8840 8841 flags = 0; 8842 switch (ctsio->cdb[0]) { 8843 case COMPARE_AND_WRITE: { 8844 struct scsi_compare_and_write *cdb; 8845 8846 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8847 if (cdb->byte2 & SRW10_FUA) 8848 flags |= CTL_LLF_FUA; 8849 if (cdb->byte2 & SRW10_DPO) 8850 flags |= CTL_LLF_DPO; 8851 lba = scsi_8btou64(cdb->addr); 8852 num_blocks = cdb->length; 8853 break; 8854 } 8855 default: 8856 /* 8857 * We got a command we don't support. This shouldn't 8858 * happen, commands should be filtered out above us. 8859 */ 8860 ctl_set_invalid_opcode(ctsio); 8861 ctl_done((union ctl_io *)ctsio); 8862 8863 return (CTL_RETVAL_COMPLETE); 8864 break; /* NOTREACHED */ 8865 } 8866 8867 /* 8868 * The first check is to make sure we're in bounds, the second 8869 * check is to catch wrap-around problems. If the lba + num blocks 8870 * is less than the lba, then we've wrapped around and the block 8871 * range is invalid anyway. 8872 */ 8873 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8874 || ((lba + num_blocks) < lba)) { 8875 ctl_set_lba_out_of_range(ctsio, 8876 MAX(lba, lun->be_lun->maxlba + 1)); 8877 ctl_done((union ctl_io *)ctsio); 8878 return (CTL_RETVAL_COMPLETE); 8879 } 8880 8881 /* 8882 * According to SBC-3, a transfer length of 0 is not an error. 8883 */ 8884 if (num_blocks == 0) { 8885 ctl_set_success(ctsio); 8886 ctl_done((union ctl_io *)ctsio); 8887 return (CTL_RETVAL_COMPLETE); 8888 } 8889 8890 /* Set FUA if write cache is disabled. */ 8891 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8892 flags |= CTL_LLF_FUA; 8893 8894 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8895 ctsio->kern_rel_offset = 0; 8896 8897 /* 8898 * Set the IO_CONT flag, so that if this I/O gets passed to 8899 * ctl_data_submit_done(), it'll get passed back to 8900 * ctl_ctl_cnw_cont() for further processing. 8901 */ 8902 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8903 ctsio->io_cont = ctl_cnw_cont; 8904 8905 lbalen = (struct ctl_lba_len_flags *) 8906 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8907 lbalen->lba = lba; 8908 lbalen->len = num_blocks; 8909 lbalen->flags = CTL_LLF_COMPARE | flags; 8910 8911 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8912 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8913 return (retval); 8914} 8915 8916int 8917ctl_verify(struct ctl_scsiio *ctsio) 8918{ 8919 struct ctl_lun *lun = CTL_LUN(ctsio); 8920 struct ctl_lba_len_flags *lbalen; 8921 uint64_t lba; 8922 uint32_t num_blocks; 8923 int bytchk, flags; 8924 int retval; 8925 8926 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8927 8928 bytchk = 0; 8929 flags = CTL_LLF_FUA; 8930 switch (ctsio->cdb[0]) { 8931 case VERIFY_10: { 8932 struct scsi_verify_10 *cdb; 8933 8934 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8935 if (cdb->byte2 & SVFY_BYTCHK) 8936 bytchk = 1; 8937 if (cdb->byte2 & SVFY_DPO) 8938 flags |= CTL_LLF_DPO; 8939 lba = scsi_4btoul(cdb->addr); 8940 num_blocks = scsi_2btoul(cdb->length); 8941 break; 8942 } 8943 case VERIFY_12: { 8944 struct scsi_verify_12 *cdb; 8945 8946 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8947 if (cdb->byte2 & SVFY_BYTCHK) 8948 bytchk = 1; 8949 if (cdb->byte2 & SVFY_DPO) 8950 flags |= CTL_LLF_DPO; 8951 lba = scsi_4btoul(cdb->addr); 8952 num_blocks = scsi_4btoul(cdb->length); 8953 break; 8954 } 8955 case VERIFY_16: { 8956 struct scsi_rw_16 *cdb; 8957 8958 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8959 if (cdb->byte2 & SVFY_BYTCHK) 8960 bytchk = 1; 8961 if (cdb->byte2 & SVFY_DPO) 8962 flags |= CTL_LLF_DPO; 8963 lba = scsi_8btou64(cdb->addr); 8964 num_blocks = scsi_4btoul(cdb->length); 8965 break; 8966 } 8967 default: 8968 /* 8969 * We got a command we don't support. This shouldn't 8970 * happen, commands should be filtered out above us. 8971 */ 8972 ctl_set_invalid_opcode(ctsio); 8973 ctl_done((union ctl_io *)ctsio); 8974 return (CTL_RETVAL_COMPLETE); 8975 } 8976 8977 /* 8978 * The first check is to make sure we're in bounds, the second 8979 * check is to catch wrap-around problems. If the lba + num blocks 8980 * is less than the lba, then we've wrapped around and the block 8981 * range is invalid anyway. 8982 */ 8983 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8984 || ((lba + num_blocks) < lba)) { 8985 ctl_set_lba_out_of_range(ctsio, 8986 MAX(lba, lun->be_lun->maxlba + 1)); 8987 ctl_done((union ctl_io *)ctsio); 8988 return (CTL_RETVAL_COMPLETE); 8989 } 8990 8991 /* 8992 * According to SBC-3, a transfer length of 0 is not an error. 8993 */ 8994 if (num_blocks == 0) { 8995 ctl_set_success(ctsio); 8996 ctl_done((union ctl_io *)ctsio); 8997 return (CTL_RETVAL_COMPLETE); 8998 } 8999 9000 lbalen = (struct ctl_lba_len_flags *) 9001 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9002 lbalen->lba = lba; 9003 lbalen->len = num_blocks; 9004 if (bytchk) { 9005 lbalen->flags = CTL_LLF_COMPARE | flags; 9006 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9007 } else { 9008 lbalen->flags = CTL_LLF_VERIFY | flags; 9009 ctsio->kern_total_len = 0; 9010 } 9011 ctsio->kern_rel_offset = 0; 9012 9013 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9014 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9015 return (retval); 9016} 9017 9018int 9019ctl_report_luns(struct ctl_scsiio *ctsio) 9020{ 9021 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9022 struct ctl_port *port = CTL_PORT(ctsio); 9023 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio); 9024 struct scsi_report_luns *cdb; 9025 struct scsi_report_luns_data *lun_data; 9026 int num_filled, num_luns, num_port_luns, retval; 9027 uint32_t alloc_len, lun_datalen; 9028 uint32_t initidx, targ_lun_id, lun_id; 9029 9030 retval = CTL_RETVAL_COMPLETE; 9031 cdb = (struct scsi_report_luns *)ctsio->cdb; 9032 9033 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9034 9035 num_luns = 0; 9036 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS; 9037 mtx_lock(&softc->ctl_lock); 9038 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9039 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9040 num_luns++; 9041 } 9042 mtx_unlock(&softc->ctl_lock); 9043 9044 switch (cdb->select_report) { 9045 case RPL_REPORT_DEFAULT: 9046 case RPL_REPORT_ALL: 9047 case RPL_REPORT_NONSUBSID: 9048 break; 9049 case RPL_REPORT_WELLKNOWN: 9050 case RPL_REPORT_ADMIN: 9051 case RPL_REPORT_CONGLOM: 9052 num_luns = 0; 9053 break; 9054 default: 9055 ctl_set_invalid_field(ctsio, 9056 /*sks_valid*/ 1, 9057 /*command*/ 1, 9058 /*field*/ 2, 9059 /*bit_valid*/ 0, 9060 /*bit*/ 0); 9061 ctl_done((union ctl_io *)ctsio); 9062 return (retval); 9063 break; /* NOTREACHED */ 9064 } 9065 9066 alloc_len = scsi_4btoul(cdb->length); 9067 /* 9068 * The initiator has to allocate at least 16 bytes for this request, 9069 * so he can at least get the header and the first LUN. Otherwise 9070 * we reject the request (per SPC-3 rev 14, section 6.21). 9071 */ 9072 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9073 sizeof(struct scsi_report_luns_lundata))) { 9074 ctl_set_invalid_field(ctsio, 9075 /*sks_valid*/ 1, 9076 /*command*/ 1, 9077 /*field*/ 6, 9078 /*bit_valid*/ 0, 9079 /*bit*/ 0); 9080 ctl_done((union ctl_io *)ctsio); 9081 return (retval); 9082 } 9083 9084 lun_datalen = sizeof(*lun_data) + 9085 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9086 9087 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9088 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9089 ctsio->kern_sg_entries = 0; 9090 9091 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9092 9093 mtx_lock(&softc->ctl_lock); 9094 for (targ_lun_id = 0, num_filled = 0; 9095 targ_lun_id < num_port_luns && num_filled < num_luns; 9096 targ_lun_id++) { 9097 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9098 if (lun_id == UINT32_MAX) 9099 continue; 9100 lun = softc->ctl_luns[lun_id]; 9101 if (lun == NULL) 9102 continue; 9103 9104 be64enc(lun_data->luns[num_filled++].lundata, 9105 ctl_encode_lun(targ_lun_id)); 9106 9107 /* 9108 * According to SPC-3, rev 14 section 6.21: 9109 * 9110 * "The execution of a REPORT LUNS command to any valid and 9111 * installed logical unit shall clear the REPORTED LUNS DATA 9112 * HAS CHANGED unit attention condition for all logical 9113 * units of that target with respect to the requesting 9114 * initiator. A valid and installed logical unit is one 9115 * having a PERIPHERAL QUALIFIER of 000b in the standard 9116 * INQUIRY data (see 6.4.2)." 9117 * 9118 * If request_lun is NULL, the LUN this report luns command 9119 * was issued to is either disabled or doesn't exist. In that 9120 * case, we shouldn't clear any pending lun change unit 9121 * attention. 9122 */ 9123 if (request_lun != NULL) { 9124 mtx_lock(&lun->lun_lock); 9125 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9126 mtx_unlock(&lun->lun_lock); 9127 } 9128 } 9129 mtx_unlock(&softc->ctl_lock); 9130 9131 /* 9132 * It's quite possible that we've returned fewer LUNs than we allocated 9133 * space for. Trim it. 9134 */ 9135 lun_datalen = sizeof(*lun_data) + 9136 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9137 9138 if (lun_datalen < alloc_len) { 9139 ctsio->residual = alloc_len - lun_datalen; 9140 ctsio->kern_data_len = lun_datalen; 9141 ctsio->kern_total_len = lun_datalen; 9142 } else { 9143 ctsio->residual = 0; 9144 ctsio->kern_data_len = alloc_len; 9145 ctsio->kern_total_len = alloc_len; 9146 } 9147 ctsio->kern_data_resid = 0; 9148 ctsio->kern_rel_offset = 0; 9149 ctsio->kern_sg_entries = 0; 9150 9151 /* 9152 * We set this to the actual data length, regardless of how much 9153 * space we actually have to return results. If the user looks at 9154 * this value, he'll know whether or not he allocated enough space 9155 * and reissue the command if necessary. We don't support well 9156 * known logical units, so if the user asks for that, return none. 9157 */ 9158 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9159 9160 /* 9161 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9162 * this request. 9163 */ 9164 ctl_set_success(ctsio); 9165 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9166 ctsio->be_move_done = ctl_config_move_done; 9167 ctl_datamove((union ctl_io *)ctsio); 9168 return (retval); 9169} 9170 9171int 9172ctl_request_sense(struct ctl_scsiio *ctsio) 9173{ 9174 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9175 struct ctl_lun *lun = CTL_LUN(ctsio); 9176 struct scsi_request_sense *cdb; 9177 struct scsi_sense_data *sense_ptr; 9178 uint32_t initidx; 9179 int have_error; 9180 u_int sense_len = SSD_FULL_SIZE; 9181 scsi_sense_data_type sense_format; 9182 ctl_ua_type ua_type; 9183 uint8_t asc = 0, ascq = 0; 9184 9185 cdb = (struct scsi_request_sense *)ctsio->cdb; 9186 9187 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9188 9189 /* 9190 * Determine which sense format the user wants. 9191 */ 9192 if (cdb->byte2 & SRS_DESC) 9193 sense_format = SSD_TYPE_DESC; 9194 else 9195 sense_format = SSD_TYPE_FIXED; 9196 9197 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9198 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9199 ctsio->kern_sg_entries = 0; 9200 9201 /* 9202 * struct scsi_sense_data, which is currently set to 256 bytes, is 9203 * larger than the largest allowed value for the length field in the 9204 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9205 */ 9206 ctsio->residual = 0; 9207 ctsio->kern_data_len = cdb->length; 9208 ctsio->kern_total_len = cdb->length; 9209 9210 ctsio->kern_data_resid = 0; 9211 ctsio->kern_rel_offset = 0; 9212 ctsio->kern_sg_entries = 0; 9213 9214 /* 9215 * If we don't have a LUN, we don't have any pending sense. 9216 */ 9217 if (lun == NULL || 9218 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9219 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9220 /* "Logical unit not supported" */ 9221 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9222 /*current_error*/ 1, 9223 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9224 /*asc*/ 0x25, 9225 /*ascq*/ 0x00, 9226 SSD_ELEM_NONE); 9227 goto send; 9228 } 9229 9230 have_error = 0; 9231 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9232 /* 9233 * Check for pending sense, and then for pending unit attentions. 9234 * Pending sense gets returned first, then pending unit attentions. 9235 */ 9236 mtx_lock(&lun->lun_lock); 9237#ifdef CTL_WITH_CA 9238 if (ctl_is_set(lun->have_ca, initidx)) { 9239 scsi_sense_data_type stored_format; 9240 9241 /* 9242 * Check to see which sense format was used for the stored 9243 * sense data. 9244 */ 9245 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9246 9247 /* 9248 * If the user requested a different sense format than the 9249 * one we stored, then we need to convert it to the other 9250 * format. If we're going from descriptor to fixed format 9251 * sense data, we may lose things in translation, depending 9252 * on what options were used. 9253 * 9254 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9255 * for some reason we'll just copy it out as-is. 9256 */ 9257 if ((stored_format == SSD_TYPE_FIXED) 9258 && (sense_format == SSD_TYPE_DESC)) 9259 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9260 &lun->pending_sense[initidx], 9261 (struct scsi_sense_data_desc *)sense_ptr); 9262 else if ((stored_format == SSD_TYPE_DESC) 9263 && (sense_format == SSD_TYPE_FIXED)) 9264 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9265 &lun->pending_sense[initidx], 9266 (struct scsi_sense_data_fixed *)sense_ptr); 9267 else 9268 memcpy(sense_ptr, &lun->pending_sense[initidx], 9269 MIN(sizeof(*sense_ptr), 9270 sizeof(lun->pending_sense[initidx]))); 9271 9272 ctl_clear_mask(lun->have_ca, initidx); 9273 have_error = 1; 9274 } else 9275#endif 9276 if (have_error == 0) { 9277 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9278 sense_format); 9279 if (ua_type != CTL_UA_NONE) 9280 have_error = 1; 9281 } 9282 if (have_error == 0) { 9283 /* 9284 * Report informational exception if have one and allowed. 9285 */ 9286 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9287 asc = lun->ie_asc; 9288 ascq = lun->ie_ascq; 9289 } 9290 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9291 /*current_error*/ 1, 9292 /*sense_key*/ SSD_KEY_NO_SENSE, 9293 /*asc*/ asc, 9294 /*ascq*/ ascq, 9295 SSD_ELEM_NONE); 9296 } 9297 mtx_unlock(&lun->lun_lock); 9298 9299send: 9300 /* 9301 * We report the SCSI status as OK, since the status of the command 9302 * itself is OK. We're reporting sense as parameter data. 9303 */ 9304 ctl_set_success(ctsio); 9305 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9306 ctsio->be_move_done = ctl_config_move_done; 9307 ctl_datamove((union ctl_io *)ctsio); 9308 return (CTL_RETVAL_COMPLETE); 9309} 9310 9311int 9312ctl_tur(struct ctl_scsiio *ctsio) 9313{ 9314 9315 CTL_DEBUG_PRINT(("ctl_tur\n")); 9316 9317 ctl_set_success(ctsio); 9318 ctl_done((union ctl_io *)ctsio); 9319 9320 return (CTL_RETVAL_COMPLETE); 9321} 9322 9323/* 9324 * SCSI VPD page 0x00, the Supported VPD Pages page. 9325 */ 9326static int 9327ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9328{ 9329 struct ctl_lun *lun = CTL_LUN(ctsio); 9330 struct scsi_vpd_supported_pages *pages; 9331 int sup_page_size; 9332 int p; 9333 9334 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9335 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9336 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9337 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9338 ctsio->kern_sg_entries = 0; 9339 9340 if (sup_page_size < alloc_len) { 9341 ctsio->residual = alloc_len - sup_page_size; 9342 ctsio->kern_data_len = sup_page_size; 9343 ctsio->kern_total_len = sup_page_size; 9344 } else { 9345 ctsio->residual = 0; 9346 ctsio->kern_data_len = alloc_len; 9347 ctsio->kern_total_len = alloc_len; 9348 } 9349 ctsio->kern_data_resid = 0; 9350 ctsio->kern_rel_offset = 0; 9351 ctsio->kern_sg_entries = 0; 9352 9353 /* 9354 * The control device is always connected. The disk device, on the 9355 * other hand, may not be online all the time. Need to change this 9356 * to figure out whether the disk device is actually online or not. 9357 */ 9358 if (lun != NULL) 9359 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9360 lun->be_lun->lun_type; 9361 else 9362 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9363 9364 p = 0; 9365 /* Supported VPD pages */ 9366 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9367 /* Serial Number */ 9368 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9369 /* Device Identification */ 9370 pages->page_list[p++] = SVPD_DEVICE_ID; 9371 /* Extended INQUIRY Data */ 9372 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9373 /* Mode Page Policy */ 9374 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9375 /* SCSI Ports */ 9376 pages->page_list[p++] = SVPD_SCSI_PORTS; 9377 /* Third-party Copy */ 9378 pages->page_list[p++] = SVPD_SCSI_TPC; 9379 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9380 /* Block limits */ 9381 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9382 /* Block Device Characteristics */ 9383 pages->page_list[p++] = SVPD_BDC; 9384 /* Logical Block Provisioning */ 9385 pages->page_list[p++] = SVPD_LBP; 9386 } 9387 pages->length = p; 9388 9389 ctl_set_success(ctsio); 9390 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9391 ctsio->be_move_done = ctl_config_move_done; 9392 ctl_datamove((union ctl_io *)ctsio); 9393 return (CTL_RETVAL_COMPLETE); 9394} 9395 9396/* 9397 * SCSI VPD page 0x80, the Unit Serial Number page. 9398 */ 9399static int 9400ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9401{ 9402 struct ctl_lun *lun = CTL_LUN(ctsio); 9403 struct scsi_vpd_unit_serial_number *sn_ptr; 9404 int data_len; 9405 9406 data_len = 4 + CTL_SN_LEN; 9407 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9408 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9409 if (data_len < alloc_len) { 9410 ctsio->residual = alloc_len - data_len; 9411 ctsio->kern_data_len = data_len; 9412 ctsio->kern_total_len = data_len; 9413 } else { 9414 ctsio->residual = 0; 9415 ctsio->kern_data_len = alloc_len; 9416 ctsio->kern_total_len = alloc_len; 9417 } 9418 ctsio->kern_data_resid = 0; 9419 ctsio->kern_rel_offset = 0; 9420 ctsio->kern_sg_entries = 0; 9421 9422 /* 9423 * The control device is always connected. The disk device, on the 9424 * other hand, may not be online all the time. Need to change this 9425 * to figure out whether the disk device is actually online or not. 9426 */ 9427 if (lun != NULL) 9428 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9429 lun->be_lun->lun_type; 9430 else 9431 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9432 9433 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9434 sn_ptr->length = CTL_SN_LEN; 9435 /* 9436 * If we don't have a LUN, we just leave the serial number as 9437 * all spaces. 9438 */ 9439 if (lun != NULL) { 9440 strncpy((char *)sn_ptr->serial_num, 9441 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9442 } else 9443 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9444 9445 ctl_set_success(ctsio); 9446 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9447 ctsio->be_move_done = ctl_config_move_done; 9448 ctl_datamove((union ctl_io *)ctsio); 9449 return (CTL_RETVAL_COMPLETE); 9450} 9451 9452 9453/* 9454 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9455 */ 9456static int 9457ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9458{ 9459 struct ctl_lun *lun = CTL_LUN(ctsio); 9460 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9461 int data_len; 9462 9463 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9464 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9465 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9466 ctsio->kern_sg_entries = 0; 9467 9468 if (data_len < alloc_len) { 9469 ctsio->residual = alloc_len - data_len; 9470 ctsio->kern_data_len = data_len; 9471 ctsio->kern_total_len = data_len; 9472 } else { 9473 ctsio->residual = 0; 9474 ctsio->kern_data_len = alloc_len; 9475 ctsio->kern_total_len = alloc_len; 9476 } 9477 ctsio->kern_data_resid = 0; 9478 ctsio->kern_rel_offset = 0; 9479 ctsio->kern_sg_entries = 0; 9480 9481 /* 9482 * The control device is always connected. The disk device, on the 9483 * other hand, may not be online all the time. 9484 */ 9485 if (lun != NULL) 9486 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9487 lun->be_lun->lun_type; 9488 else 9489 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9490 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9491 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9492 /* 9493 * We support head of queue, ordered and simple tags. 9494 */ 9495 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9496 /* 9497 * Volatile cache supported. 9498 */ 9499 eid_ptr->flags3 = SVPD_EID_V_SUP; 9500 9501 /* 9502 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9503 * attention for a particular IT nexus on all LUNs once we report 9504 * it to that nexus once. This bit is required as of SPC-4. 9505 */ 9506 eid_ptr->flags4 = SVPD_EID_LUICLR; 9507 9508 /* 9509 * We support revert to defaults (RTD) bit in MODE SELECT. 9510 */ 9511 eid_ptr->flags5 = SVPD_EID_RTD_SUP; 9512 9513 /* 9514 * XXX KDM in order to correctly answer this, we would need 9515 * information from the SIM to determine how much sense data it 9516 * can send. So this would really be a path inquiry field, most 9517 * likely. This can be set to a maximum of 252 according to SPC-4, 9518 * but the hardware may or may not be able to support that much. 9519 * 0 just means that the maximum sense data length is not reported. 9520 */ 9521 eid_ptr->max_sense_length = 0; 9522 9523 ctl_set_success(ctsio); 9524 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9525 ctsio->be_move_done = ctl_config_move_done; 9526 ctl_datamove((union ctl_io *)ctsio); 9527 return (CTL_RETVAL_COMPLETE); 9528} 9529 9530static int 9531ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9532{ 9533 struct ctl_lun *lun = CTL_LUN(ctsio); 9534 struct scsi_vpd_mode_page_policy *mpp_ptr; 9535 int data_len; 9536 9537 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9538 sizeof(struct scsi_vpd_mode_page_policy_descr); 9539 9540 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9541 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9542 ctsio->kern_sg_entries = 0; 9543 9544 if (data_len < alloc_len) { 9545 ctsio->residual = alloc_len - data_len; 9546 ctsio->kern_data_len = data_len; 9547 ctsio->kern_total_len = data_len; 9548 } else { 9549 ctsio->residual = 0; 9550 ctsio->kern_data_len = alloc_len; 9551 ctsio->kern_total_len = alloc_len; 9552 } 9553 ctsio->kern_data_resid = 0; 9554 ctsio->kern_rel_offset = 0; 9555 ctsio->kern_sg_entries = 0; 9556 9557 /* 9558 * The control device is always connected. The disk device, on the 9559 * other hand, may not be online all the time. 9560 */ 9561 if (lun != NULL) 9562 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9563 lun->be_lun->lun_type; 9564 else 9565 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9566 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9567 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9568 mpp_ptr->descr[0].page_code = 0x3f; 9569 mpp_ptr->descr[0].subpage_code = 0xff; 9570 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9571 9572 ctl_set_success(ctsio); 9573 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9574 ctsio->be_move_done = ctl_config_move_done; 9575 ctl_datamove((union ctl_io *)ctsio); 9576 return (CTL_RETVAL_COMPLETE); 9577} 9578 9579/* 9580 * SCSI VPD page 0x83, the Device Identification page. 9581 */ 9582static int 9583ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9584{ 9585 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9586 struct ctl_port *port = CTL_PORT(ctsio); 9587 struct ctl_lun *lun = CTL_LUN(ctsio); 9588 struct scsi_vpd_device_id *devid_ptr; 9589 struct scsi_vpd_id_descriptor *desc; 9590 int data_len, g; 9591 uint8_t proto; 9592 9593 data_len = sizeof(struct scsi_vpd_device_id) + 9594 sizeof(struct scsi_vpd_id_descriptor) + 9595 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9596 sizeof(struct scsi_vpd_id_descriptor) + 9597 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9598 if (lun && lun->lun_devid) 9599 data_len += lun->lun_devid->len; 9600 if (port && port->port_devid) 9601 data_len += port->port_devid->len; 9602 if (port && port->target_devid) 9603 data_len += port->target_devid->len; 9604 9605 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9606 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9607 ctsio->kern_sg_entries = 0; 9608 9609 if (data_len < alloc_len) { 9610 ctsio->residual = alloc_len - data_len; 9611 ctsio->kern_data_len = data_len; 9612 ctsio->kern_total_len = data_len; 9613 } else { 9614 ctsio->residual = 0; 9615 ctsio->kern_data_len = alloc_len; 9616 ctsio->kern_total_len = alloc_len; 9617 } 9618 ctsio->kern_data_resid = 0; 9619 ctsio->kern_rel_offset = 0; 9620 ctsio->kern_sg_entries = 0; 9621 9622 /* 9623 * The control device is always connected. The disk device, on the 9624 * other hand, may not be online all the time. 9625 */ 9626 if (lun != NULL) 9627 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9628 lun->be_lun->lun_type; 9629 else 9630 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9631 devid_ptr->page_code = SVPD_DEVICE_ID; 9632 scsi_ulto2b(data_len - 4, devid_ptr->length); 9633 9634 if (port && port->port_type == CTL_PORT_FC) 9635 proto = SCSI_PROTO_FC << 4; 9636 else if (port && port->port_type == CTL_PORT_ISCSI) 9637 proto = SCSI_PROTO_ISCSI << 4; 9638 else 9639 proto = SCSI_PROTO_SPI << 4; 9640 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9641 9642 /* 9643 * We're using a LUN association here. i.e., this device ID is a 9644 * per-LUN identifier. 9645 */ 9646 if (lun && lun->lun_devid) { 9647 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9648 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9649 lun->lun_devid->len); 9650 } 9651 9652 /* 9653 * This is for the WWPN which is a port association. 9654 */ 9655 if (port && port->port_devid) { 9656 memcpy(desc, port->port_devid->data, port->port_devid->len); 9657 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9658 port->port_devid->len); 9659 } 9660 9661 /* 9662 * This is for the Relative Target Port(type 4h) identifier 9663 */ 9664 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9665 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9666 SVPD_ID_TYPE_RELTARG; 9667 desc->length = 4; 9668 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9669 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9670 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9671 9672 /* 9673 * This is for the Target Port Group(type 5h) identifier 9674 */ 9675 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9676 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9677 SVPD_ID_TYPE_TPORTGRP; 9678 desc->length = 4; 9679 if (softc->is_single || 9680 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9681 g = 1; 9682 else 9683 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9684 scsi_ulto2b(g, &desc->identifier[2]); 9685 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9686 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9687 9688 /* 9689 * This is for the Target identifier 9690 */ 9691 if (port && port->target_devid) { 9692 memcpy(desc, port->target_devid->data, port->target_devid->len); 9693 } 9694 9695 ctl_set_success(ctsio); 9696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9697 ctsio->be_move_done = ctl_config_move_done; 9698 ctl_datamove((union ctl_io *)ctsio); 9699 return (CTL_RETVAL_COMPLETE); 9700} 9701 9702static int 9703ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9704{ 9705 struct ctl_softc *softc = CTL_SOFTC(ctsio); 9706 struct ctl_lun *lun = CTL_LUN(ctsio); 9707 struct scsi_vpd_scsi_ports *sp; 9708 struct scsi_vpd_port_designation *pd; 9709 struct scsi_vpd_port_designation_cont *pdc; 9710 struct ctl_port *port; 9711 int data_len, num_target_ports, iid_len, id_len; 9712 9713 num_target_ports = 0; 9714 iid_len = 0; 9715 id_len = 0; 9716 mtx_lock(&softc->ctl_lock); 9717 STAILQ_FOREACH(port, &softc->port_list, links) { 9718 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9719 continue; 9720 if (lun != NULL && 9721 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9722 continue; 9723 num_target_ports++; 9724 if (port->init_devid) 9725 iid_len += port->init_devid->len; 9726 if (port->port_devid) 9727 id_len += port->port_devid->len; 9728 } 9729 mtx_unlock(&softc->ctl_lock); 9730 9731 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9732 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9733 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9734 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9735 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9736 ctsio->kern_sg_entries = 0; 9737 9738 if (data_len < alloc_len) { 9739 ctsio->residual = alloc_len - data_len; 9740 ctsio->kern_data_len = data_len; 9741 ctsio->kern_total_len = data_len; 9742 } else { 9743 ctsio->residual = 0; 9744 ctsio->kern_data_len = alloc_len; 9745 ctsio->kern_total_len = alloc_len; 9746 } 9747 ctsio->kern_data_resid = 0; 9748 ctsio->kern_rel_offset = 0; 9749 ctsio->kern_sg_entries = 0; 9750 9751 /* 9752 * The control device is always connected. The disk device, on the 9753 * other hand, may not be online all the time. Need to change this 9754 * to figure out whether the disk device is actually online or not. 9755 */ 9756 if (lun != NULL) 9757 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9758 lun->be_lun->lun_type; 9759 else 9760 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9761 9762 sp->page_code = SVPD_SCSI_PORTS; 9763 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9764 sp->page_length); 9765 pd = &sp->design[0]; 9766 9767 mtx_lock(&softc->ctl_lock); 9768 STAILQ_FOREACH(port, &softc->port_list, links) { 9769 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9770 continue; 9771 if (lun != NULL && 9772 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9773 continue; 9774 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9775 if (port->init_devid) { 9776 iid_len = port->init_devid->len; 9777 memcpy(pd->initiator_transportid, 9778 port->init_devid->data, port->init_devid->len); 9779 } else 9780 iid_len = 0; 9781 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9782 pdc = (struct scsi_vpd_port_designation_cont *) 9783 (&pd->initiator_transportid[iid_len]); 9784 if (port->port_devid) { 9785 id_len = port->port_devid->len; 9786 memcpy(pdc->target_port_descriptors, 9787 port->port_devid->data, port->port_devid->len); 9788 } else 9789 id_len = 0; 9790 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9791 pd = (struct scsi_vpd_port_designation *) 9792 ((uint8_t *)pdc->target_port_descriptors + id_len); 9793 } 9794 mtx_unlock(&softc->ctl_lock); 9795 9796 ctl_set_success(ctsio); 9797 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9798 ctsio->be_move_done = ctl_config_move_done; 9799 ctl_datamove((union ctl_io *)ctsio); 9800 return (CTL_RETVAL_COMPLETE); 9801} 9802 9803static int 9804ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9805{ 9806 struct ctl_lun *lun = CTL_LUN(ctsio); 9807 struct scsi_vpd_block_limits *bl_ptr; 9808 uint64_t ival; 9809 9810 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9811 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9812 ctsio->kern_sg_entries = 0; 9813 9814 if (sizeof(*bl_ptr) < alloc_len) { 9815 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9816 ctsio->kern_data_len = sizeof(*bl_ptr); 9817 ctsio->kern_total_len = sizeof(*bl_ptr); 9818 } else { 9819 ctsio->residual = 0; 9820 ctsio->kern_data_len = alloc_len; 9821 ctsio->kern_total_len = alloc_len; 9822 } 9823 ctsio->kern_data_resid = 0; 9824 ctsio->kern_rel_offset = 0; 9825 ctsio->kern_sg_entries = 0; 9826 9827 /* 9828 * The control device is always connected. The disk device, on the 9829 * other hand, may not be online all the time. Need to change this 9830 * to figure out whether the disk device is actually online or not. 9831 */ 9832 if (lun != NULL) 9833 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9834 lun->be_lun->lun_type; 9835 else 9836 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9837 9838 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9839 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9840 bl_ptr->max_cmp_write_len = 0xff; 9841 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9842 if (lun != NULL) { 9843 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9844 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9845 ival = 0xffffffff; 9846 ctl_get_opt_number(&lun->be_lun->options, 9847 "unmap_max_lba", &ival); 9848 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9849 ival = 0xffffffff; 9850 ctl_get_opt_number(&lun->be_lun->options, 9851 "unmap_max_descr", &ival); 9852 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9853 if (lun->be_lun->ublockexp != 0) { 9854 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9855 bl_ptr->opt_unmap_grain); 9856 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9857 bl_ptr->unmap_grain_align); 9858 } 9859 } 9860 scsi_ulto4b(lun->be_lun->atomicblock, 9861 bl_ptr->max_atomic_transfer_length); 9862 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9863 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9864 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9865 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9866 ival = UINT64_MAX; 9867 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival); 9868 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9869 } 9870 9871 ctl_set_success(ctsio); 9872 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9873 ctsio->be_move_done = ctl_config_move_done; 9874 ctl_datamove((union ctl_io *)ctsio); 9875 return (CTL_RETVAL_COMPLETE); 9876} 9877 9878static int 9879ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9880{ 9881 struct ctl_lun *lun = CTL_LUN(ctsio); 9882 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9883 const char *value; 9884 u_int i; 9885 9886 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9887 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9888 ctsio->kern_sg_entries = 0; 9889 9890 if (sizeof(*bdc_ptr) < alloc_len) { 9891 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9892 ctsio->kern_data_len = sizeof(*bdc_ptr); 9893 ctsio->kern_total_len = sizeof(*bdc_ptr); 9894 } else { 9895 ctsio->residual = 0; 9896 ctsio->kern_data_len = alloc_len; 9897 ctsio->kern_total_len = alloc_len; 9898 } 9899 ctsio->kern_data_resid = 0; 9900 ctsio->kern_rel_offset = 0; 9901 ctsio->kern_sg_entries = 0; 9902 9903 /* 9904 * The control device is always connected. The disk device, on the 9905 * other hand, may not be online all the time. Need to change this 9906 * to figure out whether the disk device is actually online or not. 9907 */ 9908 if (lun != NULL) 9909 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9910 lun->be_lun->lun_type; 9911 else 9912 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9913 bdc_ptr->page_code = SVPD_BDC; 9914 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9915 if (lun != NULL && 9916 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9917 i = strtol(value, NULL, 0); 9918 else 9919 i = CTL_DEFAULT_ROTATION_RATE; 9920 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9921 if (lun != NULL && 9922 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9923 i = strtol(value, NULL, 0); 9924 else 9925 i = 0; 9926 bdc_ptr->wab_wac_ff = (i & 0x0f); 9927 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9928 9929 ctl_set_success(ctsio); 9930 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9931 ctsio->be_move_done = ctl_config_move_done; 9932 ctl_datamove((union ctl_io *)ctsio); 9933 return (CTL_RETVAL_COMPLETE); 9934} 9935 9936static int 9937ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9938{ 9939 struct ctl_lun *lun = CTL_LUN(ctsio); 9940 struct scsi_vpd_logical_block_prov *lbp_ptr; 9941 const char *value; 9942 9943 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9944 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9945 ctsio->kern_sg_entries = 0; 9946 9947 if (sizeof(*lbp_ptr) < alloc_len) { 9948 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9949 ctsio->kern_data_len = sizeof(*lbp_ptr); 9950 ctsio->kern_total_len = sizeof(*lbp_ptr); 9951 } else { 9952 ctsio->residual = 0; 9953 ctsio->kern_data_len = alloc_len; 9954 ctsio->kern_total_len = alloc_len; 9955 } 9956 ctsio->kern_data_resid = 0; 9957 ctsio->kern_rel_offset = 0; 9958 ctsio->kern_sg_entries = 0; 9959 9960 /* 9961 * The control device is always connected. The disk device, on the 9962 * other hand, may not be online all the time. Need to change this 9963 * to figure out whether the disk device is actually online or not. 9964 */ 9965 if (lun != NULL) 9966 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9967 lun->be_lun->lun_type; 9968 else 9969 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9970 9971 lbp_ptr->page_code = SVPD_LBP; 9972 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9973 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9974 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9975 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9976 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9977 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type"); 9978 if (value != NULL) { 9979 if (strcmp(value, "resource") == 0) 9980 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 9981 else if (strcmp(value, "thin") == 0) 9982 lbp_ptr->prov_type = SVPD_LBP_THIN; 9983 } else 9984 lbp_ptr->prov_type = SVPD_LBP_THIN; 9985 } 9986 9987 ctl_set_success(ctsio); 9988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9989 ctsio->be_move_done = ctl_config_move_done; 9990 ctl_datamove((union ctl_io *)ctsio); 9991 return (CTL_RETVAL_COMPLETE); 9992} 9993 9994/* 9995 * INQUIRY with the EVPD bit set. 9996 */ 9997static int 9998ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9999{ 10000 struct ctl_lun *lun = CTL_LUN(ctsio); 10001 struct scsi_inquiry *cdb; 10002 int alloc_len, retval; 10003 10004 cdb = (struct scsi_inquiry *)ctsio->cdb; 10005 alloc_len = scsi_2btoul(cdb->length); 10006 10007 switch (cdb->page_code) { 10008 case SVPD_SUPPORTED_PAGES: 10009 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10010 break; 10011 case SVPD_UNIT_SERIAL_NUMBER: 10012 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10013 break; 10014 case SVPD_DEVICE_ID: 10015 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10016 break; 10017 case SVPD_EXTENDED_INQUIRY_DATA: 10018 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10019 break; 10020 case SVPD_MODE_PAGE_POLICY: 10021 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10022 break; 10023 case SVPD_SCSI_PORTS: 10024 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10025 break; 10026 case SVPD_SCSI_TPC: 10027 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10028 break; 10029 case SVPD_BLOCK_LIMITS: 10030 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10031 goto err; 10032 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10033 break; 10034 case SVPD_BDC: 10035 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10036 goto err; 10037 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10038 break; 10039 case SVPD_LBP: 10040 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10041 goto err; 10042 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10043 break; 10044 default: 10045err: 10046 ctl_set_invalid_field(ctsio, 10047 /*sks_valid*/ 1, 10048 /*command*/ 1, 10049 /*field*/ 2, 10050 /*bit_valid*/ 0, 10051 /*bit*/ 0); 10052 ctl_done((union ctl_io *)ctsio); 10053 retval = CTL_RETVAL_COMPLETE; 10054 break; 10055 } 10056 10057 return (retval); 10058} 10059 10060/* 10061 * Standard INQUIRY data. 10062 */ 10063static int 10064ctl_inquiry_std(struct ctl_scsiio *ctsio) 10065{ 10066 struct ctl_softc *softc = CTL_SOFTC(ctsio); 10067 struct ctl_port *port = CTL_PORT(ctsio); 10068 struct ctl_lun *lun = CTL_LUN(ctsio); 10069 struct scsi_inquiry_data *inq_ptr; 10070 struct scsi_inquiry *cdb; 10071 char *val; 10072 uint32_t alloc_len, data_len; 10073 ctl_port_type port_type; 10074 10075 port_type = port->port_type; 10076 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10077 port_type = CTL_PORT_SCSI; 10078 10079 cdb = (struct scsi_inquiry *)ctsio->cdb; 10080 alloc_len = scsi_2btoul(cdb->length); 10081 10082 /* 10083 * We malloc the full inquiry data size here and fill it 10084 * in. If the user only asks for less, we'll give him 10085 * that much. 10086 */ 10087 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10088 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10089 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10090 ctsio->kern_sg_entries = 0; 10091 ctsio->kern_data_resid = 0; 10092 ctsio->kern_rel_offset = 0; 10093 10094 if (data_len < alloc_len) { 10095 ctsio->residual = alloc_len - data_len; 10096 ctsio->kern_data_len = data_len; 10097 ctsio->kern_total_len = data_len; 10098 } else { 10099 ctsio->residual = 0; 10100 ctsio->kern_data_len = alloc_len; 10101 ctsio->kern_total_len = alloc_len; 10102 } 10103 10104 if (lun != NULL) { 10105 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10106 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10107 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10108 lun->be_lun->lun_type; 10109 } else { 10110 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10111 lun->be_lun->lun_type; 10112 } 10113 if (lun->flags & CTL_LUN_REMOVABLE) 10114 inq_ptr->dev_qual2 |= SID_RMB; 10115 } else 10116 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10117 10118 /* RMB in byte 2 is 0 */ 10119 inq_ptr->version = SCSI_REV_SPC5; 10120 10121 /* 10122 * According to SAM-3, even if a device only supports a single 10123 * level of LUN addressing, it should still set the HISUP bit: 10124 * 10125 * 4.9.1 Logical unit numbers overview 10126 * 10127 * All logical unit number formats described in this standard are 10128 * hierarchical in structure even when only a single level in that 10129 * hierarchy is used. The HISUP bit shall be set to one in the 10130 * standard INQUIRY data (see SPC-2) when any logical unit number 10131 * format described in this standard is used. Non-hierarchical 10132 * formats are outside the scope of this standard. 10133 * 10134 * Therefore we set the HiSup bit here. 10135 * 10136 * The response format is 2, per SPC-3. 10137 */ 10138 inq_ptr->response_format = SID_HiSup | 2; 10139 10140 inq_ptr->additional_length = data_len - 10141 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10142 CTL_DEBUG_PRINT(("additional_length = %d\n", 10143 inq_ptr->additional_length)); 10144 10145 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10146 if (port_type == CTL_PORT_SCSI) 10147 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10148 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10149 inq_ptr->flags = SID_CmdQue; 10150 if (port_type == CTL_PORT_SCSI) 10151 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10152 10153 /* 10154 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10155 * We have 8 bytes for the vendor name, and 16 bytes for the device 10156 * name and 4 bytes for the revision. 10157 */ 10158 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10159 "vendor")) == NULL) { 10160 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10161 } else { 10162 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10163 strncpy(inq_ptr->vendor, val, 10164 min(sizeof(inq_ptr->vendor), strlen(val))); 10165 } 10166 if (lun == NULL) { 10167 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10168 sizeof(inq_ptr->product)); 10169 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10170 switch (lun->be_lun->lun_type) { 10171 case T_DIRECT: 10172 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10173 sizeof(inq_ptr->product)); 10174 break; 10175 case T_PROCESSOR: 10176 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10177 sizeof(inq_ptr->product)); 10178 break; 10179 case T_CDROM: 10180 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10181 sizeof(inq_ptr->product)); 10182 break; 10183 default: 10184 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10185 sizeof(inq_ptr->product)); 10186 break; 10187 } 10188 } else { 10189 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10190 strncpy(inq_ptr->product, val, 10191 min(sizeof(inq_ptr->product), strlen(val))); 10192 } 10193 10194 /* 10195 * XXX make this a macro somewhere so it automatically gets 10196 * incremented when we make changes. 10197 */ 10198 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10199 "revision")) == NULL) { 10200 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10201 } else { 10202 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10203 strncpy(inq_ptr->revision, val, 10204 min(sizeof(inq_ptr->revision), strlen(val))); 10205 } 10206 10207 /* 10208 * For parallel SCSI, we support double transition and single 10209 * transition clocking. We also support QAS (Quick Arbitration 10210 * and Selection) and Information Unit transfers on both the 10211 * control and array devices. 10212 */ 10213 if (port_type == CTL_PORT_SCSI) 10214 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10215 SID_SPI_IUS; 10216 10217 /* SAM-6 (no version claimed) */ 10218 scsi_ulto2b(0x00C0, inq_ptr->version1); 10219 /* SPC-5 (no version claimed) */ 10220 scsi_ulto2b(0x05C0, inq_ptr->version2); 10221 if (port_type == CTL_PORT_FC) { 10222 /* FCP-2 ANSI INCITS.350:2003 */ 10223 scsi_ulto2b(0x0917, inq_ptr->version3); 10224 } else if (port_type == CTL_PORT_SCSI) { 10225 /* SPI-4 ANSI INCITS.362:200x */ 10226 scsi_ulto2b(0x0B56, inq_ptr->version3); 10227 } else if (port_type == CTL_PORT_ISCSI) { 10228 /* iSCSI (no version claimed) */ 10229 scsi_ulto2b(0x0960, inq_ptr->version3); 10230 } else if (port_type == CTL_PORT_SAS) { 10231 /* SAS (no version claimed) */ 10232 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10233 } 10234 10235 if (lun == NULL) { 10236 /* SBC-4 (no version claimed) */ 10237 scsi_ulto2b(0x0600, inq_ptr->version4); 10238 } else { 10239 switch (lun->be_lun->lun_type) { 10240 case T_DIRECT: 10241 /* SBC-4 (no version claimed) */ 10242 scsi_ulto2b(0x0600, inq_ptr->version4); 10243 break; 10244 case T_PROCESSOR: 10245 break; 10246 case T_CDROM: 10247 /* MMC-6 (no version claimed) */ 10248 scsi_ulto2b(0x04E0, inq_ptr->version4); 10249 break; 10250 default: 10251 break; 10252 } 10253 } 10254 10255 ctl_set_success(ctsio); 10256 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10257 ctsio->be_move_done = ctl_config_move_done; 10258 ctl_datamove((union ctl_io *)ctsio); 10259 return (CTL_RETVAL_COMPLETE); 10260} 10261 10262int 10263ctl_inquiry(struct ctl_scsiio *ctsio) 10264{ 10265 struct scsi_inquiry *cdb; 10266 int retval; 10267 10268 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10269 10270 cdb = (struct scsi_inquiry *)ctsio->cdb; 10271 if (cdb->byte2 & SI_EVPD) 10272 retval = ctl_inquiry_evpd(ctsio); 10273 else if (cdb->page_code == 0) 10274 retval = ctl_inquiry_std(ctsio); 10275 else { 10276 ctl_set_invalid_field(ctsio, 10277 /*sks_valid*/ 1, 10278 /*command*/ 1, 10279 /*field*/ 2, 10280 /*bit_valid*/ 0, 10281 /*bit*/ 0); 10282 ctl_done((union ctl_io *)ctsio); 10283 return (CTL_RETVAL_COMPLETE); 10284 } 10285 10286 return (retval); 10287} 10288 10289int 10290ctl_get_config(struct ctl_scsiio *ctsio) 10291{ 10292 struct ctl_lun *lun = CTL_LUN(ctsio); 10293 struct scsi_get_config_header *hdr; 10294 struct scsi_get_config_feature *feature; 10295 struct scsi_get_config *cdb; 10296 uint32_t alloc_len, data_len; 10297 int rt, starting; 10298 10299 cdb = (struct scsi_get_config *)ctsio->cdb; 10300 rt = (cdb->rt & SGC_RT_MASK); 10301 starting = scsi_2btoul(cdb->starting_feature); 10302 alloc_len = scsi_2btoul(cdb->length); 10303 10304 data_len = sizeof(struct scsi_get_config_header) + 10305 sizeof(struct scsi_get_config_feature) + 8 + 10306 sizeof(struct scsi_get_config_feature) + 8 + 10307 sizeof(struct scsi_get_config_feature) + 4 + 10308 sizeof(struct scsi_get_config_feature) + 4 + 10309 sizeof(struct scsi_get_config_feature) + 8 + 10310 sizeof(struct scsi_get_config_feature) + 10311 sizeof(struct scsi_get_config_feature) + 4 + 10312 sizeof(struct scsi_get_config_feature) + 4 + 10313 sizeof(struct scsi_get_config_feature) + 4 + 10314 sizeof(struct scsi_get_config_feature) + 4 + 10315 sizeof(struct scsi_get_config_feature) + 4 + 10316 sizeof(struct scsi_get_config_feature) + 4; 10317 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10318 ctsio->kern_sg_entries = 0; 10319 ctsio->kern_data_resid = 0; 10320 ctsio->kern_rel_offset = 0; 10321 10322 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10323 if (lun->flags & CTL_LUN_NO_MEDIA) 10324 scsi_ulto2b(0x0000, hdr->current_profile); 10325 else 10326 scsi_ulto2b(0x0010, hdr->current_profile); 10327 feature = (struct scsi_get_config_feature *)(hdr + 1); 10328 10329 if (starting > 0x003b) 10330 goto done; 10331 if (starting > 0x003a) 10332 goto f3b; 10333 if (starting > 0x002b) 10334 goto f3a; 10335 if (starting > 0x002a) 10336 goto f2b; 10337 if (starting > 0x001f) 10338 goto f2a; 10339 if (starting > 0x001e) 10340 goto f1f; 10341 if (starting > 0x001d) 10342 goto f1e; 10343 if (starting > 0x0010) 10344 goto f1d; 10345 if (starting > 0x0003) 10346 goto f10; 10347 if (starting > 0x0002) 10348 goto f3; 10349 if (starting > 0x0001) 10350 goto f2; 10351 if (starting > 0x0000) 10352 goto f1; 10353 10354 /* Profile List */ 10355 scsi_ulto2b(0x0000, feature->feature_code); 10356 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10357 feature->add_length = 8; 10358 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10359 feature->feature_data[2] = 0x00; 10360 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10361 feature->feature_data[6] = 0x01; 10362 feature = (struct scsi_get_config_feature *) 10363 &feature->feature_data[feature->add_length]; 10364 10365f1: /* Core */ 10366 scsi_ulto2b(0x0001, feature->feature_code); 10367 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10368 feature->add_length = 8; 10369 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10370 feature->feature_data[4] = 0x03; 10371 feature = (struct scsi_get_config_feature *) 10372 &feature->feature_data[feature->add_length]; 10373 10374f2: /* Morphing */ 10375 scsi_ulto2b(0x0002, feature->feature_code); 10376 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10377 feature->add_length = 4; 10378 feature->feature_data[0] = 0x02; 10379 feature = (struct scsi_get_config_feature *) 10380 &feature->feature_data[feature->add_length]; 10381 10382f3: /* Removable Medium */ 10383 scsi_ulto2b(0x0003, feature->feature_code); 10384 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10385 feature->add_length = 4; 10386 feature->feature_data[0] = 0x39; 10387 feature = (struct scsi_get_config_feature *) 10388 &feature->feature_data[feature->add_length]; 10389 10390 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10391 goto done; 10392 10393f10: /* Random Read */ 10394 scsi_ulto2b(0x0010, feature->feature_code); 10395 feature->flags = 0x00; 10396 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10397 feature->flags |= SGC_F_CURRENT; 10398 feature->add_length = 8; 10399 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10400 scsi_ulto2b(1, &feature->feature_data[4]); 10401 feature->feature_data[6] = 0x00; 10402 feature = (struct scsi_get_config_feature *) 10403 &feature->feature_data[feature->add_length]; 10404 10405f1d: /* Multi-Read */ 10406 scsi_ulto2b(0x001D, feature->feature_code); 10407 feature->flags = 0x00; 10408 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10409 feature->flags |= SGC_F_CURRENT; 10410 feature->add_length = 0; 10411 feature = (struct scsi_get_config_feature *) 10412 &feature->feature_data[feature->add_length]; 10413 10414f1e: /* CD Read */ 10415 scsi_ulto2b(0x001E, feature->feature_code); 10416 feature->flags = 0x00; 10417 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10418 feature->flags |= SGC_F_CURRENT; 10419 feature->add_length = 4; 10420 feature->feature_data[0] = 0x00; 10421 feature = (struct scsi_get_config_feature *) 10422 &feature->feature_data[feature->add_length]; 10423 10424f1f: /* DVD Read */ 10425 scsi_ulto2b(0x001F, feature->feature_code); 10426 feature->flags = 0x08; 10427 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10428 feature->flags |= SGC_F_CURRENT; 10429 feature->add_length = 4; 10430 feature->feature_data[0] = 0x01; 10431 feature->feature_data[2] = 0x03; 10432 feature = (struct scsi_get_config_feature *) 10433 &feature->feature_data[feature->add_length]; 10434 10435f2a: /* DVD+RW */ 10436 scsi_ulto2b(0x002A, feature->feature_code); 10437 feature->flags = 0x04; 10438 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10439 feature->flags |= SGC_F_CURRENT; 10440 feature->add_length = 4; 10441 feature->feature_data[0] = 0x00; 10442 feature->feature_data[1] = 0x00; 10443 feature = (struct scsi_get_config_feature *) 10444 &feature->feature_data[feature->add_length]; 10445 10446f2b: /* DVD+R */ 10447 scsi_ulto2b(0x002B, feature->feature_code); 10448 feature->flags = 0x00; 10449 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10450 feature->flags |= SGC_F_CURRENT; 10451 feature->add_length = 4; 10452 feature->feature_data[0] = 0x00; 10453 feature = (struct scsi_get_config_feature *) 10454 &feature->feature_data[feature->add_length]; 10455 10456f3a: /* DVD+RW Dual Layer */ 10457 scsi_ulto2b(0x003A, feature->feature_code); 10458 feature->flags = 0x00; 10459 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10460 feature->flags |= SGC_F_CURRENT; 10461 feature->add_length = 4; 10462 feature->feature_data[0] = 0x00; 10463 feature->feature_data[1] = 0x00; 10464 feature = (struct scsi_get_config_feature *) 10465 &feature->feature_data[feature->add_length]; 10466 10467f3b: /* DVD+R Dual Layer */ 10468 scsi_ulto2b(0x003B, feature->feature_code); 10469 feature->flags = 0x00; 10470 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10471 feature->flags |= SGC_F_CURRENT; 10472 feature->add_length = 4; 10473 feature->feature_data[0] = 0x00; 10474 feature = (struct scsi_get_config_feature *) 10475 &feature->feature_data[feature->add_length]; 10476 10477done: 10478 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10479 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10480 feature = (struct scsi_get_config_feature *)(hdr + 1); 10481 if (scsi_2btoul(feature->feature_code) == starting) 10482 feature = (struct scsi_get_config_feature *) 10483 &feature->feature_data[feature->add_length]; 10484 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10485 } 10486 scsi_ulto4b(data_len - 4, hdr->data_length); 10487 if (data_len < alloc_len) { 10488 ctsio->residual = alloc_len - data_len; 10489 ctsio->kern_data_len = data_len; 10490 ctsio->kern_total_len = data_len; 10491 } else { 10492 ctsio->residual = 0; 10493 ctsio->kern_data_len = alloc_len; 10494 ctsio->kern_total_len = alloc_len; 10495 } 10496 10497 ctl_set_success(ctsio); 10498 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10499 ctsio->be_move_done = ctl_config_move_done; 10500 ctl_datamove((union ctl_io *)ctsio); 10501 return (CTL_RETVAL_COMPLETE); 10502} 10503 10504int 10505ctl_get_event_status(struct ctl_scsiio *ctsio) 10506{ 10507 struct scsi_get_event_status_header *hdr; 10508 struct scsi_get_event_status *cdb; 10509 uint32_t alloc_len, data_len; 10510 int notif_class; 10511 10512 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10513 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10514 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10515 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10516 ctl_done((union ctl_io *)ctsio); 10517 return (CTL_RETVAL_COMPLETE); 10518 } 10519 notif_class = cdb->notif_class; 10520 alloc_len = scsi_2btoul(cdb->length); 10521 10522 data_len = sizeof(struct scsi_get_event_status_header); 10523 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10524 ctsio->kern_sg_entries = 0; 10525 ctsio->kern_data_resid = 0; 10526 ctsio->kern_rel_offset = 0; 10527 10528 if (data_len < alloc_len) { 10529 ctsio->residual = alloc_len - data_len; 10530 ctsio->kern_data_len = data_len; 10531 ctsio->kern_total_len = data_len; 10532 } else { 10533 ctsio->residual = 0; 10534 ctsio->kern_data_len = alloc_len; 10535 ctsio->kern_total_len = alloc_len; 10536 } 10537 10538 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10539 scsi_ulto2b(0, hdr->descr_length); 10540 hdr->nea_class = SGESN_NEA; 10541 hdr->supported_class = 0; 10542 10543 ctl_set_success(ctsio); 10544 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10545 ctsio->be_move_done = ctl_config_move_done; 10546 ctl_datamove((union ctl_io *)ctsio); 10547 return (CTL_RETVAL_COMPLETE); 10548} 10549 10550int 10551ctl_mechanism_status(struct ctl_scsiio *ctsio) 10552{ 10553 struct scsi_mechanism_status_header *hdr; 10554 struct scsi_mechanism_status *cdb; 10555 uint32_t alloc_len, data_len; 10556 10557 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10558 alloc_len = scsi_2btoul(cdb->length); 10559 10560 data_len = sizeof(struct scsi_mechanism_status_header); 10561 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10562 ctsio->kern_sg_entries = 0; 10563 ctsio->kern_data_resid = 0; 10564 ctsio->kern_rel_offset = 0; 10565 10566 if (data_len < alloc_len) { 10567 ctsio->residual = alloc_len - data_len; 10568 ctsio->kern_data_len = data_len; 10569 ctsio->kern_total_len = data_len; 10570 } else { 10571 ctsio->residual = 0; 10572 ctsio->kern_data_len = alloc_len; 10573 ctsio->kern_total_len = alloc_len; 10574 } 10575 10576 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10577 hdr->state1 = 0x00; 10578 hdr->state2 = 0xe0; 10579 scsi_ulto3b(0, hdr->lba); 10580 hdr->slots_num = 0; 10581 scsi_ulto2b(0, hdr->slots_length); 10582 10583 ctl_set_success(ctsio); 10584 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10585 ctsio->be_move_done = ctl_config_move_done; 10586 ctl_datamove((union ctl_io *)ctsio); 10587 return (CTL_RETVAL_COMPLETE); 10588} 10589 10590static void 10591ctl_ultomsf(uint32_t lba, uint8_t *buf) 10592{ 10593 10594 lba += 150; 10595 buf[0] = 0; 10596 buf[1] = bin2bcd((lba / 75) / 60); 10597 buf[2] = bin2bcd((lba / 75) % 60); 10598 buf[3] = bin2bcd(lba % 75); 10599} 10600 10601int 10602ctl_read_toc(struct ctl_scsiio *ctsio) 10603{ 10604 struct ctl_lun *lun = CTL_LUN(ctsio); 10605 struct scsi_read_toc_hdr *hdr; 10606 struct scsi_read_toc_type01_descr *descr; 10607 struct scsi_read_toc *cdb; 10608 uint32_t alloc_len, data_len; 10609 int format, msf; 10610 10611 cdb = (struct scsi_read_toc *)ctsio->cdb; 10612 msf = (cdb->byte2 & CD_MSF) != 0; 10613 format = cdb->format; 10614 alloc_len = scsi_2btoul(cdb->data_len); 10615 10616 data_len = sizeof(struct scsi_read_toc_hdr); 10617 if (format == 0) 10618 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10619 else 10620 data_len += sizeof(struct scsi_read_toc_type01_descr); 10621 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10622 ctsio->kern_sg_entries = 0; 10623 ctsio->kern_data_resid = 0; 10624 ctsio->kern_rel_offset = 0; 10625 10626 if (data_len < alloc_len) { 10627 ctsio->residual = alloc_len - data_len; 10628 ctsio->kern_data_len = data_len; 10629 ctsio->kern_total_len = data_len; 10630 } else { 10631 ctsio->residual = 0; 10632 ctsio->kern_data_len = alloc_len; 10633 ctsio->kern_total_len = alloc_len; 10634 } 10635 10636 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10637 if (format == 0) { 10638 scsi_ulto2b(0x12, hdr->data_length); 10639 hdr->first = 1; 10640 hdr->last = 1; 10641 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10642 descr->addr_ctl = 0x14; 10643 descr->track_number = 1; 10644 if (msf) 10645 ctl_ultomsf(0, descr->track_start); 10646 else 10647 scsi_ulto4b(0, descr->track_start); 10648 descr++; 10649 descr->addr_ctl = 0x14; 10650 descr->track_number = 0xaa; 10651 if (msf) 10652 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10653 else 10654 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10655 } else { 10656 scsi_ulto2b(0x0a, hdr->data_length); 10657 hdr->first = 1; 10658 hdr->last = 1; 10659 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10660 descr->addr_ctl = 0x14; 10661 descr->track_number = 1; 10662 if (msf) 10663 ctl_ultomsf(0, descr->track_start); 10664 else 10665 scsi_ulto4b(0, descr->track_start); 10666 } 10667 10668 ctl_set_success(ctsio); 10669 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10670 ctsio->be_move_done = ctl_config_move_done; 10671 ctl_datamove((union ctl_io *)ctsio); 10672 return (CTL_RETVAL_COMPLETE); 10673} 10674 10675/* 10676 * For known CDB types, parse the LBA and length. 10677 */ 10678static int 10679ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10680{ 10681 if (io->io_hdr.io_type != CTL_IO_SCSI) 10682 return (1); 10683 10684 switch (io->scsiio.cdb[0]) { 10685 case COMPARE_AND_WRITE: { 10686 struct scsi_compare_and_write *cdb; 10687 10688 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10689 10690 *lba = scsi_8btou64(cdb->addr); 10691 *len = cdb->length; 10692 break; 10693 } 10694 case READ_6: 10695 case WRITE_6: { 10696 struct scsi_rw_6 *cdb; 10697 10698 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10699 10700 *lba = scsi_3btoul(cdb->addr); 10701 /* only 5 bits are valid in the most significant address byte */ 10702 *lba &= 0x1fffff; 10703 *len = cdb->length; 10704 break; 10705 } 10706 case READ_10: 10707 case WRITE_10: { 10708 struct scsi_rw_10 *cdb; 10709 10710 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10711 10712 *lba = scsi_4btoul(cdb->addr); 10713 *len = scsi_2btoul(cdb->length); 10714 break; 10715 } 10716 case WRITE_VERIFY_10: { 10717 struct scsi_write_verify_10 *cdb; 10718 10719 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10720 10721 *lba = scsi_4btoul(cdb->addr); 10722 *len = scsi_2btoul(cdb->length); 10723 break; 10724 } 10725 case READ_12: 10726 case WRITE_12: { 10727 struct scsi_rw_12 *cdb; 10728 10729 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10730 10731 *lba = scsi_4btoul(cdb->addr); 10732 *len = scsi_4btoul(cdb->length); 10733 break; 10734 } 10735 case WRITE_VERIFY_12: { 10736 struct scsi_write_verify_12 *cdb; 10737 10738 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10739 10740 *lba = scsi_4btoul(cdb->addr); 10741 *len = scsi_4btoul(cdb->length); 10742 break; 10743 } 10744 case READ_16: 10745 case WRITE_16: { 10746 struct scsi_rw_16 *cdb; 10747 10748 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10749 10750 *lba = scsi_8btou64(cdb->addr); 10751 *len = scsi_4btoul(cdb->length); 10752 break; 10753 } 10754 case WRITE_ATOMIC_16: { 10755 struct scsi_write_atomic_16 *cdb; 10756 10757 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10758 10759 *lba = scsi_8btou64(cdb->addr); 10760 *len = scsi_2btoul(cdb->length); 10761 break; 10762 } 10763 case WRITE_VERIFY_16: { 10764 struct scsi_write_verify_16 *cdb; 10765 10766 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10767 10768 *lba = scsi_8btou64(cdb->addr); 10769 *len = scsi_4btoul(cdb->length); 10770 break; 10771 } 10772 case WRITE_SAME_10: { 10773 struct scsi_write_same_10 *cdb; 10774 10775 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10776 10777 *lba = scsi_4btoul(cdb->addr); 10778 *len = scsi_2btoul(cdb->length); 10779 break; 10780 } 10781 case WRITE_SAME_16: { 10782 struct scsi_write_same_16 *cdb; 10783 10784 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10785 10786 *lba = scsi_8btou64(cdb->addr); 10787 *len = scsi_4btoul(cdb->length); 10788 break; 10789 } 10790 case VERIFY_10: { 10791 struct scsi_verify_10 *cdb; 10792 10793 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10794 10795 *lba = scsi_4btoul(cdb->addr); 10796 *len = scsi_2btoul(cdb->length); 10797 break; 10798 } 10799 case VERIFY_12: { 10800 struct scsi_verify_12 *cdb; 10801 10802 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10803 10804 *lba = scsi_4btoul(cdb->addr); 10805 *len = scsi_4btoul(cdb->length); 10806 break; 10807 } 10808 case VERIFY_16: { 10809 struct scsi_verify_16 *cdb; 10810 10811 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10812 10813 *lba = scsi_8btou64(cdb->addr); 10814 *len = scsi_4btoul(cdb->length); 10815 break; 10816 } 10817 case UNMAP: { 10818 *lba = 0; 10819 *len = UINT64_MAX; 10820 break; 10821 } 10822 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10823 struct scsi_get_lba_status *cdb; 10824 10825 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10826 *lba = scsi_8btou64(cdb->addr); 10827 *len = UINT32_MAX; 10828 break; 10829 } 10830 default: 10831 return (1); 10832 break; /* NOTREACHED */ 10833 } 10834 10835 return (0); 10836} 10837 10838static ctl_action 10839ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10840 bool seq) 10841{ 10842 uint64_t endlba1, endlba2; 10843 10844 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10845 endlba2 = lba2 + len2 - 1; 10846 10847 if ((endlba1 < lba2) || (endlba2 < lba1)) 10848 return (CTL_ACTION_PASS); 10849 else 10850 return (CTL_ACTION_BLOCK); 10851} 10852 10853static int 10854ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10855{ 10856 struct ctl_ptr_len_flags *ptrlen; 10857 struct scsi_unmap_desc *buf, *end, *range; 10858 uint64_t lba; 10859 uint32_t len; 10860 10861 /* If not UNMAP -- go other way. */ 10862 if (io->io_hdr.io_type != CTL_IO_SCSI || 10863 io->scsiio.cdb[0] != UNMAP) 10864 return (CTL_ACTION_ERROR); 10865 10866 /* If UNMAP without data -- block and wait for data. */ 10867 ptrlen = (struct ctl_ptr_len_flags *) 10868 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10869 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10870 ptrlen->ptr == NULL) 10871 return (CTL_ACTION_BLOCK); 10872 10873 /* UNMAP with data -- check for collision. */ 10874 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10875 end = buf + ptrlen->len / sizeof(*buf); 10876 for (range = buf; range < end; range++) { 10877 lba = scsi_8btou64(range->lba); 10878 len = scsi_4btoul(range->length); 10879 if ((lba < lba2 + len2) && (lba + len > lba2)) 10880 return (CTL_ACTION_BLOCK); 10881 } 10882 return (CTL_ACTION_PASS); 10883} 10884 10885static ctl_action 10886ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10887{ 10888 uint64_t lba1, lba2; 10889 uint64_t len1, len2; 10890 int retval; 10891 10892 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10893 return (CTL_ACTION_ERROR); 10894 10895 retval = ctl_extent_check_unmap(io1, lba2, len2); 10896 if (retval != CTL_ACTION_ERROR) 10897 return (retval); 10898 10899 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10900 return (CTL_ACTION_ERROR); 10901 10902 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10903 seq = FALSE; 10904 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10905} 10906 10907static ctl_action 10908ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10909{ 10910 uint64_t lba1, lba2; 10911 uint64_t len1, len2; 10912 10913 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10914 return (CTL_ACTION_PASS); 10915 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10916 return (CTL_ACTION_ERROR); 10917 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10918 return (CTL_ACTION_ERROR); 10919 10920 if (lba1 + len1 == lba2) 10921 return (CTL_ACTION_BLOCK); 10922 return (CTL_ACTION_PASS); 10923} 10924 10925static ctl_action 10926ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10927 union ctl_io *ooa_io) 10928{ 10929 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10930 const ctl_serialize_action *serialize_row; 10931 10932 /* 10933 * The initiator attempted multiple untagged commands at the same 10934 * time. Can't do that. 10935 */ 10936 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10937 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10938 && ((pending_io->io_hdr.nexus.targ_port == 10939 ooa_io->io_hdr.nexus.targ_port) 10940 && (pending_io->io_hdr.nexus.initid == 10941 ooa_io->io_hdr.nexus.initid)) 10942 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10943 CTL_FLAG_STATUS_SENT)) == 0)) 10944 return (CTL_ACTION_OVERLAP); 10945 10946 /* 10947 * The initiator attempted to send multiple tagged commands with 10948 * the same ID. (It's fine if different initiators have the same 10949 * tag ID.) 10950 * 10951 * Even if all of those conditions are true, we don't kill the I/O 10952 * if the command ahead of us has been aborted. We won't end up 10953 * sending it to the FETD, and it's perfectly legal to resend a 10954 * command with the same tag number as long as the previous 10955 * instance of this tag number has been aborted somehow. 10956 */ 10957 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10958 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10959 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10960 && ((pending_io->io_hdr.nexus.targ_port == 10961 ooa_io->io_hdr.nexus.targ_port) 10962 && (pending_io->io_hdr.nexus.initid == 10963 ooa_io->io_hdr.nexus.initid)) 10964 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10965 CTL_FLAG_STATUS_SENT)) == 0)) 10966 return (CTL_ACTION_OVERLAP_TAG); 10967 10968 /* 10969 * If we get a head of queue tag, SAM-3 says that we should 10970 * immediately execute it. 10971 * 10972 * What happens if this command would normally block for some other 10973 * reason? e.g. a request sense with a head of queue tag 10974 * immediately after a write. Normally that would block, but this 10975 * will result in its getting executed immediately... 10976 * 10977 * We currently return "pass" instead of "skip", so we'll end up 10978 * going through the rest of the queue to check for overlapped tags. 10979 * 10980 * XXX KDM check for other types of blockage first?? 10981 */ 10982 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10983 return (CTL_ACTION_PASS); 10984 10985 /* 10986 * Ordered tags have to block until all items ahead of them 10987 * have completed. If we get called with an ordered tag, we always 10988 * block, if something else is ahead of us in the queue. 10989 */ 10990 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10991 return (CTL_ACTION_BLOCK); 10992 10993 /* 10994 * Simple tags get blocked until all head of queue and ordered tags 10995 * ahead of them have completed. I'm lumping untagged commands in 10996 * with simple tags here. XXX KDM is that the right thing to do? 10997 */ 10998 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10999 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11000 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11001 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11002 return (CTL_ACTION_BLOCK); 11003 11004 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11005 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 11006 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 11007 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 11008 pending_io->scsiio.cdb[1], pending_io)); 11009 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11010 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 11011 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 11012 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 11013 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 11014 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 11015 ooa_io->scsiio.cdb[1], ooa_io)); 11016 11017 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11018 11019 switch (serialize_row[pending_entry->seridx]) { 11020 case CTL_SER_BLOCK: 11021 return (CTL_ACTION_BLOCK); 11022 case CTL_SER_EXTENT: 11023 return (ctl_extent_check(ooa_io, pending_io, 11024 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11025 case CTL_SER_EXTENTOPT: 11026 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11027 SCP_QUEUE_ALG_UNRESTRICTED) 11028 return (ctl_extent_check(ooa_io, pending_io, 11029 (lun->be_lun && 11030 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11031 return (CTL_ACTION_PASS); 11032 case CTL_SER_EXTENTSEQ: 11033 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11034 return (ctl_extent_check_seq(ooa_io, pending_io)); 11035 return (CTL_ACTION_PASS); 11036 case CTL_SER_PASS: 11037 return (CTL_ACTION_PASS); 11038 case CTL_SER_BLOCKOPT: 11039 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11040 SCP_QUEUE_ALG_UNRESTRICTED) 11041 return (CTL_ACTION_BLOCK); 11042 return (CTL_ACTION_PASS); 11043 case CTL_SER_SKIP: 11044 return (CTL_ACTION_SKIP); 11045 default: 11046 panic("%s: Invalid serialization value %d for %d => %d", 11047 __func__, serialize_row[pending_entry->seridx], 11048 pending_entry->seridx, ooa_entry->seridx); 11049 } 11050 11051 return (CTL_ACTION_ERROR); 11052} 11053 11054/* 11055 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11056 * Assumptions: 11057 * - pending_io is generally either incoming, or on the blocked queue 11058 * - starting I/O is the I/O we want to start the check with. 11059 */ 11060static ctl_action 11061ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11062 union ctl_io *starting_io) 11063{ 11064 union ctl_io *ooa_io; 11065 ctl_action action; 11066 11067 mtx_assert(&lun->lun_lock, MA_OWNED); 11068 11069 /* 11070 * Run back along the OOA queue, starting with the current 11071 * blocked I/O and going through every I/O before it on the 11072 * queue. If starting_io is NULL, we'll just end up returning 11073 * CTL_ACTION_PASS. 11074 */ 11075 for (ooa_io = starting_io; ooa_io != NULL; 11076 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11077 ooa_links)){ 11078 11079 /* 11080 * This routine just checks to see whether 11081 * cur_blocked is blocked by ooa_io, which is ahead 11082 * of it in the queue. It doesn't queue/dequeue 11083 * cur_blocked. 11084 */ 11085 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11086 switch (action) { 11087 case CTL_ACTION_BLOCK: 11088 case CTL_ACTION_OVERLAP: 11089 case CTL_ACTION_OVERLAP_TAG: 11090 case CTL_ACTION_SKIP: 11091 case CTL_ACTION_ERROR: 11092 return (action); 11093 break; /* NOTREACHED */ 11094 case CTL_ACTION_PASS: 11095 break; 11096 default: 11097 panic("%s: Invalid action %d\n", __func__, action); 11098 } 11099 } 11100 11101 return (CTL_ACTION_PASS); 11102} 11103 11104/* 11105 * Assumptions: 11106 * - An I/O has just completed, and has been removed from the per-LUN OOA 11107 * queue, so some items on the blocked queue may now be unblocked. 11108 */ 11109static int 11110ctl_check_blocked(struct ctl_lun *lun) 11111{ 11112 struct ctl_softc *softc = lun->ctl_softc; 11113 union ctl_io *cur_blocked, *next_blocked; 11114 11115 mtx_assert(&lun->lun_lock, MA_OWNED); 11116 11117 /* 11118 * Run forward from the head of the blocked queue, checking each 11119 * entry against the I/Os prior to it on the OOA queue to see if 11120 * there is still any blockage. 11121 * 11122 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11123 * with our removing a variable on it while it is traversing the 11124 * list. 11125 */ 11126 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11127 cur_blocked != NULL; cur_blocked = next_blocked) { 11128 union ctl_io *prev_ooa; 11129 ctl_action action; 11130 11131 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11132 blocked_links); 11133 11134 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11135 ctl_ooaq, ooa_links); 11136 11137 /* 11138 * If cur_blocked happens to be the first item in the OOA 11139 * queue now, prev_ooa will be NULL, and the action 11140 * returned will just be CTL_ACTION_PASS. 11141 */ 11142 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11143 11144 switch (action) { 11145 case CTL_ACTION_BLOCK: 11146 /* Nothing to do here, still blocked */ 11147 break; 11148 case CTL_ACTION_OVERLAP: 11149 case CTL_ACTION_OVERLAP_TAG: 11150 /* 11151 * This shouldn't happen! In theory we've already 11152 * checked this command for overlap... 11153 */ 11154 break; 11155 case CTL_ACTION_PASS: 11156 case CTL_ACTION_SKIP: { 11157 const struct ctl_cmd_entry *entry; 11158 11159 /* 11160 * The skip case shouldn't happen, this transaction 11161 * should have never made it onto the blocked queue. 11162 */ 11163 /* 11164 * This I/O is no longer blocked, we can remove it 11165 * from the blocked queue. Since this is a TAILQ 11166 * (doubly linked list), we can do O(1) removals 11167 * from any place on the list. 11168 */ 11169 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11170 blocked_links); 11171 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11172 11173 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11174 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11175 /* 11176 * Need to send IO back to original side to 11177 * run 11178 */ 11179 union ctl_ha_msg msg_info; 11180 11181 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11182 msg_info.hdr.original_sc = 11183 cur_blocked->io_hdr.original_sc; 11184 msg_info.hdr.serializing_sc = cur_blocked; 11185 msg_info.hdr.msg_type = CTL_MSG_R2R; 11186 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11187 sizeof(msg_info.hdr), M_NOWAIT); 11188 break; 11189 } 11190 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11191 11192 /* 11193 * Check this I/O for LUN state changes that may 11194 * have happened while this command was blocked. 11195 * The LUN state may have been changed by a command 11196 * ahead of us in the queue, so we need to re-check 11197 * for any states that can be caused by SCSI 11198 * commands. 11199 */ 11200 if (ctl_scsiio_lun_check(lun, entry, 11201 &cur_blocked->scsiio) == 0) { 11202 cur_blocked->io_hdr.flags |= 11203 CTL_FLAG_IS_WAS_ON_RTR; 11204 ctl_enqueue_rtr(cur_blocked); 11205 } else 11206 ctl_done(cur_blocked); 11207 break; 11208 } 11209 default: 11210 /* 11211 * This probably shouldn't happen -- we shouldn't 11212 * get CTL_ACTION_ERROR, or anything else. 11213 */ 11214 break; 11215 } 11216 } 11217 11218 return (CTL_RETVAL_COMPLETE); 11219} 11220 11221/* 11222 * This routine (with one exception) checks LUN flags that can be set by 11223 * commands ahead of us in the OOA queue. These flags have to be checked 11224 * when a command initially comes in, and when we pull a command off the 11225 * blocked queue and are preparing to execute it. The reason we have to 11226 * check these flags for commands on the blocked queue is that the LUN 11227 * state may have been changed by a command ahead of us while we're on the 11228 * blocked queue. 11229 * 11230 * Ordering is somewhat important with these checks, so please pay 11231 * careful attention to the placement of any new checks. 11232 */ 11233static int 11234ctl_scsiio_lun_check(struct ctl_lun *lun, 11235 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11236{ 11237 struct ctl_softc *softc = lun->ctl_softc; 11238 int retval; 11239 uint32_t residx; 11240 11241 retval = 0; 11242 11243 mtx_assert(&lun->lun_lock, MA_OWNED); 11244 11245 /* 11246 * If this shelf is a secondary shelf controller, we may have to 11247 * reject some commands disallowed by HA mode and link state. 11248 */ 11249 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11250 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11251 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11252 ctl_set_lun_unavail(ctsio); 11253 retval = 1; 11254 goto bailout; 11255 } 11256 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11257 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11258 ctl_set_lun_transit(ctsio); 11259 retval = 1; 11260 goto bailout; 11261 } 11262 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11263 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11264 ctl_set_lun_standby(ctsio); 11265 retval = 1; 11266 goto bailout; 11267 } 11268 11269 /* The rest of checks are only done on executing side */ 11270 if (softc->ha_mode == CTL_HA_MODE_XFER) 11271 goto bailout; 11272 } 11273 11274 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11275 if (lun->be_lun && 11276 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11277 ctl_set_hw_write_protected(ctsio); 11278 retval = 1; 11279 goto bailout; 11280 } 11281 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11282 ctl_set_sense(ctsio, /*current_error*/ 1, 11283 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11284 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11285 retval = 1; 11286 goto bailout; 11287 } 11288 } 11289 11290 /* 11291 * Check for a reservation conflict. If this command isn't allowed 11292 * even on reserved LUNs, and if this initiator isn't the one who 11293 * reserved us, reject the command with a reservation conflict. 11294 */ 11295 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11296 if ((lun->flags & CTL_LUN_RESERVED) 11297 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11298 if (lun->res_idx != residx) { 11299 ctl_set_reservation_conflict(ctsio); 11300 retval = 1; 11301 goto bailout; 11302 } 11303 } 11304 11305 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11306 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11307 /* No reservation or command is allowed. */; 11308 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11309 (lun->pr_res_type == SPR_TYPE_WR_EX || 11310 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11311 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11312 /* The command is allowed for Write Exclusive resv. */; 11313 } else { 11314 /* 11315 * if we aren't registered or it's a res holder type 11316 * reservation and this isn't the res holder then set a 11317 * conflict. 11318 */ 11319 if (ctl_get_prkey(lun, residx) == 0 || 11320 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11321 ctl_set_reservation_conflict(ctsio); 11322 retval = 1; 11323 goto bailout; 11324 } 11325 } 11326 11327 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11328 if (lun->flags & CTL_LUN_EJECTED) 11329 ctl_set_lun_ejected(ctsio); 11330 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11331 if (lun->flags & CTL_LUN_REMOVABLE) 11332 ctl_set_lun_no_media(ctsio); 11333 else 11334 ctl_set_lun_int_reqd(ctsio); 11335 } else if (lun->flags & CTL_LUN_STOPPED) 11336 ctl_set_lun_stopped(ctsio); 11337 else 11338 goto bailout; 11339 retval = 1; 11340 goto bailout; 11341 } 11342 11343bailout: 11344 return (retval); 11345} 11346 11347static void 11348ctl_failover_io(union ctl_io *io, int have_lock) 11349{ 11350 ctl_set_busy(&io->scsiio); 11351 ctl_done(io); 11352} 11353 11354static void 11355ctl_failover_lun(union ctl_io *rio) 11356{ 11357 struct ctl_softc *softc = CTL_SOFTC(rio); 11358 struct ctl_lun *lun; 11359 struct ctl_io_hdr *io, *next_io; 11360 uint32_t targ_lun; 11361 11362 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11363 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11364 11365 /* Find and lock the LUN. */ 11366 mtx_lock(&softc->ctl_lock); 11367 if (targ_lun > CTL_MAX_LUNS || 11368 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11369 mtx_unlock(&softc->ctl_lock); 11370 return; 11371 } 11372 mtx_lock(&lun->lun_lock); 11373 mtx_unlock(&softc->ctl_lock); 11374 if (lun->flags & CTL_LUN_DISABLED) { 11375 mtx_unlock(&lun->lun_lock); 11376 return; 11377 } 11378 11379 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11380 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11381 /* We are master */ 11382 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11383 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11384 io->flags |= CTL_FLAG_ABORT; 11385 io->flags |= CTL_FLAG_FAILOVER; 11386 } else { /* This can be only due to DATAMOVE */ 11387 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11388 io->flags &= ~CTL_FLAG_DMA_INPROG; 11389 io->flags |= CTL_FLAG_IO_ACTIVE; 11390 io->port_status = 31340; 11391 ctl_enqueue_isc((union ctl_io *)io); 11392 } 11393 } 11394 /* We are slave */ 11395 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11396 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11397 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11398 io->flags |= CTL_FLAG_FAILOVER; 11399 } else { 11400 ctl_set_busy(&((union ctl_io *)io)-> 11401 scsiio); 11402 ctl_done((union ctl_io *)io); 11403 } 11404 } 11405 } 11406 } else { /* SERIALIZE modes */ 11407 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11408 next_io) { 11409 /* We are master */ 11410 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11411 TAILQ_REMOVE(&lun->blocked_queue, io, 11412 blocked_links); 11413 io->flags &= ~CTL_FLAG_BLOCKED; 11414 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11415 ctl_free_io((union ctl_io *)io); 11416 } 11417 } 11418 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11419 /* We are master */ 11420 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11421 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11422 ctl_free_io((union ctl_io *)io); 11423 } 11424 /* We are slave */ 11425 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11426 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11427 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11428 ctl_set_busy(&((union ctl_io *)io)-> 11429 scsiio); 11430 ctl_done((union ctl_io *)io); 11431 } 11432 } 11433 } 11434 ctl_check_blocked(lun); 11435 } 11436 mtx_unlock(&lun->lun_lock); 11437} 11438 11439static int 11440ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11441{ 11442 struct ctl_lun *lun; 11443 const struct ctl_cmd_entry *entry; 11444 uint32_t initidx, targ_lun; 11445 int retval = 0; 11446 11447 lun = NULL; 11448 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11449 if (targ_lun < CTL_MAX_LUNS) 11450 lun = softc->ctl_luns[targ_lun]; 11451 if (lun) { 11452 /* 11453 * If the LUN is invalid, pretend that it doesn't exist. 11454 * It will go away as soon as all pending I/O has been 11455 * completed. 11456 */ 11457 mtx_lock(&lun->lun_lock); 11458 if (lun->flags & CTL_LUN_DISABLED) { 11459 mtx_unlock(&lun->lun_lock); 11460 lun = NULL; 11461 } 11462 } 11463 CTL_LUN(ctsio) = lun; 11464 if (lun) { 11465 CTL_BACKEND_LUN(ctsio) = lun->be_lun; 11466 11467 /* 11468 * Every I/O goes into the OOA queue for a particular LUN, 11469 * and stays there until completion. 11470 */ 11471#ifdef CTL_TIME_IO 11472 if (TAILQ_EMPTY(&lun->ooa_queue)) 11473 lun->idle_time += getsbinuptime() - lun->last_busy; 11474#endif 11475 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11476 } 11477 11478 /* Get command entry and return error if it is unsuppotyed. */ 11479 entry = ctl_validate_command(ctsio); 11480 if (entry == NULL) { 11481 if (lun) 11482 mtx_unlock(&lun->lun_lock); 11483 return (retval); 11484 } 11485 11486 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11487 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11488 11489 /* 11490 * Check to see whether we can send this command to LUNs that don't 11491 * exist. This should pretty much only be the case for inquiry 11492 * and request sense. Further checks, below, really require having 11493 * a LUN, so we can't really check the command anymore. Just put 11494 * it on the rtr queue. 11495 */ 11496 if (lun == NULL) { 11497 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11498 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11499 ctl_enqueue_rtr((union ctl_io *)ctsio); 11500 return (retval); 11501 } 11502 11503 ctl_set_unsupported_lun(ctsio); 11504 ctl_done((union ctl_io *)ctsio); 11505 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11506 return (retval); 11507 } else { 11508 /* 11509 * Make sure we support this particular command on this LUN. 11510 * e.g., we don't support writes to the control LUN. 11511 */ 11512 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11513 mtx_unlock(&lun->lun_lock); 11514 ctl_set_invalid_opcode(ctsio); 11515 ctl_done((union ctl_io *)ctsio); 11516 return (retval); 11517 } 11518 } 11519 11520 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11521 11522#ifdef CTL_WITH_CA 11523 /* 11524 * If we've got a request sense, it'll clear the contingent 11525 * allegiance condition. Otherwise, if we have a CA condition for 11526 * this initiator, clear it, because it sent down a command other 11527 * than request sense. 11528 */ 11529 if ((ctsio->cdb[0] != REQUEST_SENSE) 11530 && (ctl_is_set(lun->have_ca, initidx))) 11531 ctl_clear_mask(lun->have_ca, initidx); 11532#endif 11533 11534 /* 11535 * If the command has this flag set, it handles its own unit 11536 * attention reporting, we shouldn't do anything. Otherwise we 11537 * check for any pending unit attentions, and send them back to the 11538 * initiator. We only do this when a command initially comes in, 11539 * not when we pull it off the blocked queue. 11540 * 11541 * According to SAM-3, section 5.3.2, the order that things get 11542 * presented back to the host is basically unit attentions caused 11543 * by some sort of reset event, busy status, reservation conflicts 11544 * or task set full, and finally any other status. 11545 * 11546 * One issue here is that some of the unit attentions we report 11547 * don't fall into the "reset" category (e.g. "reported luns data 11548 * has changed"). So reporting it here, before the reservation 11549 * check, may be technically wrong. I guess the only thing to do 11550 * would be to check for and report the reset events here, and then 11551 * check for the other unit attention types after we check for a 11552 * reservation conflict. 11553 * 11554 * XXX KDM need to fix this 11555 */ 11556 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11557 ctl_ua_type ua_type; 11558 u_int sense_len = 0; 11559 11560 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11561 &sense_len, SSD_TYPE_NONE); 11562 if (ua_type != CTL_UA_NONE) { 11563 mtx_unlock(&lun->lun_lock); 11564 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11565 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11566 ctsio->sense_len = sense_len; 11567 ctl_done((union ctl_io *)ctsio); 11568 return (retval); 11569 } 11570 } 11571 11572 11573 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11574 mtx_unlock(&lun->lun_lock); 11575 ctl_done((union ctl_io *)ctsio); 11576 return (retval); 11577 } 11578 11579 /* 11580 * XXX CHD this is where we want to send IO to other side if 11581 * this LUN is secondary on this SC. We will need to make a copy 11582 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11583 * the copy we send as FROM_OTHER. 11584 * We also need to stuff the address of the original IO so we can 11585 * find it easily. Something similar will need be done on the other 11586 * side so when we are done we can find the copy. 11587 */ 11588 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11589 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11590 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11591 union ctl_ha_msg msg_info; 11592 int isc_retval; 11593 11594 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11595 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11596 mtx_unlock(&lun->lun_lock); 11597 11598 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11599 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11600 msg_info.hdr.serializing_sc = NULL; 11601 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11602 msg_info.scsi.tag_num = ctsio->tag_num; 11603 msg_info.scsi.tag_type = ctsio->tag_type; 11604 msg_info.scsi.cdb_len = ctsio->cdb_len; 11605 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11606 11607 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11608 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11609 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11610 ctl_set_busy(ctsio); 11611 ctl_done((union ctl_io *)ctsio); 11612 return (retval); 11613 } 11614 return (retval); 11615 } 11616 11617 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11618 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11619 ctl_ooaq, ooa_links))) { 11620 case CTL_ACTION_BLOCK: 11621 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11622 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11623 blocked_links); 11624 mtx_unlock(&lun->lun_lock); 11625 return (retval); 11626 case CTL_ACTION_PASS: 11627 case CTL_ACTION_SKIP: 11628 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11629 mtx_unlock(&lun->lun_lock); 11630 ctl_enqueue_rtr((union ctl_io *)ctsio); 11631 break; 11632 case CTL_ACTION_OVERLAP: 11633 mtx_unlock(&lun->lun_lock); 11634 ctl_set_overlapped_cmd(ctsio); 11635 ctl_done((union ctl_io *)ctsio); 11636 break; 11637 case CTL_ACTION_OVERLAP_TAG: 11638 mtx_unlock(&lun->lun_lock); 11639 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11640 ctl_done((union ctl_io *)ctsio); 11641 break; 11642 case CTL_ACTION_ERROR: 11643 default: 11644 mtx_unlock(&lun->lun_lock); 11645 ctl_set_internal_failure(ctsio, 11646 /*sks_valid*/ 0, 11647 /*retry_count*/ 0); 11648 ctl_done((union ctl_io *)ctsio); 11649 break; 11650 } 11651 return (retval); 11652} 11653 11654const struct ctl_cmd_entry * 11655ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11656{ 11657 const struct ctl_cmd_entry *entry; 11658 int service_action; 11659 11660 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11661 if (sa) 11662 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11663 if (entry->flags & CTL_CMD_FLAG_SA5) { 11664 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11665 entry = &((const struct ctl_cmd_entry *) 11666 entry->execute)[service_action]; 11667 } 11668 return (entry); 11669} 11670 11671const struct ctl_cmd_entry * 11672ctl_validate_command(struct ctl_scsiio *ctsio) 11673{ 11674 const struct ctl_cmd_entry *entry; 11675 int i, sa; 11676 uint8_t diff; 11677 11678 entry = ctl_get_cmd_entry(ctsio, &sa); 11679 if (entry->execute == NULL) { 11680 if (sa) 11681 ctl_set_invalid_field(ctsio, 11682 /*sks_valid*/ 1, 11683 /*command*/ 1, 11684 /*field*/ 1, 11685 /*bit_valid*/ 1, 11686 /*bit*/ 4); 11687 else 11688 ctl_set_invalid_opcode(ctsio); 11689 ctl_done((union ctl_io *)ctsio); 11690 return (NULL); 11691 } 11692 KASSERT(entry->length > 0, 11693 ("Not defined length for command 0x%02x/0x%02x", 11694 ctsio->cdb[0], ctsio->cdb[1])); 11695 for (i = 1; i < entry->length; i++) { 11696 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11697 if (diff == 0) 11698 continue; 11699 ctl_set_invalid_field(ctsio, 11700 /*sks_valid*/ 1, 11701 /*command*/ 1, 11702 /*field*/ i, 11703 /*bit_valid*/ 1, 11704 /*bit*/ fls(diff) - 1); 11705 ctl_done((union ctl_io *)ctsio); 11706 return (NULL); 11707 } 11708 return (entry); 11709} 11710 11711static int 11712ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11713{ 11714 11715 switch (lun_type) { 11716 case T_DIRECT: 11717 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11718 return (0); 11719 break; 11720 case T_PROCESSOR: 11721 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11722 return (0); 11723 break; 11724 case T_CDROM: 11725 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11726 return (0); 11727 break; 11728 default: 11729 return (0); 11730 } 11731 return (1); 11732} 11733 11734static int 11735ctl_scsiio(struct ctl_scsiio *ctsio) 11736{ 11737 int retval; 11738 const struct ctl_cmd_entry *entry; 11739 11740 retval = CTL_RETVAL_COMPLETE; 11741 11742 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11743 11744 entry = ctl_get_cmd_entry(ctsio, NULL); 11745 11746 /* 11747 * If this I/O has been aborted, just send it straight to 11748 * ctl_done() without executing it. 11749 */ 11750 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11751 ctl_done((union ctl_io *)ctsio); 11752 goto bailout; 11753 } 11754 11755 /* 11756 * All the checks should have been handled by ctl_scsiio_precheck(). 11757 * We should be clear now to just execute the I/O. 11758 */ 11759 retval = entry->execute(ctsio); 11760 11761bailout: 11762 return (retval); 11763} 11764 11765/* 11766 * Since we only implement one target right now, a bus reset simply resets 11767 * our single target. 11768 */ 11769static int 11770ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11771{ 11772 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11773} 11774 11775static int 11776ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11777 ctl_ua_type ua_type) 11778{ 11779 struct ctl_port *port = CTL_PORT(io); 11780 struct ctl_lun *lun; 11781 int retval; 11782 11783 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11784 union ctl_ha_msg msg_info; 11785 11786 msg_info.hdr.nexus = io->io_hdr.nexus; 11787 if (ua_type==CTL_UA_TARG_RESET) 11788 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11789 else 11790 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11791 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11792 msg_info.hdr.original_sc = NULL; 11793 msg_info.hdr.serializing_sc = NULL; 11794 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11795 sizeof(msg_info.task), M_WAITOK); 11796 } 11797 retval = 0; 11798 11799 mtx_lock(&softc->ctl_lock); 11800 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11801 if (port != NULL && 11802 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11803 continue; 11804 retval += ctl_do_lun_reset(lun, io, ua_type); 11805 } 11806 mtx_unlock(&softc->ctl_lock); 11807 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11808 return (retval); 11809} 11810 11811/* 11812 * The LUN should always be set. The I/O is optional, and is used to 11813 * distinguish between I/Os sent by this initiator, and by other 11814 * initiators. We set unit attention for initiators other than this one. 11815 * SAM-3 is vague on this point. It does say that a unit attention should 11816 * be established for other initiators when a LUN is reset (see section 11817 * 5.7.3), but it doesn't specifically say that the unit attention should 11818 * be established for this particular initiator when a LUN is reset. Here 11819 * is the relevant text, from SAM-3 rev 8: 11820 * 11821 * 5.7.2 When a SCSI initiator port aborts its own tasks 11822 * 11823 * When a SCSI initiator port causes its own task(s) to be aborted, no 11824 * notification that the task(s) have been aborted shall be returned to 11825 * the SCSI initiator port other than the completion response for the 11826 * command or task management function action that caused the task(s) to 11827 * be aborted and notification(s) associated with related effects of the 11828 * action (e.g., a reset unit attention condition). 11829 * 11830 * XXX KDM for now, we're setting unit attention for all initiators. 11831 */ 11832static int 11833ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11834{ 11835 union ctl_io *xio; 11836#if 0 11837 uint32_t initidx; 11838#endif 11839 int i; 11840 11841 mtx_lock(&lun->lun_lock); 11842 /* 11843 * Run through the OOA queue and abort each I/O. 11844 */ 11845 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11846 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11847 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11848 } 11849 11850 /* 11851 * This version sets unit attention for every 11852 */ 11853#if 0 11854 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11855 ctl_est_ua_all(lun, initidx, ua_type); 11856#else 11857 ctl_est_ua_all(lun, -1, ua_type); 11858#endif 11859 11860 /* 11861 * A reset (any kind, really) clears reservations established with 11862 * RESERVE/RELEASE. It does not clear reservations established 11863 * with PERSISTENT RESERVE OUT, but we don't support that at the 11864 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11865 * reservations made with the RESERVE/RELEASE commands, because 11866 * those commands are obsolete in SPC-3. 11867 */ 11868 lun->flags &= ~CTL_LUN_RESERVED; 11869 11870#ifdef CTL_WITH_CA 11871 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11872 ctl_clear_mask(lun->have_ca, i); 11873#endif 11874 lun->prevent_count = 0; 11875 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11876 ctl_clear_mask(lun->prevent, i); 11877 mtx_unlock(&lun->lun_lock); 11878 11879 return (0); 11880} 11881 11882static int 11883ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11884{ 11885 struct ctl_lun *lun; 11886 uint32_t targ_lun; 11887 int retval; 11888 11889 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11890 mtx_lock(&softc->ctl_lock); 11891 if (targ_lun >= CTL_MAX_LUNS || 11892 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11893 mtx_unlock(&softc->ctl_lock); 11894 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11895 return (1); 11896 } 11897 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11898 mtx_unlock(&softc->ctl_lock); 11899 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11900 11901 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11902 union ctl_ha_msg msg_info; 11903 11904 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11905 msg_info.hdr.nexus = io->io_hdr.nexus; 11906 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11907 msg_info.hdr.original_sc = NULL; 11908 msg_info.hdr.serializing_sc = NULL; 11909 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11910 sizeof(msg_info.task), M_WAITOK); 11911 } 11912 return (retval); 11913} 11914 11915static void 11916ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11917 int other_sc) 11918{ 11919 union ctl_io *xio; 11920 11921 mtx_assert(&lun->lun_lock, MA_OWNED); 11922 11923 /* 11924 * Run through the OOA queue and attempt to find the given I/O. 11925 * The target port, initiator ID, tag type and tag number have to 11926 * match the values that we got from the initiator. If we have an 11927 * untagged command to abort, simply abort the first untagged command 11928 * we come to. We only allow one untagged command at a time of course. 11929 */ 11930 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11931 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11932 11933 if ((targ_port == UINT32_MAX || 11934 targ_port == xio->io_hdr.nexus.targ_port) && 11935 (init_id == UINT32_MAX || 11936 init_id == xio->io_hdr.nexus.initid)) { 11937 if (targ_port != xio->io_hdr.nexus.targ_port || 11938 init_id != xio->io_hdr.nexus.initid) 11939 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11940 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11941 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11942 union ctl_ha_msg msg_info; 11943 11944 msg_info.hdr.nexus = xio->io_hdr.nexus; 11945 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11946 msg_info.task.tag_num = xio->scsiio.tag_num; 11947 msg_info.task.tag_type = xio->scsiio.tag_type; 11948 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11949 msg_info.hdr.original_sc = NULL; 11950 msg_info.hdr.serializing_sc = NULL; 11951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11952 sizeof(msg_info.task), M_NOWAIT); 11953 } 11954 } 11955 } 11956} 11957 11958static int 11959ctl_abort_task_set(union ctl_io *io) 11960{ 11961 struct ctl_softc *softc = CTL_SOFTC(io); 11962 struct ctl_lun *lun; 11963 uint32_t targ_lun; 11964 11965 /* 11966 * Look up the LUN. 11967 */ 11968 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11969 mtx_lock(&softc->ctl_lock); 11970 if (targ_lun >= CTL_MAX_LUNS || 11971 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11972 mtx_unlock(&softc->ctl_lock); 11973 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11974 return (1); 11975 } 11976 11977 mtx_lock(&lun->lun_lock); 11978 mtx_unlock(&softc->ctl_lock); 11979 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11980 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11981 io->io_hdr.nexus.initid, 11982 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11983 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11984 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11985 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11986 } 11987 mtx_unlock(&lun->lun_lock); 11988 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11989 return (0); 11990} 11991 11992static int 11993ctl_i_t_nexus_reset(union ctl_io *io) 11994{ 11995 struct ctl_softc *softc = CTL_SOFTC(io); 11996 struct ctl_lun *lun; 11997 uint32_t initidx; 11998 11999 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12000 union ctl_ha_msg msg_info; 12001 12002 msg_info.hdr.nexus = io->io_hdr.nexus; 12003 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12004 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12005 msg_info.hdr.original_sc = NULL; 12006 msg_info.hdr.serializing_sc = NULL; 12007 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12008 sizeof(msg_info.task), M_WAITOK); 12009 } 12010 12011 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12012 mtx_lock(&softc->ctl_lock); 12013 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12014 mtx_lock(&lun->lun_lock); 12015 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12016 io->io_hdr.nexus.initid, 1); 12017#ifdef CTL_WITH_CA 12018 ctl_clear_mask(lun->have_ca, initidx); 12019#endif 12020 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12021 lun->flags &= ~CTL_LUN_RESERVED; 12022 if (ctl_is_set(lun->prevent, initidx)) { 12023 ctl_clear_mask(lun->prevent, initidx); 12024 lun->prevent_count--; 12025 } 12026 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12027 mtx_unlock(&lun->lun_lock); 12028 } 12029 mtx_unlock(&softc->ctl_lock); 12030 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12031 return (0); 12032} 12033 12034static int 12035ctl_abort_task(union ctl_io *io) 12036{ 12037 struct ctl_softc *softc = CTL_SOFTC(io); 12038 union ctl_io *xio; 12039 struct ctl_lun *lun; 12040#if 0 12041 struct sbuf sb; 12042 char printbuf[128]; 12043#endif 12044 int found; 12045 uint32_t targ_lun; 12046 12047 found = 0; 12048 12049 /* 12050 * Look up the LUN. 12051 */ 12052 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12053 mtx_lock(&softc->ctl_lock); 12054 if (targ_lun >= CTL_MAX_LUNS || 12055 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12056 mtx_unlock(&softc->ctl_lock); 12057 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12058 return (1); 12059 } 12060 12061#if 0 12062 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12063 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12064#endif 12065 12066 mtx_lock(&lun->lun_lock); 12067 mtx_unlock(&softc->ctl_lock); 12068 /* 12069 * Run through the OOA queue and attempt to find the given I/O. 12070 * The target port, initiator ID, tag type and tag number have to 12071 * match the values that we got from the initiator. If we have an 12072 * untagged command to abort, simply abort the first untagged command 12073 * we come to. We only allow one untagged command at a time of course. 12074 */ 12075 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12076 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12077#if 0 12078 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12079 12080 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12081 lun->lun, xio->scsiio.tag_num, 12082 xio->scsiio.tag_type, 12083 (xio->io_hdr.blocked_links.tqe_prev 12084 == NULL) ? "" : " BLOCKED", 12085 (xio->io_hdr.flags & 12086 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12087 (xio->io_hdr.flags & 12088 CTL_FLAG_ABORT) ? " ABORT" : "", 12089 (xio->io_hdr.flags & 12090 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12091 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12092 sbuf_finish(&sb); 12093 printf("%s\n", sbuf_data(&sb)); 12094#endif 12095 12096 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12097 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12098 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12099 continue; 12100 12101 /* 12102 * If the abort says that the task is untagged, the 12103 * task in the queue must be untagged. Otherwise, 12104 * we just check to see whether the tag numbers 12105 * match. This is because the QLogic firmware 12106 * doesn't pass back the tag type in an abort 12107 * request. 12108 */ 12109#if 0 12110 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12111 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12112 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12113#endif 12114 /* 12115 * XXX KDM we've got problems with FC, because it 12116 * doesn't send down a tag type with aborts. So we 12117 * can only really go by the tag number... 12118 * This may cause problems with parallel SCSI. 12119 * Need to figure that out!! 12120 */ 12121 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12122 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12123 found = 1; 12124 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12125 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12126 union ctl_ha_msg msg_info; 12127 12128 msg_info.hdr.nexus = io->io_hdr.nexus; 12129 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12130 msg_info.task.tag_num = io->taskio.tag_num; 12131 msg_info.task.tag_type = io->taskio.tag_type; 12132 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12133 msg_info.hdr.original_sc = NULL; 12134 msg_info.hdr.serializing_sc = NULL; 12135#if 0 12136 printf("Sent Abort to other side\n"); 12137#endif 12138 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12139 sizeof(msg_info.task), M_NOWAIT); 12140 } 12141#if 0 12142 printf("ctl_abort_task: found I/O to abort\n"); 12143#endif 12144 } 12145 } 12146 mtx_unlock(&lun->lun_lock); 12147 12148 if (found == 0) { 12149 /* 12150 * This isn't really an error. It's entirely possible for 12151 * the abort and command completion to cross on the wire. 12152 * This is more of an informative/diagnostic error. 12153 */ 12154#if 0 12155 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12156 "%u:%u:%u tag %d type %d\n", 12157 io->io_hdr.nexus.initid, 12158 io->io_hdr.nexus.targ_port, 12159 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12160 io->taskio.tag_type); 12161#endif 12162 } 12163 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12164 return (0); 12165} 12166 12167static int 12168ctl_query_task(union ctl_io *io, int task_set) 12169{ 12170 struct ctl_softc *softc = CTL_SOFTC(io); 12171 union ctl_io *xio; 12172 struct ctl_lun *lun; 12173 int found = 0; 12174 uint32_t targ_lun; 12175 12176 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12177 mtx_lock(&softc->ctl_lock); 12178 if (targ_lun >= CTL_MAX_LUNS || 12179 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12180 mtx_unlock(&softc->ctl_lock); 12181 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12182 return (1); 12183 } 12184 mtx_lock(&lun->lun_lock); 12185 mtx_unlock(&softc->ctl_lock); 12186 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12187 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12188 12189 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12190 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12191 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12192 continue; 12193 12194 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12195 found = 1; 12196 break; 12197 } 12198 } 12199 mtx_unlock(&lun->lun_lock); 12200 if (found) 12201 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12202 else 12203 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12204 return (0); 12205} 12206 12207static int 12208ctl_query_async_event(union ctl_io *io) 12209{ 12210 struct ctl_softc *softc = CTL_SOFTC(io); 12211 struct ctl_lun *lun; 12212 ctl_ua_type ua; 12213 uint32_t targ_lun, initidx; 12214 12215 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12216 mtx_lock(&softc->ctl_lock); 12217 if (targ_lun >= CTL_MAX_LUNS || 12218 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12219 mtx_unlock(&softc->ctl_lock); 12220 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12221 return (1); 12222 } 12223 mtx_lock(&lun->lun_lock); 12224 mtx_unlock(&softc->ctl_lock); 12225 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12226 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12227 mtx_unlock(&lun->lun_lock); 12228 if (ua != CTL_UA_NONE) 12229 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12230 else 12231 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12232 return (0); 12233} 12234 12235static void 12236ctl_run_task(union ctl_io *io) 12237{ 12238 struct ctl_softc *softc = CTL_SOFTC(io); 12239 int retval = 1; 12240 12241 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12242 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12243 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12244 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12245 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12246 switch (io->taskio.task_action) { 12247 case CTL_TASK_ABORT_TASK: 12248 retval = ctl_abort_task(io); 12249 break; 12250 case CTL_TASK_ABORT_TASK_SET: 12251 case CTL_TASK_CLEAR_TASK_SET: 12252 retval = ctl_abort_task_set(io); 12253 break; 12254 case CTL_TASK_CLEAR_ACA: 12255 break; 12256 case CTL_TASK_I_T_NEXUS_RESET: 12257 retval = ctl_i_t_nexus_reset(io); 12258 break; 12259 case CTL_TASK_LUN_RESET: 12260 retval = ctl_lun_reset(softc, io); 12261 break; 12262 case CTL_TASK_TARGET_RESET: 12263 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12264 break; 12265 case CTL_TASK_BUS_RESET: 12266 retval = ctl_bus_reset(softc, io); 12267 break; 12268 case CTL_TASK_PORT_LOGIN: 12269 break; 12270 case CTL_TASK_PORT_LOGOUT: 12271 break; 12272 case CTL_TASK_QUERY_TASK: 12273 retval = ctl_query_task(io, 0); 12274 break; 12275 case CTL_TASK_QUERY_TASK_SET: 12276 retval = ctl_query_task(io, 1); 12277 break; 12278 case CTL_TASK_QUERY_ASYNC_EVENT: 12279 retval = ctl_query_async_event(io); 12280 break; 12281 default: 12282 printf("%s: got unknown task management event %d\n", 12283 __func__, io->taskio.task_action); 12284 break; 12285 } 12286 if (retval == 0) 12287 io->io_hdr.status = CTL_SUCCESS; 12288 else 12289 io->io_hdr.status = CTL_ERROR; 12290 ctl_done(io); 12291} 12292 12293/* 12294 * For HA operation. Handle commands that come in from the other 12295 * controller. 12296 */ 12297static void 12298ctl_handle_isc(union ctl_io *io) 12299{ 12300 struct ctl_softc *softc = CTL_SOFTC(io); 12301 struct ctl_lun *lun; 12302 const struct ctl_cmd_entry *entry; 12303 uint32_t targ_lun; 12304 12305 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12306 switch (io->io_hdr.msg_type) { 12307 case CTL_MSG_SERIALIZE: 12308 ctl_serialize_other_sc_cmd(&io->scsiio); 12309 break; 12310 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12311 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12312 if (targ_lun >= CTL_MAX_LUNS || 12313 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12314 ctl_done(io); 12315 break; 12316 } 12317 mtx_lock(&lun->lun_lock); 12318 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12319 mtx_unlock(&lun->lun_lock); 12320 ctl_done(io); 12321 break; 12322 } 12323 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12324 mtx_unlock(&lun->lun_lock); 12325 ctl_enqueue_rtr(io); 12326 break; 12327 case CTL_MSG_FINISH_IO: 12328 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12329 ctl_done(io); 12330 break; 12331 } 12332 if (targ_lun >= CTL_MAX_LUNS || 12333 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12334 ctl_free_io(io); 12335 break; 12336 } 12337 mtx_lock(&lun->lun_lock); 12338 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12339 ctl_check_blocked(lun); 12340 mtx_unlock(&lun->lun_lock); 12341 ctl_free_io(io); 12342 break; 12343 case CTL_MSG_PERS_ACTION: 12344 ctl_hndl_per_res_out_on_other_sc(io); 12345 ctl_free_io(io); 12346 break; 12347 case CTL_MSG_BAD_JUJU: 12348 ctl_done(io); 12349 break; 12350 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12351 ctl_datamove_remote(io); 12352 break; 12353 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12354 io->scsiio.be_move_done(io); 12355 break; 12356 case CTL_MSG_FAILOVER: 12357 ctl_failover_lun(io); 12358 ctl_free_io(io); 12359 break; 12360 default: 12361 printf("%s: Invalid message type %d\n", 12362 __func__, io->io_hdr.msg_type); 12363 ctl_free_io(io); 12364 break; 12365 } 12366 12367} 12368 12369 12370/* 12371 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12372 * there is no match. 12373 */ 12374static ctl_lun_error_pattern 12375ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12376{ 12377 const struct ctl_cmd_entry *entry; 12378 ctl_lun_error_pattern filtered_pattern, pattern; 12379 12380 pattern = desc->error_pattern; 12381 12382 /* 12383 * XXX KDM we need more data passed into this function to match a 12384 * custom pattern, and we actually need to implement custom pattern 12385 * matching. 12386 */ 12387 if (pattern & CTL_LUN_PAT_CMD) 12388 return (CTL_LUN_PAT_CMD); 12389 12390 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12391 return (CTL_LUN_PAT_ANY); 12392 12393 entry = ctl_get_cmd_entry(ctsio, NULL); 12394 12395 filtered_pattern = entry->pattern & pattern; 12396 12397 /* 12398 * If the user requested specific flags in the pattern (e.g. 12399 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12400 * flags. 12401 * 12402 * If the user did not specify any flags, it doesn't matter whether 12403 * or not the command supports the flags. 12404 */ 12405 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12406 (pattern & ~CTL_LUN_PAT_MASK)) 12407 return (CTL_LUN_PAT_NONE); 12408 12409 /* 12410 * If the user asked for a range check, see if the requested LBA 12411 * range overlaps with this command's LBA range. 12412 */ 12413 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12414 uint64_t lba1; 12415 uint64_t len1; 12416 ctl_action action; 12417 int retval; 12418 12419 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12420 if (retval != 0) 12421 return (CTL_LUN_PAT_NONE); 12422 12423 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12424 desc->lba_range.len, FALSE); 12425 /* 12426 * A "pass" means that the LBA ranges don't overlap, so 12427 * this doesn't match the user's range criteria. 12428 */ 12429 if (action == CTL_ACTION_PASS) 12430 return (CTL_LUN_PAT_NONE); 12431 } 12432 12433 return (filtered_pattern); 12434} 12435 12436static void 12437ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12438{ 12439 struct ctl_error_desc *desc, *desc2; 12440 12441 mtx_assert(&lun->lun_lock, MA_OWNED); 12442 12443 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12444 ctl_lun_error_pattern pattern; 12445 /* 12446 * Check to see whether this particular command matches 12447 * the pattern in the descriptor. 12448 */ 12449 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12450 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12451 continue; 12452 12453 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12454 case CTL_LUN_INJ_ABORTED: 12455 ctl_set_aborted(&io->scsiio); 12456 break; 12457 case CTL_LUN_INJ_MEDIUM_ERR: 12458 ctl_set_medium_error(&io->scsiio, 12459 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12460 CTL_FLAG_DATA_OUT); 12461 break; 12462 case CTL_LUN_INJ_UA: 12463 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12464 * OCCURRED */ 12465 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12466 break; 12467 case CTL_LUN_INJ_CUSTOM: 12468 /* 12469 * We're assuming the user knows what he is doing. 12470 * Just copy the sense information without doing 12471 * checks. 12472 */ 12473 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12474 MIN(sizeof(desc->custom_sense), 12475 sizeof(io->scsiio.sense_data))); 12476 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12477 io->scsiio.sense_len = SSD_FULL_SIZE; 12478 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12479 break; 12480 case CTL_LUN_INJ_NONE: 12481 default: 12482 /* 12483 * If this is an error injection type we don't know 12484 * about, clear the continuous flag (if it is set) 12485 * so it will get deleted below. 12486 */ 12487 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12488 break; 12489 } 12490 /* 12491 * By default, each error injection action is a one-shot 12492 */ 12493 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12494 continue; 12495 12496 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12497 12498 free(desc, M_CTL); 12499 } 12500} 12501 12502#ifdef CTL_IO_DELAY 12503static void 12504ctl_datamove_timer_wakeup(void *arg) 12505{ 12506 union ctl_io *io; 12507 12508 io = (union ctl_io *)arg; 12509 12510 ctl_datamove(io); 12511} 12512#endif /* CTL_IO_DELAY */ 12513 12514void 12515ctl_datamove(union ctl_io *io) 12516{ 12517 void (*fe_datamove)(union ctl_io *io); 12518 12519 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 12520 12521 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12522 12523#ifdef CTL_TIME_IO 12524 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12525 char str[256]; 12526 char path_str[64]; 12527 struct sbuf sb; 12528 12529 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12530 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12531 12532 sbuf_cat(&sb, path_str); 12533 switch (io->io_hdr.io_type) { 12534 case CTL_IO_SCSI: 12535 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12536 sbuf_printf(&sb, "\n"); 12537 sbuf_cat(&sb, path_str); 12538 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12539 io->scsiio.tag_num, io->scsiio.tag_type); 12540 break; 12541 case CTL_IO_TASK: 12542 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12543 "Tag Type: %d\n", io->taskio.task_action, 12544 io->taskio.tag_num, io->taskio.tag_type); 12545 break; 12546 default: 12547 panic("%s: Invalid CTL I/O type %d\n", 12548 __func__, io->io_hdr.io_type); 12549 } 12550 sbuf_cat(&sb, path_str); 12551 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12552 (intmax_t)time_uptime - io->io_hdr.start_time); 12553 sbuf_finish(&sb); 12554 printf("%s", sbuf_data(&sb)); 12555 } 12556#endif /* CTL_TIME_IO */ 12557 12558#ifdef CTL_IO_DELAY 12559 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12560 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12561 } else { 12562 if ((lun != NULL) 12563 && (lun->delay_info.datamove_delay > 0)) { 12564 12565 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12566 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12567 callout_reset(&io->io_hdr.delay_callout, 12568 lun->delay_info.datamove_delay * hz, 12569 ctl_datamove_timer_wakeup, io); 12570 if (lun->delay_info.datamove_type == 12571 CTL_DELAY_TYPE_ONESHOT) 12572 lun->delay_info.datamove_delay = 0; 12573 return; 12574 } 12575 } 12576#endif 12577 12578 /* 12579 * This command has been aborted. Set the port status, so we fail 12580 * the data move. 12581 */ 12582 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12583 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12584 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12585 io->io_hdr.nexus.targ_port, 12586 io->io_hdr.nexus.targ_lun); 12587 io->io_hdr.port_status = 31337; 12588 /* 12589 * Note that the backend, in this case, will get the 12590 * callback in its context. In other cases it may get 12591 * called in the frontend's interrupt thread context. 12592 */ 12593 io->scsiio.be_move_done(io); 12594 return; 12595 } 12596 12597 /* Don't confuse frontend with zero length data move. */ 12598 if (io->scsiio.kern_data_len == 0) { 12599 io->scsiio.be_move_done(io); 12600 return; 12601 } 12602 12603 fe_datamove = CTL_PORT(io)->fe_datamove; 12604 fe_datamove(io); 12605} 12606 12607static void 12608ctl_send_datamove_done(union ctl_io *io, int have_lock) 12609{ 12610 union ctl_ha_msg msg; 12611#ifdef CTL_TIME_IO 12612 struct bintime cur_bt; 12613#endif 12614 12615 memset(&msg, 0, sizeof(msg)); 12616 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12617 msg.hdr.original_sc = io; 12618 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12619 msg.hdr.nexus = io->io_hdr.nexus; 12620 msg.hdr.status = io->io_hdr.status; 12621 msg.scsi.tag_num = io->scsiio.tag_num; 12622 msg.scsi.tag_type = io->scsiio.tag_type; 12623 msg.scsi.scsi_status = io->scsiio.scsi_status; 12624 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12625 io->scsiio.sense_len); 12626 msg.scsi.sense_len = io->scsiio.sense_len; 12627 msg.scsi.sense_residual = io->scsiio.sense_residual; 12628 msg.scsi.fetd_status = io->io_hdr.port_status; 12629 msg.scsi.residual = io->scsiio.residual; 12630 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12631 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12632 ctl_failover_io(io, /*have_lock*/ have_lock); 12633 return; 12634 } 12635 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12636 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12637 msg.scsi.sense_len, M_WAITOK); 12638 12639#ifdef CTL_TIME_IO 12640 getbinuptime(&cur_bt); 12641 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12642 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12643#endif 12644 io->io_hdr.num_dmas++; 12645} 12646 12647/* 12648 * The DMA to the remote side is done, now we need to tell the other side 12649 * we're done so it can continue with its data movement. 12650 */ 12651static void 12652ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12653{ 12654 union ctl_io *io; 12655 uint32_t i; 12656 12657 io = rq->context; 12658 12659 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12660 printf("%s: ISC DMA write failed with error %d", __func__, 12661 rq->ret); 12662 ctl_set_internal_failure(&io->scsiio, 12663 /*sks_valid*/ 1, 12664 /*retry_count*/ rq->ret); 12665 } 12666 12667 ctl_dt_req_free(rq); 12668 12669 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12670 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12671 free(io->io_hdr.remote_sglist, M_CTL); 12672 io->io_hdr.remote_sglist = NULL; 12673 io->io_hdr.local_sglist = NULL; 12674 12675 /* 12676 * The data is in local and remote memory, so now we need to send 12677 * status (good or back) back to the other side. 12678 */ 12679 ctl_send_datamove_done(io, /*have_lock*/ 0); 12680} 12681 12682/* 12683 * We've moved the data from the host/controller into local memory. Now we 12684 * need to push it over to the remote controller's memory. 12685 */ 12686static int 12687ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12688{ 12689 int retval; 12690 12691 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12692 ctl_datamove_remote_write_cb); 12693 return (retval); 12694} 12695 12696static void 12697ctl_datamove_remote_write(union ctl_io *io) 12698{ 12699 int retval; 12700 void (*fe_datamove)(union ctl_io *io); 12701 12702 /* 12703 * - Get the data from the host/HBA into local memory. 12704 * - DMA memory from the local controller to the remote controller. 12705 * - Send status back to the remote controller. 12706 */ 12707 12708 retval = ctl_datamove_remote_sgl_setup(io); 12709 if (retval != 0) 12710 return; 12711 12712 /* Switch the pointer over so the FETD knows what to do */ 12713 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12714 12715 /* 12716 * Use a custom move done callback, since we need to send completion 12717 * back to the other controller, not to the backend on this side. 12718 */ 12719 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12720 12721 fe_datamove = CTL_PORT(io)->fe_datamove; 12722 fe_datamove(io); 12723} 12724 12725static int 12726ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12727{ 12728#if 0 12729 char str[256]; 12730 char path_str[64]; 12731 struct sbuf sb; 12732#endif 12733 uint32_t i; 12734 12735 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12736 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12737 free(io->io_hdr.remote_sglist, M_CTL); 12738 io->io_hdr.remote_sglist = NULL; 12739 io->io_hdr.local_sglist = NULL; 12740 12741#if 0 12742 scsi_path_string(io, path_str, sizeof(path_str)); 12743 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12744 sbuf_cat(&sb, path_str); 12745 scsi_command_string(&io->scsiio, NULL, &sb); 12746 sbuf_printf(&sb, "\n"); 12747 sbuf_cat(&sb, path_str); 12748 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12749 io->scsiio.tag_num, io->scsiio.tag_type); 12750 sbuf_cat(&sb, path_str); 12751 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12752 io->io_hdr.flags, io->io_hdr.status); 12753 sbuf_finish(&sb); 12754 printk("%s", sbuf_data(&sb)); 12755#endif 12756 12757 12758 /* 12759 * The read is done, now we need to send status (good or bad) back 12760 * to the other side. 12761 */ 12762 ctl_send_datamove_done(io, /*have_lock*/ 0); 12763 12764 return (0); 12765} 12766 12767static void 12768ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12769{ 12770 union ctl_io *io; 12771 void (*fe_datamove)(union ctl_io *io); 12772 12773 io = rq->context; 12774 12775 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12776 printf("%s: ISC DMA read failed with error %d\n", __func__, 12777 rq->ret); 12778 ctl_set_internal_failure(&io->scsiio, 12779 /*sks_valid*/ 1, 12780 /*retry_count*/ rq->ret); 12781 } 12782 12783 ctl_dt_req_free(rq); 12784 12785 /* Switch the pointer over so the FETD knows what to do */ 12786 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12787 12788 /* 12789 * Use a custom move done callback, since we need to send completion 12790 * back to the other controller, not to the backend on this side. 12791 */ 12792 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12793 12794 /* XXX KDM add checks like the ones in ctl_datamove? */ 12795 12796 fe_datamove = CTL_PORT(io)->fe_datamove; 12797 fe_datamove(io); 12798} 12799 12800static int 12801ctl_datamove_remote_sgl_setup(union ctl_io *io) 12802{ 12803 struct ctl_sg_entry *local_sglist; 12804 uint32_t len_to_go; 12805 int retval; 12806 int i; 12807 12808 retval = 0; 12809 local_sglist = io->io_hdr.local_sglist; 12810 len_to_go = io->scsiio.kern_data_len; 12811 12812 /* 12813 * The difficult thing here is that the size of the various 12814 * S/G segments may be different than the size from the 12815 * remote controller. That'll make it harder when DMAing 12816 * the data back to the other side. 12817 */ 12818 for (i = 0; len_to_go > 0; i++) { 12819 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12820 local_sglist[i].addr = 12821 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12822 12823 len_to_go -= local_sglist[i].len; 12824 } 12825 /* 12826 * Reset the number of S/G entries accordingly. The original 12827 * number of S/G entries is available in rem_sg_entries. 12828 */ 12829 io->scsiio.kern_sg_entries = i; 12830 12831#if 0 12832 printf("%s: kern_sg_entries = %d\n", __func__, 12833 io->scsiio.kern_sg_entries); 12834 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12835 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12836 local_sglist[i].addr, local_sglist[i].len); 12837#endif 12838 12839 return (retval); 12840} 12841 12842static int 12843ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12844 ctl_ha_dt_cb callback) 12845{ 12846 struct ctl_ha_dt_req *rq; 12847 struct ctl_sg_entry *remote_sglist, *local_sglist; 12848 uint32_t local_used, remote_used, total_used; 12849 int i, j, isc_ret; 12850 12851 rq = ctl_dt_req_alloc(); 12852 12853 /* 12854 * If we failed to allocate the request, and if the DMA didn't fail 12855 * anyway, set busy status. This is just a resource allocation 12856 * failure. 12857 */ 12858 if ((rq == NULL) 12859 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12860 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12861 ctl_set_busy(&io->scsiio); 12862 12863 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12864 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12865 12866 if (rq != NULL) 12867 ctl_dt_req_free(rq); 12868 12869 /* 12870 * The data move failed. We need to return status back 12871 * to the other controller. No point in trying to DMA 12872 * data to the remote controller. 12873 */ 12874 12875 ctl_send_datamove_done(io, /*have_lock*/ 0); 12876 12877 return (1); 12878 } 12879 12880 local_sglist = io->io_hdr.local_sglist; 12881 remote_sglist = io->io_hdr.remote_sglist; 12882 local_used = 0; 12883 remote_used = 0; 12884 total_used = 0; 12885 12886 /* 12887 * Pull/push the data over the wire from/to the other controller. 12888 * This takes into account the possibility that the local and 12889 * remote sglists may not be identical in terms of the size of 12890 * the elements and the number of elements. 12891 * 12892 * One fundamental assumption here is that the length allocated for 12893 * both the local and remote sglists is identical. Otherwise, we've 12894 * essentially got a coding error of some sort. 12895 */ 12896 isc_ret = CTL_HA_STATUS_SUCCESS; 12897 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12898 uint32_t cur_len; 12899 uint8_t *tmp_ptr; 12900 12901 rq->command = command; 12902 rq->context = io; 12903 12904 /* 12905 * Both pointers should be aligned. But it is possible 12906 * that the allocation length is not. They should both 12907 * also have enough slack left over at the end, though, 12908 * to round up to the next 8 byte boundary. 12909 */ 12910 cur_len = MIN(local_sglist[i].len - local_used, 12911 remote_sglist[j].len - remote_used); 12912 rq->size = cur_len; 12913 12914 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12915 tmp_ptr += local_used; 12916 12917#if 0 12918 /* Use physical addresses when talking to ISC hardware */ 12919 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12920 /* XXX KDM use busdma */ 12921 rq->local = vtophys(tmp_ptr); 12922 } else 12923 rq->local = tmp_ptr; 12924#else 12925 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12926 ("HA does not support BUS_ADDR")); 12927 rq->local = tmp_ptr; 12928#endif 12929 12930 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12931 tmp_ptr += remote_used; 12932 rq->remote = tmp_ptr; 12933 12934 rq->callback = NULL; 12935 12936 local_used += cur_len; 12937 if (local_used >= local_sglist[i].len) { 12938 i++; 12939 local_used = 0; 12940 } 12941 12942 remote_used += cur_len; 12943 if (remote_used >= remote_sglist[j].len) { 12944 j++; 12945 remote_used = 0; 12946 } 12947 total_used += cur_len; 12948 12949 if (total_used >= io->scsiio.kern_data_len) 12950 rq->callback = callback; 12951 12952#if 0 12953 printf("%s: %s: local %p remote %p size %d\n", __func__, 12954 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12955 rq->local, rq->remote, rq->size); 12956#endif 12957 12958 isc_ret = ctl_dt_single(rq); 12959 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12960 break; 12961 } 12962 if (isc_ret != CTL_HA_STATUS_WAIT) { 12963 rq->ret = isc_ret; 12964 callback(rq); 12965 } 12966 12967 return (0); 12968} 12969 12970static void 12971ctl_datamove_remote_read(union ctl_io *io) 12972{ 12973 int retval; 12974 uint32_t i; 12975 12976 /* 12977 * This will send an error to the other controller in the case of a 12978 * failure. 12979 */ 12980 retval = ctl_datamove_remote_sgl_setup(io); 12981 if (retval != 0) 12982 return; 12983 12984 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12985 ctl_datamove_remote_read_cb); 12986 if (retval != 0) { 12987 /* 12988 * Make sure we free memory if there was an error.. The 12989 * ctl_datamove_remote_xfer() function will send the 12990 * datamove done message, or call the callback with an 12991 * error if there is a problem. 12992 */ 12993 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12994 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12995 free(io->io_hdr.remote_sglist, M_CTL); 12996 io->io_hdr.remote_sglist = NULL; 12997 io->io_hdr.local_sglist = NULL; 12998 } 12999} 13000 13001/* 13002 * Process a datamove request from the other controller. This is used for 13003 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13004 * first. Once that is complete, the data gets DMAed into the remote 13005 * controller's memory. For reads, we DMA from the remote controller's 13006 * memory into our memory first, and then move it out to the FETD. 13007 */ 13008static void 13009ctl_datamove_remote(union ctl_io *io) 13010{ 13011 13012 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED); 13013 13014 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13015 ctl_failover_io(io, /*have_lock*/ 0); 13016 return; 13017 } 13018 13019 /* 13020 * Note that we look for an aborted I/O here, but don't do some of 13021 * the other checks that ctl_datamove() normally does. 13022 * We don't need to run the datamove delay code, since that should 13023 * have been done if need be on the other controller. 13024 */ 13025 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13026 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13027 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13028 io->io_hdr.nexus.targ_port, 13029 io->io_hdr.nexus.targ_lun); 13030 io->io_hdr.port_status = 31338; 13031 ctl_send_datamove_done(io, /*have_lock*/ 0); 13032 return; 13033 } 13034 13035 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13036 ctl_datamove_remote_write(io); 13037 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13038 ctl_datamove_remote_read(io); 13039 else { 13040 io->io_hdr.port_status = 31339; 13041 ctl_send_datamove_done(io, /*have_lock*/ 0); 13042 } 13043} 13044 13045static void 13046ctl_process_done(union ctl_io *io) 13047{ 13048 struct ctl_softc *softc = CTL_SOFTC(io); 13049 struct ctl_lun *lun = CTL_LUN(io); 13050 void (*fe_done)(union ctl_io *io); 13051 union ctl_ha_msg msg; 13052 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13053 13054 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13055 fe_done = softc->ctl_ports[targ_port]->fe_done; 13056 13057#ifdef CTL_TIME_IO 13058 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13059 char str[256]; 13060 char path_str[64]; 13061 struct sbuf sb; 13062 13063 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13064 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13065 13066 sbuf_cat(&sb, path_str); 13067 switch (io->io_hdr.io_type) { 13068 case CTL_IO_SCSI: 13069 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13070 sbuf_printf(&sb, "\n"); 13071 sbuf_cat(&sb, path_str); 13072 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13073 io->scsiio.tag_num, io->scsiio.tag_type); 13074 break; 13075 case CTL_IO_TASK: 13076 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13077 "Tag Type: %d\n", io->taskio.task_action, 13078 io->taskio.tag_num, io->taskio.tag_type); 13079 break; 13080 default: 13081 panic("%s: Invalid CTL I/O type %d\n", 13082 __func__, io->io_hdr.io_type); 13083 } 13084 sbuf_cat(&sb, path_str); 13085 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13086 (intmax_t)time_uptime - io->io_hdr.start_time); 13087 sbuf_finish(&sb); 13088 printf("%s", sbuf_data(&sb)); 13089 } 13090#endif /* CTL_TIME_IO */ 13091 13092 switch (io->io_hdr.io_type) { 13093 case CTL_IO_SCSI: 13094 break; 13095 case CTL_IO_TASK: 13096 if (ctl_debug & CTL_DEBUG_INFO) 13097 ctl_io_error_print(io, NULL); 13098 fe_done(io); 13099 return; 13100 default: 13101 panic("%s: Invalid CTL I/O type %d\n", 13102 __func__, io->io_hdr.io_type); 13103 } 13104 13105 if (lun == NULL) { 13106 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13107 io->io_hdr.nexus.targ_mapped_lun)); 13108 goto bailout; 13109 } 13110 13111 mtx_lock(&lun->lun_lock); 13112 13113 /* 13114 * Check to see if we have any informational exception and status 13115 * of this command can be modified to report it in form of either 13116 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13117 */ 13118 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13119 io->io_hdr.status == CTL_SUCCESS && 13120 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13121 uint8_t mrie = lun->MODE_IE.mrie; 13122 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13123 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13124 if (((mrie == SIEP_MRIE_REC_COND && per) || 13125 mrie == SIEP_MRIE_REC_UNCOND || 13126 mrie == SIEP_MRIE_NO_SENSE) && 13127 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13128 CTL_CMD_FLAG_NO_SENSE) == 0) { 13129 ctl_set_sense(&io->scsiio, 13130 /*current_error*/ 1, 13131 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13132 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13133 /*asc*/ lun->ie_asc, 13134 /*ascq*/ lun->ie_ascq, 13135 SSD_ELEM_NONE); 13136 lun->ie_reported = 1; 13137 } 13138 } else if (lun->ie_reported < 0) 13139 lun->ie_reported = 0; 13140 13141 /* 13142 * Check to see if we have any errors to inject here. We only 13143 * inject errors for commands that don't already have errors set. 13144 */ 13145 if (!STAILQ_EMPTY(&lun->error_list) && 13146 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13147 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13148 ctl_inject_error(lun, io); 13149 13150 /* 13151 * XXX KDM how do we treat commands that aren't completed 13152 * successfully? 13153 * 13154 * XXX KDM should we also track I/O latency? 13155 */ 13156 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13157 io->io_hdr.io_type == CTL_IO_SCSI) { 13158#ifdef CTL_TIME_IO 13159 struct bintime cur_bt; 13160#endif 13161 int type; 13162 13163 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13164 CTL_FLAG_DATA_IN) 13165 type = CTL_STATS_READ; 13166 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13167 CTL_FLAG_DATA_OUT) 13168 type = CTL_STATS_WRITE; 13169 else 13170 type = CTL_STATS_NO_IO; 13171 13172 lun->stats.ports[targ_port].bytes[type] += 13173 io->scsiio.kern_total_len; 13174 lun->stats.ports[targ_port].operations[type]++; 13175#ifdef CTL_TIME_IO 13176 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13177 &io->io_hdr.dma_bt); 13178 getbinuptime(&cur_bt); 13179 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13180 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13181#endif 13182 lun->stats.ports[targ_port].num_dmas[type] += 13183 io->io_hdr.num_dmas; 13184 } 13185 13186 /* 13187 * Remove this from the OOA queue. 13188 */ 13189 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13190#ifdef CTL_TIME_IO 13191 if (TAILQ_EMPTY(&lun->ooa_queue)) 13192 lun->last_busy = getsbinuptime(); 13193#endif 13194 13195 /* 13196 * Run through the blocked queue on this LUN and see if anything 13197 * has become unblocked, now that this transaction is done. 13198 */ 13199 ctl_check_blocked(lun); 13200 13201 /* 13202 * If the LUN has been invalidated, free it if there is nothing 13203 * left on its OOA queue. 13204 */ 13205 if ((lun->flags & CTL_LUN_INVALID) 13206 && TAILQ_EMPTY(&lun->ooa_queue)) { 13207 mtx_unlock(&lun->lun_lock); 13208 mtx_lock(&softc->ctl_lock); 13209 ctl_free_lun(lun); 13210 mtx_unlock(&softc->ctl_lock); 13211 } else 13212 mtx_unlock(&lun->lun_lock); 13213 13214bailout: 13215 13216 /* 13217 * If this command has been aborted, make sure we set the status 13218 * properly. The FETD is responsible for freeing the I/O and doing 13219 * whatever it needs to do to clean up its state. 13220 */ 13221 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13222 ctl_set_task_aborted(&io->scsiio); 13223 13224 /* 13225 * If enabled, print command error status. 13226 */ 13227 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13228 (ctl_debug & CTL_DEBUG_INFO) != 0) 13229 ctl_io_error_print(io, NULL); 13230 13231 /* 13232 * Tell the FETD or the other shelf controller we're done with this 13233 * command. Note that only SCSI commands get to this point. Task 13234 * management commands are completed above. 13235 */ 13236 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13237 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13238 memset(&msg, 0, sizeof(msg)); 13239 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13240 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13241 msg.hdr.nexus = io->io_hdr.nexus; 13242 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13243 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13244 M_WAITOK); 13245 } 13246 13247 fe_done(io); 13248} 13249 13250#ifdef CTL_WITH_CA 13251/* 13252 * Front end should call this if it doesn't do autosense. When the request 13253 * sense comes back in from the initiator, we'll dequeue this and send it. 13254 */ 13255int 13256ctl_queue_sense(union ctl_io *io) 13257{ 13258 struct ctl_softc *softc = CTL_SOFTC(io); 13259 struct ctl_port *port = CTL_PORT(io); 13260 struct ctl_lun *lun; 13261 uint32_t initidx, targ_lun; 13262 13263 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13264 13265 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13266 13267 /* 13268 * LUN lookup will likely move to the ctl_work_thread() once we 13269 * have our new queueing infrastructure (that doesn't put things on 13270 * a per-LUN queue initially). That is so that we can handle 13271 * things like an INQUIRY to a LUN that we don't have enabled. We 13272 * can't deal with that right now. 13273 * If we don't have a LUN for this, just toss the sense information. 13274 */ 13275 mtx_lock(&softc->ctl_lock); 13276 if (targ_lun >= CTL_MAX_LUNS || 13277 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13278 mtx_unlock(&softc->ctl_lock); 13279 goto bailout; 13280 } 13281 mtx_lock(&lun->lun_lock); 13282 mtx_unlock(&softc->ctl_lock); 13283 13284 /* 13285 * Already have CA set for this LUN...toss the sense information. 13286 */ 13287 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13288 if (ctl_is_set(lun->have_ca, initidx)) { 13289 mtx_unlock(&lun->lun_lock); 13290 goto bailout; 13291 } 13292 13293 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13294 MIN(sizeof(lun->pending_sense[initidx]), 13295 sizeof(io->scsiio.sense_data))); 13296 ctl_set_mask(lun->have_ca, initidx); 13297 mtx_unlock(&lun->lun_lock); 13298 13299bailout: 13300 ctl_free_io(io); 13301 return (CTL_RETVAL_COMPLETE); 13302} 13303#endif 13304 13305/* 13306 * Primary command inlet from frontend ports. All SCSI and task I/O 13307 * requests must go through this function. 13308 */ 13309int 13310ctl_queue(union ctl_io *io) 13311{ 13312 struct ctl_port *port = CTL_PORT(io); 13313 13314 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13315 13316#ifdef CTL_TIME_IO 13317 io->io_hdr.start_time = time_uptime; 13318 getbinuptime(&io->io_hdr.start_bt); 13319#endif /* CTL_TIME_IO */ 13320 13321 /* Map FE-specific LUN ID into global one. */ 13322 io->io_hdr.nexus.targ_mapped_lun = 13323 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13324 13325 switch (io->io_hdr.io_type) { 13326 case CTL_IO_SCSI: 13327 case CTL_IO_TASK: 13328 if (ctl_debug & CTL_DEBUG_CDB) 13329 ctl_io_print(io); 13330 ctl_enqueue_incoming(io); 13331 break; 13332 default: 13333 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13334 return (EINVAL); 13335 } 13336 13337 return (CTL_RETVAL_COMPLETE); 13338} 13339 13340#ifdef CTL_IO_DELAY 13341static void 13342ctl_done_timer_wakeup(void *arg) 13343{ 13344 union ctl_io *io; 13345 13346 io = (union ctl_io *)arg; 13347 ctl_done(io); 13348} 13349#endif /* CTL_IO_DELAY */ 13350 13351void 13352ctl_serseq_done(union ctl_io *io) 13353{ 13354 struct ctl_lun *lun = CTL_LUN(io);; 13355 13356 if (lun->be_lun == NULL || 13357 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13358 return; 13359 mtx_lock(&lun->lun_lock); 13360 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13361 ctl_check_blocked(lun); 13362 mtx_unlock(&lun->lun_lock); 13363} 13364 13365void 13366ctl_done(union ctl_io *io) 13367{ 13368 13369 /* 13370 * Enable this to catch duplicate completion issues. 13371 */ 13372#if 0 13373 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13374 printf("%s: type %d msg %d cdb %x iptl: " 13375 "%u:%u:%u tag 0x%04x " 13376 "flag %#x status %x\n", 13377 __func__, 13378 io->io_hdr.io_type, 13379 io->io_hdr.msg_type, 13380 io->scsiio.cdb[0], 13381 io->io_hdr.nexus.initid, 13382 io->io_hdr.nexus.targ_port, 13383 io->io_hdr.nexus.targ_lun, 13384 (io->io_hdr.io_type == 13385 CTL_IO_TASK) ? 13386 io->taskio.tag_num : 13387 io->scsiio.tag_num, 13388 io->io_hdr.flags, 13389 io->io_hdr.status); 13390 } else 13391 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13392#endif 13393 13394 /* 13395 * This is an internal copy of an I/O, and should not go through 13396 * the normal done processing logic. 13397 */ 13398 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13399 return; 13400 13401#ifdef CTL_IO_DELAY 13402 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13403 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13404 } else { 13405 struct ctl_lun *lun = CTL_LUN(io); 13406 13407 if ((lun != NULL) 13408 && (lun->delay_info.done_delay > 0)) { 13409 13410 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13411 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13412 callout_reset(&io->io_hdr.delay_callout, 13413 lun->delay_info.done_delay * hz, 13414 ctl_done_timer_wakeup, io); 13415 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13416 lun->delay_info.done_delay = 0; 13417 return; 13418 } 13419 } 13420#endif /* CTL_IO_DELAY */ 13421 13422 ctl_enqueue_done(io); 13423} 13424 13425static void 13426ctl_work_thread(void *arg) 13427{ 13428 struct ctl_thread *thr = (struct ctl_thread *)arg; 13429 struct ctl_softc *softc = thr->ctl_softc; 13430 union ctl_io *io; 13431 int retval; 13432 13433 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13434 13435 for (;;) { 13436 /* 13437 * We handle the queues in this order: 13438 * - ISC 13439 * - done queue (to free up resources, unblock other commands) 13440 * - RtR queue 13441 * - incoming queue 13442 * 13443 * If those queues are empty, we break out of the loop and 13444 * go to sleep. 13445 */ 13446 mtx_lock(&thr->queue_lock); 13447 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13448 if (io != NULL) { 13449 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13450 mtx_unlock(&thr->queue_lock); 13451 ctl_handle_isc(io); 13452 continue; 13453 } 13454 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13455 if (io != NULL) { 13456 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13457 /* clear any blocked commands, call fe_done */ 13458 mtx_unlock(&thr->queue_lock); 13459 ctl_process_done(io); 13460 continue; 13461 } 13462 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13463 if (io != NULL) { 13464 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13465 mtx_unlock(&thr->queue_lock); 13466 if (io->io_hdr.io_type == CTL_IO_TASK) 13467 ctl_run_task(io); 13468 else 13469 ctl_scsiio_precheck(softc, &io->scsiio); 13470 continue; 13471 } 13472 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13473 if (io != NULL) { 13474 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13475 mtx_unlock(&thr->queue_lock); 13476 retval = ctl_scsiio(&io->scsiio); 13477 if (retval != CTL_RETVAL_COMPLETE) 13478 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13479 continue; 13480 } 13481 13482 /* Sleep until we have something to do. */ 13483 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13484 } 13485} 13486 13487static void 13488ctl_lun_thread(void *arg) 13489{ 13490 struct ctl_softc *softc = (struct ctl_softc *)arg; 13491 struct ctl_be_lun *be_lun; 13492 13493 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13494 13495 for (;;) { 13496 mtx_lock(&softc->ctl_lock); 13497 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13498 if (be_lun != NULL) { 13499 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13500 mtx_unlock(&softc->ctl_lock); 13501 ctl_create_lun(be_lun); 13502 continue; 13503 } 13504 13505 /* Sleep until we have something to do. */ 13506 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13507 PDROP | PRIBIO, "-", 0); 13508 } 13509} 13510 13511static void 13512ctl_thresh_thread(void *arg) 13513{ 13514 struct ctl_softc *softc = (struct ctl_softc *)arg; 13515 struct ctl_lun *lun; 13516 struct ctl_logical_block_provisioning_page *page; 13517 const char *attr; 13518 union ctl_ha_msg msg; 13519 uint64_t thres, val; 13520 int i, e, set; 13521 13522 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13523 13524 for (;;) { 13525 mtx_lock(&softc->ctl_lock); 13526 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13527 if ((lun->flags & CTL_LUN_DISABLED) || 13528 (lun->flags & CTL_LUN_NO_MEDIA) || 13529 lun->backend->lun_attr == NULL) 13530 continue; 13531 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13532 softc->ha_mode == CTL_HA_MODE_XFER) 13533 continue; 13534 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13535 continue; 13536 e = 0; 13537 page = &lun->MODE_LBP; 13538 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13539 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13540 continue; 13541 thres = scsi_4btoul(page->descr[i].count); 13542 thres <<= CTL_LBP_EXPONENT; 13543 switch (page->descr[i].resource) { 13544 case 0x01: 13545 attr = "blocksavail"; 13546 break; 13547 case 0x02: 13548 attr = "blocksused"; 13549 break; 13550 case 0xf1: 13551 attr = "poolblocksavail"; 13552 break; 13553 case 0xf2: 13554 attr = "poolblocksused"; 13555 break; 13556 default: 13557 continue; 13558 } 13559 mtx_unlock(&softc->ctl_lock); // XXX 13560 val = lun->backend->lun_attr( 13561 lun->be_lun->be_lun, attr); 13562 mtx_lock(&softc->ctl_lock); 13563 if (val == UINT64_MAX) 13564 continue; 13565 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13566 == SLBPPD_ARMING_INC) 13567 e = (val >= thres); 13568 else 13569 e = (val <= thres); 13570 if (e) 13571 break; 13572 } 13573 mtx_lock(&lun->lun_lock); 13574 if (e) { 13575 scsi_u64to8b((uint8_t *)&page->descr[i] - 13576 (uint8_t *)page, lun->ua_tpt_info); 13577 if (lun->lasttpt == 0 || 13578 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13579 lun->lasttpt = time_uptime; 13580 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13581 set = 1; 13582 } else 13583 set = 0; 13584 } else { 13585 lun->lasttpt = 0; 13586 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13587 set = -1; 13588 } 13589 mtx_unlock(&lun->lun_lock); 13590 if (set != 0 && 13591 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13592 /* Send msg to other side. */ 13593 bzero(&msg.ua, sizeof(msg.ua)); 13594 msg.hdr.msg_type = CTL_MSG_UA; 13595 msg.hdr.nexus.initid = -1; 13596 msg.hdr.nexus.targ_port = -1; 13597 msg.hdr.nexus.targ_lun = lun->lun; 13598 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13599 msg.ua.ua_all = 1; 13600 msg.ua.ua_set = (set > 0); 13601 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13602 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13603 mtx_unlock(&softc->ctl_lock); // XXX 13604 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13605 sizeof(msg.ua), M_WAITOK); 13606 mtx_lock(&softc->ctl_lock); 13607 } 13608 } 13609 mtx_unlock(&softc->ctl_lock); 13610 pause("-", CTL_LBP_PERIOD * hz); 13611 } 13612} 13613 13614static void 13615ctl_enqueue_incoming(union ctl_io *io) 13616{ 13617 struct ctl_softc *softc = CTL_SOFTC(io); 13618 struct ctl_thread *thr; 13619 u_int idx; 13620 13621 idx = (io->io_hdr.nexus.targ_port * 127 + 13622 io->io_hdr.nexus.initid) % worker_threads; 13623 thr = &softc->threads[idx]; 13624 mtx_lock(&thr->queue_lock); 13625 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13626 mtx_unlock(&thr->queue_lock); 13627 wakeup(thr); 13628} 13629 13630static void 13631ctl_enqueue_rtr(union ctl_io *io) 13632{ 13633 struct ctl_softc *softc = CTL_SOFTC(io); 13634 struct ctl_thread *thr; 13635 13636 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13637 mtx_lock(&thr->queue_lock); 13638 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13639 mtx_unlock(&thr->queue_lock); 13640 wakeup(thr); 13641} 13642 13643static void 13644ctl_enqueue_done(union ctl_io *io) 13645{ 13646 struct ctl_softc *softc = CTL_SOFTC(io); 13647 struct ctl_thread *thr; 13648 13649 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13650 mtx_lock(&thr->queue_lock); 13651 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13652 mtx_unlock(&thr->queue_lock); 13653 wakeup(thr); 13654} 13655 13656static void 13657ctl_enqueue_isc(union ctl_io *io) 13658{ 13659 struct ctl_softc *softc = CTL_SOFTC(io); 13660 struct ctl_thread *thr; 13661 13662 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13663 mtx_lock(&thr->queue_lock); 13664 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13665 mtx_unlock(&thr->queue_lock); 13666 wakeup(thr); 13667} 13668 13669/* 13670 * vim: ts=8 13671 */ 13672