ctl.c revision 275888
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 275888 2014-12-18 08:32:06Z 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 5177/* 5178 * SCSI release command. 5179 */ 5180int 5181ctl_scsi_release(struct ctl_scsiio *ctsio) 5182{ 5183 int length, longid, thirdparty_id, resv_id; 5184 struct ctl_softc *ctl_softc; 5185 struct ctl_lun *lun; 5186 uint32_t residx; 5187 5188 length = 0; 5189 resv_id = 0; 5190 5191 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5192 5193 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5194 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5195 ctl_softc = control_softc; 5196 5197 switch (ctsio->cdb[0]) { 5198 case RELEASE_10: { 5199 struct scsi_release_10 *cdb; 5200 5201 cdb = (struct scsi_release_10 *)ctsio->cdb; 5202 5203 if (cdb->byte2 & SR10_LONGID) 5204 longid = 1; 5205 else 5206 thirdparty_id = cdb->thirdparty_id; 5207 5208 resv_id = cdb->resv_id; 5209 length = scsi_2btoul(cdb->length); 5210 break; 5211 } 5212 } 5213 5214 5215 /* 5216 * XXX KDM right now, we only support LUN reservation. We don't 5217 * support 3rd party reservations, or extent reservations, which 5218 * might actually need the parameter list. If we've gotten this 5219 * far, we've got a LUN reservation. Anything else got kicked out 5220 * above. So, according to SPC, ignore the length. 5221 */ 5222 length = 0; 5223 5224 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5225 && (length > 0)) { 5226 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5227 ctsio->kern_data_len = length; 5228 ctsio->kern_total_len = length; 5229 ctsio->kern_data_resid = 0; 5230 ctsio->kern_rel_offset = 0; 5231 ctsio->kern_sg_entries = 0; 5232 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5233 ctsio->be_move_done = ctl_config_move_done; 5234 ctl_datamove((union ctl_io *)ctsio); 5235 5236 return (CTL_RETVAL_COMPLETE); 5237 } 5238 5239 if (length > 0) 5240 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5241 5242 mtx_lock(&lun->lun_lock); 5243 5244 /* 5245 * According to SPC, it is not an error for an intiator to attempt 5246 * to release a reservation on a LUN that isn't reserved, or that 5247 * is reserved by another initiator. The reservation can only be 5248 * released, though, by the initiator who made it or by one of 5249 * several reset type events. 5250 */ 5251 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5252 lun->flags &= ~CTL_LUN_RESERVED; 5253 5254 mtx_unlock(&lun->lun_lock); 5255 5256 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5257 free(ctsio->kern_data_ptr, M_CTL); 5258 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5259 } 5260 5261 ctl_set_success(ctsio); 5262 ctl_done((union ctl_io *)ctsio); 5263 return (CTL_RETVAL_COMPLETE); 5264} 5265 5266int 5267ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5268{ 5269 int extent, thirdparty, longid; 5270 int resv_id, length; 5271 uint64_t thirdparty_id; 5272 struct ctl_softc *ctl_softc; 5273 struct ctl_lun *lun; 5274 uint32_t residx; 5275 5276 extent = 0; 5277 thirdparty = 0; 5278 longid = 0; 5279 resv_id = 0; 5280 length = 0; 5281 thirdparty_id = 0; 5282 5283 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5284 5285 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5286 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5287 ctl_softc = control_softc; 5288 5289 switch (ctsio->cdb[0]) { 5290 case RESERVE_10: { 5291 struct scsi_reserve_10 *cdb; 5292 5293 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5294 5295 if (cdb->byte2 & SR10_LONGID) 5296 longid = 1; 5297 else 5298 thirdparty_id = cdb->thirdparty_id; 5299 5300 resv_id = cdb->resv_id; 5301 length = scsi_2btoul(cdb->length); 5302 break; 5303 } 5304 } 5305 5306 /* 5307 * XXX KDM right now, we only support LUN reservation. We don't 5308 * support 3rd party reservations, or extent reservations, which 5309 * might actually need the parameter list. If we've gotten this 5310 * far, we've got a LUN reservation. Anything else got kicked out 5311 * above. So, according to SPC, ignore the length. 5312 */ 5313 length = 0; 5314 5315 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5316 && (length > 0)) { 5317 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5318 ctsio->kern_data_len = length; 5319 ctsio->kern_total_len = length; 5320 ctsio->kern_data_resid = 0; 5321 ctsio->kern_rel_offset = 0; 5322 ctsio->kern_sg_entries = 0; 5323 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5324 ctsio->be_move_done = ctl_config_move_done; 5325 ctl_datamove((union ctl_io *)ctsio); 5326 5327 return (CTL_RETVAL_COMPLETE); 5328 } 5329 5330 if (length > 0) 5331 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5332 5333 mtx_lock(&lun->lun_lock); 5334 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5335 ctl_set_reservation_conflict(ctsio); 5336 goto bailout; 5337 } 5338 5339 lun->flags |= CTL_LUN_RESERVED; 5340 lun->res_idx = residx; 5341 5342 ctl_set_success(ctsio); 5343 5344bailout: 5345 mtx_unlock(&lun->lun_lock); 5346 5347 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5348 free(ctsio->kern_data_ptr, M_CTL); 5349 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5350 } 5351 5352 ctl_done((union ctl_io *)ctsio); 5353 return (CTL_RETVAL_COMPLETE); 5354} 5355 5356int 5357ctl_start_stop(struct ctl_scsiio *ctsio) 5358{ 5359 struct scsi_start_stop_unit *cdb; 5360 struct ctl_lun *lun; 5361 struct ctl_softc *ctl_softc; 5362 int retval; 5363 5364 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5365 5366 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5367 ctl_softc = control_softc; 5368 retval = 0; 5369 5370 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5371 5372 /* 5373 * XXX KDM 5374 * We don't support the immediate bit on a stop unit. In order to 5375 * do that, we would need to code up a way to know that a stop is 5376 * pending, and hold off any new commands until it completes, one 5377 * way or another. Then we could accept or reject those commands 5378 * depending on its status. We would almost need to do the reverse 5379 * of what we do below for an immediate start -- return the copy of 5380 * the ctl_io to the FETD with status to send to the host (and to 5381 * free the copy!) and then free the original I/O once the stop 5382 * actually completes. That way, the OOA queue mechanism can work 5383 * to block commands that shouldn't proceed. Another alternative 5384 * would be to put the copy in the queue in place of the original, 5385 * and return the original back to the caller. That could be 5386 * slightly safer.. 5387 */ 5388 if ((cdb->byte2 & SSS_IMMED) 5389 && ((cdb->how & SSS_START) == 0)) { 5390 ctl_set_invalid_field(ctsio, 5391 /*sks_valid*/ 1, 5392 /*command*/ 1, 5393 /*field*/ 1, 5394 /*bit_valid*/ 1, 5395 /*bit*/ 0); 5396 ctl_done((union ctl_io *)ctsio); 5397 return (CTL_RETVAL_COMPLETE); 5398 } 5399 5400 if ((lun->flags & CTL_LUN_PR_RESERVED) 5401 && ((cdb->how & SSS_START)==0)) { 5402 uint32_t residx; 5403 5404 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5405 if (ctl_get_prkey(lun, residx) == 0 5406 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5407 5408 ctl_set_reservation_conflict(ctsio); 5409 ctl_done((union ctl_io *)ctsio); 5410 return (CTL_RETVAL_COMPLETE); 5411 } 5412 } 5413 5414 /* 5415 * If there is no backend on this device, we can't start or stop 5416 * it. In theory we shouldn't get any start/stop commands in the 5417 * first place at this level if the LUN doesn't have a backend. 5418 * That should get stopped by the command decode code. 5419 */ 5420 if (lun->backend == NULL) { 5421 ctl_set_invalid_opcode(ctsio); 5422 ctl_done((union ctl_io *)ctsio); 5423 return (CTL_RETVAL_COMPLETE); 5424 } 5425 5426 /* 5427 * XXX KDM Copan-specific offline behavior. 5428 * Figure out a reasonable way to port this? 5429 */ 5430#ifdef NEEDTOPORT 5431 mtx_lock(&lun->lun_lock); 5432 5433 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5434 && (lun->flags & CTL_LUN_OFFLINE)) { 5435 /* 5436 * If the LUN is offline, and the on/offline bit isn't set, 5437 * reject the start or stop. Otherwise, let it through. 5438 */ 5439 mtx_unlock(&lun->lun_lock); 5440 ctl_set_lun_not_ready(ctsio); 5441 ctl_done((union ctl_io *)ctsio); 5442 } else { 5443 mtx_unlock(&lun->lun_lock); 5444#endif /* NEEDTOPORT */ 5445 /* 5446 * This could be a start or a stop when we're online, 5447 * or a stop/offline or start/online. A start or stop when 5448 * we're offline is covered in the case above. 5449 */ 5450 /* 5451 * In the non-immediate case, we send the request to 5452 * the backend and return status to the user when 5453 * it is done. 5454 * 5455 * In the immediate case, we allocate a new ctl_io 5456 * to hold a copy of the request, and send that to 5457 * the backend. We then set good status on the 5458 * user's request and return it immediately. 5459 */ 5460 if (cdb->byte2 & SSS_IMMED) { 5461 union ctl_io *new_io; 5462 5463 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5464 ctl_copy_io((union ctl_io *)ctsio, new_io); 5465 retval = lun->backend->config_write(new_io); 5466 ctl_set_success(ctsio); 5467 ctl_done((union ctl_io *)ctsio); 5468 } else { 5469 retval = lun->backend->config_write( 5470 (union ctl_io *)ctsio); 5471 } 5472#ifdef NEEDTOPORT 5473 } 5474#endif 5475 return (retval); 5476} 5477 5478/* 5479 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5480 * we don't really do anything with the LBA and length fields if the user 5481 * passes them in. Instead we'll just flush out the cache for the entire 5482 * LUN. 5483 */ 5484int 5485ctl_sync_cache(struct ctl_scsiio *ctsio) 5486{ 5487 struct ctl_lun *lun; 5488 struct ctl_softc *ctl_softc; 5489 uint64_t starting_lba; 5490 uint32_t block_count; 5491 int retval; 5492 5493 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5494 5495 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5496 ctl_softc = control_softc; 5497 retval = 0; 5498 5499 switch (ctsio->cdb[0]) { 5500 case SYNCHRONIZE_CACHE: { 5501 struct scsi_sync_cache *cdb; 5502 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5503 5504 starting_lba = scsi_4btoul(cdb->begin_lba); 5505 block_count = scsi_2btoul(cdb->lb_count); 5506 break; 5507 } 5508 case SYNCHRONIZE_CACHE_16: { 5509 struct scsi_sync_cache_16 *cdb; 5510 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5511 5512 starting_lba = scsi_8btou64(cdb->begin_lba); 5513 block_count = scsi_4btoul(cdb->lb_count); 5514 break; 5515 } 5516 default: 5517 ctl_set_invalid_opcode(ctsio); 5518 ctl_done((union ctl_io *)ctsio); 5519 goto bailout; 5520 break; /* NOTREACHED */ 5521 } 5522 5523 /* 5524 * We check the LBA and length, but don't do anything with them. 5525 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5526 * get flushed. This check will just help satisfy anyone who wants 5527 * to see an error for an out of range LBA. 5528 */ 5529 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5530 ctl_set_lba_out_of_range(ctsio); 5531 ctl_done((union ctl_io *)ctsio); 5532 goto bailout; 5533 } 5534 5535 /* 5536 * If this LUN has no backend, we can't flush the cache anyway. 5537 */ 5538 if (lun->backend == NULL) { 5539 ctl_set_invalid_opcode(ctsio); 5540 ctl_done((union ctl_io *)ctsio); 5541 goto bailout; 5542 } 5543 5544 /* 5545 * Check to see whether we're configured to send the SYNCHRONIZE 5546 * CACHE command directly to the back end. 5547 */ 5548 mtx_lock(&lun->lun_lock); 5549 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5550 && (++(lun->sync_count) >= lun->sync_interval)) { 5551 lun->sync_count = 0; 5552 mtx_unlock(&lun->lun_lock); 5553 retval = lun->backend->config_write((union ctl_io *)ctsio); 5554 } else { 5555 mtx_unlock(&lun->lun_lock); 5556 ctl_set_success(ctsio); 5557 ctl_done((union ctl_io *)ctsio); 5558 } 5559 5560bailout: 5561 5562 return (retval); 5563} 5564 5565int 5566ctl_format(struct ctl_scsiio *ctsio) 5567{ 5568 struct scsi_format *cdb; 5569 struct ctl_lun *lun; 5570 struct ctl_softc *ctl_softc; 5571 int length, defect_list_len; 5572 5573 CTL_DEBUG_PRINT(("ctl_format\n")); 5574 5575 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5576 ctl_softc = control_softc; 5577 5578 cdb = (struct scsi_format *)ctsio->cdb; 5579 5580 length = 0; 5581 if (cdb->byte2 & SF_FMTDATA) { 5582 if (cdb->byte2 & SF_LONGLIST) 5583 length = sizeof(struct scsi_format_header_long); 5584 else 5585 length = sizeof(struct scsi_format_header_short); 5586 } 5587 5588 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5589 && (length > 0)) { 5590 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5591 ctsio->kern_data_len = length; 5592 ctsio->kern_total_len = length; 5593 ctsio->kern_data_resid = 0; 5594 ctsio->kern_rel_offset = 0; 5595 ctsio->kern_sg_entries = 0; 5596 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5597 ctsio->be_move_done = ctl_config_move_done; 5598 ctl_datamove((union ctl_io *)ctsio); 5599 5600 return (CTL_RETVAL_COMPLETE); 5601 } 5602 5603 defect_list_len = 0; 5604 5605 if (cdb->byte2 & SF_FMTDATA) { 5606 if (cdb->byte2 & SF_LONGLIST) { 5607 struct scsi_format_header_long *header; 5608 5609 header = (struct scsi_format_header_long *) 5610 ctsio->kern_data_ptr; 5611 5612 defect_list_len = scsi_4btoul(header->defect_list_len); 5613 if (defect_list_len != 0) { 5614 ctl_set_invalid_field(ctsio, 5615 /*sks_valid*/ 1, 5616 /*command*/ 0, 5617 /*field*/ 2, 5618 /*bit_valid*/ 0, 5619 /*bit*/ 0); 5620 goto bailout; 5621 } 5622 } else { 5623 struct scsi_format_header_short *header; 5624 5625 header = (struct scsi_format_header_short *) 5626 ctsio->kern_data_ptr; 5627 5628 defect_list_len = scsi_2btoul(header->defect_list_len); 5629 if (defect_list_len != 0) { 5630 ctl_set_invalid_field(ctsio, 5631 /*sks_valid*/ 1, 5632 /*command*/ 0, 5633 /*field*/ 2, 5634 /*bit_valid*/ 0, 5635 /*bit*/ 0); 5636 goto bailout; 5637 } 5638 } 5639 } 5640 5641 /* 5642 * The format command will clear out the "Medium format corrupted" 5643 * status if set by the configuration code. That status is really 5644 * just a way to notify the host that we have lost the media, and 5645 * get them to issue a command that will basically make them think 5646 * they're blowing away the media. 5647 */ 5648 mtx_lock(&lun->lun_lock); 5649 lun->flags &= ~CTL_LUN_INOPERABLE; 5650 mtx_unlock(&lun->lun_lock); 5651 5652 ctl_set_success(ctsio); 5653bailout: 5654 5655 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5656 free(ctsio->kern_data_ptr, M_CTL); 5657 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5658 } 5659 5660 ctl_done((union ctl_io *)ctsio); 5661 return (CTL_RETVAL_COMPLETE); 5662} 5663 5664int 5665ctl_read_buffer(struct ctl_scsiio *ctsio) 5666{ 5667 struct scsi_read_buffer *cdb; 5668 struct ctl_lun *lun; 5669 int buffer_offset, len; 5670 static uint8_t descr[4]; 5671 static uint8_t echo_descr[4] = { 0 }; 5672 5673 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5674 5675 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5676 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5677 5678 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5679 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5680 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5681 ctl_set_invalid_field(ctsio, 5682 /*sks_valid*/ 1, 5683 /*command*/ 1, 5684 /*field*/ 1, 5685 /*bit_valid*/ 1, 5686 /*bit*/ 4); 5687 ctl_done((union ctl_io *)ctsio); 5688 return (CTL_RETVAL_COMPLETE); 5689 } 5690 5691 len = scsi_3btoul(cdb->length); 5692 buffer_offset = scsi_3btoul(cdb->offset); 5693 5694 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5695 ctl_set_invalid_field(ctsio, 5696 /*sks_valid*/ 1, 5697 /*command*/ 1, 5698 /*field*/ 6, 5699 /*bit_valid*/ 0, 5700 /*bit*/ 0); 5701 ctl_done((union ctl_io *)ctsio); 5702 return (CTL_RETVAL_COMPLETE); 5703 } 5704 5705 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5706 descr[0] = 0; 5707 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5708 ctsio->kern_data_ptr = descr; 5709 len = min(len, sizeof(descr)); 5710 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5711 ctsio->kern_data_ptr = echo_descr; 5712 len = min(len, sizeof(echo_descr)); 5713 } else { 5714 if (lun->write_buffer == NULL) { 5715 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5716 M_CTL, M_WAITOK); 5717 } 5718 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5719 } 5720 ctsio->kern_data_len = len; 5721 ctsio->kern_total_len = len; 5722 ctsio->kern_data_resid = 0; 5723 ctsio->kern_rel_offset = 0; 5724 ctsio->kern_sg_entries = 0; 5725 ctl_set_success(ctsio); 5726 ctsio->be_move_done = ctl_config_move_done; 5727 ctl_datamove((union ctl_io *)ctsio); 5728 return (CTL_RETVAL_COMPLETE); 5729} 5730 5731int 5732ctl_write_buffer(struct ctl_scsiio *ctsio) 5733{ 5734 struct scsi_write_buffer *cdb; 5735 struct ctl_lun *lun; 5736 int buffer_offset, len; 5737 5738 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5739 5740 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5741 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5742 5743 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5744 ctl_set_invalid_field(ctsio, 5745 /*sks_valid*/ 1, 5746 /*command*/ 1, 5747 /*field*/ 1, 5748 /*bit_valid*/ 1, 5749 /*bit*/ 4); 5750 ctl_done((union ctl_io *)ctsio); 5751 return (CTL_RETVAL_COMPLETE); 5752 } 5753 5754 len = scsi_3btoul(cdb->length); 5755 buffer_offset = scsi_3btoul(cdb->offset); 5756 5757 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5758 ctl_set_invalid_field(ctsio, 5759 /*sks_valid*/ 1, 5760 /*command*/ 1, 5761 /*field*/ 6, 5762 /*bit_valid*/ 0, 5763 /*bit*/ 0); 5764 ctl_done((union ctl_io *)ctsio); 5765 return (CTL_RETVAL_COMPLETE); 5766 } 5767 5768 /* 5769 * If we've got a kernel request that hasn't been malloced yet, 5770 * malloc it and tell the caller the data buffer is here. 5771 */ 5772 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5773 if (lun->write_buffer == NULL) { 5774 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5775 M_CTL, M_WAITOK); 5776 } 5777 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5778 ctsio->kern_data_len = len; 5779 ctsio->kern_total_len = len; 5780 ctsio->kern_data_resid = 0; 5781 ctsio->kern_rel_offset = 0; 5782 ctsio->kern_sg_entries = 0; 5783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5784 ctsio->be_move_done = ctl_config_move_done; 5785 ctl_datamove((union ctl_io *)ctsio); 5786 5787 return (CTL_RETVAL_COMPLETE); 5788 } 5789 5790 ctl_set_success(ctsio); 5791 ctl_done((union ctl_io *)ctsio); 5792 return (CTL_RETVAL_COMPLETE); 5793} 5794 5795int 5796ctl_write_same(struct ctl_scsiio *ctsio) 5797{ 5798 struct ctl_lun *lun; 5799 struct ctl_lba_len_flags *lbalen; 5800 uint64_t lba; 5801 uint32_t num_blocks; 5802 int len, retval; 5803 uint8_t byte2; 5804 5805 retval = CTL_RETVAL_COMPLETE; 5806 5807 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5808 5809 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5810 5811 switch (ctsio->cdb[0]) { 5812 case WRITE_SAME_10: { 5813 struct scsi_write_same_10 *cdb; 5814 5815 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5816 5817 lba = scsi_4btoul(cdb->addr); 5818 num_blocks = scsi_2btoul(cdb->length); 5819 byte2 = cdb->byte2; 5820 break; 5821 } 5822 case WRITE_SAME_16: { 5823 struct scsi_write_same_16 *cdb; 5824 5825 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5826 5827 lba = scsi_8btou64(cdb->addr); 5828 num_blocks = scsi_4btoul(cdb->length); 5829 byte2 = cdb->byte2; 5830 break; 5831 } 5832 default: 5833 /* 5834 * We got a command we don't support. This shouldn't 5835 * happen, commands should be filtered out above us. 5836 */ 5837 ctl_set_invalid_opcode(ctsio); 5838 ctl_done((union ctl_io *)ctsio); 5839 5840 return (CTL_RETVAL_COMPLETE); 5841 break; /* NOTREACHED */ 5842 } 5843 5844 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5845 if ((byte2 & SWS_UNMAP) == 0 && 5846 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5847 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5848 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5849 ctl_done((union ctl_io *)ctsio); 5850 return (CTL_RETVAL_COMPLETE); 5851 } 5852 5853 /* 5854 * The first check is to make sure we're in bounds, the second 5855 * check is to catch wrap-around problems. If the lba + num blocks 5856 * is less than the lba, then we've wrapped around and the block 5857 * range is invalid anyway. 5858 */ 5859 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5860 || ((lba + num_blocks) < lba)) { 5861 ctl_set_lba_out_of_range(ctsio); 5862 ctl_done((union ctl_io *)ctsio); 5863 return (CTL_RETVAL_COMPLETE); 5864 } 5865 5866 /* Zero number of blocks means "to the last logical block" */ 5867 if (num_blocks == 0) { 5868 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5869 ctl_set_invalid_field(ctsio, 5870 /*sks_valid*/ 0, 5871 /*command*/ 1, 5872 /*field*/ 0, 5873 /*bit_valid*/ 0, 5874 /*bit*/ 0); 5875 ctl_done((union ctl_io *)ctsio); 5876 return (CTL_RETVAL_COMPLETE); 5877 } 5878 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5879 } 5880 5881 len = lun->be_lun->blocksize; 5882 5883 /* 5884 * If we've got a kernel request that hasn't been malloced yet, 5885 * malloc it and tell the caller the data buffer is here. 5886 */ 5887 if ((byte2 & SWS_NDOB) == 0 && 5888 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5889 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5890 ctsio->kern_data_len = len; 5891 ctsio->kern_total_len = len; 5892 ctsio->kern_data_resid = 0; 5893 ctsio->kern_rel_offset = 0; 5894 ctsio->kern_sg_entries = 0; 5895 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5896 ctsio->be_move_done = ctl_config_move_done; 5897 ctl_datamove((union ctl_io *)ctsio); 5898 5899 return (CTL_RETVAL_COMPLETE); 5900 } 5901 5902 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5903 lbalen->lba = lba; 5904 lbalen->len = num_blocks; 5905 lbalen->flags = byte2; 5906 retval = lun->backend->config_write((union ctl_io *)ctsio); 5907 5908 return (retval); 5909} 5910 5911int 5912ctl_unmap(struct ctl_scsiio *ctsio) 5913{ 5914 struct ctl_lun *lun; 5915 struct scsi_unmap *cdb; 5916 struct ctl_ptr_len_flags *ptrlen; 5917 struct scsi_unmap_header *hdr; 5918 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5919 uint64_t lba; 5920 uint32_t num_blocks; 5921 int len, retval; 5922 uint8_t byte2; 5923 5924 retval = CTL_RETVAL_COMPLETE; 5925 5926 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5927 5928 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5929 cdb = (struct scsi_unmap *)ctsio->cdb; 5930 5931 len = scsi_2btoul(cdb->length); 5932 byte2 = cdb->byte2; 5933 5934 /* 5935 * If we've got a kernel request that hasn't been malloced yet, 5936 * malloc it and tell the caller the data buffer is here. 5937 */ 5938 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5939 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5940 ctsio->kern_data_len = len; 5941 ctsio->kern_total_len = len; 5942 ctsio->kern_data_resid = 0; 5943 ctsio->kern_rel_offset = 0; 5944 ctsio->kern_sg_entries = 0; 5945 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5946 ctsio->be_move_done = ctl_config_move_done; 5947 ctl_datamove((union ctl_io *)ctsio); 5948 5949 return (CTL_RETVAL_COMPLETE); 5950 } 5951 5952 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5953 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5954 if (len < sizeof (*hdr) || 5955 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5956 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5957 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5958 ctl_set_invalid_field(ctsio, 5959 /*sks_valid*/ 0, 5960 /*command*/ 0, 5961 /*field*/ 0, 5962 /*bit_valid*/ 0, 5963 /*bit*/ 0); 5964 goto done; 5965 } 5966 len = scsi_2btoul(hdr->desc_length); 5967 buf = (struct scsi_unmap_desc *)(hdr + 1); 5968 end = buf + len / sizeof(*buf); 5969 5970 endnz = buf; 5971 for (range = buf; range < end; range++) { 5972 lba = scsi_8btou64(range->lba); 5973 num_blocks = scsi_4btoul(range->length); 5974 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5975 || ((lba + num_blocks) < lba)) { 5976 ctl_set_lba_out_of_range(ctsio); 5977 ctl_done((union ctl_io *)ctsio); 5978 return (CTL_RETVAL_COMPLETE); 5979 } 5980 if (num_blocks != 0) 5981 endnz = range + 1; 5982 } 5983 5984 /* 5985 * Block backend can not handle zero last range. 5986 * Filter it out and return if there is nothing left. 5987 */ 5988 len = (uint8_t *)endnz - (uint8_t *)buf; 5989 if (len == 0) { 5990 ctl_set_success(ctsio); 5991 goto done; 5992 } 5993 5994 mtx_lock(&lun->lun_lock); 5995 ptrlen = (struct ctl_ptr_len_flags *) 5996 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5997 ptrlen->ptr = (void *)buf; 5998 ptrlen->len = len; 5999 ptrlen->flags = byte2; 6000 ctl_check_blocked(lun); 6001 mtx_unlock(&lun->lun_lock); 6002 6003 retval = lun->backend->config_write((union ctl_io *)ctsio); 6004 return (retval); 6005 6006done: 6007 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6008 free(ctsio->kern_data_ptr, M_CTL); 6009 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6010 } 6011 ctl_done((union ctl_io *)ctsio); 6012 return (CTL_RETVAL_COMPLETE); 6013} 6014 6015/* 6016 * Note that this function currently doesn't actually do anything inside 6017 * CTL to enforce things if the DQue bit is turned on. 6018 * 6019 * Also note that this function can't be used in the default case, because 6020 * the DQue bit isn't set in the changeable mask for the control mode page 6021 * anyway. This is just here as an example for how to implement a page 6022 * handler, and a placeholder in case we want to allow the user to turn 6023 * tagged queueing on and off. 6024 * 6025 * The D_SENSE bit handling is functional, however, and will turn 6026 * descriptor sense on and off for a given LUN. 6027 */ 6028int 6029ctl_control_page_handler(struct ctl_scsiio *ctsio, 6030 struct ctl_page_index *page_index, uint8_t *page_ptr) 6031{ 6032 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6033 struct ctl_lun *lun; 6034 struct ctl_softc *softc; 6035 int set_ua; 6036 uint32_t initidx; 6037 6038 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6039 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6040 set_ua = 0; 6041 6042 user_cp = (struct scsi_control_page *)page_ptr; 6043 current_cp = (struct scsi_control_page *) 6044 (page_index->page_data + (page_index->page_len * 6045 CTL_PAGE_CURRENT)); 6046 saved_cp = (struct scsi_control_page *) 6047 (page_index->page_data + (page_index->page_len * 6048 CTL_PAGE_SAVED)); 6049 6050 softc = control_softc; 6051 6052 mtx_lock(&lun->lun_lock); 6053 if (((current_cp->rlec & SCP_DSENSE) == 0) 6054 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6055 /* 6056 * Descriptor sense is currently turned off and the user 6057 * wants to turn it on. 6058 */ 6059 current_cp->rlec |= SCP_DSENSE; 6060 saved_cp->rlec |= SCP_DSENSE; 6061 lun->flags |= CTL_LUN_SENSE_DESC; 6062 set_ua = 1; 6063 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6064 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6065 /* 6066 * Descriptor sense is currently turned on, and the user 6067 * wants to turn it off. 6068 */ 6069 current_cp->rlec &= ~SCP_DSENSE; 6070 saved_cp->rlec &= ~SCP_DSENSE; 6071 lun->flags &= ~CTL_LUN_SENSE_DESC; 6072 set_ua = 1; 6073 } 6074 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6075 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6076 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6077 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6078 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6079 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6080 set_ua = 1; 6081 } 6082 if ((current_cp->eca_and_aen & SCP_SWP) != 6083 (user_cp->eca_and_aen & SCP_SWP)) { 6084 current_cp->eca_and_aen &= ~SCP_SWP; 6085 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6086 saved_cp->eca_and_aen &= ~SCP_SWP; 6087 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6088 set_ua = 1; 6089 } 6090 if (set_ua != 0) 6091 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6092 mtx_unlock(&lun->lun_lock); 6093 6094 return (0); 6095} 6096 6097int 6098ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6099 struct ctl_page_index *page_index, uint8_t *page_ptr) 6100{ 6101 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6102 struct ctl_lun *lun; 6103 int set_ua; 6104 uint32_t initidx; 6105 6106 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6107 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6108 set_ua = 0; 6109 6110 user_cp = (struct scsi_caching_page *)page_ptr; 6111 current_cp = (struct scsi_caching_page *) 6112 (page_index->page_data + (page_index->page_len * 6113 CTL_PAGE_CURRENT)); 6114 saved_cp = (struct scsi_caching_page *) 6115 (page_index->page_data + (page_index->page_len * 6116 CTL_PAGE_SAVED)); 6117 6118 mtx_lock(&lun->lun_lock); 6119 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6120 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6121 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6122 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6123 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6124 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6125 set_ua = 1; 6126 } 6127 if (set_ua != 0) 6128 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6129 mtx_unlock(&lun->lun_lock); 6130 6131 return (0); 6132} 6133 6134int 6135ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6136 struct ctl_page_index *page_index, 6137 uint8_t *page_ptr) 6138{ 6139 uint8_t *c; 6140 int i; 6141 6142 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6143 ctl_time_io_secs = 6144 (c[0] << 8) | 6145 (c[1] << 0) | 6146 0; 6147 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6148 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6149 printf("page data:"); 6150 for (i=0; i<8; i++) 6151 printf(" %.2x",page_ptr[i]); 6152 printf("\n"); 6153 return (0); 6154} 6155 6156int 6157ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6158 struct ctl_page_index *page_index, 6159 int pc) 6160{ 6161 struct copan_debugconf_subpage *page; 6162 6163 page = (struct copan_debugconf_subpage *)page_index->page_data + 6164 (page_index->page_len * pc); 6165 6166 switch (pc) { 6167 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6168 case SMS_PAGE_CTRL_DEFAULT >> 6: 6169 case SMS_PAGE_CTRL_SAVED >> 6: 6170 /* 6171 * We don't update the changable or default bits for this page. 6172 */ 6173 break; 6174 case SMS_PAGE_CTRL_CURRENT >> 6: 6175 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6176 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6177 break; 6178 default: 6179#ifdef NEEDTOPORT 6180 EPRINT(0, "Invalid PC %d!!", pc); 6181#endif /* NEEDTOPORT */ 6182 break; 6183 } 6184 return (0); 6185} 6186 6187 6188static int 6189ctl_do_mode_select(union ctl_io *io) 6190{ 6191 struct scsi_mode_page_header *page_header; 6192 struct ctl_page_index *page_index; 6193 struct ctl_scsiio *ctsio; 6194 int control_dev, page_len; 6195 int page_len_offset, page_len_size; 6196 union ctl_modepage_info *modepage_info; 6197 struct ctl_lun *lun; 6198 int *len_left, *len_used; 6199 int retval, i; 6200 6201 ctsio = &io->scsiio; 6202 page_index = NULL; 6203 page_len = 0; 6204 retval = CTL_RETVAL_COMPLETE; 6205 6206 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6207 6208 if (lun->be_lun->lun_type != T_DIRECT) 6209 control_dev = 1; 6210 else 6211 control_dev = 0; 6212 6213 modepage_info = (union ctl_modepage_info *) 6214 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6215 len_left = &modepage_info->header.len_left; 6216 len_used = &modepage_info->header.len_used; 6217 6218do_next_page: 6219 6220 page_header = (struct scsi_mode_page_header *) 6221 (ctsio->kern_data_ptr + *len_used); 6222 6223 if (*len_left == 0) { 6224 free(ctsio->kern_data_ptr, M_CTL); 6225 ctl_set_success(ctsio); 6226 ctl_done((union ctl_io *)ctsio); 6227 return (CTL_RETVAL_COMPLETE); 6228 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6229 6230 free(ctsio->kern_data_ptr, M_CTL); 6231 ctl_set_param_len_error(ctsio); 6232 ctl_done((union ctl_io *)ctsio); 6233 return (CTL_RETVAL_COMPLETE); 6234 6235 } else if ((page_header->page_code & SMPH_SPF) 6236 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6237 6238 free(ctsio->kern_data_ptr, M_CTL); 6239 ctl_set_param_len_error(ctsio); 6240 ctl_done((union ctl_io *)ctsio); 6241 return (CTL_RETVAL_COMPLETE); 6242 } 6243 6244 6245 /* 6246 * XXX KDM should we do something with the block descriptor? 6247 */ 6248 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6249 6250 if ((control_dev != 0) 6251 && (lun->mode_pages.index[i].page_flags & 6252 CTL_PAGE_FLAG_DISK_ONLY)) 6253 continue; 6254 6255 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6256 (page_header->page_code & SMPH_PC_MASK)) 6257 continue; 6258 6259 /* 6260 * If neither page has a subpage code, then we've got a 6261 * match. 6262 */ 6263 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6264 && ((page_header->page_code & SMPH_SPF) == 0)) { 6265 page_index = &lun->mode_pages.index[i]; 6266 page_len = page_header->page_length; 6267 break; 6268 } 6269 6270 /* 6271 * If both pages have subpages, then the subpage numbers 6272 * have to match. 6273 */ 6274 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6275 && (page_header->page_code & SMPH_SPF)) { 6276 struct scsi_mode_page_header_sp *sph; 6277 6278 sph = (struct scsi_mode_page_header_sp *)page_header; 6279 6280 if (lun->mode_pages.index[i].subpage == 6281 sph->subpage) { 6282 page_index = &lun->mode_pages.index[i]; 6283 page_len = scsi_2btoul(sph->page_length); 6284 break; 6285 } 6286 } 6287 } 6288 6289 /* 6290 * If we couldn't find the page, or if we don't have a mode select 6291 * handler for it, send back an error to the user. 6292 */ 6293 if ((page_index == NULL) 6294 || (page_index->select_handler == NULL)) { 6295 ctl_set_invalid_field(ctsio, 6296 /*sks_valid*/ 1, 6297 /*command*/ 0, 6298 /*field*/ *len_used, 6299 /*bit_valid*/ 0, 6300 /*bit*/ 0); 6301 free(ctsio->kern_data_ptr, M_CTL); 6302 ctl_done((union ctl_io *)ctsio); 6303 return (CTL_RETVAL_COMPLETE); 6304 } 6305 6306 if (page_index->page_code & SMPH_SPF) { 6307 page_len_offset = 2; 6308 page_len_size = 2; 6309 } else { 6310 page_len_size = 1; 6311 page_len_offset = 1; 6312 } 6313 6314 /* 6315 * If the length the initiator gives us isn't the one we specify in 6316 * the mode page header, or if they didn't specify enough data in 6317 * the CDB to avoid truncating this page, kick out the request. 6318 */ 6319 if ((page_len != (page_index->page_len - page_len_offset - 6320 page_len_size)) 6321 || (*len_left < page_index->page_len)) { 6322 6323 6324 ctl_set_invalid_field(ctsio, 6325 /*sks_valid*/ 1, 6326 /*command*/ 0, 6327 /*field*/ *len_used + page_len_offset, 6328 /*bit_valid*/ 0, 6329 /*bit*/ 0); 6330 free(ctsio->kern_data_ptr, M_CTL); 6331 ctl_done((union ctl_io *)ctsio); 6332 return (CTL_RETVAL_COMPLETE); 6333 } 6334 6335 /* 6336 * Run through the mode page, checking to make sure that the bits 6337 * the user changed are actually legal for him to change. 6338 */ 6339 for (i = 0; i < page_index->page_len; i++) { 6340 uint8_t *user_byte, *change_mask, *current_byte; 6341 int bad_bit; 6342 int j; 6343 6344 user_byte = (uint8_t *)page_header + i; 6345 change_mask = page_index->page_data + 6346 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6347 current_byte = page_index->page_data + 6348 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6349 6350 /* 6351 * Check to see whether the user set any bits in this byte 6352 * that he is not allowed to set. 6353 */ 6354 if ((*user_byte & ~(*change_mask)) == 6355 (*current_byte & ~(*change_mask))) 6356 continue; 6357 6358 /* 6359 * Go through bit by bit to determine which one is illegal. 6360 */ 6361 bad_bit = 0; 6362 for (j = 7; j >= 0; j--) { 6363 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6364 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6365 bad_bit = i; 6366 break; 6367 } 6368 } 6369 ctl_set_invalid_field(ctsio, 6370 /*sks_valid*/ 1, 6371 /*command*/ 0, 6372 /*field*/ *len_used + i, 6373 /*bit_valid*/ 1, 6374 /*bit*/ bad_bit); 6375 free(ctsio->kern_data_ptr, M_CTL); 6376 ctl_done((union ctl_io *)ctsio); 6377 return (CTL_RETVAL_COMPLETE); 6378 } 6379 6380 /* 6381 * Decrement these before we call the page handler, since we may 6382 * end up getting called back one way or another before the handler 6383 * returns to this context. 6384 */ 6385 *len_left -= page_index->page_len; 6386 *len_used += page_index->page_len; 6387 6388 retval = page_index->select_handler(ctsio, page_index, 6389 (uint8_t *)page_header); 6390 6391 /* 6392 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6393 * wait until this queued command completes to finish processing 6394 * the mode page. If it returns anything other than 6395 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6396 * already set the sense information, freed the data pointer, and 6397 * completed the io for us. 6398 */ 6399 if (retval != CTL_RETVAL_COMPLETE) 6400 goto bailout_no_done; 6401 6402 /* 6403 * If the initiator sent us more than one page, parse the next one. 6404 */ 6405 if (*len_left > 0) 6406 goto do_next_page; 6407 6408 ctl_set_success(ctsio); 6409 free(ctsio->kern_data_ptr, M_CTL); 6410 ctl_done((union ctl_io *)ctsio); 6411 6412bailout_no_done: 6413 6414 return (CTL_RETVAL_COMPLETE); 6415 6416} 6417 6418int 6419ctl_mode_select(struct ctl_scsiio *ctsio) 6420{ 6421 int param_len, pf, sp; 6422 int header_size, bd_len; 6423 int len_left, len_used; 6424 struct ctl_page_index *page_index; 6425 struct ctl_lun *lun; 6426 int control_dev, page_len; 6427 union ctl_modepage_info *modepage_info; 6428 int retval; 6429 6430 pf = 0; 6431 sp = 0; 6432 page_len = 0; 6433 len_used = 0; 6434 len_left = 0; 6435 retval = 0; 6436 bd_len = 0; 6437 page_index = NULL; 6438 6439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6440 6441 if (lun->be_lun->lun_type != T_DIRECT) 6442 control_dev = 1; 6443 else 6444 control_dev = 0; 6445 6446 switch (ctsio->cdb[0]) { 6447 case MODE_SELECT_6: { 6448 struct scsi_mode_select_6 *cdb; 6449 6450 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6451 6452 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6453 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6454 6455 param_len = cdb->length; 6456 header_size = sizeof(struct scsi_mode_header_6); 6457 break; 6458 } 6459 case MODE_SELECT_10: { 6460 struct scsi_mode_select_10 *cdb; 6461 6462 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6463 6464 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6465 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6466 6467 param_len = scsi_2btoul(cdb->length); 6468 header_size = sizeof(struct scsi_mode_header_10); 6469 break; 6470 } 6471 default: 6472 ctl_set_invalid_opcode(ctsio); 6473 ctl_done((union ctl_io *)ctsio); 6474 return (CTL_RETVAL_COMPLETE); 6475 break; /* NOTREACHED */ 6476 } 6477 6478 /* 6479 * From SPC-3: 6480 * "A parameter list length of zero indicates that the Data-Out Buffer 6481 * shall be empty. This condition shall not be considered as an error." 6482 */ 6483 if (param_len == 0) { 6484 ctl_set_success(ctsio); 6485 ctl_done((union ctl_io *)ctsio); 6486 return (CTL_RETVAL_COMPLETE); 6487 } 6488 6489 /* 6490 * Since we'll hit this the first time through, prior to 6491 * allocation, we don't need to free a data buffer here. 6492 */ 6493 if (param_len < header_size) { 6494 ctl_set_param_len_error(ctsio); 6495 ctl_done((union ctl_io *)ctsio); 6496 return (CTL_RETVAL_COMPLETE); 6497 } 6498 6499 /* 6500 * Allocate the data buffer and grab the user's data. In theory, 6501 * we shouldn't have to sanity check the parameter list length here 6502 * because the maximum size is 64K. We should be able to malloc 6503 * that much without too many problems. 6504 */ 6505 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6506 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6507 ctsio->kern_data_len = param_len; 6508 ctsio->kern_total_len = param_len; 6509 ctsio->kern_data_resid = 0; 6510 ctsio->kern_rel_offset = 0; 6511 ctsio->kern_sg_entries = 0; 6512 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6513 ctsio->be_move_done = ctl_config_move_done; 6514 ctl_datamove((union ctl_io *)ctsio); 6515 6516 return (CTL_RETVAL_COMPLETE); 6517 } 6518 6519 switch (ctsio->cdb[0]) { 6520 case MODE_SELECT_6: { 6521 struct scsi_mode_header_6 *mh6; 6522 6523 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6524 bd_len = mh6->blk_desc_len; 6525 break; 6526 } 6527 case MODE_SELECT_10: { 6528 struct scsi_mode_header_10 *mh10; 6529 6530 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6531 bd_len = scsi_2btoul(mh10->blk_desc_len); 6532 break; 6533 } 6534 default: 6535 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6536 break; 6537 } 6538 6539 if (param_len < (header_size + bd_len)) { 6540 free(ctsio->kern_data_ptr, M_CTL); 6541 ctl_set_param_len_error(ctsio); 6542 ctl_done((union ctl_io *)ctsio); 6543 return (CTL_RETVAL_COMPLETE); 6544 } 6545 6546 /* 6547 * Set the IO_CONT flag, so that if this I/O gets passed to 6548 * ctl_config_write_done(), it'll get passed back to 6549 * ctl_do_mode_select() for further processing, or completion if 6550 * we're all done. 6551 */ 6552 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6553 ctsio->io_cont = ctl_do_mode_select; 6554 6555 modepage_info = (union ctl_modepage_info *) 6556 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6557 6558 memset(modepage_info, 0, sizeof(*modepage_info)); 6559 6560 len_left = param_len - header_size - bd_len; 6561 len_used = header_size + bd_len; 6562 6563 modepage_info->header.len_left = len_left; 6564 modepage_info->header.len_used = len_used; 6565 6566 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6567} 6568 6569int 6570ctl_mode_sense(struct ctl_scsiio *ctsio) 6571{ 6572 struct ctl_lun *lun; 6573 int pc, page_code, dbd, llba, subpage; 6574 int alloc_len, page_len, header_len, total_len; 6575 struct scsi_mode_block_descr *block_desc; 6576 struct ctl_page_index *page_index; 6577 int control_dev; 6578 6579 dbd = 0; 6580 llba = 0; 6581 block_desc = NULL; 6582 page_index = NULL; 6583 6584 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6585 6586 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6587 6588 if (lun->be_lun->lun_type != T_DIRECT) 6589 control_dev = 1; 6590 else 6591 control_dev = 0; 6592 6593 switch (ctsio->cdb[0]) { 6594 case MODE_SENSE_6: { 6595 struct scsi_mode_sense_6 *cdb; 6596 6597 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6598 6599 header_len = sizeof(struct scsi_mode_hdr_6); 6600 if (cdb->byte2 & SMS_DBD) 6601 dbd = 1; 6602 else 6603 header_len += sizeof(struct scsi_mode_block_descr); 6604 6605 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6606 page_code = cdb->page & SMS_PAGE_CODE; 6607 subpage = cdb->subpage; 6608 alloc_len = cdb->length; 6609 break; 6610 } 6611 case MODE_SENSE_10: { 6612 struct scsi_mode_sense_10 *cdb; 6613 6614 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6615 6616 header_len = sizeof(struct scsi_mode_hdr_10); 6617 6618 if (cdb->byte2 & SMS_DBD) 6619 dbd = 1; 6620 else 6621 header_len += sizeof(struct scsi_mode_block_descr); 6622 if (cdb->byte2 & SMS10_LLBAA) 6623 llba = 1; 6624 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6625 page_code = cdb->page & SMS_PAGE_CODE; 6626 subpage = cdb->subpage; 6627 alloc_len = scsi_2btoul(cdb->length); 6628 break; 6629 } 6630 default: 6631 ctl_set_invalid_opcode(ctsio); 6632 ctl_done((union ctl_io *)ctsio); 6633 return (CTL_RETVAL_COMPLETE); 6634 break; /* NOTREACHED */ 6635 } 6636 6637 /* 6638 * We have to make a first pass through to calculate the size of 6639 * the pages that match the user's query. Then we allocate enough 6640 * memory to hold it, and actually copy the data into the buffer. 6641 */ 6642 switch (page_code) { 6643 case SMS_ALL_PAGES_PAGE: { 6644 int i; 6645 6646 page_len = 0; 6647 6648 /* 6649 * At the moment, values other than 0 and 0xff here are 6650 * reserved according to SPC-3. 6651 */ 6652 if ((subpage != SMS_SUBPAGE_PAGE_0) 6653 && (subpage != SMS_SUBPAGE_ALL)) { 6654 ctl_set_invalid_field(ctsio, 6655 /*sks_valid*/ 1, 6656 /*command*/ 1, 6657 /*field*/ 3, 6658 /*bit_valid*/ 0, 6659 /*bit*/ 0); 6660 ctl_done((union ctl_io *)ctsio); 6661 return (CTL_RETVAL_COMPLETE); 6662 } 6663 6664 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6665 if ((control_dev != 0) 6666 && (lun->mode_pages.index[i].page_flags & 6667 CTL_PAGE_FLAG_DISK_ONLY)) 6668 continue; 6669 6670 /* 6671 * We don't use this subpage if the user didn't 6672 * request all subpages. 6673 */ 6674 if ((lun->mode_pages.index[i].subpage != 0) 6675 && (subpage == SMS_SUBPAGE_PAGE_0)) 6676 continue; 6677 6678#if 0 6679 printf("found page %#x len %d\n", 6680 lun->mode_pages.index[i].page_code & 6681 SMPH_PC_MASK, 6682 lun->mode_pages.index[i].page_len); 6683#endif 6684 page_len += lun->mode_pages.index[i].page_len; 6685 } 6686 break; 6687 } 6688 default: { 6689 int i; 6690 6691 page_len = 0; 6692 6693 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6694 /* Look for the right page code */ 6695 if ((lun->mode_pages.index[i].page_code & 6696 SMPH_PC_MASK) != page_code) 6697 continue; 6698 6699 /* Look for the right subpage or the subpage wildcard*/ 6700 if ((lun->mode_pages.index[i].subpage != subpage) 6701 && (subpage != SMS_SUBPAGE_ALL)) 6702 continue; 6703 6704 /* Make sure the page is supported for this dev type */ 6705 if ((control_dev != 0) 6706 && (lun->mode_pages.index[i].page_flags & 6707 CTL_PAGE_FLAG_DISK_ONLY)) 6708 continue; 6709 6710#if 0 6711 printf("found page %#x len %d\n", 6712 lun->mode_pages.index[i].page_code & 6713 SMPH_PC_MASK, 6714 lun->mode_pages.index[i].page_len); 6715#endif 6716 6717 page_len += lun->mode_pages.index[i].page_len; 6718 } 6719 6720 if (page_len == 0) { 6721 ctl_set_invalid_field(ctsio, 6722 /*sks_valid*/ 1, 6723 /*command*/ 1, 6724 /*field*/ 2, 6725 /*bit_valid*/ 1, 6726 /*bit*/ 5); 6727 ctl_done((union ctl_io *)ctsio); 6728 return (CTL_RETVAL_COMPLETE); 6729 } 6730 break; 6731 } 6732 } 6733 6734 total_len = header_len + page_len; 6735#if 0 6736 printf("header_len = %d, page_len = %d, total_len = %d\n", 6737 header_len, page_len, total_len); 6738#endif 6739 6740 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6741 ctsio->kern_sg_entries = 0; 6742 ctsio->kern_data_resid = 0; 6743 ctsio->kern_rel_offset = 0; 6744 if (total_len < alloc_len) { 6745 ctsio->residual = alloc_len - total_len; 6746 ctsio->kern_data_len = total_len; 6747 ctsio->kern_total_len = total_len; 6748 } else { 6749 ctsio->residual = 0; 6750 ctsio->kern_data_len = alloc_len; 6751 ctsio->kern_total_len = alloc_len; 6752 } 6753 6754 switch (ctsio->cdb[0]) { 6755 case MODE_SENSE_6: { 6756 struct scsi_mode_hdr_6 *header; 6757 6758 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6759 6760 header->datalen = ctl_min(total_len - 1, 254); 6761 if (control_dev == 0) { 6762 header->dev_specific = 0x10; /* DPOFUA */ 6763 if ((lun->flags & CTL_LUN_READONLY) || 6764 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6765 .eca_and_aen & SCP_SWP) != 0) 6766 header->dev_specific |= 0x80; /* WP */ 6767 } 6768 if (dbd) 6769 header->block_descr_len = 0; 6770 else 6771 header->block_descr_len = 6772 sizeof(struct scsi_mode_block_descr); 6773 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6774 break; 6775 } 6776 case MODE_SENSE_10: { 6777 struct scsi_mode_hdr_10 *header; 6778 int datalen; 6779 6780 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6781 6782 datalen = ctl_min(total_len - 2, 65533); 6783 scsi_ulto2b(datalen, header->datalen); 6784 if (control_dev == 0) { 6785 header->dev_specific = 0x10; /* DPOFUA */ 6786 if ((lun->flags & CTL_LUN_READONLY) || 6787 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6788 .eca_and_aen & SCP_SWP) != 0) 6789 header->dev_specific |= 0x80; /* WP */ 6790 } 6791 if (dbd) 6792 scsi_ulto2b(0, header->block_descr_len); 6793 else 6794 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6795 header->block_descr_len); 6796 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6797 break; 6798 } 6799 default: 6800 panic("invalid CDB type %#x", ctsio->cdb[0]); 6801 break; /* NOTREACHED */ 6802 } 6803 6804 /* 6805 * If we've got a disk, use its blocksize in the block 6806 * descriptor. Otherwise, just set it to 0. 6807 */ 6808 if (dbd == 0) { 6809 if (control_dev == 0) 6810 scsi_ulto3b(lun->be_lun->blocksize, 6811 block_desc->block_len); 6812 else 6813 scsi_ulto3b(0, block_desc->block_len); 6814 } 6815 6816 switch (page_code) { 6817 case SMS_ALL_PAGES_PAGE: { 6818 int i, data_used; 6819 6820 data_used = header_len; 6821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6822 struct ctl_page_index *page_index; 6823 6824 page_index = &lun->mode_pages.index[i]; 6825 6826 if ((control_dev != 0) 6827 && (page_index->page_flags & 6828 CTL_PAGE_FLAG_DISK_ONLY)) 6829 continue; 6830 6831 /* 6832 * We don't use this subpage if the user didn't 6833 * request all subpages. We already checked (above) 6834 * to make sure the user only specified a subpage 6835 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6836 */ 6837 if ((page_index->subpage != 0) 6838 && (subpage == SMS_SUBPAGE_PAGE_0)) 6839 continue; 6840 6841 /* 6842 * Call the handler, if it exists, to update the 6843 * page to the latest values. 6844 */ 6845 if (page_index->sense_handler != NULL) 6846 page_index->sense_handler(ctsio, page_index,pc); 6847 6848 memcpy(ctsio->kern_data_ptr + data_used, 6849 page_index->page_data + 6850 (page_index->page_len * pc), 6851 page_index->page_len); 6852 data_used += page_index->page_len; 6853 } 6854 break; 6855 } 6856 default: { 6857 int i, data_used; 6858 6859 data_used = header_len; 6860 6861 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6862 struct ctl_page_index *page_index; 6863 6864 page_index = &lun->mode_pages.index[i]; 6865 6866 /* Look for the right page code */ 6867 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6868 continue; 6869 6870 /* Look for the right subpage or the subpage wildcard*/ 6871 if ((page_index->subpage != subpage) 6872 && (subpage != SMS_SUBPAGE_ALL)) 6873 continue; 6874 6875 /* Make sure the page is supported for this dev type */ 6876 if ((control_dev != 0) 6877 && (page_index->page_flags & 6878 CTL_PAGE_FLAG_DISK_ONLY)) 6879 continue; 6880 6881 /* 6882 * Call the handler, if it exists, to update the 6883 * page to the latest values. 6884 */ 6885 if (page_index->sense_handler != NULL) 6886 page_index->sense_handler(ctsio, page_index,pc); 6887 6888 memcpy(ctsio->kern_data_ptr + data_used, 6889 page_index->page_data + 6890 (page_index->page_len * pc), 6891 page_index->page_len); 6892 data_used += page_index->page_len; 6893 } 6894 break; 6895 } 6896 } 6897 6898 ctl_set_success(ctsio); 6899 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6900 ctsio->be_move_done = ctl_config_move_done; 6901 ctl_datamove((union ctl_io *)ctsio); 6902 return (CTL_RETVAL_COMPLETE); 6903} 6904 6905int 6906ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6907 struct ctl_page_index *page_index, 6908 int pc) 6909{ 6910 struct ctl_lun *lun; 6911 struct scsi_log_param_header *phdr; 6912 uint8_t *data; 6913 uint64_t val; 6914 6915 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6916 data = page_index->page_data; 6917 6918 if (lun->backend->lun_attr != NULL && 6919 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6920 != UINT64_MAX) { 6921 phdr = (struct scsi_log_param_header *)data; 6922 scsi_ulto2b(0x0001, phdr->param_code); 6923 phdr->param_control = SLP_LBIN | SLP_LP; 6924 phdr->param_len = 8; 6925 data = (uint8_t *)(phdr + 1); 6926 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6927 data[4] = 0x02; /* per-pool */ 6928 data += phdr->param_len; 6929 } 6930 6931 if (lun->backend->lun_attr != NULL && 6932 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6933 != UINT64_MAX) { 6934 phdr = (struct scsi_log_param_header *)data; 6935 scsi_ulto2b(0x0002, phdr->param_code); 6936 phdr->param_control = SLP_LBIN | SLP_LP; 6937 phdr->param_len = 8; 6938 data = (uint8_t *)(phdr + 1); 6939 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6940 data[4] = 0x01; /* per-LUN */ 6941 data += phdr->param_len; 6942 } 6943 6944 if (lun->backend->lun_attr != NULL && 6945 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6946 != UINT64_MAX) { 6947 phdr = (struct scsi_log_param_header *)data; 6948 scsi_ulto2b(0x00f1, phdr->param_code); 6949 phdr->param_control = SLP_LBIN | SLP_LP; 6950 phdr->param_len = 8; 6951 data = (uint8_t *)(phdr + 1); 6952 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6953 data[4] = 0x02; /* per-pool */ 6954 data += phdr->param_len; 6955 } 6956 6957 if (lun->backend->lun_attr != NULL && 6958 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6959 != UINT64_MAX) { 6960 phdr = (struct scsi_log_param_header *)data; 6961 scsi_ulto2b(0x00f2, phdr->param_code); 6962 phdr->param_control = SLP_LBIN | SLP_LP; 6963 phdr->param_len = 8; 6964 data = (uint8_t *)(phdr + 1); 6965 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6966 data[4] = 0x02; /* per-pool */ 6967 data += phdr->param_len; 6968 } 6969 6970 page_index->page_len = data - page_index->page_data; 6971 return (0); 6972} 6973 6974int 6975ctl_log_sense(struct ctl_scsiio *ctsio) 6976{ 6977 struct ctl_lun *lun; 6978 int i, pc, page_code, subpage; 6979 int alloc_len, total_len; 6980 struct ctl_page_index *page_index; 6981 struct scsi_log_sense *cdb; 6982 struct scsi_log_header *header; 6983 6984 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6985 6986 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6987 cdb = (struct scsi_log_sense *)ctsio->cdb; 6988 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6989 page_code = cdb->page & SLS_PAGE_CODE; 6990 subpage = cdb->subpage; 6991 alloc_len = scsi_2btoul(cdb->length); 6992 6993 page_index = NULL; 6994 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6995 page_index = &lun->log_pages.index[i]; 6996 6997 /* Look for the right page code */ 6998 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6999 continue; 7000 7001 /* Look for the right subpage or the subpage wildcard*/ 7002 if (page_index->subpage != subpage) 7003 continue; 7004 7005 break; 7006 } 7007 if (i >= CTL_NUM_LOG_PAGES) { 7008 ctl_set_invalid_field(ctsio, 7009 /*sks_valid*/ 1, 7010 /*command*/ 1, 7011 /*field*/ 2, 7012 /*bit_valid*/ 0, 7013 /*bit*/ 0); 7014 ctl_done((union ctl_io *)ctsio); 7015 return (CTL_RETVAL_COMPLETE); 7016 } 7017 7018 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7019 7020 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7021 ctsio->kern_sg_entries = 0; 7022 ctsio->kern_data_resid = 0; 7023 ctsio->kern_rel_offset = 0; 7024 if (total_len < alloc_len) { 7025 ctsio->residual = alloc_len - total_len; 7026 ctsio->kern_data_len = total_len; 7027 ctsio->kern_total_len = total_len; 7028 } else { 7029 ctsio->residual = 0; 7030 ctsio->kern_data_len = alloc_len; 7031 ctsio->kern_total_len = alloc_len; 7032 } 7033 7034 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7035 header->page = page_index->page_code; 7036 if (page_index->subpage) { 7037 header->page |= SL_SPF; 7038 header->subpage = page_index->subpage; 7039 } 7040 scsi_ulto2b(page_index->page_len, header->datalen); 7041 7042 /* 7043 * Call the handler, if it exists, to update the 7044 * page to the latest values. 7045 */ 7046 if (page_index->sense_handler != NULL) 7047 page_index->sense_handler(ctsio, page_index, pc); 7048 7049 memcpy(header + 1, page_index->page_data, page_index->page_len); 7050 7051 ctl_set_success(ctsio); 7052 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7053 ctsio->be_move_done = ctl_config_move_done; 7054 ctl_datamove((union ctl_io *)ctsio); 7055 return (CTL_RETVAL_COMPLETE); 7056} 7057 7058int 7059ctl_read_capacity(struct ctl_scsiio *ctsio) 7060{ 7061 struct scsi_read_capacity *cdb; 7062 struct scsi_read_capacity_data *data; 7063 struct ctl_lun *lun; 7064 uint32_t lba; 7065 7066 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7067 7068 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7069 7070 lba = scsi_4btoul(cdb->addr); 7071 if (((cdb->pmi & SRC_PMI) == 0) 7072 && (lba != 0)) { 7073 ctl_set_invalid_field(/*ctsio*/ ctsio, 7074 /*sks_valid*/ 1, 7075 /*command*/ 1, 7076 /*field*/ 2, 7077 /*bit_valid*/ 0, 7078 /*bit*/ 0); 7079 ctl_done((union ctl_io *)ctsio); 7080 return (CTL_RETVAL_COMPLETE); 7081 } 7082 7083 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7084 7085 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7086 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7087 ctsio->residual = 0; 7088 ctsio->kern_data_len = sizeof(*data); 7089 ctsio->kern_total_len = sizeof(*data); 7090 ctsio->kern_data_resid = 0; 7091 ctsio->kern_rel_offset = 0; 7092 ctsio->kern_sg_entries = 0; 7093 7094 /* 7095 * If the maximum LBA is greater than 0xfffffffe, the user must 7096 * issue a SERVICE ACTION IN (16) command, with the read capacity 7097 * serivce action set. 7098 */ 7099 if (lun->be_lun->maxlba > 0xfffffffe) 7100 scsi_ulto4b(0xffffffff, data->addr); 7101 else 7102 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7103 7104 /* 7105 * XXX KDM this may not be 512 bytes... 7106 */ 7107 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7108 7109 ctl_set_success(ctsio); 7110 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7111 ctsio->be_move_done = ctl_config_move_done; 7112 ctl_datamove((union ctl_io *)ctsio); 7113 return (CTL_RETVAL_COMPLETE); 7114} 7115 7116int 7117ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7118{ 7119 struct scsi_read_capacity_16 *cdb; 7120 struct scsi_read_capacity_data_long *data; 7121 struct ctl_lun *lun; 7122 uint64_t lba; 7123 uint32_t alloc_len; 7124 7125 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7126 7127 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7128 7129 alloc_len = scsi_4btoul(cdb->alloc_len); 7130 lba = scsi_8btou64(cdb->addr); 7131 7132 if ((cdb->reladr & SRC16_PMI) 7133 && (lba != 0)) { 7134 ctl_set_invalid_field(/*ctsio*/ ctsio, 7135 /*sks_valid*/ 1, 7136 /*command*/ 1, 7137 /*field*/ 2, 7138 /*bit_valid*/ 0, 7139 /*bit*/ 0); 7140 ctl_done((union ctl_io *)ctsio); 7141 return (CTL_RETVAL_COMPLETE); 7142 } 7143 7144 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7145 7146 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7147 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7148 7149 if (sizeof(*data) < alloc_len) { 7150 ctsio->residual = alloc_len - sizeof(*data); 7151 ctsio->kern_data_len = sizeof(*data); 7152 ctsio->kern_total_len = sizeof(*data); 7153 } else { 7154 ctsio->residual = 0; 7155 ctsio->kern_data_len = alloc_len; 7156 ctsio->kern_total_len = alloc_len; 7157 } 7158 ctsio->kern_data_resid = 0; 7159 ctsio->kern_rel_offset = 0; 7160 ctsio->kern_sg_entries = 0; 7161 7162 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7163 /* XXX KDM this may not be 512 bytes... */ 7164 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7165 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7166 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7167 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7168 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7169 7170 ctl_set_success(ctsio); 7171 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7172 ctsio->be_move_done = ctl_config_move_done; 7173 ctl_datamove((union ctl_io *)ctsio); 7174 return (CTL_RETVAL_COMPLETE); 7175} 7176 7177int 7178ctl_read_defect(struct ctl_scsiio *ctsio) 7179{ 7180 struct scsi_read_defect_data_10 *ccb10; 7181 struct scsi_read_defect_data_12 *ccb12; 7182 struct scsi_read_defect_data_hdr_10 *data10; 7183 struct scsi_read_defect_data_hdr_12 *data12; 7184 uint32_t alloc_len, data_len; 7185 uint8_t format; 7186 7187 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7188 7189 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7190 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7191 format = ccb10->format; 7192 alloc_len = scsi_2btoul(ccb10->alloc_length); 7193 data_len = sizeof(*data10); 7194 } else { 7195 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7196 format = ccb12->format; 7197 alloc_len = scsi_4btoul(ccb12->alloc_length); 7198 data_len = sizeof(*data12); 7199 } 7200 if (alloc_len == 0) { 7201 ctl_set_success(ctsio); 7202 ctl_done((union ctl_io *)ctsio); 7203 return (CTL_RETVAL_COMPLETE); 7204 } 7205 7206 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7207 if (data_len < alloc_len) { 7208 ctsio->residual = alloc_len - data_len; 7209 ctsio->kern_data_len = data_len; 7210 ctsio->kern_total_len = data_len; 7211 } else { 7212 ctsio->residual = 0; 7213 ctsio->kern_data_len = alloc_len; 7214 ctsio->kern_total_len = alloc_len; 7215 } 7216 ctsio->kern_data_resid = 0; 7217 ctsio->kern_rel_offset = 0; 7218 ctsio->kern_sg_entries = 0; 7219 7220 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7221 data10 = (struct scsi_read_defect_data_hdr_10 *) 7222 ctsio->kern_data_ptr; 7223 data10->format = format; 7224 scsi_ulto2b(0, data10->length); 7225 } else { 7226 data12 = (struct scsi_read_defect_data_hdr_12 *) 7227 ctsio->kern_data_ptr; 7228 data12->format = format; 7229 scsi_ulto2b(0, data12->generation); 7230 scsi_ulto4b(0, data12->length); 7231 } 7232 7233 ctl_set_success(ctsio); 7234 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7235 ctsio->be_move_done = ctl_config_move_done; 7236 ctl_datamove((union ctl_io *)ctsio); 7237 return (CTL_RETVAL_COMPLETE); 7238} 7239 7240int 7241ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7242{ 7243 struct scsi_maintenance_in *cdb; 7244 int retval; 7245 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7246 int num_target_port_groups, num_target_ports; 7247 struct ctl_lun *lun; 7248 struct ctl_softc *softc; 7249 struct ctl_port *port; 7250 struct scsi_target_group_data *rtg_ptr; 7251 struct scsi_target_group_data_extended *rtg_ext_ptr; 7252 struct scsi_target_port_group_descriptor *tpg_desc; 7253 7254 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7255 7256 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7257 softc = control_softc; 7258 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7259 7260 retval = CTL_RETVAL_COMPLETE; 7261 7262 switch (cdb->byte2 & STG_PDF_MASK) { 7263 case STG_PDF_LENGTH: 7264 ext = 0; 7265 break; 7266 case STG_PDF_EXTENDED: 7267 ext = 1; 7268 break; 7269 default: 7270 ctl_set_invalid_field(/*ctsio*/ ctsio, 7271 /*sks_valid*/ 1, 7272 /*command*/ 1, 7273 /*field*/ 2, 7274 /*bit_valid*/ 1, 7275 /*bit*/ 5); 7276 ctl_done((union ctl_io *)ctsio); 7277 return(retval); 7278 } 7279 7280 if (softc->is_single) 7281 num_target_port_groups = 1; 7282 else 7283 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7284 num_target_ports = 0; 7285 mtx_lock(&softc->ctl_lock); 7286 STAILQ_FOREACH(port, &softc->port_list, links) { 7287 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7288 continue; 7289 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7290 continue; 7291 num_target_ports++; 7292 } 7293 mtx_unlock(&softc->ctl_lock); 7294 7295 if (ext) 7296 total_len = sizeof(struct scsi_target_group_data_extended); 7297 else 7298 total_len = sizeof(struct scsi_target_group_data); 7299 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7300 num_target_port_groups + 7301 sizeof(struct scsi_target_port_descriptor) * 7302 num_target_ports * num_target_port_groups; 7303 7304 alloc_len = scsi_4btoul(cdb->length); 7305 7306 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7307 7308 ctsio->kern_sg_entries = 0; 7309 7310 if (total_len < alloc_len) { 7311 ctsio->residual = alloc_len - total_len; 7312 ctsio->kern_data_len = total_len; 7313 ctsio->kern_total_len = total_len; 7314 } else { 7315 ctsio->residual = 0; 7316 ctsio->kern_data_len = alloc_len; 7317 ctsio->kern_total_len = alloc_len; 7318 } 7319 ctsio->kern_data_resid = 0; 7320 ctsio->kern_rel_offset = 0; 7321 7322 if (ext) { 7323 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7324 ctsio->kern_data_ptr; 7325 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7326 rtg_ext_ptr->format_type = 0x10; 7327 rtg_ext_ptr->implicit_transition_time = 0; 7328 tpg_desc = &rtg_ext_ptr->groups[0]; 7329 } else { 7330 rtg_ptr = (struct scsi_target_group_data *) 7331 ctsio->kern_data_ptr; 7332 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7333 tpg_desc = &rtg_ptr->groups[0]; 7334 } 7335 7336 mtx_lock(&softc->ctl_lock); 7337 pg = softc->port_offset / CTL_MAX_PORTS; 7338 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7339 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7340 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7341 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7342 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7343 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7344 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7345 } else { 7346 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7347 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7348 } 7349 } else { 7350 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7351 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7352 } 7353 for (g = 0; g < num_target_port_groups; g++) { 7354 tpg_desc->pref_state = (g == pg) ? gs : os; 7355 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7356 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7357 tpg_desc->status = TPG_IMPLICIT; 7358 pc = 0; 7359 STAILQ_FOREACH(port, &softc->port_list, links) { 7360 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7361 continue; 7362 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7363 CTL_MAX_LUNS) 7364 continue; 7365 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7366 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7367 relative_target_port_identifier); 7368 pc++; 7369 } 7370 tpg_desc->target_port_count = pc; 7371 tpg_desc = (struct scsi_target_port_group_descriptor *) 7372 &tpg_desc->descriptors[pc]; 7373 } 7374 mtx_unlock(&softc->ctl_lock); 7375 7376 ctl_set_success(ctsio); 7377 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7378 ctsio->be_move_done = ctl_config_move_done; 7379 ctl_datamove((union ctl_io *)ctsio); 7380 return(retval); 7381} 7382 7383int 7384ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7385{ 7386 struct ctl_lun *lun; 7387 struct scsi_report_supported_opcodes *cdb; 7388 const struct ctl_cmd_entry *entry, *sentry; 7389 struct scsi_report_supported_opcodes_all *all; 7390 struct scsi_report_supported_opcodes_descr *descr; 7391 struct scsi_report_supported_opcodes_one *one; 7392 int retval; 7393 int alloc_len, total_len; 7394 int opcode, service_action, i, j, num; 7395 7396 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7397 7398 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7399 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7400 7401 retval = CTL_RETVAL_COMPLETE; 7402 7403 opcode = cdb->requested_opcode; 7404 service_action = scsi_2btoul(cdb->requested_service_action); 7405 switch (cdb->options & RSO_OPTIONS_MASK) { 7406 case RSO_OPTIONS_ALL: 7407 num = 0; 7408 for (i = 0; i < 256; i++) { 7409 entry = &ctl_cmd_table[i]; 7410 if (entry->flags & CTL_CMD_FLAG_SA5) { 7411 for (j = 0; j < 32; j++) { 7412 sentry = &((const struct ctl_cmd_entry *) 7413 entry->execute)[j]; 7414 if (ctl_cmd_applicable( 7415 lun->be_lun->lun_type, sentry)) 7416 num++; 7417 } 7418 } else { 7419 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7420 entry)) 7421 num++; 7422 } 7423 } 7424 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7425 num * sizeof(struct scsi_report_supported_opcodes_descr); 7426 break; 7427 case RSO_OPTIONS_OC: 7428 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7429 ctl_set_invalid_field(/*ctsio*/ ctsio, 7430 /*sks_valid*/ 1, 7431 /*command*/ 1, 7432 /*field*/ 2, 7433 /*bit_valid*/ 1, 7434 /*bit*/ 2); 7435 ctl_done((union ctl_io *)ctsio); 7436 return (CTL_RETVAL_COMPLETE); 7437 } 7438 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7439 break; 7440 case RSO_OPTIONS_OC_SA: 7441 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7442 service_action >= 32) { 7443 ctl_set_invalid_field(/*ctsio*/ ctsio, 7444 /*sks_valid*/ 1, 7445 /*command*/ 1, 7446 /*field*/ 2, 7447 /*bit_valid*/ 1, 7448 /*bit*/ 2); 7449 ctl_done((union ctl_io *)ctsio); 7450 return (CTL_RETVAL_COMPLETE); 7451 } 7452 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7453 break; 7454 default: 7455 ctl_set_invalid_field(/*ctsio*/ ctsio, 7456 /*sks_valid*/ 1, 7457 /*command*/ 1, 7458 /*field*/ 2, 7459 /*bit_valid*/ 1, 7460 /*bit*/ 2); 7461 ctl_done((union ctl_io *)ctsio); 7462 return (CTL_RETVAL_COMPLETE); 7463 } 7464 7465 alloc_len = scsi_4btoul(cdb->length); 7466 7467 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7468 7469 ctsio->kern_sg_entries = 0; 7470 7471 if (total_len < alloc_len) { 7472 ctsio->residual = alloc_len - total_len; 7473 ctsio->kern_data_len = total_len; 7474 ctsio->kern_total_len = total_len; 7475 } else { 7476 ctsio->residual = 0; 7477 ctsio->kern_data_len = alloc_len; 7478 ctsio->kern_total_len = alloc_len; 7479 } 7480 ctsio->kern_data_resid = 0; 7481 ctsio->kern_rel_offset = 0; 7482 7483 switch (cdb->options & RSO_OPTIONS_MASK) { 7484 case RSO_OPTIONS_ALL: 7485 all = (struct scsi_report_supported_opcodes_all *) 7486 ctsio->kern_data_ptr; 7487 num = 0; 7488 for (i = 0; i < 256; i++) { 7489 entry = &ctl_cmd_table[i]; 7490 if (entry->flags & CTL_CMD_FLAG_SA5) { 7491 for (j = 0; j < 32; j++) { 7492 sentry = &((const struct ctl_cmd_entry *) 7493 entry->execute)[j]; 7494 if (!ctl_cmd_applicable( 7495 lun->be_lun->lun_type, sentry)) 7496 continue; 7497 descr = &all->descr[num++]; 7498 descr->opcode = i; 7499 scsi_ulto2b(j, descr->service_action); 7500 descr->flags = RSO_SERVACTV; 7501 scsi_ulto2b(sentry->length, 7502 descr->cdb_length); 7503 } 7504 } else { 7505 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7506 entry)) 7507 continue; 7508 descr = &all->descr[num++]; 7509 descr->opcode = i; 7510 scsi_ulto2b(0, descr->service_action); 7511 descr->flags = 0; 7512 scsi_ulto2b(entry->length, descr->cdb_length); 7513 } 7514 } 7515 scsi_ulto4b( 7516 num * sizeof(struct scsi_report_supported_opcodes_descr), 7517 all->length); 7518 break; 7519 case RSO_OPTIONS_OC: 7520 one = (struct scsi_report_supported_opcodes_one *) 7521 ctsio->kern_data_ptr; 7522 entry = &ctl_cmd_table[opcode]; 7523 goto fill_one; 7524 case RSO_OPTIONS_OC_SA: 7525 one = (struct scsi_report_supported_opcodes_one *) 7526 ctsio->kern_data_ptr; 7527 entry = &ctl_cmd_table[opcode]; 7528 entry = &((const struct ctl_cmd_entry *) 7529 entry->execute)[service_action]; 7530fill_one: 7531 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7532 one->support = 3; 7533 scsi_ulto2b(entry->length, one->cdb_length); 7534 one->cdb_usage[0] = opcode; 7535 memcpy(&one->cdb_usage[1], entry->usage, 7536 entry->length - 1); 7537 } else 7538 one->support = 1; 7539 break; 7540 } 7541 7542 ctl_set_success(ctsio); 7543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7544 ctsio->be_move_done = ctl_config_move_done; 7545 ctl_datamove((union ctl_io *)ctsio); 7546 return(retval); 7547} 7548 7549int 7550ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7551{ 7552 struct scsi_report_supported_tmf *cdb; 7553 struct scsi_report_supported_tmf_data *data; 7554 int retval; 7555 int alloc_len, total_len; 7556 7557 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7558 7559 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7560 7561 retval = CTL_RETVAL_COMPLETE; 7562 7563 total_len = sizeof(struct scsi_report_supported_tmf_data); 7564 alloc_len = scsi_4btoul(cdb->length); 7565 7566 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7567 7568 ctsio->kern_sg_entries = 0; 7569 7570 if (total_len < alloc_len) { 7571 ctsio->residual = alloc_len - total_len; 7572 ctsio->kern_data_len = total_len; 7573 ctsio->kern_total_len = total_len; 7574 } else { 7575 ctsio->residual = 0; 7576 ctsio->kern_data_len = alloc_len; 7577 ctsio->kern_total_len = alloc_len; 7578 } 7579 ctsio->kern_data_resid = 0; 7580 ctsio->kern_rel_offset = 0; 7581 7582 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7583 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7584 data->byte2 |= RST_ITNRS; 7585 7586 ctl_set_success(ctsio); 7587 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7588 ctsio->be_move_done = ctl_config_move_done; 7589 ctl_datamove((union ctl_io *)ctsio); 7590 return (retval); 7591} 7592 7593int 7594ctl_report_timestamp(struct ctl_scsiio *ctsio) 7595{ 7596 struct scsi_report_timestamp *cdb; 7597 struct scsi_report_timestamp_data *data; 7598 struct timeval tv; 7599 int64_t timestamp; 7600 int retval; 7601 int alloc_len, total_len; 7602 7603 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7604 7605 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7606 7607 retval = CTL_RETVAL_COMPLETE; 7608 7609 total_len = sizeof(struct scsi_report_timestamp_data); 7610 alloc_len = scsi_4btoul(cdb->length); 7611 7612 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7613 7614 ctsio->kern_sg_entries = 0; 7615 7616 if (total_len < alloc_len) { 7617 ctsio->residual = alloc_len - total_len; 7618 ctsio->kern_data_len = total_len; 7619 ctsio->kern_total_len = total_len; 7620 } else { 7621 ctsio->residual = 0; 7622 ctsio->kern_data_len = alloc_len; 7623 ctsio->kern_total_len = alloc_len; 7624 } 7625 ctsio->kern_data_resid = 0; 7626 ctsio->kern_rel_offset = 0; 7627 7628 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7629 scsi_ulto2b(sizeof(*data) - 2, data->length); 7630 data->origin = RTS_ORIG_OUTSIDE; 7631 getmicrotime(&tv); 7632 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7633 scsi_ulto4b(timestamp >> 16, data->timestamp); 7634 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 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_persistent_reserve_in(struct ctl_scsiio *ctsio) 7645{ 7646 struct scsi_per_res_in *cdb; 7647 int alloc_len, total_len = 0; 7648 /* struct scsi_per_res_in_rsrv in_data; */ 7649 struct ctl_lun *lun; 7650 struct ctl_softc *softc; 7651 uint64_t key; 7652 7653 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7654 7655 softc = control_softc; 7656 7657 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7658 7659 alloc_len = scsi_2btoul(cdb->length); 7660 7661 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7662 7663retry: 7664 mtx_lock(&lun->lun_lock); 7665 switch (cdb->action) { 7666 case SPRI_RK: /* read keys */ 7667 total_len = sizeof(struct scsi_per_res_in_keys) + 7668 lun->pr_key_count * 7669 sizeof(struct scsi_per_res_key); 7670 break; 7671 case SPRI_RR: /* read reservation */ 7672 if (lun->flags & CTL_LUN_PR_RESERVED) 7673 total_len = sizeof(struct scsi_per_res_in_rsrv); 7674 else 7675 total_len = sizeof(struct scsi_per_res_in_header); 7676 break; 7677 case SPRI_RC: /* report capabilities */ 7678 total_len = sizeof(struct scsi_per_res_cap); 7679 break; 7680 case SPRI_RS: /* read full status */ 7681 total_len = sizeof(struct scsi_per_res_in_header) + 7682 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7683 lun->pr_key_count; 7684 break; 7685 default: 7686 panic("Invalid PR type %x", cdb->action); 7687 } 7688 mtx_unlock(&lun->lun_lock); 7689 7690 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7691 7692 if (total_len < alloc_len) { 7693 ctsio->residual = alloc_len - total_len; 7694 ctsio->kern_data_len = total_len; 7695 ctsio->kern_total_len = total_len; 7696 } else { 7697 ctsio->residual = 0; 7698 ctsio->kern_data_len = alloc_len; 7699 ctsio->kern_total_len = alloc_len; 7700 } 7701 7702 ctsio->kern_data_resid = 0; 7703 ctsio->kern_rel_offset = 0; 7704 ctsio->kern_sg_entries = 0; 7705 7706 mtx_lock(&lun->lun_lock); 7707 switch (cdb->action) { 7708 case SPRI_RK: { // read keys 7709 struct scsi_per_res_in_keys *res_keys; 7710 int i, key_count; 7711 7712 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7713 7714 /* 7715 * We had to drop the lock to allocate our buffer, which 7716 * leaves time for someone to come in with another 7717 * persistent reservation. (That is unlikely, though, 7718 * since this should be the only persistent reservation 7719 * command active right now.) 7720 */ 7721 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7722 (lun->pr_key_count * 7723 sizeof(struct scsi_per_res_key)))){ 7724 mtx_unlock(&lun->lun_lock); 7725 free(ctsio->kern_data_ptr, M_CTL); 7726 printf("%s: reservation length changed, retrying\n", 7727 __func__); 7728 goto retry; 7729 } 7730 7731 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7732 7733 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7734 lun->pr_key_count, res_keys->header.length); 7735 7736 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7737 if ((key = ctl_get_prkey(lun, i)) == 0) 7738 continue; 7739 7740 /* 7741 * We used lun->pr_key_count to calculate the 7742 * size to allocate. If it turns out the number of 7743 * initiators with the registered flag set is 7744 * larger than that (i.e. they haven't been kept in 7745 * sync), we've got a problem. 7746 */ 7747 if (key_count >= lun->pr_key_count) { 7748#ifdef NEEDTOPORT 7749 csevent_log(CSC_CTL | CSC_SHELF_SW | 7750 CTL_PR_ERROR, 7751 csevent_LogType_Fault, 7752 csevent_AlertLevel_Yellow, 7753 csevent_FRU_ShelfController, 7754 csevent_FRU_Firmware, 7755 csevent_FRU_Unknown, 7756 "registered keys %d >= key " 7757 "count %d", key_count, 7758 lun->pr_key_count); 7759#endif 7760 key_count++; 7761 continue; 7762 } 7763 scsi_u64to8b(key, res_keys->keys[key_count].key); 7764 key_count++; 7765 } 7766 break; 7767 } 7768 case SPRI_RR: { // read reservation 7769 struct scsi_per_res_in_rsrv *res; 7770 int tmp_len, header_only; 7771 7772 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7773 7774 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7775 7776 if (lun->flags & CTL_LUN_PR_RESERVED) 7777 { 7778 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7779 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7780 res->header.length); 7781 header_only = 0; 7782 } else { 7783 tmp_len = sizeof(struct scsi_per_res_in_header); 7784 scsi_ulto4b(0, res->header.length); 7785 header_only = 1; 7786 } 7787 7788 /* 7789 * We had to drop the lock to allocate our buffer, which 7790 * leaves time for someone to come in with another 7791 * persistent reservation. (That is unlikely, though, 7792 * since this should be the only persistent reservation 7793 * command active right now.) 7794 */ 7795 if (tmp_len != total_len) { 7796 mtx_unlock(&lun->lun_lock); 7797 free(ctsio->kern_data_ptr, M_CTL); 7798 printf("%s: reservation status changed, retrying\n", 7799 __func__); 7800 goto retry; 7801 } 7802 7803 /* 7804 * No reservation held, so we're done. 7805 */ 7806 if (header_only != 0) 7807 break; 7808 7809 /* 7810 * If the registration is an All Registrants type, the key 7811 * is 0, since it doesn't really matter. 7812 */ 7813 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7814 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7815 res->data.reservation); 7816 } 7817 res->data.scopetype = lun->res_type; 7818 break; 7819 } 7820 case SPRI_RC: //report capabilities 7821 { 7822 struct scsi_per_res_cap *res_cap; 7823 uint16_t type_mask; 7824 7825 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7826 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7827 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7828 type_mask = SPRI_TM_WR_EX_AR | 7829 SPRI_TM_EX_AC_RO | 7830 SPRI_TM_WR_EX_RO | 7831 SPRI_TM_EX_AC | 7832 SPRI_TM_WR_EX | 7833 SPRI_TM_EX_AC_AR; 7834 scsi_ulto2b(type_mask, res_cap->type_mask); 7835 break; 7836 } 7837 case SPRI_RS: { // read full status 7838 struct scsi_per_res_in_full *res_status; 7839 struct scsi_per_res_in_full_desc *res_desc; 7840 struct ctl_port *port; 7841 int i, len; 7842 7843 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7844 7845 /* 7846 * We had to drop the lock to allocate our buffer, which 7847 * leaves time for someone to come in with another 7848 * persistent reservation. (That is unlikely, though, 7849 * since this should be the only persistent reservation 7850 * command active right now.) 7851 */ 7852 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7853 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7854 lun->pr_key_count)){ 7855 mtx_unlock(&lun->lun_lock); 7856 free(ctsio->kern_data_ptr, M_CTL); 7857 printf("%s: reservation length changed, retrying\n", 7858 __func__); 7859 goto retry; 7860 } 7861 7862 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7863 7864 res_desc = &res_status->desc[0]; 7865 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7866 if ((key = ctl_get_prkey(lun, i)) == 0) 7867 continue; 7868 7869 scsi_u64to8b(key, res_desc->res_key.key); 7870 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7871 (lun->pr_res_idx == i || 7872 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7873 res_desc->flags = SPRI_FULL_R_HOLDER; 7874 res_desc->scopetype = lun->res_type; 7875 } 7876 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7877 res_desc->rel_trgt_port_id); 7878 len = 0; 7879 port = softc->ctl_ports[ 7880 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7881 if (port != NULL) 7882 len = ctl_create_iid(port, 7883 i % CTL_MAX_INIT_PER_PORT, 7884 res_desc->transport_id); 7885 scsi_ulto4b(len, res_desc->additional_length); 7886 res_desc = (struct scsi_per_res_in_full_desc *) 7887 &res_desc->transport_id[len]; 7888 } 7889 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7890 res_status->header.length); 7891 break; 7892 } 7893 default: 7894 /* 7895 * This is a bug, because we just checked for this above, 7896 * and should have returned an error. 7897 */ 7898 panic("Invalid PR type %x", cdb->action); 7899 break; /* NOTREACHED */ 7900 } 7901 mtx_unlock(&lun->lun_lock); 7902 7903 ctl_set_success(ctsio); 7904 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7905 ctsio->be_move_done = ctl_config_move_done; 7906 ctl_datamove((union ctl_io *)ctsio); 7907 return (CTL_RETVAL_COMPLETE); 7908} 7909 7910static void 7911ctl_set_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 7912{ 7913 int off = lun->ctl_softc->persis_offset; 7914 7915 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 7916 ctl_est_ua(lun, residx - off, ua); 7917} 7918 7919/* 7920 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7921 * it should return. 7922 */ 7923static int 7924ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7925 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7926 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7927 struct scsi_per_res_out_parms* param) 7928{ 7929 union ctl_ha_msg persis_io; 7930 int retval, i; 7931 int isc_retval; 7932 7933 retval = 0; 7934 7935 mtx_lock(&lun->lun_lock); 7936 if (sa_res_key == 0) { 7937 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7938 /* validate scope and type */ 7939 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7940 SPR_LU_SCOPE) { 7941 mtx_unlock(&lun->lun_lock); 7942 ctl_set_invalid_field(/*ctsio*/ ctsio, 7943 /*sks_valid*/ 1, 7944 /*command*/ 1, 7945 /*field*/ 2, 7946 /*bit_valid*/ 1, 7947 /*bit*/ 4); 7948 ctl_done((union ctl_io *)ctsio); 7949 return (1); 7950 } 7951 7952 if (type>8 || type==2 || type==4 || type==0) { 7953 mtx_unlock(&lun->lun_lock); 7954 ctl_set_invalid_field(/*ctsio*/ ctsio, 7955 /*sks_valid*/ 1, 7956 /*command*/ 1, 7957 /*field*/ 2, 7958 /*bit_valid*/ 1, 7959 /*bit*/ 0); 7960 ctl_done((union ctl_io *)ctsio); 7961 return (1); 7962 } 7963 7964 /* 7965 * Unregister everybody else and build UA for 7966 * them 7967 */ 7968 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7969 if (i == residx || ctl_get_prkey(lun, i) == 0) 7970 continue; 7971 7972 ctl_clr_prkey(lun, i); 7973 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7974 } 7975 lun->pr_key_count = 1; 7976 lun->res_type = type; 7977 if (lun->res_type != SPR_TYPE_WR_EX_AR 7978 && lun->res_type != SPR_TYPE_EX_AC_AR) 7979 lun->pr_res_idx = residx; 7980 7981 /* send msg to other side */ 7982 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7983 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7984 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7985 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7986 persis_io.pr.pr_info.res_type = type; 7987 memcpy(persis_io.pr.pr_info.sa_res_key, 7988 param->serv_act_res_key, 7989 sizeof(param->serv_act_res_key)); 7990 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7991 &persis_io, sizeof(persis_io), 0)) > 7992 CTL_HA_STATUS_SUCCESS) { 7993 printf("CTL:Persis Out error returned " 7994 "from ctl_ha_msg_send %d\n", 7995 isc_retval); 7996 } 7997 } else { 7998 /* not all registrants */ 7999 mtx_unlock(&lun->lun_lock); 8000 free(ctsio->kern_data_ptr, M_CTL); 8001 ctl_set_invalid_field(ctsio, 8002 /*sks_valid*/ 1, 8003 /*command*/ 0, 8004 /*field*/ 8, 8005 /*bit_valid*/ 0, 8006 /*bit*/ 0); 8007 ctl_done((union ctl_io *)ctsio); 8008 return (1); 8009 } 8010 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8011 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8012 int found = 0; 8013 8014 if (res_key == sa_res_key) { 8015 /* special case */ 8016 /* 8017 * The spec implies this is not good but doesn't 8018 * say what to do. There are two choices either 8019 * generate a res conflict or check condition 8020 * with illegal field in parameter data. Since 8021 * that is what is done when the sa_res_key is 8022 * zero I'll take that approach since this has 8023 * to do with the sa_res_key. 8024 */ 8025 mtx_unlock(&lun->lun_lock); 8026 free(ctsio->kern_data_ptr, M_CTL); 8027 ctl_set_invalid_field(ctsio, 8028 /*sks_valid*/ 1, 8029 /*command*/ 0, 8030 /*field*/ 8, 8031 /*bit_valid*/ 0, 8032 /*bit*/ 0); 8033 ctl_done((union ctl_io *)ctsio); 8034 return (1); 8035 } 8036 8037 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8038 if (ctl_get_prkey(lun, i) != sa_res_key) 8039 continue; 8040 8041 found = 1; 8042 ctl_clr_prkey(lun, i); 8043 lun->pr_key_count--; 8044 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8045 } 8046 if (!found) { 8047 mtx_unlock(&lun->lun_lock); 8048 free(ctsio->kern_data_ptr, M_CTL); 8049 ctl_set_reservation_conflict(ctsio); 8050 ctl_done((union ctl_io *)ctsio); 8051 return (CTL_RETVAL_COMPLETE); 8052 } 8053 /* send msg to other side */ 8054 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8055 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8056 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8057 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8058 persis_io.pr.pr_info.res_type = type; 8059 memcpy(persis_io.pr.pr_info.sa_res_key, 8060 param->serv_act_res_key, 8061 sizeof(param->serv_act_res_key)); 8062 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8063 &persis_io, sizeof(persis_io), 0)) > 8064 CTL_HA_STATUS_SUCCESS) { 8065 printf("CTL:Persis Out error returned from " 8066 "ctl_ha_msg_send %d\n", isc_retval); 8067 } 8068 } else { 8069 /* Reserved but not all registrants */ 8070 /* sa_res_key is res holder */ 8071 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8072 /* validate scope and type */ 8073 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8074 SPR_LU_SCOPE) { 8075 mtx_unlock(&lun->lun_lock); 8076 ctl_set_invalid_field(/*ctsio*/ ctsio, 8077 /*sks_valid*/ 1, 8078 /*command*/ 1, 8079 /*field*/ 2, 8080 /*bit_valid*/ 1, 8081 /*bit*/ 4); 8082 ctl_done((union ctl_io *)ctsio); 8083 return (1); 8084 } 8085 8086 if (type>8 || type==2 || type==4 || type==0) { 8087 mtx_unlock(&lun->lun_lock); 8088 ctl_set_invalid_field(/*ctsio*/ ctsio, 8089 /*sks_valid*/ 1, 8090 /*command*/ 1, 8091 /*field*/ 2, 8092 /*bit_valid*/ 1, 8093 /*bit*/ 0); 8094 ctl_done((union ctl_io *)ctsio); 8095 return (1); 8096 } 8097 8098 /* 8099 * Do the following: 8100 * if sa_res_key != res_key remove all 8101 * registrants w/sa_res_key and generate UA 8102 * for these registrants(Registrations 8103 * Preempted) if it wasn't an exclusive 8104 * reservation generate UA(Reservations 8105 * Preempted) for all other registered nexuses 8106 * if the type has changed. Establish the new 8107 * reservation and holder. If res_key and 8108 * sa_res_key are the same do the above 8109 * except don't unregister the res holder. 8110 */ 8111 8112 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8113 if (i == residx || ctl_get_prkey(lun, i) == 0) 8114 continue; 8115 8116 if (sa_res_key == ctl_get_prkey(lun, i)) { 8117 ctl_clr_prkey(lun, i); 8118 lun->pr_key_count--; 8119 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8120 } else if (type != lun->res_type 8121 && (lun->res_type == SPR_TYPE_WR_EX_RO 8122 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8123 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8124 } 8125 } 8126 lun->res_type = type; 8127 if (lun->res_type != SPR_TYPE_WR_EX_AR 8128 && lun->res_type != SPR_TYPE_EX_AC_AR) 8129 lun->pr_res_idx = residx; 8130 else 8131 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8132 8133 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8134 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8135 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8136 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8137 persis_io.pr.pr_info.res_type = type; 8138 memcpy(persis_io.pr.pr_info.sa_res_key, 8139 param->serv_act_res_key, 8140 sizeof(param->serv_act_res_key)); 8141 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8142 &persis_io, sizeof(persis_io), 0)) > 8143 CTL_HA_STATUS_SUCCESS) { 8144 printf("CTL:Persis Out error returned " 8145 "from ctl_ha_msg_send %d\n", 8146 isc_retval); 8147 } 8148 } else { 8149 /* 8150 * sa_res_key is not the res holder just 8151 * remove registrants 8152 */ 8153 int found=0; 8154 8155 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8156 if (sa_res_key != ctl_get_prkey(lun, i)) 8157 continue; 8158 8159 found = 1; 8160 ctl_clr_prkey(lun, i); 8161 lun->pr_key_count--; 8162 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8163 } 8164 8165 if (!found) { 8166 mtx_unlock(&lun->lun_lock); 8167 free(ctsio->kern_data_ptr, M_CTL); 8168 ctl_set_reservation_conflict(ctsio); 8169 ctl_done((union ctl_io *)ctsio); 8170 return (1); 8171 } 8172 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8173 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8174 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8175 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8176 persis_io.pr.pr_info.res_type = type; 8177 memcpy(persis_io.pr.pr_info.sa_res_key, 8178 param->serv_act_res_key, 8179 sizeof(param->serv_act_res_key)); 8180 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8181 &persis_io, sizeof(persis_io), 0)) > 8182 CTL_HA_STATUS_SUCCESS) { 8183 printf("CTL:Persis Out error returned " 8184 "from ctl_ha_msg_send %d\n", 8185 isc_retval); 8186 } 8187 } 8188 } 8189 8190 lun->PRGeneration++; 8191 mtx_unlock(&lun->lun_lock); 8192 8193 return (retval); 8194} 8195 8196static void 8197ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8198{ 8199 uint64_t sa_res_key; 8200 int i; 8201 8202 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8203 8204 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8205 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8206 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8207 if (sa_res_key == 0) { 8208 /* 8209 * Unregister everybody else and build UA for 8210 * them 8211 */ 8212 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8213 if (i == msg->pr.pr_info.residx || 8214 ctl_get_prkey(lun, i) == 0) 8215 continue; 8216 8217 ctl_clr_prkey(lun, i); 8218 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8219 } 8220 8221 lun->pr_key_count = 1; 8222 lun->res_type = msg->pr.pr_info.res_type; 8223 if (lun->res_type != SPR_TYPE_WR_EX_AR 8224 && lun->res_type != SPR_TYPE_EX_AC_AR) 8225 lun->pr_res_idx = msg->pr.pr_info.residx; 8226 } else { 8227 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8228 if (sa_res_key == ctl_get_prkey(lun, i)) 8229 continue; 8230 8231 ctl_clr_prkey(lun, i); 8232 lun->pr_key_count--; 8233 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8234 } 8235 } 8236 } else { 8237 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8238 if (i == msg->pr.pr_info.residx || 8239 ctl_get_prkey(lun, i) == 0) 8240 continue; 8241 8242 if (sa_res_key == ctl_get_prkey(lun, i)) { 8243 ctl_clr_prkey(lun, i); 8244 lun->pr_key_count--; 8245 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8246 } else if (msg->pr.pr_info.res_type != lun->res_type 8247 && (lun->res_type == SPR_TYPE_WR_EX_RO 8248 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8249 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8250 } 8251 } 8252 lun->res_type = msg->pr.pr_info.res_type; 8253 if (lun->res_type != SPR_TYPE_WR_EX_AR 8254 && lun->res_type != SPR_TYPE_EX_AC_AR) 8255 lun->pr_res_idx = msg->pr.pr_info.residx; 8256 else 8257 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8258 } 8259 lun->PRGeneration++; 8260 8261} 8262 8263 8264int 8265ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8266{ 8267 int retval; 8268 int isc_retval; 8269 u_int32_t param_len; 8270 struct scsi_per_res_out *cdb; 8271 struct ctl_lun *lun; 8272 struct scsi_per_res_out_parms* param; 8273 struct ctl_softc *softc; 8274 uint32_t residx; 8275 uint64_t res_key, sa_res_key, key; 8276 uint8_t type; 8277 union ctl_ha_msg persis_io; 8278 int i; 8279 8280 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8281 8282 retval = CTL_RETVAL_COMPLETE; 8283 8284 softc = control_softc; 8285 8286 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8287 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8288 8289 /* 8290 * We only support whole-LUN scope. The scope & type are ignored for 8291 * register, register and ignore existing key and clear. 8292 * We sometimes ignore scope and type on preempts too!! 8293 * Verify reservation type here as well. 8294 */ 8295 type = cdb->scope_type & SPR_TYPE_MASK; 8296 if ((cdb->action == SPRO_RESERVE) 8297 || (cdb->action == SPRO_RELEASE)) { 8298 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8299 ctl_set_invalid_field(/*ctsio*/ ctsio, 8300 /*sks_valid*/ 1, 8301 /*command*/ 1, 8302 /*field*/ 2, 8303 /*bit_valid*/ 1, 8304 /*bit*/ 4); 8305 ctl_done((union ctl_io *)ctsio); 8306 return (CTL_RETVAL_COMPLETE); 8307 } 8308 8309 if (type>8 || type==2 || type==4 || type==0) { 8310 ctl_set_invalid_field(/*ctsio*/ ctsio, 8311 /*sks_valid*/ 1, 8312 /*command*/ 1, 8313 /*field*/ 2, 8314 /*bit_valid*/ 1, 8315 /*bit*/ 0); 8316 ctl_done((union ctl_io *)ctsio); 8317 return (CTL_RETVAL_COMPLETE); 8318 } 8319 } 8320 8321 param_len = scsi_4btoul(cdb->length); 8322 8323 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8324 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8325 ctsio->kern_data_len = param_len; 8326 ctsio->kern_total_len = param_len; 8327 ctsio->kern_data_resid = 0; 8328 ctsio->kern_rel_offset = 0; 8329 ctsio->kern_sg_entries = 0; 8330 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8331 ctsio->be_move_done = ctl_config_move_done; 8332 ctl_datamove((union ctl_io *)ctsio); 8333 8334 return (CTL_RETVAL_COMPLETE); 8335 } 8336 8337 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8338 8339 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8340 res_key = scsi_8btou64(param->res_key.key); 8341 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8342 8343 /* 8344 * Validate the reservation key here except for SPRO_REG_IGNO 8345 * This must be done for all other service actions 8346 */ 8347 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8348 mtx_lock(&lun->lun_lock); 8349 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8350 if (res_key != key) { 8351 /* 8352 * The current key passed in doesn't match 8353 * the one the initiator previously 8354 * registered. 8355 */ 8356 mtx_unlock(&lun->lun_lock); 8357 free(ctsio->kern_data_ptr, M_CTL); 8358 ctl_set_reservation_conflict(ctsio); 8359 ctl_done((union ctl_io *)ctsio); 8360 return (CTL_RETVAL_COMPLETE); 8361 } 8362 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8363 /* 8364 * We are not registered 8365 */ 8366 mtx_unlock(&lun->lun_lock); 8367 free(ctsio->kern_data_ptr, M_CTL); 8368 ctl_set_reservation_conflict(ctsio); 8369 ctl_done((union ctl_io *)ctsio); 8370 return (CTL_RETVAL_COMPLETE); 8371 } else if (res_key != 0) { 8372 /* 8373 * We are not registered and trying to register but 8374 * the register key isn't zero. 8375 */ 8376 mtx_unlock(&lun->lun_lock); 8377 free(ctsio->kern_data_ptr, M_CTL); 8378 ctl_set_reservation_conflict(ctsio); 8379 ctl_done((union ctl_io *)ctsio); 8380 return (CTL_RETVAL_COMPLETE); 8381 } 8382 mtx_unlock(&lun->lun_lock); 8383 } 8384 8385 switch (cdb->action & SPRO_ACTION_MASK) { 8386 case SPRO_REGISTER: 8387 case SPRO_REG_IGNO: { 8388 8389#if 0 8390 printf("Registration received\n"); 8391#endif 8392 8393 /* 8394 * We don't support any of these options, as we report in 8395 * the read capabilities request (see 8396 * ctl_persistent_reserve_in(), above). 8397 */ 8398 if ((param->flags & SPR_SPEC_I_PT) 8399 || (param->flags & SPR_ALL_TG_PT) 8400 || (param->flags & SPR_APTPL)) { 8401 int bit_ptr; 8402 8403 if (param->flags & SPR_APTPL) 8404 bit_ptr = 0; 8405 else if (param->flags & SPR_ALL_TG_PT) 8406 bit_ptr = 2; 8407 else /* SPR_SPEC_I_PT */ 8408 bit_ptr = 3; 8409 8410 free(ctsio->kern_data_ptr, M_CTL); 8411 ctl_set_invalid_field(ctsio, 8412 /*sks_valid*/ 1, 8413 /*command*/ 0, 8414 /*field*/ 20, 8415 /*bit_valid*/ 1, 8416 /*bit*/ bit_ptr); 8417 ctl_done((union ctl_io *)ctsio); 8418 return (CTL_RETVAL_COMPLETE); 8419 } 8420 8421 mtx_lock(&lun->lun_lock); 8422 8423 /* 8424 * The initiator wants to clear the 8425 * key/unregister. 8426 */ 8427 if (sa_res_key == 0) { 8428 if ((res_key == 0 8429 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8430 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8431 && ctl_get_prkey(lun, residx) == 0)) { 8432 mtx_unlock(&lun->lun_lock); 8433 goto done; 8434 } 8435 8436 ctl_clr_prkey(lun, residx); 8437 lun->pr_key_count--; 8438 8439 if (residx == lun->pr_res_idx) { 8440 lun->flags &= ~CTL_LUN_PR_RESERVED; 8441 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8442 8443 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8444 || lun->res_type == SPR_TYPE_EX_AC_RO) 8445 && lun->pr_key_count) { 8446 /* 8447 * If the reservation is a registrants 8448 * only type we need to generate a UA 8449 * for other registered inits. The 8450 * sense code should be RESERVATIONS 8451 * RELEASED 8452 */ 8453 8454 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8455 if (ctl_get_prkey(lun, i + 8456 softc->persis_offset) == 0) 8457 continue; 8458 ctl_est_ua(lun, i, 8459 CTL_UA_RES_RELEASE); 8460 } 8461 } 8462 lun->res_type = 0; 8463 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8464 if (lun->pr_key_count==0) { 8465 lun->flags &= ~CTL_LUN_PR_RESERVED; 8466 lun->res_type = 0; 8467 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8468 } 8469 } 8470 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8471 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8472 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8473 persis_io.pr.pr_info.residx = residx; 8474 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8475 &persis_io, sizeof(persis_io), 0 )) > 8476 CTL_HA_STATUS_SUCCESS) { 8477 printf("CTL:Persis Out error returned from " 8478 "ctl_ha_msg_send %d\n", isc_retval); 8479 } 8480 } else /* sa_res_key != 0 */ { 8481 8482 /* 8483 * If we aren't registered currently then increment 8484 * the key count and set the registered flag. 8485 */ 8486 ctl_alloc_prkey(lun, residx); 8487 if (ctl_get_prkey(lun, residx) == 0) 8488 lun->pr_key_count++; 8489 ctl_set_prkey(lun, residx, sa_res_key); 8490 8491 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8492 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8493 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8494 persis_io.pr.pr_info.residx = residx; 8495 memcpy(persis_io.pr.pr_info.sa_res_key, 8496 param->serv_act_res_key, 8497 sizeof(param->serv_act_res_key)); 8498 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8499 &persis_io, sizeof(persis_io), 0)) > 8500 CTL_HA_STATUS_SUCCESS) { 8501 printf("CTL:Persis Out error returned from " 8502 "ctl_ha_msg_send %d\n", isc_retval); 8503 } 8504 } 8505 lun->PRGeneration++; 8506 mtx_unlock(&lun->lun_lock); 8507 8508 break; 8509 } 8510 case SPRO_RESERVE: 8511#if 0 8512 printf("Reserve executed type %d\n", type); 8513#endif 8514 mtx_lock(&lun->lun_lock); 8515 if (lun->flags & CTL_LUN_PR_RESERVED) { 8516 /* 8517 * if this isn't the reservation holder and it's 8518 * not a "all registrants" type or if the type is 8519 * different then we have a conflict 8520 */ 8521 if ((lun->pr_res_idx != residx 8522 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8523 || lun->res_type != type) { 8524 mtx_unlock(&lun->lun_lock); 8525 free(ctsio->kern_data_ptr, M_CTL); 8526 ctl_set_reservation_conflict(ctsio); 8527 ctl_done((union ctl_io *)ctsio); 8528 return (CTL_RETVAL_COMPLETE); 8529 } 8530 mtx_unlock(&lun->lun_lock); 8531 } else /* create a reservation */ { 8532 /* 8533 * If it's not an "all registrants" type record 8534 * reservation holder 8535 */ 8536 if (type != SPR_TYPE_WR_EX_AR 8537 && type != SPR_TYPE_EX_AC_AR) 8538 lun->pr_res_idx = residx; /* Res holder */ 8539 else 8540 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8541 8542 lun->flags |= CTL_LUN_PR_RESERVED; 8543 lun->res_type = type; 8544 8545 mtx_unlock(&lun->lun_lock); 8546 8547 /* send msg to other side */ 8548 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8549 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8550 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8551 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8552 persis_io.pr.pr_info.res_type = type; 8553 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8554 &persis_io, sizeof(persis_io), 0)) > 8555 CTL_HA_STATUS_SUCCESS) { 8556 printf("CTL:Persis Out error returned from " 8557 "ctl_ha_msg_send %d\n", isc_retval); 8558 } 8559 } 8560 break; 8561 8562 case SPRO_RELEASE: 8563 mtx_lock(&lun->lun_lock); 8564 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8565 /* No reservation exists return good status */ 8566 mtx_unlock(&lun->lun_lock); 8567 goto done; 8568 } 8569 /* 8570 * Is this nexus a reservation holder? 8571 */ 8572 if (lun->pr_res_idx != residx 8573 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8574 /* 8575 * not a res holder return good status but 8576 * do nothing 8577 */ 8578 mtx_unlock(&lun->lun_lock); 8579 goto done; 8580 } 8581 8582 if (lun->res_type != type) { 8583 mtx_unlock(&lun->lun_lock); 8584 free(ctsio->kern_data_ptr, M_CTL); 8585 ctl_set_illegal_pr_release(ctsio); 8586 ctl_done((union ctl_io *)ctsio); 8587 return (CTL_RETVAL_COMPLETE); 8588 } 8589 8590 /* okay to release */ 8591 lun->flags &= ~CTL_LUN_PR_RESERVED; 8592 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8593 lun->res_type = 0; 8594 8595 /* 8596 * if this isn't an exclusive access 8597 * res generate UA for all other 8598 * registrants. 8599 */ 8600 if (type != SPR_TYPE_EX_AC 8601 && type != SPR_TYPE_WR_EX) { 8602 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8603 if (i == residx || 8604 ctl_get_prkey(lun, 8605 i + softc->persis_offset) == 0) 8606 continue; 8607 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8608 } 8609 } 8610 mtx_unlock(&lun->lun_lock); 8611 /* Send msg to other side */ 8612 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8613 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8614 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8615 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8616 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8617 printf("CTL:Persis Out error returned from " 8618 "ctl_ha_msg_send %d\n", isc_retval); 8619 } 8620 break; 8621 8622 case SPRO_CLEAR: 8623 /* send msg to other side */ 8624 8625 mtx_lock(&lun->lun_lock); 8626 lun->flags &= ~CTL_LUN_PR_RESERVED; 8627 lun->res_type = 0; 8628 lun->pr_key_count = 0; 8629 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8630 8631 ctl_clr_prkey(lun, residx); 8632 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8633 if (ctl_get_prkey(lun, i) != 0) { 8634 ctl_clr_prkey(lun, i); 8635 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8636 } 8637 lun->PRGeneration++; 8638 mtx_unlock(&lun->lun_lock); 8639 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8640 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8641 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8642 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8643 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8644 printf("CTL:Persis Out error returned from " 8645 "ctl_ha_msg_send %d\n", isc_retval); 8646 } 8647 break; 8648 8649 case SPRO_PREEMPT: 8650 case SPRO_PRE_ABO: { 8651 int nretval; 8652 8653 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8654 residx, ctsio, cdb, param); 8655 if (nretval != 0) 8656 return (CTL_RETVAL_COMPLETE); 8657 break; 8658 } 8659 default: 8660 panic("Invalid PR type %x", cdb->action); 8661 } 8662 8663done: 8664 free(ctsio->kern_data_ptr, M_CTL); 8665 ctl_set_success(ctsio); 8666 ctl_done((union ctl_io *)ctsio); 8667 8668 return (retval); 8669} 8670 8671/* 8672 * This routine is for handling a message from the other SC pertaining to 8673 * persistent reserve out. All the error checking will have been done 8674 * so only perorming the action need be done here to keep the two 8675 * in sync. 8676 */ 8677static void 8678ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8679{ 8680 struct ctl_lun *lun; 8681 struct ctl_softc *softc; 8682 int i; 8683 uint32_t targ_lun; 8684 8685 softc = control_softc; 8686 8687 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8688 lun = softc->ctl_luns[targ_lun]; 8689 mtx_lock(&lun->lun_lock); 8690 switch(msg->pr.pr_info.action) { 8691 case CTL_PR_REG_KEY: 8692 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8693 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8694 lun->pr_key_count++; 8695 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8696 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8697 lun->PRGeneration++; 8698 break; 8699 8700 case CTL_PR_UNREG_KEY: 8701 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8702 lun->pr_key_count--; 8703 8704 /* XXX Need to see if the reservation has been released */ 8705 /* if so do we need to generate UA? */ 8706 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8707 lun->flags &= ~CTL_LUN_PR_RESERVED; 8708 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8709 8710 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8711 || lun->res_type == SPR_TYPE_EX_AC_RO) 8712 && lun->pr_key_count) { 8713 /* 8714 * If the reservation is a registrants 8715 * only type we need to generate a UA 8716 * for other registered inits. The 8717 * sense code should be RESERVATIONS 8718 * RELEASED 8719 */ 8720 8721 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8722 if (ctl_get_prkey(lun, i + 8723 softc->persis_offset) == 0) 8724 continue; 8725 8726 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8727 } 8728 } 8729 lun->res_type = 0; 8730 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8731 if (lun->pr_key_count==0) { 8732 lun->flags &= ~CTL_LUN_PR_RESERVED; 8733 lun->res_type = 0; 8734 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8735 } 8736 } 8737 lun->PRGeneration++; 8738 break; 8739 8740 case CTL_PR_RESERVE: 8741 lun->flags |= CTL_LUN_PR_RESERVED; 8742 lun->res_type = msg->pr.pr_info.res_type; 8743 lun->pr_res_idx = msg->pr.pr_info.residx; 8744 8745 break; 8746 8747 case CTL_PR_RELEASE: 8748 /* 8749 * if this isn't an exclusive access res generate UA for all 8750 * other registrants. 8751 */ 8752 if (lun->res_type != SPR_TYPE_EX_AC 8753 && lun->res_type != SPR_TYPE_WR_EX) { 8754 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8755 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8756 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8757 } 8758 8759 lun->flags &= ~CTL_LUN_PR_RESERVED; 8760 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8761 lun->res_type = 0; 8762 break; 8763 8764 case CTL_PR_PREEMPT: 8765 ctl_pro_preempt_other(lun, msg); 8766 break; 8767 case CTL_PR_CLEAR: 8768 lun->flags &= ~CTL_LUN_PR_RESERVED; 8769 lun->res_type = 0; 8770 lun->pr_key_count = 0; 8771 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8772 8773 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8774 if (ctl_get_prkey(lun, i) == 0) 8775 continue; 8776 ctl_clr_prkey(lun, i); 8777 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8778 } 8779 lun->PRGeneration++; 8780 break; 8781 } 8782 8783 mtx_unlock(&lun->lun_lock); 8784} 8785 8786int 8787ctl_read_write(struct ctl_scsiio *ctsio) 8788{ 8789 struct ctl_lun *lun; 8790 struct ctl_lba_len_flags *lbalen; 8791 uint64_t lba; 8792 uint32_t num_blocks; 8793 int flags, retval; 8794 int isread; 8795 8796 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8797 8798 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8799 8800 flags = 0; 8801 retval = CTL_RETVAL_COMPLETE; 8802 8803 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8804 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8805 switch (ctsio->cdb[0]) { 8806 case READ_6: 8807 case WRITE_6: { 8808 struct scsi_rw_6 *cdb; 8809 8810 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8811 8812 lba = scsi_3btoul(cdb->addr); 8813 /* only 5 bits are valid in the most significant address byte */ 8814 lba &= 0x1fffff; 8815 num_blocks = cdb->length; 8816 /* 8817 * This is correct according to SBC-2. 8818 */ 8819 if (num_blocks == 0) 8820 num_blocks = 256; 8821 break; 8822 } 8823 case READ_10: 8824 case WRITE_10: { 8825 struct scsi_rw_10 *cdb; 8826 8827 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8828 if (cdb->byte2 & SRW10_FUA) 8829 flags |= CTL_LLF_FUA; 8830 if (cdb->byte2 & SRW10_DPO) 8831 flags |= CTL_LLF_DPO; 8832 lba = scsi_4btoul(cdb->addr); 8833 num_blocks = scsi_2btoul(cdb->length); 8834 break; 8835 } 8836 case WRITE_VERIFY_10: { 8837 struct scsi_write_verify_10 *cdb; 8838 8839 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8840 flags |= CTL_LLF_FUA; 8841 if (cdb->byte2 & SWV_DPO) 8842 flags |= CTL_LLF_DPO; 8843 lba = scsi_4btoul(cdb->addr); 8844 num_blocks = scsi_2btoul(cdb->length); 8845 break; 8846 } 8847 case READ_12: 8848 case WRITE_12: { 8849 struct scsi_rw_12 *cdb; 8850 8851 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8852 if (cdb->byte2 & SRW12_FUA) 8853 flags |= CTL_LLF_FUA; 8854 if (cdb->byte2 & SRW12_DPO) 8855 flags |= CTL_LLF_DPO; 8856 lba = scsi_4btoul(cdb->addr); 8857 num_blocks = scsi_4btoul(cdb->length); 8858 break; 8859 } 8860 case WRITE_VERIFY_12: { 8861 struct scsi_write_verify_12 *cdb; 8862 8863 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8864 flags |= CTL_LLF_FUA; 8865 if (cdb->byte2 & SWV_DPO) 8866 flags |= CTL_LLF_DPO; 8867 lba = scsi_4btoul(cdb->addr); 8868 num_blocks = scsi_4btoul(cdb->length); 8869 break; 8870 } 8871 case READ_16: 8872 case WRITE_16: { 8873 struct scsi_rw_16 *cdb; 8874 8875 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8876 if (cdb->byte2 & SRW12_FUA) 8877 flags |= CTL_LLF_FUA; 8878 if (cdb->byte2 & SRW12_DPO) 8879 flags |= CTL_LLF_DPO; 8880 lba = scsi_8btou64(cdb->addr); 8881 num_blocks = scsi_4btoul(cdb->length); 8882 break; 8883 } 8884 case WRITE_ATOMIC_16: { 8885 struct scsi_rw_16 *cdb; 8886 8887 if (lun->be_lun->atomicblock == 0) { 8888 ctl_set_invalid_opcode(ctsio); 8889 ctl_done((union ctl_io *)ctsio); 8890 return (CTL_RETVAL_COMPLETE); 8891 } 8892 8893 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8894 if (cdb->byte2 & SRW12_FUA) 8895 flags |= CTL_LLF_FUA; 8896 if (cdb->byte2 & SRW12_DPO) 8897 flags |= CTL_LLF_DPO; 8898 lba = scsi_8btou64(cdb->addr); 8899 num_blocks = scsi_4btoul(cdb->length); 8900 if (num_blocks > lun->be_lun->atomicblock) { 8901 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8902 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8903 /*bit*/ 0); 8904 ctl_done((union ctl_io *)ctsio); 8905 return (CTL_RETVAL_COMPLETE); 8906 } 8907 break; 8908 } 8909 case WRITE_VERIFY_16: { 8910 struct scsi_write_verify_16 *cdb; 8911 8912 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8913 flags |= CTL_LLF_FUA; 8914 if (cdb->byte2 & SWV_DPO) 8915 flags |= CTL_LLF_DPO; 8916 lba = scsi_8btou64(cdb->addr); 8917 num_blocks = scsi_4btoul(cdb->length); 8918 break; 8919 } 8920 default: 8921 /* 8922 * We got a command we don't support. This shouldn't 8923 * happen, commands should be filtered out above us. 8924 */ 8925 ctl_set_invalid_opcode(ctsio); 8926 ctl_done((union ctl_io *)ctsio); 8927 8928 return (CTL_RETVAL_COMPLETE); 8929 break; /* NOTREACHED */ 8930 } 8931 8932 /* 8933 * The first check is to make sure we're in bounds, the second 8934 * check is to catch wrap-around problems. If the lba + num blocks 8935 * is less than the lba, then we've wrapped around and the block 8936 * range is invalid anyway. 8937 */ 8938 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8939 || ((lba + num_blocks) < lba)) { 8940 ctl_set_lba_out_of_range(ctsio); 8941 ctl_done((union ctl_io *)ctsio); 8942 return (CTL_RETVAL_COMPLETE); 8943 } 8944 8945 /* 8946 * According to SBC-3, a transfer length of 0 is not an error. 8947 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8948 * translates to 256 blocks for those commands. 8949 */ 8950 if (num_blocks == 0) { 8951 ctl_set_success(ctsio); 8952 ctl_done((union ctl_io *)ctsio); 8953 return (CTL_RETVAL_COMPLETE); 8954 } 8955 8956 /* Set FUA and/or DPO if caches are disabled. */ 8957 if (isread) { 8958 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8959 SCP_RCD) != 0) 8960 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8961 } else { 8962 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8963 SCP_WCE) == 0) 8964 flags |= CTL_LLF_FUA; 8965 } 8966 8967 lbalen = (struct ctl_lba_len_flags *) 8968 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8969 lbalen->lba = lba; 8970 lbalen->len = num_blocks; 8971 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8972 8973 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8974 ctsio->kern_rel_offset = 0; 8975 8976 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8977 8978 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8979 8980 return (retval); 8981} 8982 8983static int 8984ctl_cnw_cont(union ctl_io *io) 8985{ 8986 struct ctl_scsiio *ctsio; 8987 struct ctl_lun *lun; 8988 struct ctl_lba_len_flags *lbalen; 8989 int retval; 8990 8991 ctsio = &io->scsiio; 8992 ctsio->io_hdr.status = CTL_STATUS_NONE; 8993 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8994 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8995 lbalen = (struct ctl_lba_len_flags *) 8996 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8997 lbalen->flags &= ~CTL_LLF_COMPARE; 8998 lbalen->flags |= CTL_LLF_WRITE; 8999 9000 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9001 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9002 return (retval); 9003} 9004 9005int 9006ctl_cnw(struct ctl_scsiio *ctsio) 9007{ 9008 struct ctl_lun *lun; 9009 struct ctl_lba_len_flags *lbalen; 9010 uint64_t lba; 9011 uint32_t num_blocks; 9012 int flags, retval; 9013 9014 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9015 9016 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9017 9018 flags = 0; 9019 retval = CTL_RETVAL_COMPLETE; 9020 9021 switch (ctsio->cdb[0]) { 9022 case COMPARE_AND_WRITE: { 9023 struct scsi_compare_and_write *cdb; 9024 9025 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9026 if (cdb->byte2 & SRW10_FUA) 9027 flags |= CTL_LLF_FUA; 9028 if (cdb->byte2 & SRW10_DPO) 9029 flags |= CTL_LLF_DPO; 9030 lba = scsi_8btou64(cdb->addr); 9031 num_blocks = cdb->length; 9032 break; 9033 } 9034 default: 9035 /* 9036 * We got a command we don't support. This shouldn't 9037 * happen, commands should be filtered out above us. 9038 */ 9039 ctl_set_invalid_opcode(ctsio); 9040 ctl_done((union ctl_io *)ctsio); 9041 9042 return (CTL_RETVAL_COMPLETE); 9043 break; /* NOTREACHED */ 9044 } 9045 9046 /* 9047 * The first check is to make sure we're in bounds, the second 9048 * check is to catch wrap-around problems. If the lba + num blocks 9049 * is less than the lba, then we've wrapped around and the block 9050 * range is invalid anyway. 9051 */ 9052 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9053 || ((lba + num_blocks) < lba)) { 9054 ctl_set_lba_out_of_range(ctsio); 9055 ctl_done((union ctl_io *)ctsio); 9056 return (CTL_RETVAL_COMPLETE); 9057 } 9058 9059 /* 9060 * According to SBC-3, a transfer length of 0 is not an error. 9061 */ 9062 if (num_blocks == 0) { 9063 ctl_set_success(ctsio); 9064 ctl_done((union ctl_io *)ctsio); 9065 return (CTL_RETVAL_COMPLETE); 9066 } 9067 9068 /* Set FUA if write cache is disabled. */ 9069 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9070 SCP_WCE) == 0) 9071 flags |= CTL_LLF_FUA; 9072 9073 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9074 ctsio->kern_rel_offset = 0; 9075 9076 /* 9077 * Set the IO_CONT flag, so that if this I/O gets passed to 9078 * ctl_data_submit_done(), it'll get passed back to 9079 * ctl_ctl_cnw_cont() for further processing. 9080 */ 9081 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9082 ctsio->io_cont = ctl_cnw_cont; 9083 9084 lbalen = (struct ctl_lba_len_flags *) 9085 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9086 lbalen->lba = lba; 9087 lbalen->len = num_blocks; 9088 lbalen->flags = CTL_LLF_COMPARE | flags; 9089 9090 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9091 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9092 return (retval); 9093} 9094 9095int 9096ctl_verify(struct ctl_scsiio *ctsio) 9097{ 9098 struct ctl_lun *lun; 9099 struct ctl_lba_len_flags *lbalen; 9100 uint64_t lba; 9101 uint32_t num_blocks; 9102 int bytchk, flags; 9103 int retval; 9104 9105 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9106 9107 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9108 9109 bytchk = 0; 9110 flags = CTL_LLF_FUA; 9111 retval = CTL_RETVAL_COMPLETE; 9112 9113 switch (ctsio->cdb[0]) { 9114 case VERIFY_10: { 9115 struct scsi_verify_10 *cdb; 9116 9117 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9118 if (cdb->byte2 & SVFY_BYTCHK) 9119 bytchk = 1; 9120 if (cdb->byte2 & SVFY_DPO) 9121 flags |= CTL_LLF_DPO; 9122 lba = scsi_4btoul(cdb->addr); 9123 num_blocks = scsi_2btoul(cdb->length); 9124 break; 9125 } 9126 case VERIFY_12: { 9127 struct scsi_verify_12 *cdb; 9128 9129 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9130 if (cdb->byte2 & SVFY_BYTCHK) 9131 bytchk = 1; 9132 if (cdb->byte2 & SVFY_DPO) 9133 flags |= CTL_LLF_DPO; 9134 lba = scsi_4btoul(cdb->addr); 9135 num_blocks = scsi_4btoul(cdb->length); 9136 break; 9137 } 9138 case VERIFY_16: { 9139 struct scsi_rw_16 *cdb; 9140 9141 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9142 if (cdb->byte2 & SVFY_BYTCHK) 9143 bytchk = 1; 9144 if (cdb->byte2 & SVFY_DPO) 9145 flags |= CTL_LLF_DPO; 9146 lba = scsi_8btou64(cdb->addr); 9147 num_blocks = scsi_4btoul(cdb->length); 9148 break; 9149 } 9150 default: 9151 /* 9152 * We got a command we don't support. This shouldn't 9153 * happen, commands should be filtered out above us. 9154 */ 9155 ctl_set_invalid_opcode(ctsio); 9156 ctl_done((union ctl_io *)ctsio); 9157 return (CTL_RETVAL_COMPLETE); 9158 } 9159 9160 /* 9161 * The first check is to make sure we're in bounds, the second 9162 * check is to catch wrap-around problems. If the lba + num blocks 9163 * is less than the lba, then we've wrapped around and the block 9164 * range is invalid anyway. 9165 */ 9166 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9167 || ((lba + num_blocks) < lba)) { 9168 ctl_set_lba_out_of_range(ctsio); 9169 ctl_done((union ctl_io *)ctsio); 9170 return (CTL_RETVAL_COMPLETE); 9171 } 9172 9173 /* 9174 * According to SBC-3, a transfer length of 0 is not an error. 9175 */ 9176 if (num_blocks == 0) { 9177 ctl_set_success(ctsio); 9178 ctl_done((union ctl_io *)ctsio); 9179 return (CTL_RETVAL_COMPLETE); 9180 } 9181 9182 lbalen = (struct ctl_lba_len_flags *) 9183 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9184 lbalen->lba = lba; 9185 lbalen->len = num_blocks; 9186 if (bytchk) { 9187 lbalen->flags = CTL_LLF_COMPARE | flags; 9188 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9189 } else { 9190 lbalen->flags = CTL_LLF_VERIFY | flags; 9191 ctsio->kern_total_len = 0; 9192 } 9193 ctsio->kern_rel_offset = 0; 9194 9195 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9196 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9197 return (retval); 9198} 9199 9200int 9201ctl_report_luns(struct ctl_scsiio *ctsio) 9202{ 9203 struct scsi_report_luns *cdb; 9204 struct scsi_report_luns_data *lun_data; 9205 struct ctl_lun *lun, *request_lun; 9206 int num_luns, retval; 9207 uint32_t alloc_len, lun_datalen; 9208 int num_filled, well_known; 9209 uint32_t initidx, targ_lun_id, lun_id; 9210 9211 retval = CTL_RETVAL_COMPLETE; 9212 well_known = 0; 9213 9214 cdb = (struct scsi_report_luns *)ctsio->cdb; 9215 9216 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9217 9218 mtx_lock(&control_softc->ctl_lock); 9219 num_luns = control_softc->num_luns; 9220 mtx_unlock(&control_softc->ctl_lock); 9221 9222 switch (cdb->select_report) { 9223 case RPL_REPORT_DEFAULT: 9224 case RPL_REPORT_ALL: 9225 break; 9226 case RPL_REPORT_WELLKNOWN: 9227 well_known = 1; 9228 num_luns = 0; 9229 break; 9230 default: 9231 ctl_set_invalid_field(ctsio, 9232 /*sks_valid*/ 1, 9233 /*command*/ 1, 9234 /*field*/ 2, 9235 /*bit_valid*/ 0, 9236 /*bit*/ 0); 9237 ctl_done((union ctl_io *)ctsio); 9238 return (retval); 9239 break; /* NOTREACHED */ 9240 } 9241 9242 alloc_len = scsi_4btoul(cdb->length); 9243 /* 9244 * The initiator has to allocate at least 16 bytes for this request, 9245 * so he can at least get the header and the first LUN. Otherwise 9246 * we reject the request (per SPC-3 rev 14, section 6.21). 9247 */ 9248 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9249 sizeof(struct scsi_report_luns_lundata))) { 9250 ctl_set_invalid_field(ctsio, 9251 /*sks_valid*/ 1, 9252 /*command*/ 1, 9253 /*field*/ 6, 9254 /*bit_valid*/ 0, 9255 /*bit*/ 0); 9256 ctl_done((union ctl_io *)ctsio); 9257 return (retval); 9258 } 9259 9260 request_lun = (struct ctl_lun *) 9261 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9262 9263 lun_datalen = sizeof(*lun_data) + 9264 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9265 9266 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9267 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9268 ctsio->kern_sg_entries = 0; 9269 9270 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9271 9272 mtx_lock(&control_softc->ctl_lock); 9273 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9274 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9275 if (lun_id >= CTL_MAX_LUNS) 9276 continue; 9277 lun = control_softc->ctl_luns[lun_id]; 9278 if (lun == NULL) 9279 continue; 9280 9281 if (targ_lun_id <= 0xff) { 9282 /* 9283 * Peripheral addressing method, bus number 0. 9284 */ 9285 lun_data->luns[num_filled].lundata[0] = 9286 RPL_LUNDATA_ATYP_PERIPH; 9287 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9288 num_filled++; 9289 } else if (targ_lun_id <= 0x3fff) { 9290 /* 9291 * Flat addressing method. 9292 */ 9293 lun_data->luns[num_filled].lundata[0] = 9294 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9295 lun_data->luns[num_filled].lundata[1] = 9296 (targ_lun_id & 0xff); 9297 num_filled++; 9298 } else if (targ_lun_id <= 0xffffff) { 9299 /* 9300 * Extended flat addressing method. 9301 */ 9302 lun_data->luns[num_filled].lundata[0] = 9303 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9304 scsi_ulto3b(targ_lun_id, 9305 &lun_data->luns[num_filled].lundata[1]); 9306 num_filled++; 9307 } else { 9308 printf("ctl_report_luns: bogus LUN number %jd, " 9309 "skipping\n", (intmax_t)targ_lun_id); 9310 } 9311 /* 9312 * According to SPC-3, rev 14 section 6.21: 9313 * 9314 * "The execution of a REPORT LUNS command to any valid and 9315 * installed logical unit shall clear the REPORTED LUNS DATA 9316 * HAS CHANGED unit attention condition for all logical 9317 * units of that target with respect to the requesting 9318 * initiator. A valid and installed logical unit is one 9319 * having a PERIPHERAL QUALIFIER of 000b in the standard 9320 * INQUIRY data (see 6.4.2)." 9321 * 9322 * If request_lun is NULL, the LUN this report luns command 9323 * was issued to is either disabled or doesn't exist. In that 9324 * case, we shouldn't clear any pending lun change unit 9325 * attention. 9326 */ 9327 if (request_lun != NULL) { 9328 mtx_lock(&lun->lun_lock); 9329 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE); 9330 mtx_unlock(&lun->lun_lock); 9331 } 9332 } 9333 mtx_unlock(&control_softc->ctl_lock); 9334 9335 /* 9336 * It's quite possible that we've returned fewer LUNs than we allocated 9337 * space for. Trim it. 9338 */ 9339 lun_datalen = sizeof(*lun_data) + 9340 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9341 9342 if (lun_datalen < alloc_len) { 9343 ctsio->residual = alloc_len - lun_datalen; 9344 ctsio->kern_data_len = lun_datalen; 9345 ctsio->kern_total_len = lun_datalen; 9346 } else { 9347 ctsio->residual = 0; 9348 ctsio->kern_data_len = alloc_len; 9349 ctsio->kern_total_len = alloc_len; 9350 } 9351 ctsio->kern_data_resid = 0; 9352 ctsio->kern_rel_offset = 0; 9353 ctsio->kern_sg_entries = 0; 9354 9355 /* 9356 * We set this to the actual data length, regardless of how much 9357 * space we actually have to return results. If the user looks at 9358 * this value, he'll know whether or not he allocated enough space 9359 * and reissue the command if necessary. We don't support well 9360 * known logical units, so if the user asks for that, return none. 9361 */ 9362 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9363 9364 /* 9365 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9366 * this request. 9367 */ 9368 ctl_set_success(ctsio); 9369 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9370 ctsio->be_move_done = ctl_config_move_done; 9371 ctl_datamove((union ctl_io *)ctsio); 9372 return (retval); 9373} 9374 9375int 9376ctl_request_sense(struct ctl_scsiio *ctsio) 9377{ 9378 struct scsi_request_sense *cdb; 9379 struct scsi_sense_data *sense_ptr; 9380 struct ctl_lun *lun; 9381 uint32_t initidx; 9382 int have_error; 9383 scsi_sense_data_type sense_format; 9384 ctl_ua_type ua_type; 9385 9386 cdb = (struct scsi_request_sense *)ctsio->cdb; 9387 9388 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9389 9390 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9391 9392 /* 9393 * Determine which sense format the user wants. 9394 */ 9395 if (cdb->byte2 & SRS_DESC) 9396 sense_format = SSD_TYPE_DESC; 9397 else 9398 sense_format = SSD_TYPE_FIXED; 9399 9400 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9401 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9402 ctsio->kern_sg_entries = 0; 9403 9404 /* 9405 * struct scsi_sense_data, which is currently set to 256 bytes, is 9406 * larger than the largest allowed value for the length field in the 9407 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9408 */ 9409 ctsio->residual = 0; 9410 ctsio->kern_data_len = cdb->length; 9411 ctsio->kern_total_len = cdb->length; 9412 9413 ctsio->kern_data_resid = 0; 9414 ctsio->kern_rel_offset = 0; 9415 ctsio->kern_sg_entries = 0; 9416 9417 /* 9418 * If we don't have a LUN, we don't have any pending sense. 9419 */ 9420 if (lun == NULL) 9421 goto no_sense; 9422 9423 have_error = 0; 9424 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9425 /* 9426 * Check for pending sense, and then for pending unit attentions. 9427 * Pending sense gets returned first, then pending unit attentions. 9428 */ 9429 mtx_lock(&lun->lun_lock); 9430#ifdef CTL_WITH_CA 9431 if (ctl_is_set(lun->have_ca, initidx)) { 9432 scsi_sense_data_type stored_format; 9433 9434 /* 9435 * Check to see which sense format was used for the stored 9436 * sense data. 9437 */ 9438 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9439 9440 /* 9441 * If the user requested a different sense format than the 9442 * one we stored, then we need to convert it to the other 9443 * format. If we're going from descriptor to fixed format 9444 * sense data, we may lose things in translation, depending 9445 * on what options were used. 9446 * 9447 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9448 * for some reason we'll just copy it out as-is. 9449 */ 9450 if ((stored_format == SSD_TYPE_FIXED) 9451 && (sense_format == SSD_TYPE_DESC)) 9452 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9453 &lun->pending_sense[initidx], 9454 (struct scsi_sense_data_desc *)sense_ptr); 9455 else if ((stored_format == SSD_TYPE_DESC) 9456 && (sense_format == SSD_TYPE_FIXED)) 9457 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9458 &lun->pending_sense[initidx], 9459 (struct scsi_sense_data_fixed *)sense_ptr); 9460 else 9461 memcpy(sense_ptr, &lun->pending_sense[initidx], 9462 ctl_min(sizeof(*sense_ptr), 9463 sizeof(lun->pending_sense[initidx]))); 9464 9465 ctl_clear_mask(lun->have_ca, initidx); 9466 have_error = 1; 9467 } else 9468#endif 9469 { 9470 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9471 if (ua_type != CTL_UA_NONE) 9472 have_error = 1; 9473 } 9474 mtx_unlock(&lun->lun_lock); 9475 9476 /* 9477 * We already have a pending error, return it. 9478 */ 9479 if (have_error != 0) { 9480 /* 9481 * We report the SCSI status as OK, since the status of the 9482 * request sense command itself is OK. 9483 * We report 0 for the sense length, because we aren't doing 9484 * autosense in this case. We're reporting sense as 9485 * parameter data. 9486 */ 9487 ctl_set_success(ctsio); 9488 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9489 ctsio->be_move_done = ctl_config_move_done; 9490 ctl_datamove((union ctl_io *)ctsio); 9491 return (CTL_RETVAL_COMPLETE); 9492 } 9493 9494no_sense: 9495 9496 /* 9497 * No sense information to report, so we report that everything is 9498 * okay. 9499 */ 9500 ctl_set_sense_data(sense_ptr, 9501 lun, 9502 sense_format, 9503 /*current_error*/ 1, 9504 /*sense_key*/ SSD_KEY_NO_SENSE, 9505 /*asc*/ 0x00, 9506 /*ascq*/ 0x00, 9507 SSD_ELEM_NONE); 9508 9509 /* 9510 * We report 0 for the sense length, because we aren't doing 9511 * autosense in this case. We're reporting sense as parameter data. 9512 */ 9513 ctl_set_success(ctsio); 9514 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9515 ctsio->be_move_done = ctl_config_move_done; 9516 ctl_datamove((union ctl_io *)ctsio); 9517 return (CTL_RETVAL_COMPLETE); 9518} 9519 9520int 9521ctl_tur(struct ctl_scsiio *ctsio) 9522{ 9523 9524 CTL_DEBUG_PRINT(("ctl_tur\n")); 9525 9526 ctl_set_success(ctsio); 9527 ctl_done((union ctl_io *)ctsio); 9528 9529 return (CTL_RETVAL_COMPLETE); 9530} 9531 9532#ifdef notyet 9533static int 9534ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9535{ 9536 9537} 9538#endif 9539 9540static int 9541ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9542{ 9543 struct scsi_vpd_supported_pages *pages; 9544 int sup_page_size; 9545 struct ctl_lun *lun; 9546 9547 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9548 9549 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9550 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9551 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9552 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9553 ctsio->kern_sg_entries = 0; 9554 9555 if (sup_page_size < alloc_len) { 9556 ctsio->residual = alloc_len - sup_page_size; 9557 ctsio->kern_data_len = sup_page_size; 9558 ctsio->kern_total_len = sup_page_size; 9559 } else { 9560 ctsio->residual = 0; 9561 ctsio->kern_data_len = alloc_len; 9562 ctsio->kern_total_len = alloc_len; 9563 } 9564 ctsio->kern_data_resid = 0; 9565 ctsio->kern_rel_offset = 0; 9566 ctsio->kern_sg_entries = 0; 9567 9568 /* 9569 * The control device is always connected. The disk device, on the 9570 * other hand, may not be online all the time. Need to change this 9571 * to figure out whether the disk device is actually online or not. 9572 */ 9573 if (lun != NULL) 9574 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9575 lun->be_lun->lun_type; 9576 else 9577 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9578 9579 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9580 /* Supported VPD pages */ 9581 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9582 /* Serial Number */ 9583 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9584 /* Device Identification */ 9585 pages->page_list[2] = SVPD_DEVICE_ID; 9586 /* Extended INQUIRY Data */ 9587 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9588 /* Mode Page Policy */ 9589 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9590 /* SCSI Ports */ 9591 pages->page_list[5] = SVPD_SCSI_PORTS; 9592 /* Third-party Copy */ 9593 pages->page_list[6] = SVPD_SCSI_TPC; 9594 /* Block limits */ 9595 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9596 /* Block Device Characteristics */ 9597 pages->page_list[8] = SVPD_BDC; 9598 /* Logical Block Provisioning */ 9599 pages->page_list[9] = SVPD_LBP; 9600 9601 ctl_set_success(ctsio); 9602 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9603 ctsio->be_move_done = ctl_config_move_done; 9604 ctl_datamove((union ctl_io *)ctsio); 9605 return (CTL_RETVAL_COMPLETE); 9606} 9607 9608static int 9609ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9610{ 9611 struct scsi_vpd_unit_serial_number *sn_ptr; 9612 struct ctl_lun *lun; 9613 int data_len; 9614 9615 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9616 9617 data_len = 4 + CTL_SN_LEN; 9618 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9619 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9620 if (data_len < alloc_len) { 9621 ctsio->residual = alloc_len - data_len; 9622 ctsio->kern_data_len = data_len; 9623 ctsio->kern_total_len = data_len; 9624 } else { 9625 ctsio->residual = 0; 9626 ctsio->kern_data_len = alloc_len; 9627 ctsio->kern_total_len = alloc_len; 9628 } 9629 ctsio->kern_data_resid = 0; 9630 ctsio->kern_rel_offset = 0; 9631 ctsio->kern_sg_entries = 0; 9632 9633 /* 9634 * The control device is always connected. The disk device, on the 9635 * other hand, may not be online all the time. Need to change this 9636 * to figure out whether the disk device is actually online or not. 9637 */ 9638 if (lun != NULL) 9639 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9640 lun->be_lun->lun_type; 9641 else 9642 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9643 9644 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9645 sn_ptr->length = CTL_SN_LEN; 9646 /* 9647 * If we don't have a LUN, we just leave the serial number as 9648 * all spaces. 9649 */ 9650 if (lun != NULL) { 9651 strncpy((char *)sn_ptr->serial_num, 9652 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9653 } else 9654 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9655 9656 ctl_set_success(ctsio); 9657 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9658 ctsio->be_move_done = ctl_config_move_done; 9659 ctl_datamove((union ctl_io *)ctsio); 9660 return (CTL_RETVAL_COMPLETE); 9661} 9662 9663 9664static int 9665ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9666{ 9667 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9668 struct ctl_lun *lun; 9669 int data_len; 9670 9671 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9672 9673 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9674 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9675 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9676 ctsio->kern_sg_entries = 0; 9677 9678 if (data_len < alloc_len) { 9679 ctsio->residual = alloc_len - data_len; 9680 ctsio->kern_data_len = data_len; 9681 ctsio->kern_total_len = data_len; 9682 } else { 9683 ctsio->residual = 0; 9684 ctsio->kern_data_len = alloc_len; 9685 ctsio->kern_total_len = alloc_len; 9686 } 9687 ctsio->kern_data_resid = 0; 9688 ctsio->kern_rel_offset = 0; 9689 ctsio->kern_sg_entries = 0; 9690 9691 /* 9692 * The control device is always connected. The disk device, on the 9693 * other hand, may not be online all the time. 9694 */ 9695 if (lun != NULL) 9696 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9697 lun->be_lun->lun_type; 9698 else 9699 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9700 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9701 eid_ptr->page_length = data_len - 4; 9702 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9703 eid_ptr->flags3 = SVPD_EID_V_SUP; 9704 9705 ctl_set_success(ctsio); 9706 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9707 ctsio->be_move_done = ctl_config_move_done; 9708 ctl_datamove((union ctl_io *)ctsio); 9709 return (CTL_RETVAL_COMPLETE); 9710} 9711 9712static int 9713ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9714{ 9715 struct scsi_vpd_mode_page_policy *mpp_ptr; 9716 struct ctl_lun *lun; 9717 int data_len; 9718 9719 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9720 9721 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9722 sizeof(struct scsi_vpd_mode_page_policy_descr); 9723 9724 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9725 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9726 ctsio->kern_sg_entries = 0; 9727 9728 if (data_len < alloc_len) { 9729 ctsio->residual = alloc_len - data_len; 9730 ctsio->kern_data_len = data_len; 9731 ctsio->kern_total_len = data_len; 9732 } else { 9733 ctsio->residual = 0; 9734 ctsio->kern_data_len = alloc_len; 9735 ctsio->kern_total_len = alloc_len; 9736 } 9737 ctsio->kern_data_resid = 0; 9738 ctsio->kern_rel_offset = 0; 9739 ctsio->kern_sg_entries = 0; 9740 9741 /* 9742 * The control device is always connected. The disk device, on the 9743 * other hand, may not be online all the time. 9744 */ 9745 if (lun != NULL) 9746 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9747 lun->be_lun->lun_type; 9748 else 9749 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9750 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9751 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9752 mpp_ptr->descr[0].page_code = 0x3f; 9753 mpp_ptr->descr[0].subpage_code = 0xff; 9754 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9755 9756 ctl_set_success(ctsio); 9757 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9758 ctsio->be_move_done = ctl_config_move_done; 9759 ctl_datamove((union ctl_io *)ctsio); 9760 return (CTL_RETVAL_COMPLETE); 9761} 9762 9763static int 9764ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9765{ 9766 struct scsi_vpd_device_id *devid_ptr; 9767 struct scsi_vpd_id_descriptor *desc; 9768 struct ctl_softc *ctl_softc; 9769 struct ctl_lun *lun; 9770 struct ctl_port *port; 9771 int data_len; 9772 uint8_t proto; 9773 9774 ctl_softc = control_softc; 9775 9776 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9777 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9778 9779 data_len = sizeof(struct scsi_vpd_device_id) + 9780 sizeof(struct scsi_vpd_id_descriptor) + 9781 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9782 sizeof(struct scsi_vpd_id_descriptor) + 9783 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9784 if (lun && lun->lun_devid) 9785 data_len += lun->lun_devid->len; 9786 if (port->port_devid) 9787 data_len += port->port_devid->len; 9788 if (port->target_devid) 9789 data_len += port->target_devid->len; 9790 9791 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9792 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9793 ctsio->kern_sg_entries = 0; 9794 9795 if (data_len < alloc_len) { 9796 ctsio->residual = alloc_len - data_len; 9797 ctsio->kern_data_len = data_len; 9798 ctsio->kern_total_len = data_len; 9799 } else { 9800 ctsio->residual = 0; 9801 ctsio->kern_data_len = alloc_len; 9802 ctsio->kern_total_len = alloc_len; 9803 } 9804 ctsio->kern_data_resid = 0; 9805 ctsio->kern_rel_offset = 0; 9806 ctsio->kern_sg_entries = 0; 9807 9808 /* 9809 * The control device is always connected. The disk device, on the 9810 * other hand, may not be online all the time. 9811 */ 9812 if (lun != NULL) 9813 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9814 lun->be_lun->lun_type; 9815 else 9816 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9817 devid_ptr->page_code = SVPD_DEVICE_ID; 9818 scsi_ulto2b(data_len - 4, devid_ptr->length); 9819 9820 if (port->port_type == CTL_PORT_FC) 9821 proto = SCSI_PROTO_FC << 4; 9822 else if (port->port_type == CTL_PORT_ISCSI) 9823 proto = SCSI_PROTO_ISCSI << 4; 9824 else 9825 proto = SCSI_PROTO_SPI << 4; 9826 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9827 9828 /* 9829 * We're using a LUN association here. i.e., this device ID is a 9830 * per-LUN identifier. 9831 */ 9832 if (lun && lun->lun_devid) { 9833 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9834 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9835 lun->lun_devid->len); 9836 } 9837 9838 /* 9839 * This is for the WWPN which is a port association. 9840 */ 9841 if (port->port_devid) { 9842 memcpy(desc, port->port_devid->data, port->port_devid->len); 9843 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9844 port->port_devid->len); 9845 } 9846 9847 /* 9848 * This is for the Relative Target Port(type 4h) identifier 9849 */ 9850 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9851 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9852 SVPD_ID_TYPE_RELTARG; 9853 desc->length = 4; 9854 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9855 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9856 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9857 9858 /* 9859 * This is for the Target Port Group(type 5h) identifier 9860 */ 9861 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9862 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9863 SVPD_ID_TYPE_TPORTGRP; 9864 desc->length = 4; 9865 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9866 &desc->identifier[2]); 9867 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9868 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9869 9870 /* 9871 * This is for the Target identifier 9872 */ 9873 if (port->target_devid) { 9874 memcpy(desc, port->target_devid->data, port->target_devid->len); 9875 } 9876 9877 ctl_set_success(ctsio); 9878 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9879 ctsio->be_move_done = ctl_config_move_done; 9880 ctl_datamove((union ctl_io *)ctsio); 9881 return (CTL_RETVAL_COMPLETE); 9882} 9883 9884static int 9885ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9886{ 9887 struct ctl_softc *softc = control_softc; 9888 struct scsi_vpd_scsi_ports *sp; 9889 struct scsi_vpd_port_designation *pd; 9890 struct scsi_vpd_port_designation_cont *pdc; 9891 struct ctl_lun *lun; 9892 struct ctl_port *port; 9893 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9894 int num_target_port_groups; 9895 9896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9897 9898 if (softc->is_single) 9899 num_target_port_groups = 1; 9900 else 9901 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9902 num_target_ports = 0; 9903 iid_len = 0; 9904 id_len = 0; 9905 mtx_lock(&softc->ctl_lock); 9906 STAILQ_FOREACH(port, &softc->port_list, links) { 9907 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9908 continue; 9909 if (lun != NULL && 9910 ctl_map_lun_back(port->targ_port, lun->lun) >= 9911 CTL_MAX_LUNS) 9912 continue; 9913 num_target_ports++; 9914 if (port->init_devid) 9915 iid_len += port->init_devid->len; 9916 if (port->port_devid) 9917 id_len += port->port_devid->len; 9918 } 9919 mtx_unlock(&softc->ctl_lock); 9920 9921 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9922 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9923 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9924 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9925 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9926 ctsio->kern_sg_entries = 0; 9927 9928 if (data_len < alloc_len) { 9929 ctsio->residual = alloc_len - data_len; 9930 ctsio->kern_data_len = data_len; 9931 ctsio->kern_total_len = data_len; 9932 } else { 9933 ctsio->residual = 0; 9934 ctsio->kern_data_len = alloc_len; 9935 ctsio->kern_total_len = alloc_len; 9936 } 9937 ctsio->kern_data_resid = 0; 9938 ctsio->kern_rel_offset = 0; 9939 ctsio->kern_sg_entries = 0; 9940 9941 /* 9942 * The control device is always connected. The disk device, on the 9943 * other hand, may not be online all the time. Need to change this 9944 * to figure out whether the disk device is actually online or not. 9945 */ 9946 if (lun != NULL) 9947 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9948 lun->be_lun->lun_type; 9949 else 9950 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9951 9952 sp->page_code = SVPD_SCSI_PORTS; 9953 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9954 sp->page_length); 9955 pd = &sp->design[0]; 9956 9957 mtx_lock(&softc->ctl_lock); 9958 pg = softc->port_offset / CTL_MAX_PORTS; 9959 for (g = 0; g < num_target_port_groups; g++) { 9960 STAILQ_FOREACH(port, &softc->port_list, links) { 9961 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9962 continue; 9963 if (lun != NULL && 9964 ctl_map_lun_back(port->targ_port, lun->lun) >= 9965 CTL_MAX_LUNS) 9966 continue; 9967 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 9968 scsi_ulto2b(p, pd->relative_port_id); 9969 if (port->init_devid && g == pg) { 9970 iid_len = port->init_devid->len; 9971 memcpy(pd->initiator_transportid, 9972 port->init_devid->data, port->init_devid->len); 9973 } else 9974 iid_len = 0; 9975 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9976 pdc = (struct scsi_vpd_port_designation_cont *) 9977 (&pd->initiator_transportid[iid_len]); 9978 if (port->port_devid && g == pg) { 9979 id_len = port->port_devid->len; 9980 memcpy(pdc->target_port_descriptors, 9981 port->port_devid->data, port->port_devid->len); 9982 } else 9983 id_len = 0; 9984 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9985 pd = (struct scsi_vpd_port_designation *) 9986 ((uint8_t *)pdc->target_port_descriptors + id_len); 9987 } 9988 } 9989 mtx_unlock(&softc->ctl_lock); 9990 9991 ctl_set_success(ctsio); 9992 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9993 ctsio->be_move_done = ctl_config_move_done; 9994 ctl_datamove((union ctl_io *)ctsio); 9995 return (CTL_RETVAL_COMPLETE); 9996} 9997 9998static int 9999ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10000{ 10001 struct scsi_vpd_block_limits *bl_ptr; 10002 struct ctl_lun *lun; 10003 int bs; 10004 10005 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10006 10007 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10008 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10009 ctsio->kern_sg_entries = 0; 10010 10011 if (sizeof(*bl_ptr) < alloc_len) { 10012 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10013 ctsio->kern_data_len = sizeof(*bl_ptr); 10014 ctsio->kern_total_len = sizeof(*bl_ptr); 10015 } else { 10016 ctsio->residual = 0; 10017 ctsio->kern_data_len = alloc_len; 10018 ctsio->kern_total_len = alloc_len; 10019 } 10020 ctsio->kern_data_resid = 0; 10021 ctsio->kern_rel_offset = 0; 10022 ctsio->kern_sg_entries = 0; 10023 10024 /* 10025 * The control device is always connected. The disk device, on the 10026 * other hand, may not be online all the time. Need to change this 10027 * to figure out whether the disk device is actually online or not. 10028 */ 10029 if (lun != NULL) 10030 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10031 lun->be_lun->lun_type; 10032 else 10033 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10034 10035 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10036 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10037 bl_ptr->max_cmp_write_len = 0xff; 10038 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10039 if (lun != NULL) { 10040 bs = lun->be_lun->blocksize; 10041 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10042 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10043 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10044 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10045 if (lun->be_lun->pblockexp != 0) { 10046 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10047 bl_ptr->opt_unmap_grain); 10048 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10049 bl_ptr->unmap_grain_align); 10050 } 10051 } 10052 scsi_ulto4b(lun->be_lun->atomicblock, 10053 bl_ptr->max_atomic_transfer_length); 10054 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10055 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10056 } 10057 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10058 10059 ctl_set_success(ctsio); 10060 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10061 ctsio->be_move_done = ctl_config_move_done; 10062 ctl_datamove((union ctl_io *)ctsio); 10063 return (CTL_RETVAL_COMPLETE); 10064} 10065 10066static int 10067ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10068{ 10069 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10070 struct ctl_lun *lun; 10071 const char *value; 10072 u_int i; 10073 10074 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10075 10076 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10077 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10078 ctsio->kern_sg_entries = 0; 10079 10080 if (sizeof(*bdc_ptr) < alloc_len) { 10081 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10082 ctsio->kern_data_len = sizeof(*bdc_ptr); 10083 ctsio->kern_total_len = sizeof(*bdc_ptr); 10084 } else { 10085 ctsio->residual = 0; 10086 ctsio->kern_data_len = alloc_len; 10087 ctsio->kern_total_len = alloc_len; 10088 } 10089 ctsio->kern_data_resid = 0; 10090 ctsio->kern_rel_offset = 0; 10091 ctsio->kern_sg_entries = 0; 10092 10093 /* 10094 * The control device is always connected. The disk device, on the 10095 * other hand, may not be online all the time. Need to change this 10096 * to figure out whether the disk device is actually online or not. 10097 */ 10098 if (lun != NULL) 10099 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10100 lun->be_lun->lun_type; 10101 else 10102 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10103 bdc_ptr->page_code = SVPD_BDC; 10104 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10105 if (lun != NULL && 10106 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10107 i = strtol(value, NULL, 0); 10108 else 10109 i = CTL_DEFAULT_ROTATION_RATE; 10110 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10111 if (lun != NULL && 10112 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10113 i = strtol(value, NULL, 0); 10114 else 10115 i = 0; 10116 bdc_ptr->wab_wac_ff = (i & 0x0f); 10117 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10118 10119 ctl_set_success(ctsio); 10120 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10121 ctsio->be_move_done = ctl_config_move_done; 10122 ctl_datamove((union ctl_io *)ctsio); 10123 return (CTL_RETVAL_COMPLETE); 10124} 10125 10126static int 10127ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10128{ 10129 struct scsi_vpd_logical_block_prov *lbp_ptr; 10130 struct ctl_lun *lun; 10131 10132 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10133 10134 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10135 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10136 ctsio->kern_sg_entries = 0; 10137 10138 if (sizeof(*lbp_ptr) < alloc_len) { 10139 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10140 ctsio->kern_data_len = sizeof(*lbp_ptr); 10141 ctsio->kern_total_len = sizeof(*lbp_ptr); 10142 } else { 10143 ctsio->residual = 0; 10144 ctsio->kern_data_len = alloc_len; 10145 ctsio->kern_total_len = alloc_len; 10146 } 10147 ctsio->kern_data_resid = 0; 10148 ctsio->kern_rel_offset = 0; 10149 ctsio->kern_sg_entries = 0; 10150 10151 /* 10152 * The control device is always connected. The disk device, on the 10153 * other hand, may not be online all the time. Need to change this 10154 * to figure out whether the disk device is actually online or not. 10155 */ 10156 if (lun != NULL) 10157 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10158 lun->be_lun->lun_type; 10159 else 10160 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10161 10162 lbp_ptr->page_code = SVPD_LBP; 10163 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10164 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10165 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10166 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10167 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10168 lbp_ptr->prov_type = SVPD_LBP_THIN; 10169 } 10170 10171 ctl_set_success(ctsio); 10172 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10173 ctsio->be_move_done = ctl_config_move_done; 10174 ctl_datamove((union ctl_io *)ctsio); 10175 return (CTL_RETVAL_COMPLETE); 10176} 10177 10178static int 10179ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10180{ 10181 struct scsi_inquiry *cdb; 10182 int alloc_len, retval; 10183 10184 cdb = (struct scsi_inquiry *)ctsio->cdb; 10185 10186 retval = CTL_RETVAL_COMPLETE; 10187 10188 alloc_len = scsi_2btoul(cdb->length); 10189 10190 switch (cdb->page_code) { 10191 case SVPD_SUPPORTED_PAGES: 10192 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10193 break; 10194 case SVPD_UNIT_SERIAL_NUMBER: 10195 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10196 break; 10197 case SVPD_DEVICE_ID: 10198 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10199 break; 10200 case SVPD_EXTENDED_INQUIRY_DATA: 10201 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10202 break; 10203 case SVPD_MODE_PAGE_POLICY: 10204 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10205 break; 10206 case SVPD_SCSI_PORTS: 10207 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10208 break; 10209 case SVPD_SCSI_TPC: 10210 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10211 break; 10212 case SVPD_BLOCK_LIMITS: 10213 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10214 break; 10215 case SVPD_BDC: 10216 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10217 break; 10218 case SVPD_LBP: 10219 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10220 break; 10221 default: 10222 ctl_set_invalid_field(ctsio, 10223 /*sks_valid*/ 1, 10224 /*command*/ 1, 10225 /*field*/ 2, 10226 /*bit_valid*/ 0, 10227 /*bit*/ 0); 10228 ctl_done((union ctl_io *)ctsio); 10229 retval = CTL_RETVAL_COMPLETE; 10230 break; 10231 } 10232 10233 return (retval); 10234} 10235 10236static int 10237ctl_inquiry_std(struct ctl_scsiio *ctsio) 10238{ 10239 struct scsi_inquiry_data *inq_ptr; 10240 struct scsi_inquiry *cdb; 10241 struct ctl_softc *ctl_softc; 10242 struct ctl_lun *lun; 10243 char *val; 10244 uint32_t alloc_len, data_len; 10245 ctl_port_type port_type; 10246 10247 ctl_softc = control_softc; 10248 10249 /* 10250 * Figure out whether we're talking to a Fibre Channel port or not. 10251 * We treat the ioctl front end, and any SCSI adapters, as packetized 10252 * SCSI front ends. 10253 */ 10254 port_type = ctl_softc->ctl_ports[ 10255 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10256 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10257 port_type = CTL_PORT_SCSI; 10258 10259 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10260 cdb = (struct scsi_inquiry *)ctsio->cdb; 10261 alloc_len = scsi_2btoul(cdb->length); 10262 10263 /* 10264 * We malloc the full inquiry data size here and fill it 10265 * in. If the user only asks for less, we'll give him 10266 * that much. 10267 */ 10268 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10269 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10270 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10271 ctsio->kern_sg_entries = 0; 10272 ctsio->kern_data_resid = 0; 10273 ctsio->kern_rel_offset = 0; 10274 10275 if (data_len < alloc_len) { 10276 ctsio->residual = alloc_len - data_len; 10277 ctsio->kern_data_len = data_len; 10278 ctsio->kern_total_len = data_len; 10279 } else { 10280 ctsio->residual = 0; 10281 ctsio->kern_data_len = alloc_len; 10282 ctsio->kern_total_len = alloc_len; 10283 } 10284 10285 /* 10286 * If we have a LUN configured, report it as connected. Otherwise, 10287 * report that it is offline or no device is supported, depending 10288 * on the value of inquiry_pq_no_lun. 10289 * 10290 * According to the spec (SPC-4 r34), the peripheral qualifier 10291 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10292 * 10293 * "A peripheral device having the specified peripheral device type 10294 * is not connected to this logical unit. However, the device 10295 * server is capable of supporting the specified peripheral device 10296 * type on this logical unit." 10297 * 10298 * According to the same spec, the peripheral qualifier 10299 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10300 * 10301 * "The device server is not capable of supporting a peripheral 10302 * device on this logical unit. For this peripheral qualifier the 10303 * peripheral device type shall be set to 1Fh. All other peripheral 10304 * device type values are reserved for this peripheral qualifier." 10305 * 10306 * Given the text, it would seem that we probably want to report that 10307 * the LUN is offline here. There is no LUN connected, but we can 10308 * support a LUN at the given LUN number. 10309 * 10310 * In the real world, though, it sounds like things are a little 10311 * different: 10312 * 10313 * - Linux, when presented with a LUN with the offline peripheral 10314 * qualifier, will create an sg driver instance for it. So when 10315 * you attach it to CTL, you wind up with a ton of sg driver 10316 * instances. (One for every LUN that Linux bothered to probe.) 10317 * Linux does this despite the fact that it issues a REPORT LUNs 10318 * to LUN 0 to get the inventory of supported LUNs. 10319 * 10320 * - There is other anecdotal evidence (from Emulex folks) about 10321 * arrays that use the offline peripheral qualifier for LUNs that 10322 * are on the "passive" path in an active/passive array. 10323 * 10324 * So the solution is provide a hopefully reasonable default 10325 * (return bad/no LUN) and allow the user to change the behavior 10326 * with a tunable/sysctl variable. 10327 */ 10328 if (lun != NULL) 10329 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10330 lun->be_lun->lun_type; 10331 else if (ctl_softc->inquiry_pq_no_lun == 0) 10332 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10333 else 10334 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10335 10336 /* RMB in byte 2 is 0 */ 10337 inq_ptr->version = SCSI_REV_SPC4; 10338 10339 /* 10340 * According to SAM-3, even if a device only supports a single 10341 * level of LUN addressing, it should still set the HISUP bit: 10342 * 10343 * 4.9.1 Logical unit numbers overview 10344 * 10345 * All logical unit number formats described in this standard are 10346 * hierarchical in structure even when only a single level in that 10347 * hierarchy is used. The HISUP bit shall be set to one in the 10348 * standard INQUIRY data (see SPC-2) when any logical unit number 10349 * format described in this standard is used. Non-hierarchical 10350 * formats are outside the scope of this standard. 10351 * 10352 * Therefore we set the HiSup bit here. 10353 * 10354 * The reponse format is 2, per SPC-3. 10355 */ 10356 inq_ptr->response_format = SID_HiSup | 2; 10357 10358 inq_ptr->additional_length = data_len - 10359 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10360 CTL_DEBUG_PRINT(("additional_length = %d\n", 10361 inq_ptr->additional_length)); 10362 10363 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10364 /* 16 bit addressing */ 10365 if (port_type == CTL_PORT_SCSI) 10366 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10367 /* XXX set the SID_MultiP bit here if we're actually going to 10368 respond on multiple ports */ 10369 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10370 10371 /* 16 bit data bus, synchronous transfers */ 10372 if (port_type == CTL_PORT_SCSI) 10373 inq_ptr->flags = SID_WBus16 | SID_Sync; 10374 /* 10375 * XXX KDM do we want to support tagged queueing on the control 10376 * device at all? 10377 */ 10378 if ((lun == NULL) 10379 || (lun->be_lun->lun_type != T_PROCESSOR)) 10380 inq_ptr->flags |= SID_CmdQue; 10381 /* 10382 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10383 * We have 8 bytes for the vendor name, and 16 bytes for the device 10384 * name and 4 bytes for the revision. 10385 */ 10386 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10387 "vendor")) == NULL) { 10388 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10389 } else { 10390 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10391 strncpy(inq_ptr->vendor, val, 10392 min(sizeof(inq_ptr->vendor), strlen(val))); 10393 } 10394 if (lun == NULL) { 10395 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10396 sizeof(inq_ptr->product)); 10397 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10398 switch (lun->be_lun->lun_type) { 10399 case T_DIRECT: 10400 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10401 sizeof(inq_ptr->product)); 10402 break; 10403 case T_PROCESSOR: 10404 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10405 sizeof(inq_ptr->product)); 10406 break; 10407 default: 10408 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10409 sizeof(inq_ptr->product)); 10410 break; 10411 } 10412 } else { 10413 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10414 strncpy(inq_ptr->product, val, 10415 min(sizeof(inq_ptr->product), strlen(val))); 10416 } 10417 10418 /* 10419 * XXX make this a macro somewhere so it automatically gets 10420 * incremented when we make changes. 10421 */ 10422 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10423 "revision")) == NULL) { 10424 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10425 } else { 10426 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10427 strncpy(inq_ptr->revision, val, 10428 min(sizeof(inq_ptr->revision), strlen(val))); 10429 } 10430 10431 /* 10432 * For parallel SCSI, we support double transition and single 10433 * transition clocking. We also support QAS (Quick Arbitration 10434 * and Selection) and Information Unit transfers on both the 10435 * control and array devices. 10436 */ 10437 if (port_type == CTL_PORT_SCSI) 10438 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10439 SID_SPI_IUS; 10440 10441 /* SAM-5 (no version claimed) */ 10442 scsi_ulto2b(0x00A0, inq_ptr->version1); 10443 /* SPC-4 (no version claimed) */ 10444 scsi_ulto2b(0x0460, inq_ptr->version2); 10445 if (port_type == CTL_PORT_FC) { 10446 /* FCP-2 ANSI INCITS.350:2003 */ 10447 scsi_ulto2b(0x0917, inq_ptr->version3); 10448 } else if (port_type == CTL_PORT_SCSI) { 10449 /* SPI-4 ANSI INCITS.362:200x */ 10450 scsi_ulto2b(0x0B56, inq_ptr->version3); 10451 } else if (port_type == CTL_PORT_ISCSI) { 10452 /* iSCSI (no version claimed) */ 10453 scsi_ulto2b(0x0960, inq_ptr->version3); 10454 } else if (port_type == CTL_PORT_SAS) { 10455 /* SAS (no version claimed) */ 10456 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10457 } 10458 10459 if (lun == NULL) { 10460 /* SBC-4 (no version claimed) */ 10461 scsi_ulto2b(0x0600, inq_ptr->version4); 10462 } else { 10463 switch (lun->be_lun->lun_type) { 10464 case T_DIRECT: 10465 /* SBC-4 (no version claimed) */ 10466 scsi_ulto2b(0x0600, inq_ptr->version4); 10467 break; 10468 case T_PROCESSOR: 10469 default: 10470 break; 10471 } 10472 } 10473 10474 ctl_set_success(ctsio); 10475 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10476 ctsio->be_move_done = ctl_config_move_done; 10477 ctl_datamove((union ctl_io *)ctsio); 10478 return (CTL_RETVAL_COMPLETE); 10479} 10480 10481int 10482ctl_inquiry(struct ctl_scsiio *ctsio) 10483{ 10484 struct scsi_inquiry *cdb; 10485 int retval; 10486 10487 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10488 10489 cdb = (struct scsi_inquiry *)ctsio->cdb; 10490 if (cdb->byte2 & SI_EVPD) 10491 retval = ctl_inquiry_evpd(ctsio); 10492 else if (cdb->page_code == 0) 10493 retval = ctl_inquiry_std(ctsio); 10494 else { 10495 ctl_set_invalid_field(ctsio, 10496 /*sks_valid*/ 1, 10497 /*command*/ 1, 10498 /*field*/ 2, 10499 /*bit_valid*/ 0, 10500 /*bit*/ 0); 10501 ctl_done((union ctl_io *)ctsio); 10502 return (CTL_RETVAL_COMPLETE); 10503 } 10504 10505 return (retval); 10506} 10507 10508/* 10509 * For known CDB types, parse the LBA and length. 10510 */ 10511static int 10512ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10513{ 10514 if (io->io_hdr.io_type != CTL_IO_SCSI) 10515 return (1); 10516 10517 switch (io->scsiio.cdb[0]) { 10518 case COMPARE_AND_WRITE: { 10519 struct scsi_compare_and_write *cdb; 10520 10521 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10522 10523 *lba = scsi_8btou64(cdb->addr); 10524 *len = cdb->length; 10525 break; 10526 } 10527 case READ_6: 10528 case WRITE_6: { 10529 struct scsi_rw_6 *cdb; 10530 10531 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10532 10533 *lba = scsi_3btoul(cdb->addr); 10534 /* only 5 bits are valid in the most significant address byte */ 10535 *lba &= 0x1fffff; 10536 *len = cdb->length; 10537 break; 10538 } 10539 case READ_10: 10540 case WRITE_10: { 10541 struct scsi_rw_10 *cdb; 10542 10543 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10544 10545 *lba = scsi_4btoul(cdb->addr); 10546 *len = scsi_2btoul(cdb->length); 10547 break; 10548 } 10549 case WRITE_VERIFY_10: { 10550 struct scsi_write_verify_10 *cdb; 10551 10552 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10553 10554 *lba = scsi_4btoul(cdb->addr); 10555 *len = scsi_2btoul(cdb->length); 10556 break; 10557 } 10558 case READ_12: 10559 case WRITE_12: { 10560 struct scsi_rw_12 *cdb; 10561 10562 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10563 10564 *lba = scsi_4btoul(cdb->addr); 10565 *len = scsi_4btoul(cdb->length); 10566 break; 10567 } 10568 case WRITE_VERIFY_12: { 10569 struct scsi_write_verify_12 *cdb; 10570 10571 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10572 10573 *lba = scsi_4btoul(cdb->addr); 10574 *len = scsi_4btoul(cdb->length); 10575 break; 10576 } 10577 case READ_16: 10578 case WRITE_16: 10579 case WRITE_ATOMIC_16: { 10580 struct scsi_rw_16 *cdb; 10581 10582 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10583 10584 *lba = scsi_8btou64(cdb->addr); 10585 *len = scsi_4btoul(cdb->length); 10586 break; 10587 } 10588 case WRITE_VERIFY_16: { 10589 struct scsi_write_verify_16 *cdb; 10590 10591 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10592 10593 *lba = scsi_8btou64(cdb->addr); 10594 *len = scsi_4btoul(cdb->length); 10595 break; 10596 } 10597 case WRITE_SAME_10: { 10598 struct scsi_write_same_10 *cdb; 10599 10600 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10601 10602 *lba = scsi_4btoul(cdb->addr); 10603 *len = scsi_2btoul(cdb->length); 10604 break; 10605 } 10606 case WRITE_SAME_16: { 10607 struct scsi_write_same_16 *cdb; 10608 10609 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10610 10611 *lba = scsi_8btou64(cdb->addr); 10612 *len = scsi_4btoul(cdb->length); 10613 break; 10614 } 10615 case VERIFY_10: { 10616 struct scsi_verify_10 *cdb; 10617 10618 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10619 10620 *lba = scsi_4btoul(cdb->addr); 10621 *len = scsi_2btoul(cdb->length); 10622 break; 10623 } 10624 case VERIFY_12: { 10625 struct scsi_verify_12 *cdb; 10626 10627 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10628 10629 *lba = scsi_4btoul(cdb->addr); 10630 *len = scsi_4btoul(cdb->length); 10631 break; 10632 } 10633 case VERIFY_16: { 10634 struct scsi_verify_16 *cdb; 10635 10636 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10637 10638 *lba = scsi_8btou64(cdb->addr); 10639 *len = scsi_4btoul(cdb->length); 10640 break; 10641 } 10642 case UNMAP: { 10643 *lba = 0; 10644 *len = UINT64_MAX; 10645 break; 10646 } 10647 default: 10648 return (1); 10649 break; /* NOTREACHED */ 10650 } 10651 10652 return (0); 10653} 10654 10655static ctl_action 10656ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10657{ 10658 uint64_t endlba1, endlba2; 10659 10660 endlba1 = lba1 + len1 - 1; 10661 endlba2 = lba2 + len2 - 1; 10662 10663 if ((endlba1 < lba2) 10664 || (endlba2 < lba1)) 10665 return (CTL_ACTION_PASS); 10666 else 10667 return (CTL_ACTION_BLOCK); 10668} 10669 10670static int 10671ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10672{ 10673 struct ctl_ptr_len_flags *ptrlen; 10674 struct scsi_unmap_desc *buf, *end, *range; 10675 uint64_t lba; 10676 uint32_t len; 10677 10678 /* If not UNMAP -- go other way. */ 10679 if (io->io_hdr.io_type != CTL_IO_SCSI || 10680 io->scsiio.cdb[0] != UNMAP) 10681 return (CTL_ACTION_ERROR); 10682 10683 /* If UNMAP without data -- block and wait for data. */ 10684 ptrlen = (struct ctl_ptr_len_flags *) 10685 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10686 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10687 ptrlen->ptr == NULL) 10688 return (CTL_ACTION_BLOCK); 10689 10690 /* UNMAP with data -- check for collision. */ 10691 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10692 end = buf + ptrlen->len / sizeof(*buf); 10693 for (range = buf; range < end; range++) { 10694 lba = scsi_8btou64(range->lba); 10695 len = scsi_4btoul(range->length); 10696 if ((lba < lba2 + len2) && (lba + len > lba2)) 10697 return (CTL_ACTION_BLOCK); 10698 } 10699 return (CTL_ACTION_PASS); 10700} 10701 10702static ctl_action 10703ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10704{ 10705 uint64_t lba1, lba2; 10706 uint64_t len1, len2; 10707 int retval; 10708 10709 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10710 return (CTL_ACTION_ERROR); 10711 10712 retval = ctl_extent_check_unmap(io2, lba1, len1); 10713 if (retval != CTL_ACTION_ERROR) 10714 return (retval); 10715 10716 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10717 return (CTL_ACTION_ERROR); 10718 10719 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10720} 10721 10722static ctl_action 10723ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10724 union ctl_io *ooa_io) 10725{ 10726 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10727 ctl_serialize_action *serialize_row; 10728 10729 /* 10730 * The initiator attempted multiple untagged commands at the same 10731 * time. Can't do that. 10732 */ 10733 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10734 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10735 && ((pending_io->io_hdr.nexus.targ_port == 10736 ooa_io->io_hdr.nexus.targ_port) 10737 && (pending_io->io_hdr.nexus.initid.id == 10738 ooa_io->io_hdr.nexus.initid.id)) 10739 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10740 return (CTL_ACTION_OVERLAP); 10741 10742 /* 10743 * The initiator attempted to send multiple tagged commands with 10744 * the same ID. (It's fine if different initiators have the same 10745 * tag ID.) 10746 * 10747 * Even if all of those conditions are true, we don't kill the I/O 10748 * if the command ahead of us has been aborted. We won't end up 10749 * sending it to the FETD, and it's perfectly legal to resend a 10750 * command with the same tag number as long as the previous 10751 * instance of this tag number has been aborted somehow. 10752 */ 10753 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10754 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10755 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10756 && ((pending_io->io_hdr.nexus.targ_port == 10757 ooa_io->io_hdr.nexus.targ_port) 10758 && (pending_io->io_hdr.nexus.initid.id == 10759 ooa_io->io_hdr.nexus.initid.id)) 10760 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10761 return (CTL_ACTION_OVERLAP_TAG); 10762 10763 /* 10764 * If we get a head of queue tag, SAM-3 says that we should 10765 * immediately execute it. 10766 * 10767 * What happens if this command would normally block for some other 10768 * reason? e.g. a request sense with a head of queue tag 10769 * immediately after a write. Normally that would block, but this 10770 * will result in its getting executed immediately... 10771 * 10772 * We currently return "pass" instead of "skip", so we'll end up 10773 * going through the rest of the queue to check for overlapped tags. 10774 * 10775 * XXX KDM check for other types of blockage first?? 10776 */ 10777 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10778 return (CTL_ACTION_PASS); 10779 10780 /* 10781 * Ordered tags have to block until all items ahead of them 10782 * have completed. If we get called with an ordered tag, we always 10783 * block, if something else is ahead of us in the queue. 10784 */ 10785 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10786 return (CTL_ACTION_BLOCK); 10787 10788 /* 10789 * Simple tags get blocked until all head of queue and ordered tags 10790 * ahead of them have completed. I'm lumping untagged commands in 10791 * with simple tags here. XXX KDM is that the right thing to do? 10792 */ 10793 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10794 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10795 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10796 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10797 return (CTL_ACTION_BLOCK); 10798 10799 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10800 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10801 10802 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10803 10804 switch (serialize_row[pending_entry->seridx]) { 10805 case CTL_SER_BLOCK: 10806 return (CTL_ACTION_BLOCK); 10807 case CTL_SER_EXTENT: 10808 return (ctl_extent_check(pending_io, ooa_io)); 10809 case CTL_SER_EXTENTOPT: 10810 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10811 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10812 return (ctl_extent_check(pending_io, ooa_io)); 10813 /* FALLTHROUGH */ 10814 case CTL_SER_PASS: 10815 return (CTL_ACTION_PASS); 10816 case CTL_SER_BLOCKOPT: 10817 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10818 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10819 return (CTL_ACTION_BLOCK); 10820 return (CTL_ACTION_PASS); 10821 case CTL_SER_SKIP: 10822 return (CTL_ACTION_SKIP); 10823 default: 10824 panic("invalid serialization value %d", 10825 serialize_row[pending_entry->seridx]); 10826 } 10827 10828 return (CTL_ACTION_ERROR); 10829} 10830 10831/* 10832 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10833 * Assumptions: 10834 * - pending_io is generally either incoming, or on the blocked queue 10835 * - starting I/O is the I/O we want to start the check with. 10836 */ 10837static ctl_action 10838ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10839 union ctl_io *starting_io) 10840{ 10841 union ctl_io *ooa_io; 10842 ctl_action action; 10843 10844 mtx_assert(&lun->lun_lock, MA_OWNED); 10845 10846 /* 10847 * Run back along the OOA queue, starting with the current 10848 * blocked I/O and going through every I/O before it on the 10849 * queue. If starting_io is NULL, we'll just end up returning 10850 * CTL_ACTION_PASS. 10851 */ 10852 for (ooa_io = starting_io; ooa_io != NULL; 10853 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10854 ooa_links)){ 10855 10856 /* 10857 * This routine just checks to see whether 10858 * cur_blocked is blocked by ooa_io, which is ahead 10859 * of it in the queue. It doesn't queue/dequeue 10860 * cur_blocked. 10861 */ 10862 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10863 switch (action) { 10864 case CTL_ACTION_BLOCK: 10865 case CTL_ACTION_OVERLAP: 10866 case CTL_ACTION_OVERLAP_TAG: 10867 case CTL_ACTION_SKIP: 10868 case CTL_ACTION_ERROR: 10869 return (action); 10870 break; /* NOTREACHED */ 10871 case CTL_ACTION_PASS: 10872 break; 10873 default: 10874 panic("invalid action %d", action); 10875 break; /* NOTREACHED */ 10876 } 10877 } 10878 10879 return (CTL_ACTION_PASS); 10880} 10881 10882/* 10883 * Assumptions: 10884 * - An I/O has just completed, and has been removed from the per-LUN OOA 10885 * queue, so some items on the blocked queue may now be unblocked. 10886 */ 10887static int 10888ctl_check_blocked(struct ctl_lun *lun) 10889{ 10890 union ctl_io *cur_blocked, *next_blocked; 10891 10892 mtx_assert(&lun->lun_lock, MA_OWNED); 10893 10894 /* 10895 * Run forward from the head of the blocked queue, checking each 10896 * entry against the I/Os prior to it on the OOA queue to see if 10897 * there is still any blockage. 10898 * 10899 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10900 * with our removing a variable on it while it is traversing the 10901 * list. 10902 */ 10903 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10904 cur_blocked != NULL; cur_blocked = next_blocked) { 10905 union ctl_io *prev_ooa; 10906 ctl_action action; 10907 10908 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10909 blocked_links); 10910 10911 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10912 ctl_ooaq, ooa_links); 10913 10914 /* 10915 * If cur_blocked happens to be the first item in the OOA 10916 * queue now, prev_ooa will be NULL, and the action 10917 * returned will just be CTL_ACTION_PASS. 10918 */ 10919 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10920 10921 switch (action) { 10922 case CTL_ACTION_BLOCK: 10923 /* Nothing to do here, still blocked */ 10924 break; 10925 case CTL_ACTION_OVERLAP: 10926 case CTL_ACTION_OVERLAP_TAG: 10927 /* 10928 * This shouldn't happen! In theory we've already 10929 * checked this command for overlap... 10930 */ 10931 break; 10932 case CTL_ACTION_PASS: 10933 case CTL_ACTION_SKIP: { 10934 struct ctl_softc *softc; 10935 const struct ctl_cmd_entry *entry; 10936 int isc_retval; 10937 10938 /* 10939 * The skip case shouldn't happen, this transaction 10940 * should have never made it onto the blocked queue. 10941 */ 10942 /* 10943 * This I/O is no longer blocked, we can remove it 10944 * from the blocked queue. Since this is a TAILQ 10945 * (doubly linked list), we can do O(1) removals 10946 * from any place on the list. 10947 */ 10948 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10949 blocked_links); 10950 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10951 10952 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10953 /* 10954 * Need to send IO back to original side to 10955 * run 10956 */ 10957 union ctl_ha_msg msg_info; 10958 10959 msg_info.hdr.original_sc = 10960 cur_blocked->io_hdr.original_sc; 10961 msg_info.hdr.serializing_sc = cur_blocked; 10962 msg_info.hdr.msg_type = CTL_MSG_R2R; 10963 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10964 &msg_info, sizeof(msg_info), 0)) > 10965 CTL_HA_STATUS_SUCCESS) { 10966 printf("CTL:Check Blocked error from " 10967 "ctl_ha_msg_send %d\n", 10968 isc_retval); 10969 } 10970 break; 10971 } 10972 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 10973 softc = control_softc; 10974 10975 /* 10976 * Check this I/O for LUN state changes that may 10977 * have happened while this command was blocked. 10978 * The LUN state may have been changed by a command 10979 * ahead of us in the queue, so we need to re-check 10980 * for any states that can be caused by SCSI 10981 * commands. 10982 */ 10983 if (ctl_scsiio_lun_check(softc, lun, entry, 10984 &cur_blocked->scsiio) == 0) { 10985 cur_blocked->io_hdr.flags |= 10986 CTL_FLAG_IS_WAS_ON_RTR; 10987 ctl_enqueue_rtr(cur_blocked); 10988 } else 10989 ctl_done(cur_blocked); 10990 break; 10991 } 10992 default: 10993 /* 10994 * This probably shouldn't happen -- we shouldn't 10995 * get CTL_ACTION_ERROR, or anything else. 10996 */ 10997 break; 10998 } 10999 } 11000 11001 return (CTL_RETVAL_COMPLETE); 11002} 11003 11004/* 11005 * This routine (with one exception) checks LUN flags that can be set by 11006 * commands ahead of us in the OOA queue. These flags have to be checked 11007 * when a command initially comes in, and when we pull a command off the 11008 * blocked queue and are preparing to execute it. The reason we have to 11009 * check these flags for commands on the blocked queue is that the LUN 11010 * state may have been changed by a command ahead of us while we're on the 11011 * blocked queue. 11012 * 11013 * Ordering is somewhat important with these checks, so please pay 11014 * careful attention to the placement of any new checks. 11015 */ 11016static int 11017ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11018 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11019{ 11020 int retval; 11021 uint32_t residx; 11022 11023 retval = 0; 11024 11025 mtx_assert(&lun->lun_lock, MA_OWNED); 11026 11027 /* 11028 * If this shelf is a secondary shelf controller, we have to reject 11029 * any media access commands. 11030 */ 11031 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11032 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11033 ctl_set_lun_standby(ctsio); 11034 retval = 1; 11035 goto bailout; 11036 } 11037 11038 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11039 if (lun->flags & CTL_LUN_READONLY) { 11040 ctl_set_sense(ctsio, /*current_error*/ 1, 11041 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11042 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11043 retval = 1; 11044 goto bailout; 11045 } 11046 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11047 .eca_and_aen & SCP_SWP) != 0) { 11048 ctl_set_sense(ctsio, /*current_error*/ 1, 11049 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11050 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11051 retval = 1; 11052 goto bailout; 11053 } 11054 } 11055 11056 /* 11057 * Check for a reservation conflict. If this command isn't allowed 11058 * even on reserved LUNs, and if this initiator isn't the one who 11059 * reserved us, reject the command with a reservation conflict. 11060 */ 11061 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11062 if ((lun->flags & CTL_LUN_RESERVED) 11063 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11064 if (lun->res_idx != residx) { 11065 ctl_set_reservation_conflict(ctsio); 11066 retval = 1; 11067 goto bailout; 11068 } 11069 } 11070 11071 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11072 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11073 /* No reservation or command is allowed. */; 11074 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11075 (lun->res_type == SPR_TYPE_WR_EX || 11076 lun->res_type == SPR_TYPE_WR_EX_RO || 11077 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11078 /* The command is allowed for Write Exclusive resv. */; 11079 } else { 11080 /* 11081 * if we aren't registered or it's a res holder type 11082 * reservation and this isn't the res holder then set a 11083 * conflict. 11084 */ 11085 if (ctl_get_prkey(lun, residx) == 0 11086 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11087 ctl_set_reservation_conflict(ctsio); 11088 retval = 1; 11089 goto bailout; 11090 } 11091 11092 } 11093 11094 if ((lun->flags & CTL_LUN_OFFLINE) 11095 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11096 ctl_set_lun_not_ready(ctsio); 11097 retval = 1; 11098 goto bailout; 11099 } 11100 11101 /* 11102 * If the LUN is stopped, see if this particular command is allowed 11103 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11104 */ 11105 if ((lun->flags & CTL_LUN_STOPPED) 11106 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11107 /* "Logical unit not ready, initializing cmd. required" */ 11108 ctl_set_lun_stopped(ctsio); 11109 retval = 1; 11110 goto bailout; 11111 } 11112 11113 if ((lun->flags & CTL_LUN_INOPERABLE) 11114 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11115 /* "Medium format corrupted" */ 11116 ctl_set_medium_format_corrupted(ctsio); 11117 retval = 1; 11118 goto bailout; 11119 } 11120 11121bailout: 11122 return (retval); 11123 11124} 11125 11126static void 11127ctl_failover_io(union ctl_io *io, int have_lock) 11128{ 11129 ctl_set_busy(&io->scsiio); 11130 ctl_done(io); 11131} 11132 11133static void 11134ctl_failover(void) 11135{ 11136 struct ctl_lun *lun; 11137 struct ctl_softc *ctl_softc; 11138 union ctl_io *next_io, *pending_io; 11139 union ctl_io *io; 11140 int lun_idx; 11141 11142 ctl_softc = control_softc; 11143 11144 mtx_lock(&ctl_softc->ctl_lock); 11145 /* 11146 * Remove any cmds from the other SC from the rtr queue. These 11147 * will obviously only be for LUNs for which we're the primary. 11148 * We can't send status or get/send data for these commands. 11149 * Since they haven't been executed yet, we can just remove them. 11150 * We'll either abort them or delete them below, depending on 11151 * which HA mode we're in. 11152 */ 11153#ifdef notyet 11154 mtx_lock(&ctl_softc->queue_lock); 11155 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11156 io != NULL; io = next_io) { 11157 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11158 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11159 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11160 ctl_io_hdr, links); 11161 } 11162 mtx_unlock(&ctl_softc->queue_lock); 11163#endif 11164 11165 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11166 lun = ctl_softc->ctl_luns[lun_idx]; 11167 if (lun==NULL) 11168 continue; 11169 11170 /* 11171 * Processor LUNs are primary on both sides. 11172 * XXX will this always be true? 11173 */ 11174 if (lun->be_lun->lun_type == T_PROCESSOR) 11175 continue; 11176 11177 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11178 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11179 printf("FAILOVER: primary lun %d\n", lun_idx); 11180 /* 11181 * Remove all commands from the other SC. First from the 11182 * blocked queue then from the ooa queue. Once we have 11183 * removed them. Call ctl_check_blocked to see if there 11184 * is anything that can run. 11185 */ 11186 for (io = (union ctl_io *)TAILQ_FIRST( 11187 &lun->blocked_queue); io != NULL; io = next_io) { 11188 11189 next_io = (union ctl_io *)TAILQ_NEXT( 11190 &io->io_hdr, blocked_links); 11191 11192 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11193 TAILQ_REMOVE(&lun->blocked_queue, 11194 &io->io_hdr,blocked_links); 11195 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11196 TAILQ_REMOVE(&lun->ooa_queue, 11197 &io->io_hdr, ooa_links); 11198 11199 ctl_free_io(io); 11200 } 11201 } 11202 11203 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11204 io != NULL; io = next_io) { 11205 11206 next_io = (union ctl_io *)TAILQ_NEXT( 11207 &io->io_hdr, ooa_links); 11208 11209 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11210 11211 TAILQ_REMOVE(&lun->ooa_queue, 11212 &io->io_hdr, 11213 ooa_links); 11214 11215 ctl_free_io(io); 11216 } 11217 } 11218 ctl_check_blocked(lun); 11219 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11220 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11221 11222 printf("FAILOVER: primary lun %d\n", lun_idx); 11223 /* 11224 * Abort all commands from the other SC. We can't 11225 * send status back for them now. These should get 11226 * cleaned up when they are completed or come out 11227 * for a datamove operation. 11228 */ 11229 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11230 io != NULL; io = next_io) { 11231 next_io = (union ctl_io *)TAILQ_NEXT( 11232 &io->io_hdr, ooa_links); 11233 11234 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11235 io->io_hdr.flags |= CTL_FLAG_ABORT; 11236 } 11237 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11238 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11239 11240 printf("FAILOVER: secondary lun %d\n", lun_idx); 11241 11242 lun->flags |= CTL_LUN_PRIMARY_SC; 11243 11244 /* 11245 * We send all I/O that was sent to this controller 11246 * and redirected to the other side back with 11247 * busy status, and have the initiator retry it. 11248 * Figuring out how much data has been transferred, 11249 * etc. and picking up where we left off would be 11250 * very tricky. 11251 * 11252 * XXX KDM need to remove I/O from the blocked 11253 * queue as well! 11254 */ 11255 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11256 &lun->ooa_queue); pending_io != NULL; 11257 pending_io = next_io) { 11258 11259 next_io = (union ctl_io *)TAILQ_NEXT( 11260 &pending_io->io_hdr, ooa_links); 11261 11262 pending_io->io_hdr.flags &= 11263 ~CTL_FLAG_SENT_2OTHER_SC; 11264 11265 if (pending_io->io_hdr.flags & 11266 CTL_FLAG_IO_ACTIVE) { 11267 pending_io->io_hdr.flags |= 11268 CTL_FLAG_FAILOVER; 11269 } else { 11270 ctl_set_busy(&pending_io->scsiio); 11271 ctl_done(pending_io); 11272 } 11273 } 11274 11275 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11276 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11277 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11278 printf("FAILOVER: secondary lun %d\n", lun_idx); 11279 /* 11280 * if the first io on the OOA is not on the RtR queue 11281 * add it. 11282 */ 11283 lun->flags |= CTL_LUN_PRIMARY_SC; 11284 11285 pending_io = (union ctl_io *)TAILQ_FIRST( 11286 &lun->ooa_queue); 11287 if (pending_io==NULL) { 11288 printf("Nothing on OOA queue\n"); 11289 continue; 11290 } 11291 11292 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11293 if ((pending_io->io_hdr.flags & 11294 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11295 pending_io->io_hdr.flags |= 11296 CTL_FLAG_IS_WAS_ON_RTR; 11297 ctl_enqueue_rtr(pending_io); 11298 } 11299#if 0 11300 else 11301 { 11302 printf("Tag 0x%04x is running\n", 11303 pending_io->scsiio.tag_num); 11304 } 11305#endif 11306 11307 next_io = (union ctl_io *)TAILQ_NEXT( 11308 &pending_io->io_hdr, ooa_links); 11309 for (pending_io=next_io; pending_io != NULL; 11310 pending_io = next_io) { 11311 pending_io->io_hdr.flags &= 11312 ~CTL_FLAG_SENT_2OTHER_SC; 11313 next_io = (union ctl_io *)TAILQ_NEXT( 11314 &pending_io->io_hdr, ooa_links); 11315 if (pending_io->io_hdr.flags & 11316 CTL_FLAG_IS_WAS_ON_RTR) { 11317#if 0 11318 printf("Tag 0x%04x is running\n", 11319 pending_io->scsiio.tag_num); 11320#endif 11321 continue; 11322 } 11323 11324 switch (ctl_check_ooa(lun, pending_io, 11325 (union ctl_io *)TAILQ_PREV( 11326 &pending_io->io_hdr, ctl_ooaq, 11327 ooa_links))) { 11328 11329 case CTL_ACTION_BLOCK: 11330 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11331 &pending_io->io_hdr, 11332 blocked_links); 11333 pending_io->io_hdr.flags |= 11334 CTL_FLAG_BLOCKED; 11335 break; 11336 case CTL_ACTION_PASS: 11337 case CTL_ACTION_SKIP: 11338 pending_io->io_hdr.flags |= 11339 CTL_FLAG_IS_WAS_ON_RTR; 11340 ctl_enqueue_rtr(pending_io); 11341 break; 11342 case CTL_ACTION_OVERLAP: 11343 ctl_set_overlapped_cmd( 11344 (struct ctl_scsiio *)pending_io); 11345 ctl_done(pending_io); 11346 break; 11347 case CTL_ACTION_OVERLAP_TAG: 11348 ctl_set_overlapped_tag( 11349 (struct ctl_scsiio *)pending_io, 11350 pending_io->scsiio.tag_num & 0xff); 11351 ctl_done(pending_io); 11352 break; 11353 case CTL_ACTION_ERROR: 11354 default: 11355 ctl_set_internal_failure( 11356 (struct ctl_scsiio *)pending_io, 11357 0, // sks_valid 11358 0); //retry count 11359 ctl_done(pending_io); 11360 break; 11361 } 11362 } 11363 11364 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11365 } else { 11366 panic("Unhandled HA mode failover, LUN flags = %#x, " 11367 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11368 } 11369 } 11370 ctl_pause_rtr = 0; 11371 mtx_unlock(&ctl_softc->ctl_lock); 11372} 11373 11374static int 11375ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11376{ 11377 struct ctl_lun *lun; 11378 const struct ctl_cmd_entry *entry; 11379 uint32_t initidx, targ_lun; 11380 int retval; 11381 11382 retval = 0; 11383 11384 lun = NULL; 11385 11386 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11387 if ((targ_lun < CTL_MAX_LUNS) 11388 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11389 /* 11390 * If the LUN is invalid, pretend that it doesn't exist. 11391 * It will go away as soon as all pending I/O has been 11392 * completed. 11393 */ 11394 mtx_lock(&lun->lun_lock); 11395 if (lun->flags & CTL_LUN_DISABLED) { 11396 mtx_unlock(&lun->lun_lock); 11397 lun = NULL; 11398 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11399 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11400 } else { 11401 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11402 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11403 lun->be_lun; 11404 if (lun->be_lun->lun_type == T_PROCESSOR) { 11405 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11406 } 11407 11408 /* 11409 * Every I/O goes into the OOA queue for a 11410 * particular LUN, and stays there until completion. 11411 */ 11412 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11413 ooa_links); 11414 } 11415 } else { 11416 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11417 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11418 } 11419 11420 /* Get command entry and return error if it is unsuppotyed. */ 11421 entry = ctl_validate_command(ctsio); 11422 if (entry == NULL) { 11423 if (lun) 11424 mtx_unlock(&lun->lun_lock); 11425 return (retval); 11426 } 11427 11428 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11429 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11430 11431 /* 11432 * Check to see whether we can send this command to LUNs that don't 11433 * exist. This should pretty much only be the case for inquiry 11434 * and request sense. Further checks, below, really require having 11435 * a LUN, so we can't really check the command anymore. Just put 11436 * it on the rtr queue. 11437 */ 11438 if (lun == NULL) { 11439 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11440 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11441 ctl_enqueue_rtr((union ctl_io *)ctsio); 11442 return (retval); 11443 } 11444 11445 ctl_set_unsupported_lun(ctsio); 11446 ctl_done((union ctl_io *)ctsio); 11447 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11448 return (retval); 11449 } else { 11450 /* 11451 * Make sure we support this particular command on this LUN. 11452 * e.g., we don't support writes to the control LUN. 11453 */ 11454 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11455 mtx_unlock(&lun->lun_lock); 11456 ctl_set_invalid_opcode(ctsio); 11457 ctl_done((union ctl_io *)ctsio); 11458 return (retval); 11459 } 11460 } 11461 11462 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11463 11464#ifdef CTL_WITH_CA 11465 /* 11466 * If we've got a request sense, it'll clear the contingent 11467 * allegiance condition. Otherwise, if we have a CA condition for 11468 * this initiator, clear it, because it sent down a command other 11469 * than request sense. 11470 */ 11471 if ((ctsio->cdb[0] != REQUEST_SENSE) 11472 && (ctl_is_set(lun->have_ca, initidx))) 11473 ctl_clear_mask(lun->have_ca, initidx); 11474#endif 11475 11476 /* 11477 * If the command has this flag set, it handles its own unit 11478 * attention reporting, we shouldn't do anything. Otherwise we 11479 * check for any pending unit attentions, and send them back to the 11480 * initiator. We only do this when a command initially comes in, 11481 * not when we pull it off the blocked queue. 11482 * 11483 * According to SAM-3, section 5.3.2, the order that things get 11484 * presented back to the host is basically unit attentions caused 11485 * by some sort of reset event, busy status, reservation conflicts 11486 * or task set full, and finally any other status. 11487 * 11488 * One issue here is that some of the unit attentions we report 11489 * don't fall into the "reset" category (e.g. "reported luns data 11490 * has changed"). So reporting it here, before the reservation 11491 * check, may be technically wrong. I guess the only thing to do 11492 * would be to check for and report the reset events here, and then 11493 * check for the other unit attention types after we check for a 11494 * reservation conflict. 11495 * 11496 * XXX KDM need to fix this 11497 */ 11498 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11499 ctl_ua_type ua_type; 11500 scsi_sense_data_type sense_format; 11501 11502 if (lun->flags & CTL_LUN_SENSE_DESC) 11503 sense_format = SSD_TYPE_DESC; 11504 else 11505 sense_format = SSD_TYPE_FIXED; 11506 11507 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11508 sense_format); 11509 if (ua_type != CTL_UA_NONE) { 11510 mtx_unlock(&lun->lun_lock); 11511 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11512 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11513 ctsio->sense_len = SSD_FULL_SIZE; 11514 ctl_done((union ctl_io *)ctsio); 11515 return (retval); 11516 } 11517 } 11518 11519 11520 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11521 mtx_unlock(&lun->lun_lock); 11522 ctl_done((union ctl_io *)ctsio); 11523 return (retval); 11524 } 11525 11526 /* 11527 * XXX CHD this is where we want to send IO to other side if 11528 * this LUN is secondary on this SC. We will need to make a copy 11529 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11530 * the copy we send as FROM_OTHER. 11531 * We also need to stuff the address of the original IO so we can 11532 * find it easily. Something similar will need be done on the other 11533 * side so when we are done we can find the copy. 11534 */ 11535 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11536 union ctl_ha_msg msg_info; 11537 int isc_retval; 11538 11539 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11540 11541 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11542 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11543#if 0 11544 printf("1. ctsio %p\n", ctsio); 11545#endif 11546 msg_info.hdr.serializing_sc = NULL; 11547 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11548 msg_info.scsi.tag_num = ctsio->tag_num; 11549 msg_info.scsi.tag_type = ctsio->tag_type; 11550 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11551 11552 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11553 11554 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11555 (void *)&msg_info, sizeof(msg_info), 0)) > 11556 CTL_HA_STATUS_SUCCESS) { 11557 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11558 isc_retval); 11559 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11560 } else { 11561#if 0 11562 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11563#endif 11564 } 11565 11566 /* 11567 * XXX KDM this I/O is off the incoming queue, but hasn't 11568 * been inserted on any other queue. We may need to come 11569 * up with a holding queue while we wait for serialization 11570 * so that we have an idea of what we're waiting for from 11571 * the other side. 11572 */ 11573 mtx_unlock(&lun->lun_lock); 11574 return (retval); 11575 } 11576 11577 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11578 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11579 ctl_ooaq, ooa_links))) { 11580 case CTL_ACTION_BLOCK: 11581 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11582 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11583 blocked_links); 11584 mtx_unlock(&lun->lun_lock); 11585 return (retval); 11586 case CTL_ACTION_PASS: 11587 case CTL_ACTION_SKIP: 11588 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11589 mtx_unlock(&lun->lun_lock); 11590 ctl_enqueue_rtr((union ctl_io *)ctsio); 11591 break; 11592 case CTL_ACTION_OVERLAP: 11593 mtx_unlock(&lun->lun_lock); 11594 ctl_set_overlapped_cmd(ctsio); 11595 ctl_done((union ctl_io *)ctsio); 11596 break; 11597 case CTL_ACTION_OVERLAP_TAG: 11598 mtx_unlock(&lun->lun_lock); 11599 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11600 ctl_done((union ctl_io *)ctsio); 11601 break; 11602 case CTL_ACTION_ERROR: 11603 default: 11604 mtx_unlock(&lun->lun_lock); 11605 ctl_set_internal_failure(ctsio, 11606 /*sks_valid*/ 0, 11607 /*retry_count*/ 0); 11608 ctl_done((union ctl_io *)ctsio); 11609 break; 11610 } 11611 return (retval); 11612} 11613 11614const struct ctl_cmd_entry * 11615ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11616{ 11617 const struct ctl_cmd_entry *entry; 11618 int service_action; 11619 11620 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11621 if (sa) 11622 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11623 if (entry->flags & CTL_CMD_FLAG_SA5) { 11624 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11625 entry = &((const struct ctl_cmd_entry *) 11626 entry->execute)[service_action]; 11627 } 11628 return (entry); 11629} 11630 11631const struct ctl_cmd_entry * 11632ctl_validate_command(struct ctl_scsiio *ctsio) 11633{ 11634 const struct ctl_cmd_entry *entry; 11635 int i, sa; 11636 uint8_t diff; 11637 11638 entry = ctl_get_cmd_entry(ctsio, &sa); 11639 if (entry->execute == NULL) { 11640 if (sa) 11641 ctl_set_invalid_field(ctsio, 11642 /*sks_valid*/ 1, 11643 /*command*/ 1, 11644 /*field*/ 1, 11645 /*bit_valid*/ 1, 11646 /*bit*/ 4); 11647 else 11648 ctl_set_invalid_opcode(ctsio); 11649 ctl_done((union ctl_io *)ctsio); 11650 return (NULL); 11651 } 11652 KASSERT(entry->length > 0, 11653 ("Not defined length for command 0x%02x/0x%02x", 11654 ctsio->cdb[0], ctsio->cdb[1])); 11655 for (i = 1; i < entry->length; i++) { 11656 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11657 if (diff == 0) 11658 continue; 11659 ctl_set_invalid_field(ctsio, 11660 /*sks_valid*/ 1, 11661 /*command*/ 1, 11662 /*field*/ i, 11663 /*bit_valid*/ 1, 11664 /*bit*/ fls(diff) - 1); 11665 ctl_done((union ctl_io *)ctsio); 11666 return (NULL); 11667 } 11668 return (entry); 11669} 11670 11671static int 11672ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11673{ 11674 11675 switch (lun_type) { 11676 case T_PROCESSOR: 11677 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11678 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11679 return (0); 11680 break; 11681 case T_DIRECT: 11682 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11683 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11684 return (0); 11685 break; 11686 default: 11687 return (0); 11688 } 11689 return (1); 11690} 11691 11692static int 11693ctl_scsiio(struct ctl_scsiio *ctsio) 11694{ 11695 int retval; 11696 const struct ctl_cmd_entry *entry; 11697 11698 retval = CTL_RETVAL_COMPLETE; 11699 11700 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11701 11702 entry = ctl_get_cmd_entry(ctsio, NULL); 11703 11704 /* 11705 * If this I/O has been aborted, just send it straight to 11706 * ctl_done() without executing it. 11707 */ 11708 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11709 ctl_done((union ctl_io *)ctsio); 11710 goto bailout; 11711 } 11712 11713 /* 11714 * All the checks should have been handled by ctl_scsiio_precheck(). 11715 * We should be clear now to just execute the I/O. 11716 */ 11717 retval = entry->execute(ctsio); 11718 11719bailout: 11720 return (retval); 11721} 11722 11723/* 11724 * Since we only implement one target right now, a bus reset simply resets 11725 * our single target. 11726 */ 11727static int 11728ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11729{ 11730 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11731} 11732 11733static int 11734ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11735 ctl_ua_type ua_type) 11736{ 11737 struct ctl_lun *lun; 11738 int retval; 11739 11740 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11741 union ctl_ha_msg msg_info; 11742 11743 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11744 msg_info.hdr.nexus = io->io_hdr.nexus; 11745 if (ua_type==CTL_UA_TARG_RESET) 11746 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11747 else 11748 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11749 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11750 msg_info.hdr.original_sc = NULL; 11751 msg_info.hdr.serializing_sc = NULL; 11752 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11753 (void *)&msg_info, sizeof(msg_info), 0)) { 11754 } 11755 } 11756 retval = 0; 11757 11758 mtx_lock(&ctl_softc->ctl_lock); 11759 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11760 retval += ctl_lun_reset(lun, io, ua_type); 11761 mtx_unlock(&ctl_softc->ctl_lock); 11762 11763 return (retval); 11764} 11765 11766/* 11767 * The LUN should always be set. The I/O is optional, and is used to 11768 * distinguish between I/Os sent by this initiator, and by other 11769 * initiators. We set unit attention for initiators other than this one. 11770 * SAM-3 is vague on this point. It does say that a unit attention should 11771 * be established for other initiators when a LUN is reset (see section 11772 * 5.7.3), but it doesn't specifically say that the unit attention should 11773 * be established for this particular initiator when a LUN is reset. Here 11774 * is the relevant text, from SAM-3 rev 8: 11775 * 11776 * 5.7.2 When a SCSI initiator port aborts its own tasks 11777 * 11778 * When a SCSI initiator port causes its own task(s) to be aborted, no 11779 * notification that the task(s) have been aborted shall be returned to 11780 * the SCSI initiator port other than the completion response for the 11781 * command or task management function action that caused the task(s) to 11782 * be aborted and notification(s) associated with related effects of the 11783 * action (e.g., a reset unit attention condition). 11784 * 11785 * XXX KDM for now, we're setting unit attention for all initiators. 11786 */ 11787static int 11788ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11789{ 11790 union ctl_io *xio; 11791#if 0 11792 uint32_t initidx; 11793#endif 11794#ifdef CTL_WITH_CA 11795 int i; 11796#endif 11797 11798 mtx_lock(&lun->lun_lock); 11799 /* 11800 * Run through the OOA queue and abort each I/O. 11801 */ 11802#if 0 11803 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11804#endif 11805 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11806 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11807 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11808 } 11809 11810 /* 11811 * This version sets unit attention for every 11812 */ 11813#if 0 11814 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11815 ctl_est_ua_all(lun, initidx, ua_type); 11816#else 11817 ctl_est_ua_all(lun, -1, ua_type); 11818#endif 11819 11820 /* 11821 * A reset (any kind, really) clears reservations established with 11822 * RESERVE/RELEASE. It does not clear reservations established 11823 * with PERSISTENT RESERVE OUT, but we don't support that at the 11824 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11825 * reservations made with the RESERVE/RELEASE commands, because 11826 * those commands are obsolete in SPC-3. 11827 */ 11828 lun->flags &= ~CTL_LUN_RESERVED; 11829 11830#ifdef CTL_WITH_CA 11831 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11832 ctl_clear_mask(lun->have_ca, i); 11833#endif 11834 mtx_unlock(&lun->lun_lock); 11835 11836 return (0); 11837} 11838 11839static void 11840ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11841 int other_sc) 11842{ 11843 union ctl_io *xio; 11844 11845 mtx_assert(&lun->lun_lock, MA_OWNED); 11846 11847 /* 11848 * Run through the OOA queue and attempt to find the given I/O. 11849 * The target port, initiator ID, tag type and tag number have to 11850 * match the values that we got from the initiator. If we have an 11851 * untagged command to abort, simply abort the first untagged command 11852 * we come to. We only allow one untagged command at a time of course. 11853 */ 11854 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11855 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11856 11857 if ((targ_port == UINT32_MAX || 11858 targ_port == xio->io_hdr.nexus.targ_port) && 11859 (init_id == UINT32_MAX || 11860 init_id == xio->io_hdr.nexus.initid.id)) { 11861 if (targ_port != xio->io_hdr.nexus.targ_port || 11862 init_id != xio->io_hdr.nexus.initid.id) 11863 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11864 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11865 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11866 union ctl_ha_msg msg_info; 11867 11868 msg_info.hdr.nexus = xio->io_hdr.nexus; 11869 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11870 msg_info.task.tag_num = xio->scsiio.tag_num; 11871 msg_info.task.tag_type = xio->scsiio.tag_type; 11872 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11873 msg_info.hdr.original_sc = NULL; 11874 msg_info.hdr.serializing_sc = NULL; 11875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11876 (void *)&msg_info, sizeof(msg_info), 0); 11877 } 11878 } 11879 } 11880} 11881 11882static int 11883ctl_abort_task_set(union ctl_io *io) 11884{ 11885 struct ctl_softc *softc = control_softc; 11886 struct ctl_lun *lun; 11887 uint32_t targ_lun; 11888 11889 /* 11890 * Look up the LUN. 11891 */ 11892 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11893 mtx_lock(&softc->ctl_lock); 11894 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11895 lun = softc->ctl_luns[targ_lun]; 11896 else { 11897 mtx_unlock(&softc->ctl_lock); 11898 return (1); 11899 } 11900 11901 mtx_lock(&lun->lun_lock); 11902 mtx_unlock(&softc->ctl_lock); 11903 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11904 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11905 io->io_hdr.nexus.initid.id, 11906 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11907 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11908 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11909 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11910 } 11911 mtx_unlock(&lun->lun_lock); 11912 return (0); 11913} 11914 11915static int 11916ctl_i_t_nexus_reset(union ctl_io *io) 11917{ 11918 struct ctl_softc *softc = control_softc; 11919 struct ctl_lun *lun; 11920 uint32_t initidx, residx; 11921 11922 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11923 residx = ctl_get_resindex(&io->io_hdr.nexus); 11924 mtx_lock(&softc->ctl_lock); 11925 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11926 mtx_lock(&lun->lun_lock); 11927 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11928 io->io_hdr.nexus.initid.id, 11929 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11930#ifdef CTL_WITH_CA 11931 ctl_clear_mask(lun->have_ca, initidx); 11932#endif 11933 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 11934 lun->flags &= ~CTL_LUN_RESERVED; 11935 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 11936 mtx_unlock(&lun->lun_lock); 11937 } 11938 mtx_unlock(&softc->ctl_lock); 11939 return (0); 11940} 11941 11942static int 11943ctl_abort_task(union ctl_io *io) 11944{ 11945 union ctl_io *xio; 11946 struct ctl_lun *lun; 11947 struct ctl_softc *ctl_softc; 11948#if 0 11949 struct sbuf sb; 11950 char printbuf[128]; 11951#endif 11952 int found; 11953 uint32_t targ_lun; 11954 11955 ctl_softc = control_softc; 11956 found = 0; 11957 11958 /* 11959 * Look up the LUN. 11960 */ 11961 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11962 mtx_lock(&ctl_softc->ctl_lock); 11963 if ((targ_lun < CTL_MAX_LUNS) 11964 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11965 lun = ctl_softc->ctl_luns[targ_lun]; 11966 else { 11967 mtx_unlock(&ctl_softc->ctl_lock); 11968 return (1); 11969 } 11970 11971#if 0 11972 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11973 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11974#endif 11975 11976 mtx_lock(&lun->lun_lock); 11977 mtx_unlock(&ctl_softc->ctl_lock); 11978 /* 11979 * Run through the OOA queue and attempt to find the given I/O. 11980 * The target port, initiator ID, tag type and tag number have to 11981 * match the values that we got from the initiator. If we have an 11982 * untagged command to abort, simply abort the first untagged command 11983 * we come to. We only allow one untagged command at a time of course. 11984 */ 11985#if 0 11986 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11987#endif 11988 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11989 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11990#if 0 11991 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11992 11993 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11994 lun->lun, xio->scsiio.tag_num, 11995 xio->scsiio.tag_type, 11996 (xio->io_hdr.blocked_links.tqe_prev 11997 == NULL) ? "" : " BLOCKED", 11998 (xio->io_hdr.flags & 11999 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12000 (xio->io_hdr.flags & 12001 CTL_FLAG_ABORT) ? " ABORT" : "", 12002 (xio->io_hdr.flags & 12003 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12004 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12005 sbuf_finish(&sb); 12006 printf("%s\n", sbuf_data(&sb)); 12007#endif 12008 12009 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12010 && (xio->io_hdr.nexus.initid.id == 12011 io->io_hdr.nexus.initid.id)) { 12012 /* 12013 * If the abort says that the task is untagged, the 12014 * task in the queue must be untagged. Otherwise, 12015 * we just check to see whether the tag numbers 12016 * match. This is because the QLogic firmware 12017 * doesn't pass back the tag type in an abort 12018 * request. 12019 */ 12020#if 0 12021 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12022 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12023 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12024#endif 12025 /* 12026 * XXX KDM we've got problems with FC, because it 12027 * doesn't send down a tag type with aborts. So we 12028 * can only really go by the tag number... 12029 * This may cause problems with parallel SCSI. 12030 * Need to figure that out!! 12031 */ 12032 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12033 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12034 found = 1; 12035 if ((io->io_hdr.flags & 12036 CTL_FLAG_FROM_OTHER_SC) == 0 && 12037 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12038 union ctl_ha_msg msg_info; 12039 12040 io->io_hdr.flags |= 12041 CTL_FLAG_SENT_2OTHER_SC; 12042 msg_info.hdr.nexus = io->io_hdr.nexus; 12043 msg_info.task.task_action = 12044 CTL_TASK_ABORT_TASK; 12045 msg_info.task.tag_num = 12046 io->taskio.tag_num; 12047 msg_info.task.tag_type = 12048 io->taskio.tag_type; 12049 msg_info.hdr.msg_type = 12050 CTL_MSG_MANAGE_TASKS; 12051 msg_info.hdr.original_sc = NULL; 12052 msg_info.hdr.serializing_sc = NULL; 12053#if 0 12054 printf("Sent Abort to other side\n"); 12055#endif 12056 if (CTL_HA_STATUS_SUCCESS != 12057 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12058 (void *)&msg_info, 12059 sizeof(msg_info), 0)) { 12060 } 12061 } 12062#if 0 12063 printf("ctl_abort_task: found I/O to abort\n"); 12064#endif 12065 break; 12066 } 12067 } 12068 } 12069 mtx_unlock(&lun->lun_lock); 12070 12071 if (found == 0) { 12072 /* 12073 * This isn't really an error. It's entirely possible for 12074 * the abort and command completion to cross on the wire. 12075 * This is more of an informative/diagnostic error. 12076 */ 12077#if 0 12078 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12079 "%d:%d:%d:%d tag %d type %d\n", 12080 io->io_hdr.nexus.initid.id, 12081 io->io_hdr.nexus.targ_port, 12082 io->io_hdr.nexus.targ_target.id, 12083 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12084 io->taskio.tag_type); 12085#endif 12086 } 12087 return (0); 12088} 12089 12090static void 12091ctl_run_task(union ctl_io *io) 12092{ 12093 struct ctl_softc *ctl_softc = control_softc; 12094 int retval = 1; 12095 const char *task_desc; 12096 12097 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12098 12099 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12100 ("ctl_run_task: Unextected io_type %d\n", 12101 io->io_hdr.io_type)); 12102 12103 task_desc = ctl_scsi_task_string(&io->taskio); 12104 if (task_desc != NULL) { 12105#ifdef NEEDTOPORT 12106 csevent_log(CSC_CTL | CSC_SHELF_SW | 12107 CTL_TASK_REPORT, 12108 csevent_LogType_Trace, 12109 csevent_Severity_Information, 12110 csevent_AlertLevel_Green, 12111 csevent_FRU_Firmware, 12112 csevent_FRU_Unknown, 12113 "CTL: received task: %s",task_desc); 12114#endif 12115 } else { 12116#ifdef NEEDTOPORT 12117 csevent_log(CSC_CTL | CSC_SHELF_SW | 12118 CTL_TASK_REPORT, 12119 csevent_LogType_Trace, 12120 csevent_Severity_Information, 12121 csevent_AlertLevel_Green, 12122 csevent_FRU_Firmware, 12123 csevent_FRU_Unknown, 12124 "CTL: received unknown task " 12125 "type: %d (%#x)", 12126 io->taskio.task_action, 12127 io->taskio.task_action); 12128#endif 12129 } 12130 switch (io->taskio.task_action) { 12131 case CTL_TASK_ABORT_TASK: 12132 retval = ctl_abort_task(io); 12133 break; 12134 case CTL_TASK_ABORT_TASK_SET: 12135 case CTL_TASK_CLEAR_TASK_SET: 12136 retval = ctl_abort_task_set(io); 12137 break; 12138 case CTL_TASK_CLEAR_ACA: 12139 break; 12140 case CTL_TASK_I_T_NEXUS_RESET: 12141 retval = ctl_i_t_nexus_reset(io); 12142 break; 12143 case CTL_TASK_LUN_RESET: { 12144 struct ctl_lun *lun; 12145 uint32_t targ_lun; 12146 12147 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12148 mtx_lock(&ctl_softc->ctl_lock); 12149 if ((targ_lun < CTL_MAX_LUNS) 12150 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12151 lun = ctl_softc->ctl_luns[targ_lun]; 12152 else { 12153 mtx_unlock(&ctl_softc->ctl_lock); 12154 retval = 1; 12155 break; 12156 } 12157 12158 if (!(io->io_hdr.flags & 12159 CTL_FLAG_FROM_OTHER_SC)) { 12160 union ctl_ha_msg msg_info; 12161 12162 io->io_hdr.flags |= 12163 CTL_FLAG_SENT_2OTHER_SC; 12164 msg_info.hdr.msg_type = 12165 CTL_MSG_MANAGE_TASKS; 12166 msg_info.hdr.nexus = io->io_hdr.nexus; 12167 msg_info.task.task_action = 12168 CTL_TASK_LUN_RESET; 12169 msg_info.hdr.original_sc = NULL; 12170 msg_info.hdr.serializing_sc = NULL; 12171 if (CTL_HA_STATUS_SUCCESS != 12172 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12173 (void *)&msg_info, 12174 sizeof(msg_info), 0)) { 12175 } 12176 } 12177 12178 retval = ctl_lun_reset(lun, io, 12179 CTL_UA_LUN_RESET); 12180 mtx_unlock(&ctl_softc->ctl_lock); 12181 break; 12182 } 12183 case CTL_TASK_TARGET_RESET: 12184 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12185 break; 12186 case CTL_TASK_BUS_RESET: 12187 retval = ctl_bus_reset(ctl_softc, io); 12188 break; 12189 case CTL_TASK_PORT_LOGIN: 12190 break; 12191 case CTL_TASK_PORT_LOGOUT: 12192 break; 12193 default: 12194 printf("ctl_run_task: got unknown task management event %d\n", 12195 io->taskio.task_action); 12196 break; 12197 } 12198 if (retval == 0) 12199 io->io_hdr.status = CTL_SUCCESS; 12200 else 12201 io->io_hdr.status = CTL_ERROR; 12202 ctl_done(io); 12203} 12204 12205/* 12206 * For HA operation. Handle commands that come in from the other 12207 * controller. 12208 */ 12209static void 12210ctl_handle_isc(union ctl_io *io) 12211{ 12212 int free_io; 12213 struct ctl_lun *lun; 12214 struct ctl_softc *ctl_softc; 12215 uint32_t targ_lun; 12216 12217 ctl_softc = control_softc; 12218 12219 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12220 lun = ctl_softc->ctl_luns[targ_lun]; 12221 12222 switch (io->io_hdr.msg_type) { 12223 case CTL_MSG_SERIALIZE: 12224 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12225 break; 12226 case CTL_MSG_R2R: { 12227 const struct ctl_cmd_entry *entry; 12228 12229 /* 12230 * This is only used in SER_ONLY mode. 12231 */ 12232 free_io = 0; 12233 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12234 mtx_lock(&lun->lun_lock); 12235 if (ctl_scsiio_lun_check(ctl_softc, lun, 12236 entry, (struct ctl_scsiio *)io) != 0) { 12237 mtx_unlock(&lun->lun_lock); 12238 ctl_done(io); 12239 break; 12240 } 12241 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12242 mtx_unlock(&lun->lun_lock); 12243 ctl_enqueue_rtr(io); 12244 break; 12245 } 12246 case CTL_MSG_FINISH_IO: 12247 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12248 free_io = 0; 12249 ctl_done(io); 12250 } else { 12251 free_io = 1; 12252 mtx_lock(&lun->lun_lock); 12253 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12254 ooa_links); 12255 ctl_check_blocked(lun); 12256 mtx_unlock(&lun->lun_lock); 12257 } 12258 break; 12259 case CTL_MSG_PERS_ACTION: 12260 ctl_hndl_per_res_out_on_other_sc( 12261 (union ctl_ha_msg *)&io->presio.pr_msg); 12262 free_io = 1; 12263 break; 12264 case CTL_MSG_BAD_JUJU: 12265 free_io = 0; 12266 ctl_done(io); 12267 break; 12268 case CTL_MSG_DATAMOVE: 12269 /* Only used in XFER mode */ 12270 free_io = 0; 12271 ctl_datamove_remote(io); 12272 break; 12273 case CTL_MSG_DATAMOVE_DONE: 12274 /* Only used in XFER mode */ 12275 free_io = 0; 12276 io->scsiio.be_move_done(io); 12277 break; 12278 default: 12279 free_io = 1; 12280 printf("%s: Invalid message type %d\n", 12281 __func__, io->io_hdr.msg_type); 12282 break; 12283 } 12284 if (free_io) 12285 ctl_free_io(io); 12286 12287} 12288 12289 12290/* 12291 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12292 * there is no match. 12293 */ 12294static ctl_lun_error_pattern 12295ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12296{ 12297 const struct ctl_cmd_entry *entry; 12298 ctl_lun_error_pattern filtered_pattern, pattern; 12299 12300 pattern = desc->error_pattern; 12301 12302 /* 12303 * XXX KDM we need more data passed into this function to match a 12304 * custom pattern, and we actually need to implement custom pattern 12305 * matching. 12306 */ 12307 if (pattern & CTL_LUN_PAT_CMD) 12308 return (CTL_LUN_PAT_CMD); 12309 12310 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12311 return (CTL_LUN_PAT_ANY); 12312 12313 entry = ctl_get_cmd_entry(ctsio, NULL); 12314 12315 filtered_pattern = entry->pattern & pattern; 12316 12317 /* 12318 * If the user requested specific flags in the pattern (e.g. 12319 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12320 * flags. 12321 * 12322 * If the user did not specify any flags, it doesn't matter whether 12323 * or not the command supports the flags. 12324 */ 12325 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12326 (pattern & ~CTL_LUN_PAT_MASK)) 12327 return (CTL_LUN_PAT_NONE); 12328 12329 /* 12330 * If the user asked for a range check, see if the requested LBA 12331 * range overlaps with this command's LBA range. 12332 */ 12333 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12334 uint64_t lba1; 12335 uint64_t len1; 12336 ctl_action action; 12337 int retval; 12338 12339 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12340 if (retval != 0) 12341 return (CTL_LUN_PAT_NONE); 12342 12343 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12344 desc->lba_range.len); 12345 /* 12346 * A "pass" means that the LBA ranges don't overlap, so 12347 * this doesn't match the user's range criteria. 12348 */ 12349 if (action == CTL_ACTION_PASS) 12350 return (CTL_LUN_PAT_NONE); 12351 } 12352 12353 return (filtered_pattern); 12354} 12355 12356static void 12357ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12358{ 12359 struct ctl_error_desc *desc, *desc2; 12360 12361 mtx_assert(&lun->lun_lock, MA_OWNED); 12362 12363 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12364 ctl_lun_error_pattern pattern; 12365 /* 12366 * Check to see whether this particular command matches 12367 * the pattern in the descriptor. 12368 */ 12369 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12370 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12371 continue; 12372 12373 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12374 case CTL_LUN_INJ_ABORTED: 12375 ctl_set_aborted(&io->scsiio); 12376 break; 12377 case CTL_LUN_INJ_MEDIUM_ERR: 12378 ctl_set_medium_error(&io->scsiio); 12379 break; 12380 case CTL_LUN_INJ_UA: 12381 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12382 * OCCURRED */ 12383 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12384 break; 12385 case CTL_LUN_INJ_CUSTOM: 12386 /* 12387 * We're assuming the user knows what he is doing. 12388 * Just copy the sense information without doing 12389 * checks. 12390 */ 12391 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12392 ctl_min(sizeof(desc->custom_sense), 12393 sizeof(io->scsiio.sense_data))); 12394 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12395 io->scsiio.sense_len = SSD_FULL_SIZE; 12396 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12397 break; 12398 case CTL_LUN_INJ_NONE: 12399 default: 12400 /* 12401 * If this is an error injection type we don't know 12402 * about, clear the continuous flag (if it is set) 12403 * so it will get deleted below. 12404 */ 12405 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12406 break; 12407 } 12408 /* 12409 * By default, each error injection action is a one-shot 12410 */ 12411 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12412 continue; 12413 12414 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12415 12416 free(desc, M_CTL); 12417 } 12418} 12419 12420#ifdef CTL_IO_DELAY 12421static void 12422ctl_datamove_timer_wakeup(void *arg) 12423{ 12424 union ctl_io *io; 12425 12426 io = (union ctl_io *)arg; 12427 12428 ctl_datamove(io); 12429} 12430#endif /* CTL_IO_DELAY */ 12431 12432void 12433ctl_datamove(union ctl_io *io) 12434{ 12435 void (*fe_datamove)(union ctl_io *io); 12436 12437 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12438 12439 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12440 12441#ifdef CTL_TIME_IO 12442 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12443 char str[256]; 12444 char path_str[64]; 12445 struct sbuf sb; 12446 12447 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12448 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12449 12450 sbuf_cat(&sb, path_str); 12451 switch (io->io_hdr.io_type) { 12452 case CTL_IO_SCSI: 12453 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12454 sbuf_printf(&sb, "\n"); 12455 sbuf_cat(&sb, path_str); 12456 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12457 io->scsiio.tag_num, io->scsiio.tag_type); 12458 break; 12459 case CTL_IO_TASK: 12460 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12461 "Tag Type: %d\n", io->taskio.task_action, 12462 io->taskio.tag_num, io->taskio.tag_type); 12463 break; 12464 default: 12465 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12466 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12467 break; 12468 } 12469 sbuf_cat(&sb, path_str); 12470 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12471 (intmax_t)time_uptime - io->io_hdr.start_time); 12472 sbuf_finish(&sb); 12473 printf("%s", sbuf_data(&sb)); 12474 } 12475#endif /* CTL_TIME_IO */ 12476 12477#ifdef CTL_IO_DELAY 12478 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12479 struct ctl_lun *lun; 12480 12481 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12482 12483 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12484 } else { 12485 struct ctl_lun *lun; 12486 12487 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12488 if ((lun != NULL) 12489 && (lun->delay_info.datamove_delay > 0)) { 12490 struct callout *callout; 12491 12492 callout = (struct callout *)&io->io_hdr.timer_bytes; 12493 callout_init(callout, /*mpsafe*/ 1); 12494 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12495 callout_reset(callout, 12496 lun->delay_info.datamove_delay * hz, 12497 ctl_datamove_timer_wakeup, io); 12498 if (lun->delay_info.datamove_type == 12499 CTL_DELAY_TYPE_ONESHOT) 12500 lun->delay_info.datamove_delay = 0; 12501 return; 12502 } 12503 } 12504#endif 12505 12506 /* 12507 * This command has been aborted. Set the port status, so we fail 12508 * the data move. 12509 */ 12510 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12511 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12512 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12513 io->io_hdr.nexus.targ_port, 12514 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12515 io->io_hdr.nexus.targ_lun); 12516 io->io_hdr.port_status = 31337; 12517 /* 12518 * Note that the backend, in this case, will get the 12519 * callback in its context. In other cases it may get 12520 * called in the frontend's interrupt thread context. 12521 */ 12522 io->scsiio.be_move_done(io); 12523 return; 12524 } 12525 12526 /* Don't confuse frontend with zero length data move. */ 12527 if (io->scsiio.kern_data_len == 0) { 12528 io->scsiio.be_move_done(io); 12529 return; 12530 } 12531 12532 /* 12533 * If we're in XFER mode and this I/O is from the other shelf 12534 * controller, we need to send the DMA to the other side to 12535 * actually transfer the data to/from the host. In serialize only 12536 * mode the transfer happens below CTL and ctl_datamove() is only 12537 * called on the machine that originally received the I/O. 12538 */ 12539 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12540 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12541 union ctl_ha_msg msg; 12542 uint32_t sg_entries_sent; 12543 int do_sg_copy; 12544 int i; 12545 12546 memset(&msg, 0, sizeof(msg)); 12547 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12548 msg.hdr.original_sc = io->io_hdr.original_sc; 12549 msg.hdr.serializing_sc = io; 12550 msg.hdr.nexus = io->io_hdr.nexus; 12551 msg.dt.flags = io->io_hdr.flags; 12552 /* 12553 * We convert everything into a S/G list here. We can't 12554 * pass by reference, only by value between controllers. 12555 * So we can't pass a pointer to the S/G list, only as many 12556 * S/G entries as we can fit in here. If it's possible for 12557 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12558 * then we need to break this up into multiple transfers. 12559 */ 12560 if (io->scsiio.kern_sg_entries == 0) { 12561 msg.dt.kern_sg_entries = 1; 12562 /* 12563 * If this is in cached memory, flush the cache 12564 * before we send the DMA request to the other 12565 * controller. We want to do this in either the 12566 * read or the write case. The read case is 12567 * straightforward. In the write case, we want to 12568 * make sure nothing is in the local cache that 12569 * could overwrite the DMAed data. 12570 */ 12571 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12572 /* 12573 * XXX KDM use bus_dmamap_sync() here. 12574 */ 12575 } 12576 12577 /* 12578 * Convert to a physical address if this is a 12579 * virtual address. 12580 */ 12581 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12582 msg.dt.sg_list[0].addr = 12583 io->scsiio.kern_data_ptr; 12584 } else { 12585 /* 12586 * XXX KDM use busdma here! 12587 */ 12588#if 0 12589 msg.dt.sg_list[0].addr = (void *) 12590 vtophys(io->scsiio.kern_data_ptr); 12591#endif 12592 } 12593 12594 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12595 do_sg_copy = 0; 12596 } else { 12597 struct ctl_sg_entry *sgl; 12598 12599 do_sg_copy = 1; 12600 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12601 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12602 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12603 /* 12604 * XXX KDM use bus_dmamap_sync() here. 12605 */ 12606 } 12607 } 12608 12609 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12610 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12611 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12612 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12613 msg.dt.sg_sequence = 0; 12614 12615 /* 12616 * Loop until we've sent all of the S/G entries. On the 12617 * other end, we'll recompose these S/G entries into one 12618 * contiguous list before passing it to the 12619 */ 12620 for (sg_entries_sent = 0; sg_entries_sent < 12621 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12622 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12623 sizeof(msg.dt.sg_list[0])), 12624 msg.dt.kern_sg_entries - sg_entries_sent); 12625 12626 if (do_sg_copy != 0) { 12627 struct ctl_sg_entry *sgl; 12628 int j; 12629 12630 sgl = (struct ctl_sg_entry *) 12631 io->scsiio.kern_data_ptr; 12632 /* 12633 * If this is in cached memory, flush the cache 12634 * before we send the DMA request to the other 12635 * controller. We want to do this in either 12636 * the * read or the write case. The read 12637 * case is straightforward. In the write 12638 * case, we want to make sure nothing is 12639 * in the local cache that could overwrite 12640 * the DMAed data. 12641 */ 12642 12643 for (i = sg_entries_sent, j = 0; 12644 i < msg.dt.cur_sg_entries; i++, j++) { 12645 if ((io->io_hdr.flags & 12646 CTL_FLAG_NO_DATASYNC) == 0) { 12647 /* 12648 * XXX KDM use bus_dmamap_sync() 12649 */ 12650 } 12651 if ((io->io_hdr.flags & 12652 CTL_FLAG_BUS_ADDR) == 0) { 12653 /* 12654 * XXX KDM use busdma. 12655 */ 12656#if 0 12657 msg.dt.sg_list[j].addr =(void *) 12658 vtophys(sgl[i].addr); 12659#endif 12660 } else { 12661 msg.dt.sg_list[j].addr = 12662 sgl[i].addr; 12663 } 12664 msg.dt.sg_list[j].len = sgl[i].len; 12665 } 12666 } 12667 12668 sg_entries_sent += msg.dt.cur_sg_entries; 12669 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12670 msg.dt.sg_last = 1; 12671 else 12672 msg.dt.sg_last = 0; 12673 12674 /* 12675 * XXX KDM drop and reacquire the lock here? 12676 */ 12677 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12678 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12679 /* 12680 * XXX do something here. 12681 */ 12682 } 12683 12684 msg.dt.sent_sg_entries = sg_entries_sent; 12685 } 12686 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12687 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12688 ctl_failover_io(io, /*have_lock*/ 0); 12689 12690 } else { 12691 12692 /* 12693 * Lookup the fe_datamove() function for this particular 12694 * front end. 12695 */ 12696 fe_datamove = 12697 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12698 12699 fe_datamove(io); 12700 } 12701} 12702 12703static void 12704ctl_send_datamove_done(union ctl_io *io, int have_lock) 12705{ 12706 union ctl_ha_msg msg; 12707 int isc_status; 12708 12709 memset(&msg, 0, sizeof(msg)); 12710 12711 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12712 msg.hdr.original_sc = io; 12713 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12714 msg.hdr.nexus = io->io_hdr.nexus; 12715 msg.hdr.status = io->io_hdr.status; 12716 msg.scsi.tag_num = io->scsiio.tag_num; 12717 msg.scsi.tag_type = io->scsiio.tag_type; 12718 msg.scsi.scsi_status = io->scsiio.scsi_status; 12719 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12720 sizeof(io->scsiio.sense_data)); 12721 msg.scsi.sense_len = io->scsiio.sense_len; 12722 msg.scsi.sense_residual = io->scsiio.sense_residual; 12723 msg.scsi.fetd_status = io->io_hdr.port_status; 12724 msg.scsi.residual = io->scsiio.residual; 12725 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12726 12727 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12728 ctl_failover_io(io, /*have_lock*/ have_lock); 12729 return; 12730 } 12731 12732 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12733 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12734 /* XXX do something if this fails */ 12735 } 12736 12737} 12738 12739/* 12740 * The DMA to the remote side is done, now we need to tell the other side 12741 * we're done so it can continue with its data movement. 12742 */ 12743static void 12744ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12745{ 12746 union ctl_io *io; 12747 12748 io = rq->context; 12749 12750 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12751 printf("%s: ISC DMA write failed with error %d", __func__, 12752 rq->ret); 12753 ctl_set_internal_failure(&io->scsiio, 12754 /*sks_valid*/ 1, 12755 /*retry_count*/ rq->ret); 12756 } 12757 12758 ctl_dt_req_free(rq); 12759 12760 /* 12761 * In this case, we had to malloc the memory locally. Free it. 12762 */ 12763 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12764 int i; 12765 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12766 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12767 } 12768 /* 12769 * The data is in local and remote memory, so now we need to send 12770 * status (good or back) back to the other side. 12771 */ 12772 ctl_send_datamove_done(io, /*have_lock*/ 0); 12773} 12774 12775/* 12776 * We've moved the data from the host/controller into local memory. Now we 12777 * need to push it over to the remote controller's memory. 12778 */ 12779static int 12780ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12781{ 12782 int retval; 12783 12784 retval = 0; 12785 12786 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12787 ctl_datamove_remote_write_cb); 12788 12789 return (retval); 12790} 12791 12792static void 12793ctl_datamove_remote_write(union ctl_io *io) 12794{ 12795 int retval; 12796 void (*fe_datamove)(union ctl_io *io); 12797 12798 /* 12799 * - Get the data from the host/HBA into local memory. 12800 * - DMA memory from the local controller to the remote controller. 12801 * - Send status back to the remote controller. 12802 */ 12803 12804 retval = ctl_datamove_remote_sgl_setup(io); 12805 if (retval != 0) 12806 return; 12807 12808 /* Switch the pointer over so the FETD knows what to do */ 12809 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12810 12811 /* 12812 * Use a custom move done callback, since we need to send completion 12813 * back to the other controller, not to the backend on this side. 12814 */ 12815 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12816 12817 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12818 12819 fe_datamove(io); 12820 12821 return; 12822 12823} 12824 12825static int 12826ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12827{ 12828#if 0 12829 char str[256]; 12830 char path_str[64]; 12831 struct sbuf sb; 12832#endif 12833 12834 /* 12835 * In this case, we had to malloc the memory locally. Free it. 12836 */ 12837 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12838 int i; 12839 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12840 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12841 } 12842 12843#if 0 12844 scsi_path_string(io, path_str, sizeof(path_str)); 12845 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12846 sbuf_cat(&sb, path_str); 12847 scsi_command_string(&io->scsiio, NULL, &sb); 12848 sbuf_printf(&sb, "\n"); 12849 sbuf_cat(&sb, path_str); 12850 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12851 io->scsiio.tag_num, io->scsiio.tag_type); 12852 sbuf_cat(&sb, path_str); 12853 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12854 io->io_hdr.flags, io->io_hdr.status); 12855 sbuf_finish(&sb); 12856 printk("%s", sbuf_data(&sb)); 12857#endif 12858 12859 12860 /* 12861 * The read is done, now we need to send status (good or bad) back 12862 * to the other side. 12863 */ 12864 ctl_send_datamove_done(io, /*have_lock*/ 0); 12865 12866 return (0); 12867} 12868 12869static void 12870ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12871{ 12872 union ctl_io *io; 12873 void (*fe_datamove)(union ctl_io *io); 12874 12875 io = rq->context; 12876 12877 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12878 printf("%s: ISC DMA read failed with error %d", __func__, 12879 rq->ret); 12880 ctl_set_internal_failure(&io->scsiio, 12881 /*sks_valid*/ 1, 12882 /*retry_count*/ rq->ret); 12883 } 12884 12885 ctl_dt_req_free(rq); 12886 12887 /* Switch the pointer over so the FETD knows what to do */ 12888 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12889 12890 /* 12891 * Use a custom move done callback, since we need to send completion 12892 * back to the other controller, not to the backend on this side. 12893 */ 12894 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12895 12896 /* XXX KDM add checks like the ones in ctl_datamove? */ 12897 12898 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12899 12900 fe_datamove(io); 12901} 12902 12903static int 12904ctl_datamove_remote_sgl_setup(union ctl_io *io) 12905{ 12906 struct ctl_sg_entry *local_sglist, *remote_sglist; 12907 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12908 struct ctl_softc *softc; 12909 int retval; 12910 int i; 12911 12912 retval = 0; 12913 softc = control_softc; 12914 12915 local_sglist = io->io_hdr.local_sglist; 12916 local_dma_sglist = io->io_hdr.local_dma_sglist; 12917 remote_sglist = io->io_hdr.remote_sglist; 12918 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12919 12920 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12921 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12922 local_sglist[i].len = remote_sglist[i].len; 12923 12924 /* 12925 * XXX Detect the situation where the RS-level I/O 12926 * redirector on the other side has already read the 12927 * data off of the AOR RS on this side, and 12928 * transferred it to remote (mirror) memory on the 12929 * other side. Since we already have the data in 12930 * memory here, we just need to use it. 12931 * 12932 * XXX KDM this can probably be removed once we 12933 * get the cache device code in and take the 12934 * current AOR implementation out. 12935 */ 12936#ifdef NEEDTOPORT 12937 if ((remote_sglist[i].addr >= 12938 (void *)vtophys(softc->mirr->addr)) 12939 && (remote_sglist[i].addr < 12940 ((void *)vtophys(softc->mirr->addr) + 12941 CacheMirrorOffset))) { 12942 local_sglist[i].addr = remote_sglist[i].addr - 12943 CacheMirrorOffset; 12944 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12945 CTL_FLAG_DATA_IN) 12946 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12947 } else { 12948 local_sglist[i].addr = remote_sglist[i].addr + 12949 CacheMirrorOffset; 12950 } 12951#endif 12952#if 0 12953 printf("%s: local %p, remote %p, len %d\n", 12954 __func__, local_sglist[i].addr, 12955 remote_sglist[i].addr, local_sglist[i].len); 12956#endif 12957 } 12958 } else { 12959 uint32_t len_to_go; 12960 12961 /* 12962 * In this case, we don't have automatically allocated 12963 * memory for this I/O on this controller. This typically 12964 * happens with internal CTL I/O -- e.g. inquiry, mode 12965 * sense, etc. Anything coming from RAIDCore will have 12966 * a mirror area available. 12967 */ 12968 len_to_go = io->scsiio.kern_data_len; 12969 12970 /* 12971 * Clear the no datasync flag, we have to use malloced 12972 * buffers. 12973 */ 12974 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12975 12976 /* 12977 * The difficult thing here is that the size of the various 12978 * S/G segments may be different than the size from the 12979 * remote controller. That'll make it harder when DMAing 12980 * the data back to the other side. 12981 */ 12982 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12983 sizeof(io->io_hdr.remote_sglist[0])) && 12984 (len_to_go > 0); i++) { 12985 local_sglist[i].len = ctl_min(len_to_go, 131072); 12986 CTL_SIZE_8B(local_dma_sglist[i].len, 12987 local_sglist[i].len); 12988 local_sglist[i].addr = 12989 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12990 12991 local_dma_sglist[i].addr = local_sglist[i].addr; 12992 12993 if (local_sglist[i].addr == NULL) { 12994 int j; 12995 12996 printf("malloc failed for %zd bytes!", 12997 local_dma_sglist[i].len); 12998 for (j = 0; j < i; j++) { 12999 free(local_sglist[j].addr, M_CTL); 13000 } 13001 ctl_set_internal_failure(&io->scsiio, 13002 /*sks_valid*/ 1, 13003 /*retry_count*/ 4857); 13004 retval = 1; 13005 goto bailout_error; 13006 13007 } 13008 /* XXX KDM do we need a sync here? */ 13009 13010 len_to_go -= local_sglist[i].len; 13011 } 13012 /* 13013 * Reset the number of S/G entries accordingly. The 13014 * original number of S/G entries is available in 13015 * rem_sg_entries. 13016 */ 13017 io->scsiio.kern_sg_entries = i; 13018 13019#if 0 13020 printf("%s: kern_sg_entries = %d\n", __func__, 13021 io->scsiio.kern_sg_entries); 13022 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13023 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13024 local_sglist[i].addr, local_sglist[i].len, 13025 local_dma_sglist[i].len); 13026#endif 13027 } 13028 13029 13030 return (retval); 13031 13032bailout_error: 13033 13034 ctl_send_datamove_done(io, /*have_lock*/ 0); 13035 13036 return (retval); 13037} 13038 13039static int 13040ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13041 ctl_ha_dt_cb callback) 13042{ 13043 struct ctl_ha_dt_req *rq; 13044 struct ctl_sg_entry *remote_sglist, *local_sglist; 13045 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13046 uint32_t local_used, remote_used, total_used; 13047 int retval; 13048 int i, j; 13049 13050 retval = 0; 13051 13052 rq = ctl_dt_req_alloc(); 13053 13054 /* 13055 * If we failed to allocate the request, and if the DMA didn't fail 13056 * anyway, set busy status. This is just a resource allocation 13057 * failure. 13058 */ 13059 if ((rq == NULL) 13060 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13061 ctl_set_busy(&io->scsiio); 13062 13063 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13064 13065 if (rq != NULL) 13066 ctl_dt_req_free(rq); 13067 13068 /* 13069 * The data move failed. We need to return status back 13070 * to the other controller. No point in trying to DMA 13071 * data to the remote controller. 13072 */ 13073 13074 ctl_send_datamove_done(io, /*have_lock*/ 0); 13075 13076 retval = 1; 13077 13078 goto bailout; 13079 } 13080 13081 local_sglist = io->io_hdr.local_sglist; 13082 local_dma_sglist = io->io_hdr.local_dma_sglist; 13083 remote_sglist = io->io_hdr.remote_sglist; 13084 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13085 local_used = 0; 13086 remote_used = 0; 13087 total_used = 0; 13088 13089 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13090 rq->ret = CTL_HA_STATUS_SUCCESS; 13091 rq->context = io; 13092 callback(rq); 13093 goto bailout; 13094 } 13095 13096 /* 13097 * Pull/push the data over the wire from/to the other controller. 13098 * This takes into account the possibility that the local and 13099 * remote sglists may not be identical in terms of the size of 13100 * the elements and the number of elements. 13101 * 13102 * One fundamental assumption here is that the length allocated for 13103 * both the local and remote sglists is identical. Otherwise, we've 13104 * essentially got a coding error of some sort. 13105 */ 13106 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13107 int isc_ret; 13108 uint32_t cur_len, dma_length; 13109 uint8_t *tmp_ptr; 13110 13111 rq->id = CTL_HA_DATA_CTL; 13112 rq->command = command; 13113 rq->context = io; 13114 13115 /* 13116 * Both pointers should be aligned. But it is possible 13117 * that the allocation length is not. They should both 13118 * also have enough slack left over at the end, though, 13119 * to round up to the next 8 byte boundary. 13120 */ 13121 cur_len = ctl_min(local_sglist[i].len - local_used, 13122 remote_sglist[j].len - remote_used); 13123 13124 /* 13125 * In this case, we have a size issue and need to decrease 13126 * the size, except in the case where we actually have less 13127 * than 8 bytes left. In that case, we need to increase 13128 * the DMA length to get the last bit. 13129 */ 13130 if ((cur_len & 0x7) != 0) { 13131 if (cur_len > 0x7) { 13132 cur_len = cur_len - (cur_len & 0x7); 13133 dma_length = cur_len; 13134 } else { 13135 CTL_SIZE_8B(dma_length, cur_len); 13136 } 13137 13138 } else 13139 dma_length = cur_len; 13140 13141 /* 13142 * If we had to allocate memory for this I/O, instead of using 13143 * the non-cached mirror memory, we'll need to flush the cache 13144 * before trying to DMA to the other controller. 13145 * 13146 * We could end up doing this multiple times for the same 13147 * segment if we have a larger local segment than remote 13148 * segment. That shouldn't be an issue. 13149 */ 13150 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13151 /* 13152 * XXX KDM use bus_dmamap_sync() here. 13153 */ 13154 } 13155 13156 rq->size = dma_length; 13157 13158 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13159 tmp_ptr += local_used; 13160 13161 /* Use physical addresses when talking to ISC hardware */ 13162 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13163 /* XXX KDM use busdma */ 13164#if 0 13165 rq->local = vtophys(tmp_ptr); 13166#endif 13167 } else 13168 rq->local = tmp_ptr; 13169 13170 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13171 tmp_ptr += remote_used; 13172 rq->remote = tmp_ptr; 13173 13174 rq->callback = NULL; 13175 13176 local_used += cur_len; 13177 if (local_used >= local_sglist[i].len) { 13178 i++; 13179 local_used = 0; 13180 } 13181 13182 remote_used += cur_len; 13183 if (remote_used >= remote_sglist[j].len) { 13184 j++; 13185 remote_used = 0; 13186 } 13187 total_used += cur_len; 13188 13189 if (total_used >= io->scsiio.kern_data_len) 13190 rq->callback = callback; 13191 13192 if ((rq->size & 0x7) != 0) { 13193 printf("%s: warning: size %d is not on 8b boundary\n", 13194 __func__, rq->size); 13195 } 13196 if (((uintptr_t)rq->local & 0x7) != 0) { 13197 printf("%s: warning: local %p not on 8b boundary\n", 13198 __func__, rq->local); 13199 } 13200 if (((uintptr_t)rq->remote & 0x7) != 0) { 13201 printf("%s: warning: remote %p not on 8b boundary\n", 13202 __func__, rq->local); 13203 } 13204#if 0 13205 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13206 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13207 rq->local, rq->remote, rq->size); 13208#endif 13209 13210 isc_ret = ctl_dt_single(rq); 13211 if (isc_ret == CTL_HA_STATUS_WAIT) 13212 continue; 13213 13214 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13215 rq->ret = CTL_HA_STATUS_SUCCESS; 13216 } else { 13217 rq->ret = isc_ret; 13218 } 13219 callback(rq); 13220 goto bailout; 13221 } 13222 13223bailout: 13224 return (retval); 13225 13226} 13227 13228static void 13229ctl_datamove_remote_read(union ctl_io *io) 13230{ 13231 int retval; 13232 int i; 13233 13234 /* 13235 * This will send an error to the other controller in the case of a 13236 * failure. 13237 */ 13238 retval = ctl_datamove_remote_sgl_setup(io); 13239 if (retval != 0) 13240 return; 13241 13242 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13243 ctl_datamove_remote_read_cb); 13244 if ((retval != 0) 13245 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13246 /* 13247 * Make sure we free memory if there was an error.. The 13248 * ctl_datamove_remote_xfer() function will send the 13249 * datamove done message, or call the callback with an 13250 * error if there is a problem. 13251 */ 13252 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13253 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13254 } 13255 13256 return; 13257} 13258 13259/* 13260 * Process a datamove request from the other controller. This is used for 13261 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13262 * first. Once that is complete, the data gets DMAed into the remote 13263 * controller's memory. For reads, we DMA from the remote controller's 13264 * memory into our memory first, and then move it out to the FETD. 13265 */ 13266static void 13267ctl_datamove_remote(union ctl_io *io) 13268{ 13269 struct ctl_softc *softc; 13270 13271 softc = control_softc; 13272 13273 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13274 13275 /* 13276 * Note that we look for an aborted I/O here, but don't do some of 13277 * the other checks that ctl_datamove() normally does. 13278 * We don't need to run the datamove delay code, since that should 13279 * have been done if need be on the other controller. 13280 */ 13281 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13282 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13283 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13284 io->io_hdr.nexus.targ_port, 13285 io->io_hdr.nexus.targ_target.id, 13286 io->io_hdr.nexus.targ_lun); 13287 io->io_hdr.port_status = 31338; 13288 ctl_send_datamove_done(io, /*have_lock*/ 0); 13289 return; 13290 } 13291 13292 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13293 ctl_datamove_remote_write(io); 13294 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13295 ctl_datamove_remote_read(io); 13296 } else { 13297 union ctl_ha_msg msg; 13298 struct scsi_sense_data *sense; 13299 uint8_t sks[3]; 13300 int retry_count; 13301 13302 memset(&msg, 0, sizeof(msg)); 13303 13304 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13305 msg.hdr.status = CTL_SCSI_ERROR; 13306 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13307 13308 retry_count = 4243; 13309 13310 sense = &msg.scsi.sense_data; 13311 sks[0] = SSD_SCS_VALID; 13312 sks[1] = (retry_count >> 8) & 0xff; 13313 sks[2] = retry_count & 0xff; 13314 13315 /* "Internal target failure" */ 13316 scsi_set_sense_data(sense, 13317 /*sense_format*/ SSD_TYPE_NONE, 13318 /*current_error*/ 1, 13319 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13320 /*asc*/ 0x44, 13321 /*ascq*/ 0x00, 13322 /*type*/ SSD_ELEM_SKS, 13323 /*size*/ sizeof(sks), 13324 /*data*/ sks, 13325 SSD_ELEM_NONE); 13326 13327 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13328 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13329 ctl_failover_io(io, /*have_lock*/ 1); 13330 return; 13331 } 13332 13333 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13334 CTL_HA_STATUS_SUCCESS) { 13335 /* XXX KDM what to do if this fails? */ 13336 } 13337 return; 13338 } 13339 13340} 13341 13342static int 13343ctl_process_done(union ctl_io *io) 13344{ 13345 struct ctl_lun *lun; 13346 struct ctl_softc *ctl_softc = control_softc; 13347 void (*fe_done)(union ctl_io *io); 13348 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13349 13350 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13351 13352 fe_done = 13353 control_softc->ctl_ports[targ_port]->fe_done; 13354 13355#ifdef CTL_TIME_IO 13356 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13357 char str[256]; 13358 char path_str[64]; 13359 struct sbuf sb; 13360 13361 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13362 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13363 13364 sbuf_cat(&sb, path_str); 13365 switch (io->io_hdr.io_type) { 13366 case CTL_IO_SCSI: 13367 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13368 sbuf_printf(&sb, "\n"); 13369 sbuf_cat(&sb, path_str); 13370 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13371 io->scsiio.tag_num, io->scsiio.tag_type); 13372 break; 13373 case CTL_IO_TASK: 13374 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13375 "Tag Type: %d\n", io->taskio.task_action, 13376 io->taskio.tag_num, io->taskio.tag_type); 13377 break; 13378 default: 13379 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13380 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13381 break; 13382 } 13383 sbuf_cat(&sb, path_str); 13384 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13385 (intmax_t)time_uptime - io->io_hdr.start_time); 13386 sbuf_finish(&sb); 13387 printf("%s", sbuf_data(&sb)); 13388 } 13389#endif /* CTL_TIME_IO */ 13390 13391 switch (io->io_hdr.io_type) { 13392 case CTL_IO_SCSI: 13393 break; 13394 case CTL_IO_TASK: 13395 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13396 ctl_io_error_print(io, NULL); 13397 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13398 ctl_free_io(io); 13399 else 13400 fe_done(io); 13401 return (CTL_RETVAL_COMPLETE); 13402 default: 13403 panic("ctl_process_done: invalid io type %d\n", 13404 io->io_hdr.io_type); 13405 break; /* NOTREACHED */ 13406 } 13407 13408 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13409 if (lun == NULL) { 13410 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13411 io->io_hdr.nexus.targ_mapped_lun)); 13412 goto bailout; 13413 } 13414 13415 mtx_lock(&lun->lun_lock); 13416 13417 /* 13418 * Check to see if we have any errors to inject here. We only 13419 * inject errors for commands that don't already have errors set. 13420 */ 13421 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13422 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13423 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13424 ctl_inject_error(lun, io); 13425 13426 /* 13427 * XXX KDM how do we treat commands that aren't completed 13428 * successfully? 13429 * 13430 * XXX KDM should we also track I/O latency? 13431 */ 13432 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13433 io->io_hdr.io_type == CTL_IO_SCSI) { 13434#ifdef CTL_TIME_IO 13435 struct bintime cur_bt; 13436#endif 13437 int type; 13438 13439 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13440 CTL_FLAG_DATA_IN) 13441 type = CTL_STATS_READ; 13442 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13443 CTL_FLAG_DATA_OUT) 13444 type = CTL_STATS_WRITE; 13445 else 13446 type = CTL_STATS_NO_IO; 13447 13448 lun->stats.ports[targ_port].bytes[type] += 13449 io->scsiio.kern_total_len; 13450 lun->stats.ports[targ_port].operations[type]++; 13451#ifdef CTL_TIME_IO 13452 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13453 &io->io_hdr.dma_bt); 13454 lun->stats.ports[targ_port].num_dmas[type] += 13455 io->io_hdr.num_dmas; 13456 getbintime(&cur_bt); 13457 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13458 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13459#endif 13460 } 13461 13462 /* 13463 * Remove this from the OOA queue. 13464 */ 13465 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13466 13467 /* 13468 * Run through the blocked queue on this LUN and see if anything 13469 * has become unblocked, now that this transaction is done. 13470 */ 13471 ctl_check_blocked(lun); 13472 13473 /* 13474 * If the LUN has been invalidated, free it if there is nothing 13475 * left on its OOA queue. 13476 */ 13477 if ((lun->flags & CTL_LUN_INVALID) 13478 && TAILQ_EMPTY(&lun->ooa_queue)) { 13479 mtx_unlock(&lun->lun_lock); 13480 mtx_lock(&ctl_softc->ctl_lock); 13481 ctl_free_lun(lun); 13482 mtx_unlock(&ctl_softc->ctl_lock); 13483 } else 13484 mtx_unlock(&lun->lun_lock); 13485 13486bailout: 13487 13488 /* 13489 * If this command has been aborted, make sure we set the status 13490 * properly. The FETD is responsible for freeing the I/O and doing 13491 * whatever it needs to do to clean up its state. 13492 */ 13493 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13494 ctl_set_task_aborted(&io->scsiio); 13495 13496 /* 13497 * If enabled, print command error status. 13498 * We don't print UAs unless debugging was enabled explicitly. 13499 */ 13500 do { 13501 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13502 break; 13503 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13504 break; 13505 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13506 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13507 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13508 int error_code, sense_key, asc, ascq; 13509 13510 scsi_extract_sense_len(&io->scsiio.sense_data, 13511 io->scsiio.sense_len, &error_code, &sense_key, 13512 &asc, &ascq, /*show_errors*/ 0); 13513 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13514 break; 13515 } 13516 13517 ctl_io_error_print(io, NULL); 13518 } while (0); 13519 13520 /* 13521 * Tell the FETD or the other shelf controller we're done with this 13522 * command. Note that only SCSI commands get to this point. Task 13523 * management commands are completed above. 13524 * 13525 * We only send status to the other controller if we're in XFER 13526 * mode. In SER_ONLY mode, the I/O is done on the controller that 13527 * received the I/O (from CTL's perspective), and so the status is 13528 * generated there. 13529 * 13530 * XXX KDM if we hold the lock here, we could cause a deadlock 13531 * if the frontend comes back in in this context to queue 13532 * something. 13533 */ 13534 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13535 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13536 union ctl_ha_msg msg; 13537 13538 memset(&msg, 0, sizeof(msg)); 13539 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13540 msg.hdr.original_sc = io->io_hdr.original_sc; 13541 msg.hdr.nexus = io->io_hdr.nexus; 13542 msg.hdr.status = io->io_hdr.status; 13543 msg.scsi.scsi_status = io->scsiio.scsi_status; 13544 msg.scsi.tag_num = io->scsiio.tag_num; 13545 msg.scsi.tag_type = io->scsiio.tag_type; 13546 msg.scsi.sense_len = io->scsiio.sense_len; 13547 msg.scsi.sense_residual = io->scsiio.sense_residual; 13548 msg.scsi.residual = io->scsiio.residual; 13549 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13550 sizeof(io->scsiio.sense_data)); 13551 /* 13552 * We copy this whether or not this is an I/O-related 13553 * command. Otherwise, we'd have to go and check to see 13554 * whether it's a read/write command, and it really isn't 13555 * worth it. 13556 */ 13557 memcpy(&msg.scsi.lbalen, 13558 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13559 sizeof(msg.scsi.lbalen)); 13560 13561 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13562 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13563 /* XXX do something here */ 13564 } 13565 13566 ctl_free_io(io); 13567 } else 13568 fe_done(io); 13569 13570 return (CTL_RETVAL_COMPLETE); 13571} 13572 13573#ifdef CTL_WITH_CA 13574/* 13575 * Front end should call this if it doesn't do autosense. When the request 13576 * sense comes back in from the initiator, we'll dequeue this and send it. 13577 */ 13578int 13579ctl_queue_sense(union ctl_io *io) 13580{ 13581 struct ctl_lun *lun; 13582 struct ctl_softc *ctl_softc; 13583 uint32_t initidx, targ_lun; 13584 13585 ctl_softc = control_softc; 13586 13587 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13588 13589 /* 13590 * LUN lookup will likely move to the ctl_work_thread() once we 13591 * have our new queueing infrastructure (that doesn't put things on 13592 * a per-LUN queue initially). That is so that we can handle 13593 * things like an INQUIRY to a LUN that we don't have enabled. We 13594 * can't deal with that right now. 13595 */ 13596 mtx_lock(&ctl_softc->ctl_lock); 13597 13598 /* 13599 * If we don't have a LUN for this, just toss the sense 13600 * information. 13601 */ 13602 targ_lun = io->io_hdr.nexus.targ_lun; 13603 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13604 if ((targ_lun < CTL_MAX_LUNS) 13605 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13606 lun = ctl_softc->ctl_luns[targ_lun]; 13607 else 13608 goto bailout; 13609 13610 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13611 13612 mtx_lock(&lun->lun_lock); 13613 /* 13614 * Already have CA set for this LUN...toss the sense information. 13615 */ 13616 if (ctl_is_set(lun->have_ca, initidx)) { 13617 mtx_unlock(&lun->lun_lock); 13618 goto bailout; 13619 } 13620 13621 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13622 ctl_min(sizeof(lun->pending_sense[initidx]), 13623 sizeof(io->scsiio.sense_data))); 13624 ctl_set_mask(lun->have_ca, initidx); 13625 mtx_unlock(&lun->lun_lock); 13626 13627bailout: 13628 mtx_unlock(&ctl_softc->ctl_lock); 13629 13630 ctl_free_io(io); 13631 13632 return (CTL_RETVAL_COMPLETE); 13633} 13634#endif 13635 13636/* 13637 * Primary command inlet from frontend ports. All SCSI and task I/O 13638 * requests must go through this function. 13639 */ 13640int 13641ctl_queue(union ctl_io *io) 13642{ 13643 struct ctl_softc *ctl_softc; 13644 13645 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13646 13647 ctl_softc = control_softc; 13648 13649#ifdef CTL_TIME_IO 13650 io->io_hdr.start_time = time_uptime; 13651 getbintime(&io->io_hdr.start_bt); 13652#endif /* CTL_TIME_IO */ 13653 13654 /* Map FE-specific LUN ID into global one. */ 13655 io->io_hdr.nexus.targ_mapped_lun = 13656 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13657 13658 switch (io->io_hdr.io_type) { 13659 case CTL_IO_SCSI: 13660 case CTL_IO_TASK: 13661 if (ctl_debug & CTL_DEBUG_CDB) 13662 ctl_io_print(io); 13663 ctl_enqueue_incoming(io); 13664 break; 13665 default: 13666 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13667 return (EINVAL); 13668 } 13669 13670 return (CTL_RETVAL_COMPLETE); 13671} 13672 13673#ifdef CTL_IO_DELAY 13674static void 13675ctl_done_timer_wakeup(void *arg) 13676{ 13677 union ctl_io *io; 13678 13679 io = (union ctl_io *)arg; 13680 ctl_done(io); 13681} 13682#endif /* CTL_IO_DELAY */ 13683 13684void 13685ctl_done(union ctl_io *io) 13686{ 13687 struct ctl_softc *ctl_softc; 13688 13689 ctl_softc = control_softc; 13690 13691 /* 13692 * Enable this to catch duplicate completion issues. 13693 */ 13694#if 0 13695 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13696 printf("%s: type %d msg %d cdb %x iptl: " 13697 "%d:%d:%d:%d tag 0x%04x " 13698 "flag %#x status %x\n", 13699 __func__, 13700 io->io_hdr.io_type, 13701 io->io_hdr.msg_type, 13702 io->scsiio.cdb[0], 13703 io->io_hdr.nexus.initid.id, 13704 io->io_hdr.nexus.targ_port, 13705 io->io_hdr.nexus.targ_target.id, 13706 io->io_hdr.nexus.targ_lun, 13707 (io->io_hdr.io_type == 13708 CTL_IO_TASK) ? 13709 io->taskio.tag_num : 13710 io->scsiio.tag_num, 13711 io->io_hdr.flags, 13712 io->io_hdr.status); 13713 } else 13714 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13715#endif 13716 13717 /* 13718 * This is an internal copy of an I/O, and should not go through 13719 * the normal done processing logic. 13720 */ 13721 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13722 return; 13723 13724 /* 13725 * We need to send a msg to the serializing shelf to finish the IO 13726 * as well. We don't send a finish message to the other shelf if 13727 * this is a task management command. Task management commands 13728 * aren't serialized in the OOA queue, but rather just executed on 13729 * both shelf controllers for commands that originated on that 13730 * controller. 13731 */ 13732 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13733 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13734 union ctl_ha_msg msg_io; 13735 13736 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13737 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13738 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13739 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13740 } 13741 /* continue on to finish IO */ 13742 } 13743#ifdef CTL_IO_DELAY 13744 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13745 struct ctl_lun *lun; 13746 13747 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13748 13749 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13750 } else { 13751 struct ctl_lun *lun; 13752 13753 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13754 13755 if ((lun != NULL) 13756 && (lun->delay_info.done_delay > 0)) { 13757 struct callout *callout; 13758 13759 callout = (struct callout *)&io->io_hdr.timer_bytes; 13760 callout_init(callout, /*mpsafe*/ 1); 13761 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13762 callout_reset(callout, 13763 lun->delay_info.done_delay * hz, 13764 ctl_done_timer_wakeup, io); 13765 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13766 lun->delay_info.done_delay = 0; 13767 return; 13768 } 13769 } 13770#endif /* CTL_IO_DELAY */ 13771 13772 ctl_enqueue_done(io); 13773} 13774 13775int 13776ctl_isc(struct ctl_scsiio *ctsio) 13777{ 13778 struct ctl_lun *lun; 13779 int retval; 13780 13781 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13782 13783 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13784 13785 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13786 13787 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13788 13789 return (retval); 13790} 13791 13792 13793static void 13794ctl_work_thread(void *arg) 13795{ 13796 struct ctl_thread *thr = (struct ctl_thread *)arg; 13797 struct ctl_softc *softc = thr->ctl_softc; 13798 union ctl_io *io; 13799 int retval; 13800 13801 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13802 13803 for (;;) { 13804 retval = 0; 13805 13806 /* 13807 * We handle the queues in this order: 13808 * - ISC 13809 * - done queue (to free up resources, unblock other commands) 13810 * - RtR queue 13811 * - incoming queue 13812 * 13813 * If those queues are empty, we break out of the loop and 13814 * go to sleep. 13815 */ 13816 mtx_lock(&thr->queue_lock); 13817 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13818 if (io != NULL) { 13819 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13820 mtx_unlock(&thr->queue_lock); 13821 ctl_handle_isc(io); 13822 continue; 13823 } 13824 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13825 if (io != NULL) { 13826 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13827 /* clear any blocked commands, call fe_done */ 13828 mtx_unlock(&thr->queue_lock); 13829 retval = ctl_process_done(io); 13830 continue; 13831 } 13832 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13833 if (io != NULL) { 13834 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13835 mtx_unlock(&thr->queue_lock); 13836 if (io->io_hdr.io_type == CTL_IO_TASK) 13837 ctl_run_task(io); 13838 else 13839 ctl_scsiio_precheck(softc, &io->scsiio); 13840 continue; 13841 } 13842 if (!ctl_pause_rtr) { 13843 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13844 if (io != NULL) { 13845 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13846 mtx_unlock(&thr->queue_lock); 13847 retval = ctl_scsiio(&io->scsiio); 13848 if (retval != CTL_RETVAL_COMPLETE) 13849 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13850 continue; 13851 } 13852 } 13853 13854 /* Sleep until we have something to do. */ 13855 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13856 } 13857} 13858 13859static void 13860ctl_lun_thread(void *arg) 13861{ 13862 struct ctl_softc *softc = (struct ctl_softc *)arg; 13863 struct ctl_be_lun *be_lun; 13864 int retval; 13865 13866 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13867 13868 for (;;) { 13869 retval = 0; 13870 mtx_lock(&softc->ctl_lock); 13871 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13872 if (be_lun != NULL) { 13873 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13874 mtx_unlock(&softc->ctl_lock); 13875 ctl_create_lun(be_lun); 13876 continue; 13877 } 13878 13879 /* Sleep until we have something to do. */ 13880 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13881 PDROP | PRIBIO, "-", 0); 13882 } 13883} 13884 13885static void 13886ctl_thresh_thread(void *arg) 13887{ 13888 struct ctl_softc *softc = (struct ctl_softc *)arg; 13889 struct ctl_lun *lun; 13890 struct ctl_be_lun *be_lun; 13891 struct scsi_da_rw_recovery_page *rwpage; 13892 struct ctl_logical_block_provisioning_page *page; 13893 const char *attr; 13894 uint64_t thres, val; 13895 int i, e; 13896 13897 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13898 13899 for (;;) { 13900 mtx_lock(&softc->ctl_lock); 13901 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13902 be_lun = lun->be_lun; 13903 if ((lun->flags & CTL_LUN_DISABLED) || 13904 (lun->flags & CTL_LUN_OFFLINE) || 13905 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 13906 lun->backend->lun_attr == NULL) 13907 continue; 13908 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13909 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13910 continue; 13911 e = 0; 13912 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13913 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13914 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13915 continue; 13916 thres = scsi_4btoul(page->descr[i].count); 13917 thres <<= CTL_LBP_EXPONENT; 13918 switch (page->descr[i].resource) { 13919 case 0x01: 13920 attr = "blocksavail"; 13921 break; 13922 case 0x02: 13923 attr = "blocksused"; 13924 break; 13925 case 0xf1: 13926 attr = "poolblocksavail"; 13927 break; 13928 case 0xf2: 13929 attr = "poolblocksused"; 13930 break; 13931 default: 13932 continue; 13933 } 13934 mtx_unlock(&softc->ctl_lock); // XXX 13935 val = lun->backend->lun_attr( 13936 lun->be_lun->be_lun, attr); 13937 mtx_lock(&softc->ctl_lock); 13938 if (val == UINT64_MAX) 13939 continue; 13940 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13941 == SLBPPD_ARMING_INC) 13942 e |= (val >= thres); 13943 else 13944 e |= (val <= thres); 13945 } 13946 mtx_lock(&lun->lun_lock); 13947 if (e) { 13948 if (lun->lasttpt == 0 || 13949 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13950 lun->lasttpt = time_uptime; 13951 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13952 } 13953 } else { 13954 lun->lasttpt = 0; 13955 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13956 } 13957 mtx_unlock(&lun->lun_lock); 13958 } 13959 mtx_unlock(&softc->ctl_lock); 13960 pause("-", CTL_LBP_PERIOD * hz); 13961 } 13962} 13963 13964static void 13965ctl_enqueue_incoming(union ctl_io *io) 13966{ 13967 struct ctl_softc *softc = control_softc; 13968 struct ctl_thread *thr; 13969 u_int idx; 13970 13971 idx = (io->io_hdr.nexus.targ_port * 127 + 13972 io->io_hdr.nexus.initid.id) % worker_threads; 13973 thr = &softc->threads[idx]; 13974 mtx_lock(&thr->queue_lock); 13975 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13976 mtx_unlock(&thr->queue_lock); 13977 wakeup(thr); 13978} 13979 13980static void 13981ctl_enqueue_rtr(union ctl_io *io) 13982{ 13983 struct ctl_softc *softc = control_softc; 13984 struct ctl_thread *thr; 13985 13986 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13987 mtx_lock(&thr->queue_lock); 13988 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13989 mtx_unlock(&thr->queue_lock); 13990 wakeup(thr); 13991} 13992 13993static void 13994ctl_enqueue_done(union ctl_io *io) 13995{ 13996 struct ctl_softc *softc = control_softc; 13997 struct ctl_thread *thr; 13998 13999 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14000 mtx_lock(&thr->queue_lock); 14001 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14002 mtx_unlock(&thr->queue_lock); 14003 wakeup(thr); 14004} 14005 14006static void 14007ctl_enqueue_isc(union ctl_io *io) 14008{ 14009 struct ctl_softc *softc = control_softc; 14010 struct ctl_thread *thr; 14011 14012 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14013 mtx_lock(&thr->queue_lock); 14014 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14015 mtx_unlock(&thr->queue_lock); 14016 wakeup(thr); 14017} 14018 14019/* Initialization and failover */ 14020 14021void 14022ctl_init_isc_msg(void) 14023{ 14024 printf("CTL: Still calling this thing\n"); 14025} 14026 14027/* 14028 * Init component 14029 * Initializes component into configuration defined by bootMode 14030 * (see hasc-sv.c) 14031 * returns hasc_Status: 14032 * OK 14033 * ERROR - fatal error 14034 */ 14035static ctl_ha_comp_status 14036ctl_isc_init(struct ctl_ha_component *c) 14037{ 14038 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14039 14040 c->status = ret; 14041 return ret; 14042} 14043 14044/* Start component 14045 * Starts component in state requested. If component starts successfully, 14046 * it must set its own state to the requestrd state 14047 * When requested state is HASC_STATE_HA, the component may refine it 14048 * by adding _SLAVE or _MASTER flags. 14049 * Currently allowed state transitions are: 14050 * UNKNOWN->HA - initial startup 14051 * UNKNOWN->SINGLE - initial startup when no parter detected 14052 * HA->SINGLE - failover 14053 * returns ctl_ha_comp_status: 14054 * OK - component successfully started in requested state 14055 * FAILED - could not start the requested state, failover may 14056 * be possible 14057 * ERROR - fatal error detected, no future startup possible 14058 */ 14059static ctl_ha_comp_status 14060ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14061{ 14062 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14063 14064 printf("%s: go\n", __func__); 14065 14066 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14067 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14068 control_softc->is_single = 0; 14069 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14070 != CTL_HA_STATUS_SUCCESS) { 14071 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14072 ret = CTL_HA_COMP_STATUS_ERROR; 14073 } 14074 } else if (CTL_HA_STATE_IS_HA(c->state) 14075 && CTL_HA_STATE_IS_SINGLE(state)){ 14076 // HA->SINGLE transition 14077 ctl_failover(); 14078 control_softc->is_single = 1; 14079 } else { 14080 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14081 c->state, state); 14082 ret = CTL_HA_COMP_STATUS_ERROR; 14083 } 14084 if (CTL_HA_STATE_IS_SINGLE(state)) 14085 control_softc->is_single = 1; 14086 14087 c->state = state; 14088 c->status = ret; 14089 return ret; 14090} 14091 14092/* 14093 * Quiesce component 14094 * The component must clear any error conditions (set status to OK) and 14095 * prepare itself to another Start call 14096 * returns ctl_ha_comp_status: 14097 * OK 14098 * ERROR 14099 */ 14100static ctl_ha_comp_status 14101ctl_isc_quiesce(struct ctl_ha_component *c) 14102{ 14103 int ret = CTL_HA_COMP_STATUS_OK; 14104 14105 ctl_pause_rtr = 1; 14106 c->status = ret; 14107 return ret; 14108} 14109 14110struct ctl_ha_component ctl_ha_component_ctlisc = 14111{ 14112 .name = "CTL ISC", 14113 .state = CTL_HA_STATE_UNKNOWN, 14114 .init = ctl_isc_init, 14115 .start = ctl_isc_start, 14116 .quiesce = ctl_isc_quiesce 14117}; 14118 14119/* 14120 * vim: ts=8 14121 */ 14122