ctl.c revision 311434
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37/* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43#define _CTL_C 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: stable/11/sys/cam/ctl/ctl.c 311434 2017-01-05 11:50:50Z 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*/0 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"); 410 411/* 412 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 413 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 414 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 415 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 416 */ 417#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 418 419static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 420 int param); 421static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 422static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 423static int ctl_init(void); 424void ctl_shutdown(void); 425static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 426static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 427static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 428static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 429 struct ctl_ooa *ooa_hdr, 430 struct ctl_ooa_entry *kern_entries); 431static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 432 struct thread *td); 433static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 434 struct ctl_be_lun *be_lun); 435static int ctl_free_lun(struct ctl_lun *lun); 436static void ctl_create_lun(struct ctl_be_lun *be_lun); 437static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 438 439static int ctl_do_mode_select(union ctl_io *io); 440static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 441 uint64_t res_key, uint64_t sa_res_key, 442 uint8_t type, uint32_t residx, 443 struct ctl_scsiio *ctsio, 444 struct scsi_per_res_out *cdb, 445 struct scsi_per_res_out_parms* param); 446static void ctl_pro_preempt_other(struct ctl_lun *lun, 447 union ctl_ha_msg *msg); 448static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 449static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 450static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 451static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 452static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 453static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 454static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 455 int alloc_len); 456static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 457 int alloc_len); 458static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 459static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 460static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 461static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 462static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 463static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 464 bool seq); 465static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 466static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 467 union ctl_io *pending_io, union ctl_io *ooa_io); 468static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 469 union ctl_io *starting_io); 470static int ctl_check_blocked(struct ctl_lun *lun); 471static int ctl_scsiio_lun_check(struct ctl_lun *lun, 472 const struct ctl_cmd_entry *entry, 473 struct ctl_scsiio *ctsio); 474static void ctl_failover_lun(union ctl_io *io); 475static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 476 struct ctl_scsiio *ctsio); 477static int ctl_scsiio(struct ctl_scsiio *ctsio); 478 479static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 480static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 481 ctl_ua_type ua_type); 482static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 483 ctl_ua_type ua_type); 484static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 485static int ctl_abort_task(union ctl_io *io); 486static int ctl_abort_task_set(union ctl_io *io); 487static int ctl_query_task(union ctl_io *io, int task_set); 488static int ctl_i_t_nexus_reset(union ctl_io *io); 489static int ctl_query_async_event(union ctl_io *io); 490static void ctl_run_task(union ctl_io *io); 491#ifdef CTL_IO_DELAY 492static void ctl_datamove_timer_wakeup(void *arg); 493static void ctl_done_timer_wakeup(void *arg); 494#endif /* CTL_IO_DELAY */ 495 496static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 497static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 498static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 499static void ctl_datamove_remote_write(union ctl_io *io); 500static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 501static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 502static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 503static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 504 ctl_ha_dt_cb callback); 505static void ctl_datamove_remote_read(union ctl_io *io); 506static void ctl_datamove_remote(union ctl_io *io); 507static void ctl_process_done(union ctl_io *io); 508static void ctl_lun_thread(void *arg); 509static void ctl_thresh_thread(void *arg); 510static void ctl_work_thread(void *arg); 511static void ctl_enqueue_incoming(union ctl_io *io); 512static void ctl_enqueue_rtr(union ctl_io *io); 513static void ctl_enqueue_done(union ctl_io *io); 514static void ctl_enqueue_isc(union ctl_io *io); 515static const struct ctl_cmd_entry * 516 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 517static const struct ctl_cmd_entry * 518 ctl_validate_command(struct ctl_scsiio *ctsio); 519static int ctl_cmd_applicable(uint8_t lun_type, 520 const struct ctl_cmd_entry *entry); 521 522static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 523static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 524static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 525static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 526 527/* 528 * Load the serialization table. This isn't very pretty, but is probably 529 * the easiest way to do it. 530 */ 531#include "ctl_ser_table.c" 532 533/* 534 * We only need to define open, close and ioctl routines for this driver. 535 */ 536static struct cdevsw ctl_cdevsw = { 537 .d_version = D_VERSION, 538 .d_flags = 0, 539 .d_open = ctl_open, 540 .d_close = ctl_close, 541 .d_ioctl = ctl_ioctl, 542 .d_name = "ctl", 543}; 544 545 546MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 547 548static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 549 550static moduledata_t ctl_moduledata = { 551 "ctl", 552 ctl_module_event_handler, 553 NULL 554}; 555 556DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 557MODULE_VERSION(ctl, 1); 558 559static struct ctl_frontend ha_frontend = 560{ 561 .name = "ha", 562}; 563 564static void 565ctl_ha_datamove(union ctl_io *io) 566{ 567 struct ctl_lun *lun; 568 struct ctl_sg_entry *sgl; 569 union ctl_ha_msg msg; 570 uint32_t sg_entries_sent; 571 int do_sg_copy, i, j; 572 573 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 574 memset(&msg.dt, 0, sizeof(msg.dt)); 575 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 576 msg.hdr.original_sc = io->io_hdr.original_sc; 577 msg.hdr.serializing_sc = io; 578 msg.hdr.nexus = io->io_hdr.nexus; 579 msg.hdr.status = io->io_hdr.status; 580 msg.dt.flags = io->io_hdr.flags; 581 582 /* 583 * We convert everything into a S/G list here. We can't 584 * pass by reference, only by value between controllers. 585 * So we can't pass a pointer to the S/G list, only as many 586 * S/G entries as we can fit in here. If it's possible for 587 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 588 * then we need to break this up into multiple transfers. 589 */ 590 if (io->scsiio.kern_sg_entries == 0) { 591 msg.dt.kern_sg_entries = 1; 592#if 0 593 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 594 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 595 } else { 596 /* XXX KDM use busdma here! */ 597 msg.dt.sg_list[0].addr = 598 (void *)vtophys(io->scsiio.kern_data_ptr); 599 } 600#else 601 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 602 ("HA does not support BUS_ADDR")); 603 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 604#endif 605 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 606 do_sg_copy = 0; 607 } else { 608 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 609 do_sg_copy = 1; 610 } 611 612 msg.dt.kern_data_len = io->scsiio.kern_data_len; 613 msg.dt.kern_total_len = io->scsiio.kern_total_len; 614 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 615 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 616 msg.dt.sg_sequence = 0; 617 618 /* 619 * Loop until we've sent all of the S/G entries. On the 620 * other end, we'll recompose these S/G entries into one 621 * contiguous list before processing. 622 */ 623 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 624 msg.dt.sg_sequence++) { 625 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 626 sizeof(msg.dt.sg_list[0])), 627 msg.dt.kern_sg_entries - sg_entries_sent); 628 if (do_sg_copy != 0) { 629 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 630 for (i = sg_entries_sent, j = 0; 631 i < msg.dt.cur_sg_entries; i++, j++) { 632#if 0 633 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 634 msg.dt.sg_list[j].addr = sgl[i].addr; 635 } else { 636 /* XXX KDM use busdma here! */ 637 msg.dt.sg_list[j].addr = 638 (void *)vtophys(sgl[i].addr); 639 } 640#else 641 KASSERT((io->io_hdr.flags & 642 CTL_FLAG_BUS_ADDR) == 0, 643 ("HA does not support BUS_ADDR")); 644 msg.dt.sg_list[j].addr = sgl[i].addr; 645#endif 646 msg.dt.sg_list[j].len = sgl[i].len; 647 } 648 } 649 650 sg_entries_sent += msg.dt.cur_sg_entries; 651 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 652 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 653 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 654 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 655 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 656 io->io_hdr.port_status = 31341; 657 io->scsiio.be_move_done(io); 658 return; 659 } 660 msg.dt.sent_sg_entries = sg_entries_sent; 661 } 662 663 /* 664 * Officially handover the request from us to peer. 665 * If failover has just happened, then we must return error. 666 * If failover happen just after, then it is not our problem. 667 */ 668 if (lun) 669 mtx_lock(&lun->lun_lock); 670 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 671 if (lun) 672 mtx_unlock(&lun->lun_lock); 673 io->io_hdr.port_status = 31342; 674 io->scsiio.be_move_done(io); 675 return; 676 } 677 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 678 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 679 if (lun) 680 mtx_unlock(&lun->lun_lock); 681} 682 683static void 684ctl_ha_done(union ctl_io *io) 685{ 686 union ctl_ha_msg msg; 687 688 if (io->io_hdr.io_type == CTL_IO_SCSI) { 689 memset(&msg, 0, sizeof(msg)); 690 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 691 msg.hdr.original_sc = io->io_hdr.original_sc; 692 msg.hdr.nexus = io->io_hdr.nexus; 693 msg.hdr.status = io->io_hdr.status; 694 msg.scsi.scsi_status = io->scsiio.scsi_status; 695 msg.scsi.tag_num = io->scsiio.tag_num; 696 msg.scsi.tag_type = io->scsiio.tag_type; 697 msg.scsi.sense_len = io->scsiio.sense_len; 698 msg.scsi.sense_residual = io->scsiio.sense_residual; 699 msg.scsi.residual = io->scsiio.residual; 700 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 701 io->scsiio.sense_len); 702 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 703 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 704 msg.scsi.sense_len, M_WAITOK); 705 } 706 ctl_free_io(io); 707} 708 709static void 710ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 711 union ctl_ha_msg *msg_info) 712{ 713 struct ctl_scsiio *ctsio; 714 715 if (msg_info->hdr.original_sc == NULL) { 716 printf("%s: original_sc == NULL!\n", __func__); 717 /* XXX KDM now what? */ 718 return; 719 } 720 721 ctsio = &msg_info->hdr.original_sc->scsiio; 722 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 723 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 724 ctsio->io_hdr.status = msg_info->hdr.status; 725 ctsio->scsi_status = msg_info->scsi.scsi_status; 726 ctsio->sense_len = msg_info->scsi.sense_len; 727 ctsio->sense_residual = msg_info->scsi.sense_residual; 728 ctsio->residual = msg_info->scsi.residual; 729 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 730 msg_info->scsi.sense_len); 731 ctl_enqueue_isc((union ctl_io *)ctsio); 732} 733 734static void 735ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 736 union ctl_ha_msg *msg_info) 737{ 738 struct ctl_scsiio *ctsio; 739 740 if (msg_info->hdr.serializing_sc == NULL) { 741 printf("%s: serializing_sc == NULL!\n", __func__); 742 /* XXX KDM now what? */ 743 return; 744 } 745 746 ctsio = &msg_info->hdr.serializing_sc->scsiio; 747 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 748 ctl_enqueue_isc((union ctl_io *)ctsio); 749} 750 751void 752ctl_isc_announce_lun(struct ctl_lun *lun) 753{ 754 struct ctl_softc *softc = lun->ctl_softc; 755 union ctl_ha_msg *msg; 756 struct ctl_ha_msg_lun_pr_key pr_key; 757 int i, k; 758 759 if (softc->ha_link != CTL_HA_LINK_ONLINE) 760 return; 761 mtx_lock(&lun->lun_lock); 762 i = sizeof(msg->lun); 763 if (lun->lun_devid) 764 i += lun->lun_devid->len; 765 i += sizeof(pr_key) * lun->pr_key_count; 766alloc: 767 mtx_unlock(&lun->lun_lock); 768 msg = malloc(i, M_CTL, M_WAITOK); 769 mtx_lock(&lun->lun_lock); 770 k = sizeof(msg->lun); 771 if (lun->lun_devid) 772 k += lun->lun_devid->len; 773 k += sizeof(pr_key) * lun->pr_key_count; 774 if (i < k) { 775 free(msg, M_CTL); 776 i = k; 777 goto alloc; 778 } 779 bzero(&msg->lun, sizeof(msg->lun)); 780 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 781 msg->hdr.nexus.targ_lun = lun->lun; 782 msg->hdr.nexus.targ_mapped_lun = lun->lun; 783 msg->lun.flags = lun->flags; 784 msg->lun.pr_generation = lun->pr_generation; 785 msg->lun.pr_res_idx = lun->pr_res_idx; 786 msg->lun.pr_res_type = lun->pr_res_type; 787 msg->lun.pr_key_count = lun->pr_key_count; 788 i = 0; 789 if (lun->lun_devid) { 790 msg->lun.lun_devid_len = lun->lun_devid->len; 791 memcpy(&msg->lun.data[i], lun->lun_devid->data, 792 msg->lun.lun_devid_len); 793 i += msg->lun.lun_devid_len; 794 } 795 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 796 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 797 continue; 798 pr_key.pr_iid = k; 799 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 800 i += sizeof(pr_key); 801 } 802 mtx_unlock(&lun->lun_lock); 803 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 804 M_WAITOK); 805 free(msg, M_CTL); 806 807 if (lun->flags & CTL_LUN_PRIMARY_SC) { 808 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 809 ctl_isc_announce_mode(lun, -1, 810 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 811 lun->mode_pages.index[i].subpage); 812 } 813 } 814} 815 816void 817ctl_isc_announce_port(struct ctl_port *port) 818{ 819 struct ctl_softc *softc = port->ctl_softc; 820 union ctl_ha_msg *msg; 821 int i; 822 823 if (port->targ_port < softc->port_min || 824 port->targ_port >= softc->port_max || 825 softc->ha_link != CTL_HA_LINK_ONLINE) 826 return; 827 i = sizeof(msg->port) + strlen(port->port_name) + 1; 828 if (port->lun_map) 829 i += sizeof(uint32_t) * CTL_MAX_LUNS; 830 if (port->port_devid) 831 i += port->port_devid->len; 832 if (port->target_devid) 833 i += port->target_devid->len; 834 if (port->init_devid) 835 i += port->init_devid->len; 836 msg = malloc(i, M_CTL, M_WAITOK); 837 bzero(&msg->port, sizeof(msg->port)); 838 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 839 msg->hdr.nexus.targ_port = port->targ_port; 840 msg->port.port_type = port->port_type; 841 msg->port.physical_port = port->physical_port; 842 msg->port.virtual_port = port->virtual_port; 843 msg->port.status = port->status; 844 i = 0; 845 msg->port.name_len = sprintf(&msg->port.data[i], 846 "%d:%s", softc->ha_id, port->port_name) + 1; 847 i += msg->port.name_len; 848 if (port->lun_map) { 849 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 850 memcpy(&msg->port.data[i], port->lun_map, 851 msg->port.lun_map_len); 852 i += msg->port.lun_map_len; 853 } 854 if (port->port_devid) { 855 msg->port.port_devid_len = port->port_devid->len; 856 memcpy(&msg->port.data[i], port->port_devid->data, 857 msg->port.port_devid_len); 858 i += msg->port.port_devid_len; 859 } 860 if (port->target_devid) { 861 msg->port.target_devid_len = port->target_devid->len; 862 memcpy(&msg->port.data[i], port->target_devid->data, 863 msg->port.target_devid_len); 864 i += msg->port.target_devid_len; 865 } 866 if (port->init_devid) { 867 msg->port.init_devid_len = port->init_devid->len; 868 memcpy(&msg->port.data[i], port->init_devid->data, 869 msg->port.init_devid_len); 870 i += msg->port.init_devid_len; 871 } 872 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 873 M_WAITOK); 874 free(msg, M_CTL); 875} 876 877void 878ctl_isc_announce_iid(struct ctl_port *port, int iid) 879{ 880 struct ctl_softc *softc = port->ctl_softc; 881 union ctl_ha_msg *msg; 882 int i, l; 883 884 if (port->targ_port < softc->port_min || 885 port->targ_port >= softc->port_max || 886 softc->ha_link != CTL_HA_LINK_ONLINE) 887 return; 888 mtx_lock(&softc->ctl_lock); 889 i = sizeof(msg->iid); 890 l = 0; 891 if (port->wwpn_iid[iid].name) 892 l = strlen(port->wwpn_iid[iid].name) + 1; 893 i += l; 894 msg = malloc(i, M_CTL, M_NOWAIT); 895 if (msg == NULL) { 896 mtx_unlock(&softc->ctl_lock); 897 return; 898 } 899 bzero(&msg->iid, sizeof(msg->iid)); 900 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 901 msg->hdr.nexus.targ_port = port->targ_port; 902 msg->hdr.nexus.initid = iid; 903 msg->iid.in_use = port->wwpn_iid[iid].in_use; 904 msg->iid.name_len = l; 905 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 906 if (port->wwpn_iid[iid].name) 907 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 908 mtx_unlock(&softc->ctl_lock); 909 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 910 free(msg, M_CTL); 911} 912 913void 914ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 915 uint8_t page, uint8_t subpage) 916{ 917 struct ctl_softc *softc = lun->ctl_softc; 918 union ctl_ha_msg msg; 919 u_int i; 920 921 if (softc->ha_link != CTL_HA_LINK_ONLINE) 922 return; 923 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 924 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 925 page && lun->mode_pages.index[i].subpage == subpage) 926 break; 927 } 928 if (i == CTL_NUM_MODE_PAGES) 929 return; 930 931 /* Don't try to replicate pages not present on this device. */ 932 if (lun->mode_pages.index[i].page_data == NULL) 933 return; 934 935 bzero(&msg.mode, sizeof(msg.mode)); 936 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 937 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 938 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 939 msg.hdr.nexus.targ_lun = lun->lun; 940 msg.hdr.nexus.targ_mapped_lun = lun->lun; 941 msg.mode.page_code = page; 942 msg.mode.subpage = subpage; 943 msg.mode.page_len = lun->mode_pages.index[i].page_len; 944 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 945 msg.mode.page_len); 946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 947 M_WAITOK); 948} 949 950static void 951ctl_isc_ha_link_up(struct ctl_softc *softc) 952{ 953 struct ctl_port *port; 954 struct ctl_lun *lun; 955 union ctl_ha_msg msg; 956 int i; 957 958 /* Announce this node parameters to peer for validation. */ 959 msg.login.msg_type = CTL_MSG_LOGIN; 960 msg.login.version = CTL_HA_VERSION; 961 msg.login.ha_mode = softc->ha_mode; 962 msg.login.ha_id = softc->ha_id; 963 msg.login.max_luns = CTL_MAX_LUNS; 964 msg.login.max_ports = CTL_MAX_PORTS; 965 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 966 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 967 M_WAITOK); 968 969 STAILQ_FOREACH(port, &softc->port_list, links) { 970 ctl_isc_announce_port(port); 971 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 972 if (port->wwpn_iid[i].in_use) 973 ctl_isc_announce_iid(port, i); 974 } 975 } 976 STAILQ_FOREACH(lun, &softc->lun_list, links) 977 ctl_isc_announce_lun(lun); 978} 979 980static void 981ctl_isc_ha_link_down(struct ctl_softc *softc) 982{ 983 struct ctl_port *port; 984 struct ctl_lun *lun; 985 union ctl_io *io; 986 int i; 987 988 mtx_lock(&softc->ctl_lock); 989 STAILQ_FOREACH(lun, &softc->lun_list, links) { 990 mtx_lock(&lun->lun_lock); 991 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 992 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 993 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 994 } 995 mtx_unlock(&lun->lun_lock); 996 997 mtx_unlock(&softc->ctl_lock); 998 io = ctl_alloc_io(softc->othersc_pool); 999 mtx_lock(&softc->ctl_lock); 1000 ctl_zero_io(io); 1001 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1002 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1003 ctl_enqueue_isc(io); 1004 } 1005 1006 STAILQ_FOREACH(port, &softc->port_list, links) { 1007 if (port->targ_port >= softc->port_min && 1008 port->targ_port < softc->port_max) 1009 continue; 1010 port->status &= ~CTL_PORT_STATUS_ONLINE; 1011 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1012 port->wwpn_iid[i].in_use = 0; 1013 free(port->wwpn_iid[i].name, M_CTL); 1014 port->wwpn_iid[i].name = NULL; 1015 } 1016 } 1017 mtx_unlock(&softc->ctl_lock); 1018} 1019 1020static void 1021ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1022{ 1023 struct ctl_lun *lun; 1024 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1025 1026 mtx_lock(&softc->ctl_lock); 1027 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 1028 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 1029 mtx_lock(&lun->lun_lock); 1030 mtx_unlock(&softc->ctl_lock); 1031 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 1032 msg->ua.ua_set) 1033 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1034 if (msg->ua.ua_all) { 1035 if (msg->ua.ua_set) 1036 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1037 else 1038 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1039 } else { 1040 if (msg->ua.ua_set) 1041 ctl_est_ua(lun, iid, msg->ua.ua_type); 1042 else 1043 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1044 } 1045 mtx_unlock(&lun->lun_lock); 1046 } else 1047 mtx_unlock(&softc->ctl_lock); 1048} 1049 1050static void 1051ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1052{ 1053 struct ctl_lun *lun; 1054 struct ctl_ha_msg_lun_pr_key pr_key; 1055 int i, k; 1056 ctl_lun_flags oflags; 1057 uint32_t targ_lun; 1058 1059 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1060 mtx_lock(&softc->ctl_lock); 1061 if ((targ_lun >= CTL_MAX_LUNS) || 1062 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1063 mtx_unlock(&softc->ctl_lock); 1064 return; 1065 } 1066 mtx_lock(&lun->lun_lock); 1067 mtx_unlock(&softc->ctl_lock); 1068 if (lun->flags & CTL_LUN_DISABLED) { 1069 mtx_unlock(&lun->lun_lock); 1070 return; 1071 } 1072 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1073 if (msg->lun.lun_devid_len != i || (i > 0 && 1074 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1075 mtx_unlock(&lun->lun_lock); 1076 printf("%s: Received conflicting HA LUN %d\n", 1077 __func__, msg->hdr.nexus.targ_lun); 1078 return; 1079 } else { 1080 /* Record whether peer is primary. */ 1081 oflags = lun->flags; 1082 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1083 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1084 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1085 else 1086 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1087 if (oflags != lun->flags) 1088 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1089 1090 /* If peer is primary and we are not -- use data */ 1091 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1092 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1093 lun->pr_generation = msg->lun.pr_generation; 1094 lun->pr_res_idx = msg->lun.pr_res_idx; 1095 lun->pr_res_type = msg->lun.pr_res_type; 1096 lun->pr_key_count = msg->lun.pr_key_count; 1097 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1098 ctl_clr_prkey(lun, k); 1099 for (k = 0; k < msg->lun.pr_key_count; k++) { 1100 memcpy(&pr_key, &msg->lun.data[i], 1101 sizeof(pr_key)); 1102 ctl_alloc_prkey(lun, pr_key.pr_iid); 1103 ctl_set_prkey(lun, pr_key.pr_iid, 1104 pr_key.pr_key); 1105 i += sizeof(pr_key); 1106 } 1107 } 1108 1109 mtx_unlock(&lun->lun_lock); 1110 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1111 __func__, msg->hdr.nexus.targ_lun, 1112 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1113 "primary" : "secondary")); 1114 1115 /* If we are primary but peer doesn't know -- notify */ 1116 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1117 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1118 ctl_isc_announce_lun(lun); 1119 } 1120} 1121 1122static void 1123ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1124{ 1125 struct ctl_port *port; 1126 struct ctl_lun *lun; 1127 int i, new; 1128 1129 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1130 if (port == NULL) { 1131 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1132 msg->hdr.nexus.targ_port)); 1133 new = 1; 1134 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1135 port->frontend = &ha_frontend; 1136 port->targ_port = msg->hdr.nexus.targ_port; 1137 port->fe_datamove = ctl_ha_datamove; 1138 port->fe_done = ctl_ha_done; 1139 } else if (port->frontend == &ha_frontend) { 1140 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1141 msg->hdr.nexus.targ_port)); 1142 new = 0; 1143 } else { 1144 printf("%s: Received conflicting HA port %d\n", 1145 __func__, msg->hdr.nexus.targ_port); 1146 return; 1147 } 1148 port->port_type = msg->port.port_type; 1149 port->physical_port = msg->port.physical_port; 1150 port->virtual_port = msg->port.virtual_port; 1151 port->status = msg->port.status; 1152 i = 0; 1153 free(port->port_name, M_CTL); 1154 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1155 M_CTL); 1156 i += msg->port.name_len; 1157 if (msg->port.lun_map_len != 0) { 1158 if (port->lun_map == NULL) 1159 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 1160 M_CTL, M_WAITOK); 1161 memcpy(port->lun_map, &msg->port.data[i], 1162 sizeof(uint32_t) * CTL_MAX_LUNS); 1163 i += msg->port.lun_map_len; 1164 } else { 1165 free(port->lun_map, M_CTL); 1166 port->lun_map = NULL; 1167 } 1168 if (msg->port.port_devid_len != 0) { 1169 if (port->port_devid == NULL || 1170 port->port_devid->len != msg->port.port_devid_len) { 1171 free(port->port_devid, M_CTL); 1172 port->port_devid = malloc(sizeof(struct ctl_devid) + 1173 msg->port.port_devid_len, M_CTL, M_WAITOK); 1174 } 1175 memcpy(port->port_devid->data, &msg->port.data[i], 1176 msg->port.port_devid_len); 1177 port->port_devid->len = msg->port.port_devid_len; 1178 i += msg->port.port_devid_len; 1179 } else { 1180 free(port->port_devid, M_CTL); 1181 port->port_devid = NULL; 1182 } 1183 if (msg->port.target_devid_len != 0) { 1184 if (port->target_devid == NULL || 1185 port->target_devid->len != msg->port.target_devid_len) { 1186 free(port->target_devid, M_CTL); 1187 port->target_devid = malloc(sizeof(struct ctl_devid) + 1188 msg->port.target_devid_len, M_CTL, M_WAITOK); 1189 } 1190 memcpy(port->target_devid->data, &msg->port.data[i], 1191 msg->port.target_devid_len); 1192 port->target_devid->len = msg->port.target_devid_len; 1193 i += msg->port.target_devid_len; 1194 } else { 1195 free(port->target_devid, M_CTL); 1196 port->target_devid = NULL; 1197 } 1198 if (msg->port.init_devid_len != 0) { 1199 if (port->init_devid == NULL || 1200 port->init_devid->len != msg->port.init_devid_len) { 1201 free(port->init_devid, M_CTL); 1202 port->init_devid = malloc(sizeof(struct ctl_devid) + 1203 msg->port.init_devid_len, M_CTL, M_WAITOK); 1204 } 1205 memcpy(port->init_devid->data, &msg->port.data[i], 1206 msg->port.init_devid_len); 1207 port->init_devid->len = msg->port.init_devid_len; 1208 i += msg->port.init_devid_len; 1209 } else { 1210 free(port->init_devid, M_CTL); 1211 port->init_devid = NULL; 1212 } 1213 if (new) { 1214 if (ctl_port_register(port) != 0) { 1215 printf("%s: ctl_port_register() failed with error\n", 1216 __func__); 1217 } 1218 } 1219 mtx_lock(&softc->ctl_lock); 1220 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1221 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 1222 continue; 1223 mtx_lock(&lun->lun_lock); 1224 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1225 mtx_unlock(&lun->lun_lock); 1226 } 1227 mtx_unlock(&softc->ctl_lock); 1228} 1229 1230static void 1231ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1232{ 1233 struct ctl_port *port; 1234 int iid; 1235 1236 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1237 if (port == NULL) { 1238 printf("%s: Received IID for unknown port %d\n", 1239 __func__, msg->hdr.nexus.targ_port); 1240 return; 1241 } 1242 iid = msg->hdr.nexus.initid; 1243 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1244 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1245 free(port->wwpn_iid[iid].name, M_CTL); 1246 if (msg->iid.name_len) { 1247 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1248 msg->iid.name_len, M_CTL); 1249 } else 1250 port->wwpn_iid[iid].name = NULL; 1251} 1252 1253static void 1254ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1255{ 1256 1257 if (msg->login.version != CTL_HA_VERSION) { 1258 printf("CTL HA peers have different versions %d != %d\n", 1259 msg->login.version, CTL_HA_VERSION); 1260 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1261 return; 1262 } 1263 if (msg->login.ha_mode != softc->ha_mode) { 1264 printf("CTL HA peers have different ha_mode %d != %d\n", 1265 msg->login.ha_mode, softc->ha_mode); 1266 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1267 return; 1268 } 1269 if (msg->login.ha_id == softc->ha_id) { 1270 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1271 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1272 return; 1273 } 1274 if (msg->login.max_luns != CTL_MAX_LUNS || 1275 msg->login.max_ports != CTL_MAX_PORTS || 1276 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1277 printf("CTL HA peers have different limits\n"); 1278 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1279 return; 1280 } 1281} 1282 1283static void 1284ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1285{ 1286 struct ctl_lun *lun; 1287 u_int i; 1288 uint32_t initidx, targ_lun; 1289 1290 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1291 mtx_lock(&softc->ctl_lock); 1292 if ((targ_lun >= CTL_MAX_LUNS) || 1293 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1294 mtx_unlock(&softc->ctl_lock); 1295 return; 1296 } 1297 mtx_lock(&lun->lun_lock); 1298 mtx_unlock(&softc->ctl_lock); 1299 if (lun->flags & CTL_LUN_DISABLED) { 1300 mtx_unlock(&lun->lun_lock); 1301 return; 1302 } 1303 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1304 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1305 msg->mode.page_code && 1306 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1307 break; 1308 } 1309 if (i == CTL_NUM_MODE_PAGES) { 1310 mtx_unlock(&lun->lun_lock); 1311 return; 1312 } 1313 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1314 lun->mode_pages.index[i].page_len); 1315 initidx = ctl_get_initindex(&msg->hdr.nexus); 1316 if (initidx != -1) 1317 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1318 mtx_unlock(&lun->lun_lock); 1319} 1320 1321/* 1322 * ISC (Inter Shelf Communication) event handler. Events from the HA 1323 * subsystem come in here. 1324 */ 1325static void 1326ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1327{ 1328 struct ctl_softc *softc = control_softc; 1329 union ctl_io *io; 1330 struct ctl_prio *presio; 1331 ctl_ha_status isc_status; 1332 1333 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1334 if (event == CTL_HA_EVT_MSG_RECV) { 1335 union ctl_ha_msg *msg, msgbuf; 1336 1337 if (param > sizeof(msgbuf)) 1338 msg = malloc(param, M_CTL, M_WAITOK); 1339 else 1340 msg = &msgbuf; 1341 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1342 M_WAITOK); 1343 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1344 printf("%s: Error receiving message: %d\n", 1345 __func__, isc_status); 1346 if (msg != &msgbuf) 1347 free(msg, M_CTL); 1348 return; 1349 } 1350 1351 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1352 switch (msg->hdr.msg_type) { 1353 case CTL_MSG_SERIALIZE: 1354 io = ctl_alloc_io(softc->othersc_pool); 1355 ctl_zero_io(io); 1356 // populate ctsio from msg 1357 io->io_hdr.io_type = CTL_IO_SCSI; 1358 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1359 io->io_hdr.original_sc = msg->hdr.original_sc; 1360 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1361 CTL_FLAG_IO_ACTIVE; 1362 /* 1363 * If we're in serialization-only mode, we don't 1364 * want to go through full done processing. Thus 1365 * the COPY flag. 1366 * 1367 * XXX KDM add another flag that is more specific. 1368 */ 1369 if (softc->ha_mode != CTL_HA_MODE_XFER) 1370 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1371 io->io_hdr.nexus = msg->hdr.nexus; 1372#if 0 1373 printf("port %u, iid %u, lun %u\n", 1374 io->io_hdr.nexus.targ_port, 1375 io->io_hdr.nexus.initid, 1376 io->io_hdr.nexus.targ_lun); 1377#endif 1378 io->scsiio.tag_num = msg->scsi.tag_num; 1379 io->scsiio.tag_type = msg->scsi.tag_type; 1380#ifdef CTL_TIME_IO 1381 io->io_hdr.start_time = time_uptime; 1382 getbinuptime(&io->io_hdr.start_bt); 1383#endif /* CTL_TIME_IO */ 1384 io->scsiio.cdb_len = msg->scsi.cdb_len; 1385 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1386 CTL_MAX_CDBLEN); 1387 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1388 const struct ctl_cmd_entry *entry; 1389 1390 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1391 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1392 io->io_hdr.flags |= 1393 entry->flags & CTL_FLAG_DATA_MASK; 1394 } 1395 ctl_enqueue_isc(io); 1396 break; 1397 1398 /* Performed on the Originating SC, XFER mode only */ 1399 case CTL_MSG_DATAMOVE: { 1400 struct ctl_sg_entry *sgl; 1401 int i, j; 1402 1403 io = msg->hdr.original_sc; 1404 if (io == NULL) { 1405 printf("%s: original_sc == NULL!\n", __func__); 1406 /* XXX KDM do something here */ 1407 break; 1408 } 1409 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1410 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1411 /* 1412 * Keep track of this, we need to send it back over 1413 * when the datamove is complete. 1414 */ 1415 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1416 if (msg->hdr.status == CTL_SUCCESS) 1417 io->io_hdr.status = msg->hdr.status; 1418 1419 if (msg->dt.sg_sequence == 0) { 1420#ifdef CTL_TIME_IO 1421 getbinuptime(&io->io_hdr.dma_start_bt); 1422#endif 1423 i = msg->dt.kern_sg_entries + 1424 msg->dt.kern_data_len / 1425 CTL_HA_DATAMOVE_SEGMENT + 1; 1426 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1427 M_WAITOK | M_ZERO); 1428 io->io_hdr.remote_sglist = sgl; 1429 io->io_hdr.local_sglist = 1430 &sgl[msg->dt.kern_sg_entries]; 1431 1432 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1433 1434 io->scsiio.kern_sg_entries = 1435 msg->dt.kern_sg_entries; 1436 io->scsiio.rem_sg_entries = 1437 msg->dt.kern_sg_entries; 1438 io->scsiio.kern_data_len = 1439 msg->dt.kern_data_len; 1440 io->scsiio.kern_total_len = 1441 msg->dt.kern_total_len; 1442 io->scsiio.kern_data_resid = 1443 msg->dt.kern_data_resid; 1444 io->scsiio.kern_rel_offset = 1445 msg->dt.kern_rel_offset; 1446 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1447 io->io_hdr.flags |= msg->dt.flags & 1448 CTL_FLAG_BUS_ADDR; 1449 } else 1450 sgl = (struct ctl_sg_entry *) 1451 io->scsiio.kern_data_ptr; 1452 1453 for (i = msg->dt.sent_sg_entries, j = 0; 1454 i < (msg->dt.sent_sg_entries + 1455 msg->dt.cur_sg_entries); i++, j++) { 1456 sgl[i].addr = msg->dt.sg_list[j].addr; 1457 sgl[i].len = msg->dt.sg_list[j].len; 1458 1459#if 0 1460 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1461 __func__, sgl[i].addr, sgl[i].len, j, i); 1462#endif 1463 } 1464 1465 /* 1466 * If this is the last piece of the I/O, we've got 1467 * the full S/G list. Queue processing in the thread. 1468 * Otherwise wait for the next piece. 1469 */ 1470 if (msg->dt.sg_last != 0) 1471 ctl_enqueue_isc(io); 1472 break; 1473 } 1474 /* Performed on the Serializing (primary) SC, XFER mode only */ 1475 case CTL_MSG_DATAMOVE_DONE: { 1476 if (msg->hdr.serializing_sc == NULL) { 1477 printf("%s: serializing_sc == NULL!\n", 1478 __func__); 1479 /* XXX KDM now what? */ 1480 break; 1481 } 1482 /* 1483 * We grab the sense information here in case 1484 * there was a failure, so we can return status 1485 * back to the initiator. 1486 */ 1487 io = msg->hdr.serializing_sc; 1488 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1489 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1490 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1491 io->io_hdr.port_status = msg->scsi.fetd_status; 1492 io->scsiio.residual = msg->scsi.residual; 1493 if (msg->hdr.status != CTL_STATUS_NONE) { 1494 io->io_hdr.status = msg->hdr.status; 1495 io->scsiio.scsi_status = msg->scsi.scsi_status; 1496 io->scsiio.sense_len = msg->scsi.sense_len; 1497 io->scsiio.sense_residual =msg->scsi.sense_residual; 1498 memcpy(&io->scsiio.sense_data, 1499 &msg->scsi.sense_data, 1500 msg->scsi.sense_len); 1501 if (msg->hdr.status == CTL_SUCCESS) 1502 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1503 } 1504 ctl_enqueue_isc(io); 1505 break; 1506 } 1507 1508 /* Preformed on Originating SC, SER_ONLY mode */ 1509 case CTL_MSG_R2R: 1510 io = msg->hdr.original_sc; 1511 if (io == NULL) { 1512 printf("%s: original_sc == NULL!\n", 1513 __func__); 1514 break; 1515 } 1516 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1517 io->io_hdr.msg_type = CTL_MSG_R2R; 1518 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1519 ctl_enqueue_isc(io); 1520 break; 1521 1522 /* 1523 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1524 * mode. 1525 * Performed on the Originating (i.e. secondary) SC in XFER 1526 * mode 1527 */ 1528 case CTL_MSG_FINISH_IO: 1529 if (softc->ha_mode == CTL_HA_MODE_XFER) 1530 ctl_isc_handler_finish_xfer(softc, msg); 1531 else 1532 ctl_isc_handler_finish_ser_only(softc, msg); 1533 break; 1534 1535 /* Preformed on Originating SC */ 1536 case CTL_MSG_BAD_JUJU: 1537 io = msg->hdr.original_sc; 1538 if (io == NULL) { 1539 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1540 __func__); 1541 break; 1542 } 1543 ctl_copy_sense_data(msg, io); 1544 /* 1545 * IO should have already been cleaned up on other 1546 * SC so clear this flag so we won't send a message 1547 * back to finish the IO there. 1548 */ 1549 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1550 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1551 1552 /* io = msg->hdr.serializing_sc; */ 1553 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1554 ctl_enqueue_isc(io); 1555 break; 1556 1557 /* Handle resets sent from the other side */ 1558 case CTL_MSG_MANAGE_TASKS: { 1559 struct ctl_taskio *taskio; 1560 taskio = (struct ctl_taskio *)ctl_alloc_io( 1561 softc->othersc_pool); 1562 ctl_zero_io((union ctl_io *)taskio); 1563 taskio->io_hdr.io_type = CTL_IO_TASK; 1564 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1565 taskio->io_hdr.nexus = msg->hdr.nexus; 1566 taskio->task_action = msg->task.task_action; 1567 taskio->tag_num = msg->task.tag_num; 1568 taskio->tag_type = msg->task.tag_type; 1569#ifdef CTL_TIME_IO 1570 taskio->io_hdr.start_time = time_uptime; 1571 getbinuptime(&taskio->io_hdr.start_bt); 1572#endif /* CTL_TIME_IO */ 1573 ctl_run_task((union ctl_io *)taskio); 1574 break; 1575 } 1576 /* Persistent Reserve action which needs attention */ 1577 case CTL_MSG_PERS_ACTION: 1578 presio = (struct ctl_prio *)ctl_alloc_io( 1579 softc->othersc_pool); 1580 ctl_zero_io((union ctl_io *)presio); 1581 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1582 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1583 presio->io_hdr.nexus = msg->hdr.nexus; 1584 presio->pr_msg = msg->pr; 1585 ctl_enqueue_isc((union ctl_io *)presio); 1586 break; 1587 case CTL_MSG_UA: 1588 ctl_isc_ua(softc, msg, param); 1589 break; 1590 case CTL_MSG_PORT_SYNC: 1591 ctl_isc_port_sync(softc, msg, param); 1592 break; 1593 case CTL_MSG_LUN_SYNC: 1594 ctl_isc_lun_sync(softc, msg, param); 1595 break; 1596 case CTL_MSG_IID_SYNC: 1597 ctl_isc_iid_sync(softc, msg, param); 1598 break; 1599 case CTL_MSG_LOGIN: 1600 ctl_isc_login(softc, msg, param); 1601 break; 1602 case CTL_MSG_MODE_SYNC: 1603 ctl_isc_mode_sync(softc, msg, param); 1604 break; 1605 default: 1606 printf("Received HA message of unknown type %d\n", 1607 msg->hdr.msg_type); 1608 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1609 break; 1610 } 1611 if (msg != &msgbuf) 1612 free(msg, M_CTL); 1613 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1614 printf("CTL: HA link status changed from %d to %d\n", 1615 softc->ha_link, param); 1616 if (param == softc->ha_link) 1617 return; 1618 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1619 softc->ha_link = param; 1620 ctl_isc_ha_link_down(softc); 1621 } else { 1622 softc->ha_link = param; 1623 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1624 ctl_isc_ha_link_up(softc); 1625 } 1626 return; 1627 } else { 1628 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1629 return; 1630 } 1631} 1632 1633static void 1634ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1635{ 1636 1637 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1638 src->scsi.sense_len); 1639 dest->scsiio.scsi_status = src->scsi.scsi_status; 1640 dest->scsiio.sense_len = src->scsi.sense_len; 1641 dest->io_hdr.status = src->hdr.status; 1642} 1643 1644static void 1645ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1646{ 1647 1648 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1649 src->scsiio.sense_len); 1650 dest->scsi.scsi_status = src->scsiio.scsi_status; 1651 dest->scsi.sense_len = src->scsiio.sense_len; 1652 dest->hdr.status = src->io_hdr.status; 1653} 1654 1655void 1656ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1657{ 1658 struct ctl_softc *softc = lun->ctl_softc; 1659 ctl_ua_type *pu; 1660 1661 if (initidx < softc->init_min || initidx >= softc->init_max) 1662 return; 1663 mtx_assert(&lun->lun_lock, MA_OWNED); 1664 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1665 if (pu == NULL) 1666 return; 1667 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1668} 1669 1670void 1671ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1672{ 1673 int i; 1674 1675 mtx_assert(&lun->lun_lock, MA_OWNED); 1676 if (lun->pending_ua[port] == NULL) 1677 return; 1678 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1679 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1680 continue; 1681 lun->pending_ua[port][i] |= ua; 1682 } 1683} 1684 1685void 1686ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1687{ 1688 struct ctl_softc *softc = lun->ctl_softc; 1689 int i; 1690 1691 mtx_assert(&lun->lun_lock, MA_OWNED); 1692 for (i = softc->port_min; i < softc->port_max; i++) 1693 ctl_est_ua_port(lun, i, except, ua); 1694} 1695 1696void 1697ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1698{ 1699 struct ctl_softc *softc = lun->ctl_softc; 1700 ctl_ua_type *pu; 1701 1702 if (initidx < softc->init_min || initidx >= softc->init_max) 1703 return; 1704 mtx_assert(&lun->lun_lock, MA_OWNED); 1705 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1706 if (pu == NULL) 1707 return; 1708 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1709} 1710 1711void 1712ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1713{ 1714 struct ctl_softc *softc = lun->ctl_softc; 1715 int i, j; 1716 1717 mtx_assert(&lun->lun_lock, MA_OWNED); 1718 for (i = softc->port_min; i < softc->port_max; i++) { 1719 if (lun->pending_ua[i] == NULL) 1720 continue; 1721 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1722 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1723 continue; 1724 lun->pending_ua[i][j] &= ~ua; 1725 } 1726 } 1727} 1728 1729void 1730ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1731 ctl_ua_type ua_type) 1732{ 1733 struct ctl_lun *lun; 1734 1735 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1736 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1737 mtx_lock(&lun->lun_lock); 1738 ctl_clr_ua(lun, initidx, ua_type); 1739 mtx_unlock(&lun->lun_lock); 1740 } 1741} 1742 1743static int 1744ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1745{ 1746 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1747 struct ctl_lun *lun; 1748 struct ctl_lun_req ireq; 1749 int error, value; 1750 1751 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1752 error = sysctl_handle_int(oidp, &value, 0, req); 1753 if ((error != 0) || (req->newptr == NULL)) 1754 return (error); 1755 1756 mtx_lock(&softc->ctl_lock); 1757 if (value == 0) 1758 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1759 else 1760 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1761 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1762 mtx_unlock(&softc->ctl_lock); 1763 bzero(&ireq, sizeof(ireq)); 1764 ireq.reqtype = CTL_LUNREQ_MODIFY; 1765 ireq.reqdata.modify.lun_id = lun->lun; 1766 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1767 curthread); 1768 if (ireq.status != CTL_LUN_OK) { 1769 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1770 __func__, ireq.status, ireq.error_str); 1771 } 1772 mtx_lock(&softc->ctl_lock); 1773 } 1774 mtx_unlock(&softc->ctl_lock); 1775 return (0); 1776} 1777 1778static int 1779ctl_init(void) 1780{ 1781 struct make_dev_args args; 1782 struct ctl_softc *softc; 1783 void *other_pool; 1784 int i, error; 1785 1786 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1787 M_WAITOK | M_ZERO); 1788 1789 make_dev_args_init(&args); 1790 args.mda_devsw = &ctl_cdevsw; 1791 args.mda_uid = UID_ROOT; 1792 args.mda_gid = GID_OPERATOR; 1793 args.mda_mode = 0600; 1794 args.mda_si_drv1 = softc; 1795 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1796 if (error != 0) { 1797 free(control_softc, M_DEVBUF); 1798 return (error); 1799 } 1800 1801 sysctl_ctx_init(&softc->sysctl_ctx); 1802 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1803 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1804 CTLFLAG_RD, 0, "CAM Target Layer"); 1805 1806 if (softc->sysctl_tree == NULL) { 1807 printf("%s: unable to allocate sysctl tree\n", __func__); 1808 destroy_dev(softc->dev); 1809 free(control_softc, M_DEVBUF); 1810 control_softc = NULL; 1811 return (ENOMEM); 1812 } 1813 1814 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1815 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1816 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1817 softc->flags = 0; 1818 1819 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1820 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1821 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1822 1823 /* 1824 * In Copan's HA scheme, the "master" and "slave" roles are 1825 * figured out through the slot the controller is in. Although it 1826 * is an active/active system, someone has to be in charge. 1827 */ 1828 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1829 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1830 "HA head ID (0 - no HA)"); 1831 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1832 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1833 softc->is_single = 1; 1834 softc->port_cnt = CTL_MAX_PORTS; 1835 softc->port_min = 0; 1836 } else { 1837 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES; 1838 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1839 } 1840 softc->port_max = softc->port_min + softc->port_cnt; 1841 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1842 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1843 1844 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1845 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1846 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1847 1848 STAILQ_INIT(&softc->lun_list); 1849 STAILQ_INIT(&softc->pending_lun_queue); 1850 STAILQ_INIT(&softc->fe_list); 1851 STAILQ_INIT(&softc->port_list); 1852 STAILQ_INIT(&softc->be_list); 1853 ctl_tpc_init(softc); 1854 1855 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1856 &other_pool) != 0) 1857 { 1858 printf("ctl: can't allocate %d entry other SC pool, " 1859 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1860 return (ENOMEM); 1861 } 1862 softc->othersc_pool = other_pool; 1863 1864 if (worker_threads <= 0) 1865 worker_threads = max(1, mp_ncpus / 4); 1866 if (worker_threads > CTL_MAX_THREADS) 1867 worker_threads = CTL_MAX_THREADS; 1868 1869 for (i = 0; i < worker_threads; i++) { 1870 struct ctl_thread *thr = &softc->threads[i]; 1871 1872 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1873 thr->ctl_softc = softc; 1874 STAILQ_INIT(&thr->incoming_queue); 1875 STAILQ_INIT(&thr->rtr_queue); 1876 STAILQ_INIT(&thr->done_queue); 1877 STAILQ_INIT(&thr->isc_queue); 1878 1879 error = kproc_kthread_add(ctl_work_thread, thr, 1880 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1881 if (error != 0) { 1882 printf("error creating CTL work thread!\n"); 1883 ctl_pool_free(other_pool); 1884 return (error); 1885 } 1886 } 1887 error = kproc_kthread_add(ctl_lun_thread, softc, 1888 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1889 if (error != 0) { 1890 printf("error creating CTL lun thread!\n"); 1891 ctl_pool_free(other_pool); 1892 return (error); 1893 } 1894 error = kproc_kthread_add(ctl_thresh_thread, softc, 1895 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1896 if (error != 0) { 1897 printf("error creating CTL threshold thread!\n"); 1898 ctl_pool_free(other_pool); 1899 return (error); 1900 } 1901 1902 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1903 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1904 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1905 1906 if (softc->is_single == 0) { 1907 ctl_frontend_register(&ha_frontend); 1908 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1909 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1910 softc->is_single = 1; 1911 } else 1912 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1913 != CTL_HA_STATUS_SUCCESS) { 1914 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1915 softc->is_single = 1; 1916 } 1917 } 1918 return (0); 1919} 1920 1921void 1922ctl_shutdown(void) 1923{ 1924 struct ctl_softc *softc = control_softc; 1925 struct ctl_lun *lun, *next_lun; 1926 1927 if (softc->is_single == 0) { 1928 ctl_ha_msg_shutdown(softc); 1929 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1930 != CTL_HA_STATUS_SUCCESS) 1931 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1932 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1933 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1934 ctl_frontend_deregister(&ha_frontend); 1935 } 1936 1937 mtx_lock(&softc->ctl_lock); 1938 1939 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1940 ctl_free_lun(lun); 1941 1942 mtx_unlock(&softc->ctl_lock); 1943 1944#if 0 1945 ctl_shutdown_thread(softc->work_thread); 1946 mtx_destroy(&softc->queue_lock); 1947#endif 1948 1949 ctl_tpc_shutdown(softc); 1950 uma_zdestroy(softc->io_zone); 1951 mtx_destroy(&softc->ctl_lock); 1952 1953 destroy_dev(softc->dev); 1954 1955 sysctl_ctx_free(&softc->sysctl_ctx); 1956 1957 free(control_softc, M_DEVBUF); 1958 control_softc = NULL; 1959} 1960 1961static int 1962ctl_module_event_handler(module_t mod, int what, void *arg) 1963{ 1964 1965 switch (what) { 1966 case MOD_LOAD: 1967 return (ctl_init()); 1968 case MOD_UNLOAD: 1969 return (EBUSY); 1970 default: 1971 return (EOPNOTSUPP); 1972 } 1973} 1974 1975/* 1976 * XXX KDM should we do some access checks here? Bump a reference count to 1977 * prevent a CTL module from being unloaded while someone has it open? 1978 */ 1979static int 1980ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1981{ 1982 return (0); 1983} 1984 1985static int 1986ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1987{ 1988 return (0); 1989} 1990 1991/* 1992 * Remove an initiator by port number and initiator ID. 1993 * Returns 0 for success, -1 for failure. 1994 */ 1995int 1996ctl_remove_initiator(struct ctl_port *port, int iid) 1997{ 1998 struct ctl_softc *softc = port->ctl_softc; 1999 2000 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2001 2002 if (iid > CTL_MAX_INIT_PER_PORT) { 2003 printf("%s: initiator ID %u > maximun %u!\n", 2004 __func__, iid, CTL_MAX_INIT_PER_PORT); 2005 return (-1); 2006 } 2007 2008 mtx_lock(&softc->ctl_lock); 2009 port->wwpn_iid[iid].in_use--; 2010 port->wwpn_iid[iid].last_use = time_uptime; 2011 mtx_unlock(&softc->ctl_lock); 2012 ctl_isc_announce_iid(port, iid); 2013 2014 return (0); 2015} 2016 2017/* 2018 * Add an initiator to the initiator map. 2019 * Returns iid for success, < 0 for failure. 2020 */ 2021int 2022ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2023{ 2024 struct ctl_softc *softc = port->ctl_softc; 2025 time_t best_time; 2026 int i, best; 2027 2028 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2029 2030 if (iid >= CTL_MAX_INIT_PER_PORT) { 2031 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2032 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2033 free(name, M_CTL); 2034 return (-1); 2035 } 2036 2037 mtx_lock(&softc->ctl_lock); 2038 2039 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2040 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2041 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2042 iid = i; 2043 break; 2044 } 2045 if (name != NULL && port->wwpn_iid[i].name != NULL && 2046 strcmp(name, port->wwpn_iid[i].name) == 0) { 2047 iid = i; 2048 break; 2049 } 2050 } 2051 } 2052 2053 if (iid < 0) { 2054 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2055 if (port->wwpn_iid[i].in_use == 0 && 2056 port->wwpn_iid[i].wwpn == 0 && 2057 port->wwpn_iid[i].name == NULL) { 2058 iid = i; 2059 break; 2060 } 2061 } 2062 } 2063 2064 if (iid < 0) { 2065 best = -1; 2066 best_time = INT32_MAX; 2067 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2068 if (port->wwpn_iid[i].in_use == 0) { 2069 if (port->wwpn_iid[i].last_use < best_time) { 2070 best = i; 2071 best_time = port->wwpn_iid[i].last_use; 2072 } 2073 } 2074 } 2075 iid = best; 2076 } 2077 2078 if (iid < 0) { 2079 mtx_unlock(&softc->ctl_lock); 2080 free(name, M_CTL); 2081 return (-2); 2082 } 2083 2084 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2085 /* 2086 * This is not an error yet. 2087 */ 2088 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2089#if 0 2090 printf("%s: port %d iid %u WWPN %#jx arrived" 2091 " again\n", __func__, port->targ_port, 2092 iid, (uintmax_t)wwpn); 2093#endif 2094 goto take; 2095 } 2096 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2097 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2098#if 0 2099 printf("%s: port %d iid %u name '%s' arrived" 2100 " again\n", __func__, port->targ_port, 2101 iid, name); 2102#endif 2103 goto take; 2104 } 2105 2106 /* 2107 * This is an error, but what do we do about it? The 2108 * driver is telling us we have a new WWPN for this 2109 * initiator ID, so we pretty much need to use it. 2110 */ 2111 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2112 " but WWPN %#jx '%s' is still at that address\n", 2113 __func__, port->targ_port, iid, wwpn, name, 2114 (uintmax_t)port->wwpn_iid[iid].wwpn, 2115 port->wwpn_iid[iid].name); 2116 2117 /* 2118 * XXX KDM clear have_ca and ua_pending on each LUN for 2119 * this initiator. 2120 */ 2121 } 2122take: 2123 free(port->wwpn_iid[iid].name, M_CTL); 2124 port->wwpn_iid[iid].name = name; 2125 port->wwpn_iid[iid].wwpn = wwpn; 2126 port->wwpn_iid[iid].in_use++; 2127 mtx_unlock(&softc->ctl_lock); 2128 ctl_isc_announce_iid(port, iid); 2129 2130 return (iid); 2131} 2132 2133static int 2134ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2135{ 2136 int len; 2137 2138 switch (port->port_type) { 2139 case CTL_PORT_FC: 2140 { 2141 struct scsi_transportid_fcp *id = 2142 (struct scsi_transportid_fcp *)buf; 2143 if (port->wwpn_iid[iid].wwpn == 0) 2144 return (0); 2145 memset(id, 0, sizeof(*id)); 2146 id->format_protocol = SCSI_PROTO_FC; 2147 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2148 return (sizeof(*id)); 2149 } 2150 case CTL_PORT_ISCSI: 2151 { 2152 struct scsi_transportid_iscsi_port *id = 2153 (struct scsi_transportid_iscsi_port *)buf; 2154 if (port->wwpn_iid[iid].name == NULL) 2155 return (0); 2156 memset(id, 0, 256); 2157 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2158 SCSI_PROTO_ISCSI; 2159 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2160 len = roundup2(min(len, 252), 4); 2161 scsi_ulto2b(len, id->additional_length); 2162 return (sizeof(*id) + len); 2163 } 2164 case CTL_PORT_SAS: 2165 { 2166 struct scsi_transportid_sas *id = 2167 (struct scsi_transportid_sas *)buf; 2168 if (port->wwpn_iid[iid].wwpn == 0) 2169 return (0); 2170 memset(id, 0, sizeof(*id)); 2171 id->format_protocol = SCSI_PROTO_SAS; 2172 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2173 return (sizeof(*id)); 2174 } 2175 default: 2176 { 2177 struct scsi_transportid_spi *id = 2178 (struct scsi_transportid_spi *)buf; 2179 memset(id, 0, sizeof(*id)); 2180 id->format_protocol = SCSI_PROTO_SPI; 2181 scsi_ulto2b(iid, id->scsi_addr); 2182 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2183 return (sizeof(*id)); 2184 } 2185 } 2186} 2187 2188/* 2189 * Serialize a command that went down the "wrong" side, and so was sent to 2190 * this controller for execution. The logic is a little different than the 2191 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2192 * sent back to the other side, but in the success case, we execute the 2193 * command on this side (XFER mode) or tell the other side to execute it 2194 * (SER_ONLY mode). 2195 */ 2196static int 2197ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2198{ 2199 struct ctl_softc *softc = control_softc; 2200 union ctl_ha_msg msg_info; 2201 struct ctl_port *port; 2202 struct ctl_lun *lun; 2203 const struct ctl_cmd_entry *entry; 2204 int retval = 0; 2205 uint32_t targ_lun; 2206 2207 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2208 mtx_lock(&softc->ctl_lock); 2209 2210 /* Make sure that we know about this port. */ 2211 port = ctl_io_port(&ctsio->io_hdr); 2212 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2213 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2214 /*retry_count*/ 1); 2215 goto badjuju; 2216 } 2217 2218 /* Make sure that we know about this LUN. */ 2219 if ((targ_lun < CTL_MAX_LUNS) && 2220 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 2221 mtx_lock(&lun->lun_lock); 2222 mtx_unlock(&softc->ctl_lock); 2223 /* 2224 * If the LUN is invalid, pretend that it doesn't exist. 2225 * It will go away as soon as all pending I/O has been 2226 * completed. 2227 */ 2228 if (lun->flags & CTL_LUN_DISABLED) { 2229 mtx_unlock(&lun->lun_lock); 2230 lun = NULL; 2231 } 2232 } else { 2233 mtx_unlock(&softc->ctl_lock); 2234 lun = NULL; 2235 } 2236 if (lun == NULL) { 2237 /* 2238 * The other node would not send this request to us unless 2239 * received announce that we are primary node for this LUN. 2240 * If this LUN does not exist now, it is probably result of 2241 * a race, so respond to initiator in the most opaque way. 2242 */ 2243 ctl_set_busy(ctsio); 2244 goto badjuju; 2245 } 2246 2247 entry = ctl_get_cmd_entry(ctsio, NULL); 2248 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2249 mtx_unlock(&lun->lun_lock); 2250 goto badjuju; 2251 } 2252 2253 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2254 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2255 2256 /* 2257 * Every I/O goes into the OOA queue for a 2258 * particular LUN, and stays there until completion. 2259 */ 2260#ifdef CTL_TIME_IO 2261 if (TAILQ_EMPTY(&lun->ooa_queue)) 2262 lun->idle_time += getsbinuptime() - lun->last_busy; 2263#endif 2264 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2265 2266 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2267 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2268 ooa_links))) { 2269 case CTL_ACTION_BLOCK: 2270 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2271 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2272 blocked_links); 2273 mtx_unlock(&lun->lun_lock); 2274 break; 2275 case CTL_ACTION_PASS: 2276 case CTL_ACTION_SKIP: 2277 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2278 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2279 ctl_enqueue_rtr((union ctl_io *)ctsio); 2280 mtx_unlock(&lun->lun_lock); 2281 } else { 2282 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2283 mtx_unlock(&lun->lun_lock); 2284 2285 /* send msg back to other side */ 2286 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2287 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2288 msg_info.hdr.msg_type = CTL_MSG_R2R; 2289 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2290 sizeof(msg_info.hdr), M_WAITOK); 2291 } 2292 break; 2293 case CTL_ACTION_OVERLAP: 2294 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2295 mtx_unlock(&lun->lun_lock); 2296 ctl_set_overlapped_cmd(ctsio); 2297 goto badjuju; 2298 case CTL_ACTION_OVERLAP_TAG: 2299 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2300 mtx_unlock(&lun->lun_lock); 2301 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2302 goto badjuju; 2303 case CTL_ACTION_ERROR: 2304 default: 2305 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2306 mtx_unlock(&lun->lun_lock); 2307 2308 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2309 /*retry_count*/ 0); 2310badjuju: 2311 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2312 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2313 msg_info.hdr.serializing_sc = NULL; 2314 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2315 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2316 sizeof(msg_info.scsi), M_WAITOK); 2317 retval = 1; 2318 break; 2319 } 2320 return (retval); 2321} 2322 2323/* 2324 * Returns 0 for success, errno for failure. 2325 */ 2326static void 2327ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2328 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2329{ 2330 union ctl_io *io; 2331 2332 mtx_lock(&lun->lun_lock); 2333 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2334 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2335 ooa_links)) { 2336 struct ctl_ooa_entry *entry; 2337 2338 /* 2339 * If we've got more than we can fit, just count the 2340 * remaining entries. 2341 */ 2342 if (*cur_fill_num >= ooa_hdr->alloc_num) 2343 continue; 2344 2345 entry = &kern_entries[*cur_fill_num]; 2346 2347 entry->tag_num = io->scsiio.tag_num; 2348 entry->lun_num = lun->lun; 2349#ifdef CTL_TIME_IO 2350 entry->start_bt = io->io_hdr.start_bt; 2351#endif 2352 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2353 entry->cdb_len = io->scsiio.cdb_len; 2354 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2355 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2356 2357 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2358 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2359 2360 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2361 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2362 2363 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2364 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2365 2366 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2367 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2368 } 2369 mtx_unlock(&lun->lun_lock); 2370} 2371 2372static void * 2373ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str, 2374 size_t error_str_len) 2375{ 2376 void *kptr; 2377 2378 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2379 2380 if (copyin(user_addr, kptr, len) != 0) { 2381 snprintf(error_str, error_str_len, "Error copying %d bytes " 2382 "from user address %p to kernel address %p", len, 2383 user_addr, kptr); 2384 free(kptr, M_CTL); 2385 return (NULL); 2386 } 2387 2388 return (kptr); 2389} 2390 2391static void 2392ctl_free_args(int num_args, struct ctl_be_arg *args) 2393{ 2394 int i; 2395 2396 if (args == NULL) 2397 return; 2398 2399 for (i = 0; i < num_args; i++) { 2400 free(args[i].kname, M_CTL); 2401 free(args[i].kvalue, M_CTL); 2402 } 2403 2404 free(args, M_CTL); 2405} 2406 2407static struct ctl_be_arg * 2408ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2409 char *error_str, size_t error_str_len) 2410{ 2411 struct ctl_be_arg *args; 2412 int i; 2413 2414 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2415 error_str, error_str_len); 2416 2417 if (args == NULL) 2418 goto bailout; 2419 2420 for (i = 0; i < num_args; i++) { 2421 args[i].kname = NULL; 2422 args[i].kvalue = NULL; 2423 } 2424 2425 for (i = 0; i < num_args; i++) { 2426 uint8_t *tmpptr; 2427 2428 if (args[i].namelen == 0) { 2429 snprintf(error_str, error_str_len, "Argument %d " 2430 "name length is zero", i); 2431 goto bailout; 2432 } 2433 2434 args[i].kname = ctl_copyin_alloc(args[i].name, 2435 args[i].namelen, error_str, error_str_len); 2436 if (args[i].kname == NULL) 2437 goto bailout; 2438 2439 if (args[i].kname[args[i].namelen - 1] != '\0') { 2440 snprintf(error_str, error_str_len, "Argument %d " 2441 "name is not NUL-terminated", i); 2442 goto bailout; 2443 } 2444 2445 if (args[i].flags & CTL_BEARG_RD) { 2446 if (args[i].vallen == 0) { 2447 snprintf(error_str, error_str_len, "Argument %d " 2448 "value length is zero", i); 2449 goto bailout; 2450 } 2451 2452 tmpptr = ctl_copyin_alloc(args[i].value, 2453 args[i].vallen, error_str, error_str_len); 2454 if (tmpptr == NULL) 2455 goto bailout; 2456 2457 if ((args[i].flags & CTL_BEARG_ASCII) 2458 && (tmpptr[args[i].vallen - 1] != '\0')) { 2459 snprintf(error_str, error_str_len, "Argument " 2460 "%d value is not NUL-terminated", i); 2461 free(tmpptr, M_CTL); 2462 goto bailout; 2463 } 2464 args[i].kvalue = tmpptr; 2465 } else { 2466 args[i].kvalue = malloc(args[i].vallen, 2467 M_CTL, M_WAITOK | M_ZERO); 2468 } 2469 } 2470 2471 return (args); 2472bailout: 2473 2474 ctl_free_args(num_args, args); 2475 2476 return (NULL); 2477} 2478 2479static void 2480ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2481{ 2482 int i; 2483 2484 for (i = 0; i < num_args; i++) { 2485 if (args[i].flags & CTL_BEARG_WR) 2486 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2487 } 2488} 2489 2490/* 2491 * Escape characters that are illegal or not recommended in XML. 2492 */ 2493int 2494ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2495{ 2496 char *end = str + size; 2497 int retval; 2498 2499 retval = 0; 2500 2501 for (; *str && str < end; str++) { 2502 switch (*str) { 2503 case '&': 2504 retval = sbuf_printf(sb, "&"); 2505 break; 2506 case '>': 2507 retval = sbuf_printf(sb, ">"); 2508 break; 2509 case '<': 2510 retval = sbuf_printf(sb, "<"); 2511 break; 2512 default: 2513 retval = sbuf_putc(sb, *str); 2514 break; 2515 } 2516 2517 if (retval != 0) 2518 break; 2519 2520 } 2521 2522 return (retval); 2523} 2524 2525static void 2526ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2527{ 2528 struct scsi_vpd_id_descriptor *desc; 2529 int i; 2530 2531 if (id == NULL || id->len < 4) 2532 return; 2533 desc = (struct scsi_vpd_id_descriptor *)id->data; 2534 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2535 case SVPD_ID_TYPE_T10: 2536 sbuf_printf(sb, "t10."); 2537 break; 2538 case SVPD_ID_TYPE_EUI64: 2539 sbuf_printf(sb, "eui."); 2540 break; 2541 case SVPD_ID_TYPE_NAA: 2542 sbuf_printf(sb, "naa."); 2543 break; 2544 case SVPD_ID_TYPE_SCSI_NAME: 2545 break; 2546 } 2547 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2548 case SVPD_ID_CODESET_BINARY: 2549 for (i = 0; i < desc->length; i++) 2550 sbuf_printf(sb, "%02x", desc->identifier[i]); 2551 break; 2552 case SVPD_ID_CODESET_ASCII: 2553 sbuf_printf(sb, "%.*s", (int)desc->length, 2554 (char *)desc->identifier); 2555 break; 2556 case SVPD_ID_CODESET_UTF8: 2557 sbuf_printf(sb, "%s", (char *)desc->identifier); 2558 break; 2559 } 2560} 2561 2562static int 2563ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2564 struct thread *td) 2565{ 2566 struct ctl_softc *softc = dev->si_drv1; 2567 struct ctl_lun *lun; 2568 int retval; 2569 2570 retval = 0; 2571 2572 switch (cmd) { 2573 case CTL_IO: 2574 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2575 break; 2576 case CTL_ENABLE_PORT: 2577 case CTL_DISABLE_PORT: 2578 case CTL_SET_PORT_WWNS: { 2579 struct ctl_port *port; 2580 struct ctl_port_entry *entry; 2581 2582 entry = (struct ctl_port_entry *)addr; 2583 2584 mtx_lock(&softc->ctl_lock); 2585 STAILQ_FOREACH(port, &softc->port_list, links) { 2586 int action, done; 2587 2588 if (port->targ_port < softc->port_min || 2589 port->targ_port >= softc->port_max) 2590 continue; 2591 2592 action = 0; 2593 done = 0; 2594 if ((entry->port_type == CTL_PORT_NONE) 2595 && (entry->targ_port == port->targ_port)) { 2596 /* 2597 * If the user only wants to enable or 2598 * disable or set WWNs on a specific port, 2599 * do the operation and we're done. 2600 */ 2601 action = 1; 2602 done = 1; 2603 } else if (entry->port_type & port->port_type) { 2604 /* 2605 * Compare the user's type mask with the 2606 * particular frontend type to see if we 2607 * have a match. 2608 */ 2609 action = 1; 2610 done = 0; 2611 2612 /* 2613 * Make sure the user isn't trying to set 2614 * WWNs on multiple ports at the same time. 2615 */ 2616 if (cmd == CTL_SET_PORT_WWNS) { 2617 printf("%s: Can't set WWNs on " 2618 "multiple ports\n", __func__); 2619 retval = EINVAL; 2620 break; 2621 } 2622 } 2623 if (action == 0) 2624 continue; 2625 2626 /* 2627 * XXX KDM we have to drop the lock here, because 2628 * the online/offline operations can potentially 2629 * block. We need to reference count the frontends 2630 * so they can't go away, 2631 */ 2632 if (cmd == CTL_ENABLE_PORT) { 2633 mtx_unlock(&softc->ctl_lock); 2634 ctl_port_online(port); 2635 mtx_lock(&softc->ctl_lock); 2636 } else if (cmd == CTL_DISABLE_PORT) { 2637 mtx_unlock(&softc->ctl_lock); 2638 ctl_port_offline(port); 2639 mtx_lock(&softc->ctl_lock); 2640 } else if (cmd == CTL_SET_PORT_WWNS) { 2641 ctl_port_set_wwns(port, 2642 (entry->flags & CTL_PORT_WWNN_VALID) ? 2643 1 : 0, entry->wwnn, 2644 (entry->flags & CTL_PORT_WWPN_VALID) ? 2645 1 : 0, entry->wwpn); 2646 } 2647 if (done != 0) 2648 break; 2649 } 2650 mtx_unlock(&softc->ctl_lock); 2651 break; 2652 } 2653 case CTL_GET_OOA: { 2654 struct ctl_ooa *ooa_hdr; 2655 struct ctl_ooa_entry *entries; 2656 uint32_t cur_fill_num; 2657 2658 ooa_hdr = (struct ctl_ooa *)addr; 2659 2660 if ((ooa_hdr->alloc_len == 0) 2661 || (ooa_hdr->alloc_num == 0)) { 2662 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2663 "must be non-zero\n", __func__, 2664 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2665 retval = EINVAL; 2666 break; 2667 } 2668 2669 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2670 sizeof(struct ctl_ooa_entry))) { 2671 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2672 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2673 __func__, ooa_hdr->alloc_len, 2674 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2675 retval = EINVAL; 2676 break; 2677 } 2678 2679 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2680 if (entries == NULL) { 2681 printf("%s: could not allocate %d bytes for OOA " 2682 "dump\n", __func__, ooa_hdr->alloc_len); 2683 retval = ENOMEM; 2684 break; 2685 } 2686 2687 mtx_lock(&softc->ctl_lock); 2688 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2689 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2690 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2691 mtx_unlock(&softc->ctl_lock); 2692 free(entries, M_CTL); 2693 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2694 __func__, (uintmax_t)ooa_hdr->lun_num); 2695 retval = EINVAL; 2696 break; 2697 } 2698 2699 cur_fill_num = 0; 2700 2701 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2702 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2703 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2704 ooa_hdr, entries); 2705 } 2706 } else { 2707 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2708 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2709 entries); 2710 } 2711 mtx_unlock(&softc->ctl_lock); 2712 2713 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2714 ooa_hdr->fill_len = ooa_hdr->fill_num * 2715 sizeof(struct ctl_ooa_entry); 2716 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2717 if (retval != 0) { 2718 printf("%s: error copying out %d bytes for OOA dump\n", 2719 __func__, ooa_hdr->fill_len); 2720 } 2721 2722 getbinuptime(&ooa_hdr->cur_bt); 2723 2724 if (cur_fill_num > ooa_hdr->alloc_num) { 2725 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2726 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2727 } else { 2728 ooa_hdr->dropped_num = 0; 2729 ooa_hdr->status = CTL_OOA_OK; 2730 } 2731 2732 free(entries, M_CTL); 2733 break; 2734 } 2735 case CTL_DELAY_IO: { 2736 struct ctl_io_delay_info *delay_info; 2737 2738 delay_info = (struct ctl_io_delay_info *)addr; 2739 2740#ifdef CTL_IO_DELAY 2741 mtx_lock(&softc->ctl_lock); 2742 2743 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2744 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2745 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2746 } else { 2747 lun = softc->ctl_luns[delay_info->lun_id]; 2748 mtx_lock(&lun->lun_lock); 2749 2750 delay_info->status = CTL_DELAY_STATUS_OK; 2751 2752 switch (delay_info->delay_type) { 2753 case CTL_DELAY_TYPE_CONT: 2754 break; 2755 case CTL_DELAY_TYPE_ONESHOT: 2756 break; 2757 default: 2758 delay_info->status = 2759 CTL_DELAY_STATUS_INVALID_TYPE; 2760 break; 2761 } 2762 2763 switch (delay_info->delay_loc) { 2764 case CTL_DELAY_LOC_DATAMOVE: 2765 lun->delay_info.datamove_type = 2766 delay_info->delay_type; 2767 lun->delay_info.datamove_delay = 2768 delay_info->delay_secs; 2769 break; 2770 case CTL_DELAY_LOC_DONE: 2771 lun->delay_info.done_type = 2772 delay_info->delay_type; 2773 lun->delay_info.done_delay = 2774 delay_info->delay_secs; 2775 break; 2776 default: 2777 delay_info->status = 2778 CTL_DELAY_STATUS_INVALID_LOC; 2779 break; 2780 } 2781 mtx_unlock(&lun->lun_lock); 2782 } 2783 2784 mtx_unlock(&softc->ctl_lock); 2785#else 2786 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2787#endif /* CTL_IO_DELAY */ 2788 break; 2789 } 2790 case CTL_GETSTATS: { 2791 struct ctl_stats *stats; 2792 int i; 2793 2794 stats = (struct ctl_stats *)addr; 2795 2796 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2797 stats->alloc_len) { 2798 stats->status = CTL_SS_NEED_MORE_SPACE; 2799 stats->num_luns = softc->num_luns; 2800 break; 2801 } 2802 /* 2803 * XXX KDM no locking here. If the LUN list changes, 2804 * things can blow up. 2805 */ 2806 i = 0; 2807 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2808 retval = copyout(&lun->stats, &stats->lun_stats[i++], 2809 sizeof(lun->stats)); 2810 if (retval != 0) 2811 break; 2812 } 2813 stats->num_luns = softc->num_luns; 2814 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2815 softc->num_luns; 2816 stats->status = CTL_SS_OK; 2817#ifdef CTL_TIME_IO 2818 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2819#else 2820 stats->flags = CTL_STATS_FLAG_NONE; 2821#endif 2822 getnanouptime(&stats->timestamp); 2823 break; 2824 } 2825 case CTL_ERROR_INJECT: { 2826 struct ctl_error_desc *err_desc, *new_err_desc; 2827 2828 err_desc = (struct ctl_error_desc *)addr; 2829 2830 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2831 M_WAITOK | M_ZERO); 2832 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2833 2834 mtx_lock(&softc->ctl_lock); 2835 lun = softc->ctl_luns[err_desc->lun_id]; 2836 if (lun == NULL) { 2837 mtx_unlock(&softc->ctl_lock); 2838 free(new_err_desc, M_CTL); 2839 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2840 __func__, (uintmax_t)err_desc->lun_id); 2841 retval = EINVAL; 2842 break; 2843 } 2844 mtx_lock(&lun->lun_lock); 2845 mtx_unlock(&softc->ctl_lock); 2846 2847 /* 2848 * We could do some checking here to verify the validity 2849 * of the request, but given the complexity of error 2850 * injection requests, the checking logic would be fairly 2851 * complex. 2852 * 2853 * For now, if the request is invalid, it just won't get 2854 * executed and might get deleted. 2855 */ 2856 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2857 2858 /* 2859 * XXX KDM check to make sure the serial number is unique, 2860 * in case we somehow manage to wrap. That shouldn't 2861 * happen for a very long time, but it's the right thing to 2862 * do. 2863 */ 2864 new_err_desc->serial = lun->error_serial; 2865 err_desc->serial = lun->error_serial; 2866 lun->error_serial++; 2867 2868 mtx_unlock(&lun->lun_lock); 2869 break; 2870 } 2871 case CTL_ERROR_INJECT_DELETE: { 2872 struct ctl_error_desc *delete_desc, *desc, *desc2; 2873 int delete_done; 2874 2875 delete_desc = (struct ctl_error_desc *)addr; 2876 delete_done = 0; 2877 2878 mtx_lock(&softc->ctl_lock); 2879 lun = softc->ctl_luns[delete_desc->lun_id]; 2880 if (lun == NULL) { 2881 mtx_unlock(&softc->ctl_lock); 2882 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2883 __func__, (uintmax_t)delete_desc->lun_id); 2884 retval = EINVAL; 2885 break; 2886 } 2887 mtx_lock(&lun->lun_lock); 2888 mtx_unlock(&softc->ctl_lock); 2889 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2890 if (desc->serial != delete_desc->serial) 2891 continue; 2892 2893 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2894 links); 2895 free(desc, M_CTL); 2896 delete_done = 1; 2897 } 2898 mtx_unlock(&lun->lun_lock); 2899 if (delete_done == 0) { 2900 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2901 "error serial %ju on LUN %u\n", __func__, 2902 delete_desc->serial, delete_desc->lun_id); 2903 retval = EINVAL; 2904 break; 2905 } 2906 break; 2907 } 2908 case CTL_DUMP_STRUCTS: { 2909 int i, j, k; 2910 struct ctl_port *port; 2911 struct ctl_frontend *fe; 2912 2913 mtx_lock(&softc->ctl_lock); 2914 printf("CTL Persistent Reservation information start:\n"); 2915 for (i = 0; i < CTL_MAX_LUNS; i++) { 2916 lun = softc->ctl_luns[i]; 2917 2918 if ((lun == NULL) 2919 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2920 continue; 2921 2922 for (j = 0; j < CTL_MAX_PORTS; j++) { 2923 if (lun->pr_keys[j] == NULL) 2924 continue; 2925 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2926 if (lun->pr_keys[j][k] == 0) 2927 continue; 2928 printf(" LUN %d port %d iid %d key " 2929 "%#jx\n", i, j, k, 2930 (uintmax_t)lun->pr_keys[j][k]); 2931 } 2932 } 2933 } 2934 printf("CTL Persistent Reservation information end\n"); 2935 printf("CTL Ports:\n"); 2936 STAILQ_FOREACH(port, &softc->port_list, links) { 2937 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2938 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2939 port->frontend->name, port->port_type, 2940 port->physical_port, port->virtual_port, 2941 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2942 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2943 if (port->wwpn_iid[j].in_use == 0 && 2944 port->wwpn_iid[j].wwpn == 0 && 2945 port->wwpn_iid[j].name == NULL) 2946 continue; 2947 2948 printf(" iid %u use %d WWPN %#jx '%s'\n", 2949 j, port->wwpn_iid[j].in_use, 2950 (uintmax_t)port->wwpn_iid[j].wwpn, 2951 port->wwpn_iid[j].name); 2952 } 2953 } 2954 printf("CTL Port information end\n"); 2955 mtx_unlock(&softc->ctl_lock); 2956 /* 2957 * XXX KDM calling this without a lock. We'd likely want 2958 * to drop the lock before calling the frontend's dump 2959 * routine anyway. 2960 */ 2961 printf("CTL Frontends:\n"); 2962 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2963 printf(" Frontend '%s'\n", fe->name); 2964 if (fe->fe_dump != NULL) 2965 fe->fe_dump(); 2966 } 2967 printf("CTL Frontend information end\n"); 2968 break; 2969 } 2970 case CTL_LUN_REQ: { 2971 struct ctl_lun_req *lun_req; 2972 struct ctl_backend_driver *backend; 2973 2974 lun_req = (struct ctl_lun_req *)addr; 2975 2976 backend = ctl_backend_find(lun_req->backend); 2977 if (backend == NULL) { 2978 lun_req->status = CTL_LUN_ERROR; 2979 snprintf(lun_req->error_str, 2980 sizeof(lun_req->error_str), 2981 "Backend \"%s\" not found.", 2982 lun_req->backend); 2983 break; 2984 } 2985 if (lun_req->num_be_args > 0) { 2986 lun_req->kern_be_args = ctl_copyin_args( 2987 lun_req->num_be_args, 2988 lun_req->be_args, 2989 lun_req->error_str, 2990 sizeof(lun_req->error_str)); 2991 if (lun_req->kern_be_args == NULL) { 2992 lun_req->status = CTL_LUN_ERROR; 2993 break; 2994 } 2995 } 2996 2997 retval = backend->ioctl(dev, cmd, addr, flag, td); 2998 2999 if (lun_req->num_be_args > 0) { 3000 ctl_copyout_args(lun_req->num_be_args, 3001 lun_req->kern_be_args); 3002 ctl_free_args(lun_req->num_be_args, 3003 lun_req->kern_be_args); 3004 } 3005 break; 3006 } 3007 case CTL_LUN_LIST: { 3008 struct sbuf *sb; 3009 struct ctl_lun_list *list; 3010 struct ctl_option *opt; 3011 3012 list = (struct ctl_lun_list *)addr; 3013 3014 /* 3015 * Allocate a fixed length sbuf here, based on the length 3016 * of the user's buffer. We could allocate an auto-extending 3017 * buffer, and then tell the user how much larger our 3018 * amount of data is than his buffer, but that presents 3019 * some problems: 3020 * 3021 * 1. The sbuf(9) routines use a blocking malloc, and so 3022 * we can't hold a lock while calling them with an 3023 * auto-extending buffer. 3024 * 3025 * 2. There is not currently a LUN reference counting 3026 * mechanism, outside of outstanding transactions on 3027 * the LUN's OOA queue. So a LUN could go away on us 3028 * while we're getting the LUN number, backend-specific 3029 * information, etc. Thus, given the way things 3030 * currently work, we need to hold the CTL lock while 3031 * grabbing LUN information. 3032 * 3033 * So, from the user's standpoint, the best thing to do is 3034 * allocate what he thinks is a reasonable buffer length, 3035 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3036 * double the buffer length and try again. (And repeat 3037 * that until he succeeds.) 3038 */ 3039 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3040 if (sb == NULL) { 3041 list->status = CTL_LUN_LIST_ERROR; 3042 snprintf(list->error_str, sizeof(list->error_str), 3043 "Unable to allocate %d bytes for LUN list", 3044 list->alloc_len); 3045 break; 3046 } 3047 3048 sbuf_printf(sb, "<ctllunlist>\n"); 3049 3050 mtx_lock(&softc->ctl_lock); 3051 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3052 mtx_lock(&lun->lun_lock); 3053 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3054 (uintmax_t)lun->lun); 3055 3056 /* 3057 * Bail out as soon as we see that we've overfilled 3058 * the buffer. 3059 */ 3060 if (retval != 0) 3061 break; 3062 3063 retval = sbuf_printf(sb, "\t<backend_type>%s" 3064 "</backend_type>\n", 3065 (lun->backend == NULL) ? "none" : 3066 lun->backend->name); 3067 3068 if (retval != 0) 3069 break; 3070 3071 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3072 lun->be_lun->lun_type); 3073 3074 if (retval != 0) 3075 break; 3076 3077 if (lun->backend == NULL) { 3078 retval = sbuf_printf(sb, "</lun>\n"); 3079 if (retval != 0) 3080 break; 3081 continue; 3082 } 3083 3084 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3085 (lun->be_lun->maxlba > 0) ? 3086 lun->be_lun->maxlba + 1 : 0); 3087 3088 if (retval != 0) 3089 break; 3090 3091 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3092 lun->be_lun->blocksize); 3093 3094 if (retval != 0) 3095 break; 3096 3097 retval = sbuf_printf(sb, "\t<serial_number>"); 3098 3099 if (retval != 0) 3100 break; 3101 3102 retval = ctl_sbuf_printf_esc(sb, 3103 lun->be_lun->serial_num, 3104 sizeof(lun->be_lun->serial_num)); 3105 3106 if (retval != 0) 3107 break; 3108 3109 retval = sbuf_printf(sb, "</serial_number>\n"); 3110 3111 if (retval != 0) 3112 break; 3113 3114 retval = sbuf_printf(sb, "\t<device_id>"); 3115 3116 if (retval != 0) 3117 break; 3118 3119 retval = ctl_sbuf_printf_esc(sb, 3120 lun->be_lun->device_id, 3121 sizeof(lun->be_lun->device_id)); 3122 3123 if (retval != 0) 3124 break; 3125 3126 retval = sbuf_printf(sb, "</device_id>\n"); 3127 3128 if (retval != 0) 3129 break; 3130 3131 if (lun->backend->lun_info != NULL) { 3132 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3133 if (retval != 0) 3134 break; 3135 } 3136 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3137 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3138 opt->name, opt->value, opt->name); 3139 if (retval != 0) 3140 break; 3141 } 3142 3143 retval = sbuf_printf(sb, "</lun>\n"); 3144 3145 if (retval != 0) 3146 break; 3147 mtx_unlock(&lun->lun_lock); 3148 } 3149 if (lun != NULL) 3150 mtx_unlock(&lun->lun_lock); 3151 mtx_unlock(&softc->ctl_lock); 3152 3153 if ((retval != 0) 3154 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3155 retval = 0; 3156 sbuf_delete(sb); 3157 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3158 snprintf(list->error_str, sizeof(list->error_str), 3159 "Out of space, %d bytes is too small", 3160 list->alloc_len); 3161 break; 3162 } 3163 3164 sbuf_finish(sb); 3165 3166 retval = copyout(sbuf_data(sb), list->lun_xml, 3167 sbuf_len(sb) + 1); 3168 3169 list->fill_len = sbuf_len(sb) + 1; 3170 list->status = CTL_LUN_LIST_OK; 3171 sbuf_delete(sb); 3172 break; 3173 } 3174 case CTL_ISCSI: { 3175 struct ctl_iscsi *ci; 3176 struct ctl_frontend *fe; 3177 3178 ci = (struct ctl_iscsi *)addr; 3179 3180 fe = ctl_frontend_find("iscsi"); 3181 if (fe == NULL) { 3182 ci->status = CTL_ISCSI_ERROR; 3183 snprintf(ci->error_str, sizeof(ci->error_str), 3184 "Frontend \"iscsi\" not found."); 3185 break; 3186 } 3187 3188 retval = fe->ioctl(dev, cmd, addr, flag, td); 3189 break; 3190 } 3191 case CTL_PORT_REQ: { 3192 struct ctl_req *req; 3193 struct ctl_frontend *fe; 3194 3195 req = (struct ctl_req *)addr; 3196 3197 fe = ctl_frontend_find(req->driver); 3198 if (fe == NULL) { 3199 req->status = CTL_LUN_ERROR; 3200 snprintf(req->error_str, sizeof(req->error_str), 3201 "Frontend \"%s\" not found.", req->driver); 3202 break; 3203 } 3204 if (req->num_args > 0) { 3205 req->kern_args = ctl_copyin_args(req->num_args, 3206 req->args, req->error_str, sizeof(req->error_str)); 3207 if (req->kern_args == NULL) { 3208 req->status = CTL_LUN_ERROR; 3209 break; 3210 } 3211 } 3212 3213 if (fe->ioctl) 3214 retval = fe->ioctl(dev, cmd, addr, flag, td); 3215 else 3216 retval = ENODEV; 3217 3218 if (req->num_args > 0) { 3219 ctl_copyout_args(req->num_args, req->kern_args); 3220 ctl_free_args(req->num_args, req->kern_args); 3221 } 3222 break; 3223 } 3224 case CTL_PORT_LIST: { 3225 struct sbuf *sb; 3226 struct ctl_port *port; 3227 struct ctl_lun_list *list; 3228 struct ctl_option *opt; 3229 int j; 3230 uint32_t plun; 3231 3232 list = (struct ctl_lun_list *)addr; 3233 3234 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3235 if (sb == NULL) { 3236 list->status = CTL_LUN_LIST_ERROR; 3237 snprintf(list->error_str, sizeof(list->error_str), 3238 "Unable to allocate %d bytes for LUN list", 3239 list->alloc_len); 3240 break; 3241 } 3242 3243 sbuf_printf(sb, "<ctlportlist>\n"); 3244 3245 mtx_lock(&softc->ctl_lock); 3246 STAILQ_FOREACH(port, &softc->port_list, links) { 3247 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3248 (uintmax_t)port->targ_port); 3249 3250 /* 3251 * Bail out as soon as we see that we've overfilled 3252 * the buffer. 3253 */ 3254 if (retval != 0) 3255 break; 3256 3257 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3258 "</frontend_type>\n", port->frontend->name); 3259 if (retval != 0) 3260 break; 3261 3262 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3263 port->port_type); 3264 if (retval != 0) 3265 break; 3266 3267 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3268 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3269 if (retval != 0) 3270 break; 3271 3272 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3273 port->port_name); 3274 if (retval != 0) 3275 break; 3276 3277 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3278 port->physical_port); 3279 if (retval != 0) 3280 break; 3281 3282 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3283 port->virtual_port); 3284 if (retval != 0) 3285 break; 3286 3287 if (port->target_devid != NULL) { 3288 sbuf_printf(sb, "\t<target>"); 3289 ctl_id_sbuf(port->target_devid, sb); 3290 sbuf_printf(sb, "</target>\n"); 3291 } 3292 3293 if (port->port_devid != NULL) { 3294 sbuf_printf(sb, "\t<port>"); 3295 ctl_id_sbuf(port->port_devid, sb); 3296 sbuf_printf(sb, "</port>\n"); 3297 } 3298 3299 if (port->port_info != NULL) { 3300 retval = port->port_info(port->onoff_arg, sb); 3301 if (retval != 0) 3302 break; 3303 } 3304 STAILQ_FOREACH(opt, &port->options, links) { 3305 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3306 opt->name, opt->value, opt->name); 3307 if (retval != 0) 3308 break; 3309 } 3310 3311 if (port->lun_map != NULL) { 3312 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3313 for (j = 0; j < CTL_MAX_LUNS; j++) { 3314 plun = ctl_lun_map_from_port(port, j); 3315 if (plun >= CTL_MAX_LUNS) 3316 continue; 3317 sbuf_printf(sb, 3318 "\t<lun id=\"%u\">%u</lun>\n", 3319 j, plun); 3320 } 3321 } 3322 3323 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3324 if (port->wwpn_iid[j].in_use == 0 || 3325 (port->wwpn_iid[j].wwpn == 0 && 3326 port->wwpn_iid[j].name == NULL)) 3327 continue; 3328 3329 if (port->wwpn_iid[j].name != NULL) 3330 retval = sbuf_printf(sb, 3331 "\t<initiator id=\"%u\">%s</initiator>\n", 3332 j, port->wwpn_iid[j].name); 3333 else 3334 retval = sbuf_printf(sb, 3335 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3336 j, port->wwpn_iid[j].wwpn); 3337 if (retval != 0) 3338 break; 3339 } 3340 if (retval != 0) 3341 break; 3342 3343 retval = sbuf_printf(sb, "</targ_port>\n"); 3344 if (retval != 0) 3345 break; 3346 } 3347 mtx_unlock(&softc->ctl_lock); 3348 3349 if ((retval != 0) 3350 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3351 retval = 0; 3352 sbuf_delete(sb); 3353 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3354 snprintf(list->error_str, sizeof(list->error_str), 3355 "Out of space, %d bytes is too small", 3356 list->alloc_len); 3357 break; 3358 } 3359 3360 sbuf_finish(sb); 3361 3362 retval = copyout(sbuf_data(sb), list->lun_xml, 3363 sbuf_len(sb) + 1); 3364 3365 list->fill_len = sbuf_len(sb) + 1; 3366 list->status = CTL_LUN_LIST_OK; 3367 sbuf_delete(sb); 3368 break; 3369 } 3370 case CTL_LUN_MAP: { 3371 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3372 struct ctl_port *port; 3373 3374 mtx_lock(&softc->ctl_lock); 3375 if (lm->port < softc->port_min || 3376 lm->port >= softc->port_max || 3377 (port = softc->ctl_ports[lm->port]) == NULL) { 3378 mtx_unlock(&softc->ctl_lock); 3379 return (ENXIO); 3380 } 3381 if (port->status & CTL_PORT_STATUS_ONLINE) { 3382 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3383 if (ctl_lun_map_to_port(port, lun->lun) >= 3384 CTL_MAX_LUNS) 3385 continue; 3386 mtx_lock(&lun->lun_lock); 3387 ctl_est_ua_port(lun, lm->port, -1, 3388 CTL_UA_LUN_CHANGE); 3389 mtx_unlock(&lun->lun_lock); 3390 } 3391 } 3392 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3393 if (lm->plun < CTL_MAX_LUNS) { 3394 if (lm->lun == UINT32_MAX) 3395 retval = ctl_lun_map_unset(port, lm->plun); 3396 else if (lm->lun < CTL_MAX_LUNS && 3397 softc->ctl_luns[lm->lun] != NULL) 3398 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3399 else 3400 return (ENXIO); 3401 } else if (lm->plun == UINT32_MAX) { 3402 if (lm->lun == UINT32_MAX) 3403 retval = ctl_lun_map_deinit(port); 3404 else 3405 retval = ctl_lun_map_init(port); 3406 } else 3407 return (ENXIO); 3408 if (port->status & CTL_PORT_STATUS_ONLINE) 3409 ctl_isc_announce_port(port); 3410 break; 3411 } 3412 default: { 3413 /* XXX KDM should we fix this? */ 3414#if 0 3415 struct ctl_backend_driver *backend; 3416 unsigned int type; 3417 int found; 3418 3419 found = 0; 3420 3421 /* 3422 * We encode the backend type as the ioctl type for backend 3423 * ioctls. So parse it out here, and then search for a 3424 * backend of this type. 3425 */ 3426 type = _IOC_TYPE(cmd); 3427 3428 STAILQ_FOREACH(backend, &softc->be_list, links) { 3429 if (backend->type == type) { 3430 found = 1; 3431 break; 3432 } 3433 } 3434 if (found == 0) { 3435 printf("ctl: unknown ioctl command %#lx or backend " 3436 "%d\n", cmd, type); 3437 retval = EINVAL; 3438 break; 3439 } 3440 retval = backend->ioctl(dev, cmd, addr, flag, td); 3441#endif 3442 retval = ENOTTY; 3443 break; 3444 } 3445 } 3446 return (retval); 3447} 3448 3449uint32_t 3450ctl_get_initindex(struct ctl_nexus *nexus) 3451{ 3452 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3453} 3454 3455int 3456ctl_lun_map_init(struct ctl_port *port) 3457{ 3458 struct ctl_softc *softc = port->ctl_softc; 3459 struct ctl_lun *lun; 3460 uint32_t i; 3461 3462 if (port->lun_map == NULL) 3463 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3464 M_CTL, M_NOWAIT); 3465 if (port->lun_map == NULL) 3466 return (ENOMEM); 3467 for (i = 0; i < CTL_MAX_LUNS; i++) 3468 port->lun_map[i] = UINT32_MAX; 3469 if (port->status & CTL_PORT_STATUS_ONLINE) { 3470 if (port->lun_disable != NULL) { 3471 STAILQ_FOREACH(lun, &softc->lun_list, links) 3472 port->lun_disable(port->targ_lun_arg, lun->lun); 3473 } 3474 ctl_isc_announce_port(port); 3475 } 3476 return (0); 3477} 3478 3479int 3480ctl_lun_map_deinit(struct ctl_port *port) 3481{ 3482 struct ctl_softc *softc = port->ctl_softc; 3483 struct ctl_lun *lun; 3484 3485 if (port->lun_map == NULL) 3486 return (0); 3487 free(port->lun_map, M_CTL); 3488 port->lun_map = NULL; 3489 if (port->status & CTL_PORT_STATUS_ONLINE) { 3490 if (port->lun_enable != NULL) { 3491 STAILQ_FOREACH(lun, &softc->lun_list, links) 3492 port->lun_enable(port->targ_lun_arg, lun->lun); 3493 } 3494 ctl_isc_announce_port(port); 3495 } 3496 return (0); 3497} 3498 3499int 3500ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3501{ 3502 int status; 3503 uint32_t old; 3504 3505 if (port->lun_map == NULL) { 3506 status = ctl_lun_map_init(port); 3507 if (status != 0) 3508 return (status); 3509 } 3510 old = port->lun_map[plun]; 3511 port->lun_map[plun] = glun; 3512 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3513 if (port->lun_enable != NULL) 3514 port->lun_enable(port->targ_lun_arg, plun); 3515 ctl_isc_announce_port(port); 3516 } 3517 return (0); 3518} 3519 3520int 3521ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3522{ 3523 uint32_t old; 3524 3525 if (port->lun_map == NULL) 3526 return (0); 3527 old = port->lun_map[plun]; 3528 port->lun_map[plun] = UINT32_MAX; 3529 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3530 if (port->lun_disable != NULL) 3531 port->lun_disable(port->targ_lun_arg, plun); 3532 ctl_isc_announce_port(port); 3533 } 3534 return (0); 3535} 3536 3537uint32_t 3538ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3539{ 3540 3541 if (port == NULL) 3542 return (UINT32_MAX); 3543 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3544 return (lun_id); 3545 return (port->lun_map[lun_id]); 3546} 3547 3548uint32_t 3549ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3550{ 3551 uint32_t i; 3552 3553 if (port == NULL) 3554 return (UINT32_MAX); 3555 if (port->lun_map == NULL) 3556 return (lun_id); 3557 for (i = 0; i < CTL_MAX_LUNS; i++) { 3558 if (port->lun_map[i] == lun_id) 3559 return (i); 3560 } 3561 return (UINT32_MAX); 3562} 3563 3564uint32_t 3565ctl_decode_lun(uint64_t encoded) 3566{ 3567 uint8_t lun[8]; 3568 uint32_t result = 0xffffffff; 3569 3570 be64enc(lun, encoded); 3571 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3572 case RPL_LUNDATA_ATYP_PERIPH: 3573 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3574 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3575 result = lun[1]; 3576 break; 3577 case RPL_LUNDATA_ATYP_FLAT: 3578 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3579 lun[6] == 0 && lun[7] == 0) 3580 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3581 break; 3582 case RPL_LUNDATA_ATYP_EXTLUN: 3583 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3584 case 0x02: 3585 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3586 case 0x00: 3587 result = lun[1]; 3588 break; 3589 case 0x10: 3590 result = (lun[1] << 16) + (lun[2] << 8) + 3591 lun[3]; 3592 break; 3593 case 0x20: 3594 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3595 result = (lun[2] << 24) + 3596 (lun[3] << 16) + (lun[4] << 8) + 3597 lun[5]; 3598 break; 3599 } 3600 break; 3601 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3602 result = 0xffffffff; 3603 break; 3604 } 3605 break; 3606 } 3607 return (result); 3608} 3609 3610uint64_t 3611ctl_encode_lun(uint32_t decoded) 3612{ 3613 uint64_t l = decoded; 3614 3615 if (l <= 0xff) 3616 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3617 if (l <= 0x3fff) 3618 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3619 if (l <= 0xffffff) 3620 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3621 (l << 32)); 3622 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3623} 3624 3625static struct ctl_port * 3626ctl_io_port(struct ctl_io_hdr *io_hdr) 3627{ 3628 3629 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3630} 3631 3632int 3633ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3634{ 3635 int i; 3636 3637 for (i = first; i < last; i++) { 3638 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3639 return (i); 3640 } 3641 return (-1); 3642} 3643 3644int 3645ctl_set_mask(uint32_t *mask, uint32_t bit) 3646{ 3647 uint32_t chunk, piece; 3648 3649 chunk = bit >> 5; 3650 piece = bit % (sizeof(uint32_t) * 8); 3651 3652 if ((mask[chunk] & (1 << piece)) != 0) 3653 return (-1); 3654 else 3655 mask[chunk] |= (1 << piece); 3656 3657 return (0); 3658} 3659 3660int 3661ctl_clear_mask(uint32_t *mask, uint32_t bit) 3662{ 3663 uint32_t chunk, piece; 3664 3665 chunk = bit >> 5; 3666 piece = bit % (sizeof(uint32_t) * 8); 3667 3668 if ((mask[chunk] & (1 << piece)) == 0) 3669 return (-1); 3670 else 3671 mask[chunk] &= ~(1 << piece); 3672 3673 return (0); 3674} 3675 3676int 3677ctl_is_set(uint32_t *mask, uint32_t bit) 3678{ 3679 uint32_t chunk, piece; 3680 3681 chunk = bit >> 5; 3682 piece = bit % (sizeof(uint32_t) * 8); 3683 3684 if ((mask[chunk] & (1 << piece)) == 0) 3685 return (0); 3686 else 3687 return (1); 3688} 3689 3690static uint64_t 3691ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3692{ 3693 uint64_t *t; 3694 3695 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3696 if (t == NULL) 3697 return (0); 3698 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3699} 3700 3701static void 3702ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3703{ 3704 uint64_t *t; 3705 3706 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3707 if (t == NULL) 3708 return; 3709 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3710} 3711 3712static void 3713ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3714{ 3715 uint64_t *p; 3716 u_int i; 3717 3718 i = residx/CTL_MAX_INIT_PER_PORT; 3719 if (lun->pr_keys[i] != NULL) 3720 return; 3721 mtx_unlock(&lun->lun_lock); 3722 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3723 M_WAITOK | M_ZERO); 3724 mtx_lock(&lun->lun_lock); 3725 if (lun->pr_keys[i] == NULL) 3726 lun->pr_keys[i] = p; 3727 else 3728 free(p, M_CTL); 3729} 3730 3731static void 3732ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3733{ 3734 uint64_t *t; 3735 3736 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3737 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3738 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3739} 3740 3741/* 3742 * ctl_softc, pool_name, total_ctl_io are passed in. 3743 * npool is passed out. 3744 */ 3745int 3746ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3747 uint32_t total_ctl_io, void **npool) 3748{ 3749#ifdef IO_POOLS 3750 struct ctl_io_pool *pool; 3751 3752 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3753 M_NOWAIT | M_ZERO); 3754 if (pool == NULL) 3755 return (ENOMEM); 3756 3757 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3758 pool->ctl_softc = ctl_softc; 3759 pool->zone = uma_zsecond_create(pool->name, NULL, 3760 NULL, NULL, NULL, ctl_softc->io_zone); 3761 /* uma_prealloc(pool->zone, total_ctl_io); */ 3762 3763 *npool = pool; 3764#else 3765 *npool = ctl_softc->io_zone; 3766#endif 3767 return (0); 3768} 3769 3770void 3771ctl_pool_free(struct ctl_io_pool *pool) 3772{ 3773 3774 if (pool == NULL) 3775 return; 3776 3777#ifdef IO_POOLS 3778 uma_zdestroy(pool->zone); 3779 free(pool, M_CTL); 3780#endif 3781} 3782 3783union ctl_io * 3784ctl_alloc_io(void *pool_ref) 3785{ 3786 union ctl_io *io; 3787#ifdef IO_POOLS 3788 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3789 3790 io = uma_zalloc(pool->zone, M_WAITOK); 3791#else 3792 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3793#endif 3794 if (io != NULL) 3795 io->io_hdr.pool = pool_ref; 3796 return (io); 3797} 3798 3799union ctl_io * 3800ctl_alloc_io_nowait(void *pool_ref) 3801{ 3802 union ctl_io *io; 3803#ifdef IO_POOLS 3804 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3805 3806 io = uma_zalloc(pool->zone, M_NOWAIT); 3807#else 3808 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3809#endif 3810 if (io != NULL) 3811 io->io_hdr.pool = pool_ref; 3812 return (io); 3813} 3814 3815void 3816ctl_free_io(union ctl_io *io) 3817{ 3818#ifdef IO_POOLS 3819 struct ctl_io_pool *pool; 3820#endif 3821 3822 if (io == NULL) 3823 return; 3824 3825#ifdef IO_POOLS 3826 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3827 uma_zfree(pool->zone, io); 3828#else 3829 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3830#endif 3831} 3832 3833void 3834ctl_zero_io(union ctl_io *io) 3835{ 3836 void *pool_ref; 3837 3838 if (io == NULL) 3839 return; 3840 3841 /* 3842 * May need to preserve linked list pointers at some point too. 3843 */ 3844 pool_ref = io->io_hdr.pool; 3845 memset(io, 0, sizeof(*io)); 3846 io->io_hdr.pool = pool_ref; 3847} 3848 3849int 3850ctl_expand_number(const char *buf, uint64_t *num) 3851{ 3852 char *endptr; 3853 uint64_t number; 3854 unsigned shift; 3855 3856 number = strtoq(buf, &endptr, 0); 3857 3858 switch (tolower((unsigned char)*endptr)) { 3859 case 'e': 3860 shift = 60; 3861 break; 3862 case 'p': 3863 shift = 50; 3864 break; 3865 case 't': 3866 shift = 40; 3867 break; 3868 case 'g': 3869 shift = 30; 3870 break; 3871 case 'm': 3872 shift = 20; 3873 break; 3874 case 'k': 3875 shift = 10; 3876 break; 3877 case 'b': 3878 case '\0': /* No unit. */ 3879 *num = number; 3880 return (0); 3881 default: 3882 /* Unrecognized unit. */ 3883 return (-1); 3884 } 3885 3886 if ((number << shift) >> shift != number) { 3887 /* Overflow */ 3888 return (-1); 3889 } 3890 *num = number << shift; 3891 return (0); 3892} 3893 3894 3895/* 3896 * This routine could be used in the future to load default and/or saved 3897 * mode page parameters for a particuar lun. 3898 */ 3899static int 3900ctl_init_page_index(struct ctl_lun *lun) 3901{ 3902 int i, page_code; 3903 struct ctl_page_index *page_index; 3904 const char *value; 3905 uint64_t ival; 3906 3907 memcpy(&lun->mode_pages.index, page_index_template, 3908 sizeof(page_index_template)); 3909 3910 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3911 3912 page_index = &lun->mode_pages.index[i]; 3913 if (lun->be_lun->lun_type == T_DIRECT && 3914 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3915 continue; 3916 if (lun->be_lun->lun_type == T_PROCESSOR && 3917 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3918 continue; 3919 if (lun->be_lun->lun_type == T_CDROM && 3920 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3921 continue; 3922 3923 page_code = page_index->page_code & SMPH_PC_MASK; 3924 switch (page_code) { 3925 case SMS_RW_ERROR_RECOVERY_PAGE: { 3926 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3927 ("subpage %#x for page %#x is incorrect!", 3928 page_index->subpage, page_code)); 3929 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3930 &rw_er_page_default, 3931 sizeof(rw_er_page_default)); 3932 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3933 &rw_er_page_changeable, 3934 sizeof(rw_er_page_changeable)); 3935 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3936 &rw_er_page_default, 3937 sizeof(rw_er_page_default)); 3938 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3939 &rw_er_page_default, 3940 sizeof(rw_er_page_default)); 3941 page_index->page_data = 3942 (uint8_t *)lun->mode_pages.rw_er_page; 3943 break; 3944 } 3945 case SMS_FORMAT_DEVICE_PAGE: { 3946 struct scsi_format_page *format_page; 3947 3948 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3949 ("subpage %#x for page %#x is incorrect!", 3950 page_index->subpage, page_code)); 3951 3952 /* 3953 * Sectors per track are set above. Bytes per 3954 * sector need to be set here on a per-LUN basis. 3955 */ 3956 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3957 &format_page_default, 3958 sizeof(format_page_default)); 3959 memcpy(&lun->mode_pages.format_page[ 3960 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3961 sizeof(format_page_changeable)); 3962 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3963 &format_page_default, 3964 sizeof(format_page_default)); 3965 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3966 &format_page_default, 3967 sizeof(format_page_default)); 3968 3969 format_page = &lun->mode_pages.format_page[ 3970 CTL_PAGE_CURRENT]; 3971 scsi_ulto2b(lun->be_lun->blocksize, 3972 format_page->bytes_per_sector); 3973 3974 format_page = &lun->mode_pages.format_page[ 3975 CTL_PAGE_DEFAULT]; 3976 scsi_ulto2b(lun->be_lun->blocksize, 3977 format_page->bytes_per_sector); 3978 3979 format_page = &lun->mode_pages.format_page[ 3980 CTL_PAGE_SAVED]; 3981 scsi_ulto2b(lun->be_lun->blocksize, 3982 format_page->bytes_per_sector); 3983 3984 page_index->page_data = 3985 (uint8_t *)lun->mode_pages.format_page; 3986 break; 3987 } 3988 case SMS_RIGID_DISK_PAGE: { 3989 struct scsi_rigid_disk_page *rigid_disk_page; 3990 uint32_t sectors_per_cylinder; 3991 uint64_t cylinders; 3992#ifndef __XSCALE__ 3993 int shift; 3994#endif /* !__XSCALE__ */ 3995 3996 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3997 ("subpage %#x for page %#x is incorrect!", 3998 page_index->subpage, page_code)); 3999 4000 /* 4001 * Rotation rate and sectors per track are set 4002 * above. We calculate the cylinders here based on 4003 * capacity. Due to the number of heads and 4004 * sectors per track we're using, smaller arrays 4005 * may turn out to have 0 cylinders. Linux and 4006 * FreeBSD don't pay attention to these mode pages 4007 * to figure out capacity, but Solaris does. It 4008 * seems to deal with 0 cylinders just fine, and 4009 * works out a fake geometry based on the capacity. 4010 */ 4011 memcpy(&lun->mode_pages.rigid_disk_page[ 4012 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4013 sizeof(rigid_disk_page_default)); 4014 memcpy(&lun->mode_pages.rigid_disk_page[ 4015 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4016 sizeof(rigid_disk_page_changeable)); 4017 4018 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4019 CTL_DEFAULT_HEADS; 4020 4021 /* 4022 * The divide method here will be more accurate, 4023 * probably, but results in floating point being 4024 * used in the kernel on i386 (__udivdi3()). On the 4025 * XScale, though, __udivdi3() is implemented in 4026 * software. 4027 * 4028 * The shift method for cylinder calculation is 4029 * accurate if sectors_per_cylinder is a power of 4030 * 2. Otherwise it might be slightly off -- you 4031 * might have a bit of a truncation problem. 4032 */ 4033#ifdef __XSCALE__ 4034 cylinders = (lun->be_lun->maxlba + 1) / 4035 sectors_per_cylinder; 4036#else 4037 for (shift = 31; shift > 0; shift--) { 4038 if (sectors_per_cylinder & (1 << shift)) 4039 break; 4040 } 4041 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4042#endif 4043 4044 /* 4045 * We've basically got 3 bytes, or 24 bits for the 4046 * cylinder size in the mode page. If we're over, 4047 * just round down to 2^24. 4048 */ 4049 if (cylinders > 0xffffff) 4050 cylinders = 0xffffff; 4051 4052 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4053 CTL_PAGE_DEFAULT]; 4054 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4055 4056 if ((value = ctl_get_opt(&lun->be_lun->options, 4057 "rpm")) != NULL) { 4058 scsi_ulto2b(strtol(value, NULL, 0), 4059 rigid_disk_page->rotation_rate); 4060 } 4061 4062 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4063 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4064 sizeof(rigid_disk_page_default)); 4065 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4066 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4067 sizeof(rigid_disk_page_default)); 4068 4069 page_index->page_data = 4070 (uint8_t *)lun->mode_pages.rigid_disk_page; 4071 break; 4072 } 4073 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4074 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4075 ("subpage %#x for page %#x is incorrect!", 4076 page_index->subpage, page_code)); 4077 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4078 &verify_er_page_default, 4079 sizeof(verify_er_page_default)); 4080 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4081 &verify_er_page_changeable, 4082 sizeof(verify_er_page_changeable)); 4083 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4084 &verify_er_page_default, 4085 sizeof(verify_er_page_default)); 4086 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4087 &verify_er_page_default, 4088 sizeof(verify_er_page_default)); 4089 page_index->page_data = 4090 (uint8_t *)lun->mode_pages.verify_er_page; 4091 break; 4092 } 4093 case SMS_CACHING_PAGE: { 4094 struct scsi_caching_page *caching_page; 4095 4096 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4097 ("subpage %#x for page %#x is incorrect!", 4098 page_index->subpage, page_code)); 4099 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4100 &caching_page_default, 4101 sizeof(caching_page_default)); 4102 memcpy(&lun->mode_pages.caching_page[ 4103 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4104 sizeof(caching_page_changeable)); 4105 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4106 &caching_page_default, 4107 sizeof(caching_page_default)); 4108 caching_page = &lun->mode_pages.caching_page[ 4109 CTL_PAGE_SAVED]; 4110 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4111 if (value != NULL && strcmp(value, "off") == 0) 4112 caching_page->flags1 &= ~SCP_WCE; 4113 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4114 if (value != NULL && strcmp(value, "off") == 0) 4115 caching_page->flags1 |= SCP_RCD; 4116 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4117 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4118 sizeof(caching_page_default)); 4119 page_index->page_data = 4120 (uint8_t *)lun->mode_pages.caching_page; 4121 break; 4122 } 4123 case SMS_CONTROL_MODE_PAGE: { 4124 switch (page_index->subpage) { 4125 case SMS_SUBPAGE_PAGE_0: { 4126 struct scsi_control_page *control_page; 4127 4128 memcpy(&lun->mode_pages.control_page[ 4129 CTL_PAGE_DEFAULT], 4130 &control_page_default, 4131 sizeof(control_page_default)); 4132 memcpy(&lun->mode_pages.control_page[ 4133 CTL_PAGE_CHANGEABLE], 4134 &control_page_changeable, 4135 sizeof(control_page_changeable)); 4136 memcpy(&lun->mode_pages.control_page[ 4137 CTL_PAGE_SAVED], 4138 &control_page_default, 4139 sizeof(control_page_default)); 4140 control_page = &lun->mode_pages.control_page[ 4141 CTL_PAGE_SAVED]; 4142 value = ctl_get_opt(&lun->be_lun->options, 4143 "reordering"); 4144 if (value != NULL && 4145 strcmp(value, "unrestricted") == 0) { 4146 control_page->queue_flags &= 4147 ~SCP_QUEUE_ALG_MASK; 4148 control_page->queue_flags |= 4149 SCP_QUEUE_ALG_UNRESTRICTED; 4150 } 4151 memcpy(&lun->mode_pages.control_page[ 4152 CTL_PAGE_CURRENT], 4153 &lun->mode_pages.control_page[ 4154 CTL_PAGE_SAVED], 4155 sizeof(control_page_default)); 4156 page_index->page_data = 4157 (uint8_t *)lun->mode_pages.control_page; 4158 break; 4159 } 4160 case 0x01: 4161 memcpy(&lun->mode_pages.control_ext_page[ 4162 CTL_PAGE_DEFAULT], 4163 &control_ext_page_default, 4164 sizeof(control_ext_page_default)); 4165 memcpy(&lun->mode_pages.control_ext_page[ 4166 CTL_PAGE_CHANGEABLE], 4167 &control_ext_page_changeable, 4168 sizeof(control_ext_page_changeable)); 4169 memcpy(&lun->mode_pages.control_ext_page[ 4170 CTL_PAGE_SAVED], 4171 &control_ext_page_default, 4172 sizeof(control_ext_page_default)); 4173 memcpy(&lun->mode_pages.control_ext_page[ 4174 CTL_PAGE_CURRENT], 4175 &lun->mode_pages.control_ext_page[ 4176 CTL_PAGE_SAVED], 4177 sizeof(control_ext_page_default)); 4178 page_index->page_data = 4179 (uint8_t *)lun->mode_pages.control_ext_page; 4180 break; 4181 default: 4182 panic("subpage %#x for page %#x is incorrect!", 4183 page_index->subpage, page_code); 4184 } 4185 break; 4186 } 4187 case SMS_INFO_EXCEPTIONS_PAGE: { 4188 switch (page_index->subpage) { 4189 case SMS_SUBPAGE_PAGE_0: 4190 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4191 &ie_page_default, 4192 sizeof(ie_page_default)); 4193 memcpy(&lun->mode_pages.ie_page[ 4194 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4195 sizeof(ie_page_changeable)); 4196 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4197 &ie_page_default, 4198 sizeof(ie_page_default)); 4199 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4200 &ie_page_default, 4201 sizeof(ie_page_default)); 4202 page_index->page_data = 4203 (uint8_t *)lun->mode_pages.ie_page; 4204 break; 4205 case 0x02: { 4206 struct ctl_logical_block_provisioning_page *page; 4207 4208 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4209 &lbp_page_default, 4210 sizeof(lbp_page_default)); 4211 memcpy(&lun->mode_pages.lbp_page[ 4212 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4213 sizeof(lbp_page_changeable)); 4214 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4215 &lbp_page_default, 4216 sizeof(lbp_page_default)); 4217 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4218 value = ctl_get_opt(&lun->be_lun->options, 4219 "avail-threshold"); 4220 if (value != NULL && 4221 ctl_expand_number(value, &ival) == 0) { 4222 page->descr[0].flags |= SLBPPD_ENABLED | 4223 SLBPPD_ARMING_DEC; 4224 if (lun->be_lun->blocksize) 4225 ival /= lun->be_lun->blocksize; 4226 else 4227 ival /= 512; 4228 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4229 page->descr[0].count); 4230 } 4231 value = ctl_get_opt(&lun->be_lun->options, 4232 "used-threshold"); 4233 if (value != NULL && 4234 ctl_expand_number(value, &ival) == 0) { 4235 page->descr[1].flags |= SLBPPD_ENABLED | 4236 SLBPPD_ARMING_INC; 4237 if (lun->be_lun->blocksize) 4238 ival /= lun->be_lun->blocksize; 4239 else 4240 ival /= 512; 4241 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4242 page->descr[1].count); 4243 } 4244 value = ctl_get_opt(&lun->be_lun->options, 4245 "pool-avail-threshold"); 4246 if (value != NULL && 4247 ctl_expand_number(value, &ival) == 0) { 4248 page->descr[2].flags |= SLBPPD_ENABLED | 4249 SLBPPD_ARMING_DEC; 4250 if (lun->be_lun->blocksize) 4251 ival /= lun->be_lun->blocksize; 4252 else 4253 ival /= 512; 4254 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4255 page->descr[2].count); 4256 } 4257 value = ctl_get_opt(&lun->be_lun->options, 4258 "pool-used-threshold"); 4259 if (value != NULL && 4260 ctl_expand_number(value, &ival) == 0) { 4261 page->descr[3].flags |= SLBPPD_ENABLED | 4262 SLBPPD_ARMING_INC; 4263 if (lun->be_lun->blocksize) 4264 ival /= lun->be_lun->blocksize; 4265 else 4266 ival /= 512; 4267 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4268 page->descr[3].count); 4269 } 4270 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4271 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4272 sizeof(lbp_page_default)); 4273 page_index->page_data = 4274 (uint8_t *)lun->mode_pages.lbp_page; 4275 break; 4276 } 4277 default: 4278 panic("subpage %#x for page %#x is incorrect!", 4279 page_index->subpage, page_code); 4280 } 4281 break; 4282 } 4283 case SMS_CDDVD_CAPS_PAGE:{ 4284 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4285 ("subpage %#x for page %#x is incorrect!", 4286 page_index->subpage, page_code)); 4287 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4288 &cddvd_page_default, 4289 sizeof(cddvd_page_default)); 4290 memcpy(&lun->mode_pages.cddvd_page[ 4291 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4292 sizeof(cddvd_page_changeable)); 4293 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4294 &cddvd_page_default, 4295 sizeof(cddvd_page_default)); 4296 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4297 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4298 sizeof(cddvd_page_default)); 4299 page_index->page_data = 4300 (uint8_t *)lun->mode_pages.cddvd_page; 4301 break; 4302 } 4303 default: 4304 panic("invalid page code value %#x", page_code); 4305 } 4306 } 4307 4308 return (CTL_RETVAL_COMPLETE); 4309} 4310 4311static int 4312ctl_init_log_page_index(struct ctl_lun *lun) 4313{ 4314 struct ctl_page_index *page_index; 4315 int i, j, k, prev; 4316 4317 memcpy(&lun->log_pages.index, log_page_index_template, 4318 sizeof(log_page_index_template)); 4319 4320 prev = -1; 4321 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4322 4323 page_index = &lun->log_pages.index[i]; 4324 if (lun->be_lun->lun_type == T_DIRECT && 4325 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4326 continue; 4327 if (lun->be_lun->lun_type == T_PROCESSOR && 4328 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4329 continue; 4330 if (lun->be_lun->lun_type == T_CDROM && 4331 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4332 continue; 4333 4334 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4335 lun->backend->lun_attr == NULL) 4336 continue; 4337 4338 if (page_index->page_code != prev) { 4339 lun->log_pages.pages_page[j] = page_index->page_code; 4340 prev = page_index->page_code; 4341 j++; 4342 } 4343 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4344 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4345 k++; 4346 } 4347 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4348 lun->log_pages.index[0].page_len = j; 4349 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4350 lun->log_pages.index[1].page_len = k * 2; 4351 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4352 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4353 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4354 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4355 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4356 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4357 4358 return (CTL_RETVAL_COMPLETE); 4359} 4360 4361static int 4362hex2bin(const char *str, uint8_t *buf, int buf_size) 4363{ 4364 int i; 4365 u_char c; 4366 4367 memset(buf, 0, buf_size); 4368 while (isspace(str[0])) 4369 str++; 4370 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4371 str += 2; 4372 buf_size *= 2; 4373 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4374 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4375 str++; 4376 c = str[i]; 4377 if (isdigit(c)) 4378 c -= '0'; 4379 else if (isalpha(c)) 4380 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4381 else 4382 break; 4383 if (c >= 16) 4384 break; 4385 if ((i & 1) == 0) 4386 buf[i / 2] |= (c << 4); 4387 else 4388 buf[i / 2] |= c; 4389 } 4390 return ((i + 1) / 2); 4391} 4392 4393/* 4394 * LUN allocation. 4395 * 4396 * Requirements: 4397 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4398 * wants us to allocate the LUN and he can block. 4399 * - ctl_softc is always set 4400 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4401 * 4402 * Returns 0 for success, non-zero (errno) for failure. 4403 */ 4404static int 4405ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4406 struct ctl_be_lun *const be_lun) 4407{ 4408 struct ctl_lun *nlun, *lun; 4409 struct scsi_vpd_id_descriptor *desc; 4410 struct scsi_vpd_id_t10 *t10id; 4411 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4412 int lun_number, i, lun_malloced; 4413 int devidlen, idlen1, idlen2 = 0, len; 4414 4415 if (be_lun == NULL) 4416 return (EINVAL); 4417 4418 /* 4419 * We currently only support Direct Access or Processor LUN types. 4420 */ 4421 switch (be_lun->lun_type) { 4422 case T_DIRECT: 4423 case T_PROCESSOR: 4424 case T_CDROM: 4425 break; 4426 case T_SEQUENTIAL: 4427 case T_CHANGER: 4428 default: 4429 be_lun->lun_config_status(be_lun->be_lun, 4430 CTL_LUN_CONFIG_FAILURE); 4431 break; 4432 } 4433 if (ctl_lun == NULL) { 4434 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4435 lun_malloced = 1; 4436 } else { 4437 lun_malloced = 0; 4438 lun = ctl_lun; 4439 } 4440 4441 memset(lun, 0, sizeof(*lun)); 4442 if (lun_malloced) 4443 lun->flags = CTL_LUN_MALLOCED; 4444 4445 /* Generate LUN ID. */ 4446 devidlen = max(CTL_DEVID_MIN_LEN, 4447 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4448 idlen1 = sizeof(*t10id) + devidlen; 4449 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4450 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4451 if (scsiname != NULL) { 4452 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4453 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4454 } 4455 eui = ctl_get_opt(&be_lun->options, "eui"); 4456 if (eui != NULL) { 4457 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4458 } 4459 naa = ctl_get_opt(&be_lun->options, "naa"); 4460 if (naa != NULL) { 4461 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4462 } 4463 uuid = ctl_get_opt(&be_lun->options, "uuid"); 4464 if (uuid != NULL) { 4465 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4466 } 4467 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4468 M_CTL, M_WAITOK | M_ZERO); 4469 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4470 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4471 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4472 desc->length = idlen1; 4473 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4474 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4475 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4476 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4477 } else { 4478 strncpy(t10id->vendor, vendor, 4479 min(sizeof(t10id->vendor), strlen(vendor))); 4480 } 4481 strncpy((char *)t10id->vendor_spec_id, 4482 (char *)be_lun->device_id, devidlen); 4483 if (scsiname != NULL) { 4484 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4485 desc->length); 4486 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4487 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4488 SVPD_ID_TYPE_SCSI_NAME; 4489 desc->length = idlen2; 4490 strlcpy(desc->identifier, scsiname, idlen2); 4491 } 4492 if (eui != NULL) { 4493 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4494 desc->length); 4495 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4496 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4497 SVPD_ID_TYPE_EUI64; 4498 desc->length = hex2bin(eui, desc->identifier, 16); 4499 desc->length = desc->length > 12 ? 16 : 4500 (desc->length > 8 ? 12 : 8); 4501 len -= 16 - desc->length; 4502 } 4503 if (naa != NULL) { 4504 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4505 desc->length); 4506 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4507 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4508 SVPD_ID_TYPE_NAA; 4509 desc->length = hex2bin(naa, desc->identifier, 16); 4510 desc->length = desc->length > 8 ? 16 : 8; 4511 len -= 16 - desc->length; 4512 } 4513 if (uuid != NULL) { 4514 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4515 desc->length); 4516 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4517 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4518 SVPD_ID_TYPE_UUID; 4519 desc->identifier[0] = 0x10; 4520 hex2bin(uuid, &desc->identifier[2], 16); 4521 desc->length = 18; 4522 } 4523 lun->lun_devid->len = len; 4524 4525 mtx_lock(&ctl_softc->ctl_lock); 4526 /* 4527 * See if the caller requested a particular LUN number. If so, see 4528 * if it is available. Otherwise, allocate the first available LUN. 4529 */ 4530 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4531 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4532 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4533 mtx_unlock(&ctl_softc->ctl_lock); 4534 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4535 printf("ctl: requested LUN ID %d is higher " 4536 "than CTL_MAX_LUNS - 1 (%d)\n", 4537 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4538 } else { 4539 /* 4540 * XXX KDM return an error, or just assign 4541 * another LUN ID in this case?? 4542 */ 4543 printf("ctl: requested LUN ID %d is already " 4544 "in use\n", be_lun->req_lun_id); 4545 } 4546 if (lun->flags & CTL_LUN_MALLOCED) 4547 free(lun, M_CTL); 4548 be_lun->lun_config_status(be_lun->be_lun, 4549 CTL_LUN_CONFIG_FAILURE); 4550 return (ENOSPC); 4551 } 4552 lun_number = be_lun->req_lun_id; 4553 } else { 4554 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4555 if (lun_number == -1) { 4556 mtx_unlock(&ctl_softc->ctl_lock); 4557 printf("ctl: can't allocate LUN, out of LUNs\n"); 4558 if (lun->flags & CTL_LUN_MALLOCED) 4559 free(lun, M_CTL); 4560 be_lun->lun_config_status(be_lun->be_lun, 4561 CTL_LUN_CONFIG_FAILURE); 4562 return (ENOSPC); 4563 } 4564 } 4565 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4566 4567 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4568 lun->lun = lun_number; 4569 lun->be_lun = be_lun; 4570 /* 4571 * The processor LUN is always enabled. Disk LUNs come on line 4572 * disabled, and must be enabled by the backend. 4573 */ 4574 lun->flags |= CTL_LUN_DISABLED; 4575 lun->backend = be_lun->be; 4576 be_lun->ctl_lun = lun; 4577 be_lun->lun_id = lun_number; 4578 atomic_add_int(&be_lun->be->num_luns, 1); 4579 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4580 lun->flags |= CTL_LUN_EJECTED; 4581 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4582 lun->flags |= CTL_LUN_NO_MEDIA; 4583 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4584 lun->flags |= CTL_LUN_STOPPED; 4585 4586 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4587 lun->flags |= CTL_LUN_PRIMARY_SC; 4588 4589 value = ctl_get_opt(&be_lun->options, "removable"); 4590 if (value != NULL) { 4591 if (strcmp(value, "on") == 0) 4592 lun->flags |= CTL_LUN_REMOVABLE; 4593 } else if (be_lun->lun_type == T_CDROM) 4594 lun->flags |= CTL_LUN_REMOVABLE; 4595 4596 lun->ctl_softc = ctl_softc; 4597#ifdef CTL_TIME_IO 4598 lun->last_busy = getsbinuptime(); 4599#endif 4600 TAILQ_INIT(&lun->ooa_queue); 4601 TAILQ_INIT(&lun->blocked_queue); 4602 STAILQ_INIT(&lun->error_list); 4603 lun->ie_reported = 1; 4604 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4605 ctl_tpc_lun_init(lun); 4606 4607 /* 4608 * Initialize the mode and log page index. 4609 */ 4610 ctl_init_page_index(lun); 4611 ctl_init_log_page_index(lun); 4612 4613 /* 4614 * Now, before we insert this lun on the lun list, set the lun 4615 * inventory changed UA for all other luns. 4616 */ 4617 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4618 mtx_lock(&nlun->lun_lock); 4619 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4620 mtx_unlock(&nlun->lun_lock); 4621 } 4622 4623 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4624 4625 ctl_softc->ctl_luns[lun_number] = lun; 4626 4627 ctl_softc->num_luns++; 4628 4629 /* Setup statistics gathering */ 4630 lun->stats.device_type = be_lun->lun_type; 4631 lun->stats.lun_number = lun_number; 4632 lun->stats.blocksize = be_lun->blocksize; 4633 if (be_lun->blocksize == 0) 4634 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4635 for (i = 0;i < CTL_MAX_PORTS;i++) 4636 lun->stats.ports[i].targ_port = i; 4637 4638 mtx_unlock(&ctl_softc->ctl_lock); 4639 4640 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4641 return (0); 4642} 4643 4644/* 4645 * Delete a LUN. 4646 * Assumptions: 4647 * - LUN has already been marked invalid and any pending I/O has been taken 4648 * care of. 4649 */ 4650static int 4651ctl_free_lun(struct ctl_lun *lun) 4652{ 4653 struct ctl_softc *softc; 4654 struct ctl_lun *nlun; 4655 int i; 4656 4657 softc = lun->ctl_softc; 4658 4659 mtx_assert(&softc->ctl_lock, MA_OWNED); 4660 4661 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4662 4663 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4664 4665 softc->ctl_luns[lun->lun] = NULL; 4666 4667 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4668 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4669 4670 softc->num_luns--; 4671 4672 /* 4673 * Tell the backend to free resources, if this LUN has a backend. 4674 */ 4675 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4676 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4677 4678 lun->ie_reportcnt = UINT32_MAX; 4679 callout_drain(&lun->ie_callout); 4680 4681 ctl_tpc_lun_shutdown(lun); 4682 mtx_destroy(&lun->lun_lock); 4683 free(lun->lun_devid, M_CTL); 4684 for (i = 0; i < CTL_MAX_PORTS; i++) 4685 free(lun->pending_ua[i], M_CTL); 4686 for (i = 0; i < CTL_MAX_PORTS; i++) 4687 free(lun->pr_keys[i], M_CTL); 4688 free(lun->write_buffer, M_CTL); 4689 if (lun->flags & CTL_LUN_MALLOCED) 4690 free(lun, M_CTL); 4691 4692 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4693 mtx_lock(&nlun->lun_lock); 4694 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4695 mtx_unlock(&nlun->lun_lock); 4696 } 4697 4698 return (0); 4699} 4700 4701static void 4702ctl_create_lun(struct ctl_be_lun *be_lun) 4703{ 4704 4705 /* 4706 * ctl_alloc_lun() should handle all potential failure cases. 4707 */ 4708 ctl_alloc_lun(control_softc, NULL, be_lun); 4709} 4710 4711int 4712ctl_add_lun(struct ctl_be_lun *be_lun) 4713{ 4714 struct ctl_softc *softc = control_softc; 4715 4716 mtx_lock(&softc->ctl_lock); 4717 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4718 mtx_unlock(&softc->ctl_lock); 4719 wakeup(&softc->pending_lun_queue); 4720 4721 return (0); 4722} 4723 4724int 4725ctl_enable_lun(struct ctl_be_lun *be_lun) 4726{ 4727 struct ctl_softc *softc; 4728 struct ctl_port *port, *nport; 4729 struct ctl_lun *lun; 4730 int retval; 4731 4732 lun = (struct ctl_lun *)be_lun->ctl_lun; 4733 softc = lun->ctl_softc; 4734 4735 mtx_lock(&softc->ctl_lock); 4736 mtx_lock(&lun->lun_lock); 4737 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4738 /* 4739 * eh? Why did we get called if the LUN is already 4740 * enabled? 4741 */ 4742 mtx_unlock(&lun->lun_lock); 4743 mtx_unlock(&softc->ctl_lock); 4744 return (0); 4745 } 4746 lun->flags &= ~CTL_LUN_DISABLED; 4747 mtx_unlock(&lun->lun_lock); 4748 4749 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4750 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4751 port->lun_map != NULL || port->lun_enable == NULL) 4752 continue; 4753 4754 /* 4755 * Drop the lock while we call the FETD's enable routine. 4756 * This can lead to a callback into CTL (at least in the 4757 * case of the internal initiator frontend. 4758 */ 4759 mtx_unlock(&softc->ctl_lock); 4760 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4761 mtx_lock(&softc->ctl_lock); 4762 if (retval != 0) { 4763 printf("%s: FETD %s port %d returned error " 4764 "%d for lun_enable on lun %jd\n", 4765 __func__, port->port_name, port->targ_port, 4766 retval, (intmax_t)lun->lun); 4767 } 4768 } 4769 4770 mtx_unlock(&softc->ctl_lock); 4771 ctl_isc_announce_lun(lun); 4772 4773 return (0); 4774} 4775 4776int 4777ctl_disable_lun(struct ctl_be_lun *be_lun) 4778{ 4779 struct ctl_softc *softc; 4780 struct ctl_port *port; 4781 struct ctl_lun *lun; 4782 int retval; 4783 4784 lun = (struct ctl_lun *)be_lun->ctl_lun; 4785 softc = lun->ctl_softc; 4786 4787 mtx_lock(&softc->ctl_lock); 4788 mtx_lock(&lun->lun_lock); 4789 if (lun->flags & CTL_LUN_DISABLED) { 4790 mtx_unlock(&lun->lun_lock); 4791 mtx_unlock(&softc->ctl_lock); 4792 return (0); 4793 } 4794 lun->flags |= CTL_LUN_DISABLED; 4795 mtx_unlock(&lun->lun_lock); 4796 4797 STAILQ_FOREACH(port, &softc->port_list, links) { 4798 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4799 port->lun_map != NULL || port->lun_disable == NULL) 4800 continue; 4801 4802 /* 4803 * Drop the lock before we call the frontend's disable 4804 * routine, to avoid lock order reversals. 4805 * 4806 * XXX KDM what happens if the frontend list changes while 4807 * we're traversing it? It's unlikely, but should be handled. 4808 */ 4809 mtx_unlock(&softc->ctl_lock); 4810 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4811 mtx_lock(&softc->ctl_lock); 4812 if (retval != 0) { 4813 printf("%s: FETD %s port %d returned error " 4814 "%d for lun_disable on lun %jd\n", 4815 __func__, port->port_name, port->targ_port, 4816 retval, (intmax_t)lun->lun); 4817 } 4818 } 4819 4820 mtx_unlock(&softc->ctl_lock); 4821 ctl_isc_announce_lun(lun); 4822 4823 return (0); 4824} 4825 4826int 4827ctl_start_lun(struct ctl_be_lun *be_lun) 4828{ 4829 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4830 4831 mtx_lock(&lun->lun_lock); 4832 lun->flags &= ~CTL_LUN_STOPPED; 4833 mtx_unlock(&lun->lun_lock); 4834 return (0); 4835} 4836 4837int 4838ctl_stop_lun(struct ctl_be_lun *be_lun) 4839{ 4840 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4841 4842 mtx_lock(&lun->lun_lock); 4843 lun->flags |= CTL_LUN_STOPPED; 4844 mtx_unlock(&lun->lun_lock); 4845 return (0); 4846} 4847 4848int 4849ctl_lun_no_media(struct ctl_be_lun *be_lun) 4850{ 4851 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4852 4853 mtx_lock(&lun->lun_lock); 4854 lun->flags |= CTL_LUN_NO_MEDIA; 4855 mtx_unlock(&lun->lun_lock); 4856 return (0); 4857} 4858 4859int 4860ctl_lun_has_media(struct ctl_be_lun *be_lun) 4861{ 4862 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4863 union ctl_ha_msg msg; 4864 4865 mtx_lock(&lun->lun_lock); 4866 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4867 if (lun->flags & CTL_LUN_REMOVABLE) 4868 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4869 mtx_unlock(&lun->lun_lock); 4870 if ((lun->flags & CTL_LUN_REMOVABLE) && 4871 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4872 bzero(&msg.ua, sizeof(msg.ua)); 4873 msg.hdr.msg_type = CTL_MSG_UA; 4874 msg.hdr.nexus.initid = -1; 4875 msg.hdr.nexus.targ_port = -1; 4876 msg.hdr.nexus.targ_lun = lun->lun; 4877 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4878 msg.ua.ua_all = 1; 4879 msg.ua.ua_set = 1; 4880 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4881 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4882 M_WAITOK); 4883 } 4884 return (0); 4885} 4886 4887int 4888ctl_lun_ejected(struct ctl_be_lun *be_lun) 4889{ 4890 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4891 4892 mtx_lock(&lun->lun_lock); 4893 lun->flags |= CTL_LUN_EJECTED; 4894 mtx_unlock(&lun->lun_lock); 4895 return (0); 4896} 4897 4898int 4899ctl_lun_primary(struct ctl_be_lun *be_lun) 4900{ 4901 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4902 4903 mtx_lock(&lun->lun_lock); 4904 lun->flags |= CTL_LUN_PRIMARY_SC; 4905 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4906 mtx_unlock(&lun->lun_lock); 4907 ctl_isc_announce_lun(lun); 4908 return (0); 4909} 4910 4911int 4912ctl_lun_secondary(struct ctl_be_lun *be_lun) 4913{ 4914 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4915 4916 mtx_lock(&lun->lun_lock); 4917 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4918 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4919 mtx_unlock(&lun->lun_lock); 4920 ctl_isc_announce_lun(lun); 4921 return (0); 4922} 4923 4924int 4925ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4926{ 4927 struct ctl_softc *softc; 4928 struct ctl_lun *lun; 4929 4930 lun = (struct ctl_lun *)be_lun->ctl_lun; 4931 softc = lun->ctl_softc; 4932 4933 mtx_lock(&lun->lun_lock); 4934 4935 /* 4936 * The LUN needs to be disabled before it can be marked invalid. 4937 */ 4938 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4939 mtx_unlock(&lun->lun_lock); 4940 return (-1); 4941 } 4942 /* 4943 * Mark the LUN invalid. 4944 */ 4945 lun->flags |= CTL_LUN_INVALID; 4946 4947 /* 4948 * If there is nothing in the OOA queue, go ahead and free the LUN. 4949 * If we have something in the OOA queue, we'll free it when the 4950 * last I/O completes. 4951 */ 4952 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4953 mtx_unlock(&lun->lun_lock); 4954 mtx_lock(&softc->ctl_lock); 4955 ctl_free_lun(lun); 4956 mtx_unlock(&softc->ctl_lock); 4957 } else 4958 mtx_unlock(&lun->lun_lock); 4959 4960 return (0); 4961} 4962 4963void 4964ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4965{ 4966 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4967 union ctl_ha_msg msg; 4968 4969 mtx_lock(&lun->lun_lock); 4970 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4971 mtx_unlock(&lun->lun_lock); 4972 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4973 /* Send msg to other side. */ 4974 bzero(&msg.ua, sizeof(msg.ua)); 4975 msg.hdr.msg_type = CTL_MSG_UA; 4976 msg.hdr.nexus.initid = -1; 4977 msg.hdr.nexus.targ_port = -1; 4978 msg.hdr.nexus.targ_lun = lun->lun; 4979 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4980 msg.ua.ua_all = 1; 4981 msg.ua.ua_set = 1; 4982 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4983 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4984 M_WAITOK); 4985 } 4986} 4987 4988/* 4989 * Backend "memory move is complete" callback for requests that never 4990 * make it down to say RAIDCore's configuration code. 4991 */ 4992int 4993ctl_config_move_done(union ctl_io *io) 4994{ 4995 int retval; 4996 4997 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4998 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4999 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5000 5001 if ((io->io_hdr.port_status != 0) && 5002 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5003 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5004 /* 5005 * For hardware error sense keys, the sense key 5006 * specific value is defined to be a retry count, 5007 * but we use it to pass back an internal FETD 5008 * error code. XXX KDM Hopefully the FETD is only 5009 * using 16 bits for an error code, since that's 5010 * all the space we have in the sks field. 5011 */ 5012 ctl_set_internal_failure(&io->scsiio, 5013 /*sks_valid*/ 1, 5014 /*retry_count*/ 5015 io->io_hdr.port_status); 5016 } 5017 5018 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5019 ctl_data_print(io); 5020 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5021 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5022 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5023 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5024 /* 5025 * XXX KDM just assuming a single pointer here, and not a 5026 * S/G list. If we start using S/G lists for config data, 5027 * we'll need to know how to clean them up here as well. 5028 */ 5029 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5030 free(io->scsiio.kern_data_ptr, M_CTL); 5031 ctl_done(io); 5032 retval = CTL_RETVAL_COMPLETE; 5033 } else { 5034 /* 5035 * XXX KDM now we need to continue data movement. Some 5036 * options: 5037 * - call ctl_scsiio() again? We don't do this for data 5038 * writes, because for those at least we know ahead of 5039 * time where the write will go and how long it is. For 5040 * config writes, though, that information is largely 5041 * contained within the write itself, thus we need to 5042 * parse out the data again. 5043 * 5044 * - Call some other function once the data is in? 5045 */ 5046 5047 /* 5048 * XXX KDM call ctl_scsiio() again for now, and check flag 5049 * bits to see whether we're allocated or not. 5050 */ 5051 retval = ctl_scsiio(&io->scsiio); 5052 } 5053 return (retval); 5054} 5055 5056/* 5057 * This gets called by a backend driver when it is done with a 5058 * data_submit method. 5059 */ 5060void 5061ctl_data_submit_done(union ctl_io *io) 5062{ 5063 /* 5064 * If the IO_CONT flag is set, we need to call the supplied 5065 * function to continue processing the I/O, instead of completing 5066 * the I/O just yet. 5067 * 5068 * If there is an error, though, we don't want to keep processing. 5069 * Instead, just send status back to the initiator. 5070 */ 5071 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5072 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5073 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5074 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5075 io->scsiio.io_cont(io); 5076 return; 5077 } 5078 ctl_done(io); 5079} 5080 5081/* 5082 * This gets called by a backend driver when it is done with a 5083 * configuration write. 5084 */ 5085void 5086ctl_config_write_done(union ctl_io *io) 5087{ 5088 uint8_t *buf; 5089 5090 /* 5091 * If the IO_CONT flag is set, we need to call the supplied 5092 * function to continue processing the I/O, instead of completing 5093 * the I/O just yet. 5094 * 5095 * If there is an error, though, we don't want to keep processing. 5096 * Instead, just send status back to the initiator. 5097 */ 5098 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5099 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5100 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5101 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5102 io->scsiio.io_cont(io); 5103 return; 5104 } 5105 /* 5106 * Since a configuration write can be done for commands that actually 5107 * have data allocated, like write buffer, and commands that have 5108 * no data, like start/stop unit, we need to check here. 5109 */ 5110 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5111 buf = io->scsiio.kern_data_ptr; 5112 else 5113 buf = NULL; 5114 ctl_done(io); 5115 if (buf) 5116 free(buf, M_CTL); 5117} 5118 5119void 5120ctl_config_read_done(union ctl_io *io) 5121{ 5122 uint8_t *buf; 5123 5124 /* 5125 * If there is some error -- we are done, skip data transfer. 5126 */ 5127 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5128 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5129 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5130 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5131 buf = io->scsiio.kern_data_ptr; 5132 else 5133 buf = NULL; 5134 ctl_done(io); 5135 if (buf) 5136 free(buf, M_CTL); 5137 return; 5138 } 5139 5140 /* 5141 * If the IO_CONT flag is set, we need to call the supplied 5142 * function to continue processing the I/O, instead of completing 5143 * the I/O just yet. 5144 */ 5145 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5146 io->scsiio.io_cont(io); 5147 return; 5148 } 5149 5150 ctl_datamove(io); 5151} 5152 5153/* 5154 * SCSI release command. 5155 */ 5156int 5157ctl_scsi_release(struct ctl_scsiio *ctsio) 5158{ 5159 struct ctl_lun *lun; 5160 uint32_t residx; 5161 5162 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5163 5164 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5165 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5166 5167 /* 5168 * XXX KDM right now, we only support LUN reservation. We don't 5169 * support 3rd party reservations, or extent reservations, which 5170 * might actually need the parameter list. If we've gotten this 5171 * far, we've got a LUN reservation. Anything else got kicked out 5172 * above. So, according to SPC, ignore the length. 5173 */ 5174 5175 mtx_lock(&lun->lun_lock); 5176 5177 /* 5178 * According to SPC, it is not an error for an intiator to attempt 5179 * to release a reservation on a LUN that isn't reserved, or that 5180 * is reserved by another initiator. The reservation can only be 5181 * released, though, by the initiator who made it or by one of 5182 * several reset type events. 5183 */ 5184 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5185 lun->flags &= ~CTL_LUN_RESERVED; 5186 5187 mtx_unlock(&lun->lun_lock); 5188 5189 ctl_set_success(ctsio); 5190 ctl_done((union ctl_io *)ctsio); 5191 return (CTL_RETVAL_COMPLETE); 5192} 5193 5194int 5195ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5196{ 5197 struct ctl_lun *lun; 5198 uint32_t residx; 5199 5200 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5201 5202 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5203 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5204 5205 /* 5206 * XXX KDM right now, we only support LUN reservation. We don't 5207 * support 3rd party reservations, or extent reservations, which 5208 * might actually need the parameter list. If we've gotten this 5209 * far, we've got a LUN reservation. Anything else got kicked out 5210 * above. So, according to SPC, ignore the length. 5211 */ 5212 5213 mtx_lock(&lun->lun_lock); 5214 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5215 ctl_set_reservation_conflict(ctsio); 5216 goto bailout; 5217 } 5218 5219 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5220 if (lun->flags & CTL_LUN_PR_RESERVED) { 5221 ctl_set_success(ctsio); 5222 goto bailout; 5223 } 5224 5225 lun->flags |= CTL_LUN_RESERVED; 5226 lun->res_idx = residx; 5227 ctl_set_success(ctsio); 5228 5229bailout: 5230 mtx_unlock(&lun->lun_lock); 5231 ctl_done((union ctl_io *)ctsio); 5232 return (CTL_RETVAL_COMPLETE); 5233} 5234 5235int 5236ctl_start_stop(struct ctl_scsiio *ctsio) 5237{ 5238 struct scsi_start_stop_unit *cdb; 5239 struct ctl_lun *lun; 5240 int retval; 5241 5242 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5243 5244 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5245 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5246 5247 if ((cdb->how & SSS_PC_MASK) == 0) { 5248 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5249 (cdb->how & SSS_START) == 0) { 5250 uint32_t residx; 5251 5252 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5253 if (ctl_get_prkey(lun, residx) == 0 || 5254 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5255 5256 ctl_set_reservation_conflict(ctsio); 5257 ctl_done((union ctl_io *)ctsio); 5258 return (CTL_RETVAL_COMPLETE); 5259 } 5260 } 5261 5262 if ((cdb->how & SSS_LOEJ) && 5263 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5264 ctl_set_invalid_field(ctsio, 5265 /*sks_valid*/ 1, 5266 /*command*/ 1, 5267 /*field*/ 4, 5268 /*bit_valid*/ 1, 5269 /*bit*/ 1); 5270 ctl_done((union ctl_io *)ctsio); 5271 return (CTL_RETVAL_COMPLETE); 5272 } 5273 5274 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5275 lun->prevent_count > 0) { 5276 /* "Medium removal prevented" */ 5277 ctl_set_sense(ctsio, /*current_error*/ 1, 5278 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5279 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5280 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5281 ctl_done((union ctl_io *)ctsio); 5282 return (CTL_RETVAL_COMPLETE); 5283 } 5284 } 5285 5286 retval = lun->backend->config_write((union ctl_io *)ctsio); 5287 return (retval); 5288} 5289 5290int 5291ctl_prevent_allow(struct ctl_scsiio *ctsio) 5292{ 5293 struct ctl_lun *lun; 5294 struct scsi_prevent *cdb; 5295 int retval; 5296 uint32_t initidx; 5297 5298 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5299 5300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5301 cdb = (struct scsi_prevent *)ctsio->cdb; 5302 5303 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5304 ctl_set_invalid_opcode(ctsio); 5305 ctl_done((union ctl_io *)ctsio); 5306 return (CTL_RETVAL_COMPLETE); 5307 } 5308 5309 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5310 mtx_lock(&lun->lun_lock); 5311 if ((cdb->how & PR_PREVENT) && 5312 ctl_is_set(lun->prevent, initidx) == 0) { 5313 ctl_set_mask(lun->prevent, initidx); 5314 lun->prevent_count++; 5315 } else if ((cdb->how & PR_PREVENT) == 0 && 5316 ctl_is_set(lun->prevent, initidx)) { 5317 ctl_clear_mask(lun->prevent, initidx); 5318 lun->prevent_count--; 5319 } 5320 mtx_unlock(&lun->lun_lock); 5321 retval = lun->backend->config_write((union ctl_io *)ctsio); 5322 return (retval); 5323} 5324 5325/* 5326 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5327 * we don't really do anything with the LBA and length fields if the user 5328 * passes them in. Instead we'll just flush out the cache for the entire 5329 * LUN. 5330 */ 5331int 5332ctl_sync_cache(struct ctl_scsiio *ctsio) 5333{ 5334 struct ctl_lun *lun; 5335 struct ctl_softc *softc; 5336 struct ctl_lba_len_flags *lbalen; 5337 uint64_t starting_lba; 5338 uint32_t block_count; 5339 int retval; 5340 uint8_t byte2; 5341 5342 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5343 5344 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5345 softc = lun->ctl_softc; 5346 retval = 0; 5347 5348 switch (ctsio->cdb[0]) { 5349 case SYNCHRONIZE_CACHE: { 5350 struct scsi_sync_cache *cdb; 5351 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5352 5353 starting_lba = scsi_4btoul(cdb->begin_lba); 5354 block_count = scsi_2btoul(cdb->lb_count); 5355 byte2 = cdb->byte2; 5356 break; 5357 } 5358 case SYNCHRONIZE_CACHE_16: { 5359 struct scsi_sync_cache_16 *cdb; 5360 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5361 5362 starting_lba = scsi_8btou64(cdb->begin_lba); 5363 block_count = scsi_4btoul(cdb->lb_count); 5364 byte2 = cdb->byte2; 5365 break; 5366 } 5367 default: 5368 ctl_set_invalid_opcode(ctsio); 5369 ctl_done((union ctl_io *)ctsio); 5370 goto bailout; 5371 break; /* NOTREACHED */ 5372 } 5373 5374 /* 5375 * We check the LBA and length, but don't do anything with them. 5376 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5377 * get flushed. This check will just help satisfy anyone who wants 5378 * to see an error for an out of range LBA. 5379 */ 5380 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5381 ctl_set_lba_out_of_range(ctsio, 5382 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5383 ctl_done((union ctl_io *)ctsio); 5384 goto bailout; 5385 } 5386 5387 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5388 lbalen->lba = starting_lba; 5389 lbalen->len = block_count; 5390 lbalen->flags = byte2; 5391 retval = lun->backend->config_write((union ctl_io *)ctsio); 5392 5393bailout: 5394 return (retval); 5395} 5396 5397int 5398ctl_format(struct ctl_scsiio *ctsio) 5399{ 5400 struct scsi_format *cdb; 5401 struct ctl_lun *lun; 5402 int length, defect_list_len; 5403 5404 CTL_DEBUG_PRINT(("ctl_format\n")); 5405 5406 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5407 5408 cdb = (struct scsi_format *)ctsio->cdb; 5409 5410 length = 0; 5411 if (cdb->byte2 & SF_FMTDATA) { 5412 if (cdb->byte2 & SF_LONGLIST) 5413 length = sizeof(struct scsi_format_header_long); 5414 else 5415 length = sizeof(struct scsi_format_header_short); 5416 } 5417 5418 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5419 && (length > 0)) { 5420 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5421 ctsio->kern_data_len = length; 5422 ctsio->kern_total_len = length; 5423 ctsio->kern_data_resid = 0; 5424 ctsio->kern_rel_offset = 0; 5425 ctsio->kern_sg_entries = 0; 5426 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5427 ctsio->be_move_done = ctl_config_move_done; 5428 ctl_datamove((union ctl_io *)ctsio); 5429 5430 return (CTL_RETVAL_COMPLETE); 5431 } 5432 5433 defect_list_len = 0; 5434 5435 if (cdb->byte2 & SF_FMTDATA) { 5436 if (cdb->byte2 & SF_LONGLIST) { 5437 struct scsi_format_header_long *header; 5438 5439 header = (struct scsi_format_header_long *) 5440 ctsio->kern_data_ptr; 5441 5442 defect_list_len = scsi_4btoul(header->defect_list_len); 5443 if (defect_list_len != 0) { 5444 ctl_set_invalid_field(ctsio, 5445 /*sks_valid*/ 1, 5446 /*command*/ 0, 5447 /*field*/ 2, 5448 /*bit_valid*/ 0, 5449 /*bit*/ 0); 5450 goto bailout; 5451 } 5452 } else { 5453 struct scsi_format_header_short *header; 5454 5455 header = (struct scsi_format_header_short *) 5456 ctsio->kern_data_ptr; 5457 5458 defect_list_len = scsi_2btoul(header->defect_list_len); 5459 if (defect_list_len != 0) { 5460 ctl_set_invalid_field(ctsio, 5461 /*sks_valid*/ 1, 5462 /*command*/ 0, 5463 /*field*/ 2, 5464 /*bit_valid*/ 0, 5465 /*bit*/ 0); 5466 goto bailout; 5467 } 5468 } 5469 } 5470 5471 ctl_set_success(ctsio); 5472bailout: 5473 5474 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5475 free(ctsio->kern_data_ptr, M_CTL); 5476 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5477 } 5478 5479 ctl_done((union ctl_io *)ctsio); 5480 return (CTL_RETVAL_COMPLETE); 5481} 5482 5483int 5484ctl_read_buffer(struct ctl_scsiio *ctsio) 5485{ 5486 struct ctl_lun *lun; 5487 uint64_t buffer_offset; 5488 uint32_t len; 5489 uint8_t byte2; 5490 static uint8_t descr[4]; 5491 static uint8_t echo_descr[4] = { 0 }; 5492 5493 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5494 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5495 switch (ctsio->cdb[0]) { 5496 case READ_BUFFER: { 5497 struct scsi_read_buffer *cdb; 5498 5499 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5500 buffer_offset = scsi_3btoul(cdb->offset); 5501 len = scsi_3btoul(cdb->length); 5502 byte2 = cdb->byte2; 5503 break; 5504 } 5505 case READ_BUFFER_16: { 5506 struct scsi_read_buffer_16 *cdb; 5507 5508 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5509 buffer_offset = scsi_8btou64(cdb->offset); 5510 len = scsi_4btoul(cdb->length); 5511 byte2 = cdb->byte2; 5512 break; 5513 } 5514 default: /* This shouldn't happen. */ 5515 ctl_set_invalid_opcode(ctsio); 5516 ctl_done((union ctl_io *)ctsio); 5517 return (CTL_RETVAL_COMPLETE); 5518 } 5519 5520 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5521 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5522 ctl_set_invalid_field(ctsio, 5523 /*sks_valid*/ 1, 5524 /*command*/ 1, 5525 /*field*/ 6, 5526 /*bit_valid*/ 0, 5527 /*bit*/ 0); 5528 ctl_done((union ctl_io *)ctsio); 5529 return (CTL_RETVAL_COMPLETE); 5530 } 5531 5532 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5533 descr[0] = 0; 5534 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5535 ctsio->kern_data_ptr = descr; 5536 len = min(len, sizeof(descr)); 5537 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5538 ctsio->kern_data_ptr = echo_descr; 5539 len = min(len, sizeof(echo_descr)); 5540 } else { 5541 if (lun->write_buffer == NULL) { 5542 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5543 M_CTL, M_WAITOK); 5544 } 5545 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5546 } 5547 ctsio->kern_data_len = len; 5548 ctsio->kern_total_len = len; 5549 ctsio->kern_data_resid = 0; 5550 ctsio->kern_rel_offset = 0; 5551 ctsio->kern_sg_entries = 0; 5552 ctl_set_success(ctsio); 5553 ctsio->be_move_done = ctl_config_move_done; 5554 ctl_datamove((union ctl_io *)ctsio); 5555 return (CTL_RETVAL_COMPLETE); 5556} 5557 5558int 5559ctl_write_buffer(struct ctl_scsiio *ctsio) 5560{ 5561 struct scsi_write_buffer *cdb; 5562 struct ctl_lun *lun; 5563 int buffer_offset, len; 5564 5565 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5566 5567 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5568 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5569 5570 len = scsi_3btoul(cdb->length); 5571 buffer_offset = scsi_3btoul(cdb->offset); 5572 5573 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5574 ctl_set_invalid_field(ctsio, 5575 /*sks_valid*/ 1, 5576 /*command*/ 1, 5577 /*field*/ 6, 5578 /*bit_valid*/ 0, 5579 /*bit*/ 0); 5580 ctl_done((union ctl_io *)ctsio); 5581 return (CTL_RETVAL_COMPLETE); 5582 } 5583 5584 /* 5585 * If we've got a kernel request that hasn't been malloced yet, 5586 * malloc it and tell the caller the data buffer is here. 5587 */ 5588 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5589 if (lun->write_buffer == NULL) { 5590 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5591 M_CTL, M_WAITOK); 5592 } 5593 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5594 ctsio->kern_data_len = len; 5595 ctsio->kern_total_len = len; 5596 ctsio->kern_data_resid = 0; 5597 ctsio->kern_rel_offset = 0; 5598 ctsio->kern_sg_entries = 0; 5599 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5600 ctsio->be_move_done = ctl_config_move_done; 5601 ctl_datamove((union ctl_io *)ctsio); 5602 5603 return (CTL_RETVAL_COMPLETE); 5604 } 5605 5606 ctl_set_success(ctsio); 5607 ctl_done((union ctl_io *)ctsio); 5608 return (CTL_RETVAL_COMPLETE); 5609} 5610 5611int 5612ctl_write_same(struct ctl_scsiio *ctsio) 5613{ 5614 struct ctl_lun *lun; 5615 struct ctl_lba_len_flags *lbalen; 5616 uint64_t lba; 5617 uint32_t num_blocks; 5618 int len, retval; 5619 uint8_t byte2; 5620 5621 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5622 5623 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5624 5625 switch (ctsio->cdb[0]) { 5626 case WRITE_SAME_10: { 5627 struct scsi_write_same_10 *cdb; 5628 5629 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5630 5631 lba = scsi_4btoul(cdb->addr); 5632 num_blocks = scsi_2btoul(cdb->length); 5633 byte2 = cdb->byte2; 5634 break; 5635 } 5636 case WRITE_SAME_16: { 5637 struct scsi_write_same_16 *cdb; 5638 5639 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5640 5641 lba = scsi_8btou64(cdb->addr); 5642 num_blocks = scsi_4btoul(cdb->length); 5643 byte2 = cdb->byte2; 5644 break; 5645 } 5646 default: 5647 /* 5648 * We got a command we don't support. This shouldn't 5649 * happen, commands should be filtered out above us. 5650 */ 5651 ctl_set_invalid_opcode(ctsio); 5652 ctl_done((union ctl_io *)ctsio); 5653 5654 return (CTL_RETVAL_COMPLETE); 5655 break; /* NOTREACHED */ 5656 } 5657 5658 /* ANCHOR flag can be used only together with UNMAP */ 5659 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5660 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5661 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5662 ctl_done((union ctl_io *)ctsio); 5663 return (CTL_RETVAL_COMPLETE); 5664 } 5665 5666 /* 5667 * The first check is to make sure we're in bounds, the second 5668 * check is to catch wrap-around problems. If the lba + num blocks 5669 * is less than the lba, then we've wrapped around and the block 5670 * range is invalid anyway. 5671 */ 5672 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5673 || ((lba + num_blocks) < lba)) { 5674 ctl_set_lba_out_of_range(ctsio, 5675 MAX(lba, lun->be_lun->maxlba + 1)); 5676 ctl_done((union ctl_io *)ctsio); 5677 return (CTL_RETVAL_COMPLETE); 5678 } 5679 5680 /* Zero number of blocks means "to the last logical block" */ 5681 if (num_blocks == 0) { 5682 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5683 ctl_set_invalid_field(ctsio, 5684 /*sks_valid*/ 0, 5685 /*command*/ 1, 5686 /*field*/ 0, 5687 /*bit_valid*/ 0, 5688 /*bit*/ 0); 5689 ctl_done((union ctl_io *)ctsio); 5690 return (CTL_RETVAL_COMPLETE); 5691 } 5692 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5693 } 5694 5695 len = lun->be_lun->blocksize; 5696 5697 /* 5698 * If we've got a kernel request that hasn't been malloced yet, 5699 * malloc it and tell the caller the data buffer is here. 5700 */ 5701 if ((byte2 & SWS_NDOB) == 0 && 5702 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5703 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5704 ctsio->kern_data_len = len; 5705 ctsio->kern_total_len = len; 5706 ctsio->kern_data_resid = 0; 5707 ctsio->kern_rel_offset = 0; 5708 ctsio->kern_sg_entries = 0; 5709 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5710 ctsio->be_move_done = ctl_config_move_done; 5711 ctl_datamove((union ctl_io *)ctsio); 5712 5713 return (CTL_RETVAL_COMPLETE); 5714 } 5715 5716 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5717 lbalen->lba = lba; 5718 lbalen->len = num_blocks; 5719 lbalen->flags = byte2; 5720 retval = lun->backend->config_write((union ctl_io *)ctsio); 5721 5722 return (retval); 5723} 5724 5725int 5726ctl_unmap(struct ctl_scsiio *ctsio) 5727{ 5728 struct ctl_lun *lun; 5729 struct scsi_unmap *cdb; 5730 struct ctl_ptr_len_flags *ptrlen; 5731 struct scsi_unmap_header *hdr; 5732 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5733 uint64_t lba; 5734 uint32_t num_blocks; 5735 int len, retval; 5736 uint8_t byte2; 5737 5738 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5739 5740 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5741 cdb = (struct scsi_unmap *)ctsio->cdb; 5742 5743 len = scsi_2btoul(cdb->length); 5744 byte2 = cdb->byte2; 5745 5746 /* 5747 * If we've got a kernel request that hasn't been malloced yet, 5748 * malloc it and tell the caller the data buffer is here. 5749 */ 5750 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5751 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5752 ctsio->kern_data_len = len; 5753 ctsio->kern_total_len = len; 5754 ctsio->kern_data_resid = 0; 5755 ctsio->kern_rel_offset = 0; 5756 ctsio->kern_sg_entries = 0; 5757 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5758 ctsio->be_move_done = ctl_config_move_done; 5759 ctl_datamove((union ctl_io *)ctsio); 5760 5761 return (CTL_RETVAL_COMPLETE); 5762 } 5763 5764 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5765 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5766 if (len < sizeof (*hdr) || 5767 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5768 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5769 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5770 ctl_set_invalid_field(ctsio, 5771 /*sks_valid*/ 0, 5772 /*command*/ 0, 5773 /*field*/ 0, 5774 /*bit_valid*/ 0, 5775 /*bit*/ 0); 5776 goto done; 5777 } 5778 len = scsi_2btoul(hdr->desc_length); 5779 buf = (struct scsi_unmap_desc *)(hdr + 1); 5780 end = buf + len / sizeof(*buf); 5781 5782 endnz = buf; 5783 for (range = buf; range < end; range++) { 5784 lba = scsi_8btou64(range->lba); 5785 num_blocks = scsi_4btoul(range->length); 5786 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5787 || ((lba + num_blocks) < lba)) { 5788 ctl_set_lba_out_of_range(ctsio, 5789 MAX(lba, lun->be_lun->maxlba + 1)); 5790 ctl_done((union ctl_io *)ctsio); 5791 return (CTL_RETVAL_COMPLETE); 5792 } 5793 if (num_blocks != 0) 5794 endnz = range + 1; 5795 } 5796 5797 /* 5798 * Block backend can not handle zero last range. 5799 * Filter it out and return if there is nothing left. 5800 */ 5801 len = (uint8_t *)endnz - (uint8_t *)buf; 5802 if (len == 0) { 5803 ctl_set_success(ctsio); 5804 goto done; 5805 } 5806 5807 mtx_lock(&lun->lun_lock); 5808 ptrlen = (struct ctl_ptr_len_flags *) 5809 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5810 ptrlen->ptr = (void *)buf; 5811 ptrlen->len = len; 5812 ptrlen->flags = byte2; 5813 ctl_check_blocked(lun); 5814 mtx_unlock(&lun->lun_lock); 5815 5816 retval = lun->backend->config_write((union ctl_io *)ctsio); 5817 return (retval); 5818 5819done: 5820 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5821 free(ctsio->kern_data_ptr, M_CTL); 5822 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5823 } 5824 ctl_done((union ctl_io *)ctsio); 5825 return (CTL_RETVAL_COMPLETE); 5826} 5827 5828int 5829ctl_default_page_handler(struct ctl_scsiio *ctsio, 5830 struct ctl_page_index *page_index, uint8_t *page_ptr) 5831{ 5832 struct ctl_lun *lun; 5833 uint8_t *current_cp, *saved_cp; 5834 int set_ua; 5835 uint32_t initidx; 5836 5837 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5838 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5839 set_ua = 0; 5840 5841 current_cp = (page_index->page_data + (page_index->page_len * 5842 CTL_PAGE_CURRENT)); 5843 saved_cp = (page_index->page_data + (page_index->page_len * 5844 CTL_PAGE_SAVED)); 5845 5846 mtx_lock(&lun->lun_lock); 5847 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5848 memcpy(current_cp, page_ptr, page_index->page_len); 5849 memcpy(saved_cp, page_ptr, page_index->page_len); 5850 set_ua = 1; 5851 } 5852 if (set_ua != 0) 5853 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5854 mtx_unlock(&lun->lun_lock); 5855 if (set_ua) { 5856 ctl_isc_announce_mode(lun, 5857 ctl_get_initindex(&ctsio->io_hdr.nexus), 5858 page_index->page_code, page_index->subpage); 5859 } 5860 return (CTL_RETVAL_COMPLETE); 5861} 5862 5863static void 5864ctl_ie_timer(void *arg) 5865{ 5866 struct ctl_lun *lun = arg; 5867 uint64_t t; 5868 5869 if (lun->ie_asc == 0) 5870 return; 5871 5872 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5873 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5874 else 5875 lun->ie_reported = 0; 5876 5877 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5878 lun->ie_reportcnt++; 5879 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5880 if (t == 0 || t == UINT32_MAX) 5881 t = 3000; /* 5 min */ 5882 callout_schedule(&lun->ie_callout, t * hz / 10); 5883 } 5884} 5885 5886int 5887ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5888 struct ctl_page_index *page_index, uint8_t *page_ptr) 5889{ 5890 struct scsi_info_exceptions_page *pg; 5891 struct ctl_lun *lun; 5892 uint64_t t; 5893 5894 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5895 5896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5897 pg = (struct scsi_info_exceptions_page *)page_ptr; 5898 mtx_lock(&lun->lun_lock); 5899 if (pg->info_flags & SIEP_FLAGS_TEST) { 5900 lun->ie_asc = 0x5d; 5901 lun->ie_ascq = 0xff; 5902 if (pg->mrie == SIEP_MRIE_UA) { 5903 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5904 lun->ie_reported = 1; 5905 } else { 5906 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5907 lun->ie_reported = -1; 5908 } 5909 lun->ie_reportcnt = 1; 5910 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5911 lun->ie_reportcnt++; 5912 t = scsi_4btoul(pg->interval_timer); 5913 if (t == 0 || t == UINT32_MAX) 5914 t = 3000; /* 5 min */ 5915 callout_reset(&lun->ie_callout, t * hz / 10, 5916 ctl_ie_timer, lun); 5917 } 5918 } else { 5919 lun->ie_asc = 0; 5920 lun->ie_ascq = 0; 5921 lun->ie_reported = 1; 5922 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5923 lun->ie_reportcnt = UINT32_MAX; 5924 callout_stop(&lun->ie_callout); 5925 } 5926 mtx_unlock(&lun->lun_lock); 5927 return (CTL_RETVAL_COMPLETE); 5928} 5929 5930static int 5931ctl_do_mode_select(union ctl_io *io) 5932{ 5933 struct scsi_mode_page_header *page_header; 5934 struct ctl_page_index *page_index; 5935 struct ctl_scsiio *ctsio; 5936 int page_len, page_len_offset, page_len_size; 5937 union ctl_modepage_info *modepage_info; 5938 struct ctl_lun *lun; 5939 int *len_left, *len_used; 5940 int retval, i; 5941 5942 ctsio = &io->scsiio; 5943 page_index = NULL; 5944 page_len = 0; 5945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5946 5947 modepage_info = (union ctl_modepage_info *) 5948 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5949 len_left = &modepage_info->header.len_left; 5950 len_used = &modepage_info->header.len_used; 5951 5952do_next_page: 5953 5954 page_header = (struct scsi_mode_page_header *) 5955 (ctsio->kern_data_ptr + *len_used); 5956 5957 if (*len_left == 0) { 5958 free(ctsio->kern_data_ptr, M_CTL); 5959 ctl_set_success(ctsio); 5960 ctl_done((union ctl_io *)ctsio); 5961 return (CTL_RETVAL_COMPLETE); 5962 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5963 5964 free(ctsio->kern_data_ptr, M_CTL); 5965 ctl_set_param_len_error(ctsio); 5966 ctl_done((union ctl_io *)ctsio); 5967 return (CTL_RETVAL_COMPLETE); 5968 5969 } else if ((page_header->page_code & SMPH_SPF) 5970 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5971 5972 free(ctsio->kern_data_ptr, M_CTL); 5973 ctl_set_param_len_error(ctsio); 5974 ctl_done((union ctl_io *)ctsio); 5975 return (CTL_RETVAL_COMPLETE); 5976 } 5977 5978 5979 /* 5980 * XXX KDM should we do something with the block descriptor? 5981 */ 5982 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5983 page_index = &lun->mode_pages.index[i]; 5984 if (lun->be_lun->lun_type == T_DIRECT && 5985 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5986 continue; 5987 if (lun->be_lun->lun_type == T_PROCESSOR && 5988 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5989 continue; 5990 if (lun->be_lun->lun_type == T_CDROM && 5991 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5992 continue; 5993 5994 if ((page_index->page_code & SMPH_PC_MASK) != 5995 (page_header->page_code & SMPH_PC_MASK)) 5996 continue; 5997 5998 /* 5999 * If neither page has a subpage code, then we've got a 6000 * match. 6001 */ 6002 if (((page_index->page_code & SMPH_SPF) == 0) 6003 && ((page_header->page_code & SMPH_SPF) == 0)) { 6004 page_len = page_header->page_length; 6005 break; 6006 } 6007 6008 /* 6009 * If both pages have subpages, then the subpage numbers 6010 * have to match. 6011 */ 6012 if ((page_index->page_code & SMPH_SPF) 6013 && (page_header->page_code & SMPH_SPF)) { 6014 struct scsi_mode_page_header_sp *sph; 6015 6016 sph = (struct scsi_mode_page_header_sp *)page_header; 6017 if (page_index->subpage == sph->subpage) { 6018 page_len = scsi_2btoul(sph->page_length); 6019 break; 6020 } 6021 } 6022 } 6023 6024 /* 6025 * If we couldn't find the page, or if we don't have a mode select 6026 * handler for it, send back an error to the user. 6027 */ 6028 if ((i >= CTL_NUM_MODE_PAGES) 6029 || (page_index->select_handler == NULL)) { 6030 ctl_set_invalid_field(ctsio, 6031 /*sks_valid*/ 1, 6032 /*command*/ 0, 6033 /*field*/ *len_used, 6034 /*bit_valid*/ 0, 6035 /*bit*/ 0); 6036 free(ctsio->kern_data_ptr, M_CTL); 6037 ctl_done((union ctl_io *)ctsio); 6038 return (CTL_RETVAL_COMPLETE); 6039 } 6040 6041 if (page_index->page_code & SMPH_SPF) { 6042 page_len_offset = 2; 6043 page_len_size = 2; 6044 } else { 6045 page_len_size = 1; 6046 page_len_offset = 1; 6047 } 6048 6049 /* 6050 * If the length the initiator gives us isn't the one we specify in 6051 * the mode page header, or if they didn't specify enough data in 6052 * the CDB to avoid truncating this page, kick out the request. 6053 */ 6054 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6055 ctl_set_invalid_field(ctsio, 6056 /*sks_valid*/ 1, 6057 /*command*/ 0, 6058 /*field*/ *len_used + page_len_offset, 6059 /*bit_valid*/ 0, 6060 /*bit*/ 0); 6061 free(ctsio->kern_data_ptr, M_CTL); 6062 ctl_done((union ctl_io *)ctsio); 6063 return (CTL_RETVAL_COMPLETE); 6064 } 6065 if (*len_left < page_index->page_len) { 6066 free(ctsio->kern_data_ptr, M_CTL); 6067 ctl_set_param_len_error(ctsio); 6068 ctl_done((union ctl_io *)ctsio); 6069 return (CTL_RETVAL_COMPLETE); 6070 } 6071 6072 /* 6073 * Run through the mode page, checking to make sure that the bits 6074 * the user changed are actually legal for him to change. 6075 */ 6076 for (i = 0; i < page_index->page_len; i++) { 6077 uint8_t *user_byte, *change_mask, *current_byte; 6078 int bad_bit; 6079 int j; 6080 6081 user_byte = (uint8_t *)page_header + i; 6082 change_mask = page_index->page_data + 6083 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6084 current_byte = page_index->page_data + 6085 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6086 6087 /* 6088 * Check to see whether the user set any bits in this byte 6089 * that he is not allowed to set. 6090 */ 6091 if ((*user_byte & ~(*change_mask)) == 6092 (*current_byte & ~(*change_mask))) 6093 continue; 6094 6095 /* 6096 * Go through bit by bit to determine which one is illegal. 6097 */ 6098 bad_bit = 0; 6099 for (j = 7; j >= 0; j--) { 6100 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6101 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6102 bad_bit = i; 6103 break; 6104 } 6105 } 6106 ctl_set_invalid_field(ctsio, 6107 /*sks_valid*/ 1, 6108 /*command*/ 0, 6109 /*field*/ *len_used + i, 6110 /*bit_valid*/ 1, 6111 /*bit*/ bad_bit); 6112 free(ctsio->kern_data_ptr, M_CTL); 6113 ctl_done((union ctl_io *)ctsio); 6114 return (CTL_RETVAL_COMPLETE); 6115 } 6116 6117 /* 6118 * Decrement these before we call the page handler, since we may 6119 * end up getting called back one way or another before the handler 6120 * returns to this context. 6121 */ 6122 *len_left -= page_index->page_len; 6123 *len_used += page_index->page_len; 6124 6125 retval = page_index->select_handler(ctsio, page_index, 6126 (uint8_t *)page_header); 6127 6128 /* 6129 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6130 * wait until this queued command completes to finish processing 6131 * the mode page. If it returns anything other than 6132 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6133 * already set the sense information, freed the data pointer, and 6134 * completed the io for us. 6135 */ 6136 if (retval != CTL_RETVAL_COMPLETE) 6137 goto bailout_no_done; 6138 6139 /* 6140 * If the initiator sent us more than one page, parse the next one. 6141 */ 6142 if (*len_left > 0) 6143 goto do_next_page; 6144 6145 ctl_set_success(ctsio); 6146 free(ctsio->kern_data_ptr, M_CTL); 6147 ctl_done((union ctl_io *)ctsio); 6148 6149bailout_no_done: 6150 6151 return (CTL_RETVAL_COMPLETE); 6152 6153} 6154 6155int 6156ctl_mode_select(struct ctl_scsiio *ctsio) 6157{ 6158 int param_len, pf, sp; 6159 int header_size, bd_len; 6160 union ctl_modepage_info *modepage_info; 6161 6162 switch (ctsio->cdb[0]) { 6163 case MODE_SELECT_6: { 6164 struct scsi_mode_select_6 *cdb; 6165 6166 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6167 6168 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6169 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6170 param_len = cdb->length; 6171 header_size = sizeof(struct scsi_mode_header_6); 6172 break; 6173 } 6174 case MODE_SELECT_10: { 6175 struct scsi_mode_select_10 *cdb; 6176 6177 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6178 6179 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6180 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6181 param_len = scsi_2btoul(cdb->length); 6182 header_size = sizeof(struct scsi_mode_header_10); 6183 break; 6184 } 6185 default: 6186 ctl_set_invalid_opcode(ctsio); 6187 ctl_done((union ctl_io *)ctsio); 6188 return (CTL_RETVAL_COMPLETE); 6189 } 6190 6191 /* 6192 * From SPC-3: 6193 * "A parameter list length of zero indicates that the Data-Out Buffer 6194 * shall be empty. This condition shall not be considered as an error." 6195 */ 6196 if (param_len == 0) { 6197 ctl_set_success(ctsio); 6198 ctl_done((union ctl_io *)ctsio); 6199 return (CTL_RETVAL_COMPLETE); 6200 } 6201 6202 /* 6203 * Since we'll hit this the first time through, prior to 6204 * allocation, we don't need to free a data buffer here. 6205 */ 6206 if (param_len < header_size) { 6207 ctl_set_param_len_error(ctsio); 6208 ctl_done((union ctl_io *)ctsio); 6209 return (CTL_RETVAL_COMPLETE); 6210 } 6211 6212 /* 6213 * Allocate the data buffer and grab the user's data. In theory, 6214 * we shouldn't have to sanity check the parameter list length here 6215 * because the maximum size is 64K. We should be able to malloc 6216 * that much without too many problems. 6217 */ 6218 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6219 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6220 ctsio->kern_data_len = param_len; 6221 ctsio->kern_total_len = param_len; 6222 ctsio->kern_data_resid = 0; 6223 ctsio->kern_rel_offset = 0; 6224 ctsio->kern_sg_entries = 0; 6225 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6226 ctsio->be_move_done = ctl_config_move_done; 6227 ctl_datamove((union ctl_io *)ctsio); 6228 6229 return (CTL_RETVAL_COMPLETE); 6230 } 6231 6232 switch (ctsio->cdb[0]) { 6233 case MODE_SELECT_6: { 6234 struct scsi_mode_header_6 *mh6; 6235 6236 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6237 bd_len = mh6->blk_desc_len; 6238 break; 6239 } 6240 case MODE_SELECT_10: { 6241 struct scsi_mode_header_10 *mh10; 6242 6243 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6244 bd_len = scsi_2btoul(mh10->blk_desc_len); 6245 break; 6246 } 6247 default: 6248 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6249 } 6250 6251 if (param_len < (header_size + bd_len)) { 6252 free(ctsio->kern_data_ptr, M_CTL); 6253 ctl_set_param_len_error(ctsio); 6254 ctl_done((union ctl_io *)ctsio); 6255 return (CTL_RETVAL_COMPLETE); 6256 } 6257 6258 /* 6259 * Set the IO_CONT flag, so that if this I/O gets passed to 6260 * ctl_config_write_done(), it'll get passed back to 6261 * ctl_do_mode_select() for further processing, or completion if 6262 * we're all done. 6263 */ 6264 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6265 ctsio->io_cont = ctl_do_mode_select; 6266 6267 modepage_info = (union ctl_modepage_info *) 6268 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6269 memset(modepage_info, 0, sizeof(*modepage_info)); 6270 modepage_info->header.len_left = param_len - header_size - bd_len; 6271 modepage_info->header.len_used = header_size + bd_len; 6272 6273 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6274} 6275 6276int 6277ctl_mode_sense(struct ctl_scsiio *ctsio) 6278{ 6279 struct ctl_lun *lun; 6280 int pc, page_code, dbd, llba, subpage; 6281 int alloc_len, page_len, header_len, total_len; 6282 struct scsi_mode_block_descr *block_desc; 6283 struct ctl_page_index *page_index; 6284 6285 dbd = 0; 6286 llba = 0; 6287 block_desc = NULL; 6288 6289 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6290 6291 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6292 switch (ctsio->cdb[0]) { 6293 case MODE_SENSE_6: { 6294 struct scsi_mode_sense_6 *cdb; 6295 6296 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6297 6298 header_len = sizeof(struct scsi_mode_hdr_6); 6299 if (cdb->byte2 & SMS_DBD) 6300 dbd = 1; 6301 else 6302 header_len += sizeof(struct scsi_mode_block_descr); 6303 6304 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6305 page_code = cdb->page & SMS_PAGE_CODE; 6306 subpage = cdb->subpage; 6307 alloc_len = cdb->length; 6308 break; 6309 } 6310 case MODE_SENSE_10: { 6311 struct scsi_mode_sense_10 *cdb; 6312 6313 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6314 6315 header_len = sizeof(struct scsi_mode_hdr_10); 6316 6317 if (cdb->byte2 & SMS_DBD) 6318 dbd = 1; 6319 else 6320 header_len += sizeof(struct scsi_mode_block_descr); 6321 if (cdb->byte2 & SMS10_LLBAA) 6322 llba = 1; 6323 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6324 page_code = cdb->page & SMS_PAGE_CODE; 6325 subpage = cdb->subpage; 6326 alloc_len = scsi_2btoul(cdb->length); 6327 break; 6328 } 6329 default: 6330 ctl_set_invalid_opcode(ctsio); 6331 ctl_done((union ctl_io *)ctsio); 6332 return (CTL_RETVAL_COMPLETE); 6333 break; /* NOTREACHED */ 6334 } 6335 6336 /* 6337 * We have to make a first pass through to calculate the size of 6338 * the pages that match the user's query. Then we allocate enough 6339 * memory to hold it, and actually copy the data into the buffer. 6340 */ 6341 switch (page_code) { 6342 case SMS_ALL_PAGES_PAGE: { 6343 u_int i; 6344 6345 page_len = 0; 6346 6347 /* 6348 * At the moment, values other than 0 and 0xff here are 6349 * reserved according to SPC-3. 6350 */ 6351 if ((subpage != SMS_SUBPAGE_PAGE_0) 6352 && (subpage != SMS_SUBPAGE_ALL)) { 6353 ctl_set_invalid_field(ctsio, 6354 /*sks_valid*/ 1, 6355 /*command*/ 1, 6356 /*field*/ 3, 6357 /*bit_valid*/ 0, 6358 /*bit*/ 0); 6359 ctl_done((union ctl_io *)ctsio); 6360 return (CTL_RETVAL_COMPLETE); 6361 } 6362 6363 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6364 page_index = &lun->mode_pages.index[i]; 6365 6366 /* Make sure the page is supported for this dev type */ 6367 if (lun->be_lun->lun_type == T_DIRECT && 6368 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6369 continue; 6370 if (lun->be_lun->lun_type == T_PROCESSOR && 6371 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6372 continue; 6373 if (lun->be_lun->lun_type == T_CDROM && 6374 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6375 continue; 6376 6377 /* 6378 * We don't use this subpage if the user didn't 6379 * request all subpages. 6380 */ 6381 if ((page_index->subpage != 0) 6382 && (subpage == SMS_SUBPAGE_PAGE_0)) 6383 continue; 6384 6385#if 0 6386 printf("found page %#x len %d\n", 6387 page_index->page_code & SMPH_PC_MASK, 6388 page_index->page_len); 6389#endif 6390 page_len += page_index->page_len; 6391 } 6392 break; 6393 } 6394 default: { 6395 u_int i; 6396 6397 page_len = 0; 6398 6399 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6400 page_index = &lun->mode_pages.index[i]; 6401 6402 /* Make sure the page is supported for this dev type */ 6403 if (lun->be_lun->lun_type == T_DIRECT && 6404 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6405 continue; 6406 if (lun->be_lun->lun_type == T_PROCESSOR && 6407 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6408 continue; 6409 if (lun->be_lun->lun_type == T_CDROM && 6410 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6411 continue; 6412 6413 /* Look for the right page code */ 6414 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6415 continue; 6416 6417 /* Look for the right subpage or the subpage wildcard*/ 6418 if ((page_index->subpage != subpage) 6419 && (subpage != SMS_SUBPAGE_ALL)) 6420 continue; 6421 6422#if 0 6423 printf("found page %#x len %d\n", 6424 page_index->page_code & SMPH_PC_MASK, 6425 page_index->page_len); 6426#endif 6427 6428 page_len += page_index->page_len; 6429 } 6430 6431 if (page_len == 0) { 6432 ctl_set_invalid_field(ctsio, 6433 /*sks_valid*/ 1, 6434 /*command*/ 1, 6435 /*field*/ 2, 6436 /*bit_valid*/ 1, 6437 /*bit*/ 5); 6438 ctl_done((union ctl_io *)ctsio); 6439 return (CTL_RETVAL_COMPLETE); 6440 } 6441 break; 6442 } 6443 } 6444 6445 total_len = header_len + page_len; 6446#if 0 6447 printf("header_len = %d, page_len = %d, total_len = %d\n", 6448 header_len, page_len, total_len); 6449#endif 6450 6451 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6452 ctsio->kern_sg_entries = 0; 6453 ctsio->kern_data_resid = 0; 6454 ctsio->kern_rel_offset = 0; 6455 if (total_len < alloc_len) { 6456 ctsio->residual = alloc_len - total_len; 6457 ctsio->kern_data_len = total_len; 6458 ctsio->kern_total_len = total_len; 6459 } else { 6460 ctsio->residual = 0; 6461 ctsio->kern_data_len = alloc_len; 6462 ctsio->kern_total_len = alloc_len; 6463 } 6464 6465 switch (ctsio->cdb[0]) { 6466 case MODE_SENSE_6: { 6467 struct scsi_mode_hdr_6 *header; 6468 6469 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6470 6471 header->datalen = MIN(total_len - 1, 254); 6472 if (lun->be_lun->lun_type == T_DIRECT) { 6473 header->dev_specific = 0x10; /* DPOFUA */ 6474 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6475 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6476 header->dev_specific |= 0x80; /* WP */ 6477 } 6478 if (dbd) 6479 header->block_descr_len = 0; 6480 else 6481 header->block_descr_len = 6482 sizeof(struct scsi_mode_block_descr); 6483 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6484 break; 6485 } 6486 case MODE_SENSE_10: { 6487 struct scsi_mode_hdr_10 *header; 6488 int datalen; 6489 6490 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6491 6492 datalen = MIN(total_len - 2, 65533); 6493 scsi_ulto2b(datalen, header->datalen); 6494 if (lun->be_lun->lun_type == T_DIRECT) { 6495 header->dev_specific = 0x10; /* DPOFUA */ 6496 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6497 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6498 header->dev_specific |= 0x80; /* WP */ 6499 } 6500 if (dbd) 6501 scsi_ulto2b(0, header->block_descr_len); 6502 else 6503 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6504 header->block_descr_len); 6505 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6506 break; 6507 } 6508 default: 6509 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6510 } 6511 6512 /* 6513 * If we've got a disk, use its blocksize in the block 6514 * descriptor. Otherwise, just set it to 0. 6515 */ 6516 if (dbd == 0) { 6517 if (lun->be_lun->lun_type == T_DIRECT) 6518 scsi_ulto3b(lun->be_lun->blocksize, 6519 block_desc->block_len); 6520 else 6521 scsi_ulto3b(0, block_desc->block_len); 6522 } 6523 6524 switch (page_code) { 6525 case SMS_ALL_PAGES_PAGE: { 6526 int i, data_used; 6527 6528 data_used = header_len; 6529 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6530 struct ctl_page_index *page_index; 6531 6532 page_index = &lun->mode_pages.index[i]; 6533 if (lun->be_lun->lun_type == T_DIRECT && 6534 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6535 continue; 6536 if (lun->be_lun->lun_type == T_PROCESSOR && 6537 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6538 continue; 6539 if (lun->be_lun->lun_type == T_CDROM && 6540 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6541 continue; 6542 6543 /* 6544 * We don't use this subpage if the user didn't 6545 * request all subpages. We already checked (above) 6546 * to make sure the user only specified a subpage 6547 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6548 */ 6549 if ((page_index->subpage != 0) 6550 && (subpage == SMS_SUBPAGE_PAGE_0)) 6551 continue; 6552 6553 /* 6554 * Call the handler, if it exists, to update the 6555 * page to the latest values. 6556 */ 6557 if (page_index->sense_handler != NULL) 6558 page_index->sense_handler(ctsio, page_index,pc); 6559 6560 memcpy(ctsio->kern_data_ptr + data_used, 6561 page_index->page_data + 6562 (page_index->page_len * pc), 6563 page_index->page_len); 6564 data_used += page_index->page_len; 6565 } 6566 break; 6567 } 6568 default: { 6569 int i, data_used; 6570 6571 data_used = header_len; 6572 6573 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6574 struct ctl_page_index *page_index; 6575 6576 page_index = &lun->mode_pages.index[i]; 6577 6578 /* Look for the right page code */ 6579 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6580 continue; 6581 6582 /* Look for the right subpage or the subpage wildcard*/ 6583 if ((page_index->subpage != subpage) 6584 && (subpage != SMS_SUBPAGE_ALL)) 6585 continue; 6586 6587 /* Make sure the page is supported for this dev type */ 6588 if (lun->be_lun->lun_type == T_DIRECT && 6589 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6590 continue; 6591 if (lun->be_lun->lun_type == T_PROCESSOR && 6592 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6593 continue; 6594 if (lun->be_lun->lun_type == T_CDROM && 6595 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6596 continue; 6597 6598 /* 6599 * Call the handler, if it exists, to update the 6600 * page to the latest values. 6601 */ 6602 if (page_index->sense_handler != NULL) 6603 page_index->sense_handler(ctsio, page_index,pc); 6604 6605 memcpy(ctsio->kern_data_ptr + data_used, 6606 page_index->page_data + 6607 (page_index->page_len * pc), 6608 page_index->page_len); 6609 data_used += page_index->page_len; 6610 } 6611 break; 6612 } 6613 } 6614 6615 ctl_set_success(ctsio); 6616 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6617 ctsio->be_move_done = ctl_config_move_done; 6618 ctl_datamove((union ctl_io *)ctsio); 6619 return (CTL_RETVAL_COMPLETE); 6620} 6621 6622int 6623ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6624 struct ctl_page_index *page_index, 6625 int pc) 6626{ 6627 struct ctl_lun *lun; 6628 struct scsi_log_param_header *phdr; 6629 uint8_t *data; 6630 uint64_t val; 6631 6632 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6633 data = page_index->page_data; 6634 6635 if (lun->backend->lun_attr != NULL && 6636 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6637 != UINT64_MAX) { 6638 phdr = (struct scsi_log_param_header *)data; 6639 scsi_ulto2b(0x0001, phdr->param_code); 6640 phdr->param_control = SLP_LBIN | SLP_LP; 6641 phdr->param_len = 8; 6642 data = (uint8_t *)(phdr + 1); 6643 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6644 data[4] = 0x02; /* per-pool */ 6645 data += phdr->param_len; 6646 } 6647 6648 if (lun->backend->lun_attr != NULL && 6649 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6650 != UINT64_MAX) { 6651 phdr = (struct scsi_log_param_header *)data; 6652 scsi_ulto2b(0x0002, phdr->param_code); 6653 phdr->param_control = SLP_LBIN | SLP_LP; 6654 phdr->param_len = 8; 6655 data = (uint8_t *)(phdr + 1); 6656 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6657 data[4] = 0x01; /* per-LUN */ 6658 data += phdr->param_len; 6659 } 6660 6661 if (lun->backend->lun_attr != NULL && 6662 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6663 != UINT64_MAX) { 6664 phdr = (struct scsi_log_param_header *)data; 6665 scsi_ulto2b(0x00f1, phdr->param_code); 6666 phdr->param_control = SLP_LBIN | SLP_LP; 6667 phdr->param_len = 8; 6668 data = (uint8_t *)(phdr + 1); 6669 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6670 data[4] = 0x02; /* per-pool */ 6671 data += phdr->param_len; 6672 } 6673 6674 if (lun->backend->lun_attr != NULL && 6675 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6676 != UINT64_MAX) { 6677 phdr = (struct scsi_log_param_header *)data; 6678 scsi_ulto2b(0x00f2, phdr->param_code); 6679 phdr->param_control = SLP_LBIN | SLP_LP; 6680 phdr->param_len = 8; 6681 data = (uint8_t *)(phdr + 1); 6682 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6683 data[4] = 0x02; /* per-pool */ 6684 data += phdr->param_len; 6685 } 6686 6687 page_index->page_len = data - page_index->page_data; 6688 return (0); 6689} 6690 6691int 6692ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6693 struct ctl_page_index *page_index, 6694 int pc) 6695{ 6696 struct ctl_lun *lun; 6697 struct stat_page *data; 6698 uint64_t rn, wn, rb, wb; 6699 struct bintime rt, wt; 6700 int i; 6701 6702 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6703 data = (struct stat_page *)page_index->page_data; 6704 6705 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6706 data->sap.hdr.param_control = SLP_LBIN; 6707 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6708 sizeof(struct scsi_log_param_header); 6709 rn = wn = rb = wb = 0; 6710 bintime_clear(&rt); 6711 bintime_clear(&wt); 6712 for (i = 0; i < CTL_MAX_PORTS; i++) { 6713 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6714 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6715 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6716 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6717 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6718 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6719 } 6720 scsi_u64to8b(rn, data->sap.read_num); 6721 scsi_u64to8b(wn, data->sap.write_num); 6722 if (lun->stats.blocksize > 0) { 6723 scsi_u64to8b(wb / lun->stats.blocksize, 6724 data->sap.recvieved_lba); 6725 scsi_u64to8b(rb / lun->stats.blocksize, 6726 data->sap.transmitted_lba); 6727 } 6728 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6729 data->sap.read_int); 6730 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6731 data->sap.write_int); 6732 scsi_u64to8b(0, data->sap.weighted_num); 6733 scsi_u64to8b(0, data->sap.weighted_int); 6734 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6735 data->it.hdr.param_control = SLP_LBIN; 6736 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6737 sizeof(struct scsi_log_param_header); 6738#ifdef CTL_TIME_IO 6739 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6740#endif 6741 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6742 data->it.hdr.param_control = SLP_LBIN; 6743 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6744 sizeof(struct scsi_log_param_header); 6745 scsi_ulto4b(3, data->ti.exponent); 6746 scsi_ulto4b(1, data->ti.integer); 6747 return (0); 6748} 6749 6750int 6751ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6752 struct ctl_page_index *page_index, 6753 int pc) 6754{ 6755 struct ctl_lun *lun; 6756 struct scsi_log_informational_exceptions *data; 6757 6758 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6759 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6760 6761 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6762 data->hdr.param_control = SLP_LBIN; 6763 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6764 sizeof(struct scsi_log_param_header); 6765 data->ie_asc = lun->ie_asc; 6766 data->ie_ascq = lun->ie_ascq; 6767 data->temperature = 0xff; 6768 return (0); 6769} 6770 6771int 6772ctl_log_sense(struct ctl_scsiio *ctsio) 6773{ 6774 struct ctl_lun *lun; 6775 int i, pc, page_code, subpage; 6776 int alloc_len, total_len; 6777 struct ctl_page_index *page_index; 6778 struct scsi_log_sense *cdb; 6779 struct scsi_log_header *header; 6780 6781 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6782 6783 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6784 cdb = (struct scsi_log_sense *)ctsio->cdb; 6785 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6786 page_code = cdb->page & SLS_PAGE_CODE; 6787 subpage = cdb->subpage; 6788 alloc_len = scsi_2btoul(cdb->length); 6789 6790 page_index = NULL; 6791 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6792 page_index = &lun->log_pages.index[i]; 6793 6794 /* Look for the right page code */ 6795 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6796 continue; 6797 6798 /* Look for the right subpage or the subpage wildcard*/ 6799 if (page_index->subpage != subpage) 6800 continue; 6801 6802 break; 6803 } 6804 if (i >= CTL_NUM_LOG_PAGES) { 6805 ctl_set_invalid_field(ctsio, 6806 /*sks_valid*/ 1, 6807 /*command*/ 1, 6808 /*field*/ 2, 6809 /*bit_valid*/ 0, 6810 /*bit*/ 0); 6811 ctl_done((union ctl_io *)ctsio); 6812 return (CTL_RETVAL_COMPLETE); 6813 } 6814 6815 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6816 6817 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6818 ctsio->kern_sg_entries = 0; 6819 ctsio->kern_data_resid = 0; 6820 ctsio->kern_rel_offset = 0; 6821 if (total_len < alloc_len) { 6822 ctsio->residual = alloc_len - total_len; 6823 ctsio->kern_data_len = total_len; 6824 ctsio->kern_total_len = total_len; 6825 } else { 6826 ctsio->residual = 0; 6827 ctsio->kern_data_len = alloc_len; 6828 ctsio->kern_total_len = alloc_len; 6829 } 6830 6831 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6832 header->page = page_index->page_code; 6833 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6834 header->page |= SL_DS; 6835 if (page_index->subpage) { 6836 header->page |= SL_SPF; 6837 header->subpage = page_index->subpage; 6838 } 6839 scsi_ulto2b(page_index->page_len, header->datalen); 6840 6841 /* 6842 * Call the handler, if it exists, to update the 6843 * page to the latest values. 6844 */ 6845 if (page_index->sense_handler != NULL) 6846 page_index->sense_handler(ctsio, page_index, pc); 6847 6848 memcpy(header + 1, page_index->page_data, page_index->page_len); 6849 6850 ctl_set_success(ctsio); 6851 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6852 ctsio->be_move_done = ctl_config_move_done; 6853 ctl_datamove((union ctl_io *)ctsio); 6854 return (CTL_RETVAL_COMPLETE); 6855} 6856 6857int 6858ctl_read_capacity(struct ctl_scsiio *ctsio) 6859{ 6860 struct scsi_read_capacity *cdb; 6861 struct scsi_read_capacity_data *data; 6862 struct ctl_lun *lun; 6863 uint32_t lba; 6864 6865 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6866 6867 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6868 6869 lba = scsi_4btoul(cdb->addr); 6870 if (((cdb->pmi & SRC_PMI) == 0) 6871 && (lba != 0)) { 6872 ctl_set_invalid_field(/*ctsio*/ ctsio, 6873 /*sks_valid*/ 1, 6874 /*command*/ 1, 6875 /*field*/ 2, 6876 /*bit_valid*/ 0, 6877 /*bit*/ 0); 6878 ctl_done((union ctl_io *)ctsio); 6879 return (CTL_RETVAL_COMPLETE); 6880 } 6881 6882 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6883 6884 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6885 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6886 ctsio->residual = 0; 6887 ctsio->kern_data_len = sizeof(*data); 6888 ctsio->kern_total_len = sizeof(*data); 6889 ctsio->kern_data_resid = 0; 6890 ctsio->kern_rel_offset = 0; 6891 ctsio->kern_sg_entries = 0; 6892 6893 /* 6894 * If the maximum LBA is greater than 0xfffffffe, the user must 6895 * issue a SERVICE ACTION IN (16) command, with the read capacity 6896 * serivce action set. 6897 */ 6898 if (lun->be_lun->maxlba > 0xfffffffe) 6899 scsi_ulto4b(0xffffffff, data->addr); 6900 else 6901 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6902 6903 /* 6904 * XXX KDM this may not be 512 bytes... 6905 */ 6906 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6907 6908 ctl_set_success(ctsio); 6909 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6910 ctsio->be_move_done = ctl_config_move_done; 6911 ctl_datamove((union ctl_io *)ctsio); 6912 return (CTL_RETVAL_COMPLETE); 6913} 6914 6915int 6916ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6917{ 6918 struct scsi_read_capacity_16 *cdb; 6919 struct scsi_read_capacity_data_long *data; 6920 struct ctl_lun *lun; 6921 uint64_t lba; 6922 uint32_t alloc_len; 6923 6924 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6925 6926 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6927 6928 alloc_len = scsi_4btoul(cdb->alloc_len); 6929 lba = scsi_8btou64(cdb->addr); 6930 6931 if ((cdb->reladr & SRC16_PMI) 6932 && (lba != 0)) { 6933 ctl_set_invalid_field(/*ctsio*/ ctsio, 6934 /*sks_valid*/ 1, 6935 /*command*/ 1, 6936 /*field*/ 2, 6937 /*bit_valid*/ 0, 6938 /*bit*/ 0); 6939 ctl_done((union ctl_io *)ctsio); 6940 return (CTL_RETVAL_COMPLETE); 6941 } 6942 6943 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6944 6945 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6946 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6947 6948 if (sizeof(*data) < alloc_len) { 6949 ctsio->residual = alloc_len - sizeof(*data); 6950 ctsio->kern_data_len = sizeof(*data); 6951 ctsio->kern_total_len = sizeof(*data); 6952 } else { 6953 ctsio->residual = 0; 6954 ctsio->kern_data_len = alloc_len; 6955 ctsio->kern_total_len = alloc_len; 6956 } 6957 ctsio->kern_data_resid = 0; 6958 ctsio->kern_rel_offset = 0; 6959 ctsio->kern_sg_entries = 0; 6960 6961 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6962 /* XXX KDM this may not be 512 bytes... */ 6963 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6964 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6965 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6966 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6967 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6968 6969 ctl_set_success(ctsio); 6970 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6971 ctsio->be_move_done = ctl_config_move_done; 6972 ctl_datamove((union ctl_io *)ctsio); 6973 return (CTL_RETVAL_COMPLETE); 6974} 6975 6976int 6977ctl_get_lba_status(struct ctl_scsiio *ctsio) 6978{ 6979 struct scsi_get_lba_status *cdb; 6980 struct scsi_get_lba_status_data *data; 6981 struct ctl_lun *lun; 6982 struct ctl_lba_len_flags *lbalen; 6983 uint64_t lba; 6984 uint32_t alloc_len, total_len; 6985 int retval; 6986 6987 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6988 6989 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6990 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6991 lba = scsi_8btou64(cdb->addr); 6992 alloc_len = scsi_4btoul(cdb->alloc_len); 6993 6994 if (lba > lun->be_lun->maxlba) { 6995 ctl_set_lba_out_of_range(ctsio, lba); 6996 ctl_done((union ctl_io *)ctsio); 6997 return (CTL_RETVAL_COMPLETE); 6998 } 6999 7000 total_len = sizeof(*data) + sizeof(data->descr[0]); 7001 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7002 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7003 7004 if (total_len < alloc_len) { 7005 ctsio->residual = alloc_len - total_len; 7006 ctsio->kern_data_len = total_len; 7007 ctsio->kern_total_len = total_len; 7008 } else { 7009 ctsio->residual = 0; 7010 ctsio->kern_data_len = alloc_len; 7011 ctsio->kern_total_len = alloc_len; 7012 } 7013 ctsio->kern_data_resid = 0; 7014 ctsio->kern_rel_offset = 0; 7015 ctsio->kern_sg_entries = 0; 7016 7017 /* Fill dummy data in case backend can't tell anything. */ 7018 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7019 scsi_u64to8b(lba, data->descr[0].addr); 7020 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7021 data->descr[0].length); 7022 data->descr[0].status = 0; /* Mapped or unknown. */ 7023 7024 ctl_set_success(ctsio); 7025 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7026 ctsio->be_move_done = ctl_config_move_done; 7027 7028 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7029 lbalen->lba = lba; 7030 lbalen->len = total_len; 7031 lbalen->flags = 0; 7032 retval = lun->backend->config_read((union ctl_io *)ctsio); 7033 return (CTL_RETVAL_COMPLETE); 7034} 7035 7036int 7037ctl_read_defect(struct ctl_scsiio *ctsio) 7038{ 7039 struct scsi_read_defect_data_10 *ccb10; 7040 struct scsi_read_defect_data_12 *ccb12; 7041 struct scsi_read_defect_data_hdr_10 *data10; 7042 struct scsi_read_defect_data_hdr_12 *data12; 7043 uint32_t alloc_len, data_len; 7044 uint8_t format; 7045 7046 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7047 7048 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7049 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7050 format = ccb10->format; 7051 alloc_len = scsi_2btoul(ccb10->alloc_length); 7052 data_len = sizeof(*data10); 7053 } else { 7054 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7055 format = ccb12->format; 7056 alloc_len = scsi_4btoul(ccb12->alloc_length); 7057 data_len = sizeof(*data12); 7058 } 7059 if (alloc_len == 0) { 7060 ctl_set_success(ctsio); 7061 ctl_done((union ctl_io *)ctsio); 7062 return (CTL_RETVAL_COMPLETE); 7063 } 7064 7065 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7066 if (data_len < alloc_len) { 7067 ctsio->residual = alloc_len - data_len; 7068 ctsio->kern_data_len = data_len; 7069 ctsio->kern_total_len = data_len; 7070 } else { 7071 ctsio->residual = 0; 7072 ctsio->kern_data_len = alloc_len; 7073 ctsio->kern_total_len = alloc_len; 7074 } 7075 ctsio->kern_data_resid = 0; 7076 ctsio->kern_rel_offset = 0; 7077 ctsio->kern_sg_entries = 0; 7078 7079 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7080 data10 = (struct scsi_read_defect_data_hdr_10 *) 7081 ctsio->kern_data_ptr; 7082 data10->format = format; 7083 scsi_ulto2b(0, data10->length); 7084 } else { 7085 data12 = (struct scsi_read_defect_data_hdr_12 *) 7086 ctsio->kern_data_ptr; 7087 data12->format = format; 7088 scsi_ulto2b(0, data12->generation); 7089 scsi_ulto4b(0, data12->length); 7090 } 7091 7092 ctl_set_success(ctsio); 7093 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7094 ctsio->be_move_done = ctl_config_move_done; 7095 ctl_datamove((union ctl_io *)ctsio); 7096 return (CTL_RETVAL_COMPLETE); 7097} 7098 7099int 7100ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7101{ 7102 struct scsi_maintenance_in *cdb; 7103 int retval; 7104 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7105 int num_ha_groups, num_target_ports, shared_group; 7106 struct ctl_lun *lun; 7107 struct ctl_softc *softc; 7108 struct ctl_port *port; 7109 struct scsi_target_group_data *rtg_ptr; 7110 struct scsi_target_group_data_extended *rtg_ext_ptr; 7111 struct scsi_target_port_group_descriptor *tpg_desc; 7112 7113 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7114 7115 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7116 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7117 softc = lun->ctl_softc; 7118 7119 retval = CTL_RETVAL_COMPLETE; 7120 7121 switch (cdb->byte2 & STG_PDF_MASK) { 7122 case STG_PDF_LENGTH: 7123 ext = 0; 7124 break; 7125 case STG_PDF_EXTENDED: 7126 ext = 1; 7127 break; 7128 default: 7129 ctl_set_invalid_field(/*ctsio*/ ctsio, 7130 /*sks_valid*/ 1, 7131 /*command*/ 1, 7132 /*field*/ 2, 7133 /*bit_valid*/ 1, 7134 /*bit*/ 5); 7135 ctl_done((union ctl_io *)ctsio); 7136 return(retval); 7137 } 7138 7139 num_target_ports = 0; 7140 shared_group = (softc->is_single != 0); 7141 mtx_lock(&softc->ctl_lock); 7142 STAILQ_FOREACH(port, &softc->port_list, links) { 7143 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7144 continue; 7145 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7146 continue; 7147 num_target_ports++; 7148 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7149 shared_group = 1; 7150 } 7151 mtx_unlock(&softc->ctl_lock); 7152 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7153 7154 if (ext) 7155 total_len = sizeof(struct scsi_target_group_data_extended); 7156 else 7157 total_len = sizeof(struct scsi_target_group_data); 7158 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7159 (shared_group + num_ha_groups) + 7160 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7161 7162 alloc_len = scsi_4btoul(cdb->length); 7163 7164 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7165 7166 ctsio->kern_sg_entries = 0; 7167 7168 if (total_len < alloc_len) { 7169 ctsio->residual = alloc_len - total_len; 7170 ctsio->kern_data_len = total_len; 7171 ctsio->kern_total_len = total_len; 7172 } else { 7173 ctsio->residual = 0; 7174 ctsio->kern_data_len = alloc_len; 7175 ctsio->kern_total_len = alloc_len; 7176 } 7177 ctsio->kern_data_resid = 0; 7178 ctsio->kern_rel_offset = 0; 7179 7180 if (ext) { 7181 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7182 ctsio->kern_data_ptr; 7183 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7184 rtg_ext_ptr->format_type = 0x10; 7185 rtg_ext_ptr->implicit_transition_time = 0; 7186 tpg_desc = &rtg_ext_ptr->groups[0]; 7187 } else { 7188 rtg_ptr = (struct scsi_target_group_data *) 7189 ctsio->kern_data_ptr; 7190 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7191 tpg_desc = &rtg_ptr->groups[0]; 7192 } 7193 7194 mtx_lock(&softc->ctl_lock); 7195 pg = softc->port_min / softc->port_cnt; 7196 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7197 /* Some shelf is known to be primary. */ 7198 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7199 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7200 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7201 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7202 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7203 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7204 else 7205 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7206 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7207 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7208 } else { 7209 ts = os; 7210 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7211 } 7212 } else { 7213 /* No known primary shelf. */ 7214 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7215 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7216 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7217 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7218 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7219 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7220 } else { 7221 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7222 } 7223 } 7224 if (shared_group) { 7225 tpg_desc->pref_state = ts; 7226 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7227 TPG_U_SUP | TPG_T_SUP; 7228 scsi_ulto2b(1, tpg_desc->target_port_group); 7229 tpg_desc->status = TPG_IMPLICIT; 7230 pc = 0; 7231 STAILQ_FOREACH(port, &softc->port_list, links) { 7232 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7233 continue; 7234 if (!softc->is_single && 7235 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7236 continue; 7237 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7238 continue; 7239 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7240 relative_target_port_identifier); 7241 pc++; 7242 } 7243 tpg_desc->target_port_count = pc; 7244 tpg_desc = (struct scsi_target_port_group_descriptor *) 7245 &tpg_desc->descriptors[pc]; 7246 } 7247 for (g = 0; g < num_ha_groups; g++) { 7248 tpg_desc->pref_state = (g == pg) ? ts : os; 7249 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7250 TPG_U_SUP | TPG_T_SUP; 7251 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7252 tpg_desc->status = TPG_IMPLICIT; 7253 pc = 0; 7254 STAILQ_FOREACH(port, &softc->port_list, links) { 7255 if (port->targ_port < g * softc->port_cnt || 7256 port->targ_port >= (g + 1) * softc->port_cnt) 7257 continue; 7258 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7259 continue; 7260 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7261 continue; 7262 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7263 continue; 7264 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7265 relative_target_port_identifier); 7266 pc++; 7267 } 7268 tpg_desc->target_port_count = pc; 7269 tpg_desc = (struct scsi_target_port_group_descriptor *) 7270 &tpg_desc->descriptors[pc]; 7271 } 7272 mtx_unlock(&softc->ctl_lock); 7273 7274 ctl_set_success(ctsio); 7275 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7276 ctsio->be_move_done = ctl_config_move_done; 7277 ctl_datamove((union ctl_io *)ctsio); 7278 return(retval); 7279} 7280 7281int 7282ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7283{ 7284 struct ctl_lun *lun; 7285 struct scsi_report_supported_opcodes *cdb; 7286 const struct ctl_cmd_entry *entry, *sentry; 7287 struct scsi_report_supported_opcodes_all *all; 7288 struct scsi_report_supported_opcodes_descr *descr; 7289 struct scsi_report_supported_opcodes_one *one; 7290 int retval; 7291 int alloc_len, total_len; 7292 int opcode, service_action, i, j, num; 7293 7294 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7295 7296 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7298 7299 retval = CTL_RETVAL_COMPLETE; 7300 7301 opcode = cdb->requested_opcode; 7302 service_action = scsi_2btoul(cdb->requested_service_action); 7303 switch (cdb->options & RSO_OPTIONS_MASK) { 7304 case RSO_OPTIONS_ALL: 7305 num = 0; 7306 for (i = 0; i < 256; i++) { 7307 entry = &ctl_cmd_table[i]; 7308 if (entry->flags & CTL_CMD_FLAG_SA5) { 7309 for (j = 0; j < 32; j++) { 7310 sentry = &((const struct ctl_cmd_entry *) 7311 entry->execute)[j]; 7312 if (ctl_cmd_applicable( 7313 lun->be_lun->lun_type, sentry)) 7314 num++; 7315 } 7316 } else { 7317 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7318 entry)) 7319 num++; 7320 } 7321 } 7322 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7323 num * sizeof(struct scsi_report_supported_opcodes_descr); 7324 break; 7325 case RSO_OPTIONS_OC: 7326 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7327 ctl_set_invalid_field(/*ctsio*/ ctsio, 7328 /*sks_valid*/ 1, 7329 /*command*/ 1, 7330 /*field*/ 2, 7331 /*bit_valid*/ 1, 7332 /*bit*/ 2); 7333 ctl_done((union ctl_io *)ctsio); 7334 return (CTL_RETVAL_COMPLETE); 7335 } 7336 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7337 break; 7338 case RSO_OPTIONS_OC_SA: 7339 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7340 service_action >= 32) { 7341 ctl_set_invalid_field(/*ctsio*/ ctsio, 7342 /*sks_valid*/ 1, 7343 /*command*/ 1, 7344 /*field*/ 2, 7345 /*bit_valid*/ 1, 7346 /*bit*/ 2); 7347 ctl_done((union ctl_io *)ctsio); 7348 return (CTL_RETVAL_COMPLETE); 7349 } 7350 /* FALLTHROUGH */ 7351 case RSO_OPTIONS_OC_ASA: 7352 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7353 break; 7354 default: 7355 ctl_set_invalid_field(/*ctsio*/ ctsio, 7356 /*sks_valid*/ 1, 7357 /*command*/ 1, 7358 /*field*/ 2, 7359 /*bit_valid*/ 1, 7360 /*bit*/ 2); 7361 ctl_done((union ctl_io *)ctsio); 7362 return (CTL_RETVAL_COMPLETE); 7363 } 7364 7365 alloc_len = scsi_4btoul(cdb->length); 7366 7367 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7368 7369 ctsio->kern_sg_entries = 0; 7370 7371 if (total_len < alloc_len) { 7372 ctsio->residual = alloc_len - total_len; 7373 ctsio->kern_data_len = total_len; 7374 ctsio->kern_total_len = total_len; 7375 } else { 7376 ctsio->residual = 0; 7377 ctsio->kern_data_len = alloc_len; 7378 ctsio->kern_total_len = alloc_len; 7379 } 7380 ctsio->kern_data_resid = 0; 7381 ctsio->kern_rel_offset = 0; 7382 7383 switch (cdb->options & RSO_OPTIONS_MASK) { 7384 case RSO_OPTIONS_ALL: 7385 all = (struct scsi_report_supported_opcodes_all *) 7386 ctsio->kern_data_ptr; 7387 num = 0; 7388 for (i = 0; i < 256; i++) { 7389 entry = &ctl_cmd_table[i]; 7390 if (entry->flags & CTL_CMD_FLAG_SA5) { 7391 for (j = 0; j < 32; j++) { 7392 sentry = &((const struct ctl_cmd_entry *) 7393 entry->execute)[j]; 7394 if (!ctl_cmd_applicable( 7395 lun->be_lun->lun_type, sentry)) 7396 continue; 7397 descr = &all->descr[num++]; 7398 descr->opcode = i; 7399 scsi_ulto2b(j, descr->service_action); 7400 descr->flags = RSO_SERVACTV; 7401 scsi_ulto2b(sentry->length, 7402 descr->cdb_length); 7403 } 7404 } else { 7405 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7406 entry)) 7407 continue; 7408 descr = &all->descr[num++]; 7409 descr->opcode = i; 7410 scsi_ulto2b(0, descr->service_action); 7411 descr->flags = 0; 7412 scsi_ulto2b(entry->length, descr->cdb_length); 7413 } 7414 } 7415 scsi_ulto4b( 7416 num * sizeof(struct scsi_report_supported_opcodes_descr), 7417 all->length); 7418 break; 7419 case RSO_OPTIONS_OC: 7420 one = (struct scsi_report_supported_opcodes_one *) 7421 ctsio->kern_data_ptr; 7422 entry = &ctl_cmd_table[opcode]; 7423 goto fill_one; 7424 case RSO_OPTIONS_OC_SA: 7425 one = (struct scsi_report_supported_opcodes_one *) 7426 ctsio->kern_data_ptr; 7427 entry = &ctl_cmd_table[opcode]; 7428 entry = &((const struct ctl_cmd_entry *) 7429 entry->execute)[service_action]; 7430fill_one: 7431 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7432 one->support = 3; 7433 scsi_ulto2b(entry->length, one->cdb_length); 7434 one->cdb_usage[0] = opcode; 7435 memcpy(&one->cdb_usage[1], entry->usage, 7436 entry->length - 1); 7437 } else 7438 one->support = 1; 7439 break; 7440 case RSO_OPTIONS_OC_ASA: 7441 one = (struct scsi_report_supported_opcodes_one *) 7442 ctsio->kern_data_ptr; 7443 entry = &ctl_cmd_table[opcode]; 7444 if (entry->flags & CTL_CMD_FLAG_SA5) { 7445 entry = &((const struct ctl_cmd_entry *) 7446 entry->execute)[service_action]; 7447 } else if (service_action != 0) { 7448 one->support = 1; 7449 break; 7450 } 7451 goto fill_one; 7452 } 7453 7454 ctl_set_success(ctsio); 7455 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7456 ctsio->be_move_done = ctl_config_move_done; 7457 ctl_datamove((union ctl_io *)ctsio); 7458 return(retval); 7459} 7460 7461int 7462ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7463{ 7464 struct scsi_report_supported_tmf *cdb; 7465 struct scsi_report_supported_tmf_ext_data *data; 7466 int retval; 7467 int alloc_len, total_len; 7468 7469 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7470 7471 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7472 7473 retval = CTL_RETVAL_COMPLETE; 7474 7475 if (cdb->options & RST_REPD) 7476 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7477 else 7478 total_len = sizeof(struct scsi_report_supported_tmf_data); 7479 alloc_len = scsi_4btoul(cdb->length); 7480 7481 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7482 7483 ctsio->kern_sg_entries = 0; 7484 7485 if (total_len < alloc_len) { 7486 ctsio->residual = alloc_len - total_len; 7487 ctsio->kern_data_len = total_len; 7488 ctsio->kern_total_len = total_len; 7489 } else { 7490 ctsio->residual = 0; 7491 ctsio->kern_data_len = alloc_len; 7492 ctsio->kern_total_len = alloc_len; 7493 } 7494 ctsio->kern_data_resid = 0; 7495 ctsio->kern_rel_offset = 0; 7496 7497 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7498 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7499 RST_TRS; 7500 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7501 data->length = total_len - 4; 7502 7503 ctl_set_success(ctsio); 7504 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7505 ctsio->be_move_done = ctl_config_move_done; 7506 ctl_datamove((union ctl_io *)ctsio); 7507 return (retval); 7508} 7509 7510int 7511ctl_report_timestamp(struct ctl_scsiio *ctsio) 7512{ 7513 struct scsi_report_timestamp *cdb; 7514 struct scsi_report_timestamp_data *data; 7515 struct timeval tv; 7516 int64_t timestamp; 7517 int retval; 7518 int alloc_len, total_len; 7519 7520 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7521 7522 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7523 7524 retval = CTL_RETVAL_COMPLETE; 7525 7526 total_len = sizeof(struct scsi_report_timestamp_data); 7527 alloc_len = scsi_4btoul(cdb->length); 7528 7529 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7530 7531 ctsio->kern_sg_entries = 0; 7532 7533 if (total_len < alloc_len) { 7534 ctsio->residual = alloc_len - total_len; 7535 ctsio->kern_data_len = total_len; 7536 ctsio->kern_total_len = total_len; 7537 } else { 7538 ctsio->residual = 0; 7539 ctsio->kern_data_len = alloc_len; 7540 ctsio->kern_total_len = alloc_len; 7541 } 7542 ctsio->kern_data_resid = 0; 7543 ctsio->kern_rel_offset = 0; 7544 7545 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7546 scsi_ulto2b(sizeof(*data) - 2, data->length); 7547 data->origin = RTS_ORIG_OUTSIDE; 7548 getmicrotime(&tv); 7549 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7550 scsi_ulto4b(timestamp >> 16, data->timestamp); 7551 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7552 7553 ctl_set_success(ctsio); 7554 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7555 ctsio->be_move_done = ctl_config_move_done; 7556 ctl_datamove((union ctl_io *)ctsio); 7557 return (retval); 7558} 7559 7560int 7561ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7562{ 7563 struct scsi_per_res_in *cdb; 7564 int alloc_len, total_len = 0; 7565 /* struct scsi_per_res_in_rsrv in_data; */ 7566 struct ctl_lun *lun; 7567 struct ctl_softc *softc; 7568 uint64_t key; 7569 7570 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7571 7572 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7573 7574 alloc_len = scsi_2btoul(cdb->length); 7575 7576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7577 softc = lun->ctl_softc; 7578 7579retry: 7580 mtx_lock(&lun->lun_lock); 7581 switch (cdb->action) { 7582 case SPRI_RK: /* read keys */ 7583 total_len = sizeof(struct scsi_per_res_in_keys) + 7584 lun->pr_key_count * 7585 sizeof(struct scsi_per_res_key); 7586 break; 7587 case SPRI_RR: /* read reservation */ 7588 if (lun->flags & CTL_LUN_PR_RESERVED) 7589 total_len = sizeof(struct scsi_per_res_in_rsrv); 7590 else 7591 total_len = sizeof(struct scsi_per_res_in_header); 7592 break; 7593 case SPRI_RC: /* report capabilities */ 7594 total_len = sizeof(struct scsi_per_res_cap); 7595 break; 7596 case SPRI_RS: /* read full status */ 7597 total_len = sizeof(struct scsi_per_res_in_header) + 7598 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7599 lun->pr_key_count; 7600 break; 7601 default: 7602 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7603 } 7604 mtx_unlock(&lun->lun_lock); 7605 7606 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7607 7608 if (total_len < alloc_len) { 7609 ctsio->residual = alloc_len - total_len; 7610 ctsio->kern_data_len = total_len; 7611 ctsio->kern_total_len = total_len; 7612 } else { 7613 ctsio->residual = 0; 7614 ctsio->kern_data_len = alloc_len; 7615 ctsio->kern_total_len = alloc_len; 7616 } 7617 7618 ctsio->kern_data_resid = 0; 7619 ctsio->kern_rel_offset = 0; 7620 ctsio->kern_sg_entries = 0; 7621 7622 mtx_lock(&lun->lun_lock); 7623 switch (cdb->action) { 7624 case SPRI_RK: { // read keys 7625 struct scsi_per_res_in_keys *res_keys; 7626 int i, key_count; 7627 7628 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7629 7630 /* 7631 * We had to drop the lock to allocate our buffer, which 7632 * leaves time for someone to come in with another 7633 * persistent reservation. (That is unlikely, though, 7634 * since this should be the only persistent reservation 7635 * command active right now.) 7636 */ 7637 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7638 (lun->pr_key_count * 7639 sizeof(struct scsi_per_res_key)))){ 7640 mtx_unlock(&lun->lun_lock); 7641 free(ctsio->kern_data_ptr, M_CTL); 7642 printf("%s: reservation length changed, retrying\n", 7643 __func__); 7644 goto retry; 7645 } 7646 7647 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7648 7649 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7650 lun->pr_key_count, res_keys->header.length); 7651 7652 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7653 if ((key = ctl_get_prkey(lun, i)) == 0) 7654 continue; 7655 7656 /* 7657 * We used lun->pr_key_count to calculate the 7658 * size to allocate. If it turns out the number of 7659 * initiators with the registered flag set is 7660 * larger than that (i.e. they haven't been kept in 7661 * sync), we've got a problem. 7662 */ 7663 if (key_count >= lun->pr_key_count) { 7664 key_count++; 7665 continue; 7666 } 7667 scsi_u64to8b(key, res_keys->keys[key_count].key); 7668 key_count++; 7669 } 7670 break; 7671 } 7672 case SPRI_RR: { // read reservation 7673 struct scsi_per_res_in_rsrv *res; 7674 int tmp_len, header_only; 7675 7676 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7677 7678 scsi_ulto4b(lun->pr_generation, res->header.generation); 7679 7680 if (lun->flags & CTL_LUN_PR_RESERVED) 7681 { 7682 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7683 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7684 res->header.length); 7685 header_only = 0; 7686 } else { 7687 tmp_len = sizeof(struct scsi_per_res_in_header); 7688 scsi_ulto4b(0, res->header.length); 7689 header_only = 1; 7690 } 7691 7692 /* 7693 * We had to drop the lock to allocate our buffer, which 7694 * leaves time for someone to come in with another 7695 * persistent reservation. (That is unlikely, though, 7696 * since this should be the only persistent reservation 7697 * command active right now.) 7698 */ 7699 if (tmp_len != total_len) { 7700 mtx_unlock(&lun->lun_lock); 7701 free(ctsio->kern_data_ptr, M_CTL); 7702 printf("%s: reservation status changed, retrying\n", 7703 __func__); 7704 goto retry; 7705 } 7706 7707 /* 7708 * No reservation held, so we're done. 7709 */ 7710 if (header_only != 0) 7711 break; 7712 7713 /* 7714 * If the registration is an All Registrants type, the key 7715 * is 0, since it doesn't really matter. 7716 */ 7717 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7718 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7719 res->data.reservation); 7720 } 7721 res->data.scopetype = lun->pr_res_type; 7722 break; 7723 } 7724 case SPRI_RC: //report capabilities 7725 { 7726 struct scsi_per_res_cap *res_cap; 7727 uint16_t type_mask; 7728 7729 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7730 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7731 res_cap->flags1 = SPRI_CRH; 7732 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7733 type_mask = SPRI_TM_WR_EX_AR | 7734 SPRI_TM_EX_AC_RO | 7735 SPRI_TM_WR_EX_RO | 7736 SPRI_TM_EX_AC | 7737 SPRI_TM_WR_EX | 7738 SPRI_TM_EX_AC_AR; 7739 scsi_ulto2b(type_mask, res_cap->type_mask); 7740 break; 7741 } 7742 case SPRI_RS: { // read full status 7743 struct scsi_per_res_in_full *res_status; 7744 struct scsi_per_res_in_full_desc *res_desc; 7745 struct ctl_port *port; 7746 int i, len; 7747 7748 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7749 7750 /* 7751 * We had to drop the lock to allocate our buffer, which 7752 * leaves time for someone to come in with another 7753 * persistent reservation. (That is unlikely, though, 7754 * since this should be the only persistent reservation 7755 * command active right now.) 7756 */ 7757 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7758 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7759 lun->pr_key_count)){ 7760 mtx_unlock(&lun->lun_lock); 7761 free(ctsio->kern_data_ptr, M_CTL); 7762 printf("%s: reservation length changed, retrying\n", 7763 __func__); 7764 goto retry; 7765 } 7766 7767 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7768 7769 res_desc = &res_status->desc[0]; 7770 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7771 if ((key = ctl_get_prkey(lun, i)) == 0) 7772 continue; 7773 7774 scsi_u64to8b(key, res_desc->res_key.key); 7775 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7776 (lun->pr_res_idx == i || 7777 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7778 res_desc->flags = SPRI_FULL_R_HOLDER; 7779 res_desc->scopetype = lun->pr_res_type; 7780 } 7781 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7782 res_desc->rel_trgt_port_id); 7783 len = 0; 7784 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7785 if (port != NULL) 7786 len = ctl_create_iid(port, 7787 i % CTL_MAX_INIT_PER_PORT, 7788 res_desc->transport_id); 7789 scsi_ulto4b(len, res_desc->additional_length); 7790 res_desc = (struct scsi_per_res_in_full_desc *) 7791 &res_desc->transport_id[len]; 7792 } 7793 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7794 res_status->header.length); 7795 break; 7796 } 7797 default: 7798 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7799 } 7800 mtx_unlock(&lun->lun_lock); 7801 7802 ctl_set_success(ctsio); 7803 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7804 ctsio->be_move_done = ctl_config_move_done; 7805 ctl_datamove((union ctl_io *)ctsio); 7806 return (CTL_RETVAL_COMPLETE); 7807} 7808 7809/* 7810 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7811 * it should return. 7812 */ 7813static int 7814ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7815 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7816 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7817 struct scsi_per_res_out_parms* param) 7818{ 7819 union ctl_ha_msg persis_io; 7820 int i; 7821 7822 mtx_lock(&lun->lun_lock); 7823 if (sa_res_key == 0) { 7824 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7825 /* validate scope and type */ 7826 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7827 SPR_LU_SCOPE) { 7828 mtx_unlock(&lun->lun_lock); 7829 ctl_set_invalid_field(/*ctsio*/ ctsio, 7830 /*sks_valid*/ 1, 7831 /*command*/ 1, 7832 /*field*/ 2, 7833 /*bit_valid*/ 1, 7834 /*bit*/ 4); 7835 ctl_done((union ctl_io *)ctsio); 7836 return (1); 7837 } 7838 7839 if (type>8 || type==2 || type==4 || type==0) { 7840 mtx_unlock(&lun->lun_lock); 7841 ctl_set_invalid_field(/*ctsio*/ ctsio, 7842 /*sks_valid*/ 1, 7843 /*command*/ 1, 7844 /*field*/ 2, 7845 /*bit_valid*/ 1, 7846 /*bit*/ 0); 7847 ctl_done((union ctl_io *)ctsio); 7848 return (1); 7849 } 7850 7851 /* 7852 * Unregister everybody else and build UA for 7853 * them 7854 */ 7855 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7856 if (i == residx || ctl_get_prkey(lun, i) == 0) 7857 continue; 7858 7859 ctl_clr_prkey(lun, i); 7860 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7861 } 7862 lun->pr_key_count = 1; 7863 lun->pr_res_type = type; 7864 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7865 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7866 lun->pr_res_idx = residx; 7867 lun->pr_generation++; 7868 mtx_unlock(&lun->lun_lock); 7869 7870 /* send msg to other side */ 7871 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7872 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7873 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7874 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7875 persis_io.pr.pr_info.res_type = type; 7876 memcpy(persis_io.pr.pr_info.sa_res_key, 7877 param->serv_act_res_key, 7878 sizeof(param->serv_act_res_key)); 7879 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7880 sizeof(persis_io.pr), M_WAITOK); 7881 } else { 7882 /* not all registrants */ 7883 mtx_unlock(&lun->lun_lock); 7884 free(ctsio->kern_data_ptr, M_CTL); 7885 ctl_set_invalid_field(ctsio, 7886 /*sks_valid*/ 1, 7887 /*command*/ 0, 7888 /*field*/ 8, 7889 /*bit_valid*/ 0, 7890 /*bit*/ 0); 7891 ctl_done((union ctl_io *)ctsio); 7892 return (1); 7893 } 7894 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7895 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7896 int found = 0; 7897 7898 if (res_key == sa_res_key) { 7899 /* special case */ 7900 /* 7901 * The spec implies this is not good but doesn't 7902 * say what to do. There are two choices either 7903 * generate a res conflict or check condition 7904 * with illegal field in parameter data. Since 7905 * that is what is done when the sa_res_key is 7906 * zero I'll take that approach since this has 7907 * to do with the sa_res_key. 7908 */ 7909 mtx_unlock(&lun->lun_lock); 7910 free(ctsio->kern_data_ptr, M_CTL); 7911 ctl_set_invalid_field(ctsio, 7912 /*sks_valid*/ 1, 7913 /*command*/ 0, 7914 /*field*/ 8, 7915 /*bit_valid*/ 0, 7916 /*bit*/ 0); 7917 ctl_done((union ctl_io *)ctsio); 7918 return (1); 7919 } 7920 7921 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7922 if (ctl_get_prkey(lun, i) != sa_res_key) 7923 continue; 7924 7925 found = 1; 7926 ctl_clr_prkey(lun, i); 7927 lun->pr_key_count--; 7928 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7929 } 7930 if (!found) { 7931 mtx_unlock(&lun->lun_lock); 7932 free(ctsio->kern_data_ptr, M_CTL); 7933 ctl_set_reservation_conflict(ctsio); 7934 ctl_done((union ctl_io *)ctsio); 7935 return (CTL_RETVAL_COMPLETE); 7936 } 7937 lun->pr_generation++; 7938 mtx_unlock(&lun->lun_lock); 7939 7940 /* send msg to other side */ 7941 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7942 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7943 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7944 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7945 persis_io.pr.pr_info.res_type = type; 7946 memcpy(persis_io.pr.pr_info.sa_res_key, 7947 param->serv_act_res_key, 7948 sizeof(param->serv_act_res_key)); 7949 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7950 sizeof(persis_io.pr), M_WAITOK); 7951 } else { 7952 /* Reserved but not all registrants */ 7953 /* sa_res_key is res holder */ 7954 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7955 /* validate scope and type */ 7956 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7957 SPR_LU_SCOPE) { 7958 mtx_unlock(&lun->lun_lock); 7959 ctl_set_invalid_field(/*ctsio*/ ctsio, 7960 /*sks_valid*/ 1, 7961 /*command*/ 1, 7962 /*field*/ 2, 7963 /*bit_valid*/ 1, 7964 /*bit*/ 4); 7965 ctl_done((union ctl_io *)ctsio); 7966 return (1); 7967 } 7968 7969 if (type>8 || type==2 || type==4 || type==0) { 7970 mtx_unlock(&lun->lun_lock); 7971 ctl_set_invalid_field(/*ctsio*/ ctsio, 7972 /*sks_valid*/ 1, 7973 /*command*/ 1, 7974 /*field*/ 2, 7975 /*bit_valid*/ 1, 7976 /*bit*/ 0); 7977 ctl_done((union ctl_io *)ctsio); 7978 return (1); 7979 } 7980 7981 /* 7982 * Do the following: 7983 * if sa_res_key != res_key remove all 7984 * registrants w/sa_res_key and generate UA 7985 * for these registrants(Registrations 7986 * Preempted) if it wasn't an exclusive 7987 * reservation generate UA(Reservations 7988 * Preempted) for all other registered nexuses 7989 * if the type has changed. Establish the new 7990 * reservation and holder. If res_key and 7991 * sa_res_key are the same do the above 7992 * except don't unregister the res holder. 7993 */ 7994 7995 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7996 if (i == residx || ctl_get_prkey(lun, i) == 0) 7997 continue; 7998 7999 if (sa_res_key == ctl_get_prkey(lun, i)) { 8000 ctl_clr_prkey(lun, i); 8001 lun->pr_key_count--; 8002 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8003 } else if (type != lun->pr_res_type && 8004 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8005 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8006 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8007 } 8008 } 8009 lun->pr_res_type = type; 8010 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8011 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8012 lun->pr_res_idx = residx; 8013 else 8014 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8015 lun->pr_generation++; 8016 mtx_unlock(&lun->lun_lock); 8017 8018 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8019 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8020 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8021 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8022 persis_io.pr.pr_info.res_type = type; 8023 memcpy(persis_io.pr.pr_info.sa_res_key, 8024 param->serv_act_res_key, 8025 sizeof(param->serv_act_res_key)); 8026 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8027 sizeof(persis_io.pr), M_WAITOK); 8028 } else { 8029 /* 8030 * sa_res_key is not the res holder just 8031 * remove registrants 8032 */ 8033 int found=0; 8034 8035 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8036 if (sa_res_key != ctl_get_prkey(lun, i)) 8037 continue; 8038 8039 found = 1; 8040 ctl_clr_prkey(lun, i); 8041 lun->pr_key_count--; 8042 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8043 } 8044 8045 if (!found) { 8046 mtx_unlock(&lun->lun_lock); 8047 free(ctsio->kern_data_ptr, M_CTL); 8048 ctl_set_reservation_conflict(ctsio); 8049 ctl_done((union ctl_io *)ctsio); 8050 return (1); 8051 } 8052 lun->pr_generation++; 8053 mtx_unlock(&lun->lun_lock); 8054 8055 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8056 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8057 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8058 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8059 persis_io.pr.pr_info.res_type = type; 8060 memcpy(persis_io.pr.pr_info.sa_res_key, 8061 param->serv_act_res_key, 8062 sizeof(param->serv_act_res_key)); 8063 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8064 sizeof(persis_io.pr), M_WAITOK); 8065 } 8066 } 8067 return (0); 8068} 8069 8070static void 8071ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8072{ 8073 uint64_t sa_res_key; 8074 int i; 8075 8076 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8077 8078 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8079 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8080 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8081 if (sa_res_key == 0) { 8082 /* 8083 * Unregister everybody else and build UA for 8084 * them 8085 */ 8086 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8087 if (i == msg->pr.pr_info.residx || 8088 ctl_get_prkey(lun, i) == 0) 8089 continue; 8090 8091 ctl_clr_prkey(lun, i); 8092 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8093 } 8094 8095 lun->pr_key_count = 1; 8096 lun->pr_res_type = msg->pr.pr_info.res_type; 8097 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8098 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8099 lun->pr_res_idx = msg->pr.pr_info.residx; 8100 } else { 8101 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8102 if (sa_res_key == ctl_get_prkey(lun, i)) 8103 continue; 8104 8105 ctl_clr_prkey(lun, i); 8106 lun->pr_key_count--; 8107 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8108 } 8109 } 8110 } else { 8111 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8112 if (i == msg->pr.pr_info.residx || 8113 ctl_get_prkey(lun, i) == 0) 8114 continue; 8115 8116 if (sa_res_key == ctl_get_prkey(lun, i)) { 8117 ctl_clr_prkey(lun, i); 8118 lun->pr_key_count--; 8119 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8120 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8121 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8122 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8123 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8124 } 8125 } 8126 lun->pr_res_type = msg->pr.pr_info.res_type; 8127 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8128 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8129 lun->pr_res_idx = msg->pr.pr_info.residx; 8130 else 8131 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8132 } 8133 lun->pr_generation++; 8134 8135} 8136 8137 8138int 8139ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8140{ 8141 int retval; 8142 u_int32_t param_len; 8143 struct scsi_per_res_out *cdb; 8144 struct ctl_lun *lun; 8145 struct scsi_per_res_out_parms* param; 8146 struct ctl_softc *softc; 8147 uint32_t residx; 8148 uint64_t res_key, sa_res_key, key; 8149 uint8_t type; 8150 union ctl_ha_msg persis_io; 8151 int i; 8152 8153 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8154 8155 retval = CTL_RETVAL_COMPLETE; 8156 8157 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8158 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8159 softc = lun->ctl_softc; 8160 8161 /* 8162 * We only support whole-LUN scope. The scope & type are ignored for 8163 * register, register and ignore existing key and clear. 8164 * We sometimes ignore scope and type on preempts too!! 8165 * Verify reservation type here as well. 8166 */ 8167 type = cdb->scope_type & SPR_TYPE_MASK; 8168 if ((cdb->action == SPRO_RESERVE) 8169 || (cdb->action == SPRO_RELEASE)) { 8170 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8171 ctl_set_invalid_field(/*ctsio*/ ctsio, 8172 /*sks_valid*/ 1, 8173 /*command*/ 1, 8174 /*field*/ 2, 8175 /*bit_valid*/ 1, 8176 /*bit*/ 4); 8177 ctl_done((union ctl_io *)ctsio); 8178 return (CTL_RETVAL_COMPLETE); 8179 } 8180 8181 if (type>8 || type==2 || type==4 || type==0) { 8182 ctl_set_invalid_field(/*ctsio*/ ctsio, 8183 /*sks_valid*/ 1, 8184 /*command*/ 1, 8185 /*field*/ 2, 8186 /*bit_valid*/ 1, 8187 /*bit*/ 0); 8188 ctl_done((union ctl_io *)ctsio); 8189 return (CTL_RETVAL_COMPLETE); 8190 } 8191 } 8192 8193 param_len = scsi_4btoul(cdb->length); 8194 8195 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8196 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8197 ctsio->kern_data_len = param_len; 8198 ctsio->kern_total_len = param_len; 8199 ctsio->kern_data_resid = 0; 8200 ctsio->kern_rel_offset = 0; 8201 ctsio->kern_sg_entries = 0; 8202 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8203 ctsio->be_move_done = ctl_config_move_done; 8204 ctl_datamove((union ctl_io *)ctsio); 8205 8206 return (CTL_RETVAL_COMPLETE); 8207 } 8208 8209 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8210 8211 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8212 res_key = scsi_8btou64(param->res_key.key); 8213 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8214 8215 /* 8216 * Validate the reservation key here except for SPRO_REG_IGNO 8217 * This must be done for all other service actions 8218 */ 8219 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8220 mtx_lock(&lun->lun_lock); 8221 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8222 if (res_key != key) { 8223 /* 8224 * The current key passed in doesn't match 8225 * the one the initiator previously 8226 * registered. 8227 */ 8228 mtx_unlock(&lun->lun_lock); 8229 free(ctsio->kern_data_ptr, M_CTL); 8230 ctl_set_reservation_conflict(ctsio); 8231 ctl_done((union ctl_io *)ctsio); 8232 return (CTL_RETVAL_COMPLETE); 8233 } 8234 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8235 /* 8236 * We are not registered 8237 */ 8238 mtx_unlock(&lun->lun_lock); 8239 free(ctsio->kern_data_ptr, M_CTL); 8240 ctl_set_reservation_conflict(ctsio); 8241 ctl_done((union ctl_io *)ctsio); 8242 return (CTL_RETVAL_COMPLETE); 8243 } else if (res_key != 0) { 8244 /* 8245 * We are not registered and trying to register but 8246 * the register key isn't zero. 8247 */ 8248 mtx_unlock(&lun->lun_lock); 8249 free(ctsio->kern_data_ptr, M_CTL); 8250 ctl_set_reservation_conflict(ctsio); 8251 ctl_done((union ctl_io *)ctsio); 8252 return (CTL_RETVAL_COMPLETE); 8253 } 8254 mtx_unlock(&lun->lun_lock); 8255 } 8256 8257 switch (cdb->action & SPRO_ACTION_MASK) { 8258 case SPRO_REGISTER: 8259 case SPRO_REG_IGNO: { 8260 8261#if 0 8262 printf("Registration received\n"); 8263#endif 8264 8265 /* 8266 * We don't support any of these options, as we report in 8267 * the read capabilities request (see 8268 * ctl_persistent_reserve_in(), above). 8269 */ 8270 if ((param->flags & SPR_SPEC_I_PT) 8271 || (param->flags & SPR_ALL_TG_PT) 8272 || (param->flags & SPR_APTPL)) { 8273 int bit_ptr; 8274 8275 if (param->flags & SPR_APTPL) 8276 bit_ptr = 0; 8277 else if (param->flags & SPR_ALL_TG_PT) 8278 bit_ptr = 2; 8279 else /* SPR_SPEC_I_PT */ 8280 bit_ptr = 3; 8281 8282 free(ctsio->kern_data_ptr, M_CTL); 8283 ctl_set_invalid_field(ctsio, 8284 /*sks_valid*/ 1, 8285 /*command*/ 0, 8286 /*field*/ 20, 8287 /*bit_valid*/ 1, 8288 /*bit*/ bit_ptr); 8289 ctl_done((union ctl_io *)ctsio); 8290 return (CTL_RETVAL_COMPLETE); 8291 } 8292 8293 mtx_lock(&lun->lun_lock); 8294 8295 /* 8296 * The initiator wants to clear the 8297 * key/unregister. 8298 */ 8299 if (sa_res_key == 0) { 8300 if ((res_key == 0 8301 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8302 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8303 && ctl_get_prkey(lun, residx) == 0)) { 8304 mtx_unlock(&lun->lun_lock); 8305 goto done; 8306 } 8307 8308 ctl_clr_prkey(lun, residx); 8309 lun->pr_key_count--; 8310 8311 if (residx == lun->pr_res_idx) { 8312 lun->flags &= ~CTL_LUN_PR_RESERVED; 8313 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8314 8315 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8316 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8317 lun->pr_key_count) { 8318 /* 8319 * If the reservation is a registrants 8320 * only type we need to generate a UA 8321 * for other registered inits. The 8322 * sense code should be RESERVATIONS 8323 * RELEASED 8324 */ 8325 8326 for (i = softc->init_min; i < softc->init_max; i++){ 8327 if (ctl_get_prkey(lun, i) == 0) 8328 continue; 8329 ctl_est_ua(lun, i, 8330 CTL_UA_RES_RELEASE); 8331 } 8332 } 8333 lun->pr_res_type = 0; 8334 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8335 if (lun->pr_key_count==0) { 8336 lun->flags &= ~CTL_LUN_PR_RESERVED; 8337 lun->pr_res_type = 0; 8338 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8339 } 8340 } 8341 lun->pr_generation++; 8342 mtx_unlock(&lun->lun_lock); 8343 8344 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8345 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8346 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8347 persis_io.pr.pr_info.residx = residx; 8348 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8349 sizeof(persis_io.pr), M_WAITOK); 8350 } else /* sa_res_key != 0 */ { 8351 8352 /* 8353 * If we aren't registered currently then increment 8354 * the key count and set the registered flag. 8355 */ 8356 ctl_alloc_prkey(lun, residx); 8357 if (ctl_get_prkey(lun, residx) == 0) 8358 lun->pr_key_count++; 8359 ctl_set_prkey(lun, residx, sa_res_key); 8360 lun->pr_generation++; 8361 mtx_unlock(&lun->lun_lock); 8362 8363 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8364 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8365 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8366 persis_io.pr.pr_info.residx = residx; 8367 memcpy(persis_io.pr.pr_info.sa_res_key, 8368 param->serv_act_res_key, 8369 sizeof(param->serv_act_res_key)); 8370 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8371 sizeof(persis_io.pr), M_WAITOK); 8372 } 8373 8374 break; 8375 } 8376 case SPRO_RESERVE: 8377#if 0 8378 printf("Reserve executed type %d\n", type); 8379#endif 8380 mtx_lock(&lun->lun_lock); 8381 if (lun->flags & CTL_LUN_PR_RESERVED) { 8382 /* 8383 * if this isn't the reservation holder and it's 8384 * not a "all registrants" type or if the type is 8385 * different then we have a conflict 8386 */ 8387 if ((lun->pr_res_idx != residx 8388 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8389 || lun->pr_res_type != type) { 8390 mtx_unlock(&lun->lun_lock); 8391 free(ctsio->kern_data_ptr, M_CTL); 8392 ctl_set_reservation_conflict(ctsio); 8393 ctl_done((union ctl_io *)ctsio); 8394 return (CTL_RETVAL_COMPLETE); 8395 } 8396 mtx_unlock(&lun->lun_lock); 8397 } else /* create a reservation */ { 8398 /* 8399 * If it's not an "all registrants" type record 8400 * reservation holder 8401 */ 8402 if (type != SPR_TYPE_WR_EX_AR 8403 && type != SPR_TYPE_EX_AC_AR) 8404 lun->pr_res_idx = residx; /* Res holder */ 8405 else 8406 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8407 8408 lun->flags |= CTL_LUN_PR_RESERVED; 8409 lun->pr_res_type = type; 8410 8411 mtx_unlock(&lun->lun_lock); 8412 8413 /* send msg to other side */ 8414 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8415 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8416 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8417 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8418 persis_io.pr.pr_info.res_type = type; 8419 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8420 sizeof(persis_io.pr), M_WAITOK); 8421 } 8422 break; 8423 8424 case SPRO_RELEASE: 8425 mtx_lock(&lun->lun_lock); 8426 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8427 /* No reservation exists return good status */ 8428 mtx_unlock(&lun->lun_lock); 8429 goto done; 8430 } 8431 /* 8432 * Is this nexus a reservation holder? 8433 */ 8434 if (lun->pr_res_idx != residx 8435 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8436 /* 8437 * not a res holder return good status but 8438 * do nothing 8439 */ 8440 mtx_unlock(&lun->lun_lock); 8441 goto done; 8442 } 8443 8444 if (lun->pr_res_type != type) { 8445 mtx_unlock(&lun->lun_lock); 8446 free(ctsio->kern_data_ptr, M_CTL); 8447 ctl_set_illegal_pr_release(ctsio); 8448 ctl_done((union ctl_io *)ctsio); 8449 return (CTL_RETVAL_COMPLETE); 8450 } 8451 8452 /* okay to release */ 8453 lun->flags &= ~CTL_LUN_PR_RESERVED; 8454 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8455 lun->pr_res_type = 0; 8456 8457 /* 8458 * If this isn't an exclusive access reservation and NUAR 8459 * is not set, generate UA for all other registrants. 8460 */ 8461 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8462 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8463 for (i = softc->init_min; i < softc->init_max; i++) { 8464 if (i == residx || ctl_get_prkey(lun, i) == 0) 8465 continue; 8466 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8467 } 8468 } 8469 mtx_unlock(&lun->lun_lock); 8470 8471 /* Send msg to other side */ 8472 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8473 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8474 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8475 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8476 sizeof(persis_io.pr), M_WAITOK); 8477 break; 8478 8479 case SPRO_CLEAR: 8480 /* send msg to other side */ 8481 8482 mtx_lock(&lun->lun_lock); 8483 lun->flags &= ~CTL_LUN_PR_RESERVED; 8484 lun->pr_res_type = 0; 8485 lun->pr_key_count = 0; 8486 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8487 8488 ctl_clr_prkey(lun, residx); 8489 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8490 if (ctl_get_prkey(lun, i) != 0) { 8491 ctl_clr_prkey(lun, i); 8492 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8493 } 8494 lun->pr_generation++; 8495 mtx_unlock(&lun->lun_lock); 8496 8497 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8498 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8499 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8500 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8501 sizeof(persis_io.pr), M_WAITOK); 8502 break; 8503 8504 case SPRO_PREEMPT: 8505 case SPRO_PRE_ABO: { 8506 int nretval; 8507 8508 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8509 residx, ctsio, cdb, param); 8510 if (nretval != 0) 8511 return (CTL_RETVAL_COMPLETE); 8512 break; 8513 } 8514 default: 8515 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8516 } 8517 8518done: 8519 free(ctsio->kern_data_ptr, M_CTL); 8520 ctl_set_success(ctsio); 8521 ctl_done((union ctl_io *)ctsio); 8522 8523 return (retval); 8524} 8525 8526/* 8527 * This routine is for handling a message from the other SC pertaining to 8528 * persistent reserve out. All the error checking will have been done 8529 * so only perorming the action need be done here to keep the two 8530 * in sync. 8531 */ 8532static void 8533ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8534{ 8535 struct ctl_softc *softc = control_softc; 8536 struct ctl_lun *lun; 8537 int i; 8538 uint32_t residx, targ_lun; 8539 8540 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8541 mtx_lock(&softc->ctl_lock); 8542 if ((targ_lun >= CTL_MAX_LUNS) || 8543 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8544 mtx_unlock(&softc->ctl_lock); 8545 return; 8546 } 8547 mtx_lock(&lun->lun_lock); 8548 mtx_unlock(&softc->ctl_lock); 8549 if (lun->flags & CTL_LUN_DISABLED) { 8550 mtx_unlock(&lun->lun_lock); 8551 return; 8552 } 8553 residx = ctl_get_initindex(&msg->hdr.nexus); 8554 switch(msg->pr.pr_info.action) { 8555 case CTL_PR_REG_KEY: 8556 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8557 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8558 lun->pr_key_count++; 8559 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8560 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8561 lun->pr_generation++; 8562 break; 8563 8564 case CTL_PR_UNREG_KEY: 8565 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8566 lun->pr_key_count--; 8567 8568 /* XXX Need to see if the reservation has been released */ 8569 /* if so do we need to generate UA? */ 8570 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8571 lun->flags &= ~CTL_LUN_PR_RESERVED; 8572 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8573 8574 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8575 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8576 lun->pr_key_count) { 8577 /* 8578 * If the reservation is a registrants 8579 * only type we need to generate a UA 8580 * for other registered inits. The 8581 * sense code should be RESERVATIONS 8582 * RELEASED 8583 */ 8584 8585 for (i = softc->init_min; i < softc->init_max; i++) { 8586 if (ctl_get_prkey(lun, i) == 0) 8587 continue; 8588 8589 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8590 } 8591 } 8592 lun->pr_res_type = 0; 8593 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8594 if (lun->pr_key_count==0) { 8595 lun->flags &= ~CTL_LUN_PR_RESERVED; 8596 lun->pr_res_type = 0; 8597 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8598 } 8599 } 8600 lun->pr_generation++; 8601 break; 8602 8603 case CTL_PR_RESERVE: 8604 lun->flags |= CTL_LUN_PR_RESERVED; 8605 lun->pr_res_type = msg->pr.pr_info.res_type; 8606 lun->pr_res_idx = msg->pr.pr_info.residx; 8607 8608 break; 8609 8610 case CTL_PR_RELEASE: 8611 /* 8612 * If this isn't an exclusive access reservation and NUAR 8613 * is not set, generate UA for all other registrants. 8614 */ 8615 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8616 lun->pr_res_type != SPR_TYPE_WR_EX && 8617 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8618 for (i = softc->init_min; i < softc->init_max; i++) 8619 if (i == residx || ctl_get_prkey(lun, i) == 0) 8620 continue; 8621 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8622 } 8623 8624 lun->flags &= ~CTL_LUN_PR_RESERVED; 8625 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8626 lun->pr_res_type = 0; 8627 break; 8628 8629 case CTL_PR_PREEMPT: 8630 ctl_pro_preempt_other(lun, msg); 8631 break; 8632 case CTL_PR_CLEAR: 8633 lun->flags &= ~CTL_LUN_PR_RESERVED; 8634 lun->pr_res_type = 0; 8635 lun->pr_key_count = 0; 8636 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8637 8638 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8639 if (ctl_get_prkey(lun, i) == 0) 8640 continue; 8641 ctl_clr_prkey(lun, i); 8642 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8643 } 8644 lun->pr_generation++; 8645 break; 8646 } 8647 8648 mtx_unlock(&lun->lun_lock); 8649} 8650 8651int 8652ctl_read_write(struct ctl_scsiio *ctsio) 8653{ 8654 struct ctl_lun *lun; 8655 struct ctl_lba_len_flags *lbalen; 8656 uint64_t lba; 8657 uint32_t num_blocks; 8658 int flags, retval; 8659 int isread; 8660 8661 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8662 8663 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8664 8665 flags = 0; 8666 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8667 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8668 switch (ctsio->cdb[0]) { 8669 case READ_6: 8670 case WRITE_6: { 8671 struct scsi_rw_6 *cdb; 8672 8673 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8674 8675 lba = scsi_3btoul(cdb->addr); 8676 /* only 5 bits are valid in the most significant address byte */ 8677 lba &= 0x1fffff; 8678 num_blocks = cdb->length; 8679 /* 8680 * This is correct according to SBC-2. 8681 */ 8682 if (num_blocks == 0) 8683 num_blocks = 256; 8684 break; 8685 } 8686 case READ_10: 8687 case WRITE_10: { 8688 struct scsi_rw_10 *cdb; 8689 8690 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8691 if (cdb->byte2 & SRW10_FUA) 8692 flags |= CTL_LLF_FUA; 8693 if (cdb->byte2 & SRW10_DPO) 8694 flags |= CTL_LLF_DPO; 8695 lba = scsi_4btoul(cdb->addr); 8696 num_blocks = scsi_2btoul(cdb->length); 8697 break; 8698 } 8699 case WRITE_VERIFY_10: { 8700 struct scsi_write_verify_10 *cdb; 8701 8702 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8703 flags |= CTL_LLF_FUA; 8704 if (cdb->byte2 & SWV_DPO) 8705 flags |= CTL_LLF_DPO; 8706 lba = scsi_4btoul(cdb->addr); 8707 num_blocks = scsi_2btoul(cdb->length); 8708 break; 8709 } 8710 case READ_12: 8711 case WRITE_12: { 8712 struct scsi_rw_12 *cdb; 8713 8714 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8715 if (cdb->byte2 & SRW12_FUA) 8716 flags |= CTL_LLF_FUA; 8717 if (cdb->byte2 & SRW12_DPO) 8718 flags |= CTL_LLF_DPO; 8719 lba = scsi_4btoul(cdb->addr); 8720 num_blocks = scsi_4btoul(cdb->length); 8721 break; 8722 } 8723 case WRITE_VERIFY_12: { 8724 struct scsi_write_verify_12 *cdb; 8725 8726 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8727 flags |= CTL_LLF_FUA; 8728 if (cdb->byte2 & SWV_DPO) 8729 flags |= CTL_LLF_DPO; 8730 lba = scsi_4btoul(cdb->addr); 8731 num_blocks = scsi_4btoul(cdb->length); 8732 break; 8733 } 8734 case READ_16: 8735 case WRITE_16: { 8736 struct scsi_rw_16 *cdb; 8737 8738 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8739 if (cdb->byte2 & SRW12_FUA) 8740 flags |= CTL_LLF_FUA; 8741 if (cdb->byte2 & SRW12_DPO) 8742 flags |= CTL_LLF_DPO; 8743 lba = scsi_8btou64(cdb->addr); 8744 num_blocks = scsi_4btoul(cdb->length); 8745 break; 8746 } 8747 case WRITE_ATOMIC_16: { 8748 struct scsi_write_atomic_16 *cdb; 8749 8750 if (lun->be_lun->atomicblock == 0) { 8751 ctl_set_invalid_opcode(ctsio); 8752 ctl_done((union ctl_io *)ctsio); 8753 return (CTL_RETVAL_COMPLETE); 8754 } 8755 8756 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8757 if (cdb->byte2 & SRW12_FUA) 8758 flags |= CTL_LLF_FUA; 8759 if (cdb->byte2 & SRW12_DPO) 8760 flags |= CTL_LLF_DPO; 8761 lba = scsi_8btou64(cdb->addr); 8762 num_blocks = scsi_2btoul(cdb->length); 8763 if (num_blocks > lun->be_lun->atomicblock) { 8764 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8765 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8766 /*bit*/ 0); 8767 ctl_done((union ctl_io *)ctsio); 8768 return (CTL_RETVAL_COMPLETE); 8769 } 8770 break; 8771 } 8772 case WRITE_VERIFY_16: { 8773 struct scsi_write_verify_16 *cdb; 8774 8775 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8776 flags |= CTL_LLF_FUA; 8777 if (cdb->byte2 & SWV_DPO) 8778 flags |= CTL_LLF_DPO; 8779 lba = scsi_8btou64(cdb->addr); 8780 num_blocks = scsi_4btoul(cdb->length); 8781 break; 8782 } 8783 default: 8784 /* 8785 * We got a command we don't support. This shouldn't 8786 * happen, commands should be filtered out above us. 8787 */ 8788 ctl_set_invalid_opcode(ctsio); 8789 ctl_done((union ctl_io *)ctsio); 8790 8791 return (CTL_RETVAL_COMPLETE); 8792 break; /* NOTREACHED */ 8793 } 8794 8795 /* 8796 * The first check is to make sure we're in bounds, the second 8797 * check is to catch wrap-around problems. If the lba + num blocks 8798 * is less than the lba, then we've wrapped around and the block 8799 * range is invalid anyway. 8800 */ 8801 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8802 || ((lba + num_blocks) < lba)) { 8803 ctl_set_lba_out_of_range(ctsio, 8804 MAX(lba, lun->be_lun->maxlba + 1)); 8805 ctl_done((union ctl_io *)ctsio); 8806 return (CTL_RETVAL_COMPLETE); 8807 } 8808 8809 /* 8810 * According to SBC-3, a transfer length of 0 is not an error. 8811 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8812 * translates to 256 blocks for those commands. 8813 */ 8814 if (num_blocks == 0) { 8815 ctl_set_success(ctsio); 8816 ctl_done((union ctl_io *)ctsio); 8817 return (CTL_RETVAL_COMPLETE); 8818 } 8819 8820 /* Set FUA and/or DPO if caches are disabled. */ 8821 if (isread) { 8822 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8823 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8824 } else { 8825 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8826 flags |= CTL_LLF_FUA; 8827 } 8828 8829 lbalen = (struct ctl_lba_len_flags *) 8830 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8831 lbalen->lba = lba; 8832 lbalen->len = num_blocks; 8833 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8834 8835 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8836 ctsio->kern_rel_offset = 0; 8837 8838 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8839 8840 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8841 return (retval); 8842} 8843 8844static int 8845ctl_cnw_cont(union ctl_io *io) 8846{ 8847 struct ctl_scsiio *ctsio; 8848 struct ctl_lun *lun; 8849 struct ctl_lba_len_flags *lbalen; 8850 int retval; 8851 8852 ctsio = &io->scsiio; 8853 ctsio->io_hdr.status = CTL_STATUS_NONE; 8854 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8855 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8856 lbalen = (struct ctl_lba_len_flags *) 8857 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8858 lbalen->flags &= ~CTL_LLF_COMPARE; 8859 lbalen->flags |= CTL_LLF_WRITE; 8860 8861 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8862 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8863 return (retval); 8864} 8865 8866int 8867ctl_cnw(struct ctl_scsiio *ctsio) 8868{ 8869 struct ctl_lun *lun; 8870 struct ctl_lba_len_flags *lbalen; 8871 uint64_t lba; 8872 uint32_t num_blocks; 8873 int flags, retval; 8874 8875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8876 8877 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8878 8879 flags = 0; 8880 switch (ctsio->cdb[0]) { 8881 case COMPARE_AND_WRITE: { 8882 struct scsi_compare_and_write *cdb; 8883 8884 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8885 if (cdb->byte2 & SRW10_FUA) 8886 flags |= CTL_LLF_FUA; 8887 if (cdb->byte2 & SRW10_DPO) 8888 flags |= CTL_LLF_DPO; 8889 lba = scsi_8btou64(cdb->addr); 8890 num_blocks = cdb->length; 8891 break; 8892 } 8893 default: 8894 /* 8895 * We got a command we don't support. This shouldn't 8896 * happen, commands should be filtered out above us. 8897 */ 8898 ctl_set_invalid_opcode(ctsio); 8899 ctl_done((union ctl_io *)ctsio); 8900 8901 return (CTL_RETVAL_COMPLETE); 8902 break; /* NOTREACHED */ 8903 } 8904 8905 /* 8906 * The first check is to make sure we're in bounds, the second 8907 * check is to catch wrap-around problems. If the lba + num blocks 8908 * is less than the lba, then we've wrapped around and the block 8909 * range is invalid anyway. 8910 */ 8911 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8912 || ((lba + num_blocks) < lba)) { 8913 ctl_set_lba_out_of_range(ctsio, 8914 MAX(lba, lun->be_lun->maxlba + 1)); 8915 ctl_done((union ctl_io *)ctsio); 8916 return (CTL_RETVAL_COMPLETE); 8917 } 8918 8919 /* 8920 * According to SBC-3, a transfer length of 0 is not an error. 8921 */ 8922 if (num_blocks == 0) { 8923 ctl_set_success(ctsio); 8924 ctl_done((union ctl_io *)ctsio); 8925 return (CTL_RETVAL_COMPLETE); 8926 } 8927 8928 /* Set FUA if write cache is disabled. */ 8929 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8930 flags |= CTL_LLF_FUA; 8931 8932 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8933 ctsio->kern_rel_offset = 0; 8934 8935 /* 8936 * Set the IO_CONT flag, so that if this I/O gets passed to 8937 * ctl_data_submit_done(), it'll get passed back to 8938 * ctl_ctl_cnw_cont() for further processing. 8939 */ 8940 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8941 ctsio->io_cont = ctl_cnw_cont; 8942 8943 lbalen = (struct ctl_lba_len_flags *) 8944 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8945 lbalen->lba = lba; 8946 lbalen->len = num_blocks; 8947 lbalen->flags = CTL_LLF_COMPARE | flags; 8948 8949 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8950 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8951 return (retval); 8952} 8953 8954int 8955ctl_verify(struct ctl_scsiio *ctsio) 8956{ 8957 struct ctl_lun *lun; 8958 struct ctl_lba_len_flags *lbalen; 8959 uint64_t lba; 8960 uint32_t num_blocks; 8961 int bytchk, flags; 8962 int retval; 8963 8964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8965 8966 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8967 8968 bytchk = 0; 8969 flags = CTL_LLF_FUA; 8970 switch (ctsio->cdb[0]) { 8971 case VERIFY_10: { 8972 struct scsi_verify_10 *cdb; 8973 8974 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8975 if (cdb->byte2 & SVFY_BYTCHK) 8976 bytchk = 1; 8977 if (cdb->byte2 & SVFY_DPO) 8978 flags |= CTL_LLF_DPO; 8979 lba = scsi_4btoul(cdb->addr); 8980 num_blocks = scsi_2btoul(cdb->length); 8981 break; 8982 } 8983 case VERIFY_12: { 8984 struct scsi_verify_12 *cdb; 8985 8986 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8987 if (cdb->byte2 & SVFY_BYTCHK) 8988 bytchk = 1; 8989 if (cdb->byte2 & SVFY_DPO) 8990 flags |= CTL_LLF_DPO; 8991 lba = scsi_4btoul(cdb->addr); 8992 num_blocks = scsi_4btoul(cdb->length); 8993 break; 8994 } 8995 case VERIFY_16: { 8996 struct scsi_rw_16 *cdb; 8997 8998 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8999 if (cdb->byte2 & SVFY_BYTCHK) 9000 bytchk = 1; 9001 if (cdb->byte2 & SVFY_DPO) 9002 flags |= CTL_LLF_DPO; 9003 lba = scsi_8btou64(cdb->addr); 9004 num_blocks = scsi_4btoul(cdb->length); 9005 break; 9006 } 9007 default: 9008 /* 9009 * We got a command we don't support. This shouldn't 9010 * happen, commands should be filtered out above us. 9011 */ 9012 ctl_set_invalid_opcode(ctsio); 9013 ctl_done((union ctl_io *)ctsio); 9014 return (CTL_RETVAL_COMPLETE); 9015 } 9016 9017 /* 9018 * The first check is to make sure we're in bounds, the second 9019 * check is to catch wrap-around problems. If the lba + num blocks 9020 * is less than the lba, then we've wrapped around and the block 9021 * range is invalid anyway. 9022 */ 9023 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9024 || ((lba + num_blocks) < lba)) { 9025 ctl_set_lba_out_of_range(ctsio, 9026 MAX(lba, lun->be_lun->maxlba + 1)); 9027 ctl_done((union ctl_io *)ctsio); 9028 return (CTL_RETVAL_COMPLETE); 9029 } 9030 9031 /* 9032 * According to SBC-3, a transfer length of 0 is not an error. 9033 */ 9034 if (num_blocks == 0) { 9035 ctl_set_success(ctsio); 9036 ctl_done((union ctl_io *)ctsio); 9037 return (CTL_RETVAL_COMPLETE); 9038 } 9039 9040 lbalen = (struct ctl_lba_len_flags *) 9041 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9042 lbalen->lba = lba; 9043 lbalen->len = num_blocks; 9044 if (bytchk) { 9045 lbalen->flags = CTL_LLF_COMPARE | flags; 9046 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9047 } else { 9048 lbalen->flags = CTL_LLF_VERIFY | flags; 9049 ctsio->kern_total_len = 0; 9050 } 9051 ctsio->kern_rel_offset = 0; 9052 9053 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9054 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9055 return (retval); 9056} 9057 9058int 9059ctl_report_luns(struct ctl_scsiio *ctsio) 9060{ 9061 struct ctl_softc *softc; 9062 struct scsi_report_luns *cdb; 9063 struct scsi_report_luns_data *lun_data; 9064 struct ctl_lun *lun, *request_lun; 9065 struct ctl_port *port; 9066 int num_luns, retval; 9067 uint32_t alloc_len, lun_datalen; 9068 int num_filled; 9069 uint32_t initidx, targ_lun_id, lun_id; 9070 9071 retval = CTL_RETVAL_COMPLETE; 9072 cdb = (struct scsi_report_luns *)ctsio->cdb; 9073 port = ctl_io_port(&ctsio->io_hdr); 9074 softc = port->ctl_softc; 9075 9076 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9077 9078 mtx_lock(&softc->ctl_lock); 9079 num_luns = 0; 9080 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9081 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9082 num_luns++; 9083 } 9084 mtx_unlock(&softc->ctl_lock); 9085 9086 switch (cdb->select_report) { 9087 case RPL_REPORT_DEFAULT: 9088 case RPL_REPORT_ALL: 9089 case RPL_REPORT_NONSUBSID: 9090 break; 9091 case RPL_REPORT_WELLKNOWN: 9092 case RPL_REPORT_ADMIN: 9093 case RPL_REPORT_CONGLOM: 9094 num_luns = 0; 9095 break; 9096 default: 9097 ctl_set_invalid_field(ctsio, 9098 /*sks_valid*/ 1, 9099 /*command*/ 1, 9100 /*field*/ 2, 9101 /*bit_valid*/ 0, 9102 /*bit*/ 0); 9103 ctl_done((union ctl_io *)ctsio); 9104 return (retval); 9105 break; /* NOTREACHED */ 9106 } 9107 9108 alloc_len = scsi_4btoul(cdb->length); 9109 /* 9110 * The initiator has to allocate at least 16 bytes for this request, 9111 * so he can at least get the header and the first LUN. Otherwise 9112 * we reject the request (per SPC-3 rev 14, section 6.21). 9113 */ 9114 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9115 sizeof(struct scsi_report_luns_lundata))) { 9116 ctl_set_invalid_field(ctsio, 9117 /*sks_valid*/ 1, 9118 /*command*/ 1, 9119 /*field*/ 6, 9120 /*bit_valid*/ 0, 9121 /*bit*/ 0); 9122 ctl_done((union ctl_io *)ctsio); 9123 return (retval); 9124 } 9125 9126 request_lun = (struct ctl_lun *) 9127 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9128 9129 lun_datalen = sizeof(*lun_data) + 9130 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9131 9132 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9133 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9134 ctsio->kern_sg_entries = 0; 9135 9136 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9137 9138 mtx_lock(&softc->ctl_lock); 9139 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9140 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9141 if (lun_id >= CTL_MAX_LUNS) 9142 continue; 9143 lun = softc->ctl_luns[lun_id]; 9144 if (lun == NULL) 9145 continue; 9146 9147 be64enc(lun_data->luns[num_filled++].lundata, 9148 ctl_encode_lun(targ_lun_id)); 9149 9150 /* 9151 * According to SPC-3, rev 14 section 6.21: 9152 * 9153 * "The execution of a REPORT LUNS command to any valid and 9154 * installed logical unit shall clear the REPORTED LUNS DATA 9155 * HAS CHANGED unit attention condition for all logical 9156 * units of that target with respect to the requesting 9157 * initiator. A valid and installed logical unit is one 9158 * having a PERIPHERAL QUALIFIER of 000b in the standard 9159 * INQUIRY data (see 6.4.2)." 9160 * 9161 * If request_lun is NULL, the LUN this report luns command 9162 * was issued to is either disabled or doesn't exist. In that 9163 * case, we shouldn't clear any pending lun change unit 9164 * attention. 9165 */ 9166 if (request_lun != NULL) { 9167 mtx_lock(&lun->lun_lock); 9168 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9169 mtx_unlock(&lun->lun_lock); 9170 } 9171 } 9172 mtx_unlock(&softc->ctl_lock); 9173 9174 /* 9175 * It's quite possible that we've returned fewer LUNs than we allocated 9176 * space for. Trim it. 9177 */ 9178 lun_datalen = sizeof(*lun_data) + 9179 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9180 9181 if (lun_datalen < alloc_len) { 9182 ctsio->residual = alloc_len - lun_datalen; 9183 ctsio->kern_data_len = lun_datalen; 9184 ctsio->kern_total_len = lun_datalen; 9185 } else { 9186 ctsio->residual = 0; 9187 ctsio->kern_data_len = alloc_len; 9188 ctsio->kern_total_len = alloc_len; 9189 } 9190 ctsio->kern_data_resid = 0; 9191 ctsio->kern_rel_offset = 0; 9192 ctsio->kern_sg_entries = 0; 9193 9194 /* 9195 * We set this to the actual data length, regardless of how much 9196 * space we actually have to return results. If the user looks at 9197 * this value, he'll know whether or not he allocated enough space 9198 * and reissue the command if necessary. We don't support well 9199 * known logical units, so if the user asks for that, return none. 9200 */ 9201 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9202 9203 /* 9204 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9205 * this request. 9206 */ 9207 ctl_set_success(ctsio); 9208 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9209 ctsio->be_move_done = ctl_config_move_done; 9210 ctl_datamove((union ctl_io *)ctsio); 9211 return (retval); 9212} 9213 9214int 9215ctl_request_sense(struct ctl_scsiio *ctsio) 9216{ 9217 struct scsi_request_sense *cdb; 9218 struct scsi_sense_data *sense_ptr; 9219 struct ctl_softc *softc; 9220 struct ctl_lun *lun; 9221 uint32_t initidx; 9222 int have_error; 9223 scsi_sense_data_type sense_format; 9224 ctl_ua_type ua_type; 9225 uint8_t asc = 0, ascq = 0; 9226 9227 cdb = (struct scsi_request_sense *)ctsio->cdb; 9228 9229 softc = control_softc; 9230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9231 9232 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9233 9234 /* 9235 * Determine which sense format the user wants. 9236 */ 9237 if (cdb->byte2 & SRS_DESC) 9238 sense_format = SSD_TYPE_DESC; 9239 else 9240 sense_format = SSD_TYPE_FIXED; 9241 9242 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9243 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9244 ctsio->kern_sg_entries = 0; 9245 9246 /* 9247 * struct scsi_sense_data, which is currently set to 256 bytes, is 9248 * larger than the largest allowed value for the length field in the 9249 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9250 */ 9251 ctsio->residual = 0; 9252 ctsio->kern_data_len = cdb->length; 9253 ctsio->kern_total_len = cdb->length; 9254 9255 ctsio->kern_data_resid = 0; 9256 ctsio->kern_rel_offset = 0; 9257 ctsio->kern_sg_entries = 0; 9258 9259 /* 9260 * If we don't have a LUN, we don't have any pending sense. 9261 */ 9262 if (lun == NULL || 9263 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9264 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9265 /* "Logical unit not supported" */ 9266 ctl_set_sense_data(sense_ptr, NULL, sense_format, 9267 /*current_error*/ 1, 9268 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9269 /*asc*/ 0x25, 9270 /*ascq*/ 0x00, 9271 SSD_ELEM_NONE); 9272 goto send; 9273 } 9274 9275 have_error = 0; 9276 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9277 /* 9278 * Check for pending sense, and then for pending unit attentions. 9279 * Pending sense gets returned first, then pending unit attentions. 9280 */ 9281 mtx_lock(&lun->lun_lock); 9282#ifdef CTL_WITH_CA 9283 if (ctl_is_set(lun->have_ca, initidx)) { 9284 scsi_sense_data_type stored_format; 9285 9286 /* 9287 * Check to see which sense format was used for the stored 9288 * sense data. 9289 */ 9290 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9291 9292 /* 9293 * If the user requested a different sense format than the 9294 * one we stored, then we need to convert it to the other 9295 * format. If we're going from descriptor to fixed format 9296 * sense data, we may lose things in translation, depending 9297 * on what options were used. 9298 * 9299 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9300 * for some reason we'll just copy it out as-is. 9301 */ 9302 if ((stored_format == SSD_TYPE_FIXED) 9303 && (sense_format == SSD_TYPE_DESC)) 9304 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9305 &lun->pending_sense[initidx], 9306 (struct scsi_sense_data_desc *)sense_ptr); 9307 else if ((stored_format == SSD_TYPE_DESC) 9308 && (sense_format == SSD_TYPE_FIXED)) 9309 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9310 &lun->pending_sense[initidx], 9311 (struct scsi_sense_data_fixed *)sense_ptr); 9312 else 9313 memcpy(sense_ptr, &lun->pending_sense[initidx], 9314 MIN(sizeof(*sense_ptr), 9315 sizeof(lun->pending_sense[initidx]))); 9316 9317 ctl_clear_mask(lun->have_ca, initidx); 9318 have_error = 1; 9319 } else 9320#endif 9321 if (have_error == 0) { 9322 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9323 if (ua_type != CTL_UA_NONE) 9324 have_error = 1; 9325 } 9326 if (have_error == 0) { 9327 /* 9328 * Report informational exception if have one and allowed. 9329 */ 9330 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9331 asc = lun->ie_asc; 9332 ascq = lun->ie_ascq; 9333 } 9334 ctl_set_sense_data(sense_ptr, lun, sense_format, 9335 /*current_error*/ 1, 9336 /*sense_key*/ SSD_KEY_NO_SENSE, 9337 /*asc*/ asc, 9338 /*ascq*/ ascq, 9339 SSD_ELEM_NONE); 9340 } 9341 mtx_unlock(&lun->lun_lock); 9342 9343send: 9344 /* 9345 * We report the SCSI status as OK, since the status of the command 9346 * itself is OK. We're reporting sense as parameter data. 9347 */ 9348 ctl_set_success(ctsio); 9349 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9350 ctsio->be_move_done = ctl_config_move_done; 9351 ctl_datamove((union ctl_io *)ctsio); 9352 return (CTL_RETVAL_COMPLETE); 9353} 9354 9355int 9356ctl_tur(struct ctl_scsiio *ctsio) 9357{ 9358 9359 CTL_DEBUG_PRINT(("ctl_tur\n")); 9360 9361 ctl_set_success(ctsio); 9362 ctl_done((union ctl_io *)ctsio); 9363 9364 return (CTL_RETVAL_COMPLETE); 9365} 9366 9367/* 9368 * SCSI VPD page 0x00, the Supported VPD Pages page. 9369 */ 9370static int 9371ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9372{ 9373 struct scsi_vpd_supported_pages *pages; 9374 int sup_page_size; 9375 struct ctl_lun *lun; 9376 int p; 9377 9378 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9379 9380 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9381 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9382 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9383 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9384 ctsio->kern_sg_entries = 0; 9385 9386 if (sup_page_size < alloc_len) { 9387 ctsio->residual = alloc_len - sup_page_size; 9388 ctsio->kern_data_len = sup_page_size; 9389 ctsio->kern_total_len = sup_page_size; 9390 } else { 9391 ctsio->residual = 0; 9392 ctsio->kern_data_len = alloc_len; 9393 ctsio->kern_total_len = alloc_len; 9394 } 9395 ctsio->kern_data_resid = 0; 9396 ctsio->kern_rel_offset = 0; 9397 ctsio->kern_sg_entries = 0; 9398 9399 /* 9400 * The control device is always connected. The disk device, on the 9401 * other hand, may not be online all the time. Need to change this 9402 * to figure out whether the disk device is actually online or not. 9403 */ 9404 if (lun != NULL) 9405 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9406 lun->be_lun->lun_type; 9407 else 9408 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9409 9410 p = 0; 9411 /* Supported VPD pages */ 9412 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9413 /* Serial Number */ 9414 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9415 /* Device Identification */ 9416 pages->page_list[p++] = SVPD_DEVICE_ID; 9417 /* Extended INQUIRY Data */ 9418 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9419 /* Mode Page Policy */ 9420 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9421 /* SCSI Ports */ 9422 pages->page_list[p++] = SVPD_SCSI_PORTS; 9423 /* Third-party Copy */ 9424 pages->page_list[p++] = SVPD_SCSI_TPC; 9425 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9426 /* Block limits */ 9427 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9428 /* Block Device Characteristics */ 9429 pages->page_list[p++] = SVPD_BDC; 9430 /* Logical Block Provisioning */ 9431 pages->page_list[p++] = SVPD_LBP; 9432 } 9433 pages->length = p; 9434 9435 ctl_set_success(ctsio); 9436 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9437 ctsio->be_move_done = ctl_config_move_done; 9438 ctl_datamove((union ctl_io *)ctsio); 9439 return (CTL_RETVAL_COMPLETE); 9440} 9441 9442/* 9443 * SCSI VPD page 0x80, the Unit Serial Number page. 9444 */ 9445static int 9446ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9447{ 9448 struct scsi_vpd_unit_serial_number *sn_ptr; 9449 struct ctl_lun *lun; 9450 int data_len; 9451 9452 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9453 9454 data_len = 4 + CTL_SN_LEN; 9455 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9456 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9457 if (data_len < alloc_len) { 9458 ctsio->residual = alloc_len - data_len; 9459 ctsio->kern_data_len = data_len; 9460 ctsio->kern_total_len = data_len; 9461 } else { 9462 ctsio->residual = 0; 9463 ctsio->kern_data_len = alloc_len; 9464 ctsio->kern_total_len = alloc_len; 9465 } 9466 ctsio->kern_data_resid = 0; 9467 ctsio->kern_rel_offset = 0; 9468 ctsio->kern_sg_entries = 0; 9469 9470 /* 9471 * The control device is always connected. The disk device, on the 9472 * other hand, may not be online all the time. Need to change this 9473 * to figure out whether the disk device is actually online or not. 9474 */ 9475 if (lun != NULL) 9476 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9477 lun->be_lun->lun_type; 9478 else 9479 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9480 9481 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9482 sn_ptr->length = CTL_SN_LEN; 9483 /* 9484 * If we don't have a LUN, we just leave the serial number as 9485 * all spaces. 9486 */ 9487 if (lun != NULL) { 9488 strncpy((char *)sn_ptr->serial_num, 9489 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9490 } else 9491 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9492 9493 ctl_set_success(ctsio); 9494 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9495 ctsio->be_move_done = ctl_config_move_done; 9496 ctl_datamove((union ctl_io *)ctsio); 9497 return (CTL_RETVAL_COMPLETE); 9498} 9499 9500 9501/* 9502 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9503 */ 9504static int 9505ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9506{ 9507 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9508 struct ctl_lun *lun; 9509 int data_len; 9510 9511 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9512 9513 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9514 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9515 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9516 ctsio->kern_sg_entries = 0; 9517 9518 if (data_len < alloc_len) { 9519 ctsio->residual = alloc_len - data_len; 9520 ctsio->kern_data_len = data_len; 9521 ctsio->kern_total_len = data_len; 9522 } else { 9523 ctsio->residual = 0; 9524 ctsio->kern_data_len = alloc_len; 9525 ctsio->kern_total_len = alloc_len; 9526 } 9527 ctsio->kern_data_resid = 0; 9528 ctsio->kern_rel_offset = 0; 9529 ctsio->kern_sg_entries = 0; 9530 9531 /* 9532 * The control device is always connected. The disk device, on the 9533 * other hand, may not be online all the time. 9534 */ 9535 if (lun != NULL) 9536 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9537 lun->be_lun->lun_type; 9538 else 9539 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9540 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9541 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9542 /* 9543 * We support head of queue, ordered and simple tags. 9544 */ 9545 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9546 /* 9547 * Volatile cache supported. 9548 */ 9549 eid_ptr->flags3 = SVPD_EID_V_SUP; 9550 9551 /* 9552 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9553 * attention for a particular IT nexus on all LUNs once we report 9554 * it to that nexus once. This bit is required as of SPC-4. 9555 */ 9556 eid_ptr->flags4 = SVPD_EID_LUICLR; 9557 9558 /* 9559 * XXX KDM in order to correctly answer this, we would need 9560 * information from the SIM to determine how much sense data it 9561 * can send. So this would really be a path inquiry field, most 9562 * likely. This can be set to a maximum of 252 according to SPC-4, 9563 * but the hardware may or may not be able to support that much. 9564 * 0 just means that the maximum sense data length is not reported. 9565 */ 9566 eid_ptr->max_sense_length = 0; 9567 9568 ctl_set_success(ctsio); 9569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9570 ctsio->be_move_done = ctl_config_move_done; 9571 ctl_datamove((union ctl_io *)ctsio); 9572 return (CTL_RETVAL_COMPLETE); 9573} 9574 9575static int 9576ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9577{ 9578 struct scsi_vpd_mode_page_policy *mpp_ptr; 9579 struct ctl_lun *lun; 9580 int data_len; 9581 9582 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9583 9584 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9585 sizeof(struct scsi_vpd_mode_page_policy_descr); 9586 9587 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9588 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9589 ctsio->kern_sg_entries = 0; 9590 9591 if (data_len < alloc_len) { 9592 ctsio->residual = alloc_len - data_len; 9593 ctsio->kern_data_len = data_len; 9594 ctsio->kern_total_len = data_len; 9595 } else { 9596 ctsio->residual = 0; 9597 ctsio->kern_data_len = alloc_len; 9598 ctsio->kern_total_len = alloc_len; 9599 } 9600 ctsio->kern_data_resid = 0; 9601 ctsio->kern_rel_offset = 0; 9602 ctsio->kern_sg_entries = 0; 9603 9604 /* 9605 * The control device is always connected. The disk device, on the 9606 * other hand, may not be online all the time. 9607 */ 9608 if (lun != NULL) 9609 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9610 lun->be_lun->lun_type; 9611 else 9612 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9613 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9614 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9615 mpp_ptr->descr[0].page_code = 0x3f; 9616 mpp_ptr->descr[0].subpage_code = 0xff; 9617 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9618 9619 ctl_set_success(ctsio); 9620 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9621 ctsio->be_move_done = ctl_config_move_done; 9622 ctl_datamove((union ctl_io *)ctsio); 9623 return (CTL_RETVAL_COMPLETE); 9624} 9625 9626/* 9627 * SCSI VPD page 0x83, the Device Identification page. 9628 */ 9629static int 9630ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9631{ 9632 struct scsi_vpd_device_id *devid_ptr; 9633 struct scsi_vpd_id_descriptor *desc; 9634 struct ctl_softc *softc; 9635 struct ctl_lun *lun; 9636 struct ctl_port *port; 9637 int data_len, g; 9638 uint8_t proto; 9639 9640 softc = control_softc; 9641 9642 port = ctl_io_port(&ctsio->io_hdr); 9643 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9644 9645 data_len = sizeof(struct scsi_vpd_device_id) + 9646 sizeof(struct scsi_vpd_id_descriptor) + 9647 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9648 sizeof(struct scsi_vpd_id_descriptor) + 9649 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9650 if (lun && lun->lun_devid) 9651 data_len += lun->lun_devid->len; 9652 if (port && port->port_devid) 9653 data_len += port->port_devid->len; 9654 if (port && port->target_devid) 9655 data_len += port->target_devid->len; 9656 9657 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9658 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9659 ctsio->kern_sg_entries = 0; 9660 9661 if (data_len < alloc_len) { 9662 ctsio->residual = alloc_len - data_len; 9663 ctsio->kern_data_len = data_len; 9664 ctsio->kern_total_len = data_len; 9665 } else { 9666 ctsio->residual = 0; 9667 ctsio->kern_data_len = alloc_len; 9668 ctsio->kern_total_len = alloc_len; 9669 } 9670 ctsio->kern_data_resid = 0; 9671 ctsio->kern_rel_offset = 0; 9672 ctsio->kern_sg_entries = 0; 9673 9674 /* 9675 * The control device is always connected. The disk device, on the 9676 * other hand, may not be online all the time. 9677 */ 9678 if (lun != NULL) 9679 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9680 lun->be_lun->lun_type; 9681 else 9682 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9683 devid_ptr->page_code = SVPD_DEVICE_ID; 9684 scsi_ulto2b(data_len - 4, devid_ptr->length); 9685 9686 if (port && port->port_type == CTL_PORT_FC) 9687 proto = SCSI_PROTO_FC << 4; 9688 else if (port && port->port_type == CTL_PORT_ISCSI) 9689 proto = SCSI_PROTO_ISCSI << 4; 9690 else 9691 proto = SCSI_PROTO_SPI << 4; 9692 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9693 9694 /* 9695 * We're using a LUN association here. i.e., this device ID is a 9696 * per-LUN identifier. 9697 */ 9698 if (lun && lun->lun_devid) { 9699 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9700 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9701 lun->lun_devid->len); 9702 } 9703 9704 /* 9705 * This is for the WWPN which is a port association. 9706 */ 9707 if (port && port->port_devid) { 9708 memcpy(desc, port->port_devid->data, port->port_devid->len); 9709 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9710 port->port_devid->len); 9711 } 9712 9713 /* 9714 * This is for the Relative Target Port(type 4h) identifier 9715 */ 9716 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9717 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9718 SVPD_ID_TYPE_RELTARG; 9719 desc->length = 4; 9720 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9721 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9722 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9723 9724 /* 9725 * This is for the Target Port Group(type 5h) identifier 9726 */ 9727 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9728 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9729 SVPD_ID_TYPE_TPORTGRP; 9730 desc->length = 4; 9731 if (softc->is_single || 9732 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9733 g = 1; 9734 else 9735 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9736 scsi_ulto2b(g, &desc->identifier[2]); 9737 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9738 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9739 9740 /* 9741 * This is for the Target identifier 9742 */ 9743 if (port && port->target_devid) { 9744 memcpy(desc, port->target_devid->data, port->target_devid->len); 9745 } 9746 9747 ctl_set_success(ctsio); 9748 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9749 ctsio->be_move_done = ctl_config_move_done; 9750 ctl_datamove((union ctl_io *)ctsio); 9751 return (CTL_RETVAL_COMPLETE); 9752} 9753 9754static int 9755ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9756{ 9757 struct ctl_softc *softc = control_softc; 9758 struct scsi_vpd_scsi_ports *sp; 9759 struct scsi_vpd_port_designation *pd; 9760 struct scsi_vpd_port_designation_cont *pdc; 9761 struct ctl_lun *lun; 9762 struct ctl_port *port; 9763 int data_len, num_target_ports, iid_len, id_len; 9764 9765 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9766 9767 num_target_ports = 0; 9768 iid_len = 0; 9769 id_len = 0; 9770 mtx_lock(&softc->ctl_lock); 9771 STAILQ_FOREACH(port, &softc->port_list, links) { 9772 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9773 continue; 9774 if (lun != NULL && 9775 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9776 continue; 9777 num_target_ports++; 9778 if (port->init_devid) 9779 iid_len += port->init_devid->len; 9780 if (port->port_devid) 9781 id_len += port->port_devid->len; 9782 } 9783 mtx_unlock(&softc->ctl_lock); 9784 9785 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9786 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9787 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9788 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9789 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9790 ctsio->kern_sg_entries = 0; 9791 9792 if (data_len < alloc_len) { 9793 ctsio->residual = alloc_len - data_len; 9794 ctsio->kern_data_len = data_len; 9795 ctsio->kern_total_len = data_len; 9796 } else { 9797 ctsio->residual = 0; 9798 ctsio->kern_data_len = alloc_len; 9799 ctsio->kern_total_len = alloc_len; 9800 } 9801 ctsio->kern_data_resid = 0; 9802 ctsio->kern_rel_offset = 0; 9803 ctsio->kern_sg_entries = 0; 9804 9805 /* 9806 * The control device is always connected. The disk device, on the 9807 * other hand, may not be online all the time. Need to change this 9808 * to figure out whether the disk device is actually online or not. 9809 */ 9810 if (lun != NULL) 9811 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9812 lun->be_lun->lun_type; 9813 else 9814 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9815 9816 sp->page_code = SVPD_SCSI_PORTS; 9817 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9818 sp->page_length); 9819 pd = &sp->design[0]; 9820 9821 mtx_lock(&softc->ctl_lock); 9822 STAILQ_FOREACH(port, &softc->port_list, links) { 9823 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9824 continue; 9825 if (lun != NULL && 9826 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9827 continue; 9828 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9829 if (port->init_devid) { 9830 iid_len = port->init_devid->len; 9831 memcpy(pd->initiator_transportid, 9832 port->init_devid->data, port->init_devid->len); 9833 } else 9834 iid_len = 0; 9835 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9836 pdc = (struct scsi_vpd_port_designation_cont *) 9837 (&pd->initiator_transportid[iid_len]); 9838 if (port->port_devid) { 9839 id_len = port->port_devid->len; 9840 memcpy(pdc->target_port_descriptors, 9841 port->port_devid->data, port->port_devid->len); 9842 } else 9843 id_len = 0; 9844 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9845 pd = (struct scsi_vpd_port_designation *) 9846 ((uint8_t *)pdc->target_port_descriptors + id_len); 9847 } 9848 mtx_unlock(&softc->ctl_lock); 9849 9850 ctl_set_success(ctsio); 9851 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9852 ctsio->be_move_done = ctl_config_move_done; 9853 ctl_datamove((union ctl_io *)ctsio); 9854 return (CTL_RETVAL_COMPLETE); 9855} 9856 9857static int 9858ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9859{ 9860 struct scsi_vpd_block_limits *bl_ptr; 9861 struct ctl_lun *lun; 9862 uint64_t ival; 9863 9864 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9865 9866 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9867 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9868 ctsio->kern_sg_entries = 0; 9869 9870 if (sizeof(*bl_ptr) < alloc_len) { 9871 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9872 ctsio->kern_data_len = sizeof(*bl_ptr); 9873 ctsio->kern_total_len = sizeof(*bl_ptr); 9874 } else { 9875 ctsio->residual = 0; 9876 ctsio->kern_data_len = alloc_len; 9877 ctsio->kern_total_len = alloc_len; 9878 } 9879 ctsio->kern_data_resid = 0; 9880 ctsio->kern_rel_offset = 0; 9881 ctsio->kern_sg_entries = 0; 9882 9883 /* 9884 * The control device is always connected. The disk device, on the 9885 * other hand, may not be online all the time. Need to change this 9886 * to figure out whether the disk device is actually online or not. 9887 */ 9888 if (lun != NULL) 9889 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9890 lun->be_lun->lun_type; 9891 else 9892 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9893 9894 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9895 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9896 bl_ptr->max_cmp_write_len = 0xff; 9897 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9898 if (lun != NULL) { 9899 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9900 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9901 ival = 0xffffffff; 9902 ctl_get_opt_number(&lun->be_lun->options, 9903 "unmap_max_lba", &ival); 9904 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9905 ival = 0xffffffff; 9906 ctl_get_opt_number(&lun->be_lun->options, 9907 "unmap_max_descr", &ival); 9908 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9909 if (lun->be_lun->ublockexp != 0) { 9910 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9911 bl_ptr->opt_unmap_grain); 9912 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9913 bl_ptr->unmap_grain_align); 9914 } 9915 } 9916 scsi_ulto4b(lun->be_lun->atomicblock, 9917 bl_ptr->max_atomic_transfer_length); 9918 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9919 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9920 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9921 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9922 ival = UINT64_MAX; 9923 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival); 9924 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9925 } 9926 9927 ctl_set_success(ctsio); 9928 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9929 ctsio->be_move_done = ctl_config_move_done; 9930 ctl_datamove((union ctl_io *)ctsio); 9931 return (CTL_RETVAL_COMPLETE); 9932} 9933 9934static int 9935ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9936{ 9937 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9938 struct ctl_lun *lun; 9939 const char *value; 9940 u_int i; 9941 9942 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9943 9944 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9945 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9946 ctsio->kern_sg_entries = 0; 9947 9948 if (sizeof(*bdc_ptr) < alloc_len) { 9949 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9950 ctsio->kern_data_len = sizeof(*bdc_ptr); 9951 ctsio->kern_total_len = sizeof(*bdc_ptr); 9952 } else { 9953 ctsio->residual = 0; 9954 ctsio->kern_data_len = alloc_len; 9955 ctsio->kern_total_len = alloc_len; 9956 } 9957 ctsio->kern_data_resid = 0; 9958 ctsio->kern_rel_offset = 0; 9959 ctsio->kern_sg_entries = 0; 9960 9961 /* 9962 * The control device is always connected. The disk device, on the 9963 * other hand, may not be online all the time. Need to change this 9964 * to figure out whether the disk device is actually online or not. 9965 */ 9966 if (lun != NULL) 9967 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9968 lun->be_lun->lun_type; 9969 else 9970 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9971 bdc_ptr->page_code = SVPD_BDC; 9972 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9973 if (lun != NULL && 9974 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9975 i = strtol(value, NULL, 0); 9976 else 9977 i = CTL_DEFAULT_ROTATION_RATE; 9978 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9979 if (lun != NULL && 9980 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9981 i = strtol(value, NULL, 0); 9982 else 9983 i = 0; 9984 bdc_ptr->wab_wac_ff = (i & 0x0f); 9985 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9986 9987 ctl_set_success(ctsio); 9988 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9989 ctsio->be_move_done = ctl_config_move_done; 9990 ctl_datamove((union ctl_io *)ctsio); 9991 return (CTL_RETVAL_COMPLETE); 9992} 9993 9994static int 9995ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9996{ 9997 struct scsi_vpd_logical_block_prov *lbp_ptr; 9998 struct ctl_lun *lun; 9999 const char *value; 10000 10001 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10002 10003 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10004 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10005 ctsio->kern_sg_entries = 0; 10006 10007 if (sizeof(*lbp_ptr) < alloc_len) { 10008 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10009 ctsio->kern_data_len = sizeof(*lbp_ptr); 10010 ctsio->kern_total_len = sizeof(*lbp_ptr); 10011 } else { 10012 ctsio->residual = 0; 10013 ctsio->kern_data_len = alloc_len; 10014 ctsio->kern_total_len = alloc_len; 10015 } 10016 ctsio->kern_data_resid = 0; 10017 ctsio->kern_rel_offset = 0; 10018 ctsio->kern_sg_entries = 0; 10019 10020 /* 10021 * The control device is always connected. The disk device, on the 10022 * other hand, may not be online all the time. Need to change this 10023 * to figure out whether the disk device is actually online or not. 10024 */ 10025 if (lun != NULL) 10026 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10027 lun->be_lun->lun_type; 10028 else 10029 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10030 10031 lbp_ptr->page_code = SVPD_LBP; 10032 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10033 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10034 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10035 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10036 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10037 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type"); 10038 if (value != NULL) { 10039 if (strcmp(value, "resource") == 0) 10040 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10041 else if (strcmp(value, "thin") == 0) 10042 lbp_ptr->prov_type = SVPD_LBP_THIN; 10043 } else 10044 lbp_ptr->prov_type = SVPD_LBP_THIN; 10045 } 10046 10047 ctl_set_success(ctsio); 10048 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10049 ctsio->be_move_done = ctl_config_move_done; 10050 ctl_datamove((union ctl_io *)ctsio); 10051 return (CTL_RETVAL_COMPLETE); 10052} 10053 10054/* 10055 * INQUIRY with the EVPD bit set. 10056 */ 10057static int 10058ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10059{ 10060 struct ctl_lun *lun; 10061 struct scsi_inquiry *cdb; 10062 int alloc_len, retval; 10063 10064 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10065 cdb = (struct scsi_inquiry *)ctsio->cdb; 10066 alloc_len = scsi_2btoul(cdb->length); 10067 10068 switch (cdb->page_code) { 10069 case SVPD_SUPPORTED_PAGES: 10070 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10071 break; 10072 case SVPD_UNIT_SERIAL_NUMBER: 10073 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10074 break; 10075 case SVPD_DEVICE_ID: 10076 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10077 break; 10078 case SVPD_EXTENDED_INQUIRY_DATA: 10079 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10080 break; 10081 case SVPD_MODE_PAGE_POLICY: 10082 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10083 break; 10084 case SVPD_SCSI_PORTS: 10085 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10086 break; 10087 case SVPD_SCSI_TPC: 10088 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10089 break; 10090 case SVPD_BLOCK_LIMITS: 10091 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10092 goto err; 10093 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10094 break; 10095 case SVPD_BDC: 10096 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10097 goto err; 10098 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10099 break; 10100 case SVPD_LBP: 10101 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10102 goto err; 10103 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10104 break; 10105 default: 10106err: 10107 ctl_set_invalid_field(ctsio, 10108 /*sks_valid*/ 1, 10109 /*command*/ 1, 10110 /*field*/ 2, 10111 /*bit_valid*/ 0, 10112 /*bit*/ 0); 10113 ctl_done((union ctl_io *)ctsio); 10114 retval = CTL_RETVAL_COMPLETE; 10115 break; 10116 } 10117 10118 return (retval); 10119} 10120 10121/* 10122 * Standard INQUIRY data. 10123 */ 10124static int 10125ctl_inquiry_std(struct ctl_scsiio *ctsio) 10126{ 10127 struct scsi_inquiry_data *inq_ptr; 10128 struct scsi_inquiry *cdb; 10129 struct ctl_softc *softc = control_softc; 10130 struct ctl_port *port; 10131 struct ctl_lun *lun; 10132 char *val; 10133 uint32_t alloc_len, data_len; 10134 ctl_port_type port_type; 10135 10136 port = ctl_io_port(&ctsio->io_hdr); 10137 port_type = port->port_type; 10138 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10139 port_type = CTL_PORT_SCSI; 10140 10141 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10142 cdb = (struct scsi_inquiry *)ctsio->cdb; 10143 alloc_len = scsi_2btoul(cdb->length); 10144 10145 /* 10146 * We malloc the full inquiry data size here and fill it 10147 * in. If the user only asks for less, we'll give him 10148 * that much. 10149 */ 10150 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10151 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10152 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10153 ctsio->kern_sg_entries = 0; 10154 ctsio->kern_data_resid = 0; 10155 ctsio->kern_rel_offset = 0; 10156 10157 if (data_len < alloc_len) { 10158 ctsio->residual = alloc_len - data_len; 10159 ctsio->kern_data_len = data_len; 10160 ctsio->kern_total_len = data_len; 10161 } else { 10162 ctsio->residual = 0; 10163 ctsio->kern_data_len = alloc_len; 10164 ctsio->kern_total_len = alloc_len; 10165 } 10166 10167 if (lun != NULL) { 10168 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10169 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10170 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10171 lun->be_lun->lun_type; 10172 } else { 10173 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10174 lun->be_lun->lun_type; 10175 } 10176 if (lun->flags & CTL_LUN_REMOVABLE) 10177 inq_ptr->dev_qual2 |= SID_RMB; 10178 } else 10179 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10180 10181 /* RMB in byte 2 is 0 */ 10182 inq_ptr->version = SCSI_REV_SPC5; 10183 10184 /* 10185 * According to SAM-3, even if a device only supports a single 10186 * level of LUN addressing, it should still set the HISUP bit: 10187 * 10188 * 4.9.1 Logical unit numbers overview 10189 * 10190 * All logical unit number formats described in this standard are 10191 * hierarchical in structure even when only a single level in that 10192 * hierarchy is used. The HISUP bit shall be set to one in the 10193 * standard INQUIRY data (see SPC-2) when any logical unit number 10194 * format described in this standard is used. Non-hierarchical 10195 * formats are outside the scope of this standard. 10196 * 10197 * Therefore we set the HiSup bit here. 10198 * 10199 * The response format is 2, per SPC-3. 10200 */ 10201 inq_ptr->response_format = SID_HiSup | 2; 10202 10203 inq_ptr->additional_length = data_len - 10204 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10205 CTL_DEBUG_PRINT(("additional_length = %d\n", 10206 inq_ptr->additional_length)); 10207 10208 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10209 if (port_type == CTL_PORT_SCSI) 10210 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10211 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10212 inq_ptr->flags = SID_CmdQue; 10213 if (port_type == CTL_PORT_SCSI) 10214 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10215 10216 /* 10217 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10218 * We have 8 bytes for the vendor name, and 16 bytes for the device 10219 * name and 4 bytes for the revision. 10220 */ 10221 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10222 "vendor")) == NULL) { 10223 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10224 } else { 10225 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10226 strncpy(inq_ptr->vendor, val, 10227 min(sizeof(inq_ptr->vendor), strlen(val))); 10228 } 10229 if (lun == NULL) { 10230 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10231 sizeof(inq_ptr->product)); 10232 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10233 switch (lun->be_lun->lun_type) { 10234 case T_DIRECT: 10235 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10236 sizeof(inq_ptr->product)); 10237 break; 10238 case T_PROCESSOR: 10239 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10240 sizeof(inq_ptr->product)); 10241 break; 10242 case T_CDROM: 10243 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10244 sizeof(inq_ptr->product)); 10245 break; 10246 default: 10247 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10248 sizeof(inq_ptr->product)); 10249 break; 10250 } 10251 } else { 10252 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10253 strncpy(inq_ptr->product, val, 10254 min(sizeof(inq_ptr->product), strlen(val))); 10255 } 10256 10257 /* 10258 * XXX make this a macro somewhere so it automatically gets 10259 * incremented when we make changes. 10260 */ 10261 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10262 "revision")) == NULL) { 10263 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10264 } else { 10265 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10266 strncpy(inq_ptr->revision, val, 10267 min(sizeof(inq_ptr->revision), strlen(val))); 10268 } 10269 10270 /* 10271 * For parallel SCSI, we support double transition and single 10272 * transition clocking. We also support QAS (Quick Arbitration 10273 * and Selection) and Information Unit transfers on both the 10274 * control and array devices. 10275 */ 10276 if (port_type == CTL_PORT_SCSI) 10277 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10278 SID_SPI_IUS; 10279 10280 /* SAM-6 (no version claimed) */ 10281 scsi_ulto2b(0x00C0, inq_ptr->version1); 10282 /* SPC-5 (no version claimed) */ 10283 scsi_ulto2b(0x05C0, inq_ptr->version2); 10284 if (port_type == CTL_PORT_FC) { 10285 /* FCP-2 ANSI INCITS.350:2003 */ 10286 scsi_ulto2b(0x0917, inq_ptr->version3); 10287 } else if (port_type == CTL_PORT_SCSI) { 10288 /* SPI-4 ANSI INCITS.362:200x */ 10289 scsi_ulto2b(0x0B56, inq_ptr->version3); 10290 } else if (port_type == CTL_PORT_ISCSI) { 10291 /* iSCSI (no version claimed) */ 10292 scsi_ulto2b(0x0960, inq_ptr->version3); 10293 } else if (port_type == CTL_PORT_SAS) { 10294 /* SAS (no version claimed) */ 10295 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10296 } 10297 10298 if (lun == NULL) { 10299 /* SBC-4 (no version claimed) */ 10300 scsi_ulto2b(0x0600, inq_ptr->version4); 10301 } else { 10302 switch (lun->be_lun->lun_type) { 10303 case T_DIRECT: 10304 /* SBC-4 (no version claimed) */ 10305 scsi_ulto2b(0x0600, inq_ptr->version4); 10306 break; 10307 case T_PROCESSOR: 10308 break; 10309 case T_CDROM: 10310 /* MMC-6 (no version claimed) */ 10311 scsi_ulto2b(0x04E0, inq_ptr->version4); 10312 break; 10313 default: 10314 break; 10315 } 10316 } 10317 10318 ctl_set_success(ctsio); 10319 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10320 ctsio->be_move_done = ctl_config_move_done; 10321 ctl_datamove((union ctl_io *)ctsio); 10322 return (CTL_RETVAL_COMPLETE); 10323} 10324 10325int 10326ctl_inquiry(struct ctl_scsiio *ctsio) 10327{ 10328 struct scsi_inquiry *cdb; 10329 int retval; 10330 10331 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10332 10333 cdb = (struct scsi_inquiry *)ctsio->cdb; 10334 if (cdb->byte2 & SI_EVPD) 10335 retval = ctl_inquiry_evpd(ctsio); 10336 else if (cdb->page_code == 0) 10337 retval = ctl_inquiry_std(ctsio); 10338 else { 10339 ctl_set_invalid_field(ctsio, 10340 /*sks_valid*/ 1, 10341 /*command*/ 1, 10342 /*field*/ 2, 10343 /*bit_valid*/ 0, 10344 /*bit*/ 0); 10345 ctl_done((union ctl_io *)ctsio); 10346 return (CTL_RETVAL_COMPLETE); 10347 } 10348 10349 return (retval); 10350} 10351 10352int 10353ctl_get_config(struct ctl_scsiio *ctsio) 10354{ 10355 struct scsi_get_config_header *hdr; 10356 struct scsi_get_config_feature *feature; 10357 struct scsi_get_config *cdb; 10358 struct ctl_lun *lun; 10359 uint32_t alloc_len, data_len; 10360 int rt, starting; 10361 10362 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10363 cdb = (struct scsi_get_config *)ctsio->cdb; 10364 rt = (cdb->rt & SGC_RT_MASK); 10365 starting = scsi_2btoul(cdb->starting_feature); 10366 alloc_len = scsi_2btoul(cdb->length); 10367 10368 data_len = sizeof(struct scsi_get_config_header) + 10369 sizeof(struct scsi_get_config_feature) + 8 + 10370 sizeof(struct scsi_get_config_feature) + 8 + 10371 sizeof(struct scsi_get_config_feature) + 4 + 10372 sizeof(struct scsi_get_config_feature) + 4 + 10373 sizeof(struct scsi_get_config_feature) + 8 + 10374 sizeof(struct scsi_get_config_feature) + 10375 sizeof(struct scsi_get_config_feature) + 4 + 10376 sizeof(struct scsi_get_config_feature) + 4 + 10377 sizeof(struct scsi_get_config_feature) + 4 + 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 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10382 ctsio->kern_sg_entries = 0; 10383 ctsio->kern_data_resid = 0; 10384 ctsio->kern_rel_offset = 0; 10385 10386 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10387 if (lun->flags & CTL_LUN_NO_MEDIA) 10388 scsi_ulto2b(0x0000, hdr->current_profile); 10389 else 10390 scsi_ulto2b(0x0010, hdr->current_profile); 10391 feature = (struct scsi_get_config_feature *)(hdr + 1); 10392 10393 if (starting > 0x003b) 10394 goto done; 10395 if (starting > 0x003a) 10396 goto f3b; 10397 if (starting > 0x002b) 10398 goto f3a; 10399 if (starting > 0x002a) 10400 goto f2b; 10401 if (starting > 0x001f) 10402 goto f2a; 10403 if (starting > 0x001e) 10404 goto f1f; 10405 if (starting > 0x001d) 10406 goto f1e; 10407 if (starting > 0x0010) 10408 goto f1d; 10409 if (starting > 0x0003) 10410 goto f10; 10411 if (starting > 0x0002) 10412 goto f3; 10413 if (starting > 0x0001) 10414 goto f2; 10415 if (starting > 0x0000) 10416 goto f1; 10417 10418 /* Profile List */ 10419 scsi_ulto2b(0x0000, feature->feature_code); 10420 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10421 feature->add_length = 8; 10422 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10423 feature->feature_data[2] = 0x00; 10424 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10425 feature->feature_data[6] = 0x01; 10426 feature = (struct scsi_get_config_feature *) 10427 &feature->feature_data[feature->add_length]; 10428 10429f1: /* Core */ 10430 scsi_ulto2b(0x0001, feature->feature_code); 10431 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10432 feature->add_length = 8; 10433 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10434 feature->feature_data[4] = 0x03; 10435 feature = (struct scsi_get_config_feature *) 10436 &feature->feature_data[feature->add_length]; 10437 10438f2: /* Morphing */ 10439 scsi_ulto2b(0x0002, feature->feature_code); 10440 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10441 feature->add_length = 4; 10442 feature->feature_data[0] = 0x02; 10443 feature = (struct scsi_get_config_feature *) 10444 &feature->feature_data[feature->add_length]; 10445 10446f3: /* Removable Medium */ 10447 scsi_ulto2b(0x0003, feature->feature_code); 10448 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10449 feature->add_length = 4; 10450 feature->feature_data[0] = 0x39; 10451 feature = (struct scsi_get_config_feature *) 10452 &feature->feature_data[feature->add_length]; 10453 10454 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10455 goto done; 10456 10457f10: /* Random Read */ 10458 scsi_ulto2b(0x0010, feature->feature_code); 10459 feature->flags = 0x00; 10460 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10461 feature->flags |= SGC_F_CURRENT; 10462 feature->add_length = 8; 10463 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10464 scsi_ulto2b(1, &feature->feature_data[4]); 10465 feature->feature_data[6] = 0x00; 10466 feature = (struct scsi_get_config_feature *) 10467 &feature->feature_data[feature->add_length]; 10468 10469f1d: /* Multi-Read */ 10470 scsi_ulto2b(0x001D, feature->feature_code); 10471 feature->flags = 0x00; 10472 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10473 feature->flags |= SGC_F_CURRENT; 10474 feature->add_length = 0; 10475 feature = (struct scsi_get_config_feature *) 10476 &feature->feature_data[feature->add_length]; 10477 10478f1e: /* CD Read */ 10479 scsi_ulto2b(0x001E, feature->feature_code); 10480 feature->flags = 0x00; 10481 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10482 feature->flags |= SGC_F_CURRENT; 10483 feature->add_length = 4; 10484 feature->feature_data[0] = 0x00; 10485 feature = (struct scsi_get_config_feature *) 10486 &feature->feature_data[feature->add_length]; 10487 10488f1f: /* DVD Read */ 10489 scsi_ulto2b(0x001F, feature->feature_code); 10490 feature->flags = 0x08; 10491 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10492 feature->flags |= SGC_F_CURRENT; 10493 feature->add_length = 4; 10494 feature->feature_data[0] = 0x01; 10495 feature->feature_data[2] = 0x03; 10496 feature = (struct scsi_get_config_feature *) 10497 &feature->feature_data[feature->add_length]; 10498 10499f2a: /* DVD+RW */ 10500 scsi_ulto2b(0x002A, feature->feature_code); 10501 feature->flags = 0x04; 10502 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10503 feature->flags |= SGC_F_CURRENT; 10504 feature->add_length = 4; 10505 feature->feature_data[0] = 0x00; 10506 feature->feature_data[1] = 0x00; 10507 feature = (struct scsi_get_config_feature *) 10508 &feature->feature_data[feature->add_length]; 10509 10510f2b: /* DVD+R */ 10511 scsi_ulto2b(0x002B, feature->feature_code); 10512 feature->flags = 0x00; 10513 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10514 feature->flags |= SGC_F_CURRENT; 10515 feature->add_length = 4; 10516 feature->feature_data[0] = 0x00; 10517 feature = (struct scsi_get_config_feature *) 10518 &feature->feature_data[feature->add_length]; 10519 10520f3a: /* DVD+RW Dual Layer */ 10521 scsi_ulto2b(0x003A, feature->feature_code); 10522 feature->flags = 0x00; 10523 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10524 feature->flags |= SGC_F_CURRENT; 10525 feature->add_length = 4; 10526 feature->feature_data[0] = 0x00; 10527 feature->feature_data[1] = 0x00; 10528 feature = (struct scsi_get_config_feature *) 10529 &feature->feature_data[feature->add_length]; 10530 10531f3b: /* DVD+R Dual Layer */ 10532 scsi_ulto2b(0x003B, feature->feature_code); 10533 feature->flags = 0x00; 10534 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10535 feature->flags |= SGC_F_CURRENT; 10536 feature->add_length = 4; 10537 feature->feature_data[0] = 0x00; 10538 feature = (struct scsi_get_config_feature *) 10539 &feature->feature_data[feature->add_length]; 10540 10541done: 10542 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10543 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10544 feature = (struct scsi_get_config_feature *)(hdr + 1); 10545 if (scsi_2btoul(feature->feature_code) == starting) 10546 feature = (struct scsi_get_config_feature *) 10547 &feature->feature_data[feature->add_length]; 10548 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10549 } 10550 scsi_ulto4b(data_len - 4, hdr->data_length); 10551 if (data_len < alloc_len) { 10552 ctsio->residual = alloc_len - data_len; 10553 ctsio->kern_data_len = data_len; 10554 ctsio->kern_total_len = data_len; 10555 } else { 10556 ctsio->residual = 0; 10557 ctsio->kern_data_len = alloc_len; 10558 ctsio->kern_total_len = alloc_len; 10559 } 10560 10561 ctl_set_success(ctsio); 10562 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10563 ctsio->be_move_done = ctl_config_move_done; 10564 ctl_datamove((union ctl_io *)ctsio); 10565 return (CTL_RETVAL_COMPLETE); 10566} 10567 10568int 10569ctl_get_event_status(struct ctl_scsiio *ctsio) 10570{ 10571 struct scsi_get_event_status_header *hdr; 10572 struct scsi_get_event_status *cdb; 10573 struct ctl_lun *lun; 10574 uint32_t alloc_len, data_len; 10575 int notif_class; 10576 10577 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10578 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10579 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10580 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10581 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10582 ctl_done((union ctl_io *)ctsio); 10583 return (CTL_RETVAL_COMPLETE); 10584 } 10585 notif_class = cdb->notif_class; 10586 alloc_len = scsi_2btoul(cdb->length); 10587 10588 data_len = sizeof(struct scsi_get_event_status_header); 10589 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10590 ctsio->kern_sg_entries = 0; 10591 ctsio->kern_data_resid = 0; 10592 ctsio->kern_rel_offset = 0; 10593 10594 if (data_len < alloc_len) { 10595 ctsio->residual = alloc_len - data_len; 10596 ctsio->kern_data_len = data_len; 10597 ctsio->kern_total_len = data_len; 10598 } else { 10599 ctsio->residual = 0; 10600 ctsio->kern_data_len = alloc_len; 10601 ctsio->kern_total_len = alloc_len; 10602 } 10603 10604 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10605 scsi_ulto2b(0, hdr->descr_length); 10606 hdr->nea_class = SGESN_NEA; 10607 hdr->supported_class = 0; 10608 10609 ctl_set_success(ctsio); 10610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10611 ctsio->be_move_done = ctl_config_move_done; 10612 ctl_datamove((union ctl_io *)ctsio); 10613 return (CTL_RETVAL_COMPLETE); 10614} 10615 10616int 10617ctl_mechanism_status(struct ctl_scsiio *ctsio) 10618{ 10619 struct scsi_mechanism_status_header *hdr; 10620 struct scsi_mechanism_status *cdb; 10621 struct ctl_lun *lun; 10622 uint32_t alloc_len, data_len; 10623 10624 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10625 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10626 alloc_len = scsi_2btoul(cdb->length); 10627 10628 data_len = sizeof(struct scsi_mechanism_status_header); 10629 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10630 ctsio->kern_sg_entries = 0; 10631 ctsio->kern_data_resid = 0; 10632 ctsio->kern_rel_offset = 0; 10633 10634 if (data_len < alloc_len) { 10635 ctsio->residual = alloc_len - data_len; 10636 ctsio->kern_data_len = data_len; 10637 ctsio->kern_total_len = data_len; 10638 } else { 10639 ctsio->residual = 0; 10640 ctsio->kern_data_len = alloc_len; 10641 ctsio->kern_total_len = alloc_len; 10642 } 10643 10644 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10645 hdr->state1 = 0x00; 10646 hdr->state2 = 0xe0; 10647 scsi_ulto3b(0, hdr->lba); 10648 hdr->slots_num = 0; 10649 scsi_ulto2b(0, hdr->slots_length); 10650 10651 ctl_set_success(ctsio); 10652 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10653 ctsio->be_move_done = ctl_config_move_done; 10654 ctl_datamove((union ctl_io *)ctsio); 10655 return (CTL_RETVAL_COMPLETE); 10656} 10657 10658static void 10659ctl_ultomsf(uint32_t lba, uint8_t *buf) 10660{ 10661 10662 lba += 150; 10663 buf[0] = 0; 10664 buf[1] = bin2bcd((lba / 75) / 60); 10665 buf[2] = bin2bcd((lba / 75) % 60); 10666 buf[3] = bin2bcd(lba % 75); 10667} 10668 10669int 10670ctl_read_toc(struct ctl_scsiio *ctsio) 10671{ 10672 struct scsi_read_toc_hdr *hdr; 10673 struct scsi_read_toc_type01_descr *descr; 10674 struct scsi_read_toc *cdb; 10675 struct ctl_lun *lun; 10676 uint32_t alloc_len, data_len; 10677 int format, msf; 10678 10679 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10680 cdb = (struct scsi_read_toc *)ctsio->cdb; 10681 msf = (cdb->byte2 & CD_MSF) != 0; 10682 format = cdb->format; 10683 alloc_len = scsi_2btoul(cdb->data_len); 10684 10685 data_len = sizeof(struct scsi_read_toc_hdr); 10686 if (format == 0) 10687 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10688 else 10689 data_len += sizeof(struct scsi_read_toc_type01_descr); 10690 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10691 ctsio->kern_sg_entries = 0; 10692 ctsio->kern_data_resid = 0; 10693 ctsio->kern_rel_offset = 0; 10694 10695 if (data_len < alloc_len) { 10696 ctsio->residual = alloc_len - data_len; 10697 ctsio->kern_data_len = data_len; 10698 ctsio->kern_total_len = data_len; 10699 } else { 10700 ctsio->residual = 0; 10701 ctsio->kern_data_len = alloc_len; 10702 ctsio->kern_total_len = alloc_len; 10703 } 10704 10705 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10706 if (format == 0) { 10707 scsi_ulto2b(0x12, hdr->data_length); 10708 hdr->first = 1; 10709 hdr->last = 1; 10710 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10711 descr->addr_ctl = 0x14; 10712 descr->track_number = 1; 10713 if (msf) 10714 ctl_ultomsf(0, descr->track_start); 10715 else 10716 scsi_ulto4b(0, descr->track_start); 10717 descr++; 10718 descr->addr_ctl = 0x14; 10719 descr->track_number = 0xaa; 10720 if (msf) 10721 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10722 else 10723 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10724 } else { 10725 scsi_ulto2b(0x0a, hdr->data_length); 10726 hdr->first = 1; 10727 hdr->last = 1; 10728 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10729 descr->addr_ctl = 0x14; 10730 descr->track_number = 1; 10731 if (msf) 10732 ctl_ultomsf(0, descr->track_start); 10733 else 10734 scsi_ulto4b(0, descr->track_start); 10735 } 10736 10737 ctl_set_success(ctsio); 10738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10739 ctsio->be_move_done = ctl_config_move_done; 10740 ctl_datamove((union ctl_io *)ctsio); 10741 return (CTL_RETVAL_COMPLETE); 10742} 10743 10744/* 10745 * For known CDB types, parse the LBA and length. 10746 */ 10747static int 10748ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10749{ 10750 if (io->io_hdr.io_type != CTL_IO_SCSI) 10751 return (1); 10752 10753 switch (io->scsiio.cdb[0]) { 10754 case COMPARE_AND_WRITE: { 10755 struct scsi_compare_and_write *cdb; 10756 10757 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10758 10759 *lba = scsi_8btou64(cdb->addr); 10760 *len = cdb->length; 10761 break; 10762 } 10763 case READ_6: 10764 case WRITE_6: { 10765 struct scsi_rw_6 *cdb; 10766 10767 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10768 10769 *lba = scsi_3btoul(cdb->addr); 10770 /* only 5 bits are valid in the most significant address byte */ 10771 *lba &= 0x1fffff; 10772 *len = cdb->length; 10773 break; 10774 } 10775 case READ_10: 10776 case WRITE_10: { 10777 struct scsi_rw_10 *cdb; 10778 10779 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10780 10781 *lba = scsi_4btoul(cdb->addr); 10782 *len = scsi_2btoul(cdb->length); 10783 break; 10784 } 10785 case WRITE_VERIFY_10: { 10786 struct scsi_write_verify_10 *cdb; 10787 10788 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10789 10790 *lba = scsi_4btoul(cdb->addr); 10791 *len = scsi_2btoul(cdb->length); 10792 break; 10793 } 10794 case READ_12: 10795 case WRITE_12: { 10796 struct scsi_rw_12 *cdb; 10797 10798 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10799 10800 *lba = scsi_4btoul(cdb->addr); 10801 *len = scsi_4btoul(cdb->length); 10802 break; 10803 } 10804 case WRITE_VERIFY_12: { 10805 struct scsi_write_verify_12 *cdb; 10806 10807 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10808 10809 *lba = scsi_4btoul(cdb->addr); 10810 *len = scsi_4btoul(cdb->length); 10811 break; 10812 } 10813 case READ_16: 10814 case WRITE_16: { 10815 struct scsi_rw_16 *cdb; 10816 10817 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10818 10819 *lba = scsi_8btou64(cdb->addr); 10820 *len = scsi_4btoul(cdb->length); 10821 break; 10822 } 10823 case WRITE_ATOMIC_16: { 10824 struct scsi_write_atomic_16 *cdb; 10825 10826 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10827 10828 *lba = scsi_8btou64(cdb->addr); 10829 *len = scsi_2btoul(cdb->length); 10830 break; 10831 } 10832 case WRITE_VERIFY_16: { 10833 struct scsi_write_verify_16 *cdb; 10834 10835 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10836 10837 *lba = scsi_8btou64(cdb->addr); 10838 *len = scsi_4btoul(cdb->length); 10839 break; 10840 } 10841 case WRITE_SAME_10: { 10842 struct scsi_write_same_10 *cdb; 10843 10844 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10845 10846 *lba = scsi_4btoul(cdb->addr); 10847 *len = scsi_2btoul(cdb->length); 10848 break; 10849 } 10850 case WRITE_SAME_16: { 10851 struct scsi_write_same_16 *cdb; 10852 10853 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10854 10855 *lba = scsi_8btou64(cdb->addr); 10856 *len = scsi_4btoul(cdb->length); 10857 break; 10858 } 10859 case VERIFY_10: { 10860 struct scsi_verify_10 *cdb; 10861 10862 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10863 10864 *lba = scsi_4btoul(cdb->addr); 10865 *len = scsi_2btoul(cdb->length); 10866 break; 10867 } 10868 case VERIFY_12: { 10869 struct scsi_verify_12 *cdb; 10870 10871 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10872 10873 *lba = scsi_4btoul(cdb->addr); 10874 *len = scsi_4btoul(cdb->length); 10875 break; 10876 } 10877 case VERIFY_16: { 10878 struct scsi_verify_16 *cdb; 10879 10880 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10881 10882 *lba = scsi_8btou64(cdb->addr); 10883 *len = scsi_4btoul(cdb->length); 10884 break; 10885 } 10886 case UNMAP: { 10887 *lba = 0; 10888 *len = UINT64_MAX; 10889 break; 10890 } 10891 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10892 struct scsi_get_lba_status *cdb; 10893 10894 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10895 *lba = scsi_8btou64(cdb->addr); 10896 *len = UINT32_MAX; 10897 break; 10898 } 10899 default: 10900 return (1); 10901 break; /* NOTREACHED */ 10902 } 10903 10904 return (0); 10905} 10906 10907static ctl_action 10908ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10909 bool seq) 10910{ 10911 uint64_t endlba1, endlba2; 10912 10913 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10914 endlba2 = lba2 + len2 - 1; 10915 10916 if ((endlba1 < lba2) || (endlba2 < lba1)) 10917 return (CTL_ACTION_PASS); 10918 else 10919 return (CTL_ACTION_BLOCK); 10920} 10921 10922static int 10923ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10924{ 10925 struct ctl_ptr_len_flags *ptrlen; 10926 struct scsi_unmap_desc *buf, *end, *range; 10927 uint64_t lba; 10928 uint32_t len; 10929 10930 /* If not UNMAP -- go other way. */ 10931 if (io->io_hdr.io_type != CTL_IO_SCSI || 10932 io->scsiio.cdb[0] != UNMAP) 10933 return (CTL_ACTION_ERROR); 10934 10935 /* If UNMAP without data -- block and wait for data. */ 10936 ptrlen = (struct ctl_ptr_len_flags *) 10937 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10938 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10939 ptrlen->ptr == NULL) 10940 return (CTL_ACTION_BLOCK); 10941 10942 /* UNMAP with data -- check for collision. */ 10943 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10944 end = buf + ptrlen->len / sizeof(*buf); 10945 for (range = buf; range < end; range++) { 10946 lba = scsi_8btou64(range->lba); 10947 len = scsi_4btoul(range->length); 10948 if ((lba < lba2 + len2) && (lba + len > lba2)) 10949 return (CTL_ACTION_BLOCK); 10950 } 10951 return (CTL_ACTION_PASS); 10952} 10953 10954static ctl_action 10955ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10956{ 10957 uint64_t lba1, lba2; 10958 uint64_t len1, len2; 10959 int retval; 10960 10961 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10962 return (CTL_ACTION_ERROR); 10963 10964 retval = ctl_extent_check_unmap(io1, lba2, len2); 10965 if (retval != CTL_ACTION_ERROR) 10966 return (retval); 10967 10968 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10969 return (CTL_ACTION_ERROR); 10970 10971 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10972 seq = FALSE; 10973 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10974} 10975 10976static ctl_action 10977ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10978{ 10979 uint64_t lba1, lba2; 10980 uint64_t len1, len2; 10981 10982 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10983 return (CTL_ACTION_PASS); 10984 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10985 return (CTL_ACTION_ERROR); 10986 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10987 return (CTL_ACTION_ERROR); 10988 10989 if (lba1 + len1 == lba2) 10990 return (CTL_ACTION_BLOCK); 10991 return (CTL_ACTION_PASS); 10992} 10993 10994static ctl_action 10995ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10996 union ctl_io *ooa_io) 10997{ 10998 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10999 const ctl_serialize_action *serialize_row; 11000 11001 /* 11002 * The initiator attempted multiple untagged commands at the same 11003 * time. Can't do that. 11004 */ 11005 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11006 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11007 && ((pending_io->io_hdr.nexus.targ_port == 11008 ooa_io->io_hdr.nexus.targ_port) 11009 && (pending_io->io_hdr.nexus.initid == 11010 ooa_io->io_hdr.nexus.initid)) 11011 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11012 CTL_FLAG_STATUS_SENT)) == 0)) 11013 return (CTL_ACTION_OVERLAP); 11014 11015 /* 11016 * The initiator attempted to send multiple tagged commands with 11017 * the same ID. (It's fine if different initiators have the same 11018 * tag ID.) 11019 * 11020 * Even if all of those conditions are true, we don't kill the I/O 11021 * if the command ahead of us has been aborted. We won't end up 11022 * sending it to the FETD, and it's perfectly legal to resend a 11023 * command with the same tag number as long as the previous 11024 * instance of this tag number has been aborted somehow. 11025 */ 11026 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11027 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11028 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11029 && ((pending_io->io_hdr.nexus.targ_port == 11030 ooa_io->io_hdr.nexus.targ_port) 11031 && (pending_io->io_hdr.nexus.initid == 11032 ooa_io->io_hdr.nexus.initid)) 11033 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11034 CTL_FLAG_STATUS_SENT)) == 0)) 11035 return (CTL_ACTION_OVERLAP_TAG); 11036 11037 /* 11038 * If we get a head of queue tag, SAM-3 says that we should 11039 * immediately execute it. 11040 * 11041 * What happens if this command would normally block for some other 11042 * reason? e.g. a request sense with a head of queue tag 11043 * immediately after a write. Normally that would block, but this 11044 * will result in its getting executed immediately... 11045 * 11046 * We currently return "pass" instead of "skip", so we'll end up 11047 * going through the rest of the queue to check for overlapped tags. 11048 * 11049 * XXX KDM check for other types of blockage first?? 11050 */ 11051 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11052 return (CTL_ACTION_PASS); 11053 11054 /* 11055 * Ordered tags have to block until all items ahead of them 11056 * have completed. If we get called with an ordered tag, we always 11057 * block, if something else is ahead of us in the queue. 11058 */ 11059 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11060 return (CTL_ACTION_BLOCK); 11061 11062 /* 11063 * Simple tags get blocked until all head of queue and ordered tags 11064 * ahead of them have completed. I'm lumping untagged commands in 11065 * with simple tags here. XXX KDM is that the right thing to do? 11066 */ 11067 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11068 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11069 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11070 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11071 return (CTL_ACTION_BLOCK); 11072 11073 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11074 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 11075 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 11076 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 11077 pending_io->scsiio.cdb[1], pending_io)); 11078 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11079 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 11080 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 11081 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 11082 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 11083 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 11084 ooa_io->scsiio.cdb[1], ooa_io)); 11085 11086 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11087 11088 switch (serialize_row[pending_entry->seridx]) { 11089 case CTL_SER_BLOCK: 11090 return (CTL_ACTION_BLOCK); 11091 case CTL_SER_EXTENT: 11092 return (ctl_extent_check(ooa_io, pending_io, 11093 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11094 case CTL_SER_EXTENTOPT: 11095 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11096 SCP_QUEUE_ALG_UNRESTRICTED) 11097 return (ctl_extent_check(ooa_io, pending_io, 11098 (lun->be_lun && 11099 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11100 return (CTL_ACTION_PASS); 11101 case CTL_SER_EXTENTSEQ: 11102 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11103 return (ctl_extent_check_seq(ooa_io, pending_io)); 11104 return (CTL_ACTION_PASS); 11105 case CTL_SER_PASS: 11106 return (CTL_ACTION_PASS); 11107 case CTL_SER_BLOCKOPT: 11108 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11109 SCP_QUEUE_ALG_UNRESTRICTED) 11110 return (CTL_ACTION_BLOCK); 11111 return (CTL_ACTION_PASS); 11112 case CTL_SER_SKIP: 11113 return (CTL_ACTION_SKIP); 11114 default: 11115 panic("%s: Invalid serialization value %d for %d => %d", 11116 __func__, serialize_row[pending_entry->seridx], 11117 pending_entry->seridx, ooa_entry->seridx); 11118 } 11119 11120 return (CTL_ACTION_ERROR); 11121} 11122 11123/* 11124 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11125 * Assumptions: 11126 * - pending_io is generally either incoming, or on the blocked queue 11127 * - starting I/O is the I/O we want to start the check with. 11128 */ 11129static ctl_action 11130ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11131 union ctl_io *starting_io) 11132{ 11133 union ctl_io *ooa_io; 11134 ctl_action action; 11135 11136 mtx_assert(&lun->lun_lock, MA_OWNED); 11137 11138 /* 11139 * Run back along the OOA queue, starting with the current 11140 * blocked I/O and going through every I/O before it on the 11141 * queue. If starting_io is NULL, we'll just end up returning 11142 * CTL_ACTION_PASS. 11143 */ 11144 for (ooa_io = starting_io; ooa_io != NULL; 11145 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11146 ooa_links)){ 11147 11148 /* 11149 * This routine just checks to see whether 11150 * cur_blocked is blocked by ooa_io, which is ahead 11151 * of it in the queue. It doesn't queue/dequeue 11152 * cur_blocked. 11153 */ 11154 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11155 switch (action) { 11156 case CTL_ACTION_BLOCK: 11157 case CTL_ACTION_OVERLAP: 11158 case CTL_ACTION_OVERLAP_TAG: 11159 case CTL_ACTION_SKIP: 11160 case CTL_ACTION_ERROR: 11161 return (action); 11162 break; /* NOTREACHED */ 11163 case CTL_ACTION_PASS: 11164 break; 11165 default: 11166 panic("%s: Invalid action %d\n", __func__, action); 11167 } 11168 } 11169 11170 return (CTL_ACTION_PASS); 11171} 11172 11173/* 11174 * Assumptions: 11175 * - An I/O has just completed, and has been removed from the per-LUN OOA 11176 * queue, so some items on the blocked queue may now be unblocked. 11177 */ 11178static int 11179ctl_check_blocked(struct ctl_lun *lun) 11180{ 11181 struct ctl_softc *softc = lun->ctl_softc; 11182 union ctl_io *cur_blocked, *next_blocked; 11183 11184 mtx_assert(&lun->lun_lock, MA_OWNED); 11185 11186 /* 11187 * Run forward from the head of the blocked queue, checking each 11188 * entry against the I/Os prior to it on the OOA queue to see if 11189 * there is still any blockage. 11190 * 11191 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11192 * with our removing a variable on it while it is traversing the 11193 * list. 11194 */ 11195 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11196 cur_blocked != NULL; cur_blocked = next_blocked) { 11197 union ctl_io *prev_ooa; 11198 ctl_action action; 11199 11200 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11201 blocked_links); 11202 11203 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11204 ctl_ooaq, ooa_links); 11205 11206 /* 11207 * If cur_blocked happens to be the first item in the OOA 11208 * queue now, prev_ooa will be NULL, and the action 11209 * returned will just be CTL_ACTION_PASS. 11210 */ 11211 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11212 11213 switch (action) { 11214 case CTL_ACTION_BLOCK: 11215 /* Nothing to do here, still blocked */ 11216 break; 11217 case CTL_ACTION_OVERLAP: 11218 case CTL_ACTION_OVERLAP_TAG: 11219 /* 11220 * This shouldn't happen! In theory we've already 11221 * checked this command for overlap... 11222 */ 11223 break; 11224 case CTL_ACTION_PASS: 11225 case CTL_ACTION_SKIP: { 11226 const struct ctl_cmd_entry *entry; 11227 11228 /* 11229 * The skip case shouldn't happen, this transaction 11230 * should have never made it onto the blocked queue. 11231 */ 11232 /* 11233 * This I/O is no longer blocked, we can remove it 11234 * from the blocked queue. Since this is a TAILQ 11235 * (doubly linked list), we can do O(1) removals 11236 * from any place on the list. 11237 */ 11238 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11239 blocked_links); 11240 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11241 11242 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11243 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11244 /* 11245 * Need to send IO back to original side to 11246 * run 11247 */ 11248 union ctl_ha_msg msg_info; 11249 11250 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11251 msg_info.hdr.original_sc = 11252 cur_blocked->io_hdr.original_sc; 11253 msg_info.hdr.serializing_sc = cur_blocked; 11254 msg_info.hdr.msg_type = CTL_MSG_R2R; 11255 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11256 sizeof(msg_info.hdr), M_NOWAIT); 11257 break; 11258 } 11259 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11260 11261 /* 11262 * Check this I/O for LUN state changes that may 11263 * have happened while this command was blocked. 11264 * The LUN state may have been changed by a command 11265 * ahead of us in the queue, so we need to re-check 11266 * for any states that can be caused by SCSI 11267 * commands. 11268 */ 11269 if (ctl_scsiio_lun_check(lun, entry, 11270 &cur_blocked->scsiio) == 0) { 11271 cur_blocked->io_hdr.flags |= 11272 CTL_FLAG_IS_WAS_ON_RTR; 11273 ctl_enqueue_rtr(cur_blocked); 11274 } else 11275 ctl_done(cur_blocked); 11276 break; 11277 } 11278 default: 11279 /* 11280 * This probably shouldn't happen -- we shouldn't 11281 * get CTL_ACTION_ERROR, or anything else. 11282 */ 11283 break; 11284 } 11285 } 11286 11287 return (CTL_RETVAL_COMPLETE); 11288} 11289 11290/* 11291 * This routine (with one exception) checks LUN flags that can be set by 11292 * commands ahead of us in the OOA queue. These flags have to be checked 11293 * when a command initially comes in, and when we pull a command off the 11294 * blocked queue and are preparing to execute it. The reason we have to 11295 * check these flags for commands on the blocked queue is that the LUN 11296 * state may have been changed by a command ahead of us while we're on the 11297 * blocked queue. 11298 * 11299 * Ordering is somewhat important with these checks, so please pay 11300 * careful attention to the placement of any new checks. 11301 */ 11302static int 11303ctl_scsiio_lun_check(struct ctl_lun *lun, 11304 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11305{ 11306 struct ctl_softc *softc = lun->ctl_softc; 11307 int retval; 11308 uint32_t residx; 11309 11310 retval = 0; 11311 11312 mtx_assert(&lun->lun_lock, MA_OWNED); 11313 11314 /* 11315 * If this shelf is a secondary shelf controller, we may have to 11316 * reject some commands disallowed by HA mode and link state. 11317 */ 11318 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11319 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11320 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11321 ctl_set_lun_unavail(ctsio); 11322 retval = 1; 11323 goto bailout; 11324 } 11325 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11326 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11327 ctl_set_lun_transit(ctsio); 11328 retval = 1; 11329 goto bailout; 11330 } 11331 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11332 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11333 ctl_set_lun_standby(ctsio); 11334 retval = 1; 11335 goto bailout; 11336 } 11337 11338 /* The rest of checks are only done on executing side */ 11339 if (softc->ha_mode == CTL_HA_MODE_XFER) 11340 goto bailout; 11341 } 11342 11343 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11344 if (lun->be_lun && 11345 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11346 ctl_set_hw_write_protected(ctsio); 11347 retval = 1; 11348 goto bailout; 11349 } 11350 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11351 ctl_set_sense(ctsio, /*current_error*/ 1, 11352 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11353 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11354 retval = 1; 11355 goto bailout; 11356 } 11357 } 11358 11359 /* 11360 * Check for a reservation conflict. If this command isn't allowed 11361 * even on reserved LUNs, and if this initiator isn't the one who 11362 * reserved us, reject the command with a reservation conflict. 11363 */ 11364 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11365 if ((lun->flags & CTL_LUN_RESERVED) 11366 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11367 if (lun->res_idx != residx) { 11368 ctl_set_reservation_conflict(ctsio); 11369 retval = 1; 11370 goto bailout; 11371 } 11372 } 11373 11374 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11375 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11376 /* No reservation or command is allowed. */; 11377 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11378 (lun->pr_res_type == SPR_TYPE_WR_EX || 11379 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11380 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11381 /* The command is allowed for Write Exclusive resv. */; 11382 } else { 11383 /* 11384 * if we aren't registered or it's a res holder type 11385 * reservation and this isn't the res holder then set a 11386 * conflict. 11387 */ 11388 if (ctl_get_prkey(lun, residx) == 0 || 11389 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11390 ctl_set_reservation_conflict(ctsio); 11391 retval = 1; 11392 goto bailout; 11393 } 11394 } 11395 11396 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11397 if (lun->flags & CTL_LUN_EJECTED) 11398 ctl_set_lun_ejected(ctsio); 11399 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11400 if (lun->flags & CTL_LUN_REMOVABLE) 11401 ctl_set_lun_no_media(ctsio); 11402 else 11403 ctl_set_lun_int_reqd(ctsio); 11404 } else if (lun->flags & CTL_LUN_STOPPED) 11405 ctl_set_lun_stopped(ctsio); 11406 else 11407 goto bailout; 11408 retval = 1; 11409 goto bailout; 11410 } 11411 11412bailout: 11413 return (retval); 11414} 11415 11416static void 11417ctl_failover_io(union ctl_io *io, int have_lock) 11418{ 11419 ctl_set_busy(&io->scsiio); 11420 ctl_done(io); 11421} 11422 11423static void 11424ctl_failover_lun(union ctl_io *rio) 11425{ 11426 struct ctl_softc *softc = control_softc; 11427 struct ctl_lun *lun; 11428 struct ctl_io_hdr *io, *next_io; 11429 uint32_t targ_lun; 11430 11431 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11432 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11433 11434 /* Find and lock the LUN. */ 11435 mtx_lock(&softc->ctl_lock); 11436 if ((targ_lun < CTL_MAX_LUNS) && 11437 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11438 mtx_lock(&lun->lun_lock); 11439 mtx_unlock(&softc->ctl_lock); 11440 if (lun->flags & CTL_LUN_DISABLED) { 11441 mtx_unlock(&lun->lun_lock); 11442 return; 11443 } 11444 } else { 11445 mtx_unlock(&softc->ctl_lock); 11446 return; 11447 } 11448 11449 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11450 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11451 /* We are master */ 11452 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11453 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11454 io->flags |= CTL_FLAG_ABORT; 11455 io->flags |= CTL_FLAG_FAILOVER; 11456 } else { /* This can be only due to DATAMOVE */ 11457 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11458 io->flags &= ~CTL_FLAG_DMA_INPROG; 11459 io->flags |= CTL_FLAG_IO_ACTIVE; 11460 io->port_status = 31340; 11461 ctl_enqueue_isc((union ctl_io *)io); 11462 } 11463 } 11464 /* We are slave */ 11465 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11466 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11467 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11468 io->flags |= CTL_FLAG_FAILOVER; 11469 } else { 11470 ctl_set_busy(&((union ctl_io *)io)-> 11471 scsiio); 11472 ctl_done((union ctl_io *)io); 11473 } 11474 } 11475 } 11476 } else { /* SERIALIZE modes */ 11477 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11478 next_io) { 11479 /* We are master */ 11480 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11481 TAILQ_REMOVE(&lun->blocked_queue, io, 11482 blocked_links); 11483 io->flags &= ~CTL_FLAG_BLOCKED; 11484 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11485 ctl_free_io((union ctl_io *)io); 11486 } 11487 } 11488 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11489 /* We are master */ 11490 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11491 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11492 ctl_free_io((union ctl_io *)io); 11493 } 11494 /* We are slave */ 11495 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11496 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11497 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11498 ctl_set_busy(&((union ctl_io *)io)-> 11499 scsiio); 11500 ctl_done((union ctl_io *)io); 11501 } 11502 } 11503 } 11504 ctl_check_blocked(lun); 11505 } 11506 mtx_unlock(&lun->lun_lock); 11507} 11508 11509static int 11510ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11511{ 11512 struct ctl_lun *lun; 11513 const struct ctl_cmd_entry *entry; 11514 uint32_t initidx, targ_lun; 11515 int retval; 11516 11517 retval = 0; 11518 11519 lun = NULL; 11520 11521 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11522 if ((targ_lun < CTL_MAX_LUNS) 11523 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11524 /* 11525 * If the LUN is invalid, pretend that it doesn't exist. 11526 * It will go away as soon as all pending I/O has been 11527 * completed. 11528 */ 11529 mtx_lock(&lun->lun_lock); 11530 if (lun->flags & CTL_LUN_DISABLED) { 11531 mtx_unlock(&lun->lun_lock); 11532 lun = NULL; 11533 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11534 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11535 } else { 11536 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11537 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11538 lun->be_lun; 11539 11540 /* 11541 * Every I/O goes into the OOA queue for a 11542 * particular LUN, and stays there until completion. 11543 */ 11544#ifdef CTL_TIME_IO 11545 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11546 lun->idle_time += getsbinuptime() - 11547 lun->last_busy; 11548 } 11549#endif 11550 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11551 ooa_links); 11552 } 11553 } else { 11554 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11555 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11556 } 11557 11558 /* Get command entry and return error if it is unsuppotyed. */ 11559 entry = ctl_validate_command(ctsio); 11560 if (entry == NULL) { 11561 if (lun) 11562 mtx_unlock(&lun->lun_lock); 11563 return (retval); 11564 } 11565 11566 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11567 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11568 11569 /* 11570 * Check to see whether we can send this command to LUNs that don't 11571 * exist. This should pretty much only be the case for inquiry 11572 * and request sense. Further checks, below, really require having 11573 * a LUN, so we can't really check the command anymore. Just put 11574 * it on the rtr queue. 11575 */ 11576 if (lun == NULL) { 11577 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11578 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11579 ctl_enqueue_rtr((union ctl_io *)ctsio); 11580 return (retval); 11581 } 11582 11583 ctl_set_unsupported_lun(ctsio); 11584 ctl_done((union ctl_io *)ctsio); 11585 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11586 return (retval); 11587 } else { 11588 /* 11589 * Make sure we support this particular command on this LUN. 11590 * e.g., we don't support writes to the control LUN. 11591 */ 11592 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11593 mtx_unlock(&lun->lun_lock); 11594 ctl_set_invalid_opcode(ctsio); 11595 ctl_done((union ctl_io *)ctsio); 11596 return (retval); 11597 } 11598 } 11599 11600 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11601 11602#ifdef CTL_WITH_CA 11603 /* 11604 * If we've got a request sense, it'll clear the contingent 11605 * allegiance condition. Otherwise, if we have a CA condition for 11606 * this initiator, clear it, because it sent down a command other 11607 * than request sense. 11608 */ 11609 if ((ctsio->cdb[0] != REQUEST_SENSE) 11610 && (ctl_is_set(lun->have_ca, initidx))) 11611 ctl_clear_mask(lun->have_ca, initidx); 11612#endif 11613 11614 /* 11615 * If the command has this flag set, it handles its own unit 11616 * attention reporting, we shouldn't do anything. Otherwise we 11617 * check for any pending unit attentions, and send them back to the 11618 * initiator. We only do this when a command initially comes in, 11619 * not when we pull it off the blocked queue. 11620 * 11621 * According to SAM-3, section 5.3.2, the order that things get 11622 * presented back to the host is basically unit attentions caused 11623 * by some sort of reset event, busy status, reservation conflicts 11624 * or task set full, and finally any other status. 11625 * 11626 * One issue here is that some of the unit attentions we report 11627 * don't fall into the "reset" category (e.g. "reported luns data 11628 * has changed"). So reporting it here, before the reservation 11629 * check, may be technically wrong. I guess the only thing to do 11630 * would be to check for and report the reset events here, and then 11631 * check for the other unit attention types after we check for a 11632 * reservation conflict. 11633 * 11634 * XXX KDM need to fix this 11635 */ 11636 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11637 ctl_ua_type ua_type; 11638 11639 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11640 SSD_TYPE_NONE); 11641 if (ua_type != CTL_UA_NONE) { 11642 mtx_unlock(&lun->lun_lock); 11643 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11644 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11645 ctsio->sense_len = SSD_FULL_SIZE; 11646 ctl_done((union ctl_io *)ctsio); 11647 return (retval); 11648 } 11649 } 11650 11651 11652 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11653 mtx_unlock(&lun->lun_lock); 11654 ctl_done((union ctl_io *)ctsio); 11655 return (retval); 11656 } 11657 11658 /* 11659 * XXX CHD this is where we want to send IO to other side if 11660 * this LUN is secondary on this SC. We will need to make a copy 11661 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11662 * the copy we send as FROM_OTHER. 11663 * We also need to stuff the address of the original IO so we can 11664 * find it easily. Something similar will need be done on the other 11665 * side so when we are done we can find the copy. 11666 */ 11667 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11668 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11669 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11670 union ctl_ha_msg msg_info; 11671 int isc_retval; 11672 11673 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11674 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11675 mtx_unlock(&lun->lun_lock); 11676 11677 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11678 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11679 msg_info.hdr.serializing_sc = NULL; 11680 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11681 msg_info.scsi.tag_num = ctsio->tag_num; 11682 msg_info.scsi.tag_type = ctsio->tag_type; 11683 msg_info.scsi.cdb_len = ctsio->cdb_len; 11684 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11685 11686 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11687 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11688 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11689 ctl_set_busy(ctsio); 11690 ctl_done((union ctl_io *)ctsio); 11691 return (retval); 11692 } 11693 return (retval); 11694 } 11695 11696 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11697 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11698 ctl_ooaq, ooa_links))) { 11699 case CTL_ACTION_BLOCK: 11700 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11701 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11702 blocked_links); 11703 mtx_unlock(&lun->lun_lock); 11704 return (retval); 11705 case CTL_ACTION_PASS: 11706 case CTL_ACTION_SKIP: 11707 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11708 mtx_unlock(&lun->lun_lock); 11709 ctl_enqueue_rtr((union ctl_io *)ctsio); 11710 break; 11711 case CTL_ACTION_OVERLAP: 11712 mtx_unlock(&lun->lun_lock); 11713 ctl_set_overlapped_cmd(ctsio); 11714 ctl_done((union ctl_io *)ctsio); 11715 break; 11716 case CTL_ACTION_OVERLAP_TAG: 11717 mtx_unlock(&lun->lun_lock); 11718 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11719 ctl_done((union ctl_io *)ctsio); 11720 break; 11721 case CTL_ACTION_ERROR: 11722 default: 11723 mtx_unlock(&lun->lun_lock); 11724 ctl_set_internal_failure(ctsio, 11725 /*sks_valid*/ 0, 11726 /*retry_count*/ 0); 11727 ctl_done((union ctl_io *)ctsio); 11728 break; 11729 } 11730 return (retval); 11731} 11732 11733const struct ctl_cmd_entry * 11734ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11735{ 11736 const struct ctl_cmd_entry *entry; 11737 int service_action; 11738 11739 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11740 if (sa) 11741 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11742 if (entry->flags & CTL_CMD_FLAG_SA5) { 11743 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11744 entry = &((const struct ctl_cmd_entry *) 11745 entry->execute)[service_action]; 11746 } 11747 return (entry); 11748} 11749 11750const struct ctl_cmd_entry * 11751ctl_validate_command(struct ctl_scsiio *ctsio) 11752{ 11753 const struct ctl_cmd_entry *entry; 11754 int i, sa; 11755 uint8_t diff; 11756 11757 entry = ctl_get_cmd_entry(ctsio, &sa); 11758 if (entry->execute == NULL) { 11759 if (sa) 11760 ctl_set_invalid_field(ctsio, 11761 /*sks_valid*/ 1, 11762 /*command*/ 1, 11763 /*field*/ 1, 11764 /*bit_valid*/ 1, 11765 /*bit*/ 4); 11766 else 11767 ctl_set_invalid_opcode(ctsio); 11768 ctl_done((union ctl_io *)ctsio); 11769 return (NULL); 11770 } 11771 KASSERT(entry->length > 0, 11772 ("Not defined length for command 0x%02x/0x%02x", 11773 ctsio->cdb[0], ctsio->cdb[1])); 11774 for (i = 1; i < entry->length; i++) { 11775 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11776 if (diff == 0) 11777 continue; 11778 ctl_set_invalid_field(ctsio, 11779 /*sks_valid*/ 1, 11780 /*command*/ 1, 11781 /*field*/ i, 11782 /*bit_valid*/ 1, 11783 /*bit*/ fls(diff) - 1); 11784 ctl_done((union ctl_io *)ctsio); 11785 return (NULL); 11786 } 11787 return (entry); 11788} 11789 11790static int 11791ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11792{ 11793 11794 switch (lun_type) { 11795 case T_DIRECT: 11796 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11797 return (0); 11798 break; 11799 case T_PROCESSOR: 11800 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11801 return (0); 11802 break; 11803 case T_CDROM: 11804 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11805 return (0); 11806 break; 11807 default: 11808 return (0); 11809 } 11810 return (1); 11811} 11812 11813static int 11814ctl_scsiio(struct ctl_scsiio *ctsio) 11815{ 11816 int retval; 11817 const struct ctl_cmd_entry *entry; 11818 11819 retval = CTL_RETVAL_COMPLETE; 11820 11821 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11822 11823 entry = ctl_get_cmd_entry(ctsio, NULL); 11824 11825 /* 11826 * If this I/O has been aborted, just send it straight to 11827 * ctl_done() without executing it. 11828 */ 11829 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11830 ctl_done((union ctl_io *)ctsio); 11831 goto bailout; 11832 } 11833 11834 /* 11835 * All the checks should have been handled by ctl_scsiio_precheck(). 11836 * We should be clear now to just execute the I/O. 11837 */ 11838 retval = entry->execute(ctsio); 11839 11840bailout: 11841 return (retval); 11842} 11843 11844/* 11845 * Since we only implement one target right now, a bus reset simply resets 11846 * our single target. 11847 */ 11848static int 11849ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11850{ 11851 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11852} 11853 11854static int 11855ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11856 ctl_ua_type ua_type) 11857{ 11858 struct ctl_port *port; 11859 struct ctl_lun *lun; 11860 int retval; 11861 11862 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11863 union ctl_ha_msg msg_info; 11864 11865 msg_info.hdr.nexus = io->io_hdr.nexus; 11866 if (ua_type==CTL_UA_TARG_RESET) 11867 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11868 else 11869 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11870 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11871 msg_info.hdr.original_sc = NULL; 11872 msg_info.hdr.serializing_sc = NULL; 11873 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11874 sizeof(msg_info.task), M_WAITOK); 11875 } 11876 retval = 0; 11877 11878 mtx_lock(&softc->ctl_lock); 11879 port = ctl_io_port(&io->io_hdr); 11880 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11881 if (port != NULL && 11882 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11883 continue; 11884 retval += ctl_do_lun_reset(lun, io, ua_type); 11885 } 11886 mtx_unlock(&softc->ctl_lock); 11887 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11888 return (retval); 11889} 11890 11891/* 11892 * The LUN should always be set. The I/O is optional, and is used to 11893 * distinguish between I/Os sent by this initiator, and by other 11894 * initiators. We set unit attention for initiators other than this one. 11895 * SAM-3 is vague on this point. It does say that a unit attention should 11896 * be established for other initiators when a LUN is reset (see section 11897 * 5.7.3), but it doesn't specifically say that the unit attention should 11898 * be established for this particular initiator when a LUN is reset. Here 11899 * is the relevant text, from SAM-3 rev 8: 11900 * 11901 * 5.7.2 When a SCSI initiator port aborts its own tasks 11902 * 11903 * When a SCSI initiator port causes its own task(s) to be aborted, no 11904 * notification that the task(s) have been aborted shall be returned to 11905 * the SCSI initiator port other than the completion response for the 11906 * command or task management function action that caused the task(s) to 11907 * be aborted and notification(s) associated with related effects of the 11908 * action (e.g., a reset unit attention condition). 11909 * 11910 * XXX KDM for now, we're setting unit attention for all initiators. 11911 */ 11912static int 11913ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11914{ 11915 union ctl_io *xio; 11916#if 0 11917 uint32_t initidx; 11918#endif 11919 int i; 11920 11921 mtx_lock(&lun->lun_lock); 11922 /* 11923 * Run through the OOA queue and abort each I/O. 11924 */ 11925 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11926 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11927 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11928 } 11929 11930 /* 11931 * This version sets unit attention for every 11932 */ 11933#if 0 11934 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11935 ctl_est_ua_all(lun, initidx, ua_type); 11936#else 11937 ctl_est_ua_all(lun, -1, ua_type); 11938#endif 11939 11940 /* 11941 * A reset (any kind, really) clears reservations established with 11942 * RESERVE/RELEASE. It does not clear reservations established 11943 * with PERSISTENT RESERVE OUT, but we don't support that at the 11944 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11945 * reservations made with the RESERVE/RELEASE commands, because 11946 * those commands are obsolete in SPC-3. 11947 */ 11948 lun->flags &= ~CTL_LUN_RESERVED; 11949 11950#ifdef CTL_WITH_CA 11951 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11952 ctl_clear_mask(lun->have_ca, i); 11953#endif 11954 lun->prevent_count = 0; 11955 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11956 ctl_clear_mask(lun->prevent, i); 11957 mtx_unlock(&lun->lun_lock); 11958 11959 return (0); 11960} 11961 11962static int 11963ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11964{ 11965 struct ctl_lun *lun; 11966 uint32_t targ_lun; 11967 int retval; 11968 11969 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11970 mtx_lock(&softc->ctl_lock); 11971 if ((targ_lun >= CTL_MAX_LUNS) || 11972 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11973 mtx_unlock(&softc->ctl_lock); 11974 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11975 return (1); 11976 } 11977 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11978 mtx_unlock(&softc->ctl_lock); 11979 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11980 11981 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11982 union ctl_ha_msg msg_info; 11983 11984 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11985 msg_info.hdr.nexus = io->io_hdr.nexus; 11986 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11987 msg_info.hdr.original_sc = NULL; 11988 msg_info.hdr.serializing_sc = NULL; 11989 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11990 sizeof(msg_info.task), M_WAITOK); 11991 } 11992 return (retval); 11993} 11994 11995static void 11996ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11997 int other_sc) 11998{ 11999 union ctl_io *xio; 12000 12001 mtx_assert(&lun->lun_lock, MA_OWNED); 12002 12003 /* 12004 * Run through the OOA queue and attempt to find the given I/O. 12005 * The target port, initiator ID, tag type and tag number have to 12006 * match the values that we got from the initiator. If we have an 12007 * untagged command to abort, simply abort the first untagged command 12008 * we come to. We only allow one untagged command at a time of course. 12009 */ 12010 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12011 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12012 12013 if ((targ_port == UINT32_MAX || 12014 targ_port == xio->io_hdr.nexus.targ_port) && 12015 (init_id == UINT32_MAX || 12016 init_id == xio->io_hdr.nexus.initid)) { 12017 if (targ_port != xio->io_hdr.nexus.targ_port || 12018 init_id != xio->io_hdr.nexus.initid) 12019 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12020 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12021 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12022 union ctl_ha_msg msg_info; 12023 12024 msg_info.hdr.nexus = xio->io_hdr.nexus; 12025 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12026 msg_info.task.tag_num = xio->scsiio.tag_num; 12027 msg_info.task.tag_type = xio->scsiio.tag_type; 12028 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12029 msg_info.hdr.original_sc = NULL; 12030 msg_info.hdr.serializing_sc = NULL; 12031 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12032 sizeof(msg_info.task), M_NOWAIT); 12033 } 12034 } 12035 } 12036} 12037 12038static int 12039ctl_abort_task_set(union ctl_io *io) 12040{ 12041 struct ctl_softc *softc = control_softc; 12042 struct ctl_lun *lun; 12043 uint32_t targ_lun; 12044 12045 /* 12046 * Look up the LUN. 12047 */ 12048 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12049 mtx_lock(&softc->ctl_lock); 12050 if ((targ_lun >= CTL_MAX_LUNS) || 12051 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12052 mtx_unlock(&softc->ctl_lock); 12053 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12054 return (1); 12055 } 12056 12057 mtx_lock(&lun->lun_lock); 12058 mtx_unlock(&softc->ctl_lock); 12059 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12060 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12061 io->io_hdr.nexus.initid, 12062 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12063 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12064 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12065 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12066 } 12067 mtx_unlock(&lun->lun_lock); 12068 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12069 return (0); 12070} 12071 12072static int 12073ctl_i_t_nexus_reset(union ctl_io *io) 12074{ 12075 struct ctl_softc *softc = control_softc; 12076 struct ctl_lun *lun; 12077 uint32_t initidx; 12078 12079 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12080 union ctl_ha_msg msg_info; 12081 12082 msg_info.hdr.nexus = io->io_hdr.nexus; 12083 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12084 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12085 msg_info.hdr.original_sc = NULL; 12086 msg_info.hdr.serializing_sc = NULL; 12087 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12088 sizeof(msg_info.task), M_WAITOK); 12089 } 12090 12091 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12092 mtx_lock(&softc->ctl_lock); 12093 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12094 mtx_lock(&lun->lun_lock); 12095 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12096 io->io_hdr.nexus.initid, 1); 12097#ifdef CTL_WITH_CA 12098 ctl_clear_mask(lun->have_ca, initidx); 12099#endif 12100 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12101 lun->flags &= ~CTL_LUN_RESERVED; 12102 if (ctl_is_set(lun->prevent, initidx)) { 12103 ctl_clear_mask(lun->prevent, initidx); 12104 lun->prevent_count--; 12105 } 12106 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12107 mtx_unlock(&lun->lun_lock); 12108 } 12109 mtx_unlock(&softc->ctl_lock); 12110 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12111 return (0); 12112} 12113 12114static int 12115ctl_abort_task(union ctl_io *io) 12116{ 12117 union ctl_io *xio; 12118 struct ctl_lun *lun; 12119 struct ctl_softc *softc; 12120#if 0 12121 struct sbuf sb; 12122 char printbuf[128]; 12123#endif 12124 int found; 12125 uint32_t targ_lun; 12126 12127 softc = control_softc; 12128 found = 0; 12129 12130 /* 12131 * Look up the LUN. 12132 */ 12133 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12134 mtx_lock(&softc->ctl_lock); 12135 if ((targ_lun >= CTL_MAX_LUNS) || 12136 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12137 mtx_unlock(&softc->ctl_lock); 12138 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12139 return (1); 12140 } 12141 12142#if 0 12143 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12144 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12145#endif 12146 12147 mtx_lock(&lun->lun_lock); 12148 mtx_unlock(&softc->ctl_lock); 12149 /* 12150 * Run through the OOA queue and attempt to find the given I/O. 12151 * The target port, initiator ID, tag type and tag number have to 12152 * match the values that we got from the initiator. If we have an 12153 * untagged command to abort, simply abort the first untagged command 12154 * we come to. We only allow one untagged command at a time of course. 12155 */ 12156 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12157 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12158#if 0 12159 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12160 12161 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12162 lun->lun, xio->scsiio.tag_num, 12163 xio->scsiio.tag_type, 12164 (xio->io_hdr.blocked_links.tqe_prev 12165 == NULL) ? "" : " BLOCKED", 12166 (xio->io_hdr.flags & 12167 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12168 (xio->io_hdr.flags & 12169 CTL_FLAG_ABORT) ? " ABORT" : "", 12170 (xio->io_hdr.flags & 12171 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12172 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12173 sbuf_finish(&sb); 12174 printf("%s\n", sbuf_data(&sb)); 12175#endif 12176 12177 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12178 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12179 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12180 continue; 12181 12182 /* 12183 * If the abort says that the task is untagged, the 12184 * task in the queue must be untagged. Otherwise, 12185 * we just check to see whether the tag numbers 12186 * match. This is because the QLogic firmware 12187 * doesn't pass back the tag type in an abort 12188 * request. 12189 */ 12190#if 0 12191 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12192 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12193 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12194#endif 12195 /* 12196 * XXX KDM we've got problems with FC, because it 12197 * doesn't send down a tag type with aborts. So we 12198 * can only really go by the tag number... 12199 * This may cause problems with parallel SCSI. 12200 * Need to figure that out!! 12201 */ 12202 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12203 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12204 found = 1; 12205 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12206 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12207 union ctl_ha_msg msg_info; 12208 12209 msg_info.hdr.nexus = io->io_hdr.nexus; 12210 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12211 msg_info.task.tag_num = io->taskio.tag_num; 12212 msg_info.task.tag_type = io->taskio.tag_type; 12213 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12214 msg_info.hdr.original_sc = NULL; 12215 msg_info.hdr.serializing_sc = NULL; 12216#if 0 12217 printf("Sent Abort to other side\n"); 12218#endif 12219 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12220 sizeof(msg_info.task), M_NOWAIT); 12221 } 12222#if 0 12223 printf("ctl_abort_task: found I/O to abort\n"); 12224#endif 12225 } 12226 } 12227 mtx_unlock(&lun->lun_lock); 12228 12229 if (found == 0) { 12230 /* 12231 * This isn't really an error. It's entirely possible for 12232 * the abort and command completion to cross on the wire. 12233 * This is more of an informative/diagnostic error. 12234 */ 12235#if 0 12236 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12237 "%u:%u:%u tag %d type %d\n", 12238 io->io_hdr.nexus.initid, 12239 io->io_hdr.nexus.targ_port, 12240 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12241 io->taskio.tag_type); 12242#endif 12243 } 12244 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12245 return (0); 12246} 12247 12248static int 12249ctl_query_task(union ctl_io *io, int task_set) 12250{ 12251 union ctl_io *xio; 12252 struct ctl_lun *lun; 12253 struct ctl_softc *softc; 12254 int found = 0; 12255 uint32_t targ_lun; 12256 12257 softc = control_softc; 12258 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12259 mtx_lock(&softc->ctl_lock); 12260 if ((targ_lun >= CTL_MAX_LUNS) || 12261 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12262 mtx_unlock(&softc->ctl_lock); 12263 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12264 return (1); 12265 } 12266 mtx_lock(&lun->lun_lock); 12267 mtx_unlock(&softc->ctl_lock); 12268 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12269 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12270 12271 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12272 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12273 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12274 continue; 12275 12276 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12277 found = 1; 12278 break; 12279 } 12280 } 12281 mtx_unlock(&lun->lun_lock); 12282 if (found) 12283 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12284 else 12285 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12286 return (0); 12287} 12288 12289static int 12290ctl_query_async_event(union ctl_io *io) 12291{ 12292 struct ctl_lun *lun; 12293 struct ctl_softc *softc; 12294 ctl_ua_type ua; 12295 uint32_t targ_lun, initidx; 12296 12297 softc = control_softc; 12298 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12299 mtx_lock(&softc->ctl_lock); 12300 if ((targ_lun >= CTL_MAX_LUNS) || 12301 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12302 mtx_unlock(&softc->ctl_lock); 12303 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12304 return (1); 12305 } 12306 mtx_lock(&lun->lun_lock); 12307 mtx_unlock(&softc->ctl_lock); 12308 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12309 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12310 mtx_unlock(&lun->lun_lock); 12311 if (ua != CTL_UA_NONE) 12312 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12313 else 12314 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12315 return (0); 12316} 12317 12318static void 12319ctl_run_task(union ctl_io *io) 12320{ 12321 struct ctl_softc *softc = control_softc; 12322 int retval = 1; 12323 12324 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12325 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12326 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12327 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12328 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12329 switch (io->taskio.task_action) { 12330 case CTL_TASK_ABORT_TASK: 12331 retval = ctl_abort_task(io); 12332 break; 12333 case CTL_TASK_ABORT_TASK_SET: 12334 case CTL_TASK_CLEAR_TASK_SET: 12335 retval = ctl_abort_task_set(io); 12336 break; 12337 case CTL_TASK_CLEAR_ACA: 12338 break; 12339 case CTL_TASK_I_T_NEXUS_RESET: 12340 retval = ctl_i_t_nexus_reset(io); 12341 break; 12342 case CTL_TASK_LUN_RESET: 12343 retval = ctl_lun_reset(softc, io); 12344 break; 12345 case CTL_TASK_TARGET_RESET: 12346 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12347 break; 12348 case CTL_TASK_BUS_RESET: 12349 retval = ctl_bus_reset(softc, io); 12350 break; 12351 case CTL_TASK_PORT_LOGIN: 12352 break; 12353 case CTL_TASK_PORT_LOGOUT: 12354 break; 12355 case CTL_TASK_QUERY_TASK: 12356 retval = ctl_query_task(io, 0); 12357 break; 12358 case CTL_TASK_QUERY_TASK_SET: 12359 retval = ctl_query_task(io, 1); 12360 break; 12361 case CTL_TASK_QUERY_ASYNC_EVENT: 12362 retval = ctl_query_async_event(io); 12363 break; 12364 default: 12365 printf("%s: got unknown task management event %d\n", 12366 __func__, io->taskio.task_action); 12367 break; 12368 } 12369 if (retval == 0) 12370 io->io_hdr.status = CTL_SUCCESS; 12371 else 12372 io->io_hdr.status = CTL_ERROR; 12373 ctl_done(io); 12374} 12375 12376/* 12377 * For HA operation. Handle commands that come in from the other 12378 * controller. 12379 */ 12380static void 12381ctl_handle_isc(union ctl_io *io) 12382{ 12383 int free_io; 12384 struct ctl_lun *lun; 12385 struct ctl_softc *softc = control_softc; 12386 uint32_t targ_lun; 12387 12388 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12389 lun = softc->ctl_luns[targ_lun]; 12390 12391 switch (io->io_hdr.msg_type) { 12392 case CTL_MSG_SERIALIZE: 12393 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12394 break; 12395 case CTL_MSG_R2R: { 12396 const struct ctl_cmd_entry *entry; 12397 12398 /* 12399 * This is only used in SER_ONLY mode. 12400 */ 12401 free_io = 0; 12402 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12403 mtx_lock(&lun->lun_lock); 12404 if (ctl_scsiio_lun_check(lun, 12405 entry, (struct ctl_scsiio *)io) != 0) { 12406 mtx_unlock(&lun->lun_lock); 12407 ctl_done(io); 12408 break; 12409 } 12410 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12411 mtx_unlock(&lun->lun_lock); 12412 ctl_enqueue_rtr(io); 12413 break; 12414 } 12415 case CTL_MSG_FINISH_IO: 12416 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12417 free_io = 0; 12418 ctl_done(io); 12419 } else { 12420 free_io = 1; 12421 mtx_lock(&lun->lun_lock); 12422 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12423 ooa_links); 12424 ctl_check_blocked(lun); 12425 mtx_unlock(&lun->lun_lock); 12426 } 12427 break; 12428 case CTL_MSG_PERS_ACTION: 12429 ctl_hndl_per_res_out_on_other_sc( 12430 (union ctl_ha_msg *)&io->presio.pr_msg); 12431 free_io = 1; 12432 break; 12433 case CTL_MSG_BAD_JUJU: 12434 free_io = 0; 12435 ctl_done(io); 12436 break; 12437 case CTL_MSG_DATAMOVE: 12438 /* Only used in XFER mode */ 12439 free_io = 0; 12440 ctl_datamove_remote(io); 12441 break; 12442 case CTL_MSG_DATAMOVE_DONE: 12443 /* Only used in XFER mode */ 12444 free_io = 0; 12445 io->scsiio.be_move_done(io); 12446 break; 12447 case CTL_MSG_FAILOVER: 12448 ctl_failover_lun(io); 12449 free_io = 1; 12450 break; 12451 default: 12452 free_io = 1; 12453 printf("%s: Invalid message type %d\n", 12454 __func__, io->io_hdr.msg_type); 12455 break; 12456 } 12457 if (free_io) 12458 ctl_free_io(io); 12459 12460} 12461 12462 12463/* 12464 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12465 * there is no match. 12466 */ 12467static ctl_lun_error_pattern 12468ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12469{ 12470 const struct ctl_cmd_entry *entry; 12471 ctl_lun_error_pattern filtered_pattern, pattern; 12472 12473 pattern = desc->error_pattern; 12474 12475 /* 12476 * XXX KDM we need more data passed into this function to match a 12477 * custom pattern, and we actually need to implement custom pattern 12478 * matching. 12479 */ 12480 if (pattern & CTL_LUN_PAT_CMD) 12481 return (CTL_LUN_PAT_CMD); 12482 12483 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12484 return (CTL_LUN_PAT_ANY); 12485 12486 entry = ctl_get_cmd_entry(ctsio, NULL); 12487 12488 filtered_pattern = entry->pattern & pattern; 12489 12490 /* 12491 * If the user requested specific flags in the pattern (e.g. 12492 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12493 * flags. 12494 * 12495 * If the user did not specify any flags, it doesn't matter whether 12496 * or not the command supports the flags. 12497 */ 12498 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12499 (pattern & ~CTL_LUN_PAT_MASK)) 12500 return (CTL_LUN_PAT_NONE); 12501 12502 /* 12503 * If the user asked for a range check, see if the requested LBA 12504 * range overlaps with this command's LBA range. 12505 */ 12506 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12507 uint64_t lba1; 12508 uint64_t len1; 12509 ctl_action action; 12510 int retval; 12511 12512 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12513 if (retval != 0) 12514 return (CTL_LUN_PAT_NONE); 12515 12516 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12517 desc->lba_range.len, FALSE); 12518 /* 12519 * A "pass" means that the LBA ranges don't overlap, so 12520 * this doesn't match the user's range criteria. 12521 */ 12522 if (action == CTL_ACTION_PASS) 12523 return (CTL_LUN_PAT_NONE); 12524 } 12525 12526 return (filtered_pattern); 12527} 12528 12529static void 12530ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12531{ 12532 struct ctl_error_desc *desc, *desc2; 12533 12534 mtx_assert(&lun->lun_lock, MA_OWNED); 12535 12536 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12537 ctl_lun_error_pattern pattern; 12538 /* 12539 * Check to see whether this particular command matches 12540 * the pattern in the descriptor. 12541 */ 12542 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12543 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12544 continue; 12545 12546 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12547 case CTL_LUN_INJ_ABORTED: 12548 ctl_set_aborted(&io->scsiio); 12549 break; 12550 case CTL_LUN_INJ_MEDIUM_ERR: 12551 ctl_set_medium_error(&io->scsiio, 12552 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12553 CTL_FLAG_DATA_OUT); 12554 break; 12555 case CTL_LUN_INJ_UA: 12556 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12557 * OCCURRED */ 12558 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12559 break; 12560 case CTL_LUN_INJ_CUSTOM: 12561 /* 12562 * We're assuming the user knows what he is doing. 12563 * Just copy the sense information without doing 12564 * checks. 12565 */ 12566 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12567 MIN(sizeof(desc->custom_sense), 12568 sizeof(io->scsiio.sense_data))); 12569 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12570 io->scsiio.sense_len = SSD_FULL_SIZE; 12571 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12572 break; 12573 case CTL_LUN_INJ_NONE: 12574 default: 12575 /* 12576 * If this is an error injection type we don't know 12577 * about, clear the continuous flag (if it is set) 12578 * so it will get deleted below. 12579 */ 12580 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12581 break; 12582 } 12583 /* 12584 * By default, each error injection action is a one-shot 12585 */ 12586 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12587 continue; 12588 12589 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12590 12591 free(desc, M_CTL); 12592 } 12593} 12594 12595#ifdef CTL_IO_DELAY 12596static void 12597ctl_datamove_timer_wakeup(void *arg) 12598{ 12599 union ctl_io *io; 12600 12601 io = (union ctl_io *)arg; 12602 12603 ctl_datamove(io); 12604} 12605#endif /* CTL_IO_DELAY */ 12606 12607void 12608ctl_datamove(union ctl_io *io) 12609{ 12610 struct ctl_lun *lun; 12611 void (*fe_datamove)(union ctl_io *io); 12612 12613 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12614 12615 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12616 12617 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12618#ifdef CTL_TIME_IO 12619 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12620 char str[256]; 12621 char path_str[64]; 12622 struct sbuf sb; 12623 12624 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12625 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12626 12627 sbuf_cat(&sb, path_str); 12628 switch (io->io_hdr.io_type) { 12629 case CTL_IO_SCSI: 12630 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12631 sbuf_printf(&sb, "\n"); 12632 sbuf_cat(&sb, path_str); 12633 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12634 io->scsiio.tag_num, io->scsiio.tag_type); 12635 break; 12636 case CTL_IO_TASK: 12637 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12638 "Tag Type: %d\n", io->taskio.task_action, 12639 io->taskio.tag_num, io->taskio.tag_type); 12640 break; 12641 default: 12642 panic("%s: Invalid CTL I/O type %d\n", 12643 __func__, io->io_hdr.io_type); 12644 } 12645 sbuf_cat(&sb, path_str); 12646 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12647 (intmax_t)time_uptime - io->io_hdr.start_time); 12648 sbuf_finish(&sb); 12649 printf("%s", sbuf_data(&sb)); 12650 } 12651#endif /* CTL_TIME_IO */ 12652 12653#ifdef CTL_IO_DELAY 12654 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12655 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12656 } else { 12657 if ((lun != NULL) 12658 && (lun->delay_info.datamove_delay > 0)) { 12659 12660 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12661 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12662 callout_reset(&io->io_hdr.delay_callout, 12663 lun->delay_info.datamove_delay * hz, 12664 ctl_datamove_timer_wakeup, io); 12665 if (lun->delay_info.datamove_type == 12666 CTL_DELAY_TYPE_ONESHOT) 12667 lun->delay_info.datamove_delay = 0; 12668 return; 12669 } 12670 } 12671#endif 12672 12673 /* 12674 * This command has been aborted. Set the port status, so we fail 12675 * the data move. 12676 */ 12677 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12678 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12679 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12680 io->io_hdr.nexus.targ_port, 12681 io->io_hdr.nexus.targ_lun); 12682 io->io_hdr.port_status = 31337; 12683 /* 12684 * Note that the backend, in this case, will get the 12685 * callback in its context. In other cases it may get 12686 * called in the frontend's interrupt thread context. 12687 */ 12688 io->scsiio.be_move_done(io); 12689 return; 12690 } 12691 12692 /* Don't confuse frontend with zero length data move. */ 12693 if (io->scsiio.kern_data_len == 0) { 12694 io->scsiio.be_move_done(io); 12695 return; 12696 } 12697 12698 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12699 fe_datamove(io); 12700} 12701 12702static void 12703ctl_send_datamove_done(union ctl_io *io, int have_lock) 12704{ 12705 union ctl_ha_msg msg; 12706#ifdef CTL_TIME_IO 12707 struct bintime cur_bt; 12708#endif 12709 12710 memset(&msg, 0, sizeof(msg)); 12711 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12712 msg.hdr.original_sc = io; 12713 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12714 msg.hdr.nexus = io->io_hdr.nexus; 12715 msg.hdr.status = io->io_hdr.status; 12716 msg.scsi.tag_num = io->scsiio.tag_num; 12717 msg.scsi.tag_type = io->scsiio.tag_type; 12718 msg.scsi.scsi_status = io->scsiio.scsi_status; 12719 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12720 io->scsiio.sense_len); 12721 msg.scsi.sense_len = io->scsiio.sense_len; 12722 msg.scsi.sense_residual = io->scsiio.sense_residual; 12723 msg.scsi.fetd_status = io->io_hdr.port_status; 12724 msg.scsi.residual = io->scsiio.residual; 12725 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12726 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12727 ctl_failover_io(io, /*have_lock*/ have_lock); 12728 return; 12729 } 12730 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12731 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12732 msg.scsi.sense_len, M_WAITOK); 12733 12734#ifdef CTL_TIME_IO 12735 getbinuptime(&cur_bt); 12736 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12737 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12738#endif 12739 io->io_hdr.num_dmas++; 12740} 12741 12742/* 12743 * The DMA to the remote side is done, now we need to tell the other side 12744 * we're done so it can continue with its data movement. 12745 */ 12746static void 12747ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12748{ 12749 union ctl_io *io; 12750 uint32_t i; 12751 12752 io = rq->context; 12753 12754 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12755 printf("%s: ISC DMA write failed with error %d", __func__, 12756 rq->ret); 12757 ctl_set_internal_failure(&io->scsiio, 12758 /*sks_valid*/ 1, 12759 /*retry_count*/ rq->ret); 12760 } 12761 12762 ctl_dt_req_free(rq); 12763 12764 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12765 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12766 free(io->io_hdr.remote_sglist, M_CTL); 12767 io->io_hdr.remote_sglist = NULL; 12768 io->io_hdr.local_sglist = NULL; 12769 12770 /* 12771 * The data is in local and remote memory, so now we need to send 12772 * status (good or back) back to the other side. 12773 */ 12774 ctl_send_datamove_done(io, /*have_lock*/ 0); 12775} 12776 12777/* 12778 * We've moved the data from the host/controller into local memory. Now we 12779 * need to push it over to the remote controller's memory. 12780 */ 12781static int 12782ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12783{ 12784 int retval; 12785 12786 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12787 ctl_datamove_remote_write_cb); 12788 return (retval); 12789} 12790 12791static void 12792ctl_datamove_remote_write(union ctl_io *io) 12793{ 12794 int retval; 12795 void (*fe_datamove)(union ctl_io *io); 12796 12797 /* 12798 * - Get the data from the host/HBA into local memory. 12799 * - DMA memory from the local controller to the remote controller. 12800 * - Send status back to the remote controller. 12801 */ 12802 12803 retval = ctl_datamove_remote_sgl_setup(io); 12804 if (retval != 0) 12805 return; 12806 12807 /* Switch the pointer over so the FETD knows what to do */ 12808 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12809 12810 /* 12811 * Use a custom move done callback, since we need to send completion 12812 * back to the other controller, not to the backend on this side. 12813 */ 12814 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12815 12816 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12817 fe_datamove(io); 12818} 12819 12820static int 12821ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12822{ 12823#if 0 12824 char str[256]; 12825 char path_str[64]; 12826 struct sbuf sb; 12827#endif 12828 uint32_t i; 12829 12830 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12831 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12832 free(io->io_hdr.remote_sglist, M_CTL); 12833 io->io_hdr.remote_sglist = NULL; 12834 io->io_hdr.local_sglist = NULL; 12835 12836#if 0 12837 scsi_path_string(io, path_str, sizeof(path_str)); 12838 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12839 sbuf_cat(&sb, path_str); 12840 scsi_command_string(&io->scsiio, NULL, &sb); 12841 sbuf_printf(&sb, "\n"); 12842 sbuf_cat(&sb, path_str); 12843 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12844 io->scsiio.tag_num, io->scsiio.tag_type); 12845 sbuf_cat(&sb, path_str); 12846 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12847 io->io_hdr.flags, io->io_hdr.status); 12848 sbuf_finish(&sb); 12849 printk("%s", sbuf_data(&sb)); 12850#endif 12851 12852 12853 /* 12854 * The read is done, now we need to send status (good or bad) back 12855 * to the other side. 12856 */ 12857 ctl_send_datamove_done(io, /*have_lock*/ 0); 12858 12859 return (0); 12860} 12861 12862static void 12863ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12864{ 12865 union ctl_io *io; 12866 void (*fe_datamove)(union ctl_io *io); 12867 12868 io = rq->context; 12869 12870 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12871 printf("%s: ISC DMA read failed with error %d\n", __func__, 12872 rq->ret); 12873 ctl_set_internal_failure(&io->scsiio, 12874 /*sks_valid*/ 1, 12875 /*retry_count*/ rq->ret); 12876 } 12877 12878 ctl_dt_req_free(rq); 12879 12880 /* Switch the pointer over so the FETD knows what to do */ 12881 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12882 12883 /* 12884 * Use a custom move done callback, since we need to send completion 12885 * back to the other controller, not to the backend on this side. 12886 */ 12887 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12888 12889 /* XXX KDM add checks like the ones in ctl_datamove? */ 12890 12891 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12892 fe_datamove(io); 12893} 12894 12895static int 12896ctl_datamove_remote_sgl_setup(union ctl_io *io) 12897{ 12898 struct ctl_sg_entry *local_sglist; 12899 uint32_t len_to_go; 12900 int retval; 12901 int i; 12902 12903 retval = 0; 12904 local_sglist = io->io_hdr.local_sglist; 12905 len_to_go = io->scsiio.kern_data_len; 12906 12907 /* 12908 * The difficult thing here is that the size of the various 12909 * S/G segments may be different than the size from the 12910 * remote controller. That'll make it harder when DMAing 12911 * the data back to the other side. 12912 */ 12913 for (i = 0; len_to_go > 0; i++) { 12914 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12915 local_sglist[i].addr = 12916 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12917 12918 len_to_go -= local_sglist[i].len; 12919 } 12920 /* 12921 * Reset the number of S/G entries accordingly. The original 12922 * number of S/G entries is available in rem_sg_entries. 12923 */ 12924 io->scsiio.kern_sg_entries = i; 12925 12926#if 0 12927 printf("%s: kern_sg_entries = %d\n", __func__, 12928 io->scsiio.kern_sg_entries); 12929 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12930 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12931 local_sglist[i].addr, local_sglist[i].len); 12932#endif 12933 12934 return (retval); 12935} 12936 12937static int 12938ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12939 ctl_ha_dt_cb callback) 12940{ 12941 struct ctl_ha_dt_req *rq; 12942 struct ctl_sg_entry *remote_sglist, *local_sglist; 12943 uint32_t local_used, remote_used, total_used; 12944 int i, j, isc_ret; 12945 12946 rq = ctl_dt_req_alloc(); 12947 12948 /* 12949 * If we failed to allocate the request, and if the DMA didn't fail 12950 * anyway, set busy status. This is just a resource allocation 12951 * failure. 12952 */ 12953 if ((rq == NULL) 12954 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12955 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12956 ctl_set_busy(&io->scsiio); 12957 12958 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12959 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12960 12961 if (rq != NULL) 12962 ctl_dt_req_free(rq); 12963 12964 /* 12965 * The data move failed. We need to return status back 12966 * to the other controller. No point in trying to DMA 12967 * data to the remote controller. 12968 */ 12969 12970 ctl_send_datamove_done(io, /*have_lock*/ 0); 12971 12972 return (1); 12973 } 12974 12975 local_sglist = io->io_hdr.local_sglist; 12976 remote_sglist = io->io_hdr.remote_sglist; 12977 local_used = 0; 12978 remote_used = 0; 12979 total_used = 0; 12980 12981 /* 12982 * Pull/push the data over the wire from/to the other controller. 12983 * This takes into account the possibility that the local and 12984 * remote sglists may not be identical in terms of the size of 12985 * the elements and the number of elements. 12986 * 12987 * One fundamental assumption here is that the length allocated for 12988 * both the local and remote sglists is identical. Otherwise, we've 12989 * essentially got a coding error of some sort. 12990 */ 12991 isc_ret = CTL_HA_STATUS_SUCCESS; 12992 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12993 uint32_t cur_len; 12994 uint8_t *tmp_ptr; 12995 12996 rq->command = command; 12997 rq->context = io; 12998 12999 /* 13000 * Both pointers should be aligned. But it is possible 13001 * that the allocation length is not. They should both 13002 * also have enough slack left over at the end, though, 13003 * to round up to the next 8 byte boundary. 13004 */ 13005 cur_len = MIN(local_sglist[i].len - local_used, 13006 remote_sglist[j].len - remote_used); 13007 rq->size = cur_len; 13008 13009 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13010 tmp_ptr += local_used; 13011 13012#if 0 13013 /* Use physical addresses when talking to ISC hardware */ 13014 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13015 /* XXX KDM use busdma */ 13016 rq->local = vtophys(tmp_ptr); 13017 } else 13018 rq->local = tmp_ptr; 13019#else 13020 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 13021 ("HA does not support BUS_ADDR")); 13022 rq->local = tmp_ptr; 13023#endif 13024 13025 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13026 tmp_ptr += remote_used; 13027 rq->remote = tmp_ptr; 13028 13029 rq->callback = NULL; 13030 13031 local_used += cur_len; 13032 if (local_used >= local_sglist[i].len) { 13033 i++; 13034 local_used = 0; 13035 } 13036 13037 remote_used += cur_len; 13038 if (remote_used >= remote_sglist[j].len) { 13039 j++; 13040 remote_used = 0; 13041 } 13042 total_used += cur_len; 13043 13044 if (total_used >= io->scsiio.kern_data_len) 13045 rq->callback = callback; 13046 13047#if 0 13048 printf("%s: %s: local %p remote %p size %d\n", __func__, 13049 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13050 rq->local, rq->remote, rq->size); 13051#endif 13052 13053 isc_ret = ctl_dt_single(rq); 13054 if (isc_ret > CTL_HA_STATUS_SUCCESS) 13055 break; 13056 } 13057 if (isc_ret != CTL_HA_STATUS_WAIT) { 13058 rq->ret = isc_ret; 13059 callback(rq); 13060 } 13061 13062 return (0); 13063} 13064 13065static void 13066ctl_datamove_remote_read(union ctl_io *io) 13067{ 13068 int retval; 13069 uint32_t i; 13070 13071 /* 13072 * This will send an error to the other controller in the case of a 13073 * failure. 13074 */ 13075 retval = ctl_datamove_remote_sgl_setup(io); 13076 if (retval != 0) 13077 return; 13078 13079 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13080 ctl_datamove_remote_read_cb); 13081 if (retval != 0) { 13082 /* 13083 * Make sure we free memory if there was an error.. The 13084 * ctl_datamove_remote_xfer() function will send the 13085 * datamove done message, or call the callback with an 13086 * error if there is a problem. 13087 */ 13088 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13089 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13090 free(io->io_hdr.remote_sglist, M_CTL); 13091 io->io_hdr.remote_sglist = NULL; 13092 io->io_hdr.local_sglist = NULL; 13093 } 13094} 13095 13096/* 13097 * Process a datamove request from the other controller. This is used for 13098 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13099 * first. Once that is complete, the data gets DMAed into the remote 13100 * controller's memory. For reads, we DMA from the remote controller's 13101 * memory into our memory first, and then move it out to the FETD. 13102 */ 13103static void 13104ctl_datamove_remote(union ctl_io *io) 13105{ 13106 13107 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13108 13109 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13110 ctl_failover_io(io, /*have_lock*/ 0); 13111 return; 13112 } 13113 13114 /* 13115 * Note that we look for an aborted I/O here, but don't do some of 13116 * the other checks that ctl_datamove() normally does. 13117 * We don't need to run the datamove delay code, since that should 13118 * have been done if need be on the other controller. 13119 */ 13120 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13121 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13122 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13123 io->io_hdr.nexus.targ_port, 13124 io->io_hdr.nexus.targ_lun); 13125 io->io_hdr.port_status = 31338; 13126 ctl_send_datamove_done(io, /*have_lock*/ 0); 13127 return; 13128 } 13129 13130 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13131 ctl_datamove_remote_write(io); 13132 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13133 ctl_datamove_remote_read(io); 13134 else { 13135 io->io_hdr.port_status = 31339; 13136 ctl_send_datamove_done(io, /*have_lock*/ 0); 13137 } 13138} 13139 13140static void 13141ctl_process_done(union ctl_io *io) 13142{ 13143 struct ctl_lun *lun; 13144 struct ctl_softc *softc = control_softc; 13145 void (*fe_done)(union ctl_io *io); 13146 union ctl_ha_msg msg; 13147 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13148 13149 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13150 fe_done = softc->ctl_ports[targ_port]->fe_done; 13151 13152#ifdef CTL_TIME_IO 13153 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13154 char str[256]; 13155 char path_str[64]; 13156 struct sbuf sb; 13157 13158 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13159 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13160 13161 sbuf_cat(&sb, path_str); 13162 switch (io->io_hdr.io_type) { 13163 case CTL_IO_SCSI: 13164 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13165 sbuf_printf(&sb, "\n"); 13166 sbuf_cat(&sb, path_str); 13167 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13168 io->scsiio.tag_num, io->scsiio.tag_type); 13169 break; 13170 case CTL_IO_TASK: 13171 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13172 "Tag Type: %d\n", io->taskio.task_action, 13173 io->taskio.tag_num, io->taskio.tag_type); 13174 break; 13175 default: 13176 panic("%s: Invalid CTL I/O type %d\n", 13177 __func__, io->io_hdr.io_type); 13178 } 13179 sbuf_cat(&sb, path_str); 13180 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13181 (intmax_t)time_uptime - io->io_hdr.start_time); 13182 sbuf_finish(&sb); 13183 printf("%s", sbuf_data(&sb)); 13184 } 13185#endif /* CTL_TIME_IO */ 13186 13187 switch (io->io_hdr.io_type) { 13188 case CTL_IO_SCSI: 13189 break; 13190 case CTL_IO_TASK: 13191 if (ctl_debug & CTL_DEBUG_INFO) 13192 ctl_io_error_print(io, NULL); 13193 fe_done(io); 13194 return; 13195 default: 13196 panic("%s: Invalid CTL I/O type %d\n", 13197 __func__, io->io_hdr.io_type); 13198 } 13199 13200 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13201 if (lun == NULL) { 13202 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13203 io->io_hdr.nexus.targ_mapped_lun)); 13204 goto bailout; 13205 } 13206 13207 mtx_lock(&lun->lun_lock); 13208 13209 /* 13210 * Check to see if we have any informational exception and status 13211 * of this command can be modified to report it in form of either 13212 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13213 */ 13214 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13215 io->io_hdr.status == CTL_SUCCESS && 13216 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13217 uint8_t mrie = lun->MODE_IE.mrie; 13218 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13219 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13220 if (((mrie == SIEP_MRIE_REC_COND && per) || 13221 mrie == SIEP_MRIE_REC_UNCOND || 13222 mrie == SIEP_MRIE_NO_SENSE) && 13223 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13224 CTL_CMD_FLAG_NO_SENSE) == 0) { 13225 ctl_set_sense(&io->scsiio, 13226 /*current_error*/ 1, 13227 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13228 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13229 /*asc*/ lun->ie_asc, 13230 /*ascq*/ lun->ie_ascq, 13231 SSD_ELEM_NONE); 13232 lun->ie_reported = 1; 13233 } 13234 } else if (lun->ie_reported < 0) 13235 lun->ie_reported = 0; 13236 13237 /* 13238 * Check to see if we have any errors to inject here. We only 13239 * inject errors for commands that don't already have errors set. 13240 */ 13241 if (!STAILQ_EMPTY(&lun->error_list) && 13242 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13243 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13244 ctl_inject_error(lun, io); 13245 13246 /* 13247 * XXX KDM how do we treat commands that aren't completed 13248 * successfully? 13249 * 13250 * XXX KDM should we also track I/O latency? 13251 */ 13252 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13253 io->io_hdr.io_type == CTL_IO_SCSI) { 13254#ifdef CTL_TIME_IO 13255 struct bintime cur_bt; 13256#endif 13257 int type; 13258 13259 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13260 CTL_FLAG_DATA_IN) 13261 type = CTL_STATS_READ; 13262 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13263 CTL_FLAG_DATA_OUT) 13264 type = CTL_STATS_WRITE; 13265 else 13266 type = CTL_STATS_NO_IO; 13267 13268 lun->stats.ports[targ_port].bytes[type] += 13269 io->scsiio.kern_total_len; 13270 lun->stats.ports[targ_port].operations[type]++; 13271#ifdef CTL_TIME_IO 13272 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13273 &io->io_hdr.dma_bt); 13274 getbinuptime(&cur_bt); 13275 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13276 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13277#endif 13278 lun->stats.ports[targ_port].num_dmas[type] += 13279 io->io_hdr.num_dmas; 13280 } 13281 13282 /* 13283 * Remove this from the OOA queue. 13284 */ 13285 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13286#ifdef CTL_TIME_IO 13287 if (TAILQ_EMPTY(&lun->ooa_queue)) 13288 lun->last_busy = getsbinuptime(); 13289#endif 13290 13291 /* 13292 * Run through the blocked queue on this LUN and see if anything 13293 * has become unblocked, now that this transaction is done. 13294 */ 13295 ctl_check_blocked(lun); 13296 13297 /* 13298 * If the LUN has been invalidated, free it if there is nothing 13299 * left on its OOA queue. 13300 */ 13301 if ((lun->flags & CTL_LUN_INVALID) 13302 && TAILQ_EMPTY(&lun->ooa_queue)) { 13303 mtx_unlock(&lun->lun_lock); 13304 mtx_lock(&softc->ctl_lock); 13305 ctl_free_lun(lun); 13306 mtx_unlock(&softc->ctl_lock); 13307 } else 13308 mtx_unlock(&lun->lun_lock); 13309 13310bailout: 13311 13312 /* 13313 * If this command has been aborted, make sure we set the status 13314 * properly. The FETD is responsible for freeing the I/O and doing 13315 * whatever it needs to do to clean up its state. 13316 */ 13317 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13318 ctl_set_task_aborted(&io->scsiio); 13319 13320 /* 13321 * If enabled, print command error status. 13322 */ 13323 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13324 (ctl_debug & CTL_DEBUG_INFO) != 0) 13325 ctl_io_error_print(io, NULL); 13326 13327 /* 13328 * Tell the FETD or the other shelf controller we're done with this 13329 * command. Note that only SCSI commands get to this point. Task 13330 * management commands are completed above. 13331 */ 13332 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13333 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13334 memset(&msg, 0, sizeof(msg)); 13335 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13336 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13337 msg.hdr.nexus = io->io_hdr.nexus; 13338 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13339 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13340 M_WAITOK); 13341 } 13342 13343 fe_done(io); 13344} 13345 13346#ifdef CTL_WITH_CA 13347/* 13348 * Front end should call this if it doesn't do autosense. When the request 13349 * sense comes back in from the initiator, we'll dequeue this and send it. 13350 */ 13351int 13352ctl_queue_sense(union ctl_io *io) 13353{ 13354 struct ctl_lun *lun; 13355 struct ctl_port *port; 13356 struct ctl_softc *softc; 13357 uint32_t initidx, targ_lun; 13358 13359 softc = control_softc; 13360 13361 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13362 13363 /* 13364 * LUN lookup will likely move to the ctl_work_thread() once we 13365 * have our new queueing infrastructure (that doesn't put things on 13366 * a per-LUN queue initially). That is so that we can handle 13367 * things like an INQUIRY to a LUN that we don't have enabled. We 13368 * can't deal with that right now. 13369 */ 13370 mtx_lock(&softc->ctl_lock); 13371 13372 /* 13373 * If we don't have a LUN for this, just toss the sense 13374 * information. 13375 */ 13376 port = ctl_io_port(&ctsio->io_hdr); 13377 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13378 if ((targ_lun < CTL_MAX_LUNS) 13379 && (softc->ctl_luns[targ_lun] != NULL)) 13380 lun = softc->ctl_luns[targ_lun]; 13381 else 13382 goto bailout; 13383 13384 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13385 13386 mtx_lock(&lun->lun_lock); 13387 /* 13388 * Already have CA set for this LUN...toss the sense information. 13389 */ 13390 if (ctl_is_set(lun->have_ca, initidx)) { 13391 mtx_unlock(&lun->lun_lock); 13392 goto bailout; 13393 } 13394 13395 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13396 MIN(sizeof(lun->pending_sense[initidx]), 13397 sizeof(io->scsiio.sense_data))); 13398 ctl_set_mask(lun->have_ca, initidx); 13399 mtx_unlock(&lun->lun_lock); 13400 13401bailout: 13402 mtx_unlock(&softc->ctl_lock); 13403 13404 ctl_free_io(io); 13405 13406 return (CTL_RETVAL_COMPLETE); 13407} 13408#endif 13409 13410/* 13411 * Primary command inlet from frontend ports. All SCSI and task I/O 13412 * requests must go through this function. 13413 */ 13414int 13415ctl_queue(union ctl_io *io) 13416{ 13417 struct ctl_port *port; 13418 13419 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13420 13421#ifdef CTL_TIME_IO 13422 io->io_hdr.start_time = time_uptime; 13423 getbinuptime(&io->io_hdr.start_bt); 13424#endif /* CTL_TIME_IO */ 13425 13426 /* Map FE-specific LUN ID into global one. */ 13427 port = ctl_io_port(&io->io_hdr); 13428 io->io_hdr.nexus.targ_mapped_lun = 13429 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13430 13431 switch (io->io_hdr.io_type) { 13432 case CTL_IO_SCSI: 13433 case CTL_IO_TASK: 13434 if (ctl_debug & CTL_DEBUG_CDB) 13435 ctl_io_print(io); 13436 ctl_enqueue_incoming(io); 13437 break; 13438 default: 13439 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13440 return (EINVAL); 13441 } 13442 13443 return (CTL_RETVAL_COMPLETE); 13444} 13445 13446#ifdef CTL_IO_DELAY 13447static void 13448ctl_done_timer_wakeup(void *arg) 13449{ 13450 union ctl_io *io; 13451 13452 io = (union ctl_io *)arg; 13453 ctl_done(io); 13454} 13455#endif /* CTL_IO_DELAY */ 13456 13457void 13458ctl_serseq_done(union ctl_io *io) 13459{ 13460 struct ctl_lun *lun; 13461 13462 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13463 if (lun->be_lun == NULL || 13464 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13465 return; 13466 mtx_lock(&lun->lun_lock); 13467 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13468 ctl_check_blocked(lun); 13469 mtx_unlock(&lun->lun_lock); 13470} 13471 13472void 13473ctl_done(union ctl_io *io) 13474{ 13475 13476 /* 13477 * Enable this to catch duplicate completion issues. 13478 */ 13479#if 0 13480 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13481 printf("%s: type %d msg %d cdb %x iptl: " 13482 "%u:%u:%u tag 0x%04x " 13483 "flag %#x status %x\n", 13484 __func__, 13485 io->io_hdr.io_type, 13486 io->io_hdr.msg_type, 13487 io->scsiio.cdb[0], 13488 io->io_hdr.nexus.initid, 13489 io->io_hdr.nexus.targ_port, 13490 io->io_hdr.nexus.targ_lun, 13491 (io->io_hdr.io_type == 13492 CTL_IO_TASK) ? 13493 io->taskio.tag_num : 13494 io->scsiio.tag_num, 13495 io->io_hdr.flags, 13496 io->io_hdr.status); 13497 } else 13498 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13499#endif 13500 13501 /* 13502 * This is an internal copy of an I/O, and should not go through 13503 * the normal done processing logic. 13504 */ 13505 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13506 return; 13507 13508#ifdef CTL_IO_DELAY 13509 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13510 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13511 } else { 13512 struct ctl_lun *lun; 13513 13514 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13515 13516 if ((lun != NULL) 13517 && (lun->delay_info.done_delay > 0)) { 13518 13519 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13520 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13521 callout_reset(&io->io_hdr.delay_callout, 13522 lun->delay_info.done_delay * hz, 13523 ctl_done_timer_wakeup, io); 13524 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13525 lun->delay_info.done_delay = 0; 13526 return; 13527 } 13528 } 13529#endif /* CTL_IO_DELAY */ 13530 13531 ctl_enqueue_done(io); 13532} 13533 13534static void 13535ctl_work_thread(void *arg) 13536{ 13537 struct ctl_thread *thr = (struct ctl_thread *)arg; 13538 struct ctl_softc *softc = thr->ctl_softc; 13539 union ctl_io *io; 13540 int retval; 13541 13542 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13543 13544 for (;;) { 13545 /* 13546 * We handle the queues in this order: 13547 * - ISC 13548 * - done queue (to free up resources, unblock other commands) 13549 * - RtR queue 13550 * - incoming queue 13551 * 13552 * If those queues are empty, we break out of the loop and 13553 * go to sleep. 13554 */ 13555 mtx_lock(&thr->queue_lock); 13556 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13557 if (io != NULL) { 13558 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13559 mtx_unlock(&thr->queue_lock); 13560 ctl_handle_isc(io); 13561 continue; 13562 } 13563 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13564 if (io != NULL) { 13565 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13566 /* clear any blocked commands, call fe_done */ 13567 mtx_unlock(&thr->queue_lock); 13568 ctl_process_done(io); 13569 continue; 13570 } 13571 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13572 if (io != NULL) { 13573 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13574 mtx_unlock(&thr->queue_lock); 13575 if (io->io_hdr.io_type == CTL_IO_TASK) 13576 ctl_run_task(io); 13577 else 13578 ctl_scsiio_precheck(softc, &io->scsiio); 13579 continue; 13580 } 13581 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13582 if (io != NULL) { 13583 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13584 mtx_unlock(&thr->queue_lock); 13585 retval = ctl_scsiio(&io->scsiio); 13586 if (retval != CTL_RETVAL_COMPLETE) 13587 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13588 continue; 13589 } 13590 13591 /* Sleep until we have something to do. */ 13592 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13593 } 13594} 13595 13596static void 13597ctl_lun_thread(void *arg) 13598{ 13599 struct ctl_softc *softc = (struct ctl_softc *)arg; 13600 struct ctl_be_lun *be_lun; 13601 13602 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13603 13604 for (;;) { 13605 mtx_lock(&softc->ctl_lock); 13606 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13607 if (be_lun != NULL) { 13608 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13609 mtx_unlock(&softc->ctl_lock); 13610 ctl_create_lun(be_lun); 13611 continue; 13612 } 13613 13614 /* Sleep until we have something to do. */ 13615 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13616 PDROP | PRIBIO, "-", 0); 13617 } 13618} 13619 13620static void 13621ctl_thresh_thread(void *arg) 13622{ 13623 struct ctl_softc *softc = (struct ctl_softc *)arg; 13624 struct ctl_lun *lun; 13625 struct ctl_logical_block_provisioning_page *page; 13626 const char *attr; 13627 union ctl_ha_msg msg; 13628 uint64_t thres, val; 13629 int i, e, set; 13630 13631 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13632 13633 for (;;) { 13634 mtx_lock(&softc->ctl_lock); 13635 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13636 if ((lun->flags & CTL_LUN_DISABLED) || 13637 (lun->flags & CTL_LUN_NO_MEDIA) || 13638 lun->backend->lun_attr == NULL) 13639 continue; 13640 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13641 softc->ha_mode == CTL_HA_MODE_XFER) 13642 continue; 13643 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13644 continue; 13645 e = 0; 13646 page = &lun->MODE_LBP; 13647 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13648 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13649 continue; 13650 thres = scsi_4btoul(page->descr[i].count); 13651 thres <<= CTL_LBP_EXPONENT; 13652 switch (page->descr[i].resource) { 13653 case 0x01: 13654 attr = "blocksavail"; 13655 break; 13656 case 0x02: 13657 attr = "blocksused"; 13658 break; 13659 case 0xf1: 13660 attr = "poolblocksavail"; 13661 break; 13662 case 0xf2: 13663 attr = "poolblocksused"; 13664 break; 13665 default: 13666 continue; 13667 } 13668 mtx_unlock(&softc->ctl_lock); // XXX 13669 val = lun->backend->lun_attr( 13670 lun->be_lun->be_lun, attr); 13671 mtx_lock(&softc->ctl_lock); 13672 if (val == UINT64_MAX) 13673 continue; 13674 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13675 == SLBPPD_ARMING_INC) 13676 e = (val >= thres); 13677 else 13678 e = (val <= thres); 13679 if (e) 13680 break; 13681 } 13682 mtx_lock(&lun->lun_lock); 13683 if (e) { 13684 scsi_u64to8b((uint8_t *)&page->descr[i] - 13685 (uint8_t *)page, lun->ua_tpt_info); 13686 if (lun->lasttpt == 0 || 13687 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13688 lun->lasttpt = time_uptime; 13689 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13690 set = 1; 13691 } else 13692 set = 0; 13693 } else { 13694 lun->lasttpt = 0; 13695 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13696 set = -1; 13697 } 13698 mtx_unlock(&lun->lun_lock); 13699 if (set != 0 && 13700 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13701 /* Send msg to other side. */ 13702 bzero(&msg.ua, sizeof(msg.ua)); 13703 msg.hdr.msg_type = CTL_MSG_UA; 13704 msg.hdr.nexus.initid = -1; 13705 msg.hdr.nexus.targ_port = -1; 13706 msg.hdr.nexus.targ_lun = lun->lun; 13707 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13708 msg.ua.ua_all = 1; 13709 msg.ua.ua_set = (set > 0); 13710 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13711 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13712 mtx_unlock(&softc->ctl_lock); // XXX 13713 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13714 sizeof(msg.ua), M_WAITOK); 13715 mtx_lock(&softc->ctl_lock); 13716 } 13717 } 13718 mtx_unlock(&softc->ctl_lock); 13719 pause("-", CTL_LBP_PERIOD * hz); 13720 } 13721} 13722 13723static void 13724ctl_enqueue_incoming(union ctl_io *io) 13725{ 13726 struct ctl_softc *softc = control_softc; 13727 struct ctl_thread *thr; 13728 u_int idx; 13729 13730 idx = (io->io_hdr.nexus.targ_port * 127 + 13731 io->io_hdr.nexus.initid) % worker_threads; 13732 thr = &softc->threads[idx]; 13733 mtx_lock(&thr->queue_lock); 13734 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13735 mtx_unlock(&thr->queue_lock); 13736 wakeup(thr); 13737} 13738 13739static void 13740ctl_enqueue_rtr(union ctl_io *io) 13741{ 13742 struct ctl_softc *softc = control_softc; 13743 struct ctl_thread *thr; 13744 13745 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13746 mtx_lock(&thr->queue_lock); 13747 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13748 mtx_unlock(&thr->queue_lock); 13749 wakeup(thr); 13750} 13751 13752static void 13753ctl_enqueue_done(union ctl_io *io) 13754{ 13755 struct ctl_softc *softc = control_softc; 13756 struct ctl_thread *thr; 13757 13758 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13759 mtx_lock(&thr->queue_lock); 13760 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13761 mtx_unlock(&thr->queue_lock); 13762 wakeup(thr); 13763} 13764 13765static void 13766ctl_enqueue_isc(union ctl_io *io) 13767{ 13768 struct ctl_softc *softc = control_softc; 13769 struct ctl_thread *thr; 13770 13771 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13772 mtx_lock(&thr->queue_lock); 13773 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13774 mtx_unlock(&thr->queue_lock); 13775 wakeup(thr); 13776} 13777 13778/* 13779 * vim: ts=8 13780 */ 13781