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