ctl.c revision 275892
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 275892 2014-12-18 08:38:07Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/ctype.h> 50#include <sys/kernel.h> 51#include <sys/types.h> 52#include <sys/kthread.h> 53#include <sys/bio.h> 54#include <sys/fcntl.h> 55#include <sys/lock.h> 56#include <sys/module.h> 57#include <sys/mutex.h> 58#include <sys/condvar.h> 59#include <sys/malloc.h> 60#include <sys/conf.h> 61#include <sys/ioccom.h> 62#include <sys/queue.h> 63#include <sys/sbuf.h> 64#include <sys/smp.h> 65#include <sys/endian.h> 66#include <sys/sysctl.h> 67#include <vm/uma.h> 68 69#include <cam/cam.h> 70#include <cam/scsi/scsi_all.h> 71#include <cam/scsi/scsi_da.h> 72#include <cam/ctl/ctl_io.h> 73#include <cam/ctl/ctl.h> 74#include <cam/ctl/ctl_frontend.h> 75#include <cam/ctl/ctl_frontend_internal.h> 76#include <cam/ctl/ctl_util.h> 77#include <cam/ctl/ctl_backend.h> 78#include <cam/ctl/ctl_ioctl.h> 79#include <cam/ctl/ctl_ha.h> 80#include <cam/ctl/ctl_private.h> 81#include <cam/ctl/ctl_debug.h> 82#include <cam/ctl/ctl_scsi_all.h> 83#include <cam/ctl/ctl_error.h> 84 85struct ctl_softc *control_softc = NULL; 86 87/* 88 * Size and alignment macros needed for Copan-specific HA hardware. These 89 * can go away when the HA code is re-written, and uses busdma for any 90 * hardware. 91 */ 92#define CTL_ALIGN_8B(target, source, type) \ 93 if (((uint32_t)source & 0x7) != 0) \ 94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 95 else \ 96 target = (type)source; 97 98#define CTL_SIZE_8B(target, size) \ 99 if ((size & 0x7) != 0) \ 100 target = size + (0x8 - (size & 0x7)); \ 101 else \ 102 target = size; 103 104#define CTL_ALIGN_8B_MARGIN 16 105 106/* 107 * Template mode pages. 108 */ 109 110/* 111 * Note that these are default values only. The actual values will be 112 * filled in when the user does a mode sense. 113 */ 114static struct copan_debugconf_subpage debugconf_page_default = { 115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 116 DBGCNF_SUBPAGE_CODE, /* subpage */ 117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 119 DBGCNF_VERSION, /* page_version */ 120 {CTL_TIME_IO_DEFAULT_SECS>>8, 121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 122}; 123 124static struct copan_debugconf_subpage debugconf_page_changeable = { 125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 126 DBGCNF_SUBPAGE_CODE, /* subpage */ 127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 129 0, /* page_version */ 130 {0xff,0xff}, /* ctl_time_io_secs */ 131}; 132 133static struct scsi_da_rw_recovery_page rw_er_page_default = { 134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 137 /*read_retry_count*/0, 138 /*correction_span*/0, 139 /*head_offset_count*/0, 140 /*data_strobe_offset_cnt*/0, 141 /*byte8*/SMS_RWER_LBPERE, 142 /*write_retry_count*/0, 143 /*reserved2*/0, 144 /*recovery_time_limit*/{0, 0}, 145}; 146 147static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 150 /*byte3*/0, 151 /*read_retry_count*/0, 152 /*correction_span*/0, 153 /*head_offset_count*/0, 154 /*data_strobe_offset_cnt*/0, 155 /*byte8*/0, 156 /*write_retry_count*/0, 157 /*reserved2*/0, 158 /*recovery_time_limit*/{0, 0}, 159}; 160 161static struct scsi_format_page format_page_default = { 162 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 163 /*page_length*/sizeof(struct scsi_format_page) - 2, 164 /*tracks_per_zone*/ {0, 0}, 165 /*alt_sectors_per_zone*/ {0, 0}, 166 /*alt_tracks_per_zone*/ {0, 0}, 167 /*alt_tracks_per_lun*/ {0, 0}, 168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 170 /*bytes_per_sector*/ {0, 0}, 171 /*interleave*/ {0, 0}, 172 /*track_skew*/ {0, 0}, 173 /*cylinder_skew*/ {0, 0}, 174 /*flags*/ SFP_HSEC, 175 /*reserved*/ {0, 0, 0} 176}; 177 178static struct scsi_format_page format_page_changeable = { 179 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 180 /*page_length*/sizeof(struct scsi_format_page) - 2, 181 /*tracks_per_zone*/ {0, 0}, 182 /*alt_sectors_per_zone*/ {0, 0}, 183 /*alt_tracks_per_zone*/ {0, 0}, 184 /*alt_tracks_per_lun*/ {0, 0}, 185 /*sectors_per_track*/ {0, 0}, 186 /*bytes_per_sector*/ {0, 0}, 187 /*interleave*/ {0, 0}, 188 /*track_skew*/ {0, 0}, 189 /*cylinder_skew*/ {0, 0}, 190 /*flags*/ 0, 191 /*reserved*/ {0, 0, 0} 192}; 193 194static struct scsi_rigid_disk_page rigid_disk_page_default = { 195 /*page_code*/SMS_RIGID_DISK_PAGE, 196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 197 /*cylinders*/ {0, 0, 0}, 198 /*heads*/ CTL_DEFAULT_HEADS, 199 /*start_write_precomp*/ {0, 0, 0}, 200 /*start_reduced_current*/ {0, 0, 0}, 201 /*step_rate*/ {0, 0}, 202 /*landing_zone_cylinder*/ {0, 0, 0}, 203 /*rpl*/ SRDP_RPL_DISABLED, 204 /*rotational_offset*/ 0, 205 /*reserved1*/ 0, 206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 207 CTL_DEFAULT_ROTATION_RATE & 0xff}, 208 /*reserved2*/ {0, 0} 209}; 210 211static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 212 /*page_code*/SMS_RIGID_DISK_PAGE, 213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 214 /*cylinders*/ {0, 0, 0}, 215 /*heads*/ 0, 216 /*start_write_precomp*/ {0, 0, 0}, 217 /*start_reduced_current*/ {0, 0, 0}, 218 /*step_rate*/ {0, 0}, 219 /*landing_zone_cylinder*/ {0, 0, 0}, 220 /*rpl*/ 0, 221 /*rotational_offset*/ 0, 222 /*reserved1*/ 0, 223 /*rotation_rate*/ {0, 0}, 224 /*reserved2*/ {0, 0} 225}; 226 227static struct scsi_caching_page caching_page_default = { 228 /*page_code*/SMS_CACHING_PAGE, 229 /*page_length*/sizeof(struct scsi_caching_page) - 2, 230 /*flags1*/ SCP_DISC | SCP_WCE, 231 /*ret_priority*/ 0, 232 /*disable_pf_transfer_len*/ {0xff, 0xff}, 233 /*min_prefetch*/ {0, 0}, 234 /*max_prefetch*/ {0xff, 0xff}, 235 /*max_pf_ceiling*/ {0xff, 0xff}, 236 /*flags2*/ 0, 237 /*cache_segments*/ 0, 238 /*cache_seg_size*/ {0, 0}, 239 /*reserved*/ 0, 240 /*non_cache_seg_size*/ {0, 0, 0} 241}; 242 243static struct scsi_caching_page caching_page_changeable = { 244 /*page_code*/SMS_CACHING_PAGE, 245 /*page_length*/sizeof(struct scsi_caching_page) - 2, 246 /*flags1*/ SCP_WCE | SCP_RCD, 247 /*ret_priority*/ 0, 248 /*disable_pf_transfer_len*/ {0, 0}, 249 /*min_prefetch*/ {0, 0}, 250 /*max_prefetch*/ {0, 0}, 251 /*max_pf_ceiling*/ {0, 0}, 252 /*flags2*/ 0, 253 /*cache_segments*/ 0, 254 /*cache_seg_size*/ {0, 0}, 255 /*reserved*/ 0, 256 /*non_cache_seg_size*/ {0, 0, 0} 257}; 258 259static struct scsi_control_page control_page_default = { 260 /*page_code*/SMS_CONTROL_MODE_PAGE, 261 /*page_length*/sizeof(struct scsi_control_page) - 2, 262 /*rlec*/0, 263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 264 /*eca_and_aen*/0, 265 /*flags4*/SCP_TAS, 266 /*aen_holdoff_period*/{0, 0}, 267 /*busy_timeout_period*/{0, 0}, 268 /*extended_selftest_completion_time*/{0, 0} 269}; 270 271static struct scsi_control_page control_page_changeable = { 272 /*page_code*/SMS_CONTROL_MODE_PAGE, 273 /*page_length*/sizeof(struct scsi_control_page) - 2, 274 /*rlec*/SCP_DSENSE, 275 /*queue_flags*/SCP_QUEUE_ALG_MASK, 276 /*eca_and_aen*/SCP_SWP, 277 /*flags4*/0, 278 /*aen_holdoff_period*/{0, 0}, 279 /*busy_timeout_period*/{0, 0}, 280 /*extended_selftest_completion_time*/{0, 0} 281}; 282 283static struct scsi_info_exceptions_page ie_page_default = { 284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 286 /*info_flags*/SIEP_FLAGS_DEXCPT, 287 /*mrie*/0, 288 /*interval_timer*/{0, 0, 0, 0}, 289 /*report_count*/{0, 0, 0, 0} 290}; 291 292static struct scsi_info_exceptions_page ie_page_changeable = { 293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 295 /*info_flags*/0, 296 /*mrie*/0, 297 /*interval_timer*/{0, 0, 0, 0}, 298 /*report_count*/{0, 0, 0, 0} 299}; 300 301#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 302 303static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 305 /*subpage_code*/0x02, 306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 307 /*flags*/0, 308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 309 /*descr*/{}}, 310 {{/*flags*/0, 311 /*resource*/0x01, 312 /*reserved*/{0, 0}, 313 /*count*/{0, 0, 0, 0}}, 314 {/*flags*/0, 315 /*resource*/0x02, 316 /*reserved*/{0, 0}, 317 /*count*/{0, 0, 0, 0}}, 318 {/*flags*/0, 319 /*resource*/0xf1, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0xf2, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}} 326 } 327}; 328 329static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 331 /*subpage_code*/0x02, 332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 333 /*flags*/0, 334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 335 /*descr*/{}}, 336 {{/*flags*/0, 337 /*resource*/0, 338 /*reserved*/{0, 0}, 339 /*count*/{0, 0, 0, 0}}, 340 {/*flags*/0, 341 /*resource*/0, 342 /*reserved*/{0, 0}, 343 /*count*/{0, 0, 0, 0}}, 344 {/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}} 352 } 353}; 354 355/* 356 * XXX KDM move these into the softc. 357 */ 358static int rcv_sync_msg; 359static uint8_t ctl_pause_rtr; 360 361SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 362static int worker_threads = -1; 363TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 364SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 365 &worker_threads, 1, "Number of worker threads"); 366static int ctl_debug = CTL_DEBUG_NONE; 367TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 368SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 369 &ctl_debug, 0, "Enabled debug flags"); 370 371/* 372 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 373 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 374 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 375 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 376 */ 377#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 378 379static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 380 int param); 381static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 382static int ctl_init(void); 383void ctl_shutdown(void); 384static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 385static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 386static void ctl_ioctl_online(void *arg); 387static void ctl_ioctl_offline(void *arg); 388static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 389static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 390static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 391static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 392static int ctl_ioctl_submit_wait(union ctl_io *io); 393static void ctl_ioctl_datamove(union ctl_io *io); 394static void ctl_ioctl_done(union ctl_io *io); 395static void ctl_ioctl_hard_startstop_callback(void *arg, 396 struct cfi_metatask *metatask); 397static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 398static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 399 struct ctl_ooa *ooa_hdr, 400 struct ctl_ooa_entry *kern_entries); 401static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 402 struct thread *td); 403static uint32_t ctl_map_lun(int port_num, uint32_t lun); 404static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 405static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 406 struct ctl_be_lun *be_lun, struct ctl_id target_id); 407static int ctl_free_lun(struct ctl_lun *lun); 408static void ctl_create_lun(struct ctl_be_lun *be_lun); 409/** 410static void ctl_failover_change_pages(struct ctl_softc *softc, 411 struct ctl_scsiio *ctsio, int master); 412**/ 413 414static int ctl_do_mode_select(union ctl_io *io); 415static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 416 uint64_t res_key, uint64_t sa_res_key, 417 uint8_t type, uint32_t residx, 418 struct ctl_scsiio *ctsio, 419 struct scsi_per_res_out *cdb, 420 struct scsi_per_res_out_parms* param); 421static void ctl_pro_preempt_other(struct ctl_lun *lun, 422 union ctl_ha_msg *msg); 423static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 424static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 425static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 426static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 427static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 428static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 429static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 430 int alloc_len); 431static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 432 int alloc_len); 433static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 434static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 435static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 436static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 437static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 438static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 439static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 440 union ctl_io *pending_io, union ctl_io *ooa_io); 441static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 442 union ctl_io *starting_io); 443static int ctl_check_blocked(struct ctl_lun *lun); 444static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 445 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 int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 451 struct ctl_scsiio *ctsio); 452static int ctl_scsiio(struct ctl_scsiio *ctsio); 453 454static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 455static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 456 ctl_ua_type ua_type); 457static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 458 ctl_ua_type ua_type); 459static int ctl_abort_task(union ctl_io *io); 460static int ctl_abort_task_set(union ctl_io *io); 461static int ctl_i_t_nexus_reset(union ctl_io *io); 462static void ctl_run_task(union ctl_io *io); 463#ifdef CTL_IO_DELAY 464static void ctl_datamove_timer_wakeup(void *arg); 465static void ctl_done_timer_wakeup(void *arg); 466#endif /* CTL_IO_DELAY */ 467 468static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 469static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 470static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 471static void ctl_datamove_remote_write(union ctl_io *io); 472static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 473static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 474static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 475static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 476 ctl_ha_dt_cb callback); 477static void ctl_datamove_remote_read(union ctl_io *io); 478static void ctl_datamove_remote(union ctl_io *io); 479static int ctl_process_done(union ctl_io *io); 480static void ctl_lun_thread(void *arg); 481static void ctl_thresh_thread(void *arg); 482static void ctl_work_thread(void *arg); 483static void ctl_enqueue_incoming(union ctl_io *io); 484static void ctl_enqueue_rtr(union ctl_io *io); 485static void ctl_enqueue_done(union ctl_io *io); 486static void ctl_enqueue_isc(union ctl_io *io); 487static const struct ctl_cmd_entry * 488 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 489static const struct ctl_cmd_entry * 490 ctl_validate_command(struct ctl_scsiio *ctsio); 491static int ctl_cmd_applicable(uint8_t lun_type, 492 const struct ctl_cmd_entry *entry); 493 494/* 495 * Load the serialization table. This isn't very pretty, but is probably 496 * the easiest way to do it. 497 */ 498#include "ctl_ser_table.c" 499 500/* 501 * We only need to define open, close and ioctl routines for this driver. 502 */ 503static struct cdevsw ctl_cdevsw = { 504 .d_version = D_VERSION, 505 .d_flags = 0, 506 .d_open = ctl_open, 507 .d_close = ctl_close, 508 .d_ioctl = ctl_ioctl, 509 .d_name = "ctl", 510}; 511 512 513MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 514MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 515 516static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 517 518static moduledata_t ctl_moduledata = { 519 "ctl", 520 ctl_module_event_handler, 521 NULL 522}; 523 524DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 525MODULE_VERSION(ctl, 1); 526 527static struct ctl_frontend ioctl_frontend = 528{ 529 .name = "ioctl", 530}; 531 532static void 533ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 534 union ctl_ha_msg *msg_info) 535{ 536 struct ctl_scsiio *ctsio; 537 538 if (msg_info->hdr.original_sc == NULL) { 539 printf("%s: original_sc == NULL!\n", __func__); 540 /* XXX KDM now what? */ 541 return; 542 } 543 544 ctsio = &msg_info->hdr.original_sc->scsiio; 545 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 546 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 547 ctsio->io_hdr.status = msg_info->hdr.status; 548 ctsio->scsi_status = msg_info->scsi.scsi_status; 549 ctsio->sense_len = msg_info->scsi.sense_len; 550 ctsio->sense_residual = msg_info->scsi.sense_residual; 551 ctsio->residual = msg_info->scsi.residual; 552 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 553 sizeof(ctsio->sense_data)); 554 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 555 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 556 ctl_enqueue_isc((union ctl_io *)ctsio); 557} 558 559static void 560ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 561 union ctl_ha_msg *msg_info) 562{ 563 struct ctl_scsiio *ctsio; 564 565 if (msg_info->hdr.serializing_sc == NULL) { 566 printf("%s: serializing_sc == NULL!\n", __func__); 567 /* XXX KDM now what? */ 568 return; 569 } 570 571 ctsio = &msg_info->hdr.serializing_sc->scsiio; 572#if 0 573 /* 574 * Attempt to catch the situation where an I/O has 575 * been freed, and we're using it again. 576 */ 577 if (ctsio->io_hdr.io_type == 0xff) { 578 union ctl_io *tmp_io; 579 tmp_io = (union ctl_io *)ctsio; 580 printf("%s: %p use after free!\n", __func__, 581 ctsio); 582 printf("%s: type %d msg %d cdb %x iptl: " 583 "%d:%d:%d:%d tag 0x%04x " 584 "flag %#x status %x\n", 585 __func__, 586 tmp_io->io_hdr.io_type, 587 tmp_io->io_hdr.msg_type, 588 tmp_io->scsiio.cdb[0], 589 tmp_io->io_hdr.nexus.initid.id, 590 tmp_io->io_hdr.nexus.targ_port, 591 tmp_io->io_hdr.nexus.targ_target.id, 592 tmp_io->io_hdr.nexus.targ_lun, 593 (tmp_io->io_hdr.io_type == 594 CTL_IO_TASK) ? 595 tmp_io->taskio.tag_num : 596 tmp_io->scsiio.tag_num, 597 tmp_io->io_hdr.flags, 598 tmp_io->io_hdr.status); 599 } 600#endif 601 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 602 ctl_enqueue_isc((union ctl_io *)ctsio); 603} 604 605/* 606 * ISC (Inter Shelf Communication) event handler. Events from the HA 607 * subsystem come in here. 608 */ 609static void 610ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 611{ 612 struct ctl_softc *ctl_softc; 613 union ctl_io *io; 614 struct ctl_prio *presio; 615 ctl_ha_status isc_status; 616 617 ctl_softc = control_softc; 618 io = NULL; 619 620 621#if 0 622 printf("CTL: Isc Msg event %d\n", event); 623#endif 624 if (event == CTL_HA_EVT_MSG_RECV) { 625 union ctl_ha_msg msg_info; 626 627 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 628 sizeof(msg_info), /*wait*/ 0); 629#if 0 630 printf("CTL: msg_type %d\n", msg_info.msg_type); 631#endif 632 if (isc_status != 0) { 633 printf("Error receiving message, status = %d\n", 634 isc_status); 635 return; 636 } 637 638 switch (msg_info.hdr.msg_type) { 639 case CTL_MSG_SERIALIZE: 640#if 0 641 printf("Serialize\n"); 642#endif 643 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool); 644 if (io == NULL) { 645 printf("ctl_isc_event_handler: can't allocate " 646 "ctl_io!\n"); 647 /* Bad Juju */ 648 /* Need to set busy and send msg back */ 649 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 650 msg_info.hdr.status = CTL_SCSI_ERROR; 651 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 652 msg_info.scsi.sense_len = 0; 653 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 654 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 655 } 656 goto bailout; 657 } 658 ctl_zero_io(io); 659 // populate ctsio from msg_info 660 io->io_hdr.io_type = CTL_IO_SCSI; 661 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 662 io->io_hdr.original_sc = msg_info.hdr.original_sc; 663#if 0 664 printf("pOrig %x\n", (int)msg_info.original_sc); 665#endif 666 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 667 CTL_FLAG_IO_ACTIVE; 668 /* 669 * If we're in serialization-only mode, we don't 670 * want to go through full done processing. Thus 671 * the COPY flag. 672 * 673 * XXX KDM add another flag that is more specific. 674 */ 675 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 676 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 677 io->io_hdr.nexus = msg_info.hdr.nexus; 678#if 0 679 printf("targ %d, port %d, iid %d, lun %d\n", 680 io->io_hdr.nexus.targ_target.id, 681 io->io_hdr.nexus.targ_port, 682 io->io_hdr.nexus.initid.id, 683 io->io_hdr.nexus.targ_lun); 684#endif 685 io->scsiio.tag_num = msg_info.scsi.tag_num; 686 io->scsiio.tag_type = msg_info.scsi.tag_type; 687 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 688 CTL_MAX_CDBLEN); 689 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 690 const struct ctl_cmd_entry *entry; 691 692 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 693 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 694 io->io_hdr.flags |= 695 entry->flags & CTL_FLAG_DATA_MASK; 696 } 697 ctl_enqueue_isc(io); 698 break; 699 700 /* Performed on the Originating SC, XFER mode only */ 701 case CTL_MSG_DATAMOVE: { 702 struct ctl_sg_entry *sgl; 703 int i, j; 704 705 io = msg_info.hdr.original_sc; 706 if (io == NULL) { 707 printf("%s: original_sc == NULL!\n", __func__); 708 /* XXX KDM do something here */ 709 break; 710 } 711 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 712 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 713 /* 714 * Keep track of this, we need to send it back over 715 * when the datamove is complete. 716 */ 717 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 718 719 if (msg_info.dt.sg_sequence == 0) { 720 /* 721 * XXX KDM we use the preallocated S/G list 722 * here, but we'll need to change this to 723 * dynamic allocation if we need larger S/G 724 * lists. 725 */ 726 if (msg_info.dt.kern_sg_entries > 727 sizeof(io->io_hdr.remote_sglist) / 728 sizeof(io->io_hdr.remote_sglist[0])) { 729 printf("%s: number of S/G entries " 730 "needed %u > allocated num %zd\n", 731 __func__, 732 msg_info.dt.kern_sg_entries, 733 sizeof(io->io_hdr.remote_sglist)/ 734 sizeof(io->io_hdr.remote_sglist[0])); 735 736 /* 737 * XXX KDM send a message back to 738 * the other side to shut down the 739 * DMA. The error will come back 740 * through via the normal channel. 741 */ 742 break; 743 } 744 sgl = io->io_hdr.remote_sglist; 745 memset(sgl, 0, 746 sizeof(io->io_hdr.remote_sglist)); 747 748 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 749 750 io->scsiio.kern_sg_entries = 751 msg_info.dt.kern_sg_entries; 752 io->scsiio.rem_sg_entries = 753 msg_info.dt.kern_sg_entries; 754 io->scsiio.kern_data_len = 755 msg_info.dt.kern_data_len; 756 io->scsiio.kern_total_len = 757 msg_info.dt.kern_total_len; 758 io->scsiio.kern_data_resid = 759 msg_info.dt.kern_data_resid; 760 io->scsiio.kern_rel_offset = 761 msg_info.dt.kern_rel_offset; 762 /* 763 * Clear out per-DMA flags. 764 */ 765 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 766 /* 767 * Add per-DMA flags that are set for this 768 * particular DMA request. 769 */ 770 io->io_hdr.flags |= msg_info.dt.flags & 771 CTL_FLAG_RDMA_MASK; 772 } else 773 sgl = (struct ctl_sg_entry *) 774 io->scsiio.kern_data_ptr; 775 776 for (i = msg_info.dt.sent_sg_entries, j = 0; 777 i < (msg_info.dt.sent_sg_entries + 778 msg_info.dt.cur_sg_entries); i++, j++) { 779 sgl[i].addr = msg_info.dt.sg_list[j].addr; 780 sgl[i].len = msg_info.dt.sg_list[j].len; 781 782#if 0 783 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 784 __func__, 785 msg_info.dt.sg_list[j].addr, 786 msg_info.dt.sg_list[j].len, 787 sgl[i].addr, sgl[i].len, j, i); 788#endif 789 } 790#if 0 791 memcpy(&sgl[msg_info.dt.sent_sg_entries], 792 msg_info.dt.sg_list, 793 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 794#endif 795 796 /* 797 * If this is the last piece of the I/O, we've got 798 * the full S/G list. Queue processing in the thread. 799 * Otherwise wait for the next piece. 800 */ 801 if (msg_info.dt.sg_last != 0) 802 ctl_enqueue_isc(io); 803 break; 804 } 805 /* Performed on the Serializing (primary) SC, XFER mode only */ 806 case CTL_MSG_DATAMOVE_DONE: { 807 if (msg_info.hdr.serializing_sc == NULL) { 808 printf("%s: serializing_sc == NULL!\n", 809 __func__); 810 /* XXX KDM now what? */ 811 break; 812 } 813 /* 814 * We grab the sense information here in case 815 * there was a failure, so we can return status 816 * back to the initiator. 817 */ 818 io = msg_info.hdr.serializing_sc; 819 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 820 io->io_hdr.status = msg_info.hdr.status; 821 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 822 io->scsiio.sense_len = msg_info.scsi.sense_len; 823 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 824 io->io_hdr.port_status = msg_info.scsi.fetd_status; 825 io->scsiio.residual = msg_info.scsi.residual; 826 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 827 sizeof(io->scsiio.sense_data)); 828 ctl_enqueue_isc(io); 829 break; 830 } 831 832 /* Preformed on Originating SC, SER_ONLY mode */ 833 case CTL_MSG_R2R: 834 io = msg_info.hdr.original_sc; 835 if (io == NULL) { 836 printf("%s: Major Bummer\n", __func__); 837 return; 838 } else { 839#if 0 840 printf("pOrig %x\n",(int) ctsio); 841#endif 842 } 843 io->io_hdr.msg_type = CTL_MSG_R2R; 844 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 845 ctl_enqueue_isc(io); 846 break; 847 848 /* 849 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 850 * mode. 851 * Performed on the Originating (i.e. secondary) SC in XFER 852 * mode 853 */ 854 case CTL_MSG_FINISH_IO: 855 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 856 ctl_isc_handler_finish_xfer(ctl_softc, 857 &msg_info); 858 else 859 ctl_isc_handler_finish_ser_only(ctl_softc, 860 &msg_info); 861 break; 862 863 /* Preformed on Originating SC */ 864 case CTL_MSG_BAD_JUJU: 865 io = msg_info.hdr.original_sc; 866 if (io == NULL) { 867 printf("%s: Bad JUJU!, original_sc is NULL!\n", 868 __func__); 869 break; 870 } 871 ctl_copy_sense_data(&msg_info, io); 872 /* 873 * IO should have already been cleaned up on other 874 * SC so clear this flag so we won't send a message 875 * back to finish the IO there. 876 */ 877 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 878 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 879 880 /* io = msg_info.hdr.serializing_sc; */ 881 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 882 ctl_enqueue_isc(io); 883 break; 884 885 /* Handle resets sent from the other side */ 886 case CTL_MSG_MANAGE_TASKS: { 887 struct ctl_taskio *taskio; 888 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 889 ctl_softc->othersc_pool); 890 if (taskio == NULL) { 891 printf("ctl_isc_event_handler: can't allocate " 892 "ctl_io!\n"); 893 /* Bad Juju */ 894 /* should I just call the proper reset func 895 here??? */ 896 goto bailout; 897 } 898 ctl_zero_io((union ctl_io *)taskio); 899 taskio->io_hdr.io_type = CTL_IO_TASK; 900 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 901 taskio->io_hdr.nexus = msg_info.hdr.nexus; 902 taskio->task_action = msg_info.task.task_action; 903 taskio->tag_num = msg_info.task.tag_num; 904 taskio->tag_type = msg_info.task.tag_type; 905#ifdef CTL_TIME_IO 906 taskio->io_hdr.start_time = time_uptime; 907 getbintime(&taskio->io_hdr.start_bt); 908#if 0 909 cs_prof_gettime(&taskio->io_hdr.start_ticks); 910#endif 911#endif /* CTL_TIME_IO */ 912 ctl_run_task((union ctl_io *)taskio); 913 break; 914 } 915 /* Persistent Reserve action which needs attention */ 916 case CTL_MSG_PERS_ACTION: 917 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 918 ctl_softc->othersc_pool); 919 if (presio == NULL) { 920 printf("ctl_isc_event_handler: can't allocate " 921 "ctl_io!\n"); 922 /* Bad Juju */ 923 /* Need to set busy and send msg back */ 924 goto bailout; 925 } 926 ctl_zero_io((union ctl_io *)presio); 927 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 928 presio->pr_msg = msg_info.pr; 929 ctl_enqueue_isc((union ctl_io *)presio); 930 break; 931 case CTL_MSG_SYNC_FE: 932 rcv_sync_msg = 1; 933 break; 934 default: 935 printf("How did I get here?\n"); 936 } 937 } else if (event == CTL_HA_EVT_MSG_SENT) { 938 if (param != CTL_HA_STATUS_SUCCESS) { 939 printf("Bad status from ctl_ha_msg_send status %d\n", 940 param); 941 } 942 return; 943 } else if (event == CTL_HA_EVT_DISCONNECT) { 944 printf("CTL: Got a disconnect from Isc\n"); 945 return; 946 } else { 947 printf("ctl_isc_event_handler: Unknown event %d\n", event); 948 return; 949 } 950 951bailout: 952 return; 953} 954 955static void 956ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 957{ 958 struct scsi_sense_data *sense; 959 960 sense = &dest->scsiio.sense_data; 961 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 962 dest->scsiio.scsi_status = src->scsi.scsi_status; 963 dest->scsiio.sense_len = src->scsi.sense_len; 964 dest->io_hdr.status = src->hdr.status; 965} 966 967static void 968ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 969{ 970 ctl_ua_type *pu; 971 972 mtx_assert(&lun->lun_lock, MA_OWNED); 973 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 974 if (pu == NULL) 975 return; 976 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 977} 978 979static void 980ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 981{ 982 int i, j; 983 984 mtx_assert(&lun->lun_lock, MA_OWNED); 985 for (i = 0; i < CTL_MAX_PORTS; i++) { 986 if (lun->pending_ua[i] == NULL) 987 continue; 988 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 989 if (i * CTL_MAX_INIT_PER_PORT + j == except) 990 continue; 991 lun->pending_ua[i][j] |= ua; 992 } 993 } 994} 995 996static void 997ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 998{ 999 ctl_ua_type *pu; 1000 1001 mtx_assert(&lun->lun_lock, MA_OWNED); 1002 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1003 if (pu == NULL) 1004 return; 1005 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1006} 1007 1008static void 1009ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1010{ 1011 int i, j; 1012 1013 mtx_assert(&lun->lun_lock, MA_OWNED); 1014 for (i = 0; i < CTL_MAX_PORTS; i++) { 1015 if (lun->pending_ua[i] == NULL) 1016 continue; 1017 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1018 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1019 continue; 1020 lun->pending_ua[i][j] &= ~ua; 1021 } 1022 } 1023} 1024 1025static int 1026ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 1027{ 1028 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1029 struct ctl_lun *lun; 1030 int error, value; 1031 1032 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 1033 value = 0; 1034 else 1035 value = 1; 1036 1037 error = sysctl_handle_int(oidp, &value, 0, req); 1038 if ((error != 0) || (req->newptr == NULL)) 1039 return (error); 1040 1041 mtx_lock(&softc->ctl_lock); 1042 if (value == 0) 1043 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1044 else 1045 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1046 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1047 mtx_lock(&lun->lun_lock); 1048 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1049 mtx_unlock(&lun->lun_lock); 1050 } 1051 mtx_unlock(&softc->ctl_lock); 1052 return (0); 1053} 1054 1055static int 1056ctl_init(void) 1057{ 1058 struct ctl_softc *softc; 1059 void *other_pool; 1060 struct ctl_port *port; 1061 int i, error, retval; 1062 //int isc_retval; 1063 1064 retval = 0; 1065 ctl_pause_rtr = 0; 1066 rcv_sync_msg = 0; 1067 1068 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1069 M_WAITOK | M_ZERO); 1070 softc = control_softc; 1071 1072 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1073 "cam/ctl"); 1074 1075 softc->dev->si_drv1 = softc; 1076 1077 /* 1078 * By default, return a "bad LUN" peripheral qualifier for unknown 1079 * LUNs. The user can override this default using the tunable or 1080 * sysctl. See the comment in ctl_inquiry_std() for more details. 1081 */ 1082 softc->inquiry_pq_no_lun = 1; 1083 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1084 &softc->inquiry_pq_no_lun); 1085 sysctl_ctx_init(&softc->sysctl_ctx); 1086 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1087 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1088 CTLFLAG_RD, 0, "CAM Target Layer"); 1089 1090 if (softc->sysctl_tree == NULL) { 1091 printf("%s: unable to allocate sysctl tree\n", __func__); 1092 destroy_dev(softc->dev); 1093 free(control_softc, M_DEVBUF); 1094 control_softc = NULL; 1095 return (ENOMEM); 1096 } 1097 1098 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1099 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1100 "inquiry_pq_no_lun", CTLFLAG_RW, 1101 &softc->inquiry_pq_no_lun, 0, 1102 "Report no lun possible for invalid LUNs"); 1103 1104 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1105 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1106 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1107 softc->open_count = 0; 1108 1109 /* 1110 * Default to actually sending a SYNCHRONIZE CACHE command down to 1111 * the drive. 1112 */ 1113 softc->flags = CTL_FLAG_REAL_SYNC; 1114 1115 /* 1116 * In Copan's HA scheme, the "master" and "slave" roles are 1117 * figured out through the slot the controller is in. Although it 1118 * is an active/active system, someone has to be in charge. 1119 */ 1120 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1121 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1122 "HA head ID (0 - no HA)"); 1123 if (softc->ha_id == 0) { 1124 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1125 softc->is_single = 1; 1126 softc->port_offset = 0; 1127 } else 1128 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1129 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1130 1131 /* 1132 * XXX KDM need to figure out where we want to get our target ID 1133 * and WWID. Is it different on each port? 1134 */ 1135 softc->target.id = 0; 1136 softc->target.wwid[0] = 0x12345678; 1137 softc->target.wwid[1] = 0x87654321; 1138 STAILQ_INIT(&softc->lun_list); 1139 STAILQ_INIT(&softc->pending_lun_queue); 1140 STAILQ_INIT(&softc->fe_list); 1141 STAILQ_INIT(&softc->port_list); 1142 STAILQ_INIT(&softc->be_list); 1143 ctl_tpc_init(softc); 1144 1145 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1146 &other_pool) != 0) 1147 { 1148 printf("ctl: can't allocate %d entry other SC pool, " 1149 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1150 return (ENOMEM); 1151 } 1152 softc->othersc_pool = other_pool; 1153 1154 if (worker_threads <= 0) 1155 worker_threads = max(1, mp_ncpus / 4); 1156 if (worker_threads > CTL_MAX_THREADS) 1157 worker_threads = CTL_MAX_THREADS; 1158 1159 for (i = 0; i < worker_threads; i++) { 1160 struct ctl_thread *thr = &softc->threads[i]; 1161 1162 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1163 thr->ctl_softc = softc; 1164 STAILQ_INIT(&thr->incoming_queue); 1165 STAILQ_INIT(&thr->rtr_queue); 1166 STAILQ_INIT(&thr->done_queue); 1167 STAILQ_INIT(&thr->isc_queue); 1168 1169 error = kproc_kthread_add(ctl_work_thread, thr, 1170 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1171 if (error != 0) { 1172 printf("error creating CTL work thread!\n"); 1173 ctl_pool_free(other_pool); 1174 return (error); 1175 } 1176 } 1177 error = kproc_kthread_add(ctl_lun_thread, softc, 1178 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1179 if (error != 0) { 1180 printf("error creating CTL lun thread!\n"); 1181 ctl_pool_free(other_pool); 1182 return (error); 1183 } 1184 error = kproc_kthread_add(ctl_thresh_thread, softc, 1185 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1186 if (error != 0) { 1187 printf("error creating CTL threshold thread!\n"); 1188 ctl_pool_free(other_pool); 1189 return (error); 1190 } 1191 if (bootverbose) 1192 printf("ctl: CAM Target Layer loaded\n"); 1193 1194 /* 1195 * Initialize the ioctl front end. 1196 */ 1197 ctl_frontend_register(&ioctl_frontend); 1198 port = &softc->ioctl_info.port; 1199 port->frontend = &ioctl_frontend; 1200 sprintf(softc->ioctl_info.port_name, "ioctl"); 1201 port->port_type = CTL_PORT_IOCTL; 1202 port->num_requested_ctl_io = 100; 1203 port->port_name = softc->ioctl_info.port_name; 1204 port->port_online = ctl_ioctl_online; 1205 port->port_offline = ctl_ioctl_offline; 1206 port->onoff_arg = &softc->ioctl_info; 1207 port->lun_enable = ctl_ioctl_lun_enable; 1208 port->lun_disable = ctl_ioctl_lun_disable; 1209 port->targ_lun_arg = &softc->ioctl_info; 1210 port->fe_datamove = ctl_ioctl_datamove; 1211 port->fe_done = ctl_ioctl_done; 1212 port->max_targets = 15; 1213 port->max_target_id = 15; 1214 1215 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1216 printf("ctl: ioctl front end registration failed, will " 1217 "continue anyway\n"); 1218 } 1219 1220 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1221 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1222 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1223 1224#ifdef CTL_IO_DELAY 1225 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1226 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1227 sizeof(struct callout), CTL_TIMER_BYTES); 1228 return (EINVAL); 1229 } 1230#endif /* CTL_IO_DELAY */ 1231 1232 return (0); 1233} 1234 1235void 1236ctl_shutdown(void) 1237{ 1238 struct ctl_softc *softc; 1239 struct ctl_lun *lun, *next_lun; 1240 1241 softc = (struct ctl_softc *)control_softc; 1242 1243 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1244 printf("ctl: ioctl front end deregistration failed\n"); 1245 1246 mtx_lock(&softc->ctl_lock); 1247 1248 /* 1249 * Free up each LUN. 1250 */ 1251 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1252 next_lun = STAILQ_NEXT(lun, links); 1253 ctl_free_lun(lun); 1254 } 1255 1256 mtx_unlock(&softc->ctl_lock); 1257 1258 ctl_frontend_deregister(&ioctl_frontend); 1259 1260#if 0 1261 ctl_shutdown_thread(softc->work_thread); 1262 mtx_destroy(&softc->queue_lock); 1263#endif 1264 1265 ctl_tpc_shutdown(softc); 1266 uma_zdestroy(softc->io_zone); 1267 mtx_destroy(&softc->ctl_lock); 1268 1269 destroy_dev(softc->dev); 1270 1271 sysctl_ctx_free(&softc->sysctl_ctx); 1272 1273 free(control_softc, M_DEVBUF); 1274 control_softc = NULL; 1275 1276 if (bootverbose) 1277 printf("ctl: CAM Target Layer unloaded\n"); 1278} 1279 1280static int 1281ctl_module_event_handler(module_t mod, int what, void *arg) 1282{ 1283 1284 switch (what) { 1285 case MOD_LOAD: 1286 return (ctl_init()); 1287 case MOD_UNLOAD: 1288 return (EBUSY); 1289 default: 1290 return (EOPNOTSUPP); 1291 } 1292} 1293 1294/* 1295 * XXX KDM should we do some access checks here? Bump a reference count to 1296 * prevent a CTL module from being unloaded while someone has it open? 1297 */ 1298static int 1299ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1300{ 1301 return (0); 1302} 1303 1304static int 1305ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1306{ 1307 return (0); 1308} 1309 1310int 1311ctl_port_enable(ctl_port_type port_type) 1312{ 1313 struct ctl_softc *softc = control_softc; 1314 struct ctl_port *port; 1315 1316 if (softc->is_single == 0) { 1317 union ctl_ha_msg msg_info; 1318 int isc_retval; 1319 1320#if 0 1321 printf("%s: HA mode, synchronizing frontend enable\n", 1322 __func__); 1323#endif 1324 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1325 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1326 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1327 printf("Sync msg send error retval %d\n", isc_retval); 1328 } 1329 if (!rcv_sync_msg) { 1330 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1331 sizeof(msg_info), 1); 1332 } 1333#if 0 1334 printf("CTL:Frontend Enable\n"); 1335 } else { 1336 printf("%s: single mode, skipping frontend synchronization\n", 1337 __func__); 1338#endif 1339 } 1340 1341 STAILQ_FOREACH(port, &softc->port_list, links) { 1342 if (port_type & port->port_type) 1343 { 1344#if 0 1345 printf("port %d\n", port->targ_port); 1346#endif 1347 ctl_port_online(port); 1348 } 1349 } 1350 1351 return (0); 1352} 1353 1354int 1355ctl_port_disable(ctl_port_type port_type) 1356{ 1357 struct ctl_softc *softc; 1358 struct ctl_port *port; 1359 1360 softc = control_softc; 1361 1362 STAILQ_FOREACH(port, &softc->port_list, links) { 1363 if (port_type & port->port_type) 1364 ctl_port_offline(port); 1365 } 1366 1367 return (0); 1368} 1369 1370/* 1371 * Returns 0 for success, 1 for failure. 1372 * Currently the only failure mode is if there aren't enough entries 1373 * allocated. So, in case of a failure, look at num_entries_dropped, 1374 * reallocate and try again. 1375 */ 1376int 1377ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1378 int *num_entries_filled, int *num_entries_dropped, 1379 ctl_port_type port_type, int no_virtual) 1380{ 1381 struct ctl_softc *softc; 1382 struct ctl_port *port; 1383 int entries_dropped, entries_filled; 1384 int retval; 1385 int i; 1386 1387 softc = control_softc; 1388 1389 retval = 0; 1390 entries_filled = 0; 1391 entries_dropped = 0; 1392 1393 i = 0; 1394 mtx_lock(&softc->ctl_lock); 1395 STAILQ_FOREACH(port, &softc->port_list, links) { 1396 struct ctl_port_entry *entry; 1397 1398 if ((port->port_type & port_type) == 0) 1399 continue; 1400 1401 if ((no_virtual != 0) 1402 && (port->virtual_port != 0)) 1403 continue; 1404 1405 if (entries_filled >= num_entries_alloced) { 1406 entries_dropped++; 1407 continue; 1408 } 1409 entry = &entries[i]; 1410 1411 entry->port_type = port->port_type; 1412 strlcpy(entry->port_name, port->port_name, 1413 sizeof(entry->port_name)); 1414 entry->physical_port = port->physical_port; 1415 entry->virtual_port = port->virtual_port; 1416 entry->wwnn = port->wwnn; 1417 entry->wwpn = port->wwpn; 1418 1419 i++; 1420 entries_filled++; 1421 } 1422 1423 mtx_unlock(&softc->ctl_lock); 1424 1425 if (entries_dropped > 0) 1426 retval = 1; 1427 1428 *num_entries_dropped = entries_dropped; 1429 *num_entries_filled = entries_filled; 1430 1431 return (retval); 1432} 1433 1434static void 1435ctl_ioctl_online(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 1444static void 1445ctl_ioctl_offline(void *arg) 1446{ 1447 struct ctl_ioctl_info *ioctl_info; 1448 1449 ioctl_info = (struct ctl_ioctl_info *)arg; 1450 1451 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1452} 1453 1454/* 1455 * Remove an initiator by port number and initiator ID. 1456 * Returns 0 for success, -1 for failure. 1457 */ 1458int 1459ctl_remove_initiator(struct ctl_port *port, int iid) 1460{ 1461 struct ctl_softc *softc = control_softc; 1462 1463 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1464 1465 if (iid > CTL_MAX_INIT_PER_PORT) { 1466 printf("%s: initiator ID %u > maximun %u!\n", 1467 __func__, iid, CTL_MAX_INIT_PER_PORT); 1468 return (-1); 1469 } 1470 1471 mtx_lock(&softc->ctl_lock); 1472 port->wwpn_iid[iid].in_use--; 1473 port->wwpn_iid[iid].last_use = time_uptime; 1474 mtx_unlock(&softc->ctl_lock); 1475 1476 return (0); 1477} 1478 1479/* 1480 * Add an initiator to the initiator map. 1481 * Returns iid for success, < 0 for failure. 1482 */ 1483int 1484ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1485{ 1486 struct ctl_softc *softc = control_softc; 1487 time_t best_time; 1488 int i, best; 1489 1490 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1491 1492 if (iid >= CTL_MAX_INIT_PER_PORT) { 1493 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1494 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1495 free(name, M_CTL); 1496 return (-1); 1497 } 1498 1499 mtx_lock(&softc->ctl_lock); 1500 1501 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1502 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1503 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1504 iid = i; 1505 break; 1506 } 1507 if (name != NULL && port->wwpn_iid[i].name != NULL && 1508 strcmp(name, port->wwpn_iid[i].name) == 0) { 1509 iid = i; 1510 break; 1511 } 1512 } 1513 } 1514 1515 if (iid < 0) { 1516 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1517 if (port->wwpn_iid[i].in_use == 0 && 1518 port->wwpn_iid[i].wwpn == 0 && 1519 port->wwpn_iid[i].name == NULL) { 1520 iid = i; 1521 break; 1522 } 1523 } 1524 } 1525 1526 if (iid < 0) { 1527 best = -1; 1528 best_time = INT32_MAX; 1529 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1530 if (port->wwpn_iid[i].in_use == 0) { 1531 if (port->wwpn_iid[i].last_use < best_time) { 1532 best = i; 1533 best_time = port->wwpn_iid[i].last_use; 1534 } 1535 } 1536 } 1537 iid = best; 1538 } 1539 1540 if (iid < 0) { 1541 mtx_unlock(&softc->ctl_lock); 1542 free(name, M_CTL); 1543 return (-2); 1544 } 1545 1546 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1547 /* 1548 * This is not an error yet. 1549 */ 1550 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1551#if 0 1552 printf("%s: port %d iid %u WWPN %#jx arrived" 1553 " again\n", __func__, port->targ_port, 1554 iid, (uintmax_t)wwpn); 1555#endif 1556 goto take; 1557 } 1558 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1559 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1560#if 0 1561 printf("%s: port %d iid %u name '%s' arrived" 1562 " again\n", __func__, port->targ_port, 1563 iid, name); 1564#endif 1565 goto take; 1566 } 1567 1568 /* 1569 * This is an error, but what do we do about it? The 1570 * driver is telling us we have a new WWPN for this 1571 * initiator ID, so we pretty much need to use it. 1572 */ 1573 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1574 " but WWPN %#jx '%s' is still at that address\n", 1575 __func__, port->targ_port, iid, wwpn, name, 1576 (uintmax_t)port->wwpn_iid[iid].wwpn, 1577 port->wwpn_iid[iid].name); 1578 1579 /* 1580 * XXX KDM clear have_ca and ua_pending on each LUN for 1581 * this initiator. 1582 */ 1583 } 1584take: 1585 free(port->wwpn_iid[iid].name, M_CTL); 1586 port->wwpn_iid[iid].name = name; 1587 port->wwpn_iid[iid].wwpn = wwpn; 1588 port->wwpn_iid[iid].in_use++; 1589 mtx_unlock(&softc->ctl_lock); 1590 1591 return (iid); 1592} 1593 1594static int 1595ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1596{ 1597 int len; 1598 1599 switch (port->port_type) { 1600 case CTL_PORT_FC: 1601 { 1602 struct scsi_transportid_fcp *id = 1603 (struct scsi_transportid_fcp *)buf; 1604 if (port->wwpn_iid[iid].wwpn == 0) 1605 return (0); 1606 memset(id, 0, sizeof(*id)); 1607 id->format_protocol = SCSI_PROTO_FC; 1608 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1609 return (sizeof(*id)); 1610 } 1611 case CTL_PORT_ISCSI: 1612 { 1613 struct scsi_transportid_iscsi_port *id = 1614 (struct scsi_transportid_iscsi_port *)buf; 1615 if (port->wwpn_iid[iid].name == NULL) 1616 return (0); 1617 memset(id, 0, 256); 1618 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1619 SCSI_PROTO_ISCSI; 1620 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1621 len = roundup2(min(len, 252), 4); 1622 scsi_ulto2b(len, id->additional_length); 1623 return (sizeof(*id) + len); 1624 } 1625 case CTL_PORT_SAS: 1626 { 1627 struct scsi_transportid_sas *id = 1628 (struct scsi_transportid_sas *)buf; 1629 if (port->wwpn_iid[iid].wwpn == 0) 1630 return (0); 1631 memset(id, 0, sizeof(*id)); 1632 id->format_protocol = SCSI_PROTO_SAS; 1633 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1634 return (sizeof(*id)); 1635 } 1636 default: 1637 { 1638 struct scsi_transportid_spi *id = 1639 (struct scsi_transportid_spi *)buf; 1640 memset(id, 0, sizeof(*id)); 1641 id->format_protocol = SCSI_PROTO_SPI; 1642 scsi_ulto2b(iid, id->scsi_addr); 1643 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1644 return (sizeof(*id)); 1645 } 1646 } 1647} 1648 1649static int 1650ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1651{ 1652 return (0); 1653} 1654 1655static int 1656ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1657{ 1658 return (0); 1659} 1660 1661/* 1662 * Data movement routine for the CTL ioctl frontend port. 1663 */ 1664static int 1665ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1666{ 1667 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1668 struct ctl_sg_entry ext_entry, kern_entry; 1669 int ext_sglen, ext_sg_entries, kern_sg_entries; 1670 int ext_sg_start, ext_offset; 1671 int len_to_copy, len_copied; 1672 int kern_watermark, ext_watermark; 1673 int ext_sglist_malloced; 1674 int i, j; 1675 1676 ext_sglist_malloced = 0; 1677 ext_sg_start = 0; 1678 ext_offset = 0; 1679 1680 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1681 1682 /* 1683 * If this flag is set, fake the data transfer. 1684 */ 1685 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1686 ctsio->ext_data_filled = ctsio->ext_data_len; 1687 goto bailout; 1688 } 1689 1690 /* 1691 * To simplify things here, if we have a single buffer, stick it in 1692 * a S/G entry and just make it a single entry S/G list. 1693 */ 1694 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1695 int len_seen; 1696 1697 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1698 1699 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1700 M_WAITOK); 1701 ext_sglist_malloced = 1; 1702 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1703 ext_sglen) != 0) { 1704 ctl_set_internal_failure(ctsio, 1705 /*sks_valid*/ 0, 1706 /*retry_count*/ 0); 1707 goto bailout; 1708 } 1709 ext_sg_entries = ctsio->ext_sg_entries; 1710 len_seen = 0; 1711 for (i = 0; i < ext_sg_entries; i++) { 1712 if ((len_seen + ext_sglist[i].len) >= 1713 ctsio->ext_data_filled) { 1714 ext_sg_start = i; 1715 ext_offset = ctsio->ext_data_filled - len_seen; 1716 break; 1717 } 1718 len_seen += ext_sglist[i].len; 1719 } 1720 } else { 1721 ext_sglist = &ext_entry; 1722 ext_sglist->addr = ctsio->ext_data_ptr; 1723 ext_sglist->len = ctsio->ext_data_len; 1724 ext_sg_entries = 1; 1725 ext_sg_start = 0; 1726 ext_offset = ctsio->ext_data_filled; 1727 } 1728 1729 if (ctsio->kern_sg_entries > 0) { 1730 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1731 kern_sg_entries = ctsio->kern_sg_entries; 1732 } else { 1733 kern_sglist = &kern_entry; 1734 kern_sglist->addr = ctsio->kern_data_ptr; 1735 kern_sglist->len = ctsio->kern_data_len; 1736 kern_sg_entries = 1; 1737 } 1738 1739 1740 kern_watermark = 0; 1741 ext_watermark = ext_offset; 1742 len_copied = 0; 1743 for (i = ext_sg_start, j = 0; 1744 i < ext_sg_entries && j < kern_sg_entries;) { 1745 uint8_t *ext_ptr, *kern_ptr; 1746 1747 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1748 kern_sglist[j].len - kern_watermark); 1749 1750 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1751 ext_ptr = ext_ptr + ext_watermark; 1752 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1753 /* 1754 * XXX KDM fix this! 1755 */ 1756 panic("need to implement bus address support"); 1757#if 0 1758 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1759#endif 1760 } else 1761 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1762 kern_ptr = kern_ptr + kern_watermark; 1763 1764 kern_watermark += len_to_copy; 1765 ext_watermark += len_to_copy; 1766 1767 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1768 CTL_FLAG_DATA_IN) { 1769 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1770 "bytes to user\n", len_to_copy)); 1771 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1772 "to %p\n", kern_ptr, ext_ptr)); 1773 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1774 ctl_set_internal_failure(ctsio, 1775 /*sks_valid*/ 0, 1776 /*retry_count*/ 0); 1777 goto bailout; 1778 } 1779 } else { 1780 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1781 "bytes from user\n", len_to_copy)); 1782 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1783 "to %p\n", ext_ptr, kern_ptr)); 1784 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1785 ctl_set_internal_failure(ctsio, 1786 /*sks_valid*/ 0, 1787 /*retry_count*/0); 1788 goto bailout; 1789 } 1790 } 1791 1792 len_copied += len_to_copy; 1793 1794 if (ext_sglist[i].len == ext_watermark) { 1795 i++; 1796 ext_watermark = 0; 1797 } 1798 1799 if (kern_sglist[j].len == kern_watermark) { 1800 j++; 1801 kern_watermark = 0; 1802 } 1803 } 1804 1805 ctsio->ext_data_filled += len_copied; 1806 1807 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1808 "kern_sg_entries: %d\n", ext_sg_entries, 1809 kern_sg_entries)); 1810 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1811 "kern_data_len = %d\n", ctsio->ext_data_len, 1812 ctsio->kern_data_len)); 1813 1814 1815 /* XXX KDM set residual?? */ 1816bailout: 1817 1818 if (ext_sglist_malloced != 0) 1819 free(ext_sglist, M_CTL); 1820 1821 return (CTL_RETVAL_COMPLETE); 1822} 1823 1824/* 1825 * Serialize a command that went down the "wrong" side, and so was sent to 1826 * this controller for execution. The logic is a little different than the 1827 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1828 * sent back to the other side, but in the success case, we execute the 1829 * command on this side (XFER mode) or tell the other side to execute it 1830 * (SER_ONLY mode). 1831 */ 1832static int 1833ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1834{ 1835 struct ctl_softc *ctl_softc; 1836 union ctl_ha_msg msg_info; 1837 struct ctl_lun *lun; 1838 int retval = 0; 1839 uint32_t targ_lun; 1840 1841 ctl_softc = control_softc; 1842 1843 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1844 lun = ctl_softc->ctl_luns[targ_lun]; 1845 if (lun==NULL) 1846 { 1847 /* 1848 * Why isn't LUN defined? The other side wouldn't 1849 * send a cmd if the LUN is undefined. 1850 */ 1851 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1852 1853 /* "Logical unit not supported" */ 1854 ctl_set_sense_data(&msg_info.scsi.sense_data, 1855 lun, 1856 /*sense_format*/SSD_TYPE_NONE, 1857 /*current_error*/ 1, 1858 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1859 /*asc*/ 0x25, 1860 /*ascq*/ 0x00, 1861 SSD_ELEM_NONE); 1862 1863 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1864 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1865 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1866 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1867 msg_info.hdr.serializing_sc = NULL; 1868 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1869 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1870 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1871 } 1872 return(1); 1873 1874 } 1875 1876 mtx_lock(&lun->lun_lock); 1877 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1878 1879 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1880 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1881 ooa_links))) { 1882 case CTL_ACTION_BLOCK: 1883 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1884 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1885 blocked_links); 1886 break; 1887 case CTL_ACTION_PASS: 1888 case CTL_ACTION_SKIP: 1889 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1890 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1891 ctl_enqueue_rtr((union ctl_io *)ctsio); 1892 } else { 1893 1894 /* send msg back to other side */ 1895 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1896 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1897 msg_info.hdr.msg_type = CTL_MSG_R2R; 1898#if 0 1899 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1900#endif 1901 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1902 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1903 } 1904 } 1905 break; 1906 case CTL_ACTION_OVERLAP: 1907 /* OVERLAPPED COMMANDS ATTEMPTED */ 1908 ctl_set_sense_data(&msg_info.scsi.sense_data, 1909 lun, 1910 /*sense_format*/SSD_TYPE_NONE, 1911 /*current_error*/ 1, 1912 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1913 /*asc*/ 0x4E, 1914 /*ascq*/ 0x00, 1915 SSD_ELEM_NONE); 1916 1917 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1918 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1919 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1920 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1921 msg_info.hdr.serializing_sc = NULL; 1922 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1923#if 0 1924 printf("BAD JUJU:Major Bummer Overlap\n"); 1925#endif 1926 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1927 retval = 1; 1928 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1929 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1930 } 1931 break; 1932 case CTL_ACTION_OVERLAP_TAG: 1933 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1934 ctl_set_sense_data(&msg_info.scsi.sense_data, 1935 lun, 1936 /*sense_format*/SSD_TYPE_NONE, 1937 /*current_error*/ 1, 1938 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1939 /*asc*/ 0x4D, 1940 /*ascq*/ ctsio->tag_num & 0xff, 1941 SSD_ELEM_NONE); 1942 1943 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1944 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1945 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1946 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1947 msg_info.hdr.serializing_sc = NULL; 1948 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1949#if 0 1950 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1951#endif 1952 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1953 retval = 1; 1954 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1955 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1956 } 1957 break; 1958 case CTL_ACTION_ERROR: 1959 default: 1960 /* "Internal target failure" */ 1961 ctl_set_sense_data(&msg_info.scsi.sense_data, 1962 lun, 1963 /*sense_format*/SSD_TYPE_NONE, 1964 /*current_error*/ 1, 1965 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1966 /*asc*/ 0x44, 1967 /*ascq*/ 0x00, 1968 SSD_ELEM_NONE); 1969 1970 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1971 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1972 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1973 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1974 msg_info.hdr.serializing_sc = NULL; 1975 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1976#if 0 1977 printf("BAD JUJU:Major Bummer HW Error\n"); 1978#endif 1979 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1980 retval = 1; 1981 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1982 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1983 } 1984 break; 1985 } 1986 mtx_unlock(&lun->lun_lock); 1987 return (retval); 1988} 1989 1990static int 1991ctl_ioctl_submit_wait(union ctl_io *io) 1992{ 1993 struct ctl_fe_ioctl_params params; 1994 ctl_fe_ioctl_state last_state; 1995 int done, retval; 1996 1997 retval = 0; 1998 1999 bzero(¶ms, sizeof(params)); 2000 2001 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 2002 cv_init(¶ms.sem, "ctlioccv"); 2003 params.state = CTL_IOCTL_INPROG; 2004 last_state = params.state; 2005 2006 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 2007 2008 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 2009 2010 /* This shouldn't happen */ 2011 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 2012 return (retval); 2013 2014 done = 0; 2015 2016 do { 2017 mtx_lock(¶ms.ioctl_mtx); 2018 /* 2019 * Check the state here, and don't sleep if the state has 2020 * already changed (i.e. wakeup has already occured, but we 2021 * weren't waiting yet). 2022 */ 2023 if (params.state == last_state) { 2024 /* XXX KDM cv_wait_sig instead? */ 2025 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 2026 } 2027 last_state = params.state; 2028 2029 switch (params.state) { 2030 case CTL_IOCTL_INPROG: 2031 /* Why did we wake up? */ 2032 /* XXX KDM error here? */ 2033 mtx_unlock(¶ms.ioctl_mtx); 2034 break; 2035 case CTL_IOCTL_DATAMOVE: 2036 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 2037 2038 /* 2039 * change last_state back to INPROG to avoid 2040 * deadlock on subsequent data moves. 2041 */ 2042 params.state = last_state = CTL_IOCTL_INPROG; 2043 2044 mtx_unlock(¶ms.ioctl_mtx); 2045 ctl_ioctl_do_datamove(&io->scsiio); 2046 /* 2047 * Note that in some cases, most notably writes, 2048 * this will queue the I/O and call us back later. 2049 * In other cases, generally reads, this routine 2050 * will immediately call back and wake us up, 2051 * probably using our own context. 2052 */ 2053 io->scsiio.be_move_done(io); 2054 break; 2055 case CTL_IOCTL_DONE: 2056 mtx_unlock(¶ms.ioctl_mtx); 2057 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2058 done = 1; 2059 break; 2060 default: 2061 mtx_unlock(¶ms.ioctl_mtx); 2062 /* XXX KDM error here? */ 2063 break; 2064 } 2065 } while (done == 0); 2066 2067 mtx_destroy(¶ms.ioctl_mtx); 2068 cv_destroy(¶ms.sem); 2069 2070 return (CTL_RETVAL_COMPLETE); 2071} 2072 2073static void 2074ctl_ioctl_datamove(union ctl_io *io) 2075{ 2076 struct ctl_fe_ioctl_params *params; 2077 2078 params = (struct ctl_fe_ioctl_params *) 2079 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2080 2081 mtx_lock(¶ms->ioctl_mtx); 2082 params->state = CTL_IOCTL_DATAMOVE; 2083 cv_broadcast(¶ms->sem); 2084 mtx_unlock(¶ms->ioctl_mtx); 2085} 2086 2087static void 2088ctl_ioctl_done(union ctl_io *io) 2089{ 2090 struct ctl_fe_ioctl_params *params; 2091 2092 params = (struct ctl_fe_ioctl_params *) 2093 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2094 2095 mtx_lock(¶ms->ioctl_mtx); 2096 params->state = CTL_IOCTL_DONE; 2097 cv_broadcast(¶ms->sem); 2098 mtx_unlock(¶ms->ioctl_mtx); 2099} 2100 2101static void 2102ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2103{ 2104 struct ctl_fe_ioctl_startstop_info *sd_info; 2105 2106 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2107 2108 sd_info->hs_info.status = metatask->status; 2109 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2110 sd_info->hs_info.luns_complete = 2111 metatask->taskinfo.startstop.luns_complete; 2112 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2113 2114 cv_broadcast(&sd_info->sem); 2115} 2116 2117static void 2118ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2119{ 2120 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2121 2122 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2123 2124 mtx_lock(fe_bbr_info->lock); 2125 fe_bbr_info->bbr_info->status = metatask->status; 2126 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2127 fe_bbr_info->wakeup_done = 1; 2128 mtx_unlock(fe_bbr_info->lock); 2129 2130 cv_broadcast(&fe_bbr_info->sem); 2131} 2132 2133/* 2134 * Returns 0 for success, errno for failure. 2135 */ 2136static int 2137ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2138 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2139{ 2140 union ctl_io *io; 2141 int retval; 2142 2143 retval = 0; 2144 2145 mtx_lock(&lun->lun_lock); 2146 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2147 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2148 ooa_links)) { 2149 struct ctl_ooa_entry *entry; 2150 2151 /* 2152 * If we've got more than we can fit, just count the 2153 * remaining entries. 2154 */ 2155 if (*cur_fill_num >= ooa_hdr->alloc_num) 2156 continue; 2157 2158 entry = &kern_entries[*cur_fill_num]; 2159 2160 entry->tag_num = io->scsiio.tag_num; 2161 entry->lun_num = lun->lun; 2162#ifdef CTL_TIME_IO 2163 entry->start_bt = io->io_hdr.start_bt; 2164#endif 2165 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2166 entry->cdb_len = io->scsiio.cdb_len; 2167 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2168 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2169 2170 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2171 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2172 2173 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2174 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2175 2176 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2177 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2178 2179 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2180 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2181 } 2182 mtx_unlock(&lun->lun_lock); 2183 2184 return (retval); 2185} 2186 2187static void * 2188ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2189 size_t error_str_len) 2190{ 2191 void *kptr; 2192 2193 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2194 2195 if (copyin(user_addr, kptr, len) != 0) { 2196 snprintf(error_str, error_str_len, "Error copying %d bytes " 2197 "from user address %p to kernel address %p", len, 2198 user_addr, kptr); 2199 free(kptr, M_CTL); 2200 return (NULL); 2201 } 2202 2203 return (kptr); 2204} 2205 2206static void 2207ctl_free_args(int num_args, struct ctl_be_arg *args) 2208{ 2209 int i; 2210 2211 if (args == NULL) 2212 return; 2213 2214 for (i = 0; i < num_args; i++) { 2215 free(args[i].kname, M_CTL); 2216 free(args[i].kvalue, M_CTL); 2217 } 2218 2219 free(args, M_CTL); 2220} 2221 2222static struct ctl_be_arg * 2223ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2224 char *error_str, size_t error_str_len) 2225{ 2226 struct ctl_be_arg *args; 2227 int i; 2228 2229 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2230 error_str, error_str_len); 2231 2232 if (args == NULL) 2233 goto bailout; 2234 2235 for (i = 0; i < num_args; i++) { 2236 args[i].kname = NULL; 2237 args[i].kvalue = NULL; 2238 } 2239 2240 for (i = 0; i < num_args; i++) { 2241 uint8_t *tmpptr; 2242 2243 args[i].kname = ctl_copyin_alloc(args[i].name, 2244 args[i].namelen, error_str, error_str_len); 2245 if (args[i].kname == NULL) 2246 goto bailout; 2247 2248 if (args[i].kname[args[i].namelen - 1] != '\0') { 2249 snprintf(error_str, error_str_len, "Argument %d " 2250 "name is not NUL-terminated", i); 2251 goto bailout; 2252 } 2253 2254 if (args[i].flags & CTL_BEARG_RD) { 2255 tmpptr = ctl_copyin_alloc(args[i].value, 2256 args[i].vallen, error_str, error_str_len); 2257 if (tmpptr == NULL) 2258 goto bailout; 2259 if ((args[i].flags & CTL_BEARG_ASCII) 2260 && (tmpptr[args[i].vallen - 1] != '\0')) { 2261 snprintf(error_str, error_str_len, "Argument " 2262 "%d value is not NUL-terminated", i); 2263 goto bailout; 2264 } 2265 args[i].kvalue = tmpptr; 2266 } else { 2267 args[i].kvalue = malloc(args[i].vallen, 2268 M_CTL, M_WAITOK | M_ZERO); 2269 } 2270 } 2271 2272 return (args); 2273bailout: 2274 2275 ctl_free_args(num_args, args); 2276 2277 return (NULL); 2278} 2279 2280static void 2281ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2282{ 2283 int i; 2284 2285 for (i = 0; i < num_args; i++) { 2286 if (args[i].flags & CTL_BEARG_WR) 2287 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2288 } 2289} 2290 2291/* 2292 * Escape characters that are illegal or not recommended in XML. 2293 */ 2294int 2295ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2296{ 2297 char *end = str + size; 2298 int retval; 2299 2300 retval = 0; 2301 2302 for (; *str && str < end; str++) { 2303 switch (*str) { 2304 case '&': 2305 retval = sbuf_printf(sb, "&"); 2306 break; 2307 case '>': 2308 retval = sbuf_printf(sb, ">"); 2309 break; 2310 case '<': 2311 retval = sbuf_printf(sb, "<"); 2312 break; 2313 default: 2314 retval = sbuf_putc(sb, *str); 2315 break; 2316 } 2317 2318 if (retval != 0) 2319 break; 2320 2321 } 2322 2323 return (retval); 2324} 2325 2326static void 2327ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2328{ 2329 struct scsi_vpd_id_descriptor *desc; 2330 int i; 2331 2332 if (id == NULL || id->len < 4) 2333 return; 2334 desc = (struct scsi_vpd_id_descriptor *)id->data; 2335 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2336 case SVPD_ID_TYPE_T10: 2337 sbuf_printf(sb, "t10."); 2338 break; 2339 case SVPD_ID_TYPE_EUI64: 2340 sbuf_printf(sb, "eui."); 2341 break; 2342 case SVPD_ID_TYPE_NAA: 2343 sbuf_printf(sb, "naa."); 2344 break; 2345 case SVPD_ID_TYPE_SCSI_NAME: 2346 break; 2347 } 2348 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2349 case SVPD_ID_CODESET_BINARY: 2350 for (i = 0; i < desc->length; i++) 2351 sbuf_printf(sb, "%02x", desc->identifier[i]); 2352 break; 2353 case SVPD_ID_CODESET_ASCII: 2354 sbuf_printf(sb, "%.*s", (int)desc->length, 2355 (char *)desc->identifier); 2356 break; 2357 case SVPD_ID_CODESET_UTF8: 2358 sbuf_printf(sb, "%s", (char *)desc->identifier); 2359 break; 2360 } 2361} 2362 2363static int 2364ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2365 struct thread *td) 2366{ 2367 struct ctl_softc *softc; 2368 int retval; 2369 2370 softc = control_softc; 2371 2372 retval = 0; 2373 2374 switch (cmd) { 2375 case CTL_IO: { 2376 union ctl_io *io; 2377 void *pool_tmp; 2378 2379 /* 2380 * If we haven't been "enabled", don't allow any SCSI I/O 2381 * to this FETD. 2382 */ 2383 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2384 retval = EPERM; 2385 break; 2386 } 2387 2388 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2389 2390 /* 2391 * Need to save the pool reference so it doesn't get 2392 * spammed by the user's ctl_io. 2393 */ 2394 pool_tmp = io->io_hdr.pool; 2395 memcpy(io, (void *)addr, sizeof(*io)); 2396 io->io_hdr.pool = pool_tmp; 2397 2398 /* 2399 * No status yet, so make sure the status is set properly. 2400 */ 2401 io->io_hdr.status = CTL_STATUS_NONE; 2402 2403 /* 2404 * The user sets the initiator ID, target and LUN IDs. 2405 */ 2406 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2407 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2408 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2409 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2410 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2411 2412 retval = ctl_ioctl_submit_wait(io); 2413 2414 if (retval != 0) { 2415 ctl_free_io(io); 2416 break; 2417 } 2418 2419 memcpy((void *)addr, io, sizeof(*io)); 2420 2421 /* return this to our pool */ 2422 ctl_free_io(io); 2423 2424 break; 2425 } 2426 case CTL_ENABLE_PORT: 2427 case CTL_DISABLE_PORT: 2428 case CTL_SET_PORT_WWNS: { 2429 struct ctl_port *port; 2430 struct ctl_port_entry *entry; 2431 2432 entry = (struct ctl_port_entry *)addr; 2433 2434 mtx_lock(&softc->ctl_lock); 2435 STAILQ_FOREACH(port, &softc->port_list, links) { 2436 int action, done; 2437 2438 action = 0; 2439 done = 0; 2440 2441 if ((entry->port_type == CTL_PORT_NONE) 2442 && (entry->targ_port == port->targ_port)) { 2443 /* 2444 * If the user only wants to enable or 2445 * disable or set WWNs on a specific port, 2446 * do the operation and we're done. 2447 */ 2448 action = 1; 2449 done = 1; 2450 } else if (entry->port_type & port->port_type) { 2451 /* 2452 * Compare the user's type mask with the 2453 * particular frontend type to see if we 2454 * have a match. 2455 */ 2456 action = 1; 2457 done = 0; 2458 2459 /* 2460 * Make sure the user isn't trying to set 2461 * WWNs on multiple ports at the same time. 2462 */ 2463 if (cmd == CTL_SET_PORT_WWNS) { 2464 printf("%s: Can't set WWNs on " 2465 "multiple ports\n", __func__); 2466 retval = EINVAL; 2467 break; 2468 } 2469 } 2470 if (action != 0) { 2471 /* 2472 * XXX KDM we have to drop the lock here, 2473 * because the online/offline operations 2474 * can potentially block. We need to 2475 * reference count the frontends so they 2476 * can't go away, 2477 */ 2478 mtx_unlock(&softc->ctl_lock); 2479 2480 if (cmd == CTL_ENABLE_PORT) { 2481 struct ctl_lun *lun; 2482 2483 STAILQ_FOREACH(lun, &softc->lun_list, 2484 links) { 2485 port->lun_enable(port->targ_lun_arg, 2486 lun->target, 2487 lun->lun); 2488 } 2489 2490 ctl_port_online(port); 2491 } else if (cmd == CTL_DISABLE_PORT) { 2492 struct ctl_lun *lun; 2493 2494 ctl_port_offline(port); 2495 2496 STAILQ_FOREACH(lun, &softc->lun_list, 2497 links) { 2498 port->lun_disable( 2499 port->targ_lun_arg, 2500 lun->target, 2501 lun->lun); 2502 } 2503 } 2504 2505 mtx_lock(&softc->ctl_lock); 2506 2507 if (cmd == CTL_SET_PORT_WWNS) 2508 ctl_port_set_wwns(port, 2509 (entry->flags & CTL_PORT_WWNN_VALID) ? 2510 1 : 0, entry->wwnn, 2511 (entry->flags & CTL_PORT_WWPN_VALID) ? 2512 1 : 0, entry->wwpn); 2513 } 2514 if (done != 0) 2515 break; 2516 } 2517 mtx_unlock(&softc->ctl_lock); 2518 break; 2519 } 2520 case CTL_GET_PORT_LIST: { 2521 struct ctl_port *port; 2522 struct ctl_port_list *list; 2523 int i; 2524 2525 list = (struct ctl_port_list *)addr; 2526 2527 if (list->alloc_len != (list->alloc_num * 2528 sizeof(struct ctl_port_entry))) { 2529 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2530 "alloc_num %u * sizeof(struct ctl_port_entry) " 2531 "%zu\n", __func__, list->alloc_len, 2532 list->alloc_num, sizeof(struct ctl_port_entry)); 2533 retval = EINVAL; 2534 break; 2535 } 2536 list->fill_len = 0; 2537 list->fill_num = 0; 2538 list->dropped_num = 0; 2539 i = 0; 2540 mtx_lock(&softc->ctl_lock); 2541 STAILQ_FOREACH(port, &softc->port_list, links) { 2542 struct ctl_port_entry entry, *list_entry; 2543 2544 if (list->fill_num >= list->alloc_num) { 2545 list->dropped_num++; 2546 continue; 2547 } 2548 2549 entry.port_type = port->port_type; 2550 strlcpy(entry.port_name, port->port_name, 2551 sizeof(entry.port_name)); 2552 entry.targ_port = port->targ_port; 2553 entry.physical_port = port->physical_port; 2554 entry.virtual_port = port->virtual_port; 2555 entry.wwnn = port->wwnn; 2556 entry.wwpn = port->wwpn; 2557 if (port->status & CTL_PORT_STATUS_ONLINE) 2558 entry.online = 1; 2559 else 2560 entry.online = 0; 2561 2562 list_entry = &list->entries[i]; 2563 2564 retval = copyout(&entry, list_entry, sizeof(entry)); 2565 if (retval != 0) { 2566 printf("%s: CTL_GET_PORT_LIST: copyout " 2567 "returned %d\n", __func__, retval); 2568 break; 2569 } 2570 i++; 2571 list->fill_num++; 2572 list->fill_len += sizeof(entry); 2573 } 2574 mtx_unlock(&softc->ctl_lock); 2575 2576 /* 2577 * If this is non-zero, we had a copyout fault, so there's 2578 * probably no point in attempting to set the status inside 2579 * the structure. 2580 */ 2581 if (retval != 0) 2582 break; 2583 2584 if (list->dropped_num > 0) 2585 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2586 else 2587 list->status = CTL_PORT_LIST_OK; 2588 break; 2589 } 2590 case CTL_DUMP_OOA: { 2591 struct ctl_lun *lun; 2592 union ctl_io *io; 2593 char printbuf[128]; 2594 struct sbuf sb; 2595 2596 mtx_lock(&softc->ctl_lock); 2597 printf("Dumping OOA queues:\n"); 2598 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2599 mtx_lock(&lun->lun_lock); 2600 for (io = (union ctl_io *)TAILQ_FIRST( 2601 &lun->ooa_queue); io != NULL; 2602 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2603 ooa_links)) { 2604 sbuf_new(&sb, printbuf, sizeof(printbuf), 2605 SBUF_FIXEDLEN); 2606 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2607 (intmax_t)lun->lun, 2608 io->scsiio.tag_num, 2609 (io->io_hdr.flags & 2610 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2611 (io->io_hdr.flags & 2612 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2613 (io->io_hdr.flags & 2614 CTL_FLAG_ABORT) ? " ABORT" : "", 2615 (io->io_hdr.flags & 2616 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2617 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2618 sbuf_finish(&sb); 2619 printf("%s\n", sbuf_data(&sb)); 2620 } 2621 mtx_unlock(&lun->lun_lock); 2622 } 2623 printf("OOA queues dump done\n"); 2624 mtx_unlock(&softc->ctl_lock); 2625 break; 2626 } 2627 case CTL_GET_OOA: { 2628 struct ctl_lun *lun; 2629 struct ctl_ooa *ooa_hdr; 2630 struct ctl_ooa_entry *entries; 2631 uint32_t cur_fill_num; 2632 2633 ooa_hdr = (struct ctl_ooa *)addr; 2634 2635 if ((ooa_hdr->alloc_len == 0) 2636 || (ooa_hdr->alloc_num == 0)) { 2637 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2638 "must be non-zero\n", __func__, 2639 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2640 retval = EINVAL; 2641 break; 2642 } 2643 2644 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2645 sizeof(struct ctl_ooa_entry))) { 2646 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2647 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2648 __func__, ooa_hdr->alloc_len, 2649 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2650 retval = EINVAL; 2651 break; 2652 } 2653 2654 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2655 if (entries == NULL) { 2656 printf("%s: could not allocate %d bytes for OOA " 2657 "dump\n", __func__, ooa_hdr->alloc_len); 2658 retval = ENOMEM; 2659 break; 2660 } 2661 2662 mtx_lock(&softc->ctl_lock); 2663 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2664 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2665 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2666 mtx_unlock(&softc->ctl_lock); 2667 free(entries, M_CTL); 2668 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2669 __func__, (uintmax_t)ooa_hdr->lun_num); 2670 retval = EINVAL; 2671 break; 2672 } 2673 2674 cur_fill_num = 0; 2675 2676 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2677 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2678 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2679 ooa_hdr, entries); 2680 if (retval != 0) 2681 break; 2682 } 2683 if (retval != 0) { 2684 mtx_unlock(&softc->ctl_lock); 2685 free(entries, M_CTL); 2686 break; 2687 } 2688 } else { 2689 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2690 2691 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2692 entries); 2693 } 2694 mtx_unlock(&softc->ctl_lock); 2695 2696 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2697 ooa_hdr->fill_len = ooa_hdr->fill_num * 2698 sizeof(struct ctl_ooa_entry); 2699 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2700 if (retval != 0) { 2701 printf("%s: error copying out %d bytes for OOA dump\n", 2702 __func__, ooa_hdr->fill_len); 2703 } 2704 2705 getbintime(&ooa_hdr->cur_bt); 2706 2707 if (cur_fill_num > ooa_hdr->alloc_num) { 2708 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2709 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2710 } else { 2711 ooa_hdr->dropped_num = 0; 2712 ooa_hdr->status = CTL_OOA_OK; 2713 } 2714 2715 free(entries, M_CTL); 2716 break; 2717 } 2718 case CTL_CHECK_OOA: { 2719 union ctl_io *io; 2720 struct ctl_lun *lun; 2721 struct ctl_ooa_info *ooa_info; 2722 2723 2724 ooa_info = (struct ctl_ooa_info *)addr; 2725 2726 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2727 ooa_info->status = CTL_OOA_INVALID_LUN; 2728 break; 2729 } 2730 mtx_lock(&softc->ctl_lock); 2731 lun = softc->ctl_luns[ooa_info->lun_id]; 2732 if (lun == NULL) { 2733 mtx_unlock(&softc->ctl_lock); 2734 ooa_info->status = CTL_OOA_INVALID_LUN; 2735 break; 2736 } 2737 mtx_lock(&lun->lun_lock); 2738 mtx_unlock(&softc->ctl_lock); 2739 ooa_info->num_entries = 0; 2740 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2741 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2742 &io->io_hdr, ooa_links)) { 2743 ooa_info->num_entries++; 2744 } 2745 mtx_unlock(&lun->lun_lock); 2746 2747 ooa_info->status = CTL_OOA_SUCCESS; 2748 2749 break; 2750 } 2751 case CTL_HARD_START: 2752 case CTL_HARD_STOP: { 2753 struct ctl_fe_ioctl_startstop_info ss_info; 2754 struct cfi_metatask *metatask; 2755 struct mtx hs_mtx; 2756 2757 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2758 2759 cv_init(&ss_info.sem, "hard start/stop cv" ); 2760 2761 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2762 if (metatask == NULL) { 2763 retval = ENOMEM; 2764 mtx_destroy(&hs_mtx); 2765 break; 2766 } 2767 2768 if (cmd == CTL_HARD_START) 2769 metatask->tasktype = CFI_TASK_STARTUP; 2770 else 2771 metatask->tasktype = CFI_TASK_SHUTDOWN; 2772 2773 metatask->callback = ctl_ioctl_hard_startstop_callback; 2774 metatask->callback_arg = &ss_info; 2775 2776 cfi_action(metatask); 2777 2778 /* Wait for the callback */ 2779 mtx_lock(&hs_mtx); 2780 cv_wait_sig(&ss_info.sem, &hs_mtx); 2781 mtx_unlock(&hs_mtx); 2782 2783 /* 2784 * All information has been copied from the metatask by the 2785 * time cv_broadcast() is called, so we free the metatask here. 2786 */ 2787 cfi_free_metatask(metatask); 2788 2789 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2790 2791 mtx_destroy(&hs_mtx); 2792 break; 2793 } 2794 case CTL_BBRREAD: { 2795 struct ctl_bbrread_info *bbr_info; 2796 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2797 struct mtx bbr_mtx; 2798 struct cfi_metatask *metatask; 2799 2800 bbr_info = (struct ctl_bbrread_info *)addr; 2801 2802 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2803 2804 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2805 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2806 2807 fe_bbr_info.bbr_info = bbr_info; 2808 fe_bbr_info.lock = &bbr_mtx; 2809 2810 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2811 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2812 2813 if (metatask == NULL) { 2814 mtx_destroy(&bbr_mtx); 2815 cv_destroy(&fe_bbr_info.sem); 2816 retval = ENOMEM; 2817 break; 2818 } 2819 metatask->tasktype = CFI_TASK_BBRREAD; 2820 metatask->callback = ctl_ioctl_bbrread_callback; 2821 metatask->callback_arg = &fe_bbr_info; 2822 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2823 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2824 metatask->taskinfo.bbrread.len = bbr_info->len; 2825 2826 cfi_action(metatask); 2827 2828 mtx_lock(&bbr_mtx); 2829 while (fe_bbr_info.wakeup_done == 0) 2830 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2831 mtx_unlock(&bbr_mtx); 2832 2833 bbr_info->status = metatask->status; 2834 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2835 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2836 memcpy(&bbr_info->sense_data, 2837 &metatask->taskinfo.bbrread.sense_data, 2838 ctl_min(sizeof(bbr_info->sense_data), 2839 sizeof(metatask->taskinfo.bbrread.sense_data))); 2840 2841 cfi_free_metatask(metatask); 2842 2843 mtx_destroy(&bbr_mtx); 2844 cv_destroy(&fe_bbr_info.sem); 2845 2846 break; 2847 } 2848 case CTL_DELAY_IO: { 2849 struct ctl_io_delay_info *delay_info; 2850#ifdef CTL_IO_DELAY 2851 struct ctl_lun *lun; 2852#endif /* CTL_IO_DELAY */ 2853 2854 delay_info = (struct ctl_io_delay_info *)addr; 2855 2856#ifdef CTL_IO_DELAY 2857 mtx_lock(&softc->ctl_lock); 2858 2859 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2860 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2861 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2862 } else { 2863 lun = softc->ctl_luns[delay_info->lun_id]; 2864 mtx_lock(&lun->lun_lock); 2865 2866 delay_info->status = CTL_DELAY_STATUS_OK; 2867 2868 switch (delay_info->delay_type) { 2869 case CTL_DELAY_TYPE_CONT: 2870 break; 2871 case CTL_DELAY_TYPE_ONESHOT: 2872 break; 2873 default: 2874 delay_info->status = 2875 CTL_DELAY_STATUS_INVALID_TYPE; 2876 break; 2877 } 2878 2879 switch (delay_info->delay_loc) { 2880 case CTL_DELAY_LOC_DATAMOVE: 2881 lun->delay_info.datamove_type = 2882 delay_info->delay_type; 2883 lun->delay_info.datamove_delay = 2884 delay_info->delay_secs; 2885 break; 2886 case CTL_DELAY_LOC_DONE: 2887 lun->delay_info.done_type = 2888 delay_info->delay_type; 2889 lun->delay_info.done_delay = 2890 delay_info->delay_secs; 2891 break; 2892 default: 2893 delay_info->status = 2894 CTL_DELAY_STATUS_INVALID_LOC; 2895 break; 2896 } 2897 mtx_unlock(&lun->lun_lock); 2898 } 2899 2900 mtx_unlock(&softc->ctl_lock); 2901#else 2902 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2903#endif /* CTL_IO_DELAY */ 2904 break; 2905 } 2906 case CTL_REALSYNC_SET: { 2907 int *syncstate; 2908 2909 syncstate = (int *)addr; 2910 2911 mtx_lock(&softc->ctl_lock); 2912 switch (*syncstate) { 2913 case 0: 2914 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2915 break; 2916 case 1: 2917 softc->flags |= CTL_FLAG_REAL_SYNC; 2918 break; 2919 default: 2920 retval = EINVAL; 2921 break; 2922 } 2923 mtx_unlock(&softc->ctl_lock); 2924 break; 2925 } 2926 case CTL_REALSYNC_GET: { 2927 int *syncstate; 2928 2929 syncstate = (int*)addr; 2930 2931 mtx_lock(&softc->ctl_lock); 2932 if (softc->flags & CTL_FLAG_REAL_SYNC) 2933 *syncstate = 1; 2934 else 2935 *syncstate = 0; 2936 mtx_unlock(&softc->ctl_lock); 2937 2938 break; 2939 } 2940 case CTL_SETSYNC: 2941 case CTL_GETSYNC: { 2942 struct ctl_sync_info *sync_info; 2943 struct ctl_lun *lun; 2944 2945 sync_info = (struct ctl_sync_info *)addr; 2946 2947 mtx_lock(&softc->ctl_lock); 2948 lun = softc->ctl_luns[sync_info->lun_id]; 2949 if (lun == NULL) { 2950 mtx_unlock(&softc->ctl_lock); 2951 sync_info->status = CTL_GS_SYNC_NO_LUN; 2952 } 2953 /* 2954 * Get or set the sync interval. We're not bounds checking 2955 * in the set case, hopefully the user won't do something 2956 * silly. 2957 */ 2958 mtx_lock(&lun->lun_lock); 2959 mtx_unlock(&softc->ctl_lock); 2960 if (cmd == CTL_GETSYNC) 2961 sync_info->sync_interval = lun->sync_interval; 2962 else 2963 lun->sync_interval = sync_info->sync_interval; 2964 mtx_unlock(&lun->lun_lock); 2965 2966 sync_info->status = CTL_GS_SYNC_OK; 2967 2968 break; 2969 } 2970 case CTL_GETSTATS: { 2971 struct ctl_stats *stats; 2972 struct ctl_lun *lun; 2973 int i; 2974 2975 stats = (struct ctl_stats *)addr; 2976 2977 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2978 stats->alloc_len) { 2979 stats->status = CTL_SS_NEED_MORE_SPACE; 2980 stats->num_luns = softc->num_luns; 2981 break; 2982 } 2983 /* 2984 * XXX KDM no locking here. If the LUN list changes, 2985 * things can blow up. 2986 */ 2987 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2988 i++, lun = STAILQ_NEXT(lun, links)) { 2989 retval = copyout(&lun->stats, &stats->lun_stats[i], 2990 sizeof(lun->stats)); 2991 if (retval != 0) 2992 break; 2993 } 2994 stats->num_luns = softc->num_luns; 2995 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2996 softc->num_luns; 2997 stats->status = CTL_SS_OK; 2998#ifdef CTL_TIME_IO 2999 stats->flags = CTL_STATS_FLAG_TIME_VALID; 3000#else 3001 stats->flags = CTL_STATS_FLAG_NONE; 3002#endif 3003 getnanouptime(&stats->timestamp); 3004 break; 3005 } 3006 case CTL_ERROR_INJECT: { 3007 struct ctl_error_desc *err_desc, *new_err_desc; 3008 struct ctl_lun *lun; 3009 3010 err_desc = (struct ctl_error_desc *)addr; 3011 3012 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 3013 M_WAITOK | M_ZERO); 3014 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 3015 3016 mtx_lock(&softc->ctl_lock); 3017 lun = softc->ctl_luns[err_desc->lun_id]; 3018 if (lun == NULL) { 3019 mtx_unlock(&softc->ctl_lock); 3020 free(new_err_desc, M_CTL); 3021 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 3022 __func__, (uintmax_t)err_desc->lun_id); 3023 retval = EINVAL; 3024 break; 3025 } 3026 mtx_lock(&lun->lun_lock); 3027 mtx_unlock(&softc->ctl_lock); 3028 3029 /* 3030 * We could do some checking here to verify the validity 3031 * of the request, but given the complexity of error 3032 * injection requests, the checking logic would be fairly 3033 * complex. 3034 * 3035 * For now, if the request is invalid, it just won't get 3036 * executed and might get deleted. 3037 */ 3038 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3039 3040 /* 3041 * XXX KDM check to make sure the serial number is unique, 3042 * in case we somehow manage to wrap. That shouldn't 3043 * happen for a very long time, but it's the right thing to 3044 * do. 3045 */ 3046 new_err_desc->serial = lun->error_serial; 3047 err_desc->serial = lun->error_serial; 3048 lun->error_serial++; 3049 3050 mtx_unlock(&lun->lun_lock); 3051 break; 3052 } 3053 case CTL_ERROR_INJECT_DELETE: { 3054 struct ctl_error_desc *delete_desc, *desc, *desc2; 3055 struct ctl_lun *lun; 3056 int delete_done; 3057 3058 delete_desc = (struct ctl_error_desc *)addr; 3059 delete_done = 0; 3060 3061 mtx_lock(&softc->ctl_lock); 3062 lun = softc->ctl_luns[delete_desc->lun_id]; 3063 if (lun == NULL) { 3064 mtx_unlock(&softc->ctl_lock); 3065 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3066 __func__, (uintmax_t)delete_desc->lun_id); 3067 retval = EINVAL; 3068 break; 3069 } 3070 mtx_lock(&lun->lun_lock); 3071 mtx_unlock(&softc->ctl_lock); 3072 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3073 if (desc->serial != delete_desc->serial) 3074 continue; 3075 3076 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3077 links); 3078 free(desc, M_CTL); 3079 delete_done = 1; 3080 } 3081 mtx_unlock(&lun->lun_lock); 3082 if (delete_done == 0) { 3083 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3084 "error serial %ju on LUN %u\n", __func__, 3085 delete_desc->serial, delete_desc->lun_id); 3086 retval = EINVAL; 3087 break; 3088 } 3089 break; 3090 } 3091 case CTL_DUMP_STRUCTS: { 3092 int i, j, k; 3093 struct ctl_port *port; 3094 struct ctl_frontend *fe; 3095 3096 mtx_lock(&softc->ctl_lock); 3097 printf("CTL Persistent Reservation information start:\n"); 3098 for (i = 0; i < CTL_MAX_LUNS; i++) { 3099 struct ctl_lun *lun; 3100 3101 lun = softc->ctl_luns[i]; 3102 3103 if ((lun == NULL) 3104 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3105 continue; 3106 3107 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3108 if (lun->pr_keys[j] == NULL) 3109 continue; 3110 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3111 if (lun->pr_keys[j][k] == 0) 3112 continue; 3113 printf(" LUN %d port %d iid %d key " 3114 "%#jx\n", i, j, k, 3115 (uintmax_t)lun->pr_keys[j][k]); 3116 } 3117 } 3118 } 3119 printf("CTL Persistent Reservation information end\n"); 3120 printf("CTL Ports:\n"); 3121 STAILQ_FOREACH(port, &softc->port_list, links) { 3122 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3123 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3124 port->frontend->name, port->port_type, 3125 port->physical_port, port->virtual_port, 3126 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3127 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3128 if (port->wwpn_iid[j].in_use == 0 && 3129 port->wwpn_iid[j].wwpn == 0 && 3130 port->wwpn_iid[j].name == NULL) 3131 continue; 3132 3133 printf(" iid %u use %d WWPN %#jx '%s'\n", 3134 j, port->wwpn_iid[j].in_use, 3135 (uintmax_t)port->wwpn_iid[j].wwpn, 3136 port->wwpn_iid[j].name); 3137 } 3138 } 3139 printf("CTL Port information end\n"); 3140 mtx_unlock(&softc->ctl_lock); 3141 /* 3142 * XXX KDM calling this without a lock. We'd likely want 3143 * to drop the lock before calling the frontend's dump 3144 * routine anyway. 3145 */ 3146 printf("CTL Frontends:\n"); 3147 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3148 printf(" Frontend '%s'\n", fe->name); 3149 if (fe->fe_dump != NULL) 3150 fe->fe_dump(); 3151 } 3152 printf("CTL Frontend information end\n"); 3153 break; 3154 } 3155 case CTL_LUN_REQ: { 3156 struct ctl_lun_req *lun_req; 3157 struct ctl_backend_driver *backend; 3158 3159 lun_req = (struct ctl_lun_req *)addr; 3160 3161 backend = ctl_backend_find(lun_req->backend); 3162 if (backend == NULL) { 3163 lun_req->status = CTL_LUN_ERROR; 3164 snprintf(lun_req->error_str, 3165 sizeof(lun_req->error_str), 3166 "Backend \"%s\" not found.", 3167 lun_req->backend); 3168 break; 3169 } 3170 if (lun_req->num_be_args > 0) { 3171 lun_req->kern_be_args = ctl_copyin_args( 3172 lun_req->num_be_args, 3173 lun_req->be_args, 3174 lun_req->error_str, 3175 sizeof(lun_req->error_str)); 3176 if (lun_req->kern_be_args == NULL) { 3177 lun_req->status = CTL_LUN_ERROR; 3178 break; 3179 } 3180 } 3181 3182 retval = backend->ioctl(dev, cmd, addr, flag, td); 3183 3184 if (lun_req->num_be_args > 0) { 3185 ctl_copyout_args(lun_req->num_be_args, 3186 lun_req->kern_be_args); 3187 ctl_free_args(lun_req->num_be_args, 3188 lun_req->kern_be_args); 3189 } 3190 break; 3191 } 3192 case CTL_LUN_LIST: { 3193 struct sbuf *sb; 3194 struct ctl_lun *lun; 3195 struct ctl_lun_list *list; 3196 struct ctl_option *opt; 3197 3198 list = (struct ctl_lun_list *)addr; 3199 3200 /* 3201 * Allocate a fixed length sbuf here, based on the length 3202 * of the user's buffer. We could allocate an auto-extending 3203 * buffer, and then tell the user how much larger our 3204 * amount of data is than his buffer, but that presents 3205 * some problems: 3206 * 3207 * 1. The sbuf(9) routines use a blocking malloc, and so 3208 * we can't hold a lock while calling them with an 3209 * auto-extending buffer. 3210 * 3211 * 2. There is not currently a LUN reference counting 3212 * mechanism, outside of outstanding transactions on 3213 * the LUN's OOA queue. So a LUN could go away on us 3214 * while we're getting the LUN number, backend-specific 3215 * information, etc. Thus, given the way things 3216 * currently work, we need to hold the CTL lock while 3217 * grabbing LUN information. 3218 * 3219 * So, from the user's standpoint, the best thing to do is 3220 * allocate what he thinks is a reasonable buffer length, 3221 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3222 * double the buffer length and try again. (And repeat 3223 * that until he succeeds.) 3224 */ 3225 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3226 if (sb == NULL) { 3227 list->status = CTL_LUN_LIST_ERROR; 3228 snprintf(list->error_str, sizeof(list->error_str), 3229 "Unable to allocate %d bytes for LUN list", 3230 list->alloc_len); 3231 break; 3232 } 3233 3234 sbuf_printf(sb, "<ctllunlist>\n"); 3235 3236 mtx_lock(&softc->ctl_lock); 3237 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3238 mtx_lock(&lun->lun_lock); 3239 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3240 (uintmax_t)lun->lun); 3241 3242 /* 3243 * Bail out as soon as we see that we've overfilled 3244 * the buffer. 3245 */ 3246 if (retval != 0) 3247 break; 3248 3249 retval = sbuf_printf(sb, "\t<backend_type>%s" 3250 "</backend_type>\n", 3251 (lun->backend == NULL) ? "none" : 3252 lun->backend->name); 3253 3254 if (retval != 0) 3255 break; 3256 3257 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3258 lun->be_lun->lun_type); 3259 3260 if (retval != 0) 3261 break; 3262 3263 if (lun->backend == NULL) { 3264 retval = sbuf_printf(sb, "</lun>\n"); 3265 if (retval != 0) 3266 break; 3267 continue; 3268 } 3269 3270 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3271 (lun->be_lun->maxlba > 0) ? 3272 lun->be_lun->maxlba + 1 : 0); 3273 3274 if (retval != 0) 3275 break; 3276 3277 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3278 lun->be_lun->blocksize); 3279 3280 if (retval != 0) 3281 break; 3282 3283 retval = sbuf_printf(sb, "\t<serial_number>"); 3284 3285 if (retval != 0) 3286 break; 3287 3288 retval = ctl_sbuf_printf_esc(sb, 3289 lun->be_lun->serial_num, 3290 sizeof(lun->be_lun->serial_num)); 3291 3292 if (retval != 0) 3293 break; 3294 3295 retval = sbuf_printf(sb, "</serial_number>\n"); 3296 3297 if (retval != 0) 3298 break; 3299 3300 retval = sbuf_printf(sb, "\t<device_id>"); 3301 3302 if (retval != 0) 3303 break; 3304 3305 retval = ctl_sbuf_printf_esc(sb, 3306 lun->be_lun->device_id, 3307 sizeof(lun->be_lun->device_id)); 3308 3309 if (retval != 0) 3310 break; 3311 3312 retval = sbuf_printf(sb, "</device_id>\n"); 3313 3314 if (retval != 0) 3315 break; 3316 3317 if (lun->backend->lun_info != NULL) { 3318 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3319 if (retval != 0) 3320 break; 3321 } 3322 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3323 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3324 opt->name, opt->value, opt->name); 3325 if (retval != 0) 3326 break; 3327 } 3328 3329 retval = sbuf_printf(sb, "</lun>\n"); 3330 3331 if (retval != 0) 3332 break; 3333 mtx_unlock(&lun->lun_lock); 3334 } 3335 if (lun != NULL) 3336 mtx_unlock(&lun->lun_lock); 3337 mtx_unlock(&softc->ctl_lock); 3338 3339 if ((retval != 0) 3340 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3341 retval = 0; 3342 sbuf_delete(sb); 3343 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3344 snprintf(list->error_str, sizeof(list->error_str), 3345 "Out of space, %d bytes is too small", 3346 list->alloc_len); 3347 break; 3348 } 3349 3350 sbuf_finish(sb); 3351 3352 retval = copyout(sbuf_data(sb), list->lun_xml, 3353 sbuf_len(sb) + 1); 3354 3355 list->fill_len = sbuf_len(sb) + 1; 3356 list->status = CTL_LUN_LIST_OK; 3357 sbuf_delete(sb); 3358 break; 3359 } 3360 case CTL_ISCSI: { 3361 struct ctl_iscsi *ci; 3362 struct ctl_frontend *fe; 3363 3364 ci = (struct ctl_iscsi *)addr; 3365 3366 fe = ctl_frontend_find("iscsi"); 3367 if (fe == NULL) { 3368 ci->status = CTL_ISCSI_ERROR; 3369 snprintf(ci->error_str, sizeof(ci->error_str), 3370 "Frontend \"iscsi\" not found."); 3371 break; 3372 } 3373 3374 retval = fe->ioctl(dev, cmd, addr, flag, td); 3375 break; 3376 } 3377 case CTL_PORT_REQ: { 3378 struct ctl_req *req; 3379 struct ctl_frontend *fe; 3380 3381 req = (struct ctl_req *)addr; 3382 3383 fe = ctl_frontend_find(req->driver); 3384 if (fe == NULL) { 3385 req->status = CTL_LUN_ERROR; 3386 snprintf(req->error_str, sizeof(req->error_str), 3387 "Frontend \"%s\" not found.", req->driver); 3388 break; 3389 } 3390 if (req->num_args > 0) { 3391 req->kern_args = ctl_copyin_args(req->num_args, 3392 req->args, req->error_str, sizeof(req->error_str)); 3393 if (req->kern_args == NULL) { 3394 req->status = CTL_LUN_ERROR; 3395 break; 3396 } 3397 } 3398 3399 retval = fe->ioctl(dev, cmd, addr, flag, td); 3400 3401 if (req->num_args > 0) { 3402 ctl_copyout_args(req->num_args, req->kern_args); 3403 ctl_free_args(req->num_args, req->kern_args); 3404 } 3405 break; 3406 } 3407 case CTL_PORT_LIST: { 3408 struct sbuf *sb; 3409 struct ctl_port *port; 3410 struct ctl_lun_list *list; 3411 struct ctl_option *opt; 3412 int j; 3413 3414 list = (struct ctl_lun_list *)addr; 3415 3416 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3417 if (sb == NULL) { 3418 list->status = CTL_LUN_LIST_ERROR; 3419 snprintf(list->error_str, sizeof(list->error_str), 3420 "Unable to allocate %d bytes for LUN list", 3421 list->alloc_len); 3422 break; 3423 } 3424 3425 sbuf_printf(sb, "<ctlportlist>\n"); 3426 3427 mtx_lock(&softc->ctl_lock); 3428 STAILQ_FOREACH(port, &softc->port_list, links) { 3429 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3430 (uintmax_t)port->targ_port); 3431 3432 /* 3433 * Bail out as soon as we see that we've overfilled 3434 * the buffer. 3435 */ 3436 if (retval != 0) 3437 break; 3438 3439 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3440 "</frontend_type>\n", port->frontend->name); 3441 if (retval != 0) 3442 break; 3443 3444 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3445 port->port_type); 3446 if (retval != 0) 3447 break; 3448 3449 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3450 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3451 if (retval != 0) 3452 break; 3453 3454 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3455 port->port_name); 3456 if (retval != 0) 3457 break; 3458 3459 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3460 port->physical_port); 3461 if (retval != 0) 3462 break; 3463 3464 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3465 port->virtual_port); 3466 if (retval != 0) 3467 break; 3468 3469 if (port->target_devid != NULL) { 3470 sbuf_printf(sb, "\t<target>"); 3471 ctl_id_sbuf(port->target_devid, sb); 3472 sbuf_printf(sb, "</target>\n"); 3473 } 3474 3475 if (port->port_devid != NULL) { 3476 sbuf_printf(sb, "\t<port>"); 3477 ctl_id_sbuf(port->port_devid, sb); 3478 sbuf_printf(sb, "</port>\n"); 3479 } 3480 3481 if (port->port_info != NULL) { 3482 retval = port->port_info(port->onoff_arg, sb); 3483 if (retval != 0) 3484 break; 3485 } 3486 STAILQ_FOREACH(opt, &port->options, links) { 3487 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3488 opt->name, opt->value, opt->name); 3489 if (retval != 0) 3490 break; 3491 } 3492 3493 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3494 if (port->wwpn_iid[j].in_use == 0 || 3495 (port->wwpn_iid[j].wwpn == 0 && 3496 port->wwpn_iid[j].name == NULL)) 3497 continue; 3498 3499 if (port->wwpn_iid[j].name != NULL) 3500 retval = sbuf_printf(sb, 3501 "\t<initiator>%u %s</initiator>\n", 3502 j, port->wwpn_iid[j].name); 3503 else 3504 retval = sbuf_printf(sb, 3505 "\t<initiator>%u naa.%08jx</initiator>\n", 3506 j, port->wwpn_iid[j].wwpn); 3507 if (retval != 0) 3508 break; 3509 } 3510 if (retval != 0) 3511 break; 3512 3513 retval = sbuf_printf(sb, "</targ_port>\n"); 3514 if (retval != 0) 3515 break; 3516 } 3517 mtx_unlock(&softc->ctl_lock); 3518 3519 if ((retval != 0) 3520 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3521 retval = 0; 3522 sbuf_delete(sb); 3523 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3524 snprintf(list->error_str, sizeof(list->error_str), 3525 "Out of space, %d bytes is too small", 3526 list->alloc_len); 3527 break; 3528 } 3529 3530 sbuf_finish(sb); 3531 3532 retval = copyout(sbuf_data(sb), list->lun_xml, 3533 sbuf_len(sb) + 1); 3534 3535 list->fill_len = sbuf_len(sb) + 1; 3536 list->status = CTL_LUN_LIST_OK; 3537 sbuf_delete(sb); 3538 break; 3539 } 3540 default: { 3541 /* XXX KDM should we fix this? */ 3542#if 0 3543 struct ctl_backend_driver *backend; 3544 unsigned int type; 3545 int found; 3546 3547 found = 0; 3548 3549 /* 3550 * We encode the backend type as the ioctl type for backend 3551 * ioctls. So parse it out here, and then search for a 3552 * backend of this type. 3553 */ 3554 type = _IOC_TYPE(cmd); 3555 3556 STAILQ_FOREACH(backend, &softc->be_list, links) { 3557 if (backend->type == type) { 3558 found = 1; 3559 break; 3560 } 3561 } 3562 if (found == 0) { 3563 printf("ctl: unknown ioctl command %#lx or backend " 3564 "%d\n", cmd, type); 3565 retval = EINVAL; 3566 break; 3567 } 3568 retval = backend->ioctl(dev, cmd, addr, flag, td); 3569#endif 3570 retval = ENOTTY; 3571 break; 3572 } 3573 } 3574 return (retval); 3575} 3576 3577uint32_t 3578ctl_get_initindex(struct ctl_nexus *nexus) 3579{ 3580 if (nexus->targ_port < CTL_MAX_PORTS) 3581 return (nexus->initid.id + 3582 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3583 else 3584 return (nexus->initid.id + 3585 ((nexus->targ_port - CTL_MAX_PORTS) * 3586 CTL_MAX_INIT_PER_PORT)); 3587} 3588 3589uint32_t 3590ctl_get_resindex(struct ctl_nexus *nexus) 3591{ 3592 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3593} 3594 3595uint32_t 3596ctl_port_idx(int port_num) 3597{ 3598 if (port_num < CTL_MAX_PORTS) 3599 return(port_num); 3600 else 3601 return(port_num - CTL_MAX_PORTS); 3602} 3603 3604static uint32_t 3605ctl_map_lun(int port_num, uint32_t lun_id) 3606{ 3607 struct ctl_port *port; 3608 3609 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3610 if (port == NULL) 3611 return (UINT32_MAX); 3612 if (port->lun_map == NULL) 3613 return (lun_id); 3614 return (port->lun_map(port->targ_lun_arg, lun_id)); 3615} 3616 3617static uint32_t 3618ctl_map_lun_back(int port_num, uint32_t lun_id) 3619{ 3620 struct ctl_port *port; 3621 uint32_t i; 3622 3623 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3624 if (port->lun_map == NULL) 3625 return (lun_id); 3626 for (i = 0; i < CTL_MAX_LUNS; i++) { 3627 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3628 return (i); 3629 } 3630 return (UINT32_MAX); 3631} 3632 3633/* 3634 * Note: This only works for bitmask sizes that are at least 32 bits, and 3635 * that are a power of 2. 3636 */ 3637int 3638ctl_ffz(uint32_t *mask, uint32_t size) 3639{ 3640 uint32_t num_chunks, num_pieces; 3641 int i, j; 3642 3643 num_chunks = (size >> 5); 3644 if (num_chunks == 0) 3645 num_chunks++; 3646 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3647 3648 for (i = 0; i < num_chunks; i++) { 3649 for (j = 0; j < num_pieces; j++) { 3650 if ((mask[i] & (1 << j)) == 0) 3651 return ((i << 5) + j); 3652 } 3653 } 3654 3655 return (-1); 3656} 3657 3658int 3659ctl_set_mask(uint32_t *mask, uint32_t bit) 3660{ 3661 uint32_t chunk, piece; 3662 3663 chunk = bit >> 5; 3664 piece = bit % (sizeof(uint32_t) * 8); 3665 3666 if ((mask[chunk] & (1 << piece)) != 0) 3667 return (-1); 3668 else 3669 mask[chunk] |= (1 << piece); 3670 3671 return (0); 3672} 3673 3674int 3675ctl_clear_mask(uint32_t *mask, uint32_t bit) 3676{ 3677 uint32_t chunk, piece; 3678 3679 chunk = bit >> 5; 3680 piece = bit % (sizeof(uint32_t) * 8); 3681 3682 if ((mask[chunk] & (1 << piece)) == 0) 3683 return (-1); 3684 else 3685 mask[chunk] &= ~(1 << piece); 3686 3687 return (0); 3688} 3689 3690int 3691ctl_is_set(uint32_t *mask, uint32_t bit) 3692{ 3693 uint32_t chunk, piece; 3694 3695 chunk = bit >> 5; 3696 piece = bit % (sizeof(uint32_t) * 8); 3697 3698 if ((mask[chunk] & (1 << piece)) == 0) 3699 return (0); 3700 else 3701 return (1); 3702} 3703 3704static uint64_t 3705ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3706{ 3707 uint64_t *t; 3708 3709 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3710 if (t == NULL) 3711 return (0); 3712 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3713} 3714 3715static void 3716ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3717{ 3718 uint64_t *t; 3719 3720 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3721 if (t == NULL) 3722 return; 3723 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3724} 3725 3726static void 3727ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3728{ 3729 uint64_t *p; 3730 u_int i; 3731 3732 i = residx/CTL_MAX_INIT_PER_PORT; 3733 if (lun->pr_keys[i] != NULL) 3734 return; 3735 mtx_unlock(&lun->lun_lock); 3736 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3737 M_WAITOK | M_ZERO); 3738 mtx_lock(&lun->lun_lock); 3739 if (lun->pr_keys[i] == NULL) 3740 lun->pr_keys[i] = p; 3741 else 3742 free(p, M_CTL); 3743} 3744 3745static void 3746ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3747{ 3748 uint64_t *t; 3749 3750 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3751 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3752 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3753} 3754 3755/* 3756 * ctl_softc, pool_name, total_ctl_io are passed in. 3757 * npool is passed out. 3758 */ 3759int 3760ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3761 uint32_t total_ctl_io, void **npool) 3762{ 3763#ifdef IO_POOLS 3764 struct ctl_io_pool *pool; 3765 3766 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3767 M_NOWAIT | M_ZERO); 3768 if (pool == NULL) 3769 return (ENOMEM); 3770 3771 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3772 pool->ctl_softc = ctl_softc; 3773 pool->zone = uma_zsecond_create(pool->name, NULL, 3774 NULL, NULL, NULL, ctl_softc->io_zone); 3775 /* uma_prealloc(pool->zone, total_ctl_io); */ 3776 3777 *npool = pool; 3778#else 3779 *npool = ctl_softc->io_zone; 3780#endif 3781 return (0); 3782} 3783 3784void 3785ctl_pool_free(struct ctl_io_pool *pool) 3786{ 3787 3788 if (pool == NULL) 3789 return; 3790 3791#ifdef IO_POOLS 3792 uma_zdestroy(pool->zone); 3793 free(pool, M_CTL); 3794#endif 3795} 3796 3797union ctl_io * 3798ctl_alloc_io(void *pool_ref) 3799{ 3800 union ctl_io *io; 3801#ifdef IO_POOLS 3802 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3803 3804 io = uma_zalloc(pool->zone, M_WAITOK); 3805#else 3806 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3807#endif 3808 if (io != NULL) 3809 io->io_hdr.pool = pool_ref; 3810 return (io); 3811} 3812 3813union ctl_io * 3814ctl_alloc_io_nowait(void *pool_ref) 3815{ 3816 union ctl_io *io; 3817#ifdef IO_POOLS 3818 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3819 3820 io = uma_zalloc(pool->zone, M_NOWAIT); 3821#else 3822 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3823#endif 3824 if (io != NULL) 3825 io->io_hdr.pool = pool_ref; 3826 return (io); 3827} 3828 3829void 3830ctl_free_io(union ctl_io *io) 3831{ 3832#ifdef IO_POOLS 3833 struct ctl_io_pool *pool; 3834#endif 3835 3836 if (io == NULL) 3837 return; 3838 3839#ifdef IO_POOLS 3840 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3841 uma_zfree(pool->zone, io); 3842#else 3843 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3844#endif 3845} 3846 3847void 3848ctl_zero_io(union ctl_io *io) 3849{ 3850 void *pool_ref; 3851 3852 if (io == NULL) 3853 return; 3854 3855 /* 3856 * May need to preserve linked list pointers at some point too. 3857 */ 3858 pool_ref = io->io_hdr.pool; 3859 memset(io, 0, sizeof(*io)); 3860 io->io_hdr.pool = pool_ref; 3861} 3862 3863/* 3864 * This routine is currently used for internal copies of ctl_ios that need 3865 * to persist for some reason after we've already returned status to the 3866 * FETD. (Thus the flag set.) 3867 * 3868 * XXX XXX 3869 * Note that this makes a blind copy of all fields in the ctl_io, except 3870 * for the pool reference. This includes any memory that has been 3871 * allocated! That memory will no longer be valid after done has been 3872 * called, so this would be VERY DANGEROUS for command that actually does 3873 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3874 * start and stop commands, which don't transfer any data, so this is not a 3875 * problem. If it is used for anything else, the caller would also need to 3876 * allocate data buffer space and this routine would need to be modified to 3877 * copy the data buffer(s) as well. 3878 */ 3879void 3880ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3881{ 3882 void *pool_ref; 3883 3884 if ((src == NULL) 3885 || (dest == NULL)) 3886 return; 3887 3888 /* 3889 * May need to preserve linked list pointers at some point too. 3890 */ 3891 pool_ref = dest->io_hdr.pool; 3892 3893 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3894 3895 dest->io_hdr.pool = pool_ref; 3896 /* 3897 * We need to know that this is an internal copy, and doesn't need 3898 * to get passed back to the FETD that allocated it. 3899 */ 3900 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3901} 3902 3903static int 3904ctl_expand_number(const char *buf, uint64_t *num) 3905{ 3906 char *endptr; 3907 uint64_t number; 3908 unsigned shift; 3909 3910 number = strtoq(buf, &endptr, 0); 3911 3912 switch (tolower((unsigned char)*endptr)) { 3913 case 'e': 3914 shift = 60; 3915 break; 3916 case 'p': 3917 shift = 50; 3918 break; 3919 case 't': 3920 shift = 40; 3921 break; 3922 case 'g': 3923 shift = 30; 3924 break; 3925 case 'm': 3926 shift = 20; 3927 break; 3928 case 'k': 3929 shift = 10; 3930 break; 3931 case 'b': 3932 case '\0': /* No unit. */ 3933 *num = number; 3934 return (0); 3935 default: 3936 /* Unrecognized unit. */ 3937 return (-1); 3938 } 3939 3940 if ((number << shift) >> shift != number) { 3941 /* Overflow */ 3942 return (-1); 3943 } 3944 *num = number << shift; 3945 return (0); 3946} 3947 3948 3949/* 3950 * This routine could be used in the future to load default and/or saved 3951 * mode page parameters for a particuar lun. 3952 */ 3953static int 3954ctl_init_page_index(struct ctl_lun *lun) 3955{ 3956 int i; 3957 struct ctl_page_index *page_index; 3958 const char *value; 3959 uint64_t ival; 3960 3961 memcpy(&lun->mode_pages.index, page_index_template, 3962 sizeof(page_index_template)); 3963 3964 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3965 3966 page_index = &lun->mode_pages.index[i]; 3967 /* 3968 * If this is a disk-only mode page, there's no point in 3969 * setting it up. For some pages, we have to have some 3970 * basic information about the disk in order to calculate the 3971 * mode page data. 3972 */ 3973 if ((lun->be_lun->lun_type != T_DIRECT) 3974 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3975 continue; 3976 3977 switch (page_index->page_code & SMPH_PC_MASK) { 3978 case SMS_RW_ERROR_RECOVERY_PAGE: { 3979 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3980 panic("subpage is incorrect!"); 3981 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3982 &rw_er_page_default, 3983 sizeof(rw_er_page_default)); 3984 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3985 &rw_er_page_changeable, 3986 sizeof(rw_er_page_changeable)); 3987 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3988 &rw_er_page_default, 3989 sizeof(rw_er_page_default)); 3990 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3991 &rw_er_page_default, 3992 sizeof(rw_er_page_default)); 3993 page_index->page_data = 3994 (uint8_t *)lun->mode_pages.rw_er_page; 3995 break; 3996 } 3997 case SMS_FORMAT_DEVICE_PAGE: { 3998 struct scsi_format_page *format_page; 3999 4000 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4001 panic("subpage is incorrect!"); 4002 4003 /* 4004 * Sectors per track are set above. Bytes per 4005 * sector need to be set here on a per-LUN basis. 4006 */ 4007 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4008 &format_page_default, 4009 sizeof(format_page_default)); 4010 memcpy(&lun->mode_pages.format_page[ 4011 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4012 sizeof(format_page_changeable)); 4013 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4014 &format_page_default, 4015 sizeof(format_page_default)); 4016 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4017 &format_page_default, 4018 sizeof(format_page_default)); 4019 4020 format_page = &lun->mode_pages.format_page[ 4021 CTL_PAGE_CURRENT]; 4022 scsi_ulto2b(lun->be_lun->blocksize, 4023 format_page->bytes_per_sector); 4024 4025 format_page = &lun->mode_pages.format_page[ 4026 CTL_PAGE_DEFAULT]; 4027 scsi_ulto2b(lun->be_lun->blocksize, 4028 format_page->bytes_per_sector); 4029 4030 format_page = &lun->mode_pages.format_page[ 4031 CTL_PAGE_SAVED]; 4032 scsi_ulto2b(lun->be_lun->blocksize, 4033 format_page->bytes_per_sector); 4034 4035 page_index->page_data = 4036 (uint8_t *)lun->mode_pages.format_page; 4037 break; 4038 } 4039 case SMS_RIGID_DISK_PAGE: { 4040 struct scsi_rigid_disk_page *rigid_disk_page; 4041 uint32_t sectors_per_cylinder; 4042 uint64_t cylinders; 4043#ifndef __XSCALE__ 4044 int shift; 4045#endif /* !__XSCALE__ */ 4046 4047 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4048 panic("invalid subpage value %d", 4049 page_index->subpage); 4050 4051 /* 4052 * Rotation rate and sectors per track are set 4053 * above. We calculate the cylinders here based on 4054 * capacity. Due to the number of heads and 4055 * sectors per track we're using, smaller arrays 4056 * may turn out to have 0 cylinders. Linux and 4057 * FreeBSD don't pay attention to these mode pages 4058 * to figure out capacity, but Solaris does. It 4059 * seems to deal with 0 cylinders just fine, and 4060 * works out a fake geometry based on the capacity. 4061 */ 4062 memcpy(&lun->mode_pages.rigid_disk_page[ 4063 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4064 sizeof(rigid_disk_page_default)); 4065 memcpy(&lun->mode_pages.rigid_disk_page[ 4066 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4067 sizeof(rigid_disk_page_changeable)); 4068 4069 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4070 CTL_DEFAULT_HEADS; 4071 4072 /* 4073 * The divide method here will be more accurate, 4074 * probably, but results in floating point being 4075 * used in the kernel on i386 (__udivdi3()). On the 4076 * XScale, though, __udivdi3() is implemented in 4077 * software. 4078 * 4079 * The shift method for cylinder calculation is 4080 * accurate if sectors_per_cylinder is a power of 4081 * 2. Otherwise it might be slightly off -- you 4082 * might have a bit of a truncation problem. 4083 */ 4084#ifdef __XSCALE__ 4085 cylinders = (lun->be_lun->maxlba + 1) / 4086 sectors_per_cylinder; 4087#else 4088 for (shift = 31; shift > 0; shift--) { 4089 if (sectors_per_cylinder & (1 << shift)) 4090 break; 4091 } 4092 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4093#endif 4094 4095 /* 4096 * We've basically got 3 bytes, or 24 bits for the 4097 * cylinder size in the mode page. If we're over, 4098 * just round down to 2^24. 4099 */ 4100 if (cylinders > 0xffffff) 4101 cylinders = 0xffffff; 4102 4103 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4104 CTL_PAGE_DEFAULT]; 4105 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4106 4107 if ((value = ctl_get_opt(&lun->be_lun->options, 4108 "rpm")) != NULL) { 4109 scsi_ulto2b(strtol(value, NULL, 0), 4110 rigid_disk_page->rotation_rate); 4111 } 4112 4113 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4114 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4115 sizeof(rigid_disk_page_default)); 4116 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4117 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4118 sizeof(rigid_disk_page_default)); 4119 4120 page_index->page_data = 4121 (uint8_t *)lun->mode_pages.rigid_disk_page; 4122 break; 4123 } 4124 case SMS_CACHING_PAGE: { 4125 struct scsi_caching_page *caching_page; 4126 4127 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4128 panic("invalid subpage value %d", 4129 page_index->subpage); 4130 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4131 &caching_page_default, 4132 sizeof(caching_page_default)); 4133 memcpy(&lun->mode_pages.caching_page[ 4134 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4135 sizeof(caching_page_changeable)); 4136 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4137 &caching_page_default, 4138 sizeof(caching_page_default)); 4139 caching_page = &lun->mode_pages.caching_page[ 4140 CTL_PAGE_SAVED]; 4141 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4142 if (value != NULL && strcmp(value, "off") == 0) 4143 caching_page->flags1 &= ~SCP_WCE; 4144 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4145 if (value != NULL && strcmp(value, "off") == 0) 4146 caching_page->flags1 |= SCP_RCD; 4147 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4148 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4149 sizeof(caching_page_default)); 4150 page_index->page_data = 4151 (uint8_t *)lun->mode_pages.caching_page; 4152 break; 4153 } 4154 case SMS_CONTROL_MODE_PAGE: { 4155 struct scsi_control_page *control_page; 4156 4157 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4158 panic("invalid subpage value %d", 4159 page_index->subpage); 4160 4161 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4162 &control_page_default, 4163 sizeof(control_page_default)); 4164 memcpy(&lun->mode_pages.control_page[ 4165 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4166 sizeof(control_page_changeable)); 4167 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4168 &control_page_default, 4169 sizeof(control_page_default)); 4170 control_page = &lun->mode_pages.control_page[ 4171 CTL_PAGE_SAVED]; 4172 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4173 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4174 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4175 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4176 } 4177 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4178 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4179 sizeof(control_page_default)); 4180 page_index->page_data = 4181 (uint8_t *)lun->mode_pages.control_page; 4182 break; 4183 4184 } 4185 case SMS_INFO_EXCEPTIONS_PAGE: { 4186 switch (page_index->subpage) { 4187 case SMS_SUBPAGE_PAGE_0: 4188 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4189 &ie_page_default, 4190 sizeof(ie_page_default)); 4191 memcpy(&lun->mode_pages.ie_page[ 4192 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4193 sizeof(ie_page_changeable)); 4194 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4195 &ie_page_default, 4196 sizeof(ie_page_default)); 4197 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4198 &ie_page_default, 4199 sizeof(ie_page_default)); 4200 page_index->page_data = 4201 (uint8_t *)lun->mode_pages.ie_page; 4202 break; 4203 case 0x02: { 4204 struct ctl_logical_block_provisioning_page *page; 4205 4206 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4207 &lbp_page_default, 4208 sizeof(lbp_page_default)); 4209 memcpy(&lun->mode_pages.lbp_page[ 4210 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4211 sizeof(lbp_page_changeable)); 4212 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4213 &lbp_page_default, 4214 sizeof(lbp_page_default)); 4215 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4216 value = ctl_get_opt(&lun->be_lun->options, 4217 "avail-threshold"); 4218 if (value != NULL && 4219 ctl_expand_number(value, &ival) == 0) { 4220 page->descr[0].flags |= SLBPPD_ENABLED | 4221 SLBPPD_ARMING_DEC; 4222 if (lun->be_lun->blocksize) 4223 ival /= lun->be_lun->blocksize; 4224 else 4225 ival /= 512; 4226 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4227 page->descr[0].count); 4228 } 4229 value = ctl_get_opt(&lun->be_lun->options, 4230 "used-threshold"); 4231 if (value != NULL && 4232 ctl_expand_number(value, &ival) == 0) { 4233 page->descr[1].flags |= SLBPPD_ENABLED | 4234 SLBPPD_ARMING_INC; 4235 if (lun->be_lun->blocksize) 4236 ival /= lun->be_lun->blocksize; 4237 else 4238 ival /= 512; 4239 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4240 page->descr[1].count); 4241 } 4242 value = ctl_get_opt(&lun->be_lun->options, 4243 "pool-avail-threshold"); 4244 if (value != NULL && 4245 ctl_expand_number(value, &ival) == 0) { 4246 page->descr[2].flags |= SLBPPD_ENABLED | 4247 SLBPPD_ARMING_DEC; 4248 if (lun->be_lun->blocksize) 4249 ival /= lun->be_lun->blocksize; 4250 else 4251 ival /= 512; 4252 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4253 page->descr[2].count); 4254 } 4255 value = ctl_get_opt(&lun->be_lun->options, 4256 "pool-used-threshold"); 4257 if (value != NULL && 4258 ctl_expand_number(value, &ival) == 0) { 4259 page->descr[3].flags |= SLBPPD_ENABLED | 4260 SLBPPD_ARMING_INC; 4261 if (lun->be_lun->blocksize) 4262 ival /= lun->be_lun->blocksize; 4263 else 4264 ival /= 512; 4265 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4266 page->descr[3].count); 4267 } 4268 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4269 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4270 sizeof(lbp_page_default)); 4271 page_index->page_data = 4272 (uint8_t *)lun->mode_pages.lbp_page; 4273 }} 4274 break; 4275 } 4276 case SMS_VENDOR_SPECIFIC_PAGE:{ 4277 switch (page_index->subpage) { 4278 case DBGCNF_SUBPAGE_CODE: { 4279 struct copan_debugconf_subpage *current_page, 4280 *saved_page; 4281 4282 memcpy(&lun->mode_pages.debugconf_subpage[ 4283 CTL_PAGE_CURRENT], 4284 &debugconf_page_default, 4285 sizeof(debugconf_page_default)); 4286 memcpy(&lun->mode_pages.debugconf_subpage[ 4287 CTL_PAGE_CHANGEABLE], 4288 &debugconf_page_changeable, 4289 sizeof(debugconf_page_changeable)); 4290 memcpy(&lun->mode_pages.debugconf_subpage[ 4291 CTL_PAGE_DEFAULT], 4292 &debugconf_page_default, 4293 sizeof(debugconf_page_default)); 4294 memcpy(&lun->mode_pages.debugconf_subpage[ 4295 CTL_PAGE_SAVED], 4296 &debugconf_page_default, 4297 sizeof(debugconf_page_default)); 4298 page_index->page_data = 4299 (uint8_t *)lun->mode_pages.debugconf_subpage; 4300 4301 current_page = (struct copan_debugconf_subpage *) 4302 (page_index->page_data + 4303 (page_index->page_len * 4304 CTL_PAGE_CURRENT)); 4305 saved_page = (struct copan_debugconf_subpage *) 4306 (page_index->page_data + 4307 (page_index->page_len * 4308 CTL_PAGE_SAVED)); 4309 break; 4310 } 4311 default: 4312 panic("invalid subpage value %d", 4313 page_index->subpage); 4314 break; 4315 } 4316 break; 4317 } 4318 default: 4319 panic("invalid page value %d", 4320 page_index->page_code & SMPH_PC_MASK); 4321 break; 4322 } 4323 } 4324 4325 return (CTL_RETVAL_COMPLETE); 4326} 4327 4328static int 4329ctl_init_log_page_index(struct ctl_lun *lun) 4330{ 4331 struct ctl_page_index *page_index; 4332 int i, j, k, prev; 4333 4334 memcpy(&lun->log_pages.index, log_page_index_template, 4335 sizeof(log_page_index_template)); 4336 4337 prev = -1; 4338 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4339 4340 page_index = &lun->log_pages.index[i]; 4341 /* 4342 * If this is a disk-only mode page, there's no point in 4343 * setting it up. For some pages, we have to have some 4344 * basic information about the disk in order to calculate the 4345 * mode page data. 4346 */ 4347 if ((lun->be_lun->lun_type != T_DIRECT) 4348 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4349 continue; 4350 4351 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4352 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 4353 lun->backend->lun_attr == NULL)) 4354 continue; 4355 4356 if (page_index->page_code != prev) { 4357 lun->log_pages.pages_page[j] = page_index->page_code; 4358 prev = page_index->page_code; 4359 j++; 4360 } 4361 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4362 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4363 k++; 4364 } 4365 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4366 lun->log_pages.index[0].page_len = j; 4367 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4368 lun->log_pages.index[1].page_len = k * 2; 4369 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4370 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4371 4372 return (CTL_RETVAL_COMPLETE); 4373} 4374 4375static int 4376hex2bin(const char *str, uint8_t *buf, int buf_size) 4377{ 4378 int i; 4379 u_char c; 4380 4381 memset(buf, 0, buf_size); 4382 while (isspace(str[0])) 4383 str++; 4384 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4385 str += 2; 4386 buf_size *= 2; 4387 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4388 c = str[i]; 4389 if (isdigit(c)) 4390 c -= '0'; 4391 else if (isalpha(c)) 4392 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4393 else 4394 break; 4395 if (c >= 16) 4396 break; 4397 if ((i & 1) == 0) 4398 buf[i / 2] |= (c << 4); 4399 else 4400 buf[i / 2] |= c; 4401 } 4402 return ((i + 1) / 2); 4403} 4404 4405/* 4406 * LUN allocation. 4407 * 4408 * Requirements: 4409 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4410 * wants us to allocate the LUN and he can block. 4411 * - ctl_softc is always set 4412 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4413 * 4414 * Returns 0 for success, non-zero (errno) for failure. 4415 */ 4416static int 4417ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4418 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4419{ 4420 struct ctl_lun *nlun, *lun; 4421 struct ctl_port *port; 4422 struct scsi_vpd_id_descriptor *desc; 4423 struct scsi_vpd_id_t10 *t10id; 4424 const char *eui, *naa, *scsiname, *vendor, *value; 4425 int lun_number, i, lun_malloced; 4426 int devidlen, idlen1, idlen2 = 0, len; 4427 4428 if (be_lun == NULL) 4429 return (EINVAL); 4430 4431 /* 4432 * We currently only support Direct Access or Processor LUN types. 4433 */ 4434 switch (be_lun->lun_type) { 4435 case T_DIRECT: 4436 break; 4437 case T_PROCESSOR: 4438 break; 4439 case T_SEQUENTIAL: 4440 case T_CHANGER: 4441 default: 4442 be_lun->lun_config_status(be_lun->be_lun, 4443 CTL_LUN_CONFIG_FAILURE); 4444 break; 4445 } 4446 if (ctl_lun == NULL) { 4447 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4448 lun_malloced = 1; 4449 } else { 4450 lun_malloced = 0; 4451 lun = ctl_lun; 4452 } 4453 4454 memset(lun, 0, sizeof(*lun)); 4455 if (lun_malloced) 4456 lun->flags = CTL_LUN_MALLOCED; 4457 4458 /* Generate LUN ID. */ 4459 devidlen = max(CTL_DEVID_MIN_LEN, 4460 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4461 idlen1 = sizeof(*t10id) + devidlen; 4462 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4463 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4464 if (scsiname != NULL) { 4465 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4466 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4467 } 4468 eui = ctl_get_opt(&be_lun->options, "eui"); 4469 if (eui != NULL) { 4470 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4471 } 4472 naa = ctl_get_opt(&be_lun->options, "naa"); 4473 if (naa != NULL) { 4474 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4475 } 4476 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4477 M_CTL, M_WAITOK | M_ZERO); 4478 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4479 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4480 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4481 desc->length = idlen1; 4482 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4483 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4484 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4485 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4486 } else { 4487 strncpy(t10id->vendor, vendor, 4488 min(sizeof(t10id->vendor), strlen(vendor))); 4489 } 4490 strncpy((char *)t10id->vendor_spec_id, 4491 (char *)be_lun->device_id, devidlen); 4492 if (scsiname != NULL) { 4493 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4494 desc->length); 4495 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4496 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4497 SVPD_ID_TYPE_SCSI_NAME; 4498 desc->length = idlen2; 4499 strlcpy(desc->identifier, scsiname, idlen2); 4500 } 4501 if (eui != NULL) { 4502 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4503 desc->length); 4504 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4505 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4506 SVPD_ID_TYPE_EUI64; 4507 desc->length = hex2bin(eui, desc->identifier, 16); 4508 desc->length = desc->length > 12 ? 16 : 4509 (desc->length > 8 ? 12 : 8); 4510 len -= 16 - desc->length; 4511 } 4512 if (naa != NULL) { 4513 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4514 desc->length); 4515 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4516 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4517 SVPD_ID_TYPE_NAA; 4518 desc->length = hex2bin(naa, desc->identifier, 16); 4519 desc->length = desc->length > 8 ? 16 : 8; 4520 len -= 16 - desc->length; 4521 } 4522 lun->lun_devid->len = len; 4523 4524 mtx_lock(&ctl_softc->ctl_lock); 4525 /* 4526 * See if the caller requested a particular LUN number. If so, see 4527 * if it is available. Otherwise, allocate the first available LUN. 4528 */ 4529 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4530 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4531 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4532 mtx_unlock(&ctl_softc->ctl_lock); 4533 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4534 printf("ctl: requested LUN ID %d is higher " 4535 "than CTL_MAX_LUNS - 1 (%d)\n", 4536 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4537 } else { 4538 /* 4539 * XXX KDM return an error, or just assign 4540 * another LUN ID in this case?? 4541 */ 4542 printf("ctl: requested LUN ID %d is already " 4543 "in use\n", be_lun->req_lun_id); 4544 } 4545 if (lun->flags & CTL_LUN_MALLOCED) 4546 free(lun, M_CTL); 4547 be_lun->lun_config_status(be_lun->be_lun, 4548 CTL_LUN_CONFIG_FAILURE); 4549 return (ENOSPC); 4550 } 4551 lun_number = be_lun->req_lun_id; 4552 } else { 4553 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4554 if (lun_number == -1) { 4555 mtx_unlock(&ctl_softc->ctl_lock); 4556 printf("ctl: can't allocate LUN on target %ju, out of " 4557 "LUNs\n", (uintmax_t)target_id.id); 4558 if (lun->flags & CTL_LUN_MALLOCED) 4559 free(lun, M_CTL); 4560 be_lun->lun_config_status(be_lun->be_lun, 4561 CTL_LUN_CONFIG_FAILURE); 4562 return (ENOSPC); 4563 } 4564 } 4565 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4566 4567 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4568 lun->target = target_id; 4569 lun->lun = lun_number; 4570 lun->be_lun = be_lun; 4571 /* 4572 * The processor LUN is always enabled. Disk LUNs come on line 4573 * disabled, and must be enabled by the backend. 4574 */ 4575 lun->flags |= CTL_LUN_DISABLED; 4576 lun->backend = be_lun->be; 4577 be_lun->ctl_lun = lun; 4578 be_lun->lun_id = lun_number; 4579 atomic_add_int(&be_lun->be->num_luns, 1); 4580 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4581 lun->flags |= CTL_LUN_OFFLINE; 4582 4583 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4584 lun->flags |= CTL_LUN_STOPPED; 4585 4586 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4587 lun->flags |= CTL_LUN_INOPERABLE; 4588 4589 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4590 lun->flags |= CTL_LUN_PRIMARY_SC; 4591 4592 value = ctl_get_opt(&be_lun->options, "readonly"); 4593 if (value != NULL && strcmp(value, "on") == 0) 4594 lun->flags |= CTL_LUN_READONLY; 4595 4596 lun->ctl_softc = ctl_softc; 4597 TAILQ_INIT(&lun->ooa_queue); 4598 TAILQ_INIT(&lun->blocked_queue); 4599 STAILQ_INIT(&lun->error_list); 4600 ctl_tpc_lun_init(lun); 4601 4602 /* 4603 * Initialize the mode and log page index. 4604 */ 4605 ctl_init_page_index(lun); 4606 ctl_init_log_page_index(lun); 4607 4608 /* 4609 * Now, before we insert this lun on the lun list, set the lun 4610 * inventory changed UA for all other luns. 4611 */ 4612 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4613 mtx_lock(&nlun->lun_lock); 4614 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4615 mtx_unlock(&nlun->lun_lock); 4616 } 4617 4618 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4619 4620 ctl_softc->ctl_luns[lun_number] = lun; 4621 4622 ctl_softc->num_luns++; 4623 4624 /* Setup statistics gathering */ 4625 lun->stats.device_type = be_lun->lun_type; 4626 lun->stats.lun_number = lun_number; 4627 if (lun->stats.device_type == T_DIRECT) 4628 lun->stats.blocksize = be_lun->blocksize; 4629 else 4630 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4631 for (i = 0;i < CTL_MAX_PORTS;i++) 4632 lun->stats.ports[i].targ_port = i; 4633 4634 mtx_unlock(&ctl_softc->ctl_lock); 4635 4636 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4637 4638 /* 4639 * Run through each registered FETD and bring it online if it isn't 4640 * already. Enable the target ID if it hasn't been enabled, and 4641 * enable this particular LUN. 4642 */ 4643 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4644 int retval; 4645 4646 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4647 if (retval != 0) { 4648 printf("ctl_alloc_lun: FETD %s port %d returned error " 4649 "%d for lun_enable on target %ju lun %d\n", 4650 port->port_name, port->targ_port, retval, 4651 (uintmax_t)target_id.id, lun_number); 4652 } else 4653 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4654 } 4655 return (0); 4656} 4657 4658/* 4659 * Delete a LUN. 4660 * Assumptions: 4661 * - LUN has already been marked invalid and any pending I/O has been taken 4662 * care of. 4663 */ 4664static int 4665ctl_free_lun(struct ctl_lun *lun) 4666{ 4667 struct ctl_softc *softc; 4668#if 0 4669 struct ctl_port *port; 4670#endif 4671 struct ctl_lun *nlun; 4672 int i; 4673 4674 softc = lun->ctl_softc; 4675 4676 mtx_assert(&softc->ctl_lock, MA_OWNED); 4677 4678 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4679 4680 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4681 4682 softc->ctl_luns[lun->lun] = NULL; 4683 4684 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4685 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4686 4687 softc->num_luns--; 4688 4689 /* 4690 * XXX KDM this scheme only works for a single target/multiple LUN 4691 * setup. It needs to be revamped for a multiple target scheme. 4692 * 4693 * XXX KDM this results in port->lun_disable() getting called twice, 4694 * once when ctl_disable_lun() is called, and a second time here. 4695 * We really need to re-think the LUN disable semantics. There 4696 * should probably be several steps/levels to LUN removal: 4697 * - disable 4698 * - invalidate 4699 * - free 4700 * 4701 * Right now we only have a disable method when communicating to 4702 * the front end ports, at least for individual LUNs. 4703 */ 4704#if 0 4705 STAILQ_FOREACH(port, &softc->port_list, links) { 4706 int retval; 4707 4708 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4709 lun->lun); 4710 if (retval != 0) { 4711 printf("ctl_free_lun: FETD %s port %d returned error " 4712 "%d for lun_disable on target %ju lun %jd\n", 4713 port->port_name, port->targ_port, retval, 4714 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4715 } 4716 4717 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4718 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4719 4720 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4721 if (retval != 0) { 4722 printf("ctl_free_lun: FETD %s port %d " 4723 "returned error %d for targ_disable on " 4724 "target %ju\n", port->port_name, 4725 port->targ_port, retval, 4726 (uintmax_t)lun->target.id); 4727 } else 4728 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4729 4730 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4731 continue; 4732 4733#if 0 4734 port->port_offline(port->onoff_arg); 4735 port->status &= ~CTL_PORT_STATUS_ONLINE; 4736#endif 4737 } 4738 } 4739#endif 4740 4741 /* 4742 * Tell the backend to free resources, if this LUN has a backend. 4743 */ 4744 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4745 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4746 4747 ctl_tpc_lun_shutdown(lun); 4748 mtx_destroy(&lun->lun_lock); 4749 free(lun->lun_devid, M_CTL); 4750 for (i = 0; i < CTL_MAX_PORTS; i++) 4751 free(lun->pending_ua[i], M_CTL); 4752 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) 4753 free(lun->pr_keys[i], M_CTL); 4754 free(lun->write_buffer, M_CTL); 4755 if (lun->flags & CTL_LUN_MALLOCED) 4756 free(lun, M_CTL); 4757 4758 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4759 mtx_lock(&nlun->lun_lock); 4760 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4761 mtx_unlock(&nlun->lun_lock); 4762 } 4763 4764 return (0); 4765} 4766 4767static void 4768ctl_create_lun(struct ctl_be_lun *be_lun) 4769{ 4770 struct ctl_softc *ctl_softc; 4771 4772 ctl_softc = control_softc; 4773 4774 /* 4775 * ctl_alloc_lun() should handle all potential failure cases. 4776 */ 4777 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4778} 4779 4780int 4781ctl_add_lun(struct ctl_be_lun *be_lun) 4782{ 4783 struct ctl_softc *ctl_softc = control_softc; 4784 4785 mtx_lock(&ctl_softc->ctl_lock); 4786 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4787 mtx_unlock(&ctl_softc->ctl_lock); 4788 wakeup(&ctl_softc->pending_lun_queue); 4789 4790 return (0); 4791} 4792 4793int 4794ctl_enable_lun(struct ctl_be_lun *be_lun) 4795{ 4796 struct ctl_softc *ctl_softc; 4797 struct ctl_port *port, *nport; 4798 struct ctl_lun *lun; 4799 int retval; 4800 4801 ctl_softc = control_softc; 4802 4803 lun = (struct ctl_lun *)be_lun->ctl_lun; 4804 4805 mtx_lock(&ctl_softc->ctl_lock); 4806 mtx_lock(&lun->lun_lock); 4807 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4808 /* 4809 * eh? Why did we get called if the LUN is already 4810 * enabled? 4811 */ 4812 mtx_unlock(&lun->lun_lock); 4813 mtx_unlock(&ctl_softc->ctl_lock); 4814 return (0); 4815 } 4816 lun->flags &= ~CTL_LUN_DISABLED; 4817 mtx_unlock(&lun->lun_lock); 4818 4819 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4820 nport = STAILQ_NEXT(port, links); 4821 4822 /* 4823 * Drop the lock while we call the FETD's enable routine. 4824 * This can lead to a callback into CTL (at least in the 4825 * case of the internal initiator frontend. 4826 */ 4827 mtx_unlock(&ctl_softc->ctl_lock); 4828 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4829 mtx_lock(&ctl_softc->ctl_lock); 4830 if (retval != 0) { 4831 printf("%s: FETD %s port %d returned error " 4832 "%d for lun_enable on target %ju lun %jd\n", 4833 __func__, port->port_name, port->targ_port, retval, 4834 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4835 } 4836#if 0 4837 else { 4838 /* NOTE: TODO: why does lun enable affect port status? */ 4839 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4840 } 4841#endif 4842 } 4843 4844 mtx_unlock(&ctl_softc->ctl_lock); 4845 4846 return (0); 4847} 4848 4849int 4850ctl_disable_lun(struct ctl_be_lun *be_lun) 4851{ 4852 struct ctl_softc *ctl_softc; 4853 struct ctl_port *port; 4854 struct ctl_lun *lun; 4855 int retval; 4856 4857 ctl_softc = control_softc; 4858 4859 lun = (struct ctl_lun *)be_lun->ctl_lun; 4860 4861 mtx_lock(&ctl_softc->ctl_lock); 4862 mtx_lock(&lun->lun_lock); 4863 if (lun->flags & CTL_LUN_DISABLED) { 4864 mtx_unlock(&lun->lun_lock); 4865 mtx_unlock(&ctl_softc->ctl_lock); 4866 return (0); 4867 } 4868 lun->flags |= CTL_LUN_DISABLED; 4869 mtx_unlock(&lun->lun_lock); 4870 4871 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4872 mtx_unlock(&ctl_softc->ctl_lock); 4873 /* 4874 * Drop the lock before we call the frontend's disable 4875 * routine, to avoid lock order reversals. 4876 * 4877 * XXX KDM what happens if the frontend list changes while 4878 * we're traversing it? It's unlikely, but should be handled. 4879 */ 4880 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4881 lun->lun); 4882 mtx_lock(&ctl_softc->ctl_lock); 4883 if (retval != 0) { 4884 printf("ctl_alloc_lun: FETD %s port %d returned error " 4885 "%d for lun_disable on target %ju lun %jd\n", 4886 port->port_name, port->targ_port, retval, 4887 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4888 } 4889 } 4890 4891 mtx_unlock(&ctl_softc->ctl_lock); 4892 4893 return (0); 4894} 4895 4896int 4897ctl_start_lun(struct ctl_be_lun *be_lun) 4898{ 4899 struct ctl_softc *ctl_softc; 4900 struct ctl_lun *lun; 4901 4902 ctl_softc = control_softc; 4903 4904 lun = (struct ctl_lun *)be_lun->ctl_lun; 4905 4906 mtx_lock(&lun->lun_lock); 4907 lun->flags &= ~CTL_LUN_STOPPED; 4908 mtx_unlock(&lun->lun_lock); 4909 4910 return (0); 4911} 4912 4913int 4914ctl_stop_lun(struct ctl_be_lun *be_lun) 4915{ 4916 struct ctl_softc *ctl_softc; 4917 struct ctl_lun *lun; 4918 4919 ctl_softc = control_softc; 4920 4921 lun = (struct ctl_lun *)be_lun->ctl_lun; 4922 4923 mtx_lock(&lun->lun_lock); 4924 lun->flags |= CTL_LUN_STOPPED; 4925 mtx_unlock(&lun->lun_lock); 4926 4927 return (0); 4928} 4929 4930int 4931ctl_lun_offline(struct ctl_be_lun *be_lun) 4932{ 4933 struct ctl_softc *ctl_softc; 4934 struct ctl_lun *lun; 4935 4936 ctl_softc = control_softc; 4937 4938 lun = (struct ctl_lun *)be_lun->ctl_lun; 4939 4940 mtx_lock(&lun->lun_lock); 4941 lun->flags |= CTL_LUN_OFFLINE; 4942 mtx_unlock(&lun->lun_lock); 4943 4944 return (0); 4945} 4946 4947int 4948ctl_lun_online(struct ctl_be_lun *be_lun) 4949{ 4950 struct ctl_softc *ctl_softc; 4951 struct ctl_lun *lun; 4952 4953 ctl_softc = control_softc; 4954 4955 lun = (struct ctl_lun *)be_lun->ctl_lun; 4956 4957 mtx_lock(&lun->lun_lock); 4958 lun->flags &= ~CTL_LUN_OFFLINE; 4959 mtx_unlock(&lun->lun_lock); 4960 4961 return (0); 4962} 4963 4964int 4965ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4966{ 4967 struct ctl_softc *ctl_softc; 4968 struct ctl_lun *lun; 4969 4970 ctl_softc = control_softc; 4971 4972 lun = (struct ctl_lun *)be_lun->ctl_lun; 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(&ctl_softc->ctl_lock); 4996 ctl_free_lun(lun); 4997 mtx_unlock(&ctl_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_softc *ctl_softc; 5008 struct ctl_lun *lun; 5009 5010 ctl_softc = control_softc; 5011 lun = (struct ctl_lun *)be_lun->ctl_lun; 5012 5013 mtx_lock(&lun->lun_lock); 5014 lun->flags |= CTL_LUN_INOPERABLE; 5015 mtx_unlock(&lun->lun_lock); 5016 5017 return (0); 5018} 5019 5020int 5021ctl_lun_operable(struct ctl_be_lun *be_lun) 5022{ 5023 struct ctl_softc *ctl_softc; 5024 struct ctl_lun *lun; 5025 5026 ctl_softc = control_softc; 5027 lun = (struct ctl_lun *)be_lun->ctl_lun; 5028 5029 mtx_lock(&lun->lun_lock); 5030 lun->flags &= ~CTL_LUN_INOPERABLE; 5031 mtx_unlock(&lun->lun_lock); 5032 5033 return (0); 5034} 5035 5036void 5037ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5038{ 5039 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5040 5041 mtx_lock(&lun->lun_lock); 5042 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 5043 mtx_unlock(&lun->lun_lock); 5044} 5045 5046/* 5047 * Backend "memory move is complete" callback for requests that never 5048 * make it down to say RAIDCore's configuration code. 5049 */ 5050int 5051ctl_config_move_done(union ctl_io *io) 5052{ 5053 int retval; 5054 5055 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5056 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5057 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5058 5059 if ((io->io_hdr.port_status != 0) && 5060 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5061 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5062 /* 5063 * For hardware error sense keys, the sense key 5064 * specific value is defined to be a retry count, 5065 * but we use it to pass back an internal FETD 5066 * error code. XXX KDM Hopefully the FETD is only 5067 * using 16 bits for an error code, since that's 5068 * all the space we have in the sks field. 5069 */ 5070 ctl_set_internal_failure(&io->scsiio, 5071 /*sks_valid*/ 1, 5072 /*retry_count*/ 5073 io->io_hdr.port_status); 5074 } 5075 5076 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5077 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5078 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5079 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5080 /* 5081 * XXX KDM just assuming a single pointer here, and not a 5082 * S/G list. If we start using S/G lists for config data, 5083 * we'll need to know how to clean them up here as well. 5084 */ 5085 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5086 free(io->scsiio.kern_data_ptr, M_CTL); 5087 ctl_done(io); 5088 retval = CTL_RETVAL_COMPLETE; 5089 } else { 5090 /* 5091 * XXX KDM now we need to continue data movement. Some 5092 * options: 5093 * - call ctl_scsiio() again? We don't do this for data 5094 * writes, because for those at least we know ahead of 5095 * time where the write will go and how long it is. For 5096 * config writes, though, that information is largely 5097 * contained within the write itself, thus we need to 5098 * parse out the data again. 5099 * 5100 * - Call some other function once the data is in? 5101 */ 5102 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5103 ctl_data_print(io); 5104 5105 /* 5106 * XXX KDM call ctl_scsiio() again for now, and check flag 5107 * bits to see whether we're allocated or not. 5108 */ 5109 retval = ctl_scsiio(&io->scsiio); 5110 } 5111 return (retval); 5112} 5113 5114/* 5115 * This gets called by a backend driver when it is done with a 5116 * data_submit method. 5117 */ 5118void 5119ctl_data_submit_done(union ctl_io *io) 5120{ 5121 /* 5122 * If the IO_CONT flag is set, we need to call the supplied 5123 * function to continue processing the I/O, instead of completing 5124 * the I/O just yet. 5125 * 5126 * If there is an error, though, we don't want to keep processing. 5127 * Instead, just send status back to the initiator. 5128 */ 5129 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5130 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5131 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5132 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5133 io->scsiio.io_cont(io); 5134 return; 5135 } 5136 ctl_done(io); 5137} 5138 5139/* 5140 * This gets called by a backend driver when it is done with a 5141 * configuration write. 5142 */ 5143void 5144ctl_config_write_done(union ctl_io *io) 5145{ 5146 uint8_t *buf; 5147 5148 /* 5149 * If the IO_CONT flag is set, we need to call the supplied 5150 * function to continue processing the I/O, instead of completing 5151 * the I/O just yet. 5152 * 5153 * If there is an error, though, we don't want to keep processing. 5154 * Instead, just send status back to the initiator. 5155 */ 5156 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5157 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5158 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5159 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5160 io->scsiio.io_cont(io); 5161 return; 5162 } 5163 /* 5164 * Since a configuration write can be done for commands that actually 5165 * have data allocated, like write buffer, and commands that have 5166 * no data, like start/stop unit, we need to check here. 5167 */ 5168 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5169 buf = io->scsiio.kern_data_ptr; 5170 else 5171 buf = NULL; 5172 ctl_done(io); 5173 if (buf) 5174 free(buf, M_CTL); 5175} 5176 5177void 5178ctl_config_read_done(union ctl_io *io) 5179{ 5180 uint8_t *buf; 5181 5182 /* 5183 * If there is some error -- we are done, skip data transfer. 5184 */ 5185 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5186 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5187 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5188 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5189 buf = io->scsiio.kern_data_ptr; 5190 else 5191 buf = NULL; 5192 ctl_done(io); 5193 if (buf) 5194 free(buf, M_CTL); 5195 return; 5196 } 5197 5198 /* 5199 * If the IO_CONT flag is set, we need to call the supplied 5200 * function to continue processing the I/O, instead of completing 5201 * the I/O just yet. 5202 */ 5203 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5204 io->scsiio.io_cont(io); 5205 return; 5206 } 5207 5208 ctl_datamove(io); 5209} 5210 5211/* 5212 * SCSI release command. 5213 */ 5214int 5215ctl_scsi_release(struct ctl_scsiio *ctsio) 5216{ 5217 int length, longid, thirdparty_id, resv_id; 5218 struct ctl_softc *ctl_softc; 5219 struct ctl_lun *lun; 5220 uint32_t residx; 5221 5222 length = 0; 5223 resv_id = 0; 5224 5225 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5226 5227 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5228 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5229 ctl_softc = control_softc; 5230 5231 switch (ctsio->cdb[0]) { 5232 case RELEASE_10: { 5233 struct scsi_release_10 *cdb; 5234 5235 cdb = (struct scsi_release_10 *)ctsio->cdb; 5236 5237 if (cdb->byte2 & SR10_LONGID) 5238 longid = 1; 5239 else 5240 thirdparty_id = cdb->thirdparty_id; 5241 5242 resv_id = cdb->resv_id; 5243 length = scsi_2btoul(cdb->length); 5244 break; 5245 } 5246 } 5247 5248 5249 /* 5250 * XXX KDM right now, we only support LUN reservation. We don't 5251 * support 3rd party reservations, or extent reservations, which 5252 * might actually need the parameter list. If we've gotten this 5253 * far, we've got a LUN reservation. Anything else got kicked out 5254 * above. So, according to SPC, ignore the length. 5255 */ 5256 length = 0; 5257 5258 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5259 && (length > 0)) { 5260 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5261 ctsio->kern_data_len = length; 5262 ctsio->kern_total_len = length; 5263 ctsio->kern_data_resid = 0; 5264 ctsio->kern_rel_offset = 0; 5265 ctsio->kern_sg_entries = 0; 5266 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5267 ctsio->be_move_done = ctl_config_move_done; 5268 ctl_datamove((union ctl_io *)ctsio); 5269 5270 return (CTL_RETVAL_COMPLETE); 5271 } 5272 5273 if (length > 0) 5274 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5275 5276 mtx_lock(&lun->lun_lock); 5277 5278 /* 5279 * According to SPC, it is not an error for an intiator to attempt 5280 * to release a reservation on a LUN that isn't reserved, or that 5281 * is reserved by another initiator. The reservation can only be 5282 * released, though, by the initiator who made it or by one of 5283 * several reset type events. 5284 */ 5285 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5286 lun->flags &= ~CTL_LUN_RESERVED; 5287 5288 mtx_unlock(&lun->lun_lock); 5289 5290 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5291 free(ctsio->kern_data_ptr, M_CTL); 5292 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5293 } 5294 5295 ctl_set_success(ctsio); 5296 ctl_done((union ctl_io *)ctsio); 5297 return (CTL_RETVAL_COMPLETE); 5298} 5299 5300int 5301ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5302{ 5303 int extent, thirdparty, longid; 5304 int resv_id, length; 5305 uint64_t thirdparty_id; 5306 struct ctl_softc *ctl_softc; 5307 struct ctl_lun *lun; 5308 uint32_t residx; 5309 5310 extent = 0; 5311 thirdparty = 0; 5312 longid = 0; 5313 resv_id = 0; 5314 length = 0; 5315 thirdparty_id = 0; 5316 5317 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5318 5319 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5320 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5321 ctl_softc = control_softc; 5322 5323 switch (ctsio->cdb[0]) { 5324 case RESERVE_10: { 5325 struct scsi_reserve_10 *cdb; 5326 5327 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5328 5329 if (cdb->byte2 & SR10_LONGID) 5330 longid = 1; 5331 else 5332 thirdparty_id = cdb->thirdparty_id; 5333 5334 resv_id = cdb->resv_id; 5335 length = scsi_2btoul(cdb->length); 5336 break; 5337 } 5338 } 5339 5340 /* 5341 * XXX KDM right now, we only support LUN reservation. We don't 5342 * support 3rd party reservations, or extent reservations, which 5343 * might actually need the parameter list. If we've gotten this 5344 * far, we've got a LUN reservation. Anything else got kicked out 5345 * above. So, according to SPC, ignore the length. 5346 */ 5347 length = 0; 5348 5349 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5350 && (length > 0)) { 5351 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5352 ctsio->kern_data_len = length; 5353 ctsio->kern_total_len = length; 5354 ctsio->kern_data_resid = 0; 5355 ctsio->kern_rel_offset = 0; 5356 ctsio->kern_sg_entries = 0; 5357 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5358 ctsio->be_move_done = ctl_config_move_done; 5359 ctl_datamove((union ctl_io *)ctsio); 5360 5361 return (CTL_RETVAL_COMPLETE); 5362 } 5363 5364 if (length > 0) 5365 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5366 5367 mtx_lock(&lun->lun_lock); 5368 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5369 ctl_set_reservation_conflict(ctsio); 5370 goto bailout; 5371 } 5372 5373 lun->flags |= CTL_LUN_RESERVED; 5374 lun->res_idx = residx; 5375 5376 ctl_set_success(ctsio); 5377 5378bailout: 5379 mtx_unlock(&lun->lun_lock); 5380 5381 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5382 free(ctsio->kern_data_ptr, M_CTL); 5383 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5384 } 5385 5386 ctl_done((union ctl_io *)ctsio); 5387 return (CTL_RETVAL_COMPLETE); 5388} 5389 5390int 5391ctl_start_stop(struct ctl_scsiio *ctsio) 5392{ 5393 struct scsi_start_stop_unit *cdb; 5394 struct ctl_lun *lun; 5395 struct ctl_softc *ctl_softc; 5396 int retval; 5397 5398 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5399 5400 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5401 ctl_softc = control_softc; 5402 retval = 0; 5403 5404 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5405 5406 /* 5407 * XXX KDM 5408 * We don't support the immediate bit on a stop unit. In order to 5409 * do that, we would need to code up a way to know that a stop is 5410 * pending, and hold off any new commands until it completes, one 5411 * way or another. Then we could accept or reject those commands 5412 * depending on its status. We would almost need to do the reverse 5413 * of what we do below for an immediate start -- return the copy of 5414 * the ctl_io to the FETD with status to send to the host (and to 5415 * free the copy!) and then free the original I/O once the stop 5416 * actually completes. That way, the OOA queue mechanism can work 5417 * to block commands that shouldn't proceed. Another alternative 5418 * would be to put the copy in the queue in place of the original, 5419 * and return the original back to the caller. That could be 5420 * slightly safer.. 5421 */ 5422 if ((cdb->byte2 & SSS_IMMED) 5423 && ((cdb->how & SSS_START) == 0)) { 5424 ctl_set_invalid_field(ctsio, 5425 /*sks_valid*/ 1, 5426 /*command*/ 1, 5427 /*field*/ 1, 5428 /*bit_valid*/ 1, 5429 /*bit*/ 0); 5430 ctl_done((union ctl_io *)ctsio); 5431 return (CTL_RETVAL_COMPLETE); 5432 } 5433 5434 if ((lun->flags & CTL_LUN_PR_RESERVED) 5435 && ((cdb->how & SSS_START)==0)) { 5436 uint32_t residx; 5437 5438 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5439 if (ctl_get_prkey(lun, residx) == 0 5440 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5441 5442 ctl_set_reservation_conflict(ctsio); 5443 ctl_done((union ctl_io *)ctsio); 5444 return (CTL_RETVAL_COMPLETE); 5445 } 5446 } 5447 5448 /* 5449 * If there is no backend on this device, we can't start or stop 5450 * it. In theory we shouldn't get any start/stop commands in the 5451 * first place at this level if the LUN doesn't have a backend. 5452 * That should get stopped by the command decode code. 5453 */ 5454 if (lun->backend == NULL) { 5455 ctl_set_invalid_opcode(ctsio); 5456 ctl_done((union ctl_io *)ctsio); 5457 return (CTL_RETVAL_COMPLETE); 5458 } 5459 5460 /* 5461 * XXX KDM Copan-specific offline behavior. 5462 * Figure out a reasonable way to port this? 5463 */ 5464#ifdef NEEDTOPORT 5465 mtx_lock(&lun->lun_lock); 5466 5467 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5468 && (lun->flags & CTL_LUN_OFFLINE)) { 5469 /* 5470 * If the LUN is offline, and the on/offline bit isn't set, 5471 * reject the start or stop. Otherwise, let it through. 5472 */ 5473 mtx_unlock(&lun->lun_lock); 5474 ctl_set_lun_not_ready(ctsio); 5475 ctl_done((union ctl_io *)ctsio); 5476 } else { 5477 mtx_unlock(&lun->lun_lock); 5478#endif /* NEEDTOPORT */ 5479 /* 5480 * This could be a start or a stop when we're online, 5481 * or a stop/offline or start/online. A start or stop when 5482 * we're offline is covered in the case above. 5483 */ 5484 /* 5485 * In the non-immediate case, we send the request to 5486 * the backend and return status to the user when 5487 * it is done. 5488 * 5489 * In the immediate case, we allocate a new ctl_io 5490 * to hold a copy of the request, and send that to 5491 * the backend. We then set good status on the 5492 * user's request and return it immediately. 5493 */ 5494 if (cdb->byte2 & SSS_IMMED) { 5495 union ctl_io *new_io; 5496 5497 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5498 ctl_copy_io((union ctl_io *)ctsio, new_io); 5499 retval = lun->backend->config_write(new_io); 5500 ctl_set_success(ctsio); 5501 ctl_done((union ctl_io *)ctsio); 5502 } else { 5503 retval = lun->backend->config_write( 5504 (union ctl_io *)ctsio); 5505 } 5506#ifdef NEEDTOPORT 5507 } 5508#endif 5509 return (retval); 5510} 5511 5512/* 5513 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5514 * we don't really do anything with the LBA and length fields if the user 5515 * passes them in. Instead we'll just flush out the cache for the entire 5516 * LUN. 5517 */ 5518int 5519ctl_sync_cache(struct ctl_scsiio *ctsio) 5520{ 5521 struct ctl_lun *lun; 5522 struct ctl_softc *ctl_softc; 5523 uint64_t starting_lba; 5524 uint32_t block_count; 5525 int retval; 5526 5527 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5528 5529 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5530 ctl_softc = control_softc; 5531 retval = 0; 5532 5533 switch (ctsio->cdb[0]) { 5534 case SYNCHRONIZE_CACHE: { 5535 struct scsi_sync_cache *cdb; 5536 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5537 5538 starting_lba = scsi_4btoul(cdb->begin_lba); 5539 block_count = scsi_2btoul(cdb->lb_count); 5540 break; 5541 } 5542 case SYNCHRONIZE_CACHE_16: { 5543 struct scsi_sync_cache_16 *cdb; 5544 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5545 5546 starting_lba = scsi_8btou64(cdb->begin_lba); 5547 block_count = scsi_4btoul(cdb->lb_count); 5548 break; 5549 } 5550 default: 5551 ctl_set_invalid_opcode(ctsio); 5552 ctl_done((union ctl_io *)ctsio); 5553 goto bailout; 5554 break; /* NOTREACHED */ 5555 } 5556 5557 /* 5558 * We check the LBA and length, but don't do anything with them. 5559 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5560 * get flushed. This check will just help satisfy anyone who wants 5561 * to see an error for an out of range LBA. 5562 */ 5563 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5564 ctl_set_lba_out_of_range(ctsio); 5565 ctl_done((union ctl_io *)ctsio); 5566 goto bailout; 5567 } 5568 5569 /* 5570 * If this LUN has no backend, we can't flush the cache anyway. 5571 */ 5572 if (lun->backend == NULL) { 5573 ctl_set_invalid_opcode(ctsio); 5574 ctl_done((union ctl_io *)ctsio); 5575 goto bailout; 5576 } 5577 5578 /* 5579 * Check to see whether we're configured to send the SYNCHRONIZE 5580 * CACHE command directly to the back end. 5581 */ 5582 mtx_lock(&lun->lun_lock); 5583 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5584 && (++(lun->sync_count) >= lun->sync_interval)) { 5585 lun->sync_count = 0; 5586 mtx_unlock(&lun->lun_lock); 5587 retval = lun->backend->config_write((union ctl_io *)ctsio); 5588 } else { 5589 mtx_unlock(&lun->lun_lock); 5590 ctl_set_success(ctsio); 5591 ctl_done((union ctl_io *)ctsio); 5592 } 5593 5594bailout: 5595 5596 return (retval); 5597} 5598 5599int 5600ctl_format(struct ctl_scsiio *ctsio) 5601{ 5602 struct scsi_format *cdb; 5603 struct ctl_lun *lun; 5604 struct ctl_softc *ctl_softc; 5605 int length, defect_list_len; 5606 5607 CTL_DEBUG_PRINT(("ctl_format\n")); 5608 5609 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5610 ctl_softc = control_softc; 5611 5612 cdb = (struct scsi_format *)ctsio->cdb; 5613 5614 length = 0; 5615 if (cdb->byte2 & SF_FMTDATA) { 5616 if (cdb->byte2 & SF_LONGLIST) 5617 length = sizeof(struct scsi_format_header_long); 5618 else 5619 length = sizeof(struct scsi_format_header_short); 5620 } 5621 5622 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5623 && (length > 0)) { 5624 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5625 ctsio->kern_data_len = length; 5626 ctsio->kern_total_len = length; 5627 ctsio->kern_data_resid = 0; 5628 ctsio->kern_rel_offset = 0; 5629 ctsio->kern_sg_entries = 0; 5630 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5631 ctsio->be_move_done = ctl_config_move_done; 5632 ctl_datamove((union ctl_io *)ctsio); 5633 5634 return (CTL_RETVAL_COMPLETE); 5635 } 5636 5637 defect_list_len = 0; 5638 5639 if (cdb->byte2 & SF_FMTDATA) { 5640 if (cdb->byte2 & SF_LONGLIST) { 5641 struct scsi_format_header_long *header; 5642 5643 header = (struct scsi_format_header_long *) 5644 ctsio->kern_data_ptr; 5645 5646 defect_list_len = scsi_4btoul(header->defect_list_len); 5647 if (defect_list_len != 0) { 5648 ctl_set_invalid_field(ctsio, 5649 /*sks_valid*/ 1, 5650 /*command*/ 0, 5651 /*field*/ 2, 5652 /*bit_valid*/ 0, 5653 /*bit*/ 0); 5654 goto bailout; 5655 } 5656 } else { 5657 struct scsi_format_header_short *header; 5658 5659 header = (struct scsi_format_header_short *) 5660 ctsio->kern_data_ptr; 5661 5662 defect_list_len = scsi_2btoul(header->defect_list_len); 5663 if (defect_list_len != 0) { 5664 ctl_set_invalid_field(ctsio, 5665 /*sks_valid*/ 1, 5666 /*command*/ 0, 5667 /*field*/ 2, 5668 /*bit_valid*/ 0, 5669 /*bit*/ 0); 5670 goto bailout; 5671 } 5672 } 5673 } 5674 5675 /* 5676 * The format command will clear out the "Medium format corrupted" 5677 * status if set by the configuration code. That status is really 5678 * just a way to notify the host that we have lost the media, and 5679 * get them to issue a command that will basically make them think 5680 * they're blowing away the media. 5681 */ 5682 mtx_lock(&lun->lun_lock); 5683 lun->flags &= ~CTL_LUN_INOPERABLE; 5684 mtx_unlock(&lun->lun_lock); 5685 5686 ctl_set_success(ctsio); 5687bailout: 5688 5689 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5690 free(ctsio->kern_data_ptr, M_CTL); 5691 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5692 } 5693 5694 ctl_done((union ctl_io *)ctsio); 5695 return (CTL_RETVAL_COMPLETE); 5696} 5697 5698int 5699ctl_read_buffer(struct ctl_scsiio *ctsio) 5700{ 5701 struct scsi_read_buffer *cdb; 5702 struct ctl_lun *lun; 5703 int buffer_offset, len; 5704 static uint8_t descr[4]; 5705 static uint8_t echo_descr[4] = { 0 }; 5706 5707 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5708 5709 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5710 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5711 5712 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5713 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5714 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5715 ctl_set_invalid_field(ctsio, 5716 /*sks_valid*/ 1, 5717 /*command*/ 1, 5718 /*field*/ 1, 5719 /*bit_valid*/ 1, 5720 /*bit*/ 4); 5721 ctl_done((union ctl_io *)ctsio); 5722 return (CTL_RETVAL_COMPLETE); 5723 } 5724 5725 len = scsi_3btoul(cdb->length); 5726 buffer_offset = scsi_3btoul(cdb->offset); 5727 5728 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5729 ctl_set_invalid_field(ctsio, 5730 /*sks_valid*/ 1, 5731 /*command*/ 1, 5732 /*field*/ 6, 5733 /*bit_valid*/ 0, 5734 /*bit*/ 0); 5735 ctl_done((union ctl_io *)ctsio); 5736 return (CTL_RETVAL_COMPLETE); 5737 } 5738 5739 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5740 descr[0] = 0; 5741 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5742 ctsio->kern_data_ptr = descr; 5743 len = min(len, sizeof(descr)); 5744 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5745 ctsio->kern_data_ptr = echo_descr; 5746 len = min(len, sizeof(echo_descr)); 5747 } else { 5748 if (lun->write_buffer == NULL) { 5749 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5750 M_CTL, M_WAITOK); 5751 } 5752 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5753 } 5754 ctsio->kern_data_len = len; 5755 ctsio->kern_total_len = len; 5756 ctsio->kern_data_resid = 0; 5757 ctsio->kern_rel_offset = 0; 5758 ctsio->kern_sg_entries = 0; 5759 ctl_set_success(ctsio); 5760 ctsio->be_move_done = ctl_config_move_done; 5761 ctl_datamove((union ctl_io *)ctsio); 5762 return (CTL_RETVAL_COMPLETE); 5763} 5764 5765int 5766ctl_write_buffer(struct ctl_scsiio *ctsio) 5767{ 5768 struct scsi_write_buffer *cdb; 5769 struct ctl_lun *lun; 5770 int buffer_offset, len; 5771 5772 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5773 5774 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5775 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5776 5777 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5778 ctl_set_invalid_field(ctsio, 5779 /*sks_valid*/ 1, 5780 /*command*/ 1, 5781 /*field*/ 1, 5782 /*bit_valid*/ 1, 5783 /*bit*/ 4); 5784 ctl_done((union ctl_io *)ctsio); 5785 return (CTL_RETVAL_COMPLETE); 5786 } 5787 5788 len = scsi_3btoul(cdb->length); 5789 buffer_offset = scsi_3btoul(cdb->offset); 5790 5791 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5792 ctl_set_invalid_field(ctsio, 5793 /*sks_valid*/ 1, 5794 /*command*/ 1, 5795 /*field*/ 6, 5796 /*bit_valid*/ 0, 5797 /*bit*/ 0); 5798 ctl_done((union ctl_io *)ctsio); 5799 return (CTL_RETVAL_COMPLETE); 5800 } 5801 5802 /* 5803 * If we've got a kernel request that hasn't been malloced yet, 5804 * malloc it and tell the caller the data buffer is here. 5805 */ 5806 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5807 if (lun->write_buffer == NULL) { 5808 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5809 M_CTL, M_WAITOK); 5810 } 5811 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5812 ctsio->kern_data_len = len; 5813 ctsio->kern_total_len = len; 5814 ctsio->kern_data_resid = 0; 5815 ctsio->kern_rel_offset = 0; 5816 ctsio->kern_sg_entries = 0; 5817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5818 ctsio->be_move_done = ctl_config_move_done; 5819 ctl_datamove((union ctl_io *)ctsio); 5820 5821 return (CTL_RETVAL_COMPLETE); 5822 } 5823 5824 ctl_set_success(ctsio); 5825 ctl_done((union ctl_io *)ctsio); 5826 return (CTL_RETVAL_COMPLETE); 5827} 5828 5829int 5830ctl_write_same(struct ctl_scsiio *ctsio) 5831{ 5832 struct ctl_lun *lun; 5833 struct ctl_lba_len_flags *lbalen; 5834 uint64_t lba; 5835 uint32_t num_blocks; 5836 int len, retval; 5837 uint8_t byte2; 5838 5839 retval = CTL_RETVAL_COMPLETE; 5840 5841 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5842 5843 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5844 5845 switch (ctsio->cdb[0]) { 5846 case WRITE_SAME_10: { 5847 struct scsi_write_same_10 *cdb; 5848 5849 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5850 5851 lba = scsi_4btoul(cdb->addr); 5852 num_blocks = scsi_2btoul(cdb->length); 5853 byte2 = cdb->byte2; 5854 break; 5855 } 5856 case WRITE_SAME_16: { 5857 struct scsi_write_same_16 *cdb; 5858 5859 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5860 5861 lba = scsi_8btou64(cdb->addr); 5862 num_blocks = scsi_4btoul(cdb->length); 5863 byte2 = cdb->byte2; 5864 break; 5865 } 5866 default: 5867 /* 5868 * We got a command we don't support. This shouldn't 5869 * happen, commands should be filtered out above us. 5870 */ 5871 ctl_set_invalid_opcode(ctsio); 5872 ctl_done((union ctl_io *)ctsio); 5873 5874 return (CTL_RETVAL_COMPLETE); 5875 break; /* NOTREACHED */ 5876 } 5877 5878 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5879 if ((byte2 & SWS_UNMAP) == 0 && 5880 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5881 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5882 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5883 ctl_done((union ctl_io *)ctsio); 5884 return (CTL_RETVAL_COMPLETE); 5885 } 5886 5887 /* 5888 * The first check is to make sure we're in bounds, the second 5889 * check is to catch wrap-around problems. If the lba + num blocks 5890 * is less than the lba, then we've wrapped around and the block 5891 * range is invalid anyway. 5892 */ 5893 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5894 || ((lba + num_blocks) < lba)) { 5895 ctl_set_lba_out_of_range(ctsio); 5896 ctl_done((union ctl_io *)ctsio); 5897 return (CTL_RETVAL_COMPLETE); 5898 } 5899 5900 /* Zero number of blocks means "to the last logical block" */ 5901 if (num_blocks == 0) { 5902 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5903 ctl_set_invalid_field(ctsio, 5904 /*sks_valid*/ 0, 5905 /*command*/ 1, 5906 /*field*/ 0, 5907 /*bit_valid*/ 0, 5908 /*bit*/ 0); 5909 ctl_done((union ctl_io *)ctsio); 5910 return (CTL_RETVAL_COMPLETE); 5911 } 5912 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5913 } 5914 5915 len = lun->be_lun->blocksize; 5916 5917 /* 5918 * If we've got a kernel request that hasn't been malloced yet, 5919 * malloc it and tell the caller the data buffer is here. 5920 */ 5921 if ((byte2 & SWS_NDOB) == 0 && 5922 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5923 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5924 ctsio->kern_data_len = len; 5925 ctsio->kern_total_len = len; 5926 ctsio->kern_data_resid = 0; 5927 ctsio->kern_rel_offset = 0; 5928 ctsio->kern_sg_entries = 0; 5929 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5930 ctsio->be_move_done = ctl_config_move_done; 5931 ctl_datamove((union ctl_io *)ctsio); 5932 5933 return (CTL_RETVAL_COMPLETE); 5934 } 5935 5936 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5937 lbalen->lba = lba; 5938 lbalen->len = num_blocks; 5939 lbalen->flags = byte2; 5940 retval = lun->backend->config_write((union ctl_io *)ctsio); 5941 5942 return (retval); 5943} 5944 5945int 5946ctl_unmap(struct ctl_scsiio *ctsio) 5947{ 5948 struct ctl_lun *lun; 5949 struct scsi_unmap *cdb; 5950 struct ctl_ptr_len_flags *ptrlen; 5951 struct scsi_unmap_header *hdr; 5952 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5953 uint64_t lba; 5954 uint32_t num_blocks; 5955 int len, retval; 5956 uint8_t byte2; 5957 5958 retval = CTL_RETVAL_COMPLETE; 5959 5960 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5961 5962 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5963 cdb = (struct scsi_unmap *)ctsio->cdb; 5964 5965 len = scsi_2btoul(cdb->length); 5966 byte2 = cdb->byte2; 5967 5968 /* 5969 * If we've got a kernel request that hasn't been malloced yet, 5970 * malloc it and tell the caller the data buffer is here. 5971 */ 5972 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5973 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5974 ctsio->kern_data_len = len; 5975 ctsio->kern_total_len = len; 5976 ctsio->kern_data_resid = 0; 5977 ctsio->kern_rel_offset = 0; 5978 ctsio->kern_sg_entries = 0; 5979 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5980 ctsio->be_move_done = ctl_config_move_done; 5981 ctl_datamove((union ctl_io *)ctsio); 5982 5983 return (CTL_RETVAL_COMPLETE); 5984 } 5985 5986 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5987 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5988 if (len < sizeof (*hdr) || 5989 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5990 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5991 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5992 ctl_set_invalid_field(ctsio, 5993 /*sks_valid*/ 0, 5994 /*command*/ 0, 5995 /*field*/ 0, 5996 /*bit_valid*/ 0, 5997 /*bit*/ 0); 5998 goto done; 5999 } 6000 len = scsi_2btoul(hdr->desc_length); 6001 buf = (struct scsi_unmap_desc *)(hdr + 1); 6002 end = buf + len / sizeof(*buf); 6003 6004 endnz = buf; 6005 for (range = buf; range < end; range++) { 6006 lba = scsi_8btou64(range->lba); 6007 num_blocks = scsi_4btoul(range->length); 6008 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6009 || ((lba + num_blocks) < lba)) { 6010 ctl_set_lba_out_of_range(ctsio); 6011 ctl_done((union ctl_io *)ctsio); 6012 return (CTL_RETVAL_COMPLETE); 6013 } 6014 if (num_blocks != 0) 6015 endnz = range + 1; 6016 } 6017 6018 /* 6019 * Block backend can not handle zero last range. 6020 * Filter it out and return if there is nothing left. 6021 */ 6022 len = (uint8_t *)endnz - (uint8_t *)buf; 6023 if (len == 0) { 6024 ctl_set_success(ctsio); 6025 goto done; 6026 } 6027 6028 mtx_lock(&lun->lun_lock); 6029 ptrlen = (struct ctl_ptr_len_flags *) 6030 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6031 ptrlen->ptr = (void *)buf; 6032 ptrlen->len = len; 6033 ptrlen->flags = byte2; 6034 ctl_check_blocked(lun); 6035 mtx_unlock(&lun->lun_lock); 6036 6037 retval = lun->backend->config_write((union ctl_io *)ctsio); 6038 return (retval); 6039 6040done: 6041 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6042 free(ctsio->kern_data_ptr, M_CTL); 6043 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6044 } 6045 ctl_done((union ctl_io *)ctsio); 6046 return (CTL_RETVAL_COMPLETE); 6047} 6048 6049/* 6050 * Note that this function currently doesn't actually do anything inside 6051 * CTL to enforce things if the DQue bit is turned on. 6052 * 6053 * Also note that this function can't be used in the default case, because 6054 * the DQue bit isn't set in the changeable mask for the control mode page 6055 * anyway. This is just here as an example for how to implement a page 6056 * handler, and a placeholder in case we want to allow the user to turn 6057 * tagged queueing on and off. 6058 * 6059 * The D_SENSE bit handling is functional, however, and will turn 6060 * descriptor sense on and off for a given LUN. 6061 */ 6062int 6063ctl_control_page_handler(struct ctl_scsiio *ctsio, 6064 struct ctl_page_index *page_index, uint8_t *page_ptr) 6065{ 6066 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6067 struct ctl_lun *lun; 6068 struct ctl_softc *softc; 6069 int set_ua; 6070 uint32_t initidx; 6071 6072 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6073 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6074 set_ua = 0; 6075 6076 user_cp = (struct scsi_control_page *)page_ptr; 6077 current_cp = (struct scsi_control_page *) 6078 (page_index->page_data + (page_index->page_len * 6079 CTL_PAGE_CURRENT)); 6080 saved_cp = (struct scsi_control_page *) 6081 (page_index->page_data + (page_index->page_len * 6082 CTL_PAGE_SAVED)); 6083 6084 softc = control_softc; 6085 6086 mtx_lock(&lun->lun_lock); 6087 if (((current_cp->rlec & SCP_DSENSE) == 0) 6088 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6089 /* 6090 * Descriptor sense is currently turned off and the user 6091 * wants to turn it on. 6092 */ 6093 current_cp->rlec |= SCP_DSENSE; 6094 saved_cp->rlec |= SCP_DSENSE; 6095 lun->flags |= CTL_LUN_SENSE_DESC; 6096 set_ua = 1; 6097 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6098 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6099 /* 6100 * Descriptor sense is currently turned on, and the user 6101 * wants to turn it off. 6102 */ 6103 current_cp->rlec &= ~SCP_DSENSE; 6104 saved_cp->rlec &= ~SCP_DSENSE; 6105 lun->flags &= ~CTL_LUN_SENSE_DESC; 6106 set_ua = 1; 6107 } 6108 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6109 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6110 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6111 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6112 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6113 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6114 set_ua = 1; 6115 } 6116 if ((current_cp->eca_and_aen & SCP_SWP) != 6117 (user_cp->eca_and_aen & SCP_SWP)) { 6118 current_cp->eca_and_aen &= ~SCP_SWP; 6119 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6120 saved_cp->eca_and_aen &= ~SCP_SWP; 6121 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6122 set_ua = 1; 6123 } 6124 if (set_ua != 0) 6125 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6126 mtx_unlock(&lun->lun_lock); 6127 6128 return (0); 6129} 6130 6131int 6132ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6133 struct ctl_page_index *page_index, uint8_t *page_ptr) 6134{ 6135 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6136 struct ctl_lun *lun; 6137 int set_ua; 6138 uint32_t initidx; 6139 6140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6141 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6142 set_ua = 0; 6143 6144 user_cp = (struct scsi_caching_page *)page_ptr; 6145 current_cp = (struct scsi_caching_page *) 6146 (page_index->page_data + (page_index->page_len * 6147 CTL_PAGE_CURRENT)); 6148 saved_cp = (struct scsi_caching_page *) 6149 (page_index->page_data + (page_index->page_len * 6150 CTL_PAGE_SAVED)); 6151 6152 mtx_lock(&lun->lun_lock); 6153 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6154 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6155 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6156 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6157 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6158 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6159 set_ua = 1; 6160 } 6161 if (set_ua != 0) 6162 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6163 mtx_unlock(&lun->lun_lock); 6164 6165 return (0); 6166} 6167 6168int 6169ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6170 struct ctl_page_index *page_index, 6171 uint8_t *page_ptr) 6172{ 6173 uint8_t *c; 6174 int i; 6175 6176 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6177 ctl_time_io_secs = 6178 (c[0] << 8) | 6179 (c[1] << 0) | 6180 0; 6181 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6182 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6183 printf("page data:"); 6184 for (i=0; i<8; i++) 6185 printf(" %.2x",page_ptr[i]); 6186 printf("\n"); 6187 return (0); 6188} 6189 6190int 6191ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6192 struct ctl_page_index *page_index, 6193 int pc) 6194{ 6195 struct copan_debugconf_subpage *page; 6196 6197 page = (struct copan_debugconf_subpage *)page_index->page_data + 6198 (page_index->page_len * pc); 6199 6200 switch (pc) { 6201 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6202 case SMS_PAGE_CTRL_DEFAULT >> 6: 6203 case SMS_PAGE_CTRL_SAVED >> 6: 6204 /* 6205 * We don't update the changable or default bits for this page. 6206 */ 6207 break; 6208 case SMS_PAGE_CTRL_CURRENT >> 6: 6209 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6210 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6211 break; 6212 default: 6213#ifdef NEEDTOPORT 6214 EPRINT(0, "Invalid PC %d!!", pc); 6215#endif /* NEEDTOPORT */ 6216 break; 6217 } 6218 return (0); 6219} 6220 6221 6222static int 6223ctl_do_mode_select(union ctl_io *io) 6224{ 6225 struct scsi_mode_page_header *page_header; 6226 struct ctl_page_index *page_index; 6227 struct ctl_scsiio *ctsio; 6228 int control_dev, page_len; 6229 int page_len_offset, page_len_size; 6230 union ctl_modepage_info *modepage_info; 6231 struct ctl_lun *lun; 6232 int *len_left, *len_used; 6233 int retval, i; 6234 6235 ctsio = &io->scsiio; 6236 page_index = NULL; 6237 page_len = 0; 6238 retval = CTL_RETVAL_COMPLETE; 6239 6240 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6241 6242 if (lun->be_lun->lun_type != T_DIRECT) 6243 control_dev = 1; 6244 else 6245 control_dev = 0; 6246 6247 modepage_info = (union ctl_modepage_info *) 6248 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6249 len_left = &modepage_info->header.len_left; 6250 len_used = &modepage_info->header.len_used; 6251 6252do_next_page: 6253 6254 page_header = (struct scsi_mode_page_header *) 6255 (ctsio->kern_data_ptr + *len_used); 6256 6257 if (*len_left == 0) { 6258 free(ctsio->kern_data_ptr, M_CTL); 6259 ctl_set_success(ctsio); 6260 ctl_done((union ctl_io *)ctsio); 6261 return (CTL_RETVAL_COMPLETE); 6262 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6263 6264 free(ctsio->kern_data_ptr, M_CTL); 6265 ctl_set_param_len_error(ctsio); 6266 ctl_done((union ctl_io *)ctsio); 6267 return (CTL_RETVAL_COMPLETE); 6268 6269 } else if ((page_header->page_code & SMPH_SPF) 6270 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6271 6272 free(ctsio->kern_data_ptr, M_CTL); 6273 ctl_set_param_len_error(ctsio); 6274 ctl_done((union ctl_io *)ctsio); 6275 return (CTL_RETVAL_COMPLETE); 6276 } 6277 6278 6279 /* 6280 * XXX KDM should we do something with the block descriptor? 6281 */ 6282 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6283 6284 if ((control_dev != 0) 6285 && (lun->mode_pages.index[i].page_flags & 6286 CTL_PAGE_FLAG_DISK_ONLY)) 6287 continue; 6288 6289 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6290 (page_header->page_code & SMPH_PC_MASK)) 6291 continue; 6292 6293 /* 6294 * If neither page has a subpage code, then we've got a 6295 * match. 6296 */ 6297 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6298 && ((page_header->page_code & SMPH_SPF) == 0)) { 6299 page_index = &lun->mode_pages.index[i]; 6300 page_len = page_header->page_length; 6301 break; 6302 } 6303 6304 /* 6305 * If both pages have subpages, then the subpage numbers 6306 * have to match. 6307 */ 6308 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6309 && (page_header->page_code & SMPH_SPF)) { 6310 struct scsi_mode_page_header_sp *sph; 6311 6312 sph = (struct scsi_mode_page_header_sp *)page_header; 6313 6314 if (lun->mode_pages.index[i].subpage == 6315 sph->subpage) { 6316 page_index = &lun->mode_pages.index[i]; 6317 page_len = scsi_2btoul(sph->page_length); 6318 break; 6319 } 6320 } 6321 } 6322 6323 /* 6324 * If we couldn't find the page, or if we don't have a mode select 6325 * handler for it, send back an error to the user. 6326 */ 6327 if ((page_index == NULL) 6328 || (page_index->select_handler == NULL)) { 6329 ctl_set_invalid_field(ctsio, 6330 /*sks_valid*/ 1, 6331 /*command*/ 0, 6332 /*field*/ *len_used, 6333 /*bit_valid*/ 0, 6334 /*bit*/ 0); 6335 free(ctsio->kern_data_ptr, M_CTL); 6336 ctl_done((union ctl_io *)ctsio); 6337 return (CTL_RETVAL_COMPLETE); 6338 } 6339 6340 if (page_index->page_code & SMPH_SPF) { 6341 page_len_offset = 2; 6342 page_len_size = 2; 6343 } else { 6344 page_len_size = 1; 6345 page_len_offset = 1; 6346 } 6347 6348 /* 6349 * If the length the initiator gives us isn't the one we specify in 6350 * the mode page header, or if they didn't specify enough data in 6351 * the CDB to avoid truncating this page, kick out the request. 6352 */ 6353 if ((page_len != (page_index->page_len - page_len_offset - 6354 page_len_size)) 6355 || (*len_left < page_index->page_len)) { 6356 6357 6358 ctl_set_invalid_field(ctsio, 6359 /*sks_valid*/ 1, 6360 /*command*/ 0, 6361 /*field*/ *len_used + page_len_offset, 6362 /*bit_valid*/ 0, 6363 /*bit*/ 0); 6364 free(ctsio->kern_data_ptr, M_CTL); 6365 ctl_done((union ctl_io *)ctsio); 6366 return (CTL_RETVAL_COMPLETE); 6367 } 6368 6369 /* 6370 * Run through the mode page, checking to make sure that the bits 6371 * the user changed are actually legal for him to change. 6372 */ 6373 for (i = 0; i < page_index->page_len; i++) { 6374 uint8_t *user_byte, *change_mask, *current_byte; 6375 int bad_bit; 6376 int j; 6377 6378 user_byte = (uint8_t *)page_header + i; 6379 change_mask = page_index->page_data + 6380 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6381 current_byte = page_index->page_data + 6382 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6383 6384 /* 6385 * Check to see whether the user set any bits in this byte 6386 * that he is not allowed to set. 6387 */ 6388 if ((*user_byte & ~(*change_mask)) == 6389 (*current_byte & ~(*change_mask))) 6390 continue; 6391 6392 /* 6393 * Go through bit by bit to determine which one is illegal. 6394 */ 6395 bad_bit = 0; 6396 for (j = 7; j >= 0; j--) { 6397 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6398 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6399 bad_bit = i; 6400 break; 6401 } 6402 } 6403 ctl_set_invalid_field(ctsio, 6404 /*sks_valid*/ 1, 6405 /*command*/ 0, 6406 /*field*/ *len_used + i, 6407 /*bit_valid*/ 1, 6408 /*bit*/ bad_bit); 6409 free(ctsio->kern_data_ptr, M_CTL); 6410 ctl_done((union ctl_io *)ctsio); 6411 return (CTL_RETVAL_COMPLETE); 6412 } 6413 6414 /* 6415 * Decrement these before we call the page handler, since we may 6416 * end up getting called back one way or another before the handler 6417 * returns to this context. 6418 */ 6419 *len_left -= page_index->page_len; 6420 *len_used += page_index->page_len; 6421 6422 retval = page_index->select_handler(ctsio, page_index, 6423 (uint8_t *)page_header); 6424 6425 /* 6426 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6427 * wait until this queued command completes to finish processing 6428 * the mode page. If it returns anything other than 6429 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6430 * already set the sense information, freed the data pointer, and 6431 * completed the io for us. 6432 */ 6433 if (retval != CTL_RETVAL_COMPLETE) 6434 goto bailout_no_done; 6435 6436 /* 6437 * If the initiator sent us more than one page, parse the next one. 6438 */ 6439 if (*len_left > 0) 6440 goto do_next_page; 6441 6442 ctl_set_success(ctsio); 6443 free(ctsio->kern_data_ptr, M_CTL); 6444 ctl_done((union ctl_io *)ctsio); 6445 6446bailout_no_done: 6447 6448 return (CTL_RETVAL_COMPLETE); 6449 6450} 6451 6452int 6453ctl_mode_select(struct ctl_scsiio *ctsio) 6454{ 6455 int param_len, pf, sp; 6456 int header_size, bd_len; 6457 int len_left, len_used; 6458 struct ctl_page_index *page_index; 6459 struct ctl_lun *lun; 6460 int control_dev, page_len; 6461 union ctl_modepage_info *modepage_info; 6462 int retval; 6463 6464 pf = 0; 6465 sp = 0; 6466 page_len = 0; 6467 len_used = 0; 6468 len_left = 0; 6469 retval = 0; 6470 bd_len = 0; 6471 page_index = NULL; 6472 6473 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6474 6475 if (lun->be_lun->lun_type != T_DIRECT) 6476 control_dev = 1; 6477 else 6478 control_dev = 0; 6479 6480 switch (ctsio->cdb[0]) { 6481 case MODE_SELECT_6: { 6482 struct scsi_mode_select_6 *cdb; 6483 6484 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6485 6486 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6487 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6488 6489 param_len = cdb->length; 6490 header_size = sizeof(struct scsi_mode_header_6); 6491 break; 6492 } 6493 case MODE_SELECT_10: { 6494 struct scsi_mode_select_10 *cdb; 6495 6496 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6497 6498 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6499 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6500 6501 param_len = scsi_2btoul(cdb->length); 6502 header_size = sizeof(struct scsi_mode_header_10); 6503 break; 6504 } 6505 default: 6506 ctl_set_invalid_opcode(ctsio); 6507 ctl_done((union ctl_io *)ctsio); 6508 return (CTL_RETVAL_COMPLETE); 6509 break; /* NOTREACHED */ 6510 } 6511 6512 /* 6513 * From SPC-3: 6514 * "A parameter list length of zero indicates that the Data-Out Buffer 6515 * shall be empty. This condition shall not be considered as an error." 6516 */ 6517 if (param_len == 0) { 6518 ctl_set_success(ctsio); 6519 ctl_done((union ctl_io *)ctsio); 6520 return (CTL_RETVAL_COMPLETE); 6521 } 6522 6523 /* 6524 * Since we'll hit this the first time through, prior to 6525 * allocation, we don't need to free a data buffer here. 6526 */ 6527 if (param_len < header_size) { 6528 ctl_set_param_len_error(ctsio); 6529 ctl_done((union ctl_io *)ctsio); 6530 return (CTL_RETVAL_COMPLETE); 6531 } 6532 6533 /* 6534 * Allocate the data buffer and grab the user's data. In theory, 6535 * we shouldn't have to sanity check the parameter list length here 6536 * because the maximum size is 64K. We should be able to malloc 6537 * that much without too many problems. 6538 */ 6539 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6540 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6541 ctsio->kern_data_len = param_len; 6542 ctsio->kern_total_len = param_len; 6543 ctsio->kern_data_resid = 0; 6544 ctsio->kern_rel_offset = 0; 6545 ctsio->kern_sg_entries = 0; 6546 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6547 ctsio->be_move_done = ctl_config_move_done; 6548 ctl_datamove((union ctl_io *)ctsio); 6549 6550 return (CTL_RETVAL_COMPLETE); 6551 } 6552 6553 switch (ctsio->cdb[0]) { 6554 case MODE_SELECT_6: { 6555 struct scsi_mode_header_6 *mh6; 6556 6557 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6558 bd_len = mh6->blk_desc_len; 6559 break; 6560 } 6561 case MODE_SELECT_10: { 6562 struct scsi_mode_header_10 *mh10; 6563 6564 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6565 bd_len = scsi_2btoul(mh10->blk_desc_len); 6566 break; 6567 } 6568 default: 6569 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6570 break; 6571 } 6572 6573 if (param_len < (header_size + bd_len)) { 6574 free(ctsio->kern_data_ptr, M_CTL); 6575 ctl_set_param_len_error(ctsio); 6576 ctl_done((union ctl_io *)ctsio); 6577 return (CTL_RETVAL_COMPLETE); 6578 } 6579 6580 /* 6581 * Set the IO_CONT flag, so that if this I/O gets passed to 6582 * ctl_config_write_done(), it'll get passed back to 6583 * ctl_do_mode_select() for further processing, or completion if 6584 * we're all done. 6585 */ 6586 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6587 ctsio->io_cont = ctl_do_mode_select; 6588 6589 modepage_info = (union ctl_modepage_info *) 6590 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6591 6592 memset(modepage_info, 0, sizeof(*modepage_info)); 6593 6594 len_left = param_len - header_size - bd_len; 6595 len_used = header_size + bd_len; 6596 6597 modepage_info->header.len_left = len_left; 6598 modepage_info->header.len_used = len_used; 6599 6600 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6601} 6602 6603int 6604ctl_mode_sense(struct ctl_scsiio *ctsio) 6605{ 6606 struct ctl_lun *lun; 6607 int pc, page_code, dbd, llba, subpage; 6608 int alloc_len, page_len, header_len, total_len; 6609 struct scsi_mode_block_descr *block_desc; 6610 struct ctl_page_index *page_index; 6611 int control_dev; 6612 6613 dbd = 0; 6614 llba = 0; 6615 block_desc = NULL; 6616 page_index = NULL; 6617 6618 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6619 6620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6621 6622 if (lun->be_lun->lun_type != T_DIRECT) 6623 control_dev = 1; 6624 else 6625 control_dev = 0; 6626 6627 switch (ctsio->cdb[0]) { 6628 case MODE_SENSE_6: { 6629 struct scsi_mode_sense_6 *cdb; 6630 6631 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6632 6633 header_len = sizeof(struct scsi_mode_hdr_6); 6634 if (cdb->byte2 & SMS_DBD) 6635 dbd = 1; 6636 else 6637 header_len += sizeof(struct scsi_mode_block_descr); 6638 6639 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6640 page_code = cdb->page & SMS_PAGE_CODE; 6641 subpage = cdb->subpage; 6642 alloc_len = cdb->length; 6643 break; 6644 } 6645 case MODE_SENSE_10: { 6646 struct scsi_mode_sense_10 *cdb; 6647 6648 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6649 6650 header_len = sizeof(struct scsi_mode_hdr_10); 6651 6652 if (cdb->byte2 & SMS_DBD) 6653 dbd = 1; 6654 else 6655 header_len += sizeof(struct scsi_mode_block_descr); 6656 if (cdb->byte2 & SMS10_LLBAA) 6657 llba = 1; 6658 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6659 page_code = cdb->page & SMS_PAGE_CODE; 6660 subpage = cdb->subpage; 6661 alloc_len = scsi_2btoul(cdb->length); 6662 break; 6663 } 6664 default: 6665 ctl_set_invalid_opcode(ctsio); 6666 ctl_done((union ctl_io *)ctsio); 6667 return (CTL_RETVAL_COMPLETE); 6668 break; /* NOTREACHED */ 6669 } 6670 6671 /* 6672 * We have to make a first pass through to calculate the size of 6673 * the pages that match the user's query. Then we allocate enough 6674 * memory to hold it, and actually copy the data into the buffer. 6675 */ 6676 switch (page_code) { 6677 case SMS_ALL_PAGES_PAGE: { 6678 int i; 6679 6680 page_len = 0; 6681 6682 /* 6683 * At the moment, values other than 0 and 0xff here are 6684 * reserved according to SPC-3. 6685 */ 6686 if ((subpage != SMS_SUBPAGE_PAGE_0) 6687 && (subpage != SMS_SUBPAGE_ALL)) { 6688 ctl_set_invalid_field(ctsio, 6689 /*sks_valid*/ 1, 6690 /*command*/ 1, 6691 /*field*/ 3, 6692 /*bit_valid*/ 0, 6693 /*bit*/ 0); 6694 ctl_done((union ctl_io *)ctsio); 6695 return (CTL_RETVAL_COMPLETE); 6696 } 6697 6698 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6699 if ((control_dev != 0) 6700 && (lun->mode_pages.index[i].page_flags & 6701 CTL_PAGE_FLAG_DISK_ONLY)) 6702 continue; 6703 6704 /* 6705 * We don't use this subpage if the user didn't 6706 * request all subpages. 6707 */ 6708 if ((lun->mode_pages.index[i].subpage != 0) 6709 && (subpage == SMS_SUBPAGE_PAGE_0)) 6710 continue; 6711 6712#if 0 6713 printf("found page %#x len %d\n", 6714 lun->mode_pages.index[i].page_code & 6715 SMPH_PC_MASK, 6716 lun->mode_pages.index[i].page_len); 6717#endif 6718 page_len += lun->mode_pages.index[i].page_len; 6719 } 6720 break; 6721 } 6722 default: { 6723 int i; 6724 6725 page_len = 0; 6726 6727 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6728 /* Look for the right page code */ 6729 if ((lun->mode_pages.index[i].page_code & 6730 SMPH_PC_MASK) != page_code) 6731 continue; 6732 6733 /* Look for the right subpage or the subpage wildcard*/ 6734 if ((lun->mode_pages.index[i].subpage != subpage) 6735 && (subpage != SMS_SUBPAGE_ALL)) 6736 continue; 6737 6738 /* Make sure the page is supported for this dev type */ 6739 if ((control_dev != 0) 6740 && (lun->mode_pages.index[i].page_flags & 6741 CTL_PAGE_FLAG_DISK_ONLY)) 6742 continue; 6743 6744#if 0 6745 printf("found page %#x len %d\n", 6746 lun->mode_pages.index[i].page_code & 6747 SMPH_PC_MASK, 6748 lun->mode_pages.index[i].page_len); 6749#endif 6750 6751 page_len += lun->mode_pages.index[i].page_len; 6752 } 6753 6754 if (page_len == 0) { 6755 ctl_set_invalid_field(ctsio, 6756 /*sks_valid*/ 1, 6757 /*command*/ 1, 6758 /*field*/ 2, 6759 /*bit_valid*/ 1, 6760 /*bit*/ 5); 6761 ctl_done((union ctl_io *)ctsio); 6762 return (CTL_RETVAL_COMPLETE); 6763 } 6764 break; 6765 } 6766 } 6767 6768 total_len = header_len + page_len; 6769#if 0 6770 printf("header_len = %d, page_len = %d, total_len = %d\n", 6771 header_len, page_len, total_len); 6772#endif 6773 6774 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6775 ctsio->kern_sg_entries = 0; 6776 ctsio->kern_data_resid = 0; 6777 ctsio->kern_rel_offset = 0; 6778 if (total_len < alloc_len) { 6779 ctsio->residual = alloc_len - total_len; 6780 ctsio->kern_data_len = total_len; 6781 ctsio->kern_total_len = total_len; 6782 } else { 6783 ctsio->residual = 0; 6784 ctsio->kern_data_len = alloc_len; 6785 ctsio->kern_total_len = alloc_len; 6786 } 6787 6788 switch (ctsio->cdb[0]) { 6789 case MODE_SENSE_6: { 6790 struct scsi_mode_hdr_6 *header; 6791 6792 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6793 6794 header->datalen = ctl_min(total_len - 1, 254); 6795 if (control_dev == 0) { 6796 header->dev_specific = 0x10; /* DPOFUA */ 6797 if ((lun->flags & CTL_LUN_READONLY) || 6798 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6799 .eca_and_aen & SCP_SWP) != 0) 6800 header->dev_specific |= 0x80; /* WP */ 6801 } 6802 if (dbd) 6803 header->block_descr_len = 0; 6804 else 6805 header->block_descr_len = 6806 sizeof(struct scsi_mode_block_descr); 6807 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6808 break; 6809 } 6810 case MODE_SENSE_10: { 6811 struct scsi_mode_hdr_10 *header; 6812 int datalen; 6813 6814 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6815 6816 datalen = ctl_min(total_len - 2, 65533); 6817 scsi_ulto2b(datalen, header->datalen); 6818 if (control_dev == 0) { 6819 header->dev_specific = 0x10; /* DPOFUA */ 6820 if ((lun->flags & CTL_LUN_READONLY) || 6821 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6822 .eca_and_aen & SCP_SWP) != 0) 6823 header->dev_specific |= 0x80; /* WP */ 6824 } 6825 if (dbd) 6826 scsi_ulto2b(0, header->block_descr_len); 6827 else 6828 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6829 header->block_descr_len); 6830 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6831 break; 6832 } 6833 default: 6834 panic("invalid CDB type %#x", ctsio->cdb[0]); 6835 break; /* NOTREACHED */ 6836 } 6837 6838 /* 6839 * If we've got a disk, use its blocksize in the block 6840 * descriptor. Otherwise, just set it to 0. 6841 */ 6842 if (dbd == 0) { 6843 if (control_dev == 0) 6844 scsi_ulto3b(lun->be_lun->blocksize, 6845 block_desc->block_len); 6846 else 6847 scsi_ulto3b(0, block_desc->block_len); 6848 } 6849 6850 switch (page_code) { 6851 case SMS_ALL_PAGES_PAGE: { 6852 int i, data_used; 6853 6854 data_used = header_len; 6855 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6856 struct ctl_page_index *page_index; 6857 6858 page_index = &lun->mode_pages.index[i]; 6859 6860 if ((control_dev != 0) 6861 && (page_index->page_flags & 6862 CTL_PAGE_FLAG_DISK_ONLY)) 6863 continue; 6864 6865 /* 6866 * We don't use this subpage if the user didn't 6867 * request all subpages. We already checked (above) 6868 * to make sure the user only specified a subpage 6869 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6870 */ 6871 if ((page_index->subpage != 0) 6872 && (subpage == SMS_SUBPAGE_PAGE_0)) 6873 continue; 6874 6875 /* 6876 * Call the handler, if it exists, to update the 6877 * page to the latest values. 6878 */ 6879 if (page_index->sense_handler != NULL) 6880 page_index->sense_handler(ctsio, page_index,pc); 6881 6882 memcpy(ctsio->kern_data_ptr + data_used, 6883 page_index->page_data + 6884 (page_index->page_len * pc), 6885 page_index->page_len); 6886 data_used += page_index->page_len; 6887 } 6888 break; 6889 } 6890 default: { 6891 int i, data_used; 6892 6893 data_used = header_len; 6894 6895 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6896 struct ctl_page_index *page_index; 6897 6898 page_index = &lun->mode_pages.index[i]; 6899 6900 /* Look for the right page code */ 6901 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6902 continue; 6903 6904 /* Look for the right subpage or the subpage wildcard*/ 6905 if ((page_index->subpage != subpage) 6906 && (subpage != SMS_SUBPAGE_ALL)) 6907 continue; 6908 6909 /* Make sure the page is supported for this dev type */ 6910 if ((control_dev != 0) 6911 && (page_index->page_flags & 6912 CTL_PAGE_FLAG_DISK_ONLY)) 6913 continue; 6914 6915 /* 6916 * Call the handler, if it exists, to update the 6917 * page to the latest values. 6918 */ 6919 if (page_index->sense_handler != NULL) 6920 page_index->sense_handler(ctsio, page_index,pc); 6921 6922 memcpy(ctsio->kern_data_ptr + data_used, 6923 page_index->page_data + 6924 (page_index->page_len * pc), 6925 page_index->page_len); 6926 data_used += page_index->page_len; 6927 } 6928 break; 6929 } 6930 } 6931 6932 ctl_set_success(ctsio); 6933 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6934 ctsio->be_move_done = ctl_config_move_done; 6935 ctl_datamove((union ctl_io *)ctsio); 6936 return (CTL_RETVAL_COMPLETE); 6937} 6938 6939int 6940ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6941 struct ctl_page_index *page_index, 6942 int pc) 6943{ 6944 struct ctl_lun *lun; 6945 struct scsi_log_param_header *phdr; 6946 uint8_t *data; 6947 uint64_t val; 6948 6949 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6950 data = page_index->page_data; 6951 6952 if (lun->backend->lun_attr != NULL && 6953 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6954 != UINT64_MAX) { 6955 phdr = (struct scsi_log_param_header *)data; 6956 scsi_ulto2b(0x0001, phdr->param_code); 6957 phdr->param_control = SLP_LBIN | SLP_LP; 6958 phdr->param_len = 8; 6959 data = (uint8_t *)(phdr + 1); 6960 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6961 data[4] = 0x02; /* per-pool */ 6962 data += phdr->param_len; 6963 } 6964 6965 if (lun->backend->lun_attr != NULL && 6966 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6967 != UINT64_MAX) { 6968 phdr = (struct scsi_log_param_header *)data; 6969 scsi_ulto2b(0x0002, phdr->param_code); 6970 phdr->param_control = SLP_LBIN | SLP_LP; 6971 phdr->param_len = 8; 6972 data = (uint8_t *)(phdr + 1); 6973 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6974 data[4] = 0x01; /* per-LUN */ 6975 data += phdr->param_len; 6976 } 6977 6978 if (lun->backend->lun_attr != NULL && 6979 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6980 != UINT64_MAX) { 6981 phdr = (struct scsi_log_param_header *)data; 6982 scsi_ulto2b(0x00f1, phdr->param_code); 6983 phdr->param_control = SLP_LBIN | SLP_LP; 6984 phdr->param_len = 8; 6985 data = (uint8_t *)(phdr + 1); 6986 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6987 data[4] = 0x02; /* per-pool */ 6988 data += phdr->param_len; 6989 } 6990 6991 if (lun->backend->lun_attr != NULL && 6992 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6993 != UINT64_MAX) { 6994 phdr = (struct scsi_log_param_header *)data; 6995 scsi_ulto2b(0x00f2, phdr->param_code); 6996 phdr->param_control = SLP_LBIN | SLP_LP; 6997 phdr->param_len = 8; 6998 data = (uint8_t *)(phdr + 1); 6999 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 7000 data[4] = 0x02; /* per-pool */ 7001 data += phdr->param_len; 7002 } 7003 7004 page_index->page_len = data - page_index->page_data; 7005 return (0); 7006} 7007 7008int 7009ctl_log_sense(struct ctl_scsiio *ctsio) 7010{ 7011 struct ctl_lun *lun; 7012 int i, pc, page_code, subpage; 7013 int alloc_len, total_len; 7014 struct ctl_page_index *page_index; 7015 struct scsi_log_sense *cdb; 7016 struct scsi_log_header *header; 7017 7018 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7019 7020 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7021 cdb = (struct scsi_log_sense *)ctsio->cdb; 7022 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7023 page_code = cdb->page & SLS_PAGE_CODE; 7024 subpage = cdb->subpage; 7025 alloc_len = scsi_2btoul(cdb->length); 7026 7027 page_index = NULL; 7028 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7029 page_index = &lun->log_pages.index[i]; 7030 7031 /* Look for the right page code */ 7032 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7033 continue; 7034 7035 /* Look for the right subpage or the subpage wildcard*/ 7036 if (page_index->subpage != subpage) 7037 continue; 7038 7039 break; 7040 } 7041 if (i >= CTL_NUM_LOG_PAGES) { 7042 ctl_set_invalid_field(ctsio, 7043 /*sks_valid*/ 1, 7044 /*command*/ 1, 7045 /*field*/ 2, 7046 /*bit_valid*/ 0, 7047 /*bit*/ 0); 7048 ctl_done((union ctl_io *)ctsio); 7049 return (CTL_RETVAL_COMPLETE); 7050 } 7051 7052 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7053 7054 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7055 ctsio->kern_sg_entries = 0; 7056 ctsio->kern_data_resid = 0; 7057 ctsio->kern_rel_offset = 0; 7058 if (total_len < alloc_len) { 7059 ctsio->residual = alloc_len - total_len; 7060 ctsio->kern_data_len = total_len; 7061 ctsio->kern_total_len = total_len; 7062 } else { 7063 ctsio->residual = 0; 7064 ctsio->kern_data_len = alloc_len; 7065 ctsio->kern_total_len = alloc_len; 7066 } 7067 7068 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7069 header->page = page_index->page_code; 7070 if (page_index->subpage) { 7071 header->page |= SL_SPF; 7072 header->subpage = page_index->subpage; 7073 } 7074 scsi_ulto2b(page_index->page_len, header->datalen); 7075 7076 /* 7077 * Call the handler, if it exists, to update the 7078 * page to the latest values. 7079 */ 7080 if (page_index->sense_handler != NULL) 7081 page_index->sense_handler(ctsio, page_index, pc); 7082 7083 memcpy(header + 1, page_index->page_data, page_index->page_len); 7084 7085 ctl_set_success(ctsio); 7086 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7087 ctsio->be_move_done = ctl_config_move_done; 7088 ctl_datamove((union ctl_io *)ctsio); 7089 return (CTL_RETVAL_COMPLETE); 7090} 7091 7092int 7093ctl_read_capacity(struct ctl_scsiio *ctsio) 7094{ 7095 struct scsi_read_capacity *cdb; 7096 struct scsi_read_capacity_data *data; 7097 struct ctl_lun *lun; 7098 uint32_t lba; 7099 7100 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7101 7102 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7103 7104 lba = scsi_4btoul(cdb->addr); 7105 if (((cdb->pmi & SRC_PMI) == 0) 7106 && (lba != 0)) { 7107 ctl_set_invalid_field(/*ctsio*/ ctsio, 7108 /*sks_valid*/ 1, 7109 /*command*/ 1, 7110 /*field*/ 2, 7111 /*bit_valid*/ 0, 7112 /*bit*/ 0); 7113 ctl_done((union ctl_io *)ctsio); 7114 return (CTL_RETVAL_COMPLETE); 7115 } 7116 7117 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7118 7119 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7120 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7121 ctsio->residual = 0; 7122 ctsio->kern_data_len = sizeof(*data); 7123 ctsio->kern_total_len = sizeof(*data); 7124 ctsio->kern_data_resid = 0; 7125 ctsio->kern_rel_offset = 0; 7126 ctsio->kern_sg_entries = 0; 7127 7128 /* 7129 * If the maximum LBA is greater than 0xfffffffe, the user must 7130 * issue a SERVICE ACTION IN (16) command, with the read capacity 7131 * serivce action set. 7132 */ 7133 if (lun->be_lun->maxlba > 0xfffffffe) 7134 scsi_ulto4b(0xffffffff, data->addr); 7135 else 7136 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7137 7138 /* 7139 * XXX KDM this may not be 512 bytes... 7140 */ 7141 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7142 7143 ctl_set_success(ctsio); 7144 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7145 ctsio->be_move_done = ctl_config_move_done; 7146 ctl_datamove((union ctl_io *)ctsio); 7147 return (CTL_RETVAL_COMPLETE); 7148} 7149 7150int 7151ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7152{ 7153 struct scsi_read_capacity_16 *cdb; 7154 struct scsi_read_capacity_data_long *data; 7155 struct ctl_lun *lun; 7156 uint64_t lba; 7157 uint32_t alloc_len; 7158 7159 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7160 7161 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7162 7163 alloc_len = scsi_4btoul(cdb->alloc_len); 7164 lba = scsi_8btou64(cdb->addr); 7165 7166 if ((cdb->reladr & SRC16_PMI) 7167 && (lba != 0)) { 7168 ctl_set_invalid_field(/*ctsio*/ ctsio, 7169 /*sks_valid*/ 1, 7170 /*command*/ 1, 7171 /*field*/ 2, 7172 /*bit_valid*/ 0, 7173 /*bit*/ 0); 7174 ctl_done((union ctl_io *)ctsio); 7175 return (CTL_RETVAL_COMPLETE); 7176 } 7177 7178 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7179 7180 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7181 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7182 7183 if (sizeof(*data) < alloc_len) { 7184 ctsio->residual = alloc_len - sizeof(*data); 7185 ctsio->kern_data_len = sizeof(*data); 7186 ctsio->kern_total_len = sizeof(*data); 7187 } else { 7188 ctsio->residual = 0; 7189 ctsio->kern_data_len = alloc_len; 7190 ctsio->kern_total_len = alloc_len; 7191 } 7192 ctsio->kern_data_resid = 0; 7193 ctsio->kern_rel_offset = 0; 7194 ctsio->kern_sg_entries = 0; 7195 7196 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7197 /* XXX KDM this may not be 512 bytes... */ 7198 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7199 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7200 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7201 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7202 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7203 7204 ctl_set_success(ctsio); 7205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7206 ctsio->be_move_done = ctl_config_move_done; 7207 ctl_datamove((union ctl_io *)ctsio); 7208 return (CTL_RETVAL_COMPLETE); 7209} 7210 7211int 7212ctl_get_lba_status(struct ctl_scsiio *ctsio) 7213{ 7214 struct scsi_get_lba_status *cdb; 7215 struct scsi_get_lba_status_data *data; 7216 struct ctl_lun *lun; 7217 struct ctl_lba_len_flags *lbalen; 7218 uint64_t lba; 7219 uint32_t alloc_len, total_len; 7220 int retval; 7221 7222 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7223 7224 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7225 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7226 lba = scsi_8btou64(cdb->addr); 7227 alloc_len = scsi_4btoul(cdb->alloc_len); 7228 7229 if (lba > lun->be_lun->maxlba) { 7230 ctl_set_lba_out_of_range(ctsio); 7231 ctl_done((union ctl_io *)ctsio); 7232 return (CTL_RETVAL_COMPLETE); 7233 } 7234 7235 total_len = sizeof(*data) + sizeof(data->descr[0]); 7236 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7237 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7238 7239 if (total_len < alloc_len) { 7240 ctsio->residual = alloc_len - total_len; 7241 ctsio->kern_data_len = total_len; 7242 ctsio->kern_total_len = total_len; 7243 } else { 7244 ctsio->residual = 0; 7245 ctsio->kern_data_len = alloc_len; 7246 ctsio->kern_total_len = alloc_len; 7247 } 7248 ctsio->kern_data_resid = 0; 7249 ctsio->kern_rel_offset = 0; 7250 ctsio->kern_sg_entries = 0; 7251 7252 /* Fill dummy data in case backend can't tell anything. */ 7253 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7254 scsi_u64to8b(lba, data->descr[0].addr); 7255 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7256 data->descr[0].length); 7257 data->descr[0].status = 0; /* Mapped or unknown. */ 7258 7259 ctl_set_success(ctsio); 7260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7261 ctsio->be_move_done = ctl_config_move_done; 7262 7263 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7264 lbalen->lba = lba; 7265 lbalen->len = total_len; 7266 lbalen->flags = 0; 7267 retval = lun->backend->config_read((union ctl_io *)ctsio); 7268 return (CTL_RETVAL_COMPLETE); 7269} 7270 7271int 7272ctl_read_defect(struct ctl_scsiio *ctsio) 7273{ 7274 struct scsi_read_defect_data_10 *ccb10; 7275 struct scsi_read_defect_data_12 *ccb12; 7276 struct scsi_read_defect_data_hdr_10 *data10; 7277 struct scsi_read_defect_data_hdr_12 *data12; 7278 uint32_t alloc_len, data_len; 7279 uint8_t format; 7280 7281 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7282 7283 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7284 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7285 format = ccb10->format; 7286 alloc_len = scsi_2btoul(ccb10->alloc_length); 7287 data_len = sizeof(*data10); 7288 } else { 7289 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7290 format = ccb12->format; 7291 alloc_len = scsi_4btoul(ccb12->alloc_length); 7292 data_len = sizeof(*data12); 7293 } 7294 if (alloc_len == 0) { 7295 ctl_set_success(ctsio); 7296 ctl_done((union ctl_io *)ctsio); 7297 return (CTL_RETVAL_COMPLETE); 7298 } 7299 7300 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7301 if (data_len < alloc_len) { 7302 ctsio->residual = alloc_len - data_len; 7303 ctsio->kern_data_len = data_len; 7304 ctsio->kern_total_len = data_len; 7305 } else { 7306 ctsio->residual = 0; 7307 ctsio->kern_data_len = alloc_len; 7308 ctsio->kern_total_len = alloc_len; 7309 } 7310 ctsio->kern_data_resid = 0; 7311 ctsio->kern_rel_offset = 0; 7312 ctsio->kern_sg_entries = 0; 7313 7314 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7315 data10 = (struct scsi_read_defect_data_hdr_10 *) 7316 ctsio->kern_data_ptr; 7317 data10->format = format; 7318 scsi_ulto2b(0, data10->length); 7319 } else { 7320 data12 = (struct scsi_read_defect_data_hdr_12 *) 7321 ctsio->kern_data_ptr; 7322 data12->format = format; 7323 scsi_ulto2b(0, data12->generation); 7324 scsi_ulto4b(0, data12->length); 7325 } 7326 7327 ctl_set_success(ctsio); 7328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7329 ctsio->be_move_done = ctl_config_move_done; 7330 ctl_datamove((union ctl_io *)ctsio); 7331 return (CTL_RETVAL_COMPLETE); 7332} 7333 7334int 7335ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7336{ 7337 struct scsi_maintenance_in *cdb; 7338 int retval; 7339 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7340 int num_target_port_groups, num_target_ports; 7341 struct ctl_lun *lun; 7342 struct ctl_softc *softc; 7343 struct ctl_port *port; 7344 struct scsi_target_group_data *rtg_ptr; 7345 struct scsi_target_group_data_extended *rtg_ext_ptr; 7346 struct scsi_target_port_group_descriptor *tpg_desc; 7347 7348 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7349 7350 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7351 softc = control_softc; 7352 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7353 7354 retval = CTL_RETVAL_COMPLETE; 7355 7356 switch (cdb->byte2 & STG_PDF_MASK) { 7357 case STG_PDF_LENGTH: 7358 ext = 0; 7359 break; 7360 case STG_PDF_EXTENDED: 7361 ext = 1; 7362 break; 7363 default: 7364 ctl_set_invalid_field(/*ctsio*/ ctsio, 7365 /*sks_valid*/ 1, 7366 /*command*/ 1, 7367 /*field*/ 2, 7368 /*bit_valid*/ 1, 7369 /*bit*/ 5); 7370 ctl_done((union ctl_io *)ctsio); 7371 return(retval); 7372 } 7373 7374 if (softc->is_single) 7375 num_target_port_groups = 1; 7376 else 7377 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7378 num_target_ports = 0; 7379 mtx_lock(&softc->ctl_lock); 7380 STAILQ_FOREACH(port, &softc->port_list, links) { 7381 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7382 continue; 7383 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7384 continue; 7385 num_target_ports++; 7386 } 7387 mtx_unlock(&softc->ctl_lock); 7388 7389 if (ext) 7390 total_len = sizeof(struct scsi_target_group_data_extended); 7391 else 7392 total_len = sizeof(struct scsi_target_group_data); 7393 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7394 num_target_port_groups + 7395 sizeof(struct scsi_target_port_descriptor) * 7396 num_target_ports * num_target_port_groups; 7397 7398 alloc_len = scsi_4btoul(cdb->length); 7399 7400 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7401 7402 ctsio->kern_sg_entries = 0; 7403 7404 if (total_len < alloc_len) { 7405 ctsio->residual = alloc_len - total_len; 7406 ctsio->kern_data_len = total_len; 7407 ctsio->kern_total_len = total_len; 7408 } else { 7409 ctsio->residual = 0; 7410 ctsio->kern_data_len = alloc_len; 7411 ctsio->kern_total_len = alloc_len; 7412 } 7413 ctsio->kern_data_resid = 0; 7414 ctsio->kern_rel_offset = 0; 7415 7416 if (ext) { 7417 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7418 ctsio->kern_data_ptr; 7419 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7420 rtg_ext_ptr->format_type = 0x10; 7421 rtg_ext_ptr->implicit_transition_time = 0; 7422 tpg_desc = &rtg_ext_ptr->groups[0]; 7423 } else { 7424 rtg_ptr = (struct scsi_target_group_data *) 7425 ctsio->kern_data_ptr; 7426 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7427 tpg_desc = &rtg_ptr->groups[0]; 7428 } 7429 7430 mtx_lock(&softc->ctl_lock); 7431 pg = softc->port_offset / CTL_MAX_PORTS; 7432 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7433 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7434 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7435 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7436 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7437 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7438 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7439 } else { 7440 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7441 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7442 } 7443 } else { 7444 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7445 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7446 } 7447 for (g = 0; g < num_target_port_groups; g++) { 7448 tpg_desc->pref_state = (g == pg) ? gs : os; 7449 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7450 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7451 tpg_desc->status = TPG_IMPLICIT; 7452 pc = 0; 7453 STAILQ_FOREACH(port, &softc->port_list, links) { 7454 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7455 continue; 7456 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7457 CTL_MAX_LUNS) 7458 continue; 7459 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7460 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7461 relative_target_port_identifier); 7462 pc++; 7463 } 7464 tpg_desc->target_port_count = pc; 7465 tpg_desc = (struct scsi_target_port_group_descriptor *) 7466 &tpg_desc->descriptors[pc]; 7467 } 7468 mtx_unlock(&softc->ctl_lock); 7469 7470 ctl_set_success(ctsio); 7471 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7472 ctsio->be_move_done = ctl_config_move_done; 7473 ctl_datamove((union ctl_io *)ctsio); 7474 return(retval); 7475} 7476 7477int 7478ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7479{ 7480 struct ctl_lun *lun; 7481 struct scsi_report_supported_opcodes *cdb; 7482 const struct ctl_cmd_entry *entry, *sentry; 7483 struct scsi_report_supported_opcodes_all *all; 7484 struct scsi_report_supported_opcodes_descr *descr; 7485 struct scsi_report_supported_opcodes_one *one; 7486 int retval; 7487 int alloc_len, total_len; 7488 int opcode, service_action, i, j, num; 7489 7490 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7491 7492 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7493 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7494 7495 retval = CTL_RETVAL_COMPLETE; 7496 7497 opcode = cdb->requested_opcode; 7498 service_action = scsi_2btoul(cdb->requested_service_action); 7499 switch (cdb->options & RSO_OPTIONS_MASK) { 7500 case RSO_OPTIONS_ALL: 7501 num = 0; 7502 for (i = 0; i < 256; i++) { 7503 entry = &ctl_cmd_table[i]; 7504 if (entry->flags & CTL_CMD_FLAG_SA5) { 7505 for (j = 0; j < 32; j++) { 7506 sentry = &((const struct ctl_cmd_entry *) 7507 entry->execute)[j]; 7508 if (ctl_cmd_applicable( 7509 lun->be_lun->lun_type, sentry)) 7510 num++; 7511 } 7512 } else { 7513 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7514 entry)) 7515 num++; 7516 } 7517 } 7518 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7519 num * sizeof(struct scsi_report_supported_opcodes_descr); 7520 break; 7521 case RSO_OPTIONS_OC: 7522 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7523 ctl_set_invalid_field(/*ctsio*/ ctsio, 7524 /*sks_valid*/ 1, 7525 /*command*/ 1, 7526 /*field*/ 2, 7527 /*bit_valid*/ 1, 7528 /*bit*/ 2); 7529 ctl_done((union ctl_io *)ctsio); 7530 return (CTL_RETVAL_COMPLETE); 7531 } 7532 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7533 break; 7534 case RSO_OPTIONS_OC_SA: 7535 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7536 service_action >= 32) { 7537 ctl_set_invalid_field(/*ctsio*/ ctsio, 7538 /*sks_valid*/ 1, 7539 /*command*/ 1, 7540 /*field*/ 2, 7541 /*bit_valid*/ 1, 7542 /*bit*/ 2); 7543 ctl_done((union ctl_io *)ctsio); 7544 return (CTL_RETVAL_COMPLETE); 7545 } 7546 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7547 break; 7548 default: 7549 ctl_set_invalid_field(/*ctsio*/ ctsio, 7550 /*sks_valid*/ 1, 7551 /*command*/ 1, 7552 /*field*/ 2, 7553 /*bit_valid*/ 1, 7554 /*bit*/ 2); 7555 ctl_done((union ctl_io *)ctsio); 7556 return (CTL_RETVAL_COMPLETE); 7557 } 7558 7559 alloc_len = scsi_4btoul(cdb->length); 7560 7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7562 7563 ctsio->kern_sg_entries = 0; 7564 7565 if (total_len < alloc_len) { 7566 ctsio->residual = alloc_len - total_len; 7567 ctsio->kern_data_len = total_len; 7568 ctsio->kern_total_len = total_len; 7569 } else { 7570 ctsio->residual = 0; 7571 ctsio->kern_data_len = alloc_len; 7572 ctsio->kern_total_len = alloc_len; 7573 } 7574 ctsio->kern_data_resid = 0; 7575 ctsio->kern_rel_offset = 0; 7576 7577 switch (cdb->options & RSO_OPTIONS_MASK) { 7578 case RSO_OPTIONS_ALL: 7579 all = (struct scsi_report_supported_opcodes_all *) 7580 ctsio->kern_data_ptr; 7581 num = 0; 7582 for (i = 0; i < 256; i++) { 7583 entry = &ctl_cmd_table[i]; 7584 if (entry->flags & CTL_CMD_FLAG_SA5) { 7585 for (j = 0; j < 32; j++) { 7586 sentry = &((const struct ctl_cmd_entry *) 7587 entry->execute)[j]; 7588 if (!ctl_cmd_applicable( 7589 lun->be_lun->lun_type, sentry)) 7590 continue; 7591 descr = &all->descr[num++]; 7592 descr->opcode = i; 7593 scsi_ulto2b(j, descr->service_action); 7594 descr->flags = RSO_SERVACTV; 7595 scsi_ulto2b(sentry->length, 7596 descr->cdb_length); 7597 } 7598 } else { 7599 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7600 entry)) 7601 continue; 7602 descr = &all->descr[num++]; 7603 descr->opcode = i; 7604 scsi_ulto2b(0, descr->service_action); 7605 descr->flags = 0; 7606 scsi_ulto2b(entry->length, descr->cdb_length); 7607 } 7608 } 7609 scsi_ulto4b( 7610 num * sizeof(struct scsi_report_supported_opcodes_descr), 7611 all->length); 7612 break; 7613 case RSO_OPTIONS_OC: 7614 one = (struct scsi_report_supported_opcodes_one *) 7615 ctsio->kern_data_ptr; 7616 entry = &ctl_cmd_table[opcode]; 7617 goto fill_one; 7618 case RSO_OPTIONS_OC_SA: 7619 one = (struct scsi_report_supported_opcodes_one *) 7620 ctsio->kern_data_ptr; 7621 entry = &ctl_cmd_table[opcode]; 7622 entry = &((const struct ctl_cmd_entry *) 7623 entry->execute)[service_action]; 7624fill_one: 7625 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7626 one->support = 3; 7627 scsi_ulto2b(entry->length, one->cdb_length); 7628 one->cdb_usage[0] = opcode; 7629 memcpy(&one->cdb_usage[1], entry->usage, 7630 entry->length - 1); 7631 } else 7632 one->support = 1; 7633 break; 7634 } 7635 7636 ctl_set_success(ctsio); 7637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7638 ctsio->be_move_done = ctl_config_move_done; 7639 ctl_datamove((union ctl_io *)ctsio); 7640 return(retval); 7641} 7642 7643int 7644ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7645{ 7646 struct scsi_report_supported_tmf *cdb; 7647 struct scsi_report_supported_tmf_data *data; 7648 int retval; 7649 int alloc_len, total_len; 7650 7651 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7652 7653 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7654 7655 retval = CTL_RETVAL_COMPLETE; 7656 7657 total_len = sizeof(struct scsi_report_supported_tmf_data); 7658 alloc_len = scsi_4btoul(cdb->length); 7659 7660 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7661 7662 ctsio->kern_sg_entries = 0; 7663 7664 if (total_len < alloc_len) { 7665 ctsio->residual = alloc_len - total_len; 7666 ctsio->kern_data_len = total_len; 7667 ctsio->kern_total_len = total_len; 7668 } else { 7669 ctsio->residual = 0; 7670 ctsio->kern_data_len = alloc_len; 7671 ctsio->kern_total_len = alloc_len; 7672 } 7673 ctsio->kern_data_resid = 0; 7674 ctsio->kern_rel_offset = 0; 7675 7676 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7677 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7678 data->byte2 |= RST_ITNRS; 7679 7680 ctl_set_success(ctsio); 7681 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7682 ctsio->be_move_done = ctl_config_move_done; 7683 ctl_datamove((union ctl_io *)ctsio); 7684 return (retval); 7685} 7686 7687int 7688ctl_report_timestamp(struct ctl_scsiio *ctsio) 7689{ 7690 struct scsi_report_timestamp *cdb; 7691 struct scsi_report_timestamp_data *data; 7692 struct timeval tv; 7693 int64_t timestamp; 7694 int retval; 7695 int alloc_len, total_len; 7696 7697 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7698 7699 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7700 7701 retval = CTL_RETVAL_COMPLETE; 7702 7703 total_len = sizeof(struct scsi_report_timestamp_data); 7704 alloc_len = scsi_4btoul(cdb->length); 7705 7706 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7707 7708 ctsio->kern_sg_entries = 0; 7709 7710 if (total_len < alloc_len) { 7711 ctsio->residual = alloc_len - total_len; 7712 ctsio->kern_data_len = total_len; 7713 ctsio->kern_total_len = total_len; 7714 } else { 7715 ctsio->residual = 0; 7716 ctsio->kern_data_len = alloc_len; 7717 ctsio->kern_total_len = alloc_len; 7718 } 7719 ctsio->kern_data_resid = 0; 7720 ctsio->kern_rel_offset = 0; 7721 7722 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7723 scsi_ulto2b(sizeof(*data) - 2, data->length); 7724 data->origin = RTS_ORIG_OUTSIDE; 7725 getmicrotime(&tv); 7726 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7727 scsi_ulto4b(timestamp >> 16, data->timestamp); 7728 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7729 7730 ctl_set_success(ctsio); 7731 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7732 ctsio->be_move_done = ctl_config_move_done; 7733 ctl_datamove((union ctl_io *)ctsio); 7734 return (retval); 7735} 7736 7737int 7738ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7739{ 7740 struct scsi_per_res_in *cdb; 7741 int alloc_len, total_len = 0; 7742 /* struct scsi_per_res_in_rsrv in_data; */ 7743 struct ctl_lun *lun; 7744 struct ctl_softc *softc; 7745 uint64_t key; 7746 7747 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7748 7749 softc = control_softc; 7750 7751 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7752 7753 alloc_len = scsi_2btoul(cdb->length); 7754 7755 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7756 7757retry: 7758 mtx_lock(&lun->lun_lock); 7759 switch (cdb->action) { 7760 case SPRI_RK: /* read keys */ 7761 total_len = sizeof(struct scsi_per_res_in_keys) + 7762 lun->pr_key_count * 7763 sizeof(struct scsi_per_res_key); 7764 break; 7765 case SPRI_RR: /* read reservation */ 7766 if (lun->flags & CTL_LUN_PR_RESERVED) 7767 total_len = sizeof(struct scsi_per_res_in_rsrv); 7768 else 7769 total_len = sizeof(struct scsi_per_res_in_header); 7770 break; 7771 case SPRI_RC: /* report capabilities */ 7772 total_len = sizeof(struct scsi_per_res_cap); 7773 break; 7774 case SPRI_RS: /* read full status */ 7775 total_len = sizeof(struct scsi_per_res_in_header) + 7776 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7777 lun->pr_key_count; 7778 break; 7779 default: 7780 panic("Invalid PR type %x", cdb->action); 7781 } 7782 mtx_unlock(&lun->lun_lock); 7783 7784 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7785 7786 if (total_len < alloc_len) { 7787 ctsio->residual = alloc_len - total_len; 7788 ctsio->kern_data_len = total_len; 7789 ctsio->kern_total_len = total_len; 7790 } else { 7791 ctsio->residual = 0; 7792 ctsio->kern_data_len = alloc_len; 7793 ctsio->kern_total_len = alloc_len; 7794 } 7795 7796 ctsio->kern_data_resid = 0; 7797 ctsio->kern_rel_offset = 0; 7798 ctsio->kern_sg_entries = 0; 7799 7800 mtx_lock(&lun->lun_lock); 7801 switch (cdb->action) { 7802 case SPRI_RK: { // read keys 7803 struct scsi_per_res_in_keys *res_keys; 7804 int i, key_count; 7805 7806 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7807 7808 /* 7809 * We had to drop the lock to allocate our buffer, which 7810 * leaves time for someone to come in with another 7811 * persistent reservation. (That is unlikely, though, 7812 * since this should be the only persistent reservation 7813 * command active right now.) 7814 */ 7815 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7816 (lun->pr_key_count * 7817 sizeof(struct scsi_per_res_key)))){ 7818 mtx_unlock(&lun->lun_lock); 7819 free(ctsio->kern_data_ptr, M_CTL); 7820 printf("%s: reservation length changed, retrying\n", 7821 __func__); 7822 goto retry; 7823 } 7824 7825 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7826 7827 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7828 lun->pr_key_count, res_keys->header.length); 7829 7830 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7831 if ((key = ctl_get_prkey(lun, i)) == 0) 7832 continue; 7833 7834 /* 7835 * We used lun->pr_key_count to calculate the 7836 * size to allocate. If it turns out the number of 7837 * initiators with the registered flag set is 7838 * larger than that (i.e. they haven't been kept in 7839 * sync), we've got a problem. 7840 */ 7841 if (key_count >= lun->pr_key_count) { 7842#ifdef NEEDTOPORT 7843 csevent_log(CSC_CTL | CSC_SHELF_SW | 7844 CTL_PR_ERROR, 7845 csevent_LogType_Fault, 7846 csevent_AlertLevel_Yellow, 7847 csevent_FRU_ShelfController, 7848 csevent_FRU_Firmware, 7849 csevent_FRU_Unknown, 7850 "registered keys %d >= key " 7851 "count %d", key_count, 7852 lun->pr_key_count); 7853#endif 7854 key_count++; 7855 continue; 7856 } 7857 scsi_u64to8b(key, res_keys->keys[key_count].key); 7858 key_count++; 7859 } 7860 break; 7861 } 7862 case SPRI_RR: { // read reservation 7863 struct scsi_per_res_in_rsrv *res; 7864 int tmp_len, header_only; 7865 7866 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7867 7868 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7869 7870 if (lun->flags & CTL_LUN_PR_RESERVED) 7871 { 7872 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7873 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7874 res->header.length); 7875 header_only = 0; 7876 } else { 7877 tmp_len = sizeof(struct scsi_per_res_in_header); 7878 scsi_ulto4b(0, res->header.length); 7879 header_only = 1; 7880 } 7881 7882 /* 7883 * We had to drop the lock to allocate our buffer, which 7884 * leaves time for someone to come in with another 7885 * persistent reservation. (That is unlikely, though, 7886 * since this should be the only persistent reservation 7887 * command active right now.) 7888 */ 7889 if (tmp_len != total_len) { 7890 mtx_unlock(&lun->lun_lock); 7891 free(ctsio->kern_data_ptr, M_CTL); 7892 printf("%s: reservation status changed, retrying\n", 7893 __func__); 7894 goto retry; 7895 } 7896 7897 /* 7898 * No reservation held, so we're done. 7899 */ 7900 if (header_only != 0) 7901 break; 7902 7903 /* 7904 * If the registration is an All Registrants type, the key 7905 * is 0, since it doesn't really matter. 7906 */ 7907 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7908 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7909 res->data.reservation); 7910 } 7911 res->data.scopetype = lun->res_type; 7912 break; 7913 } 7914 case SPRI_RC: //report capabilities 7915 { 7916 struct scsi_per_res_cap *res_cap; 7917 uint16_t type_mask; 7918 7919 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7920 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7921 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7922 type_mask = SPRI_TM_WR_EX_AR | 7923 SPRI_TM_EX_AC_RO | 7924 SPRI_TM_WR_EX_RO | 7925 SPRI_TM_EX_AC | 7926 SPRI_TM_WR_EX | 7927 SPRI_TM_EX_AC_AR; 7928 scsi_ulto2b(type_mask, res_cap->type_mask); 7929 break; 7930 } 7931 case SPRI_RS: { // read full status 7932 struct scsi_per_res_in_full *res_status; 7933 struct scsi_per_res_in_full_desc *res_desc; 7934 struct ctl_port *port; 7935 int i, len; 7936 7937 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7938 7939 /* 7940 * We had to drop the lock to allocate our buffer, which 7941 * leaves time for someone to come in with another 7942 * persistent reservation. (That is unlikely, though, 7943 * since this should be the only persistent reservation 7944 * command active right now.) 7945 */ 7946 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7947 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7948 lun->pr_key_count)){ 7949 mtx_unlock(&lun->lun_lock); 7950 free(ctsio->kern_data_ptr, M_CTL); 7951 printf("%s: reservation length changed, retrying\n", 7952 __func__); 7953 goto retry; 7954 } 7955 7956 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7957 7958 res_desc = &res_status->desc[0]; 7959 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7960 if ((key = ctl_get_prkey(lun, i)) == 0) 7961 continue; 7962 7963 scsi_u64to8b(key, res_desc->res_key.key); 7964 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7965 (lun->pr_res_idx == i || 7966 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7967 res_desc->flags = SPRI_FULL_R_HOLDER; 7968 res_desc->scopetype = lun->res_type; 7969 } 7970 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7971 res_desc->rel_trgt_port_id); 7972 len = 0; 7973 port = softc->ctl_ports[ 7974 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7975 if (port != NULL) 7976 len = ctl_create_iid(port, 7977 i % CTL_MAX_INIT_PER_PORT, 7978 res_desc->transport_id); 7979 scsi_ulto4b(len, res_desc->additional_length); 7980 res_desc = (struct scsi_per_res_in_full_desc *) 7981 &res_desc->transport_id[len]; 7982 } 7983 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7984 res_status->header.length); 7985 break; 7986 } 7987 default: 7988 /* 7989 * This is a bug, because we just checked for this above, 7990 * and should have returned an error. 7991 */ 7992 panic("Invalid PR type %x", cdb->action); 7993 break; /* NOTREACHED */ 7994 } 7995 mtx_unlock(&lun->lun_lock); 7996 7997 ctl_set_success(ctsio); 7998 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7999 ctsio->be_move_done = ctl_config_move_done; 8000 ctl_datamove((union ctl_io *)ctsio); 8001 return (CTL_RETVAL_COMPLETE); 8002} 8003 8004static void 8005ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 8006{ 8007 int off = lun->ctl_softc->persis_offset; 8008 8009 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 8010 ctl_est_ua(lun, residx - off, ua); 8011} 8012 8013/* 8014 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8015 * it should return. 8016 */ 8017static int 8018ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8019 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8020 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8021 struct scsi_per_res_out_parms* param) 8022{ 8023 union ctl_ha_msg persis_io; 8024 int retval, i; 8025 int isc_retval; 8026 8027 retval = 0; 8028 8029 mtx_lock(&lun->lun_lock); 8030 if (sa_res_key == 0) { 8031 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8032 /* validate scope and type */ 8033 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8034 SPR_LU_SCOPE) { 8035 mtx_unlock(&lun->lun_lock); 8036 ctl_set_invalid_field(/*ctsio*/ ctsio, 8037 /*sks_valid*/ 1, 8038 /*command*/ 1, 8039 /*field*/ 2, 8040 /*bit_valid*/ 1, 8041 /*bit*/ 4); 8042 ctl_done((union ctl_io *)ctsio); 8043 return (1); 8044 } 8045 8046 if (type>8 || type==2 || type==4 || type==0) { 8047 mtx_unlock(&lun->lun_lock); 8048 ctl_set_invalid_field(/*ctsio*/ ctsio, 8049 /*sks_valid*/ 1, 8050 /*command*/ 1, 8051 /*field*/ 2, 8052 /*bit_valid*/ 1, 8053 /*bit*/ 0); 8054 ctl_done((union ctl_io *)ctsio); 8055 return (1); 8056 } 8057 8058 /* 8059 * Unregister everybody else and build UA for 8060 * them 8061 */ 8062 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8063 if (i == residx || ctl_get_prkey(lun, i) == 0) 8064 continue; 8065 8066 ctl_clr_prkey(lun, i); 8067 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8068 } 8069 lun->pr_key_count = 1; 8070 lun->res_type = type; 8071 if (lun->res_type != SPR_TYPE_WR_EX_AR 8072 && lun->res_type != SPR_TYPE_EX_AC_AR) 8073 lun->pr_res_idx = residx; 8074 8075 /* send msg to other side */ 8076 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8077 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8078 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8079 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8080 persis_io.pr.pr_info.res_type = type; 8081 memcpy(persis_io.pr.pr_info.sa_res_key, 8082 param->serv_act_res_key, 8083 sizeof(param->serv_act_res_key)); 8084 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8085 &persis_io, sizeof(persis_io), 0)) > 8086 CTL_HA_STATUS_SUCCESS) { 8087 printf("CTL:Persis Out error returned " 8088 "from ctl_ha_msg_send %d\n", 8089 isc_retval); 8090 } 8091 } else { 8092 /* not all registrants */ 8093 mtx_unlock(&lun->lun_lock); 8094 free(ctsio->kern_data_ptr, M_CTL); 8095 ctl_set_invalid_field(ctsio, 8096 /*sks_valid*/ 1, 8097 /*command*/ 0, 8098 /*field*/ 8, 8099 /*bit_valid*/ 0, 8100 /*bit*/ 0); 8101 ctl_done((union ctl_io *)ctsio); 8102 return (1); 8103 } 8104 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8105 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8106 int found = 0; 8107 8108 if (res_key == sa_res_key) { 8109 /* special case */ 8110 /* 8111 * The spec implies this is not good but doesn't 8112 * say what to do. There are two choices either 8113 * generate a res conflict or check condition 8114 * with illegal field in parameter data. Since 8115 * that is what is done when the sa_res_key is 8116 * zero I'll take that approach since this has 8117 * to do with the sa_res_key. 8118 */ 8119 mtx_unlock(&lun->lun_lock); 8120 free(ctsio->kern_data_ptr, M_CTL); 8121 ctl_set_invalid_field(ctsio, 8122 /*sks_valid*/ 1, 8123 /*command*/ 0, 8124 /*field*/ 8, 8125 /*bit_valid*/ 0, 8126 /*bit*/ 0); 8127 ctl_done((union ctl_io *)ctsio); 8128 return (1); 8129 } 8130 8131 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8132 if (ctl_get_prkey(lun, i) != sa_res_key) 8133 continue; 8134 8135 found = 1; 8136 ctl_clr_prkey(lun, i); 8137 lun->pr_key_count--; 8138 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8139 } 8140 if (!found) { 8141 mtx_unlock(&lun->lun_lock); 8142 free(ctsio->kern_data_ptr, M_CTL); 8143 ctl_set_reservation_conflict(ctsio); 8144 ctl_done((union ctl_io *)ctsio); 8145 return (CTL_RETVAL_COMPLETE); 8146 } 8147 /* send msg to other side */ 8148 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8149 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8150 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8151 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8152 persis_io.pr.pr_info.res_type = type; 8153 memcpy(persis_io.pr.pr_info.sa_res_key, 8154 param->serv_act_res_key, 8155 sizeof(param->serv_act_res_key)); 8156 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8157 &persis_io, sizeof(persis_io), 0)) > 8158 CTL_HA_STATUS_SUCCESS) { 8159 printf("CTL:Persis Out error returned from " 8160 "ctl_ha_msg_send %d\n", isc_retval); 8161 } 8162 } else { 8163 /* Reserved but not all registrants */ 8164 /* sa_res_key is res holder */ 8165 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8166 /* validate scope and type */ 8167 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8168 SPR_LU_SCOPE) { 8169 mtx_unlock(&lun->lun_lock); 8170 ctl_set_invalid_field(/*ctsio*/ ctsio, 8171 /*sks_valid*/ 1, 8172 /*command*/ 1, 8173 /*field*/ 2, 8174 /*bit_valid*/ 1, 8175 /*bit*/ 4); 8176 ctl_done((union ctl_io *)ctsio); 8177 return (1); 8178 } 8179 8180 if (type>8 || type==2 || type==4 || type==0) { 8181 mtx_unlock(&lun->lun_lock); 8182 ctl_set_invalid_field(/*ctsio*/ ctsio, 8183 /*sks_valid*/ 1, 8184 /*command*/ 1, 8185 /*field*/ 2, 8186 /*bit_valid*/ 1, 8187 /*bit*/ 0); 8188 ctl_done((union ctl_io *)ctsio); 8189 return (1); 8190 } 8191 8192 /* 8193 * Do the following: 8194 * if sa_res_key != res_key remove all 8195 * registrants w/sa_res_key and generate UA 8196 * for these registrants(Registrations 8197 * Preempted) if it wasn't an exclusive 8198 * reservation generate UA(Reservations 8199 * Preempted) for all other registered nexuses 8200 * if the type has changed. Establish the new 8201 * reservation and holder. If res_key and 8202 * sa_res_key are the same do the above 8203 * except don't unregister the res holder. 8204 */ 8205 8206 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8207 if (i == residx || ctl_get_prkey(lun, i) == 0) 8208 continue; 8209 8210 if (sa_res_key == ctl_get_prkey(lun, i)) { 8211 ctl_clr_prkey(lun, i); 8212 lun->pr_key_count--; 8213 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8214 } else if (type != lun->res_type 8215 && (lun->res_type == SPR_TYPE_WR_EX_RO 8216 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8217 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8218 } 8219 } 8220 lun->res_type = type; 8221 if (lun->res_type != SPR_TYPE_WR_EX_AR 8222 && lun->res_type != SPR_TYPE_EX_AC_AR) 8223 lun->pr_res_idx = residx; 8224 else 8225 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8226 8227 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8228 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8229 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8230 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8231 persis_io.pr.pr_info.res_type = type; 8232 memcpy(persis_io.pr.pr_info.sa_res_key, 8233 param->serv_act_res_key, 8234 sizeof(param->serv_act_res_key)); 8235 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8236 &persis_io, sizeof(persis_io), 0)) > 8237 CTL_HA_STATUS_SUCCESS) { 8238 printf("CTL:Persis Out error returned " 8239 "from ctl_ha_msg_send %d\n", 8240 isc_retval); 8241 } 8242 } else { 8243 /* 8244 * sa_res_key is not the res holder just 8245 * remove registrants 8246 */ 8247 int found=0; 8248 8249 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8250 if (sa_res_key != ctl_get_prkey(lun, i)) 8251 continue; 8252 8253 found = 1; 8254 ctl_clr_prkey(lun, i); 8255 lun->pr_key_count--; 8256 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8257 } 8258 8259 if (!found) { 8260 mtx_unlock(&lun->lun_lock); 8261 free(ctsio->kern_data_ptr, M_CTL); 8262 ctl_set_reservation_conflict(ctsio); 8263 ctl_done((union ctl_io *)ctsio); 8264 return (1); 8265 } 8266 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8267 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8268 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8269 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8270 persis_io.pr.pr_info.res_type = type; 8271 memcpy(persis_io.pr.pr_info.sa_res_key, 8272 param->serv_act_res_key, 8273 sizeof(param->serv_act_res_key)); 8274 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8275 &persis_io, sizeof(persis_io), 0)) > 8276 CTL_HA_STATUS_SUCCESS) { 8277 printf("CTL:Persis Out error returned " 8278 "from ctl_ha_msg_send %d\n", 8279 isc_retval); 8280 } 8281 } 8282 } 8283 8284 lun->PRGeneration++; 8285 mtx_unlock(&lun->lun_lock); 8286 8287 return (retval); 8288} 8289 8290static void 8291ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8292{ 8293 uint64_t sa_res_key; 8294 int i; 8295 8296 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8297 8298 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8299 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8300 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8301 if (sa_res_key == 0) { 8302 /* 8303 * Unregister everybody else and build UA for 8304 * them 8305 */ 8306 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8307 if (i == msg->pr.pr_info.residx || 8308 ctl_get_prkey(lun, i) == 0) 8309 continue; 8310 8311 ctl_clr_prkey(lun, i); 8312 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8313 } 8314 8315 lun->pr_key_count = 1; 8316 lun->res_type = msg->pr.pr_info.res_type; 8317 if (lun->res_type != SPR_TYPE_WR_EX_AR 8318 && lun->res_type != SPR_TYPE_EX_AC_AR) 8319 lun->pr_res_idx = msg->pr.pr_info.residx; 8320 } else { 8321 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8322 if (sa_res_key == ctl_get_prkey(lun, i)) 8323 continue; 8324 8325 ctl_clr_prkey(lun, i); 8326 lun->pr_key_count--; 8327 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8328 } 8329 } 8330 } else { 8331 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8332 if (i == msg->pr.pr_info.residx || 8333 ctl_get_prkey(lun, i) == 0) 8334 continue; 8335 8336 if (sa_res_key == ctl_get_prkey(lun, i)) { 8337 ctl_clr_prkey(lun, i); 8338 lun->pr_key_count--; 8339 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8340 } else if (msg->pr.pr_info.res_type != lun->res_type 8341 && (lun->res_type == SPR_TYPE_WR_EX_RO 8342 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8343 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8344 } 8345 } 8346 lun->res_type = msg->pr.pr_info.res_type; 8347 if (lun->res_type != SPR_TYPE_WR_EX_AR 8348 && lun->res_type != SPR_TYPE_EX_AC_AR) 8349 lun->pr_res_idx = msg->pr.pr_info.residx; 8350 else 8351 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8352 } 8353 lun->PRGeneration++; 8354 8355} 8356 8357 8358int 8359ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8360{ 8361 int retval; 8362 int isc_retval; 8363 u_int32_t param_len; 8364 struct scsi_per_res_out *cdb; 8365 struct ctl_lun *lun; 8366 struct scsi_per_res_out_parms* param; 8367 struct ctl_softc *softc; 8368 uint32_t residx; 8369 uint64_t res_key, sa_res_key, key; 8370 uint8_t type; 8371 union ctl_ha_msg persis_io; 8372 int i; 8373 8374 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8375 8376 retval = CTL_RETVAL_COMPLETE; 8377 8378 softc = control_softc; 8379 8380 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8381 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8382 8383 /* 8384 * We only support whole-LUN scope. The scope & type are ignored for 8385 * register, register and ignore existing key and clear. 8386 * We sometimes ignore scope and type on preempts too!! 8387 * Verify reservation type here as well. 8388 */ 8389 type = cdb->scope_type & SPR_TYPE_MASK; 8390 if ((cdb->action == SPRO_RESERVE) 8391 || (cdb->action == SPRO_RELEASE)) { 8392 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8393 ctl_set_invalid_field(/*ctsio*/ ctsio, 8394 /*sks_valid*/ 1, 8395 /*command*/ 1, 8396 /*field*/ 2, 8397 /*bit_valid*/ 1, 8398 /*bit*/ 4); 8399 ctl_done((union ctl_io *)ctsio); 8400 return (CTL_RETVAL_COMPLETE); 8401 } 8402 8403 if (type>8 || type==2 || type==4 || type==0) { 8404 ctl_set_invalid_field(/*ctsio*/ ctsio, 8405 /*sks_valid*/ 1, 8406 /*command*/ 1, 8407 /*field*/ 2, 8408 /*bit_valid*/ 1, 8409 /*bit*/ 0); 8410 ctl_done((union ctl_io *)ctsio); 8411 return (CTL_RETVAL_COMPLETE); 8412 } 8413 } 8414 8415 param_len = scsi_4btoul(cdb->length); 8416 8417 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8418 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8419 ctsio->kern_data_len = param_len; 8420 ctsio->kern_total_len = param_len; 8421 ctsio->kern_data_resid = 0; 8422 ctsio->kern_rel_offset = 0; 8423 ctsio->kern_sg_entries = 0; 8424 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8425 ctsio->be_move_done = ctl_config_move_done; 8426 ctl_datamove((union ctl_io *)ctsio); 8427 8428 return (CTL_RETVAL_COMPLETE); 8429 } 8430 8431 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8432 8433 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8434 res_key = scsi_8btou64(param->res_key.key); 8435 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8436 8437 /* 8438 * Validate the reservation key here except for SPRO_REG_IGNO 8439 * This must be done for all other service actions 8440 */ 8441 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8442 mtx_lock(&lun->lun_lock); 8443 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8444 if (res_key != key) { 8445 /* 8446 * The current key passed in doesn't match 8447 * the one the initiator previously 8448 * registered. 8449 */ 8450 mtx_unlock(&lun->lun_lock); 8451 free(ctsio->kern_data_ptr, M_CTL); 8452 ctl_set_reservation_conflict(ctsio); 8453 ctl_done((union ctl_io *)ctsio); 8454 return (CTL_RETVAL_COMPLETE); 8455 } 8456 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8457 /* 8458 * We are not registered 8459 */ 8460 mtx_unlock(&lun->lun_lock); 8461 free(ctsio->kern_data_ptr, M_CTL); 8462 ctl_set_reservation_conflict(ctsio); 8463 ctl_done((union ctl_io *)ctsio); 8464 return (CTL_RETVAL_COMPLETE); 8465 } else if (res_key != 0) { 8466 /* 8467 * We are not registered and trying to register but 8468 * the register key isn't zero. 8469 */ 8470 mtx_unlock(&lun->lun_lock); 8471 free(ctsio->kern_data_ptr, M_CTL); 8472 ctl_set_reservation_conflict(ctsio); 8473 ctl_done((union ctl_io *)ctsio); 8474 return (CTL_RETVAL_COMPLETE); 8475 } 8476 mtx_unlock(&lun->lun_lock); 8477 } 8478 8479 switch (cdb->action & SPRO_ACTION_MASK) { 8480 case SPRO_REGISTER: 8481 case SPRO_REG_IGNO: { 8482 8483#if 0 8484 printf("Registration received\n"); 8485#endif 8486 8487 /* 8488 * We don't support any of these options, as we report in 8489 * the read capabilities request (see 8490 * ctl_persistent_reserve_in(), above). 8491 */ 8492 if ((param->flags & SPR_SPEC_I_PT) 8493 || (param->flags & SPR_ALL_TG_PT) 8494 || (param->flags & SPR_APTPL)) { 8495 int bit_ptr; 8496 8497 if (param->flags & SPR_APTPL) 8498 bit_ptr = 0; 8499 else if (param->flags & SPR_ALL_TG_PT) 8500 bit_ptr = 2; 8501 else /* SPR_SPEC_I_PT */ 8502 bit_ptr = 3; 8503 8504 free(ctsio->kern_data_ptr, M_CTL); 8505 ctl_set_invalid_field(ctsio, 8506 /*sks_valid*/ 1, 8507 /*command*/ 0, 8508 /*field*/ 20, 8509 /*bit_valid*/ 1, 8510 /*bit*/ bit_ptr); 8511 ctl_done((union ctl_io *)ctsio); 8512 return (CTL_RETVAL_COMPLETE); 8513 } 8514 8515 mtx_lock(&lun->lun_lock); 8516 8517 /* 8518 * The initiator wants to clear the 8519 * key/unregister. 8520 */ 8521 if (sa_res_key == 0) { 8522 if ((res_key == 0 8523 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8524 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8525 && ctl_get_prkey(lun, residx) == 0)) { 8526 mtx_unlock(&lun->lun_lock); 8527 goto done; 8528 } 8529 8530 ctl_clr_prkey(lun, residx); 8531 lun->pr_key_count--; 8532 8533 if (residx == lun->pr_res_idx) { 8534 lun->flags &= ~CTL_LUN_PR_RESERVED; 8535 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8536 8537 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8538 || lun->res_type == SPR_TYPE_EX_AC_RO) 8539 && lun->pr_key_count) { 8540 /* 8541 * If the reservation is a registrants 8542 * only type we need to generate a UA 8543 * for other registered inits. The 8544 * sense code should be RESERVATIONS 8545 * RELEASED 8546 */ 8547 8548 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8549 if (ctl_get_prkey(lun, i + 8550 softc->persis_offset) == 0) 8551 continue; 8552 ctl_est_ua(lun, i, 8553 CTL_UA_RES_RELEASE); 8554 } 8555 } 8556 lun->res_type = 0; 8557 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8558 if (lun->pr_key_count==0) { 8559 lun->flags &= ~CTL_LUN_PR_RESERVED; 8560 lun->res_type = 0; 8561 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8562 } 8563 } 8564 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8565 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8566 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8567 persis_io.pr.pr_info.residx = residx; 8568 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8569 &persis_io, sizeof(persis_io), 0 )) > 8570 CTL_HA_STATUS_SUCCESS) { 8571 printf("CTL:Persis Out error returned from " 8572 "ctl_ha_msg_send %d\n", isc_retval); 8573 } 8574 } else /* sa_res_key != 0 */ { 8575 8576 /* 8577 * If we aren't registered currently then increment 8578 * the key count and set the registered flag. 8579 */ 8580 ctl_alloc_prkey(lun, residx); 8581 if (ctl_get_prkey(lun, residx) == 0) 8582 lun->pr_key_count++; 8583 ctl_set_prkey(lun, residx, sa_res_key); 8584 8585 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8586 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8587 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8588 persis_io.pr.pr_info.residx = residx; 8589 memcpy(persis_io.pr.pr_info.sa_res_key, 8590 param->serv_act_res_key, 8591 sizeof(param->serv_act_res_key)); 8592 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8593 &persis_io, sizeof(persis_io), 0)) > 8594 CTL_HA_STATUS_SUCCESS) { 8595 printf("CTL:Persis Out error returned from " 8596 "ctl_ha_msg_send %d\n", isc_retval); 8597 } 8598 } 8599 lun->PRGeneration++; 8600 mtx_unlock(&lun->lun_lock); 8601 8602 break; 8603 } 8604 case SPRO_RESERVE: 8605#if 0 8606 printf("Reserve executed type %d\n", type); 8607#endif 8608 mtx_lock(&lun->lun_lock); 8609 if (lun->flags & CTL_LUN_PR_RESERVED) { 8610 /* 8611 * if this isn't the reservation holder and it's 8612 * not a "all registrants" type or if the type is 8613 * different then we have a conflict 8614 */ 8615 if ((lun->pr_res_idx != residx 8616 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8617 || lun->res_type != type) { 8618 mtx_unlock(&lun->lun_lock); 8619 free(ctsio->kern_data_ptr, M_CTL); 8620 ctl_set_reservation_conflict(ctsio); 8621 ctl_done((union ctl_io *)ctsio); 8622 return (CTL_RETVAL_COMPLETE); 8623 } 8624 mtx_unlock(&lun->lun_lock); 8625 } else /* create a reservation */ { 8626 /* 8627 * If it's not an "all registrants" type record 8628 * reservation holder 8629 */ 8630 if (type != SPR_TYPE_WR_EX_AR 8631 && type != SPR_TYPE_EX_AC_AR) 8632 lun->pr_res_idx = residx; /* Res holder */ 8633 else 8634 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8635 8636 lun->flags |= CTL_LUN_PR_RESERVED; 8637 lun->res_type = type; 8638 8639 mtx_unlock(&lun->lun_lock); 8640 8641 /* send msg to other side */ 8642 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8643 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8644 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8645 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8646 persis_io.pr.pr_info.res_type = type; 8647 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8648 &persis_io, sizeof(persis_io), 0)) > 8649 CTL_HA_STATUS_SUCCESS) { 8650 printf("CTL:Persis Out error returned from " 8651 "ctl_ha_msg_send %d\n", isc_retval); 8652 } 8653 } 8654 break; 8655 8656 case SPRO_RELEASE: 8657 mtx_lock(&lun->lun_lock); 8658 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8659 /* No reservation exists return good status */ 8660 mtx_unlock(&lun->lun_lock); 8661 goto done; 8662 } 8663 /* 8664 * Is this nexus a reservation holder? 8665 */ 8666 if (lun->pr_res_idx != residx 8667 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8668 /* 8669 * not a res holder return good status but 8670 * do nothing 8671 */ 8672 mtx_unlock(&lun->lun_lock); 8673 goto done; 8674 } 8675 8676 if (lun->res_type != type) { 8677 mtx_unlock(&lun->lun_lock); 8678 free(ctsio->kern_data_ptr, M_CTL); 8679 ctl_set_illegal_pr_release(ctsio); 8680 ctl_done((union ctl_io *)ctsio); 8681 return (CTL_RETVAL_COMPLETE); 8682 } 8683 8684 /* okay to release */ 8685 lun->flags &= ~CTL_LUN_PR_RESERVED; 8686 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8687 lun->res_type = 0; 8688 8689 /* 8690 * if this isn't an exclusive access 8691 * res generate UA for all other 8692 * registrants. 8693 */ 8694 if (type != SPR_TYPE_EX_AC 8695 && type != SPR_TYPE_WR_EX) { 8696 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8697 if (i == residx || 8698 ctl_get_prkey(lun, 8699 i + softc->persis_offset) == 0) 8700 continue; 8701 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8702 } 8703 } 8704 mtx_unlock(&lun->lun_lock); 8705 /* Send msg to other side */ 8706 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8707 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8708 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8709 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8710 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8711 printf("CTL:Persis Out error returned from " 8712 "ctl_ha_msg_send %d\n", isc_retval); 8713 } 8714 break; 8715 8716 case SPRO_CLEAR: 8717 /* send msg to other side */ 8718 8719 mtx_lock(&lun->lun_lock); 8720 lun->flags &= ~CTL_LUN_PR_RESERVED; 8721 lun->res_type = 0; 8722 lun->pr_key_count = 0; 8723 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8724 8725 ctl_clr_prkey(lun, residx); 8726 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8727 if (ctl_get_prkey(lun, i) != 0) { 8728 ctl_clr_prkey(lun, i); 8729 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8730 } 8731 lun->PRGeneration++; 8732 mtx_unlock(&lun->lun_lock); 8733 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8734 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8735 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8736 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8737 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8738 printf("CTL:Persis Out error returned from " 8739 "ctl_ha_msg_send %d\n", isc_retval); 8740 } 8741 break; 8742 8743 case SPRO_PREEMPT: 8744 case SPRO_PRE_ABO: { 8745 int nretval; 8746 8747 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8748 residx, ctsio, cdb, param); 8749 if (nretval != 0) 8750 return (CTL_RETVAL_COMPLETE); 8751 break; 8752 } 8753 default: 8754 panic("Invalid PR type %x", cdb->action); 8755 } 8756 8757done: 8758 free(ctsio->kern_data_ptr, M_CTL); 8759 ctl_set_success(ctsio); 8760 ctl_done((union ctl_io *)ctsio); 8761 8762 return (retval); 8763} 8764 8765/* 8766 * This routine is for handling a message from the other SC pertaining to 8767 * persistent reserve out. All the error checking will have been done 8768 * so only perorming the action need be done here to keep the two 8769 * in sync. 8770 */ 8771static void 8772ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8773{ 8774 struct ctl_lun *lun; 8775 struct ctl_softc *softc; 8776 int i; 8777 uint32_t targ_lun; 8778 8779 softc = control_softc; 8780 8781 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8782 lun = softc->ctl_luns[targ_lun]; 8783 mtx_lock(&lun->lun_lock); 8784 switch(msg->pr.pr_info.action) { 8785 case CTL_PR_REG_KEY: 8786 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8787 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8788 lun->pr_key_count++; 8789 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8790 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8791 lun->PRGeneration++; 8792 break; 8793 8794 case CTL_PR_UNREG_KEY: 8795 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8796 lun->pr_key_count--; 8797 8798 /* XXX Need to see if the reservation has been released */ 8799 /* if so do we need to generate UA? */ 8800 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8801 lun->flags &= ~CTL_LUN_PR_RESERVED; 8802 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8803 8804 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8805 || lun->res_type == SPR_TYPE_EX_AC_RO) 8806 && lun->pr_key_count) { 8807 /* 8808 * If the reservation is a registrants 8809 * only type we need to generate a UA 8810 * for other registered inits. The 8811 * sense code should be RESERVATIONS 8812 * RELEASED 8813 */ 8814 8815 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8816 if (ctl_get_prkey(lun, i + 8817 softc->persis_offset) == 0) 8818 continue; 8819 8820 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8821 } 8822 } 8823 lun->res_type = 0; 8824 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8825 if (lun->pr_key_count==0) { 8826 lun->flags &= ~CTL_LUN_PR_RESERVED; 8827 lun->res_type = 0; 8828 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8829 } 8830 } 8831 lun->PRGeneration++; 8832 break; 8833 8834 case CTL_PR_RESERVE: 8835 lun->flags |= CTL_LUN_PR_RESERVED; 8836 lun->res_type = msg->pr.pr_info.res_type; 8837 lun->pr_res_idx = msg->pr.pr_info.residx; 8838 8839 break; 8840 8841 case CTL_PR_RELEASE: 8842 /* 8843 * if this isn't an exclusive access res generate UA for all 8844 * other registrants. 8845 */ 8846 if (lun->res_type != SPR_TYPE_EX_AC 8847 && lun->res_type != SPR_TYPE_WR_EX) { 8848 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8849 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8850 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8851 } 8852 8853 lun->flags &= ~CTL_LUN_PR_RESERVED; 8854 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8855 lun->res_type = 0; 8856 break; 8857 8858 case CTL_PR_PREEMPT: 8859 ctl_pro_preempt_other(lun, msg); 8860 break; 8861 case CTL_PR_CLEAR: 8862 lun->flags &= ~CTL_LUN_PR_RESERVED; 8863 lun->res_type = 0; 8864 lun->pr_key_count = 0; 8865 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8866 8867 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8868 if (ctl_get_prkey(lun, i) == 0) 8869 continue; 8870 ctl_clr_prkey(lun, i); 8871 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8872 } 8873 lun->PRGeneration++; 8874 break; 8875 } 8876 8877 mtx_unlock(&lun->lun_lock); 8878} 8879 8880int 8881ctl_read_write(struct ctl_scsiio *ctsio) 8882{ 8883 struct ctl_lun *lun; 8884 struct ctl_lba_len_flags *lbalen; 8885 uint64_t lba; 8886 uint32_t num_blocks; 8887 int flags, retval; 8888 int isread; 8889 8890 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8891 8892 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8893 8894 flags = 0; 8895 retval = CTL_RETVAL_COMPLETE; 8896 8897 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8898 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8899 switch (ctsio->cdb[0]) { 8900 case READ_6: 8901 case WRITE_6: { 8902 struct scsi_rw_6 *cdb; 8903 8904 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8905 8906 lba = scsi_3btoul(cdb->addr); 8907 /* only 5 bits are valid in the most significant address byte */ 8908 lba &= 0x1fffff; 8909 num_blocks = cdb->length; 8910 /* 8911 * This is correct according to SBC-2. 8912 */ 8913 if (num_blocks == 0) 8914 num_blocks = 256; 8915 break; 8916 } 8917 case READ_10: 8918 case WRITE_10: { 8919 struct scsi_rw_10 *cdb; 8920 8921 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8922 if (cdb->byte2 & SRW10_FUA) 8923 flags |= CTL_LLF_FUA; 8924 if (cdb->byte2 & SRW10_DPO) 8925 flags |= CTL_LLF_DPO; 8926 lba = scsi_4btoul(cdb->addr); 8927 num_blocks = scsi_2btoul(cdb->length); 8928 break; 8929 } 8930 case WRITE_VERIFY_10: { 8931 struct scsi_write_verify_10 *cdb; 8932 8933 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8934 flags |= CTL_LLF_FUA; 8935 if (cdb->byte2 & SWV_DPO) 8936 flags |= CTL_LLF_DPO; 8937 lba = scsi_4btoul(cdb->addr); 8938 num_blocks = scsi_2btoul(cdb->length); 8939 break; 8940 } 8941 case READ_12: 8942 case WRITE_12: { 8943 struct scsi_rw_12 *cdb; 8944 8945 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8946 if (cdb->byte2 & SRW12_FUA) 8947 flags |= CTL_LLF_FUA; 8948 if (cdb->byte2 & SRW12_DPO) 8949 flags |= CTL_LLF_DPO; 8950 lba = scsi_4btoul(cdb->addr); 8951 num_blocks = scsi_4btoul(cdb->length); 8952 break; 8953 } 8954 case WRITE_VERIFY_12: { 8955 struct scsi_write_verify_12 *cdb; 8956 8957 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8958 flags |= CTL_LLF_FUA; 8959 if (cdb->byte2 & SWV_DPO) 8960 flags |= CTL_LLF_DPO; 8961 lba = scsi_4btoul(cdb->addr); 8962 num_blocks = scsi_4btoul(cdb->length); 8963 break; 8964 } 8965 case READ_16: 8966 case WRITE_16: { 8967 struct scsi_rw_16 *cdb; 8968 8969 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8970 if (cdb->byte2 & SRW12_FUA) 8971 flags |= CTL_LLF_FUA; 8972 if (cdb->byte2 & SRW12_DPO) 8973 flags |= CTL_LLF_DPO; 8974 lba = scsi_8btou64(cdb->addr); 8975 num_blocks = scsi_4btoul(cdb->length); 8976 break; 8977 } 8978 case WRITE_ATOMIC_16: { 8979 struct scsi_rw_16 *cdb; 8980 8981 if (lun->be_lun->atomicblock == 0) { 8982 ctl_set_invalid_opcode(ctsio); 8983 ctl_done((union ctl_io *)ctsio); 8984 return (CTL_RETVAL_COMPLETE); 8985 } 8986 8987 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8988 if (cdb->byte2 & SRW12_FUA) 8989 flags |= CTL_LLF_FUA; 8990 if (cdb->byte2 & SRW12_DPO) 8991 flags |= CTL_LLF_DPO; 8992 lba = scsi_8btou64(cdb->addr); 8993 num_blocks = scsi_4btoul(cdb->length); 8994 if (num_blocks > lun->be_lun->atomicblock) { 8995 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8996 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8997 /*bit*/ 0); 8998 ctl_done((union ctl_io *)ctsio); 8999 return (CTL_RETVAL_COMPLETE); 9000 } 9001 break; 9002 } 9003 case WRITE_VERIFY_16: { 9004 struct scsi_write_verify_16 *cdb; 9005 9006 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9007 flags |= CTL_LLF_FUA; 9008 if (cdb->byte2 & SWV_DPO) 9009 flags |= CTL_LLF_DPO; 9010 lba = scsi_8btou64(cdb->addr); 9011 num_blocks = scsi_4btoul(cdb->length); 9012 break; 9013 } 9014 default: 9015 /* 9016 * We got a command we don't support. This shouldn't 9017 * happen, commands should be filtered out above us. 9018 */ 9019 ctl_set_invalid_opcode(ctsio); 9020 ctl_done((union ctl_io *)ctsio); 9021 9022 return (CTL_RETVAL_COMPLETE); 9023 break; /* NOTREACHED */ 9024 } 9025 9026 /* 9027 * The first check is to make sure we're in bounds, the second 9028 * check is to catch wrap-around problems. If the lba + num blocks 9029 * is less than the lba, then we've wrapped around and the block 9030 * range is invalid anyway. 9031 */ 9032 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9033 || ((lba + num_blocks) < lba)) { 9034 ctl_set_lba_out_of_range(ctsio); 9035 ctl_done((union ctl_io *)ctsio); 9036 return (CTL_RETVAL_COMPLETE); 9037 } 9038 9039 /* 9040 * According to SBC-3, a transfer length of 0 is not an error. 9041 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9042 * translates to 256 blocks for those commands. 9043 */ 9044 if (num_blocks == 0) { 9045 ctl_set_success(ctsio); 9046 ctl_done((union ctl_io *)ctsio); 9047 return (CTL_RETVAL_COMPLETE); 9048 } 9049 9050 /* Set FUA and/or DPO if caches are disabled. */ 9051 if (isread) { 9052 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9053 SCP_RCD) != 0) 9054 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9055 } else { 9056 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9057 SCP_WCE) == 0) 9058 flags |= CTL_LLF_FUA; 9059 } 9060 9061 lbalen = (struct ctl_lba_len_flags *) 9062 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9063 lbalen->lba = lba; 9064 lbalen->len = num_blocks; 9065 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9066 9067 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9068 ctsio->kern_rel_offset = 0; 9069 9070 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9071 9072 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9073 9074 return (retval); 9075} 9076 9077static int 9078ctl_cnw_cont(union ctl_io *io) 9079{ 9080 struct ctl_scsiio *ctsio; 9081 struct ctl_lun *lun; 9082 struct ctl_lba_len_flags *lbalen; 9083 int retval; 9084 9085 ctsio = &io->scsiio; 9086 ctsio->io_hdr.status = CTL_STATUS_NONE; 9087 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9088 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9089 lbalen = (struct ctl_lba_len_flags *) 9090 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9091 lbalen->flags &= ~CTL_LLF_COMPARE; 9092 lbalen->flags |= CTL_LLF_WRITE; 9093 9094 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9095 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9096 return (retval); 9097} 9098 9099int 9100ctl_cnw(struct ctl_scsiio *ctsio) 9101{ 9102 struct ctl_lun *lun; 9103 struct ctl_lba_len_flags *lbalen; 9104 uint64_t lba; 9105 uint32_t num_blocks; 9106 int flags, retval; 9107 9108 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9109 9110 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9111 9112 flags = 0; 9113 retval = CTL_RETVAL_COMPLETE; 9114 9115 switch (ctsio->cdb[0]) { 9116 case COMPARE_AND_WRITE: { 9117 struct scsi_compare_and_write *cdb; 9118 9119 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9120 if (cdb->byte2 & SRW10_FUA) 9121 flags |= CTL_LLF_FUA; 9122 if (cdb->byte2 & SRW10_DPO) 9123 flags |= CTL_LLF_DPO; 9124 lba = scsi_8btou64(cdb->addr); 9125 num_blocks = cdb->length; 9126 break; 9127 } 9128 default: 9129 /* 9130 * We got a command we don't support. This shouldn't 9131 * happen, commands should be filtered out above us. 9132 */ 9133 ctl_set_invalid_opcode(ctsio); 9134 ctl_done((union ctl_io *)ctsio); 9135 9136 return (CTL_RETVAL_COMPLETE); 9137 break; /* NOTREACHED */ 9138 } 9139 9140 /* 9141 * The first check is to make sure we're in bounds, the second 9142 * check is to catch wrap-around problems. If the lba + num blocks 9143 * is less than the lba, then we've wrapped around and the block 9144 * range is invalid anyway. 9145 */ 9146 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9147 || ((lba + num_blocks) < lba)) { 9148 ctl_set_lba_out_of_range(ctsio); 9149 ctl_done((union ctl_io *)ctsio); 9150 return (CTL_RETVAL_COMPLETE); 9151 } 9152 9153 /* 9154 * According to SBC-3, a transfer length of 0 is not an error. 9155 */ 9156 if (num_blocks == 0) { 9157 ctl_set_success(ctsio); 9158 ctl_done((union ctl_io *)ctsio); 9159 return (CTL_RETVAL_COMPLETE); 9160 } 9161 9162 /* Set FUA if write cache is disabled. */ 9163 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9164 SCP_WCE) == 0) 9165 flags |= CTL_LLF_FUA; 9166 9167 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9168 ctsio->kern_rel_offset = 0; 9169 9170 /* 9171 * Set the IO_CONT flag, so that if this I/O gets passed to 9172 * ctl_data_submit_done(), it'll get passed back to 9173 * ctl_ctl_cnw_cont() for further processing. 9174 */ 9175 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9176 ctsio->io_cont = ctl_cnw_cont; 9177 9178 lbalen = (struct ctl_lba_len_flags *) 9179 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9180 lbalen->lba = lba; 9181 lbalen->len = num_blocks; 9182 lbalen->flags = CTL_LLF_COMPARE | flags; 9183 9184 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9185 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9186 return (retval); 9187} 9188 9189int 9190ctl_verify(struct ctl_scsiio *ctsio) 9191{ 9192 struct ctl_lun *lun; 9193 struct ctl_lba_len_flags *lbalen; 9194 uint64_t lba; 9195 uint32_t num_blocks; 9196 int bytchk, flags; 9197 int retval; 9198 9199 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9200 9201 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9202 9203 bytchk = 0; 9204 flags = CTL_LLF_FUA; 9205 retval = CTL_RETVAL_COMPLETE; 9206 9207 switch (ctsio->cdb[0]) { 9208 case VERIFY_10: { 9209 struct scsi_verify_10 *cdb; 9210 9211 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9212 if (cdb->byte2 & SVFY_BYTCHK) 9213 bytchk = 1; 9214 if (cdb->byte2 & SVFY_DPO) 9215 flags |= CTL_LLF_DPO; 9216 lba = scsi_4btoul(cdb->addr); 9217 num_blocks = scsi_2btoul(cdb->length); 9218 break; 9219 } 9220 case VERIFY_12: { 9221 struct scsi_verify_12 *cdb; 9222 9223 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9224 if (cdb->byte2 & SVFY_BYTCHK) 9225 bytchk = 1; 9226 if (cdb->byte2 & SVFY_DPO) 9227 flags |= CTL_LLF_DPO; 9228 lba = scsi_4btoul(cdb->addr); 9229 num_blocks = scsi_4btoul(cdb->length); 9230 break; 9231 } 9232 case VERIFY_16: { 9233 struct scsi_rw_16 *cdb; 9234 9235 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9236 if (cdb->byte2 & SVFY_BYTCHK) 9237 bytchk = 1; 9238 if (cdb->byte2 & SVFY_DPO) 9239 flags |= CTL_LLF_DPO; 9240 lba = scsi_8btou64(cdb->addr); 9241 num_blocks = scsi_4btoul(cdb->length); 9242 break; 9243 } 9244 default: 9245 /* 9246 * We got a command we don't support. This shouldn't 9247 * happen, commands should be filtered out above us. 9248 */ 9249 ctl_set_invalid_opcode(ctsio); 9250 ctl_done((union ctl_io *)ctsio); 9251 return (CTL_RETVAL_COMPLETE); 9252 } 9253 9254 /* 9255 * The first check is to make sure we're in bounds, the second 9256 * check is to catch wrap-around problems. If the lba + num blocks 9257 * is less than the lba, then we've wrapped around and the block 9258 * range is invalid anyway. 9259 */ 9260 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9261 || ((lba + num_blocks) < lba)) { 9262 ctl_set_lba_out_of_range(ctsio); 9263 ctl_done((union ctl_io *)ctsio); 9264 return (CTL_RETVAL_COMPLETE); 9265 } 9266 9267 /* 9268 * According to SBC-3, a transfer length of 0 is not an error. 9269 */ 9270 if (num_blocks == 0) { 9271 ctl_set_success(ctsio); 9272 ctl_done((union ctl_io *)ctsio); 9273 return (CTL_RETVAL_COMPLETE); 9274 } 9275 9276 lbalen = (struct ctl_lba_len_flags *) 9277 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9278 lbalen->lba = lba; 9279 lbalen->len = num_blocks; 9280 if (bytchk) { 9281 lbalen->flags = CTL_LLF_COMPARE | flags; 9282 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9283 } else { 9284 lbalen->flags = CTL_LLF_VERIFY | flags; 9285 ctsio->kern_total_len = 0; 9286 } 9287 ctsio->kern_rel_offset = 0; 9288 9289 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9290 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9291 return (retval); 9292} 9293 9294int 9295ctl_report_luns(struct ctl_scsiio *ctsio) 9296{ 9297 struct scsi_report_luns *cdb; 9298 struct scsi_report_luns_data *lun_data; 9299 struct ctl_lun *lun, *request_lun; 9300 int num_luns, retval; 9301 uint32_t alloc_len, lun_datalen; 9302 int num_filled, well_known; 9303 uint32_t initidx, targ_lun_id, lun_id; 9304 9305 retval = CTL_RETVAL_COMPLETE; 9306 well_known = 0; 9307 9308 cdb = (struct scsi_report_luns *)ctsio->cdb; 9309 9310 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9311 9312 mtx_lock(&control_softc->ctl_lock); 9313 num_luns = control_softc->num_luns; 9314 mtx_unlock(&control_softc->ctl_lock); 9315 9316 switch (cdb->select_report) { 9317 case RPL_REPORT_DEFAULT: 9318 case RPL_REPORT_ALL: 9319 break; 9320 case RPL_REPORT_WELLKNOWN: 9321 well_known = 1; 9322 num_luns = 0; 9323 break; 9324 default: 9325 ctl_set_invalid_field(ctsio, 9326 /*sks_valid*/ 1, 9327 /*command*/ 1, 9328 /*field*/ 2, 9329 /*bit_valid*/ 0, 9330 /*bit*/ 0); 9331 ctl_done((union ctl_io *)ctsio); 9332 return (retval); 9333 break; /* NOTREACHED */ 9334 } 9335 9336 alloc_len = scsi_4btoul(cdb->length); 9337 /* 9338 * The initiator has to allocate at least 16 bytes for this request, 9339 * so he can at least get the header and the first LUN. Otherwise 9340 * we reject the request (per SPC-3 rev 14, section 6.21). 9341 */ 9342 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9343 sizeof(struct scsi_report_luns_lundata))) { 9344 ctl_set_invalid_field(ctsio, 9345 /*sks_valid*/ 1, 9346 /*command*/ 1, 9347 /*field*/ 6, 9348 /*bit_valid*/ 0, 9349 /*bit*/ 0); 9350 ctl_done((union ctl_io *)ctsio); 9351 return (retval); 9352 } 9353 9354 request_lun = (struct ctl_lun *) 9355 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9356 9357 lun_datalen = sizeof(*lun_data) + 9358 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9359 9360 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9361 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9362 ctsio->kern_sg_entries = 0; 9363 9364 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9365 9366 mtx_lock(&control_softc->ctl_lock); 9367 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9368 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9369 if (lun_id >= CTL_MAX_LUNS) 9370 continue; 9371 lun = control_softc->ctl_luns[lun_id]; 9372 if (lun == NULL) 9373 continue; 9374 9375 if (targ_lun_id <= 0xff) { 9376 /* 9377 * Peripheral addressing method, bus number 0. 9378 */ 9379 lun_data->luns[num_filled].lundata[0] = 9380 RPL_LUNDATA_ATYP_PERIPH; 9381 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9382 num_filled++; 9383 } else if (targ_lun_id <= 0x3fff) { 9384 /* 9385 * Flat addressing method. 9386 */ 9387 lun_data->luns[num_filled].lundata[0] = 9388 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9389 lun_data->luns[num_filled].lundata[1] = 9390 (targ_lun_id & 0xff); 9391 num_filled++; 9392 } else if (targ_lun_id <= 0xffffff) { 9393 /* 9394 * Extended flat addressing method. 9395 */ 9396 lun_data->luns[num_filled].lundata[0] = 9397 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9398 scsi_ulto3b(targ_lun_id, 9399 &lun_data->luns[num_filled].lundata[1]); 9400 num_filled++; 9401 } else { 9402 printf("ctl_report_luns: bogus LUN number %jd, " 9403 "skipping\n", (intmax_t)targ_lun_id); 9404 } 9405 /* 9406 * According to SPC-3, rev 14 section 6.21: 9407 * 9408 * "The execution of a REPORT LUNS command to any valid and 9409 * installed logical unit shall clear the REPORTED LUNS DATA 9410 * HAS CHANGED unit attention condition for all logical 9411 * units of that target with respect to the requesting 9412 * initiator. A valid and installed logical unit is one 9413 * having a PERIPHERAL QUALIFIER of 000b in the standard 9414 * INQUIRY data (see 6.4.2)." 9415 * 9416 * If request_lun is NULL, the LUN this report luns command 9417 * was issued to is either disabled or doesn't exist. In that 9418 * case, we shouldn't clear any pending lun change unit 9419 * attention. 9420 */ 9421 if (request_lun != NULL) { 9422 mtx_lock(&lun->lun_lock); 9423 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE); 9424 mtx_unlock(&lun->lun_lock); 9425 } 9426 } 9427 mtx_unlock(&control_softc->ctl_lock); 9428 9429 /* 9430 * It's quite possible that we've returned fewer LUNs than we allocated 9431 * space for. Trim it. 9432 */ 9433 lun_datalen = sizeof(*lun_data) + 9434 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9435 9436 if (lun_datalen < alloc_len) { 9437 ctsio->residual = alloc_len - lun_datalen; 9438 ctsio->kern_data_len = lun_datalen; 9439 ctsio->kern_total_len = lun_datalen; 9440 } else { 9441 ctsio->residual = 0; 9442 ctsio->kern_data_len = alloc_len; 9443 ctsio->kern_total_len = alloc_len; 9444 } 9445 ctsio->kern_data_resid = 0; 9446 ctsio->kern_rel_offset = 0; 9447 ctsio->kern_sg_entries = 0; 9448 9449 /* 9450 * We set this to the actual data length, regardless of how much 9451 * space we actually have to return results. If the user looks at 9452 * this value, he'll know whether or not he allocated enough space 9453 * and reissue the command if necessary. We don't support well 9454 * known logical units, so if the user asks for that, return none. 9455 */ 9456 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9457 9458 /* 9459 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9460 * this request. 9461 */ 9462 ctl_set_success(ctsio); 9463 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9464 ctsio->be_move_done = ctl_config_move_done; 9465 ctl_datamove((union ctl_io *)ctsio); 9466 return (retval); 9467} 9468 9469int 9470ctl_request_sense(struct ctl_scsiio *ctsio) 9471{ 9472 struct scsi_request_sense *cdb; 9473 struct scsi_sense_data *sense_ptr; 9474 struct ctl_lun *lun; 9475 uint32_t initidx; 9476 int have_error; 9477 scsi_sense_data_type sense_format; 9478 ctl_ua_type ua_type; 9479 9480 cdb = (struct scsi_request_sense *)ctsio->cdb; 9481 9482 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9483 9484 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9485 9486 /* 9487 * Determine which sense format the user wants. 9488 */ 9489 if (cdb->byte2 & SRS_DESC) 9490 sense_format = SSD_TYPE_DESC; 9491 else 9492 sense_format = SSD_TYPE_FIXED; 9493 9494 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9495 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9496 ctsio->kern_sg_entries = 0; 9497 9498 /* 9499 * struct scsi_sense_data, which is currently set to 256 bytes, is 9500 * larger than the largest allowed value for the length field in the 9501 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9502 */ 9503 ctsio->residual = 0; 9504 ctsio->kern_data_len = cdb->length; 9505 ctsio->kern_total_len = cdb->length; 9506 9507 ctsio->kern_data_resid = 0; 9508 ctsio->kern_rel_offset = 0; 9509 ctsio->kern_sg_entries = 0; 9510 9511 /* 9512 * If we don't have a LUN, we don't have any pending sense. 9513 */ 9514 if (lun == NULL) 9515 goto no_sense; 9516 9517 have_error = 0; 9518 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9519 /* 9520 * Check for pending sense, and then for pending unit attentions. 9521 * Pending sense gets returned first, then pending unit attentions. 9522 */ 9523 mtx_lock(&lun->lun_lock); 9524#ifdef CTL_WITH_CA 9525 if (ctl_is_set(lun->have_ca, initidx)) { 9526 scsi_sense_data_type stored_format; 9527 9528 /* 9529 * Check to see which sense format was used for the stored 9530 * sense data. 9531 */ 9532 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9533 9534 /* 9535 * If the user requested a different sense format than the 9536 * one we stored, then we need to convert it to the other 9537 * format. If we're going from descriptor to fixed format 9538 * sense data, we may lose things in translation, depending 9539 * on what options were used. 9540 * 9541 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9542 * for some reason we'll just copy it out as-is. 9543 */ 9544 if ((stored_format == SSD_TYPE_FIXED) 9545 && (sense_format == SSD_TYPE_DESC)) 9546 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9547 &lun->pending_sense[initidx], 9548 (struct scsi_sense_data_desc *)sense_ptr); 9549 else if ((stored_format == SSD_TYPE_DESC) 9550 && (sense_format == SSD_TYPE_FIXED)) 9551 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9552 &lun->pending_sense[initidx], 9553 (struct scsi_sense_data_fixed *)sense_ptr); 9554 else 9555 memcpy(sense_ptr, &lun->pending_sense[initidx], 9556 ctl_min(sizeof(*sense_ptr), 9557 sizeof(lun->pending_sense[initidx]))); 9558 9559 ctl_clear_mask(lun->have_ca, initidx); 9560 have_error = 1; 9561 } else 9562#endif 9563 { 9564 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9565 if (ua_type != CTL_UA_NONE) 9566 have_error = 1; 9567 } 9568 mtx_unlock(&lun->lun_lock); 9569 9570 /* 9571 * We already have a pending error, return it. 9572 */ 9573 if (have_error != 0) { 9574 /* 9575 * We report the SCSI status as OK, since the status of the 9576 * request sense command itself is OK. 9577 * We report 0 for the sense length, because we aren't doing 9578 * autosense in this case. We're reporting sense as 9579 * parameter data. 9580 */ 9581 ctl_set_success(ctsio); 9582 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9583 ctsio->be_move_done = ctl_config_move_done; 9584 ctl_datamove((union ctl_io *)ctsio); 9585 return (CTL_RETVAL_COMPLETE); 9586 } 9587 9588no_sense: 9589 9590 /* 9591 * No sense information to report, so we report that everything is 9592 * okay. 9593 */ 9594 ctl_set_sense_data(sense_ptr, 9595 lun, 9596 sense_format, 9597 /*current_error*/ 1, 9598 /*sense_key*/ SSD_KEY_NO_SENSE, 9599 /*asc*/ 0x00, 9600 /*ascq*/ 0x00, 9601 SSD_ELEM_NONE); 9602 9603 /* 9604 * We report 0 for the sense length, because we aren't doing 9605 * autosense in this case. We're reporting sense as parameter data. 9606 */ 9607 ctl_set_success(ctsio); 9608 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9609 ctsio->be_move_done = ctl_config_move_done; 9610 ctl_datamove((union ctl_io *)ctsio); 9611 return (CTL_RETVAL_COMPLETE); 9612} 9613 9614int 9615ctl_tur(struct ctl_scsiio *ctsio) 9616{ 9617 9618 CTL_DEBUG_PRINT(("ctl_tur\n")); 9619 9620 ctl_set_success(ctsio); 9621 ctl_done((union ctl_io *)ctsio); 9622 9623 return (CTL_RETVAL_COMPLETE); 9624} 9625 9626#ifdef notyet 9627static int 9628ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9629{ 9630 9631} 9632#endif 9633 9634static int 9635ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9636{ 9637 struct scsi_vpd_supported_pages *pages; 9638 int sup_page_size; 9639 struct ctl_lun *lun; 9640 9641 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9642 9643 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9644 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9645 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9646 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9647 ctsio->kern_sg_entries = 0; 9648 9649 if (sup_page_size < alloc_len) { 9650 ctsio->residual = alloc_len - sup_page_size; 9651 ctsio->kern_data_len = sup_page_size; 9652 ctsio->kern_total_len = sup_page_size; 9653 } else { 9654 ctsio->residual = 0; 9655 ctsio->kern_data_len = alloc_len; 9656 ctsio->kern_total_len = alloc_len; 9657 } 9658 ctsio->kern_data_resid = 0; 9659 ctsio->kern_rel_offset = 0; 9660 ctsio->kern_sg_entries = 0; 9661 9662 /* 9663 * The control device is always connected. The disk device, on the 9664 * other hand, may not be online all the time. Need to change this 9665 * to figure out whether the disk device is actually online or not. 9666 */ 9667 if (lun != NULL) 9668 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9669 lun->be_lun->lun_type; 9670 else 9671 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9672 9673 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9674 /* Supported VPD pages */ 9675 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9676 /* Serial Number */ 9677 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9678 /* Device Identification */ 9679 pages->page_list[2] = SVPD_DEVICE_ID; 9680 /* Extended INQUIRY Data */ 9681 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9682 /* Mode Page Policy */ 9683 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9684 /* SCSI Ports */ 9685 pages->page_list[5] = SVPD_SCSI_PORTS; 9686 /* Third-party Copy */ 9687 pages->page_list[6] = SVPD_SCSI_TPC; 9688 /* Block limits */ 9689 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9690 /* Block Device Characteristics */ 9691 pages->page_list[8] = SVPD_BDC; 9692 /* Logical Block Provisioning */ 9693 pages->page_list[9] = SVPD_LBP; 9694 9695 ctl_set_success(ctsio); 9696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9697 ctsio->be_move_done = ctl_config_move_done; 9698 ctl_datamove((union ctl_io *)ctsio); 9699 return (CTL_RETVAL_COMPLETE); 9700} 9701 9702static int 9703ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9704{ 9705 struct scsi_vpd_unit_serial_number *sn_ptr; 9706 struct ctl_lun *lun; 9707 int data_len; 9708 9709 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9710 9711 data_len = 4 + CTL_SN_LEN; 9712 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9713 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9714 if (data_len < alloc_len) { 9715 ctsio->residual = alloc_len - data_len; 9716 ctsio->kern_data_len = data_len; 9717 ctsio->kern_total_len = data_len; 9718 } else { 9719 ctsio->residual = 0; 9720 ctsio->kern_data_len = alloc_len; 9721 ctsio->kern_total_len = alloc_len; 9722 } 9723 ctsio->kern_data_resid = 0; 9724 ctsio->kern_rel_offset = 0; 9725 ctsio->kern_sg_entries = 0; 9726 9727 /* 9728 * The control device is always connected. The disk device, on the 9729 * other hand, may not be online all the time. Need to change this 9730 * to figure out whether the disk device is actually online or not. 9731 */ 9732 if (lun != NULL) 9733 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9734 lun->be_lun->lun_type; 9735 else 9736 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9737 9738 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9739 sn_ptr->length = CTL_SN_LEN; 9740 /* 9741 * If we don't have a LUN, we just leave the serial number as 9742 * all spaces. 9743 */ 9744 if (lun != NULL) { 9745 strncpy((char *)sn_ptr->serial_num, 9746 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9747 } else 9748 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9749 9750 ctl_set_success(ctsio); 9751 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9752 ctsio->be_move_done = ctl_config_move_done; 9753 ctl_datamove((union ctl_io *)ctsio); 9754 return (CTL_RETVAL_COMPLETE); 9755} 9756 9757 9758static int 9759ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9760{ 9761 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9762 struct ctl_lun *lun; 9763 int data_len; 9764 9765 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9766 9767 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9768 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9769 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9770 ctsio->kern_sg_entries = 0; 9771 9772 if (data_len < alloc_len) { 9773 ctsio->residual = alloc_len - data_len; 9774 ctsio->kern_data_len = data_len; 9775 ctsio->kern_total_len = data_len; 9776 } else { 9777 ctsio->residual = 0; 9778 ctsio->kern_data_len = alloc_len; 9779 ctsio->kern_total_len = alloc_len; 9780 } 9781 ctsio->kern_data_resid = 0; 9782 ctsio->kern_rel_offset = 0; 9783 ctsio->kern_sg_entries = 0; 9784 9785 /* 9786 * The control device is always connected. The disk device, on the 9787 * other hand, may not be online all the time. 9788 */ 9789 if (lun != NULL) 9790 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9791 lun->be_lun->lun_type; 9792 else 9793 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9794 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9795 eid_ptr->page_length = data_len - 4; 9796 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9797 eid_ptr->flags3 = SVPD_EID_V_SUP; 9798 9799 ctl_set_success(ctsio); 9800 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9801 ctsio->be_move_done = ctl_config_move_done; 9802 ctl_datamove((union ctl_io *)ctsio); 9803 return (CTL_RETVAL_COMPLETE); 9804} 9805 9806static int 9807ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9808{ 9809 struct scsi_vpd_mode_page_policy *mpp_ptr; 9810 struct ctl_lun *lun; 9811 int data_len; 9812 9813 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9814 9815 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9816 sizeof(struct scsi_vpd_mode_page_policy_descr); 9817 9818 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9819 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9820 ctsio->kern_sg_entries = 0; 9821 9822 if (data_len < alloc_len) { 9823 ctsio->residual = alloc_len - data_len; 9824 ctsio->kern_data_len = data_len; 9825 ctsio->kern_total_len = data_len; 9826 } else { 9827 ctsio->residual = 0; 9828 ctsio->kern_data_len = alloc_len; 9829 ctsio->kern_total_len = alloc_len; 9830 } 9831 ctsio->kern_data_resid = 0; 9832 ctsio->kern_rel_offset = 0; 9833 ctsio->kern_sg_entries = 0; 9834 9835 /* 9836 * The control device is always connected. The disk device, on the 9837 * other hand, may not be online all the time. 9838 */ 9839 if (lun != NULL) 9840 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9841 lun->be_lun->lun_type; 9842 else 9843 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9844 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9845 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9846 mpp_ptr->descr[0].page_code = 0x3f; 9847 mpp_ptr->descr[0].subpage_code = 0xff; 9848 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9849 9850 ctl_set_success(ctsio); 9851 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9852 ctsio->be_move_done = ctl_config_move_done; 9853 ctl_datamove((union ctl_io *)ctsio); 9854 return (CTL_RETVAL_COMPLETE); 9855} 9856 9857static int 9858ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9859{ 9860 struct scsi_vpd_device_id *devid_ptr; 9861 struct scsi_vpd_id_descriptor *desc; 9862 struct ctl_softc *ctl_softc; 9863 struct ctl_lun *lun; 9864 struct ctl_port *port; 9865 int data_len; 9866 uint8_t proto; 9867 9868 ctl_softc = control_softc; 9869 9870 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9871 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9872 9873 data_len = sizeof(struct scsi_vpd_device_id) + 9874 sizeof(struct scsi_vpd_id_descriptor) + 9875 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9876 sizeof(struct scsi_vpd_id_descriptor) + 9877 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9878 if (lun && lun->lun_devid) 9879 data_len += lun->lun_devid->len; 9880 if (port->port_devid) 9881 data_len += port->port_devid->len; 9882 if (port->target_devid) 9883 data_len += port->target_devid->len; 9884 9885 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9886 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9887 ctsio->kern_sg_entries = 0; 9888 9889 if (data_len < alloc_len) { 9890 ctsio->residual = alloc_len - data_len; 9891 ctsio->kern_data_len = data_len; 9892 ctsio->kern_total_len = data_len; 9893 } else { 9894 ctsio->residual = 0; 9895 ctsio->kern_data_len = alloc_len; 9896 ctsio->kern_total_len = alloc_len; 9897 } 9898 ctsio->kern_data_resid = 0; 9899 ctsio->kern_rel_offset = 0; 9900 ctsio->kern_sg_entries = 0; 9901 9902 /* 9903 * The control device is always connected. The disk device, on the 9904 * other hand, may not be online all the time. 9905 */ 9906 if (lun != NULL) 9907 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9908 lun->be_lun->lun_type; 9909 else 9910 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9911 devid_ptr->page_code = SVPD_DEVICE_ID; 9912 scsi_ulto2b(data_len - 4, devid_ptr->length); 9913 9914 if (port->port_type == CTL_PORT_FC) 9915 proto = SCSI_PROTO_FC << 4; 9916 else if (port->port_type == CTL_PORT_ISCSI) 9917 proto = SCSI_PROTO_ISCSI << 4; 9918 else 9919 proto = SCSI_PROTO_SPI << 4; 9920 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9921 9922 /* 9923 * We're using a LUN association here. i.e., this device ID is a 9924 * per-LUN identifier. 9925 */ 9926 if (lun && lun->lun_devid) { 9927 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9928 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9929 lun->lun_devid->len); 9930 } 9931 9932 /* 9933 * This is for the WWPN which is a port association. 9934 */ 9935 if (port->port_devid) { 9936 memcpy(desc, port->port_devid->data, port->port_devid->len); 9937 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9938 port->port_devid->len); 9939 } 9940 9941 /* 9942 * This is for the Relative Target Port(type 4h) identifier 9943 */ 9944 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9945 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9946 SVPD_ID_TYPE_RELTARG; 9947 desc->length = 4; 9948 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9949 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9950 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9951 9952 /* 9953 * This is for the Target Port Group(type 5h) identifier 9954 */ 9955 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9956 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9957 SVPD_ID_TYPE_TPORTGRP; 9958 desc->length = 4; 9959 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9960 &desc->identifier[2]); 9961 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9962 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9963 9964 /* 9965 * This is for the Target identifier 9966 */ 9967 if (port->target_devid) { 9968 memcpy(desc, port->target_devid->data, port->target_devid->len); 9969 } 9970 9971 ctl_set_success(ctsio); 9972 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9973 ctsio->be_move_done = ctl_config_move_done; 9974 ctl_datamove((union ctl_io *)ctsio); 9975 return (CTL_RETVAL_COMPLETE); 9976} 9977 9978static int 9979ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9980{ 9981 struct ctl_softc *softc = control_softc; 9982 struct scsi_vpd_scsi_ports *sp; 9983 struct scsi_vpd_port_designation *pd; 9984 struct scsi_vpd_port_designation_cont *pdc; 9985 struct ctl_lun *lun; 9986 struct ctl_port *port; 9987 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9988 int num_target_port_groups; 9989 9990 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9991 9992 if (softc->is_single) 9993 num_target_port_groups = 1; 9994 else 9995 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9996 num_target_ports = 0; 9997 iid_len = 0; 9998 id_len = 0; 9999 mtx_lock(&softc->ctl_lock); 10000 STAILQ_FOREACH(port, &softc->port_list, links) { 10001 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10002 continue; 10003 if (lun != NULL && 10004 ctl_map_lun_back(port->targ_port, lun->lun) >= 10005 CTL_MAX_LUNS) 10006 continue; 10007 num_target_ports++; 10008 if (port->init_devid) 10009 iid_len += port->init_devid->len; 10010 if (port->port_devid) 10011 id_len += port->port_devid->len; 10012 } 10013 mtx_unlock(&softc->ctl_lock); 10014 10015 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10016 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10017 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10018 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10019 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10020 ctsio->kern_sg_entries = 0; 10021 10022 if (data_len < alloc_len) { 10023 ctsio->residual = alloc_len - data_len; 10024 ctsio->kern_data_len = data_len; 10025 ctsio->kern_total_len = data_len; 10026 } else { 10027 ctsio->residual = 0; 10028 ctsio->kern_data_len = alloc_len; 10029 ctsio->kern_total_len = alloc_len; 10030 } 10031 ctsio->kern_data_resid = 0; 10032 ctsio->kern_rel_offset = 0; 10033 ctsio->kern_sg_entries = 0; 10034 10035 /* 10036 * The control device is always connected. The disk device, on the 10037 * other hand, may not be online all the time. Need to change this 10038 * to figure out whether the disk device is actually online or not. 10039 */ 10040 if (lun != NULL) 10041 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10042 lun->be_lun->lun_type; 10043 else 10044 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10045 10046 sp->page_code = SVPD_SCSI_PORTS; 10047 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10048 sp->page_length); 10049 pd = &sp->design[0]; 10050 10051 mtx_lock(&softc->ctl_lock); 10052 pg = softc->port_offset / CTL_MAX_PORTS; 10053 for (g = 0; g < num_target_port_groups; g++) { 10054 STAILQ_FOREACH(port, &softc->port_list, links) { 10055 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10056 continue; 10057 if (lun != NULL && 10058 ctl_map_lun_back(port->targ_port, lun->lun) >= 10059 CTL_MAX_LUNS) 10060 continue; 10061 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10062 scsi_ulto2b(p, pd->relative_port_id); 10063 if (port->init_devid && g == pg) { 10064 iid_len = port->init_devid->len; 10065 memcpy(pd->initiator_transportid, 10066 port->init_devid->data, port->init_devid->len); 10067 } else 10068 iid_len = 0; 10069 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10070 pdc = (struct scsi_vpd_port_designation_cont *) 10071 (&pd->initiator_transportid[iid_len]); 10072 if (port->port_devid && g == pg) { 10073 id_len = port->port_devid->len; 10074 memcpy(pdc->target_port_descriptors, 10075 port->port_devid->data, port->port_devid->len); 10076 } else 10077 id_len = 0; 10078 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10079 pd = (struct scsi_vpd_port_designation *) 10080 ((uint8_t *)pdc->target_port_descriptors + id_len); 10081 } 10082 } 10083 mtx_unlock(&softc->ctl_lock); 10084 10085 ctl_set_success(ctsio); 10086 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10087 ctsio->be_move_done = ctl_config_move_done; 10088 ctl_datamove((union ctl_io *)ctsio); 10089 return (CTL_RETVAL_COMPLETE); 10090} 10091 10092static int 10093ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10094{ 10095 struct scsi_vpd_block_limits *bl_ptr; 10096 struct ctl_lun *lun; 10097 int bs; 10098 10099 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10100 10101 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10102 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10103 ctsio->kern_sg_entries = 0; 10104 10105 if (sizeof(*bl_ptr) < alloc_len) { 10106 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10107 ctsio->kern_data_len = sizeof(*bl_ptr); 10108 ctsio->kern_total_len = sizeof(*bl_ptr); 10109 } else { 10110 ctsio->residual = 0; 10111 ctsio->kern_data_len = alloc_len; 10112 ctsio->kern_total_len = alloc_len; 10113 } 10114 ctsio->kern_data_resid = 0; 10115 ctsio->kern_rel_offset = 0; 10116 ctsio->kern_sg_entries = 0; 10117 10118 /* 10119 * The control device is always connected. The disk device, on the 10120 * other hand, may not be online all the time. Need to change this 10121 * to figure out whether the disk device is actually online or not. 10122 */ 10123 if (lun != NULL) 10124 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10125 lun->be_lun->lun_type; 10126 else 10127 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10128 10129 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10130 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10131 bl_ptr->max_cmp_write_len = 0xff; 10132 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10133 if (lun != NULL) { 10134 bs = lun->be_lun->blocksize; 10135 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10136 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10137 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10138 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10139 if (lun->be_lun->pblockexp != 0) { 10140 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10141 bl_ptr->opt_unmap_grain); 10142 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10143 bl_ptr->unmap_grain_align); 10144 } 10145 } 10146 scsi_ulto4b(lun->be_lun->atomicblock, 10147 bl_ptr->max_atomic_transfer_length); 10148 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10149 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10150 } 10151 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10152 10153 ctl_set_success(ctsio); 10154 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10155 ctsio->be_move_done = ctl_config_move_done; 10156 ctl_datamove((union ctl_io *)ctsio); 10157 return (CTL_RETVAL_COMPLETE); 10158} 10159 10160static int 10161ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10162{ 10163 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10164 struct ctl_lun *lun; 10165 const char *value; 10166 u_int i; 10167 10168 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10169 10170 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10171 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10172 ctsio->kern_sg_entries = 0; 10173 10174 if (sizeof(*bdc_ptr) < alloc_len) { 10175 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10176 ctsio->kern_data_len = sizeof(*bdc_ptr); 10177 ctsio->kern_total_len = sizeof(*bdc_ptr); 10178 } else { 10179 ctsio->residual = 0; 10180 ctsio->kern_data_len = alloc_len; 10181 ctsio->kern_total_len = alloc_len; 10182 } 10183 ctsio->kern_data_resid = 0; 10184 ctsio->kern_rel_offset = 0; 10185 ctsio->kern_sg_entries = 0; 10186 10187 /* 10188 * The control device is always connected. The disk device, on the 10189 * other hand, may not be online all the time. Need to change this 10190 * to figure out whether the disk device is actually online or not. 10191 */ 10192 if (lun != NULL) 10193 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10194 lun->be_lun->lun_type; 10195 else 10196 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10197 bdc_ptr->page_code = SVPD_BDC; 10198 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10199 if (lun != NULL && 10200 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10201 i = strtol(value, NULL, 0); 10202 else 10203 i = CTL_DEFAULT_ROTATION_RATE; 10204 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10205 if (lun != NULL && 10206 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10207 i = strtol(value, NULL, 0); 10208 else 10209 i = 0; 10210 bdc_ptr->wab_wac_ff = (i & 0x0f); 10211 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10212 10213 ctl_set_success(ctsio); 10214 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10215 ctsio->be_move_done = ctl_config_move_done; 10216 ctl_datamove((union ctl_io *)ctsio); 10217 return (CTL_RETVAL_COMPLETE); 10218} 10219 10220static int 10221ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10222{ 10223 struct scsi_vpd_logical_block_prov *lbp_ptr; 10224 struct ctl_lun *lun; 10225 10226 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10227 10228 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10229 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10230 ctsio->kern_sg_entries = 0; 10231 10232 if (sizeof(*lbp_ptr) < alloc_len) { 10233 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10234 ctsio->kern_data_len = sizeof(*lbp_ptr); 10235 ctsio->kern_total_len = sizeof(*lbp_ptr); 10236 } else { 10237 ctsio->residual = 0; 10238 ctsio->kern_data_len = alloc_len; 10239 ctsio->kern_total_len = alloc_len; 10240 } 10241 ctsio->kern_data_resid = 0; 10242 ctsio->kern_rel_offset = 0; 10243 ctsio->kern_sg_entries = 0; 10244 10245 /* 10246 * The control device is always connected. The disk device, on the 10247 * other hand, may not be online all the time. Need to change this 10248 * to figure out whether the disk device is actually online or not. 10249 */ 10250 if (lun != NULL) 10251 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10252 lun->be_lun->lun_type; 10253 else 10254 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10255 10256 lbp_ptr->page_code = SVPD_LBP; 10257 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10258 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10259 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10260 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10261 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10262 lbp_ptr->prov_type = SVPD_LBP_THIN; 10263 } 10264 10265 ctl_set_success(ctsio); 10266 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10267 ctsio->be_move_done = ctl_config_move_done; 10268 ctl_datamove((union ctl_io *)ctsio); 10269 return (CTL_RETVAL_COMPLETE); 10270} 10271 10272static int 10273ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10274{ 10275 struct scsi_inquiry *cdb; 10276 int alloc_len, retval; 10277 10278 cdb = (struct scsi_inquiry *)ctsio->cdb; 10279 10280 retval = CTL_RETVAL_COMPLETE; 10281 10282 alloc_len = scsi_2btoul(cdb->length); 10283 10284 switch (cdb->page_code) { 10285 case SVPD_SUPPORTED_PAGES: 10286 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10287 break; 10288 case SVPD_UNIT_SERIAL_NUMBER: 10289 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10290 break; 10291 case SVPD_DEVICE_ID: 10292 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10293 break; 10294 case SVPD_EXTENDED_INQUIRY_DATA: 10295 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10296 break; 10297 case SVPD_MODE_PAGE_POLICY: 10298 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10299 break; 10300 case SVPD_SCSI_PORTS: 10301 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10302 break; 10303 case SVPD_SCSI_TPC: 10304 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10305 break; 10306 case SVPD_BLOCK_LIMITS: 10307 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10308 break; 10309 case SVPD_BDC: 10310 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10311 break; 10312 case SVPD_LBP: 10313 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10314 break; 10315 default: 10316 ctl_set_invalid_field(ctsio, 10317 /*sks_valid*/ 1, 10318 /*command*/ 1, 10319 /*field*/ 2, 10320 /*bit_valid*/ 0, 10321 /*bit*/ 0); 10322 ctl_done((union ctl_io *)ctsio); 10323 retval = CTL_RETVAL_COMPLETE; 10324 break; 10325 } 10326 10327 return (retval); 10328} 10329 10330static int 10331ctl_inquiry_std(struct ctl_scsiio *ctsio) 10332{ 10333 struct scsi_inquiry_data *inq_ptr; 10334 struct scsi_inquiry *cdb; 10335 struct ctl_softc *ctl_softc; 10336 struct ctl_lun *lun; 10337 char *val; 10338 uint32_t alloc_len, data_len; 10339 ctl_port_type port_type; 10340 10341 ctl_softc = control_softc; 10342 10343 /* 10344 * Figure out whether we're talking to a Fibre Channel port or not. 10345 * We treat the ioctl front end, and any SCSI adapters, as packetized 10346 * SCSI front ends. 10347 */ 10348 port_type = ctl_softc->ctl_ports[ 10349 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10350 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10351 port_type = CTL_PORT_SCSI; 10352 10353 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10354 cdb = (struct scsi_inquiry *)ctsio->cdb; 10355 alloc_len = scsi_2btoul(cdb->length); 10356 10357 /* 10358 * We malloc the full inquiry data size here and fill it 10359 * in. If the user only asks for less, we'll give him 10360 * that much. 10361 */ 10362 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10363 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10364 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10365 ctsio->kern_sg_entries = 0; 10366 ctsio->kern_data_resid = 0; 10367 ctsio->kern_rel_offset = 0; 10368 10369 if (data_len < alloc_len) { 10370 ctsio->residual = alloc_len - data_len; 10371 ctsio->kern_data_len = data_len; 10372 ctsio->kern_total_len = data_len; 10373 } else { 10374 ctsio->residual = 0; 10375 ctsio->kern_data_len = alloc_len; 10376 ctsio->kern_total_len = alloc_len; 10377 } 10378 10379 /* 10380 * If we have a LUN configured, report it as connected. Otherwise, 10381 * report that it is offline or no device is supported, depending 10382 * on the value of inquiry_pq_no_lun. 10383 * 10384 * According to the spec (SPC-4 r34), the peripheral qualifier 10385 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10386 * 10387 * "A peripheral device having the specified peripheral device type 10388 * is not connected to this logical unit. However, the device 10389 * server is capable of supporting the specified peripheral device 10390 * type on this logical unit." 10391 * 10392 * According to the same spec, the peripheral qualifier 10393 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10394 * 10395 * "The device server is not capable of supporting a peripheral 10396 * device on this logical unit. For this peripheral qualifier the 10397 * peripheral device type shall be set to 1Fh. All other peripheral 10398 * device type values are reserved for this peripheral qualifier." 10399 * 10400 * Given the text, it would seem that we probably want to report that 10401 * the LUN is offline here. There is no LUN connected, but we can 10402 * support a LUN at the given LUN number. 10403 * 10404 * In the real world, though, it sounds like things are a little 10405 * different: 10406 * 10407 * - Linux, when presented with a LUN with the offline peripheral 10408 * qualifier, will create an sg driver instance for it. So when 10409 * you attach it to CTL, you wind up with a ton of sg driver 10410 * instances. (One for every LUN that Linux bothered to probe.) 10411 * Linux does this despite the fact that it issues a REPORT LUNs 10412 * to LUN 0 to get the inventory of supported LUNs. 10413 * 10414 * - There is other anecdotal evidence (from Emulex folks) about 10415 * arrays that use the offline peripheral qualifier for LUNs that 10416 * are on the "passive" path in an active/passive array. 10417 * 10418 * So the solution is provide a hopefully reasonable default 10419 * (return bad/no LUN) and allow the user to change the behavior 10420 * with a tunable/sysctl variable. 10421 */ 10422 if (lun != NULL) 10423 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10424 lun->be_lun->lun_type; 10425 else if (ctl_softc->inquiry_pq_no_lun == 0) 10426 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10427 else 10428 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10429 10430 /* RMB in byte 2 is 0 */ 10431 inq_ptr->version = SCSI_REV_SPC4; 10432 10433 /* 10434 * According to SAM-3, even if a device only supports a single 10435 * level of LUN addressing, it should still set the HISUP bit: 10436 * 10437 * 4.9.1 Logical unit numbers overview 10438 * 10439 * All logical unit number formats described in this standard are 10440 * hierarchical in structure even when only a single level in that 10441 * hierarchy is used. The HISUP bit shall be set to one in the 10442 * standard INQUIRY data (see SPC-2) when any logical unit number 10443 * format described in this standard is used. Non-hierarchical 10444 * formats are outside the scope of this standard. 10445 * 10446 * Therefore we set the HiSup bit here. 10447 * 10448 * The reponse format is 2, per SPC-3. 10449 */ 10450 inq_ptr->response_format = SID_HiSup | 2; 10451 10452 inq_ptr->additional_length = data_len - 10453 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10454 CTL_DEBUG_PRINT(("additional_length = %d\n", 10455 inq_ptr->additional_length)); 10456 10457 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10458 /* 16 bit addressing */ 10459 if (port_type == CTL_PORT_SCSI) 10460 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10461 /* XXX set the SID_MultiP bit here if we're actually going to 10462 respond on multiple ports */ 10463 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10464 10465 /* 16 bit data bus, synchronous transfers */ 10466 if (port_type == CTL_PORT_SCSI) 10467 inq_ptr->flags = SID_WBus16 | SID_Sync; 10468 /* 10469 * XXX KDM do we want to support tagged queueing on the control 10470 * device at all? 10471 */ 10472 if ((lun == NULL) 10473 || (lun->be_lun->lun_type != T_PROCESSOR)) 10474 inq_ptr->flags |= SID_CmdQue; 10475 /* 10476 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10477 * We have 8 bytes for the vendor name, and 16 bytes for the device 10478 * name and 4 bytes for the revision. 10479 */ 10480 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10481 "vendor")) == NULL) { 10482 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10483 } else { 10484 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10485 strncpy(inq_ptr->vendor, val, 10486 min(sizeof(inq_ptr->vendor), strlen(val))); 10487 } 10488 if (lun == NULL) { 10489 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10490 sizeof(inq_ptr->product)); 10491 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10492 switch (lun->be_lun->lun_type) { 10493 case T_DIRECT: 10494 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10495 sizeof(inq_ptr->product)); 10496 break; 10497 case T_PROCESSOR: 10498 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10499 sizeof(inq_ptr->product)); 10500 break; 10501 default: 10502 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10503 sizeof(inq_ptr->product)); 10504 break; 10505 } 10506 } else { 10507 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10508 strncpy(inq_ptr->product, val, 10509 min(sizeof(inq_ptr->product), strlen(val))); 10510 } 10511 10512 /* 10513 * XXX make this a macro somewhere so it automatically gets 10514 * incremented when we make changes. 10515 */ 10516 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10517 "revision")) == NULL) { 10518 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10519 } else { 10520 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10521 strncpy(inq_ptr->revision, val, 10522 min(sizeof(inq_ptr->revision), strlen(val))); 10523 } 10524 10525 /* 10526 * For parallel SCSI, we support double transition and single 10527 * transition clocking. We also support QAS (Quick Arbitration 10528 * and Selection) and Information Unit transfers on both the 10529 * control and array devices. 10530 */ 10531 if (port_type == CTL_PORT_SCSI) 10532 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10533 SID_SPI_IUS; 10534 10535 /* SAM-5 (no version claimed) */ 10536 scsi_ulto2b(0x00A0, inq_ptr->version1); 10537 /* SPC-4 (no version claimed) */ 10538 scsi_ulto2b(0x0460, inq_ptr->version2); 10539 if (port_type == CTL_PORT_FC) { 10540 /* FCP-2 ANSI INCITS.350:2003 */ 10541 scsi_ulto2b(0x0917, inq_ptr->version3); 10542 } else if (port_type == CTL_PORT_SCSI) { 10543 /* SPI-4 ANSI INCITS.362:200x */ 10544 scsi_ulto2b(0x0B56, inq_ptr->version3); 10545 } else if (port_type == CTL_PORT_ISCSI) { 10546 /* iSCSI (no version claimed) */ 10547 scsi_ulto2b(0x0960, inq_ptr->version3); 10548 } else if (port_type == CTL_PORT_SAS) { 10549 /* SAS (no version claimed) */ 10550 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10551 } 10552 10553 if (lun == NULL) { 10554 /* SBC-4 (no version claimed) */ 10555 scsi_ulto2b(0x0600, inq_ptr->version4); 10556 } else { 10557 switch (lun->be_lun->lun_type) { 10558 case T_DIRECT: 10559 /* SBC-4 (no version claimed) */ 10560 scsi_ulto2b(0x0600, inq_ptr->version4); 10561 break; 10562 case T_PROCESSOR: 10563 default: 10564 break; 10565 } 10566 } 10567 10568 ctl_set_success(ctsio); 10569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10570 ctsio->be_move_done = ctl_config_move_done; 10571 ctl_datamove((union ctl_io *)ctsio); 10572 return (CTL_RETVAL_COMPLETE); 10573} 10574 10575int 10576ctl_inquiry(struct ctl_scsiio *ctsio) 10577{ 10578 struct scsi_inquiry *cdb; 10579 int retval; 10580 10581 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10582 10583 cdb = (struct scsi_inquiry *)ctsio->cdb; 10584 if (cdb->byte2 & SI_EVPD) 10585 retval = ctl_inquiry_evpd(ctsio); 10586 else if (cdb->page_code == 0) 10587 retval = ctl_inquiry_std(ctsio); 10588 else { 10589 ctl_set_invalid_field(ctsio, 10590 /*sks_valid*/ 1, 10591 /*command*/ 1, 10592 /*field*/ 2, 10593 /*bit_valid*/ 0, 10594 /*bit*/ 0); 10595 ctl_done((union ctl_io *)ctsio); 10596 return (CTL_RETVAL_COMPLETE); 10597 } 10598 10599 return (retval); 10600} 10601 10602/* 10603 * For known CDB types, parse the LBA and length. 10604 */ 10605static int 10606ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10607{ 10608 if (io->io_hdr.io_type != CTL_IO_SCSI) 10609 return (1); 10610 10611 switch (io->scsiio.cdb[0]) { 10612 case COMPARE_AND_WRITE: { 10613 struct scsi_compare_and_write *cdb; 10614 10615 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10616 10617 *lba = scsi_8btou64(cdb->addr); 10618 *len = cdb->length; 10619 break; 10620 } 10621 case READ_6: 10622 case WRITE_6: { 10623 struct scsi_rw_6 *cdb; 10624 10625 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10626 10627 *lba = scsi_3btoul(cdb->addr); 10628 /* only 5 bits are valid in the most significant address byte */ 10629 *lba &= 0x1fffff; 10630 *len = cdb->length; 10631 break; 10632 } 10633 case READ_10: 10634 case WRITE_10: { 10635 struct scsi_rw_10 *cdb; 10636 10637 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10638 10639 *lba = scsi_4btoul(cdb->addr); 10640 *len = scsi_2btoul(cdb->length); 10641 break; 10642 } 10643 case WRITE_VERIFY_10: { 10644 struct scsi_write_verify_10 *cdb; 10645 10646 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10647 10648 *lba = scsi_4btoul(cdb->addr); 10649 *len = scsi_2btoul(cdb->length); 10650 break; 10651 } 10652 case READ_12: 10653 case WRITE_12: { 10654 struct scsi_rw_12 *cdb; 10655 10656 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10657 10658 *lba = scsi_4btoul(cdb->addr); 10659 *len = scsi_4btoul(cdb->length); 10660 break; 10661 } 10662 case WRITE_VERIFY_12: { 10663 struct scsi_write_verify_12 *cdb; 10664 10665 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10666 10667 *lba = scsi_4btoul(cdb->addr); 10668 *len = scsi_4btoul(cdb->length); 10669 break; 10670 } 10671 case READ_16: 10672 case WRITE_16: 10673 case WRITE_ATOMIC_16: { 10674 struct scsi_rw_16 *cdb; 10675 10676 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10677 10678 *lba = scsi_8btou64(cdb->addr); 10679 *len = scsi_4btoul(cdb->length); 10680 break; 10681 } 10682 case WRITE_VERIFY_16: { 10683 struct scsi_write_verify_16 *cdb; 10684 10685 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10686 10687 *lba = scsi_8btou64(cdb->addr); 10688 *len = scsi_4btoul(cdb->length); 10689 break; 10690 } 10691 case WRITE_SAME_10: { 10692 struct scsi_write_same_10 *cdb; 10693 10694 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10695 10696 *lba = scsi_4btoul(cdb->addr); 10697 *len = scsi_2btoul(cdb->length); 10698 break; 10699 } 10700 case WRITE_SAME_16: { 10701 struct scsi_write_same_16 *cdb; 10702 10703 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10704 10705 *lba = scsi_8btou64(cdb->addr); 10706 *len = scsi_4btoul(cdb->length); 10707 break; 10708 } 10709 case VERIFY_10: { 10710 struct scsi_verify_10 *cdb; 10711 10712 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10713 10714 *lba = scsi_4btoul(cdb->addr); 10715 *len = scsi_2btoul(cdb->length); 10716 break; 10717 } 10718 case VERIFY_12: { 10719 struct scsi_verify_12 *cdb; 10720 10721 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10722 10723 *lba = scsi_4btoul(cdb->addr); 10724 *len = scsi_4btoul(cdb->length); 10725 break; 10726 } 10727 case VERIFY_16: { 10728 struct scsi_verify_16 *cdb; 10729 10730 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10731 10732 *lba = scsi_8btou64(cdb->addr); 10733 *len = scsi_4btoul(cdb->length); 10734 break; 10735 } 10736 case UNMAP: { 10737 *lba = 0; 10738 *len = UINT64_MAX; 10739 break; 10740 } 10741 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10742 struct scsi_get_lba_status *cdb; 10743 10744 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10745 *lba = scsi_8btou64(cdb->addr); 10746 *len = UINT32_MAX; 10747 break; 10748 } 10749 default: 10750 return (1); 10751 break; /* NOTREACHED */ 10752 } 10753 10754 return (0); 10755} 10756 10757static ctl_action 10758ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10759{ 10760 uint64_t endlba1, endlba2; 10761 10762 endlba1 = lba1 + len1 - 1; 10763 endlba2 = lba2 + len2 - 1; 10764 10765 if ((endlba1 < lba2) 10766 || (endlba2 < lba1)) 10767 return (CTL_ACTION_PASS); 10768 else 10769 return (CTL_ACTION_BLOCK); 10770} 10771 10772static int 10773ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10774{ 10775 struct ctl_ptr_len_flags *ptrlen; 10776 struct scsi_unmap_desc *buf, *end, *range; 10777 uint64_t lba; 10778 uint32_t len; 10779 10780 /* If not UNMAP -- go other way. */ 10781 if (io->io_hdr.io_type != CTL_IO_SCSI || 10782 io->scsiio.cdb[0] != UNMAP) 10783 return (CTL_ACTION_ERROR); 10784 10785 /* If UNMAP without data -- block and wait for data. */ 10786 ptrlen = (struct ctl_ptr_len_flags *) 10787 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10788 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10789 ptrlen->ptr == NULL) 10790 return (CTL_ACTION_BLOCK); 10791 10792 /* UNMAP with data -- check for collision. */ 10793 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10794 end = buf + ptrlen->len / sizeof(*buf); 10795 for (range = buf; range < end; range++) { 10796 lba = scsi_8btou64(range->lba); 10797 len = scsi_4btoul(range->length); 10798 if ((lba < lba2 + len2) && (lba + len > lba2)) 10799 return (CTL_ACTION_BLOCK); 10800 } 10801 return (CTL_ACTION_PASS); 10802} 10803 10804static ctl_action 10805ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10806{ 10807 uint64_t lba1, lba2; 10808 uint64_t len1, len2; 10809 int retval; 10810 10811 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10812 return (CTL_ACTION_ERROR); 10813 10814 retval = ctl_extent_check_unmap(io2, lba1, len1); 10815 if (retval != CTL_ACTION_ERROR) 10816 return (retval); 10817 10818 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10819 return (CTL_ACTION_ERROR); 10820 10821 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10822} 10823 10824static ctl_action 10825ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10826 union ctl_io *ooa_io) 10827{ 10828 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10829 ctl_serialize_action *serialize_row; 10830 10831 /* 10832 * The initiator attempted multiple untagged commands at the same 10833 * time. Can't do that. 10834 */ 10835 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10836 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10837 && ((pending_io->io_hdr.nexus.targ_port == 10838 ooa_io->io_hdr.nexus.targ_port) 10839 && (pending_io->io_hdr.nexus.initid.id == 10840 ooa_io->io_hdr.nexus.initid.id)) 10841 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10842 return (CTL_ACTION_OVERLAP); 10843 10844 /* 10845 * The initiator attempted to send multiple tagged commands with 10846 * the same ID. (It's fine if different initiators have the same 10847 * tag ID.) 10848 * 10849 * Even if all of those conditions are true, we don't kill the I/O 10850 * if the command ahead of us has been aborted. We won't end up 10851 * sending it to the FETD, and it's perfectly legal to resend a 10852 * command with the same tag number as long as the previous 10853 * instance of this tag number has been aborted somehow. 10854 */ 10855 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10856 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10857 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10858 && ((pending_io->io_hdr.nexus.targ_port == 10859 ooa_io->io_hdr.nexus.targ_port) 10860 && (pending_io->io_hdr.nexus.initid.id == 10861 ooa_io->io_hdr.nexus.initid.id)) 10862 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10863 return (CTL_ACTION_OVERLAP_TAG); 10864 10865 /* 10866 * If we get a head of queue tag, SAM-3 says that we should 10867 * immediately execute it. 10868 * 10869 * What happens if this command would normally block for some other 10870 * reason? e.g. a request sense with a head of queue tag 10871 * immediately after a write. Normally that would block, but this 10872 * will result in its getting executed immediately... 10873 * 10874 * We currently return "pass" instead of "skip", so we'll end up 10875 * going through the rest of the queue to check for overlapped tags. 10876 * 10877 * XXX KDM check for other types of blockage first?? 10878 */ 10879 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10880 return (CTL_ACTION_PASS); 10881 10882 /* 10883 * Ordered tags have to block until all items ahead of them 10884 * have completed. If we get called with an ordered tag, we always 10885 * block, if something else is ahead of us in the queue. 10886 */ 10887 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10888 return (CTL_ACTION_BLOCK); 10889 10890 /* 10891 * Simple tags get blocked until all head of queue and ordered tags 10892 * ahead of them have completed. I'm lumping untagged commands in 10893 * with simple tags here. XXX KDM is that the right thing to do? 10894 */ 10895 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10896 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10897 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10898 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10899 return (CTL_ACTION_BLOCK); 10900 10901 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10902 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10903 10904 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10905 10906 switch (serialize_row[pending_entry->seridx]) { 10907 case CTL_SER_BLOCK: 10908 return (CTL_ACTION_BLOCK); 10909 case CTL_SER_EXTENT: 10910 return (ctl_extent_check(pending_io, ooa_io)); 10911 case CTL_SER_EXTENTOPT: 10912 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10913 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10914 return (ctl_extent_check(pending_io, ooa_io)); 10915 /* FALLTHROUGH */ 10916 case CTL_SER_PASS: 10917 return (CTL_ACTION_PASS); 10918 case CTL_SER_BLOCKOPT: 10919 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10920 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10921 return (CTL_ACTION_BLOCK); 10922 return (CTL_ACTION_PASS); 10923 case CTL_SER_SKIP: 10924 return (CTL_ACTION_SKIP); 10925 default: 10926 panic("invalid serialization value %d", 10927 serialize_row[pending_entry->seridx]); 10928 } 10929 10930 return (CTL_ACTION_ERROR); 10931} 10932 10933/* 10934 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10935 * Assumptions: 10936 * - pending_io is generally either incoming, or on the blocked queue 10937 * - starting I/O is the I/O we want to start the check with. 10938 */ 10939static ctl_action 10940ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10941 union ctl_io *starting_io) 10942{ 10943 union ctl_io *ooa_io; 10944 ctl_action action; 10945 10946 mtx_assert(&lun->lun_lock, MA_OWNED); 10947 10948 /* 10949 * Run back along the OOA queue, starting with the current 10950 * blocked I/O and going through every I/O before it on the 10951 * queue. If starting_io is NULL, we'll just end up returning 10952 * CTL_ACTION_PASS. 10953 */ 10954 for (ooa_io = starting_io; ooa_io != NULL; 10955 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10956 ooa_links)){ 10957 10958 /* 10959 * This routine just checks to see whether 10960 * cur_blocked is blocked by ooa_io, which is ahead 10961 * of it in the queue. It doesn't queue/dequeue 10962 * cur_blocked. 10963 */ 10964 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10965 switch (action) { 10966 case CTL_ACTION_BLOCK: 10967 case CTL_ACTION_OVERLAP: 10968 case CTL_ACTION_OVERLAP_TAG: 10969 case CTL_ACTION_SKIP: 10970 case CTL_ACTION_ERROR: 10971 return (action); 10972 break; /* NOTREACHED */ 10973 case CTL_ACTION_PASS: 10974 break; 10975 default: 10976 panic("invalid action %d", action); 10977 break; /* NOTREACHED */ 10978 } 10979 } 10980 10981 return (CTL_ACTION_PASS); 10982} 10983 10984/* 10985 * Assumptions: 10986 * - An I/O has just completed, and has been removed from the per-LUN OOA 10987 * queue, so some items on the blocked queue may now be unblocked. 10988 */ 10989static int 10990ctl_check_blocked(struct ctl_lun *lun) 10991{ 10992 union ctl_io *cur_blocked, *next_blocked; 10993 10994 mtx_assert(&lun->lun_lock, MA_OWNED); 10995 10996 /* 10997 * Run forward from the head of the blocked queue, checking each 10998 * entry against the I/Os prior to it on the OOA queue to see if 10999 * there is still any blockage. 11000 * 11001 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11002 * with our removing a variable on it while it is traversing the 11003 * list. 11004 */ 11005 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11006 cur_blocked != NULL; cur_blocked = next_blocked) { 11007 union ctl_io *prev_ooa; 11008 ctl_action action; 11009 11010 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11011 blocked_links); 11012 11013 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11014 ctl_ooaq, ooa_links); 11015 11016 /* 11017 * If cur_blocked happens to be the first item in the OOA 11018 * queue now, prev_ooa will be NULL, and the action 11019 * returned will just be CTL_ACTION_PASS. 11020 */ 11021 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11022 11023 switch (action) { 11024 case CTL_ACTION_BLOCK: 11025 /* Nothing to do here, still blocked */ 11026 break; 11027 case CTL_ACTION_OVERLAP: 11028 case CTL_ACTION_OVERLAP_TAG: 11029 /* 11030 * This shouldn't happen! In theory we've already 11031 * checked this command for overlap... 11032 */ 11033 break; 11034 case CTL_ACTION_PASS: 11035 case CTL_ACTION_SKIP: { 11036 struct ctl_softc *softc; 11037 const struct ctl_cmd_entry *entry; 11038 int isc_retval; 11039 11040 /* 11041 * The skip case shouldn't happen, this transaction 11042 * should have never made it onto the blocked queue. 11043 */ 11044 /* 11045 * This I/O is no longer blocked, we can remove it 11046 * from the blocked queue. Since this is a TAILQ 11047 * (doubly linked list), we can do O(1) removals 11048 * from any place on the list. 11049 */ 11050 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11051 blocked_links); 11052 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11053 11054 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11055 /* 11056 * Need to send IO back to original side to 11057 * run 11058 */ 11059 union ctl_ha_msg msg_info; 11060 11061 msg_info.hdr.original_sc = 11062 cur_blocked->io_hdr.original_sc; 11063 msg_info.hdr.serializing_sc = cur_blocked; 11064 msg_info.hdr.msg_type = CTL_MSG_R2R; 11065 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11066 &msg_info, sizeof(msg_info), 0)) > 11067 CTL_HA_STATUS_SUCCESS) { 11068 printf("CTL:Check Blocked error from " 11069 "ctl_ha_msg_send %d\n", 11070 isc_retval); 11071 } 11072 break; 11073 } 11074 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11075 softc = control_softc; 11076 11077 /* 11078 * Check this I/O for LUN state changes that may 11079 * have happened while this command was blocked. 11080 * The LUN state may have been changed by a command 11081 * ahead of us in the queue, so we need to re-check 11082 * for any states that can be caused by SCSI 11083 * commands. 11084 */ 11085 if (ctl_scsiio_lun_check(softc, lun, entry, 11086 &cur_blocked->scsiio) == 0) { 11087 cur_blocked->io_hdr.flags |= 11088 CTL_FLAG_IS_WAS_ON_RTR; 11089 ctl_enqueue_rtr(cur_blocked); 11090 } else 11091 ctl_done(cur_blocked); 11092 break; 11093 } 11094 default: 11095 /* 11096 * This probably shouldn't happen -- we shouldn't 11097 * get CTL_ACTION_ERROR, or anything else. 11098 */ 11099 break; 11100 } 11101 } 11102 11103 return (CTL_RETVAL_COMPLETE); 11104} 11105 11106/* 11107 * This routine (with one exception) checks LUN flags that can be set by 11108 * commands ahead of us in the OOA queue. These flags have to be checked 11109 * when a command initially comes in, and when we pull a command off the 11110 * blocked queue and are preparing to execute it. The reason we have to 11111 * check these flags for commands on the blocked queue is that the LUN 11112 * state may have been changed by a command ahead of us while we're on the 11113 * blocked queue. 11114 * 11115 * Ordering is somewhat important with these checks, so please pay 11116 * careful attention to the placement of any new checks. 11117 */ 11118static int 11119ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11120 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11121{ 11122 int retval; 11123 uint32_t residx; 11124 11125 retval = 0; 11126 11127 mtx_assert(&lun->lun_lock, MA_OWNED); 11128 11129 /* 11130 * If this shelf is a secondary shelf controller, we have to reject 11131 * any media access commands. 11132 */ 11133 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11134 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11135 ctl_set_lun_standby(ctsio); 11136 retval = 1; 11137 goto bailout; 11138 } 11139 11140 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11141 if (lun->flags & CTL_LUN_READONLY) { 11142 ctl_set_sense(ctsio, /*current_error*/ 1, 11143 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11144 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11145 retval = 1; 11146 goto bailout; 11147 } 11148 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11149 .eca_and_aen & SCP_SWP) != 0) { 11150 ctl_set_sense(ctsio, /*current_error*/ 1, 11151 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11152 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11153 retval = 1; 11154 goto bailout; 11155 } 11156 } 11157 11158 /* 11159 * Check for a reservation conflict. If this command isn't allowed 11160 * even on reserved LUNs, and if this initiator isn't the one who 11161 * reserved us, reject the command with a reservation conflict. 11162 */ 11163 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11164 if ((lun->flags & CTL_LUN_RESERVED) 11165 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11166 if (lun->res_idx != residx) { 11167 ctl_set_reservation_conflict(ctsio); 11168 retval = 1; 11169 goto bailout; 11170 } 11171 } 11172 11173 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11174 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11175 /* No reservation or command is allowed. */; 11176 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11177 (lun->res_type == SPR_TYPE_WR_EX || 11178 lun->res_type == SPR_TYPE_WR_EX_RO || 11179 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11180 /* The command is allowed for Write Exclusive resv. */; 11181 } else { 11182 /* 11183 * if we aren't registered or it's a res holder type 11184 * reservation and this isn't the res holder then set a 11185 * conflict. 11186 */ 11187 if (ctl_get_prkey(lun, residx) == 0 11188 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11189 ctl_set_reservation_conflict(ctsio); 11190 retval = 1; 11191 goto bailout; 11192 } 11193 11194 } 11195 11196 if ((lun->flags & CTL_LUN_OFFLINE) 11197 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11198 ctl_set_lun_not_ready(ctsio); 11199 retval = 1; 11200 goto bailout; 11201 } 11202 11203 /* 11204 * If the LUN is stopped, see if this particular command is allowed 11205 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11206 */ 11207 if ((lun->flags & CTL_LUN_STOPPED) 11208 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11209 /* "Logical unit not ready, initializing cmd. required" */ 11210 ctl_set_lun_stopped(ctsio); 11211 retval = 1; 11212 goto bailout; 11213 } 11214 11215 if ((lun->flags & CTL_LUN_INOPERABLE) 11216 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11217 /* "Medium format corrupted" */ 11218 ctl_set_medium_format_corrupted(ctsio); 11219 retval = 1; 11220 goto bailout; 11221 } 11222 11223bailout: 11224 return (retval); 11225 11226} 11227 11228static void 11229ctl_failover_io(union ctl_io *io, int have_lock) 11230{ 11231 ctl_set_busy(&io->scsiio); 11232 ctl_done(io); 11233} 11234 11235static void 11236ctl_failover(void) 11237{ 11238 struct ctl_lun *lun; 11239 struct ctl_softc *ctl_softc; 11240 union ctl_io *next_io, *pending_io; 11241 union ctl_io *io; 11242 int lun_idx; 11243 11244 ctl_softc = control_softc; 11245 11246 mtx_lock(&ctl_softc->ctl_lock); 11247 /* 11248 * Remove any cmds from the other SC from the rtr queue. These 11249 * will obviously only be for LUNs for which we're the primary. 11250 * We can't send status or get/send data for these commands. 11251 * Since they haven't been executed yet, we can just remove them. 11252 * We'll either abort them or delete them below, depending on 11253 * which HA mode we're in. 11254 */ 11255#ifdef notyet 11256 mtx_lock(&ctl_softc->queue_lock); 11257 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11258 io != NULL; io = next_io) { 11259 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11260 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11261 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11262 ctl_io_hdr, links); 11263 } 11264 mtx_unlock(&ctl_softc->queue_lock); 11265#endif 11266 11267 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11268 lun = ctl_softc->ctl_luns[lun_idx]; 11269 if (lun==NULL) 11270 continue; 11271 11272 /* 11273 * Processor LUNs are primary on both sides. 11274 * XXX will this always be true? 11275 */ 11276 if (lun->be_lun->lun_type == T_PROCESSOR) 11277 continue; 11278 11279 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11280 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11281 printf("FAILOVER: primary lun %d\n", lun_idx); 11282 /* 11283 * Remove all commands from the other SC. First from the 11284 * blocked queue then from the ooa queue. Once we have 11285 * removed them. Call ctl_check_blocked to see if there 11286 * is anything that can run. 11287 */ 11288 for (io = (union ctl_io *)TAILQ_FIRST( 11289 &lun->blocked_queue); io != NULL; io = next_io) { 11290 11291 next_io = (union ctl_io *)TAILQ_NEXT( 11292 &io->io_hdr, blocked_links); 11293 11294 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11295 TAILQ_REMOVE(&lun->blocked_queue, 11296 &io->io_hdr,blocked_links); 11297 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11298 TAILQ_REMOVE(&lun->ooa_queue, 11299 &io->io_hdr, ooa_links); 11300 11301 ctl_free_io(io); 11302 } 11303 } 11304 11305 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11306 io != NULL; io = next_io) { 11307 11308 next_io = (union ctl_io *)TAILQ_NEXT( 11309 &io->io_hdr, ooa_links); 11310 11311 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11312 11313 TAILQ_REMOVE(&lun->ooa_queue, 11314 &io->io_hdr, 11315 ooa_links); 11316 11317 ctl_free_io(io); 11318 } 11319 } 11320 ctl_check_blocked(lun); 11321 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11322 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11323 11324 printf("FAILOVER: primary lun %d\n", lun_idx); 11325 /* 11326 * Abort all commands from the other SC. We can't 11327 * send status back for them now. These should get 11328 * cleaned up when they are completed or come out 11329 * for a datamove operation. 11330 */ 11331 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11332 io != NULL; io = next_io) { 11333 next_io = (union ctl_io *)TAILQ_NEXT( 11334 &io->io_hdr, ooa_links); 11335 11336 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11337 io->io_hdr.flags |= CTL_FLAG_ABORT; 11338 } 11339 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11340 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11341 11342 printf("FAILOVER: secondary lun %d\n", lun_idx); 11343 11344 lun->flags |= CTL_LUN_PRIMARY_SC; 11345 11346 /* 11347 * We send all I/O that was sent to this controller 11348 * and redirected to the other side back with 11349 * busy status, and have the initiator retry it. 11350 * Figuring out how much data has been transferred, 11351 * etc. and picking up where we left off would be 11352 * very tricky. 11353 * 11354 * XXX KDM need to remove I/O from the blocked 11355 * queue as well! 11356 */ 11357 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11358 &lun->ooa_queue); pending_io != NULL; 11359 pending_io = next_io) { 11360 11361 next_io = (union ctl_io *)TAILQ_NEXT( 11362 &pending_io->io_hdr, ooa_links); 11363 11364 pending_io->io_hdr.flags &= 11365 ~CTL_FLAG_SENT_2OTHER_SC; 11366 11367 if (pending_io->io_hdr.flags & 11368 CTL_FLAG_IO_ACTIVE) { 11369 pending_io->io_hdr.flags |= 11370 CTL_FLAG_FAILOVER; 11371 } else { 11372 ctl_set_busy(&pending_io->scsiio); 11373 ctl_done(pending_io); 11374 } 11375 } 11376 11377 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11378 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11379 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11380 printf("FAILOVER: secondary lun %d\n", lun_idx); 11381 /* 11382 * if the first io on the OOA is not on the RtR queue 11383 * add it. 11384 */ 11385 lun->flags |= CTL_LUN_PRIMARY_SC; 11386 11387 pending_io = (union ctl_io *)TAILQ_FIRST( 11388 &lun->ooa_queue); 11389 if (pending_io==NULL) { 11390 printf("Nothing on OOA queue\n"); 11391 continue; 11392 } 11393 11394 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11395 if ((pending_io->io_hdr.flags & 11396 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11397 pending_io->io_hdr.flags |= 11398 CTL_FLAG_IS_WAS_ON_RTR; 11399 ctl_enqueue_rtr(pending_io); 11400 } 11401#if 0 11402 else 11403 { 11404 printf("Tag 0x%04x is running\n", 11405 pending_io->scsiio.tag_num); 11406 } 11407#endif 11408 11409 next_io = (union ctl_io *)TAILQ_NEXT( 11410 &pending_io->io_hdr, ooa_links); 11411 for (pending_io=next_io; pending_io != NULL; 11412 pending_io = next_io) { 11413 pending_io->io_hdr.flags &= 11414 ~CTL_FLAG_SENT_2OTHER_SC; 11415 next_io = (union ctl_io *)TAILQ_NEXT( 11416 &pending_io->io_hdr, ooa_links); 11417 if (pending_io->io_hdr.flags & 11418 CTL_FLAG_IS_WAS_ON_RTR) { 11419#if 0 11420 printf("Tag 0x%04x is running\n", 11421 pending_io->scsiio.tag_num); 11422#endif 11423 continue; 11424 } 11425 11426 switch (ctl_check_ooa(lun, pending_io, 11427 (union ctl_io *)TAILQ_PREV( 11428 &pending_io->io_hdr, ctl_ooaq, 11429 ooa_links))) { 11430 11431 case CTL_ACTION_BLOCK: 11432 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11433 &pending_io->io_hdr, 11434 blocked_links); 11435 pending_io->io_hdr.flags |= 11436 CTL_FLAG_BLOCKED; 11437 break; 11438 case CTL_ACTION_PASS: 11439 case CTL_ACTION_SKIP: 11440 pending_io->io_hdr.flags |= 11441 CTL_FLAG_IS_WAS_ON_RTR; 11442 ctl_enqueue_rtr(pending_io); 11443 break; 11444 case CTL_ACTION_OVERLAP: 11445 ctl_set_overlapped_cmd( 11446 (struct ctl_scsiio *)pending_io); 11447 ctl_done(pending_io); 11448 break; 11449 case CTL_ACTION_OVERLAP_TAG: 11450 ctl_set_overlapped_tag( 11451 (struct ctl_scsiio *)pending_io, 11452 pending_io->scsiio.tag_num & 0xff); 11453 ctl_done(pending_io); 11454 break; 11455 case CTL_ACTION_ERROR: 11456 default: 11457 ctl_set_internal_failure( 11458 (struct ctl_scsiio *)pending_io, 11459 0, // sks_valid 11460 0); //retry count 11461 ctl_done(pending_io); 11462 break; 11463 } 11464 } 11465 11466 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11467 } else { 11468 panic("Unhandled HA mode failover, LUN flags = %#x, " 11469 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11470 } 11471 } 11472 ctl_pause_rtr = 0; 11473 mtx_unlock(&ctl_softc->ctl_lock); 11474} 11475 11476static int 11477ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11478{ 11479 struct ctl_lun *lun; 11480 const struct ctl_cmd_entry *entry; 11481 uint32_t initidx, targ_lun; 11482 int retval; 11483 11484 retval = 0; 11485 11486 lun = NULL; 11487 11488 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11489 if ((targ_lun < CTL_MAX_LUNS) 11490 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11491 /* 11492 * If the LUN is invalid, pretend that it doesn't exist. 11493 * It will go away as soon as all pending I/O has been 11494 * completed. 11495 */ 11496 mtx_lock(&lun->lun_lock); 11497 if (lun->flags & CTL_LUN_DISABLED) { 11498 mtx_unlock(&lun->lun_lock); 11499 lun = NULL; 11500 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11501 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11502 } else { 11503 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11504 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11505 lun->be_lun; 11506 if (lun->be_lun->lun_type == T_PROCESSOR) { 11507 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11508 } 11509 11510 /* 11511 * Every I/O goes into the OOA queue for a 11512 * particular LUN, and stays there until completion. 11513 */ 11514 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11515 ooa_links); 11516 } 11517 } else { 11518 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11519 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11520 } 11521 11522 /* Get command entry and return error if it is unsuppotyed. */ 11523 entry = ctl_validate_command(ctsio); 11524 if (entry == NULL) { 11525 if (lun) 11526 mtx_unlock(&lun->lun_lock); 11527 return (retval); 11528 } 11529 11530 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11531 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11532 11533 /* 11534 * Check to see whether we can send this command to LUNs that don't 11535 * exist. This should pretty much only be the case for inquiry 11536 * and request sense. Further checks, below, really require having 11537 * a LUN, so we can't really check the command anymore. Just put 11538 * it on the rtr queue. 11539 */ 11540 if (lun == NULL) { 11541 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11542 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11543 ctl_enqueue_rtr((union ctl_io *)ctsio); 11544 return (retval); 11545 } 11546 11547 ctl_set_unsupported_lun(ctsio); 11548 ctl_done((union ctl_io *)ctsio); 11549 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11550 return (retval); 11551 } else { 11552 /* 11553 * Make sure we support this particular command on this LUN. 11554 * e.g., we don't support writes to the control LUN. 11555 */ 11556 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11557 mtx_unlock(&lun->lun_lock); 11558 ctl_set_invalid_opcode(ctsio); 11559 ctl_done((union ctl_io *)ctsio); 11560 return (retval); 11561 } 11562 } 11563 11564 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11565 11566#ifdef CTL_WITH_CA 11567 /* 11568 * If we've got a request sense, it'll clear the contingent 11569 * allegiance condition. Otherwise, if we have a CA condition for 11570 * this initiator, clear it, because it sent down a command other 11571 * than request sense. 11572 */ 11573 if ((ctsio->cdb[0] != REQUEST_SENSE) 11574 && (ctl_is_set(lun->have_ca, initidx))) 11575 ctl_clear_mask(lun->have_ca, initidx); 11576#endif 11577 11578 /* 11579 * If the command has this flag set, it handles its own unit 11580 * attention reporting, we shouldn't do anything. Otherwise we 11581 * check for any pending unit attentions, and send them back to the 11582 * initiator. We only do this when a command initially comes in, 11583 * not when we pull it off the blocked queue. 11584 * 11585 * According to SAM-3, section 5.3.2, the order that things get 11586 * presented back to the host is basically unit attentions caused 11587 * by some sort of reset event, busy status, reservation conflicts 11588 * or task set full, and finally any other status. 11589 * 11590 * One issue here is that some of the unit attentions we report 11591 * don't fall into the "reset" category (e.g. "reported luns data 11592 * has changed"). So reporting it here, before the reservation 11593 * check, may be technically wrong. I guess the only thing to do 11594 * would be to check for and report the reset events here, and then 11595 * check for the other unit attention types after we check for a 11596 * reservation conflict. 11597 * 11598 * XXX KDM need to fix this 11599 */ 11600 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11601 ctl_ua_type ua_type; 11602 scsi_sense_data_type sense_format; 11603 11604 if (lun->flags & CTL_LUN_SENSE_DESC) 11605 sense_format = SSD_TYPE_DESC; 11606 else 11607 sense_format = SSD_TYPE_FIXED; 11608 11609 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11610 sense_format); 11611 if (ua_type != CTL_UA_NONE) { 11612 mtx_unlock(&lun->lun_lock); 11613 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11614 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11615 ctsio->sense_len = SSD_FULL_SIZE; 11616 ctl_done((union ctl_io *)ctsio); 11617 return (retval); 11618 } 11619 } 11620 11621 11622 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11623 mtx_unlock(&lun->lun_lock); 11624 ctl_done((union ctl_io *)ctsio); 11625 return (retval); 11626 } 11627 11628 /* 11629 * XXX CHD this is where we want to send IO to other side if 11630 * this LUN is secondary on this SC. We will need to make a copy 11631 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11632 * the copy we send as FROM_OTHER. 11633 * We also need to stuff the address of the original IO so we can 11634 * find it easily. Something similar will need be done on the other 11635 * side so when we are done we can find the copy. 11636 */ 11637 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11638 union ctl_ha_msg msg_info; 11639 int isc_retval; 11640 11641 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11642 11643 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11644 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11645#if 0 11646 printf("1. ctsio %p\n", ctsio); 11647#endif 11648 msg_info.hdr.serializing_sc = NULL; 11649 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11650 msg_info.scsi.tag_num = ctsio->tag_num; 11651 msg_info.scsi.tag_type = ctsio->tag_type; 11652 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11653 11654 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11655 11656 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11657 (void *)&msg_info, sizeof(msg_info), 0)) > 11658 CTL_HA_STATUS_SUCCESS) { 11659 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11660 isc_retval); 11661 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11662 } else { 11663#if 0 11664 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11665#endif 11666 } 11667 11668 /* 11669 * XXX KDM this I/O is off the incoming queue, but hasn't 11670 * been inserted on any other queue. We may need to come 11671 * up with a holding queue while we wait for serialization 11672 * so that we have an idea of what we're waiting for from 11673 * the other side. 11674 */ 11675 mtx_unlock(&lun->lun_lock); 11676 return (retval); 11677 } 11678 11679 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11680 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11681 ctl_ooaq, ooa_links))) { 11682 case CTL_ACTION_BLOCK: 11683 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11684 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11685 blocked_links); 11686 mtx_unlock(&lun->lun_lock); 11687 return (retval); 11688 case CTL_ACTION_PASS: 11689 case CTL_ACTION_SKIP: 11690 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11691 mtx_unlock(&lun->lun_lock); 11692 ctl_enqueue_rtr((union ctl_io *)ctsio); 11693 break; 11694 case CTL_ACTION_OVERLAP: 11695 mtx_unlock(&lun->lun_lock); 11696 ctl_set_overlapped_cmd(ctsio); 11697 ctl_done((union ctl_io *)ctsio); 11698 break; 11699 case CTL_ACTION_OVERLAP_TAG: 11700 mtx_unlock(&lun->lun_lock); 11701 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11702 ctl_done((union ctl_io *)ctsio); 11703 break; 11704 case CTL_ACTION_ERROR: 11705 default: 11706 mtx_unlock(&lun->lun_lock); 11707 ctl_set_internal_failure(ctsio, 11708 /*sks_valid*/ 0, 11709 /*retry_count*/ 0); 11710 ctl_done((union ctl_io *)ctsio); 11711 break; 11712 } 11713 return (retval); 11714} 11715 11716const struct ctl_cmd_entry * 11717ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11718{ 11719 const struct ctl_cmd_entry *entry; 11720 int service_action; 11721 11722 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11723 if (sa) 11724 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11725 if (entry->flags & CTL_CMD_FLAG_SA5) { 11726 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11727 entry = &((const struct ctl_cmd_entry *) 11728 entry->execute)[service_action]; 11729 } 11730 return (entry); 11731} 11732 11733const struct ctl_cmd_entry * 11734ctl_validate_command(struct ctl_scsiio *ctsio) 11735{ 11736 const struct ctl_cmd_entry *entry; 11737 int i, sa; 11738 uint8_t diff; 11739 11740 entry = ctl_get_cmd_entry(ctsio, &sa); 11741 if (entry->execute == NULL) { 11742 if (sa) 11743 ctl_set_invalid_field(ctsio, 11744 /*sks_valid*/ 1, 11745 /*command*/ 1, 11746 /*field*/ 1, 11747 /*bit_valid*/ 1, 11748 /*bit*/ 4); 11749 else 11750 ctl_set_invalid_opcode(ctsio); 11751 ctl_done((union ctl_io *)ctsio); 11752 return (NULL); 11753 } 11754 KASSERT(entry->length > 0, 11755 ("Not defined length for command 0x%02x/0x%02x", 11756 ctsio->cdb[0], ctsio->cdb[1])); 11757 for (i = 1; i < entry->length; i++) { 11758 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11759 if (diff == 0) 11760 continue; 11761 ctl_set_invalid_field(ctsio, 11762 /*sks_valid*/ 1, 11763 /*command*/ 1, 11764 /*field*/ i, 11765 /*bit_valid*/ 1, 11766 /*bit*/ fls(diff) - 1); 11767 ctl_done((union ctl_io *)ctsio); 11768 return (NULL); 11769 } 11770 return (entry); 11771} 11772 11773static int 11774ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11775{ 11776 11777 switch (lun_type) { 11778 case T_PROCESSOR: 11779 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11780 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11781 return (0); 11782 break; 11783 case T_DIRECT: 11784 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11785 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11786 return (0); 11787 break; 11788 default: 11789 return (0); 11790 } 11791 return (1); 11792} 11793 11794static int 11795ctl_scsiio(struct ctl_scsiio *ctsio) 11796{ 11797 int retval; 11798 const struct ctl_cmd_entry *entry; 11799 11800 retval = CTL_RETVAL_COMPLETE; 11801 11802 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11803 11804 entry = ctl_get_cmd_entry(ctsio, NULL); 11805 11806 /* 11807 * If this I/O has been aborted, just send it straight to 11808 * ctl_done() without executing it. 11809 */ 11810 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11811 ctl_done((union ctl_io *)ctsio); 11812 goto bailout; 11813 } 11814 11815 /* 11816 * All the checks should have been handled by ctl_scsiio_precheck(). 11817 * We should be clear now to just execute the I/O. 11818 */ 11819 retval = entry->execute(ctsio); 11820 11821bailout: 11822 return (retval); 11823} 11824 11825/* 11826 * Since we only implement one target right now, a bus reset simply resets 11827 * our single target. 11828 */ 11829static int 11830ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11831{ 11832 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11833} 11834 11835static int 11836ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11837 ctl_ua_type ua_type) 11838{ 11839 struct ctl_lun *lun; 11840 int retval; 11841 11842 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11843 union ctl_ha_msg msg_info; 11844 11845 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11846 msg_info.hdr.nexus = io->io_hdr.nexus; 11847 if (ua_type==CTL_UA_TARG_RESET) 11848 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11849 else 11850 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11851 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11852 msg_info.hdr.original_sc = NULL; 11853 msg_info.hdr.serializing_sc = NULL; 11854 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11855 (void *)&msg_info, sizeof(msg_info), 0)) { 11856 } 11857 } 11858 retval = 0; 11859 11860 mtx_lock(&ctl_softc->ctl_lock); 11861 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11862 retval += ctl_lun_reset(lun, io, ua_type); 11863 mtx_unlock(&ctl_softc->ctl_lock); 11864 11865 return (retval); 11866} 11867 11868/* 11869 * The LUN should always be set. The I/O is optional, and is used to 11870 * distinguish between I/Os sent by this initiator, and by other 11871 * initiators. We set unit attention for initiators other than this one. 11872 * SAM-3 is vague on this point. It does say that a unit attention should 11873 * be established for other initiators when a LUN is reset (see section 11874 * 5.7.3), but it doesn't specifically say that the unit attention should 11875 * be established for this particular initiator when a LUN is reset. Here 11876 * is the relevant text, from SAM-3 rev 8: 11877 * 11878 * 5.7.2 When a SCSI initiator port aborts its own tasks 11879 * 11880 * When a SCSI initiator port causes its own task(s) to be aborted, no 11881 * notification that the task(s) have been aborted shall be returned to 11882 * the SCSI initiator port other than the completion response for the 11883 * command or task management function action that caused the task(s) to 11884 * be aborted and notification(s) associated with related effects of the 11885 * action (e.g., a reset unit attention condition). 11886 * 11887 * XXX KDM for now, we're setting unit attention for all initiators. 11888 */ 11889static int 11890ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11891{ 11892 union ctl_io *xio; 11893#if 0 11894 uint32_t initidx; 11895#endif 11896#ifdef CTL_WITH_CA 11897 int i; 11898#endif 11899 11900 mtx_lock(&lun->lun_lock); 11901 /* 11902 * Run through the OOA queue and abort each I/O. 11903 */ 11904#if 0 11905 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11906#endif 11907 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11908 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11909 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11910 } 11911 11912 /* 11913 * This version sets unit attention for every 11914 */ 11915#if 0 11916 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11917 ctl_est_ua_all(lun, initidx, ua_type); 11918#else 11919 ctl_est_ua_all(lun, -1, ua_type); 11920#endif 11921 11922 /* 11923 * A reset (any kind, really) clears reservations established with 11924 * RESERVE/RELEASE. It does not clear reservations established 11925 * with PERSISTENT RESERVE OUT, but we don't support that at the 11926 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11927 * reservations made with the RESERVE/RELEASE commands, because 11928 * those commands are obsolete in SPC-3. 11929 */ 11930 lun->flags &= ~CTL_LUN_RESERVED; 11931 11932#ifdef CTL_WITH_CA 11933 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11934 ctl_clear_mask(lun->have_ca, i); 11935#endif 11936 mtx_unlock(&lun->lun_lock); 11937 11938 return (0); 11939} 11940 11941static void 11942ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11943 int other_sc) 11944{ 11945 union ctl_io *xio; 11946 11947 mtx_assert(&lun->lun_lock, MA_OWNED); 11948 11949 /* 11950 * Run through the OOA queue and attempt to find the given I/O. 11951 * The target port, initiator ID, tag type and tag number have to 11952 * match the values that we got from the initiator. If we have an 11953 * untagged command to abort, simply abort the first untagged command 11954 * we come to. We only allow one untagged command at a time of course. 11955 */ 11956 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11957 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11958 11959 if ((targ_port == UINT32_MAX || 11960 targ_port == xio->io_hdr.nexus.targ_port) && 11961 (init_id == UINT32_MAX || 11962 init_id == xio->io_hdr.nexus.initid.id)) { 11963 if (targ_port != xio->io_hdr.nexus.targ_port || 11964 init_id != xio->io_hdr.nexus.initid.id) 11965 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11966 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11967 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11968 union ctl_ha_msg msg_info; 11969 11970 msg_info.hdr.nexus = xio->io_hdr.nexus; 11971 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11972 msg_info.task.tag_num = xio->scsiio.tag_num; 11973 msg_info.task.tag_type = xio->scsiio.tag_type; 11974 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11975 msg_info.hdr.original_sc = NULL; 11976 msg_info.hdr.serializing_sc = NULL; 11977 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11978 (void *)&msg_info, sizeof(msg_info), 0); 11979 } 11980 } 11981 } 11982} 11983 11984static int 11985ctl_abort_task_set(union ctl_io *io) 11986{ 11987 struct ctl_softc *softc = control_softc; 11988 struct ctl_lun *lun; 11989 uint32_t targ_lun; 11990 11991 /* 11992 * Look up the LUN. 11993 */ 11994 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11995 mtx_lock(&softc->ctl_lock); 11996 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11997 lun = softc->ctl_luns[targ_lun]; 11998 else { 11999 mtx_unlock(&softc->ctl_lock); 12000 return (1); 12001 } 12002 12003 mtx_lock(&lun->lun_lock); 12004 mtx_unlock(&softc->ctl_lock); 12005 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12006 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12007 io->io_hdr.nexus.initid.id, 12008 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12009 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12010 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12011 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12012 } 12013 mtx_unlock(&lun->lun_lock); 12014 return (0); 12015} 12016 12017static int 12018ctl_i_t_nexus_reset(union ctl_io *io) 12019{ 12020 struct ctl_softc *softc = control_softc; 12021 struct ctl_lun *lun; 12022 uint32_t initidx, residx; 12023 12024 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12025 residx = ctl_get_resindex(&io->io_hdr.nexus); 12026 mtx_lock(&softc->ctl_lock); 12027 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12028 mtx_lock(&lun->lun_lock); 12029 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12030 io->io_hdr.nexus.initid.id, 12031 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12032#ifdef CTL_WITH_CA 12033 ctl_clear_mask(lun->have_ca, initidx); 12034#endif 12035 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12036 lun->flags &= ~CTL_LUN_RESERVED; 12037 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12038 mtx_unlock(&lun->lun_lock); 12039 } 12040 mtx_unlock(&softc->ctl_lock); 12041 return (0); 12042} 12043 12044static int 12045ctl_abort_task(union ctl_io *io) 12046{ 12047 union ctl_io *xio; 12048 struct ctl_lun *lun; 12049 struct ctl_softc *ctl_softc; 12050#if 0 12051 struct sbuf sb; 12052 char printbuf[128]; 12053#endif 12054 int found; 12055 uint32_t targ_lun; 12056 12057 ctl_softc = control_softc; 12058 found = 0; 12059 12060 /* 12061 * Look up the LUN. 12062 */ 12063 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12064 mtx_lock(&ctl_softc->ctl_lock); 12065 if ((targ_lun < CTL_MAX_LUNS) 12066 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12067 lun = ctl_softc->ctl_luns[targ_lun]; 12068 else { 12069 mtx_unlock(&ctl_softc->ctl_lock); 12070 return (1); 12071 } 12072 12073#if 0 12074 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12075 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12076#endif 12077 12078 mtx_lock(&lun->lun_lock); 12079 mtx_unlock(&ctl_softc->ctl_lock); 12080 /* 12081 * Run through the OOA queue and attempt to find the given I/O. 12082 * The target port, initiator ID, tag type and tag number have to 12083 * match the values that we got from the initiator. If we have an 12084 * untagged command to abort, simply abort the first untagged command 12085 * we come to. We only allow one untagged command at a time of course. 12086 */ 12087#if 0 12088 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12089#endif 12090 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12091 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12092#if 0 12093 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12094 12095 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12096 lun->lun, xio->scsiio.tag_num, 12097 xio->scsiio.tag_type, 12098 (xio->io_hdr.blocked_links.tqe_prev 12099 == NULL) ? "" : " BLOCKED", 12100 (xio->io_hdr.flags & 12101 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12102 (xio->io_hdr.flags & 12103 CTL_FLAG_ABORT) ? " ABORT" : "", 12104 (xio->io_hdr.flags & 12105 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12106 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12107 sbuf_finish(&sb); 12108 printf("%s\n", sbuf_data(&sb)); 12109#endif 12110 12111 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12112 && (xio->io_hdr.nexus.initid.id == 12113 io->io_hdr.nexus.initid.id)) { 12114 /* 12115 * If the abort says that the task is untagged, the 12116 * task in the queue must be untagged. Otherwise, 12117 * we just check to see whether the tag numbers 12118 * match. This is because the QLogic firmware 12119 * doesn't pass back the tag type in an abort 12120 * request. 12121 */ 12122#if 0 12123 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12124 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12125 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12126#endif 12127 /* 12128 * XXX KDM we've got problems with FC, because it 12129 * doesn't send down a tag type with aborts. So we 12130 * can only really go by the tag number... 12131 * This may cause problems with parallel SCSI. 12132 * Need to figure that out!! 12133 */ 12134 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12135 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12136 found = 1; 12137 if ((io->io_hdr.flags & 12138 CTL_FLAG_FROM_OTHER_SC) == 0 && 12139 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12140 union ctl_ha_msg msg_info; 12141 12142 io->io_hdr.flags |= 12143 CTL_FLAG_SENT_2OTHER_SC; 12144 msg_info.hdr.nexus = io->io_hdr.nexus; 12145 msg_info.task.task_action = 12146 CTL_TASK_ABORT_TASK; 12147 msg_info.task.tag_num = 12148 io->taskio.tag_num; 12149 msg_info.task.tag_type = 12150 io->taskio.tag_type; 12151 msg_info.hdr.msg_type = 12152 CTL_MSG_MANAGE_TASKS; 12153 msg_info.hdr.original_sc = NULL; 12154 msg_info.hdr.serializing_sc = NULL; 12155#if 0 12156 printf("Sent Abort to other side\n"); 12157#endif 12158 if (CTL_HA_STATUS_SUCCESS != 12159 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12160 (void *)&msg_info, 12161 sizeof(msg_info), 0)) { 12162 } 12163 } 12164#if 0 12165 printf("ctl_abort_task: found I/O to abort\n"); 12166#endif 12167 break; 12168 } 12169 } 12170 } 12171 mtx_unlock(&lun->lun_lock); 12172 12173 if (found == 0) { 12174 /* 12175 * This isn't really an error. It's entirely possible for 12176 * the abort and command completion to cross on the wire. 12177 * This is more of an informative/diagnostic error. 12178 */ 12179#if 0 12180 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12181 "%d:%d:%d:%d tag %d type %d\n", 12182 io->io_hdr.nexus.initid.id, 12183 io->io_hdr.nexus.targ_port, 12184 io->io_hdr.nexus.targ_target.id, 12185 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12186 io->taskio.tag_type); 12187#endif 12188 } 12189 return (0); 12190} 12191 12192static void 12193ctl_run_task(union ctl_io *io) 12194{ 12195 struct ctl_softc *ctl_softc = control_softc; 12196 int retval = 1; 12197 const char *task_desc; 12198 12199 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12200 12201 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12202 ("ctl_run_task: Unextected io_type %d\n", 12203 io->io_hdr.io_type)); 12204 12205 task_desc = ctl_scsi_task_string(&io->taskio); 12206 if (task_desc != NULL) { 12207#ifdef NEEDTOPORT 12208 csevent_log(CSC_CTL | CSC_SHELF_SW | 12209 CTL_TASK_REPORT, 12210 csevent_LogType_Trace, 12211 csevent_Severity_Information, 12212 csevent_AlertLevel_Green, 12213 csevent_FRU_Firmware, 12214 csevent_FRU_Unknown, 12215 "CTL: received task: %s",task_desc); 12216#endif 12217 } else { 12218#ifdef NEEDTOPORT 12219 csevent_log(CSC_CTL | CSC_SHELF_SW | 12220 CTL_TASK_REPORT, 12221 csevent_LogType_Trace, 12222 csevent_Severity_Information, 12223 csevent_AlertLevel_Green, 12224 csevent_FRU_Firmware, 12225 csevent_FRU_Unknown, 12226 "CTL: received unknown task " 12227 "type: %d (%#x)", 12228 io->taskio.task_action, 12229 io->taskio.task_action); 12230#endif 12231 } 12232 switch (io->taskio.task_action) { 12233 case CTL_TASK_ABORT_TASK: 12234 retval = ctl_abort_task(io); 12235 break; 12236 case CTL_TASK_ABORT_TASK_SET: 12237 case CTL_TASK_CLEAR_TASK_SET: 12238 retval = ctl_abort_task_set(io); 12239 break; 12240 case CTL_TASK_CLEAR_ACA: 12241 break; 12242 case CTL_TASK_I_T_NEXUS_RESET: 12243 retval = ctl_i_t_nexus_reset(io); 12244 break; 12245 case CTL_TASK_LUN_RESET: { 12246 struct ctl_lun *lun; 12247 uint32_t targ_lun; 12248 12249 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12250 mtx_lock(&ctl_softc->ctl_lock); 12251 if ((targ_lun < CTL_MAX_LUNS) 12252 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12253 lun = ctl_softc->ctl_luns[targ_lun]; 12254 else { 12255 mtx_unlock(&ctl_softc->ctl_lock); 12256 retval = 1; 12257 break; 12258 } 12259 12260 if (!(io->io_hdr.flags & 12261 CTL_FLAG_FROM_OTHER_SC)) { 12262 union ctl_ha_msg msg_info; 12263 12264 io->io_hdr.flags |= 12265 CTL_FLAG_SENT_2OTHER_SC; 12266 msg_info.hdr.msg_type = 12267 CTL_MSG_MANAGE_TASKS; 12268 msg_info.hdr.nexus = io->io_hdr.nexus; 12269 msg_info.task.task_action = 12270 CTL_TASK_LUN_RESET; 12271 msg_info.hdr.original_sc = NULL; 12272 msg_info.hdr.serializing_sc = NULL; 12273 if (CTL_HA_STATUS_SUCCESS != 12274 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12275 (void *)&msg_info, 12276 sizeof(msg_info), 0)) { 12277 } 12278 } 12279 12280 retval = ctl_lun_reset(lun, io, 12281 CTL_UA_LUN_RESET); 12282 mtx_unlock(&ctl_softc->ctl_lock); 12283 break; 12284 } 12285 case CTL_TASK_TARGET_RESET: 12286 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12287 break; 12288 case CTL_TASK_BUS_RESET: 12289 retval = ctl_bus_reset(ctl_softc, io); 12290 break; 12291 case CTL_TASK_PORT_LOGIN: 12292 break; 12293 case CTL_TASK_PORT_LOGOUT: 12294 break; 12295 default: 12296 printf("ctl_run_task: got unknown task management event %d\n", 12297 io->taskio.task_action); 12298 break; 12299 } 12300 if (retval == 0) 12301 io->io_hdr.status = CTL_SUCCESS; 12302 else 12303 io->io_hdr.status = CTL_ERROR; 12304 ctl_done(io); 12305} 12306 12307/* 12308 * For HA operation. Handle commands that come in from the other 12309 * controller. 12310 */ 12311static void 12312ctl_handle_isc(union ctl_io *io) 12313{ 12314 int free_io; 12315 struct ctl_lun *lun; 12316 struct ctl_softc *ctl_softc; 12317 uint32_t targ_lun; 12318 12319 ctl_softc = control_softc; 12320 12321 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12322 lun = ctl_softc->ctl_luns[targ_lun]; 12323 12324 switch (io->io_hdr.msg_type) { 12325 case CTL_MSG_SERIALIZE: 12326 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12327 break; 12328 case CTL_MSG_R2R: { 12329 const struct ctl_cmd_entry *entry; 12330 12331 /* 12332 * This is only used in SER_ONLY mode. 12333 */ 12334 free_io = 0; 12335 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12336 mtx_lock(&lun->lun_lock); 12337 if (ctl_scsiio_lun_check(ctl_softc, lun, 12338 entry, (struct ctl_scsiio *)io) != 0) { 12339 mtx_unlock(&lun->lun_lock); 12340 ctl_done(io); 12341 break; 12342 } 12343 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12344 mtx_unlock(&lun->lun_lock); 12345 ctl_enqueue_rtr(io); 12346 break; 12347 } 12348 case CTL_MSG_FINISH_IO: 12349 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12350 free_io = 0; 12351 ctl_done(io); 12352 } else { 12353 free_io = 1; 12354 mtx_lock(&lun->lun_lock); 12355 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12356 ooa_links); 12357 ctl_check_blocked(lun); 12358 mtx_unlock(&lun->lun_lock); 12359 } 12360 break; 12361 case CTL_MSG_PERS_ACTION: 12362 ctl_hndl_per_res_out_on_other_sc( 12363 (union ctl_ha_msg *)&io->presio.pr_msg); 12364 free_io = 1; 12365 break; 12366 case CTL_MSG_BAD_JUJU: 12367 free_io = 0; 12368 ctl_done(io); 12369 break; 12370 case CTL_MSG_DATAMOVE: 12371 /* Only used in XFER mode */ 12372 free_io = 0; 12373 ctl_datamove_remote(io); 12374 break; 12375 case CTL_MSG_DATAMOVE_DONE: 12376 /* Only used in XFER mode */ 12377 free_io = 0; 12378 io->scsiio.be_move_done(io); 12379 break; 12380 default: 12381 free_io = 1; 12382 printf("%s: Invalid message type %d\n", 12383 __func__, io->io_hdr.msg_type); 12384 break; 12385 } 12386 if (free_io) 12387 ctl_free_io(io); 12388 12389} 12390 12391 12392/* 12393 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12394 * there is no match. 12395 */ 12396static ctl_lun_error_pattern 12397ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12398{ 12399 const struct ctl_cmd_entry *entry; 12400 ctl_lun_error_pattern filtered_pattern, pattern; 12401 12402 pattern = desc->error_pattern; 12403 12404 /* 12405 * XXX KDM we need more data passed into this function to match a 12406 * custom pattern, and we actually need to implement custom pattern 12407 * matching. 12408 */ 12409 if (pattern & CTL_LUN_PAT_CMD) 12410 return (CTL_LUN_PAT_CMD); 12411 12412 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12413 return (CTL_LUN_PAT_ANY); 12414 12415 entry = ctl_get_cmd_entry(ctsio, NULL); 12416 12417 filtered_pattern = entry->pattern & pattern; 12418 12419 /* 12420 * If the user requested specific flags in the pattern (e.g. 12421 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12422 * flags. 12423 * 12424 * If the user did not specify any flags, it doesn't matter whether 12425 * or not the command supports the flags. 12426 */ 12427 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12428 (pattern & ~CTL_LUN_PAT_MASK)) 12429 return (CTL_LUN_PAT_NONE); 12430 12431 /* 12432 * If the user asked for a range check, see if the requested LBA 12433 * range overlaps with this command's LBA range. 12434 */ 12435 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12436 uint64_t lba1; 12437 uint64_t len1; 12438 ctl_action action; 12439 int retval; 12440 12441 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12442 if (retval != 0) 12443 return (CTL_LUN_PAT_NONE); 12444 12445 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12446 desc->lba_range.len); 12447 /* 12448 * A "pass" means that the LBA ranges don't overlap, so 12449 * this doesn't match the user's range criteria. 12450 */ 12451 if (action == CTL_ACTION_PASS) 12452 return (CTL_LUN_PAT_NONE); 12453 } 12454 12455 return (filtered_pattern); 12456} 12457 12458static void 12459ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12460{ 12461 struct ctl_error_desc *desc, *desc2; 12462 12463 mtx_assert(&lun->lun_lock, MA_OWNED); 12464 12465 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12466 ctl_lun_error_pattern pattern; 12467 /* 12468 * Check to see whether this particular command matches 12469 * the pattern in the descriptor. 12470 */ 12471 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12472 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12473 continue; 12474 12475 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12476 case CTL_LUN_INJ_ABORTED: 12477 ctl_set_aborted(&io->scsiio); 12478 break; 12479 case CTL_LUN_INJ_MEDIUM_ERR: 12480 ctl_set_medium_error(&io->scsiio); 12481 break; 12482 case CTL_LUN_INJ_UA: 12483 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12484 * OCCURRED */ 12485 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12486 break; 12487 case CTL_LUN_INJ_CUSTOM: 12488 /* 12489 * We're assuming the user knows what he is doing. 12490 * Just copy the sense information without doing 12491 * checks. 12492 */ 12493 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12494 ctl_min(sizeof(desc->custom_sense), 12495 sizeof(io->scsiio.sense_data))); 12496 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12497 io->scsiio.sense_len = SSD_FULL_SIZE; 12498 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12499 break; 12500 case CTL_LUN_INJ_NONE: 12501 default: 12502 /* 12503 * If this is an error injection type we don't know 12504 * about, clear the continuous flag (if it is set) 12505 * so it will get deleted below. 12506 */ 12507 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12508 break; 12509 } 12510 /* 12511 * By default, each error injection action is a one-shot 12512 */ 12513 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12514 continue; 12515 12516 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12517 12518 free(desc, M_CTL); 12519 } 12520} 12521 12522#ifdef CTL_IO_DELAY 12523static void 12524ctl_datamove_timer_wakeup(void *arg) 12525{ 12526 union ctl_io *io; 12527 12528 io = (union ctl_io *)arg; 12529 12530 ctl_datamove(io); 12531} 12532#endif /* CTL_IO_DELAY */ 12533 12534void 12535ctl_datamove(union ctl_io *io) 12536{ 12537 void (*fe_datamove)(union ctl_io *io); 12538 12539 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12540 12541 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12542 12543#ifdef CTL_TIME_IO 12544 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12545 char str[256]; 12546 char path_str[64]; 12547 struct sbuf sb; 12548 12549 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12550 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12551 12552 sbuf_cat(&sb, path_str); 12553 switch (io->io_hdr.io_type) { 12554 case CTL_IO_SCSI: 12555 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12556 sbuf_printf(&sb, "\n"); 12557 sbuf_cat(&sb, path_str); 12558 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12559 io->scsiio.tag_num, io->scsiio.tag_type); 12560 break; 12561 case CTL_IO_TASK: 12562 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12563 "Tag Type: %d\n", io->taskio.task_action, 12564 io->taskio.tag_num, io->taskio.tag_type); 12565 break; 12566 default: 12567 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12568 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12569 break; 12570 } 12571 sbuf_cat(&sb, path_str); 12572 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12573 (intmax_t)time_uptime - io->io_hdr.start_time); 12574 sbuf_finish(&sb); 12575 printf("%s", sbuf_data(&sb)); 12576 } 12577#endif /* CTL_TIME_IO */ 12578 12579#ifdef CTL_IO_DELAY 12580 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12581 struct ctl_lun *lun; 12582 12583 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12584 12585 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12586 } else { 12587 struct ctl_lun *lun; 12588 12589 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12590 if ((lun != NULL) 12591 && (lun->delay_info.datamove_delay > 0)) { 12592 struct callout *callout; 12593 12594 callout = (struct callout *)&io->io_hdr.timer_bytes; 12595 callout_init(callout, /*mpsafe*/ 1); 12596 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12597 callout_reset(callout, 12598 lun->delay_info.datamove_delay * hz, 12599 ctl_datamove_timer_wakeup, io); 12600 if (lun->delay_info.datamove_type == 12601 CTL_DELAY_TYPE_ONESHOT) 12602 lun->delay_info.datamove_delay = 0; 12603 return; 12604 } 12605 } 12606#endif 12607 12608 /* 12609 * This command has been aborted. Set the port status, so we fail 12610 * the data move. 12611 */ 12612 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12613 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12614 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12615 io->io_hdr.nexus.targ_port, 12616 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12617 io->io_hdr.nexus.targ_lun); 12618 io->io_hdr.port_status = 31337; 12619 /* 12620 * Note that the backend, in this case, will get the 12621 * callback in its context. In other cases it may get 12622 * called in the frontend's interrupt thread context. 12623 */ 12624 io->scsiio.be_move_done(io); 12625 return; 12626 } 12627 12628 /* Don't confuse frontend with zero length data move. */ 12629 if (io->scsiio.kern_data_len == 0) { 12630 io->scsiio.be_move_done(io); 12631 return; 12632 } 12633 12634 /* 12635 * If we're in XFER mode and this I/O is from the other shelf 12636 * controller, we need to send the DMA to the other side to 12637 * actually transfer the data to/from the host. In serialize only 12638 * mode the transfer happens below CTL and ctl_datamove() is only 12639 * called on the machine that originally received the I/O. 12640 */ 12641 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12642 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12643 union ctl_ha_msg msg; 12644 uint32_t sg_entries_sent; 12645 int do_sg_copy; 12646 int i; 12647 12648 memset(&msg, 0, sizeof(msg)); 12649 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12650 msg.hdr.original_sc = io->io_hdr.original_sc; 12651 msg.hdr.serializing_sc = io; 12652 msg.hdr.nexus = io->io_hdr.nexus; 12653 msg.dt.flags = io->io_hdr.flags; 12654 /* 12655 * We convert everything into a S/G list here. We can't 12656 * pass by reference, only by value between controllers. 12657 * So we can't pass a pointer to the S/G list, only as many 12658 * S/G entries as we can fit in here. If it's possible for 12659 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12660 * then we need to break this up into multiple transfers. 12661 */ 12662 if (io->scsiio.kern_sg_entries == 0) { 12663 msg.dt.kern_sg_entries = 1; 12664 /* 12665 * If this is in cached memory, flush the cache 12666 * before we send the DMA request to the other 12667 * controller. We want to do this in either the 12668 * read or the write case. The read case is 12669 * straightforward. In the write case, we want to 12670 * make sure nothing is in the local cache that 12671 * could overwrite the DMAed data. 12672 */ 12673 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12674 /* 12675 * XXX KDM use bus_dmamap_sync() here. 12676 */ 12677 } 12678 12679 /* 12680 * Convert to a physical address if this is a 12681 * virtual address. 12682 */ 12683 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12684 msg.dt.sg_list[0].addr = 12685 io->scsiio.kern_data_ptr; 12686 } else { 12687 /* 12688 * XXX KDM use busdma here! 12689 */ 12690#if 0 12691 msg.dt.sg_list[0].addr = (void *) 12692 vtophys(io->scsiio.kern_data_ptr); 12693#endif 12694 } 12695 12696 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12697 do_sg_copy = 0; 12698 } else { 12699 struct ctl_sg_entry *sgl; 12700 12701 do_sg_copy = 1; 12702 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12703 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12704 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12705 /* 12706 * XXX KDM use bus_dmamap_sync() here. 12707 */ 12708 } 12709 } 12710 12711 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12712 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12713 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12714 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12715 msg.dt.sg_sequence = 0; 12716 12717 /* 12718 * Loop until we've sent all of the S/G entries. On the 12719 * other end, we'll recompose these S/G entries into one 12720 * contiguous list before passing it to the 12721 */ 12722 for (sg_entries_sent = 0; sg_entries_sent < 12723 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12724 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12725 sizeof(msg.dt.sg_list[0])), 12726 msg.dt.kern_sg_entries - sg_entries_sent); 12727 12728 if (do_sg_copy != 0) { 12729 struct ctl_sg_entry *sgl; 12730 int j; 12731 12732 sgl = (struct ctl_sg_entry *) 12733 io->scsiio.kern_data_ptr; 12734 /* 12735 * If this is in cached memory, flush the cache 12736 * before we send the DMA request to the other 12737 * controller. We want to do this in either 12738 * the * read or the write case. The read 12739 * case is straightforward. In the write 12740 * case, we want to make sure nothing is 12741 * in the local cache that could overwrite 12742 * the DMAed data. 12743 */ 12744 12745 for (i = sg_entries_sent, j = 0; 12746 i < msg.dt.cur_sg_entries; i++, j++) { 12747 if ((io->io_hdr.flags & 12748 CTL_FLAG_NO_DATASYNC) == 0) { 12749 /* 12750 * XXX KDM use bus_dmamap_sync() 12751 */ 12752 } 12753 if ((io->io_hdr.flags & 12754 CTL_FLAG_BUS_ADDR) == 0) { 12755 /* 12756 * XXX KDM use busdma. 12757 */ 12758#if 0 12759 msg.dt.sg_list[j].addr =(void *) 12760 vtophys(sgl[i].addr); 12761#endif 12762 } else { 12763 msg.dt.sg_list[j].addr = 12764 sgl[i].addr; 12765 } 12766 msg.dt.sg_list[j].len = sgl[i].len; 12767 } 12768 } 12769 12770 sg_entries_sent += msg.dt.cur_sg_entries; 12771 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12772 msg.dt.sg_last = 1; 12773 else 12774 msg.dt.sg_last = 0; 12775 12776 /* 12777 * XXX KDM drop and reacquire the lock here? 12778 */ 12779 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12780 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12781 /* 12782 * XXX do something here. 12783 */ 12784 } 12785 12786 msg.dt.sent_sg_entries = sg_entries_sent; 12787 } 12788 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12789 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12790 ctl_failover_io(io, /*have_lock*/ 0); 12791 12792 } else { 12793 12794 /* 12795 * Lookup the fe_datamove() function for this particular 12796 * front end. 12797 */ 12798 fe_datamove = 12799 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12800 12801 fe_datamove(io); 12802 } 12803} 12804 12805static void 12806ctl_send_datamove_done(union ctl_io *io, int have_lock) 12807{ 12808 union ctl_ha_msg msg; 12809 int isc_status; 12810 12811 memset(&msg, 0, sizeof(msg)); 12812 12813 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12814 msg.hdr.original_sc = io; 12815 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12816 msg.hdr.nexus = io->io_hdr.nexus; 12817 msg.hdr.status = io->io_hdr.status; 12818 msg.scsi.tag_num = io->scsiio.tag_num; 12819 msg.scsi.tag_type = io->scsiio.tag_type; 12820 msg.scsi.scsi_status = io->scsiio.scsi_status; 12821 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12822 sizeof(io->scsiio.sense_data)); 12823 msg.scsi.sense_len = io->scsiio.sense_len; 12824 msg.scsi.sense_residual = io->scsiio.sense_residual; 12825 msg.scsi.fetd_status = io->io_hdr.port_status; 12826 msg.scsi.residual = io->scsiio.residual; 12827 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12828 12829 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12830 ctl_failover_io(io, /*have_lock*/ have_lock); 12831 return; 12832 } 12833 12834 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12835 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12836 /* XXX do something if this fails */ 12837 } 12838 12839} 12840 12841/* 12842 * The DMA to the remote side is done, now we need to tell the other side 12843 * we're done so it can continue with its data movement. 12844 */ 12845static void 12846ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12847{ 12848 union ctl_io *io; 12849 12850 io = rq->context; 12851 12852 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12853 printf("%s: ISC DMA write failed with error %d", __func__, 12854 rq->ret); 12855 ctl_set_internal_failure(&io->scsiio, 12856 /*sks_valid*/ 1, 12857 /*retry_count*/ rq->ret); 12858 } 12859 12860 ctl_dt_req_free(rq); 12861 12862 /* 12863 * In this case, we had to malloc the memory locally. Free it. 12864 */ 12865 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12866 int i; 12867 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12868 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12869 } 12870 /* 12871 * The data is in local and remote memory, so now we need to send 12872 * status (good or back) back to the other side. 12873 */ 12874 ctl_send_datamove_done(io, /*have_lock*/ 0); 12875} 12876 12877/* 12878 * We've moved the data from the host/controller into local memory. Now we 12879 * need to push it over to the remote controller's memory. 12880 */ 12881static int 12882ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12883{ 12884 int retval; 12885 12886 retval = 0; 12887 12888 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12889 ctl_datamove_remote_write_cb); 12890 12891 return (retval); 12892} 12893 12894static void 12895ctl_datamove_remote_write(union ctl_io *io) 12896{ 12897 int retval; 12898 void (*fe_datamove)(union ctl_io *io); 12899 12900 /* 12901 * - Get the data from the host/HBA into local memory. 12902 * - DMA memory from the local controller to the remote controller. 12903 * - Send status back to the remote controller. 12904 */ 12905 12906 retval = ctl_datamove_remote_sgl_setup(io); 12907 if (retval != 0) 12908 return; 12909 12910 /* Switch the pointer over so the FETD knows what to do */ 12911 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12912 12913 /* 12914 * Use a custom move done callback, since we need to send completion 12915 * back to the other controller, not to the backend on this side. 12916 */ 12917 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12918 12919 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12920 12921 fe_datamove(io); 12922 12923 return; 12924 12925} 12926 12927static int 12928ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12929{ 12930#if 0 12931 char str[256]; 12932 char path_str[64]; 12933 struct sbuf sb; 12934#endif 12935 12936 /* 12937 * In this case, we had to malloc the memory locally. Free it. 12938 */ 12939 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12940 int i; 12941 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12942 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12943 } 12944 12945#if 0 12946 scsi_path_string(io, path_str, sizeof(path_str)); 12947 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12948 sbuf_cat(&sb, path_str); 12949 scsi_command_string(&io->scsiio, NULL, &sb); 12950 sbuf_printf(&sb, "\n"); 12951 sbuf_cat(&sb, path_str); 12952 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12953 io->scsiio.tag_num, io->scsiio.tag_type); 12954 sbuf_cat(&sb, path_str); 12955 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12956 io->io_hdr.flags, io->io_hdr.status); 12957 sbuf_finish(&sb); 12958 printk("%s", sbuf_data(&sb)); 12959#endif 12960 12961 12962 /* 12963 * The read is done, now we need to send status (good or bad) back 12964 * to the other side. 12965 */ 12966 ctl_send_datamove_done(io, /*have_lock*/ 0); 12967 12968 return (0); 12969} 12970 12971static void 12972ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12973{ 12974 union ctl_io *io; 12975 void (*fe_datamove)(union ctl_io *io); 12976 12977 io = rq->context; 12978 12979 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12980 printf("%s: ISC DMA read failed with error %d", __func__, 12981 rq->ret); 12982 ctl_set_internal_failure(&io->scsiio, 12983 /*sks_valid*/ 1, 12984 /*retry_count*/ rq->ret); 12985 } 12986 12987 ctl_dt_req_free(rq); 12988 12989 /* Switch the pointer over so the FETD knows what to do */ 12990 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12991 12992 /* 12993 * Use a custom move done callback, since we need to send completion 12994 * back to the other controller, not to the backend on this side. 12995 */ 12996 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12997 12998 /* XXX KDM add checks like the ones in ctl_datamove? */ 12999 13000 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13001 13002 fe_datamove(io); 13003} 13004 13005static int 13006ctl_datamove_remote_sgl_setup(union ctl_io *io) 13007{ 13008 struct ctl_sg_entry *local_sglist, *remote_sglist; 13009 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13010 struct ctl_softc *softc; 13011 int retval; 13012 int i; 13013 13014 retval = 0; 13015 softc = control_softc; 13016 13017 local_sglist = io->io_hdr.local_sglist; 13018 local_dma_sglist = io->io_hdr.local_dma_sglist; 13019 remote_sglist = io->io_hdr.remote_sglist; 13020 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13021 13022 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13023 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13024 local_sglist[i].len = remote_sglist[i].len; 13025 13026 /* 13027 * XXX Detect the situation where the RS-level I/O 13028 * redirector on the other side has already read the 13029 * data off of the AOR RS on this side, and 13030 * transferred it to remote (mirror) memory on the 13031 * other side. Since we already have the data in 13032 * memory here, we just need to use it. 13033 * 13034 * XXX KDM this can probably be removed once we 13035 * get the cache device code in and take the 13036 * current AOR implementation out. 13037 */ 13038#ifdef NEEDTOPORT 13039 if ((remote_sglist[i].addr >= 13040 (void *)vtophys(softc->mirr->addr)) 13041 && (remote_sglist[i].addr < 13042 ((void *)vtophys(softc->mirr->addr) + 13043 CacheMirrorOffset))) { 13044 local_sglist[i].addr = remote_sglist[i].addr - 13045 CacheMirrorOffset; 13046 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13047 CTL_FLAG_DATA_IN) 13048 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13049 } else { 13050 local_sglist[i].addr = remote_sglist[i].addr + 13051 CacheMirrorOffset; 13052 } 13053#endif 13054#if 0 13055 printf("%s: local %p, remote %p, len %d\n", 13056 __func__, local_sglist[i].addr, 13057 remote_sglist[i].addr, local_sglist[i].len); 13058#endif 13059 } 13060 } else { 13061 uint32_t len_to_go; 13062 13063 /* 13064 * In this case, we don't have automatically allocated 13065 * memory for this I/O on this controller. This typically 13066 * happens with internal CTL I/O -- e.g. inquiry, mode 13067 * sense, etc. Anything coming from RAIDCore will have 13068 * a mirror area available. 13069 */ 13070 len_to_go = io->scsiio.kern_data_len; 13071 13072 /* 13073 * Clear the no datasync flag, we have to use malloced 13074 * buffers. 13075 */ 13076 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13077 13078 /* 13079 * The difficult thing here is that the size of the various 13080 * S/G segments may be different than the size from the 13081 * remote controller. That'll make it harder when DMAing 13082 * the data back to the other side. 13083 */ 13084 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13085 sizeof(io->io_hdr.remote_sglist[0])) && 13086 (len_to_go > 0); i++) { 13087 local_sglist[i].len = ctl_min(len_to_go, 131072); 13088 CTL_SIZE_8B(local_dma_sglist[i].len, 13089 local_sglist[i].len); 13090 local_sglist[i].addr = 13091 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13092 13093 local_dma_sglist[i].addr = local_sglist[i].addr; 13094 13095 if (local_sglist[i].addr == NULL) { 13096 int j; 13097 13098 printf("malloc failed for %zd bytes!", 13099 local_dma_sglist[i].len); 13100 for (j = 0; j < i; j++) { 13101 free(local_sglist[j].addr, M_CTL); 13102 } 13103 ctl_set_internal_failure(&io->scsiio, 13104 /*sks_valid*/ 1, 13105 /*retry_count*/ 4857); 13106 retval = 1; 13107 goto bailout_error; 13108 13109 } 13110 /* XXX KDM do we need a sync here? */ 13111 13112 len_to_go -= local_sglist[i].len; 13113 } 13114 /* 13115 * Reset the number of S/G entries accordingly. The 13116 * original number of S/G entries is available in 13117 * rem_sg_entries. 13118 */ 13119 io->scsiio.kern_sg_entries = i; 13120 13121#if 0 13122 printf("%s: kern_sg_entries = %d\n", __func__, 13123 io->scsiio.kern_sg_entries); 13124 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13125 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13126 local_sglist[i].addr, local_sglist[i].len, 13127 local_dma_sglist[i].len); 13128#endif 13129 } 13130 13131 13132 return (retval); 13133 13134bailout_error: 13135 13136 ctl_send_datamove_done(io, /*have_lock*/ 0); 13137 13138 return (retval); 13139} 13140 13141static int 13142ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13143 ctl_ha_dt_cb callback) 13144{ 13145 struct ctl_ha_dt_req *rq; 13146 struct ctl_sg_entry *remote_sglist, *local_sglist; 13147 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13148 uint32_t local_used, remote_used, total_used; 13149 int retval; 13150 int i, j; 13151 13152 retval = 0; 13153 13154 rq = ctl_dt_req_alloc(); 13155 13156 /* 13157 * If we failed to allocate the request, and if the DMA didn't fail 13158 * anyway, set busy status. This is just a resource allocation 13159 * failure. 13160 */ 13161 if ((rq == NULL) 13162 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13163 ctl_set_busy(&io->scsiio); 13164 13165 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13166 13167 if (rq != NULL) 13168 ctl_dt_req_free(rq); 13169 13170 /* 13171 * The data move failed. We need to return status back 13172 * to the other controller. No point in trying to DMA 13173 * data to the remote controller. 13174 */ 13175 13176 ctl_send_datamove_done(io, /*have_lock*/ 0); 13177 13178 retval = 1; 13179 13180 goto bailout; 13181 } 13182 13183 local_sglist = io->io_hdr.local_sglist; 13184 local_dma_sglist = io->io_hdr.local_dma_sglist; 13185 remote_sglist = io->io_hdr.remote_sglist; 13186 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13187 local_used = 0; 13188 remote_used = 0; 13189 total_used = 0; 13190 13191 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13192 rq->ret = CTL_HA_STATUS_SUCCESS; 13193 rq->context = io; 13194 callback(rq); 13195 goto bailout; 13196 } 13197 13198 /* 13199 * Pull/push the data over the wire from/to the other controller. 13200 * This takes into account the possibility that the local and 13201 * remote sglists may not be identical in terms of the size of 13202 * the elements and the number of elements. 13203 * 13204 * One fundamental assumption here is that the length allocated for 13205 * both the local and remote sglists is identical. Otherwise, we've 13206 * essentially got a coding error of some sort. 13207 */ 13208 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13209 int isc_ret; 13210 uint32_t cur_len, dma_length; 13211 uint8_t *tmp_ptr; 13212 13213 rq->id = CTL_HA_DATA_CTL; 13214 rq->command = command; 13215 rq->context = io; 13216 13217 /* 13218 * Both pointers should be aligned. But it is possible 13219 * that the allocation length is not. They should both 13220 * also have enough slack left over at the end, though, 13221 * to round up to the next 8 byte boundary. 13222 */ 13223 cur_len = ctl_min(local_sglist[i].len - local_used, 13224 remote_sglist[j].len - remote_used); 13225 13226 /* 13227 * In this case, we have a size issue and need to decrease 13228 * the size, except in the case where we actually have less 13229 * than 8 bytes left. In that case, we need to increase 13230 * the DMA length to get the last bit. 13231 */ 13232 if ((cur_len & 0x7) != 0) { 13233 if (cur_len > 0x7) { 13234 cur_len = cur_len - (cur_len & 0x7); 13235 dma_length = cur_len; 13236 } else { 13237 CTL_SIZE_8B(dma_length, cur_len); 13238 } 13239 13240 } else 13241 dma_length = cur_len; 13242 13243 /* 13244 * If we had to allocate memory for this I/O, instead of using 13245 * the non-cached mirror memory, we'll need to flush the cache 13246 * before trying to DMA to the other controller. 13247 * 13248 * We could end up doing this multiple times for the same 13249 * segment if we have a larger local segment than remote 13250 * segment. That shouldn't be an issue. 13251 */ 13252 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13253 /* 13254 * XXX KDM use bus_dmamap_sync() here. 13255 */ 13256 } 13257 13258 rq->size = dma_length; 13259 13260 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13261 tmp_ptr += local_used; 13262 13263 /* Use physical addresses when talking to ISC hardware */ 13264 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13265 /* XXX KDM use busdma */ 13266#if 0 13267 rq->local = vtophys(tmp_ptr); 13268#endif 13269 } else 13270 rq->local = tmp_ptr; 13271 13272 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13273 tmp_ptr += remote_used; 13274 rq->remote = tmp_ptr; 13275 13276 rq->callback = NULL; 13277 13278 local_used += cur_len; 13279 if (local_used >= local_sglist[i].len) { 13280 i++; 13281 local_used = 0; 13282 } 13283 13284 remote_used += cur_len; 13285 if (remote_used >= remote_sglist[j].len) { 13286 j++; 13287 remote_used = 0; 13288 } 13289 total_used += cur_len; 13290 13291 if (total_used >= io->scsiio.kern_data_len) 13292 rq->callback = callback; 13293 13294 if ((rq->size & 0x7) != 0) { 13295 printf("%s: warning: size %d is not on 8b boundary\n", 13296 __func__, rq->size); 13297 } 13298 if (((uintptr_t)rq->local & 0x7) != 0) { 13299 printf("%s: warning: local %p not on 8b boundary\n", 13300 __func__, rq->local); 13301 } 13302 if (((uintptr_t)rq->remote & 0x7) != 0) { 13303 printf("%s: warning: remote %p not on 8b boundary\n", 13304 __func__, rq->local); 13305 } 13306#if 0 13307 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13308 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13309 rq->local, rq->remote, rq->size); 13310#endif 13311 13312 isc_ret = ctl_dt_single(rq); 13313 if (isc_ret == CTL_HA_STATUS_WAIT) 13314 continue; 13315 13316 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13317 rq->ret = CTL_HA_STATUS_SUCCESS; 13318 } else { 13319 rq->ret = isc_ret; 13320 } 13321 callback(rq); 13322 goto bailout; 13323 } 13324 13325bailout: 13326 return (retval); 13327 13328} 13329 13330static void 13331ctl_datamove_remote_read(union ctl_io *io) 13332{ 13333 int retval; 13334 int i; 13335 13336 /* 13337 * This will send an error to the other controller in the case of a 13338 * failure. 13339 */ 13340 retval = ctl_datamove_remote_sgl_setup(io); 13341 if (retval != 0) 13342 return; 13343 13344 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13345 ctl_datamove_remote_read_cb); 13346 if ((retval != 0) 13347 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13348 /* 13349 * Make sure we free memory if there was an error.. The 13350 * ctl_datamove_remote_xfer() function will send the 13351 * datamove done message, or call the callback with an 13352 * error if there is a problem. 13353 */ 13354 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13355 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13356 } 13357 13358 return; 13359} 13360 13361/* 13362 * Process a datamove request from the other controller. This is used for 13363 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13364 * first. Once that is complete, the data gets DMAed into the remote 13365 * controller's memory. For reads, we DMA from the remote controller's 13366 * memory into our memory first, and then move it out to the FETD. 13367 */ 13368static void 13369ctl_datamove_remote(union ctl_io *io) 13370{ 13371 struct ctl_softc *softc; 13372 13373 softc = control_softc; 13374 13375 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13376 13377 /* 13378 * Note that we look for an aborted I/O here, but don't do some of 13379 * the other checks that ctl_datamove() normally does. 13380 * We don't need to run the datamove delay code, since that should 13381 * have been done if need be on the other controller. 13382 */ 13383 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13384 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13385 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13386 io->io_hdr.nexus.targ_port, 13387 io->io_hdr.nexus.targ_target.id, 13388 io->io_hdr.nexus.targ_lun); 13389 io->io_hdr.port_status = 31338; 13390 ctl_send_datamove_done(io, /*have_lock*/ 0); 13391 return; 13392 } 13393 13394 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13395 ctl_datamove_remote_write(io); 13396 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13397 ctl_datamove_remote_read(io); 13398 } else { 13399 union ctl_ha_msg msg; 13400 struct scsi_sense_data *sense; 13401 uint8_t sks[3]; 13402 int retry_count; 13403 13404 memset(&msg, 0, sizeof(msg)); 13405 13406 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13407 msg.hdr.status = CTL_SCSI_ERROR; 13408 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13409 13410 retry_count = 4243; 13411 13412 sense = &msg.scsi.sense_data; 13413 sks[0] = SSD_SCS_VALID; 13414 sks[1] = (retry_count >> 8) & 0xff; 13415 sks[2] = retry_count & 0xff; 13416 13417 /* "Internal target failure" */ 13418 scsi_set_sense_data(sense, 13419 /*sense_format*/ SSD_TYPE_NONE, 13420 /*current_error*/ 1, 13421 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13422 /*asc*/ 0x44, 13423 /*ascq*/ 0x00, 13424 /*type*/ SSD_ELEM_SKS, 13425 /*size*/ sizeof(sks), 13426 /*data*/ sks, 13427 SSD_ELEM_NONE); 13428 13429 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13430 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13431 ctl_failover_io(io, /*have_lock*/ 1); 13432 return; 13433 } 13434 13435 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13436 CTL_HA_STATUS_SUCCESS) { 13437 /* XXX KDM what to do if this fails? */ 13438 } 13439 return; 13440 } 13441 13442} 13443 13444static int 13445ctl_process_done(union ctl_io *io) 13446{ 13447 struct ctl_lun *lun; 13448 struct ctl_softc *ctl_softc = control_softc; 13449 void (*fe_done)(union ctl_io *io); 13450 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13451 13452 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13453 13454 fe_done = 13455 control_softc->ctl_ports[targ_port]->fe_done; 13456 13457#ifdef CTL_TIME_IO 13458 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13459 char str[256]; 13460 char path_str[64]; 13461 struct sbuf sb; 13462 13463 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13464 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13465 13466 sbuf_cat(&sb, path_str); 13467 switch (io->io_hdr.io_type) { 13468 case CTL_IO_SCSI: 13469 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13470 sbuf_printf(&sb, "\n"); 13471 sbuf_cat(&sb, path_str); 13472 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13473 io->scsiio.tag_num, io->scsiio.tag_type); 13474 break; 13475 case CTL_IO_TASK: 13476 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13477 "Tag Type: %d\n", io->taskio.task_action, 13478 io->taskio.tag_num, io->taskio.tag_type); 13479 break; 13480 default: 13481 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13482 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13483 break; 13484 } 13485 sbuf_cat(&sb, path_str); 13486 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13487 (intmax_t)time_uptime - io->io_hdr.start_time); 13488 sbuf_finish(&sb); 13489 printf("%s", sbuf_data(&sb)); 13490 } 13491#endif /* CTL_TIME_IO */ 13492 13493 switch (io->io_hdr.io_type) { 13494 case CTL_IO_SCSI: 13495 break; 13496 case CTL_IO_TASK: 13497 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13498 ctl_io_error_print(io, NULL); 13499 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13500 ctl_free_io(io); 13501 else 13502 fe_done(io); 13503 return (CTL_RETVAL_COMPLETE); 13504 default: 13505 panic("ctl_process_done: invalid io type %d\n", 13506 io->io_hdr.io_type); 13507 break; /* NOTREACHED */ 13508 } 13509 13510 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13511 if (lun == NULL) { 13512 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13513 io->io_hdr.nexus.targ_mapped_lun)); 13514 goto bailout; 13515 } 13516 13517 mtx_lock(&lun->lun_lock); 13518 13519 /* 13520 * Check to see if we have any errors to inject here. We only 13521 * inject errors for commands that don't already have errors set. 13522 */ 13523 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13524 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13525 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13526 ctl_inject_error(lun, io); 13527 13528 /* 13529 * XXX KDM how do we treat commands that aren't completed 13530 * successfully? 13531 * 13532 * XXX KDM should we also track I/O latency? 13533 */ 13534 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13535 io->io_hdr.io_type == CTL_IO_SCSI) { 13536#ifdef CTL_TIME_IO 13537 struct bintime cur_bt; 13538#endif 13539 int type; 13540 13541 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13542 CTL_FLAG_DATA_IN) 13543 type = CTL_STATS_READ; 13544 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13545 CTL_FLAG_DATA_OUT) 13546 type = CTL_STATS_WRITE; 13547 else 13548 type = CTL_STATS_NO_IO; 13549 13550 lun->stats.ports[targ_port].bytes[type] += 13551 io->scsiio.kern_total_len; 13552 lun->stats.ports[targ_port].operations[type]++; 13553#ifdef CTL_TIME_IO 13554 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13555 &io->io_hdr.dma_bt); 13556 lun->stats.ports[targ_port].num_dmas[type] += 13557 io->io_hdr.num_dmas; 13558 getbintime(&cur_bt); 13559 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13560 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13561#endif 13562 } 13563 13564 /* 13565 * Remove this from the OOA queue. 13566 */ 13567 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13568 13569 /* 13570 * Run through the blocked queue on this LUN and see if anything 13571 * has become unblocked, now that this transaction is done. 13572 */ 13573 ctl_check_blocked(lun); 13574 13575 /* 13576 * If the LUN has been invalidated, free it if there is nothing 13577 * left on its OOA queue. 13578 */ 13579 if ((lun->flags & CTL_LUN_INVALID) 13580 && TAILQ_EMPTY(&lun->ooa_queue)) { 13581 mtx_unlock(&lun->lun_lock); 13582 mtx_lock(&ctl_softc->ctl_lock); 13583 ctl_free_lun(lun); 13584 mtx_unlock(&ctl_softc->ctl_lock); 13585 } else 13586 mtx_unlock(&lun->lun_lock); 13587 13588bailout: 13589 13590 /* 13591 * If this command has been aborted, make sure we set the status 13592 * properly. The FETD is responsible for freeing the I/O and doing 13593 * whatever it needs to do to clean up its state. 13594 */ 13595 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13596 ctl_set_task_aborted(&io->scsiio); 13597 13598 /* 13599 * If enabled, print command error status. 13600 * We don't print UAs unless debugging was enabled explicitly. 13601 */ 13602 do { 13603 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13604 break; 13605 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13606 break; 13607 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13608 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13609 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13610 int error_code, sense_key, asc, ascq; 13611 13612 scsi_extract_sense_len(&io->scsiio.sense_data, 13613 io->scsiio.sense_len, &error_code, &sense_key, 13614 &asc, &ascq, /*show_errors*/ 0); 13615 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13616 break; 13617 } 13618 13619 ctl_io_error_print(io, NULL); 13620 } while (0); 13621 13622 /* 13623 * Tell the FETD or the other shelf controller we're done with this 13624 * command. Note that only SCSI commands get to this point. Task 13625 * management commands are completed above. 13626 * 13627 * We only send status to the other controller if we're in XFER 13628 * mode. In SER_ONLY mode, the I/O is done on the controller that 13629 * received the I/O (from CTL's perspective), and so the status is 13630 * generated there. 13631 * 13632 * XXX KDM if we hold the lock here, we could cause a deadlock 13633 * if the frontend comes back in in this context to queue 13634 * something. 13635 */ 13636 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13637 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13638 union ctl_ha_msg msg; 13639 13640 memset(&msg, 0, sizeof(msg)); 13641 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13642 msg.hdr.original_sc = io->io_hdr.original_sc; 13643 msg.hdr.nexus = io->io_hdr.nexus; 13644 msg.hdr.status = io->io_hdr.status; 13645 msg.scsi.scsi_status = io->scsiio.scsi_status; 13646 msg.scsi.tag_num = io->scsiio.tag_num; 13647 msg.scsi.tag_type = io->scsiio.tag_type; 13648 msg.scsi.sense_len = io->scsiio.sense_len; 13649 msg.scsi.sense_residual = io->scsiio.sense_residual; 13650 msg.scsi.residual = io->scsiio.residual; 13651 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13652 sizeof(io->scsiio.sense_data)); 13653 /* 13654 * We copy this whether or not this is an I/O-related 13655 * command. Otherwise, we'd have to go and check to see 13656 * whether it's a read/write command, and it really isn't 13657 * worth it. 13658 */ 13659 memcpy(&msg.scsi.lbalen, 13660 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13661 sizeof(msg.scsi.lbalen)); 13662 13663 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13664 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13665 /* XXX do something here */ 13666 } 13667 13668 ctl_free_io(io); 13669 } else 13670 fe_done(io); 13671 13672 return (CTL_RETVAL_COMPLETE); 13673} 13674 13675#ifdef CTL_WITH_CA 13676/* 13677 * Front end should call this if it doesn't do autosense. When the request 13678 * sense comes back in from the initiator, we'll dequeue this and send it. 13679 */ 13680int 13681ctl_queue_sense(union ctl_io *io) 13682{ 13683 struct ctl_lun *lun; 13684 struct ctl_softc *ctl_softc; 13685 uint32_t initidx, targ_lun; 13686 13687 ctl_softc = control_softc; 13688 13689 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13690 13691 /* 13692 * LUN lookup will likely move to the ctl_work_thread() once we 13693 * have our new queueing infrastructure (that doesn't put things on 13694 * a per-LUN queue initially). That is so that we can handle 13695 * things like an INQUIRY to a LUN that we don't have enabled. We 13696 * can't deal with that right now. 13697 */ 13698 mtx_lock(&ctl_softc->ctl_lock); 13699 13700 /* 13701 * If we don't have a LUN for this, just toss the sense 13702 * information. 13703 */ 13704 targ_lun = io->io_hdr.nexus.targ_lun; 13705 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13706 if ((targ_lun < CTL_MAX_LUNS) 13707 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13708 lun = ctl_softc->ctl_luns[targ_lun]; 13709 else 13710 goto bailout; 13711 13712 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13713 13714 mtx_lock(&lun->lun_lock); 13715 /* 13716 * Already have CA set for this LUN...toss the sense information. 13717 */ 13718 if (ctl_is_set(lun->have_ca, initidx)) { 13719 mtx_unlock(&lun->lun_lock); 13720 goto bailout; 13721 } 13722 13723 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13724 ctl_min(sizeof(lun->pending_sense[initidx]), 13725 sizeof(io->scsiio.sense_data))); 13726 ctl_set_mask(lun->have_ca, initidx); 13727 mtx_unlock(&lun->lun_lock); 13728 13729bailout: 13730 mtx_unlock(&ctl_softc->ctl_lock); 13731 13732 ctl_free_io(io); 13733 13734 return (CTL_RETVAL_COMPLETE); 13735} 13736#endif 13737 13738/* 13739 * Primary command inlet from frontend ports. All SCSI and task I/O 13740 * requests must go through this function. 13741 */ 13742int 13743ctl_queue(union ctl_io *io) 13744{ 13745 struct ctl_softc *ctl_softc; 13746 13747 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13748 13749 ctl_softc = control_softc; 13750 13751#ifdef CTL_TIME_IO 13752 io->io_hdr.start_time = time_uptime; 13753 getbintime(&io->io_hdr.start_bt); 13754#endif /* CTL_TIME_IO */ 13755 13756 /* Map FE-specific LUN ID into global one. */ 13757 io->io_hdr.nexus.targ_mapped_lun = 13758 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13759 13760 switch (io->io_hdr.io_type) { 13761 case CTL_IO_SCSI: 13762 case CTL_IO_TASK: 13763 if (ctl_debug & CTL_DEBUG_CDB) 13764 ctl_io_print(io); 13765 ctl_enqueue_incoming(io); 13766 break; 13767 default: 13768 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13769 return (EINVAL); 13770 } 13771 13772 return (CTL_RETVAL_COMPLETE); 13773} 13774 13775#ifdef CTL_IO_DELAY 13776static void 13777ctl_done_timer_wakeup(void *arg) 13778{ 13779 union ctl_io *io; 13780 13781 io = (union ctl_io *)arg; 13782 ctl_done(io); 13783} 13784#endif /* CTL_IO_DELAY */ 13785 13786void 13787ctl_done(union ctl_io *io) 13788{ 13789 struct ctl_softc *ctl_softc; 13790 13791 ctl_softc = control_softc; 13792 13793 /* 13794 * Enable this to catch duplicate completion issues. 13795 */ 13796#if 0 13797 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13798 printf("%s: type %d msg %d cdb %x iptl: " 13799 "%d:%d:%d:%d tag 0x%04x " 13800 "flag %#x status %x\n", 13801 __func__, 13802 io->io_hdr.io_type, 13803 io->io_hdr.msg_type, 13804 io->scsiio.cdb[0], 13805 io->io_hdr.nexus.initid.id, 13806 io->io_hdr.nexus.targ_port, 13807 io->io_hdr.nexus.targ_target.id, 13808 io->io_hdr.nexus.targ_lun, 13809 (io->io_hdr.io_type == 13810 CTL_IO_TASK) ? 13811 io->taskio.tag_num : 13812 io->scsiio.tag_num, 13813 io->io_hdr.flags, 13814 io->io_hdr.status); 13815 } else 13816 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13817#endif 13818 13819 /* 13820 * This is an internal copy of an I/O, and should not go through 13821 * the normal done processing logic. 13822 */ 13823 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13824 return; 13825 13826 /* 13827 * We need to send a msg to the serializing shelf to finish the IO 13828 * as well. We don't send a finish message to the other shelf if 13829 * this is a task management command. Task management commands 13830 * aren't serialized in the OOA queue, but rather just executed on 13831 * both shelf controllers for commands that originated on that 13832 * controller. 13833 */ 13834 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13835 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13836 union ctl_ha_msg msg_io; 13837 13838 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13839 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13840 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13841 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13842 } 13843 /* continue on to finish IO */ 13844 } 13845#ifdef CTL_IO_DELAY 13846 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13847 struct ctl_lun *lun; 13848 13849 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13850 13851 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13852 } else { 13853 struct ctl_lun *lun; 13854 13855 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13856 13857 if ((lun != NULL) 13858 && (lun->delay_info.done_delay > 0)) { 13859 struct callout *callout; 13860 13861 callout = (struct callout *)&io->io_hdr.timer_bytes; 13862 callout_init(callout, /*mpsafe*/ 1); 13863 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13864 callout_reset(callout, 13865 lun->delay_info.done_delay * hz, 13866 ctl_done_timer_wakeup, io); 13867 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13868 lun->delay_info.done_delay = 0; 13869 return; 13870 } 13871 } 13872#endif /* CTL_IO_DELAY */ 13873 13874 ctl_enqueue_done(io); 13875} 13876 13877int 13878ctl_isc(struct ctl_scsiio *ctsio) 13879{ 13880 struct ctl_lun *lun; 13881 int retval; 13882 13883 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13884 13885 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13886 13887 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13888 13889 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13890 13891 return (retval); 13892} 13893 13894 13895static void 13896ctl_work_thread(void *arg) 13897{ 13898 struct ctl_thread *thr = (struct ctl_thread *)arg; 13899 struct ctl_softc *softc = thr->ctl_softc; 13900 union ctl_io *io; 13901 int retval; 13902 13903 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13904 13905 for (;;) { 13906 retval = 0; 13907 13908 /* 13909 * We handle the queues in this order: 13910 * - ISC 13911 * - done queue (to free up resources, unblock other commands) 13912 * - RtR queue 13913 * - incoming queue 13914 * 13915 * If those queues are empty, we break out of the loop and 13916 * go to sleep. 13917 */ 13918 mtx_lock(&thr->queue_lock); 13919 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13920 if (io != NULL) { 13921 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13922 mtx_unlock(&thr->queue_lock); 13923 ctl_handle_isc(io); 13924 continue; 13925 } 13926 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13927 if (io != NULL) { 13928 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13929 /* clear any blocked commands, call fe_done */ 13930 mtx_unlock(&thr->queue_lock); 13931 retval = ctl_process_done(io); 13932 continue; 13933 } 13934 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13935 if (io != NULL) { 13936 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13937 mtx_unlock(&thr->queue_lock); 13938 if (io->io_hdr.io_type == CTL_IO_TASK) 13939 ctl_run_task(io); 13940 else 13941 ctl_scsiio_precheck(softc, &io->scsiio); 13942 continue; 13943 } 13944 if (!ctl_pause_rtr) { 13945 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13946 if (io != NULL) { 13947 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13948 mtx_unlock(&thr->queue_lock); 13949 retval = ctl_scsiio(&io->scsiio); 13950 if (retval != CTL_RETVAL_COMPLETE) 13951 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13952 continue; 13953 } 13954 } 13955 13956 /* Sleep until we have something to do. */ 13957 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13958 } 13959} 13960 13961static void 13962ctl_lun_thread(void *arg) 13963{ 13964 struct ctl_softc *softc = (struct ctl_softc *)arg; 13965 struct ctl_be_lun *be_lun; 13966 int retval; 13967 13968 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13969 13970 for (;;) { 13971 retval = 0; 13972 mtx_lock(&softc->ctl_lock); 13973 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13974 if (be_lun != NULL) { 13975 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13976 mtx_unlock(&softc->ctl_lock); 13977 ctl_create_lun(be_lun); 13978 continue; 13979 } 13980 13981 /* Sleep until we have something to do. */ 13982 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13983 PDROP | PRIBIO, "-", 0); 13984 } 13985} 13986 13987static void 13988ctl_thresh_thread(void *arg) 13989{ 13990 struct ctl_softc *softc = (struct ctl_softc *)arg; 13991 struct ctl_lun *lun; 13992 struct ctl_be_lun *be_lun; 13993 struct scsi_da_rw_recovery_page *rwpage; 13994 struct ctl_logical_block_provisioning_page *page; 13995 const char *attr; 13996 uint64_t thres, val; 13997 int i, e; 13998 13999 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14000 14001 for (;;) { 14002 mtx_lock(&softc->ctl_lock); 14003 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14004 be_lun = lun->be_lun; 14005 if ((lun->flags & CTL_LUN_DISABLED) || 14006 (lun->flags & CTL_LUN_OFFLINE) || 14007 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 14008 lun->backend->lun_attr == NULL) 14009 continue; 14010 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14011 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14012 continue; 14013 e = 0; 14014 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14015 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14016 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14017 continue; 14018 thres = scsi_4btoul(page->descr[i].count); 14019 thres <<= CTL_LBP_EXPONENT; 14020 switch (page->descr[i].resource) { 14021 case 0x01: 14022 attr = "blocksavail"; 14023 break; 14024 case 0x02: 14025 attr = "blocksused"; 14026 break; 14027 case 0xf1: 14028 attr = "poolblocksavail"; 14029 break; 14030 case 0xf2: 14031 attr = "poolblocksused"; 14032 break; 14033 default: 14034 continue; 14035 } 14036 mtx_unlock(&softc->ctl_lock); // XXX 14037 val = lun->backend->lun_attr( 14038 lun->be_lun->be_lun, attr); 14039 mtx_lock(&softc->ctl_lock); 14040 if (val == UINT64_MAX) 14041 continue; 14042 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14043 == SLBPPD_ARMING_INC) 14044 e |= (val >= thres); 14045 else 14046 e |= (val <= thres); 14047 } 14048 mtx_lock(&lun->lun_lock); 14049 if (e) { 14050 if (lun->lasttpt == 0 || 14051 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14052 lun->lasttpt = time_uptime; 14053 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14054 } 14055 } else { 14056 lun->lasttpt = 0; 14057 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14058 } 14059 mtx_unlock(&lun->lun_lock); 14060 } 14061 mtx_unlock(&softc->ctl_lock); 14062 pause("-", CTL_LBP_PERIOD * hz); 14063 } 14064} 14065 14066static void 14067ctl_enqueue_incoming(union ctl_io *io) 14068{ 14069 struct ctl_softc *softc = control_softc; 14070 struct ctl_thread *thr; 14071 u_int idx; 14072 14073 idx = (io->io_hdr.nexus.targ_port * 127 + 14074 io->io_hdr.nexus.initid.id) % worker_threads; 14075 thr = &softc->threads[idx]; 14076 mtx_lock(&thr->queue_lock); 14077 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14078 mtx_unlock(&thr->queue_lock); 14079 wakeup(thr); 14080} 14081 14082static void 14083ctl_enqueue_rtr(union ctl_io *io) 14084{ 14085 struct ctl_softc *softc = control_softc; 14086 struct ctl_thread *thr; 14087 14088 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14089 mtx_lock(&thr->queue_lock); 14090 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14091 mtx_unlock(&thr->queue_lock); 14092 wakeup(thr); 14093} 14094 14095static void 14096ctl_enqueue_done(union ctl_io *io) 14097{ 14098 struct ctl_softc *softc = control_softc; 14099 struct ctl_thread *thr; 14100 14101 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14102 mtx_lock(&thr->queue_lock); 14103 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14104 mtx_unlock(&thr->queue_lock); 14105 wakeup(thr); 14106} 14107 14108static void 14109ctl_enqueue_isc(union ctl_io *io) 14110{ 14111 struct ctl_softc *softc = control_softc; 14112 struct ctl_thread *thr; 14113 14114 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14115 mtx_lock(&thr->queue_lock); 14116 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14117 mtx_unlock(&thr->queue_lock); 14118 wakeup(thr); 14119} 14120 14121/* Initialization and failover */ 14122 14123void 14124ctl_init_isc_msg(void) 14125{ 14126 printf("CTL: Still calling this thing\n"); 14127} 14128 14129/* 14130 * Init component 14131 * Initializes component into configuration defined by bootMode 14132 * (see hasc-sv.c) 14133 * returns hasc_Status: 14134 * OK 14135 * ERROR - fatal error 14136 */ 14137static ctl_ha_comp_status 14138ctl_isc_init(struct ctl_ha_component *c) 14139{ 14140 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14141 14142 c->status = ret; 14143 return ret; 14144} 14145 14146/* Start component 14147 * Starts component in state requested. If component starts successfully, 14148 * it must set its own state to the requestrd state 14149 * When requested state is HASC_STATE_HA, the component may refine it 14150 * by adding _SLAVE or _MASTER flags. 14151 * Currently allowed state transitions are: 14152 * UNKNOWN->HA - initial startup 14153 * UNKNOWN->SINGLE - initial startup when no parter detected 14154 * HA->SINGLE - failover 14155 * returns ctl_ha_comp_status: 14156 * OK - component successfully started in requested state 14157 * FAILED - could not start the requested state, failover may 14158 * be possible 14159 * ERROR - fatal error detected, no future startup possible 14160 */ 14161static ctl_ha_comp_status 14162ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14163{ 14164 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14165 14166 printf("%s: go\n", __func__); 14167 14168 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14169 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14170 control_softc->is_single = 0; 14171 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14172 != CTL_HA_STATUS_SUCCESS) { 14173 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14174 ret = CTL_HA_COMP_STATUS_ERROR; 14175 } 14176 } else if (CTL_HA_STATE_IS_HA(c->state) 14177 && CTL_HA_STATE_IS_SINGLE(state)){ 14178 // HA->SINGLE transition 14179 ctl_failover(); 14180 control_softc->is_single = 1; 14181 } else { 14182 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14183 c->state, state); 14184 ret = CTL_HA_COMP_STATUS_ERROR; 14185 } 14186 if (CTL_HA_STATE_IS_SINGLE(state)) 14187 control_softc->is_single = 1; 14188 14189 c->state = state; 14190 c->status = ret; 14191 return ret; 14192} 14193 14194/* 14195 * Quiesce component 14196 * The component must clear any error conditions (set status to OK) and 14197 * prepare itself to another Start call 14198 * returns ctl_ha_comp_status: 14199 * OK 14200 * ERROR 14201 */ 14202static ctl_ha_comp_status 14203ctl_isc_quiesce(struct ctl_ha_component *c) 14204{ 14205 int ret = CTL_HA_COMP_STATUS_OK; 14206 14207 ctl_pause_rtr = 1; 14208 c->status = ret; 14209 return ret; 14210} 14211 14212struct ctl_ha_component ctl_ha_component_ctlisc = 14213{ 14214 .name = "CTL ISC", 14215 .state = CTL_HA_STATE_UNKNOWN, 14216 .init = ctl_isc_init, 14217 .start = ctl_isc_start, 14218 .quiesce = ctl_isc_quiesce 14219}; 14220 14221/* 14222 * vim: ts=8 14223 */ 14224