ctl.c revision 288358
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: head/sys/cam/ctl/ctl.c 288358 2015-09-29 09:09:37Z 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 copan_debugconf_subpage debugconf_page_default = { 97 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 98 DBGCNF_SUBPAGE_CODE, /* subpage */ 99 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 100 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 101 DBGCNF_VERSION, /* page_version */ 102 {CTL_TIME_IO_DEFAULT_SECS>>8, 103 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 104}; 105 106const static struct copan_debugconf_subpage debugconf_page_changeable = { 107 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 108 DBGCNF_SUBPAGE_CODE, /* subpage */ 109 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 110 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 111 0, /* page_version */ 112 {0xff,0xff}, /* ctl_time_io_secs */ 113}; 114 115const static struct scsi_da_rw_recovery_page rw_er_page_default = { 116 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 117 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 118 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 119 /*read_retry_count*/0, 120 /*correction_span*/0, 121 /*head_offset_count*/0, 122 /*data_strobe_offset_cnt*/0, 123 /*byte8*/SMS_RWER_LBPERE, 124 /*write_retry_count*/0, 125 /*reserved2*/0, 126 /*recovery_time_limit*/{0, 0}, 127}; 128 129const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 130 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 131 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 132 /*byte3*/0, 133 /*read_retry_count*/0, 134 /*correction_span*/0, 135 /*head_offset_count*/0, 136 /*data_strobe_offset_cnt*/0, 137 /*byte8*/0, 138 /*write_retry_count*/0, 139 /*reserved2*/0, 140 /*recovery_time_limit*/{0, 0}, 141}; 142 143const static struct scsi_format_page format_page_default = { 144 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 145 /*page_length*/sizeof(struct scsi_format_page) - 2, 146 /*tracks_per_zone*/ {0, 0}, 147 /*alt_sectors_per_zone*/ {0, 0}, 148 /*alt_tracks_per_zone*/ {0, 0}, 149 /*alt_tracks_per_lun*/ {0, 0}, 150 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 151 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 152 /*bytes_per_sector*/ {0, 0}, 153 /*interleave*/ {0, 0}, 154 /*track_skew*/ {0, 0}, 155 /*cylinder_skew*/ {0, 0}, 156 /*flags*/ SFP_HSEC, 157 /*reserved*/ {0, 0, 0} 158}; 159 160const static struct scsi_format_page format_page_changeable = { 161 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 162 /*page_length*/sizeof(struct scsi_format_page) - 2, 163 /*tracks_per_zone*/ {0, 0}, 164 /*alt_sectors_per_zone*/ {0, 0}, 165 /*alt_tracks_per_zone*/ {0, 0}, 166 /*alt_tracks_per_lun*/ {0, 0}, 167 /*sectors_per_track*/ {0, 0}, 168 /*bytes_per_sector*/ {0, 0}, 169 /*interleave*/ {0, 0}, 170 /*track_skew*/ {0, 0}, 171 /*cylinder_skew*/ {0, 0}, 172 /*flags*/ 0, 173 /*reserved*/ {0, 0, 0} 174}; 175 176const static struct scsi_rigid_disk_page rigid_disk_page_default = { 177 /*page_code*/SMS_RIGID_DISK_PAGE, 178 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 179 /*cylinders*/ {0, 0, 0}, 180 /*heads*/ CTL_DEFAULT_HEADS, 181 /*start_write_precomp*/ {0, 0, 0}, 182 /*start_reduced_current*/ {0, 0, 0}, 183 /*step_rate*/ {0, 0}, 184 /*landing_zone_cylinder*/ {0, 0, 0}, 185 /*rpl*/ SRDP_RPL_DISABLED, 186 /*rotational_offset*/ 0, 187 /*reserved1*/ 0, 188 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 189 CTL_DEFAULT_ROTATION_RATE & 0xff}, 190 /*reserved2*/ {0, 0} 191}; 192 193const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 194 /*page_code*/SMS_RIGID_DISK_PAGE, 195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 196 /*cylinders*/ {0, 0, 0}, 197 /*heads*/ 0, 198 /*start_write_precomp*/ {0, 0, 0}, 199 /*start_reduced_current*/ {0, 0, 0}, 200 /*step_rate*/ {0, 0}, 201 /*landing_zone_cylinder*/ {0, 0, 0}, 202 /*rpl*/ 0, 203 /*rotational_offset*/ 0, 204 /*reserved1*/ 0, 205 /*rotation_rate*/ {0, 0}, 206 /*reserved2*/ {0, 0} 207}; 208 209const static struct scsi_caching_page caching_page_default = { 210 /*page_code*/SMS_CACHING_PAGE, 211 /*page_length*/sizeof(struct scsi_caching_page) - 2, 212 /*flags1*/ SCP_DISC | SCP_WCE, 213 /*ret_priority*/ 0, 214 /*disable_pf_transfer_len*/ {0xff, 0xff}, 215 /*min_prefetch*/ {0, 0}, 216 /*max_prefetch*/ {0xff, 0xff}, 217 /*max_pf_ceiling*/ {0xff, 0xff}, 218 /*flags2*/ 0, 219 /*cache_segments*/ 0, 220 /*cache_seg_size*/ {0, 0}, 221 /*reserved*/ 0, 222 /*non_cache_seg_size*/ {0, 0, 0} 223}; 224 225const static struct scsi_caching_page caching_page_changeable = { 226 /*page_code*/SMS_CACHING_PAGE, 227 /*page_length*/sizeof(struct scsi_caching_page) - 2, 228 /*flags1*/ SCP_WCE | SCP_RCD, 229 /*ret_priority*/ 0, 230 /*disable_pf_transfer_len*/ {0, 0}, 231 /*min_prefetch*/ {0, 0}, 232 /*max_prefetch*/ {0, 0}, 233 /*max_pf_ceiling*/ {0, 0}, 234 /*flags2*/ 0, 235 /*cache_segments*/ 0, 236 /*cache_seg_size*/ {0, 0}, 237 /*reserved*/ 0, 238 /*non_cache_seg_size*/ {0, 0, 0} 239}; 240 241const static struct scsi_control_page control_page_default = { 242 /*page_code*/SMS_CONTROL_MODE_PAGE, 243 /*page_length*/sizeof(struct scsi_control_page) - 2, 244 /*rlec*/0, 245 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 246 /*eca_and_aen*/0, 247 /*flags4*/SCP_TAS, 248 /*aen_holdoff_period*/{0, 0}, 249 /*busy_timeout_period*/{0, 0}, 250 /*extended_selftest_completion_time*/{0, 0} 251}; 252 253const static struct scsi_control_page control_page_changeable = { 254 /*page_code*/SMS_CONTROL_MODE_PAGE, 255 /*page_length*/sizeof(struct scsi_control_page) - 2, 256 /*rlec*/SCP_DSENSE, 257 /*queue_flags*/SCP_QUEUE_ALG_MASK, 258 /*eca_and_aen*/SCP_SWP, 259 /*flags4*/0, 260 /*aen_holdoff_period*/{0, 0}, 261 /*busy_timeout_period*/{0, 0}, 262 /*extended_selftest_completion_time*/{0, 0} 263}; 264 265#define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 266 267const static struct scsi_control_ext_page control_ext_page_default = { 268 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 269 /*subpage_code*/0x01, 270 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 271 /*flags*/0, 272 /*prio*/0, 273 /*max_sense*/0 274}; 275 276const static struct scsi_control_ext_page control_ext_page_changeable = { 277 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 278 /*subpage_code*/0x01, 279 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 280 /*flags*/0, 281 /*prio*/0, 282 /*max_sense*/0 283}; 284 285const static struct scsi_info_exceptions_page ie_page_default = { 286 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 287 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 288 /*info_flags*/SIEP_FLAGS_DEXCPT, 289 /*mrie*/0, 290 /*interval_timer*/{0, 0, 0, 0}, 291 /*report_count*/{0, 0, 0, 0} 292}; 293 294const static struct scsi_info_exceptions_page ie_page_changeable = { 295 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 296 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 297 /*info_flags*/0, 298 /*mrie*/0, 299 /*interval_timer*/{0, 0, 0, 0}, 300 /*report_count*/{0, 0, 0, 0} 301}; 302 303#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 304 305const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 307 /*subpage_code*/0x02, 308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 309 /*flags*/0, 310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 311 /*descr*/{}}, 312 {{/*flags*/0, 313 /*resource*/0x01, 314 /*reserved*/{0, 0}, 315 /*count*/{0, 0, 0, 0}}, 316 {/*flags*/0, 317 /*resource*/0x02, 318 /*reserved*/{0, 0}, 319 /*count*/{0, 0, 0, 0}}, 320 {/*flags*/0, 321 /*resource*/0xf1, 322 /*reserved*/{0, 0}, 323 /*count*/{0, 0, 0, 0}}, 324 {/*flags*/0, 325 /*resource*/0xf2, 326 /*reserved*/{0, 0}, 327 /*count*/{0, 0, 0, 0}} 328 } 329}; 330 331const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 333 /*subpage_code*/0x02, 334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 335 /*flags*/0, 336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 337 /*descr*/{}}, 338 {{/*flags*/0, 339 /*resource*/0, 340 /*reserved*/{0, 0}, 341 /*count*/{0, 0, 0, 0}}, 342 {/*flags*/0, 343 /*resource*/0, 344 /*reserved*/{0, 0}, 345 /*count*/{0, 0, 0, 0}}, 346 {/*flags*/0, 347 /*resource*/0, 348 /*reserved*/{0, 0}, 349 /*count*/{0, 0, 0, 0}}, 350 {/*flags*/0, 351 /*resource*/0, 352 /*reserved*/{0, 0}, 353 /*count*/{0, 0, 0, 0}} 354 } 355}; 356 357const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 358 /*page_code*/SMS_CDDVD_CAPS_PAGE, 359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 360 /*caps1*/0x3f, 361 /*caps2*/0x00, 362 /*caps3*/0xf0, 363 /*caps4*/0x00, 364 /*caps5*/0x29, 365 /*caps6*/0x00, 366 /*obsolete*/{0, 0}, 367 /*nvol_levels*/{0, 0}, 368 /*buffer_size*/{8, 0}, 369 /*obsolete2*/{0, 0}, 370 /*reserved*/0, 371 /*digital*/0, 372 /*obsolete3*/0, 373 /*copy_management*/0, 374 /*reserved2*/0, 375 /*rotation_control*/0, 376 /*cur_write_speed*/0, 377 /*num_speed_descr*/0, 378}; 379 380const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 381 /*page_code*/SMS_CDDVD_CAPS_PAGE, 382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 383 /*caps1*/0, 384 /*caps2*/0, 385 /*caps3*/0, 386 /*caps4*/0, 387 /*caps5*/0, 388 /*caps6*/0, 389 /*obsolete*/{0, 0}, 390 /*nvol_levels*/{0, 0}, 391 /*buffer_size*/{0, 0}, 392 /*obsolete2*/{0, 0}, 393 /*reserved*/0, 394 /*digital*/0, 395 /*obsolete3*/0, 396 /*copy_management*/0, 397 /*reserved2*/0, 398 /*rotation_control*/0, 399 /*cur_write_speed*/0, 400 /*num_speed_descr*/0, 401}; 402 403SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 404static int worker_threads = -1; 405SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 406 &worker_threads, 1, "Number of worker threads"); 407static int ctl_debug = CTL_DEBUG_NONE; 408SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 409 &ctl_debug, 0, "Enabled debug flags"); 410 411/* 412 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 413 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 414 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 415 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 416 */ 417#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 418 419static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 420 int param); 421static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 422static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 423static int ctl_init(void); 424void ctl_shutdown(void); 425static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 426static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 427static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 428static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 429 struct ctl_ooa *ooa_hdr, 430 struct ctl_ooa_entry *kern_entries); 431static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 432 struct thread *td); 433static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 434 struct ctl_be_lun *be_lun); 435static int ctl_free_lun(struct ctl_lun *lun); 436static void ctl_create_lun(struct ctl_be_lun *be_lun); 437static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 438 439static int ctl_do_mode_select(union ctl_io *io); 440static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 441 uint64_t res_key, uint64_t sa_res_key, 442 uint8_t type, uint32_t residx, 443 struct ctl_scsiio *ctsio, 444 struct scsi_per_res_out *cdb, 445 struct scsi_per_res_out_parms* param); 446static void ctl_pro_preempt_other(struct ctl_lun *lun, 447 union ctl_ha_msg *msg); 448static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 449static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 450static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 451static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 452static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 453static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 454static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 455 int alloc_len); 456static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 457 int alloc_len); 458static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 459static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 460static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 461static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 462static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 463static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 464 bool seq); 465static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 466static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 467 union ctl_io *pending_io, union ctl_io *ooa_io); 468static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 469 union ctl_io *starting_io); 470static int ctl_check_blocked(struct ctl_lun *lun); 471static int ctl_scsiio_lun_check(struct ctl_lun *lun, 472 const struct ctl_cmd_entry *entry, 473 struct ctl_scsiio *ctsio); 474static void ctl_failover_lun(union ctl_io *io); 475static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 476 struct ctl_scsiio *ctsio); 477static int ctl_scsiio(struct ctl_scsiio *ctsio); 478 479static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 480static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 481 ctl_ua_type ua_type); 482static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 483 ctl_ua_type ua_type); 484static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 485static int ctl_abort_task(union ctl_io *io); 486static int ctl_abort_task_set(union ctl_io *io); 487static int ctl_query_task(union ctl_io *io, int task_set); 488static int ctl_i_t_nexus_reset(union ctl_io *io); 489static int ctl_query_async_event(union ctl_io *io); 490static void ctl_run_task(union ctl_io *io); 491#ifdef CTL_IO_DELAY 492static void ctl_datamove_timer_wakeup(void *arg); 493static void ctl_done_timer_wakeup(void *arg); 494#endif /* CTL_IO_DELAY */ 495 496static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 497static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 498static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 499static void ctl_datamove_remote_write(union ctl_io *io); 500static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 501static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 502static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 503static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 504 ctl_ha_dt_cb callback); 505static void ctl_datamove_remote_read(union ctl_io *io); 506static void ctl_datamove_remote(union ctl_io *io); 507static void ctl_process_done(union ctl_io *io); 508static void ctl_lun_thread(void *arg); 509static void ctl_thresh_thread(void *arg); 510static void ctl_work_thread(void *arg); 511static void ctl_enqueue_incoming(union ctl_io *io); 512static void ctl_enqueue_rtr(union ctl_io *io); 513static void ctl_enqueue_done(union ctl_io *io); 514static void ctl_enqueue_isc(union ctl_io *io); 515static const struct ctl_cmd_entry * 516 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 517static const struct ctl_cmd_entry * 518 ctl_validate_command(struct ctl_scsiio *ctsio); 519static int ctl_cmd_applicable(uint8_t lun_type, 520 const struct ctl_cmd_entry *entry); 521 522static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 523static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 524static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 525static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 526 527/* 528 * Load the serialization table. This isn't very pretty, but is probably 529 * the easiest way to do it. 530 */ 531#include "ctl_ser_table.c" 532 533/* 534 * We only need to define open, close and ioctl routines for this driver. 535 */ 536static struct cdevsw ctl_cdevsw = { 537 .d_version = D_VERSION, 538 .d_flags = 0, 539 .d_open = ctl_open, 540 .d_close = ctl_close, 541 .d_ioctl = ctl_ioctl, 542 .d_name = "ctl", 543}; 544 545 546MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 547 548static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 549 550static moduledata_t ctl_moduledata = { 551 "ctl", 552 ctl_module_event_handler, 553 NULL 554}; 555 556DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 557MODULE_VERSION(ctl, 1); 558 559static struct ctl_frontend ha_frontend = 560{ 561 .name = "ha", 562}; 563 564static void 565ctl_ha_datamove(union ctl_io *io) 566{ 567 struct ctl_lun *lun; 568 struct ctl_sg_entry *sgl; 569 union ctl_ha_msg msg; 570 uint32_t sg_entries_sent; 571 int do_sg_copy, i, j; 572 573 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 574 memset(&msg.dt, 0, sizeof(msg.dt)); 575 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 576 msg.hdr.original_sc = io->io_hdr.original_sc; 577 msg.hdr.serializing_sc = io; 578 msg.hdr.nexus = io->io_hdr.nexus; 579 msg.hdr.status = io->io_hdr.status; 580 msg.dt.flags = io->io_hdr.flags; 581 582 /* 583 * We convert everything into a S/G list here. We can't 584 * pass by reference, only by value between controllers. 585 * So we can't pass a pointer to the S/G list, only as many 586 * S/G entries as we can fit in here. If it's possible for 587 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 588 * then we need to break this up into multiple transfers. 589 */ 590 if (io->scsiio.kern_sg_entries == 0) { 591 msg.dt.kern_sg_entries = 1; 592#if 0 593 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 594 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 595 } else { 596 /* XXX KDM use busdma here! */ 597 msg.dt.sg_list[0].addr = 598 (void *)vtophys(io->scsiio.kern_data_ptr); 599 } 600#else 601 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 602 ("HA does not support BUS_ADDR")); 603 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 604#endif 605 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 606 do_sg_copy = 0; 607 } else { 608 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 609 do_sg_copy = 1; 610 } 611 612 msg.dt.kern_data_len = io->scsiio.kern_data_len; 613 msg.dt.kern_total_len = io->scsiio.kern_total_len; 614 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 615 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 616 msg.dt.sg_sequence = 0; 617 618 /* 619 * Loop until we've sent all of the S/G entries. On the 620 * other end, we'll recompose these S/G entries into one 621 * contiguous list before processing. 622 */ 623 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 624 msg.dt.sg_sequence++) { 625 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 626 sizeof(msg.dt.sg_list[0])), 627 msg.dt.kern_sg_entries - sg_entries_sent); 628 if (do_sg_copy != 0) { 629 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 630 for (i = sg_entries_sent, j = 0; 631 i < msg.dt.cur_sg_entries; i++, j++) { 632#if 0 633 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 634 msg.dt.sg_list[j].addr = sgl[i].addr; 635 } else { 636 /* XXX KDM use busdma here! */ 637 msg.dt.sg_list[j].addr = 638 (void *)vtophys(sgl[i].addr); 639 } 640#else 641 KASSERT((io->io_hdr.flags & 642 CTL_FLAG_BUS_ADDR) == 0, 643 ("HA does not support BUS_ADDR")); 644 msg.dt.sg_list[j].addr = sgl[i].addr; 645#endif 646 msg.dt.sg_list[j].len = sgl[i].len; 647 } 648 } 649 650 sg_entries_sent += msg.dt.cur_sg_entries; 651 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 652 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 653 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 654 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 655 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 656 io->io_hdr.port_status = 31341; 657 io->scsiio.be_move_done(io); 658 return; 659 } 660 msg.dt.sent_sg_entries = sg_entries_sent; 661 } 662 663 /* 664 * Officially handover the request from us to peer. 665 * If failover has just happened, then we must return error. 666 * If failover happen just after, then it is not our problem. 667 */ 668 if (lun) 669 mtx_lock(&lun->lun_lock); 670 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 671 if (lun) 672 mtx_unlock(&lun->lun_lock); 673 io->io_hdr.port_status = 31342; 674 io->scsiio.be_move_done(io); 675 return; 676 } 677 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 678 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 679 if (lun) 680 mtx_unlock(&lun->lun_lock); 681} 682 683static void 684ctl_ha_done(union ctl_io *io) 685{ 686 union ctl_ha_msg msg; 687 688 if (io->io_hdr.io_type == CTL_IO_SCSI) { 689 memset(&msg, 0, sizeof(msg)); 690 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 691 msg.hdr.original_sc = io->io_hdr.original_sc; 692 msg.hdr.nexus = io->io_hdr.nexus; 693 msg.hdr.status = io->io_hdr.status; 694 msg.scsi.scsi_status = io->scsiio.scsi_status; 695 msg.scsi.tag_num = io->scsiio.tag_num; 696 msg.scsi.tag_type = io->scsiio.tag_type; 697 msg.scsi.sense_len = io->scsiio.sense_len; 698 msg.scsi.sense_residual = io->scsiio.sense_residual; 699 msg.scsi.residual = io->scsiio.residual; 700 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 701 io->scsiio.sense_len); 702 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 703 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 704 msg.scsi.sense_len, M_WAITOK); 705 } 706 ctl_free_io(io); 707} 708 709static void 710ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 711 union ctl_ha_msg *msg_info) 712{ 713 struct ctl_scsiio *ctsio; 714 715 if (msg_info->hdr.original_sc == NULL) { 716 printf("%s: original_sc == NULL!\n", __func__); 717 /* XXX KDM now what? */ 718 return; 719 } 720 721 ctsio = &msg_info->hdr.original_sc->scsiio; 722 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 723 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 724 ctsio->io_hdr.status = msg_info->hdr.status; 725 ctsio->scsi_status = msg_info->scsi.scsi_status; 726 ctsio->sense_len = msg_info->scsi.sense_len; 727 ctsio->sense_residual = msg_info->scsi.sense_residual; 728 ctsio->residual = msg_info->scsi.residual; 729 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 730 msg_info->scsi.sense_len); 731 ctl_enqueue_isc((union ctl_io *)ctsio); 732} 733 734static void 735ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 736 union ctl_ha_msg *msg_info) 737{ 738 struct ctl_scsiio *ctsio; 739 740 if (msg_info->hdr.serializing_sc == NULL) { 741 printf("%s: serializing_sc == NULL!\n", __func__); 742 /* XXX KDM now what? */ 743 return; 744 } 745 746 ctsio = &msg_info->hdr.serializing_sc->scsiio; 747 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 748 ctl_enqueue_isc((union ctl_io *)ctsio); 749} 750 751void 752ctl_isc_announce_lun(struct ctl_lun *lun) 753{ 754 struct ctl_softc *softc = lun->ctl_softc; 755 union ctl_ha_msg *msg; 756 struct ctl_ha_msg_lun_pr_key pr_key; 757 int i, k; 758 759 if (softc->ha_link != CTL_HA_LINK_ONLINE) 760 return; 761 mtx_lock(&lun->lun_lock); 762 i = sizeof(msg->lun); 763 if (lun->lun_devid) 764 i += lun->lun_devid->len; 765 i += sizeof(pr_key) * lun->pr_key_count; 766alloc: 767 mtx_unlock(&lun->lun_lock); 768 msg = malloc(i, M_CTL, M_WAITOK); 769 mtx_lock(&lun->lun_lock); 770 k = sizeof(msg->lun); 771 if (lun->lun_devid) 772 k += lun->lun_devid->len; 773 k += sizeof(pr_key) * lun->pr_key_count; 774 if (i < k) { 775 free(msg, M_CTL); 776 i = k; 777 goto alloc; 778 } 779 bzero(&msg->lun, sizeof(msg->lun)); 780 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 781 msg->hdr.nexus.targ_lun = lun->lun; 782 msg->hdr.nexus.targ_mapped_lun = lun->lun; 783 msg->lun.flags = lun->flags; 784 msg->lun.pr_generation = lun->PRGeneration; 785 msg->lun.pr_res_idx = lun->pr_res_idx; 786 msg->lun.pr_res_type = lun->res_type; 787 msg->lun.pr_key_count = lun->pr_key_count; 788 i = 0; 789 if (lun->lun_devid) { 790 msg->lun.lun_devid_len = lun->lun_devid->len; 791 memcpy(&msg->lun.data[i], lun->lun_devid->data, 792 msg->lun.lun_devid_len); 793 i += msg->lun.lun_devid_len; 794 } 795 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 796 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 797 continue; 798 pr_key.pr_iid = k; 799 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 800 i += sizeof(pr_key); 801 } 802 mtx_unlock(&lun->lun_lock); 803 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 804 M_WAITOK); 805 free(msg, M_CTL); 806 807 if (lun->flags & CTL_LUN_PRIMARY_SC) { 808 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 809 ctl_isc_announce_mode(lun, -1, 810 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 811 lun->mode_pages.index[i].subpage); 812 } 813 } 814} 815 816void 817ctl_isc_announce_port(struct ctl_port *port) 818{ 819 struct ctl_softc *softc = port->ctl_softc; 820 union ctl_ha_msg *msg; 821 int i; 822 823 if (port->targ_port < softc->port_min || 824 port->targ_port >= softc->port_max || 825 softc->ha_link != CTL_HA_LINK_ONLINE) 826 return; 827 i = sizeof(msg->port) + strlen(port->port_name) + 1; 828 if (port->lun_map) 829 i += sizeof(uint32_t) * CTL_MAX_LUNS; 830 if (port->port_devid) 831 i += port->port_devid->len; 832 if (port->target_devid) 833 i += port->target_devid->len; 834 if (port->init_devid) 835 i += port->init_devid->len; 836 msg = malloc(i, M_CTL, M_WAITOK); 837 bzero(&msg->port, sizeof(msg->port)); 838 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 839 msg->hdr.nexus.targ_port = port->targ_port; 840 msg->port.port_type = port->port_type; 841 msg->port.physical_port = port->physical_port; 842 msg->port.virtual_port = port->virtual_port; 843 msg->port.status = port->status; 844 i = 0; 845 msg->port.name_len = sprintf(&msg->port.data[i], 846 "%d:%s", softc->ha_id, port->port_name) + 1; 847 i += msg->port.name_len; 848 if (port->lun_map) { 849 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 850 memcpy(&msg->port.data[i], port->lun_map, 851 msg->port.lun_map_len); 852 i += msg->port.lun_map_len; 853 } 854 if (port->port_devid) { 855 msg->port.port_devid_len = port->port_devid->len; 856 memcpy(&msg->port.data[i], port->port_devid->data, 857 msg->port.port_devid_len); 858 i += msg->port.port_devid_len; 859 } 860 if (port->target_devid) { 861 msg->port.target_devid_len = port->target_devid->len; 862 memcpy(&msg->port.data[i], port->target_devid->data, 863 msg->port.target_devid_len); 864 i += msg->port.target_devid_len; 865 } 866 if (port->init_devid) { 867 msg->port.init_devid_len = port->init_devid->len; 868 memcpy(&msg->port.data[i], port->init_devid->data, 869 msg->port.init_devid_len); 870 i += msg->port.init_devid_len; 871 } 872 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 873 M_WAITOK); 874 free(msg, M_CTL); 875} 876 877void 878ctl_isc_announce_iid(struct ctl_port *port, int iid) 879{ 880 struct ctl_softc *softc = port->ctl_softc; 881 union ctl_ha_msg *msg; 882 int i, l; 883 884 if (port->targ_port < softc->port_min || 885 port->targ_port >= softc->port_max || 886 softc->ha_link != CTL_HA_LINK_ONLINE) 887 return; 888 mtx_lock(&softc->ctl_lock); 889 i = sizeof(msg->iid); 890 l = 0; 891 if (port->wwpn_iid[iid].name) 892 l = strlen(port->wwpn_iid[iid].name) + 1; 893 i += l; 894 msg = malloc(i, M_CTL, M_NOWAIT); 895 if (msg == NULL) { 896 mtx_unlock(&softc->ctl_lock); 897 return; 898 } 899 bzero(&msg->iid, sizeof(msg->iid)); 900 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 901 msg->hdr.nexus.targ_port = port->targ_port; 902 msg->hdr.nexus.initid = iid; 903 msg->iid.in_use = port->wwpn_iid[iid].in_use; 904 msg->iid.name_len = l; 905 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 906 if (port->wwpn_iid[iid].name) 907 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 908 mtx_unlock(&softc->ctl_lock); 909 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 910 free(msg, M_CTL); 911} 912 913void 914ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 915 uint8_t page, uint8_t subpage) 916{ 917 struct ctl_softc *softc = lun->ctl_softc; 918 union ctl_ha_msg msg; 919 int i; 920 921 if (softc->ha_link != CTL_HA_LINK_ONLINE) 922 return; 923 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 924 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 925 page && lun->mode_pages.index[i].subpage == subpage) 926 break; 927 } 928 if (i == CTL_NUM_MODE_PAGES) 929 return; 930 bzero(&msg.mode, sizeof(msg.mode)); 931 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 932 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 933 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 934 msg.hdr.nexus.targ_lun = lun->lun; 935 msg.hdr.nexus.targ_mapped_lun = lun->lun; 936 msg.mode.page_code = page; 937 msg.mode.subpage = subpage; 938 msg.mode.page_len = lun->mode_pages.index[i].page_len; 939 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 940 msg.mode.page_len); 941 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 942 M_WAITOK); 943} 944 945static void 946ctl_isc_ha_link_up(struct ctl_softc *softc) 947{ 948 struct ctl_port *port; 949 struct ctl_lun *lun; 950 union ctl_ha_msg msg; 951 int i; 952 953 /* Announce this node parameters to peer for validation. */ 954 msg.login.msg_type = CTL_MSG_LOGIN; 955 msg.login.version = CTL_HA_VERSION; 956 msg.login.ha_mode = softc->ha_mode; 957 msg.login.ha_id = softc->ha_id; 958 msg.login.max_luns = CTL_MAX_LUNS; 959 msg.login.max_ports = CTL_MAX_PORTS; 960 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 961 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 962 M_WAITOK); 963 964 STAILQ_FOREACH(port, &softc->port_list, links) { 965 ctl_isc_announce_port(port); 966 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 967 if (port->wwpn_iid[i].in_use) 968 ctl_isc_announce_iid(port, i); 969 } 970 } 971 STAILQ_FOREACH(lun, &softc->lun_list, links) 972 ctl_isc_announce_lun(lun); 973} 974 975static void 976ctl_isc_ha_link_down(struct ctl_softc *softc) 977{ 978 struct ctl_port *port; 979 struct ctl_lun *lun; 980 union ctl_io *io; 981 int i; 982 983 mtx_lock(&softc->ctl_lock); 984 STAILQ_FOREACH(lun, &softc->lun_list, links) { 985 mtx_lock(&lun->lun_lock); 986 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 987 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 988 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 989 } 990 mtx_unlock(&lun->lun_lock); 991 992 mtx_unlock(&softc->ctl_lock); 993 io = ctl_alloc_io(softc->othersc_pool); 994 mtx_lock(&softc->ctl_lock); 995 ctl_zero_io(io); 996 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 997 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 998 ctl_enqueue_isc(io); 999 } 1000 1001 STAILQ_FOREACH(port, &softc->port_list, links) { 1002 if (port->targ_port >= softc->port_min && 1003 port->targ_port < softc->port_max) 1004 continue; 1005 port->status &= ~CTL_PORT_STATUS_ONLINE; 1006 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1007 port->wwpn_iid[i].in_use = 0; 1008 free(port->wwpn_iid[i].name, M_CTL); 1009 port->wwpn_iid[i].name = NULL; 1010 } 1011 } 1012 mtx_unlock(&softc->ctl_lock); 1013} 1014 1015static void 1016ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1017{ 1018 struct ctl_lun *lun; 1019 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1020 1021 mtx_lock(&softc->ctl_lock); 1022 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 1023 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 1024 mtx_lock(&lun->lun_lock); 1025 mtx_unlock(&softc->ctl_lock); 1026 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 1027 msg->ua.ua_set) 1028 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1029 if (msg->ua.ua_all) { 1030 if (msg->ua.ua_set) 1031 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1032 else 1033 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1034 } else { 1035 if (msg->ua.ua_set) 1036 ctl_est_ua(lun, iid, msg->ua.ua_type); 1037 else 1038 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1039 } 1040 mtx_unlock(&lun->lun_lock); 1041 } else 1042 mtx_unlock(&softc->ctl_lock); 1043} 1044 1045static void 1046ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1047{ 1048 struct ctl_lun *lun; 1049 struct ctl_ha_msg_lun_pr_key pr_key; 1050 int i, k; 1051 ctl_lun_flags oflags; 1052 uint32_t targ_lun; 1053 1054 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1055 mtx_lock(&softc->ctl_lock); 1056 if ((targ_lun >= CTL_MAX_LUNS) || 1057 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1058 mtx_unlock(&softc->ctl_lock); 1059 return; 1060 } 1061 mtx_lock(&lun->lun_lock); 1062 mtx_unlock(&softc->ctl_lock); 1063 if (lun->flags & CTL_LUN_DISABLED) { 1064 mtx_unlock(&lun->lun_lock); 1065 return; 1066 } 1067 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1068 if (msg->lun.lun_devid_len != i || (i > 0 && 1069 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1070 mtx_unlock(&lun->lun_lock); 1071 printf("%s: Received conflicting HA LUN %d\n", 1072 __func__, msg->hdr.nexus.targ_lun); 1073 return; 1074 } else { 1075 /* Record whether peer is primary. */ 1076 oflags = lun->flags; 1077 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1078 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1079 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1080 else 1081 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1082 if (oflags != lun->flags) 1083 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1084 1085 /* If peer is primary and we are not -- use data */ 1086 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1087 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1088 lun->PRGeneration = msg->lun.pr_generation; 1089 lun->pr_res_idx = msg->lun.pr_res_idx; 1090 lun->res_type = msg->lun.pr_res_type; 1091 lun->pr_key_count = msg->lun.pr_key_count; 1092 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1093 ctl_clr_prkey(lun, k); 1094 for (k = 0; k < msg->lun.pr_key_count; k++) { 1095 memcpy(&pr_key, &msg->lun.data[i], 1096 sizeof(pr_key)); 1097 ctl_alloc_prkey(lun, pr_key.pr_iid); 1098 ctl_set_prkey(lun, pr_key.pr_iid, 1099 pr_key.pr_key); 1100 i += sizeof(pr_key); 1101 } 1102 } 1103 1104 mtx_unlock(&lun->lun_lock); 1105 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1106 __func__, msg->hdr.nexus.targ_lun, 1107 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1108 "primary" : "secondary")); 1109 1110 /* If we are primary but peer doesn't know -- notify */ 1111 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1112 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1113 ctl_isc_announce_lun(lun); 1114 } 1115} 1116 1117static void 1118ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1119{ 1120 struct ctl_port *port; 1121 struct ctl_lun *lun; 1122 int i, new; 1123 1124 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1125 if (port == NULL) { 1126 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1127 msg->hdr.nexus.targ_port)); 1128 new = 1; 1129 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1130 port->frontend = &ha_frontend; 1131 port->targ_port = msg->hdr.nexus.targ_port; 1132 port->fe_datamove = ctl_ha_datamove; 1133 port->fe_done = ctl_ha_done; 1134 } else if (port->frontend == &ha_frontend) { 1135 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1136 msg->hdr.nexus.targ_port)); 1137 new = 0; 1138 } else { 1139 printf("%s: Received conflicting HA port %d\n", 1140 __func__, msg->hdr.nexus.targ_port); 1141 return; 1142 } 1143 port->port_type = msg->port.port_type; 1144 port->physical_port = msg->port.physical_port; 1145 port->virtual_port = msg->port.virtual_port; 1146 port->status = msg->port.status; 1147 i = 0; 1148 free(port->port_name, M_CTL); 1149 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1150 M_CTL); 1151 i += msg->port.name_len; 1152 if (msg->port.lun_map_len != 0) { 1153 if (port->lun_map == NULL) 1154 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 1155 M_CTL, M_WAITOK); 1156 memcpy(port->lun_map, &msg->port.data[i], 1157 sizeof(uint32_t) * CTL_MAX_LUNS); 1158 i += msg->port.lun_map_len; 1159 } else { 1160 free(port->lun_map, M_CTL); 1161 port->lun_map = NULL; 1162 } 1163 if (msg->port.port_devid_len != 0) { 1164 if (port->port_devid == NULL || 1165 port->port_devid->len != msg->port.port_devid_len) { 1166 free(port->port_devid, M_CTL); 1167 port->port_devid = malloc(sizeof(struct ctl_devid) + 1168 msg->port.port_devid_len, M_CTL, M_WAITOK); 1169 } 1170 memcpy(port->port_devid->data, &msg->port.data[i], 1171 msg->port.port_devid_len); 1172 port->port_devid->len = msg->port.port_devid_len; 1173 i += msg->port.port_devid_len; 1174 } else { 1175 free(port->port_devid, M_CTL); 1176 port->port_devid = NULL; 1177 } 1178 if (msg->port.target_devid_len != 0) { 1179 if (port->target_devid == NULL || 1180 port->target_devid->len != msg->port.target_devid_len) { 1181 free(port->target_devid, M_CTL); 1182 port->target_devid = malloc(sizeof(struct ctl_devid) + 1183 msg->port.target_devid_len, M_CTL, M_WAITOK); 1184 } 1185 memcpy(port->target_devid->data, &msg->port.data[i], 1186 msg->port.target_devid_len); 1187 port->target_devid->len = msg->port.target_devid_len; 1188 i += msg->port.target_devid_len; 1189 } else { 1190 free(port->target_devid, M_CTL); 1191 port->target_devid = NULL; 1192 } 1193 if (msg->port.init_devid_len != 0) { 1194 if (port->init_devid == NULL || 1195 port->init_devid->len != msg->port.init_devid_len) { 1196 free(port->init_devid, M_CTL); 1197 port->init_devid = malloc(sizeof(struct ctl_devid) + 1198 msg->port.init_devid_len, M_CTL, M_WAITOK); 1199 } 1200 memcpy(port->init_devid->data, &msg->port.data[i], 1201 msg->port.init_devid_len); 1202 port->init_devid->len = msg->port.init_devid_len; 1203 i += msg->port.init_devid_len; 1204 } else { 1205 free(port->init_devid, M_CTL); 1206 port->init_devid = NULL; 1207 } 1208 if (new) { 1209 if (ctl_port_register(port) != 0) { 1210 printf("%s: ctl_port_register() failed with error\n", 1211 __func__); 1212 } 1213 } 1214 mtx_lock(&softc->ctl_lock); 1215 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1216 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 1217 continue; 1218 mtx_lock(&lun->lun_lock); 1219 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1220 mtx_unlock(&lun->lun_lock); 1221 } 1222 mtx_unlock(&softc->ctl_lock); 1223} 1224 1225static void 1226ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1227{ 1228 struct ctl_port *port; 1229 int iid; 1230 1231 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1232 if (port == NULL) { 1233 printf("%s: Received IID for unknown port %d\n", 1234 __func__, msg->hdr.nexus.targ_port); 1235 return; 1236 } 1237 iid = msg->hdr.nexus.initid; 1238 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1239 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1240 free(port->wwpn_iid[iid].name, M_CTL); 1241 if (msg->iid.name_len) { 1242 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1243 msg->iid.name_len, M_CTL); 1244 } else 1245 port->wwpn_iid[iid].name = NULL; 1246} 1247 1248static void 1249ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1250{ 1251 1252 if (msg->login.version != CTL_HA_VERSION) { 1253 printf("CTL HA peers have different versions %d != %d\n", 1254 msg->login.version, CTL_HA_VERSION); 1255 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1256 return; 1257 } 1258 if (msg->login.ha_mode != softc->ha_mode) { 1259 printf("CTL HA peers have different ha_mode %d != %d\n", 1260 msg->login.ha_mode, softc->ha_mode); 1261 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1262 return; 1263 } 1264 if (msg->login.ha_id == softc->ha_id) { 1265 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1266 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1267 return; 1268 } 1269 if (msg->login.max_luns != CTL_MAX_LUNS || 1270 msg->login.max_ports != CTL_MAX_PORTS || 1271 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1272 printf("CTL HA peers have different limits\n"); 1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1274 return; 1275 } 1276} 1277 1278static void 1279ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1280{ 1281 struct ctl_lun *lun; 1282 int i; 1283 uint32_t initidx, targ_lun; 1284 1285 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1286 mtx_lock(&softc->ctl_lock); 1287 if ((targ_lun >= CTL_MAX_LUNS) || 1288 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1289 mtx_unlock(&softc->ctl_lock); 1290 return; 1291 } 1292 mtx_lock(&lun->lun_lock); 1293 mtx_unlock(&softc->ctl_lock); 1294 if (lun->flags & CTL_LUN_DISABLED) { 1295 mtx_unlock(&lun->lun_lock); 1296 return; 1297 } 1298 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1299 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1300 msg->mode.page_code && 1301 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1302 break; 1303 } 1304 if (i == CTL_NUM_MODE_PAGES) { 1305 mtx_unlock(&lun->lun_lock); 1306 return; 1307 } 1308 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1309 lun->mode_pages.index[i].page_len); 1310 initidx = ctl_get_initindex(&msg->hdr.nexus); 1311 if (initidx != -1) 1312 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1313 mtx_unlock(&lun->lun_lock); 1314} 1315 1316/* 1317 * ISC (Inter Shelf Communication) event handler. Events from the HA 1318 * subsystem come in here. 1319 */ 1320static void 1321ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1322{ 1323 struct ctl_softc *softc = control_softc; 1324 union ctl_io *io; 1325 struct ctl_prio *presio; 1326 ctl_ha_status isc_status; 1327 1328 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1329 if (event == CTL_HA_EVT_MSG_RECV) { 1330 union ctl_ha_msg *msg, msgbuf; 1331 1332 if (param > sizeof(msgbuf)) 1333 msg = malloc(param, M_CTL, M_WAITOK); 1334 else 1335 msg = &msgbuf; 1336 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1337 M_WAITOK); 1338 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1339 printf("%s: Error receiving message: %d\n", 1340 __func__, isc_status); 1341 if (msg != &msgbuf) 1342 free(msg, M_CTL); 1343 return; 1344 } 1345 1346 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1347 switch (msg->hdr.msg_type) { 1348 case CTL_MSG_SERIALIZE: 1349 io = ctl_alloc_io(softc->othersc_pool); 1350 ctl_zero_io(io); 1351 // populate ctsio from msg 1352 io->io_hdr.io_type = CTL_IO_SCSI; 1353 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1354 io->io_hdr.original_sc = msg->hdr.original_sc; 1355 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1356 CTL_FLAG_IO_ACTIVE; 1357 /* 1358 * If we're in serialization-only mode, we don't 1359 * want to go through full done processing. Thus 1360 * the COPY flag. 1361 * 1362 * XXX KDM add another flag that is more specific. 1363 */ 1364 if (softc->ha_mode != CTL_HA_MODE_XFER) 1365 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1366 io->io_hdr.nexus = msg->hdr.nexus; 1367#if 0 1368 printf("port %u, iid %u, lun %u\n", 1369 io->io_hdr.nexus.targ_port, 1370 io->io_hdr.nexus.initid, 1371 io->io_hdr.nexus.targ_lun); 1372#endif 1373 io->scsiio.tag_num = msg->scsi.tag_num; 1374 io->scsiio.tag_type = msg->scsi.tag_type; 1375#ifdef CTL_TIME_IO 1376 io->io_hdr.start_time = time_uptime; 1377 getbinuptime(&io->io_hdr.start_bt); 1378#endif /* CTL_TIME_IO */ 1379 io->scsiio.cdb_len = msg->scsi.cdb_len; 1380 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1381 CTL_MAX_CDBLEN); 1382 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1383 const struct ctl_cmd_entry *entry; 1384 1385 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1386 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1387 io->io_hdr.flags |= 1388 entry->flags & CTL_FLAG_DATA_MASK; 1389 } 1390 ctl_enqueue_isc(io); 1391 break; 1392 1393 /* Performed on the Originating SC, XFER mode only */ 1394 case CTL_MSG_DATAMOVE: { 1395 struct ctl_sg_entry *sgl; 1396 int i, j; 1397 1398 io = msg->hdr.original_sc; 1399 if (io == NULL) { 1400 printf("%s: original_sc == NULL!\n", __func__); 1401 /* XXX KDM do something here */ 1402 break; 1403 } 1404 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1405 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1406 /* 1407 * Keep track of this, we need to send it back over 1408 * when the datamove is complete. 1409 */ 1410 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1411 if (msg->hdr.status == CTL_SUCCESS) 1412 io->io_hdr.status = msg->hdr.status; 1413 1414 if (msg->dt.sg_sequence == 0) { 1415#ifdef CTL_TIME_IO 1416 getbinuptime(&io->io_hdr.dma_start_bt); 1417#endif 1418 i = msg->dt.kern_sg_entries + 1419 msg->dt.kern_data_len / 1420 CTL_HA_DATAMOVE_SEGMENT + 1; 1421 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1422 M_WAITOK | M_ZERO); 1423 io->io_hdr.remote_sglist = sgl; 1424 io->io_hdr.local_sglist = 1425 &sgl[msg->dt.kern_sg_entries]; 1426 1427 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1428 1429 io->scsiio.kern_sg_entries = 1430 msg->dt.kern_sg_entries; 1431 io->scsiio.rem_sg_entries = 1432 msg->dt.kern_sg_entries; 1433 io->scsiio.kern_data_len = 1434 msg->dt.kern_data_len; 1435 io->scsiio.kern_total_len = 1436 msg->dt.kern_total_len; 1437 io->scsiio.kern_data_resid = 1438 msg->dt.kern_data_resid; 1439 io->scsiio.kern_rel_offset = 1440 msg->dt.kern_rel_offset; 1441 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1442 io->io_hdr.flags |= msg->dt.flags & 1443 CTL_FLAG_BUS_ADDR; 1444 } else 1445 sgl = (struct ctl_sg_entry *) 1446 io->scsiio.kern_data_ptr; 1447 1448 for (i = msg->dt.sent_sg_entries, j = 0; 1449 i < (msg->dt.sent_sg_entries + 1450 msg->dt.cur_sg_entries); i++, j++) { 1451 sgl[i].addr = msg->dt.sg_list[j].addr; 1452 sgl[i].len = msg->dt.sg_list[j].len; 1453 1454#if 0 1455 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1456 __func__, sgl[i].addr, sgl[i].len, j, i); 1457#endif 1458 } 1459 1460 /* 1461 * If this is the last piece of the I/O, we've got 1462 * the full S/G list. Queue processing in the thread. 1463 * Otherwise wait for the next piece. 1464 */ 1465 if (msg->dt.sg_last != 0) 1466 ctl_enqueue_isc(io); 1467 break; 1468 } 1469 /* Performed on the Serializing (primary) SC, XFER mode only */ 1470 case CTL_MSG_DATAMOVE_DONE: { 1471 if (msg->hdr.serializing_sc == NULL) { 1472 printf("%s: serializing_sc == NULL!\n", 1473 __func__); 1474 /* XXX KDM now what? */ 1475 break; 1476 } 1477 /* 1478 * We grab the sense information here in case 1479 * there was a failure, so we can return status 1480 * back to the initiator. 1481 */ 1482 io = msg->hdr.serializing_sc; 1483 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1484 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1485 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1486 io->io_hdr.port_status = msg->scsi.fetd_status; 1487 io->scsiio.residual = msg->scsi.residual; 1488 if (msg->hdr.status != CTL_STATUS_NONE) { 1489 io->io_hdr.status = msg->hdr.status; 1490 io->scsiio.scsi_status = msg->scsi.scsi_status; 1491 io->scsiio.sense_len = msg->scsi.sense_len; 1492 io->scsiio.sense_residual =msg->scsi.sense_residual; 1493 memcpy(&io->scsiio.sense_data, 1494 &msg->scsi.sense_data, 1495 msg->scsi.sense_len); 1496 if (msg->hdr.status == CTL_SUCCESS) 1497 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1498 } 1499 ctl_enqueue_isc(io); 1500 break; 1501 } 1502 1503 /* Preformed on Originating SC, SER_ONLY mode */ 1504 case CTL_MSG_R2R: 1505 io = msg->hdr.original_sc; 1506 if (io == NULL) { 1507 printf("%s: original_sc == NULL!\n", 1508 __func__); 1509 break; 1510 } 1511 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1512 io->io_hdr.msg_type = CTL_MSG_R2R; 1513 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1514 ctl_enqueue_isc(io); 1515 break; 1516 1517 /* 1518 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1519 * mode. 1520 * Performed on the Originating (i.e. secondary) SC in XFER 1521 * mode 1522 */ 1523 case CTL_MSG_FINISH_IO: 1524 if (softc->ha_mode == CTL_HA_MODE_XFER) 1525 ctl_isc_handler_finish_xfer(softc, msg); 1526 else 1527 ctl_isc_handler_finish_ser_only(softc, msg); 1528 break; 1529 1530 /* Preformed on Originating SC */ 1531 case CTL_MSG_BAD_JUJU: 1532 io = msg->hdr.original_sc; 1533 if (io == NULL) { 1534 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1535 __func__); 1536 break; 1537 } 1538 ctl_copy_sense_data(msg, io); 1539 /* 1540 * IO should have already been cleaned up on other 1541 * SC so clear this flag so we won't send a message 1542 * back to finish the IO there. 1543 */ 1544 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1545 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1546 1547 /* io = msg->hdr.serializing_sc; */ 1548 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1549 ctl_enqueue_isc(io); 1550 break; 1551 1552 /* Handle resets sent from the other side */ 1553 case CTL_MSG_MANAGE_TASKS: { 1554 struct ctl_taskio *taskio; 1555 taskio = (struct ctl_taskio *)ctl_alloc_io( 1556 softc->othersc_pool); 1557 ctl_zero_io((union ctl_io *)taskio); 1558 taskio->io_hdr.io_type = CTL_IO_TASK; 1559 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1560 taskio->io_hdr.nexus = msg->hdr.nexus; 1561 taskio->task_action = msg->task.task_action; 1562 taskio->tag_num = msg->task.tag_num; 1563 taskio->tag_type = msg->task.tag_type; 1564#ifdef CTL_TIME_IO 1565 taskio->io_hdr.start_time = time_uptime; 1566 getbinuptime(&taskio->io_hdr.start_bt); 1567#endif /* CTL_TIME_IO */ 1568 ctl_run_task((union ctl_io *)taskio); 1569 break; 1570 } 1571 /* Persistent Reserve action which needs attention */ 1572 case CTL_MSG_PERS_ACTION: 1573 presio = (struct ctl_prio *)ctl_alloc_io( 1574 softc->othersc_pool); 1575 ctl_zero_io((union ctl_io *)presio); 1576 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1577 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1578 presio->io_hdr.nexus = msg->hdr.nexus; 1579 presio->pr_msg = msg->pr; 1580 ctl_enqueue_isc((union ctl_io *)presio); 1581 break; 1582 case CTL_MSG_UA: 1583 ctl_isc_ua(softc, msg, param); 1584 break; 1585 case CTL_MSG_PORT_SYNC: 1586 ctl_isc_port_sync(softc, msg, param); 1587 break; 1588 case CTL_MSG_LUN_SYNC: 1589 ctl_isc_lun_sync(softc, msg, param); 1590 break; 1591 case CTL_MSG_IID_SYNC: 1592 ctl_isc_iid_sync(softc, msg, param); 1593 break; 1594 case CTL_MSG_LOGIN: 1595 ctl_isc_login(softc, msg, param); 1596 break; 1597 case CTL_MSG_MODE_SYNC: 1598 ctl_isc_mode_sync(softc, msg, param); 1599 break; 1600 default: 1601 printf("Received HA message of unknown type %d\n", 1602 msg->hdr.msg_type); 1603 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1604 break; 1605 } 1606 if (msg != &msgbuf) 1607 free(msg, M_CTL); 1608 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1609 printf("CTL: HA link status changed from %d to %d\n", 1610 softc->ha_link, param); 1611 if (param == softc->ha_link) 1612 return; 1613 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1614 softc->ha_link = param; 1615 ctl_isc_ha_link_down(softc); 1616 } else { 1617 softc->ha_link = param; 1618 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1619 ctl_isc_ha_link_up(softc); 1620 } 1621 return; 1622 } else { 1623 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1624 return; 1625 } 1626} 1627 1628static void 1629ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1630{ 1631 1632 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1633 src->scsi.sense_len); 1634 dest->scsiio.scsi_status = src->scsi.scsi_status; 1635 dest->scsiio.sense_len = src->scsi.sense_len; 1636 dest->io_hdr.status = src->hdr.status; 1637} 1638 1639static void 1640ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1641{ 1642 1643 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1644 src->scsiio.sense_len); 1645 dest->scsi.scsi_status = src->scsiio.scsi_status; 1646 dest->scsi.sense_len = src->scsiio.sense_len; 1647 dest->hdr.status = src->io_hdr.status; 1648} 1649 1650void 1651ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1652{ 1653 struct ctl_softc *softc = lun->ctl_softc; 1654 ctl_ua_type *pu; 1655 1656 if (initidx < softc->init_min || initidx >= softc->init_max) 1657 return; 1658 mtx_assert(&lun->lun_lock, MA_OWNED); 1659 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1660 if (pu == NULL) 1661 return; 1662 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1663} 1664 1665void 1666ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1667{ 1668 int i; 1669 1670 mtx_assert(&lun->lun_lock, MA_OWNED); 1671 if (lun->pending_ua[port] == NULL) 1672 return; 1673 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1674 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1675 continue; 1676 lun->pending_ua[port][i] |= ua; 1677 } 1678} 1679 1680void 1681ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1682{ 1683 struct ctl_softc *softc = lun->ctl_softc; 1684 int i; 1685 1686 mtx_assert(&lun->lun_lock, MA_OWNED); 1687 for (i = softc->port_min; i < softc->port_max; i++) 1688 ctl_est_ua_port(lun, i, except, ua); 1689} 1690 1691void 1692ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1693{ 1694 struct ctl_softc *softc = lun->ctl_softc; 1695 ctl_ua_type *pu; 1696 1697 if (initidx < softc->init_min || initidx >= softc->init_max) 1698 return; 1699 mtx_assert(&lun->lun_lock, MA_OWNED); 1700 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1701 if (pu == NULL) 1702 return; 1703 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1704} 1705 1706void 1707ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1708{ 1709 struct ctl_softc *softc = lun->ctl_softc; 1710 int i, j; 1711 1712 mtx_assert(&lun->lun_lock, MA_OWNED); 1713 for (i = softc->port_min; i < softc->port_max; i++) { 1714 if (lun->pending_ua[i] == NULL) 1715 continue; 1716 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1717 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1718 continue; 1719 lun->pending_ua[i][j] &= ~ua; 1720 } 1721 } 1722} 1723 1724void 1725ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1726 ctl_ua_type ua_type) 1727{ 1728 struct ctl_lun *lun; 1729 1730 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1731 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1732 mtx_lock(&lun->lun_lock); 1733 ctl_clr_ua(lun, initidx, ua_type); 1734 mtx_unlock(&lun->lun_lock); 1735 } 1736} 1737 1738static int 1739ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1740{ 1741 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1742 struct ctl_lun *lun; 1743 struct ctl_lun_req ireq; 1744 int error, value; 1745 1746 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1747 error = sysctl_handle_int(oidp, &value, 0, req); 1748 if ((error != 0) || (req->newptr == NULL)) 1749 return (error); 1750 1751 mtx_lock(&softc->ctl_lock); 1752 if (value == 0) 1753 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1754 else 1755 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1756 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1757 mtx_unlock(&softc->ctl_lock); 1758 bzero(&ireq, sizeof(ireq)); 1759 ireq.reqtype = CTL_LUNREQ_MODIFY; 1760 ireq.reqdata.modify.lun_id = lun->lun; 1761 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1762 curthread); 1763 if (ireq.status != CTL_LUN_OK) { 1764 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1765 __func__, ireq.status, ireq.error_str); 1766 } 1767 mtx_lock(&softc->ctl_lock); 1768 } 1769 mtx_unlock(&softc->ctl_lock); 1770 return (0); 1771} 1772 1773static int 1774ctl_init(void) 1775{ 1776 struct ctl_softc *softc; 1777 void *other_pool; 1778 int i, error; 1779 1780 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1781 M_WAITOK | M_ZERO); 1782 1783 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1784 "cam/ctl"); 1785 softc->dev->si_drv1 = softc; 1786 1787 sysctl_ctx_init(&softc->sysctl_ctx); 1788 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1789 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1790 CTLFLAG_RD, 0, "CAM Target Layer"); 1791 1792 if (softc->sysctl_tree == NULL) { 1793 printf("%s: unable to allocate sysctl tree\n", __func__); 1794 destroy_dev(softc->dev); 1795 free(control_softc, M_DEVBUF); 1796 control_softc = NULL; 1797 return (ENOMEM); 1798 } 1799 1800 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1801 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1802 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1803 softc->flags = 0; 1804 1805 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1806 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1807 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1808 1809 /* 1810 * In Copan's HA scheme, the "master" and "slave" roles are 1811 * figured out through the slot the controller is in. Although it 1812 * is an active/active system, someone has to be in charge. 1813 */ 1814 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1815 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1816 "HA head ID (0 - no HA)"); 1817 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1818 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1819 softc->is_single = 1; 1820 softc->port_cnt = CTL_MAX_PORTS; 1821 softc->port_min = 0; 1822 } else { 1823 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1824 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1825 } 1826 softc->port_max = softc->port_min + softc->port_cnt; 1827 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1828 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1829 1830 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1831 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1832 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1833 1834 STAILQ_INIT(&softc->lun_list); 1835 STAILQ_INIT(&softc->pending_lun_queue); 1836 STAILQ_INIT(&softc->fe_list); 1837 STAILQ_INIT(&softc->port_list); 1838 STAILQ_INIT(&softc->be_list); 1839 ctl_tpc_init(softc); 1840 1841 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1842 &other_pool) != 0) 1843 { 1844 printf("ctl: can't allocate %d entry other SC pool, " 1845 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1846 return (ENOMEM); 1847 } 1848 softc->othersc_pool = other_pool; 1849 1850 if (worker_threads <= 0) 1851 worker_threads = max(1, mp_ncpus / 4); 1852 if (worker_threads > CTL_MAX_THREADS) 1853 worker_threads = CTL_MAX_THREADS; 1854 1855 for (i = 0; i < worker_threads; i++) { 1856 struct ctl_thread *thr = &softc->threads[i]; 1857 1858 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1859 thr->ctl_softc = softc; 1860 STAILQ_INIT(&thr->incoming_queue); 1861 STAILQ_INIT(&thr->rtr_queue); 1862 STAILQ_INIT(&thr->done_queue); 1863 STAILQ_INIT(&thr->isc_queue); 1864 1865 error = kproc_kthread_add(ctl_work_thread, thr, 1866 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1867 if (error != 0) { 1868 printf("error creating CTL work thread!\n"); 1869 ctl_pool_free(other_pool); 1870 return (error); 1871 } 1872 } 1873 error = kproc_kthread_add(ctl_lun_thread, softc, 1874 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1875 if (error != 0) { 1876 printf("error creating CTL lun thread!\n"); 1877 ctl_pool_free(other_pool); 1878 return (error); 1879 } 1880 error = kproc_kthread_add(ctl_thresh_thread, softc, 1881 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1882 if (error != 0) { 1883 printf("error creating CTL threshold thread!\n"); 1884 ctl_pool_free(other_pool); 1885 return (error); 1886 } 1887 1888 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1889 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1890 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1891 1892 if (softc->is_single == 0) { 1893 ctl_frontend_register(&ha_frontend); 1894 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1895 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1896 softc->is_single = 1; 1897 } else 1898 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1899 != CTL_HA_STATUS_SUCCESS) { 1900 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1901 softc->is_single = 1; 1902 } 1903 } 1904 return (0); 1905} 1906 1907void 1908ctl_shutdown(void) 1909{ 1910 struct ctl_softc *softc = control_softc; 1911 struct ctl_lun *lun, *next_lun; 1912 1913 if (softc->is_single == 0) { 1914 ctl_ha_msg_shutdown(softc); 1915 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1916 != CTL_HA_STATUS_SUCCESS) 1917 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1918 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1919 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1920 ctl_frontend_deregister(&ha_frontend); 1921 } 1922 1923 mtx_lock(&softc->ctl_lock); 1924 1925 /* 1926 * Free up each LUN. 1927 */ 1928 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1929 next_lun = STAILQ_NEXT(lun, links); 1930 ctl_free_lun(lun); 1931 } 1932 1933 mtx_unlock(&softc->ctl_lock); 1934 1935#if 0 1936 ctl_shutdown_thread(softc->work_thread); 1937 mtx_destroy(&softc->queue_lock); 1938#endif 1939 1940 ctl_tpc_shutdown(softc); 1941 uma_zdestroy(softc->io_zone); 1942 mtx_destroy(&softc->ctl_lock); 1943 1944 destroy_dev(softc->dev); 1945 1946 sysctl_ctx_free(&softc->sysctl_ctx); 1947 1948 free(control_softc, M_DEVBUF); 1949 control_softc = NULL; 1950} 1951 1952static int 1953ctl_module_event_handler(module_t mod, int what, void *arg) 1954{ 1955 1956 switch (what) { 1957 case MOD_LOAD: 1958 return (ctl_init()); 1959 case MOD_UNLOAD: 1960 return (EBUSY); 1961 default: 1962 return (EOPNOTSUPP); 1963 } 1964} 1965 1966/* 1967 * XXX KDM should we do some access checks here? Bump a reference count to 1968 * prevent a CTL module from being unloaded while someone has it open? 1969 */ 1970static int 1971ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1972{ 1973 return (0); 1974} 1975 1976static int 1977ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1978{ 1979 return (0); 1980} 1981 1982/* 1983 * Remove an initiator by port number and initiator ID. 1984 * Returns 0 for success, -1 for failure. 1985 */ 1986int 1987ctl_remove_initiator(struct ctl_port *port, int iid) 1988{ 1989 struct ctl_softc *softc = port->ctl_softc; 1990 1991 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1992 1993 if (iid > CTL_MAX_INIT_PER_PORT) { 1994 printf("%s: initiator ID %u > maximun %u!\n", 1995 __func__, iid, CTL_MAX_INIT_PER_PORT); 1996 return (-1); 1997 } 1998 1999 mtx_lock(&softc->ctl_lock); 2000 port->wwpn_iid[iid].in_use--; 2001 port->wwpn_iid[iid].last_use = time_uptime; 2002 mtx_unlock(&softc->ctl_lock); 2003 ctl_isc_announce_iid(port, iid); 2004 2005 return (0); 2006} 2007 2008/* 2009 * Add an initiator to the initiator map. 2010 * Returns iid for success, < 0 for failure. 2011 */ 2012int 2013ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2014{ 2015 struct ctl_softc *softc = port->ctl_softc; 2016 time_t best_time; 2017 int i, best; 2018 2019 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2020 2021 if (iid >= CTL_MAX_INIT_PER_PORT) { 2022 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2023 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2024 free(name, M_CTL); 2025 return (-1); 2026 } 2027 2028 mtx_lock(&softc->ctl_lock); 2029 2030 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2031 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2032 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2033 iid = i; 2034 break; 2035 } 2036 if (name != NULL && port->wwpn_iid[i].name != NULL && 2037 strcmp(name, port->wwpn_iid[i].name) == 0) { 2038 iid = i; 2039 break; 2040 } 2041 } 2042 } 2043 2044 if (iid < 0) { 2045 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2046 if (port->wwpn_iid[i].in_use == 0 && 2047 port->wwpn_iid[i].wwpn == 0 && 2048 port->wwpn_iid[i].name == NULL) { 2049 iid = i; 2050 break; 2051 } 2052 } 2053 } 2054 2055 if (iid < 0) { 2056 best = -1; 2057 best_time = INT32_MAX; 2058 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2059 if (port->wwpn_iid[i].in_use == 0) { 2060 if (port->wwpn_iid[i].last_use < best_time) { 2061 best = i; 2062 best_time = port->wwpn_iid[i].last_use; 2063 } 2064 } 2065 } 2066 iid = best; 2067 } 2068 2069 if (iid < 0) { 2070 mtx_unlock(&softc->ctl_lock); 2071 free(name, M_CTL); 2072 return (-2); 2073 } 2074 2075 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2076 /* 2077 * This is not an error yet. 2078 */ 2079 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2080#if 0 2081 printf("%s: port %d iid %u WWPN %#jx arrived" 2082 " again\n", __func__, port->targ_port, 2083 iid, (uintmax_t)wwpn); 2084#endif 2085 goto take; 2086 } 2087 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2088 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2089#if 0 2090 printf("%s: port %d iid %u name '%s' arrived" 2091 " again\n", __func__, port->targ_port, 2092 iid, name); 2093#endif 2094 goto take; 2095 } 2096 2097 /* 2098 * This is an error, but what do we do about it? The 2099 * driver is telling us we have a new WWPN for this 2100 * initiator ID, so we pretty much need to use it. 2101 */ 2102 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2103 " but WWPN %#jx '%s' is still at that address\n", 2104 __func__, port->targ_port, iid, wwpn, name, 2105 (uintmax_t)port->wwpn_iid[iid].wwpn, 2106 port->wwpn_iid[iid].name); 2107 2108 /* 2109 * XXX KDM clear have_ca and ua_pending on each LUN for 2110 * this initiator. 2111 */ 2112 } 2113take: 2114 free(port->wwpn_iid[iid].name, M_CTL); 2115 port->wwpn_iid[iid].name = name; 2116 port->wwpn_iid[iid].wwpn = wwpn; 2117 port->wwpn_iid[iid].in_use++; 2118 mtx_unlock(&softc->ctl_lock); 2119 ctl_isc_announce_iid(port, iid); 2120 2121 return (iid); 2122} 2123 2124static int 2125ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2126{ 2127 int len; 2128 2129 switch (port->port_type) { 2130 case CTL_PORT_FC: 2131 { 2132 struct scsi_transportid_fcp *id = 2133 (struct scsi_transportid_fcp *)buf; 2134 if (port->wwpn_iid[iid].wwpn == 0) 2135 return (0); 2136 memset(id, 0, sizeof(*id)); 2137 id->format_protocol = SCSI_PROTO_FC; 2138 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2139 return (sizeof(*id)); 2140 } 2141 case CTL_PORT_ISCSI: 2142 { 2143 struct scsi_transportid_iscsi_port *id = 2144 (struct scsi_transportid_iscsi_port *)buf; 2145 if (port->wwpn_iid[iid].name == NULL) 2146 return (0); 2147 memset(id, 0, 256); 2148 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2149 SCSI_PROTO_ISCSI; 2150 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2151 len = roundup2(min(len, 252), 4); 2152 scsi_ulto2b(len, id->additional_length); 2153 return (sizeof(*id) + len); 2154 } 2155 case CTL_PORT_SAS: 2156 { 2157 struct scsi_transportid_sas *id = 2158 (struct scsi_transportid_sas *)buf; 2159 if (port->wwpn_iid[iid].wwpn == 0) 2160 return (0); 2161 memset(id, 0, sizeof(*id)); 2162 id->format_protocol = SCSI_PROTO_SAS; 2163 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2164 return (sizeof(*id)); 2165 } 2166 default: 2167 { 2168 struct scsi_transportid_spi *id = 2169 (struct scsi_transportid_spi *)buf; 2170 memset(id, 0, sizeof(*id)); 2171 id->format_protocol = SCSI_PROTO_SPI; 2172 scsi_ulto2b(iid, id->scsi_addr); 2173 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2174 return (sizeof(*id)); 2175 } 2176 } 2177} 2178 2179/* 2180 * Serialize a command that went down the "wrong" side, and so was sent to 2181 * this controller for execution. The logic is a little different than the 2182 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2183 * sent back to the other side, but in the success case, we execute the 2184 * command on this side (XFER mode) or tell the other side to execute it 2185 * (SER_ONLY mode). 2186 */ 2187static int 2188ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2189{ 2190 struct ctl_softc *softc = control_softc; 2191 union ctl_ha_msg msg_info; 2192 struct ctl_port *port; 2193 struct ctl_lun *lun; 2194 const struct ctl_cmd_entry *entry; 2195 int retval = 0; 2196 uint32_t targ_lun; 2197 2198 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2199 mtx_lock(&softc->ctl_lock); 2200 2201 /* Make sure that we know about this port. */ 2202 port = ctl_io_port(&ctsio->io_hdr); 2203 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2204 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2205 /*retry_count*/ 1); 2206 goto badjuju; 2207 } 2208 2209 /* Make sure that we know about this LUN. */ 2210 if ((targ_lun < CTL_MAX_LUNS) && 2211 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 2212 mtx_lock(&lun->lun_lock); 2213 mtx_unlock(&softc->ctl_lock); 2214 /* 2215 * If the LUN is invalid, pretend that it doesn't exist. 2216 * It will go away as soon as all pending I/O has been 2217 * completed. 2218 */ 2219 if (lun->flags & CTL_LUN_DISABLED) { 2220 mtx_unlock(&lun->lun_lock); 2221 lun = NULL; 2222 } 2223 } else { 2224 mtx_unlock(&softc->ctl_lock); 2225 lun = NULL; 2226 } 2227 if (lun == NULL) { 2228 /* 2229 * The other node would not send this request to us unless 2230 * received announce that we are primary node for this LUN. 2231 * If this LUN does not exist now, it is probably result of 2232 * a race, so respond to initiator in the most opaque way. 2233 */ 2234 ctl_set_busy(ctsio); 2235 goto badjuju; 2236 } 2237 2238 entry = ctl_get_cmd_entry(ctsio, NULL); 2239 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2240 mtx_unlock(&lun->lun_lock); 2241 goto badjuju; 2242 } 2243 2244 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2245 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2246 2247 /* 2248 * Every I/O goes into the OOA queue for a 2249 * particular LUN, and stays there until completion. 2250 */ 2251#ifdef CTL_TIME_IO 2252 if (TAILQ_EMPTY(&lun->ooa_queue)) 2253 lun->idle_time += getsbinuptime() - lun->last_busy; 2254#endif 2255 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2256 2257 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2258 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2259 ooa_links))) { 2260 case CTL_ACTION_BLOCK: 2261 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2262 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2263 blocked_links); 2264 mtx_unlock(&lun->lun_lock); 2265 break; 2266 case CTL_ACTION_PASS: 2267 case CTL_ACTION_SKIP: 2268 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2269 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2270 ctl_enqueue_rtr((union ctl_io *)ctsio); 2271 mtx_unlock(&lun->lun_lock); 2272 } else { 2273 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2274 mtx_unlock(&lun->lun_lock); 2275 2276 /* send msg back to other side */ 2277 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2278 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2279 msg_info.hdr.msg_type = CTL_MSG_R2R; 2280 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2281 sizeof(msg_info.hdr), M_WAITOK); 2282 } 2283 break; 2284 case CTL_ACTION_OVERLAP: 2285 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2286 mtx_unlock(&lun->lun_lock); 2287 ctl_set_overlapped_cmd(ctsio); 2288 goto badjuju; 2289 case CTL_ACTION_OVERLAP_TAG: 2290 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2291 mtx_unlock(&lun->lun_lock); 2292 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2293 goto badjuju; 2294 case CTL_ACTION_ERROR: 2295 default: 2296 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2297 mtx_unlock(&lun->lun_lock); 2298 2299 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2300 /*retry_count*/ 0); 2301badjuju: 2302 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2303 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2304 msg_info.hdr.serializing_sc = NULL; 2305 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2306 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2307 sizeof(msg_info.scsi), M_WAITOK); 2308 retval = 1; 2309 break; 2310 } 2311 return (retval); 2312} 2313 2314/* 2315 * Returns 0 for success, errno for failure. 2316 */ 2317static void 2318ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2319 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2320{ 2321 union ctl_io *io; 2322 2323 mtx_lock(&lun->lun_lock); 2324 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2325 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2326 ooa_links)) { 2327 struct ctl_ooa_entry *entry; 2328 2329 /* 2330 * If we've got more than we can fit, just count the 2331 * remaining entries. 2332 */ 2333 if (*cur_fill_num >= ooa_hdr->alloc_num) 2334 continue; 2335 2336 entry = &kern_entries[*cur_fill_num]; 2337 2338 entry->tag_num = io->scsiio.tag_num; 2339 entry->lun_num = lun->lun; 2340#ifdef CTL_TIME_IO 2341 entry->start_bt = io->io_hdr.start_bt; 2342#endif 2343 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2344 entry->cdb_len = io->scsiio.cdb_len; 2345 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2346 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2347 2348 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2349 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2350 2351 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2352 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2353 2354 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2355 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2356 2357 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2358 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2359 } 2360 mtx_unlock(&lun->lun_lock); 2361} 2362 2363static void * 2364ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2365 size_t error_str_len) 2366{ 2367 void *kptr; 2368 2369 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2370 2371 if (copyin(user_addr, kptr, len) != 0) { 2372 snprintf(error_str, error_str_len, "Error copying %d bytes " 2373 "from user address %p to kernel address %p", len, 2374 user_addr, kptr); 2375 free(kptr, M_CTL); 2376 return (NULL); 2377 } 2378 2379 return (kptr); 2380} 2381 2382static void 2383ctl_free_args(int num_args, struct ctl_be_arg *args) 2384{ 2385 int i; 2386 2387 if (args == NULL) 2388 return; 2389 2390 for (i = 0; i < num_args; i++) { 2391 free(args[i].kname, M_CTL); 2392 free(args[i].kvalue, M_CTL); 2393 } 2394 2395 free(args, M_CTL); 2396} 2397 2398static struct ctl_be_arg * 2399ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2400 char *error_str, size_t error_str_len) 2401{ 2402 struct ctl_be_arg *args; 2403 int i; 2404 2405 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2406 error_str, error_str_len); 2407 2408 if (args == NULL) 2409 goto bailout; 2410 2411 for (i = 0; i < num_args; i++) { 2412 args[i].kname = NULL; 2413 args[i].kvalue = NULL; 2414 } 2415 2416 for (i = 0; i < num_args; i++) { 2417 uint8_t *tmpptr; 2418 2419 args[i].kname = ctl_copyin_alloc(args[i].name, 2420 args[i].namelen, error_str, error_str_len); 2421 if (args[i].kname == NULL) 2422 goto bailout; 2423 2424 if (args[i].kname[args[i].namelen - 1] != '\0') { 2425 snprintf(error_str, error_str_len, "Argument %d " 2426 "name is not NUL-terminated", i); 2427 goto bailout; 2428 } 2429 2430 if (args[i].flags & CTL_BEARG_RD) { 2431 tmpptr = ctl_copyin_alloc(args[i].value, 2432 args[i].vallen, error_str, error_str_len); 2433 if (tmpptr == NULL) 2434 goto bailout; 2435 if ((args[i].flags & CTL_BEARG_ASCII) 2436 && (tmpptr[args[i].vallen - 1] != '\0')) { 2437 snprintf(error_str, error_str_len, "Argument " 2438 "%d value is not NUL-terminated", i); 2439 goto bailout; 2440 } 2441 args[i].kvalue = tmpptr; 2442 } else { 2443 args[i].kvalue = malloc(args[i].vallen, 2444 M_CTL, M_WAITOK | M_ZERO); 2445 } 2446 } 2447 2448 return (args); 2449bailout: 2450 2451 ctl_free_args(num_args, args); 2452 2453 return (NULL); 2454} 2455 2456static void 2457ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2458{ 2459 int i; 2460 2461 for (i = 0; i < num_args; i++) { 2462 if (args[i].flags & CTL_BEARG_WR) 2463 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2464 } 2465} 2466 2467/* 2468 * Escape characters that are illegal or not recommended in XML. 2469 */ 2470int 2471ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2472{ 2473 char *end = str + size; 2474 int retval; 2475 2476 retval = 0; 2477 2478 for (; *str && str < end; str++) { 2479 switch (*str) { 2480 case '&': 2481 retval = sbuf_printf(sb, "&"); 2482 break; 2483 case '>': 2484 retval = sbuf_printf(sb, ">"); 2485 break; 2486 case '<': 2487 retval = sbuf_printf(sb, "<"); 2488 break; 2489 default: 2490 retval = sbuf_putc(sb, *str); 2491 break; 2492 } 2493 2494 if (retval != 0) 2495 break; 2496 2497 } 2498 2499 return (retval); 2500} 2501 2502static void 2503ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2504{ 2505 struct scsi_vpd_id_descriptor *desc; 2506 int i; 2507 2508 if (id == NULL || id->len < 4) 2509 return; 2510 desc = (struct scsi_vpd_id_descriptor *)id->data; 2511 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2512 case SVPD_ID_TYPE_T10: 2513 sbuf_printf(sb, "t10."); 2514 break; 2515 case SVPD_ID_TYPE_EUI64: 2516 sbuf_printf(sb, "eui."); 2517 break; 2518 case SVPD_ID_TYPE_NAA: 2519 sbuf_printf(sb, "naa."); 2520 break; 2521 case SVPD_ID_TYPE_SCSI_NAME: 2522 break; 2523 } 2524 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2525 case SVPD_ID_CODESET_BINARY: 2526 for (i = 0; i < desc->length; i++) 2527 sbuf_printf(sb, "%02x", desc->identifier[i]); 2528 break; 2529 case SVPD_ID_CODESET_ASCII: 2530 sbuf_printf(sb, "%.*s", (int)desc->length, 2531 (char *)desc->identifier); 2532 break; 2533 case SVPD_ID_CODESET_UTF8: 2534 sbuf_printf(sb, "%s", (char *)desc->identifier); 2535 break; 2536 } 2537} 2538 2539static int 2540ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2541 struct thread *td) 2542{ 2543 struct ctl_softc *softc = dev->si_drv1; 2544 struct ctl_lun *lun; 2545 int retval; 2546 2547 retval = 0; 2548 2549 switch (cmd) { 2550 case CTL_IO: 2551 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2552 break; 2553 case CTL_ENABLE_PORT: 2554 case CTL_DISABLE_PORT: 2555 case CTL_SET_PORT_WWNS: { 2556 struct ctl_port *port; 2557 struct ctl_port_entry *entry; 2558 2559 entry = (struct ctl_port_entry *)addr; 2560 2561 mtx_lock(&softc->ctl_lock); 2562 STAILQ_FOREACH(port, &softc->port_list, links) { 2563 int action, done; 2564 2565 if (port->targ_port < softc->port_min || 2566 port->targ_port >= softc->port_max) 2567 continue; 2568 2569 action = 0; 2570 done = 0; 2571 if ((entry->port_type == CTL_PORT_NONE) 2572 && (entry->targ_port == port->targ_port)) { 2573 /* 2574 * If the user only wants to enable or 2575 * disable or set WWNs on a specific port, 2576 * do the operation and we're done. 2577 */ 2578 action = 1; 2579 done = 1; 2580 } else if (entry->port_type & port->port_type) { 2581 /* 2582 * Compare the user's type mask with the 2583 * particular frontend type to see if we 2584 * have a match. 2585 */ 2586 action = 1; 2587 done = 0; 2588 2589 /* 2590 * Make sure the user isn't trying to set 2591 * WWNs on multiple ports at the same time. 2592 */ 2593 if (cmd == CTL_SET_PORT_WWNS) { 2594 printf("%s: Can't set WWNs on " 2595 "multiple ports\n", __func__); 2596 retval = EINVAL; 2597 break; 2598 } 2599 } 2600 if (action == 0) 2601 continue; 2602 2603 /* 2604 * XXX KDM we have to drop the lock here, because 2605 * the online/offline operations can potentially 2606 * block. We need to reference count the frontends 2607 * so they can't go away, 2608 */ 2609 if (cmd == CTL_ENABLE_PORT) { 2610 mtx_unlock(&softc->ctl_lock); 2611 ctl_port_online(port); 2612 mtx_lock(&softc->ctl_lock); 2613 } else if (cmd == CTL_DISABLE_PORT) { 2614 mtx_unlock(&softc->ctl_lock); 2615 ctl_port_offline(port); 2616 mtx_lock(&softc->ctl_lock); 2617 } else if (cmd == CTL_SET_PORT_WWNS) { 2618 ctl_port_set_wwns(port, 2619 (entry->flags & CTL_PORT_WWNN_VALID) ? 2620 1 : 0, entry->wwnn, 2621 (entry->flags & CTL_PORT_WWPN_VALID) ? 2622 1 : 0, entry->wwpn); 2623 } 2624 if (done != 0) 2625 break; 2626 } 2627 mtx_unlock(&softc->ctl_lock); 2628 break; 2629 } 2630 case CTL_GET_OOA: { 2631 struct ctl_ooa *ooa_hdr; 2632 struct ctl_ooa_entry *entries; 2633 uint32_t cur_fill_num; 2634 2635 ooa_hdr = (struct ctl_ooa *)addr; 2636 2637 if ((ooa_hdr->alloc_len == 0) 2638 || (ooa_hdr->alloc_num == 0)) { 2639 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2640 "must be non-zero\n", __func__, 2641 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2642 retval = EINVAL; 2643 break; 2644 } 2645 2646 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2647 sizeof(struct ctl_ooa_entry))) { 2648 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2649 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2650 __func__, ooa_hdr->alloc_len, 2651 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2652 retval = EINVAL; 2653 break; 2654 } 2655 2656 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2657 if (entries == NULL) { 2658 printf("%s: could not allocate %d bytes for OOA " 2659 "dump\n", __func__, ooa_hdr->alloc_len); 2660 retval = ENOMEM; 2661 break; 2662 } 2663 2664 mtx_lock(&softc->ctl_lock); 2665 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2666 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2667 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2668 mtx_unlock(&softc->ctl_lock); 2669 free(entries, M_CTL); 2670 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2671 __func__, (uintmax_t)ooa_hdr->lun_num); 2672 retval = EINVAL; 2673 break; 2674 } 2675 2676 cur_fill_num = 0; 2677 2678 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2679 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2680 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2681 ooa_hdr, entries); 2682 } 2683 } else { 2684 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2685 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2686 entries); 2687 } 2688 mtx_unlock(&softc->ctl_lock); 2689 2690 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2691 ooa_hdr->fill_len = ooa_hdr->fill_num * 2692 sizeof(struct ctl_ooa_entry); 2693 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2694 if (retval != 0) { 2695 printf("%s: error copying out %d bytes for OOA dump\n", 2696 __func__, ooa_hdr->fill_len); 2697 } 2698 2699 getbinuptime(&ooa_hdr->cur_bt); 2700 2701 if (cur_fill_num > ooa_hdr->alloc_num) { 2702 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2703 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2704 } else { 2705 ooa_hdr->dropped_num = 0; 2706 ooa_hdr->status = CTL_OOA_OK; 2707 } 2708 2709 free(entries, M_CTL); 2710 break; 2711 } 2712 case CTL_DELAY_IO: { 2713 struct ctl_io_delay_info *delay_info; 2714 2715 delay_info = (struct ctl_io_delay_info *)addr; 2716 2717#ifdef CTL_IO_DELAY 2718 mtx_lock(&softc->ctl_lock); 2719 2720 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2721 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2722 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2723 } else { 2724 lun = softc->ctl_luns[delay_info->lun_id]; 2725 mtx_lock(&lun->lun_lock); 2726 2727 delay_info->status = CTL_DELAY_STATUS_OK; 2728 2729 switch (delay_info->delay_type) { 2730 case CTL_DELAY_TYPE_CONT: 2731 break; 2732 case CTL_DELAY_TYPE_ONESHOT: 2733 break; 2734 default: 2735 delay_info->status = 2736 CTL_DELAY_STATUS_INVALID_TYPE; 2737 break; 2738 } 2739 2740 switch (delay_info->delay_loc) { 2741 case CTL_DELAY_LOC_DATAMOVE: 2742 lun->delay_info.datamove_type = 2743 delay_info->delay_type; 2744 lun->delay_info.datamove_delay = 2745 delay_info->delay_secs; 2746 break; 2747 case CTL_DELAY_LOC_DONE: 2748 lun->delay_info.done_type = 2749 delay_info->delay_type; 2750 lun->delay_info.done_delay = 2751 delay_info->delay_secs; 2752 break; 2753 default: 2754 delay_info->status = 2755 CTL_DELAY_STATUS_INVALID_LOC; 2756 break; 2757 } 2758 mtx_unlock(&lun->lun_lock); 2759 } 2760 2761 mtx_unlock(&softc->ctl_lock); 2762#else 2763 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2764#endif /* CTL_IO_DELAY */ 2765 break; 2766 } 2767 case CTL_GETSTATS: { 2768 struct ctl_stats *stats; 2769 int i; 2770 2771 stats = (struct ctl_stats *)addr; 2772 2773 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2774 stats->alloc_len) { 2775 stats->status = CTL_SS_NEED_MORE_SPACE; 2776 stats->num_luns = softc->num_luns; 2777 break; 2778 } 2779 /* 2780 * XXX KDM no locking here. If the LUN list changes, 2781 * things can blow up. 2782 */ 2783 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2784 i++, lun = STAILQ_NEXT(lun, links)) { 2785 retval = copyout(&lun->stats, &stats->lun_stats[i], 2786 sizeof(lun->stats)); 2787 if (retval != 0) 2788 break; 2789 } 2790 stats->num_luns = softc->num_luns; 2791 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2792 softc->num_luns; 2793 stats->status = CTL_SS_OK; 2794#ifdef CTL_TIME_IO 2795 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2796#else 2797 stats->flags = CTL_STATS_FLAG_NONE; 2798#endif 2799 getnanouptime(&stats->timestamp); 2800 break; 2801 } 2802 case CTL_ERROR_INJECT: { 2803 struct ctl_error_desc *err_desc, *new_err_desc; 2804 2805 err_desc = (struct ctl_error_desc *)addr; 2806 2807 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2808 M_WAITOK | M_ZERO); 2809 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2810 2811 mtx_lock(&softc->ctl_lock); 2812 lun = softc->ctl_luns[err_desc->lun_id]; 2813 if (lun == NULL) { 2814 mtx_unlock(&softc->ctl_lock); 2815 free(new_err_desc, M_CTL); 2816 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2817 __func__, (uintmax_t)err_desc->lun_id); 2818 retval = EINVAL; 2819 break; 2820 } 2821 mtx_lock(&lun->lun_lock); 2822 mtx_unlock(&softc->ctl_lock); 2823 2824 /* 2825 * We could do some checking here to verify the validity 2826 * of the request, but given the complexity of error 2827 * injection requests, the checking logic would be fairly 2828 * complex. 2829 * 2830 * For now, if the request is invalid, it just won't get 2831 * executed and might get deleted. 2832 */ 2833 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2834 2835 /* 2836 * XXX KDM check to make sure the serial number is unique, 2837 * in case we somehow manage to wrap. That shouldn't 2838 * happen for a very long time, but it's the right thing to 2839 * do. 2840 */ 2841 new_err_desc->serial = lun->error_serial; 2842 err_desc->serial = lun->error_serial; 2843 lun->error_serial++; 2844 2845 mtx_unlock(&lun->lun_lock); 2846 break; 2847 } 2848 case CTL_ERROR_INJECT_DELETE: { 2849 struct ctl_error_desc *delete_desc, *desc, *desc2; 2850 int delete_done; 2851 2852 delete_desc = (struct ctl_error_desc *)addr; 2853 delete_done = 0; 2854 2855 mtx_lock(&softc->ctl_lock); 2856 lun = softc->ctl_luns[delete_desc->lun_id]; 2857 if (lun == NULL) { 2858 mtx_unlock(&softc->ctl_lock); 2859 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2860 __func__, (uintmax_t)delete_desc->lun_id); 2861 retval = EINVAL; 2862 break; 2863 } 2864 mtx_lock(&lun->lun_lock); 2865 mtx_unlock(&softc->ctl_lock); 2866 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2867 if (desc->serial != delete_desc->serial) 2868 continue; 2869 2870 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2871 links); 2872 free(desc, M_CTL); 2873 delete_done = 1; 2874 } 2875 mtx_unlock(&lun->lun_lock); 2876 if (delete_done == 0) { 2877 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2878 "error serial %ju on LUN %u\n", __func__, 2879 delete_desc->serial, delete_desc->lun_id); 2880 retval = EINVAL; 2881 break; 2882 } 2883 break; 2884 } 2885 case CTL_DUMP_STRUCTS: { 2886 int i, j, k; 2887 struct ctl_port *port; 2888 struct ctl_frontend *fe; 2889 2890 mtx_lock(&softc->ctl_lock); 2891 printf("CTL Persistent Reservation information start:\n"); 2892 for (i = 0; i < CTL_MAX_LUNS; i++) { 2893 lun = softc->ctl_luns[i]; 2894 2895 if ((lun == NULL) 2896 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2897 continue; 2898 2899 for (j = 0; j < CTL_MAX_PORTS; j++) { 2900 if (lun->pr_keys[j] == NULL) 2901 continue; 2902 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2903 if (lun->pr_keys[j][k] == 0) 2904 continue; 2905 printf(" LUN %d port %d iid %d key " 2906 "%#jx\n", i, j, k, 2907 (uintmax_t)lun->pr_keys[j][k]); 2908 } 2909 } 2910 } 2911 printf("CTL Persistent Reservation information end\n"); 2912 printf("CTL Ports:\n"); 2913 STAILQ_FOREACH(port, &softc->port_list, links) { 2914 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2915 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2916 port->frontend->name, port->port_type, 2917 port->physical_port, port->virtual_port, 2918 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2919 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2920 if (port->wwpn_iid[j].in_use == 0 && 2921 port->wwpn_iid[j].wwpn == 0 && 2922 port->wwpn_iid[j].name == NULL) 2923 continue; 2924 2925 printf(" iid %u use %d WWPN %#jx '%s'\n", 2926 j, port->wwpn_iid[j].in_use, 2927 (uintmax_t)port->wwpn_iid[j].wwpn, 2928 port->wwpn_iid[j].name); 2929 } 2930 } 2931 printf("CTL Port information end\n"); 2932 mtx_unlock(&softc->ctl_lock); 2933 /* 2934 * XXX KDM calling this without a lock. We'd likely want 2935 * to drop the lock before calling the frontend's dump 2936 * routine anyway. 2937 */ 2938 printf("CTL Frontends:\n"); 2939 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2940 printf(" Frontend '%s'\n", fe->name); 2941 if (fe->fe_dump != NULL) 2942 fe->fe_dump(); 2943 } 2944 printf("CTL Frontend information end\n"); 2945 break; 2946 } 2947 case CTL_LUN_REQ: { 2948 struct ctl_lun_req *lun_req; 2949 struct ctl_backend_driver *backend; 2950 2951 lun_req = (struct ctl_lun_req *)addr; 2952 2953 backend = ctl_backend_find(lun_req->backend); 2954 if (backend == NULL) { 2955 lun_req->status = CTL_LUN_ERROR; 2956 snprintf(lun_req->error_str, 2957 sizeof(lun_req->error_str), 2958 "Backend \"%s\" not found.", 2959 lun_req->backend); 2960 break; 2961 } 2962 if (lun_req->num_be_args > 0) { 2963 lun_req->kern_be_args = ctl_copyin_args( 2964 lun_req->num_be_args, 2965 lun_req->be_args, 2966 lun_req->error_str, 2967 sizeof(lun_req->error_str)); 2968 if (lun_req->kern_be_args == NULL) { 2969 lun_req->status = CTL_LUN_ERROR; 2970 break; 2971 } 2972 } 2973 2974 retval = backend->ioctl(dev, cmd, addr, flag, td); 2975 2976 if (lun_req->num_be_args > 0) { 2977 ctl_copyout_args(lun_req->num_be_args, 2978 lun_req->kern_be_args); 2979 ctl_free_args(lun_req->num_be_args, 2980 lun_req->kern_be_args); 2981 } 2982 break; 2983 } 2984 case CTL_LUN_LIST: { 2985 struct sbuf *sb; 2986 struct ctl_lun_list *list; 2987 struct ctl_option *opt; 2988 2989 list = (struct ctl_lun_list *)addr; 2990 2991 /* 2992 * Allocate a fixed length sbuf here, based on the length 2993 * of the user's buffer. We could allocate an auto-extending 2994 * buffer, and then tell the user how much larger our 2995 * amount of data is than his buffer, but that presents 2996 * some problems: 2997 * 2998 * 1. The sbuf(9) routines use a blocking malloc, and so 2999 * we can't hold a lock while calling them with an 3000 * auto-extending buffer. 3001 * 3002 * 2. There is not currently a LUN reference counting 3003 * mechanism, outside of outstanding transactions on 3004 * the LUN's OOA queue. So a LUN could go away on us 3005 * while we're getting the LUN number, backend-specific 3006 * information, etc. Thus, given the way things 3007 * currently work, we need to hold the CTL lock while 3008 * grabbing LUN information. 3009 * 3010 * So, from the user's standpoint, the best thing to do is 3011 * allocate what he thinks is a reasonable buffer length, 3012 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3013 * double the buffer length and try again. (And repeat 3014 * that until he succeeds.) 3015 */ 3016 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3017 if (sb == NULL) { 3018 list->status = CTL_LUN_LIST_ERROR; 3019 snprintf(list->error_str, sizeof(list->error_str), 3020 "Unable to allocate %d bytes for LUN list", 3021 list->alloc_len); 3022 break; 3023 } 3024 3025 sbuf_printf(sb, "<ctllunlist>\n"); 3026 3027 mtx_lock(&softc->ctl_lock); 3028 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3029 mtx_lock(&lun->lun_lock); 3030 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3031 (uintmax_t)lun->lun); 3032 3033 /* 3034 * Bail out as soon as we see that we've overfilled 3035 * the buffer. 3036 */ 3037 if (retval != 0) 3038 break; 3039 3040 retval = sbuf_printf(sb, "\t<backend_type>%s" 3041 "</backend_type>\n", 3042 (lun->backend == NULL) ? "none" : 3043 lun->backend->name); 3044 3045 if (retval != 0) 3046 break; 3047 3048 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3049 lun->be_lun->lun_type); 3050 3051 if (retval != 0) 3052 break; 3053 3054 if (lun->backend == NULL) { 3055 retval = sbuf_printf(sb, "</lun>\n"); 3056 if (retval != 0) 3057 break; 3058 continue; 3059 } 3060 3061 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3062 (lun->be_lun->maxlba > 0) ? 3063 lun->be_lun->maxlba + 1 : 0); 3064 3065 if (retval != 0) 3066 break; 3067 3068 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3069 lun->be_lun->blocksize); 3070 3071 if (retval != 0) 3072 break; 3073 3074 retval = sbuf_printf(sb, "\t<serial_number>"); 3075 3076 if (retval != 0) 3077 break; 3078 3079 retval = ctl_sbuf_printf_esc(sb, 3080 lun->be_lun->serial_num, 3081 sizeof(lun->be_lun->serial_num)); 3082 3083 if (retval != 0) 3084 break; 3085 3086 retval = sbuf_printf(sb, "</serial_number>\n"); 3087 3088 if (retval != 0) 3089 break; 3090 3091 retval = sbuf_printf(sb, "\t<device_id>"); 3092 3093 if (retval != 0) 3094 break; 3095 3096 retval = ctl_sbuf_printf_esc(sb, 3097 lun->be_lun->device_id, 3098 sizeof(lun->be_lun->device_id)); 3099 3100 if (retval != 0) 3101 break; 3102 3103 retval = sbuf_printf(sb, "</device_id>\n"); 3104 3105 if (retval != 0) 3106 break; 3107 3108 if (lun->backend->lun_info != NULL) { 3109 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3110 if (retval != 0) 3111 break; 3112 } 3113 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3114 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3115 opt->name, opt->value, opt->name); 3116 if (retval != 0) 3117 break; 3118 } 3119 3120 retval = sbuf_printf(sb, "</lun>\n"); 3121 3122 if (retval != 0) 3123 break; 3124 mtx_unlock(&lun->lun_lock); 3125 } 3126 if (lun != NULL) 3127 mtx_unlock(&lun->lun_lock); 3128 mtx_unlock(&softc->ctl_lock); 3129 3130 if ((retval != 0) 3131 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3132 retval = 0; 3133 sbuf_delete(sb); 3134 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3135 snprintf(list->error_str, sizeof(list->error_str), 3136 "Out of space, %d bytes is too small", 3137 list->alloc_len); 3138 break; 3139 } 3140 3141 sbuf_finish(sb); 3142 3143 retval = copyout(sbuf_data(sb), list->lun_xml, 3144 sbuf_len(sb) + 1); 3145 3146 list->fill_len = sbuf_len(sb) + 1; 3147 list->status = CTL_LUN_LIST_OK; 3148 sbuf_delete(sb); 3149 break; 3150 } 3151 case CTL_ISCSI: { 3152 struct ctl_iscsi *ci; 3153 struct ctl_frontend *fe; 3154 3155 ci = (struct ctl_iscsi *)addr; 3156 3157 fe = ctl_frontend_find("iscsi"); 3158 if (fe == NULL) { 3159 ci->status = CTL_ISCSI_ERROR; 3160 snprintf(ci->error_str, sizeof(ci->error_str), 3161 "Frontend \"iscsi\" not found."); 3162 break; 3163 } 3164 3165 retval = fe->ioctl(dev, cmd, addr, flag, td); 3166 break; 3167 } 3168 case CTL_PORT_REQ: { 3169 struct ctl_req *req; 3170 struct ctl_frontend *fe; 3171 3172 req = (struct ctl_req *)addr; 3173 3174 fe = ctl_frontend_find(req->driver); 3175 if (fe == NULL) { 3176 req->status = CTL_LUN_ERROR; 3177 snprintf(req->error_str, sizeof(req->error_str), 3178 "Frontend \"%s\" not found.", req->driver); 3179 break; 3180 } 3181 if (req->num_args > 0) { 3182 req->kern_args = ctl_copyin_args(req->num_args, 3183 req->args, req->error_str, sizeof(req->error_str)); 3184 if (req->kern_args == NULL) { 3185 req->status = CTL_LUN_ERROR; 3186 break; 3187 } 3188 } 3189 3190 if (fe->ioctl) 3191 retval = fe->ioctl(dev, cmd, addr, flag, td); 3192 else 3193 retval = ENODEV; 3194 3195 if (req->num_args > 0) { 3196 ctl_copyout_args(req->num_args, req->kern_args); 3197 ctl_free_args(req->num_args, req->kern_args); 3198 } 3199 break; 3200 } 3201 case CTL_PORT_LIST: { 3202 struct sbuf *sb; 3203 struct ctl_port *port; 3204 struct ctl_lun_list *list; 3205 struct ctl_option *opt; 3206 int j; 3207 uint32_t plun; 3208 3209 list = (struct ctl_lun_list *)addr; 3210 3211 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3212 if (sb == NULL) { 3213 list->status = CTL_LUN_LIST_ERROR; 3214 snprintf(list->error_str, sizeof(list->error_str), 3215 "Unable to allocate %d bytes for LUN list", 3216 list->alloc_len); 3217 break; 3218 } 3219 3220 sbuf_printf(sb, "<ctlportlist>\n"); 3221 3222 mtx_lock(&softc->ctl_lock); 3223 STAILQ_FOREACH(port, &softc->port_list, links) { 3224 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3225 (uintmax_t)port->targ_port); 3226 3227 /* 3228 * Bail out as soon as we see that we've overfilled 3229 * the buffer. 3230 */ 3231 if (retval != 0) 3232 break; 3233 3234 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3235 "</frontend_type>\n", port->frontend->name); 3236 if (retval != 0) 3237 break; 3238 3239 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3240 port->port_type); 3241 if (retval != 0) 3242 break; 3243 3244 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3245 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3250 port->port_name); 3251 if (retval != 0) 3252 break; 3253 3254 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3255 port->physical_port); 3256 if (retval != 0) 3257 break; 3258 3259 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3260 port->virtual_port); 3261 if (retval != 0) 3262 break; 3263 3264 if (port->target_devid != NULL) { 3265 sbuf_printf(sb, "\t<target>"); 3266 ctl_id_sbuf(port->target_devid, sb); 3267 sbuf_printf(sb, "</target>\n"); 3268 } 3269 3270 if (port->port_devid != NULL) { 3271 sbuf_printf(sb, "\t<port>"); 3272 ctl_id_sbuf(port->port_devid, sb); 3273 sbuf_printf(sb, "</port>\n"); 3274 } 3275 3276 if (port->port_info != NULL) { 3277 retval = port->port_info(port->onoff_arg, sb); 3278 if (retval != 0) 3279 break; 3280 } 3281 STAILQ_FOREACH(opt, &port->options, links) { 3282 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3283 opt->name, opt->value, opt->name); 3284 if (retval != 0) 3285 break; 3286 } 3287 3288 if (port->lun_map != NULL) { 3289 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3290 for (j = 0; j < CTL_MAX_LUNS; j++) { 3291 plun = ctl_lun_map_from_port(port, j); 3292 if (plun >= CTL_MAX_LUNS) 3293 continue; 3294 sbuf_printf(sb, 3295 "\t<lun id=\"%u\">%u</lun>\n", 3296 j, plun); 3297 } 3298 } 3299 3300 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3301 if (port->wwpn_iid[j].in_use == 0 || 3302 (port->wwpn_iid[j].wwpn == 0 && 3303 port->wwpn_iid[j].name == NULL)) 3304 continue; 3305 3306 if (port->wwpn_iid[j].name != NULL) 3307 retval = sbuf_printf(sb, 3308 "\t<initiator id=\"%u\">%s</initiator>\n", 3309 j, port->wwpn_iid[j].name); 3310 else 3311 retval = sbuf_printf(sb, 3312 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3313 j, port->wwpn_iid[j].wwpn); 3314 if (retval != 0) 3315 break; 3316 } 3317 if (retval != 0) 3318 break; 3319 3320 retval = sbuf_printf(sb, "</targ_port>\n"); 3321 if (retval != 0) 3322 break; 3323 } 3324 mtx_unlock(&softc->ctl_lock); 3325 3326 if ((retval != 0) 3327 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3328 retval = 0; 3329 sbuf_delete(sb); 3330 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3331 snprintf(list->error_str, sizeof(list->error_str), 3332 "Out of space, %d bytes is too small", 3333 list->alloc_len); 3334 break; 3335 } 3336 3337 sbuf_finish(sb); 3338 3339 retval = copyout(sbuf_data(sb), list->lun_xml, 3340 sbuf_len(sb) + 1); 3341 3342 list->fill_len = sbuf_len(sb) + 1; 3343 list->status = CTL_LUN_LIST_OK; 3344 sbuf_delete(sb); 3345 break; 3346 } 3347 case CTL_LUN_MAP: { 3348 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3349 struct ctl_port *port; 3350 3351 mtx_lock(&softc->ctl_lock); 3352 if (lm->port < softc->port_min || 3353 lm->port >= softc->port_max || 3354 (port = softc->ctl_ports[lm->port]) == NULL) { 3355 mtx_unlock(&softc->ctl_lock); 3356 return (ENXIO); 3357 } 3358 if (port->status & CTL_PORT_STATUS_ONLINE) { 3359 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3360 if (ctl_lun_map_to_port(port, lun->lun) >= 3361 CTL_MAX_LUNS) 3362 continue; 3363 mtx_lock(&lun->lun_lock); 3364 ctl_est_ua_port(lun, lm->port, -1, 3365 CTL_UA_LUN_CHANGE); 3366 mtx_unlock(&lun->lun_lock); 3367 } 3368 } 3369 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3370 if (lm->plun < CTL_MAX_LUNS) { 3371 if (lm->lun == UINT32_MAX) 3372 retval = ctl_lun_map_unset(port, lm->plun); 3373 else if (lm->lun < CTL_MAX_LUNS && 3374 softc->ctl_luns[lm->lun] != NULL) 3375 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3376 else 3377 return (ENXIO); 3378 } else if (lm->plun == UINT32_MAX) { 3379 if (lm->lun == UINT32_MAX) 3380 retval = ctl_lun_map_deinit(port); 3381 else 3382 retval = ctl_lun_map_init(port); 3383 } else 3384 return (ENXIO); 3385 if (port->status & CTL_PORT_STATUS_ONLINE) 3386 ctl_isc_announce_port(port); 3387 break; 3388 } 3389 default: { 3390 /* XXX KDM should we fix this? */ 3391#if 0 3392 struct ctl_backend_driver *backend; 3393 unsigned int type; 3394 int found; 3395 3396 found = 0; 3397 3398 /* 3399 * We encode the backend type as the ioctl type for backend 3400 * ioctls. So parse it out here, and then search for a 3401 * backend of this type. 3402 */ 3403 type = _IOC_TYPE(cmd); 3404 3405 STAILQ_FOREACH(backend, &softc->be_list, links) { 3406 if (backend->type == type) { 3407 found = 1; 3408 break; 3409 } 3410 } 3411 if (found == 0) { 3412 printf("ctl: unknown ioctl command %#lx or backend " 3413 "%d\n", cmd, type); 3414 retval = EINVAL; 3415 break; 3416 } 3417 retval = backend->ioctl(dev, cmd, addr, flag, td); 3418#endif 3419 retval = ENOTTY; 3420 break; 3421 } 3422 } 3423 return (retval); 3424} 3425 3426uint32_t 3427ctl_get_initindex(struct ctl_nexus *nexus) 3428{ 3429 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3430} 3431 3432int 3433ctl_lun_map_init(struct ctl_port *port) 3434{ 3435 struct ctl_softc *softc = port->ctl_softc; 3436 struct ctl_lun *lun; 3437 uint32_t i; 3438 3439 if (port->lun_map == NULL) 3440 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3441 M_CTL, M_NOWAIT); 3442 if (port->lun_map == NULL) 3443 return (ENOMEM); 3444 for (i = 0; i < CTL_MAX_LUNS; i++) 3445 port->lun_map[i] = UINT32_MAX; 3446 if (port->status & CTL_PORT_STATUS_ONLINE) { 3447 if (port->lun_disable != NULL) { 3448 STAILQ_FOREACH(lun, &softc->lun_list, links) 3449 port->lun_disable(port->targ_lun_arg, lun->lun); 3450 } 3451 ctl_isc_announce_port(port); 3452 } 3453 return (0); 3454} 3455 3456int 3457ctl_lun_map_deinit(struct ctl_port *port) 3458{ 3459 struct ctl_softc *softc = port->ctl_softc; 3460 struct ctl_lun *lun; 3461 3462 if (port->lun_map == NULL) 3463 return (0); 3464 free(port->lun_map, M_CTL); 3465 port->lun_map = NULL; 3466 if (port->status & CTL_PORT_STATUS_ONLINE) { 3467 if (port->lun_enable != NULL) { 3468 STAILQ_FOREACH(lun, &softc->lun_list, links) 3469 port->lun_enable(port->targ_lun_arg, lun->lun); 3470 } 3471 ctl_isc_announce_port(port); 3472 } 3473 return (0); 3474} 3475 3476int 3477ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3478{ 3479 int status; 3480 uint32_t old; 3481 3482 if (port->lun_map == NULL) { 3483 status = ctl_lun_map_init(port); 3484 if (status != 0) 3485 return (status); 3486 } 3487 old = port->lun_map[plun]; 3488 port->lun_map[plun] = glun; 3489 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3490 if (port->lun_enable != NULL) 3491 port->lun_enable(port->targ_lun_arg, plun); 3492 ctl_isc_announce_port(port); 3493 } 3494 return (0); 3495} 3496 3497int 3498ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3499{ 3500 uint32_t old; 3501 3502 if (port->lun_map == NULL) 3503 return (0); 3504 old = port->lun_map[plun]; 3505 port->lun_map[plun] = UINT32_MAX; 3506 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3507 if (port->lun_disable != NULL) 3508 port->lun_disable(port->targ_lun_arg, plun); 3509 ctl_isc_announce_port(port); 3510 } 3511 return (0); 3512} 3513 3514uint32_t 3515ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3516{ 3517 3518 if (port == NULL) 3519 return (UINT32_MAX); 3520 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3521 return (lun_id); 3522 return (port->lun_map[lun_id]); 3523} 3524 3525uint32_t 3526ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3527{ 3528 uint32_t i; 3529 3530 if (port == NULL) 3531 return (UINT32_MAX); 3532 if (port->lun_map == NULL) 3533 return (lun_id); 3534 for (i = 0; i < CTL_MAX_LUNS; i++) { 3535 if (port->lun_map[i] == lun_id) 3536 return (i); 3537 } 3538 return (UINT32_MAX); 3539} 3540 3541static struct ctl_port * 3542ctl_io_port(struct ctl_io_hdr *io_hdr) 3543{ 3544 3545 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3546} 3547 3548int 3549ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3550{ 3551 int i; 3552 3553 for (i = first; i < last; i++) { 3554 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3555 return (i); 3556 } 3557 return (-1); 3558} 3559 3560int 3561ctl_set_mask(uint32_t *mask, uint32_t bit) 3562{ 3563 uint32_t chunk, piece; 3564 3565 chunk = bit >> 5; 3566 piece = bit % (sizeof(uint32_t) * 8); 3567 3568 if ((mask[chunk] & (1 << piece)) != 0) 3569 return (-1); 3570 else 3571 mask[chunk] |= (1 << piece); 3572 3573 return (0); 3574} 3575 3576int 3577ctl_clear_mask(uint32_t *mask, uint32_t bit) 3578{ 3579 uint32_t chunk, piece; 3580 3581 chunk = bit >> 5; 3582 piece = bit % (sizeof(uint32_t) * 8); 3583 3584 if ((mask[chunk] & (1 << piece)) == 0) 3585 return (-1); 3586 else 3587 mask[chunk] &= ~(1 << piece); 3588 3589 return (0); 3590} 3591 3592int 3593ctl_is_set(uint32_t *mask, uint32_t bit) 3594{ 3595 uint32_t chunk, piece; 3596 3597 chunk = bit >> 5; 3598 piece = bit % (sizeof(uint32_t) * 8); 3599 3600 if ((mask[chunk] & (1 << piece)) == 0) 3601 return (0); 3602 else 3603 return (1); 3604} 3605 3606static uint64_t 3607ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3608{ 3609 uint64_t *t; 3610 3611 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3612 if (t == NULL) 3613 return (0); 3614 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3615} 3616 3617static void 3618ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3619{ 3620 uint64_t *t; 3621 3622 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3623 if (t == NULL) 3624 return; 3625 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3626} 3627 3628static void 3629ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3630{ 3631 uint64_t *p; 3632 u_int i; 3633 3634 i = residx/CTL_MAX_INIT_PER_PORT; 3635 if (lun->pr_keys[i] != NULL) 3636 return; 3637 mtx_unlock(&lun->lun_lock); 3638 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3639 M_WAITOK | M_ZERO); 3640 mtx_lock(&lun->lun_lock); 3641 if (lun->pr_keys[i] == NULL) 3642 lun->pr_keys[i] = p; 3643 else 3644 free(p, M_CTL); 3645} 3646 3647static void 3648ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3649{ 3650 uint64_t *t; 3651 3652 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3653 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3654 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3655} 3656 3657/* 3658 * ctl_softc, pool_name, total_ctl_io are passed in. 3659 * npool is passed out. 3660 */ 3661int 3662ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3663 uint32_t total_ctl_io, void **npool) 3664{ 3665#ifdef IO_POOLS 3666 struct ctl_io_pool *pool; 3667 3668 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3669 M_NOWAIT | M_ZERO); 3670 if (pool == NULL) 3671 return (ENOMEM); 3672 3673 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3674 pool->ctl_softc = ctl_softc; 3675 pool->zone = uma_zsecond_create(pool->name, NULL, 3676 NULL, NULL, NULL, ctl_softc->io_zone); 3677 /* uma_prealloc(pool->zone, total_ctl_io); */ 3678 3679 *npool = pool; 3680#else 3681 *npool = ctl_softc->io_zone; 3682#endif 3683 return (0); 3684} 3685 3686void 3687ctl_pool_free(struct ctl_io_pool *pool) 3688{ 3689 3690 if (pool == NULL) 3691 return; 3692 3693#ifdef IO_POOLS 3694 uma_zdestroy(pool->zone); 3695 free(pool, M_CTL); 3696#endif 3697} 3698 3699union ctl_io * 3700ctl_alloc_io(void *pool_ref) 3701{ 3702 union ctl_io *io; 3703#ifdef IO_POOLS 3704 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3705 3706 io = uma_zalloc(pool->zone, M_WAITOK); 3707#else 3708 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3709#endif 3710 if (io != NULL) 3711 io->io_hdr.pool = pool_ref; 3712 return (io); 3713} 3714 3715union ctl_io * 3716ctl_alloc_io_nowait(void *pool_ref) 3717{ 3718 union ctl_io *io; 3719#ifdef IO_POOLS 3720 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3721 3722 io = uma_zalloc(pool->zone, M_NOWAIT); 3723#else 3724 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3725#endif 3726 if (io != NULL) 3727 io->io_hdr.pool = pool_ref; 3728 return (io); 3729} 3730 3731void 3732ctl_free_io(union ctl_io *io) 3733{ 3734#ifdef IO_POOLS 3735 struct ctl_io_pool *pool; 3736#endif 3737 3738 if (io == NULL) 3739 return; 3740 3741#ifdef IO_POOLS 3742 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3743 uma_zfree(pool->zone, io); 3744#else 3745 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3746#endif 3747} 3748 3749void 3750ctl_zero_io(union ctl_io *io) 3751{ 3752 void *pool_ref; 3753 3754 if (io == NULL) 3755 return; 3756 3757 /* 3758 * May need to preserve linked list pointers at some point too. 3759 */ 3760 pool_ref = io->io_hdr.pool; 3761 memset(io, 0, sizeof(*io)); 3762 io->io_hdr.pool = pool_ref; 3763} 3764 3765int 3766ctl_expand_number(const char *buf, uint64_t *num) 3767{ 3768 char *endptr; 3769 uint64_t number; 3770 unsigned shift; 3771 3772 number = strtoq(buf, &endptr, 0); 3773 3774 switch (tolower((unsigned char)*endptr)) { 3775 case 'e': 3776 shift = 60; 3777 break; 3778 case 'p': 3779 shift = 50; 3780 break; 3781 case 't': 3782 shift = 40; 3783 break; 3784 case 'g': 3785 shift = 30; 3786 break; 3787 case 'm': 3788 shift = 20; 3789 break; 3790 case 'k': 3791 shift = 10; 3792 break; 3793 case 'b': 3794 case '\0': /* No unit. */ 3795 *num = number; 3796 return (0); 3797 default: 3798 /* Unrecognized unit. */ 3799 return (-1); 3800 } 3801 3802 if ((number << shift) >> shift != number) { 3803 /* Overflow */ 3804 return (-1); 3805 } 3806 *num = number << shift; 3807 return (0); 3808} 3809 3810 3811/* 3812 * This routine could be used in the future to load default and/or saved 3813 * mode page parameters for a particuar lun. 3814 */ 3815static int 3816ctl_init_page_index(struct ctl_lun *lun) 3817{ 3818 int i; 3819 struct ctl_page_index *page_index; 3820 const char *value; 3821 uint64_t ival; 3822 3823 memcpy(&lun->mode_pages.index, page_index_template, 3824 sizeof(page_index_template)); 3825 3826 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3827 3828 page_index = &lun->mode_pages.index[i]; 3829 if (lun->be_lun->lun_type == T_DIRECT && 3830 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3831 continue; 3832 if (lun->be_lun->lun_type == T_PROCESSOR && 3833 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3834 continue; 3835 if (lun->be_lun->lun_type == T_CDROM && 3836 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3837 continue; 3838 3839 switch (page_index->page_code & SMPH_PC_MASK) { 3840 case SMS_RW_ERROR_RECOVERY_PAGE: { 3841 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3842 panic("subpage is incorrect!"); 3843 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3844 &rw_er_page_default, 3845 sizeof(rw_er_page_default)); 3846 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3847 &rw_er_page_changeable, 3848 sizeof(rw_er_page_changeable)); 3849 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3850 &rw_er_page_default, 3851 sizeof(rw_er_page_default)); 3852 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3853 &rw_er_page_default, 3854 sizeof(rw_er_page_default)); 3855 page_index->page_data = 3856 (uint8_t *)lun->mode_pages.rw_er_page; 3857 break; 3858 } 3859 case SMS_FORMAT_DEVICE_PAGE: { 3860 struct scsi_format_page *format_page; 3861 3862 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3863 panic("subpage is incorrect!"); 3864 3865 /* 3866 * Sectors per track are set above. Bytes per 3867 * sector need to be set here on a per-LUN basis. 3868 */ 3869 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3870 &format_page_default, 3871 sizeof(format_page_default)); 3872 memcpy(&lun->mode_pages.format_page[ 3873 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3874 sizeof(format_page_changeable)); 3875 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3876 &format_page_default, 3877 sizeof(format_page_default)); 3878 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3879 &format_page_default, 3880 sizeof(format_page_default)); 3881 3882 format_page = &lun->mode_pages.format_page[ 3883 CTL_PAGE_CURRENT]; 3884 scsi_ulto2b(lun->be_lun->blocksize, 3885 format_page->bytes_per_sector); 3886 3887 format_page = &lun->mode_pages.format_page[ 3888 CTL_PAGE_DEFAULT]; 3889 scsi_ulto2b(lun->be_lun->blocksize, 3890 format_page->bytes_per_sector); 3891 3892 format_page = &lun->mode_pages.format_page[ 3893 CTL_PAGE_SAVED]; 3894 scsi_ulto2b(lun->be_lun->blocksize, 3895 format_page->bytes_per_sector); 3896 3897 page_index->page_data = 3898 (uint8_t *)lun->mode_pages.format_page; 3899 break; 3900 } 3901 case SMS_RIGID_DISK_PAGE: { 3902 struct scsi_rigid_disk_page *rigid_disk_page; 3903 uint32_t sectors_per_cylinder; 3904 uint64_t cylinders; 3905#ifndef __XSCALE__ 3906 int shift; 3907#endif /* !__XSCALE__ */ 3908 3909 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3910 panic("invalid subpage value %d", 3911 page_index->subpage); 3912 3913 /* 3914 * Rotation rate and sectors per track are set 3915 * above. We calculate the cylinders here based on 3916 * capacity. Due to the number of heads and 3917 * sectors per track we're using, smaller arrays 3918 * may turn out to have 0 cylinders. Linux and 3919 * FreeBSD don't pay attention to these mode pages 3920 * to figure out capacity, but Solaris does. It 3921 * seems to deal with 0 cylinders just fine, and 3922 * works out a fake geometry based on the capacity. 3923 */ 3924 memcpy(&lun->mode_pages.rigid_disk_page[ 3925 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3926 sizeof(rigid_disk_page_default)); 3927 memcpy(&lun->mode_pages.rigid_disk_page[ 3928 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3929 sizeof(rigid_disk_page_changeable)); 3930 3931 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3932 CTL_DEFAULT_HEADS; 3933 3934 /* 3935 * The divide method here will be more accurate, 3936 * probably, but results in floating point being 3937 * used in the kernel on i386 (__udivdi3()). On the 3938 * XScale, though, __udivdi3() is implemented in 3939 * software. 3940 * 3941 * The shift method for cylinder calculation is 3942 * accurate if sectors_per_cylinder is a power of 3943 * 2. Otherwise it might be slightly off -- you 3944 * might have a bit of a truncation problem. 3945 */ 3946#ifdef __XSCALE__ 3947 cylinders = (lun->be_lun->maxlba + 1) / 3948 sectors_per_cylinder; 3949#else 3950 for (shift = 31; shift > 0; shift--) { 3951 if (sectors_per_cylinder & (1 << shift)) 3952 break; 3953 } 3954 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3955#endif 3956 3957 /* 3958 * We've basically got 3 bytes, or 24 bits for the 3959 * cylinder size in the mode page. If we're over, 3960 * just round down to 2^24. 3961 */ 3962 if (cylinders > 0xffffff) 3963 cylinders = 0xffffff; 3964 3965 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3966 CTL_PAGE_DEFAULT]; 3967 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3968 3969 if ((value = ctl_get_opt(&lun->be_lun->options, 3970 "rpm")) != NULL) { 3971 scsi_ulto2b(strtol(value, NULL, 0), 3972 rigid_disk_page->rotation_rate); 3973 } 3974 3975 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3976 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3977 sizeof(rigid_disk_page_default)); 3978 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3979 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3980 sizeof(rigid_disk_page_default)); 3981 3982 page_index->page_data = 3983 (uint8_t *)lun->mode_pages.rigid_disk_page; 3984 break; 3985 } 3986 case SMS_CACHING_PAGE: { 3987 struct scsi_caching_page *caching_page; 3988 3989 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3990 panic("invalid subpage value %d", 3991 page_index->subpage); 3992 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3993 &caching_page_default, 3994 sizeof(caching_page_default)); 3995 memcpy(&lun->mode_pages.caching_page[ 3996 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3997 sizeof(caching_page_changeable)); 3998 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3999 &caching_page_default, 4000 sizeof(caching_page_default)); 4001 caching_page = &lun->mode_pages.caching_page[ 4002 CTL_PAGE_SAVED]; 4003 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4004 if (value != NULL && strcmp(value, "off") == 0) 4005 caching_page->flags1 &= ~SCP_WCE; 4006 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4007 if (value != NULL && strcmp(value, "off") == 0) 4008 caching_page->flags1 |= SCP_RCD; 4009 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4010 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4011 sizeof(caching_page_default)); 4012 page_index->page_data = 4013 (uint8_t *)lun->mode_pages.caching_page; 4014 break; 4015 } 4016 case SMS_CONTROL_MODE_PAGE: { 4017 switch (page_index->subpage) { 4018 case SMS_SUBPAGE_PAGE_0: { 4019 struct scsi_control_page *control_page; 4020 4021 memcpy(&lun->mode_pages.control_page[ 4022 CTL_PAGE_DEFAULT], 4023 &control_page_default, 4024 sizeof(control_page_default)); 4025 memcpy(&lun->mode_pages.control_page[ 4026 CTL_PAGE_CHANGEABLE], 4027 &control_page_changeable, 4028 sizeof(control_page_changeable)); 4029 memcpy(&lun->mode_pages.control_page[ 4030 CTL_PAGE_SAVED], 4031 &control_page_default, 4032 sizeof(control_page_default)); 4033 control_page = &lun->mode_pages.control_page[ 4034 CTL_PAGE_SAVED]; 4035 value = ctl_get_opt(&lun->be_lun->options, 4036 "reordering"); 4037 if (value != NULL && 4038 strcmp(value, "unrestricted") == 0) { 4039 control_page->queue_flags &= 4040 ~SCP_QUEUE_ALG_MASK; 4041 control_page->queue_flags |= 4042 SCP_QUEUE_ALG_UNRESTRICTED; 4043 } 4044 memcpy(&lun->mode_pages.control_page[ 4045 CTL_PAGE_CURRENT], 4046 &lun->mode_pages.control_page[ 4047 CTL_PAGE_SAVED], 4048 sizeof(control_page_default)); 4049 page_index->page_data = 4050 (uint8_t *)lun->mode_pages.control_page; 4051 break; 4052 } 4053 case 0x01: 4054 memcpy(&lun->mode_pages.control_ext_page[ 4055 CTL_PAGE_DEFAULT], 4056 &control_ext_page_default, 4057 sizeof(control_ext_page_default)); 4058 memcpy(&lun->mode_pages.control_ext_page[ 4059 CTL_PAGE_CHANGEABLE], 4060 &control_ext_page_changeable, 4061 sizeof(control_ext_page_changeable)); 4062 memcpy(&lun->mode_pages.control_ext_page[ 4063 CTL_PAGE_SAVED], 4064 &control_ext_page_default, 4065 sizeof(control_ext_page_default)); 4066 memcpy(&lun->mode_pages.control_ext_page[ 4067 CTL_PAGE_CURRENT], 4068 &lun->mode_pages.control_ext_page[ 4069 CTL_PAGE_SAVED], 4070 sizeof(control_ext_page_default)); 4071 page_index->page_data = 4072 (uint8_t *)lun->mode_pages.control_ext_page; 4073 break; 4074 } 4075 break; 4076 } 4077 case SMS_INFO_EXCEPTIONS_PAGE: { 4078 switch (page_index->subpage) { 4079 case SMS_SUBPAGE_PAGE_0: 4080 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4081 &ie_page_default, 4082 sizeof(ie_page_default)); 4083 memcpy(&lun->mode_pages.ie_page[ 4084 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4085 sizeof(ie_page_changeable)); 4086 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4087 &ie_page_default, 4088 sizeof(ie_page_default)); 4089 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4090 &ie_page_default, 4091 sizeof(ie_page_default)); 4092 page_index->page_data = 4093 (uint8_t *)lun->mode_pages.ie_page; 4094 break; 4095 case 0x02: { 4096 struct ctl_logical_block_provisioning_page *page; 4097 4098 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4099 &lbp_page_default, 4100 sizeof(lbp_page_default)); 4101 memcpy(&lun->mode_pages.lbp_page[ 4102 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4103 sizeof(lbp_page_changeable)); 4104 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4105 &lbp_page_default, 4106 sizeof(lbp_page_default)); 4107 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4108 value = ctl_get_opt(&lun->be_lun->options, 4109 "avail-threshold"); 4110 if (value != NULL && 4111 ctl_expand_number(value, &ival) == 0) { 4112 page->descr[0].flags |= SLBPPD_ENABLED | 4113 SLBPPD_ARMING_DEC; 4114 if (lun->be_lun->blocksize) 4115 ival /= lun->be_lun->blocksize; 4116 else 4117 ival /= 512; 4118 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4119 page->descr[0].count); 4120 } 4121 value = ctl_get_opt(&lun->be_lun->options, 4122 "used-threshold"); 4123 if (value != NULL && 4124 ctl_expand_number(value, &ival) == 0) { 4125 page->descr[1].flags |= SLBPPD_ENABLED | 4126 SLBPPD_ARMING_INC; 4127 if (lun->be_lun->blocksize) 4128 ival /= lun->be_lun->blocksize; 4129 else 4130 ival /= 512; 4131 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4132 page->descr[1].count); 4133 } 4134 value = ctl_get_opt(&lun->be_lun->options, 4135 "pool-avail-threshold"); 4136 if (value != NULL && 4137 ctl_expand_number(value, &ival) == 0) { 4138 page->descr[2].flags |= SLBPPD_ENABLED | 4139 SLBPPD_ARMING_DEC; 4140 if (lun->be_lun->blocksize) 4141 ival /= lun->be_lun->blocksize; 4142 else 4143 ival /= 512; 4144 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4145 page->descr[2].count); 4146 } 4147 value = ctl_get_opt(&lun->be_lun->options, 4148 "pool-used-threshold"); 4149 if (value != NULL && 4150 ctl_expand_number(value, &ival) == 0) { 4151 page->descr[3].flags |= SLBPPD_ENABLED | 4152 SLBPPD_ARMING_INC; 4153 if (lun->be_lun->blocksize) 4154 ival /= lun->be_lun->blocksize; 4155 else 4156 ival /= 512; 4157 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4158 page->descr[3].count); 4159 } 4160 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4161 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4162 sizeof(lbp_page_default)); 4163 page_index->page_data = 4164 (uint8_t *)lun->mode_pages.lbp_page; 4165 }} 4166 break; 4167 } 4168 case SMS_CDDVD_CAPS_PAGE:{ 4169 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4170 &cddvd_page_default, 4171 sizeof(cddvd_page_default)); 4172 memcpy(&lun->mode_pages.cddvd_page[ 4173 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4174 sizeof(cddvd_page_changeable)); 4175 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4176 &cddvd_page_default, 4177 sizeof(cddvd_page_default)); 4178 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4179 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4180 sizeof(cddvd_page_default)); 4181 page_index->page_data = 4182 (uint8_t *)lun->mode_pages.cddvd_page; 4183 break; 4184 } 4185 case SMS_VENDOR_SPECIFIC_PAGE:{ 4186 switch (page_index->subpage) { 4187 case DBGCNF_SUBPAGE_CODE: { 4188 memcpy(&lun->mode_pages.debugconf_subpage[ 4189 CTL_PAGE_CURRENT], 4190 &debugconf_page_default, 4191 sizeof(debugconf_page_default)); 4192 memcpy(&lun->mode_pages.debugconf_subpage[ 4193 CTL_PAGE_CHANGEABLE], 4194 &debugconf_page_changeable, 4195 sizeof(debugconf_page_changeable)); 4196 memcpy(&lun->mode_pages.debugconf_subpage[ 4197 CTL_PAGE_DEFAULT], 4198 &debugconf_page_default, 4199 sizeof(debugconf_page_default)); 4200 memcpy(&lun->mode_pages.debugconf_subpage[ 4201 CTL_PAGE_SAVED], 4202 &debugconf_page_default, 4203 sizeof(debugconf_page_default)); 4204 page_index->page_data = 4205 (uint8_t *)lun->mode_pages.debugconf_subpage; 4206 break; 4207 } 4208 default: 4209 panic("invalid subpage value %d", 4210 page_index->subpage); 4211 break; 4212 } 4213 break; 4214 } 4215 default: 4216 panic("invalid page value %d", 4217 page_index->page_code & SMPH_PC_MASK); 4218 break; 4219 } 4220 } 4221 4222 return (CTL_RETVAL_COMPLETE); 4223} 4224 4225static int 4226ctl_init_log_page_index(struct ctl_lun *lun) 4227{ 4228 struct ctl_page_index *page_index; 4229 int i, j, k, prev; 4230 4231 memcpy(&lun->log_pages.index, log_page_index_template, 4232 sizeof(log_page_index_template)); 4233 4234 prev = -1; 4235 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4236 4237 page_index = &lun->log_pages.index[i]; 4238 if (lun->be_lun->lun_type == T_DIRECT && 4239 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4240 continue; 4241 if (lun->be_lun->lun_type == T_PROCESSOR && 4242 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4243 continue; 4244 if (lun->be_lun->lun_type == T_CDROM && 4245 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4246 continue; 4247 4248 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4249 lun->backend->lun_attr == NULL) 4250 continue; 4251 4252 if (page_index->page_code != prev) { 4253 lun->log_pages.pages_page[j] = page_index->page_code; 4254 prev = page_index->page_code; 4255 j++; 4256 } 4257 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4258 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4259 k++; 4260 } 4261 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4262 lun->log_pages.index[0].page_len = j; 4263 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4264 lun->log_pages.index[1].page_len = k * 2; 4265 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4266 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4267 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4268 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4269 4270 return (CTL_RETVAL_COMPLETE); 4271} 4272 4273static int 4274hex2bin(const char *str, uint8_t *buf, int buf_size) 4275{ 4276 int i; 4277 u_char c; 4278 4279 memset(buf, 0, buf_size); 4280 while (isspace(str[0])) 4281 str++; 4282 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4283 str += 2; 4284 buf_size *= 2; 4285 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4286 c = str[i]; 4287 if (isdigit(c)) 4288 c -= '0'; 4289 else if (isalpha(c)) 4290 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4291 else 4292 break; 4293 if (c >= 16) 4294 break; 4295 if ((i & 1) == 0) 4296 buf[i / 2] |= (c << 4); 4297 else 4298 buf[i / 2] |= c; 4299 } 4300 return ((i + 1) / 2); 4301} 4302 4303/* 4304 * LUN allocation. 4305 * 4306 * Requirements: 4307 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4308 * wants us to allocate the LUN and he can block. 4309 * - ctl_softc is always set 4310 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4311 * 4312 * Returns 0 for success, non-zero (errno) for failure. 4313 */ 4314static int 4315ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4316 struct ctl_be_lun *const be_lun) 4317{ 4318 struct ctl_lun *nlun, *lun; 4319 struct scsi_vpd_id_descriptor *desc; 4320 struct scsi_vpd_id_t10 *t10id; 4321 const char *eui, *naa, *scsiname, *vendor, *value; 4322 int lun_number, i, lun_malloced; 4323 int devidlen, idlen1, idlen2 = 0, len; 4324 4325 if (be_lun == NULL) 4326 return (EINVAL); 4327 4328 /* 4329 * We currently only support Direct Access or Processor LUN types. 4330 */ 4331 switch (be_lun->lun_type) { 4332 case T_DIRECT: 4333 case T_PROCESSOR: 4334 case T_CDROM: 4335 break; 4336 case T_SEQUENTIAL: 4337 case T_CHANGER: 4338 default: 4339 be_lun->lun_config_status(be_lun->be_lun, 4340 CTL_LUN_CONFIG_FAILURE); 4341 break; 4342 } 4343 if (ctl_lun == NULL) { 4344 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4345 lun_malloced = 1; 4346 } else { 4347 lun_malloced = 0; 4348 lun = ctl_lun; 4349 } 4350 4351 memset(lun, 0, sizeof(*lun)); 4352 if (lun_malloced) 4353 lun->flags = CTL_LUN_MALLOCED; 4354 4355 /* Generate LUN ID. */ 4356 devidlen = max(CTL_DEVID_MIN_LEN, 4357 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4358 idlen1 = sizeof(*t10id) + devidlen; 4359 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4360 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4361 if (scsiname != NULL) { 4362 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4363 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4364 } 4365 eui = ctl_get_opt(&be_lun->options, "eui"); 4366 if (eui != NULL) { 4367 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4368 } 4369 naa = ctl_get_opt(&be_lun->options, "naa"); 4370 if (naa != NULL) { 4371 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4372 } 4373 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4374 M_CTL, M_WAITOK | M_ZERO); 4375 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4376 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4377 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4378 desc->length = idlen1; 4379 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4380 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4381 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4382 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4383 } else { 4384 strncpy(t10id->vendor, vendor, 4385 min(sizeof(t10id->vendor), strlen(vendor))); 4386 } 4387 strncpy((char *)t10id->vendor_spec_id, 4388 (char *)be_lun->device_id, devidlen); 4389 if (scsiname != NULL) { 4390 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4391 desc->length); 4392 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4393 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4394 SVPD_ID_TYPE_SCSI_NAME; 4395 desc->length = idlen2; 4396 strlcpy(desc->identifier, scsiname, idlen2); 4397 } 4398 if (eui != NULL) { 4399 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4400 desc->length); 4401 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4402 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4403 SVPD_ID_TYPE_EUI64; 4404 desc->length = hex2bin(eui, desc->identifier, 16); 4405 desc->length = desc->length > 12 ? 16 : 4406 (desc->length > 8 ? 12 : 8); 4407 len -= 16 - desc->length; 4408 } 4409 if (naa != NULL) { 4410 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4411 desc->length); 4412 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4413 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4414 SVPD_ID_TYPE_NAA; 4415 desc->length = hex2bin(naa, desc->identifier, 16); 4416 desc->length = desc->length > 8 ? 16 : 8; 4417 len -= 16 - desc->length; 4418 } 4419 lun->lun_devid->len = len; 4420 4421 mtx_lock(&ctl_softc->ctl_lock); 4422 /* 4423 * See if the caller requested a particular LUN number. If so, see 4424 * if it is available. Otherwise, allocate the first available LUN. 4425 */ 4426 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4427 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4428 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4429 mtx_unlock(&ctl_softc->ctl_lock); 4430 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4431 printf("ctl: requested LUN ID %d is higher " 4432 "than CTL_MAX_LUNS - 1 (%d)\n", 4433 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4434 } else { 4435 /* 4436 * XXX KDM return an error, or just assign 4437 * another LUN ID in this case?? 4438 */ 4439 printf("ctl: requested LUN ID %d is already " 4440 "in use\n", be_lun->req_lun_id); 4441 } 4442 if (lun->flags & CTL_LUN_MALLOCED) 4443 free(lun, M_CTL); 4444 be_lun->lun_config_status(be_lun->be_lun, 4445 CTL_LUN_CONFIG_FAILURE); 4446 return (ENOSPC); 4447 } 4448 lun_number = be_lun->req_lun_id; 4449 } else { 4450 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4451 if (lun_number == -1) { 4452 mtx_unlock(&ctl_softc->ctl_lock); 4453 printf("ctl: can't allocate LUN, out of LUNs\n"); 4454 if (lun->flags & CTL_LUN_MALLOCED) 4455 free(lun, M_CTL); 4456 be_lun->lun_config_status(be_lun->be_lun, 4457 CTL_LUN_CONFIG_FAILURE); 4458 return (ENOSPC); 4459 } 4460 } 4461 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4462 4463 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4464 lun->lun = lun_number; 4465 lun->be_lun = be_lun; 4466 /* 4467 * The processor LUN is always enabled. Disk LUNs come on line 4468 * disabled, and must be enabled by the backend. 4469 */ 4470 lun->flags |= CTL_LUN_DISABLED; 4471 lun->backend = be_lun->be; 4472 be_lun->ctl_lun = lun; 4473 be_lun->lun_id = lun_number; 4474 atomic_add_int(&be_lun->be->num_luns, 1); 4475 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4476 lun->flags |= CTL_LUN_EJECTED; 4477 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4478 lun->flags |= CTL_LUN_NO_MEDIA; 4479 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4480 lun->flags |= CTL_LUN_STOPPED; 4481 4482 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4483 lun->flags |= CTL_LUN_PRIMARY_SC; 4484 4485 value = ctl_get_opt(&be_lun->options, "removable"); 4486 if (value != NULL) { 4487 if (strcmp(value, "on") == 0) 4488 lun->flags |= CTL_LUN_REMOVABLE; 4489 } else if (be_lun->lun_type == T_CDROM) 4490 lun->flags |= CTL_LUN_REMOVABLE; 4491 4492 lun->ctl_softc = ctl_softc; 4493#ifdef CTL_TIME_IO 4494 lun->last_busy = getsbinuptime(); 4495#endif 4496 TAILQ_INIT(&lun->ooa_queue); 4497 TAILQ_INIT(&lun->blocked_queue); 4498 STAILQ_INIT(&lun->error_list); 4499 ctl_tpc_lun_init(lun); 4500 4501 /* 4502 * Initialize the mode and log page index. 4503 */ 4504 ctl_init_page_index(lun); 4505 ctl_init_log_page_index(lun); 4506 4507 /* 4508 * Now, before we insert this lun on the lun list, set the lun 4509 * inventory changed UA for all other luns. 4510 */ 4511 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4512 mtx_lock(&nlun->lun_lock); 4513 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4514 mtx_unlock(&nlun->lun_lock); 4515 } 4516 4517 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4518 4519 ctl_softc->ctl_luns[lun_number] = lun; 4520 4521 ctl_softc->num_luns++; 4522 4523 /* Setup statistics gathering */ 4524 lun->stats.device_type = be_lun->lun_type; 4525 lun->stats.lun_number = lun_number; 4526 lun->stats.blocksize = be_lun->blocksize; 4527 if (be_lun->blocksize == 0) 4528 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4529 for (i = 0;i < CTL_MAX_PORTS;i++) 4530 lun->stats.ports[i].targ_port = i; 4531 4532 mtx_unlock(&ctl_softc->ctl_lock); 4533 4534 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4535 return (0); 4536} 4537 4538/* 4539 * Delete a LUN. 4540 * Assumptions: 4541 * - LUN has already been marked invalid and any pending I/O has been taken 4542 * care of. 4543 */ 4544static int 4545ctl_free_lun(struct ctl_lun *lun) 4546{ 4547 struct ctl_softc *softc; 4548 struct ctl_lun *nlun; 4549 int i; 4550 4551 softc = lun->ctl_softc; 4552 4553 mtx_assert(&softc->ctl_lock, MA_OWNED); 4554 4555 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4556 4557 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4558 4559 softc->ctl_luns[lun->lun] = NULL; 4560 4561 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4562 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4563 4564 softc->num_luns--; 4565 4566 /* 4567 * Tell the backend to free resources, if this LUN has a backend. 4568 */ 4569 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4570 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4571 4572 ctl_tpc_lun_shutdown(lun); 4573 mtx_destroy(&lun->lun_lock); 4574 free(lun->lun_devid, M_CTL); 4575 for (i = 0; i < CTL_MAX_PORTS; i++) 4576 free(lun->pending_ua[i], M_CTL); 4577 for (i = 0; i < CTL_MAX_PORTS; i++) 4578 free(lun->pr_keys[i], M_CTL); 4579 free(lun->write_buffer, M_CTL); 4580 if (lun->flags & CTL_LUN_MALLOCED) 4581 free(lun, M_CTL); 4582 4583 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4584 mtx_lock(&nlun->lun_lock); 4585 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4586 mtx_unlock(&nlun->lun_lock); 4587 } 4588 4589 return (0); 4590} 4591 4592static void 4593ctl_create_lun(struct ctl_be_lun *be_lun) 4594{ 4595 4596 /* 4597 * ctl_alloc_lun() should handle all potential failure cases. 4598 */ 4599 ctl_alloc_lun(control_softc, NULL, be_lun); 4600} 4601 4602int 4603ctl_add_lun(struct ctl_be_lun *be_lun) 4604{ 4605 struct ctl_softc *softc = control_softc; 4606 4607 mtx_lock(&softc->ctl_lock); 4608 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4609 mtx_unlock(&softc->ctl_lock); 4610 wakeup(&softc->pending_lun_queue); 4611 4612 return (0); 4613} 4614 4615int 4616ctl_enable_lun(struct ctl_be_lun *be_lun) 4617{ 4618 struct ctl_softc *softc; 4619 struct ctl_port *port, *nport; 4620 struct ctl_lun *lun; 4621 int retval; 4622 4623 lun = (struct ctl_lun *)be_lun->ctl_lun; 4624 softc = lun->ctl_softc; 4625 4626 mtx_lock(&softc->ctl_lock); 4627 mtx_lock(&lun->lun_lock); 4628 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4629 /* 4630 * eh? Why did we get called if the LUN is already 4631 * enabled? 4632 */ 4633 mtx_unlock(&lun->lun_lock); 4634 mtx_unlock(&softc->ctl_lock); 4635 return (0); 4636 } 4637 lun->flags &= ~CTL_LUN_DISABLED; 4638 mtx_unlock(&lun->lun_lock); 4639 4640 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4641 nport = STAILQ_NEXT(port, links); 4642 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4643 port->lun_map != NULL || port->lun_enable == NULL) 4644 continue; 4645 4646 /* 4647 * Drop the lock while we call the FETD's enable routine. 4648 * This can lead to a callback into CTL (at least in the 4649 * case of the internal initiator frontend. 4650 */ 4651 mtx_unlock(&softc->ctl_lock); 4652 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4653 mtx_lock(&softc->ctl_lock); 4654 if (retval != 0) { 4655 printf("%s: FETD %s port %d returned error " 4656 "%d for lun_enable on lun %jd\n", 4657 __func__, port->port_name, port->targ_port, 4658 retval, (intmax_t)lun->lun); 4659 } 4660 } 4661 4662 mtx_unlock(&softc->ctl_lock); 4663 ctl_isc_announce_lun(lun); 4664 4665 return (0); 4666} 4667 4668int 4669ctl_disable_lun(struct ctl_be_lun *be_lun) 4670{ 4671 struct ctl_softc *softc; 4672 struct ctl_port *port; 4673 struct ctl_lun *lun; 4674 int retval; 4675 4676 lun = (struct ctl_lun *)be_lun->ctl_lun; 4677 softc = lun->ctl_softc; 4678 4679 mtx_lock(&softc->ctl_lock); 4680 mtx_lock(&lun->lun_lock); 4681 if (lun->flags & CTL_LUN_DISABLED) { 4682 mtx_unlock(&lun->lun_lock); 4683 mtx_unlock(&softc->ctl_lock); 4684 return (0); 4685 } 4686 lun->flags |= CTL_LUN_DISABLED; 4687 mtx_unlock(&lun->lun_lock); 4688 4689 STAILQ_FOREACH(port, &softc->port_list, links) { 4690 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4691 port->lun_map != NULL || port->lun_disable == NULL) 4692 continue; 4693 4694 /* 4695 * Drop the lock before we call the frontend's disable 4696 * routine, to avoid lock order reversals. 4697 * 4698 * XXX KDM what happens if the frontend list changes while 4699 * we're traversing it? It's unlikely, but should be handled. 4700 */ 4701 mtx_unlock(&softc->ctl_lock); 4702 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4703 mtx_lock(&softc->ctl_lock); 4704 if (retval != 0) { 4705 printf("%s: FETD %s port %d returned error " 4706 "%d for lun_disable on lun %jd\n", 4707 __func__, port->port_name, port->targ_port, 4708 retval, (intmax_t)lun->lun); 4709 } 4710 } 4711 4712 mtx_unlock(&softc->ctl_lock); 4713 ctl_isc_announce_lun(lun); 4714 4715 return (0); 4716} 4717 4718int 4719ctl_start_lun(struct ctl_be_lun *be_lun) 4720{ 4721 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4722 4723 mtx_lock(&lun->lun_lock); 4724 lun->flags &= ~CTL_LUN_STOPPED; 4725 mtx_unlock(&lun->lun_lock); 4726 return (0); 4727} 4728 4729int 4730ctl_stop_lun(struct ctl_be_lun *be_lun) 4731{ 4732 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4733 4734 mtx_lock(&lun->lun_lock); 4735 lun->flags |= CTL_LUN_STOPPED; 4736 mtx_unlock(&lun->lun_lock); 4737 return (0); 4738} 4739 4740int 4741ctl_lun_no_media(struct ctl_be_lun *be_lun) 4742{ 4743 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4744 4745 mtx_lock(&lun->lun_lock); 4746 lun->flags |= CTL_LUN_NO_MEDIA; 4747 mtx_unlock(&lun->lun_lock); 4748 return (0); 4749} 4750 4751int 4752ctl_lun_has_media(struct ctl_be_lun *be_lun) 4753{ 4754 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4755 union ctl_ha_msg msg; 4756 4757 mtx_lock(&lun->lun_lock); 4758 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4759 if (lun->flags & CTL_LUN_REMOVABLE) 4760 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4761 mtx_unlock(&lun->lun_lock); 4762 if ((lun->flags & CTL_LUN_REMOVABLE) && 4763 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4764 bzero(&msg.ua, sizeof(msg.ua)); 4765 msg.hdr.msg_type = CTL_MSG_UA; 4766 msg.hdr.nexus.initid = -1; 4767 msg.hdr.nexus.targ_port = -1; 4768 msg.hdr.nexus.targ_lun = lun->lun; 4769 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4770 msg.ua.ua_all = 1; 4771 msg.ua.ua_set = 1; 4772 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4773 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4774 M_WAITOK); 4775 } 4776 return (0); 4777} 4778 4779int 4780ctl_lun_ejected(struct ctl_be_lun *be_lun) 4781{ 4782 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4783 4784 mtx_lock(&lun->lun_lock); 4785 lun->flags |= CTL_LUN_EJECTED; 4786 mtx_unlock(&lun->lun_lock); 4787 return (0); 4788} 4789 4790int 4791ctl_lun_primary(struct ctl_be_lun *be_lun) 4792{ 4793 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4794 4795 mtx_lock(&lun->lun_lock); 4796 lun->flags |= CTL_LUN_PRIMARY_SC; 4797 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4798 mtx_unlock(&lun->lun_lock); 4799 ctl_isc_announce_lun(lun); 4800 return (0); 4801} 4802 4803int 4804ctl_lun_secondary(struct ctl_be_lun *be_lun) 4805{ 4806 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4807 4808 mtx_lock(&lun->lun_lock); 4809 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4810 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4811 mtx_unlock(&lun->lun_lock); 4812 ctl_isc_announce_lun(lun); 4813 return (0); 4814} 4815 4816int 4817ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4818{ 4819 struct ctl_softc *softc; 4820 struct ctl_lun *lun; 4821 4822 lun = (struct ctl_lun *)be_lun->ctl_lun; 4823 softc = lun->ctl_softc; 4824 4825 mtx_lock(&lun->lun_lock); 4826 4827 /* 4828 * The LUN needs to be disabled before it can be marked invalid. 4829 */ 4830 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4831 mtx_unlock(&lun->lun_lock); 4832 return (-1); 4833 } 4834 /* 4835 * Mark the LUN invalid. 4836 */ 4837 lun->flags |= CTL_LUN_INVALID; 4838 4839 /* 4840 * If there is nothing in the OOA queue, go ahead and free the LUN. 4841 * If we have something in the OOA queue, we'll free it when the 4842 * last I/O completes. 4843 */ 4844 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4845 mtx_unlock(&lun->lun_lock); 4846 mtx_lock(&softc->ctl_lock); 4847 ctl_free_lun(lun); 4848 mtx_unlock(&softc->ctl_lock); 4849 } else 4850 mtx_unlock(&lun->lun_lock); 4851 4852 return (0); 4853} 4854 4855void 4856ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4857{ 4858 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4859 union ctl_ha_msg msg; 4860 4861 mtx_lock(&lun->lun_lock); 4862 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4863 mtx_unlock(&lun->lun_lock); 4864 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4865 /* Send msg to other side. */ 4866 bzero(&msg.ua, sizeof(msg.ua)); 4867 msg.hdr.msg_type = CTL_MSG_UA; 4868 msg.hdr.nexus.initid = -1; 4869 msg.hdr.nexus.targ_port = -1; 4870 msg.hdr.nexus.targ_lun = lun->lun; 4871 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4872 msg.ua.ua_all = 1; 4873 msg.ua.ua_set = 1; 4874 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4876 M_WAITOK); 4877 } 4878} 4879 4880/* 4881 * Backend "memory move is complete" callback for requests that never 4882 * make it down to say RAIDCore's configuration code. 4883 */ 4884int 4885ctl_config_move_done(union ctl_io *io) 4886{ 4887 int retval; 4888 4889 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4890 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4891 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4892 4893 if ((io->io_hdr.port_status != 0) && 4894 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4895 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4896 /* 4897 * For hardware error sense keys, the sense key 4898 * specific value is defined to be a retry count, 4899 * but we use it to pass back an internal FETD 4900 * error code. XXX KDM Hopefully the FETD is only 4901 * using 16 bits for an error code, since that's 4902 * all the space we have in the sks field. 4903 */ 4904 ctl_set_internal_failure(&io->scsiio, 4905 /*sks_valid*/ 1, 4906 /*retry_count*/ 4907 io->io_hdr.port_status); 4908 } 4909 4910 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4911 ctl_data_print(io); 4912 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4913 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4914 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4915 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4916 /* 4917 * XXX KDM just assuming a single pointer here, and not a 4918 * S/G list. If we start using S/G lists for config data, 4919 * we'll need to know how to clean them up here as well. 4920 */ 4921 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4922 free(io->scsiio.kern_data_ptr, M_CTL); 4923 ctl_done(io); 4924 retval = CTL_RETVAL_COMPLETE; 4925 } else { 4926 /* 4927 * XXX KDM now we need to continue data movement. Some 4928 * options: 4929 * - call ctl_scsiio() again? We don't do this for data 4930 * writes, because for those at least we know ahead of 4931 * time where the write will go and how long it is. For 4932 * config writes, though, that information is largely 4933 * contained within the write itself, thus we need to 4934 * parse out the data again. 4935 * 4936 * - Call some other function once the data is in? 4937 */ 4938 4939 /* 4940 * XXX KDM call ctl_scsiio() again for now, and check flag 4941 * bits to see whether we're allocated or not. 4942 */ 4943 retval = ctl_scsiio(&io->scsiio); 4944 } 4945 return (retval); 4946} 4947 4948/* 4949 * This gets called by a backend driver when it is done with a 4950 * data_submit method. 4951 */ 4952void 4953ctl_data_submit_done(union ctl_io *io) 4954{ 4955 /* 4956 * If the IO_CONT flag is set, we need to call the supplied 4957 * function to continue processing the I/O, instead of completing 4958 * the I/O just yet. 4959 * 4960 * If there is an error, though, we don't want to keep processing. 4961 * Instead, just send status back to the initiator. 4962 */ 4963 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4964 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4965 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4966 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4967 io->scsiio.io_cont(io); 4968 return; 4969 } 4970 ctl_done(io); 4971} 4972 4973/* 4974 * This gets called by a backend driver when it is done with a 4975 * configuration write. 4976 */ 4977void 4978ctl_config_write_done(union ctl_io *io) 4979{ 4980 uint8_t *buf; 4981 4982 /* 4983 * If the IO_CONT flag is set, we need to call the supplied 4984 * function to continue processing the I/O, instead of completing 4985 * the I/O just yet. 4986 * 4987 * If there is an error, though, we don't want to keep processing. 4988 * Instead, just send status back to the initiator. 4989 */ 4990 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4991 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4992 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4993 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4994 io->scsiio.io_cont(io); 4995 return; 4996 } 4997 /* 4998 * Since a configuration write can be done for commands that actually 4999 * have data allocated, like write buffer, and commands that have 5000 * no data, like start/stop unit, we need to check here. 5001 */ 5002 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5003 buf = io->scsiio.kern_data_ptr; 5004 else 5005 buf = NULL; 5006 ctl_done(io); 5007 if (buf) 5008 free(buf, M_CTL); 5009} 5010 5011void 5012ctl_config_read_done(union ctl_io *io) 5013{ 5014 uint8_t *buf; 5015 5016 /* 5017 * If there is some error -- we are done, skip data transfer. 5018 */ 5019 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5020 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5021 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5022 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5023 buf = io->scsiio.kern_data_ptr; 5024 else 5025 buf = NULL; 5026 ctl_done(io); 5027 if (buf) 5028 free(buf, M_CTL); 5029 return; 5030 } 5031 5032 /* 5033 * If the IO_CONT flag is set, we need to call the supplied 5034 * function to continue processing the I/O, instead of completing 5035 * the I/O just yet. 5036 */ 5037 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5038 io->scsiio.io_cont(io); 5039 return; 5040 } 5041 5042 ctl_datamove(io); 5043} 5044 5045/* 5046 * SCSI release command. 5047 */ 5048int 5049ctl_scsi_release(struct ctl_scsiio *ctsio) 5050{ 5051 struct ctl_lun *lun; 5052 uint32_t residx; 5053 5054 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5055 5056 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5057 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5058 5059 /* 5060 * XXX KDM right now, we only support LUN reservation. We don't 5061 * support 3rd party reservations, or extent reservations, which 5062 * might actually need the parameter list. If we've gotten this 5063 * far, we've got a LUN reservation. Anything else got kicked out 5064 * above. So, according to SPC, ignore the length. 5065 */ 5066 5067 mtx_lock(&lun->lun_lock); 5068 5069 /* 5070 * According to SPC, it is not an error for an intiator to attempt 5071 * to release a reservation on a LUN that isn't reserved, or that 5072 * is reserved by another initiator. The reservation can only be 5073 * released, though, by the initiator who made it or by one of 5074 * several reset type events. 5075 */ 5076 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5077 lun->flags &= ~CTL_LUN_RESERVED; 5078 5079 mtx_unlock(&lun->lun_lock); 5080 5081 ctl_set_success(ctsio); 5082 ctl_done((union ctl_io *)ctsio); 5083 return (CTL_RETVAL_COMPLETE); 5084} 5085 5086int 5087ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5088{ 5089 struct ctl_lun *lun; 5090 uint32_t residx; 5091 5092 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5093 5094 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5095 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5096 5097 /* 5098 * XXX KDM right now, we only support LUN reservation. We don't 5099 * support 3rd party reservations, or extent reservations, which 5100 * might actually need the parameter list. If we've gotten this 5101 * far, we've got a LUN reservation. Anything else got kicked out 5102 * above. So, according to SPC, ignore the length. 5103 */ 5104 5105 mtx_lock(&lun->lun_lock); 5106 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5107 ctl_set_reservation_conflict(ctsio); 5108 goto bailout; 5109 } 5110 lun->flags |= CTL_LUN_RESERVED; 5111 lun->res_idx = residx; 5112 ctl_set_success(ctsio); 5113 5114bailout: 5115 mtx_unlock(&lun->lun_lock); 5116 ctl_done((union ctl_io *)ctsio); 5117 return (CTL_RETVAL_COMPLETE); 5118} 5119 5120int 5121ctl_start_stop(struct ctl_scsiio *ctsio) 5122{ 5123 struct scsi_start_stop_unit *cdb; 5124 struct ctl_lun *lun; 5125 int retval; 5126 5127 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5128 5129 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5130 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5131 5132 if ((lun->flags & CTL_LUN_PR_RESERVED) 5133 && ((cdb->how & SSS_START)==0)) { 5134 uint32_t residx; 5135 5136 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5137 if (ctl_get_prkey(lun, residx) == 0 5138 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5139 5140 ctl_set_reservation_conflict(ctsio); 5141 ctl_done((union ctl_io *)ctsio); 5142 return (CTL_RETVAL_COMPLETE); 5143 } 5144 } 5145 5146 if ((cdb->how & SSS_LOEJ) && 5147 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5148 ctl_set_invalid_field(ctsio, 5149 /*sks_valid*/ 1, 5150 /*command*/ 1, 5151 /*field*/ 4, 5152 /*bit_valid*/ 1, 5153 /*bit*/ 1); 5154 ctl_done((union ctl_io *)ctsio); 5155 return (CTL_RETVAL_COMPLETE); 5156 } 5157 5158 retval = lun->backend->config_write((union ctl_io *)ctsio); 5159 return (retval); 5160} 5161 5162int 5163ctl_prevent_allow(struct ctl_scsiio *ctsio) 5164{ 5165 struct ctl_lun *lun; 5166 int retval; 5167 5168 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5169 5170 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5171 5172 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5173 ctl_set_invalid_opcode(ctsio); 5174 ctl_done((union ctl_io *)ctsio); 5175 return (CTL_RETVAL_COMPLETE); 5176 } 5177 5178 retval = lun->backend->config_write((union ctl_io *)ctsio); 5179 return (retval); 5180} 5181 5182/* 5183 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5184 * we don't really do anything with the LBA and length fields if the user 5185 * passes them in. Instead we'll just flush out the cache for the entire 5186 * LUN. 5187 */ 5188int 5189ctl_sync_cache(struct ctl_scsiio *ctsio) 5190{ 5191 struct ctl_lun *lun; 5192 struct ctl_softc *softc; 5193 struct ctl_lba_len_flags *lbalen; 5194 uint64_t starting_lba; 5195 uint32_t block_count; 5196 int retval; 5197 uint8_t byte2; 5198 5199 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5200 5201 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5202 softc = lun->ctl_softc; 5203 retval = 0; 5204 5205 switch (ctsio->cdb[0]) { 5206 case SYNCHRONIZE_CACHE: { 5207 struct scsi_sync_cache *cdb; 5208 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5209 5210 starting_lba = scsi_4btoul(cdb->begin_lba); 5211 block_count = scsi_2btoul(cdb->lb_count); 5212 byte2 = cdb->byte2; 5213 break; 5214 } 5215 case SYNCHRONIZE_CACHE_16: { 5216 struct scsi_sync_cache_16 *cdb; 5217 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5218 5219 starting_lba = scsi_8btou64(cdb->begin_lba); 5220 block_count = scsi_4btoul(cdb->lb_count); 5221 byte2 = cdb->byte2; 5222 break; 5223 } 5224 default: 5225 ctl_set_invalid_opcode(ctsio); 5226 ctl_done((union ctl_io *)ctsio); 5227 goto bailout; 5228 break; /* NOTREACHED */ 5229 } 5230 5231 /* 5232 * We check the LBA and length, but don't do anything with them. 5233 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5234 * get flushed. This check will just help satisfy anyone who wants 5235 * to see an error for an out of range LBA. 5236 */ 5237 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5238 ctl_set_lba_out_of_range(ctsio); 5239 ctl_done((union ctl_io *)ctsio); 5240 goto bailout; 5241 } 5242 5243 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5244 lbalen->lba = starting_lba; 5245 lbalen->len = block_count; 5246 lbalen->flags = byte2; 5247 retval = lun->backend->config_write((union ctl_io *)ctsio); 5248 5249bailout: 5250 return (retval); 5251} 5252 5253int 5254ctl_format(struct ctl_scsiio *ctsio) 5255{ 5256 struct scsi_format *cdb; 5257 struct ctl_lun *lun; 5258 int length, defect_list_len; 5259 5260 CTL_DEBUG_PRINT(("ctl_format\n")); 5261 5262 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5263 5264 cdb = (struct scsi_format *)ctsio->cdb; 5265 5266 length = 0; 5267 if (cdb->byte2 & SF_FMTDATA) { 5268 if (cdb->byte2 & SF_LONGLIST) 5269 length = sizeof(struct scsi_format_header_long); 5270 else 5271 length = sizeof(struct scsi_format_header_short); 5272 } 5273 5274 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5275 && (length > 0)) { 5276 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5277 ctsio->kern_data_len = length; 5278 ctsio->kern_total_len = length; 5279 ctsio->kern_data_resid = 0; 5280 ctsio->kern_rel_offset = 0; 5281 ctsio->kern_sg_entries = 0; 5282 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5283 ctsio->be_move_done = ctl_config_move_done; 5284 ctl_datamove((union ctl_io *)ctsio); 5285 5286 return (CTL_RETVAL_COMPLETE); 5287 } 5288 5289 defect_list_len = 0; 5290 5291 if (cdb->byte2 & SF_FMTDATA) { 5292 if (cdb->byte2 & SF_LONGLIST) { 5293 struct scsi_format_header_long *header; 5294 5295 header = (struct scsi_format_header_long *) 5296 ctsio->kern_data_ptr; 5297 5298 defect_list_len = scsi_4btoul(header->defect_list_len); 5299 if (defect_list_len != 0) { 5300 ctl_set_invalid_field(ctsio, 5301 /*sks_valid*/ 1, 5302 /*command*/ 0, 5303 /*field*/ 2, 5304 /*bit_valid*/ 0, 5305 /*bit*/ 0); 5306 goto bailout; 5307 } 5308 } else { 5309 struct scsi_format_header_short *header; 5310 5311 header = (struct scsi_format_header_short *) 5312 ctsio->kern_data_ptr; 5313 5314 defect_list_len = scsi_2btoul(header->defect_list_len); 5315 if (defect_list_len != 0) { 5316 ctl_set_invalid_field(ctsio, 5317 /*sks_valid*/ 1, 5318 /*command*/ 0, 5319 /*field*/ 2, 5320 /*bit_valid*/ 0, 5321 /*bit*/ 0); 5322 goto bailout; 5323 } 5324 } 5325 } 5326 5327 ctl_set_success(ctsio); 5328bailout: 5329 5330 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5331 free(ctsio->kern_data_ptr, M_CTL); 5332 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5333 } 5334 5335 ctl_done((union ctl_io *)ctsio); 5336 return (CTL_RETVAL_COMPLETE); 5337} 5338 5339int 5340ctl_read_buffer(struct ctl_scsiio *ctsio) 5341{ 5342 struct ctl_lun *lun; 5343 uint64_t buffer_offset; 5344 uint32_t len; 5345 uint8_t byte2; 5346 static uint8_t descr[4]; 5347 static uint8_t echo_descr[4] = { 0 }; 5348 5349 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5350 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5351 switch (ctsio->cdb[0]) { 5352 case READ_BUFFER: { 5353 struct scsi_read_buffer *cdb; 5354 5355 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5356 buffer_offset = scsi_3btoul(cdb->offset); 5357 len = scsi_3btoul(cdb->length); 5358 byte2 = cdb->byte2; 5359 break; 5360 } 5361 case READ_BUFFER_16: { 5362 struct scsi_read_buffer_16 *cdb; 5363 5364 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5365 buffer_offset = scsi_8btou64(cdb->offset); 5366 len = scsi_4btoul(cdb->length); 5367 byte2 = cdb->byte2; 5368 break; 5369 } 5370 default: /* This shouldn't happen. */ 5371 ctl_set_invalid_opcode(ctsio); 5372 ctl_done((union ctl_io *)ctsio); 5373 return (CTL_RETVAL_COMPLETE); 5374 } 5375 5376 if ((byte2 & RWB_MODE) != RWB_MODE_DATA && 5377 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5378 (byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5379 ctl_set_invalid_field(ctsio, 5380 /*sks_valid*/ 1, 5381 /*command*/ 1, 5382 /*field*/ 1, 5383 /*bit_valid*/ 1, 5384 /*bit*/ 4); 5385 ctl_done((union ctl_io *)ctsio); 5386 return (CTL_RETVAL_COMPLETE); 5387 } 5388 5389 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5390 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5391 ctl_set_invalid_field(ctsio, 5392 /*sks_valid*/ 1, 5393 /*command*/ 1, 5394 /*field*/ 6, 5395 /*bit_valid*/ 0, 5396 /*bit*/ 0); 5397 ctl_done((union ctl_io *)ctsio); 5398 return (CTL_RETVAL_COMPLETE); 5399 } 5400 5401 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5402 descr[0] = 0; 5403 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5404 ctsio->kern_data_ptr = descr; 5405 len = min(len, sizeof(descr)); 5406 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5407 ctsio->kern_data_ptr = echo_descr; 5408 len = min(len, sizeof(echo_descr)); 5409 } else { 5410 if (lun->write_buffer == NULL) { 5411 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5412 M_CTL, M_WAITOK); 5413 } 5414 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5415 } 5416 ctsio->kern_data_len = len; 5417 ctsio->kern_total_len = len; 5418 ctsio->kern_data_resid = 0; 5419 ctsio->kern_rel_offset = 0; 5420 ctsio->kern_sg_entries = 0; 5421 ctl_set_success(ctsio); 5422 ctsio->be_move_done = ctl_config_move_done; 5423 ctl_datamove((union ctl_io *)ctsio); 5424 return (CTL_RETVAL_COMPLETE); 5425} 5426 5427int 5428ctl_write_buffer(struct ctl_scsiio *ctsio) 5429{ 5430 struct scsi_write_buffer *cdb; 5431 struct ctl_lun *lun; 5432 int buffer_offset, len; 5433 5434 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5435 5436 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5437 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5438 5439 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5440 ctl_set_invalid_field(ctsio, 5441 /*sks_valid*/ 1, 5442 /*command*/ 1, 5443 /*field*/ 1, 5444 /*bit_valid*/ 1, 5445 /*bit*/ 4); 5446 ctl_done((union ctl_io *)ctsio); 5447 return (CTL_RETVAL_COMPLETE); 5448 } 5449 5450 len = scsi_3btoul(cdb->length); 5451 buffer_offset = scsi_3btoul(cdb->offset); 5452 5453 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5454 ctl_set_invalid_field(ctsio, 5455 /*sks_valid*/ 1, 5456 /*command*/ 1, 5457 /*field*/ 6, 5458 /*bit_valid*/ 0, 5459 /*bit*/ 0); 5460 ctl_done((union ctl_io *)ctsio); 5461 return (CTL_RETVAL_COMPLETE); 5462 } 5463 5464 /* 5465 * If we've got a kernel request that hasn't been malloced yet, 5466 * malloc it and tell the caller the data buffer is here. 5467 */ 5468 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5469 if (lun->write_buffer == NULL) { 5470 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5471 M_CTL, M_WAITOK); 5472 } 5473 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5474 ctsio->kern_data_len = len; 5475 ctsio->kern_total_len = len; 5476 ctsio->kern_data_resid = 0; 5477 ctsio->kern_rel_offset = 0; 5478 ctsio->kern_sg_entries = 0; 5479 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5480 ctsio->be_move_done = ctl_config_move_done; 5481 ctl_datamove((union ctl_io *)ctsio); 5482 5483 return (CTL_RETVAL_COMPLETE); 5484 } 5485 5486 ctl_set_success(ctsio); 5487 ctl_done((union ctl_io *)ctsio); 5488 return (CTL_RETVAL_COMPLETE); 5489} 5490 5491int 5492ctl_write_same(struct ctl_scsiio *ctsio) 5493{ 5494 struct ctl_lun *lun; 5495 struct ctl_lba_len_flags *lbalen; 5496 uint64_t lba; 5497 uint32_t num_blocks; 5498 int len, retval; 5499 uint8_t byte2; 5500 5501 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5502 5503 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5504 5505 switch (ctsio->cdb[0]) { 5506 case WRITE_SAME_10: { 5507 struct scsi_write_same_10 *cdb; 5508 5509 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5510 5511 lba = scsi_4btoul(cdb->addr); 5512 num_blocks = scsi_2btoul(cdb->length); 5513 byte2 = cdb->byte2; 5514 break; 5515 } 5516 case WRITE_SAME_16: { 5517 struct scsi_write_same_16 *cdb; 5518 5519 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5520 5521 lba = scsi_8btou64(cdb->addr); 5522 num_blocks = scsi_4btoul(cdb->length); 5523 byte2 = cdb->byte2; 5524 break; 5525 } 5526 default: 5527 /* 5528 * We got a command we don't support. This shouldn't 5529 * happen, commands should be filtered out above us. 5530 */ 5531 ctl_set_invalid_opcode(ctsio); 5532 ctl_done((union ctl_io *)ctsio); 5533 5534 return (CTL_RETVAL_COMPLETE); 5535 break; /* NOTREACHED */ 5536 } 5537 5538 /* ANCHOR flag can be used only together with UNMAP */ 5539 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5540 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5541 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5542 ctl_done((union ctl_io *)ctsio); 5543 return (CTL_RETVAL_COMPLETE); 5544 } 5545 5546 /* 5547 * The first check is to make sure we're in bounds, the second 5548 * check is to catch wrap-around problems. If the lba + num blocks 5549 * is less than the lba, then we've wrapped around and the block 5550 * range is invalid anyway. 5551 */ 5552 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5553 || ((lba + num_blocks) < lba)) { 5554 ctl_set_lba_out_of_range(ctsio); 5555 ctl_done((union ctl_io *)ctsio); 5556 return (CTL_RETVAL_COMPLETE); 5557 } 5558 5559 /* Zero number of blocks means "to the last logical block" */ 5560 if (num_blocks == 0) { 5561 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5562 ctl_set_invalid_field(ctsio, 5563 /*sks_valid*/ 0, 5564 /*command*/ 1, 5565 /*field*/ 0, 5566 /*bit_valid*/ 0, 5567 /*bit*/ 0); 5568 ctl_done((union ctl_io *)ctsio); 5569 return (CTL_RETVAL_COMPLETE); 5570 } 5571 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5572 } 5573 5574 len = lun->be_lun->blocksize; 5575 5576 /* 5577 * If we've got a kernel request that hasn't been malloced yet, 5578 * malloc it and tell the caller the data buffer is here. 5579 */ 5580 if ((byte2 & SWS_NDOB) == 0 && 5581 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5582 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5583 ctsio->kern_data_len = len; 5584 ctsio->kern_total_len = len; 5585 ctsio->kern_data_resid = 0; 5586 ctsio->kern_rel_offset = 0; 5587 ctsio->kern_sg_entries = 0; 5588 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5589 ctsio->be_move_done = ctl_config_move_done; 5590 ctl_datamove((union ctl_io *)ctsio); 5591 5592 return (CTL_RETVAL_COMPLETE); 5593 } 5594 5595 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5596 lbalen->lba = lba; 5597 lbalen->len = num_blocks; 5598 lbalen->flags = byte2; 5599 retval = lun->backend->config_write((union ctl_io *)ctsio); 5600 5601 return (retval); 5602} 5603 5604int 5605ctl_unmap(struct ctl_scsiio *ctsio) 5606{ 5607 struct ctl_lun *lun; 5608 struct scsi_unmap *cdb; 5609 struct ctl_ptr_len_flags *ptrlen; 5610 struct scsi_unmap_header *hdr; 5611 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5612 uint64_t lba; 5613 uint32_t num_blocks; 5614 int len, retval; 5615 uint8_t byte2; 5616 5617 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5618 5619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5620 cdb = (struct scsi_unmap *)ctsio->cdb; 5621 5622 len = scsi_2btoul(cdb->length); 5623 byte2 = cdb->byte2; 5624 5625 /* 5626 * If we've got a kernel request that hasn't been malloced yet, 5627 * malloc it and tell the caller the data buffer is here. 5628 */ 5629 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5630 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5631 ctsio->kern_data_len = len; 5632 ctsio->kern_total_len = len; 5633 ctsio->kern_data_resid = 0; 5634 ctsio->kern_rel_offset = 0; 5635 ctsio->kern_sg_entries = 0; 5636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5637 ctsio->be_move_done = ctl_config_move_done; 5638 ctl_datamove((union ctl_io *)ctsio); 5639 5640 return (CTL_RETVAL_COMPLETE); 5641 } 5642 5643 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5644 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5645 if (len < sizeof (*hdr) || 5646 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5647 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5648 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5649 ctl_set_invalid_field(ctsio, 5650 /*sks_valid*/ 0, 5651 /*command*/ 0, 5652 /*field*/ 0, 5653 /*bit_valid*/ 0, 5654 /*bit*/ 0); 5655 goto done; 5656 } 5657 len = scsi_2btoul(hdr->desc_length); 5658 buf = (struct scsi_unmap_desc *)(hdr + 1); 5659 end = buf + len / sizeof(*buf); 5660 5661 endnz = buf; 5662 for (range = buf; range < end; range++) { 5663 lba = scsi_8btou64(range->lba); 5664 num_blocks = scsi_4btoul(range->length); 5665 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5666 || ((lba + num_blocks) < lba)) { 5667 ctl_set_lba_out_of_range(ctsio); 5668 ctl_done((union ctl_io *)ctsio); 5669 return (CTL_RETVAL_COMPLETE); 5670 } 5671 if (num_blocks != 0) 5672 endnz = range + 1; 5673 } 5674 5675 /* 5676 * Block backend can not handle zero last range. 5677 * Filter it out and return if there is nothing left. 5678 */ 5679 len = (uint8_t *)endnz - (uint8_t *)buf; 5680 if (len == 0) { 5681 ctl_set_success(ctsio); 5682 goto done; 5683 } 5684 5685 mtx_lock(&lun->lun_lock); 5686 ptrlen = (struct ctl_ptr_len_flags *) 5687 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5688 ptrlen->ptr = (void *)buf; 5689 ptrlen->len = len; 5690 ptrlen->flags = byte2; 5691 ctl_check_blocked(lun); 5692 mtx_unlock(&lun->lun_lock); 5693 5694 retval = lun->backend->config_write((union ctl_io *)ctsio); 5695 return (retval); 5696 5697done: 5698 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5699 free(ctsio->kern_data_ptr, M_CTL); 5700 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5701 } 5702 ctl_done((union ctl_io *)ctsio); 5703 return (CTL_RETVAL_COMPLETE); 5704} 5705 5706/* 5707 * Note that this function currently doesn't actually do anything inside 5708 * CTL to enforce things if the DQue bit is turned on. 5709 * 5710 * Also note that this function can't be used in the default case, because 5711 * the DQue bit isn't set in the changeable mask for the control mode page 5712 * anyway. This is just here as an example for how to implement a page 5713 * handler, and a placeholder in case we want to allow the user to turn 5714 * tagged queueing on and off. 5715 * 5716 * The D_SENSE bit handling is functional, however, and will turn 5717 * descriptor sense on and off for a given LUN. 5718 */ 5719int 5720ctl_control_page_handler(struct ctl_scsiio *ctsio, 5721 struct ctl_page_index *page_index, uint8_t *page_ptr) 5722{ 5723 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5724 struct ctl_lun *lun; 5725 int set_ua; 5726 uint32_t initidx; 5727 5728 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5729 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5730 set_ua = 0; 5731 5732 user_cp = (struct scsi_control_page *)page_ptr; 5733 current_cp = (struct scsi_control_page *) 5734 (page_index->page_data + (page_index->page_len * 5735 CTL_PAGE_CURRENT)); 5736 saved_cp = (struct scsi_control_page *) 5737 (page_index->page_data + (page_index->page_len * 5738 CTL_PAGE_SAVED)); 5739 5740 mtx_lock(&lun->lun_lock); 5741 if (((current_cp->rlec & SCP_DSENSE) == 0) 5742 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5743 /* 5744 * Descriptor sense is currently turned off and the user 5745 * wants to turn it on. 5746 */ 5747 current_cp->rlec |= SCP_DSENSE; 5748 saved_cp->rlec |= SCP_DSENSE; 5749 lun->flags |= CTL_LUN_SENSE_DESC; 5750 set_ua = 1; 5751 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5752 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5753 /* 5754 * Descriptor sense is currently turned on, and the user 5755 * wants to turn it off. 5756 */ 5757 current_cp->rlec &= ~SCP_DSENSE; 5758 saved_cp->rlec &= ~SCP_DSENSE; 5759 lun->flags &= ~CTL_LUN_SENSE_DESC; 5760 set_ua = 1; 5761 } 5762 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5763 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5764 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5765 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5766 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5767 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5768 set_ua = 1; 5769 } 5770 if ((current_cp->eca_and_aen & SCP_SWP) != 5771 (user_cp->eca_and_aen & SCP_SWP)) { 5772 current_cp->eca_and_aen &= ~SCP_SWP; 5773 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5774 saved_cp->eca_and_aen &= ~SCP_SWP; 5775 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5776 set_ua = 1; 5777 } 5778 if (set_ua != 0) 5779 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5780 mtx_unlock(&lun->lun_lock); 5781 if (set_ua) { 5782 ctl_isc_announce_mode(lun, 5783 ctl_get_initindex(&ctsio->io_hdr.nexus), 5784 page_index->page_code, page_index->subpage); 5785 } 5786 return (0); 5787} 5788 5789int 5790ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5791 struct ctl_page_index *page_index, uint8_t *page_ptr) 5792{ 5793 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5794 struct ctl_lun *lun; 5795 int set_ua; 5796 uint32_t initidx; 5797 5798 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5799 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5800 set_ua = 0; 5801 5802 user_cp = (struct scsi_caching_page *)page_ptr; 5803 current_cp = (struct scsi_caching_page *) 5804 (page_index->page_data + (page_index->page_len * 5805 CTL_PAGE_CURRENT)); 5806 saved_cp = (struct scsi_caching_page *) 5807 (page_index->page_data + (page_index->page_len * 5808 CTL_PAGE_SAVED)); 5809 5810 mtx_lock(&lun->lun_lock); 5811 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5812 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5813 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5814 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5815 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5816 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5817 set_ua = 1; 5818 } 5819 if (set_ua != 0) 5820 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5821 mtx_unlock(&lun->lun_lock); 5822 if (set_ua) { 5823 ctl_isc_announce_mode(lun, 5824 ctl_get_initindex(&ctsio->io_hdr.nexus), 5825 page_index->page_code, page_index->subpage); 5826 } 5827 return (0); 5828} 5829 5830int 5831ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5832 struct ctl_page_index *page_index, 5833 uint8_t *page_ptr) 5834{ 5835 uint8_t *c; 5836 int i; 5837 5838 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5839 ctl_time_io_secs = 5840 (c[0] << 8) | 5841 (c[1] << 0) | 5842 0; 5843 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5844 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5845 printf("page data:"); 5846 for (i=0; i<8; i++) 5847 printf(" %.2x",page_ptr[i]); 5848 printf("\n"); 5849 return (0); 5850} 5851 5852int 5853ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5854 struct ctl_page_index *page_index, 5855 int pc) 5856{ 5857 struct copan_debugconf_subpage *page; 5858 5859 page = (struct copan_debugconf_subpage *)page_index->page_data + 5860 (page_index->page_len * pc); 5861 5862 switch (pc) { 5863 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5864 case SMS_PAGE_CTRL_DEFAULT >> 6: 5865 case SMS_PAGE_CTRL_SAVED >> 6: 5866 /* 5867 * We don't update the changable or default bits for this page. 5868 */ 5869 break; 5870 case SMS_PAGE_CTRL_CURRENT >> 6: 5871 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5872 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5873 break; 5874 default: 5875 break; 5876 } 5877 return (0); 5878} 5879 5880 5881static int 5882ctl_do_mode_select(union ctl_io *io) 5883{ 5884 struct scsi_mode_page_header *page_header; 5885 struct ctl_page_index *page_index; 5886 struct ctl_scsiio *ctsio; 5887 int page_len, page_len_offset, page_len_size; 5888 union ctl_modepage_info *modepage_info; 5889 struct ctl_lun *lun; 5890 int *len_left, *len_used; 5891 int retval, i; 5892 5893 ctsio = &io->scsiio; 5894 page_index = NULL; 5895 page_len = 0; 5896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5897 5898 modepage_info = (union ctl_modepage_info *) 5899 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5900 len_left = &modepage_info->header.len_left; 5901 len_used = &modepage_info->header.len_used; 5902 5903do_next_page: 5904 5905 page_header = (struct scsi_mode_page_header *) 5906 (ctsio->kern_data_ptr + *len_used); 5907 5908 if (*len_left == 0) { 5909 free(ctsio->kern_data_ptr, M_CTL); 5910 ctl_set_success(ctsio); 5911 ctl_done((union ctl_io *)ctsio); 5912 return (CTL_RETVAL_COMPLETE); 5913 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5914 5915 free(ctsio->kern_data_ptr, M_CTL); 5916 ctl_set_param_len_error(ctsio); 5917 ctl_done((union ctl_io *)ctsio); 5918 return (CTL_RETVAL_COMPLETE); 5919 5920 } else if ((page_header->page_code & SMPH_SPF) 5921 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5922 5923 free(ctsio->kern_data_ptr, M_CTL); 5924 ctl_set_param_len_error(ctsio); 5925 ctl_done((union ctl_io *)ctsio); 5926 return (CTL_RETVAL_COMPLETE); 5927 } 5928 5929 5930 /* 5931 * XXX KDM should we do something with the block descriptor? 5932 */ 5933 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5934 page_index = &lun->mode_pages.index[i]; 5935 if (lun->be_lun->lun_type == T_DIRECT && 5936 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5937 continue; 5938 if (lun->be_lun->lun_type == T_PROCESSOR && 5939 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5940 continue; 5941 if (lun->be_lun->lun_type == T_CDROM && 5942 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5943 continue; 5944 5945 if ((page_index->page_code & SMPH_PC_MASK) != 5946 (page_header->page_code & SMPH_PC_MASK)) 5947 continue; 5948 5949 /* 5950 * If neither page has a subpage code, then we've got a 5951 * match. 5952 */ 5953 if (((page_index->page_code & SMPH_SPF) == 0) 5954 && ((page_header->page_code & SMPH_SPF) == 0)) { 5955 page_len = page_header->page_length; 5956 break; 5957 } 5958 5959 /* 5960 * If both pages have subpages, then the subpage numbers 5961 * have to match. 5962 */ 5963 if ((page_index->page_code & SMPH_SPF) 5964 && (page_header->page_code & SMPH_SPF)) { 5965 struct scsi_mode_page_header_sp *sph; 5966 5967 sph = (struct scsi_mode_page_header_sp *)page_header; 5968 if (page_index->subpage == sph->subpage) { 5969 page_len = scsi_2btoul(sph->page_length); 5970 break; 5971 } 5972 } 5973 } 5974 5975 /* 5976 * If we couldn't find the page, or if we don't have a mode select 5977 * handler for it, send back an error to the user. 5978 */ 5979 if ((i >= CTL_NUM_MODE_PAGES) 5980 || (page_index->select_handler == NULL)) { 5981 ctl_set_invalid_field(ctsio, 5982 /*sks_valid*/ 1, 5983 /*command*/ 0, 5984 /*field*/ *len_used, 5985 /*bit_valid*/ 0, 5986 /*bit*/ 0); 5987 free(ctsio->kern_data_ptr, M_CTL); 5988 ctl_done((union ctl_io *)ctsio); 5989 return (CTL_RETVAL_COMPLETE); 5990 } 5991 5992 if (page_index->page_code & SMPH_SPF) { 5993 page_len_offset = 2; 5994 page_len_size = 2; 5995 } else { 5996 page_len_size = 1; 5997 page_len_offset = 1; 5998 } 5999 6000 /* 6001 * If the length the initiator gives us isn't the one we specify in 6002 * the mode page header, or if they didn't specify enough data in 6003 * the CDB to avoid truncating this page, kick out the request. 6004 */ 6005 if ((page_len != (page_index->page_len - page_len_offset - 6006 page_len_size)) 6007 || (*len_left < page_index->page_len)) { 6008 6009 6010 ctl_set_invalid_field(ctsio, 6011 /*sks_valid*/ 1, 6012 /*command*/ 0, 6013 /*field*/ *len_used + page_len_offset, 6014 /*bit_valid*/ 0, 6015 /*bit*/ 0); 6016 free(ctsio->kern_data_ptr, M_CTL); 6017 ctl_done((union ctl_io *)ctsio); 6018 return (CTL_RETVAL_COMPLETE); 6019 } 6020 6021 /* 6022 * Run through the mode page, checking to make sure that the bits 6023 * the user changed are actually legal for him to change. 6024 */ 6025 for (i = 0; i < page_index->page_len; i++) { 6026 uint8_t *user_byte, *change_mask, *current_byte; 6027 int bad_bit; 6028 int j; 6029 6030 user_byte = (uint8_t *)page_header + i; 6031 change_mask = page_index->page_data + 6032 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6033 current_byte = page_index->page_data + 6034 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6035 6036 /* 6037 * Check to see whether the user set any bits in this byte 6038 * that he is not allowed to set. 6039 */ 6040 if ((*user_byte & ~(*change_mask)) == 6041 (*current_byte & ~(*change_mask))) 6042 continue; 6043 6044 /* 6045 * Go through bit by bit to determine which one is illegal. 6046 */ 6047 bad_bit = 0; 6048 for (j = 7; j >= 0; j--) { 6049 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6050 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6051 bad_bit = i; 6052 break; 6053 } 6054 } 6055 ctl_set_invalid_field(ctsio, 6056 /*sks_valid*/ 1, 6057 /*command*/ 0, 6058 /*field*/ *len_used + i, 6059 /*bit_valid*/ 1, 6060 /*bit*/ bad_bit); 6061 free(ctsio->kern_data_ptr, M_CTL); 6062 ctl_done((union ctl_io *)ctsio); 6063 return (CTL_RETVAL_COMPLETE); 6064 } 6065 6066 /* 6067 * Decrement these before we call the page handler, since we may 6068 * end up getting called back one way or another before the handler 6069 * returns to this context. 6070 */ 6071 *len_left -= page_index->page_len; 6072 *len_used += page_index->page_len; 6073 6074 retval = page_index->select_handler(ctsio, page_index, 6075 (uint8_t *)page_header); 6076 6077 /* 6078 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6079 * wait until this queued command completes to finish processing 6080 * the mode page. If it returns anything other than 6081 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6082 * already set the sense information, freed the data pointer, and 6083 * completed the io for us. 6084 */ 6085 if (retval != CTL_RETVAL_COMPLETE) 6086 goto bailout_no_done; 6087 6088 /* 6089 * If the initiator sent us more than one page, parse the next one. 6090 */ 6091 if (*len_left > 0) 6092 goto do_next_page; 6093 6094 ctl_set_success(ctsio); 6095 free(ctsio->kern_data_ptr, M_CTL); 6096 ctl_done((union ctl_io *)ctsio); 6097 6098bailout_no_done: 6099 6100 return (CTL_RETVAL_COMPLETE); 6101 6102} 6103 6104int 6105ctl_mode_select(struct ctl_scsiio *ctsio) 6106{ 6107 int param_len, pf, sp; 6108 int header_size, bd_len; 6109 union ctl_modepage_info *modepage_info; 6110 6111 switch (ctsio->cdb[0]) { 6112 case MODE_SELECT_6: { 6113 struct scsi_mode_select_6 *cdb; 6114 6115 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6116 6117 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6118 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6119 param_len = cdb->length; 6120 header_size = sizeof(struct scsi_mode_header_6); 6121 break; 6122 } 6123 case MODE_SELECT_10: { 6124 struct scsi_mode_select_10 *cdb; 6125 6126 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6127 6128 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6129 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6130 param_len = scsi_2btoul(cdb->length); 6131 header_size = sizeof(struct scsi_mode_header_10); 6132 break; 6133 } 6134 default: 6135 ctl_set_invalid_opcode(ctsio); 6136 ctl_done((union ctl_io *)ctsio); 6137 return (CTL_RETVAL_COMPLETE); 6138 } 6139 6140 /* 6141 * From SPC-3: 6142 * "A parameter list length of zero indicates that the Data-Out Buffer 6143 * shall be empty. This condition shall not be considered as an error." 6144 */ 6145 if (param_len == 0) { 6146 ctl_set_success(ctsio); 6147 ctl_done((union ctl_io *)ctsio); 6148 return (CTL_RETVAL_COMPLETE); 6149 } 6150 6151 /* 6152 * Since we'll hit this the first time through, prior to 6153 * allocation, we don't need to free a data buffer here. 6154 */ 6155 if (param_len < header_size) { 6156 ctl_set_param_len_error(ctsio); 6157 ctl_done((union ctl_io *)ctsio); 6158 return (CTL_RETVAL_COMPLETE); 6159 } 6160 6161 /* 6162 * Allocate the data buffer and grab the user's data. In theory, 6163 * we shouldn't have to sanity check the parameter list length here 6164 * because the maximum size is 64K. We should be able to malloc 6165 * that much without too many problems. 6166 */ 6167 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6168 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6169 ctsio->kern_data_len = param_len; 6170 ctsio->kern_total_len = param_len; 6171 ctsio->kern_data_resid = 0; 6172 ctsio->kern_rel_offset = 0; 6173 ctsio->kern_sg_entries = 0; 6174 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6175 ctsio->be_move_done = ctl_config_move_done; 6176 ctl_datamove((union ctl_io *)ctsio); 6177 6178 return (CTL_RETVAL_COMPLETE); 6179 } 6180 6181 switch (ctsio->cdb[0]) { 6182 case MODE_SELECT_6: { 6183 struct scsi_mode_header_6 *mh6; 6184 6185 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6186 bd_len = mh6->blk_desc_len; 6187 break; 6188 } 6189 case MODE_SELECT_10: { 6190 struct scsi_mode_header_10 *mh10; 6191 6192 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6193 bd_len = scsi_2btoul(mh10->blk_desc_len); 6194 break; 6195 } 6196 default: 6197 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6198 break; 6199 } 6200 6201 if (param_len < (header_size + bd_len)) { 6202 free(ctsio->kern_data_ptr, M_CTL); 6203 ctl_set_param_len_error(ctsio); 6204 ctl_done((union ctl_io *)ctsio); 6205 return (CTL_RETVAL_COMPLETE); 6206 } 6207 6208 /* 6209 * Set the IO_CONT flag, so that if this I/O gets passed to 6210 * ctl_config_write_done(), it'll get passed back to 6211 * ctl_do_mode_select() for further processing, or completion if 6212 * we're all done. 6213 */ 6214 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6215 ctsio->io_cont = ctl_do_mode_select; 6216 6217 modepage_info = (union ctl_modepage_info *) 6218 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6219 memset(modepage_info, 0, sizeof(*modepage_info)); 6220 modepage_info->header.len_left = param_len - header_size - bd_len; 6221 modepage_info->header.len_used = header_size + bd_len; 6222 6223 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6224} 6225 6226int 6227ctl_mode_sense(struct ctl_scsiio *ctsio) 6228{ 6229 struct ctl_lun *lun; 6230 int pc, page_code, dbd, llba, subpage; 6231 int alloc_len, page_len, header_len, total_len; 6232 struct scsi_mode_block_descr *block_desc; 6233 struct ctl_page_index *page_index; 6234 6235 dbd = 0; 6236 llba = 0; 6237 block_desc = NULL; 6238 6239 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6240 6241 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6242 switch (ctsio->cdb[0]) { 6243 case MODE_SENSE_6: { 6244 struct scsi_mode_sense_6 *cdb; 6245 6246 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6247 6248 header_len = sizeof(struct scsi_mode_hdr_6); 6249 if (cdb->byte2 & SMS_DBD) 6250 dbd = 1; 6251 else 6252 header_len += sizeof(struct scsi_mode_block_descr); 6253 6254 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6255 page_code = cdb->page & SMS_PAGE_CODE; 6256 subpage = cdb->subpage; 6257 alloc_len = cdb->length; 6258 break; 6259 } 6260 case MODE_SENSE_10: { 6261 struct scsi_mode_sense_10 *cdb; 6262 6263 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6264 6265 header_len = sizeof(struct scsi_mode_hdr_10); 6266 6267 if (cdb->byte2 & SMS_DBD) 6268 dbd = 1; 6269 else 6270 header_len += sizeof(struct scsi_mode_block_descr); 6271 if (cdb->byte2 & SMS10_LLBAA) 6272 llba = 1; 6273 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6274 page_code = cdb->page & SMS_PAGE_CODE; 6275 subpage = cdb->subpage; 6276 alloc_len = scsi_2btoul(cdb->length); 6277 break; 6278 } 6279 default: 6280 ctl_set_invalid_opcode(ctsio); 6281 ctl_done((union ctl_io *)ctsio); 6282 return (CTL_RETVAL_COMPLETE); 6283 break; /* NOTREACHED */ 6284 } 6285 6286 /* 6287 * We have to make a first pass through to calculate the size of 6288 * the pages that match the user's query. Then we allocate enough 6289 * memory to hold it, and actually copy the data into the buffer. 6290 */ 6291 switch (page_code) { 6292 case SMS_ALL_PAGES_PAGE: { 6293 int i; 6294 6295 page_len = 0; 6296 6297 /* 6298 * At the moment, values other than 0 and 0xff here are 6299 * reserved according to SPC-3. 6300 */ 6301 if ((subpage != SMS_SUBPAGE_PAGE_0) 6302 && (subpage != SMS_SUBPAGE_ALL)) { 6303 ctl_set_invalid_field(ctsio, 6304 /*sks_valid*/ 1, 6305 /*command*/ 1, 6306 /*field*/ 3, 6307 /*bit_valid*/ 0, 6308 /*bit*/ 0); 6309 ctl_done((union ctl_io *)ctsio); 6310 return (CTL_RETVAL_COMPLETE); 6311 } 6312 6313 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6314 page_index = &lun->mode_pages.index[i]; 6315 6316 /* Make sure the page is supported for this dev type */ 6317 if (lun->be_lun->lun_type == T_DIRECT && 6318 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6319 continue; 6320 if (lun->be_lun->lun_type == T_PROCESSOR && 6321 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6322 continue; 6323 if (lun->be_lun->lun_type == T_CDROM && 6324 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6325 continue; 6326 6327 /* 6328 * We don't use this subpage if the user didn't 6329 * request all subpages. 6330 */ 6331 if ((page_index->subpage != 0) 6332 && (subpage == SMS_SUBPAGE_PAGE_0)) 6333 continue; 6334 6335#if 0 6336 printf("found page %#x len %d\n", 6337 page_index->page_code & SMPH_PC_MASK, 6338 page_index->page_len); 6339#endif 6340 page_len += page_index->page_len; 6341 } 6342 break; 6343 } 6344 default: { 6345 int i; 6346 6347 page_len = 0; 6348 6349 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6350 page_index = &lun->mode_pages.index[i]; 6351 6352 /* Make sure the page is supported for this dev type */ 6353 if (lun->be_lun->lun_type == T_DIRECT && 6354 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6355 continue; 6356 if (lun->be_lun->lun_type == T_PROCESSOR && 6357 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6358 continue; 6359 if (lun->be_lun->lun_type == T_CDROM && 6360 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6361 continue; 6362 6363 /* Look for the right page code */ 6364 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6365 continue; 6366 6367 /* Look for the right subpage or the subpage wildcard*/ 6368 if ((page_index->subpage != subpage) 6369 && (subpage != SMS_SUBPAGE_ALL)) 6370 continue; 6371 6372#if 0 6373 printf("found page %#x len %d\n", 6374 page_index->page_code & SMPH_PC_MASK, 6375 page_index->page_len); 6376#endif 6377 6378 page_len += page_index->page_len; 6379 } 6380 6381 if (page_len == 0) { 6382 ctl_set_invalid_field(ctsio, 6383 /*sks_valid*/ 1, 6384 /*command*/ 1, 6385 /*field*/ 2, 6386 /*bit_valid*/ 1, 6387 /*bit*/ 5); 6388 ctl_done((union ctl_io *)ctsio); 6389 return (CTL_RETVAL_COMPLETE); 6390 } 6391 break; 6392 } 6393 } 6394 6395 total_len = header_len + page_len; 6396#if 0 6397 printf("header_len = %d, page_len = %d, total_len = %d\n", 6398 header_len, page_len, total_len); 6399#endif 6400 6401 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6402 ctsio->kern_sg_entries = 0; 6403 ctsio->kern_data_resid = 0; 6404 ctsio->kern_rel_offset = 0; 6405 if (total_len < alloc_len) { 6406 ctsio->residual = alloc_len - total_len; 6407 ctsio->kern_data_len = total_len; 6408 ctsio->kern_total_len = total_len; 6409 } else { 6410 ctsio->residual = 0; 6411 ctsio->kern_data_len = alloc_len; 6412 ctsio->kern_total_len = alloc_len; 6413 } 6414 6415 switch (ctsio->cdb[0]) { 6416 case MODE_SENSE_6: { 6417 struct scsi_mode_hdr_6 *header; 6418 6419 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6420 6421 header->datalen = MIN(total_len - 1, 254); 6422 if (lun->be_lun->lun_type == T_DIRECT) { 6423 header->dev_specific = 0x10; /* DPOFUA */ 6424 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6425 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6426 .eca_and_aen & SCP_SWP) != 0) 6427 header->dev_specific |= 0x80; /* WP */ 6428 } 6429 if (dbd) 6430 header->block_descr_len = 0; 6431 else 6432 header->block_descr_len = 6433 sizeof(struct scsi_mode_block_descr); 6434 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6435 break; 6436 } 6437 case MODE_SENSE_10: { 6438 struct scsi_mode_hdr_10 *header; 6439 int datalen; 6440 6441 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6442 6443 datalen = MIN(total_len - 2, 65533); 6444 scsi_ulto2b(datalen, header->datalen); 6445 if (lun->be_lun->lun_type == T_DIRECT) { 6446 header->dev_specific = 0x10; /* DPOFUA */ 6447 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6448 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6449 .eca_and_aen & SCP_SWP) != 0) 6450 header->dev_specific |= 0x80; /* WP */ 6451 } 6452 if (dbd) 6453 scsi_ulto2b(0, header->block_descr_len); 6454 else 6455 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6456 header->block_descr_len); 6457 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6458 break; 6459 } 6460 default: 6461 panic("invalid CDB type %#x", ctsio->cdb[0]); 6462 break; /* NOTREACHED */ 6463 } 6464 6465 /* 6466 * If we've got a disk, use its blocksize in the block 6467 * descriptor. Otherwise, just set it to 0. 6468 */ 6469 if (dbd == 0) { 6470 if (lun->be_lun->lun_type == T_DIRECT) 6471 scsi_ulto3b(lun->be_lun->blocksize, 6472 block_desc->block_len); 6473 else 6474 scsi_ulto3b(0, block_desc->block_len); 6475 } 6476 6477 switch (page_code) { 6478 case SMS_ALL_PAGES_PAGE: { 6479 int i, data_used; 6480 6481 data_used = header_len; 6482 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6483 struct ctl_page_index *page_index; 6484 6485 page_index = &lun->mode_pages.index[i]; 6486 if (lun->be_lun->lun_type == T_DIRECT && 6487 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6488 continue; 6489 if (lun->be_lun->lun_type == T_PROCESSOR && 6490 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6491 continue; 6492 if (lun->be_lun->lun_type == T_CDROM && 6493 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6494 continue; 6495 6496 /* 6497 * We don't use this subpage if the user didn't 6498 * request all subpages. We already checked (above) 6499 * to make sure the user only specified a subpage 6500 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6501 */ 6502 if ((page_index->subpage != 0) 6503 && (subpage == SMS_SUBPAGE_PAGE_0)) 6504 continue; 6505 6506 /* 6507 * Call the handler, if it exists, to update the 6508 * page to the latest values. 6509 */ 6510 if (page_index->sense_handler != NULL) 6511 page_index->sense_handler(ctsio, page_index,pc); 6512 6513 memcpy(ctsio->kern_data_ptr + data_used, 6514 page_index->page_data + 6515 (page_index->page_len * pc), 6516 page_index->page_len); 6517 data_used += page_index->page_len; 6518 } 6519 break; 6520 } 6521 default: { 6522 int i, data_used; 6523 6524 data_used = header_len; 6525 6526 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6527 struct ctl_page_index *page_index; 6528 6529 page_index = &lun->mode_pages.index[i]; 6530 6531 /* Look for the right page code */ 6532 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6533 continue; 6534 6535 /* Look for the right subpage or the subpage wildcard*/ 6536 if ((page_index->subpage != subpage) 6537 && (subpage != SMS_SUBPAGE_ALL)) 6538 continue; 6539 6540 /* Make sure the page is supported for this dev type */ 6541 if (lun->be_lun->lun_type == T_DIRECT && 6542 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6543 continue; 6544 if (lun->be_lun->lun_type == T_PROCESSOR && 6545 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6546 continue; 6547 if (lun->be_lun->lun_type == T_CDROM && 6548 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6549 continue; 6550 6551 /* 6552 * Call the handler, if it exists, to update the 6553 * page to the latest values. 6554 */ 6555 if (page_index->sense_handler != NULL) 6556 page_index->sense_handler(ctsio, page_index,pc); 6557 6558 memcpy(ctsio->kern_data_ptr + data_used, 6559 page_index->page_data + 6560 (page_index->page_len * pc), 6561 page_index->page_len); 6562 data_used += page_index->page_len; 6563 } 6564 break; 6565 } 6566 } 6567 6568 ctl_set_success(ctsio); 6569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6570 ctsio->be_move_done = ctl_config_move_done; 6571 ctl_datamove((union ctl_io *)ctsio); 6572 return (CTL_RETVAL_COMPLETE); 6573} 6574 6575int 6576ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6577 struct ctl_page_index *page_index, 6578 int pc) 6579{ 6580 struct ctl_lun *lun; 6581 struct scsi_log_param_header *phdr; 6582 uint8_t *data; 6583 uint64_t val; 6584 6585 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6586 data = page_index->page_data; 6587 6588 if (lun->backend->lun_attr != NULL && 6589 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6590 != UINT64_MAX) { 6591 phdr = (struct scsi_log_param_header *)data; 6592 scsi_ulto2b(0x0001, phdr->param_code); 6593 phdr->param_control = SLP_LBIN | SLP_LP; 6594 phdr->param_len = 8; 6595 data = (uint8_t *)(phdr + 1); 6596 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6597 data[4] = 0x02; /* per-pool */ 6598 data += phdr->param_len; 6599 } 6600 6601 if (lun->backend->lun_attr != NULL && 6602 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6603 != UINT64_MAX) { 6604 phdr = (struct scsi_log_param_header *)data; 6605 scsi_ulto2b(0x0002, phdr->param_code); 6606 phdr->param_control = SLP_LBIN | SLP_LP; 6607 phdr->param_len = 8; 6608 data = (uint8_t *)(phdr + 1); 6609 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6610 data[4] = 0x01; /* per-LUN */ 6611 data += phdr->param_len; 6612 } 6613 6614 if (lun->backend->lun_attr != NULL && 6615 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6616 != UINT64_MAX) { 6617 phdr = (struct scsi_log_param_header *)data; 6618 scsi_ulto2b(0x00f1, phdr->param_code); 6619 phdr->param_control = SLP_LBIN | SLP_LP; 6620 phdr->param_len = 8; 6621 data = (uint8_t *)(phdr + 1); 6622 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6623 data[4] = 0x02; /* per-pool */ 6624 data += phdr->param_len; 6625 } 6626 6627 if (lun->backend->lun_attr != NULL && 6628 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6629 != UINT64_MAX) { 6630 phdr = (struct scsi_log_param_header *)data; 6631 scsi_ulto2b(0x00f2, phdr->param_code); 6632 phdr->param_control = SLP_LBIN | SLP_LP; 6633 phdr->param_len = 8; 6634 data = (uint8_t *)(phdr + 1); 6635 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6636 data[4] = 0x02; /* per-pool */ 6637 data += phdr->param_len; 6638 } 6639 6640 page_index->page_len = data - page_index->page_data; 6641 return (0); 6642} 6643 6644int 6645ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6646 struct ctl_page_index *page_index, 6647 int pc) 6648{ 6649 struct ctl_lun *lun; 6650 struct stat_page *data; 6651 uint64_t rn, wn, rb, wb; 6652 struct bintime rt, wt; 6653 int i; 6654 6655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6656 data = (struct stat_page *)page_index->page_data; 6657 6658 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6659 data->sap.hdr.param_control = SLP_LBIN; 6660 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6661 sizeof(struct scsi_log_param_header); 6662 rn = wn = rb = wb = 0; 6663 bintime_clear(&rt); 6664 bintime_clear(&wt); 6665 for (i = 0; i < CTL_MAX_PORTS; i++) { 6666 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6667 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6668 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6669 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6670 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6671 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6672 } 6673 scsi_u64to8b(rn, data->sap.read_num); 6674 scsi_u64to8b(wn, data->sap.write_num); 6675 if (lun->stats.blocksize > 0) { 6676 scsi_u64to8b(wb / lun->stats.blocksize, 6677 data->sap.recvieved_lba); 6678 scsi_u64to8b(rb / lun->stats.blocksize, 6679 data->sap.transmitted_lba); 6680 } 6681 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6682 data->sap.read_int); 6683 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6684 data->sap.write_int); 6685 scsi_u64to8b(0, data->sap.weighted_num); 6686 scsi_u64to8b(0, data->sap.weighted_int); 6687 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6688 data->it.hdr.param_control = SLP_LBIN; 6689 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6690 sizeof(struct scsi_log_param_header); 6691#ifdef CTL_TIME_IO 6692 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6693#endif 6694 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6695 data->it.hdr.param_control = SLP_LBIN; 6696 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6697 sizeof(struct scsi_log_param_header); 6698 scsi_ulto4b(3, data->ti.exponent); 6699 scsi_ulto4b(1, data->ti.integer); 6700 6701 page_index->page_len = sizeof(*data); 6702 return (0); 6703} 6704 6705int 6706ctl_log_sense(struct ctl_scsiio *ctsio) 6707{ 6708 struct ctl_lun *lun; 6709 int i, pc, page_code, subpage; 6710 int alloc_len, total_len; 6711 struct ctl_page_index *page_index; 6712 struct scsi_log_sense *cdb; 6713 struct scsi_log_header *header; 6714 6715 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6716 6717 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6718 cdb = (struct scsi_log_sense *)ctsio->cdb; 6719 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6720 page_code = cdb->page & SLS_PAGE_CODE; 6721 subpage = cdb->subpage; 6722 alloc_len = scsi_2btoul(cdb->length); 6723 6724 page_index = NULL; 6725 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6726 page_index = &lun->log_pages.index[i]; 6727 6728 /* Look for the right page code */ 6729 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6730 continue; 6731 6732 /* Look for the right subpage or the subpage wildcard*/ 6733 if (page_index->subpage != subpage) 6734 continue; 6735 6736 break; 6737 } 6738 if (i >= CTL_NUM_LOG_PAGES) { 6739 ctl_set_invalid_field(ctsio, 6740 /*sks_valid*/ 1, 6741 /*command*/ 1, 6742 /*field*/ 2, 6743 /*bit_valid*/ 0, 6744 /*bit*/ 0); 6745 ctl_done((union ctl_io *)ctsio); 6746 return (CTL_RETVAL_COMPLETE); 6747 } 6748 6749 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6750 6751 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6752 ctsio->kern_sg_entries = 0; 6753 ctsio->kern_data_resid = 0; 6754 ctsio->kern_rel_offset = 0; 6755 if (total_len < alloc_len) { 6756 ctsio->residual = alloc_len - total_len; 6757 ctsio->kern_data_len = total_len; 6758 ctsio->kern_total_len = total_len; 6759 } else { 6760 ctsio->residual = 0; 6761 ctsio->kern_data_len = alloc_len; 6762 ctsio->kern_total_len = alloc_len; 6763 } 6764 6765 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6766 header->page = page_index->page_code; 6767 if (page_index->subpage) { 6768 header->page |= SL_SPF; 6769 header->subpage = page_index->subpage; 6770 } 6771 scsi_ulto2b(page_index->page_len, header->datalen); 6772 6773 /* 6774 * Call the handler, if it exists, to update the 6775 * page to the latest values. 6776 */ 6777 if (page_index->sense_handler != NULL) 6778 page_index->sense_handler(ctsio, page_index, pc); 6779 6780 memcpy(header + 1, page_index->page_data, page_index->page_len); 6781 6782 ctl_set_success(ctsio); 6783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6784 ctsio->be_move_done = ctl_config_move_done; 6785 ctl_datamove((union ctl_io *)ctsio); 6786 return (CTL_RETVAL_COMPLETE); 6787} 6788 6789int 6790ctl_read_capacity(struct ctl_scsiio *ctsio) 6791{ 6792 struct scsi_read_capacity *cdb; 6793 struct scsi_read_capacity_data *data; 6794 struct ctl_lun *lun; 6795 uint32_t lba; 6796 6797 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6798 6799 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6800 6801 lba = scsi_4btoul(cdb->addr); 6802 if (((cdb->pmi & SRC_PMI) == 0) 6803 && (lba != 0)) { 6804 ctl_set_invalid_field(/*ctsio*/ ctsio, 6805 /*sks_valid*/ 1, 6806 /*command*/ 1, 6807 /*field*/ 2, 6808 /*bit_valid*/ 0, 6809 /*bit*/ 0); 6810 ctl_done((union ctl_io *)ctsio); 6811 return (CTL_RETVAL_COMPLETE); 6812 } 6813 6814 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6815 6816 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6817 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6818 ctsio->residual = 0; 6819 ctsio->kern_data_len = sizeof(*data); 6820 ctsio->kern_total_len = sizeof(*data); 6821 ctsio->kern_data_resid = 0; 6822 ctsio->kern_rel_offset = 0; 6823 ctsio->kern_sg_entries = 0; 6824 6825 /* 6826 * If the maximum LBA is greater than 0xfffffffe, the user must 6827 * issue a SERVICE ACTION IN (16) command, with the read capacity 6828 * serivce action set. 6829 */ 6830 if (lun->be_lun->maxlba > 0xfffffffe) 6831 scsi_ulto4b(0xffffffff, data->addr); 6832 else 6833 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6834 6835 /* 6836 * XXX KDM this may not be 512 bytes... 6837 */ 6838 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6839 6840 ctl_set_success(ctsio); 6841 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6842 ctsio->be_move_done = ctl_config_move_done; 6843 ctl_datamove((union ctl_io *)ctsio); 6844 return (CTL_RETVAL_COMPLETE); 6845} 6846 6847int 6848ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6849{ 6850 struct scsi_read_capacity_16 *cdb; 6851 struct scsi_read_capacity_data_long *data; 6852 struct ctl_lun *lun; 6853 uint64_t lba; 6854 uint32_t alloc_len; 6855 6856 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6857 6858 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6859 6860 alloc_len = scsi_4btoul(cdb->alloc_len); 6861 lba = scsi_8btou64(cdb->addr); 6862 6863 if ((cdb->reladr & SRC16_PMI) 6864 && (lba != 0)) { 6865 ctl_set_invalid_field(/*ctsio*/ ctsio, 6866 /*sks_valid*/ 1, 6867 /*command*/ 1, 6868 /*field*/ 2, 6869 /*bit_valid*/ 0, 6870 /*bit*/ 0); 6871 ctl_done((union ctl_io *)ctsio); 6872 return (CTL_RETVAL_COMPLETE); 6873 } 6874 6875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6876 6877 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6878 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6879 6880 if (sizeof(*data) < alloc_len) { 6881 ctsio->residual = alloc_len - sizeof(*data); 6882 ctsio->kern_data_len = sizeof(*data); 6883 ctsio->kern_total_len = sizeof(*data); 6884 } else { 6885 ctsio->residual = 0; 6886 ctsio->kern_data_len = alloc_len; 6887 ctsio->kern_total_len = alloc_len; 6888 } 6889 ctsio->kern_data_resid = 0; 6890 ctsio->kern_rel_offset = 0; 6891 ctsio->kern_sg_entries = 0; 6892 6893 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6894 /* XXX KDM this may not be 512 bytes... */ 6895 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6896 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6897 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6898 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6899 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6900 6901 ctl_set_success(ctsio); 6902 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6903 ctsio->be_move_done = ctl_config_move_done; 6904 ctl_datamove((union ctl_io *)ctsio); 6905 return (CTL_RETVAL_COMPLETE); 6906} 6907 6908int 6909ctl_get_lba_status(struct ctl_scsiio *ctsio) 6910{ 6911 struct scsi_get_lba_status *cdb; 6912 struct scsi_get_lba_status_data *data; 6913 struct ctl_lun *lun; 6914 struct ctl_lba_len_flags *lbalen; 6915 uint64_t lba; 6916 uint32_t alloc_len, total_len; 6917 int retval; 6918 6919 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6920 6921 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6922 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6923 lba = scsi_8btou64(cdb->addr); 6924 alloc_len = scsi_4btoul(cdb->alloc_len); 6925 6926 if (lba > lun->be_lun->maxlba) { 6927 ctl_set_lba_out_of_range(ctsio); 6928 ctl_done((union ctl_io *)ctsio); 6929 return (CTL_RETVAL_COMPLETE); 6930 } 6931 6932 total_len = sizeof(*data) + sizeof(data->descr[0]); 6933 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6934 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6935 6936 if (total_len < alloc_len) { 6937 ctsio->residual = alloc_len - total_len; 6938 ctsio->kern_data_len = total_len; 6939 ctsio->kern_total_len = total_len; 6940 } else { 6941 ctsio->residual = 0; 6942 ctsio->kern_data_len = alloc_len; 6943 ctsio->kern_total_len = alloc_len; 6944 } 6945 ctsio->kern_data_resid = 0; 6946 ctsio->kern_rel_offset = 0; 6947 ctsio->kern_sg_entries = 0; 6948 6949 /* Fill dummy data in case backend can't tell anything. */ 6950 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6951 scsi_u64to8b(lba, data->descr[0].addr); 6952 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6953 data->descr[0].length); 6954 data->descr[0].status = 0; /* Mapped or unknown. */ 6955 6956 ctl_set_success(ctsio); 6957 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6958 ctsio->be_move_done = ctl_config_move_done; 6959 6960 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6961 lbalen->lba = lba; 6962 lbalen->len = total_len; 6963 lbalen->flags = 0; 6964 retval = lun->backend->config_read((union ctl_io *)ctsio); 6965 return (CTL_RETVAL_COMPLETE); 6966} 6967 6968int 6969ctl_read_defect(struct ctl_scsiio *ctsio) 6970{ 6971 struct scsi_read_defect_data_10 *ccb10; 6972 struct scsi_read_defect_data_12 *ccb12; 6973 struct scsi_read_defect_data_hdr_10 *data10; 6974 struct scsi_read_defect_data_hdr_12 *data12; 6975 uint32_t alloc_len, data_len; 6976 uint8_t format; 6977 6978 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 6979 6980 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 6981 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 6982 format = ccb10->format; 6983 alloc_len = scsi_2btoul(ccb10->alloc_length); 6984 data_len = sizeof(*data10); 6985 } else { 6986 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 6987 format = ccb12->format; 6988 alloc_len = scsi_4btoul(ccb12->alloc_length); 6989 data_len = sizeof(*data12); 6990 } 6991 if (alloc_len == 0) { 6992 ctl_set_success(ctsio); 6993 ctl_done((union ctl_io *)ctsio); 6994 return (CTL_RETVAL_COMPLETE); 6995 } 6996 6997 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 6998 if (data_len < alloc_len) { 6999 ctsio->residual = alloc_len - data_len; 7000 ctsio->kern_data_len = data_len; 7001 ctsio->kern_total_len = data_len; 7002 } else { 7003 ctsio->residual = 0; 7004 ctsio->kern_data_len = alloc_len; 7005 ctsio->kern_total_len = alloc_len; 7006 } 7007 ctsio->kern_data_resid = 0; 7008 ctsio->kern_rel_offset = 0; 7009 ctsio->kern_sg_entries = 0; 7010 7011 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7012 data10 = (struct scsi_read_defect_data_hdr_10 *) 7013 ctsio->kern_data_ptr; 7014 data10->format = format; 7015 scsi_ulto2b(0, data10->length); 7016 } else { 7017 data12 = (struct scsi_read_defect_data_hdr_12 *) 7018 ctsio->kern_data_ptr; 7019 data12->format = format; 7020 scsi_ulto2b(0, data12->generation); 7021 scsi_ulto4b(0, data12->length); 7022 } 7023 7024 ctl_set_success(ctsio); 7025 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7026 ctsio->be_move_done = ctl_config_move_done; 7027 ctl_datamove((union ctl_io *)ctsio); 7028 return (CTL_RETVAL_COMPLETE); 7029} 7030 7031int 7032ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7033{ 7034 struct scsi_maintenance_in *cdb; 7035 int retval; 7036 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7037 int num_target_port_groups, num_target_ports; 7038 struct ctl_lun *lun; 7039 struct ctl_softc *softc; 7040 struct ctl_port *port; 7041 struct scsi_target_group_data *rtg_ptr; 7042 struct scsi_target_group_data_extended *rtg_ext_ptr; 7043 struct scsi_target_port_group_descriptor *tpg_desc; 7044 7045 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7046 7047 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7048 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7049 softc = lun->ctl_softc; 7050 7051 retval = CTL_RETVAL_COMPLETE; 7052 7053 switch (cdb->byte2 & STG_PDF_MASK) { 7054 case STG_PDF_LENGTH: 7055 ext = 0; 7056 break; 7057 case STG_PDF_EXTENDED: 7058 ext = 1; 7059 break; 7060 default: 7061 ctl_set_invalid_field(/*ctsio*/ ctsio, 7062 /*sks_valid*/ 1, 7063 /*command*/ 1, 7064 /*field*/ 2, 7065 /*bit_valid*/ 1, 7066 /*bit*/ 5); 7067 ctl_done((union ctl_io *)ctsio); 7068 return(retval); 7069 } 7070 7071 if (softc->is_single) 7072 num_target_port_groups = 1; 7073 else 7074 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7075 num_target_ports = 0; 7076 mtx_lock(&softc->ctl_lock); 7077 STAILQ_FOREACH(port, &softc->port_list, links) { 7078 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7079 continue; 7080 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7081 continue; 7082 num_target_ports++; 7083 } 7084 mtx_unlock(&softc->ctl_lock); 7085 7086 if (ext) 7087 total_len = sizeof(struct scsi_target_group_data_extended); 7088 else 7089 total_len = sizeof(struct scsi_target_group_data); 7090 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7091 num_target_port_groups + 7092 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7093 7094 alloc_len = scsi_4btoul(cdb->length); 7095 7096 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7097 7098 ctsio->kern_sg_entries = 0; 7099 7100 if (total_len < alloc_len) { 7101 ctsio->residual = alloc_len - total_len; 7102 ctsio->kern_data_len = total_len; 7103 ctsio->kern_total_len = total_len; 7104 } else { 7105 ctsio->residual = 0; 7106 ctsio->kern_data_len = alloc_len; 7107 ctsio->kern_total_len = alloc_len; 7108 } 7109 ctsio->kern_data_resid = 0; 7110 ctsio->kern_rel_offset = 0; 7111 7112 if (ext) { 7113 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7114 ctsio->kern_data_ptr; 7115 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7116 rtg_ext_ptr->format_type = 0x10; 7117 rtg_ext_ptr->implicit_transition_time = 0; 7118 tpg_desc = &rtg_ext_ptr->groups[0]; 7119 } else { 7120 rtg_ptr = (struct scsi_target_group_data *) 7121 ctsio->kern_data_ptr; 7122 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7123 tpg_desc = &rtg_ptr->groups[0]; 7124 } 7125 7126 mtx_lock(&softc->ctl_lock); 7127 pg = softc->port_min / softc->port_cnt; 7128 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7129 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7130 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7131 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7132 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7133 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7134 else 7135 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7136 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7137 os = gs; 7138 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7139 } else 7140 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7141 for (g = 0; g < num_target_port_groups; g++) { 7142 tpg_desc->pref_state = (g == pg) ? gs : os; 7143 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7144 TPG_U_SUP | TPG_T_SUP; 7145 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7146 tpg_desc->status = TPG_IMPLICIT; 7147 pc = 0; 7148 STAILQ_FOREACH(port, &softc->port_list, links) { 7149 if (port->targ_port < g * softc->port_cnt || 7150 port->targ_port >= (g + 1) * softc->port_cnt) 7151 continue; 7152 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7153 continue; 7154 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7155 continue; 7156 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7157 relative_target_port_identifier); 7158 pc++; 7159 } 7160 tpg_desc->target_port_count = pc; 7161 tpg_desc = (struct scsi_target_port_group_descriptor *) 7162 &tpg_desc->descriptors[pc]; 7163 } 7164 mtx_unlock(&softc->ctl_lock); 7165 7166 ctl_set_success(ctsio); 7167 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7168 ctsio->be_move_done = ctl_config_move_done; 7169 ctl_datamove((union ctl_io *)ctsio); 7170 return(retval); 7171} 7172 7173int 7174ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7175{ 7176 struct ctl_lun *lun; 7177 struct scsi_report_supported_opcodes *cdb; 7178 const struct ctl_cmd_entry *entry, *sentry; 7179 struct scsi_report_supported_opcodes_all *all; 7180 struct scsi_report_supported_opcodes_descr *descr; 7181 struct scsi_report_supported_opcodes_one *one; 7182 int retval; 7183 int alloc_len, total_len; 7184 int opcode, service_action, i, j, num; 7185 7186 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7187 7188 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7189 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7190 7191 retval = CTL_RETVAL_COMPLETE; 7192 7193 opcode = cdb->requested_opcode; 7194 service_action = scsi_2btoul(cdb->requested_service_action); 7195 switch (cdb->options & RSO_OPTIONS_MASK) { 7196 case RSO_OPTIONS_ALL: 7197 num = 0; 7198 for (i = 0; i < 256; i++) { 7199 entry = &ctl_cmd_table[i]; 7200 if (entry->flags & CTL_CMD_FLAG_SA5) { 7201 for (j = 0; j < 32; j++) { 7202 sentry = &((const struct ctl_cmd_entry *) 7203 entry->execute)[j]; 7204 if (ctl_cmd_applicable( 7205 lun->be_lun->lun_type, sentry)) 7206 num++; 7207 } 7208 } else { 7209 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7210 entry)) 7211 num++; 7212 } 7213 } 7214 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7215 num * sizeof(struct scsi_report_supported_opcodes_descr); 7216 break; 7217 case RSO_OPTIONS_OC: 7218 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7219 ctl_set_invalid_field(/*ctsio*/ ctsio, 7220 /*sks_valid*/ 1, 7221 /*command*/ 1, 7222 /*field*/ 2, 7223 /*bit_valid*/ 1, 7224 /*bit*/ 2); 7225 ctl_done((union ctl_io *)ctsio); 7226 return (CTL_RETVAL_COMPLETE); 7227 } 7228 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7229 break; 7230 case RSO_OPTIONS_OC_SA: 7231 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7232 service_action >= 32) { 7233 ctl_set_invalid_field(/*ctsio*/ ctsio, 7234 /*sks_valid*/ 1, 7235 /*command*/ 1, 7236 /*field*/ 2, 7237 /*bit_valid*/ 1, 7238 /*bit*/ 2); 7239 ctl_done((union ctl_io *)ctsio); 7240 return (CTL_RETVAL_COMPLETE); 7241 } 7242 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7243 break; 7244 default: 7245 ctl_set_invalid_field(/*ctsio*/ ctsio, 7246 /*sks_valid*/ 1, 7247 /*command*/ 1, 7248 /*field*/ 2, 7249 /*bit_valid*/ 1, 7250 /*bit*/ 2); 7251 ctl_done((union ctl_io *)ctsio); 7252 return (CTL_RETVAL_COMPLETE); 7253 } 7254 7255 alloc_len = scsi_4btoul(cdb->length); 7256 7257 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7258 7259 ctsio->kern_sg_entries = 0; 7260 7261 if (total_len < alloc_len) { 7262 ctsio->residual = alloc_len - total_len; 7263 ctsio->kern_data_len = total_len; 7264 ctsio->kern_total_len = total_len; 7265 } else { 7266 ctsio->residual = 0; 7267 ctsio->kern_data_len = alloc_len; 7268 ctsio->kern_total_len = alloc_len; 7269 } 7270 ctsio->kern_data_resid = 0; 7271 ctsio->kern_rel_offset = 0; 7272 7273 switch (cdb->options & RSO_OPTIONS_MASK) { 7274 case RSO_OPTIONS_ALL: 7275 all = (struct scsi_report_supported_opcodes_all *) 7276 ctsio->kern_data_ptr; 7277 num = 0; 7278 for (i = 0; i < 256; i++) { 7279 entry = &ctl_cmd_table[i]; 7280 if (entry->flags & CTL_CMD_FLAG_SA5) { 7281 for (j = 0; j < 32; j++) { 7282 sentry = &((const struct ctl_cmd_entry *) 7283 entry->execute)[j]; 7284 if (!ctl_cmd_applicable( 7285 lun->be_lun->lun_type, sentry)) 7286 continue; 7287 descr = &all->descr[num++]; 7288 descr->opcode = i; 7289 scsi_ulto2b(j, descr->service_action); 7290 descr->flags = RSO_SERVACTV; 7291 scsi_ulto2b(sentry->length, 7292 descr->cdb_length); 7293 } 7294 } else { 7295 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7296 entry)) 7297 continue; 7298 descr = &all->descr[num++]; 7299 descr->opcode = i; 7300 scsi_ulto2b(0, descr->service_action); 7301 descr->flags = 0; 7302 scsi_ulto2b(entry->length, descr->cdb_length); 7303 } 7304 } 7305 scsi_ulto4b( 7306 num * sizeof(struct scsi_report_supported_opcodes_descr), 7307 all->length); 7308 break; 7309 case RSO_OPTIONS_OC: 7310 one = (struct scsi_report_supported_opcodes_one *) 7311 ctsio->kern_data_ptr; 7312 entry = &ctl_cmd_table[opcode]; 7313 goto fill_one; 7314 case RSO_OPTIONS_OC_SA: 7315 one = (struct scsi_report_supported_opcodes_one *) 7316 ctsio->kern_data_ptr; 7317 entry = &ctl_cmd_table[opcode]; 7318 entry = &((const struct ctl_cmd_entry *) 7319 entry->execute)[service_action]; 7320fill_one: 7321 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7322 one->support = 3; 7323 scsi_ulto2b(entry->length, one->cdb_length); 7324 one->cdb_usage[0] = opcode; 7325 memcpy(&one->cdb_usage[1], entry->usage, 7326 entry->length - 1); 7327 } else 7328 one->support = 1; 7329 break; 7330 } 7331 7332 ctl_set_success(ctsio); 7333 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7334 ctsio->be_move_done = ctl_config_move_done; 7335 ctl_datamove((union ctl_io *)ctsio); 7336 return(retval); 7337} 7338 7339int 7340ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7341{ 7342 struct scsi_report_supported_tmf *cdb; 7343 struct scsi_report_supported_tmf_data *data; 7344 int retval; 7345 int alloc_len, total_len; 7346 7347 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7348 7349 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7350 7351 retval = CTL_RETVAL_COMPLETE; 7352 7353 total_len = sizeof(struct scsi_report_supported_tmf_data); 7354 alloc_len = scsi_4btoul(cdb->length); 7355 7356 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7357 7358 ctsio->kern_sg_entries = 0; 7359 7360 if (total_len < alloc_len) { 7361 ctsio->residual = alloc_len - total_len; 7362 ctsio->kern_data_len = total_len; 7363 ctsio->kern_total_len = total_len; 7364 } else { 7365 ctsio->residual = 0; 7366 ctsio->kern_data_len = alloc_len; 7367 ctsio->kern_total_len = alloc_len; 7368 } 7369 ctsio->kern_data_resid = 0; 7370 ctsio->kern_rel_offset = 0; 7371 7372 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7373 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7374 RST_TRS; 7375 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7376 7377 ctl_set_success(ctsio); 7378 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7379 ctsio->be_move_done = ctl_config_move_done; 7380 ctl_datamove((union ctl_io *)ctsio); 7381 return (retval); 7382} 7383 7384int 7385ctl_report_timestamp(struct ctl_scsiio *ctsio) 7386{ 7387 struct scsi_report_timestamp *cdb; 7388 struct scsi_report_timestamp_data *data; 7389 struct timeval tv; 7390 int64_t timestamp; 7391 int retval; 7392 int alloc_len, total_len; 7393 7394 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7395 7396 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7397 7398 retval = CTL_RETVAL_COMPLETE; 7399 7400 total_len = sizeof(struct scsi_report_timestamp_data); 7401 alloc_len = scsi_4btoul(cdb->length); 7402 7403 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7404 7405 ctsio->kern_sg_entries = 0; 7406 7407 if (total_len < alloc_len) { 7408 ctsio->residual = alloc_len - total_len; 7409 ctsio->kern_data_len = total_len; 7410 ctsio->kern_total_len = total_len; 7411 } else { 7412 ctsio->residual = 0; 7413 ctsio->kern_data_len = alloc_len; 7414 ctsio->kern_total_len = alloc_len; 7415 } 7416 ctsio->kern_data_resid = 0; 7417 ctsio->kern_rel_offset = 0; 7418 7419 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7420 scsi_ulto2b(sizeof(*data) - 2, data->length); 7421 data->origin = RTS_ORIG_OUTSIDE; 7422 getmicrotime(&tv); 7423 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7424 scsi_ulto4b(timestamp >> 16, data->timestamp); 7425 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7426 7427 ctl_set_success(ctsio); 7428 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7429 ctsio->be_move_done = ctl_config_move_done; 7430 ctl_datamove((union ctl_io *)ctsio); 7431 return (retval); 7432} 7433 7434int 7435ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7436{ 7437 struct scsi_per_res_in *cdb; 7438 int alloc_len, total_len = 0; 7439 /* struct scsi_per_res_in_rsrv in_data; */ 7440 struct ctl_lun *lun; 7441 struct ctl_softc *softc; 7442 uint64_t key; 7443 7444 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7445 7446 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7447 7448 alloc_len = scsi_2btoul(cdb->length); 7449 7450 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7451 softc = lun->ctl_softc; 7452 7453retry: 7454 mtx_lock(&lun->lun_lock); 7455 switch (cdb->action) { 7456 case SPRI_RK: /* read keys */ 7457 total_len = sizeof(struct scsi_per_res_in_keys) + 7458 lun->pr_key_count * 7459 sizeof(struct scsi_per_res_key); 7460 break; 7461 case SPRI_RR: /* read reservation */ 7462 if (lun->flags & CTL_LUN_PR_RESERVED) 7463 total_len = sizeof(struct scsi_per_res_in_rsrv); 7464 else 7465 total_len = sizeof(struct scsi_per_res_in_header); 7466 break; 7467 case SPRI_RC: /* report capabilities */ 7468 total_len = sizeof(struct scsi_per_res_cap); 7469 break; 7470 case SPRI_RS: /* read full status */ 7471 total_len = sizeof(struct scsi_per_res_in_header) + 7472 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7473 lun->pr_key_count; 7474 break; 7475 default: 7476 panic("Invalid PR type %x", cdb->action); 7477 } 7478 mtx_unlock(&lun->lun_lock); 7479 7480 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7481 7482 if (total_len < alloc_len) { 7483 ctsio->residual = alloc_len - total_len; 7484 ctsio->kern_data_len = total_len; 7485 ctsio->kern_total_len = total_len; 7486 } else { 7487 ctsio->residual = 0; 7488 ctsio->kern_data_len = alloc_len; 7489 ctsio->kern_total_len = alloc_len; 7490 } 7491 7492 ctsio->kern_data_resid = 0; 7493 ctsio->kern_rel_offset = 0; 7494 ctsio->kern_sg_entries = 0; 7495 7496 mtx_lock(&lun->lun_lock); 7497 switch (cdb->action) { 7498 case SPRI_RK: { // read keys 7499 struct scsi_per_res_in_keys *res_keys; 7500 int i, key_count; 7501 7502 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7503 7504 /* 7505 * We had to drop the lock to allocate our buffer, which 7506 * leaves time for someone to come in with another 7507 * persistent reservation. (That is unlikely, though, 7508 * since this should be the only persistent reservation 7509 * command active right now.) 7510 */ 7511 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7512 (lun->pr_key_count * 7513 sizeof(struct scsi_per_res_key)))){ 7514 mtx_unlock(&lun->lun_lock); 7515 free(ctsio->kern_data_ptr, M_CTL); 7516 printf("%s: reservation length changed, retrying\n", 7517 __func__); 7518 goto retry; 7519 } 7520 7521 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7522 7523 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7524 lun->pr_key_count, res_keys->header.length); 7525 7526 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7527 if ((key = ctl_get_prkey(lun, i)) == 0) 7528 continue; 7529 7530 /* 7531 * We used lun->pr_key_count to calculate the 7532 * size to allocate. If it turns out the number of 7533 * initiators with the registered flag set is 7534 * larger than that (i.e. they haven't been kept in 7535 * sync), we've got a problem. 7536 */ 7537 if (key_count >= lun->pr_key_count) { 7538 key_count++; 7539 continue; 7540 } 7541 scsi_u64to8b(key, res_keys->keys[key_count].key); 7542 key_count++; 7543 } 7544 break; 7545 } 7546 case SPRI_RR: { // read reservation 7547 struct scsi_per_res_in_rsrv *res; 7548 int tmp_len, header_only; 7549 7550 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7551 7552 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7553 7554 if (lun->flags & CTL_LUN_PR_RESERVED) 7555 { 7556 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7557 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7558 res->header.length); 7559 header_only = 0; 7560 } else { 7561 tmp_len = sizeof(struct scsi_per_res_in_header); 7562 scsi_ulto4b(0, res->header.length); 7563 header_only = 1; 7564 } 7565 7566 /* 7567 * We had to drop the lock to allocate our buffer, which 7568 * leaves time for someone to come in with another 7569 * persistent reservation. (That is unlikely, though, 7570 * since this should be the only persistent reservation 7571 * command active right now.) 7572 */ 7573 if (tmp_len != total_len) { 7574 mtx_unlock(&lun->lun_lock); 7575 free(ctsio->kern_data_ptr, M_CTL); 7576 printf("%s: reservation status changed, retrying\n", 7577 __func__); 7578 goto retry; 7579 } 7580 7581 /* 7582 * No reservation held, so we're done. 7583 */ 7584 if (header_only != 0) 7585 break; 7586 7587 /* 7588 * If the registration is an All Registrants type, the key 7589 * is 0, since it doesn't really matter. 7590 */ 7591 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7592 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7593 res->data.reservation); 7594 } 7595 res->data.scopetype = lun->res_type; 7596 break; 7597 } 7598 case SPRI_RC: //report capabilities 7599 { 7600 struct scsi_per_res_cap *res_cap; 7601 uint16_t type_mask; 7602 7603 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7604 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7605 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7606 type_mask = SPRI_TM_WR_EX_AR | 7607 SPRI_TM_EX_AC_RO | 7608 SPRI_TM_WR_EX_RO | 7609 SPRI_TM_EX_AC | 7610 SPRI_TM_WR_EX | 7611 SPRI_TM_EX_AC_AR; 7612 scsi_ulto2b(type_mask, res_cap->type_mask); 7613 break; 7614 } 7615 case SPRI_RS: { // read full status 7616 struct scsi_per_res_in_full *res_status; 7617 struct scsi_per_res_in_full_desc *res_desc; 7618 struct ctl_port *port; 7619 int i, len; 7620 7621 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7622 7623 /* 7624 * We had to drop the lock to allocate our buffer, which 7625 * leaves time for someone to come in with another 7626 * persistent reservation. (That is unlikely, though, 7627 * since this should be the only persistent reservation 7628 * command active right now.) 7629 */ 7630 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7631 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7632 lun->pr_key_count)){ 7633 mtx_unlock(&lun->lun_lock); 7634 free(ctsio->kern_data_ptr, M_CTL); 7635 printf("%s: reservation length changed, retrying\n", 7636 __func__); 7637 goto retry; 7638 } 7639 7640 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7641 7642 res_desc = &res_status->desc[0]; 7643 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7644 if ((key = ctl_get_prkey(lun, i)) == 0) 7645 continue; 7646 7647 scsi_u64to8b(key, res_desc->res_key.key); 7648 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7649 (lun->pr_res_idx == i || 7650 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7651 res_desc->flags = SPRI_FULL_R_HOLDER; 7652 res_desc->scopetype = lun->res_type; 7653 } 7654 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7655 res_desc->rel_trgt_port_id); 7656 len = 0; 7657 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7658 if (port != NULL) 7659 len = ctl_create_iid(port, 7660 i % CTL_MAX_INIT_PER_PORT, 7661 res_desc->transport_id); 7662 scsi_ulto4b(len, res_desc->additional_length); 7663 res_desc = (struct scsi_per_res_in_full_desc *) 7664 &res_desc->transport_id[len]; 7665 } 7666 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7667 res_status->header.length); 7668 break; 7669 } 7670 default: 7671 /* 7672 * This is a bug, because we just checked for this above, 7673 * and should have returned an error. 7674 */ 7675 panic("Invalid PR type %x", cdb->action); 7676 break; /* NOTREACHED */ 7677 } 7678 mtx_unlock(&lun->lun_lock); 7679 7680 ctl_set_success(ctsio); 7681 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7682 ctsio->be_move_done = ctl_config_move_done; 7683 ctl_datamove((union ctl_io *)ctsio); 7684 return (CTL_RETVAL_COMPLETE); 7685} 7686 7687/* 7688 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7689 * it should return. 7690 */ 7691static int 7692ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7693 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7694 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7695 struct scsi_per_res_out_parms* param) 7696{ 7697 union ctl_ha_msg persis_io; 7698 int i; 7699 7700 mtx_lock(&lun->lun_lock); 7701 if (sa_res_key == 0) { 7702 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7703 /* validate scope and type */ 7704 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7705 SPR_LU_SCOPE) { 7706 mtx_unlock(&lun->lun_lock); 7707 ctl_set_invalid_field(/*ctsio*/ ctsio, 7708 /*sks_valid*/ 1, 7709 /*command*/ 1, 7710 /*field*/ 2, 7711 /*bit_valid*/ 1, 7712 /*bit*/ 4); 7713 ctl_done((union ctl_io *)ctsio); 7714 return (1); 7715 } 7716 7717 if (type>8 || type==2 || type==4 || type==0) { 7718 mtx_unlock(&lun->lun_lock); 7719 ctl_set_invalid_field(/*ctsio*/ ctsio, 7720 /*sks_valid*/ 1, 7721 /*command*/ 1, 7722 /*field*/ 2, 7723 /*bit_valid*/ 1, 7724 /*bit*/ 0); 7725 ctl_done((union ctl_io *)ctsio); 7726 return (1); 7727 } 7728 7729 /* 7730 * Unregister everybody else and build UA for 7731 * them 7732 */ 7733 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7734 if (i == residx || ctl_get_prkey(lun, i) == 0) 7735 continue; 7736 7737 ctl_clr_prkey(lun, i); 7738 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7739 } 7740 lun->pr_key_count = 1; 7741 lun->res_type = type; 7742 if (lun->res_type != SPR_TYPE_WR_EX_AR 7743 && lun->res_type != SPR_TYPE_EX_AC_AR) 7744 lun->pr_res_idx = residx; 7745 lun->PRGeneration++; 7746 mtx_unlock(&lun->lun_lock); 7747 7748 /* send msg to other side */ 7749 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7750 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7751 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7752 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7753 persis_io.pr.pr_info.res_type = type; 7754 memcpy(persis_io.pr.pr_info.sa_res_key, 7755 param->serv_act_res_key, 7756 sizeof(param->serv_act_res_key)); 7757 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7758 sizeof(persis_io.pr), M_WAITOK); 7759 } else { 7760 /* not all registrants */ 7761 mtx_unlock(&lun->lun_lock); 7762 free(ctsio->kern_data_ptr, M_CTL); 7763 ctl_set_invalid_field(ctsio, 7764 /*sks_valid*/ 1, 7765 /*command*/ 0, 7766 /*field*/ 8, 7767 /*bit_valid*/ 0, 7768 /*bit*/ 0); 7769 ctl_done((union ctl_io *)ctsio); 7770 return (1); 7771 } 7772 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7773 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7774 int found = 0; 7775 7776 if (res_key == sa_res_key) { 7777 /* special case */ 7778 /* 7779 * The spec implies this is not good but doesn't 7780 * say what to do. There are two choices either 7781 * generate a res conflict or check condition 7782 * with illegal field in parameter data. Since 7783 * that is what is done when the sa_res_key is 7784 * zero I'll take that approach since this has 7785 * to do with the sa_res_key. 7786 */ 7787 mtx_unlock(&lun->lun_lock); 7788 free(ctsio->kern_data_ptr, M_CTL); 7789 ctl_set_invalid_field(ctsio, 7790 /*sks_valid*/ 1, 7791 /*command*/ 0, 7792 /*field*/ 8, 7793 /*bit_valid*/ 0, 7794 /*bit*/ 0); 7795 ctl_done((union ctl_io *)ctsio); 7796 return (1); 7797 } 7798 7799 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7800 if (ctl_get_prkey(lun, i) != sa_res_key) 7801 continue; 7802 7803 found = 1; 7804 ctl_clr_prkey(lun, i); 7805 lun->pr_key_count--; 7806 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7807 } 7808 if (!found) { 7809 mtx_unlock(&lun->lun_lock); 7810 free(ctsio->kern_data_ptr, M_CTL); 7811 ctl_set_reservation_conflict(ctsio); 7812 ctl_done((union ctl_io *)ctsio); 7813 return (CTL_RETVAL_COMPLETE); 7814 } 7815 lun->PRGeneration++; 7816 mtx_unlock(&lun->lun_lock); 7817 7818 /* send msg to other side */ 7819 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7820 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7821 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7822 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7823 persis_io.pr.pr_info.res_type = type; 7824 memcpy(persis_io.pr.pr_info.sa_res_key, 7825 param->serv_act_res_key, 7826 sizeof(param->serv_act_res_key)); 7827 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7828 sizeof(persis_io.pr), M_WAITOK); 7829 } else { 7830 /* Reserved but not all registrants */ 7831 /* sa_res_key is res holder */ 7832 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7833 /* validate scope and type */ 7834 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7835 SPR_LU_SCOPE) { 7836 mtx_unlock(&lun->lun_lock); 7837 ctl_set_invalid_field(/*ctsio*/ ctsio, 7838 /*sks_valid*/ 1, 7839 /*command*/ 1, 7840 /*field*/ 2, 7841 /*bit_valid*/ 1, 7842 /*bit*/ 4); 7843 ctl_done((union ctl_io *)ctsio); 7844 return (1); 7845 } 7846 7847 if (type>8 || type==2 || type==4 || type==0) { 7848 mtx_unlock(&lun->lun_lock); 7849 ctl_set_invalid_field(/*ctsio*/ ctsio, 7850 /*sks_valid*/ 1, 7851 /*command*/ 1, 7852 /*field*/ 2, 7853 /*bit_valid*/ 1, 7854 /*bit*/ 0); 7855 ctl_done((union ctl_io *)ctsio); 7856 return (1); 7857 } 7858 7859 /* 7860 * Do the following: 7861 * if sa_res_key != res_key remove all 7862 * registrants w/sa_res_key and generate UA 7863 * for these registrants(Registrations 7864 * Preempted) if it wasn't an exclusive 7865 * reservation generate UA(Reservations 7866 * Preempted) for all other registered nexuses 7867 * if the type has changed. Establish the new 7868 * reservation and holder. If res_key and 7869 * sa_res_key are the same do the above 7870 * except don't unregister the res holder. 7871 */ 7872 7873 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7874 if (i == residx || ctl_get_prkey(lun, i) == 0) 7875 continue; 7876 7877 if (sa_res_key == ctl_get_prkey(lun, i)) { 7878 ctl_clr_prkey(lun, i); 7879 lun->pr_key_count--; 7880 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7881 } else if (type != lun->res_type 7882 && (lun->res_type == SPR_TYPE_WR_EX_RO 7883 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7884 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7885 } 7886 } 7887 lun->res_type = type; 7888 if (lun->res_type != SPR_TYPE_WR_EX_AR 7889 && lun->res_type != SPR_TYPE_EX_AC_AR) 7890 lun->pr_res_idx = residx; 7891 else 7892 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7893 lun->PRGeneration++; 7894 mtx_unlock(&lun->lun_lock); 7895 7896 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7897 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7898 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7899 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7900 persis_io.pr.pr_info.res_type = type; 7901 memcpy(persis_io.pr.pr_info.sa_res_key, 7902 param->serv_act_res_key, 7903 sizeof(param->serv_act_res_key)); 7904 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7905 sizeof(persis_io.pr), M_WAITOK); 7906 } else { 7907 /* 7908 * sa_res_key is not the res holder just 7909 * remove registrants 7910 */ 7911 int found=0; 7912 7913 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7914 if (sa_res_key != ctl_get_prkey(lun, i)) 7915 continue; 7916 7917 found = 1; 7918 ctl_clr_prkey(lun, i); 7919 lun->pr_key_count--; 7920 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7921 } 7922 7923 if (!found) { 7924 mtx_unlock(&lun->lun_lock); 7925 free(ctsio->kern_data_ptr, M_CTL); 7926 ctl_set_reservation_conflict(ctsio); 7927 ctl_done((union ctl_io *)ctsio); 7928 return (1); 7929 } 7930 lun->PRGeneration++; 7931 mtx_unlock(&lun->lun_lock); 7932 7933 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7934 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7935 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7936 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7937 persis_io.pr.pr_info.res_type = type; 7938 memcpy(persis_io.pr.pr_info.sa_res_key, 7939 param->serv_act_res_key, 7940 sizeof(param->serv_act_res_key)); 7941 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7942 sizeof(persis_io.pr), M_WAITOK); 7943 } 7944 } 7945 return (0); 7946} 7947 7948static void 7949ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7950{ 7951 uint64_t sa_res_key; 7952 int i; 7953 7954 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7955 7956 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7957 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7958 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7959 if (sa_res_key == 0) { 7960 /* 7961 * Unregister everybody else and build UA for 7962 * them 7963 */ 7964 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7965 if (i == msg->pr.pr_info.residx || 7966 ctl_get_prkey(lun, i) == 0) 7967 continue; 7968 7969 ctl_clr_prkey(lun, i); 7970 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7971 } 7972 7973 lun->pr_key_count = 1; 7974 lun->res_type = msg->pr.pr_info.res_type; 7975 if (lun->res_type != SPR_TYPE_WR_EX_AR 7976 && lun->res_type != SPR_TYPE_EX_AC_AR) 7977 lun->pr_res_idx = msg->pr.pr_info.residx; 7978 } else { 7979 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7980 if (sa_res_key == ctl_get_prkey(lun, i)) 7981 continue; 7982 7983 ctl_clr_prkey(lun, i); 7984 lun->pr_key_count--; 7985 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7986 } 7987 } 7988 } else { 7989 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7990 if (i == msg->pr.pr_info.residx || 7991 ctl_get_prkey(lun, i) == 0) 7992 continue; 7993 7994 if (sa_res_key == ctl_get_prkey(lun, i)) { 7995 ctl_clr_prkey(lun, i); 7996 lun->pr_key_count--; 7997 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7998 } else if (msg->pr.pr_info.res_type != lun->res_type 7999 && (lun->res_type == SPR_TYPE_WR_EX_RO 8000 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8001 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8002 } 8003 } 8004 lun->res_type = msg->pr.pr_info.res_type; 8005 if (lun->res_type != SPR_TYPE_WR_EX_AR 8006 && lun->res_type != SPR_TYPE_EX_AC_AR) 8007 lun->pr_res_idx = msg->pr.pr_info.residx; 8008 else 8009 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8010 } 8011 lun->PRGeneration++; 8012 8013} 8014 8015 8016int 8017ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8018{ 8019 int retval; 8020 u_int32_t param_len; 8021 struct scsi_per_res_out *cdb; 8022 struct ctl_lun *lun; 8023 struct scsi_per_res_out_parms* param; 8024 struct ctl_softc *softc; 8025 uint32_t residx; 8026 uint64_t res_key, sa_res_key, key; 8027 uint8_t type; 8028 union ctl_ha_msg persis_io; 8029 int i; 8030 8031 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8032 8033 retval = CTL_RETVAL_COMPLETE; 8034 8035 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8036 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8037 softc = lun->ctl_softc; 8038 8039 /* 8040 * We only support whole-LUN scope. The scope & type are ignored for 8041 * register, register and ignore existing key and clear. 8042 * We sometimes ignore scope and type on preempts too!! 8043 * Verify reservation type here as well. 8044 */ 8045 type = cdb->scope_type & SPR_TYPE_MASK; 8046 if ((cdb->action == SPRO_RESERVE) 8047 || (cdb->action == SPRO_RELEASE)) { 8048 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8049 ctl_set_invalid_field(/*ctsio*/ ctsio, 8050 /*sks_valid*/ 1, 8051 /*command*/ 1, 8052 /*field*/ 2, 8053 /*bit_valid*/ 1, 8054 /*bit*/ 4); 8055 ctl_done((union ctl_io *)ctsio); 8056 return (CTL_RETVAL_COMPLETE); 8057 } 8058 8059 if (type>8 || type==2 || type==4 || type==0) { 8060 ctl_set_invalid_field(/*ctsio*/ ctsio, 8061 /*sks_valid*/ 1, 8062 /*command*/ 1, 8063 /*field*/ 2, 8064 /*bit_valid*/ 1, 8065 /*bit*/ 0); 8066 ctl_done((union ctl_io *)ctsio); 8067 return (CTL_RETVAL_COMPLETE); 8068 } 8069 } 8070 8071 param_len = scsi_4btoul(cdb->length); 8072 8073 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8074 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8075 ctsio->kern_data_len = param_len; 8076 ctsio->kern_total_len = param_len; 8077 ctsio->kern_data_resid = 0; 8078 ctsio->kern_rel_offset = 0; 8079 ctsio->kern_sg_entries = 0; 8080 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8081 ctsio->be_move_done = ctl_config_move_done; 8082 ctl_datamove((union ctl_io *)ctsio); 8083 8084 return (CTL_RETVAL_COMPLETE); 8085 } 8086 8087 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8088 8089 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8090 res_key = scsi_8btou64(param->res_key.key); 8091 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8092 8093 /* 8094 * Validate the reservation key here except for SPRO_REG_IGNO 8095 * This must be done for all other service actions 8096 */ 8097 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8098 mtx_lock(&lun->lun_lock); 8099 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8100 if (res_key != key) { 8101 /* 8102 * The current key passed in doesn't match 8103 * the one the initiator previously 8104 * registered. 8105 */ 8106 mtx_unlock(&lun->lun_lock); 8107 free(ctsio->kern_data_ptr, M_CTL); 8108 ctl_set_reservation_conflict(ctsio); 8109 ctl_done((union ctl_io *)ctsio); 8110 return (CTL_RETVAL_COMPLETE); 8111 } 8112 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8113 /* 8114 * We are not registered 8115 */ 8116 mtx_unlock(&lun->lun_lock); 8117 free(ctsio->kern_data_ptr, M_CTL); 8118 ctl_set_reservation_conflict(ctsio); 8119 ctl_done((union ctl_io *)ctsio); 8120 return (CTL_RETVAL_COMPLETE); 8121 } else if (res_key != 0) { 8122 /* 8123 * We are not registered and trying to register but 8124 * the register key isn't zero. 8125 */ 8126 mtx_unlock(&lun->lun_lock); 8127 free(ctsio->kern_data_ptr, M_CTL); 8128 ctl_set_reservation_conflict(ctsio); 8129 ctl_done((union ctl_io *)ctsio); 8130 return (CTL_RETVAL_COMPLETE); 8131 } 8132 mtx_unlock(&lun->lun_lock); 8133 } 8134 8135 switch (cdb->action & SPRO_ACTION_MASK) { 8136 case SPRO_REGISTER: 8137 case SPRO_REG_IGNO: { 8138 8139#if 0 8140 printf("Registration received\n"); 8141#endif 8142 8143 /* 8144 * We don't support any of these options, as we report in 8145 * the read capabilities request (see 8146 * ctl_persistent_reserve_in(), above). 8147 */ 8148 if ((param->flags & SPR_SPEC_I_PT) 8149 || (param->flags & SPR_ALL_TG_PT) 8150 || (param->flags & SPR_APTPL)) { 8151 int bit_ptr; 8152 8153 if (param->flags & SPR_APTPL) 8154 bit_ptr = 0; 8155 else if (param->flags & SPR_ALL_TG_PT) 8156 bit_ptr = 2; 8157 else /* SPR_SPEC_I_PT */ 8158 bit_ptr = 3; 8159 8160 free(ctsio->kern_data_ptr, M_CTL); 8161 ctl_set_invalid_field(ctsio, 8162 /*sks_valid*/ 1, 8163 /*command*/ 0, 8164 /*field*/ 20, 8165 /*bit_valid*/ 1, 8166 /*bit*/ bit_ptr); 8167 ctl_done((union ctl_io *)ctsio); 8168 return (CTL_RETVAL_COMPLETE); 8169 } 8170 8171 mtx_lock(&lun->lun_lock); 8172 8173 /* 8174 * The initiator wants to clear the 8175 * key/unregister. 8176 */ 8177 if (sa_res_key == 0) { 8178 if ((res_key == 0 8179 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8180 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8181 && ctl_get_prkey(lun, residx) == 0)) { 8182 mtx_unlock(&lun->lun_lock); 8183 goto done; 8184 } 8185 8186 ctl_clr_prkey(lun, residx); 8187 lun->pr_key_count--; 8188 8189 if (residx == lun->pr_res_idx) { 8190 lun->flags &= ~CTL_LUN_PR_RESERVED; 8191 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8192 8193 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8194 || lun->res_type == SPR_TYPE_EX_AC_RO) 8195 && lun->pr_key_count) { 8196 /* 8197 * If the reservation is a registrants 8198 * only type we need to generate a UA 8199 * for other registered inits. The 8200 * sense code should be RESERVATIONS 8201 * RELEASED 8202 */ 8203 8204 for (i = softc->init_min; i < softc->init_max; i++){ 8205 if (ctl_get_prkey(lun, i) == 0) 8206 continue; 8207 ctl_est_ua(lun, i, 8208 CTL_UA_RES_RELEASE); 8209 } 8210 } 8211 lun->res_type = 0; 8212 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8213 if (lun->pr_key_count==0) { 8214 lun->flags &= ~CTL_LUN_PR_RESERVED; 8215 lun->res_type = 0; 8216 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8217 } 8218 } 8219 lun->PRGeneration++; 8220 mtx_unlock(&lun->lun_lock); 8221 8222 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8223 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8224 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8225 persis_io.pr.pr_info.residx = residx; 8226 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8227 sizeof(persis_io.pr), M_WAITOK); 8228 } else /* sa_res_key != 0 */ { 8229 8230 /* 8231 * If we aren't registered currently then increment 8232 * the key count and set the registered flag. 8233 */ 8234 ctl_alloc_prkey(lun, residx); 8235 if (ctl_get_prkey(lun, residx) == 0) 8236 lun->pr_key_count++; 8237 ctl_set_prkey(lun, residx, sa_res_key); 8238 lun->PRGeneration++; 8239 mtx_unlock(&lun->lun_lock); 8240 8241 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8242 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8243 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8244 persis_io.pr.pr_info.residx = residx; 8245 memcpy(persis_io.pr.pr_info.sa_res_key, 8246 param->serv_act_res_key, 8247 sizeof(param->serv_act_res_key)); 8248 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8249 sizeof(persis_io.pr), M_WAITOK); 8250 } 8251 8252 break; 8253 } 8254 case SPRO_RESERVE: 8255#if 0 8256 printf("Reserve executed type %d\n", type); 8257#endif 8258 mtx_lock(&lun->lun_lock); 8259 if (lun->flags & CTL_LUN_PR_RESERVED) { 8260 /* 8261 * if this isn't the reservation holder and it's 8262 * not a "all registrants" type or if the type is 8263 * different then we have a conflict 8264 */ 8265 if ((lun->pr_res_idx != residx 8266 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8267 || lun->res_type != type) { 8268 mtx_unlock(&lun->lun_lock); 8269 free(ctsio->kern_data_ptr, M_CTL); 8270 ctl_set_reservation_conflict(ctsio); 8271 ctl_done((union ctl_io *)ctsio); 8272 return (CTL_RETVAL_COMPLETE); 8273 } 8274 mtx_unlock(&lun->lun_lock); 8275 } else /* create a reservation */ { 8276 /* 8277 * If it's not an "all registrants" type record 8278 * reservation holder 8279 */ 8280 if (type != SPR_TYPE_WR_EX_AR 8281 && type != SPR_TYPE_EX_AC_AR) 8282 lun->pr_res_idx = residx; /* Res holder */ 8283 else 8284 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8285 8286 lun->flags |= CTL_LUN_PR_RESERVED; 8287 lun->res_type = type; 8288 8289 mtx_unlock(&lun->lun_lock); 8290 8291 /* send msg to other side */ 8292 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8293 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8294 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8295 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8296 persis_io.pr.pr_info.res_type = type; 8297 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8298 sizeof(persis_io.pr), M_WAITOK); 8299 } 8300 break; 8301 8302 case SPRO_RELEASE: 8303 mtx_lock(&lun->lun_lock); 8304 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8305 /* No reservation exists return good status */ 8306 mtx_unlock(&lun->lun_lock); 8307 goto done; 8308 } 8309 /* 8310 * Is this nexus a reservation holder? 8311 */ 8312 if (lun->pr_res_idx != residx 8313 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8314 /* 8315 * not a res holder return good status but 8316 * do nothing 8317 */ 8318 mtx_unlock(&lun->lun_lock); 8319 goto done; 8320 } 8321 8322 if (lun->res_type != type) { 8323 mtx_unlock(&lun->lun_lock); 8324 free(ctsio->kern_data_ptr, M_CTL); 8325 ctl_set_illegal_pr_release(ctsio); 8326 ctl_done((union ctl_io *)ctsio); 8327 return (CTL_RETVAL_COMPLETE); 8328 } 8329 8330 /* okay to release */ 8331 lun->flags &= ~CTL_LUN_PR_RESERVED; 8332 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8333 lun->res_type = 0; 8334 8335 /* 8336 * if this isn't an exclusive access 8337 * res generate UA for all other 8338 * registrants. 8339 */ 8340 if (type != SPR_TYPE_EX_AC 8341 && type != SPR_TYPE_WR_EX) { 8342 for (i = softc->init_min; i < softc->init_max; i++) { 8343 if (i == residx || ctl_get_prkey(lun, i) == 0) 8344 continue; 8345 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8346 } 8347 } 8348 mtx_unlock(&lun->lun_lock); 8349 8350 /* Send msg to other side */ 8351 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8352 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8353 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8354 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8355 sizeof(persis_io.pr), M_WAITOK); 8356 break; 8357 8358 case SPRO_CLEAR: 8359 /* send msg to other side */ 8360 8361 mtx_lock(&lun->lun_lock); 8362 lun->flags &= ~CTL_LUN_PR_RESERVED; 8363 lun->res_type = 0; 8364 lun->pr_key_count = 0; 8365 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8366 8367 ctl_clr_prkey(lun, residx); 8368 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8369 if (ctl_get_prkey(lun, i) != 0) { 8370 ctl_clr_prkey(lun, i); 8371 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8372 } 8373 lun->PRGeneration++; 8374 mtx_unlock(&lun->lun_lock); 8375 8376 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8377 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8378 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8379 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8380 sizeof(persis_io.pr), M_WAITOK); 8381 break; 8382 8383 case SPRO_PREEMPT: 8384 case SPRO_PRE_ABO: { 8385 int nretval; 8386 8387 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8388 residx, ctsio, cdb, param); 8389 if (nretval != 0) 8390 return (CTL_RETVAL_COMPLETE); 8391 break; 8392 } 8393 default: 8394 panic("Invalid PR type %x", cdb->action); 8395 } 8396 8397done: 8398 free(ctsio->kern_data_ptr, M_CTL); 8399 ctl_set_success(ctsio); 8400 ctl_done((union ctl_io *)ctsio); 8401 8402 return (retval); 8403} 8404 8405/* 8406 * This routine is for handling a message from the other SC pertaining to 8407 * persistent reserve out. All the error checking will have been done 8408 * so only perorming the action need be done here to keep the two 8409 * in sync. 8410 */ 8411static void 8412ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8413{ 8414 struct ctl_softc *softc = control_softc; 8415 struct ctl_lun *lun; 8416 int i; 8417 uint32_t residx, targ_lun; 8418 8419 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8420 mtx_lock(&softc->ctl_lock); 8421 if ((targ_lun >= CTL_MAX_LUNS) || 8422 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8423 mtx_unlock(&softc->ctl_lock); 8424 return; 8425 } 8426 mtx_lock(&lun->lun_lock); 8427 mtx_unlock(&softc->ctl_lock); 8428 if (lun->flags & CTL_LUN_DISABLED) { 8429 mtx_unlock(&lun->lun_lock); 8430 return; 8431 } 8432 residx = ctl_get_initindex(&msg->hdr.nexus); 8433 switch(msg->pr.pr_info.action) { 8434 case CTL_PR_REG_KEY: 8435 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8436 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8437 lun->pr_key_count++; 8438 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8439 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8440 lun->PRGeneration++; 8441 break; 8442 8443 case CTL_PR_UNREG_KEY: 8444 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8445 lun->pr_key_count--; 8446 8447 /* XXX Need to see if the reservation has been released */ 8448 /* if so do we need to generate UA? */ 8449 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8450 lun->flags &= ~CTL_LUN_PR_RESERVED; 8451 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8452 8453 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8454 || lun->res_type == SPR_TYPE_EX_AC_RO) 8455 && lun->pr_key_count) { 8456 /* 8457 * If the reservation is a registrants 8458 * only type we need to generate a UA 8459 * for other registered inits. The 8460 * sense code should be RESERVATIONS 8461 * RELEASED 8462 */ 8463 8464 for (i = softc->init_min; i < softc->init_max; i++) { 8465 if (ctl_get_prkey(lun, i) == 0) 8466 continue; 8467 8468 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8469 } 8470 } 8471 lun->res_type = 0; 8472 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8473 if (lun->pr_key_count==0) { 8474 lun->flags &= ~CTL_LUN_PR_RESERVED; 8475 lun->res_type = 0; 8476 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8477 } 8478 } 8479 lun->PRGeneration++; 8480 break; 8481 8482 case CTL_PR_RESERVE: 8483 lun->flags |= CTL_LUN_PR_RESERVED; 8484 lun->res_type = msg->pr.pr_info.res_type; 8485 lun->pr_res_idx = msg->pr.pr_info.residx; 8486 8487 break; 8488 8489 case CTL_PR_RELEASE: 8490 /* 8491 * if this isn't an exclusive access res generate UA for all 8492 * other registrants. 8493 */ 8494 if (lun->res_type != SPR_TYPE_EX_AC 8495 && lun->res_type != SPR_TYPE_WR_EX) { 8496 for (i = softc->init_min; i < softc->init_max; i++) 8497 if (i == residx || ctl_get_prkey(lun, i) == 0) 8498 continue; 8499 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8500 } 8501 8502 lun->flags &= ~CTL_LUN_PR_RESERVED; 8503 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8504 lun->res_type = 0; 8505 break; 8506 8507 case CTL_PR_PREEMPT: 8508 ctl_pro_preempt_other(lun, msg); 8509 break; 8510 case CTL_PR_CLEAR: 8511 lun->flags &= ~CTL_LUN_PR_RESERVED; 8512 lun->res_type = 0; 8513 lun->pr_key_count = 0; 8514 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8515 8516 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8517 if (ctl_get_prkey(lun, i) == 0) 8518 continue; 8519 ctl_clr_prkey(lun, i); 8520 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8521 } 8522 lun->PRGeneration++; 8523 break; 8524 } 8525 8526 mtx_unlock(&lun->lun_lock); 8527} 8528 8529int 8530ctl_read_write(struct ctl_scsiio *ctsio) 8531{ 8532 struct ctl_lun *lun; 8533 struct ctl_lba_len_flags *lbalen; 8534 uint64_t lba; 8535 uint32_t num_blocks; 8536 int flags, retval; 8537 int isread; 8538 8539 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8540 8541 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8542 8543 flags = 0; 8544 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8545 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8546 switch (ctsio->cdb[0]) { 8547 case READ_6: 8548 case WRITE_6: { 8549 struct scsi_rw_6 *cdb; 8550 8551 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8552 8553 lba = scsi_3btoul(cdb->addr); 8554 /* only 5 bits are valid in the most significant address byte */ 8555 lba &= 0x1fffff; 8556 num_blocks = cdb->length; 8557 /* 8558 * This is correct according to SBC-2. 8559 */ 8560 if (num_blocks == 0) 8561 num_blocks = 256; 8562 break; 8563 } 8564 case READ_10: 8565 case WRITE_10: { 8566 struct scsi_rw_10 *cdb; 8567 8568 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8569 if (cdb->byte2 & SRW10_FUA) 8570 flags |= CTL_LLF_FUA; 8571 if (cdb->byte2 & SRW10_DPO) 8572 flags |= CTL_LLF_DPO; 8573 lba = scsi_4btoul(cdb->addr); 8574 num_blocks = scsi_2btoul(cdb->length); 8575 break; 8576 } 8577 case WRITE_VERIFY_10: { 8578 struct scsi_write_verify_10 *cdb; 8579 8580 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8581 flags |= CTL_LLF_FUA; 8582 if (cdb->byte2 & SWV_DPO) 8583 flags |= CTL_LLF_DPO; 8584 lba = scsi_4btoul(cdb->addr); 8585 num_blocks = scsi_2btoul(cdb->length); 8586 break; 8587 } 8588 case READ_12: 8589 case WRITE_12: { 8590 struct scsi_rw_12 *cdb; 8591 8592 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8593 if (cdb->byte2 & SRW12_FUA) 8594 flags |= CTL_LLF_FUA; 8595 if (cdb->byte2 & SRW12_DPO) 8596 flags |= CTL_LLF_DPO; 8597 lba = scsi_4btoul(cdb->addr); 8598 num_blocks = scsi_4btoul(cdb->length); 8599 break; 8600 } 8601 case WRITE_VERIFY_12: { 8602 struct scsi_write_verify_12 *cdb; 8603 8604 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8605 flags |= CTL_LLF_FUA; 8606 if (cdb->byte2 & SWV_DPO) 8607 flags |= CTL_LLF_DPO; 8608 lba = scsi_4btoul(cdb->addr); 8609 num_blocks = scsi_4btoul(cdb->length); 8610 break; 8611 } 8612 case READ_16: 8613 case WRITE_16: { 8614 struct scsi_rw_16 *cdb; 8615 8616 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8617 if (cdb->byte2 & SRW12_FUA) 8618 flags |= CTL_LLF_FUA; 8619 if (cdb->byte2 & SRW12_DPO) 8620 flags |= CTL_LLF_DPO; 8621 lba = scsi_8btou64(cdb->addr); 8622 num_blocks = scsi_4btoul(cdb->length); 8623 break; 8624 } 8625 case WRITE_ATOMIC_16: { 8626 struct scsi_write_atomic_16 *cdb; 8627 8628 if (lun->be_lun->atomicblock == 0) { 8629 ctl_set_invalid_opcode(ctsio); 8630 ctl_done((union ctl_io *)ctsio); 8631 return (CTL_RETVAL_COMPLETE); 8632 } 8633 8634 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8635 if (cdb->byte2 & SRW12_FUA) 8636 flags |= CTL_LLF_FUA; 8637 if (cdb->byte2 & SRW12_DPO) 8638 flags |= CTL_LLF_DPO; 8639 lba = scsi_8btou64(cdb->addr); 8640 num_blocks = scsi_2btoul(cdb->length); 8641 if (num_blocks > lun->be_lun->atomicblock) { 8642 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8643 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8644 /*bit*/ 0); 8645 ctl_done((union ctl_io *)ctsio); 8646 return (CTL_RETVAL_COMPLETE); 8647 } 8648 break; 8649 } 8650 case WRITE_VERIFY_16: { 8651 struct scsi_write_verify_16 *cdb; 8652 8653 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8654 flags |= CTL_LLF_FUA; 8655 if (cdb->byte2 & SWV_DPO) 8656 flags |= CTL_LLF_DPO; 8657 lba = scsi_8btou64(cdb->addr); 8658 num_blocks = scsi_4btoul(cdb->length); 8659 break; 8660 } 8661 default: 8662 /* 8663 * We got a command we don't support. This shouldn't 8664 * happen, commands should be filtered out above us. 8665 */ 8666 ctl_set_invalid_opcode(ctsio); 8667 ctl_done((union ctl_io *)ctsio); 8668 8669 return (CTL_RETVAL_COMPLETE); 8670 break; /* NOTREACHED */ 8671 } 8672 8673 /* 8674 * The first check is to make sure we're in bounds, the second 8675 * check is to catch wrap-around problems. If the lba + num blocks 8676 * is less than the lba, then we've wrapped around and the block 8677 * range is invalid anyway. 8678 */ 8679 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8680 || ((lba + num_blocks) < lba)) { 8681 ctl_set_lba_out_of_range(ctsio); 8682 ctl_done((union ctl_io *)ctsio); 8683 return (CTL_RETVAL_COMPLETE); 8684 } 8685 8686 /* 8687 * According to SBC-3, a transfer length of 0 is not an error. 8688 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8689 * translates to 256 blocks for those commands. 8690 */ 8691 if (num_blocks == 0) { 8692 ctl_set_success(ctsio); 8693 ctl_done((union ctl_io *)ctsio); 8694 return (CTL_RETVAL_COMPLETE); 8695 } 8696 8697 /* Set FUA and/or DPO if caches are disabled. */ 8698 if (isread) { 8699 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8700 SCP_RCD) != 0) 8701 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8702 } else { 8703 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8704 SCP_WCE) == 0) 8705 flags |= CTL_LLF_FUA; 8706 } 8707 8708 lbalen = (struct ctl_lba_len_flags *) 8709 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8710 lbalen->lba = lba; 8711 lbalen->len = num_blocks; 8712 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8713 8714 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8715 ctsio->kern_rel_offset = 0; 8716 8717 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8718 8719 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8720 return (retval); 8721} 8722 8723static int 8724ctl_cnw_cont(union ctl_io *io) 8725{ 8726 struct ctl_scsiio *ctsio; 8727 struct ctl_lun *lun; 8728 struct ctl_lba_len_flags *lbalen; 8729 int retval; 8730 8731 ctsio = &io->scsiio; 8732 ctsio->io_hdr.status = CTL_STATUS_NONE; 8733 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8734 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8735 lbalen = (struct ctl_lba_len_flags *) 8736 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8737 lbalen->flags &= ~CTL_LLF_COMPARE; 8738 lbalen->flags |= CTL_LLF_WRITE; 8739 8740 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8741 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8742 return (retval); 8743} 8744 8745int 8746ctl_cnw(struct ctl_scsiio *ctsio) 8747{ 8748 struct ctl_lun *lun; 8749 struct ctl_lba_len_flags *lbalen; 8750 uint64_t lba; 8751 uint32_t num_blocks; 8752 int flags, retval; 8753 8754 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8755 8756 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8757 8758 flags = 0; 8759 switch (ctsio->cdb[0]) { 8760 case COMPARE_AND_WRITE: { 8761 struct scsi_compare_and_write *cdb; 8762 8763 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8764 if (cdb->byte2 & SRW10_FUA) 8765 flags |= CTL_LLF_FUA; 8766 if (cdb->byte2 & SRW10_DPO) 8767 flags |= CTL_LLF_DPO; 8768 lba = scsi_8btou64(cdb->addr); 8769 num_blocks = cdb->length; 8770 break; 8771 } 8772 default: 8773 /* 8774 * We got a command we don't support. This shouldn't 8775 * happen, commands should be filtered out above us. 8776 */ 8777 ctl_set_invalid_opcode(ctsio); 8778 ctl_done((union ctl_io *)ctsio); 8779 8780 return (CTL_RETVAL_COMPLETE); 8781 break; /* NOTREACHED */ 8782 } 8783 8784 /* 8785 * The first check is to make sure we're in bounds, the second 8786 * check is to catch wrap-around problems. If the lba + num blocks 8787 * is less than the lba, then we've wrapped around and the block 8788 * range is invalid anyway. 8789 */ 8790 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8791 || ((lba + num_blocks) < lba)) { 8792 ctl_set_lba_out_of_range(ctsio); 8793 ctl_done((union ctl_io *)ctsio); 8794 return (CTL_RETVAL_COMPLETE); 8795 } 8796 8797 /* 8798 * According to SBC-3, a transfer length of 0 is not an error. 8799 */ 8800 if (num_blocks == 0) { 8801 ctl_set_success(ctsio); 8802 ctl_done((union ctl_io *)ctsio); 8803 return (CTL_RETVAL_COMPLETE); 8804 } 8805 8806 /* Set FUA if write cache is disabled. */ 8807 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8808 SCP_WCE) == 0) 8809 flags |= CTL_LLF_FUA; 8810 8811 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8812 ctsio->kern_rel_offset = 0; 8813 8814 /* 8815 * Set the IO_CONT flag, so that if this I/O gets passed to 8816 * ctl_data_submit_done(), it'll get passed back to 8817 * ctl_ctl_cnw_cont() for further processing. 8818 */ 8819 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8820 ctsio->io_cont = ctl_cnw_cont; 8821 8822 lbalen = (struct ctl_lba_len_flags *) 8823 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8824 lbalen->lba = lba; 8825 lbalen->len = num_blocks; 8826 lbalen->flags = CTL_LLF_COMPARE | flags; 8827 8828 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8829 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8830 return (retval); 8831} 8832 8833int 8834ctl_verify(struct ctl_scsiio *ctsio) 8835{ 8836 struct ctl_lun *lun; 8837 struct ctl_lba_len_flags *lbalen; 8838 uint64_t lba; 8839 uint32_t num_blocks; 8840 int bytchk, flags; 8841 int retval; 8842 8843 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8844 8845 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8846 8847 bytchk = 0; 8848 flags = CTL_LLF_FUA; 8849 switch (ctsio->cdb[0]) { 8850 case VERIFY_10: { 8851 struct scsi_verify_10 *cdb; 8852 8853 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8854 if (cdb->byte2 & SVFY_BYTCHK) 8855 bytchk = 1; 8856 if (cdb->byte2 & SVFY_DPO) 8857 flags |= CTL_LLF_DPO; 8858 lba = scsi_4btoul(cdb->addr); 8859 num_blocks = scsi_2btoul(cdb->length); 8860 break; 8861 } 8862 case VERIFY_12: { 8863 struct scsi_verify_12 *cdb; 8864 8865 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8866 if (cdb->byte2 & SVFY_BYTCHK) 8867 bytchk = 1; 8868 if (cdb->byte2 & SVFY_DPO) 8869 flags |= CTL_LLF_DPO; 8870 lba = scsi_4btoul(cdb->addr); 8871 num_blocks = scsi_4btoul(cdb->length); 8872 break; 8873 } 8874 case VERIFY_16: { 8875 struct scsi_rw_16 *cdb; 8876 8877 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8878 if (cdb->byte2 & SVFY_BYTCHK) 8879 bytchk = 1; 8880 if (cdb->byte2 & SVFY_DPO) 8881 flags |= CTL_LLF_DPO; 8882 lba = scsi_8btou64(cdb->addr); 8883 num_blocks = scsi_4btoul(cdb->length); 8884 break; 8885 } 8886 default: 8887 /* 8888 * We got a command we don't support. This shouldn't 8889 * happen, commands should be filtered out above us. 8890 */ 8891 ctl_set_invalid_opcode(ctsio); 8892 ctl_done((union ctl_io *)ctsio); 8893 return (CTL_RETVAL_COMPLETE); 8894 } 8895 8896 /* 8897 * The first check is to make sure we're in bounds, the second 8898 * check is to catch wrap-around problems. If the lba + num blocks 8899 * is less than the lba, then we've wrapped around and the block 8900 * range is invalid anyway. 8901 */ 8902 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8903 || ((lba + num_blocks) < lba)) { 8904 ctl_set_lba_out_of_range(ctsio); 8905 ctl_done((union ctl_io *)ctsio); 8906 return (CTL_RETVAL_COMPLETE); 8907 } 8908 8909 /* 8910 * According to SBC-3, a transfer length of 0 is not an error. 8911 */ 8912 if (num_blocks == 0) { 8913 ctl_set_success(ctsio); 8914 ctl_done((union ctl_io *)ctsio); 8915 return (CTL_RETVAL_COMPLETE); 8916 } 8917 8918 lbalen = (struct ctl_lba_len_flags *) 8919 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8920 lbalen->lba = lba; 8921 lbalen->len = num_blocks; 8922 if (bytchk) { 8923 lbalen->flags = CTL_LLF_COMPARE | flags; 8924 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8925 } else { 8926 lbalen->flags = CTL_LLF_VERIFY | flags; 8927 ctsio->kern_total_len = 0; 8928 } 8929 ctsio->kern_rel_offset = 0; 8930 8931 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8932 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8933 return (retval); 8934} 8935 8936int 8937ctl_report_luns(struct ctl_scsiio *ctsio) 8938{ 8939 struct ctl_softc *softc; 8940 struct scsi_report_luns *cdb; 8941 struct scsi_report_luns_data *lun_data; 8942 struct ctl_lun *lun, *request_lun; 8943 struct ctl_port *port; 8944 int num_luns, retval; 8945 uint32_t alloc_len, lun_datalen; 8946 int num_filled; 8947 uint32_t initidx, targ_lun_id, lun_id; 8948 8949 retval = CTL_RETVAL_COMPLETE; 8950 cdb = (struct scsi_report_luns *)ctsio->cdb; 8951 port = ctl_io_port(&ctsio->io_hdr); 8952 softc = port->ctl_softc; 8953 8954 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8955 8956 mtx_lock(&softc->ctl_lock); 8957 num_luns = 0; 8958 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 8959 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 8960 num_luns++; 8961 } 8962 mtx_unlock(&softc->ctl_lock); 8963 8964 switch (cdb->select_report) { 8965 case RPL_REPORT_DEFAULT: 8966 case RPL_REPORT_ALL: 8967 case RPL_REPORT_NONSUBSID: 8968 break; 8969 case RPL_REPORT_WELLKNOWN: 8970 case RPL_REPORT_ADMIN: 8971 case RPL_REPORT_CONGLOM: 8972 num_luns = 0; 8973 break; 8974 default: 8975 ctl_set_invalid_field(ctsio, 8976 /*sks_valid*/ 1, 8977 /*command*/ 1, 8978 /*field*/ 2, 8979 /*bit_valid*/ 0, 8980 /*bit*/ 0); 8981 ctl_done((union ctl_io *)ctsio); 8982 return (retval); 8983 break; /* NOTREACHED */ 8984 } 8985 8986 alloc_len = scsi_4btoul(cdb->length); 8987 /* 8988 * The initiator has to allocate at least 16 bytes for this request, 8989 * so he can at least get the header and the first LUN. Otherwise 8990 * we reject the request (per SPC-3 rev 14, section 6.21). 8991 */ 8992 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 8993 sizeof(struct scsi_report_luns_lundata))) { 8994 ctl_set_invalid_field(ctsio, 8995 /*sks_valid*/ 1, 8996 /*command*/ 1, 8997 /*field*/ 6, 8998 /*bit_valid*/ 0, 8999 /*bit*/ 0); 9000 ctl_done((union ctl_io *)ctsio); 9001 return (retval); 9002 } 9003 9004 request_lun = (struct ctl_lun *) 9005 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9006 9007 lun_datalen = sizeof(*lun_data) + 9008 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9009 9010 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9011 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9012 ctsio->kern_sg_entries = 0; 9013 9014 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9015 9016 mtx_lock(&softc->ctl_lock); 9017 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9018 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9019 if (lun_id >= CTL_MAX_LUNS) 9020 continue; 9021 lun = softc->ctl_luns[lun_id]; 9022 if (lun == NULL) 9023 continue; 9024 9025 if (targ_lun_id <= 0xff) { 9026 /* 9027 * Peripheral addressing method, bus number 0. 9028 */ 9029 lun_data->luns[num_filled].lundata[0] = 9030 RPL_LUNDATA_ATYP_PERIPH; 9031 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9032 num_filled++; 9033 } else if (targ_lun_id <= 0x3fff) { 9034 /* 9035 * Flat addressing method. 9036 */ 9037 lun_data->luns[num_filled].lundata[0] = 9038 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9039 lun_data->luns[num_filled].lundata[1] = 9040 (targ_lun_id & 0xff); 9041 num_filled++; 9042 } else if (targ_lun_id <= 0xffffff) { 9043 /* 9044 * Extended flat addressing method. 9045 */ 9046 lun_data->luns[num_filled].lundata[0] = 9047 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9048 scsi_ulto3b(targ_lun_id, 9049 &lun_data->luns[num_filled].lundata[1]); 9050 num_filled++; 9051 } else { 9052 printf("ctl_report_luns: bogus LUN number %jd, " 9053 "skipping\n", (intmax_t)targ_lun_id); 9054 } 9055 /* 9056 * According to SPC-3, rev 14 section 6.21: 9057 * 9058 * "The execution of a REPORT LUNS command to any valid and 9059 * installed logical unit shall clear the REPORTED LUNS DATA 9060 * HAS CHANGED unit attention condition for all logical 9061 * units of that target with respect to the requesting 9062 * initiator. A valid and installed logical unit is one 9063 * having a PERIPHERAL QUALIFIER of 000b in the standard 9064 * INQUIRY data (see 6.4.2)." 9065 * 9066 * If request_lun is NULL, the LUN this report luns command 9067 * was issued to is either disabled or doesn't exist. In that 9068 * case, we shouldn't clear any pending lun change unit 9069 * attention. 9070 */ 9071 if (request_lun != NULL) { 9072 mtx_lock(&lun->lun_lock); 9073 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9074 mtx_unlock(&lun->lun_lock); 9075 } 9076 } 9077 mtx_unlock(&softc->ctl_lock); 9078 9079 /* 9080 * It's quite possible that we've returned fewer LUNs than we allocated 9081 * space for. Trim it. 9082 */ 9083 lun_datalen = sizeof(*lun_data) + 9084 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9085 9086 if (lun_datalen < alloc_len) { 9087 ctsio->residual = alloc_len - lun_datalen; 9088 ctsio->kern_data_len = lun_datalen; 9089 ctsio->kern_total_len = lun_datalen; 9090 } else { 9091 ctsio->residual = 0; 9092 ctsio->kern_data_len = alloc_len; 9093 ctsio->kern_total_len = alloc_len; 9094 } 9095 ctsio->kern_data_resid = 0; 9096 ctsio->kern_rel_offset = 0; 9097 ctsio->kern_sg_entries = 0; 9098 9099 /* 9100 * We set this to the actual data length, regardless of how much 9101 * space we actually have to return results. If the user looks at 9102 * this value, he'll know whether or not he allocated enough space 9103 * and reissue the command if necessary. We don't support well 9104 * known logical units, so if the user asks for that, return none. 9105 */ 9106 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9107 9108 /* 9109 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9110 * this request. 9111 */ 9112 ctl_set_success(ctsio); 9113 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9114 ctsio->be_move_done = ctl_config_move_done; 9115 ctl_datamove((union ctl_io *)ctsio); 9116 return (retval); 9117} 9118 9119int 9120ctl_request_sense(struct ctl_scsiio *ctsio) 9121{ 9122 struct scsi_request_sense *cdb; 9123 struct scsi_sense_data *sense_ptr; 9124 struct ctl_softc *ctl_softc; 9125 struct ctl_lun *lun; 9126 uint32_t initidx; 9127 int have_error; 9128 scsi_sense_data_type sense_format; 9129 ctl_ua_type ua_type; 9130 9131 cdb = (struct scsi_request_sense *)ctsio->cdb; 9132 9133 ctl_softc = control_softc; 9134 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9135 9136 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9137 9138 /* 9139 * Determine which sense format the user wants. 9140 */ 9141 if (cdb->byte2 & SRS_DESC) 9142 sense_format = SSD_TYPE_DESC; 9143 else 9144 sense_format = SSD_TYPE_FIXED; 9145 9146 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9147 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9148 ctsio->kern_sg_entries = 0; 9149 9150 /* 9151 * struct scsi_sense_data, which is currently set to 256 bytes, is 9152 * larger than the largest allowed value for the length field in the 9153 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9154 */ 9155 ctsio->residual = 0; 9156 ctsio->kern_data_len = cdb->length; 9157 ctsio->kern_total_len = cdb->length; 9158 9159 ctsio->kern_data_resid = 0; 9160 ctsio->kern_rel_offset = 0; 9161 ctsio->kern_sg_entries = 0; 9162 9163 /* 9164 * If we don't have a LUN, we don't have any pending sense. 9165 */ 9166 if (lun == NULL) 9167 goto no_sense; 9168 9169 have_error = 0; 9170 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9171 /* 9172 * Check for pending sense, and then for pending unit attentions. 9173 * Pending sense gets returned first, then pending unit attentions. 9174 */ 9175 mtx_lock(&lun->lun_lock); 9176#ifdef CTL_WITH_CA 9177 if (ctl_is_set(lun->have_ca, initidx)) { 9178 scsi_sense_data_type stored_format; 9179 9180 /* 9181 * Check to see which sense format was used for the stored 9182 * sense data. 9183 */ 9184 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9185 9186 /* 9187 * If the user requested a different sense format than the 9188 * one we stored, then we need to convert it to the other 9189 * format. If we're going from descriptor to fixed format 9190 * sense data, we may lose things in translation, depending 9191 * on what options were used. 9192 * 9193 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9194 * for some reason we'll just copy it out as-is. 9195 */ 9196 if ((stored_format == SSD_TYPE_FIXED) 9197 && (sense_format == SSD_TYPE_DESC)) 9198 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9199 &lun->pending_sense[initidx], 9200 (struct scsi_sense_data_desc *)sense_ptr); 9201 else if ((stored_format == SSD_TYPE_DESC) 9202 && (sense_format == SSD_TYPE_FIXED)) 9203 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9204 &lun->pending_sense[initidx], 9205 (struct scsi_sense_data_fixed *)sense_ptr); 9206 else 9207 memcpy(sense_ptr, &lun->pending_sense[initidx], 9208 MIN(sizeof(*sense_ptr), 9209 sizeof(lun->pending_sense[initidx]))); 9210 9211 ctl_clear_mask(lun->have_ca, initidx); 9212 have_error = 1; 9213 } else 9214#endif 9215 { 9216 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9217 if (ua_type != CTL_UA_NONE) 9218 have_error = 1; 9219 if (ua_type == CTL_UA_LUN_CHANGE) { 9220 mtx_unlock(&lun->lun_lock); 9221 mtx_lock(&ctl_softc->ctl_lock); 9222 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9223 mtx_unlock(&ctl_softc->ctl_lock); 9224 mtx_lock(&lun->lun_lock); 9225 } 9226 9227 } 9228 mtx_unlock(&lun->lun_lock); 9229 9230 /* 9231 * We already have a pending error, return it. 9232 */ 9233 if (have_error != 0) { 9234 /* 9235 * We report the SCSI status as OK, since the status of the 9236 * request sense command itself is OK. 9237 * We report 0 for the sense length, because we aren't doing 9238 * autosense in this case. We're reporting sense as 9239 * parameter data. 9240 */ 9241 ctl_set_success(ctsio); 9242 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9243 ctsio->be_move_done = ctl_config_move_done; 9244 ctl_datamove((union ctl_io *)ctsio); 9245 return (CTL_RETVAL_COMPLETE); 9246 } 9247 9248no_sense: 9249 9250 /* 9251 * No sense information to report, so we report that everything is 9252 * okay. 9253 */ 9254 ctl_set_sense_data(sense_ptr, 9255 lun, 9256 sense_format, 9257 /*current_error*/ 1, 9258 /*sense_key*/ SSD_KEY_NO_SENSE, 9259 /*asc*/ 0x00, 9260 /*ascq*/ 0x00, 9261 SSD_ELEM_NONE); 9262 9263 /* 9264 * We report 0 for the sense length, because we aren't doing 9265 * autosense in this case. We're reporting sense as parameter data. 9266 */ 9267 ctl_set_success(ctsio); 9268 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9269 ctsio->be_move_done = ctl_config_move_done; 9270 ctl_datamove((union ctl_io *)ctsio); 9271 return (CTL_RETVAL_COMPLETE); 9272} 9273 9274int 9275ctl_tur(struct ctl_scsiio *ctsio) 9276{ 9277 9278 CTL_DEBUG_PRINT(("ctl_tur\n")); 9279 9280 ctl_set_success(ctsio); 9281 ctl_done((union ctl_io *)ctsio); 9282 9283 return (CTL_RETVAL_COMPLETE); 9284} 9285 9286/* 9287 * SCSI VPD page 0x00, the Supported VPD Pages page. 9288 */ 9289static int 9290ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9291{ 9292 struct scsi_vpd_supported_pages *pages; 9293 int sup_page_size; 9294 struct ctl_lun *lun; 9295 int p; 9296 9297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9298 9299 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9300 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9301 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9302 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9303 ctsio->kern_sg_entries = 0; 9304 9305 if (sup_page_size < alloc_len) { 9306 ctsio->residual = alloc_len - sup_page_size; 9307 ctsio->kern_data_len = sup_page_size; 9308 ctsio->kern_total_len = sup_page_size; 9309 } else { 9310 ctsio->residual = 0; 9311 ctsio->kern_data_len = alloc_len; 9312 ctsio->kern_total_len = alloc_len; 9313 } 9314 ctsio->kern_data_resid = 0; 9315 ctsio->kern_rel_offset = 0; 9316 ctsio->kern_sg_entries = 0; 9317 9318 /* 9319 * The control device is always connected. The disk device, on the 9320 * other hand, may not be online all the time. Need to change this 9321 * to figure out whether the disk device is actually online or not. 9322 */ 9323 if (lun != NULL) 9324 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9325 lun->be_lun->lun_type; 9326 else 9327 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9328 9329 p = 0; 9330 /* Supported VPD pages */ 9331 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9332 /* Serial Number */ 9333 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9334 /* Device Identification */ 9335 pages->page_list[p++] = SVPD_DEVICE_ID; 9336 /* Extended INQUIRY Data */ 9337 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9338 /* Mode Page Policy */ 9339 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9340 /* SCSI Ports */ 9341 pages->page_list[p++] = SVPD_SCSI_PORTS; 9342 /* Third-party Copy */ 9343 pages->page_list[p++] = SVPD_SCSI_TPC; 9344 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9345 /* Block limits */ 9346 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9347 /* Block Device Characteristics */ 9348 pages->page_list[p++] = SVPD_BDC; 9349 /* Logical Block Provisioning */ 9350 pages->page_list[p++] = SVPD_LBP; 9351 } 9352 pages->length = p; 9353 9354 ctl_set_success(ctsio); 9355 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9356 ctsio->be_move_done = ctl_config_move_done; 9357 ctl_datamove((union ctl_io *)ctsio); 9358 return (CTL_RETVAL_COMPLETE); 9359} 9360 9361/* 9362 * SCSI VPD page 0x80, the Unit Serial Number page. 9363 */ 9364static int 9365ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9366{ 9367 struct scsi_vpd_unit_serial_number *sn_ptr; 9368 struct ctl_lun *lun; 9369 int data_len; 9370 9371 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9372 9373 data_len = 4 + CTL_SN_LEN; 9374 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9375 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9376 if (data_len < alloc_len) { 9377 ctsio->residual = alloc_len - data_len; 9378 ctsio->kern_data_len = data_len; 9379 ctsio->kern_total_len = data_len; 9380 } else { 9381 ctsio->residual = 0; 9382 ctsio->kern_data_len = alloc_len; 9383 ctsio->kern_total_len = alloc_len; 9384 } 9385 ctsio->kern_data_resid = 0; 9386 ctsio->kern_rel_offset = 0; 9387 ctsio->kern_sg_entries = 0; 9388 9389 /* 9390 * The control device is always connected. The disk device, on the 9391 * other hand, may not be online all the time. Need to change this 9392 * to figure out whether the disk device is actually online or not. 9393 */ 9394 if (lun != NULL) 9395 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9396 lun->be_lun->lun_type; 9397 else 9398 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9399 9400 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9401 sn_ptr->length = CTL_SN_LEN; 9402 /* 9403 * If we don't have a LUN, we just leave the serial number as 9404 * all spaces. 9405 */ 9406 if (lun != NULL) { 9407 strncpy((char *)sn_ptr->serial_num, 9408 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9409 } else 9410 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9411 9412 ctl_set_success(ctsio); 9413 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9414 ctsio->be_move_done = ctl_config_move_done; 9415 ctl_datamove((union ctl_io *)ctsio); 9416 return (CTL_RETVAL_COMPLETE); 9417} 9418 9419 9420/* 9421 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9422 */ 9423static int 9424ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9425{ 9426 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9427 struct ctl_lun *lun; 9428 int data_len; 9429 9430 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9431 9432 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9433 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9434 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9435 ctsio->kern_sg_entries = 0; 9436 9437 if (data_len < alloc_len) { 9438 ctsio->residual = alloc_len - data_len; 9439 ctsio->kern_data_len = data_len; 9440 ctsio->kern_total_len = data_len; 9441 } else { 9442 ctsio->residual = 0; 9443 ctsio->kern_data_len = alloc_len; 9444 ctsio->kern_total_len = alloc_len; 9445 } 9446 ctsio->kern_data_resid = 0; 9447 ctsio->kern_rel_offset = 0; 9448 ctsio->kern_sg_entries = 0; 9449 9450 /* 9451 * The control device is always connected. The disk device, on the 9452 * other hand, may not be online all the time. 9453 */ 9454 if (lun != NULL) 9455 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9456 lun->be_lun->lun_type; 9457 else 9458 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9459 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9460 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9461 /* 9462 * We support head of queue, ordered and simple tags. 9463 */ 9464 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9465 /* 9466 * Volatile cache supported. 9467 */ 9468 eid_ptr->flags3 = SVPD_EID_V_SUP; 9469 9470 /* 9471 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9472 * attention for a particular IT nexus on all LUNs once we report 9473 * it to that nexus once. This bit is required as of SPC-4. 9474 */ 9475 eid_ptr->flags4 = SVPD_EID_LUICLT; 9476 9477 /* 9478 * XXX KDM in order to correctly answer this, we would need 9479 * information from the SIM to determine how much sense data it 9480 * can send. So this would really be a path inquiry field, most 9481 * likely. This can be set to a maximum of 252 according to SPC-4, 9482 * but the hardware may or may not be able to support that much. 9483 * 0 just means that the maximum sense data length is not reported. 9484 */ 9485 eid_ptr->max_sense_length = 0; 9486 9487 ctl_set_success(ctsio); 9488 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9489 ctsio->be_move_done = ctl_config_move_done; 9490 ctl_datamove((union ctl_io *)ctsio); 9491 return (CTL_RETVAL_COMPLETE); 9492} 9493 9494static int 9495ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9496{ 9497 struct scsi_vpd_mode_page_policy *mpp_ptr; 9498 struct ctl_lun *lun; 9499 int data_len; 9500 9501 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9502 9503 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9504 sizeof(struct scsi_vpd_mode_page_policy_descr); 9505 9506 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9507 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9508 ctsio->kern_sg_entries = 0; 9509 9510 if (data_len < alloc_len) { 9511 ctsio->residual = alloc_len - data_len; 9512 ctsio->kern_data_len = data_len; 9513 ctsio->kern_total_len = data_len; 9514 } else { 9515 ctsio->residual = 0; 9516 ctsio->kern_data_len = alloc_len; 9517 ctsio->kern_total_len = alloc_len; 9518 } 9519 ctsio->kern_data_resid = 0; 9520 ctsio->kern_rel_offset = 0; 9521 ctsio->kern_sg_entries = 0; 9522 9523 /* 9524 * The control device is always connected. The disk device, on the 9525 * other hand, may not be online all the time. 9526 */ 9527 if (lun != NULL) 9528 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9529 lun->be_lun->lun_type; 9530 else 9531 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9532 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9533 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9534 mpp_ptr->descr[0].page_code = 0x3f; 9535 mpp_ptr->descr[0].subpage_code = 0xff; 9536 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9537 9538 ctl_set_success(ctsio); 9539 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9540 ctsio->be_move_done = ctl_config_move_done; 9541 ctl_datamove((union ctl_io *)ctsio); 9542 return (CTL_RETVAL_COMPLETE); 9543} 9544 9545/* 9546 * SCSI VPD page 0x83, the Device Identification page. 9547 */ 9548static int 9549ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9550{ 9551 struct scsi_vpd_device_id *devid_ptr; 9552 struct scsi_vpd_id_descriptor *desc; 9553 struct ctl_softc *softc; 9554 struct ctl_lun *lun; 9555 struct ctl_port *port; 9556 int data_len; 9557 uint8_t proto; 9558 9559 softc = control_softc; 9560 9561 port = ctl_io_port(&ctsio->io_hdr); 9562 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9563 9564 data_len = sizeof(struct scsi_vpd_device_id) + 9565 sizeof(struct scsi_vpd_id_descriptor) + 9566 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9567 sizeof(struct scsi_vpd_id_descriptor) + 9568 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9569 if (lun && lun->lun_devid) 9570 data_len += lun->lun_devid->len; 9571 if (port && port->port_devid) 9572 data_len += port->port_devid->len; 9573 if (port && port->target_devid) 9574 data_len += port->target_devid->len; 9575 9576 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9577 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9578 ctsio->kern_sg_entries = 0; 9579 9580 if (data_len < alloc_len) { 9581 ctsio->residual = alloc_len - data_len; 9582 ctsio->kern_data_len = data_len; 9583 ctsio->kern_total_len = data_len; 9584 } else { 9585 ctsio->residual = 0; 9586 ctsio->kern_data_len = alloc_len; 9587 ctsio->kern_total_len = alloc_len; 9588 } 9589 ctsio->kern_data_resid = 0; 9590 ctsio->kern_rel_offset = 0; 9591 ctsio->kern_sg_entries = 0; 9592 9593 /* 9594 * The control device is always connected. The disk device, on the 9595 * other hand, may not be online all the time. 9596 */ 9597 if (lun != NULL) 9598 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9599 lun->be_lun->lun_type; 9600 else 9601 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9602 devid_ptr->page_code = SVPD_DEVICE_ID; 9603 scsi_ulto2b(data_len - 4, devid_ptr->length); 9604 9605 if (port && port->port_type == CTL_PORT_FC) 9606 proto = SCSI_PROTO_FC << 4; 9607 else if (port && port->port_type == CTL_PORT_ISCSI) 9608 proto = SCSI_PROTO_ISCSI << 4; 9609 else 9610 proto = SCSI_PROTO_SPI << 4; 9611 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9612 9613 /* 9614 * We're using a LUN association here. i.e., this device ID is a 9615 * per-LUN identifier. 9616 */ 9617 if (lun && lun->lun_devid) { 9618 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9619 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9620 lun->lun_devid->len); 9621 } 9622 9623 /* 9624 * This is for the WWPN which is a port association. 9625 */ 9626 if (port && port->port_devid) { 9627 memcpy(desc, port->port_devid->data, port->port_devid->len); 9628 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9629 port->port_devid->len); 9630 } 9631 9632 /* 9633 * This is for the Relative Target Port(type 4h) identifier 9634 */ 9635 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9636 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9637 SVPD_ID_TYPE_RELTARG; 9638 desc->length = 4; 9639 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9640 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9641 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9642 9643 /* 9644 * This is for the Target Port Group(type 5h) identifier 9645 */ 9646 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9647 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9648 SVPD_ID_TYPE_TPORTGRP; 9649 desc->length = 4; 9650 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9651 &desc->identifier[2]); 9652 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9653 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9654 9655 /* 9656 * This is for the Target identifier 9657 */ 9658 if (port && port->target_devid) { 9659 memcpy(desc, port->target_devid->data, port->target_devid->len); 9660 } 9661 9662 ctl_set_success(ctsio); 9663 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9664 ctsio->be_move_done = ctl_config_move_done; 9665 ctl_datamove((union ctl_io *)ctsio); 9666 return (CTL_RETVAL_COMPLETE); 9667} 9668 9669static int 9670ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9671{ 9672 struct ctl_softc *softc = control_softc; 9673 struct scsi_vpd_scsi_ports *sp; 9674 struct scsi_vpd_port_designation *pd; 9675 struct scsi_vpd_port_designation_cont *pdc; 9676 struct ctl_lun *lun; 9677 struct ctl_port *port; 9678 int data_len, num_target_ports, iid_len, id_len; 9679 9680 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9681 9682 num_target_ports = 0; 9683 iid_len = 0; 9684 id_len = 0; 9685 mtx_lock(&softc->ctl_lock); 9686 STAILQ_FOREACH(port, &softc->port_list, links) { 9687 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9688 continue; 9689 if (lun != NULL && 9690 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9691 continue; 9692 num_target_ports++; 9693 if (port->init_devid) 9694 iid_len += port->init_devid->len; 9695 if (port->port_devid) 9696 id_len += port->port_devid->len; 9697 } 9698 mtx_unlock(&softc->ctl_lock); 9699 9700 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9701 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9702 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9703 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9704 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9705 ctsio->kern_sg_entries = 0; 9706 9707 if (data_len < alloc_len) { 9708 ctsio->residual = alloc_len - data_len; 9709 ctsio->kern_data_len = data_len; 9710 ctsio->kern_total_len = data_len; 9711 } else { 9712 ctsio->residual = 0; 9713 ctsio->kern_data_len = alloc_len; 9714 ctsio->kern_total_len = alloc_len; 9715 } 9716 ctsio->kern_data_resid = 0; 9717 ctsio->kern_rel_offset = 0; 9718 ctsio->kern_sg_entries = 0; 9719 9720 /* 9721 * The control device is always connected. The disk device, on the 9722 * other hand, may not be online all the time. Need to change this 9723 * to figure out whether the disk device is actually online or not. 9724 */ 9725 if (lun != NULL) 9726 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9727 lun->be_lun->lun_type; 9728 else 9729 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9730 9731 sp->page_code = SVPD_SCSI_PORTS; 9732 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9733 sp->page_length); 9734 pd = &sp->design[0]; 9735 9736 mtx_lock(&softc->ctl_lock); 9737 STAILQ_FOREACH(port, &softc->port_list, links) { 9738 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9739 continue; 9740 if (lun != NULL && 9741 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9742 continue; 9743 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9744 if (port->init_devid) { 9745 iid_len = port->init_devid->len; 9746 memcpy(pd->initiator_transportid, 9747 port->init_devid->data, port->init_devid->len); 9748 } else 9749 iid_len = 0; 9750 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9751 pdc = (struct scsi_vpd_port_designation_cont *) 9752 (&pd->initiator_transportid[iid_len]); 9753 if (port->port_devid) { 9754 id_len = port->port_devid->len; 9755 memcpy(pdc->target_port_descriptors, 9756 port->port_devid->data, port->port_devid->len); 9757 } else 9758 id_len = 0; 9759 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9760 pd = (struct scsi_vpd_port_designation *) 9761 ((uint8_t *)pdc->target_port_descriptors + id_len); 9762 } 9763 mtx_unlock(&softc->ctl_lock); 9764 9765 ctl_set_success(ctsio); 9766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9767 ctsio->be_move_done = ctl_config_move_done; 9768 ctl_datamove((union ctl_io *)ctsio); 9769 return (CTL_RETVAL_COMPLETE); 9770} 9771 9772static int 9773ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9774{ 9775 struct scsi_vpd_block_limits *bl_ptr; 9776 struct ctl_lun *lun; 9777 9778 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9779 9780 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9781 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9782 ctsio->kern_sg_entries = 0; 9783 9784 if (sizeof(*bl_ptr) < alloc_len) { 9785 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9786 ctsio->kern_data_len = sizeof(*bl_ptr); 9787 ctsio->kern_total_len = sizeof(*bl_ptr); 9788 } else { 9789 ctsio->residual = 0; 9790 ctsio->kern_data_len = alloc_len; 9791 ctsio->kern_total_len = alloc_len; 9792 } 9793 ctsio->kern_data_resid = 0; 9794 ctsio->kern_rel_offset = 0; 9795 ctsio->kern_sg_entries = 0; 9796 9797 /* 9798 * The control device is always connected. The disk device, on the 9799 * other hand, may not be online all the time. Need to change this 9800 * to figure out whether the disk device is actually online or not. 9801 */ 9802 if (lun != NULL) 9803 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9804 lun->be_lun->lun_type; 9805 else 9806 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9807 9808 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9809 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9810 bl_ptr->max_cmp_write_len = 0xff; 9811 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9812 if (lun != NULL) { 9813 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9814 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9815 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9816 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9817 if (lun->be_lun->ublockexp != 0) { 9818 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9819 bl_ptr->opt_unmap_grain); 9820 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9821 bl_ptr->unmap_grain_align); 9822 } 9823 } 9824 scsi_ulto4b(lun->be_lun->atomicblock, 9825 bl_ptr->max_atomic_transfer_length); 9826 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9827 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9828 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9829 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9830 } 9831 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9832 9833 ctl_set_success(ctsio); 9834 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9835 ctsio->be_move_done = ctl_config_move_done; 9836 ctl_datamove((union ctl_io *)ctsio); 9837 return (CTL_RETVAL_COMPLETE); 9838} 9839 9840static int 9841ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9842{ 9843 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9844 struct ctl_lun *lun; 9845 const char *value; 9846 u_int i; 9847 9848 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9849 9850 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9851 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9852 ctsio->kern_sg_entries = 0; 9853 9854 if (sizeof(*bdc_ptr) < alloc_len) { 9855 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9856 ctsio->kern_data_len = sizeof(*bdc_ptr); 9857 ctsio->kern_total_len = sizeof(*bdc_ptr); 9858 } else { 9859 ctsio->residual = 0; 9860 ctsio->kern_data_len = alloc_len; 9861 ctsio->kern_total_len = alloc_len; 9862 } 9863 ctsio->kern_data_resid = 0; 9864 ctsio->kern_rel_offset = 0; 9865 ctsio->kern_sg_entries = 0; 9866 9867 /* 9868 * The control device is always connected. The disk device, on the 9869 * other hand, may not be online all the time. Need to change this 9870 * to figure out whether the disk device is actually online or not. 9871 */ 9872 if (lun != NULL) 9873 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9874 lun->be_lun->lun_type; 9875 else 9876 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9877 bdc_ptr->page_code = SVPD_BDC; 9878 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9879 if (lun != NULL && 9880 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9881 i = strtol(value, NULL, 0); 9882 else 9883 i = CTL_DEFAULT_ROTATION_RATE; 9884 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9885 if (lun != NULL && 9886 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9887 i = strtol(value, NULL, 0); 9888 else 9889 i = 0; 9890 bdc_ptr->wab_wac_ff = (i & 0x0f); 9891 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9892 9893 ctl_set_success(ctsio); 9894 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9895 ctsio->be_move_done = ctl_config_move_done; 9896 ctl_datamove((union ctl_io *)ctsio); 9897 return (CTL_RETVAL_COMPLETE); 9898} 9899 9900static int 9901ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9902{ 9903 struct scsi_vpd_logical_block_prov *lbp_ptr; 9904 struct ctl_lun *lun; 9905 9906 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9907 9908 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9909 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9910 ctsio->kern_sg_entries = 0; 9911 9912 if (sizeof(*lbp_ptr) < alloc_len) { 9913 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9914 ctsio->kern_data_len = sizeof(*lbp_ptr); 9915 ctsio->kern_total_len = sizeof(*lbp_ptr); 9916 } else { 9917 ctsio->residual = 0; 9918 ctsio->kern_data_len = alloc_len; 9919 ctsio->kern_total_len = alloc_len; 9920 } 9921 ctsio->kern_data_resid = 0; 9922 ctsio->kern_rel_offset = 0; 9923 ctsio->kern_sg_entries = 0; 9924 9925 /* 9926 * The control device is always connected. The disk device, on the 9927 * other hand, may not be online all the time. Need to change this 9928 * to figure out whether the disk device is actually online or not. 9929 */ 9930 if (lun != NULL) 9931 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9932 lun->be_lun->lun_type; 9933 else 9934 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9935 9936 lbp_ptr->page_code = SVPD_LBP; 9937 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9938 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9939 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9940 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9941 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9942 lbp_ptr->prov_type = SVPD_LBP_THIN; 9943 } 9944 9945 ctl_set_success(ctsio); 9946 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9947 ctsio->be_move_done = ctl_config_move_done; 9948 ctl_datamove((union ctl_io *)ctsio); 9949 return (CTL_RETVAL_COMPLETE); 9950} 9951 9952/* 9953 * INQUIRY with the EVPD bit set. 9954 */ 9955static int 9956ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9957{ 9958 struct ctl_lun *lun; 9959 struct scsi_inquiry *cdb; 9960 int alloc_len, retval; 9961 9962 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9963 cdb = (struct scsi_inquiry *)ctsio->cdb; 9964 alloc_len = scsi_2btoul(cdb->length); 9965 9966 switch (cdb->page_code) { 9967 case SVPD_SUPPORTED_PAGES: 9968 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9969 break; 9970 case SVPD_UNIT_SERIAL_NUMBER: 9971 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9972 break; 9973 case SVPD_DEVICE_ID: 9974 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9975 break; 9976 case SVPD_EXTENDED_INQUIRY_DATA: 9977 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 9978 break; 9979 case SVPD_MODE_PAGE_POLICY: 9980 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 9981 break; 9982 case SVPD_SCSI_PORTS: 9983 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 9984 break; 9985 case SVPD_SCSI_TPC: 9986 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 9987 break; 9988 case SVPD_BLOCK_LIMITS: 9989 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9990 goto err; 9991 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9992 break; 9993 case SVPD_BDC: 9994 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 9995 goto err; 9996 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 9997 break; 9998 case SVPD_LBP: 9999 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10000 goto err; 10001 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10002 break; 10003 default: 10004err: 10005 ctl_set_invalid_field(ctsio, 10006 /*sks_valid*/ 1, 10007 /*command*/ 1, 10008 /*field*/ 2, 10009 /*bit_valid*/ 0, 10010 /*bit*/ 0); 10011 ctl_done((union ctl_io *)ctsio); 10012 retval = CTL_RETVAL_COMPLETE; 10013 break; 10014 } 10015 10016 return (retval); 10017} 10018 10019/* 10020 * Standard INQUIRY data. 10021 */ 10022static int 10023ctl_inquiry_std(struct ctl_scsiio *ctsio) 10024{ 10025 struct scsi_inquiry_data *inq_ptr; 10026 struct scsi_inquiry *cdb; 10027 struct ctl_softc *softc = control_softc; 10028 struct ctl_port *port; 10029 struct ctl_lun *lun; 10030 char *val; 10031 uint32_t alloc_len, data_len; 10032 ctl_port_type port_type; 10033 10034 port = ctl_io_port(&ctsio->io_hdr); 10035 port_type = port->port_type; 10036 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10037 port_type = CTL_PORT_SCSI; 10038 10039 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10040 cdb = (struct scsi_inquiry *)ctsio->cdb; 10041 alloc_len = scsi_2btoul(cdb->length); 10042 10043 /* 10044 * We malloc the full inquiry data size here and fill it 10045 * in. If the user only asks for less, we'll give him 10046 * that much. 10047 */ 10048 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10049 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10050 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10051 ctsio->kern_sg_entries = 0; 10052 ctsio->kern_data_resid = 0; 10053 ctsio->kern_rel_offset = 0; 10054 10055 if (data_len < alloc_len) { 10056 ctsio->residual = alloc_len - data_len; 10057 ctsio->kern_data_len = data_len; 10058 ctsio->kern_total_len = data_len; 10059 } else { 10060 ctsio->residual = 0; 10061 ctsio->kern_data_len = alloc_len; 10062 ctsio->kern_total_len = alloc_len; 10063 } 10064 10065 if (lun != NULL) { 10066 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10067 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10068 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10069 lun->be_lun->lun_type; 10070 } else { 10071 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10072 lun->be_lun->lun_type; 10073 } 10074 if (lun->flags & CTL_LUN_REMOVABLE) 10075 inq_ptr->dev_qual2 |= SID_RMB; 10076 } else 10077 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10078 10079 /* RMB in byte 2 is 0 */ 10080 inq_ptr->version = SCSI_REV_SPC4; 10081 10082 /* 10083 * According to SAM-3, even if a device only supports a single 10084 * level of LUN addressing, it should still set the HISUP bit: 10085 * 10086 * 4.9.1 Logical unit numbers overview 10087 * 10088 * All logical unit number formats described in this standard are 10089 * hierarchical in structure even when only a single level in that 10090 * hierarchy is used. The HISUP bit shall be set to one in the 10091 * standard INQUIRY data (see SPC-2) when any logical unit number 10092 * format described in this standard is used. Non-hierarchical 10093 * formats are outside the scope of this standard. 10094 * 10095 * Therefore we set the HiSup bit here. 10096 * 10097 * The reponse format is 2, per SPC-3. 10098 */ 10099 inq_ptr->response_format = SID_HiSup | 2; 10100 10101 inq_ptr->additional_length = data_len - 10102 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10103 CTL_DEBUG_PRINT(("additional_length = %d\n", 10104 inq_ptr->additional_length)); 10105 10106 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10107 if (port_type == CTL_PORT_SCSI) 10108 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10109 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10110 inq_ptr->flags = SID_CmdQue; 10111 if (port_type == CTL_PORT_SCSI) 10112 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10113 10114 /* 10115 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10116 * We have 8 bytes for the vendor name, and 16 bytes for the device 10117 * name and 4 bytes for the revision. 10118 */ 10119 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10120 "vendor")) == NULL) { 10121 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10122 } else { 10123 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10124 strncpy(inq_ptr->vendor, val, 10125 min(sizeof(inq_ptr->vendor), strlen(val))); 10126 } 10127 if (lun == NULL) { 10128 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10129 sizeof(inq_ptr->product)); 10130 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10131 switch (lun->be_lun->lun_type) { 10132 case T_DIRECT: 10133 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10134 sizeof(inq_ptr->product)); 10135 break; 10136 case T_PROCESSOR: 10137 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10138 sizeof(inq_ptr->product)); 10139 break; 10140 case T_CDROM: 10141 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10142 sizeof(inq_ptr->product)); 10143 break; 10144 default: 10145 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10146 sizeof(inq_ptr->product)); 10147 break; 10148 } 10149 } else { 10150 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10151 strncpy(inq_ptr->product, val, 10152 min(sizeof(inq_ptr->product), strlen(val))); 10153 } 10154 10155 /* 10156 * XXX make this a macro somewhere so it automatically gets 10157 * incremented when we make changes. 10158 */ 10159 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10160 "revision")) == NULL) { 10161 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10162 } else { 10163 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10164 strncpy(inq_ptr->revision, val, 10165 min(sizeof(inq_ptr->revision), strlen(val))); 10166 } 10167 10168 /* 10169 * For parallel SCSI, we support double transition and single 10170 * transition clocking. We also support QAS (Quick Arbitration 10171 * and Selection) and Information Unit transfers on both the 10172 * control and array devices. 10173 */ 10174 if (port_type == CTL_PORT_SCSI) 10175 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10176 SID_SPI_IUS; 10177 10178 /* SAM-5 (no version claimed) */ 10179 scsi_ulto2b(0x00A0, inq_ptr->version1); 10180 /* SPC-4 (no version claimed) */ 10181 scsi_ulto2b(0x0460, inq_ptr->version2); 10182 if (port_type == CTL_PORT_FC) { 10183 /* FCP-2 ANSI INCITS.350:2003 */ 10184 scsi_ulto2b(0x0917, inq_ptr->version3); 10185 } else if (port_type == CTL_PORT_SCSI) { 10186 /* SPI-4 ANSI INCITS.362:200x */ 10187 scsi_ulto2b(0x0B56, inq_ptr->version3); 10188 } else if (port_type == CTL_PORT_ISCSI) { 10189 /* iSCSI (no version claimed) */ 10190 scsi_ulto2b(0x0960, inq_ptr->version3); 10191 } else if (port_type == CTL_PORT_SAS) { 10192 /* SAS (no version claimed) */ 10193 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10194 } 10195 10196 if (lun == NULL) { 10197 /* SBC-4 (no version claimed) */ 10198 scsi_ulto2b(0x0600, inq_ptr->version4); 10199 } else { 10200 switch (lun->be_lun->lun_type) { 10201 case T_DIRECT: 10202 /* SBC-4 (no version claimed) */ 10203 scsi_ulto2b(0x0600, inq_ptr->version4); 10204 break; 10205 case T_PROCESSOR: 10206 break; 10207 case T_CDROM: 10208 /* MMC-6 (no version claimed) */ 10209 scsi_ulto2b(0x04E0, inq_ptr->version4); 10210 break; 10211 default: 10212 break; 10213 } 10214 } 10215 10216 ctl_set_success(ctsio); 10217 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10218 ctsio->be_move_done = ctl_config_move_done; 10219 ctl_datamove((union ctl_io *)ctsio); 10220 return (CTL_RETVAL_COMPLETE); 10221} 10222 10223int 10224ctl_inquiry(struct ctl_scsiio *ctsio) 10225{ 10226 struct scsi_inquiry *cdb; 10227 int retval; 10228 10229 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10230 10231 cdb = (struct scsi_inquiry *)ctsio->cdb; 10232 if (cdb->byte2 & SI_EVPD) 10233 retval = ctl_inquiry_evpd(ctsio); 10234 else if (cdb->page_code == 0) 10235 retval = ctl_inquiry_std(ctsio); 10236 else { 10237 ctl_set_invalid_field(ctsio, 10238 /*sks_valid*/ 1, 10239 /*command*/ 1, 10240 /*field*/ 2, 10241 /*bit_valid*/ 0, 10242 /*bit*/ 0); 10243 ctl_done((union ctl_io *)ctsio); 10244 return (CTL_RETVAL_COMPLETE); 10245 } 10246 10247 return (retval); 10248} 10249 10250int 10251ctl_get_config(struct ctl_scsiio *ctsio) 10252{ 10253 struct scsi_get_config_header *hdr; 10254 struct scsi_get_config_feature *feature; 10255 struct scsi_get_config *cdb; 10256 struct ctl_lun *lun; 10257 uint32_t alloc_len, data_len; 10258 int rt, starting; 10259 10260 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10261 cdb = (struct scsi_get_config *)ctsio->cdb; 10262 rt = (cdb->rt & SGC_RT_MASK); 10263 starting = scsi_2btoul(cdb->starting_feature); 10264 alloc_len = scsi_2btoul(cdb->length); 10265 10266 data_len = sizeof(struct scsi_get_config_header) + 10267 sizeof(struct scsi_get_config_feature) + 8 + 10268 sizeof(struct scsi_get_config_feature) + 8 + 10269 sizeof(struct scsi_get_config_feature) + 4 + 10270 sizeof(struct scsi_get_config_feature) + 4 + 10271 sizeof(struct scsi_get_config_feature) + 8 + 10272 sizeof(struct scsi_get_config_feature) + 10273 sizeof(struct scsi_get_config_feature) + 4 + 10274 sizeof(struct scsi_get_config_feature) + 4; 10275 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10276 ctsio->kern_sg_entries = 0; 10277 ctsio->kern_data_resid = 0; 10278 ctsio->kern_rel_offset = 0; 10279 10280 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10281 if (lun->flags & CTL_LUN_NO_MEDIA) 10282 scsi_ulto2b(0x0000, hdr->current_profile); 10283 else 10284 scsi_ulto2b(0x0010, hdr->current_profile); 10285 feature = (struct scsi_get_config_feature *)(hdr + 1); 10286 10287 if (starting > 0x001f) 10288 goto done; 10289 if (starting > 0x001e) 10290 goto f1f; 10291 if (starting > 0x001d) 10292 goto f1e; 10293 if (starting > 0x0010) 10294 goto f1d; 10295 if (starting > 0x0003) 10296 goto f10; 10297 if (starting > 0x0002) 10298 goto f3; 10299 if (starting > 0x0001) 10300 goto f2; 10301 if (starting > 0x0000) 10302 goto f1; 10303 10304 /* Profile List */ 10305 scsi_ulto2b(0x0000, feature->feature_code); 10306 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10307 feature->add_length = 8; 10308 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10309 feature->feature_data[2] = 0x00; 10310 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10311 feature->feature_data[6] = 0x01; 10312 feature = (struct scsi_get_config_feature *) 10313 &feature->feature_data[feature->add_length]; 10314 10315f1: /* Core */ 10316 scsi_ulto2b(0x0001, feature->feature_code); 10317 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10318 feature->add_length = 8; 10319 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10320 feature->feature_data[4] = 0x03; 10321 feature = (struct scsi_get_config_feature *) 10322 &feature->feature_data[feature->add_length]; 10323 10324f2: /* Morphing */ 10325 scsi_ulto2b(0x0002, feature->feature_code); 10326 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10327 feature->add_length = 4; 10328 feature->feature_data[0] = 0x02; 10329 feature = (struct scsi_get_config_feature *) 10330 &feature->feature_data[feature->add_length]; 10331 10332f3: /* Removable Medium */ 10333 scsi_ulto2b(0x0003, feature->feature_code); 10334 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10335 feature->add_length = 4; 10336 feature->feature_data[0] = 0x39; 10337 feature = (struct scsi_get_config_feature *) 10338 &feature->feature_data[feature->add_length]; 10339 10340 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10341 goto done; 10342 10343f10: /* Random Read */ 10344 scsi_ulto2b(0x0010, feature->feature_code); 10345 feature->flags = 0x00; 10346 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10347 feature->flags |= SGC_F_CURRENT; 10348 feature->add_length = 8; 10349 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10350 scsi_ulto2b(1, &feature->feature_data[4]); 10351 feature->feature_data[6] = 0x00; 10352 feature = (struct scsi_get_config_feature *) 10353 &feature->feature_data[feature->add_length]; 10354 10355f1d: /* Multi-Read */ 10356 scsi_ulto2b(0x001D, feature->feature_code); 10357 feature->flags = 0x00; 10358 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10359 feature->flags |= SGC_F_CURRENT; 10360 feature->add_length = 0; 10361 feature = (struct scsi_get_config_feature *) 10362 &feature->feature_data[feature->add_length]; 10363 10364f1e: /* CD Read */ 10365 scsi_ulto2b(0x001E, feature->feature_code); 10366 feature->flags = 0x00; 10367 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10368 feature->flags |= SGC_F_CURRENT; 10369 feature->add_length = 4; 10370 feature->feature_data[0] = 0x00; 10371 feature = (struct scsi_get_config_feature *) 10372 &feature->feature_data[feature->add_length]; 10373 10374f1f: /* DVD Read */ 10375 scsi_ulto2b(0x001F, feature->feature_code); 10376 feature->flags = 0x08; 10377 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10378 feature->flags |= SGC_F_CURRENT; 10379 feature->add_length = 4; 10380 feature->feature_data[0] = 0x01; 10381 feature->feature_data[2] = 0x03; 10382 feature = (struct scsi_get_config_feature *) 10383 &feature->feature_data[feature->add_length]; 10384 10385done: 10386 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10387 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10388 feature = (struct scsi_get_config_feature *)(hdr + 1); 10389 if (scsi_2btoul(feature->feature_code) == starting) 10390 feature = (struct scsi_get_config_feature *) 10391 &feature->feature_data[feature->add_length]; 10392 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10393 } 10394 scsi_ulto4b(data_len - 4, hdr->data_length); 10395 if (data_len < alloc_len) { 10396 ctsio->residual = alloc_len - data_len; 10397 ctsio->kern_data_len = data_len; 10398 ctsio->kern_total_len = data_len; 10399 } else { 10400 ctsio->residual = 0; 10401 ctsio->kern_data_len = alloc_len; 10402 ctsio->kern_total_len = alloc_len; 10403 } 10404 10405 ctl_set_success(ctsio); 10406 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10407 ctsio->be_move_done = ctl_config_move_done; 10408 ctl_datamove((union ctl_io *)ctsio); 10409 return (CTL_RETVAL_COMPLETE); 10410} 10411 10412int 10413ctl_get_event_status(struct ctl_scsiio *ctsio) 10414{ 10415 struct scsi_get_event_status_header *hdr; 10416 struct scsi_get_event_status *cdb; 10417 struct ctl_lun *lun; 10418 uint32_t alloc_len, data_len; 10419 int notif_class; 10420 10421 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10422 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10423 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10424 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10425 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10426 ctl_done((union ctl_io *)ctsio); 10427 return (CTL_RETVAL_COMPLETE); 10428 } 10429 notif_class = cdb->notif_class; 10430 alloc_len = scsi_2btoul(cdb->length); 10431 10432 data_len = sizeof(struct scsi_get_event_status_header); 10433 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10434 ctsio->kern_sg_entries = 0; 10435 ctsio->kern_data_resid = 0; 10436 ctsio->kern_rel_offset = 0; 10437 10438 if (data_len < alloc_len) { 10439 ctsio->residual = alloc_len - data_len; 10440 ctsio->kern_data_len = data_len; 10441 ctsio->kern_total_len = data_len; 10442 } else { 10443 ctsio->residual = 0; 10444 ctsio->kern_data_len = alloc_len; 10445 ctsio->kern_total_len = alloc_len; 10446 } 10447 10448 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10449 scsi_ulto2b(0, hdr->descr_length); 10450 hdr->nea_class = SGESN_NEA; 10451 hdr->supported_class = 0; 10452 10453 ctl_set_success(ctsio); 10454 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10455 ctsio->be_move_done = ctl_config_move_done; 10456 ctl_datamove((union ctl_io *)ctsio); 10457 return (CTL_RETVAL_COMPLETE); 10458} 10459 10460int 10461ctl_mechanism_status(struct ctl_scsiio *ctsio) 10462{ 10463 struct scsi_mechanism_status_header *hdr; 10464 struct scsi_mechanism_status *cdb; 10465 struct ctl_lun *lun; 10466 uint32_t alloc_len, data_len; 10467 10468 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10469 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10470 alloc_len = scsi_2btoul(cdb->length); 10471 10472 data_len = sizeof(struct scsi_mechanism_status_header); 10473 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10474 ctsio->kern_sg_entries = 0; 10475 ctsio->kern_data_resid = 0; 10476 ctsio->kern_rel_offset = 0; 10477 10478 if (data_len < alloc_len) { 10479 ctsio->residual = alloc_len - data_len; 10480 ctsio->kern_data_len = data_len; 10481 ctsio->kern_total_len = data_len; 10482 } else { 10483 ctsio->residual = 0; 10484 ctsio->kern_data_len = alloc_len; 10485 ctsio->kern_total_len = alloc_len; 10486 } 10487 10488 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10489 hdr->state1 = 0x00; 10490 hdr->state2 = 0xe0; 10491 scsi_ulto3b(0, hdr->lba); 10492 hdr->slots_num = 0; 10493 scsi_ulto2b(0, hdr->slots_length); 10494 10495 ctl_set_success(ctsio); 10496 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10497 ctsio->be_move_done = ctl_config_move_done; 10498 ctl_datamove((union ctl_io *)ctsio); 10499 return (CTL_RETVAL_COMPLETE); 10500} 10501 10502static void 10503ctl_ultomsf(uint32_t lba, uint8_t *buf) 10504{ 10505 10506 lba += 150; 10507 buf[0] = 0; 10508 buf[1] = bin2bcd((lba / 75) / 60); 10509 buf[2] = bin2bcd((lba / 75) % 60); 10510 buf[3] = bin2bcd(lba % 75); 10511} 10512 10513int 10514ctl_read_toc(struct ctl_scsiio *ctsio) 10515{ 10516 struct scsi_read_toc_hdr *hdr; 10517 struct scsi_read_toc_type01_descr *descr; 10518 struct scsi_read_toc *cdb; 10519 struct ctl_lun *lun; 10520 uint32_t alloc_len, data_len; 10521 int format, msf; 10522 10523 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10524 cdb = (struct scsi_read_toc *)ctsio->cdb; 10525 msf = (cdb->byte2 & CD_MSF) != 0; 10526 format = cdb->format; 10527 alloc_len = scsi_2btoul(cdb->data_len); 10528 10529 data_len = sizeof(struct scsi_read_toc_hdr); 10530 if (format == 0) 10531 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10532 else 10533 data_len += sizeof(struct scsi_read_toc_type01_descr); 10534 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10535 ctsio->kern_sg_entries = 0; 10536 ctsio->kern_data_resid = 0; 10537 ctsio->kern_rel_offset = 0; 10538 10539 if (data_len < alloc_len) { 10540 ctsio->residual = alloc_len - data_len; 10541 ctsio->kern_data_len = data_len; 10542 ctsio->kern_total_len = data_len; 10543 } else { 10544 ctsio->residual = 0; 10545 ctsio->kern_data_len = alloc_len; 10546 ctsio->kern_total_len = alloc_len; 10547 } 10548 10549 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10550 if (format == 0) { 10551 scsi_ulto2b(0x12, hdr->data_length); 10552 hdr->first = 1; 10553 hdr->last = 1; 10554 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10555 descr->addr_ctl = 0x14; 10556 descr->track_number = 1; 10557 if (msf) 10558 ctl_ultomsf(0, descr->track_start); 10559 else 10560 scsi_ulto4b(0, descr->track_start); 10561 descr++; 10562 descr->addr_ctl = 0x14; 10563 descr->track_number = 0xaa; 10564 if (msf) 10565 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10566 else 10567 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10568 } else { 10569 scsi_ulto2b(0x0a, hdr->data_length); 10570 hdr->first = 1; 10571 hdr->last = 1; 10572 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10573 descr->addr_ctl = 0x14; 10574 descr->track_number = 1; 10575 if (msf) 10576 ctl_ultomsf(0, descr->track_start); 10577 else 10578 scsi_ulto4b(0, descr->track_start); 10579 } 10580 10581 ctl_set_success(ctsio); 10582 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10583 ctsio->be_move_done = ctl_config_move_done; 10584 ctl_datamove((union ctl_io *)ctsio); 10585 return (CTL_RETVAL_COMPLETE); 10586} 10587 10588/* 10589 * For known CDB types, parse the LBA and length. 10590 */ 10591static int 10592ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10593{ 10594 if (io->io_hdr.io_type != CTL_IO_SCSI) 10595 return (1); 10596 10597 switch (io->scsiio.cdb[0]) { 10598 case COMPARE_AND_WRITE: { 10599 struct scsi_compare_and_write *cdb; 10600 10601 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10602 10603 *lba = scsi_8btou64(cdb->addr); 10604 *len = cdb->length; 10605 break; 10606 } 10607 case READ_6: 10608 case WRITE_6: { 10609 struct scsi_rw_6 *cdb; 10610 10611 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10612 10613 *lba = scsi_3btoul(cdb->addr); 10614 /* only 5 bits are valid in the most significant address byte */ 10615 *lba &= 0x1fffff; 10616 *len = cdb->length; 10617 break; 10618 } 10619 case READ_10: 10620 case WRITE_10: { 10621 struct scsi_rw_10 *cdb; 10622 10623 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10624 10625 *lba = scsi_4btoul(cdb->addr); 10626 *len = scsi_2btoul(cdb->length); 10627 break; 10628 } 10629 case WRITE_VERIFY_10: { 10630 struct scsi_write_verify_10 *cdb; 10631 10632 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10633 10634 *lba = scsi_4btoul(cdb->addr); 10635 *len = scsi_2btoul(cdb->length); 10636 break; 10637 } 10638 case READ_12: 10639 case WRITE_12: { 10640 struct scsi_rw_12 *cdb; 10641 10642 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10643 10644 *lba = scsi_4btoul(cdb->addr); 10645 *len = scsi_4btoul(cdb->length); 10646 break; 10647 } 10648 case WRITE_VERIFY_12: { 10649 struct scsi_write_verify_12 *cdb; 10650 10651 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10652 10653 *lba = scsi_4btoul(cdb->addr); 10654 *len = scsi_4btoul(cdb->length); 10655 break; 10656 } 10657 case READ_16: 10658 case WRITE_16: { 10659 struct scsi_rw_16 *cdb; 10660 10661 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10662 10663 *lba = scsi_8btou64(cdb->addr); 10664 *len = scsi_4btoul(cdb->length); 10665 break; 10666 } 10667 case WRITE_ATOMIC_16: { 10668 struct scsi_write_atomic_16 *cdb; 10669 10670 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10671 10672 *lba = scsi_8btou64(cdb->addr); 10673 *len = scsi_2btoul(cdb->length); 10674 break; 10675 } 10676 case WRITE_VERIFY_16: { 10677 struct scsi_write_verify_16 *cdb; 10678 10679 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10680 10681 *lba = scsi_8btou64(cdb->addr); 10682 *len = scsi_4btoul(cdb->length); 10683 break; 10684 } 10685 case WRITE_SAME_10: { 10686 struct scsi_write_same_10 *cdb; 10687 10688 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10689 10690 *lba = scsi_4btoul(cdb->addr); 10691 *len = scsi_2btoul(cdb->length); 10692 break; 10693 } 10694 case WRITE_SAME_16: { 10695 struct scsi_write_same_16 *cdb; 10696 10697 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10698 10699 *lba = scsi_8btou64(cdb->addr); 10700 *len = scsi_4btoul(cdb->length); 10701 break; 10702 } 10703 case VERIFY_10: { 10704 struct scsi_verify_10 *cdb; 10705 10706 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10707 10708 *lba = scsi_4btoul(cdb->addr); 10709 *len = scsi_2btoul(cdb->length); 10710 break; 10711 } 10712 case VERIFY_12: { 10713 struct scsi_verify_12 *cdb; 10714 10715 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10716 10717 *lba = scsi_4btoul(cdb->addr); 10718 *len = scsi_4btoul(cdb->length); 10719 break; 10720 } 10721 case VERIFY_16: { 10722 struct scsi_verify_16 *cdb; 10723 10724 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10725 10726 *lba = scsi_8btou64(cdb->addr); 10727 *len = scsi_4btoul(cdb->length); 10728 break; 10729 } 10730 case UNMAP: { 10731 *lba = 0; 10732 *len = UINT64_MAX; 10733 break; 10734 } 10735 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10736 struct scsi_get_lba_status *cdb; 10737 10738 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10739 *lba = scsi_8btou64(cdb->addr); 10740 *len = UINT32_MAX; 10741 break; 10742 } 10743 default: 10744 return (1); 10745 break; /* NOTREACHED */ 10746 } 10747 10748 return (0); 10749} 10750 10751static ctl_action 10752ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10753 bool seq) 10754{ 10755 uint64_t endlba1, endlba2; 10756 10757 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10758 endlba2 = lba2 + len2 - 1; 10759 10760 if ((endlba1 < lba2) || (endlba2 < lba1)) 10761 return (CTL_ACTION_PASS); 10762 else 10763 return (CTL_ACTION_BLOCK); 10764} 10765 10766static int 10767ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10768{ 10769 struct ctl_ptr_len_flags *ptrlen; 10770 struct scsi_unmap_desc *buf, *end, *range; 10771 uint64_t lba; 10772 uint32_t len; 10773 10774 /* If not UNMAP -- go other way. */ 10775 if (io->io_hdr.io_type != CTL_IO_SCSI || 10776 io->scsiio.cdb[0] != UNMAP) 10777 return (CTL_ACTION_ERROR); 10778 10779 /* If UNMAP without data -- block and wait for data. */ 10780 ptrlen = (struct ctl_ptr_len_flags *) 10781 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10782 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10783 ptrlen->ptr == NULL) 10784 return (CTL_ACTION_BLOCK); 10785 10786 /* UNMAP with data -- check for collision. */ 10787 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10788 end = buf + ptrlen->len / sizeof(*buf); 10789 for (range = buf; range < end; range++) { 10790 lba = scsi_8btou64(range->lba); 10791 len = scsi_4btoul(range->length); 10792 if ((lba < lba2 + len2) && (lba + len > lba2)) 10793 return (CTL_ACTION_BLOCK); 10794 } 10795 return (CTL_ACTION_PASS); 10796} 10797 10798static ctl_action 10799ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10800{ 10801 uint64_t lba1, lba2; 10802 uint64_t len1, len2; 10803 int retval; 10804 10805 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10806 return (CTL_ACTION_ERROR); 10807 10808 retval = ctl_extent_check_unmap(io1, lba2, len2); 10809 if (retval != CTL_ACTION_ERROR) 10810 return (retval); 10811 10812 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10813 return (CTL_ACTION_ERROR); 10814 10815 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10816 seq = FALSE; 10817 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10818} 10819 10820static ctl_action 10821ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10822{ 10823 uint64_t lba1, lba2; 10824 uint64_t len1, len2; 10825 10826 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10827 return (CTL_ACTION_PASS); 10828 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10829 return (CTL_ACTION_ERROR); 10830 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10831 return (CTL_ACTION_ERROR); 10832 10833 if (lba1 + len1 == lba2) 10834 return (CTL_ACTION_BLOCK); 10835 return (CTL_ACTION_PASS); 10836} 10837 10838static ctl_action 10839ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10840 union ctl_io *ooa_io) 10841{ 10842 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10843 const ctl_serialize_action *serialize_row; 10844 10845 /* 10846 * The initiator attempted multiple untagged commands at the same 10847 * time. Can't do that. 10848 */ 10849 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10850 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10851 && ((pending_io->io_hdr.nexus.targ_port == 10852 ooa_io->io_hdr.nexus.targ_port) 10853 && (pending_io->io_hdr.nexus.initid == 10854 ooa_io->io_hdr.nexus.initid)) 10855 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10856 CTL_FLAG_STATUS_SENT)) == 0)) 10857 return (CTL_ACTION_OVERLAP); 10858 10859 /* 10860 * The initiator attempted to send multiple tagged commands with 10861 * the same ID. (It's fine if different initiators have the same 10862 * tag ID.) 10863 * 10864 * Even if all of those conditions are true, we don't kill the I/O 10865 * if the command ahead of us has been aborted. We won't end up 10866 * sending it to the FETD, and it's perfectly legal to resend a 10867 * command with the same tag number as long as the previous 10868 * instance of this tag number has been aborted somehow. 10869 */ 10870 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10871 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10872 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10873 && ((pending_io->io_hdr.nexus.targ_port == 10874 ooa_io->io_hdr.nexus.targ_port) 10875 && (pending_io->io_hdr.nexus.initid == 10876 ooa_io->io_hdr.nexus.initid)) 10877 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10878 CTL_FLAG_STATUS_SENT)) == 0)) 10879 return (CTL_ACTION_OVERLAP_TAG); 10880 10881 /* 10882 * If we get a head of queue tag, SAM-3 says that we should 10883 * immediately execute it. 10884 * 10885 * What happens if this command would normally block for some other 10886 * reason? e.g. a request sense with a head of queue tag 10887 * immediately after a write. Normally that would block, but this 10888 * will result in its getting executed immediately... 10889 * 10890 * We currently return "pass" instead of "skip", so we'll end up 10891 * going through the rest of the queue to check for overlapped tags. 10892 * 10893 * XXX KDM check for other types of blockage first?? 10894 */ 10895 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10896 return (CTL_ACTION_PASS); 10897 10898 /* 10899 * Ordered tags have to block until all items ahead of them 10900 * have completed. If we get called with an ordered tag, we always 10901 * block, if something else is ahead of us in the queue. 10902 */ 10903 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10904 return (CTL_ACTION_BLOCK); 10905 10906 /* 10907 * Simple tags get blocked until all head of queue and ordered tags 10908 * ahead of them have completed. I'm lumping untagged commands in 10909 * with simple tags here. XXX KDM is that the right thing to do? 10910 */ 10911 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10912 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10913 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10914 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10915 return (CTL_ACTION_BLOCK); 10916 10917 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10918 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10919 10920 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10921 10922 switch (serialize_row[pending_entry->seridx]) { 10923 case CTL_SER_BLOCK: 10924 return (CTL_ACTION_BLOCK); 10925 case CTL_SER_EXTENT: 10926 return (ctl_extent_check(ooa_io, pending_io, 10927 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10928 case CTL_SER_EXTENTOPT: 10929 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10930 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10931 return (ctl_extent_check(ooa_io, pending_io, 10932 (lun->be_lun && 10933 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 10934 return (CTL_ACTION_PASS); 10935 case CTL_SER_EXTENTSEQ: 10936 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 10937 return (ctl_extent_check_seq(ooa_io, pending_io)); 10938 return (CTL_ACTION_PASS); 10939 case CTL_SER_PASS: 10940 return (CTL_ACTION_PASS); 10941 case CTL_SER_BLOCKOPT: 10942 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10943 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10944 return (CTL_ACTION_BLOCK); 10945 return (CTL_ACTION_PASS); 10946 case CTL_SER_SKIP: 10947 return (CTL_ACTION_SKIP); 10948 default: 10949 panic("invalid serialization value %d", 10950 serialize_row[pending_entry->seridx]); 10951 } 10952 10953 return (CTL_ACTION_ERROR); 10954} 10955 10956/* 10957 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10958 * Assumptions: 10959 * - pending_io is generally either incoming, or on the blocked queue 10960 * - starting I/O is the I/O we want to start the check with. 10961 */ 10962static ctl_action 10963ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10964 union ctl_io *starting_io) 10965{ 10966 union ctl_io *ooa_io; 10967 ctl_action action; 10968 10969 mtx_assert(&lun->lun_lock, MA_OWNED); 10970 10971 /* 10972 * Run back along the OOA queue, starting with the current 10973 * blocked I/O and going through every I/O before it on the 10974 * queue. If starting_io is NULL, we'll just end up returning 10975 * CTL_ACTION_PASS. 10976 */ 10977 for (ooa_io = starting_io; ooa_io != NULL; 10978 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10979 ooa_links)){ 10980 10981 /* 10982 * This routine just checks to see whether 10983 * cur_blocked is blocked by ooa_io, which is ahead 10984 * of it in the queue. It doesn't queue/dequeue 10985 * cur_blocked. 10986 */ 10987 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10988 switch (action) { 10989 case CTL_ACTION_BLOCK: 10990 case CTL_ACTION_OVERLAP: 10991 case CTL_ACTION_OVERLAP_TAG: 10992 case CTL_ACTION_SKIP: 10993 case CTL_ACTION_ERROR: 10994 return (action); 10995 break; /* NOTREACHED */ 10996 case CTL_ACTION_PASS: 10997 break; 10998 default: 10999 panic("invalid action %d", action); 11000 break; /* NOTREACHED */ 11001 } 11002 } 11003 11004 return (CTL_ACTION_PASS); 11005} 11006 11007/* 11008 * Assumptions: 11009 * - An I/O has just completed, and has been removed from the per-LUN OOA 11010 * queue, so some items on the blocked queue may now be unblocked. 11011 */ 11012static int 11013ctl_check_blocked(struct ctl_lun *lun) 11014{ 11015 struct ctl_softc *softc = lun->ctl_softc; 11016 union ctl_io *cur_blocked, *next_blocked; 11017 11018 mtx_assert(&lun->lun_lock, MA_OWNED); 11019 11020 /* 11021 * Run forward from the head of the blocked queue, checking each 11022 * entry against the I/Os prior to it on the OOA queue to see if 11023 * there is still any blockage. 11024 * 11025 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11026 * with our removing a variable on it while it is traversing the 11027 * list. 11028 */ 11029 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11030 cur_blocked != NULL; cur_blocked = next_blocked) { 11031 union ctl_io *prev_ooa; 11032 ctl_action action; 11033 11034 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11035 blocked_links); 11036 11037 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11038 ctl_ooaq, ooa_links); 11039 11040 /* 11041 * If cur_blocked happens to be the first item in the OOA 11042 * queue now, prev_ooa will be NULL, and the action 11043 * returned will just be CTL_ACTION_PASS. 11044 */ 11045 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11046 11047 switch (action) { 11048 case CTL_ACTION_BLOCK: 11049 /* Nothing to do here, still blocked */ 11050 break; 11051 case CTL_ACTION_OVERLAP: 11052 case CTL_ACTION_OVERLAP_TAG: 11053 /* 11054 * This shouldn't happen! In theory we've already 11055 * checked this command for overlap... 11056 */ 11057 break; 11058 case CTL_ACTION_PASS: 11059 case CTL_ACTION_SKIP: { 11060 const struct ctl_cmd_entry *entry; 11061 11062 /* 11063 * The skip case shouldn't happen, this transaction 11064 * should have never made it onto the blocked queue. 11065 */ 11066 /* 11067 * This I/O is no longer blocked, we can remove it 11068 * from the blocked queue. Since this is a TAILQ 11069 * (doubly linked list), we can do O(1) removals 11070 * from any place on the list. 11071 */ 11072 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11073 blocked_links); 11074 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11075 11076 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11077 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11078 /* 11079 * Need to send IO back to original side to 11080 * run 11081 */ 11082 union ctl_ha_msg msg_info; 11083 11084 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11085 msg_info.hdr.original_sc = 11086 cur_blocked->io_hdr.original_sc; 11087 msg_info.hdr.serializing_sc = cur_blocked; 11088 msg_info.hdr.msg_type = CTL_MSG_R2R; 11089 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11090 sizeof(msg_info.hdr), M_NOWAIT); 11091 break; 11092 } 11093 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11094 11095 /* 11096 * Check this I/O for LUN state changes that may 11097 * have happened while this command was blocked. 11098 * The LUN state may have been changed by a command 11099 * ahead of us in the queue, so we need to re-check 11100 * for any states that can be caused by SCSI 11101 * commands. 11102 */ 11103 if (ctl_scsiio_lun_check(lun, entry, 11104 &cur_blocked->scsiio) == 0) { 11105 cur_blocked->io_hdr.flags |= 11106 CTL_FLAG_IS_WAS_ON_RTR; 11107 ctl_enqueue_rtr(cur_blocked); 11108 } else 11109 ctl_done(cur_blocked); 11110 break; 11111 } 11112 default: 11113 /* 11114 * This probably shouldn't happen -- we shouldn't 11115 * get CTL_ACTION_ERROR, or anything else. 11116 */ 11117 break; 11118 } 11119 } 11120 11121 return (CTL_RETVAL_COMPLETE); 11122} 11123 11124/* 11125 * This routine (with one exception) checks LUN flags that can be set by 11126 * commands ahead of us in the OOA queue. These flags have to be checked 11127 * when a command initially comes in, and when we pull a command off the 11128 * blocked queue and are preparing to execute it. The reason we have to 11129 * check these flags for commands on the blocked queue is that the LUN 11130 * state may have been changed by a command ahead of us while we're on the 11131 * blocked queue. 11132 * 11133 * Ordering is somewhat important with these checks, so please pay 11134 * careful attention to the placement of any new checks. 11135 */ 11136static int 11137ctl_scsiio_lun_check(struct ctl_lun *lun, 11138 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11139{ 11140 struct ctl_softc *softc = lun->ctl_softc; 11141 int retval; 11142 uint32_t residx; 11143 11144 retval = 0; 11145 11146 mtx_assert(&lun->lun_lock, MA_OWNED); 11147 11148 /* 11149 * If this shelf is a secondary shelf controller, we may have to 11150 * reject some commands disallowed by HA mode and link state. 11151 */ 11152 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11153 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11154 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11155 ctl_set_lun_unavail(ctsio); 11156 retval = 1; 11157 goto bailout; 11158 } 11159 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11160 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11161 ctl_set_lun_transit(ctsio); 11162 retval = 1; 11163 goto bailout; 11164 } 11165 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11166 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11167 ctl_set_lun_standby(ctsio); 11168 retval = 1; 11169 goto bailout; 11170 } 11171 11172 /* The rest of checks are only done on executing side */ 11173 if (softc->ha_mode == CTL_HA_MODE_XFER) 11174 goto bailout; 11175 } 11176 11177 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11178 if (lun->be_lun && 11179 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11180 ctl_set_hw_write_protected(ctsio); 11181 retval = 1; 11182 goto bailout; 11183 } 11184 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11185 .eca_and_aen & SCP_SWP) != 0) { 11186 ctl_set_sense(ctsio, /*current_error*/ 1, 11187 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11188 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11189 retval = 1; 11190 goto bailout; 11191 } 11192 } 11193 11194 /* 11195 * Check for a reservation conflict. If this command isn't allowed 11196 * even on reserved LUNs, and if this initiator isn't the one who 11197 * reserved us, reject the command with a reservation conflict. 11198 */ 11199 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11200 if ((lun->flags & CTL_LUN_RESERVED) 11201 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11202 if (lun->res_idx != residx) { 11203 ctl_set_reservation_conflict(ctsio); 11204 retval = 1; 11205 goto bailout; 11206 } 11207 } 11208 11209 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11210 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11211 /* No reservation or command is allowed. */; 11212 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11213 (lun->res_type == SPR_TYPE_WR_EX || 11214 lun->res_type == SPR_TYPE_WR_EX_RO || 11215 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11216 /* The command is allowed for Write Exclusive resv. */; 11217 } else { 11218 /* 11219 * if we aren't registered or it's a res holder type 11220 * reservation and this isn't the res holder then set a 11221 * conflict. 11222 */ 11223 if (ctl_get_prkey(lun, residx) == 0 11224 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11225 ctl_set_reservation_conflict(ctsio); 11226 retval = 1; 11227 goto bailout; 11228 } 11229 } 11230 11231 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11232 if (lun->flags & CTL_LUN_EJECTED) 11233 ctl_set_lun_ejected(ctsio); 11234 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11235 if (lun->flags & CTL_LUN_REMOVABLE) 11236 ctl_set_lun_no_media(ctsio); 11237 else 11238 ctl_set_lun_int_reqd(ctsio); 11239 } else if (lun->flags & CTL_LUN_STOPPED) 11240 ctl_set_lun_stopped(ctsio); 11241 else 11242 goto bailout; 11243 retval = 1; 11244 goto bailout; 11245 } 11246 11247bailout: 11248 return (retval); 11249} 11250 11251static void 11252ctl_failover_io(union ctl_io *io, int have_lock) 11253{ 11254 ctl_set_busy(&io->scsiio); 11255 ctl_done(io); 11256} 11257 11258static void 11259ctl_failover_lun(union ctl_io *rio) 11260{ 11261 struct ctl_softc *softc = control_softc; 11262 struct ctl_lun *lun; 11263 struct ctl_io_hdr *io, *next_io; 11264 uint32_t targ_lun; 11265 11266 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11267 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11268 11269 /* Find and lock the LUN. */ 11270 mtx_lock(&softc->ctl_lock); 11271 if ((targ_lun < CTL_MAX_LUNS) && 11272 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11273 mtx_lock(&lun->lun_lock); 11274 mtx_unlock(&softc->ctl_lock); 11275 if (lun->flags & CTL_LUN_DISABLED) { 11276 mtx_unlock(&lun->lun_lock); 11277 return; 11278 } 11279 } else { 11280 mtx_unlock(&softc->ctl_lock); 11281 return; 11282 } 11283 11284 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11285 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11286 /* We are master */ 11287 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11288 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11289 io->flags |= CTL_FLAG_ABORT; 11290 io->flags |= CTL_FLAG_FAILOVER; 11291 } else { /* This can be only due to DATAMOVE */ 11292 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11293 io->flags &= ~CTL_FLAG_DMA_INPROG; 11294 io->flags |= CTL_FLAG_IO_ACTIVE; 11295 io->port_status = 31340; 11296 ctl_enqueue_isc((union ctl_io *)io); 11297 } 11298 } 11299 /* We are slave */ 11300 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11301 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11302 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11303 io->flags |= CTL_FLAG_FAILOVER; 11304 } else { 11305 ctl_set_busy(&((union ctl_io *)io)-> 11306 scsiio); 11307 ctl_done((union ctl_io *)io); 11308 } 11309 } 11310 } 11311 } else { /* SERIALIZE modes */ 11312 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11313 next_io) { 11314 /* We are master */ 11315 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11316 TAILQ_REMOVE(&lun->blocked_queue, io, 11317 blocked_links); 11318 io->flags &= ~CTL_FLAG_BLOCKED; 11319 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11320 ctl_free_io((union ctl_io *)io); 11321 } 11322 } 11323 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11324 /* We are master */ 11325 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11326 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11327 ctl_free_io((union ctl_io *)io); 11328 } 11329 /* We are slave */ 11330 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11331 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11332 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11333 ctl_set_busy(&((union ctl_io *)io)-> 11334 scsiio); 11335 ctl_done((union ctl_io *)io); 11336 } 11337 } 11338 } 11339 ctl_check_blocked(lun); 11340 } 11341 mtx_unlock(&lun->lun_lock); 11342} 11343 11344static int 11345ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11346{ 11347 struct ctl_lun *lun; 11348 const struct ctl_cmd_entry *entry; 11349 uint32_t initidx, targ_lun; 11350 int retval; 11351 11352 retval = 0; 11353 11354 lun = NULL; 11355 11356 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11357 if ((targ_lun < CTL_MAX_LUNS) 11358 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11359 /* 11360 * If the LUN is invalid, pretend that it doesn't exist. 11361 * It will go away as soon as all pending I/O has been 11362 * completed. 11363 */ 11364 mtx_lock(&lun->lun_lock); 11365 if (lun->flags & CTL_LUN_DISABLED) { 11366 mtx_unlock(&lun->lun_lock); 11367 lun = NULL; 11368 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11369 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11370 } else { 11371 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11372 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11373 lun->be_lun; 11374 11375 /* 11376 * Every I/O goes into the OOA queue for a 11377 * particular LUN, and stays there until completion. 11378 */ 11379#ifdef CTL_TIME_IO 11380 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11381 lun->idle_time += getsbinuptime() - 11382 lun->last_busy; 11383 } 11384#endif 11385 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11386 ooa_links); 11387 } 11388 } else { 11389 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11390 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11391 } 11392 11393 /* Get command entry and return error if it is unsuppotyed. */ 11394 entry = ctl_validate_command(ctsio); 11395 if (entry == NULL) { 11396 if (lun) 11397 mtx_unlock(&lun->lun_lock); 11398 return (retval); 11399 } 11400 11401 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11402 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11403 11404 /* 11405 * Check to see whether we can send this command to LUNs that don't 11406 * exist. This should pretty much only be the case for inquiry 11407 * and request sense. Further checks, below, really require having 11408 * a LUN, so we can't really check the command anymore. Just put 11409 * it on the rtr queue. 11410 */ 11411 if (lun == NULL) { 11412 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11413 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11414 ctl_enqueue_rtr((union ctl_io *)ctsio); 11415 return (retval); 11416 } 11417 11418 ctl_set_unsupported_lun(ctsio); 11419 ctl_done((union ctl_io *)ctsio); 11420 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11421 return (retval); 11422 } else { 11423 /* 11424 * Make sure we support this particular command on this LUN. 11425 * e.g., we don't support writes to the control LUN. 11426 */ 11427 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11428 mtx_unlock(&lun->lun_lock); 11429 ctl_set_invalid_opcode(ctsio); 11430 ctl_done((union ctl_io *)ctsio); 11431 return (retval); 11432 } 11433 } 11434 11435 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11436 11437#ifdef CTL_WITH_CA 11438 /* 11439 * If we've got a request sense, it'll clear the contingent 11440 * allegiance condition. Otherwise, if we have a CA condition for 11441 * this initiator, clear it, because it sent down a command other 11442 * than request sense. 11443 */ 11444 if ((ctsio->cdb[0] != REQUEST_SENSE) 11445 && (ctl_is_set(lun->have_ca, initidx))) 11446 ctl_clear_mask(lun->have_ca, initidx); 11447#endif 11448 11449 /* 11450 * If the command has this flag set, it handles its own unit 11451 * attention reporting, we shouldn't do anything. Otherwise we 11452 * check for any pending unit attentions, and send them back to the 11453 * initiator. We only do this when a command initially comes in, 11454 * not when we pull it off the blocked queue. 11455 * 11456 * According to SAM-3, section 5.3.2, the order that things get 11457 * presented back to the host is basically unit attentions caused 11458 * by some sort of reset event, busy status, reservation conflicts 11459 * or task set full, and finally any other status. 11460 * 11461 * One issue here is that some of the unit attentions we report 11462 * don't fall into the "reset" category (e.g. "reported luns data 11463 * has changed"). So reporting it here, before the reservation 11464 * check, may be technically wrong. I guess the only thing to do 11465 * would be to check for and report the reset events here, and then 11466 * check for the other unit attention types after we check for a 11467 * reservation conflict. 11468 * 11469 * XXX KDM need to fix this 11470 */ 11471 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11472 ctl_ua_type ua_type; 11473 11474 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11475 SSD_TYPE_NONE); 11476 if (ua_type != CTL_UA_NONE) { 11477 mtx_unlock(&lun->lun_lock); 11478 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11479 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11480 ctsio->sense_len = SSD_FULL_SIZE; 11481 ctl_done((union ctl_io *)ctsio); 11482 return (retval); 11483 } 11484 } 11485 11486 11487 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11488 mtx_unlock(&lun->lun_lock); 11489 ctl_done((union ctl_io *)ctsio); 11490 return (retval); 11491 } 11492 11493 /* 11494 * XXX CHD this is where we want to send IO to other side if 11495 * this LUN is secondary on this SC. We will need to make a copy 11496 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11497 * the copy we send as FROM_OTHER. 11498 * We also need to stuff the address of the original IO so we can 11499 * find it easily. Something similar will need be done on the other 11500 * side so when we are done we can find the copy. 11501 */ 11502 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11503 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11504 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11505 union ctl_ha_msg msg_info; 11506 int isc_retval; 11507 11508 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11509 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11510 mtx_unlock(&lun->lun_lock); 11511 11512 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11513 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11514 msg_info.hdr.serializing_sc = NULL; 11515 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11516 msg_info.scsi.tag_num = ctsio->tag_num; 11517 msg_info.scsi.tag_type = ctsio->tag_type; 11518 msg_info.scsi.cdb_len = ctsio->cdb_len; 11519 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11520 11521 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11522 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11523 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11524 ctl_set_busy(ctsio); 11525 ctl_done((union ctl_io *)ctsio); 11526 return (retval); 11527 } 11528 return (retval); 11529 } 11530 11531 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11532 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11533 ctl_ooaq, ooa_links))) { 11534 case CTL_ACTION_BLOCK: 11535 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11536 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11537 blocked_links); 11538 mtx_unlock(&lun->lun_lock); 11539 return (retval); 11540 case CTL_ACTION_PASS: 11541 case CTL_ACTION_SKIP: 11542 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11543 mtx_unlock(&lun->lun_lock); 11544 ctl_enqueue_rtr((union ctl_io *)ctsio); 11545 break; 11546 case CTL_ACTION_OVERLAP: 11547 mtx_unlock(&lun->lun_lock); 11548 ctl_set_overlapped_cmd(ctsio); 11549 ctl_done((union ctl_io *)ctsio); 11550 break; 11551 case CTL_ACTION_OVERLAP_TAG: 11552 mtx_unlock(&lun->lun_lock); 11553 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11554 ctl_done((union ctl_io *)ctsio); 11555 break; 11556 case CTL_ACTION_ERROR: 11557 default: 11558 mtx_unlock(&lun->lun_lock); 11559 ctl_set_internal_failure(ctsio, 11560 /*sks_valid*/ 0, 11561 /*retry_count*/ 0); 11562 ctl_done((union ctl_io *)ctsio); 11563 break; 11564 } 11565 return (retval); 11566} 11567 11568const struct ctl_cmd_entry * 11569ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11570{ 11571 const struct ctl_cmd_entry *entry; 11572 int service_action; 11573 11574 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11575 if (sa) 11576 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11577 if (entry->flags & CTL_CMD_FLAG_SA5) { 11578 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11579 entry = &((const struct ctl_cmd_entry *) 11580 entry->execute)[service_action]; 11581 } 11582 return (entry); 11583} 11584 11585const struct ctl_cmd_entry * 11586ctl_validate_command(struct ctl_scsiio *ctsio) 11587{ 11588 const struct ctl_cmd_entry *entry; 11589 int i, sa; 11590 uint8_t diff; 11591 11592 entry = ctl_get_cmd_entry(ctsio, &sa); 11593 if (entry->execute == NULL) { 11594 if (sa) 11595 ctl_set_invalid_field(ctsio, 11596 /*sks_valid*/ 1, 11597 /*command*/ 1, 11598 /*field*/ 1, 11599 /*bit_valid*/ 1, 11600 /*bit*/ 4); 11601 else 11602 ctl_set_invalid_opcode(ctsio); 11603 ctl_done((union ctl_io *)ctsio); 11604 return (NULL); 11605 } 11606 KASSERT(entry->length > 0, 11607 ("Not defined length for command 0x%02x/0x%02x", 11608 ctsio->cdb[0], ctsio->cdb[1])); 11609 for (i = 1; i < entry->length; i++) { 11610 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11611 if (diff == 0) 11612 continue; 11613 ctl_set_invalid_field(ctsio, 11614 /*sks_valid*/ 1, 11615 /*command*/ 1, 11616 /*field*/ i, 11617 /*bit_valid*/ 1, 11618 /*bit*/ fls(diff) - 1); 11619 ctl_done((union ctl_io *)ctsio); 11620 return (NULL); 11621 } 11622 return (entry); 11623} 11624 11625static int 11626ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11627{ 11628 11629 switch (lun_type) { 11630 case T_DIRECT: 11631 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11632 return (0); 11633 break; 11634 case T_PROCESSOR: 11635 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11636 return (0); 11637 break; 11638 case T_CDROM: 11639 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11640 return (0); 11641 break; 11642 default: 11643 return (0); 11644 } 11645 return (1); 11646} 11647 11648static int 11649ctl_scsiio(struct ctl_scsiio *ctsio) 11650{ 11651 int retval; 11652 const struct ctl_cmd_entry *entry; 11653 11654 retval = CTL_RETVAL_COMPLETE; 11655 11656 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11657 11658 entry = ctl_get_cmd_entry(ctsio, NULL); 11659 11660 /* 11661 * If this I/O has been aborted, just send it straight to 11662 * ctl_done() without executing it. 11663 */ 11664 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11665 ctl_done((union ctl_io *)ctsio); 11666 goto bailout; 11667 } 11668 11669 /* 11670 * All the checks should have been handled by ctl_scsiio_precheck(). 11671 * We should be clear now to just execute the I/O. 11672 */ 11673 retval = entry->execute(ctsio); 11674 11675bailout: 11676 return (retval); 11677} 11678 11679/* 11680 * Since we only implement one target right now, a bus reset simply resets 11681 * our single target. 11682 */ 11683static int 11684ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11685{ 11686 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11687} 11688 11689static int 11690ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11691 ctl_ua_type ua_type) 11692{ 11693 struct ctl_port *port; 11694 struct ctl_lun *lun; 11695 int retval; 11696 11697 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11698 union ctl_ha_msg msg_info; 11699 11700 msg_info.hdr.nexus = io->io_hdr.nexus; 11701 if (ua_type==CTL_UA_TARG_RESET) 11702 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11703 else 11704 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11705 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11706 msg_info.hdr.original_sc = NULL; 11707 msg_info.hdr.serializing_sc = NULL; 11708 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11709 sizeof(msg_info.task), M_WAITOK); 11710 } 11711 retval = 0; 11712 11713 mtx_lock(&softc->ctl_lock); 11714 port = ctl_io_port(&io->io_hdr); 11715 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11716 if (port != NULL && 11717 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11718 continue; 11719 retval += ctl_do_lun_reset(lun, io, ua_type); 11720 } 11721 mtx_unlock(&softc->ctl_lock); 11722 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11723 return (retval); 11724} 11725 11726/* 11727 * The LUN should always be set. The I/O is optional, and is used to 11728 * distinguish between I/Os sent by this initiator, and by other 11729 * initiators. We set unit attention for initiators other than this one. 11730 * SAM-3 is vague on this point. It does say that a unit attention should 11731 * be established for other initiators when a LUN is reset (see section 11732 * 5.7.3), but it doesn't specifically say that the unit attention should 11733 * be established for this particular initiator when a LUN is reset. Here 11734 * is the relevant text, from SAM-3 rev 8: 11735 * 11736 * 5.7.2 When a SCSI initiator port aborts its own tasks 11737 * 11738 * When a SCSI initiator port causes its own task(s) to be aborted, no 11739 * notification that the task(s) have been aborted shall be returned to 11740 * the SCSI initiator port other than the completion response for the 11741 * command or task management function action that caused the task(s) to 11742 * be aborted and notification(s) associated with related effects of the 11743 * action (e.g., a reset unit attention condition). 11744 * 11745 * XXX KDM for now, we're setting unit attention for all initiators. 11746 */ 11747static int 11748ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11749{ 11750 union ctl_io *xio; 11751#if 0 11752 uint32_t initidx; 11753#endif 11754#ifdef CTL_WITH_CA 11755 int i; 11756#endif 11757 11758 mtx_lock(&lun->lun_lock); 11759 /* 11760 * Run through the OOA queue and abort each I/O. 11761 */ 11762 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11763 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11764 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11765 } 11766 11767 /* 11768 * This version sets unit attention for every 11769 */ 11770#if 0 11771 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11772 ctl_est_ua_all(lun, initidx, ua_type); 11773#else 11774 ctl_est_ua_all(lun, -1, ua_type); 11775#endif 11776 11777 /* 11778 * A reset (any kind, really) clears reservations established with 11779 * RESERVE/RELEASE. It does not clear reservations established 11780 * with PERSISTENT RESERVE OUT, but we don't support that at the 11781 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11782 * reservations made with the RESERVE/RELEASE commands, because 11783 * those commands are obsolete in SPC-3. 11784 */ 11785 lun->flags &= ~CTL_LUN_RESERVED; 11786 11787#ifdef CTL_WITH_CA 11788 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11789 ctl_clear_mask(lun->have_ca, i); 11790#endif 11791 mtx_unlock(&lun->lun_lock); 11792 11793 return (0); 11794} 11795 11796static int 11797ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11798{ 11799 struct ctl_lun *lun; 11800 uint32_t targ_lun; 11801 int retval; 11802 11803 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11804 mtx_lock(&softc->ctl_lock); 11805 if ((targ_lun >= CTL_MAX_LUNS) || 11806 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11807 mtx_unlock(&softc->ctl_lock); 11808 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11809 return (1); 11810 } 11811 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11812 mtx_unlock(&softc->ctl_lock); 11813 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11814 11815 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11816 union ctl_ha_msg msg_info; 11817 11818 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11819 msg_info.hdr.nexus = io->io_hdr.nexus; 11820 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11821 msg_info.hdr.original_sc = NULL; 11822 msg_info.hdr.serializing_sc = NULL; 11823 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11824 sizeof(msg_info.task), M_WAITOK); 11825 } 11826 return (retval); 11827} 11828 11829static void 11830ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11831 int other_sc) 11832{ 11833 union ctl_io *xio; 11834 11835 mtx_assert(&lun->lun_lock, MA_OWNED); 11836 11837 /* 11838 * Run through the OOA queue and attempt to find the given I/O. 11839 * The target port, initiator ID, tag type and tag number have to 11840 * match the values that we got from the initiator. If we have an 11841 * untagged command to abort, simply abort the first untagged command 11842 * we come to. We only allow one untagged command at a time of course. 11843 */ 11844 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11845 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11846 11847 if ((targ_port == UINT32_MAX || 11848 targ_port == xio->io_hdr.nexus.targ_port) && 11849 (init_id == UINT32_MAX || 11850 init_id == xio->io_hdr.nexus.initid)) { 11851 if (targ_port != xio->io_hdr.nexus.targ_port || 11852 init_id != xio->io_hdr.nexus.initid) 11853 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11854 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11855 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11856 union ctl_ha_msg msg_info; 11857 11858 msg_info.hdr.nexus = xio->io_hdr.nexus; 11859 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11860 msg_info.task.tag_num = xio->scsiio.tag_num; 11861 msg_info.task.tag_type = xio->scsiio.tag_type; 11862 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11863 msg_info.hdr.original_sc = NULL; 11864 msg_info.hdr.serializing_sc = NULL; 11865 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11866 sizeof(msg_info.task), M_NOWAIT); 11867 } 11868 } 11869 } 11870} 11871 11872static int 11873ctl_abort_task_set(union ctl_io *io) 11874{ 11875 struct ctl_softc *softc = control_softc; 11876 struct ctl_lun *lun; 11877 uint32_t targ_lun; 11878 11879 /* 11880 * Look up the LUN. 11881 */ 11882 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11883 mtx_lock(&softc->ctl_lock); 11884 if ((targ_lun >= CTL_MAX_LUNS) || 11885 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11886 mtx_unlock(&softc->ctl_lock); 11887 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11888 return (1); 11889 } 11890 11891 mtx_lock(&lun->lun_lock); 11892 mtx_unlock(&softc->ctl_lock); 11893 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11894 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11895 io->io_hdr.nexus.initid, 11896 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11897 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11898 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11899 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11900 } 11901 mtx_unlock(&lun->lun_lock); 11902 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11903 return (0); 11904} 11905 11906static int 11907ctl_i_t_nexus_reset(union ctl_io *io) 11908{ 11909 struct ctl_softc *softc = control_softc; 11910 struct ctl_lun *lun; 11911 uint32_t initidx; 11912 11913 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11914 union ctl_ha_msg msg_info; 11915 11916 msg_info.hdr.nexus = io->io_hdr.nexus; 11917 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 11918 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11919 msg_info.hdr.original_sc = NULL; 11920 msg_info.hdr.serializing_sc = NULL; 11921 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11922 sizeof(msg_info.task), M_WAITOK); 11923 } 11924 11925 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11926 mtx_lock(&softc->ctl_lock); 11927 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11928 mtx_lock(&lun->lun_lock); 11929 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11930 io->io_hdr.nexus.initid, 1); 11931#ifdef CTL_WITH_CA 11932 ctl_clear_mask(lun->have_ca, initidx); 11933#endif 11934 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 11935 lun->flags &= ~CTL_LUN_RESERVED; 11936 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 11937 mtx_unlock(&lun->lun_lock); 11938 } 11939 mtx_unlock(&softc->ctl_lock); 11940 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11941 return (0); 11942} 11943 11944static int 11945ctl_abort_task(union ctl_io *io) 11946{ 11947 union ctl_io *xio; 11948 struct ctl_lun *lun; 11949 struct ctl_softc *softc; 11950#if 0 11951 struct sbuf sb; 11952 char printbuf[128]; 11953#endif 11954 int found; 11955 uint32_t targ_lun; 11956 11957 softc = control_softc; 11958 found = 0; 11959 11960 /* 11961 * Look up the LUN. 11962 */ 11963 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11964 mtx_lock(&softc->ctl_lock); 11965 if ((targ_lun >= CTL_MAX_LUNS) || 11966 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11967 mtx_unlock(&softc->ctl_lock); 11968 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11969 return (1); 11970 } 11971 11972#if 0 11973 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11974 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11975#endif 11976 11977 mtx_lock(&lun->lun_lock); 11978 mtx_unlock(&softc->ctl_lock); 11979 /* 11980 * Run through the OOA queue and attempt to find the given I/O. 11981 * The target port, initiator ID, tag type and tag number have to 11982 * match the values that we got from the initiator. If we have an 11983 * untagged command to abort, simply abort the first untagged command 11984 * we come to. We only allow one untagged command at a time of course. 11985 */ 11986 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11987 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11988#if 0 11989 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11990 11991 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11992 lun->lun, xio->scsiio.tag_num, 11993 xio->scsiio.tag_type, 11994 (xio->io_hdr.blocked_links.tqe_prev 11995 == NULL) ? "" : " BLOCKED", 11996 (xio->io_hdr.flags & 11997 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11998 (xio->io_hdr.flags & 11999 CTL_FLAG_ABORT) ? " ABORT" : "", 12000 (xio->io_hdr.flags & 12001 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12002 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12003 sbuf_finish(&sb); 12004 printf("%s\n", sbuf_data(&sb)); 12005#endif 12006 12007 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12008 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12009 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12010 continue; 12011 12012 /* 12013 * If the abort says that the task is untagged, the 12014 * task in the queue must be untagged. Otherwise, 12015 * we just check to see whether the tag numbers 12016 * match. This is because the QLogic firmware 12017 * doesn't pass back the tag type in an abort 12018 * request. 12019 */ 12020#if 0 12021 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12022 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12023 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12024#endif 12025 /* 12026 * XXX KDM we've got problems with FC, because it 12027 * doesn't send down a tag type with aborts. So we 12028 * can only really go by the tag number... 12029 * This may cause problems with parallel SCSI. 12030 * Need to figure that out!! 12031 */ 12032 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12033 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12034 found = 1; 12035 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12036 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12037 union ctl_ha_msg msg_info; 12038 12039 msg_info.hdr.nexus = io->io_hdr.nexus; 12040 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12041 msg_info.task.tag_num = io->taskio.tag_num; 12042 msg_info.task.tag_type = io->taskio.tag_type; 12043 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12044 msg_info.hdr.original_sc = NULL; 12045 msg_info.hdr.serializing_sc = NULL; 12046#if 0 12047 printf("Sent Abort to other side\n"); 12048#endif 12049 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12050 sizeof(msg_info.task), M_NOWAIT); 12051 } 12052#if 0 12053 printf("ctl_abort_task: found I/O to abort\n"); 12054#endif 12055 } 12056 } 12057 mtx_unlock(&lun->lun_lock); 12058 12059 if (found == 0) { 12060 /* 12061 * This isn't really an error. It's entirely possible for 12062 * the abort and command completion to cross on the wire. 12063 * This is more of an informative/diagnostic error. 12064 */ 12065#if 0 12066 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12067 "%u:%u:%u tag %d type %d\n", 12068 io->io_hdr.nexus.initid, 12069 io->io_hdr.nexus.targ_port, 12070 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12071 io->taskio.tag_type); 12072#endif 12073 } 12074 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12075 return (0); 12076} 12077 12078static int 12079ctl_query_task(union ctl_io *io, int task_set) 12080{ 12081 union ctl_io *xio; 12082 struct ctl_lun *lun; 12083 struct ctl_softc *softc; 12084 int found = 0; 12085 uint32_t targ_lun; 12086 12087 softc = control_softc; 12088 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12089 mtx_lock(&softc->ctl_lock); 12090 if ((targ_lun >= CTL_MAX_LUNS) || 12091 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12092 mtx_unlock(&softc->ctl_lock); 12093 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12094 return (1); 12095 } 12096 mtx_lock(&lun->lun_lock); 12097 mtx_unlock(&softc->ctl_lock); 12098 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12099 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12100 12101 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12102 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12103 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12104 continue; 12105 12106 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12107 found = 1; 12108 break; 12109 } 12110 } 12111 mtx_unlock(&lun->lun_lock); 12112 if (found) 12113 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12114 else 12115 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12116 return (0); 12117} 12118 12119static int 12120ctl_query_async_event(union ctl_io *io) 12121{ 12122 struct ctl_lun *lun; 12123 struct ctl_softc *softc; 12124 ctl_ua_type ua; 12125 uint32_t targ_lun, initidx; 12126 12127 softc = control_softc; 12128 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12129 mtx_lock(&softc->ctl_lock); 12130 if ((targ_lun >= CTL_MAX_LUNS) || 12131 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12132 mtx_unlock(&softc->ctl_lock); 12133 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12134 return (1); 12135 } 12136 mtx_lock(&lun->lun_lock); 12137 mtx_unlock(&softc->ctl_lock); 12138 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12139 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12140 mtx_unlock(&lun->lun_lock); 12141 if (ua != CTL_UA_NONE) 12142 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12143 else 12144 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12145 return (0); 12146} 12147 12148static void 12149ctl_run_task(union ctl_io *io) 12150{ 12151 struct ctl_softc *softc = control_softc; 12152 int retval = 1; 12153 12154 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12155 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12156 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12157 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12158 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12159 switch (io->taskio.task_action) { 12160 case CTL_TASK_ABORT_TASK: 12161 retval = ctl_abort_task(io); 12162 break; 12163 case CTL_TASK_ABORT_TASK_SET: 12164 case CTL_TASK_CLEAR_TASK_SET: 12165 retval = ctl_abort_task_set(io); 12166 break; 12167 case CTL_TASK_CLEAR_ACA: 12168 break; 12169 case CTL_TASK_I_T_NEXUS_RESET: 12170 retval = ctl_i_t_nexus_reset(io); 12171 break; 12172 case CTL_TASK_LUN_RESET: 12173 retval = ctl_lun_reset(softc, io); 12174 break; 12175 case CTL_TASK_TARGET_RESET: 12176 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12177 break; 12178 case CTL_TASK_BUS_RESET: 12179 retval = ctl_bus_reset(softc, io); 12180 break; 12181 case CTL_TASK_PORT_LOGIN: 12182 break; 12183 case CTL_TASK_PORT_LOGOUT: 12184 break; 12185 case CTL_TASK_QUERY_TASK: 12186 retval = ctl_query_task(io, 0); 12187 break; 12188 case CTL_TASK_QUERY_TASK_SET: 12189 retval = ctl_query_task(io, 1); 12190 break; 12191 case CTL_TASK_QUERY_ASYNC_EVENT: 12192 retval = ctl_query_async_event(io); 12193 break; 12194 default: 12195 printf("%s: got unknown task management event %d\n", 12196 __func__, io->taskio.task_action); 12197 break; 12198 } 12199 if (retval == 0) 12200 io->io_hdr.status = CTL_SUCCESS; 12201 else 12202 io->io_hdr.status = CTL_ERROR; 12203 ctl_done(io); 12204} 12205 12206/* 12207 * For HA operation. Handle commands that come in from the other 12208 * controller. 12209 */ 12210static void 12211ctl_handle_isc(union ctl_io *io) 12212{ 12213 int free_io; 12214 struct ctl_lun *lun; 12215 struct ctl_softc *softc = control_softc; 12216 uint32_t targ_lun; 12217 12218 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12219 lun = softc->ctl_luns[targ_lun]; 12220 12221 switch (io->io_hdr.msg_type) { 12222 case CTL_MSG_SERIALIZE: 12223 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12224 break; 12225 case CTL_MSG_R2R: { 12226 const struct ctl_cmd_entry *entry; 12227 12228 /* 12229 * This is only used in SER_ONLY mode. 12230 */ 12231 free_io = 0; 12232 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12233 mtx_lock(&lun->lun_lock); 12234 if (ctl_scsiio_lun_check(lun, 12235 entry, (struct ctl_scsiio *)io) != 0) { 12236 mtx_unlock(&lun->lun_lock); 12237 ctl_done(io); 12238 break; 12239 } 12240 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12241 mtx_unlock(&lun->lun_lock); 12242 ctl_enqueue_rtr(io); 12243 break; 12244 } 12245 case CTL_MSG_FINISH_IO: 12246 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12247 free_io = 0; 12248 ctl_done(io); 12249 } else { 12250 free_io = 1; 12251 mtx_lock(&lun->lun_lock); 12252 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12253 ooa_links); 12254 ctl_check_blocked(lun); 12255 mtx_unlock(&lun->lun_lock); 12256 } 12257 break; 12258 case CTL_MSG_PERS_ACTION: 12259 ctl_hndl_per_res_out_on_other_sc( 12260 (union ctl_ha_msg *)&io->presio.pr_msg); 12261 free_io = 1; 12262 break; 12263 case CTL_MSG_BAD_JUJU: 12264 free_io = 0; 12265 ctl_done(io); 12266 break; 12267 case CTL_MSG_DATAMOVE: 12268 /* Only used in XFER mode */ 12269 free_io = 0; 12270 ctl_datamove_remote(io); 12271 break; 12272 case CTL_MSG_DATAMOVE_DONE: 12273 /* Only used in XFER mode */ 12274 free_io = 0; 12275 io->scsiio.be_move_done(io); 12276 break; 12277 case CTL_MSG_FAILOVER: 12278 ctl_failover_lun(io); 12279 free_io = 1; 12280 break; 12281 default: 12282 free_io = 1; 12283 printf("%s: Invalid message type %d\n", 12284 __func__, io->io_hdr.msg_type); 12285 break; 12286 } 12287 if (free_io) 12288 ctl_free_io(io); 12289 12290} 12291 12292 12293/* 12294 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12295 * there is no match. 12296 */ 12297static ctl_lun_error_pattern 12298ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12299{ 12300 const struct ctl_cmd_entry *entry; 12301 ctl_lun_error_pattern filtered_pattern, pattern; 12302 12303 pattern = desc->error_pattern; 12304 12305 /* 12306 * XXX KDM we need more data passed into this function to match a 12307 * custom pattern, and we actually need to implement custom pattern 12308 * matching. 12309 */ 12310 if (pattern & CTL_LUN_PAT_CMD) 12311 return (CTL_LUN_PAT_CMD); 12312 12313 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12314 return (CTL_LUN_PAT_ANY); 12315 12316 entry = ctl_get_cmd_entry(ctsio, NULL); 12317 12318 filtered_pattern = entry->pattern & pattern; 12319 12320 /* 12321 * If the user requested specific flags in the pattern (e.g. 12322 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12323 * flags. 12324 * 12325 * If the user did not specify any flags, it doesn't matter whether 12326 * or not the command supports the flags. 12327 */ 12328 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12329 (pattern & ~CTL_LUN_PAT_MASK)) 12330 return (CTL_LUN_PAT_NONE); 12331 12332 /* 12333 * If the user asked for a range check, see if the requested LBA 12334 * range overlaps with this command's LBA range. 12335 */ 12336 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12337 uint64_t lba1; 12338 uint64_t len1; 12339 ctl_action action; 12340 int retval; 12341 12342 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12343 if (retval != 0) 12344 return (CTL_LUN_PAT_NONE); 12345 12346 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12347 desc->lba_range.len, FALSE); 12348 /* 12349 * A "pass" means that the LBA ranges don't overlap, so 12350 * this doesn't match the user's range criteria. 12351 */ 12352 if (action == CTL_ACTION_PASS) 12353 return (CTL_LUN_PAT_NONE); 12354 } 12355 12356 return (filtered_pattern); 12357} 12358 12359static void 12360ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12361{ 12362 struct ctl_error_desc *desc, *desc2; 12363 12364 mtx_assert(&lun->lun_lock, MA_OWNED); 12365 12366 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12367 ctl_lun_error_pattern pattern; 12368 /* 12369 * Check to see whether this particular command matches 12370 * the pattern in the descriptor. 12371 */ 12372 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12373 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12374 continue; 12375 12376 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12377 case CTL_LUN_INJ_ABORTED: 12378 ctl_set_aborted(&io->scsiio); 12379 break; 12380 case CTL_LUN_INJ_MEDIUM_ERR: 12381 ctl_set_medium_error(&io->scsiio, 12382 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12383 CTL_FLAG_DATA_OUT); 12384 break; 12385 case CTL_LUN_INJ_UA: 12386 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12387 * OCCURRED */ 12388 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12389 break; 12390 case CTL_LUN_INJ_CUSTOM: 12391 /* 12392 * We're assuming the user knows what he is doing. 12393 * Just copy the sense information without doing 12394 * checks. 12395 */ 12396 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12397 MIN(sizeof(desc->custom_sense), 12398 sizeof(io->scsiio.sense_data))); 12399 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12400 io->scsiio.sense_len = SSD_FULL_SIZE; 12401 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12402 break; 12403 case CTL_LUN_INJ_NONE: 12404 default: 12405 /* 12406 * If this is an error injection type we don't know 12407 * about, clear the continuous flag (if it is set) 12408 * so it will get deleted below. 12409 */ 12410 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12411 break; 12412 } 12413 /* 12414 * By default, each error injection action is a one-shot 12415 */ 12416 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12417 continue; 12418 12419 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12420 12421 free(desc, M_CTL); 12422 } 12423} 12424 12425#ifdef CTL_IO_DELAY 12426static void 12427ctl_datamove_timer_wakeup(void *arg) 12428{ 12429 union ctl_io *io; 12430 12431 io = (union ctl_io *)arg; 12432 12433 ctl_datamove(io); 12434} 12435#endif /* CTL_IO_DELAY */ 12436 12437void 12438ctl_datamove(union ctl_io *io) 12439{ 12440 struct ctl_lun *lun; 12441 void (*fe_datamove)(union ctl_io *io); 12442 12443 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12444 12445 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12446 12447 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12448#ifdef CTL_TIME_IO 12449 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12450 char str[256]; 12451 char path_str[64]; 12452 struct sbuf sb; 12453 12454 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12455 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12456 12457 sbuf_cat(&sb, path_str); 12458 switch (io->io_hdr.io_type) { 12459 case CTL_IO_SCSI: 12460 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12461 sbuf_printf(&sb, "\n"); 12462 sbuf_cat(&sb, path_str); 12463 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12464 io->scsiio.tag_num, io->scsiio.tag_type); 12465 break; 12466 case CTL_IO_TASK: 12467 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12468 "Tag Type: %d\n", io->taskio.task_action, 12469 io->taskio.tag_num, io->taskio.tag_type); 12470 break; 12471 default: 12472 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12473 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12474 break; 12475 } 12476 sbuf_cat(&sb, path_str); 12477 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12478 (intmax_t)time_uptime - io->io_hdr.start_time); 12479 sbuf_finish(&sb); 12480 printf("%s", sbuf_data(&sb)); 12481 } 12482#endif /* CTL_TIME_IO */ 12483 12484#ifdef CTL_IO_DELAY 12485 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12486 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12487 } else { 12488 if ((lun != NULL) 12489 && (lun->delay_info.datamove_delay > 0)) { 12490 12491 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12492 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12493 callout_reset(&io->io_hdr.delay_callout, 12494 lun->delay_info.datamove_delay * hz, 12495 ctl_datamove_timer_wakeup, io); 12496 if (lun->delay_info.datamove_type == 12497 CTL_DELAY_TYPE_ONESHOT) 12498 lun->delay_info.datamove_delay = 0; 12499 return; 12500 } 12501 } 12502#endif 12503 12504 /* 12505 * This command has been aborted. Set the port status, so we fail 12506 * the data move. 12507 */ 12508 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12509 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12510 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12511 io->io_hdr.nexus.targ_port, 12512 io->io_hdr.nexus.targ_lun); 12513 io->io_hdr.port_status = 31337; 12514 /* 12515 * Note that the backend, in this case, will get the 12516 * callback in its context. In other cases it may get 12517 * called in the frontend's interrupt thread context. 12518 */ 12519 io->scsiio.be_move_done(io); 12520 return; 12521 } 12522 12523 /* Don't confuse frontend with zero length data move. */ 12524 if (io->scsiio.kern_data_len == 0) { 12525 io->scsiio.be_move_done(io); 12526 return; 12527 } 12528 12529 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12530 fe_datamove(io); 12531} 12532 12533static void 12534ctl_send_datamove_done(union ctl_io *io, int have_lock) 12535{ 12536 union ctl_ha_msg msg; 12537#ifdef CTL_TIME_IO 12538 struct bintime cur_bt; 12539#endif 12540 12541 memset(&msg, 0, sizeof(msg)); 12542 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12543 msg.hdr.original_sc = io; 12544 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12545 msg.hdr.nexus = io->io_hdr.nexus; 12546 msg.hdr.status = io->io_hdr.status; 12547 msg.scsi.tag_num = io->scsiio.tag_num; 12548 msg.scsi.tag_type = io->scsiio.tag_type; 12549 msg.scsi.scsi_status = io->scsiio.scsi_status; 12550 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12551 io->scsiio.sense_len); 12552 msg.scsi.sense_len = io->scsiio.sense_len; 12553 msg.scsi.sense_residual = io->scsiio.sense_residual; 12554 msg.scsi.fetd_status = io->io_hdr.port_status; 12555 msg.scsi.residual = io->scsiio.residual; 12556 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12557 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12558 ctl_failover_io(io, /*have_lock*/ have_lock); 12559 return; 12560 } 12561 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12562 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12563 msg.scsi.sense_len, M_WAITOK); 12564 12565#ifdef CTL_TIME_IO 12566 getbinuptime(&cur_bt); 12567 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12568 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12569#endif 12570 io->io_hdr.num_dmas++; 12571} 12572 12573/* 12574 * The DMA to the remote side is done, now we need to tell the other side 12575 * we're done so it can continue with its data movement. 12576 */ 12577static void 12578ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12579{ 12580 union ctl_io *io; 12581 int i; 12582 12583 io = rq->context; 12584 12585 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12586 printf("%s: ISC DMA write failed with error %d", __func__, 12587 rq->ret); 12588 ctl_set_internal_failure(&io->scsiio, 12589 /*sks_valid*/ 1, 12590 /*retry_count*/ rq->ret); 12591 } 12592 12593 ctl_dt_req_free(rq); 12594 12595 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12596 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12597 free(io->io_hdr.remote_sglist, M_CTL); 12598 io->io_hdr.remote_sglist = NULL; 12599 io->io_hdr.local_sglist = NULL; 12600 12601 /* 12602 * The data is in local and remote memory, so now we need to send 12603 * status (good or back) back to the other side. 12604 */ 12605 ctl_send_datamove_done(io, /*have_lock*/ 0); 12606} 12607 12608/* 12609 * We've moved the data from the host/controller into local memory. Now we 12610 * need to push it over to the remote controller's memory. 12611 */ 12612static int 12613ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12614{ 12615 int retval; 12616 12617 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12618 ctl_datamove_remote_write_cb); 12619 return (retval); 12620} 12621 12622static void 12623ctl_datamove_remote_write(union ctl_io *io) 12624{ 12625 int retval; 12626 void (*fe_datamove)(union ctl_io *io); 12627 12628 /* 12629 * - Get the data from the host/HBA into local memory. 12630 * - DMA memory from the local controller to the remote controller. 12631 * - Send status back to the remote controller. 12632 */ 12633 12634 retval = ctl_datamove_remote_sgl_setup(io); 12635 if (retval != 0) 12636 return; 12637 12638 /* Switch the pointer over so the FETD knows what to do */ 12639 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12640 12641 /* 12642 * Use a custom move done callback, since we need to send completion 12643 * back to the other controller, not to the backend on this side. 12644 */ 12645 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12646 12647 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12648 fe_datamove(io); 12649} 12650 12651static int 12652ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12653{ 12654#if 0 12655 char str[256]; 12656 char path_str[64]; 12657 struct sbuf sb; 12658#endif 12659 int i; 12660 12661 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12662 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12663 free(io->io_hdr.remote_sglist, M_CTL); 12664 io->io_hdr.remote_sglist = NULL; 12665 io->io_hdr.local_sglist = NULL; 12666 12667#if 0 12668 scsi_path_string(io, path_str, sizeof(path_str)); 12669 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12670 sbuf_cat(&sb, path_str); 12671 scsi_command_string(&io->scsiio, NULL, &sb); 12672 sbuf_printf(&sb, "\n"); 12673 sbuf_cat(&sb, path_str); 12674 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12675 io->scsiio.tag_num, io->scsiio.tag_type); 12676 sbuf_cat(&sb, path_str); 12677 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12678 io->io_hdr.flags, io->io_hdr.status); 12679 sbuf_finish(&sb); 12680 printk("%s", sbuf_data(&sb)); 12681#endif 12682 12683 12684 /* 12685 * The read is done, now we need to send status (good or bad) back 12686 * to the other side. 12687 */ 12688 ctl_send_datamove_done(io, /*have_lock*/ 0); 12689 12690 return (0); 12691} 12692 12693static void 12694ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12695{ 12696 union ctl_io *io; 12697 void (*fe_datamove)(union ctl_io *io); 12698 12699 io = rq->context; 12700 12701 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12702 printf("%s: ISC DMA read failed with error %d\n", __func__, 12703 rq->ret); 12704 ctl_set_internal_failure(&io->scsiio, 12705 /*sks_valid*/ 1, 12706 /*retry_count*/ rq->ret); 12707 } 12708 12709 ctl_dt_req_free(rq); 12710 12711 /* Switch the pointer over so the FETD knows what to do */ 12712 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12713 12714 /* 12715 * Use a custom move done callback, since we need to send completion 12716 * back to the other controller, not to the backend on this side. 12717 */ 12718 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12719 12720 /* XXX KDM add checks like the ones in ctl_datamove? */ 12721 12722 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12723 fe_datamove(io); 12724} 12725 12726static int 12727ctl_datamove_remote_sgl_setup(union ctl_io *io) 12728{ 12729 struct ctl_sg_entry *local_sglist; 12730 uint32_t len_to_go; 12731 int retval; 12732 int i; 12733 12734 retval = 0; 12735 local_sglist = io->io_hdr.local_sglist; 12736 len_to_go = io->scsiio.kern_data_len; 12737 12738 /* 12739 * The difficult thing here is that the size of the various 12740 * S/G segments may be different than the size from the 12741 * remote controller. That'll make it harder when DMAing 12742 * the data back to the other side. 12743 */ 12744 for (i = 0; len_to_go > 0; i++) { 12745 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12746 local_sglist[i].addr = 12747 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12748 12749 len_to_go -= local_sglist[i].len; 12750 } 12751 /* 12752 * Reset the number of S/G entries accordingly. The original 12753 * number of S/G entries is available in rem_sg_entries. 12754 */ 12755 io->scsiio.kern_sg_entries = i; 12756 12757#if 0 12758 printf("%s: kern_sg_entries = %d\n", __func__, 12759 io->scsiio.kern_sg_entries); 12760 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12761 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12762 local_sglist[i].addr, local_sglist[i].len); 12763#endif 12764 12765 return (retval); 12766} 12767 12768static int 12769ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12770 ctl_ha_dt_cb callback) 12771{ 12772 struct ctl_ha_dt_req *rq; 12773 struct ctl_sg_entry *remote_sglist, *local_sglist; 12774 uint32_t local_used, remote_used, total_used; 12775 int i, j, isc_ret; 12776 12777 rq = ctl_dt_req_alloc(); 12778 12779 /* 12780 * If we failed to allocate the request, and if the DMA didn't fail 12781 * anyway, set busy status. This is just a resource allocation 12782 * failure. 12783 */ 12784 if ((rq == NULL) 12785 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12786 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12787 ctl_set_busy(&io->scsiio); 12788 12789 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12790 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12791 12792 if (rq != NULL) 12793 ctl_dt_req_free(rq); 12794 12795 /* 12796 * The data move failed. We need to return status back 12797 * to the other controller. No point in trying to DMA 12798 * data to the remote controller. 12799 */ 12800 12801 ctl_send_datamove_done(io, /*have_lock*/ 0); 12802 12803 return (1); 12804 } 12805 12806 local_sglist = io->io_hdr.local_sglist; 12807 remote_sglist = io->io_hdr.remote_sglist; 12808 local_used = 0; 12809 remote_used = 0; 12810 total_used = 0; 12811 12812 /* 12813 * Pull/push the data over the wire from/to the other controller. 12814 * This takes into account the possibility that the local and 12815 * remote sglists may not be identical in terms of the size of 12816 * the elements and the number of elements. 12817 * 12818 * One fundamental assumption here is that the length allocated for 12819 * both the local and remote sglists is identical. Otherwise, we've 12820 * essentially got a coding error of some sort. 12821 */ 12822 isc_ret = CTL_HA_STATUS_SUCCESS; 12823 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12824 uint32_t cur_len; 12825 uint8_t *tmp_ptr; 12826 12827 rq->command = command; 12828 rq->context = io; 12829 12830 /* 12831 * Both pointers should be aligned. But it is possible 12832 * that the allocation length is not. They should both 12833 * also have enough slack left over at the end, though, 12834 * to round up to the next 8 byte boundary. 12835 */ 12836 cur_len = MIN(local_sglist[i].len - local_used, 12837 remote_sglist[j].len - remote_used); 12838 rq->size = cur_len; 12839 12840 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12841 tmp_ptr += local_used; 12842 12843#if 0 12844 /* Use physical addresses when talking to ISC hardware */ 12845 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12846 /* XXX KDM use busdma */ 12847 rq->local = vtophys(tmp_ptr); 12848 } else 12849 rq->local = tmp_ptr; 12850#else 12851 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12852 ("HA does not support BUS_ADDR")); 12853 rq->local = tmp_ptr; 12854#endif 12855 12856 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12857 tmp_ptr += remote_used; 12858 rq->remote = tmp_ptr; 12859 12860 rq->callback = NULL; 12861 12862 local_used += cur_len; 12863 if (local_used >= local_sglist[i].len) { 12864 i++; 12865 local_used = 0; 12866 } 12867 12868 remote_used += cur_len; 12869 if (remote_used >= remote_sglist[j].len) { 12870 j++; 12871 remote_used = 0; 12872 } 12873 total_used += cur_len; 12874 12875 if (total_used >= io->scsiio.kern_data_len) 12876 rq->callback = callback; 12877 12878#if 0 12879 printf("%s: %s: local %p remote %p size %d\n", __func__, 12880 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12881 rq->local, rq->remote, rq->size); 12882#endif 12883 12884 isc_ret = ctl_dt_single(rq); 12885 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12886 break; 12887 } 12888 if (isc_ret != CTL_HA_STATUS_WAIT) { 12889 rq->ret = isc_ret; 12890 callback(rq); 12891 } 12892 12893 return (0); 12894} 12895 12896static void 12897ctl_datamove_remote_read(union ctl_io *io) 12898{ 12899 int retval; 12900 int i; 12901 12902 /* 12903 * This will send an error to the other controller in the case of a 12904 * failure. 12905 */ 12906 retval = ctl_datamove_remote_sgl_setup(io); 12907 if (retval != 0) 12908 return; 12909 12910 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12911 ctl_datamove_remote_read_cb); 12912 if (retval != 0) { 12913 /* 12914 * Make sure we free memory if there was an error.. The 12915 * ctl_datamove_remote_xfer() function will send the 12916 * datamove done message, or call the callback with an 12917 * error if there is a problem. 12918 */ 12919 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12920 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12921 free(io->io_hdr.remote_sglist, M_CTL); 12922 io->io_hdr.remote_sglist = NULL; 12923 io->io_hdr.local_sglist = NULL; 12924 } 12925} 12926 12927/* 12928 * Process a datamove request from the other controller. This is used for 12929 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12930 * first. Once that is complete, the data gets DMAed into the remote 12931 * controller's memory. For reads, we DMA from the remote controller's 12932 * memory into our memory first, and then move it out to the FETD. 12933 */ 12934static void 12935ctl_datamove_remote(union ctl_io *io) 12936{ 12937 12938 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12939 12940 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12941 ctl_failover_io(io, /*have_lock*/ 0); 12942 return; 12943 } 12944 12945 /* 12946 * Note that we look for an aborted I/O here, but don't do some of 12947 * the other checks that ctl_datamove() normally does. 12948 * We don't need to run the datamove delay code, since that should 12949 * have been done if need be on the other controller. 12950 */ 12951 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12952 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 12953 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12954 io->io_hdr.nexus.targ_port, 12955 io->io_hdr.nexus.targ_lun); 12956 io->io_hdr.port_status = 31338; 12957 ctl_send_datamove_done(io, /*have_lock*/ 0); 12958 return; 12959 } 12960 12961 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 12962 ctl_datamove_remote_write(io); 12963 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 12964 ctl_datamove_remote_read(io); 12965 else { 12966 io->io_hdr.port_status = 31339; 12967 ctl_send_datamove_done(io, /*have_lock*/ 0); 12968 } 12969} 12970 12971static void 12972ctl_process_done(union ctl_io *io) 12973{ 12974 struct ctl_lun *lun; 12975 struct ctl_softc *softc = control_softc; 12976 void (*fe_done)(union ctl_io *io); 12977 union ctl_ha_msg msg; 12978 uint32_t targ_port = io->io_hdr.nexus.targ_port; 12979 12980 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12981 fe_done = softc->ctl_ports[targ_port]->fe_done; 12982 12983#ifdef CTL_TIME_IO 12984 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12985 char str[256]; 12986 char path_str[64]; 12987 struct sbuf sb; 12988 12989 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12990 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12991 12992 sbuf_cat(&sb, path_str); 12993 switch (io->io_hdr.io_type) { 12994 case CTL_IO_SCSI: 12995 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12996 sbuf_printf(&sb, "\n"); 12997 sbuf_cat(&sb, path_str); 12998 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12999 io->scsiio.tag_num, io->scsiio.tag_type); 13000 break; 13001 case CTL_IO_TASK: 13002 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13003 "Tag Type: %d\n", io->taskio.task_action, 13004 io->taskio.tag_num, io->taskio.tag_type); 13005 break; 13006 default: 13007 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13008 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13009 break; 13010 } 13011 sbuf_cat(&sb, path_str); 13012 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13013 (intmax_t)time_uptime - io->io_hdr.start_time); 13014 sbuf_finish(&sb); 13015 printf("%s", sbuf_data(&sb)); 13016 } 13017#endif /* CTL_TIME_IO */ 13018 13019 switch (io->io_hdr.io_type) { 13020 case CTL_IO_SCSI: 13021 break; 13022 case CTL_IO_TASK: 13023 if (ctl_debug & CTL_DEBUG_INFO) 13024 ctl_io_error_print(io, NULL); 13025 fe_done(io); 13026 return; 13027 default: 13028 panic("ctl_process_done: invalid io type %d\n", 13029 io->io_hdr.io_type); 13030 break; /* NOTREACHED */ 13031 } 13032 13033 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13034 if (lun == NULL) { 13035 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13036 io->io_hdr.nexus.targ_mapped_lun)); 13037 goto bailout; 13038 } 13039 13040 mtx_lock(&lun->lun_lock); 13041 13042 /* 13043 * Check to see if we have any errors to inject here. We only 13044 * inject errors for commands that don't already have errors set. 13045 */ 13046 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13047 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13048 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13049 ctl_inject_error(lun, io); 13050 13051 /* 13052 * XXX KDM how do we treat commands that aren't completed 13053 * successfully? 13054 * 13055 * XXX KDM should we also track I/O latency? 13056 */ 13057 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13058 io->io_hdr.io_type == CTL_IO_SCSI) { 13059#ifdef CTL_TIME_IO 13060 struct bintime cur_bt; 13061#endif 13062 int type; 13063 13064 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13065 CTL_FLAG_DATA_IN) 13066 type = CTL_STATS_READ; 13067 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13068 CTL_FLAG_DATA_OUT) 13069 type = CTL_STATS_WRITE; 13070 else 13071 type = CTL_STATS_NO_IO; 13072 13073 lun->stats.ports[targ_port].bytes[type] += 13074 io->scsiio.kern_total_len; 13075 lun->stats.ports[targ_port].operations[type]++; 13076#ifdef CTL_TIME_IO 13077 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13078 &io->io_hdr.dma_bt); 13079 getbinuptime(&cur_bt); 13080 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13081 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13082#endif 13083 lun->stats.ports[targ_port].num_dmas[type] += 13084 io->io_hdr.num_dmas; 13085 } 13086 13087 /* 13088 * Remove this from the OOA queue. 13089 */ 13090 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13091#ifdef CTL_TIME_IO 13092 if (TAILQ_EMPTY(&lun->ooa_queue)) 13093 lun->last_busy = getsbinuptime(); 13094#endif 13095 13096 /* 13097 * Run through the blocked queue on this LUN and see if anything 13098 * has become unblocked, now that this transaction is done. 13099 */ 13100 ctl_check_blocked(lun); 13101 13102 /* 13103 * If the LUN has been invalidated, free it if there is nothing 13104 * left on its OOA queue. 13105 */ 13106 if ((lun->flags & CTL_LUN_INVALID) 13107 && TAILQ_EMPTY(&lun->ooa_queue)) { 13108 mtx_unlock(&lun->lun_lock); 13109 mtx_lock(&softc->ctl_lock); 13110 ctl_free_lun(lun); 13111 mtx_unlock(&softc->ctl_lock); 13112 } else 13113 mtx_unlock(&lun->lun_lock); 13114 13115bailout: 13116 13117 /* 13118 * If this command has been aborted, make sure we set the status 13119 * properly. The FETD is responsible for freeing the I/O and doing 13120 * whatever it needs to do to clean up its state. 13121 */ 13122 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13123 ctl_set_task_aborted(&io->scsiio); 13124 13125 /* 13126 * If enabled, print command error status. 13127 */ 13128 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13129 (ctl_debug & CTL_DEBUG_INFO) != 0) 13130 ctl_io_error_print(io, NULL); 13131 13132 /* 13133 * Tell the FETD or the other shelf controller we're done with this 13134 * command. Note that only SCSI commands get to this point. Task 13135 * management commands are completed above. 13136 */ 13137 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13138 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13139 memset(&msg, 0, sizeof(msg)); 13140 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13141 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13142 msg.hdr.nexus = io->io_hdr.nexus; 13143 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13144 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13145 M_WAITOK); 13146 } 13147 13148 fe_done(io); 13149} 13150 13151#ifdef CTL_WITH_CA 13152/* 13153 * Front end should call this if it doesn't do autosense. When the request 13154 * sense comes back in from the initiator, we'll dequeue this and send it. 13155 */ 13156int 13157ctl_queue_sense(union ctl_io *io) 13158{ 13159 struct ctl_lun *lun; 13160 struct ctl_port *port; 13161 struct ctl_softc *softc; 13162 uint32_t initidx, targ_lun; 13163 13164 softc = control_softc; 13165 13166 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13167 13168 /* 13169 * LUN lookup will likely move to the ctl_work_thread() once we 13170 * have our new queueing infrastructure (that doesn't put things on 13171 * a per-LUN queue initially). That is so that we can handle 13172 * things like an INQUIRY to a LUN that we don't have enabled. We 13173 * can't deal with that right now. 13174 */ 13175 mtx_lock(&softc->ctl_lock); 13176 13177 /* 13178 * If we don't have a LUN for this, just toss the sense 13179 * information. 13180 */ 13181 port = ctl_io_port(&ctsio->io_hdr); 13182 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13183 if ((targ_lun < CTL_MAX_LUNS) 13184 && (softc->ctl_luns[targ_lun] != NULL)) 13185 lun = softc->ctl_luns[targ_lun]; 13186 else 13187 goto bailout; 13188 13189 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13190 13191 mtx_lock(&lun->lun_lock); 13192 /* 13193 * Already have CA set for this LUN...toss the sense information. 13194 */ 13195 if (ctl_is_set(lun->have_ca, initidx)) { 13196 mtx_unlock(&lun->lun_lock); 13197 goto bailout; 13198 } 13199 13200 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13201 MIN(sizeof(lun->pending_sense[initidx]), 13202 sizeof(io->scsiio.sense_data))); 13203 ctl_set_mask(lun->have_ca, initidx); 13204 mtx_unlock(&lun->lun_lock); 13205 13206bailout: 13207 mtx_unlock(&softc->ctl_lock); 13208 13209 ctl_free_io(io); 13210 13211 return (CTL_RETVAL_COMPLETE); 13212} 13213#endif 13214 13215/* 13216 * Primary command inlet from frontend ports. All SCSI and task I/O 13217 * requests must go through this function. 13218 */ 13219int 13220ctl_queue(union ctl_io *io) 13221{ 13222 struct ctl_port *port; 13223 13224 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13225 13226#ifdef CTL_TIME_IO 13227 io->io_hdr.start_time = time_uptime; 13228 getbinuptime(&io->io_hdr.start_bt); 13229#endif /* CTL_TIME_IO */ 13230 13231 /* Map FE-specific LUN ID into global one. */ 13232 port = ctl_io_port(&io->io_hdr); 13233 io->io_hdr.nexus.targ_mapped_lun = 13234 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13235 13236 switch (io->io_hdr.io_type) { 13237 case CTL_IO_SCSI: 13238 case CTL_IO_TASK: 13239 if (ctl_debug & CTL_DEBUG_CDB) 13240 ctl_io_print(io); 13241 ctl_enqueue_incoming(io); 13242 break; 13243 default: 13244 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13245 return (EINVAL); 13246 } 13247 13248 return (CTL_RETVAL_COMPLETE); 13249} 13250 13251#ifdef CTL_IO_DELAY 13252static void 13253ctl_done_timer_wakeup(void *arg) 13254{ 13255 union ctl_io *io; 13256 13257 io = (union ctl_io *)arg; 13258 ctl_done(io); 13259} 13260#endif /* CTL_IO_DELAY */ 13261 13262void 13263ctl_serseq_done(union ctl_io *io) 13264{ 13265 struct ctl_lun *lun; 13266 13267 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13268 if (lun->be_lun == NULL || 13269 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13270 return; 13271 mtx_lock(&lun->lun_lock); 13272 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13273 ctl_check_blocked(lun); 13274 mtx_unlock(&lun->lun_lock); 13275} 13276 13277void 13278ctl_done(union ctl_io *io) 13279{ 13280 13281 /* 13282 * Enable this to catch duplicate completion issues. 13283 */ 13284#if 0 13285 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13286 printf("%s: type %d msg %d cdb %x iptl: " 13287 "%u:%u:%u tag 0x%04x " 13288 "flag %#x status %x\n", 13289 __func__, 13290 io->io_hdr.io_type, 13291 io->io_hdr.msg_type, 13292 io->scsiio.cdb[0], 13293 io->io_hdr.nexus.initid, 13294 io->io_hdr.nexus.targ_port, 13295 io->io_hdr.nexus.targ_lun, 13296 (io->io_hdr.io_type == 13297 CTL_IO_TASK) ? 13298 io->taskio.tag_num : 13299 io->scsiio.tag_num, 13300 io->io_hdr.flags, 13301 io->io_hdr.status); 13302 } else 13303 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13304#endif 13305 13306 /* 13307 * This is an internal copy of an I/O, and should not go through 13308 * the normal done processing logic. 13309 */ 13310 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13311 return; 13312 13313#ifdef CTL_IO_DELAY 13314 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13315 struct ctl_lun *lun; 13316 13317 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13318 13319 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13320 } else { 13321 struct ctl_lun *lun; 13322 13323 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13324 13325 if ((lun != NULL) 13326 && (lun->delay_info.done_delay > 0)) { 13327 13328 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13329 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13330 callout_reset(&io->io_hdr.delay_callout, 13331 lun->delay_info.done_delay * hz, 13332 ctl_done_timer_wakeup, io); 13333 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13334 lun->delay_info.done_delay = 0; 13335 return; 13336 } 13337 } 13338#endif /* CTL_IO_DELAY */ 13339 13340 ctl_enqueue_done(io); 13341} 13342 13343static void 13344ctl_work_thread(void *arg) 13345{ 13346 struct ctl_thread *thr = (struct ctl_thread *)arg; 13347 struct ctl_softc *softc = thr->ctl_softc; 13348 union ctl_io *io; 13349 int retval; 13350 13351 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13352 13353 for (;;) { 13354 /* 13355 * We handle the queues in this order: 13356 * - ISC 13357 * - done queue (to free up resources, unblock other commands) 13358 * - RtR queue 13359 * - incoming queue 13360 * 13361 * If those queues are empty, we break out of the loop and 13362 * go to sleep. 13363 */ 13364 mtx_lock(&thr->queue_lock); 13365 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13366 if (io != NULL) { 13367 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13368 mtx_unlock(&thr->queue_lock); 13369 ctl_handle_isc(io); 13370 continue; 13371 } 13372 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13373 if (io != NULL) { 13374 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13375 /* clear any blocked commands, call fe_done */ 13376 mtx_unlock(&thr->queue_lock); 13377 ctl_process_done(io); 13378 continue; 13379 } 13380 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13381 if (io != NULL) { 13382 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13383 mtx_unlock(&thr->queue_lock); 13384 if (io->io_hdr.io_type == CTL_IO_TASK) 13385 ctl_run_task(io); 13386 else 13387 ctl_scsiio_precheck(softc, &io->scsiio); 13388 continue; 13389 } 13390 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13391 if (io != NULL) { 13392 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13393 mtx_unlock(&thr->queue_lock); 13394 retval = ctl_scsiio(&io->scsiio); 13395 if (retval != CTL_RETVAL_COMPLETE) 13396 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13397 continue; 13398 } 13399 13400 /* Sleep until we have something to do. */ 13401 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13402 } 13403} 13404 13405static void 13406ctl_lun_thread(void *arg) 13407{ 13408 struct ctl_softc *softc = (struct ctl_softc *)arg; 13409 struct ctl_be_lun *be_lun; 13410 13411 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13412 13413 for (;;) { 13414 mtx_lock(&softc->ctl_lock); 13415 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13416 if (be_lun != NULL) { 13417 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13418 mtx_unlock(&softc->ctl_lock); 13419 ctl_create_lun(be_lun); 13420 continue; 13421 } 13422 13423 /* Sleep until we have something to do. */ 13424 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13425 PDROP | PRIBIO, "-", 0); 13426 } 13427} 13428 13429static void 13430ctl_thresh_thread(void *arg) 13431{ 13432 struct ctl_softc *softc = (struct ctl_softc *)arg; 13433 struct ctl_lun *lun; 13434 struct scsi_da_rw_recovery_page *rwpage; 13435 struct ctl_logical_block_provisioning_page *page; 13436 const char *attr; 13437 union ctl_ha_msg msg; 13438 uint64_t thres, val; 13439 int i, e, set; 13440 13441 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13442 13443 for (;;) { 13444 mtx_lock(&softc->ctl_lock); 13445 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13446 if ((lun->flags & CTL_LUN_DISABLED) || 13447 (lun->flags & CTL_LUN_NO_MEDIA) || 13448 lun->backend->lun_attr == NULL) 13449 continue; 13450 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13451 softc->ha_mode == CTL_HA_MODE_XFER) 13452 continue; 13453 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13454 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13455 continue; 13456 e = 0; 13457 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13458 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13459 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13460 continue; 13461 thres = scsi_4btoul(page->descr[i].count); 13462 thres <<= CTL_LBP_EXPONENT; 13463 switch (page->descr[i].resource) { 13464 case 0x01: 13465 attr = "blocksavail"; 13466 break; 13467 case 0x02: 13468 attr = "blocksused"; 13469 break; 13470 case 0xf1: 13471 attr = "poolblocksavail"; 13472 break; 13473 case 0xf2: 13474 attr = "poolblocksused"; 13475 break; 13476 default: 13477 continue; 13478 } 13479 mtx_unlock(&softc->ctl_lock); // XXX 13480 val = lun->backend->lun_attr( 13481 lun->be_lun->be_lun, attr); 13482 mtx_lock(&softc->ctl_lock); 13483 if (val == UINT64_MAX) 13484 continue; 13485 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13486 == SLBPPD_ARMING_INC) 13487 e = (val >= thres); 13488 else 13489 e = (val <= thres); 13490 if (e) 13491 break; 13492 } 13493 mtx_lock(&lun->lun_lock); 13494 if (e) { 13495 scsi_u64to8b((uint8_t *)&page->descr[i] - 13496 (uint8_t *)page, lun->ua_tpt_info); 13497 if (lun->lasttpt == 0 || 13498 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13499 lun->lasttpt = time_uptime; 13500 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13501 set = 1; 13502 } else 13503 set = 0; 13504 } else { 13505 lun->lasttpt = 0; 13506 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13507 set = -1; 13508 } 13509 mtx_unlock(&lun->lun_lock); 13510 if (set != 0 && 13511 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13512 /* Send msg to other side. */ 13513 bzero(&msg.ua, sizeof(msg.ua)); 13514 msg.hdr.msg_type = CTL_MSG_UA; 13515 msg.hdr.nexus.initid = -1; 13516 msg.hdr.nexus.targ_port = -1; 13517 msg.hdr.nexus.targ_lun = lun->lun; 13518 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13519 msg.ua.ua_all = 1; 13520 msg.ua.ua_set = (set > 0); 13521 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13522 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13523 mtx_unlock(&softc->ctl_lock); // XXX 13524 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13525 sizeof(msg.ua), M_WAITOK); 13526 mtx_lock(&softc->ctl_lock); 13527 } 13528 } 13529 mtx_unlock(&softc->ctl_lock); 13530 pause("-", CTL_LBP_PERIOD * hz); 13531 } 13532} 13533 13534static void 13535ctl_enqueue_incoming(union ctl_io *io) 13536{ 13537 struct ctl_softc *softc = control_softc; 13538 struct ctl_thread *thr; 13539 u_int idx; 13540 13541 idx = (io->io_hdr.nexus.targ_port * 127 + 13542 io->io_hdr.nexus.initid) % worker_threads; 13543 thr = &softc->threads[idx]; 13544 mtx_lock(&thr->queue_lock); 13545 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13546 mtx_unlock(&thr->queue_lock); 13547 wakeup(thr); 13548} 13549 13550static void 13551ctl_enqueue_rtr(union ctl_io *io) 13552{ 13553 struct ctl_softc *softc = control_softc; 13554 struct ctl_thread *thr; 13555 13556 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13557 mtx_lock(&thr->queue_lock); 13558 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13559 mtx_unlock(&thr->queue_lock); 13560 wakeup(thr); 13561} 13562 13563static void 13564ctl_enqueue_done(union ctl_io *io) 13565{ 13566 struct ctl_softc *softc = control_softc; 13567 struct ctl_thread *thr; 13568 13569 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13570 mtx_lock(&thr->queue_lock); 13571 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13572 mtx_unlock(&thr->queue_lock); 13573 wakeup(thr); 13574} 13575 13576static void 13577ctl_enqueue_isc(union ctl_io *io) 13578{ 13579 struct ctl_softc *softc = control_softc; 13580 struct ctl_thread *thr; 13581 13582 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13583 mtx_lock(&thr->queue_lock); 13584 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13585 mtx_unlock(&thr->queue_lock); 13586 wakeup(thr); 13587} 13588 13589/* 13590 * vim: ts=8 13591 */ 13592