ctl.c revision 286931
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$ 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 286931 2015-08-19 17:47:47Z 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 */ 114const static 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 124const static 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 133const static 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 147const static 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 161const static 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 178const static 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 194const static 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 211const static 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 227const static 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 243const static 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 259const static 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 271const static 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 283const static 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 292const static 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 303const static 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 329const static 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, int lun_id); 389static int ctl_ioctl_lun_disable(void *arg, 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 int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 404 struct ctl_be_lun *be_lun); 405static int ctl_free_lun(struct ctl_lun *lun); 406static void ctl_create_lun(struct ctl_be_lun *be_lun); 407static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 408/** 409static void ctl_failover_change_pages(struct ctl_softc *softc, 410 struct ctl_scsiio *ctsio, int master); 411**/ 412 413static int ctl_do_mode_select(union ctl_io *io); 414static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 415 uint64_t res_key, uint64_t sa_res_key, 416 uint8_t type, uint32_t residx, 417 struct ctl_scsiio *ctsio, 418 struct scsi_per_res_out *cdb, 419 struct scsi_per_res_out_parms* param); 420static void ctl_pro_preempt_other(struct ctl_lun *lun, 421 union ctl_ha_msg *msg); 422static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 423static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 424static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 425static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 426static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 427static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 428static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 429 int alloc_len); 430static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 431 int alloc_len); 432static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 433static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 434static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 435static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 436static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 437static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 438 bool seq); 439static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 440static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 441 union ctl_io *pending_io, union ctl_io *ooa_io); 442static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 443 union ctl_io *starting_io); 444static int ctl_check_blocked(struct ctl_lun *lun); 445static int ctl_scsiio_lun_check(struct ctl_lun *lun, 446 const struct ctl_cmd_entry *entry, 447 struct ctl_scsiio *ctsio); 448//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 449static void ctl_failover(void); 450static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 451 ctl_ua_type ua_type); 452static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 453 struct ctl_scsiio *ctsio); 454static int ctl_scsiio(struct ctl_scsiio *ctsio); 455 456static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 457static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 458 ctl_ua_type ua_type); 459static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 460 ctl_ua_type ua_type); 461static int ctl_abort_task(union ctl_io *io); 462static int ctl_abort_task_set(union ctl_io *io); 463static int ctl_i_t_nexus_reset(union ctl_io *io); 464static void ctl_run_task(union ctl_io *io); 465#ifdef CTL_IO_DELAY 466static void ctl_datamove_timer_wakeup(void *arg); 467static void ctl_done_timer_wakeup(void *arg); 468#endif /* CTL_IO_DELAY */ 469 470static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 471static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 472static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 473static void ctl_datamove_remote_write(union ctl_io *io); 474static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 475static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 476static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 477static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 478 ctl_ha_dt_cb callback); 479static void ctl_datamove_remote_read(union ctl_io *io); 480static void ctl_datamove_remote(union ctl_io *io); 481static int ctl_process_done(union ctl_io *io); 482static void ctl_lun_thread(void *arg); 483static void ctl_thresh_thread(void *arg); 484static void ctl_work_thread(void *arg); 485static void ctl_enqueue_incoming(union ctl_io *io); 486static void ctl_enqueue_rtr(union ctl_io *io); 487static void ctl_enqueue_done(union ctl_io *io); 488static void ctl_enqueue_isc(union ctl_io *io); 489static const struct ctl_cmd_entry * 490 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 491static const struct ctl_cmd_entry * 492 ctl_validate_command(struct ctl_scsiio *ctsio); 493static int ctl_cmd_applicable(uint8_t lun_type, 494 const struct ctl_cmd_entry *entry); 495 496/* 497 * Load the serialization table. This isn't very pretty, but is probably 498 * the easiest way to do it. 499 */ 500#include "ctl_ser_table.c" 501 502/* 503 * We only need to define open, close and ioctl routines for this driver. 504 */ 505static struct cdevsw ctl_cdevsw = { 506 .d_version = D_VERSION, 507 .d_flags = 0, 508 .d_open = ctl_open, 509 .d_close = ctl_close, 510 .d_ioctl = ctl_ioctl, 511 .d_name = "ctl", 512}; 513 514 515MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 516MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 517 518static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 519 520static moduledata_t ctl_moduledata = { 521 "ctl", 522 ctl_module_event_handler, 523 NULL 524}; 525 526DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 527MODULE_VERSION(ctl, 1); 528 529static struct ctl_frontend ioctl_frontend = 530{ 531 .name = "ioctl", 532}; 533 534static void 535ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 536 union ctl_ha_msg *msg_info) 537{ 538 struct ctl_scsiio *ctsio; 539 540 if (msg_info->hdr.original_sc == NULL) { 541 printf("%s: original_sc == NULL!\n", __func__); 542 /* XXX KDM now what? */ 543 return; 544 } 545 546 ctsio = &msg_info->hdr.original_sc->scsiio; 547 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 548 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 549 ctsio->io_hdr.status = msg_info->hdr.status; 550 ctsio->scsi_status = msg_info->scsi.scsi_status; 551 ctsio->sense_len = msg_info->scsi.sense_len; 552 ctsio->sense_residual = msg_info->scsi.sense_residual; 553 ctsio->residual = msg_info->scsi.residual; 554 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 555 sizeof(ctsio->sense_data)); 556 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 557 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 558 ctl_enqueue_isc((union ctl_io *)ctsio); 559} 560 561static void 562ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 563 union ctl_ha_msg *msg_info) 564{ 565 struct ctl_scsiio *ctsio; 566 567 if (msg_info->hdr.serializing_sc == NULL) { 568 printf("%s: serializing_sc == NULL!\n", __func__); 569 /* XXX KDM now what? */ 570 return; 571 } 572 573 ctsio = &msg_info->hdr.serializing_sc->scsiio; 574#if 0 575 /* 576 * Attempt to catch the situation where an I/O has 577 * been freed, and we're using it again. 578 */ 579 if (ctsio->io_hdr.io_type == 0xff) { 580 union ctl_io *tmp_io; 581 tmp_io = (union ctl_io *)ctsio; 582 printf("%s: %p use after free!\n", __func__, 583 ctsio); 584 printf("%s: type %d msg %d cdb %x iptl: " 585 "%d:%d:%d:%d tag 0x%04x " 586 "flag %#x status %x\n", 587 __func__, 588 tmp_io->io_hdr.io_type, 589 tmp_io->io_hdr.msg_type, 590 tmp_io->scsiio.cdb[0], 591 tmp_io->io_hdr.nexus.initid.id, 592 tmp_io->io_hdr.nexus.targ_port, 593 tmp_io->io_hdr.nexus.targ_target.id, 594 tmp_io->io_hdr.nexus.targ_lun, 595 (tmp_io->io_hdr.io_type == 596 CTL_IO_TASK) ? 597 tmp_io->taskio.tag_num : 598 tmp_io->scsiio.tag_num, 599 tmp_io->io_hdr.flags, 600 tmp_io->io_hdr.status); 601 } 602#endif 603 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 604 ctl_enqueue_isc((union ctl_io *)ctsio); 605} 606 607/* 608 * ISC (Inter Shelf Communication) event handler. Events from the HA 609 * subsystem come in here. 610 */ 611static void 612ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 613{ 614 struct ctl_softc *softc; 615 union ctl_io *io; 616 struct ctl_prio *presio; 617 ctl_ha_status isc_status; 618 619 softc = control_softc; 620 io = NULL; 621 622 623#if 0 624 printf("CTL: Isc Msg event %d\n", event); 625#endif 626 if (event == CTL_HA_EVT_MSG_RECV) { 627 union ctl_ha_msg msg_info; 628 629 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 630 sizeof(msg_info), /*wait*/ 0); 631#if 0 632 printf("CTL: msg_type %d\n", msg_info.msg_type); 633#endif 634 if (isc_status != 0) { 635 printf("Error receiving message, status = %d\n", 636 isc_status); 637 return; 638 } 639 640 switch (msg_info.hdr.msg_type) { 641 case CTL_MSG_SERIALIZE: 642#if 0 643 printf("Serialize\n"); 644#endif 645 io = ctl_alloc_io_nowait(softc->othersc_pool); 646 if (io == NULL) { 647 printf("ctl_isc_event_handler: can't allocate " 648 "ctl_io!\n"); 649 /* Bad Juju */ 650 /* Need to set busy and send msg back */ 651 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 652 msg_info.hdr.status = CTL_SCSI_ERROR; 653 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 654 msg_info.scsi.sense_len = 0; 655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 656 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 657 } 658 goto bailout; 659 } 660 ctl_zero_io(io); 661 // populate ctsio from msg_info 662 io->io_hdr.io_type = CTL_IO_SCSI; 663 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 664 io->io_hdr.original_sc = msg_info.hdr.original_sc; 665#if 0 666 printf("pOrig %x\n", (int)msg_info.original_sc); 667#endif 668 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 669 CTL_FLAG_IO_ACTIVE; 670 /* 671 * If we're in serialization-only mode, we don't 672 * want to go through full done processing. Thus 673 * the COPY flag. 674 * 675 * XXX KDM add another flag that is more specific. 676 */ 677 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY) 678 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 679 io->io_hdr.nexus = msg_info.hdr.nexus; 680#if 0 681 printf("targ %d, port %d, iid %d, lun %d\n", 682 io->io_hdr.nexus.targ_target.id, 683 io->io_hdr.nexus.targ_port, 684 io->io_hdr.nexus.initid.id, 685 io->io_hdr.nexus.targ_lun); 686#endif 687 io->scsiio.tag_num = msg_info.scsi.tag_num; 688 io->scsiio.tag_type = msg_info.scsi.tag_type; 689 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 690 CTL_MAX_CDBLEN); 691 if (softc->ha_mode == CTL_HA_MODE_XFER) { 692 const struct ctl_cmd_entry *entry; 693 694 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 695 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 696 io->io_hdr.flags |= 697 entry->flags & CTL_FLAG_DATA_MASK; 698 } 699 ctl_enqueue_isc(io); 700 break; 701 702 /* Performed on the Originating SC, XFER mode only */ 703 case CTL_MSG_DATAMOVE: { 704 struct ctl_sg_entry *sgl; 705 int i, j; 706 707 io = msg_info.hdr.original_sc; 708 if (io == NULL) { 709 printf("%s: original_sc == NULL!\n", __func__); 710 /* XXX KDM do something here */ 711 break; 712 } 713 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 714 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 715 /* 716 * Keep track of this, we need to send it back over 717 * when the datamove is complete. 718 */ 719 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 720 721 if (msg_info.dt.sg_sequence == 0) { 722 /* 723 * XXX KDM we use the preallocated S/G list 724 * here, but we'll need to change this to 725 * dynamic allocation if we need larger S/G 726 * lists. 727 */ 728 if (msg_info.dt.kern_sg_entries > 729 sizeof(io->io_hdr.remote_sglist) / 730 sizeof(io->io_hdr.remote_sglist[0])) { 731 printf("%s: number of S/G entries " 732 "needed %u > allocated num %zd\n", 733 __func__, 734 msg_info.dt.kern_sg_entries, 735 sizeof(io->io_hdr.remote_sglist)/ 736 sizeof(io->io_hdr.remote_sglist[0])); 737 738 /* 739 * XXX KDM send a message back to 740 * the other side to shut down the 741 * DMA. The error will come back 742 * through via the normal channel. 743 */ 744 break; 745 } 746 sgl = io->io_hdr.remote_sglist; 747 memset(sgl, 0, 748 sizeof(io->io_hdr.remote_sglist)); 749 750 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 751 752 io->scsiio.kern_sg_entries = 753 msg_info.dt.kern_sg_entries; 754 io->scsiio.rem_sg_entries = 755 msg_info.dt.kern_sg_entries; 756 io->scsiio.kern_data_len = 757 msg_info.dt.kern_data_len; 758 io->scsiio.kern_total_len = 759 msg_info.dt.kern_total_len; 760 io->scsiio.kern_data_resid = 761 msg_info.dt.kern_data_resid; 762 io->scsiio.kern_rel_offset = 763 msg_info.dt.kern_rel_offset; 764 /* 765 * Clear out per-DMA flags. 766 */ 767 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 768 /* 769 * Add per-DMA flags that are set for this 770 * particular DMA request. 771 */ 772 io->io_hdr.flags |= msg_info.dt.flags & 773 CTL_FLAG_RDMA_MASK; 774 } else 775 sgl = (struct ctl_sg_entry *) 776 io->scsiio.kern_data_ptr; 777 778 for (i = msg_info.dt.sent_sg_entries, j = 0; 779 i < (msg_info.dt.sent_sg_entries + 780 msg_info.dt.cur_sg_entries); i++, j++) { 781 sgl[i].addr = msg_info.dt.sg_list[j].addr; 782 sgl[i].len = msg_info.dt.sg_list[j].len; 783 784#if 0 785 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 786 __func__, 787 msg_info.dt.sg_list[j].addr, 788 msg_info.dt.sg_list[j].len, 789 sgl[i].addr, sgl[i].len, j, i); 790#endif 791 } 792#if 0 793 memcpy(&sgl[msg_info.dt.sent_sg_entries], 794 msg_info.dt.sg_list, 795 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 796#endif 797 798 /* 799 * If this is the last piece of the I/O, we've got 800 * the full S/G list. Queue processing in the thread. 801 * Otherwise wait for the next piece. 802 */ 803 if (msg_info.dt.sg_last != 0) 804 ctl_enqueue_isc(io); 805 break; 806 } 807 /* Performed on the Serializing (primary) SC, XFER mode only */ 808 case CTL_MSG_DATAMOVE_DONE: { 809 if (msg_info.hdr.serializing_sc == NULL) { 810 printf("%s: serializing_sc == NULL!\n", 811 __func__); 812 /* XXX KDM now what? */ 813 break; 814 } 815 /* 816 * We grab the sense information here in case 817 * there was a failure, so we can return status 818 * back to the initiator. 819 */ 820 io = msg_info.hdr.serializing_sc; 821 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 822 io->io_hdr.status = msg_info.hdr.status; 823 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 824 io->scsiio.sense_len = msg_info.scsi.sense_len; 825 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 826 io->io_hdr.port_status = msg_info.scsi.fetd_status; 827 io->scsiio.residual = msg_info.scsi.residual; 828 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 829 sizeof(io->scsiio.sense_data)); 830 ctl_enqueue_isc(io); 831 break; 832 } 833 834 /* Preformed on Originating SC, SER_ONLY mode */ 835 case CTL_MSG_R2R: 836 io = msg_info.hdr.original_sc; 837 if (io == NULL) { 838 printf("%s: Major Bummer\n", __func__); 839 return; 840 } else { 841#if 0 842 printf("pOrig %x\n",(int) ctsio); 843#endif 844 } 845 io->io_hdr.msg_type = CTL_MSG_R2R; 846 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 847 ctl_enqueue_isc(io); 848 break; 849 850 /* 851 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 852 * mode. 853 * Performed on the Originating (i.e. secondary) SC in XFER 854 * mode 855 */ 856 case CTL_MSG_FINISH_IO: 857 if (softc->ha_mode == CTL_HA_MODE_XFER) 858 ctl_isc_handler_finish_xfer(softc, 859 &msg_info); 860 else 861 ctl_isc_handler_finish_ser_only(softc, 862 &msg_info); 863 break; 864 865 /* Preformed on Originating SC */ 866 case CTL_MSG_BAD_JUJU: 867 io = msg_info.hdr.original_sc; 868 if (io == NULL) { 869 printf("%s: Bad JUJU!, original_sc is NULL!\n", 870 __func__); 871 break; 872 } 873 ctl_copy_sense_data(&msg_info, io); 874 /* 875 * IO should have already been cleaned up on other 876 * SC so clear this flag so we won't send a message 877 * back to finish the IO there. 878 */ 879 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 880 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 881 882 /* io = msg_info.hdr.serializing_sc; */ 883 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 884 ctl_enqueue_isc(io); 885 break; 886 887 /* Handle resets sent from the other side */ 888 case CTL_MSG_MANAGE_TASKS: { 889 struct ctl_taskio *taskio; 890 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 891 softc->othersc_pool); 892 if (taskio == NULL) { 893 printf("ctl_isc_event_handler: can't allocate " 894 "ctl_io!\n"); 895 /* Bad Juju */ 896 /* should I just call the proper reset func 897 here??? */ 898 goto bailout; 899 } 900 ctl_zero_io((union ctl_io *)taskio); 901 taskio->io_hdr.io_type = CTL_IO_TASK; 902 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 903 taskio->io_hdr.nexus = msg_info.hdr.nexus; 904 taskio->task_action = msg_info.task.task_action; 905 taskio->tag_num = msg_info.task.tag_num; 906 taskio->tag_type = msg_info.task.tag_type; 907#ifdef CTL_TIME_IO 908 taskio->io_hdr.start_time = time_uptime; 909 getbintime(&taskio->io_hdr.start_bt); 910#if 0 911 cs_prof_gettime(&taskio->io_hdr.start_ticks); 912#endif 913#endif /* CTL_TIME_IO */ 914 ctl_run_task((union ctl_io *)taskio); 915 break; 916 } 917 /* Persistent Reserve action which needs attention */ 918 case CTL_MSG_PERS_ACTION: 919 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 920 softc->othersc_pool); 921 if (presio == NULL) { 922 printf("ctl_isc_event_handler: can't allocate " 923 "ctl_io!\n"); 924 /* Bad Juju */ 925 /* Need to set busy and send msg back */ 926 goto bailout; 927 } 928 ctl_zero_io((union ctl_io *)presio); 929 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 930 presio->pr_msg = msg_info.pr; 931 ctl_enqueue_isc((union ctl_io *)presio); 932 break; 933 case CTL_MSG_SYNC_FE: 934 rcv_sync_msg = 1; 935 break; 936 default: 937 printf("How did I get here?\n"); 938 } 939 } else if (event == CTL_HA_EVT_MSG_SENT) { 940 if (param != CTL_HA_STATUS_SUCCESS) { 941 printf("Bad status from ctl_ha_msg_send status %d\n", 942 param); 943 } 944 return; 945 } else if (event == CTL_HA_EVT_DISCONNECT) { 946 printf("CTL: Got a disconnect from Isc\n"); 947 return; 948 } else { 949 printf("ctl_isc_event_handler: Unknown event %d\n", event); 950 return; 951 } 952 953bailout: 954 return; 955} 956 957static void 958ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 959{ 960 struct scsi_sense_data *sense; 961 962 sense = &dest->scsiio.sense_data; 963 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 964 dest->scsiio.scsi_status = src->scsi.scsi_status; 965 dest->scsiio.sense_len = src->scsi.sense_len; 966 dest->io_hdr.status = src->hdr.status; 967} 968 969static void 970ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 971{ 972 ctl_ua_type *pu; 973 974 mtx_assert(&lun->lun_lock, MA_OWNED); 975 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 976 if (pu == NULL) 977 return; 978 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 979} 980 981static void 982ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 983{ 984 int i, j; 985 986 mtx_assert(&lun->lun_lock, MA_OWNED); 987 for (i = 0; i < CTL_MAX_PORTS; i++) { 988 if (lun->pending_ua[i] == NULL) 989 continue; 990 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 991 if (i * CTL_MAX_INIT_PER_PORT + j == except) 992 continue; 993 lun->pending_ua[i][j] |= ua; 994 } 995 } 996} 997 998static void 999ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1000{ 1001 ctl_ua_type *pu; 1002 1003 mtx_assert(&lun->lun_lock, MA_OWNED); 1004 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1005 if (pu == NULL) 1006 return; 1007 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1008} 1009 1010static void 1011ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1012{ 1013 int i, j; 1014 1015 mtx_assert(&lun->lun_lock, MA_OWNED); 1016 for (i = 0; i < CTL_MAX_PORTS; i++) { 1017 if (lun->pending_ua[i] == NULL) 1018 continue; 1019 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1020 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1021 continue; 1022 lun->pending_ua[i][j] &= ~ua; 1023 } 1024 } 1025} 1026 1027static int 1028ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 1029{ 1030 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1031 struct ctl_lun *lun; 1032 int error, value; 1033 1034 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 1035 value = 0; 1036 else 1037 value = 1; 1038 1039 error = sysctl_handle_int(oidp, &value, 0, req); 1040 if ((error != 0) || (req->newptr == NULL)) 1041 return (error); 1042 1043 mtx_lock(&softc->ctl_lock); 1044 if (value == 0) 1045 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1046 else 1047 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1048 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1049 mtx_lock(&lun->lun_lock); 1050 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1051 mtx_unlock(&lun->lun_lock); 1052 } 1053 mtx_unlock(&softc->ctl_lock); 1054 return (0); 1055} 1056 1057static int 1058ctl_init(void) 1059{ 1060 struct ctl_softc *softc; 1061 void *other_pool; 1062 struct ctl_port *port; 1063 int i, error, retval; 1064 //int isc_retval; 1065 1066 retval = 0; 1067 ctl_pause_rtr = 0; 1068 rcv_sync_msg = 0; 1069 1070 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1071 M_WAITOK | M_ZERO); 1072 softc = control_softc; 1073 1074 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1075 "cam/ctl"); 1076 1077 softc->dev->si_drv1 = softc; 1078 1079 /* 1080 * By default, return a "bad LUN" peripheral qualifier for unknown 1081 * LUNs. The user can override this default using the tunable or 1082 * sysctl. See the comment in ctl_inquiry_std() for more details. 1083 */ 1084 softc->inquiry_pq_no_lun = 1; 1085 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1086 &softc->inquiry_pq_no_lun); 1087 sysctl_ctx_init(&softc->sysctl_ctx); 1088 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1089 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1090 CTLFLAG_RD, 0, "CAM Target Layer"); 1091 1092 if (softc->sysctl_tree == NULL) { 1093 printf("%s: unable to allocate sysctl tree\n", __func__); 1094 destroy_dev(softc->dev); 1095 free(control_softc, M_DEVBUF); 1096 control_softc = NULL; 1097 return (ENOMEM); 1098 } 1099 1100 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1101 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1102 "inquiry_pq_no_lun", CTLFLAG_RW, 1103 &softc->inquiry_pq_no_lun, 0, 1104 "Report no lun possible for invalid LUNs"); 1105 1106 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1107 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1108 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1109 softc->open_count = 0; 1110 1111 /* 1112 * Default to actually sending a SYNCHRONIZE CACHE command down to 1113 * the drive. 1114 */ 1115 softc->flags = CTL_FLAG_REAL_SYNC; 1116 1117 /* 1118 * In Copan's HA scheme, the "master" and "slave" roles are 1119 * figured out through the slot the controller is in. Although it 1120 * is an active/active system, someone has to be in charge. 1121 */ 1122 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1123 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1124 "HA head ID (0 - no HA)"); 1125 if (softc->ha_id == 0) { 1126 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1127 softc->is_single = 1; 1128 softc->port_offset = 0; 1129 } else 1130 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1131 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1132 1133 STAILQ_INIT(&softc->lun_list); 1134 STAILQ_INIT(&softc->pending_lun_queue); 1135 STAILQ_INIT(&softc->fe_list); 1136 STAILQ_INIT(&softc->port_list); 1137 STAILQ_INIT(&softc->be_list); 1138 ctl_tpc_init(softc); 1139 1140 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1141 &other_pool) != 0) 1142 { 1143 printf("ctl: can't allocate %d entry other SC pool, " 1144 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1145 return (ENOMEM); 1146 } 1147 softc->othersc_pool = other_pool; 1148 1149 if (worker_threads <= 0) 1150 worker_threads = max(1, mp_ncpus / 4); 1151 if (worker_threads > CTL_MAX_THREADS) 1152 worker_threads = CTL_MAX_THREADS; 1153 1154 for (i = 0; i < worker_threads; i++) { 1155 struct ctl_thread *thr = &softc->threads[i]; 1156 1157 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1158 thr->ctl_softc = softc; 1159 STAILQ_INIT(&thr->incoming_queue); 1160 STAILQ_INIT(&thr->rtr_queue); 1161 STAILQ_INIT(&thr->done_queue); 1162 STAILQ_INIT(&thr->isc_queue); 1163 1164 error = kproc_kthread_add(ctl_work_thread, thr, 1165 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1166 if (error != 0) { 1167 printf("error creating CTL work thread!\n"); 1168 ctl_pool_free(other_pool); 1169 return (error); 1170 } 1171 } 1172 error = kproc_kthread_add(ctl_lun_thread, softc, 1173 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1174 if (error != 0) { 1175 printf("error creating CTL lun thread!\n"); 1176 ctl_pool_free(other_pool); 1177 return (error); 1178 } 1179 error = kproc_kthread_add(ctl_thresh_thread, softc, 1180 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1181 if (error != 0) { 1182 printf("error creating CTL threshold thread!\n"); 1183 ctl_pool_free(other_pool); 1184 return (error); 1185 } 1186 1187 /* 1188 * Initialize the ioctl front end. 1189 */ 1190 ctl_frontend_register(&ioctl_frontend); 1191 port = &softc->ioctl_info.port; 1192 port->frontend = &ioctl_frontend; 1193 sprintf(softc->ioctl_info.port_name, "ioctl"); 1194 port->port_type = CTL_PORT_IOCTL; 1195 port->num_requested_ctl_io = 100; 1196 port->port_name = softc->ioctl_info.port_name; 1197 port->port_online = ctl_ioctl_online; 1198 port->port_offline = ctl_ioctl_offline; 1199 port->onoff_arg = &softc->ioctl_info; 1200 port->lun_enable = ctl_ioctl_lun_enable; 1201 port->lun_disable = ctl_ioctl_lun_disable; 1202 port->targ_lun_arg = &softc->ioctl_info; 1203 port->fe_datamove = ctl_ioctl_datamove; 1204 port->fe_done = ctl_ioctl_done; 1205 port->max_targets = 15; 1206 port->max_target_id = 15; 1207 1208 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1209 printf("ctl: ioctl front end registration failed, will " 1210 "continue anyway\n"); 1211 } 1212 1213 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1214 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1215 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1216 1217#ifdef CTL_IO_DELAY 1218 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1219 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1220 sizeof(struct callout), CTL_TIMER_BYTES); 1221 return (EINVAL); 1222 } 1223#endif /* CTL_IO_DELAY */ 1224 1225 return (0); 1226} 1227 1228void 1229ctl_shutdown(void) 1230{ 1231 struct ctl_softc *softc; 1232 struct ctl_lun *lun, *next_lun; 1233 1234 softc = (struct ctl_softc *)control_softc; 1235 1236 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1237 printf("ctl: ioctl front end deregistration failed\n"); 1238 1239 mtx_lock(&softc->ctl_lock); 1240 1241 /* 1242 * Free up each LUN. 1243 */ 1244 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1245 next_lun = STAILQ_NEXT(lun, links); 1246 ctl_free_lun(lun); 1247 } 1248 1249 mtx_unlock(&softc->ctl_lock); 1250 1251 ctl_frontend_deregister(&ioctl_frontend); 1252 1253#if 0 1254 ctl_shutdown_thread(softc->work_thread); 1255 mtx_destroy(&softc->queue_lock); 1256#endif 1257 1258 ctl_tpc_shutdown(softc); 1259 uma_zdestroy(softc->io_zone); 1260 mtx_destroy(&softc->ctl_lock); 1261 1262 destroy_dev(softc->dev); 1263 1264 sysctl_ctx_free(&softc->sysctl_ctx); 1265 1266 free(control_softc, M_DEVBUF); 1267 control_softc = NULL; 1268} 1269 1270static int 1271ctl_module_event_handler(module_t mod, int what, void *arg) 1272{ 1273 1274 switch (what) { 1275 case MOD_LOAD: 1276 return (ctl_init()); 1277 case MOD_UNLOAD: 1278 return (EBUSY); 1279 default: 1280 return (EOPNOTSUPP); 1281 } 1282} 1283 1284/* 1285 * XXX KDM should we do some access checks here? Bump a reference count to 1286 * prevent a CTL module from being unloaded while someone has it open? 1287 */ 1288static int 1289ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1290{ 1291 return (0); 1292} 1293 1294static int 1295ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1296{ 1297 return (0); 1298} 1299 1300int 1301ctl_port_enable(ctl_port_type port_type) 1302{ 1303 struct ctl_softc *softc = control_softc; 1304 struct ctl_port *port; 1305 1306 if (softc->is_single == 0) { 1307 union ctl_ha_msg msg_info; 1308 int isc_retval; 1309 1310#if 0 1311 printf("%s: HA mode, synchronizing frontend enable\n", 1312 __func__); 1313#endif 1314 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1315 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1316 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1317 printf("Sync msg send error retval %d\n", isc_retval); 1318 } 1319 if (!rcv_sync_msg) { 1320 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1321 sizeof(msg_info), 1); 1322 } 1323#if 0 1324 printf("CTL:Frontend Enable\n"); 1325 } else { 1326 printf("%s: single mode, skipping frontend synchronization\n", 1327 __func__); 1328#endif 1329 } 1330 1331 STAILQ_FOREACH(port, &softc->port_list, links) { 1332 if (port_type & port->port_type) 1333 { 1334#if 0 1335 printf("port %d\n", port->targ_port); 1336#endif 1337 ctl_port_online(port); 1338 } 1339 } 1340 1341 return (0); 1342} 1343 1344int 1345ctl_port_disable(ctl_port_type port_type) 1346{ 1347 struct ctl_softc *softc; 1348 struct ctl_port *port; 1349 1350 softc = control_softc; 1351 1352 STAILQ_FOREACH(port, &softc->port_list, links) { 1353 if (port_type & port->port_type) 1354 ctl_port_offline(port); 1355 } 1356 1357 return (0); 1358} 1359 1360/* 1361 * Returns 0 for success, 1 for failure. 1362 * Currently the only failure mode is if there aren't enough entries 1363 * allocated. So, in case of a failure, look at num_entries_dropped, 1364 * reallocate and try again. 1365 */ 1366int 1367ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1368 int *num_entries_filled, int *num_entries_dropped, 1369 ctl_port_type port_type, int no_virtual) 1370{ 1371 struct ctl_softc *softc; 1372 struct ctl_port *port; 1373 int entries_dropped, entries_filled; 1374 int retval; 1375 int i; 1376 1377 softc = control_softc; 1378 1379 retval = 0; 1380 entries_filled = 0; 1381 entries_dropped = 0; 1382 1383 i = 0; 1384 mtx_lock(&softc->ctl_lock); 1385 STAILQ_FOREACH(port, &softc->port_list, links) { 1386 struct ctl_port_entry *entry; 1387 1388 if ((port->port_type & port_type) == 0) 1389 continue; 1390 1391 if ((no_virtual != 0) 1392 && (port->virtual_port != 0)) 1393 continue; 1394 1395 if (entries_filled >= num_entries_alloced) { 1396 entries_dropped++; 1397 continue; 1398 } 1399 entry = &entries[i]; 1400 1401 entry->port_type = port->port_type; 1402 strlcpy(entry->port_name, port->port_name, 1403 sizeof(entry->port_name)); 1404 entry->physical_port = port->physical_port; 1405 entry->virtual_port = port->virtual_port; 1406 entry->wwnn = port->wwnn; 1407 entry->wwpn = port->wwpn; 1408 1409 i++; 1410 entries_filled++; 1411 } 1412 1413 mtx_unlock(&softc->ctl_lock); 1414 1415 if (entries_dropped > 0) 1416 retval = 1; 1417 1418 *num_entries_dropped = entries_dropped; 1419 *num_entries_filled = entries_filled; 1420 1421 return (retval); 1422} 1423 1424static void 1425ctl_ioctl_online(void *arg) 1426{ 1427 struct ctl_ioctl_info *ioctl_info; 1428 1429 ioctl_info = (struct ctl_ioctl_info *)arg; 1430 1431 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1432} 1433 1434static void 1435ctl_ioctl_offline(void *arg) 1436{ 1437 struct ctl_ioctl_info *ioctl_info; 1438 1439 ioctl_info = (struct ctl_ioctl_info *)arg; 1440 1441 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1442} 1443 1444/* 1445 * Remove an initiator by port number and initiator ID. 1446 * Returns 0 for success, -1 for failure. 1447 */ 1448int 1449ctl_remove_initiator(struct ctl_port *port, int iid) 1450{ 1451 struct ctl_softc *softc = control_softc; 1452 1453 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1454 1455 if (iid > CTL_MAX_INIT_PER_PORT) { 1456 printf("%s: initiator ID %u > maximun %u!\n", 1457 __func__, iid, CTL_MAX_INIT_PER_PORT); 1458 return (-1); 1459 } 1460 1461 mtx_lock(&softc->ctl_lock); 1462 port->wwpn_iid[iid].in_use--; 1463 port->wwpn_iid[iid].last_use = time_uptime; 1464 mtx_unlock(&softc->ctl_lock); 1465 1466 return (0); 1467} 1468 1469/* 1470 * Add an initiator to the initiator map. 1471 * Returns iid for success, < 0 for failure. 1472 */ 1473int 1474ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1475{ 1476 struct ctl_softc *softc = control_softc; 1477 time_t best_time; 1478 int i, best; 1479 1480 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1481 1482 if (iid >= CTL_MAX_INIT_PER_PORT) { 1483 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1484 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1485 free(name, M_CTL); 1486 return (-1); 1487 } 1488 1489 mtx_lock(&softc->ctl_lock); 1490 1491 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1492 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1493 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1494 iid = i; 1495 break; 1496 } 1497 if (name != NULL && port->wwpn_iid[i].name != NULL && 1498 strcmp(name, port->wwpn_iid[i].name) == 0) { 1499 iid = i; 1500 break; 1501 } 1502 } 1503 } 1504 1505 if (iid < 0) { 1506 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1507 if (port->wwpn_iid[i].in_use == 0 && 1508 port->wwpn_iid[i].wwpn == 0 && 1509 port->wwpn_iid[i].name == NULL) { 1510 iid = i; 1511 break; 1512 } 1513 } 1514 } 1515 1516 if (iid < 0) { 1517 best = -1; 1518 best_time = INT32_MAX; 1519 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1520 if (port->wwpn_iid[i].in_use == 0) { 1521 if (port->wwpn_iid[i].last_use < best_time) { 1522 best = i; 1523 best_time = port->wwpn_iid[i].last_use; 1524 } 1525 } 1526 } 1527 iid = best; 1528 } 1529 1530 if (iid < 0) { 1531 mtx_unlock(&softc->ctl_lock); 1532 free(name, M_CTL); 1533 return (-2); 1534 } 1535 1536 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1537 /* 1538 * This is not an error yet. 1539 */ 1540 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1541#if 0 1542 printf("%s: port %d iid %u WWPN %#jx arrived" 1543 " again\n", __func__, port->targ_port, 1544 iid, (uintmax_t)wwpn); 1545#endif 1546 goto take; 1547 } 1548 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1549 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1550#if 0 1551 printf("%s: port %d iid %u name '%s' arrived" 1552 " again\n", __func__, port->targ_port, 1553 iid, name); 1554#endif 1555 goto take; 1556 } 1557 1558 /* 1559 * This is an error, but what do we do about it? The 1560 * driver is telling us we have a new WWPN for this 1561 * initiator ID, so we pretty much need to use it. 1562 */ 1563 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1564 " but WWPN %#jx '%s' is still at that address\n", 1565 __func__, port->targ_port, iid, wwpn, name, 1566 (uintmax_t)port->wwpn_iid[iid].wwpn, 1567 port->wwpn_iid[iid].name); 1568 1569 /* 1570 * XXX KDM clear have_ca and ua_pending on each LUN for 1571 * this initiator. 1572 */ 1573 } 1574take: 1575 free(port->wwpn_iid[iid].name, M_CTL); 1576 port->wwpn_iid[iid].name = name; 1577 port->wwpn_iid[iid].wwpn = wwpn; 1578 port->wwpn_iid[iid].in_use++; 1579 mtx_unlock(&softc->ctl_lock); 1580 1581 return (iid); 1582} 1583 1584static int 1585ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1586{ 1587 int len; 1588 1589 switch (port->port_type) { 1590 case CTL_PORT_FC: 1591 { 1592 struct scsi_transportid_fcp *id = 1593 (struct scsi_transportid_fcp *)buf; 1594 if (port->wwpn_iid[iid].wwpn == 0) 1595 return (0); 1596 memset(id, 0, sizeof(*id)); 1597 id->format_protocol = SCSI_PROTO_FC; 1598 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1599 return (sizeof(*id)); 1600 } 1601 case CTL_PORT_ISCSI: 1602 { 1603 struct scsi_transportid_iscsi_port *id = 1604 (struct scsi_transportid_iscsi_port *)buf; 1605 if (port->wwpn_iid[iid].name == NULL) 1606 return (0); 1607 memset(id, 0, 256); 1608 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1609 SCSI_PROTO_ISCSI; 1610 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1611 len = roundup2(min(len, 252), 4); 1612 scsi_ulto2b(len, id->additional_length); 1613 return (sizeof(*id) + len); 1614 } 1615 case CTL_PORT_SAS: 1616 { 1617 struct scsi_transportid_sas *id = 1618 (struct scsi_transportid_sas *)buf; 1619 if (port->wwpn_iid[iid].wwpn == 0) 1620 return (0); 1621 memset(id, 0, sizeof(*id)); 1622 id->format_protocol = SCSI_PROTO_SAS; 1623 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1624 return (sizeof(*id)); 1625 } 1626 default: 1627 { 1628 struct scsi_transportid_spi *id = 1629 (struct scsi_transportid_spi *)buf; 1630 memset(id, 0, sizeof(*id)); 1631 id->format_protocol = SCSI_PROTO_SPI; 1632 scsi_ulto2b(iid, id->scsi_addr); 1633 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1634 return (sizeof(*id)); 1635 } 1636 } 1637} 1638 1639static int 1640ctl_ioctl_lun_enable(void *arg, int lun_id) 1641{ 1642 return (0); 1643} 1644 1645static int 1646ctl_ioctl_lun_disable(void *arg, int lun_id) 1647{ 1648 return (0); 1649} 1650 1651/* 1652 * Data movement routine for the CTL ioctl frontend port. 1653 */ 1654static int 1655ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1656{ 1657 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1658 struct ctl_sg_entry ext_entry, kern_entry; 1659 int ext_sglen, ext_sg_entries, kern_sg_entries; 1660 int ext_sg_start, ext_offset; 1661 int len_to_copy, len_copied; 1662 int kern_watermark, ext_watermark; 1663 int ext_sglist_malloced; 1664 int i, j; 1665 1666 ext_sglist_malloced = 0; 1667 ext_sg_start = 0; 1668 ext_offset = 0; 1669 1670 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1671 1672 /* 1673 * If this flag is set, fake the data transfer. 1674 */ 1675 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1676 ctsio->ext_data_filled = ctsio->ext_data_len; 1677 goto bailout; 1678 } 1679 1680 /* 1681 * To simplify things here, if we have a single buffer, stick it in 1682 * a S/G entry and just make it a single entry S/G list. 1683 */ 1684 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1685 int len_seen; 1686 1687 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1688 1689 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1690 M_WAITOK); 1691 ext_sglist_malloced = 1; 1692 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1693 ext_sglen) != 0) { 1694 ctl_set_internal_failure(ctsio, 1695 /*sks_valid*/ 0, 1696 /*retry_count*/ 0); 1697 goto bailout; 1698 } 1699 ext_sg_entries = ctsio->ext_sg_entries; 1700 len_seen = 0; 1701 for (i = 0; i < ext_sg_entries; i++) { 1702 if ((len_seen + ext_sglist[i].len) >= 1703 ctsio->ext_data_filled) { 1704 ext_sg_start = i; 1705 ext_offset = ctsio->ext_data_filled - len_seen; 1706 break; 1707 } 1708 len_seen += ext_sglist[i].len; 1709 } 1710 } else { 1711 ext_sglist = &ext_entry; 1712 ext_sglist->addr = ctsio->ext_data_ptr; 1713 ext_sglist->len = ctsio->ext_data_len; 1714 ext_sg_entries = 1; 1715 ext_sg_start = 0; 1716 ext_offset = ctsio->ext_data_filled; 1717 } 1718 1719 if (ctsio->kern_sg_entries > 0) { 1720 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1721 kern_sg_entries = ctsio->kern_sg_entries; 1722 } else { 1723 kern_sglist = &kern_entry; 1724 kern_sglist->addr = ctsio->kern_data_ptr; 1725 kern_sglist->len = ctsio->kern_data_len; 1726 kern_sg_entries = 1; 1727 } 1728 1729 1730 kern_watermark = 0; 1731 ext_watermark = ext_offset; 1732 len_copied = 0; 1733 for (i = ext_sg_start, j = 0; 1734 i < ext_sg_entries && j < kern_sg_entries;) { 1735 uint8_t *ext_ptr, *kern_ptr; 1736 1737 len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 1738 kern_sglist[j].len - kern_watermark); 1739 1740 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1741 ext_ptr = ext_ptr + ext_watermark; 1742 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1743 /* 1744 * XXX KDM fix this! 1745 */ 1746 panic("need to implement bus address support"); 1747#if 0 1748 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1749#endif 1750 } else 1751 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1752 kern_ptr = kern_ptr + kern_watermark; 1753 1754 kern_watermark += len_to_copy; 1755 ext_watermark += len_to_copy; 1756 1757 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1758 CTL_FLAG_DATA_IN) { 1759 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1760 "bytes to user\n", len_to_copy)); 1761 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1762 "to %p\n", kern_ptr, ext_ptr)); 1763 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1764 ctl_set_internal_failure(ctsio, 1765 /*sks_valid*/ 0, 1766 /*retry_count*/ 0); 1767 goto bailout; 1768 } 1769 } else { 1770 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1771 "bytes from user\n", len_to_copy)); 1772 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1773 "to %p\n", ext_ptr, kern_ptr)); 1774 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1775 ctl_set_internal_failure(ctsio, 1776 /*sks_valid*/ 0, 1777 /*retry_count*/0); 1778 goto bailout; 1779 } 1780 } 1781 1782 len_copied += len_to_copy; 1783 1784 if (ext_sglist[i].len == ext_watermark) { 1785 i++; 1786 ext_watermark = 0; 1787 } 1788 1789 if (kern_sglist[j].len == kern_watermark) { 1790 j++; 1791 kern_watermark = 0; 1792 } 1793 } 1794 1795 ctsio->ext_data_filled += len_copied; 1796 1797 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1798 "kern_sg_entries: %d\n", ext_sg_entries, 1799 kern_sg_entries)); 1800 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1801 "kern_data_len = %d\n", ctsio->ext_data_len, 1802 ctsio->kern_data_len)); 1803 1804 1805 /* XXX KDM set residual?? */ 1806bailout: 1807 1808 if (ext_sglist_malloced != 0) 1809 free(ext_sglist, M_CTL); 1810 1811 return (CTL_RETVAL_COMPLETE); 1812} 1813 1814/* 1815 * Serialize a command that went down the "wrong" side, and so was sent to 1816 * this controller for execution. The logic is a little different than the 1817 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1818 * sent back to the other side, but in the success case, we execute the 1819 * command on this side (XFER mode) or tell the other side to execute it 1820 * (SER_ONLY mode). 1821 */ 1822static int 1823ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1824{ 1825 struct ctl_softc *softc; 1826 union ctl_ha_msg msg_info; 1827 struct ctl_lun *lun; 1828 int retval = 0; 1829 uint32_t targ_lun; 1830 1831 softc = control_softc; 1832 1833 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1834 lun = softc->ctl_luns[targ_lun]; 1835 if (lun==NULL) 1836 { 1837 /* 1838 * Why isn't LUN defined? The other side wouldn't 1839 * send a cmd if the LUN is undefined. 1840 */ 1841 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1842 1843 /* "Logical unit not supported" */ 1844 ctl_set_sense_data(&msg_info.scsi.sense_data, 1845 lun, 1846 /*sense_format*/SSD_TYPE_NONE, 1847 /*current_error*/ 1, 1848 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1849 /*asc*/ 0x25, 1850 /*ascq*/ 0x00, 1851 SSD_ELEM_NONE); 1852 1853 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1854 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1855 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1856 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1857 msg_info.hdr.serializing_sc = NULL; 1858 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1859 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1860 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1861 } 1862 return(1); 1863 1864 } 1865 1866 mtx_lock(&lun->lun_lock); 1867 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1868 1869 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1870 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1871 ooa_links))) { 1872 case CTL_ACTION_BLOCK: 1873 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1874 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1875 blocked_links); 1876 break; 1877 case CTL_ACTION_PASS: 1878 case CTL_ACTION_SKIP: 1879 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1880 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1881 ctl_enqueue_rtr((union ctl_io *)ctsio); 1882 } else { 1883 1884 /* send msg back to other side */ 1885 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1886 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1887 msg_info.hdr.msg_type = CTL_MSG_R2R; 1888#if 0 1889 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1890#endif 1891 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1892 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1893 } 1894 } 1895 break; 1896 case CTL_ACTION_OVERLAP: 1897 /* OVERLAPPED COMMANDS ATTEMPTED */ 1898 ctl_set_sense_data(&msg_info.scsi.sense_data, 1899 lun, 1900 /*sense_format*/SSD_TYPE_NONE, 1901 /*current_error*/ 1, 1902 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1903 /*asc*/ 0x4E, 1904 /*ascq*/ 0x00, 1905 SSD_ELEM_NONE); 1906 1907 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1908 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1909 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1910 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1911 msg_info.hdr.serializing_sc = NULL; 1912 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1913#if 0 1914 printf("BAD JUJU:Major Bummer Overlap\n"); 1915#endif 1916 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1917 retval = 1; 1918 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1919 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1920 } 1921 break; 1922 case CTL_ACTION_OVERLAP_TAG: 1923 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1924 ctl_set_sense_data(&msg_info.scsi.sense_data, 1925 lun, 1926 /*sense_format*/SSD_TYPE_NONE, 1927 /*current_error*/ 1, 1928 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1929 /*asc*/ 0x4D, 1930 /*ascq*/ ctsio->tag_num & 0xff, 1931 SSD_ELEM_NONE); 1932 1933 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1934 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1935 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1936 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1937 msg_info.hdr.serializing_sc = NULL; 1938 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1939#if 0 1940 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1941#endif 1942 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1943 retval = 1; 1944 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1945 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1946 } 1947 break; 1948 case CTL_ACTION_ERROR: 1949 default: 1950 /* "Internal target failure" */ 1951 ctl_set_sense_data(&msg_info.scsi.sense_data, 1952 lun, 1953 /*sense_format*/SSD_TYPE_NONE, 1954 /*current_error*/ 1, 1955 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1956 /*asc*/ 0x44, 1957 /*ascq*/ 0x00, 1958 SSD_ELEM_NONE); 1959 1960 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1961 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1962 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1963 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1964 msg_info.hdr.serializing_sc = NULL; 1965 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1966#if 0 1967 printf("BAD JUJU:Major Bummer HW Error\n"); 1968#endif 1969 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1970 retval = 1; 1971 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1972 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1973 } 1974 break; 1975 } 1976 mtx_unlock(&lun->lun_lock); 1977 return (retval); 1978} 1979 1980static int 1981ctl_ioctl_submit_wait(union ctl_io *io) 1982{ 1983 struct ctl_fe_ioctl_params params; 1984 ctl_fe_ioctl_state last_state; 1985 int done, retval; 1986 1987 retval = 0; 1988 1989 bzero(¶ms, sizeof(params)); 1990 1991 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1992 cv_init(¶ms.sem, "ctlioccv"); 1993 params.state = CTL_IOCTL_INPROG; 1994 last_state = params.state; 1995 1996 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1997 1998 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1999 2000 /* This shouldn't happen */ 2001 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 2002 return (retval); 2003 2004 done = 0; 2005 2006 do { 2007 mtx_lock(¶ms.ioctl_mtx); 2008 /* 2009 * Check the state here, and don't sleep if the state has 2010 * already changed (i.e. wakeup has already occured, but we 2011 * weren't waiting yet). 2012 */ 2013 if (params.state == last_state) { 2014 /* XXX KDM cv_wait_sig instead? */ 2015 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 2016 } 2017 last_state = params.state; 2018 2019 switch (params.state) { 2020 case CTL_IOCTL_INPROG: 2021 /* Why did we wake up? */ 2022 /* XXX KDM error here? */ 2023 mtx_unlock(¶ms.ioctl_mtx); 2024 break; 2025 case CTL_IOCTL_DATAMOVE: 2026 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 2027 2028 /* 2029 * change last_state back to INPROG to avoid 2030 * deadlock on subsequent data moves. 2031 */ 2032 params.state = last_state = CTL_IOCTL_INPROG; 2033 2034 mtx_unlock(¶ms.ioctl_mtx); 2035 ctl_ioctl_do_datamove(&io->scsiio); 2036 /* 2037 * Note that in some cases, most notably writes, 2038 * this will queue the I/O and call us back later. 2039 * In other cases, generally reads, this routine 2040 * will immediately call back and wake us up, 2041 * probably using our own context. 2042 */ 2043 io->scsiio.be_move_done(io); 2044 break; 2045 case CTL_IOCTL_DONE: 2046 mtx_unlock(¶ms.ioctl_mtx); 2047 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2048 done = 1; 2049 break; 2050 default: 2051 mtx_unlock(¶ms.ioctl_mtx); 2052 /* XXX KDM error here? */ 2053 break; 2054 } 2055 } while (done == 0); 2056 2057 mtx_destroy(¶ms.ioctl_mtx); 2058 cv_destroy(¶ms.sem); 2059 2060 return (CTL_RETVAL_COMPLETE); 2061} 2062 2063static void 2064ctl_ioctl_datamove(union ctl_io *io) 2065{ 2066 struct ctl_fe_ioctl_params *params; 2067 2068 params = (struct ctl_fe_ioctl_params *) 2069 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2070 2071 mtx_lock(¶ms->ioctl_mtx); 2072 params->state = CTL_IOCTL_DATAMOVE; 2073 cv_broadcast(¶ms->sem); 2074 mtx_unlock(¶ms->ioctl_mtx); 2075} 2076 2077static void 2078ctl_ioctl_done(union ctl_io *io) 2079{ 2080 struct ctl_fe_ioctl_params *params; 2081 2082 params = (struct ctl_fe_ioctl_params *) 2083 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2084 2085 mtx_lock(¶ms->ioctl_mtx); 2086 params->state = CTL_IOCTL_DONE; 2087 cv_broadcast(¶ms->sem); 2088 mtx_unlock(¶ms->ioctl_mtx); 2089} 2090 2091static void 2092ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2093{ 2094 struct ctl_fe_ioctl_startstop_info *sd_info; 2095 2096 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2097 2098 sd_info->hs_info.status = metatask->status; 2099 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2100 sd_info->hs_info.luns_complete = 2101 metatask->taskinfo.startstop.luns_complete; 2102 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2103 2104 cv_broadcast(&sd_info->sem); 2105} 2106 2107static void 2108ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2109{ 2110 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2111 2112 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2113 2114 mtx_lock(fe_bbr_info->lock); 2115 fe_bbr_info->bbr_info->status = metatask->status; 2116 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2117 fe_bbr_info->wakeup_done = 1; 2118 mtx_unlock(fe_bbr_info->lock); 2119 2120 cv_broadcast(&fe_bbr_info->sem); 2121} 2122 2123/* 2124 * Returns 0 for success, errno for failure. 2125 */ 2126static int 2127ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2128 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2129{ 2130 union ctl_io *io; 2131 int retval; 2132 2133 retval = 0; 2134 2135 mtx_lock(&lun->lun_lock); 2136 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2137 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2138 ooa_links)) { 2139 struct ctl_ooa_entry *entry; 2140 2141 /* 2142 * If we've got more than we can fit, just count the 2143 * remaining entries. 2144 */ 2145 if (*cur_fill_num >= ooa_hdr->alloc_num) 2146 continue; 2147 2148 entry = &kern_entries[*cur_fill_num]; 2149 2150 entry->tag_num = io->scsiio.tag_num; 2151 entry->lun_num = lun->lun; 2152#ifdef CTL_TIME_IO 2153 entry->start_bt = io->io_hdr.start_bt; 2154#endif 2155 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2156 entry->cdb_len = io->scsiio.cdb_len; 2157 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2158 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2159 2160 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2161 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2162 2163 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2164 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2165 2166 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2167 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2168 2169 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2170 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2171 } 2172 mtx_unlock(&lun->lun_lock); 2173 2174 return (retval); 2175} 2176 2177static void * 2178ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2179 size_t error_str_len) 2180{ 2181 void *kptr; 2182 2183 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2184 2185 if (copyin(user_addr, kptr, len) != 0) { 2186 snprintf(error_str, error_str_len, "Error copying %d bytes " 2187 "from user address %p to kernel address %p", len, 2188 user_addr, kptr); 2189 free(kptr, M_CTL); 2190 return (NULL); 2191 } 2192 2193 return (kptr); 2194} 2195 2196static void 2197ctl_free_args(int num_args, struct ctl_be_arg *args) 2198{ 2199 int i; 2200 2201 if (args == NULL) 2202 return; 2203 2204 for (i = 0; i < num_args; i++) { 2205 free(args[i].kname, M_CTL); 2206 free(args[i].kvalue, M_CTL); 2207 } 2208 2209 free(args, M_CTL); 2210} 2211 2212static struct ctl_be_arg * 2213ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2214 char *error_str, size_t error_str_len) 2215{ 2216 struct ctl_be_arg *args; 2217 int i; 2218 2219 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2220 error_str, error_str_len); 2221 2222 if (args == NULL) 2223 goto bailout; 2224 2225 for (i = 0; i < num_args; i++) { 2226 args[i].kname = NULL; 2227 args[i].kvalue = NULL; 2228 } 2229 2230 for (i = 0; i < num_args; i++) { 2231 uint8_t *tmpptr; 2232 2233 args[i].kname = ctl_copyin_alloc(args[i].name, 2234 args[i].namelen, error_str, error_str_len); 2235 if (args[i].kname == NULL) 2236 goto bailout; 2237 2238 if (args[i].kname[args[i].namelen - 1] != '\0') { 2239 snprintf(error_str, error_str_len, "Argument %d " 2240 "name is not NUL-terminated", i); 2241 goto bailout; 2242 } 2243 2244 if (args[i].flags & CTL_BEARG_RD) { 2245 tmpptr = ctl_copyin_alloc(args[i].value, 2246 args[i].vallen, error_str, error_str_len); 2247 if (tmpptr == NULL) 2248 goto bailout; 2249 if ((args[i].flags & CTL_BEARG_ASCII) 2250 && (tmpptr[args[i].vallen - 1] != '\0')) { 2251 snprintf(error_str, error_str_len, "Argument " 2252 "%d value is not NUL-terminated", i); 2253 goto bailout; 2254 } 2255 args[i].kvalue = tmpptr; 2256 } else { 2257 args[i].kvalue = malloc(args[i].vallen, 2258 M_CTL, M_WAITOK | M_ZERO); 2259 } 2260 } 2261 2262 return (args); 2263bailout: 2264 2265 ctl_free_args(num_args, args); 2266 2267 return (NULL); 2268} 2269 2270static void 2271ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2272{ 2273 int i; 2274 2275 for (i = 0; i < num_args; i++) { 2276 if (args[i].flags & CTL_BEARG_WR) 2277 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2278 } 2279} 2280 2281/* 2282 * Escape characters that are illegal or not recommended in XML. 2283 */ 2284int 2285ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2286{ 2287 char *end = str + size; 2288 int retval; 2289 2290 retval = 0; 2291 2292 for (; *str && str < end; str++) { 2293 switch (*str) { 2294 case '&': 2295 retval = sbuf_printf(sb, "&"); 2296 break; 2297 case '>': 2298 retval = sbuf_printf(sb, ">"); 2299 break; 2300 case '<': 2301 retval = sbuf_printf(sb, "<"); 2302 break; 2303 default: 2304 retval = sbuf_putc(sb, *str); 2305 break; 2306 } 2307 2308 if (retval != 0) 2309 break; 2310 2311 } 2312 2313 return (retval); 2314} 2315 2316static void 2317ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2318{ 2319 struct scsi_vpd_id_descriptor *desc; 2320 int i; 2321 2322 if (id == NULL || id->len < 4) 2323 return; 2324 desc = (struct scsi_vpd_id_descriptor *)id->data; 2325 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2326 case SVPD_ID_TYPE_T10: 2327 sbuf_printf(sb, "t10."); 2328 break; 2329 case SVPD_ID_TYPE_EUI64: 2330 sbuf_printf(sb, "eui."); 2331 break; 2332 case SVPD_ID_TYPE_NAA: 2333 sbuf_printf(sb, "naa."); 2334 break; 2335 case SVPD_ID_TYPE_SCSI_NAME: 2336 break; 2337 } 2338 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2339 case SVPD_ID_CODESET_BINARY: 2340 for (i = 0; i < desc->length; i++) 2341 sbuf_printf(sb, "%02x", desc->identifier[i]); 2342 break; 2343 case SVPD_ID_CODESET_ASCII: 2344 sbuf_printf(sb, "%.*s", (int)desc->length, 2345 (char *)desc->identifier); 2346 break; 2347 case SVPD_ID_CODESET_UTF8: 2348 sbuf_printf(sb, "%s", (char *)desc->identifier); 2349 break; 2350 } 2351} 2352 2353static int 2354ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2355 struct thread *td) 2356{ 2357 struct ctl_softc *softc; 2358 int retval; 2359 2360 softc = control_softc; 2361 2362 retval = 0; 2363 2364 switch (cmd) { 2365 case CTL_IO: { 2366 union ctl_io *io; 2367 void *pool_tmp; 2368 2369 /* 2370 * If we haven't been "enabled", don't allow any SCSI I/O 2371 * to this FETD. 2372 */ 2373 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2374 retval = EPERM; 2375 break; 2376 } 2377 2378 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2379 2380 /* 2381 * Need to save the pool reference so it doesn't get 2382 * spammed by the user's ctl_io. 2383 */ 2384 pool_tmp = io->io_hdr.pool; 2385 memcpy(io, (void *)addr, sizeof(*io)); 2386 io->io_hdr.pool = pool_tmp; 2387 2388 /* 2389 * No status yet, so make sure the status is set properly. 2390 */ 2391 io->io_hdr.status = CTL_STATUS_NONE; 2392 2393 /* 2394 * The user sets the initiator ID, target and LUN IDs. 2395 */ 2396 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2397 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2398 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2399 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2400 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2401 2402 retval = ctl_ioctl_submit_wait(io); 2403 2404 if (retval != 0) { 2405 ctl_free_io(io); 2406 break; 2407 } 2408 2409 memcpy((void *)addr, io, sizeof(*io)); 2410 2411 /* return this to our pool */ 2412 ctl_free_io(io); 2413 2414 break; 2415 } 2416 case CTL_ENABLE_PORT: 2417 case CTL_DISABLE_PORT: 2418 case CTL_SET_PORT_WWNS: { 2419 struct ctl_port *port; 2420 struct ctl_port_entry *entry; 2421 2422 entry = (struct ctl_port_entry *)addr; 2423 2424 mtx_lock(&softc->ctl_lock); 2425 STAILQ_FOREACH(port, &softc->port_list, links) { 2426 int action, done; 2427 2428 action = 0; 2429 done = 0; 2430 2431 if ((entry->port_type == CTL_PORT_NONE) 2432 && (entry->targ_port == port->targ_port)) { 2433 /* 2434 * If the user only wants to enable or 2435 * disable or set WWNs on a specific port, 2436 * do the operation and we're done. 2437 */ 2438 action = 1; 2439 done = 1; 2440 } else if (entry->port_type & port->port_type) { 2441 /* 2442 * Compare the user's type mask with the 2443 * particular frontend type to see if we 2444 * have a match. 2445 */ 2446 action = 1; 2447 done = 0; 2448 2449 /* 2450 * Make sure the user isn't trying to set 2451 * WWNs on multiple ports at the same time. 2452 */ 2453 if (cmd == CTL_SET_PORT_WWNS) { 2454 printf("%s: Can't set WWNs on " 2455 "multiple ports\n", __func__); 2456 retval = EINVAL; 2457 break; 2458 } 2459 } 2460 if (action != 0) { 2461 /* 2462 * XXX KDM we have to drop the lock here, 2463 * because the online/offline operations 2464 * can potentially block. We need to 2465 * reference count the frontends so they 2466 * can't go away, 2467 */ 2468 mtx_unlock(&softc->ctl_lock); 2469 2470 if (cmd == CTL_ENABLE_PORT) { 2471 ctl_port_online(port); 2472 } else if (cmd == CTL_DISABLE_PORT) { 2473 ctl_port_offline(port); 2474 } 2475 2476 mtx_lock(&softc->ctl_lock); 2477 2478 if (cmd == CTL_SET_PORT_WWNS) 2479 ctl_port_set_wwns(port, 2480 (entry->flags & CTL_PORT_WWNN_VALID) ? 2481 1 : 0, entry->wwnn, 2482 (entry->flags & CTL_PORT_WWPN_VALID) ? 2483 1 : 0, entry->wwpn); 2484 } 2485 if (done != 0) 2486 break; 2487 } 2488 mtx_unlock(&softc->ctl_lock); 2489 break; 2490 } 2491 case CTL_GET_PORT_LIST: { 2492 struct ctl_port *port; 2493 struct ctl_port_list *list; 2494 int i; 2495 2496 list = (struct ctl_port_list *)addr; 2497 2498 if (list->alloc_len != (list->alloc_num * 2499 sizeof(struct ctl_port_entry))) { 2500 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2501 "alloc_num %u * sizeof(struct ctl_port_entry) " 2502 "%zu\n", __func__, list->alloc_len, 2503 list->alloc_num, sizeof(struct ctl_port_entry)); 2504 retval = EINVAL; 2505 break; 2506 } 2507 list->fill_len = 0; 2508 list->fill_num = 0; 2509 list->dropped_num = 0; 2510 i = 0; 2511 mtx_lock(&softc->ctl_lock); 2512 STAILQ_FOREACH(port, &softc->port_list, links) { 2513 struct ctl_port_entry entry, *list_entry; 2514 2515 if (list->fill_num >= list->alloc_num) { 2516 list->dropped_num++; 2517 continue; 2518 } 2519 2520 entry.port_type = port->port_type; 2521 strlcpy(entry.port_name, port->port_name, 2522 sizeof(entry.port_name)); 2523 entry.targ_port = port->targ_port; 2524 entry.physical_port = port->physical_port; 2525 entry.virtual_port = port->virtual_port; 2526 entry.wwnn = port->wwnn; 2527 entry.wwpn = port->wwpn; 2528 if (port->status & CTL_PORT_STATUS_ONLINE) 2529 entry.online = 1; 2530 else 2531 entry.online = 0; 2532 2533 list_entry = &list->entries[i]; 2534 2535 retval = copyout(&entry, list_entry, sizeof(entry)); 2536 if (retval != 0) { 2537 printf("%s: CTL_GET_PORT_LIST: copyout " 2538 "returned %d\n", __func__, retval); 2539 break; 2540 } 2541 i++; 2542 list->fill_num++; 2543 list->fill_len += sizeof(entry); 2544 } 2545 mtx_unlock(&softc->ctl_lock); 2546 2547 /* 2548 * If this is non-zero, we had a copyout fault, so there's 2549 * probably no point in attempting to set the status inside 2550 * the structure. 2551 */ 2552 if (retval != 0) 2553 break; 2554 2555 if (list->dropped_num > 0) 2556 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2557 else 2558 list->status = CTL_PORT_LIST_OK; 2559 break; 2560 } 2561 case CTL_DUMP_OOA: { 2562 struct ctl_lun *lun; 2563 union ctl_io *io; 2564 char printbuf[128]; 2565 struct sbuf sb; 2566 2567 mtx_lock(&softc->ctl_lock); 2568 printf("Dumping OOA queues:\n"); 2569 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2570 mtx_lock(&lun->lun_lock); 2571 for (io = (union ctl_io *)TAILQ_FIRST( 2572 &lun->ooa_queue); io != NULL; 2573 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2574 ooa_links)) { 2575 sbuf_new(&sb, printbuf, sizeof(printbuf), 2576 SBUF_FIXEDLEN); 2577 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2578 (intmax_t)lun->lun, 2579 io->scsiio.tag_num, 2580 (io->io_hdr.flags & 2581 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2582 (io->io_hdr.flags & 2583 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2584 (io->io_hdr.flags & 2585 CTL_FLAG_ABORT) ? " ABORT" : "", 2586 (io->io_hdr.flags & 2587 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2588 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2589 sbuf_finish(&sb); 2590 printf("%s\n", sbuf_data(&sb)); 2591 } 2592 mtx_unlock(&lun->lun_lock); 2593 } 2594 printf("OOA queues dump done\n"); 2595 mtx_unlock(&softc->ctl_lock); 2596 break; 2597 } 2598 case CTL_GET_OOA: { 2599 struct ctl_lun *lun; 2600 struct ctl_ooa *ooa_hdr; 2601 struct ctl_ooa_entry *entries; 2602 uint32_t cur_fill_num; 2603 2604 ooa_hdr = (struct ctl_ooa *)addr; 2605 2606 if ((ooa_hdr->alloc_len == 0) 2607 || (ooa_hdr->alloc_num == 0)) { 2608 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2609 "must be non-zero\n", __func__, 2610 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2611 retval = EINVAL; 2612 break; 2613 } 2614 2615 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2616 sizeof(struct ctl_ooa_entry))) { 2617 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2618 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2619 __func__, ooa_hdr->alloc_len, 2620 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2621 retval = EINVAL; 2622 break; 2623 } 2624 2625 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2626 if (entries == NULL) { 2627 printf("%s: could not allocate %d bytes for OOA " 2628 "dump\n", __func__, ooa_hdr->alloc_len); 2629 retval = ENOMEM; 2630 break; 2631 } 2632 2633 mtx_lock(&softc->ctl_lock); 2634 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2635 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2636 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2637 mtx_unlock(&softc->ctl_lock); 2638 free(entries, M_CTL); 2639 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2640 __func__, (uintmax_t)ooa_hdr->lun_num); 2641 retval = EINVAL; 2642 break; 2643 } 2644 2645 cur_fill_num = 0; 2646 2647 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2648 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2649 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2650 ooa_hdr, entries); 2651 if (retval != 0) 2652 break; 2653 } 2654 if (retval != 0) { 2655 mtx_unlock(&softc->ctl_lock); 2656 free(entries, M_CTL); 2657 break; 2658 } 2659 } else { 2660 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2661 2662 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2663 entries); 2664 } 2665 mtx_unlock(&softc->ctl_lock); 2666 2667 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2668 ooa_hdr->fill_len = ooa_hdr->fill_num * 2669 sizeof(struct ctl_ooa_entry); 2670 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2671 if (retval != 0) { 2672 printf("%s: error copying out %d bytes for OOA dump\n", 2673 __func__, ooa_hdr->fill_len); 2674 } 2675 2676 getbintime(&ooa_hdr->cur_bt); 2677 2678 if (cur_fill_num > ooa_hdr->alloc_num) { 2679 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2680 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2681 } else { 2682 ooa_hdr->dropped_num = 0; 2683 ooa_hdr->status = CTL_OOA_OK; 2684 } 2685 2686 free(entries, M_CTL); 2687 break; 2688 } 2689 case CTL_CHECK_OOA: { 2690 union ctl_io *io; 2691 struct ctl_lun *lun; 2692 struct ctl_ooa_info *ooa_info; 2693 2694 2695 ooa_info = (struct ctl_ooa_info *)addr; 2696 2697 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2698 ooa_info->status = CTL_OOA_INVALID_LUN; 2699 break; 2700 } 2701 mtx_lock(&softc->ctl_lock); 2702 lun = softc->ctl_luns[ooa_info->lun_id]; 2703 if (lun == NULL) { 2704 mtx_unlock(&softc->ctl_lock); 2705 ooa_info->status = CTL_OOA_INVALID_LUN; 2706 break; 2707 } 2708 mtx_lock(&lun->lun_lock); 2709 mtx_unlock(&softc->ctl_lock); 2710 ooa_info->num_entries = 0; 2711 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2712 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2713 &io->io_hdr, ooa_links)) { 2714 ooa_info->num_entries++; 2715 } 2716 mtx_unlock(&lun->lun_lock); 2717 2718 ooa_info->status = CTL_OOA_SUCCESS; 2719 2720 break; 2721 } 2722 case CTL_HARD_START: 2723 case CTL_HARD_STOP: { 2724 struct ctl_fe_ioctl_startstop_info ss_info; 2725 struct cfi_metatask *metatask; 2726 struct mtx hs_mtx; 2727 2728 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2729 2730 cv_init(&ss_info.sem, "hard start/stop cv" ); 2731 2732 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2733 if (metatask == NULL) { 2734 retval = ENOMEM; 2735 mtx_destroy(&hs_mtx); 2736 break; 2737 } 2738 2739 if (cmd == CTL_HARD_START) 2740 metatask->tasktype = CFI_TASK_STARTUP; 2741 else 2742 metatask->tasktype = CFI_TASK_SHUTDOWN; 2743 2744 metatask->callback = ctl_ioctl_hard_startstop_callback; 2745 metatask->callback_arg = &ss_info; 2746 2747 cfi_action(metatask); 2748 2749 /* Wait for the callback */ 2750 mtx_lock(&hs_mtx); 2751 cv_wait_sig(&ss_info.sem, &hs_mtx); 2752 mtx_unlock(&hs_mtx); 2753 2754 /* 2755 * All information has been copied from the metatask by the 2756 * time cv_broadcast() is called, so we free the metatask here. 2757 */ 2758 cfi_free_metatask(metatask); 2759 2760 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2761 2762 mtx_destroy(&hs_mtx); 2763 break; 2764 } 2765 case CTL_BBRREAD: { 2766 struct ctl_bbrread_info *bbr_info; 2767 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2768 struct mtx bbr_mtx; 2769 struct cfi_metatask *metatask; 2770 2771 bbr_info = (struct ctl_bbrread_info *)addr; 2772 2773 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2774 2775 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2776 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2777 2778 fe_bbr_info.bbr_info = bbr_info; 2779 fe_bbr_info.lock = &bbr_mtx; 2780 2781 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2782 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2783 2784 if (metatask == NULL) { 2785 mtx_destroy(&bbr_mtx); 2786 cv_destroy(&fe_bbr_info.sem); 2787 retval = ENOMEM; 2788 break; 2789 } 2790 metatask->tasktype = CFI_TASK_BBRREAD; 2791 metatask->callback = ctl_ioctl_bbrread_callback; 2792 metatask->callback_arg = &fe_bbr_info; 2793 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2794 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2795 metatask->taskinfo.bbrread.len = bbr_info->len; 2796 2797 cfi_action(metatask); 2798 2799 mtx_lock(&bbr_mtx); 2800 while (fe_bbr_info.wakeup_done == 0) 2801 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2802 mtx_unlock(&bbr_mtx); 2803 2804 bbr_info->status = metatask->status; 2805 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2806 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2807 memcpy(&bbr_info->sense_data, 2808 &metatask->taskinfo.bbrread.sense_data, 2809 MIN(sizeof(bbr_info->sense_data), 2810 sizeof(metatask->taskinfo.bbrread.sense_data))); 2811 2812 cfi_free_metatask(metatask); 2813 2814 mtx_destroy(&bbr_mtx); 2815 cv_destroy(&fe_bbr_info.sem); 2816 2817 break; 2818 } 2819 case CTL_DELAY_IO: { 2820 struct ctl_io_delay_info *delay_info; 2821#ifdef CTL_IO_DELAY 2822 struct ctl_lun *lun; 2823#endif /* CTL_IO_DELAY */ 2824 2825 delay_info = (struct ctl_io_delay_info *)addr; 2826 2827#ifdef CTL_IO_DELAY 2828 mtx_lock(&softc->ctl_lock); 2829 2830 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2831 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2832 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2833 } else { 2834 lun = softc->ctl_luns[delay_info->lun_id]; 2835 mtx_lock(&lun->lun_lock); 2836 2837 delay_info->status = CTL_DELAY_STATUS_OK; 2838 2839 switch (delay_info->delay_type) { 2840 case CTL_DELAY_TYPE_CONT: 2841 break; 2842 case CTL_DELAY_TYPE_ONESHOT: 2843 break; 2844 default: 2845 delay_info->status = 2846 CTL_DELAY_STATUS_INVALID_TYPE; 2847 break; 2848 } 2849 2850 switch (delay_info->delay_loc) { 2851 case CTL_DELAY_LOC_DATAMOVE: 2852 lun->delay_info.datamove_type = 2853 delay_info->delay_type; 2854 lun->delay_info.datamove_delay = 2855 delay_info->delay_secs; 2856 break; 2857 case CTL_DELAY_LOC_DONE: 2858 lun->delay_info.done_type = 2859 delay_info->delay_type; 2860 lun->delay_info.done_delay = 2861 delay_info->delay_secs; 2862 break; 2863 default: 2864 delay_info->status = 2865 CTL_DELAY_STATUS_INVALID_LOC; 2866 break; 2867 } 2868 mtx_unlock(&lun->lun_lock); 2869 } 2870 2871 mtx_unlock(&softc->ctl_lock); 2872#else 2873 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2874#endif /* CTL_IO_DELAY */ 2875 break; 2876 } 2877 case CTL_REALSYNC_SET: { 2878 int *syncstate; 2879 2880 syncstate = (int *)addr; 2881 2882 mtx_lock(&softc->ctl_lock); 2883 switch (*syncstate) { 2884 case 0: 2885 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2886 break; 2887 case 1: 2888 softc->flags |= CTL_FLAG_REAL_SYNC; 2889 break; 2890 default: 2891 retval = EINVAL; 2892 break; 2893 } 2894 mtx_unlock(&softc->ctl_lock); 2895 break; 2896 } 2897 case CTL_REALSYNC_GET: { 2898 int *syncstate; 2899 2900 syncstate = (int*)addr; 2901 2902 mtx_lock(&softc->ctl_lock); 2903 if (softc->flags & CTL_FLAG_REAL_SYNC) 2904 *syncstate = 1; 2905 else 2906 *syncstate = 0; 2907 mtx_unlock(&softc->ctl_lock); 2908 2909 break; 2910 } 2911 case CTL_SETSYNC: 2912 case CTL_GETSYNC: { 2913 struct ctl_sync_info *sync_info; 2914 struct ctl_lun *lun; 2915 2916 sync_info = (struct ctl_sync_info *)addr; 2917 2918 mtx_lock(&softc->ctl_lock); 2919 lun = softc->ctl_luns[sync_info->lun_id]; 2920 if (lun == NULL) { 2921 mtx_unlock(&softc->ctl_lock); 2922 sync_info->status = CTL_GS_SYNC_NO_LUN; 2923 } 2924 /* 2925 * Get or set the sync interval. We're not bounds checking 2926 * in the set case, hopefully the user won't do something 2927 * silly. 2928 */ 2929 mtx_lock(&lun->lun_lock); 2930 mtx_unlock(&softc->ctl_lock); 2931 if (cmd == CTL_GETSYNC) 2932 sync_info->sync_interval = lun->sync_interval; 2933 else 2934 lun->sync_interval = sync_info->sync_interval; 2935 mtx_unlock(&lun->lun_lock); 2936 2937 sync_info->status = CTL_GS_SYNC_OK; 2938 2939 break; 2940 } 2941 case CTL_GETSTATS: { 2942 struct ctl_stats *stats; 2943 struct ctl_lun *lun; 2944 int i; 2945 2946 stats = (struct ctl_stats *)addr; 2947 2948 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2949 stats->alloc_len) { 2950 stats->status = CTL_SS_NEED_MORE_SPACE; 2951 stats->num_luns = softc->num_luns; 2952 break; 2953 } 2954 /* 2955 * XXX KDM no locking here. If the LUN list changes, 2956 * things can blow up. 2957 */ 2958 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2959 i++, lun = STAILQ_NEXT(lun, links)) { 2960 retval = copyout(&lun->stats, &stats->lun_stats[i], 2961 sizeof(lun->stats)); 2962 if (retval != 0) 2963 break; 2964 } 2965 stats->num_luns = softc->num_luns; 2966 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2967 softc->num_luns; 2968 stats->status = CTL_SS_OK; 2969#ifdef CTL_TIME_IO 2970 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2971#else 2972 stats->flags = CTL_STATS_FLAG_NONE; 2973#endif 2974 getnanouptime(&stats->timestamp); 2975 break; 2976 } 2977 case CTL_ERROR_INJECT: { 2978 struct ctl_error_desc *err_desc, *new_err_desc; 2979 struct ctl_lun *lun; 2980 2981 err_desc = (struct ctl_error_desc *)addr; 2982 2983 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2984 M_WAITOK | M_ZERO); 2985 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2986 2987 mtx_lock(&softc->ctl_lock); 2988 lun = softc->ctl_luns[err_desc->lun_id]; 2989 if (lun == NULL) { 2990 mtx_unlock(&softc->ctl_lock); 2991 free(new_err_desc, M_CTL); 2992 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2993 __func__, (uintmax_t)err_desc->lun_id); 2994 retval = EINVAL; 2995 break; 2996 } 2997 mtx_lock(&lun->lun_lock); 2998 mtx_unlock(&softc->ctl_lock); 2999 3000 /* 3001 * We could do some checking here to verify the validity 3002 * of the request, but given the complexity of error 3003 * injection requests, the checking logic would be fairly 3004 * complex. 3005 * 3006 * For now, if the request is invalid, it just won't get 3007 * executed and might get deleted. 3008 */ 3009 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3010 3011 /* 3012 * XXX KDM check to make sure the serial number is unique, 3013 * in case we somehow manage to wrap. That shouldn't 3014 * happen for a very long time, but it's the right thing to 3015 * do. 3016 */ 3017 new_err_desc->serial = lun->error_serial; 3018 err_desc->serial = lun->error_serial; 3019 lun->error_serial++; 3020 3021 mtx_unlock(&lun->lun_lock); 3022 break; 3023 } 3024 case CTL_ERROR_INJECT_DELETE: { 3025 struct ctl_error_desc *delete_desc, *desc, *desc2; 3026 struct ctl_lun *lun; 3027 int delete_done; 3028 3029 delete_desc = (struct ctl_error_desc *)addr; 3030 delete_done = 0; 3031 3032 mtx_lock(&softc->ctl_lock); 3033 lun = softc->ctl_luns[delete_desc->lun_id]; 3034 if (lun == NULL) { 3035 mtx_unlock(&softc->ctl_lock); 3036 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3037 __func__, (uintmax_t)delete_desc->lun_id); 3038 retval = EINVAL; 3039 break; 3040 } 3041 mtx_lock(&lun->lun_lock); 3042 mtx_unlock(&softc->ctl_lock); 3043 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3044 if (desc->serial != delete_desc->serial) 3045 continue; 3046 3047 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3048 links); 3049 free(desc, M_CTL); 3050 delete_done = 1; 3051 } 3052 mtx_unlock(&lun->lun_lock); 3053 if (delete_done == 0) { 3054 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3055 "error serial %ju on LUN %u\n", __func__, 3056 delete_desc->serial, delete_desc->lun_id); 3057 retval = EINVAL; 3058 break; 3059 } 3060 break; 3061 } 3062 case CTL_DUMP_STRUCTS: { 3063 int i, j, k; 3064 struct ctl_port *port; 3065 struct ctl_frontend *fe; 3066 3067 mtx_lock(&softc->ctl_lock); 3068 printf("CTL Persistent Reservation information start:\n"); 3069 for (i = 0; i < CTL_MAX_LUNS; i++) { 3070 struct ctl_lun *lun; 3071 3072 lun = softc->ctl_luns[i]; 3073 3074 if ((lun == NULL) 3075 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3076 continue; 3077 3078 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3079 if (lun->pr_keys[j] == NULL) 3080 continue; 3081 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3082 if (lun->pr_keys[j][k] == 0) 3083 continue; 3084 printf(" LUN %d port %d iid %d key " 3085 "%#jx\n", i, j, k, 3086 (uintmax_t)lun->pr_keys[j][k]); 3087 } 3088 } 3089 } 3090 printf("CTL Persistent Reservation information end\n"); 3091 printf("CTL Ports:\n"); 3092 STAILQ_FOREACH(port, &softc->port_list, links) { 3093 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3094 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3095 port->frontend->name, port->port_type, 3096 port->physical_port, port->virtual_port, 3097 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3098 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3099 if (port->wwpn_iid[j].in_use == 0 && 3100 port->wwpn_iid[j].wwpn == 0 && 3101 port->wwpn_iid[j].name == NULL) 3102 continue; 3103 3104 printf(" iid %u use %d WWPN %#jx '%s'\n", 3105 j, port->wwpn_iid[j].in_use, 3106 (uintmax_t)port->wwpn_iid[j].wwpn, 3107 port->wwpn_iid[j].name); 3108 } 3109 } 3110 printf("CTL Port information end\n"); 3111 mtx_unlock(&softc->ctl_lock); 3112 /* 3113 * XXX KDM calling this without a lock. We'd likely want 3114 * to drop the lock before calling the frontend's dump 3115 * routine anyway. 3116 */ 3117 printf("CTL Frontends:\n"); 3118 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3119 printf(" Frontend '%s'\n", fe->name); 3120 if (fe->fe_dump != NULL) 3121 fe->fe_dump(); 3122 } 3123 printf("CTL Frontend information end\n"); 3124 break; 3125 } 3126 case CTL_LUN_REQ: { 3127 struct ctl_lun_req *lun_req; 3128 struct ctl_backend_driver *backend; 3129 3130 lun_req = (struct ctl_lun_req *)addr; 3131 3132 backend = ctl_backend_find(lun_req->backend); 3133 if (backend == NULL) { 3134 lun_req->status = CTL_LUN_ERROR; 3135 snprintf(lun_req->error_str, 3136 sizeof(lun_req->error_str), 3137 "Backend \"%s\" not found.", 3138 lun_req->backend); 3139 break; 3140 } 3141 if (lun_req->num_be_args > 0) { 3142 lun_req->kern_be_args = ctl_copyin_args( 3143 lun_req->num_be_args, 3144 lun_req->be_args, 3145 lun_req->error_str, 3146 sizeof(lun_req->error_str)); 3147 if (lun_req->kern_be_args == NULL) { 3148 lun_req->status = CTL_LUN_ERROR; 3149 break; 3150 } 3151 } 3152 3153 retval = backend->ioctl(dev, cmd, addr, flag, td); 3154 3155 if (lun_req->num_be_args > 0) { 3156 ctl_copyout_args(lun_req->num_be_args, 3157 lun_req->kern_be_args); 3158 ctl_free_args(lun_req->num_be_args, 3159 lun_req->kern_be_args); 3160 } 3161 break; 3162 } 3163 case CTL_LUN_LIST: { 3164 struct sbuf *sb; 3165 struct ctl_lun *lun; 3166 struct ctl_lun_list *list; 3167 struct ctl_option *opt; 3168 3169 list = (struct ctl_lun_list *)addr; 3170 3171 /* 3172 * Allocate a fixed length sbuf here, based on the length 3173 * of the user's buffer. We could allocate an auto-extending 3174 * buffer, and then tell the user how much larger our 3175 * amount of data is than his buffer, but that presents 3176 * some problems: 3177 * 3178 * 1. The sbuf(9) routines use a blocking malloc, and so 3179 * we can't hold a lock while calling them with an 3180 * auto-extending buffer. 3181 * 3182 * 2. There is not currently a LUN reference counting 3183 * mechanism, outside of outstanding transactions on 3184 * the LUN's OOA queue. So a LUN could go away on us 3185 * while we're getting the LUN number, backend-specific 3186 * information, etc. Thus, given the way things 3187 * currently work, we need to hold the CTL lock while 3188 * grabbing LUN information. 3189 * 3190 * So, from the user's standpoint, the best thing to do is 3191 * allocate what he thinks is a reasonable buffer length, 3192 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3193 * double the buffer length and try again. (And repeat 3194 * that until he succeeds.) 3195 */ 3196 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3197 if (sb == NULL) { 3198 list->status = CTL_LUN_LIST_ERROR; 3199 snprintf(list->error_str, sizeof(list->error_str), 3200 "Unable to allocate %d bytes for LUN list", 3201 list->alloc_len); 3202 break; 3203 } 3204 3205 sbuf_printf(sb, "<ctllunlist>\n"); 3206 3207 mtx_lock(&softc->ctl_lock); 3208 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3209 mtx_lock(&lun->lun_lock); 3210 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3211 (uintmax_t)lun->lun); 3212 3213 /* 3214 * Bail out as soon as we see that we've overfilled 3215 * the buffer. 3216 */ 3217 if (retval != 0) 3218 break; 3219 3220 retval = sbuf_printf(sb, "\t<backend_type>%s" 3221 "</backend_type>\n", 3222 (lun->backend == NULL) ? "none" : 3223 lun->backend->name); 3224 3225 if (retval != 0) 3226 break; 3227 3228 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3229 lun->be_lun->lun_type); 3230 3231 if (retval != 0) 3232 break; 3233 3234 if (lun->backend == NULL) { 3235 retval = sbuf_printf(sb, "</lun>\n"); 3236 if (retval != 0) 3237 break; 3238 continue; 3239 } 3240 3241 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3242 (lun->be_lun->maxlba > 0) ? 3243 lun->be_lun->maxlba + 1 : 0); 3244 3245 if (retval != 0) 3246 break; 3247 3248 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3249 lun->be_lun->blocksize); 3250 3251 if (retval != 0) 3252 break; 3253 3254 retval = sbuf_printf(sb, "\t<serial_number>"); 3255 3256 if (retval != 0) 3257 break; 3258 3259 retval = ctl_sbuf_printf_esc(sb, 3260 lun->be_lun->serial_num, 3261 sizeof(lun->be_lun->serial_num)); 3262 3263 if (retval != 0) 3264 break; 3265 3266 retval = sbuf_printf(sb, "</serial_number>\n"); 3267 3268 if (retval != 0) 3269 break; 3270 3271 retval = sbuf_printf(sb, "\t<device_id>"); 3272 3273 if (retval != 0) 3274 break; 3275 3276 retval = ctl_sbuf_printf_esc(sb, 3277 lun->be_lun->device_id, 3278 sizeof(lun->be_lun->device_id)); 3279 3280 if (retval != 0) 3281 break; 3282 3283 retval = sbuf_printf(sb, "</device_id>\n"); 3284 3285 if (retval != 0) 3286 break; 3287 3288 if (lun->backend->lun_info != NULL) { 3289 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3290 if (retval != 0) 3291 break; 3292 } 3293 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3294 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3295 opt->name, opt->value, opt->name); 3296 if (retval != 0) 3297 break; 3298 } 3299 3300 retval = sbuf_printf(sb, "</lun>\n"); 3301 3302 if (retval != 0) 3303 break; 3304 mtx_unlock(&lun->lun_lock); 3305 } 3306 if (lun != NULL) 3307 mtx_unlock(&lun->lun_lock); 3308 mtx_unlock(&softc->ctl_lock); 3309 3310 if ((retval != 0) 3311 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3312 retval = 0; 3313 sbuf_delete(sb); 3314 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3315 snprintf(list->error_str, sizeof(list->error_str), 3316 "Out of space, %d bytes is too small", 3317 list->alloc_len); 3318 break; 3319 } 3320 3321 sbuf_finish(sb); 3322 3323 retval = copyout(sbuf_data(sb), list->lun_xml, 3324 sbuf_len(sb) + 1); 3325 3326 list->fill_len = sbuf_len(sb) + 1; 3327 list->status = CTL_LUN_LIST_OK; 3328 sbuf_delete(sb); 3329 break; 3330 } 3331 case CTL_ISCSI: { 3332 struct ctl_iscsi *ci; 3333 struct ctl_frontend *fe; 3334 3335 ci = (struct ctl_iscsi *)addr; 3336 3337 fe = ctl_frontend_find("iscsi"); 3338 if (fe == NULL) { 3339 ci->status = CTL_ISCSI_ERROR; 3340 snprintf(ci->error_str, sizeof(ci->error_str), 3341 "Frontend \"iscsi\" not found."); 3342 break; 3343 } 3344 3345 retval = fe->ioctl(dev, cmd, addr, flag, td); 3346 break; 3347 } 3348 case CTL_PORT_REQ: { 3349 struct ctl_req *req; 3350 struct ctl_frontend *fe; 3351 3352 req = (struct ctl_req *)addr; 3353 3354 fe = ctl_frontend_find(req->driver); 3355 if (fe == NULL) { 3356 req->status = CTL_LUN_ERROR; 3357 snprintf(req->error_str, sizeof(req->error_str), 3358 "Frontend \"%s\" not found.", req->driver); 3359 break; 3360 } 3361 if (req->num_args > 0) { 3362 req->kern_args = ctl_copyin_args(req->num_args, 3363 req->args, req->error_str, sizeof(req->error_str)); 3364 if (req->kern_args == NULL) { 3365 req->status = CTL_LUN_ERROR; 3366 break; 3367 } 3368 } 3369 3370 retval = fe->ioctl(dev, cmd, addr, flag, td); 3371 3372 if (req->num_args > 0) { 3373 ctl_copyout_args(req->num_args, req->kern_args); 3374 ctl_free_args(req->num_args, req->kern_args); 3375 } 3376 break; 3377 } 3378 case CTL_PORT_LIST: { 3379 struct sbuf *sb; 3380 struct ctl_port *port; 3381 struct ctl_lun_list *list; 3382 struct ctl_option *opt; 3383 int j; 3384 uint32_t plun; 3385 3386 list = (struct ctl_lun_list *)addr; 3387 3388 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3389 if (sb == NULL) { 3390 list->status = CTL_LUN_LIST_ERROR; 3391 snprintf(list->error_str, sizeof(list->error_str), 3392 "Unable to allocate %d bytes for LUN list", 3393 list->alloc_len); 3394 break; 3395 } 3396 3397 sbuf_printf(sb, "<ctlportlist>\n"); 3398 3399 mtx_lock(&softc->ctl_lock); 3400 STAILQ_FOREACH(port, &softc->port_list, links) { 3401 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3402 (uintmax_t)port->targ_port); 3403 3404 /* 3405 * Bail out as soon as we see that we've overfilled 3406 * the buffer. 3407 */ 3408 if (retval != 0) 3409 break; 3410 3411 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3412 "</frontend_type>\n", port->frontend->name); 3413 if (retval != 0) 3414 break; 3415 3416 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3417 port->port_type); 3418 if (retval != 0) 3419 break; 3420 3421 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3422 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3423 if (retval != 0) 3424 break; 3425 3426 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3427 port->port_name); 3428 if (retval != 0) 3429 break; 3430 3431 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3432 port->physical_port); 3433 if (retval != 0) 3434 break; 3435 3436 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3437 port->virtual_port); 3438 if (retval != 0) 3439 break; 3440 3441 if (port->target_devid != NULL) { 3442 sbuf_printf(sb, "\t<target>"); 3443 ctl_id_sbuf(port->target_devid, sb); 3444 sbuf_printf(sb, "</target>\n"); 3445 } 3446 3447 if (port->port_devid != NULL) { 3448 sbuf_printf(sb, "\t<port>"); 3449 ctl_id_sbuf(port->port_devid, sb); 3450 sbuf_printf(sb, "</port>\n"); 3451 } 3452 3453 if (port->port_info != NULL) { 3454 retval = port->port_info(port->onoff_arg, sb); 3455 if (retval != 0) 3456 break; 3457 } 3458 STAILQ_FOREACH(opt, &port->options, links) { 3459 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3460 opt->name, opt->value, opt->name); 3461 if (retval != 0) 3462 break; 3463 } 3464 3465 if (port->lun_map != NULL) { 3466 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3467 for (j = 0; j < CTL_MAX_LUNS; j++) { 3468 plun = ctl_lun_map_from_port(port, j); 3469 if (plun >= CTL_MAX_LUNS) 3470 continue; 3471 sbuf_printf(sb, 3472 "\t<lun id=\"%u\">%u</lun>\n", 3473 j, plun); 3474 } 3475 } 3476 3477 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3478 if (port->wwpn_iid[j].in_use == 0 || 3479 (port->wwpn_iid[j].wwpn == 0 && 3480 port->wwpn_iid[j].name == NULL)) 3481 continue; 3482 3483 if (port->wwpn_iid[j].name != NULL) 3484 retval = sbuf_printf(sb, 3485 "\t<initiator id=\"%u\">%s</initiator>\n", 3486 j, port->wwpn_iid[j].name); 3487 else 3488 retval = sbuf_printf(sb, 3489 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3490 j, port->wwpn_iid[j].wwpn); 3491 if (retval != 0) 3492 break; 3493 } 3494 if (retval != 0) 3495 break; 3496 3497 retval = sbuf_printf(sb, "</targ_port>\n"); 3498 if (retval != 0) 3499 break; 3500 } 3501 mtx_unlock(&softc->ctl_lock); 3502 3503 if ((retval != 0) 3504 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3505 retval = 0; 3506 sbuf_delete(sb); 3507 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3508 snprintf(list->error_str, sizeof(list->error_str), 3509 "Out of space, %d bytes is too small", 3510 list->alloc_len); 3511 break; 3512 } 3513 3514 sbuf_finish(sb); 3515 3516 retval = copyout(sbuf_data(sb), list->lun_xml, 3517 sbuf_len(sb) + 1); 3518 3519 list->fill_len = sbuf_len(sb) + 1; 3520 list->status = CTL_LUN_LIST_OK; 3521 sbuf_delete(sb); 3522 break; 3523 } 3524 case CTL_LUN_MAP: { 3525 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3526 struct ctl_port *port; 3527 3528 mtx_lock(&softc->ctl_lock); 3529 if (lm->port >= CTL_MAX_PORTS || 3530 (port = softc->ctl_ports[lm->port]) == NULL) { 3531 mtx_unlock(&softc->ctl_lock); 3532 return (ENXIO); 3533 } 3534 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3535 if (lm->plun < CTL_MAX_LUNS) { 3536 if (lm->lun == UINT32_MAX) 3537 retval = ctl_lun_map_unset(port, lm->plun); 3538 else if (lm->lun < CTL_MAX_LUNS && 3539 softc->ctl_luns[lm->lun] != NULL) 3540 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3541 else 3542 return (ENXIO); 3543 } else if (lm->plun == UINT32_MAX) { 3544 if (lm->lun == UINT32_MAX) 3545 retval = ctl_lun_map_deinit(port); 3546 else 3547 retval = ctl_lun_map_init(port); 3548 } else 3549 return (ENXIO); 3550 break; 3551 } 3552 default: { 3553 /* XXX KDM should we fix this? */ 3554#if 0 3555 struct ctl_backend_driver *backend; 3556 unsigned int type; 3557 int found; 3558 3559 found = 0; 3560 3561 /* 3562 * We encode the backend type as the ioctl type for backend 3563 * ioctls. So parse it out here, and then search for a 3564 * backend of this type. 3565 */ 3566 type = _IOC_TYPE(cmd); 3567 3568 STAILQ_FOREACH(backend, &softc->be_list, links) { 3569 if (backend->type == type) { 3570 found = 1; 3571 break; 3572 } 3573 } 3574 if (found == 0) { 3575 printf("ctl: unknown ioctl command %#lx or backend " 3576 "%d\n", cmd, type); 3577 retval = EINVAL; 3578 break; 3579 } 3580 retval = backend->ioctl(dev, cmd, addr, flag, td); 3581#endif 3582 retval = ENOTTY; 3583 break; 3584 } 3585 } 3586 return (retval); 3587} 3588 3589uint32_t 3590ctl_get_initindex(struct ctl_nexus *nexus) 3591{ 3592 if (nexus->targ_port < CTL_MAX_PORTS) 3593 return (nexus->initid.id + 3594 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3595 else 3596 return (nexus->initid.id + 3597 ((nexus->targ_port - CTL_MAX_PORTS) * 3598 CTL_MAX_INIT_PER_PORT)); 3599} 3600 3601uint32_t 3602ctl_get_resindex(struct ctl_nexus *nexus) 3603{ 3604 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3605} 3606 3607uint32_t 3608ctl_port_idx(int port_num) 3609{ 3610 if (port_num < CTL_MAX_PORTS) 3611 return(port_num); 3612 else 3613 return(port_num - CTL_MAX_PORTS); 3614} 3615 3616int 3617ctl_lun_map_init(struct ctl_port *port) 3618{ 3619 struct ctl_softc *softc = control_softc; 3620 struct ctl_lun *lun; 3621 uint32_t i; 3622 3623 if (port->lun_map == NULL) 3624 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3625 M_CTL, M_NOWAIT); 3626 if (port->lun_map == NULL) 3627 return (ENOMEM); 3628 for (i = 0; i < CTL_MAX_LUNS; i++) 3629 port->lun_map[i] = UINT32_MAX; 3630 if (port->status & CTL_PORT_STATUS_ONLINE) { 3631 STAILQ_FOREACH(lun, &softc->lun_list, links) 3632 port->lun_disable(port->targ_lun_arg, lun->lun); 3633 } 3634 return (0); 3635} 3636 3637int 3638ctl_lun_map_deinit(struct ctl_port *port) 3639{ 3640 struct ctl_softc *softc = control_softc; 3641 struct ctl_lun *lun; 3642 3643 if (port->lun_map == NULL) 3644 return (0); 3645 free(port->lun_map, M_CTL); 3646 port->lun_map = NULL; 3647 if (port->status & CTL_PORT_STATUS_ONLINE) { 3648 STAILQ_FOREACH(lun, &softc->lun_list, links) 3649 port->lun_enable(port->targ_lun_arg, lun->lun); 3650 } 3651 return (0); 3652} 3653 3654int 3655ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3656{ 3657 int status; 3658 uint32_t old; 3659 3660 if (port->lun_map == NULL) { 3661 status = ctl_lun_map_init(port); 3662 if (status != 0) 3663 return (status); 3664 } 3665 old = port->lun_map[plun]; 3666 port->lun_map[plun] = glun; 3667 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) 3668 port->lun_enable(port->targ_lun_arg, plun); 3669 return (0); 3670} 3671 3672int 3673ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3674{ 3675 uint32_t old; 3676 3677 if (port->lun_map == NULL) 3678 return (0); 3679 old = port->lun_map[plun]; 3680 port->lun_map[plun] = UINT32_MAX; 3681 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) 3682 port->lun_disable(port->targ_lun_arg, plun); 3683 return (0); 3684} 3685 3686uint32_t 3687ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3688{ 3689 3690 if (port == NULL) 3691 return (UINT32_MAX); 3692 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3693 return (lun_id); 3694 return (port->lun_map[lun_id]); 3695} 3696 3697uint32_t 3698ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3699{ 3700 uint32_t i; 3701 3702 if (port == NULL) 3703 return (UINT32_MAX); 3704 if (port->lun_map == NULL) 3705 return (lun_id); 3706 for (i = 0; i < CTL_MAX_LUNS; i++) { 3707 if (port->lun_map[i] == lun_id) 3708 return (i); 3709 } 3710 return (UINT32_MAX); 3711} 3712 3713static struct ctl_port * 3714ctl_io_port(struct ctl_io_hdr *io_hdr) 3715{ 3716 int port_num; 3717 3718 port_num = io_hdr->nexus.targ_port; 3719 return (control_softc->ctl_ports[ctl_port_idx(port_num)]); 3720} 3721 3722/* 3723 * Note: This only works for bitmask sizes that are at least 32 bits, and 3724 * that are a power of 2. 3725 */ 3726int 3727ctl_ffz(uint32_t *mask, uint32_t size) 3728{ 3729 uint32_t num_chunks, num_pieces; 3730 int i, j; 3731 3732 num_chunks = (size >> 5); 3733 if (num_chunks == 0) 3734 num_chunks++; 3735 num_pieces = MIN((sizeof(uint32_t) * 8), size); 3736 3737 for (i = 0; i < num_chunks; i++) { 3738 for (j = 0; j < num_pieces; j++) { 3739 if ((mask[i] & (1 << j)) == 0) 3740 return ((i << 5) + j); 3741 } 3742 } 3743 3744 return (-1); 3745} 3746 3747int 3748ctl_set_mask(uint32_t *mask, uint32_t bit) 3749{ 3750 uint32_t chunk, piece; 3751 3752 chunk = bit >> 5; 3753 piece = bit % (sizeof(uint32_t) * 8); 3754 3755 if ((mask[chunk] & (1 << piece)) != 0) 3756 return (-1); 3757 else 3758 mask[chunk] |= (1 << piece); 3759 3760 return (0); 3761} 3762 3763int 3764ctl_clear_mask(uint32_t *mask, uint32_t bit) 3765{ 3766 uint32_t chunk, piece; 3767 3768 chunk = bit >> 5; 3769 piece = bit % (sizeof(uint32_t) * 8); 3770 3771 if ((mask[chunk] & (1 << piece)) == 0) 3772 return (-1); 3773 else 3774 mask[chunk] &= ~(1 << piece); 3775 3776 return (0); 3777} 3778 3779int 3780ctl_is_set(uint32_t *mask, uint32_t bit) 3781{ 3782 uint32_t chunk, piece; 3783 3784 chunk = bit >> 5; 3785 piece = bit % (sizeof(uint32_t) * 8); 3786 3787 if ((mask[chunk] & (1 << piece)) == 0) 3788 return (0); 3789 else 3790 return (1); 3791} 3792 3793static uint64_t 3794ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3795{ 3796 uint64_t *t; 3797 3798 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3799 if (t == NULL) 3800 return (0); 3801 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3802} 3803 3804static void 3805ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3806{ 3807 uint64_t *t; 3808 3809 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3810 if (t == NULL) 3811 return; 3812 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3813} 3814 3815static void 3816ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3817{ 3818 uint64_t *p; 3819 u_int i; 3820 3821 i = residx/CTL_MAX_INIT_PER_PORT; 3822 if (lun->pr_keys[i] != NULL) 3823 return; 3824 mtx_unlock(&lun->lun_lock); 3825 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3826 M_WAITOK | M_ZERO); 3827 mtx_lock(&lun->lun_lock); 3828 if (lun->pr_keys[i] == NULL) 3829 lun->pr_keys[i] = p; 3830 else 3831 free(p, M_CTL); 3832} 3833 3834static void 3835ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3836{ 3837 uint64_t *t; 3838 3839 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3840 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3841 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3842} 3843 3844/* 3845 * ctl_softc, pool_name, total_ctl_io are passed in. 3846 * npool is passed out. 3847 */ 3848int 3849ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3850 uint32_t total_ctl_io, void **npool) 3851{ 3852#ifdef IO_POOLS 3853 struct ctl_io_pool *pool; 3854 3855 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3856 M_NOWAIT | M_ZERO); 3857 if (pool == NULL) 3858 return (ENOMEM); 3859 3860 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3861 pool->ctl_softc = ctl_softc; 3862 pool->zone = uma_zsecond_create(pool->name, NULL, 3863 NULL, NULL, NULL, ctl_softc->io_zone); 3864 /* uma_prealloc(pool->zone, total_ctl_io); */ 3865 3866 *npool = pool; 3867#else 3868 *npool = ctl_softc->io_zone; 3869#endif 3870 return (0); 3871} 3872 3873void 3874ctl_pool_free(struct ctl_io_pool *pool) 3875{ 3876 3877 if (pool == NULL) 3878 return; 3879 3880#ifdef IO_POOLS 3881 uma_zdestroy(pool->zone); 3882 free(pool, M_CTL); 3883#endif 3884} 3885 3886union ctl_io * 3887ctl_alloc_io(void *pool_ref) 3888{ 3889 union ctl_io *io; 3890#ifdef IO_POOLS 3891 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3892 3893 io = uma_zalloc(pool->zone, M_WAITOK); 3894#else 3895 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3896#endif 3897 if (io != NULL) 3898 io->io_hdr.pool = pool_ref; 3899 return (io); 3900} 3901 3902union ctl_io * 3903ctl_alloc_io_nowait(void *pool_ref) 3904{ 3905 union ctl_io *io; 3906#ifdef IO_POOLS 3907 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3908 3909 io = uma_zalloc(pool->zone, M_NOWAIT); 3910#else 3911 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3912#endif 3913 if (io != NULL) 3914 io->io_hdr.pool = pool_ref; 3915 return (io); 3916} 3917 3918void 3919ctl_free_io(union ctl_io *io) 3920{ 3921#ifdef IO_POOLS 3922 struct ctl_io_pool *pool; 3923#endif 3924 3925 if (io == NULL) 3926 return; 3927 3928#ifdef IO_POOLS 3929 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3930 uma_zfree(pool->zone, io); 3931#else 3932 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3933#endif 3934} 3935 3936void 3937ctl_zero_io(union ctl_io *io) 3938{ 3939 void *pool_ref; 3940 3941 if (io == NULL) 3942 return; 3943 3944 /* 3945 * May need to preserve linked list pointers at some point too. 3946 */ 3947 pool_ref = io->io_hdr.pool; 3948 memset(io, 0, sizeof(*io)); 3949 io->io_hdr.pool = pool_ref; 3950} 3951 3952/* 3953 * This routine is currently used for internal copies of ctl_ios that need 3954 * to persist for some reason after we've already returned status to the 3955 * FETD. (Thus the flag set.) 3956 * 3957 * XXX XXX 3958 * Note that this makes a blind copy of all fields in the ctl_io, except 3959 * for the pool reference. This includes any memory that has been 3960 * allocated! That memory will no longer be valid after done has been 3961 * called, so this would be VERY DANGEROUS for command that actually does 3962 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3963 * start and stop commands, which don't transfer any data, so this is not a 3964 * problem. If it is used for anything else, the caller would also need to 3965 * allocate data buffer space and this routine would need to be modified to 3966 * copy the data buffer(s) as well. 3967 */ 3968void 3969ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3970{ 3971 void *pool_ref; 3972 3973 if ((src == NULL) 3974 || (dest == NULL)) 3975 return; 3976 3977 /* 3978 * May need to preserve linked list pointers at some point too. 3979 */ 3980 pool_ref = dest->io_hdr.pool; 3981 3982 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3983 3984 dest->io_hdr.pool = pool_ref; 3985 /* 3986 * We need to know that this is an internal copy, and doesn't need 3987 * to get passed back to the FETD that allocated it. 3988 */ 3989 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3990} 3991 3992int 3993ctl_expand_number(const char *buf, uint64_t *num) 3994{ 3995 char *endptr; 3996 uint64_t number; 3997 unsigned shift; 3998 3999 number = strtoq(buf, &endptr, 0); 4000 4001 switch (tolower((unsigned char)*endptr)) { 4002 case 'e': 4003 shift = 60; 4004 break; 4005 case 'p': 4006 shift = 50; 4007 break; 4008 case 't': 4009 shift = 40; 4010 break; 4011 case 'g': 4012 shift = 30; 4013 break; 4014 case 'm': 4015 shift = 20; 4016 break; 4017 case 'k': 4018 shift = 10; 4019 break; 4020 case 'b': 4021 case '\0': /* No unit. */ 4022 *num = number; 4023 return (0); 4024 default: 4025 /* Unrecognized unit. */ 4026 return (-1); 4027 } 4028 4029 if ((number << shift) >> shift != number) { 4030 /* Overflow */ 4031 return (-1); 4032 } 4033 *num = number << shift; 4034 return (0); 4035} 4036 4037 4038/* 4039 * This routine could be used in the future to load default and/or saved 4040 * mode page parameters for a particuar lun. 4041 */ 4042static int 4043ctl_init_page_index(struct ctl_lun *lun) 4044{ 4045 int i; 4046 struct ctl_page_index *page_index; 4047 const char *value; 4048 uint64_t ival; 4049 4050 memcpy(&lun->mode_pages.index, page_index_template, 4051 sizeof(page_index_template)); 4052 4053 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4054 4055 page_index = &lun->mode_pages.index[i]; 4056 /* 4057 * If this is a disk-only mode page, there's no point in 4058 * setting it up. For some pages, we have to have some 4059 * basic information about the disk in order to calculate the 4060 * mode page data. 4061 */ 4062 if ((lun->be_lun->lun_type != T_DIRECT) 4063 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4064 continue; 4065 4066 switch (page_index->page_code & SMPH_PC_MASK) { 4067 case SMS_RW_ERROR_RECOVERY_PAGE: { 4068 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4069 panic("subpage is incorrect!"); 4070 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4071 &rw_er_page_default, 4072 sizeof(rw_er_page_default)); 4073 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4074 &rw_er_page_changeable, 4075 sizeof(rw_er_page_changeable)); 4076 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4077 &rw_er_page_default, 4078 sizeof(rw_er_page_default)); 4079 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4080 &rw_er_page_default, 4081 sizeof(rw_er_page_default)); 4082 page_index->page_data = 4083 (uint8_t *)lun->mode_pages.rw_er_page; 4084 break; 4085 } 4086 case SMS_FORMAT_DEVICE_PAGE: { 4087 struct scsi_format_page *format_page; 4088 4089 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4090 panic("subpage is incorrect!"); 4091 4092 /* 4093 * Sectors per track are set above. Bytes per 4094 * sector need to be set here on a per-LUN basis. 4095 */ 4096 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4097 &format_page_default, 4098 sizeof(format_page_default)); 4099 memcpy(&lun->mode_pages.format_page[ 4100 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4101 sizeof(format_page_changeable)); 4102 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4103 &format_page_default, 4104 sizeof(format_page_default)); 4105 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4106 &format_page_default, 4107 sizeof(format_page_default)); 4108 4109 format_page = &lun->mode_pages.format_page[ 4110 CTL_PAGE_CURRENT]; 4111 scsi_ulto2b(lun->be_lun->blocksize, 4112 format_page->bytes_per_sector); 4113 4114 format_page = &lun->mode_pages.format_page[ 4115 CTL_PAGE_DEFAULT]; 4116 scsi_ulto2b(lun->be_lun->blocksize, 4117 format_page->bytes_per_sector); 4118 4119 format_page = &lun->mode_pages.format_page[ 4120 CTL_PAGE_SAVED]; 4121 scsi_ulto2b(lun->be_lun->blocksize, 4122 format_page->bytes_per_sector); 4123 4124 page_index->page_data = 4125 (uint8_t *)lun->mode_pages.format_page; 4126 break; 4127 } 4128 case SMS_RIGID_DISK_PAGE: { 4129 struct scsi_rigid_disk_page *rigid_disk_page; 4130 uint32_t sectors_per_cylinder; 4131 uint64_t cylinders; 4132#ifndef __XSCALE__ 4133 int shift; 4134#endif /* !__XSCALE__ */ 4135 4136 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4137 panic("invalid subpage value %d", 4138 page_index->subpage); 4139 4140 /* 4141 * Rotation rate and sectors per track are set 4142 * above. We calculate the cylinders here based on 4143 * capacity. Due to the number of heads and 4144 * sectors per track we're using, smaller arrays 4145 * may turn out to have 0 cylinders. Linux and 4146 * FreeBSD don't pay attention to these mode pages 4147 * to figure out capacity, but Solaris does. It 4148 * seems to deal with 0 cylinders just fine, and 4149 * works out a fake geometry based on the capacity. 4150 */ 4151 memcpy(&lun->mode_pages.rigid_disk_page[ 4152 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4153 sizeof(rigid_disk_page_default)); 4154 memcpy(&lun->mode_pages.rigid_disk_page[ 4155 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4156 sizeof(rigid_disk_page_changeable)); 4157 4158 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4159 CTL_DEFAULT_HEADS; 4160 4161 /* 4162 * The divide method here will be more accurate, 4163 * probably, but results in floating point being 4164 * used in the kernel on i386 (__udivdi3()). On the 4165 * XScale, though, __udivdi3() is implemented in 4166 * software. 4167 * 4168 * The shift method for cylinder calculation is 4169 * accurate if sectors_per_cylinder is a power of 4170 * 2. Otherwise it might be slightly off -- you 4171 * might have a bit of a truncation problem. 4172 */ 4173#ifdef __XSCALE__ 4174 cylinders = (lun->be_lun->maxlba + 1) / 4175 sectors_per_cylinder; 4176#else 4177 for (shift = 31; shift > 0; shift--) { 4178 if (sectors_per_cylinder & (1 << shift)) 4179 break; 4180 } 4181 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4182#endif 4183 4184 /* 4185 * We've basically got 3 bytes, or 24 bits for the 4186 * cylinder size in the mode page. If we're over, 4187 * just round down to 2^24. 4188 */ 4189 if (cylinders > 0xffffff) 4190 cylinders = 0xffffff; 4191 4192 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4193 CTL_PAGE_DEFAULT]; 4194 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4195 4196 if ((value = ctl_get_opt(&lun->be_lun->options, 4197 "rpm")) != NULL) { 4198 scsi_ulto2b(strtol(value, NULL, 0), 4199 rigid_disk_page->rotation_rate); 4200 } 4201 4202 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4203 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4204 sizeof(rigid_disk_page_default)); 4205 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4206 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4207 sizeof(rigid_disk_page_default)); 4208 4209 page_index->page_data = 4210 (uint8_t *)lun->mode_pages.rigid_disk_page; 4211 break; 4212 } 4213 case SMS_CACHING_PAGE: { 4214 struct scsi_caching_page *caching_page; 4215 4216 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4217 panic("invalid subpage value %d", 4218 page_index->subpage); 4219 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4220 &caching_page_default, 4221 sizeof(caching_page_default)); 4222 memcpy(&lun->mode_pages.caching_page[ 4223 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4224 sizeof(caching_page_changeable)); 4225 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4226 &caching_page_default, 4227 sizeof(caching_page_default)); 4228 caching_page = &lun->mode_pages.caching_page[ 4229 CTL_PAGE_SAVED]; 4230 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4231 if (value != NULL && strcmp(value, "off") == 0) 4232 caching_page->flags1 &= ~SCP_WCE; 4233 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4234 if (value != NULL && strcmp(value, "off") == 0) 4235 caching_page->flags1 |= SCP_RCD; 4236 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4237 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4238 sizeof(caching_page_default)); 4239 page_index->page_data = 4240 (uint8_t *)lun->mode_pages.caching_page; 4241 break; 4242 } 4243 case SMS_CONTROL_MODE_PAGE: { 4244 struct scsi_control_page *control_page; 4245 4246 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4247 panic("invalid subpage value %d", 4248 page_index->subpage); 4249 4250 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4251 &control_page_default, 4252 sizeof(control_page_default)); 4253 memcpy(&lun->mode_pages.control_page[ 4254 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4255 sizeof(control_page_changeable)); 4256 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4257 &control_page_default, 4258 sizeof(control_page_default)); 4259 control_page = &lun->mode_pages.control_page[ 4260 CTL_PAGE_SAVED]; 4261 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4262 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4263 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4264 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4265 } 4266 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4267 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4268 sizeof(control_page_default)); 4269 page_index->page_data = 4270 (uint8_t *)lun->mode_pages.control_page; 4271 break; 4272 4273 } 4274 case SMS_INFO_EXCEPTIONS_PAGE: { 4275 switch (page_index->subpage) { 4276 case SMS_SUBPAGE_PAGE_0: 4277 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4278 &ie_page_default, 4279 sizeof(ie_page_default)); 4280 memcpy(&lun->mode_pages.ie_page[ 4281 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4282 sizeof(ie_page_changeable)); 4283 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4284 &ie_page_default, 4285 sizeof(ie_page_default)); 4286 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4287 &ie_page_default, 4288 sizeof(ie_page_default)); 4289 page_index->page_data = 4290 (uint8_t *)lun->mode_pages.ie_page; 4291 break; 4292 case 0x02: { 4293 struct ctl_logical_block_provisioning_page *page; 4294 4295 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4296 &lbp_page_default, 4297 sizeof(lbp_page_default)); 4298 memcpy(&lun->mode_pages.lbp_page[ 4299 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4300 sizeof(lbp_page_changeable)); 4301 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4302 &lbp_page_default, 4303 sizeof(lbp_page_default)); 4304 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4305 value = ctl_get_opt(&lun->be_lun->options, 4306 "avail-threshold"); 4307 if (value != NULL && 4308 ctl_expand_number(value, &ival) == 0) { 4309 page->descr[0].flags |= SLBPPD_ENABLED | 4310 SLBPPD_ARMING_DEC; 4311 if (lun->be_lun->blocksize) 4312 ival /= lun->be_lun->blocksize; 4313 else 4314 ival /= 512; 4315 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4316 page->descr[0].count); 4317 } 4318 value = ctl_get_opt(&lun->be_lun->options, 4319 "used-threshold"); 4320 if (value != NULL && 4321 ctl_expand_number(value, &ival) == 0) { 4322 page->descr[1].flags |= SLBPPD_ENABLED | 4323 SLBPPD_ARMING_INC; 4324 if (lun->be_lun->blocksize) 4325 ival /= lun->be_lun->blocksize; 4326 else 4327 ival /= 512; 4328 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4329 page->descr[1].count); 4330 } 4331 value = ctl_get_opt(&lun->be_lun->options, 4332 "pool-avail-threshold"); 4333 if (value != NULL && 4334 ctl_expand_number(value, &ival) == 0) { 4335 page->descr[2].flags |= SLBPPD_ENABLED | 4336 SLBPPD_ARMING_DEC; 4337 if (lun->be_lun->blocksize) 4338 ival /= lun->be_lun->blocksize; 4339 else 4340 ival /= 512; 4341 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4342 page->descr[2].count); 4343 } 4344 value = ctl_get_opt(&lun->be_lun->options, 4345 "pool-used-threshold"); 4346 if (value != NULL && 4347 ctl_expand_number(value, &ival) == 0) { 4348 page->descr[3].flags |= SLBPPD_ENABLED | 4349 SLBPPD_ARMING_INC; 4350 if (lun->be_lun->blocksize) 4351 ival /= lun->be_lun->blocksize; 4352 else 4353 ival /= 512; 4354 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4355 page->descr[3].count); 4356 } 4357 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4358 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4359 sizeof(lbp_page_default)); 4360 page_index->page_data = 4361 (uint8_t *)lun->mode_pages.lbp_page; 4362 }} 4363 break; 4364 } 4365 case SMS_VENDOR_SPECIFIC_PAGE:{ 4366 switch (page_index->subpage) { 4367 case DBGCNF_SUBPAGE_CODE: { 4368 struct copan_debugconf_subpage *current_page, 4369 *saved_page; 4370 4371 memcpy(&lun->mode_pages.debugconf_subpage[ 4372 CTL_PAGE_CURRENT], 4373 &debugconf_page_default, 4374 sizeof(debugconf_page_default)); 4375 memcpy(&lun->mode_pages.debugconf_subpage[ 4376 CTL_PAGE_CHANGEABLE], 4377 &debugconf_page_changeable, 4378 sizeof(debugconf_page_changeable)); 4379 memcpy(&lun->mode_pages.debugconf_subpage[ 4380 CTL_PAGE_DEFAULT], 4381 &debugconf_page_default, 4382 sizeof(debugconf_page_default)); 4383 memcpy(&lun->mode_pages.debugconf_subpage[ 4384 CTL_PAGE_SAVED], 4385 &debugconf_page_default, 4386 sizeof(debugconf_page_default)); 4387 page_index->page_data = 4388 (uint8_t *)lun->mode_pages.debugconf_subpage; 4389 4390 current_page = (struct copan_debugconf_subpage *) 4391 (page_index->page_data + 4392 (page_index->page_len * 4393 CTL_PAGE_CURRENT)); 4394 saved_page = (struct copan_debugconf_subpage *) 4395 (page_index->page_data + 4396 (page_index->page_len * 4397 CTL_PAGE_SAVED)); 4398 break; 4399 } 4400 default: 4401 panic("invalid subpage value %d", 4402 page_index->subpage); 4403 break; 4404 } 4405 break; 4406 } 4407 default: 4408 panic("invalid page value %d", 4409 page_index->page_code & SMPH_PC_MASK); 4410 break; 4411 } 4412 } 4413 4414 return (CTL_RETVAL_COMPLETE); 4415} 4416 4417static int 4418ctl_init_log_page_index(struct ctl_lun *lun) 4419{ 4420 struct ctl_page_index *page_index; 4421 int i, j, k, prev; 4422 4423 memcpy(&lun->log_pages.index, log_page_index_template, 4424 sizeof(log_page_index_template)); 4425 4426 prev = -1; 4427 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4428 4429 page_index = &lun->log_pages.index[i]; 4430 /* 4431 * If this is a disk-only mode page, there's no point in 4432 * setting it up. For some pages, we have to have some 4433 * basic information about the disk in order to calculate the 4434 * mode page data. 4435 */ 4436 if ((lun->be_lun->lun_type != T_DIRECT) 4437 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4438 continue; 4439 4440 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4441 lun->backend->lun_attr == NULL) 4442 continue; 4443 4444 if (page_index->page_code != prev) { 4445 lun->log_pages.pages_page[j] = page_index->page_code; 4446 prev = page_index->page_code; 4447 j++; 4448 } 4449 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4450 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4451 k++; 4452 } 4453 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4454 lun->log_pages.index[0].page_len = j; 4455 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4456 lun->log_pages.index[1].page_len = k * 2; 4457 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4458 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4459 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4460 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4461 4462 return (CTL_RETVAL_COMPLETE); 4463} 4464 4465static int 4466hex2bin(const char *str, uint8_t *buf, int buf_size) 4467{ 4468 int i; 4469 u_char c; 4470 4471 memset(buf, 0, buf_size); 4472 while (isspace(str[0])) 4473 str++; 4474 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4475 str += 2; 4476 buf_size *= 2; 4477 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4478 c = str[i]; 4479 if (isdigit(c)) 4480 c -= '0'; 4481 else if (isalpha(c)) 4482 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4483 else 4484 break; 4485 if (c >= 16) 4486 break; 4487 if ((i & 1) == 0) 4488 buf[i / 2] |= (c << 4); 4489 else 4490 buf[i / 2] |= c; 4491 } 4492 return ((i + 1) / 2); 4493} 4494 4495/* 4496 * LUN allocation. 4497 * 4498 * Requirements: 4499 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4500 * wants us to allocate the LUN and he can block. 4501 * - ctl_softc is always set 4502 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4503 * 4504 * Returns 0 for success, non-zero (errno) for failure. 4505 */ 4506static int 4507ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4508 struct ctl_be_lun *const be_lun) 4509{ 4510 struct ctl_lun *nlun, *lun; 4511 struct scsi_vpd_id_descriptor *desc; 4512 struct scsi_vpd_id_t10 *t10id; 4513 const char *eui, *naa, *scsiname, *vendor, *value; 4514 int lun_number, i, lun_malloced; 4515 int devidlen, idlen1, idlen2 = 0, len; 4516 4517 if (be_lun == NULL) 4518 return (EINVAL); 4519 4520 /* 4521 * We currently only support Direct Access or Processor LUN types. 4522 */ 4523 switch (be_lun->lun_type) { 4524 case T_DIRECT: 4525 break; 4526 case T_PROCESSOR: 4527 break; 4528 case T_SEQUENTIAL: 4529 case T_CHANGER: 4530 default: 4531 be_lun->lun_config_status(be_lun->be_lun, 4532 CTL_LUN_CONFIG_FAILURE); 4533 break; 4534 } 4535 if (ctl_lun == NULL) { 4536 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4537 lun_malloced = 1; 4538 } else { 4539 lun_malloced = 0; 4540 lun = ctl_lun; 4541 } 4542 4543 memset(lun, 0, sizeof(*lun)); 4544 if (lun_malloced) 4545 lun->flags = CTL_LUN_MALLOCED; 4546 4547 /* Generate LUN ID. */ 4548 devidlen = max(CTL_DEVID_MIN_LEN, 4549 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4550 idlen1 = sizeof(*t10id) + devidlen; 4551 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4552 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4553 if (scsiname != NULL) { 4554 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4555 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4556 } 4557 eui = ctl_get_opt(&be_lun->options, "eui"); 4558 if (eui != NULL) { 4559 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4560 } 4561 naa = ctl_get_opt(&be_lun->options, "naa"); 4562 if (naa != NULL) { 4563 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4564 } 4565 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4566 M_CTL, M_WAITOK | M_ZERO); 4567 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4568 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4569 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4570 desc->length = idlen1; 4571 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4572 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4573 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4574 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4575 } else { 4576 strncpy(t10id->vendor, vendor, 4577 min(sizeof(t10id->vendor), strlen(vendor))); 4578 } 4579 strncpy((char *)t10id->vendor_spec_id, 4580 (char *)be_lun->device_id, devidlen); 4581 if (scsiname != NULL) { 4582 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4583 desc->length); 4584 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4585 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4586 SVPD_ID_TYPE_SCSI_NAME; 4587 desc->length = idlen2; 4588 strlcpy(desc->identifier, scsiname, idlen2); 4589 } 4590 if (eui != NULL) { 4591 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4592 desc->length); 4593 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4594 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4595 SVPD_ID_TYPE_EUI64; 4596 desc->length = hex2bin(eui, desc->identifier, 16); 4597 desc->length = desc->length > 12 ? 16 : 4598 (desc->length > 8 ? 12 : 8); 4599 len -= 16 - desc->length; 4600 } 4601 if (naa != NULL) { 4602 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4603 desc->length); 4604 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4605 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4606 SVPD_ID_TYPE_NAA; 4607 desc->length = hex2bin(naa, desc->identifier, 16); 4608 desc->length = desc->length > 8 ? 16 : 8; 4609 len -= 16 - desc->length; 4610 } 4611 lun->lun_devid->len = len; 4612 4613 mtx_lock(&ctl_softc->ctl_lock); 4614 /* 4615 * See if the caller requested a particular LUN number. If so, see 4616 * if it is available. Otherwise, allocate the first available LUN. 4617 */ 4618 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4619 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4620 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4621 mtx_unlock(&ctl_softc->ctl_lock); 4622 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4623 printf("ctl: requested LUN ID %d is higher " 4624 "than CTL_MAX_LUNS - 1 (%d)\n", 4625 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4626 } else { 4627 /* 4628 * XXX KDM return an error, or just assign 4629 * another LUN ID in this case?? 4630 */ 4631 printf("ctl: requested LUN ID %d is already " 4632 "in use\n", be_lun->req_lun_id); 4633 } 4634 if (lun->flags & CTL_LUN_MALLOCED) 4635 free(lun, M_CTL); 4636 be_lun->lun_config_status(be_lun->be_lun, 4637 CTL_LUN_CONFIG_FAILURE); 4638 return (ENOSPC); 4639 } 4640 lun_number = be_lun->req_lun_id; 4641 } else { 4642 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4643 if (lun_number == -1) { 4644 mtx_unlock(&ctl_softc->ctl_lock); 4645 printf("ctl: can't allocate LUN, out of LUNs\n"); 4646 if (lun->flags & CTL_LUN_MALLOCED) 4647 free(lun, M_CTL); 4648 be_lun->lun_config_status(be_lun->be_lun, 4649 CTL_LUN_CONFIG_FAILURE); 4650 return (ENOSPC); 4651 } 4652 } 4653 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4654 4655 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4656 lun->lun = lun_number; 4657 lun->be_lun = be_lun; 4658 /* 4659 * The processor LUN is always enabled. Disk LUNs come on line 4660 * disabled, and must be enabled by the backend. 4661 */ 4662 lun->flags |= CTL_LUN_DISABLED; 4663 lun->backend = be_lun->be; 4664 be_lun->ctl_lun = lun; 4665 be_lun->lun_id = lun_number; 4666 atomic_add_int(&be_lun->be->num_luns, 1); 4667 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4668 lun->flags |= CTL_LUN_OFFLINE; 4669 4670 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4671 lun->flags |= CTL_LUN_STOPPED; 4672 4673 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4674 lun->flags |= CTL_LUN_INOPERABLE; 4675 4676 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4677 lun->flags |= CTL_LUN_PRIMARY_SC; 4678 4679 value = ctl_get_opt(&be_lun->options, "readonly"); 4680 if (value != NULL && strcmp(value, "on") == 0) 4681 lun->flags |= CTL_LUN_READONLY; 4682 4683 lun->serseq = CTL_LUN_SERSEQ_OFF; 4684 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ) 4685 lun->serseq = CTL_LUN_SERSEQ_READ; 4686 value = ctl_get_opt(&be_lun->options, "serseq"); 4687 if (value != NULL && strcmp(value, "on") == 0) 4688 lun->serseq = CTL_LUN_SERSEQ_ON; 4689 else if (value != NULL && strcmp(value, "read") == 0) 4690 lun->serseq = CTL_LUN_SERSEQ_READ; 4691 else if (value != NULL && strcmp(value, "off") == 0) 4692 lun->serseq = CTL_LUN_SERSEQ_OFF; 4693 4694 lun->ctl_softc = ctl_softc; 4695#ifdef CTL_TIME_IO 4696 lun->last_busy = getsbinuptime(); 4697#endif 4698 TAILQ_INIT(&lun->ooa_queue); 4699 TAILQ_INIT(&lun->blocked_queue); 4700 STAILQ_INIT(&lun->error_list); 4701 ctl_tpc_lun_init(lun); 4702 4703 /* 4704 * Initialize the mode and log page index. 4705 */ 4706 ctl_init_page_index(lun); 4707 ctl_init_log_page_index(lun); 4708 4709 /* 4710 * Now, before we insert this lun on the lun list, set the lun 4711 * inventory changed UA for all other luns. 4712 */ 4713 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4714 mtx_lock(&nlun->lun_lock); 4715 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4716 mtx_unlock(&nlun->lun_lock); 4717 } 4718 4719 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4720 4721 ctl_softc->ctl_luns[lun_number] = lun; 4722 4723 ctl_softc->num_luns++; 4724 4725 /* Setup statistics gathering */ 4726 lun->stats.device_type = be_lun->lun_type; 4727 lun->stats.lun_number = lun_number; 4728 if (lun->stats.device_type == T_DIRECT) 4729 lun->stats.blocksize = be_lun->blocksize; 4730 else 4731 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4732 for (i = 0;i < CTL_MAX_PORTS;i++) 4733 lun->stats.ports[i].targ_port = i; 4734 4735 mtx_unlock(&ctl_softc->ctl_lock); 4736 4737 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4738 return (0); 4739} 4740 4741/* 4742 * Delete a LUN. 4743 * Assumptions: 4744 * - LUN has already been marked invalid and any pending I/O has been taken 4745 * care of. 4746 */ 4747static int 4748ctl_free_lun(struct ctl_lun *lun) 4749{ 4750 struct ctl_softc *softc; 4751 struct ctl_lun *nlun; 4752 int i; 4753 4754 softc = lun->ctl_softc; 4755 4756 mtx_assert(&softc->ctl_lock, MA_OWNED); 4757 4758 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4759 4760 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4761 4762 softc->ctl_luns[lun->lun] = NULL; 4763 4764 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4765 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4766 4767 softc->num_luns--; 4768 4769 /* 4770 * Tell the backend to free resources, if this LUN has a backend. 4771 */ 4772 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4773 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4774 4775 ctl_tpc_lun_shutdown(lun); 4776 mtx_destroy(&lun->lun_lock); 4777 free(lun->lun_devid, M_CTL); 4778 for (i = 0; i < CTL_MAX_PORTS; i++) 4779 free(lun->pending_ua[i], M_CTL); 4780 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) 4781 free(lun->pr_keys[i], M_CTL); 4782 free(lun->write_buffer, M_CTL); 4783 if (lun->flags & CTL_LUN_MALLOCED) 4784 free(lun, M_CTL); 4785 4786 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4787 mtx_lock(&nlun->lun_lock); 4788 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4789 mtx_unlock(&nlun->lun_lock); 4790 } 4791 4792 return (0); 4793} 4794 4795static void 4796ctl_create_lun(struct ctl_be_lun *be_lun) 4797{ 4798 struct ctl_softc *softc; 4799 4800 softc = control_softc; 4801 4802 /* 4803 * ctl_alloc_lun() should handle all potential failure cases. 4804 */ 4805 ctl_alloc_lun(softc, NULL, be_lun); 4806} 4807 4808int 4809ctl_add_lun(struct ctl_be_lun *be_lun) 4810{ 4811 struct ctl_softc *softc = control_softc; 4812 4813 mtx_lock(&softc->ctl_lock); 4814 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4815 mtx_unlock(&softc->ctl_lock); 4816 wakeup(&softc->pending_lun_queue); 4817 4818 return (0); 4819} 4820 4821int 4822ctl_enable_lun(struct ctl_be_lun *be_lun) 4823{ 4824 struct ctl_softc *softc; 4825 struct ctl_port *port, *nport; 4826 struct ctl_lun *lun; 4827 int retval; 4828 4829 lun = (struct ctl_lun *)be_lun->ctl_lun; 4830 softc = lun->ctl_softc; 4831 4832 mtx_lock(&softc->ctl_lock); 4833 mtx_lock(&lun->lun_lock); 4834 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4835 /* 4836 * eh? Why did we get called if the LUN is already 4837 * enabled? 4838 */ 4839 mtx_unlock(&lun->lun_lock); 4840 mtx_unlock(&softc->ctl_lock); 4841 return (0); 4842 } 4843 lun->flags &= ~CTL_LUN_DISABLED; 4844 mtx_unlock(&lun->lun_lock); 4845 4846 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4847 nport = STAILQ_NEXT(port, links); 4848 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4849 port->lun_map != NULL) 4850 continue; 4851 4852 /* 4853 * Drop the lock while we call the FETD's enable routine. 4854 * This can lead to a callback into CTL (at least in the 4855 * case of the internal initiator frontend. 4856 */ 4857 mtx_unlock(&softc->ctl_lock); 4858 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4859 mtx_lock(&softc->ctl_lock); 4860 if (retval != 0) { 4861 printf("%s: FETD %s port %d returned error " 4862 "%d for lun_enable on lun %jd\n", 4863 __func__, port->port_name, port->targ_port, 4864 retval, (intmax_t)lun->lun); 4865 } 4866 } 4867 4868 mtx_unlock(&softc->ctl_lock); 4869 4870 return (0); 4871} 4872 4873int 4874ctl_disable_lun(struct ctl_be_lun *be_lun) 4875{ 4876 struct ctl_softc *softc; 4877 struct ctl_port *port; 4878 struct ctl_lun *lun; 4879 int retval; 4880 4881 lun = (struct ctl_lun *)be_lun->ctl_lun; 4882 softc = lun->ctl_softc; 4883 4884 mtx_lock(&softc->ctl_lock); 4885 mtx_lock(&lun->lun_lock); 4886 if (lun->flags & CTL_LUN_DISABLED) { 4887 mtx_unlock(&lun->lun_lock); 4888 mtx_unlock(&softc->ctl_lock); 4889 return (0); 4890 } 4891 lun->flags |= CTL_LUN_DISABLED; 4892 mtx_unlock(&lun->lun_lock); 4893 4894 STAILQ_FOREACH(port, &softc->port_list, links) { 4895 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4896 port->lun_map != NULL) 4897 continue; 4898 mtx_unlock(&softc->ctl_lock); 4899 /* 4900 * Drop the lock before we call the frontend's disable 4901 * routine, to avoid lock order reversals. 4902 * 4903 * XXX KDM what happens if the frontend list changes while 4904 * we're traversing it? It's unlikely, but should be handled. 4905 */ 4906 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4907 mtx_lock(&softc->ctl_lock); 4908 if (retval != 0) { 4909 printf("%s: FETD %s port %d returned error " 4910 "%d for lun_disable on lun %jd\n", 4911 __func__, port->port_name, port->targ_port, 4912 retval, (intmax_t)lun->lun); 4913 } 4914 } 4915 4916 mtx_unlock(&softc->ctl_lock); 4917 4918 return (0); 4919} 4920 4921int 4922ctl_start_lun(struct ctl_be_lun *be_lun) 4923{ 4924 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4925 4926 mtx_lock(&lun->lun_lock); 4927 lun->flags &= ~CTL_LUN_STOPPED; 4928 mtx_unlock(&lun->lun_lock); 4929 return (0); 4930} 4931 4932int 4933ctl_stop_lun(struct ctl_be_lun *be_lun) 4934{ 4935 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4936 4937 mtx_lock(&lun->lun_lock); 4938 lun->flags |= CTL_LUN_STOPPED; 4939 mtx_unlock(&lun->lun_lock); 4940 return (0); 4941} 4942 4943int 4944ctl_lun_offline(struct ctl_be_lun *be_lun) 4945{ 4946 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4947 4948 mtx_lock(&lun->lun_lock); 4949 lun->flags |= CTL_LUN_OFFLINE; 4950 mtx_unlock(&lun->lun_lock); 4951 return (0); 4952} 4953 4954int 4955ctl_lun_online(struct ctl_be_lun *be_lun) 4956{ 4957 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4958 4959 mtx_lock(&lun->lun_lock); 4960 lun->flags &= ~CTL_LUN_OFFLINE; 4961 mtx_unlock(&lun->lun_lock); 4962 return (0); 4963} 4964 4965int 4966ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4967{ 4968 struct ctl_softc *softc; 4969 struct ctl_lun *lun; 4970 4971 lun = (struct ctl_lun *)be_lun->ctl_lun; 4972 softc = lun->ctl_softc; 4973 4974 mtx_lock(&lun->lun_lock); 4975 4976 /* 4977 * The LUN needs to be disabled before it can be marked invalid. 4978 */ 4979 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4980 mtx_unlock(&lun->lun_lock); 4981 return (-1); 4982 } 4983 /* 4984 * Mark the LUN invalid. 4985 */ 4986 lun->flags |= CTL_LUN_INVALID; 4987 4988 /* 4989 * If there is nothing in the OOA queue, go ahead and free the LUN. 4990 * If we have something in the OOA queue, we'll free it when the 4991 * last I/O completes. 4992 */ 4993 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4994 mtx_unlock(&lun->lun_lock); 4995 mtx_lock(&softc->ctl_lock); 4996 ctl_free_lun(lun); 4997 mtx_unlock(&softc->ctl_lock); 4998 } else 4999 mtx_unlock(&lun->lun_lock); 5000 5001 return (0); 5002} 5003 5004int 5005ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5006{ 5007 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5008 5009 mtx_lock(&lun->lun_lock); 5010 lun->flags |= CTL_LUN_INOPERABLE; 5011 mtx_unlock(&lun->lun_lock); 5012 return (0); 5013} 5014 5015int 5016ctl_lun_operable(struct ctl_be_lun *be_lun) 5017{ 5018 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5019 5020 mtx_lock(&lun->lun_lock); 5021 lun->flags &= ~CTL_LUN_INOPERABLE; 5022 mtx_unlock(&lun->lun_lock); 5023 return (0); 5024} 5025 5026void 5027ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5028{ 5029 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5030 5031 mtx_lock(&lun->lun_lock); 5032 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 5033 mtx_unlock(&lun->lun_lock); 5034} 5035 5036/* 5037 * Backend "memory move is complete" callback for requests that never 5038 * make it down to say RAIDCore's configuration code. 5039 */ 5040int 5041ctl_config_move_done(union ctl_io *io) 5042{ 5043 int retval; 5044 5045 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5046 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5047 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5048 5049 if ((io->io_hdr.port_status != 0) && 5050 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5051 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5052 /* 5053 * For hardware error sense keys, the sense key 5054 * specific value is defined to be a retry count, 5055 * but we use it to pass back an internal FETD 5056 * error code. XXX KDM Hopefully the FETD is only 5057 * using 16 bits for an error code, since that's 5058 * all the space we have in the sks field. 5059 */ 5060 ctl_set_internal_failure(&io->scsiio, 5061 /*sks_valid*/ 1, 5062 /*retry_count*/ 5063 io->io_hdr.port_status); 5064 } 5065 5066 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5067 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5068 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5069 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5070 /* 5071 * XXX KDM just assuming a single pointer here, and not a 5072 * S/G list. If we start using S/G lists for config data, 5073 * we'll need to know how to clean them up here as well. 5074 */ 5075 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5076 free(io->scsiio.kern_data_ptr, M_CTL); 5077 ctl_done(io); 5078 retval = CTL_RETVAL_COMPLETE; 5079 } else { 5080 /* 5081 * XXX KDM now we need to continue data movement. Some 5082 * options: 5083 * - call ctl_scsiio() again? We don't do this for data 5084 * writes, because for those at least we know ahead of 5085 * time where the write will go and how long it is. For 5086 * config writes, though, that information is largely 5087 * contained within the write itself, thus we need to 5088 * parse out the data again. 5089 * 5090 * - Call some other function once the data is in? 5091 */ 5092 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5093 ctl_data_print(io); 5094 5095 /* 5096 * XXX KDM call ctl_scsiio() again for now, and check flag 5097 * bits to see whether we're allocated or not. 5098 */ 5099 retval = ctl_scsiio(&io->scsiio); 5100 } 5101 return (retval); 5102} 5103 5104/* 5105 * This gets called by a backend driver when it is done with a 5106 * data_submit method. 5107 */ 5108void 5109ctl_data_submit_done(union ctl_io *io) 5110{ 5111 /* 5112 * If the IO_CONT flag is set, we need to call the supplied 5113 * function to continue processing the I/O, instead of completing 5114 * the I/O just yet. 5115 * 5116 * If there is an error, though, we don't want to keep processing. 5117 * Instead, just send status back to the initiator. 5118 */ 5119 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5120 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5121 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5122 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5123 io->scsiio.io_cont(io); 5124 return; 5125 } 5126 ctl_done(io); 5127} 5128 5129/* 5130 * This gets called by a backend driver when it is done with a 5131 * configuration write. 5132 */ 5133void 5134ctl_config_write_done(union ctl_io *io) 5135{ 5136 uint8_t *buf; 5137 5138 /* 5139 * If the IO_CONT flag is set, we need to call the supplied 5140 * function to continue processing the I/O, instead of completing 5141 * the I/O just yet. 5142 * 5143 * If there is an error, though, we don't want to keep processing. 5144 * Instead, just send status back to the initiator. 5145 */ 5146 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5147 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5148 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5149 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5150 io->scsiio.io_cont(io); 5151 return; 5152 } 5153 /* 5154 * Since a configuration write can be done for commands that actually 5155 * have data allocated, like write buffer, and commands that have 5156 * no data, like start/stop unit, we need to check here. 5157 */ 5158 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5159 buf = io->scsiio.kern_data_ptr; 5160 else 5161 buf = NULL; 5162 ctl_done(io); 5163 if (buf) 5164 free(buf, M_CTL); 5165} 5166 5167void 5168ctl_config_read_done(union ctl_io *io) 5169{ 5170 uint8_t *buf; 5171 5172 /* 5173 * If there is some error -- we are done, skip data transfer. 5174 */ 5175 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5176 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5177 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5178 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5179 buf = io->scsiio.kern_data_ptr; 5180 else 5181 buf = NULL; 5182 ctl_done(io); 5183 if (buf) 5184 free(buf, M_CTL); 5185 return; 5186 } 5187 5188 /* 5189 * If the IO_CONT flag is set, we need to call the supplied 5190 * function to continue processing the I/O, instead of completing 5191 * the I/O just yet. 5192 */ 5193 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5194 io->scsiio.io_cont(io); 5195 return; 5196 } 5197 5198 ctl_datamove(io); 5199} 5200 5201/* 5202 * SCSI release command. 5203 */ 5204int 5205ctl_scsi_release(struct ctl_scsiio *ctsio) 5206{ 5207 int length, longid, thirdparty_id, resv_id; 5208 struct ctl_lun *lun; 5209 uint32_t residx; 5210 5211 length = 0; 5212 resv_id = 0; 5213 5214 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5215 5216 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5217 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5218 5219 switch (ctsio->cdb[0]) { 5220 case RELEASE_10: { 5221 struct scsi_release_10 *cdb; 5222 5223 cdb = (struct scsi_release_10 *)ctsio->cdb; 5224 5225 if (cdb->byte2 & SR10_LONGID) 5226 longid = 1; 5227 else 5228 thirdparty_id = cdb->thirdparty_id; 5229 5230 resv_id = cdb->resv_id; 5231 length = scsi_2btoul(cdb->length); 5232 break; 5233 } 5234 } 5235 5236 5237 /* 5238 * XXX KDM right now, we only support LUN reservation. We don't 5239 * support 3rd party reservations, or extent reservations, which 5240 * might actually need the parameter list. If we've gotten this 5241 * far, we've got a LUN reservation. Anything else got kicked out 5242 * above. So, according to SPC, ignore the length. 5243 */ 5244 length = 0; 5245 5246 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5247 && (length > 0)) { 5248 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5249 ctsio->kern_data_len = length; 5250 ctsio->kern_total_len = length; 5251 ctsio->kern_data_resid = 0; 5252 ctsio->kern_rel_offset = 0; 5253 ctsio->kern_sg_entries = 0; 5254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5255 ctsio->be_move_done = ctl_config_move_done; 5256 ctl_datamove((union ctl_io *)ctsio); 5257 5258 return (CTL_RETVAL_COMPLETE); 5259 } 5260 5261 if (length > 0) 5262 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5263 5264 mtx_lock(&lun->lun_lock); 5265 5266 /* 5267 * According to SPC, it is not an error for an intiator to attempt 5268 * to release a reservation on a LUN that isn't reserved, or that 5269 * is reserved by another initiator. The reservation can only be 5270 * released, though, by the initiator who made it or by one of 5271 * several reset type events. 5272 */ 5273 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5274 lun->flags &= ~CTL_LUN_RESERVED; 5275 5276 mtx_unlock(&lun->lun_lock); 5277 5278 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5279 free(ctsio->kern_data_ptr, M_CTL); 5280 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5281 } 5282 5283 ctl_set_success(ctsio); 5284 ctl_done((union ctl_io *)ctsio); 5285 return (CTL_RETVAL_COMPLETE); 5286} 5287 5288int 5289ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5290{ 5291 int extent, thirdparty, longid; 5292 int resv_id, length; 5293 uint64_t thirdparty_id; 5294 struct ctl_lun *lun; 5295 uint32_t residx; 5296 5297 extent = 0; 5298 thirdparty = 0; 5299 longid = 0; 5300 resv_id = 0; 5301 length = 0; 5302 thirdparty_id = 0; 5303 5304 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5305 5306 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5307 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5308 5309 switch (ctsio->cdb[0]) { 5310 case RESERVE_10: { 5311 struct scsi_reserve_10 *cdb; 5312 5313 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5314 5315 if (cdb->byte2 & SR10_LONGID) 5316 longid = 1; 5317 else 5318 thirdparty_id = cdb->thirdparty_id; 5319 5320 resv_id = cdb->resv_id; 5321 length = scsi_2btoul(cdb->length); 5322 break; 5323 } 5324 } 5325 5326 /* 5327 * XXX KDM right now, we only support LUN reservation. We don't 5328 * support 3rd party reservations, or extent reservations, which 5329 * might actually need the parameter list. If we've gotten this 5330 * far, we've got a LUN reservation. Anything else got kicked out 5331 * above. So, according to SPC, ignore the length. 5332 */ 5333 length = 0; 5334 5335 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5336 && (length > 0)) { 5337 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5338 ctsio->kern_data_len = length; 5339 ctsio->kern_total_len = length; 5340 ctsio->kern_data_resid = 0; 5341 ctsio->kern_rel_offset = 0; 5342 ctsio->kern_sg_entries = 0; 5343 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5344 ctsio->be_move_done = ctl_config_move_done; 5345 ctl_datamove((union ctl_io *)ctsio); 5346 5347 return (CTL_RETVAL_COMPLETE); 5348 } 5349 5350 if (length > 0) 5351 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5352 5353 mtx_lock(&lun->lun_lock); 5354 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5355 ctl_set_reservation_conflict(ctsio); 5356 goto bailout; 5357 } 5358 5359 lun->flags |= CTL_LUN_RESERVED; 5360 lun->res_idx = residx; 5361 5362 ctl_set_success(ctsio); 5363 5364bailout: 5365 mtx_unlock(&lun->lun_lock); 5366 5367 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5368 free(ctsio->kern_data_ptr, M_CTL); 5369 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5370 } 5371 5372 ctl_done((union ctl_io *)ctsio); 5373 return (CTL_RETVAL_COMPLETE); 5374} 5375 5376int 5377ctl_start_stop(struct ctl_scsiio *ctsio) 5378{ 5379 struct scsi_start_stop_unit *cdb; 5380 struct ctl_lun *lun; 5381 int retval; 5382 5383 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5384 5385 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5386 retval = 0; 5387 5388 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5389 5390 /* 5391 * XXX KDM 5392 * We don't support the immediate bit on a stop unit. In order to 5393 * do that, we would need to code up a way to know that a stop is 5394 * pending, and hold off any new commands until it completes, one 5395 * way or another. Then we could accept or reject those commands 5396 * depending on its status. We would almost need to do the reverse 5397 * of what we do below for an immediate start -- return the copy of 5398 * the ctl_io to the FETD with status to send to the host (and to 5399 * free the copy!) and then free the original I/O once the stop 5400 * actually completes. That way, the OOA queue mechanism can work 5401 * to block commands that shouldn't proceed. Another alternative 5402 * would be to put the copy in the queue in place of the original, 5403 * and return the original back to the caller. That could be 5404 * slightly safer.. 5405 */ 5406 if ((cdb->byte2 & SSS_IMMED) 5407 && ((cdb->how & SSS_START) == 0)) { 5408 ctl_set_invalid_field(ctsio, 5409 /*sks_valid*/ 1, 5410 /*command*/ 1, 5411 /*field*/ 1, 5412 /*bit_valid*/ 1, 5413 /*bit*/ 0); 5414 ctl_done((union ctl_io *)ctsio); 5415 return (CTL_RETVAL_COMPLETE); 5416 } 5417 5418 if ((lun->flags & CTL_LUN_PR_RESERVED) 5419 && ((cdb->how & SSS_START)==0)) { 5420 uint32_t residx; 5421 5422 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5423 if (ctl_get_prkey(lun, residx) == 0 5424 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5425 5426 ctl_set_reservation_conflict(ctsio); 5427 ctl_done((union ctl_io *)ctsio); 5428 return (CTL_RETVAL_COMPLETE); 5429 } 5430 } 5431 5432 /* 5433 * If there is no backend on this device, we can't start or stop 5434 * it. In theory we shouldn't get any start/stop commands in the 5435 * first place at this level if the LUN doesn't have a backend. 5436 * That should get stopped by the command decode code. 5437 */ 5438 if (lun->backend == NULL) { 5439 ctl_set_invalid_opcode(ctsio); 5440 ctl_done((union ctl_io *)ctsio); 5441 return (CTL_RETVAL_COMPLETE); 5442 } 5443 5444 /* 5445 * XXX KDM Copan-specific offline behavior. 5446 * Figure out a reasonable way to port this? 5447 */ 5448#ifdef NEEDTOPORT 5449 mtx_lock(&lun->lun_lock); 5450 5451 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5452 && (lun->flags & CTL_LUN_OFFLINE)) { 5453 /* 5454 * If the LUN is offline, and the on/offline bit isn't set, 5455 * reject the start or stop. Otherwise, let it through. 5456 */ 5457 mtx_unlock(&lun->lun_lock); 5458 ctl_set_lun_not_ready(ctsio); 5459 ctl_done((union ctl_io *)ctsio); 5460 } else { 5461 mtx_unlock(&lun->lun_lock); 5462#endif /* NEEDTOPORT */ 5463 /* 5464 * This could be a start or a stop when we're online, 5465 * or a stop/offline or start/online. A start or stop when 5466 * we're offline is covered in the case above. 5467 */ 5468 /* 5469 * In the non-immediate case, we send the request to 5470 * the backend and return status to the user when 5471 * it is done. 5472 * 5473 * In the immediate case, we allocate a new ctl_io 5474 * to hold a copy of the request, and send that to 5475 * the backend. We then set good status on the 5476 * user's request and return it immediately. 5477 */ 5478 if (cdb->byte2 & SSS_IMMED) { 5479 union ctl_io *new_io; 5480 5481 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5482 ctl_copy_io((union ctl_io *)ctsio, new_io); 5483 retval = lun->backend->config_write(new_io); 5484 ctl_set_success(ctsio); 5485 ctl_done((union ctl_io *)ctsio); 5486 } else { 5487 retval = lun->backend->config_write( 5488 (union ctl_io *)ctsio); 5489 } 5490#ifdef NEEDTOPORT 5491 } 5492#endif 5493 return (retval); 5494} 5495 5496/* 5497 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5498 * we don't really do anything with the LBA and length fields if the user 5499 * passes them in. Instead we'll just flush out the cache for the entire 5500 * LUN. 5501 */ 5502int 5503ctl_sync_cache(struct ctl_scsiio *ctsio) 5504{ 5505 struct ctl_lun *lun; 5506 struct ctl_softc *softc; 5507 struct ctl_lba_len_flags *lbalen; 5508 uint64_t starting_lba; 5509 uint32_t block_count; 5510 int retval; 5511 uint8_t byte2; 5512 5513 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5514 5515 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5516 softc = lun->ctl_softc; 5517 retval = 0; 5518 5519 switch (ctsio->cdb[0]) { 5520 case SYNCHRONIZE_CACHE: { 5521 struct scsi_sync_cache *cdb; 5522 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5523 5524 starting_lba = scsi_4btoul(cdb->begin_lba); 5525 block_count = scsi_2btoul(cdb->lb_count); 5526 byte2 = cdb->byte2; 5527 break; 5528 } 5529 case SYNCHRONIZE_CACHE_16: { 5530 struct scsi_sync_cache_16 *cdb; 5531 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5532 5533 starting_lba = scsi_8btou64(cdb->begin_lba); 5534 block_count = scsi_4btoul(cdb->lb_count); 5535 byte2 = cdb->byte2; 5536 break; 5537 } 5538 default: 5539 ctl_set_invalid_opcode(ctsio); 5540 ctl_done((union ctl_io *)ctsio); 5541 goto bailout; 5542 break; /* NOTREACHED */ 5543 } 5544 5545 /* 5546 * We check the LBA and length, but don't do anything with them. 5547 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5548 * get flushed. This check will just help satisfy anyone who wants 5549 * to see an error for an out of range LBA. 5550 */ 5551 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5552 ctl_set_lba_out_of_range(ctsio); 5553 ctl_done((union ctl_io *)ctsio); 5554 goto bailout; 5555 } 5556 5557 /* 5558 * If this LUN has no backend, we can't flush the cache anyway. 5559 */ 5560 if (lun->backend == NULL) { 5561 ctl_set_invalid_opcode(ctsio); 5562 ctl_done((union ctl_io *)ctsio); 5563 goto bailout; 5564 } 5565 5566 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5567 lbalen->lba = starting_lba; 5568 lbalen->len = block_count; 5569 lbalen->flags = byte2; 5570 5571 /* 5572 * Check to see whether we're configured to send the SYNCHRONIZE 5573 * CACHE command directly to the back end. 5574 */ 5575 mtx_lock(&lun->lun_lock); 5576 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5577 && (++(lun->sync_count) >= lun->sync_interval)) { 5578 lun->sync_count = 0; 5579 mtx_unlock(&lun->lun_lock); 5580 retval = lun->backend->config_write((union ctl_io *)ctsio); 5581 } else { 5582 mtx_unlock(&lun->lun_lock); 5583 ctl_set_success(ctsio); 5584 ctl_done((union ctl_io *)ctsio); 5585 } 5586 5587bailout: 5588 5589 return (retval); 5590} 5591 5592int 5593ctl_format(struct ctl_scsiio *ctsio) 5594{ 5595 struct scsi_format *cdb; 5596 struct ctl_lun *lun; 5597 int length, defect_list_len; 5598 5599 CTL_DEBUG_PRINT(("ctl_format\n")); 5600 5601 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5602 5603 cdb = (struct scsi_format *)ctsio->cdb; 5604 5605 length = 0; 5606 if (cdb->byte2 & SF_FMTDATA) { 5607 if (cdb->byte2 & SF_LONGLIST) 5608 length = sizeof(struct scsi_format_header_long); 5609 else 5610 length = sizeof(struct scsi_format_header_short); 5611 } 5612 5613 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5614 && (length > 0)) { 5615 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5616 ctsio->kern_data_len = length; 5617 ctsio->kern_total_len = length; 5618 ctsio->kern_data_resid = 0; 5619 ctsio->kern_rel_offset = 0; 5620 ctsio->kern_sg_entries = 0; 5621 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5622 ctsio->be_move_done = ctl_config_move_done; 5623 ctl_datamove((union ctl_io *)ctsio); 5624 5625 return (CTL_RETVAL_COMPLETE); 5626 } 5627 5628 defect_list_len = 0; 5629 5630 if (cdb->byte2 & SF_FMTDATA) { 5631 if (cdb->byte2 & SF_LONGLIST) { 5632 struct scsi_format_header_long *header; 5633 5634 header = (struct scsi_format_header_long *) 5635 ctsio->kern_data_ptr; 5636 5637 defect_list_len = scsi_4btoul(header->defect_list_len); 5638 if (defect_list_len != 0) { 5639 ctl_set_invalid_field(ctsio, 5640 /*sks_valid*/ 1, 5641 /*command*/ 0, 5642 /*field*/ 2, 5643 /*bit_valid*/ 0, 5644 /*bit*/ 0); 5645 goto bailout; 5646 } 5647 } else { 5648 struct scsi_format_header_short *header; 5649 5650 header = (struct scsi_format_header_short *) 5651 ctsio->kern_data_ptr; 5652 5653 defect_list_len = scsi_2btoul(header->defect_list_len); 5654 if (defect_list_len != 0) { 5655 ctl_set_invalid_field(ctsio, 5656 /*sks_valid*/ 1, 5657 /*command*/ 0, 5658 /*field*/ 2, 5659 /*bit_valid*/ 0, 5660 /*bit*/ 0); 5661 goto bailout; 5662 } 5663 } 5664 } 5665 5666 /* 5667 * The format command will clear out the "Medium format corrupted" 5668 * status if set by the configuration code. That status is really 5669 * just a way to notify the host that we have lost the media, and 5670 * get them to issue a command that will basically make them think 5671 * they're blowing away the media. 5672 */ 5673 mtx_lock(&lun->lun_lock); 5674 lun->flags &= ~CTL_LUN_INOPERABLE; 5675 mtx_unlock(&lun->lun_lock); 5676 5677 ctl_set_success(ctsio); 5678bailout: 5679 5680 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5681 free(ctsio->kern_data_ptr, M_CTL); 5682 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5683 } 5684 5685 ctl_done((union ctl_io *)ctsio); 5686 return (CTL_RETVAL_COMPLETE); 5687} 5688 5689int 5690ctl_read_buffer(struct ctl_scsiio *ctsio) 5691{ 5692 struct scsi_read_buffer *cdb; 5693 struct ctl_lun *lun; 5694 int buffer_offset, len; 5695 static uint8_t descr[4]; 5696 static uint8_t echo_descr[4] = { 0 }; 5697 5698 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5699 5700 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5701 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5702 5703 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5704 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5705 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5706 ctl_set_invalid_field(ctsio, 5707 /*sks_valid*/ 1, 5708 /*command*/ 1, 5709 /*field*/ 1, 5710 /*bit_valid*/ 1, 5711 /*bit*/ 4); 5712 ctl_done((union ctl_io *)ctsio); 5713 return (CTL_RETVAL_COMPLETE); 5714 } 5715 5716 len = scsi_3btoul(cdb->length); 5717 buffer_offset = scsi_3btoul(cdb->offset); 5718 5719 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5720 ctl_set_invalid_field(ctsio, 5721 /*sks_valid*/ 1, 5722 /*command*/ 1, 5723 /*field*/ 6, 5724 /*bit_valid*/ 0, 5725 /*bit*/ 0); 5726 ctl_done((union ctl_io *)ctsio); 5727 return (CTL_RETVAL_COMPLETE); 5728 } 5729 5730 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5731 descr[0] = 0; 5732 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5733 ctsio->kern_data_ptr = descr; 5734 len = min(len, sizeof(descr)); 5735 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5736 ctsio->kern_data_ptr = echo_descr; 5737 len = min(len, sizeof(echo_descr)); 5738 } else { 5739 if (lun->write_buffer == NULL) { 5740 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5741 M_CTL, M_WAITOK); 5742 } 5743 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5744 } 5745 ctsio->kern_data_len = len; 5746 ctsio->kern_total_len = len; 5747 ctsio->kern_data_resid = 0; 5748 ctsio->kern_rel_offset = 0; 5749 ctsio->kern_sg_entries = 0; 5750 ctl_set_success(ctsio); 5751 ctsio->be_move_done = ctl_config_move_done; 5752 ctl_datamove((union ctl_io *)ctsio); 5753 return (CTL_RETVAL_COMPLETE); 5754} 5755 5756int 5757ctl_write_buffer(struct ctl_scsiio *ctsio) 5758{ 5759 struct scsi_write_buffer *cdb; 5760 struct ctl_lun *lun; 5761 int buffer_offset, len; 5762 5763 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5764 5765 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5766 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5767 5768 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5769 ctl_set_invalid_field(ctsio, 5770 /*sks_valid*/ 1, 5771 /*command*/ 1, 5772 /*field*/ 1, 5773 /*bit_valid*/ 1, 5774 /*bit*/ 4); 5775 ctl_done((union ctl_io *)ctsio); 5776 return (CTL_RETVAL_COMPLETE); 5777 } 5778 5779 len = scsi_3btoul(cdb->length); 5780 buffer_offset = scsi_3btoul(cdb->offset); 5781 5782 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5783 ctl_set_invalid_field(ctsio, 5784 /*sks_valid*/ 1, 5785 /*command*/ 1, 5786 /*field*/ 6, 5787 /*bit_valid*/ 0, 5788 /*bit*/ 0); 5789 ctl_done((union ctl_io *)ctsio); 5790 return (CTL_RETVAL_COMPLETE); 5791 } 5792 5793 /* 5794 * If we've got a kernel request that hasn't been malloced yet, 5795 * malloc it and tell the caller the data buffer is here. 5796 */ 5797 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5798 if (lun->write_buffer == NULL) { 5799 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5800 M_CTL, M_WAITOK); 5801 } 5802 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5803 ctsio->kern_data_len = len; 5804 ctsio->kern_total_len = len; 5805 ctsio->kern_data_resid = 0; 5806 ctsio->kern_rel_offset = 0; 5807 ctsio->kern_sg_entries = 0; 5808 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5809 ctsio->be_move_done = ctl_config_move_done; 5810 ctl_datamove((union ctl_io *)ctsio); 5811 5812 return (CTL_RETVAL_COMPLETE); 5813 } 5814 5815 ctl_set_success(ctsio); 5816 ctl_done((union ctl_io *)ctsio); 5817 return (CTL_RETVAL_COMPLETE); 5818} 5819 5820int 5821ctl_write_same(struct ctl_scsiio *ctsio) 5822{ 5823 struct ctl_lun *lun; 5824 struct ctl_lba_len_flags *lbalen; 5825 uint64_t lba; 5826 uint32_t num_blocks; 5827 int len, retval; 5828 uint8_t byte2; 5829 5830 retval = CTL_RETVAL_COMPLETE; 5831 5832 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5833 5834 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5835 5836 switch (ctsio->cdb[0]) { 5837 case WRITE_SAME_10: { 5838 struct scsi_write_same_10 *cdb; 5839 5840 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5841 5842 lba = scsi_4btoul(cdb->addr); 5843 num_blocks = scsi_2btoul(cdb->length); 5844 byte2 = cdb->byte2; 5845 break; 5846 } 5847 case WRITE_SAME_16: { 5848 struct scsi_write_same_16 *cdb; 5849 5850 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5851 5852 lba = scsi_8btou64(cdb->addr); 5853 num_blocks = scsi_4btoul(cdb->length); 5854 byte2 = cdb->byte2; 5855 break; 5856 } 5857 default: 5858 /* 5859 * We got a command we don't support. This shouldn't 5860 * happen, commands should be filtered out above us. 5861 */ 5862 ctl_set_invalid_opcode(ctsio); 5863 ctl_done((union ctl_io *)ctsio); 5864 5865 return (CTL_RETVAL_COMPLETE); 5866 break; /* NOTREACHED */ 5867 } 5868 5869 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5870 if ((byte2 & SWS_UNMAP) == 0 && 5871 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5872 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5873 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5874 ctl_done((union ctl_io *)ctsio); 5875 return (CTL_RETVAL_COMPLETE); 5876 } 5877 5878 /* 5879 * The first check is to make sure we're in bounds, the second 5880 * check is to catch wrap-around problems. If the lba + num blocks 5881 * is less than the lba, then we've wrapped around and the block 5882 * range is invalid anyway. 5883 */ 5884 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5885 || ((lba + num_blocks) < lba)) { 5886 ctl_set_lba_out_of_range(ctsio); 5887 ctl_done((union ctl_io *)ctsio); 5888 return (CTL_RETVAL_COMPLETE); 5889 } 5890 5891 /* Zero number of blocks means "to the last logical block" */ 5892 if (num_blocks == 0) { 5893 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5894 ctl_set_invalid_field(ctsio, 5895 /*sks_valid*/ 0, 5896 /*command*/ 1, 5897 /*field*/ 0, 5898 /*bit_valid*/ 0, 5899 /*bit*/ 0); 5900 ctl_done((union ctl_io *)ctsio); 5901 return (CTL_RETVAL_COMPLETE); 5902 } 5903 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5904 } 5905 5906 len = lun->be_lun->blocksize; 5907 5908 /* 5909 * If we've got a kernel request that hasn't been malloced yet, 5910 * malloc it and tell the caller the data buffer is here. 5911 */ 5912 if ((byte2 & SWS_NDOB) == 0 && 5913 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5914 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5915 ctsio->kern_data_len = len; 5916 ctsio->kern_total_len = len; 5917 ctsio->kern_data_resid = 0; 5918 ctsio->kern_rel_offset = 0; 5919 ctsio->kern_sg_entries = 0; 5920 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5921 ctsio->be_move_done = ctl_config_move_done; 5922 ctl_datamove((union ctl_io *)ctsio); 5923 5924 return (CTL_RETVAL_COMPLETE); 5925 } 5926 5927 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5928 lbalen->lba = lba; 5929 lbalen->len = num_blocks; 5930 lbalen->flags = byte2; 5931 retval = lun->backend->config_write((union ctl_io *)ctsio); 5932 5933 return (retval); 5934} 5935 5936int 5937ctl_unmap(struct ctl_scsiio *ctsio) 5938{ 5939 struct ctl_lun *lun; 5940 struct scsi_unmap *cdb; 5941 struct ctl_ptr_len_flags *ptrlen; 5942 struct scsi_unmap_header *hdr; 5943 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5944 uint64_t lba; 5945 uint32_t num_blocks; 5946 int len, retval; 5947 uint8_t byte2; 5948 5949 retval = CTL_RETVAL_COMPLETE; 5950 5951 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5952 5953 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5954 cdb = (struct scsi_unmap *)ctsio->cdb; 5955 5956 len = scsi_2btoul(cdb->length); 5957 byte2 = cdb->byte2; 5958 5959 /* 5960 * If we've got a kernel request that hasn't been malloced yet, 5961 * malloc it and tell the caller the data buffer is here. 5962 */ 5963 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5964 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5965 ctsio->kern_data_len = len; 5966 ctsio->kern_total_len = len; 5967 ctsio->kern_data_resid = 0; 5968 ctsio->kern_rel_offset = 0; 5969 ctsio->kern_sg_entries = 0; 5970 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5971 ctsio->be_move_done = ctl_config_move_done; 5972 ctl_datamove((union ctl_io *)ctsio); 5973 5974 return (CTL_RETVAL_COMPLETE); 5975 } 5976 5977 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5978 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5979 if (len < sizeof (*hdr) || 5980 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5981 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5982 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5983 ctl_set_invalid_field(ctsio, 5984 /*sks_valid*/ 0, 5985 /*command*/ 0, 5986 /*field*/ 0, 5987 /*bit_valid*/ 0, 5988 /*bit*/ 0); 5989 goto done; 5990 } 5991 len = scsi_2btoul(hdr->desc_length); 5992 buf = (struct scsi_unmap_desc *)(hdr + 1); 5993 end = buf + len / sizeof(*buf); 5994 5995 endnz = buf; 5996 for (range = buf; range < end; range++) { 5997 lba = scsi_8btou64(range->lba); 5998 num_blocks = scsi_4btoul(range->length); 5999 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6000 || ((lba + num_blocks) < lba)) { 6001 ctl_set_lba_out_of_range(ctsio); 6002 ctl_done((union ctl_io *)ctsio); 6003 return (CTL_RETVAL_COMPLETE); 6004 } 6005 if (num_blocks != 0) 6006 endnz = range + 1; 6007 } 6008 6009 /* 6010 * Block backend can not handle zero last range. 6011 * Filter it out and return if there is nothing left. 6012 */ 6013 len = (uint8_t *)endnz - (uint8_t *)buf; 6014 if (len == 0) { 6015 ctl_set_success(ctsio); 6016 goto done; 6017 } 6018 6019 mtx_lock(&lun->lun_lock); 6020 ptrlen = (struct ctl_ptr_len_flags *) 6021 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6022 ptrlen->ptr = (void *)buf; 6023 ptrlen->len = len; 6024 ptrlen->flags = byte2; 6025 ctl_check_blocked(lun); 6026 mtx_unlock(&lun->lun_lock); 6027 6028 retval = lun->backend->config_write((union ctl_io *)ctsio); 6029 return (retval); 6030 6031done: 6032 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6033 free(ctsio->kern_data_ptr, M_CTL); 6034 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6035 } 6036 ctl_done((union ctl_io *)ctsio); 6037 return (CTL_RETVAL_COMPLETE); 6038} 6039 6040/* 6041 * Note that this function currently doesn't actually do anything inside 6042 * CTL to enforce things if the DQue bit is turned on. 6043 * 6044 * Also note that this function can't be used in the default case, because 6045 * the DQue bit isn't set in the changeable mask for the control mode page 6046 * anyway. This is just here as an example for how to implement a page 6047 * handler, and a placeholder in case we want to allow the user to turn 6048 * tagged queueing on and off. 6049 * 6050 * The D_SENSE bit handling is functional, however, and will turn 6051 * descriptor sense on and off for a given LUN. 6052 */ 6053int 6054ctl_control_page_handler(struct ctl_scsiio *ctsio, 6055 struct ctl_page_index *page_index, uint8_t *page_ptr) 6056{ 6057 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6058 struct ctl_lun *lun; 6059 int set_ua; 6060 uint32_t initidx; 6061 6062 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6063 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6064 set_ua = 0; 6065 6066 user_cp = (struct scsi_control_page *)page_ptr; 6067 current_cp = (struct scsi_control_page *) 6068 (page_index->page_data + (page_index->page_len * 6069 CTL_PAGE_CURRENT)); 6070 saved_cp = (struct scsi_control_page *) 6071 (page_index->page_data + (page_index->page_len * 6072 CTL_PAGE_SAVED)); 6073 6074 mtx_lock(&lun->lun_lock); 6075 if (((current_cp->rlec & SCP_DSENSE) == 0) 6076 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6077 /* 6078 * Descriptor sense is currently turned off and the user 6079 * wants to turn it on. 6080 */ 6081 current_cp->rlec |= SCP_DSENSE; 6082 saved_cp->rlec |= SCP_DSENSE; 6083 lun->flags |= CTL_LUN_SENSE_DESC; 6084 set_ua = 1; 6085 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6086 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6087 /* 6088 * Descriptor sense is currently turned on, and the user 6089 * wants to turn it off. 6090 */ 6091 current_cp->rlec &= ~SCP_DSENSE; 6092 saved_cp->rlec &= ~SCP_DSENSE; 6093 lun->flags &= ~CTL_LUN_SENSE_DESC; 6094 set_ua = 1; 6095 } 6096 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6097 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6098 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6099 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6100 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6101 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6102 set_ua = 1; 6103 } 6104 if ((current_cp->eca_and_aen & SCP_SWP) != 6105 (user_cp->eca_and_aen & SCP_SWP)) { 6106 current_cp->eca_and_aen &= ~SCP_SWP; 6107 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6108 saved_cp->eca_and_aen &= ~SCP_SWP; 6109 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6110 set_ua = 1; 6111 } 6112 if (set_ua != 0) 6113 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6114 mtx_unlock(&lun->lun_lock); 6115 6116 return (0); 6117} 6118 6119int 6120ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6121 struct ctl_page_index *page_index, uint8_t *page_ptr) 6122{ 6123 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6124 struct ctl_lun *lun; 6125 int set_ua; 6126 uint32_t initidx; 6127 6128 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6129 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6130 set_ua = 0; 6131 6132 user_cp = (struct scsi_caching_page *)page_ptr; 6133 current_cp = (struct scsi_caching_page *) 6134 (page_index->page_data + (page_index->page_len * 6135 CTL_PAGE_CURRENT)); 6136 saved_cp = (struct scsi_caching_page *) 6137 (page_index->page_data + (page_index->page_len * 6138 CTL_PAGE_SAVED)); 6139 6140 mtx_lock(&lun->lun_lock); 6141 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6142 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6143 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6144 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6145 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6146 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6147 set_ua = 1; 6148 } 6149 if (set_ua != 0) 6150 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6151 mtx_unlock(&lun->lun_lock); 6152 6153 return (0); 6154} 6155 6156int 6157ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6158 struct ctl_page_index *page_index, 6159 uint8_t *page_ptr) 6160{ 6161 uint8_t *c; 6162 int i; 6163 6164 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6165 ctl_time_io_secs = 6166 (c[0] << 8) | 6167 (c[1] << 0) | 6168 0; 6169 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6170 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6171 printf("page data:"); 6172 for (i=0; i<8; i++) 6173 printf(" %.2x",page_ptr[i]); 6174 printf("\n"); 6175 return (0); 6176} 6177 6178int 6179ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6180 struct ctl_page_index *page_index, 6181 int pc) 6182{ 6183 struct copan_debugconf_subpage *page; 6184 6185 page = (struct copan_debugconf_subpage *)page_index->page_data + 6186 (page_index->page_len * pc); 6187 6188 switch (pc) { 6189 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6190 case SMS_PAGE_CTRL_DEFAULT >> 6: 6191 case SMS_PAGE_CTRL_SAVED >> 6: 6192 /* 6193 * We don't update the changable or default bits for this page. 6194 */ 6195 break; 6196 case SMS_PAGE_CTRL_CURRENT >> 6: 6197 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6198 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6199 break; 6200 default: 6201#ifdef NEEDTOPORT 6202 EPRINT(0, "Invalid PC %d!!", pc); 6203#endif /* NEEDTOPORT */ 6204 break; 6205 } 6206 return (0); 6207} 6208 6209 6210static int 6211ctl_do_mode_select(union ctl_io *io) 6212{ 6213 struct scsi_mode_page_header *page_header; 6214 struct ctl_page_index *page_index; 6215 struct ctl_scsiio *ctsio; 6216 int control_dev, page_len; 6217 int page_len_offset, page_len_size; 6218 union ctl_modepage_info *modepage_info; 6219 struct ctl_lun *lun; 6220 int *len_left, *len_used; 6221 int retval, i; 6222 6223 ctsio = &io->scsiio; 6224 page_index = NULL; 6225 page_len = 0; 6226 retval = CTL_RETVAL_COMPLETE; 6227 6228 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6229 6230 if (lun->be_lun->lun_type != T_DIRECT) 6231 control_dev = 1; 6232 else 6233 control_dev = 0; 6234 6235 modepage_info = (union ctl_modepage_info *) 6236 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6237 len_left = &modepage_info->header.len_left; 6238 len_used = &modepage_info->header.len_used; 6239 6240do_next_page: 6241 6242 page_header = (struct scsi_mode_page_header *) 6243 (ctsio->kern_data_ptr + *len_used); 6244 6245 if (*len_left == 0) { 6246 free(ctsio->kern_data_ptr, M_CTL); 6247 ctl_set_success(ctsio); 6248 ctl_done((union ctl_io *)ctsio); 6249 return (CTL_RETVAL_COMPLETE); 6250 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6251 6252 free(ctsio->kern_data_ptr, M_CTL); 6253 ctl_set_param_len_error(ctsio); 6254 ctl_done((union ctl_io *)ctsio); 6255 return (CTL_RETVAL_COMPLETE); 6256 6257 } else if ((page_header->page_code & SMPH_SPF) 6258 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6259 6260 free(ctsio->kern_data_ptr, M_CTL); 6261 ctl_set_param_len_error(ctsio); 6262 ctl_done((union ctl_io *)ctsio); 6263 return (CTL_RETVAL_COMPLETE); 6264 } 6265 6266 6267 /* 6268 * XXX KDM should we do something with the block descriptor? 6269 */ 6270 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6271 6272 if ((control_dev != 0) 6273 && (lun->mode_pages.index[i].page_flags & 6274 CTL_PAGE_FLAG_DISK_ONLY)) 6275 continue; 6276 6277 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6278 (page_header->page_code & SMPH_PC_MASK)) 6279 continue; 6280 6281 /* 6282 * If neither page has a subpage code, then we've got a 6283 * match. 6284 */ 6285 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6286 && ((page_header->page_code & SMPH_SPF) == 0)) { 6287 page_index = &lun->mode_pages.index[i]; 6288 page_len = page_header->page_length; 6289 break; 6290 } 6291 6292 /* 6293 * If both pages have subpages, then the subpage numbers 6294 * have to match. 6295 */ 6296 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6297 && (page_header->page_code & SMPH_SPF)) { 6298 struct scsi_mode_page_header_sp *sph; 6299 6300 sph = (struct scsi_mode_page_header_sp *)page_header; 6301 6302 if (lun->mode_pages.index[i].subpage == 6303 sph->subpage) { 6304 page_index = &lun->mode_pages.index[i]; 6305 page_len = scsi_2btoul(sph->page_length); 6306 break; 6307 } 6308 } 6309 } 6310 6311 /* 6312 * If we couldn't find the page, or if we don't have a mode select 6313 * handler for it, send back an error to the user. 6314 */ 6315 if ((page_index == NULL) 6316 || (page_index->select_handler == NULL)) { 6317 ctl_set_invalid_field(ctsio, 6318 /*sks_valid*/ 1, 6319 /*command*/ 0, 6320 /*field*/ *len_used, 6321 /*bit_valid*/ 0, 6322 /*bit*/ 0); 6323 free(ctsio->kern_data_ptr, M_CTL); 6324 ctl_done((union ctl_io *)ctsio); 6325 return (CTL_RETVAL_COMPLETE); 6326 } 6327 6328 if (page_index->page_code & SMPH_SPF) { 6329 page_len_offset = 2; 6330 page_len_size = 2; 6331 } else { 6332 page_len_size = 1; 6333 page_len_offset = 1; 6334 } 6335 6336 /* 6337 * If the length the initiator gives us isn't the one we specify in 6338 * the mode page header, or if they didn't specify enough data in 6339 * the CDB to avoid truncating this page, kick out the request. 6340 */ 6341 if ((page_len != (page_index->page_len - page_len_offset - 6342 page_len_size)) 6343 || (*len_left < page_index->page_len)) { 6344 6345 6346 ctl_set_invalid_field(ctsio, 6347 /*sks_valid*/ 1, 6348 /*command*/ 0, 6349 /*field*/ *len_used + page_len_offset, 6350 /*bit_valid*/ 0, 6351 /*bit*/ 0); 6352 free(ctsio->kern_data_ptr, M_CTL); 6353 ctl_done((union ctl_io *)ctsio); 6354 return (CTL_RETVAL_COMPLETE); 6355 } 6356 6357 /* 6358 * Run through the mode page, checking to make sure that the bits 6359 * the user changed are actually legal for him to change. 6360 */ 6361 for (i = 0; i < page_index->page_len; i++) { 6362 uint8_t *user_byte, *change_mask, *current_byte; 6363 int bad_bit; 6364 int j; 6365 6366 user_byte = (uint8_t *)page_header + i; 6367 change_mask = page_index->page_data + 6368 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6369 current_byte = page_index->page_data + 6370 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6371 6372 /* 6373 * Check to see whether the user set any bits in this byte 6374 * that he is not allowed to set. 6375 */ 6376 if ((*user_byte & ~(*change_mask)) == 6377 (*current_byte & ~(*change_mask))) 6378 continue; 6379 6380 /* 6381 * Go through bit by bit to determine which one is illegal. 6382 */ 6383 bad_bit = 0; 6384 for (j = 7; j >= 0; j--) { 6385 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6386 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6387 bad_bit = i; 6388 break; 6389 } 6390 } 6391 ctl_set_invalid_field(ctsio, 6392 /*sks_valid*/ 1, 6393 /*command*/ 0, 6394 /*field*/ *len_used + i, 6395 /*bit_valid*/ 1, 6396 /*bit*/ bad_bit); 6397 free(ctsio->kern_data_ptr, M_CTL); 6398 ctl_done((union ctl_io *)ctsio); 6399 return (CTL_RETVAL_COMPLETE); 6400 } 6401 6402 /* 6403 * Decrement these before we call the page handler, since we may 6404 * end up getting called back one way or another before the handler 6405 * returns to this context. 6406 */ 6407 *len_left -= page_index->page_len; 6408 *len_used += page_index->page_len; 6409 6410 retval = page_index->select_handler(ctsio, page_index, 6411 (uint8_t *)page_header); 6412 6413 /* 6414 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6415 * wait until this queued command completes to finish processing 6416 * the mode page. If it returns anything other than 6417 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6418 * already set the sense information, freed the data pointer, and 6419 * completed the io for us. 6420 */ 6421 if (retval != CTL_RETVAL_COMPLETE) 6422 goto bailout_no_done; 6423 6424 /* 6425 * If the initiator sent us more than one page, parse the next one. 6426 */ 6427 if (*len_left > 0) 6428 goto do_next_page; 6429 6430 ctl_set_success(ctsio); 6431 free(ctsio->kern_data_ptr, M_CTL); 6432 ctl_done((union ctl_io *)ctsio); 6433 6434bailout_no_done: 6435 6436 return (CTL_RETVAL_COMPLETE); 6437 6438} 6439 6440int 6441ctl_mode_select(struct ctl_scsiio *ctsio) 6442{ 6443 int param_len, pf, sp; 6444 int header_size, bd_len; 6445 int len_left, len_used; 6446 struct ctl_page_index *page_index; 6447 struct ctl_lun *lun; 6448 int control_dev, page_len; 6449 union ctl_modepage_info *modepage_info; 6450 int retval; 6451 6452 pf = 0; 6453 sp = 0; 6454 page_len = 0; 6455 len_used = 0; 6456 len_left = 0; 6457 retval = 0; 6458 bd_len = 0; 6459 page_index = NULL; 6460 6461 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6462 6463 if (lun->be_lun->lun_type != T_DIRECT) 6464 control_dev = 1; 6465 else 6466 control_dev = 0; 6467 6468 switch (ctsio->cdb[0]) { 6469 case MODE_SELECT_6: { 6470 struct scsi_mode_select_6 *cdb; 6471 6472 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6473 6474 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6475 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6476 6477 param_len = cdb->length; 6478 header_size = sizeof(struct scsi_mode_header_6); 6479 break; 6480 } 6481 case MODE_SELECT_10: { 6482 struct scsi_mode_select_10 *cdb; 6483 6484 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6485 6486 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6487 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6488 6489 param_len = scsi_2btoul(cdb->length); 6490 header_size = sizeof(struct scsi_mode_header_10); 6491 break; 6492 } 6493 default: 6494 ctl_set_invalid_opcode(ctsio); 6495 ctl_done((union ctl_io *)ctsio); 6496 return (CTL_RETVAL_COMPLETE); 6497 break; /* NOTREACHED */ 6498 } 6499 6500 /* 6501 * From SPC-3: 6502 * "A parameter list length of zero indicates that the Data-Out Buffer 6503 * shall be empty. This condition shall not be considered as an error." 6504 */ 6505 if (param_len == 0) { 6506 ctl_set_success(ctsio); 6507 ctl_done((union ctl_io *)ctsio); 6508 return (CTL_RETVAL_COMPLETE); 6509 } 6510 6511 /* 6512 * Since we'll hit this the first time through, prior to 6513 * allocation, we don't need to free a data buffer here. 6514 */ 6515 if (param_len < header_size) { 6516 ctl_set_param_len_error(ctsio); 6517 ctl_done((union ctl_io *)ctsio); 6518 return (CTL_RETVAL_COMPLETE); 6519 } 6520 6521 /* 6522 * Allocate the data buffer and grab the user's data. In theory, 6523 * we shouldn't have to sanity check the parameter list length here 6524 * because the maximum size is 64K. We should be able to malloc 6525 * that much without too many problems. 6526 */ 6527 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6528 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6529 ctsio->kern_data_len = param_len; 6530 ctsio->kern_total_len = param_len; 6531 ctsio->kern_data_resid = 0; 6532 ctsio->kern_rel_offset = 0; 6533 ctsio->kern_sg_entries = 0; 6534 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6535 ctsio->be_move_done = ctl_config_move_done; 6536 ctl_datamove((union ctl_io *)ctsio); 6537 6538 return (CTL_RETVAL_COMPLETE); 6539 } 6540 6541 switch (ctsio->cdb[0]) { 6542 case MODE_SELECT_6: { 6543 struct scsi_mode_header_6 *mh6; 6544 6545 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6546 bd_len = mh6->blk_desc_len; 6547 break; 6548 } 6549 case MODE_SELECT_10: { 6550 struct scsi_mode_header_10 *mh10; 6551 6552 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6553 bd_len = scsi_2btoul(mh10->blk_desc_len); 6554 break; 6555 } 6556 default: 6557 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6558 break; 6559 } 6560 6561 if (param_len < (header_size + bd_len)) { 6562 free(ctsio->kern_data_ptr, M_CTL); 6563 ctl_set_param_len_error(ctsio); 6564 ctl_done((union ctl_io *)ctsio); 6565 return (CTL_RETVAL_COMPLETE); 6566 } 6567 6568 /* 6569 * Set the IO_CONT flag, so that if this I/O gets passed to 6570 * ctl_config_write_done(), it'll get passed back to 6571 * ctl_do_mode_select() for further processing, or completion if 6572 * we're all done. 6573 */ 6574 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6575 ctsio->io_cont = ctl_do_mode_select; 6576 6577 modepage_info = (union ctl_modepage_info *) 6578 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6579 6580 memset(modepage_info, 0, sizeof(*modepage_info)); 6581 6582 len_left = param_len - header_size - bd_len; 6583 len_used = header_size + bd_len; 6584 6585 modepage_info->header.len_left = len_left; 6586 modepage_info->header.len_used = len_used; 6587 6588 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6589} 6590 6591int 6592ctl_mode_sense(struct ctl_scsiio *ctsio) 6593{ 6594 struct ctl_lun *lun; 6595 int pc, page_code, dbd, llba, subpage; 6596 int alloc_len, page_len, header_len, total_len; 6597 struct scsi_mode_block_descr *block_desc; 6598 struct ctl_page_index *page_index; 6599 int control_dev; 6600 6601 dbd = 0; 6602 llba = 0; 6603 block_desc = NULL; 6604 page_index = NULL; 6605 6606 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6607 6608 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6609 6610 if (lun->be_lun->lun_type != T_DIRECT) 6611 control_dev = 1; 6612 else 6613 control_dev = 0; 6614 6615 switch (ctsio->cdb[0]) { 6616 case MODE_SENSE_6: { 6617 struct scsi_mode_sense_6 *cdb; 6618 6619 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6620 6621 header_len = sizeof(struct scsi_mode_hdr_6); 6622 if (cdb->byte2 & SMS_DBD) 6623 dbd = 1; 6624 else 6625 header_len += sizeof(struct scsi_mode_block_descr); 6626 6627 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6628 page_code = cdb->page & SMS_PAGE_CODE; 6629 subpage = cdb->subpage; 6630 alloc_len = cdb->length; 6631 break; 6632 } 6633 case MODE_SENSE_10: { 6634 struct scsi_mode_sense_10 *cdb; 6635 6636 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6637 6638 header_len = sizeof(struct scsi_mode_hdr_10); 6639 6640 if (cdb->byte2 & SMS_DBD) 6641 dbd = 1; 6642 else 6643 header_len += sizeof(struct scsi_mode_block_descr); 6644 if (cdb->byte2 & SMS10_LLBAA) 6645 llba = 1; 6646 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6647 page_code = cdb->page & SMS_PAGE_CODE; 6648 subpage = cdb->subpage; 6649 alloc_len = scsi_2btoul(cdb->length); 6650 break; 6651 } 6652 default: 6653 ctl_set_invalid_opcode(ctsio); 6654 ctl_done((union ctl_io *)ctsio); 6655 return (CTL_RETVAL_COMPLETE); 6656 break; /* NOTREACHED */ 6657 } 6658 6659 /* 6660 * We have to make a first pass through to calculate the size of 6661 * the pages that match the user's query. Then we allocate enough 6662 * memory to hold it, and actually copy the data into the buffer. 6663 */ 6664 switch (page_code) { 6665 case SMS_ALL_PAGES_PAGE: { 6666 int i; 6667 6668 page_len = 0; 6669 6670 /* 6671 * At the moment, values other than 0 and 0xff here are 6672 * reserved according to SPC-3. 6673 */ 6674 if ((subpage != SMS_SUBPAGE_PAGE_0) 6675 && (subpage != SMS_SUBPAGE_ALL)) { 6676 ctl_set_invalid_field(ctsio, 6677 /*sks_valid*/ 1, 6678 /*command*/ 1, 6679 /*field*/ 3, 6680 /*bit_valid*/ 0, 6681 /*bit*/ 0); 6682 ctl_done((union ctl_io *)ctsio); 6683 return (CTL_RETVAL_COMPLETE); 6684 } 6685 6686 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6687 if ((control_dev != 0) 6688 && (lun->mode_pages.index[i].page_flags & 6689 CTL_PAGE_FLAG_DISK_ONLY)) 6690 continue; 6691 6692 /* 6693 * We don't use this subpage if the user didn't 6694 * request all subpages. 6695 */ 6696 if ((lun->mode_pages.index[i].subpage != 0) 6697 && (subpage == SMS_SUBPAGE_PAGE_0)) 6698 continue; 6699 6700#if 0 6701 printf("found page %#x len %d\n", 6702 lun->mode_pages.index[i].page_code & 6703 SMPH_PC_MASK, 6704 lun->mode_pages.index[i].page_len); 6705#endif 6706 page_len += lun->mode_pages.index[i].page_len; 6707 } 6708 break; 6709 } 6710 default: { 6711 int i; 6712 6713 page_len = 0; 6714 6715 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6716 /* Look for the right page code */ 6717 if ((lun->mode_pages.index[i].page_code & 6718 SMPH_PC_MASK) != page_code) 6719 continue; 6720 6721 /* Look for the right subpage or the subpage wildcard*/ 6722 if ((lun->mode_pages.index[i].subpage != subpage) 6723 && (subpage != SMS_SUBPAGE_ALL)) 6724 continue; 6725 6726 /* Make sure the page is supported for this dev type */ 6727 if ((control_dev != 0) 6728 && (lun->mode_pages.index[i].page_flags & 6729 CTL_PAGE_FLAG_DISK_ONLY)) 6730 continue; 6731 6732#if 0 6733 printf("found page %#x len %d\n", 6734 lun->mode_pages.index[i].page_code & 6735 SMPH_PC_MASK, 6736 lun->mode_pages.index[i].page_len); 6737#endif 6738 6739 page_len += lun->mode_pages.index[i].page_len; 6740 } 6741 6742 if (page_len == 0) { 6743 ctl_set_invalid_field(ctsio, 6744 /*sks_valid*/ 1, 6745 /*command*/ 1, 6746 /*field*/ 2, 6747 /*bit_valid*/ 1, 6748 /*bit*/ 5); 6749 ctl_done((union ctl_io *)ctsio); 6750 return (CTL_RETVAL_COMPLETE); 6751 } 6752 break; 6753 } 6754 } 6755 6756 total_len = header_len + page_len; 6757#if 0 6758 printf("header_len = %d, page_len = %d, total_len = %d\n", 6759 header_len, page_len, total_len); 6760#endif 6761 6762 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6763 ctsio->kern_sg_entries = 0; 6764 ctsio->kern_data_resid = 0; 6765 ctsio->kern_rel_offset = 0; 6766 if (total_len < alloc_len) { 6767 ctsio->residual = alloc_len - total_len; 6768 ctsio->kern_data_len = total_len; 6769 ctsio->kern_total_len = total_len; 6770 } else { 6771 ctsio->residual = 0; 6772 ctsio->kern_data_len = alloc_len; 6773 ctsio->kern_total_len = alloc_len; 6774 } 6775 6776 switch (ctsio->cdb[0]) { 6777 case MODE_SENSE_6: { 6778 struct scsi_mode_hdr_6 *header; 6779 6780 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6781 6782 header->datalen = MIN(total_len - 1, 254); 6783 if (control_dev == 0) { 6784 header->dev_specific = 0x10; /* DPOFUA */ 6785 if ((lun->flags & CTL_LUN_READONLY) || 6786 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6787 .eca_and_aen & SCP_SWP) != 0) 6788 header->dev_specific |= 0x80; /* WP */ 6789 } 6790 if (dbd) 6791 header->block_descr_len = 0; 6792 else 6793 header->block_descr_len = 6794 sizeof(struct scsi_mode_block_descr); 6795 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6796 break; 6797 } 6798 case MODE_SENSE_10: { 6799 struct scsi_mode_hdr_10 *header; 6800 int datalen; 6801 6802 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6803 6804 datalen = MIN(total_len - 2, 65533); 6805 scsi_ulto2b(datalen, header->datalen); 6806 if (control_dev == 0) { 6807 header->dev_specific = 0x10; /* DPOFUA */ 6808 if ((lun->flags & CTL_LUN_READONLY) || 6809 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6810 .eca_and_aen & SCP_SWP) != 0) 6811 header->dev_specific |= 0x80; /* WP */ 6812 } 6813 if (dbd) 6814 scsi_ulto2b(0, header->block_descr_len); 6815 else 6816 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6817 header->block_descr_len); 6818 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6819 break; 6820 } 6821 default: 6822 panic("invalid CDB type %#x", ctsio->cdb[0]); 6823 break; /* NOTREACHED */ 6824 } 6825 6826 /* 6827 * If we've got a disk, use its blocksize in the block 6828 * descriptor. Otherwise, just set it to 0. 6829 */ 6830 if (dbd == 0) { 6831 if (control_dev == 0) 6832 scsi_ulto3b(lun->be_lun->blocksize, 6833 block_desc->block_len); 6834 else 6835 scsi_ulto3b(0, block_desc->block_len); 6836 } 6837 6838 switch (page_code) { 6839 case SMS_ALL_PAGES_PAGE: { 6840 int i, data_used; 6841 6842 data_used = header_len; 6843 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6844 struct ctl_page_index *page_index; 6845 6846 page_index = &lun->mode_pages.index[i]; 6847 6848 if ((control_dev != 0) 6849 && (page_index->page_flags & 6850 CTL_PAGE_FLAG_DISK_ONLY)) 6851 continue; 6852 6853 /* 6854 * We don't use this subpage if the user didn't 6855 * request all subpages. We already checked (above) 6856 * to make sure the user only specified a subpage 6857 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6858 */ 6859 if ((page_index->subpage != 0) 6860 && (subpage == SMS_SUBPAGE_PAGE_0)) 6861 continue; 6862 6863 /* 6864 * Call the handler, if it exists, to update the 6865 * page to the latest values. 6866 */ 6867 if (page_index->sense_handler != NULL) 6868 page_index->sense_handler(ctsio, page_index,pc); 6869 6870 memcpy(ctsio->kern_data_ptr + data_used, 6871 page_index->page_data + 6872 (page_index->page_len * pc), 6873 page_index->page_len); 6874 data_used += page_index->page_len; 6875 } 6876 break; 6877 } 6878 default: { 6879 int i, data_used; 6880 6881 data_used = header_len; 6882 6883 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6884 struct ctl_page_index *page_index; 6885 6886 page_index = &lun->mode_pages.index[i]; 6887 6888 /* Look for the right page code */ 6889 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6890 continue; 6891 6892 /* Look for the right subpage or the subpage wildcard*/ 6893 if ((page_index->subpage != subpage) 6894 && (subpage != SMS_SUBPAGE_ALL)) 6895 continue; 6896 6897 /* Make sure the page is supported for this dev type */ 6898 if ((control_dev != 0) 6899 && (page_index->page_flags & 6900 CTL_PAGE_FLAG_DISK_ONLY)) 6901 continue; 6902 6903 /* 6904 * Call the handler, if it exists, to update the 6905 * page to the latest values. 6906 */ 6907 if (page_index->sense_handler != NULL) 6908 page_index->sense_handler(ctsio, page_index,pc); 6909 6910 memcpy(ctsio->kern_data_ptr + data_used, 6911 page_index->page_data + 6912 (page_index->page_len * pc), 6913 page_index->page_len); 6914 data_used += page_index->page_len; 6915 } 6916 break; 6917 } 6918 } 6919 6920 ctl_set_success(ctsio); 6921 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6922 ctsio->be_move_done = ctl_config_move_done; 6923 ctl_datamove((union ctl_io *)ctsio); 6924 return (CTL_RETVAL_COMPLETE); 6925} 6926 6927int 6928ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6929 struct ctl_page_index *page_index, 6930 int pc) 6931{ 6932 struct ctl_lun *lun; 6933 struct scsi_log_param_header *phdr; 6934 uint8_t *data; 6935 uint64_t val; 6936 6937 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6938 data = page_index->page_data; 6939 6940 if (lun->backend->lun_attr != NULL && 6941 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6942 != UINT64_MAX) { 6943 phdr = (struct scsi_log_param_header *)data; 6944 scsi_ulto2b(0x0001, phdr->param_code); 6945 phdr->param_control = SLP_LBIN | SLP_LP; 6946 phdr->param_len = 8; 6947 data = (uint8_t *)(phdr + 1); 6948 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6949 data[4] = 0x02; /* per-pool */ 6950 data += phdr->param_len; 6951 } 6952 6953 if (lun->backend->lun_attr != NULL && 6954 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6955 != UINT64_MAX) { 6956 phdr = (struct scsi_log_param_header *)data; 6957 scsi_ulto2b(0x0002, phdr->param_code); 6958 phdr->param_control = SLP_LBIN | SLP_LP; 6959 phdr->param_len = 8; 6960 data = (uint8_t *)(phdr + 1); 6961 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6962 data[4] = 0x01; /* per-LUN */ 6963 data += phdr->param_len; 6964 } 6965 6966 if (lun->backend->lun_attr != NULL && 6967 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6968 != UINT64_MAX) { 6969 phdr = (struct scsi_log_param_header *)data; 6970 scsi_ulto2b(0x00f1, phdr->param_code); 6971 phdr->param_control = SLP_LBIN | SLP_LP; 6972 phdr->param_len = 8; 6973 data = (uint8_t *)(phdr + 1); 6974 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6975 data[4] = 0x02; /* per-pool */ 6976 data += phdr->param_len; 6977 } 6978 6979 if (lun->backend->lun_attr != NULL && 6980 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6981 != UINT64_MAX) { 6982 phdr = (struct scsi_log_param_header *)data; 6983 scsi_ulto2b(0x00f2, phdr->param_code); 6984 phdr->param_control = SLP_LBIN | SLP_LP; 6985 phdr->param_len = 8; 6986 data = (uint8_t *)(phdr + 1); 6987 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6988 data[4] = 0x02; /* per-pool */ 6989 data += phdr->param_len; 6990 } 6991 6992 page_index->page_len = data - page_index->page_data; 6993 return (0); 6994} 6995 6996int 6997ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6998 struct ctl_page_index *page_index, 6999 int pc) 7000{ 7001 struct ctl_lun *lun; 7002 struct stat_page *data; 7003 uint64_t rn, wn, rb, wb; 7004 struct bintime rt, wt; 7005 int i; 7006 7007 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7008 data = (struct stat_page *)page_index->page_data; 7009 7010 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 7011 data->sap.hdr.param_control = SLP_LBIN; 7012 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 7013 sizeof(struct scsi_log_param_header); 7014 rn = wn = rb = wb = 0; 7015 bintime_clear(&rt); 7016 bintime_clear(&wt); 7017 for (i = 0; i < CTL_MAX_PORTS; i++) { 7018 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 7019 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 7020 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 7021 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 7022 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 7023 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 7024 } 7025 scsi_u64to8b(rn, data->sap.read_num); 7026 scsi_u64to8b(wn, data->sap.write_num); 7027 if (lun->stats.blocksize > 0) { 7028 scsi_u64to8b(wb / lun->stats.blocksize, 7029 data->sap.recvieved_lba); 7030 scsi_u64to8b(rb / lun->stats.blocksize, 7031 data->sap.transmitted_lba); 7032 } 7033 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 7034 data->sap.read_int); 7035 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 7036 data->sap.write_int); 7037 scsi_u64to8b(0, data->sap.weighted_num); 7038 scsi_u64to8b(0, data->sap.weighted_int); 7039 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 7040 data->it.hdr.param_control = SLP_LBIN; 7041 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 7042 sizeof(struct scsi_log_param_header); 7043#ifdef CTL_TIME_IO 7044 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 7045#endif 7046 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 7047 data->it.hdr.param_control = SLP_LBIN; 7048 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 7049 sizeof(struct scsi_log_param_header); 7050 scsi_ulto4b(3, data->ti.exponent); 7051 scsi_ulto4b(1, data->ti.integer); 7052 7053 page_index->page_len = sizeof(*data); 7054 return (0); 7055} 7056 7057int 7058ctl_log_sense(struct ctl_scsiio *ctsio) 7059{ 7060 struct ctl_lun *lun; 7061 int i, pc, page_code, subpage; 7062 int alloc_len, total_len; 7063 struct ctl_page_index *page_index; 7064 struct scsi_log_sense *cdb; 7065 struct scsi_log_header *header; 7066 7067 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7068 7069 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7070 cdb = (struct scsi_log_sense *)ctsio->cdb; 7071 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7072 page_code = cdb->page & SLS_PAGE_CODE; 7073 subpage = cdb->subpage; 7074 alloc_len = scsi_2btoul(cdb->length); 7075 7076 page_index = NULL; 7077 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7078 page_index = &lun->log_pages.index[i]; 7079 7080 /* Look for the right page code */ 7081 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7082 continue; 7083 7084 /* Look for the right subpage or the subpage wildcard*/ 7085 if (page_index->subpage != subpage) 7086 continue; 7087 7088 break; 7089 } 7090 if (i >= CTL_NUM_LOG_PAGES) { 7091 ctl_set_invalid_field(ctsio, 7092 /*sks_valid*/ 1, 7093 /*command*/ 1, 7094 /*field*/ 2, 7095 /*bit_valid*/ 0, 7096 /*bit*/ 0); 7097 ctl_done((union ctl_io *)ctsio); 7098 return (CTL_RETVAL_COMPLETE); 7099 } 7100 7101 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7102 7103 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7104 ctsio->kern_sg_entries = 0; 7105 ctsio->kern_data_resid = 0; 7106 ctsio->kern_rel_offset = 0; 7107 if (total_len < alloc_len) { 7108 ctsio->residual = alloc_len - total_len; 7109 ctsio->kern_data_len = total_len; 7110 ctsio->kern_total_len = total_len; 7111 } else { 7112 ctsio->residual = 0; 7113 ctsio->kern_data_len = alloc_len; 7114 ctsio->kern_total_len = alloc_len; 7115 } 7116 7117 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7118 header->page = page_index->page_code; 7119 if (page_index->subpage) { 7120 header->page |= SL_SPF; 7121 header->subpage = page_index->subpage; 7122 } 7123 scsi_ulto2b(page_index->page_len, header->datalen); 7124 7125 /* 7126 * Call the handler, if it exists, to update the 7127 * page to the latest values. 7128 */ 7129 if (page_index->sense_handler != NULL) 7130 page_index->sense_handler(ctsio, page_index, pc); 7131 7132 memcpy(header + 1, page_index->page_data, page_index->page_len); 7133 7134 ctl_set_success(ctsio); 7135 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7136 ctsio->be_move_done = ctl_config_move_done; 7137 ctl_datamove((union ctl_io *)ctsio); 7138 return (CTL_RETVAL_COMPLETE); 7139} 7140 7141int 7142ctl_read_capacity(struct ctl_scsiio *ctsio) 7143{ 7144 struct scsi_read_capacity *cdb; 7145 struct scsi_read_capacity_data *data; 7146 struct ctl_lun *lun; 7147 uint32_t lba; 7148 7149 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7150 7151 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7152 7153 lba = scsi_4btoul(cdb->addr); 7154 if (((cdb->pmi & SRC_PMI) == 0) 7155 && (lba != 0)) { 7156 ctl_set_invalid_field(/*ctsio*/ ctsio, 7157 /*sks_valid*/ 1, 7158 /*command*/ 1, 7159 /*field*/ 2, 7160 /*bit_valid*/ 0, 7161 /*bit*/ 0); 7162 ctl_done((union ctl_io *)ctsio); 7163 return (CTL_RETVAL_COMPLETE); 7164 } 7165 7166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7167 7168 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7169 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7170 ctsio->residual = 0; 7171 ctsio->kern_data_len = sizeof(*data); 7172 ctsio->kern_total_len = sizeof(*data); 7173 ctsio->kern_data_resid = 0; 7174 ctsio->kern_rel_offset = 0; 7175 ctsio->kern_sg_entries = 0; 7176 7177 /* 7178 * If the maximum LBA is greater than 0xfffffffe, the user must 7179 * issue a SERVICE ACTION IN (16) command, with the read capacity 7180 * serivce action set. 7181 */ 7182 if (lun->be_lun->maxlba > 0xfffffffe) 7183 scsi_ulto4b(0xffffffff, data->addr); 7184 else 7185 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7186 7187 /* 7188 * XXX KDM this may not be 512 bytes... 7189 */ 7190 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7191 7192 ctl_set_success(ctsio); 7193 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7194 ctsio->be_move_done = ctl_config_move_done; 7195 ctl_datamove((union ctl_io *)ctsio); 7196 return (CTL_RETVAL_COMPLETE); 7197} 7198 7199int 7200ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7201{ 7202 struct scsi_read_capacity_16 *cdb; 7203 struct scsi_read_capacity_data_long *data; 7204 struct ctl_lun *lun; 7205 uint64_t lba; 7206 uint32_t alloc_len; 7207 7208 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7209 7210 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7211 7212 alloc_len = scsi_4btoul(cdb->alloc_len); 7213 lba = scsi_8btou64(cdb->addr); 7214 7215 if ((cdb->reladr & SRC16_PMI) 7216 && (lba != 0)) { 7217 ctl_set_invalid_field(/*ctsio*/ ctsio, 7218 /*sks_valid*/ 1, 7219 /*command*/ 1, 7220 /*field*/ 2, 7221 /*bit_valid*/ 0, 7222 /*bit*/ 0); 7223 ctl_done((union ctl_io *)ctsio); 7224 return (CTL_RETVAL_COMPLETE); 7225 } 7226 7227 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7228 7229 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7230 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7231 7232 if (sizeof(*data) < alloc_len) { 7233 ctsio->residual = alloc_len - sizeof(*data); 7234 ctsio->kern_data_len = sizeof(*data); 7235 ctsio->kern_total_len = sizeof(*data); 7236 } else { 7237 ctsio->residual = 0; 7238 ctsio->kern_data_len = alloc_len; 7239 ctsio->kern_total_len = alloc_len; 7240 } 7241 ctsio->kern_data_resid = 0; 7242 ctsio->kern_rel_offset = 0; 7243 ctsio->kern_sg_entries = 0; 7244 7245 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7246 /* XXX KDM this may not be 512 bytes... */ 7247 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7248 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7249 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7250 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7251 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7252 7253 ctl_set_success(ctsio); 7254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7255 ctsio->be_move_done = ctl_config_move_done; 7256 ctl_datamove((union ctl_io *)ctsio); 7257 return (CTL_RETVAL_COMPLETE); 7258} 7259 7260int 7261ctl_get_lba_status(struct ctl_scsiio *ctsio) 7262{ 7263 struct scsi_get_lba_status *cdb; 7264 struct scsi_get_lba_status_data *data; 7265 struct ctl_lun *lun; 7266 struct ctl_lba_len_flags *lbalen; 7267 uint64_t lba; 7268 uint32_t alloc_len, total_len; 7269 int retval; 7270 7271 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7272 7273 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7274 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7275 lba = scsi_8btou64(cdb->addr); 7276 alloc_len = scsi_4btoul(cdb->alloc_len); 7277 7278 if (lba > lun->be_lun->maxlba) { 7279 ctl_set_lba_out_of_range(ctsio); 7280 ctl_done((union ctl_io *)ctsio); 7281 return (CTL_RETVAL_COMPLETE); 7282 } 7283 7284 total_len = sizeof(*data) + sizeof(data->descr[0]); 7285 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7286 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7287 7288 if (total_len < alloc_len) { 7289 ctsio->residual = alloc_len - total_len; 7290 ctsio->kern_data_len = total_len; 7291 ctsio->kern_total_len = total_len; 7292 } else { 7293 ctsio->residual = 0; 7294 ctsio->kern_data_len = alloc_len; 7295 ctsio->kern_total_len = alloc_len; 7296 } 7297 ctsio->kern_data_resid = 0; 7298 ctsio->kern_rel_offset = 0; 7299 ctsio->kern_sg_entries = 0; 7300 7301 /* Fill dummy data in case backend can't tell anything. */ 7302 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7303 scsi_u64to8b(lba, data->descr[0].addr); 7304 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7305 data->descr[0].length); 7306 data->descr[0].status = 0; /* Mapped or unknown. */ 7307 7308 ctl_set_success(ctsio); 7309 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7310 ctsio->be_move_done = ctl_config_move_done; 7311 7312 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7313 lbalen->lba = lba; 7314 lbalen->len = total_len; 7315 lbalen->flags = 0; 7316 retval = lun->backend->config_read((union ctl_io *)ctsio); 7317 return (CTL_RETVAL_COMPLETE); 7318} 7319 7320int 7321ctl_read_defect(struct ctl_scsiio *ctsio) 7322{ 7323 struct scsi_read_defect_data_10 *ccb10; 7324 struct scsi_read_defect_data_12 *ccb12; 7325 struct scsi_read_defect_data_hdr_10 *data10; 7326 struct scsi_read_defect_data_hdr_12 *data12; 7327 uint32_t alloc_len, data_len; 7328 uint8_t format; 7329 7330 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7331 7332 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7333 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7334 format = ccb10->format; 7335 alloc_len = scsi_2btoul(ccb10->alloc_length); 7336 data_len = sizeof(*data10); 7337 } else { 7338 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7339 format = ccb12->format; 7340 alloc_len = scsi_4btoul(ccb12->alloc_length); 7341 data_len = sizeof(*data12); 7342 } 7343 if (alloc_len == 0) { 7344 ctl_set_success(ctsio); 7345 ctl_done((union ctl_io *)ctsio); 7346 return (CTL_RETVAL_COMPLETE); 7347 } 7348 7349 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7350 if (data_len < alloc_len) { 7351 ctsio->residual = alloc_len - data_len; 7352 ctsio->kern_data_len = data_len; 7353 ctsio->kern_total_len = data_len; 7354 } else { 7355 ctsio->residual = 0; 7356 ctsio->kern_data_len = alloc_len; 7357 ctsio->kern_total_len = alloc_len; 7358 } 7359 ctsio->kern_data_resid = 0; 7360 ctsio->kern_rel_offset = 0; 7361 ctsio->kern_sg_entries = 0; 7362 7363 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7364 data10 = (struct scsi_read_defect_data_hdr_10 *) 7365 ctsio->kern_data_ptr; 7366 data10->format = format; 7367 scsi_ulto2b(0, data10->length); 7368 } else { 7369 data12 = (struct scsi_read_defect_data_hdr_12 *) 7370 ctsio->kern_data_ptr; 7371 data12->format = format; 7372 scsi_ulto2b(0, data12->generation); 7373 scsi_ulto4b(0, data12->length); 7374 } 7375 7376 ctl_set_success(ctsio); 7377 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7378 ctsio->be_move_done = ctl_config_move_done; 7379 ctl_datamove((union ctl_io *)ctsio); 7380 return (CTL_RETVAL_COMPLETE); 7381} 7382 7383int 7384ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7385{ 7386 struct scsi_maintenance_in *cdb; 7387 int retval; 7388 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7389 int num_target_port_groups, num_target_ports; 7390 struct ctl_lun *lun; 7391 struct ctl_softc *softc; 7392 struct ctl_port *port; 7393 struct scsi_target_group_data *rtg_ptr; 7394 struct scsi_target_group_data_extended *rtg_ext_ptr; 7395 struct scsi_target_port_group_descriptor *tpg_desc; 7396 7397 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7398 7399 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7400 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7401 softc = lun->ctl_softc; 7402 7403 retval = CTL_RETVAL_COMPLETE; 7404 7405 switch (cdb->byte2 & STG_PDF_MASK) { 7406 case STG_PDF_LENGTH: 7407 ext = 0; 7408 break; 7409 case STG_PDF_EXTENDED: 7410 ext = 1; 7411 break; 7412 default: 7413 ctl_set_invalid_field(/*ctsio*/ ctsio, 7414 /*sks_valid*/ 1, 7415 /*command*/ 1, 7416 /*field*/ 2, 7417 /*bit_valid*/ 1, 7418 /*bit*/ 5); 7419 ctl_done((union ctl_io *)ctsio); 7420 return(retval); 7421 } 7422 7423 if (softc->is_single) 7424 num_target_port_groups = 1; 7425 else 7426 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7427 num_target_ports = 0; 7428 mtx_lock(&softc->ctl_lock); 7429 STAILQ_FOREACH(port, &softc->port_list, links) { 7430 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7431 continue; 7432 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7433 continue; 7434 num_target_ports++; 7435 } 7436 mtx_unlock(&softc->ctl_lock); 7437 7438 if (ext) 7439 total_len = sizeof(struct scsi_target_group_data_extended); 7440 else 7441 total_len = sizeof(struct scsi_target_group_data); 7442 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7443 num_target_port_groups + 7444 sizeof(struct scsi_target_port_descriptor) * 7445 num_target_ports * num_target_port_groups; 7446 7447 alloc_len = scsi_4btoul(cdb->length); 7448 7449 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7450 7451 ctsio->kern_sg_entries = 0; 7452 7453 if (total_len < alloc_len) { 7454 ctsio->residual = alloc_len - total_len; 7455 ctsio->kern_data_len = total_len; 7456 ctsio->kern_total_len = total_len; 7457 } else { 7458 ctsio->residual = 0; 7459 ctsio->kern_data_len = alloc_len; 7460 ctsio->kern_total_len = alloc_len; 7461 } 7462 ctsio->kern_data_resid = 0; 7463 ctsio->kern_rel_offset = 0; 7464 7465 if (ext) { 7466 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7467 ctsio->kern_data_ptr; 7468 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7469 rtg_ext_ptr->format_type = 0x10; 7470 rtg_ext_ptr->implicit_transition_time = 0; 7471 tpg_desc = &rtg_ext_ptr->groups[0]; 7472 } else { 7473 rtg_ptr = (struct scsi_target_group_data *) 7474 ctsio->kern_data_ptr; 7475 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7476 tpg_desc = &rtg_ptr->groups[0]; 7477 } 7478 7479 mtx_lock(&softc->ctl_lock); 7480 pg = softc->port_offset / CTL_MAX_PORTS; 7481 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7482 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7483 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7484 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7485 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7486 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7487 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7488 } else { 7489 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7490 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7491 } 7492 } else { 7493 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7494 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7495 } 7496 for (g = 0; g < num_target_port_groups; g++) { 7497 tpg_desc->pref_state = (g == pg) ? gs : os; 7498 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7499 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7500 tpg_desc->status = TPG_IMPLICIT; 7501 pc = 0; 7502 STAILQ_FOREACH(port, &softc->port_list, links) { 7503 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7504 continue; 7505 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7506 continue; 7507 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7508 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7509 relative_target_port_identifier); 7510 pc++; 7511 } 7512 tpg_desc->target_port_count = pc; 7513 tpg_desc = (struct scsi_target_port_group_descriptor *) 7514 &tpg_desc->descriptors[pc]; 7515 } 7516 mtx_unlock(&softc->ctl_lock); 7517 7518 ctl_set_success(ctsio); 7519 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7520 ctsio->be_move_done = ctl_config_move_done; 7521 ctl_datamove((union ctl_io *)ctsio); 7522 return(retval); 7523} 7524 7525int 7526ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7527{ 7528 struct ctl_lun *lun; 7529 struct scsi_report_supported_opcodes *cdb; 7530 const struct ctl_cmd_entry *entry, *sentry; 7531 struct scsi_report_supported_opcodes_all *all; 7532 struct scsi_report_supported_opcodes_descr *descr; 7533 struct scsi_report_supported_opcodes_one *one; 7534 int retval; 7535 int alloc_len, total_len; 7536 int opcode, service_action, i, j, num; 7537 7538 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7539 7540 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7541 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7542 7543 retval = CTL_RETVAL_COMPLETE; 7544 7545 opcode = cdb->requested_opcode; 7546 service_action = scsi_2btoul(cdb->requested_service_action); 7547 switch (cdb->options & RSO_OPTIONS_MASK) { 7548 case RSO_OPTIONS_ALL: 7549 num = 0; 7550 for (i = 0; i < 256; i++) { 7551 entry = &ctl_cmd_table[i]; 7552 if (entry->flags & CTL_CMD_FLAG_SA5) { 7553 for (j = 0; j < 32; j++) { 7554 sentry = &((const struct ctl_cmd_entry *) 7555 entry->execute)[j]; 7556 if (ctl_cmd_applicable( 7557 lun->be_lun->lun_type, sentry)) 7558 num++; 7559 } 7560 } else { 7561 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7562 entry)) 7563 num++; 7564 } 7565 } 7566 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7567 num * sizeof(struct scsi_report_supported_opcodes_descr); 7568 break; 7569 case RSO_OPTIONS_OC: 7570 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7571 ctl_set_invalid_field(/*ctsio*/ ctsio, 7572 /*sks_valid*/ 1, 7573 /*command*/ 1, 7574 /*field*/ 2, 7575 /*bit_valid*/ 1, 7576 /*bit*/ 2); 7577 ctl_done((union ctl_io *)ctsio); 7578 return (CTL_RETVAL_COMPLETE); 7579 } 7580 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7581 break; 7582 case RSO_OPTIONS_OC_SA: 7583 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7584 service_action >= 32) { 7585 ctl_set_invalid_field(/*ctsio*/ ctsio, 7586 /*sks_valid*/ 1, 7587 /*command*/ 1, 7588 /*field*/ 2, 7589 /*bit_valid*/ 1, 7590 /*bit*/ 2); 7591 ctl_done((union ctl_io *)ctsio); 7592 return (CTL_RETVAL_COMPLETE); 7593 } 7594 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7595 break; 7596 default: 7597 ctl_set_invalid_field(/*ctsio*/ ctsio, 7598 /*sks_valid*/ 1, 7599 /*command*/ 1, 7600 /*field*/ 2, 7601 /*bit_valid*/ 1, 7602 /*bit*/ 2); 7603 ctl_done((union ctl_io *)ctsio); 7604 return (CTL_RETVAL_COMPLETE); 7605 } 7606 7607 alloc_len = scsi_4btoul(cdb->length); 7608 7609 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7610 7611 ctsio->kern_sg_entries = 0; 7612 7613 if (total_len < alloc_len) { 7614 ctsio->residual = alloc_len - total_len; 7615 ctsio->kern_data_len = total_len; 7616 ctsio->kern_total_len = total_len; 7617 } else { 7618 ctsio->residual = 0; 7619 ctsio->kern_data_len = alloc_len; 7620 ctsio->kern_total_len = alloc_len; 7621 } 7622 ctsio->kern_data_resid = 0; 7623 ctsio->kern_rel_offset = 0; 7624 7625 switch (cdb->options & RSO_OPTIONS_MASK) { 7626 case RSO_OPTIONS_ALL: 7627 all = (struct scsi_report_supported_opcodes_all *) 7628 ctsio->kern_data_ptr; 7629 num = 0; 7630 for (i = 0; i < 256; i++) { 7631 entry = &ctl_cmd_table[i]; 7632 if (entry->flags & CTL_CMD_FLAG_SA5) { 7633 for (j = 0; j < 32; j++) { 7634 sentry = &((const struct ctl_cmd_entry *) 7635 entry->execute)[j]; 7636 if (!ctl_cmd_applicable( 7637 lun->be_lun->lun_type, sentry)) 7638 continue; 7639 descr = &all->descr[num++]; 7640 descr->opcode = i; 7641 scsi_ulto2b(j, descr->service_action); 7642 descr->flags = RSO_SERVACTV; 7643 scsi_ulto2b(sentry->length, 7644 descr->cdb_length); 7645 } 7646 } else { 7647 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7648 entry)) 7649 continue; 7650 descr = &all->descr[num++]; 7651 descr->opcode = i; 7652 scsi_ulto2b(0, descr->service_action); 7653 descr->flags = 0; 7654 scsi_ulto2b(entry->length, descr->cdb_length); 7655 } 7656 } 7657 scsi_ulto4b( 7658 num * sizeof(struct scsi_report_supported_opcodes_descr), 7659 all->length); 7660 break; 7661 case RSO_OPTIONS_OC: 7662 one = (struct scsi_report_supported_opcodes_one *) 7663 ctsio->kern_data_ptr; 7664 entry = &ctl_cmd_table[opcode]; 7665 goto fill_one; 7666 case RSO_OPTIONS_OC_SA: 7667 one = (struct scsi_report_supported_opcodes_one *) 7668 ctsio->kern_data_ptr; 7669 entry = &ctl_cmd_table[opcode]; 7670 entry = &((const struct ctl_cmd_entry *) 7671 entry->execute)[service_action]; 7672fill_one: 7673 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7674 one->support = 3; 7675 scsi_ulto2b(entry->length, one->cdb_length); 7676 one->cdb_usage[0] = opcode; 7677 memcpy(&one->cdb_usage[1], entry->usage, 7678 entry->length - 1); 7679 } else 7680 one->support = 1; 7681 break; 7682 } 7683 7684 ctl_set_success(ctsio); 7685 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7686 ctsio->be_move_done = ctl_config_move_done; 7687 ctl_datamove((union ctl_io *)ctsio); 7688 return(retval); 7689} 7690 7691int 7692ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7693{ 7694 struct scsi_report_supported_tmf *cdb; 7695 struct scsi_report_supported_tmf_data *data; 7696 int retval; 7697 int alloc_len, total_len; 7698 7699 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7700 7701 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7702 7703 retval = CTL_RETVAL_COMPLETE; 7704 7705 total_len = sizeof(struct scsi_report_supported_tmf_data); 7706 alloc_len = scsi_4btoul(cdb->length); 7707 7708 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7709 7710 ctsio->kern_sg_entries = 0; 7711 7712 if (total_len < alloc_len) { 7713 ctsio->residual = alloc_len - total_len; 7714 ctsio->kern_data_len = total_len; 7715 ctsio->kern_total_len = total_len; 7716 } else { 7717 ctsio->residual = 0; 7718 ctsio->kern_data_len = alloc_len; 7719 ctsio->kern_total_len = alloc_len; 7720 } 7721 ctsio->kern_data_resid = 0; 7722 ctsio->kern_rel_offset = 0; 7723 7724 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7725 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7726 data->byte2 |= RST_ITNRS; 7727 7728 ctl_set_success(ctsio); 7729 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7730 ctsio->be_move_done = ctl_config_move_done; 7731 ctl_datamove((union ctl_io *)ctsio); 7732 return (retval); 7733} 7734 7735int 7736ctl_report_timestamp(struct ctl_scsiio *ctsio) 7737{ 7738 struct scsi_report_timestamp *cdb; 7739 struct scsi_report_timestamp_data *data; 7740 struct timeval tv; 7741 int64_t timestamp; 7742 int retval; 7743 int alloc_len, total_len; 7744 7745 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7746 7747 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7748 7749 retval = CTL_RETVAL_COMPLETE; 7750 7751 total_len = sizeof(struct scsi_report_timestamp_data); 7752 alloc_len = scsi_4btoul(cdb->length); 7753 7754 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7755 7756 ctsio->kern_sg_entries = 0; 7757 7758 if (total_len < alloc_len) { 7759 ctsio->residual = alloc_len - total_len; 7760 ctsio->kern_data_len = total_len; 7761 ctsio->kern_total_len = total_len; 7762 } else { 7763 ctsio->residual = 0; 7764 ctsio->kern_data_len = alloc_len; 7765 ctsio->kern_total_len = alloc_len; 7766 } 7767 ctsio->kern_data_resid = 0; 7768 ctsio->kern_rel_offset = 0; 7769 7770 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7771 scsi_ulto2b(sizeof(*data) - 2, data->length); 7772 data->origin = RTS_ORIG_OUTSIDE; 7773 getmicrotime(&tv); 7774 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7775 scsi_ulto4b(timestamp >> 16, data->timestamp); 7776 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7777 7778 ctl_set_success(ctsio); 7779 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7780 ctsio->be_move_done = ctl_config_move_done; 7781 ctl_datamove((union ctl_io *)ctsio); 7782 return (retval); 7783} 7784 7785int 7786ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7787{ 7788 struct scsi_per_res_in *cdb; 7789 int alloc_len, total_len = 0; 7790 /* struct scsi_per_res_in_rsrv in_data; */ 7791 struct ctl_lun *lun; 7792 struct ctl_softc *softc; 7793 uint64_t key; 7794 7795 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7796 7797 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7798 7799 alloc_len = scsi_2btoul(cdb->length); 7800 7801 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7802 softc = lun->ctl_softc; 7803 7804retry: 7805 mtx_lock(&lun->lun_lock); 7806 switch (cdb->action) { 7807 case SPRI_RK: /* read keys */ 7808 total_len = sizeof(struct scsi_per_res_in_keys) + 7809 lun->pr_key_count * 7810 sizeof(struct scsi_per_res_key); 7811 break; 7812 case SPRI_RR: /* read reservation */ 7813 if (lun->flags & CTL_LUN_PR_RESERVED) 7814 total_len = sizeof(struct scsi_per_res_in_rsrv); 7815 else 7816 total_len = sizeof(struct scsi_per_res_in_header); 7817 break; 7818 case SPRI_RC: /* report capabilities */ 7819 total_len = sizeof(struct scsi_per_res_cap); 7820 break; 7821 case SPRI_RS: /* read full status */ 7822 total_len = sizeof(struct scsi_per_res_in_header) + 7823 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7824 lun->pr_key_count; 7825 break; 7826 default: 7827 panic("Invalid PR type %x", cdb->action); 7828 } 7829 mtx_unlock(&lun->lun_lock); 7830 7831 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7832 7833 if (total_len < alloc_len) { 7834 ctsio->residual = alloc_len - total_len; 7835 ctsio->kern_data_len = total_len; 7836 ctsio->kern_total_len = total_len; 7837 } else { 7838 ctsio->residual = 0; 7839 ctsio->kern_data_len = alloc_len; 7840 ctsio->kern_total_len = alloc_len; 7841 } 7842 7843 ctsio->kern_data_resid = 0; 7844 ctsio->kern_rel_offset = 0; 7845 ctsio->kern_sg_entries = 0; 7846 7847 mtx_lock(&lun->lun_lock); 7848 switch (cdb->action) { 7849 case SPRI_RK: { // read keys 7850 struct scsi_per_res_in_keys *res_keys; 7851 int i, key_count; 7852 7853 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7854 7855 /* 7856 * We had to drop the lock to allocate our buffer, which 7857 * leaves time for someone to come in with another 7858 * persistent reservation. (That is unlikely, though, 7859 * since this should be the only persistent reservation 7860 * command active right now.) 7861 */ 7862 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7863 (lun->pr_key_count * 7864 sizeof(struct scsi_per_res_key)))){ 7865 mtx_unlock(&lun->lun_lock); 7866 free(ctsio->kern_data_ptr, M_CTL); 7867 printf("%s: reservation length changed, retrying\n", 7868 __func__); 7869 goto retry; 7870 } 7871 7872 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7873 7874 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7875 lun->pr_key_count, res_keys->header.length); 7876 7877 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7878 if ((key = ctl_get_prkey(lun, i)) == 0) 7879 continue; 7880 7881 /* 7882 * We used lun->pr_key_count to calculate the 7883 * size to allocate. If it turns out the number of 7884 * initiators with the registered flag set is 7885 * larger than that (i.e. they haven't been kept in 7886 * sync), we've got a problem. 7887 */ 7888 if (key_count >= lun->pr_key_count) { 7889#ifdef NEEDTOPORT 7890 csevent_log(CSC_CTL | CSC_SHELF_SW | 7891 CTL_PR_ERROR, 7892 csevent_LogType_Fault, 7893 csevent_AlertLevel_Yellow, 7894 csevent_FRU_ShelfController, 7895 csevent_FRU_Firmware, 7896 csevent_FRU_Unknown, 7897 "registered keys %d >= key " 7898 "count %d", key_count, 7899 lun->pr_key_count); 7900#endif 7901 key_count++; 7902 continue; 7903 } 7904 scsi_u64to8b(key, res_keys->keys[key_count].key); 7905 key_count++; 7906 } 7907 break; 7908 } 7909 case SPRI_RR: { // read reservation 7910 struct scsi_per_res_in_rsrv *res; 7911 int tmp_len, header_only; 7912 7913 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7914 7915 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7916 7917 if (lun->flags & CTL_LUN_PR_RESERVED) 7918 { 7919 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7920 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7921 res->header.length); 7922 header_only = 0; 7923 } else { 7924 tmp_len = sizeof(struct scsi_per_res_in_header); 7925 scsi_ulto4b(0, res->header.length); 7926 header_only = 1; 7927 } 7928 7929 /* 7930 * We had to drop the lock to allocate our buffer, which 7931 * leaves time for someone to come in with another 7932 * persistent reservation. (That is unlikely, though, 7933 * since this should be the only persistent reservation 7934 * command active right now.) 7935 */ 7936 if (tmp_len != total_len) { 7937 mtx_unlock(&lun->lun_lock); 7938 free(ctsio->kern_data_ptr, M_CTL); 7939 printf("%s: reservation status changed, retrying\n", 7940 __func__); 7941 goto retry; 7942 } 7943 7944 /* 7945 * No reservation held, so we're done. 7946 */ 7947 if (header_only != 0) 7948 break; 7949 7950 /* 7951 * If the registration is an All Registrants type, the key 7952 * is 0, since it doesn't really matter. 7953 */ 7954 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7955 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7956 res->data.reservation); 7957 } 7958 res->data.scopetype = lun->res_type; 7959 break; 7960 } 7961 case SPRI_RC: //report capabilities 7962 { 7963 struct scsi_per_res_cap *res_cap; 7964 uint16_t type_mask; 7965 7966 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7967 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7968 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7969 type_mask = SPRI_TM_WR_EX_AR | 7970 SPRI_TM_EX_AC_RO | 7971 SPRI_TM_WR_EX_RO | 7972 SPRI_TM_EX_AC | 7973 SPRI_TM_WR_EX | 7974 SPRI_TM_EX_AC_AR; 7975 scsi_ulto2b(type_mask, res_cap->type_mask); 7976 break; 7977 } 7978 case SPRI_RS: { // read full status 7979 struct scsi_per_res_in_full *res_status; 7980 struct scsi_per_res_in_full_desc *res_desc; 7981 struct ctl_port *port; 7982 int i, len; 7983 7984 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7985 7986 /* 7987 * We had to drop the lock to allocate our buffer, which 7988 * leaves time for someone to come in with another 7989 * persistent reservation. (That is unlikely, though, 7990 * since this should be the only persistent reservation 7991 * command active right now.) 7992 */ 7993 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7994 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7995 lun->pr_key_count)){ 7996 mtx_unlock(&lun->lun_lock); 7997 free(ctsio->kern_data_ptr, M_CTL); 7998 printf("%s: reservation length changed, retrying\n", 7999 __func__); 8000 goto retry; 8001 } 8002 8003 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8004 8005 res_desc = &res_status->desc[0]; 8006 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 8007 if ((key = ctl_get_prkey(lun, i)) == 0) 8008 continue; 8009 8010 scsi_u64to8b(key, res_desc->res_key.key); 8011 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8012 (lun->pr_res_idx == i || 8013 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8014 res_desc->flags = SPRI_FULL_R_HOLDER; 8015 res_desc->scopetype = lun->res_type; 8016 } 8017 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8018 res_desc->rel_trgt_port_id); 8019 len = 0; 8020 port = softc->ctl_ports[ 8021 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 8022 if (port != NULL) 8023 len = ctl_create_iid(port, 8024 i % CTL_MAX_INIT_PER_PORT, 8025 res_desc->transport_id); 8026 scsi_ulto4b(len, res_desc->additional_length); 8027 res_desc = (struct scsi_per_res_in_full_desc *) 8028 &res_desc->transport_id[len]; 8029 } 8030 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8031 res_status->header.length); 8032 break; 8033 } 8034 default: 8035 /* 8036 * This is a bug, because we just checked for this above, 8037 * and should have returned an error. 8038 */ 8039 panic("Invalid PR type %x", cdb->action); 8040 break; /* NOTREACHED */ 8041 } 8042 mtx_unlock(&lun->lun_lock); 8043 8044 ctl_set_success(ctsio); 8045 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8046 ctsio->be_move_done = ctl_config_move_done; 8047 ctl_datamove((union ctl_io *)ctsio); 8048 return (CTL_RETVAL_COMPLETE); 8049} 8050 8051static void 8052ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 8053{ 8054 int off = lun->ctl_softc->persis_offset; 8055 8056 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 8057 ctl_est_ua(lun, residx - off, ua); 8058} 8059 8060/* 8061 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8062 * it should return. 8063 */ 8064static int 8065ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8066 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8067 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8068 struct scsi_per_res_out_parms* param) 8069{ 8070 union ctl_ha_msg persis_io; 8071 int retval, i; 8072 int isc_retval; 8073 8074 retval = 0; 8075 8076 mtx_lock(&lun->lun_lock); 8077 if (sa_res_key == 0) { 8078 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8079 /* validate scope and type */ 8080 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8081 SPR_LU_SCOPE) { 8082 mtx_unlock(&lun->lun_lock); 8083 ctl_set_invalid_field(/*ctsio*/ ctsio, 8084 /*sks_valid*/ 1, 8085 /*command*/ 1, 8086 /*field*/ 2, 8087 /*bit_valid*/ 1, 8088 /*bit*/ 4); 8089 ctl_done((union ctl_io *)ctsio); 8090 return (1); 8091 } 8092 8093 if (type>8 || type==2 || type==4 || type==0) { 8094 mtx_unlock(&lun->lun_lock); 8095 ctl_set_invalid_field(/*ctsio*/ ctsio, 8096 /*sks_valid*/ 1, 8097 /*command*/ 1, 8098 /*field*/ 2, 8099 /*bit_valid*/ 1, 8100 /*bit*/ 0); 8101 ctl_done((union ctl_io *)ctsio); 8102 return (1); 8103 } 8104 8105 /* 8106 * Unregister everybody else and build UA for 8107 * them 8108 */ 8109 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8110 if (i == residx || ctl_get_prkey(lun, i) == 0) 8111 continue; 8112 8113 ctl_clr_prkey(lun, i); 8114 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8115 } 8116 lun->pr_key_count = 1; 8117 lun->res_type = type; 8118 if (lun->res_type != SPR_TYPE_WR_EX_AR 8119 && lun->res_type != SPR_TYPE_EX_AC_AR) 8120 lun->pr_res_idx = residx; 8121 8122 /* send msg to other side */ 8123 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8124 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8125 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8126 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8127 persis_io.pr.pr_info.res_type = type; 8128 memcpy(persis_io.pr.pr_info.sa_res_key, 8129 param->serv_act_res_key, 8130 sizeof(param->serv_act_res_key)); 8131 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8132 &persis_io, sizeof(persis_io), 0)) > 8133 CTL_HA_STATUS_SUCCESS) { 8134 printf("CTL:Persis Out error returned " 8135 "from ctl_ha_msg_send %d\n", 8136 isc_retval); 8137 } 8138 } else { 8139 /* not all registrants */ 8140 mtx_unlock(&lun->lun_lock); 8141 free(ctsio->kern_data_ptr, M_CTL); 8142 ctl_set_invalid_field(ctsio, 8143 /*sks_valid*/ 1, 8144 /*command*/ 0, 8145 /*field*/ 8, 8146 /*bit_valid*/ 0, 8147 /*bit*/ 0); 8148 ctl_done((union ctl_io *)ctsio); 8149 return (1); 8150 } 8151 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8152 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8153 int found = 0; 8154 8155 if (res_key == sa_res_key) { 8156 /* special case */ 8157 /* 8158 * The spec implies this is not good but doesn't 8159 * say what to do. There are two choices either 8160 * generate a res conflict or check condition 8161 * with illegal field in parameter data. Since 8162 * that is what is done when the sa_res_key is 8163 * zero I'll take that approach since this has 8164 * to do with the sa_res_key. 8165 */ 8166 mtx_unlock(&lun->lun_lock); 8167 free(ctsio->kern_data_ptr, M_CTL); 8168 ctl_set_invalid_field(ctsio, 8169 /*sks_valid*/ 1, 8170 /*command*/ 0, 8171 /*field*/ 8, 8172 /*bit_valid*/ 0, 8173 /*bit*/ 0); 8174 ctl_done((union ctl_io *)ctsio); 8175 return (1); 8176 } 8177 8178 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8179 if (ctl_get_prkey(lun, i) != sa_res_key) 8180 continue; 8181 8182 found = 1; 8183 ctl_clr_prkey(lun, i); 8184 lun->pr_key_count--; 8185 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8186 } 8187 if (!found) { 8188 mtx_unlock(&lun->lun_lock); 8189 free(ctsio->kern_data_ptr, M_CTL); 8190 ctl_set_reservation_conflict(ctsio); 8191 ctl_done((union ctl_io *)ctsio); 8192 return (CTL_RETVAL_COMPLETE); 8193 } 8194 /* send msg to other side */ 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 from " 8207 "ctl_ha_msg_send %d\n", isc_retval); 8208 } 8209 } else { 8210 /* Reserved but not all registrants */ 8211 /* sa_res_key is res holder */ 8212 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8213 /* validate scope and type */ 8214 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8215 SPR_LU_SCOPE) { 8216 mtx_unlock(&lun->lun_lock); 8217 ctl_set_invalid_field(/*ctsio*/ ctsio, 8218 /*sks_valid*/ 1, 8219 /*command*/ 1, 8220 /*field*/ 2, 8221 /*bit_valid*/ 1, 8222 /*bit*/ 4); 8223 ctl_done((union ctl_io *)ctsio); 8224 return (1); 8225 } 8226 8227 if (type>8 || type==2 || type==4 || type==0) { 8228 mtx_unlock(&lun->lun_lock); 8229 ctl_set_invalid_field(/*ctsio*/ ctsio, 8230 /*sks_valid*/ 1, 8231 /*command*/ 1, 8232 /*field*/ 2, 8233 /*bit_valid*/ 1, 8234 /*bit*/ 0); 8235 ctl_done((union ctl_io *)ctsio); 8236 return (1); 8237 } 8238 8239 /* 8240 * Do the following: 8241 * if sa_res_key != res_key remove all 8242 * registrants w/sa_res_key and generate UA 8243 * for these registrants(Registrations 8244 * Preempted) if it wasn't an exclusive 8245 * reservation generate UA(Reservations 8246 * Preempted) for all other registered nexuses 8247 * if the type has changed. Establish the new 8248 * reservation and holder. If res_key and 8249 * sa_res_key are the same do the above 8250 * except don't unregister the res holder. 8251 */ 8252 8253 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8254 if (i == residx || ctl_get_prkey(lun, i) == 0) 8255 continue; 8256 8257 if (sa_res_key == ctl_get_prkey(lun, i)) { 8258 ctl_clr_prkey(lun, i); 8259 lun->pr_key_count--; 8260 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8261 } else if (type != lun->res_type 8262 && (lun->res_type == SPR_TYPE_WR_EX_RO 8263 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8264 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8265 } 8266 } 8267 lun->res_type = type; 8268 if (lun->res_type != SPR_TYPE_WR_EX_AR 8269 && lun->res_type != SPR_TYPE_EX_AC_AR) 8270 lun->pr_res_idx = residx; 8271 else 8272 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8273 8274 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8275 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8276 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8277 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8278 persis_io.pr.pr_info.res_type = type; 8279 memcpy(persis_io.pr.pr_info.sa_res_key, 8280 param->serv_act_res_key, 8281 sizeof(param->serv_act_res_key)); 8282 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8283 &persis_io, sizeof(persis_io), 0)) > 8284 CTL_HA_STATUS_SUCCESS) { 8285 printf("CTL:Persis Out error returned " 8286 "from ctl_ha_msg_send %d\n", 8287 isc_retval); 8288 } 8289 } else { 8290 /* 8291 * sa_res_key is not the res holder just 8292 * remove registrants 8293 */ 8294 int found=0; 8295 8296 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8297 if (sa_res_key != ctl_get_prkey(lun, i)) 8298 continue; 8299 8300 found = 1; 8301 ctl_clr_prkey(lun, i); 8302 lun->pr_key_count--; 8303 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8304 } 8305 8306 if (!found) { 8307 mtx_unlock(&lun->lun_lock); 8308 free(ctsio->kern_data_ptr, M_CTL); 8309 ctl_set_reservation_conflict(ctsio); 8310 ctl_done((union ctl_io *)ctsio); 8311 return (1); 8312 } 8313 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8314 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8315 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8316 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8317 persis_io.pr.pr_info.res_type = type; 8318 memcpy(persis_io.pr.pr_info.sa_res_key, 8319 param->serv_act_res_key, 8320 sizeof(param->serv_act_res_key)); 8321 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8322 &persis_io, sizeof(persis_io), 0)) > 8323 CTL_HA_STATUS_SUCCESS) { 8324 printf("CTL:Persis Out error returned " 8325 "from ctl_ha_msg_send %d\n", 8326 isc_retval); 8327 } 8328 } 8329 } 8330 8331 lun->PRGeneration++; 8332 mtx_unlock(&lun->lun_lock); 8333 8334 return (retval); 8335} 8336 8337static void 8338ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8339{ 8340 uint64_t sa_res_key; 8341 int i; 8342 8343 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8344 8345 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8346 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8347 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8348 if (sa_res_key == 0) { 8349 /* 8350 * Unregister everybody else and build UA for 8351 * them 8352 */ 8353 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8354 if (i == msg->pr.pr_info.residx || 8355 ctl_get_prkey(lun, i) == 0) 8356 continue; 8357 8358 ctl_clr_prkey(lun, i); 8359 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8360 } 8361 8362 lun->pr_key_count = 1; 8363 lun->res_type = msg->pr.pr_info.res_type; 8364 if (lun->res_type != SPR_TYPE_WR_EX_AR 8365 && lun->res_type != SPR_TYPE_EX_AC_AR) 8366 lun->pr_res_idx = msg->pr.pr_info.residx; 8367 } else { 8368 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8369 if (sa_res_key == ctl_get_prkey(lun, i)) 8370 continue; 8371 8372 ctl_clr_prkey(lun, i); 8373 lun->pr_key_count--; 8374 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8375 } 8376 } 8377 } else { 8378 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8379 if (i == msg->pr.pr_info.residx || 8380 ctl_get_prkey(lun, i) == 0) 8381 continue; 8382 8383 if (sa_res_key == ctl_get_prkey(lun, i)) { 8384 ctl_clr_prkey(lun, i); 8385 lun->pr_key_count--; 8386 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8387 } else if (msg->pr.pr_info.res_type != lun->res_type 8388 && (lun->res_type == SPR_TYPE_WR_EX_RO 8389 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8390 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8391 } 8392 } 8393 lun->res_type = msg->pr.pr_info.res_type; 8394 if (lun->res_type != SPR_TYPE_WR_EX_AR 8395 && lun->res_type != SPR_TYPE_EX_AC_AR) 8396 lun->pr_res_idx = msg->pr.pr_info.residx; 8397 else 8398 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8399 } 8400 lun->PRGeneration++; 8401 8402} 8403 8404 8405int 8406ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8407{ 8408 int retval; 8409 int isc_retval; 8410 u_int32_t param_len; 8411 struct scsi_per_res_out *cdb; 8412 struct ctl_lun *lun; 8413 struct scsi_per_res_out_parms* param; 8414 struct ctl_softc *softc; 8415 uint32_t residx; 8416 uint64_t res_key, sa_res_key, key; 8417 uint8_t type; 8418 union ctl_ha_msg persis_io; 8419 int i; 8420 8421 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8422 8423 retval = CTL_RETVAL_COMPLETE; 8424 8425 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8426 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8427 softc = lun->ctl_softc; 8428 8429 /* 8430 * We only support whole-LUN scope. The scope & type are ignored for 8431 * register, register and ignore existing key and clear. 8432 * We sometimes ignore scope and type on preempts too!! 8433 * Verify reservation type here as well. 8434 */ 8435 type = cdb->scope_type & SPR_TYPE_MASK; 8436 if ((cdb->action == SPRO_RESERVE) 8437 || (cdb->action == SPRO_RELEASE)) { 8438 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8439 ctl_set_invalid_field(/*ctsio*/ ctsio, 8440 /*sks_valid*/ 1, 8441 /*command*/ 1, 8442 /*field*/ 2, 8443 /*bit_valid*/ 1, 8444 /*bit*/ 4); 8445 ctl_done((union ctl_io *)ctsio); 8446 return (CTL_RETVAL_COMPLETE); 8447 } 8448 8449 if (type>8 || type==2 || type==4 || type==0) { 8450 ctl_set_invalid_field(/*ctsio*/ ctsio, 8451 /*sks_valid*/ 1, 8452 /*command*/ 1, 8453 /*field*/ 2, 8454 /*bit_valid*/ 1, 8455 /*bit*/ 0); 8456 ctl_done((union ctl_io *)ctsio); 8457 return (CTL_RETVAL_COMPLETE); 8458 } 8459 } 8460 8461 param_len = scsi_4btoul(cdb->length); 8462 8463 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8464 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8465 ctsio->kern_data_len = param_len; 8466 ctsio->kern_total_len = param_len; 8467 ctsio->kern_data_resid = 0; 8468 ctsio->kern_rel_offset = 0; 8469 ctsio->kern_sg_entries = 0; 8470 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8471 ctsio->be_move_done = ctl_config_move_done; 8472 ctl_datamove((union ctl_io *)ctsio); 8473 8474 return (CTL_RETVAL_COMPLETE); 8475 } 8476 8477 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8478 8479 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8480 res_key = scsi_8btou64(param->res_key.key); 8481 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8482 8483 /* 8484 * Validate the reservation key here except for SPRO_REG_IGNO 8485 * This must be done for all other service actions 8486 */ 8487 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8488 mtx_lock(&lun->lun_lock); 8489 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8490 if (res_key != key) { 8491 /* 8492 * The current key passed in doesn't match 8493 * the one the initiator previously 8494 * registered. 8495 */ 8496 mtx_unlock(&lun->lun_lock); 8497 free(ctsio->kern_data_ptr, M_CTL); 8498 ctl_set_reservation_conflict(ctsio); 8499 ctl_done((union ctl_io *)ctsio); 8500 return (CTL_RETVAL_COMPLETE); 8501 } 8502 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8503 /* 8504 * We are not registered 8505 */ 8506 mtx_unlock(&lun->lun_lock); 8507 free(ctsio->kern_data_ptr, M_CTL); 8508 ctl_set_reservation_conflict(ctsio); 8509 ctl_done((union ctl_io *)ctsio); 8510 return (CTL_RETVAL_COMPLETE); 8511 } else if (res_key != 0) { 8512 /* 8513 * We are not registered and trying to register but 8514 * the register key isn't zero. 8515 */ 8516 mtx_unlock(&lun->lun_lock); 8517 free(ctsio->kern_data_ptr, M_CTL); 8518 ctl_set_reservation_conflict(ctsio); 8519 ctl_done((union ctl_io *)ctsio); 8520 return (CTL_RETVAL_COMPLETE); 8521 } 8522 mtx_unlock(&lun->lun_lock); 8523 } 8524 8525 switch (cdb->action & SPRO_ACTION_MASK) { 8526 case SPRO_REGISTER: 8527 case SPRO_REG_IGNO: { 8528 8529#if 0 8530 printf("Registration received\n"); 8531#endif 8532 8533 /* 8534 * We don't support any of these options, as we report in 8535 * the read capabilities request (see 8536 * ctl_persistent_reserve_in(), above). 8537 */ 8538 if ((param->flags & SPR_SPEC_I_PT) 8539 || (param->flags & SPR_ALL_TG_PT) 8540 || (param->flags & SPR_APTPL)) { 8541 int bit_ptr; 8542 8543 if (param->flags & SPR_APTPL) 8544 bit_ptr = 0; 8545 else if (param->flags & SPR_ALL_TG_PT) 8546 bit_ptr = 2; 8547 else /* SPR_SPEC_I_PT */ 8548 bit_ptr = 3; 8549 8550 free(ctsio->kern_data_ptr, M_CTL); 8551 ctl_set_invalid_field(ctsio, 8552 /*sks_valid*/ 1, 8553 /*command*/ 0, 8554 /*field*/ 20, 8555 /*bit_valid*/ 1, 8556 /*bit*/ bit_ptr); 8557 ctl_done((union ctl_io *)ctsio); 8558 return (CTL_RETVAL_COMPLETE); 8559 } 8560 8561 mtx_lock(&lun->lun_lock); 8562 8563 /* 8564 * The initiator wants to clear the 8565 * key/unregister. 8566 */ 8567 if (sa_res_key == 0) { 8568 if ((res_key == 0 8569 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8570 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8571 && ctl_get_prkey(lun, residx) == 0)) { 8572 mtx_unlock(&lun->lun_lock); 8573 goto done; 8574 } 8575 8576 ctl_clr_prkey(lun, residx); 8577 lun->pr_key_count--; 8578 8579 if (residx == lun->pr_res_idx) { 8580 lun->flags &= ~CTL_LUN_PR_RESERVED; 8581 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8582 8583 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8584 || lun->res_type == SPR_TYPE_EX_AC_RO) 8585 && lun->pr_key_count) { 8586 /* 8587 * If the reservation is a registrants 8588 * only type we need to generate a UA 8589 * for other registered inits. The 8590 * sense code should be RESERVATIONS 8591 * RELEASED 8592 */ 8593 8594 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8595 if (ctl_get_prkey(lun, i + 8596 softc->persis_offset) == 0) 8597 continue; 8598 ctl_est_ua(lun, i, 8599 CTL_UA_RES_RELEASE); 8600 } 8601 } 8602 lun->res_type = 0; 8603 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8604 if (lun->pr_key_count==0) { 8605 lun->flags &= ~CTL_LUN_PR_RESERVED; 8606 lun->res_type = 0; 8607 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8608 } 8609 } 8610 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8611 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8612 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8613 persis_io.pr.pr_info.residx = residx; 8614 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8615 &persis_io, sizeof(persis_io), 0 )) > 8616 CTL_HA_STATUS_SUCCESS) { 8617 printf("CTL:Persis Out error returned from " 8618 "ctl_ha_msg_send %d\n", isc_retval); 8619 } 8620 } else /* sa_res_key != 0 */ { 8621 8622 /* 8623 * If we aren't registered currently then increment 8624 * the key count and set the registered flag. 8625 */ 8626 ctl_alloc_prkey(lun, residx); 8627 if (ctl_get_prkey(lun, residx) == 0) 8628 lun->pr_key_count++; 8629 ctl_set_prkey(lun, residx, sa_res_key); 8630 8631 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8632 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8633 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8634 persis_io.pr.pr_info.residx = residx; 8635 memcpy(persis_io.pr.pr_info.sa_res_key, 8636 param->serv_act_res_key, 8637 sizeof(param->serv_act_res_key)); 8638 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8639 &persis_io, sizeof(persis_io), 0)) > 8640 CTL_HA_STATUS_SUCCESS) { 8641 printf("CTL:Persis Out error returned from " 8642 "ctl_ha_msg_send %d\n", isc_retval); 8643 } 8644 } 8645 lun->PRGeneration++; 8646 mtx_unlock(&lun->lun_lock); 8647 8648 break; 8649 } 8650 case SPRO_RESERVE: 8651#if 0 8652 printf("Reserve executed type %d\n", type); 8653#endif 8654 mtx_lock(&lun->lun_lock); 8655 if (lun->flags & CTL_LUN_PR_RESERVED) { 8656 /* 8657 * if this isn't the reservation holder and it's 8658 * not a "all registrants" type or if the type is 8659 * different then we have a conflict 8660 */ 8661 if ((lun->pr_res_idx != residx 8662 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8663 || lun->res_type != type) { 8664 mtx_unlock(&lun->lun_lock); 8665 free(ctsio->kern_data_ptr, M_CTL); 8666 ctl_set_reservation_conflict(ctsio); 8667 ctl_done((union ctl_io *)ctsio); 8668 return (CTL_RETVAL_COMPLETE); 8669 } 8670 mtx_unlock(&lun->lun_lock); 8671 } else /* create a reservation */ { 8672 /* 8673 * If it's not an "all registrants" type record 8674 * reservation holder 8675 */ 8676 if (type != SPR_TYPE_WR_EX_AR 8677 && type != SPR_TYPE_EX_AC_AR) 8678 lun->pr_res_idx = residx; /* Res holder */ 8679 else 8680 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8681 8682 lun->flags |= CTL_LUN_PR_RESERVED; 8683 lun->res_type = type; 8684 8685 mtx_unlock(&lun->lun_lock); 8686 8687 /* send msg to other side */ 8688 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8689 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8690 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8691 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8692 persis_io.pr.pr_info.res_type = type; 8693 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8694 &persis_io, sizeof(persis_io), 0)) > 8695 CTL_HA_STATUS_SUCCESS) { 8696 printf("CTL:Persis Out error returned from " 8697 "ctl_ha_msg_send %d\n", isc_retval); 8698 } 8699 } 8700 break; 8701 8702 case SPRO_RELEASE: 8703 mtx_lock(&lun->lun_lock); 8704 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8705 /* No reservation exists return good status */ 8706 mtx_unlock(&lun->lun_lock); 8707 goto done; 8708 } 8709 /* 8710 * Is this nexus a reservation holder? 8711 */ 8712 if (lun->pr_res_idx != residx 8713 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8714 /* 8715 * not a res holder return good status but 8716 * do nothing 8717 */ 8718 mtx_unlock(&lun->lun_lock); 8719 goto done; 8720 } 8721 8722 if (lun->res_type != type) { 8723 mtx_unlock(&lun->lun_lock); 8724 free(ctsio->kern_data_ptr, M_CTL); 8725 ctl_set_illegal_pr_release(ctsio); 8726 ctl_done((union ctl_io *)ctsio); 8727 return (CTL_RETVAL_COMPLETE); 8728 } 8729 8730 /* okay to release */ 8731 lun->flags &= ~CTL_LUN_PR_RESERVED; 8732 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8733 lun->res_type = 0; 8734 8735 /* 8736 * if this isn't an exclusive access 8737 * res generate UA for all other 8738 * registrants. 8739 */ 8740 if (type != SPR_TYPE_EX_AC 8741 && type != SPR_TYPE_WR_EX) { 8742 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8743 if (i == residx || 8744 ctl_get_prkey(lun, 8745 i + softc->persis_offset) == 0) 8746 continue; 8747 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8748 } 8749 } 8750 mtx_unlock(&lun->lun_lock); 8751 /* Send msg to other side */ 8752 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8753 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8754 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8755 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8756 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8757 printf("CTL:Persis Out error returned from " 8758 "ctl_ha_msg_send %d\n", isc_retval); 8759 } 8760 break; 8761 8762 case SPRO_CLEAR: 8763 /* send msg to other side */ 8764 8765 mtx_lock(&lun->lun_lock); 8766 lun->flags &= ~CTL_LUN_PR_RESERVED; 8767 lun->res_type = 0; 8768 lun->pr_key_count = 0; 8769 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8770 8771 ctl_clr_prkey(lun, residx); 8772 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8773 if (ctl_get_prkey(lun, i) != 0) { 8774 ctl_clr_prkey(lun, i); 8775 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8776 } 8777 lun->PRGeneration++; 8778 mtx_unlock(&lun->lun_lock); 8779 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8780 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8781 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8782 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8783 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8784 printf("CTL:Persis Out error returned from " 8785 "ctl_ha_msg_send %d\n", isc_retval); 8786 } 8787 break; 8788 8789 case SPRO_PREEMPT: 8790 case SPRO_PRE_ABO: { 8791 int nretval; 8792 8793 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8794 residx, ctsio, cdb, param); 8795 if (nretval != 0) 8796 return (CTL_RETVAL_COMPLETE); 8797 break; 8798 } 8799 default: 8800 panic("Invalid PR type %x", cdb->action); 8801 } 8802 8803done: 8804 free(ctsio->kern_data_ptr, M_CTL); 8805 ctl_set_success(ctsio); 8806 ctl_done((union ctl_io *)ctsio); 8807 8808 return (retval); 8809} 8810 8811/* 8812 * This routine is for handling a message from the other SC pertaining to 8813 * persistent reserve out. All the error checking will have been done 8814 * so only perorming the action need be done here to keep the two 8815 * in sync. 8816 */ 8817static void 8818ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8819{ 8820 struct ctl_lun *lun; 8821 struct ctl_softc *softc; 8822 int i; 8823 uint32_t targ_lun; 8824 8825 softc = control_softc; 8826 8827 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8828 lun = softc->ctl_luns[targ_lun]; 8829 mtx_lock(&lun->lun_lock); 8830 switch(msg->pr.pr_info.action) { 8831 case CTL_PR_REG_KEY: 8832 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8833 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8834 lun->pr_key_count++; 8835 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8836 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8837 lun->PRGeneration++; 8838 break; 8839 8840 case CTL_PR_UNREG_KEY: 8841 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8842 lun->pr_key_count--; 8843 8844 /* XXX Need to see if the reservation has been released */ 8845 /* if so do we need to generate UA? */ 8846 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8847 lun->flags &= ~CTL_LUN_PR_RESERVED; 8848 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8849 8850 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8851 || lun->res_type == SPR_TYPE_EX_AC_RO) 8852 && lun->pr_key_count) { 8853 /* 8854 * If the reservation is a registrants 8855 * only type we need to generate a UA 8856 * for other registered inits. The 8857 * sense code should be RESERVATIONS 8858 * RELEASED 8859 */ 8860 8861 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8862 if (ctl_get_prkey(lun, i + 8863 softc->persis_offset) == 0) 8864 continue; 8865 8866 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8867 } 8868 } 8869 lun->res_type = 0; 8870 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8871 if (lun->pr_key_count==0) { 8872 lun->flags &= ~CTL_LUN_PR_RESERVED; 8873 lun->res_type = 0; 8874 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8875 } 8876 } 8877 lun->PRGeneration++; 8878 break; 8879 8880 case CTL_PR_RESERVE: 8881 lun->flags |= CTL_LUN_PR_RESERVED; 8882 lun->res_type = msg->pr.pr_info.res_type; 8883 lun->pr_res_idx = msg->pr.pr_info.residx; 8884 8885 break; 8886 8887 case CTL_PR_RELEASE: 8888 /* 8889 * if this isn't an exclusive access res generate UA for all 8890 * other registrants. 8891 */ 8892 if (lun->res_type != SPR_TYPE_EX_AC 8893 && lun->res_type != SPR_TYPE_WR_EX) { 8894 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8895 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8896 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8897 } 8898 8899 lun->flags &= ~CTL_LUN_PR_RESERVED; 8900 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8901 lun->res_type = 0; 8902 break; 8903 8904 case CTL_PR_PREEMPT: 8905 ctl_pro_preempt_other(lun, msg); 8906 break; 8907 case CTL_PR_CLEAR: 8908 lun->flags &= ~CTL_LUN_PR_RESERVED; 8909 lun->res_type = 0; 8910 lun->pr_key_count = 0; 8911 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8912 8913 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8914 if (ctl_get_prkey(lun, i) == 0) 8915 continue; 8916 ctl_clr_prkey(lun, i); 8917 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8918 } 8919 lun->PRGeneration++; 8920 break; 8921 } 8922 8923 mtx_unlock(&lun->lun_lock); 8924} 8925 8926int 8927ctl_read_write(struct ctl_scsiio *ctsio) 8928{ 8929 struct ctl_lun *lun; 8930 struct ctl_lba_len_flags *lbalen; 8931 uint64_t lba; 8932 uint32_t num_blocks; 8933 int flags, retval; 8934 int isread; 8935 8936 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8937 8938 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8939 8940 flags = 0; 8941 retval = CTL_RETVAL_COMPLETE; 8942 8943 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8944 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8945 switch (ctsio->cdb[0]) { 8946 case READ_6: 8947 case WRITE_6: { 8948 struct scsi_rw_6 *cdb; 8949 8950 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8951 8952 lba = scsi_3btoul(cdb->addr); 8953 /* only 5 bits are valid in the most significant address byte */ 8954 lba &= 0x1fffff; 8955 num_blocks = cdb->length; 8956 /* 8957 * This is correct according to SBC-2. 8958 */ 8959 if (num_blocks == 0) 8960 num_blocks = 256; 8961 break; 8962 } 8963 case READ_10: 8964 case WRITE_10: { 8965 struct scsi_rw_10 *cdb; 8966 8967 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8968 if (cdb->byte2 & SRW10_FUA) 8969 flags |= CTL_LLF_FUA; 8970 if (cdb->byte2 & SRW10_DPO) 8971 flags |= CTL_LLF_DPO; 8972 lba = scsi_4btoul(cdb->addr); 8973 num_blocks = scsi_2btoul(cdb->length); 8974 break; 8975 } 8976 case WRITE_VERIFY_10: { 8977 struct scsi_write_verify_10 *cdb; 8978 8979 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8980 flags |= CTL_LLF_FUA; 8981 if (cdb->byte2 & SWV_DPO) 8982 flags |= CTL_LLF_DPO; 8983 lba = scsi_4btoul(cdb->addr); 8984 num_blocks = scsi_2btoul(cdb->length); 8985 break; 8986 } 8987 case READ_12: 8988 case WRITE_12: { 8989 struct scsi_rw_12 *cdb; 8990 8991 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8992 if (cdb->byte2 & SRW12_FUA) 8993 flags |= CTL_LLF_FUA; 8994 if (cdb->byte2 & SRW12_DPO) 8995 flags |= CTL_LLF_DPO; 8996 lba = scsi_4btoul(cdb->addr); 8997 num_blocks = scsi_4btoul(cdb->length); 8998 break; 8999 } 9000 case WRITE_VERIFY_12: { 9001 struct scsi_write_verify_12 *cdb; 9002 9003 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9004 flags |= CTL_LLF_FUA; 9005 if (cdb->byte2 & SWV_DPO) 9006 flags |= CTL_LLF_DPO; 9007 lba = scsi_4btoul(cdb->addr); 9008 num_blocks = scsi_4btoul(cdb->length); 9009 break; 9010 } 9011 case READ_16: 9012 case WRITE_16: { 9013 struct scsi_rw_16 *cdb; 9014 9015 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9016 if (cdb->byte2 & SRW12_FUA) 9017 flags |= CTL_LLF_FUA; 9018 if (cdb->byte2 & SRW12_DPO) 9019 flags |= CTL_LLF_DPO; 9020 lba = scsi_8btou64(cdb->addr); 9021 num_blocks = scsi_4btoul(cdb->length); 9022 break; 9023 } 9024 case WRITE_ATOMIC_16: { 9025 struct scsi_rw_16 *cdb; 9026 9027 if (lun->be_lun->atomicblock == 0) { 9028 ctl_set_invalid_opcode(ctsio); 9029 ctl_done((union ctl_io *)ctsio); 9030 return (CTL_RETVAL_COMPLETE); 9031 } 9032 9033 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9034 if (cdb->byte2 & SRW12_FUA) 9035 flags |= CTL_LLF_FUA; 9036 if (cdb->byte2 & SRW12_DPO) 9037 flags |= CTL_LLF_DPO; 9038 lba = scsi_8btou64(cdb->addr); 9039 num_blocks = scsi_4btoul(cdb->length); 9040 if (num_blocks > lun->be_lun->atomicblock) { 9041 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9042 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9043 /*bit*/ 0); 9044 ctl_done((union ctl_io *)ctsio); 9045 return (CTL_RETVAL_COMPLETE); 9046 } 9047 break; 9048 } 9049 case WRITE_VERIFY_16: { 9050 struct scsi_write_verify_16 *cdb; 9051 9052 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9053 flags |= CTL_LLF_FUA; 9054 if (cdb->byte2 & SWV_DPO) 9055 flags |= CTL_LLF_DPO; 9056 lba = scsi_8btou64(cdb->addr); 9057 num_blocks = scsi_4btoul(cdb->length); 9058 break; 9059 } 9060 default: 9061 /* 9062 * We got a command we don't support. This shouldn't 9063 * happen, commands should be filtered out above us. 9064 */ 9065 ctl_set_invalid_opcode(ctsio); 9066 ctl_done((union ctl_io *)ctsio); 9067 9068 return (CTL_RETVAL_COMPLETE); 9069 break; /* NOTREACHED */ 9070 } 9071 9072 /* 9073 * The first check is to make sure we're in bounds, the second 9074 * check is to catch wrap-around problems. If the lba + num blocks 9075 * is less than the lba, then we've wrapped around and the block 9076 * range is invalid anyway. 9077 */ 9078 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9079 || ((lba + num_blocks) < lba)) { 9080 ctl_set_lba_out_of_range(ctsio); 9081 ctl_done((union ctl_io *)ctsio); 9082 return (CTL_RETVAL_COMPLETE); 9083 } 9084 9085 /* 9086 * According to SBC-3, a transfer length of 0 is not an error. 9087 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9088 * translates to 256 blocks for those commands. 9089 */ 9090 if (num_blocks == 0) { 9091 ctl_set_success(ctsio); 9092 ctl_done((union ctl_io *)ctsio); 9093 return (CTL_RETVAL_COMPLETE); 9094 } 9095 9096 /* Set FUA and/or DPO if caches are disabled. */ 9097 if (isread) { 9098 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9099 SCP_RCD) != 0) 9100 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9101 } else { 9102 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9103 SCP_WCE) == 0) 9104 flags |= CTL_LLF_FUA; 9105 } 9106 9107 lbalen = (struct ctl_lba_len_flags *) 9108 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9109 lbalen->lba = lba; 9110 lbalen->len = num_blocks; 9111 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9112 9113 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9114 ctsio->kern_rel_offset = 0; 9115 9116 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9117 9118 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9119 9120 return (retval); 9121} 9122 9123static int 9124ctl_cnw_cont(union ctl_io *io) 9125{ 9126 struct ctl_scsiio *ctsio; 9127 struct ctl_lun *lun; 9128 struct ctl_lba_len_flags *lbalen; 9129 int retval; 9130 9131 ctsio = &io->scsiio; 9132 ctsio->io_hdr.status = CTL_STATUS_NONE; 9133 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9134 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9135 lbalen = (struct ctl_lba_len_flags *) 9136 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9137 lbalen->flags &= ~CTL_LLF_COMPARE; 9138 lbalen->flags |= CTL_LLF_WRITE; 9139 9140 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9141 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9142 return (retval); 9143} 9144 9145int 9146ctl_cnw(struct ctl_scsiio *ctsio) 9147{ 9148 struct ctl_lun *lun; 9149 struct ctl_lba_len_flags *lbalen; 9150 uint64_t lba; 9151 uint32_t num_blocks; 9152 int flags, retval; 9153 9154 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9155 9156 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9157 9158 flags = 0; 9159 retval = CTL_RETVAL_COMPLETE; 9160 9161 switch (ctsio->cdb[0]) { 9162 case COMPARE_AND_WRITE: { 9163 struct scsi_compare_and_write *cdb; 9164 9165 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9166 if (cdb->byte2 & SRW10_FUA) 9167 flags |= CTL_LLF_FUA; 9168 if (cdb->byte2 & SRW10_DPO) 9169 flags |= CTL_LLF_DPO; 9170 lba = scsi_8btou64(cdb->addr); 9171 num_blocks = cdb->length; 9172 break; 9173 } 9174 default: 9175 /* 9176 * We got a command we don't support. This shouldn't 9177 * happen, commands should be filtered out above us. 9178 */ 9179 ctl_set_invalid_opcode(ctsio); 9180 ctl_done((union ctl_io *)ctsio); 9181 9182 return (CTL_RETVAL_COMPLETE); 9183 break; /* NOTREACHED */ 9184 } 9185 9186 /* 9187 * The first check is to make sure we're in bounds, the second 9188 * check is to catch wrap-around problems. If the lba + num blocks 9189 * is less than the lba, then we've wrapped around and the block 9190 * range is invalid anyway. 9191 */ 9192 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9193 || ((lba + num_blocks) < lba)) { 9194 ctl_set_lba_out_of_range(ctsio); 9195 ctl_done((union ctl_io *)ctsio); 9196 return (CTL_RETVAL_COMPLETE); 9197 } 9198 9199 /* 9200 * According to SBC-3, a transfer length of 0 is not an error. 9201 */ 9202 if (num_blocks == 0) { 9203 ctl_set_success(ctsio); 9204 ctl_done((union ctl_io *)ctsio); 9205 return (CTL_RETVAL_COMPLETE); 9206 } 9207 9208 /* Set FUA if write cache is disabled. */ 9209 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9210 SCP_WCE) == 0) 9211 flags |= CTL_LLF_FUA; 9212 9213 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9214 ctsio->kern_rel_offset = 0; 9215 9216 /* 9217 * Set the IO_CONT flag, so that if this I/O gets passed to 9218 * ctl_data_submit_done(), it'll get passed back to 9219 * ctl_ctl_cnw_cont() for further processing. 9220 */ 9221 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9222 ctsio->io_cont = ctl_cnw_cont; 9223 9224 lbalen = (struct ctl_lba_len_flags *) 9225 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9226 lbalen->lba = lba; 9227 lbalen->len = num_blocks; 9228 lbalen->flags = CTL_LLF_COMPARE | flags; 9229 9230 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9231 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9232 return (retval); 9233} 9234 9235int 9236ctl_verify(struct ctl_scsiio *ctsio) 9237{ 9238 struct ctl_lun *lun; 9239 struct ctl_lba_len_flags *lbalen; 9240 uint64_t lba; 9241 uint32_t num_blocks; 9242 int bytchk, flags; 9243 int retval; 9244 9245 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9246 9247 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9248 9249 bytchk = 0; 9250 flags = CTL_LLF_FUA; 9251 retval = CTL_RETVAL_COMPLETE; 9252 9253 switch (ctsio->cdb[0]) { 9254 case VERIFY_10: { 9255 struct scsi_verify_10 *cdb; 9256 9257 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9258 if (cdb->byte2 & SVFY_BYTCHK) 9259 bytchk = 1; 9260 if (cdb->byte2 & SVFY_DPO) 9261 flags |= CTL_LLF_DPO; 9262 lba = scsi_4btoul(cdb->addr); 9263 num_blocks = scsi_2btoul(cdb->length); 9264 break; 9265 } 9266 case VERIFY_12: { 9267 struct scsi_verify_12 *cdb; 9268 9269 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9270 if (cdb->byte2 & SVFY_BYTCHK) 9271 bytchk = 1; 9272 if (cdb->byte2 & SVFY_DPO) 9273 flags |= CTL_LLF_DPO; 9274 lba = scsi_4btoul(cdb->addr); 9275 num_blocks = scsi_4btoul(cdb->length); 9276 break; 9277 } 9278 case VERIFY_16: { 9279 struct scsi_rw_16 *cdb; 9280 9281 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9282 if (cdb->byte2 & SVFY_BYTCHK) 9283 bytchk = 1; 9284 if (cdb->byte2 & SVFY_DPO) 9285 flags |= CTL_LLF_DPO; 9286 lba = scsi_8btou64(cdb->addr); 9287 num_blocks = scsi_4btoul(cdb->length); 9288 break; 9289 } 9290 default: 9291 /* 9292 * We got a command we don't support. This shouldn't 9293 * happen, commands should be filtered out above us. 9294 */ 9295 ctl_set_invalid_opcode(ctsio); 9296 ctl_done((union ctl_io *)ctsio); 9297 return (CTL_RETVAL_COMPLETE); 9298 } 9299 9300 /* 9301 * The first check is to make sure we're in bounds, the second 9302 * check is to catch wrap-around problems. If the lba + num blocks 9303 * is less than the lba, then we've wrapped around and the block 9304 * range is invalid anyway. 9305 */ 9306 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9307 || ((lba + num_blocks) < lba)) { 9308 ctl_set_lba_out_of_range(ctsio); 9309 ctl_done((union ctl_io *)ctsio); 9310 return (CTL_RETVAL_COMPLETE); 9311 } 9312 9313 /* 9314 * According to SBC-3, a transfer length of 0 is not an error. 9315 */ 9316 if (num_blocks == 0) { 9317 ctl_set_success(ctsio); 9318 ctl_done((union ctl_io *)ctsio); 9319 return (CTL_RETVAL_COMPLETE); 9320 } 9321 9322 lbalen = (struct ctl_lba_len_flags *) 9323 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9324 lbalen->lba = lba; 9325 lbalen->len = num_blocks; 9326 if (bytchk) { 9327 lbalen->flags = CTL_LLF_COMPARE | flags; 9328 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9329 } else { 9330 lbalen->flags = CTL_LLF_VERIFY | flags; 9331 ctsio->kern_total_len = 0; 9332 } 9333 ctsio->kern_rel_offset = 0; 9334 9335 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9336 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9337 return (retval); 9338} 9339 9340int 9341ctl_report_luns(struct ctl_scsiio *ctsio) 9342{ 9343 struct ctl_softc *softc = control_softc; 9344 struct scsi_report_luns *cdb; 9345 struct scsi_report_luns_data *lun_data; 9346 struct ctl_lun *lun, *request_lun; 9347 struct ctl_port *port; 9348 int num_luns, retval; 9349 uint32_t alloc_len, lun_datalen; 9350 int num_filled, well_known; 9351 uint32_t initidx, targ_lun_id, lun_id; 9352 9353 retval = CTL_RETVAL_COMPLETE; 9354 well_known = 0; 9355 9356 cdb = (struct scsi_report_luns *)ctsio->cdb; 9357 port = ctl_io_port(&ctsio->io_hdr); 9358 9359 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9360 9361 mtx_lock(&softc->ctl_lock); 9362 num_luns = 0; 9363 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9364 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9365 num_luns++; 9366 } 9367 mtx_unlock(&softc->ctl_lock); 9368 9369 switch (cdb->select_report) { 9370 case RPL_REPORT_DEFAULT: 9371 case RPL_REPORT_ALL: 9372 break; 9373 case RPL_REPORT_WELLKNOWN: 9374 well_known = 1; 9375 num_luns = 0; 9376 break; 9377 default: 9378 ctl_set_invalid_field(ctsio, 9379 /*sks_valid*/ 1, 9380 /*command*/ 1, 9381 /*field*/ 2, 9382 /*bit_valid*/ 0, 9383 /*bit*/ 0); 9384 ctl_done((union ctl_io *)ctsio); 9385 return (retval); 9386 break; /* NOTREACHED */ 9387 } 9388 9389 alloc_len = scsi_4btoul(cdb->length); 9390 /* 9391 * The initiator has to allocate at least 16 bytes for this request, 9392 * so he can at least get the header and the first LUN. Otherwise 9393 * we reject the request (per SPC-3 rev 14, section 6.21). 9394 */ 9395 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9396 sizeof(struct scsi_report_luns_lundata))) { 9397 ctl_set_invalid_field(ctsio, 9398 /*sks_valid*/ 1, 9399 /*command*/ 1, 9400 /*field*/ 6, 9401 /*bit_valid*/ 0, 9402 /*bit*/ 0); 9403 ctl_done((union ctl_io *)ctsio); 9404 return (retval); 9405 } 9406 9407 request_lun = (struct ctl_lun *) 9408 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9409 9410 lun_datalen = sizeof(*lun_data) + 9411 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9412 9413 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9414 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9415 ctsio->kern_sg_entries = 0; 9416 9417 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9418 9419 mtx_lock(&softc->ctl_lock); 9420 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9421 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9422 if (lun_id >= CTL_MAX_LUNS) 9423 continue; 9424 lun = softc->ctl_luns[lun_id]; 9425 if (lun == NULL) 9426 continue; 9427 9428 if (targ_lun_id <= 0xff) { 9429 /* 9430 * Peripheral addressing method, bus number 0. 9431 */ 9432 lun_data->luns[num_filled].lundata[0] = 9433 RPL_LUNDATA_ATYP_PERIPH; 9434 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9435 num_filled++; 9436 } else if (targ_lun_id <= 0x3fff) { 9437 /* 9438 * Flat addressing method. 9439 */ 9440 lun_data->luns[num_filled].lundata[0] = 9441 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9442 lun_data->luns[num_filled].lundata[1] = 9443 (targ_lun_id & 0xff); 9444 num_filled++; 9445 } else if (targ_lun_id <= 0xffffff) { 9446 /* 9447 * Extended flat addressing method. 9448 */ 9449 lun_data->luns[num_filled].lundata[0] = 9450 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9451 scsi_ulto3b(targ_lun_id, 9452 &lun_data->luns[num_filled].lundata[1]); 9453 num_filled++; 9454 } else { 9455 printf("ctl_report_luns: bogus LUN number %jd, " 9456 "skipping\n", (intmax_t)targ_lun_id); 9457 } 9458 /* 9459 * According to SPC-3, rev 14 section 6.21: 9460 * 9461 * "The execution of a REPORT LUNS command to any valid and 9462 * installed logical unit shall clear the REPORTED LUNS DATA 9463 * HAS CHANGED unit attention condition for all logical 9464 * units of that target with respect to the requesting 9465 * initiator. A valid and installed logical unit is one 9466 * having a PERIPHERAL QUALIFIER of 000b in the standard 9467 * INQUIRY data (see 6.4.2)." 9468 * 9469 * If request_lun is NULL, the LUN this report luns command 9470 * was issued to is either disabled or doesn't exist. In that 9471 * case, we shouldn't clear any pending lun change unit 9472 * attention. 9473 */ 9474 if (request_lun != NULL) { 9475 mtx_lock(&lun->lun_lock); 9476 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE); 9477 mtx_unlock(&lun->lun_lock); 9478 } 9479 } 9480 mtx_unlock(&softc->ctl_lock); 9481 9482 /* 9483 * It's quite possible that we've returned fewer LUNs than we allocated 9484 * space for. Trim it. 9485 */ 9486 lun_datalen = sizeof(*lun_data) + 9487 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9488 9489 if (lun_datalen < alloc_len) { 9490 ctsio->residual = alloc_len - lun_datalen; 9491 ctsio->kern_data_len = lun_datalen; 9492 ctsio->kern_total_len = lun_datalen; 9493 } else { 9494 ctsio->residual = 0; 9495 ctsio->kern_data_len = alloc_len; 9496 ctsio->kern_total_len = alloc_len; 9497 } 9498 ctsio->kern_data_resid = 0; 9499 ctsio->kern_rel_offset = 0; 9500 ctsio->kern_sg_entries = 0; 9501 9502 /* 9503 * We set this to the actual data length, regardless of how much 9504 * space we actually have to return results. If the user looks at 9505 * this value, he'll know whether or not he allocated enough space 9506 * and reissue the command if necessary. We don't support well 9507 * known logical units, so if the user asks for that, return none. 9508 */ 9509 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9510 9511 /* 9512 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9513 * this request. 9514 */ 9515 ctl_set_success(ctsio); 9516 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9517 ctsio->be_move_done = ctl_config_move_done; 9518 ctl_datamove((union ctl_io *)ctsio); 9519 return (retval); 9520} 9521 9522int 9523ctl_request_sense(struct ctl_scsiio *ctsio) 9524{ 9525 struct scsi_request_sense *cdb; 9526 struct scsi_sense_data *sense_ptr; 9527 struct ctl_softc *ctl_softc; 9528 struct ctl_lun *lun; 9529 uint32_t initidx; 9530 int have_error; 9531 scsi_sense_data_type sense_format; 9532 ctl_ua_type ua_type; 9533 9534 cdb = (struct scsi_request_sense *)ctsio->cdb; 9535 9536 ctl_softc = control_softc; 9537 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9538 9539 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9540 9541 /* 9542 * Determine which sense format the user wants. 9543 */ 9544 if (cdb->byte2 & SRS_DESC) 9545 sense_format = SSD_TYPE_DESC; 9546 else 9547 sense_format = SSD_TYPE_FIXED; 9548 9549 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9550 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9551 ctsio->kern_sg_entries = 0; 9552 9553 /* 9554 * struct scsi_sense_data, which is currently set to 256 bytes, is 9555 * larger than the largest allowed value for the length field in the 9556 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9557 */ 9558 ctsio->residual = 0; 9559 ctsio->kern_data_len = cdb->length; 9560 ctsio->kern_total_len = cdb->length; 9561 9562 ctsio->kern_data_resid = 0; 9563 ctsio->kern_rel_offset = 0; 9564 ctsio->kern_sg_entries = 0; 9565 9566 /* 9567 * If we don't have a LUN, we don't have any pending sense. 9568 */ 9569 if (lun == NULL) 9570 goto no_sense; 9571 9572 have_error = 0; 9573 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9574 /* 9575 * Check for pending sense, and then for pending unit attentions. 9576 * Pending sense gets returned first, then pending unit attentions. 9577 */ 9578 mtx_lock(&lun->lun_lock); 9579#ifdef CTL_WITH_CA 9580 if (ctl_is_set(lun->have_ca, initidx)) { 9581 scsi_sense_data_type stored_format; 9582 9583 /* 9584 * Check to see which sense format was used for the stored 9585 * sense data. 9586 */ 9587 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9588 9589 /* 9590 * If the user requested a different sense format than the 9591 * one we stored, then we need to convert it to the other 9592 * format. If we're going from descriptor to fixed format 9593 * sense data, we may lose things in translation, depending 9594 * on what options were used. 9595 * 9596 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9597 * for some reason we'll just copy it out as-is. 9598 */ 9599 if ((stored_format == SSD_TYPE_FIXED) 9600 && (sense_format == SSD_TYPE_DESC)) 9601 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9602 &lun->pending_sense[initidx], 9603 (struct scsi_sense_data_desc *)sense_ptr); 9604 else if ((stored_format == SSD_TYPE_DESC) 9605 && (sense_format == SSD_TYPE_FIXED)) 9606 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9607 &lun->pending_sense[initidx], 9608 (struct scsi_sense_data_fixed *)sense_ptr); 9609 else 9610 memcpy(sense_ptr, &lun->pending_sense[initidx], 9611 MIN(sizeof(*sense_ptr), 9612 sizeof(lun->pending_sense[initidx]))); 9613 9614 ctl_clear_mask(lun->have_ca, initidx); 9615 have_error = 1; 9616 } else 9617#endif 9618 { 9619 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9620 if (ua_type != CTL_UA_NONE) 9621 have_error = 1; 9622 if (ua_type == CTL_UA_LUN_CHANGE) { 9623 mtx_unlock(&lun->lun_lock); 9624 mtx_lock(&ctl_softc->ctl_lock); 9625 ctl_clear_ua(ctl_softc, initidx, ua_type); 9626 mtx_unlock(&ctl_softc->ctl_lock); 9627 mtx_lock(&lun->lun_lock); 9628 } 9629 9630 } 9631 mtx_unlock(&lun->lun_lock); 9632 9633 /* 9634 * We already have a pending error, return it. 9635 */ 9636 if (have_error != 0) { 9637 /* 9638 * We report the SCSI status as OK, since the status of the 9639 * request sense command itself is OK. 9640 * We report 0 for the sense length, because we aren't doing 9641 * autosense in this case. We're reporting sense as 9642 * parameter data. 9643 */ 9644 ctl_set_success(ctsio); 9645 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9646 ctsio->be_move_done = ctl_config_move_done; 9647 ctl_datamove((union ctl_io *)ctsio); 9648 return (CTL_RETVAL_COMPLETE); 9649 } 9650 9651no_sense: 9652 9653 /* 9654 * No sense information to report, so we report that everything is 9655 * okay. 9656 */ 9657 ctl_set_sense_data(sense_ptr, 9658 lun, 9659 sense_format, 9660 /*current_error*/ 1, 9661 /*sense_key*/ SSD_KEY_NO_SENSE, 9662 /*asc*/ 0x00, 9663 /*ascq*/ 0x00, 9664 SSD_ELEM_NONE); 9665 9666 /* 9667 * We report 0 for the sense length, because we aren't doing 9668 * autosense in this case. We're reporting sense as parameter data. 9669 */ 9670 ctl_set_success(ctsio); 9671 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9672 ctsio->be_move_done = ctl_config_move_done; 9673 ctl_datamove((union ctl_io *)ctsio); 9674 return (CTL_RETVAL_COMPLETE); 9675} 9676 9677int 9678ctl_tur(struct ctl_scsiio *ctsio) 9679{ 9680 9681 CTL_DEBUG_PRINT(("ctl_tur\n")); 9682 9683 ctl_set_success(ctsio); 9684 ctl_done((union ctl_io *)ctsio); 9685 9686 return (CTL_RETVAL_COMPLETE); 9687} 9688 9689#ifdef notyet 9690static int 9691ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9692{ 9693 9694} 9695#endif 9696 9697/* 9698 * SCSI VPD page 0x00, the Supported VPD Pages page. 9699 */ 9700static int 9701ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9702{ 9703 struct scsi_vpd_supported_pages *pages; 9704 int sup_page_size; 9705 struct ctl_lun *lun; 9706 int p; 9707 9708 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9709 9710 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9711 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9712 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9713 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9714 ctsio->kern_sg_entries = 0; 9715 9716 if (sup_page_size < alloc_len) { 9717 ctsio->residual = alloc_len - sup_page_size; 9718 ctsio->kern_data_len = sup_page_size; 9719 ctsio->kern_total_len = sup_page_size; 9720 } else { 9721 ctsio->residual = 0; 9722 ctsio->kern_data_len = alloc_len; 9723 ctsio->kern_total_len = alloc_len; 9724 } 9725 ctsio->kern_data_resid = 0; 9726 ctsio->kern_rel_offset = 0; 9727 ctsio->kern_sg_entries = 0; 9728 9729 /* 9730 * The control device is always connected. The disk device, on the 9731 * other hand, may not be online all the time. Need to change this 9732 * to figure out whether the disk device is actually online or not. 9733 */ 9734 if (lun != NULL) 9735 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9736 lun->be_lun->lun_type; 9737 else 9738 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9739 9740 p = 0; 9741 /* Supported VPD pages */ 9742 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9743 /* Serial Number */ 9744 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9745 /* Device Identification */ 9746 pages->page_list[p++] = SVPD_DEVICE_ID; 9747 /* Extended INQUIRY Data */ 9748 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9749 /* Mode Page Policy */ 9750 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9751 /* SCSI Ports */ 9752 pages->page_list[p++] = SVPD_SCSI_PORTS; 9753 /* Third-party Copy */ 9754 pages->page_list[p++] = SVPD_SCSI_TPC; 9755 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9756 /* Block limits */ 9757 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9758 /* Block Device Characteristics */ 9759 pages->page_list[p++] = SVPD_BDC; 9760 /* Logical Block Provisioning */ 9761 pages->page_list[p++] = SVPD_LBP; 9762 } 9763 pages->length = p; 9764 9765 ctl_set_success(ctsio); 9766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9767 ctsio->be_move_done = ctl_config_move_done; 9768 ctl_datamove((union ctl_io *)ctsio); 9769 return (CTL_RETVAL_COMPLETE); 9770} 9771 9772/* 9773 * SCSI VPD page 0x80, the Unit Serial Number page. 9774 */ 9775static int 9776ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9777{ 9778 struct scsi_vpd_unit_serial_number *sn_ptr; 9779 struct ctl_lun *lun; 9780 int data_len; 9781 9782 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9783 9784 data_len = 4 + CTL_SN_LEN; 9785 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9786 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9787 if (data_len < alloc_len) { 9788 ctsio->residual = alloc_len - data_len; 9789 ctsio->kern_data_len = data_len; 9790 ctsio->kern_total_len = data_len; 9791 } else { 9792 ctsio->residual = 0; 9793 ctsio->kern_data_len = alloc_len; 9794 ctsio->kern_total_len = alloc_len; 9795 } 9796 ctsio->kern_data_resid = 0; 9797 ctsio->kern_rel_offset = 0; 9798 ctsio->kern_sg_entries = 0; 9799 9800 /* 9801 * The control device is always connected. The disk device, on the 9802 * other hand, may not be online all the time. Need to change this 9803 * to figure out whether the disk device is actually online or not. 9804 */ 9805 if (lun != NULL) 9806 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9807 lun->be_lun->lun_type; 9808 else 9809 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9810 9811 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9812 sn_ptr->length = CTL_SN_LEN; 9813 /* 9814 * If we don't have a LUN, we just leave the serial number as 9815 * all spaces. 9816 */ 9817 if (lun != NULL) { 9818 strncpy((char *)sn_ptr->serial_num, 9819 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9820 } else 9821 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9822 9823 ctl_set_success(ctsio); 9824 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9825 ctsio->be_move_done = ctl_config_move_done; 9826 ctl_datamove((union ctl_io *)ctsio); 9827 return (CTL_RETVAL_COMPLETE); 9828} 9829 9830 9831/* 9832 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9833 */ 9834static int 9835ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9836{ 9837 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9838 struct ctl_lun *lun; 9839 int data_len; 9840 9841 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9842 9843 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9844 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9845 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9846 ctsio->kern_sg_entries = 0; 9847 9848 if (data_len < alloc_len) { 9849 ctsio->residual = alloc_len - data_len; 9850 ctsio->kern_data_len = data_len; 9851 ctsio->kern_total_len = data_len; 9852 } else { 9853 ctsio->residual = 0; 9854 ctsio->kern_data_len = alloc_len; 9855 ctsio->kern_total_len = alloc_len; 9856 } 9857 ctsio->kern_data_resid = 0; 9858 ctsio->kern_rel_offset = 0; 9859 ctsio->kern_sg_entries = 0; 9860 9861 /* 9862 * The control device is always connected. The disk device, on the 9863 * other hand, may not be online all the time. 9864 */ 9865 if (lun != NULL) 9866 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9867 lun->be_lun->lun_type; 9868 else 9869 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9870 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9871 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9872 /* 9873 * We support head of queue, ordered and simple tags. 9874 */ 9875 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9876 /* 9877 * Volatile cache supported. 9878 */ 9879 eid_ptr->flags3 = SVPD_EID_V_SUP; 9880 9881 /* 9882 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9883 * attention for a particular IT nexus on all LUNs once we report 9884 * it to that nexus once. This bit is required as of SPC-4. 9885 */ 9886 eid_ptr->flags4 = SVPD_EID_LUICLT; 9887 9888 /* 9889 * XXX KDM in order to correctly answer this, we would need 9890 * information from the SIM to determine how much sense data it 9891 * can send. So this would really be a path inquiry field, most 9892 * likely. This can be set to a maximum of 252 according to SPC-4, 9893 * but the hardware may or may not be able to support that much. 9894 * 0 just means that the maximum sense data length is not reported. 9895 */ 9896 eid_ptr->max_sense_length = 0; 9897 9898 ctl_set_success(ctsio); 9899 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9900 ctsio->be_move_done = ctl_config_move_done; 9901 ctl_datamove((union ctl_io *)ctsio); 9902 return (CTL_RETVAL_COMPLETE); 9903} 9904 9905static int 9906ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9907{ 9908 struct scsi_vpd_mode_page_policy *mpp_ptr; 9909 struct ctl_lun *lun; 9910 int data_len; 9911 9912 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9913 9914 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9915 sizeof(struct scsi_vpd_mode_page_policy_descr); 9916 9917 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9918 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9919 ctsio->kern_sg_entries = 0; 9920 9921 if (data_len < alloc_len) { 9922 ctsio->residual = alloc_len - data_len; 9923 ctsio->kern_data_len = data_len; 9924 ctsio->kern_total_len = data_len; 9925 } else { 9926 ctsio->residual = 0; 9927 ctsio->kern_data_len = alloc_len; 9928 ctsio->kern_total_len = alloc_len; 9929 } 9930 ctsio->kern_data_resid = 0; 9931 ctsio->kern_rel_offset = 0; 9932 ctsio->kern_sg_entries = 0; 9933 9934 /* 9935 * The control device is always connected. The disk device, on the 9936 * other hand, may not be online all the time. 9937 */ 9938 if (lun != NULL) 9939 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9940 lun->be_lun->lun_type; 9941 else 9942 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9943 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9944 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9945 mpp_ptr->descr[0].page_code = 0x3f; 9946 mpp_ptr->descr[0].subpage_code = 0xff; 9947 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9948 9949 ctl_set_success(ctsio); 9950 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9951 ctsio->be_move_done = ctl_config_move_done; 9952 ctl_datamove((union ctl_io *)ctsio); 9953 return (CTL_RETVAL_COMPLETE); 9954} 9955 9956/* 9957 * SCSI VPD page 0x83, the Device Identification page. 9958 */ 9959static int 9960ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9961{ 9962 struct scsi_vpd_device_id *devid_ptr; 9963 struct scsi_vpd_id_descriptor *desc; 9964 struct ctl_softc *softc; 9965 struct ctl_lun *lun; 9966 struct ctl_port *port; 9967 int data_len; 9968 uint8_t proto; 9969 9970 softc = control_softc; 9971 9972 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9973 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9974 9975 data_len = sizeof(struct scsi_vpd_device_id) + 9976 sizeof(struct scsi_vpd_id_descriptor) + 9977 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9978 sizeof(struct scsi_vpd_id_descriptor) + 9979 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9980 if (lun && lun->lun_devid) 9981 data_len += lun->lun_devid->len; 9982 if (port->port_devid) 9983 data_len += port->port_devid->len; 9984 if (port->target_devid) 9985 data_len += port->target_devid->len; 9986 9987 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9988 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9989 ctsio->kern_sg_entries = 0; 9990 9991 if (data_len < alloc_len) { 9992 ctsio->residual = alloc_len - data_len; 9993 ctsio->kern_data_len = data_len; 9994 ctsio->kern_total_len = data_len; 9995 } else { 9996 ctsio->residual = 0; 9997 ctsio->kern_data_len = alloc_len; 9998 ctsio->kern_total_len = alloc_len; 9999 } 10000 ctsio->kern_data_resid = 0; 10001 ctsio->kern_rel_offset = 0; 10002 ctsio->kern_sg_entries = 0; 10003 10004 /* 10005 * The control device is always connected. The disk device, on the 10006 * other hand, may not be online all the time. 10007 */ 10008 if (lun != NULL) 10009 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10010 lun->be_lun->lun_type; 10011 else 10012 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10013 devid_ptr->page_code = SVPD_DEVICE_ID; 10014 scsi_ulto2b(data_len - 4, devid_ptr->length); 10015 10016 if (port->port_type == CTL_PORT_FC) 10017 proto = SCSI_PROTO_FC << 4; 10018 else if (port->port_type == CTL_PORT_ISCSI) 10019 proto = SCSI_PROTO_ISCSI << 4; 10020 else 10021 proto = SCSI_PROTO_SPI << 4; 10022 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10023 10024 /* 10025 * We're using a LUN association here. i.e., this device ID is a 10026 * per-LUN identifier. 10027 */ 10028 if (lun && lun->lun_devid) { 10029 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10030 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10031 lun->lun_devid->len); 10032 } 10033 10034 /* 10035 * This is for the WWPN which is a port association. 10036 */ 10037 if (port->port_devid) { 10038 memcpy(desc, port->port_devid->data, port->port_devid->len); 10039 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10040 port->port_devid->len); 10041 } 10042 10043 /* 10044 * This is for the Relative Target Port(type 4h) identifier 10045 */ 10046 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10047 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10048 SVPD_ID_TYPE_RELTARG; 10049 desc->length = 4; 10050 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10051 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10052 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10053 10054 /* 10055 * This is for the Target Port Group(type 5h) identifier 10056 */ 10057 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10058 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10059 SVPD_ID_TYPE_TPORTGRP; 10060 desc->length = 4; 10061 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10062 &desc->identifier[2]); 10063 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10064 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10065 10066 /* 10067 * This is for the Target identifier 10068 */ 10069 if (port->target_devid) { 10070 memcpy(desc, port->target_devid->data, port->target_devid->len); 10071 } 10072 10073 ctl_set_success(ctsio); 10074 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10075 ctsio->be_move_done = ctl_config_move_done; 10076 ctl_datamove((union ctl_io *)ctsio); 10077 return (CTL_RETVAL_COMPLETE); 10078} 10079 10080static int 10081ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10082{ 10083 struct ctl_softc *softc = control_softc; 10084 struct scsi_vpd_scsi_ports *sp; 10085 struct scsi_vpd_port_designation *pd; 10086 struct scsi_vpd_port_designation_cont *pdc; 10087 struct ctl_lun *lun; 10088 struct ctl_port *port; 10089 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10090 int num_target_port_groups; 10091 10092 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10093 10094 if (softc->is_single) 10095 num_target_port_groups = 1; 10096 else 10097 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10098 num_target_ports = 0; 10099 iid_len = 0; 10100 id_len = 0; 10101 mtx_lock(&softc->ctl_lock); 10102 STAILQ_FOREACH(port, &softc->port_list, links) { 10103 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10104 continue; 10105 if (lun != NULL && 10106 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10107 continue; 10108 num_target_ports++; 10109 if (port->init_devid) 10110 iid_len += port->init_devid->len; 10111 if (port->port_devid) 10112 id_len += port->port_devid->len; 10113 } 10114 mtx_unlock(&softc->ctl_lock); 10115 10116 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10117 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10118 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10119 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10120 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10121 ctsio->kern_sg_entries = 0; 10122 10123 if (data_len < alloc_len) { 10124 ctsio->residual = alloc_len - data_len; 10125 ctsio->kern_data_len = data_len; 10126 ctsio->kern_total_len = data_len; 10127 } else { 10128 ctsio->residual = 0; 10129 ctsio->kern_data_len = alloc_len; 10130 ctsio->kern_total_len = alloc_len; 10131 } 10132 ctsio->kern_data_resid = 0; 10133 ctsio->kern_rel_offset = 0; 10134 ctsio->kern_sg_entries = 0; 10135 10136 /* 10137 * The control device is always connected. The disk device, on the 10138 * other hand, may not be online all the time. Need to change this 10139 * to figure out whether the disk device is actually online or not. 10140 */ 10141 if (lun != NULL) 10142 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10143 lun->be_lun->lun_type; 10144 else 10145 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10146 10147 sp->page_code = SVPD_SCSI_PORTS; 10148 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10149 sp->page_length); 10150 pd = &sp->design[0]; 10151 10152 mtx_lock(&softc->ctl_lock); 10153 pg = softc->port_offset / CTL_MAX_PORTS; 10154 for (g = 0; g < num_target_port_groups; g++) { 10155 STAILQ_FOREACH(port, &softc->port_list, links) { 10156 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10157 continue; 10158 if (lun != NULL && 10159 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10160 continue; 10161 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10162 scsi_ulto2b(p, pd->relative_port_id); 10163 if (port->init_devid && g == pg) { 10164 iid_len = port->init_devid->len; 10165 memcpy(pd->initiator_transportid, 10166 port->init_devid->data, port->init_devid->len); 10167 } else 10168 iid_len = 0; 10169 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10170 pdc = (struct scsi_vpd_port_designation_cont *) 10171 (&pd->initiator_transportid[iid_len]); 10172 if (port->port_devid && g == pg) { 10173 id_len = port->port_devid->len; 10174 memcpy(pdc->target_port_descriptors, 10175 port->port_devid->data, port->port_devid->len); 10176 } else 10177 id_len = 0; 10178 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10179 pd = (struct scsi_vpd_port_designation *) 10180 ((uint8_t *)pdc->target_port_descriptors + id_len); 10181 } 10182 } 10183 mtx_unlock(&softc->ctl_lock); 10184 10185 ctl_set_success(ctsio); 10186 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10187 ctsio->be_move_done = ctl_config_move_done; 10188 ctl_datamove((union ctl_io *)ctsio); 10189 return (CTL_RETVAL_COMPLETE); 10190} 10191 10192static int 10193ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10194{ 10195 struct scsi_vpd_block_limits *bl_ptr; 10196 struct ctl_lun *lun; 10197 int bs; 10198 10199 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10200 10201 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10202 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10203 ctsio->kern_sg_entries = 0; 10204 10205 if (sizeof(*bl_ptr) < alloc_len) { 10206 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10207 ctsio->kern_data_len = sizeof(*bl_ptr); 10208 ctsio->kern_total_len = sizeof(*bl_ptr); 10209 } else { 10210 ctsio->residual = 0; 10211 ctsio->kern_data_len = alloc_len; 10212 ctsio->kern_total_len = alloc_len; 10213 } 10214 ctsio->kern_data_resid = 0; 10215 ctsio->kern_rel_offset = 0; 10216 ctsio->kern_sg_entries = 0; 10217 10218 /* 10219 * The control device is always connected. The disk device, on the 10220 * other hand, may not be online all the time. Need to change this 10221 * to figure out whether the disk device is actually online or not. 10222 */ 10223 if (lun != NULL) 10224 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10225 lun->be_lun->lun_type; 10226 else 10227 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10228 10229 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10230 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10231 bl_ptr->max_cmp_write_len = 0xff; 10232 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10233 if (lun != NULL) { 10234 bs = lun->be_lun->blocksize; 10235 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 10236 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10237 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10238 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10239 if (lun->be_lun->ublockexp != 0) { 10240 scsi_ulto4b((1 << lun->be_lun->ublockexp), 10241 bl_ptr->opt_unmap_grain); 10242 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 10243 bl_ptr->unmap_grain_align); 10244 } 10245 } 10246 scsi_ulto4b(lun->be_lun->atomicblock, 10247 bl_ptr->max_atomic_transfer_length); 10248 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10249 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10250 } 10251 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10252 10253 ctl_set_success(ctsio); 10254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10255 ctsio->be_move_done = ctl_config_move_done; 10256 ctl_datamove((union ctl_io *)ctsio); 10257 return (CTL_RETVAL_COMPLETE); 10258} 10259 10260static int 10261ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10262{ 10263 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10264 struct ctl_lun *lun; 10265 const char *value; 10266 u_int i; 10267 10268 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10269 10270 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10271 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10272 ctsio->kern_sg_entries = 0; 10273 10274 if (sizeof(*bdc_ptr) < alloc_len) { 10275 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10276 ctsio->kern_data_len = sizeof(*bdc_ptr); 10277 ctsio->kern_total_len = sizeof(*bdc_ptr); 10278 } else { 10279 ctsio->residual = 0; 10280 ctsio->kern_data_len = alloc_len; 10281 ctsio->kern_total_len = alloc_len; 10282 } 10283 ctsio->kern_data_resid = 0; 10284 ctsio->kern_rel_offset = 0; 10285 ctsio->kern_sg_entries = 0; 10286 10287 /* 10288 * The control device is always connected. The disk device, on the 10289 * other hand, may not be online all the time. Need to change this 10290 * to figure out whether the disk device is actually online or not. 10291 */ 10292 if (lun != NULL) 10293 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10294 lun->be_lun->lun_type; 10295 else 10296 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10297 bdc_ptr->page_code = SVPD_BDC; 10298 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10299 if (lun != NULL && 10300 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10301 i = strtol(value, NULL, 0); 10302 else 10303 i = CTL_DEFAULT_ROTATION_RATE; 10304 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10305 if (lun != NULL && 10306 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10307 i = strtol(value, NULL, 0); 10308 else 10309 i = 0; 10310 bdc_ptr->wab_wac_ff = (i & 0x0f); 10311 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10312 10313 ctl_set_success(ctsio); 10314 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10315 ctsio->be_move_done = ctl_config_move_done; 10316 ctl_datamove((union ctl_io *)ctsio); 10317 return (CTL_RETVAL_COMPLETE); 10318} 10319 10320static int 10321ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10322{ 10323 struct scsi_vpd_logical_block_prov *lbp_ptr; 10324 struct ctl_lun *lun; 10325 10326 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10327 10328 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10329 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10330 ctsio->kern_sg_entries = 0; 10331 10332 if (sizeof(*lbp_ptr) < alloc_len) { 10333 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10334 ctsio->kern_data_len = sizeof(*lbp_ptr); 10335 ctsio->kern_total_len = sizeof(*lbp_ptr); 10336 } else { 10337 ctsio->residual = 0; 10338 ctsio->kern_data_len = alloc_len; 10339 ctsio->kern_total_len = alloc_len; 10340 } 10341 ctsio->kern_data_resid = 0; 10342 ctsio->kern_rel_offset = 0; 10343 ctsio->kern_sg_entries = 0; 10344 10345 /* 10346 * The control device is always connected. The disk device, on the 10347 * other hand, may not be online all the time. Need to change this 10348 * to figure out whether the disk device is actually online or not. 10349 */ 10350 if (lun != NULL) 10351 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10352 lun->be_lun->lun_type; 10353 else 10354 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10355 10356 lbp_ptr->page_code = SVPD_LBP; 10357 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10358 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10359 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10360 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10361 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10362 lbp_ptr->prov_type = SVPD_LBP_THIN; 10363 } 10364 10365 ctl_set_success(ctsio); 10366 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10367 ctsio->be_move_done = ctl_config_move_done; 10368 ctl_datamove((union ctl_io *)ctsio); 10369 return (CTL_RETVAL_COMPLETE); 10370} 10371 10372/* 10373 * INQUIRY with the EVPD bit set. 10374 */ 10375static int 10376ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10377{ 10378 struct ctl_lun *lun; 10379 struct scsi_inquiry *cdb; 10380 int alloc_len, retval; 10381 10382 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10383 cdb = (struct scsi_inquiry *)ctsio->cdb; 10384 alloc_len = scsi_2btoul(cdb->length); 10385 10386 switch (cdb->page_code) { 10387 case SVPD_SUPPORTED_PAGES: 10388 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10389 break; 10390 case SVPD_UNIT_SERIAL_NUMBER: 10391 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10392 break; 10393 case SVPD_DEVICE_ID: 10394 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10395 break; 10396 case SVPD_EXTENDED_INQUIRY_DATA: 10397 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10398 break; 10399 case SVPD_MODE_PAGE_POLICY: 10400 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10401 break; 10402 case SVPD_SCSI_PORTS: 10403 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10404 break; 10405 case SVPD_SCSI_TPC: 10406 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10407 break; 10408 case SVPD_BLOCK_LIMITS: 10409 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10410 goto err; 10411 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10412 break; 10413 case SVPD_BDC: 10414 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10415 goto err; 10416 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10417 break; 10418 case SVPD_LBP: 10419 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10420 goto err; 10421 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10422 break; 10423 default: 10424err: 10425 ctl_set_invalid_field(ctsio, 10426 /*sks_valid*/ 1, 10427 /*command*/ 1, 10428 /*field*/ 2, 10429 /*bit_valid*/ 0, 10430 /*bit*/ 0); 10431 ctl_done((union ctl_io *)ctsio); 10432 retval = CTL_RETVAL_COMPLETE; 10433 break; 10434 } 10435 10436 return (retval); 10437} 10438 10439/* 10440 * Standard INQUIRY data. 10441 */ 10442static int 10443ctl_inquiry_std(struct ctl_scsiio *ctsio) 10444{ 10445 struct scsi_inquiry_data *inq_ptr; 10446 struct scsi_inquiry *cdb; 10447 struct ctl_softc *softc; 10448 struct ctl_lun *lun; 10449 char *val; 10450 uint32_t alloc_len, data_len; 10451 ctl_port_type port_type; 10452 10453 softc = control_softc; 10454 10455 /* 10456 * Figure out whether we're talking to a Fibre Channel port or not. 10457 * We treat the ioctl front end, and any SCSI adapters, as packetized 10458 * SCSI front ends. 10459 */ 10460 port_type = softc->ctl_ports[ 10461 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10462 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10463 port_type = CTL_PORT_SCSI; 10464 10465 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10466 cdb = (struct scsi_inquiry *)ctsio->cdb; 10467 alloc_len = scsi_2btoul(cdb->length); 10468 10469 /* 10470 * We malloc the full inquiry data size here and fill it 10471 * in. If the user only asks for less, we'll give him 10472 * that much. 10473 */ 10474 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10475 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10476 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10477 ctsio->kern_sg_entries = 0; 10478 ctsio->kern_data_resid = 0; 10479 ctsio->kern_rel_offset = 0; 10480 10481 if (data_len < alloc_len) { 10482 ctsio->residual = alloc_len - data_len; 10483 ctsio->kern_data_len = data_len; 10484 ctsio->kern_total_len = data_len; 10485 } else { 10486 ctsio->residual = 0; 10487 ctsio->kern_data_len = alloc_len; 10488 ctsio->kern_total_len = alloc_len; 10489 } 10490 10491 /* 10492 * If we have a LUN configured, report it as connected. Otherwise, 10493 * report that it is offline or no device is supported, depending 10494 * on the value of inquiry_pq_no_lun. 10495 * 10496 * According to the spec (SPC-4 r34), the peripheral qualifier 10497 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10498 * 10499 * "A peripheral device having the specified peripheral device type 10500 * is not connected to this logical unit. However, the device 10501 * server is capable of supporting the specified peripheral device 10502 * type on this logical unit." 10503 * 10504 * According to the same spec, the peripheral qualifier 10505 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10506 * 10507 * "The device server is not capable of supporting a peripheral 10508 * device on this logical unit. For this peripheral qualifier the 10509 * peripheral device type shall be set to 1Fh. All other peripheral 10510 * device type values are reserved for this peripheral qualifier." 10511 * 10512 * Given the text, it would seem that we probably want to report that 10513 * the LUN is offline here. There is no LUN connected, but we can 10514 * support a LUN at the given LUN number. 10515 * 10516 * In the real world, though, it sounds like things are a little 10517 * different: 10518 * 10519 * - Linux, when presented with a LUN with the offline peripheral 10520 * qualifier, will create an sg driver instance for it. So when 10521 * you attach it to CTL, you wind up with a ton of sg driver 10522 * instances. (One for every LUN that Linux bothered to probe.) 10523 * Linux does this despite the fact that it issues a REPORT LUNs 10524 * to LUN 0 to get the inventory of supported LUNs. 10525 * 10526 * - There is other anecdotal evidence (from Emulex folks) about 10527 * arrays that use the offline peripheral qualifier for LUNs that 10528 * are on the "passive" path in an active/passive array. 10529 * 10530 * So the solution is provide a hopefully reasonable default 10531 * (return bad/no LUN) and allow the user to change the behavior 10532 * with a tunable/sysctl variable. 10533 */ 10534 if (lun != NULL) 10535 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10536 lun->be_lun->lun_type; 10537 else if (softc->inquiry_pq_no_lun == 0) 10538 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10539 else 10540 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10541 10542 /* RMB in byte 2 is 0 */ 10543 inq_ptr->version = SCSI_REV_SPC4; 10544 10545 /* 10546 * According to SAM-3, even if a device only supports a single 10547 * level of LUN addressing, it should still set the HISUP bit: 10548 * 10549 * 4.9.1 Logical unit numbers overview 10550 * 10551 * All logical unit number formats described in this standard are 10552 * hierarchical in structure even when only a single level in that 10553 * hierarchy is used. The HISUP bit shall be set to one in the 10554 * standard INQUIRY data (see SPC-2) when any logical unit number 10555 * format described in this standard is used. Non-hierarchical 10556 * formats are outside the scope of this standard. 10557 * 10558 * Therefore we set the HiSup bit here. 10559 * 10560 * The reponse format is 2, per SPC-3. 10561 */ 10562 inq_ptr->response_format = SID_HiSup | 2; 10563 10564 inq_ptr->additional_length = data_len - 10565 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10566 CTL_DEBUG_PRINT(("additional_length = %d\n", 10567 inq_ptr->additional_length)); 10568 10569 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10570 /* 16 bit addressing */ 10571 if (port_type == CTL_PORT_SCSI) 10572 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10573 /* XXX set the SID_MultiP bit here if we're actually going to 10574 respond on multiple ports */ 10575 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10576 10577 /* 16 bit data bus, synchronous transfers */ 10578 if (port_type == CTL_PORT_SCSI) 10579 inq_ptr->flags = SID_WBus16 | SID_Sync; 10580 /* 10581 * XXX KDM do we want to support tagged queueing on the control 10582 * device at all? 10583 */ 10584 if ((lun == NULL) 10585 || (lun->be_lun->lun_type != T_PROCESSOR)) 10586 inq_ptr->flags |= SID_CmdQue; 10587 /* 10588 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10589 * We have 8 bytes for the vendor name, and 16 bytes for the device 10590 * name and 4 bytes for the revision. 10591 */ 10592 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10593 "vendor")) == NULL) { 10594 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10595 } else { 10596 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10597 strncpy(inq_ptr->vendor, val, 10598 min(sizeof(inq_ptr->vendor), strlen(val))); 10599 } 10600 if (lun == NULL) { 10601 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10602 sizeof(inq_ptr->product)); 10603 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10604 switch (lun->be_lun->lun_type) { 10605 case T_DIRECT: 10606 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10607 sizeof(inq_ptr->product)); 10608 break; 10609 case T_PROCESSOR: 10610 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10611 sizeof(inq_ptr->product)); 10612 break; 10613 default: 10614 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10615 sizeof(inq_ptr->product)); 10616 break; 10617 } 10618 } else { 10619 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10620 strncpy(inq_ptr->product, val, 10621 min(sizeof(inq_ptr->product), strlen(val))); 10622 } 10623 10624 /* 10625 * XXX make this a macro somewhere so it automatically gets 10626 * incremented when we make changes. 10627 */ 10628 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10629 "revision")) == NULL) { 10630 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10631 } else { 10632 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10633 strncpy(inq_ptr->revision, val, 10634 min(sizeof(inq_ptr->revision), strlen(val))); 10635 } 10636 10637 /* 10638 * For parallel SCSI, we support double transition and single 10639 * transition clocking. We also support QAS (Quick Arbitration 10640 * and Selection) and Information Unit transfers on both the 10641 * control and array devices. 10642 */ 10643 if (port_type == CTL_PORT_SCSI) 10644 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10645 SID_SPI_IUS; 10646 10647 /* SAM-5 (no version claimed) */ 10648 scsi_ulto2b(0x00A0, inq_ptr->version1); 10649 /* SPC-4 (no version claimed) */ 10650 scsi_ulto2b(0x0460, inq_ptr->version2); 10651 if (port_type == CTL_PORT_FC) { 10652 /* FCP-2 ANSI INCITS.350:2003 */ 10653 scsi_ulto2b(0x0917, inq_ptr->version3); 10654 } else if (port_type == CTL_PORT_SCSI) { 10655 /* SPI-4 ANSI INCITS.362:200x */ 10656 scsi_ulto2b(0x0B56, inq_ptr->version3); 10657 } else if (port_type == CTL_PORT_ISCSI) { 10658 /* iSCSI (no version claimed) */ 10659 scsi_ulto2b(0x0960, inq_ptr->version3); 10660 } else if (port_type == CTL_PORT_SAS) { 10661 /* SAS (no version claimed) */ 10662 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10663 } 10664 10665 if (lun == NULL) { 10666 /* SBC-4 (no version claimed) */ 10667 scsi_ulto2b(0x0600, inq_ptr->version4); 10668 } else { 10669 switch (lun->be_lun->lun_type) { 10670 case T_DIRECT: 10671 /* SBC-4 (no version claimed) */ 10672 scsi_ulto2b(0x0600, inq_ptr->version4); 10673 break; 10674 case T_PROCESSOR: 10675 default: 10676 break; 10677 } 10678 } 10679 10680 ctl_set_success(ctsio); 10681 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10682 ctsio->be_move_done = ctl_config_move_done; 10683 ctl_datamove((union ctl_io *)ctsio); 10684 return (CTL_RETVAL_COMPLETE); 10685} 10686 10687int 10688ctl_inquiry(struct ctl_scsiio *ctsio) 10689{ 10690 struct scsi_inquiry *cdb; 10691 int retval; 10692 10693 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10694 10695 cdb = (struct scsi_inquiry *)ctsio->cdb; 10696 if (cdb->byte2 & SI_EVPD) 10697 retval = ctl_inquiry_evpd(ctsio); 10698 else if (cdb->page_code == 0) 10699 retval = ctl_inquiry_std(ctsio); 10700 else { 10701 ctl_set_invalid_field(ctsio, 10702 /*sks_valid*/ 1, 10703 /*command*/ 1, 10704 /*field*/ 2, 10705 /*bit_valid*/ 0, 10706 /*bit*/ 0); 10707 ctl_done((union ctl_io *)ctsio); 10708 return (CTL_RETVAL_COMPLETE); 10709 } 10710 10711 return (retval); 10712} 10713 10714/* 10715 * For known CDB types, parse the LBA and length. 10716 */ 10717static int 10718ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10719{ 10720 if (io->io_hdr.io_type != CTL_IO_SCSI) 10721 return (1); 10722 10723 switch (io->scsiio.cdb[0]) { 10724 case COMPARE_AND_WRITE: { 10725 struct scsi_compare_and_write *cdb; 10726 10727 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10728 10729 *lba = scsi_8btou64(cdb->addr); 10730 *len = cdb->length; 10731 break; 10732 } 10733 case READ_6: 10734 case WRITE_6: { 10735 struct scsi_rw_6 *cdb; 10736 10737 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10738 10739 *lba = scsi_3btoul(cdb->addr); 10740 /* only 5 bits are valid in the most significant address byte */ 10741 *lba &= 0x1fffff; 10742 *len = cdb->length; 10743 break; 10744 } 10745 case READ_10: 10746 case WRITE_10: { 10747 struct scsi_rw_10 *cdb; 10748 10749 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10750 10751 *lba = scsi_4btoul(cdb->addr); 10752 *len = scsi_2btoul(cdb->length); 10753 break; 10754 } 10755 case WRITE_VERIFY_10: { 10756 struct scsi_write_verify_10 *cdb; 10757 10758 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10759 10760 *lba = scsi_4btoul(cdb->addr); 10761 *len = scsi_2btoul(cdb->length); 10762 break; 10763 } 10764 case READ_12: 10765 case WRITE_12: { 10766 struct scsi_rw_12 *cdb; 10767 10768 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10769 10770 *lba = scsi_4btoul(cdb->addr); 10771 *len = scsi_4btoul(cdb->length); 10772 break; 10773 } 10774 case WRITE_VERIFY_12: { 10775 struct scsi_write_verify_12 *cdb; 10776 10777 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10778 10779 *lba = scsi_4btoul(cdb->addr); 10780 *len = scsi_4btoul(cdb->length); 10781 break; 10782 } 10783 case READ_16: 10784 case WRITE_16: 10785 case WRITE_ATOMIC_16: { 10786 struct scsi_rw_16 *cdb; 10787 10788 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10789 10790 *lba = scsi_8btou64(cdb->addr); 10791 *len = scsi_4btoul(cdb->length); 10792 break; 10793 } 10794 case WRITE_VERIFY_16: { 10795 struct scsi_write_verify_16 *cdb; 10796 10797 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10798 10799 *lba = scsi_8btou64(cdb->addr); 10800 *len = scsi_4btoul(cdb->length); 10801 break; 10802 } 10803 case WRITE_SAME_10: { 10804 struct scsi_write_same_10 *cdb; 10805 10806 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10807 10808 *lba = scsi_4btoul(cdb->addr); 10809 *len = scsi_2btoul(cdb->length); 10810 break; 10811 } 10812 case WRITE_SAME_16: { 10813 struct scsi_write_same_16 *cdb; 10814 10815 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10816 10817 *lba = scsi_8btou64(cdb->addr); 10818 *len = scsi_4btoul(cdb->length); 10819 break; 10820 } 10821 case VERIFY_10: { 10822 struct scsi_verify_10 *cdb; 10823 10824 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10825 10826 *lba = scsi_4btoul(cdb->addr); 10827 *len = scsi_2btoul(cdb->length); 10828 break; 10829 } 10830 case VERIFY_12: { 10831 struct scsi_verify_12 *cdb; 10832 10833 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10834 10835 *lba = scsi_4btoul(cdb->addr); 10836 *len = scsi_4btoul(cdb->length); 10837 break; 10838 } 10839 case VERIFY_16: { 10840 struct scsi_verify_16 *cdb; 10841 10842 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10843 10844 *lba = scsi_8btou64(cdb->addr); 10845 *len = scsi_4btoul(cdb->length); 10846 break; 10847 } 10848 case UNMAP: { 10849 *lba = 0; 10850 *len = UINT64_MAX; 10851 break; 10852 } 10853 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10854 struct scsi_get_lba_status *cdb; 10855 10856 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10857 *lba = scsi_8btou64(cdb->addr); 10858 *len = UINT32_MAX; 10859 break; 10860 } 10861 default: 10862 return (1); 10863 break; /* NOTREACHED */ 10864 } 10865 10866 return (0); 10867} 10868 10869static ctl_action 10870ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10871 bool seq) 10872{ 10873 uint64_t endlba1, endlba2; 10874 10875 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10876 endlba2 = lba2 + len2 - 1; 10877 10878 if ((endlba1 < lba2) || (endlba2 < lba1)) 10879 return (CTL_ACTION_PASS); 10880 else 10881 return (CTL_ACTION_BLOCK); 10882} 10883 10884static int 10885ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10886{ 10887 struct ctl_ptr_len_flags *ptrlen; 10888 struct scsi_unmap_desc *buf, *end, *range; 10889 uint64_t lba; 10890 uint32_t len; 10891 10892 /* If not UNMAP -- go other way. */ 10893 if (io->io_hdr.io_type != CTL_IO_SCSI || 10894 io->scsiio.cdb[0] != UNMAP) 10895 return (CTL_ACTION_ERROR); 10896 10897 /* If UNMAP without data -- block and wait for data. */ 10898 ptrlen = (struct ctl_ptr_len_flags *) 10899 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10900 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10901 ptrlen->ptr == NULL) 10902 return (CTL_ACTION_BLOCK); 10903 10904 /* UNMAP with data -- check for collision. */ 10905 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10906 end = buf + ptrlen->len / sizeof(*buf); 10907 for (range = buf; range < end; range++) { 10908 lba = scsi_8btou64(range->lba); 10909 len = scsi_4btoul(range->length); 10910 if ((lba < lba2 + len2) && (lba + len > lba2)) 10911 return (CTL_ACTION_BLOCK); 10912 } 10913 return (CTL_ACTION_PASS); 10914} 10915 10916static ctl_action 10917ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10918{ 10919 uint64_t lba1, lba2; 10920 uint64_t len1, len2; 10921 int retval; 10922 10923 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10924 return (CTL_ACTION_ERROR); 10925 10926 retval = ctl_extent_check_unmap(io1, lba2, len2); 10927 if (retval != CTL_ACTION_ERROR) 10928 return (retval); 10929 10930 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10931 return (CTL_ACTION_ERROR); 10932 10933 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10934} 10935 10936static ctl_action 10937ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10938{ 10939 uint64_t lba1, lba2; 10940 uint64_t len1, len2; 10941 10942 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10943 return (CTL_ACTION_ERROR); 10944 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10945 return (CTL_ACTION_ERROR); 10946 10947 if (lba1 + len1 == lba2) 10948 return (CTL_ACTION_BLOCK); 10949 return (CTL_ACTION_PASS); 10950} 10951 10952static ctl_action 10953ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10954 union ctl_io *ooa_io) 10955{ 10956 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10957 ctl_serialize_action *serialize_row; 10958 10959 /* 10960 * The initiator attempted multiple untagged commands at the same 10961 * time. Can't do that. 10962 */ 10963 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10964 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10965 && ((pending_io->io_hdr.nexus.targ_port == 10966 ooa_io->io_hdr.nexus.targ_port) 10967 && (pending_io->io_hdr.nexus.initid.id == 10968 ooa_io->io_hdr.nexus.initid.id)) 10969 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10970 CTL_FLAG_STATUS_SENT)) == 0)) 10971 return (CTL_ACTION_OVERLAP); 10972 10973 /* 10974 * The initiator attempted to send multiple tagged commands with 10975 * the same ID. (It's fine if different initiators have the same 10976 * tag ID.) 10977 * 10978 * Even if all of those conditions are true, we don't kill the I/O 10979 * if the command ahead of us has been aborted. We won't end up 10980 * sending it to the FETD, and it's perfectly legal to resend a 10981 * command with the same tag number as long as the previous 10982 * instance of this tag number has been aborted somehow. 10983 */ 10984 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10985 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10986 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10987 && ((pending_io->io_hdr.nexus.targ_port == 10988 ooa_io->io_hdr.nexus.targ_port) 10989 && (pending_io->io_hdr.nexus.initid.id == 10990 ooa_io->io_hdr.nexus.initid.id)) 10991 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10992 CTL_FLAG_STATUS_SENT)) == 0)) 10993 return (CTL_ACTION_OVERLAP_TAG); 10994 10995 /* 10996 * If we get a head of queue tag, SAM-3 says that we should 10997 * immediately execute it. 10998 * 10999 * What happens if this command would normally block for some other 11000 * reason? e.g. a request sense with a head of queue tag 11001 * immediately after a write. Normally that would block, but this 11002 * will result in its getting executed immediately... 11003 * 11004 * We currently return "pass" instead of "skip", so we'll end up 11005 * going through the rest of the queue to check for overlapped tags. 11006 * 11007 * XXX KDM check for other types of blockage first?? 11008 */ 11009 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11010 return (CTL_ACTION_PASS); 11011 11012 /* 11013 * Ordered tags have to block until all items ahead of them 11014 * have completed. If we get called with an ordered tag, we always 11015 * block, if something else is ahead of us in the queue. 11016 */ 11017 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11018 return (CTL_ACTION_BLOCK); 11019 11020 /* 11021 * Simple tags get blocked until all head of queue and ordered tags 11022 * ahead of them have completed. I'm lumping untagged commands in 11023 * with simple tags here. XXX KDM is that the right thing to do? 11024 */ 11025 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11026 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11027 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11028 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11029 return (CTL_ACTION_BLOCK); 11030 11031 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11032 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11033 11034 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11035 11036 switch (serialize_row[pending_entry->seridx]) { 11037 case CTL_SER_BLOCK: 11038 return (CTL_ACTION_BLOCK); 11039 case CTL_SER_EXTENT: 11040 return (ctl_extent_check(ooa_io, pending_io, 11041 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11042 case CTL_SER_EXTENTOPT: 11043 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11044 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11045 return (ctl_extent_check(ooa_io, pending_io, 11046 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11047 return (CTL_ACTION_PASS); 11048 case CTL_SER_EXTENTSEQ: 11049 if (lun->serseq != CTL_LUN_SERSEQ_OFF) 11050 return (ctl_extent_check_seq(ooa_io, pending_io)); 11051 return (CTL_ACTION_PASS); 11052 case CTL_SER_PASS: 11053 return (CTL_ACTION_PASS); 11054 case CTL_SER_BLOCKOPT: 11055 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11056 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11057 return (CTL_ACTION_BLOCK); 11058 return (CTL_ACTION_PASS); 11059 case CTL_SER_SKIP: 11060 return (CTL_ACTION_SKIP); 11061 default: 11062 panic("invalid serialization value %d", 11063 serialize_row[pending_entry->seridx]); 11064 } 11065 11066 return (CTL_ACTION_ERROR); 11067} 11068 11069/* 11070 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11071 * Assumptions: 11072 * - pending_io is generally either incoming, or on the blocked queue 11073 * - starting I/O is the I/O we want to start the check with. 11074 */ 11075static ctl_action 11076ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11077 union ctl_io *starting_io) 11078{ 11079 union ctl_io *ooa_io; 11080 ctl_action action; 11081 11082 mtx_assert(&lun->lun_lock, MA_OWNED); 11083 11084 /* 11085 * Run back along the OOA queue, starting with the current 11086 * blocked I/O and going through every I/O before it on the 11087 * queue. If starting_io is NULL, we'll just end up returning 11088 * CTL_ACTION_PASS. 11089 */ 11090 for (ooa_io = starting_io; ooa_io != NULL; 11091 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11092 ooa_links)){ 11093 11094 /* 11095 * This routine just checks to see whether 11096 * cur_blocked is blocked by ooa_io, which is ahead 11097 * of it in the queue. It doesn't queue/dequeue 11098 * cur_blocked. 11099 */ 11100 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11101 switch (action) { 11102 case CTL_ACTION_BLOCK: 11103 case CTL_ACTION_OVERLAP: 11104 case CTL_ACTION_OVERLAP_TAG: 11105 case CTL_ACTION_SKIP: 11106 case CTL_ACTION_ERROR: 11107 return (action); 11108 break; /* NOTREACHED */ 11109 case CTL_ACTION_PASS: 11110 break; 11111 default: 11112 panic("invalid action %d", action); 11113 break; /* NOTREACHED */ 11114 } 11115 } 11116 11117 return (CTL_ACTION_PASS); 11118} 11119 11120/* 11121 * Assumptions: 11122 * - An I/O has just completed, and has been removed from the per-LUN OOA 11123 * queue, so some items on the blocked queue may now be unblocked. 11124 */ 11125static int 11126ctl_check_blocked(struct ctl_lun *lun) 11127{ 11128 union ctl_io *cur_blocked, *next_blocked; 11129 11130 mtx_assert(&lun->lun_lock, MA_OWNED); 11131 11132 /* 11133 * Run forward from the head of the blocked queue, checking each 11134 * entry against the I/Os prior to it on the OOA queue to see if 11135 * there is still any blockage. 11136 * 11137 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11138 * with our removing a variable on it while it is traversing the 11139 * list. 11140 */ 11141 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11142 cur_blocked != NULL; cur_blocked = next_blocked) { 11143 union ctl_io *prev_ooa; 11144 ctl_action action; 11145 11146 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11147 blocked_links); 11148 11149 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11150 ctl_ooaq, ooa_links); 11151 11152 /* 11153 * If cur_blocked happens to be the first item in the OOA 11154 * queue now, prev_ooa will be NULL, and the action 11155 * returned will just be CTL_ACTION_PASS. 11156 */ 11157 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11158 11159 switch (action) { 11160 case CTL_ACTION_BLOCK: 11161 /* Nothing to do here, still blocked */ 11162 break; 11163 case CTL_ACTION_OVERLAP: 11164 case CTL_ACTION_OVERLAP_TAG: 11165 /* 11166 * This shouldn't happen! In theory we've already 11167 * checked this command for overlap... 11168 */ 11169 break; 11170 case CTL_ACTION_PASS: 11171 case CTL_ACTION_SKIP: { 11172 const struct ctl_cmd_entry *entry; 11173 int isc_retval; 11174 11175 /* 11176 * The skip case shouldn't happen, this transaction 11177 * should have never made it onto the blocked queue. 11178 */ 11179 /* 11180 * This I/O is no longer blocked, we can remove it 11181 * from the blocked queue. Since this is a TAILQ 11182 * (doubly linked list), we can do O(1) removals 11183 * from any place on the list. 11184 */ 11185 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11186 blocked_links); 11187 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11188 11189 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11190 /* 11191 * Need to send IO back to original side to 11192 * run 11193 */ 11194 union ctl_ha_msg msg_info; 11195 11196 msg_info.hdr.original_sc = 11197 cur_blocked->io_hdr.original_sc; 11198 msg_info.hdr.serializing_sc = cur_blocked; 11199 msg_info.hdr.msg_type = CTL_MSG_R2R; 11200 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11201 &msg_info, sizeof(msg_info), 0)) > 11202 CTL_HA_STATUS_SUCCESS) { 11203 printf("CTL:Check Blocked error from " 11204 "ctl_ha_msg_send %d\n", 11205 isc_retval); 11206 } 11207 break; 11208 } 11209 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11210 11211 /* 11212 * Check this I/O for LUN state changes that may 11213 * have happened while this command was blocked. 11214 * The LUN state may have been changed by a command 11215 * ahead of us in the queue, so we need to re-check 11216 * for any states that can be caused by SCSI 11217 * commands. 11218 */ 11219 if (ctl_scsiio_lun_check(lun, entry, 11220 &cur_blocked->scsiio) == 0) { 11221 cur_blocked->io_hdr.flags |= 11222 CTL_FLAG_IS_WAS_ON_RTR; 11223 ctl_enqueue_rtr(cur_blocked); 11224 } else 11225 ctl_done(cur_blocked); 11226 break; 11227 } 11228 default: 11229 /* 11230 * This probably shouldn't happen -- we shouldn't 11231 * get CTL_ACTION_ERROR, or anything else. 11232 */ 11233 break; 11234 } 11235 } 11236 11237 return (CTL_RETVAL_COMPLETE); 11238} 11239 11240/* 11241 * This routine (with one exception) checks LUN flags that can be set by 11242 * commands ahead of us in the OOA queue. These flags have to be checked 11243 * when a command initially comes in, and when we pull a command off the 11244 * blocked queue and are preparing to execute it. The reason we have to 11245 * check these flags for commands on the blocked queue is that the LUN 11246 * state may have been changed by a command ahead of us while we're on the 11247 * blocked queue. 11248 * 11249 * Ordering is somewhat important with these checks, so please pay 11250 * careful attention to the placement of any new checks. 11251 */ 11252static int 11253ctl_scsiio_lun_check(struct ctl_lun *lun, 11254 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11255{ 11256 struct ctl_softc *softc = lun->ctl_softc; 11257 int retval; 11258 uint32_t residx; 11259 11260 retval = 0; 11261 11262 mtx_assert(&lun->lun_lock, MA_OWNED); 11263 11264 /* 11265 * If this shelf is a secondary shelf controller, we have to reject 11266 * any media access commands. 11267 */ 11268 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11269 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11270 ctl_set_lun_standby(ctsio); 11271 retval = 1; 11272 goto bailout; 11273 } 11274 11275 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11276 if (lun->flags & CTL_LUN_READONLY) { 11277 ctl_set_sense(ctsio, /*current_error*/ 1, 11278 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11279 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11280 retval = 1; 11281 goto bailout; 11282 } 11283 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11284 .eca_and_aen & SCP_SWP) != 0) { 11285 ctl_set_sense(ctsio, /*current_error*/ 1, 11286 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11287 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11288 retval = 1; 11289 goto bailout; 11290 } 11291 } 11292 11293 /* 11294 * Check for a reservation conflict. If this command isn't allowed 11295 * even on reserved LUNs, and if this initiator isn't the one who 11296 * reserved us, reject the command with a reservation conflict. 11297 */ 11298 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11299 if ((lun->flags & CTL_LUN_RESERVED) 11300 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11301 if (lun->res_idx != residx) { 11302 ctl_set_reservation_conflict(ctsio); 11303 retval = 1; 11304 goto bailout; 11305 } 11306 } 11307 11308 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11309 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11310 /* No reservation or command is allowed. */; 11311 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11312 (lun->res_type == SPR_TYPE_WR_EX || 11313 lun->res_type == SPR_TYPE_WR_EX_RO || 11314 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11315 /* The command is allowed for Write Exclusive resv. */; 11316 } else { 11317 /* 11318 * if we aren't registered or it's a res holder type 11319 * reservation and this isn't the res holder then set a 11320 * conflict. 11321 */ 11322 if (ctl_get_prkey(lun, residx) == 0 11323 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11324 ctl_set_reservation_conflict(ctsio); 11325 retval = 1; 11326 goto bailout; 11327 } 11328 11329 } 11330 11331 if ((lun->flags & CTL_LUN_OFFLINE) 11332 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11333 ctl_set_lun_not_ready(ctsio); 11334 retval = 1; 11335 goto bailout; 11336 } 11337 11338 /* 11339 * If the LUN is stopped, see if this particular command is allowed 11340 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11341 */ 11342 if ((lun->flags & CTL_LUN_STOPPED) 11343 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11344 /* "Logical unit not ready, initializing cmd. required" */ 11345 ctl_set_lun_stopped(ctsio); 11346 retval = 1; 11347 goto bailout; 11348 } 11349 11350 if ((lun->flags & CTL_LUN_INOPERABLE) 11351 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11352 /* "Medium format corrupted" */ 11353 ctl_set_medium_format_corrupted(ctsio); 11354 retval = 1; 11355 goto bailout; 11356 } 11357 11358bailout: 11359 return (retval); 11360 11361} 11362 11363static void 11364ctl_failover_io(union ctl_io *io, int have_lock) 11365{ 11366 ctl_set_busy(&io->scsiio); 11367 ctl_done(io); 11368} 11369 11370static void 11371ctl_failover(void) 11372{ 11373 struct ctl_lun *lun; 11374 struct ctl_softc *softc; 11375 union ctl_io *next_io, *pending_io; 11376 union ctl_io *io; 11377 int lun_idx; 11378 11379 softc = control_softc; 11380 11381 mtx_lock(&softc->ctl_lock); 11382 /* 11383 * Remove any cmds from the other SC from the rtr queue. These 11384 * will obviously only be for LUNs for which we're the primary. 11385 * We can't send status or get/send data for these commands. 11386 * Since they haven't been executed yet, we can just remove them. 11387 * We'll either abort them or delete them below, depending on 11388 * which HA mode we're in. 11389 */ 11390#ifdef notyet 11391 mtx_lock(&softc->queue_lock); 11392 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 11393 io != NULL; io = next_io) { 11394 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11395 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11396 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 11397 ctl_io_hdr, links); 11398 } 11399 mtx_unlock(&softc->queue_lock); 11400#endif 11401 11402 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) { 11403 lun = softc->ctl_luns[lun_idx]; 11404 if (lun==NULL) 11405 continue; 11406 11407 /* 11408 * Processor LUNs are primary on both sides. 11409 * XXX will this always be true? 11410 */ 11411 if (lun->be_lun->lun_type == T_PROCESSOR) 11412 continue; 11413 11414 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11415 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11416 printf("FAILOVER: primary lun %d\n", lun_idx); 11417 /* 11418 * Remove all commands from the other SC. First from the 11419 * blocked queue then from the ooa queue. Once we have 11420 * removed them. Call ctl_check_blocked to see if there 11421 * is anything that can run. 11422 */ 11423 for (io = (union ctl_io *)TAILQ_FIRST( 11424 &lun->blocked_queue); io != NULL; io = next_io) { 11425 11426 next_io = (union ctl_io *)TAILQ_NEXT( 11427 &io->io_hdr, blocked_links); 11428 11429 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11430 TAILQ_REMOVE(&lun->blocked_queue, 11431 &io->io_hdr,blocked_links); 11432 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11433 TAILQ_REMOVE(&lun->ooa_queue, 11434 &io->io_hdr, ooa_links); 11435 11436 ctl_free_io(io); 11437 } 11438 } 11439 11440 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11441 io != NULL; io = next_io) { 11442 11443 next_io = (union ctl_io *)TAILQ_NEXT( 11444 &io->io_hdr, ooa_links); 11445 11446 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11447 11448 TAILQ_REMOVE(&lun->ooa_queue, 11449 &io->io_hdr, 11450 ooa_links); 11451 11452 ctl_free_io(io); 11453 } 11454 } 11455 ctl_check_blocked(lun); 11456 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11457 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11458 11459 printf("FAILOVER: primary lun %d\n", lun_idx); 11460 /* 11461 * Abort all commands from the other SC. We can't 11462 * send status back for them now. These should get 11463 * cleaned up when they are completed or come out 11464 * for a datamove operation. 11465 */ 11466 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11467 io != NULL; io = next_io) { 11468 next_io = (union ctl_io *)TAILQ_NEXT( 11469 &io->io_hdr, ooa_links); 11470 11471 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11472 io->io_hdr.flags |= CTL_FLAG_ABORT; 11473 } 11474 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11475 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11476 11477 printf("FAILOVER: secondary lun %d\n", lun_idx); 11478 11479 lun->flags |= CTL_LUN_PRIMARY_SC; 11480 11481 /* 11482 * We send all I/O that was sent to this controller 11483 * and redirected to the other side back with 11484 * busy status, and have the initiator retry it. 11485 * Figuring out how much data has been transferred, 11486 * etc. and picking up where we left off would be 11487 * very tricky. 11488 * 11489 * XXX KDM need to remove I/O from the blocked 11490 * queue as well! 11491 */ 11492 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11493 &lun->ooa_queue); pending_io != NULL; 11494 pending_io = next_io) { 11495 11496 next_io = (union ctl_io *)TAILQ_NEXT( 11497 &pending_io->io_hdr, ooa_links); 11498 11499 pending_io->io_hdr.flags &= 11500 ~CTL_FLAG_SENT_2OTHER_SC; 11501 11502 if (pending_io->io_hdr.flags & 11503 CTL_FLAG_IO_ACTIVE) { 11504 pending_io->io_hdr.flags |= 11505 CTL_FLAG_FAILOVER; 11506 } else { 11507 ctl_set_busy(&pending_io->scsiio); 11508 ctl_done(pending_io); 11509 } 11510 } 11511 11512 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11513 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11514 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11515 printf("FAILOVER: secondary lun %d\n", lun_idx); 11516 /* 11517 * if the first io on the OOA is not on the RtR queue 11518 * add it. 11519 */ 11520 lun->flags |= CTL_LUN_PRIMARY_SC; 11521 11522 pending_io = (union ctl_io *)TAILQ_FIRST( 11523 &lun->ooa_queue); 11524 if (pending_io==NULL) { 11525 printf("Nothing on OOA queue\n"); 11526 continue; 11527 } 11528 11529 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11530 if ((pending_io->io_hdr.flags & 11531 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11532 pending_io->io_hdr.flags |= 11533 CTL_FLAG_IS_WAS_ON_RTR; 11534 ctl_enqueue_rtr(pending_io); 11535 } 11536#if 0 11537 else 11538 { 11539 printf("Tag 0x%04x is running\n", 11540 pending_io->scsiio.tag_num); 11541 } 11542#endif 11543 11544 next_io = (union ctl_io *)TAILQ_NEXT( 11545 &pending_io->io_hdr, ooa_links); 11546 for (pending_io=next_io; pending_io != NULL; 11547 pending_io = next_io) { 11548 pending_io->io_hdr.flags &= 11549 ~CTL_FLAG_SENT_2OTHER_SC; 11550 next_io = (union ctl_io *)TAILQ_NEXT( 11551 &pending_io->io_hdr, ooa_links); 11552 if (pending_io->io_hdr.flags & 11553 CTL_FLAG_IS_WAS_ON_RTR) { 11554#if 0 11555 printf("Tag 0x%04x is running\n", 11556 pending_io->scsiio.tag_num); 11557#endif 11558 continue; 11559 } 11560 11561 switch (ctl_check_ooa(lun, pending_io, 11562 (union ctl_io *)TAILQ_PREV( 11563 &pending_io->io_hdr, ctl_ooaq, 11564 ooa_links))) { 11565 11566 case CTL_ACTION_BLOCK: 11567 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11568 &pending_io->io_hdr, 11569 blocked_links); 11570 pending_io->io_hdr.flags |= 11571 CTL_FLAG_BLOCKED; 11572 break; 11573 case CTL_ACTION_PASS: 11574 case CTL_ACTION_SKIP: 11575 pending_io->io_hdr.flags |= 11576 CTL_FLAG_IS_WAS_ON_RTR; 11577 ctl_enqueue_rtr(pending_io); 11578 break; 11579 case CTL_ACTION_OVERLAP: 11580 ctl_set_overlapped_cmd( 11581 (struct ctl_scsiio *)pending_io); 11582 ctl_done(pending_io); 11583 break; 11584 case CTL_ACTION_OVERLAP_TAG: 11585 ctl_set_overlapped_tag( 11586 (struct ctl_scsiio *)pending_io, 11587 pending_io->scsiio.tag_num & 0xff); 11588 ctl_done(pending_io); 11589 break; 11590 case CTL_ACTION_ERROR: 11591 default: 11592 ctl_set_internal_failure( 11593 (struct ctl_scsiio *)pending_io, 11594 0, // sks_valid 11595 0); //retry count 11596 ctl_done(pending_io); 11597 break; 11598 } 11599 } 11600 11601 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11602 } else { 11603 panic("Unhandled HA mode failover, LUN flags = %#x, " 11604 "ha_mode = #%x", lun->flags, softc->ha_mode); 11605 } 11606 } 11607 ctl_pause_rtr = 0; 11608 mtx_unlock(&softc->ctl_lock); 11609} 11610 11611static void 11612ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 11613 ctl_ua_type ua_type) 11614{ 11615 struct ctl_lun *lun; 11616 ctl_ua_type *pu; 11617 11618 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 11619 11620 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 11621 mtx_lock(&lun->lun_lock); 11622 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 11623 if (pu != NULL) 11624 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type; 11625 mtx_unlock(&lun->lun_lock); 11626 } 11627} 11628 11629static int 11630ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11631{ 11632 struct ctl_lun *lun; 11633 const struct ctl_cmd_entry *entry; 11634 uint32_t initidx, targ_lun; 11635 int retval; 11636 11637 retval = 0; 11638 11639 lun = NULL; 11640 11641 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11642 if ((targ_lun < CTL_MAX_LUNS) 11643 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11644 /* 11645 * If the LUN is invalid, pretend that it doesn't exist. 11646 * It will go away as soon as all pending I/O has been 11647 * completed. 11648 */ 11649 mtx_lock(&lun->lun_lock); 11650 if (lun->flags & CTL_LUN_DISABLED) { 11651 mtx_unlock(&lun->lun_lock); 11652 lun = NULL; 11653 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11654 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11655 } else { 11656 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11657 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11658 lun->be_lun; 11659 if (lun->be_lun->lun_type == T_PROCESSOR) { 11660 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11661 } 11662 11663 /* 11664 * Every I/O goes into the OOA queue for a 11665 * particular LUN, and stays there until completion. 11666 */ 11667#ifdef CTL_TIME_IO 11668 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11669 lun->idle_time += getsbinuptime() - 11670 lun->last_busy; 11671 } 11672#endif 11673 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11674 ooa_links); 11675 } 11676 } else { 11677 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11678 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11679 } 11680 11681 /* Get command entry and return error if it is unsuppotyed. */ 11682 entry = ctl_validate_command(ctsio); 11683 if (entry == NULL) { 11684 if (lun) 11685 mtx_unlock(&lun->lun_lock); 11686 return (retval); 11687 } 11688 11689 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11690 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11691 11692 /* 11693 * Check to see whether we can send this command to LUNs that don't 11694 * exist. This should pretty much only be the case for inquiry 11695 * and request sense. Further checks, below, really require having 11696 * a LUN, so we can't really check the command anymore. Just put 11697 * it on the rtr queue. 11698 */ 11699 if (lun == NULL) { 11700 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11701 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11702 ctl_enqueue_rtr((union ctl_io *)ctsio); 11703 return (retval); 11704 } 11705 11706 ctl_set_unsupported_lun(ctsio); 11707 ctl_done((union ctl_io *)ctsio); 11708 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11709 return (retval); 11710 } else { 11711 /* 11712 * Make sure we support this particular command on this LUN. 11713 * e.g., we don't support writes to the control LUN. 11714 */ 11715 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11716 mtx_unlock(&lun->lun_lock); 11717 ctl_set_invalid_opcode(ctsio); 11718 ctl_done((union ctl_io *)ctsio); 11719 return (retval); 11720 } 11721 } 11722 11723 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11724 11725#ifdef CTL_WITH_CA 11726 /* 11727 * If we've got a request sense, it'll clear the contingent 11728 * allegiance condition. Otherwise, if we have a CA condition for 11729 * this initiator, clear it, because it sent down a command other 11730 * than request sense. 11731 */ 11732 if ((ctsio->cdb[0] != REQUEST_SENSE) 11733 && (ctl_is_set(lun->have_ca, initidx))) 11734 ctl_clear_mask(lun->have_ca, initidx); 11735#endif 11736 11737 /* 11738 * If the command has this flag set, it handles its own unit 11739 * attention reporting, we shouldn't do anything. Otherwise we 11740 * check for any pending unit attentions, and send them back to the 11741 * initiator. We only do this when a command initially comes in, 11742 * not when we pull it off the blocked queue. 11743 * 11744 * According to SAM-3, section 5.3.2, the order that things get 11745 * presented back to the host is basically unit attentions caused 11746 * by some sort of reset event, busy status, reservation conflicts 11747 * or task set full, and finally any other status. 11748 * 11749 * One issue here is that some of the unit attentions we report 11750 * don't fall into the "reset" category (e.g. "reported luns data 11751 * has changed"). So reporting it here, before the reservation 11752 * check, may be technically wrong. I guess the only thing to do 11753 * would be to check for and report the reset events here, and then 11754 * check for the other unit attention types after we check for a 11755 * reservation conflict. 11756 * 11757 * XXX KDM need to fix this 11758 */ 11759 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11760 ctl_ua_type ua_type; 11761 scsi_sense_data_type sense_format; 11762 11763 if (lun->flags & CTL_LUN_SENSE_DESC) 11764 sense_format = SSD_TYPE_DESC; 11765 else 11766 sense_format = SSD_TYPE_FIXED; 11767 11768 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11769 sense_format); 11770 if (ua_type != CTL_UA_NONE) { 11771 mtx_unlock(&lun->lun_lock); 11772 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11773 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11774 ctsio->sense_len = SSD_FULL_SIZE; 11775 ctl_done((union ctl_io *)ctsio); 11776 return (retval); 11777 } 11778 } 11779 11780 11781 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11782 mtx_unlock(&lun->lun_lock); 11783 ctl_done((union ctl_io *)ctsio); 11784 return (retval); 11785 } 11786 11787 /* 11788 * XXX CHD this is where we want to send IO to other side if 11789 * this LUN is secondary on this SC. We will need to make a copy 11790 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11791 * the copy we send as FROM_OTHER. 11792 * We also need to stuff the address of the original IO so we can 11793 * find it easily. Something similar will need be done on the other 11794 * side so when we are done we can find the copy. 11795 */ 11796 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11797 union ctl_ha_msg msg_info; 11798 int isc_retval; 11799 11800 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11801 11802 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11803 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11804#if 0 11805 printf("1. ctsio %p\n", ctsio); 11806#endif 11807 msg_info.hdr.serializing_sc = NULL; 11808 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11809 msg_info.scsi.tag_num = ctsio->tag_num; 11810 msg_info.scsi.tag_type = ctsio->tag_type; 11811 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11812 11813 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11814 11815 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11816 (void *)&msg_info, sizeof(msg_info), 0)) > 11817 CTL_HA_STATUS_SUCCESS) { 11818 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11819 isc_retval); 11820 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11821 } else { 11822#if 0 11823 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11824#endif 11825 } 11826 11827 /* 11828 * XXX KDM this I/O is off the incoming queue, but hasn't 11829 * been inserted on any other queue. We may need to come 11830 * up with a holding queue while we wait for serialization 11831 * so that we have an idea of what we're waiting for from 11832 * the other side. 11833 */ 11834 mtx_unlock(&lun->lun_lock); 11835 return (retval); 11836 } 11837 11838 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11839 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11840 ctl_ooaq, ooa_links))) { 11841 case CTL_ACTION_BLOCK: 11842 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11843 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11844 blocked_links); 11845 mtx_unlock(&lun->lun_lock); 11846 return (retval); 11847 case CTL_ACTION_PASS: 11848 case CTL_ACTION_SKIP: 11849 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11850 mtx_unlock(&lun->lun_lock); 11851 ctl_enqueue_rtr((union ctl_io *)ctsio); 11852 break; 11853 case CTL_ACTION_OVERLAP: 11854 mtx_unlock(&lun->lun_lock); 11855 ctl_set_overlapped_cmd(ctsio); 11856 ctl_done((union ctl_io *)ctsio); 11857 break; 11858 case CTL_ACTION_OVERLAP_TAG: 11859 mtx_unlock(&lun->lun_lock); 11860 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11861 ctl_done((union ctl_io *)ctsio); 11862 break; 11863 case CTL_ACTION_ERROR: 11864 default: 11865 mtx_unlock(&lun->lun_lock); 11866 ctl_set_internal_failure(ctsio, 11867 /*sks_valid*/ 0, 11868 /*retry_count*/ 0); 11869 ctl_done((union ctl_io *)ctsio); 11870 break; 11871 } 11872 return (retval); 11873} 11874 11875const struct ctl_cmd_entry * 11876ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11877{ 11878 const struct ctl_cmd_entry *entry; 11879 int service_action; 11880 11881 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11882 if (sa) 11883 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11884 if (entry->flags & CTL_CMD_FLAG_SA5) { 11885 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11886 entry = &((const struct ctl_cmd_entry *) 11887 entry->execute)[service_action]; 11888 } 11889 return (entry); 11890} 11891 11892const struct ctl_cmd_entry * 11893ctl_validate_command(struct ctl_scsiio *ctsio) 11894{ 11895 const struct ctl_cmd_entry *entry; 11896 int i, sa; 11897 uint8_t diff; 11898 11899 entry = ctl_get_cmd_entry(ctsio, &sa); 11900 if (entry->execute == NULL) { 11901 if (sa) 11902 ctl_set_invalid_field(ctsio, 11903 /*sks_valid*/ 1, 11904 /*command*/ 1, 11905 /*field*/ 1, 11906 /*bit_valid*/ 1, 11907 /*bit*/ 4); 11908 else 11909 ctl_set_invalid_opcode(ctsio); 11910 ctl_done((union ctl_io *)ctsio); 11911 return (NULL); 11912 } 11913 KASSERT(entry->length > 0, 11914 ("Not defined length for command 0x%02x/0x%02x", 11915 ctsio->cdb[0], ctsio->cdb[1])); 11916 for (i = 1; i < entry->length; i++) { 11917 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11918 if (diff == 0) 11919 continue; 11920 ctl_set_invalid_field(ctsio, 11921 /*sks_valid*/ 1, 11922 /*command*/ 1, 11923 /*field*/ i, 11924 /*bit_valid*/ 1, 11925 /*bit*/ fls(diff) - 1); 11926 ctl_done((union ctl_io *)ctsio); 11927 return (NULL); 11928 } 11929 return (entry); 11930} 11931 11932static int 11933ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11934{ 11935 11936 switch (lun_type) { 11937 case T_PROCESSOR: 11938 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11939 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11940 return (0); 11941 break; 11942 case T_DIRECT: 11943 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11944 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11945 return (0); 11946 break; 11947 default: 11948 return (0); 11949 } 11950 return (1); 11951} 11952 11953static int 11954ctl_scsiio(struct ctl_scsiio *ctsio) 11955{ 11956 int retval; 11957 const struct ctl_cmd_entry *entry; 11958 11959 retval = CTL_RETVAL_COMPLETE; 11960 11961 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11962 11963 entry = ctl_get_cmd_entry(ctsio, NULL); 11964 11965 /* 11966 * If this I/O has been aborted, just send it straight to 11967 * ctl_done() without executing it. 11968 */ 11969 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11970 ctl_done((union ctl_io *)ctsio); 11971 goto bailout; 11972 } 11973 11974 /* 11975 * All the checks should have been handled by ctl_scsiio_precheck(). 11976 * We should be clear now to just execute the I/O. 11977 */ 11978 retval = entry->execute(ctsio); 11979 11980bailout: 11981 return (retval); 11982} 11983 11984/* 11985 * Since we only implement one target right now, a bus reset simply resets 11986 * our single target. 11987 */ 11988static int 11989ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11990{ 11991 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11992} 11993 11994static int 11995ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11996 ctl_ua_type ua_type) 11997{ 11998 struct ctl_lun *lun; 11999 int retval; 12000 12001 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12002 union ctl_ha_msg msg_info; 12003 12004 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12005 msg_info.hdr.nexus = io->io_hdr.nexus; 12006 if (ua_type==CTL_UA_TARG_RESET) 12007 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12008 else 12009 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12010 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12011 msg_info.hdr.original_sc = NULL; 12012 msg_info.hdr.serializing_sc = NULL; 12013 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12014 (void *)&msg_info, sizeof(msg_info), 0)) { 12015 } 12016 } 12017 retval = 0; 12018 12019 mtx_lock(&softc->ctl_lock); 12020 STAILQ_FOREACH(lun, &softc->lun_list, links) 12021 retval += ctl_lun_reset(lun, io, ua_type); 12022 mtx_unlock(&softc->ctl_lock); 12023 12024 return (retval); 12025} 12026 12027/* 12028 * The LUN should always be set. The I/O is optional, and is used to 12029 * distinguish between I/Os sent by this initiator, and by other 12030 * initiators. We set unit attention for initiators other than this one. 12031 * SAM-3 is vague on this point. It does say that a unit attention should 12032 * be established for other initiators when a LUN is reset (see section 12033 * 5.7.3), but it doesn't specifically say that the unit attention should 12034 * be established for this particular initiator when a LUN is reset. Here 12035 * is the relevant text, from SAM-3 rev 8: 12036 * 12037 * 5.7.2 When a SCSI initiator port aborts its own tasks 12038 * 12039 * When a SCSI initiator port causes its own task(s) to be aborted, no 12040 * notification that the task(s) have been aborted shall be returned to 12041 * the SCSI initiator port other than the completion response for the 12042 * command or task management function action that caused the task(s) to 12043 * be aborted and notification(s) associated with related effects of the 12044 * action (e.g., a reset unit attention condition). 12045 * 12046 * XXX KDM for now, we're setting unit attention for all initiators. 12047 */ 12048static int 12049ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12050{ 12051 union ctl_io *xio; 12052#if 0 12053 uint32_t initidx; 12054#endif 12055#ifdef CTL_WITH_CA 12056 int i; 12057#endif 12058 12059 mtx_lock(&lun->lun_lock); 12060 /* 12061 * Run through the OOA queue and abort each I/O. 12062 */ 12063 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12064 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12065 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12066 } 12067 12068 /* 12069 * This version sets unit attention for every 12070 */ 12071#if 0 12072 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12073 ctl_est_ua_all(lun, initidx, ua_type); 12074#else 12075 ctl_est_ua_all(lun, -1, ua_type); 12076#endif 12077 12078 /* 12079 * A reset (any kind, really) clears reservations established with 12080 * RESERVE/RELEASE. It does not clear reservations established 12081 * with PERSISTENT RESERVE OUT, but we don't support that at the 12082 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12083 * reservations made with the RESERVE/RELEASE commands, because 12084 * those commands are obsolete in SPC-3. 12085 */ 12086 lun->flags &= ~CTL_LUN_RESERVED; 12087 12088#ifdef CTL_WITH_CA 12089 for (i = 0; i < CTL_MAX_INITIATORS; i++) 12090 ctl_clear_mask(lun->have_ca, i); 12091#endif 12092 mtx_unlock(&lun->lun_lock); 12093 12094 return (0); 12095} 12096 12097static void 12098ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12099 int other_sc) 12100{ 12101 union ctl_io *xio; 12102 12103 mtx_assert(&lun->lun_lock, MA_OWNED); 12104 12105 /* 12106 * Run through the OOA queue and attempt to find the given I/O. 12107 * The target port, initiator ID, tag type and tag number have to 12108 * match the values that we got from the initiator. If we have an 12109 * untagged command to abort, simply abort the first untagged command 12110 * we come to. We only allow one untagged command at a time of course. 12111 */ 12112 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12113 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12114 12115 if ((targ_port == UINT32_MAX || 12116 targ_port == xio->io_hdr.nexus.targ_port) && 12117 (init_id == UINT32_MAX || 12118 init_id == xio->io_hdr.nexus.initid.id)) { 12119 if (targ_port != xio->io_hdr.nexus.targ_port || 12120 init_id != xio->io_hdr.nexus.initid.id) 12121 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12122 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12123 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12124 union ctl_ha_msg msg_info; 12125 12126 msg_info.hdr.nexus = xio->io_hdr.nexus; 12127 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12128 msg_info.task.tag_num = xio->scsiio.tag_num; 12129 msg_info.task.tag_type = xio->scsiio.tag_type; 12130 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12131 msg_info.hdr.original_sc = NULL; 12132 msg_info.hdr.serializing_sc = NULL; 12133 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12134 (void *)&msg_info, sizeof(msg_info), 0); 12135 } 12136 } 12137 } 12138} 12139 12140static int 12141ctl_abort_task_set(union ctl_io *io) 12142{ 12143 struct ctl_softc *softc = control_softc; 12144 struct ctl_lun *lun; 12145 uint32_t targ_lun; 12146 12147 /* 12148 * Look up the LUN. 12149 */ 12150 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12151 mtx_lock(&softc->ctl_lock); 12152 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12153 lun = softc->ctl_luns[targ_lun]; 12154 else { 12155 mtx_unlock(&softc->ctl_lock); 12156 return (1); 12157 } 12158 12159 mtx_lock(&lun->lun_lock); 12160 mtx_unlock(&softc->ctl_lock); 12161 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12162 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12163 io->io_hdr.nexus.initid.id, 12164 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12165 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12166 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12167 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12168 } 12169 mtx_unlock(&lun->lun_lock); 12170 return (0); 12171} 12172 12173static int 12174ctl_i_t_nexus_reset(union ctl_io *io) 12175{ 12176 struct ctl_softc *softc = control_softc; 12177 struct ctl_lun *lun; 12178 uint32_t initidx, residx; 12179 12180 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12181 residx = ctl_get_resindex(&io->io_hdr.nexus); 12182 mtx_lock(&softc->ctl_lock); 12183 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12184 mtx_lock(&lun->lun_lock); 12185 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12186 io->io_hdr.nexus.initid.id, 12187 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12188#ifdef CTL_WITH_CA 12189 ctl_clear_mask(lun->have_ca, initidx); 12190#endif 12191 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12192 lun->flags &= ~CTL_LUN_RESERVED; 12193 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12194 mtx_unlock(&lun->lun_lock); 12195 } 12196 mtx_unlock(&softc->ctl_lock); 12197 return (0); 12198} 12199 12200static int 12201ctl_abort_task(union ctl_io *io) 12202{ 12203 union ctl_io *xio; 12204 struct ctl_lun *lun; 12205 struct ctl_softc *softc; 12206#if 0 12207 struct sbuf sb; 12208 char printbuf[128]; 12209#endif 12210 int found; 12211 uint32_t targ_lun; 12212 12213 softc = control_softc; 12214 found = 0; 12215 12216 /* 12217 * Look up the LUN. 12218 */ 12219 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12220 mtx_lock(&softc->ctl_lock); 12221 if ((targ_lun < CTL_MAX_LUNS) 12222 && (softc->ctl_luns[targ_lun] != NULL)) 12223 lun = softc->ctl_luns[targ_lun]; 12224 else { 12225 mtx_unlock(&softc->ctl_lock); 12226 return (1); 12227 } 12228 12229#if 0 12230 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12231 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12232#endif 12233 12234 mtx_lock(&lun->lun_lock); 12235 mtx_unlock(&softc->ctl_lock); 12236 /* 12237 * Run through the OOA queue and attempt to find the given I/O. 12238 * The target port, initiator ID, tag type and tag number have to 12239 * match the values that we got from the initiator. If we have an 12240 * untagged command to abort, simply abort the first untagged command 12241 * we come to. We only allow one untagged command at a time of course. 12242 */ 12243 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12244 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12245#if 0 12246 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12247 12248 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12249 lun->lun, xio->scsiio.tag_num, 12250 xio->scsiio.tag_type, 12251 (xio->io_hdr.blocked_links.tqe_prev 12252 == NULL) ? "" : " BLOCKED", 12253 (xio->io_hdr.flags & 12254 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12255 (xio->io_hdr.flags & 12256 CTL_FLAG_ABORT) ? " ABORT" : "", 12257 (xio->io_hdr.flags & 12258 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12259 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12260 sbuf_finish(&sb); 12261 printf("%s\n", sbuf_data(&sb)); 12262#endif 12263 12264 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12265 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id) 12266 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12267 continue; 12268 12269 /* 12270 * If the abort says that the task is untagged, the 12271 * task in the queue must be untagged. Otherwise, 12272 * we just check to see whether the tag numbers 12273 * match. This is because the QLogic firmware 12274 * doesn't pass back the tag type in an abort 12275 * request. 12276 */ 12277#if 0 12278 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12279 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12280 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12281#endif 12282 /* 12283 * XXX KDM we've got problems with FC, because it 12284 * doesn't send down a tag type with aborts. So we 12285 * can only really go by the tag number... 12286 * This may cause problems with parallel SCSI. 12287 * Need to figure that out!! 12288 */ 12289 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12290 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12291 found = 1; 12292 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12293 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12294 union ctl_ha_msg msg_info; 12295 12296 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12297 msg_info.hdr.nexus = io->io_hdr.nexus; 12298 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12299 msg_info.task.tag_num = io->taskio.tag_num; 12300 msg_info.task.tag_type = io->taskio.tag_type; 12301 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12302 msg_info.hdr.original_sc = NULL; 12303 msg_info.hdr.serializing_sc = NULL; 12304#if 0 12305 printf("Sent Abort to other side\n"); 12306#endif 12307 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12308 (void *)&msg_info, sizeof(msg_info), 0) != 12309 CTL_HA_STATUS_SUCCESS) { 12310 } 12311 } 12312#if 0 12313 printf("ctl_abort_task: found I/O to abort\n"); 12314#endif 12315 } 12316 } 12317 mtx_unlock(&lun->lun_lock); 12318 12319 if (found == 0) { 12320 /* 12321 * This isn't really an error. It's entirely possible for 12322 * the abort and command completion to cross on the wire. 12323 * This is more of an informative/diagnostic error. 12324 */ 12325#if 0 12326 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12327 "%d:%d:%d:%d tag %d type %d\n", 12328 io->io_hdr.nexus.initid.id, 12329 io->io_hdr.nexus.targ_port, 12330 io->io_hdr.nexus.targ_target.id, 12331 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12332 io->taskio.tag_type); 12333#endif 12334 } 12335 return (0); 12336} 12337 12338static void 12339ctl_run_task(union ctl_io *io) 12340{ 12341 struct ctl_softc *softc = control_softc; 12342 int retval = 1; 12343 const char *task_desc; 12344 12345 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12346 12347 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12348 ("ctl_run_task: Unextected io_type %d\n", 12349 io->io_hdr.io_type)); 12350 12351 task_desc = ctl_scsi_task_string(&io->taskio); 12352 if (task_desc != NULL) { 12353#ifdef NEEDTOPORT 12354 csevent_log(CSC_CTL | CSC_SHELF_SW | 12355 CTL_TASK_REPORT, 12356 csevent_LogType_Trace, 12357 csevent_Severity_Information, 12358 csevent_AlertLevel_Green, 12359 csevent_FRU_Firmware, 12360 csevent_FRU_Unknown, 12361 "CTL: received task: %s",task_desc); 12362#endif 12363 } else { 12364#ifdef NEEDTOPORT 12365 csevent_log(CSC_CTL | CSC_SHELF_SW | 12366 CTL_TASK_REPORT, 12367 csevent_LogType_Trace, 12368 csevent_Severity_Information, 12369 csevent_AlertLevel_Green, 12370 csevent_FRU_Firmware, 12371 csevent_FRU_Unknown, 12372 "CTL: received unknown task " 12373 "type: %d (%#x)", 12374 io->taskio.task_action, 12375 io->taskio.task_action); 12376#endif 12377 } 12378 switch (io->taskio.task_action) { 12379 case CTL_TASK_ABORT_TASK: 12380 retval = ctl_abort_task(io); 12381 break; 12382 case CTL_TASK_ABORT_TASK_SET: 12383 case CTL_TASK_CLEAR_TASK_SET: 12384 retval = ctl_abort_task_set(io); 12385 break; 12386 case CTL_TASK_CLEAR_ACA: 12387 break; 12388 case CTL_TASK_I_T_NEXUS_RESET: 12389 retval = ctl_i_t_nexus_reset(io); 12390 break; 12391 case CTL_TASK_LUN_RESET: { 12392 struct ctl_lun *lun; 12393 uint32_t targ_lun; 12394 12395 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12396 mtx_lock(&softc->ctl_lock); 12397 if ((targ_lun < CTL_MAX_LUNS) 12398 && (softc->ctl_luns[targ_lun] != NULL)) 12399 lun = softc->ctl_luns[targ_lun]; 12400 else { 12401 mtx_unlock(&softc->ctl_lock); 12402 retval = 1; 12403 break; 12404 } 12405 12406 if (!(io->io_hdr.flags & 12407 CTL_FLAG_FROM_OTHER_SC)) { 12408 union ctl_ha_msg msg_info; 12409 12410 io->io_hdr.flags |= 12411 CTL_FLAG_SENT_2OTHER_SC; 12412 msg_info.hdr.msg_type = 12413 CTL_MSG_MANAGE_TASKS; 12414 msg_info.hdr.nexus = io->io_hdr.nexus; 12415 msg_info.task.task_action = 12416 CTL_TASK_LUN_RESET; 12417 msg_info.hdr.original_sc = NULL; 12418 msg_info.hdr.serializing_sc = NULL; 12419 if (CTL_HA_STATUS_SUCCESS != 12420 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12421 (void *)&msg_info, 12422 sizeof(msg_info), 0)) { 12423 } 12424 } 12425 12426 retval = ctl_lun_reset(lun, io, 12427 CTL_UA_LUN_RESET); 12428 mtx_unlock(&softc->ctl_lock); 12429 break; 12430 } 12431 case CTL_TASK_TARGET_RESET: 12432 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12433 break; 12434 case CTL_TASK_BUS_RESET: 12435 retval = ctl_bus_reset(softc, io); 12436 break; 12437 case CTL_TASK_PORT_LOGIN: 12438 break; 12439 case CTL_TASK_PORT_LOGOUT: 12440 break; 12441 default: 12442 printf("ctl_run_task: got unknown task management event %d\n", 12443 io->taskio.task_action); 12444 break; 12445 } 12446 if (retval == 0) 12447 io->io_hdr.status = CTL_SUCCESS; 12448 else 12449 io->io_hdr.status = CTL_ERROR; 12450 ctl_done(io); 12451} 12452 12453/* 12454 * For HA operation. Handle commands that come in from the other 12455 * controller. 12456 */ 12457static void 12458ctl_handle_isc(union ctl_io *io) 12459{ 12460 int free_io; 12461 struct ctl_lun *lun; 12462 struct ctl_softc *softc; 12463 uint32_t targ_lun; 12464 12465 softc = control_softc; 12466 12467 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12468 lun = softc->ctl_luns[targ_lun]; 12469 12470 switch (io->io_hdr.msg_type) { 12471 case CTL_MSG_SERIALIZE: 12472 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12473 break; 12474 case CTL_MSG_R2R: { 12475 const struct ctl_cmd_entry *entry; 12476 12477 /* 12478 * This is only used in SER_ONLY mode. 12479 */ 12480 free_io = 0; 12481 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12482 mtx_lock(&lun->lun_lock); 12483 if (ctl_scsiio_lun_check(lun, 12484 entry, (struct ctl_scsiio *)io) != 0) { 12485 mtx_unlock(&lun->lun_lock); 12486 ctl_done(io); 12487 break; 12488 } 12489 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12490 mtx_unlock(&lun->lun_lock); 12491 ctl_enqueue_rtr(io); 12492 break; 12493 } 12494 case CTL_MSG_FINISH_IO: 12495 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12496 free_io = 0; 12497 ctl_done(io); 12498 } else { 12499 free_io = 1; 12500 mtx_lock(&lun->lun_lock); 12501 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12502 ooa_links); 12503 ctl_check_blocked(lun); 12504 mtx_unlock(&lun->lun_lock); 12505 } 12506 break; 12507 case CTL_MSG_PERS_ACTION: 12508 ctl_hndl_per_res_out_on_other_sc( 12509 (union ctl_ha_msg *)&io->presio.pr_msg); 12510 free_io = 1; 12511 break; 12512 case CTL_MSG_BAD_JUJU: 12513 free_io = 0; 12514 ctl_done(io); 12515 break; 12516 case CTL_MSG_DATAMOVE: 12517 /* Only used in XFER mode */ 12518 free_io = 0; 12519 ctl_datamove_remote(io); 12520 break; 12521 case CTL_MSG_DATAMOVE_DONE: 12522 /* Only used in XFER mode */ 12523 free_io = 0; 12524 io->scsiio.be_move_done(io); 12525 break; 12526 default: 12527 free_io = 1; 12528 printf("%s: Invalid message type %d\n", 12529 __func__, io->io_hdr.msg_type); 12530 break; 12531 } 12532 if (free_io) 12533 ctl_free_io(io); 12534 12535} 12536 12537 12538/* 12539 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12540 * there is no match. 12541 */ 12542static ctl_lun_error_pattern 12543ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12544{ 12545 const struct ctl_cmd_entry *entry; 12546 ctl_lun_error_pattern filtered_pattern, pattern; 12547 12548 pattern = desc->error_pattern; 12549 12550 /* 12551 * XXX KDM we need more data passed into this function to match a 12552 * custom pattern, and we actually need to implement custom pattern 12553 * matching. 12554 */ 12555 if (pattern & CTL_LUN_PAT_CMD) 12556 return (CTL_LUN_PAT_CMD); 12557 12558 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12559 return (CTL_LUN_PAT_ANY); 12560 12561 entry = ctl_get_cmd_entry(ctsio, NULL); 12562 12563 filtered_pattern = entry->pattern & pattern; 12564 12565 /* 12566 * If the user requested specific flags in the pattern (e.g. 12567 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12568 * flags. 12569 * 12570 * If the user did not specify any flags, it doesn't matter whether 12571 * or not the command supports the flags. 12572 */ 12573 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12574 (pattern & ~CTL_LUN_PAT_MASK)) 12575 return (CTL_LUN_PAT_NONE); 12576 12577 /* 12578 * If the user asked for a range check, see if the requested LBA 12579 * range overlaps with this command's LBA range. 12580 */ 12581 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12582 uint64_t lba1; 12583 uint64_t len1; 12584 ctl_action action; 12585 int retval; 12586 12587 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12588 if (retval != 0) 12589 return (CTL_LUN_PAT_NONE); 12590 12591 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12592 desc->lba_range.len, FALSE); 12593 /* 12594 * A "pass" means that the LBA ranges don't overlap, so 12595 * this doesn't match the user's range criteria. 12596 */ 12597 if (action == CTL_ACTION_PASS) 12598 return (CTL_LUN_PAT_NONE); 12599 } 12600 12601 return (filtered_pattern); 12602} 12603 12604static void 12605ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12606{ 12607 struct ctl_error_desc *desc, *desc2; 12608 12609 mtx_assert(&lun->lun_lock, MA_OWNED); 12610 12611 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12612 ctl_lun_error_pattern pattern; 12613 /* 12614 * Check to see whether this particular command matches 12615 * the pattern in the descriptor. 12616 */ 12617 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12618 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12619 continue; 12620 12621 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12622 case CTL_LUN_INJ_ABORTED: 12623 ctl_set_aborted(&io->scsiio); 12624 break; 12625 case CTL_LUN_INJ_MEDIUM_ERR: 12626 ctl_set_medium_error(&io->scsiio); 12627 break; 12628 case CTL_LUN_INJ_UA: 12629 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12630 * OCCURRED */ 12631 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12632 break; 12633 case CTL_LUN_INJ_CUSTOM: 12634 /* 12635 * We're assuming the user knows what he is doing. 12636 * Just copy the sense information without doing 12637 * checks. 12638 */ 12639 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12640 MIN(sizeof(desc->custom_sense), 12641 sizeof(io->scsiio.sense_data))); 12642 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12643 io->scsiio.sense_len = SSD_FULL_SIZE; 12644 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12645 break; 12646 case CTL_LUN_INJ_NONE: 12647 default: 12648 /* 12649 * If this is an error injection type we don't know 12650 * about, clear the continuous flag (if it is set) 12651 * so it will get deleted below. 12652 */ 12653 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12654 break; 12655 } 12656 /* 12657 * By default, each error injection action is a one-shot 12658 */ 12659 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12660 continue; 12661 12662 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12663 12664 free(desc, M_CTL); 12665 } 12666} 12667 12668#ifdef CTL_IO_DELAY 12669static void 12670ctl_datamove_timer_wakeup(void *arg) 12671{ 12672 union ctl_io *io; 12673 12674 io = (union ctl_io *)arg; 12675 12676 ctl_datamove(io); 12677} 12678#endif /* CTL_IO_DELAY */ 12679 12680void 12681ctl_datamove(union ctl_io *io) 12682{ 12683 void (*fe_datamove)(union ctl_io *io); 12684 12685 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12686 12687 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12688 12689#ifdef CTL_TIME_IO 12690 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12691 char str[256]; 12692 char path_str[64]; 12693 struct sbuf sb; 12694 12695 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12696 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12697 12698 sbuf_cat(&sb, path_str); 12699 switch (io->io_hdr.io_type) { 12700 case CTL_IO_SCSI: 12701 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12702 sbuf_printf(&sb, "\n"); 12703 sbuf_cat(&sb, path_str); 12704 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12705 io->scsiio.tag_num, io->scsiio.tag_type); 12706 break; 12707 case CTL_IO_TASK: 12708 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12709 "Tag Type: %d\n", io->taskio.task_action, 12710 io->taskio.tag_num, io->taskio.tag_type); 12711 break; 12712 default: 12713 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12714 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12715 break; 12716 } 12717 sbuf_cat(&sb, path_str); 12718 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12719 (intmax_t)time_uptime - io->io_hdr.start_time); 12720 sbuf_finish(&sb); 12721 printf("%s", sbuf_data(&sb)); 12722 } 12723#endif /* CTL_TIME_IO */ 12724 12725#ifdef CTL_IO_DELAY 12726 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12727 struct ctl_lun *lun; 12728 12729 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12730 12731 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12732 } else { 12733 struct ctl_lun *lun; 12734 12735 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12736 if ((lun != NULL) 12737 && (lun->delay_info.datamove_delay > 0)) { 12738 struct callout *callout; 12739 12740 callout = (struct callout *)&io->io_hdr.timer_bytes; 12741 callout_init(callout, /*mpsafe*/ 1); 12742 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12743 callout_reset(callout, 12744 lun->delay_info.datamove_delay * hz, 12745 ctl_datamove_timer_wakeup, io); 12746 if (lun->delay_info.datamove_type == 12747 CTL_DELAY_TYPE_ONESHOT) 12748 lun->delay_info.datamove_delay = 0; 12749 return; 12750 } 12751 } 12752#endif 12753 12754 /* 12755 * This command has been aborted. Set the port status, so we fail 12756 * the data move. 12757 */ 12758 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12759 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12760 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12761 io->io_hdr.nexus.targ_port, 12762 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12763 io->io_hdr.nexus.targ_lun); 12764 io->io_hdr.port_status = 31337; 12765 /* 12766 * Note that the backend, in this case, will get the 12767 * callback in its context. In other cases it may get 12768 * called in the frontend's interrupt thread context. 12769 */ 12770 io->scsiio.be_move_done(io); 12771 return; 12772 } 12773 12774 /* Don't confuse frontend with zero length data move. */ 12775 if (io->scsiio.kern_data_len == 0) { 12776 io->scsiio.be_move_done(io); 12777 return; 12778 } 12779 12780 /* 12781 * If we're in XFER mode and this I/O is from the other shelf 12782 * controller, we need to send the DMA to the other side to 12783 * actually transfer the data to/from the host. In serialize only 12784 * mode the transfer happens below CTL and ctl_datamove() is only 12785 * called on the machine that originally received the I/O. 12786 */ 12787 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12788 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12789 union ctl_ha_msg msg; 12790 uint32_t sg_entries_sent; 12791 int do_sg_copy; 12792 int i; 12793 12794 memset(&msg, 0, sizeof(msg)); 12795 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12796 msg.hdr.original_sc = io->io_hdr.original_sc; 12797 msg.hdr.serializing_sc = io; 12798 msg.hdr.nexus = io->io_hdr.nexus; 12799 msg.dt.flags = io->io_hdr.flags; 12800 /* 12801 * We convert everything into a S/G list here. We can't 12802 * pass by reference, only by value between controllers. 12803 * So we can't pass a pointer to the S/G list, only as many 12804 * S/G entries as we can fit in here. If it's possible for 12805 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12806 * then we need to break this up into multiple transfers. 12807 */ 12808 if (io->scsiio.kern_sg_entries == 0) { 12809 msg.dt.kern_sg_entries = 1; 12810 /* 12811 * If this is in cached memory, flush the cache 12812 * before we send the DMA request to the other 12813 * controller. We want to do this in either the 12814 * read or the write case. The read case is 12815 * straightforward. In the write case, we want to 12816 * make sure nothing is in the local cache that 12817 * could overwrite the DMAed data. 12818 */ 12819 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12820 /* 12821 * XXX KDM use bus_dmamap_sync() here. 12822 */ 12823 } 12824 12825 /* 12826 * Convert to a physical address if this is a 12827 * virtual address. 12828 */ 12829 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12830 msg.dt.sg_list[0].addr = 12831 io->scsiio.kern_data_ptr; 12832 } else { 12833 /* 12834 * XXX KDM use busdma here! 12835 */ 12836#if 0 12837 msg.dt.sg_list[0].addr = (void *) 12838 vtophys(io->scsiio.kern_data_ptr); 12839#endif 12840 } 12841 12842 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12843 do_sg_copy = 0; 12844 } else { 12845 struct ctl_sg_entry *sgl; 12846 12847 do_sg_copy = 1; 12848 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12849 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12850 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12851 /* 12852 * XXX KDM use bus_dmamap_sync() here. 12853 */ 12854 } 12855 } 12856 12857 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12858 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12859 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12860 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12861 msg.dt.sg_sequence = 0; 12862 12863 /* 12864 * Loop until we've sent all of the S/G entries. On the 12865 * other end, we'll recompose these S/G entries into one 12866 * contiguous list before passing it to the 12867 */ 12868 for (sg_entries_sent = 0; sg_entries_sent < 12869 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12870 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12871 sizeof(msg.dt.sg_list[0])), 12872 msg.dt.kern_sg_entries - sg_entries_sent); 12873 12874 if (do_sg_copy != 0) { 12875 struct ctl_sg_entry *sgl; 12876 int j; 12877 12878 sgl = (struct ctl_sg_entry *) 12879 io->scsiio.kern_data_ptr; 12880 /* 12881 * If this is in cached memory, flush the cache 12882 * before we send the DMA request to the other 12883 * controller. We want to do this in either 12884 * the * read or the write case. The read 12885 * case is straightforward. In the write 12886 * case, we want to make sure nothing is 12887 * in the local cache that could overwrite 12888 * the DMAed data. 12889 */ 12890 12891 for (i = sg_entries_sent, j = 0; 12892 i < msg.dt.cur_sg_entries; i++, j++) { 12893 if ((io->io_hdr.flags & 12894 CTL_FLAG_NO_DATASYNC) == 0) { 12895 /* 12896 * XXX KDM use bus_dmamap_sync() 12897 */ 12898 } 12899 if ((io->io_hdr.flags & 12900 CTL_FLAG_BUS_ADDR) == 0) { 12901 /* 12902 * XXX KDM use busdma. 12903 */ 12904#if 0 12905 msg.dt.sg_list[j].addr =(void *) 12906 vtophys(sgl[i].addr); 12907#endif 12908 } else { 12909 msg.dt.sg_list[j].addr = 12910 sgl[i].addr; 12911 } 12912 msg.dt.sg_list[j].len = sgl[i].len; 12913 } 12914 } 12915 12916 sg_entries_sent += msg.dt.cur_sg_entries; 12917 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12918 msg.dt.sg_last = 1; 12919 else 12920 msg.dt.sg_last = 0; 12921 12922 /* 12923 * XXX KDM drop and reacquire the lock here? 12924 */ 12925 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12926 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12927 /* 12928 * XXX do something here. 12929 */ 12930 } 12931 12932 msg.dt.sent_sg_entries = sg_entries_sent; 12933 } 12934 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12935 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12936 ctl_failover_io(io, /*have_lock*/ 0); 12937 12938 } else { 12939 12940 /* 12941 * Lookup the fe_datamove() function for this particular 12942 * front end. 12943 */ 12944 fe_datamove = 12945 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12946 12947 fe_datamove(io); 12948 } 12949} 12950 12951static void 12952ctl_send_datamove_done(union ctl_io *io, int have_lock) 12953{ 12954 union ctl_ha_msg msg; 12955 int isc_status; 12956 12957 memset(&msg, 0, sizeof(msg)); 12958 12959 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12960 msg.hdr.original_sc = io; 12961 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12962 msg.hdr.nexus = io->io_hdr.nexus; 12963 msg.hdr.status = io->io_hdr.status; 12964 msg.scsi.tag_num = io->scsiio.tag_num; 12965 msg.scsi.tag_type = io->scsiio.tag_type; 12966 msg.scsi.scsi_status = io->scsiio.scsi_status; 12967 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12968 sizeof(io->scsiio.sense_data)); 12969 msg.scsi.sense_len = io->scsiio.sense_len; 12970 msg.scsi.sense_residual = io->scsiio.sense_residual; 12971 msg.scsi.fetd_status = io->io_hdr.port_status; 12972 msg.scsi.residual = io->scsiio.residual; 12973 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12974 12975 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12976 ctl_failover_io(io, /*have_lock*/ have_lock); 12977 return; 12978 } 12979 12980 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12981 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12982 /* XXX do something if this fails */ 12983 } 12984 12985} 12986 12987/* 12988 * The DMA to the remote side is done, now we need to tell the other side 12989 * we're done so it can continue with its data movement. 12990 */ 12991static void 12992ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12993{ 12994 union ctl_io *io; 12995 12996 io = rq->context; 12997 12998 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12999 printf("%s: ISC DMA write failed with error %d", __func__, 13000 rq->ret); 13001 ctl_set_internal_failure(&io->scsiio, 13002 /*sks_valid*/ 1, 13003 /*retry_count*/ rq->ret); 13004 } 13005 13006 ctl_dt_req_free(rq); 13007 13008 /* 13009 * In this case, we had to malloc the memory locally. Free it. 13010 */ 13011 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13012 int i; 13013 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13014 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13015 } 13016 /* 13017 * The data is in local and remote memory, so now we need to send 13018 * status (good or back) back to the other side. 13019 */ 13020 ctl_send_datamove_done(io, /*have_lock*/ 0); 13021} 13022 13023/* 13024 * We've moved the data from the host/controller into local memory. Now we 13025 * need to push it over to the remote controller's memory. 13026 */ 13027static int 13028ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13029{ 13030 int retval; 13031 13032 retval = 0; 13033 13034 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13035 ctl_datamove_remote_write_cb); 13036 13037 return (retval); 13038} 13039 13040static void 13041ctl_datamove_remote_write(union ctl_io *io) 13042{ 13043 int retval; 13044 void (*fe_datamove)(union ctl_io *io); 13045 13046 /* 13047 * - Get the data from the host/HBA into local memory. 13048 * - DMA memory from the local controller to the remote controller. 13049 * - Send status back to the remote controller. 13050 */ 13051 13052 retval = ctl_datamove_remote_sgl_setup(io); 13053 if (retval != 0) 13054 return; 13055 13056 /* Switch the pointer over so the FETD knows what to do */ 13057 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13058 13059 /* 13060 * Use a custom move done callback, since we need to send completion 13061 * back to the other controller, not to the backend on this side. 13062 */ 13063 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13064 13065 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13066 13067 fe_datamove(io); 13068 13069 return; 13070 13071} 13072 13073static int 13074ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13075{ 13076#if 0 13077 char str[256]; 13078 char path_str[64]; 13079 struct sbuf sb; 13080#endif 13081 13082 /* 13083 * In this case, we had to malloc the memory locally. Free it. 13084 */ 13085 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13086 int i; 13087 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13088 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13089 } 13090 13091#if 0 13092 scsi_path_string(io, path_str, sizeof(path_str)); 13093 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13094 sbuf_cat(&sb, path_str); 13095 scsi_command_string(&io->scsiio, NULL, &sb); 13096 sbuf_printf(&sb, "\n"); 13097 sbuf_cat(&sb, path_str); 13098 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13099 io->scsiio.tag_num, io->scsiio.tag_type); 13100 sbuf_cat(&sb, path_str); 13101 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13102 io->io_hdr.flags, io->io_hdr.status); 13103 sbuf_finish(&sb); 13104 printk("%s", sbuf_data(&sb)); 13105#endif 13106 13107 13108 /* 13109 * The read is done, now we need to send status (good or bad) back 13110 * to the other side. 13111 */ 13112 ctl_send_datamove_done(io, /*have_lock*/ 0); 13113 13114 return (0); 13115} 13116 13117static void 13118ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13119{ 13120 union ctl_io *io; 13121 void (*fe_datamove)(union ctl_io *io); 13122 13123 io = rq->context; 13124 13125 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13126 printf("%s: ISC DMA read failed with error %d", __func__, 13127 rq->ret); 13128 ctl_set_internal_failure(&io->scsiio, 13129 /*sks_valid*/ 1, 13130 /*retry_count*/ rq->ret); 13131 } 13132 13133 ctl_dt_req_free(rq); 13134 13135 /* Switch the pointer over so the FETD knows what to do */ 13136 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13137 13138 /* 13139 * Use a custom move done callback, since we need to send completion 13140 * back to the other controller, not to the backend on this side. 13141 */ 13142 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13143 13144 /* XXX KDM add checks like the ones in ctl_datamove? */ 13145 13146 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13147 13148 fe_datamove(io); 13149} 13150 13151static int 13152ctl_datamove_remote_sgl_setup(union ctl_io *io) 13153{ 13154 struct ctl_sg_entry *local_sglist, *remote_sglist; 13155 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13156 struct ctl_softc *softc; 13157 int retval; 13158 int i; 13159 13160 retval = 0; 13161 softc = control_softc; 13162 13163 local_sglist = io->io_hdr.local_sglist; 13164 local_dma_sglist = io->io_hdr.local_dma_sglist; 13165 remote_sglist = io->io_hdr.remote_sglist; 13166 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13167 13168 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13169 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13170 local_sglist[i].len = remote_sglist[i].len; 13171 13172 /* 13173 * XXX Detect the situation where the RS-level I/O 13174 * redirector on the other side has already read the 13175 * data off of the AOR RS on this side, and 13176 * transferred it to remote (mirror) memory on the 13177 * other side. Since we already have the data in 13178 * memory here, we just need to use it. 13179 * 13180 * XXX KDM this can probably be removed once we 13181 * get the cache device code in and take the 13182 * current AOR implementation out. 13183 */ 13184#ifdef NEEDTOPORT 13185 if ((remote_sglist[i].addr >= 13186 (void *)vtophys(softc->mirr->addr)) 13187 && (remote_sglist[i].addr < 13188 ((void *)vtophys(softc->mirr->addr) + 13189 CacheMirrorOffset))) { 13190 local_sglist[i].addr = remote_sglist[i].addr - 13191 CacheMirrorOffset; 13192 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13193 CTL_FLAG_DATA_IN) 13194 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13195 } else { 13196 local_sglist[i].addr = remote_sglist[i].addr + 13197 CacheMirrorOffset; 13198 } 13199#endif 13200#if 0 13201 printf("%s: local %p, remote %p, len %d\n", 13202 __func__, local_sglist[i].addr, 13203 remote_sglist[i].addr, local_sglist[i].len); 13204#endif 13205 } 13206 } else { 13207 uint32_t len_to_go; 13208 13209 /* 13210 * In this case, we don't have automatically allocated 13211 * memory for this I/O on this controller. This typically 13212 * happens with internal CTL I/O -- e.g. inquiry, mode 13213 * sense, etc. Anything coming from RAIDCore will have 13214 * a mirror area available. 13215 */ 13216 len_to_go = io->scsiio.kern_data_len; 13217 13218 /* 13219 * Clear the no datasync flag, we have to use malloced 13220 * buffers. 13221 */ 13222 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13223 13224 /* 13225 * The difficult thing here is that the size of the various 13226 * S/G segments may be different than the size from the 13227 * remote controller. That'll make it harder when DMAing 13228 * the data back to the other side. 13229 */ 13230 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13231 sizeof(io->io_hdr.remote_sglist[0])) && 13232 (len_to_go > 0); i++) { 13233 local_sglist[i].len = MIN(len_to_go, 131072); 13234 CTL_SIZE_8B(local_dma_sglist[i].len, 13235 local_sglist[i].len); 13236 local_sglist[i].addr = 13237 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13238 13239 local_dma_sglist[i].addr = local_sglist[i].addr; 13240 13241 if (local_sglist[i].addr == NULL) { 13242 int j; 13243 13244 printf("malloc failed for %zd bytes!", 13245 local_dma_sglist[i].len); 13246 for (j = 0; j < i; j++) { 13247 free(local_sglist[j].addr, M_CTL); 13248 } 13249 ctl_set_internal_failure(&io->scsiio, 13250 /*sks_valid*/ 1, 13251 /*retry_count*/ 4857); 13252 retval = 1; 13253 goto bailout_error; 13254 13255 } 13256 /* XXX KDM do we need a sync here? */ 13257 13258 len_to_go -= local_sglist[i].len; 13259 } 13260 /* 13261 * Reset the number of S/G entries accordingly. The 13262 * original number of S/G entries is available in 13263 * rem_sg_entries. 13264 */ 13265 io->scsiio.kern_sg_entries = i; 13266 13267#if 0 13268 printf("%s: kern_sg_entries = %d\n", __func__, 13269 io->scsiio.kern_sg_entries); 13270 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13271 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13272 local_sglist[i].addr, local_sglist[i].len, 13273 local_dma_sglist[i].len); 13274#endif 13275 } 13276 13277 13278 return (retval); 13279 13280bailout_error: 13281 13282 ctl_send_datamove_done(io, /*have_lock*/ 0); 13283 13284 return (retval); 13285} 13286 13287static int 13288ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13289 ctl_ha_dt_cb callback) 13290{ 13291 struct ctl_ha_dt_req *rq; 13292 struct ctl_sg_entry *remote_sglist, *local_sglist; 13293 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13294 uint32_t local_used, remote_used, total_used; 13295 int retval; 13296 int i, j; 13297 13298 retval = 0; 13299 13300 rq = ctl_dt_req_alloc(); 13301 13302 /* 13303 * If we failed to allocate the request, and if the DMA didn't fail 13304 * anyway, set busy status. This is just a resource allocation 13305 * failure. 13306 */ 13307 if ((rq == NULL) 13308 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13309 ctl_set_busy(&io->scsiio); 13310 13311 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13312 13313 if (rq != NULL) 13314 ctl_dt_req_free(rq); 13315 13316 /* 13317 * The data move failed. We need to return status back 13318 * to the other controller. No point in trying to DMA 13319 * data to the remote controller. 13320 */ 13321 13322 ctl_send_datamove_done(io, /*have_lock*/ 0); 13323 13324 retval = 1; 13325 13326 goto bailout; 13327 } 13328 13329 local_sglist = io->io_hdr.local_sglist; 13330 local_dma_sglist = io->io_hdr.local_dma_sglist; 13331 remote_sglist = io->io_hdr.remote_sglist; 13332 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13333 local_used = 0; 13334 remote_used = 0; 13335 total_used = 0; 13336 13337 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13338 rq->ret = CTL_HA_STATUS_SUCCESS; 13339 rq->context = io; 13340 callback(rq); 13341 goto bailout; 13342 } 13343 13344 /* 13345 * Pull/push the data over the wire from/to the other controller. 13346 * This takes into account the possibility that the local and 13347 * remote sglists may not be identical in terms of the size of 13348 * the elements and the number of elements. 13349 * 13350 * One fundamental assumption here is that the length allocated for 13351 * both the local and remote sglists is identical. Otherwise, we've 13352 * essentially got a coding error of some sort. 13353 */ 13354 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13355 int isc_ret; 13356 uint32_t cur_len, dma_length; 13357 uint8_t *tmp_ptr; 13358 13359 rq->id = CTL_HA_DATA_CTL; 13360 rq->command = command; 13361 rq->context = io; 13362 13363 /* 13364 * Both pointers should be aligned. But it is possible 13365 * that the allocation length is not. They should both 13366 * also have enough slack left over at the end, though, 13367 * to round up to the next 8 byte boundary. 13368 */ 13369 cur_len = MIN(local_sglist[i].len - local_used, 13370 remote_sglist[j].len - remote_used); 13371 13372 /* 13373 * In this case, we have a size issue and need to decrease 13374 * the size, except in the case where we actually have less 13375 * than 8 bytes left. In that case, we need to increase 13376 * the DMA length to get the last bit. 13377 */ 13378 if ((cur_len & 0x7) != 0) { 13379 if (cur_len > 0x7) { 13380 cur_len = cur_len - (cur_len & 0x7); 13381 dma_length = cur_len; 13382 } else { 13383 CTL_SIZE_8B(dma_length, cur_len); 13384 } 13385 13386 } else 13387 dma_length = cur_len; 13388 13389 /* 13390 * If we had to allocate memory for this I/O, instead of using 13391 * the non-cached mirror memory, we'll need to flush the cache 13392 * before trying to DMA to the other controller. 13393 * 13394 * We could end up doing this multiple times for the same 13395 * segment if we have a larger local segment than remote 13396 * segment. That shouldn't be an issue. 13397 */ 13398 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13399 /* 13400 * XXX KDM use bus_dmamap_sync() here. 13401 */ 13402 } 13403 13404 rq->size = dma_length; 13405 13406 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13407 tmp_ptr += local_used; 13408 13409 /* Use physical addresses when talking to ISC hardware */ 13410 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13411 /* XXX KDM use busdma */ 13412#if 0 13413 rq->local = vtophys(tmp_ptr); 13414#endif 13415 } else 13416 rq->local = tmp_ptr; 13417 13418 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13419 tmp_ptr += remote_used; 13420 rq->remote = tmp_ptr; 13421 13422 rq->callback = NULL; 13423 13424 local_used += cur_len; 13425 if (local_used >= local_sglist[i].len) { 13426 i++; 13427 local_used = 0; 13428 } 13429 13430 remote_used += cur_len; 13431 if (remote_used >= remote_sglist[j].len) { 13432 j++; 13433 remote_used = 0; 13434 } 13435 total_used += cur_len; 13436 13437 if (total_used >= io->scsiio.kern_data_len) 13438 rq->callback = callback; 13439 13440 if ((rq->size & 0x7) != 0) { 13441 printf("%s: warning: size %d is not on 8b boundary\n", 13442 __func__, rq->size); 13443 } 13444 if (((uintptr_t)rq->local & 0x7) != 0) { 13445 printf("%s: warning: local %p not on 8b boundary\n", 13446 __func__, rq->local); 13447 } 13448 if (((uintptr_t)rq->remote & 0x7) != 0) { 13449 printf("%s: warning: remote %p not on 8b boundary\n", 13450 __func__, rq->local); 13451 } 13452#if 0 13453 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13454 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13455 rq->local, rq->remote, rq->size); 13456#endif 13457 13458 isc_ret = ctl_dt_single(rq); 13459 if (isc_ret == CTL_HA_STATUS_WAIT) 13460 continue; 13461 13462 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13463 rq->ret = CTL_HA_STATUS_SUCCESS; 13464 } else { 13465 rq->ret = isc_ret; 13466 } 13467 callback(rq); 13468 goto bailout; 13469 } 13470 13471bailout: 13472 return (retval); 13473 13474} 13475 13476static void 13477ctl_datamove_remote_read(union ctl_io *io) 13478{ 13479 int retval; 13480 int i; 13481 13482 /* 13483 * This will send an error to the other controller in the case of a 13484 * failure. 13485 */ 13486 retval = ctl_datamove_remote_sgl_setup(io); 13487 if (retval != 0) 13488 return; 13489 13490 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13491 ctl_datamove_remote_read_cb); 13492 if ((retval != 0) 13493 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13494 /* 13495 * Make sure we free memory if there was an error.. The 13496 * ctl_datamove_remote_xfer() function will send the 13497 * datamove done message, or call the callback with an 13498 * error if there is a problem. 13499 */ 13500 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13501 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13502 } 13503 13504 return; 13505} 13506 13507/* 13508 * Process a datamove request from the other controller. This is used for 13509 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13510 * first. Once that is complete, the data gets DMAed into the remote 13511 * controller's memory. For reads, we DMA from the remote controller's 13512 * memory into our memory first, and then move it out to the FETD. 13513 */ 13514static void 13515ctl_datamove_remote(union ctl_io *io) 13516{ 13517 struct ctl_softc *softc; 13518 13519 softc = control_softc; 13520 13521 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13522 13523 /* 13524 * Note that we look for an aborted I/O here, but don't do some of 13525 * the other checks that ctl_datamove() normally does. 13526 * We don't need to run the datamove delay code, since that should 13527 * have been done if need be on the other controller. 13528 */ 13529 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13530 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13531 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13532 io->io_hdr.nexus.targ_port, 13533 io->io_hdr.nexus.targ_target.id, 13534 io->io_hdr.nexus.targ_lun); 13535 io->io_hdr.port_status = 31338; 13536 ctl_send_datamove_done(io, /*have_lock*/ 0); 13537 return; 13538 } 13539 13540 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13541 ctl_datamove_remote_write(io); 13542 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13543 ctl_datamove_remote_read(io); 13544 } else { 13545 union ctl_ha_msg msg; 13546 struct scsi_sense_data *sense; 13547 uint8_t sks[3]; 13548 int retry_count; 13549 13550 memset(&msg, 0, sizeof(msg)); 13551 13552 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13553 msg.hdr.status = CTL_SCSI_ERROR; 13554 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13555 13556 retry_count = 4243; 13557 13558 sense = &msg.scsi.sense_data; 13559 sks[0] = SSD_SCS_VALID; 13560 sks[1] = (retry_count >> 8) & 0xff; 13561 sks[2] = retry_count & 0xff; 13562 13563 /* "Internal target failure" */ 13564 scsi_set_sense_data(sense, 13565 /*sense_format*/ SSD_TYPE_NONE, 13566 /*current_error*/ 1, 13567 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13568 /*asc*/ 0x44, 13569 /*ascq*/ 0x00, 13570 /*type*/ SSD_ELEM_SKS, 13571 /*size*/ sizeof(sks), 13572 /*data*/ sks, 13573 SSD_ELEM_NONE); 13574 13575 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13576 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13577 ctl_failover_io(io, /*have_lock*/ 1); 13578 return; 13579 } 13580 13581 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13582 CTL_HA_STATUS_SUCCESS) { 13583 /* XXX KDM what to do if this fails? */ 13584 } 13585 return; 13586 } 13587 13588} 13589 13590static int 13591ctl_process_done(union ctl_io *io) 13592{ 13593 struct ctl_lun *lun; 13594 struct ctl_softc *softc = control_softc; 13595 void (*fe_done)(union ctl_io *io); 13596 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13597 13598 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13599 13600 fe_done = softc->ctl_ports[targ_port]->fe_done; 13601 13602#ifdef CTL_TIME_IO 13603 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13604 char str[256]; 13605 char path_str[64]; 13606 struct sbuf sb; 13607 13608 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13609 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13610 13611 sbuf_cat(&sb, path_str); 13612 switch (io->io_hdr.io_type) { 13613 case CTL_IO_SCSI: 13614 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13615 sbuf_printf(&sb, "\n"); 13616 sbuf_cat(&sb, path_str); 13617 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13618 io->scsiio.tag_num, io->scsiio.tag_type); 13619 break; 13620 case CTL_IO_TASK: 13621 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13622 "Tag Type: %d\n", io->taskio.task_action, 13623 io->taskio.tag_num, io->taskio.tag_type); 13624 break; 13625 default: 13626 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13627 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13628 break; 13629 } 13630 sbuf_cat(&sb, path_str); 13631 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13632 (intmax_t)time_uptime - io->io_hdr.start_time); 13633 sbuf_finish(&sb); 13634 printf("%s", sbuf_data(&sb)); 13635 } 13636#endif /* CTL_TIME_IO */ 13637 13638 switch (io->io_hdr.io_type) { 13639 case CTL_IO_SCSI: 13640 break; 13641 case CTL_IO_TASK: 13642 if (ctl_debug & CTL_DEBUG_INFO) 13643 ctl_io_error_print(io, NULL); 13644 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13645 ctl_free_io(io); 13646 else 13647 fe_done(io); 13648 return (CTL_RETVAL_COMPLETE); 13649 default: 13650 panic("ctl_process_done: invalid io type %d\n", 13651 io->io_hdr.io_type); 13652 break; /* NOTREACHED */ 13653 } 13654 13655 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13656 if (lun == NULL) { 13657 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13658 io->io_hdr.nexus.targ_mapped_lun)); 13659 goto bailout; 13660 } 13661 13662 mtx_lock(&lun->lun_lock); 13663 13664 /* 13665 * Check to see if we have any errors to inject here. We only 13666 * inject errors for commands that don't already have errors set. 13667 */ 13668 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13669 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13670 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13671 ctl_inject_error(lun, io); 13672 13673 /* 13674 * XXX KDM how do we treat commands that aren't completed 13675 * successfully? 13676 * 13677 * XXX KDM should we also track I/O latency? 13678 */ 13679 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13680 io->io_hdr.io_type == CTL_IO_SCSI) { 13681#ifdef CTL_TIME_IO 13682 struct bintime cur_bt; 13683#endif 13684 int type; 13685 13686 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13687 CTL_FLAG_DATA_IN) 13688 type = CTL_STATS_READ; 13689 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13690 CTL_FLAG_DATA_OUT) 13691 type = CTL_STATS_WRITE; 13692 else 13693 type = CTL_STATS_NO_IO; 13694 13695 lun->stats.ports[targ_port].bytes[type] += 13696 io->scsiio.kern_total_len; 13697 lun->stats.ports[targ_port].operations[type]++; 13698#ifdef CTL_TIME_IO 13699 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13700 &io->io_hdr.dma_bt); 13701 lun->stats.ports[targ_port].num_dmas[type] += 13702 io->io_hdr.num_dmas; 13703 getbintime(&cur_bt); 13704 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13705 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13706#endif 13707 } 13708 13709 /* 13710 * Remove this from the OOA queue. 13711 */ 13712 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13713#ifdef CTL_TIME_IO 13714 if (TAILQ_EMPTY(&lun->ooa_queue)) 13715 lun->last_busy = getsbinuptime(); 13716#endif 13717 13718 /* 13719 * Run through the blocked queue on this LUN and see if anything 13720 * has become unblocked, now that this transaction is done. 13721 */ 13722 ctl_check_blocked(lun); 13723 13724 /* 13725 * If the LUN has been invalidated, free it if there is nothing 13726 * left on its OOA queue. 13727 */ 13728 if ((lun->flags & CTL_LUN_INVALID) 13729 && TAILQ_EMPTY(&lun->ooa_queue)) { 13730 mtx_unlock(&lun->lun_lock); 13731 mtx_lock(&softc->ctl_lock); 13732 ctl_free_lun(lun); 13733 mtx_unlock(&softc->ctl_lock); 13734 } else 13735 mtx_unlock(&lun->lun_lock); 13736 13737bailout: 13738 13739 /* 13740 * If this command has been aborted, make sure we set the status 13741 * properly. The FETD is responsible for freeing the I/O and doing 13742 * whatever it needs to do to clean up its state. 13743 */ 13744 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13745 ctl_set_task_aborted(&io->scsiio); 13746 13747 /* 13748 * If enabled, print command error status. 13749 */ 13750 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13751 (ctl_debug & CTL_DEBUG_INFO) != 0) 13752 ctl_io_error_print(io, NULL); 13753 13754 /* 13755 * Tell the FETD or the other shelf controller we're done with this 13756 * command. Note that only SCSI commands get to this point. Task 13757 * management commands are completed above. 13758 * 13759 * We only send status to the other controller if we're in XFER 13760 * mode. In SER_ONLY mode, the I/O is done on the controller that 13761 * received the I/O (from CTL's perspective), and so the status is 13762 * generated there. 13763 * 13764 * XXX KDM if we hold the lock here, we could cause a deadlock 13765 * if the frontend comes back in in this context to queue 13766 * something. 13767 */ 13768 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13769 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13770 union ctl_ha_msg msg; 13771 13772 memset(&msg, 0, sizeof(msg)); 13773 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13774 msg.hdr.original_sc = io->io_hdr.original_sc; 13775 msg.hdr.nexus = io->io_hdr.nexus; 13776 msg.hdr.status = io->io_hdr.status; 13777 msg.scsi.scsi_status = io->scsiio.scsi_status; 13778 msg.scsi.tag_num = io->scsiio.tag_num; 13779 msg.scsi.tag_type = io->scsiio.tag_type; 13780 msg.scsi.sense_len = io->scsiio.sense_len; 13781 msg.scsi.sense_residual = io->scsiio.sense_residual; 13782 msg.scsi.residual = io->scsiio.residual; 13783 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13784 sizeof(io->scsiio.sense_data)); 13785 /* 13786 * We copy this whether or not this is an I/O-related 13787 * command. Otherwise, we'd have to go and check to see 13788 * whether it's a read/write command, and it really isn't 13789 * worth it. 13790 */ 13791 memcpy(&msg.scsi.lbalen, 13792 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13793 sizeof(msg.scsi.lbalen)); 13794 13795 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13796 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13797 /* XXX do something here */ 13798 } 13799 13800 ctl_free_io(io); 13801 } else 13802 fe_done(io); 13803 13804 return (CTL_RETVAL_COMPLETE); 13805} 13806 13807#ifdef CTL_WITH_CA 13808/* 13809 * Front end should call this if it doesn't do autosense. When the request 13810 * sense comes back in from the initiator, we'll dequeue this and send it. 13811 */ 13812int 13813ctl_queue_sense(union ctl_io *io) 13814{ 13815 struct ctl_lun *lun; 13816 struct ctl_port *port; 13817 struct ctl_softc *softc; 13818 uint32_t initidx, targ_lun; 13819 13820 softc = control_softc; 13821 13822 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13823 13824 /* 13825 * LUN lookup will likely move to the ctl_work_thread() once we 13826 * have our new queueing infrastructure (that doesn't put things on 13827 * a per-LUN queue initially). That is so that we can handle 13828 * things like an INQUIRY to a LUN that we don't have enabled. We 13829 * can't deal with that right now. 13830 */ 13831 mtx_lock(&softc->ctl_lock); 13832 13833 /* 13834 * If we don't have a LUN for this, just toss the sense 13835 * information. 13836 */ 13837 port = ctl_io_port(&ctsio->io_hdr); 13838 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13839 if ((targ_lun < CTL_MAX_LUNS) 13840 && (softc->ctl_luns[targ_lun] != NULL)) 13841 lun = softc->ctl_luns[targ_lun]; 13842 else 13843 goto bailout; 13844 13845 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13846 13847 mtx_lock(&lun->lun_lock); 13848 /* 13849 * Already have CA set for this LUN...toss the sense information. 13850 */ 13851 if (ctl_is_set(lun->have_ca, initidx)) { 13852 mtx_unlock(&lun->lun_lock); 13853 goto bailout; 13854 } 13855 13856 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13857 MIN(sizeof(lun->pending_sense[initidx]), 13858 sizeof(io->scsiio.sense_data))); 13859 ctl_set_mask(lun->have_ca, initidx); 13860 mtx_unlock(&lun->lun_lock); 13861 13862bailout: 13863 mtx_unlock(&softc->ctl_lock); 13864 13865 ctl_free_io(io); 13866 13867 return (CTL_RETVAL_COMPLETE); 13868} 13869#endif 13870 13871/* 13872 * Primary command inlet from frontend ports. All SCSI and task I/O 13873 * requests must go through this function. 13874 */ 13875int 13876ctl_queue(union ctl_io *io) 13877{ 13878 struct ctl_port *port; 13879 13880 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13881 13882#ifdef CTL_TIME_IO 13883 io->io_hdr.start_time = time_uptime; 13884 getbintime(&io->io_hdr.start_bt); 13885#endif /* CTL_TIME_IO */ 13886 13887 /* Map FE-specific LUN ID into global one. */ 13888 port = ctl_io_port(&io->io_hdr); 13889 io->io_hdr.nexus.targ_mapped_lun = 13890 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13891 13892 switch (io->io_hdr.io_type) { 13893 case CTL_IO_SCSI: 13894 case CTL_IO_TASK: 13895 if (ctl_debug & CTL_DEBUG_CDB) 13896 ctl_io_print(io); 13897 ctl_enqueue_incoming(io); 13898 break; 13899 default: 13900 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13901 return (EINVAL); 13902 } 13903 13904 return (CTL_RETVAL_COMPLETE); 13905} 13906 13907#ifdef CTL_IO_DELAY 13908static void 13909ctl_done_timer_wakeup(void *arg) 13910{ 13911 union ctl_io *io; 13912 13913 io = (union ctl_io *)arg; 13914 ctl_done(io); 13915} 13916#endif /* CTL_IO_DELAY */ 13917 13918void 13919ctl_done(union ctl_io *io) 13920{ 13921 13922 /* 13923 * Enable this to catch duplicate completion issues. 13924 */ 13925#if 0 13926 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13927 printf("%s: type %d msg %d cdb %x iptl: " 13928 "%d:%d:%d:%d tag 0x%04x " 13929 "flag %#x status %x\n", 13930 __func__, 13931 io->io_hdr.io_type, 13932 io->io_hdr.msg_type, 13933 io->scsiio.cdb[0], 13934 io->io_hdr.nexus.initid.id, 13935 io->io_hdr.nexus.targ_port, 13936 io->io_hdr.nexus.targ_target.id, 13937 io->io_hdr.nexus.targ_lun, 13938 (io->io_hdr.io_type == 13939 CTL_IO_TASK) ? 13940 io->taskio.tag_num : 13941 io->scsiio.tag_num, 13942 io->io_hdr.flags, 13943 io->io_hdr.status); 13944 } else 13945 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13946#endif 13947 13948 /* 13949 * This is an internal copy of an I/O, and should not go through 13950 * the normal done processing logic. 13951 */ 13952 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13953 return; 13954 13955 /* 13956 * We need to send a msg to the serializing shelf to finish the IO 13957 * as well. We don't send a finish message to the other shelf if 13958 * this is a task management command. Task management commands 13959 * aren't serialized in the OOA queue, but rather just executed on 13960 * both shelf controllers for commands that originated on that 13961 * controller. 13962 */ 13963 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13964 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13965 union ctl_ha_msg msg_io; 13966 13967 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13968 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13969 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13970 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13971 } 13972 /* continue on to finish IO */ 13973 } 13974#ifdef CTL_IO_DELAY 13975 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13976 struct ctl_lun *lun; 13977 13978 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13979 13980 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13981 } else { 13982 struct ctl_lun *lun; 13983 13984 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13985 13986 if ((lun != NULL) 13987 && (lun->delay_info.done_delay > 0)) { 13988 struct callout *callout; 13989 13990 callout = (struct callout *)&io->io_hdr.timer_bytes; 13991 callout_init(callout, /*mpsafe*/ 1); 13992 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13993 callout_reset(callout, 13994 lun->delay_info.done_delay * hz, 13995 ctl_done_timer_wakeup, io); 13996 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13997 lun->delay_info.done_delay = 0; 13998 return; 13999 } 14000 } 14001#endif /* CTL_IO_DELAY */ 14002 14003 ctl_enqueue_done(io); 14004} 14005 14006int 14007ctl_isc(struct ctl_scsiio *ctsio) 14008{ 14009 struct ctl_lun *lun; 14010 int retval; 14011 14012 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14013 14014 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14015 14016 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14017 14018 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14019 14020 return (retval); 14021} 14022 14023 14024static void 14025ctl_work_thread(void *arg) 14026{ 14027 struct ctl_thread *thr = (struct ctl_thread *)arg; 14028 struct ctl_softc *softc = thr->ctl_softc; 14029 union ctl_io *io; 14030 int retval; 14031 14032 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14033 14034 for (;;) { 14035 retval = 0; 14036 14037 /* 14038 * We handle the queues in this order: 14039 * - ISC 14040 * - done queue (to free up resources, unblock other commands) 14041 * - RtR queue 14042 * - incoming queue 14043 * 14044 * If those queues are empty, we break out of the loop and 14045 * go to sleep. 14046 */ 14047 mtx_lock(&thr->queue_lock); 14048 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14049 if (io != NULL) { 14050 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14051 mtx_unlock(&thr->queue_lock); 14052 ctl_handle_isc(io); 14053 continue; 14054 } 14055 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14056 if (io != NULL) { 14057 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14058 /* clear any blocked commands, call fe_done */ 14059 mtx_unlock(&thr->queue_lock); 14060 retval = ctl_process_done(io); 14061 continue; 14062 } 14063 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14064 if (io != NULL) { 14065 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14066 mtx_unlock(&thr->queue_lock); 14067 if (io->io_hdr.io_type == CTL_IO_TASK) 14068 ctl_run_task(io); 14069 else 14070 ctl_scsiio_precheck(softc, &io->scsiio); 14071 continue; 14072 } 14073 if (!ctl_pause_rtr) { 14074 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14075 if (io != NULL) { 14076 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14077 mtx_unlock(&thr->queue_lock); 14078 retval = ctl_scsiio(&io->scsiio); 14079 if (retval != CTL_RETVAL_COMPLETE) 14080 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14081 continue; 14082 } 14083 } 14084 14085 /* Sleep until we have something to do. */ 14086 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14087 } 14088} 14089 14090static void 14091ctl_lun_thread(void *arg) 14092{ 14093 struct ctl_softc *softc = (struct ctl_softc *)arg; 14094 struct ctl_be_lun *be_lun; 14095 int retval; 14096 14097 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14098 14099 for (;;) { 14100 retval = 0; 14101 mtx_lock(&softc->ctl_lock); 14102 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14103 if (be_lun != NULL) { 14104 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14105 mtx_unlock(&softc->ctl_lock); 14106 ctl_create_lun(be_lun); 14107 continue; 14108 } 14109 14110 /* Sleep until we have something to do. */ 14111 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14112 PDROP | PRIBIO, "-", 0); 14113 } 14114} 14115 14116static void 14117ctl_thresh_thread(void *arg) 14118{ 14119 struct ctl_softc *softc = (struct ctl_softc *)arg; 14120 struct ctl_lun *lun; 14121 struct ctl_be_lun *be_lun; 14122 struct scsi_da_rw_recovery_page *rwpage; 14123 struct ctl_logical_block_provisioning_page *page; 14124 const char *attr; 14125 uint64_t thres, val; 14126 int i, e; 14127 14128 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14129 14130 for (;;) { 14131 mtx_lock(&softc->ctl_lock); 14132 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14133 be_lun = lun->be_lun; 14134 if ((lun->flags & CTL_LUN_DISABLED) || 14135 (lun->flags & CTL_LUN_OFFLINE) || 14136 lun->backend->lun_attr == NULL) 14137 continue; 14138 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14139 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14140 continue; 14141 e = 0; 14142 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14143 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14144 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14145 continue; 14146 thres = scsi_4btoul(page->descr[i].count); 14147 thres <<= CTL_LBP_EXPONENT; 14148 switch (page->descr[i].resource) { 14149 case 0x01: 14150 attr = "blocksavail"; 14151 break; 14152 case 0x02: 14153 attr = "blocksused"; 14154 break; 14155 case 0xf1: 14156 attr = "poolblocksavail"; 14157 break; 14158 case 0xf2: 14159 attr = "poolblocksused"; 14160 break; 14161 default: 14162 continue; 14163 } 14164 mtx_unlock(&softc->ctl_lock); // XXX 14165 val = lun->backend->lun_attr( 14166 lun->be_lun->be_lun, attr); 14167 mtx_lock(&softc->ctl_lock); 14168 if (val == UINT64_MAX) 14169 continue; 14170 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14171 == SLBPPD_ARMING_INC) 14172 e |= (val >= thres); 14173 else 14174 e |= (val <= thres); 14175 } 14176 mtx_lock(&lun->lun_lock); 14177 if (e) { 14178 if (lun->lasttpt == 0 || 14179 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14180 lun->lasttpt = time_uptime; 14181 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14182 } 14183 } else { 14184 lun->lasttpt = 0; 14185 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14186 } 14187 mtx_unlock(&lun->lun_lock); 14188 } 14189 mtx_unlock(&softc->ctl_lock); 14190 pause("-", CTL_LBP_PERIOD * hz); 14191 } 14192} 14193 14194static void 14195ctl_enqueue_incoming(union ctl_io *io) 14196{ 14197 struct ctl_softc *softc = control_softc; 14198 struct ctl_thread *thr; 14199 u_int idx; 14200 14201 idx = (io->io_hdr.nexus.targ_port * 127 + 14202 io->io_hdr.nexus.initid.id) % worker_threads; 14203 thr = &softc->threads[idx]; 14204 mtx_lock(&thr->queue_lock); 14205 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14206 mtx_unlock(&thr->queue_lock); 14207 wakeup(thr); 14208} 14209 14210static void 14211ctl_enqueue_rtr(union ctl_io *io) 14212{ 14213 struct ctl_softc *softc = control_softc; 14214 struct ctl_thread *thr; 14215 14216 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14217 mtx_lock(&thr->queue_lock); 14218 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14219 mtx_unlock(&thr->queue_lock); 14220 wakeup(thr); 14221} 14222 14223static void 14224ctl_enqueue_done(union ctl_io *io) 14225{ 14226 struct ctl_softc *softc = control_softc; 14227 struct ctl_thread *thr; 14228 14229 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14230 mtx_lock(&thr->queue_lock); 14231 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14232 mtx_unlock(&thr->queue_lock); 14233 wakeup(thr); 14234} 14235 14236static void 14237ctl_enqueue_isc(union ctl_io *io) 14238{ 14239 struct ctl_softc *softc = control_softc; 14240 struct ctl_thread *thr; 14241 14242 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14243 mtx_lock(&thr->queue_lock); 14244 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14245 mtx_unlock(&thr->queue_lock); 14246 wakeup(thr); 14247} 14248 14249/* Initialization and failover */ 14250 14251void 14252ctl_init_isc_msg(void) 14253{ 14254 printf("CTL: Still calling this thing\n"); 14255} 14256 14257/* 14258 * Init component 14259 * Initializes component into configuration defined by bootMode 14260 * (see hasc-sv.c) 14261 * returns hasc_Status: 14262 * OK 14263 * ERROR - fatal error 14264 */ 14265static ctl_ha_comp_status 14266ctl_isc_init(struct ctl_ha_component *c) 14267{ 14268 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14269 14270 c->status = ret; 14271 return ret; 14272} 14273 14274/* Start component 14275 * Starts component in state requested. If component starts successfully, 14276 * it must set its own state to the requestrd state 14277 * When requested state is HASC_STATE_HA, the component may refine it 14278 * by adding _SLAVE or _MASTER flags. 14279 * Currently allowed state transitions are: 14280 * UNKNOWN->HA - initial startup 14281 * UNKNOWN->SINGLE - initial startup when no parter detected 14282 * HA->SINGLE - failover 14283 * returns ctl_ha_comp_status: 14284 * OK - component successfully started in requested state 14285 * FAILED - could not start the requested state, failover may 14286 * be possible 14287 * ERROR - fatal error detected, no future startup possible 14288 */ 14289static ctl_ha_comp_status 14290ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14291{ 14292 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14293 14294 printf("%s: go\n", __func__); 14295 14296 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14297 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14298 control_softc->is_single = 0; 14299 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14300 != CTL_HA_STATUS_SUCCESS) { 14301 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14302 ret = CTL_HA_COMP_STATUS_ERROR; 14303 } 14304 } else if (CTL_HA_STATE_IS_HA(c->state) 14305 && CTL_HA_STATE_IS_SINGLE(state)){ 14306 // HA->SINGLE transition 14307 ctl_failover(); 14308 control_softc->is_single = 1; 14309 } else { 14310 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14311 c->state, state); 14312 ret = CTL_HA_COMP_STATUS_ERROR; 14313 } 14314 if (CTL_HA_STATE_IS_SINGLE(state)) 14315 control_softc->is_single = 1; 14316 14317 c->state = state; 14318 c->status = ret; 14319 return ret; 14320} 14321 14322/* 14323 * Quiesce component 14324 * The component must clear any error conditions (set status to OK) and 14325 * prepare itself to another Start call 14326 * returns ctl_ha_comp_status: 14327 * OK 14328 * ERROR 14329 */ 14330static ctl_ha_comp_status 14331ctl_isc_quiesce(struct ctl_ha_component *c) 14332{ 14333 int ret = CTL_HA_COMP_STATUS_OK; 14334 14335 ctl_pause_rtr = 1; 14336 c->status = ret; 14337 return ret; 14338} 14339 14340struct ctl_ha_component ctl_ha_component_ctlisc = 14341{ 14342 .name = "CTL ISC", 14343 .state = CTL_HA_STATE_UNKNOWN, 14344 .init = ctl_isc_init, 14345 .start = ctl_isc_start, 14346 .quiesce = ctl_isc_quiesce 14347}; 14348 14349/* 14350 * vim: ts=8 14351 */ 14352