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