ctl.c revision 275886
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 275886 2014-12-18 08:30:28Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/ctype.h> 50#include <sys/kernel.h> 51#include <sys/types.h> 52#include <sys/kthread.h> 53#include <sys/bio.h> 54#include <sys/fcntl.h> 55#include <sys/lock.h> 56#include <sys/module.h> 57#include <sys/mutex.h> 58#include <sys/condvar.h> 59#include <sys/malloc.h> 60#include <sys/conf.h> 61#include <sys/ioccom.h> 62#include <sys/queue.h> 63#include <sys/sbuf.h> 64#include <sys/smp.h> 65#include <sys/endian.h> 66#include <sys/sysctl.h> 67#include <vm/uma.h> 68 69#include <cam/cam.h> 70#include <cam/scsi/scsi_all.h> 71#include <cam/scsi/scsi_da.h> 72#include <cam/ctl/ctl_io.h> 73#include <cam/ctl/ctl.h> 74#include <cam/ctl/ctl_frontend.h> 75#include <cam/ctl/ctl_frontend_internal.h> 76#include <cam/ctl/ctl_util.h> 77#include <cam/ctl/ctl_backend.h> 78#include <cam/ctl/ctl_ioctl.h> 79#include <cam/ctl/ctl_ha.h> 80#include <cam/ctl/ctl_private.h> 81#include <cam/ctl/ctl_debug.h> 82#include <cam/ctl/ctl_scsi_all.h> 83#include <cam/ctl/ctl_error.h> 84 85struct ctl_softc *control_softc = NULL; 86 87/* 88 * Size and alignment macros needed for Copan-specific HA hardware. These 89 * can go away when the HA code is re-written, and uses busdma for any 90 * hardware. 91 */ 92#define CTL_ALIGN_8B(target, source, type) \ 93 if (((uint32_t)source & 0x7) != 0) \ 94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 95 else \ 96 target = (type)source; 97 98#define CTL_SIZE_8B(target, size) \ 99 if ((size & 0x7) != 0) \ 100 target = size + (0x8 - (size & 0x7)); \ 101 else \ 102 target = size; 103 104#define CTL_ALIGN_8B_MARGIN 16 105 106/* 107 * Template mode pages. 108 */ 109 110/* 111 * Note that these are default values only. The actual values will be 112 * filled in when the user does a mode sense. 113 */ 114static struct copan_debugconf_subpage debugconf_page_default = { 115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 116 DBGCNF_SUBPAGE_CODE, /* subpage */ 117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 119 DBGCNF_VERSION, /* page_version */ 120 {CTL_TIME_IO_DEFAULT_SECS>>8, 121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 122}; 123 124static struct copan_debugconf_subpage debugconf_page_changeable = { 125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 126 DBGCNF_SUBPAGE_CODE, /* subpage */ 127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 129 0, /* page_version */ 130 {0xff,0xff}, /* ctl_time_io_secs */ 131}; 132 133static struct scsi_da_rw_recovery_page rw_er_page_default = { 134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 137 /*read_retry_count*/0, 138 /*correction_span*/0, 139 /*head_offset_count*/0, 140 /*data_strobe_offset_cnt*/0, 141 /*byte8*/SMS_RWER_LBPERE, 142 /*write_retry_count*/0, 143 /*reserved2*/0, 144 /*recovery_time_limit*/{0, 0}, 145}; 146 147static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 150 /*byte3*/0, 151 /*read_retry_count*/0, 152 /*correction_span*/0, 153 /*head_offset_count*/0, 154 /*data_strobe_offset_cnt*/0, 155 /*byte8*/0, 156 /*write_retry_count*/0, 157 /*reserved2*/0, 158 /*recovery_time_limit*/{0, 0}, 159}; 160 161static struct scsi_format_page format_page_default = { 162 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 163 /*page_length*/sizeof(struct scsi_format_page) - 2, 164 /*tracks_per_zone*/ {0, 0}, 165 /*alt_sectors_per_zone*/ {0, 0}, 166 /*alt_tracks_per_zone*/ {0, 0}, 167 /*alt_tracks_per_lun*/ {0, 0}, 168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 170 /*bytes_per_sector*/ {0, 0}, 171 /*interleave*/ {0, 0}, 172 /*track_skew*/ {0, 0}, 173 /*cylinder_skew*/ {0, 0}, 174 /*flags*/ SFP_HSEC, 175 /*reserved*/ {0, 0, 0} 176}; 177 178static struct scsi_format_page format_page_changeable = { 179 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 180 /*page_length*/sizeof(struct scsi_format_page) - 2, 181 /*tracks_per_zone*/ {0, 0}, 182 /*alt_sectors_per_zone*/ {0, 0}, 183 /*alt_tracks_per_zone*/ {0, 0}, 184 /*alt_tracks_per_lun*/ {0, 0}, 185 /*sectors_per_track*/ {0, 0}, 186 /*bytes_per_sector*/ {0, 0}, 187 /*interleave*/ {0, 0}, 188 /*track_skew*/ {0, 0}, 189 /*cylinder_skew*/ {0, 0}, 190 /*flags*/ 0, 191 /*reserved*/ {0, 0, 0} 192}; 193 194static struct scsi_rigid_disk_page rigid_disk_page_default = { 195 /*page_code*/SMS_RIGID_DISK_PAGE, 196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 197 /*cylinders*/ {0, 0, 0}, 198 /*heads*/ CTL_DEFAULT_HEADS, 199 /*start_write_precomp*/ {0, 0, 0}, 200 /*start_reduced_current*/ {0, 0, 0}, 201 /*step_rate*/ {0, 0}, 202 /*landing_zone_cylinder*/ {0, 0, 0}, 203 /*rpl*/ SRDP_RPL_DISABLED, 204 /*rotational_offset*/ 0, 205 /*reserved1*/ 0, 206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 207 CTL_DEFAULT_ROTATION_RATE & 0xff}, 208 /*reserved2*/ {0, 0} 209}; 210 211static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 212 /*page_code*/SMS_RIGID_DISK_PAGE, 213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 214 /*cylinders*/ {0, 0, 0}, 215 /*heads*/ 0, 216 /*start_write_precomp*/ {0, 0, 0}, 217 /*start_reduced_current*/ {0, 0, 0}, 218 /*step_rate*/ {0, 0}, 219 /*landing_zone_cylinder*/ {0, 0, 0}, 220 /*rpl*/ 0, 221 /*rotational_offset*/ 0, 222 /*reserved1*/ 0, 223 /*rotation_rate*/ {0, 0}, 224 /*reserved2*/ {0, 0} 225}; 226 227static struct scsi_caching_page caching_page_default = { 228 /*page_code*/SMS_CACHING_PAGE, 229 /*page_length*/sizeof(struct scsi_caching_page) - 2, 230 /*flags1*/ SCP_DISC | SCP_WCE, 231 /*ret_priority*/ 0, 232 /*disable_pf_transfer_len*/ {0xff, 0xff}, 233 /*min_prefetch*/ {0, 0}, 234 /*max_prefetch*/ {0xff, 0xff}, 235 /*max_pf_ceiling*/ {0xff, 0xff}, 236 /*flags2*/ 0, 237 /*cache_segments*/ 0, 238 /*cache_seg_size*/ {0, 0}, 239 /*reserved*/ 0, 240 /*non_cache_seg_size*/ {0, 0, 0} 241}; 242 243static struct scsi_caching_page caching_page_changeable = { 244 /*page_code*/SMS_CACHING_PAGE, 245 /*page_length*/sizeof(struct scsi_caching_page) - 2, 246 /*flags1*/ SCP_WCE | SCP_RCD, 247 /*ret_priority*/ 0, 248 /*disable_pf_transfer_len*/ {0, 0}, 249 /*min_prefetch*/ {0, 0}, 250 /*max_prefetch*/ {0, 0}, 251 /*max_pf_ceiling*/ {0, 0}, 252 /*flags2*/ 0, 253 /*cache_segments*/ 0, 254 /*cache_seg_size*/ {0, 0}, 255 /*reserved*/ 0, 256 /*non_cache_seg_size*/ {0, 0, 0} 257}; 258 259static struct scsi_control_page control_page_default = { 260 /*page_code*/SMS_CONTROL_MODE_PAGE, 261 /*page_length*/sizeof(struct scsi_control_page) - 2, 262 /*rlec*/0, 263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 264 /*eca_and_aen*/0, 265 /*flags4*/SCP_TAS, 266 /*aen_holdoff_period*/{0, 0}, 267 /*busy_timeout_period*/{0, 0}, 268 /*extended_selftest_completion_time*/{0, 0} 269}; 270 271static struct scsi_control_page control_page_changeable = { 272 /*page_code*/SMS_CONTROL_MODE_PAGE, 273 /*page_length*/sizeof(struct scsi_control_page) - 2, 274 /*rlec*/SCP_DSENSE, 275 /*queue_flags*/SCP_QUEUE_ALG_MASK, 276 /*eca_and_aen*/SCP_SWP, 277 /*flags4*/0, 278 /*aen_holdoff_period*/{0, 0}, 279 /*busy_timeout_period*/{0, 0}, 280 /*extended_selftest_completion_time*/{0, 0} 281}; 282 283static struct scsi_info_exceptions_page ie_page_default = { 284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 286 /*info_flags*/SIEP_FLAGS_DEXCPT, 287 /*mrie*/0, 288 /*interval_timer*/{0, 0, 0, 0}, 289 /*report_count*/{0, 0, 0, 0} 290}; 291 292static struct scsi_info_exceptions_page ie_page_changeable = { 293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 295 /*info_flags*/0, 296 /*mrie*/0, 297 /*interval_timer*/{0, 0, 0, 0}, 298 /*report_count*/{0, 0, 0, 0} 299}; 300 301#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 302 303static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 305 /*subpage_code*/0x02, 306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 307 /*flags*/0, 308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 309 /*descr*/{}}, 310 {{/*flags*/0, 311 /*resource*/0x01, 312 /*reserved*/{0, 0}, 313 /*count*/{0, 0, 0, 0}}, 314 {/*flags*/0, 315 /*resource*/0x02, 316 /*reserved*/{0, 0}, 317 /*count*/{0, 0, 0, 0}}, 318 {/*flags*/0, 319 /*resource*/0xf1, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0xf2, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}} 326 } 327}; 328 329static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 331 /*subpage_code*/0x02, 332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 333 /*flags*/0, 334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 335 /*descr*/{}}, 336 {{/*flags*/0, 337 /*resource*/0, 338 /*reserved*/{0, 0}, 339 /*count*/{0, 0, 0, 0}}, 340 {/*flags*/0, 341 /*resource*/0, 342 /*reserved*/{0, 0}, 343 /*count*/{0, 0, 0, 0}}, 344 {/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}} 352 } 353}; 354 355/* 356 * XXX KDM move these into the softc. 357 */ 358static int rcv_sync_msg; 359static uint8_t ctl_pause_rtr; 360 361SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 362static int worker_threads = -1; 363TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 364SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 365 &worker_threads, 1, "Number of worker threads"); 366static int ctl_debug = CTL_DEBUG_NONE; 367TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 368SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 369 &ctl_debug, 0, "Enabled debug flags"); 370 371/* 372 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 373 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 374 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 375 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 376 */ 377#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 378 379static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 380 int param); 381static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 382static int ctl_init(void); 383void ctl_shutdown(void); 384static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 385static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 386static void ctl_ioctl_online(void *arg); 387static void ctl_ioctl_offline(void *arg); 388static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 389static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 390static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 391static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 392static int ctl_ioctl_submit_wait(union ctl_io *io); 393static void ctl_ioctl_datamove(union ctl_io *io); 394static void ctl_ioctl_done(union ctl_io *io); 395static void ctl_ioctl_hard_startstop_callback(void *arg, 396 struct cfi_metatask *metatask); 397static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 398static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 399 struct ctl_ooa *ooa_hdr, 400 struct ctl_ooa_entry *kern_entries); 401static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 402 struct thread *td); 403static uint32_t ctl_map_lun(int port_num, uint32_t lun); 404static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 405#ifdef unused 406static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 407 uint32_t targ_target, uint32_t targ_lun, 408 int can_wait); 409static void ctl_kfree_io(union ctl_io *io); 410#endif /* unused */ 411static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 412 struct ctl_be_lun *be_lun, struct ctl_id target_id); 413static int ctl_free_lun(struct ctl_lun *lun); 414static void ctl_create_lun(struct ctl_be_lun *be_lun); 415/** 416static void ctl_failover_change_pages(struct ctl_softc *softc, 417 struct ctl_scsiio *ctsio, int master); 418**/ 419 420static int ctl_do_mode_select(union ctl_io *io); 421static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 422 uint64_t res_key, uint64_t sa_res_key, 423 uint8_t type, uint32_t residx, 424 struct ctl_scsiio *ctsio, 425 struct scsi_per_res_out *cdb, 426 struct scsi_per_res_out_parms* param); 427static void ctl_pro_preempt_other(struct ctl_lun *lun, 428 union ctl_ha_msg *msg); 429static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 430static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 431static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 432static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 433static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 434static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 435static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 436 int alloc_len); 437static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 438 int alloc_len); 439static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 440static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 441static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 442static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 443static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 444static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 445static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 446 union ctl_io *pending_io, union ctl_io *ooa_io); 447static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 448 union ctl_io *starting_io); 449static int ctl_check_blocked(struct ctl_lun *lun); 450static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 451 struct ctl_lun *lun, 452 const struct ctl_cmd_entry *entry, 453 struct ctl_scsiio *ctsio); 454//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 455static void ctl_failover(void); 456static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 457 struct ctl_scsiio *ctsio); 458static int ctl_scsiio(struct ctl_scsiio *ctsio); 459 460static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 461static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 462 ctl_ua_type ua_type); 463static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 464 ctl_ua_type ua_type); 465static int ctl_abort_task(union ctl_io *io); 466static int ctl_abort_task_set(union ctl_io *io); 467static int ctl_i_t_nexus_reset(union ctl_io *io); 468static void ctl_run_task(union ctl_io *io); 469#ifdef CTL_IO_DELAY 470static void ctl_datamove_timer_wakeup(void *arg); 471static void ctl_done_timer_wakeup(void *arg); 472#endif /* CTL_IO_DELAY */ 473 474static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 475static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 476static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 477static void ctl_datamove_remote_write(union ctl_io *io); 478static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 479static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 480static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 481static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 482 ctl_ha_dt_cb callback); 483static void ctl_datamove_remote_read(union ctl_io *io); 484static void ctl_datamove_remote(union ctl_io *io); 485static int ctl_process_done(union ctl_io *io); 486static void ctl_lun_thread(void *arg); 487static void ctl_thresh_thread(void *arg); 488static void ctl_work_thread(void *arg); 489static void ctl_enqueue_incoming(union ctl_io *io); 490static void ctl_enqueue_rtr(union ctl_io *io); 491static void ctl_enqueue_done(union ctl_io *io); 492static void ctl_enqueue_isc(union ctl_io *io); 493static const struct ctl_cmd_entry * 494 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 495static const struct ctl_cmd_entry * 496 ctl_validate_command(struct ctl_scsiio *ctsio); 497static int ctl_cmd_applicable(uint8_t lun_type, 498 const struct ctl_cmd_entry *entry); 499 500/* 501 * Load the serialization table. This isn't very pretty, but is probably 502 * the easiest way to do it. 503 */ 504#include "ctl_ser_table.c" 505 506/* 507 * We only need to define open, close and ioctl routines for this driver. 508 */ 509static struct cdevsw ctl_cdevsw = { 510 .d_version = D_VERSION, 511 .d_flags = 0, 512 .d_open = ctl_open, 513 .d_close = ctl_close, 514 .d_ioctl = ctl_ioctl, 515 .d_name = "ctl", 516}; 517 518 519MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 520MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 521 522static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 523 524static moduledata_t ctl_moduledata = { 525 "ctl", 526 ctl_module_event_handler, 527 NULL 528}; 529 530DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 531MODULE_VERSION(ctl, 1); 532 533static struct ctl_frontend ioctl_frontend = 534{ 535 .name = "ioctl", 536}; 537 538static void 539ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 540 union ctl_ha_msg *msg_info) 541{ 542 struct ctl_scsiio *ctsio; 543 544 if (msg_info->hdr.original_sc == NULL) { 545 printf("%s: original_sc == NULL!\n", __func__); 546 /* XXX KDM now what? */ 547 return; 548 } 549 550 ctsio = &msg_info->hdr.original_sc->scsiio; 551 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 553 ctsio->io_hdr.status = msg_info->hdr.status; 554 ctsio->scsi_status = msg_info->scsi.scsi_status; 555 ctsio->sense_len = msg_info->scsi.sense_len; 556 ctsio->sense_residual = msg_info->scsi.sense_residual; 557 ctsio->residual = msg_info->scsi.residual; 558 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 559 sizeof(ctsio->sense_data)); 560 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 561 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 562 ctl_enqueue_isc((union ctl_io *)ctsio); 563} 564 565static void 566ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 567 union ctl_ha_msg *msg_info) 568{ 569 struct ctl_scsiio *ctsio; 570 571 if (msg_info->hdr.serializing_sc == NULL) { 572 printf("%s: serializing_sc == NULL!\n", __func__); 573 /* XXX KDM now what? */ 574 return; 575 } 576 577 ctsio = &msg_info->hdr.serializing_sc->scsiio; 578#if 0 579 /* 580 * Attempt to catch the situation where an I/O has 581 * been freed, and we're using it again. 582 */ 583 if (ctsio->io_hdr.io_type == 0xff) { 584 union ctl_io *tmp_io; 585 tmp_io = (union ctl_io *)ctsio; 586 printf("%s: %p use after free!\n", __func__, 587 ctsio); 588 printf("%s: type %d msg %d cdb %x iptl: " 589 "%d:%d:%d:%d tag 0x%04x " 590 "flag %#x status %x\n", 591 __func__, 592 tmp_io->io_hdr.io_type, 593 tmp_io->io_hdr.msg_type, 594 tmp_io->scsiio.cdb[0], 595 tmp_io->io_hdr.nexus.initid.id, 596 tmp_io->io_hdr.nexus.targ_port, 597 tmp_io->io_hdr.nexus.targ_target.id, 598 tmp_io->io_hdr.nexus.targ_lun, 599 (tmp_io->io_hdr.io_type == 600 CTL_IO_TASK) ? 601 tmp_io->taskio.tag_num : 602 tmp_io->scsiio.tag_num, 603 tmp_io->io_hdr.flags, 604 tmp_io->io_hdr.status); 605 } 606#endif 607 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 608 ctl_enqueue_isc((union ctl_io *)ctsio); 609} 610 611/* 612 * ISC (Inter Shelf Communication) event handler. Events from the HA 613 * subsystem come in here. 614 */ 615static void 616ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 617{ 618 struct ctl_softc *ctl_softc; 619 union ctl_io *io; 620 struct ctl_prio *presio; 621 ctl_ha_status isc_status; 622 623 ctl_softc = control_softc; 624 io = NULL; 625 626 627#if 0 628 printf("CTL: Isc Msg event %d\n", event); 629#endif 630 if (event == CTL_HA_EVT_MSG_RECV) { 631 union ctl_ha_msg msg_info; 632 633 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 634 sizeof(msg_info), /*wait*/ 0); 635#if 0 636 printf("CTL: msg_type %d\n", msg_info.msg_type); 637#endif 638 if (isc_status != 0) { 639 printf("Error receiving message, status = %d\n", 640 isc_status); 641 return; 642 } 643 644 switch (msg_info.hdr.msg_type) { 645 case CTL_MSG_SERIALIZE: 646#if 0 647 printf("Serialize\n"); 648#endif 649 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool); 650 if (io == NULL) { 651 printf("ctl_isc_event_handler: can't allocate " 652 "ctl_io!\n"); 653 /* Bad Juju */ 654 /* Need to set busy and send msg back */ 655 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 656 msg_info.hdr.status = CTL_SCSI_ERROR; 657 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 658 msg_info.scsi.sense_len = 0; 659 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 660 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 661 } 662 goto bailout; 663 } 664 ctl_zero_io(io); 665 // populate ctsio from msg_info 666 io->io_hdr.io_type = CTL_IO_SCSI; 667 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 668 io->io_hdr.original_sc = msg_info.hdr.original_sc; 669#if 0 670 printf("pOrig %x\n", (int)msg_info.original_sc); 671#endif 672 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 673 CTL_FLAG_IO_ACTIVE; 674 /* 675 * If we're in serialization-only mode, we don't 676 * want to go through full done processing. Thus 677 * the COPY flag. 678 * 679 * XXX KDM add another flag that is more specific. 680 */ 681 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 682 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 683 io->io_hdr.nexus = msg_info.hdr.nexus; 684#if 0 685 printf("targ %d, port %d, iid %d, lun %d\n", 686 io->io_hdr.nexus.targ_target.id, 687 io->io_hdr.nexus.targ_port, 688 io->io_hdr.nexus.initid.id, 689 io->io_hdr.nexus.targ_lun); 690#endif 691 io->scsiio.tag_num = msg_info.scsi.tag_num; 692 io->scsiio.tag_type = msg_info.scsi.tag_type; 693 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 694 CTL_MAX_CDBLEN); 695 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 696 const struct ctl_cmd_entry *entry; 697 698 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 699 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 700 io->io_hdr.flags |= 701 entry->flags & CTL_FLAG_DATA_MASK; 702 } 703 ctl_enqueue_isc(io); 704 break; 705 706 /* Performed on the Originating SC, XFER mode only */ 707 case CTL_MSG_DATAMOVE: { 708 struct ctl_sg_entry *sgl; 709 int i, j; 710 711 io = msg_info.hdr.original_sc; 712 if (io == NULL) { 713 printf("%s: original_sc == NULL!\n", __func__); 714 /* XXX KDM do something here */ 715 break; 716 } 717 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 718 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 719 /* 720 * Keep track of this, we need to send it back over 721 * when the datamove is complete. 722 */ 723 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 724 725 if (msg_info.dt.sg_sequence == 0) { 726 /* 727 * XXX KDM we use the preallocated S/G list 728 * here, but we'll need to change this to 729 * dynamic allocation if we need larger S/G 730 * lists. 731 */ 732 if (msg_info.dt.kern_sg_entries > 733 sizeof(io->io_hdr.remote_sglist) / 734 sizeof(io->io_hdr.remote_sglist[0])) { 735 printf("%s: number of S/G entries " 736 "needed %u > allocated num %zd\n", 737 __func__, 738 msg_info.dt.kern_sg_entries, 739 sizeof(io->io_hdr.remote_sglist)/ 740 sizeof(io->io_hdr.remote_sglist[0])); 741 742 /* 743 * XXX KDM send a message back to 744 * the other side to shut down the 745 * DMA. The error will come back 746 * through via the normal channel. 747 */ 748 break; 749 } 750 sgl = io->io_hdr.remote_sglist; 751 memset(sgl, 0, 752 sizeof(io->io_hdr.remote_sglist)); 753 754 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 755 756 io->scsiio.kern_sg_entries = 757 msg_info.dt.kern_sg_entries; 758 io->scsiio.rem_sg_entries = 759 msg_info.dt.kern_sg_entries; 760 io->scsiio.kern_data_len = 761 msg_info.dt.kern_data_len; 762 io->scsiio.kern_total_len = 763 msg_info.dt.kern_total_len; 764 io->scsiio.kern_data_resid = 765 msg_info.dt.kern_data_resid; 766 io->scsiio.kern_rel_offset = 767 msg_info.dt.kern_rel_offset; 768 /* 769 * Clear out per-DMA flags. 770 */ 771 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 772 /* 773 * Add per-DMA flags that are set for this 774 * particular DMA request. 775 */ 776 io->io_hdr.flags |= msg_info.dt.flags & 777 CTL_FLAG_RDMA_MASK; 778 } else 779 sgl = (struct ctl_sg_entry *) 780 io->scsiio.kern_data_ptr; 781 782 for (i = msg_info.dt.sent_sg_entries, j = 0; 783 i < (msg_info.dt.sent_sg_entries + 784 msg_info.dt.cur_sg_entries); i++, j++) { 785 sgl[i].addr = msg_info.dt.sg_list[j].addr; 786 sgl[i].len = msg_info.dt.sg_list[j].len; 787 788#if 0 789 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 790 __func__, 791 msg_info.dt.sg_list[j].addr, 792 msg_info.dt.sg_list[j].len, 793 sgl[i].addr, sgl[i].len, j, i); 794#endif 795 } 796#if 0 797 memcpy(&sgl[msg_info.dt.sent_sg_entries], 798 msg_info.dt.sg_list, 799 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 800#endif 801 802 /* 803 * If this is the last piece of the I/O, we've got 804 * the full S/G list. Queue processing in the thread. 805 * Otherwise wait for the next piece. 806 */ 807 if (msg_info.dt.sg_last != 0) 808 ctl_enqueue_isc(io); 809 break; 810 } 811 /* Performed on the Serializing (primary) SC, XFER mode only */ 812 case CTL_MSG_DATAMOVE_DONE: { 813 if (msg_info.hdr.serializing_sc == NULL) { 814 printf("%s: serializing_sc == NULL!\n", 815 __func__); 816 /* XXX KDM now what? */ 817 break; 818 } 819 /* 820 * We grab the sense information here in case 821 * there was a failure, so we can return status 822 * back to the initiator. 823 */ 824 io = msg_info.hdr.serializing_sc; 825 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 826 io->io_hdr.status = msg_info.hdr.status; 827 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 828 io->scsiio.sense_len = msg_info.scsi.sense_len; 829 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 830 io->io_hdr.port_status = msg_info.scsi.fetd_status; 831 io->scsiio.residual = msg_info.scsi.residual; 832 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 833 sizeof(io->scsiio.sense_data)); 834 ctl_enqueue_isc(io); 835 break; 836 } 837 838 /* Preformed on Originating SC, SER_ONLY mode */ 839 case CTL_MSG_R2R: 840 io = msg_info.hdr.original_sc; 841 if (io == NULL) { 842 printf("%s: Major Bummer\n", __func__); 843 return; 844 } else { 845#if 0 846 printf("pOrig %x\n",(int) ctsio); 847#endif 848 } 849 io->io_hdr.msg_type = CTL_MSG_R2R; 850 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 851 ctl_enqueue_isc(io); 852 break; 853 854 /* 855 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 856 * mode. 857 * Performed on the Originating (i.e. secondary) SC in XFER 858 * mode 859 */ 860 case CTL_MSG_FINISH_IO: 861 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 862 ctl_isc_handler_finish_xfer(ctl_softc, 863 &msg_info); 864 else 865 ctl_isc_handler_finish_ser_only(ctl_softc, 866 &msg_info); 867 break; 868 869 /* Preformed on Originating SC */ 870 case CTL_MSG_BAD_JUJU: 871 io = msg_info.hdr.original_sc; 872 if (io == NULL) { 873 printf("%s: Bad JUJU!, original_sc is NULL!\n", 874 __func__); 875 break; 876 } 877 ctl_copy_sense_data(&msg_info, io); 878 /* 879 * IO should have already been cleaned up on other 880 * SC so clear this flag so we won't send a message 881 * back to finish the IO there. 882 */ 883 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 884 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 885 886 /* io = msg_info.hdr.serializing_sc; */ 887 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 888 ctl_enqueue_isc(io); 889 break; 890 891 /* Handle resets sent from the other side */ 892 case CTL_MSG_MANAGE_TASKS: { 893 struct ctl_taskio *taskio; 894 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 895 ctl_softc->othersc_pool); 896 if (taskio == NULL) { 897 printf("ctl_isc_event_handler: can't allocate " 898 "ctl_io!\n"); 899 /* Bad Juju */ 900 /* should I just call the proper reset func 901 here??? */ 902 goto bailout; 903 } 904 ctl_zero_io((union ctl_io *)taskio); 905 taskio->io_hdr.io_type = CTL_IO_TASK; 906 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 907 taskio->io_hdr.nexus = msg_info.hdr.nexus; 908 taskio->task_action = msg_info.task.task_action; 909 taskio->tag_num = msg_info.task.tag_num; 910 taskio->tag_type = msg_info.task.tag_type; 911#ifdef CTL_TIME_IO 912 taskio->io_hdr.start_time = time_uptime; 913 getbintime(&taskio->io_hdr.start_bt); 914#if 0 915 cs_prof_gettime(&taskio->io_hdr.start_ticks); 916#endif 917#endif /* CTL_TIME_IO */ 918 ctl_run_task((union ctl_io *)taskio); 919 break; 920 } 921 /* Persistent Reserve action which needs attention */ 922 case CTL_MSG_PERS_ACTION: 923 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 924 ctl_softc->othersc_pool); 925 if (presio == NULL) { 926 printf("ctl_isc_event_handler: can't allocate " 927 "ctl_io!\n"); 928 /* Bad Juju */ 929 /* Need to set busy and send msg back */ 930 goto bailout; 931 } 932 ctl_zero_io((union ctl_io *)presio); 933 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 934 presio->pr_msg = msg_info.pr; 935 ctl_enqueue_isc((union ctl_io *)presio); 936 break; 937 case CTL_MSG_SYNC_FE: 938 rcv_sync_msg = 1; 939 break; 940 default: 941 printf("How did I get here?\n"); 942 } 943 } else if (event == CTL_HA_EVT_MSG_SENT) { 944 if (param != CTL_HA_STATUS_SUCCESS) { 945 printf("Bad status from ctl_ha_msg_send status %d\n", 946 param); 947 } 948 return; 949 } else if (event == CTL_HA_EVT_DISCONNECT) { 950 printf("CTL: Got a disconnect from Isc\n"); 951 return; 952 } else { 953 printf("ctl_isc_event_handler: Unknown event %d\n", event); 954 return; 955 } 956 957bailout: 958 return; 959} 960 961static void 962ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 963{ 964 struct scsi_sense_data *sense; 965 966 sense = &dest->scsiio.sense_data; 967 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 968 dest->scsiio.scsi_status = src->scsi.scsi_status; 969 dest->scsiio.sense_len = src->scsi.sense_len; 970 dest->io_hdr.status = src->hdr.status; 971} 972 973static int 974ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 975{ 976 struct ctl_softc *softc = (struct ctl_softc *)arg1; 977 struct ctl_lun *lun; 978 int error, value, i; 979 980 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 981 value = 0; 982 else 983 value = 1; 984 985 error = sysctl_handle_int(oidp, &value, 0, req); 986 if ((error != 0) || (req->newptr == NULL)) 987 return (error); 988 989 mtx_lock(&softc->ctl_lock); 990 if (value == 0) 991 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 992 else 993 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 994 STAILQ_FOREACH(lun, &softc->lun_list, links) { 995 mtx_lock(&lun->lun_lock); 996 for (i = 0; i < CTL_MAX_INITIATORS; i++) 997 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE; 998 mtx_unlock(&lun->lun_lock); 999 } 1000 mtx_unlock(&softc->ctl_lock); 1001 return (0); 1002} 1003 1004static int 1005ctl_init(void) 1006{ 1007 struct ctl_softc *softc; 1008 void *other_pool; 1009 struct ctl_port *port; 1010 int i, error, retval; 1011 //int isc_retval; 1012 1013 retval = 0; 1014 ctl_pause_rtr = 0; 1015 rcv_sync_msg = 0; 1016 1017 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1018 M_WAITOK | M_ZERO); 1019 softc = control_softc; 1020 1021 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1022 "cam/ctl"); 1023 1024 softc->dev->si_drv1 = softc; 1025 1026 /* 1027 * By default, return a "bad LUN" peripheral qualifier for unknown 1028 * LUNs. The user can override this default using the tunable or 1029 * sysctl. See the comment in ctl_inquiry_std() for more details. 1030 */ 1031 softc->inquiry_pq_no_lun = 1; 1032 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1033 &softc->inquiry_pq_no_lun); 1034 sysctl_ctx_init(&softc->sysctl_ctx); 1035 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1036 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1037 CTLFLAG_RD, 0, "CAM Target Layer"); 1038 1039 if (softc->sysctl_tree == NULL) { 1040 printf("%s: unable to allocate sysctl tree\n", __func__); 1041 destroy_dev(softc->dev); 1042 free(control_softc, M_DEVBUF); 1043 control_softc = NULL; 1044 return (ENOMEM); 1045 } 1046 1047 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1048 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1049 "inquiry_pq_no_lun", CTLFLAG_RW, 1050 &softc->inquiry_pq_no_lun, 0, 1051 "Report no lun possible for invalid LUNs"); 1052 1053 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1054 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1055 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1056 softc->open_count = 0; 1057 1058 /* 1059 * Default to actually sending a SYNCHRONIZE CACHE command down to 1060 * the drive. 1061 */ 1062 softc->flags = CTL_FLAG_REAL_SYNC; 1063 1064 /* 1065 * In Copan's HA scheme, the "master" and "slave" roles are 1066 * figured out through the slot the controller is in. Although it 1067 * is an active/active system, someone has to be in charge. 1068 */ 1069 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1070 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1071 "HA head ID (0 - no HA)"); 1072 if (softc->ha_id == 0) { 1073 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1074 softc->is_single = 1; 1075 softc->port_offset = 0; 1076 } else 1077 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1078 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1079 1080 /* 1081 * XXX KDM need to figure out where we want to get our target ID 1082 * and WWID. Is it different on each port? 1083 */ 1084 softc->target.id = 0; 1085 softc->target.wwid[0] = 0x12345678; 1086 softc->target.wwid[1] = 0x87654321; 1087 STAILQ_INIT(&softc->lun_list); 1088 STAILQ_INIT(&softc->pending_lun_queue); 1089 STAILQ_INIT(&softc->fe_list); 1090 STAILQ_INIT(&softc->port_list); 1091 STAILQ_INIT(&softc->be_list); 1092 ctl_tpc_init(softc); 1093 1094 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1095 &other_pool) != 0) 1096 { 1097 printf("ctl: can't allocate %d entry other SC pool, " 1098 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1099 return (ENOMEM); 1100 } 1101 softc->othersc_pool = other_pool; 1102 1103 if (worker_threads <= 0) 1104 worker_threads = max(1, mp_ncpus / 4); 1105 if (worker_threads > CTL_MAX_THREADS) 1106 worker_threads = CTL_MAX_THREADS; 1107 1108 for (i = 0; i < worker_threads; i++) { 1109 struct ctl_thread *thr = &softc->threads[i]; 1110 1111 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1112 thr->ctl_softc = softc; 1113 STAILQ_INIT(&thr->incoming_queue); 1114 STAILQ_INIT(&thr->rtr_queue); 1115 STAILQ_INIT(&thr->done_queue); 1116 STAILQ_INIT(&thr->isc_queue); 1117 1118 error = kproc_kthread_add(ctl_work_thread, thr, 1119 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1120 if (error != 0) { 1121 printf("error creating CTL work thread!\n"); 1122 ctl_pool_free(other_pool); 1123 return (error); 1124 } 1125 } 1126 error = kproc_kthread_add(ctl_lun_thread, softc, 1127 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1128 if (error != 0) { 1129 printf("error creating CTL lun thread!\n"); 1130 ctl_pool_free(other_pool); 1131 return (error); 1132 } 1133 error = kproc_kthread_add(ctl_thresh_thread, softc, 1134 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1135 if (error != 0) { 1136 printf("error creating CTL threshold thread!\n"); 1137 ctl_pool_free(other_pool); 1138 return (error); 1139 } 1140 if (bootverbose) 1141 printf("ctl: CAM Target Layer loaded\n"); 1142 1143 /* 1144 * Initialize the ioctl front end. 1145 */ 1146 ctl_frontend_register(&ioctl_frontend); 1147 port = &softc->ioctl_info.port; 1148 port->frontend = &ioctl_frontend; 1149 sprintf(softc->ioctl_info.port_name, "ioctl"); 1150 port->port_type = CTL_PORT_IOCTL; 1151 port->num_requested_ctl_io = 100; 1152 port->port_name = softc->ioctl_info.port_name; 1153 port->port_online = ctl_ioctl_online; 1154 port->port_offline = ctl_ioctl_offline; 1155 port->onoff_arg = &softc->ioctl_info; 1156 port->lun_enable = ctl_ioctl_lun_enable; 1157 port->lun_disable = ctl_ioctl_lun_disable; 1158 port->targ_lun_arg = &softc->ioctl_info; 1159 port->fe_datamove = ctl_ioctl_datamove; 1160 port->fe_done = ctl_ioctl_done; 1161 port->max_targets = 15; 1162 port->max_target_id = 15; 1163 1164 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1165 printf("ctl: ioctl front end registration failed, will " 1166 "continue anyway\n"); 1167 } 1168 1169 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1170 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1171 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1172 1173#ifdef CTL_IO_DELAY 1174 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1175 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1176 sizeof(struct callout), CTL_TIMER_BYTES); 1177 return (EINVAL); 1178 } 1179#endif /* CTL_IO_DELAY */ 1180 1181 return (0); 1182} 1183 1184void 1185ctl_shutdown(void) 1186{ 1187 struct ctl_softc *softc; 1188 struct ctl_lun *lun, *next_lun; 1189 1190 softc = (struct ctl_softc *)control_softc; 1191 1192 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1193 printf("ctl: ioctl front end deregistration failed\n"); 1194 1195 mtx_lock(&softc->ctl_lock); 1196 1197 /* 1198 * Free up each LUN. 1199 */ 1200 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1201 next_lun = STAILQ_NEXT(lun, links); 1202 ctl_free_lun(lun); 1203 } 1204 1205 mtx_unlock(&softc->ctl_lock); 1206 1207 ctl_frontend_deregister(&ioctl_frontend); 1208 1209#if 0 1210 ctl_shutdown_thread(softc->work_thread); 1211 mtx_destroy(&softc->queue_lock); 1212#endif 1213 1214 ctl_tpc_shutdown(softc); 1215 uma_zdestroy(softc->io_zone); 1216 mtx_destroy(&softc->ctl_lock); 1217 1218 destroy_dev(softc->dev); 1219 1220 sysctl_ctx_free(&softc->sysctl_ctx); 1221 1222 free(control_softc, M_DEVBUF); 1223 control_softc = NULL; 1224 1225 if (bootverbose) 1226 printf("ctl: CAM Target Layer unloaded\n"); 1227} 1228 1229static int 1230ctl_module_event_handler(module_t mod, int what, void *arg) 1231{ 1232 1233 switch (what) { 1234 case MOD_LOAD: 1235 return (ctl_init()); 1236 case MOD_UNLOAD: 1237 return (EBUSY); 1238 default: 1239 return (EOPNOTSUPP); 1240 } 1241} 1242 1243/* 1244 * XXX KDM should we do some access checks here? Bump a reference count to 1245 * prevent a CTL module from being unloaded while someone has it open? 1246 */ 1247static int 1248ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1249{ 1250 return (0); 1251} 1252 1253static int 1254ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1255{ 1256 return (0); 1257} 1258 1259int 1260ctl_port_enable(ctl_port_type port_type) 1261{ 1262 struct ctl_softc *softc = control_softc; 1263 struct ctl_port *port; 1264 1265 if (softc->is_single == 0) { 1266 union ctl_ha_msg msg_info; 1267 int isc_retval; 1268 1269#if 0 1270 printf("%s: HA mode, synchronizing frontend enable\n", 1271 __func__); 1272#endif 1273 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1274 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1275 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1276 printf("Sync msg send error retval %d\n", isc_retval); 1277 } 1278 if (!rcv_sync_msg) { 1279 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1280 sizeof(msg_info), 1); 1281 } 1282#if 0 1283 printf("CTL:Frontend Enable\n"); 1284 } else { 1285 printf("%s: single mode, skipping frontend synchronization\n", 1286 __func__); 1287#endif 1288 } 1289 1290 STAILQ_FOREACH(port, &softc->port_list, links) { 1291 if (port_type & port->port_type) 1292 { 1293#if 0 1294 printf("port %d\n", port->targ_port); 1295#endif 1296 ctl_port_online(port); 1297 } 1298 } 1299 1300 return (0); 1301} 1302 1303int 1304ctl_port_disable(ctl_port_type port_type) 1305{ 1306 struct ctl_softc *softc; 1307 struct ctl_port *port; 1308 1309 softc = control_softc; 1310 1311 STAILQ_FOREACH(port, &softc->port_list, links) { 1312 if (port_type & port->port_type) 1313 ctl_port_offline(port); 1314 } 1315 1316 return (0); 1317} 1318 1319/* 1320 * Returns 0 for success, 1 for failure. 1321 * Currently the only failure mode is if there aren't enough entries 1322 * allocated. So, in case of a failure, look at num_entries_dropped, 1323 * reallocate and try again. 1324 */ 1325int 1326ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1327 int *num_entries_filled, int *num_entries_dropped, 1328 ctl_port_type port_type, int no_virtual) 1329{ 1330 struct ctl_softc *softc; 1331 struct ctl_port *port; 1332 int entries_dropped, entries_filled; 1333 int retval; 1334 int i; 1335 1336 softc = control_softc; 1337 1338 retval = 0; 1339 entries_filled = 0; 1340 entries_dropped = 0; 1341 1342 i = 0; 1343 mtx_lock(&softc->ctl_lock); 1344 STAILQ_FOREACH(port, &softc->port_list, links) { 1345 struct ctl_port_entry *entry; 1346 1347 if ((port->port_type & port_type) == 0) 1348 continue; 1349 1350 if ((no_virtual != 0) 1351 && (port->virtual_port != 0)) 1352 continue; 1353 1354 if (entries_filled >= num_entries_alloced) { 1355 entries_dropped++; 1356 continue; 1357 } 1358 entry = &entries[i]; 1359 1360 entry->port_type = port->port_type; 1361 strlcpy(entry->port_name, port->port_name, 1362 sizeof(entry->port_name)); 1363 entry->physical_port = port->physical_port; 1364 entry->virtual_port = port->virtual_port; 1365 entry->wwnn = port->wwnn; 1366 entry->wwpn = port->wwpn; 1367 1368 i++; 1369 entries_filled++; 1370 } 1371 1372 mtx_unlock(&softc->ctl_lock); 1373 1374 if (entries_dropped > 0) 1375 retval = 1; 1376 1377 *num_entries_dropped = entries_dropped; 1378 *num_entries_filled = entries_filled; 1379 1380 return (retval); 1381} 1382 1383static void 1384ctl_ioctl_online(void *arg) 1385{ 1386 struct ctl_ioctl_info *ioctl_info; 1387 1388 ioctl_info = (struct ctl_ioctl_info *)arg; 1389 1390 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1391} 1392 1393static void 1394ctl_ioctl_offline(void *arg) 1395{ 1396 struct ctl_ioctl_info *ioctl_info; 1397 1398 ioctl_info = (struct ctl_ioctl_info *)arg; 1399 1400 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1401} 1402 1403/* 1404 * Remove an initiator by port number and initiator ID. 1405 * Returns 0 for success, -1 for failure. 1406 */ 1407int 1408ctl_remove_initiator(struct ctl_port *port, int iid) 1409{ 1410 struct ctl_softc *softc = control_softc; 1411 1412 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1413 1414 if (iid > CTL_MAX_INIT_PER_PORT) { 1415 printf("%s: initiator ID %u > maximun %u!\n", 1416 __func__, iid, CTL_MAX_INIT_PER_PORT); 1417 return (-1); 1418 } 1419 1420 mtx_lock(&softc->ctl_lock); 1421 port->wwpn_iid[iid].in_use--; 1422 port->wwpn_iid[iid].last_use = time_uptime; 1423 mtx_unlock(&softc->ctl_lock); 1424 1425 return (0); 1426} 1427 1428/* 1429 * Add an initiator to the initiator map. 1430 * Returns iid for success, < 0 for failure. 1431 */ 1432int 1433ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1434{ 1435 struct ctl_softc *softc = control_softc; 1436 time_t best_time; 1437 int i, best; 1438 1439 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1440 1441 if (iid >= CTL_MAX_INIT_PER_PORT) { 1442 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1443 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1444 free(name, M_CTL); 1445 return (-1); 1446 } 1447 1448 mtx_lock(&softc->ctl_lock); 1449 1450 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1451 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1452 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1453 iid = i; 1454 break; 1455 } 1456 if (name != NULL && port->wwpn_iid[i].name != NULL && 1457 strcmp(name, port->wwpn_iid[i].name) == 0) { 1458 iid = i; 1459 break; 1460 } 1461 } 1462 } 1463 1464 if (iid < 0) { 1465 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1466 if (port->wwpn_iid[i].in_use == 0 && 1467 port->wwpn_iid[i].wwpn == 0 && 1468 port->wwpn_iid[i].name == NULL) { 1469 iid = i; 1470 break; 1471 } 1472 } 1473 } 1474 1475 if (iid < 0) { 1476 best = -1; 1477 best_time = INT32_MAX; 1478 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1479 if (port->wwpn_iid[i].in_use == 0) { 1480 if (port->wwpn_iid[i].last_use < best_time) { 1481 best = i; 1482 best_time = port->wwpn_iid[i].last_use; 1483 } 1484 } 1485 } 1486 iid = best; 1487 } 1488 1489 if (iid < 0) { 1490 mtx_unlock(&softc->ctl_lock); 1491 free(name, M_CTL); 1492 return (-2); 1493 } 1494 1495 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1496 /* 1497 * This is not an error yet. 1498 */ 1499 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1500#if 0 1501 printf("%s: port %d iid %u WWPN %#jx arrived" 1502 " again\n", __func__, port->targ_port, 1503 iid, (uintmax_t)wwpn); 1504#endif 1505 goto take; 1506 } 1507 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1508 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1509#if 0 1510 printf("%s: port %d iid %u name '%s' arrived" 1511 " again\n", __func__, port->targ_port, 1512 iid, name); 1513#endif 1514 goto take; 1515 } 1516 1517 /* 1518 * This is an error, but what do we do about it? The 1519 * driver is telling us we have a new WWPN for this 1520 * initiator ID, so we pretty much need to use it. 1521 */ 1522 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1523 " but WWPN %#jx '%s' is still at that address\n", 1524 __func__, port->targ_port, iid, wwpn, name, 1525 (uintmax_t)port->wwpn_iid[iid].wwpn, 1526 port->wwpn_iid[iid].name); 1527 1528 /* 1529 * XXX KDM clear have_ca and ua_pending on each LUN for 1530 * this initiator. 1531 */ 1532 } 1533take: 1534 free(port->wwpn_iid[iid].name, M_CTL); 1535 port->wwpn_iid[iid].name = name; 1536 port->wwpn_iid[iid].wwpn = wwpn; 1537 port->wwpn_iid[iid].in_use++; 1538 mtx_unlock(&softc->ctl_lock); 1539 1540 return (iid); 1541} 1542 1543static int 1544ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1545{ 1546 int len; 1547 1548 switch (port->port_type) { 1549 case CTL_PORT_FC: 1550 { 1551 struct scsi_transportid_fcp *id = 1552 (struct scsi_transportid_fcp *)buf; 1553 if (port->wwpn_iid[iid].wwpn == 0) 1554 return (0); 1555 memset(id, 0, sizeof(*id)); 1556 id->format_protocol = SCSI_PROTO_FC; 1557 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1558 return (sizeof(*id)); 1559 } 1560 case CTL_PORT_ISCSI: 1561 { 1562 struct scsi_transportid_iscsi_port *id = 1563 (struct scsi_transportid_iscsi_port *)buf; 1564 if (port->wwpn_iid[iid].name == NULL) 1565 return (0); 1566 memset(id, 0, 256); 1567 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1568 SCSI_PROTO_ISCSI; 1569 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1570 len = roundup2(min(len, 252), 4); 1571 scsi_ulto2b(len, id->additional_length); 1572 return (sizeof(*id) + len); 1573 } 1574 case CTL_PORT_SAS: 1575 { 1576 struct scsi_transportid_sas *id = 1577 (struct scsi_transportid_sas *)buf; 1578 if (port->wwpn_iid[iid].wwpn == 0) 1579 return (0); 1580 memset(id, 0, sizeof(*id)); 1581 id->format_protocol = SCSI_PROTO_SAS; 1582 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1583 return (sizeof(*id)); 1584 } 1585 default: 1586 { 1587 struct scsi_transportid_spi *id = 1588 (struct scsi_transportid_spi *)buf; 1589 memset(id, 0, sizeof(*id)); 1590 id->format_protocol = SCSI_PROTO_SPI; 1591 scsi_ulto2b(iid, id->scsi_addr); 1592 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1593 return (sizeof(*id)); 1594 } 1595 } 1596} 1597 1598static int 1599ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1600{ 1601 return (0); 1602} 1603 1604static int 1605ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1606{ 1607 return (0); 1608} 1609 1610/* 1611 * Data movement routine for the CTL ioctl frontend port. 1612 */ 1613static int 1614ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1615{ 1616 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1617 struct ctl_sg_entry ext_entry, kern_entry; 1618 int ext_sglen, ext_sg_entries, kern_sg_entries; 1619 int ext_sg_start, ext_offset; 1620 int len_to_copy, len_copied; 1621 int kern_watermark, ext_watermark; 1622 int ext_sglist_malloced; 1623 int i, j; 1624 1625 ext_sglist_malloced = 0; 1626 ext_sg_start = 0; 1627 ext_offset = 0; 1628 1629 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1630 1631 /* 1632 * If this flag is set, fake the data transfer. 1633 */ 1634 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1635 ctsio->ext_data_filled = ctsio->ext_data_len; 1636 goto bailout; 1637 } 1638 1639 /* 1640 * To simplify things here, if we have a single buffer, stick it in 1641 * a S/G entry and just make it a single entry S/G list. 1642 */ 1643 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1644 int len_seen; 1645 1646 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1647 1648 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1649 M_WAITOK); 1650 ext_sglist_malloced = 1; 1651 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1652 ext_sglen) != 0) { 1653 ctl_set_internal_failure(ctsio, 1654 /*sks_valid*/ 0, 1655 /*retry_count*/ 0); 1656 goto bailout; 1657 } 1658 ext_sg_entries = ctsio->ext_sg_entries; 1659 len_seen = 0; 1660 for (i = 0; i < ext_sg_entries; i++) { 1661 if ((len_seen + ext_sglist[i].len) >= 1662 ctsio->ext_data_filled) { 1663 ext_sg_start = i; 1664 ext_offset = ctsio->ext_data_filled - len_seen; 1665 break; 1666 } 1667 len_seen += ext_sglist[i].len; 1668 } 1669 } else { 1670 ext_sglist = &ext_entry; 1671 ext_sglist->addr = ctsio->ext_data_ptr; 1672 ext_sglist->len = ctsio->ext_data_len; 1673 ext_sg_entries = 1; 1674 ext_sg_start = 0; 1675 ext_offset = ctsio->ext_data_filled; 1676 } 1677 1678 if (ctsio->kern_sg_entries > 0) { 1679 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1680 kern_sg_entries = ctsio->kern_sg_entries; 1681 } else { 1682 kern_sglist = &kern_entry; 1683 kern_sglist->addr = ctsio->kern_data_ptr; 1684 kern_sglist->len = ctsio->kern_data_len; 1685 kern_sg_entries = 1; 1686 } 1687 1688 1689 kern_watermark = 0; 1690 ext_watermark = ext_offset; 1691 len_copied = 0; 1692 for (i = ext_sg_start, j = 0; 1693 i < ext_sg_entries && j < kern_sg_entries;) { 1694 uint8_t *ext_ptr, *kern_ptr; 1695 1696 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1697 kern_sglist[j].len - kern_watermark); 1698 1699 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1700 ext_ptr = ext_ptr + ext_watermark; 1701 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1702 /* 1703 * XXX KDM fix this! 1704 */ 1705 panic("need to implement bus address support"); 1706#if 0 1707 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1708#endif 1709 } else 1710 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1711 kern_ptr = kern_ptr + kern_watermark; 1712 1713 kern_watermark += len_to_copy; 1714 ext_watermark += len_to_copy; 1715 1716 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1717 CTL_FLAG_DATA_IN) { 1718 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1719 "bytes to user\n", len_to_copy)); 1720 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1721 "to %p\n", kern_ptr, ext_ptr)); 1722 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1723 ctl_set_internal_failure(ctsio, 1724 /*sks_valid*/ 0, 1725 /*retry_count*/ 0); 1726 goto bailout; 1727 } 1728 } else { 1729 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1730 "bytes from user\n", len_to_copy)); 1731 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1732 "to %p\n", ext_ptr, kern_ptr)); 1733 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1734 ctl_set_internal_failure(ctsio, 1735 /*sks_valid*/ 0, 1736 /*retry_count*/0); 1737 goto bailout; 1738 } 1739 } 1740 1741 len_copied += len_to_copy; 1742 1743 if (ext_sglist[i].len == ext_watermark) { 1744 i++; 1745 ext_watermark = 0; 1746 } 1747 1748 if (kern_sglist[j].len == kern_watermark) { 1749 j++; 1750 kern_watermark = 0; 1751 } 1752 } 1753 1754 ctsio->ext_data_filled += len_copied; 1755 1756 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1757 "kern_sg_entries: %d\n", ext_sg_entries, 1758 kern_sg_entries)); 1759 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1760 "kern_data_len = %d\n", ctsio->ext_data_len, 1761 ctsio->kern_data_len)); 1762 1763 1764 /* XXX KDM set residual?? */ 1765bailout: 1766 1767 if (ext_sglist_malloced != 0) 1768 free(ext_sglist, M_CTL); 1769 1770 return (CTL_RETVAL_COMPLETE); 1771} 1772 1773/* 1774 * Serialize a command that went down the "wrong" side, and so was sent to 1775 * this controller for execution. The logic is a little different than the 1776 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1777 * sent back to the other side, but in the success case, we execute the 1778 * command on this side (XFER mode) or tell the other side to execute it 1779 * (SER_ONLY mode). 1780 */ 1781static int 1782ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1783{ 1784 struct ctl_softc *ctl_softc; 1785 union ctl_ha_msg msg_info; 1786 struct ctl_lun *lun; 1787 int retval = 0; 1788 uint32_t targ_lun; 1789 1790 ctl_softc = control_softc; 1791 1792 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1793 lun = ctl_softc->ctl_luns[targ_lun]; 1794 if (lun==NULL) 1795 { 1796 /* 1797 * Why isn't LUN defined? The other side wouldn't 1798 * send a cmd if the LUN is undefined. 1799 */ 1800 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1801 1802 /* "Logical unit not supported" */ 1803 ctl_set_sense_data(&msg_info.scsi.sense_data, 1804 lun, 1805 /*sense_format*/SSD_TYPE_NONE, 1806 /*current_error*/ 1, 1807 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1808 /*asc*/ 0x25, 1809 /*ascq*/ 0x00, 1810 SSD_ELEM_NONE); 1811 1812 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1813 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1814 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1815 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1816 msg_info.hdr.serializing_sc = NULL; 1817 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1818 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1819 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1820 } 1821 return(1); 1822 1823 } 1824 1825 mtx_lock(&lun->lun_lock); 1826 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1827 1828 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1829 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1830 ooa_links))) { 1831 case CTL_ACTION_BLOCK: 1832 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1833 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1834 blocked_links); 1835 break; 1836 case CTL_ACTION_PASS: 1837 case CTL_ACTION_SKIP: 1838 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1839 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1840 ctl_enqueue_rtr((union ctl_io *)ctsio); 1841 } else { 1842 1843 /* send msg back to other side */ 1844 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1845 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1846 msg_info.hdr.msg_type = CTL_MSG_R2R; 1847#if 0 1848 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1849#endif 1850 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1851 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1852 } 1853 } 1854 break; 1855 case CTL_ACTION_OVERLAP: 1856 /* OVERLAPPED COMMANDS ATTEMPTED */ 1857 ctl_set_sense_data(&msg_info.scsi.sense_data, 1858 lun, 1859 /*sense_format*/SSD_TYPE_NONE, 1860 /*current_error*/ 1, 1861 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1862 /*asc*/ 0x4E, 1863 /*ascq*/ 0x00, 1864 SSD_ELEM_NONE); 1865 1866 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1867 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1868 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1869 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1870 msg_info.hdr.serializing_sc = NULL; 1871 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1872#if 0 1873 printf("BAD JUJU:Major Bummer Overlap\n"); 1874#endif 1875 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1876 retval = 1; 1877 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1878 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1879 } 1880 break; 1881 case CTL_ACTION_OVERLAP_TAG: 1882 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1883 ctl_set_sense_data(&msg_info.scsi.sense_data, 1884 lun, 1885 /*sense_format*/SSD_TYPE_NONE, 1886 /*current_error*/ 1, 1887 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1888 /*asc*/ 0x4D, 1889 /*ascq*/ ctsio->tag_num & 0xff, 1890 SSD_ELEM_NONE); 1891 1892 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1893 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1894 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1895 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1896 msg_info.hdr.serializing_sc = NULL; 1897 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1898#if 0 1899 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1900#endif 1901 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1902 retval = 1; 1903 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1904 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1905 } 1906 break; 1907 case CTL_ACTION_ERROR: 1908 default: 1909 /* "Internal target failure" */ 1910 ctl_set_sense_data(&msg_info.scsi.sense_data, 1911 lun, 1912 /*sense_format*/SSD_TYPE_NONE, 1913 /*current_error*/ 1, 1914 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1915 /*asc*/ 0x44, 1916 /*ascq*/ 0x00, 1917 SSD_ELEM_NONE); 1918 1919 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1920 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1921 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1922 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1923 msg_info.hdr.serializing_sc = NULL; 1924 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1925#if 0 1926 printf("BAD JUJU:Major Bummer HW Error\n"); 1927#endif 1928 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1929 retval = 1; 1930 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1931 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1932 } 1933 break; 1934 } 1935 mtx_unlock(&lun->lun_lock); 1936 return (retval); 1937} 1938 1939static int 1940ctl_ioctl_submit_wait(union ctl_io *io) 1941{ 1942 struct ctl_fe_ioctl_params params; 1943 ctl_fe_ioctl_state last_state; 1944 int done, retval; 1945 1946 retval = 0; 1947 1948 bzero(¶ms, sizeof(params)); 1949 1950 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1951 cv_init(¶ms.sem, "ctlioccv"); 1952 params.state = CTL_IOCTL_INPROG; 1953 last_state = params.state; 1954 1955 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1956 1957 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1958 1959 /* This shouldn't happen */ 1960 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1961 return (retval); 1962 1963 done = 0; 1964 1965 do { 1966 mtx_lock(¶ms.ioctl_mtx); 1967 /* 1968 * Check the state here, and don't sleep if the state has 1969 * already changed (i.e. wakeup has already occured, but we 1970 * weren't waiting yet). 1971 */ 1972 if (params.state == last_state) { 1973 /* XXX KDM cv_wait_sig instead? */ 1974 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1975 } 1976 last_state = params.state; 1977 1978 switch (params.state) { 1979 case CTL_IOCTL_INPROG: 1980 /* Why did we wake up? */ 1981 /* XXX KDM error here? */ 1982 mtx_unlock(¶ms.ioctl_mtx); 1983 break; 1984 case CTL_IOCTL_DATAMOVE: 1985 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1986 1987 /* 1988 * change last_state back to INPROG to avoid 1989 * deadlock on subsequent data moves. 1990 */ 1991 params.state = last_state = CTL_IOCTL_INPROG; 1992 1993 mtx_unlock(¶ms.ioctl_mtx); 1994 ctl_ioctl_do_datamove(&io->scsiio); 1995 /* 1996 * Note that in some cases, most notably writes, 1997 * this will queue the I/O and call us back later. 1998 * In other cases, generally reads, this routine 1999 * will immediately call back and wake us up, 2000 * probably using our own context. 2001 */ 2002 io->scsiio.be_move_done(io); 2003 break; 2004 case CTL_IOCTL_DONE: 2005 mtx_unlock(¶ms.ioctl_mtx); 2006 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2007 done = 1; 2008 break; 2009 default: 2010 mtx_unlock(¶ms.ioctl_mtx); 2011 /* XXX KDM error here? */ 2012 break; 2013 } 2014 } while (done == 0); 2015 2016 mtx_destroy(¶ms.ioctl_mtx); 2017 cv_destroy(¶ms.sem); 2018 2019 return (CTL_RETVAL_COMPLETE); 2020} 2021 2022static void 2023ctl_ioctl_datamove(union ctl_io *io) 2024{ 2025 struct ctl_fe_ioctl_params *params; 2026 2027 params = (struct ctl_fe_ioctl_params *) 2028 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2029 2030 mtx_lock(¶ms->ioctl_mtx); 2031 params->state = CTL_IOCTL_DATAMOVE; 2032 cv_broadcast(¶ms->sem); 2033 mtx_unlock(¶ms->ioctl_mtx); 2034} 2035 2036static void 2037ctl_ioctl_done(union ctl_io *io) 2038{ 2039 struct ctl_fe_ioctl_params *params; 2040 2041 params = (struct ctl_fe_ioctl_params *) 2042 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2043 2044 mtx_lock(¶ms->ioctl_mtx); 2045 params->state = CTL_IOCTL_DONE; 2046 cv_broadcast(¶ms->sem); 2047 mtx_unlock(¶ms->ioctl_mtx); 2048} 2049 2050static void 2051ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2052{ 2053 struct ctl_fe_ioctl_startstop_info *sd_info; 2054 2055 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2056 2057 sd_info->hs_info.status = metatask->status; 2058 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2059 sd_info->hs_info.luns_complete = 2060 metatask->taskinfo.startstop.luns_complete; 2061 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2062 2063 cv_broadcast(&sd_info->sem); 2064} 2065 2066static void 2067ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2068{ 2069 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2070 2071 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2072 2073 mtx_lock(fe_bbr_info->lock); 2074 fe_bbr_info->bbr_info->status = metatask->status; 2075 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2076 fe_bbr_info->wakeup_done = 1; 2077 mtx_unlock(fe_bbr_info->lock); 2078 2079 cv_broadcast(&fe_bbr_info->sem); 2080} 2081 2082/* 2083 * Returns 0 for success, errno for failure. 2084 */ 2085static int 2086ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2087 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2088{ 2089 union ctl_io *io; 2090 int retval; 2091 2092 retval = 0; 2093 2094 mtx_lock(&lun->lun_lock); 2095 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2096 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2097 ooa_links)) { 2098 struct ctl_ooa_entry *entry; 2099 2100 /* 2101 * If we've got more than we can fit, just count the 2102 * remaining entries. 2103 */ 2104 if (*cur_fill_num >= ooa_hdr->alloc_num) 2105 continue; 2106 2107 entry = &kern_entries[*cur_fill_num]; 2108 2109 entry->tag_num = io->scsiio.tag_num; 2110 entry->lun_num = lun->lun; 2111#ifdef CTL_TIME_IO 2112 entry->start_bt = io->io_hdr.start_bt; 2113#endif 2114 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2115 entry->cdb_len = io->scsiio.cdb_len; 2116 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2117 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2118 2119 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2120 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2121 2122 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2123 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2124 2125 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2126 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2127 2128 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2129 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2130 } 2131 mtx_unlock(&lun->lun_lock); 2132 2133 return (retval); 2134} 2135 2136static void * 2137ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2138 size_t error_str_len) 2139{ 2140 void *kptr; 2141 2142 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2143 2144 if (copyin(user_addr, kptr, len) != 0) { 2145 snprintf(error_str, error_str_len, "Error copying %d bytes " 2146 "from user address %p to kernel address %p", len, 2147 user_addr, kptr); 2148 free(kptr, M_CTL); 2149 return (NULL); 2150 } 2151 2152 return (kptr); 2153} 2154 2155static void 2156ctl_free_args(int num_args, struct ctl_be_arg *args) 2157{ 2158 int i; 2159 2160 if (args == NULL) 2161 return; 2162 2163 for (i = 0; i < num_args; i++) { 2164 free(args[i].kname, M_CTL); 2165 free(args[i].kvalue, M_CTL); 2166 } 2167 2168 free(args, M_CTL); 2169} 2170 2171static struct ctl_be_arg * 2172ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2173 char *error_str, size_t error_str_len) 2174{ 2175 struct ctl_be_arg *args; 2176 int i; 2177 2178 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2179 error_str, error_str_len); 2180 2181 if (args == NULL) 2182 goto bailout; 2183 2184 for (i = 0; i < num_args; i++) { 2185 args[i].kname = NULL; 2186 args[i].kvalue = NULL; 2187 } 2188 2189 for (i = 0; i < num_args; i++) { 2190 uint8_t *tmpptr; 2191 2192 args[i].kname = ctl_copyin_alloc(args[i].name, 2193 args[i].namelen, error_str, error_str_len); 2194 if (args[i].kname == NULL) 2195 goto bailout; 2196 2197 if (args[i].kname[args[i].namelen - 1] != '\0') { 2198 snprintf(error_str, error_str_len, "Argument %d " 2199 "name is not NUL-terminated", i); 2200 goto bailout; 2201 } 2202 2203 if (args[i].flags & CTL_BEARG_RD) { 2204 tmpptr = ctl_copyin_alloc(args[i].value, 2205 args[i].vallen, error_str, error_str_len); 2206 if (tmpptr == NULL) 2207 goto bailout; 2208 if ((args[i].flags & CTL_BEARG_ASCII) 2209 && (tmpptr[args[i].vallen - 1] != '\0')) { 2210 snprintf(error_str, error_str_len, "Argument " 2211 "%d value is not NUL-terminated", i); 2212 goto bailout; 2213 } 2214 args[i].kvalue = tmpptr; 2215 } else { 2216 args[i].kvalue = malloc(args[i].vallen, 2217 M_CTL, M_WAITOK | M_ZERO); 2218 } 2219 } 2220 2221 return (args); 2222bailout: 2223 2224 ctl_free_args(num_args, args); 2225 2226 return (NULL); 2227} 2228 2229static void 2230ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2231{ 2232 int i; 2233 2234 for (i = 0; i < num_args; i++) { 2235 if (args[i].flags & CTL_BEARG_WR) 2236 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2237 } 2238} 2239 2240/* 2241 * Escape characters that are illegal or not recommended in XML. 2242 */ 2243int 2244ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2245{ 2246 char *end = str + size; 2247 int retval; 2248 2249 retval = 0; 2250 2251 for (; *str && str < end; str++) { 2252 switch (*str) { 2253 case '&': 2254 retval = sbuf_printf(sb, "&"); 2255 break; 2256 case '>': 2257 retval = sbuf_printf(sb, ">"); 2258 break; 2259 case '<': 2260 retval = sbuf_printf(sb, "<"); 2261 break; 2262 default: 2263 retval = sbuf_putc(sb, *str); 2264 break; 2265 } 2266 2267 if (retval != 0) 2268 break; 2269 2270 } 2271 2272 return (retval); 2273} 2274 2275static void 2276ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2277{ 2278 struct scsi_vpd_id_descriptor *desc; 2279 int i; 2280 2281 if (id == NULL || id->len < 4) 2282 return; 2283 desc = (struct scsi_vpd_id_descriptor *)id->data; 2284 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2285 case SVPD_ID_TYPE_T10: 2286 sbuf_printf(sb, "t10."); 2287 break; 2288 case SVPD_ID_TYPE_EUI64: 2289 sbuf_printf(sb, "eui."); 2290 break; 2291 case SVPD_ID_TYPE_NAA: 2292 sbuf_printf(sb, "naa."); 2293 break; 2294 case SVPD_ID_TYPE_SCSI_NAME: 2295 break; 2296 } 2297 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2298 case SVPD_ID_CODESET_BINARY: 2299 for (i = 0; i < desc->length; i++) 2300 sbuf_printf(sb, "%02x", desc->identifier[i]); 2301 break; 2302 case SVPD_ID_CODESET_ASCII: 2303 sbuf_printf(sb, "%.*s", (int)desc->length, 2304 (char *)desc->identifier); 2305 break; 2306 case SVPD_ID_CODESET_UTF8: 2307 sbuf_printf(sb, "%s", (char *)desc->identifier); 2308 break; 2309 } 2310} 2311 2312static int 2313ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2314 struct thread *td) 2315{ 2316 struct ctl_softc *softc; 2317 int retval; 2318 2319 softc = control_softc; 2320 2321 retval = 0; 2322 2323 switch (cmd) { 2324 case CTL_IO: { 2325 union ctl_io *io; 2326 void *pool_tmp; 2327 2328 /* 2329 * If we haven't been "enabled", don't allow any SCSI I/O 2330 * to this FETD. 2331 */ 2332 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2333 retval = EPERM; 2334 break; 2335 } 2336 2337 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2338 2339 /* 2340 * Need to save the pool reference so it doesn't get 2341 * spammed by the user's ctl_io. 2342 */ 2343 pool_tmp = io->io_hdr.pool; 2344 memcpy(io, (void *)addr, sizeof(*io)); 2345 io->io_hdr.pool = pool_tmp; 2346 2347 /* 2348 * No status yet, so make sure the status is set properly. 2349 */ 2350 io->io_hdr.status = CTL_STATUS_NONE; 2351 2352 /* 2353 * The user sets the initiator ID, target and LUN IDs. 2354 */ 2355 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2356 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2357 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2358 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2359 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2360 2361 retval = ctl_ioctl_submit_wait(io); 2362 2363 if (retval != 0) { 2364 ctl_free_io(io); 2365 break; 2366 } 2367 2368 memcpy((void *)addr, io, sizeof(*io)); 2369 2370 /* return this to our pool */ 2371 ctl_free_io(io); 2372 2373 break; 2374 } 2375 case CTL_ENABLE_PORT: 2376 case CTL_DISABLE_PORT: 2377 case CTL_SET_PORT_WWNS: { 2378 struct ctl_port *port; 2379 struct ctl_port_entry *entry; 2380 2381 entry = (struct ctl_port_entry *)addr; 2382 2383 mtx_lock(&softc->ctl_lock); 2384 STAILQ_FOREACH(port, &softc->port_list, links) { 2385 int action, done; 2386 2387 action = 0; 2388 done = 0; 2389 2390 if ((entry->port_type == CTL_PORT_NONE) 2391 && (entry->targ_port == port->targ_port)) { 2392 /* 2393 * If the user only wants to enable or 2394 * disable or set WWNs on a specific port, 2395 * do the operation and we're done. 2396 */ 2397 action = 1; 2398 done = 1; 2399 } else if (entry->port_type & port->port_type) { 2400 /* 2401 * Compare the user's type mask with the 2402 * particular frontend type to see if we 2403 * have a match. 2404 */ 2405 action = 1; 2406 done = 0; 2407 2408 /* 2409 * Make sure the user isn't trying to set 2410 * WWNs on multiple ports at the same time. 2411 */ 2412 if (cmd == CTL_SET_PORT_WWNS) { 2413 printf("%s: Can't set WWNs on " 2414 "multiple ports\n", __func__); 2415 retval = EINVAL; 2416 break; 2417 } 2418 } 2419 if (action != 0) { 2420 /* 2421 * XXX KDM we have to drop the lock here, 2422 * because the online/offline operations 2423 * can potentially block. We need to 2424 * reference count the frontends so they 2425 * can't go away, 2426 */ 2427 mtx_unlock(&softc->ctl_lock); 2428 2429 if (cmd == CTL_ENABLE_PORT) { 2430 struct ctl_lun *lun; 2431 2432 STAILQ_FOREACH(lun, &softc->lun_list, 2433 links) { 2434 port->lun_enable(port->targ_lun_arg, 2435 lun->target, 2436 lun->lun); 2437 } 2438 2439 ctl_port_online(port); 2440 } else if (cmd == CTL_DISABLE_PORT) { 2441 struct ctl_lun *lun; 2442 2443 ctl_port_offline(port); 2444 2445 STAILQ_FOREACH(lun, &softc->lun_list, 2446 links) { 2447 port->lun_disable( 2448 port->targ_lun_arg, 2449 lun->target, 2450 lun->lun); 2451 } 2452 } 2453 2454 mtx_lock(&softc->ctl_lock); 2455 2456 if (cmd == CTL_SET_PORT_WWNS) 2457 ctl_port_set_wwns(port, 2458 (entry->flags & CTL_PORT_WWNN_VALID) ? 2459 1 : 0, entry->wwnn, 2460 (entry->flags & CTL_PORT_WWPN_VALID) ? 2461 1 : 0, entry->wwpn); 2462 } 2463 if (done != 0) 2464 break; 2465 } 2466 mtx_unlock(&softc->ctl_lock); 2467 break; 2468 } 2469 case CTL_GET_PORT_LIST: { 2470 struct ctl_port *port; 2471 struct ctl_port_list *list; 2472 int i; 2473 2474 list = (struct ctl_port_list *)addr; 2475 2476 if (list->alloc_len != (list->alloc_num * 2477 sizeof(struct ctl_port_entry))) { 2478 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2479 "alloc_num %u * sizeof(struct ctl_port_entry) " 2480 "%zu\n", __func__, list->alloc_len, 2481 list->alloc_num, sizeof(struct ctl_port_entry)); 2482 retval = EINVAL; 2483 break; 2484 } 2485 list->fill_len = 0; 2486 list->fill_num = 0; 2487 list->dropped_num = 0; 2488 i = 0; 2489 mtx_lock(&softc->ctl_lock); 2490 STAILQ_FOREACH(port, &softc->port_list, links) { 2491 struct ctl_port_entry entry, *list_entry; 2492 2493 if (list->fill_num >= list->alloc_num) { 2494 list->dropped_num++; 2495 continue; 2496 } 2497 2498 entry.port_type = port->port_type; 2499 strlcpy(entry.port_name, port->port_name, 2500 sizeof(entry.port_name)); 2501 entry.targ_port = port->targ_port; 2502 entry.physical_port = port->physical_port; 2503 entry.virtual_port = port->virtual_port; 2504 entry.wwnn = port->wwnn; 2505 entry.wwpn = port->wwpn; 2506 if (port->status & CTL_PORT_STATUS_ONLINE) 2507 entry.online = 1; 2508 else 2509 entry.online = 0; 2510 2511 list_entry = &list->entries[i]; 2512 2513 retval = copyout(&entry, list_entry, sizeof(entry)); 2514 if (retval != 0) { 2515 printf("%s: CTL_GET_PORT_LIST: copyout " 2516 "returned %d\n", __func__, retval); 2517 break; 2518 } 2519 i++; 2520 list->fill_num++; 2521 list->fill_len += sizeof(entry); 2522 } 2523 mtx_unlock(&softc->ctl_lock); 2524 2525 /* 2526 * If this is non-zero, we had a copyout fault, so there's 2527 * probably no point in attempting to set the status inside 2528 * the structure. 2529 */ 2530 if (retval != 0) 2531 break; 2532 2533 if (list->dropped_num > 0) 2534 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2535 else 2536 list->status = CTL_PORT_LIST_OK; 2537 break; 2538 } 2539 case CTL_DUMP_OOA: { 2540 struct ctl_lun *lun; 2541 union ctl_io *io; 2542 char printbuf[128]; 2543 struct sbuf sb; 2544 2545 mtx_lock(&softc->ctl_lock); 2546 printf("Dumping OOA queues:\n"); 2547 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2548 mtx_lock(&lun->lun_lock); 2549 for (io = (union ctl_io *)TAILQ_FIRST( 2550 &lun->ooa_queue); io != NULL; 2551 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2552 ooa_links)) { 2553 sbuf_new(&sb, printbuf, sizeof(printbuf), 2554 SBUF_FIXEDLEN); 2555 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2556 (intmax_t)lun->lun, 2557 io->scsiio.tag_num, 2558 (io->io_hdr.flags & 2559 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2560 (io->io_hdr.flags & 2561 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2562 (io->io_hdr.flags & 2563 CTL_FLAG_ABORT) ? " ABORT" : "", 2564 (io->io_hdr.flags & 2565 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2566 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2567 sbuf_finish(&sb); 2568 printf("%s\n", sbuf_data(&sb)); 2569 } 2570 mtx_unlock(&lun->lun_lock); 2571 } 2572 printf("OOA queues dump done\n"); 2573 mtx_unlock(&softc->ctl_lock); 2574 break; 2575 } 2576 case CTL_GET_OOA: { 2577 struct ctl_lun *lun; 2578 struct ctl_ooa *ooa_hdr; 2579 struct ctl_ooa_entry *entries; 2580 uint32_t cur_fill_num; 2581 2582 ooa_hdr = (struct ctl_ooa *)addr; 2583 2584 if ((ooa_hdr->alloc_len == 0) 2585 || (ooa_hdr->alloc_num == 0)) { 2586 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2587 "must be non-zero\n", __func__, 2588 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2589 retval = EINVAL; 2590 break; 2591 } 2592 2593 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2594 sizeof(struct ctl_ooa_entry))) { 2595 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2596 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2597 __func__, ooa_hdr->alloc_len, 2598 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2599 retval = EINVAL; 2600 break; 2601 } 2602 2603 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2604 if (entries == NULL) { 2605 printf("%s: could not allocate %d bytes for OOA " 2606 "dump\n", __func__, ooa_hdr->alloc_len); 2607 retval = ENOMEM; 2608 break; 2609 } 2610 2611 mtx_lock(&softc->ctl_lock); 2612 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2613 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2614 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2615 mtx_unlock(&softc->ctl_lock); 2616 free(entries, M_CTL); 2617 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2618 __func__, (uintmax_t)ooa_hdr->lun_num); 2619 retval = EINVAL; 2620 break; 2621 } 2622 2623 cur_fill_num = 0; 2624 2625 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2626 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2627 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2628 ooa_hdr, entries); 2629 if (retval != 0) 2630 break; 2631 } 2632 if (retval != 0) { 2633 mtx_unlock(&softc->ctl_lock); 2634 free(entries, M_CTL); 2635 break; 2636 } 2637 } else { 2638 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2639 2640 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2641 entries); 2642 } 2643 mtx_unlock(&softc->ctl_lock); 2644 2645 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2646 ooa_hdr->fill_len = ooa_hdr->fill_num * 2647 sizeof(struct ctl_ooa_entry); 2648 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2649 if (retval != 0) { 2650 printf("%s: error copying out %d bytes for OOA dump\n", 2651 __func__, ooa_hdr->fill_len); 2652 } 2653 2654 getbintime(&ooa_hdr->cur_bt); 2655 2656 if (cur_fill_num > ooa_hdr->alloc_num) { 2657 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2658 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2659 } else { 2660 ooa_hdr->dropped_num = 0; 2661 ooa_hdr->status = CTL_OOA_OK; 2662 } 2663 2664 free(entries, M_CTL); 2665 break; 2666 } 2667 case CTL_CHECK_OOA: { 2668 union ctl_io *io; 2669 struct ctl_lun *lun; 2670 struct ctl_ooa_info *ooa_info; 2671 2672 2673 ooa_info = (struct ctl_ooa_info *)addr; 2674 2675 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2676 ooa_info->status = CTL_OOA_INVALID_LUN; 2677 break; 2678 } 2679 mtx_lock(&softc->ctl_lock); 2680 lun = softc->ctl_luns[ooa_info->lun_id]; 2681 if (lun == NULL) { 2682 mtx_unlock(&softc->ctl_lock); 2683 ooa_info->status = CTL_OOA_INVALID_LUN; 2684 break; 2685 } 2686 mtx_lock(&lun->lun_lock); 2687 mtx_unlock(&softc->ctl_lock); 2688 ooa_info->num_entries = 0; 2689 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2690 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2691 &io->io_hdr, ooa_links)) { 2692 ooa_info->num_entries++; 2693 } 2694 mtx_unlock(&lun->lun_lock); 2695 2696 ooa_info->status = CTL_OOA_SUCCESS; 2697 2698 break; 2699 } 2700 case CTL_HARD_START: 2701 case CTL_HARD_STOP: { 2702 struct ctl_fe_ioctl_startstop_info ss_info; 2703 struct cfi_metatask *metatask; 2704 struct mtx hs_mtx; 2705 2706 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2707 2708 cv_init(&ss_info.sem, "hard start/stop cv" ); 2709 2710 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2711 if (metatask == NULL) { 2712 retval = ENOMEM; 2713 mtx_destroy(&hs_mtx); 2714 break; 2715 } 2716 2717 if (cmd == CTL_HARD_START) 2718 metatask->tasktype = CFI_TASK_STARTUP; 2719 else 2720 metatask->tasktype = CFI_TASK_SHUTDOWN; 2721 2722 metatask->callback = ctl_ioctl_hard_startstop_callback; 2723 metatask->callback_arg = &ss_info; 2724 2725 cfi_action(metatask); 2726 2727 /* Wait for the callback */ 2728 mtx_lock(&hs_mtx); 2729 cv_wait_sig(&ss_info.sem, &hs_mtx); 2730 mtx_unlock(&hs_mtx); 2731 2732 /* 2733 * All information has been copied from the metatask by the 2734 * time cv_broadcast() is called, so we free the metatask here. 2735 */ 2736 cfi_free_metatask(metatask); 2737 2738 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2739 2740 mtx_destroy(&hs_mtx); 2741 break; 2742 } 2743 case CTL_BBRREAD: { 2744 struct ctl_bbrread_info *bbr_info; 2745 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2746 struct mtx bbr_mtx; 2747 struct cfi_metatask *metatask; 2748 2749 bbr_info = (struct ctl_bbrread_info *)addr; 2750 2751 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2752 2753 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2754 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2755 2756 fe_bbr_info.bbr_info = bbr_info; 2757 fe_bbr_info.lock = &bbr_mtx; 2758 2759 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2760 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2761 2762 if (metatask == NULL) { 2763 mtx_destroy(&bbr_mtx); 2764 cv_destroy(&fe_bbr_info.sem); 2765 retval = ENOMEM; 2766 break; 2767 } 2768 metatask->tasktype = CFI_TASK_BBRREAD; 2769 metatask->callback = ctl_ioctl_bbrread_callback; 2770 metatask->callback_arg = &fe_bbr_info; 2771 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2772 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2773 metatask->taskinfo.bbrread.len = bbr_info->len; 2774 2775 cfi_action(metatask); 2776 2777 mtx_lock(&bbr_mtx); 2778 while (fe_bbr_info.wakeup_done == 0) 2779 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2780 mtx_unlock(&bbr_mtx); 2781 2782 bbr_info->status = metatask->status; 2783 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2784 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2785 memcpy(&bbr_info->sense_data, 2786 &metatask->taskinfo.bbrread.sense_data, 2787 ctl_min(sizeof(bbr_info->sense_data), 2788 sizeof(metatask->taskinfo.bbrread.sense_data))); 2789 2790 cfi_free_metatask(metatask); 2791 2792 mtx_destroy(&bbr_mtx); 2793 cv_destroy(&fe_bbr_info.sem); 2794 2795 break; 2796 } 2797 case CTL_DELAY_IO: { 2798 struct ctl_io_delay_info *delay_info; 2799#ifdef CTL_IO_DELAY 2800 struct ctl_lun *lun; 2801#endif /* CTL_IO_DELAY */ 2802 2803 delay_info = (struct ctl_io_delay_info *)addr; 2804 2805#ifdef CTL_IO_DELAY 2806 mtx_lock(&softc->ctl_lock); 2807 2808 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2809 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2810 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2811 } else { 2812 lun = softc->ctl_luns[delay_info->lun_id]; 2813 mtx_lock(&lun->lun_lock); 2814 2815 delay_info->status = CTL_DELAY_STATUS_OK; 2816 2817 switch (delay_info->delay_type) { 2818 case CTL_DELAY_TYPE_CONT: 2819 break; 2820 case CTL_DELAY_TYPE_ONESHOT: 2821 break; 2822 default: 2823 delay_info->status = 2824 CTL_DELAY_STATUS_INVALID_TYPE; 2825 break; 2826 } 2827 2828 switch (delay_info->delay_loc) { 2829 case CTL_DELAY_LOC_DATAMOVE: 2830 lun->delay_info.datamove_type = 2831 delay_info->delay_type; 2832 lun->delay_info.datamove_delay = 2833 delay_info->delay_secs; 2834 break; 2835 case CTL_DELAY_LOC_DONE: 2836 lun->delay_info.done_type = 2837 delay_info->delay_type; 2838 lun->delay_info.done_delay = 2839 delay_info->delay_secs; 2840 break; 2841 default: 2842 delay_info->status = 2843 CTL_DELAY_STATUS_INVALID_LOC; 2844 break; 2845 } 2846 mtx_unlock(&lun->lun_lock); 2847 } 2848 2849 mtx_unlock(&softc->ctl_lock); 2850#else 2851 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2852#endif /* CTL_IO_DELAY */ 2853 break; 2854 } 2855 case CTL_REALSYNC_SET: { 2856 int *syncstate; 2857 2858 syncstate = (int *)addr; 2859 2860 mtx_lock(&softc->ctl_lock); 2861 switch (*syncstate) { 2862 case 0: 2863 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2864 break; 2865 case 1: 2866 softc->flags |= CTL_FLAG_REAL_SYNC; 2867 break; 2868 default: 2869 retval = EINVAL; 2870 break; 2871 } 2872 mtx_unlock(&softc->ctl_lock); 2873 break; 2874 } 2875 case CTL_REALSYNC_GET: { 2876 int *syncstate; 2877 2878 syncstate = (int*)addr; 2879 2880 mtx_lock(&softc->ctl_lock); 2881 if (softc->flags & CTL_FLAG_REAL_SYNC) 2882 *syncstate = 1; 2883 else 2884 *syncstate = 0; 2885 mtx_unlock(&softc->ctl_lock); 2886 2887 break; 2888 } 2889 case CTL_SETSYNC: 2890 case CTL_GETSYNC: { 2891 struct ctl_sync_info *sync_info; 2892 struct ctl_lun *lun; 2893 2894 sync_info = (struct ctl_sync_info *)addr; 2895 2896 mtx_lock(&softc->ctl_lock); 2897 lun = softc->ctl_luns[sync_info->lun_id]; 2898 if (lun == NULL) { 2899 mtx_unlock(&softc->ctl_lock); 2900 sync_info->status = CTL_GS_SYNC_NO_LUN; 2901 } 2902 /* 2903 * Get or set the sync interval. We're not bounds checking 2904 * in the set case, hopefully the user won't do something 2905 * silly. 2906 */ 2907 mtx_lock(&lun->lun_lock); 2908 mtx_unlock(&softc->ctl_lock); 2909 if (cmd == CTL_GETSYNC) 2910 sync_info->sync_interval = lun->sync_interval; 2911 else 2912 lun->sync_interval = sync_info->sync_interval; 2913 mtx_unlock(&lun->lun_lock); 2914 2915 sync_info->status = CTL_GS_SYNC_OK; 2916 2917 break; 2918 } 2919 case CTL_GETSTATS: { 2920 struct ctl_stats *stats; 2921 struct ctl_lun *lun; 2922 int i; 2923 2924 stats = (struct ctl_stats *)addr; 2925 2926 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2927 stats->alloc_len) { 2928 stats->status = CTL_SS_NEED_MORE_SPACE; 2929 stats->num_luns = softc->num_luns; 2930 break; 2931 } 2932 /* 2933 * XXX KDM no locking here. If the LUN list changes, 2934 * things can blow up. 2935 */ 2936 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2937 i++, lun = STAILQ_NEXT(lun, links)) { 2938 retval = copyout(&lun->stats, &stats->lun_stats[i], 2939 sizeof(lun->stats)); 2940 if (retval != 0) 2941 break; 2942 } 2943 stats->num_luns = softc->num_luns; 2944 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2945 softc->num_luns; 2946 stats->status = CTL_SS_OK; 2947#ifdef CTL_TIME_IO 2948 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2949#else 2950 stats->flags = CTL_STATS_FLAG_NONE; 2951#endif 2952 getnanouptime(&stats->timestamp); 2953 break; 2954 } 2955 case CTL_ERROR_INJECT: { 2956 struct ctl_error_desc *err_desc, *new_err_desc; 2957 struct ctl_lun *lun; 2958 2959 err_desc = (struct ctl_error_desc *)addr; 2960 2961 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2962 M_WAITOK | M_ZERO); 2963 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2964 2965 mtx_lock(&softc->ctl_lock); 2966 lun = softc->ctl_luns[err_desc->lun_id]; 2967 if (lun == NULL) { 2968 mtx_unlock(&softc->ctl_lock); 2969 free(new_err_desc, M_CTL); 2970 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2971 __func__, (uintmax_t)err_desc->lun_id); 2972 retval = EINVAL; 2973 break; 2974 } 2975 mtx_lock(&lun->lun_lock); 2976 mtx_unlock(&softc->ctl_lock); 2977 2978 /* 2979 * We could do some checking here to verify the validity 2980 * of the request, but given the complexity of error 2981 * injection requests, the checking logic would be fairly 2982 * complex. 2983 * 2984 * For now, if the request is invalid, it just won't get 2985 * executed and might get deleted. 2986 */ 2987 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2988 2989 /* 2990 * XXX KDM check to make sure the serial number is unique, 2991 * in case we somehow manage to wrap. That shouldn't 2992 * happen for a very long time, but it's the right thing to 2993 * do. 2994 */ 2995 new_err_desc->serial = lun->error_serial; 2996 err_desc->serial = lun->error_serial; 2997 lun->error_serial++; 2998 2999 mtx_unlock(&lun->lun_lock); 3000 break; 3001 } 3002 case CTL_ERROR_INJECT_DELETE: { 3003 struct ctl_error_desc *delete_desc, *desc, *desc2; 3004 struct ctl_lun *lun; 3005 int delete_done; 3006 3007 delete_desc = (struct ctl_error_desc *)addr; 3008 delete_done = 0; 3009 3010 mtx_lock(&softc->ctl_lock); 3011 lun = softc->ctl_luns[delete_desc->lun_id]; 3012 if (lun == NULL) { 3013 mtx_unlock(&softc->ctl_lock); 3014 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3015 __func__, (uintmax_t)delete_desc->lun_id); 3016 retval = EINVAL; 3017 break; 3018 } 3019 mtx_lock(&lun->lun_lock); 3020 mtx_unlock(&softc->ctl_lock); 3021 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3022 if (desc->serial != delete_desc->serial) 3023 continue; 3024 3025 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3026 links); 3027 free(desc, M_CTL); 3028 delete_done = 1; 3029 } 3030 mtx_unlock(&lun->lun_lock); 3031 if (delete_done == 0) { 3032 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3033 "error serial %ju on LUN %u\n", __func__, 3034 delete_desc->serial, delete_desc->lun_id); 3035 retval = EINVAL; 3036 break; 3037 } 3038 break; 3039 } 3040 case CTL_DUMP_STRUCTS: { 3041 int i, j, k; 3042 struct ctl_port *port; 3043 struct ctl_frontend *fe; 3044 3045 mtx_lock(&softc->ctl_lock); 3046 printf("CTL Persistent Reservation information start:\n"); 3047 for (i = 0; i < CTL_MAX_LUNS; i++) { 3048 struct ctl_lun *lun; 3049 3050 lun = softc->ctl_luns[i]; 3051 3052 if ((lun == NULL) 3053 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3054 continue; 3055 3056 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3057 if (lun->pr_keys[j] == NULL) 3058 continue; 3059 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3060 if (lun->pr_keys[j][k] == 0) 3061 continue; 3062 printf(" LUN %d port %d iid %d key " 3063 "%#jx\n", i, j, k, 3064 (uintmax_t)lun->pr_keys[j][k]); 3065 } 3066 } 3067 } 3068 printf("CTL Persistent Reservation information end\n"); 3069 printf("CTL Ports:\n"); 3070 STAILQ_FOREACH(port, &softc->port_list, links) { 3071 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3072 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3073 port->frontend->name, port->port_type, 3074 port->physical_port, port->virtual_port, 3075 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3076 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3077 if (port->wwpn_iid[j].in_use == 0 && 3078 port->wwpn_iid[j].wwpn == 0 && 3079 port->wwpn_iid[j].name == NULL) 3080 continue; 3081 3082 printf(" iid %u use %d WWPN %#jx '%s'\n", 3083 j, port->wwpn_iid[j].in_use, 3084 (uintmax_t)port->wwpn_iid[j].wwpn, 3085 port->wwpn_iid[j].name); 3086 } 3087 } 3088 printf("CTL Port information end\n"); 3089 mtx_unlock(&softc->ctl_lock); 3090 /* 3091 * XXX KDM calling this without a lock. We'd likely want 3092 * to drop the lock before calling the frontend's dump 3093 * routine anyway. 3094 */ 3095 printf("CTL Frontends:\n"); 3096 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3097 printf(" Frontend '%s'\n", fe->name); 3098 if (fe->fe_dump != NULL) 3099 fe->fe_dump(); 3100 } 3101 printf("CTL Frontend information end\n"); 3102 break; 3103 } 3104 case CTL_LUN_REQ: { 3105 struct ctl_lun_req *lun_req; 3106 struct ctl_backend_driver *backend; 3107 3108 lun_req = (struct ctl_lun_req *)addr; 3109 3110 backend = ctl_backend_find(lun_req->backend); 3111 if (backend == NULL) { 3112 lun_req->status = CTL_LUN_ERROR; 3113 snprintf(lun_req->error_str, 3114 sizeof(lun_req->error_str), 3115 "Backend \"%s\" not found.", 3116 lun_req->backend); 3117 break; 3118 } 3119 if (lun_req->num_be_args > 0) { 3120 lun_req->kern_be_args = ctl_copyin_args( 3121 lun_req->num_be_args, 3122 lun_req->be_args, 3123 lun_req->error_str, 3124 sizeof(lun_req->error_str)); 3125 if (lun_req->kern_be_args == NULL) { 3126 lun_req->status = CTL_LUN_ERROR; 3127 break; 3128 } 3129 } 3130 3131 retval = backend->ioctl(dev, cmd, addr, flag, td); 3132 3133 if (lun_req->num_be_args > 0) { 3134 ctl_copyout_args(lun_req->num_be_args, 3135 lun_req->kern_be_args); 3136 ctl_free_args(lun_req->num_be_args, 3137 lun_req->kern_be_args); 3138 } 3139 break; 3140 } 3141 case CTL_LUN_LIST: { 3142 struct sbuf *sb; 3143 struct ctl_lun *lun; 3144 struct ctl_lun_list *list; 3145 struct ctl_option *opt; 3146 3147 list = (struct ctl_lun_list *)addr; 3148 3149 /* 3150 * Allocate a fixed length sbuf here, based on the length 3151 * of the user's buffer. We could allocate an auto-extending 3152 * buffer, and then tell the user how much larger our 3153 * amount of data is than his buffer, but that presents 3154 * some problems: 3155 * 3156 * 1. The sbuf(9) routines use a blocking malloc, and so 3157 * we can't hold a lock while calling them with an 3158 * auto-extending buffer. 3159 * 3160 * 2. There is not currently a LUN reference counting 3161 * mechanism, outside of outstanding transactions on 3162 * the LUN's OOA queue. So a LUN could go away on us 3163 * while we're getting the LUN number, backend-specific 3164 * information, etc. Thus, given the way things 3165 * currently work, we need to hold the CTL lock while 3166 * grabbing LUN information. 3167 * 3168 * So, from the user's standpoint, the best thing to do is 3169 * allocate what he thinks is a reasonable buffer length, 3170 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3171 * double the buffer length and try again. (And repeat 3172 * that until he succeeds.) 3173 */ 3174 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3175 if (sb == NULL) { 3176 list->status = CTL_LUN_LIST_ERROR; 3177 snprintf(list->error_str, sizeof(list->error_str), 3178 "Unable to allocate %d bytes for LUN list", 3179 list->alloc_len); 3180 break; 3181 } 3182 3183 sbuf_printf(sb, "<ctllunlist>\n"); 3184 3185 mtx_lock(&softc->ctl_lock); 3186 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3187 mtx_lock(&lun->lun_lock); 3188 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3189 (uintmax_t)lun->lun); 3190 3191 /* 3192 * Bail out as soon as we see that we've overfilled 3193 * the buffer. 3194 */ 3195 if (retval != 0) 3196 break; 3197 3198 retval = sbuf_printf(sb, "\t<backend_type>%s" 3199 "</backend_type>\n", 3200 (lun->backend == NULL) ? "none" : 3201 lun->backend->name); 3202 3203 if (retval != 0) 3204 break; 3205 3206 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3207 lun->be_lun->lun_type); 3208 3209 if (retval != 0) 3210 break; 3211 3212 if (lun->backend == NULL) { 3213 retval = sbuf_printf(sb, "</lun>\n"); 3214 if (retval != 0) 3215 break; 3216 continue; 3217 } 3218 3219 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3220 (lun->be_lun->maxlba > 0) ? 3221 lun->be_lun->maxlba + 1 : 0); 3222 3223 if (retval != 0) 3224 break; 3225 3226 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3227 lun->be_lun->blocksize); 3228 3229 if (retval != 0) 3230 break; 3231 3232 retval = sbuf_printf(sb, "\t<serial_number>"); 3233 3234 if (retval != 0) 3235 break; 3236 3237 retval = ctl_sbuf_printf_esc(sb, 3238 lun->be_lun->serial_num, 3239 sizeof(lun->be_lun->serial_num)); 3240 3241 if (retval != 0) 3242 break; 3243 3244 retval = sbuf_printf(sb, "</serial_number>\n"); 3245 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<device_id>"); 3250 3251 if (retval != 0) 3252 break; 3253 3254 retval = ctl_sbuf_printf_esc(sb, 3255 lun->be_lun->device_id, 3256 sizeof(lun->be_lun->device_id)); 3257 3258 if (retval != 0) 3259 break; 3260 3261 retval = sbuf_printf(sb, "</device_id>\n"); 3262 3263 if (retval != 0) 3264 break; 3265 3266 if (lun->backend->lun_info != NULL) { 3267 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3268 if (retval != 0) 3269 break; 3270 } 3271 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3272 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3273 opt->name, opt->value, opt->name); 3274 if (retval != 0) 3275 break; 3276 } 3277 3278 retval = sbuf_printf(sb, "</lun>\n"); 3279 3280 if (retval != 0) 3281 break; 3282 mtx_unlock(&lun->lun_lock); 3283 } 3284 if (lun != NULL) 3285 mtx_unlock(&lun->lun_lock); 3286 mtx_unlock(&softc->ctl_lock); 3287 3288 if ((retval != 0) 3289 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3290 retval = 0; 3291 sbuf_delete(sb); 3292 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3293 snprintf(list->error_str, sizeof(list->error_str), 3294 "Out of space, %d bytes is too small", 3295 list->alloc_len); 3296 break; 3297 } 3298 3299 sbuf_finish(sb); 3300 3301 retval = copyout(sbuf_data(sb), list->lun_xml, 3302 sbuf_len(sb) + 1); 3303 3304 list->fill_len = sbuf_len(sb) + 1; 3305 list->status = CTL_LUN_LIST_OK; 3306 sbuf_delete(sb); 3307 break; 3308 } 3309 case CTL_ISCSI: { 3310 struct ctl_iscsi *ci; 3311 struct ctl_frontend *fe; 3312 3313 ci = (struct ctl_iscsi *)addr; 3314 3315 fe = ctl_frontend_find("iscsi"); 3316 if (fe == NULL) { 3317 ci->status = CTL_ISCSI_ERROR; 3318 snprintf(ci->error_str, sizeof(ci->error_str), 3319 "Frontend \"iscsi\" not found."); 3320 break; 3321 } 3322 3323 retval = fe->ioctl(dev, cmd, addr, flag, td); 3324 break; 3325 } 3326 case CTL_PORT_REQ: { 3327 struct ctl_req *req; 3328 struct ctl_frontend *fe; 3329 3330 req = (struct ctl_req *)addr; 3331 3332 fe = ctl_frontend_find(req->driver); 3333 if (fe == NULL) { 3334 req->status = CTL_LUN_ERROR; 3335 snprintf(req->error_str, sizeof(req->error_str), 3336 "Frontend \"%s\" not found.", req->driver); 3337 break; 3338 } 3339 if (req->num_args > 0) { 3340 req->kern_args = ctl_copyin_args(req->num_args, 3341 req->args, req->error_str, sizeof(req->error_str)); 3342 if (req->kern_args == NULL) { 3343 req->status = CTL_LUN_ERROR; 3344 break; 3345 } 3346 } 3347 3348 retval = fe->ioctl(dev, cmd, addr, flag, td); 3349 3350 if (req->num_args > 0) { 3351 ctl_copyout_args(req->num_args, req->kern_args); 3352 ctl_free_args(req->num_args, req->kern_args); 3353 } 3354 break; 3355 } 3356 case CTL_PORT_LIST: { 3357 struct sbuf *sb; 3358 struct ctl_port *port; 3359 struct ctl_lun_list *list; 3360 struct ctl_option *opt; 3361 int j; 3362 3363 list = (struct ctl_lun_list *)addr; 3364 3365 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3366 if (sb == NULL) { 3367 list->status = CTL_LUN_LIST_ERROR; 3368 snprintf(list->error_str, sizeof(list->error_str), 3369 "Unable to allocate %d bytes for LUN list", 3370 list->alloc_len); 3371 break; 3372 } 3373 3374 sbuf_printf(sb, "<ctlportlist>\n"); 3375 3376 mtx_lock(&softc->ctl_lock); 3377 STAILQ_FOREACH(port, &softc->port_list, links) { 3378 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3379 (uintmax_t)port->targ_port); 3380 3381 /* 3382 * Bail out as soon as we see that we've overfilled 3383 * the buffer. 3384 */ 3385 if (retval != 0) 3386 break; 3387 3388 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3389 "</frontend_type>\n", port->frontend->name); 3390 if (retval != 0) 3391 break; 3392 3393 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3394 port->port_type); 3395 if (retval != 0) 3396 break; 3397 3398 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3399 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3400 if (retval != 0) 3401 break; 3402 3403 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3404 port->port_name); 3405 if (retval != 0) 3406 break; 3407 3408 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3409 port->physical_port); 3410 if (retval != 0) 3411 break; 3412 3413 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3414 port->virtual_port); 3415 if (retval != 0) 3416 break; 3417 3418 if (port->target_devid != NULL) { 3419 sbuf_printf(sb, "\t<target>"); 3420 ctl_id_sbuf(port->target_devid, sb); 3421 sbuf_printf(sb, "</target>\n"); 3422 } 3423 3424 if (port->port_devid != NULL) { 3425 sbuf_printf(sb, "\t<port>"); 3426 ctl_id_sbuf(port->port_devid, sb); 3427 sbuf_printf(sb, "</port>\n"); 3428 } 3429 3430 if (port->port_info != NULL) { 3431 retval = port->port_info(port->onoff_arg, sb); 3432 if (retval != 0) 3433 break; 3434 } 3435 STAILQ_FOREACH(opt, &port->options, links) { 3436 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3437 opt->name, opt->value, opt->name); 3438 if (retval != 0) 3439 break; 3440 } 3441 3442 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3443 if (port->wwpn_iid[j].in_use == 0 || 3444 (port->wwpn_iid[j].wwpn == 0 && 3445 port->wwpn_iid[j].name == NULL)) 3446 continue; 3447 3448 if (port->wwpn_iid[j].name != NULL) 3449 retval = sbuf_printf(sb, 3450 "\t<initiator>%u %s</initiator>\n", 3451 j, port->wwpn_iid[j].name); 3452 else 3453 retval = sbuf_printf(sb, 3454 "\t<initiator>%u naa.%08jx</initiator>\n", 3455 j, port->wwpn_iid[j].wwpn); 3456 if (retval != 0) 3457 break; 3458 } 3459 if (retval != 0) 3460 break; 3461 3462 retval = sbuf_printf(sb, "</targ_port>\n"); 3463 if (retval != 0) 3464 break; 3465 } 3466 mtx_unlock(&softc->ctl_lock); 3467 3468 if ((retval != 0) 3469 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3470 retval = 0; 3471 sbuf_delete(sb); 3472 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3473 snprintf(list->error_str, sizeof(list->error_str), 3474 "Out of space, %d bytes is too small", 3475 list->alloc_len); 3476 break; 3477 } 3478 3479 sbuf_finish(sb); 3480 3481 retval = copyout(sbuf_data(sb), list->lun_xml, 3482 sbuf_len(sb) + 1); 3483 3484 list->fill_len = sbuf_len(sb) + 1; 3485 list->status = CTL_LUN_LIST_OK; 3486 sbuf_delete(sb); 3487 break; 3488 } 3489 default: { 3490 /* XXX KDM should we fix this? */ 3491#if 0 3492 struct ctl_backend_driver *backend; 3493 unsigned int type; 3494 int found; 3495 3496 found = 0; 3497 3498 /* 3499 * We encode the backend type as the ioctl type for backend 3500 * ioctls. So parse it out here, and then search for a 3501 * backend of this type. 3502 */ 3503 type = _IOC_TYPE(cmd); 3504 3505 STAILQ_FOREACH(backend, &softc->be_list, links) { 3506 if (backend->type == type) { 3507 found = 1; 3508 break; 3509 } 3510 } 3511 if (found == 0) { 3512 printf("ctl: unknown ioctl command %#lx or backend " 3513 "%d\n", cmd, type); 3514 retval = EINVAL; 3515 break; 3516 } 3517 retval = backend->ioctl(dev, cmd, addr, flag, td); 3518#endif 3519 retval = ENOTTY; 3520 break; 3521 } 3522 } 3523 return (retval); 3524} 3525 3526uint32_t 3527ctl_get_initindex(struct ctl_nexus *nexus) 3528{ 3529 if (nexus->targ_port < CTL_MAX_PORTS) 3530 return (nexus->initid.id + 3531 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3532 else 3533 return (nexus->initid.id + 3534 ((nexus->targ_port - CTL_MAX_PORTS) * 3535 CTL_MAX_INIT_PER_PORT)); 3536} 3537 3538uint32_t 3539ctl_get_resindex(struct ctl_nexus *nexus) 3540{ 3541 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3542} 3543 3544uint32_t 3545ctl_port_idx(int port_num) 3546{ 3547 if (port_num < CTL_MAX_PORTS) 3548 return(port_num); 3549 else 3550 return(port_num - CTL_MAX_PORTS); 3551} 3552 3553static uint32_t 3554ctl_map_lun(int port_num, uint32_t lun_id) 3555{ 3556 struct ctl_port *port; 3557 3558 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3559 if (port == NULL) 3560 return (UINT32_MAX); 3561 if (port->lun_map == NULL) 3562 return (lun_id); 3563 return (port->lun_map(port->targ_lun_arg, lun_id)); 3564} 3565 3566static uint32_t 3567ctl_map_lun_back(int port_num, uint32_t lun_id) 3568{ 3569 struct ctl_port *port; 3570 uint32_t i; 3571 3572 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3573 if (port->lun_map == NULL) 3574 return (lun_id); 3575 for (i = 0; i < CTL_MAX_LUNS; i++) { 3576 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3577 return (i); 3578 } 3579 return (UINT32_MAX); 3580} 3581 3582/* 3583 * Note: This only works for bitmask sizes that are at least 32 bits, and 3584 * that are a power of 2. 3585 */ 3586int 3587ctl_ffz(uint32_t *mask, uint32_t size) 3588{ 3589 uint32_t num_chunks, num_pieces; 3590 int i, j; 3591 3592 num_chunks = (size >> 5); 3593 if (num_chunks == 0) 3594 num_chunks++; 3595 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3596 3597 for (i = 0; i < num_chunks; i++) { 3598 for (j = 0; j < num_pieces; j++) { 3599 if ((mask[i] & (1 << j)) == 0) 3600 return ((i << 5) + j); 3601 } 3602 } 3603 3604 return (-1); 3605} 3606 3607int 3608ctl_set_mask(uint32_t *mask, uint32_t bit) 3609{ 3610 uint32_t chunk, piece; 3611 3612 chunk = bit >> 5; 3613 piece = bit % (sizeof(uint32_t) * 8); 3614 3615 if ((mask[chunk] & (1 << piece)) != 0) 3616 return (-1); 3617 else 3618 mask[chunk] |= (1 << piece); 3619 3620 return (0); 3621} 3622 3623int 3624ctl_clear_mask(uint32_t *mask, uint32_t bit) 3625{ 3626 uint32_t chunk, piece; 3627 3628 chunk = bit >> 5; 3629 piece = bit % (sizeof(uint32_t) * 8); 3630 3631 if ((mask[chunk] & (1 << piece)) == 0) 3632 return (-1); 3633 else 3634 mask[chunk] &= ~(1 << piece); 3635 3636 return (0); 3637} 3638 3639int 3640ctl_is_set(uint32_t *mask, uint32_t bit) 3641{ 3642 uint32_t chunk, piece; 3643 3644 chunk = bit >> 5; 3645 piece = bit % (sizeof(uint32_t) * 8); 3646 3647 if ((mask[chunk] & (1 << piece)) == 0) 3648 return (0); 3649 else 3650 return (1); 3651} 3652 3653static uint64_t 3654ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3655{ 3656 uint64_t *t; 3657 3658 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3659 if (t == NULL) 3660 return (0); 3661 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3662} 3663 3664static void 3665ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3666{ 3667 uint64_t *t; 3668 3669 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3670 if (t == NULL) 3671 return; 3672 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3673} 3674 3675static void 3676ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3677{ 3678 uint64_t *p; 3679 u_int i; 3680 3681 i = residx/CTL_MAX_INIT_PER_PORT; 3682 if (lun->pr_keys[i] != NULL) 3683 return; 3684 mtx_unlock(&lun->lun_lock); 3685 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3686 M_WAITOK | M_ZERO); 3687 mtx_lock(&lun->lun_lock); 3688 if (lun->pr_keys[i] == NULL) 3689 lun->pr_keys[i] = p; 3690 else 3691 free(p, M_CTL); 3692} 3693 3694static void 3695ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3696{ 3697 uint64_t *t; 3698 3699 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3700 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3701 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3702} 3703 3704#ifdef unused 3705/* 3706 * The bus, target and lun are optional, they can be filled in later. 3707 * can_wait is used to determine whether we can wait on the malloc or not. 3708 */ 3709union ctl_io* 3710ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3711 uint32_t targ_lun, int can_wait) 3712{ 3713 union ctl_io *io; 3714 3715 if (can_wait) 3716 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3717 else 3718 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3719 3720 if (io != NULL) { 3721 io->io_hdr.io_type = io_type; 3722 io->io_hdr.targ_port = targ_port; 3723 /* 3724 * XXX KDM this needs to change/go away. We need to move 3725 * to a preallocated pool of ctl_scsiio structures. 3726 */ 3727 io->io_hdr.nexus.targ_target.id = targ_target; 3728 io->io_hdr.nexus.targ_lun = targ_lun; 3729 } 3730 3731 return (io); 3732} 3733 3734void 3735ctl_kfree_io(union ctl_io *io) 3736{ 3737 free(io, M_CTL); 3738} 3739#endif /* unused */ 3740 3741/* 3742 * ctl_softc, pool_name, total_ctl_io are passed in. 3743 * npool is passed out. 3744 */ 3745int 3746ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3747 uint32_t total_ctl_io, void **npool) 3748{ 3749#ifdef IO_POOLS 3750 struct ctl_io_pool *pool; 3751 3752 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3753 M_NOWAIT | M_ZERO); 3754 if (pool == NULL) 3755 return (ENOMEM); 3756 3757 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3758 pool->ctl_softc = ctl_softc; 3759 pool->zone = uma_zsecond_create(pool->name, NULL, 3760 NULL, NULL, NULL, ctl_softc->io_zone); 3761 /* uma_prealloc(pool->zone, total_ctl_io); */ 3762 3763 *npool = pool; 3764#else 3765 *npool = ctl_softc->io_zone; 3766#endif 3767 return (0); 3768} 3769 3770void 3771ctl_pool_free(struct ctl_io_pool *pool) 3772{ 3773 3774 if (pool == NULL) 3775 return; 3776 3777#ifdef IO_POOLS 3778 uma_zdestroy(pool->zone); 3779 free(pool, M_CTL); 3780#endif 3781} 3782 3783union ctl_io * 3784ctl_alloc_io(void *pool_ref) 3785{ 3786 union ctl_io *io; 3787#ifdef IO_POOLS 3788 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3789 3790 io = uma_zalloc(pool->zone, M_WAITOK); 3791#else 3792 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3793#endif 3794 if (io != NULL) 3795 io->io_hdr.pool = pool_ref; 3796 return (io); 3797} 3798 3799union ctl_io * 3800ctl_alloc_io_nowait(void *pool_ref) 3801{ 3802 union ctl_io *io; 3803#ifdef IO_POOLS 3804 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3805 3806 io = uma_zalloc(pool->zone, M_NOWAIT); 3807#else 3808 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3809#endif 3810 if (io != NULL) 3811 io->io_hdr.pool = pool_ref; 3812 return (io); 3813} 3814 3815void 3816ctl_free_io(union ctl_io *io) 3817{ 3818#ifdef IO_POOLS 3819 struct ctl_io_pool *pool; 3820#endif 3821 3822 if (io == NULL) 3823 return; 3824 3825#ifdef IO_POOLS 3826 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3827 uma_zfree(pool->zone, io); 3828#else 3829 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3830#endif 3831} 3832 3833void 3834ctl_zero_io(union ctl_io *io) 3835{ 3836 void *pool_ref; 3837 3838 if (io == NULL) 3839 return; 3840 3841 /* 3842 * May need to preserve linked list pointers at some point too. 3843 */ 3844 pool_ref = io->io_hdr.pool; 3845 memset(io, 0, sizeof(*io)); 3846 io->io_hdr.pool = pool_ref; 3847} 3848 3849/* 3850 * This routine is currently used for internal copies of ctl_ios that need 3851 * to persist for some reason after we've already returned status to the 3852 * FETD. (Thus the flag set.) 3853 * 3854 * XXX XXX 3855 * Note that this makes a blind copy of all fields in the ctl_io, except 3856 * for the pool reference. This includes any memory that has been 3857 * allocated! That memory will no longer be valid after done has been 3858 * called, so this would be VERY DANGEROUS for command that actually does 3859 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3860 * start and stop commands, which don't transfer any data, so this is not a 3861 * problem. If it is used for anything else, the caller would also need to 3862 * allocate data buffer space and this routine would need to be modified to 3863 * copy the data buffer(s) as well. 3864 */ 3865void 3866ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3867{ 3868 void *pool_ref; 3869 3870 if ((src == NULL) 3871 || (dest == NULL)) 3872 return; 3873 3874 /* 3875 * May need to preserve linked list pointers at some point too. 3876 */ 3877 pool_ref = dest->io_hdr.pool; 3878 3879 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3880 3881 dest->io_hdr.pool = pool_ref; 3882 /* 3883 * We need to know that this is an internal copy, and doesn't need 3884 * to get passed back to the FETD that allocated it. 3885 */ 3886 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3887} 3888 3889static int 3890ctl_expand_number(const char *buf, uint64_t *num) 3891{ 3892 char *endptr; 3893 uint64_t number; 3894 unsigned shift; 3895 3896 number = strtoq(buf, &endptr, 0); 3897 3898 switch (tolower((unsigned char)*endptr)) { 3899 case 'e': 3900 shift = 60; 3901 break; 3902 case 'p': 3903 shift = 50; 3904 break; 3905 case 't': 3906 shift = 40; 3907 break; 3908 case 'g': 3909 shift = 30; 3910 break; 3911 case 'm': 3912 shift = 20; 3913 break; 3914 case 'k': 3915 shift = 10; 3916 break; 3917 case 'b': 3918 case '\0': /* No unit. */ 3919 *num = number; 3920 return (0); 3921 default: 3922 /* Unrecognized unit. */ 3923 return (-1); 3924 } 3925 3926 if ((number << shift) >> shift != number) { 3927 /* Overflow */ 3928 return (-1); 3929 } 3930 *num = number << shift; 3931 return (0); 3932} 3933 3934 3935/* 3936 * This routine could be used in the future to load default and/or saved 3937 * mode page parameters for a particuar lun. 3938 */ 3939static int 3940ctl_init_page_index(struct ctl_lun *lun) 3941{ 3942 int i; 3943 struct ctl_page_index *page_index; 3944 const char *value; 3945 uint64_t ival; 3946 3947 memcpy(&lun->mode_pages.index, page_index_template, 3948 sizeof(page_index_template)); 3949 3950 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3951 3952 page_index = &lun->mode_pages.index[i]; 3953 /* 3954 * If this is a disk-only mode page, there's no point in 3955 * setting it up. For some pages, we have to have some 3956 * basic information about the disk in order to calculate the 3957 * mode page data. 3958 */ 3959 if ((lun->be_lun->lun_type != T_DIRECT) 3960 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3961 continue; 3962 3963 switch (page_index->page_code & SMPH_PC_MASK) { 3964 case SMS_RW_ERROR_RECOVERY_PAGE: { 3965 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3966 panic("subpage is incorrect!"); 3967 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3968 &rw_er_page_default, 3969 sizeof(rw_er_page_default)); 3970 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3971 &rw_er_page_changeable, 3972 sizeof(rw_er_page_changeable)); 3973 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3974 &rw_er_page_default, 3975 sizeof(rw_er_page_default)); 3976 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3977 &rw_er_page_default, 3978 sizeof(rw_er_page_default)); 3979 page_index->page_data = 3980 (uint8_t *)lun->mode_pages.rw_er_page; 3981 break; 3982 } 3983 case SMS_FORMAT_DEVICE_PAGE: { 3984 struct scsi_format_page *format_page; 3985 3986 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3987 panic("subpage is incorrect!"); 3988 3989 /* 3990 * Sectors per track are set above. Bytes per 3991 * sector need to be set here on a per-LUN basis. 3992 */ 3993 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3994 &format_page_default, 3995 sizeof(format_page_default)); 3996 memcpy(&lun->mode_pages.format_page[ 3997 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3998 sizeof(format_page_changeable)); 3999 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4000 &format_page_default, 4001 sizeof(format_page_default)); 4002 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4003 &format_page_default, 4004 sizeof(format_page_default)); 4005 4006 format_page = &lun->mode_pages.format_page[ 4007 CTL_PAGE_CURRENT]; 4008 scsi_ulto2b(lun->be_lun->blocksize, 4009 format_page->bytes_per_sector); 4010 4011 format_page = &lun->mode_pages.format_page[ 4012 CTL_PAGE_DEFAULT]; 4013 scsi_ulto2b(lun->be_lun->blocksize, 4014 format_page->bytes_per_sector); 4015 4016 format_page = &lun->mode_pages.format_page[ 4017 CTL_PAGE_SAVED]; 4018 scsi_ulto2b(lun->be_lun->blocksize, 4019 format_page->bytes_per_sector); 4020 4021 page_index->page_data = 4022 (uint8_t *)lun->mode_pages.format_page; 4023 break; 4024 } 4025 case SMS_RIGID_DISK_PAGE: { 4026 struct scsi_rigid_disk_page *rigid_disk_page; 4027 uint32_t sectors_per_cylinder; 4028 uint64_t cylinders; 4029#ifndef __XSCALE__ 4030 int shift; 4031#endif /* !__XSCALE__ */ 4032 4033 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4034 panic("invalid subpage value %d", 4035 page_index->subpage); 4036 4037 /* 4038 * Rotation rate and sectors per track are set 4039 * above. We calculate the cylinders here based on 4040 * capacity. Due to the number of heads and 4041 * sectors per track we're using, smaller arrays 4042 * may turn out to have 0 cylinders. Linux and 4043 * FreeBSD don't pay attention to these mode pages 4044 * to figure out capacity, but Solaris does. It 4045 * seems to deal with 0 cylinders just fine, and 4046 * works out a fake geometry based on the capacity. 4047 */ 4048 memcpy(&lun->mode_pages.rigid_disk_page[ 4049 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4050 sizeof(rigid_disk_page_default)); 4051 memcpy(&lun->mode_pages.rigid_disk_page[ 4052 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4053 sizeof(rigid_disk_page_changeable)); 4054 4055 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4056 CTL_DEFAULT_HEADS; 4057 4058 /* 4059 * The divide method here will be more accurate, 4060 * probably, but results in floating point being 4061 * used in the kernel on i386 (__udivdi3()). On the 4062 * XScale, though, __udivdi3() is implemented in 4063 * software. 4064 * 4065 * The shift method for cylinder calculation is 4066 * accurate if sectors_per_cylinder is a power of 4067 * 2. Otherwise it might be slightly off -- you 4068 * might have a bit of a truncation problem. 4069 */ 4070#ifdef __XSCALE__ 4071 cylinders = (lun->be_lun->maxlba + 1) / 4072 sectors_per_cylinder; 4073#else 4074 for (shift = 31; shift > 0; shift--) { 4075 if (sectors_per_cylinder & (1 << shift)) 4076 break; 4077 } 4078 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4079#endif 4080 4081 /* 4082 * We've basically got 3 bytes, or 24 bits for the 4083 * cylinder size in the mode page. If we're over, 4084 * just round down to 2^24. 4085 */ 4086 if (cylinders > 0xffffff) 4087 cylinders = 0xffffff; 4088 4089 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4090 CTL_PAGE_DEFAULT]; 4091 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4092 4093 if ((value = ctl_get_opt(&lun->be_lun->options, 4094 "rpm")) != NULL) { 4095 scsi_ulto2b(strtol(value, NULL, 0), 4096 rigid_disk_page->rotation_rate); 4097 } 4098 4099 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4100 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4101 sizeof(rigid_disk_page_default)); 4102 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4103 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4104 sizeof(rigid_disk_page_default)); 4105 4106 page_index->page_data = 4107 (uint8_t *)lun->mode_pages.rigid_disk_page; 4108 break; 4109 } 4110 case SMS_CACHING_PAGE: { 4111 struct scsi_caching_page *caching_page; 4112 4113 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4114 panic("invalid subpage value %d", 4115 page_index->subpage); 4116 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4117 &caching_page_default, 4118 sizeof(caching_page_default)); 4119 memcpy(&lun->mode_pages.caching_page[ 4120 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4121 sizeof(caching_page_changeable)); 4122 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4123 &caching_page_default, 4124 sizeof(caching_page_default)); 4125 caching_page = &lun->mode_pages.caching_page[ 4126 CTL_PAGE_SAVED]; 4127 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4128 if (value != NULL && strcmp(value, "off") == 0) 4129 caching_page->flags1 &= ~SCP_WCE; 4130 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4131 if (value != NULL && strcmp(value, "off") == 0) 4132 caching_page->flags1 |= SCP_RCD; 4133 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4134 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4135 sizeof(caching_page_default)); 4136 page_index->page_data = 4137 (uint8_t *)lun->mode_pages.caching_page; 4138 break; 4139 } 4140 case SMS_CONTROL_MODE_PAGE: { 4141 struct scsi_control_page *control_page; 4142 4143 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4144 panic("invalid subpage value %d", 4145 page_index->subpage); 4146 4147 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4148 &control_page_default, 4149 sizeof(control_page_default)); 4150 memcpy(&lun->mode_pages.control_page[ 4151 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4152 sizeof(control_page_changeable)); 4153 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4154 &control_page_default, 4155 sizeof(control_page_default)); 4156 control_page = &lun->mode_pages.control_page[ 4157 CTL_PAGE_SAVED]; 4158 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4159 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4160 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4161 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4162 } 4163 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4164 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4165 sizeof(control_page_default)); 4166 page_index->page_data = 4167 (uint8_t *)lun->mode_pages.control_page; 4168 break; 4169 4170 } 4171 case SMS_INFO_EXCEPTIONS_PAGE: { 4172 switch (page_index->subpage) { 4173 case SMS_SUBPAGE_PAGE_0: 4174 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4175 &ie_page_default, 4176 sizeof(ie_page_default)); 4177 memcpy(&lun->mode_pages.ie_page[ 4178 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4179 sizeof(ie_page_changeable)); 4180 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4181 &ie_page_default, 4182 sizeof(ie_page_default)); 4183 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4184 &ie_page_default, 4185 sizeof(ie_page_default)); 4186 page_index->page_data = 4187 (uint8_t *)lun->mode_pages.ie_page; 4188 break; 4189 case 0x02: { 4190 struct ctl_logical_block_provisioning_page *page; 4191 4192 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4193 &lbp_page_default, 4194 sizeof(lbp_page_default)); 4195 memcpy(&lun->mode_pages.lbp_page[ 4196 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4197 sizeof(lbp_page_changeable)); 4198 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4199 &lbp_page_default, 4200 sizeof(lbp_page_default)); 4201 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4202 value = ctl_get_opt(&lun->be_lun->options, 4203 "avail-threshold"); 4204 if (value != NULL && 4205 ctl_expand_number(value, &ival) == 0) { 4206 page->descr[0].flags |= SLBPPD_ENABLED | 4207 SLBPPD_ARMING_DEC; 4208 if (lun->be_lun->blocksize) 4209 ival /= lun->be_lun->blocksize; 4210 else 4211 ival /= 512; 4212 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4213 page->descr[0].count); 4214 } 4215 value = ctl_get_opt(&lun->be_lun->options, 4216 "used-threshold"); 4217 if (value != NULL && 4218 ctl_expand_number(value, &ival) == 0) { 4219 page->descr[1].flags |= SLBPPD_ENABLED | 4220 SLBPPD_ARMING_INC; 4221 if (lun->be_lun->blocksize) 4222 ival /= lun->be_lun->blocksize; 4223 else 4224 ival /= 512; 4225 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4226 page->descr[1].count); 4227 } 4228 value = ctl_get_opt(&lun->be_lun->options, 4229 "pool-avail-threshold"); 4230 if (value != NULL && 4231 ctl_expand_number(value, &ival) == 0) { 4232 page->descr[2].flags |= SLBPPD_ENABLED | 4233 SLBPPD_ARMING_DEC; 4234 if (lun->be_lun->blocksize) 4235 ival /= lun->be_lun->blocksize; 4236 else 4237 ival /= 512; 4238 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4239 page->descr[2].count); 4240 } 4241 value = ctl_get_opt(&lun->be_lun->options, 4242 "pool-used-threshold"); 4243 if (value != NULL && 4244 ctl_expand_number(value, &ival) == 0) { 4245 page->descr[3].flags |= SLBPPD_ENABLED | 4246 SLBPPD_ARMING_INC; 4247 if (lun->be_lun->blocksize) 4248 ival /= lun->be_lun->blocksize; 4249 else 4250 ival /= 512; 4251 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4252 page->descr[3].count); 4253 } 4254 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4255 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4256 sizeof(lbp_page_default)); 4257 page_index->page_data = 4258 (uint8_t *)lun->mode_pages.lbp_page; 4259 }} 4260 break; 4261 } 4262 case SMS_VENDOR_SPECIFIC_PAGE:{ 4263 switch (page_index->subpage) { 4264 case DBGCNF_SUBPAGE_CODE: { 4265 struct copan_debugconf_subpage *current_page, 4266 *saved_page; 4267 4268 memcpy(&lun->mode_pages.debugconf_subpage[ 4269 CTL_PAGE_CURRENT], 4270 &debugconf_page_default, 4271 sizeof(debugconf_page_default)); 4272 memcpy(&lun->mode_pages.debugconf_subpage[ 4273 CTL_PAGE_CHANGEABLE], 4274 &debugconf_page_changeable, 4275 sizeof(debugconf_page_changeable)); 4276 memcpy(&lun->mode_pages.debugconf_subpage[ 4277 CTL_PAGE_DEFAULT], 4278 &debugconf_page_default, 4279 sizeof(debugconf_page_default)); 4280 memcpy(&lun->mode_pages.debugconf_subpage[ 4281 CTL_PAGE_SAVED], 4282 &debugconf_page_default, 4283 sizeof(debugconf_page_default)); 4284 page_index->page_data = 4285 (uint8_t *)lun->mode_pages.debugconf_subpage; 4286 4287 current_page = (struct copan_debugconf_subpage *) 4288 (page_index->page_data + 4289 (page_index->page_len * 4290 CTL_PAGE_CURRENT)); 4291 saved_page = (struct copan_debugconf_subpage *) 4292 (page_index->page_data + 4293 (page_index->page_len * 4294 CTL_PAGE_SAVED)); 4295 break; 4296 } 4297 default: 4298 panic("invalid subpage value %d", 4299 page_index->subpage); 4300 break; 4301 } 4302 break; 4303 } 4304 default: 4305 panic("invalid page value %d", 4306 page_index->page_code & SMPH_PC_MASK); 4307 break; 4308 } 4309 } 4310 4311 return (CTL_RETVAL_COMPLETE); 4312} 4313 4314static int 4315ctl_init_log_page_index(struct ctl_lun *lun) 4316{ 4317 struct ctl_page_index *page_index; 4318 int i, j, k, prev; 4319 4320 memcpy(&lun->log_pages.index, log_page_index_template, 4321 sizeof(log_page_index_template)); 4322 4323 prev = -1; 4324 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4325 4326 page_index = &lun->log_pages.index[i]; 4327 /* 4328 * If this is a disk-only mode page, there's no point in 4329 * setting it up. For some pages, we have to have some 4330 * basic information about the disk in order to calculate the 4331 * mode page data. 4332 */ 4333 if ((lun->be_lun->lun_type != T_DIRECT) 4334 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4335 continue; 4336 4337 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4338 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 4339 lun->backend->lun_attr == NULL)) 4340 continue; 4341 4342 if (page_index->page_code != prev) { 4343 lun->log_pages.pages_page[j] = page_index->page_code; 4344 prev = page_index->page_code; 4345 j++; 4346 } 4347 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4348 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4349 k++; 4350 } 4351 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4352 lun->log_pages.index[0].page_len = j; 4353 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4354 lun->log_pages.index[1].page_len = k * 2; 4355 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4356 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4357 4358 return (CTL_RETVAL_COMPLETE); 4359} 4360 4361static int 4362hex2bin(const char *str, uint8_t *buf, int buf_size) 4363{ 4364 int i; 4365 u_char c; 4366 4367 memset(buf, 0, buf_size); 4368 while (isspace(str[0])) 4369 str++; 4370 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4371 str += 2; 4372 buf_size *= 2; 4373 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4374 c = str[i]; 4375 if (isdigit(c)) 4376 c -= '0'; 4377 else if (isalpha(c)) 4378 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4379 else 4380 break; 4381 if (c >= 16) 4382 break; 4383 if ((i & 1) == 0) 4384 buf[i / 2] |= (c << 4); 4385 else 4386 buf[i / 2] |= c; 4387 } 4388 return ((i + 1) / 2); 4389} 4390 4391/* 4392 * LUN allocation. 4393 * 4394 * Requirements: 4395 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4396 * wants us to allocate the LUN and he can block. 4397 * - ctl_softc is always set 4398 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4399 * 4400 * Returns 0 for success, non-zero (errno) for failure. 4401 */ 4402static int 4403ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4404 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4405{ 4406 struct ctl_lun *nlun, *lun; 4407 struct ctl_port *port; 4408 struct scsi_vpd_id_descriptor *desc; 4409 struct scsi_vpd_id_t10 *t10id; 4410 const char *eui, *naa, *scsiname, *vendor, *value; 4411 int lun_number, i, lun_malloced; 4412 int devidlen, idlen1, idlen2 = 0, len; 4413 4414 if (be_lun == NULL) 4415 return (EINVAL); 4416 4417 /* 4418 * We currently only support Direct Access or Processor LUN types. 4419 */ 4420 switch (be_lun->lun_type) { 4421 case T_DIRECT: 4422 break; 4423 case T_PROCESSOR: 4424 break; 4425 case T_SEQUENTIAL: 4426 case T_CHANGER: 4427 default: 4428 be_lun->lun_config_status(be_lun->be_lun, 4429 CTL_LUN_CONFIG_FAILURE); 4430 break; 4431 } 4432 if (ctl_lun == NULL) { 4433 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4434 lun_malloced = 1; 4435 } else { 4436 lun_malloced = 0; 4437 lun = ctl_lun; 4438 } 4439 4440 memset(lun, 0, sizeof(*lun)); 4441 if (lun_malloced) 4442 lun->flags = CTL_LUN_MALLOCED; 4443 4444 /* Generate LUN ID. */ 4445 devidlen = max(CTL_DEVID_MIN_LEN, 4446 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4447 idlen1 = sizeof(*t10id) + devidlen; 4448 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4449 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4450 if (scsiname != NULL) { 4451 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4452 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4453 } 4454 eui = ctl_get_opt(&be_lun->options, "eui"); 4455 if (eui != NULL) { 4456 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4457 } 4458 naa = ctl_get_opt(&be_lun->options, "naa"); 4459 if (naa != NULL) { 4460 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4461 } 4462 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4463 M_CTL, M_WAITOK | M_ZERO); 4464 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4465 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4466 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4467 desc->length = idlen1; 4468 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4469 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4470 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4471 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4472 } else { 4473 strncpy(t10id->vendor, vendor, 4474 min(sizeof(t10id->vendor), strlen(vendor))); 4475 } 4476 strncpy((char *)t10id->vendor_spec_id, 4477 (char *)be_lun->device_id, devidlen); 4478 if (scsiname != NULL) { 4479 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4480 desc->length); 4481 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4482 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4483 SVPD_ID_TYPE_SCSI_NAME; 4484 desc->length = idlen2; 4485 strlcpy(desc->identifier, scsiname, idlen2); 4486 } 4487 if (eui != NULL) { 4488 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4489 desc->length); 4490 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4491 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4492 SVPD_ID_TYPE_EUI64; 4493 desc->length = hex2bin(eui, desc->identifier, 16); 4494 desc->length = desc->length > 12 ? 16 : 4495 (desc->length > 8 ? 12 : 8); 4496 len -= 16 - desc->length; 4497 } 4498 if (naa != NULL) { 4499 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4500 desc->length); 4501 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4502 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4503 SVPD_ID_TYPE_NAA; 4504 desc->length = hex2bin(naa, desc->identifier, 16); 4505 desc->length = desc->length > 8 ? 16 : 8; 4506 len -= 16 - desc->length; 4507 } 4508 lun->lun_devid->len = len; 4509 4510 mtx_lock(&ctl_softc->ctl_lock); 4511 /* 4512 * See if the caller requested a particular LUN number. If so, see 4513 * if it is available. Otherwise, allocate the first available LUN. 4514 */ 4515 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4516 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4517 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4518 mtx_unlock(&ctl_softc->ctl_lock); 4519 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4520 printf("ctl: requested LUN ID %d is higher " 4521 "than CTL_MAX_LUNS - 1 (%d)\n", 4522 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4523 } else { 4524 /* 4525 * XXX KDM return an error, or just assign 4526 * another LUN ID in this case?? 4527 */ 4528 printf("ctl: requested LUN ID %d is already " 4529 "in use\n", be_lun->req_lun_id); 4530 } 4531 if (lun->flags & CTL_LUN_MALLOCED) 4532 free(lun, M_CTL); 4533 be_lun->lun_config_status(be_lun->be_lun, 4534 CTL_LUN_CONFIG_FAILURE); 4535 return (ENOSPC); 4536 } 4537 lun_number = be_lun->req_lun_id; 4538 } else { 4539 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4540 if (lun_number == -1) { 4541 mtx_unlock(&ctl_softc->ctl_lock); 4542 printf("ctl: can't allocate LUN on target %ju, out of " 4543 "LUNs\n", (uintmax_t)target_id.id); 4544 if (lun->flags & CTL_LUN_MALLOCED) 4545 free(lun, M_CTL); 4546 be_lun->lun_config_status(be_lun->be_lun, 4547 CTL_LUN_CONFIG_FAILURE); 4548 return (ENOSPC); 4549 } 4550 } 4551 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4552 4553 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4554 lun->target = target_id; 4555 lun->lun = lun_number; 4556 lun->be_lun = be_lun; 4557 /* 4558 * The processor LUN is always enabled. Disk LUNs come on line 4559 * disabled, and must be enabled by the backend. 4560 */ 4561 lun->flags |= CTL_LUN_DISABLED; 4562 lun->backend = be_lun->be; 4563 be_lun->ctl_lun = lun; 4564 be_lun->lun_id = lun_number; 4565 atomic_add_int(&be_lun->be->num_luns, 1); 4566 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4567 lun->flags |= CTL_LUN_OFFLINE; 4568 4569 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4570 lun->flags |= CTL_LUN_STOPPED; 4571 4572 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4573 lun->flags |= CTL_LUN_INOPERABLE; 4574 4575 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4576 lun->flags |= CTL_LUN_PRIMARY_SC; 4577 4578 value = ctl_get_opt(&be_lun->options, "readonly"); 4579 if (value != NULL && strcmp(value, "on") == 0) 4580 lun->flags |= CTL_LUN_READONLY; 4581 4582 lun->ctl_softc = ctl_softc; 4583 TAILQ_INIT(&lun->ooa_queue); 4584 TAILQ_INIT(&lun->blocked_queue); 4585 STAILQ_INIT(&lun->error_list); 4586 ctl_tpc_lun_init(lun); 4587 4588 /* 4589 * Initialize the mode and log page index. 4590 */ 4591 ctl_init_page_index(lun); 4592 ctl_init_log_page_index(lun); 4593 4594 /* 4595 * Set the poweron UA for all initiators on this LUN only. 4596 */ 4597 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4598 lun->pending_ua[i] = CTL_UA_POWERON; 4599 4600 /* 4601 * Now, before we insert this lun on the lun list, set the lun 4602 * inventory changed UA for all other luns. 4603 */ 4604 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4605 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4606 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4607 } 4608 } 4609 4610 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4611 4612 ctl_softc->ctl_luns[lun_number] = lun; 4613 4614 ctl_softc->num_luns++; 4615 4616 /* Setup statistics gathering */ 4617 lun->stats.device_type = be_lun->lun_type; 4618 lun->stats.lun_number = lun_number; 4619 if (lun->stats.device_type == T_DIRECT) 4620 lun->stats.blocksize = be_lun->blocksize; 4621 else 4622 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4623 for (i = 0;i < CTL_MAX_PORTS;i++) 4624 lun->stats.ports[i].targ_port = i; 4625 4626 mtx_unlock(&ctl_softc->ctl_lock); 4627 4628 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4629 4630 /* 4631 * Run through each registered FETD and bring it online if it isn't 4632 * already. Enable the target ID if it hasn't been enabled, and 4633 * enable this particular LUN. 4634 */ 4635 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4636 int retval; 4637 4638 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4639 if (retval != 0) { 4640 printf("ctl_alloc_lun: FETD %s port %d returned error " 4641 "%d for lun_enable on target %ju lun %d\n", 4642 port->port_name, port->targ_port, retval, 4643 (uintmax_t)target_id.id, lun_number); 4644 } else 4645 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4646 } 4647 return (0); 4648} 4649 4650/* 4651 * Delete a LUN. 4652 * Assumptions: 4653 * - LUN has already been marked invalid and any pending I/O has been taken 4654 * care of. 4655 */ 4656static int 4657ctl_free_lun(struct ctl_lun *lun) 4658{ 4659 struct ctl_softc *softc; 4660#if 0 4661 struct ctl_port *port; 4662#endif 4663 struct ctl_lun *nlun; 4664 int i; 4665 4666 softc = lun->ctl_softc; 4667 4668 mtx_assert(&softc->ctl_lock, MA_OWNED); 4669 4670 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4671 4672 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4673 4674 softc->ctl_luns[lun->lun] = NULL; 4675 4676 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4677 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4678 4679 softc->num_luns--; 4680 4681 /* 4682 * XXX KDM this scheme only works for a single target/multiple LUN 4683 * setup. It needs to be revamped for a multiple target scheme. 4684 * 4685 * XXX KDM this results in port->lun_disable() getting called twice, 4686 * once when ctl_disable_lun() is called, and a second time here. 4687 * We really need to re-think the LUN disable semantics. There 4688 * should probably be several steps/levels to LUN removal: 4689 * - disable 4690 * - invalidate 4691 * - free 4692 * 4693 * Right now we only have a disable method when communicating to 4694 * the front end ports, at least for individual LUNs. 4695 */ 4696#if 0 4697 STAILQ_FOREACH(port, &softc->port_list, links) { 4698 int retval; 4699 4700 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4701 lun->lun); 4702 if (retval != 0) { 4703 printf("ctl_free_lun: FETD %s port %d returned error " 4704 "%d for lun_disable on target %ju lun %jd\n", 4705 port->port_name, port->targ_port, retval, 4706 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4707 } 4708 4709 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4710 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4711 4712 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4713 if (retval != 0) { 4714 printf("ctl_free_lun: FETD %s port %d " 4715 "returned error %d for targ_disable on " 4716 "target %ju\n", port->port_name, 4717 port->targ_port, retval, 4718 (uintmax_t)lun->target.id); 4719 } else 4720 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4721 4722 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4723 continue; 4724 4725#if 0 4726 port->port_offline(port->onoff_arg); 4727 port->status &= ~CTL_PORT_STATUS_ONLINE; 4728#endif 4729 } 4730 } 4731#endif 4732 4733 /* 4734 * Tell the backend to free resources, if this LUN has a backend. 4735 */ 4736 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4737 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4738 4739 ctl_tpc_lun_shutdown(lun); 4740 mtx_destroy(&lun->lun_lock); 4741 free(lun->lun_devid, M_CTL); 4742 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) { 4743 if (lun->pr_keys[i] != NULL) 4744 free(lun->pr_keys[i], M_CTL); 4745 } 4746 free(lun->write_buffer, M_CTL); 4747 if (lun->flags & CTL_LUN_MALLOCED) 4748 free(lun, M_CTL); 4749 4750 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4751 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4752 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4753 } 4754 } 4755 4756 return (0); 4757} 4758 4759static void 4760ctl_create_lun(struct ctl_be_lun *be_lun) 4761{ 4762 struct ctl_softc *ctl_softc; 4763 4764 ctl_softc = control_softc; 4765 4766 /* 4767 * ctl_alloc_lun() should handle all potential failure cases. 4768 */ 4769 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4770} 4771 4772int 4773ctl_add_lun(struct ctl_be_lun *be_lun) 4774{ 4775 struct ctl_softc *ctl_softc = control_softc; 4776 4777 mtx_lock(&ctl_softc->ctl_lock); 4778 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4779 mtx_unlock(&ctl_softc->ctl_lock); 4780 wakeup(&ctl_softc->pending_lun_queue); 4781 4782 return (0); 4783} 4784 4785int 4786ctl_enable_lun(struct ctl_be_lun *be_lun) 4787{ 4788 struct ctl_softc *ctl_softc; 4789 struct ctl_port *port, *nport; 4790 struct ctl_lun *lun; 4791 int retval; 4792 4793 ctl_softc = control_softc; 4794 4795 lun = (struct ctl_lun *)be_lun->ctl_lun; 4796 4797 mtx_lock(&ctl_softc->ctl_lock); 4798 mtx_lock(&lun->lun_lock); 4799 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4800 /* 4801 * eh? Why did we get called if the LUN is already 4802 * enabled? 4803 */ 4804 mtx_unlock(&lun->lun_lock); 4805 mtx_unlock(&ctl_softc->ctl_lock); 4806 return (0); 4807 } 4808 lun->flags &= ~CTL_LUN_DISABLED; 4809 mtx_unlock(&lun->lun_lock); 4810 4811 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4812 nport = STAILQ_NEXT(port, links); 4813 4814 /* 4815 * Drop the lock while we call the FETD's enable routine. 4816 * This can lead to a callback into CTL (at least in the 4817 * case of the internal initiator frontend. 4818 */ 4819 mtx_unlock(&ctl_softc->ctl_lock); 4820 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4821 mtx_lock(&ctl_softc->ctl_lock); 4822 if (retval != 0) { 4823 printf("%s: FETD %s port %d returned error " 4824 "%d for lun_enable on target %ju lun %jd\n", 4825 __func__, port->port_name, port->targ_port, retval, 4826 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4827 } 4828#if 0 4829 else { 4830 /* NOTE: TODO: why does lun enable affect port status? */ 4831 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4832 } 4833#endif 4834 } 4835 4836 mtx_unlock(&ctl_softc->ctl_lock); 4837 4838 return (0); 4839} 4840 4841int 4842ctl_disable_lun(struct ctl_be_lun *be_lun) 4843{ 4844 struct ctl_softc *ctl_softc; 4845 struct ctl_port *port; 4846 struct ctl_lun *lun; 4847 int retval; 4848 4849 ctl_softc = control_softc; 4850 4851 lun = (struct ctl_lun *)be_lun->ctl_lun; 4852 4853 mtx_lock(&ctl_softc->ctl_lock); 4854 mtx_lock(&lun->lun_lock); 4855 if (lun->flags & CTL_LUN_DISABLED) { 4856 mtx_unlock(&lun->lun_lock); 4857 mtx_unlock(&ctl_softc->ctl_lock); 4858 return (0); 4859 } 4860 lun->flags |= CTL_LUN_DISABLED; 4861 mtx_unlock(&lun->lun_lock); 4862 4863 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4864 mtx_unlock(&ctl_softc->ctl_lock); 4865 /* 4866 * Drop the lock before we call the frontend's disable 4867 * routine, to avoid lock order reversals. 4868 * 4869 * XXX KDM what happens if the frontend list changes while 4870 * we're traversing it? It's unlikely, but should be handled. 4871 */ 4872 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4873 lun->lun); 4874 mtx_lock(&ctl_softc->ctl_lock); 4875 if (retval != 0) { 4876 printf("ctl_alloc_lun: FETD %s port %d returned error " 4877 "%d for lun_disable on target %ju lun %jd\n", 4878 port->port_name, port->targ_port, retval, 4879 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4880 } 4881 } 4882 4883 mtx_unlock(&ctl_softc->ctl_lock); 4884 4885 return (0); 4886} 4887 4888int 4889ctl_start_lun(struct ctl_be_lun *be_lun) 4890{ 4891 struct ctl_softc *ctl_softc; 4892 struct ctl_lun *lun; 4893 4894 ctl_softc = control_softc; 4895 4896 lun = (struct ctl_lun *)be_lun->ctl_lun; 4897 4898 mtx_lock(&lun->lun_lock); 4899 lun->flags &= ~CTL_LUN_STOPPED; 4900 mtx_unlock(&lun->lun_lock); 4901 4902 return (0); 4903} 4904 4905int 4906ctl_stop_lun(struct ctl_be_lun *be_lun) 4907{ 4908 struct ctl_softc *ctl_softc; 4909 struct ctl_lun *lun; 4910 4911 ctl_softc = control_softc; 4912 4913 lun = (struct ctl_lun *)be_lun->ctl_lun; 4914 4915 mtx_lock(&lun->lun_lock); 4916 lun->flags |= CTL_LUN_STOPPED; 4917 mtx_unlock(&lun->lun_lock); 4918 4919 return (0); 4920} 4921 4922int 4923ctl_lun_offline(struct ctl_be_lun *be_lun) 4924{ 4925 struct ctl_softc *ctl_softc; 4926 struct ctl_lun *lun; 4927 4928 ctl_softc = control_softc; 4929 4930 lun = (struct ctl_lun *)be_lun->ctl_lun; 4931 4932 mtx_lock(&lun->lun_lock); 4933 lun->flags |= CTL_LUN_OFFLINE; 4934 mtx_unlock(&lun->lun_lock); 4935 4936 return (0); 4937} 4938 4939int 4940ctl_lun_online(struct ctl_be_lun *be_lun) 4941{ 4942 struct ctl_softc *ctl_softc; 4943 struct ctl_lun *lun; 4944 4945 ctl_softc = control_softc; 4946 4947 lun = (struct ctl_lun *)be_lun->ctl_lun; 4948 4949 mtx_lock(&lun->lun_lock); 4950 lun->flags &= ~CTL_LUN_OFFLINE; 4951 mtx_unlock(&lun->lun_lock); 4952 4953 return (0); 4954} 4955 4956int 4957ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4958{ 4959 struct ctl_softc *ctl_softc; 4960 struct ctl_lun *lun; 4961 4962 ctl_softc = control_softc; 4963 4964 lun = (struct ctl_lun *)be_lun->ctl_lun; 4965 4966 mtx_lock(&lun->lun_lock); 4967 4968 /* 4969 * The LUN needs to be disabled before it can be marked invalid. 4970 */ 4971 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4972 mtx_unlock(&lun->lun_lock); 4973 return (-1); 4974 } 4975 /* 4976 * Mark the LUN invalid. 4977 */ 4978 lun->flags |= CTL_LUN_INVALID; 4979 4980 /* 4981 * If there is nothing in the OOA queue, go ahead and free the LUN. 4982 * If we have something in the OOA queue, we'll free it when the 4983 * last I/O completes. 4984 */ 4985 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4986 mtx_unlock(&lun->lun_lock); 4987 mtx_lock(&ctl_softc->ctl_lock); 4988 ctl_free_lun(lun); 4989 mtx_unlock(&ctl_softc->ctl_lock); 4990 } else 4991 mtx_unlock(&lun->lun_lock); 4992 4993 return (0); 4994} 4995 4996int 4997ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4998{ 4999 struct ctl_softc *ctl_softc; 5000 struct ctl_lun *lun; 5001 5002 ctl_softc = control_softc; 5003 lun = (struct ctl_lun *)be_lun->ctl_lun; 5004 5005 mtx_lock(&lun->lun_lock); 5006 lun->flags |= CTL_LUN_INOPERABLE; 5007 mtx_unlock(&lun->lun_lock); 5008 5009 return (0); 5010} 5011 5012int 5013ctl_lun_operable(struct ctl_be_lun *be_lun) 5014{ 5015 struct ctl_softc *ctl_softc; 5016 struct ctl_lun *lun; 5017 5018 ctl_softc = control_softc; 5019 lun = (struct ctl_lun *)be_lun->ctl_lun; 5020 5021 mtx_lock(&lun->lun_lock); 5022 lun->flags &= ~CTL_LUN_INOPERABLE; 5023 mtx_unlock(&lun->lun_lock); 5024 5025 return (0); 5026} 5027 5028void 5029ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5030{ 5031 struct ctl_lun *lun; 5032 struct ctl_softc *softc; 5033 int i; 5034 5035 softc = control_softc; 5036 5037 lun = (struct ctl_lun *)be_lun->ctl_lun; 5038 5039 mtx_lock(&lun->lun_lock); 5040 5041 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5042 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5043 5044 mtx_unlock(&lun->lun_lock); 5045} 5046 5047/* 5048 * Backend "memory move is complete" callback for requests that never 5049 * make it down to say RAIDCore's configuration code. 5050 */ 5051int 5052ctl_config_move_done(union ctl_io *io) 5053{ 5054 int retval; 5055 5056 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5057 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5058 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5059 5060 if ((io->io_hdr.port_status != 0) && 5061 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5062 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5063 /* 5064 * For hardware error sense keys, the sense key 5065 * specific value is defined to be a retry count, 5066 * but we use it to pass back an internal FETD 5067 * error code. XXX KDM Hopefully the FETD is only 5068 * using 16 bits for an error code, since that's 5069 * all the space we have in the sks field. 5070 */ 5071 ctl_set_internal_failure(&io->scsiio, 5072 /*sks_valid*/ 1, 5073 /*retry_count*/ 5074 io->io_hdr.port_status); 5075 } 5076 5077 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5078 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5079 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5080 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5081 /* 5082 * XXX KDM just assuming a single pointer here, and not a 5083 * S/G list. If we start using S/G lists for config data, 5084 * we'll need to know how to clean them up here as well. 5085 */ 5086 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5087 free(io->scsiio.kern_data_ptr, M_CTL); 5088 ctl_done(io); 5089 retval = CTL_RETVAL_COMPLETE; 5090 } else { 5091 /* 5092 * XXX KDM now we need to continue data movement. Some 5093 * options: 5094 * - call ctl_scsiio() again? We don't do this for data 5095 * writes, because for those at least we know ahead of 5096 * time where the write will go and how long it is. For 5097 * config writes, though, that information is largely 5098 * contained within the write itself, thus we need to 5099 * parse out the data again. 5100 * 5101 * - Call some other function once the data is in? 5102 */ 5103 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5104 ctl_data_print(io); 5105 5106 /* 5107 * XXX KDM call ctl_scsiio() again for now, and check flag 5108 * bits to see whether we're allocated or not. 5109 */ 5110 retval = ctl_scsiio(&io->scsiio); 5111 } 5112 return (retval); 5113} 5114 5115/* 5116 * This gets called by a backend driver when it is done with a 5117 * data_submit method. 5118 */ 5119void 5120ctl_data_submit_done(union ctl_io *io) 5121{ 5122 /* 5123 * If the IO_CONT flag is set, we need to call the supplied 5124 * function to continue processing the I/O, instead of completing 5125 * the I/O just yet. 5126 * 5127 * If there is an error, though, we don't want to keep processing. 5128 * Instead, just send status back to the initiator. 5129 */ 5130 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5131 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5132 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5133 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5134 io->scsiio.io_cont(io); 5135 return; 5136 } 5137 ctl_done(io); 5138} 5139 5140/* 5141 * This gets called by a backend driver when it is done with a 5142 * configuration write. 5143 */ 5144void 5145ctl_config_write_done(union ctl_io *io) 5146{ 5147 uint8_t *buf; 5148 5149 /* 5150 * If the IO_CONT flag is set, we need to call the supplied 5151 * function to continue processing the I/O, instead of completing 5152 * the I/O just yet. 5153 * 5154 * If there is an error, though, we don't want to keep processing. 5155 * Instead, just send status back to the initiator. 5156 */ 5157 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5158 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5159 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5160 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5161 io->scsiio.io_cont(io); 5162 return; 5163 } 5164 /* 5165 * Since a configuration write can be done for commands that actually 5166 * have data allocated, like write buffer, and commands that have 5167 * no data, like start/stop unit, we need to check here. 5168 */ 5169 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5170 buf = io->scsiio.kern_data_ptr; 5171 else 5172 buf = NULL; 5173 ctl_done(io); 5174 if (buf) 5175 free(buf, M_CTL); 5176} 5177 5178/* 5179 * SCSI release command. 5180 */ 5181int 5182ctl_scsi_release(struct ctl_scsiio *ctsio) 5183{ 5184 int length, longid, thirdparty_id, resv_id; 5185 struct ctl_softc *ctl_softc; 5186 struct ctl_lun *lun; 5187 uint32_t residx; 5188 5189 length = 0; 5190 resv_id = 0; 5191 5192 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5193 5194 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5195 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5196 ctl_softc = control_softc; 5197 5198 switch (ctsio->cdb[0]) { 5199 case RELEASE_10: { 5200 struct scsi_release_10 *cdb; 5201 5202 cdb = (struct scsi_release_10 *)ctsio->cdb; 5203 5204 if (cdb->byte2 & SR10_LONGID) 5205 longid = 1; 5206 else 5207 thirdparty_id = cdb->thirdparty_id; 5208 5209 resv_id = cdb->resv_id; 5210 length = scsi_2btoul(cdb->length); 5211 break; 5212 } 5213 } 5214 5215 5216 /* 5217 * XXX KDM right now, we only support LUN reservation. We don't 5218 * support 3rd party reservations, or extent reservations, which 5219 * might actually need the parameter list. If we've gotten this 5220 * far, we've got a LUN reservation. Anything else got kicked out 5221 * above. So, according to SPC, ignore the length. 5222 */ 5223 length = 0; 5224 5225 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5226 && (length > 0)) { 5227 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5228 ctsio->kern_data_len = length; 5229 ctsio->kern_total_len = length; 5230 ctsio->kern_data_resid = 0; 5231 ctsio->kern_rel_offset = 0; 5232 ctsio->kern_sg_entries = 0; 5233 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5234 ctsio->be_move_done = ctl_config_move_done; 5235 ctl_datamove((union ctl_io *)ctsio); 5236 5237 return (CTL_RETVAL_COMPLETE); 5238 } 5239 5240 if (length > 0) 5241 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5242 5243 mtx_lock(&lun->lun_lock); 5244 5245 /* 5246 * According to SPC, it is not an error for an intiator to attempt 5247 * to release a reservation on a LUN that isn't reserved, or that 5248 * is reserved by another initiator. The reservation can only be 5249 * released, though, by the initiator who made it or by one of 5250 * several reset type events. 5251 */ 5252 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5253 lun->flags &= ~CTL_LUN_RESERVED; 5254 5255 mtx_unlock(&lun->lun_lock); 5256 5257 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5258 free(ctsio->kern_data_ptr, M_CTL); 5259 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5260 } 5261 5262 ctl_set_success(ctsio); 5263 ctl_done((union ctl_io *)ctsio); 5264 return (CTL_RETVAL_COMPLETE); 5265} 5266 5267int 5268ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5269{ 5270 int extent, thirdparty, longid; 5271 int resv_id, length; 5272 uint64_t thirdparty_id; 5273 struct ctl_softc *ctl_softc; 5274 struct ctl_lun *lun; 5275 uint32_t residx; 5276 5277 extent = 0; 5278 thirdparty = 0; 5279 longid = 0; 5280 resv_id = 0; 5281 length = 0; 5282 thirdparty_id = 0; 5283 5284 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5285 5286 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5287 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5288 ctl_softc = control_softc; 5289 5290 switch (ctsio->cdb[0]) { 5291 case RESERVE_10: { 5292 struct scsi_reserve_10 *cdb; 5293 5294 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5295 5296 if (cdb->byte2 & SR10_LONGID) 5297 longid = 1; 5298 else 5299 thirdparty_id = cdb->thirdparty_id; 5300 5301 resv_id = cdb->resv_id; 5302 length = scsi_2btoul(cdb->length); 5303 break; 5304 } 5305 } 5306 5307 /* 5308 * XXX KDM right now, we only support LUN reservation. We don't 5309 * support 3rd party reservations, or extent reservations, which 5310 * might actually need the parameter list. If we've gotten this 5311 * far, we've got a LUN reservation. Anything else got kicked out 5312 * above. So, according to SPC, ignore the length. 5313 */ 5314 length = 0; 5315 5316 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5317 && (length > 0)) { 5318 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5319 ctsio->kern_data_len = length; 5320 ctsio->kern_total_len = length; 5321 ctsio->kern_data_resid = 0; 5322 ctsio->kern_rel_offset = 0; 5323 ctsio->kern_sg_entries = 0; 5324 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5325 ctsio->be_move_done = ctl_config_move_done; 5326 ctl_datamove((union ctl_io *)ctsio); 5327 5328 return (CTL_RETVAL_COMPLETE); 5329 } 5330 5331 if (length > 0) 5332 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5333 5334 mtx_lock(&lun->lun_lock); 5335 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5336 ctl_set_reservation_conflict(ctsio); 5337 goto bailout; 5338 } 5339 5340 lun->flags |= CTL_LUN_RESERVED; 5341 lun->res_idx = residx; 5342 5343 ctl_set_success(ctsio); 5344 5345bailout: 5346 mtx_unlock(&lun->lun_lock); 5347 5348 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5349 free(ctsio->kern_data_ptr, M_CTL); 5350 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5351 } 5352 5353 ctl_done((union ctl_io *)ctsio); 5354 return (CTL_RETVAL_COMPLETE); 5355} 5356 5357int 5358ctl_start_stop(struct ctl_scsiio *ctsio) 5359{ 5360 struct scsi_start_stop_unit *cdb; 5361 struct ctl_lun *lun; 5362 struct ctl_softc *ctl_softc; 5363 int retval; 5364 5365 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5366 5367 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5368 ctl_softc = control_softc; 5369 retval = 0; 5370 5371 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5372 5373 /* 5374 * XXX KDM 5375 * We don't support the immediate bit on a stop unit. In order to 5376 * do that, we would need to code up a way to know that a stop is 5377 * pending, and hold off any new commands until it completes, one 5378 * way or another. Then we could accept or reject those commands 5379 * depending on its status. We would almost need to do the reverse 5380 * of what we do below for an immediate start -- return the copy of 5381 * the ctl_io to the FETD with status to send to the host (and to 5382 * free the copy!) and then free the original I/O once the stop 5383 * actually completes. That way, the OOA queue mechanism can work 5384 * to block commands that shouldn't proceed. Another alternative 5385 * would be to put the copy in the queue in place of the original, 5386 * and return the original back to the caller. That could be 5387 * slightly safer.. 5388 */ 5389 if ((cdb->byte2 & SSS_IMMED) 5390 && ((cdb->how & SSS_START) == 0)) { 5391 ctl_set_invalid_field(ctsio, 5392 /*sks_valid*/ 1, 5393 /*command*/ 1, 5394 /*field*/ 1, 5395 /*bit_valid*/ 1, 5396 /*bit*/ 0); 5397 ctl_done((union ctl_io *)ctsio); 5398 return (CTL_RETVAL_COMPLETE); 5399 } 5400 5401 if ((lun->flags & CTL_LUN_PR_RESERVED) 5402 && ((cdb->how & SSS_START)==0)) { 5403 uint32_t residx; 5404 5405 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5406 if (ctl_get_prkey(lun, residx) == 0 5407 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5408 5409 ctl_set_reservation_conflict(ctsio); 5410 ctl_done((union ctl_io *)ctsio); 5411 return (CTL_RETVAL_COMPLETE); 5412 } 5413 } 5414 5415 /* 5416 * If there is no backend on this device, we can't start or stop 5417 * it. In theory we shouldn't get any start/stop commands in the 5418 * first place at this level if the LUN doesn't have a backend. 5419 * That should get stopped by the command decode code. 5420 */ 5421 if (lun->backend == NULL) { 5422 ctl_set_invalid_opcode(ctsio); 5423 ctl_done((union ctl_io *)ctsio); 5424 return (CTL_RETVAL_COMPLETE); 5425 } 5426 5427 /* 5428 * XXX KDM Copan-specific offline behavior. 5429 * Figure out a reasonable way to port this? 5430 */ 5431#ifdef NEEDTOPORT 5432 mtx_lock(&lun->lun_lock); 5433 5434 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5435 && (lun->flags & CTL_LUN_OFFLINE)) { 5436 /* 5437 * If the LUN is offline, and the on/offline bit isn't set, 5438 * reject the start or stop. Otherwise, let it through. 5439 */ 5440 mtx_unlock(&lun->lun_lock); 5441 ctl_set_lun_not_ready(ctsio); 5442 ctl_done((union ctl_io *)ctsio); 5443 } else { 5444 mtx_unlock(&lun->lun_lock); 5445#endif /* NEEDTOPORT */ 5446 /* 5447 * This could be a start or a stop when we're online, 5448 * or a stop/offline or start/online. A start or stop when 5449 * we're offline is covered in the case above. 5450 */ 5451 /* 5452 * In the non-immediate case, we send the request to 5453 * the backend and return status to the user when 5454 * it is done. 5455 * 5456 * In the immediate case, we allocate a new ctl_io 5457 * to hold a copy of the request, and send that to 5458 * the backend. We then set good status on the 5459 * user's request and return it immediately. 5460 */ 5461 if (cdb->byte2 & SSS_IMMED) { 5462 union ctl_io *new_io; 5463 5464 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5465 ctl_copy_io((union ctl_io *)ctsio, new_io); 5466 retval = lun->backend->config_write(new_io); 5467 ctl_set_success(ctsio); 5468 ctl_done((union ctl_io *)ctsio); 5469 } else { 5470 retval = lun->backend->config_write( 5471 (union ctl_io *)ctsio); 5472 } 5473#ifdef NEEDTOPORT 5474 } 5475#endif 5476 return (retval); 5477} 5478 5479/* 5480 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5481 * we don't really do anything with the LBA and length fields if the user 5482 * passes them in. Instead we'll just flush out the cache for the entire 5483 * LUN. 5484 */ 5485int 5486ctl_sync_cache(struct ctl_scsiio *ctsio) 5487{ 5488 struct ctl_lun *lun; 5489 struct ctl_softc *ctl_softc; 5490 uint64_t starting_lba; 5491 uint32_t block_count; 5492 int retval; 5493 5494 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5495 5496 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5497 ctl_softc = control_softc; 5498 retval = 0; 5499 5500 switch (ctsio->cdb[0]) { 5501 case SYNCHRONIZE_CACHE: { 5502 struct scsi_sync_cache *cdb; 5503 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5504 5505 starting_lba = scsi_4btoul(cdb->begin_lba); 5506 block_count = scsi_2btoul(cdb->lb_count); 5507 break; 5508 } 5509 case SYNCHRONIZE_CACHE_16: { 5510 struct scsi_sync_cache_16 *cdb; 5511 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5512 5513 starting_lba = scsi_8btou64(cdb->begin_lba); 5514 block_count = scsi_4btoul(cdb->lb_count); 5515 break; 5516 } 5517 default: 5518 ctl_set_invalid_opcode(ctsio); 5519 ctl_done((union ctl_io *)ctsio); 5520 goto bailout; 5521 break; /* NOTREACHED */ 5522 } 5523 5524 /* 5525 * We check the LBA and length, but don't do anything with them. 5526 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5527 * get flushed. This check will just help satisfy anyone who wants 5528 * to see an error for an out of range LBA. 5529 */ 5530 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5531 ctl_set_lba_out_of_range(ctsio); 5532 ctl_done((union ctl_io *)ctsio); 5533 goto bailout; 5534 } 5535 5536 /* 5537 * If this LUN has no backend, we can't flush the cache anyway. 5538 */ 5539 if (lun->backend == NULL) { 5540 ctl_set_invalid_opcode(ctsio); 5541 ctl_done((union ctl_io *)ctsio); 5542 goto bailout; 5543 } 5544 5545 /* 5546 * Check to see whether we're configured to send the SYNCHRONIZE 5547 * CACHE command directly to the back end. 5548 */ 5549 mtx_lock(&lun->lun_lock); 5550 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5551 && (++(lun->sync_count) >= lun->sync_interval)) { 5552 lun->sync_count = 0; 5553 mtx_unlock(&lun->lun_lock); 5554 retval = lun->backend->config_write((union ctl_io *)ctsio); 5555 } else { 5556 mtx_unlock(&lun->lun_lock); 5557 ctl_set_success(ctsio); 5558 ctl_done((union ctl_io *)ctsio); 5559 } 5560 5561bailout: 5562 5563 return (retval); 5564} 5565 5566int 5567ctl_format(struct ctl_scsiio *ctsio) 5568{ 5569 struct scsi_format *cdb; 5570 struct ctl_lun *lun; 5571 struct ctl_softc *ctl_softc; 5572 int length, defect_list_len; 5573 5574 CTL_DEBUG_PRINT(("ctl_format\n")); 5575 5576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5577 ctl_softc = control_softc; 5578 5579 cdb = (struct scsi_format *)ctsio->cdb; 5580 5581 length = 0; 5582 if (cdb->byte2 & SF_FMTDATA) { 5583 if (cdb->byte2 & SF_LONGLIST) 5584 length = sizeof(struct scsi_format_header_long); 5585 else 5586 length = sizeof(struct scsi_format_header_short); 5587 } 5588 5589 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5590 && (length > 0)) { 5591 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5592 ctsio->kern_data_len = length; 5593 ctsio->kern_total_len = length; 5594 ctsio->kern_data_resid = 0; 5595 ctsio->kern_rel_offset = 0; 5596 ctsio->kern_sg_entries = 0; 5597 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5598 ctsio->be_move_done = ctl_config_move_done; 5599 ctl_datamove((union ctl_io *)ctsio); 5600 5601 return (CTL_RETVAL_COMPLETE); 5602 } 5603 5604 defect_list_len = 0; 5605 5606 if (cdb->byte2 & SF_FMTDATA) { 5607 if (cdb->byte2 & SF_LONGLIST) { 5608 struct scsi_format_header_long *header; 5609 5610 header = (struct scsi_format_header_long *) 5611 ctsio->kern_data_ptr; 5612 5613 defect_list_len = scsi_4btoul(header->defect_list_len); 5614 if (defect_list_len != 0) { 5615 ctl_set_invalid_field(ctsio, 5616 /*sks_valid*/ 1, 5617 /*command*/ 0, 5618 /*field*/ 2, 5619 /*bit_valid*/ 0, 5620 /*bit*/ 0); 5621 goto bailout; 5622 } 5623 } else { 5624 struct scsi_format_header_short *header; 5625 5626 header = (struct scsi_format_header_short *) 5627 ctsio->kern_data_ptr; 5628 5629 defect_list_len = scsi_2btoul(header->defect_list_len); 5630 if (defect_list_len != 0) { 5631 ctl_set_invalid_field(ctsio, 5632 /*sks_valid*/ 1, 5633 /*command*/ 0, 5634 /*field*/ 2, 5635 /*bit_valid*/ 0, 5636 /*bit*/ 0); 5637 goto bailout; 5638 } 5639 } 5640 } 5641 5642 /* 5643 * The format command will clear out the "Medium format corrupted" 5644 * status if set by the configuration code. That status is really 5645 * just a way to notify the host that we have lost the media, and 5646 * get them to issue a command that will basically make them think 5647 * they're blowing away the media. 5648 */ 5649 mtx_lock(&lun->lun_lock); 5650 lun->flags &= ~CTL_LUN_INOPERABLE; 5651 mtx_unlock(&lun->lun_lock); 5652 5653 ctl_set_success(ctsio); 5654bailout: 5655 5656 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5657 free(ctsio->kern_data_ptr, M_CTL); 5658 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5659 } 5660 5661 ctl_done((union ctl_io *)ctsio); 5662 return (CTL_RETVAL_COMPLETE); 5663} 5664 5665int 5666ctl_read_buffer(struct ctl_scsiio *ctsio) 5667{ 5668 struct scsi_read_buffer *cdb; 5669 struct ctl_lun *lun; 5670 int buffer_offset, len; 5671 static uint8_t descr[4]; 5672 static uint8_t echo_descr[4] = { 0 }; 5673 5674 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5675 5676 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5677 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5678 5679 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5680 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5681 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5682 ctl_set_invalid_field(ctsio, 5683 /*sks_valid*/ 1, 5684 /*command*/ 1, 5685 /*field*/ 1, 5686 /*bit_valid*/ 1, 5687 /*bit*/ 4); 5688 ctl_done((union ctl_io *)ctsio); 5689 return (CTL_RETVAL_COMPLETE); 5690 } 5691 5692 len = scsi_3btoul(cdb->length); 5693 buffer_offset = scsi_3btoul(cdb->offset); 5694 5695 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5696 ctl_set_invalid_field(ctsio, 5697 /*sks_valid*/ 1, 5698 /*command*/ 1, 5699 /*field*/ 6, 5700 /*bit_valid*/ 0, 5701 /*bit*/ 0); 5702 ctl_done((union ctl_io *)ctsio); 5703 return (CTL_RETVAL_COMPLETE); 5704 } 5705 5706 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5707 descr[0] = 0; 5708 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5709 ctsio->kern_data_ptr = descr; 5710 len = min(len, sizeof(descr)); 5711 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5712 ctsio->kern_data_ptr = echo_descr; 5713 len = min(len, sizeof(echo_descr)); 5714 } else { 5715 if (lun->write_buffer == NULL) { 5716 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5717 M_CTL, M_WAITOK); 5718 } 5719 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5720 } 5721 ctsio->kern_data_len = len; 5722 ctsio->kern_total_len = len; 5723 ctsio->kern_data_resid = 0; 5724 ctsio->kern_rel_offset = 0; 5725 ctsio->kern_sg_entries = 0; 5726 ctl_set_success(ctsio); 5727 ctsio->be_move_done = ctl_config_move_done; 5728 ctl_datamove((union ctl_io *)ctsio); 5729 return (CTL_RETVAL_COMPLETE); 5730} 5731 5732int 5733ctl_write_buffer(struct ctl_scsiio *ctsio) 5734{ 5735 struct scsi_write_buffer *cdb; 5736 struct ctl_lun *lun; 5737 int buffer_offset, len; 5738 5739 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5740 5741 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5742 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5743 5744 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5745 ctl_set_invalid_field(ctsio, 5746 /*sks_valid*/ 1, 5747 /*command*/ 1, 5748 /*field*/ 1, 5749 /*bit_valid*/ 1, 5750 /*bit*/ 4); 5751 ctl_done((union ctl_io *)ctsio); 5752 return (CTL_RETVAL_COMPLETE); 5753 } 5754 5755 len = scsi_3btoul(cdb->length); 5756 buffer_offset = scsi_3btoul(cdb->offset); 5757 5758 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5759 ctl_set_invalid_field(ctsio, 5760 /*sks_valid*/ 1, 5761 /*command*/ 1, 5762 /*field*/ 6, 5763 /*bit_valid*/ 0, 5764 /*bit*/ 0); 5765 ctl_done((union ctl_io *)ctsio); 5766 return (CTL_RETVAL_COMPLETE); 5767 } 5768 5769 /* 5770 * If we've got a kernel request that hasn't been malloced yet, 5771 * malloc it and tell the caller the data buffer is here. 5772 */ 5773 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5774 if (lun->write_buffer == NULL) { 5775 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5776 M_CTL, M_WAITOK); 5777 } 5778 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5779 ctsio->kern_data_len = len; 5780 ctsio->kern_total_len = len; 5781 ctsio->kern_data_resid = 0; 5782 ctsio->kern_rel_offset = 0; 5783 ctsio->kern_sg_entries = 0; 5784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5785 ctsio->be_move_done = ctl_config_move_done; 5786 ctl_datamove((union ctl_io *)ctsio); 5787 5788 return (CTL_RETVAL_COMPLETE); 5789 } 5790 5791 ctl_set_success(ctsio); 5792 ctl_done((union ctl_io *)ctsio); 5793 return (CTL_RETVAL_COMPLETE); 5794} 5795 5796int 5797ctl_write_same(struct ctl_scsiio *ctsio) 5798{ 5799 struct ctl_lun *lun; 5800 struct ctl_lba_len_flags *lbalen; 5801 uint64_t lba; 5802 uint32_t num_blocks; 5803 int len, retval; 5804 uint8_t byte2; 5805 5806 retval = CTL_RETVAL_COMPLETE; 5807 5808 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5809 5810 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5811 5812 switch (ctsio->cdb[0]) { 5813 case WRITE_SAME_10: { 5814 struct scsi_write_same_10 *cdb; 5815 5816 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5817 5818 lba = scsi_4btoul(cdb->addr); 5819 num_blocks = scsi_2btoul(cdb->length); 5820 byte2 = cdb->byte2; 5821 break; 5822 } 5823 case WRITE_SAME_16: { 5824 struct scsi_write_same_16 *cdb; 5825 5826 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5827 5828 lba = scsi_8btou64(cdb->addr); 5829 num_blocks = scsi_4btoul(cdb->length); 5830 byte2 = cdb->byte2; 5831 break; 5832 } 5833 default: 5834 /* 5835 * We got a command we don't support. This shouldn't 5836 * happen, commands should be filtered out above us. 5837 */ 5838 ctl_set_invalid_opcode(ctsio); 5839 ctl_done((union ctl_io *)ctsio); 5840 5841 return (CTL_RETVAL_COMPLETE); 5842 break; /* NOTREACHED */ 5843 } 5844 5845 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5846 if ((byte2 & SWS_UNMAP) == 0 && 5847 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5848 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5849 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5850 ctl_done((union ctl_io *)ctsio); 5851 return (CTL_RETVAL_COMPLETE); 5852 } 5853 5854 /* 5855 * The first check is to make sure we're in bounds, the second 5856 * check is to catch wrap-around problems. If the lba + num blocks 5857 * is less than the lba, then we've wrapped around and the block 5858 * range is invalid anyway. 5859 */ 5860 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5861 || ((lba + num_blocks) < lba)) { 5862 ctl_set_lba_out_of_range(ctsio); 5863 ctl_done((union ctl_io *)ctsio); 5864 return (CTL_RETVAL_COMPLETE); 5865 } 5866 5867 /* Zero number of blocks means "to the last logical block" */ 5868 if (num_blocks == 0) { 5869 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5870 ctl_set_invalid_field(ctsio, 5871 /*sks_valid*/ 0, 5872 /*command*/ 1, 5873 /*field*/ 0, 5874 /*bit_valid*/ 0, 5875 /*bit*/ 0); 5876 ctl_done((union ctl_io *)ctsio); 5877 return (CTL_RETVAL_COMPLETE); 5878 } 5879 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5880 } 5881 5882 len = lun->be_lun->blocksize; 5883 5884 /* 5885 * If we've got a kernel request that hasn't been malloced yet, 5886 * malloc it and tell the caller the data buffer is here. 5887 */ 5888 if ((byte2 & SWS_NDOB) == 0 && 5889 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5890 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5891 ctsio->kern_data_len = len; 5892 ctsio->kern_total_len = len; 5893 ctsio->kern_data_resid = 0; 5894 ctsio->kern_rel_offset = 0; 5895 ctsio->kern_sg_entries = 0; 5896 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5897 ctsio->be_move_done = ctl_config_move_done; 5898 ctl_datamove((union ctl_io *)ctsio); 5899 5900 return (CTL_RETVAL_COMPLETE); 5901 } 5902 5903 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5904 lbalen->lba = lba; 5905 lbalen->len = num_blocks; 5906 lbalen->flags = byte2; 5907 retval = lun->backend->config_write((union ctl_io *)ctsio); 5908 5909 return (retval); 5910} 5911 5912int 5913ctl_unmap(struct ctl_scsiio *ctsio) 5914{ 5915 struct ctl_lun *lun; 5916 struct scsi_unmap *cdb; 5917 struct ctl_ptr_len_flags *ptrlen; 5918 struct scsi_unmap_header *hdr; 5919 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5920 uint64_t lba; 5921 uint32_t num_blocks; 5922 int len, retval; 5923 uint8_t byte2; 5924 5925 retval = CTL_RETVAL_COMPLETE; 5926 5927 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5928 5929 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5930 cdb = (struct scsi_unmap *)ctsio->cdb; 5931 5932 len = scsi_2btoul(cdb->length); 5933 byte2 = cdb->byte2; 5934 5935 /* 5936 * If we've got a kernel request that hasn't been malloced yet, 5937 * malloc it and tell the caller the data buffer is here. 5938 */ 5939 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5940 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5941 ctsio->kern_data_len = len; 5942 ctsio->kern_total_len = len; 5943 ctsio->kern_data_resid = 0; 5944 ctsio->kern_rel_offset = 0; 5945 ctsio->kern_sg_entries = 0; 5946 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5947 ctsio->be_move_done = ctl_config_move_done; 5948 ctl_datamove((union ctl_io *)ctsio); 5949 5950 return (CTL_RETVAL_COMPLETE); 5951 } 5952 5953 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5954 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5955 if (len < sizeof (*hdr) || 5956 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5957 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5958 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5959 ctl_set_invalid_field(ctsio, 5960 /*sks_valid*/ 0, 5961 /*command*/ 0, 5962 /*field*/ 0, 5963 /*bit_valid*/ 0, 5964 /*bit*/ 0); 5965 goto done; 5966 } 5967 len = scsi_2btoul(hdr->desc_length); 5968 buf = (struct scsi_unmap_desc *)(hdr + 1); 5969 end = buf + len / sizeof(*buf); 5970 5971 endnz = buf; 5972 for (range = buf; range < end; range++) { 5973 lba = scsi_8btou64(range->lba); 5974 num_blocks = scsi_4btoul(range->length); 5975 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5976 || ((lba + num_blocks) < lba)) { 5977 ctl_set_lba_out_of_range(ctsio); 5978 ctl_done((union ctl_io *)ctsio); 5979 return (CTL_RETVAL_COMPLETE); 5980 } 5981 if (num_blocks != 0) 5982 endnz = range + 1; 5983 } 5984 5985 /* 5986 * Block backend can not handle zero last range. 5987 * Filter it out and return if there is nothing left. 5988 */ 5989 len = (uint8_t *)endnz - (uint8_t *)buf; 5990 if (len == 0) { 5991 ctl_set_success(ctsio); 5992 goto done; 5993 } 5994 5995 mtx_lock(&lun->lun_lock); 5996 ptrlen = (struct ctl_ptr_len_flags *) 5997 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5998 ptrlen->ptr = (void *)buf; 5999 ptrlen->len = len; 6000 ptrlen->flags = byte2; 6001 ctl_check_blocked(lun); 6002 mtx_unlock(&lun->lun_lock); 6003 6004 retval = lun->backend->config_write((union ctl_io *)ctsio); 6005 return (retval); 6006 6007done: 6008 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6009 free(ctsio->kern_data_ptr, M_CTL); 6010 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6011 } 6012 ctl_done((union ctl_io *)ctsio); 6013 return (CTL_RETVAL_COMPLETE); 6014} 6015 6016/* 6017 * Note that this function currently doesn't actually do anything inside 6018 * CTL to enforce things if the DQue bit is turned on. 6019 * 6020 * Also note that this function can't be used in the default case, because 6021 * the DQue bit isn't set in the changeable mask for the control mode page 6022 * anyway. This is just here as an example for how to implement a page 6023 * handler, and a placeholder in case we want to allow the user to turn 6024 * tagged queueing on and off. 6025 * 6026 * The D_SENSE bit handling is functional, however, and will turn 6027 * descriptor sense on and off for a given LUN. 6028 */ 6029int 6030ctl_control_page_handler(struct ctl_scsiio *ctsio, 6031 struct ctl_page_index *page_index, uint8_t *page_ptr) 6032{ 6033 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6034 struct ctl_lun *lun; 6035 struct ctl_softc *softc; 6036 int set_ua; 6037 uint32_t initidx; 6038 6039 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6040 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6041 set_ua = 0; 6042 6043 user_cp = (struct scsi_control_page *)page_ptr; 6044 current_cp = (struct scsi_control_page *) 6045 (page_index->page_data + (page_index->page_len * 6046 CTL_PAGE_CURRENT)); 6047 saved_cp = (struct scsi_control_page *) 6048 (page_index->page_data + (page_index->page_len * 6049 CTL_PAGE_SAVED)); 6050 6051 softc = control_softc; 6052 6053 mtx_lock(&lun->lun_lock); 6054 if (((current_cp->rlec & SCP_DSENSE) == 0) 6055 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6056 /* 6057 * Descriptor sense is currently turned off and the user 6058 * wants to turn it on. 6059 */ 6060 current_cp->rlec |= SCP_DSENSE; 6061 saved_cp->rlec |= SCP_DSENSE; 6062 lun->flags |= CTL_LUN_SENSE_DESC; 6063 set_ua = 1; 6064 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6065 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6066 /* 6067 * Descriptor sense is currently turned on, and the user 6068 * wants to turn it off. 6069 */ 6070 current_cp->rlec &= ~SCP_DSENSE; 6071 saved_cp->rlec &= ~SCP_DSENSE; 6072 lun->flags &= ~CTL_LUN_SENSE_DESC; 6073 set_ua = 1; 6074 } 6075 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6076 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6077 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6078 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6079 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6080 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6081 set_ua = 1; 6082 } 6083 if ((current_cp->eca_and_aen & SCP_SWP) != 6084 (user_cp->eca_and_aen & SCP_SWP)) { 6085 current_cp->eca_and_aen &= ~SCP_SWP; 6086 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6087 saved_cp->eca_and_aen &= ~SCP_SWP; 6088 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6089 set_ua = 1; 6090 } 6091 if (set_ua != 0) { 6092 int i; 6093 /* 6094 * Let other initiators know that the mode 6095 * parameters for this LUN have changed. 6096 */ 6097 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6098 if (i == initidx) 6099 continue; 6100 6101 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6102 } 6103 } 6104 mtx_unlock(&lun->lun_lock); 6105 6106 return (0); 6107} 6108 6109int 6110ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6111 struct ctl_page_index *page_index, uint8_t *page_ptr) 6112{ 6113 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6114 struct ctl_lun *lun; 6115 int set_ua; 6116 uint32_t initidx; 6117 6118 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6119 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6120 set_ua = 0; 6121 6122 user_cp = (struct scsi_caching_page *)page_ptr; 6123 current_cp = (struct scsi_caching_page *) 6124 (page_index->page_data + (page_index->page_len * 6125 CTL_PAGE_CURRENT)); 6126 saved_cp = (struct scsi_caching_page *) 6127 (page_index->page_data + (page_index->page_len * 6128 CTL_PAGE_SAVED)); 6129 6130 mtx_lock(&lun->lun_lock); 6131 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6132 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6133 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6134 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6135 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6136 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6137 set_ua = 1; 6138 } 6139 if (set_ua != 0) { 6140 int i; 6141 /* 6142 * Let other initiators know that the mode 6143 * parameters for this LUN have changed. 6144 */ 6145 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6146 if (i == initidx) 6147 continue; 6148 6149 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6150 } 6151 } 6152 mtx_unlock(&lun->lun_lock); 6153 6154 return (0); 6155} 6156 6157int 6158ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6159 struct ctl_page_index *page_index, 6160 uint8_t *page_ptr) 6161{ 6162 uint8_t *c; 6163 int i; 6164 6165 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6166 ctl_time_io_secs = 6167 (c[0] << 8) | 6168 (c[1] << 0) | 6169 0; 6170 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6171 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6172 printf("page data:"); 6173 for (i=0; i<8; i++) 6174 printf(" %.2x",page_ptr[i]); 6175 printf("\n"); 6176 return (0); 6177} 6178 6179int 6180ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6181 struct ctl_page_index *page_index, 6182 int pc) 6183{ 6184 struct copan_debugconf_subpage *page; 6185 6186 page = (struct copan_debugconf_subpage *)page_index->page_data + 6187 (page_index->page_len * pc); 6188 6189 switch (pc) { 6190 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6191 case SMS_PAGE_CTRL_DEFAULT >> 6: 6192 case SMS_PAGE_CTRL_SAVED >> 6: 6193 /* 6194 * We don't update the changable or default bits for this page. 6195 */ 6196 break; 6197 case SMS_PAGE_CTRL_CURRENT >> 6: 6198 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6199 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6200 break; 6201 default: 6202#ifdef NEEDTOPORT 6203 EPRINT(0, "Invalid PC %d!!", pc); 6204#endif /* NEEDTOPORT */ 6205 break; 6206 } 6207 return (0); 6208} 6209 6210 6211static int 6212ctl_do_mode_select(union ctl_io *io) 6213{ 6214 struct scsi_mode_page_header *page_header; 6215 struct ctl_page_index *page_index; 6216 struct ctl_scsiio *ctsio; 6217 int control_dev, page_len; 6218 int page_len_offset, page_len_size; 6219 union ctl_modepage_info *modepage_info; 6220 struct ctl_lun *lun; 6221 int *len_left, *len_used; 6222 int retval, i; 6223 6224 ctsio = &io->scsiio; 6225 page_index = NULL; 6226 page_len = 0; 6227 retval = CTL_RETVAL_COMPLETE; 6228 6229 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6230 6231 if (lun->be_lun->lun_type != T_DIRECT) 6232 control_dev = 1; 6233 else 6234 control_dev = 0; 6235 6236 modepage_info = (union ctl_modepage_info *) 6237 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6238 len_left = &modepage_info->header.len_left; 6239 len_used = &modepage_info->header.len_used; 6240 6241do_next_page: 6242 6243 page_header = (struct scsi_mode_page_header *) 6244 (ctsio->kern_data_ptr + *len_used); 6245 6246 if (*len_left == 0) { 6247 free(ctsio->kern_data_ptr, M_CTL); 6248 ctl_set_success(ctsio); 6249 ctl_done((union ctl_io *)ctsio); 6250 return (CTL_RETVAL_COMPLETE); 6251 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6252 6253 free(ctsio->kern_data_ptr, M_CTL); 6254 ctl_set_param_len_error(ctsio); 6255 ctl_done((union ctl_io *)ctsio); 6256 return (CTL_RETVAL_COMPLETE); 6257 6258 } else if ((page_header->page_code & SMPH_SPF) 6259 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6260 6261 free(ctsio->kern_data_ptr, M_CTL); 6262 ctl_set_param_len_error(ctsio); 6263 ctl_done((union ctl_io *)ctsio); 6264 return (CTL_RETVAL_COMPLETE); 6265 } 6266 6267 6268 /* 6269 * XXX KDM should we do something with the block descriptor? 6270 */ 6271 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6272 6273 if ((control_dev != 0) 6274 && (lun->mode_pages.index[i].page_flags & 6275 CTL_PAGE_FLAG_DISK_ONLY)) 6276 continue; 6277 6278 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6279 (page_header->page_code & SMPH_PC_MASK)) 6280 continue; 6281 6282 /* 6283 * If neither page has a subpage code, then we've got a 6284 * match. 6285 */ 6286 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6287 && ((page_header->page_code & SMPH_SPF) == 0)) { 6288 page_index = &lun->mode_pages.index[i]; 6289 page_len = page_header->page_length; 6290 break; 6291 } 6292 6293 /* 6294 * If both pages have subpages, then the subpage numbers 6295 * have to match. 6296 */ 6297 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6298 && (page_header->page_code & SMPH_SPF)) { 6299 struct scsi_mode_page_header_sp *sph; 6300 6301 sph = (struct scsi_mode_page_header_sp *)page_header; 6302 6303 if (lun->mode_pages.index[i].subpage == 6304 sph->subpage) { 6305 page_index = &lun->mode_pages.index[i]; 6306 page_len = scsi_2btoul(sph->page_length); 6307 break; 6308 } 6309 } 6310 } 6311 6312 /* 6313 * If we couldn't find the page, or if we don't have a mode select 6314 * handler for it, send back an error to the user. 6315 */ 6316 if ((page_index == NULL) 6317 || (page_index->select_handler == NULL)) { 6318 ctl_set_invalid_field(ctsio, 6319 /*sks_valid*/ 1, 6320 /*command*/ 0, 6321 /*field*/ *len_used, 6322 /*bit_valid*/ 0, 6323 /*bit*/ 0); 6324 free(ctsio->kern_data_ptr, M_CTL); 6325 ctl_done((union ctl_io *)ctsio); 6326 return (CTL_RETVAL_COMPLETE); 6327 } 6328 6329 if (page_index->page_code & SMPH_SPF) { 6330 page_len_offset = 2; 6331 page_len_size = 2; 6332 } else { 6333 page_len_size = 1; 6334 page_len_offset = 1; 6335 } 6336 6337 /* 6338 * If the length the initiator gives us isn't the one we specify in 6339 * the mode page header, or if they didn't specify enough data in 6340 * the CDB to avoid truncating this page, kick out the request. 6341 */ 6342 if ((page_len != (page_index->page_len - page_len_offset - 6343 page_len_size)) 6344 || (*len_left < page_index->page_len)) { 6345 6346 6347 ctl_set_invalid_field(ctsio, 6348 /*sks_valid*/ 1, 6349 /*command*/ 0, 6350 /*field*/ *len_used + page_len_offset, 6351 /*bit_valid*/ 0, 6352 /*bit*/ 0); 6353 free(ctsio->kern_data_ptr, M_CTL); 6354 ctl_done((union ctl_io *)ctsio); 6355 return (CTL_RETVAL_COMPLETE); 6356 } 6357 6358 /* 6359 * Run through the mode page, checking to make sure that the bits 6360 * the user changed are actually legal for him to change. 6361 */ 6362 for (i = 0; i < page_index->page_len; i++) { 6363 uint8_t *user_byte, *change_mask, *current_byte; 6364 int bad_bit; 6365 int j; 6366 6367 user_byte = (uint8_t *)page_header + i; 6368 change_mask = page_index->page_data + 6369 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6370 current_byte = page_index->page_data + 6371 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6372 6373 /* 6374 * Check to see whether the user set any bits in this byte 6375 * that he is not allowed to set. 6376 */ 6377 if ((*user_byte & ~(*change_mask)) == 6378 (*current_byte & ~(*change_mask))) 6379 continue; 6380 6381 /* 6382 * Go through bit by bit to determine which one is illegal. 6383 */ 6384 bad_bit = 0; 6385 for (j = 7; j >= 0; j--) { 6386 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6387 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6388 bad_bit = i; 6389 break; 6390 } 6391 } 6392 ctl_set_invalid_field(ctsio, 6393 /*sks_valid*/ 1, 6394 /*command*/ 0, 6395 /*field*/ *len_used + i, 6396 /*bit_valid*/ 1, 6397 /*bit*/ bad_bit); 6398 free(ctsio->kern_data_ptr, M_CTL); 6399 ctl_done((union ctl_io *)ctsio); 6400 return (CTL_RETVAL_COMPLETE); 6401 } 6402 6403 /* 6404 * Decrement these before we call the page handler, since we may 6405 * end up getting called back one way or another before the handler 6406 * returns to this context. 6407 */ 6408 *len_left -= page_index->page_len; 6409 *len_used += page_index->page_len; 6410 6411 retval = page_index->select_handler(ctsio, page_index, 6412 (uint8_t *)page_header); 6413 6414 /* 6415 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6416 * wait until this queued command completes to finish processing 6417 * the mode page. If it returns anything other than 6418 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6419 * already set the sense information, freed the data pointer, and 6420 * completed the io for us. 6421 */ 6422 if (retval != CTL_RETVAL_COMPLETE) 6423 goto bailout_no_done; 6424 6425 /* 6426 * If the initiator sent us more than one page, parse the next one. 6427 */ 6428 if (*len_left > 0) 6429 goto do_next_page; 6430 6431 ctl_set_success(ctsio); 6432 free(ctsio->kern_data_ptr, M_CTL); 6433 ctl_done((union ctl_io *)ctsio); 6434 6435bailout_no_done: 6436 6437 return (CTL_RETVAL_COMPLETE); 6438 6439} 6440 6441int 6442ctl_mode_select(struct ctl_scsiio *ctsio) 6443{ 6444 int param_len, pf, sp; 6445 int header_size, bd_len; 6446 int len_left, len_used; 6447 struct ctl_page_index *page_index; 6448 struct ctl_lun *lun; 6449 int control_dev, page_len; 6450 union ctl_modepage_info *modepage_info; 6451 int retval; 6452 6453 pf = 0; 6454 sp = 0; 6455 page_len = 0; 6456 len_used = 0; 6457 len_left = 0; 6458 retval = 0; 6459 bd_len = 0; 6460 page_index = NULL; 6461 6462 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6463 6464 if (lun->be_lun->lun_type != T_DIRECT) 6465 control_dev = 1; 6466 else 6467 control_dev = 0; 6468 6469 switch (ctsio->cdb[0]) { 6470 case MODE_SELECT_6: { 6471 struct scsi_mode_select_6 *cdb; 6472 6473 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6474 6475 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6476 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6477 6478 param_len = cdb->length; 6479 header_size = sizeof(struct scsi_mode_header_6); 6480 break; 6481 } 6482 case MODE_SELECT_10: { 6483 struct scsi_mode_select_10 *cdb; 6484 6485 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6486 6487 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6488 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6489 6490 param_len = scsi_2btoul(cdb->length); 6491 header_size = sizeof(struct scsi_mode_header_10); 6492 break; 6493 } 6494 default: 6495 ctl_set_invalid_opcode(ctsio); 6496 ctl_done((union ctl_io *)ctsio); 6497 return (CTL_RETVAL_COMPLETE); 6498 break; /* NOTREACHED */ 6499 } 6500 6501 /* 6502 * From SPC-3: 6503 * "A parameter list length of zero indicates that the Data-Out Buffer 6504 * shall be empty. This condition shall not be considered as an error." 6505 */ 6506 if (param_len == 0) { 6507 ctl_set_success(ctsio); 6508 ctl_done((union ctl_io *)ctsio); 6509 return (CTL_RETVAL_COMPLETE); 6510 } 6511 6512 /* 6513 * Since we'll hit this the first time through, prior to 6514 * allocation, we don't need to free a data buffer here. 6515 */ 6516 if (param_len < header_size) { 6517 ctl_set_param_len_error(ctsio); 6518 ctl_done((union ctl_io *)ctsio); 6519 return (CTL_RETVAL_COMPLETE); 6520 } 6521 6522 /* 6523 * Allocate the data buffer and grab the user's data. In theory, 6524 * we shouldn't have to sanity check the parameter list length here 6525 * because the maximum size is 64K. We should be able to malloc 6526 * that much without too many problems. 6527 */ 6528 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6529 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6530 ctsio->kern_data_len = param_len; 6531 ctsio->kern_total_len = param_len; 6532 ctsio->kern_data_resid = 0; 6533 ctsio->kern_rel_offset = 0; 6534 ctsio->kern_sg_entries = 0; 6535 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6536 ctsio->be_move_done = ctl_config_move_done; 6537 ctl_datamove((union ctl_io *)ctsio); 6538 6539 return (CTL_RETVAL_COMPLETE); 6540 } 6541 6542 switch (ctsio->cdb[0]) { 6543 case MODE_SELECT_6: { 6544 struct scsi_mode_header_6 *mh6; 6545 6546 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6547 bd_len = mh6->blk_desc_len; 6548 break; 6549 } 6550 case MODE_SELECT_10: { 6551 struct scsi_mode_header_10 *mh10; 6552 6553 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6554 bd_len = scsi_2btoul(mh10->blk_desc_len); 6555 break; 6556 } 6557 default: 6558 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6559 break; 6560 } 6561 6562 if (param_len < (header_size + bd_len)) { 6563 free(ctsio->kern_data_ptr, M_CTL); 6564 ctl_set_param_len_error(ctsio); 6565 ctl_done((union ctl_io *)ctsio); 6566 return (CTL_RETVAL_COMPLETE); 6567 } 6568 6569 /* 6570 * Set the IO_CONT flag, so that if this I/O gets passed to 6571 * ctl_config_write_done(), it'll get passed back to 6572 * ctl_do_mode_select() for further processing, or completion if 6573 * we're all done. 6574 */ 6575 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6576 ctsio->io_cont = ctl_do_mode_select; 6577 6578 modepage_info = (union ctl_modepage_info *) 6579 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6580 6581 memset(modepage_info, 0, sizeof(*modepage_info)); 6582 6583 len_left = param_len - header_size - bd_len; 6584 len_used = header_size + bd_len; 6585 6586 modepage_info->header.len_left = len_left; 6587 modepage_info->header.len_used = len_used; 6588 6589 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6590} 6591 6592int 6593ctl_mode_sense(struct ctl_scsiio *ctsio) 6594{ 6595 struct ctl_lun *lun; 6596 int pc, page_code, dbd, llba, subpage; 6597 int alloc_len, page_len, header_len, total_len; 6598 struct scsi_mode_block_descr *block_desc; 6599 struct ctl_page_index *page_index; 6600 int control_dev; 6601 6602 dbd = 0; 6603 llba = 0; 6604 block_desc = NULL; 6605 page_index = NULL; 6606 6607 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6608 6609 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6610 6611 if (lun->be_lun->lun_type != T_DIRECT) 6612 control_dev = 1; 6613 else 6614 control_dev = 0; 6615 6616 switch (ctsio->cdb[0]) { 6617 case MODE_SENSE_6: { 6618 struct scsi_mode_sense_6 *cdb; 6619 6620 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6621 6622 header_len = sizeof(struct scsi_mode_hdr_6); 6623 if (cdb->byte2 & SMS_DBD) 6624 dbd = 1; 6625 else 6626 header_len += sizeof(struct scsi_mode_block_descr); 6627 6628 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6629 page_code = cdb->page & SMS_PAGE_CODE; 6630 subpage = cdb->subpage; 6631 alloc_len = cdb->length; 6632 break; 6633 } 6634 case MODE_SENSE_10: { 6635 struct scsi_mode_sense_10 *cdb; 6636 6637 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6638 6639 header_len = sizeof(struct scsi_mode_hdr_10); 6640 6641 if (cdb->byte2 & SMS_DBD) 6642 dbd = 1; 6643 else 6644 header_len += sizeof(struct scsi_mode_block_descr); 6645 if (cdb->byte2 & SMS10_LLBAA) 6646 llba = 1; 6647 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6648 page_code = cdb->page & SMS_PAGE_CODE; 6649 subpage = cdb->subpage; 6650 alloc_len = scsi_2btoul(cdb->length); 6651 break; 6652 } 6653 default: 6654 ctl_set_invalid_opcode(ctsio); 6655 ctl_done((union ctl_io *)ctsio); 6656 return (CTL_RETVAL_COMPLETE); 6657 break; /* NOTREACHED */ 6658 } 6659 6660 /* 6661 * We have to make a first pass through to calculate the size of 6662 * the pages that match the user's query. Then we allocate enough 6663 * memory to hold it, and actually copy the data into the buffer. 6664 */ 6665 switch (page_code) { 6666 case SMS_ALL_PAGES_PAGE: { 6667 int i; 6668 6669 page_len = 0; 6670 6671 /* 6672 * At the moment, values other than 0 and 0xff here are 6673 * reserved according to SPC-3. 6674 */ 6675 if ((subpage != SMS_SUBPAGE_PAGE_0) 6676 && (subpage != SMS_SUBPAGE_ALL)) { 6677 ctl_set_invalid_field(ctsio, 6678 /*sks_valid*/ 1, 6679 /*command*/ 1, 6680 /*field*/ 3, 6681 /*bit_valid*/ 0, 6682 /*bit*/ 0); 6683 ctl_done((union ctl_io *)ctsio); 6684 return (CTL_RETVAL_COMPLETE); 6685 } 6686 6687 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6688 if ((control_dev != 0) 6689 && (lun->mode_pages.index[i].page_flags & 6690 CTL_PAGE_FLAG_DISK_ONLY)) 6691 continue; 6692 6693 /* 6694 * We don't use this subpage if the user didn't 6695 * request all subpages. 6696 */ 6697 if ((lun->mode_pages.index[i].subpage != 0) 6698 && (subpage == SMS_SUBPAGE_PAGE_0)) 6699 continue; 6700 6701#if 0 6702 printf("found page %#x len %d\n", 6703 lun->mode_pages.index[i].page_code & 6704 SMPH_PC_MASK, 6705 lun->mode_pages.index[i].page_len); 6706#endif 6707 page_len += lun->mode_pages.index[i].page_len; 6708 } 6709 break; 6710 } 6711 default: { 6712 int i; 6713 6714 page_len = 0; 6715 6716 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6717 /* Look for the right page code */ 6718 if ((lun->mode_pages.index[i].page_code & 6719 SMPH_PC_MASK) != page_code) 6720 continue; 6721 6722 /* Look for the right subpage or the subpage wildcard*/ 6723 if ((lun->mode_pages.index[i].subpage != subpage) 6724 && (subpage != SMS_SUBPAGE_ALL)) 6725 continue; 6726 6727 /* Make sure the page is supported for this dev type */ 6728 if ((control_dev != 0) 6729 && (lun->mode_pages.index[i].page_flags & 6730 CTL_PAGE_FLAG_DISK_ONLY)) 6731 continue; 6732 6733#if 0 6734 printf("found page %#x len %d\n", 6735 lun->mode_pages.index[i].page_code & 6736 SMPH_PC_MASK, 6737 lun->mode_pages.index[i].page_len); 6738#endif 6739 6740 page_len += lun->mode_pages.index[i].page_len; 6741 } 6742 6743 if (page_len == 0) { 6744 ctl_set_invalid_field(ctsio, 6745 /*sks_valid*/ 1, 6746 /*command*/ 1, 6747 /*field*/ 2, 6748 /*bit_valid*/ 1, 6749 /*bit*/ 5); 6750 ctl_done((union ctl_io *)ctsio); 6751 return (CTL_RETVAL_COMPLETE); 6752 } 6753 break; 6754 } 6755 } 6756 6757 total_len = header_len + page_len; 6758#if 0 6759 printf("header_len = %d, page_len = %d, total_len = %d\n", 6760 header_len, page_len, total_len); 6761#endif 6762 6763 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6764 ctsio->kern_sg_entries = 0; 6765 ctsio->kern_data_resid = 0; 6766 ctsio->kern_rel_offset = 0; 6767 if (total_len < alloc_len) { 6768 ctsio->residual = alloc_len - total_len; 6769 ctsio->kern_data_len = total_len; 6770 ctsio->kern_total_len = total_len; 6771 } else { 6772 ctsio->residual = 0; 6773 ctsio->kern_data_len = alloc_len; 6774 ctsio->kern_total_len = alloc_len; 6775 } 6776 6777 switch (ctsio->cdb[0]) { 6778 case MODE_SENSE_6: { 6779 struct scsi_mode_hdr_6 *header; 6780 6781 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6782 6783 header->datalen = ctl_min(total_len - 1, 254); 6784 if (control_dev == 0) { 6785 header->dev_specific = 0x10; /* DPOFUA */ 6786 if ((lun->flags & CTL_LUN_READONLY) || 6787 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6788 .eca_and_aen & SCP_SWP) != 0) 6789 header->dev_specific |= 0x80; /* WP */ 6790 } 6791 if (dbd) 6792 header->block_descr_len = 0; 6793 else 6794 header->block_descr_len = 6795 sizeof(struct scsi_mode_block_descr); 6796 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6797 break; 6798 } 6799 case MODE_SENSE_10: { 6800 struct scsi_mode_hdr_10 *header; 6801 int datalen; 6802 6803 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6804 6805 datalen = ctl_min(total_len - 2, 65533); 6806 scsi_ulto2b(datalen, header->datalen); 6807 if (control_dev == 0) { 6808 header->dev_specific = 0x10; /* DPOFUA */ 6809 if ((lun->flags & CTL_LUN_READONLY) || 6810 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6811 .eca_and_aen & SCP_SWP) != 0) 6812 header->dev_specific |= 0x80; /* WP */ 6813 } 6814 if (dbd) 6815 scsi_ulto2b(0, header->block_descr_len); 6816 else 6817 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6818 header->block_descr_len); 6819 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6820 break; 6821 } 6822 default: 6823 panic("invalid CDB type %#x", ctsio->cdb[0]); 6824 break; /* NOTREACHED */ 6825 } 6826 6827 /* 6828 * If we've got a disk, use its blocksize in the block 6829 * descriptor. Otherwise, just set it to 0. 6830 */ 6831 if (dbd == 0) { 6832 if (control_dev == 0) 6833 scsi_ulto3b(lun->be_lun->blocksize, 6834 block_desc->block_len); 6835 else 6836 scsi_ulto3b(0, block_desc->block_len); 6837 } 6838 6839 switch (page_code) { 6840 case SMS_ALL_PAGES_PAGE: { 6841 int i, data_used; 6842 6843 data_used = header_len; 6844 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6845 struct ctl_page_index *page_index; 6846 6847 page_index = &lun->mode_pages.index[i]; 6848 6849 if ((control_dev != 0) 6850 && (page_index->page_flags & 6851 CTL_PAGE_FLAG_DISK_ONLY)) 6852 continue; 6853 6854 /* 6855 * We don't use this subpage if the user didn't 6856 * request all subpages. We already checked (above) 6857 * to make sure the user only specified a subpage 6858 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6859 */ 6860 if ((page_index->subpage != 0) 6861 && (subpage == SMS_SUBPAGE_PAGE_0)) 6862 continue; 6863 6864 /* 6865 * Call the handler, if it exists, to update the 6866 * page to the latest values. 6867 */ 6868 if (page_index->sense_handler != NULL) 6869 page_index->sense_handler(ctsio, page_index,pc); 6870 6871 memcpy(ctsio->kern_data_ptr + data_used, 6872 page_index->page_data + 6873 (page_index->page_len * pc), 6874 page_index->page_len); 6875 data_used += page_index->page_len; 6876 } 6877 break; 6878 } 6879 default: { 6880 int i, data_used; 6881 6882 data_used = header_len; 6883 6884 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6885 struct ctl_page_index *page_index; 6886 6887 page_index = &lun->mode_pages.index[i]; 6888 6889 /* Look for the right page code */ 6890 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6891 continue; 6892 6893 /* Look for the right subpage or the subpage wildcard*/ 6894 if ((page_index->subpage != subpage) 6895 && (subpage != SMS_SUBPAGE_ALL)) 6896 continue; 6897 6898 /* Make sure the page is supported for this dev type */ 6899 if ((control_dev != 0) 6900 && (page_index->page_flags & 6901 CTL_PAGE_FLAG_DISK_ONLY)) 6902 continue; 6903 6904 /* 6905 * Call the handler, if it exists, to update the 6906 * page to the latest values. 6907 */ 6908 if (page_index->sense_handler != NULL) 6909 page_index->sense_handler(ctsio, page_index,pc); 6910 6911 memcpy(ctsio->kern_data_ptr + data_used, 6912 page_index->page_data + 6913 (page_index->page_len * pc), 6914 page_index->page_len); 6915 data_used += page_index->page_len; 6916 } 6917 break; 6918 } 6919 } 6920 6921 ctl_set_success(ctsio); 6922 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6923 ctsio->be_move_done = ctl_config_move_done; 6924 ctl_datamove((union ctl_io *)ctsio); 6925 return (CTL_RETVAL_COMPLETE); 6926} 6927 6928int 6929ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6930 struct ctl_page_index *page_index, 6931 int pc) 6932{ 6933 struct ctl_lun *lun; 6934 struct scsi_log_param_header *phdr; 6935 uint8_t *data; 6936 uint64_t val; 6937 6938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6939 data = page_index->page_data; 6940 6941 if (lun->backend->lun_attr != NULL && 6942 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6943 != UINT64_MAX) { 6944 phdr = (struct scsi_log_param_header *)data; 6945 scsi_ulto2b(0x0001, phdr->param_code); 6946 phdr->param_control = SLP_LBIN | SLP_LP; 6947 phdr->param_len = 8; 6948 data = (uint8_t *)(phdr + 1); 6949 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6950 data[4] = 0x02; /* per-pool */ 6951 data += phdr->param_len; 6952 } 6953 6954 if (lun->backend->lun_attr != NULL && 6955 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6956 != UINT64_MAX) { 6957 phdr = (struct scsi_log_param_header *)data; 6958 scsi_ulto2b(0x0002, phdr->param_code); 6959 phdr->param_control = SLP_LBIN | SLP_LP; 6960 phdr->param_len = 8; 6961 data = (uint8_t *)(phdr + 1); 6962 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6963 data[4] = 0x01; /* per-LUN */ 6964 data += phdr->param_len; 6965 } 6966 6967 if (lun->backend->lun_attr != NULL && 6968 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6969 != UINT64_MAX) { 6970 phdr = (struct scsi_log_param_header *)data; 6971 scsi_ulto2b(0x00f1, phdr->param_code); 6972 phdr->param_control = SLP_LBIN | SLP_LP; 6973 phdr->param_len = 8; 6974 data = (uint8_t *)(phdr + 1); 6975 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6976 data[4] = 0x02; /* per-pool */ 6977 data += phdr->param_len; 6978 } 6979 6980 if (lun->backend->lun_attr != NULL && 6981 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6982 != UINT64_MAX) { 6983 phdr = (struct scsi_log_param_header *)data; 6984 scsi_ulto2b(0x00f2, phdr->param_code); 6985 phdr->param_control = SLP_LBIN | SLP_LP; 6986 phdr->param_len = 8; 6987 data = (uint8_t *)(phdr + 1); 6988 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6989 data[4] = 0x02; /* per-pool */ 6990 data += phdr->param_len; 6991 } 6992 6993 page_index->page_len = data - page_index->page_data; 6994 return (0); 6995} 6996 6997int 6998ctl_log_sense(struct ctl_scsiio *ctsio) 6999{ 7000 struct ctl_lun *lun; 7001 int i, pc, page_code, subpage; 7002 int alloc_len, total_len; 7003 struct ctl_page_index *page_index; 7004 struct scsi_log_sense *cdb; 7005 struct scsi_log_header *header; 7006 7007 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7008 7009 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7010 cdb = (struct scsi_log_sense *)ctsio->cdb; 7011 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7012 page_code = cdb->page & SLS_PAGE_CODE; 7013 subpage = cdb->subpage; 7014 alloc_len = scsi_2btoul(cdb->length); 7015 7016 page_index = NULL; 7017 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7018 page_index = &lun->log_pages.index[i]; 7019 7020 /* Look for the right page code */ 7021 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7022 continue; 7023 7024 /* Look for the right subpage or the subpage wildcard*/ 7025 if (page_index->subpage != subpage) 7026 continue; 7027 7028 break; 7029 } 7030 if (i >= CTL_NUM_LOG_PAGES) { 7031 ctl_set_invalid_field(ctsio, 7032 /*sks_valid*/ 1, 7033 /*command*/ 1, 7034 /*field*/ 2, 7035 /*bit_valid*/ 0, 7036 /*bit*/ 0); 7037 ctl_done((union ctl_io *)ctsio); 7038 return (CTL_RETVAL_COMPLETE); 7039 } 7040 7041 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7042 7043 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7044 ctsio->kern_sg_entries = 0; 7045 ctsio->kern_data_resid = 0; 7046 ctsio->kern_rel_offset = 0; 7047 if (total_len < alloc_len) { 7048 ctsio->residual = alloc_len - total_len; 7049 ctsio->kern_data_len = total_len; 7050 ctsio->kern_total_len = total_len; 7051 } else { 7052 ctsio->residual = 0; 7053 ctsio->kern_data_len = alloc_len; 7054 ctsio->kern_total_len = alloc_len; 7055 } 7056 7057 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7058 header->page = page_index->page_code; 7059 if (page_index->subpage) { 7060 header->page |= SL_SPF; 7061 header->subpage = page_index->subpage; 7062 } 7063 scsi_ulto2b(page_index->page_len, header->datalen); 7064 7065 /* 7066 * Call the handler, if it exists, to update the 7067 * page to the latest values. 7068 */ 7069 if (page_index->sense_handler != NULL) 7070 page_index->sense_handler(ctsio, page_index, pc); 7071 7072 memcpy(header + 1, page_index->page_data, page_index->page_len); 7073 7074 ctl_set_success(ctsio); 7075 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7076 ctsio->be_move_done = ctl_config_move_done; 7077 ctl_datamove((union ctl_io *)ctsio); 7078 return (CTL_RETVAL_COMPLETE); 7079} 7080 7081int 7082ctl_read_capacity(struct ctl_scsiio *ctsio) 7083{ 7084 struct scsi_read_capacity *cdb; 7085 struct scsi_read_capacity_data *data; 7086 struct ctl_lun *lun; 7087 uint32_t lba; 7088 7089 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7090 7091 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7092 7093 lba = scsi_4btoul(cdb->addr); 7094 if (((cdb->pmi & SRC_PMI) == 0) 7095 && (lba != 0)) { 7096 ctl_set_invalid_field(/*ctsio*/ ctsio, 7097 /*sks_valid*/ 1, 7098 /*command*/ 1, 7099 /*field*/ 2, 7100 /*bit_valid*/ 0, 7101 /*bit*/ 0); 7102 ctl_done((union ctl_io *)ctsio); 7103 return (CTL_RETVAL_COMPLETE); 7104 } 7105 7106 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7107 7108 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7109 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7110 ctsio->residual = 0; 7111 ctsio->kern_data_len = sizeof(*data); 7112 ctsio->kern_total_len = sizeof(*data); 7113 ctsio->kern_data_resid = 0; 7114 ctsio->kern_rel_offset = 0; 7115 ctsio->kern_sg_entries = 0; 7116 7117 /* 7118 * If the maximum LBA is greater than 0xfffffffe, the user must 7119 * issue a SERVICE ACTION IN (16) command, with the read capacity 7120 * serivce action set. 7121 */ 7122 if (lun->be_lun->maxlba > 0xfffffffe) 7123 scsi_ulto4b(0xffffffff, data->addr); 7124 else 7125 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7126 7127 /* 7128 * XXX KDM this may not be 512 bytes... 7129 */ 7130 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7131 7132 ctl_set_success(ctsio); 7133 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7134 ctsio->be_move_done = ctl_config_move_done; 7135 ctl_datamove((union ctl_io *)ctsio); 7136 return (CTL_RETVAL_COMPLETE); 7137} 7138 7139int 7140ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7141{ 7142 struct scsi_read_capacity_16 *cdb; 7143 struct scsi_read_capacity_data_long *data; 7144 struct ctl_lun *lun; 7145 uint64_t lba; 7146 uint32_t alloc_len; 7147 7148 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7149 7150 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7151 7152 alloc_len = scsi_4btoul(cdb->alloc_len); 7153 lba = scsi_8btou64(cdb->addr); 7154 7155 if ((cdb->reladr & SRC16_PMI) 7156 && (lba != 0)) { 7157 ctl_set_invalid_field(/*ctsio*/ ctsio, 7158 /*sks_valid*/ 1, 7159 /*command*/ 1, 7160 /*field*/ 2, 7161 /*bit_valid*/ 0, 7162 /*bit*/ 0); 7163 ctl_done((union ctl_io *)ctsio); 7164 return (CTL_RETVAL_COMPLETE); 7165 } 7166 7167 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7168 7169 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7170 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7171 7172 if (sizeof(*data) < alloc_len) { 7173 ctsio->residual = alloc_len - sizeof(*data); 7174 ctsio->kern_data_len = sizeof(*data); 7175 ctsio->kern_total_len = sizeof(*data); 7176 } else { 7177 ctsio->residual = 0; 7178 ctsio->kern_data_len = alloc_len; 7179 ctsio->kern_total_len = alloc_len; 7180 } 7181 ctsio->kern_data_resid = 0; 7182 ctsio->kern_rel_offset = 0; 7183 ctsio->kern_sg_entries = 0; 7184 7185 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7186 /* XXX KDM this may not be 512 bytes... */ 7187 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7188 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7189 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7190 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7191 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7192 7193 ctl_set_success(ctsio); 7194 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7195 ctsio->be_move_done = ctl_config_move_done; 7196 ctl_datamove((union ctl_io *)ctsio); 7197 return (CTL_RETVAL_COMPLETE); 7198} 7199 7200int 7201ctl_read_defect(struct ctl_scsiio *ctsio) 7202{ 7203 struct scsi_read_defect_data_10 *ccb10; 7204 struct scsi_read_defect_data_12 *ccb12; 7205 struct scsi_read_defect_data_hdr_10 *data10; 7206 struct scsi_read_defect_data_hdr_12 *data12; 7207 uint32_t alloc_len, data_len; 7208 uint8_t format; 7209 7210 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7211 7212 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7213 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7214 format = ccb10->format; 7215 alloc_len = scsi_2btoul(ccb10->alloc_length); 7216 data_len = sizeof(*data10); 7217 } else { 7218 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7219 format = ccb12->format; 7220 alloc_len = scsi_4btoul(ccb12->alloc_length); 7221 data_len = sizeof(*data12); 7222 } 7223 if (alloc_len == 0) { 7224 ctl_set_success(ctsio); 7225 ctl_done((union ctl_io *)ctsio); 7226 return (CTL_RETVAL_COMPLETE); 7227 } 7228 7229 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7230 if (data_len < alloc_len) { 7231 ctsio->residual = alloc_len - data_len; 7232 ctsio->kern_data_len = data_len; 7233 ctsio->kern_total_len = data_len; 7234 } else { 7235 ctsio->residual = 0; 7236 ctsio->kern_data_len = alloc_len; 7237 ctsio->kern_total_len = alloc_len; 7238 } 7239 ctsio->kern_data_resid = 0; 7240 ctsio->kern_rel_offset = 0; 7241 ctsio->kern_sg_entries = 0; 7242 7243 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7244 data10 = (struct scsi_read_defect_data_hdr_10 *) 7245 ctsio->kern_data_ptr; 7246 data10->format = format; 7247 scsi_ulto2b(0, data10->length); 7248 } else { 7249 data12 = (struct scsi_read_defect_data_hdr_12 *) 7250 ctsio->kern_data_ptr; 7251 data12->format = format; 7252 scsi_ulto2b(0, data12->generation); 7253 scsi_ulto4b(0, data12->length); 7254 } 7255 7256 ctl_set_success(ctsio); 7257 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7258 ctsio->be_move_done = ctl_config_move_done; 7259 ctl_datamove((union ctl_io *)ctsio); 7260 return (CTL_RETVAL_COMPLETE); 7261} 7262 7263int 7264ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7265{ 7266 struct scsi_maintenance_in *cdb; 7267 int retval; 7268 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7269 int num_target_port_groups, num_target_ports; 7270 struct ctl_lun *lun; 7271 struct ctl_softc *softc; 7272 struct ctl_port *port; 7273 struct scsi_target_group_data *rtg_ptr; 7274 struct scsi_target_group_data_extended *rtg_ext_ptr; 7275 struct scsi_target_port_group_descriptor *tpg_desc; 7276 7277 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7278 7279 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7280 softc = control_softc; 7281 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7282 7283 retval = CTL_RETVAL_COMPLETE; 7284 7285 switch (cdb->byte2 & STG_PDF_MASK) { 7286 case STG_PDF_LENGTH: 7287 ext = 0; 7288 break; 7289 case STG_PDF_EXTENDED: 7290 ext = 1; 7291 break; 7292 default: 7293 ctl_set_invalid_field(/*ctsio*/ ctsio, 7294 /*sks_valid*/ 1, 7295 /*command*/ 1, 7296 /*field*/ 2, 7297 /*bit_valid*/ 1, 7298 /*bit*/ 5); 7299 ctl_done((union ctl_io *)ctsio); 7300 return(retval); 7301 } 7302 7303 if (softc->is_single) 7304 num_target_port_groups = 1; 7305 else 7306 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7307 num_target_ports = 0; 7308 mtx_lock(&softc->ctl_lock); 7309 STAILQ_FOREACH(port, &softc->port_list, links) { 7310 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7311 continue; 7312 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7313 continue; 7314 num_target_ports++; 7315 } 7316 mtx_unlock(&softc->ctl_lock); 7317 7318 if (ext) 7319 total_len = sizeof(struct scsi_target_group_data_extended); 7320 else 7321 total_len = sizeof(struct scsi_target_group_data); 7322 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7323 num_target_port_groups + 7324 sizeof(struct scsi_target_port_descriptor) * 7325 num_target_ports * num_target_port_groups; 7326 7327 alloc_len = scsi_4btoul(cdb->length); 7328 7329 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7330 7331 ctsio->kern_sg_entries = 0; 7332 7333 if (total_len < alloc_len) { 7334 ctsio->residual = alloc_len - total_len; 7335 ctsio->kern_data_len = total_len; 7336 ctsio->kern_total_len = total_len; 7337 } else { 7338 ctsio->residual = 0; 7339 ctsio->kern_data_len = alloc_len; 7340 ctsio->kern_total_len = alloc_len; 7341 } 7342 ctsio->kern_data_resid = 0; 7343 ctsio->kern_rel_offset = 0; 7344 7345 if (ext) { 7346 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7347 ctsio->kern_data_ptr; 7348 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7349 rtg_ext_ptr->format_type = 0x10; 7350 rtg_ext_ptr->implicit_transition_time = 0; 7351 tpg_desc = &rtg_ext_ptr->groups[0]; 7352 } else { 7353 rtg_ptr = (struct scsi_target_group_data *) 7354 ctsio->kern_data_ptr; 7355 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7356 tpg_desc = &rtg_ptr->groups[0]; 7357 } 7358 7359 mtx_lock(&softc->ctl_lock); 7360 pg = softc->port_offset / CTL_MAX_PORTS; 7361 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7362 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7363 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7364 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7365 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7366 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7367 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7368 } else { 7369 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7370 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7371 } 7372 } else { 7373 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7374 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7375 } 7376 for (g = 0; g < num_target_port_groups; g++) { 7377 tpg_desc->pref_state = (g == pg) ? gs : os; 7378 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7379 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7380 tpg_desc->status = TPG_IMPLICIT; 7381 pc = 0; 7382 STAILQ_FOREACH(port, &softc->port_list, links) { 7383 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7384 continue; 7385 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7386 CTL_MAX_LUNS) 7387 continue; 7388 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7389 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7390 relative_target_port_identifier); 7391 pc++; 7392 } 7393 tpg_desc->target_port_count = pc; 7394 tpg_desc = (struct scsi_target_port_group_descriptor *) 7395 &tpg_desc->descriptors[pc]; 7396 } 7397 mtx_unlock(&softc->ctl_lock); 7398 7399 ctl_set_success(ctsio); 7400 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7401 ctsio->be_move_done = ctl_config_move_done; 7402 ctl_datamove((union ctl_io *)ctsio); 7403 return(retval); 7404} 7405 7406int 7407ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7408{ 7409 struct ctl_lun *lun; 7410 struct scsi_report_supported_opcodes *cdb; 7411 const struct ctl_cmd_entry *entry, *sentry; 7412 struct scsi_report_supported_opcodes_all *all; 7413 struct scsi_report_supported_opcodes_descr *descr; 7414 struct scsi_report_supported_opcodes_one *one; 7415 int retval; 7416 int alloc_len, total_len; 7417 int opcode, service_action, i, j, num; 7418 7419 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7420 7421 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7422 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7423 7424 retval = CTL_RETVAL_COMPLETE; 7425 7426 opcode = cdb->requested_opcode; 7427 service_action = scsi_2btoul(cdb->requested_service_action); 7428 switch (cdb->options & RSO_OPTIONS_MASK) { 7429 case RSO_OPTIONS_ALL: 7430 num = 0; 7431 for (i = 0; i < 256; i++) { 7432 entry = &ctl_cmd_table[i]; 7433 if (entry->flags & CTL_CMD_FLAG_SA5) { 7434 for (j = 0; j < 32; j++) { 7435 sentry = &((const struct ctl_cmd_entry *) 7436 entry->execute)[j]; 7437 if (ctl_cmd_applicable( 7438 lun->be_lun->lun_type, sentry)) 7439 num++; 7440 } 7441 } else { 7442 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7443 entry)) 7444 num++; 7445 } 7446 } 7447 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7448 num * sizeof(struct scsi_report_supported_opcodes_descr); 7449 break; 7450 case RSO_OPTIONS_OC: 7451 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7452 ctl_set_invalid_field(/*ctsio*/ ctsio, 7453 /*sks_valid*/ 1, 7454 /*command*/ 1, 7455 /*field*/ 2, 7456 /*bit_valid*/ 1, 7457 /*bit*/ 2); 7458 ctl_done((union ctl_io *)ctsio); 7459 return (CTL_RETVAL_COMPLETE); 7460 } 7461 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7462 break; 7463 case RSO_OPTIONS_OC_SA: 7464 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7465 service_action >= 32) { 7466 ctl_set_invalid_field(/*ctsio*/ ctsio, 7467 /*sks_valid*/ 1, 7468 /*command*/ 1, 7469 /*field*/ 2, 7470 /*bit_valid*/ 1, 7471 /*bit*/ 2); 7472 ctl_done((union ctl_io *)ctsio); 7473 return (CTL_RETVAL_COMPLETE); 7474 } 7475 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7476 break; 7477 default: 7478 ctl_set_invalid_field(/*ctsio*/ ctsio, 7479 /*sks_valid*/ 1, 7480 /*command*/ 1, 7481 /*field*/ 2, 7482 /*bit_valid*/ 1, 7483 /*bit*/ 2); 7484 ctl_done((union ctl_io *)ctsio); 7485 return (CTL_RETVAL_COMPLETE); 7486 } 7487 7488 alloc_len = scsi_4btoul(cdb->length); 7489 7490 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7491 7492 ctsio->kern_sg_entries = 0; 7493 7494 if (total_len < alloc_len) { 7495 ctsio->residual = alloc_len - total_len; 7496 ctsio->kern_data_len = total_len; 7497 ctsio->kern_total_len = total_len; 7498 } else { 7499 ctsio->residual = 0; 7500 ctsio->kern_data_len = alloc_len; 7501 ctsio->kern_total_len = alloc_len; 7502 } 7503 ctsio->kern_data_resid = 0; 7504 ctsio->kern_rel_offset = 0; 7505 7506 switch (cdb->options & RSO_OPTIONS_MASK) { 7507 case RSO_OPTIONS_ALL: 7508 all = (struct scsi_report_supported_opcodes_all *) 7509 ctsio->kern_data_ptr; 7510 num = 0; 7511 for (i = 0; i < 256; i++) { 7512 entry = &ctl_cmd_table[i]; 7513 if (entry->flags & CTL_CMD_FLAG_SA5) { 7514 for (j = 0; j < 32; j++) { 7515 sentry = &((const struct ctl_cmd_entry *) 7516 entry->execute)[j]; 7517 if (!ctl_cmd_applicable( 7518 lun->be_lun->lun_type, sentry)) 7519 continue; 7520 descr = &all->descr[num++]; 7521 descr->opcode = i; 7522 scsi_ulto2b(j, descr->service_action); 7523 descr->flags = RSO_SERVACTV; 7524 scsi_ulto2b(sentry->length, 7525 descr->cdb_length); 7526 } 7527 } else { 7528 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7529 entry)) 7530 continue; 7531 descr = &all->descr[num++]; 7532 descr->opcode = i; 7533 scsi_ulto2b(0, descr->service_action); 7534 descr->flags = 0; 7535 scsi_ulto2b(entry->length, descr->cdb_length); 7536 } 7537 } 7538 scsi_ulto4b( 7539 num * sizeof(struct scsi_report_supported_opcodes_descr), 7540 all->length); 7541 break; 7542 case RSO_OPTIONS_OC: 7543 one = (struct scsi_report_supported_opcodes_one *) 7544 ctsio->kern_data_ptr; 7545 entry = &ctl_cmd_table[opcode]; 7546 goto fill_one; 7547 case RSO_OPTIONS_OC_SA: 7548 one = (struct scsi_report_supported_opcodes_one *) 7549 ctsio->kern_data_ptr; 7550 entry = &ctl_cmd_table[opcode]; 7551 entry = &((const struct ctl_cmd_entry *) 7552 entry->execute)[service_action]; 7553fill_one: 7554 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7555 one->support = 3; 7556 scsi_ulto2b(entry->length, one->cdb_length); 7557 one->cdb_usage[0] = opcode; 7558 memcpy(&one->cdb_usage[1], entry->usage, 7559 entry->length - 1); 7560 } else 7561 one->support = 1; 7562 break; 7563 } 7564 7565 ctl_set_success(ctsio); 7566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7567 ctsio->be_move_done = ctl_config_move_done; 7568 ctl_datamove((union ctl_io *)ctsio); 7569 return(retval); 7570} 7571 7572int 7573ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7574{ 7575 struct scsi_report_supported_tmf *cdb; 7576 struct scsi_report_supported_tmf_data *data; 7577 int retval; 7578 int alloc_len, total_len; 7579 7580 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7581 7582 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7583 7584 retval = CTL_RETVAL_COMPLETE; 7585 7586 total_len = sizeof(struct scsi_report_supported_tmf_data); 7587 alloc_len = scsi_4btoul(cdb->length); 7588 7589 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7590 7591 ctsio->kern_sg_entries = 0; 7592 7593 if (total_len < alloc_len) { 7594 ctsio->residual = alloc_len - total_len; 7595 ctsio->kern_data_len = total_len; 7596 ctsio->kern_total_len = total_len; 7597 } else { 7598 ctsio->residual = 0; 7599 ctsio->kern_data_len = alloc_len; 7600 ctsio->kern_total_len = alloc_len; 7601 } 7602 ctsio->kern_data_resid = 0; 7603 ctsio->kern_rel_offset = 0; 7604 7605 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7606 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7607 data->byte2 |= RST_ITNRS; 7608 7609 ctl_set_success(ctsio); 7610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7611 ctsio->be_move_done = ctl_config_move_done; 7612 ctl_datamove((union ctl_io *)ctsio); 7613 return (retval); 7614} 7615 7616int 7617ctl_report_timestamp(struct ctl_scsiio *ctsio) 7618{ 7619 struct scsi_report_timestamp *cdb; 7620 struct scsi_report_timestamp_data *data; 7621 struct timeval tv; 7622 int64_t timestamp; 7623 int retval; 7624 int alloc_len, total_len; 7625 7626 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7627 7628 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7629 7630 retval = CTL_RETVAL_COMPLETE; 7631 7632 total_len = sizeof(struct scsi_report_timestamp_data); 7633 alloc_len = scsi_4btoul(cdb->length); 7634 7635 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7636 7637 ctsio->kern_sg_entries = 0; 7638 7639 if (total_len < alloc_len) { 7640 ctsio->residual = alloc_len - total_len; 7641 ctsio->kern_data_len = total_len; 7642 ctsio->kern_total_len = total_len; 7643 } else { 7644 ctsio->residual = 0; 7645 ctsio->kern_data_len = alloc_len; 7646 ctsio->kern_total_len = alloc_len; 7647 } 7648 ctsio->kern_data_resid = 0; 7649 ctsio->kern_rel_offset = 0; 7650 7651 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7652 scsi_ulto2b(sizeof(*data) - 2, data->length); 7653 data->origin = RTS_ORIG_OUTSIDE; 7654 getmicrotime(&tv); 7655 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7656 scsi_ulto4b(timestamp >> 16, data->timestamp); 7657 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7658 7659 ctl_set_success(ctsio); 7660 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7661 ctsio->be_move_done = ctl_config_move_done; 7662 ctl_datamove((union ctl_io *)ctsio); 7663 return (retval); 7664} 7665 7666int 7667ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7668{ 7669 struct scsi_per_res_in *cdb; 7670 int alloc_len, total_len = 0; 7671 /* struct scsi_per_res_in_rsrv in_data; */ 7672 struct ctl_lun *lun; 7673 struct ctl_softc *softc; 7674 uint64_t key; 7675 7676 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7677 7678 softc = control_softc; 7679 7680 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7681 7682 alloc_len = scsi_2btoul(cdb->length); 7683 7684 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7685 7686retry: 7687 mtx_lock(&lun->lun_lock); 7688 switch (cdb->action) { 7689 case SPRI_RK: /* read keys */ 7690 total_len = sizeof(struct scsi_per_res_in_keys) + 7691 lun->pr_key_count * 7692 sizeof(struct scsi_per_res_key); 7693 break; 7694 case SPRI_RR: /* read reservation */ 7695 if (lun->flags & CTL_LUN_PR_RESERVED) 7696 total_len = sizeof(struct scsi_per_res_in_rsrv); 7697 else 7698 total_len = sizeof(struct scsi_per_res_in_header); 7699 break; 7700 case SPRI_RC: /* report capabilities */ 7701 total_len = sizeof(struct scsi_per_res_cap); 7702 break; 7703 case SPRI_RS: /* read full status */ 7704 total_len = sizeof(struct scsi_per_res_in_header) + 7705 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7706 lun->pr_key_count; 7707 break; 7708 default: 7709 panic("Invalid PR type %x", cdb->action); 7710 } 7711 mtx_unlock(&lun->lun_lock); 7712 7713 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7714 7715 if (total_len < alloc_len) { 7716 ctsio->residual = alloc_len - total_len; 7717 ctsio->kern_data_len = total_len; 7718 ctsio->kern_total_len = total_len; 7719 } else { 7720 ctsio->residual = 0; 7721 ctsio->kern_data_len = alloc_len; 7722 ctsio->kern_total_len = alloc_len; 7723 } 7724 7725 ctsio->kern_data_resid = 0; 7726 ctsio->kern_rel_offset = 0; 7727 ctsio->kern_sg_entries = 0; 7728 7729 mtx_lock(&lun->lun_lock); 7730 switch (cdb->action) { 7731 case SPRI_RK: { // read keys 7732 struct scsi_per_res_in_keys *res_keys; 7733 int i, key_count; 7734 7735 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7736 7737 /* 7738 * We had to drop the lock to allocate our buffer, which 7739 * leaves time for someone to come in with another 7740 * persistent reservation. (That is unlikely, though, 7741 * since this should be the only persistent reservation 7742 * command active right now.) 7743 */ 7744 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7745 (lun->pr_key_count * 7746 sizeof(struct scsi_per_res_key)))){ 7747 mtx_unlock(&lun->lun_lock); 7748 free(ctsio->kern_data_ptr, M_CTL); 7749 printf("%s: reservation length changed, retrying\n", 7750 __func__); 7751 goto retry; 7752 } 7753 7754 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7755 7756 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7757 lun->pr_key_count, res_keys->header.length); 7758 7759 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7760 if ((key = ctl_get_prkey(lun, i)) == 0) 7761 continue; 7762 7763 /* 7764 * We used lun->pr_key_count to calculate the 7765 * size to allocate. If it turns out the number of 7766 * initiators with the registered flag set is 7767 * larger than that (i.e. they haven't been kept in 7768 * sync), we've got a problem. 7769 */ 7770 if (key_count >= lun->pr_key_count) { 7771#ifdef NEEDTOPORT 7772 csevent_log(CSC_CTL | CSC_SHELF_SW | 7773 CTL_PR_ERROR, 7774 csevent_LogType_Fault, 7775 csevent_AlertLevel_Yellow, 7776 csevent_FRU_ShelfController, 7777 csevent_FRU_Firmware, 7778 csevent_FRU_Unknown, 7779 "registered keys %d >= key " 7780 "count %d", key_count, 7781 lun->pr_key_count); 7782#endif 7783 key_count++; 7784 continue; 7785 } 7786 scsi_u64to8b(key, res_keys->keys[key_count].key); 7787 key_count++; 7788 } 7789 break; 7790 } 7791 case SPRI_RR: { // read reservation 7792 struct scsi_per_res_in_rsrv *res; 7793 int tmp_len, header_only; 7794 7795 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7796 7797 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7798 7799 if (lun->flags & CTL_LUN_PR_RESERVED) 7800 { 7801 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7802 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7803 res->header.length); 7804 header_only = 0; 7805 } else { 7806 tmp_len = sizeof(struct scsi_per_res_in_header); 7807 scsi_ulto4b(0, res->header.length); 7808 header_only = 1; 7809 } 7810 7811 /* 7812 * We had to drop the lock to allocate our buffer, which 7813 * leaves time for someone to come in with another 7814 * persistent reservation. (That is unlikely, though, 7815 * since this should be the only persistent reservation 7816 * command active right now.) 7817 */ 7818 if (tmp_len != total_len) { 7819 mtx_unlock(&lun->lun_lock); 7820 free(ctsio->kern_data_ptr, M_CTL); 7821 printf("%s: reservation status changed, retrying\n", 7822 __func__); 7823 goto retry; 7824 } 7825 7826 /* 7827 * No reservation held, so we're done. 7828 */ 7829 if (header_only != 0) 7830 break; 7831 7832 /* 7833 * If the registration is an All Registrants type, the key 7834 * is 0, since it doesn't really matter. 7835 */ 7836 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7837 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7838 res->data.reservation); 7839 } 7840 res->data.scopetype = lun->res_type; 7841 break; 7842 } 7843 case SPRI_RC: //report capabilities 7844 { 7845 struct scsi_per_res_cap *res_cap; 7846 uint16_t type_mask; 7847 7848 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7849 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7850 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7851 type_mask = SPRI_TM_WR_EX_AR | 7852 SPRI_TM_EX_AC_RO | 7853 SPRI_TM_WR_EX_RO | 7854 SPRI_TM_EX_AC | 7855 SPRI_TM_WR_EX | 7856 SPRI_TM_EX_AC_AR; 7857 scsi_ulto2b(type_mask, res_cap->type_mask); 7858 break; 7859 } 7860 case SPRI_RS: { // read full status 7861 struct scsi_per_res_in_full *res_status; 7862 struct scsi_per_res_in_full_desc *res_desc; 7863 struct ctl_port *port; 7864 int i, len; 7865 7866 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7867 7868 /* 7869 * We had to drop the lock to allocate our buffer, which 7870 * leaves time for someone to come in with another 7871 * persistent reservation. (That is unlikely, though, 7872 * since this should be the only persistent reservation 7873 * command active right now.) 7874 */ 7875 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7876 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7877 lun->pr_key_count)){ 7878 mtx_unlock(&lun->lun_lock); 7879 free(ctsio->kern_data_ptr, M_CTL); 7880 printf("%s: reservation length changed, retrying\n", 7881 __func__); 7882 goto retry; 7883 } 7884 7885 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7886 7887 res_desc = &res_status->desc[0]; 7888 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7889 if ((key = ctl_get_prkey(lun, i)) == 0) 7890 continue; 7891 7892 scsi_u64to8b(key, res_desc->res_key.key); 7893 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7894 (lun->pr_res_idx == i || 7895 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7896 res_desc->flags = SPRI_FULL_R_HOLDER; 7897 res_desc->scopetype = lun->res_type; 7898 } 7899 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7900 res_desc->rel_trgt_port_id); 7901 len = 0; 7902 port = softc->ctl_ports[ 7903 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7904 if (port != NULL) 7905 len = ctl_create_iid(port, 7906 i % CTL_MAX_INIT_PER_PORT, 7907 res_desc->transport_id); 7908 scsi_ulto4b(len, res_desc->additional_length); 7909 res_desc = (struct scsi_per_res_in_full_desc *) 7910 &res_desc->transport_id[len]; 7911 } 7912 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7913 res_status->header.length); 7914 break; 7915 } 7916 default: 7917 /* 7918 * This is a bug, because we just checked for this above, 7919 * and should have returned an error. 7920 */ 7921 panic("Invalid PR type %x", cdb->action); 7922 break; /* NOTREACHED */ 7923 } 7924 mtx_unlock(&lun->lun_lock); 7925 7926 ctl_set_success(ctsio); 7927 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7928 ctsio->be_move_done = ctl_config_move_done; 7929 ctl_datamove((union ctl_io *)ctsio); 7930 return (CTL_RETVAL_COMPLETE); 7931} 7932 7933static void 7934ctl_set_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 7935{ 7936 int off = lun->ctl_softc->persis_offset; 7937 7938 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 7939 lun->pending_ua[residx - off] |= ua; 7940} 7941 7942/* 7943 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7944 * it should return. 7945 */ 7946static int 7947ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7948 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7949 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7950 struct scsi_per_res_out_parms* param) 7951{ 7952 union ctl_ha_msg persis_io; 7953 int retval, i; 7954 int isc_retval; 7955 7956 retval = 0; 7957 7958 mtx_lock(&lun->lun_lock); 7959 if (sa_res_key == 0) { 7960 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7961 /* validate scope and type */ 7962 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7963 SPR_LU_SCOPE) { 7964 mtx_unlock(&lun->lun_lock); 7965 ctl_set_invalid_field(/*ctsio*/ ctsio, 7966 /*sks_valid*/ 1, 7967 /*command*/ 1, 7968 /*field*/ 2, 7969 /*bit_valid*/ 1, 7970 /*bit*/ 4); 7971 ctl_done((union ctl_io *)ctsio); 7972 return (1); 7973 } 7974 7975 if (type>8 || type==2 || type==4 || type==0) { 7976 mtx_unlock(&lun->lun_lock); 7977 ctl_set_invalid_field(/*ctsio*/ ctsio, 7978 /*sks_valid*/ 1, 7979 /*command*/ 1, 7980 /*field*/ 2, 7981 /*bit_valid*/ 1, 7982 /*bit*/ 0); 7983 ctl_done((union ctl_io *)ctsio); 7984 return (1); 7985 } 7986 7987 /* 7988 * Unregister everybody else and build UA for 7989 * them 7990 */ 7991 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7992 if (i == residx || ctl_get_prkey(lun, i) == 0) 7993 continue; 7994 7995 ctl_clr_prkey(lun, i); 7996 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7997 } 7998 lun->pr_key_count = 1; 7999 lun->res_type = type; 8000 if (lun->res_type != SPR_TYPE_WR_EX_AR 8001 && lun->res_type != SPR_TYPE_EX_AC_AR) 8002 lun->pr_res_idx = residx; 8003 8004 /* send msg to other side */ 8005 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8006 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8007 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8008 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8009 persis_io.pr.pr_info.res_type = type; 8010 memcpy(persis_io.pr.pr_info.sa_res_key, 8011 param->serv_act_res_key, 8012 sizeof(param->serv_act_res_key)); 8013 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8014 &persis_io, sizeof(persis_io), 0)) > 8015 CTL_HA_STATUS_SUCCESS) { 8016 printf("CTL:Persis Out error returned " 8017 "from ctl_ha_msg_send %d\n", 8018 isc_retval); 8019 } 8020 } else { 8021 /* not all registrants */ 8022 mtx_unlock(&lun->lun_lock); 8023 free(ctsio->kern_data_ptr, M_CTL); 8024 ctl_set_invalid_field(ctsio, 8025 /*sks_valid*/ 1, 8026 /*command*/ 0, 8027 /*field*/ 8, 8028 /*bit_valid*/ 0, 8029 /*bit*/ 0); 8030 ctl_done((union ctl_io *)ctsio); 8031 return (1); 8032 } 8033 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8034 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8035 int found = 0; 8036 8037 if (res_key == sa_res_key) { 8038 /* special case */ 8039 /* 8040 * The spec implies this is not good but doesn't 8041 * say what to do. There are two choices either 8042 * generate a res conflict or check condition 8043 * with illegal field in parameter data. Since 8044 * that is what is done when the sa_res_key is 8045 * zero I'll take that approach since this has 8046 * to do with the sa_res_key. 8047 */ 8048 mtx_unlock(&lun->lun_lock); 8049 free(ctsio->kern_data_ptr, M_CTL); 8050 ctl_set_invalid_field(ctsio, 8051 /*sks_valid*/ 1, 8052 /*command*/ 0, 8053 /*field*/ 8, 8054 /*bit_valid*/ 0, 8055 /*bit*/ 0); 8056 ctl_done((union ctl_io *)ctsio); 8057 return (1); 8058 } 8059 8060 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8061 if (ctl_get_prkey(lun, i) != sa_res_key) 8062 continue; 8063 8064 found = 1; 8065 ctl_clr_prkey(lun, i); 8066 lun->pr_key_count--; 8067 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8068 } 8069 if (!found) { 8070 mtx_unlock(&lun->lun_lock); 8071 free(ctsio->kern_data_ptr, M_CTL); 8072 ctl_set_reservation_conflict(ctsio); 8073 ctl_done((union ctl_io *)ctsio); 8074 return (CTL_RETVAL_COMPLETE); 8075 } 8076 /* send msg to other side */ 8077 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8078 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8079 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8080 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8081 persis_io.pr.pr_info.res_type = type; 8082 memcpy(persis_io.pr.pr_info.sa_res_key, 8083 param->serv_act_res_key, 8084 sizeof(param->serv_act_res_key)); 8085 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8086 &persis_io, sizeof(persis_io), 0)) > 8087 CTL_HA_STATUS_SUCCESS) { 8088 printf("CTL:Persis Out error returned from " 8089 "ctl_ha_msg_send %d\n", isc_retval); 8090 } 8091 } else { 8092 /* Reserved but not all registrants */ 8093 /* sa_res_key is res holder */ 8094 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8095 /* validate scope and type */ 8096 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8097 SPR_LU_SCOPE) { 8098 mtx_unlock(&lun->lun_lock); 8099 ctl_set_invalid_field(/*ctsio*/ ctsio, 8100 /*sks_valid*/ 1, 8101 /*command*/ 1, 8102 /*field*/ 2, 8103 /*bit_valid*/ 1, 8104 /*bit*/ 4); 8105 ctl_done((union ctl_io *)ctsio); 8106 return (1); 8107 } 8108 8109 if (type>8 || type==2 || type==4 || type==0) { 8110 mtx_unlock(&lun->lun_lock); 8111 ctl_set_invalid_field(/*ctsio*/ ctsio, 8112 /*sks_valid*/ 1, 8113 /*command*/ 1, 8114 /*field*/ 2, 8115 /*bit_valid*/ 1, 8116 /*bit*/ 0); 8117 ctl_done((union ctl_io *)ctsio); 8118 return (1); 8119 } 8120 8121 /* 8122 * Do the following: 8123 * if sa_res_key != res_key remove all 8124 * registrants w/sa_res_key and generate UA 8125 * for these registrants(Registrations 8126 * Preempted) if it wasn't an exclusive 8127 * reservation generate UA(Reservations 8128 * Preempted) for all other registered nexuses 8129 * if the type has changed. Establish the new 8130 * reservation and holder. If res_key and 8131 * sa_res_key are the same do the above 8132 * except don't unregister the res holder. 8133 */ 8134 8135 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8136 if (i == residx || ctl_get_prkey(lun, i) == 0) 8137 continue; 8138 8139 if (sa_res_key == ctl_get_prkey(lun, i)) { 8140 ctl_clr_prkey(lun, i); 8141 lun->pr_key_count--; 8142 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8143 } else if (type != lun->res_type 8144 && (lun->res_type == SPR_TYPE_WR_EX_RO 8145 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8146 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8147 } 8148 } 8149 lun->res_type = type; 8150 if (lun->res_type != SPR_TYPE_WR_EX_AR 8151 && lun->res_type != SPR_TYPE_EX_AC_AR) 8152 lun->pr_res_idx = residx; 8153 else 8154 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8155 8156 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8157 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8158 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8159 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8160 persis_io.pr.pr_info.res_type = type; 8161 memcpy(persis_io.pr.pr_info.sa_res_key, 8162 param->serv_act_res_key, 8163 sizeof(param->serv_act_res_key)); 8164 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8165 &persis_io, sizeof(persis_io), 0)) > 8166 CTL_HA_STATUS_SUCCESS) { 8167 printf("CTL:Persis Out error returned " 8168 "from ctl_ha_msg_send %d\n", 8169 isc_retval); 8170 } 8171 } else { 8172 /* 8173 * sa_res_key is not the res holder just 8174 * remove registrants 8175 */ 8176 int found=0; 8177 8178 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8179 if (sa_res_key != ctl_get_prkey(lun, i)) 8180 continue; 8181 8182 found = 1; 8183 ctl_clr_prkey(lun, i); 8184 lun->pr_key_count--; 8185 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8186 } 8187 8188 if (!found) { 8189 mtx_unlock(&lun->lun_lock); 8190 free(ctsio->kern_data_ptr, M_CTL); 8191 ctl_set_reservation_conflict(ctsio); 8192 ctl_done((union ctl_io *)ctsio); 8193 return (1); 8194 } 8195 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8196 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8197 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8198 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8199 persis_io.pr.pr_info.res_type = type; 8200 memcpy(persis_io.pr.pr_info.sa_res_key, 8201 param->serv_act_res_key, 8202 sizeof(param->serv_act_res_key)); 8203 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8204 &persis_io, sizeof(persis_io), 0)) > 8205 CTL_HA_STATUS_SUCCESS) { 8206 printf("CTL:Persis Out error returned " 8207 "from ctl_ha_msg_send %d\n", 8208 isc_retval); 8209 } 8210 } 8211 } 8212 8213 lun->PRGeneration++; 8214 mtx_unlock(&lun->lun_lock); 8215 8216 return (retval); 8217} 8218 8219static void 8220ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8221{ 8222 uint64_t sa_res_key; 8223 int i; 8224 8225 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8226 8227 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8228 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8229 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8230 if (sa_res_key == 0) { 8231 /* 8232 * Unregister everybody else and build UA for 8233 * them 8234 */ 8235 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8236 if (i == msg->pr.pr_info.residx || 8237 ctl_get_prkey(lun, i) == 0) 8238 continue; 8239 8240 ctl_clr_prkey(lun, i); 8241 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8242 } 8243 8244 lun->pr_key_count = 1; 8245 lun->res_type = msg->pr.pr_info.res_type; 8246 if (lun->res_type != SPR_TYPE_WR_EX_AR 8247 && lun->res_type != SPR_TYPE_EX_AC_AR) 8248 lun->pr_res_idx = msg->pr.pr_info.residx; 8249 } else { 8250 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8251 if (sa_res_key == ctl_get_prkey(lun, i)) 8252 continue; 8253 8254 ctl_clr_prkey(lun, i); 8255 lun->pr_key_count--; 8256 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8257 } 8258 } 8259 } else { 8260 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8261 if (i == msg->pr.pr_info.residx || 8262 ctl_get_prkey(lun, i) == 0) 8263 continue; 8264 8265 if (sa_res_key == ctl_get_prkey(lun, i)) { 8266 ctl_clr_prkey(lun, i); 8267 lun->pr_key_count--; 8268 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8269 } else if (msg->pr.pr_info.res_type != lun->res_type 8270 && (lun->res_type == SPR_TYPE_WR_EX_RO 8271 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8272 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8273 } 8274 } 8275 lun->res_type = msg->pr.pr_info.res_type; 8276 if (lun->res_type != SPR_TYPE_WR_EX_AR 8277 && lun->res_type != SPR_TYPE_EX_AC_AR) 8278 lun->pr_res_idx = msg->pr.pr_info.residx; 8279 else 8280 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8281 } 8282 lun->PRGeneration++; 8283 8284} 8285 8286 8287int 8288ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8289{ 8290 int retval; 8291 int isc_retval; 8292 u_int32_t param_len; 8293 struct scsi_per_res_out *cdb; 8294 struct ctl_lun *lun; 8295 struct scsi_per_res_out_parms* param; 8296 struct ctl_softc *softc; 8297 uint32_t residx; 8298 uint64_t res_key, sa_res_key, key; 8299 uint8_t type; 8300 union ctl_ha_msg persis_io; 8301 int i; 8302 8303 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8304 8305 retval = CTL_RETVAL_COMPLETE; 8306 8307 softc = control_softc; 8308 8309 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8310 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8311 8312 /* 8313 * We only support whole-LUN scope. The scope & type are ignored for 8314 * register, register and ignore existing key and clear. 8315 * We sometimes ignore scope and type on preempts too!! 8316 * Verify reservation type here as well. 8317 */ 8318 type = cdb->scope_type & SPR_TYPE_MASK; 8319 if ((cdb->action == SPRO_RESERVE) 8320 || (cdb->action == SPRO_RELEASE)) { 8321 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8322 ctl_set_invalid_field(/*ctsio*/ ctsio, 8323 /*sks_valid*/ 1, 8324 /*command*/ 1, 8325 /*field*/ 2, 8326 /*bit_valid*/ 1, 8327 /*bit*/ 4); 8328 ctl_done((union ctl_io *)ctsio); 8329 return (CTL_RETVAL_COMPLETE); 8330 } 8331 8332 if (type>8 || type==2 || type==4 || type==0) { 8333 ctl_set_invalid_field(/*ctsio*/ ctsio, 8334 /*sks_valid*/ 1, 8335 /*command*/ 1, 8336 /*field*/ 2, 8337 /*bit_valid*/ 1, 8338 /*bit*/ 0); 8339 ctl_done((union ctl_io *)ctsio); 8340 return (CTL_RETVAL_COMPLETE); 8341 } 8342 } 8343 8344 param_len = scsi_4btoul(cdb->length); 8345 8346 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8347 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8348 ctsio->kern_data_len = param_len; 8349 ctsio->kern_total_len = param_len; 8350 ctsio->kern_data_resid = 0; 8351 ctsio->kern_rel_offset = 0; 8352 ctsio->kern_sg_entries = 0; 8353 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8354 ctsio->be_move_done = ctl_config_move_done; 8355 ctl_datamove((union ctl_io *)ctsio); 8356 8357 return (CTL_RETVAL_COMPLETE); 8358 } 8359 8360 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8361 8362 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8363 res_key = scsi_8btou64(param->res_key.key); 8364 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8365 8366 /* 8367 * Validate the reservation key here except for SPRO_REG_IGNO 8368 * This must be done for all other service actions 8369 */ 8370 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8371 mtx_lock(&lun->lun_lock); 8372 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8373 if (res_key != key) { 8374 /* 8375 * The current key passed in doesn't match 8376 * the one the initiator previously 8377 * registered. 8378 */ 8379 mtx_unlock(&lun->lun_lock); 8380 free(ctsio->kern_data_ptr, M_CTL); 8381 ctl_set_reservation_conflict(ctsio); 8382 ctl_done((union ctl_io *)ctsio); 8383 return (CTL_RETVAL_COMPLETE); 8384 } 8385 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8386 /* 8387 * We are not registered 8388 */ 8389 mtx_unlock(&lun->lun_lock); 8390 free(ctsio->kern_data_ptr, M_CTL); 8391 ctl_set_reservation_conflict(ctsio); 8392 ctl_done((union ctl_io *)ctsio); 8393 return (CTL_RETVAL_COMPLETE); 8394 } else if (res_key != 0) { 8395 /* 8396 * We are not registered and trying to register but 8397 * the register key isn't zero. 8398 */ 8399 mtx_unlock(&lun->lun_lock); 8400 free(ctsio->kern_data_ptr, M_CTL); 8401 ctl_set_reservation_conflict(ctsio); 8402 ctl_done((union ctl_io *)ctsio); 8403 return (CTL_RETVAL_COMPLETE); 8404 } 8405 mtx_unlock(&lun->lun_lock); 8406 } 8407 8408 switch (cdb->action & SPRO_ACTION_MASK) { 8409 case SPRO_REGISTER: 8410 case SPRO_REG_IGNO: { 8411 8412#if 0 8413 printf("Registration received\n"); 8414#endif 8415 8416 /* 8417 * We don't support any of these options, as we report in 8418 * the read capabilities request (see 8419 * ctl_persistent_reserve_in(), above). 8420 */ 8421 if ((param->flags & SPR_SPEC_I_PT) 8422 || (param->flags & SPR_ALL_TG_PT) 8423 || (param->flags & SPR_APTPL)) { 8424 int bit_ptr; 8425 8426 if (param->flags & SPR_APTPL) 8427 bit_ptr = 0; 8428 else if (param->flags & SPR_ALL_TG_PT) 8429 bit_ptr = 2; 8430 else /* SPR_SPEC_I_PT */ 8431 bit_ptr = 3; 8432 8433 free(ctsio->kern_data_ptr, M_CTL); 8434 ctl_set_invalid_field(ctsio, 8435 /*sks_valid*/ 1, 8436 /*command*/ 0, 8437 /*field*/ 20, 8438 /*bit_valid*/ 1, 8439 /*bit*/ bit_ptr); 8440 ctl_done((union ctl_io *)ctsio); 8441 return (CTL_RETVAL_COMPLETE); 8442 } 8443 8444 mtx_lock(&lun->lun_lock); 8445 8446 /* 8447 * The initiator wants to clear the 8448 * key/unregister. 8449 */ 8450 if (sa_res_key == 0) { 8451 if ((res_key == 0 8452 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8453 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8454 && ctl_get_prkey(lun, residx) == 0)) { 8455 mtx_unlock(&lun->lun_lock); 8456 goto done; 8457 } 8458 8459 ctl_clr_prkey(lun, residx); 8460 lun->pr_key_count--; 8461 8462 if (residx == lun->pr_res_idx) { 8463 lun->flags &= ~CTL_LUN_PR_RESERVED; 8464 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8465 8466 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8467 || lun->res_type == SPR_TYPE_EX_AC_RO) 8468 && lun->pr_key_count) { 8469 /* 8470 * If the reservation is a registrants 8471 * only type we need to generate a UA 8472 * for other registered inits. The 8473 * sense code should be RESERVATIONS 8474 * RELEASED 8475 */ 8476 8477 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8478 if (ctl_get_prkey(lun, i + 8479 softc->persis_offset) == 0) 8480 continue; 8481 lun->pending_ua[i] |= 8482 CTL_UA_RES_RELEASE; 8483 } 8484 } 8485 lun->res_type = 0; 8486 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8487 if (lun->pr_key_count==0) { 8488 lun->flags &= ~CTL_LUN_PR_RESERVED; 8489 lun->res_type = 0; 8490 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8491 } 8492 } 8493 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8494 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8495 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8496 persis_io.pr.pr_info.residx = residx; 8497 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8498 &persis_io, sizeof(persis_io), 0 )) > 8499 CTL_HA_STATUS_SUCCESS) { 8500 printf("CTL:Persis Out error returned from " 8501 "ctl_ha_msg_send %d\n", isc_retval); 8502 } 8503 } else /* sa_res_key != 0 */ { 8504 8505 /* 8506 * If we aren't registered currently then increment 8507 * the key count and set the registered flag. 8508 */ 8509 ctl_alloc_prkey(lun, residx); 8510 if (ctl_get_prkey(lun, residx) == 0) 8511 lun->pr_key_count++; 8512 ctl_set_prkey(lun, residx, sa_res_key); 8513 8514 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8515 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8516 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8517 persis_io.pr.pr_info.residx = residx; 8518 memcpy(persis_io.pr.pr_info.sa_res_key, 8519 param->serv_act_res_key, 8520 sizeof(param->serv_act_res_key)); 8521 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8522 &persis_io, sizeof(persis_io), 0)) > 8523 CTL_HA_STATUS_SUCCESS) { 8524 printf("CTL:Persis Out error returned from " 8525 "ctl_ha_msg_send %d\n", isc_retval); 8526 } 8527 } 8528 lun->PRGeneration++; 8529 mtx_unlock(&lun->lun_lock); 8530 8531 break; 8532 } 8533 case SPRO_RESERVE: 8534#if 0 8535 printf("Reserve executed type %d\n", type); 8536#endif 8537 mtx_lock(&lun->lun_lock); 8538 if (lun->flags & CTL_LUN_PR_RESERVED) { 8539 /* 8540 * if this isn't the reservation holder and it's 8541 * not a "all registrants" type or if the type is 8542 * different then we have a conflict 8543 */ 8544 if ((lun->pr_res_idx != residx 8545 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8546 || lun->res_type != type) { 8547 mtx_unlock(&lun->lun_lock); 8548 free(ctsio->kern_data_ptr, M_CTL); 8549 ctl_set_reservation_conflict(ctsio); 8550 ctl_done((union ctl_io *)ctsio); 8551 return (CTL_RETVAL_COMPLETE); 8552 } 8553 mtx_unlock(&lun->lun_lock); 8554 } else /* create a reservation */ { 8555 /* 8556 * If it's not an "all registrants" type record 8557 * reservation holder 8558 */ 8559 if (type != SPR_TYPE_WR_EX_AR 8560 && type != SPR_TYPE_EX_AC_AR) 8561 lun->pr_res_idx = residx; /* Res holder */ 8562 else 8563 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8564 8565 lun->flags |= CTL_LUN_PR_RESERVED; 8566 lun->res_type = type; 8567 8568 mtx_unlock(&lun->lun_lock); 8569 8570 /* send msg to other side */ 8571 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8572 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8573 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8574 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8575 persis_io.pr.pr_info.res_type = type; 8576 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8577 &persis_io, sizeof(persis_io), 0)) > 8578 CTL_HA_STATUS_SUCCESS) { 8579 printf("CTL:Persis Out error returned from " 8580 "ctl_ha_msg_send %d\n", isc_retval); 8581 } 8582 } 8583 break; 8584 8585 case SPRO_RELEASE: 8586 mtx_lock(&lun->lun_lock); 8587 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8588 /* No reservation exists return good status */ 8589 mtx_unlock(&lun->lun_lock); 8590 goto done; 8591 } 8592 /* 8593 * Is this nexus a reservation holder? 8594 */ 8595 if (lun->pr_res_idx != residx 8596 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8597 /* 8598 * not a res holder return good status but 8599 * do nothing 8600 */ 8601 mtx_unlock(&lun->lun_lock); 8602 goto done; 8603 } 8604 8605 if (lun->res_type != type) { 8606 mtx_unlock(&lun->lun_lock); 8607 free(ctsio->kern_data_ptr, M_CTL); 8608 ctl_set_illegal_pr_release(ctsio); 8609 ctl_done((union ctl_io *)ctsio); 8610 return (CTL_RETVAL_COMPLETE); 8611 } 8612 8613 /* okay to release */ 8614 lun->flags &= ~CTL_LUN_PR_RESERVED; 8615 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8616 lun->res_type = 0; 8617 8618 /* 8619 * if this isn't an exclusive access 8620 * res generate UA for all other 8621 * registrants. 8622 */ 8623 if (type != SPR_TYPE_EX_AC 8624 && type != SPR_TYPE_WR_EX) { 8625 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8626 if (i == residx || 8627 ctl_get_prkey(lun, 8628 i + softc->persis_offset) == 0) 8629 continue; 8630 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8631 } 8632 } 8633 mtx_unlock(&lun->lun_lock); 8634 /* Send msg to other side */ 8635 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8636 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8637 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8638 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8639 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8640 printf("CTL:Persis Out error returned from " 8641 "ctl_ha_msg_send %d\n", isc_retval); 8642 } 8643 break; 8644 8645 case SPRO_CLEAR: 8646 /* send msg to other side */ 8647 8648 mtx_lock(&lun->lun_lock); 8649 lun->flags &= ~CTL_LUN_PR_RESERVED; 8650 lun->res_type = 0; 8651 lun->pr_key_count = 0; 8652 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8653 8654 ctl_clr_prkey(lun, residx); 8655 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8656 if (ctl_get_prkey(lun, i) != 0) { 8657 ctl_clr_prkey(lun, i); 8658 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8659 } 8660 lun->PRGeneration++; 8661 mtx_unlock(&lun->lun_lock); 8662 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8663 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8664 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8665 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8666 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8667 printf("CTL:Persis Out error returned from " 8668 "ctl_ha_msg_send %d\n", isc_retval); 8669 } 8670 break; 8671 8672 case SPRO_PREEMPT: 8673 case SPRO_PRE_ABO: { 8674 int nretval; 8675 8676 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8677 residx, ctsio, cdb, param); 8678 if (nretval != 0) 8679 return (CTL_RETVAL_COMPLETE); 8680 break; 8681 } 8682 default: 8683 panic("Invalid PR type %x", cdb->action); 8684 } 8685 8686done: 8687 free(ctsio->kern_data_ptr, M_CTL); 8688 ctl_set_success(ctsio); 8689 ctl_done((union ctl_io *)ctsio); 8690 8691 return (retval); 8692} 8693 8694/* 8695 * This routine is for handling a message from the other SC pertaining to 8696 * persistent reserve out. All the error checking will have been done 8697 * so only perorming the action need be done here to keep the two 8698 * in sync. 8699 */ 8700static void 8701ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8702{ 8703 struct ctl_lun *lun; 8704 struct ctl_softc *softc; 8705 int i; 8706 uint32_t targ_lun; 8707 8708 softc = control_softc; 8709 8710 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8711 lun = softc->ctl_luns[targ_lun]; 8712 mtx_lock(&lun->lun_lock); 8713 switch(msg->pr.pr_info.action) { 8714 case CTL_PR_REG_KEY: 8715 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8716 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8717 lun->pr_key_count++; 8718 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8719 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8720 lun->PRGeneration++; 8721 break; 8722 8723 case CTL_PR_UNREG_KEY: 8724 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8725 lun->pr_key_count--; 8726 8727 /* XXX Need to see if the reservation has been released */ 8728 /* if so do we need to generate UA? */ 8729 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8730 lun->flags &= ~CTL_LUN_PR_RESERVED; 8731 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8732 8733 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8734 || lun->res_type == SPR_TYPE_EX_AC_RO) 8735 && lun->pr_key_count) { 8736 /* 8737 * If the reservation is a registrants 8738 * only type we need to generate a UA 8739 * for other registered inits. The 8740 * sense code should be RESERVATIONS 8741 * RELEASED 8742 */ 8743 8744 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8745 if (ctl_get_prkey(lun, i + 8746 softc->persis_offset) == 0) 8747 continue; 8748 8749 lun->pending_ua[i] |= 8750 CTL_UA_RES_RELEASE; 8751 } 8752 } 8753 lun->res_type = 0; 8754 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8755 if (lun->pr_key_count==0) { 8756 lun->flags &= ~CTL_LUN_PR_RESERVED; 8757 lun->res_type = 0; 8758 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8759 } 8760 } 8761 lun->PRGeneration++; 8762 break; 8763 8764 case CTL_PR_RESERVE: 8765 lun->flags |= CTL_LUN_PR_RESERVED; 8766 lun->res_type = msg->pr.pr_info.res_type; 8767 lun->pr_res_idx = msg->pr.pr_info.residx; 8768 8769 break; 8770 8771 case CTL_PR_RELEASE: 8772 /* 8773 * if this isn't an exclusive access res generate UA for all 8774 * other registrants. 8775 */ 8776 if (lun->res_type != SPR_TYPE_EX_AC 8777 && lun->res_type != SPR_TYPE_WR_EX) { 8778 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8779 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8780 lun->pending_ua[i] |= 8781 CTL_UA_RES_RELEASE; 8782 } 8783 8784 lun->flags &= ~CTL_LUN_PR_RESERVED; 8785 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8786 lun->res_type = 0; 8787 break; 8788 8789 case CTL_PR_PREEMPT: 8790 ctl_pro_preempt_other(lun, msg); 8791 break; 8792 case CTL_PR_CLEAR: 8793 lun->flags &= ~CTL_LUN_PR_RESERVED; 8794 lun->res_type = 0; 8795 lun->pr_key_count = 0; 8796 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8797 8798 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8799 if (ctl_get_prkey(lun, i) == 0) 8800 continue; 8801 ctl_clr_prkey(lun, i); 8802 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8803 } 8804 lun->PRGeneration++; 8805 break; 8806 } 8807 8808 mtx_unlock(&lun->lun_lock); 8809} 8810 8811int 8812ctl_read_write(struct ctl_scsiio *ctsio) 8813{ 8814 struct ctl_lun *lun; 8815 struct ctl_lba_len_flags *lbalen; 8816 uint64_t lba; 8817 uint32_t num_blocks; 8818 int flags, retval; 8819 int isread; 8820 8821 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8822 8823 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8824 8825 flags = 0; 8826 retval = CTL_RETVAL_COMPLETE; 8827 8828 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8829 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8830 switch (ctsio->cdb[0]) { 8831 case READ_6: 8832 case WRITE_6: { 8833 struct scsi_rw_6 *cdb; 8834 8835 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8836 8837 lba = scsi_3btoul(cdb->addr); 8838 /* only 5 bits are valid in the most significant address byte */ 8839 lba &= 0x1fffff; 8840 num_blocks = cdb->length; 8841 /* 8842 * This is correct according to SBC-2. 8843 */ 8844 if (num_blocks == 0) 8845 num_blocks = 256; 8846 break; 8847 } 8848 case READ_10: 8849 case WRITE_10: { 8850 struct scsi_rw_10 *cdb; 8851 8852 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8853 if (cdb->byte2 & SRW10_FUA) 8854 flags |= CTL_LLF_FUA; 8855 if (cdb->byte2 & SRW10_DPO) 8856 flags |= CTL_LLF_DPO; 8857 lba = scsi_4btoul(cdb->addr); 8858 num_blocks = scsi_2btoul(cdb->length); 8859 break; 8860 } 8861 case WRITE_VERIFY_10: { 8862 struct scsi_write_verify_10 *cdb; 8863 8864 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8865 flags |= CTL_LLF_FUA; 8866 if (cdb->byte2 & SWV_DPO) 8867 flags |= CTL_LLF_DPO; 8868 lba = scsi_4btoul(cdb->addr); 8869 num_blocks = scsi_2btoul(cdb->length); 8870 break; 8871 } 8872 case READ_12: 8873 case WRITE_12: { 8874 struct scsi_rw_12 *cdb; 8875 8876 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8877 if (cdb->byte2 & SRW12_FUA) 8878 flags |= CTL_LLF_FUA; 8879 if (cdb->byte2 & SRW12_DPO) 8880 flags |= CTL_LLF_DPO; 8881 lba = scsi_4btoul(cdb->addr); 8882 num_blocks = scsi_4btoul(cdb->length); 8883 break; 8884 } 8885 case WRITE_VERIFY_12: { 8886 struct scsi_write_verify_12 *cdb; 8887 8888 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8889 flags |= CTL_LLF_FUA; 8890 if (cdb->byte2 & SWV_DPO) 8891 flags |= CTL_LLF_DPO; 8892 lba = scsi_4btoul(cdb->addr); 8893 num_blocks = scsi_4btoul(cdb->length); 8894 break; 8895 } 8896 case READ_16: 8897 case WRITE_16: { 8898 struct scsi_rw_16 *cdb; 8899 8900 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8901 if (cdb->byte2 & SRW12_FUA) 8902 flags |= CTL_LLF_FUA; 8903 if (cdb->byte2 & SRW12_DPO) 8904 flags |= CTL_LLF_DPO; 8905 lba = scsi_8btou64(cdb->addr); 8906 num_blocks = scsi_4btoul(cdb->length); 8907 break; 8908 } 8909 case WRITE_ATOMIC_16: { 8910 struct scsi_rw_16 *cdb; 8911 8912 if (lun->be_lun->atomicblock == 0) { 8913 ctl_set_invalid_opcode(ctsio); 8914 ctl_done((union ctl_io *)ctsio); 8915 return (CTL_RETVAL_COMPLETE); 8916 } 8917 8918 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8919 if (cdb->byte2 & SRW12_FUA) 8920 flags |= CTL_LLF_FUA; 8921 if (cdb->byte2 & SRW12_DPO) 8922 flags |= CTL_LLF_DPO; 8923 lba = scsi_8btou64(cdb->addr); 8924 num_blocks = scsi_4btoul(cdb->length); 8925 if (num_blocks > lun->be_lun->atomicblock) { 8926 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8927 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8928 /*bit*/ 0); 8929 ctl_done((union ctl_io *)ctsio); 8930 return (CTL_RETVAL_COMPLETE); 8931 } 8932 break; 8933 } 8934 case WRITE_VERIFY_16: { 8935 struct scsi_write_verify_16 *cdb; 8936 8937 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8938 flags |= CTL_LLF_FUA; 8939 if (cdb->byte2 & SWV_DPO) 8940 flags |= CTL_LLF_DPO; 8941 lba = scsi_8btou64(cdb->addr); 8942 num_blocks = scsi_4btoul(cdb->length); 8943 break; 8944 } 8945 default: 8946 /* 8947 * We got a command we don't support. This shouldn't 8948 * happen, commands should be filtered out above us. 8949 */ 8950 ctl_set_invalid_opcode(ctsio); 8951 ctl_done((union ctl_io *)ctsio); 8952 8953 return (CTL_RETVAL_COMPLETE); 8954 break; /* NOTREACHED */ 8955 } 8956 8957 /* 8958 * The first check is to make sure we're in bounds, the second 8959 * check is to catch wrap-around problems. If the lba + num blocks 8960 * is less than the lba, then we've wrapped around and the block 8961 * range is invalid anyway. 8962 */ 8963 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8964 || ((lba + num_blocks) < lba)) { 8965 ctl_set_lba_out_of_range(ctsio); 8966 ctl_done((union ctl_io *)ctsio); 8967 return (CTL_RETVAL_COMPLETE); 8968 } 8969 8970 /* 8971 * According to SBC-3, a transfer length of 0 is not an error. 8972 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8973 * translates to 256 blocks for those commands. 8974 */ 8975 if (num_blocks == 0) { 8976 ctl_set_success(ctsio); 8977 ctl_done((union ctl_io *)ctsio); 8978 return (CTL_RETVAL_COMPLETE); 8979 } 8980 8981 /* Set FUA and/or DPO if caches are disabled. */ 8982 if (isread) { 8983 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8984 SCP_RCD) != 0) 8985 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8986 } else { 8987 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8988 SCP_WCE) == 0) 8989 flags |= CTL_LLF_FUA; 8990 } 8991 8992 lbalen = (struct ctl_lba_len_flags *) 8993 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8994 lbalen->lba = lba; 8995 lbalen->len = num_blocks; 8996 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8997 8998 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8999 ctsio->kern_rel_offset = 0; 9000 9001 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9002 9003 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9004 9005 return (retval); 9006} 9007 9008static int 9009ctl_cnw_cont(union ctl_io *io) 9010{ 9011 struct ctl_scsiio *ctsio; 9012 struct ctl_lun *lun; 9013 struct ctl_lba_len_flags *lbalen; 9014 int retval; 9015 9016 ctsio = &io->scsiio; 9017 ctsio->io_hdr.status = CTL_STATUS_NONE; 9018 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9019 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9020 lbalen = (struct ctl_lba_len_flags *) 9021 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9022 lbalen->flags &= ~CTL_LLF_COMPARE; 9023 lbalen->flags |= CTL_LLF_WRITE; 9024 9025 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9026 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9027 return (retval); 9028} 9029 9030int 9031ctl_cnw(struct ctl_scsiio *ctsio) 9032{ 9033 struct ctl_lun *lun; 9034 struct ctl_lba_len_flags *lbalen; 9035 uint64_t lba; 9036 uint32_t num_blocks; 9037 int flags, retval; 9038 9039 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9040 9041 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9042 9043 flags = 0; 9044 retval = CTL_RETVAL_COMPLETE; 9045 9046 switch (ctsio->cdb[0]) { 9047 case COMPARE_AND_WRITE: { 9048 struct scsi_compare_and_write *cdb; 9049 9050 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9051 if (cdb->byte2 & SRW10_FUA) 9052 flags |= CTL_LLF_FUA; 9053 if (cdb->byte2 & SRW10_DPO) 9054 flags |= CTL_LLF_DPO; 9055 lba = scsi_8btou64(cdb->addr); 9056 num_blocks = cdb->length; 9057 break; 9058 } 9059 default: 9060 /* 9061 * We got a command we don't support. This shouldn't 9062 * happen, commands should be filtered out above us. 9063 */ 9064 ctl_set_invalid_opcode(ctsio); 9065 ctl_done((union ctl_io *)ctsio); 9066 9067 return (CTL_RETVAL_COMPLETE); 9068 break; /* NOTREACHED */ 9069 } 9070 9071 /* 9072 * The first check is to make sure we're in bounds, the second 9073 * check is to catch wrap-around problems. If the lba + num blocks 9074 * is less than the lba, then we've wrapped around and the block 9075 * range is invalid anyway. 9076 */ 9077 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9078 || ((lba + num_blocks) < lba)) { 9079 ctl_set_lba_out_of_range(ctsio); 9080 ctl_done((union ctl_io *)ctsio); 9081 return (CTL_RETVAL_COMPLETE); 9082 } 9083 9084 /* 9085 * According to SBC-3, a transfer length of 0 is not an error. 9086 */ 9087 if (num_blocks == 0) { 9088 ctl_set_success(ctsio); 9089 ctl_done((union ctl_io *)ctsio); 9090 return (CTL_RETVAL_COMPLETE); 9091 } 9092 9093 /* Set FUA if write cache is disabled. */ 9094 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9095 SCP_WCE) == 0) 9096 flags |= CTL_LLF_FUA; 9097 9098 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9099 ctsio->kern_rel_offset = 0; 9100 9101 /* 9102 * Set the IO_CONT flag, so that if this I/O gets passed to 9103 * ctl_data_submit_done(), it'll get passed back to 9104 * ctl_ctl_cnw_cont() for further processing. 9105 */ 9106 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9107 ctsio->io_cont = ctl_cnw_cont; 9108 9109 lbalen = (struct ctl_lba_len_flags *) 9110 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9111 lbalen->lba = lba; 9112 lbalen->len = num_blocks; 9113 lbalen->flags = CTL_LLF_COMPARE | flags; 9114 9115 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9116 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9117 return (retval); 9118} 9119 9120int 9121ctl_verify(struct ctl_scsiio *ctsio) 9122{ 9123 struct ctl_lun *lun; 9124 struct ctl_lba_len_flags *lbalen; 9125 uint64_t lba; 9126 uint32_t num_blocks; 9127 int bytchk, flags; 9128 int retval; 9129 9130 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9131 9132 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9133 9134 bytchk = 0; 9135 flags = CTL_LLF_FUA; 9136 retval = CTL_RETVAL_COMPLETE; 9137 9138 switch (ctsio->cdb[0]) { 9139 case VERIFY_10: { 9140 struct scsi_verify_10 *cdb; 9141 9142 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9143 if (cdb->byte2 & SVFY_BYTCHK) 9144 bytchk = 1; 9145 if (cdb->byte2 & SVFY_DPO) 9146 flags |= CTL_LLF_DPO; 9147 lba = scsi_4btoul(cdb->addr); 9148 num_blocks = scsi_2btoul(cdb->length); 9149 break; 9150 } 9151 case VERIFY_12: { 9152 struct scsi_verify_12 *cdb; 9153 9154 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9155 if (cdb->byte2 & SVFY_BYTCHK) 9156 bytchk = 1; 9157 if (cdb->byte2 & SVFY_DPO) 9158 flags |= CTL_LLF_DPO; 9159 lba = scsi_4btoul(cdb->addr); 9160 num_blocks = scsi_4btoul(cdb->length); 9161 break; 9162 } 9163 case VERIFY_16: { 9164 struct scsi_rw_16 *cdb; 9165 9166 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9167 if (cdb->byte2 & SVFY_BYTCHK) 9168 bytchk = 1; 9169 if (cdb->byte2 & SVFY_DPO) 9170 flags |= CTL_LLF_DPO; 9171 lba = scsi_8btou64(cdb->addr); 9172 num_blocks = scsi_4btoul(cdb->length); 9173 break; 9174 } 9175 default: 9176 /* 9177 * We got a command we don't support. This shouldn't 9178 * happen, commands should be filtered out above us. 9179 */ 9180 ctl_set_invalid_opcode(ctsio); 9181 ctl_done((union ctl_io *)ctsio); 9182 return (CTL_RETVAL_COMPLETE); 9183 } 9184 9185 /* 9186 * The first check is to make sure we're in bounds, the second 9187 * check is to catch wrap-around problems. If the lba + num blocks 9188 * is less than the lba, then we've wrapped around and the block 9189 * range is invalid anyway. 9190 */ 9191 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9192 || ((lba + num_blocks) < lba)) { 9193 ctl_set_lba_out_of_range(ctsio); 9194 ctl_done((union ctl_io *)ctsio); 9195 return (CTL_RETVAL_COMPLETE); 9196 } 9197 9198 /* 9199 * According to SBC-3, a transfer length of 0 is not an error. 9200 */ 9201 if (num_blocks == 0) { 9202 ctl_set_success(ctsio); 9203 ctl_done((union ctl_io *)ctsio); 9204 return (CTL_RETVAL_COMPLETE); 9205 } 9206 9207 lbalen = (struct ctl_lba_len_flags *) 9208 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9209 lbalen->lba = lba; 9210 lbalen->len = num_blocks; 9211 if (bytchk) { 9212 lbalen->flags = CTL_LLF_COMPARE | flags; 9213 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9214 } else { 9215 lbalen->flags = CTL_LLF_VERIFY | flags; 9216 ctsio->kern_total_len = 0; 9217 } 9218 ctsio->kern_rel_offset = 0; 9219 9220 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9221 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9222 return (retval); 9223} 9224 9225int 9226ctl_report_luns(struct ctl_scsiio *ctsio) 9227{ 9228 struct scsi_report_luns *cdb; 9229 struct scsi_report_luns_data *lun_data; 9230 struct ctl_lun *lun, *request_lun; 9231 int num_luns, retval; 9232 uint32_t alloc_len, lun_datalen; 9233 int num_filled, well_known; 9234 uint32_t initidx, targ_lun_id, lun_id; 9235 9236 retval = CTL_RETVAL_COMPLETE; 9237 well_known = 0; 9238 9239 cdb = (struct scsi_report_luns *)ctsio->cdb; 9240 9241 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9242 9243 mtx_lock(&control_softc->ctl_lock); 9244 num_luns = control_softc->num_luns; 9245 mtx_unlock(&control_softc->ctl_lock); 9246 9247 switch (cdb->select_report) { 9248 case RPL_REPORT_DEFAULT: 9249 case RPL_REPORT_ALL: 9250 break; 9251 case RPL_REPORT_WELLKNOWN: 9252 well_known = 1; 9253 num_luns = 0; 9254 break; 9255 default: 9256 ctl_set_invalid_field(ctsio, 9257 /*sks_valid*/ 1, 9258 /*command*/ 1, 9259 /*field*/ 2, 9260 /*bit_valid*/ 0, 9261 /*bit*/ 0); 9262 ctl_done((union ctl_io *)ctsio); 9263 return (retval); 9264 break; /* NOTREACHED */ 9265 } 9266 9267 alloc_len = scsi_4btoul(cdb->length); 9268 /* 9269 * The initiator has to allocate at least 16 bytes for this request, 9270 * so he can at least get the header and the first LUN. Otherwise 9271 * we reject the request (per SPC-3 rev 14, section 6.21). 9272 */ 9273 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9274 sizeof(struct scsi_report_luns_lundata))) { 9275 ctl_set_invalid_field(ctsio, 9276 /*sks_valid*/ 1, 9277 /*command*/ 1, 9278 /*field*/ 6, 9279 /*bit_valid*/ 0, 9280 /*bit*/ 0); 9281 ctl_done((union ctl_io *)ctsio); 9282 return (retval); 9283 } 9284 9285 request_lun = (struct ctl_lun *) 9286 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9287 9288 lun_datalen = sizeof(*lun_data) + 9289 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9290 9291 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9292 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9293 ctsio->kern_sg_entries = 0; 9294 9295 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9296 9297 mtx_lock(&control_softc->ctl_lock); 9298 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9299 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9300 if (lun_id >= CTL_MAX_LUNS) 9301 continue; 9302 lun = control_softc->ctl_luns[lun_id]; 9303 if (lun == NULL) 9304 continue; 9305 9306 if (targ_lun_id <= 0xff) { 9307 /* 9308 * Peripheral addressing method, bus number 0. 9309 */ 9310 lun_data->luns[num_filled].lundata[0] = 9311 RPL_LUNDATA_ATYP_PERIPH; 9312 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9313 num_filled++; 9314 } else if (targ_lun_id <= 0x3fff) { 9315 /* 9316 * Flat addressing method. 9317 */ 9318 lun_data->luns[num_filled].lundata[0] = 9319 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9320 lun_data->luns[num_filled].lundata[1] = 9321 (targ_lun_id & 0xff); 9322 num_filled++; 9323 } else if (targ_lun_id <= 0xffffff) { 9324 /* 9325 * Extended flat addressing method. 9326 */ 9327 lun_data->luns[num_filled].lundata[0] = 9328 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9329 scsi_ulto3b(targ_lun_id, 9330 &lun_data->luns[num_filled].lundata[1]); 9331 num_filled++; 9332 } else { 9333 printf("ctl_report_luns: bogus LUN number %jd, " 9334 "skipping\n", (intmax_t)targ_lun_id); 9335 } 9336 /* 9337 * According to SPC-3, rev 14 section 6.21: 9338 * 9339 * "The execution of a REPORT LUNS command to any valid and 9340 * installed logical unit shall clear the REPORTED LUNS DATA 9341 * HAS CHANGED unit attention condition for all logical 9342 * units of that target with respect to the requesting 9343 * initiator. A valid and installed logical unit is one 9344 * having a PERIPHERAL QUALIFIER of 000b in the standard 9345 * INQUIRY data (see 6.4.2)." 9346 * 9347 * If request_lun is NULL, the LUN this report luns command 9348 * was issued to is either disabled or doesn't exist. In that 9349 * case, we shouldn't clear any pending lun change unit 9350 * attention. 9351 */ 9352 if (request_lun != NULL) { 9353 mtx_lock(&lun->lun_lock); 9354 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9355 mtx_unlock(&lun->lun_lock); 9356 } 9357 } 9358 mtx_unlock(&control_softc->ctl_lock); 9359 9360 /* 9361 * It's quite possible that we've returned fewer LUNs than we allocated 9362 * space for. Trim it. 9363 */ 9364 lun_datalen = sizeof(*lun_data) + 9365 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9366 9367 if (lun_datalen < alloc_len) { 9368 ctsio->residual = alloc_len - lun_datalen; 9369 ctsio->kern_data_len = lun_datalen; 9370 ctsio->kern_total_len = lun_datalen; 9371 } else { 9372 ctsio->residual = 0; 9373 ctsio->kern_data_len = alloc_len; 9374 ctsio->kern_total_len = alloc_len; 9375 } 9376 ctsio->kern_data_resid = 0; 9377 ctsio->kern_rel_offset = 0; 9378 ctsio->kern_sg_entries = 0; 9379 9380 /* 9381 * We set this to the actual data length, regardless of how much 9382 * space we actually have to return results. If the user looks at 9383 * this value, he'll know whether or not he allocated enough space 9384 * and reissue the command if necessary. We don't support well 9385 * known logical units, so if the user asks for that, return none. 9386 */ 9387 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9388 9389 /* 9390 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9391 * this request. 9392 */ 9393 ctl_set_success(ctsio); 9394 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9395 ctsio->be_move_done = ctl_config_move_done; 9396 ctl_datamove((union ctl_io *)ctsio); 9397 return (retval); 9398} 9399 9400int 9401ctl_request_sense(struct ctl_scsiio *ctsio) 9402{ 9403 struct scsi_request_sense *cdb; 9404 struct scsi_sense_data *sense_ptr; 9405 struct ctl_lun *lun; 9406 uint32_t initidx; 9407 int have_error; 9408 scsi_sense_data_type sense_format; 9409 9410 cdb = (struct scsi_request_sense *)ctsio->cdb; 9411 9412 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9413 9414 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9415 9416 /* 9417 * Determine which sense format the user wants. 9418 */ 9419 if (cdb->byte2 & SRS_DESC) 9420 sense_format = SSD_TYPE_DESC; 9421 else 9422 sense_format = SSD_TYPE_FIXED; 9423 9424 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9425 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9426 ctsio->kern_sg_entries = 0; 9427 9428 /* 9429 * struct scsi_sense_data, which is currently set to 256 bytes, is 9430 * larger than the largest allowed value for the length field in the 9431 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9432 */ 9433 ctsio->residual = 0; 9434 ctsio->kern_data_len = cdb->length; 9435 ctsio->kern_total_len = cdb->length; 9436 9437 ctsio->kern_data_resid = 0; 9438 ctsio->kern_rel_offset = 0; 9439 ctsio->kern_sg_entries = 0; 9440 9441 /* 9442 * If we don't have a LUN, we don't have any pending sense. 9443 */ 9444 if (lun == NULL) 9445 goto no_sense; 9446 9447 have_error = 0; 9448 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9449 /* 9450 * Check for pending sense, and then for pending unit attentions. 9451 * Pending sense gets returned first, then pending unit attentions. 9452 */ 9453 mtx_lock(&lun->lun_lock); 9454#ifdef CTL_WITH_CA 9455 if (ctl_is_set(lun->have_ca, initidx)) { 9456 scsi_sense_data_type stored_format; 9457 9458 /* 9459 * Check to see which sense format was used for the stored 9460 * sense data. 9461 */ 9462 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9463 9464 /* 9465 * If the user requested a different sense format than the 9466 * one we stored, then we need to convert it to the other 9467 * format. If we're going from descriptor to fixed format 9468 * sense data, we may lose things in translation, depending 9469 * on what options were used. 9470 * 9471 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9472 * for some reason we'll just copy it out as-is. 9473 */ 9474 if ((stored_format == SSD_TYPE_FIXED) 9475 && (sense_format == SSD_TYPE_DESC)) 9476 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9477 &lun->pending_sense[initidx], 9478 (struct scsi_sense_data_desc *)sense_ptr); 9479 else if ((stored_format == SSD_TYPE_DESC) 9480 && (sense_format == SSD_TYPE_FIXED)) 9481 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9482 &lun->pending_sense[initidx], 9483 (struct scsi_sense_data_fixed *)sense_ptr); 9484 else 9485 memcpy(sense_ptr, &lun->pending_sense[initidx], 9486 ctl_min(sizeof(*sense_ptr), 9487 sizeof(lun->pending_sense[initidx]))); 9488 9489 ctl_clear_mask(lun->have_ca, initidx); 9490 have_error = 1; 9491 } else 9492#endif 9493 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9494 ctl_ua_type ua_type; 9495 9496 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9497 sense_ptr, sense_format); 9498 if (ua_type != CTL_UA_NONE) 9499 have_error = 1; 9500 } 9501 mtx_unlock(&lun->lun_lock); 9502 9503 /* 9504 * We already have a pending error, return it. 9505 */ 9506 if (have_error != 0) { 9507 /* 9508 * We report the SCSI status as OK, since the status of the 9509 * request sense command itself is OK. 9510 * We report 0 for the sense length, because we aren't doing 9511 * autosense in this case. We're reporting sense as 9512 * parameter data. 9513 */ 9514 ctl_set_success(ctsio); 9515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9516 ctsio->be_move_done = ctl_config_move_done; 9517 ctl_datamove((union ctl_io *)ctsio); 9518 return (CTL_RETVAL_COMPLETE); 9519 } 9520 9521no_sense: 9522 9523 /* 9524 * No sense information to report, so we report that everything is 9525 * okay. 9526 */ 9527 ctl_set_sense_data(sense_ptr, 9528 lun, 9529 sense_format, 9530 /*current_error*/ 1, 9531 /*sense_key*/ SSD_KEY_NO_SENSE, 9532 /*asc*/ 0x00, 9533 /*ascq*/ 0x00, 9534 SSD_ELEM_NONE); 9535 9536 /* 9537 * We report 0 for the sense length, because we aren't doing 9538 * autosense in this case. We're reporting sense as parameter data. 9539 */ 9540 ctl_set_success(ctsio); 9541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9542 ctsio->be_move_done = ctl_config_move_done; 9543 ctl_datamove((union ctl_io *)ctsio); 9544 return (CTL_RETVAL_COMPLETE); 9545} 9546 9547int 9548ctl_tur(struct ctl_scsiio *ctsio) 9549{ 9550 9551 CTL_DEBUG_PRINT(("ctl_tur\n")); 9552 9553 ctl_set_success(ctsio); 9554 ctl_done((union ctl_io *)ctsio); 9555 9556 return (CTL_RETVAL_COMPLETE); 9557} 9558 9559#ifdef notyet 9560static int 9561ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9562{ 9563 9564} 9565#endif 9566 9567static int 9568ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9569{ 9570 struct scsi_vpd_supported_pages *pages; 9571 int sup_page_size; 9572 struct ctl_lun *lun; 9573 9574 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9575 9576 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9577 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9578 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9579 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9580 ctsio->kern_sg_entries = 0; 9581 9582 if (sup_page_size < alloc_len) { 9583 ctsio->residual = alloc_len - sup_page_size; 9584 ctsio->kern_data_len = sup_page_size; 9585 ctsio->kern_total_len = sup_page_size; 9586 } else { 9587 ctsio->residual = 0; 9588 ctsio->kern_data_len = alloc_len; 9589 ctsio->kern_total_len = alloc_len; 9590 } 9591 ctsio->kern_data_resid = 0; 9592 ctsio->kern_rel_offset = 0; 9593 ctsio->kern_sg_entries = 0; 9594 9595 /* 9596 * The control device is always connected. The disk device, on the 9597 * other hand, may not be online all the time. Need to change this 9598 * to figure out whether the disk device is actually online or not. 9599 */ 9600 if (lun != NULL) 9601 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9602 lun->be_lun->lun_type; 9603 else 9604 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9605 9606 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9607 /* Supported VPD pages */ 9608 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9609 /* Serial Number */ 9610 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9611 /* Device Identification */ 9612 pages->page_list[2] = SVPD_DEVICE_ID; 9613 /* Extended INQUIRY Data */ 9614 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9615 /* Mode Page Policy */ 9616 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9617 /* SCSI Ports */ 9618 pages->page_list[5] = SVPD_SCSI_PORTS; 9619 /* Third-party Copy */ 9620 pages->page_list[6] = SVPD_SCSI_TPC; 9621 /* Block limits */ 9622 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9623 /* Block Device Characteristics */ 9624 pages->page_list[8] = SVPD_BDC; 9625 /* Logical Block Provisioning */ 9626 pages->page_list[9] = SVPD_LBP; 9627 9628 ctl_set_success(ctsio); 9629 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9630 ctsio->be_move_done = ctl_config_move_done; 9631 ctl_datamove((union ctl_io *)ctsio); 9632 return (CTL_RETVAL_COMPLETE); 9633} 9634 9635static int 9636ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9637{ 9638 struct scsi_vpd_unit_serial_number *sn_ptr; 9639 struct ctl_lun *lun; 9640 int data_len; 9641 9642 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9643 9644 data_len = 4 + CTL_SN_LEN; 9645 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9646 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9647 if (data_len < alloc_len) { 9648 ctsio->residual = alloc_len - data_len; 9649 ctsio->kern_data_len = data_len; 9650 ctsio->kern_total_len = data_len; 9651 } else { 9652 ctsio->residual = 0; 9653 ctsio->kern_data_len = alloc_len; 9654 ctsio->kern_total_len = alloc_len; 9655 } 9656 ctsio->kern_data_resid = 0; 9657 ctsio->kern_rel_offset = 0; 9658 ctsio->kern_sg_entries = 0; 9659 9660 /* 9661 * The control device is always connected. The disk device, on the 9662 * other hand, may not be online all the time. Need to change this 9663 * to figure out whether the disk device is actually online or not. 9664 */ 9665 if (lun != NULL) 9666 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9667 lun->be_lun->lun_type; 9668 else 9669 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9670 9671 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9672 sn_ptr->length = CTL_SN_LEN; 9673 /* 9674 * If we don't have a LUN, we just leave the serial number as 9675 * all spaces. 9676 */ 9677 if (lun != NULL) { 9678 strncpy((char *)sn_ptr->serial_num, 9679 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9680 } else 9681 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9682 9683 ctl_set_success(ctsio); 9684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9685 ctsio->be_move_done = ctl_config_move_done; 9686 ctl_datamove((union ctl_io *)ctsio); 9687 return (CTL_RETVAL_COMPLETE); 9688} 9689 9690 9691static int 9692ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9693{ 9694 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9695 struct ctl_lun *lun; 9696 int data_len; 9697 9698 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9699 9700 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9701 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9702 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9703 ctsio->kern_sg_entries = 0; 9704 9705 if (data_len < alloc_len) { 9706 ctsio->residual = alloc_len - data_len; 9707 ctsio->kern_data_len = data_len; 9708 ctsio->kern_total_len = data_len; 9709 } else { 9710 ctsio->residual = 0; 9711 ctsio->kern_data_len = alloc_len; 9712 ctsio->kern_total_len = alloc_len; 9713 } 9714 ctsio->kern_data_resid = 0; 9715 ctsio->kern_rel_offset = 0; 9716 ctsio->kern_sg_entries = 0; 9717 9718 /* 9719 * The control device is always connected. The disk device, on the 9720 * other hand, may not be online all the time. 9721 */ 9722 if (lun != NULL) 9723 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9724 lun->be_lun->lun_type; 9725 else 9726 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9727 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9728 eid_ptr->page_length = data_len - 4; 9729 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9730 eid_ptr->flags3 = SVPD_EID_V_SUP; 9731 9732 ctl_set_success(ctsio); 9733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9734 ctsio->be_move_done = ctl_config_move_done; 9735 ctl_datamove((union ctl_io *)ctsio); 9736 return (CTL_RETVAL_COMPLETE); 9737} 9738 9739static int 9740ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9741{ 9742 struct scsi_vpd_mode_page_policy *mpp_ptr; 9743 struct ctl_lun *lun; 9744 int data_len; 9745 9746 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9747 9748 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9749 sizeof(struct scsi_vpd_mode_page_policy_descr); 9750 9751 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9752 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9753 ctsio->kern_sg_entries = 0; 9754 9755 if (data_len < alloc_len) { 9756 ctsio->residual = alloc_len - data_len; 9757 ctsio->kern_data_len = data_len; 9758 ctsio->kern_total_len = data_len; 9759 } else { 9760 ctsio->residual = 0; 9761 ctsio->kern_data_len = alloc_len; 9762 ctsio->kern_total_len = alloc_len; 9763 } 9764 ctsio->kern_data_resid = 0; 9765 ctsio->kern_rel_offset = 0; 9766 ctsio->kern_sg_entries = 0; 9767 9768 /* 9769 * The control device is always connected. The disk device, on the 9770 * other hand, may not be online all the time. 9771 */ 9772 if (lun != NULL) 9773 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9774 lun->be_lun->lun_type; 9775 else 9776 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9777 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9778 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9779 mpp_ptr->descr[0].page_code = 0x3f; 9780 mpp_ptr->descr[0].subpage_code = 0xff; 9781 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9782 9783 ctl_set_success(ctsio); 9784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9785 ctsio->be_move_done = ctl_config_move_done; 9786 ctl_datamove((union ctl_io *)ctsio); 9787 return (CTL_RETVAL_COMPLETE); 9788} 9789 9790static int 9791ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9792{ 9793 struct scsi_vpd_device_id *devid_ptr; 9794 struct scsi_vpd_id_descriptor *desc; 9795 struct ctl_softc *ctl_softc; 9796 struct ctl_lun *lun; 9797 struct ctl_port *port; 9798 int data_len; 9799 uint8_t proto; 9800 9801 ctl_softc = control_softc; 9802 9803 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9804 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9805 9806 data_len = sizeof(struct scsi_vpd_device_id) + 9807 sizeof(struct scsi_vpd_id_descriptor) + 9808 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9809 sizeof(struct scsi_vpd_id_descriptor) + 9810 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9811 if (lun && lun->lun_devid) 9812 data_len += lun->lun_devid->len; 9813 if (port->port_devid) 9814 data_len += port->port_devid->len; 9815 if (port->target_devid) 9816 data_len += port->target_devid->len; 9817 9818 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9819 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9820 ctsio->kern_sg_entries = 0; 9821 9822 if (data_len < alloc_len) { 9823 ctsio->residual = alloc_len - data_len; 9824 ctsio->kern_data_len = data_len; 9825 ctsio->kern_total_len = data_len; 9826 } else { 9827 ctsio->residual = 0; 9828 ctsio->kern_data_len = alloc_len; 9829 ctsio->kern_total_len = alloc_len; 9830 } 9831 ctsio->kern_data_resid = 0; 9832 ctsio->kern_rel_offset = 0; 9833 ctsio->kern_sg_entries = 0; 9834 9835 /* 9836 * The control device is always connected. The disk device, on the 9837 * other hand, may not be online all the time. 9838 */ 9839 if (lun != NULL) 9840 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9841 lun->be_lun->lun_type; 9842 else 9843 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9844 devid_ptr->page_code = SVPD_DEVICE_ID; 9845 scsi_ulto2b(data_len - 4, devid_ptr->length); 9846 9847 if (port->port_type == CTL_PORT_FC) 9848 proto = SCSI_PROTO_FC << 4; 9849 else if (port->port_type == CTL_PORT_ISCSI) 9850 proto = SCSI_PROTO_ISCSI << 4; 9851 else 9852 proto = SCSI_PROTO_SPI << 4; 9853 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9854 9855 /* 9856 * We're using a LUN association here. i.e., this device ID is a 9857 * per-LUN identifier. 9858 */ 9859 if (lun && lun->lun_devid) { 9860 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9861 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9862 lun->lun_devid->len); 9863 } 9864 9865 /* 9866 * This is for the WWPN which is a port association. 9867 */ 9868 if (port->port_devid) { 9869 memcpy(desc, port->port_devid->data, port->port_devid->len); 9870 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9871 port->port_devid->len); 9872 } 9873 9874 /* 9875 * This is for the Relative Target Port(type 4h) identifier 9876 */ 9877 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9878 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9879 SVPD_ID_TYPE_RELTARG; 9880 desc->length = 4; 9881 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9882 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9883 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9884 9885 /* 9886 * This is for the Target Port Group(type 5h) identifier 9887 */ 9888 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9889 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9890 SVPD_ID_TYPE_TPORTGRP; 9891 desc->length = 4; 9892 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9893 &desc->identifier[2]); 9894 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9895 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9896 9897 /* 9898 * This is for the Target identifier 9899 */ 9900 if (port->target_devid) { 9901 memcpy(desc, port->target_devid->data, port->target_devid->len); 9902 } 9903 9904 ctl_set_success(ctsio); 9905 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9906 ctsio->be_move_done = ctl_config_move_done; 9907 ctl_datamove((union ctl_io *)ctsio); 9908 return (CTL_RETVAL_COMPLETE); 9909} 9910 9911static int 9912ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9913{ 9914 struct ctl_softc *softc = control_softc; 9915 struct scsi_vpd_scsi_ports *sp; 9916 struct scsi_vpd_port_designation *pd; 9917 struct scsi_vpd_port_designation_cont *pdc; 9918 struct ctl_lun *lun; 9919 struct ctl_port *port; 9920 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9921 int num_target_port_groups; 9922 9923 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9924 9925 if (softc->is_single) 9926 num_target_port_groups = 1; 9927 else 9928 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9929 num_target_ports = 0; 9930 iid_len = 0; 9931 id_len = 0; 9932 mtx_lock(&softc->ctl_lock); 9933 STAILQ_FOREACH(port, &softc->port_list, links) { 9934 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9935 continue; 9936 if (lun != NULL && 9937 ctl_map_lun_back(port->targ_port, lun->lun) >= 9938 CTL_MAX_LUNS) 9939 continue; 9940 num_target_ports++; 9941 if (port->init_devid) 9942 iid_len += port->init_devid->len; 9943 if (port->port_devid) 9944 id_len += port->port_devid->len; 9945 } 9946 mtx_unlock(&softc->ctl_lock); 9947 9948 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9949 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9950 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9951 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9952 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9953 ctsio->kern_sg_entries = 0; 9954 9955 if (data_len < alloc_len) { 9956 ctsio->residual = alloc_len - data_len; 9957 ctsio->kern_data_len = data_len; 9958 ctsio->kern_total_len = data_len; 9959 } else { 9960 ctsio->residual = 0; 9961 ctsio->kern_data_len = alloc_len; 9962 ctsio->kern_total_len = alloc_len; 9963 } 9964 ctsio->kern_data_resid = 0; 9965 ctsio->kern_rel_offset = 0; 9966 ctsio->kern_sg_entries = 0; 9967 9968 /* 9969 * The control device is always connected. The disk device, on the 9970 * other hand, may not be online all the time. Need to change this 9971 * to figure out whether the disk device is actually online or not. 9972 */ 9973 if (lun != NULL) 9974 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9975 lun->be_lun->lun_type; 9976 else 9977 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9978 9979 sp->page_code = SVPD_SCSI_PORTS; 9980 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9981 sp->page_length); 9982 pd = &sp->design[0]; 9983 9984 mtx_lock(&softc->ctl_lock); 9985 pg = softc->port_offset / CTL_MAX_PORTS; 9986 for (g = 0; g < num_target_port_groups; g++) { 9987 STAILQ_FOREACH(port, &softc->port_list, links) { 9988 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9989 continue; 9990 if (lun != NULL && 9991 ctl_map_lun_back(port->targ_port, lun->lun) >= 9992 CTL_MAX_LUNS) 9993 continue; 9994 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 9995 scsi_ulto2b(p, pd->relative_port_id); 9996 if (port->init_devid && g == pg) { 9997 iid_len = port->init_devid->len; 9998 memcpy(pd->initiator_transportid, 9999 port->init_devid->data, port->init_devid->len); 10000 } else 10001 iid_len = 0; 10002 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10003 pdc = (struct scsi_vpd_port_designation_cont *) 10004 (&pd->initiator_transportid[iid_len]); 10005 if (port->port_devid && g == pg) { 10006 id_len = port->port_devid->len; 10007 memcpy(pdc->target_port_descriptors, 10008 port->port_devid->data, port->port_devid->len); 10009 } else 10010 id_len = 0; 10011 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10012 pd = (struct scsi_vpd_port_designation *) 10013 ((uint8_t *)pdc->target_port_descriptors + id_len); 10014 } 10015 } 10016 mtx_unlock(&softc->ctl_lock); 10017 10018 ctl_set_success(ctsio); 10019 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10020 ctsio->be_move_done = ctl_config_move_done; 10021 ctl_datamove((union ctl_io *)ctsio); 10022 return (CTL_RETVAL_COMPLETE); 10023} 10024 10025static int 10026ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10027{ 10028 struct scsi_vpd_block_limits *bl_ptr; 10029 struct ctl_lun *lun; 10030 int bs; 10031 10032 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10033 10034 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10035 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10036 ctsio->kern_sg_entries = 0; 10037 10038 if (sizeof(*bl_ptr) < alloc_len) { 10039 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10040 ctsio->kern_data_len = sizeof(*bl_ptr); 10041 ctsio->kern_total_len = sizeof(*bl_ptr); 10042 } else { 10043 ctsio->residual = 0; 10044 ctsio->kern_data_len = alloc_len; 10045 ctsio->kern_total_len = alloc_len; 10046 } 10047 ctsio->kern_data_resid = 0; 10048 ctsio->kern_rel_offset = 0; 10049 ctsio->kern_sg_entries = 0; 10050 10051 /* 10052 * The control device is always connected. The disk device, on the 10053 * other hand, may not be online all the time. Need to change this 10054 * to figure out whether the disk device is actually online or not. 10055 */ 10056 if (lun != NULL) 10057 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10058 lun->be_lun->lun_type; 10059 else 10060 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10061 10062 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10063 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10064 bl_ptr->max_cmp_write_len = 0xff; 10065 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10066 if (lun != NULL) { 10067 bs = lun->be_lun->blocksize; 10068 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10069 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10070 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10071 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10072 if (lun->be_lun->pblockexp != 0) { 10073 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10074 bl_ptr->opt_unmap_grain); 10075 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10076 bl_ptr->unmap_grain_align); 10077 } 10078 } 10079 scsi_ulto4b(lun->be_lun->atomicblock, 10080 bl_ptr->max_atomic_transfer_length); 10081 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10082 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10083 } 10084 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10085 10086 ctl_set_success(ctsio); 10087 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10088 ctsio->be_move_done = ctl_config_move_done; 10089 ctl_datamove((union ctl_io *)ctsio); 10090 return (CTL_RETVAL_COMPLETE); 10091} 10092 10093static int 10094ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10095{ 10096 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10097 struct ctl_lun *lun; 10098 const char *value; 10099 u_int i; 10100 10101 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10102 10103 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10104 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10105 ctsio->kern_sg_entries = 0; 10106 10107 if (sizeof(*bdc_ptr) < alloc_len) { 10108 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10109 ctsio->kern_data_len = sizeof(*bdc_ptr); 10110 ctsio->kern_total_len = sizeof(*bdc_ptr); 10111 } else { 10112 ctsio->residual = 0; 10113 ctsio->kern_data_len = alloc_len; 10114 ctsio->kern_total_len = alloc_len; 10115 } 10116 ctsio->kern_data_resid = 0; 10117 ctsio->kern_rel_offset = 0; 10118 ctsio->kern_sg_entries = 0; 10119 10120 /* 10121 * The control device is always connected. The disk device, on the 10122 * other hand, may not be online all the time. Need to change this 10123 * to figure out whether the disk device is actually online or not. 10124 */ 10125 if (lun != NULL) 10126 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10127 lun->be_lun->lun_type; 10128 else 10129 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10130 bdc_ptr->page_code = SVPD_BDC; 10131 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10132 if (lun != NULL && 10133 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10134 i = strtol(value, NULL, 0); 10135 else 10136 i = CTL_DEFAULT_ROTATION_RATE; 10137 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10138 if (lun != NULL && 10139 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10140 i = strtol(value, NULL, 0); 10141 else 10142 i = 0; 10143 bdc_ptr->wab_wac_ff = (i & 0x0f); 10144 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10145 10146 ctl_set_success(ctsio); 10147 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10148 ctsio->be_move_done = ctl_config_move_done; 10149 ctl_datamove((union ctl_io *)ctsio); 10150 return (CTL_RETVAL_COMPLETE); 10151} 10152 10153static int 10154ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10155{ 10156 struct scsi_vpd_logical_block_prov *lbp_ptr; 10157 struct ctl_lun *lun; 10158 10159 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10160 10161 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10162 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10163 ctsio->kern_sg_entries = 0; 10164 10165 if (sizeof(*lbp_ptr) < alloc_len) { 10166 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10167 ctsio->kern_data_len = sizeof(*lbp_ptr); 10168 ctsio->kern_total_len = sizeof(*lbp_ptr); 10169 } else { 10170 ctsio->residual = 0; 10171 ctsio->kern_data_len = alloc_len; 10172 ctsio->kern_total_len = alloc_len; 10173 } 10174 ctsio->kern_data_resid = 0; 10175 ctsio->kern_rel_offset = 0; 10176 ctsio->kern_sg_entries = 0; 10177 10178 /* 10179 * The control device is always connected. The disk device, on the 10180 * other hand, may not be online all the time. Need to change this 10181 * to figure out whether the disk device is actually online or not. 10182 */ 10183 if (lun != NULL) 10184 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10185 lun->be_lun->lun_type; 10186 else 10187 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10188 10189 lbp_ptr->page_code = SVPD_LBP; 10190 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10191 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10192 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10193 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10194 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10195 lbp_ptr->prov_type = SVPD_LBP_THIN; 10196 } 10197 10198 ctl_set_success(ctsio); 10199 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10200 ctsio->be_move_done = ctl_config_move_done; 10201 ctl_datamove((union ctl_io *)ctsio); 10202 return (CTL_RETVAL_COMPLETE); 10203} 10204 10205static int 10206ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10207{ 10208 struct scsi_inquiry *cdb; 10209 int alloc_len, retval; 10210 10211 cdb = (struct scsi_inquiry *)ctsio->cdb; 10212 10213 retval = CTL_RETVAL_COMPLETE; 10214 10215 alloc_len = scsi_2btoul(cdb->length); 10216 10217 switch (cdb->page_code) { 10218 case SVPD_SUPPORTED_PAGES: 10219 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10220 break; 10221 case SVPD_UNIT_SERIAL_NUMBER: 10222 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10223 break; 10224 case SVPD_DEVICE_ID: 10225 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10226 break; 10227 case SVPD_EXTENDED_INQUIRY_DATA: 10228 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10229 break; 10230 case SVPD_MODE_PAGE_POLICY: 10231 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10232 break; 10233 case SVPD_SCSI_PORTS: 10234 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10235 break; 10236 case SVPD_SCSI_TPC: 10237 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10238 break; 10239 case SVPD_BLOCK_LIMITS: 10240 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10241 break; 10242 case SVPD_BDC: 10243 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10244 break; 10245 case SVPD_LBP: 10246 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10247 break; 10248 default: 10249 ctl_set_invalid_field(ctsio, 10250 /*sks_valid*/ 1, 10251 /*command*/ 1, 10252 /*field*/ 2, 10253 /*bit_valid*/ 0, 10254 /*bit*/ 0); 10255 ctl_done((union ctl_io *)ctsio); 10256 retval = CTL_RETVAL_COMPLETE; 10257 break; 10258 } 10259 10260 return (retval); 10261} 10262 10263static int 10264ctl_inquiry_std(struct ctl_scsiio *ctsio) 10265{ 10266 struct scsi_inquiry_data *inq_ptr; 10267 struct scsi_inquiry *cdb; 10268 struct ctl_softc *ctl_softc; 10269 struct ctl_lun *lun; 10270 char *val; 10271 uint32_t alloc_len, data_len; 10272 ctl_port_type port_type; 10273 10274 ctl_softc = control_softc; 10275 10276 /* 10277 * Figure out whether we're talking to a Fibre Channel port or not. 10278 * We treat the ioctl front end, and any SCSI adapters, as packetized 10279 * SCSI front ends. 10280 */ 10281 port_type = ctl_softc->ctl_ports[ 10282 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10283 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10284 port_type = CTL_PORT_SCSI; 10285 10286 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10287 cdb = (struct scsi_inquiry *)ctsio->cdb; 10288 alloc_len = scsi_2btoul(cdb->length); 10289 10290 /* 10291 * We malloc the full inquiry data size here and fill it 10292 * in. If the user only asks for less, we'll give him 10293 * that much. 10294 */ 10295 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10296 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10297 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10298 ctsio->kern_sg_entries = 0; 10299 ctsio->kern_data_resid = 0; 10300 ctsio->kern_rel_offset = 0; 10301 10302 if (data_len < alloc_len) { 10303 ctsio->residual = alloc_len - data_len; 10304 ctsio->kern_data_len = data_len; 10305 ctsio->kern_total_len = data_len; 10306 } else { 10307 ctsio->residual = 0; 10308 ctsio->kern_data_len = alloc_len; 10309 ctsio->kern_total_len = alloc_len; 10310 } 10311 10312 /* 10313 * If we have a LUN configured, report it as connected. Otherwise, 10314 * report that it is offline or no device is supported, depending 10315 * on the value of inquiry_pq_no_lun. 10316 * 10317 * According to the spec (SPC-4 r34), the peripheral qualifier 10318 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10319 * 10320 * "A peripheral device having the specified peripheral device type 10321 * is not connected to this logical unit. However, the device 10322 * server is capable of supporting the specified peripheral device 10323 * type on this logical unit." 10324 * 10325 * According to the same spec, the peripheral qualifier 10326 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10327 * 10328 * "The device server is not capable of supporting a peripheral 10329 * device on this logical unit. For this peripheral qualifier the 10330 * peripheral device type shall be set to 1Fh. All other peripheral 10331 * device type values are reserved for this peripheral qualifier." 10332 * 10333 * Given the text, it would seem that we probably want to report that 10334 * the LUN is offline here. There is no LUN connected, but we can 10335 * support a LUN at the given LUN number. 10336 * 10337 * In the real world, though, it sounds like things are a little 10338 * different: 10339 * 10340 * - Linux, when presented with a LUN with the offline peripheral 10341 * qualifier, will create an sg driver instance for it. So when 10342 * you attach it to CTL, you wind up with a ton of sg driver 10343 * instances. (One for every LUN that Linux bothered to probe.) 10344 * Linux does this despite the fact that it issues a REPORT LUNs 10345 * to LUN 0 to get the inventory of supported LUNs. 10346 * 10347 * - There is other anecdotal evidence (from Emulex folks) about 10348 * arrays that use the offline peripheral qualifier for LUNs that 10349 * are on the "passive" path in an active/passive array. 10350 * 10351 * So the solution is provide a hopefully reasonable default 10352 * (return bad/no LUN) and allow the user to change the behavior 10353 * with a tunable/sysctl variable. 10354 */ 10355 if (lun != NULL) 10356 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10357 lun->be_lun->lun_type; 10358 else if (ctl_softc->inquiry_pq_no_lun == 0) 10359 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10360 else 10361 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10362 10363 /* RMB in byte 2 is 0 */ 10364 inq_ptr->version = SCSI_REV_SPC4; 10365 10366 /* 10367 * According to SAM-3, even if a device only supports a single 10368 * level of LUN addressing, it should still set the HISUP bit: 10369 * 10370 * 4.9.1 Logical unit numbers overview 10371 * 10372 * All logical unit number formats described in this standard are 10373 * hierarchical in structure even when only a single level in that 10374 * hierarchy is used. The HISUP bit shall be set to one in the 10375 * standard INQUIRY data (see SPC-2) when any logical unit number 10376 * format described in this standard is used. Non-hierarchical 10377 * formats are outside the scope of this standard. 10378 * 10379 * Therefore we set the HiSup bit here. 10380 * 10381 * The reponse format is 2, per SPC-3. 10382 */ 10383 inq_ptr->response_format = SID_HiSup | 2; 10384 10385 inq_ptr->additional_length = data_len - 10386 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10387 CTL_DEBUG_PRINT(("additional_length = %d\n", 10388 inq_ptr->additional_length)); 10389 10390 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10391 /* 16 bit addressing */ 10392 if (port_type == CTL_PORT_SCSI) 10393 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10394 /* XXX set the SID_MultiP bit here if we're actually going to 10395 respond on multiple ports */ 10396 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10397 10398 /* 16 bit data bus, synchronous transfers */ 10399 if (port_type == CTL_PORT_SCSI) 10400 inq_ptr->flags = SID_WBus16 | SID_Sync; 10401 /* 10402 * XXX KDM do we want to support tagged queueing on the control 10403 * device at all? 10404 */ 10405 if ((lun == NULL) 10406 || (lun->be_lun->lun_type != T_PROCESSOR)) 10407 inq_ptr->flags |= SID_CmdQue; 10408 /* 10409 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10410 * We have 8 bytes for the vendor name, and 16 bytes for the device 10411 * name and 4 bytes for the revision. 10412 */ 10413 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10414 "vendor")) == NULL) { 10415 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10416 } else { 10417 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10418 strncpy(inq_ptr->vendor, val, 10419 min(sizeof(inq_ptr->vendor), strlen(val))); 10420 } 10421 if (lun == NULL) { 10422 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10423 sizeof(inq_ptr->product)); 10424 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10425 switch (lun->be_lun->lun_type) { 10426 case T_DIRECT: 10427 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10428 sizeof(inq_ptr->product)); 10429 break; 10430 case T_PROCESSOR: 10431 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10432 sizeof(inq_ptr->product)); 10433 break; 10434 default: 10435 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10436 sizeof(inq_ptr->product)); 10437 break; 10438 } 10439 } else { 10440 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10441 strncpy(inq_ptr->product, val, 10442 min(sizeof(inq_ptr->product), strlen(val))); 10443 } 10444 10445 /* 10446 * XXX make this a macro somewhere so it automatically gets 10447 * incremented when we make changes. 10448 */ 10449 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10450 "revision")) == NULL) { 10451 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10452 } else { 10453 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10454 strncpy(inq_ptr->revision, val, 10455 min(sizeof(inq_ptr->revision), strlen(val))); 10456 } 10457 10458 /* 10459 * For parallel SCSI, we support double transition and single 10460 * transition clocking. We also support QAS (Quick Arbitration 10461 * and Selection) and Information Unit transfers on both the 10462 * control and array devices. 10463 */ 10464 if (port_type == CTL_PORT_SCSI) 10465 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10466 SID_SPI_IUS; 10467 10468 /* SAM-5 (no version claimed) */ 10469 scsi_ulto2b(0x00A0, inq_ptr->version1); 10470 /* SPC-4 (no version claimed) */ 10471 scsi_ulto2b(0x0460, inq_ptr->version2); 10472 if (port_type == CTL_PORT_FC) { 10473 /* FCP-2 ANSI INCITS.350:2003 */ 10474 scsi_ulto2b(0x0917, inq_ptr->version3); 10475 } else if (port_type == CTL_PORT_SCSI) { 10476 /* SPI-4 ANSI INCITS.362:200x */ 10477 scsi_ulto2b(0x0B56, inq_ptr->version3); 10478 } else if (port_type == CTL_PORT_ISCSI) { 10479 /* iSCSI (no version claimed) */ 10480 scsi_ulto2b(0x0960, inq_ptr->version3); 10481 } else if (port_type == CTL_PORT_SAS) { 10482 /* SAS (no version claimed) */ 10483 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10484 } 10485 10486 if (lun == NULL) { 10487 /* SBC-4 (no version claimed) */ 10488 scsi_ulto2b(0x0600, inq_ptr->version4); 10489 } else { 10490 switch (lun->be_lun->lun_type) { 10491 case T_DIRECT: 10492 /* SBC-4 (no version claimed) */ 10493 scsi_ulto2b(0x0600, inq_ptr->version4); 10494 break; 10495 case T_PROCESSOR: 10496 default: 10497 break; 10498 } 10499 } 10500 10501 ctl_set_success(ctsio); 10502 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10503 ctsio->be_move_done = ctl_config_move_done; 10504 ctl_datamove((union ctl_io *)ctsio); 10505 return (CTL_RETVAL_COMPLETE); 10506} 10507 10508int 10509ctl_inquiry(struct ctl_scsiio *ctsio) 10510{ 10511 struct scsi_inquiry *cdb; 10512 int retval; 10513 10514 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10515 10516 cdb = (struct scsi_inquiry *)ctsio->cdb; 10517 if (cdb->byte2 & SI_EVPD) 10518 retval = ctl_inquiry_evpd(ctsio); 10519 else if (cdb->page_code == 0) 10520 retval = ctl_inquiry_std(ctsio); 10521 else { 10522 ctl_set_invalid_field(ctsio, 10523 /*sks_valid*/ 1, 10524 /*command*/ 1, 10525 /*field*/ 2, 10526 /*bit_valid*/ 0, 10527 /*bit*/ 0); 10528 ctl_done((union ctl_io *)ctsio); 10529 return (CTL_RETVAL_COMPLETE); 10530 } 10531 10532 return (retval); 10533} 10534 10535/* 10536 * For known CDB types, parse the LBA and length. 10537 */ 10538static int 10539ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10540{ 10541 if (io->io_hdr.io_type != CTL_IO_SCSI) 10542 return (1); 10543 10544 switch (io->scsiio.cdb[0]) { 10545 case COMPARE_AND_WRITE: { 10546 struct scsi_compare_and_write *cdb; 10547 10548 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10549 10550 *lba = scsi_8btou64(cdb->addr); 10551 *len = cdb->length; 10552 break; 10553 } 10554 case READ_6: 10555 case WRITE_6: { 10556 struct scsi_rw_6 *cdb; 10557 10558 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10559 10560 *lba = scsi_3btoul(cdb->addr); 10561 /* only 5 bits are valid in the most significant address byte */ 10562 *lba &= 0x1fffff; 10563 *len = cdb->length; 10564 break; 10565 } 10566 case READ_10: 10567 case WRITE_10: { 10568 struct scsi_rw_10 *cdb; 10569 10570 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10571 10572 *lba = scsi_4btoul(cdb->addr); 10573 *len = scsi_2btoul(cdb->length); 10574 break; 10575 } 10576 case WRITE_VERIFY_10: { 10577 struct scsi_write_verify_10 *cdb; 10578 10579 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10580 10581 *lba = scsi_4btoul(cdb->addr); 10582 *len = scsi_2btoul(cdb->length); 10583 break; 10584 } 10585 case READ_12: 10586 case WRITE_12: { 10587 struct scsi_rw_12 *cdb; 10588 10589 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10590 10591 *lba = scsi_4btoul(cdb->addr); 10592 *len = scsi_4btoul(cdb->length); 10593 break; 10594 } 10595 case WRITE_VERIFY_12: { 10596 struct scsi_write_verify_12 *cdb; 10597 10598 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10599 10600 *lba = scsi_4btoul(cdb->addr); 10601 *len = scsi_4btoul(cdb->length); 10602 break; 10603 } 10604 case READ_16: 10605 case WRITE_16: 10606 case WRITE_ATOMIC_16: { 10607 struct scsi_rw_16 *cdb; 10608 10609 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10610 10611 *lba = scsi_8btou64(cdb->addr); 10612 *len = scsi_4btoul(cdb->length); 10613 break; 10614 } 10615 case WRITE_VERIFY_16: { 10616 struct scsi_write_verify_16 *cdb; 10617 10618 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10619 10620 *lba = scsi_8btou64(cdb->addr); 10621 *len = scsi_4btoul(cdb->length); 10622 break; 10623 } 10624 case WRITE_SAME_10: { 10625 struct scsi_write_same_10 *cdb; 10626 10627 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10628 10629 *lba = scsi_4btoul(cdb->addr); 10630 *len = scsi_2btoul(cdb->length); 10631 break; 10632 } 10633 case WRITE_SAME_16: { 10634 struct scsi_write_same_16 *cdb; 10635 10636 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10637 10638 *lba = scsi_8btou64(cdb->addr); 10639 *len = scsi_4btoul(cdb->length); 10640 break; 10641 } 10642 case VERIFY_10: { 10643 struct scsi_verify_10 *cdb; 10644 10645 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10646 10647 *lba = scsi_4btoul(cdb->addr); 10648 *len = scsi_2btoul(cdb->length); 10649 break; 10650 } 10651 case VERIFY_12: { 10652 struct scsi_verify_12 *cdb; 10653 10654 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10655 10656 *lba = scsi_4btoul(cdb->addr); 10657 *len = scsi_4btoul(cdb->length); 10658 break; 10659 } 10660 case VERIFY_16: { 10661 struct scsi_verify_16 *cdb; 10662 10663 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10664 10665 *lba = scsi_8btou64(cdb->addr); 10666 *len = scsi_4btoul(cdb->length); 10667 break; 10668 } 10669 case UNMAP: { 10670 *lba = 0; 10671 *len = UINT64_MAX; 10672 break; 10673 } 10674 default: 10675 return (1); 10676 break; /* NOTREACHED */ 10677 } 10678 10679 return (0); 10680} 10681 10682static ctl_action 10683ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10684{ 10685 uint64_t endlba1, endlba2; 10686 10687 endlba1 = lba1 + len1 - 1; 10688 endlba2 = lba2 + len2 - 1; 10689 10690 if ((endlba1 < lba2) 10691 || (endlba2 < lba1)) 10692 return (CTL_ACTION_PASS); 10693 else 10694 return (CTL_ACTION_BLOCK); 10695} 10696 10697static int 10698ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10699{ 10700 struct ctl_ptr_len_flags *ptrlen; 10701 struct scsi_unmap_desc *buf, *end, *range; 10702 uint64_t lba; 10703 uint32_t len; 10704 10705 /* If not UNMAP -- go other way. */ 10706 if (io->io_hdr.io_type != CTL_IO_SCSI || 10707 io->scsiio.cdb[0] != UNMAP) 10708 return (CTL_ACTION_ERROR); 10709 10710 /* If UNMAP without data -- block and wait for data. */ 10711 ptrlen = (struct ctl_ptr_len_flags *) 10712 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10713 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10714 ptrlen->ptr == NULL) 10715 return (CTL_ACTION_BLOCK); 10716 10717 /* UNMAP with data -- check for collision. */ 10718 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10719 end = buf + ptrlen->len / sizeof(*buf); 10720 for (range = buf; range < end; range++) { 10721 lba = scsi_8btou64(range->lba); 10722 len = scsi_4btoul(range->length); 10723 if ((lba < lba2 + len2) && (lba + len > lba2)) 10724 return (CTL_ACTION_BLOCK); 10725 } 10726 return (CTL_ACTION_PASS); 10727} 10728 10729static ctl_action 10730ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10731{ 10732 uint64_t lba1, lba2; 10733 uint64_t len1, len2; 10734 int retval; 10735 10736 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10737 return (CTL_ACTION_ERROR); 10738 10739 retval = ctl_extent_check_unmap(io2, lba1, len1); 10740 if (retval != CTL_ACTION_ERROR) 10741 return (retval); 10742 10743 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10744 return (CTL_ACTION_ERROR); 10745 10746 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10747} 10748 10749static ctl_action 10750ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10751 union ctl_io *ooa_io) 10752{ 10753 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10754 ctl_serialize_action *serialize_row; 10755 10756 /* 10757 * The initiator attempted multiple untagged commands at the same 10758 * time. Can't do that. 10759 */ 10760 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10761 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10762 && ((pending_io->io_hdr.nexus.targ_port == 10763 ooa_io->io_hdr.nexus.targ_port) 10764 && (pending_io->io_hdr.nexus.initid.id == 10765 ooa_io->io_hdr.nexus.initid.id)) 10766 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10767 return (CTL_ACTION_OVERLAP); 10768 10769 /* 10770 * The initiator attempted to send multiple tagged commands with 10771 * the same ID. (It's fine if different initiators have the same 10772 * tag ID.) 10773 * 10774 * Even if all of those conditions are true, we don't kill the I/O 10775 * if the command ahead of us has been aborted. We won't end up 10776 * sending it to the FETD, and it's perfectly legal to resend a 10777 * command with the same tag number as long as the previous 10778 * instance of this tag number has been aborted somehow. 10779 */ 10780 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10781 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10782 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10783 && ((pending_io->io_hdr.nexus.targ_port == 10784 ooa_io->io_hdr.nexus.targ_port) 10785 && (pending_io->io_hdr.nexus.initid.id == 10786 ooa_io->io_hdr.nexus.initid.id)) 10787 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10788 return (CTL_ACTION_OVERLAP_TAG); 10789 10790 /* 10791 * If we get a head of queue tag, SAM-3 says that we should 10792 * immediately execute it. 10793 * 10794 * What happens if this command would normally block for some other 10795 * reason? e.g. a request sense with a head of queue tag 10796 * immediately after a write. Normally that would block, but this 10797 * will result in its getting executed immediately... 10798 * 10799 * We currently return "pass" instead of "skip", so we'll end up 10800 * going through the rest of the queue to check for overlapped tags. 10801 * 10802 * XXX KDM check for other types of blockage first?? 10803 */ 10804 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10805 return (CTL_ACTION_PASS); 10806 10807 /* 10808 * Ordered tags have to block until all items ahead of them 10809 * have completed. If we get called with an ordered tag, we always 10810 * block, if something else is ahead of us in the queue. 10811 */ 10812 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10813 return (CTL_ACTION_BLOCK); 10814 10815 /* 10816 * Simple tags get blocked until all head of queue and ordered tags 10817 * ahead of them have completed. I'm lumping untagged commands in 10818 * with simple tags here. XXX KDM is that the right thing to do? 10819 */ 10820 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10821 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10822 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10823 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10824 return (CTL_ACTION_BLOCK); 10825 10826 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10827 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10828 10829 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10830 10831 switch (serialize_row[pending_entry->seridx]) { 10832 case CTL_SER_BLOCK: 10833 return (CTL_ACTION_BLOCK); 10834 case CTL_SER_EXTENT: 10835 return (ctl_extent_check(pending_io, ooa_io)); 10836 case CTL_SER_EXTENTOPT: 10837 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10838 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10839 return (ctl_extent_check(pending_io, ooa_io)); 10840 /* FALLTHROUGH */ 10841 case CTL_SER_PASS: 10842 return (CTL_ACTION_PASS); 10843 case CTL_SER_BLOCKOPT: 10844 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10845 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10846 return (CTL_ACTION_BLOCK); 10847 return (CTL_ACTION_PASS); 10848 case CTL_SER_SKIP: 10849 return (CTL_ACTION_SKIP); 10850 default: 10851 panic("invalid serialization value %d", 10852 serialize_row[pending_entry->seridx]); 10853 } 10854 10855 return (CTL_ACTION_ERROR); 10856} 10857 10858/* 10859 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10860 * Assumptions: 10861 * - pending_io is generally either incoming, or on the blocked queue 10862 * - starting I/O is the I/O we want to start the check with. 10863 */ 10864static ctl_action 10865ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10866 union ctl_io *starting_io) 10867{ 10868 union ctl_io *ooa_io; 10869 ctl_action action; 10870 10871 mtx_assert(&lun->lun_lock, MA_OWNED); 10872 10873 /* 10874 * Run back along the OOA queue, starting with the current 10875 * blocked I/O and going through every I/O before it on the 10876 * queue. If starting_io is NULL, we'll just end up returning 10877 * CTL_ACTION_PASS. 10878 */ 10879 for (ooa_io = starting_io; ooa_io != NULL; 10880 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10881 ooa_links)){ 10882 10883 /* 10884 * This routine just checks to see whether 10885 * cur_blocked is blocked by ooa_io, which is ahead 10886 * of it in the queue. It doesn't queue/dequeue 10887 * cur_blocked. 10888 */ 10889 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10890 switch (action) { 10891 case CTL_ACTION_BLOCK: 10892 case CTL_ACTION_OVERLAP: 10893 case CTL_ACTION_OVERLAP_TAG: 10894 case CTL_ACTION_SKIP: 10895 case CTL_ACTION_ERROR: 10896 return (action); 10897 break; /* NOTREACHED */ 10898 case CTL_ACTION_PASS: 10899 break; 10900 default: 10901 panic("invalid action %d", action); 10902 break; /* NOTREACHED */ 10903 } 10904 } 10905 10906 return (CTL_ACTION_PASS); 10907} 10908 10909/* 10910 * Assumptions: 10911 * - An I/O has just completed, and has been removed from the per-LUN OOA 10912 * queue, so some items on the blocked queue may now be unblocked. 10913 */ 10914static int 10915ctl_check_blocked(struct ctl_lun *lun) 10916{ 10917 union ctl_io *cur_blocked, *next_blocked; 10918 10919 mtx_assert(&lun->lun_lock, MA_OWNED); 10920 10921 /* 10922 * Run forward from the head of the blocked queue, checking each 10923 * entry against the I/Os prior to it on the OOA queue to see if 10924 * there is still any blockage. 10925 * 10926 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10927 * with our removing a variable on it while it is traversing the 10928 * list. 10929 */ 10930 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10931 cur_blocked != NULL; cur_blocked = next_blocked) { 10932 union ctl_io *prev_ooa; 10933 ctl_action action; 10934 10935 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10936 blocked_links); 10937 10938 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10939 ctl_ooaq, ooa_links); 10940 10941 /* 10942 * If cur_blocked happens to be the first item in the OOA 10943 * queue now, prev_ooa will be NULL, and the action 10944 * returned will just be CTL_ACTION_PASS. 10945 */ 10946 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10947 10948 switch (action) { 10949 case CTL_ACTION_BLOCK: 10950 /* Nothing to do here, still blocked */ 10951 break; 10952 case CTL_ACTION_OVERLAP: 10953 case CTL_ACTION_OVERLAP_TAG: 10954 /* 10955 * This shouldn't happen! In theory we've already 10956 * checked this command for overlap... 10957 */ 10958 break; 10959 case CTL_ACTION_PASS: 10960 case CTL_ACTION_SKIP: { 10961 struct ctl_softc *softc; 10962 const struct ctl_cmd_entry *entry; 10963 int isc_retval; 10964 10965 /* 10966 * The skip case shouldn't happen, this transaction 10967 * should have never made it onto the blocked queue. 10968 */ 10969 /* 10970 * This I/O is no longer blocked, we can remove it 10971 * from the blocked queue. Since this is a TAILQ 10972 * (doubly linked list), we can do O(1) removals 10973 * from any place on the list. 10974 */ 10975 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10976 blocked_links); 10977 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10978 10979 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10980 /* 10981 * Need to send IO back to original side to 10982 * run 10983 */ 10984 union ctl_ha_msg msg_info; 10985 10986 msg_info.hdr.original_sc = 10987 cur_blocked->io_hdr.original_sc; 10988 msg_info.hdr.serializing_sc = cur_blocked; 10989 msg_info.hdr.msg_type = CTL_MSG_R2R; 10990 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10991 &msg_info, sizeof(msg_info), 0)) > 10992 CTL_HA_STATUS_SUCCESS) { 10993 printf("CTL:Check Blocked error from " 10994 "ctl_ha_msg_send %d\n", 10995 isc_retval); 10996 } 10997 break; 10998 } 10999 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11000 softc = control_softc; 11001 11002 /* 11003 * Check this I/O for LUN state changes that may 11004 * have happened while this command was blocked. 11005 * The LUN state may have been changed by a command 11006 * ahead of us in the queue, so we need to re-check 11007 * for any states that can be caused by SCSI 11008 * commands. 11009 */ 11010 if (ctl_scsiio_lun_check(softc, lun, entry, 11011 &cur_blocked->scsiio) == 0) { 11012 cur_blocked->io_hdr.flags |= 11013 CTL_FLAG_IS_WAS_ON_RTR; 11014 ctl_enqueue_rtr(cur_blocked); 11015 } else 11016 ctl_done(cur_blocked); 11017 break; 11018 } 11019 default: 11020 /* 11021 * This probably shouldn't happen -- we shouldn't 11022 * get CTL_ACTION_ERROR, or anything else. 11023 */ 11024 break; 11025 } 11026 } 11027 11028 return (CTL_RETVAL_COMPLETE); 11029} 11030 11031/* 11032 * This routine (with one exception) checks LUN flags that can be set by 11033 * commands ahead of us in the OOA queue. These flags have to be checked 11034 * when a command initially comes in, and when we pull a command off the 11035 * blocked queue and are preparing to execute it. The reason we have to 11036 * check these flags for commands on the blocked queue is that the LUN 11037 * state may have been changed by a command ahead of us while we're on the 11038 * blocked queue. 11039 * 11040 * Ordering is somewhat important with these checks, so please pay 11041 * careful attention to the placement of any new checks. 11042 */ 11043static int 11044ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11045 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11046{ 11047 int retval; 11048 uint32_t residx; 11049 11050 retval = 0; 11051 11052 mtx_assert(&lun->lun_lock, MA_OWNED); 11053 11054 /* 11055 * If this shelf is a secondary shelf controller, we have to reject 11056 * any media access commands. 11057 */ 11058 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11059 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11060 ctl_set_lun_standby(ctsio); 11061 retval = 1; 11062 goto bailout; 11063 } 11064 11065 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11066 if (lun->flags & CTL_LUN_READONLY) { 11067 ctl_set_sense(ctsio, /*current_error*/ 1, 11068 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11069 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11070 retval = 1; 11071 goto bailout; 11072 } 11073 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11074 .eca_and_aen & SCP_SWP) != 0) { 11075 ctl_set_sense(ctsio, /*current_error*/ 1, 11076 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11077 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11078 retval = 1; 11079 goto bailout; 11080 } 11081 } 11082 11083 /* 11084 * Check for a reservation conflict. If this command isn't allowed 11085 * even on reserved LUNs, and if this initiator isn't the one who 11086 * reserved us, reject the command with a reservation conflict. 11087 */ 11088 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11089 if ((lun->flags & CTL_LUN_RESERVED) 11090 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11091 if (lun->res_idx != residx) { 11092 ctl_set_reservation_conflict(ctsio); 11093 retval = 1; 11094 goto bailout; 11095 } 11096 } 11097 11098 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11099 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11100 /* No reservation or command is allowed. */; 11101 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11102 (lun->res_type == SPR_TYPE_WR_EX || 11103 lun->res_type == SPR_TYPE_WR_EX_RO || 11104 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11105 /* The command is allowed for Write Exclusive resv. */; 11106 } else { 11107 /* 11108 * if we aren't registered or it's a res holder type 11109 * reservation and this isn't the res holder then set a 11110 * conflict. 11111 */ 11112 if (ctl_get_prkey(lun, residx) == 0 11113 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11114 ctl_set_reservation_conflict(ctsio); 11115 retval = 1; 11116 goto bailout; 11117 } 11118 11119 } 11120 11121 if ((lun->flags & CTL_LUN_OFFLINE) 11122 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11123 ctl_set_lun_not_ready(ctsio); 11124 retval = 1; 11125 goto bailout; 11126 } 11127 11128 /* 11129 * If the LUN is stopped, see if this particular command is allowed 11130 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11131 */ 11132 if ((lun->flags & CTL_LUN_STOPPED) 11133 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11134 /* "Logical unit not ready, initializing cmd. required" */ 11135 ctl_set_lun_stopped(ctsio); 11136 retval = 1; 11137 goto bailout; 11138 } 11139 11140 if ((lun->flags & CTL_LUN_INOPERABLE) 11141 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11142 /* "Medium format corrupted" */ 11143 ctl_set_medium_format_corrupted(ctsio); 11144 retval = 1; 11145 goto bailout; 11146 } 11147 11148bailout: 11149 return (retval); 11150 11151} 11152 11153static void 11154ctl_failover_io(union ctl_io *io, int have_lock) 11155{ 11156 ctl_set_busy(&io->scsiio); 11157 ctl_done(io); 11158} 11159 11160static void 11161ctl_failover(void) 11162{ 11163 struct ctl_lun *lun; 11164 struct ctl_softc *ctl_softc; 11165 union ctl_io *next_io, *pending_io; 11166 union ctl_io *io; 11167 int lun_idx; 11168 int i; 11169 11170 ctl_softc = control_softc; 11171 11172 mtx_lock(&ctl_softc->ctl_lock); 11173 /* 11174 * Remove any cmds from the other SC from the rtr queue. These 11175 * will obviously only be for LUNs for which we're the primary. 11176 * We can't send status or get/send data for these commands. 11177 * Since they haven't been executed yet, we can just remove them. 11178 * We'll either abort them or delete them below, depending on 11179 * which HA mode we're in. 11180 */ 11181#ifdef notyet 11182 mtx_lock(&ctl_softc->queue_lock); 11183 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11184 io != NULL; io = next_io) { 11185 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11186 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11187 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11188 ctl_io_hdr, links); 11189 } 11190 mtx_unlock(&ctl_softc->queue_lock); 11191#endif 11192 11193 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11194 lun = ctl_softc->ctl_luns[lun_idx]; 11195 if (lun==NULL) 11196 continue; 11197 11198 /* 11199 * Processor LUNs are primary on both sides. 11200 * XXX will this always be true? 11201 */ 11202 if (lun->be_lun->lun_type == T_PROCESSOR) 11203 continue; 11204 11205 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11206 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11207 printf("FAILOVER: primary lun %d\n", lun_idx); 11208 /* 11209 * Remove all commands from the other SC. First from the 11210 * blocked queue then from the ooa queue. Once we have 11211 * removed them. Call ctl_check_blocked to see if there 11212 * is anything that can run. 11213 */ 11214 for (io = (union ctl_io *)TAILQ_FIRST( 11215 &lun->blocked_queue); io != NULL; io = next_io) { 11216 11217 next_io = (union ctl_io *)TAILQ_NEXT( 11218 &io->io_hdr, blocked_links); 11219 11220 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11221 TAILQ_REMOVE(&lun->blocked_queue, 11222 &io->io_hdr,blocked_links); 11223 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11224 TAILQ_REMOVE(&lun->ooa_queue, 11225 &io->io_hdr, ooa_links); 11226 11227 ctl_free_io(io); 11228 } 11229 } 11230 11231 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11232 io != NULL; io = next_io) { 11233 11234 next_io = (union ctl_io *)TAILQ_NEXT( 11235 &io->io_hdr, ooa_links); 11236 11237 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11238 11239 TAILQ_REMOVE(&lun->ooa_queue, 11240 &io->io_hdr, 11241 ooa_links); 11242 11243 ctl_free_io(io); 11244 } 11245 } 11246 ctl_check_blocked(lun); 11247 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11248 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11249 11250 printf("FAILOVER: primary lun %d\n", lun_idx); 11251 /* 11252 * Abort all commands from the other SC. We can't 11253 * send status back for them now. These should get 11254 * cleaned up when they are completed or come out 11255 * for a datamove operation. 11256 */ 11257 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11258 io != NULL; io = next_io) { 11259 next_io = (union ctl_io *)TAILQ_NEXT( 11260 &io->io_hdr, ooa_links); 11261 11262 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11263 io->io_hdr.flags |= CTL_FLAG_ABORT; 11264 } 11265 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11266 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11267 11268 printf("FAILOVER: secondary lun %d\n", lun_idx); 11269 11270 lun->flags |= CTL_LUN_PRIMARY_SC; 11271 11272 /* 11273 * We send all I/O that was sent to this controller 11274 * and redirected to the other side back with 11275 * busy status, and have the initiator retry it. 11276 * Figuring out how much data has been transferred, 11277 * etc. and picking up where we left off would be 11278 * very tricky. 11279 * 11280 * XXX KDM need to remove I/O from the blocked 11281 * queue as well! 11282 */ 11283 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11284 &lun->ooa_queue); pending_io != NULL; 11285 pending_io = next_io) { 11286 11287 next_io = (union ctl_io *)TAILQ_NEXT( 11288 &pending_io->io_hdr, ooa_links); 11289 11290 pending_io->io_hdr.flags &= 11291 ~CTL_FLAG_SENT_2OTHER_SC; 11292 11293 if (pending_io->io_hdr.flags & 11294 CTL_FLAG_IO_ACTIVE) { 11295 pending_io->io_hdr.flags |= 11296 CTL_FLAG_FAILOVER; 11297 } else { 11298 ctl_set_busy(&pending_io->scsiio); 11299 ctl_done(pending_io); 11300 } 11301 } 11302 11303 /* 11304 * Build Unit Attention 11305 */ 11306 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11307 lun->pending_ua[i] |= 11308 CTL_UA_ASYM_ACC_CHANGE; 11309 } 11310 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11311 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11312 printf("FAILOVER: secondary lun %d\n", lun_idx); 11313 /* 11314 * if the first io on the OOA is not on the RtR queue 11315 * add it. 11316 */ 11317 lun->flags |= CTL_LUN_PRIMARY_SC; 11318 11319 pending_io = (union ctl_io *)TAILQ_FIRST( 11320 &lun->ooa_queue); 11321 if (pending_io==NULL) { 11322 printf("Nothing on OOA queue\n"); 11323 continue; 11324 } 11325 11326 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11327 if ((pending_io->io_hdr.flags & 11328 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11329 pending_io->io_hdr.flags |= 11330 CTL_FLAG_IS_WAS_ON_RTR; 11331 ctl_enqueue_rtr(pending_io); 11332 } 11333#if 0 11334 else 11335 { 11336 printf("Tag 0x%04x is running\n", 11337 pending_io->scsiio.tag_num); 11338 } 11339#endif 11340 11341 next_io = (union ctl_io *)TAILQ_NEXT( 11342 &pending_io->io_hdr, ooa_links); 11343 for (pending_io=next_io; pending_io != NULL; 11344 pending_io = next_io) { 11345 pending_io->io_hdr.flags &= 11346 ~CTL_FLAG_SENT_2OTHER_SC; 11347 next_io = (union ctl_io *)TAILQ_NEXT( 11348 &pending_io->io_hdr, ooa_links); 11349 if (pending_io->io_hdr.flags & 11350 CTL_FLAG_IS_WAS_ON_RTR) { 11351#if 0 11352 printf("Tag 0x%04x is running\n", 11353 pending_io->scsiio.tag_num); 11354#endif 11355 continue; 11356 } 11357 11358 switch (ctl_check_ooa(lun, pending_io, 11359 (union ctl_io *)TAILQ_PREV( 11360 &pending_io->io_hdr, ctl_ooaq, 11361 ooa_links))) { 11362 11363 case CTL_ACTION_BLOCK: 11364 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11365 &pending_io->io_hdr, 11366 blocked_links); 11367 pending_io->io_hdr.flags |= 11368 CTL_FLAG_BLOCKED; 11369 break; 11370 case CTL_ACTION_PASS: 11371 case CTL_ACTION_SKIP: 11372 pending_io->io_hdr.flags |= 11373 CTL_FLAG_IS_WAS_ON_RTR; 11374 ctl_enqueue_rtr(pending_io); 11375 break; 11376 case CTL_ACTION_OVERLAP: 11377 ctl_set_overlapped_cmd( 11378 (struct ctl_scsiio *)pending_io); 11379 ctl_done(pending_io); 11380 break; 11381 case CTL_ACTION_OVERLAP_TAG: 11382 ctl_set_overlapped_tag( 11383 (struct ctl_scsiio *)pending_io, 11384 pending_io->scsiio.tag_num & 0xff); 11385 ctl_done(pending_io); 11386 break; 11387 case CTL_ACTION_ERROR: 11388 default: 11389 ctl_set_internal_failure( 11390 (struct ctl_scsiio *)pending_io, 11391 0, // sks_valid 11392 0); //retry count 11393 ctl_done(pending_io); 11394 break; 11395 } 11396 } 11397 11398 /* 11399 * Build Unit Attention 11400 */ 11401 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11402 lun->pending_ua[i] |= 11403 CTL_UA_ASYM_ACC_CHANGE; 11404 } 11405 } else { 11406 panic("Unhandled HA mode failover, LUN flags = %#x, " 11407 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11408 } 11409 } 11410 ctl_pause_rtr = 0; 11411 mtx_unlock(&ctl_softc->ctl_lock); 11412} 11413 11414static int 11415ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11416{ 11417 struct ctl_lun *lun; 11418 const struct ctl_cmd_entry *entry; 11419 uint32_t initidx, targ_lun; 11420 int retval; 11421 11422 retval = 0; 11423 11424 lun = NULL; 11425 11426 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11427 if ((targ_lun < CTL_MAX_LUNS) 11428 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11429 /* 11430 * If the LUN is invalid, pretend that it doesn't exist. 11431 * It will go away as soon as all pending I/O has been 11432 * completed. 11433 */ 11434 mtx_lock(&lun->lun_lock); 11435 if (lun->flags & CTL_LUN_DISABLED) { 11436 mtx_unlock(&lun->lun_lock); 11437 lun = NULL; 11438 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11439 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11440 } else { 11441 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11442 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11443 lun->be_lun; 11444 if (lun->be_lun->lun_type == T_PROCESSOR) { 11445 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11446 } 11447 11448 /* 11449 * Every I/O goes into the OOA queue for a 11450 * particular LUN, and stays there until completion. 11451 */ 11452 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11453 ooa_links); 11454 } 11455 } else { 11456 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11457 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11458 } 11459 11460 /* Get command entry and return error if it is unsuppotyed. */ 11461 entry = ctl_validate_command(ctsio); 11462 if (entry == NULL) { 11463 if (lun) 11464 mtx_unlock(&lun->lun_lock); 11465 return (retval); 11466 } 11467 11468 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11469 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11470 11471 /* 11472 * Check to see whether we can send this command to LUNs that don't 11473 * exist. This should pretty much only be the case for inquiry 11474 * and request sense. Further checks, below, really require having 11475 * a LUN, so we can't really check the command anymore. Just put 11476 * it on the rtr queue. 11477 */ 11478 if (lun == NULL) { 11479 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11480 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11481 ctl_enqueue_rtr((union ctl_io *)ctsio); 11482 return (retval); 11483 } 11484 11485 ctl_set_unsupported_lun(ctsio); 11486 ctl_done((union ctl_io *)ctsio); 11487 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11488 return (retval); 11489 } else { 11490 /* 11491 * Make sure we support this particular command on this LUN. 11492 * e.g., we don't support writes to the control LUN. 11493 */ 11494 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11495 mtx_unlock(&lun->lun_lock); 11496 ctl_set_invalid_opcode(ctsio); 11497 ctl_done((union ctl_io *)ctsio); 11498 return (retval); 11499 } 11500 } 11501 11502 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11503 11504#ifdef CTL_WITH_CA 11505 /* 11506 * If we've got a request sense, it'll clear the contingent 11507 * allegiance condition. Otherwise, if we have a CA condition for 11508 * this initiator, clear it, because it sent down a command other 11509 * than request sense. 11510 */ 11511 if ((ctsio->cdb[0] != REQUEST_SENSE) 11512 && (ctl_is_set(lun->have_ca, initidx))) 11513 ctl_clear_mask(lun->have_ca, initidx); 11514#endif 11515 11516 /* 11517 * If the command has this flag set, it handles its own unit 11518 * attention reporting, we shouldn't do anything. Otherwise we 11519 * check for any pending unit attentions, and send them back to the 11520 * initiator. We only do this when a command initially comes in, 11521 * not when we pull it off the blocked queue. 11522 * 11523 * According to SAM-3, section 5.3.2, the order that things get 11524 * presented back to the host is basically unit attentions caused 11525 * by some sort of reset event, busy status, reservation conflicts 11526 * or task set full, and finally any other status. 11527 * 11528 * One issue here is that some of the unit attentions we report 11529 * don't fall into the "reset" category (e.g. "reported luns data 11530 * has changed"). So reporting it here, before the reservation 11531 * check, may be technically wrong. I guess the only thing to do 11532 * would be to check for and report the reset events here, and then 11533 * check for the other unit attention types after we check for a 11534 * reservation conflict. 11535 * 11536 * XXX KDM need to fix this 11537 */ 11538 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11539 ctl_ua_type ua_type; 11540 11541 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11542 scsi_sense_data_type sense_format; 11543 11544 if (lun != NULL) 11545 sense_format = (lun->flags & 11546 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11547 SSD_TYPE_FIXED; 11548 else 11549 sense_format = SSD_TYPE_FIXED; 11550 11551 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11552 &ctsio->sense_data, sense_format); 11553 if (ua_type != CTL_UA_NONE) { 11554 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11555 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11556 CTL_AUTOSENSE; 11557 ctsio->sense_len = SSD_FULL_SIZE; 11558 mtx_unlock(&lun->lun_lock); 11559 ctl_done((union ctl_io *)ctsio); 11560 return (retval); 11561 } 11562 } 11563 } 11564 11565 11566 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11567 mtx_unlock(&lun->lun_lock); 11568 ctl_done((union ctl_io *)ctsio); 11569 return (retval); 11570 } 11571 11572 /* 11573 * XXX CHD this is where we want to send IO to other side if 11574 * this LUN is secondary on this SC. We will need to make a copy 11575 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11576 * the copy we send as FROM_OTHER. 11577 * We also need to stuff the address of the original IO so we can 11578 * find it easily. Something similar will need be done on the other 11579 * side so when we are done we can find the copy. 11580 */ 11581 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11582 union ctl_ha_msg msg_info; 11583 int isc_retval; 11584 11585 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11586 11587 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11588 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11589#if 0 11590 printf("1. ctsio %p\n", ctsio); 11591#endif 11592 msg_info.hdr.serializing_sc = NULL; 11593 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11594 msg_info.scsi.tag_num = ctsio->tag_num; 11595 msg_info.scsi.tag_type = ctsio->tag_type; 11596 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11597 11598 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11599 11600 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11601 (void *)&msg_info, sizeof(msg_info), 0)) > 11602 CTL_HA_STATUS_SUCCESS) { 11603 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11604 isc_retval); 11605 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11606 } else { 11607#if 0 11608 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11609#endif 11610 } 11611 11612 /* 11613 * XXX KDM this I/O is off the incoming queue, but hasn't 11614 * been inserted on any other queue. We may need to come 11615 * up with a holding queue while we wait for serialization 11616 * so that we have an idea of what we're waiting for from 11617 * the other side. 11618 */ 11619 mtx_unlock(&lun->lun_lock); 11620 return (retval); 11621 } 11622 11623 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11624 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11625 ctl_ooaq, ooa_links))) { 11626 case CTL_ACTION_BLOCK: 11627 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11628 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11629 blocked_links); 11630 mtx_unlock(&lun->lun_lock); 11631 return (retval); 11632 case CTL_ACTION_PASS: 11633 case CTL_ACTION_SKIP: 11634 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11635 mtx_unlock(&lun->lun_lock); 11636 ctl_enqueue_rtr((union ctl_io *)ctsio); 11637 break; 11638 case CTL_ACTION_OVERLAP: 11639 mtx_unlock(&lun->lun_lock); 11640 ctl_set_overlapped_cmd(ctsio); 11641 ctl_done((union ctl_io *)ctsio); 11642 break; 11643 case CTL_ACTION_OVERLAP_TAG: 11644 mtx_unlock(&lun->lun_lock); 11645 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11646 ctl_done((union ctl_io *)ctsio); 11647 break; 11648 case CTL_ACTION_ERROR: 11649 default: 11650 mtx_unlock(&lun->lun_lock); 11651 ctl_set_internal_failure(ctsio, 11652 /*sks_valid*/ 0, 11653 /*retry_count*/ 0); 11654 ctl_done((union ctl_io *)ctsio); 11655 break; 11656 } 11657 return (retval); 11658} 11659 11660const struct ctl_cmd_entry * 11661ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11662{ 11663 const struct ctl_cmd_entry *entry; 11664 int service_action; 11665 11666 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11667 if (sa) 11668 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11669 if (entry->flags & CTL_CMD_FLAG_SA5) { 11670 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11671 entry = &((const struct ctl_cmd_entry *) 11672 entry->execute)[service_action]; 11673 } 11674 return (entry); 11675} 11676 11677const struct ctl_cmd_entry * 11678ctl_validate_command(struct ctl_scsiio *ctsio) 11679{ 11680 const struct ctl_cmd_entry *entry; 11681 int i, sa; 11682 uint8_t diff; 11683 11684 entry = ctl_get_cmd_entry(ctsio, &sa); 11685 if (entry->execute == NULL) { 11686 if (sa) 11687 ctl_set_invalid_field(ctsio, 11688 /*sks_valid*/ 1, 11689 /*command*/ 1, 11690 /*field*/ 1, 11691 /*bit_valid*/ 1, 11692 /*bit*/ 4); 11693 else 11694 ctl_set_invalid_opcode(ctsio); 11695 ctl_done((union ctl_io *)ctsio); 11696 return (NULL); 11697 } 11698 KASSERT(entry->length > 0, 11699 ("Not defined length for command 0x%02x/0x%02x", 11700 ctsio->cdb[0], ctsio->cdb[1])); 11701 for (i = 1; i < entry->length; i++) { 11702 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11703 if (diff == 0) 11704 continue; 11705 ctl_set_invalid_field(ctsio, 11706 /*sks_valid*/ 1, 11707 /*command*/ 1, 11708 /*field*/ i, 11709 /*bit_valid*/ 1, 11710 /*bit*/ fls(diff) - 1); 11711 ctl_done((union ctl_io *)ctsio); 11712 return (NULL); 11713 } 11714 return (entry); 11715} 11716 11717static int 11718ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11719{ 11720 11721 switch (lun_type) { 11722 case T_PROCESSOR: 11723 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11724 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11725 return (0); 11726 break; 11727 case T_DIRECT: 11728 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11729 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11730 return (0); 11731 break; 11732 default: 11733 return (0); 11734 } 11735 return (1); 11736} 11737 11738static int 11739ctl_scsiio(struct ctl_scsiio *ctsio) 11740{ 11741 int retval; 11742 const struct ctl_cmd_entry *entry; 11743 11744 retval = CTL_RETVAL_COMPLETE; 11745 11746 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11747 11748 entry = ctl_get_cmd_entry(ctsio, NULL); 11749 11750 /* 11751 * If this I/O has been aborted, just send it straight to 11752 * ctl_done() without executing it. 11753 */ 11754 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11755 ctl_done((union ctl_io *)ctsio); 11756 goto bailout; 11757 } 11758 11759 /* 11760 * All the checks should have been handled by ctl_scsiio_precheck(). 11761 * We should be clear now to just execute the I/O. 11762 */ 11763 retval = entry->execute(ctsio); 11764 11765bailout: 11766 return (retval); 11767} 11768 11769/* 11770 * Since we only implement one target right now, a bus reset simply resets 11771 * our single target. 11772 */ 11773static int 11774ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11775{ 11776 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11777} 11778 11779static int 11780ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11781 ctl_ua_type ua_type) 11782{ 11783 struct ctl_lun *lun; 11784 int retval; 11785 11786 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11787 union ctl_ha_msg msg_info; 11788 11789 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11790 msg_info.hdr.nexus = io->io_hdr.nexus; 11791 if (ua_type==CTL_UA_TARG_RESET) 11792 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11793 else 11794 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11795 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11796 msg_info.hdr.original_sc = NULL; 11797 msg_info.hdr.serializing_sc = NULL; 11798 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11799 (void *)&msg_info, sizeof(msg_info), 0)) { 11800 } 11801 } 11802 retval = 0; 11803 11804 mtx_lock(&ctl_softc->ctl_lock); 11805 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11806 retval += ctl_lun_reset(lun, io, ua_type); 11807 mtx_unlock(&ctl_softc->ctl_lock); 11808 11809 return (retval); 11810} 11811 11812/* 11813 * The LUN should always be set. The I/O is optional, and is used to 11814 * distinguish between I/Os sent by this initiator, and by other 11815 * initiators. We set unit attention for initiators other than this one. 11816 * SAM-3 is vague on this point. It does say that a unit attention should 11817 * be established for other initiators when a LUN is reset (see section 11818 * 5.7.3), but it doesn't specifically say that the unit attention should 11819 * be established for this particular initiator when a LUN is reset. Here 11820 * is the relevant text, from SAM-3 rev 8: 11821 * 11822 * 5.7.2 When a SCSI initiator port aborts its own tasks 11823 * 11824 * When a SCSI initiator port causes its own task(s) to be aborted, no 11825 * notification that the task(s) have been aborted shall be returned to 11826 * the SCSI initiator port other than the completion response for the 11827 * command or task management function action that caused the task(s) to 11828 * be aborted and notification(s) associated with related effects of the 11829 * action (e.g., a reset unit attention condition). 11830 * 11831 * XXX KDM for now, we're setting unit attention for all initiators. 11832 */ 11833static int 11834ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11835{ 11836 union ctl_io *xio; 11837#if 0 11838 uint32_t initidx; 11839#endif 11840 int i; 11841 11842 mtx_lock(&lun->lun_lock); 11843 /* 11844 * Run through the OOA queue and abort each I/O. 11845 */ 11846#if 0 11847 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11848#endif 11849 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11850 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11851 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11852 } 11853 11854 /* 11855 * This version sets unit attention for every 11856 */ 11857#if 0 11858 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11859 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11860 if (initidx == i) 11861 continue; 11862 lun->pending_ua[i] |= ua_type; 11863 } 11864#endif 11865 11866 /* 11867 * A reset (any kind, really) clears reservations established with 11868 * RESERVE/RELEASE. It does not clear reservations established 11869 * with PERSISTENT RESERVE OUT, but we don't support that at the 11870 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11871 * reservations made with the RESERVE/RELEASE commands, because 11872 * those commands are obsolete in SPC-3. 11873 */ 11874 lun->flags &= ~CTL_LUN_RESERVED; 11875 11876 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11877#ifdef CTL_WITH_CA 11878 ctl_clear_mask(lun->have_ca, i); 11879#endif 11880 lun->pending_ua[i] |= ua_type; 11881 } 11882 mtx_unlock(&lun->lun_lock); 11883 11884 return (0); 11885} 11886 11887static void 11888ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11889 int other_sc) 11890{ 11891 union ctl_io *xio; 11892 11893 mtx_assert(&lun->lun_lock, MA_OWNED); 11894 11895 /* 11896 * Run through the OOA queue and attempt to find the given I/O. 11897 * The target port, initiator ID, tag type and tag number have to 11898 * match the values that we got from the initiator. If we have an 11899 * untagged command to abort, simply abort the first untagged command 11900 * we come to. We only allow one untagged command at a time of course. 11901 */ 11902 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11903 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11904 11905 if ((targ_port == UINT32_MAX || 11906 targ_port == xio->io_hdr.nexus.targ_port) && 11907 (init_id == UINT32_MAX || 11908 init_id == xio->io_hdr.nexus.initid.id)) { 11909 if (targ_port != xio->io_hdr.nexus.targ_port || 11910 init_id != xio->io_hdr.nexus.initid.id) 11911 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11912 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11913 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11914 union ctl_ha_msg msg_info; 11915 11916 msg_info.hdr.nexus = xio->io_hdr.nexus; 11917 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11918 msg_info.task.tag_num = xio->scsiio.tag_num; 11919 msg_info.task.tag_type = xio->scsiio.tag_type; 11920 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11921 msg_info.hdr.original_sc = NULL; 11922 msg_info.hdr.serializing_sc = NULL; 11923 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11924 (void *)&msg_info, sizeof(msg_info), 0); 11925 } 11926 } 11927 } 11928} 11929 11930static int 11931ctl_abort_task_set(union ctl_io *io) 11932{ 11933 struct ctl_softc *softc = control_softc; 11934 struct ctl_lun *lun; 11935 uint32_t targ_lun; 11936 11937 /* 11938 * Look up the LUN. 11939 */ 11940 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11941 mtx_lock(&softc->ctl_lock); 11942 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11943 lun = softc->ctl_luns[targ_lun]; 11944 else { 11945 mtx_unlock(&softc->ctl_lock); 11946 return (1); 11947 } 11948 11949 mtx_lock(&lun->lun_lock); 11950 mtx_unlock(&softc->ctl_lock); 11951 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11952 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11953 io->io_hdr.nexus.initid.id, 11954 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11955 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11956 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11957 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11958 } 11959 mtx_unlock(&lun->lun_lock); 11960 return (0); 11961} 11962 11963static int 11964ctl_i_t_nexus_reset(union ctl_io *io) 11965{ 11966 struct ctl_softc *softc = control_softc; 11967 struct ctl_lun *lun; 11968 uint32_t initidx, residx; 11969 11970 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11971 residx = ctl_get_resindex(&io->io_hdr.nexus); 11972 mtx_lock(&softc->ctl_lock); 11973 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11974 mtx_lock(&lun->lun_lock); 11975 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11976 io->io_hdr.nexus.initid.id, 11977 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11978#ifdef CTL_WITH_CA 11979 ctl_clear_mask(lun->have_ca, initidx); 11980#endif 11981 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 11982 lun->flags &= ~CTL_LUN_RESERVED; 11983 lun->pending_ua[initidx] |= CTL_UA_I_T_NEXUS_LOSS; 11984 mtx_unlock(&lun->lun_lock); 11985 } 11986 mtx_unlock(&softc->ctl_lock); 11987 return (0); 11988} 11989 11990static int 11991ctl_abort_task(union ctl_io *io) 11992{ 11993 union ctl_io *xio; 11994 struct ctl_lun *lun; 11995 struct ctl_softc *ctl_softc; 11996#if 0 11997 struct sbuf sb; 11998 char printbuf[128]; 11999#endif 12000 int found; 12001 uint32_t targ_lun; 12002 12003 ctl_softc = control_softc; 12004 found = 0; 12005 12006 /* 12007 * Look up the LUN. 12008 */ 12009 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12010 mtx_lock(&ctl_softc->ctl_lock); 12011 if ((targ_lun < CTL_MAX_LUNS) 12012 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12013 lun = ctl_softc->ctl_luns[targ_lun]; 12014 else { 12015 mtx_unlock(&ctl_softc->ctl_lock); 12016 return (1); 12017 } 12018 12019#if 0 12020 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12021 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12022#endif 12023 12024 mtx_lock(&lun->lun_lock); 12025 mtx_unlock(&ctl_softc->ctl_lock); 12026 /* 12027 * Run through the OOA queue and attempt to find the given I/O. 12028 * The target port, initiator ID, tag type and tag number have to 12029 * match the values that we got from the initiator. If we have an 12030 * untagged command to abort, simply abort the first untagged command 12031 * we come to. We only allow one untagged command at a time of course. 12032 */ 12033#if 0 12034 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12035#endif 12036 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12037 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12038#if 0 12039 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12040 12041 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12042 lun->lun, xio->scsiio.tag_num, 12043 xio->scsiio.tag_type, 12044 (xio->io_hdr.blocked_links.tqe_prev 12045 == NULL) ? "" : " BLOCKED", 12046 (xio->io_hdr.flags & 12047 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12048 (xio->io_hdr.flags & 12049 CTL_FLAG_ABORT) ? " ABORT" : "", 12050 (xio->io_hdr.flags & 12051 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12052 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12053 sbuf_finish(&sb); 12054 printf("%s\n", sbuf_data(&sb)); 12055#endif 12056 12057 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12058 && (xio->io_hdr.nexus.initid.id == 12059 io->io_hdr.nexus.initid.id)) { 12060 /* 12061 * If the abort says that the task is untagged, the 12062 * task in the queue must be untagged. Otherwise, 12063 * we just check to see whether the tag numbers 12064 * match. This is because the QLogic firmware 12065 * doesn't pass back the tag type in an abort 12066 * request. 12067 */ 12068#if 0 12069 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12070 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12071 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12072#endif 12073 /* 12074 * XXX KDM we've got problems with FC, because it 12075 * doesn't send down a tag type with aborts. So we 12076 * can only really go by the tag number... 12077 * This may cause problems with parallel SCSI. 12078 * Need to figure that out!! 12079 */ 12080 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12081 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12082 found = 1; 12083 if ((io->io_hdr.flags & 12084 CTL_FLAG_FROM_OTHER_SC) == 0 && 12085 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12086 union ctl_ha_msg msg_info; 12087 12088 io->io_hdr.flags |= 12089 CTL_FLAG_SENT_2OTHER_SC; 12090 msg_info.hdr.nexus = io->io_hdr.nexus; 12091 msg_info.task.task_action = 12092 CTL_TASK_ABORT_TASK; 12093 msg_info.task.tag_num = 12094 io->taskio.tag_num; 12095 msg_info.task.tag_type = 12096 io->taskio.tag_type; 12097 msg_info.hdr.msg_type = 12098 CTL_MSG_MANAGE_TASKS; 12099 msg_info.hdr.original_sc = NULL; 12100 msg_info.hdr.serializing_sc = NULL; 12101#if 0 12102 printf("Sent Abort to other side\n"); 12103#endif 12104 if (CTL_HA_STATUS_SUCCESS != 12105 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12106 (void *)&msg_info, 12107 sizeof(msg_info), 0)) { 12108 } 12109 } 12110#if 0 12111 printf("ctl_abort_task: found I/O to abort\n"); 12112#endif 12113 break; 12114 } 12115 } 12116 } 12117 mtx_unlock(&lun->lun_lock); 12118 12119 if (found == 0) { 12120 /* 12121 * This isn't really an error. It's entirely possible for 12122 * the abort and command completion to cross on the wire. 12123 * This is more of an informative/diagnostic error. 12124 */ 12125#if 0 12126 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12127 "%d:%d:%d:%d tag %d type %d\n", 12128 io->io_hdr.nexus.initid.id, 12129 io->io_hdr.nexus.targ_port, 12130 io->io_hdr.nexus.targ_target.id, 12131 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12132 io->taskio.tag_type); 12133#endif 12134 } 12135 return (0); 12136} 12137 12138static void 12139ctl_run_task(union ctl_io *io) 12140{ 12141 struct ctl_softc *ctl_softc = control_softc; 12142 int retval = 1; 12143 const char *task_desc; 12144 12145 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12146 12147 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12148 ("ctl_run_task: Unextected io_type %d\n", 12149 io->io_hdr.io_type)); 12150 12151 task_desc = ctl_scsi_task_string(&io->taskio); 12152 if (task_desc != NULL) { 12153#ifdef NEEDTOPORT 12154 csevent_log(CSC_CTL | CSC_SHELF_SW | 12155 CTL_TASK_REPORT, 12156 csevent_LogType_Trace, 12157 csevent_Severity_Information, 12158 csevent_AlertLevel_Green, 12159 csevent_FRU_Firmware, 12160 csevent_FRU_Unknown, 12161 "CTL: received task: %s",task_desc); 12162#endif 12163 } else { 12164#ifdef NEEDTOPORT 12165 csevent_log(CSC_CTL | CSC_SHELF_SW | 12166 CTL_TASK_REPORT, 12167 csevent_LogType_Trace, 12168 csevent_Severity_Information, 12169 csevent_AlertLevel_Green, 12170 csevent_FRU_Firmware, 12171 csevent_FRU_Unknown, 12172 "CTL: received unknown task " 12173 "type: %d (%#x)", 12174 io->taskio.task_action, 12175 io->taskio.task_action); 12176#endif 12177 } 12178 switch (io->taskio.task_action) { 12179 case CTL_TASK_ABORT_TASK: 12180 retval = ctl_abort_task(io); 12181 break; 12182 case CTL_TASK_ABORT_TASK_SET: 12183 case CTL_TASK_CLEAR_TASK_SET: 12184 retval = ctl_abort_task_set(io); 12185 break; 12186 case CTL_TASK_CLEAR_ACA: 12187 break; 12188 case CTL_TASK_I_T_NEXUS_RESET: 12189 retval = ctl_i_t_nexus_reset(io); 12190 break; 12191 case CTL_TASK_LUN_RESET: { 12192 struct ctl_lun *lun; 12193 uint32_t targ_lun; 12194 12195 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12196 mtx_lock(&ctl_softc->ctl_lock); 12197 if ((targ_lun < CTL_MAX_LUNS) 12198 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12199 lun = ctl_softc->ctl_luns[targ_lun]; 12200 else { 12201 mtx_unlock(&ctl_softc->ctl_lock); 12202 retval = 1; 12203 break; 12204 } 12205 12206 if (!(io->io_hdr.flags & 12207 CTL_FLAG_FROM_OTHER_SC)) { 12208 union ctl_ha_msg msg_info; 12209 12210 io->io_hdr.flags |= 12211 CTL_FLAG_SENT_2OTHER_SC; 12212 msg_info.hdr.msg_type = 12213 CTL_MSG_MANAGE_TASKS; 12214 msg_info.hdr.nexus = io->io_hdr.nexus; 12215 msg_info.task.task_action = 12216 CTL_TASK_LUN_RESET; 12217 msg_info.hdr.original_sc = NULL; 12218 msg_info.hdr.serializing_sc = NULL; 12219 if (CTL_HA_STATUS_SUCCESS != 12220 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12221 (void *)&msg_info, 12222 sizeof(msg_info), 0)) { 12223 } 12224 } 12225 12226 retval = ctl_lun_reset(lun, io, 12227 CTL_UA_LUN_RESET); 12228 mtx_unlock(&ctl_softc->ctl_lock); 12229 break; 12230 } 12231 case CTL_TASK_TARGET_RESET: 12232 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12233 break; 12234 case CTL_TASK_BUS_RESET: 12235 retval = ctl_bus_reset(ctl_softc, io); 12236 break; 12237 case CTL_TASK_PORT_LOGIN: 12238 break; 12239 case CTL_TASK_PORT_LOGOUT: 12240 break; 12241 default: 12242 printf("ctl_run_task: got unknown task management event %d\n", 12243 io->taskio.task_action); 12244 break; 12245 } 12246 if (retval == 0) 12247 io->io_hdr.status = CTL_SUCCESS; 12248 else 12249 io->io_hdr.status = CTL_ERROR; 12250 ctl_done(io); 12251} 12252 12253/* 12254 * For HA operation. Handle commands that come in from the other 12255 * controller. 12256 */ 12257static void 12258ctl_handle_isc(union ctl_io *io) 12259{ 12260 int free_io; 12261 struct ctl_lun *lun; 12262 struct ctl_softc *ctl_softc; 12263 uint32_t targ_lun; 12264 12265 ctl_softc = control_softc; 12266 12267 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12268 lun = ctl_softc->ctl_luns[targ_lun]; 12269 12270 switch (io->io_hdr.msg_type) { 12271 case CTL_MSG_SERIALIZE: 12272 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12273 break; 12274 case CTL_MSG_R2R: { 12275 const struct ctl_cmd_entry *entry; 12276 12277 /* 12278 * This is only used in SER_ONLY mode. 12279 */ 12280 free_io = 0; 12281 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12282 mtx_lock(&lun->lun_lock); 12283 if (ctl_scsiio_lun_check(ctl_softc, lun, 12284 entry, (struct ctl_scsiio *)io) != 0) { 12285 mtx_unlock(&lun->lun_lock); 12286 ctl_done(io); 12287 break; 12288 } 12289 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12290 mtx_unlock(&lun->lun_lock); 12291 ctl_enqueue_rtr(io); 12292 break; 12293 } 12294 case CTL_MSG_FINISH_IO: 12295 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12296 free_io = 0; 12297 ctl_done(io); 12298 } else { 12299 free_io = 1; 12300 mtx_lock(&lun->lun_lock); 12301 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12302 ooa_links); 12303 ctl_check_blocked(lun); 12304 mtx_unlock(&lun->lun_lock); 12305 } 12306 break; 12307 case CTL_MSG_PERS_ACTION: 12308 ctl_hndl_per_res_out_on_other_sc( 12309 (union ctl_ha_msg *)&io->presio.pr_msg); 12310 free_io = 1; 12311 break; 12312 case CTL_MSG_BAD_JUJU: 12313 free_io = 0; 12314 ctl_done(io); 12315 break; 12316 case CTL_MSG_DATAMOVE: 12317 /* Only used in XFER mode */ 12318 free_io = 0; 12319 ctl_datamove_remote(io); 12320 break; 12321 case CTL_MSG_DATAMOVE_DONE: 12322 /* Only used in XFER mode */ 12323 free_io = 0; 12324 io->scsiio.be_move_done(io); 12325 break; 12326 default: 12327 free_io = 1; 12328 printf("%s: Invalid message type %d\n", 12329 __func__, io->io_hdr.msg_type); 12330 break; 12331 } 12332 if (free_io) 12333 ctl_free_io(io); 12334 12335} 12336 12337 12338/* 12339 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12340 * there is no match. 12341 */ 12342static ctl_lun_error_pattern 12343ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12344{ 12345 const struct ctl_cmd_entry *entry; 12346 ctl_lun_error_pattern filtered_pattern, pattern; 12347 12348 pattern = desc->error_pattern; 12349 12350 /* 12351 * XXX KDM we need more data passed into this function to match a 12352 * custom pattern, and we actually need to implement custom pattern 12353 * matching. 12354 */ 12355 if (pattern & CTL_LUN_PAT_CMD) 12356 return (CTL_LUN_PAT_CMD); 12357 12358 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12359 return (CTL_LUN_PAT_ANY); 12360 12361 entry = ctl_get_cmd_entry(ctsio, NULL); 12362 12363 filtered_pattern = entry->pattern & pattern; 12364 12365 /* 12366 * If the user requested specific flags in the pattern (e.g. 12367 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12368 * flags. 12369 * 12370 * If the user did not specify any flags, it doesn't matter whether 12371 * or not the command supports the flags. 12372 */ 12373 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12374 (pattern & ~CTL_LUN_PAT_MASK)) 12375 return (CTL_LUN_PAT_NONE); 12376 12377 /* 12378 * If the user asked for a range check, see if the requested LBA 12379 * range overlaps with this command's LBA range. 12380 */ 12381 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12382 uint64_t lba1; 12383 uint64_t len1; 12384 ctl_action action; 12385 int retval; 12386 12387 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12388 if (retval != 0) 12389 return (CTL_LUN_PAT_NONE); 12390 12391 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12392 desc->lba_range.len); 12393 /* 12394 * A "pass" means that the LBA ranges don't overlap, so 12395 * this doesn't match the user's range criteria. 12396 */ 12397 if (action == CTL_ACTION_PASS) 12398 return (CTL_LUN_PAT_NONE); 12399 } 12400 12401 return (filtered_pattern); 12402} 12403 12404static void 12405ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12406{ 12407 struct ctl_error_desc *desc, *desc2; 12408 12409 mtx_assert(&lun->lun_lock, MA_OWNED); 12410 12411 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12412 ctl_lun_error_pattern pattern; 12413 /* 12414 * Check to see whether this particular command matches 12415 * the pattern in the descriptor. 12416 */ 12417 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12418 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12419 continue; 12420 12421 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12422 case CTL_LUN_INJ_ABORTED: 12423 ctl_set_aborted(&io->scsiio); 12424 break; 12425 case CTL_LUN_INJ_MEDIUM_ERR: 12426 ctl_set_medium_error(&io->scsiio); 12427 break; 12428 case CTL_LUN_INJ_UA: 12429 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12430 * OCCURRED */ 12431 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12432 break; 12433 case CTL_LUN_INJ_CUSTOM: 12434 /* 12435 * We're assuming the user knows what he is doing. 12436 * Just copy the sense information without doing 12437 * checks. 12438 */ 12439 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12440 ctl_min(sizeof(desc->custom_sense), 12441 sizeof(io->scsiio.sense_data))); 12442 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12443 io->scsiio.sense_len = SSD_FULL_SIZE; 12444 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12445 break; 12446 case CTL_LUN_INJ_NONE: 12447 default: 12448 /* 12449 * If this is an error injection type we don't know 12450 * about, clear the continuous flag (if it is set) 12451 * so it will get deleted below. 12452 */ 12453 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12454 break; 12455 } 12456 /* 12457 * By default, each error injection action is a one-shot 12458 */ 12459 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12460 continue; 12461 12462 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12463 12464 free(desc, M_CTL); 12465 } 12466} 12467 12468#ifdef CTL_IO_DELAY 12469static void 12470ctl_datamove_timer_wakeup(void *arg) 12471{ 12472 union ctl_io *io; 12473 12474 io = (union ctl_io *)arg; 12475 12476 ctl_datamove(io); 12477} 12478#endif /* CTL_IO_DELAY */ 12479 12480void 12481ctl_datamove(union ctl_io *io) 12482{ 12483 void (*fe_datamove)(union ctl_io *io); 12484 12485 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12486 12487 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12488 12489#ifdef CTL_TIME_IO 12490 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12491 char str[256]; 12492 char path_str[64]; 12493 struct sbuf sb; 12494 12495 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12496 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12497 12498 sbuf_cat(&sb, path_str); 12499 switch (io->io_hdr.io_type) { 12500 case CTL_IO_SCSI: 12501 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12502 sbuf_printf(&sb, "\n"); 12503 sbuf_cat(&sb, path_str); 12504 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12505 io->scsiio.tag_num, io->scsiio.tag_type); 12506 break; 12507 case CTL_IO_TASK: 12508 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12509 "Tag Type: %d\n", io->taskio.task_action, 12510 io->taskio.tag_num, io->taskio.tag_type); 12511 break; 12512 default: 12513 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12514 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12515 break; 12516 } 12517 sbuf_cat(&sb, path_str); 12518 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12519 (intmax_t)time_uptime - io->io_hdr.start_time); 12520 sbuf_finish(&sb); 12521 printf("%s", sbuf_data(&sb)); 12522 } 12523#endif /* CTL_TIME_IO */ 12524 12525#ifdef CTL_IO_DELAY 12526 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12527 struct ctl_lun *lun; 12528 12529 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12530 12531 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12532 } else { 12533 struct ctl_lun *lun; 12534 12535 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12536 if ((lun != NULL) 12537 && (lun->delay_info.datamove_delay > 0)) { 12538 struct callout *callout; 12539 12540 callout = (struct callout *)&io->io_hdr.timer_bytes; 12541 callout_init(callout, /*mpsafe*/ 1); 12542 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12543 callout_reset(callout, 12544 lun->delay_info.datamove_delay * hz, 12545 ctl_datamove_timer_wakeup, io); 12546 if (lun->delay_info.datamove_type == 12547 CTL_DELAY_TYPE_ONESHOT) 12548 lun->delay_info.datamove_delay = 0; 12549 return; 12550 } 12551 } 12552#endif 12553 12554 /* 12555 * This command has been aborted. Set the port status, so we fail 12556 * the data move. 12557 */ 12558 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12559 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12560 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12561 io->io_hdr.nexus.targ_port, 12562 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12563 io->io_hdr.nexus.targ_lun); 12564 io->io_hdr.port_status = 31337; 12565 /* 12566 * Note that the backend, in this case, will get the 12567 * callback in its context. In other cases it may get 12568 * called in the frontend's interrupt thread context. 12569 */ 12570 io->scsiio.be_move_done(io); 12571 return; 12572 } 12573 12574 /* Don't confuse frontend with zero length data move. */ 12575 if (io->scsiio.kern_data_len == 0) { 12576 io->scsiio.be_move_done(io); 12577 return; 12578 } 12579 12580 /* 12581 * If we're in XFER mode and this I/O is from the other shelf 12582 * controller, we need to send the DMA to the other side to 12583 * actually transfer the data to/from the host. In serialize only 12584 * mode the transfer happens below CTL and ctl_datamove() is only 12585 * called on the machine that originally received the I/O. 12586 */ 12587 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12588 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12589 union ctl_ha_msg msg; 12590 uint32_t sg_entries_sent; 12591 int do_sg_copy; 12592 int i; 12593 12594 memset(&msg, 0, sizeof(msg)); 12595 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12596 msg.hdr.original_sc = io->io_hdr.original_sc; 12597 msg.hdr.serializing_sc = io; 12598 msg.hdr.nexus = io->io_hdr.nexus; 12599 msg.dt.flags = io->io_hdr.flags; 12600 /* 12601 * We convert everything into a S/G list here. We can't 12602 * pass by reference, only by value between controllers. 12603 * So we can't pass a pointer to the S/G list, only as many 12604 * S/G entries as we can fit in here. If it's possible for 12605 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12606 * then we need to break this up into multiple transfers. 12607 */ 12608 if (io->scsiio.kern_sg_entries == 0) { 12609 msg.dt.kern_sg_entries = 1; 12610 /* 12611 * If this is in cached memory, flush the cache 12612 * before we send the DMA request to the other 12613 * controller. We want to do this in either the 12614 * read or the write case. The read case is 12615 * straightforward. In the write case, we want to 12616 * make sure nothing is in the local cache that 12617 * could overwrite the DMAed data. 12618 */ 12619 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12620 /* 12621 * XXX KDM use bus_dmamap_sync() here. 12622 */ 12623 } 12624 12625 /* 12626 * Convert to a physical address if this is a 12627 * virtual address. 12628 */ 12629 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12630 msg.dt.sg_list[0].addr = 12631 io->scsiio.kern_data_ptr; 12632 } else { 12633 /* 12634 * XXX KDM use busdma here! 12635 */ 12636#if 0 12637 msg.dt.sg_list[0].addr = (void *) 12638 vtophys(io->scsiio.kern_data_ptr); 12639#endif 12640 } 12641 12642 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12643 do_sg_copy = 0; 12644 } else { 12645 struct ctl_sg_entry *sgl; 12646 12647 do_sg_copy = 1; 12648 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12649 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12650 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12651 /* 12652 * XXX KDM use bus_dmamap_sync() here. 12653 */ 12654 } 12655 } 12656 12657 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12658 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12659 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12660 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12661 msg.dt.sg_sequence = 0; 12662 12663 /* 12664 * Loop until we've sent all of the S/G entries. On the 12665 * other end, we'll recompose these S/G entries into one 12666 * contiguous list before passing it to the 12667 */ 12668 for (sg_entries_sent = 0; sg_entries_sent < 12669 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12670 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12671 sizeof(msg.dt.sg_list[0])), 12672 msg.dt.kern_sg_entries - sg_entries_sent); 12673 12674 if (do_sg_copy != 0) { 12675 struct ctl_sg_entry *sgl; 12676 int j; 12677 12678 sgl = (struct ctl_sg_entry *) 12679 io->scsiio.kern_data_ptr; 12680 /* 12681 * If this is in cached memory, flush the cache 12682 * before we send the DMA request to the other 12683 * controller. We want to do this in either 12684 * the * read or the write case. The read 12685 * case is straightforward. In the write 12686 * case, we want to make sure nothing is 12687 * in the local cache that could overwrite 12688 * the DMAed data. 12689 */ 12690 12691 for (i = sg_entries_sent, j = 0; 12692 i < msg.dt.cur_sg_entries; i++, j++) { 12693 if ((io->io_hdr.flags & 12694 CTL_FLAG_NO_DATASYNC) == 0) { 12695 /* 12696 * XXX KDM use bus_dmamap_sync() 12697 */ 12698 } 12699 if ((io->io_hdr.flags & 12700 CTL_FLAG_BUS_ADDR) == 0) { 12701 /* 12702 * XXX KDM use busdma. 12703 */ 12704#if 0 12705 msg.dt.sg_list[j].addr =(void *) 12706 vtophys(sgl[i].addr); 12707#endif 12708 } else { 12709 msg.dt.sg_list[j].addr = 12710 sgl[i].addr; 12711 } 12712 msg.dt.sg_list[j].len = sgl[i].len; 12713 } 12714 } 12715 12716 sg_entries_sent += msg.dt.cur_sg_entries; 12717 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12718 msg.dt.sg_last = 1; 12719 else 12720 msg.dt.sg_last = 0; 12721 12722 /* 12723 * XXX KDM drop and reacquire the lock here? 12724 */ 12725 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12726 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12727 /* 12728 * XXX do something here. 12729 */ 12730 } 12731 12732 msg.dt.sent_sg_entries = sg_entries_sent; 12733 } 12734 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12735 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12736 ctl_failover_io(io, /*have_lock*/ 0); 12737 12738 } else { 12739 12740 /* 12741 * Lookup the fe_datamove() function for this particular 12742 * front end. 12743 */ 12744 fe_datamove = 12745 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12746 12747 fe_datamove(io); 12748 } 12749} 12750 12751static void 12752ctl_send_datamove_done(union ctl_io *io, int have_lock) 12753{ 12754 union ctl_ha_msg msg; 12755 int isc_status; 12756 12757 memset(&msg, 0, sizeof(msg)); 12758 12759 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12760 msg.hdr.original_sc = io; 12761 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12762 msg.hdr.nexus = io->io_hdr.nexus; 12763 msg.hdr.status = io->io_hdr.status; 12764 msg.scsi.tag_num = io->scsiio.tag_num; 12765 msg.scsi.tag_type = io->scsiio.tag_type; 12766 msg.scsi.scsi_status = io->scsiio.scsi_status; 12767 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12768 sizeof(io->scsiio.sense_data)); 12769 msg.scsi.sense_len = io->scsiio.sense_len; 12770 msg.scsi.sense_residual = io->scsiio.sense_residual; 12771 msg.scsi.fetd_status = io->io_hdr.port_status; 12772 msg.scsi.residual = io->scsiio.residual; 12773 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12774 12775 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12776 ctl_failover_io(io, /*have_lock*/ have_lock); 12777 return; 12778 } 12779 12780 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12781 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12782 /* XXX do something if this fails */ 12783 } 12784 12785} 12786 12787/* 12788 * The DMA to the remote side is done, now we need to tell the other side 12789 * we're done so it can continue with its data movement. 12790 */ 12791static void 12792ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12793{ 12794 union ctl_io *io; 12795 12796 io = rq->context; 12797 12798 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12799 printf("%s: ISC DMA write failed with error %d", __func__, 12800 rq->ret); 12801 ctl_set_internal_failure(&io->scsiio, 12802 /*sks_valid*/ 1, 12803 /*retry_count*/ rq->ret); 12804 } 12805 12806 ctl_dt_req_free(rq); 12807 12808 /* 12809 * In this case, we had to malloc the memory locally. Free it. 12810 */ 12811 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12812 int i; 12813 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12814 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12815 } 12816 /* 12817 * The data is in local and remote memory, so now we need to send 12818 * status (good or back) back to the other side. 12819 */ 12820 ctl_send_datamove_done(io, /*have_lock*/ 0); 12821} 12822 12823/* 12824 * We've moved the data from the host/controller into local memory. Now we 12825 * need to push it over to the remote controller's memory. 12826 */ 12827static int 12828ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12829{ 12830 int retval; 12831 12832 retval = 0; 12833 12834 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12835 ctl_datamove_remote_write_cb); 12836 12837 return (retval); 12838} 12839 12840static void 12841ctl_datamove_remote_write(union ctl_io *io) 12842{ 12843 int retval; 12844 void (*fe_datamove)(union ctl_io *io); 12845 12846 /* 12847 * - Get the data from the host/HBA into local memory. 12848 * - DMA memory from the local controller to the remote controller. 12849 * - Send status back to the remote controller. 12850 */ 12851 12852 retval = ctl_datamove_remote_sgl_setup(io); 12853 if (retval != 0) 12854 return; 12855 12856 /* Switch the pointer over so the FETD knows what to do */ 12857 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12858 12859 /* 12860 * Use a custom move done callback, since we need to send completion 12861 * back to the other controller, not to the backend on this side. 12862 */ 12863 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12864 12865 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12866 12867 fe_datamove(io); 12868 12869 return; 12870 12871} 12872 12873static int 12874ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12875{ 12876#if 0 12877 char str[256]; 12878 char path_str[64]; 12879 struct sbuf sb; 12880#endif 12881 12882 /* 12883 * In this case, we had to malloc the memory locally. Free it. 12884 */ 12885 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12886 int i; 12887 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12888 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12889 } 12890 12891#if 0 12892 scsi_path_string(io, path_str, sizeof(path_str)); 12893 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12894 sbuf_cat(&sb, path_str); 12895 scsi_command_string(&io->scsiio, NULL, &sb); 12896 sbuf_printf(&sb, "\n"); 12897 sbuf_cat(&sb, path_str); 12898 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12899 io->scsiio.tag_num, io->scsiio.tag_type); 12900 sbuf_cat(&sb, path_str); 12901 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12902 io->io_hdr.flags, io->io_hdr.status); 12903 sbuf_finish(&sb); 12904 printk("%s", sbuf_data(&sb)); 12905#endif 12906 12907 12908 /* 12909 * The read is done, now we need to send status (good or bad) back 12910 * to the other side. 12911 */ 12912 ctl_send_datamove_done(io, /*have_lock*/ 0); 12913 12914 return (0); 12915} 12916 12917static void 12918ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12919{ 12920 union ctl_io *io; 12921 void (*fe_datamove)(union ctl_io *io); 12922 12923 io = rq->context; 12924 12925 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12926 printf("%s: ISC DMA read failed with error %d", __func__, 12927 rq->ret); 12928 ctl_set_internal_failure(&io->scsiio, 12929 /*sks_valid*/ 1, 12930 /*retry_count*/ rq->ret); 12931 } 12932 12933 ctl_dt_req_free(rq); 12934 12935 /* Switch the pointer over so the FETD knows what to do */ 12936 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12937 12938 /* 12939 * Use a custom move done callback, since we need to send completion 12940 * back to the other controller, not to the backend on this side. 12941 */ 12942 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12943 12944 /* XXX KDM add checks like the ones in ctl_datamove? */ 12945 12946 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12947 12948 fe_datamove(io); 12949} 12950 12951static int 12952ctl_datamove_remote_sgl_setup(union ctl_io *io) 12953{ 12954 struct ctl_sg_entry *local_sglist, *remote_sglist; 12955 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12956 struct ctl_softc *softc; 12957 int retval; 12958 int i; 12959 12960 retval = 0; 12961 softc = control_softc; 12962 12963 local_sglist = io->io_hdr.local_sglist; 12964 local_dma_sglist = io->io_hdr.local_dma_sglist; 12965 remote_sglist = io->io_hdr.remote_sglist; 12966 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12967 12968 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12969 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12970 local_sglist[i].len = remote_sglist[i].len; 12971 12972 /* 12973 * XXX Detect the situation where the RS-level I/O 12974 * redirector on the other side has already read the 12975 * data off of the AOR RS on this side, and 12976 * transferred it to remote (mirror) memory on the 12977 * other side. Since we already have the data in 12978 * memory here, we just need to use it. 12979 * 12980 * XXX KDM this can probably be removed once we 12981 * get the cache device code in and take the 12982 * current AOR implementation out. 12983 */ 12984#ifdef NEEDTOPORT 12985 if ((remote_sglist[i].addr >= 12986 (void *)vtophys(softc->mirr->addr)) 12987 && (remote_sglist[i].addr < 12988 ((void *)vtophys(softc->mirr->addr) + 12989 CacheMirrorOffset))) { 12990 local_sglist[i].addr = remote_sglist[i].addr - 12991 CacheMirrorOffset; 12992 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12993 CTL_FLAG_DATA_IN) 12994 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12995 } else { 12996 local_sglist[i].addr = remote_sglist[i].addr + 12997 CacheMirrorOffset; 12998 } 12999#endif 13000#if 0 13001 printf("%s: local %p, remote %p, len %d\n", 13002 __func__, local_sglist[i].addr, 13003 remote_sglist[i].addr, local_sglist[i].len); 13004#endif 13005 } 13006 } else { 13007 uint32_t len_to_go; 13008 13009 /* 13010 * In this case, we don't have automatically allocated 13011 * memory for this I/O on this controller. This typically 13012 * happens with internal CTL I/O -- e.g. inquiry, mode 13013 * sense, etc. Anything coming from RAIDCore will have 13014 * a mirror area available. 13015 */ 13016 len_to_go = io->scsiio.kern_data_len; 13017 13018 /* 13019 * Clear the no datasync flag, we have to use malloced 13020 * buffers. 13021 */ 13022 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13023 13024 /* 13025 * The difficult thing here is that the size of the various 13026 * S/G segments may be different than the size from the 13027 * remote controller. That'll make it harder when DMAing 13028 * the data back to the other side. 13029 */ 13030 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13031 sizeof(io->io_hdr.remote_sglist[0])) && 13032 (len_to_go > 0); i++) { 13033 local_sglist[i].len = ctl_min(len_to_go, 131072); 13034 CTL_SIZE_8B(local_dma_sglist[i].len, 13035 local_sglist[i].len); 13036 local_sglist[i].addr = 13037 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13038 13039 local_dma_sglist[i].addr = local_sglist[i].addr; 13040 13041 if (local_sglist[i].addr == NULL) { 13042 int j; 13043 13044 printf("malloc failed for %zd bytes!", 13045 local_dma_sglist[i].len); 13046 for (j = 0; j < i; j++) { 13047 free(local_sglist[j].addr, M_CTL); 13048 } 13049 ctl_set_internal_failure(&io->scsiio, 13050 /*sks_valid*/ 1, 13051 /*retry_count*/ 4857); 13052 retval = 1; 13053 goto bailout_error; 13054 13055 } 13056 /* XXX KDM do we need a sync here? */ 13057 13058 len_to_go -= local_sglist[i].len; 13059 } 13060 /* 13061 * Reset the number of S/G entries accordingly. The 13062 * original number of S/G entries is available in 13063 * rem_sg_entries. 13064 */ 13065 io->scsiio.kern_sg_entries = i; 13066 13067#if 0 13068 printf("%s: kern_sg_entries = %d\n", __func__, 13069 io->scsiio.kern_sg_entries); 13070 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13071 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13072 local_sglist[i].addr, local_sglist[i].len, 13073 local_dma_sglist[i].len); 13074#endif 13075 } 13076 13077 13078 return (retval); 13079 13080bailout_error: 13081 13082 ctl_send_datamove_done(io, /*have_lock*/ 0); 13083 13084 return (retval); 13085} 13086 13087static int 13088ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13089 ctl_ha_dt_cb callback) 13090{ 13091 struct ctl_ha_dt_req *rq; 13092 struct ctl_sg_entry *remote_sglist, *local_sglist; 13093 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13094 uint32_t local_used, remote_used, total_used; 13095 int retval; 13096 int i, j; 13097 13098 retval = 0; 13099 13100 rq = ctl_dt_req_alloc(); 13101 13102 /* 13103 * If we failed to allocate the request, and if the DMA didn't fail 13104 * anyway, set busy status. This is just a resource allocation 13105 * failure. 13106 */ 13107 if ((rq == NULL) 13108 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13109 ctl_set_busy(&io->scsiio); 13110 13111 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13112 13113 if (rq != NULL) 13114 ctl_dt_req_free(rq); 13115 13116 /* 13117 * The data move failed. We need to return status back 13118 * to the other controller. No point in trying to DMA 13119 * data to the remote controller. 13120 */ 13121 13122 ctl_send_datamove_done(io, /*have_lock*/ 0); 13123 13124 retval = 1; 13125 13126 goto bailout; 13127 } 13128 13129 local_sglist = io->io_hdr.local_sglist; 13130 local_dma_sglist = io->io_hdr.local_dma_sglist; 13131 remote_sglist = io->io_hdr.remote_sglist; 13132 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13133 local_used = 0; 13134 remote_used = 0; 13135 total_used = 0; 13136 13137 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13138 rq->ret = CTL_HA_STATUS_SUCCESS; 13139 rq->context = io; 13140 callback(rq); 13141 goto bailout; 13142 } 13143 13144 /* 13145 * Pull/push the data over the wire from/to the other controller. 13146 * This takes into account the possibility that the local and 13147 * remote sglists may not be identical in terms of the size of 13148 * the elements and the number of elements. 13149 * 13150 * One fundamental assumption here is that the length allocated for 13151 * both the local and remote sglists is identical. Otherwise, we've 13152 * essentially got a coding error of some sort. 13153 */ 13154 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13155 int isc_ret; 13156 uint32_t cur_len, dma_length; 13157 uint8_t *tmp_ptr; 13158 13159 rq->id = CTL_HA_DATA_CTL; 13160 rq->command = command; 13161 rq->context = io; 13162 13163 /* 13164 * Both pointers should be aligned. But it is possible 13165 * that the allocation length is not. They should both 13166 * also have enough slack left over at the end, though, 13167 * to round up to the next 8 byte boundary. 13168 */ 13169 cur_len = ctl_min(local_sglist[i].len - local_used, 13170 remote_sglist[j].len - remote_used); 13171 13172 /* 13173 * In this case, we have a size issue and need to decrease 13174 * the size, except in the case where we actually have less 13175 * than 8 bytes left. In that case, we need to increase 13176 * the DMA length to get the last bit. 13177 */ 13178 if ((cur_len & 0x7) != 0) { 13179 if (cur_len > 0x7) { 13180 cur_len = cur_len - (cur_len & 0x7); 13181 dma_length = cur_len; 13182 } else { 13183 CTL_SIZE_8B(dma_length, cur_len); 13184 } 13185 13186 } else 13187 dma_length = cur_len; 13188 13189 /* 13190 * If we had to allocate memory for this I/O, instead of using 13191 * the non-cached mirror memory, we'll need to flush the cache 13192 * before trying to DMA to the other controller. 13193 * 13194 * We could end up doing this multiple times for the same 13195 * segment if we have a larger local segment than remote 13196 * segment. That shouldn't be an issue. 13197 */ 13198 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13199 /* 13200 * XXX KDM use bus_dmamap_sync() here. 13201 */ 13202 } 13203 13204 rq->size = dma_length; 13205 13206 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13207 tmp_ptr += local_used; 13208 13209 /* Use physical addresses when talking to ISC hardware */ 13210 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13211 /* XXX KDM use busdma */ 13212#if 0 13213 rq->local = vtophys(tmp_ptr); 13214#endif 13215 } else 13216 rq->local = tmp_ptr; 13217 13218 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13219 tmp_ptr += remote_used; 13220 rq->remote = tmp_ptr; 13221 13222 rq->callback = NULL; 13223 13224 local_used += cur_len; 13225 if (local_used >= local_sglist[i].len) { 13226 i++; 13227 local_used = 0; 13228 } 13229 13230 remote_used += cur_len; 13231 if (remote_used >= remote_sglist[j].len) { 13232 j++; 13233 remote_used = 0; 13234 } 13235 total_used += cur_len; 13236 13237 if (total_used >= io->scsiio.kern_data_len) 13238 rq->callback = callback; 13239 13240 if ((rq->size & 0x7) != 0) { 13241 printf("%s: warning: size %d is not on 8b boundary\n", 13242 __func__, rq->size); 13243 } 13244 if (((uintptr_t)rq->local & 0x7) != 0) { 13245 printf("%s: warning: local %p not on 8b boundary\n", 13246 __func__, rq->local); 13247 } 13248 if (((uintptr_t)rq->remote & 0x7) != 0) { 13249 printf("%s: warning: remote %p not on 8b boundary\n", 13250 __func__, rq->local); 13251 } 13252#if 0 13253 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13254 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13255 rq->local, rq->remote, rq->size); 13256#endif 13257 13258 isc_ret = ctl_dt_single(rq); 13259 if (isc_ret == CTL_HA_STATUS_WAIT) 13260 continue; 13261 13262 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13263 rq->ret = CTL_HA_STATUS_SUCCESS; 13264 } else { 13265 rq->ret = isc_ret; 13266 } 13267 callback(rq); 13268 goto bailout; 13269 } 13270 13271bailout: 13272 return (retval); 13273 13274} 13275 13276static void 13277ctl_datamove_remote_read(union ctl_io *io) 13278{ 13279 int retval; 13280 int i; 13281 13282 /* 13283 * This will send an error to the other controller in the case of a 13284 * failure. 13285 */ 13286 retval = ctl_datamove_remote_sgl_setup(io); 13287 if (retval != 0) 13288 return; 13289 13290 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13291 ctl_datamove_remote_read_cb); 13292 if ((retval != 0) 13293 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13294 /* 13295 * Make sure we free memory if there was an error.. The 13296 * ctl_datamove_remote_xfer() function will send the 13297 * datamove done message, or call the callback with an 13298 * error if there is a problem. 13299 */ 13300 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13301 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13302 } 13303 13304 return; 13305} 13306 13307/* 13308 * Process a datamove request from the other controller. This is used for 13309 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13310 * first. Once that is complete, the data gets DMAed into the remote 13311 * controller's memory. For reads, we DMA from the remote controller's 13312 * memory into our memory first, and then move it out to the FETD. 13313 */ 13314static void 13315ctl_datamove_remote(union ctl_io *io) 13316{ 13317 struct ctl_softc *softc; 13318 13319 softc = control_softc; 13320 13321 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13322 13323 /* 13324 * Note that we look for an aborted I/O here, but don't do some of 13325 * the other checks that ctl_datamove() normally does. 13326 * We don't need to run the datamove delay code, since that should 13327 * have been done if need be on the other controller. 13328 */ 13329 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13330 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13331 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13332 io->io_hdr.nexus.targ_port, 13333 io->io_hdr.nexus.targ_target.id, 13334 io->io_hdr.nexus.targ_lun); 13335 io->io_hdr.port_status = 31338; 13336 ctl_send_datamove_done(io, /*have_lock*/ 0); 13337 return; 13338 } 13339 13340 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13341 ctl_datamove_remote_write(io); 13342 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13343 ctl_datamove_remote_read(io); 13344 } else { 13345 union ctl_ha_msg msg; 13346 struct scsi_sense_data *sense; 13347 uint8_t sks[3]; 13348 int retry_count; 13349 13350 memset(&msg, 0, sizeof(msg)); 13351 13352 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13353 msg.hdr.status = CTL_SCSI_ERROR; 13354 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13355 13356 retry_count = 4243; 13357 13358 sense = &msg.scsi.sense_data; 13359 sks[0] = SSD_SCS_VALID; 13360 sks[1] = (retry_count >> 8) & 0xff; 13361 sks[2] = retry_count & 0xff; 13362 13363 /* "Internal target failure" */ 13364 scsi_set_sense_data(sense, 13365 /*sense_format*/ SSD_TYPE_NONE, 13366 /*current_error*/ 1, 13367 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13368 /*asc*/ 0x44, 13369 /*ascq*/ 0x00, 13370 /*type*/ SSD_ELEM_SKS, 13371 /*size*/ sizeof(sks), 13372 /*data*/ sks, 13373 SSD_ELEM_NONE); 13374 13375 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13376 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13377 ctl_failover_io(io, /*have_lock*/ 1); 13378 return; 13379 } 13380 13381 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13382 CTL_HA_STATUS_SUCCESS) { 13383 /* XXX KDM what to do if this fails? */ 13384 } 13385 return; 13386 } 13387 13388} 13389 13390static int 13391ctl_process_done(union ctl_io *io) 13392{ 13393 struct ctl_lun *lun; 13394 struct ctl_softc *ctl_softc = control_softc; 13395 void (*fe_done)(union ctl_io *io); 13396 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13397 13398 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13399 13400 fe_done = 13401 control_softc->ctl_ports[targ_port]->fe_done; 13402 13403#ifdef CTL_TIME_IO 13404 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13405 char str[256]; 13406 char path_str[64]; 13407 struct sbuf sb; 13408 13409 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13410 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13411 13412 sbuf_cat(&sb, path_str); 13413 switch (io->io_hdr.io_type) { 13414 case CTL_IO_SCSI: 13415 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13416 sbuf_printf(&sb, "\n"); 13417 sbuf_cat(&sb, path_str); 13418 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13419 io->scsiio.tag_num, io->scsiio.tag_type); 13420 break; 13421 case CTL_IO_TASK: 13422 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13423 "Tag Type: %d\n", io->taskio.task_action, 13424 io->taskio.tag_num, io->taskio.tag_type); 13425 break; 13426 default: 13427 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13428 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13429 break; 13430 } 13431 sbuf_cat(&sb, path_str); 13432 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13433 (intmax_t)time_uptime - io->io_hdr.start_time); 13434 sbuf_finish(&sb); 13435 printf("%s", sbuf_data(&sb)); 13436 } 13437#endif /* CTL_TIME_IO */ 13438 13439 switch (io->io_hdr.io_type) { 13440 case CTL_IO_SCSI: 13441 break; 13442 case CTL_IO_TASK: 13443 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13444 ctl_io_error_print(io, NULL); 13445 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13446 ctl_free_io(io); 13447 else 13448 fe_done(io); 13449 return (CTL_RETVAL_COMPLETE); 13450 default: 13451 panic("ctl_process_done: invalid io type %d\n", 13452 io->io_hdr.io_type); 13453 break; /* NOTREACHED */ 13454 } 13455 13456 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13457 if (lun == NULL) { 13458 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13459 io->io_hdr.nexus.targ_mapped_lun)); 13460 goto bailout; 13461 } 13462 13463 mtx_lock(&lun->lun_lock); 13464 13465 /* 13466 * Check to see if we have any errors to inject here. We only 13467 * inject errors for commands that don't already have errors set. 13468 */ 13469 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13470 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13471 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13472 ctl_inject_error(lun, io); 13473 13474 /* 13475 * XXX KDM how do we treat commands that aren't completed 13476 * successfully? 13477 * 13478 * XXX KDM should we also track I/O latency? 13479 */ 13480 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13481 io->io_hdr.io_type == CTL_IO_SCSI) { 13482#ifdef CTL_TIME_IO 13483 struct bintime cur_bt; 13484#endif 13485 int type; 13486 13487 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13488 CTL_FLAG_DATA_IN) 13489 type = CTL_STATS_READ; 13490 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13491 CTL_FLAG_DATA_OUT) 13492 type = CTL_STATS_WRITE; 13493 else 13494 type = CTL_STATS_NO_IO; 13495 13496 lun->stats.ports[targ_port].bytes[type] += 13497 io->scsiio.kern_total_len; 13498 lun->stats.ports[targ_port].operations[type]++; 13499#ifdef CTL_TIME_IO 13500 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13501 &io->io_hdr.dma_bt); 13502 lun->stats.ports[targ_port].num_dmas[type] += 13503 io->io_hdr.num_dmas; 13504 getbintime(&cur_bt); 13505 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13506 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13507#endif 13508 } 13509 13510 /* 13511 * Remove this from the OOA queue. 13512 */ 13513 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13514 13515 /* 13516 * Run through the blocked queue on this LUN and see if anything 13517 * has become unblocked, now that this transaction is done. 13518 */ 13519 ctl_check_blocked(lun); 13520 13521 /* 13522 * If the LUN has been invalidated, free it if there is nothing 13523 * left on its OOA queue. 13524 */ 13525 if ((lun->flags & CTL_LUN_INVALID) 13526 && TAILQ_EMPTY(&lun->ooa_queue)) { 13527 mtx_unlock(&lun->lun_lock); 13528 mtx_lock(&ctl_softc->ctl_lock); 13529 ctl_free_lun(lun); 13530 mtx_unlock(&ctl_softc->ctl_lock); 13531 } else 13532 mtx_unlock(&lun->lun_lock); 13533 13534bailout: 13535 13536 /* 13537 * If this command has been aborted, make sure we set the status 13538 * properly. The FETD is responsible for freeing the I/O and doing 13539 * whatever it needs to do to clean up its state. 13540 */ 13541 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13542 ctl_set_task_aborted(&io->scsiio); 13543 13544 /* 13545 * If enabled, print command error status. 13546 * We don't print UAs unless debugging was enabled explicitly. 13547 */ 13548 do { 13549 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13550 break; 13551 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13552 break; 13553 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13554 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13555 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13556 int error_code, sense_key, asc, ascq; 13557 13558 scsi_extract_sense_len(&io->scsiio.sense_data, 13559 io->scsiio.sense_len, &error_code, &sense_key, 13560 &asc, &ascq, /*show_errors*/ 0); 13561 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13562 break; 13563 } 13564 13565 ctl_io_error_print(io, NULL); 13566 } while (0); 13567 13568 /* 13569 * Tell the FETD or the other shelf controller we're done with this 13570 * command. Note that only SCSI commands get to this point. Task 13571 * management commands are completed above. 13572 * 13573 * We only send status to the other controller if we're in XFER 13574 * mode. In SER_ONLY mode, the I/O is done on the controller that 13575 * received the I/O (from CTL's perspective), and so the status is 13576 * generated there. 13577 * 13578 * XXX KDM if we hold the lock here, we could cause a deadlock 13579 * if the frontend comes back in in this context to queue 13580 * something. 13581 */ 13582 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13583 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13584 union ctl_ha_msg msg; 13585 13586 memset(&msg, 0, sizeof(msg)); 13587 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13588 msg.hdr.original_sc = io->io_hdr.original_sc; 13589 msg.hdr.nexus = io->io_hdr.nexus; 13590 msg.hdr.status = io->io_hdr.status; 13591 msg.scsi.scsi_status = io->scsiio.scsi_status; 13592 msg.scsi.tag_num = io->scsiio.tag_num; 13593 msg.scsi.tag_type = io->scsiio.tag_type; 13594 msg.scsi.sense_len = io->scsiio.sense_len; 13595 msg.scsi.sense_residual = io->scsiio.sense_residual; 13596 msg.scsi.residual = io->scsiio.residual; 13597 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13598 sizeof(io->scsiio.sense_data)); 13599 /* 13600 * We copy this whether or not this is an I/O-related 13601 * command. Otherwise, we'd have to go and check to see 13602 * whether it's a read/write command, and it really isn't 13603 * worth it. 13604 */ 13605 memcpy(&msg.scsi.lbalen, 13606 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13607 sizeof(msg.scsi.lbalen)); 13608 13609 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13610 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13611 /* XXX do something here */ 13612 } 13613 13614 ctl_free_io(io); 13615 } else 13616 fe_done(io); 13617 13618 return (CTL_RETVAL_COMPLETE); 13619} 13620 13621#ifdef CTL_WITH_CA 13622/* 13623 * Front end should call this if it doesn't do autosense. When the request 13624 * sense comes back in from the initiator, we'll dequeue this and send it. 13625 */ 13626int 13627ctl_queue_sense(union ctl_io *io) 13628{ 13629 struct ctl_lun *lun; 13630 struct ctl_softc *ctl_softc; 13631 uint32_t initidx, targ_lun; 13632 13633 ctl_softc = control_softc; 13634 13635 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13636 13637 /* 13638 * LUN lookup will likely move to the ctl_work_thread() once we 13639 * have our new queueing infrastructure (that doesn't put things on 13640 * a per-LUN queue initially). That is so that we can handle 13641 * things like an INQUIRY to a LUN that we don't have enabled. We 13642 * can't deal with that right now. 13643 */ 13644 mtx_lock(&ctl_softc->ctl_lock); 13645 13646 /* 13647 * If we don't have a LUN for this, just toss the sense 13648 * information. 13649 */ 13650 targ_lun = io->io_hdr.nexus.targ_lun; 13651 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13652 if ((targ_lun < CTL_MAX_LUNS) 13653 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13654 lun = ctl_softc->ctl_luns[targ_lun]; 13655 else 13656 goto bailout; 13657 13658 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13659 13660 mtx_lock(&lun->lun_lock); 13661 /* 13662 * Already have CA set for this LUN...toss the sense information. 13663 */ 13664 if (ctl_is_set(lun->have_ca, initidx)) { 13665 mtx_unlock(&lun->lun_lock); 13666 goto bailout; 13667 } 13668 13669 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13670 ctl_min(sizeof(lun->pending_sense[initidx]), 13671 sizeof(io->scsiio.sense_data))); 13672 ctl_set_mask(lun->have_ca, initidx); 13673 mtx_unlock(&lun->lun_lock); 13674 13675bailout: 13676 mtx_unlock(&ctl_softc->ctl_lock); 13677 13678 ctl_free_io(io); 13679 13680 return (CTL_RETVAL_COMPLETE); 13681} 13682#endif 13683 13684/* 13685 * Primary command inlet from frontend ports. All SCSI and task I/O 13686 * requests must go through this function. 13687 */ 13688int 13689ctl_queue(union ctl_io *io) 13690{ 13691 struct ctl_softc *ctl_softc; 13692 13693 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13694 13695 ctl_softc = control_softc; 13696 13697#ifdef CTL_TIME_IO 13698 io->io_hdr.start_time = time_uptime; 13699 getbintime(&io->io_hdr.start_bt); 13700#endif /* CTL_TIME_IO */ 13701 13702 /* Map FE-specific LUN ID into global one. */ 13703 io->io_hdr.nexus.targ_mapped_lun = 13704 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13705 13706 switch (io->io_hdr.io_type) { 13707 case CTL_IO_SCSI: 13708 case CTL_IO_TASK: 13709 if (ctl_debug & CTL_DEBUG_CDB) 13710 ctl_io_print(io); 13711 ctl_enqueue_incoming(io); 13712 break; 13713 default: 13714 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13715 return (EINVAL); 13716 } 13717 13718 return (CTL_RETVAL_COMPLETE); 13719} 13720 13721#ifdef CTL_IO_DELAY 13722static void 13723ctl_done_timer_wakeup(void *arg) 13724{ 13725 union ctl_io *io; 13726 13727 io = (union ctl_io *)arg; 13728 ctl_done(io); 13729} 13730#endif /* CTL_IO_DELAY */ 13731 13732void 13733ctl_done(union ctl_io *io) 13734{ 13735 struct ctl_softc *ctl_softc; 13736 13737 ctl_softc = control_softc; 13738 13739 /* 13740 * Enable this to catch duplicate completion issues. 13741 */ 13742#if 0 13743 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13744 printf("%s: type %d msg %d cdb %x iptl: " 13745 "%d:%d:%d:%d tag 0x%04x " 13746 "flag %#x status %x\n", 13747 __func__, 13748 io->io_hdr.io_type, 13749 io->io_hdr.msg_type, 13750 io->scsiio.cdb[0], 13751 io->io_hdr.nexus.initid.id, 13752 io->io_hdr.nexus.targ_port, 13753 io->io_hdr.nexus.targ_target.id, 13754 io->io_hdr.nexus.targ_lun, 13755 (io->io_hdr.io_type == 13756 CTL_IO_TASK) ? 13757 io->taskio.tag_num : 13758 io->scsiio.tag_num, 13759 io->io_hdr.flags, 13760 io->io_hdr.status); 13761 } else 13762 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13763#endif 13764 13765 /* 13766 * This is an internal copy of an I/O, and should not go through 13767 * the normal done processing logic. 13768 */ 13769 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13770 return; 13771 13772 /* 13773 * We need to send a msg to the serializing shelf to finish the IO 13774 * as well. We don't send a finish message to the other shelf if 13775 * this is a task management command. Task management commands 13776 * aren't serialized in the OOA queue, but rather just executed on 13777 * both shelf controllers for commands that originated on that 13778 * controller. 13779 */ 13780 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13781 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13782 union ctl_ha_msg msg_io; 13783 13784 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13785 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13786 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13787 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13788 } 13789 /* continue on to finish IO */ 13790 } 13791#ifdef CTL_IO_DELAY 13792 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13793 struct ctl_lun *lun; 13794 13795 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13796 13797 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13798 } else { 13799 struct ctl_lun *lun; 13800 13801 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13802 13803 if ((lun != NULL) 13804 && (lun->delay_info.done_delay > 0)) { 13805 struct callout *callout; 13806 13807 callout = (struct callout *)&io->io_hdr.timer_bytes; 13808 callout_init(callout, /*mpsafe*/ 1); 13809 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13810 callout_reset(callout, 13811 lun->delay_info.done_delay * hz, 13812 ctl_done_timer_wakeup, io); 13813 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13814 lun->delay_info.done_delay = 0; 13815 return; 13816 } 13817 } 13818#endif /* CTL_IO_DELAY */ 13819 13820 ctl_enqueue_done(io); 13821} 13822 13823int 13824ctl_isc(struct ctl_scsiio *ctsio) 13825{ 13826 struct ctl_lun *lun; 13827 int retval; 13828 13829 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13830 13831 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13832 13833 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13834 13835 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13836 13837 return (retval); 13838} 13839 13840 13841static void 13842ctl_work_thread(void *arg) 13843{ 13844 struct ctl_thread *thr = (struct ctl_thread *)arg; 13845 struct ctl_softc *softc = thr->ctl_softc; 13846 union ctl_io *io; 13847 int retval; 13848 13849 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13850 13851 for (;;) { 13852 retval = 0; 13853 13854 /* 13855 * We handle the queues in this order: 13856 * - ISC 13857 * - done queue (to free up resources, unblock other commands) 13858 * - RtR queue 13859 * - incoming queue 13860 * 13861 * If those queues are empty, we break out of the loop and 13862 * go to sleep. 13863 */ 13864 mtx_lock(&thr->queue_lock); 13865 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13866 if (io != NULL) { 13867 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13868 mtx_unlock(&thr->queue_lock); 13869 ctl_handle_isc(io); 13870 continue; 13871 } 13872 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13873 if (io != NULL) { 13874 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13875 /* clear any blocked commands, call fe_done */ 13876 mtx_unlock(&thr->queue_lock); 13877 retval = ctl_process_done(io); 13878 continue; 13879 } 13880 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13881 if (io != NULL) { 13882 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13883 mtx_unlock(&thr->queue_lock); 13884 if (io->io_hdr.io_type == CTL_IO_TASK) 13885 ctl_run_task(io); 13886 else 13887 ctl_scsiio_precheck(softc, &io->scsiio); 13888 continue; 13889 } 13890 if (!ctl_pause_rtr) { 13891 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13892 if (io != NULL) { 13893 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13894 mtx_unlock(&thr->queue_lock); 13895 retval = ctl_scsiio(&io->scsiio); 13896 if (retval != CTL_RETVAL_COMPLETE) 13897 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13898 continue; 13899 } 13900 } 13901 13902 /* Sleep until we have something to do. */ 13903 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13904 } 13905} 13906 13907static void 13908ctl_lun_thread(void *arg) 13909{ 13910 struct ctl_softc *softc = (struct ctl_softc *)arg; 13911 struct ctl_be_lun *be_lun; 13912 int retval; 13913 13914 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13915 13916 for (;;) { 13917 retval = 0; 13918 mtx_lock(&softc->ctl_lock); 13919 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13920 if (be_lun != NULL) { 13921 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13922 mtx_unlock(&softc->ctl_lock); 13923 ctl_create_lun(be_lun); 13924 continue; 13925 } 13926 13927 /* Sleep until we have something to do. */ 13928 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13929 PDROP | PRIBIO, "-", 0); 13930 } 13931} 13932 13933static void 13934ctl_thresh_thread(void *arg) 13935{ 13936 struct ctl_softc *softc = (struct ctl_softc *)arg; 13937 struct ctl_lun *lun; 13938 struct ctl_be_lun *be_lun; 13939 struct scsi_da_rw_recovery_page *rwpage; 13940 struct ctl_logical_block_provisioning_page *page; 13941 const char *attr; 13942 uint64_t thres, val; 13943 int i, e; 13944 13945 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13946 13947 for (;;) { 13948 mtx_lock(&softc->ctl_lock); 13949 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13950 be_lun = lun->be_lun; 13951 if ((lun->flags & CTL_LUN_DISABLED) || 13952 (lun->flags & CTL_LUN_OFFLINE) || 13953 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 13954 lun->backend->lun_attr == NULL) 13955 continue; 13956 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13957 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13958 continue; 13959 e = 0; 13960 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13961 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13962 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13963 continue; 13964 thres = scsi_4btoul(page->descr[i].count); 13965 thres <<= CTL_LBP_EXPONENT; 13966 switch (page->descr[i].resource) { 13967 case 0x01: 13968 attr = "blocksavail"; 13969 break; 13970 case 0x02: 13971 attr = "blocksused"; 13972 break; 13973 case 0xf1: 13974 attr = "poolblocksavail"; 13975 break; 13976 case 0xf2: 13977 attr = "poolblocksused"; 13978 break; 13979 default: 13980 continue; 13981 } 13982 mtx_unlock(&softc->ctl_lock); // XXX 13983 val = lun->backend->lun_attr( 13984 lun->be_lun->be_lun, attr); 13985 mtx_lock(&softc->ctl_lock); 13986 if (val == UINT64_MAX) 13987 continue; 13988 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13989 == SLBPPD_ARMING_INC) 13990 e |= (val >= thres); 13991 else 13992 e |= (val <= thres); 13993 } 13994 mtx_lock(&lun->lun_lock); 13995 if (e) { 13996 if (lun->lasttpt == 0 || 13997 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13998 lun->lasttpt = time_uptime; 13999 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14000 lun->pending_ua[i] |= 14001 CTL_UA_THIN_PROV_THRES; 14002 } 14003 } else { 14004 lun->lasttpt = 0; 14005 for (i = 0; i < CTL_MAX_INITIATORS; i++) 14006 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES; 14007 } 14008 mtx_unlock(&lun->lun_lock); 14009 } 14010 mtx_unlock(&softc->ctl_lock); 14011 pause("-", CTL_LBP_PERIOD * hz); 14012 } 14013} 14014 14015static void 14016ctl_enqueue_incoming(union ctl_io *io) 14017{ 14018 struct ctl_softc *softc = control_softc; 14019 struct ctl_thread *thr; 14020 u_int idx; 14021 14022 idx = (io->io_hdr.nexus.targ_port * 127 + 14023 io->io_hdr.nexus.initid.id) % worker_threads; 14024 thr = &softc->threads[idx]; 14025 mtx_lock(&thr->queue_lock); 14026 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14027 mtx_unlock(&thr->queue_lock); 14028 wakeup(thr); 14029} 14030 14031static void 14032ctl_enqueue_rtr(union ctl_io *io) 14033{ 14034 struct ctl_softc *softc = control_softc; 14035 struct ctl_thread *thr; 14036 14037 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14038 mtx_lock(&thr->queue_lock); 14039 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14040 mtx_unlock(&thr->queue_lock); 14041 wakeup(thr); 14042} 14043 14044static void 14045ctl_enqueue_done(union ctl_io *io) 14046{ 14047 struct ctl_softc *softc = control_softc; 14048 struct ctl_thread *thr; 14049 14050 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14051 mtx_lock(&thr->queue_lock); 14052 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14053 mtx_unlock(&thr->queue_lock); 14054 wakeup(thr); 14055} 14056 14057static void 14058ctl_enqueue_isc(union ctl_io *io) 14059{ 14060 struct ctl_softc *softc = control_softc; 14061 struct ctl_thread *thr; 14062 14063 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14064 mtx_lock(&thr->queue_lock); 14065 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14066 mtx_unlock(&thr->queue_lock); 14067 wakeup(thr); 14068} 14069 14070/* Initialization and failover */ 14071 14072void 14073ctl_init_isc_msg(void) 14074{ 14075 printf("CTL: Still calling this thing\n"); 14076} 14077 14078/* 14079 * Init component 14080 * Initializes component into configuration defined by bootMode 14081 * (see hasc-sv.c) 14082 * returns hasc_Status: 14083 * OK 14084 * ERROR - fatal error 14085 */ 14086static ctl_ha_comp_status 14087ctl_isc_init(struct ctl_ha_component *c) 14088{ 14089 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14090 14091 c->status = ret; 14092 return ret; 14093} 14094 14095/* Start component 14096 * Starts component in state requested. If component starts successfully, 14097 * it must set its own state to the requestrd state 14098 * When requested state is HASC_STATE_HA, the component may refine it 14099 * by adding _SLAVE or _MASTER flags. 14100 * Currently allowed state transitions are: 14101 * UNKNOWN->HA - initial startup 14102 * UNKNOWN->SINGLE - initial startup when no parter detected 14103 * HA->SINGLE - failover 14104 * returns ctl_ha_comp_status: 14105 * OK - component successfully started in requested state 14106 * FAILED - could not start the requested state, failover may 14107 * be possible 14108 * ERROR - fatal error detected, no future startup possible 14109 */ 14110static ctl_ha_comp_status 14111ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14112{ 14113 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14114 14115 printf("%s: go\n", __func__); 14116 14117 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14118 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14119 control_softc->is_single = 0; 14120 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14121 != CTL_HA_STATUS_SUCCESS) { 14122 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14123 ret = CTL_HA_COMP_STATUS_ERROR; 14124 } 14125 } else if (CTL_HA_STATE_IS_HA(c->state) 14126 && CTL_HA_STATE_IS_SINGLE(state)){ 14127 // HA->SINGLE transition 14128 ctl_failover(); 14129 control_softc->is_single = 1; 14130 } else { 14131 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14132 c->state, state); 14133 ret = CTL_HA_COMP_STATUS_ERROR; 14134 } 14135 if (CTL_HA_STATE_IS_SINGLE(state)) 14136 control_softc->is_single = 1; 14137 14138 c->state = state; 14139 c->status = ret; 14140 return ret; 14141} 14142 14143/* 14144 * Quiesce component 14145 * The component must clear any error conditions (set status to OK) and 14146 * prepare itself to another Start call 14147 * returns ctl_ha_comp_status: 14148 * OK 14149 * ERROR 14150 */ 14151static ctl_ha_comp_status 14152ctl_isc_quiesce(struct ctl_ha_component *c) 14153{ 14154 int ret = CTL_HA_COMP_STATUS_OK; 14155 14156 ctl_pause_rtr = 1; 14157 c->status = ret; 14158 return ret; 14159} 14160 14161struct ctl_ha_component ctl_ha_component_ctlisc = 14162{ 14163 .name = "CTL ISC", 14164 .state = CTL_HA_STATE_UNKNOWN, 14165 .init = ctl_isc_init, 14166 .start = ctl_isc_start, 14167 .quiesce = ctl_isc_quiesce 14168}; 14169 14170/* 14171 * vim: ts=8 14172 */ 14173