ctl.c revision 312576
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 312576 2017-01-21 08:33:44Z mav $"); 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/ctype.h> 51#include <sys/kernel.h> 52#include <sys/types.h> 53#include <sys/kthread.h> 54#include <sys/bio.h> 55#include <sys/fcntl.h> 56#include <sys/lock.h> 57#include <sys/module.h> 58#include <sys/mutex.h> 59#include <sys/condvar.h> 60#include <sys/malloc.h> 61#include <sys/conf.h> 62#include <sys/ioccom.h> 63#include <sys/queue.h> 64#include <sys/sbuf.h> 65#include <sys/smp.h> 66#include <sys/endian.h> 67#include <sys/sysctl.h> 68#include <vm/uma.h> 69 70#include <cam/cam.h> 71#include <cam/scsi/scsi_all.h> 72#include <cam/scsi/scsi_cd.h> 73#include <cam/scsi/scsi_da.h> 74#include <cam/ctl/ctl_io.h> 75#include <cam/ctl/ctl.h> 76#include <cam/ctl/ctl_frontend.h> 77#include <cam/ctl/ctl_util.h> 78#include <cam/ctl/ctl_backend.h> 79#include <cam/ctl/ctl_ioctl.h> 80#include <cam/ctl/ctl_ha.h> 81#include <cam/ctl/ctl_private.h> 82#include <cam/ctl/ctl_debug.h> 83#include <cam/ctl/ctl_scsi_all.h> 84#include <cam/ctl/ctl_error.h> 85 86struct ctl_softc *control_softc = NULL; 87 88/* 89 * Template mode pages. 90 */ 91 92/* 93 * Note that these are default values only. The actual values will be 94 * filled in when the user does a mode sense. 95 */ 96const static struct scsi_da_rw_recovery_page rw_er_page_default = { 97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 100 /*read_retry_count*/0, 101 /*correction_span*/0, 102 /*head_offset_count*/0, 103 /*data_strobe_offset_cnt*/0, 104 /*byte8*/SMS_RWER_LBPERE, 105 /*write_retry_count*/0, 106 /*reserved2*/0, 107 /*recovery_time_limit*/{0, 0}, 108}; 109 110const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 113 /*byte3*/SMS_RWER_PER, 114 /*read_retry_count*/0, 115 /*correction_span*/0, 116 /*head_offset_count*/0, 117 /*data_strobe_offset_cnt*/0, 118 /*byte8*/SMS_RWER_LBPERE, 119 /*write_retry_count*/0, 120 /*reserved2*/0, 121 /*recovery_time_limit*/{0, 0}, 122}; 123 124const static struct scsi_format_page format_page_default = { 125 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 126 /*page_length*/sizeof(struct scsi_format_page) - 2, 127 /*tracks_per_zone*/ {0, 0}, 128 /*alt_sectors_per_zone*/ {0, 0}, 129 /*alt_tracks_per_zone*/ {0, 0}, 130 /*alt_tracks_per_lun*/ {0, 0}, 131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 133 /*bytes_per_sector*/ {0, 0}, 134 /*interleave*/ {0, 0}, 135 /*track_skew*/ {0, 0}, 136 /*cylinder_skew*/ {0, 0}, 137 /*flags*/ SFP_HSEC, 138 /*reserved*/ {0, 0, 0} 139}; 140 141const static struct scsi_format_page format_page_changeable = { 142 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 143 /*page_length*/sizeof(struct scsi_format_page) - 2, 144 /*tracks_per_zone*/ {0, 0}, 145 /*alt_sectors_per_zone*/ {0, 0}, 146 /*alt_tracks_per_zone*/ {0, 0}, 147 /*alt_tracks_per_lun*/ {0, 0}, 148 /*sectors_per_track*/ {0, 0}, 149 /*bytes_per_sector*/ {0, 0}, 150 /*interleave*/ {0, 0}, 151 /*track_skew*/ {0, 0}, 152 /*cylinder_skew*/ {0, 0}, 153 /*flags*/ 0, 154 /*reserved*/ {0, 0, 0} 155}; 156 157const static struct scsi_rigid_disk_page rigid_disk_page_default = { 158 /*page_code*/SMS_RIGID_DISK_PAGE, 159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 160 /*cylinders*/ {0, 0, 0}, 161 /*heads*/ CTL_DEFAULT_HEADS, 162 /*start_write_precomp*/ {0, 0, 0}, 163 /*start_reduced_current*/ {0, 0, 0}, 164 /*step_rate*/ {0, 0}, 165 /*landing_zone_cylinder*/ {0, 0, 0}, 166 /*rpl*/ SRDP_RPL_DISABLED, 167 /*rotational_offset*/ 0, 168 /*reserved1*/ 0, 169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 170 CTL_DEFAULT_ROTATION_RATE & 0xff}, 171 /*reserved2*/ {0, 0} 172}; 173 174const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 175 /*page_code*/SMS_RIGID_DISK_PAGE, 176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 177 /*cylinders*/ {0, 0, 0}, 178 /*heads*/ 0, 179 /*start_write_precomp*/ {0, 0, 0}, 180 /*start_reduced_current*/ {0, 0, 0}, 181 /*step_rate*/ {0, 0}, 182 /*landing_zone_cylinder*/ {0, 0, 0}, 183 /*rpl*/ 0, 184 /*rotational_offset*/ 0, 185 /*reserved1*/ 0, 186 /*rotation_rate*/ {0, 0}, 187 /*reserved2*/ {0, 0} 188}; 189 190const static struct scsi_da_verify_recovery_page verify_er_page_default = { 191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 193 /*byte3*/0, 194 /*read_retry_count*/0, 195 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 196 /*recovery_time_limit*/{0, 0}, 197}; 198 199const static struct scsi_da_verify_recovery_page verify_er_page_changeable = { 200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE, 201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2, 202 /*byte3*/SMS_VER_PER, 203 /*read_retry_count*/0, 204 /*reserved*/{ 0, 0, 0, 0, 0, 0 }, 205 /*recovery_time_limit*/{0, 0}, 206}; 207 208const static struct scsi_caching_page caching_page_default = { 209 /*page_code*/SMS_CACHING_PAGE, 210 /*page_length*/sizeof(struct scsi_caching_page) - 2, 211 /*flags1*/ SCP_DISC | SCP_WCE, 212 /*ret_priority*/ 0, 213 /*disable_pf_transfer_len*/ {0xff, 0xff}, 214 /*min_prefetch*/ {0, 0}, 215 /*max_prefetch*/ {0xff, 0xff}, 216 /*max_pf_ceiling*/ {0xff, 0xff}, 217 /*flags2*/ 0, 218 /*cache_segments*/ 0, 219 /*cache_seg_size*/ {0, 0}, 220 /*reserved*/ 0, 221 /*non_cache_seg_size*/ {0, 0, 0} 222}; 223 224const static struct scsi_caching_page caching_page_changeable = { 225 /*page_code*/SMS_CACHING_PAGE, 226 /*page_length*/sizeof(struct scsi_caching_page) - 2, 227 /*flags1*/ SCP_WCE | SCP_RCD, 228 /*ret_priority*/ 0, 229 /*disable_pf_transfer_len*/ {0, 0}, 230 /*min_prefetch*/ {0, 0}, 231 /*max_prefetch*/ {0, 0}, 232 /*max_pf_ceiling*/ {0, 0}, 233 /*flags2*/ 0, 234 /*cache_segments*/ 0, 235 /*cache_seg_size*/ {0, 0}, 236 /*reserved*/ 0, 237 /*non_cache_seg_size*/ {0, 0, 0} 238}; 239 240const static struct scsi_control_page control_page_default = { 241 /*page_code*/SMS_CONTROL_MODE_PAGE, 242 /*page_length*/sizeof(struct scsi_control_page) - 2, 243 /*rlec*/0, 244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 245 /*eca_and_aen*/0, 246 /*flags4*/SCP_TAS, 247 /*aen_holdoff_period*/{0, 0}, 248 /*busy_timeout_period*/{0, 0}, 249 /*extended_selftest_completion_time*/{0, 0} 250}; 251 252const static struct scsi_control_page control_page_changeable = { 253 /*page_code*/SMS_CONTROL_MODE_PAGE, 254 /*page_length*/sizeof(struct scsi_control_page) - 2, 255 /*rlec*/SCP_DSENSE, 256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR, 257 /*eca_and_aen*/SCP_SWP, 258 /*flags4*/0, 259 /*aen_holdoff_period*/{0, 0}, 260 /*busy_timeout_period*/{0, 0}, 261 /*extended_selftest_completion_time*/{0, 0} 262}; 263 264#define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 265 266const static struct scsi_control_ext_page control_ext_page_default = { 267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 268 /*subpage_code*/0x01, 269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 270 /*flags*/0, 271 /*prio*/0, 272 /*max_sense*/0 273}; 274 275const static struct scsi_control_ext_page control_ext_page_changeable = { 276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 277 /*subpage_code*/0x01, 278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 279 /*flags*/0, 280 /*prio*/0, 281 /*max_sense*/0xff 282}; 283 284const static struct scsi_info_exceptions_page ie_page_default = { 285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 287 /*info_flags*/SIEP_FLAGS_EWASC, 288 /*mrie*/SIEP_MRIE_NO, 289 /*interval_timer*/{0, 0, 0, 0}, 290 /*report_count*/{0, 0, 0, 1} 291}; 292 293const static struct scsi_info_exceptions_page ie_page_changeable = { 294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST | 297 SIEP_FLAGS_LOGERR, 298 /*mrie*/0x0f, 299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff}, 300 /*report_count*/{0xff, 0xff, 0xff, 0xff} 301}; 302 303#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 304 305const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 307 /*subpage_code*/0x02, 308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 309 /*flags*/0, 310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 311 /*descr*/{}}, 312 {{/*flags*/0, 313 /*resource*/0x01, 314 /*reserved*/{0, 0}, 315 /*count*/{0, 0, 0, 0}}, 316 {/*flags*/0, 317 /*resource*/0x02, 318 /*reserved*/{0, 0}, 319 /*count*/{0, 0, 0, 0}}, 320 {/*flags*/0, 321 /*resource*/0xf1, 322 /*reserved*/{0, 0}, 323 /*count*/{0, 0, 0, 0}}, 324 {/*flags*/0, 325 /*resource*/0xf2, 326 /*reserved*/{0, 0}, 327 /*count*/{0, 0, 0, 0}} 328 } 329}; 330 331const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 333 /*subpage_code*/0x02, 334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 335 /*flags*/SLBPP_SITUA, 336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 337 /*descr*/{}}, 338 {{/*flags*/0, 339 /*resource*/0, 340 /*reserved*/{0, 0}, 341 /*count*/{0, 0, 0, 0}}, 342 {/*flags*/0, 343 /*resource*/0, 344 /*reserved*/{0, 0}, 345 /*count*/{0, 0, 0, 0}}, 346 {/*flags*/0, 347 /*resource*/0, 348 /*reserved*/{0, 0}, 349 /*count*/{0, 0, 0, 0}}, 350 {/*flags*/0, 351 /*resource*/0, 352 /*reserved*/{0, 0}, 353 /*count*/{0, 0, 0, 0}} 354 } 355}; 356 357const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 358 /*page_code*/SMS_CDDVD_CAPS_PAGE, 359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 360 /*caps1*/0x3f, 361 /*caps2*/0x00, 362 /*caps3*/0xf0, 363 /*caps4*/0x00, 364 /*caps5*/0x29, 365 /*caps6*/0x00, 366 /*obsolete*/{0, 0}, 367 /*nvol_levels*/{0, 0}, 368 /*buffer_size*/{8, 0}, 369 /*obsolete2*/{0, 0}, 370 /*reserved*/0, 371 /*digital*/0, 372 /*obsolete3*/0, 373 /*copy_management*/0, 374 /*reserved2*/0, 375 /*rotation_control*/0, 376 /*cur_write_speed*/0, 377 /*num_speed_descr*/0, 378}; 379 380const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 381 /*page_code*/SMS_CDDVD_CAPS_PAGE, 382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 383 /*caps1*/0, 384 /*caps2*/0, 385 /*caps3*/0, 386 /*caps4*/0, 387 /*caps5*/0, 388 /*caps6*/0, 389 /*obsolete*/{0, 0}, 390 /*nvol_levels*/{0, 0}, 391 /*buffer_size*/{0, 0}, 392 /*obsolete2*/{0, 0}, 393 /*reserved*/0, 394 /*digital*/0, 395 /*obsolete3*/0, 396 /*copy_management*/0, 397 /*reserved2*/0, 398 /*rotation_control*/0, 399 /*cur_write_speed*/0, 400 /*num_speed_descr*/0, 401}; 402 403SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 404static int worker_threads = -1; 405SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 406 &worker_threads, 1, "Number of worker threads"); 407static int ctl_debug = CTL_DEBUG_NONE; 408SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 409 &ctl_debug, 0, "Enabled debug flags"); 410static int ctl_lun_map_size = 1024; 411SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN, 412 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)"); 413 414/* 415 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 416 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 417 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 418 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 419 */ 420#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 421 422static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 423 int param); 424static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 425static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 426static int ctl_init(void); 427void ctl_shutdown(void); 428static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 429static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 430static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 431static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 432 struct ctl_ooa *ooa_hdr, 433 struct ctl_ooa_entry *kern_entries); 434static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 435 struct thread *td); 436static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 437 struct ctl_be_lun *be_lun); 438static int ctl_free_lun(struct ctl_lun *lun); 439static void ctl_create_lun(struct ctl_be_lun *be_lun); 440static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 441 442static int ctl_do_mode_select(union ctl_io *io); 443static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 444 uint64_t res_key, uint64_t sa_res_key, 445 uint8_t type, uint32_t residx, 446 struct ctl_scsiio *ctsio, 447 struct scsi_per_res_out *cdb, 448 struct scsi_per_res_out_parms* param); 449static void ctl_pro_preempt_other(struct ctl_lun *lun, 450 union ctl_ha_msg *msg); 451static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 452static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 453static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 454static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 455static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 456static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 457static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 458 int alloc_len); 459static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 460 int alloc_len); 461static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 462static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 463static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 464static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 465static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 466static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 467 bool seq); 468static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 469static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 470 union ctl_io *pending_io, union ctl_io *ooa_io); 471static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 472 union ctl_io *starting_io); 473static int ctl_check_blocked(struct ctl_lun *lun); 474static int ctl_scsiio_lun_check(struct ctl_lun *lun, 475 const struct ctl_cmd_entry *entry, 476 struct ctl_scsiio *ctsio); 477static void ctl_failover_lun(union ctl_io *io); 478static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 479 struct ctl_scsiio *ctsio); 480static int ctl_scsiio(struct ctl_scsiio *ctsio); 481 482static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 483static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 484 ctl_ua_type ua_type); 485static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 486 ctl_ua_type ua_type); 487static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 488static int ctl_abort_task(union ctl_io *io); 489static int ctl_abort_task_set(union ctl_io *io); 490static int ctl_query_task(union ctl_io *io, int task_set); 491static int ctl_i_t_nexus_reset(union ctl_io *io); 492static int ctl_query_async_event(union ctl_io *io); 493static void ctl_run_task(union ctl_io *io); 494#ifdef CTL_IO_DELAY 495static void ctl_datamove_timer_wakeup(void *arg); 496static void ctl_done_timer_wakeup(void *arg); 497#endif /* CTL_IO_DELAY */ 498 499static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 500static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 501static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 502static void ctl_datamove_remote_write(union ctl_io *io); 503static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 504static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 505static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 506static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 507 ctl_ha_dt_cb callback); 508static void ctl_datamove_remote_read(union ctl_io *io); 509static void ctl_datamove_remote(union ctl_io *io); 510static void ctl_process_done(union ctl_io *io); 511static void ctl_lun_thread(void *arg); 512static void ctl_thresh_thread(void *arg); 513static void ctl_work_thread(void *arg); 514static void ctl_enqueue_incoming(union ctl_io *io); 515static void ctl_enqueue_rtr(union ctl_io *io); 516static void ctl_enqueue_done(union ctl_io *io); 517static void ctl_enqueue_isc(union ctl_io *io); 518static const struct ctl_cmd_entry * 519 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 520static const struct ctl_cmd_entry * 521 ctl_validate_command(struct ctl_scsiio *ctsio); 522static int ctl_cmd_applicable(uint8_t lun_type, 523 const struct ctl_cmd_entry *entry); 524 525static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 526static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 527static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 528static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 529 530/* 531 * Load the serialization table. This isn't very pretty, but is probably 532 * the easiest way to do it. 533 */ 534#include "ctl_ser_table.c" 535 536/* 537 * We only need to define open, close and ioctl routines for this driver. 538 */ 539static struct cdevsw ctl_cdevsw = { 540 .d_version = D_VERSION, 541 .d_flags = 0, 542 .d_open = ctl_open, 543 .d_close = ctl_close, 544 .d_ioctl = ctl_ioctl, 545 .d_name = "ctl", 546}; 547 548 549MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 550 551static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 552 553static moduledata_t ctl_moduledata = { 554 "ctl", 555 ctl_module_event_handler, 556 NULL 557}; 558 559DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 560MODULE_VERSION(ctl, 1); 561 562static struct ctl_frontend ha_frontend = 563{ 564 .name = "ha", 565}; 566 567static void 568ctl_ha_datamove(union ctl_io *io) 569{ 570 struct ctl_lun *lun; 571 struct ctl_sg_entry *sgl; 572 union ctl_ha_msg msg; 573 uint32_t sg_entries_sent; 574 int do_sg_copy, i, j; 575 576 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 577 memset(&msg.dt, 0, sizeof(msg.dt)); 578 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 579 msg.hdr.original_sc = io->io_hdr.original_sc; 580 msg.hdr.serializing_sc = io; 581 msg.hdr.nexus = io->io_hdr.nexus; 582 msg.hdr.status = io->io_hdr.status; 583 msg.dt.flags = io->io_hdr.flags; 584 585 /* 586 * We convert everything into a S/G list here. We can't 587 * pass by reference, only by value between controllers. 588 * So we can't pass a pointer to the S/G list, only as many 589 * S/G entries as we can fit in here. If it's possible for 590 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 591 * then we need to break this up into multiple transfers. 592 */ 593 if (io->scsiio.kern_sg_entries == 0) { 594 msg.dt.kern_sg_entries = 1; 595#if 0 596 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 597 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 598 } else { 599 /* XXX KDM use busdma here! */ 600 msg.dt.sg_list[0].addr = 601 (void *)vtophys(io->scsiio.kern_data_ptr); 602 } 603#else 604 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 605 ("HA does not support BUS_ADDR")); 606 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 607#endif 608 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 609 do_sg_copy = 0; 610 } else { 611 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 612 do_sg_copy = 1; 613 } 614 615 msg.dt.kern_data_len = io->scsiio.kern_data_len; 616 msg.dt.kern_total_len = io->scsiio.kern_total_len; 617 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 618 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 619 msg.dt.sg_sequence = 0; 620 621 /* 622 * Loop until we've sent all of the S/G entries. On the 623 * other end, we'll recompose these S/G entries into one 624 * contiguous list before processing. 625 */ 626 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 627 msg.dt.sg_sequence++) { 628 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 629 sizeof(msg.dt.sg_list[0])), 630 msg.dt.kern_sg_entries - sg_entries_sent); 631 if (do_sg_copy != 0) { 632 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 633 for (i = sg_entries_sent, j = 0; 634 i < msg.dt.cur_sg_entries; i++, j++) { 635#if 0 636 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 637 msg.dt.sg_list[j].addr = sgl[i].addr; 638 } else { 639 /* XXX KDM use busdma here! */ 640 msg.dt.sg_list[j].addr = 641 (void *)vtophys(sgl[i].addr); 642 } 643#else 644 KASSERT((io->io_hdr.flags & 645 CTL_FLAG_BUS_ADDR) == 0, 646 ("HA does not support BUS_ADDR")); 647 msg.dt.sg_list[j].addr = sgl[i].addr; 648#endif 649 msg.dt.sg_list[j].len = sgl[i].len; 650 } 651 } 652 653 sg_entries_sent += msg.dt.cur_sg_entries; 654 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 656 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 657 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 658 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 659 io->io_hdr.port_status = 31341; 660 io->scsiio.be_move_done(io); 661 return; 662 } 663 msg.dt.sent_sg_entries = sg_entries_sent; 664 } 665 666 /* 667 * Officially handover the request from us to peer. 668 * If failover has just happened, then we must return error. 669 * If failover happen just after, then it is not our problem. 670 */ 671 if (lun) 672 mtx_lock(&lun->lun_lock); 673 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 674 if (lun) 675 mtx_unlock(&lun->lun_lock); 676 io->io_hdr.port_status = 31342; 677 io->scsiio.be_move_done(io); 678 return; 679 } 680 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 681 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 682 if (lun) 683 mtx_unlock(&lun->lun_lock); 684} 685 686static void 687ctl_ha_done(union ctl_io *io) 688{ 689 union ctl_ha_msg msg; 690 691 if (io->io_hdr.io_type == CTL_IO_SCSI) { 692 memset(&msg, 0, sizeof(msg)); 693 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 694 msg.hdr.original_sc = io->io_hdr.original_sc; 695 msg.hdr.nexus = io->io_hdr.nexus; 696 msg.hdr.status = io->io_hdr.status; 697 msg.scsi.scsi_status = io->scsiio.scsi_status; 698 msg.scsi.tag_num = io->scsiio.tag_num; 699 msg.scsi.tag_type = io->scsiio.tag_type; 700 msg.scsi.sense_len = io->scsiio.sense_len; 701 msg.scsi.sense_residual = io->scsiio.sense_residual; 702 msg.scsi.residual = io->scsiio.residual; 703 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 704 io->scsiio.sense_len); 705 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 706 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 707 msg.scsi.sense_len, M_WAITOK); 708 } 709 ctl_free_io(io); 710} 711 712static void 713ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 714 union ctl_ha_msg *msg_info) 715{ 716 struct ctl_scsiio *ctsio; 717 718 if (msg_info->hdr.original_sc == NULL) { 719 printf("%s: original_sc == NULL!\n", __func__); 720 /* XXX KDM now what? */ 721 return; 722 } 723 724 ctsio = &msg_info->hdr.original_sc->scsiio; 725 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 726 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 727 ctsio->io_hdr.status = msg_info->hdr.status; 728 ctsio->scsi_status = msg_info->scsi.scsi_status; 729 ctsio->sense_len = msg_info->scsi.sense_len; 730 ctsio->sense_residual = msg_info->scsi.sense_residual; 731 ctsio->residual = msg_info->scsi.residual; 732 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 733 msg_info->scsi.sense_len); 734 ctl_enqueue_isc((union ctl_io *)ctsio); 735} 736 737static void 738ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 739 union ctl_ha_msg *msg_info) 740{ 741 struct ctl_scsiio *ctsio; 742 743 if (msg_info->hdr.serializing_sc == NULL) { 744 printf("%s: serializing_sc == NULL!\n", __func__); 745 /* XXX KDM now what? */ 746 return; 747 } 748 749 ctsio = &msg_info->hdr.serializing_sc->scsiio; 750 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 751 ctl_enqueue_isc((union ctl_io *)ctsio); 752} 753 754void 755ctl_isc_announce_lun(struct ctl_lun *lun) 756{ 757 struct ctl_softc *softc = lun->ctl_softc; 758 union ctl_ha_msg *msg; 759 struct ctl_ha_msg_lun_pr_key pr_key; 760 int i, k; 761 762 if (softc->ha_link != CTL_HA_LINK_ONLINE) 763 return; 764 mtx_lock(&lun->lun_lock); 765 i = sizeof(msg->lun); 766 if (lun->lun_devid) 767 i += lun->lun_devid->len; 768 i += sizeof(pr_key) * lun->pr_key_count; 769alloc: 770 mtx_unlock(&lun->lun_lock); 771 msg = malloc(i, M_CTL, M_WAITOK); 772 mtx_lock(&lun->lun_lock); 773 k = sizeof(msg->lun); 774 if (lun->lun_devid) 775 k += lun->lun_devid->len; 776 k += sizeof(pr_key) * lun->pr_key_count; 777 if (i < k) { 778 free(msg, M_CTL); 779 i = k; 780 goto alloc; 781 } 782 bzero(&msg->lun, sizeof(msg->lun)); 783 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 784 msg->hdr.nexus.targ_lun = lun->lun; 785 msg->hdr.nexus.targ_mapped_lun = lun->lun; 786 msg->lun.flags = lun->flags; 787 msg->lun.pr_generation = lun->pr_generation; 788 msg->lun.pr_res_idx = lun->pr_res_idx; 789 msg->lun.pr_res_type = lun->pr_res_type; 790 msg->lun.pr_key_count = lun->pr_key_count; 791 i = 0; 792 if (lun->lun_devid) { 793 msg->lun.lun_devid_len = lun->lun_devid->len; 794 memcpy(&msg->lun.data[i], lun->lun_devid->data, 795 msg->lun.lun_devid_len); 796 i += msg->lun.lun_devid_len; 797 } 798 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 799 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 800 continue; 801 pr_key.pr_iid = k; 802 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 803 i += sizeof(pr_key); 804 } 805 mtx_unlock(&lun->lun_lock); 806 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 807 M_WAITOK); 808 free(msg, M_CTL); 809 810 if (lun->flags & CTL_LUN_PRIMARY_SC) { 811 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 812 ctl_isc_announce_mode(lun, -1, 813 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 814 lun->mode_pages.index[i].subpage); 815 } 816 } 817} 818 819void 820ctl_isc_announce_port(struct ctl_port *port) 821{ 822 struct ctl_softc *softc = port->ctl_softc; 823 union ctl_ha_msg *msg; 824 int i; 825 826 if (port->targ_port < softc->port_min || 827 port->targ_port >= softc->port_max || 828 softc->ha_link != CTL_HA_LINK_ONLINE) 829 return; 830 i = sizeof(msg->port) + strlen(port->port_name) + 1; 831 if (port->lun_map) 832 i += port->lun_map_size * sizeof(uint32_t); 833 if (port->port_devid) 834 i += port->port_devid->len; 835 if (port->target_devid) 836 i += port->target_devid->len; 837 if (port->init_devid) 838 i += port->init_devid->len; 839 msg = malloc(i, M_CTL, M_WAITOK); 840 bzero(&msg->port, sizeof(msg->port)); 841 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 842 msg->hdr.nexus.targ_port = port->targ_port; 843 msg->port.port_type = port->port_type; 844 msg->port.physical_port = port->physical_port; 845 msg->port.virtual_port = port->virtual_port; 846 msg->port.status = port->status; 847 i = 0; 848 msg->port.name_len = sprintf(&msg->port.data[i], 849 "%d:%s", softc->ha_id, port->port_name) + 1; 850 i += msg->port.name_len; 851 if (port->lun_map) { 852 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t); 853 memcpy(&msg->port.data[i], port->lun_map, 854 msg->port.lun_map_len); 855 i += msg->port.lun_map_len; 856 } 857 if (port->port_devid) { 858 msg->port.port_devid_len = port->port_devid->len; 859 memcpy(&msg->port.data[i], port->port_devid->data, 860 msg->port.port_devid_len); 861 i += msg->port.port_devid_len; 862 } 863 if (port->target_devid) { 864 msg->port.target_devid_len = port->target_devid->len; 865 memcpy(&msg->port.data[i], port->target_devid->data, 866 msg->port.target_devid_len); 867 i += msg->port.target_devid_len; 868 } 869 if (port->init_devid) { 870 msg->port.init_devid_len = port->init_devid->len; 871 memcpy(&msg->port.data[i], port->init_devid->data, 872 msg->port.init_devid_len); 873 i += msg->port.init_devid_len; 874 } 875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 876 M_WAITOK); 877 free(msg, M_CTL); 878} 879 880void 881ctl_isc_announce_iid(struct ctl_port *port, int iid) 882{ 883 struct ctl_softc *softc = port->ctl_softc; 884 union ctl_ha_msg *msg; 885 int i, l; 886 887 if (port->targ_port < softc->port_min || 888 port->targ_port >= softc->port_max || 889 softc->ha_link != CTL_HA_LINK_ONLINE) 890 return; 891 mtx_lock(&softc->ctl_lock); 892 i = sizeof(msg->iid); 893 l = 0; 894 if (port->wwpn_iid[iid].name) 895 l = strlen(port->wwpn_iid[iid].name) + 1; 896 i += l; 897 msg = malloc(i, M_CTL, M_NOWAIT); 898 if (msg == NULL) { 899 mtx_unlock(&softc->ctl_lock); 900 return; 901 } 902 bzero(&msg->iid, sizeof(msg->iid)); 903 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 904 msg->hdr.nexus.targ_port = port->targ_port; 905 msg->hdr.nexus.initid = iid; 906 msg->iid.in_use = port->wwpn_iid[iid].in_use; 907 msg->iid.name_len = l; 908 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 909 if (port->wwpn_iid[iid].name) 910 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 911 mtx_unlock(&softc->ctl_lock); 912 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 913 free(msg, M_CTL); 914} 915 916void 917ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 918 uint8_t page, uint8_t subpage) 919{ 920 struct ctl_softc *softc = lun->ctl_softc; 921 union ctl_ha_msg msg; 922 u_int i; 923 924 if (softc->ha_link != CTL_HA_LINK_ONLINE) 925 return; 926 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 927 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 928 page && lun->mode_pages.index[i].subpage == subpage) 929 break; 930 } 931 if (i == CTL_NUM_MODE_PAGES) 932 return; 933 934 /* Don't try to replicate pages not present on this device. */ 935 if (lun->mode_pages.index[i].page_data == NULL) 936 return; 937 938 bzero(&msg.mode, sizeof(msg.mode)); 939 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 940 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 941 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 942 msg.hdr.nexus.targ_lun = lun->lun; 943 msg.hdr.nexus.targ_mapped_lun = lun->lun; 944 msg.mode.page_code = page; 945 msg.mode.subpage = subpage; 946 msg.mode.page_len = lun->mode_pages.index[i].page_len; 947 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 948 msg.mode.page_len); 949 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 950 M_WAITOK); 951} 952 953static void 954ctl_isc_ha_link_up(struct ctl_softc *softc) 955{ 956 struct ctl_port *port; 957 struct ctl_lun *lun; 958 union ctl_ha_msg msg; 959 int i; 960 961 /* Announce this node parameters to peer for validation. */ 962 msg.login.msg_type = CTL_MSG_LOGIN; 963 msg.login.version = CTL_HA_VERSION; 964 msg.login.ha_mode = softc->ha_mode; 965 msg.login.ha_id = softc->ha_id; 966 msg.login.max_luns = CTL_MAX_LUNS; 967 msg.login.max_ports = CTL_MAX_PORTS; 968 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 969 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 970 M_WAITOK); 971 972 STAILQ_FOREACH(port, &softc->port_list, links) { 973 ctl_isc_announce_port(port); 974 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 975 if (port->wwpn_iid[i].in_use) 976 ctl_isc_announce_iid(port, i); 977 } 978 } 979 STAILQ_FOREACH(lun, &softc->lun_list, links) 980 ctl_isc_announce_lun(lun); 981} 982 983static void 984ctl_isc_ha_link_down(struct ctl_softc *softc) 985{ 986 struct ctl_port *port; 987 struct ctl_lun *lun; 988 union ctl_io *io; 989 int i; 990 991 mtx_lock(&softc->ctl_lock); 992 STAILQ_FOREACH(lun, &softc->lun_list, links) { 993 mtx_lock(&lun->lun_lock); 994 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 995 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 996 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 997 } 998 mtx_unlock(&lun->lun_lock); 999 1000 mtx_unlock(&softc->ctl_lock); 1001 io = ctl_alloc_io(softc->othersc_pool); 1002 mtx_lock(&softc->ctl_lock); 1003 ctl_zero_io(io); 1004 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1005 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1006 ctl_enqueue_isc(io); 1007 } 1008 1009 STAILQ_FOREACH(port, &softc->port_list, links) { 1010 if (port->targ_port >= softc->port_min && 1011 port->targ_port < softc->port_max) 1012 continue; 1013 port->status &= ~CTL_PORT_STATUS_ONLINE; 1014 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1015 port->wwpn_iid[i].in_use = 0; 1016 free(port->wwpn_iid[i].name, M_CTL); 1017 port->wwpn_iid[i].name = NULL; 1018 } 1019 } 1020 mtx_unlock(&softc->ctl_lock); 1021} 1022 1023static void 1024ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1025{ 1026 struct ctl_lun *lun; 1027 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1028 1029 mtx_lock(&softc->ctl_lock); 1030 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS || 1031 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) { 1032 mtx_unlock(&softc->ctl_lock); 1033 return; 1034 } 1035 mtx_lock(&lun->lun_lock); 1036 mtx_unlock(&softc->ctl_lock); 1037 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set) 1038 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1039 if (msg->ua.ua_all) { 1040 if (msg->ua.ua_set) 1041 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1042 else 1043 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1044 } else { 1045 if (msg->ua.ua_set) 1046 ctl_est_ua(lun, iid, msg->ua.ua_type); 1047 else 1048 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1049 } 1050 mtx_unlock(&lun->lun_lock); 1051} 1052 1053static void 1054ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1055{ 1056 struct ctl_lun *lun; 1057 struct ctl_ha_msg_lun_pr_key pr_key; 1058 int i, k; 1059 ctl_lun_flags oflags; 1060 uint32_t targ_lun; 1061 1062 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1063 mtx_lock(&softc->ctl_lock); 1064 if (targ_lun >= CTL_MAX_LUNS || 1065 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1066 mtx_unlock(&softc->ctl_lock); 1067 return; 1068 } 1069 mtx_lock(&lun->lun_lock); 1070 mtx_unlock(&softc->ctl_lock); 1071 if (lun->flags & CTL_LUN_DISABLED) { 1072 mtx_unlock(&lun->lun_lock); 1073 return; 1074 } 1075 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1076 if (msg->lun.lun_devid_len != i || (i > 0 && 1077 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1078 mtx_unlock(&lun->lun_lock); 1079 printf("%s: Received conflicting HA LUN %d\n", 1080 __func__, targ_lun); 1081 return; 1082 } else { 1083 /* Record whether peer is primary. */ 1084 oflags = lun->flags; 1085 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1086 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1087 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1088 else 1089 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1090 if (oflags != lun->flags) 1091 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1092 1093 /* If peer is primary and we are not -- use data */ 1094 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1095 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1096 lun->pr_generation = msg->lun.pr_generation; 1097 lun->pr_res_idx = msg->lun.pr_res_idx; 1098 lun->pr_res_type = msg->lun.pr_res_type; 1099 lun->pr_key_count = msg->lun.pr_key_count; 1100 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1101 ctl_clr_prkey(lun, k); 1102 for (k = 0; k < msg->lun.pr_key_count; k++) { 1103 memcpy(&pr_key, &msg->lun.data[i], 1104 sizeof(pr_key)); 1105 ctl_alloc_prkey(lun, pr_key.pr_iid); 1106 ctl_set_prkey(lun, pr_key.pr_iid, 1107 pr_key.pr_key); 1108 i += sizeof(pr_key); 1109 } 1110 } 1111 1112 mtx_unlock(&lun->lun_lock); 1113 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1114 __func__, targ_lun, 1115 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1116 "primary" : "secondary")); 1117 1118 /* If we are primary but peer doesn't know -- notify */ 1119 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1120 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1121 ctl_isc_announce_lun(lun); 1122 } 1123} 1124 1125static void 1126ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1127{ 1128 struct ctl_port *port; 1129 struct ctl_lun *lun; 1130 int i, new; 1131 1132 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1133 if (port == NULL) { 1134 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1135 msg->hdr.nexus.targ_port)); 1136 new = 1; 1137 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1138 port->frontend = &ha_frontend; 1139 port->targ_port = msg->hdr.nexus.targ_port; 1140 port->fe_datamove = ctl_ha_datamove; 1141 port->fe_done = ctl_ha_done; 1142 } else if (port->frontend == &ha_frontend) { 1143 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1144 msg->hdr.nexus.targ_port)); 1145 new = 0; 1146 } else { 1147 printf("%s: Received conflicting HA port %d\n", 1148 __func__, msg->hdr.nexus.targ_port); 1149 return; 1150 } 1151 port->port_type = msg->port.port_type; 1152 port->physical_port = msg->port.physical_port; 1153 port->virtual_port = msg->port.virtual_port; 1154 port->status = msg->port.status; 1155 i = 0; 1156 free(port->port_name, M_CTL); 1157 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1158 M_CTL); 1159 i += msg->port.name_len; 1160 if (msg->port.lun_map_len != 0) { 1161 if (port->lun_map == NULL || 1162 port->lun_map_size * sizeof(uint32_t) < 1163 msg->port.lun_map_len) { 1164 port->lun_map_size = 0; 1165 free(port->lun_map, M_CTL); 1166 port->lun_map = malloc(msg->port.lun_map_len, 1167 M_CTL, M_WAITOK); 1168 } 1169 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len); 1170 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t); 1171 i += msg->port.lun_map_len; 1172 } else { 1173 port->lun_map_size = 0; 1174 free(port->lun_map, M_CTL); 1175 port->lun_map = NULL; 1176 } 1177 if (msg->port.port_devid_len != 0) { 1178 if (port->port_devid == NULL || 1179 port->port_devid->len < msg->port.port_devid_len) { 1180 free(port->port_devid, M_CTL); 1181 port->port_devid = malloc(sizeof(struct ctl_devid) + 1182 msg->port.port_devid_len, M_CTL, M_WAITOK); 1183 } 1184 memcpy(port->port_devid->data, &msg->port.data[i], 1185 msg->port.port_devid_len); 1186 port->port_devid->len = msg->port.port_devid_len; 1187 i += msg->port.port_devid_len; 1188 } else { 1189 free(port->port_devid, M_CTL); 1190 port->port_devid = NULL; 1191 } 1192 if (msg->port.target_devid_len != 0) { 1193 if (port->target_devid == NULL || 1194 port->target_devid->len < msg->port.target_devid_len) { 1195 free(port->target_devid, M_CTL); 1196 port->target_devid = malloc(sizeof(struct ctl_devid) + 1197 msg->port.target_devid_len, M_CTL, M_WAITOK); 1198 } 1199 memcpy(port->target_devid->data, &msg->port.data[i], 1200 msg->port.target_devid_len); 1201 port->target_devid->len = msg->port.target_devid_len; 1202 i += msg->port.target_devid_len; 1203 } else { 1204 free(port->target_devid, M_CTL); 1205 port->target_devid = NULL; 1206 } 1207 if (msg->port.init_devid_len != 0) { 1208 if (port->init_devid == NULL || 1209 port->init_devid->len < msg->port.init_devid_len) { 1210 free(port->init_devid, M_CTL); 1211 port->init_devid = malloc(sizeof(struct ctl_devid) + 1212 msg->port.init_devid_len, M_CTL, M_WAITOK); 1213 } 1214 memcpy(port->init_devid->data, &msg->port.data[i], 1215 msg->port.init_devid_len); 1216 port->init_devid->len = msg->port.init_devid_len; 1217 i += msg->port.init_devid_len; 1218 } else { 1219 free(port->init_devid, M_CTL); 1220 port->init_devid = NULL; 1221 } 1222 if (new) { 1223 if (ctl_port_register(port) != 0) { 1224 printf("%s: ctl_port_register() failed with error\n", 1225 __func__); 1226 } 1227 } 1228 mtx_lock(&softc->ctl_lock); 1229 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1230 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 1231 continue; 1232 mtx_lock(&lun->lun_lock); 1233 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1234 mtx_unlock(&lun->lun_lock); 1235 } 1236 mtx_unlock(&softc->ctl_lock); 1237} 1238 1239static void 1240ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1241{ 1242 struct ctl_port *port; 1243 int iid; 1244 1245 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1246 if (port == NULL) { 1247 printf("%s: Received IID for unknown port %d\n", 1248 __func__, msg->hdr.nexus.targ_port); 1249 return; 1250 } 1251 iid = msg->hdr.nexus.initid; 1252 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1253 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1254 free(port->wwpn_iid[iid].name, M_CTL); 1255 if (msg->iid.name_len) { 1256 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1257 msg->iid.name_len, M_CTL); 1258 } else 1259 port->wwpn_iid[iid].name = NULL; 1260} 1261 1262static void 1263ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1264{ 1265 1266 if (msg->login.version != CTL_HA_VERSION) { 1267 printf("CTL HA peers have different versions %d != %d\n", 1268 msg->login.version, CTL_HA_VERSION); 1269 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1270 return; 1271 } 1272 if (msg->login.ha_mode != softc->ha_mode) { 1273 printf("CTL HA peers have different ha_mode %d != %d\n", 1274 msg->login.ha_mode, softc->ha_mode); 1275 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1276 return; 1277 } 1278 if (msg->login.ha_id == softc->ha_id) { 1279 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1280 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1281 return; 1282 } 1283 if (msg->login.max_luns != CTL_MAX_LUNS || 1284 msg->login.max_ports != CTL_MAX_PORTS || 1285 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1286 printf("CTL HA peers have different limits\n"); 1287 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1288 return; 1289 } 1290} 1291 1292static void 1293ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1294{ 1295 struct ctl_lun *lun; 1296 u_int i; 1297 uint32_t initidx, targ_lun; 1298 1299 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1300 mtx_lock(&softc->ctl_lock); 1301 if (targ_lun >= CTL_MAX_LUNS || 1302 (lun = softc->ctl_luns[targ_lun]) == NULL) { 1303 mtx_unlock(&softc->ctl_lock); 1304 return; 1305 } 1306 mtx_lock(&lun->lun_lock); 1307 mtx_unlock(&softc->ctl_lock); 1308 if (lun->flags & CTL_LUN_DISABLED) { 1309 mtx_unlock(&lun->lun_lock); 1310 return; 1311 } 1312 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1313 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1314 msg->mode.page_code && 1315 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1316 break; 1317 } 1318 if (i == CTL_NUM_MODE_PAGES) { 1319 mtx_unlock(&lun->lun_lock); 1320 return; 1321 } 1322 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1323 lun->mode_pages.index[i].page_len); 1324 initidx = ctl_get_initindex(&msg->hdr.nexus); 1325 if (initidx != -1) 1326 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1327 mtx_unlock(&lun->lun_lock); 1328} 1329 1330/* 1331 * ISC (Inter Shelf Communication) event handler. Events from the HA 1332 * subsystem come in here. 1333 */ 1334static void 1335ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1336{ 1337 struct ctl_softc *softc = control_softc; 1338 union ctl_io *io; 1339 struct ctl_prio *presio; 1340 ctl_ha_status isc_status; 1341 1342 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1343 if (event == CTL_HA_EVT_MSG_RECV) { 1344 union ctl_ha_msg *msg, msgbuf; 1345 1346 if (param > sizeof(msgbuf)) 1347 msg = malloc(param, M_CTL, M_WAITOK); 1348 else 1349 msg = &msgbuf; 1350 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1351 M_WAITOK); 1352 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1353 printf("%s: Error receiving message: %d\n", 1354 __func__, isc_status); 1355 if (msg != &msgbuf) 1356 free(msg, M_CTL); 1357 return; 1358 } 1359 1360 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1361 switch (msg->hdr.msg_type) { 1362 case CTL_MSG_SERIALIZE: 1363 io = ctl_alloc_io(softc->othersc_pool); 1364 ctl_zero_io(io); 1365 // populate ctsio from msg 1366 io->io_hdr.io_type = CTL_IO_SCSI; 1367 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1368 io->io_hdr.original_sc = msg->hdr.original_sc; 1369 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1370 CTL_FLAG_IO_ACTIVE; 1371 /* 1372 * If we're in serialization-only mode, we don't 1373 * want to go through full done processing. Thus 1374 * the COPY flag. 1375 * 1376 * XXX KDM add another flag that is more specific. 1377 */ 1378 if (softc->ha_mode != CTL_HA_MODE_XFER) 1379 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1380 io->io_hdr.nexus = msg->hdr.nexus; 1381#if 0 1382 printf("port %u, iid %u, lun %u\n", 1383 io->io_hdr.nexus.targ_port, 1384 io->io_hdr.nexus.initid, 1385 io->io_hdr.nexus.targ_lun); 1386#endif 1387 io->scsiio.tag_num = msg->scsi.tag_num; 1388 io->scsiio.tag_type = msg->scsi.tag_type; 1389#ifdef CTL_TIME_IO 1390 io->io_hdr.start_time = time_uptime; 1391 getbinuptime(&io->io_hdr.start_bt); 1392#endif /* CTL_TIME_IO */ 1393 io->scsiio.cdb_len = msg->scsi.cdb_len; 1394 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1395 CTL_MAX_CDBLEN); 1396 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1397 const struct ctl_cmd_entry *entry; 1398 1399 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1400 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1401 io->io_hdr.flags |= 1402 entry->flags & CTL_FLAG_DATA_MASK; 1403 } 1404 ctl_enqueue_isc(io); 1405 break; 1406 1407 /* Performed on the Originating SC, XFER mode only */ 1408 case CTL_MSG_DATAMOVE: { 1409 struct ctl_sg_entry *sgl; 1410 int i, j; 1411 1412 io = msg->hdr.original_sc; 1413 if (io == NULL) { 1414 printf("%s: original_sc == NULL!\n", __func__); 1415 /* XXX KDM do something here */ 1416 break; 1417 } 1418 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1419 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1420 /* 1421 * Keep track of this, we need to send it back over 1422 * when the datamove is complete. 1423 */ 1424 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1425 if (msg->hdr.status == CTL_SUCCESS) 1426 io->io_hdr.status = msg->hdr.status; 1427 1428 if (msg->dt.sg_sequence == 0) { 1429#ifdef CTL_TIME_IO 1430 getbinuptime(&io->io_hdr.dma_start_bt); 1431#endif 1432 i = msg->dt.kern_sg_entries + 1433 msg->dt.kern_data_len / 1434 CTL_HA_DATAMOVE_SEGMENT + 1; 1435 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1436 M_WAITOK | M_ZERO); 1437 io->io_hdr.remote_sglist = sgl; 1438 io->io_hdr.local_sglist = 1439 &sgl[msg->dt.kern_sg_entries]; 1440 1441 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1442 1443 io->scsiio.kern_sg_entries = 1444 msg->dt.kern_sg_entries; 1445 io->scsiio.rem_sg_entries = 1446 msg->dt.kern_sg_entries; 1447 io->scsiio.kern_data_len = 1448 msg->dt.kern_data_len; 1449 io->scsiio.kern_total_len = 1450 msg->dt.kern_total_len; 1451 io->scsiio.kern_data_resid = 1452 msg->dt.kern_data_resid; 1453 io->scsiio.kern_rel_offset = 1454 msg->dt.kern_rel_offset; 1455 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1456 io->io_hdr.flags |= msg->dt.flags & 1457 CTL_FLAG_BUS_ADDR; 1458 } else 1459 sgl = (struct ctl_sg_entry *) 1460 io->scsiio.kern_data_ptr; 1461 1462 for (i = msg->dt.sent_sg_entries, j = 0; 1463 i < (msg->dt.sent_sg_entries + 1464 msg->dt.cur_sg_entries); i++, j++) { 1465 sgl[i].addr = msg->dt.sg_list[j].addr; 1466 sgl[i].len = msg->dt.sg_list[j].len; 1467 1468#if 0 1469 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1470 __func__, sgl[i].addr, sgl[i].len, j, i); 1471#endif 1472 } 1473 1474 /* 1475 * If this is the last piece of the I/O, we've got 1476 * the full S/G list. Queue processing in the thread. 1477 * Otherwise wait for the next piece. 1478 */ 1479 if (msg->dt.sg_last != 0) 1480 ctl_enqueue_isc(io); 1481 break; 1482 } 1483 /* Performed on the Serializing (primary) SC, XFER mode only */ 1484 case CTL_MSG_DATAMOVE_DONE: { 1485 if (msg->hdr.serializing_sc == NULL) { 1486 printf("%s: serializing_sc == NULL!\n", 1487 __func__); 1488 /* XXX KDM now what? */ 1489 break; 1490 } 1491 /* 1492 * We grab the sense information here in case 1493 * there was a failure, so we can return status 1494 * back to the initiator. 1495 */ 1496 io = msg->hdr.serializing_sc; 1497 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1498 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1499 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1500 io->io_hdr.port_status = msg->scsi.fetd_status; 1501 io->scsiio.residual = msg->scsi.residual; 1502 if (msg->hdr.status != CTL_STATUS_NONE) { 1503 io->io_hdr.status = msg->hdr.status; 1504 io->scsiio.scsi_status = msg->scsi.scsi_status; 1505 io->scsiio.sense_len = msg->scsi.sense_len; 1506 io->scsiio.sense_residual =msg->scsi.sense_residual; 1507 memcpy(&io->scsiio.sense_data, 1508 &msg->scsi.sense_data, 1509 msg->scsi.sense_len); 1510 if (msg->hdr.status == CTL_SUCCESS) 1511 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1512 } 1513 ctl_enqueue_isc(io); 1514 break; 1515 } 1516 1517 /* Preformed on Originating SC, SER_ONLY mode */ 1518 case CTL_MSG_R2R: 1519 io = msg->hdr.original_sc; 1520 if (io == NULL) { 1521 printf("%s: original_sc == NULL!\n", 1522 __func__); 1523 break; 1524 } 1525 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1526 io->io_hdr.msg_type = CTL_MSG_R2R; 1527 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1528 ctl_enqueue_isc(io); 1529 break; 1530 1531 /* 1532 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1533 * mode. 1534 * Performed on the Originating (i.e. secondary) SC in XFER 1535 * mode 1536 */ 1537 case CTL_MSG_FINISH_IO: 1538 if (softc->ha_mode == CTL_HA_MODE_XFER) 1539 ctl_isc_handler_finish_xfer(softc, msg); 1540 else 1541 ctl_isc_handler_finish_ser_only(softc, msg); 1542 break; 1543 1544 /* Preformed on Originating SC */ 1545 case CTL_MSG_BAD_JUJU: 1546 io = msg->hdr.original_sc; 1547 if (io == NULL) { 1548 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1549 __func__); 1550 break; 1551 } 1552 ctl_copy_sense_data(msg, io); 1553 /* 1554 * IO should have already been cleaned up on other 1555 * SC so clear this flag so we won't send a message 1556 * back to finish the IO there. 1557 */ 1558 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1559 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1560 1561 /* io = msg->hdr.serializing_sc; */ 1562 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1563 ctl_enqueue_isc(io); 1564 break; 1565 1566 /* Handle resets sent from the other side */ 1567 case CTL_MSG_MANAGE_TASKS: { 1568 struct ctl_taskio *taskio; 1569 taskio = (struct ctl_taskio *)ctl_alloc_io( 1570 softc->othersc_pool); 1571 ctl_zero_io((union ctl_io *)taskio); 1572 taskio->io_hdr.io_type = CTL_IO_TASK; 1573 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1574 taskio->io_hdr.nexus = msg->hdr.nexus; 1575 taskio->task_action = msg->task.task_action; 1576 taskio->tag_num = msg->task.tag_num; 1577 taskio->tag_type = msg->task.tag_type; 1578#ifdef CTL_TIME_IO 1579 taskio->io_hdr.start_time = time_uptime; 1580 getbinuptime(&taskio->io_hdr.start_bt); 1581#endif /* CTL_TIME_IO */ 1582 ctl_run_task((union ctl_io *)taskio); 1583 break; 1584 } 1585 /* Persistent Reserve action which needs attention */ 1586 case CTL_MSG_PERS_ACTION: 1587 presio = (struct ctl_prio *)ctl_alloc_io( 1588 softc->othersc_pool); 1589 ctl_zero_io((union ctl_io *)presio); 1590 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1591 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1592 presio->io_hdr.nexus = msg->hdr.nexus; 1593 presio->pr_msg = msg->pr; 1594 ctl_enqueue_isc((union ctl_io *)presio); 1595 break; 1596 case CTL_MSG_UA: 1597 ctl_isc_ua(softc, msg, param); 1598 break; 1599 case CTL_MSG_PORT_SYNC: 1600 ctl_isc_port_sync(softc, msg, param); 1601 break; 1602 case CTL_MSG_LUN_SYNC: 1603 ctl_isc_lun_sync(softc, msg, param); 1604 break; 1605 case CTL_MSG_IID_SYNC: 1606 ctl_isc_iid_sync(softc, msg, param); 1607 break; 1608 case CTL_MSG_LOGIN: 1609 ctl_isc_login(softc, msg, param); 1610 break; 1611 case CTL_MSG_MODE_SYNC: 1612 ctl_isc_mode_sync(softc, msg, param); 1613 break; 1614 default: 1615 printf("Received HA message of unknown type %d\n", 1616 msg->hdr.msg_type); 1617 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1618 break; 1619 } 1620 if (msg != &msgbuf) 1621 free(msg, M_CTL); 1622 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1623 printf("CTL: HA link status changed from %d to %d\n", 1624 softc->ha_link, param); 1625 if (param == softc->ha_link) 1626 return; 1627 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1628 softc->ha_link = param; 1629 ctl_isc_ha_link_down(softc); 1630 } else { 1631 softc->ha_link = param; 1632 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1633 ctl_isc_ha_link_up(softc); 1634 } 1635 return; 1636 } else { 1637 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1638 return; 1639 } 1640} 1641 1642static void 1643ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1644{ 1645 1646 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1647 src->scsi.sense_len); 1648 dest->scsiio.scsi_status = src->scsi.scsi_status; 1649 dest->scsiio.sense_len = src->scsi.sense_len; 1650 dest->io_hdr.status = src->hdr.status; 1651} 1652 1653static void 1654ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1655{ 1656 1657 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1658 src->scsiio.sense_len); 1659 dest->scsi.scsi_status = src->scsiio.scsi_status; 1660 dest->scsi.sense_len = src->scsiio.sense_len; 1661 dest->hdr.status = src->io_hdr.status; 1662} 1663 1664void 1665ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1666{ 1667 struct ctl_softc *softc = lun->ctl_softc; 1668 ctl_ua_type *pu; 1669 1670 if (initidx < softc->init_min || initidx >= softc->init_max) 1671 return; 1672 mtx_assert(&lun->lun_lock, MA_OWNED); 1673 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1674 if (pu == NULL) 1675 return; 1676 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1677} 1678 1679void 1680ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1681{ 1682 int i; 1683 1684 mtx_assert(&lun->lun_lock, MA_OWNED); 1685 if (lun->pending_ua[port] == NULL) 1686 return; 1687 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1688 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1689 continue; 1690 lun->pending_ua[port][i] |= ua; 1691 } 1692} 1693 1694void 1695ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1696{ 1697 struct ctl_softc *softc = lun->ctl_softc; 1698 int i; 1699 1700 mtx_assert(&lun->lun_lock, MA_OWNED); 1701 for (i = softc->port_min; i < softc->port_max; i++) 1702 ctl_est_ua_port(lun, i, except, ua); 1703} 1704 1705void 1706ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1707{ 1708 struct ctl_softc *softc = lun->ctl_softc; 1709 ctl_ua_type *pu; 1710 1711 if (initidx < softc->init_min || initidx >= softc->init_max) 1712 return; 1713 mtx_assert(&lun->lun_lock, MA_OWNED); 1714 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1715 if (pu == NULL) 1716 return; 1717 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1718} 1719 1720void 1721ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1722{ 1723 struct ctl_softc *softc = lun->ctl_softc; 1724 int i, j; 1725 1726 mtx_assert(&lun->lun_lock, MA_OWNED); 1727 for (i = softc->port_min; i < softc->port_max; i++) { 1728 if (lun->pending_ua[i] == NULL) 1729 continue; 1730 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1731 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1732 continue; 1733 lun->pending_ua[i][j] &= ~ua; 1734 } 1735 } 1736} 1737 1738void 1739ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1740 ctl_ua_type ua_type) 1741{ 1742 struct ctl_lun *lun; 1743 1744 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1745 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1746 mtx_lock(&lun->lun_lock); 1747 ctl_clr_ua(lun, initidx, ua_type); 1748 mtx_unlock(&lun->lun_lock); 1749 } 1750} 1751 1752static int 1753ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1754{ 1755 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1756 struct ctl_lun *lun; 1757 struct ctl_lun_req ireq; 1758 int error, value; 1759 1760 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1761 error = sysctl_handle_int(oidp, &value, 0, req); 1762 if ((error != 0) || (req->newptr == NULL)) 1763 return (error); 1764 1765 mtx_lock(&softc->ctl_lock); 1766 if (value == 0) 1767 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1768 else 1769 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1770 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1771 mtx_unlock(&softc->ctl_lock); 1772 bzero(&ireq, sizeof(ireq)); 1773 ireq.reqtype = CTL_LUNREQ_MODIFY; 1774 ireq.reqdata.modify.lun_id = lun->lun; 1775 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1776 curthread); 1777 if (ireq.status != CTL_LUN_OK) { 1778 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1779 __func__, ireq.status, ireq.error_str); 1780 } 1781 mtx_lock(&softc->ctl_lock); 1782 } 1783 mtx_unlock(&softc->ctl_lock); 1784 return (0); 1785} 1786 1787static int 1788ctl_init(void) 1789{ 1790 struct make_dev_args args; 1791 struct ctl_softc *softc; 1792 void *other_pool; 1793 int i, error; 1794 1795 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1796 M_WAITOK | M_ZERO); 1797 1798 make_dev_args_init(&args); 1799 args.mda_devsw = &ctl_cdevsw; 1800 args.mda_uid = UID_ROOT; 1801 args.mda_gid = GID_OPERATOR; 1802 args.mda_mode = 0600; 1803 args.mda_si_drv1 = softc; 1804 error = make_dev_s(&args, &softc->dev, "cam/ctl"); 1805 if (error != 0) { 1806 free(control_softc, M_DEVBUF); 1807 return (error); 1808 } 1809 1810 sysctl_ctx_init(&softc->sysctl_ctx); 1811 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1812 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1813 CTLFLAG_RD, 0, "CAM Target Layer"); 1814 1815 if (softc->sysctl_tree == NULL) { 1816 printf("%s: unable to allocate sysctl tree\n", __func__); 1817 destroy_dev(softc->dev); 1818 free(control_softc, M_DEVBUF); 1819 control_softc = NULL; 1820 return (ENOMEM); 1821 } 1822 1823 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1824 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1825 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1826 softc->flags = 0; 1827 1828 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1829 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1830 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1831 1832 /* 1833 * In Copan's HA scheme, the "master" and "slave" roles are 1834 * figured out through the slot the controller is in. Although it 1835 * is an active/active system, someone has to be in charge. 1836 */ 1837 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1838 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1839 "HA head ID (0 - no HA)"); 1840 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) { 1841 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1842 softc->is_single = 1; 1843 softc->port_cnt = CTL_MAX_PORTS; 1844 softc->port_min = 0; 1845 } else { 1846 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES; 1847 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1848 } 1849 softc->port_max = softc->port_min + softc->port_cnt; 1850 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1851 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1852 1853 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1854 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1855 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1856 1857 STAILQ_INIT(&softc->lun_list); 1858 STAILQ_INIT(&softc->pending_lun_queue); 1859 STAILQ_INIT(&softc->fe_list); 1860 STAILQ_INIT(&softc->port_list); 1861 STAILQ_INIT(&softc->be_list); 1862 ctl_tpc_init(softc); 1863 1864 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1865 &other_pool) != 0) 1866 { 1867 printf("ctl: can't allocate %d entry other SC pool, " 1868 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1869 return (ENOMEM); 1870 } 1871 softc->othersc_pool = other_pool; 1872 1873 if (worker_threads <= 0) 1874 worker_threads = max(1, mp_ncpus / 4); 1875 if (worker_threads > CTL_MAX_THREADS) 1876 worker_threads = CTL_MAX_THREADS; 1877 1878 for (i = 0; i < worker_threads; i++) { 1879 struct ctl_thread *thr = &softc->threads[i]; 1880 1881 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1882 thr->ctl_softc = softc; 1883 STAILQ_INIT(&thr->incoming_queue); 1884 STAILQ_INIT(&thr->rtr_queue); 1885 STAILQ_INIT(&thr->done_queue); 1886 STAILQ_INIT(&thr->isc_queue); 1887 1888 error = kproc_kthread_add(ctl_work_thread, thr, 1889 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1890 if (error != 0) { 1891 printf("error creating CTL work thread!\n"); 1892 ctl_pool_free(other_pool); 1893 return (error); 1894 } 1895 } 1896 error = kproc_kthread_add(ctl_lun_thread, softc, 1897 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1898 if (error != 0) { 1899 printf("error creating CTL lun thread!\n"); 1900 ctl_pool_free(other_pool); 1901 return (error); 1902 } 1903 error = kproc_kthread_add(ctl_thresh_thread, softc, 1904 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1905 if (error != 0) { 1906 printf("error creating CTL threshold thread!\n"); 1907 ctl_pool_free(other_pool); 1908 return (error); 1909 } 1910 1911 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1912 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1913 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1914 1915 if (softc->is_single == 0) { 1916 ctl_frontend_register(&ha_frontend); 1917 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1918 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1919 softc->is_single = 1; 1920 } else 1921 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1922 != CTL_HA_STATUS_SUCCESS) { 1923 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1924 softc->is_single = 1; 1925 } 1926 } 1927 return (0); 1928} 1929 1930void 1931ctl_shutdown(void) 1932{ 1933 struct ctl_softc *softc = control_softc; 1934 struct ctl_lun *lun, *next_lun; 1935 1936 if (softc->is_single == 0) { 1937 ctl_ha_msg_shutdown(softc); 1938 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1939 != CTL_HA_STATUS_SUCCESS) 1940 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1941 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1942 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1943 ctl_frontend_deregister(&ha_frontend); 1944 } 1945 1946 mtx_lock(&softc->ctl_lock); 1947 1948 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1949 ctl_free_lun(lun); 1950 1951 mtx_unlock(&softc->ctl_lock); 1952 1953#if 0 1954 ctl_shutdown_thread(softc->work_thread); 1955 mtx_destroy(&softc->queue_lock); 1956#endif 1957 1958 ctl_tpc_shutdown(softc); 1959 uma_zdestroy(softc->io_zone); 1960 mtx_destroy(&softc->ctl_lock); 1961 1962 destroy_dev(softc->dev); 1963 1964 sysctl_ctx_free(&softc->sysctl_ctx); 1965 1966 free(control_softc, M_DEVBUF); 1967 control_softc = NULL; 1968} 1969 1970static int 1971ctl_module_event_handler(module_t mod, int what, void *arg) 1972{ 1973 1974 switch (what) { 1975 case MOD_LOAD: 1976 return (ctl_init()); 1977 case MOD_UNLOAD: 1978 return (EBUSY); 1979 default: 1980 return (EOPNOTSUPP); 1981 } 1982} 1983 1984/* 1985 * XXX KDM should we do some access checks here? Bump a reference count to 1986 * prevent a CTL module from being unloaded while someone has it open? 1987 */ 1988static int 1989ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1990{ 1991 return (0); 1992} 1993 1994static int 1995ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1996{ 1997 return (0); 1998} 1999 2000/* 2001 * Remove an initiator by port number and initiator ID. 2002 * Returns 0 for success, -1 for failure. 2003 */ 2004int 2005ctl_remove_initiator(struct ctl_port *port, int iid) 2006{ 2007 struct ctl_softc *softc = port->ctl_softc; 2008 2009 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2010 2011 if (iid > CTL_MAX_INIT_PER_PORT) { 2012 printf("%s: initiator ID %u > maximun %u!\n", 2013 __func__, iid, CTL_MAX_INIT_PER_PORT); 2014 return (-1); 2015 } 2016 2017 mtx_lock(&softc->ctl_lock); 2018 port->wwpn_iid[iid].in_use--; 2019 port->wwpn_iid[iid].last_use = time_uptime; 2020 mtx_unlock(&softc->ctl_lock); 2021 ctl_isc_announce_iid(port, iid); 2022 2023 return (0); 2024} 2025 2026/* 2027 * Add an initiator to the initiator map. 2028 * Returns iid for success, < 0 for failure. 2029 */ 2030int 2031ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2032{ 2033 struct ctl_softc *softc = port->ctl_softc; 2034 time_t best_time; 2035 int i, best; 2036 2037 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2038 2039 if (iid >= CTL_MAX_INIT_PER_PORT) { 2040 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2041 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2042 free(name, M_CTL); 2043 return (-1); 2044 } 2045 2046 mtx_lock(&softc->ctl_lock); 2047 2048 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2049 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2050 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2051 iid = i; 2052 break; 2053 } 2054 if (name != NULL && port->wwpn_iid[i].name != NULL && 2055 strcmp(name, port->wwpn_iid[i].name) == 0) { 2056 iid = i; 2057 break; 2058 } 2059 } 2060 } 2061 2062 if (iid < 0) { 2063 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2064 if (port->wwpn_iid[i].in_use == 0 && 2065 port->wwpn_iid[i].wwpn == 0 && 2066 port->wwpn_iid[i].name == NULL) { 2067 iid = i; 2068 break; 2069 } 2070 } 2071 } 2072 2073 if (iid < 0) { 2074 best = -1; 2075 best_time = INT32_MAX; 2076 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2077 if (port->wwpn_iid[i].in_use == 0) { 2078 if (port->wwpn_iid[i].last_use < best_time) { 2079 best = i; 2080 best_time = port->wwpn_iid[i].last_use; 2081 } 2082 } 2083 } 2084 iid = best; 2085 } 2086 2087 if (iid < 0) { 2088 mtx_unlock(&softc->ctl_lock); 2089 free(name, M_CTL); 2090 return (-2); 2091 } 2092 2093 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2094 /* 2095 * This is not an error yet. 2096 */ 2097 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2098#if 0 2099 printf("%s: port %d iid %u WWPN %#jx arrived" 2100 " again\n", __func__, port->targ_port, 2101 iid, (uintmax_t)wwpn); 2102#endif 2103 goto take; 2104 } 2105 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2106 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2107#if 0 2108 printf("%s: port %d iid %u name '%s' arrived" 2109 " again\n", __func__, port->targ_port, 2110 iid, name); 2111#endif 2112 goto take; 2113 } 2114 2115 /* 2116 * This is an error, but what do we do about it? The 2117 * driver is telling us we have a new WWPN for this 2118 * initiator ID, so we pretty much need to use it. 2119 */ 2120 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2121 " but WWPN %#jx '%s' is still at that address\n", 2122 __func__, port->targ_port, iid, wwpn, name, 2123 (uintmax_t)port->wwpn_iid[iid].wwpn, 2124 port->wwpn_iid[iid].name); 2125 2126 /* 2127 * XXX KDM clear have_ca and ua_pending on each LUN for 2128 * this initiator. 2129 */ 2130 } 2131take: 2132 free(port->wwpn_iid[iid].name, M_CTL); 2133 port->wwpn_iid[iid].name = name; 2134 port->wwpn_iid[iid].wwpn = wwpn; 2135 port->wwpn_iid[iid].in_use++; 2136 mtx_unlock(&softc->ctl_lock); 2137 ctl_isc_announce_iid(port, iid); 2138 2139 return (iid); 2140} 2141 2142static int 2143ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2144{ 2145 int len; 2146 2147 switch (port->port_type) { 2148 case CTL_PORT_FC: 2149 { 2150 struct scsi_transportid_fcp *id = 2151 (struct scsi_transportid_fcp *)buf; 2152 if (port->wwpn_iid[iid].wwpn == 0) 2153 return (0); 2154 memset(id, 0, sizeof(*id)); 2155 id->format_protocol = SCSI_PROTO_FC; 2156 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2157 return (sizeof(*id)); 2158 } 2159 case CTL_PORT_ISCSI: 2160 { 2161 struct scsi_transportid_iscsi_port *id = 2162 (struct scsi_transportid_iscsi_port *)buf; 2163 if (port->wwpn_iid[iid].name == NULL) 2164 return (0); 2165 memset(id, 0, 256); 2166 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2167 SCSI_PROTO_ISCSI; 2168 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2169 len = roundup2(min(len, 252), 4); 2170 scsi_ulto2b(len, id->additional_length); 2171 return (sizeof(*id) + len); 2172 } 2173 case CTL_PORT_SAS: 2174 { 2175 struct scsi_transportid_sas *id = 2176 (struct scsi_transportid_sas *)buf; 2177 if (port->wwpn_iid[iid].wwpn == 0) 2178 return (0); 2179 memset(id, 0, sizeof(*id)); 2180 id->format_protocol = SCSI_PROTO_SAS; 2181 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2182 return (sizeof(*id)); 2183 } 2184 default: 2185 { 2186 struct scsi_transportid_spi *id = 2187 (struct scsi_transportid_spi *)buf; 2188 memset(id, 0, sizeof(*id)); 2189 id->format_protocol = SCSI_PROTO_SPI; 2190 scsi_ulto2b(iid, id->scsi_addr); 2191 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2192 return (sizeof(*id)); 2193 } 2194 } 2195} 2196 2197/* 2198 * Serialize a command that went down the "wrong" side, and so was sent to 2199 * this controller for execution. The logic is a little different than the 2200 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2201 * sent back to the other side, but in the success case, we execute the 2202 * command on this side (XFER mode) or tell the other side to execute it 2203 * (SER_ONLY mode). 2204 */ 2205static void 2206ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2207{ 2208 struct ctl_softc *softc = control_softc; 2209 union ctl_ha_msg msg_info; 2210 struct ctl_port *port; 2211 struct ctl_lun *lun; 2212 const struct ctl_cmd_entry *entry; 2213 uint32_t targ_lun; 2214 2215 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2216 mtx_lock(&softc->ctl_lock); 2217 2218 /* Make sure that we know about this port. */ 2219 port = ctl_io_port(&ctsio->io_hdr); 2220 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2221 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2222 /*retry_count*/ 1); 2223 goto badjuju; 2224 } 2225 2226 /* Make sure that we know about this LUN. */ 2227 if (targ_lun >= CTL_MAX_LUNS || 2228 (lun = softc->ctl_luns[targ_lun]) == NULL) { 2229 mtx_unlock(&softc->ctl_lock); 2230 2231 /* 2232 * The other node would not send this request to us unless 2233 * received announce that we are primary node for this LUN. 2234 * If this LUN does not exist now, it is probably result of 2235 * a race, so respond to initiator in the most opaque way. 2236 */ 2237 ctl_set_busy(ctsio); 2238 goto badjuju; 2239 } 2240 mtx_lock(&lun->lun_lock); 2241 mtx_unlock(&softc->ctl_lock); 2242 2243 /* 2244 * If the LUN is invalid, pretend that it doesn't exist. 2245 * It will go away as soon as all pending I/Os completed. 2246 */ 2247 if (lun->flags & CTL_LUN_DISABLED) { 2248 mtx_unlock(&lun->lun_lock); 2249 ctl_set_busy(ctsio); 2250 goto badjuju; 2251 } 2252 2253 entry = ctl_get_cmd_entry(ctsio, NULL); 2254 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2255 mtx_unlock(&lun->lun_lock); 2256 goto badjuju; 2257 } 2258 2259 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2260 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2261 2262 /* 2263 * Every I/O goes into the OOA queue for a 2264 * particular LUN, and stays there until completion. 2265 */ 2266#ifdef CTL_TIME_IO 2267 if (TAILQ_EMPTY(&lun->ooa_queue)) 2268 lun->idle_time += getsbinuptime() - lun->last_busy; 2269#endif 2270 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2271 2272 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2273 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2274 ooa_links))) { 2275 case CTL_ACTION_BLOCK: 2276 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2277 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2278 blocked_links); 2279 mtx_unlock(&lun->lun_lock); 2280 break; 2281 case CTL_ACTION_PASS: 2282 case CTL_ACTION_SKIP: 2283 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2284 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2285 ctl_enqueue_rtr((union ctl_io *)ctsio); 2286 mtx_unlock(&lun->lun_lock); 2287 } else { 2288 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2289 mtx_unlock(&lun->lun_lock); 2290 2291 /* send msg back to other side */ 2292 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2293 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2294 msg_info.hdr.msg_type = CTL_MSG_R2R; 2295 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2296 sizeof(msg_info.hdr), M_WAITOK); 2297 } 2298 break; 2299 case CTL_ACTION_OVERLAP: 2300 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2301 mtx_unlock(&lun->lun_lock); 2302 ctl_set_overlapped_cmd(ctsio); 2303 goto badjuju; 2304 case CTL_ACTION_OVERLAP_TAG: 2305 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2306 mtx_unlock(&lun->lun_lock); 2307 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2308 goto badjuju; 2309 case CTL_ACTION_ERROR: 2310 default: 2311 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2312 mtx_unlock(&lun->lun_lock); 2313 2314 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2315 /*retry_count*/ 0); 2316badjuju: 2317 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2318 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2319 msg_info.hdr.serializing_sc = NULL; 2320 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2321 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2322 sizeof(msg_info.scsi), M_WAITOK); 2323 ctl_free_io((union ctl_io *)ctsio); 2324 break; 2325 } 2326} 2327 2328/* 2329 * Returns 0 for success, errno for failure. 2330 */ 2331static void 2332ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2333 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2334{ 2335 union ctl_io *io; 2336 2337 mtx_lock(&lun->lun_lock); 2338 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2339 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2340 ooa_links)) { 2341 struct ctl_ooa_entry *entry; 2342 2343 /* 2344 * If we've got more than we can fit, just count the 2345 * remaining entries. 2346 */ 2347 if (*cur_fill_num >= ooa_hdr->alloc_num) 2348 continue; 2349 2350 entry = &kern_entries[*cur_fill_num]; 2351 2352 entry->tag_num = io->scsiio.tag_num; 2353 entry->lun_num = lun->lun; 2354#ifdef CTL_TIME_IO 2355 entry->start_bt = io->io_hdr.start_bt; 2356#endif 2357 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2358 entry->cdb_len = io->scsiio.cdb_len; 2359 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2360 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2361 2362 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2363 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2364 2365 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2366 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2367 2368 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2369 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2370 2371 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2372 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2373 } 2374 mtx_unlock(&lun->lun_lock); 2375} 2376 2377static void * 2378ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str, 2379 size_t error_str_len) 2380{ 2381 void *kptr; 2382 2383 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2384 2385 if (copyin(user_addr, kptr, len) != 0) { 2386 snprintf(error_str, error_str_len, "Error copying %d bytes " 2387 "from user address %p to kernel address %p", len, 2388 user_addr, kptr); 2389 free(kptr, M_CTL); 2390 return (NULL); 2391 } 2392 2393 return (kptr); 2394} 2395 2396static void 2397ctl_free_args(int num_args, struct ctl_be_arg *args) 2398{ 2399 int i; 2400 2401 if (args == NULL) 2402 return; 2403 2404 for (i = 0; i < num_args; i++) { 2405 free(args[i].kname, M_CTL); 2406 free(args[i].kvalue, M_CTL); 2407 } 2408 2409 free(args, M_CTL); 2410} 2411 2412static struct ctl_be_arg * 2413ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2414 char *error_str, size_t error_str_len) 2415{ 2416 struct ctl_be_arg *args; 2417 int i; 2418 2419 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2420 error_str, error_str_len); 2421 2422 if (args == NULL) 2423 goto bailout; 2424 2425 for (i = 0; i < num_args; i++) { 2426 args[i].kname = NULL; 2427 args[i].kvalue = NULL; 2428 } 2429 2430 for (i = 0; i < num_args; i++) { 2431 uint8_t *tmpptr; 2432 2433 if (args[i].namelen == 0) { 2434 snprintf(error_str, error_str_len, "Argument %d " 2435 "name length is zero", i); 2436 goto bailout; 2437 } 2438 2439 args[i].kname = ctl_copyin_alloc(args[i].name, 2440 args[i].namelen, error_str, error_str_len); 2441 if (args[i].kname == NULL) 2442 goto bailout; 2443 2444 if (args[i].kname[args[i].namelen - 1] != '\0') { 2445 snprintf(error_str, error_str_len, "Argument %d " 2446 "name is not NUL-terminated", i); 2447 goto bailout; 2448 } 2449 2450 if (args[i].flags & CTL_BEARG_RD) { 2451 if (args[i].vallen == 0) { 2452 snprintf(error_str, error_str_len, "Argument %d " 2453 "value length is zero", i); 2454 goto bailout; 2455 } 2456 2457 tmpptr = ctl_copyin_alloc(args[i].value, 2458 args[i].vallen, error_str, error_str_len); 2459 if (tmpptr == NULL) 2460 goto bailout; 2461 2462 if ((args[i].flags & CTL_BEARG_ASCII) 2463 && (tmpptr[args[i].vallen - 1] != '\0')) { 2464 snprintf(error_str, error_str_len, "Argument " 2465 "%d value is not NUL-terminated", i); 2466 free(tmpptr, M_CTL); 2467 goto bailout; 2468 } 2469 args[i].kvalue = tmpptr; 2470 } else { 2471 args[i].kvalue = malloc(args[i].vallen, 2472 M_CTL, M_WAITOK | M_ZERO); 2473 } 2474 } 2475 2476 return (args); 2477bailout: 2478 2479 ctl_free_args(num_args, args); 2480 2481 return (NULL); 2482} 2483 2484static void 2485ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2486{ 2487 int i; 2488 2489 for (i = 0; i < num_args; i++) { 2490 if (args[i].flags & CTL_BEARG_WR) 2491 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2492 } 2493} 2494 2495/* 2496 * Escape characters that are illegal or not recommended in XML. 2497 */ 2498int 2499ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2500{ 2501 char *end = str + size; 2502 int retval; 2503 2504 retval = 0; 2505 2506 for (; *str && str < end; str++) { 2507 switch (*str) { 2508 case '&': 2509 retval = sbuf_printf(sb, "&"); 2510 break; 2511 case '>': 2512 retval = sbuf_printf(sb, ">"); 2513 break; 2514 case '<': 2515 retval = sbuf_printf(sb, "<"); 2516 break; 2517 default: 2518 retval = sbuf_putc(sb, *str); 2519 break; 2520 } 2521 2522 if (retval != 0) 2523 break; 2524 2525 } 2526 2527 return (retval); 2528} 2529 2530static void 2531ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2532{ 2533 struct scsi_vpd_id_descriptor *desc; 2534 int i; 2535 2536 if (id == NULL || id->len < 4) 2537 return; 2538 desc = (struct scsi_vpd_id_descriptor *)id->data; 2539 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2540 case SVPD_ID_TYPE_T10: 2541 sbuf_printf(sb, "t10."); 2542 break; 2543 case SVPD_ID_TYPE_EUI64: 2544 sbuf_printf(sb, "eui."); 2545 break; 2546 case SVPD_ID_TYPE_NAA: 2547 sbuf_printf(sb, "naa."); 2548 break; 2549 case SVPD_ID_TYPE_SCSI_NAME: 2550 break; 2551 } 2552 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2553 case SVPD_ID_CODESET_BINARY: 2554 for (i = 0; i < desc->length; i++) 2555 sbuf_printf(sb, "%02x", desc->identifier[i]); 2556 break; 2557 case SVPD_ID_CODESET_ASCII: 2558 sbuf_printf(sb, "%.*s", (int)desc->length, 2559 (char *)desc->identifier); 2560 break; 2561 case SVPD_ID_CODESET_UTF8: 2562 sbuf_printf(sb, "%s", (char *)desc->identifier); 2563 break; 2564 } 2565} 2566 2567static int 2568ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2569 struct thread *td) 2570{ 2571 struct ctl_softc *softc = dev->si_drv1; 2572 struct ctl_lun *lun; 2573 int retval; 2574 2575 retval = 0; 2576 2577 switch (cmd) { 2578 case CTL_IO: 2579 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2580 break; 2581 case CTL_ENABLE_PORT: 2582 case CTL_DISABLE_PORT: 2583 case CTL_SET_PORT_WWNS: { 2584 struct ctl_port *port; 2585 struct ctl_port_entry *entry; 2586 2587 entry = (struct ctl_port_entry *)addr; 2588 2589 mtx_lock(&softc->ctl_lock); 2590 STAILQ_FOREACH(port, &softc->port_list, links) { 2591 int action, done; 2592 2593 if (port->targ_port < softc->port_min || 2594 port->targ_port >= softc->port_max) 2595 continue; 2596 2597 action = 0; 2598 done = 0; 2599 if ((entry->port_type == CTL_PORT_NONE) 2600 && (entry->targ_port == port->targ_port)) { 2601 /* 2602 * If the user only wants to enable or 2603 * disable or set WWNs on a specific port, 2604 * do the operation and we're done. 2605 */ 2606 action = 1; 2607 done = 1; 2608 } else if (entry->port_type & port->port_type) { 2609 /* 2610 * Compare the user's type mask with the 2611 * particular frontend type to see if we 2612 * have a match. 2613 */ 2614 action = 1; 2615 done = 0; 2616 2617 /* 2618 * Make sure the user isn't trying to set 2619 * WWNs on multiple ports at the same time. 2620 */ 2621 if (cmd == CTL_SET_PORT_WWNS) { 2622 printf("%s: Can't set WWNs on " 2623 "multiple ports\n", __func__); 2624 retval = EINVAL; 2625 break; 2626 } 2627 } 2628 if (action == 0) 2629 continue; 2630 2631 /* 2632 * XXX KDM we have to drop the lock here, because 2633 * the online/offline operations can potentially 2634 * block. We need to reference count the frontends 2635 * so they can't go away, 2636 */ 2637 if (cmd == CTL_ENABLE_PORT) { 2638 mtx_unlock(&softc->ctl_lock); 2639 ctl_port_online(port); 2640 mtx_lock(&softc->ctl_lock); 2641 } else if (cmd == CTL_DISABLE_PORT) { 2642 mtx_unlock(&softc->ctl_lock); 2643 ctl_port_offline(port); 2644 mtx_lock(&softc->ctl_lock); 2645 } else if (cmd == CTL_SET_PORT_WWNS) { 2646 ctl_port_set_wwns(port, 2647 (entry->flags & CTL_PORT_WWNN_VALID) ? 2648 1 : 0, entry->wwnn, 2649 (entry->flags & CTL_PORT_WWPN_VALID) ? 2650 1 : 0, entry->wwpn); 2651 } 2652 if (done != 0) 2653 break; 2654 } 2655 mtx_unlock(&softc->ctl_lock); 2656 break; 2657 } 2658 case CTL_GET_OOA: { 2659 struct ctl_ooa *ooa_hdr; 2660 struct ctl_ooa_entry *entries; 2661 uint32_t cur_fill_num; 2662 2663 ooa_hdr = (struct ctl_ooa *)addr; 2664 2665 if ((ooa_hdr->alloc_len == 0) 2666 || (ooa_hdr->alloc_num == 0)) { 2667 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2668 "must be non-zero\n", __func__, 2669 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2670 retval = EINVAL; 2671 break; 2672 } 2673 2674 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2675 sizeof(struct ctl_ooa_entry))) { 2676 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2677 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2678 __func__, ooa_hdr->alloc_len, 2679 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2680 retval = EINVAL; 2681 break; 2682 } 2683 2684 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2685 if (entries == NULL) { 2686 printf("%s: could not allocate %d bytes for OOA " 2687 "dump\n", __func__, ooa_hdr->alloc_len); 2688 retval = ENOMEM; 2689 break; 2690 } 2691 2692 mtx_lock(&softc->ctl_lock); 2693 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 && 2694 (ooa_hdr->lun_num >= CTL_MAX_LUNS || 2695 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) { 2696 mtx_unlock(&softc->ctl_lock); 2697 free(entries, M_CTL); 2698 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2699 __func__, (uintmax_t)ooa_hdr->lun_num); 2700 retval = EINVAL; 2701 break; 2702 } 2703 2704 cur_fill_num = 0; 2705 2706 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2707 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2708 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2709 ooa_hdr, entries); 2710 } 2711 } else { 2712 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2713 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2714 entries); 2715 } 2716 mtx_unlock(&softc->ctl_lock); 2717 2718 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2719 ooa_hdr->fill_len = ooa_hdr->fill_num * 2720 sizeof(struct ctl_ooa_entry); 2721 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2722 if (retval != 0) { 2723 printf("%s: error copying out %d bytes for OOA dump\n", 2724 __func__, ooa_hdr->fill_len); 2725 } 2726 2727 getbinuptime(&ooa_hdr->cur_bt); 2728 2729 if (cur_fill_num > ooa_hdr->alloc_num) { 2730 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2731 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2732 } else { 2733 ooa_hdr->dropped_num = 0; 2734 ooa_hdr->status = CTL_OOA_OK; 2735 } 2736 2737 free(entries, M_CTL); 2738 break; 2739 } 2740 case CTL_DELAY_IO: { 2741 struct ctl_io_delay_info *delay_info; 2742 2743 delay_info = (struct ctl_io_delay_info *)addr; 2744 2745#ifdef CTL_IO_DELAY 2746 mtx_lock(&softc->ctl_lock); 2747 if (delay_info->lun_id >= CTL_MAX_LUNS || 2748 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) { 2749 mtx_unlock(&softc->ctl_lock); 2750 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2751 break; 2752 } 2753 mtx_lock(&lun->lun_lock); 2754 mtx_unlock(&softc->ctl_lock); 2755 delay_info->status = CTL_DELAY_STATUS_OK; 2756 switch (delay_info->delay_type) { 2757 case CTL_DELAY_TYPE_CONT: 2758 case CTL_DELAY_TYPE_ONESHOT: 2759 break; 2760 default: 2761 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE; 2762 break; 2763 } 2764 switch (delay_info->delay_loc) { 2765 case CTL_DELAY_LOC_DATAMOVE: 2766 lun->delay_info.datamove_type = delay_info->delay_type; 2767 lun->delay_info.datamove_delay = delay_info->delay_secs; 2768 break; 2769 case CTL_DELAY_LOC_DONE: 2770 lun->delay_info.done_type = delay_info->delay_type; 2771 lun->delay_info.done_delay = delay_info->delay_secs; 2772 break; 2773 default: 2774 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC; 2775 break; 2776 } 2777 mtx_unlock(&lun->lun_lock); 2778#else 2779 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2780#endif /* CTL_IO_DELAY */ 2781 break; 2782 } 2783 case CTL_GETSTATS: { 2784 struct ctl_stats *stats; 2785 int i; 2786 2787 stats = (struct ctl_stats *)addr; 2788 2789 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2790 stats->alloc_len) { 2791 stats->status = CTL_SS_NEED_MORE_SPACE; 2792 stats->num_luns = softc->num_luns; 2793 break; 2794 } 2795 /* 2796 * XXX KDM no locking here. If the LUN list changes, 2797 * things can blow up. 2798 */ 2799 i = 0; 2800 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2801 retval = copyout(&lun->stats, &stats->lun_stats[i++], 2802 sizeof(lun->stats)); 2803 if (retval != 0) 2804 break; 2805 } 2806 stats->num_luns = softc->num_luns; 2807 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2808 softc->num_luns; 2809 stats->status = CTL_SS_OK; 2810#ifdef CTL_TIME_IO 2811 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2812#else 2813 stats->flags = CTL_STATS_FLAG_NONE; 2814#endif 2815 getnanouptime(&stats->timestamp); 2816 break; 2817 } 2818 case CTL_ERROR_INJECT: { 2819 struct ctl_error_desc *err_desc, *new_err_desc; 2820 2821 err_desc = (struct ctl_error_desc *)addr; 2822 2823 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2824 M_WAITOK | M_ZERO); 2825 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2826 2827 mtx_lock(&softc->ctl_lock); 2828 if (err_desc->lun_id >= CTL_MAX_LUNS || 2829 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) { 2830 mtx_unlock(&softc->ctl_lock); 2831 free(new_err_desc, M_CTL); 2832 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2833 __func__, (uintmax_t)err_desc->lun_id); 2834 retval = EINVAL; 2835 break; 2836 } 2837 mtx_lock(&lun->lun_lock); 2838 mtx_unlock(&softc->ctl_lock); 2839 2840 /* 2841 * We could do some checking here to verify the validity 2842 * of the request, but given the complexity of error 2843 * injection requests, the checking logic would be fairly 2844 * complex. 2845 * 2846 * For now, if the request is invalid, it just won't get 2847 * executed and might get deleted. 2848 */ 2849 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2850 2851 /* 2852 * XXX KDM check to make sure the serial number is unique, 2853 * in case we somehow manage to wrap. That shouldn't 2854 * happen for a very long time, but it's the right thing to 2855 * do. 2856 */ 2857 new_err_desc->serial = lun->error_serial; 2858 err_desc->serial = lun->error_serial; 2859 lun->error_serial++; 2860 2861 mtx_unlock(&lun->lun_lock); 2862 break; 2863 } 2864 case CTL_ERROR_INJECT_DELETE: { 2865 struct ctl_error_desc *delete_desc, *desc, *desc2; 2866 int delete_done; 2867 2868 delete_desc = (struct ctl_error_desc *)addr; 2869 delete_done = 0; 2870 2871 mtx_lock(&softc->ctl_lock); 2872 if (delete_desc->lun_id >= CTL_MAX_LUNS || 2873 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) { 2874 mtx_unlock(&softc->ctl_lock); 2875 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2876 __func__, (uintmax_t)delete_desc->lun_id); 2877 retval = EINVAL; 2878 break; 2879 } 2880 mtx_lock(&lun->lun_lock); 2881 mtx_unlock(&softc->ctl_lock); 2882 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2883 if (desc->serial != delete_desc->serial) 2884 continue; 2885 2886 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2887 links); 2888 free(desc, M_CTL); 2889 delete_done = 1; 2890 } 2891 mtx_unlock(&lun->lun_lock); 2892 if (delete_done == 0) { 2893 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2894 "error serial %ju on LUN %u\n", __func__, 2895 delete_desc->serial, delete_desc->lun_id); 2896 retval = EINVAL; 2897 break; 2898 } 2899 break; 2900 } 2901 case CTL_DUMP_STRUCTS: { 2902 int j, k; 2903 struct ctl_port *port; 2904 struct ctl_frontend *fe; 2905 2906 mtx_lock(&softc->ctl_lock); 2907 printf("CTL Persistent Reservation information start:\n"); 2908 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2909 mtx_lock(&lun->lun_lock); 2910 if ((lun->flags & CTL_LUN_DISABLED) != 0) { 2911 mtx_unlock(&lun->lun_lock); 2912 continue; 2913 } 2914 2915 for (j = 0; j < CTL_MAX_PORTS; j++) { 2916 if (lun->pr_keys[j] == NULL) 2917 continue; 2918 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2919 if (lun->pr_keys[j][k] == 0) 2920 continue; 2921 printf(" LUN %ju port %d iid %d key " 2922 "%#jx\n", lun->lun, j, k, 2923 (uintmax_t)lun->pr_keys[j][k]); 2924 } 2925 } 2926 mtx_unlock(&lun->lun_lock); 2927 } 2928 printf("CTL Persistent Reservation information end\n"); 2929 printf("CTL Ports:\n"); 2930 STAILQ_FOREACH(port, &softc->port_list, links) { 2931 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2932 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2933 port->frontend->name, port->port_type, 2934 port->physical_port, port->virtual_port, 2935 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2936 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2937 if (port->wwpn_iid[j].in_use == 0 && 2938 port->wwpn_iid[j].wwpn == 0 && 2939 port->wwpn_iid[j].name == NULL) 2940 continue; 2941 2942 printf(" iid %u use %d WWPN %#jx '%s'\n", 2943 j, port->wwpn_iid[j].in_use, 2944 (uintmax_t)port->wwpn_iid[j].wwpn, 2945 port->wwpn_iid[j].name); 2946 } 2947 } 2948 printf("CTL Port information end\n"); 2949 mtx_unlock(&softc->ctl_lock); 2950 /* 2951 * XXX KDM calling this without a lock. We'd likely want 2952 * to drop the lock before calling the frontend's dump 2953 * routine anyway. 2954 */ 2955 printf("CTL Frontends:\n"); 2956 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2957 printf(" Frontend '%s'\n", fe->name); 2958 if (fe->fe_dump != NULL) 2959 fe->fe_dump(); 2960 } 2961 printf("CTL Frontend information end\n"); 2962 break; 2963 } 2964 case CTL_LUN_REQ: { 2965 struct ctl_lun_req *lun_req; 2966 struct ctl_backend_driver *backend; 2967 2968 lun_req = (struct ctl_lun_req *)addr; 2969 2970 backend = ctl_backend_find(lun_req->backend); 2971 if (backend == NULL) { 2972 lun_req->status = CTL_LUN_ERROR; 2973 snprintf(lun_req->error_str, 2974 sizeof(lun_req->error_str), 2975 "Backend \"%s\" not found.", 2976 lun_req->backend); 2977 break; 2978 } 2979 if (lun_req->num_be_args > 0) { 2980 lun_req->kern_be_args = ctl_copyin_args( 2981 lun_req->num_be_args, 2982 lun_req->be_args, 2983 lun_req->error_str, 2984 sizeof(lun_req->error_str)); 2985 if (lun_req->kern_be_args == NULL) { 2986 lun_req->status = CTL_LUN_ERROR; 2987 break; 2988 } 2989 } 2990 2991 retval = backend->ioctl(dev, cmd, addr, flag, td); 2992 2993 if (lun_req->num_be_args > 0) { 2994 ctl_copyout_args(lun_req->num_be_args, 2995 lun_req->kern_be_args); 2996 ctl_free_args(lun_req->num_be_args, 2997 lun_req->kern_be_args); 2998 } 2999 break; 3000 } 3001 case CTL_LUN_LIST: { 3002 struct sbuf *sb; 3003 struct ctl_lun_list *list; 3004 struct ctl_option *opt; 3005 3006 list = (struct ctl_lun_list *)addr; 3007 3008 /* 3009 * Allocate a fixed length sbuf here, based on the length 3010 * of the user's buffer. We could allocate an auto-extending 3011 * buffer, and then tell the user how much larger our 3012 * amount of data is than his buffer, but that presents 3013 * some problems: 3014 * 3015 * 1. The sbuf(9) routines use a blocking malloc, and so 3016 * we can't hold a lock while calling them with an 3017 * auto-extending buffer. 3018 * 3019 * 2. There is not currently a LUN reference counting 3020 * mechanism, outside of outstanding transactions on 3021 * the LUN's OOA queue. So a LUN could go away on us 3022 * while we're getting the LUN number, backend-specific 3023 * information, etc. Thus, given the way things 3024 * currently work, we need to hold the CTL lock while 3025 * grabbing LUN information. 3026 * 3027 * So, from the user's standpoint, the best thing to do is 3028 * allocate what he thinks is a reasonable buffer length, 3029 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3030 * double the buffer length and try again. (And repeat 3031 * that until he succeeds.) 3032 */ 3033 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3034 if (sb == NULL) { 3035 list->status = CTL_LUN_LIST_ERROR; 3036 snprintf(list->error_str, sizeof(list->error_str), 3037 "Unable to allocate %d bytes for LUN list", 3038 list->alloc_len); 3039 break; 3040 } 3041 3042 sbuf_printf(sb, "<ctllunlist>\n"); 3043 3044 mtx_lock(&softc->ctl_lock); 3045 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3046 mtx_lock(&lun->lun_lock); 3047 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3048 (uintmax_t)lun->lun); 3049 3050 /* 3051 * Bail out as soon as we see that we've overfilled 3052 * the buffer. 3053 */ 3054 if (retval != 0) 3055 break; 3056 3057 retval = sbuf_printf(sb, "\t<backend_type>%s" 3058 "</backend_type>\n", 3059 (lun->backend == NULL) ? "none" : 3060 lun->backend->name); 3061 3062 if (retval != 0) 3063 break; 3064 3065 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3066 lun->be_lun->lun_type); 3067 3068 if (retval != 0) 3069 break; 3070 3071 if (lun->backend == NULL) { 3072 retval = sbuf_printf(sb, "</lun>\n"); 3073 if (retval != 0) 3074 break; 3075 continue; 3076 } 3077 3078 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3079 (lun->be_lun->maxlba > 0) ? 3080 lun->be_lun->maxlba + 1 : 0); 3081 3082 if (retval != 0) 3083 break; 3084 3085 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3086 lun->be_lun->blocksize); 3087 3088 if (retval != 0) 3089 break; 3090 3091 retval = sbuf_printf(sb, "\t<serial_number>"); 3092 3093 if (retval != 0) 3094 break; 3095 3096 retval = ctl_sbuf_printf_esc(sb, 3097 lun->be_lun->serial_num, 3098 sizeof(lun->be_lun->serial_num)); 3099 3100 if (retval != 0) 3101 break; 3102 3103 retval = sbuf_printf(sb, "</serial_number>\n"); 3104 3105 if (retval != 0) 3106 break; 3107 3108 retval = sbuf_printf(sb, "\t<device_id>"); 3109 3110 if (retval != 0) 3111 break; 3112 3113 retval = ctl_sbuf_printf_esc(sb, 3114 lun->be_lun->device_id, 3115 sizeof(lun->be_lun->device_id)); 3116 3117 if (retval != 0) 3118 break; 3119 3120 retval = sbuf_printf(sb, "</device_id>\n"); 3121 3122 if (retval != 0) 3123 break; 3124 3125 if (lun->backend->lun_info != NULL) { 3126 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3127 if (retval != 0) 3128 break; 3129 } 3130 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3131 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3132 opt->name, opt->value, opt->name); 3133 if (retval != 0) 3134 break; 3135 } 3136 3137 retval = sbuf_printf(sb, "</lun>\n"); 3138 3139 if (retval != 0) 3140 break; 3141 mtx_unlock(&lun->lun_lock); 3142 } 3143 if (lun != NULL) 3144 mtx_unlock(&lun->lun_lock); 3145 mtx_unlock(&softc->ctl_lock); 3146 3147 if ((retval != 0) 3148 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3149 retval = 0; 3150 sbuf_delete(sb); 3151 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3152 snprintf(list->error_str, sizeof(list->error_str), 3153 "Out of space, %d bytes is too small", 3154 list->alloc_len); 3155 break; 3156 } 3157 3158 sbuf_finish(sb); 3159 3160 retval = copyout(sbuf_data(sb), list->lun_xml, 3161 sbuf_len(sb) + 1); 3162 3163 list->fill_len = sbuf_len(sb) + 1; 3164 list->status = CTL_LUN_LIST_OK; 3165 sbuf_delete(sb); 3166 break; 3167 } 3168 case CTL_ISCSI: { 3169 struct ctl_iscsi *ci; 3170 struct ctl_frontend *fe; 3171 3172 ci = (struct ctl_iscsi *)addr; 3173 3174 fe = ctl_frontend_find("iscsi"); 3175 if (fe == NULL) { 3176 ci->status = CTL_ISCSI_ERROR; 3177 snprintf(ci->error_str, sizeof(ci->error_str), 3178 "Frontend \"iscsi\" not found."); 3179 break; 3180 } 3181 3182 retval = fe->ioctl(dev, cmd, addr, flag, td); 3183 break; 3184 } 3185 case CTL_PORT_REQ: { 3186 struct ctl_req *req; 3187 struct ctl_frontend *fe; 3188 3189 req = (struct ctl_req *)addr; 3190 3191 fe = ctl_frontend_find(req->driver); 3192 if (fe == NULL) { 3193 req->status = CTL_LUN_ERROR; 3194 snprintf(req->error_str, sizeof(req->error_str), 3195 "Frontend \"%s\" not found.", req->driver); 3196 break; 3197 } 3198 if (req->num_args > 0) { 3199 req->kern_args = ctl_copyin_args(req->num_args, 3200 req->args, req->error_str, sizeof(req->error_str)); 3201 if (req->kern_args == NULL) { 3202 req->status = CTL_LUN_ERROR; 3203 break; 3204 } 3205 } 3206 3207 if (fe->ioctl) 3208 retval = fe->ioctl(dev, cmd, addr, flag, td); 3209 else 3210 retval = ENODEV; 3211 3212 if (req->num_args > 0) { 3213 ctl_copyout_args(req->num_args, req->kern_args); 3214 ctl_free_args(req->num_args, req->kern_args); 3215 } 3216 break; 3217 } 3218 case CTL_PORT_LIST: { 3219 struct sbuf *sb; 3220 struct ctl_port *port; 3221 struct ctl_lun_list *list; 3222 struct ctl_option *opt; 3223 int j; 3224 uint32_t plun; 3225 3226 list = (struct ctl_lun_list *)addr; 3227 3228 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3229 if (sb == NULL) { 3230 list->status = CTL_LUN_LIST_ERROR; 3231 snprintf(list->error_str, sizeof(list->error_str), 3232 "Unable to allocate %d bytes for LUN list", 3233 list->alloc_len); 3234 break; 3235 } 3236 3237 sbuf_printf(sb, "<ctlportlist>\n"); 3238 3239 mtx_lock(&softc->ctl_lock); 3240 STAILQ_FOREACH(port, &softc->port_list, links) { 3241 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3242 (uintmax_t)port->targ_port); 3243 3244 /* 3245 * Bail out as soon as we see that we've overfilled 3246 * the buffer. 3247 */ 3248 if (retval != 0) 3249 break; 3250 3251 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3252 "</frontend_type>\n", port->frontend->name); 3253 if (retval != 0) 3254 break; 3255 3256 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3257 port->port_type); 3258 if (retval != 0) 3259 break; 3260 3261 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3262 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3263 if (retval != 0) 3264 break; 3265 3266 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3267 port->port_name); 3268 if (retval != 0) 3269 break; 3270 3271 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3272 port->physical_port); 3273 if (retval != 0) 3274 break; 3275 3276 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3277 port->virtual_port); 3278 if (retval != 0) 3279 break; 3280 3281 if (port->target_devid != NULL) { 3282 sbuf_printf(sb, "\t<target>"); 3283 ctl_id_sbuf(port->target_devid, sb); 3284 sbuf_printf(sb, "</target>\n"); 3285 } 3286 3287 if (port->port_devid != NULL) { 3288 sbuf_printf(sb, "\t<port>"); 3289 ctl_id_sbuf(port->port_devid, sb); 3290 sbuf_printf(sb, "</port>\n"); 3291 } 3292 3293 if (port->port_info != NULL) { 3294 retval = port->port_info(port->onoff_arg, sb); 3295 if (retval != 0) 3296 break; 3297 } 3298 STAILQ_FOREACH(opt, &port->options, links) { 3299 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3300 opt->name, opt->value, opt->name); 3301 if (retval != 0) 3302 break; 3303 } 3304 3305 if (port->lun_map != NULL) { 3306 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3307 for (j = 0; j < port->lun_map_size; j++) { 3308 plun = ctl_lun_map_from_port(port, j); 3309 if (plun == UINT32_MAX) 3310 continue; 3311 sbuf_printf(sb, 3312 "\t<lun id=\"%u\">%u</lun>\n", 3313 j, plun); 3314 } 3315 } 3316 3317 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3318 if (port->wwpn_iid[j].in_use == 0 || 3319 (port->wwpn_iid[j].wwpn == 0 && 3320 port->wwpn_iid[j].name == NULL)) 3321 continue; 3322 3323 if (port->wwpn_iid[j].name != NULL) 3324 retval = sbuf_printf(sb, 3325 "\t<initiator id=\"%u\">%s</initiator>\n", 3326 j, port->wwpn_iid[j].name); 3327 else 3328 retval = sbuf_printf(sb, 3329 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3330 j, port->wwpn_iid[j].wwpn); 3331 if (retval != 0) 3332 break; 3333 } 3334 if (retval != 0) 3335 break; 3336 3337 retval = sbuf_printf(sb, "</targ_port>\n"); 3338 if (retval != 0) 3339 break; 3340 } 3341 mtx_unlock(&softc->ctl_lock); 3342 3343 if ((retval != 0) 3344 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3345 retval = 0; 3346 sbuf_delete(sb); 3347 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3348 snprintf(list->error_str, sizeof(list->error_str), 3349 "Out of space, %d bytes is too small", 3350 list->alloc_len); 3351 break; 3352 } 3353 3354 sbuf_finish(sb); 3355 3356 retval = copyout(sbuf_data(sb), list->lun_xml, 3357 sbuf_len(sb) + 1); 3358 3359 list->fill_len = sbuf_len(sb) + 1; 3360 list->status = CTL_LUN_LIST_OK; 3361 sbuf_delete(sb); 3362 break; 3363 } 3364 case CTL_LUN_MAP: { 3365 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3366 struct ctl_port *port; 3367 3368 mtx_lock(&softc->ctl_lock); 3369 if (lm->port < softc->port_min || 3370 lm->port >= softc->port_max || 3371 (port = softc->ctl_ports[lm->port]) == NULL) { 3372 mtx_unlock(&softc->ctl_lock); 3373 return (ENXIO); 3374 } 3375 if (port->status & CTL_PORT_STATUS_ONLINE) { 3376 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3377 if (ctl_lun_map_to_port(port, lun->lun) == 3378 UINT32_MAX) 3379 continue; 3380 mtx_lock(&lun->lun_lock); 3381 ctl_est_ua_port(lun, lm->port, -1, 3382 CTL_UA_LUN_CHANGE); 3383 mtx_unlock(&lun->lun_lock); 3384 } 3385 } 3386 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3387 if (lm->plun != UINT32_MAX) { 3388 if (lm->lun == UINT32_MAX) 3389 retval = ctl_lun_map_unset(port, lm->plun); 3390 else if (lm->lun < CTL_MAX_LUNS && 3391 softc->ctl_luns[lm->lun] != NULL) 3392 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3393 else 3394 return (ENXIO); 3395 } else { 3396 if (lm->lun == UINT32_MAX) 3397 retval = ctl_lun_map_deinit(port); 3398 else 3399 retval = ctl_lun_map_init(port); 3400 } 3401 if (port->status & CTL_PORT_STATUS_ONLINE) 3402 ctl_isc_announce_port(port); 3403 break; 3404 } 3405 default: { 3406 /* XXX KDM should we fix this? */ 3407#if 0 3408 struct ctl_backend_driver *backend; 3409 unsigned int type; 3410 int found; 3411 3412 found = 0; 3413 3414 /* 3415 * We encode the backend type as the ioctl type for backend 3416 * ioctls. So parse it out here, and then search for a 3417 * backend of this type. 3418 */ 3419 type = _IOC_TYPE(cmd); 3420 3421 STAILQ_FOREACH(backend, &softc->be_list, links) { 3422 if (backend->type == type) { 3423 found = 1; 3424 break; 3425 } 3426 } 3427 if (found == 0) { 3428 printf("ctl: unknown ioctl command %#lx or backend " 3429 "%d\n", cmd, type); 3430 retval = EINVAL; 3431 break; 3432 } 3433 retval = backend->ioctl(dev, cmd, addr, flag, td); 3434#endif 3435 retval = ENOTTY; 3436 break; 3437 } 3438 } 3439 return (retval); 3440} 3441 3442uint32_t 3443ctl_get_initindex(struct ctl_nexus *nexus) 3444{ 3445 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3446} 3447 3448int 3449ctl_lun_map_init(struct ctl_port *port) 3450{ 3451 struct ctl_softc *softc = port->ctl_softc; 3452 struct ctl_lun *lun; 3453 int size = ctl_lun_map_size; 3454 uint32_t i; 3455 3456 if (port->lun_map == NULL || port->lun_map_size < size) { 3457 port->lun_map_size = 0; 3458 free(port->lun_map, M_CTL); 3459 port->lun_map = malloc(size * sizeof(uint32_t), 3460 M_CTL, M_NOWAIT); 3461 } 3462 if (port->lun_map == NULL) 3463 return (ENOMEM); 3464 for (i = 0; i < size; i++) 3465 port->lun_map[i] = UINT32_MAX; 3466 port->lun_map_size = size; 3467 if (port->status & CTL_PORT_STATUS_ONLINE) { 3468 if (port->lun_disable != NULL) { 3469 STAILQ_FOREACH(lun, &softc->lun_list, links) 3470 port->lun_disable(port->targ_lun_arg, lun->lun); 3471 } 3472 ctl_isc_announce_port(port); 3473 } 3474 return (0); 3475} 3476 3477int 3478ctl_lun_map_deinit(struct ctl_port *port) 3479{ 3480 struct ctl_softc *softc = port->ctl_softc; 3481 struct ctl_lun *lun; 3482 3483 if (port->lun_map == NULL) 3484 return (0); 3485 port->lun_map_size = 0; 3486 free(port->lun_map, M_CTL); 3487 port->lun_map = NULL; 3488 if (port->status & CTL_PORT_STATUS_ONLINE) { 3489 if (port->lun_enable != NULL) { 3490 STAILQ_FOREACH(lun, &softc->lun_list, links) 3491 port->lun_enable(port->targ_lun_arg, lun->lun); 3492 } 3493 ctl_isc_announce_port(port); 3494 } 3495 return (0); 3496} 3497 3498int 3499ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3500{ 3501 int status; 3502 uint32_t old; 3503 3504 if (port->lun_map == NULL) { 3505 status = ctl_lun_map_init(port); 3506 if (status != 0) 3507 return (status); 3508 } 3509 if (plun >= port->lun_map_size) 3510 return (EINVAL); 3511 old = port->lun_map[plun]; 3512 port->lun_map[plun] = glun; 3513 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) { 3514 if (port->lun_enable != NULL) 3515 port->lun_enable(port->targ_lun_arg, plun); 3516 ctl_isc_announce_port(port); 3517 } 3518 return (0); 3519} 3520 3521int 3522ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3523{ 3524 uint32_t old; 3525 3526 if (port->lun_map == NULL || plun >= port->lun_map_size) 3527 return (0); 3528 old = port->lun_map[plun]; 3529 port->lun_map[plun] = UINT32_MAX; 3530 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) { 3531 if (port->lun_disable != NULL) 3532 port->lun_disable(port->targ_lun_arg, plun); 3533 ctl_isc_announce_port(port); 3534 } 3535 return (0); 3536} 3537 3538uint32_t 3539ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3540{ 3541 3542 if (port == NULL) 3543 return (UINT32_MAX); 3544 if (port->lun_map == NULL) 3545 return (lun_id); 3546 if (lun_id > port->lun_map_size) 3547 return (UINT32_MAX); 3548 return (port->lun_map[lun_id]); 3549} 3550 3551uint32_t 3552ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3553{ 3554 uint32_t i; 3555 3556 if (port == NULL) 3557 return (UINT32_MAX); 3558 if (port->lun_map == NULL) 3559 return (lun_id); 3560 for (i = 0; i < port->lun_map_size; i++) { 3561 if (port->lun_map[i] == lun_id) 3562 return (i); 3563 } 3564 return (UINT32_MAX); 3565} 3566 3567uint32_t 3568ctl_decode_lun(uint64_t encoded) 3569{ 3570 uint8_t lun[8]; 3571 uint32_t result = 0xffffffff; 3572 3573 be64enc(lun, encoded); 3574 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) { 3575 case RPL_LUNDATA_ATYP_PERIPH: 3576 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 && 3577 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0) 3578 result = lun[1]; 3579 break; 3580 case RPL_LUNDATA_ATYP_FLAT: 3581 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 && 3582 lun[6] == 0 && lun[7] == 0) 3583 result = ((lun[0] & 0x3f) << 8) + lun[1]; 3584 break; 3585 case RPL_LUNDATA_ATYP_EXTLUN: 3586 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) { 3587 case 0x02: 3588 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) { 3589 case 0x00: 3590 result = lun[1]; 3591 break; 3592 case 0x10: 3593 result = (lun[1] << 16) + (lun[2] << 8) + 3594 lun[3]; 3595 break; 3596 case 0x20: 3597 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0) 3598 result = (lun[2] << 24) + 3599 (lun[3] << 16) + (lun[4] << 8) + 3600 lun[5]; 3601 break; 3602 } 3603 break; 3604 case RPL_LUNDATA_EXT_EAM_NOT_SPEC: 3605 result = 0xffffffff; 3606 break; 3607 } 3608 break; 3609 } 3610 return (result); 3611} 3612 3613uint64_t 3614ctl_encode_lun(uint32_t decoded) 3615{ 3616 uint64_t l = decoded; 3617 3618 if (l <= 0xff) 3619 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48)); 3620 if (l <= 0x3fff) 3621 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48)); 3622 if (l <= 0xffffff) 3623 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) | 3624 (l << 32)); 3625 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16)); 3626} 3627 3628static struct ctl_port * 3629ctl_io_port(struct ctl_io_hdr *io_hdr) 3630{ 3631 3632 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3633} 3634 3635int 3636ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3637{ 3638 int i; 3639 3640 for (i = first; i < last; i++) { 3641 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3642 return (i); 3643 } 3644 return (-1); 3645} 3646 3647int 3648ctl_set_mask(uint32_t *mask, uint32_t bit) 3649{ 3650 uint32_t chunk, piece; 3651 3652 chunk = bit >> 5; 3653 piece = bit % (sizeof(uint32_t) * 8); 3654 3655 if ((mask[chunk] & (1 << piece)) != 0) 3656 return (-1); 3657 else 3658 mask[chunk] |= (1 << piece); 3659 3660 return (0); 3661} 3662 3663int 3664ctl_clear_mask(uint32_t *mask, uint32_t bit) 3665{ 3666 uint32_t chunk, piece; 3667 3668 chunk = bit >> 5; 3669 piece = bit % (sizeof(uint32_t) * 8); 3670 3671 if ((mask[chunk] & (1 << piece)) == 0) 3672 return (-1); 3673 else 3674 mask[chunk] &= ~(1 << piece); 3675 3676 return (0); 3677} 3678 3679int 3680ctl_is_set(uint32_t *mask, uint32_t bit) 3681{ 3682 uint32_t chunk, piece; 3683 3684 chunk = bit >> 5; 3685 piece = bit % (sizeof(uint32_t) * 8); 3686 3687 if ((mask[chunk] & (1 << piece)) == 0) 3688 return (0); 3689 else 3690 return (1); 3691} 3692 3693static uint64_t 3694ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3695{ 3696 uint64_t *t; 3697 3698 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3699 if (t == NULL) 3700 return (0); 3701 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3702} 3703 3704static void 3705ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3706{ 3707 uint64_t *t; 3708 3709 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3710 if (t == NULL) 3711 return; 3712 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3713} 3714 3715static void 3716ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3717{ 3718 uint64_t *p; 3719 u_int i; 3720 3721 i = residx/CTL_MAX_INIT_PER_PORT; 3722 if (lun->pr_keys[i] != NULL) 3723 return; 3724 mtx_unlock(&lun->lun_lock); 3725 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3726 M_WAITOK | M_ZERO); 3727 mtx_lock(&lun->lun_lock); 3728 if (lun->pr_keys[i] == NULL) 3729 lun->pr_keys[i] = p; 3730 else 3731 free(p, M_CTL); 3732} 3733 3734static void 3735ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3736{ 3737 uint64_t *t; 3738 3739 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3740 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3741 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3742} 3743 3744/* 3745 * ctl_softc, pool_name, total_ctl_io are passed in. 3746 * npool is passed out. 3747 */ 3748int 3749ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3750 uint32_t total_ctl_io, void **npool) 3751{ 3752#ifdef IO_POOLS 3753 struct ctl_io_pool *pool; 3754 3755 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3756 M_NOWAIT | M_ZERO); 3757 if (pool == NULL) 3758 return (ENOMEM); 3759 3760 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3761 pool->ctl_softc = ctl_softc; 3762 pool->zone = uma_zsecond_create(pool->name, NULL, 3763 NULL, NULL, NULL, ctl_softc->io_zone); 3764 /* uma_prealloc(pool->zone, total_ctl_io); */ 3765 3766 *npool = pool; 3767#else 3768 *npool = ctl_softc->io_zone; 3769#endif 3770 return (0); 3771} 3772 3773void 3774ctl_pool_free(struct ctl_io_pool *pool) 3775{ 3776 3777 if (pool == NULL) 3778 return; 3779 3780#ifdef IO_POOLS 3781 uma_zdestroy(pool->zone); 3782 free(pool, M_CTL); 3783#endif 3784} 3785 3786union ctl_io * 3787ctl_alloc_io(void *pool_ref) 3788{ 3789 union ctl_io *io; 3790#ifdef IO_POOLS 3791 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3792 3793 io = uma_zalloc(pool->zone, M_WAITOK); 3794#else 3795 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3796#endif 3797 if (io != NULL) 3798 io->io_hdr.pool = pool_ref; 3799 return (io); 3800} 3801 3802union ctl_io * 3803ctl_alloc_io_nowait(void *pool_ref) 3804{ 3805 union ctl_io *io; 3806#ifdef IO_POOLS 3807 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3808 3809 io = uma_zalloc(pool->zone, M_NOWAIT); 3810#else 3811 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3812#endif 3813 if (io != NULL) 3814 io->io_hdr.pool = pool_ref; 3815 return (io); 3816} 3817 3818void 3819ctl_free_io(union ctl_io *io) 3820{ 3821#ifdef IO_POOLS 3822 struct ctl_io_pool *pool; 3823#endif 3824 3825 if (io == NULL) 3826 return; 3827 3828#ifdef IO_POOLS 3829 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3830 uma_zfree(pool->zone, io); 3831#else 3832 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3833#endif 3834} 3835 3836void 3837ctl_zero_io(union ctl_io *io) 3838{ 3839 void *pool_ref; 3840 3841 if (io == NULL) 3842 return; 3843 3844 /* 3845 * May need to preserve linked list pointers at some point too. 3846 */ 3847 pool_ref = io->io_hdr.pool; 3848 memset(io, 0, sizeof(*io)); 3849 io->io_hdr.pool = pool_ref; 3850} 3851 3852int 3853ctl_expand_number(const char *buf, uint64_t *num) 3854{ 3855 char *endptr; 3856 uint64_t number; 3857 unsigned shift; 3858 3859 number = strtoq(buf, &endptr, 0); 3860 3861 switch (tolower((unsigned char)*endptr)) { 3862 case 'e': 3863 shift = 60; 3864 break; 3865 case 'p': 3866 shift = 50; 3867 break; 3868 case 't': 3869 shift = 40; 3870 break; 3871 case 'g': 3872 shift = 30; 3873 break; 3874 case 'm': 3875 shift = 20; 3876 break; 3877 case 'k': 3878 shift = 10; 3879 break; 3880 case 'b': 3881 case '\0': /* No unit. */ 3882 *num = number; 3883 return (0); 3884 default: 3885 /* Unrecognized unit. */ 3886 return (-1); 3887 } 3888 3889 if ((number << shift) >> shift != number) { 3890 /* Overflow */ 3891 return (-1); 3892 } 3893 *num = number << shift; 3894 return (0); 3895} 3896 3897 3898/* 3899 * This routine could be used in the future to load default and/or saved 3900 * mode page parameters for a particuar lun. 3901 */ 3902static int 3903ctl_init_page_index(struct ctl_lun *lun) 3904{ 3905 int i, page_code; 3906 struct ctl_page_index *page_index; 3907 const char *value; 3908 uint64_t ival; 3909 3910 memcpy(&lun->mode_pages.index, page_index_template, 3911 sizeof(page_index_template)); 3912 3913 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3914 3915 page_index = &lun->mode_pages.index[i]; 3916 if (lun->be_lun->lun_type == T_DIRECT && 3917 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3918 continue; 3919 if (lun->be_lun->lun_type == T_PROCESSOR && 3920 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3921 continue; 3922 if (lun->be_lun->lun_type == T_CDROM && 3923 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3924 continue; 3925 3926 page_code = page_index->page_code & SMPH_PC_MASK; 3927 switch (page_code) { 3928 case SMS_RW_ERROR_RECOVERY_PAGE: { 3929 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3930 ("subpage %#x for page %#x is incorrect!", 3931 page_index->subpage, page_code)); 3932 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3933 &rw_er_page_default, 3934 sizeof(rw_er_page_default)); 3935 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3936 &rw_er_page_changeable, 3937 sizeof(rw_er_page_changeable)); 3938 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3939 &rw_er_page_default, 3940 sizeof(rw_er_page_default)); 3941 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3942 &rw_er_page_default, 3943 sizeof(rw_er_page_default)); 3944 page_index->page_data = 3945 (uint8_t *)lun->mode_pages.rw_er_page; 3946 break; 3947 } 3948 case SMS_FORMAT_DEVICE_PAGE: { 3949 struct scsi_format_page *format_page; 3950 3951 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 3952 ("subpage %#x for page %#x is incorrect!", 3953 page_index->subpage, page_code)); 3954 3955 /* 3956 * Sectors per track are set above. Bytes per 3957 * sector need to be set here on a per-LUN basis. 3958 */ 3959 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3960 &format_page_default, 3961 sizeof(format_page_default)); 3962 memcpy(&lun->mode_pages.format_page[ 3963 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3964 sizeof(format_page_changeable)); 3965 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3966 &format_page_default, 3967 sizeof(format_page_default)); 3968 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3969 &format_page_default, 3970 sizeof(format_page_default)); 3971 3972 format_page = &lun->mode_pages.format_page[ 3973 CTL_PAGE_CURRENT]; 3974 scsi_ulto2b(lun->be_lun->blocksize, 3975 format_page->bytes_per_sector); 3976 3977 format_page = &lun->mode_pages.format_page[ 3978 CTL_PAGE_DEFAULT]; 3979 scsi_ulto2b(lun->be_lun->blocksize, 3980 format_page->bytes_per_sector); 3981 3982 format_page = &lun->mode_pages.format_page[ 3983 CTL_PAGE_SAVED]; 3984 scsi_ulto2b(lun->be_lun->blocksize, 3985 format_page->bytes_per_sector); 3986 3987 page_index->page_data = 3988 (uint8_t *)lun->mode_pages.format_page; 3989 break; 3990 } 3991 case SMS_RIGID_DISK_PAGE: { 3992 struct scsi_rigid_disk_page *rigid_disk_page; 3993 uint32_t sectors_per_cylinder; 3994 uint64_t cylinders; 3995#ifndef __XSCALE__ 3996 int shift; 3997#endif /* !__XSCALE__ */ 3998 3999 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4000 ("subpage %#x for page %#x is incorrect!", 4001 page_index->subpage, page_code)); 4002 4003 /* 4004 * Rotation rate and sectors per track are set 4005 * above. We calculate the cylinders here based on 4006 * capacity. Due to the number of heads and 4007 * sectors per track we're using, smaller arrays 4008 * may turn out to have 0 cylinders. Linux and 4009 * FreeBSD don't pay attention to these mode pages 4010 * to figure out capacity, but Solaris does. It 4011 * seems to deal with 0 cylinders just fine, and 4012 * works out a fake geometry based on the capacity. 4013 */ 4014 memcpy(&lun->mode_pages.rigid_disk_page[ 4015 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4016 sizeof(rigid_disk_page_default)); 4017 memcpy(&lun->mode_pages.rigid_disk_page[ 4018 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4019 sizeof(rigid_disk_page_changeable)); 4020 4021 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4022 CTL_DEFAULT_HEADS; 4023 4024 /* 4025 * The divide method here will be more accurate, 4026 * probably, but results in floating point being 4027 * used in the kernel on i386 (__udivdi3()). On the 4028 * XScale, though, __udivdi3() is implemented in 4029 * software. 4030 * 4031 * The shift method for cylinder calculation is 4032 * accurate if sectors_per_cylinder is a power of 4033 * 2. Otherwise it might be slightly off -- you 4034 * might have a bit of a truncation problem. 4035 */ 4036#ifdef __XSCALE__ 4037 cylinders = (lun->be_lun->maxlba + 1) / 4038 sectors_per_cylinder; 4039#else 4040 for (shift = 31; shift > 0; shift--) { 4041 if (sectors_per_cylinder & (1 << shift)) 4042 break; 4043 } 4044 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4045#endif 4046 4047 /* 4048 * We've basically got 3 bytes, or 24 bits for the 4049 * cylinder size in the mode page. If we're over, 4050 * just round down to 2^24. 4051 */ 4052 if (cylinders > 0xffffff) 4053 cylinders = 0xffffff; 4054 4055 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4056 CTL_PAGE_DEFAULT]; 4057 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4058 4059 if ((value = ctl_get_opt(&lun->be_lun->options, 4060 "rpm")) != NULL) { 4061 scsi_ulto2b(strtol(value, NULL, 0), 4062 rigid_disk_page->rotation_rate); 4063 } 4064 4065 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4066 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4067 sizeof(rigid_disk_page_default)); 4068 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4069 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4070 sizeof(rigid_disk_page_default)); 4071 4072 page_index->page_data = 4073 (uint8_t *)lun->mode_pages.rigid_disk_page; 4074 break; 4075 } 4076 case SMS_VERIFY_ERROR_RECOVERY_PAGE: { 4077 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4078 ("subpage %#x for page %#x is incorrect!", 4079 page_index->subpage, page_code)); 4080 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT], 4081 &verify_er_page_default, 4082 sizeof(verify_er_page_default)); 4083 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE], 4084 &verify_er_page_changeable, 4085 sizeof(verify_er_page_changeable)); 4086 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT], 4087 &verify_er_page_default, 4088 sizeof(verify_er_page_default)); 4089 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED], 4090 &verify_er_page_default, 4091 sizeof(verify_er_page_default)); 4092 page_index->page_data = 4093 (uint8_t *)lun->mode_pages.verify_er_page; 4094 break; 4095 } 4096 case SMS_CACHING_PAGE: { 4097 struct scsi_caching_page *caching_page; 4098 4099 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4100 ("subpage %#x for page %#x is incorrect!", 4101 page_index->subpage, page_code)); 4102 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4103 &caching_page_default, 4104 sizeof(caching_page_default)); 4105 memcpy(&lun->mode_pages.caching_page[ 4106 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4107 sizeof(caching_page_changeable)); 4108 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4109 &caching_page_default, 4110 sizeof(caching_page_default)); 4111 caching_page = &lun->mode_pages.caching_page[ 4112 CTL_PAGE_SAVED]; 4113 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4114 if (value != NULL && strcmp(value, "off") == 0) 4115 caching_page->flags1 &= ~SCP_WCE; 4116 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4117 if (value != NULL && strcmp(value, "off") == 0) 4118 caching_page->flags1 |= SCP_RCD; 4119 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4120 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4121 sizeof(caching_page_default)); 4122 page_index->page_data = 4123 (uint8_t *)lun->mode_pages.caching_page; 4124 break; 4125 } 4126 case SMS_CONTROL_MODE_PAGE: { 4127 switch (page_index->subpage) { 4128 case SMS_SUBPAGE_PAGE_0: { 4129 struct scsi_control_page *control_page; 4130 4131 memcpy(&lun->mode_pages.control_page[ 4132 CTL_PAGE_DEFAULT], 4133 &control_page_default, 4134 sizeof(control_page_default)); 4135 memcpy(&lun->mode_pages.control_page[ 4136 CTL_PAGE_CHANGEABLE], 4137 &control_page_changeable, 4138 sizeof(control_page_changeable)); 4139 memcpy(&lun->mode_pages.control_page[ 4140 CTL_PAGE_SAVED], 4141 &control_page_default, 4142 sizeof(control_page_default)); 4143 control_page = &lun->mode_pages.control_page[ 4144 CTL_PAGE_SAVED]; 4145 value = ctl_get_opt(&lun->be_lun->options, 4146 "reordering"); 4147 if (value != NULL && 4148 strcmp(value, "unrestricted") == 0) { 4149 control_page->queue_flags &= 4150 ~SCP_QUEUE_ALG_MASK; 4151 control_page->queue_flags |= 4152 SCP_QUEUE_ALG_UNRESTRICTED; 4153 } 4154 memcpy(&lun->mode_pages.control_page[ 4155 CTL_PAGE_CURRENT], 4156 &lun->mode_pages.control_page[ 4157 CTL_PAGE_SAVED], 4158 sizeof(control_page_default)); 4159 page_index->page_data = 4160 (uint8_t *)lun->mode_pages.control_page; 4161 break; 4162 } 4163 case 0x01: 4164 memcpy(&lun->mode_pages.control_ext_page[ 4165 CTL_PAGE_DEFAULT], 4166 &control_ext_page_default, 4167 sizeof(control_ext_page_default)); 4168 memcpy(&lun->mode_pages.control_ext_page[ 4169 CTL_PAGE_CHANGEABLE], 4170 &control_ext_page_changeable, 4171 sizeof(control_ext_page_changeable)); 4172 memcpy(&lun->mode_pages.control_ext_page[ 4173 CTL_PAGE_SAVED], 4174 &control_ext_page_default, 4175 sizeof(control_ext_page_default)); 4176 memcpy(&lun->mode_pages.control_ext_page[ 4177 CTL_PAGE_CURRENT], 4178 &lun->mode_pages.control_ext_page[ 4179 CTL_PAGE_SAVED], 4180 sizeof(control_ext_page_default)); 4181 page_index->page_data = 4182 (uint8_t *)lun->mode_pages.control_ext_page; 4183 break; 4184 default: 4185 panic("subpage %#x for page %#x is incorrect!", 4186 page_index->subpage, page_code); 4187 } 4188 break; 4189 } 4190 case SMS_INFO_EXCEPTIONS_PAGE: { 4191 switch (page_index->subpage) { 4192 case SMS_SUBPAGE_PAGE_0: 4193 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4194 &ie_page_default, 4195 sizeof(ie_page_default)); 4196 memcpy(&lun->mode_pages.ie_page[ 4197 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4198 sizeof(ie_page_changeable)); 4199 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4200 &ie_page_default, 4201 sizeof(ie_page_default)); 4202 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4203 &ie_page_default, 4204 sizeof(ie_page_default)); 4205 page_index->page_data = 4206 (uint8_t *)lun->mode_pages.ie_page; 4207 break; 4208 case 0x02: { 4209 struct ctl_logical_block_provisioning_page *page; 4210 4211 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4212 &lbp_page_default, 4213 sizeof(lbp_page_default)); 4214 memcpy(&lun->mode_pages.lbp_page[ 4215 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4216 sizeof(lbp_page_changeable)); 4217 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4218 &lbp_page_default, 4219 sizeof(lbp_page_default)); 4220 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4221 value = ctl_get_opt(&lun->be_lun->options, 4222 "avail-threshold"); 4223 if (value != NULL && 4224 ctl_expand_number(value, &ival) == 0) { 4225 page->descr[0].flags |= SLBPPD_ENABLED | 4226 SLBPPD_ARMING_DEC; 4227 if (lun->be_lun->blocksize) 4228 ival /= lun->be_lun->blocksize; 4229 else 4230 ival /= 512; 4231 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4232 page->descr[0].count); 4233 } 4234 value = ctl_get_opt(&lun->be_lun->options, 4235 "used-threshold"); 4236 if (value != NULL && 4237 ctl_expand_number(value, &ival) == 0) { 4238 page->descr[1].flags |= SLBPPD_ENABLED | 4239 SLBPPD_ARMING_INC; 4240 if (lun->be_lun->blocksize) 4241 ival /= lun->be_lun->blocksize; 4242 else 4243 ival /= 512; 4244 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4245 page->descr[1].count); 4246 } 4247 value = ctl_get_opt(&lun->be_lun->options, 4248 "pool-avail-threshold"); 4249 if (value != NULL && 4250 ctl_expand_number(value, &ival) == 0) { 4251 page->descr[2].flags |= SLBPPD_ENABLED | 4252 SLBPPD_ARMING_DEC; 4253 if (lun->be_lun->blocksize) 4254 ival /= lun->be_lun->blocksize; 4255 else 4256 ival /= 512; 4257 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4258 page->descr[2].count); 4259 } 4260 value = ctl_get_opt(&lun->be_lun->options, 4261 "pool-used-threshold"); 4262 if (value != NULL && 4263 ctl_expand_number(value, &ival) == 0) { 4264 page->descr[3].flags |= SLBPPD_ENABLED | 4265 SLBPPD_ARMING_INC; 4266 if (lun->be_lun->blocksize) 4267 ival /= lun->be_lun->blocksize; 4268 else 4269 ival /= 512; 4270 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4271 page->descr[3].count); 4272 } 4273 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4274 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4275 sizeof(lbp_page_default)); 4276 page_index->page_data = 4277 (uint8_t *)lun->mode_pages.lbp_page; 4278 break; 4279 } 4280 default: 4281 panic("subpage %#x for page %#x is incorrect!", 4282 page_index->subpage, page_code); 4283 } 4284 break; 4285 } 4286 case SMS_CDDVD_CAPS_PAGE:{ 4287 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0, 4288 ("subpage %#x for page %#x is incorrect!", 4289 page_index->subpage, page_code)); 4290 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4291 &cddvd_page_default, 4292 sizeof(cddvd_page_default)); 4293 memcpy(&lun->mode_pages.cddvd_page[ 4294 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4295 sizeof(cddvd_page_changeable)); 4296 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4297 &cddvd_page_default, 4298 sizeof(cddvd_page_default)); 4299 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4300 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4301 sizeof(cddvd_page_default)); 4302 page_index->page_data = 4303 (uint8_t *)lun->mode_pages.cddvd_page; 4304 break; 4305 } 4306 default: 4307 panic("invalid page code value %#x", page_code); 4308 } 4309 } 4310 4311 return (CTL_RETVAL_COMPLETE); 4312} 4313 4314static int 4315ctl_init_log_page_index(struct ctl_lun *lun) 4316{ 4317 struct ctl_page_index *page_index; 4318 int i, j, k, prev; 4319 4320 memcpy(&lun->log_pages.index, log_page_index_template, 4321 sizeof(log_page_index_template)); 4322 4323 prev = -1; 4324 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4325 4326 page_index = &lun->log_pages.index[i]; 4327 if (lun->be_lun->lun_type == T_DIRECT && 4328 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4329 continue; 4330 if (lun->be_lun->lun_type == T_PROCESSOR && 4331 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4332 continue; 4333 if (lun->be_lun->lun_type == T_CDROM && 4334 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4335 continue; 4336 4337 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4338 lun->backend->lun_attr == NULL) 4339 continue; 4340 4341 if (page_index->page_code != prev) { 4342 lun->log_pages.pages_page[j] = page_index->page_code; 4343 prev = page_index->page_code; 4344 j++; 4345 } 4346 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4347 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4348 k++; 4349 } 4350 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4351 lun->log_pages.index[0].page_len = j; 4352 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4353 lun->log_pages.index[1].page_len = k * 2; 4354 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4355 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4356 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4357 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4358 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page; 4359 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page); 4360 4361 return (CTL_RETVAL_COMPLETE); 4362} 4363 4364static int 4365hex2bin(const char *str, uint8_t *buf, int buf_size) 4366{ 4367 int i; 4368 u_char c; 4369 4370 memset(buf, 0, buf_size); 4371 while (isspace(str[0])) 4372 str++; 4373 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4374 str += 2; 4375 buf_size *= 2; 4376 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4377 while (str[i] == '-') /* Skip dashes in UUIDs. */ 4378 str++; 4379 c = str[i]; 4380 if (isdigit(c)) 4381 c -= '0'; 4382 else if (isalpha(c)) 4383 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4384 else 4385 break; 4386 if (c >= 16) 4387 break; 4388 if ((i & 1) == 0) 4389 buf[i / 2] |= (c << 4); 4390 else 4391 buf[i / 2] |= c; 4392 } 4393 return ((i + 1) / 2); 4394} 4395 4396/* 4397 * LUN allocation. 4398 * 4399 * Requirements: 4400 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4401 * wants us to allocate the LUN and he can block. 4402 * - ctl_softc is always set 4403 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4404 * 4405 * Returns 0 for success, non-zero (errno) for failure. 4406 */ 4407static int 4408ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4409 struct ctl_be_lun *const be_lun) 4410{ 4411 struct ctl_lun *nlun, *lun; 4412 struct scsi_vpd_id_descriptor *desc; 4413 struct scsi_vpd_id_t10 *t10id; 4414 const char *eui, *naa, *scsiname, *uuid, *vendor, *value; 4415 int lun_number, i, lun_malloced; 4416 int devidlen, idlen1, idlen2 = 0, len; 4417 4418 if (be_lun == NULL) 4419 return (EINVAL); 4420 4421 /* 4422 * We currently only support Direct Access or Processor LUN types. 4423 */ 4424 switch (be_lun->lun_type) { 4425 case T_DIRECT: 4426 case T_PROCESSOR: 4427 case T_CDROM: 4428 break; 4429 case T_SEQUENTIAL: 4430 case T_CHANGER: 4431 default: 4432 be_lun->lun_config_status(be_lun->be_lun, 4433 CTL_LUN_CONFIG_FAILURE); 4434 break; 4435 } 4436 if (ctl_lun == NULL) { 4437 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4438 lun_malloced = 1; 4439 } else { 4440 lun_malloced = 0; 4441 lun = ctl_lun; 4442 } 4443 4444 memset(lun, 0, sizeof(*lun)); 4445 if (lun_malloced) 4446 lun->flags = CTL_LUN_MALLOCED; 4447 4448 /* Generate LUN ID. */ 4449 devidlen = max(CTL_DEVID_MIN_LEN, 4450 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4451 idlen1 = sizeof(*t10id) + devidlen; 4452 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4453 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4454 if (scsiname != NULL) { 4455 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4456 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4457 } 4458 eui = ctl_get_opt(&be_lun->options, "eui"); 4459 if (eui != NULL) { 4460 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4461 } 4462 naa = ctl_get_opt(&be_lun->options, "naa"); 4463 if (naa != NULL) { 4464 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4465 } 4466 uuid = ctl_get_opt(&be_lun->options, "uuid"); 4467 if (uuid != NULL) { 4468 len += sizeof(struct scsi_vpd_id_descriptor) + 18; 4469 } 4470 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4471 M_CTL, M_WAITOK | M_ZERO); 4472 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4473 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4474 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4475 desc->length = idlen1; 4476 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4477 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4478 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4479 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4480 } else { 4481 strncpy(t10id->vendor, vendor, 4482 min(sizeof(t10id->vendor), strlen(vendor))); 4483 } 4484 strncpy((char *)t10id->vendor_spec_id, 4485 (char *)be_lun->device_id, devidlen); 4486 if (scsiname != NULL) { 4487 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4488 desc->length); 4489 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4490 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4491 SVPD_ID_TYPE_SCSI_NAME; 4492 desc->length = idlen2; 4493 strlcpy(desc->identifier, scsiname, idlen2); 4494 } 4495 if (eui != NULL) { 4496 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4497 desc->length); 4498 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4499 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4500 SVPD_ID_TYPE_EUI64; 4501 desc->length = hex2bin(eui, desc->identifier, 16); 4502 desc->length = desc->length > 12 ? 16 : 4503 (desc->length > 8 ? 12 : 8); 4504 len -= 16 - desc->length; 4505 } 4506 if (naa != NULL) { 4507 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4508 desc->length); 4509 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4510 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4511 SVPD_ID_TYPE_NAA; 4512 desc->length = hex2bin(naa, desc->identifier, 16); 4513 desc->length = desc->length > 8 ? 16 : 8; 4514 len -= 16 - desc->length; 4515 } 4516 if (uuid != NULL) { 4517 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4518 desc->length); 4519 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4520 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4521 SVPD_ID_TYPE_UUID; 4522 desc->identifier[0] = 0x10; 4523 hex2bin(uuid, &desc->identifier[2], 16); 4524 desc->length = 18; 4525 } 4526 lun->lun_devid->len = len; 4527 4528 mtx_lock(&ctl_softc->ctl_lock); 4529 /* 4530 * See if the caller requested a particular LUN number. If so, see 4531 * if it is available. Otherwise, allocate the first available LUN. 4532 */ 4533 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4534 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4535 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4536 mtx_unlock(&ctl_softc->ctl_lock); 4537 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4538 printf("ctl: requested LUN ID %d is higher " 4539 "than CTL_MAX_LUNS - 1 (%d)\n", 4540 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4541 } else { 4542 /* 4543 * XXX KDM return an error, or just assign 4544 * another LUN ID in this case?? 4545 */ 4546 printf("ctl: requested LUN ID %d is already " 4547 "in use\n", be_lun->req_lun_id); 4548 } 4549 if (lun->flags & CTL_LUN_MALLOCED) 4550 free(lun, M_CTL); 4551 be_lun->lun_config_status(be_lun->be_lun, 4552 CTL_LUN_CONFIG_FAILURE); 4553 return (ENOSPC); 4554 } 4555 lun_number = be_lun->req_lun_id; 4556 } else { 4557 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4558 if (lun_number == -1) { 4559 mtx_unlock(&ctl_softc->ctl_lock); 4560 printf("ctl: can't allocate LUN, out of LUNs\n"); 4561 if (lun->flags & CTL_LUN_MALLOCED) 4562 free(lun, M_CTL); 4563 be_lun->lun_config_status(be_lun->be_lun, 4564 CTL_LUN_CONFIG_FAILURE); 4565 return (ENOSPC); 4566 } 4567 } 4568 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4569 4570 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4571 lun->lun = lun_number; 4572 lun->be_lun = be_lun; 4573 /* 4574 * The processor LUN is always enabled. Disk LUNs come on line 4575 * disabled, and must be enabled by the backend. 4576 */ 4577 lun->flags |= CTL_LUN_DISABLED; 4578 lun->backend = be_lun->be; 4579 be_lun->ctl_lun = lun; 4580 be_lun->lun_id = lun_number; 4581 atomic_add_int(&be_lun->be->num_luns, 1); 4582 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4583 lun->flags |= CTL_LUN_EJECTED; 4584 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4585 lun->flags |= CTL_LUN_NO_MEDIA; 4586 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4587 lun->flags |= CTL_LUN_STOPPED; 4588 4589 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4590 lun->flags |= CTL_LUN_PRIMARY_SC; 4591 4592 value = ctl_get_opt(&be_lun->options, "removable"); 4593 if (value != NULL) { 4594 if (strcmp(value, "on") == 0) 4595 lun->flags |= CTL_LUN_REMOVABLE; 4596 } else if (be_lun->lun_type == T_CDROM) 4597 lun->flags |= CTL_LUN_REMOVABLE; 4598 4599 lun->ctl_softc = ctl_softc; 4600#ifdef CTL_TIME_IO 4601 lun->last_busy = getsbinuptime(); 4602#endif 4603 TAILQ_INIT(&lun->ooa_queue); 4604 TAILQ_INIT(&lun->blocked_queue); 4605 STAILQ_INIT(&lun->error_list); 4606 lun->ie_reported = 1; 4607 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0); 4608 ctl_tpc_lun_init(lun); 4609 4610 /* 4611 * Initialize the mode and log page index. 4612 */ 4613 ctl_init_page_index(lun); 4614 ctl_init_log_page_index(lun); 4615 4616 /* 4617 * Now, before we insert this lun on the lun list, set the lun 4618 * inventory changed UA for all other luns. 4619 */ 4620 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4621 mtx_lock(&nlun->lun_lock); 4622 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4623 mtx_unlock(&nlun->lun_lock); 4624 } 4625 4626 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4627 4628 ctl_softc->ctl_luns[lun_number] = lun; 4629 4630 ctl_softc->num_luns++; 4631 4632 /* Setup statistics gathering */ 4633 lun->stats.device_type = be_lun->lun_type; 4634 lun->stats.lun_number = lun_number; 4635 lun->stats.blocksize = be_lun->blocksize; 4636 if (be_lun->blocksize == 0) 4637 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4638 for (i = 0;i < CTL_MAX_PORTS;i++) 4639 lun->stats.ports[i].targ_port = i; 4640 4641 mtx_unlock(&ctl_softc->ctl_lock); 4642 4643 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4644 return (0); 4645} 4646 4647/* 4648 * Delete a LUN. 4649 * Assumptions: 4650 * - LUN has already been marked invalid and any pending I/O has been taken 4651 * care of. 4652 */ 4653static int 4654ctl_free_lun(struct ctl_lun *lun) 4655{ 4656 struct ctl_softc *softc; 4657 struct ctl_lun *nlun; 4658 int i; 4659 4660 softc = lun->ctl_softc; 4661 4662 mtx_assert(&softc->ctl_lock, MA_OWNED); 4663 4664 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4665 4666 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4667 4668 softc->ctl_luns[lun->lun] = NULL; 4669 4670 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4671 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4672 4673 softc->num_luns--; 4674 4675 /* 4676 * Tell the backend to free resources, if this LUN has a backend. 4677 */ 4678 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4679 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4680 4681 lun->ie_reportcnt = UINT32_MAX; 4682 callout_drain(&lun->ie_callout); 4683 4684 ctl_tpc_lun_shutdown(lun); 4685 mtx_destroy(&lun->lun_lock); 4686 free(lun->lun_devid, M_CTL); 4687 for (i = 0; i < CTL_MAX_PORTS; i++) 4688 free(lun->pending_ua[i], M_CTL); 4689 for (i = 0; i < CTL_MAX_PORTS; i++) 4690 free(lun->pr_keys[i], M_CTL); 4691 free(lun->write_buffer, M_CTL); 4692 if (lun->flags & CTL_LUN_MALLOCED) 4693 free(lun, M_CTL); 4694 4695 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4696 mtx_lock(&nlun->lun_lock); 4697 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4698 mtx_unlock(&nlun->lun_lock); 4699 } 4700 4701 return (0); 4702} 4703 4704static void 4705ctl_create_lun(struct ctl_be_lun *be_lun) 4706{ 4707 4708 /* 4709 * ctl_alloc_lun() should handle all potential failure cases. 4710 */ 4711 ctl_alloc_lun(control_softc, NULL, be_lun); 4712} 4713 4714int 4715ctl_add_lun(struct ctl_be_lun *be_lun) 4716{ 4717 struct ctl_softc *softc = control_softc; 4718 4719 mtx_lock(&softc->ctl_lock); 4720 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4721 mtx_unlock(&softc->ctl_lock); 4722 wakeup(&softc->pending_lun_queue); 4723 4724 return (0); 4725} 4726 4727int 4728ctl_enable_lun(struct ctl_be_lun *be_lun) 4729{ 4730 struct ctl_softc *softc; 4731 struct ctl_port *port, *nport; 4732 struct ctl_lun *lun; 4733 int retval; 4734 4735 lun = (struct ctl_lun *)be_lun->ctl_lun; 4736 softc = lun->ctl_softc; 4737 4738 mtx_lock(&softc->ctl_lock); 4739 mtx_lock(&lun->lun_lock); 4740 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4741 /* 4742 * eh? Why did we get called if the LUN is already 4743 * enabled? 4744 */ 4745 mtx_unlock(&lun->lun_lock); 4746 mtx_unlock(&softc->ctl_lock); 4747 return (0); 4748 } 4749 lun->flags &= ~CTL_LUN_DISABLED; 4750 mtx_unlock(&lun->lun_lock); 4751 4752 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4753 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4754 port->lun_map != NULL || port->lun_enable == NULL) 4755 continue; 4756 4757 /* 4758 * Drop the lock while we call the FETD's enable routine. 4759 * This can lead to a callback into CTL (at least in the 4760 * case of the internal initiator frontend. 4761 */ 4762 mtx_unlock(&softc->ctl_lock); 4763 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4764 mtx_lock(&softc->ctl_lock); 4765 if (retval != 0) { 4766 printf("%s: FETD %s port %d returned error " 4767 "%d for lun_enable on lun %jd\n", 4768 __func__, port->port_name, port->targ_port, 4769 retval, (intmax_t)lun->lun); 4770 } 4771 } 4772 4773 mtx_unlock(&softc->ctl_lock); 4774 ctl_isc_announce_lun(lun); 4775 4776 return (0); 4777} 4778 4779int 4780ctl_disable_lun(struct ctl_be_lun *be_lun) 4781{ 4782 struct ctl_softc *softc; 4783 struct ctl_port *port; 4784 struct ctl_lun *lun; 4785 int retval; 4786 4787 lun = (struct ctl_lun *)be_lun->ctl_lun; 4788 softc = lun->ctl_softc; 4789 4790 mtx_lock(&softc->ctl_lock); 4791 mtx_lock(&lun->lun_lock); 4792 if (lun->flags & CTL_LUN_DISABLED) { 4793 mtx_unlock(&lun->lun_lock); 4794 mtx_unlock(&softc->ctl_lock); 4795 return (0); 4796 } 4797 lun->flags |= CTL_LUN_DISABLED; 4798 mtx_unlock(&lun->lun_lock); 4799 4800 STAILQ_FOREACH(port, &softc->port_list, links) { 4801 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4802 port->lun_map != NULL || port->lun_disable == NULL) 4803 continue; 4804 4805 /* 4806 * Drop the lock before we call the frontend's disable 4807 * routine, to avoid lock order reversals. 4808 * 4809 * XXX KDM what happens if the frontend list changes while 4810 * we're traversing it? It's unlikely, but should be handled. 4811 */ 4812 mtx_unlock(&softc->ctl_lock); 4813 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4814 mtx_lock(&softc->ctl_lock); 4815 if (retval != 0) { 4816 printf("%s: FETD %s port %d returned error " 4817 "%d for lun_disable on lun %jd\n", 4818 __func__, port->port_name, port->targ_port, 4819 retval, (intmax_t)lun->lun); 4820 } 4821 } 4822 4823 mtx_unlock(&softc->ctl_lock); 4824 ctl_isc_announce_lun(lun); 4825 4826 return (0); 4827} 4828 4829int 4830ctl_start_lun(struct ctl_be_lun *be_lun) 4831{ 4832 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4833 4834 mtx_lock(&lun->lun_lock); 4835 lun->flags &= ~CTL_LUN_STOPPED; 4836 mtx_unlock(&lun->lun_lock); 4837 return (0); 4838} 4839 4840int 4841ctl_stop_lun(struct ctl_be_lun *be_lun) 4842{ 4843 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4844 4845 mtx_lock(&lun->lun_lock); 4846 lun->flags |= CTL_LUN_STOPPED; 4847 mtx_unlock(&lun->lun_lock); 4848 return (0); 4849} 4850 4851int 4852ctl_lun_no_media(struct ctl_be_lun *be_lun) 4853{ 4854 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4855 4856 mtx_lock(&lun->lun_lock); 4857 lun->flags |= CTL_LUN_NO_MEDIA; 4858 mtx_unlock(&lun->lun_lock); 4859 return (0); 4860} 4861 4862int 4863ctl_lun_has_media(struct ctl_be_lun *be_lun) 4864{ 4865 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4866 union ctl_ha_msg msg; 4867 4868 mtx_lock(&lun->lun_lock); 4869 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4870 if (lun->flags & CTL_LUN_REMOVABLE) 4871 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4872 mtx_unlock(&lun->lun_lock); 4873 if ((lun->flags & CTL_LUN_REMOVABLE) && 4874 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4875 bzero(&msg.ua, sizeof(msg.ua)); 4876 msg.hdr.msg_type = CTL_MSG_UA; 4877 msg.hdr.nexus.initid = -1; 4878 msg.hdr.nexus.targ_port = -1; 4879 msg.hdr.nexus.targ_lun = lun->lun; 4880 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4881 msg.ua.ua_all = 1; 4882 msg.ua.ua_set = 1; 4883 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4884 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4885 M_WAITOK); 4886 } 4887 return (0); 4888} 4889 4890int 4891ctl_lun_ejected(struct ctl_be_lun *be_lun) 4892{ 4893 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4894 4895 mtx_lock(&lun->lun_lock); 4896 lun->flags |= CTL_LUN_EJECTED; 4897 mtx_unlock(&lun->lun_lock); 4898 return (0); 4899} 4900 4901int 4902ctl_lun_primary(struct ctl_be_lun *be_lun) 4903{ 4904 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4905 4906 mtx_lock(&lun->lun_lock); 4907 lun->flags |= CTL_LUN_PRIMARY_SC; 4908 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4909 mtx_unlock(&lun->lun_lock); 4910 ctl_isc_announce_lun(lun); 4911 return (0); 4912} 4913 4914int 4915ctl_lun_secondary(struct ctl_be_lun *be_lun) 4916{ 4917 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4918 4919 mtx_lock(&lun->lun_lock); 4920 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4921 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4922 mtx_unlock(&lun->lun_lock); 4923 ctl_isc_announce_lun(lun); 4924 return (0); 4925} 4926 4927int 4928ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4929{ 4930 struct ctl_softc *softc; 4931 struct ctl_lun *lun; 4932 4933 lun = (struct ctl_lun *)be_lun->ctl_lun; 4934 softc = lun->ctl_softc; 4935 4936 mtx_lock(&lun->lun_lock); 4937 4938 /* 4939 * The LUN needs to be disabled before it can be marked invalid. 4940 */ 4941 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4942 mtx_unlock(&lun->lun_lock); 4943 return (-1); 4944 } 4945 /* 4946 * Mark the LUN invalid. 4947 */ 4948 lun->flags |= CTL_LUN_INVALID; 4949 4950 /* 4951 * If there is nothing in the OOA queue, go ahead and free the LUN. 4952 * If we have something in the OOA queue, we'll free it when the 4953 * last I/O completes. 4954 */ 4955 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4956 mtx_unlock(&lun->lun_lock); 4957 mtx_lock(&softc->ctl_lock); 4958 ctl_free_lun(lun); 4959 mtx_unlock(&softc->ctl_lock); 4960 } else 4961 mtx_unlock(&lun->lun_lock); 4962 4963 return (0); 4964} 4965 4966void 4967ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4968{ 4969 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4970 union ctl_ha_msg msg; 4971 4972 mtx_lock(&lun->lun_lock); 4973 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4974 mtx_unlock(&lun->lun_lock); 4975 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4976 /* Send msg to other side. */ 4977 bzero(&msg.ua, sizeof(msg.ua)); 4978 msg.hdr.msg_type = CTL_MSG_UA; 4979 msg.hdr.nexus.initid = -1; 4980 msg.hdr.nexus.targ_port = -1; 4981 msg.hdr.nexus.targ_lun = lun->lun; 4982 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4983 msg.ua.ua_all = 1; 4984 msg.ua.ua_set = 1; 4985 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4986 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4987 M_WAITOK); 4988 } 4989} 4990 4991/* 4992 * Backend "memory move is complete" callback for requests that never 4993 * make it down to say RAIDCore's configuration code. 4994 */ 4995int 4996ctl_config_move_done(union ctl_io *io) 4997{ 4998 int retval; 4999 5000 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5001 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5002 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5003 5004 if ((io->io_hdr.port_status != 0) && 5005 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5006 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5007 /* 5008 * For hardware error sense keys, the sense key 5009 * specific value is defined to be a retry count, 5010 * but we use it to pass back an internal FETD 5011 * error code. XXX KDM Hopefully the FETD is only 5012 * using 16 bits for an error code, since that's 5013 * all the space we have in the sks field. 5014 */ 5015 ctl_set_internal_failure(&io->scsiio, 5016 /*sks_valid*/ 1, 5017 /*retry_count*/ 5018 io->io_hdr.port_status); 5019 } 5020 5021 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5022 ctl_data_print(io); 5023 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5024 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5025 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5026 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5027 /* 5028 * XXX KDM just assuming a single pointer here, and not a 5029 * S/G list. If we start using S/G lists for config data, 5030 * we'll need to know how to clean them up here as well. 5031 */ 5032 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5033 free(io->scsiio.kern_data_ptr, M_CTL); 5034 ctl_done(io); 5035 retval = CTL_RETVAL_COMPLETE; 5036 } else { 5037 /* 5038 * XXX KDM now we need to continue data movement. Some 5039 * options: 5040 * - call ctl_scsiio() again? We don't do this for data 5041 * writes, because for those at least we know ahead of 5042 * time where the write will go and how long it is. For 5043 * config writes, though, that information is largely 5044 * contained within the write itself, thus we need to 5045 * parse out the data again. 5046 * 5047 * - Call some other function once the data is in? 5048 */ 5049 5050 /* 5051 * XXX KDM call ctl_scsiio() again for now, and check flag 5052 * bits to see whether we're allocated or not. 5053 */ 5054 retval = ctl_scsiio(&io->scsiio); 5055 } 5056 return (retval); 5057} 5058 5059/* 5060 * This gets called by a backend driver when it is done with a 5061 * data_submit method. 5062 */ 5063void 5064ctl_data_submit_done(union ctl_io *io) 5065{ 5066 /* 5067 * If the IO_CONT flag is set, we need to call the supplied 5068 * function to continue processing the I/O, instead of completing 5069 * the I/O just yet. 5070 * 5071 * If there is an error, though, we don't want to keep processing. 5072 * Instead, just send status back to the initiator. 5073 */ 5074 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5075 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5076 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5077 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5078 io->scsiio.io_cont(io); 5079 return; 5080 } 5081 ctl_done(io); 5082} 5083 5084/* 5085 * This gets called by a backend driver when it is done with a 5086 * configuration write. 5087 */ 5088void 5089ctl_config_write_done(union ctl_io *io) 5090{ 5091 uint8_t *buf; 5092 5093 /* 5094 * If the IO_CONT flag is set, we need to call the supplied 5095 * function to continue processing the I/O, instead of completing 5096 * the I/O just yet. 5097 * 5098 * If there is an error, though, we don't want to keep processing. 5099 * Instead, just send status back to the initiator. 5100 */ 5101 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5102 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5103 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5104 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5105 io->scsiio.io_cont(io); 5106 return; 5107 } 5108 /* 5109 * Since a configuration write can be done for commands that actually 5110 * have data allocated, like write buffer, and commands that have 5111 * no data, like start/stop unit, we need to check here. 5112 */ 5113 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5114 buf = io->scsiio.kern_data_ptr; 5115 else 5116 buf = NULL; 5117 ctl_done(io); 5118 if (buf) 5119 free(buf, M_CTL); 5120} 5121 5122void 5123ctl_config_read_done(union ctl_io *io) 5124{ 5125 uint8_t *buf; 5126 5127 /* 5128 * If there is some error -- we are done, skip data transfer. 5129 */ 5130 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5131 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5132 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5133 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5134 buf = io->scsiio.kern_data_ptr; 5135 else 5136 buf = NULL; 5137 ctl_done(io); 5138 if (buf) 5139 free(buf, M_CTL); 5140 return; 5141 } 5142 5143 /* 5144 * If the IO_CONT flag is set, we need to call the supplied 5145 * function to continue processing the I/O, instead of completing 5146 * the I/O just yet. 5147 */ 5148 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5149 io->scsiio.io_cont(io); 5150 return; 5151 } 5152 5153 ctl_datamove(io); 5154} 5155 5156/* 5157 * SCSI release command. 5158 */ 5159int 5160ctl_scsi_release(struct ctl_scsiio *ctsio) 5161{ 5162 struct ctl_lun *lun; 5163 uint32_t residx; 5164 5165 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5166 5167 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5168 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5169 5170 /* 5171 * XXX KDM right now, we only support LUN reservation. We don't 5172 * support 3rd party reservations, or extent reservations, which 5173 * might actually need the parameter list. If we've gotten this 5174 * far, we've got a LUN reservation. Anything else got kicked out 5175 * above. So, according to SPC, ignore the length. 5176 */ 5177 5178 mtx_lock(&lun->lun_lock); 5179 5180 /* 5181 * According to SPC, it is not an error for an intiator to attempt 5182 * to release a reservation on a LUN that isn't reserved, or that 5183 * is reserved by another initiator. The reservation can only be 5184 * released, though, by the initiator who made it or by one of 5185 * several reset type events. 5186 */ 5187 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5188 lun->flags &= ~CTL_LUN_RESERVED; 5189 5190 mtx_unlock(&lun->lun_lock); 5191 5192 ctl_set_success(ctsio); 5193 ctl_done((union ctl_io *)ctsio); 5194 return (CTL_RETVAL_COMPLETE); 5195} 5196 5197int 5198ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5199{ 5200 struct ctl_lun *lun; 5201 uint32_t residx; 5202 5203 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5204 5205 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5206 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5207 5208 /* 5209 * XXX KDM right now, we only support LUN reservation. We don't 5210 * support 3rd party reservations, or extent reservations, which 5211 * might actually need the parameter list. If we've gotten this 5212 * far, we've got a LUN reservation. Anything else got kicked out 5213 * above. So, according to SPC, ignore the length. 5214 */ 5215 5216 mtx_lock(&lun->lun_lock); 5217 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5218 ctl_set_reservation_conflict(ctsio); 5219 goto bailout; 5220 } 5221 5222 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5223 if (lun->flags & CTL_LUN_PR_RESERVED) { 5224 ctl_set_success(ctsio); 5225 goto bailout; 5226 } 5227 5228 lun->flags |= CTL_LUN_RESERVED; 5229 lun->res_idx = residx; 5230 ctl_set_success(ctsio); 5231 5232bailout: 5233 mtx_unlock(&lun->lun_lock); 5234 ctl_done((union ctl_io *)ctsio); 5235 return (CTL_RETVAL_COMPLETE); 5236} 5237 5238int 5239ctl_start_stop(struct ctl_scsiio *ctsio) 5240{ 5241 struct scsi_start_stop_unit *cdb; 5242 struct ctl_lun *lun; 5243 int retval; 5244 5245 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5246 5247 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5248 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5249 5250 if ((cdb->how & SSS_PC_MASK) == 0) { 5251 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5252 (cdb->how & SSS_START) == 0) { 5253 uint32_t residx; 5254 5255 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5256 if (ctl_get_prkey(lun, residx) == 0 || 5257 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5258 5259 ctl_set_reservation_conflict(ctsio); 5260 ctl_done((union ctl_io *)ctsio); 5261 return (CTL_RETVAL_COMPLETE); 5262 } 5263 } 5264 5265 if ((cdb->how & SSS_LOEJ) && 5266 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5267 ctl_set_invalid_field(ctsio, 5268 /*sks_valid*/ 1, 5269 /*command*/ 1, 5270 /*field*/ 4, 5271 /*bit_valid*/ 1, 5272 /*bit*/ 1); 5273 ctl_done((union ctl_io *)ctsio); 5274 return (CTL_RETVAL_COMPLETE); 5275 } 5276 5277 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5278 lun->prevent_count > 0) { 5279 /* "Medium removal prevented" */ 5280 ctl_set_sense(ctsio, /*current_error*/ 1, 5281 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5282 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5283 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5284 ctl_done((union ctl_io *)ctsio); 5285 return (CTL_RETVAL_COMPLETE); 5286 } 5287 } 5288 5289 retval = lun->backend->config_write((union ctl_io *)ctsio); 5290 return (retval); 5291} 5292 5293int 5294ctl_prevent_allow(struct ctl_scsiio *ctsio) 5295{ 5296 struct ctl_lun *lun; 5297 struct scsi_prevent *cdb; 5298 int retval; 5299 uint32_t initidx; 5300 5301 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5302 5303 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5304 cdb = (struct scsi_prevent *)ctsio->cdb; 5305 5306 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5307 ctl_set_invalid_opcode(ctsio); 5308 ctl_done((union ctl_io *)ctsio); 5309 return (CTL_RETVAL_COMPLETE); 5310 } 5311 5312 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5313 mtx_lock(&lun->lun_lock); 5314 if ((cdb->how & PR_PREVENT) && 5315 ctl_is_set(lun->prevent, initidx) == 0) { 5316 ctl_set_mask(lun->prevent, initidx); 5317 lun->prevent_count++; 5318 } else if ((cdb->how & PR_PREVENT) == 0 && 5319 ctl_is_set(lun->prevent, initidx)) { 5320 ctl_clear_mask(lun->prevent, initidx); 5321 lun->prevent_count--; 5322 } 5323 mtx_unlock(&lun->lun_lock); 5324 retval = lun->backend->config_write((union ctl_io *)ctsio); 5325 return (retval); 5326} 5327 5328/* 5329 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5330 * we don't really do anything with the LBA and length fields if the user 5331 * passes them in. Instead we'll just flush out the cache for the entire 5332 * LUN. 5333 */ 5334int 5335ctl_sync_cache(struct ctl_scsiio *ctsio) 5336{ 5337 struct ctl_lun *lun; 5338 struct ctl_softc *softc; 5339 struct ctl_lba_len_flags *lbalen; 5340 uint64_t starting_lba; 5341 uint32_t block_count; 5342 int retval; 5343 uint8_t byte2; 5344 5345 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5346 5347 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5348 softc = lun->ctl_softc; 5349 retval = 0; 5350 5351 switch (ctsio->cdb[0]) { 5352 case SYNCHRONIZE_CACHE: { 5353 struct scsi_sync_cache *cdb; 5354 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5355 5356 starting_lba = scsi_4btoul(cdb->begin_lba); 5357 block_count = scsi_2btoul(cdb->lb_count); 5358 byte2 = cdb->byte2; 5359 break; 5360 } 5361 case SYNCHRONIZE_CACHE_16: { 5362 struct scsi_sync_cache_16 *cdb; 5363 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5364 5365 starting_lba = scsi_8btou64(cdb->begin_lba); 5366 block_count = scsi_4btoul(cdb->lb_count); 5367 byte2 = cdb->byte2; 5368 break; 5369 } 5370 default: 5371 ctl_set_invalid_opcode(ctsio); 5372 ctl_done((union ctl_io *)ctsio); 5373 goto bailout; 5374 break; /* NOTREACHED */ 5375 } 5376 5377 /* 5378 * We check the LBA and length, but don't do anything with them. 5379 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5380 * get flushed. This check will just help satisfy anyone who wants 5381 * to see an error for an out of range LBA. 5382 */ 5383 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5384 ctl_set_lba_out_of_range(ctsio, 5385 MAX(starting_lba, lun->be_lun->maxlba + 1)); 5386 ctl_done((union ctl_io *)ctsio); 5387 goto bailout; 5388 } 5389 5390 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5391 lbalen->lba = starting_lba; 5392 lbalen->len = block_count; 5393 lbalen->flags = byte2; 5394 retval = lun->backend->config_write((union ctl_io *)ctsio); 5395 5396bailout: 5397 return (retval); 5398} 5399 5400int 5401ctl_format(struct ctl_scsiio *ctsio) 5402{ 5403 struct scsi_format *cdb; 5404 struct ctl_lun *lun; 5405 int length, defect_list_len; 5406 5407 CTL_DEBUG_PRINT(("ctl_format\n")); 5408 5409 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5410 5411 cdb = (struct scsi_format *)ctsio->cdb; 5412 5413 length = 0; 5414 if (cdb->byte2 & SF_FMTDATA) { 5415 if (cdb->byte2 & SF_LONGLIST) 5416 length = sizeof(struct scsi_format_header_long); 5417 else 5418 length = sizeof(struct scsi_format_header_short); 5419 } 5420 5421 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5422 && (length > 0)) { 5423 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5424 ctsio->kern_data_len = length; 5425 ctsio->kern_total_len = length; 5426 ctsio->kern_data_resid = 0; 5427 ctsio->kern_rel_offset = 0; 5428 ctsio->kern_sg_entries = 0; 5429 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5430 ctsio->be_move_done = ctl_config_move_done; 5431 ctl_datamove((union ctl_io *)ctsio); 5432 5433 return (CTL_RETVAL_COMPLETE); 5434 } 5435 5436 defect_list_len = 0; 5437 5438 if (cdb->byte2 & SF_FMTDATA) { 5439 if (cdb->byte2 & SF_LONGLIST) { 5440 struct scsi_format_header_long *header; 5441 5442 header = (struct scsi_format_header_long *) 5443 ctsio->kern_data_ptr; 5444 5445 defect_list_len = scsi_4btoul(header->defect_list_len); 5446 if (defect_list_len != 0) { 5447 ctl_set_invalid_field(ctsio, 5448 /*sks_valid*/ 1, 5449 /*command*/ 0, 5450 /*field*/ 2, 5451 /*bit_valid*/ 0, 5452 /*bit*/ 0); 5453 goto bailout; 5454 } 5455 } else { 5456 struct scsi_format_header_short *header; 5457 5458 header = (struct scsi_format_header_short *) 5459 ctsio->kern_data_ptr; 5460 5461 defect_list_len = scsi_2btoul(header->defect_list_len); 5462 if (defect_list_len != 0) { 5463 ctl_set_invalid_field(ctsio, 5464 /*sks_valid*/ 1, 5465 /*command*/ 0, 5466 /*field*/ 2, 5467 /*bit_valid*/ 0, 5468 /*bit*/ 0); 5469 goto bailout; 5470 } 5471 } 5472 } 5473 5474 ctl_set_success(ctsio); 5475bailout: 5476 5477 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5478 free(ctsio->kern_data_ptr, M_CTL); 5479 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5480 } 5481 5482 ctl_done((union ctl_io *)ctsio); 5483 return (CTL_RETVAL_COMPLETE); 5484} 5485 5486int 5487ctl_read_buffer(struct ctl_scsiio *ctsio) 5488{ 5489 struct ctl_lun *lun; 5490 uint64_t buffer_offset; 5491 uint32_t len; 5492 uint8_t byte2; 5493 static uint8_t descr[4]; 5494 static uint8_t echo_descr[4] = { 0 }; 5495 5496 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5497 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5498 switch (ctsio->cdb[0]) { 5499 case READ_BUFFER: { 5500 struct scsi_read_buffer *cdb; 5501 5502 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5503 buffer_offset = scsi_3btoul(cdb->offset); 5504 len = scsi_3btoul(cdb->length); 5505 byte2 = cdb->byte2; 5506 break; 5507 } 5508 case READ_BUFFER_16: { 5509 struct scsi_read_buffer_16 *cdb; 5510 5511 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5512 buffer_offset = scsi_8btou64(cdb->offset); 5513 len = scsi_4btoul(cdb->length); 5514 byte2 = cdb->byte2; 5515 break; 5516 } 5517 default: /* This shouldn't happen. */ 5518 ctl_set_invalid_opcode(ctsio); 5519 ctl_done((union ctl_io *)ctsio); 5520 return (CTL_RETVAL_COMPLETE); 5521 } 5522 5523 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5524 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5525 ctl_set_invalid_field(ctsio, 5526 /*sks_valid*/ 1, 5527 /*command*/ 1, 5528 /*field*/ 6, 5529 /*bit_valid*/ 0, 5530 /*bit*/ 0); 5531 ctl_done((union ctl_io *)ctsio); 5532 return (CTL_RETVAL_COMPLETE); 5533 } 5534 5535 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5536 descr[0] = 0; 5537 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5538 ctsio->kern_data_ptr = descr; 5539 len = min(len, sizeof(descr)); 5540 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5541 ctsio->kern_data_ptr = echo_descr; 5542 len = min(len, sizeof(echo_descr)); 5543 } else { 5544 if (lun->write_buffer == NULL) { 5545 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5546 M_CTL, M_WAITOK); 5547 } 5548 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5549 } 5550 ctsio->kern_data_len = len; 5551 ctsio->kern_total_len = len; 5552 ctsio->kern_data_resid = 0; 5553 ctsio->kern_rel_offset = 0; 5554 ctsio->kern_sg_entries = 0; 5555 ctl_set_success(ctsio); 5556 ctsio->be_move_done = ctl_config_move_done; 5557 ctl_datamove((union ctl_io *)ctsio); 5558 return (CTL_RETVAL_COMPLETE); 5559} 5560 5561int 5562ctl_write_buffer(struct ctl_scsiio *ctsio) 5563{ 5564 struct scsi_write_buffer *cdb; 5565 struct ctl_lun *lun; 5566 int buffer_offset, len; 5567 5568 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5569 5570 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5571 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5572 5573 len = scsi_3btoul(cdb->length); 5574 buffer_offset = scsi_3btoul(cdb->offset); 5575 5576 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5577 ctl_set_invalid_field(ctsio, 5578 /*sks_valid*/ 1, 5579 /*command*/ 1, 5580 /*field*/ 6, 5581 /*bit_valid*/ 0, 5582 /*bit*/ 0); 5583 ctl_done((union ctl_io *)ctsio); 5584 return (CTL_RETVAL_COMPLETE); 5585 } 5586 5587 /* 5588 * If we've got a kernel request that hasn't been malloced yet, 5589 * malloc it and tell the caller the data buffer is here. 5590 */ 5591 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5592 if (lun->write_buffer == NULL) { 5593 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5594 M_CTL, M_WAITOK); 5595 } 5596 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5597 ctsio->kern_data_len = len; 5598 ctsio->kern_total_len = len; 5599 ctsio->kern_data_resid = 0; 5600 ctsio->kern_rel_offset = 0; 5601 ctsio->kern_sg_entries = 0; 5602 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5603 ctsio->be_move_done = ctl_config_move_done; 5604 ctl_datamove((union ctl_io *)ctsio); 5605 5606 return (CTL_RETVAL_COMPLETE); 5607 } 5608 5609 ctl_set_success(ctsio); 5610 ctl_done((union ctl_io *)ctsio); 5611 return (CTL_RETVAL_COMPLETE); 5612} 5613 5614int 5615ctl_write_same(struct ctl_scsiio *ctsio) 5616{ 5617 struct ctl_lun *lun; 5618 struct ctl_lba_len_flags *lbalen; 5619 uint64_t lba; 5620 uint32_t num_blocks; 5621 int len, retval; 5622 uint8_t byte2; 5623 5624 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5625 5626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5627 5628 switch (ctsio->cdb[0]) { 5629 case WRITE_SAME_10: { 5630 struct scsi_write_same_10 *cdb; 5631 5632 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5633 5634 lba = scsi_4btoul(cdb->addr); 5635 num_blocks = scsi_2btoul(cdb->length); 5636 byte2 = cdb->byte2; 5637 break; 5638 } 5639 case WRITE_SAME_16: { 5640 struct scsi_write_same_16 *cdb; 5641 5642 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5643 5644 lba = scsi_8btou64(cdb->addr); 5645 num_blocks = scsi_4btoul(cdb->length); 5646 byte2 = cdb->byte2; 5647 break; 5648 } 5649 default: 5650 /* 5651 * We got a command we don't support. This shouldn't 5652 * happen, commands should be filtered out above us. 5653 */ 5654 ctl_set_invalid_opcode(ctsio); 5655 ctl_done((union ctl_io *)ctsio); 5656 5657 return (CTL_RETVAL_COMPLETE); 5658 break; /* NOTREACHED */ 5659 } 5660 5661 /* ANCHOR flag can be used only together with UNMAP */ 5662 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5663 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5664 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5665 ctl_done((union ctl_io *)ctsio); 5666 return (CTL_RETVAL_COMPLETE); 5667 } 5668 5669 /* 5670 * The first check is to make sure we're in bounds, the second 5671 * check is to catch wrap-around problems. If the lba + num blocks 5672 * is less than the lba, then we've wrapped around and the block 5673 * range is invalid anyway. 5674 */ 5675 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5676 || ((lba + num_blocks) < lba)) { 5677 ctl_set_lba_out_of_range(ctsio, 5678 MAX(lba, lun->be_lun->maxlba + 1)); 5679 ctl_done((union ctl_io *)ctsio); 5680 return (CTL_RETVAL_COMPLETE); 5681 } 5682 5683 /* Zero number of blocks means "to the last logical block" */ 5684 if (num_blocks == 0) { 5685 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5686 ctl_set_invalid_field(ctsio, 5687 /*sks_valid*/ 0, 5688 /*command*/ 1, 5689 /*field*/ 0, 5690 /*bit_valid*/ 0, 5691 /*bit*/ 0); 5692 ctl_done((union ctl_io *)ctsio); 5693 return (CTL_RETVAL_COMPLETE); 5694 } 5695 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5696 } 5697 5698 len = lun->be_lun->blocksize; 5699 5700 /* 5701 * If we've got a kernel request that hasn't been malloced yet, 5702 * malloc it and tell the caller the data buffer is here. 5703 */ 5704 if ((byte2 & SWS_NDOB) == 0 && 5705 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5706 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5707 ctsio->kern_data_len = len; 5708 ctsio->kern_total_len = len; 5709 ctsio->kern_data_resid = 0; 5710 ctsio->kern_rel_offset = 0; 5711 ctsio->kern_sg_entries = 0; 5712 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5713 ctsio->be_move_done = ctl_config_move_done; 5714 ctl_datamove((union ctl_io *)ctsio); 5715 5716 return (CTL_RETVAL_COMPLETE); 5717 } 5718 5719 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5720 lbalen->lba = lba; 5721 lbalen->len = num_blocks; 5722 lbalen->flags = byte2; 5723 retval = lun->backend->config_write((union ctl_io *)ctsio); 5724 5725 return (retval); 5726} 5727 5728int 5729ctl_unmap(struct ctl_scsiio *ctsio) 5730{ 5731 struct ctl_lun *lun; 5732 struct scsi_unmap *cdb; 5733 struct ctl_ptr_len_flags *ptrlen; 5734 struct scsi_unmap_header *hdr; 5735 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5736 uint64_t lba; 5737 uint32_t num_blocks; 5738 int len, retval; 5739 uint8_t byte2; 5740 5741 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5742 5743 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5744 cdb = (struct scsi_unmap *)ctsio->cdb; 5745 5746 len = scsi_2btoul(cdb->length); 5747 byte2 = cdb->byte2; 5748 5749 /* 5750 * If we've got a kernel request that hasn't been malloced yet, 5751 * malloc it and tell the caller the data buffer is here. 5752 */ 5753 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5754 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK); 5755 ctsio->kern_data_len = len; 5756 ctsio->kern_total_len = len; 5757 ctsio->kern_data_resid = 0; 5758 ctsio->kern_rel_offset = 0; 5759 ctsio->kern_sg_entries = 0; 5760 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5761 ctsio->be_move_done = ctl_config_move_done; 5762 ctl_datamove((union ctl_io *)ctsio); 5763 5764 return (CTL_RETVAL_COMPLETE); 5765 } 5766 5767 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5768 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5769 if (len < sizeof (*hdr) || 5770 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5771 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5772 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5773 ctl_set_invalid_field(ctsio, 5774 /*sks_valid*/ 0, 5775 /*command*/ 0, 5776 /*field*/ 0, 5777 /*bit_valid*/ 0, 5778 /*bit*/ 0); 5779 goto done; 5780 } 5781 len = scsi_2btoul(hdr->desc_length); 5782 buf = (struct scsi_unmap_desc *)(hdr + 1); 5783 end = buf + len / sizeof(*buf); 5784 5785 endnz = buf; 5786 for (range = buf; range < end; range++) { 5787 lba = scsi_8btou64(range->lba); 5788 num_blocks = scsi_4btoul(range->length); 5789 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5790 || ((lba + num_blocks) < lba)) { 5791 ctl_set_lba_out_of_range(ctsio, 5792 MAX(lba, lun->be_lun->maxlba + 1)); 5793 ctl_done((union ctl_io *)ctsio); 5794 return (CTL_RETVAL_COMPLETE); 5795 } 5796 if (num_blocks != 0) 5797 endnz = range + 1; 5798 } 5799 5800 /* 5801 * Block backend can not handle zero last range. 5802 * Filter it out and return if there is nothing left. 5803 */ 5804 len = (uint8_t *)endnz - (uint8_t *)buf; 5805 if (len == 0) { 5806 ctl_set_success(ctsio); 5807 goto done; 5808 } 5809 5810 mtx_lock(&lun->lun_lock); 5811 ptrlen = (struct ctl_ptr_len_flags *) 5812 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5813 ptrlen->ptr = (void *)buf; 5814 ptrlen->len = len; 5815 ptrlen->flags = byte2; 5816 ctl_check_blocked(lun); 5817 mtx_unlock(&lun->lun_lock); 5818 5819 retval = lun->backend->config_write((union ctl_io *)ctsio); 5820 return (retval); 5821 5822done: 5823 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5824 free(ctsio->kern_data_ptr, M_CTL); 5825 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5826 } 5827 ctl_done((union ctl_io *)ctsio); 5828 return (CTL_RETVAL_COMPLETE); 5829} 5830 5831int 5832ctl_default_page_handler(struct ctl_scsiio *ctsio, 5833 struct ctl_page_index *page_index, uint8_t *page_ptr) 5834{ 5835 struct ctl_lun *lun; 5836 uint8_t *current_cp, *saved_cp; 5837 int set_ua; 5838 uint32_t initidx; 5839 5840 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5841 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5842 set_ua = 0; 5843 5844 current_cp = (page_index->page_data + (page_index->page_len * 5845 CTL_PAGE_CURRENT)); 5846 saved_cp = (page_index->page_data + (page_index->page_len * 5847 CTL_PAGE_SAVED)); 5848 5849 mtx_lock(&lun->lun_lock); 5850 if (memcmp(current_cp, page_ptr, page_index->page_len)) { 5851 memcpy(current_cp, page_ptr, page_index->page_len); 5852 memcpy(saved_cp, page_ptr, page_index->page_len); 5853 set_ua = 1; 5854 } 5855 if (set_ua != 0) 5856 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5857 mtx_unlock(&lun->lun_lock); 5858 if (set_ua) { 5859 ctl_isc_announce_mode(lun, 5860 ctl_get_initindex(&ctsio->io_hdr.nexus), 5861 page_index->page_code, page_index->subpage); 5862 } 5863 return (CTL_RETVAL_COMPLETE); 5864} 5865 5866static void 5867ctl_ie_timer(void *arg) 5868{ 5869 struct ctl_lun *lun = arg; 5870 uint64_t t; 5871 5872 if (lun->ie_asc == 0) 5873 return; 5874 5875 if (lun->MODE_IE.mrie == SIEP_MRIE_UA) 5876 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5877 else 5878 lun->ie_reported = 0; 5879 5880 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) { 5881 lun->ie_reportcnt++; 5882 t = scsi_4btoul(lun->MODE_IE.interval_timer); 5883 if (t == 0 || t == UINT32_MAX) 5884 t = 3000; /* 5 min */ 5885 callout_schedule(&lun->ie_callout, t * hz / 10); 5886 } 5887} 5888 5889int 5890ctl_ie_page_handler(struct ctl_scsiio *ctsio, 5891 struct ctl_page_index *page_index, uint8_t *page_ptr) 5892{ 5893 struct scsi_info_exceptions_page *pg; 5894 struct ctl_lun *lun; 5895 uint64_t t; 5896 5897 (void)ctl_default_page_handler(ctsio, page_index, page_ptr); 5898 5899 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5900 pg = (struct scsi_info_exceptions_page *)page_ptr; 5901 mtx_lock(&lun->lun_lock); 5902 if (pg->info_flags & SIEP_FLAGS_TEST) { 5903 lun->ie_asc = 0x5d; 5904 lun->ie_ascq = 0xff; 5905 if (pg->mrie == SIEP_MRIE_UA) { 5906 ctl_est_ua_all(lun, -1, CTL_UA_IE); 5907 lun->ie_reported = 1; 5908 } else { 5909 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5910 lun->ie_reported = -1; 5911 } 5912 lun->ie_reportcnt = 1; 5913 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) { 5914 lun->ie_reportcnt++; 5915 t = scsi_4btoul(pg->interval_timer); 5916 if (t == 0 || t == UINT32_MAX) 5917 t = 3000; /* 5 min */ 5918 callout_reset(&lun->ie_callout, t * hz / 10, 5919 ctl_ie_timer, lun); 5920 } 5921 } else { 5922 lun->ie_asc = 0; 5923 lun->ie_ascq = 0; 5924 lun->ie_reported = 1; 5925 ctl_clr_ua_all(lun, -1, CTL_UA_IE); 5926 lun->ie_reportcnt = UINT32_MAX; 5927 callout_stop(&lun->ie_callout); 5928 } 5929 mtx_unlock(&lun->lun_lock); 5930 return (CTL_RETVAL_COMPLETE); 5931} 5932 5933static int 5934ctl_do_mode_select(union ctl_io *io) 5935{ 5936 struct scsi_mode_page_header *page_header; 5937 struct ctl_page_index *page_index; 5938 struct ctl_scsiio *ctsio; 5939 int page_len, page_len_offset, page_len_size; 5940 union ctl_modepage_info *modepage_info; 5941 struct ctl_lun *lun; 5942 int *len_left, *len_used; 5943 int retval, i; 5944 5945 ctsio = &io->scsiio; 5946 page_index = NULL; 5947 page_len = 0; 5948 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5949 5950 modepage_info = (union ctl_modepage_info *) 5951 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5952 len_left = &modepage_info->header.len_left; 5953 len_used = &modepage_info->header.len_used; 5954 5955do_next_page: 5956 5957 page_header = (struct scsi_mode_page_header *) 5958 (ctsio->kern_data_ptr + *len_used); 5959 5960 if (*len_left == 0) { 5961 free(ctsio->kern_data_ptr, M_CTL); 5962 ctl_set_success(ctsio); 5963 ctl_done((union ctl_io *)ctsio); 5964 return (CTL_RETVAL_COMPLETE); 5965 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5966 5967 free(ctsio->kern_data_ptr, M_CTL); 5968 ctl_set_param_len_error(ctsio); 5969 ctl_done((union ctl_io *)ctsio); 5970 return (CTL_RETVAL_COMPLETE); 5971 5972 } else if ((page_header->page_code & SMPH_SPF) 5973 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5974 5975 free(ctsio->kern_data_ptr, M_CTL); 5976 ctl_set_param_len_error(ctsio); 5977 ctl_done((union ctl_io *)ctsio); 5978 return (CTL_RETVAL_COMPLETE); 5979 } 5980 5981 5982 /* 5983 * XXX KDM should we do something with the block descriptor? 5984 */ 5985 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5986 page_index = &lun->mode_pages.index[i]; 5987 if (lun->be_lun->lun_type == T_DIRECT && 5988 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5989 continue; 5990 if (lun->be_lun->lun_type == T_PROCESSOR && 5991 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5992 continue; 5993 if (lun->be_lun->lun_type == T_CDROM && 5994 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5995 continue; 5996 5997 if ((page_index->page_code & SMPH_PC_MASK) != 5998 (page_header->page_code & SMPH_PC_MASK)) 5999 continue; 6000 6001 /* 6002 * If neither page has a subpage code, then we've got a 6003 * match. 6004 */ 6005 if (((page_index->page_code & SMPH_SPF) == 0) 6006 && ((page_header->page_code & SMPH_SPF) == 0)) { 6007 page_len = page_header->page_length; 6008 break; 6009 } 6010 6011 /* 6012 * If both pages have subpages, then the subpage numbers 6013 * have to match. 6014 */ 6015 if ((page_index->page_code & SMPH_SPF) 6016 && (page_header->page_code & SMPH_SPF)) { 6017 struct scsi_mode_page_header_sp *sph; 6018 6019 sph = (struct scsi_mode_page_header_sp *)page_header; 6020 if (page_index->subpage == sph->subpage) { 6021 page_len = scsi_2btoul(sph->page_length); 6022 break; 6023 } 6024 } 6025 } 6026 6027 /* 6028 * If we couldn't find the page, or if we don't have a mode select 6029 * handler for it, send back an error to the user. 6030 */ 6031 if ((i >= CTL_NUM_MODE_PAGES) 6032 || (page_index->select_handler == NULL)) { 6033 ctl_set_invalid_field(ctsio, 6034 /*sks_valid*/ 1, 6035 /*command*/ 0, 6036 /*field*/ *len_used, 6037 /*bit_valid*/ 0, 6038 /*bit*/ 0); 6039 free(ctsio->kern_data_ptr, M_CTL); 6040 ctl_done((union ctl_io *)ctsio); 6041 return (CTL_RETVAL_COMPLETE); 6042 } 6043 6044 if (page_index->page_code & SMPH_SPF) { 6045 page_len_offset = 2; 6046 page_len_size = 2; 6047 } else { 6048 page_len_size = 1; 6049 page_len_offset = 1; 6050 } 6051 6052 /* 6053 * If the length the initiator gives us isn't the one we specify in 6054 * the mode page header, or if they didn't specify enough data in 6055 * the CDB to avoid truncating this page, kick out the request. 6056 */ 6057 if (page_len != page_index->page_len - page_len_offset - page_len_size) { 6058 ctl_set_invalid_field(ctsio, 6059 /*sks_valid*/ 1, 6060 /*command*/ 0, 6061 /*field*/ *len_used + page_len_offset, 6062 /*bit_valid*/ 0, 6063 /*bit*/ 0); 6064 free(ctsio->kern_data_ptr, M_CTL); 6065 ctl_done((union ctl_io *)ctsio); 6066 return (CTL_RETVAL_COMPLETE); 6067 } 6068 if (*len_left < page_index->page_len) { 6069 free(ctsio->kern_data_ptr, M_CTL); 6070 ctl_set_param_len_error(ctsio); 6071 ctl_done((union ctl_io *)ctsio); 6072 return (CTL_RETVAL_COMPLETE); 6073 } 6074 6075 /* 6076 * Run through the mode page, checking to make sure that the bits 6077 * the user changed are actually legal for him to change. 6078 */ 6079 for (i = 0; i < page_index->page_len; i++) { 6080 uint8_t *user_byte, *change_mask, *current_byte; 6081 int bad_bit; 6082 int j; 6083 6084 user_byte = (uint8_t *)page_header + i; 6085 change_mask = page_index->page_data + 6086 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6087 current_byte = page_index->page_data + 6088 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6089 6090 /* 6091 * Check to see whether the user set any bits in this byte 6092 * that he is not allowed to set. 6093 */ 6094 if ((*user_byte & ~(*change_mask)) == 6095 (*current_byte & ~(*change_mask))) 6096 continue; 6097 6098 /* 6099 * Go through bit by bit to determine which one is illegal. 6100 */ 6101 bad_bit = 0; 6102 for (j = 7; j >= 0; j--) { 6103 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6104 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6105 bad_bit = i; 6106 break; 6107 } 6108 } 6109 ctl_set_invalid_field(ctsio, 6110 /*sks_valid*/ 1, 6111 /*command*/ 0, 6112 /*field*/ *len_used + i, 6113 /*bit_valid*/ 1, 6114 /*bit*/ bad_bit); 6115 free(ctsio->kern_data_ptr, M_CTL); 6116 ctl_done((union ctl_io *)ctsio); 6117 return (CTL_RETVAL_COMPLETE); 6118 } 6119 6120 /* 6121 * Decrement these before we call the page handler, since we may 6122 * end up getting called back one way or another before the handler 6123 * returns to this context. 6124 */ 6125 *len_left -= page_index->page_len; 6126 *len_used += page_index->page_len; 6127 6128 retval = page_index->select_handler(ctsio, page_index, 6129 (uint8_t *)page_header); 6130 6131 /* 6132 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6133 * wait until this queued command completes to finish processing 6134 * the mode page. If it returns anything other than 6135 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6136 * already set the sense information, freed the data pointer, and 6137 * completed the io for us. 6138 */ 6139 if (retval != CTL_RETVAL_COMPLETE) 6140 goto bailout_no_done; 6141 6142 /* 6143 * If the initiator sent us more than one page, parse the next one. 6144 */ 6145 if (*len_left > 0) 6146 goto do_next_page; 6147 6148 ctl_set_success(ctsio); 6149 free(ctsio->kern_data_ptr, M_CTL); 6150 ctl_done((union ctl_io *)ctsio); 6151 6152bailout_no_done: 6153 6154 return (CTL_RETVAL_COMPLETE); 6155 6156} 6157 6158int 6159ctl_mode_select(struct ctl_scsiio *ctsio) 6160{ 6161 int param_len, pf, sp; 6162 int header_size, bd_len; 6163 union ctl_modepage_info *modepage_info; 6164 6165 switch (ctsio->cdb[0]) { 6166 case MODE_SELECT_6: { 6167 struct scsi_mode_select_6 *cdb; 6168 6169 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6170 6171 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6172 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6173 param_len = cdb->length; 6174 header_size = sizeof(struct scsi_mode_header_6); 6175 break; 6176 } 6177 case MODE_SELECT_10: { 6178 struct scsi_mode_select_10 *cdb; 6179 6180 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6181 6182 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6183 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6184 param_len = scsi_2btoul(cdb->length); 6185 header_size = sizeof(struct scsi_mode_header_10); 6186 break; 6187 } 6188 default: 6189 ctl_set_invalid_opcode(ctsio); 6190 ctl_done((union ctl_io *)ctsio); 6191 return (CTL_RETVAL_COMPLETE); 6192 } 6193 6194 /* 6195 * From SPC-3: 6196 * "A parameter list length of zero indicates that the Data-Out Buffer 6197 * shall be empty. This condition shall not be considered as an error." 6198 */ 6199 if (param_len == 0) { 6200 ctl_set_success(ctsio); 6201 ctl_done((union ctl_io *)ctsio); 6202 return (CTL_RETVAL_COMPLETE); 6203 } 6204 6205 /* 6206 * Since we'll hit this the first time through, prior to 6207 * allocation, we don't need to free a data buffer here. 6208 */ 6209 if (param_len < header_size) { 6210 ctl_set_param_len_error(ctsio); 6211 ctl_done((union ctl_io *)ctsio); 6212 return (CTL_RETVAL_COMPLETE); 6213 } 6214 6215 /* 6216 * Allocate the data buffer and grab the user's data. In theory, 6217 * we shouldn't have to sanity check the parameter list length here 6218 * because the maximum size is 64K. We should be able to malloc 6219 * that much without too many problems. 6220 */ 6221 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6222 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6223 ctsio->kern_data_len = param_len; 6224 ctsio->kern_total_len = param_len; 6225 ctsio->kern_data_resid = 0; 6226 ctsio->kern_rel_offset = 0; 6227 ctsio->kern_sg_entries = 0; 6228 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6229 ctsio->be_move_done = ctl_config_move_done; 6230 ctl_datamove((union ctl_io *)ctsio); 6231 6232 return (CTL_RETVAL_COMPLETE); 6233 } 6234 6235 switch (ctsio->cdb[0]) { 6236 case MODE_SELECT_6: { 6237 struct scsi_mode_header_6 *mh6; 6238 6239 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6240 bd_len = mh6->blk_desc_len; 6241 break; 6242 } 6243 case MODE_SELECT_10: { 6244 struct scsi_mode_header_10 *mh10; 6245 6246 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6247 bd_len = scsi_2btoul(mh10->blk_desc_len); 6248 break; 6249 } 6250 default: 6251 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6252 } 6253 6254 if (param_len < (header_size + bd_len)) { 6255 free(ctsio->kern_data_ptr, M_CTL); 6256 ctl_set_param_len_error(ctsio); 6257 ctl_done((union ctl_io *)ctsio); 6258 return (CTL_RETVAL_COMPLETE); 6259 } 6260 6261 /* 6262 * Set the IO_CONT flag, so that if this I/O gets passed to 6263 * ctl_config_write_done(), it'll get passed back to 6264 * ctl_do_mode_select() for further processing, or completion if 6265 * we're all done. 6266 */ 6267 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6268 ctsio->io_cont = ctl_do_mode_select; 6269 6270 modepage_info = (union ctl_modepage_info *) 6271 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6272 memset(modepage_info, 0, sizeof(*modepage_info)); 6273 modepage_info->header.len_left = param_len - header_size - bd_len; 6274 modepage_info->header.len_used = header_size + bd_len; 6275 6276 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6277} 6278 6279int 6280ctl_mode_sense(struct ctl_scsiio *ctsio) 6281{ 6282 struct ctl_lun *lun; 6283 int pc, page_code, dbd, llba, subpage; 6284 int alloc_len, page_len, header_len, total_len; 6285 struct scsi_mode_block_descr *block_desc; 6286 struct ctl_page_index *page_index; 6287 6288 dbd = 0; 6289 llba = 0; 6290 block_desc = NULL; 6291 6292 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6293 6294 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6295 switch (ctsio->cdb[0]) { 6296 case MODE_SENSE_6: { 6297 struct scsi_mode_sense_6 *cdb; 6298 6299 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6300 6301 header_len = sizeof(struct scsi_mode_hdr_6); 6302 if (cdb->byte2 & SMS_DBD) 6303 dbd = 1; 6304 else 6305 header_len += sizeof(struct scsi_mode_block_descr); 6306 6307 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6308 page_code = cdb->page & SMS_PAGE_CODE; 6309 subpage = cdb->subpage; 6310 alloc_len = cdb->length; 6311 break; 6312 } 6313 case MODE_SENSE_10: { 6314 struct scsi_mode_sense_10 *cdb; 6315 6316 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6317 6318 header_len = sizeof(struct scsi_mode_hdr_10); 6319 6320 if (cdb->byte2 & SMS_DBD) 6321 dbd = 1; 6322 else 6323 header_len += sizeof(struct scsi_mode_block_descr); 6324 if (cdb->byte2 & SMS10_LLBAA) 6325 llba = 1; 6326 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6327 page_code = cdb->page & SMS_PAGE_CODE; 6328 subpage = cdb->subpage; 6329 alloc_len = scsi_2btoul(cdb->length); 6330 break; 6331 } 6332 default: 6333 ctl_set_invalid_opcode(ctsio); 6334 ctl_done((union ctl_io *)ctsio); 6335 return (CTL_RETVAL_COMPLETE); 6336 break; /* NOTREACHED */ 6337 } 6338 6339 /* 6340 * We have to make a first pass through to calculate the size of 6341 * the pages that match the user's query. Then we allocate enough 6342 * memory to hold it, and actually copy the data into the buffer. 6343 */ 6344 switch (page_code) { 6345 case SMS_ALL_PAGES_PAGE: { 6346 u_int i; 6347 6348 page_len = 0; 6349 6350 /* 6351 * At the moment, values other than 0 and 0xff here are 6352 * reserved according to SPC-3. 6353 */ 6354 if ((subpage != SMS_SUBPAGE_PAGE_0) 6355 && (subpage != SMS_SUBPAGE_ALL)) { 6356 ctl_set_invalid_field(ctsio, 6357 /*sks_valid*/ 1, 6358 /*command*/ 1, 6359 /*field*/ 3, 6360 /*bit_valid*/ 0, 6361 /*bit*/ 0); 6362 ctl_done((union ctl_io *)ctsio); 6363 return (CTL_RETVAL_COMPLETE); 6364 } 6365 6366 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6367 page_index = &lun->mode_pages.index[i]; 6368 6369 /* Make sure the page is supported for this dev type */ 6370 if (lun->be_lun->lun_type == T_DIRECT && 6371 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6372 continue; 6373 if (lun->be_lun->lun_type == T_PROCESSOR && 6374 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6375 continue; 6376 if (lun->be_lun->lun_type == T_CDROM && 6377 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6378 continue; 6379 6380 /* 6381 * We don't use this subpage if the user didn't 6382 * request all subpages. 6383 */ 6384 if ((page_index->subpage != 0) 6385 && (subpage == SMS_SUBPAGE_PAGE_0)) 6386 continue; 6387 6388#if 0 6389 printf("found page %#x len %d\n", 6390 page_index->page_code & SMPH_PC_MASK, 6391 page_index->page_len); 6392#endif 6393 page_len += page_index->page_len; 6394 } 6395 break; 6396 } 6397 default: { 6398 u_int i; 6399 6400 page_len = 0; 6401 6402 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6403 page_index = &lun->mode_pages.index[i]; 6404 6405 /* Make sure the page is supported for this dev type */ 6406 if (lun->be_lun->lun_type == T_DIRECT && 6407 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6408 continue; 6409 if (lun->be_lun->lun_type == T_PROCESSOR && 6410 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6411 continue; 6412 if (lun->be_lun->lun_type == T_CDROM && 6413 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6414 continue; 6415 6416 /* Look for the right page code */ 6417 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6418 continue; 6419 6420 /* Look for the right subpage or the subpage wildcard*/ 6421 if ((page_index->subpage != subpage) 6422 && (subpage != SMS_SUBPAGE_ALL)) 6423 continue; 6424 6425#if 0 6426 printf("found page %#x len %d\n", 6427 page_index->page_code & SMPH_PC_MASK, 6428 page_index->page_len); 6429#endif 6430 6431 page_len += page_index->page_len; 6432 } 6433 6434 if (page_len == 0) { 6435 ctl_set_invalid_field(ctsio, 6436 /*sks_valid*/ 1, 6437 /*command*/ 1, 6438 /*field*/ 2, 6439 /*bit_valid*/ 1, 6440 /*bit*/ 5); 6441 ctl_done((union ctl_io *)ctsio); 6442 return (CTL_RETVAL_COMPLETE); 6443 } 6444 break; 6445 } 6446 } 6447 6448 total_len = header_len + page_len; 6449#if 0 6450 printf("header_len = %d, page_len = %d, total_len = %d\n", 6451 header_len, page_len, total_len); 6452#endif 6453 6454 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6455 ctsio->kern_sg_entries = 0; 6456 ctsio->kern_data_resid = 0; 6457 ctsio->kern_rel_offset = 0; 6458 if (total_len < alloc_len) { 6459 ctsio->residual = alloc_len - total_len; 6460 ctsio->kern_data_len = total_len; 6461 ctsio->kern_total_len = total_len; 6462 } else { 6463 ctsio->residual = 0; 6464 ctsio->kern_data_len = alloc_len; 6465 ctsio->kern_total_len = alloc_len; 6466 } 6467 6468 switch (ctsio->cdb[0]) { 6469 case MODE_SENSE_6: { 6470 struct scsi_mode_hdr_6 *header; 6471 6472 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6473 6474 header->datalen = MIN(total_len - 1, 254); 6475 if (lun->be_lun->lun_type == T_DIRECT) { 6476 header->dev_specific = 0x10; /* DPOFUA */ 6477 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6478 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6479 header->dev_specific |= 0x80; /* WP */ 6480 } 6481 if (dbd) 6482 header->block_descr_len = 0; 6483 else 6484 header->block_descr_len = 6485 sizeof(struct scsi_mode_block_descr); 6486 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6487 break; 6488 } 6489 case MODE_SENSE_10: { 6490 struct scsi_mode_hdr_10 *header; 6491 int datalen; 6492 6493 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6494 6495 datalen = MIN(total_len - 2, 65533); 6496 scsi_ulto2b(datalen, header->datalen); 6497 if (lun->be_lun->lun_type == T_DIRECT) { 6498 header->dev_specific = 0x10; /* DPOFUA */ 6499 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6500 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) 6501 header->dev_specific |= 0x80; /* WP */ 6502 } 6503 if (dbd) 6504 scsi_ulto2b(0, header->block_descr_len); 6505 else 6506 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6507 header->block_descr_len); 6508 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6509 break; 6510 } 6511 default: 6512 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]); 6513 } 6514 6515 /* 6516 * If we've got a disk, use its blocksize in the block 6517 * descriptor. Otherwise, just set it to 0. 6518 */ 6519 if (dbd == 0) { 6520 if (lun->be_lun->lun_type == T_DIRECT) 6521 scsi_ulto3b(lun->be_lun->blocksize, 6522 block_desc->block_len); 6523 else 6524 scsi_ulto3b(0, block_desc->block_len); 6525 } 6526 6527 switch (page_code) { 6528 case SMS_ALL_PAGES_PAGE: { 6529 int i, data_used; 6530 6531 data_used = header_len; 6532 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6533 struct ctl_page_index *page_index; 6534 6535 page_index = &lun->mode_pages.index[i]; 6536 if (lun->be_lun->lun_type == T_DIRECT && 6537 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6538 continue; 6539 if (lun->be_lun->lun_type == T_PROCESSOR && 6540 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6541 continue; 6542 if (lun->be_lun->lun_type == T_CDROM && 6543 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6544 continue; 6545 6546 /* 6547 * We don't use this subpage if the user didn't 6548 * request all subpages. We already checked (above) 6549 * to make sure the user only specified a subpage 6550 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6551 */ 6552 if ((page_index->subpage != 0) 6553 && (subpage == SMS_SUBPAGE_PAGE_0)) 6554 continue; 6555 6556 /* 6557 * Call the handler, if it exists, to update the 6558 * page to the latest values. 6559 */ 6560 if (page_index->sense_handler != NULL) 6561 page_index->sense_handler(ctsio, page_index,pc); 6562 6563 memcpy(ctsio->kern_data_ptr + data_used, 6564 page_index->page_data + 6565 (page_index->page_len * pc), 6566 page_index->page_len); 6567 data_used += page_index->page_len; 6568 } 6569 break; 6570 } 6571 default: { 6572 int i, data_used; 6573 6574 data_used = header_len; 6575 6576 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6577 struct ctl_page_index *page_index; 6578 6579 page_index = &lun->mode_pages.index[i]; 6580 6581 /* Look for the right page code */ 6582 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6583 continue; 6584 6585 /* Look for the right subpage or the subpage wildcard*/ 6586 if ((page_index->subpage != subpage) 6587 && (subpage != SMS_SUBPAGE_ALL)) 6588 continue; 6589 6590 /* Make sure the page is supported for this dev type */ 6591 if (lun->be_lun->lun_type == T_DIRECT && 6592 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6593 continue; 6594 if (lun->be_lun->lun_type == T_PROCESSOR && 6595 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6596 continue; 6597 if (lun->be_lun->lun_type == T_CDROM && 6598 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6599 continue; 6600 6601 /* 6602 * Call the handler, if it exists, to update the 6603 * page to the latest values. 6604 */ 6605 if (page_index->sense_handler != NULL) 6606 page_index->sense_handler(ctsio, page_index,pc); 6607 6608 memcpy(ctsio->kern_data_ptr + data_used, 6609 page_index->page_data + 6610 (page_index->page_len * pc), 6611 page_index->page_len); 6612 data_used += page_index->page_len; 6613 } 6614 break; 6615 } 6616 } 6617 6618 ctl_set_success(ctsio); 6619 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6620 ctsio->be_move_done = ctl_config_move_done; 6621 ctl_datamove((union ctl_io *)ctsio); 6622 return (CTL_RETVAL_COMPLETE); 6623} 6624 6625int 6626ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6627 struct ctl_page_index *page_index, 6628 int pc) 6629{ 6630 struct ctl_lun *lun; 6631 struct scsi_log_param_header *phdr; 6632 uint8_t *data; 6633 uint64_t val; 6634 6635 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6636 data = page_index->page_data; 6637 6638 if (lun->backend->lun_attr != NULL && 6639 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6640 != UINT64_MAX) { 6641 phdr = (struct scsi_log_param_header *)data; 6642 scsi_ulto2b(0x0001, phdr->param_code); 6643 phdr->param_control = SLP_LBIN | SLP_LP; 6644 phdr->param_len = 8; 6645 data = (uint8_t *)(phdr + 1); 6646 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6647 data[4] = 0x02; /* per-pool */ 6648 data += phdr->param_len; 6649 } 6650 6651 if (lun->backend->lun_attr != NULL && 6652 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6653 != UINT64_MAX) { 6654 phdr = (struct scsi_log_param_header *)data; 6655 scsi_ulto2b(0x0002, phdr->param_code); 6656 phdr->param_control = SLP_LBIN | SLP_LP; 6657 phdr->param_len = 8; 6658 data = (uint8_t *)(phdr + 1); 6659 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6660 data[4] = 0x01; /* per-LUN */ 6661 data += phdr->param_len; 6662 } 6663 6664 if (lun->backend->lun_attr != NULL && 6665 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6666 != UINT64_MAX) { 6667 phdr = (struct scsi_log_param_header *)data; 6668 scsi_ulto2b(0x00f1, phdr->param_code); 6669 phdr->param_control = SLP_LBIN | SLP_LP; 6670 phdr->param_len = 8; 6671 data = (uint8_t *)(phdr + 1); 6672 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6673 data[4] = 0x02; /* per-pool */ 6674 data += phdr->param_len; 6675 } 6676 6677 if (lun->backend->lun_attr != NULL && 6678 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6679 != UINT64_MAX) { 6680 phdr = (struct scsi_log_param_header *)data; 6681 scsi_ulto2b(0x00f2, phdr->param_code); 6682 phdr->param_control = SLP_LBIN | SLP_LP; 6683 phdr->param_len = 8; 6684 data = (uint8_t *)(phdr + 1); 6685 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6686 data[4] = 0x02; /* per-pool */ 6687 data += phdr->param_len; 6688 } 6689 6690 page_index->page_len = data - page_index->page_data; 6691 return (0); 6692} 6693 6694int 6695ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6696 struct ctl_page_index *page_index, 6697 int pc) 6698{ 6699 struct ctl_lun *lun; 6700 struct stat_page *data; 6701 uint64_t rn, wn, rb, wb; 6702 struct bintime rt, wt; 6703 int i; 6704 6705 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6706 data = (struct stat_page *)page_index->page_data; 6707 6708 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6709 data->sap.hdr.param_control = SLP_LBIN; 6710 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6711 sizeof(struct scsi_log_param_header); 6712 rn = wn = rb = wb = 0; 6713 bintime_clear(&rt); 6714 bintime_clear(&wt); 6715 for (i = 0; i < CTL_MAX_PORTS; i++) { 6716 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6717 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6718 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6719 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6720 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6721 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6722 } 6723 scsi_u64to8b(rn, data->sap.read_num); 6724 scsi_u64to8b(wn, data->sap.write_num); 6725 if (lun->stats.blocksize > 0) { 6726 scsi_u64to8b(wb / lun->stats.blocksize, 6727 data->sap.recvieved_lba); 6728 scsi_u64to8b(rb / lun->stats.blocksize, 6729 data->sap.transmitted_lba); 6730 } 6731 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6732 data->sap.read_int); 6733 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6734 data->sap.write_int); 6735 scsi_u64to8b(0, data->sap.weighted_num); 6736 scsi_u64to8b(0, data->sap.weighted_int); 6737 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6738 data->it.hdr.param_control = SLP_LBIN; 6739 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6740 sizeof(struct scsi_log_param_header); 6741#ifdef CTL_TIME_IO 6742 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6743#endif 6744 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6745 data->it.hdr.param_control = SLP_LBIN; 6746 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6747 sizeof(struct scsi_log_param_header); 6748 scsi_ulto4b(3, data->ti.exponent); 6749 scsi_ulto4b(1, data->ti.integer); 6750 return (0); 6751} 6752 6753int 6754ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio, 6755 struct ctl_page_index *page_index, 6756 int pc) 6757{ 6758 struct ctl_lun *lun; 6759 struct scsi_log_informational_exceptions *data; 6760 6761 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6762 data = (struct scsi_log_informational_exceptions *)page_index->page_data; 6763 6764 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code); 6765 data->hdr.param_control = SLP_LBIN; 6766 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) - 6767 sizeof(struct scsi_log_param_header); 6768 data->ie_asc = lun->ie_asc; 6769 data->ie_ascq = lun->ie_ascq; 6770 data->temperature = 0xff; 6771 return (0); 6772} 6773 6774int 6775ctl_log_sense(struct ctl_scsiio *ctsio) 6776{ 6777 struct ctl_lun *lun; 6778 int i, pc, page_code, subpage; 6779 int alloc_len, total_len; 6780 struct ctl_page_index *page_index; 6781 struct scsi_log_sense *cdb; 6782 struct scsi_log_header *header; 6783 6784 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6785 6786 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6787 cdb = (struct scsi_log_sense *)ctsio->cdb; 6788 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6789 page_code = cdb->page & SLS_PAGE_CODE; 6790 subpage = cdb->subpage; 6791 alloc_len = scsi_2btoul(cdb->length); 6792 6793 page_index = NULL; 6794 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6795 page_index = &lun->log_pages.index[i]; 6796 6797 /* Look for the right page code */ 6798 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6799 continue; 6800 6801 /* Look for the right subpage or the subpage wildcard*/ 6802 if (page_index->subpage != subpage) 6803 continue; 6804 6805 break; 6806 } 6807 if (i >= CTL_NUM_LOG_PAGES) { 6808 ctl_set_invalid_field(ctsio, 6809 /*sks_valid*/ 1, 6810 /*command*/ 1, 6811 /*field*/ 2, 6812 /*bit_valid*/ 0, 6813 /*bit*/ 0); 6814 ctl_done((union ctl_io *)ctsio); 6815 return (CTL_RETVAL_COMPLETE); 6816 } 6817 6818 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6819 6820 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6821 ctsio->kern_sg_entries = 0; 6822 ctsio->kern_data_resid = 0; 6823 ctsio->kern_rel_offset = 0; 6824 if (total_len < alloc_len) { 6825 ctsio->residual = alloc_len - total_len; 6826 ctsio->kern_data_len = total_len; 6827 ctsio->kern_total_len = total_len; 6828 } else { 6829 ctsio->residual = 0; 6830 ctsio->kern_data_len = alloc_len; 6831 ctsio->kern_total_len = alloc_len; 6832 } 6833 6834 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6835 header->page = page_index->page_code; 6836 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING) 6837 header->page |= SL_DS; 6838 if (page_index->subpage) { 6839 header->page |= SL_SPF; 6840 header->subpage = page_index->subpage; 6841 } 6842 scsi_ulto2b(page_index->page_len, header->datalen); 6843 6844 /* 6845 * Call the handler, if it exists, to update the 6846 * page to the latest values. 6847 */ 6848 if (page_index->sense_handler != NULL) 6849 page_index->sense_handler(ctsio, page_index, pc); 6850 6851 memcpy(header + 1, page_index->page_data, page_index->page_len); 6852 6853 ctl_set_success(ctsio); 6854 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6855 ctsio->be_move_done = ctl_config_move_done; 6856 ctl_datamove((union ctl_io *)ctsio); 6857 return (CTL_RETVAL_COMPLETE); 6858} 6859 6860int 6861ctl_read_capacity(struct ctl_scsiio *ctsio) 6862{ 6863 struct scsi_read_capacity *cdb; 6864 struct scsi_read_capacity_data *data; 6865 struct ctl_lun *lun; 6866 uint32_t lba; 6867 6868 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6869 6870 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6871 6872 lba = scsi_4btoul(cdb->addr); 6873 if (((cdb->pmi & SRC_PMI) == 0) 6874 && (lba != 0)) { 6875 ctl_set_invalid_field(/*ctsio*/ ctsio, 6876 /*sks_valid*/ 1, 6877 /*command*/ 1, 6878 /*field*/ 2, 6879 /*bit_valid*/ 0, 6880 /*bit*/ 0); 6881 ctl_done((union ctl_io *)ctsio); 6882 return (CTL_RETVAL_COMPLETE); 6883 } 6884 6885 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6886 6887 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6888 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6889 ctsio->residual = 0; 6890 ctsio->kern_data_len = sizeof(*data); 6891 ctsio->kern_total_len = sizeof(*data); 6892 ctsio->kern_data_resid = 0; 6893 ctsio->kern_rel_offset = 0; 6894 ctsio->kern_sg_entries = 0; 6895 6896 /* 6897 * If the maximum LBA is greater than 0xfffffffe, the user must 6898 * issue a SERVICE ACTION IN (16) command, with the read capacity 6899 * serivce action set. 6900 */ 6901 if (lun->be_lun->maxlba > 0xfffffffe) 6902 scsi_ulto4b(0xffffffff, data->addr); 6903 else 6904 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6905 6906 /* 6907 * XXX KDM this may not be 512 bytes... 6908 */ 6909 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6910 6911 ctl_set_success(ctsio); 6912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6913 ctsio->be_move_done = ctl_config_move_done; 6914 ctl_datamove((union ctl_io *)ctsio); 6915 return (CTL_RETVAL_COMPLETE); 6916} 6917 6918int 6919ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6920{ 6921 struct scsi_read_capacity_16 *cdb; 6922 struct scsi_read_capacity_data_long *data; 6923 struct ctl_lun *lun; 6924 uint64_t lba; 6925 uint32_t alloc_len; 6926 6927 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6928 6929 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6930 6931 alloc_len = scsi_4btoul(cdb->alloc_len); 6932 lba = scsi_8btou64(cdb->addr); 6933 6934 if ((cdb->reladr & SRC16_PMI) 6935 && (lba != 0)) { 6936 ctl_set_invalid_field(/*ctsio*/ ctsio, 6937 /*sks_valid*/ 1, 6938 /*command*/ 1, 6939 /*field*/ 2, 6940 /*bit_valid*/ 0, 6941 /*bit*/ 0); 6942 ctl_done((union ctl_io *)ctsio); 6943 return (CTL_RETVAL_COMPLETE); 6944 } 6945 6946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6947 6948 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6949 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6950 6951 if (sizeof(*data) < alloc_len) { 6952 ctsio->residual = alloc_len - sizeof(*data); 6953 ctsio->kern_data_len = sizeof(*data); 6954 ctsio->kern_total_len = sizeof(*data); 6955 } else { 6956 ctsio->residual = 0; 6957 ctsio->kern_data_len = alloc_len; 6958 ctsio->kern_total_len = alloc_len; 6959 } 6960 ctsio->kern_data_resid = 0; 6961 ctsio->kern_rel_offset = 0; 6962 ctsio->kern_sg_entries = 0; 6963 6964 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6965 /* XXX KDM this may not be 512 bytes... */ 6966 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6967 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6968 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6969 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6970 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6971 6972 ctl_set_success(ctsio); 6973 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6974 ctsio->be_move_done = ctl_config_move_done; 6975 ctl_datamove((union ctl_io *)ctsio); 6976 return (CTL_RETVAL_COMPLETE); 6977} 6978 6979int 6980ctl_get_lba_status(struct ctl_scsiio *ctsio) 6981{ 6982 struct scsi_get_lba_status *cdb; 6983 struct scsi_get_lba_status_data *data; 6984 struct ctl_lun *lun; 6985 struct ctl_lba_len_flags *lbalen; 6986 uint64_t lba; 6987 uint32_t alloc_len, total_len; 6988 int retval; 6989 6990 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6991 6992 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6993 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6994 lba = scsi_8btou64(cdb->addr); 6995 alloc_len = scsi_4btoul(cdb->alloc_len); 6996 6997 if (lba > lun->be_lun->maxlba) { 6998 ctl_set_lba_out_of_range(ctsio, lba); 6999 ctl_done((union ctl_io *)ctsio); 7000 return (CTL_RETVAL_COMPLETE); 7001 } 7002 7003 total_len = sizeof(*data) + sizeof(data->descr[0]); 7004 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7005 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7006 7007 if (total_len < alloc_len) { 7008 ctsio->residual = alloc_len - total_len; 7009 ctsio->kern_data_len = total_len; 7010 ctsio->kern_total_len = total_len; 7011 } else { 7012 ctsio->residual = 0; 7013 ctsio->kern_data_len = alloc_len; 7014 ctsio->kern_total_len = alloc_len; 7015 } 7016 ctsio->kern_data_resid = 0; 7017 ctsio->kern_rel_offset = 0; 7018 ctsio->kern_sg_entries = 0; 7019 7020 /* Fill dummy data in case backend can't tell anything. */ 7021 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7022 scsi_u64to8b(lba, data->descr[0].addr); 7023 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7024 data->descr[0].length); 7025 data->descr[0].status = 0; /* Mapped or unknown. */ 7026 7027 ctl_set_success(ctsio); 7028 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7029 ctsio->be_move_done = ctl_config_move_done; 7030 7031 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7032 lbalen->lba = lba; 7033 lbalen->len = total_len; 7034 lbalen->flags = 0; 7035 retval = lun->backend->config_read((union ctl_io *)ctsio); 7036 return (CTL_RETVAL_COMPLETE); 7037} 7038 7039int 7040ctl_read_defect(struct ctl_scsiio *ctsio) 7041{ 7042 struct scsi_read_defect_data_10 *ccb10; 7043 struct scsi_read_defect_data_12 *ccb12; 7044 struct scsi_read_defect_data_hdr_10 *data10; 7045 struct scsi_read_defect_data_hdr_12 *data12; 7046 uint32_t alloc_len, data_len; 7047 uint8_t format; 7048 7049 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7050 7051 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7052 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7053 format = ccb10->format; 7054 alloc_len = scsi_2btoul(ccb10->alloc_length); 7055 data_len = sizeof(*data10); 7056 } else { 7057 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7058 format = ccb12->format; 7059 alloc_len = scsi_4btoul(ccb12->alloc_length); 7060 data_len = sizeof(*data12); 7061 } 7062 if (alloc_len == 0) { 7063 ctl_set_success(ctsio); 7064 ctl_done((union ctl_io *)ctsio); 7065 return (CTL_RETVAL_COMPLETE); 7066 } 7067 7068 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7069 if (data_len < alloc_len) { 7070 ctsio->residual = alloc_len - data_len; 7071 ctsio->kern_data_len = data_len; 7072 ctsio->kern_total_len = data_len; 7073 } else { 7074 ctsio->residual = 0; 7075 ctsio->kern_data_len = alloc_len; 7076 ctsio->kern_total_len = alloc_len; 7077 } 7078 ctsio->kern_data_resid = 0; 7079 ctsio->kern_rel_offset = 0; 7080 ctsio->kern_sg_entries = 0; 7081 7082 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7083 data10 = (struct scsi_read_defect_data_hdr_10 *) 7084 ctsio->kern_data_ptr; 7085 data10->format = format; 7086 scsi_ulto2b(0, data10->length); 7087 } else { 7088 data12 = (struct scsi_read_defect_data_hdr_12 *) 7089 ctsio->kern_data_ptr; 7090 data12->format = format; 7091 scsi_ulto2b(0, data12->generation); 7092 scsi_ulto4b(0, data12->length); 7093 } 7094 7095 ctl_set_success(ctsio); 7096 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7097 ctsio->be_move_done = ctl_config_move_done; 7098 ctl_datamove((union ctl_io *)ctsio); 7099 return (CTL_RETVAL_COMPLETE); 7100} 7101 7102int 7103ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7104{ 7105 struct scsi_maintenance_in *cdb; 7106 int retval; 7107 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os; 7108 int num_ha_groups, num_target_ports, shared_group; 7109 struct ctl_lun *lun; 7110 struct ctl_softc *softc; 7111 struct ctl_port *port; 7112 struct scsi_target_group_data *rtg_ptr; 7113 struct scsi_target_group_data_extended *rtg_ext_ptr; 7114 struct scsi_target_port_group_descriptor *tpg_desc; 7115 7116 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7117 7118 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7119 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7120 softc = lun->ctl_softc; 7121 7122 retval = CTL_RETVAL_COMPLETE; 7123 7124 switch (cdb->byte2 & STG_PDF_MASK) { 7125 case STG_PDF_LENGTH: 7126 ext = 0; 7127 break; 7128 case STG_PDF_EXTENDED: 7129 ext = 1; 7130 break; 7131 default: 7132 ctl_set_invalid_field(/*ctsio*/ ctsio, 7133 /*sks_valid*/ 1, 7134 /*command*/ 1, 7135 /*field*/ 2, 7136 /*bit_valid*/ 1, 7137 /*bit*/ 5); 7138 ctl_done((union ctl_io *)ctsio); 7139 return(retval); 7140 } 7141 7142 num_target_ports = 0; 7143 shared_group = (softc->is_single != 0); 7144 mtx_lock(&softc->ctl_lock); 7145 STAILQ_FOREACH(port, &softc->port_list, links) { 7146 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7147 continue; 7148 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7149 continue; 7150 num_target_ports++; 7151 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7152 shared_group = 1; 7153 } 7154 mtx_unlock(&softc->ctl_lock); 7155 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES; 7156 7157 if (ext) 7158 total_len = sizeof(struct scsi_target_group_data_extended); 7159 else 7160 total_len = sizeof(struct scsi_target_group_data); 7161 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7162 (shared_group + num_ha_groups) + 7163 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7164 7165 alloc_len = scsi_4btoul(cdb->length); 7166 7167 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7168 7169 ctsio->kern_sg_entries = 0; 7170 7171 if (total_len < alloc_len) { 7172 ctsio->residual = alloc_len - total_len; 7173 ctsio->kern_data_len = total_len; 7174 ctsio->kern_total_len = total_len; 7175 } else { 7176 ctsio->residual = 0; 7177 ctsio->kern_data_len = alloc_len; 7178 ctsio->kern_total_len = alloc_len; 7179 } 7180 ctsio->kern_data_resid = 0; 7181 ctsio->kern_rel_offset = 0; 7182 7183 if (ext) { 7184 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7185 ctsio->kern_data_ptr; 7186 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7187 rtg_ext_ptr->format_type = 0x10; 7188 rtg_ext_ptr->implicit_transition_time = 0; 7189 tpg_desc = &rtg_ext_ptr->groups[0]; 7190 } else { 7191 rtg_ptr = (struct scsi_target_group_data *) 7192 ctsio->kern_data_ptr; 7193 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7194 tpg_desc = &rtg_ptr->groups[0]; 7195 } 7196 7197 mtx_lock(&softc->ctl_lock); 7198 pg = softc->port_min / softc->port_cnt; 7199 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) { 7200 /* Some shelf is known to be primary. */ 7201 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7202 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7203 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7204 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7205 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7206 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7207 else 7208 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7209 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7210 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7211 } else { 7212 ts = os; 7213 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7214 } 7215 } else { 7216 /* No known primary shelf. */ 7217 if (softc->ha_link == CTL_HA_LINK_OFFLINE) { 7218 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7219 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7220 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) { 7221 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7222 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7223 } else { 7224 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7225 } 7226 } 7227 if (shared_group) { 7228 tpg_desc->pref_state = ts; 7229 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7230 TPG_U_SUP | TPG_T_SUP; 7231 scsi_ulto2b(1, tpg_desc->target_port_group); 7232 tpg_desc->status = TPG_IMPLICIT; 7233 pc = 0; 7234 STAILQ_FOREACH(port, &softc->port_list, links) { 7235 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7236 continue; 7237 if (!softc->is_single && 7238 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0) 7239 continue; 7240 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7241 continue; 7242 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7243 relative_target_port_identifier); 7244 pc++; 7245 } 7246 tpg_desc->target_port_count = pc; 7247 tpg_desc = (struct scsi_target_port_group_descriptor *) 7248 &tpg_desc->descriptors[pc]; 7249 } 7250 for (g = 0; g < num_ha_groups; g++) { 7251 tpg_desc->pref_state = (g == pg) ? ts : os; 7252 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7253 TPG_U_SUP | TPG_T_SUP; 7254 scsi_ulto2b(2 + g, tpg_desc->target_port_group); 7255 tpg_desc->status = TPG_IMPLICIT; 7256 pc = 0; 7257 STAILQ_FOREACH(port, &softc->port_list, links) { 7258 if (port->targ_port < g * softc->port_cnt || 7259 port->targ_port >= (g + 1) * softc->port_cnt) 7260 continue; 7261 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7262 continue; 7263 if (port->status & CTL_PORT_STATUS_HA_SHARED) 7264 continue; 7265 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 7266 continue; 7267 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7268 relative_target_port_identifier); 7269 pc++; 7270 } 7271 tpg_desc->target_port_count = pc; 7272 tpg_desc = (struct scsi_target_port_group_descriptor *) 7273 &tpg_desc->descriptors[pc]; 7274 } 7275 mtx_unlock(&softc->ctl_lock); 7276 7277 ctl_set_success(ctsio); 7278 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7279 ctsio->be_move_done = ctl_config_move_done; 7280 ctl_datamove((union ctl_io *)ctsio); 7281 return(retval); 7282} 7283 7284int 7285ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7286{ 7287 struct ctl_lun *lun; 7288 struct scsi_report_supported_opcodes *cdb; 7289 const struct ctl_cmd_entry *entry, *sentry; 7290 struct scsi_report_supported_opcodes_all *all; 7291 struct scsi_report_supported_opcodes_descr *descr; 7292 struct scsi_report_supported_opcodes_one *one; 7293 int retval; 7294 int alloc_len, total_len; 7295 int opcode, service_action, i, j, num; 7296 7297 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7298 7299 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7301 7302 retval = CTL_RETVAL_COMPLETE; 7303 7304 opcode = cdb->requested_opcode; 7305 service_action = scsi_2btoul(cdb->requested_service_action); 7306 switch (cdb->options & RSO_OPTIONS_MASK) { 7307 case RSO_OPTIONS_ALL: 7308 num = 0; 7309 for (i = 0; i < 256; i++) { 7310 entry = &ctl_cmd_table[i]; 7311 if (entry->flags & CTL_CMD_FLAG_SA5) { 7312 for (j = 0; j < 32; j++) { 7313 sentry = &((const struct ctl_cmd_entry *) 7314 entry->execute)[j]; 7315 if (ctl_cmd_applicable( 7316 lun->be_lun->lun_type, sentry)) 7317 num++; 7318 } 7319 } else { 7320 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7321 entry)) 7322 num++; 7323 } 7324 } 7325 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7326 num * sizeof(struct scsi_report_supported_opcodes_descr); 7327 break; 7328 case RSO_OPTIONS_OC: 7329 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7330 ctl_set_invalid_field(/*ctsio*/ ctsio, 7331 /*sks_valid*/ 1, 7332 /*command*/ 1, 7333 /*field*/ 2, 7334 /*bit_valid*/ 1, 7335 /*bit*/ 2); 7336 ctl_done((union ctl_io *)ctsio); 7337 return (CTL_RETVAL_COMPLETE); 7338 } 7339 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7340 break; 7341 case RSO_OPTIONS_OC_SA: 7342 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7343 service_action >= 32) { 7344 ctl_set_invalid_field(/*ctsio*/ ctsio, 7345 /*sks_valid*/ 1, 7346 /*command*/ 1, 7347 /*field*/ 2, 7348 /*bit_valid*/ 1, 7349 /*bit*/ 2); 7350 ctl_done((union ctl_io *)ctsio); 7351 return (CTL_RETVAL_COMPLETE); 7352 } 7353 /* FALLTHROUGH */ 7354 case RSO_OPTIONS_OC_ASA: 7355 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7356 break; 7357 default: 7358 ctl_set_invalid_field(/*ctsio*/ ctsio, 7359 /*sks_valid*/ 1, 7360 /*command*/ 1, 7361 /*field*/ 2, 7362 /*bit_valid*/ 1, 7363 /*bit*/ 2); 7364 ctl_done((union ctl_io *)ctsio); 7365 return (CTL_RETVAL_COMPLETE); 7366 } 7367 7368 alloc_len = scsi_4btoul(cdb->length); 7369 7370 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7371 7372 ctsio->kern_sg_entries = 0; 7373 7374 if (total_len < alloc_len) { 7375 ctsio->residual = alloc_len - total_len; 7376 ctsio->kern_data_len = total_len; 7377 ctsio->kern_total_len = total_len; 7378 } else { 7379 ctsio->residual = 0; 7380 ctsio->kern_data_len = alloc_len; 7381 ctsio->kern_total_len = alloc_len; 7382 } 7383 ctsio->kern_data_resid = 0; 7384 ctsio->kern_rel_offset = 0; 7385 7386 switch (cdb->options & RSO_OPTIONS_MASK) { 7387 case RSO_OPTIONS_ALL: 7388 all = (struct scsi_report_supported_opcodes_all *) 7389 ctsio->kern_data_ptr; 7390 num = 0; 7391 for (i = 0; i < 256; i++) { 7392 entry = &ctl_cmd_table[i]; 7393 if (entry->flags & CTL_CMD_FLAG_SA5) { 7394 for (j = 0; j < 32; j++) { 7395 sentry = &((const struct ctl_cmd_entry *) 7396 entry->execute)[j]; 7397 if (!ctl_cmd_applicable( 7398 lun->be_lun->lun_type, sentry)) 7399 continue; 7400 descr = &all->descr[num++]; 7401 descr->opcode = i; 7402 scsi_ulto2b(j, descr->service_action); 7403 descr->flags = RSO_SERVACTV; 7404 scsi_ulto2b(sentry->length, 7405 descr->cdb_length); 7406 } 7407 } else { 7408 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7409 entry)) 7410 continue; 7411 descr = &all->descr[num++]; 7412 descr->opcode = i; 7413 scsi_ulto2b(0, descr->service_action); 7414 descr->flags = 0; 7415 scsi_ulto2b(entry->length, descr->cdb_length); 7416 } 7417 } 7418 scsi_ulto4b( 7419 num * sizeof(struct scsi_report_supported_opcodes_descr), 7420 all->length); 7421 break; 7422 case RSO_OPTIONS_OC: 7423 one = (struct scsi_report_supported_opcodes_one *) 7424 ctsio->kern_data_ptr; 7425 entry = &ctl_cmd_table[opcode]; 7426 goto fill_one; 7427 case RSO_OPTIONS_OC_SA: 7428 one = (struct scsi_report_supported_opcodes_one *) 7429 ctsio->kern_data_ptr; 7430 entry = &ctl_cmd_table[opcode]; 7431 entry = &((const struct ctl_cmd_entry *) 7432 entry->execute)[service_action]; 7433fill_one: 7434 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7435 one->support = 3; 7436 scsi_ulto2b(entry->length, one->cdb_length); 7437 one->cdb_usage[0] = opcode; 7438 memcpy(&one->cdb_usage[1], entry->usage, 7439 entry->length - 1); 7440 } else 7441 one->support = 1; 7442 break; 7443 case RSO_OPTIONS_OC_ASA: 7444 one = (struct scsi_report_supported_opcodes_one *) 7445 ctsio->kern_data_ptr; 7446 entry = &ctl_cmd_table[opcode]; 7447 if (entry->flags & CTL_CMD_FLAG_SA5) { 7448 entry = &((const struct ctl_cmd_entry *) 7449 entry->execute)[service_action]; 7450 } else if (service_action != 0) { 7451 one->support = 1; 7452 break; 7453 } 7454 goto fill_one; 7455 } 7456 7457 ctl_set_success(ctsio); 7458 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7459 ctsio->be_move_done = ctl_config_move_done; 7460 ctl_datamove((union ctl_io *)ctsio); 7461 return(retval); 7462} 7463 7464int 7465ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7466{ 7467 struct scsi_report_supported_tmf *cdb; 7468 struct scsi_report_supported_tmf_ext_data *data; 7469 int retval; 7470 int alloc_len, total_len; 7471 7472 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7473 7474 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7475 7476 retval = CTL_RETVAL_COMPLETE; 7477 7478 if (cdb->options & RST_REPD) 7479 total_len = sizeof(struct scsi_report_supported_tmf_ext_data); 7480 else 7481 total_len = sizeof(struct scsi_report_supported_tmf_data); 7482 alloc_len = scsi_4btoul(cdb->length); 7483 7484 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7485 7486 ctsio->kern_sg_entries = 0; 7487 7488 if (total_len < alloc_len) { 7489 ctsio->residual = alloc_len - total_len; 7490 ctsio->kern_data_len = total_len; 7491 ctsio->kern_total_len = total_len; 7492 } else { 7493 ctsio->residual = 0; 7494 ctsio->kern_data_len = alloc_len; 7495 ctsio->kern_total_len = alloc_len; 7496 } 7497 ctsio->kern_data_resid = 0; 7498 ctsio->kern_rel_offset = 0; 7499 7500 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr; 7501 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7502 RST_TRS; 7503 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7504 data->length = total_len - 4; 7505 7506 ctl_set_success(ctsio); 7507 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7508 ctsio->be_move_done = ctl_config_move_done; 7509 ctl_datamove((union ctl_io *)ctsio); 7510 return (retval); 7511} 7512 7513int 7514ctl_report_timestamp(struct ctl_scsiio *ctsio) 7515{ 7516 struct scsi_report_timestamp *cdb; 7517 struct scsi_report_timestamp_data *data; 7518 struct timeval tv; 7519 int64_t timestamp; 7520 int retval; 7521 int alloc_len, total_len; 7522 7523 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7524 7525 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7526 7527 retval = CTL_RETVAL_COMPLETE; 7528 7529 total_len = sizeof(struct scsi_report_timestamp_data); 7530 alloc_len = scsi_4btoul(cdb->length); 7531 7532 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7533 7534 ctsio->kern_sg_entries = 0; 7535 7536 if (total_len < alloc_len) { 7537 ctsio->residual = alloc_len - total_len; 7538 ctsio->kern_data_len = total_len; 7539 ctsio->kern_total_len = total_len; 7540 } else { 7541 ctsio->residual = 0; 7542 ctsio->kern_data_len = alloc_len; 7543 ctsio->kern_total_len = alloc_len; 7544 } 7545 ctsio->kern_data_resid = 0; 7546 ctsio->kern_rel_offset = 0; 7547 7548 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7549 scsi_ulto2b(sizeof(*data) - 2, data->length); 7550 data->origin = RTS_ORIG_OUTSIDE; 7551 getmicrotime(&tv); 7552 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7553 scsi_ulto4b(timestamp >> 16, data->timestamp); 7554 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7555 7556 ctl_set_success(ctsio); 7557 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7558 ctsio->be_move_done = ctl_config_move_done; 7559 ctl_datamove((union ctl_io *)ctsio); 7560 return (retval); 7561} 7562 7563int 7564ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7565{ 7566 struct scsi_per_res_in *cdb; 7567 int alloc_len, total_len = 0; 7568 /* struct scsi_per_res_in_rsrv in_data; */ 7569 struct ctl_lun *lun; 7570 struct ctl_softc *softc; 7571 uint64_t key; 7572 7573 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7574 7575 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7576 7577 alloc_len = scsi_2btoul(cdb->length); 7578 7579 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7580 softc = lun->ctl_softc; 7581 7582retry: 7583 mtx_lock(&lun->lun_lock); 7584 switch (cdb->action) { 7585 case SPRI_RK: /* read keys */ 7586 total_len = sizeof(struct scsi_per_res_in_keys) + 7587 lun->pr_key_count * 7588 sizeof(struct scsi_per_res_key); 7589 break; 7590 case SPRI_RR: /* read reservation */ 7591 if (lun->flags & CTL_LUN_PR_RESERVED) 7592 total_len = sizeof(struct scsi_per_res_in_rsrv); 7593 else 7594 total_len = sizeof(struct scsi_per_res_in_header); 7595 break; 7596 case SPRI_RC: /* report capabilities */ 7597 total_len = sizeof(struct scsi_per_res_cap); 7598 break; 7599 case SPRI_RS: /* read full status */ 7600 total_len = sizeof(struct scsi_per_res_in_header) + 7601 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7602 lun->pr_key_count; 7603 break; 7604 default: 7605 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7606 } 7607 mtx_unlock(&lun->lun_lock); 7608 7609 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7610 7611 if (total_len < alloc_len) { 7612 ctsio->residual = alloc_len - total_len; 7613 ctsio->kern_data_len = total_len; 7614 ctsio->kern_total_len = total_len; 7615 } else { 7616 ctsio->residual = 0; 7617 ctsio->kern_data_len = alloc_len; 7618 ctsio->kern_total_len = alloc_len; 7619 } 7620 7621 ctsio->kern_data_resid = 0; 7622 ctsio->kern_rel_offset = 0; 7623 ctsio->kern_sg_entries = 0; 7624 7625 mtx_lock(&lun->lun_lock); 7626 switch (cdb->action) { 7627 case SPRI_RK: { // read keys 7628 struct scsi_per_res_in_keys *res_keys; 7629 int i, key_count; 7630 7631 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7632 7633 /* 7634 * We had to drop the lock to allocate our buffer, which 7635 * leaves time for someone to come in with another 7636 * persistent reservation. (That is unlikely, though, 7637 * since this should be the only persistent reservation 7638 * command active right now.) 7639 */ 7640 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7641 (lun->pr_key_count * 7642 sizeof(struct scsi_per_res_key)))){ 7643 mtx_unlock(&lun->lun_lock); 7644 free(ctsio->kern_data_ptr, M_CTL); 7645 printf("%s: reservation length changed, retrying\n", 7646 __func__); 7647 goto retry; 7648 } 7649 7650 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7651 7652 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7653 lun->pr_key_count, res_keys->header.length); 7654 7655 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7656 if ((key = ctl_get_prkey(lun, i)) == 0) 7657 continue; 7658 7659 /* 7660 * We used lun->pr_key_count to calculate the 7661 * size to allocate. If it turns out the number of 7662 * initiators with the registered flag set is 7663 * larger than that (i.e. they haven't been kept in 7664 * sync), we've got a problem. 7665 */ 7666 if (key_count >= lun->pr_key_count) { 7667 key_count++; 7668 continue; 7669 } 7670 scsi_u64to8b(key, res_keys->keys[key_count].key); 7671 key_count++; 7672 } 7673 break; 7674 } 7675 case SPRI_RR: { // read reservation 7676 struct scsi_per_res_in_rsrv *res; 7677 int tmp_len, header_only; 7678 7679 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7680 7681 scsi_ulto4b(lun->pr_generation, res->header.generation); 7682 7683 if (lun->flags & CTL_LUN_PR_RESERVED) 7684 { 7685 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7686 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7687 res->header.length); 7688 header_only = 0; 7689 } else { 7690 tmp_len = sizeof(struct scsi_per_res_in_header); 7691 scsi_ulto4b(0, res->header.length); 7692 header_only = 1; 7693 } 7694 7695 /* 7696 * We had to drop the lock to allocate our buffer, which 7697 * leaves time for someone to come in with another 7698 * persistent reservation. (That is unlikely, though, 7699 * since this should be the only persistent reservation 7700 * command active right now.) 7701 */ 7702 if (tmp_len != total_len) { 7703 mtx_unlock(&lun->lun_lock); 7704 free(ctsio->kern_data_ptr, M_CTL); 7705 printf("%s: reservation status changed, retrying\n", 7706 __func__); 7707 goto retry; 7708 } 7709 7710 /* 7711 * No reservation held, so we're done. 7712 */ 7713 if (header_only != 0) 7714 break; 7715 7716 /* 7717 * If the registration is an All Registrants type, the key 7718 * is 0, since it doesn't really matter. 7719 */ 7720 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7721 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7722 res->data.reservation); 7723 } 7724 res->data.scopetype = lun->pr_res_type; 7725 break; 7726 } 7727 case SPRI_RC: //report capabilities 7728 { 7729 struct scsi_per_res_cap *res_cap; 7730 uint16_t type_mask; 7731 7732 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7733 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7734 res_cap->flags1 = SPRI_CRH; 7735 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7736 type_mask = SPRI_TM_WR_EX_AR | 7737 SPRI_TM_EX_AC_RO | 7738 SPRI_TM_WR_EX_RO | 7739 SPRI_TM_EX_AC | 7740 SPRI_TM_WR_EX | 7741 SPRI_TM_EX_AC_AR; 7742 scsi_ulto2b(type_mask, res_cap->type_mask); 7743 break; 7744 } 7745 case SPRI_RS: { // read full status 7746 struct scsi_per_res_in_full *res_status; 7747 struct scsi_per_res_in_full_desc *res_desc; 7748 struct ctl_port *port; 7749 int i, len; 7750 7751 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7752 7753 /* 7754 * We had to drop the lock to allocate our buffer, which 7755 * leaves time for someone to come in with another 7756 * persistent reservation. (That is unlikely, though, 7757 * since this should be the only persistent reservation 7758 * command active right now.) 7759 */ 7760 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7761 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7762 lun->pr_key_count)){ 7763 mtx_unlock(&lun->lun_lock); 7764 free(ctsio->kern_data_ptr, M_CTL); 7765 printf("%s: reservation length changed, retrying\n", 7766 __func__); 7767 goto retry; 7768 } 7769 7770 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7771 7772 res_desc = &res_status->desc[0]; 7773 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7774 if ((key = ctl_get_prkey(lun, i)) == 0) 7775 continue; 7776 7777 scsi_u64to8b(key, res_desc->res_key.key); 7778 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7779 (lun->pr_res_idx == i || 7780 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7781 res_desc->flags = SPRI_FULL_R_HOLDER; 7782 res_desc->scopetype = lun->pr_res_type; 7783 } 7784 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7785 res_desc->rel_trgt_port_id); 7786 len = 0; 7787 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7788 if (port != NULL) 7789 len = ctl_create_iid(port, 7790 i % CTL_MAX_INIT_PER_PORT, 7791 res_desc->transport_id); 7792 scsi_ulto4b(len, res_desc->additional_length); 7793 res_desc = (struct scsi_per_res_in_full_desc *) 7794 &res_desc->transport_id[len]; 7795 } 7796 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7797 res_status->header.length); 7798 break; 7799 } 7800 default: 7801 panic("%s: Invalid PR type %#x", __func__, cdb->action); 7802 } 7803 mtx_unlock(&lun->lun_lock); 7804 7805 ctl_set_success(ctsio); 7806 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7807 ctsio->be_move_done = ctl_config_move_done; 7808 ctl_datamove((union ctl_io *)ctsio); 7809 return (CTL_RETVAL_COMPLETE); 7810} 7811 7812/* 7813 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7814 * it should return. 7815 */ 7816static int 7817ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7818 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7819 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7820 struct scsi_per_res_out_parms* param) 7821{ 7822 union ctl_ha_msg persis_io; 7823 int i; 7824 7825 mtx_lock(&lun->lun_lock); 7826 if (sa_res_key == 0) { 7827 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7828 /* validate scope and type */ 7829 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7830 SPR_LU_SCOPE) { 7831 mtx_unlock(&lun->lun_lock); 7832 ctl_set_invalid_field(/*ctsio*/ ctsio, 7833 /*sks_valid*/ 1, 7834 /*command*/ 1, 7835 /*field*/ 2, 7836 /*bit_valid*/ 1, 7837 /*bit*/ 4); 7838 ctl_done((union ctl_io *)ctsio); 7839 return (1); 7840 } 7841 7842 if (type>8 || type==2 || type==4 || type==0) { 7843 mtx_unlock(&lun->lun_lock); 7844 ctl_set_invalid_field(/*ctsio*/ ctsio, 7845 /*sks_valid*/ 1, 7846 /*command*/ 1, 7847 /*field*/ 2, 7848 /*bit_valid*/ 1, 7849 /*bit*/ 0); 7850 ctl_done((union ctl_io *)ctsio); 7851 return (1); 7852 } 7853 7854 /* 7855 * Unregister everybody else and build UA for 7856 * them 7857 */ 7858 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7859 if (i == residx || ctl_get_prkey(lun, i) == 0) 7860 continue; 7861 7862 ctl_clr_prkey(lun, i); 7863 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7864 } 7865 lun->pr_key_count = 1; 7866 lun->pr_res_type = type; 7867 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7868 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7869 lun->pr_res_idx = residx; 7870 lun->pr_generation++; 7871 mtx_unlock(&lun->lun_lock); 7872 7873 /* send msg to other side */ 7874 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7875 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7876 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7877 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7878 persis_io.pr.pr_info.res_type = type; 7879 memcpy(persis_io.pr.pr_info.sa_res_key, 7880 param->serv_act_res_key, 7881 sizeof(param->serv_act_res_key)); 7882 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7883 sizeof(persis_io.pr), M_WAITOK); 7884 } else { 7885 /* not all registrants */ 7886 mtx_unlock(&lun->lun_lock); 7887 free(ctsio->kern_data_ptr, M_CTL); 7888 ctl_set_invalid_field(ctsio, 7889 /*sks_valid*/ 1, 7890 /*command*/ 0, 7891 /*field*/ 8, 7892 /*bit_valid*/ 0, 7893 /*bit*/ 0); 7894 ctl_done((union ctl_io *)ctsio); 7895 return (1); 7896 } 7897 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7898 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7899 int found = 0; 7900 7901 if (res_key == sa_res_key) { 7902 /* special case */ 7903 /* 7904 * The spec implies this is not good but doesn't 7905 * say what to do. There are two choices either 7906 * generate a res conflict or check condition 7907 * with illegal field in parameter data. Since 7908 * that is what is done when the sa_res_key is 7909 * zero I'll take that approach since this has 7910 * to do with the sa_res_key. 7911 */ 7912 mtx_unlock(&lun->lun_lock); 7913 free(ctsio->kern_data_ptr, M_CTL); 7914 ctl_set_invalid_field(ctsio, 7915 /*sks_valid*/ 1, 7916 /*command*/ 0, 7917 /*field*/ 8, 7918 /*bit_valid*/ 0, 7919 /*bit*/ 0); 7920 ctl_done((union ctl_io *)ctsio); 7921 return (1); 7922 } 7923 7924 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7925 if (ctl_get_prkey(lun, i) != sa_res_key) 7926 continue; 7927 7928 found = 1; 7929 ctl_clr_prkey(lun, i); 7930 lun->pr_key_count--; 7931 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7932 } 7933 if (!found) { 7934 mtx_unlock(&lun->lun_lock); 7935 free(ctsio->kern_data_ptr, M_CTL); 7936 ctl_set_reservation_conflict(ctsio); 7937 ctl_done((union ctl_io *)ctsio); 7938 return (CTL_RETVAL_COMPLETE); 7939 } 7940 lun->pr_generation++; 7941 mtx_unlock(&lun->lun_lock); 7942 7943 /* send msg to other side */ 7944 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7945 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7946 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7947 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7948 persis_io.pr.pr_info.res_type = type; 7949 memcpy(persis_io.pr.pr_info.sa_res_key, 7950 param->serv_act_res_key, 7951 sizeof(param->serv_act_res_key)); 7952 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7953 sizeof(persis_io.pr), M_WAITOK); 7954 } else { 7955 /* Reserved but not all registrants */ 7956 /* sa_res_key is res holder */ 7957 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7958 /* validate scope and type */ 7959 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7960 SPR_LU_SCOPE) { 7961 mtx_unlock(&lun->lun_lock); 7962 ctl_set_invalid_field(/*ctsio*/ ctsio, 7963 /*sks_valid*/ 1, 7964 /*command*/ 1, 7965 /*field*/ 2, 7966 /*bit_valid*/ 1, 7967 /*bit*/ 4); 7968 ctl_done((union ctl_io *)ctsio); 7969 return (1); 7970 } 7971 7972 if (type>8 || type==2 || type==4 || type==0) { 7973 mtx_unlock(&lun->lun_lock); 7974 ctl_set_invalid_field(/*ctsio*/ ctsio, 7975 /*sks_valid*/ 1, 7976 /*command*/ 1, 7977 /*field*/ 2, 7978 /*bit_valid*/ 1, 7979 /*bit*/ 0); 7980 ctl_done((union ctl_io *)ctsio); 7981 return (1); 7982 } 7983 7984 /* 7985 * Do the following: 7986 * if sa_res_key != res_key remove all 7987 * registrants w/sa_res_key and generate UA 7988 * for these registrants(Registrations 7989 * Preempted) if it wasn't an exclusive 7990 * reservation generate UA(Reservations 7991 * Preempted) for all other registered nexuses 7992 * if the type has changed. Establish the new 7993 * reservation and holder. If res_key and 7994 * sa_res_key are the same do the above 7995 * except don't unregister the res holder. 7996 */ 7997 7998 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7999 if (i == residx || ctl_get_prkey(lun, i) == 0) 8000 continue; 8001 8002 if (sa_res_key == ctl_get_prkey(lun, i)) { 8003 ctl_clr_prkey(lun, i); 8004 lun->pr_key_count--; 8005 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8006 } else if (type != lun->pr_res_type && 8007 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8008 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8009 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8010 } 8011 } 8012 lun->pr_res_type = type; 8013 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8014 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8015 lun->pr_res_idx = residx; 8016 else 8017 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8018 lun->pr_generation++; 8019 mtx_unlock(&lun->lun_lock); 8020 8021 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8022 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8023 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8024 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8025 persis_io.pr.pr_info.res_type = type; 8026 memcpy(persis_io.pr.pr_info.sa_res_key, 8027 param->serv_act_res_key, 8028 sizeof(param->serv_act_res_key)); 8029 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8030 sizeof(persis_io.pr), M_WAITOK); 8031 } else { 8032 /* 8033 * sa_res_key is not the res holder just 8034 * remove registrants 8035 */ 8036 int found=0; 8037 8038 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8039 if (sa_res_key != ctl_get_prkey(lun, i)) 8040 continue; 8041 8042 found = 1; 8043 ctl_clr_prkey(lun, i); 8044 lun->pr_key_count--; 8045 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8046 } 8047 8048 if (!found) { 8049 mtx_unlock(&lun->lun_lock); 8050 free(ctsio->kern_data_ptr, M_CTL); 8051 ctl_set_reservation_conflict(ctsio); 8052 ctl_done((union ctl_io *)ctsio); 8053 return (1); 8054 } 8055 lun->pr_generation++; 8056 mtx_unlock(&lun->lun_lock); 8057 8058 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8059 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8060 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8061 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8062 persis_io.pr.pr_info.res_type = type; 8063 memcpy(persis_io.pr.pr_info.sa_res_key, 8064 param->serv_act_res_key, 8065 sizeof(param->serv_act_res_key)); 8066 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8067 sizeof(persis_io.pr), M_WAITOK); 8068 } 8069 } 8070 return (0); 8071} 8072 8073static void 8074ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8075{ 8076 uint64_t sa_res_key; 8077 int i; 8078 8079 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8080 8081 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8082 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8083 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8084 if (sa_res_key == 0) { 8085 /* 8086 * Unregister everybody else and build UA for 8087 * them 8088 */ 8089 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8090 if (i == msg->pr.pr_info.residx || 8091 ctl_get_prkey(lun, i) == 0) 8092 continue; 8093 8094 ctl_clr_prkey(lun, i); 8095 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8096 } 8097 8098 lun->pr_key_count = 1; 8099 lun->pr_res_type = msg->pr.pr_info.res_type; 8100 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8101 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8102 lun->pr_res_idx = msg->pr.pr_info.residx; 8103 } else { 8104 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8105 if (sa_res_key == ctl_get_prkey(lun, i)) 8106 continue; 8107 8108 ctl_clr_prkey(lun, i); 8109 lun->pr_key_count--; 8110 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8111 } 8112 } 8113 } else { 8114 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8115 if (i == msg->pr.pr_info.residx || 8116 ctl_get_prkey(lun, i) == 0) 8117 continue; 8118 8119 if (sa_res_key == ctl_get_prkey(lun, i)) { 8120 ctl_clr_prkey(lun, i); 8121 lun->pr_key_count--; 8122 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8123 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8124 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8125 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8126 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8127 } 8128 } 8129 lun->pr_res_type = msg->pr.pr_info.res_type; 8130 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8131 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8132 lun->pr_res_idx = msg->pr.pr_info.residx; 8133 else 8134 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8135 } 8136 lun->pr_generation++; 8137 8138} 8139 8140 8141int 8142ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8143{ 8144 int retval; 8145 u_int32_t param_len; 8146 struct scsi_per_res_out *cdb; 8147 struct ctl_lun *lun; 8148 struct scsi_per_res_out_parms* param; 8149 struct ctl_softc *softc; 8150 uint32_t residx; 8151 uint64_t res_key, sa_res_key, key; 8152 uint8_t type; 8153 union ctl_ha_msg persis_io; 8154 int i; 8155 8156 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8157 8158 retval = CTL_RETVAL_COMPLETE; 8159 8160 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8161 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8162 softc = lun->ctl_softc; 8163 8164 /* 8165 * We only support whole-LUN scope. The scope & type are ignored for 8166 * register, register and ignore existing key and clear. 8167 * We sometimes ignore scope and type on preempts too!! 8168 * Verify reservation type here as well. 8169 */ 8170 type = cdb->scope_type & SPR_TYPE_MASK; 8171 if ((cdb->action == SPRO_RESERVE) 8172 || (cdb->action == SPRO_RELEASE)) { 8173 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8174 ctl_set_invalid_field(/*ctsio*/ ctsio, 8175 /*sks_valid*/ 1, 8176 /*command*/ 1, 8177 /*field*/ 2, 8178 /*bit_valid*/ 1, 8179 /*bit*/ 4); 8180 ctl_done((union ctl_io *)ctsio); 8181 return (CTL_RETVAL_COMPLETE); 8182 } 8183 8184 if (type>8 || type==2 || type==4 || type==0) { 8185 ctl_set_invalid_field(/*ctsio*/ ctsio, 8186 /*sks_valid*/ 1, 8187 /*command*/ 1, 8188 /*field*/ 2, 8189 /*bit_valid*/ 1, 8190 /*bit*/ 0); 8191 ctl_done((union ctl_io *)ctsio); 8192 return (CTL_RETVAL_COMPLETE); 8193 } 8194 } 8195 8196 param_len = scsi_4btoul(cdb->length); 8197 8198 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8199 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8200 ctsio->kern_data_len = param_len; 8201 ctsio->kern_total_len = param_len; 8202 ctsio->kern_data_resid = 0; 8203 ctsio->kern_rel_offset = 0; 8204 ctsio->kern_sg_entries = 0; 8205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8206 ctsio->be_move_done = ctl_config_move_done; 8207 ctl_datamove((union ctl_io *)ctsio); 8208 8209 return (CTL_RETVAL_COMPLETE); 8210 } 8211 8212 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8213 8214 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8215 res_key = scsi_8btou64(param->res_key.key); 8216 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8217 8218 /* 8219 * Validate the reservation key here except for SPRO_REG_IGNO 8220 * This must be done for all other service actions 8221 */ 8222 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8223 mtx_lock(&lun->lun_lock); 8224 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8225 if (res_key != key) { 8226 /* 8227 * The current key passed in doesn't match 8228 * the one the initiator previously 8229 * registered. 8230 */ 8231 mtx_unlock(&lun->lun_lock); 8232 free(ctsio->kern_data_ptr, M_CTL); 8233 ctl_set_reservation_conflict(ctsio); 8234 ctl_done((union ctl_io *)ctsio); 8235 return (CTL_RETVAL_COMPLETE); 8236 } 8237 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8238 /* 8239 * We are not registered 8240 */ 8241 mtx_unlock(&lun->lun_lock); 8242 free(ctsio->kern_data_ptr, M_CTL); 8243 ctl_set_reservation_conflict(ctsio); 8244 ctl_done((union ctl_io *)ctsio); 8245 return (CTL_RETVAL_COMPLETE); 8246 } else if (res_key != 0) { 8247 /* 8248 * We are not registered and trying to register but 8249 * the register key isn't zero. 8250 */ 8251 mtx_unlock(&lun->lun_lock); 8252 free(ctsio->kern_data_ptr, M_CTL); 8253 ctl_set_reservation_conflict(ctsio); 8254 ctl_done((union ctl_io *)ctsio); 8255 return (CTL_RETVAL_COMPLETE); 8256 } 8257 mtx_unlock(&lun->lun_lock); 8258 } 8259 8260 switch (cdb->action & SPRO_ACTION_MASK) { 8261 case SPRO_REGISTER: 8262 case SPRO_REG_IGNO: { 8263 8264#if 0 8265 printf("Registration received\n"); 8266#endif 8267 8268 /* 8269 * We don't support any of these options, as we report in 8270 * the read capabilities request (see 8271 * ctl_persistent_reserve_in(), above). 8272 */ 8273 if ((param->flags & SPR_SPEC_I_PT) 8274 || (param->flags & SPR_ALL_TG_PT) 8275 || (param->flags & SPR_APTPL)) { 8276 int bit_ptr; 8277 8278 if (param->flags & SPR_APTPL) 8279 bit_ptr = 0; 8280 else if (param->flags & SPR_ALL_TG_PT) 8281 bit_ptr = 2; 8282 else /* SPR_SPEC_I_PT */ 8283 bit_ptr = 3; 8284 8285 free(ctsio->kern_data_ptr, M_CTL); 8286 ctl_set_invalid_field(ctsio, 8287 /*sks_valid*/ 1, 8288 /*command*/ 0, 8289 /*field*/ 20, 8290 /*bit_valid*/ 1, 8291 /*bit*/ bit_ptr); 8292 ctl_done((union ctl_io *)ctsio); 8293 return (CTL_RETVAL_COMPLETE); 8294 } 8295 8296 mtx_lock(&lun->lun_lock); 8297 8298 /* 8299 * The initiator wants to clear the 8300 * key/unregister. 8301 */ 8302 if (sa_res_key == 0) { 8303 if ((res_key == 0 8304 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8305 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8306 && ctl_get_prkey(lun, residx) == 0)) { 8307 mtx_unlock(&lun->lun_lock); 8308 goto done; 8309 } 8310 8311 ctl_clr_prkey(lun, residx); 8312 lun->pr_key_count--; 8313 8314 if (residx == lun->pr_res_idx) { 8315 lun->flags &= ~CTL_LUN_PR_RESERVED; 8316 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8317 8318 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8319 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8320 lun->pr_key_count) { 8321 /* 8322 * If the reservation is a registrants 8323 * only type we need to generate a UA 8324 * for other registered inits. The 8325 * sense code should be RESERVATIONS 8326 * RELEASED 8327 */ 8328 8329 for (i = softc->init_min; i < softc->init_max; i++){ 8330 if (ctl_get_prkey(lun, i) == 0) 8331 continue; 8332 ctl_est_ua(lun, i, 8333 CTL_UA_RES_RELEASE); 8334 } 8335 } 8336 lun->pr_res_type = 0; 8337 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8338 if (lun->pr_key_count==0) { 8339 lun->flags &= ~CTL_LUN_PR_RESERVED; 8340 lun->pr_res_type = 0; 8341 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8342 } 8343 } 8344 lun->pr_generation++; 8345 mtx_unlock(&lun->lun_lock); 8346 8347 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8348 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8349 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8350 persis_io.pr.pr_info.residx = residx; 8351 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8352 sizeof(persis_io.pr), M_WAITOK); 8353 } else /* sa_res_key != 0 */ { 8354 8355 /* 8356 * If we aren't registered currently then increment 8357 * the key count and set the registered flag. 8358 */ 8359 ctl_alloc_prkey(lun, residx); 8360 if (ctl_get_prkey(lun, residx) == 0) 8361 lun->pr_key_count++; 8362 ctl_set_prkey(lun, residx, sa_res_key); 8363 lun->pr_generation++; 8364 mtx_unlock(&lun->lun_lock); 8365 8366 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8367 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8368 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8369 persis_io.pr.pr_info.residx = residx; 8370 memcpy(persis_io.pr.pr_info.sa_res_key, 8371 param->serv_act_res_key, 8372 sizeof(param->serv_act_res_key)); 8373 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8374 sizeof(persis_io.pr), M_WAITOK); 8375 } 8376 8377 break; 8378 } 8379 case SPRO_RESERVE: 8380#if 0 8381 printf("Reserve executed type %d\n", type); 8382#endif 8383 mtx_lock(&lun->lun_lock); 8384 if (lun->flags & CTL_LUN_PR_RESERVED) { 8385 /* 8386 * if this isn't the reservation holder and it's 8387 * not a "all registrants" type or if the type is 8388 * different then we have a conflict 8389 */ 8390 if ((lun->pr_res_idx != residx 8391 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8392 || lun->pr_res_type != type) { 8393 mtx_unlock(&lun->lun_lock); 8394 free(ctsio->kern_data_ptr, M_CTL); 8395 ctl_set_reservation_conflict(ctsio); 8396 ctl_done((union ctl_io *)ctsio); 8397 return (CTL_RETVAL_COMPLETE); 8398 } 8399 mtx_unlock(&lun->lun_lock); 8400 } else /* create a reservation */ { 8401 /* 8402 * If it's not an "all registrants" type record 8403 * reservation holder 8404 */ 8405 if (type != SPR_TYPE_WR_EX_AR 8406 && type != SPR_TYPE_EX_AC_AR) 8407 lun->pr_res_idx = residx; /* Res holder */ 8408 else 8409 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8410 8411 lun->flags |= CTL_LUN_PR_RESERVED; 8412 lun->pr_res_type = type; 8413 8414 mtx_unlock(&lun->lun_lock); 8415 8416 /* send msg to other side */ 8417 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8418 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8419 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8420 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8421 persis_io.pr.pr_info.res_type = type; 8422 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8423 sizeof(persis_io.pr), M_WAITOK); 8424 } 8425 break; 8426 8427 case SPRO_RELEASE: 8428 mtx_lock(&lun->lun_lock); 8429 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8430 /* No reservation exists return good status */ 8431 mtx_unlock(&lun->lun_lock); 8432 goto done; 8433 } 8434 /* 8435 * Is this nexus a reservation holder? 8436 */ 8437 if (lun->pr_res_idx != residx 8438 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8439 /* 8440 * not a res holder return good status but 8441 * do nothing 8442 */ 8443 mtx_unlock(&lun->lun_lock); 8444 goto done; 8445 } 8446 8447 if (lun->pr_res_type != type) { 8448 mtx_unlock(&lun->lun_lock); 8449 free(ctsio->kern_data_ptr, M_CTL); 8450 ctl_set_illegal_pr_release(ctsio); 8451 ctl_done((union ctl_io *)ctsio); 8452 return (CTL_RETVAL_COMPLETE); 8453 } 8454 8455 /* okay to release */ 8456 lun->flags &= ~CTL_LUN_PR_RESERVED; 8457 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8458 lun->pr_res_type = 0; 8459 8460 /* 8461 * If this isn't an exclusive access reservation and NUAR 8462 * is not set, generate UA for all other registrants. 8463 */ 8464 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX && 8465 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8466 for (i = softc->init_min; i < softc->init_max; i++) { 8467 if (i == residx || ctl_get_prkey(lun, i) == 0) 8468 continue; 8469 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8470 } 8471 } 8472 mtx_unlock(&lun->lun_lock); 8473 8474 /* Send msg to other side */ 8475 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8476 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8477 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8478 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8479 sizeof(persis_io.pr), M_WAITOK); 8480 break; 8481 8482 case SPRO_CLEAR: 8483 /* send msg to other side */ 8484 8485 mtx_lock(&lun->lun_lock); 8486 lun->flags &= ~CTL_LUN_PR_RESERVED; 8487 lun->pr_res_type = 0; 8488 lun->pr_key_count = 0; 8489 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8490 8491 ctl_clr_prkey(lun, residx); 8492 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8493 if (ctl_get_prkey(lun, i) != 0) { 8494 ctl_clr_prkey(lun, i); 8495 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8496 } 8497 lun->pr_generation++; 8498 mtx_unlock(&lun->lun_lock); 8499 8500 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8501 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8502 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8503 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8504 sizeof(persis_io.pr), M_WAITOK); 8505 break; 8506 8507 case SPRO_PREEMPT: 8508 case SPRO_PRE_ABO: { 8509 int nretval; 8510 8511 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8512 residx, ctsio, cdb, param); 8513 if (nretval != 0) 8514 return (CTL_RETVAL_COMPLETE); 8515 break; 8516 } 8517 default: 8518 panic("%s: Invalid PR type %#x", __func__, cdb->action); 8519 } 8520 8521done: 8522 free(ctsio->kern_data_ptr, M_CTL); 8523 ctl_set_success(ctsio); 8524 ctl_done((union ctl_io *)ctsio); 8525 8526 return (retval); 8527} 8528 8529/* 8530 * This routine is for handling a message from the other SC pertaining to 8531 * persistent reserve out. All the error checking will have been done 8532 * so only perorming the action need be done here to keep the two 8533 * in sync. 8534 */ 8535static void 8536ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8537{ 8538 struct ctl_softc *softc = control_softc; 8539 struct ctl_lun *lun; 8540 int i; 8541 uint32_t residx, targ_lun; 8542 8543 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8544 mtx_lock(&softc->ctl_lock); 8545 if (targ_lun >= CTL_MAX_LUNS || 8546 (lun = softc->ctl_luns[targ_lun]) == NULL) { 8547 mtx_unlock(&softc->ctl_lock); 8548 return; 8549 } 8550 mtx_lock(&lun->lun_lock); 8551 mtx_unlock(&softc->ctl_lock); 8552 if (lun->flags & CTL_LUN_DISABLED) { 8553 mtx_unlock(&lun->lun_lock); 8554 return; 8555 } 8556 residx = ctl_get_initindex(&msg->hdr.nexus); 8557 switch(msg->pr.pr_info.action) { 8558 case CTL_PR_REG_KEY: 8559 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8560 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8561 lun->pr_key_count++; 8562 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8563 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8564 lun->pr_generation++; 8565 break; 8566 8567 case CTL_PR_UNREG_KEY: 8568 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8569 lun->pr_key_count--; 8570 8571 /* XXX Need to see if the reservation has been released */ 8572 /* if so do we need to generate UA? */ 8573 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8574 lun->flags &= ~CTL_LUN_PR_RESERVED; 8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8576 8577 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8578 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8579 lun->pr_key_count) { 8580 /* 8581 * If the reservation is a registrants 8582 * only type we need to generate a UA 8583 * for other registered inits. The 8584 * sense code should be RESERVATIONS 8585 * RELEASED 8586 */ 8587 8588 for (i = softc->init_min; i < softc->init_max; i++) { 8589 if (ctl_get_prkey(lun, i) == 0) 8590 continue; 8591 8592 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8593 } 8594 } 8595 lun->pr_res_type = 0; 8596 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8597 if (lun->pr_key_count==0) { 8598 lun->flags &= ~CTL_LUN_PR_RESERVED; 8599 lun->pr_res_type = 0; 8600 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8601 } 8602 } 8603 lun->pr_generation++; 8604 break; 8605 8606 case CTL_PR_RESERVE: 8607 lun->flags |= CTL_LUN_PR_RESERVED; 8608 lun->pr_res_type = msg->pr.pr_info.res_type; 8609 lun->pr_res_idx = msg->pr.pr_info.residx; 8610 8611 break; 8612 8613 case CTL_PR_RELEASE: 8614 /* 8615 * If this isn't an exclusive access reservation and NUAR 8616 * is not set, generate UA for all other registrants. 8617 */ 8618 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8619 lun->pr_res_type != SPR_TYPE_WR_EX && 8620 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) { 8621 for (i = softc->init_min; i < softc->init_max; i++) 8622 if (i == residx || ctl_get_prkey(lun, i) == 0) 8623 continue; 8624 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8625 } 8626 8627 lun->flags &= ~CTL_LUN_PR_RESERVED; 8628 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8629 lun->pr_res_type = 0; 8630 break; 8631 8632 case CTL_PR_PREEMPT: 8633 ctl_pro_preempt_other(lun, msg); 8634 break; 8635 case CTL_PR_CLEAR: 8636 lun->flags &= ~CTL_LUN_PR_RESERVED; 8637 lun->pr_res_type = 0; 8638 lun->pr_key_count = 0; 8639 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8640 8641 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8642 if (ctl_get_prkey(lun, i) == 0) 8643 continue; 8644 ctl_clr_prkey(lun, i); 8645 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8646 } 8647 lun->pr_generation++; 8648 break; 8649 } 8650 8651 mtx_unlock(&lun->lun_lock); 8652} 8653 8654int 8655ctl_read_write(struct ctl_scsiio *ctsio) 8656{ 8657 struct ctl_lun *lun; 8658 struct ctl_lba_len_flags *lbalen; 8659 uint64_t lba; 8660 uint32_t num_blocks; 8661 int flags, retval; 8662 int isread; 8663 8664 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8665 8666 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8667 8668 flags = 0; 8669 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8670 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8671 switch (ctsio->cdb[0]) { 8672 case READ_6: 8673 case WRITE_6: { 8674 struct scsi_rw_6 *cdb; 8675 8676 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8677 8678 lba = scsi_3btoul(cdb->addr); 8679 /* only 5 bits are valid in the most significant address byte */ 8680 lba &= 0x1fffff; 8681 num_blocks = cdb->length; 8682 /* 8683 * This is correct according to SBC-2. 8684 */ 8685 if (num_blocks == 0) 8686 num_blocks = 256; 8687 break; 8688 } 8689 case READ_10: 8690 case WRITE_10: { 8691 struct scsi_rw_10 *cdb; 8692 8693 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8694 if (cdb->byte2 & SRW10_FUA) 8695 flags |= CTL_LLF_FUA; 8696 if (cdb->byte2 & SRW10_DPO) 8697 flags |= CTL_LLF_DPO; 8698 lba = scsi_4btoul(cdb->addr); 8699 num_blocks = scsi_2btoul(cdb->length); 8700 break; 8701 } 8702 case WRITE_VERIFY_10: { 8703 struct scsi_write_verify_10 *cdb; 8704 8705 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8706 flags |= CTL_LLF_FUA; 8707 if (cdb->byte2 & SWV_DPO) 8708 flags |= CTL_LLF_DPO; 8709 lba = scsi_4btoul(cdb->addr); 8710 num_blocks = scsi_2btoul(cdb->length); 8711 break; 8712 } 8713 case READ_12: 8714 case WRITE_12: { 8715 struct scsi_rw_12 *cdb; 8716 8717 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8718 if (cdb->byte2 & SRW12_FUA) 8719 flags |= CTL_LLF_FUA; 8720 if (cdb->byte2 & SRW12_DPO) 8721 flags |= CTL_LLF_DPO; 8722 lba = scsi_4btoul(cdb->addr); 8723 num_blocks = scsi_4btoul(cdb->length); 8724 break; 8725 } 8726 case WRITE_VERIFY_12: { 8727 struct scsi_write_verify_12 *cdb; 8728 8729 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8730 flags |= CTL_LLF_FUA; 8731 if (cdb->byte2 & SWV_DPO) 8732 flags |= CTL_LLF_DPO; 8733 lba = scsi_4btoul(cdb->addr); 8734 num_blocks = scsi_4btoul(cdb->length); 8735 break; 8736 } 8737 case READ_16: 8738 case WRITE_16: { 8739 struct scsi_rw_16 *cdb; 8740 8741 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8742 if (cdb->byte2 & SRW12_FUA) 8743 flags |= CTL_LLF_FUA; 8744 if (cdb->byte2 & SRW12_DPO) 8745 flags |= CTL_LLF_DPO; 8746 lba = scsi_8btou64(cdb->addr); 8747 num_blocks = scsi_4btoul(cdb->length); 8748 break; 8749 } 8750 case WRITE_ATOMIC_16: { 8751 struct scsi_write_atomic_16 *cdb; 8752 8753 if (lun->be_lun->atomicblock == 0) { 8754 ctl_set_invalid_opcode(ctsio); 8755 ctl_done((union ctl_io *)ctsio); 8756 return (CTL_RETVAL_COMPLETE); 8757 } 8758 8759 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8760 if (cdb->byte2 & SRW12_FUA) 8761 flags |= CTL_LLF_FUA; 8762 if (cdb->byte2 & SRW12_DPO) 8763 flags |= CTL_LLF_DPO; 8764 lba = scsi_8btou64(cdb->addr); 8765 num_blocks = scsi_2btoul(cdb->length); 8766 if (num_blocks > lun->be_lun->atomicblock) { 8767 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8768 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8769 /*bit*/ 0); 8770 ctl_done((union ctl_io *)ctsio); 8771 return (CTL_RETVAL_COMPLETE); 8772 } 8773 break; 8774 } 8775 case WRITE_VERIFY_16: { 8776 struct scsi_write_verify_16 *cdb; 8777 8778 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8779 flags |= CTL_LLF_FUA; 8780 if (cdb->byte2 & SWV_DPO) 8781 flags |= CTL_LLF_DPO; 8782 lba = scsi_8btou64(cdb->addr); 8783 num_blocks = scsi_4btoul(cdb->length); 8784 break; 8785 } 8786 default: 8787 /* 8788 * We got a command we don't support. This shouldn't 8789 * happen, commands should be filtered out above us. 8790 */ 8791 ctl_set_invalid_opcode(ctsio); 8792 ctl_done((union ctl_io *)ctsio); 8793 8794 return (CTL_RETVAL_COMPLETE); 8795 break; /* NOTREACHED */ 8796 } 8797 8798 /* 8799 * The first check is to make sure we're in bounds, the second 8800 * check is to catch wrap-around problems. If the lba + num blocks 8801 * is less than the lba, then we've wrapped around and the block 8802 * range is invalid anyway. 8803 */ 8804 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8805 || ((lba + num_blocks) < lba)) { 8806 ctl_set_lba_out_of_range(ctsio, 8807 MAX(lba, lun->be_lun->maxlba + 1)); 8808 ctl_done((union ctl_io *)ctsio); 8809 return (CTL_RETVAL_COMPLETE); 8810 } 8811 8812 /* 8813 * According to SBC-3, a transfer length of 0 is not an error. 8814 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8815 * translates to 256 blocks for those commands. 8816 */ 8817 if (num_blocks == 0) { 8818 ctl_set_success(ctsio); 8819 ctl_done((union ctl_io *)ctsio); 8820 return (CTL_RETVAL_COMPLETE); 8821 } 8822 8823 /* Set FUA and/or DPO if caches are disabled. */ 8824 if (isread) { 8825 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0) 8826 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8827 } else { 8828 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8829 flags |= CTL_LLF_FUA; 8830 } 8831 8832 lbalen = (struct ctl_lba_len_flags *) 8833 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8834 lbalen->lba = lba; 8835 lbalen->len = num_blocks; 8836 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8837 8838 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8839 ctsio->kern_rel_offset = 0; 8840 8841 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8842 8843 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8844 return (retval); 8845} 8846 8847static int 8848ctl_cnw_cont(union ctl_io *io) 8849{ 8850 struct ctl_scsiio *ctsio; 8851 struct ctl_lun *lun; 8852 struct ctl_lba_len_flags *lbalen; 8853 int retval; 8854 8855 ctsio = &io->scsiio; 8856 ctsio->io_hdr.status = CTL_STATUS_NONE; 8857 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8858 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8859 lbalen = (struct ctl_lba_len_flags *) 8860 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8861 lbalen->flags &= ~CTL_LLF_COMPARE; 8862 lbalen->flags |= CTL_LLF_WRITE; 8863 8864 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8865 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8866 return (retval); 8867} 8868 8869int 8870ctl_cnw(struct ctl_scsiio *ctsio) 8871{ 8872 struct ctl_lun *lun; 8873 struct ctl_lba_len_flags *lbalen; 8874 uint64_t lba; 8875 uint32_t num_blocks; 8876 int flags, retval; 8877 8878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8879 8880 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8881 8882 flags = 0; 8883 switch (ctsio->cdb[0]) { 8884 case COMPARE_AND_WRITE: { 8885 struct scsi_compare_and_write *cdb; 8886 8887 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8888 if (cdb->byte2 & SRW10_FUA) 8889 flags |= CTL_LLF_FUA; 8890 if (cdb->byte2 & SRW10_DPO) 8891 flags |= CTL_LLF_DPO; 8892 lba = scsi_8btou64(cdb->addr); 8893 num_blocks = cdb->length; 8894 break; 8895 } 8896 default: 8897 /* 8898 * We got a command we don't support. This shouldn't 8899 * happen, commands should be filtered out above us. 8900 */ 8901 ctl_set_invalid_opcode(ctsio); 8902 ctl_done((union ctl_io *)ctsio); 8903 8904 return (CTL_RETVAL_COMPLETE); 8905 break; /* NOTREACHED */ 8906 } 8907 8908 /* 8909 * The first check is to make sure we're in bounds, the second 8910 * check is to catch wrap-around problems. If the lba + num blocks 8911 * is less than the lba, then we've wrapped around and the block 8912 * range is invalid anyway. 8913 */ 8914 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8915 || ((lba + num_blocks) < lba)) { 8916 ctl_set_lba_out_of_range(ctsio, 8917 MAX(lba, lun->be_lun->maxlba + 1)); 8918 ctl_done((union ctl_io *)ctsio); 8919 return (CTL_RETVAL_COMPLETE); 8920 } 8921 8922 /* 8923 * According to SBC-3, a transfer length of 0 is not an error. 8924 */ 8925 if (num_blocks == 0) { 8926 ctl_set_success(ctsio); 8927 ctl_done((union ctl_io *)ctsio); 8928 return (CTL_RETVAL_COMPLETE); 8929 } 8930 8931 /* Set FUA if write cache is disabled. */ 8932 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0) 8933 flags |= CTL_LLF_FUA; 8934 8935 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8936 ctsio->kern_rel_offset = 0; 8937 8938 /* 8939 * Set the IO_CONT flag, so that if this I/O gets passed to 8940 * ctl_data_submit_done(), it'll get passed back to 8941 * ctl_ctl_cnw_cont() for further processing. 8942 */ 8943 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8944 ctsio->io_cont = ctl_cnw_cont; 8945 8946 lbalen = (struct ctl_lba_len_flags *) 8947 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8948 lbalen->lba = lba; 8949 lbalen->len = num_blocks; 8950 lbalen->flags = CTL_LLF_COMPARE | flags; 8951 8952 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8953 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8954 return (retval); 8955} 8956 8957int 8958ctl_verify(struct ctl_scsiio *ctsio) 8959{ 8960 struct ctl_lun *lun; 8961 struct ctl_lba_len_flags *lbalen; 8962 uint64_t lba; 8963 uint32_t num_blocks; 8964 int bytchk, flags; 8965 int retval; 8966 8967 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8968 8969 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8970 8971 bytchk = 0; 8972 flags = CTL_LLF_FUA; 8973 switch (ctsio->cdb[0]) { 8974 case VERIFY_10: { 8975 struct scsi_verify_10 *cdb; 8976 8977 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8978 if (cdb->byte2 & SVFY_BYTCHK) 8979 bytchk = 1; 8980 if (cdb->byte2 & SVFY_DPO) 8981 flags |= CTL_LLF_DPO; 8982 lba = scsi_4btoul(cdb->addr); 8983 num_blocks = scsi_2btoul(cdb->length); 8984 break; 8985 } 8986 case VERIFY_12: { 8987 struct scsi_verify_12 *cdb; 8988 8989 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8990 if (cdb->byte2 & SVFY_BYTCHK) 8991 bytchk = 1; 8992 if (cdb->byte2 & SVFY_DPO) 8993 flags |= CTL_LLF_DPO; 8994 lba = scsi_4btoul(cdb->addr); 8995 num_blocks = scsi_4btoul(cdb->length); 8996 break; 8997 } 8998 case VERIFY_16: { 8999 struct scsi_rw_16 *cdb; 9000 9001 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9002 if (cdb->byte2 & SVFY_BYTCHK) 9003 bytchk = 1; 9004 if (cdb->byte2 & SVFY_DPO) 9005 flags |= CTL_LLF_DPO; 9006 lba = scsi_8btou64(cdb->addr); 9007 num_blocks = scsi_4btoul(cdb->length); 9008 break; 9009 } 9010 default: 9011 /* 9012 * We got a command we don't support. This shouldn't 9013 * happen, commands should be filtered out above us. 9014 */ 9015 ctl_set_invalid_opcode(ctsio); 9016 ctl_done((union ctl_io *)ctsio); 9017 return (CTL_RETVAL_COMPLETE); 9018 } 9019 9020 /* 9021 * The first check is to make sure we're in bounds, the second 9022 * check is to catch wrap-around problems. If the lba + num blocks 9023 * is less than the lba, then we've wrapped around and the block 9024 * range is invalid anyway. 9025 */ 9026 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9027 || ((lba + num_blocks) < lba)) { 9028 ctl_set_lba_out_of_range(ctsio, 9029 MAX(lba, lun->be_lun->maxlba + 1)); 9030 ctl_done((union ctl_io *)ctsio); 9031 return (CTL_RETVAL_COMPLETE); 9032 } 9033 9034 /* 9035 * According to SBC-3, a transfer length of 0 is not an error. 9036 */ 9037 if (num_blocks == 0) { 9038 ctl_set_success(ctsio); 9039 ctl_done((union ctl_io *)ctsio); 9040 return (CTL_RETVAL_COMPLETE); 9041 } 9042 9043 lbalen = (struct ctl_lba_len_flags *) 9044 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9045 lbalen->lba = lba; 9046 lbalen->len = num_blocks; 9047 if (bytchk) { 9048 lbalen->flags = CTL_LLF_COMPARE | flags; 9049 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9050 } else { 9051 lbalen->flags = CTL_LLF_VERIFY | flags; 9052 ctsio->kern_total_len = 0; 9053 } 9054 ctsio->kern_rel_offset = 0; 9055 9056 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9057 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9058 return (retval); 9059} 9060 9061int 9062ctl_report_luns(struct ctl_scsiio *ctsio) 9063{ 9064 struct ctl_softc *softc; 9065 struct scsi_report_luns *cdb; 9066 struct scsi_report_luns_data *lun_data; 9067 struct ctl_lun *lun, *request_lun; 9068 struct ctl_port *port; 9069 int num_filled, num_luns, num_port_luns, retval; 9070 uint32_t alloc_len, lun_datalen; 9071 uint32_t initidx, targ_lun_id, lun_id; 9072 9073 retval = CTL_RETVAL_COMPLETE; 9074 cdb = (struct scsi_report_luns *)ctsio->cdb; 9075 port = ctl_io_port(&ctsio->io_hdr); 9076 softc = port->ctl_softc; 9077 9078 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9079 9080 num_luns = 0; 9081 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS; 9082 mtx_lock(&softc->ctl_lock); 9083 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) { 9084 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX) 9085 num_luns++; 9086 } 9087 mtx_unlock(&softc->ctl_lock); 9088 9089 switch (cdb->select_report) { 9090 case RPL_REPORT_DEFAULT: 9091 case RPL_REPORT_ALL: 9092 case RPL_REPORT_NONSUBSID: 9093 break; 9094 case RPL_REPORT_WELLKNOWN: 9095 case RPL_REPORT_ADMIN: 9096 case RPL_REPORT_CONGLOM: 9097 num_luns = 0; 9098 break; 9099 default: 9100 ctl_set_invalid_field(ctsio, 9101 /*sks_valid*/ 1, 9102 /*command*/ 1, 9103 /*field*/ 2, 9104 /*bit_valid*/ 0, 9105 /*bit*/ 0); 9106 ctl_done((union ctl_io *)ctsio); 9107 return (retval); 9108 break; /* NOTREACHED */ 9109 } 9110 9111 alloc_len = scsi_4btoul(cdb->length); 9112 /* 9113 * The initiator has to allocate at least 16 bytes for this request, 9114 * so he can at least get the header and the first LUN. Otherwise 9115 * we reject the request (per SPC-3 rev 14, section 6.21). 9116 */ 9117 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9118 sizeof(struct scsi_report_luns_lundata))) { 9119 ctl_set_invalid_field(ctsio, 9120 /*sks_valid*/ 1, 9121 /*command*/ 1, 9122 /*field*/ 6, 9123 /*bit_valid*/ 0, 9124 /*bit*/ 0); 9125 ctl_done((union ctl_io *)ctsio); 9126 return (retval); 9127 } 9128 9129 request_lun = (struct ctl_lun *) 9130 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9131 9132 lun_datalen = sizeof(*lun_data) + 9133 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9134 9135 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9136 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9137 ctsio->kern_sg_entries = 0; 9138 9139 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9140 9141 mtx_lock(&softc->ctl_lock); 9142 for (targ_lun_id = 0, num_filled = 0; 9143 targ_lun_id < num_port_luns && num_filled < num_luns; 9144 targ_lun_id++) { 9145 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9146 if (lun_id == UINT32_MAX) 9147 continue; 9148 lun = softc->ctl_luns[lun_id]; 9149 if (lun == NULL) 9150 continue; 9151 9152 be64enc(lun_data->luns[num_filled++].lundata, 9153 ctl_encode_lun(targ_lun_id)); 9154 9155 /* 9156 * According to SPC-3, rev 14 section 6.21: 9157 * 9158 * "The execution of a REPORT LUNS command to any valid and 9159 * installed logical unit shall clear the REPORTED LUNS DATA 9160 * HAS CHANGED unit attention condition for all logical 9161 * units of that target with respect to the requesting 9162 * initiator. A valid and installed logical unit is one 9163 * having a PERIPHERAL QUALIFIER of 000b in the standard 9164 * INQUIRY data (see 6.4.2)." 9165 * 9166 * If request_lun is NULL, the LUN this report luns command 9167 * was issued to is either disabled or doesn't exist. In that 9168 * case, we shouldn't clear any pending lun change unit 9169 * attention. 9170 */ 9171 if (request_lun != NULL) { 9172 mtx_lock(&lun->lun_lock); 9173 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9174 mtx_unlock(&lun->lun_lock); 9175 } 9176 } 9177 mtx_unlock(&softc->ctl_lock); 9178 9179 /* 9180 * It's quite possible that we've returned fewer LUNs than we allocated 9181 * space for. Trim it. 9182 */ 9183 lun_datalen = sizeof(*lun_data) + 9184 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9185 9186 if (lun_datalen < alloc_len) { 9187 ctsio->residual = alloc_len - lun_datalen; 9188 ctsio->kern_data_len = lun_datalen; 9189 ctsio->kern_total_len = lun_datalen; 9190 } else { 9191 ctsio->residual = 0; 9192 ctsio->kern_data_len = alloc_len; 9193 ctsio->kern_total_len = alloc_len; 9194 } 9195 ctsio->kern_data_resid = 0; 9196 ctsio->kern_rel_offset = 0; 9197 ctsio->kern_sg_entries = 0; 9198 9199 /* 9200 * We set this to the actual data length, regardless of how much 9201 * space we actually have to return results. If the user looks at 9202 * this value, he'll know whether or not he allocated enough space 9203 * and reissue the command if necessary. We don't support well 9204 * known logical units, so if the user asks for that, return none. 9205 */ 9206 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9207 9208 /* 9209 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9210 * this request. 9211 */ 9212 ctl_set_success(ctsio); 9213 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9214 ctsio->be_move_done = ctl_config_move_done; 9215 ctl_datamove((union ctl_io *)ctsio); 9216 return (retval); 9217} 9218 9219int 9220ctl_request_sense(struct ctl_scsiio *ctsio) 9221{ 9222 struct scsi_request_sense *cdb; 9223 struct scsi_sense_data *sense_ptr; 9224 struct ctl_softc *softc; 9225 struct ctl_lun *lun; 9226 uint32_t initidx; 9227 int have_error; 9228 u_int sense_len = SSD_FULL_SIZE; 9229 scsi_sense_data_type sense_format; 9230 ctl_ua_type ua_type; 9231 uint8_t asc = 0, ascq = 0; 9232 9233 cdb = (struct scsi_request_sense *)ctsio->cdb; 9234 9235 softc = control_softc; 9236 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9237 9238 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9239 9240 /* 9241 * Determine which sense format the user wants. 9242 */ 9243 if (cdb->byte2 & SRS_DESC) 9244 sense_format = SSD_TYPE_DESC; 9245 else 9246 sense_format = SSD_TYPE_FIXED; 9247 9248 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9249 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9250 ctsio->kern_sg_entries = 0; 9251 9252 /* 9253 * struct scsi_sense_data, which is currently set to 256 bytes, is 9254 * larger than the largest allowed value for the length field in the 9255 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9256 */ 9257 ctsio->residual = 0; 9258 ctsio->kern_data_len = cdb->length; 9259 ctsio->kern_total_len = cdb->length; 9260 9261 ctsio->kern_data_resid = 0; 9262 ctsio->kern_rel_offset = 0; 9263 ctsio->kern_sg_entries = 0; 9264 9265 /* 9266 * If we don't have a LUN, we don't have any pending sense. 9267 */ 9268 if (lun == NULL || 9269 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 9270 softc->ha_link < CTL_HA_LINK_UNKNOWN)) { 9271 /* "Logical unit not supported" */ 9272 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format, 9273 /*current_error*/ 1, 9274 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 9275 /*asc*/ 0x25, 9276 /*ascq*/ 0x00, 9277 SSD_ELEM_NONE); 9278 goto send; 9279 } 9280 9281 have_error = 0; 9282 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9283 /* 9284 * Check for pending sense, and then for pending unit attentions. 9285 * Pending sense gets returned first, then pending unit attentions. 9286 */ 9287 mtx_lock(&lun->lun_lock); 9288#ifdef CTL_WITH_CA 9289 if (ctl_is_set(lun->have_ca, initidx)) { 9290 scsi_sense_data_type stored_format; 9291 9292 /* 9293 * Check to see which sense format was used for the stored 9294 * sense data. 9295 */ 9296 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9297 9298 /* 9299 * If the user requested a different sense format than the 9300 * one we stored, then we need to convert it to the other 9301 * format. If we're going from descriptor to fixed format 9302 * sense data, we may lose things in translation, depending 9303 * on what options were used. 9304 * 9305 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9306 * for some reason we'll just copy it out as-is. 9307 */ 9308 if ((stored_format == SSD_TYPE_FIXED) 9309 && (sense_format == SSD_TYPE_DESC)) 9310 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9311 &lun->pending_sense[initidx], 9312 (struct scsi_sense_data_desc *)sense_ptr); 9313 else if ((stored_format == SSD_TYPE_DESC) 9314 && (sense_format == SSD_TYPE_FIXED)) 9315 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9316 &lun->pending_sense[initidx], 9317 (struct scsi_sense_data_fixed *)sense_ptr); 9318 else 9319 memcpy(sense_ptr, &lun->pending_sense[initidx], 9320 MIN(sizeof(*sense_ptr), 9321 sizeof(lun->pending_sense[initidx]))); 9322 9323 ctl_clear_mask(lun->have_ca, initidx); 9324 have_error = 1; 9325 } else 9326#endif 9327 if (have_error == 0) { 9328 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len, 9329 sense_format); 9330 if (ua_type != CTL_UA_NONE) 9331 have_error = 1; 9332 } 9333 if (have_error == 0) { 9334 /* 9335 * Report informational exception if have one and allowed. 9336 */ 9337 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) { 9338 asc = lun->ie_asc; 9339 ascq = lun->ie_ascq; 9340 } 9341 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format, 9342 /*current_error*/ 1, 9343 /*sense_key*/ SSD_KEY_NO_SENSE, 9344 /*asc*/ asc, 9345 /*ascq*/ ascq, 9346 SSD_ELEM_NONE); 9347 } 9348 mtx_unlock(&lun->lun_lock); 9349 9350send: 9351 /* 9352 * We report the SCSI status as OK, since the status of the command 9353 * itself is OK. We're reporting sense as parameter data. 9354 */ 9355 ctl_set_success(ctsio); 9356 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9357 ctsio->be_move_done = ctl_config_move_done; 9358 ctl_datamove((union ctl_io *)ctsio); 9359 return (CTL_RETVAL_COMPLETE); 9360} 9361 9362int 9363ctl_tur(struct ctl_scsiio *ctsio) 9364{ 9365 9366 CTL_DEBUG_PRINT(("ctl_tur\n")); 9367 9368 ctl_set_success(ctsio); 9369 ctl_done((union ctl_io *)ctsio); 9370 9371 return (CTL_RETVAL_COMPLETE); 9372} 9373 9374/* 9375 * SCSI VPD page 0x00, the Supported VPD Pages page. 9376 */ 9377static int 9378ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9379{ 9380 struct scsi_vpd_supported_pages *pages; 9381 int sup_page_size; 9382 struct ctl_lun *lun; 9383 int p; 9384 9385 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9386 9387 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9388 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9389 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9390 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9391 ctsio->kern_sg_entries = 0; 9392 9393 if (sup_page_size < alloc_len) { 9394 ctsio->residual = alloc_len - sup_page_size; 9395 ctsio->kern_data_len = sup_page_size; 9396 ctsio->kern_total_len = sup_page_size; 9397 } else { 9398 ctsio->residual = 0; 9399 ctsio->kern_data_len = alloc_len; 9400 ctsio->kern_total_len = alloc_len; 9401 } 9402 ctsio->kern_data_resid = 0; 9403 ctsio->kern_rel_offset = 0; 9404 ctsio->kern_sg_entries = 0; 9405 9406 /* 9407 * The control device is always connected. The disk device, on the 9408 * other hand, may not be online all the time. Need to change this 9409 * to figure out whether the disk device is actually online or not. 9410 */ 9411 if (lun != NULL) 9412 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9413 lun->be_lun->lun_type; 9414 else 9415 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9416 9417 p = 0; 9418 /* Supported VPD pages */ 9419 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9420 /* Serial Number */ 9421 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9422 /* Device Identification */ 9423 pages->page_list[p++] = SVPD_DEVICE_ID; 9424 /* Extended INQUIRY Data */ 9425 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9426 /* Mode Page Policy */ 9427 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9428 /* SCSI Ports */ 9429 pages->page_list[p++] = SVPD_SCSI_PORTS; 9430 /* Third-party Copy */ 9431 pages->page_list[p++] = SVPD_SCSI_TPC; 9432 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9433 /* Block limits */ 9434 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9435 /* Block Device Characteristics */ 9436 pages->page_list[p++] = SVPD_BDC; 9437 /* Logical Block Provisioning */ 9438 pages->page_list[p++] = SVPD_LBP; 9439 } 9440 pages->length = p; 9441 9442 ctl_set_success(ctsio); 9443 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9444 ctsio->be_move_done = ctl_config_move_done; 9445 ctl_datamove((union ctl_io *)ctsio); 9446 return (CTL_RETVAL_COMPLETE); 9447} 9448 9449/* 9450 * SCSI VPD page 0x80, the Unit Serial Number page. 9451 */ 9452static int 9453ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9454{ 9455 struct scsi_vpd_unit_serial_number *sn_ptr; 9456 struct ctl_lun *lun; 9457 int data_len; 9458 9459 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9460 9461 data_len = 4 + CTL_SN_LEN; 9462 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9463 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9464 if (data_len < alloc_len) { 9465 ctsio->residual = alloc_len - data_len; 9466 ctsio->kern_data_len = data_len; 9467 ctsio->kern_total_len = data_len; 9468 } else { 9469 ctsio->residual = 0; 9470 ctsio->kern_data_len = alloc_len; 9471 ctsio->kern_total_len = alloc_len; 9472 } 9473 ctsio->kern_data_resid = 0; 9474 ctsio->kern_rel_offset = 0; 9475 ctsio->kern_sg_entries = 0; 9476 9477 /* 9478 * The control device is always connected. The disk device, on the 9479 * other hand, may not be online all the time. Need to change this 9480 * to figure out whether the disk device is actually online or not. 9481 */ 9482 if (lun != NULL) 9483 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9484 lun->be_lun->lun_type; 9485 else 9486 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9487 9488 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9489 sn_ptr->length = CTL_SN_LEN; 9490 /* 9491 * If we don't have a LUN, we just leave the serial number as 9492 * all spaces. 9493 */ 9494 if (lun != NULL) { 9495 strncpy((char *)sn_ptr->serial_num, 9496 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9497 } else 9498 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9499 9500 ctl_set_success(ctsio); 9501 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9502 ctsio->be_move_done = ctl_config_move_done; 9503 ctl_datamove((union ctl_io *)ctsio); 9504 return (CTL_RETVAL_COMPLETE); 9505} 9506 9507 9508/* 9509 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9510 */ 9511static int 9512ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9513{ 9514 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9515 struct ctl_lun *lun; 9516 int data_len; 9517 9518 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9519 9520 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9521 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9522 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9523 ctsio->kern_sg_entries = 0; 9524 9525 if (data_len < alloc_len) { 9526 ctsio->residual = alloc_len - data_len; 9527 ctsio->kern_data_len = data_len; 9528 ctsio->kern_total_len = data_len; 9529 } else { 9530 ctsio->residual = 0; 9531 ctsio->kern_data_len = alloc_len; 9532 ctsio->kern_total_len = alloc_len; 9533 } 9534 ctsio->kern_data_resid = 0; 9535 ctsio->kern_rel_offset = 0; 9536 ctsio->kern_sg_entries = 0; 9537 9538 /* 9539 * The control device is always connected. The disk device, on the 9540 * other hand, may not be online all the time. 9541 */ 9542 if (lun != NULL) 9543 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9544 lun->be_lun->lun_type; 9545 else 9546 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9547 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9548 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9549 /* 9550 * We support head of queue, ordered and simple tags. 9551 */ 9552 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9553 /* 9554 * Volatile cache supported. 9555 */ 9556 eid_ptr->flags3 = SVPD_EID_V_SUP; 9557 9558 /* 9559 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9560 * attention for a particular IT nexus on all LUNs once we report 9561 * it to that nexus once. This bit is required as of SPC-4. 9562 */ 9563 eid_ptr->flags4 = SVPD_EID_LUICLR; 9564 9565 /* 9566 * XXX KDM in order to correctly answer this, we would need 9567 * information from the SIM to determine how much sense data it 9568 * can send. So this would really be a path inquiry field, most 9569 * likely. This can be set to a maximum of 252 according to SPC-4, 9570 * but the hardware may or may not be able to support that much. 9571 * 0 just means that the maximum sense data length is not reported. 9572 */ 9573 eid_ptr->max_sense_length = 0; 9574 9575 ctl_set_success(ctsio); 9576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9577 ctsio->be_move_done = ctl_config_move_done; 9578 ctl_datamove((union ctl_io *)ctsio); 9579 return (CTL_RETVAL_COMPLETE); 9580} 9581 9582static int 9583ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9584{ 9585 struct scsi_vpd_mode_page_policy *mpp_ptr; 9586 struct ctl_lun *lun; 9587 int data_len; 9588 9589 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9590 9591 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9592 sizeof(struct scsi_vpd_mode_page_policy_descr); 9593 9594 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9595 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9596 ctsio->kern_sg_entries = 0; 9597 9598 if (data_len < alloc_len) { 9599 ctsio->residual = alloc_len - data_len; 9600 ctsio->kern_data_len = data_len; 9601 ctsio->kern_total_len = data_len; 9602 } else { 9603 ctsio->residual = 0; 9604 ctsio->kern_data_len = alloc_len; 9605 ctsio->kern_total_len = alloc_len; 9606 } 9607 ctsio->kern_data_resid = 0; 9608 ctsio->kern_rel_offset = 0; 9609 ctsio->kern_sg_entries = 0; 9610 9611 /* 9612 * The control device is always connected. The disk device, on the 9613 * other hand, may not be online all the time. 9614 */ 9615 if (lun != NULL) 9616 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9617 lun->be_lun->lun_type; 9618 else 9619 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9620 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9621 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9622 mpp_ptr->descr[0].page_code = 0x3f; 9623 mpp_ptr->descr[0].subpage_code = 0xff; 9624 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9625 9626 ctl_set_success(ctsio); 9627 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9628 ctsio->be_move_done = ctl_config_move_done; 9629 ctl_datamove((union ctl_io *)ctsio); 9630 return (CTL_RETVAL_COMPLETE); 9631} 9632 9633/* 9634 * SCSI VPD page 0x83, the Device Identification page. 9635 */ 9636static int 9637ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9638{ 9639 struct scsi_vpd_device_id *devid_ptr; 9640 struct scsi_vpd_id_descriptor *desc; 9641 struct ctl_softc *softc; 9642 struct ctl_lun *lun; 9643 struct ctl_port *port; 9644 int data_len, g; 9645 uint8_t proto; 9646 9647 softc = control_softc; 9648 9649 port = ctl_io_port(&ctsio->io_hdr); 9650 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9651 9652 data_len = sizeof(struct scsi_vpd_device_id) + 9653 sizeof(struct scsi_vpd_id_descriptor) + 9654 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9655 sizeof(struct scsi_vpd_id_descriptor) + 9656 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9657 if (lun && lun->lun_devid) 9658 data_len += lun->lun_devid->len; 9659 if (port && port->port_devid) 9660 data_len += port->port_devid->len; 9661 if (port && port->target_devid) 9662 data_len += port->target_devid->len; 9663 9664 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9665 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9666 ctsio->kern_sg_entries = 0; 9667 9668 if (data_len < alloc_len) { 9669 ctsio->residual = alloc_len - data_len; 9670 ctsio->kern_data_len = data_len; 9671 ctsio->kern_total_len = data_len; 9672 } else { 9673 ctsio->residual = 0; 9674 ctsio->kern_data_len = alloc_len; 9675 ctsio->kern_total_len = alloc_len; 9676 } 9677 ctsio->kern_data_resid = 0; 9678 ctsio->kern_rel_offset = 0; 9679 ctsio->kern_sg_entries = 0; 9680 9681 /* 9682 * The control device is always connected. The disk device, on the 9683 * other hand, may not be online all the time. 9684 */ 9685 if (lun != NULL) 9686 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9687 lun->be_lun->lun_type; 9688 else 9689 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9690 devid_ptr->page_code = SVPD_DEVICE_ID; 9691 scsi_ulto2b(data_len - 4, devid_ptr->length); 9692 9693 if (port && port->port_type == CTL_PORT_FC) 9694 proto = SCSI_PROTO_FC << 4; 9695 else if (port && port->port_type == CTL_PORT_ISCSI) 9696 proto = SCSI_PROTO_ISCSI << 4; 9697 else 9698 proto = SCSI_PROTO_SPI << 4; 9699 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9700 9701 /* 9702 * We're using a LUN association here. i.e., this device ID is a 9703 * per-LUN identifier. 9704 */ 9705 if (lun && lun->lun_devid) { 9706 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9707 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9708 lun->lun_devid->len); 9709 } 9710 9711 /* 9712 * This is for the WWPN which is a port association. 9713 */ 9714 if (port && port->port_devid) { 9715 memcpy(desc, port->port_devid->data, port->port_devid->len); 9716 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9717 port->port_devid->len); 9718 } 9719 9720 /* 9721 * This is for the Relative Target Port(type 4h) identifier 9722 */ 9723 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9724 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9725 SVPD_ID_TYPE_RELTARG; 9726 desc->length = 4; 9727 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9728 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9729 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9730 9731 /* 9732 * This is for the Target Port Group(type 5h) identifier 9733 */ 9734 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9735 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9736 SVPD_ID_TYPE_TPORTGRP; 9737 desc->length = 4; 9738 if (softc->is_single || 9739 (port && port->status & CTL_PORT_STATUS_HA_SHARED)) 9740 g = 1; 9741 else 9742 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt; 9743 scsi_ulto2b(g, &desc->identifier[2]); 9744 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9745 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9746 9747 /* 9748 * This is for the Target identifier 9749 */ 9750 if (port && port->target_devid) { 9751 memcpy(desc, port->target_devid->data, port->target_devid->len); 9752 } 9753 9754 ctl_set_success(ctsio); 9755 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9756 ctsio->be_move_done = ctl_config_move_done; 9757 ctl_datamove((union ctl_io *)ctsio); 9758 return (CTL_RETVAL_COMPLETE); 9759} 9760 9761static int 9762ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9763{ 9764 struct ctl_softc *softc = control_softc; 9765 struct scsi_vpd_scsi_ports *sp; 9766 struct scsi_vpd_port_designation *pd; 9767 struct scsi_vpd_port_designation_cont *pdc; 9768 struct ctl_lun *lun; 9769 struct ctl_port *port; 9770 int data_len, num_target_ports, iid_len, id_len; 9771 9772 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9773 9774 num_target_ports = 0; 9775 iid_len = 0; 9776 id_len = 0; 9777 mtx_lock(&softc->ctl_lock); 9778 STAILQ_FOREACH(port, &softc->port_list, links) { 9779 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9780 continue; 9781 if (lun != NULL && 9782 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9783 continue; 9784 num_target_ports++; 9785 if (port->init_devid) 9786 iid_len += port->init_devid->len; 9787 if (port->port_devid) 9788 id_len += port->port_devid->len; 9789 } 9790 mtx_unlock(&softc->ctl_lock); 9791 9792 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9793 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9794 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9795 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9796 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9797 ctsio->kern_sg_entries = 0; 9798 9799 if (data_len < alloc_len) { 9800 ctsio->residual = alloc_len - data_len; 9801 ctsio->kern_data_len = data_len; 9802 ctsio->kern_total_len = data_len; 9803 } else { 9804 ctsio->residual = 0; 9805 ctsio->kern_data_len = alloc_len; 9806 ctsio->kern_total_len = alloc_len; 9807 } 9808 ctsio->kern_data_resid = 0; 9809 ctsio->kern_rel_offset = 0; 9810 ctsio->kern_sg_entries = 0; 9811 9812 /* 9813 * The control device is always connected. The disk device, on the 9814 * other hand, may not be online all the time. Need to change this 9815 * to figure out whether the disk device is actually online or not. 9816 */ 9817 if (lun != NULL) 9818 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9819 lun->be_lun->lun_type; 9820 else 9821 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9822 9823 sp->page_code = SVPD_SCSI_PORTS; 9824 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9825 sp->page_length); 9826 pd = &sp->design[0]; 9827 9828 mtx_lock(&softc->ctl_lock); 9829 STAILQ_FOREACH(port, &softc->port_list, links) { 9830 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9831 continue; 9832 if (lun != NULL && 9833 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 9834 continue; 9835 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9836 if (port->init_devid) { 9837 iid_len = port->init_devid->len; 9838 memcpy(pd->initiator_transportid, 9839 port->init_devid->data, port->init_devid->len); 9840 } else 9841 iid_len = 0; 9842 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9843 pdc = (struct scsi_vpd_port_designation_cont *) 9844 (&pd->initiator_transportid[iid_len]); 9845 if (port->port_devid) { 9846 id_len = port->port_devid->len; 9847 memcpy(pdc->target_port_descriptors, 9848 port->port_devid->data, port->port_devid->len); 9849 } else 9850 id_len = 0; 9851 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9852 pd = (struct scsi_vpd_port_designation *) 9853 ((uint8_t *)pdc->target_port_descriptors + id_len); 9854 } 9855 mtx_unlock(&softc->ctl_lock); 9856 9857 ctl_set_success(ctsio); 9858 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9859 ctsio->be_move_done = ctl_config_move_done; 9860 ctl_datamove((union ctl_io *)ctsio); 9861 return (CTL_RETVAL_COMPLETE); 9862} 9863 9864static int 9865ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9866{ 9867 struct scsi_vpd_block_limits *bl_ptr; 9868 struct ctl_lun *lun; 9869 uint64_t ival; 9870 9871 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9872 9873 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9874 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9875 ctsio->kern_sg_entries = 0; 9876 9877 if (sizeof(*bl_ptr) < alloc_len) { 9878 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9879 ctsio->kern_data_len = sizeof(*bl_ptr); 9880 ctsio->kern_total_len = sizeof(*bl_ptr); 9881 } else { 9882 ctsio->residual = 0; 9883 ctsio->kern_data_len = alloc_len; 9884 ctsio->kern_total_len = alloc_len; 9885 } 9886 ctsio->kern_data_resid = 0; 9887 ctsio->kern_rel_offset = 0; 9888 ctsio->kern_sg_entries = 0; 9889 9890 /* 9891 * The control device is always connected. The disk device, on the 9892 * other hand, may not be online all the time. Need to change this 9893 * to figure out whether the disk device is actually online or not. 9894 */ 9895 if (lun != NULL) 9896 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9897 lun->be_lun->lun_type; 9898 else 9899 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9900 9901 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9902 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9903 bl_ptr->max_cmp_write_len = 0xff; 9904 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9905 if (lun != NULL) { 9906 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9907 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9908 ival = 0xffffffff; 9909 ctl_get_opt_number(&lun->be_lun->options, 9910 "unmap_max_lba", &ival); 9911 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt); 9912 ival = 0xffffffff; 9913 ctl_get_opt_number(&lun->be_lun->options, 9914 "unmap_max_descr", &ival); 9915 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt); 9916 if (lun->be_lun->ublockexp != 0) { 9917 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9918 bl_ptr->opt_unmap_grain); 9919 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9920 bl_ptr->unmap_grain_align); 9921 } 9922 } 9923 scsi_ulto4b(lun->be_lun->atomicblock, 9924 bl_ptr->max_atomic_transfer_length); 9925 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9926 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9927 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9928 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9929 ival = UINT64_MAX; 9930 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival); 9931 scsi_u64to8b(ival, bl_ptr->max_write_same_length); 9932 } 9933 9934 ctl_set_success(ctsio); 9935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9936 ctsio->be_move_done = ctl_config_move_done; 9937 ctl_datamove((union ctl_io *)ctsio); 9938 return (CTL_RETVAL_COMPLETE); 9939} 9940 9941static int 9942ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9943{ 9944 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9945 struct ctl_lun *lun; 9946 const char *value; 9947 u_int i; 9948 9949 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9950 9951 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9952 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9953 ctsio->kern_sg_entries = 0; 9954 9955 if (sizeof(*bdc_ptr) < alloc_len) { 9956 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9957 ctsio->kern_data_len = sizeof(*bdc_ptr); 9958 ctsio->kern_total_len = sizeof(*bdc_ptr); 9959 } else { 9960 ctsio->residual = 0; 9961 ctsio->kern_data_len = alloc_len; 9962 ctsio->kern_total_len = alloc_len; 9963 } 9964 ctsio->kern_data_resid = 0; 9965 ctsio->kern_rel_offset = 0; 9966 ctsio->kern_sg_entries = 0; 9967 9968 /* 9969 * The control device is always connected. The disk device, on the 9970 * other hand, may not be online all the time. Need to change this 9971 * to figure out whether the disk device is actually online or not. 9972 */ 9973 if (lun != NULL) 9974 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9975 lun->be_lun->lun_type; 9976 else 9977 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9978 bdc_ptr->page_code = SVPD_BDC; 9979 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9980 if (lun != NULL && 9981 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9982 i = strtol(value, NULL, 0); 9983 else 9984 i = CTL_DEFAULT_ROTATION_RATE; 9985 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9986 if (lun != NULL && 9987 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9988 i = strtol(value, NULL, 0); 9989 else 9990 i = 0; 9991 bdc_ptr->wab_wac_ff = (i & 0x0f); 9992 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9993 9994 ctl_set_success(ctsio); 9995 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9996 ctsio->be_move_done = ctl_config_move_done; 9997 ctl_datamove((union ctl_io *)ctsio); 9998 return (CTL_RETVAL_COMPLETE); 9999} 10000 10001static int 10002ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10003{ 10004 struct scsi_vpd_logical_block_prov *lbp_ptr; 10005 struct ctl_lun *lun; 10006 const char *value; 10007 10008 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10009 10010 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10011 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10012 ctsio->kern_sg_entries = 0; 10013 10014 if (sizeof(*lbp_ptr) < alloc_len) { 10015 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10016 ctsio->kern_data_len = sizeof(*lbp_ptr); 10017 ctsio->kern_total_len = sizeof(*lbp_ptr); 10018 } else { 10019 ctsio->residual = 0; 10020 ctsio->kern_data_len = alloc_len; 10021 ctsio->kern_total_len = alloc_len; 10022 } 10023 ctsio->kern_data_resid = 0; 10024 ctsio->kern_rel_offset = 0; 10025 ctsio->kern_sg_entries = 0; 10026 10027 /* 10028 * The control device is always connected. The disk device, on the 10029 * other hand, may not be online all the time. Need to change this 10030 * to figure out whether the disk device is actually online or not. 10031 */ 10032 if (lun != NULL) 10033 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10034 lun->be_lun->lun_type; 10035 else 10036 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10037 10038 lbp_ptr->page_code = SVPD_LBP; 10039 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10040 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10041 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10042 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10043 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10044 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type"); 10045 if (value != NULL) { 10046 if (strcmp(value, "resource") == 0) 10047 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10048 else if (strcmp(value, "thin") == 0) 10049 lbp_ptr->prov_type = SVPD_LBP_THIN; 10050 } else 10051 lbp_ptr->prov_type = SVPD_LBP_THIN; 10052 } 10053 10054 ctl_set_success(ctsio); 10055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10056 ctsio->be_move_done = ctl_config_move_done; 10057 ctl_datamove((union ctl_io *)ctsio); 10058 return (CTL_RETVAL_COMPLETE); 10059} 10060 10061/* 10062 * INQUIRY with the EVPD bit set. 10063 */ 10064static int 10065ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10066{ 10067 struct ctl_lun *lun; 10068 struct scsi_inquiry *cdb; 10069 int alloc_len, retval; 10070 10071 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10072 cdb = (struct scsi_inquiry *)ctsio->cdb; 10073 alloc_len = scsi_2btoul(cdb->length); 10074 10075 switch (cdb->page_code) { 10076 case SVPD_SUPPORTED_PAGES: 10077 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10078 break; 10079 case SVPD_UNIT_SERIAL_NUMBER: 10080 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10081 break; 10082 case SVPD_DEVICE_ID: 10083 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10084 break; 10085 case SVPD_EXTENDED_INQUIRY_DATA: 10086 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10087 break; 10088 case SVPD_MODE_PAGE_POLICY: 10089 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10090 break; 10091 case SVPD_SCSI_PORTS: 10092 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10093 break; 10094 case SVPD_SCSI_TPC: 10095 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10096 break; 10097 case SVPD_BLOCK_LIMITS: 10098 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10099 goto err; 10100 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10101 break; 10102 case SVPD_BDC: 10103 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10104 goto err; 10105 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10106 break; 10107 case SVPD_LBP: 10108 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10109 goto err; 10110 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10111 break; 10112 default: 10113err: 10114 ctl_set_invalid_field(ctsio, 10115 /*sks_valid*/ 1, 10116 /*command*/ 1, 10117 /*field*/ 2, 10118 /*bit_valid*/ 0, 10119 /*bit*/ 0); 10120 ctl_done((union ctl_io *)ctsio); 10121 retval = CTL_RETVAL_COMPLETE; 10122 break; 10123 } 10124 10125 return (retval); 10126} 10127 10128/* 10129 * Standard INQUIRY data. 10130 */ 10131static int 10132ctl_inquiry_std(struct ctl_scsiio *ctsio) 10133{ 10134 struct scsi_inquiry_data *inq_ptr; 10135 struct scsi_inquiry *cdb; 10136 struct ctl_softc *softc = control_softc; 10137 struct ctl_port *port; 10138 struct ctl_lun *lun; 10139 char *val; 10140 uint32_t alloc_len, data_len; 10141 ctl_port_type port_type; 10142 10143 port = ctl_io_port(&ctsio->io_hdr); 10144 port_type = port->port_type; 10145 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10146 port_type = CTL_PORT_SCSI; 10147 10148 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10149 cdb = (struct scsi_inquiry *)ctsio->cdb; 10150 alloc_len = scsi_2btoul(cdb->length); 10151 10152 /* 10153 * We malloc the full inquiry data size here and fill it 10154 * in. If the user only asks for less, we'll give him 10155 * that much. 10156 */ 10157 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10158 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10159 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10160 ctsio->kern_sg_entries = 0; 10161 ctsio->kern_data_resid = 0; 10162 ctsio->kern_rel_offset = 0; 10163 10164 if (data_len < alloc_len) { 10165 ctsio->residual = alloc_len - data_len; 10166 ctsio->kern_data_len = data_len; 10167 ctsio->kern_total_len = data_len; 10168 } else { 10169 ctsio->residual = 0; 10170 ctsio->kern_data_len = alloc_len; 10171 ctsio->kern_total_len = alloc_len; 10172 } 10173 10174 if (lun != NULL) { 10175 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10176 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10177 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10178 lun->be_lun->lun_type; 10179 } else { 10180 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10181 lun->be_lun->lun_type; 10182 } 10183 if (lun->flags & CTL_LUN_REMOVABLE) 10184 inq_ptr->dev_qual2 |= SID_RMB; 10185 } else 10186 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10187 10188 /* RMB in byte 2 is 0 */ 10189 inq_ptr->version = SCSI_REV_SPC5; 10190 10191 /* 10192 * According to SAM-3, even if a device only supports a single 10193 * level of LUN addressing, it should still set the HISUP bit: 10194 * 10195 * 4.9.1 Logical unit numbers overview 10196 * 10197 * All logical unit number formats described in this standard are 10198 * hierarchical in structure even when only a single level in that 10199 * hierarchy is used. The HISUP bit shall be set to one in the 10200 * standard INQUIRY data (see SPC-2) when any logical unit number 10201 * format described in this standard is used. Non-hierarchical 10202 * formats are outside the scope of this standard. 10203 * 10204 * Therefore we set the HiSup bit here. 10205 * 10206 * The response format is 2, per SPC-3. 10207 */ 10208 inq_ptr->response_format = SID_HiSup | 2; 10209 10210 inq_ptr->additional_length = data_len - 10211 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10212 CTL_DEBUG_PRINT(("additional_length = %d\n", 10213 inq_ptr->additional_length)); 10214 10215 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10216 if (port_type == CTL_PORT_SCSI) 10217 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10218 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10219 inq_ptr->flags = SID_CmdQue; 10220 if (port_type == CTL_PORT_SCSI) 10221 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10222 10223 /* 10224 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10225 * We have 8 bytes for the vendor name, and 16 bytes for the device 10226 * name and 4 bytes for the revision. 10227 */ 10228 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10229 "vendor")) == NULL) { 10230 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10231 } else { 10232 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10233 strncpy(inq_ptr->vendor, val, 10234 min(sizeof(inq_ptr->vendor), strlen(val))); 10235 } 10236 if (lun == NULL) { 10237 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10238 sizeof(inq_ptr->product)); 10239 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10240 switch (lun->be_lun->lun_type) { 10241 case T_DIRECT: 10242 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10243 sizeof(inq_ptr->product)); 10244 break; 10245 case T_PROCESSOR: 10246 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10247 sizeof(inq_ptr->product)); 10248 break; 10249 case T_CDROM: 10250 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10251 sizeof(inq_ptr->product)); 10252 break; 10253 default: 10254 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10255 sizeof(inq_ptr->product)); 10256 break; 10257 } 10258 } else { 10259 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10260 strncpy(inq_ptr->product, val, 10261 min(sizeof(inq_ptr->product), strlen(val))); 10262 } 10263 10264 /* 10265 * XXX make this a macro somewhere so it automatically gets 10266 * incremented when we make changes. 10267 */ 10268 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10269 "revision")) == NULL) { 10270 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10271 } else { 10272 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10273 strncpy(inq_ptr->revision, val, 10274 min(sizeof(inq_ptr->revision), strlen(val))); 10275 } 10276 10277 /* 10278 * For parallel SCSI, we support double transition and single 10279 * transition clocking. We also support QAS (Quick Arbitration 10280 * and Selection) and Information Unit transfers on both the 10281 * control and array devices. 10282 */ 10283 if (port_type == CTL_PORT_SCSI) 10284 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10285 SID_SPI_IUS; 10286 10287 /* SAM-6 (no version claimed) */ 10288 scsi_ulto2b(0x00C0, inq_ptr->version1); 10289 /* SPC-5 (no version claimed) */ 10290 scsi_ulto2b(0x05C0, inq_ptr->version2); 10291 if (port_type == CTL_PORT_FC) { 10292 /* FCP-2 ANSI INCITS.350:2003 */ 10293 scsi_ulto2b(0x0917, inq_ptr->version3); 10294 } else if (port_type == CTL_PORT_SCSI) { 10295 /* SPI-4 ANSI INCITS.362:200x */ 10296 scsi_ulto2b(0x0B56, inq_ptr->version3); 10297 } else if (port_type == CTL_PORT_ISCSI) { 10298 /* iSCSI (no version claimed) */ 10299 scsi_ulto2b(0x0960, inq_ptr->version3); 10300 } else if (port_type == CTL_PORT_SAS) { 10301 /* SAS (no version claimed) */ 10302 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10303 } 10304 10305 if (lun == NULL) { 10306 /* SBC-4 (no version claimed) */ 10307 scsi_ulto2b(0x0600, inq_ptr->version4); 10308 } else { 10309 switch (lun->be_lun->lun_type) { 10310 case T_DIRECT: 10311 /* SBC-4 (no version claimed) */ 10312 scsi_ulto2b(0x0600, inq_ptr->version4); 10313 break; 10314 case T_PROCESSOR: 10315 break; 10316 case T_CDROM: 10317 /* MMC-6 (no version claimed) */ 10318 scsi_ulto2b(0x04E0, inq_ptr->version4); 10319 break; 10320 default: 10321 break; 10322 } 10323 } 10324 10325 ctl_set_success(ctsio); 10326 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10327 ctsio->be_move_done = ctl_config_move_done; 10328 ctl_datamove((union ctl_io *)ctsio); 10329 return (CTL_RETVAL_COMPLETE); 10330} 10331 10332int 10333ctl_inquiry(struct ctl_scsiio *ctsio) 10334{ 10335 struct scsi_inquiry *cdb; 10336 int retval; 10337 10338 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10339 10340 cdb = (struct scsi_inquiry *)ctsio->cdb; 10341 if (cdb->byte2 & SI_EVPD) 10342 retval = ctl_inquiry_evpd(ctsio); 10343 else if (cdb->page_code == 0) 10344 retval = ctl_inquiry_std(ctsio); 10345 else { 10346 ctl_set_invalid_field(ctsio, 10347 /*sks_valid*/ 1, 10348 /*command*/ 1, 10349 /*field*/ 2, 10350 /*bit_valid*/ 0, 10351 /*bit*/ 0); 10352 ctl_done((union ctl_io *)ctsio); 10353 return (CTL_RETVAL_COMPLETE); 10354 } 10355 10356 return (retval); 10357} 10358 10359int 10360ctl_get_config(struct ctl_scsiio *ctsio) 10361{ 10362 struct scsi_get_config_header *hdr; 10363 struct scsi_get_config_feature *feature; 10364 struct scsi_get_config *cdb; 10365 struct ctl_lun *lun; 10366 uint32_t alloc_len, data_len; 10367 int rt, starting; 10368 10369 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10370 cdb = (struct scsi_get_config *)ctsio->cdb; 10371 rt = (cdb->rt & SGC_RT_MASK); 10372 starting = scsi_2btoul(cdb->starting_feature); 10373 alloc_len = scsi_2btoul(cdb->length); 10374 10375 data_len = sizeof(struct scsi_get_config_header) + 10376 sizeof(struct scsi_get_config_feature) + 8 + 10377 sizeof(struct scsi_get_config_feature) + 8 + 10378 sizeof(struct scsi_get_config_feature) + 4 + 10379 sizeof(struct scsi_get_config_feature) + 4 + 10380 sizeof(struct scsi_get_config_feature) + 8 + 10381 sizeof(struct scsi_get_config_feature) + 10382 sizeof(struct scsi_get_config_feature) + 4 + 10383 sizeof(struct scsi_get_config_feature) + 4 + 10384 sizeof(struct scsi_get_config_feature) + 4 + 10385 sizeof(struct scsi_get_config_feature) + 4 + 10386 sizeof(struct scsi_get_config_feature) + 4 + 10387 sizeof(struct scsi_get_config_feature) + 4; 10388 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10389 ctsio->kern_sg_entries = 0; 10390 ctsio->kern_data_resid = 0; 10391 ctsio->kern_rel_offset = 0; 10392 10393 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10394 if (lun->flags & CTL_LUN_NO_MEDIA) 10395 scsi_ulto2b(0x0000, hdr->current_profile); 10396 else 10397 scsi_ulto2b(0x0010, hdr->current_profile); 10398 feature = (struct scsi_get_config_feature *)(hdr + 1); 10399 10400 if (starting > 0x003b) 10401 goto done; 10402 if (starting > 0x003a) 10403 goto f3b; 10404 if (starting > 0x002b) 10405 goto f3a; 10406 if (starting > 0x002a) 10407 goto f2b; 10408 if (starting > 0x001f) 10409 goto f2a; 10410 if (starting > 0x001e) 10411 goto f1f; 10412 if (starting > 0x001d) 10413 goto f1e; 10414 if (starting > 0x0010) 10415 goto f1d; 10416 if (starting > 0x0003) 10417 goto f10; 10418 if (starting > 0x0002) 10419 goto f3; 10420 if (starting > 0x0001) 10421 goto f2; 10422 if (starting > 0x0000) 10423 goto f1; 10424 10425 /* Profile List */ 10426 scsi_ulto2b(0x0000, feature->feature_code); 10427 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10428 feature->add_length = 8; 10429 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10430 feature->feature_data[2] = 0x00; 10431 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10432 feature->feature_data[6] = 0x01; 10433 feature = (struct scsi_get_config_feature *) 10434 &feature->feature_data[feature->add_length]; 10435 10436f1: /* Core */ 10437 scsi_ulto2b(0x0001, feature->feature_code); 10438 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10439 feature->add_length = 8; 10440 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10441 feature->feature_data[4] = 0x03; 10442 feature = (struct scsi_get_config_feature *) 10443 &feature->feature_data[feature->add_length]; 10444 10445f2: /* Morphing */ 10446 scsi_ulto2b(0x0002, feature->feature_code); 10447 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10448 feature->add_length = 4; 10449 feature->feature_data[0] = 0x02; 10450 feature = (struct scsi_get_config_feature *) 10451 &feature->feature_data[feature->add_length]; 10452 10453f3: /* Removable Medium */ 10454 scsi_ulto2b(0x0003, feature->feature_code); 10455 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10456 feature->add_length = 4; 10457 feature->feature_data[0] = 0x39; 10458 feature = (struct scsi_get_config_feature *) 10459 &feature->feature_data[feature->add_length]; 10460 10461 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10462 goto done; 10463 10464f10: /* Random Read */ 10465 scsi_ulto2b(0x0010, 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 = 8; 10470 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10471 scsi_ulto2b(1, &feature->feature_data[4]); 10472 feature->feature_data[6] = 0x00; 10473 feature = (struct scsi_get_config_feature *) 10474 &feature->feature_data[feature->add_length]; 10475 10476f1d: /* Multi-Read */ 10477 scsi_ulto2b(0x001D, feature->feature_code); 10478 feature->flags = 0x00; 10479 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10480 feature->flags |= SGC_F_CURRENT; 10481 feature->add_length = 0; 10482 feature = (struct scsi_get_config_feature *) 10483 &feature->feature_data[feature->add_length]; 10484 10485f1e: /* CD Read */ 10486 scsi_ulto2b(0x001E, feature->feature_code); 10487 feature->flags = 0x00; 10488 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10489 feature->flags |= SGC_F_CURRENT; 10490 feature->add_length = 4; 10491 feature->feature_data[0] = 0x00; 10492 feature = (struct scsi_get_config_feature *) 10493 &feature->feature_data[feature->add_length]; 10494 10495f1f: /* DVD Read */ 10496 scsi_ulto2b(0x001F, feature->feature_code); 10497 feature->flags = 0x08; 10498 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10499 feature->flags |= SGC_F_CURRENT; 10500 feature->add_length = 4; 10501 feature->feature_data[0] = 0x01; 10502 feature->feature_data[2] = 0x03; 10503 feature = (struct scsi_get_config_feature *) 10504 &feature->feature_data[feature->add_length]; 10505 10506f2a: /* DVD+RW */ 10507 scsi_ulto2b(0x002A, feature->feature_code); 10508 feature->flags = 0x04; 10509 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10510 feature->flags |= SGC_F_CURRENT; 10511 feature->add_length = 4; 10512 feature->feature_data[0] = 0x00; 10513 feature->feature_data[1] = 0x00; 10514 feature = (struct scsi_get_config_feature *) 10515 &feature->feature_data[feature->add_length]; 10516 10517f2b: /* DVD+R */ 10518 scsi_ulto2b(0x002B, feature->feature_code); 10519 feature->flags = 0x00; 10520 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10521 feature->flags |= SGC_F_CURRENT; 10522 feature->add_length = 4; 10523 feature->feature_data[0] = 0x00; 10524 feature = (struct scsi_get_config_feature *) 10525 &feature->feature_data[feature->add_length]; 10526 10527f3a: /* DVD+RW Dual Layer */ 10528 scsi_ulto2b(0x003A, feature->feature_code); 10529 feature->flags = 0x00; 10530 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10531 feature->flags |= SGC_F_CURRENT; 10532 feature->add_length = 4; 10533 feature->feature_data[0] = 0x00; 10534 feature->feature_data[1] = 0x00; 10535 feature = (struct scsi_get_config_feature *) 10536 &feature->feature_data[feature->add_length]; 10537 10538f3b: /* DVD+R Dual Layer */ 10539 scsi_ulto2b(0x003B, feature->feature_code); 10540 feature->flags = 0x00; 10541 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10542 feature->flags |= SGC_F_CURRENT; 10543 feature->add_length = 4; 10544 feature->feature_data[0] = 0x00; 10545 feature = (struct scsi_get_config_feature *) 10546 &feature->feature_data[feature->add_length]; 10547 10548done: 10549 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10550 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10551 feature = (struct scsi_get_config_feature *)(hdr + 1); 10552 if (scsi_2btoul(feature->feature_code) == starting) 10553 feature = (struct scsi_get_config_feature *) 10554 &feature->feature_data[feature->add_length]; 10555 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10556 } 10557 scsi_ulto4b(data_len - 4, hdr->data_length); 10558 if (data_len < alloc_len) { 10559 ctsio->residual = alloc_len - data_len; 10560 ctsio->kern_data_len = data_len; 10561 ctsio->kern_total_len = data_len; 10562 } else { 10563 ctsio->residual = 0; 10564 ctsio->kern_data_len = alloc_len; 10565 ctsio->kern_total_len = alloc_len; 10566 } 10567 10568 ctl_set_success(ctsio); 10569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10570 ctsio->be_move_done = ctl_config_move_done; 10571 ctl_datamove((union ctl_io *)ctsio); 10572 return (CTL_RETVAL_COMPLETE); 10573} 10574 10575int 10576ctl_get_event_status(struct ctl_scsiio *ctsio) 10577{ 10578 struct scsi_get_event_status_header *hdr; 10579 struct scsi_get_event_status *cdb; 10580 struct ctl_lun *lun; 10581 uint32_t alloc_len, data_len; 10582 int notif_class; 10583 10584 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10585 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10586 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10587 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10588 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10589 ctl_done((union ctl_io *)ctsio); 10590 return (CTL_RETVAL_COMPLETE); 10591 } 10592 notif_class = cdb->notif_class; 10593 alloc_len = scsi_2btoul(cdb->length); 10594 10595 data_len = sizeof(struct scsi_get_event_status_header); 10596 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10597 ctsio->kern_sg_entries = 0; 10598 ctsio->kern_data_resid = 0; 10599 ctsio->kern_rel_offset = 0; 10600 10601 if (data_len < alloc_len) { 10602 ctsio->residual = alloc_len - data_len; 10603 ctsio->kern_data_len = data_len; 10604 ctsio->kern_total_len = data_len; 10605 } else { 10606 ctsio->residual = 0; 10607 ctsio->kern_data_len = alloc_len; 10608 ctsio->kern_total_len = alloc_len; 10609 } 10610 10611 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10612 scsi_ulto2b(0, hdr->descr_length); 10613 hdr->nea_class = SGESN_NEA; 10614 hdr->supported_class = 0; 10615 10616 ctl_set_success(ctsio); 10617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10618 ctsio->be_move_done = ctl_config_move_done; 10619 ctl_datamove((union ctl_io *)ctsio); 10620 return (CTL_RETVAL_COMPLETE); 10621} 10622 10623int 10624ctl_mechanism_status(struct ctl_scsiio *ctsio) 10625{ 10626 struct scsi_mechanism_status_header *hdr; 10627 struct scsi_mechanism_status *cdb; 10628 struct ctl_lun *lun; 10629 uint32_t alloc_len, data_len; 10630 10631 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10632 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10633 alloc_len = scsi_2btoul(cdb->length); 10634 10635 data_len = sizeof(struct scsi_mechanism_status_header); 10636 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10637 ctsio->kern_sg_entries = 0; 10638 ctsio->kern_data_resid = 0; 10639 ctsio->kern_rel_offset = 0; 10640 10641 if (data_len < alloc_len) { 10642 ctsio->residual = alloc_len - data_len; 10643 ctsio->kern_data_len = data_len; 10644 ctsio->kern_total_len = data_len; 10645 } else { 10646 ctsio->residual = 0; 10647 ctsio->kern_data_len = alloc_len; 10648 ctsio->kern_total_len = alloc_len; 10649 } 10650 10651 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10652 hdr->state1 = 0x00; 10653 hdr->state2 = 0xe0; 10654 scsi_ulto3b(0, hdr->lba); 10655 hdr->slots_num = 0; 10656 scsi_ulto2b(0, hdr->slots_length); 10657 10658 ctl_set_success(ctsio); 10659 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10660 ctsio->be_move_done = ctl_config_move_done; 10661 ctl_datamove((union ctl_io *)ctsio); 10662 return (CTL_RETVAL_COMPLETE); 10663} 10664 10665static void 10666ctl_ultomsf(uint32_t lba, uint8_t *buf) 10667{ 10668 10669 lba += 150; 10670 buf[0] = 0; 10671 buf[1] = bin2bcd((lba / 75) / 60); 10672 buf[2] = bin2bcd((lba / 75) % 60); 10673 buf[3] = bin2bcd(lba % 75); 10674} 10675 10676int 10677ctl_read_toc(struct ctl_scsiio *ctsio) 10678{ 10679 struct scsi_read_toc_hdr *hdr; 10680 struct scsi_read_toc_type01_descr *descr; 10681 struct scsi_read_toc *cdb; 10682 struct ctl_lun *lun; 10683 uint32_t alloc_len, data_len; 10684 int format, msf; 10685 10686 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10687 cdb = (struct scsi_read_toc *)ctsio->cdb; 10688 msf = (cdb->byte2 & CD_MSF) != 0; 10689 format = cdb->format; 10690 alloc_len = scsi_2btoul(cdb->data_len); 10691 10692 data_len = sizeof(struct scsi_read_toc_hdr); 10693 if (format == 0) 10694 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10695 else 10696 data_len += sizeof(struct scsi_read_toc_type01_descr); 10697 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10698 ctsio->kern_sg_entries = 0; 10699 ctsio->kern_data_resid = 0; 10700 ctsio->kern_rel_offset = 0; 10701 10702 if (data_len < alloc_len) { 10703 ctsio->residual = alloc_len - data_len; 10704 ctsio->kern_data_len = data_len; 10705 ctsio->kern_total_len = data_len; 10706 } else { 10707 ctsio->residual = 0; 10708 ctsio->kern_data_len = alloc_len; 10709 ctsio->kern_total_len = alloc_len; 10710 } 10711 10712 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10713 if (format == 0) { 10714 scsi_ulto2b(0x12, hdr->data_length); 10715 hdr->first = 1; 10716 hdr->last = 1; 10717 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10718 descr->addr_ctl = 0x14; 10719 descr->track_number = 1; 10720 if (msf) 10721 ctl_ultomsf(0, descr->track_start); 10722 else 10723 scsi_ulto4b(0, descr->track_start); 10724 descr++; 10725 descr->addr_ctl = 0x14; 10726 descr->track_number = 0xaa; 10727 if (msf) 10728 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10729 else 10730 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10731 } else { 10732 scsi_ulto2b(0x0a, hdr->data_length); 10733 hdr->first = 1; 10734 hdr->last = 1; 10735 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10736 descr->addr_ctl = 0x14; 10737 descr->track_number = 1; 10738 if (msf) 10739 ctl_ultomsf(0, descr->track_start); 10740 else 10741 scsi_ulto4b(0, descr->track_start); 10742 } 10743 10744 ctl_set_success(ctsio); 10745 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10746 ctsio->be_move_done = ctl_config_move_done; 10747 ctl_datamove((union ctl_io *)ctsio); 10748 return (CTL_RETVAL_COMPLETE); 10749} 10750 10751/* 10752 * For known CDB types, parse the LBA and length. 10753 */ 10754static int 10755ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10756{ 10757 if (io->io_hdr.io_type != CTL_IO_SCSI) 10758 return (1); 10759 10760 switch (io->scsiio.cdb[0]) { 10761 case COMPARE_AND_WRITE: { 10762 struct scsi_compare_and_write *cdb; 10763 10764 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10765 10766 *lba = scsi_8btou64(cdb->addr); 10767 *len = cdb->length; 10768 break; 10769 } 10770 case READ_6: 10771 case WRITE_6: { 10772 struct scsi_rw_6 *cdb; 10773 10774 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10775 10776 *lba = scsi_3btoul(cdb->addr); 10777 /* only 5 bits are valid in the most significant address byte */ 10778 *lba &= 0x1fffff; 10779 *len = cdb->length; 10780 break; 10781 } 10782 case READ_10: 10783 case WRITE_10: { 10784 struct scsi_rw_10 *cdb; 10785 10786 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10787 10788 *lba = scsi_4btoul(cdb->addr); 10789 *len = scsi_2btoul(cdb->length); 10790 break; 10791 } 10792 case WRITE_VERIFY_10: { 10793 struct scsi_write_verify_10 *cdb; 10794 10795 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10796 10797 *lba = scsi_4btoul(cdb->addr); 10798 *len = scsi_2btoul(cdb->length); 10799 break; 10800 } 10801 case READ_12: 10802 case WRITE_12: { 10803 struct scsi_rw_12 *cdb; 10804 10805 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10806 10807 *lba = scsi_4btoul(cdb->addr); 10808 *len = scsi_4btoul(cdb->length); 10809 break; 10810 } 10811 case WRITE_VERIFY_12: { 10812 struct scsi_write_verify_12 *cdb; 10813 10814 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10815 10816 *lba = scsi_4btoul(cdb->addr); 10817 *len = scsi_4btoul(cdb->length); 10818 break; 10819 } 10820 case READ_16: 10821 case WRITE_16: { 10822 struct scsi_rw_16 *cdb; 10823 10824 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10825 10826 *lba = scsi_8btou64(cdb->addr); 10827 *len = scsi_4btoul(cdb->length); 10828 break; 10829 } 10830 case WRITE_ATOMIC_16: { 10831 struct scsi_write_atomic_16 *cdb; 10832 10833 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10834 10835 *lba = scsi_8btou64(cdb->addr); 10836 *len = scsi_2btoul(cdb->length); 10837 break; 10838 } 10839 case WRITE_VERIFY_16: { 10840 struct scsi_write_verify_16 *cdb; 10841 10842 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10843 10844 *lba = scsi_8btou64(cdb->addr); 10845 *len = scsi_4btoul(cdb->length); 10846 break; 10847 } 10848 case WRITE_SAME_10: { 10849 struct scsi_write_same_10 *cdb; 10850 10851 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10852 10853 *lba = scsi_4btoul(cdb->addr); 10854 *len = scsi_2btoul(cdb->length); 10855 break; 10856 } 10857 case WRITE_SAME_16: { 10858 struct scsi_write_same_16 *cdb; 10859 10860 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10861 10862 *lba = scsi_8btou64(cdb->addr); 10863 *len = scsi_4btoul(cdb->length); 10864 break; 10865 } 10866 case VERIFY_10: { 10867 struct scsi_verify_10 *cdb; 10868 10869 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10870 10871 *lba = scsi_4btoul(cdb->addr); 10872 *len = scsi_2btoul(cdb->length); 10873 break; 10874 } 10875 case VERIFY_12: { 10876 struct scsi_verify_12 *cdb; 10877 10878 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10879 10880 *lba = scsi_4btoul(cdb->addr); 10881 *len = scsi_4btoul(cdb->length); 10882 break; 10883 } 10884 case VERIFY_16: { 10885 struct scsi_verify_16 *cdb; 10886 10887 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10888 10889 *lba = scsi_8btou64(cdb->addr); 10890 *len = scsi_4btoul(cdb->length); 10891 break; 10892 } 10893 case UNMAP: { 10894 *lba = 0; 10895 *len = UINT64_MAX; 10896 break; 10897 } 10898 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10899 struct scsi_get_lba_status *cdb; 10900 10901 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10902 *lba = scsi_8btou64(cdb->addr); 10903 *len = UINT32_MAX; 10904 break; 10905 } 10906 default: 10907 return (1); 10908 break; /* NOTREACHED */ 10909 } 10910 10911 return (0); 10912} 10913 10914static ctl_action 10915ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10916 bool seq) 10917{ 10918 uint64_t endlba1, endlba2; 10919 10920 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10921 endlba2 = lba2 + len2 - 1; 10922 10923 if ((endlba1 < lba2) || (endlba2 < lba1)) 10924 return (CTL_ACTION_PASS); 10925 else 10926 return (CTL_ACTION_BLOCK); 10927} 10928 10929static int 10930ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10931{ 10932 struct ctl_ptr_len_flags *ptrlen; 10933 struct scsi_unmap_desc *buf, *end, *range; 10934 uint64_t lba; 10935 uint32_t len; 10936 10937 /* If not UNMAP -- go other way. */ 10938 if (io->io_hdr.io_type != CTL_IO_SCSI || 10939 io->scsiio.cdb[0] != UNMAP) 10940 return (CTL_ACTION_ERROR); 10941 10942 /* If UNMAP without data -- block and wait for data. */ 10943 ptrlen = (struct ctl_ptr_len_flags *) 10944 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10945 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10946 ptrlen->ptr == NULL) 10947 return (CTL_ACTION_BLOCK); 10948 10949 /* UNMAP with data -- check for collision. */ 10950 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10951 end = buf + ptrlen->len / sizeof(*buf); 10952 for (range = buf; range < end; range++) { 10953 lba = scsi_8btou64(range->lba); 10954 len = scsi_4btoul(range->length); 10955 if ((lba < lba2 + len2) && (lba + len > lba2)) 10956 return (CTL_ACTION_BLOCK); 10957 } 10958 return (CTL_ACTION_PASS); 10959} 10960 10961static ctl_action 10962ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10963{ 10964 uint64_t lba1, lba2; 10965 uint64_t len1, len2; 10966 int retval; 10967 10968 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10969 return (CTL_ACTION_ERROR); 10970 10971 retval = ctl_extent_check_unmap(io1, lba2, len2); 10972 if (retval != CTL_ACTION_ERROR) 10973 return (retval); 10974 10975 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10976 return (CTL_ACTION_ERROR); 10977 10978 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10979 seq = FALSE; 10980 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10981} 10982 10983static ctl_action 10984ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10985{ 10986 uint64_t lba1, lba2; 10987 uint64_t len1, len2; 10988 10989 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10990 return (CTL_ACTION_PASS); 10991 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10992 return (CTL_ACTION_ERROR); 10993 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10994 return (CTL_ACTION_ERROR); 10995 10996 if (lba1 + len1 == lba2) 10997 return (CTL_ACTION_BLOCK); 10998 return (CTL_ACTION_PASS); 10999} 11000 11001static ctl_action 11002ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 11003 union ctl_io *ooa_io) 11004{ 11005 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 11006 const ctl_serialize_action *serialize_row; 11007 11008 /* 11009 * The initiator attempted multiple untagged commands at the same 11010 * time. Can't do that. 11011 */ 11012 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11013 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11014 && ((pending_io->io_hdr.nexus.targ_port == 11015 ooa_io->io_hdr.nexus.targ_port) 11016 && (pending_io->io_hdr.nexus.initid == 11017 ooa_io->io_hdr.nexus.initid)) 11018 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11019 CTL_FLAG_STATUS_SENT)) == 0)) 11020 return (CTL_ACTION_OVERLAP); 11021 11022 /* 11023 * The initiator attempted to send multiple tagged commands with 11024 * the same ID. (It's fine if different initiators have the same 11025 * tag ID.) 11026 * 11027 * Even if all of those conditions are true, we don't kill the I/O 11028 * if the command ahead of us has been aborted. We won't end up 11029 * sending it to the FETD, and it's perfectly legal to resend a 11030 * command with the same tag number as long as the previous 11031 * instance of this tag number has been aborted somehow. 11032 */ 11033 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11034 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 11035 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 11036 && ((pending_io->io_hdr.nexus.targ_port == 11037 ooa_io->io_hdr.nexus.targ_port) 11038 && (pending_io->io_hdr.nexus.initid == 11039 ooa_io->io_hdr.nexus.initid)) 11040 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 11041 CTL_FLAG_STATUS_SENT)) == 0)) 11042 return (CTL_ACTION_OVERLAP_TAG); 11043 11044 /* 11045 * If we get a head of queue tag, SAM-3 says that we should 11046 * immediately execute it. 11047 * 11048 * What happens if this command would normally block for some other 11049 * reason? e.g. a request sense with a head of queue tag 11050 * immediately after a write. Normally that would block, but this 11051 * will result in its getting executed immediately... 11052 * 11053 * We currently return "pass" instead of "skip", so we'll end up 11054 * going through the rest of the queue to check for overlapped tags. 11055 * 11056 * XXX KDM check for other types of blockage first?? 11057 */ 11058 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11059 return (CTL_ACTION_PASS); 11060 11061 /* 11062 * Ordered tags have to block until all items ahead of them 11063 * have completed. If we get called with an ordered tag, we always 11064 * block, if something else is ahead of us in the queue. 11065 */ 11066 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11067 return (CTL_ACTION_BLOCK); 11068 11069 /* 11070 * Simple tags get blocked until all head of queue and ordered tags 11071 * ahead of them have completed. I'm lumping untagged commands in 11072 * with simple tags here. XXX KDM is that the right thing to do? 11073 */ 11074 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11075 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11076 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11077 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11078 return (CTL_ACTION_BLOCK); 11079 11080 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11081 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT, 11082 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p", 11083 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0], 11084 pending_io->scsiio.cdb[1], pending_io)); 11085 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11086 if (ooa_entry->seridx == CTL_SERIDX_INVLD) 11087 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */ 11088 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT, 11089 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p", 11090 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0], 11091 ooa_io->scsiio.cdb[1], ooa_io)); 11092 11093 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11094 11095 switch (serialize_row[pending_entry->seridx]) { 11096 case CTL_SER_BLOCK: 11097 return (CTL_ACTION_BLOCK); 11098 case CTL_SER_EXTENT: 11099 return (ctl_extent_check(ooa_io, pending_io, 11100 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11101 case CTL_SER_EXTENTOPT: 11102 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11103 SCP_QUEUE_ALG_UNRESTRICTED) 11104 return (ctl_extent_check(ooa_io, pending_io, 11105 (lun->be_lun && 11106 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11107 return (CTL_ACTION_PASS); 11108 case CTL_SER_EXTENTSEQ: 11109 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11110 return (ctl_extent_check_seq(ooa_io, pending_io)); 11111 return (CTL_ACTION_PASS); 11112 case CTL_SER_PASS: 11113 return (CTL_ACTION_PASS); 11114 case CTL_SER_BLOCKOPT: 11115 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) != 11116 SCP_QUEUE_ALG_UNRESTRICTED) 11117 return (CTL_ACTION_BLOCK); 11118 return (CTL_ACTION_PASS); 11119 case CTL_SER_SKIP: 11120 return (CTL_ACTION_SKIP); 11121 default: 11122 panic("%s: Invalid serialization value %d for %d => %d", 11123 __func__, serialize_row[pending_entry->seridx], 11124 pending_entry->seridx, ooa_entry->seridx); 11125 } 11126 11127 return (CTL_ACTION_ERROR); 11128} 11129 11130/* 11131 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11132 * Assumptions: 11133 * - pending_io is generally either incoming, or on the blocked queue 11134 * - starting I/O is the I/O we want to start the check with. 11135 */ 11136static ctl_action 11137ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11138 union ctl_io *starting_io) 11139{ 11140 union ctl_io *ooa_io; 11141 ctl_action action; 11142 11143 mtx_assert(&lun->lun_lock, MA_OWNED); 11144 11145 /* 11146 * Run back along the OOA queue, starting with the current 11147 * blocked I/O and going through every I/O before it on the 11148 * queue. If starting_io is NULL, we'll just end up returning 11149 * CTL_ACTION_PASS. 11150 */ 11151 for (ooa_io = starting_io; ooa_io != NULL; 11152 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11153 ooa_links)){ 11154 11155 /* 11156 * This routine just checks to see whether 11157 * cur_blocked is blocked by ooa_io, which is ahead 11158 * of it in the queue. It doesn't queue/dequeue 11159 * cur_blocked. 11160 */ 11161 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11162 switch (action) { 11163 case CTL_ACTION_BLOCK: 11164 case CTL_ACTION_OVERLAP: 11165 case CTL_ACTION_OVERLAP_TAG: 11166 case CTL_ACTION_SKIP: 11167 case CTL_ACTION_ERROR: 11168 return (action); 11169 break; /* NOTREACHED */ 11170 case CTL_ACTION_PASS: 11171 break; 11172 default: 11173 panic("%s: Invalid action %d\n", __func__, action); 11174 } 11175 } 11176 11177 return (CTL_ACTION_PASS); 11178} 11179 11180/* 11181 * Assumptions: 11182 * - An I/O has just completed, and has been removed from the per-LUN OOA 11183 * queue, so some items on the blocked queue may now be unblocked. 11184 */ 11185static int 11186ctl_check_blocked(struct ctl_lun *lun) 11187{ 11188 struct ctl_softc *softc = lun->ctl_softc; 11189 union ctl_io *cur_blocked, *next_blocked; 11190 11191 mtx_assert(&lun->lun_lock, MA_OWNED); 11192 11193 /* 11194 * Run forward from the head of the blocked queue, checking each 11195 * entry against the I/Os prior to it on the OOA queue to see if 11196 * there is still any blockage. 11197 * 11198 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11199 * with our removing a variable on it while it is traversing the 11200 * list. 11201 */ 11202 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11203 cur_blocked != NULL; cur_blocked = next_blocked) { 11204 union ctl_io *prev_ooa; 11205 ctl_action action; 11206 11207 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11208 blocked_links); 11209 11210 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11211 ctl_ooaq, ooa_links); 11212 11213 /* 11214 * If cur_blocked happens to be the first item in the OOA 11215 * queue now, prev_ooa will be NULL, and the action 11216 * returned will just be CTL_ACTION_PASS. 11217 */ 11218 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11219 11220 switch (action) { 11221 case CTL_ACTION_BLOCK: 11222 /* Nothing to do here, still blocked */ 11223 break; 11224 case CTL_ACTION_OVERLAP: 11225 case CTL_ACTION_OVERLAP_TAG: 11226 /* 11227 * This shouldn't happen! In theory we've already 11228 * checked this command for overlap... 11229 */ 11230 break; 11231 case CTL_ACTION_PASS: 11232 case CTL_ACTION_SKIP: { 11233 const struct ctl_cmd_entry *entry; 11234 11235 /* 11236 * The skip case shouldn't happen, this transaction 11237 * should have never made it onto the blocked queue. 11238 */ 11239 /* 11240 * This I/O is no longer blocked, we can remove it 11241 * from the blocked queue. Since this is a TAILQ 11242 * (doubly linked list), we can do O(1) removals 11243 * from any place on the list. 11244 */ 11245 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11246 blocked_links); 11247 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11248 11249 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11250 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11251 /* 11252 * Need to send IO back to original side to 11253 * run 11254 */ 11255 union ctl_ha_msg msg_info; 11256 11257 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11258 msg_info.hdr.original_sc = 11259 cur_blocked->io_hdr.original_sc; 11260 msg_info.hdr.serializing_sc = cur_blocked; 11261 msg_info.hdr.msg_type = CTL_MSG_R2R; 11262 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11263 sizeof(msg_info.hdr), M_NOWAIT); 11264 break; 11265 } 11266 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11267 11268 /* 11269 * Check this I/O for LUN state changes that may 11270 * have happened while this command was blocked. 11271 * The LUN state may have been changed by a command 11272 * ahead of us in the queue, so we need to re-check 11273 * for any states that can be caused by SCSI 11274 * commands. 11275 */ 11276 if (ctl_scsiio_lun_check(lun, entry, 11277 &cur_blocked->scsiio) == 0) { 11278 cur_blocked->io_hdr.flags |= 11279 CTL_FLAG_IS_WAS_ON_RTR; 11280 ctl_enqueue_rtr(cur_blocked); 11281 } else 11282 ctl_done(cur_blocked); 11283 break; 11284 } 11285 default: 11286 /* 11287 * This probably shouldn't happen -- we shouldn't 11288 * get CTL_ACTION_ERROR, or anything else. 11289 */ 11290 break; 11291 } 11292 } 11293 11294 return (CTL_RETVAL_COMPLETE); 11295} 11296 11297/* 11298 * This routine (with one exception) checks LUN flags that can be set by 11299 * commands ahead of us in the OOA queue. These flags have to be checked 11300 * when a command initially comes in, and when we pull a command off the 11301 * blocked queue and are preparing to execute it. The reason we have to 11302 * check these flags for commands on the blocked queue is that the LUN 11303 * state may have been changed by a command ahead of us while we're on the 11304 * blocked queue. 11305 * 11306 * Ordering is somewhat important with these checks, so please pay 11307 * careful attention to the placement of any new checks. 11308 */ 11309static int 11310ctl_scsiio_lun_check(struct ctl_lun *lun, 11311 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11312{ 11313 struct ctl_softc *softc = lun->ctl_softc; 11314 int retval; 11315 uint32_t residx; 11316 11317 retval = 0; 11318 11319 mtx_assert(&lun->lun_lock, MA_OWNED); 11320 11321 /* 11322 * If this shelf is a secondary shelf controller, we may have to 11323 * reject some commands disallowed by HA mode and link state. 11324 */ 11325 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11326 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11327 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11328 ctl_set_lun_unavail(ctsio); 11329 retval = 1; 11330 goto bailout; 11331 } 11332 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11333 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11334 ctl_set_lun_transit(ctsio); 11335 retval = 1; 11336 goto bailout; 11337 } 11338 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11339 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11340 ctl_set_lun_standby(ctsio); 11341 retval = 1; 11342 goto bailout; 11343 } 11344 11345 /* The rest of checks are only done on executing side */ 11346 if (softc->ha_mode == CTL_HA_MODE_XFER) 11347 goto bailout; 11348 } 11349 11350 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11351 if (lun->be_lun && 11352 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11353 ctl_set_hw_write_protected(ctsio); 11354 retval = 1; 11355 goto bailout; 11356 } 11357 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) { 11358 ctl_set_sense(ctsio, /*current_error*/ 1, 11359 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11360 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11361 retval = 1; 11362 goto bailout; 11363 } 11364 } 11365 11366 /* 11367 * Check for a reservation conflict. If this command isn't allowed 11368 * even on reserved LUNs, and if this initiator isn't the one who 11369 * reserved us, reject the command with a reservation conflict. 11370 */ 11371 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11372 if ((lun->flags & CTL_LUN_RESERVED) 11373 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11374 if (lun->res_idx != residx) { 11375 ctl_set_reservation_conflict(ctsio); 11376 retval = 1; 11377 goto bailout; 11378 } 11379 } 11380 11381 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11382 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11383 /* No reservation or command is allowed. */; 11384 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11385 (lun->pr_res_type == SPR_TYPE_WR_EX || 11386 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11387 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11388 /* The command is allowed for Write Exclusive resv. */; 11389 } else { 11390 /* 11391 * if we aren't registered or it's a res holder type 11392 * reservation and this isn't the res holder then set a 11393 * conflict. 11394 */ 11395 if (ctl_get_prkey(lun, residx) == 0 || 11396 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11397 ctl_set_reservation_conflict(ctsio); 11398 retval = 1; 11399 goto bailout; 11400 } 11401 } 11402 11403 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11404 if (lun->flags & CTL_LUN_EJECTED) 11405 ctl_set_lun_ejected(ctsio); 11406 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11407 if (lun->flags & CTL_LUN_REMOVABLE) 11408 ctl_set_lun_no_media(ctsio); 11409 else 11410 ctl_set_lun_int_reqd(ctsio); 11411 } else if (lun->flags & CTL_LUN_STOPPED) 11412 ctl_set_lun_stopped(ctsio); 11413 else 11414 goto bailout; 11415 retval = 1; 11416 goto bailout; 11417 } 11418 11419bailout: 11420 return (retval); 11421} 11422 11423static void 11424ctl_failover_io(union ctl_io *io, int have_lock) 11425{ 11426 ctl_set_busy(&io->scsiio); 11427 ctl_done(io); 11428} 11429 11430static void 11431ctl_failover_lun(union ctl_io *rio) 11432{ 11433 struct ctl_softc *softc = control_softc; 11434 struct ctl_lun *lun; 11435 struct ctl_io_hdr *io, *next_io; 11436 uint32_t targ_lun; 11437 11438 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11439 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11440 11441 /* Find and lock the LUN. */ 11442 mtx_lock(&softc->ctl_lock); 11443 if (targ_lun > CTL_MAX_LUNS || 11444 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11445 mtx_unlock(&softc->ctl_lock); 11446 return; 11447 } 11448 mtx_lock(&lun->lun_lock); 11449 mtx_unlock(&softc->ctl_lock); 11450 if (lun->flags & CTL_LUN_DISABLED) { 11451 mtx_unlock(&lun->lun_lock); 11452 return; 11453 } 11454 11455 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11456 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11457 /* We are master */ 11458 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11459 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11460 io->flags |= CTL_FLAG_ABORT; 11461 io->flags |= CTL_FLAG_FAILOVER; 11462 } else { /* This can be only due to DATAMOVE */ 11463 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11464 io->flags &= ~CTL_FLAG_DMA_INPROG; 11465 io->flags |= CTL_FLAG_IO_ACTIVE; 11466 io->port_status = 31340; 11467 ctl_enqueue_isc((union ctl_io *)io); 11468 } 11469 } 11470 /* We are slave */ 11471 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11472 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11473 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11474 io->flags |= CTL_FLAG_FAILOVER; 11475 } else { 11476 ctl_set_busy(&((union ctl_io *)io)-> 11477 scsiio); 11478 ctl_done((union ctl_io *)io); 11479 } 11480 } 11481 } 11482 } else { /* SERIALIZE modes */ 11483 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11484 next_io) { 11485 /* We are master */ 11486 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11487 TAILQ_REMOVE(&lun->blocked_queue, io, 11488 blocked_links); 11489 io->flags &= ~CTL_FLAG_BLOCKED; 11490 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11491 ctl_free_io((union ctl_io *)io); 11492 } 11493 } 11494 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11495 /* We are master */ 11496 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11497 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11498 ctl_free_io((union ctl_io *)io); 11499 } 11500 /* We are slave */ 11501 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11502 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11503 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11504 ctl_set_busy(&((union ctl_io *)io)-> 11505 scsiio); 11506 ctl_done((union ctl_io *)io); 11507 } 11508 } 11509 } 11510 ctl_check_blocked(lun); 11511 } 11512 mtx_unlock(&lun->lun_lock); 11513} 11514 11515static int 11516ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11517{ 11518 struct ctl_lun *lun; 11519 const struct ctl_cmd_entry *entry; 11520 uint32_t initidx, targ_lun; 11521 int retval = 0; 11522 11523 lun = NULL; 11524 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11525 if (targ_lun < CTL_MAX_LUNS) 11526 lun = softc->ctl_luns[targ_lun]; 11527 if (lun) { 11528 /* 11529 * If the LUN is invalid, pretend that it doesn't exist. 11530 * It will go away as soon as all pending I/O has been 11531 * completed. 11532 */ 11533 mtx_lock(&lun->lun_lock); 11534 if (lun->flags & CTL_LUN_DISABLED) { 11535 mtx_unlock(&lun->lun_lock); 11536 lun = NULL; 11537 } 11538 } 11539 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11540 if (lun) { 11541 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11542 lun->be_lun; 11543 11544 /* 11545 * Every I/O goes into the OOA queue for a particular LUN, 11546 * and stays there until completion. 11547 */ 11548#ifdef CTL_TIME_IO 11549 if (TAILQ_EMPTY(&lun->ooa_queue)) 11550 lun->idle_time += getsbinuptime() - lun->last_busy; 11551#endif 11552 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 11553 } 11554 11555 /* Get command entry and return error if it is unsuppotyed. */ 11556 entry = ctl_validate_command(ctsio); 11557 if (entry == NULL) { 11558 if (lun) 11559 mtx_unlock(&lun->lun_lock); 11560 return (retval); 11561 } 11562 11563 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11564 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11565 11566 /* 11567 * Check to see whether we can send this command to LUNs that don't 11568 * exist. This should pretty much only be the case for inquiry 11569 * and request sense. Further checks, below, really require having 11570 * a LUN, so we can't really check the command anymore. Just put 11571 * it on the rtr queue. 11572 */ 11573 if (lun == NULL) { 11574 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11575 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11576 ctl_enqueue_rtr((union ctl_io *)ctsio); 11577 return (retval); 11578 } 11579 11580 ctl_set_unsupported_lun(ctsio); 11581 ctl_done((union ctl_io *)ctsio); 11582 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11583 return (retval); 11584 } else { 11585 /* 11586 * Make sure we support this particular command on this LUN. 11587 * e.g., we don't support writes to the control LUN. 11588 */ 11589 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11590 mtx_unlock(&lun->lun_lock); 11591 ctl_set_invalid_opcode(ctsio); 11592 ctl_done((union ctl_io *)ctsio); 11593 return (retval); 11594 } 11595 } 11596 11597 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11598 11599#ifdef CTL_WITH_CA 11600 /* 11601 * If we've got a request sense, it'll clear the contingent 11602 * allegiance condition. Otherwise, if we have a CA condition for 11603 * this initiator, clear it, because it sent down a command other 11604 * than request sense. 11605 */ 11606 if ((ctsio->cdb[0] != REQUEST_SENSE) 11607 && (ctl_is_set(lun->have_ca, initidx))) 11608 ctl_clear_mask(lun->have_ca, initidx); 11609#endif 11610 11611 /* 11612 * If the command has this flag set, it handles its own unit 11613 * attention reporting, we shouldn't do anything. Otherwise we 11614 * check for any pending unit attentions, and send them back to the 11615 * initiator. We only do this when a command initially comes in, 11616 * not when we pull it off the blocked queue. 11617 * 11618 * According to SAM-3, section 5.3.2, the order that things get 11619 * presented back to the host is basically unit attentions caused 11620 * by some sort of reset event, busy status, reservation conflicts 11621 * or task set full, and finally any other status. 11622 * 11623 * One issue here is that some of the unit attentions we report 11624 * don't fall into the "reset" category (e.g. "reported luns data 11625 * has changed"). So reporting it here, before the reservation 11626 * check, may be technically wrong. I guess the only thing to do 11627 * would be to check for and report the reset events here, and then 11628 * check for the other unit attention types after we check for a 11629 * reservation conflict. 11630 * 11631 * XXX KDM need to fix this 11632 */ 11633 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11634 ctl_ua_type ua_type; 11635 u_int sense_len = 0; 11636 11637 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11638 &sense_len, SSD_TYPE_NONE); 11639 if (ua_type != CTL_UA_NONE) { 11640 mtx_unlock(&lun->lun_lock); 11641 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11642 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11643 ctsio->sense_len = sense_len; 11644 ctl_done((union ctl_io *)ctsio); 11645 return (retval); 11646 } 11647 } 11648 11649 11650 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11651 mtx_unlock(&lun->lun_lock); 11652 ctl_done((union ctl_io *)ctsio); 11653 return (retval); 11654 } 11655 11656 /* 11657 * XXX CHD this is where we want to send IO to other side if 11658 * this LUN is secondary on this SC. We will need to make a copy 11659 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11660 * the copy we send as FROM_OTHER. 11661 * We also need to stuff the address of the original IO so we can 11662 * find it easily. Something similar will need be done on the other 11663 * side so when we are done we can find the copy. 11664 */ 11665 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11666 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11667 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11668 union ctl_ha_msg msg_info; 11669 int isc_retval; 11670 11671 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11672 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11673 mtx_unlock(&lun->lun_lock); 11674 11675 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11676 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11677 msg_info.hdr.serializing_sc = NULL; 11678 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11679 msg_info.scsi.tag_num = ctsio->tag_num; 11680 msg_info.scsi.tag_type = ctsio->tag_type; 11681 msg_info.scsi.cdb_len = ctsio->cdb_len; 11682 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11683 11684 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11685 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11686 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11687 ctl_set_busy(ctsio); 11688 ctl_done((union ctl_io *)ctsio); 11689 return (retval); 11690 } 11691 return (retval); 11692 } 11693 11694 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11695 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11696 ctl_ooaq, ooa_links))) { 11697 case CTL_ACTION_BLOCK: 11698 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11699 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11700 blocked_links); 11701 mtx_unlock(&lun->lun_lock); 11702 return (retval); 11703 case CTL_ACTION_PASS: 11704 case CTL_ACTION_SKIP: 11705 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11706 mtx_unlock(&lun->lun_lock); 11707 ctl_enqueue_rtr((union ctl_io *)ctsio); 11708 break; 11709 case CTL_ACTION_OVERLAP: 11710 mtx_unlock(&lun->lun_lock); 11711 ctl_set_overlapped_cmd(ctsio); 11712 ctl_done((union ctl_io *)ctsio); 11713 break; 11714 case CTL_ACTION_OVERLAP_TAG: 11715 mtx_unlock(&lun->lun_lock); 11716 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11717 ctl_done((union ctl_io *)ctsio); 11718 break; 11719 case CTL_ACTION_ERROR: 11720 default: 11721 mtx_unlock(&lun->lun_lock); 11722 ctl_set_internal_failure(ctsio, 11723 /*sks_valid*/ 0, 11724 /*retry_count*/ 0); 11725 ctl_done((union ctl_io *)ctsio); 11726 break; 11727 } 11728 return (retval); 11729} 11730 11731const struct ctl_cmd_entry * 11732ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11733{ 11734 const struct ctl_cmd_entry *entry; 11735 int service_action; 11736 11737 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11738 if (sa) 11739 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11740 if (entry->flags & CTL_CMD_FLAG_SA5) { 11741 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11742 entry = &((const struct ctl_cmd_entry *) 11743 entry->execute)[service_action]; 11744 } 11745 return (entry); 11746} 11747 11748const struct ctl_cmd_entry * 11749ctl_validate_command(struct ctl_scsiio *ctsio) 11750{ 11751 const struct ctl_cmd_entry *entry; 11752 int i, sa; 11753 uint8_t diff; 11754 11755 entry = ctl_get_cmd_entry(ctsio, &sa); 11756 if (entry->execute == NULL) { 11757 if (sa) 11758 ctl_set_invalid_field(ctsio, 11759 /*sks_valid*/ 1, 11760 /*command*/ 1, 11761 /*field*/ 1, 11762 /*bit_valid*/ 1, 11763 /*bit*/ 4); 11764 else 11765 ctl_set_invalid_opcode(ctsio); 11766 ctl_done((union ctl_io *)ctsio); 11767 return (NULL); 11768 } 11769 KASSERT(entry->length > 0, 11770 ("Not defined length for command 0x%02x/0x%02x", 11771 ctsio->cdb[0], ctsio->cdb[1])); 11772 for (i = 1; i < entry->length; i++) { 11773 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11774 if (diff == 0) 11775 continue; 11776 ctl_set_invalid_field(ctsio, 11777 /*sks_valid*/ 1, 11778 /*command*/ 1, 11779 /*field*/ i, 11780 /*bit_valid*/ 1, 11781 /*bit*/ fls(diff) - 1); 11782 ctl_done((union ctl_io *)ctsio); 11783 return (NULL); 11784 } 11785 return (entry); 11786} 11787 11788static int 11789ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11790{ 11791 11792 switch (lun_type) { 11793 case T_DIRECT: 11794 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11795 return (0); 11796 break; 11797 case T_PROCESSOR: 11798 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11799 return (0); 11800 break; 11801 case T_CDROM: 11802 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11803 return (0); 11804 break; 11805 default: 11806 return (0); 11807 } 11808 return (1); 11809} 11810 11811static int 11812ctl_scsiio(struct ctl_scsiio *ctsio) 11813{ 11814 int retval; 11815 const struct ctl_cmd_entry *entry; 11816 11817 retval = CTL_RETVAL_COMPLETE; 11818 11819 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11820 11821 entry = ctl_get_cmd_entry(ctsio, NULL); 11822 11823 /* 11824 * If this I/O has been aborted, just send it straight to 11825 * ctl_done() without executing it. 11826 */ 11827 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11828 ctl_done((union ctl_io *)ctsio); 11829 goto bailout; 11830 } 11831 11832 /* 11833 * All the checks should have been handled by ctl_scsiio_precheck(). 11834 * We should be clear now to just execute the I/O. 11835 */ 11836 retval = entry->execute(ctsio); 11837 11838bailout: 11839 return (retval); 11840} 11841 11842/* 11843 * Since we only implement one target right now, a bus reset simply resets 11844 * our single target. 11845 */ 11846static int 11847ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11848{ 11849 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11850} 11851 11852static int 11853ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11854 ctl_ua_type ua_type) 11855{ 11856 struct ctl_port *port; 11857 struct ctl_lun *lun; 11858 int retval; 11859 11860 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11861 union ctl_ha_msg msg_info; 11862 11863 msg_info.hdr.nexus = io->io_hdr.nexus; 11864 if (ua_type==CTL_UA_TARG_RESET) 11865 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11866 else 11867 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11868 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11869 msg_info.hdr.original_sc = NULL; 11870 msg_info.hdr.serializing_sc = NULL; 11871 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11872 sizeof(msg_info.task), M_WAITOK); 11873 } 11874 retval = 0; 11875 11876 mtx_lock(&softc->ctl_lock); 11877 port = ctl_io_port(&io->io_hdr); 11878 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11879 if (port != NULL && 11880 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX) 11881 continue; 11882 retval += ctl_do_lun_reset(lun, io, ua_type); 11883 } 11884 mtx_unlock(&softc->ctl_lock); 11885 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11886 return (retval); 11887} 11888 11889/* 11890 * The LUN should always be set. The I/O is optional, and is used to 11891 * distinguish between I/Os sent by this initiator, and by other 11892 * initiators. We set unit attention for initiators other than this one. 11893 * SAM-3 is vague on this point. It does say that a unit attention should 11894 * be established for other initiators when a LUN is reset (see section 11895 * 5.7.3), but it doesn't specifically say that the unit attention should 11896 * be established for this particular initiator when a LUN is reset. Here 11897 * is the relevant text, from SAM-3 rev 8: 11898 * 11899 * 5.7.2 When a SCSI initiator port aborts its own tasks 11900 * 11901 * When a SCSI initiator port causes its own task(s) to be aborted, no 11902 * notification that the task(s) have been aborted shall be returned to 11903 * the SCSI initiator port other than the completion response for the 11904 * command or task management function action that caused the task(s) to 11905 * be aborted and notification(s) associated with related effects of the 11906 * action (e.g., a reset unit attention condition). 11907 * 11908 * XXX KDM for now, we're setting unit attention for all initiators. 11909 */ 11910static int 11911ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11912{ 11913 union ctl_io *xio; 11914#if 0 11915 uint32_t initidx; 11916#endif 11917 int i; 11918 11919 mtx_lock(&lun->lun_lock); 11920 /* 11921 * Run through the OOA queue and abort each I/O. 11922 */ 11923 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11924 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11925 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11926 } 11927 11928 /* 11929 * This version sets unit attention for every 11930 */ 11931#if 0 11932 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11933 ctl_est_ua_all(lun, initidx, ua_type); 11934#else 11935 ctl_est_ua_all(lun, -1, ua_type); 11936#endif 11937 11938 /* 11939 * A reset (any kind, really) clears reservations established with 11940 * RESERVE/RELEASE. It does not clear reservations established 11941 * with PERSISTENT RESERVE OUT, but we don't support that at the 11942 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11943 * reservations made with the RESERVE/RELEASE commands, because 11944 * those commands are obsolete in SPC-3. 11945 */ 11946 lun->flags &= ~CTL_LUN_RESERVED; 11947 11948#ifdef CTL_WITH_CA 11949 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11950 ctl_clear_mask(lun->have_ca, i); 11951#endif 11952 lun->prevent_count = 0; 11953 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11954 ctl_clear_mask(lun->prevent, i); 11955 mtx_unlock(&lun->lun_lock); 11956 11957 return (0); 11958} 11959 11960static int 11961ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11962{ 11963 struct ctl_lun *lun; 11964 uint32_t targ_lun; 11965 int retval; 11966 11967 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11968 mtx_lock(&softc->ctl_lock); 11969 if (targ_lun >= CTL_MAX_LUNS || 11970 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11971 mtx_unlock(&softc->ctl_lock); 11972 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11973 return (1); 11974 } 11975 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11976 mtx_unlock(&softc->ctl_lock); 11977 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11978 11979 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11980 union ctl_ha_msg msg_info; 11981 11982 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11983 msg_info.hdr.nexus = io->io_hdr.nexus; 11984 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11985 msg_info.hdr.original_sc = NULL; 11986 msg_info.hdr.serializing_sc = NULL; 11987 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11988 sizeof(msg_info.task), M_WAITOK); 11989 } 11990 return (retval); 11991} 11992 11993static void 11994ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11995 int other_sc) 11996{ 11997 union ctl_io *xio; 11998 11999 mtx_assert(&lun->lun_lock, MA_OWNED); 12000 12001 /* 12002 * Run through the OOA queue and attempt to find the given I/O. 12003 * The target port, initiator ID, tag type and tag number have to 12004 * match the values that we got from the initiator. If we have an 12005 * untagged command to abort, simply abort the first untagged command 12006 * we come to. We only allow one untagged command at a time of course. 12007 */ 12008 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12009 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12010 12011 if ((targ_port == UINT32_MAX || 12012 targ_port == xio->io_hdr.nexus.targ_port) && 12013 (init_id == UINT32_MAX || 12014 init_id == xio->io_hdr.nexus.initid)) { 12015 if (targ_port != xio->io_hdr.nexus.targ_port || 12016 init_id != xio->io_hdr.nexus.initid) 12017 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12018 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12019 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12020 union ctl_ha_msg msg_info; 12021 12022 msg_info.hdr.nexus = xio->io_hdr.nexus; 12023 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12024 msg_info.task.tag_num = xio->scsiio.tag_num; 12025 msg_info.task.tag_type = xio->scsiio.tag_type; 12026 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12027 msg_info.hdr.original_sc = NULL; 12028 msg_info.hdr.serializing_sc = NULL; 12029 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12030 sizeof(msg_info.task), M_NOWAIT); 12031 } 12032 } 12033 } 12034} 12035 12036static int 12037ctl_abort_task_set(union ctl_io *io) 12038{ 12039 struct ctl_softc *softc = control_softc; 12040 struct ctl_lun *lun; 12041 uint32_t targ_lun; 12042 12043 /* 12044 * Look up the LUN. 12045 */ 12046 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12047 mtx_lock(&softc->ctl_lock); 12048 if (targ_lun >= CTL_MAX_LUNS || 12049 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12050 mtx_unlock(&softc->ctl_lock); 12051 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12052 return (1); 12053 } 12054 12055 mtx_lock(&lun->lun_lock); 12056 mtx_unlock(&softc->ctl_lock); 12057 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12058 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12059 io->io_hdr.nexus.initid, 12060 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12061 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12062 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12063 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12064 } 12065 mtx_unlock(&lun->lun_lock); 12066 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12067 return (0); 12068} 12069 12070static int 12071ctl_i_t_nexus_reset(union ctl_io *io) 12072{ 12073 struct ctl_softc *softc = control_softc; 12074 struct ctl_lun *lun; 12075 uint32_t initidx; 12076 12077 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12078 union ctl_ha_msg msg_info; 12079 12080 msg_info.hdr.nexus = io->io_hdr.nexus; 12081 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12082 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12083 msg_info.hdr.original_sc = NULL; 12084 msg_info.hdr.serializing_sc = NULL; 12085 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12086 sizeof(msg_info.task), M_WAITOK); 12087 } 12088 12089 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12090 mtx_lock(&softc->ctl_lock); 12091 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12092 mtx_lock(&lun->lun_lock); 12093 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12094 io->io_hdr.nexus.initid, 1); 12095#ifdef CTL_WITH_CA 12096 ctl_clear_mask(lun->have_ca, initidx); 12097#endif 12098 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12099 lun->flags &= ~CTL_LUN_RESERVED; 12100 if (ctl_is_set(lun->prevent, initidx)) { 12101 ctl_clear_mask(lun->prevent, initidx); 12102 lun->prevent_count--; 12103 } 12104 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12105 mtx_unlock(&lun->lun_lock); 12106 } 12107 mtx_unlock(&softc->ctl_lock); 12108 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12109 return (0); 12110} 12111 12112static int 12113ctl_abort_task(union ctl_io *io) 12114{ 12115 union ctl_io *xio; 12116 struct ctl_lun *lun; 12117 struct ctl_softc *softc; 12118#if 0 12119 struct sbuf sb; 12120 char printbuf[128]; 12121#endif 12122 int found; 12123 uint32_t targ_lun; 12124 12125 softc = control_softc; 12126 found = 0; 12127 12128 /* 12129 * Look up the LUN. 12130 */ 12131 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12132 mtx_lock(&softc->ctl_lock); 12133 if (targ_lun >= CTL_MAX_LUNS || 12134 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12135 mtx_unlock(&softc->ctl_lock); 12136 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12137 return (1); 12138 } 12139 12140#if 0 12141 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12142 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12143#endif 12144 12145 mtx_lock(&lun->lun_lock); 12146 mtx_unlock(&softc->ctl_lock); 12147 /* 12148 * Run through the OOA queue and attempt to find the given I/O. 12149 * The target port, initiator ID, tag type and tag number have to 12150 * match the values that we got from the initiator. If we have an 12151 * untagged command to abort, simply abort the first untagged command 12152 * we come to. We only allow one untagged command at a time of course. 12153 */ 12154 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12155 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12156#if 0 12157 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12158 12159 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12160 lun->lun, xio->scsiio.tag_num, 12161 xio->scsiio.tag_type, 12162 (xio->io_hdr.blocked_links.tqe_prev 12163 == NULL) ? "" : " BLOCKED", 12164 (xio->io_hdr.flags & 12165 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12166 (xio->io_hdr.flags & 12167 CTL_FLAG_ABORT) ? " ABORT" : "", 12168 (xio->io_hdr.flags & 12169 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12170 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12171 sbuf_finish(&sb); 12172 printf("%s\n", sbuf_data(&sb)); 12173#endif 12174 12175 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12176 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12177 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12178 continue; 12179 12180 /* 12181 * If the abort says that the task is untagged, the 12182 * task in the queue must be untagged. Otherwise, 12183 * we just check to see whether the tag numbers 12184 * match. This is because the QLogic firmware 12185 * doesn't pass back the tag type in an abort 12186 * request. 12187 */ 12188#if 0 12189 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12190 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12191 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12192#endif 12193 /* 12194 * XXX KDM we've got problems with FC, because it 12195 * doesn't send down a tag type with aborts. So we 12196 * can only really go by the tag number... 12197 * This may cause problems with parallel SCSI. 12198 * Need to figure that out!! 12199 */ 12200 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12201 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12202 found = 1; 12203 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12204 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12205 union ctl_ha_msg msg_info; 12206 12207 msg_info.hdr.nexus = io->io_hdr.nexus; 12208 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12209 msg_info.task.tag_num = io->taskio.tag_num; 12210 msg_info.task.tag_type = io->taskio.tag_type; 12211 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12212 msg_info.hdr.original_sc = NULL; 12213 msg_info.hdr.serializing_sc = NULL; 12214#if 0 12215 printf("Sent Abort to other side\n"); 12216#endif 12217 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12218 sizeof(msg_info.task), M_NOWAIT); 12219 } 12220#if 0 12221 printf("ctl_abort_task: found I/O to abort\n"); 12222#endif 12223 } 12224 } 12225 mtx_unlock(&lun->lun_lock); 12226 12227 if (found == 0) { 12228 /* 12229 * This isn't really an error. It's entirely possible for 12230 * the abort and command completion to cross on the wire. 12231 * This is more of an informative/diagnostic error. 12232 */ 12233#if 0 12234 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12235 "%u:%u:%u tag %d type %d\n", 12236 io->io_hdr.nexus.initid, 12237 io->io_hdr.nexus.targ_port, 12238 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12239 io->taskio.tag_type); 12240#endif 12241 } 12242 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12243 return (0); 12244} 12245 12246static int 12247ctl_query_task(union ctl_io *io, int task_set) 12248{ 12249 union ctl_io *xio; 12250 struct ctl_lun *lun; 12251 struct ctl_softc *softc; 12252 int found = 0; 12253 uint32_t targ_lun; 12254 12255 softc = control_softc; 12256 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12257 mtx_lock(&softc->ctl_lock); 12258 if (targ_lun >= CTL_MAX_LUNS || 12259 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12260 mtx_unlock(&softc->ctl_lock); 12261 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12262 return (1); 12263 } 12264 mtx_lock(&lun->lun_lock); 12265 mtx_unlock(&softc->ctl_lock); 12266 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12267 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12268 12269 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12270 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12271 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12272 continue; 12273 12274 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12275 found = 1; 12276 break; 12277 } 12278 } 12279 mtx_unlock(&lun->lun_lock); 12280 if (found) 12281 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12282 else 12283 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12284 return (0); 12285} 12286 12287static int 12288ctl_query_async_event(union ctl_io *io) 12289{ 12290 struct ctl_lun *lun; 12291 struct ctl_softc *softc; 12292 ctl_ua_type ua; 12293 uint32_t targ_lun, initidx; 12294 12295 softc = control_softc; 12296 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12297 mtx_lock(&softc->ctl_lock); 12298 if (targ_lun >= CTL_MAX_LUNS || 12299 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12300 mtx_unlock(&softc->ctl_lock); 12301 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12302 return (1); 12303 } 12304 mtx_lock(&lun->lun_lock); 12305 mtx_unlock(&softc->ctl_lock); 12306 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12307 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12308 mtx_unlock(&lun->lun_lock); 12309 if (ua != CTL_UA_NONE) 12310 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12311 else 12312 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12313 return (0); 12314} 12315 12316static void 12317ctl_run_task(union ctl_io *io) 12318{ 12319 struct ctl_softc *softc = control_softc; 12320 int retval = 1; 12321 12322 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12323 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12324 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12325 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12326 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12327 switch (io->taskio.task_action) { 12328 case CTL_TASK_ABORT_TASK: 12329 retval = ctl_abort_task(io); 12330 break; 12331 case CTL_TASK_ABORT_TASK_SET: 12332 case CTL_TASK_CLEAR_TASK_SET: 12333 retval = ctl_abort_task_set(io); 12334 break; 12335 case CTL_TASK_CLEAR_ACA: 12336 break; 12337 case CTL_TASK_I_T_NEXUS_RESET: 12338 retval = ctl_i_t_nexus_reset(io); 12339 break; 12340 case CTL_TASK_LUN_RESET: 12341 retval = ctl_lun_reset(softc, io); 12342 break; 12343 case CTL_TASK_TARGET_RESET: 12344 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12345 break; 12346 case CTL_TASK_BUS_RESET: 12347 retval = ctl_bus_reset(softc, io); 12348 break; 12349 case CTL_TASK_PORT_LOGIN: 12350 break; 12351 case CTL_TASK_PORT_LOGOUT: 12352 break; 12353 case CTL_TASK_QUERY_TASK: 12354 retval = ctl_query_task(io, 0); 12355 break; 12356 case CTL_TASK_QUERY_TASK_SET: 12357 retval = ctl_query_task(io, 1); 12358 break; 12359 case CTL_TASK_QUERY_ASYNC_EVENT: 12360 retval = ctl_query_async_event(io); 12361 break; 12362 default: 12363 printf("%s: got unknown task management event %d\n", 12364 __func__, io->taskio.task_action); 12365 break; 12366 } 12367 if (retval == 0) 12368 io->io_hdr.status = CTL_SUCCESS; 12369 else 12370 io->io_hdr.status = CTL_ERROR; 12371 ctl_done(io); 12372} 12373 12374/* 12375 * For HA operation. Handle commands that come in from the other 12376 * controller. 12377 */ 12378static void 12379ctl_handle_isc(union ctl_io *io) 12380{ 12381 struct ctl_softc *softc = control_softc; 12382 struct ctl_lun *lun; 12383 const struct ctl_cmd_entry *entry; 12384 uint32_t targ_lun; 12385 12386 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12387 switch (io->io_hdr.msg_type) { 12388 case CTL_MSG_SERIALIZE: 12389 ctl_serialize_other_sc_cmd(&io->scsiio); 12390 break; 12391 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */ 12392 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12393 if (targ_lun >= CTL_MAX_LUNS || 12394 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12395 ctl_done(io); 12396 break; 12397 } 12398 mtx_lock(&lun->lun_lock); 12399 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) { 12400 mtx_unlock(&lun->lun_lock); 12401 ctl_done(io); 12402 break; 12403 } 12404 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12405 mtx_unlock(&lun->lun_lock); 12406 ctl_enqueue_rtr(io); 12407 break; 12408 case CTL_MSG_FINISH_IO: 12409 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12410 ctl_done(io); 12411 break; 12412 } 12413 if (targ_lun >= CTL_MAX_LUNS || 12414 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12415 ctl_free_io(io); 12416 break; 12417 } 12418 mtx_lock(&lun->lun_lock); 12419 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12420 ctl_check_blocked(lun); 12421 mtx_unlock(&lun->lun_lock); 12422 ctl_free_io(io); 12423 break; 12424 case CTL_MSG_PERS_ACTION: 12425 ctl_hndl_per_res_out_on_other_sc( 12426 (union ctl_ha_msg *)&io->presio.pr_msg); 12427 ctl_free_io(io); 12428 break; 12429 case CTL_MSG_BAD_JUJU: 12430 ctl_done(io); 12431 break; 12432 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */ 12433 ctl_datamove_remote(io); 12434 break; 12435 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */ 12436 io->scsiio.be_move_done(io); 12437 break; 12438 case CTL_MSG_FAILOVER: 12439 ctl_failover_lun(io); 12440 ctl_free_io(io); 12441 break; 12442 default: 12443 printf("%s: Invalid message type %d\n", 12444 __func__, io->io_hdr.msg_type); 12445 ctl_free_io(io); 12446 break; 12447 } 12448 12449} 12450 12451 12452/* 12453 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12454 * there is no match. 12455 */ 12456static ctl_lun_error_pattern 12457ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12458{ 12459 const struct ctl_cmd_entry *entry; 12460 ctl_lun_error_pattern filtered_pattern, pattern; 12461 12462 pattern = desc->error_pattern; 12463 12464 /* 12465 * XXX KDM we need more data passed into this function to match a 12466 * custom pattern, and we actually need to implement custom pattern 12467 * matching. 12468 */ 12469 if (pattern & CTL_LUN_PAT_CMD) 12470 return (CTL_LUN_PAT_CMD); 12471 12472 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12473 return (CTL_LUN_PAT_ANY); 12474 12475 entry = ctl_get_cmd_entry(ctsio, NULL); 12476 12477 filtered_pattern = entry->pattern & pattern; 12478 12479 /* 12480 * If the user requested specific flags in the pattern (e.g. 12481 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12482 * flags. 12483 * 12484 * If the user did not specify any flags, it doesn't matter whether 12485 * or not the command supports the flags. 12486 */ 12487 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12488 (pattern & ~CTL_LUN_PAT_MASK)) 12489 return (CTL_LUN_PAT_NONE); 12490 12491 /* 12492 * If the user asked for a range check, see if the requested LBA 12493 * range overlaps with this command's LBA range. 12494 */ 12495 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12496 uint64_t lba1; 12497 uint64_t len1; 12498 ctl_action action; 12499 int retval; 12500 12501 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12502 if (retval != 0) 12503 return (CTL_LUN_PAT_NONE); 12504 12505 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12506 desc->lba_range.len, FALSE); 12507 /* 12508 * A "pass" means that the LBA ranges don't overlap, so 12509 * this doesn't match the user's range criteria. 12510 */ 12511 if (action == CTL_ACTION_PASS) 12512 return (CTL_LUN_PAT_NONE); 12513 } 12514 12515 return (filtered_pattern); 12516} 12517 12518static void 12519ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12520{ 12521 struct ctl_error_desc *desc, *desc2; 12522 12523 mtx_assert(&lun->lun_lock, MA_OWNED); 12524 12525 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12526 ctl_lun_error_pattern pattern; 12527 /* 12528 * Check to see whether this particular command matches 12529 * the pattern in the descriptor. 12530 */ 12531 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12532 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12533 continue; 12534 12535 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12536 case CTL_LUN_INJ_ABORTED: 12537 ctl_set_aborted(&io->scsiio); 12538 break; 12539 case CTL_LUN_INJ_MEDIUM_ERR: 12540 ctl_set_medium_error(&io->scsiio, 12541 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12542 CTL_FLAG_DATA_OUT); 12543 break; 12544 case CTL_LUN_INJ_UA: 12545 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12546 * OCCURRED */ 12547 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12548 break; 12549 case CTL_LUN_INJ_CUSTOM: 12550 /* 12551 * We're assuming the user knows what he is doing. 12552 * Just copy the sense information without doing 12553 * checks. 12554 */ 12555 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12556 MIN(sizeof(desc->custom_sense), 12557 sizeof(io->scsiio.sense_data))); 12558 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12559 io->scsiio.sense_len = SSD_FULL_SIZE; 12560 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12561 break; 12562 case CTL_LUN_INJ_NONE: 12563 default: 12564 /* 12565 * If this is an error injection type we don't know 12566 * about, clear the continuous flag (if it is set) 12567 * so it will get deleted below. 12568 */ 12569 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12570 break; 12571 } 12572 /* 12573 * By default, each error injection action is a one-shot 12574 */ 12575 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12576 continue; 12577 12578 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12579 12580 free(desc, M_CTL); 12581 } 12582} 12583 12584#ifdef CTL_IO_DELAY 12585static void 12586ctl_datamove_timer_wakeup(void *arg) 12587{ 12588 union ctl_io *io; 12589 12590 io = (union ctl_io *)arg; 12591 12592 ctl_datamove(io); 12593} 12594#endif /* CTL_IO_DELAY */ 12595 12596void 12597ctl_datamove(union ctl_io *io) 12598{ 12599 struct ctl_lun *lun; 12600 void (*fe_datamove)(union ctl_io *io); 12601 12602 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12603 12604 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12605 12606 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12607#ifdef CTL_TIME_IO 12608 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12609 char str[256]; 12610 char path_str[64]; 12611 struct sbuf sb; 12612 12613 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12614 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12615 12616 sbuf_cat(&sb, path_str); 12617 switch (io->io_hdr.io_type) { 12618 case CTL_IO_SCSI: 12619 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12620 sbuf_printf(&sb, "\n"); 12621 sbuf_cat(&sb, path_str); 12622 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12623 io->scsiio.tag_num, io->scsiio.tag_type); 12624 break; 12625 case CTL_IO_TASK: 12626 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12627 "Tag Type: %d\n", io->taskio.task_action, 12628 io->taskio.tag_num, io->taskio.tag_type); 12629 break; 12630 default: 12631 panic("%s: Invalid CTL I/O type %d\n", 12632 __func__, io->io_hdr.io_type); 12633 } 12634 sbuf_cat(&sb, path_str); 12635 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12636 (intmax_t)time_uptime - io->io_hdr.start_time); 12637 sbuf_finish(&sb); 12638 printf("%s", sbuf_data(&sb)); 12639 } 12640#endif /* CTL_TIME_IO */ 12641 12642#ifdef CTL_IO_DELAY 12643 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12644 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12645 } else { 12646 if ((lun != NULL) 12647 && (lun->delay_info.datamove_delay > 0)) { 12648 12649 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12650 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12651 callout_reset(&io->io_hdr.delay_callout, 12652 lun->delay_info.datamove_delay * hz, 12653 ctl_datamove_timer_wakeup, io); 12654 if (lun->delay_info.datamove_type == 12655 CTL_DELAY_TYPE_ONESHOT) 12656 lun->delay_info.datamove_delay = 0; 12657 return; 12658 } 12659 } 12660#endif 12661 12662 /* 12663 * This command has been aborted. Set the port status, so we fail 12664 * the data move. 12665 */ 12666 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12667 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12668 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12669 io->io_hdr.nexus.targ_port, 12670 io->io_hdr.nexus.targ_lun); 12671 io->io_hdr.port_status = 31337; 12672 /* 12673 * Note that the backend, in this case, will get the 12674 * callback in its context. In other cases it may get 12675 * called in the frontend's interrupt thread context. 12676 */ 12677 io->scsiio.be_move_done(io); 12678 return; 12679 } 12680 12681 /* Don't confuse frontend with zero length data move. */ 12682 if (io->scsiio.kern_data_len == 0) { 12683 io->scsiio.be_move_done(io); 12684 return; 12685 } 12686 12687 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12688 fe_datamove(io); 12689} 12690 12691static void 12692ctl_send_datamove_done(union ctl_io *io, int have_lock) 12693{ 12694 union ctl_ha_msg msg; 12695#ifdef CTL_TIME_IO 12696 struct bintime cur_bt; 12697#endif 12698 12699 memset(&msg, 0, sizeof(msg)); 12700 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12701 msg.hdr.original_sc = io; 12702 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12703 msg.hdr.nexus = io->io_hdr.nexus; 12704 msg.hdr.status = io->io_hdr.status; 12705 msg.scsi.tag_num = io->scsiio.tag_num; 12706 msg.scsi.tag_type = io->scsiio.tag_type; 12707 msg.scsi.scsi_status = io->scsiio.scsi_status; 12708 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12709 io->scsiio.sense_len); 12710 msg.scsi.sense_len = io->scsiio.sense_len; 12711 msg.scsi.sense_residual = io->scsiio.sense_residual; 12712 msg.scsi.fetd_status = io->io_hdr.port_status; 12713 msg.scsi.residual = io->scsiio.residual; 12714 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12715 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12716 ctl_failover_io(io, /*have_lock*/ have_lock); 12717 return; 12718 } 12719 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12720 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12721 msg.scsi.sense_len, M_WAITOK); 12722 12723#ifdef CTL_TIME_IO 12724 getbinuptime(&cur_bt); 12725 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12726 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12727#endif 12728 io->io_hdr.num_dmas++; 12729} 12730 12731/* 12732 * The DMA to the remote side is done, now we need to tell the other side 12733 * we're done so it can continue with its data movement. 12734 */ 12735static void 12736ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12737{ 12738 union ctl_io *io; 12739 uint32_t i; 12740 12741 io = rq->context; 12742 12743 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12744 printf("%s: ISC DMA write failed with error %d", __func__, 12745 rq->ret); 12746 ctl_set_internal_failure(&io->scsiio, 12747 /*sks_valid*/ 1, 12748 /*retry_count*/ rq->ret); 12749 } 12750 12751 ctl_dt_req_free(rq); 12752 12753 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12754 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12755 free(io->io_hdr.remote_sglist, M_CTL); 12756 io->io_hdr.remote_sglist = NULL; 12757 io->io_hdr.local_sglist = NULL; 12758 12759 /* 12760 * The data is in local and remote memory, so now we need to send 12761 * status (good or back) back to the other side. 12762 */ 12763 ctl_send_datamove_done(io, /*have_lock*/ 0); 12764} 12765 12766/* 12767 * We've moved the data from the host/controller into local memory. Now we 12768 * need to push it over to the remote controller's memory. 12769 */ 12770static int 12771ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12772{ 12773 int retval; 12774 12775 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12776 ctl_datamove_remote_write_cb); 12777 return (retval); 12778} 12779 12780static void 12781ctl_datamove_remote_write(union ctl_io *io) 12782{ 12783 int retval; 12784 void (*fe_datamove)(union ctl_io *io); 12785 12786 /* 12787 * - Get the data from the host/HBA into local memory. 12788 * - DMA memory from the local controller to the remote controller. 12789 * - Send status back to the remote controller. 12790 */ 12791 12792 retval = ctl_datamove_remote_sgl_setup(io); 12793 if (retval != 0) 12794 return; 12795 12796 /* Switch the pointer over so the FETD knows what to do */ 12797 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12798 12799 /* 12800 * Use a custom move done callback, since we need to send completion 12801 * back to the other controller, not to the backend on this side. 12802 */ 12803 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12804 12805 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12806 fe_datamove(io); 12807} 12808 12809static int 12810ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12811{ 12812#if 0 12813 char str[256]; 12814 char path_str[64]; 12815 struct sbuf sb; 12816#endif 12817 uint32_t i; 12818 12819 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12820 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12821 free(io->io_hdr.remote_sglist, M_CTL); 12822 io->io_hdr.remote_sglist = NULL; 12823 io->io_hdr.local_sglist = NULL; 12824 12825#if 0 12826 scsi_path_string(io, path_str, sizeof(path_str)); 12827 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12828 sbuf_cat(&sb, path_str); 12829 scsi_command_string(&io->scsiio, NULL, &sb); 12830 sbuf_printf(&sb, "\n"); 12831 sbuf_cat(&sb, path_str); 12832 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12833 io->scsiio.tag_num, io->scsiio.tag_type); 12834 sbuf_cat(&sb, path_str); 12835 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12836 io->io_hdr.flags, io->io_hdr.status); 12837 sbuf_finish(&sb); 12838 printk("%s", sbuf_data(&sb)); 12839#endif 12840 12841 12842 /* 12843 * The read is done, now we need to send status (good or bad) back 12844 * to the other side. 12845 */ 12846 ctl_send_datamove_done(io, /*have_lock*/ 0); 12847 12848 return (0); 12849} 12850 12851static void 12852ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12853{ 12854 union ctl_io *io; 12855 void (*fe_datamove)(union ctl_io *io); 12856 12857 io = rq->context; 12858 12859 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12860 printf("%s: ISC DMA read failed with error %d\n", __func__, 12861 rq->ret); 12862 ctl_set_internal_failure(&io->scsiio, 12863 /*sks_valid*/ 1, 12864 /*retry_count*/ rq->ret); 12865 } 12866 12867 ctl_dt_req_free(rq); 12868 12869 /* Switch the pointer over so the FETD knows what to do */ 12870 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12871 12872 /* 12873 * Use a custom move done callback, since we need to send completion 12874 * back to the other controller, not to the backend on this side. 12875 */ 12876 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12877 12878 /* XXX KDM add checks like the ones in ctl_datamove? */ 12879 12880 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12881 fe_datamove(io); 12882} 12883 12884static int 12885ctl_datamove_remote_sgl_setup(union ctl_io *io) 12886{ 12887 struct ctl_sg_entry *local_sglist; 12888 uint32_t len_to_go; 12889 int retval; 12890 int i; 12891 12892 retval = 0; 12893 local_sglist = io->io_hdr.local_sglist; 12894 len_to_go = io->scsiio.kern_data_len; 12895 12896 /* 12897 * The difficult thing here is that the size of the various 12898 * S/G segments may be different than the size from the 12899 * remote controller. That'll make it harder when DMAing 12900 * the data back to the other side. 12901 */ 12902 for (i = 0; len_to_go > 0; i++) { 12903 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12904 local_sglist[i].addr = 12905 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12906 12907 len_to_go -= local_sglist[i].len; 12908 } 12909 /* 12910 * Reset the number of S/G entries accordingly. The original 12911 * number of S/G entries is available in rem_sg_entries. 12912 */ 12913 io->scsiio.kern_sg_entries = i; 12914 12915#if 0 12916 printf("%s: kern_sg_entries = %d\n", __func__, 12917 io->scsiio.kern_sg_entries); 12918 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12919 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12920 local_sglist[i].addr, local_sglist[i].len); 12921#endif 12922 12923 return (retval); 12924} 12925 12926static int 12927ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12928 ctl_ha_dt_cb callback) 12929{ 12930 struct ctl_ha_dt_req *rq; 12931 struct ctl_sg_entry *remote_sglist, *local_sglist; 12932 uint32_t local_used, remote_used, total_used; 12933 int i, j, isc_ret; 12934 12935 rq = ctl_dt_req_alloc(); 12936 12937 /* 12938 * If we failed to allocate the request, and if the DMA didn't fail 12939 * anyway, set busy status. This is just a resource allocation 12940 * failure. 12941 */ 12942 if ((rq == NULL) 12943 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12944 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12945 ctl_set_busy(&io->scsiio); 12946 12947 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12948 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12949 12950 if (rq != NULL) 12951 ctl_dt_req_free(rq); 12952 12953 /* 12954 * The data move failed. We need to return status back 12955 * to the other controller. No point in trying to DMA 12956 * data to the remote controller. 12957 */ 12958 12959 ctl_send_datamove_done(io, /*have_lock*/ 0); 12960 12961 return (1); 12962 } 12963 12964 local_sglist = io->io_hdr.local_sglist; 12965 remote_sglist = io->io_hdr.remote_sglist; 12966 local_used = 0; 12967 remote_used = 0; 12968 total_used = 0; 12969 12970 /* 12971 * Pull/push the data over the wire from/to the other controller. 12972 * This takes into account the possibility that the local and 12973 * remote sglists may not be identical in terms of the size of 12974 * the elements and the number of elements. 12975 * 12976 * One fundamental assumption here is that the length allocated for 12977 * both the local and remote sglists is identical. Otherwise, we've 12978 * essentially got a coding error of some sort. 12979 */ 12980 isc_ret = CTL_HA_STATUS_SUCCESS; 12981 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12982 uint32_t cur_len; 12983 uint8_t *tmp_ptr; 12984 12985 rq->command = command; 12986 rq->context = io; 12987 12988 /* 12989 * Both pointers should be aligned. But it is possible 12990 * that the allocation length is not. They should both 12991 * also have enough slack left over at the end, though, 12992 * to round up to the next 8 byte boundary. 12993 */ 12994 cur_len = MIN(local_sglist[i].len - local_used, 12995 remote_sglist[j].len - remote_used); 12996 rq->size = cur_len; 12997 12998 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12999 tmp_ptr += local_used; 13000 13001#if 0 13002 /* Use physical addresses when talking to ISC hardware */ 13003 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13004 /* XXX KDM use busdma */ 13005 rq->local = vtophys(tmp_ptr); 13006 } else 13007 rq->local = tmp_ptr; 13008#else 13009 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 13010 ("HA does not support BUS_ADDR")); 13011 rq->local = tmp_ptr; 13012#endif 13013 13014 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13015 tmp_ptr += remote_used; 13016 rq->remote = tmp_ptr; 13017 13018 rq->callback = NULL; 13019 13020 local_used += cur_len; 13021 if (local_used >= local_sglist[i].len) { 13022 i++; 13023 local_used = 0; 13024 } 13025 13026 remote_used += cur_len; 13027 if (remote_used >= remote_sglist[j].len) { 13028 j++; 13029 remote_used = 0; 13030 } 13031 total_used += cur_len; 13032 13033 if (total_used >= io->scsiio.kern_data_len) 13034 rq->callback = callback; 13035 13036#if 0 13037 printf("%s: %s: local %p remote %p size %d\n", __func__, 13038 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13039 rq->local, rq->remote, rq->size); 13040#endif 13041 13042 isc_ret = ctl_dt_single(rq); 13043 if (isc_ret > CTL_HA_STATUS_SUCCESS) 13044 break; 13045 } 13046 if (isc_ret != CTL_HA_STATUS_WAIT) { 13047 rq->ret = isc_ret; 13048 callback(rq); 13049 } 13050 13051 return (0); 13052} 13053 13054static void 13055ctl_datamove_remote_read(union ctl_io *io) 13056{ 13057 int retval; 13058 uint32_t i; 13059 13060 /* 13061 * This will send an error to the other controller in the case of a 13062 * failure. 13063 */ 13064 retval = ctl_datamove_remote_sgl_setup(io); 13065 if (retval != 0) 13066 return; 13067 13068 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13069 ctl_datamove_remote_read_cb); 13070 if (retval != 0) { 13071 /* 13072 * Make sure we free memory if there was an error.. The 13073 * ctl_datamove_remote_xfer() function will send the 13074 * datamove done message, or call the callback with an 13075 * error if there is a problem. 13076 */ 13077 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13078 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13079 free(io->io_hdr.remote_sglist, M_CTL); 13080 io->io_hdr.remote_sglist = NULL; 13081 io->io_hdr.local_sglist = NULL; 13082 } 13083} 13084 13085/* 13086 * Process a datamove request from the other controller. This is used for 13087 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13088 * first. Once that is complete, the data gets DMAed into the remote 13089 * controller's memory. For reads, we DMA from the remote controller's 13090 * memory into our memory first, and then move it out to the FETD. 13091 */ 13092static void 13093ctl_datamove_remote(union ctl_io *io) 13094{ 13095 13096 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13097 13098 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13099 ctl_failover_io(io, /*have_lock*/ 0); 13100 return; 13101 } 13102 13103 /* 13104 * Note that we look for an aborted I/O here, but don't do some of 13105 * the other checks that ctl_datamove() normally does. 13106 * We don't need to run the datamove delay code, since that should 13107 * have been done if need be on the other controller. 13108 */ 13109 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13110 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13111 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13112 io->io_hdr.nexus.targ_port, 13113 io->io_hdr.nexus.targ_lun); 13114 io->io_hdr.port_status = 31338; 13115 ctl_send_datamove_done(io, /*have_lock*/ 0); 13116 return; 13117 } 13118 13119 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13120 ctl_datamove_remote_write(io); 13121 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13122 ctl_datamove_remote_read(io); 13123 else { 13124 io->io_hdr.port_status = 31339; 13125 ctl_send_datamove_done(io, /*have_lock*/ 0); 13126 } 13127} 13128 13129static void 13130ctl_process_done(union ctl_io *io) 13131{ 13132 struct ctl_lun *lun; 13133 struct ctl_softc *softc = control_softc; 13134 void (*fe_done)(union ctl_io *io); 13135 union ctl_ha_msg msg; 13136 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13137 13138 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13139 fe_done = softc->ctl_ports[targ_port]->fe_done; 13140 13141#ifdef CTL_TIME_IO 13142 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13143 char str[256]; 13144 char path_str[64]; 13145 struct sbuf sb; 13146 13147 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13148 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13149 13150 sbuf_cat(&sb, path_str); 13151 switch (io->io_hdr.io_type) { 13152 case CTL_IO_SCSI: 13153 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13154 sbuf_printf(&sb, "\n"); 13155 sbuf_cat(&sb, path_str); 13156 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13157 io->scsiio.tag_num, io->scsiio.tag_type); 13158 break; 13159 case CTL_IO_TASK: 13160 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13161 "Tag Type: %d\n", io->taskio.task_action, 13162 io->taskio.tag_num, io->taskio.tag_type); 13163 break; 13164 default: 13165 panic("%s: Invalid CTL I/O type %d\n", 13166 __func__, io->io_hdr.io_type); 13167 } 13168 sbuf_cat(&sb, path_str); 13169 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13170 (intmax_t)time_uptime - io->io_hdr.start_time); 13171 sbuf_finish(&sb); 13172 printf("%s", sbuf_data(&sb)); 13173 } 13174#endif /* CTL_TIME_IO */ 13175 13176 switch (io->io_hdr.io_type) { 13177 case CTL_IO_SCSI: 13178 break; 13179 case CTL_IO_TASK: 13180 if (ctl_debug & CTL_DEBUG_INFO) 13181 ctl_io_error_print(io, NULL); 13182 fe_done(io); 13183 return; 13184 default: 13185 panic("%s: Invalid CTL I/O type %d\n", 13186 __func__, io->io_hdr.io_type); 13187 } 13188 13189 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13190 if (lun == NULL) { 13191 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13192 io->io_hdr.nexus.targ_mapped_lun)); 13193 goto bailout; 13194 } 13195 13196 mtx_lock(&lun->lun_lock); 13197 13198 /* 13199 * Check to see if we have any informational exception and status 13200 * of this command can be modified to report it in form of either 13201 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field. 13202 */ 13203 if (lun->ie_reported == 0 && lun->ie_asc != 0 && 13204 io->io_hdr.status == CTL_SUCCESS && 13205 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) { 13206 uint8_t mrie = lun->MODE_IE.mrie; 13207 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) || 13208 (lun->MODE_VER.byte3 & SMS_VER_PER)); 13209 if (((mrie == SIEP_MRIE_REC_COND && per) || 13210 mrie == SIEP_MRIE_REC_UNCOND || 13211 mrie == SIEP_MRIE_NO_SENSE) && 13212 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags & 13213 CTL_CMD_FLAG_NO_SENSE) == 0) { 13214 ctl_set_sense(&io->scsiio, 13215 /*current_error*/ 1, 13216 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ? 13217 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR, 13218 /*asc*/ lun->ie_asc, 13219 /*ascq*/ lun->ie_ascq, 13220 SSD_ELEM_NONE); 13221 lun->ie_reported = 1; 13222 } 13223 } else if (lun->ie_reported < 0) 13224 lun->ie_reported = 0; 13225 13226 /* 13227 * Check to see if we have any errors to inject here. We only 13228 * inject errors for commands that don't already have errors set. 13229 */ 13230 if (!STAILQ_EMPTY(&lun->error_list) && 13231 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13232 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13233 ctl_inject_error(lun, io); 13234 13235 /* 13236 * XXX KDM how do we treat commands that aren't completed 13237 * successfully? 13238 * 13239 * XXX KDM should we also track I/O latency? 13240 */ 13241 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13242 io->io_hdr.io_type == CTL_IO_SCSI) { 13243#ifdef CTL_TIME_IO 13244 struct bintime cur_bt; 13245#endif 13246 int type; 13247 13248 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13249 CTL_FLAG_DATA_IN) 13250 type = CTL_STATS_READ; 13251 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13252 CTL_FLAG_DATA_OUT) 13253 type = CTL_STATS_WRITE; 13254 else 13255 type = CTL_STATS_NO_IO; 13256 13257 lun->stats.ports[targ_port].bytes[type] += 13258 io->scsiio.kern_total_len; 13259 lun->stats.ports[targ_port].operations[type]++; 13260#ifdef CTL_TIME_IO 13261 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13262 &io->io_hdr.dma_bt); 13263 getbinuptime(&cur_bt); 13264 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13265 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13266#endif 13267 lun->stats.ports[targ_port].num_dmas[type] += 13268 io->io_hdr.num_dmas; 13269 } 13270 13271 /* 13272 * Remove this from the OOA queue. 13273 */ 13274 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13275#ifdef CTL_TIME_IO 13276 if (TAILQ_EMPTY(&lun->ooa_queue)) 13277 lun->last_busy = getsbinuptime(); 13278#endif 13279 13280 /* 13281 * Run through the blocked queue on this LUN and see if anything 13282 * has become unblocked, now that this transaction is done. 13283 */ 13284 ctl_check_blocked(lun); 13285 13286 /* 13287 * If the LUN has been invalidated, free it if there is nothing 13288 * left on its OOA queue. 13289 */ 13290 if ((lun->flags & CTL_LUN_INVALID) 13291 && TAILQ_EMPTY(&lun->ooa_queue)) { 13292 mtx_unlock(&lun->lun_lock); 13293 mtx_lock(&softc->ctl_lock); 13294 ctl_free_lun(lun); 13295 mtx_unlock(&softc->ctl_lock); 13296 } else 13297 mtx_unlock(&lun->lun_lock); 13298 13299bailout: 13300 13301 /* 13302 * If this command has been aborted, make sure we set the status 13303 * properly. The FETD is responsible for freeing the I/O and doing 13304 * whatever it needs to do to clean up its state. 13305 */ 13306 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13307 ctl_set_task_aborted(&io->scsiio); 13308 13309 /* 13310 * If enabled, print command error status. 13311 */ 13312 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13313 (ctl_debug & CTL_DEBUG_INFO) != 0) 13314 ctl_io_error_print(io, NULL); 13315 13316 /* 13317 * Tell the FETD or the other shelf controller we're done with this 13318 * command. Note that only SCSI commands get to this point. Task 13319 * management commands are completed above. 13320 */ 13321 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13322 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13323 memset(&msg, 0, sizeof(msg)); 13324 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13325 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13326 msg.hdr.nexus = io->io_hdr.nexus; 13327 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13328 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13329 M_WAITOK); 13330 } 13331 13332 fe_done(io); 13333} 13334 13335#ifdef CTL_WITH_CA 13336/* 13337 * Front end should call this if it doesn't do autosense. When the request 13338 * sense comes back in from the initiator, we'll dequeue this and send it. 13339 */ 13340int 13341ctl_queue_sense(union ctl_io *io) 13342{ 13343 struct ctl_lun *lun; 13344 struct ctl_port *port; 13345 struct ctl_softc *softc; 13346 uint32_t initidx, targ_lun; 13347 13348 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13349 13350 softc = control_softc; 13351 port = ctl_io_port(&ctsio->io_hdr); 13352 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13353 13354 /* 13355 * LUN lookup will likely move to the ctl_work_thread() once we 13356 * have our new queueing infrastructure (that doesn't put things on 13357 * a per-LUN queue initially). That is so that we can handle 13358 * things like an INQUIRY to a LUN that we don't have enabled. We 13359 * can't deal with that right now. 13360 * If we don't have a LUN for this, just toss the sense information. 13361 */ 13362 mtx_lock(&softc->ctl_lock); 13363 if (targ_lun >= CTL_MAX_LUNS || 13364 (lun = softc->ctl_luns[targ_lun]) == NULL) { 13365 mtx_unlock(&softc->ctl_lock); 13366 goto bailout; 13367 } 13368 mtx_lock(&lun->lun_lock); 13369 mtx_unlock(&softc->ctl_lock); 13370 13371 /* 13372 * Already have CA set for this LUN...toss the sense information. 13373 */ 13374 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13375 if (ctl_is_set(lun->have_ca, initidx)) { 13376 mtx_unlock(&lun->lun_lock); 13377 goto bailout; 13378 } 13379 13380 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13381 MIN(sizeof(lun->pending_sense[initidx]), 13382 sizeof(io->scsiio.sense_data))); 13383 ctl_set_mask(lun->have_ca, initidx); 13384 mtx_unlock(&lun->lun_lock); 13385 13386bailout: 13387 ctl_free_io(io); 13388 return (CTL_RETVAL_COMPLETE); 13389} 13390#endif 13391 13392/* 13393 * Primary command inlet from frontend ports. All SCSI and task I/O 13394 * requests must go through this function. 13395 */ 13396int 13397ctl_queue(union ctl_io *io) 13398{ 13399 struct ctl_port *port; 13400 13401 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13402 13403#ifdef CTL_TIME_IO 13404 io->io_hdr.start_time = time_uptime; 13405 getbinuptime(&io->io_hdr.start_bt); 13406#endif /* CTL_TIME_IO */ 13407 13408 /* Map FE-specific LUN ID into global one. */ 13409 port = ctl_io_port(&io->io_hdr); 13410 io->io_hdr.nexus.targ_mapped_lun = 13411 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13412 13413 switch (io->io_hdr.io_type) { 13414 case CTL_IO_SCSI: 13415 case CTL_IO_TASK: 13416 if (ctl_debug & CTL_DEBUG_CDB) 13417 ctl_io_print(io); 13418 ctl_enqueue_incoming(io); 13419 break; 13420 default: 13421 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13422 return (EINVAL); 13423 } 13424 13425 return (CTL_RETVAL_COMPLETE); 13426} 13427 13428#ifdef CTL_IO_DELAY 13429static void 13430ctl_done_timer_wakeup(void *arg) 13431{ 13432 union ctl_io *io; 13433 13434 io = (union ctl_io *)arg; 13435 ctl_done(io); 13436} 13437#endif /* CTL_IO_DELAY */ 13438 13439void 13440ctl_serseq_done(union ctl_io *io) 13441{ 13442 struct ctl_lun *lun; 13443 13444 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13445 if (lun->be_lun == NULL || 13446 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13447 return; 13448 mtx_lock(&lun->lun_lock); 13449 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13450 ctl_check_blocked(lun); 13451 mtx_unlock(&lun->lun_lock); 13452} 13453 13454void 13455ctl_done(union ctl_io *io) 13456{ 13457 13458 /* 13459 * Enable this to catch duplicate completion issues. 13460 */ 13461#if 0 13462 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13463 printf("%s: type %d msg %d cdb %x iptl: " 13464 "%u:%u:%u tag 0x%04x " 13465 "flag %#x status %x\n", 13466 __func__, 13467 io->io_hdr.io_type, 13468 io->io_hdr.msg_type, 13469 io->scsiio.cdb[0], 13470 io->io_hdr.nexus.initid, 13471 io->io_hdr.nexus.targ_port, 13472 io->io_hdr.nexus.targ_lun, 13473 (io->io_hdr.io_type == 13474 CTL_IO_TASK) ? 13475 io->taskio.tag_num : 13476 io->scsiio.tag_num, 13477 io->io_hdr.flags, 13478 io->io_hdr.status); 13479 } else 13480 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13481#endif 13482 13483 /* 13484 * This is an internal copy of an I/O, and should not go through 13485 * the normal done processing logic. 13486 */ 13487 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13488 return; 13489 13490#ifdef CTL_IO_DELAY 13491 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13492 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13493 } else { 13494 struct ctl_lun *lun; 13495 13496 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13497 13498 if ((lun != NULL) 13499 && (lun->delay_info.done_delay > 0)) { 13500 13501 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13502 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13503 callout_reset(&io->io_hdr.delay_callout, 13504 lun->delay_info.done_delay * hz, 13505 ctl_done_timer_wakeup, io); 13506 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13507 lun->delay_info.done_delay = 0; 13508 return; 13509 } 13510 } 13511#endif /* CTL_IO_DELAY */ 13512 13513 ctl_enqueue_done(io); 13514} 13515 13516static void 13517ctl_work_thread(void *arg) 13518{ 13519 struct ctl_thread *thr = (struct ctl_thread *)arg; 13520 struct ctl_softc *softc = thr->ctl_softc; 13521 union ctl_io *io; 13522 int retval; 13523 13524 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13525 13526 for (;;) { 13527 /* 13528 * We handle the queues in this order: 13529 * - ISC 13530 * - done queue (to free up resources, unblock other commands) 13531 * - RtR queue 13532 * - incoming queue 13533 * 13534 * If those queues are empty, we break out of the loop and 13535 * go to sleep. 13536 */ 13537 mtx_lock(&thr->queue_lock); 13538 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13539 if (io != NULL) { 13540 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13541 mtx_unlock(&thr->queue_lock); 13542 ctl_handle_isc(io); 13543 continue; 13544 } 13545 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13546 if (io != NULL) { 13547 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13548 /* clear any blocked commands, call fe_done */ 13549 mtx_unlock(&thr->queue_lock); 13550 ctl_process_done(io); 13551 continue; 13552 } 13553 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13554 if (io != NULL) { 13555 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13556 mtx_unlock(&thr->queue_lock); 13557 if (io->io_hdr.io_type == CTL_IO_TASK) 13558 ctl_run_task(io); 13559 else 13560 ctl_scsiio_precheck(softc, &io->scsiio); 13561 continue; 13562 } 13563 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13564 if (io != NULL) { 13565 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13566 mtx_unlock(&thr->queue_lock); 13567 retval = ctl_scsiio(&io->scsiio); 13568 if (retval != CTL_RETVAL_COMPLETE) 13569 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13570 continue; 13571 } 13572 13573 /* Sleep until we have something to do. */ 13574 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13575 } 13576} 13577 13578static void 13579ctl_lun_thread(void *arg) 13580{ 13581 struct ctl_softc *softc = (struct ctl_softc *)arg; 13582 struct ctl_be_lun *be_lun; 13583 13584 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13585 13586 for (;;) { 13587 mtx_lock(&softc->ctl_lock); 13588 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13589 if (be_lun != NULL) { 13590 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13591 mtx_unlock(&softc->ctl_lock); 13592 ctl_create_lun(be_lun); 13593 continue; 13594 } 13595 13596 /* Sleep until we have something to do. */ 13597 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13598 PDROP | PRIBIO, "-", 0); 13599 } 13600} 13601 13602static void 13603ctl_thresh_thread(void *arg) 13604{ 13605 struct ctl_softc *softc = (struct ctl_softc *)arg; 13606 struct ctl_lun *lun; 13607 struct ctl_logical_block_provisioning_page *page; 13608 const char *attr; 13609 union ctl_ha_msg msg; 13610 uint64_t thres, val; 13611 int i, e, set; 13612 13613 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13614 13615 for (;;) { 13616 mtx_lock(&softc->ctl_lock); 13617 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13618 if ((lun->flags & CTL_LUN_DISABLED) || 13619 (lun->flags & CTL_LUN_NO_MEDIA) || 13620 lun->backend->lun_attr == NULL) 13621 continue; 13622 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13623 softc->ha_mode == CTL_HA_MODE_XFER) 13624 continue; 13625 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0) 13626 continue; 13627 e = 0; 13628 page = &lun->MODE_LBP; 13629 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13630 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13631 continue; 13632 thres = scsi_4btoul(page->descr[i].count); 13633 thres <<= CTL_LBP_EXPONENT; 13634 switch (page->descr[i].resource) { 13635 case 0x01: 13636 attr = "blocksavail"; 13637 break; 13638 case 0x02: 13639 attr = "blocksused"; 13640 break; 13641 case 0xf1: 13642 attr = "poolblocksavail"; 13643 break; 13644 case 0xf2: 13645 attr = "poolblocksused"; 13646 break; 13647 default: 13648 continue; 13649 } 13650 mtx_unlock(&softc->ctl_lock); // XXX 13651 val = lun->backend->lun_attr( 13652 lun->be_lun->be_lun, attr); 13653 mtx_lock(&softc->ctl_lock); 13654 if (val == UINT64_MAX) 13655 continue; 13656 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13657 == SLBPPD_ARMING_INC) 13658 e = (val >= thres); 13659 else 13660 e = (val <= thres); 13661 if (e) 13662 break; 13663 } 13664 mtx_lock(&lun->lun_lock); 13665 if (e) { 13666 scsi_u64to8b((uint8_t *)&page->descr[i] - 13667 (uint8_t *)page, lun->ua_tpt_info); 13668 if (lun->lasttpt == 0 || 13669 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13670 lun->lasttpt = time_uptime; 13671 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13672 set = 1; 13673 } else 13674 set = 0; 13675 } else { 13676 lun->lasttpt = 0; 13677 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13678 set = -1; 13679 } 13680 mtx_unlock(&lun->lun_lock); 13681 if (set != 0 && 13682 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13683 /* Send msg to other side. */ 13684 bzero(&msg.ua, sizeof(msg.ua)); 13685 msg.hdr.msg_type = CTL_MSG_UA; 13686 msg.hdr.nexus.initid = -1; 13687 msg.hdr.nexus.targ_port = -1; 13688 msg.hdr.nexus.targ_lun = lun->lun; 13689 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13690 msg.ua.ua_all = 1; 13691 msg.ua.ua_set = (set > 0); 13692 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13693 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13694 mtx_unlock(&softc->ctl_lock); // XXX 13695 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13696 sizeof(msg.ua), M_WAITOK); 13697 mtx_lock(&softc->ctl_lock); 13698 } 13699 } 13700 mtx_unlock(&softc->ctl_lock); 13701 pause("-", CTL_LBP_PERIOD * hz); 13702 } 13703} 13704 13705static void 13706ctl_enqueue_incoming(union ctl_io *io) 13707{ 13708 struct ctl_softc *softc = control_softc; 13709 struct ctl_thread *thr; 13710 u_int idx; 13711 13712 idx = (io->io_hdr.nexus.targ_port * 127 + 13713 io->io_hdr.nexus.initid) % worker_threads; 13714 thr = &softc->threads[idx]; 13715 mtx_lock(&thr->queue_lock); 13716 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13717 mtx_unlock(&thr->queue_lock); 13718 wakeup(thr); 13719} 13720 13721static void 13722ctl_enqueue_rtr(union ctl_io *io) 13723{ 13724 struct ctl_softc *softc = control_softc; 13725 struct ctl_thread *thr; 13726 13727 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13728 mtx_lock(&thr->queue_lock); 13729 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13730 mtx_unlock(&thr->queue_lock); 13731 wakeup(thr); 13732} 13733 13734static void 13735ctl_enqueue_done(union ctl_io *io) 13736{ 13737 struct ctl_softc *softc = 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->done_queue, &io->io_hdr, links); 13743 mtx_unlock(&thr->queue_lock); 13744 wakeup(thr); 13745} 13746 13747static void 13748ctl_enqueue_isc(union ctl_io *io) 13749{ 13750 struct ctl_softc *softc = 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->isc_queue, &io->io_hdr, links); 13756 mtx_unlock(&thr->queue_lock); 13757 wakeup(thr); 13758} 13759 13760/* 13761 * vim: ts=8 13762 */ 13763