ctl.c revision 286353
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id$ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 286353 2015-08-05 22:24:49Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/ctype.h> 50#include <sys/kernel.h> 51#include <sys/types.h> 52#include <sys/kthread.h> 53#include <sys/bio.h> 54#include <sys/fcntl.h> 55#include <sys/lock.h> 56#include <sys/module.h> 57#include <sys/mutex.h> 58#include <sys/condvar.h> 59#include <sys/malloc.h> 60#include <sys/conf.h> 61#include <sys/ioccom.h> 62#include <sys/queue.h> 63#include <sys/sbuf.h> 64#include <sys/smp.h> 65#include <sys/endian.h> 66#include <sys/sysctl.h> 67#include <vm/uma.h> 68 69#include <cam/cam.h> 70#include <cam/scsi/scsi_all.h> 71#include <cam/scsi/scsi_da.h> 72#include <cam/ctl/ctl_io.h> 73#include <cam/ctl/ctl.h> 74#include <cam/ctl/ctl_frontend.h> 75#include <cam/ctl/ctl_frontend_internal.h> 76#include <cam/ctl/ctl_util.h> 77#include <cam/ctl/ctl_backend.h> 78#include <cam/ctl/ctl_ioctl.h> 79#include <cam/ctl/ctl_ha.h> 80#include <cam/ctl/ctl_private.h> 81#include <cam/ctl/ctl_debug.h> 82#include <cam/ctl/ctl_scsi_all.h> 83#include <cam/ctl/ctl_error.h> 84 85struct ctl_softc *control_softc = NULL; 86 87/* 88 * Size and alignment macros needed for Copan-specific HA hardware. These 89 * can go away when the HA code is re-written, and uses busdma for any 90 * hardware. 91 */ 92#define CTL_ALIGN_8B(target, source, type) \ 93 if (((uint32_t)source & 0x7) != 0) \ 94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 95 else \ 96 target = (type)source; 97 98#define CTL_SIZE_8B(target, size) \ 99 if ((size & 0x7) != 0) \ 100 target = size + (0x8 - (size & 0x7)); \ 101 else \ 102 target = size; 103 104#define CTL_ALIGN_8B_MARGIN 16 105 106/* 107 * Template mode pages. 108 */ 109 110/* 111 * Note that these are default values only. The actual values will be 112 * filled in when the user does a mode sense. 113 */ 114const static struct copan_debugconf_subpage debugconf_page_default = { 115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 116 DBGCNF_SUBPAGE_CODE, /* subpage */ 117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 119 DBGCNF_VERSION, /* page_version */ 120 {CTL_TIME_IO_DEFAULT_SECS>>8, 121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 122}; 123 124const static struct copan_debugconf_subpage debugconf_page_changeable = { 125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 126 DBGCNF_SUBPAGE_CODE, /* subpage */ 127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 129 0, /* page_version */ 130 {0xff,0xff}, /* ctl_time_io_secs */ 131}; 132 133const static struct scsi_da_rw_recovery_page rw_er_page_default = { 134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 137 /*read_retry_count*/0, 138 /*correction_span*/0, 139 /*head_offset_count*/0, 140 /*data_strobe_offset_cnt*/0, 141 /*byte8*/SMS_RWER_LBPERE, 142 /*write_retry_count*/0, 143 /*reserved2*/0, 144 /*recovery_time_limit*/{0, 0}, 145}; 146 147const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 150 /*byte3*/0, 151 /*read_retry_count*/0, 152 /*correction_span*/0, 153 /*head_offset_count*/0, 154 /*data_strobe_offset_cnt*/0, 155 /*byte8*/0, 156 /*write_retry_count*/0, 157 /*reserved2*/0, 158 /*recovery_time_limit*/{0, 0}, 159}; 160 161const static struct scsi_format_page format_page_default = { 162 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 163 /*page_length*/sizeof(struct scsi_format_page) - 2, 164 /*tracks_per_zone*/ {0, 0}, 165 /*alt_sectors_per_zone*/ {0, 0}, 166 /*alt_tracks_per_zone*/ {0, 0}, 167 /*alt_tracks_per_lun*/ {0, 0}, 168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 170 /*bytes_per_sector*/ {0, 0}, 171 /*interleave*/ {0, 0}, 172 /*track_skew*/ {0, 0}, 173 /*cylinder_skew*/ {0, 0}, 174 /*flags*/ SFP_HSEC, 175 /*reserved*/ {0, 0, 0} 176}; 177 178const static struct scsi_format_page format_page_changeable = { 179 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 180 /*page_length*/sizeof(struct scsi_format_page) - 2, 181 /*tracks_per_zone*/ {0, 0}, 182 /*alt_sectors_per_zone*/ {0, 0}, 183 /*alt_tracks_per_zone*/ {0, 0}, 184 /*alt_tracks_per_lun*/ {0, 0}, 185 /*sectors_per_track*/ {0, 0}, 186 /*bytes_per_sector*/ {0, 0}, 187 /*interleave*/ {0, 0}, 188 /*track_skew*/ {0, 0}, 189 /*cylinder_skew*/ {0, 0}, 190 /*flags*/ 0, 191 /*reserved*/ {0, 0, 0} 192}; 193 194const static struct scsi_rigid_disk_page rigid_disk_page_default = { 195 /*page_code*/SMS_RIGID_DISK_PAGE, 196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 197 /*cylinders*/ {0, 0, 0}, 198 /*heads*/ CTL_DEFAULT_HEADS, 199 /*start_write_precomp*/ {0, 0, 0}, 200 /*start_reduced_current*/ {0, 0, 0}, 201 /*step_rate*/ {0, 0}, 202 /*landing_zone_cylinder*/ {0, 0, 0}, 203 /*rpl*/ SRDP_RPL_DISABLED, 204 /*rotational_offset*/ 0, 205 /*reserved1*/ 0, 206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 207 CTL_DEFAULT_ROTATION_RATE & 0xff}, 208 /*reserved2*/ {0, 0} 209}; 210 211const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 212 /*page_code*/SMS_RIGID_DISK_PAGE, 213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 214 /*cylinders*/ {0, 0, 0}, 215 /*heads*/ 0, 216 /*start_write_precomp*/ {0, 0, 0}, 217 /*start_reduced_current*/ {0, 0, 0}, 218 /*step_rate*/ {0, 0}, 219 /*landing_zone_cylinder*/ {0, 0, 0}, 220 /*rpl*/ 0, 221 /*rotational_offset*/ 0, 222 /*reserved1*/ 0, 223 /*rotation_rate*/ {0, 0}, 224 /*reserved2*/ {0, 0} 225}; 226 227const static struct scsi_caching_page caching_page_default = { 228 /*page_code*/SMS_CACHING_PAGE, 229 /*page_length*/sizeof(struct scsi_caching_page) - 2, 230 /*flags1*/ SCP_DISC | SCP_WCE, 231 /*ret_priority*/ 0, 232 /*disable_pf_transfer_len*/ {0xff, 0xff}, 233 /*min_prefetch*/ {0, 0}, 234 /*max_prefetch*/ {0xff, 0xff}, 235 /*max_pf_ceiling*/ {0xff, 0xff}, 236 /*flags2*/ 0, 237 /*cache_segments*/ 0, 238 /*cache_seg_size*/ {0, 0}, 239 /*reserved*/ 0, 240 /*non_cache_seg_size*/ {0, 0, 0} 241}; 242 243const static struct scsi_caching_page caching_page_changeable = { 244 /*page_code*/SMS_CACHING_PAGE, 245 /*page_length*/sizeof(struct scsi_caching_page) - 2, 246 /*flags1*/ SCP_WCE | SCP_RCD, 247 /*ret_priority*/ 0, 248 /*disable_pf_transfer_len*/ {0, 0}, 249 /*min_prefetch*/ {0, 0}, 250 /*max_prefetch*/ {0, 0}, 251 /*max_pf_ceiling*/ {0, 0}, 252 /*flags2*/ 0, 253 /*cache_segments*/ 0, 254 /*cache_seg_size*/ {0, 0}, 255 /*reserved*/ 0, 256 /*non_cache_seg_size*/ {0, 0, 0} 257}; 258 259const static struct scsi_control_page control_page_default = { 260 /*page_code*/SMS_CONTROL_MODE_PAGE, 261 /*page_length*/sizeof(struct scsi_control_page) - 2, 262 /*rlec*/0, 263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 264 /*eca_and_aen*/0, 265 /*flags4*/SCP_TAS, 266 /*aen_holdoff_period*/{0, 0}, 267 /*busy_timeout_period*/{0, 0}, 268 /*extended_selftest_completion_time*/{0, 0} 269}; 270 271const static struct scsi_control_page control_page_changeable = { 272 /*page_code*/SMS_CONTROL_MODE_PAGE, 273 /*page_length*/sizeof(struct scsi_control_page) - 2, 274 /*rlec*/SCP_DSENSE, 275 /*queue_flags*/SCP_QUEUE_ALG_MASK, 276 /*eca_and_aen*/SCP_SWP, 277 /*flags4*/0, 278 /*aen_holdoff_period*/{0, 0}, 279 /*busy_timeout_period*/{0, 0}, 280 /*extended_selftest_completion_time*/{0, 0} 281}; 282 283const static struct scsi_info_exceptions_page ie_page_default = { 284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 286 /*info_flags*/SIEP_FLAGS_DEXCPT, 287 /*mrie*/0, 288 /*interval_timer*/{0, 0, 0, 0}, 289 /*report_count*/{0, 0, 0, 0} 290}; 291 292const static struct scsi_info_exceptions_page ie_page_changeable = { 293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 295 /*info_flags*/0, 296 /*mrie*/0, 297 /*interval_timer*/{0, 0, 0, 0}, 298 /*report_count*/{0, 0, 0, 0} 299}; 300 301#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 302 303const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 305 /*subpage_code*/0x02, 306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 307 /*flags*/0, 308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 309 /*descr*/{}}, 310 {{/*flags*/0, 311 /*resource*/0x01, 312 /*reserved*/{0, 0}, 313 /*count*/{0, 0, 0, 0}}, 314 {/*flags*/0, 315 /*resource*/0x02, 316 /*reserved*/{0, 0}, 317 /*count*/{0, 0, 0, 0}}, 318 {/*flags*/0, 319 /*resource*/0xf1, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0xf2, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}} 326 } 327}; 328 329const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 331 /*subpage_code*/0x02, 332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 333 /*flags*/0, 334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 335 /*descr*/{}}, 336 {{/*flags*/0, 337 /*resource*/0, 338 /*reserved*/{0, 0}, 339 /*count*/{0, 0, 0, 0}}, 340 {/*flags*/0, 341 /*resource*/0, 342 /*reserved*/{0, 0}, 343 /*count*/{0, 0, 0, 0}}, 344 {/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}} 352 } 353}; 354 355/* 356 * XXX KDM move these into the softc. 357 */ 358static int rcv_sync_msg; 359static uint8_t ctl_pause_rtr; 360 361SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 362static int worker_threads = -1; 363SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 364 &worker_threads, 1, "Number of worker threads"); 365static int ctl_debug = CTL_DEBUG_NONE; 366SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 367 &ctl_debug, 0, "Enabled debug flags"); 368 369/* 370 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 371 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 372 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 373 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 374 */ 375#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 376 377static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 378 int param); 379static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 380static int ctl_init(void); 381void ctl_shutdown(void); 382static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 383static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 384static void ctl_ioctl_online(void *arg); 385static void ctl_ioctl_offline(void *arg); 386static int ctl_ioctl_lun_enable(void *arg, int lun_id); 387static int ctl_ioctl_lun_disable(void *arg, int lun_id); 388static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 389static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 390static int ctl_ioctl_submit_wait(union ctl_io *io); 391static void ctl_ioctl_datamove(union ctl_io *io); 392static void ctl_ioctl_done(union ctl_io *io); 393static void ctl_ioctl_hard_startstop_callback(void *arg, 394 struct cfi_metatask *metatask); 395static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 396static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 397 struct ctl_ooa *ooa_hdr, 398 struct ctl_ooa_entry *kern_entries); 399static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 400 struct thread *td); 401static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 402 struct ctl_be_lun *be_lun); 403static int ctl_free_lun(struct ctl_lun *lun); 404static void ctl_create_lun(struct ctl_be_lun *be_lun); 405static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 406/** 407static void ctl_failover_change_pages(struct ctl_softc *softc, 408 struct ctl_scsiio *ctsio, int master); 409**/ 410 411static int ctl_do_mode_select(union ctl_io *io); 412static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 413 uint64_t res_key, uint64_t sa_res_key, 414 uint8_t type, uint32_t residx, 415 struct ctl_scsiio *ctsio, 416 struct scsi_per_res_out *cdb, 417 struct scsi_per_res_out_parms* param); 418static void ctl_pro_preempt_other(struct ctl_lun *lun, 419 union ctl_ha_msg *msg); 420static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 421static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 422static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 423static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 424static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 425static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 426static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 427 int alloc_len); 428static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 429 int alloc_len); 430static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 431static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 432static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 433static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 434static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 435static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 436 bool seq); 437static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 438static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 439 union ctl_io *pending_io, union ctl_io *ooa_io); 440static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 441 union ctl_io *starting_io); 442static int ctl_check_blocked(struct ctl_lun *lun); 443static int ctl_scsiio_lun_check(struct ctl_lun *lun, 444 const struct ctl_cmd_entry *entry, 445 struct ctl_scsiio *ctsio); 446//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 447static void ctl_failover(void); 448static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 449 ctl_ua_type ua_type); 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 *softc; 613 union ctl_io *io; 614 struct ctl_prio *presio; 615 ctl_ha_status isc_status; 616 617 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(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 (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 (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 (softc->ha_mode == CTL_HA_MODE_XFER) 856 ctl_isc_handler_finish_xfer(softc, 857 &msg_info); 858 else 859 ctl_isc_handler_finish_ser_only(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 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 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 STAILQ_INIT(&softc->lun_list); 1132 STAILQ_INIT(&softc->pending_lun_queue); 1133 STAILQ_INIT(&softc->fe_list); 1134 STAILQ_INIT(&softc->port_list); 1135 STAILQ_INIT(&softc->be_list); 1136 ctl_tpc_init(softc); 1137 1138 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1139 &other_pool) != 0) 1140 { 1141 printf("ctl: can't allocate %d entry other SC pool, " 1142 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1143 return (ENOMEM); 1144 } 1145 softc->othersc_pool = other_pool; 1146 1147 if (worker_threads <= 0) 1148 worker_threads = max(1, mp_ncpus / 4); 1149 if (worker_threads > CTL_MAX_THREADS) 1150 worker_threads = CTL_MAX_THREADS; 1151 1152 for (i = 0; i < worker_threads; i++) { 1153 struct ctl_thread *thr = &softc->threads[i]; 1154 1155 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1156 thr->ctl_softc = softc; 1157 STAILQ_INIT(&thr->incoming_queue); 1158 STAILQ_INIT(&thr->rtr_queue); 1159 STAILQ_INIT(&thr->done_queue); 1160 STAILQ_INIT(&thr->isc_queue); 1161 1162 error = kproc_kthread_add(ctl_work_thread, thr, 1163 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1164 if (error != 0) { 1165 printf("error creating CTL work thread!\n"); 1166 ctl_pool_free(other_pool); 1167 return (error); 1168 } 1169 } 1170 error = kproc_kthread_add(ctl_lun_thread, softc, 1171 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1172 if (error != 0) { 1173 printf("error creating CTL lun thread!\n"); 1174 ctl_pool_free(other_pool); 1175 return (error); 1176 } 1177 error = kproc_kthread_add(ctl_thresh_thread, softc, 1178 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1179 if (error != 0) { 1180 printf("error creating CTL threshold thread!\n"); 1181 ctl_pool_free(other_pool); 1182 return (error); 1183 } 1184 if (bootverbose) 1185 printf("ctl: CAM Target Layer loaded\n"); 1186 1187 /* 1188 * Initialize the ioctl front end. 1189 */ 1190 ctl_frontend_register(&ioctl_frontend); 1191 port = &softc->ioctl_info.port; 1192 port->frontend = &ioctl_frontend; 1193 sprintf(softc->ioctl_info.port_name, "ioctl"); 1194 port->port_type = CTL_PORT_IOCTL; 1195 port->num_requested_ctl_io = 100; 1196 port->port_name = softc->ioctl_info.port_name; 1197 port->port_online = ctl_ioctl_online; 1198 port->port_offline = ctl_ioctl_offline; 1199 port->onoff_arg = &softc->ioctl_info; 1200 port->lun_enable = ctl_ioctl_lun_enable; 1201 port->lun_disable = ctl_ioctl_lun_disable; 1202 port->targ_lun_arg = &softc->ioctl_info; 1203 port->fe_datamove = ctl_ioctl_datamove; 1204 port->fe_done = ctl_ioctl_done; 1205 port->max_targets = 15; 1206 port->max_target_id = 15; 1207 1208 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1209 printf("ctl: ioctl front end registration failed, will " 1210 "continue anyway\n"); 1211 } 1212 1213 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1214 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1215 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1216 1217#ifdef CTL_IO_DELAY 1218 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1219 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1220 sizeof(struct callout), CTL_TIMER_BYTES); 1221 return (EINVAL); 1222 } 1223#endif /* CTL_IO_DELAY */ 1224 1225 return (0); 1226} 1227 1228void 1229ctl_shutdown(void) 1230{ 1231 struct ctl_softc *softc; 1232 struct ctl_lun *lun, *next_lun; 1233 1234 softc = (struct ctl_softc *)control_softc; 1235 1236 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1237 printf("ctl: ioctl front end deregistration failed\n"); 1238 1239 mtx_lock(&softc->ctl_lock); 1240 1241 /* 1242 * Free up each LUN. 1243 */ 1244 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1245 next_lun = STAILQ_NEXT(lun, links); 1246 ctl_free_lun(lun); 1247 } 1248 1249 mtx_unlock(&softc->ctl_lock); 1250 1251 ctl_frontend_deregister(&ioctl_frontend); 1252 1253#if 0 1254 ctl_shutdown_thread(softc->work_thread); 1255 mtx_destroy(&softc->queue_lock); 1256#endif 1257 1258 ctl_tpc_shutdown(softc); 1259 uma_zdestroy(softc->io_zone); 1260 mtx_destroy(&softc->ctl_lock); 1261 1262 destroy_dev(softc->dev); 1263 1264 sysctl_ctx_free(&softc->sysctl_ctx); 1265 1266 free(control_softc, M_DEVBUF); 1267 control_softc = NULL; 1268 1269 if (bootverbose) 1270 printf("ctl: CAM Target Layer unloaded\n"); 1271} 1272 1273static int 1274ctl_module_event_handler(module_t mod, int what, void *arg) 1275{ 1276 1277 switch (what) { 1278 case MOD_LOAD: 1279 return (ctl_init()); 1280 case MOD_UNLOAD: 1281 return (EBUSY); 1282 default: 1283 return (EOPNOTSUPP); 1284 } 1285} 1286 1287/* 1288 * XXX KDM should we do some access checks here? Bump a reference count to 1289 * prevent a CTL module from being unloaded while someone has it open? 1290 */ 1291static int 1292ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1293{ 1294 return (0); 1295} 1296 1297static int 1298ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1299{ 1300 return (0); 1301} 1302 1303int 1304ctl_port_enable(ctl_port_type port_type) 1305{ 1306 struct ctl_softc *softc = control_softc; 1307 struct ctl_port *port; 1308 1309 if (softc->is_single == 0) { 1310 union ctl_ha_msg msg_info; 1311 int isc_retval; 1312 1313#if 0 1314 printf("%s: HA mode, synchronizing frontend enable\n", 1315 __func__); 1316#endif 1317 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1318 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1319 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1320 printf("Sync msg send error retval %d\n", isc_retval); 1321 } 1322 if (!rcv_sync_msg) { 1323 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1324 sizeof(msg_info), 1); 1325 } 1326#if 0 1327 printf("CTL:Frontend Enable\n"); 1328 } else { 1329 printf("%s: single mode, skipping frontend synchronization\n", 1330 __func__); 1331#endif 1332 } 1333 1334 STAILQ_FOREACH(port, &softc->port_list, links) { 1335 if (port_type & port->port_type) 1336 { 1337#if 0 1338 printf("port %d\n", port->targ_port); 1339#endif 1340 ctl_port_online(port); 1341 } 1342 } 1343 1344 return (0); 1345} 1346 1347int 1348ctl_port_disable(ctl_port_type port_type) 1349{ 1350 struct ctl_softc *softc; 1351 struct ctl_port *port; 1352 1353 softc = control_softc; 1354 1355 STAILQ_FOREACH(port, &softc->port_list, links) { 1356 if (port_type & port->port_type) 1357 ctl_port_offline(port); 1358 } 1359 1360 return (0); 1361} 1362 1363/* 1364 * Returns 0 for success, 1 for failure. 1365 * Currently the only failure mode is if there aren't enough entries 1366 * allocated. So, in case of a failure, look at num_entries_dropped, 1367 * reallocate and try again. 1368 */ 1369int 1370ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1371 int *num_entries_filled, int *num_entries_dropped, 1372 ctl_port_type port_type, int no_virtual) 1373{ 1374 struct ctl_softc *softc; 1375 struct ctl_port *port; 1376 int entries_dropped, entries_filled; 1377 int retval; 1378 int i; 1379 1380 softc = control_softc; 1381 1382 retval = 0; 1383 entries_filled = 0; 1384 entries_dropped = 0; 1385 1386 i = 0; 1387 mtx_lock(&softc->ctl_lock); 1388 STAILQ_FOREACH(port, &softc->port_list, links) { 1389 struct ctl_port_entry *entry; 1390 1391 if ((port->port_type & port_type) == 0) 1392 continue; 1393 1394 if ((no_virtual != 0) 1395 && (port->virtual_port != 0)) 1396 continue; 1397 1398 if (entries_filled >= num_entries_alloced) { 1399 entries_dropped++; 1400 continue; 1401 } 1402 entry = &entries[i]; 1403 1404 entry->port_type = port->port_type; 1405 strlcpy(entry->port_name, port->port_name, 1406 sizeof(entry->port_name)); 1407 entry->physical_port = port->physical_port; 1408 entry->virtual_port = port->virtual_port; 1409 entry->wwnn = port->wwnn; 1410 entry->wwpn = port->wwpn; 1411 1412 i++; 1413 entries_filled++; 1414 } 1415 1416 mtx_unlock(&softc->ctl_lock); 1417 1418 if (entries_dropped > 0) 1419 retval = 1; 1420 1421 *num_entries_dropped = entries_dropped; 1422 *num_entries_filled = entries_filled; 1423 1424 return (retval); 1425} 1426 1427static void 1428ctl_ioctl_online(void *arg) 1429{ 1430 struct ctl_ioctl_info *ioctl_info; 1431 1432 ioctl_info = (struct ctl_ioctl_info *)arg; 1433 1434 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1435} 1436 1437static void 1438ctl_ioctl_offline(void *arg) 1439{ 1440 struct ctl_ioctl_info *ioctl_info; 1441 1442 ioctl_info = (struct ctl_ioctl_info *)arg; 1443 1444 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1445} 1446 1447/* 1448 * Remove an initiator by port number and initiator ID. 1449 * Returns 0 for success, -1 for failure. 1450 */ 1451int 1452ctl_remove_initiator(struct ctl_port *port, int iid) 1453{ 1454 struct ctl_softc *softc = control_softc; 1455 1456 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1457 1458 if (iid > CTL_MAX_INIT_PER_PORT) { 1459 printf("%s: initiator ID %u > maximun %u!\n", 1460 __func__, iid, CTL_MAX_INIT_PER_PORT); 1461 return (-1); 1462 } 1463 1464 mtx_lock(&softc->ctl_lock); 1465 port->wwpn_iid[iid].in_use--; 1466 port->wwpn_iid[iid].last_use = time_uptime; 1467 mtx_unlock(&softc->ctl_lock); 1468 1469 return (0); 1470} 1471 1472/* 1473 * Add an initiator to the initiator map. 1474 * Returns iid for success, < 0 for failure. 1475 */ 1476int 1477ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1478{ 1479 struct ctl_softc *softc = control_softc; 1480 time_t best_time; 1481 int i, best; 1482 1483 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1484 1485 if (iid >= CTL_MAX_INIT_PER_PORT) { 1486 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1487 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1488 free(name, M_CTL); 1489 return (-1); 1490 } 1491 1492 mtx_lock(&softc->ctl_lock); 1493 1494 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1495 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1496 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1497 iid = i; 1498 break; 1499 } 1500 if (name != NULL && port->wwpn_iid[i].name != NULL && 1501 strcmp(name, port->wwpn_iid[i].name) == 0) { 1502 iid = i; 1503 break; 1504 } 1505 } 1506 } 1507 1508 if (iid < 0) { 1509 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1510 if (port->wwpn_iid[i].in_use == 0 && 1511 port->wwpn_iid[i].wwpn == 0 && 1512 port->wwpn_iid[i].name == NULL) { 1513 iid = i; 1514 break; 1515 } 1516 } 1517 } 1518 1519 if (iid < 0) { 1520 best = -1; 1521 best_time = INT32_MAX; 1522 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1523 if (port->wwpn_iid[i].in_use == 0) { 1524 if (port->wwpn_iid[i].last_use < best_time) { 1525 best = i; 1526 best_time = port->wwpn_iid[i].last_use; 1527 } 1528 } 1529 } 1530 iid = best; 1531 } 1532 1533 if (iid < 0) { 1534 mtx_unlock(&softc->ctl_lock); 1535 free(name, M_CTL); 1536 return (-2); 1537 } 1538 1539 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1540 /* 1541 * This is not an error yet. 1542 */ 1543 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1544#if 0 1545 printf("%s: port %d iid %u WWPN %#jx arrived" 1546 " again\n", __func__, port->targ_port, 1547 iid, (uintmax_t)wwpn); 1548#endif 1549 goto take; 1550 } 1551 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1552 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1553#if 0 1554 printf("%s: port %d iid %u name '%s' arrived" 1555 " again\n", __func__, port->targ_port, 1556 iid, name); 1557#endif 1558 goto take; 1559 } 1560 1561 /* 1562 * This is an error, but what do we do about it? The 1563 * driver is telling us we have a new WWPN for this 1564 * initiator ID, so we pretty much need to use it. 1565 */ 1566 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1567 " but WWPN %#jx '%s' is still at that address\n", 1568 __func__, port->targ_port, iid, wwpn, name, 1569 (uintmax_t)port->wwpn_iid[iid].wwpn, 1570 port->wwpn_iid[iid].name); 1571 1572 /* 1573 * XXX KDM clear have_ca and ua_pending on each LUN for 1574 * this initiator. 1575 */ 1576 } 1577take: 1578 free(port->wwpn_iid[iid].name, M_CTL); 1579 port->wwpn_iid[iid].name = name; 1580 port->wwpn_iid[iid].wwpn = wwpn; 1581 port->wwpn_iid[iid].in_use++; 1582 mtx_unlock(&softc->ctl_lock); 1583 1584 return (iid); 1585} 1586 1587static int 1588ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1589{ 1590 int len; 1591 1592 switch (port->port_type) { 1593 case CTL_PORT_FC: 1594 { 1595 struct scsi_transportid_fcp *id = 1596 (struct scsi_transportid_fcp *)buf; 1597 if (port->wwpn_iid[iid].wwpn == 0) 1598 return (0); 1599 memset(id, 0, sizeof(*id)); 1600 id->format_protocol = SCSI_PROTO_FC; 1601 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1602 return (sizeof(*id)); 1603 } 1604 case CTL_PORT_ISCSI: 1605 { 1606 struct scsi_transportid_iscsi_port *id = 1607 (struct scsi_transportid_iscsi_port *)buf; 1608 if (port->wwpn_iid[iid].name == NULL) 1609 return (0); 1610 memset(id, 0, 256); 1611 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1612 SCSI_PROTO_ISCSI; 1613 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1614 len = roundup2(min(len, 252), 4); 1615 scsi_ulto2b(len, id->additional_length); 1616 return (sizeof(*id) + len); 1617 } 1618 case CTL_PORT_SAS: 1619 { 1620 struct scsi_transportid_sas *id = 1621 (struct scsi_transportid_sas *)buf; 1622 if (port->wwpn_iid[iid].wwpn == 0) 1623 return (0); 1624 memset(id, 0, sizeof(*id)); 1625 id->format_protocol = SCSI_PROTO_SAS; 1626 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1627 return (sizeof(*id)); 1628 } 1629 default: 1630 { 1631 struct scsi_transportid_spi *id = 1632 (struct scsi_transportid_spi *)buf; 1633 memset(id, 0, sizeof(*id)); 1634 id->format_protocol = SCSI_PROTO_SPI; 1635 scsi_ulto2b(iid, id->scsi_addr); 1636 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1637 return (sizeof(*id)); 1638 } 1639 } 1640} 1641 1642static int 1643ctl_ioctl_lun_enable(void *arg, int lun_id) 1644{ 1645 return (0); 1646} 1647 1648static int 1649ctl_ioctl_lun_disable(void *arg, int lun_id) 1650{ 1651 return (0); 1652} 1653 1654/* 1655 * Data movement routine for the CTL ioctl frontend port. 1656 */ 1657static int 1658ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1659{ 1660 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1661 struct ctl_sg_entry ext_entry, kern_entry; 1662 int ext_sglen, ext_sg_entries, kern_sg_entries; 1663 int ext_sg_start, ext_offset; 1664 int len_to_copy, len_copied; 1665 int kern_watermark, ext_watermark; 1666 int ext_sglist_malloced; 1667 int i, j; 1668 1669 ext_sglist_malloced = 0; 1670 ext_sg_start = 0; 1671 ext_offset = 0; 1672 1673 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1674 1675 /* 1676 * If this flag is set, fake the data transfer. 1677 */ 1678 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1679 ctsio->ext_data_filled = ctsio->ext_data_len; 1680 goto bailout; 1681 } 1682 1683 /* 1684 * To simplify things here, if we have a single buffer, stick it in 1685 * a S/G entry and just make it a single entry S/G list. 1686 */ 1687 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1688 int len_seen; 1689 1690 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1691 1692 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1693 M_WAITOK); 1694 ext_sglist_malloced = 1; 1695 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1696 ext_sglen) != 0) { 1697 ctl_set_internal_failure(ctsio, 1698 /*sks_valid*/ 0, 1699 /*retry_count*/ 0); 1700 goto bailout; 1701 } 1702 ext_sg_entries = ctsio->ext_sg_entries; 1703 len_seen = 0; 1704 for (i = 0; i < ext_sg_entries; i++) { 1705 if ((len_seen + ext_sglist[i].len) >= 1706 ctsio->ext_data_filled) { 1707 ext_sg_start = i; 1708 ext_offset = ctsio->ext_data_filled - len_seen; 1709 break; 1710 } 1711 len_seen += ext_sglist[i].len; 1712 } 1713 } else { 1714 ext_sglist = &ext_entry; 1715 ext_sglist->addr = ctsio->ext_data_ptr; 1716 ext_sglist->len = ctsio->ext_data_len; 1717 ext_sg_entries = 1; 1718 ext_sg_start = 0; 1719 ext_offset = ctsio->ext_data_filled; 1720 } 1721 1722 if (ctsio->kern_sg_entries > 0) { 1723 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1724 kern_sg_entries = ctsio->kern_sg_entries; 1725 } else { 1726 kern_sglist = &kern_entry; 1727 kern_sglist->addr = ctsio->kern_data_ptr; 1728 kern_sglist->len = ctsio->kern_data_len; 1729 kern_sg_entries = 1; 1730 } 1731 1732 1733 kern_watermark = 0; 1734 ext_watermark = ext_offset; 1735 len_copied = 0; 1736 for (i = ext_sg_start, j = 0; 1737 i < ext_sg_entries && j < kern_sg_entries;) { 1738 uint8_t *ext_ptr, *kern_ptr; 1739 1740 len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 1741 kern_sglist[j].len - kern_watermark); 1742 1743 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1744 ext_ptr = ext_ptr + ext_watermark; 1745 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1746 /* 1747 * XXX KDM fix this! 1748 */ 1749 panic("need to implement bus address support"); 1750#if 0 1751 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1752#endif 1753 } else 1754 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1755 kern_ptr = kern_ptr + kern_watermark; 1756 1757 kern_watermark += len_to_copy; 1758 ext_watermark += len_to_copy; 1759 1760 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1761 CTL_FLAG_DATA_IN) { 1762 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1763 "bytes to user\n", len_to_copy)); 1764 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1765 "to %p\n", kern_ptr, ext_ptr)); 1766 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1767 ctl_set_internal_failure(ctsio, 1768 /*sks_valid*/ 0, 1769 /*retry_count*/ 0); 1770 goto bailout; 1771 } 1772 } else { 1773 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1774 "bytes from user\n", len_to_copy)); 1775 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1776 "to %p\n", ext_ptr, kern_ptr)); 1777 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1778 ctl_set_internal_failure(ctsio, 1779 /*sks_valid*/ 0, 1780 /*retry_count*/0); 1781 goto bailout; 1782 } 1783 } 1784 1785 len_copied += len_to_copy; 1786 1787 if (ext_sglist[i].len == ext_watermark) { 1788 i++; 1789 ext_watermark = 0; 1790 } 1791 1792 if (kern_sglist[j].len == kern_watermark) { 1793 j++; 1794 kern_watermark = 0; 1795 } 1796 } 1797 1798 ctsio->ext_data_filled += len_copied; 1799 1800 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1801 "kern_sg_entries: %d\n", ext_sg_entries, 1802 kern_sg_entries)); 1803 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1804 "kern_data_len = %d\n", ctsio->ext_data_len, 1805 ctsio->kern_data_len)); 1806 1807 1808 /* XXX KDM set residual?? */ 1809bailout: 1810 1811 if (ext_sglist_malloced != 0) 1812 free(ext_sglist, M_CTL); 1813 1814 return (CTL_RETVAL_COMPLETE); 1815} 1816 1817/* 1818 * Serialize a command that went down the "wrong" side, and so was sent to 1819 * this controller for execution. The logic is a little different than the 1820 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1821 * sent back to the other side, but in the success case, we execute the 1822 * command on this side (XFER mode) or tell the other side to execute it 1823 * (SER_ONLY mode). 1824 */ 1825static int 1826ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1827{ 1828 struct ctl_softc *softc; 1829 union ctl_ha_msg msg_info; 1830 struct ctl_lun *lun; 1831 int retval = 0; 1832 uint32_t targ_lun; 1833 1834 softc = control_softc; 1835 1836 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1837 lun = softc->ctl_luns[targ_lun]; 1838 if (lun==NULL) 1839 { 1840 /* 1841 * Why isn't LUN defined? The other side wouldn't 1842 * send a cmd if the LUN is undefined. 1843 */ 1844 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1845 1846 /* "Logical unit not supported" */ 1847 ctl_set_sense_data(&msg_info.scsi.sense_data, 1848 lun, 1849 /*sense_format*/SSD_TYPE_NONE, 1850 /*current_error*/ 1, 1851 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1852 /*asc*/ 0x25, 1853 /*ascq*/ 0x00, 1854 SSD_ELEM_NONE); 1855 1856 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1857 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1858 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1859 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1860 msg_info.hdr.serializing_sc = NULL; 1861 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1862 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1863 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1864 } 1865 return(1); 1866 1867 } 1868 1869 mtx_lock(&lun->lun_lock); 1870 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1871 1872 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1873 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1874 ooa_links))) { 1875 case CTL_ACTION_BLOCK: 1876 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1877 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1878 blocked_links); 1879 break; 1880 case CTL_ACTION_PASS: 1881 case CTL_ACTION_SKIP: 1882 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1883 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1884 ctl_enqueue_rtr((union ctl_io *)ctsio); 1885 } else { 1886 1887 /* send msg back to other side */ 1888 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1889 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1890 msg_info.hdr.msg_type = CTL_MSG_R2R; 1891#if 0 1892 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1893#endif 1894 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1895 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1896 } 1897 } 1898 break; 1899 case CTL_ACTION_OVERLAP: 1900 /* OVERLAPPED COMMANDS ATTEMPTED */ 1901 ctl_set_sense_data(&msg_info.scsi.sense_data, 1902 lun, 1903 /*sense_format*/SSD_TYPE_NONE, 1904 /*current_error*/ 1, 1905 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1906 /*asc*/ 0x4E, 1907 /*ascq*/ 0x00, 1908 SSD_ELEM_NONE); 1909 1910 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1911 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1912 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1913 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1914 msg_info.hdr.serializing_sc = NULL; 1915 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1916#if 0 1917 printf("BAD JUJU:Major Bummer Overlap\n"); 1918#endif 1919 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1920 retval = 1; 1921 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1922 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1923 } 1924 break; 1925 case CTL_ACTION_OVERLAP_TAG: 1926 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1927 ctl_set_sense_data(&msg_info.scsi.sense_data, 1928 lun, 1929 /*sense_format*/SSD_TYPE_NONE, 1930 /*current_error*/ 1, 1931 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1932 /*asc*/ 0x4D, 1933 /*ascq*/ ctsio->tag_num & 0xff, 1934 SSD_ELEM_NONE); 1935 1936 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1937 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1938 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1939 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1940 msg_info.hdr.serializing_sc = NULL; 1941 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1942#if 0 1943 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1944#endif 1945 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1946 retval = 1; 1947 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1948 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1949 } 1950 break; 1951 case CTL_ACTION_ERROR: 1952 default: 1953 /* "Internal target failure" */ 1954 ctl_set_sense_data(&msg_info.scsi.sense_data, 1955 lun, 1956 /*sense_format*/SSD_TYPE_NONE, 1957 /*current_error*/ 1, 1958 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1959 /*asc*/ 0x44, 1960 /*ascq*/ 0x00, 1961 SSD_ELEM_NONE); 1962 1963 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1964 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1965 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1966 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1967 msg_info.hdr.serializing_sc = NULL; 1968 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1969#if 0 1970 printf("BAD JUJU:Major Bummer HW Error\n"); 1971#endif 1972 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1973 retval = 1; 1974 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1975 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1976 } 1977 break; 1978 } 1979 mtx_unlock(&lun->lun_lock); 1980 return (retval); 1981} 1982 1983static int 1984ctl_ioctl_submit_wait(union ctl_io *io) 1985{ 1986 struct ctl_fe_ioctl_params params; 1987 ctl_fe_ioctl_state last_state; 1988 int done, retval; 1989 1990 retval = 0; 1991 1992 bzero(¶ms, sizeof(params)); 1993 1994 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1995 cv_init(¶ms.sem, "ctlioccv"); 1996 params.state = CTL_IOCTL_INPROG; 1997 last_state = params.state; 1998 1999 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 2000 2001 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 2002 2003 /* This shouldn't happen */ 2004 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 2005 return (retval); 2006 2007 done = 0; 2008 2009 do { 2010 mtx_lock(¶ms.ioctl_mtx); 2011 /* 2012 * Check the state here, and don't sleep if the state has 2013 * already changed (i.e. wakeup has already occured, but we 2014 * weren't waiting yet). 2015 */ 2016 if (params.state == last_state) { 2017 /* XXX KDM cv_wait_sig instead? */ 2018 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 2019 } 2020 last_state = params.state; 2021 2022 switch (params.state) { 2023 case CTL_IOCTL_INPROG: 2024 /* Why did we wake up? */ 2025 /* XXX KDM error here? */ 2026 mtx_unlock(¶ms.ioctl_mtx); 2027 break; 2028 case CTL_IOCTL_DATAMOVE: 2029 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 2030 2031 /* 2032 * change last_state back to INPROG to avoid 2033 * deadlock on subsequent data moves. 2034 */ 2035 params.state = last_state = CTL_IOCTL_INPROG; 2036 2037 mtx_unlock(¶ms.ioctl_mtx); 2038 ctl_ioctl_do_datamove(&io->scsiio); 2039 /* 2040 * Note that in some cases, most notably writes, 2041 * this will queue the I/O and call us back later. 2042 * In other cases, generally reads, this routine 2043 * will immediately call back and wake us up, 2044 * probably using our own context. 2045 */ 2046 io->scsiio.be_move_done(io); 2047 break; 2048 case CTL_IOCTL_DONE: 2049 mtx_unlock(¶ms.ioctl_mtx); 2050 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2051 done = 1; 2052 break; 2053 default: 2054 mtx_unlock(¶ms.ioctl_mtx); 2055 /* XXX KDM error here? */ 2056 break; 2057 } 2058 } while (done == 0); 2059 2060 mtx_destroy(¶ms.ioctl_mtx); 2061 cv_destroy(¶ms.sem); 2062 2063 return (CTL_RETVAL_COMPLETE); 2064} 2065 2066static void 2067ctl_ioctl_datamove(union ctl_io *io) 2068{ 2069 struct ctl_fe_ioctl_params *params; 2070 2071 params = (struct ctl_fe_ioctl_params *) 2072 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2073 2074 mtx_lock(¶ms->ioctl_mtx); 2075 params->state = CTL_IOCTL_DATAMOVE; 2076 cv_broadcast(¶ms->sem); 2077 mtx_unlock(¶ms->ioctl_mtx); 2078} 2079 2080static void 2081ctl_ioctl_done(union ctl_io *io) 2082{ 2083 struct ctl_fe_ioctl_params *params; 2084 2085 params = (struct ctl_fe_ioctl_params *) 2086 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2087 2088 mtx_lock(¶ms->ioctl_mtx); 2089 params->state = CTL_IOCTL_DONE; 2090 cv_broadcast(¶ms->sem); 2091 mtx_unlock(¶ms->ioctl_mtx); 2092} 2093 2094static void 2095ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2096{ 2097 struct ctl_fe_ioctl_startstop_info *sd_info; 2098 2099 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2100 2101 sd_info->hs_info.status = metatask->status; 2102 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2103 sd_info->hs_info.luns_complete = 2104 metatask->taskinfo.startstop.luns_complete; 2105 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2106 2107 cv_broadcast(&sd_info->sem); 2108} 2109 2110static void 2111ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2112{ 2113 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2114 2115 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2116 2117 mtx_lock(fe_bbr_info->lock); 2118 fe_bbr_info->bbr_info->status = metatask->status; 2119 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2120 fe_bbr_info->wakeup_done = 1; 2121 mtx_unlock(fe_bbr_info->lock); 2122 2123 cv_broadcast(&fe_bbr_info->sem); 2124} 2125 2126/* 2127 * Returns 0 for success, errno for failure. 2128 */ 2129static int 2130ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2131 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2132{ 2133 union ctl_io *io; 2134 int retval; 2135 2136 retval = 0; 2137 2138 mtx_lock(&lun->lun_lock); 2139 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2140 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2141 ooa_links)) { 2142 struct ctl_ooa_entry *entry; 2143 2144 /* 2145 * If we've got more than we can fit, just count the 2146 * remaining entries. 2147 */ 2148 if (*cur_fill_num >= ooa_hdr->alloc_num) 2149 continue; 2150 2151 entry = &kern_entries[*cur_fill_num]; 2152 2153 entry->tag_num = io->scsiio.tag_num; 2154 entry->lun_num = lun->lun; 2155#ifdef CTL_TIME_IO 2156 entry->start_bt = io->io_hdr.start_bt; 2157#endif 2158 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2159 entry->cdb_len = io->scsiio.cdb_len; 2160 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2161 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2162 2163 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2164 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2165 2166 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2167 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2168 2169 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2170 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2171 2172 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2173 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2174 } 2175 mtx_unlock(&lun->lun_lock); 2176 2177 return (retval); 2178} 2179 2180static void * 2181ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2182 size_t error_str_len) 2183{ 2184 void *kptr; 2185 2186 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2187 2188 if (copyin(user_addr, kptr, len) != 0) { 2189 snprintf(error_str, error_str_len, "Error copying %d bytes " 2190 "from user address %p to kernel address %p", len, 2191 user_addr, kptr); 2192 free(kptr, M_CTL); 2193 return (NULL); 2194 } 2195 2196 return (kptr); 2197} 2198 2199static void 2200ctl_free_args(int num_args, struct ctl_be_arg *args) 2201{ 2202 int i; 2203 2204 if (args == NULL) 2205 return; 2206 2207 for (i = 0; i < num_args; i++) { 2208 free(args[i].kname, M_CTL); 2209 free(args[i].kvalue, M_CTL); 2210 } 2211 2212 free(args, M_CTL); 2213} 2214 2215static struct ctl_be_arg * 2216ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2217 char *error_str, size_t error_str_len) 2218{ 2219 struct ctl_be_arg *args; 2220 int i; 2221 2222 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2223 error_str, error_str_len); 2224 2225 if (args == NULL) 2226 goto bailout; 2227 2228 for (i = 0; i < num_args; i++) { 2229 args[i].kname = NULL; 2230 args[i].kvalue = NULL; 2231 } 2232 2233 for (i = 0; i < num_args; i++) { 2234 uint8_t *tmpptr; 2235 2236 args[i].kname = ctl_copyin_alloc(args[i].name, 2237 args[i].namelen, error_str, error_str_len); 2238 if (args[i].kname == NULL) 2239 goto bailout; 2240 2241 if (args[i].kname[args[i].namelen - 1] != '\0') { 2242 snprintf(error_str, error_str_len, "Argument %d " 2243 "name is not NUL-terminated", i); 2244 goto bailout; 2245 } 2246 2247 if (args[i].flags & CTL_BEARG_RD) { 2248 tmpptr = ctl_copyin_alloc(args[i].value, 2249 args[i].vallen, error_str, error_str_len); 2250 if (tmpptr == NULL) 2251 goto bailout; 2252 if ((args[i].flags & CTL_BEARG_ASCII) 2253 && (tmpptr[args[i].vallen - 1] != '\0')) { 2254 snprintf(error_str, error_str_len, "Argument " 2255 "%d value is not NUL-terminated", i); 2256 goto bailout; 2257 } 2258 args[i].kvalue = tmpptr; 2259 } else { 2260 args[i].kvalue = malloc(args[i].vallen, 2261 M_CTL, M_WAITOK | M_ZERO); 2262 } 2263 } 2264 2265 return (args); 2266bailout: 2267 2268 ctl_free_args(num_args, args); 2269 2270 return (NULL); 2271} 2272 2273static void 2274ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2275{ 2276 int i; 2277 2278 for (i = 0; i < num_args; i++) { 2279 if (args[i].flags & CTL_BEARG_WR) 2280 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2281 } 2282} 2283 2284/* 2285 * Escape characters that are illegal or not recommended in XML. 2286 */ 2287int 2288ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2289{ 2290 char *end = str + size; 2291 int retval; 2292 2293 retval = 0; 2294 2295 for (; *str && str < end; str++) { 2296 switch (*str) { 2297 case '&': 2298 retval = sbuf_printf(sb, "&"); 2299 break; 2300 case '>': 2301 retval = sbuf_printf(sb, ">"); 2302 break; 2303 case '<': 2304 retval = sbuf_printf(sb, "<"); 2305 break; 2306 default: 2307 retval = sbuf_putc(sb, *str); 2308 break; 2309 } 2310 2311 if (retval != 0) 2312 break; 2313 2314 } 2315 2316 return (retval); 2317} 2318 2319static void 2320ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2321{ 2322 struct scsi_vpd_id_descriptor *desc; 2323 int i; 2324 2325 if (id == NULL || id->len < 4) 2326 return; 2327 desc = (struct scsi_vpd_id_descriptor *)id->data; 2328 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2329 case SVPD_ID_TYPE_T10: 2330 sbuf_printf(sb, "t10."); 2331 break; 2332 case SVPD_ID_TYPE_EUI64: 2333 sbuf_printf(sb, "eui."); 2334 break; 2335 case SVPD_ID_TYPE_NAA: 2336 sbuf_printf(sb, "naa."); 2337 break; 2338 case SVPD_ID_TYPE_SCSI_NAME: 2339 break; 2340 } 2341 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2342 case SVPD_ID_CODESET_BINARY: 2343 for (i = 0; i < desc->length; i++) 2344 sbuf_printf(sb, "%02x", desc->identifier[i]); 2345 break; 2346 case SVPD_ID_CODESET_ASCII: 2347 sbuf_printf(sb, "%.*s", (int)desc->length, 2348 (char *)desc->identifier); 2349 break; 2350 case SVPD_ID_CODESET_UTF8: 2351 sbuf_printf(sb, "%s", (char *)desc->identifier); 2352 break; 2353 } 2354} 2355 2356static int 2357ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2358 struct thread *td) 2359{ 2360 struct ctl_softc *softc; 2361 int retval; 2362 2363 softc = control_softc; 2364 2365 retval = 0; 2366 2367 switch (cmd) { 2368 case CTL_IO: { 2369 union ctl_io *io; 2370 void *pool_tmp; 2371 2372 /* 2373 * If we haven't been "enabled", don't allow any SCSI I/O 2374 * to this FETD. 2375 */ 2376 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2377 retval = EPERM; 2378 break; 2379 } 2380 2381 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2382 2383 /* 2384 * Need to save the pool reference so it doesn't get 2385 * spammed by the user's ctl_io. 2386 */ 2387 pool_tmp = io->io_hdr.pool; 2388 memcpy(io, (void *)addr, sizeof(*io)); 2389 io->io_hdr.pool = pool_tmp; 2390 2391 /* 2392 * No status yet, so make sure the status is set properly. 2393 */ 2394 io->io_hdr.status = CTL_STATUS_NONE; 2395 2396 /* 2397 * The user sets the initiator ID, target and LUN IDs. 2398 */ 2399 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2400 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2401 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2402 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2403 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2404 2405 retval = ctl_ioctl_submit_wait(io); 2406 2407 if (retval != 0) { 2408 ctl_free_io(io); 2409 break; 2410 } 2411 2412 memcpy((void *)addr, io, sizeof(*io)); 2413 2414 /* return this to our pool */ 2415 ctl_free_io(io); 2416 2417 break; 2418 } 2419 case CTL_ENABLE_PORT: 2420 case CTL_DISABLE_PORT: 2421 case CTL_SET_PORT_WWNS: { 2422 struct ctl_port *port; 2423 struct ctl_port_entry *entry; 2424 2425 entry = (struct ctl_port_entry *)addr; 2426 2427 mtx_lock(&softc->ctl_lock); 2428 STAILQ_FOREACH(port, &softc->port_list, links) { 2429 int action, done; 2430 2431 action = 0; 2432 done = 0; 2433 2434 if ((entry->port_type == CTL_PORT_NONE) 2435 && (entry->targ_port == port->targ_port)) { 2436 /* 2437 * If the user only wants to enable or 2438 * disable or set WWNs on a specific port, 2439 * do the operation and we're done. 2440 */ 2441 action = 1; 2442 done = 1; 2443 } else if (entry->port_type & port->port_type) { 2444 /* 2445 * Compare the user's type mask with the 2446 * particular frontend type to see if we 2447 * have a match. 2448 */ 2449 action = 1; 2450 done = 0; 2451 2452 /* 2453 * Make sure the user isn't trying to set 2454 * WWNs on multiple ports at the same time. 2455 */ 2456 if (cmd == CTL_SET_PORT_WWNS) { 2457 printf("%s: Can't set WWNs on " 2458 "multiple ports\n", __func__); 2459 retval = EINVAL; 2460 break; 2461 } 2462 } 2463 if (action != 0) { 2464 /* 2465 * XXX KDM we have to drop the lock here, 2466 * because the online/offline operations 2467 * can potentially block. We need to 2468 * reference count the frontends so they 2469 * can't go away, 2470 */ 2471 mtx_unlock(&softc->ctl_lock); 2472 2473 if (cmd == CTL_ENABLE_PORT) { 2474 ctl_port_online(port); 2475 } else if (cmd == CTL_DISABLE_PORT) { 2476 ctl_port_offline(port); 2477 } 2478 2479 mtx_lock(&softc->ctl_lock); 2480 2481 if (cmd == CTL_SET_PORT_WWNS) 2482 ctl_port_set_wwns(port, 2483 (entry->flags & CTL_PORT_WWNN_VALID) ? 2484 1 : 0, entry->wwnn, 2485 (entry->flags & CTL_PORT_WWPN_VALID) ? 2486 1 : 0, entry->wwpn); 2487 } 2488 if (done != 0) 2489 break; 2490 } 2491 mtx_unlock(&softc->ctl_lock); 2492 break; 2493 } 2494 case CTL_GET_PORT_LIST: { 2495 struct ctl_port *port; 2496 struct ctl_port_list *list; 2497 int i; 2498 2499 list = (struct ctl_port_list *)addr; 2500 2501 if (list->alloc_len != (list->alloc_num * 2502 sizeof(struct ctl_port_entry))) { 2503 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2504 "alloc_num %u * sizeof(struct ctl_port_entry) " 2505 "%zu\n", __func__, list->alloc_len, 2506 list->alloc_num, sizeof(struct ctl_port_entry)); 2507 retval = EINVAL; 2508 break; 2509 } 2510 list->fill_len = 0; 2511 list->fill_num = 0; 2512 list->dropped_num = 0; 2513 i = 0; 2514 mtx_lock(&softc->ctl_lock); 2515 STAILQ_FOREACH(port, &softc->port_list, links) { 2516 struct ctl_port_entry entry, *list_entry; 2517 2518 if (list->fill_num >= list->alloc_num) { 2519 list->dropped_num++; 2520 continue; 2521 } 2522 2523 entry.port_type = port->port_type; 2524 strlcpy(entry.port_name, port->port_name, 2525 sizeof(entry.port_name)); 2526 entry.targ_port = port->targ_port; 2527 entry.physical_port = port->physical_port; 2528 entry.virtual_port = port->virtual_port; 2529 entry.wwnn = port->wwnn; 2530 entry.wwpn = port->wwpn; 2531 if (port->status & CTL_PORT_STATUS_ONLINE) 2532 entry.online = 1; 2533 else 2534 entry.online = 0; 2535 2536 list_entry = &list->entries[i]; 2537 2538 retval = copyout(&entry, list_entry, sizeof(entry)); 2539 if (retval != 0) { 2540 printf("%s: CTL_GET_PORT_LIST: copyout " 2541 "returned %d\n", __func__, retval); 2542 break; 2543 } 2544 i++; 2545 list->fill_num++; 2546 list->fill_len += sizeof(entry); 2547 } 2548 mtx_unlock(&softc->ctl_lock); 2549 2550 /* 2551 * If this is non-zero, we had a copyout fault, so there's 2552 * probably no point in attempting to set the status inside 2553 * the structure. 2554 */ 2555 if (retval != 0) 2556 break; 2557 2558 if (list->dropped_num > 0) 2559 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2560 else 2561 list->status = CTL_PORT_LIST_OK; 2562 break; 2563 } 2564 case CTL_DUMP_OOA: { 2565 struct ctl_lun *lun; 2566 union ctl_io *io; 2567 char printbuf[128]; 2568 struct sbuf sb; 2569 2570 mtx_lock(&softc->ctl_lock); 2571 printf("Dumping OOA queues:\n"); 2572 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2573 mtx_lock(&lun->lun_lock); 2574 for (io = (union ctl_io *)TAILQ_FIRST( 2575 &lun->ooa_queue); io != NULL; 2576 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2577 ooa_links)) { 2578 sbuf_new(&sb, printbuf, sizeof(printbuf), 2579 SBUF_FIXEDLEN); 2580 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2581 (intmax_t)lun->lun, 2582 io->scsiio.tag_num, 2583 (io->io_hdr.flags & 2584 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2585 (io->io_hdr.flags & 2586 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2587 (io->io_hdr.flags & 2588 CTL_FLAG_ABORT) ? " ABORT" : "", 2589 (io->io_hdr.flags & 2590 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2591 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2592 sbuf_finish(&sb); 2593 printf("%s\n", sbuf_data(&sb)); 2594 } 2595 mtx_unlock(&lun->lun_lock); 2596 } 2597 printf("OOA queues dump done\n"); 2598 mtx_unlock(&softc->ctl_lock); 2599 break; 2600 } 2601 case CTL_GET_OOA: { 2602 struct ctl_lun *lun; 2603 struct ctl_ooa *ooa_hdr; 2604 struct ctl_ooa_entry *entries; 2605 uint32_t cur_fill_num; 2606 2607 ooa_hdr = (struct ctl_ooa *)addr; 2608 2609 if ((ooa_hdr->alloc_len == 0) 2610 || (ooa_hdr->alloc_num == 0)) { 2611 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2612 "must be non-zero\n", __func__, 2613 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2614 retval = EINVAL; 2615 break; 2616 } 2617 2618 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2619 sizeof(struct ctl_ooa_entry))) { 2620 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2621 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2622 __func__, ooa_hdr->alloc_len, 2623 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2624 retval = EINVAL; 2625 break; 2626 } 2627 2628 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2629 if (entries == NULL) { 2630 printf("%s: could not allocate %d bytes for OOA " 2631 "dump\n", __func__, ooa_hdr->alloc_len); 2632 retval = ENOMEM; 2633 break; 2634 } 2635 2636 mtx_lock(&softc->ctl_lock); 2637 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2638 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2639 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2640 mtx_unlock(&softc->ctl_lock); 2641 free(entries, M_CTL); 2642 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2643 __func__, (uintmax_t)ooa_hdr->lun_num); 2644 retval = EINVAL; 2645 break; 2646 } 2647 2648 cur_fill_num = 0; 2649 2650 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2651 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2652 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2653 ooa_hdr, entries); 2654 if (retval != 0) 2655 break; 2656 } 2657 if (retval != 0) { 2658 mtx_unlock(&softc->ctl_lock); 2659 free(entries, M_CTL); 2660 break; 2661 } 2662 } else { 2663 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2664 2665 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2666 entries); 2667 } 2668 mtx_unlock(&softc->ctl_lock); 2669 2670 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2671 ooa_hdr->fill_len = ooa_hdr->fill_num * 2672 sizeof(struct ctl_ooa_entry); 2673 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2674 if (retval != 0) { 2675 printf("%s: error copying out %d bytes for OOA dump\n", 2676 __func__, ooa_hdr->fill_len); 2677 } 2678 2679 getbintime(&ooa_hdr->cur_bt); 2680 2681 if (cur_fill_num > ooa_hdr->alloc_num) { 2682 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2683 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2684 } else { 2685 ooa_hdr->dropped_num = 0; 2686 ooa_hdr->status = CTL_OOA_OK; 2687 } 2688 2689 free(entries, M_CTL); 2690 break; 2691 } 2692 case CTL_CHECK_OOA: { 2693 union ctl_io *io; 2694 struct ctl_lun *lun; 2695 struct ctl_ooa_info *ooa_info; 2696 2697 2698 ooa_info = (struct ctl_ooa_info *)addr; 2699 2700 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2701 ooa_info->status = CTL_OOA_INVALID_LUN; 2702 break; 2703 } 2704 mtx_lock(&softc->ctl_lock); 2705 lun = softc->ctl_luns[ooa_info->lun_id]; 2706 if (lun == NULL) { 2707 mtx_unlock(&softc->ctl_lock); 2708 ooa_info->status = CTL_OOA_INVALID_LUN; 2709 break; 2710 } 2711 mtx_lock(&lun->lun_lock); 2712 mtx_unlock(&softc->ctl_lock); 2713 ooa_info->num_entries = 0; 2714 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2715 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2716 &io->io_hdr, ooa_links)) { 2717 ooa_info->num_entries++; 2718 } 2719 mtx_unlock(&lun->lun_lock); 2720 2721 ooa_info->status = CTL_OOA_SUCCESS; 2722 2723 break; 2724 } 2725 case CTL_HARD_START: 2726 case CTL_HARD_STOP: { 2727 struct ctl_fe_ioctl_startstop_info ss_info; 2728 struct cfi_metatask *metatask; 2729 struct mtx hs_mtx; 2730 2731 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2732 2733 cv_init(&ss_info.sem, "hard start/stop cv" ); 2734 2735 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2736 if (metatask == NULL) { 2737 retval = ENOMEM; 2738 mtx_destroy(&hs_mtx); 2739 break; 2740 } 2741 2742 if (cmd == CTL_HARD_START) 2743 metatask->tasktype = CFI_TASK_STARTUP; 2744 else 2745 metatask->tasktype = CFI_TASK_SHUTDOWN; 2746 2747 metatask->callback = ctl_ioctl_hard_startstop_callback; 2748 metatask->callback_arg = &ss_info; 2749 2750 cfi_action(metatask); 2751 2752 /* Wait for the callback */ 2753 mtx_lock(&hs_mtx); 2754 cv_wait_sig(&ss_info.sem, &hs_mtx); 2755 mtx_unlock(&hs_mtx); 2756 2757 /* 2758 * All information has been copied from the metatask by the 2759 * time cv_broadcast() is called, so we free the metatask here. 2760 */ 2761 cfi_free_metatask(metatask); 2762 2763 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2764 2765 mtx_destroy(&hs_mtx); 2766 break; 2767 } 2768 case CTL_BBRREAD: { 2769 struct ctl_bbrread_info *bbr_info; 2770 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2771 struct mtx bbr_mtx; 2772 struct cfi_metatask *metatask; 2773 2774 bbr_info = (struct ctl_bbrread_info *)addr; 2775 2776 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2777 2778 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2779 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2780 2781 fe_bbr_info.bbr_info = bbr_info; 2782 fe_bbr_info.lock = &bbr_mtx; 2783 2784 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2785 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2786 2787 if (metatask == NULL) { 2788 mtx_destroy(&bbr_mtx); 2789 cv_destroy(&fe_bbr_info.sem); 2790 retval = ENOMEM; 2791 break; 2792 } 2793 metatask->tasktype = CFI_TASK_BBRREAD; 2794 metatask->callback = ctl_ioctl_bbrread_callback; 2795 metatask->callback_arg = &fe_bbr_info; 2796 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2797 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2798 metatask->taskinfo.bbrread.len = bbr_info->len; 2799 2800 cfi_action(metatask); 2801 2802 mtx_lock(&bbr_mtx); 2803 while (fe_bbr_info.wakeup_done == 0) 2804 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2805 mtx_unlock(&bbr_mtx); 2806 2807 bbr_info->status = metatask->status; 2808 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2809 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2810 memcpy(&bbr_info->sense_data, 2811 &metatask->taskinfo.bbrread.sense_data, 2812 MIN(sizeof(bbr_info->sense_data), 2813 sizeof(metatask->taskinfo.bbrread.sense_data))); 2814 2815 cfi_free_metatask(metatask); 2816 2817 mtx_destroy(&bbr_mtx); 2818 cv_destroy(&fe_bbr_info.sem); 2819 2820 break; 2821 } 2822 case CTL_DELAY_IO: { 2823 struct ctl_io_delay_info *delay_info; 2824#ifdef CTL_IO_DELAY 2825 struct ctl_lun *lun; 2826#endif /* CTL_IO_DELAY */ 2827 2828 delay_info = (struct ctl_io_delay_info *)addr; 2829 2830#ifdef CTL_IO_DELAY 2831 mtx_lock(&softc->ctl_lock); 2832 2833 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2834 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2835 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2836 } else { 2837 lun = softc->ctl_luns[delay_info->lun_id]; 2838 mtx_lock(&lun->lun_lock); 2839 2840 delay_info->status = CTL_DELAY_STATUS_OK; 2841 2842 switch (delay_info->delay_type) { 2843 case CTL_DELAY_TYPE_CONT: 2844 break; 2845 case CTL_DELAY_TYPE_ONESHOT: 2846 break; 2847 default: 2848 delay_info->status = 2849 CTL_DELAY_STATUS_INVALID_TYPE; 2850 break; 2851 } 2852 2853 switch (delay_info->delay_loc) { 2854 case CTL_DELAY_LOC_DATAMOVE: 2855 lun->delay_info.datamove_type = 2856 delay_info->delay_type; 2857 lun->delay_info.datamove_delay = 2858 delay_info->delay_secs; 2859 break; 2860 case CTL_DELAY_LOC_DONE: 2861 lun->delay_info.done_type = 2862 delay_info->delay_type; 2863 lun->delay_info.done_delay = 2864 delay_info->delay_secs; 2865 break; 2866 default: 2867 delay_info->status = 2868 CTL_DELAY_STATUS_INVALID_LOC; 2869 break; 2870 } 2871 mtx_unlock(&lun->lun_lock); 2872 } 2873 2874 mtx_unlock(&softc->ctl_lock); 2875#else 2876 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2877#endif /* CTL_IO_DELAY */ 2878 break; 2879 } 2880 case CTL_REALSYNC_SET: { 2881 int *syncstate; 2882 2883 syncstate = (int *)addr; 2884 2885 mtx_lock(&softc->ctl_lock); 2886 switch (*syncstate) { 2887 case 0: 2888 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2889 break; 2890 case 1: 2891 softc->flags |= CTL_FLAG_REAL_SYNC; 2892 break; 2893 default: 2894 retval = EINVAL; 2895 break; 2896 } 2897 mtx_unlock(&softc->ctl_lock); 2898 break; 2899 } 2900 case CTL_REALSYNC_GET: { 2901 int *syncstate; 2902 2903 syncstate = (int*)addr; 2904 2905 mtx_lock(&softc->ctl_lock); 2906 if (softc->flags & CTL_FLAG_REAL_SYNC) 2907 *syncstate = 1; 2908 else 2909 *syncstate = 0; 2910 mtx_unlock(&softc->ctl_lock); 2911 2912 break; 2913 } 2914 case CTL_SETSYNC: 2915 case CTL_GETSYNC: { 2916 struct ctl_sync_info *sync_info; 2917 struct ctl_lun *lun; 2918 2919 sync_info = (struct ctl_sync_info *)addr; 2920 2921 mtx_lock(&softc->ctl_lock); 2922 lun = softc->ctl_luns[sync_info->lun_id]; 2923 if (lun == NULL) { 2924 mtx_unlock(&softc->ctl_lock); 2925 sync_info->status = CTL_GS_SYNC_NO_LUN; 2926 } 2927 /* 2928 * Get or set the sync interval. We're not bounds checking 2929 * in the set case, hopefully the user won't do something 2930 * silly. 2931 */ 2932 mtx_lock(&lun->lun_lock); 2933 mtx_unlock(&softc->ctl_lock); 2934 if (cmd == CTL_GETSYNC) 2935 sync_info->sync_interval = lun->sync_interval; 2936 else 2937 lun->sync_interval = sync_info->sync_interval; 2938 mtx_unlock(&lun->lun_lock); 2939 2940 sync_info->status = CTL_GS_SYNC_OK; 2941 2942 break; 2943 } 2944 case CTL_GETSTATS: { 2945 struct ctl_stats *stats; 2946 struct ctl_lun *lun; 2947 int i; 2948 2949 stats = (struct ctl_stats *)addr; 2950 2951 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2952 stats->alloc_len) { 2953 stats->status = CTL_SS_NEED_MORE_SPACE; 2954 stats->num_luns = softc->num_luns; 2955 break; 2956 } 2957 /* 2958 * XXX KDM no locking here. If the LUN list changes, 2959 * things can blow up. 2960 */ 2961 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2962 i++, lun = STAILQ_NEXT(lun, links)) { 2963 retval = copyout(&lun->stats, &stats->lun_stats[i], 2964 sizeof(lun->stats)); 2965 if (retval != 0) 2966 break; 2967 } 2968 stats->num_luns = softc->num_luns; 2969 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2970 softc->num_luns; 2971 stats->status = CTL_SS_OK; 2972#ifdef CTL_TIME_IO 2973 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2974#else 2975 stats->flags = CTL_STATS_FLAG_NONE; 2976#endif 2977 getnanouptime(&stats->timestamp); 2978 break; 2979 } 2980 case CTL_ERROR_INJECT: { 2981 struct ctl_error_desc *err_desc, *new_err_desc; 2982 struct ctl_lun *lun; 2983 2984 err_desc = (struct ctl_error_desc *)addr; 2985 2986 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2987 M_WAITOK | M_ZERO); 2988 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2989 2990 mtx_lock(&softc->ctl_lock); 2991 lun = softc->ctl_luns[err_desc->lun_id]; 2992 if (lun == NULL) { 2993 mtx_unlock(&softc->ctl_lock); 2994 free(new_err_desc, M_CTL); 2995 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2996 __func__, (uintmax_t)err_desc->lun_id); 2997 retval = EINVAL; 2998 break; 2999 } 3000 mtx_lock(&lun->lun_lock); 3001 mtx_unlock(&softc->ctl_lock); 3002 3003 /* 3004 * We could do some checking here to verify the validity 3005 * of the request, but given the complexity of error 3006 * injection requests, the checking logic would be fairly 3007 * complex. 3008 * 3009 * For now, if the request is invalid, it just won't get 3010 * executed and might get deleted. 3011 */ 3012 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3013 3014 /* 3015 * XXX KDM check to make sure the serial number is unique, 3016 * in case we somehow manage to wrap. That shouldn't 3017 * happen for a very long time, but it's the right thing to 3018 * do. 3019 */ 3020 new_err_desc->serial = lun->error_serial; 3021 err_desc->serial = lun->error_serial; 3022 lun->error_serial++; 3023 3024 mtx_unlock(&lun->lun_lock); 3025 break; 3026 } 3027 case CTL_ERROR_INJECT_DELETE: { 3028 struct ctl_error_desc *delete_desc, *desc, *desc2; 3029 struct ctl_lun *lun; 3030 int delete_done; 3031 3032 delete_desc = (struct ctl_error_desc *)addr; 3033 delete_done = 0; 3034 3035 mtx_lock(&softc->ctl_lock); 3036 lun = softc->ctl_luns[delete_desc->lun_id]; 3037 if (lun == NULL) { 3038 mtx_unlock(&softc->ctl_lock); 3039 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3040 __func__, (uintmax_t)delete_desc->lun_id); 3041 retval = EINVAL; 3042 break; 3043 } 3044 mtx_lock(&lun->lun_lock); 3045 mtx_unlock(&softc->ctl_lock); 3046 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3047 if (desc->serial != delete_desc->serial) 3048 continue; 3049 3050 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3051 links); 3052 free(desc, M_CTL); 3053 delete_done = 1; 3054 } 3055 mtx_unlock(&lun->lun_lock); 3056 if (delete_done == 0) { 3057 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3058 "error serial %ju on LUN %u\n", __func__, 3059 delete_desc->serial, delete_desc->lun_id); 3060 retval = EINVAL; 3061 break; 3062 } 3063 break; 3064 } 3065 case CTL_DUMP_STRUCTS: { 3066 int i, j, k; 3067 struct ctl_port *port; 3068 struct ctl_frontend *fe; 3069 3070 mtx_lock(&softc->ctl_lock); 3071 printf("CTL Persistent Reservation information start:\n"); 3072 for (i = 0; i < CTL_MAX_LUNS; i++) { 3073 struct ctl_lun *lun; 3074 3075 lun = softc->ctl_luns[i]; 3076 3077 if ((lun == NULL) 3078 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3079 continue; 3080 3081 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3082 if (lun->pr_keys[j] == NULL) 3083 continue; 3084 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3085 if (lun->pr_keys[j][k] == 0) 3086 continue; 3087 printf(" LUN %d port %d iid %d key " 3088 "%#jx\n", i, j, k, 3089 (uintmax_t)lun->pr_keys[j][k]); 3090 } 3091 } 3092 } 3093 printf("CTL Persistent Reservation information end\n"); 3094 printf("CTL Ports:\n"); 3095 STAILQ_FOREACH(port, &softc->port_list, links) { 3096 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3097 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3098 port->frontend->name, port->port_type, 3099 port->physical_port, port->virtual_port, 3100 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3101 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3102 if (port->wwpn_iid[j].in_use == 0 && 3103 port->wwpn_iid[j].wwpn == 0 && 3104 port->wwpn_iid[j].name == NULL) 3105 continue; 3106 3107 printf(" iid %u use %d WWPN %#jx '%s'\n", 3108 j, port->wwpn_iid[j].in_use, 3109 (uintmax_t)port->wwpn_iid[j].wwpn, 3110 port->wwpn_iid[j].name); 3111 } 3112 } 3113 printf("CTL Port information end\n"); 3114 mtx_unlock(&softc->ctl_lock); 3115 /* 3116 * XXX KDM calling this without a lock. We'd likely want 3117 * to drop the lock before calling the frontend's dump 3118 * routine anyway. 3119 */ 3120 printf("CTL Frontends:\n"); 3121 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3122 printf(" Frontend '%s'\n", fe->name); 3123 if (fe->fe_dump != NULL) 3124 fe->fe_dump(); 3125 } 3126 printf("CTL Frontend information end\n"); 3127 break; 3128 } 3129 case CTL_LUN_REQ: { 3130 struct ctl_lun_req *lun_req; 3131 struct ctl_backend_driver *backend; 3132 3133 lun_req = (struct ctl_lun_req *)addr; 3134 3135 backend = ctl_backend_find(lun_req->backend); 3136 if (backend == NULL) { 3137 lun_req->status = CTL_LUN_ERROR; 3138 snprintf(lun_req->error_str, 3139 sizeof(lun_req->error_str), 3140 "Backend \"%s\" not found.", 3141 lun_req->backend); 3142 break; 3143 } 3144 if (lun_req->num_be_args > 0) { 3145 lun_req->kern_be_args = ctl_copyin_args( 3146 lun_req->num_be_args, 3147 lun_req->be_args, 3148 lun_req->error_str, 3149 sizeof(lun_req->error_str)); 3150 if (lun_req->kern_be_args == NULL) { 3151 lun_req->status = CTL_LUN_ERROR; 3152 break; 3153 } 3154 } 3155 3156 retval = backend->ioctl(dev, cmd, addr, flag, td); 3157 3158 if (lun_req->num_be_args > 0) { 3159 ctl_copyout_args(lun_req->num_be_args, 3160 lun_req->kern_be_args); 3161 ctl_free_args(lun_req->num_be_args, 3162 lun_req->kern_be_args); 3163 } 3164 break; 3165 } 3166 case CTL_LUN_LIST: { 3167 struct sbuf *sb; 3168 struct ctl_lun *lun; 3169 struct ctl_lun_list *list; 3170 struct ctl_option *opt; 3171 3172 list = (struct ctl_lun_list *)addr; 3173 3174 /* 3175 * Allocate a fixed length sbuf here, based on the length 3176 * of the user's buffer. We could allocate an auto-extending 3177 * buffer, and then tell the user how much larger our 3178 * amount of data is than his buffer, but that presents 3179 * some problems: 3180 * 3181 * 1. The sbuf(9) routines use a blocking malloc, and so 3182 * we can't hold a lock while calling them with an 3183 * auto-extending buffer. 3184 * 3185 * 2. There is not currently a LUN reference counting 3186 * mechanism, outside of outstanding transactions on 3187 * the LUN's OOA queue. So a LUN could go away on us 3188 * while we're getting the LUN number, backend-specific 3189 * information, etc. Thus, given the way things 3190 * currently work, we need to hold the CTL lock while 3191 * grabbing LUN information. 3192 * 3193 * So, from the user's standpoint, the best thing to do is 3194 * allocate what he thinks is a reasonable buffer length, 3195 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3196 * double the buffer length and try again. (And repeat 3197 * that until he succeeds.) 3198 */ 3199 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3200 if (sb == NULL) { 3201 list->status = CTL_LUN_LIST_ERROR; 3202 snprintf(list->error_str, sizeof(list->error_str), 3203 "Unable to allocate %d bytes for LUN list", 3204 list->alloc_len); 3205 break; 3206 } 3207 3208 sbuf_printf(sb, "<ctllunlist>\n"); 3209 3210 mtx_lock(&softc->ctl_lock); 3211 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3212 mtx_lock(&lun->lun_lock); 3213 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3214 (uintmax_t)lun->lun); 3215 3216 /* 3217 * Bail out as soon as we see that we've overfilled 3218 * the buffer. 3219 */ 3220 if (retval != 0) 3221 break; 3222 3223 retval = sbuf_printf(sb, "\t<backend_type>%s" 3224 "</backend_type>\n", 3225 (lun->backend == NULL) ? "none" : 3226 lun->backend->name); 3227 3228 if (retval != 0) 3229 break; 3230 3231 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3232 lun->be_lun->lun_type); 3233 3234 if (retval != 0) 3235 break; 3236 3237 if (lun->backend == NULL) { 3238 retval = sbuf_printf(sb, "</lun>\n"); 3239 if (retval != 0) 3240 break; 3241 continue; 3242 } 3243 3244 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3245 (lun->be_lun->maxlba > 0) ? 3246 lun->be_lun->maxlba + 1 : 0); 3247 3248 if (retval != 0) 3249 break; 3250 3251 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3252 lun->be_lun->blocksize); 3253 3254 if (retval != 0) 3255 break; 3256 3257 retval = sbuf_printf(sb, "\t<serial_number>"); 3258 3259 if (retval != 0) 3260 break; 3261 3262 retval = ctl_sbuf_printf_esc(sb, 3263 lun->be_lun->serial_num, 3264 sizeof(lun->be_lun->serial_num)); 3265 3266 if (retval != 0) 3267 break; 3268 3269 retval = sbuf_printf(sb, "</serial_number>\n"); 3270 3271 if (retval != 0) 3272 break; 3273 3274 retval = sbuf_printf(sb, "\t<device_id>"); 3275 3276 if (retval != 0) 3277 break; 3278 3279 retval = ctl_sbuf_printf_esc(sb, 3280 lun->be_lun->device_id, 3281 sizeof(lun->be_lun->device_id)); 3282 3283 if (retval != 0) 3284 break; 3285 3286 retval = sbuf_printf(sb, "</device_id>\n"); 3287 3288 if (retval != 0) 3289 break; 3290 3291 if (lun->backend->lun_info != NULL) { 3292 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3293 if (retval != 0) 3294 break; 3295 } 3296 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3297 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3298 opt->name, opt->value, opt->name); 3299 if (retval != 0) 3300 break; 3301 } 3302 3303 retval = sbuf_printf(sb, "</lun>\n"); 3304 3305 if (retval != 0) 3306 break; 3307 mtx_unlock(&lun->lun_lock); 3308 } 3309 if (lun != NULL) 3310 mtx_unlock(&lun->lun_lock); 3311 mtx_unlock(&softc->ctl_lock); 3312 3313 if ((retval != 0) 3314 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3315 retval = 0; 3316 sbuf_delete(sb); 3317 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3318 snprintf(list->error_str, sizeof(list->error_str), 3319 "Out of space, %d bytes is too small", 3320 list->alloc_len); 3321 break; 3322 } 3323 3324 sbuf_finish(sb); 3325 3326 retval = copyout(sbuf_data(sb), list->lun_xml, 3327 sbuf_len(sb) + 1); 3328 3329 list->fill_len = sbuf_len(sb) + 1; 3330 list->status = CTL_LUN_LIST_OK; 3331 sbuf_delete(sb); 3332 break; 3333 } 3334 case CTL_ISCSI: { 3335 struct ctl_iscsi *ci; 3336 struct ctl_frontend *fe; 3337 3338 ci = (struct ctl_iscsi *)addr; 3339 3340 fe = ctl_frontend_find("iscsi"); 3341 if (fe == NULL) { 3342 ci->status = CTL_ISCSI_ERROR; 3343 snprintf(ci->error_str, sizeof(ci->error_str), 3344 "Frontend \"iscsi\" not found."); 3345 break; 3346 } 3347 3348 retval = fe->ioctl(dev, cmd, addr, flag, td); 3349 break; 3350 } 3351 case CTL_PORT_REQ: { 3352 struct ctl_req *req; 3353 struct ctl_frontend *fe; 3354 3355 req = (struct ctl_req *)addr; 3356 3357 fe = ctl_frontend_find(req->driver); 3358 if (fe == NULL) { 3359 req->status = CTL_LUN_ERROR; 3360 snprintf(req->error_str, sizeof(req->error_str), 3361 "Frontend \"%s\" not found.", req->driver); 3362 break; 3363 } 3364 if (req->num_args > 0) { 3365 req->kern_args = ctl_copyin_args(req->num_args, 3366 req->args, req->error_str, sizeof(req->error_str)); 3367 if (req->kern_args == NULL) { 3368 req->status = CTL_LUN_ERROR; 3369 break; 3370 } 3371 } 3372 3373 retval = fe->ioctl(dev, cmd, addr, flag, td); 3374 3375 if (req->num_args > 0) { 3376 ctl_copyout_args(req->num_args, req->kern_args); 3377 ctl_free_args(req->num_args, req->kern_args); 3378 } 3379 break; 3380 } 3381 case CTL_PORT_LIST: { 3382 struct sbuf *sb; 3383 struct ctl_port *port; 3384 struct ctl_lun_list *list; 3385 struct ctl_option *opt; 3386 int j; 3387 uint32_t plun; 3388 3389 list = (struct ctl_lun_list *)addr; 3390 3391 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3392 if (sb == NULL) { 3393 list->status = CTL_LUN_LIST_ERROR; 3394 snprintf(list->error_str, sizeof(list->error_str), 3395 "Unable to allocate %d bytes for LUN list", 3396 list->alloc_len); 3397 break; 3398 } 3399 3400 sbuf_printf(sb, "<ctlportlist>\n"); 3401 3402 mtx_lock(&softc->ctl_lock); 3403 STAILQ_FOREACH(port, &softc->port_list, links) { 3404 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3405 (uintmax_t)port->targ_port); 3406 3407 /* 3408 * Bail out as soon as we see that we've overfilled 3409 * the buffer. 3410 */ 3411 if (retval != 0) 3412 break; 3413 3414 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3415 "</frontend_type>\n", port->frontend->name); 3416 if (retval != 0) 3417 break; 3418 3419 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3420 port->port_type); 3421 if (retval != 0) 3422 break; 3423 3424 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3425 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3426 if (retval != 0) 3427 break; 3428 3429 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3430 port->port_name); 3431 if (retval != 0) 3432 break; 3433 3434 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3435 port->physical_port); 3436 if (retval != 0) 3437 break; 3438 3439 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3440 port->virtual_port); 3441 if (retval != 0) 3442 break; 3443 3444 if (port->target_devid != NULL) { 3445 sbuf_printf(sb, "\t<target>"); 3446 ctl_id_sbuf(port->target_devid, sb); 3447 sbuf_printf(sb, "</target>\n"); 3448 } 3449 3450 if (port->port_devid != NULL) { 3451 sbuf_printf(sb, "\t<port>"); 3452 ctl_id_sbuf(port->port_devid, sb); 3453 sbuf_printf(sb, "</port>\n"); 3454 } 3455 3456 if (port->port_info != NULL) { 3457 retval = port->port_info(port->onoff_arg, sb); 3458 if (retval != 0) 3459 break; 3460 } 3461 STAILQ_FOREACH(opt, &port->options, links) { 3462 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3463 opt->name, opt->value, opt->name); 3464 if (retval != 0) 3465 break; 3466 } 3467 3468 if (port->lun_map != NULL) { 3469 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3470 for (j = 0; j < CTL_MAX_LUNS; j++) { 3471 plun = ctl_lun_map_from_port(port, j); 3472 if (plun >= CTL_MAX_LUNS) 3473 continue; 3474 sbuf_printf(sb, 3475 "\t<lun id=\"%u\">%u</lun>\n", 3476 j, plun); 3477 } 3478 } 3479 3480 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3481 if (port->wwpn_iid[j].in_use == 0 || 3482 (port->wwpn_iid[j].wwpn == 0 && 3483 port->wwpn_iid[j].name == NULL)) 3484 continue; 3485 3486 if (port->wwpn_iid[j].name != NULL) 3487 retval = sbuf_printf(sb, 3488 "\t<initiator id=\"%u\">%s</initiator>\n", 3489 j, port->wwpn_iid[j].name); 3490 else 3491 retval = sbuf_printf(sb, 3492 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3493 j, port->wwpn_iid[j].wwpn); 3494 if (retval != 0) 3495 break; 3496 } 3497 if (retval != 0) 3498 break; 3499 3500 retval = sbuf_printf(sb, "</targ_port>\n"); 3501 if (retval != 0) 3502 break; 3503 } 3504 mtx_unlock(&softc->ctl_lock); 3505 3506 if ((retval != 0) 3507 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3508 retval = 0; 3509 sbuf_delete(sb); 3510 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3511 snprintf(list->error_str, sizeof(list->error_str), 3512 "Out of space, %d bytes is too small", 3513 list->alloc_len); 3514 break; 3515 } 3516 3517 sbuf_finish(sb); 3518 3519 retval = copyout(sbuf_data(sb), list->lun_xml, 3520 sbuf_len(sb) + 1); 3521 3522 list->fill_len = sbuf_len(sb) + 1; 3523 list->status = CTL_LUN_LIST_OK; 3524 sbuf_delete(sb); 3525 break; 3526 } 3527 case CTL_LUN_MAP: { 3528 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3529 struct ctl_port *port; 3530 3531 mtx_lock(&softc->ctl_lock); 3532 if (lm->port >= CTL_MAX_PORTS || 3533 (port = softc->ctl_ports[lm->port]) == NULL) { 3534 mtx_unlock(&softc->ctl_lock); 3535 return (ENXIO); 3536 } 3537 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3538 if (lm->plun < CTL_MAX_LUNS) { 3539 if (lm->lun == UINT32_MAX) 3540 retval = ctl_lun_map_unset(port, lm->plun); 3541 else if (lm->lun < CTL_MAX_LUNS && 3542 softc->ctl_luns[lm->lun] != NULL) 3543 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3544 else 3545 return (ENXIO); 3546 } else if (lm->plun == UINT32_MAX) { 3547 if (lm->lun == UINT32_MAX) 3548 retval = ctl_lun_map_deinit(port); 3549 else 3550 retval = ctl_lun_map_init(port); 3551 } else 3552 return (ENXIO); 3553 break; 3554 } 3555 default: { 3556 /* XXX KDM should we fix this? */ 3557#if 0 3558 struct ctl_backend_driver *backend; 3559 unsigned int type; 3560 int found; 3561 3562 found = 0; 3563 3564 /* 3565 * We encode the backend type as the ioctl type for backend 3566 * ioctls. So parse it out here, and then search for a 3567 * backend of this type. 3568 */ 3569 type = _IOC_TYPE(cmd); 3570 3571 STAILQ_FOREACH(backend, &softc->be_list, links) { 3572 if (backend->type == type) { 3573 found = 1; 3574 break; 3575 } 3576 } 3577 if (found == 0) { 3578 printf("ctl: unknown ioctl command %#lx or backend " 3579 "%d\n", cmd, type); 3580 retval = EINVAL; 3581 break; 3582 } 3583 retval = backend->ioctl(dev, cmd, addr, flag, td); 3584#endif 3585 retval = ENOTTY; 3586 break; 3587 } 3588 } 3589 return (retval); 3590} 3591 3592uint32_t 3593ctl_get_initindex(struct ctl_nexus *nexus) 3594{ 3595 if (nexus->targ_port < CTL_MAX_PORTS) 3596 return (nexus->initid.id + 3597 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3598 else 3599 return (nexus->initid.id + 3600 ((nexus->targ_port - CTL_MAX_PORTS) * 3601 CTL_MAX_INIT_PER_PORT)); 3602} 3603 3604uint32_t 3605ctl_get_resindex(struct ctl_nexus *nexus) 3606{ 3607 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3608} 3609 3610uint32_t 3611ctl_port_idx(int port_num) 3612{ 3613 if (port_num < CTL_MAX_PORTS) 3614 return(port_num); 3615 else 3616 return(port_num - CTL_MAX_PORTS); 3617} 3618 3619int 3620ctl_lun_map_init(struct ctl_port *port) 3621{ 3622 struct ctl_softc *softc = control_softc; 3623 struct ctl_lun *lun; 3624 uint32_t i; 3625 3626 if (port->lun_map == NULL) 3627 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3628 M_CTL, M_NOWAIT); 3629 if (port->lun_map == NULL) 3630 return (ENOMEM); 3631 for (i = 0; i < CTL_MAX_LUNS; i++) 3632 port->lun_map[i] = UINT32_MAX; 3633 if (port->status & CTL_PORT_STATUS_ONLINE) { 3634 STAILQ_FOREACH(lun, &softc->lun_list, links) 3635 port->lun_disable(port->targ_lun_arg, lun->lun); 3636 } 3637 return (0); 3638} 3639 3640int 3641ctl_lun_map_deinit(struct ctl_port *port) 3642{ 3643 struct ctl_softc *softc = control_softc; 3644 struct ctl_lun *lun; 3645 3646 if (port->lun_map == NULL) 3647 return (0); 3648 free(port->lun_map, M_CTL); 3649 port->lun_map = NULL; 3650 if (port->status & CTL_PORT_STATUS_ONLINE) { 3651 STAILQ_FOREACH(lun, &softc->lun_list, links) 3652 port->lun_enable(port->targ_lun_arg, lun->lun); 3653 } 3654 return (0); 3655} 3656 3657int 3658ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3659{ 3660 int status; 3661 uint32_t old; 3662 3663 if (port->lun_map == NULL) { 3664 status = ctl_lun_map_init(port); 3665 if (status != 0) 3666 return (status); 3667 } 3668 old = port->lun_map[plun]; 3669 port->lun_map[plun] = glun; 3670 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) 3671 port->lun_enable(port->targ_lun_arg, plun); 3672 return (0); 3673} 3674 3675int 3676ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3677{ 3678 uint32_t old; 3679 3680 if (port->lun_map == NULL) 3681 return (0); 3682 old = port->lun_map[plun]; 3683 port->lun_map[plun] = UINT32_MAX; 3684 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) 3685 port->lun_disable(port->targ_lun_arg, plun); 3686 return (0); 3687} 3688 3689uint32_t 3690ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3691{ 3692 3693 if (port == NULL) 3694 return (UINT32_MAX); 3695 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3696 return (lun_id); 3697 return (port->lun_map[lun_id]); 3698} 3699 3700uint32_t 3701ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3702{ 3703 uint32_t i; 3704 3705 if (port == NULL) 3706 return (UINT32_MAX); 3707 if (port->lun_map == NULL) 3708 return (lun_id); 3709 for (i = 0; i < CTL_MAX_LUNS; i++) { 3710 if (port->lun_map[i] == lun_id) 3711 return (i); 3712 } 3713 return (UINT32_MAX); 3714} 3715 3716static struct ctl_port * 3717ctl_io_port(struct ctl_io_hdr *io_hdr) 3718{ 3719 int port_num; 3720 3721 port_num = io_hdr->nexus.targ_port; 3722 return (control_softc->ctl_ports[ctl_port_idx(port_num)]); 3723} 3724 3725/* 3726 * Note: This only works for bitmask sizes that are at least 32 bits, and 3727 * that are a power of 2. 3728 */ 3729int 3730ctl_ffz(uint32_t *mask, uint32_t size) 3731{ 3732 uint32_t num_chunks, num_pieces; 3733 int i, j; 3734 3735 num_chunks = (size >> 5); 3736 if (num_chunks == 0) 3737 num_chunks++; 3738 num_pieces = MIN((sizeof(uint32_t) * 8), size); 3739 3740 for (i = 0; i < num_chunks; i++) { 3741 for (j = 0; j < num_pieces; j++) { 3742 if ((mask[i] & (1 << j)) == 0) 3743 return ((i << 5) + j); 3744 } 3745 } 3746 3747 return (-1); 3748} 3749 3750int 3751ctl_set_mask(uint32_t *mask, uint32_t bit) 3752{ 3753 uint32_t chunk, piece; 3754 3755 chunk = bit >> 5; 3756 piece = bit % (sizeof(uint32_t) * 8); 3757 3758 if ((mask[chunk] & (1 << piece)) != 0) 3759 return (-1); 3760 else 3761 mask[chunk] |= (1 << piece); 3762 3763 return (0); 3764} 3765 3766int 3767ctl_clear_mask(uint32_t *mask, uint32_t bit) 3768{ 3769 uint32_t chunk, piece; 3770 3771 chunk = bit >> 5; 3772 piece = bit % (sizeof(uint32_t) * 8); 3773 3774 if ((mask[chunk] & (1 << piece)) == 0) 3775 return (-1); 3776 else 3777 mask[chunk] &= ~(1 << piece); 3778 3779 return (0); 3780} 3781 3782int 3783ctl_is_set(uint32_t *mask, uint32_t bit) 3784{ 3785 uint32_t chunk, piece; 3786 3787 chunk = bit >> 5; 3788 piece = bit % (sizeof(uint32_t) * 8); 3789 3790 if ((mask[chunk] & (1 << piece)) == 0) 3791 return (0); 3792 else 3793 return (1); 3794} 3795 3796static uint64_t 3797ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3798{ 3799 uint64_t *t; 3800 3801 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3802 if (t == NULL) 3803 return (0); 3804 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3805} 3806 3807static void 3808ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3809{ 3810 uint64_t *t; 3811 3812 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3813 if (t == NULL) 3814 return; 3815 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3816} 3817 3818static void 3819ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3820{ 3821 uint64_t *p; 3822 u_int i; 3823 3824 i = residx/CTL_MAX_INIT_PER_PORT; 3825 if (lun->pr_keys[i] != NULL) 3826 return; 3827 mtx_unlock(&lun->lun_lock); 3828 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3829 M_WAITOK | M_ZERO); 3830 mtx_lock(&lun->lun_lock); 3831 if (lun->pr_keys[i] == NULL) 3832 lun->pr_keys[i] = p; 3833 else 3834 free(p, M_CTL); 3835} 3836 3837static void 3838ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3839{ 3840 uint64_t *t; 3841 3842 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3843 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3844 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3845} 3846 3847/* 3848 * ctl_softc, pool_name, total_ctl_io are passed in. 3849 * npool is passed out. 3850 */ 3851int 3852ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3853 uint32_t total_ctl_io, void **npool) 3854{ 3855#ifdef IO_POOLS 3856 struct ctl_io_pool *pool; 3857 3858 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3859 M_NOWAIT | M_ZERO); 3860 if (pool == NULL) 3861 return (ENOMEM); 3862 3863 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3864 pool->ctl_softc = ctl_softc; 3865 pool->zone = uma_zsecond_create(pool->name, NULL, 3866 NULL, NULL, NULL, ctl_softc->io_zone); 3867 /* uma_prealloc(pool->zone, total_ctl_io); */ 3868 3869 *npool = pool; 3870#else 3871 *npool = ctl_softc->io_zone; 3872#endif 3873 return (0); 3874} 3875 3876void 3877ctl_pool_free(struct ctl_io_pool *pool) 3878{ 3879 3880 if (pool == NULL) 3881 return; 3882 3883#ifdef IO_POOLS 3884 uma_zdestroy(pool->zone); 3885 free(pool, M_CTL); 3886#endif 3887} 3888 3889union ctl_io * 3890ctl_alloc_io(void *pool_ref) 3891{ 3892 union ctl_io *io; 3893#ifdef IO_POOLS 3894 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3895 3896 io = uma_zalloc(pool->zone, M_WAITOK); 3897#else 3898 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3899#endif 3900 if (io != NULL) 3901 io->io_hdr.pool = pool_ref; 3902 return (io); 3903} 3904 3905union ctl_io * 3906ctl_alloc_io_nowait(void *pool_ref) 3907{ 3908 union ctl_io *io; 3909#ifdef IO_POOLS 3910 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3911 3912 io = uma_zalloc(pool->zone, M_NOWAIT); 3913#else 3914 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3915#endif 3916 if (io != NULL) 3917 io->io_hdr.pool = pool_ref; 3918 return (io); 3919} 3920 3921void 3922ctl_free_io(union ctl_io *io) 3923{ 3924#ifdef IO_POOLS 3925 struct ctl_io_pool *pool; 3926#endif 3927 3928 if (io == NULL) 3929 return; 3930 3931#ifdef IO_POOLS 3932 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3933 uma_zfree(pool->zone, io); 3934#else 3935 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3936#endif 3937} 3938 3939void 3940ctl_zero_io(union ctl_io *io) 3941{ 3942 void *pool_ref; 3943 3944 if (io == NULL) 3945 return; 3946 3947 /* 3948 * May need to preserve linked list pointers at some point too. 3949 */ 3950 pool_ref = io->io_hdr.pool; 3951 memset(io, 0, sizeof(*io)); 3952 io->io_hdr.pool = pool_ref; 3953} 3954 3955/* 3956 * This routine is currently used for internal copies of ctl_ios that need 3957 * to persist for some reason after we've already returned status to the 3958 * FETD. (Thus the flag set.) 3959 * 3960 * XXX XXX 3961 * Note that this makes a blind copy of all fields in the ctl_io, except 3962 * for the pool reference. This includes any memory that has been 3963 * allocated! That memory will no longer be valid after done has been 3964 * called, so this would be VERY DANGEROUS for command that actually does 3965 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3966 * start and stop commands, which don't transfer any data, so this is not a 3967 * problem. If it is used for anything else, the caller would also need to 3968 * allocate data buffer space and this routine would need to be modified to 3969 * copy the data buffer(s) as well. 3970 */ 3971void 3972ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3973{ 3974 void *pool_ref; 3975 3976 if ((src == NULL) 3977 || (dest == NULL)) 3978 return; 3979 3980 /* 3981 * May need to preserve linked list pointers at some point too. 3982 */ 3983 pool_ref = dest->io_hdr.pool; 3984 3985 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3986 3987 dest->io_hdr.pool = pool_ref; 3988 /* 3989 * We need to know that this is an internal copy, and doesn't need 3990 * to get passed back to the FETD that allocated it. 3991 */ 3992 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3993} 3994 3995int 3996ctl_expand_number(const char *buf, uint64_t *num) 3997{ 3998 char *endptr; 3999 uint64_t number; 4000 unsigned shift; 4001 4002 number = strtoq(buf, &endptr, 0); 4003 4004 switch (tolower((unsigned char)*endptr)) { 4005 case 'e': 4006 shift = 60; 4007 break; 4008 case 'p': 4009 shift = 50; 4010 break; 4011 case 't': 4012 shift = 40; 4013 break; 4014 case 'g': 4015 shift = 30; 4016 break; 4017 case 'm': 4018 shift = 20; 4019 break; 4020 case 'k': 4021 shift = 10; 4022 break; 4023 case 'b': 4024 case '\0': /* No unit. */ 4025 *num = number; 4026 return (0); 4027 default: 4028 /* Unrecognized unit. */ 4029 return (-1); 4030 } 4031 4032 if ((number << shift) >> shift != number) { 4033 /* Overflow */ 4034 return (-1); 4035 } 4036 *num = number << shift; 4037 return (0); 4038} 4039 4040 4041/* 4042 * This routine could be used in the future to load default and/or saved 4043 * mode page parameters for a particuar lun. 4044 */ 4045static int 4046ctl_init_page_index(struct ctl_lun *lun) 4047{ 4048 int i; 4049 struct ctl_page_index *page_index; 4050 const char *value; 4051 uint64_t ival; 4052 4053 memcpy(&lun->mode_pages.index, page_index_template, 4054 sizeof(page_index_template)); 4055 4056 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4057 4058 page_index = &lun->mode_pages.index[i]; 4059 /* 4060 * If this is a disk-only mode page, there's no point in 4061 * setting it up. For some pages, we have to have some 4062 * basic information about the disk in order to calculate the 4063 * mode page data. 4064 */ 4065 if ((lun->be_lun->lun_type != T_DIRECT) 4066 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4067 continue; 4068 4069 switch (page_index->page_code & SMPH_PC_MASK) { 4070 case SMS_RW_ERROR_RECOVERY_PAGE: { 4071 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4072 panic("subpage is incorrect!"); 4073 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4074 &rw_er_page_default, 4075 sizeof(rw_er_page_default)); 4076 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4077 &rw_er_page_changeable, 4078 sizeof(rw_er_page_changeable)); 4079 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4080 &rw_er_page_default, 4081 sizeof(rw_er_page_default)); 4082 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4083 &rw_er_page_default, 4084 sizeof(rw_er_page_default)); 4085 page_index->page_data = 4086 (uint8_t *)lun->mode_pages.rw_er_page; 4087 break; 4088 } 4089 case SMS_FORMAT_DEVICE_PAGE: { 4090 struct scsi_format_page *format_page; 4091 4092 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4093 panic("subpage is incorrect!"); 4094 4095 /* 4096 * Sectors per track are set above. Bytes per 4097 * sector need to be set here on a per-LUN basis. 4098 */ 4099 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4100 &format_page_default, 4101 sizeof(format_page_default)); 4102 memcpy(&lun->mode_pages.format_page[ 4103 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4104 sizeof(format_page_changeable)); 4105 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4106 &format_page_default, 4107 sizeof(format_page_default)); 4108 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4109 &format_page_default, 4110 sizeof(format_page_default)); 4111 4112 format_page = &lun->mode_pages.format_page[ 4113 CTL_PAGE_CURRENT]; 4114 scsi_ulto2b(lun->be_lun->blocksize, 4115 format_page->bytes_per_sector); 4116 4117 format_page = &lun->mode_pages.format_page[ 4118 CTL_PAGE_DEFAULT]; 4119 scsi_ulto2b(lun->be_lun->blocksize, 4120 format_page->bytes_per_sector); 4121 4122 format_page = &lun->mode_pages.format_page[ 4123 CTL_PAGE_SAVED]; 4124 scsi_ulto2b(lun->be_lun->blocksize, 4125 format_page->bytes_per_sector); 4126 4127 page_index->page_data = 4128 (uint8_t *)lun->mode_pages.format_page; 4129 break; 4130 } 4131 case SMS_RIGID_DISK_PAGE: { 4132 struct scsi_rigid_disk_page *rigid_disk_page; 4133 uint32_t sectors_per_cylinder; 4134 uint64_t cylinders; 4135#ifndef __XSCALE__ 4136 int shift; 4137#endif /* !__XSCALE__ */ 4138 4139 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4140 panic("invalid subpage value %d", 4141 page_index->subpage); 4142 4143 /* 4144 * Rotation rate and sectors per track are set 4145 * above. We calculate the cylinders here based on 4146 * capacity. Due to the number of heads and 4147 * sectors per track we're using, smaller arrays 4148 * may turn out to have 0 cylinders. Linux and 4149 * FreeBSD don't pay attention to these mode pages 4150 * to figure out capacity, but Solaris does. It 4151 * seems to deal with 0 cylinders just fine, and 4152 * works out a fake geometry based on the capacity. 4153 */ 4154 memcpy(&lun->mode_pages.rigid_disk_page[ 4155 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4156 sizeof(rigid_disk_page_default)); 4157 memcpy(&lun->mode_pages.rigid_disk_page[ 4158 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4159 sizeof(rigid_disk_page_changeable)); 4160 4161 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4162 CTL_DEFAULT_HEADS; 4163 4164 /* 4165 * The divide method here will be more accurate, 4166 * probably, but results in floating point being 4167 * used in the kernel on i386 (__udivdi3()). On the 4168 * XScale, though, __udivdi3() is implemented in 4169 * software. 4170 * 4171 * The shift method for cylinder calculation is 4172 * accurate if sectors_per_cylinder is a power of 4173 * 2. Otherwise it might be slightly off -- you 4174 * might have a bit of a truncation problem. 4175 */ 4176#ifdef __XSCALE__ 4177 cylinders = (lun->be_lun->maxlba + 1) / 4178 sectors_per_cylinder; 4179#else 4180 for (shift = 31; shift > 0; shift--) { 4181 if (sectors_per_cylinder & (1 << shift)) 4182 break; 4183 } 4184 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4185#endif 4186 4187 /* 4188 * We've basically got 3 bytes, or 24 bits for the 4189 * cylinder size in the mode page. If we're over, 4190 * just round down to 2^24. 4191 */ 4192 if (cylinders > 0xffffff) 4193 cylinders = 0xffffff; 4194 4195 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4196 CTL_PAGE_DEFAULT]; 4197 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4198 4199 if ((value = ctl_get_opt(&lun->be_lun->options, 4200 "rpm")) != NULL) { 4201 scsi_ulto2b(strtol(value, NULL, 0), 4202 rigid_disk_page->rotation_rate); 4203 } 4204 4205 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4206 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4207 sizeof(rigid_disk_page_default)); 4208 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4209 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4210 sizeof(rigid_disk_page_default)); 4211 4212 page_index->page_data = 4213 (uint8_t *)lun->mode_pages.rigid_disk_page; 4214 break; 4215 } 4216 case SMS_CACHING_PAGE: { 4217 struct scsi_caching_page *caching_page; 4218 4219 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4220 panic("invalid subpage value %d", 4221 page_index->subpage); 4222 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4223 &caching_page_default, 4224 sizeof(caching_page_default)); 4225 memcpy(&lun->mode_pages.caching_page[ 4226 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4227 sizeof(caching_page_changeable)); 4228 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4229 &caching_page_default, 4230 sizeof(caching_page_default)); 4231 caching_page = &lun->mode_pages.caching_page[ 4232 CTL_PAGE_SAVED]; 4233 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4234 if (value != NULL && strcmp(value, "off") == 0) 4235 caching_page->flags1 &= ~SCP_WCE; 4236 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4237 if (value != NULL && strcmp(value, "off") == 0) 4238 caching_page->flags1 |= SCP_RCD; 4239 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4240 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4241 sizeof(caching_page_default)); 4242 page_index->page_data = 4243 (uint8_t *)lun->mode_pages.caching_page; 4244 break; 4245 } 4246 case SMS_CONTROL_MODE_PAGE: { 4247 struct scsi_control_page *control_page; 4248 4249 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4250 panic("invalid subpage value %d", 4251 page_index->subpage); 4252 4253 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4254 &control_page_default, 4255 sizeof(control_page_default)); 4256 memcpy(&lun->mode_pages.control_page[ 4257 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4258 sizeof(control_page_changeable)); 4259 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4260 &control_page_default, 4261 sizeof(control_page_default)); 4262 control_page = &lun->mode_pages.control_page[ 4263 CTL_PAGE_SAVED]; 4264 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4265 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4266 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4267 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4268 } 4269 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4270 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4271 sizeof(control_page_default)); 4272 page_index->page_data = 4273 (uint8_t *)lun->mode_pages.control_page; 4274 break; 4275 4276 } 4277 case SMS_INFO_EXCEPTIONS_PAGE: { 4278 switch (page_index->subpage) { 4279 case SMS_SUBPAGE_PAGE_0: 4280 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4281 &ie_page_default, 4282 sizeof(ie_page_default)); 4283 memcpy(&lun->mode_pages.ie_page[ 4284 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4285 sizeof(ie_page_changeable)); 4286 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4287 &ie_page_default, 4288 sizeof(ie_page_default)); 4289 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4290 &ie_page_default, 4291 sizeof(ie_page_default)); 4292 page_index->page_data = 4293 (uint8_t *)lun->mode_pages.ie_page; 4294 break; 4295 case 0x02: { 4296 struct ctl_logical_block_provisioning_page *page; 4297 4298 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4299 &lbp_page_default, 4300 sizeof(lbp_page_default)); 4301 memcpy(&lun->mode_pages.lbp_page[ 4302 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4303 sizeof(lbp_page_changeable)); 4304 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4305 &lbp_page_default, 4306 sizeof(lbp_page_default)); 4307 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4308 value = ctl_get_opt(&lun->be_lun->options, 4309 "avail-threshold"); 4310 if (value != NULL && 4311 ctl_expand_number(value, &ival) == 0) { 4312 page->descr[0].flags |= SLBPPD_ENABLED | 4313 SLBPPD_ARMING_DEC; 4314 if (lun->be_lun->blocksize) 4315 ival /= lun->be_lun->blocksize; 4316 else 4317 ival /= 512; 4318 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4319 page->descr[0].count); 4320 } 4321 value = ctl_get_opt(&lun->be_lun->options, 4322 "used-threshold"); 4323 if (value != NULL && 4324 ctl_expand_number(value, &ival) == 0) { 4325 page->descr[1].flags |= SLBPPD_ENABLED | 4326 SLBPPD_ARMING_INC; 4327 if (lun->be_lun->blocksize) 4328 ival /= lun->be_lun->blocksize; 4329 else 4330 ival /= 512; 4331 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4332 page->descr[1].count); 4333 } 4334 value = ctl_get_opt(&lun->be_lun->options, 4335 "pool-avail-threshold"); 4336 if (value != NULL && 4337 ctl_expand_number(value, &ival) == 0) { 4338 page->descr[2].flags |= SLBPPD_ENABLED | 4339 SLBPPD_ARMING_DEC; 4340 if (lun->be_lun->blocksize) 4341 ival /= lun->be_lun->blocksize; 4342 else 4343 ival /= 512; 4344 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4345 page->descr[2].count); 4346 } 4347 value = ctl_get_opt(&lun->be_lun->options, 4348 "pool-used-threshold"); 4349 if (value != NULL && 4350 ctl_expand_number(value, &ival) == 0) { 4351 page->descr[3].flags |= SLBPPD_ENABLED | 4352 SLBPPD_ARMING_INC; 4353 if (lun->be_lun->blocksize) 4354 ival /= lun->be_lun->blocksize; 4355 else 4356 ival /= 512; 4357 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4358 page->descr[3].count); 4359 } 4360 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4361 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4362 sizeof(lbp_page_default)); 4363 page_index->page_data = 4364 (uint8_t *)lun->mode_pages.lbp_page; 4365 }} 4366 break; 4367 } 4368 case SMS_VENDOR_SPECIFIC_PAGE:{ 4369 switch (page_index->subpage) { 4370 case DBGCNF_SUBPAGE_CODE: { 4371 struct copan_debugconf_subpage *current_page, 4372 *saved_page; 4373 4374 memcpy(&lun->mode_pages.debugconf_subpage[ 4375 CTL_PAGE_CURRENT], 4376 &debugconf_page_default, 4377 sizeof(debugconf_page_default)); 4378 memcpy(&lun->mode_pages.debugconf_subpage[ 4379 CTL_PAGE_CHANGEABLE], 4380 &debugconf_page_changeable, 4381 sizeof(debugconf_page_changeable)); 4382 memcpy(&lun->mode_pages.debugconf_subpage[ 4383 CTL_PAGE_DEFAULT], 4384 &debugconf_page_default, 4385 sizeof(debugconf_page_default)); 4386 memcpy(&lun->mode_pages.debugconf_subpage[ 4387 CTL_PAGE_SAVED], 4388 &debugconf_page_default, 4389 sizeof(debugconf_page_default)); 4390 page_index->page_data = 4391 (uint8_t *)lun->mode_pages.debugconf_subpage; 4392 4393 current_page = (struct copan_debugconf_subpage *) 4394 (page_index->page_data + 4395 (page_index->page_len * 4396 CTL_PAGE_CURRENT)); 4397 saved_page = (struct copan_debugconf_subpage *) 4398 (page_index->page_data + 4399 (page_index->page_len * 4400 CTL_PAGE_SAVED)); 4401 break; 4402 } 4403 default: 4404 panic("invalid subpage value %d", 4405 page_index->subpage); 4406 break; 4407 } 4408 break; 4409 } 4410 default: 4411 panic("invalid page value %d", 4412 page_index->page_code & SMPH_PC_MASK); 4413 break; 4414 } 4415 } 4416 4417 return (CTL_RETVAL_COMPLETE); 4418} 4419 4420static int 4421ctl_init_log_page_index(struct ctl_lun *lun) 4422{ 4423 struct ctl_page_index *page_index; 4424 int i, j, k, prev; 4425 4426 memcpy(&lun->log_pages.index, log_page_index_template, 4427 sizeof(log_page_index_template)); 4428 4429 prev = -1; 4430 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4431 4432 page_index = &lun->log_pages.index[i]; 4433 /* 4434 * If this is a disk-only mode page, there's no point in 4435 * setting it up. For some pages, we have to have some 4436 * basic information about the disk in order to calculate the 4437 * mode page data. 4438 */ 4439 if ((lun->be_lun->lun_type != T_DIRECT) 4440 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4441 continue; 4442 4443 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4444 lun->backend->lun_attr == NULL) 4445 continue; 4446 4447 if (page_index->page_code != prev) { 4448 lun->log_pages.pages_page[j] = page_index->page_code; 4449 prev = page_index->page_code; 4450 j++; 4451 } 4452 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4453 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4454 k++; 4455 } 4456 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4457 lun->log_pages.index[0].page_len = j; 4458 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4459 lun->log_pages.index[1].page_len = k * 2; 4460 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4461 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4462 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4463 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4464 4465 return (CTL_RETVAL_COMPLETE); 4466} 4467 4468static int 4469hex2bin(const char *str, uint8_t *buf, int buf_size) 4470{ 4471 int i; 4472 u_char c; 4473 4474 memset(buf, 0, buf_size); 4475 while (isspace(str[0])) 4476 str++; 4477 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4478 str += 2; 4479 buf_size *= 2; 4480 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4481 c = str[i]; 4482 if (isdigit(c)) 4483 c -= '0'; 4484 else if (isalpha(c)) 4485 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4486 else 4487 break; 4488 if (c >= 16) 4489 break; 4490 if ((i & 1) == 0) 4491 buf[i / 2] |= (c << 4); 4492 else 4493 buf[i / 2] |= c; 4494 } 4495 return ((i + 1) / 2); 4496} 4497 4498/* 4499 * LUN allocation. 4500 * 4501 * Requirements: 4502 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4503 * wants us to allocate the LUN and he can block. 4504 * - ctl_softc is always set 4505 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4506 * 4507 * Returns 0 for success, non-zero (errno) for failure. 4508 */ 4509static int 4510ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4511 struct ctl_be_lun *const be_lun) 4512{ 4513 struct ctl_lun *nlun, *lun; 4514 struct scsi_vpd_id_descriptor *desc; 4515 struct scsi_vpd_id_t10 *t10id; 4516 const char *eui, *naa, *scsiname, *vendor, *value; 4517 int lun_number, i, lun_malloced; 4518 int devidlen, idlen1, idlen2 = 0, len; 4519 4520 if (be_lun == NULL) 4521 return (EINVAL); 4522 4523 /* 4524 * We currently only support Direct Access or Processor LUN types. 4525 */ 4526 switch (be_lun->lun_type) { 4527 case T_DIRECT: 4528 break; 4529 case T_PROCESSOR: 4530 break; 4531 case T_SEQUENTIAL: 4532 case T_CHANGER: 4533 default: 4534 be_lun->lun_config_status(be_lun->be_lun, 4535 CTL_LUN_CONFIG_FAILURE); 4536 break; 4537 } 4538 if (ctl_lun == NULL) { 4539 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4540 lun_malloced = 1; 4541 } else { 4542 lun_malloced = 0; 4543 lun = ctl_lun; 4544 } 4545 4546 memset(lun, 0, sizeof(*lun)); 4547 if (lun_malloced) 4548 lun->flags = CTL_LUN_MALLOCED; 4549 4550 /* Generate LUN ID. */ 4551 devidlen = max(CTL_DEVID_MIN_LEN, 4552 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4553 idlen1 = sizeof(*t10id) + devidlen; 4554 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4555 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4556 if (scsiname != NULL) { 4557 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4558 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4559 } 4560 eui = ctl_get_opt(&be_lun->options, "eui"); 4561 if (eui != NULL) { 4562 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4563 } 4564 naa = ctl_get_opt(&be_lun->options, "naa"); 4565 if (naa != NULL) { 4566 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4567 } 4568 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4569 M_CTL, M_WAITOK | M_ZERO); 4570 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4571 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4572 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4573 desc->length = idlen1; 4574 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4575 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4576 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4577 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4578 } else { 4579 strncpy(t10id->vendor, vendor, 4580 min(sizeof(t10id->vendor), strlen(vendor))); 4581 } 4582 strncpy((char *)t10id->vendor_spec_id, 4583 (char *)be_lun->device_id, devidlen); 4584 if (scsiname != NULL) { 4585 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4586 desc->length); 4587 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4588 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4589 SVPD_ID_TYPE_SCSI_NAME; 4590 desc->length = idlen2; 4591 strlcpy(desc->identifier, scsiname, idlen2); 4592 } 4593 if (eui != NULL) { 4594 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4595 desc->length); 4596 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4597 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4598 SVPD_ID_TYPE_EUI64; 4599 desc->length = hex2bin(eui, desc->identifier, 16); 4600 desc->length = desc->length > 12 ? 16 : 4601 (desc->length > 8 ? 12 : 8); 4602 len -= 16 - desc->length; 4603 } 4604 if (naa != NULL) { 4605 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4606 desc->length); 4607 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4608 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4609 SVPD_ID_TYPE_NAA; 4610 desc->length = hex2bin(naa, desc->identifier, 16); 4611 desc->length = desc->length > 8 ? 16 : 8; 4612 len -= 16 - desc->length; 4613 } 4614 lun->lun_devid->len = len; 4615 4616 mtx_lock(&ctl_softc->ctl_lock); 4617 /* 4618 * See if the caller requested a particular LUN number. If so, see 4619 * if it is available. Otherwise, allocate the first available LUN. 4620 */ 4621 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4622 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4623 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4624 mtx_unlock(&ctl_softc->ctl_lock); 4625 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4626 printf("ctl: requested LUN ID %d is higher " 4627 "than CTL_MAX_LUNS - 1 (%d)\n", 4628 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4629 } else { 4630 /* 4631 * XXX KDM return an error, or just assign 4632 * another LUN ID in this case?? 4633 */ 4634 printf("ctl: requested LUN ID %d is already " 4635 "in use\n", be_lun->req_lun_id); 4636 } 4637 if (lun->flags & CTL_LUN_MALLOCED) 4638 free(lun, M_CTL); 4639 be_lun->lun_config_status(be_lun->be_lun, 4640 CTL_LUN_CONFIG_FAILURE); 4641 return (ENOSPC); 4642 } 4643 lun_number = be_lun->req_lun_id; 4644 } else { 4645 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4646 if (lun_number == -1) { 4647 mtx_unlock(&ctl_softc->ctl_lock); 4648 printf("ctl: can't allocate LUN, out of LUNs\n"); 4649 if (lun->flags & CTL_LUN_MALLOCED) 4650 free(lun, M_CTL); 4651 be_lun->lun_config_status(be_lun->be_lun, 4652 CTL_LUN_CONFIG_FAILURE); 4653 return (ENOSPC); 4654 } 4655 } 4656 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4657 4658 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4659 lun->lun = lun_number; 4660 lun->be_lun = be_lun; 4661 /* 4662 * The processor LUN is always enabled. Disk LUNs come on line 4663 * disabled, and must be enabled by the backend. 4664 */ 4665 lun->flags |= CTL_LUN_DISABLED; 4666 lun->backend = be_lun->be; 4667 be_lun->ctl_lun = lun; 4668 be_lun->lun_id = lun_number; 4669 atomic_add_int(&be_lun->be->num_luns, 1); 4670 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4671 lun->flags |= CTL_LUN_OFFLINE; 4672 4673 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4674 lun->flags |= CTL_LUN_STOPPED; 4675 4676 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4677 lun->flags |= CTL_LUN_INOPERABLE; 4678 4679 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4680 lun->flags |= CTL_LUN_PRIMARY_SC; 4681 4682 value = ctl_get_opt(&be_lun->options, "readonly"); 4683 if (value != NULL && strcmp(value, "on") == 0) 4684 lun->flags |= CTL_LUN_READONLY; 4685 4686 lun->serseq = CTL_LUN_SERSEQ_OFF; 4687 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ) 4688 lun->serseq = CTL_LUN_SERSEQ_READ; 4689 value = ctl_get_opt(&be_lun->options, "serseq"); 4690 if (value != NULL && strcmp(value, "on") == 0) 4691 lun->serseq = CTL_LUN_SERSEQ_ON; 4692 else if (value != NULL && strcmp(value, "read") == 0) 4693 lun->serseq = CTL_LUN_SERSEQ_READ; 4694 else if (value != NULL && strcmp(value, "off") == 0) 4695 lun->serseq = CTL_LUN_SERSEQ_OFF; 4696 4697 lun->ctl_softc = ctl_softc; 4698#ifdef CTL_TIME_IO 4699 lun->last_busy = getsbinuptime(); 4700#endif 4701 TAILQ_INIT(&lun->ooa_queue); 4702 TAILQ_INIT(&lun->blocked_queue); 4703 STAILQ_INIT(&lun->error_list); 4704 ctl_tpc_lun_init(lun); 4705 4706 /* 4707 * Initialize the mode and log page index. 4708 */ 4709 ctl_init_page_index(lun); 4710 ctl_init_log_page_index(lun); 4711 4712 /* 4713 * Now, before we insert this lun on the lun list, set the lun 4714 * inventory changed UA for all other luns. 4715 */ 4716 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4717 mtx_lock(&nlun->lun_lock); 4718 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4719 mtx_unlock(&nlun->lun_lock); 4720 } 4721 4722 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4723 4724 ctl_softc->ctl_luns[lun_number] = lun; 4725 4726 ctl_softc->num_luns++; 4727 4728 /* Setup statistics gathering */ 4729 lun->stats.device_type = be_lun->lun_type; 4730 lun->stats.lun_number = lun_number; 4731 if (lun->stats.device_type == T_DIRECT) 4732 lun->stats.blocksize = be_lun->blocksize; 4733 else 4734 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4735 for (i = 0;i < CTL_MAX_PORTS;i++) 4736 lun->stats.ports[i].targ_port = i; 4737 4738 mtx_unlock(&ctl_softc->ctl_lock); 4739 4740 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4741 return (0); 4742} 4743 4744/* 4745 * Delete a LUN. 4746 * Assumptions: 4747 * - LUN has already been marked invalid and any pending I/O has been taken 4748 * care of. 4749 */ 4750static int 4751ctl_free_lun(struct ctl_lun *lun) 4752{ 4753 struct ctl_softc *softc; 4754 struct ctl_lun *nlun; 4755 int i; 4756 4757 softc = lun->ctl_softc; 4758 4759 mtx_assert(&softc->ctl_lock, MA_OWNED); 4760 4761 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4762 4763 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4764 4765 softc->ctl_luns[lun->lun] = NULL; 4766 4767 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4768 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4769 4770 softc->num_luns--; 4771 4772 /* 4773 * Tell the backend to free resources, if this LUN has a backend. 4774 */ 4775 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4776 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4777 4778 ctl_tpc_lun_shutdown(lun); 4779 mtx_destroy(&lun->lun_lock); 4780 free(lun->lun_devid, M_CTL); 4781 for (i = 0; i < CTL_MAX_PORTS; i++) 4782 free(lun->pending_ua[i], M_CTL); 4783 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) 4784 free(lun->pr_keys[i], M_CTL); 4785 free(lun->write_buffer, M_CTL); 4786 if (lun->flags & CTL_LUN_MALLOCED) 4787 free(lun, M_CTL); 4788 4789 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4790 mtx_lock(&nlun->lun_lock); 4791 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4792 mtx_unlock(&nlun->lun_lock); 4793 } 4794 4795 return (0); 4796} 4797 4798static void 4799ctl_create_lun(struct ctl_be_lun *be_lun) 4800{ 4801 struct ctl_softc *softc; 4802 4803 softc = control_softc; 4804 4805 /* 4806 * ctl_alloc_lun() should handle all potential failure cases. 4807 */ 4808 ctl_alloc_lun(softc, NULL, be_lun); 4809} 4810 4811int 4812ctl_add_lun(struct ctl_be_lun *be_lun) 4813{ 4814 struct ctl_softc *softc = control_softc; 4815 4816 mtx_lock(&softc->ctl_lock); 4817 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4818 mtx_unlock(&softc->ctl_lock); 4819 wakeup(&softc->pending_lun_queue); 4820 4821 return (0); 4822} 4823 4824int 4825ctl_enable_lun(struct ctl_be_lun *be_lun) 4826{ 4827 struct ctl_softc *softc; 4828 struct ctl_port *port, *nport; 4829 struct ctl_lun *lun; 4830 int retval; 4831 4832 lun = (struct ctl_lun *)be_lun->ctl_lun; 4833 softc = lun->ctl_softc; 4834 4835 mtx_lock(&softc->ctl_lock); 4836 mtx_lock(&lun->lun_lock); 4837 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4838 /* 4839 * eh? Why did we get called if the LUN is already 4840 * enabled? 4841 */ 4842 mtx_unlock(&lun->lun_lock); 4843 mtx_unlock(&softc->ctl_lock); 4844 return (0); 4845 } 4846 lun->flags &= ~CTL_LUN_DISABLED; 4847 mtx_unlock(&lun->lun_lock); 4848 4849 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4850 nport = STAILQ_NEXT(port, links); 4851 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4852 port->lun_map != NULL) 4853 continue; 4854 4855 /* 4856 * Drop the lock while we call the FETD's enable routine. 4857 * This can lead to a callback into CTL (at least in the 4858 * case of the internal initiator frontend. 4859 */ 4860 mtx_unlock(&softc->ctl_lock); 4861 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4862 mtx_lock(&softc->ctl_lock); 4863 if (retval != 0) { 4864 printf("%s: FETD %s port %d returned error " 4865 "%d for lun_enable on lun %jd\n", 4866 __func__, port->port_name, port->targ_port, 4867 retval, (intmax_t)lun->lun); 4868 } 4869 } 4870 4871 mtx_unlock(&softc->ctl_lock); 4872 4873 return (0); 4874} 4875 4876int 4877ctl_disable_lun(struct ctl_be_lun *be_lun) 4878{ 4879 struct ctl_softc *softc; 4880 struct ctl_port *port; 4881 struct ctl_lun *lun; 4882 int retval; 4883 4884 lun = (struct ctl_lun *)be_lun->ctl_lun; 4885 softc = lun->ctl_softc; 4886 4887 mtx_lock(&softc->ctl_lock); 4888 mtx_lock(&lun->lun_lock); 4889 if (lun->flags & CTL_LUN_DISABLED) { 4890 mtx_unlock(&lun->lun_lock); 4891 mtx_unlock(&softc->ctl_lock); 4892 return (0); 4893 } 4894 lun->flags |= CTL_LUN_DISABLED; 4895 mtx_unlock(&lun->lun_lock); 4896 4897 STAILQ_FOREACH(port, &softc->port_list, links) { 4898 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4899 port->lun_map != NULL) 4900 continue; 4901 mtx_unlock(&softc->ctl_lock); 4902 /* 4903 * Drop the lock before we call the frontend's disable 4904 * routine, to avoid lock order reversals. 4905 * 4906 * XXX KDM what happens if the frontend list changes while 4907 * we're traversing it? It's unlikely, but should be handled. 4908 */ 4909 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4910 mtx_lock(&softc->ctl_lock); 4911 if (retval != 0) { 4912 printf("%s: FETD %s port %d returned error " 4913 "%d for lun_disable on lun %jd\n", 4914 __func__, port->port_name, port->targ_port, 4915 retval, (intmax_t)lun->lun); 4916 } 4917 } 4918 4919 mtx_unlock(&softc->ctl_lock); 4920 4921 return (0); 4922} 4923 4924int 4925ctl_start_lun(struct ctl_be_lun *be_lun) 4926{ 4927 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4928 4929 mtx_lock(&lun->lun_lock); 4930 lun->flags &= ~CTL_LUN_STOPPED; 4931 mtx_unlock(&lun->lun_lock); 4932 return (0); 4933} 4934 4935int 4936ctl_stop_lun(struct ctl_be_lun *be_lun) 4937{ 4938 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4939 4940 mtx_lock(&lun->lun_lock); 4941 lun->flags |= CTL_LUN_STOPPED; 4942 mtx_unlock(&lun->lun_lock); 4943 return (0); 4944} 4945 4946int 4947ctl_lun_offline(struct ctl_be_lun *be_lun) 4948{ 4949 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4950 4951 mtx_lock(&lun->lun_lock); 4952 lun->flags |= CTL_LUN_OFFLINE; 4953 mtx_unlock(&lun->lun_lock); 4954 return (0); 4955} 4956 4957int 4958ctl_lun_online(struct ctl_be_lun *be_lun) 4959{ 4960 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4961 4962 mtx_lock(&lun->lun_lock); 4963 lun->flags &= ~CTL_LUN_OFFLINE; 4964 mtx_unlock(&lun->lun_lock); 4965 return (0); 4966} 4967 4968int 4969ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4970{ 4971 struct ctl_softc *softc; 4972 struct ctl_lun *lun; 4973 4974 lun = (struct ctl_lun *)be_lun->ctl_lun; 4975 softc = lun->ctl_softc; 4976 4977 mtx_lock(&lun->lun_lock); 4978 4979 /* 4980 * The LUN needs to be disabled before it can be marked invalid. 4981 */ 4982 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4983 mtx_unlock(&lun->lun_lock); 4984 return (-1); 4985 } 4986 /* 4987 * Mark the LUN invalid. 4988 */ 4989 lun->flags |= CTL_LUN_INVALID; 4990 4991 /* 4992 * If there is nothing in the OOA queue, go ahead and free the LUN. 4993 * If we have something in the OOA queue, we'll free it when the 4994 * last I/O completes. 4995 */ 4996 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4997 mtx_unlock(&lun->lun_lock); 4998 mtx_lock(&softc->ctl_lock); 4999 ctl_free_lun(lun); 5000 mtx_unlock(&softc->ctl_lock); 5001 } else 5002 mtx_unlock(&lun->lun_lock); 5003 5004 return (0); 5005} 5006 5007int 5008ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5009{ 5010 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5011 5012 mtx_lock(&lun->lun_lock); 5013 lun->flags |= CTL_LUN_INOPERABLE; 5014 mtx_unlock(&lun->lun_lock); 5015 return (0); 5016} 5017 5018int 5019ctl_lun_operable(struct ctl_be_lun *be_lun) 5020{ 5021 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5022 5023 mtx_lock(&lun->lun_lock); 5024 lun->flags &= ~CTL_LUN_INOPERABLE; 5025 mtx_unlock(&lun->lun_lock); 5026 return (0); 5027} 5028 5029void 5030ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5031{ 5032 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5033 5034 mtx_lock(&lun->lun_lock); 5035 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 5036 mtx_unlock(&lun->lun_lock); 5037} 5038 5039/* 5040 * Backend "memory move is complete" callback for requests that never 5041 * make it down to say RAIDCore's configuration code. 5042 */ 5043int 5044ctl_config_move_done(union ctl_io *io) 5045{ 5046 int retval; 5047 5048 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5049 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5050 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5051 5052 if ((io->io_hdr.port_status != 0) && 5053 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5054 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5055 /* 5056 * For hardware error sense keys, the sense key 5057 * specific value is defined to be a retry count, 5058 * but we use it to pass back an internal FETD 5059 * error code. XXX KDM Hopefully the FETD is only 5060 * using 16 bits for an error code, since that's 5061 * all the space we have in the sks field. 5062 */ 5063 ctl_set_internal_failure(&io->scsiio, 5064 /*sks_valid*/ 1, 5065 /*retry_count*/ 5066 io->io_hdr.port_status); 5067 } 5068 5069 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5070 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5071 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5072 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5073 /* 5074 * XXX KDM just assuming a single pointer here, and not a 5075 * S/G list. If we start using S/G lists for config data, 5076 * we'll need to know how to clean them up here as well. 5077 */ 5078 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5079 free(io->scsiio.kern_data_ptr, M_CTL); 5080 ctl_done(io); 5081 retval = CTL_RETVAL_COMPLETE; 5082 } else { 5083 /* 5084 * XXX KDM now we need to continue data movement. Some 5085 * options: 5086 * - call ctl_scsiio() again? We don't do this for data 5087 * writes, because for those at least we know ahead of 5088 * time where the write will go and how long it is. For 5089 * config writes, though, that information is largely 5090 * contained within the write itself, thus we need to 5091 * parse out the data again. 5092 * 5093 * - Call some other function once the data is in? 5094 */ 5095 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5096 ctl_data_print(io); 5097 5098 /* 5099 * XXX KDM call ctl_scsiio() again for now, and check flag 5100 * bits to see whether we're allocated or not. 5101 */ 5102 retval = ctl_scsiio(&io->scsiio); 5103 } 5104 return (retval); 5105} 5106 5107/* 5108 * This gets called by a backend driver when it is done with a 5109 * data_submit method. 5110 */ 5111void 5112ctl_data_submit_done(union ctl_io *io) 5113{ 5114 /* 5115 * If the IO_CONT flag is set, we need to call the supplied 5116 * function to continue processing the I/O, instead of completing 5117 * the I/O just yet. 5118 * 5119 * If there is an error, though, we don't want to keep processing. 5120 * Instead, just send status back to the initiator. 5121 */ 5122 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5123 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5124 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5125 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5126 io->scsiio.io_cont(io); 5127 return; 5128 } 5129 ctl_done(io); 5130} 5131 5132/* 5133 * This gets called by a backend driver when it is done with a 5134 * configuration write. 5135 */ 5136void 5137ctl_config_write_done(union ctl_io *io) 5138{ 5139 uint8_t *buf; 5140 5141 /* 5142 * If the IO_CONT flag is set, we need to call the supplied 5143 * function to continue processing the I/O, instead of completing 5144 * the I/O just yet. 5145 * 5146 * If there is an error, though, we don't want to keep processing. 5147 * Instead, just send status back to the initiator. 5148 */ 5149 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5150 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5151 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5152 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5153 io->scsiio.io_cont(io); 5154 return; 5155 } 5156 /* 5157 * Since a configuration write can be done for commands that actually 5158 * have data allocated, like write buffer, and commands that have 5159 * no data, like start/stop unit, we need to check here. 5160 */ 5161 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5162 buf = io->scsiio.kern_data_ptr; 5163 else 5164 buf = NULL; 5165 ctl_done(io); 5166 if (buf) 5167 free(buf, M_CTL); 5168} 5169 5170void 5171ctl_config_read_done(union ctl_io *io) 5172{ 5173 uint8_t *buf; 5174 5175 /* 5176 * If there is some error -- we are done, skip data transfer. 5177 */ 5178 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5179 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5180 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5181 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5182 buf = io->scsiio.kern_data_ptr; 5183 else 5184 buf = NULL; 5185 ctl_done(io); 5186 if (buf) 5187 free(buf, M_CTL); 5188 return; 5189 } 5190 5191 /* 5192 * If the IO_CONT flag is set, we need to call the supplied 5193 * function to continue processing the I/O, instead of completing 5194 * the I/O just yet. 5195 */ 5196 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5197 io->scsiio.io_cont(io); 5198 return; 5199 } 5200 5201 ctl_datamove(io); 5202} 5203 5204/* 5205 * SCSI release command. 5206 */ 5207int 5208ctl_scsi_release(struct ctl_scsiio *ctsio) 5209{ 5210 int length, longid, thirdparty_id, resv_id; 5211 struct ctl_lun *lun; 5212 uint32_t residx; 5213 5214 length = 0; 5215 resv_id = 0; 5216 5217 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5218 5219 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5220 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5221 5222 switch (ctsio->cdb[0]) { 5223 case RELEASE_10: { 5224 struct scsi_release_10 *cdb; 5225 5226 cdb = (struct scsi_release_10 *)ctsio->cdb; 5227 5228 if (cdb->byte2 & SR10_LONGID) 5229 longid = 1; 5230 else 5231 thirdparty_id = cdb->thirdparty_id; 5232 5233 resv_id = cdb->resv_id; 5234 length = scsi_2btoul(cdb->length); 5235 break; 5236 } 5237 } 5238 5239 5240 /* 5241 * XXX KDM right now, we only support LUN reservation. We don't 5242 * support 3rd party reservations, or extent reservations, which 5243 * might actually need the parameter list. If we've gotten this 5244 * far, we've got a LUN reservation. Anything else got kicked out 5245 * above. So, according to SPC, ignore the length. 5246 */ 5247 length = 0; 5248 5249 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5250 && (length > 0)) { 5251 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5252 ctsio->kern_data_len = length; 5253 ctsio->kern_total_len = length; 5254 ctsio->kern_data_resid = 0; 5255 ctsio->kern_rel_offset = 0; 5256 ctsio->kern_sg_entries = 0; 5257 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5258 ctsio->be_move_done = ctl_config_move_done; 5259 ctl_datamove((union ctl_io *)ctsio); 5260 5261 return (CTL_RETVAL_COMPLETE); 5262 } 5263 5264 if (length > 0) 5265 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5266 5267 mtx_lock(&lun->lun_lock); 5268 5269 /* 5270 * According to SPC, it is not an error for an intiator to attempt 5271 * to release a reservation on a LUN that isn't reserved, or that 5272 * is reserved by another initiator. The reservation can only be 5273 * released, though, by the initiator who made it or by one of 5274 * several reset type events. 5275 */ 5276 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5277 lun->flags &= ~CTL_LUN_RESERVED; 5278 5279 mtx_unlock(&lun->lun_lock); 5280 5281 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5282 free(ctsio->kern_data_ptr, M_CTL); 5283 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5284 } 5285 5286 ctl_set_success(ctsio); 5287 ctl_done((union ctl_io *)ctsio); 5288 return (CTL_RETVAL_COMPLETE); 5289} 5290 5291int 5292ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5293{ 5294 int extent, thirdparty, longid; 5295 int resv_id, length; 5296 uint64_t thirdparty_id; 5297 struct ctl_lun *lun; 5298 uint32_t residx; 5299 5300 extent = 0; 5301 thirdparty = 0; 5302 longid = 0; 5303 resv_id = 0; 5304 length = 0; 5305 thirdparty_id = 0; 5306 5307 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5308 5309 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5310 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5311 5312 switch (ctsio->cdb[0]) { 5313 case RESERVE_10: { 5314 struct scsi_reserve_10 *cdb; 5315 5316 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5317 5318 if (cdb->byte2 & SR10_LONGID) 5319 longid = 1; 5320 else 5321 thirdparty_id = cdb->thirdparty_id; 5322 5323 resv_id = cdb->resv_id; 5324 length = scsi_2btoul(cdb->length); 5325 break; 5326 } 5327 } 5328 5329 /* 5330 * XXX KDM right now, we only support LUN reservation. We don't 5331 * support 3rd party reservations, or extent reservations, which 5332 * might actually need the parameter list. If we've gotten this 5333 * far, we've got a LUN reservation. Anything else got kicked out 5334 * above. So, according to SPC, ignore the length. 5335 */ 5336 length = 0; 5337 5338 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5339 && (length > 0)) { 5340 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5341 ctsio->kern_data_len = length; 5342 ctsio->kern_total_len = length; 5343 ctsio->kern_data_resid = 0; 5344 ctsio->kern_rel_offset = 0; 5345 ctsio->kern_sg_entries = 0; 5346 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5347 ctsio->be_move_done = ctl_config_move_done; 5348 ctl_datamove((union ctl_io *)ctsio); 5349 5350 return (CTL_RETVAL_COMPLETE); 5351 } 5352 5353 if (length > 0) 5354 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5355 5356 mtx_lock(&lun->lun_lock); 5357 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5358 ctl_set_reservation_conflict(ctsio); 5359 goto bailout; 5360 } 5361 5362 lun->flags |= CTL_LUN_RESERVED; 5363 lun->res_idx = residx; 5364 5365 ctl_set_success(ctsio); 5366 5367bailout: 5368 mtx_unlock(&lun->lun_lock); 5369 5370 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5371 free(ctsio->kern_data_ptr, M_CTL); 5372 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5373 } 5374 5375 ctl_done((union ctl_io *)ctsio); 5376 return (CTL_RETVAL_COMPLETE); 5377} 5378 5379int 5380ctl_start_stop(struct ctl_scsiio *ctsio) 5381{ 5382 struct scsi_start_stop_unit *cdb; 5383 struct ctl_lun *lun; 5384 int retval; 5385 5386 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5387 5388 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5389 retval = 0; 5390 5391 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5392 5393 /* 5394 * XXX KDM 5395 * We don't support the immediate bit on a stop unit. In order to 5396 * do that, we would need to code up a way to know that a stop is 5397 * pending, and hold off any new commands until it completes, one 5398 * way or another. Then we could accept or reject those commands 5399 * depending on its status. We would almost need to do the reverse 5400 * of what we do below for an immediate start -- return the copy of 5401 * the ctl_io to the FETD with status to send to the host (and to 5402 * free the copy!) and then free the original I/O once the stop 5403 * actually completes. That way, the OOA queue mechanism can work 5404 * to block commands that shouldn't proceed. Another alternative 5405 * would be to put the copy in the queue in place of the original, 5406 * and return the original back to the caller. That could be 5407 * slightly safer.. 5408 */ 5409 if ((cdb->byte2 & SSS_IMMED) 5410 && ((cdb->how & SSS_START) == 0)) { 5411 ctl_set_invalid_field(ctsio, 5412 /*sks_valid*/ 1, 5413 /*command*/ 1, 5414 /*field*/ 1, 5415 /*bit_valid*/ 1, 5416 /*bit*/ 0); 5417 ctl_done((union ctl_io *)ctsio); 5418 return (CTL_RETVAL_COMPLETE); 5419 } 5420 5421 if ((lun->flags & CTL_LUN_PR_RESERVED) 5422 && ((cdb->how & SSS_START)==0)) { 5423 uint32_t residx; 5424 5425 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5426 if (ctl_get_prkey(lun, residx) == 0 5427 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5428 5429 ctl_set_reservation_conflict(ctsio); 5430 ctl_done((union ctl_io *)ctsio); 5431 return (CTL_RETVAL_COMPLETE); 5432 } 5433 } 5434 5435 /* 5436 * If there is no backend on this device, we can't start or stop 5437 * it. In theory we shouldn't get any start/stop commands in the 5438 * first place at this level if the LUN doesn't have a backend. 5439 * That should get stopped by the command decode code. 5440 */ 5441 if (lun->backend == NULL) { 5442 ctl_set_invalid_opcode(ctsio); 5443 ctl_done((union ctl_io *)ctsio); 5444 return (CTL_RETVAL_COMPLETE); 5445 } 5446 5447 /* 5448 * XXX KDM Copan-specific offline behavior. 5449 * Figure out a reasonable way to port this? 5450 */ 5451#ifdef NEEDTOPORT 5452 mtx_lock(&lun->lun_lock); 5453 5454 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5455 && (lun->flags & CTL_LUN_OFFLINE)) { 5456 /* 5457 * If the LUN is offline, and the on/offline bit isn't set, 5458 * reject the start or stop. Otherwise, let it through. 5459 */ 5460 mtx_unlock(&lun->lun_lock); 5461 ctl_set_lun_not_ready(ctsio); 5462 ctl_done((union ctl_io *)ctsio); 5463 } else { 5464 mtx_unlock(&lun->lun_lock); 5465#endif /* NEEDTOPORT */ 5466 /* 5467 * This could be a start or a stop when we're online, 5468 * or a stop/offline or start/online. A start or stop when 5469 * we're offline is covered in the case above. 5470 */ 5471 /* 5472 * In the non-immediate case, we send the request to 5473 * the backend and return status to the user when 5474 * it is done. 5475 * 5476 * In the immediate case, we allocate a new ctl_io 5477 * to hold a copy of the request, and send that to 5478 * the backend. We then set good status on the 5479 * user's request and return it immediately. 5480 */ 5481 if (cdb->byte2 & SSS_IMMED) { 5482 union ctl_io *new_io; 5483 5484 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5485 ctl_copy_io((union ctl_io *)ctsio, new_io); 5486 retval = lun->backend->config_write(new_io); 5487 ctl_set_success(ctsio); 5488 ctl_done((union ctl_io *)ctsio); 5489 } else { 5490 retval = lun->backend->config_write( 5491 (union ctl_io *)ctsio); 5492 } 5493#ifdef NEEDTOPORT 5494 } 5495#endif 5496 return (retval); 5497} 5498 5499/* 5500 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5501 * we don't really do anything with the LBA and length fields if the user 5502 * passes them in. Instead we'll just flush out the cache for the entire 5503 * LUN. 5504 */ 5505int 5506ctl_sync_cache(struct ctl_scsiio *ctsio) 5507{ 5508 struct ctl_lun *lun; 5509 struct ctl_softc *softc; 5510 struct ctl_lba_len_flags *lbalen; 5511 uint64_t starting_lba; 5512 uint32_t block_count; 5513 int retval; 5514 uint8_t byte2; 5515 5516 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5517 5518 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5519 softc = lun->ctl_softc; 5520 retval = 0; 5521 5522 switch (ctsio->cdb[0]) { 5523 case SYNCHRONIZE_CACHE: { 5524 struct scsi_sync_cache *cdb; 5525 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5526 5527 starting_lba = scsi_4btoul(cdb->begin_lba); 5528 block_count = scsi_2btoul(cdb->lb_count); 5529 byte2 = cdb->byte2; 5530 break; 5531 } 5532 case SYNCHRONIZE_CACHE_16: { 5533 struct scsi_sync_cache_16 *cdb; 5534 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5535 5536 starting_lba = scsi_8btou64(cdb->begin_lba); 5537 block_count = scsi_4btoul(cdb->lb_count); 5538 byte2 = cdb->byte2; 5539 break; 5540 } 5541 default: 5542 ctl_set_invalid_opcode(ctsio); 5543 ctl_done((union ctl_io *)ctsio); 5544 goto bailout; 5545 break; /* NOTREACHED */ 5546 } 5547 5548 /* 5549 * We check the LBA and length, but don't do anything with them. 5550 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5551 * get flushed. This check will just help satisfy anyone who wants 5552 * to see an error for an out of range LBA. 5553 */ 5554 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5555 ctl_set_lba_out_of_range(ctsio); 5556 ctl_done((union ctl_io *)ctsio); 5557 goto bailout; 5558 } 5559 5560 /* 5561 * If this LUN has no backend, we can't flush the cache anyway. 5562 */ 5563 if (lun->backend == NULL) { 5564 ctl_set_invalid_opcode(ctsio); 5565 ctl_done((union ctl_io *)ctsio); 5566 goto bailout; 5567 } 5568 5569 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5570 lbalen->lba = starting_lba; 5571 lbalen->len = block_count; 5572 lbalen->flags = byte2; 5573 5574 /* 5575 * Check to see whether we're configured to send the SYNCHRONIZE 5576 * CACHE command directly to the back end. 5577 */ 5578 mtx_lock(&lun->lun_lock); 5579 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5580 && (++(lun->sync_count) >= lun->sync_interval)) { 5581 lun->sync_count = 0; 5582 mtx_unlock(&lun->lun_lock); 5583 retval = lun->backend->config_write((union ctl_io *)ctsio); 5584 } else { 5585 mtx_unlock(&lun->lun_lock); 5586 ctl_set_success(ctsio); 5587 ctl_done((union ctl_io *)ctsio); 5588 } 5589 5590bailout: 5591 5592 return (retval); 5593} 5594 5595int 5596ctl_format(struct ctl_scsiio *ctsio) 5597{ 5598 struct scsi_format *cdb; 5599 struct ctl_lun *lun; 5600 int length, defect_list_len; 5601 5602 CTL_DEBUG_PRINT(("ctl_format\n")); 5603 5604 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5605 5606 cdb = (struct scsi_format *)ctsio->cdb; 5607 5608 length = 0; 5609 if (cdb->byte2 & SF_FMTDATA) { 5610 if (cdb->byte2 & SF_LONGLIST) 5611 length = sizeof(struct scsi_format_header_long); 5612 else 5613 length = sizeof(struct scsi_format_header_short); 5614 } 5615 5616 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5617 && (length > 0)) { 5618 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5619 ctsio->kern_data_len = length; 5620 ctsio->kern_total_len = length; 5621 ctsio->kern_data_resid = 0; 5622 ctsio->kern_rel_offset = 0; 5623 ctsio->kern_sg_entries = 0; 5624 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5625 ctsio->be_move_done = ctl_config_move_done; 5626 ctl_datamove((union ctl_io *)ctsio); 5627 5628 return (CTL_RETVAL_COMPLETE); 5629 } 5630 5631 defect_list_len = 0; 5632 5633 if (cdb->byte2 & SF_FMTDATA) { 5634 if (cdb->byte2 & SF_LONGLIST) { 5635 struct scsi_format_header_long *header; 5636 5637 header = (struct scsi_format_header_long *) 5638 ctsio->kern_data_ptr; 5639 5640 defect_list_len = scsi_4btoul(header->defect_list_len); 5641 if (defect_list_len != 0) { 5642 ctl_set_invalid_field(ctsio, 5643 /*sks_valid*/ 1, 5644 /*command*/ 0, 5645 /*field*/ 2, 5646 /*bit_valid*/ 0, 5647 /*bit*/ 0); 5648 goto bailout; 5649 } 5650 } else { 5651 struct scsi_format_header_short *header; 5652 5653 header = (struct scsi_format_header_short *) 5654 ctsio->kern_data_ptr; 5655 5656 defect_list_len = scsi_2btoul(header->defect_list_len); 5657 if (defect_list_len != 0) { 5658 ctl_set_invalid_field(ctsio, 5659 /*sks_valid*/ 1, 5660 /*command*/ 0, 5661 /*field*/ 2, 5662 /*bit_valid*/ 0, 5663 /*bit*/ 0); 5664 goto bailout; 5665 } 5666 } 5667 } 5668 5669 /* 5670 * The format command will clear out the "Medium format corrupted" 5671 * status if set by the configuration code. That status is really 5672 * just a way to notify the host that we have lost the media, and 5673 * get them to issue a command that will basically make them think 5674 * they're blowing away the media. 5675 */ 5676 mtx_lock(&lun->lun_lock); 5677 lun->flags &= ~CTL_LUN_INOPERABLE; 5678 mtx_unlock(&lun->lun_lock); 5679 5680 ctl_set_success(ctsio); 5681bailout: 5682 5683 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5684 free(ctsio->kern_data_ptr, M_CTL); 5685 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5686 } 5687 5688 ctl_done((union ctl_io *)ctsio); 5689 return (CTL_RETVAL_COMPLETE); 5690} 5691 5692int 5693ctl_read_buffer(struct ctl_scsiio *ctsio) 5694{ 5695 struct scsi_read_buffer *cdb; 5696 struct ctl_lun *lun; 5697 int buffer_offset, len; 5698 static uint8_t descr[4]; 5699 static uint8_t echo_descr[4] = { 0 }; 5700 5701 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5702 5703 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5704 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5705 5706 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5707 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5708 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5709 ctl_set_invalid_field(ctsio, 5710 /*sks_valid*/ 1, 5711 /*command*/ 1, 5712 /*field*/ 1, 5713 /*bit_valid*/ 1, 5714 /*bit*/ 4); 5715 ctl_done((union ctl_io *)ctsio); 5716 return (CTL_RETVAL_COMPLETE); 5717 } 5718 5719 len = scsi_3btoul(cdb->length); 5720 buffer_offset = scsi_3btoul(cdb->offset); 5721 5722 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5723 ctl_set_invalid_field(ctsio, 5724 /*sks_valid*/ 1, 5725 /*command*/ 1, 5726 /*field*/ 6, 5727 /*bit_valid*/ 0, 5728 /*bit*/ 0); 5729 ctl_done((union ctl_io *)ctsio); 5730 return (CTL_RETVAL_COMPLETE); 5731 } 5732 5733 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5734 descr[0] = 0; 5735 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5736 ctsio->kern_data_ptr = descr; 5737 len = min(len, sizeof(descr)); 5738 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5739 ctsio->kern_data_ptr = echo_descr; 5740 len = min(len, sizeof(echo_descr)); 5741 } else { 5742 if (lun->write_buffer == NULL) { 5743 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5744 M_CTL, M_WAITOK); 5745 } 5746 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5747 } 5748 ctsio->kern_data_len = len; 5749 ctsio->kern_total_len = len; 5750 ctsio->kern_data_resid = 0; 5751 ctsio->kern_rel_offset = 0; 5752 ctsio->kern_sg_entries = 0; 5753 ctl_set_success(ctsio); 5754 ctsio->be_move_done = ctl_config_move_done; 5755 ctl_datamove((union ctl_io *)ctsio); 5756 return (CTL_RETVAL_COMPLETE); 5757} 5758 5759int 5760ctl_write_buffer(struct ctl_scsiio *ctsio) 5761{ 5762 struct scsi_write_buffer *cdb; 5763 struct ctl_lun *lun; 5764 int buffer_offset, len; 5765 5766 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5767 5768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5769 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5770 5771 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5772 ctl_set_invalid_field(ctsio, 5773 /*sks_valid*/ 1, 5774 /*command*/ 1, 5775 /*field*/ 1, 5776 /*bit_valid*/ 1, 5777 /*bit*/ 4); 5778 ctl_done((union ctl_io *)ctsio); 5779 return (CTL_RETVAL_COMPLETE); 5780 } 5781 5782 len = scsi_3btoul(cdb->length); 5783 buffer_offset = scsi_3btoul(cdb->offset); 5784 5785 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5786 ctl_set_invalid_field(ctsio, 5787 /*sks_valid*/ 1, 5788 /*command*/ 1, 5789 /*field*/ 6, 5790 /*bit_valid*/ 0, 5791 /*bit*/ 0); 5792 ctl_done((union ctl_io *)ctsio); 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 5796 /* 5797 * If we've got a kernel request that hasn't been malloced yet, 5798 * malloc it and tell the caller the data buffer is here. 5799 */ 5800 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5801 if (lun->write_buffer == NULL) { 5802 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5803 M_CTL, M_WAITOK); 5804 } 5805 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5806 ctsio->kern_data_len = len; 5807 ctsio->kern_total_len = len; 5808 ctsio->kern_data_resid = 0; 5809 ctsio->kern_rel_offset = 0; 5810 ctsio->kern_sg_entries = 0; 5811 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5812 ctsio->be_move_done = ctl_config_move_done; 5813 ctl_datamove((union ctl_io *)ctsio); 5814 5815 return (CTL_RETVAL_COMPLETE); 5816 } 5817 5818 ctl_set_success(ctsio); 5819 ctl_done((union ctl_io *)ctsio); 5820 return (CTL_RETVAL_COMPLETE); 5821} 5822 5823int 5824ctl_write_same(struct ctl_scsiio *ctsio) 5825{ 5826 struct ctl_lun *lun; 5827 struct ctl_lba_len_flags *lbalen; 5828 uint64_t lba; 5829 uint32_t num_blocks; 5830 int len, retval; 5831 uint8_t byte2; 5832 5833 retval = CTL_RETVAL_COMPLETE; 5834 5835 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5836 5837 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5838 5839 switch (ctsio->cdb[0]) { 5840 case WRITE_SAME_10: { 5841 struct scsi_write_same_10 *cdb; 5842 5843 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5844 5845 lba = scsi_4btoul(cdb->addr); 5846 num_blocks = scsi_2btoul(cdb->length); 5847 byte2 = cdb->byte2; 5848 break; 5849 } 5850 case WRITE_SAME_16: { 5851 struct scsi_write_same_16 *cdb; 5852 5853 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5854 5855 lba = scsi_8btou64(cdb->addr); 5856 num_blocks = scsi_4btoul(cdb->length); 5857 byte2 = cdb->byte2; 5858 break; 5859 } 5860 default: 5861 /* 5862 * We got a command we don't support. This shouldn't 5863 * happen, commands should be filtered out above us. 5864 */ 5865 ctl_set_invalid_opcode(ctsio); 5866 ctl_done((union ctl_io *)ctsio); 5867 5868 return (CTL_RETVAL_COMPLETE); 5869 break; /* NOTREACHED */ 5870 } 5871 5872 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5873 if ((byte2 & SWS_UNMAP) == 0 && 5874 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5875 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5876 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5877 ctl_done((union ctl_io *)ctsio); 5878 return (CTL_RETVAL_COMPLETE); 5879 } 5880 5881 /* 5882 * The first check is to make sure we're in bounds, the second 5883 * check is to catch wrap-around problems. If the lba + num blocks 5884 * is less than the lba, then we've wrapped around and the block 5885 * range is invalid anyway. 5886 */ 5887 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5888 || ((lba + num_blocks) < lba)) { 5889 ctl_set_lba_out_of_range(ctsio); 5890 ctl_done((union ctl_io *)ctsio); 5891 return (CTL_RETVAL_COMPLETE); 5892 } 5893 5894 /* Zero number of blocks means "to the last logical block" */ 5895 if (num_blocks == 0) { 5896 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5897 ctl_set_invalid_field(ctsio, 5898 /*sks_valid*/ 0, 5899 /*command*/ 1, 5900 /*field*/ 0, 5901 /*bit_valid*/ 0, 5902 /*bit*/ 0); 5903 ctl_done((union ctl_io *)ctsio); 5904 return (CTL_RETVAL_COMPLETE); 5905 } 5906 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5907 } 5908 5909 len = lun->be_lun->blocksize; 5910 5911 /* 5912 * If we've got a kernel request that hasn't been malloced yet, 5913 * malloc it and tell the caller the data buffer is here. 5914 */ 5915 if ((byte2 & SWS_NDOB) == 0 && 5916 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5917 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5918 ctsio->kern_data_len = len; 5919 ctsio->kern_total_len = len; 5920 ctsio->kern_data_resid = 0; 5921 ctsio->kern_rel_offset = 0; 5922 ctsio->kern_sg_entries = 0; 5923 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5924 ctsio->be_move_done = ctl_config_move_done; 5925 ctl_datamove((union ctl_io *)ctsio); 5926 5927 return (CTL_RETVAL_COMPLETE); 5928 } 5929 5930 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5931 lbalen->lba = lba; 5932 lbalen->len = num_blocks; 5933 lbalen->flags = byte2; 5934 retval = lun->backend->config_write((union ctl_io *)ctsio); 5935 5936 return (retval); 5937} 5938 5939int 5940ctl_unmap(struct ctl_scsiio *ctsio) 5941{ 5942 struct ctl_lun *lun; 5943 struct scsi_unmap *cdb; 5944 struct ctl_ptr_len_flags *ptrlen; 5945 struct scsi_unmap_header *hdr; 5946 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5947 uint64_t lba; 5948 uint32_t num_blocks; 5949 int len, retval; 5950 uint8_t byte2; 5951 5952 retval = CTL_RETVAL_COMPLETE; 5953 5954 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5955 5956 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5957 cdb = (struct scsi_unmap *)ctsio->cdb; 5958 5959 len = scsi_2btoul(cdb->length); 5960 byte2 = cdb->byte2; 5961 5962 /* 5963 * If we've got a kernel request that hasn't been malloced yet, 5964 * malloc it and tell the caller the data buffer is here. 5965 */ 5966 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5967 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5968 ctsio->kern_data_len = len; 5969 ctsio->kern_total_len = len; 5970 ctsio->kern_data_resid = 0; 5971 ctsio->kern_rel_offset = 0; 5972 ctsio->kern_sg_entries = 0; 5973 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5974 ctsio->be_move_done = ctl_config_move_done; 5975 ctl_datamove((union ctl_io *)ctsio); 5976 5977 return (CTL_RETVAL_COMPLETE); 5978 } 5979 5980 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5981 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5982 if (len < sizeof (*hdr) || 5983 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5984 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5985 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5986 ctl_set_invalid_field(ctsio, 5987 /*sks_valid*/ 0, 5988 /*command*/ 0, 5989 /*field*/ 0, 5990 /*bit_valid*/ 0, 5991 /*bit*/ 0); 5992 goto done; 5993 } 5994 len = scsi_2btoul(hdr->desc_length); 5995 buf = (struct scsi_unmap_desc *)(hdr + 1); 5996 end = buf + len / sizeof(*buf); 5997 5998 endnz = buf; 5999 for (range = buf; range < end; range++) { 6000 lba = scsi_8btou64(range->lba); 6001 num_blocks = scsi_4btoul(range->length); 6002 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6003 || ((lba + num_blocks) < lba)) { 6004 ctl_set_lba_out_of_range(ctsio); 6005 ctl_done((union ctl_io *)ctsio); 6006 return (CTL_RETVAL_COMPLETE); 6007 } 6008 if (num_blocks != 0) 6009 endnz = range + 1; 6010 } 6011 6012 /* 6013 * Block backend can not handle zero last range. 6014 * Filter it out and return if there is nothing left. 6015 */ 6016 len = (uint8_t *)endnz - (uint8_t *)buf; 6017 if (len == 0) { 6018 ctl_set_success(ctsio); 6019 goto done; 6020 } 6021 6022 mtx_lock(&lun->lun_lock); 6023 ptrlen = (struct ctl_ptr_len_flags *) 6024 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6025 ptrlen->ptr = (void *)buf; 6026 ptrlen->len = len; 6027 ptrlen->flags = byte2; 6028 ctl_check_blocked(lun); 6029 mtx_unlock(&lun->lun_lock); 6030 6031 retval = lun->backend->config_write((union ctl_io *)ctsio); 6032 return (retval); 6033 6034done: 6035 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6036 free(ctsio->kern_data_ptr, M_CTL); 6037 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6038 } 6039 ctl_done((union ctl_io *)ctsio); 6040 return (CTL_RETVAL_COMPLETE); 6041} 6042 6043/* 6044 * Note that this function currently doesn't actually do anything inside 6045 * CTL to enforce things if the DQue bit is turned on. 6046 * 6047 * Also note that this function can't be used in the default case, because 6048 * the DQue bit isn't set in the changeable mask for the control mode page 6049 * anyway. This is just here as an example for how to implement a page 6050 * handler, and a placeholder in case we want to allow the user to turn 6051 * tagged queueing on and off. 6052 * 6053 * The D_SENSE bit handling is functional, however, and will turn 6054 * descriptor sense on and off for a given LUN. 6055 */ 6056int 6057ctl_control_page_handler(struct ctl_scsiio *ctsio, 6058 struct ctl_page_index *page_index, uint8_t *page_ptr) 6059{ 6060 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6061 struct ctl_lun *lun; 6062 int set_ua; 6063 uint32_t initidx; 6064 6065 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6066 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6067 set_ua = 0; 6068 6069 user_cp = (struct scsi_control_page *)page_ptr; 6070 current_cp = (struct scsi_control_page *) 6071 (page_index->page_data + (page_index->page_len * 6072 CTL_PAGE_CURRENT)); 6073 saved_cp = (struct scsi_control_page *) 6074 (page_index->page_data + (page_index->page_len * 6075 CTL_PAGE_SAVED)); 6076 6077 mtx_lock(&lun->lun_lock); 6078 if (((current_cp->rlec & SCP_DSENSE) == 0) 6079 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6080 /* 6081 * Descriptor sense is currently turned off and the user 6082 * wants to turn it on. 6083 */ 6084 current_cp->rlec |= SCP_DSENSE; 6085 saved_cp->rlec |= SCP_DSENSE; 6086 lun->flags |= CTL_LUN_SENSE_DESC; 6087 set_ua = 1; 6088 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6089 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6090 /* 6091 * Descriptor sense is currently turned on, and the user 6092 * wants to turn it off. 6093 */ 6094 current_cp->rlec &= ~SCP_DSENSE; 6095 saved_cp->rlec &= ~SCP_DSENSE; 6096 lun->flags &= ~CTL_LUN_SENSE_DESC; 6097 set_ua = 1; 6098 } 6099 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6100 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6101 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6102 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6103 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6104 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6105 set_ua = 1; 6106 } 6107 if ((current_cp->eca_and_aen & SCP_SWP) != 6108 (user_cp->eca_and_aen & SCP_SWP)) { 6109 current_cp->eca_and_aen &= ~SCP_SWP; 6110 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6111 saved_cp->eca_and_aen &= ~SCP_SWP; 6112 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6113 set_ua = 1; 6114 } 6115 if (set_ua != 0) 6116 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6117 mtx_unlock(&lun->lun_lock); 6118 6119 return (0); 6120} 6121 6122int 6123ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6124 struct ctl_page_index *page_index, uint8_t *page_ptr) 6125{ 6126 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6127 struct ctl_lun *lun; 6128 int set_ua; 6129 uint32_t initidx; 6130 6131 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6132 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6133 set_ua = 0; 6134 6135 user_cp = (struct scsi_caching_page *)page_ptr; 6136 current_cp = (struct scsi_caching_page *) 6137 (page_index->page_data + (page_index->page_len * 6138 CTL_PAGE_CURRENT)); 6139 saved_cp = (struct scsi_caching_page *) 6140 (page_index->page_data + (page_index->page_len * 6141 CTL_PAGE_SAVED)); 6142 6143 mtx_lock(&lun->lun_lock); 6144 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6145 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6146 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6147 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6148 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6149 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6150 set_ua = 1; 6151 } 6152 if (set_ua != 0) 6153 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6154 mtx_unlock(&lun->lun_lock); 6155 6156 return (0); 6157} 6158 6159int 6160ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6161 struct ctl_page_index *page_index, 6162 uint8_t *page_ptr) 6163{ 6164 uint8_t *c; 6165 int i; 6166 6167 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6168 ctl_time_io_secs = 6169 (c[0] << 8) | 6170 (c[1] << 0) | 6171 0; 6172 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6173 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6174 printf("page data:"); 6175 for (i=0; i<8; i++) 6176 printf(" %.2x",page_ptr[i]); 6177 printf("\n"); 6178 return (0); 6179} 6180 6181int 6182ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6183 struct ctl_page_index *page_index, 6184 int pc) 6185{ 6186 struct copan_debugconf_subpage *page; 6187 6188 page = (struct copan_debugconf_subpage *)page_index->page_data + 6189 (page_index->page_len * pc); 6190 6191 switch (pc) { 6192 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6193 case SMS_PAGE_CTRL_DEFAULT >> 6: 6194 case SMS_PAGE_CTRL_SAVED >> 6: 6195 /* 6196 * We don't update the changable or default bits for this page. 6197 */ 6198 break; 6199 case SMS_PAGE_CTRL_CURRENT >> 6: 6200 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6201 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6202 break; 6203 default: 6204#ifdef NEEDTOPORT 6205 EPRINT(0, "Invalid PC %d!!", pc); 6206#endif /* NEEDTOPORT */ 6207 break; 6208 } 6209 return (0); 6210} 6211 6212 6213static int 6214ctl_do_mode_select(union ctl_io *io) 6215{ 6216 struct scsi_mode_page_header *page_header; 6217 struct ctl_page_index *page_index; 6218 struct ctl_scsiio *ctsio; 6219 int control_dev, page_len; 6220 int page_len_offset, page_len_size; 6221 union ctl_modepage_info *modepage_info; 6222 struct ctl_lun *lun; 6223 int *len_left, *len_used; 6224 int retval, i; 6225 6226 ctsio = &io->scsiio; 6227 page_index = NULL; 6228 page_len = 0; 6229 retval = CTL_RETVAL_COMPLETE; 6230 6231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6232 6233 if (lun->be_lun->lun_type != T_DIRECT) 6234 control_dev = 1; 6235 else 6236 control_dev = 0; 6237 6238 modepage_info = (union ctl_modepage_info *) 6239 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6240 len_left = &modepage_info->header.len_left; 6241 len_used = &modepage_info->header.len_used; 6242 6243do_next_page: 6244 6245 page_header = (struct scsi_mode_page_header *) 6246 (ctsio->kern_data_ptr + *len_used); 6247 6248 if (*len_left == 0) { 6249 free(ctsio->kern_data_ptr, M_CTL); 6250 ctl_set_success(ctsio); 6251 ctl_done((union ctl_io *)ctsio); 6252 return (CTL_RETVAL_COMPLETE); 6253 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6254 6255 free(ctsio->kern_data_ptr, M_CTL); 6256 ctl_set_param_len_error(ctsio); 6257 ctl_done((union ctl_io *)ctsio); 6258 return (CTL_RETVAL_COMPLETE); 6259 6260 } else if ((page_header->page_code & SMPH_SPF) 6261 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6262 6263 free(ctsio->kern_data_ptr, M_CTL); 6264 ctl_set_param_len_error(ctsio); 6265 ctl_done((union ctl_io *)ctsio); 6266 return (CTL_RETVAL_COMPLETE); 6267 } 6268 6269 6270 /* 6271 * XXX KDM should we do something with the block descriptor? 6272 */ 6273 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6274 6275 if ((control_dev != 0) 6276 && (lun->mode_pages.index[i].page_flags & 6277 CTL_PAGE_FLAG_DISK_ONLY)) 6278 continue; 6279 6280 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6281 (page_header->page_code & SMPH_PC_MASK)) 6282 continue; 6283 6284 /* 6285 * If neither page has a subpage code, then we've got a 6286 * match. 6287 */ 6288 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6289 && ((page_header->page_code & SMPH_SPF) == 0)) { 6290 page_index = &lun->mode_pages.index[i]; 6291 page_len = page_header->page_length; 6292 break; 6293 } 6294 6295 /* 6296 * If both pages have subpages, then the subpage numbers 6297 * have to match. 6298 */ 6299 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6300 && (page_header->page_code & SMPH_SPF)) { 6301 struct scsi_mode_page_header_sp *sph; 6302 6303 sph = (struct scsi_mode_page_header_sp *)page_header; 6304 6305 if (lun->mode_pages.index[i].subpage == 6306 sph->subpage) { 6307 page_index = &lun->mode_pages.index[i]; 6308 page_len = scsi_2btoul(sph->page_length); 6309 break; 6310 } 6311 } 6312 } 6313 6314 /* 6315 * If we couldn't find the page, or if we don't have a mode select 6316 * handler for it, send back an error to the user. 6317 */ 6318 if ((page_index == NULL) 6319 || (page_index->select_handler == NULL)) { 6320 ctl_set_invalid_field(ctsio, 6321 /*sks_valid*/ 1, 6322 /*command*/ 0, 6323 /*field*/ *len_used, 6324 /*bit_valid*/ 0, 6325 /*bit*/ 0); 6326 free(ctsio->kern_data_ptr, M_CTL); 6327 ctl_done((union ctl_io *)ctsio); 6328 return (CTL_RETVAL_COMPLETE); 6329 } 6330 6331 if (page_index->page_code & SMPH_SPF) { 6332 page_len_offset = 2; 6333 page_len_size = 2; 6334 } else { 6335 page_len_size = 1; 6336 page_len_offset = 1; 6337 } 6338 6339 /* 6340 * If the length the initiator gives us isn't the one we specify in 6341 * the mode page header, or if they didn't specify enough data in 6342 * the CDB to avoid truncating this page, kick out the request. 6343 */ 6344 if ((page_len != (page_index->page_len - page_len_offset - 6345 page_len_size)) 6346 || (*len_left < page_index->page_len)) { 6347 6348 6349 ctl_set_invalid_field(ctsio, 6350 /*sks_valid*/ 1, 6351 /*command*/ 0, 6352 /*field*/ *len_used + page_len_offset, 6353 /*bit_valid*/ 0, 6354 /*bit*/ 0); 6355 free(ctsio->kern_data_ptr, M_CTL); 6356 ctl_done((union ctl_io *)ctsio); 6357 return (CTL_RETVAL_COMPLETE); 6358 } 6359 6360 /* 6361 * Run through the mode page, checking to make sure that the bits 6362 * the user changed are actually legal for him to change. 6363 */ 6364 for (i = 0; i < page_index->page_len; i++) { 6365 uint8_t *user_byte, *change_mask, *current_byte; 6366 int bad_bit; 6367 int j; 6368 6369 user_byte = (uint8_t *)page_header + i; 6370 change_mask = page_index->page_data + 6371 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6372 current_byte = page_index->page_data + 6373 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6374 6375 /* 6376 * Check to see whether the user set any bits in this byte 6377 * that he is not allowed to set. 6378 */ 6379 if ((*user_byte & ~(*change_mask)) == 6380 (*current_byte & ~(*change_mask))) 6381 continue; 6382 6383 /* 6384 * Go through bit by bit to determine which one is illegal. 6385 */ 6386 bad_bit = 0; 6387 for (j = 7; j >= 0; j--) { 6388 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6389 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6390 bad_bit = i; 6391 break; 6392 } 6393 } 6394 ctl_set_invalid_field(ctsio, 6395 /*sks_valid*/ 1, 6396 /*command*/ 0, 6397 /*field*/ *len_used + i, 6398 /*bit_valid*/ 1, 6399 /*bit*/ bad_bit); 6400 free(ctsio->kern_data_ptr, M_CTL); 6401 ctl_done((union ctl_io *)ctsio); 6402 return (CTL_RETVAL_COMPLETE); 6403 } 6404 6405 /* 6406 * Decrement these before we call the page handler, since we may 6407 * end up getting called back one way or another before the handler 6408 * returns to this context. 6409 */ 6410 *len_left -= page_index->page_len; 6411 *len_used += page_index->page_len; 6412 6413 retval = page_index->select_handler(ctsio, page_index, 6414 (uint8_t *)page_header); 6415 6416 /* 6417 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6418 * wait until this queued command completes to finish processing 6419 * the mode page. If it returns anything other than 6420 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6421 * already set the sense information, freed the data pointer, and 6422 * completed the io for us. 6423 */ 6424 if (retval != CTL_RETVAL_COMPLETE) 6425 goto bailout_no_done; 6426 6427 /* 6428 * If the initiator sent us more than one page, parse the next one. 6429 */ 6430 if (*len_left > 0) 6431 goto do_next_page; 6432 6433 ctl_set_success(ctsio); 6434 free(ctsio->kern_data_ptr, M_CTL); 6435 ctl_done((union ctl_io *)ctsio); 6436 6437bailout_no_done: 6438 6439 return (CTL_RETVAL_COMPLETE); 6440 6441} 6442 6443int 6444ctl_mode_select(struct ctl_scsiio *ctsio) 6445{ 6446 int param_len, pf, sp; 6447 int header_size, bd_len; 6448 int len_left, len_used; 6449 struct ctl_page_index *page_index; 6450 struct ctl_lun *lun; 6451 int control_dev, page_len; 6452 union ctl_modepage_info *modepage_info; 6453 int retval; 6454 6455 pf = 0; 6456 sp = 0; 6457 page_len = 0; 6458 len_used = 0; 6459 len_left = 0; 6460 retval = 0; 6461 bd_len = 0; 6462 page_index = NULL; 6463 6464 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6465 6466 if (lun->be_lun->lun_type != T_DIRECT) 6467 control_dev = 1; 6468 else 6469 control_dev = 0; 6470 6471 switch (ctsio->cdb[0]) { 6472 case MODE_SELECT_6: { 6473 struct scsi_mode_select_6 *cdb; 6474 6475 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6476 6477 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6478 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6479 6480 param_len = cdb->length; 6481 header_size = sizeof(struct scsi_mode_header_6); 6482 break; 6483 } 6484 case MODE_SELECT_10: { 6485 struct scsi_mode_select_10 *cdb; 6486 6487 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6488 6489 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6490 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6491 6492 param_len = scsi_2btoul(cdb->length); 6493 header_size = sizeof(struct scsi_mode_header_10); 6494 break; 6495 } 6496 default: 6497 ctl_set_invalid_opcode(ctsio); 6498 ctl_done((union ctl_io *)ctsio); 6499 return (CTL_RETVAL_COMPLETE); 6500 break; /* NOTREACHED */ 6501 } 6502 6503 /* 6504 * From SPC-3: 6505 * "A parameter list length of zero indicates that the Data-Out Buffer 6506 * shall be empty. This condition shall not be considered as an error." 6507 */ 6508 if (param_len == 0) { 6509 ctl_set_success(ctsio); 6510 ctl_done((union ctl_io *)ctsio); 6511 return (CTL_RETVAL_COMPLETE); 6512 } 6513 6514 /* 6515 * Since we'll hit this the first time through, prior to 6516 * allocation, we don't need to free a data buffer here. 6517 */ 6518 if (param_len < header_size) { 6519 ctl_set_param_len_error(ctsio); 6520 ctl_done((union ctl_io *)ctsio); 6521 return (CTL_RETVAL_COMPLETE); 6522 } 6523 6524 /* 6525 * Allocate the data buffer and grab the user's data. In theory, 6526 * we shouldn't have to sanity check the parameter list length here 6527 * because the maximum size is 64K. We should be able to malloc 6528 * that much without too many problems. 6529 */ 6530 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6531 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6532 ctsio->kern_data_len = param_len; 6533 ctsio->kern_total_len = param_len; 6534 ctsio->kern_data_resid = 0; 6535 ctsio->kern_rel_offset = 0; 6536 ctsio->kern_sg_entries = 0; 6537 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6538 ctsio->be_move_done = ctl_config_move_done; 6539 ctl_datamove((union ctl_io *)ctsio); 6540 6541 return (CTL_RETVAL_COMPLETE); 6542 } 6543 6544 switch (ctsio->cdb[0]) { 6545 case MODE_SELECT_6: { 6546 struct scsi_mode_header_6 *mh6; 6547 6548 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6549 bd_len = mh6->blk_desc_len; 6550 break; 6551 } 6552 case MODE_SELECT_10: { 6553 struct scsi_mode_header_10 *mh10; 6554 6555 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6556 bd_len = scsi_2btoul(mh10->blk_desc_len); 6557 break; 6558 } 6559 default: 6560 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6561 break; 6562 } 6563 6564 if (param_len < (header_size + bd_len)) { 6565 free(ctsio->kern_data_ptr, M_CTL); 6566 ctl_set_param_len_error(ctsio); 6567 ctl_done((union ctl_io *)ctsio); 6568 return (CTL_RETVAL_COMPLETE); 6569 } 6570 6571 /* 6572 * Set the IO_CONT flag, so that if this I/O gets passed to 6573 * ctl_config_write_done(), it'll get passed back to 6574 * ctl_do_mode_select() for further processing, or completion if 6575 * we're all done. 6576 */ 6577 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6578 ctsio->io_cont = ctl_do_mode_select; 6579 6580 modepage_info = (union ctl_modepage_info *) 6581 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6582 6583 memset(modepage_info, 0, sizeof(*modepage_info)); 6584 6585 len_left = param_len - header_size - bd_len; 6586 len_used = header_size + bd_len; 6587 6588 modepage_info->header.len_left = len_left; 6589 modepage_info->header.len_used = len_used; 6590 6591 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6592} 6593 6594int 6595ctl_mode_sense(struct ctl_scsiio *ctsio) 6596{ 6597 struct ctl_lun *lun; 6598 int pc, page_code, dbd, llba, subpage; 6599 int alloc_len, page_len, header_len, total_len; 6600 struct scsi_mode_block_descr *block_desc; 6601 struct ctl_page_index *page_index; 6602 int control_dev; 6603 6604 dbd = 0; 6605 llba = 0; 6606 block_desc = NULL; 6607 page_index = NULL; 6608 6609 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6610 6611 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6612 6613 if (lun->be_lun->lun_type != T_DIRECT) 6614 control_dev = 1; 6615 else 6616 control_dev = 0; 6617 6618 switch (ctsio->cdb[0]) { 6619 case MODE_SENSE_6: { 6620 struct scsi_mode_sense_6 *cdb; 6621 6622 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6623 6624 header_len = sizeof(struct scsi_mode_hdr_6); 6625 if (cdb->byte2 & SMS_DBD) 6626 dbd = 1; 6627 else 6628 header_len += sizeof(struct scsi_mode_block_descr); 6629 6630 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6631 page_code = cdb->page & SMS_PAGE_CODE; 6632 subpage = cdb->subpage; 6633 alloc_len = cdb->length; 6634 break; 6635 } 6636 case MODE_SENSE_10: { 6637 struct scsi_mode_sense_10 *cdb; 6638 6639 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6640 6641 header_len = sizeof(struct scsi_mode_hdr_10); 6642 6643 if (cdb->byte2 & SMS_DBD) 6644 dbd = 1; 6645 else 6646 header_len += sizeof(struct scsi_mode_block_descr); 6647 if (cdb->byte2 & SMS10_LLBAA) 6648 llba = 1; 6649 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6650 page_code = cdb->page & SMS_PAGE_CODE; 6651 subpage = cdb->subpage; 6652 alloc_len = scsi_2btoul(cdb->length); 6653 break; 6654 } 6655 default: 6656 ctl_set_invalid_opcode(ctsio); 6657 ctl_done((union ctl_io *)ctsio); 6658 return (CTL_RETVAL_COMPLETE); 6659 break; /* NOTREACHED */ 6660 } 6661 6662 /* 6663 * We have to make a first pass through to calculate the size of 6664 * the pages that match the user's query. Then we allocate enough 6665 * memory to hold it, and actually copy the data into the buffer. 6666 */ 6667 switch (page_code) { 6668 case SMS_ALL_PAGES_PAGE: { 6669 int i; 6670 6671 page_len = 0; 6672 6673 /* 6674 * At the moment, values other than 0 and 0xff here are 6675 * reserved according to SPC-3. 6676 */ 6677 if ((subpage != SMS_SUBPAGE_PAGE_0) 6678 && (subpage != SMS_SUBPAGE_ALL)) { 6679 ctl_set_invalid_field(ctsio, 6680 /*sks_valid*/ 1, 6681 /*command*/ 1, 6682 /*field*/ 3, 6683 /*bit_valid*/ 0, 6684 /*bit*/ 0); 6685 ctl_done((union ctl_io *)ctsio); 6686 return (CTL_RETVAL_COMPLETE); 6687 } 6688 6689 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6690 if ((control_dev != 0) 6691 && (lun->mode_pages.index[i].page_flags & 6692 CTL_PAGE_FLAG_DISK_ONLY)) 6693 continue; 6694 6695 /* 6696 * We don't use this subpage if the user didn't 6697 * request all subpages. 6698 */ 6699 if ((lun->mode_pages.index[i].subpage != 0) 6700 && (subpage == SMS_SUBPAGE_PAGE_0)) 6701 continue; 6702 6703#if 0 6704 printf("found page %#x len %d\n", 6705 lun->mode_pages.index[i].page_code & 6706 SMPH_PC_MASK, 6707 lun->mode_pages.index[i].page_len); 6708#endif 6709 page_len += lun->mode_pages.index[i].page_len; 6710 } 6711 break; 6712 } 6713 default: { 6714 int i; 6715 6716 page_len = 0; 6717 6718 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6719 /* Look for the right page code */ 6720 if ((lun->mode_pages.index[i].page_code & 6721 SMPH_PC_MASK) != page_code) 6722 continue; 6723 6724 /* Look for the right subpage or the subpage wildcard*/ 6725 if ((lun->mode_pages.index[i].subpage != subpage) 6726 && (subpage != SMS_SUBPAGE_ALL)) 6727 continue; 6728 6729 /* Make sure the page is supported for this dev type */ 6730 if ((control_dev != 0) 6731 && (lun->mode_pages.index[i].page_flags & 6732 CTL_PAGE_FLAG_DISK_ONLY)) 6733 continue; 6734 6735#if 0 6736 printf("found page %#x len %d\n", 6737 lun->mode_pages.index[i].page_code & 6738 SMPH_PC_MASK, 6739 lun->mode_pages.index[i].page_len); 6740#endif 6741 6742 page_len += lun->mode_pages.index[i].page_len; 6743 } 6744 6745 if (page_len == 0) { 6746 ctl_set_invalid_field(ctsio, 6747 /*sks_valid*/ 1, 6748 /*command*/ 1, 6749 /*field*/ 2, 6750 /*bit_valid*/ 1, 6751 /*bit*/ 5); 6752 ctl_done((union ctl_io *)ctsio); 6753 return (CTL_RETVAL_COMPLETE); 6754 } 6755 break; 6756 } 6757 } 6758 6759 total_len = header_len + page_len; 6760#if 0 6761 printf("header_len = %d, page_len = %d, total_len = %d\n", 6762 header_len, page_len, total_len); 6763#endif 6764 6765 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6766 ctsio->kern_sg_entries = 0; 6767 ctsio->kern_data_resid = 0; 6768 ctsio->kern_rel_offset = 0; 6769 if (total_len < alloc_len) { 6770 ctsio->residual = alloc_len - total_len; 6771 ctsio->kern_data_len = total_len; 6772 ctsio->kern_total_len = total_len; 6773 } else { 6774 ctsio->residual = 0; 6775 ctsio->kern_data_len = alloc_len; 6776 ctsio->kern_total_len = alloc_len; 6777 } 6778 6779 switch (ctsio->cdb[0]) { 6780 case MODE_SENSE_6: { 6781 struct scsi_mode_hdr_6 *header; 6782 6783 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6784 6785 header->datalen = MIN(total_len - 1, 254); 6786 if (control_dev == 0) { 6787 header->dev_specific = 0x10; /* DPOFUA */ 6788 if ((lun->flags & CTL_LUN_READONLY) || 6789 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6790 .eca_and_aen & SCP_SWP) != 0) 6791 header->dev_specific |= 0x80; /* WP */ 6792 } 6793 if (dbd) 6794 header->block_descr_len = 0; 6795 else 6796 header->block_descr_len = 6797 sizeof(struct scsi_mode_block_descr); 6798 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6799 break; 6800 } 6801 case MODE_SENSE_10: { 6802 struct scsi_mode_hdr_10 *header; 6803 int datalen; 6804 6805 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6806 6807 datalen = MIN(total_len - 2, 65533); 6808 scsi_ulto2b(datalen, header->datalen); 6809 if (control_dev == 0) { 6810 header->dev_specific = 0x10; /* DPOFUA */ 6811 if ((lun->flags & CTL_LUN_READONLY) || 6812 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6813 .eca_and_aen & SCP_SWP) != 0) 6814 header->dev_specific |= 0x80; /* WP */ 6815 } 6816 if (dbd) 6817 scsi_ulto2b(0, header->block_descr_len); 6818 else 6819 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6820 header->block_descr_len); 6821 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6822 break; 6823 } 6824 default: 6825 panic("invalid CDB type %#x", ctsio->cdb[0]); 6826 break; /* NOTREACHED */ 6827 } 6828 6829 /* 6830 * If we've got a disk, use its blocksize in the block 6831 * descriptor. Otherwise, just set it to 0. 6832 */ 6833 if (dbd == 0) { 6834 if (control_dev == 0) 6835 scsi_ulto3b(lun->be_lun->blocksize, 6836 block_desc->block_len); 6837 else 6838 scsi_ulto3b(0, block_desc->block_len); 6839 } 6840 6841 switch (page_code) { 6842 case SMS_ALL_PAGES_PAGE: { 6843 int i, data_used; 6844 6845 data_used = header_len; 6846 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6847 struct ctl_page_index *page_index; 6848 6849 page_index = &lun->mode_pages.index[i]; 6850 6851 if ((control_dev != 0) 6852 && (page_index->page_flags & 6853 CTL_PAGE_FLAG_DISK_ONLY)) 6854 continue; 6855 6856 /* 6857 * We don't use this subpage if the user didn't 6858 * request all subpages. We already checked (above) 6859 * to make sure the user only specified a subpage 6860 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6861 */ 6862 if ((page_index->subpage != 0) 6863 && (subpage == SMS_SUBPAGE_PAGE_0)) 6864 continue; 6865 6866 /* 6867 * Call the handler, if it exists, to update the 6868 * page to the latest values. 6869 */ 6870 if (page_index->sense_handler != NULL) 6871 page_index->sense_handler(ctsio, page_index,pc); 6872 6873 memcpy(ctsio->kern_data_ptr + data_used, 6874 page_index->page_data + 6875 (page_index->page_len * pc), 6876 page_index->page_len); 6877 data_used += page_index->page_len; 6878 } 6879 break; 6880 } 6881 default: { 6882 int i, data_used; 6883 6884 data_used = header_len; 6885 6886 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6887 struct ctl_page_index *page_index; 6888 6889 page_index = &lun->mode_pages.index[i]; 6890 6891 /* Look for the right page code */ 6892 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6893 continue; 6894 6895 /* Look for the right subpage or the subpage wildcard*/ 6896 if ((page_index->subpage != subpage) 6897 && (subpage != SMS_SUBPAGE_ALL)) 6898 continue; 6899 6900 /* Make sure the page is supported for this dev type */ 6901 if ((control_dev != 0) 6902 && (page_index->page_flags & 6903 CTL_PAGE_FLAG_DISK_ONLY)) 6904 continue; 6905 6906 /* 6907 * Call the handler, if it exists, to update the 6908 * page to the latest values. 6909 */ 6910 if (page_index->sense_handler != NULL) 6911 page_index->sense_handler(ctsio, page_index,pc); 6912 6913 memcpy(ctsio->kern_data_ptr + data_used, 6914 page_index->page_data + 6915 (page_index->page_len * pc), 6916 page_index->page_len); 6917 data_used += page_index->page_len; 6918 } 6919 break; 6920 } 6921 } 6922 6923 ctl_set_success(ctsio); 6924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6925 ctsio->be_move_done = ctl_config_move_done; 6926 ctl_datamove((union ctl_io *)ctsio); 6927 return (CTL_RETVAL_COMPLETE); 6928} 6929 6930int 6931ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6932 struct ctl_page_index *page_index, 6933 int pc) 6934{ 6935 struct ctl_lun *lun; 6936 struct scsi_log_param_header *phdr; 6937 uint8_t *data; 6938 uint64_t val; 6939 6940 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6941 data = page_index->page_data; 6942 6943 if (lun->backend->lun_attr != NULL && 6944 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6945 != UINT64_MAX) { 6946 phdr = (struct scsi_log_param_header *)data; 6947 scsi_ulto2b(0x0001, phdr->param_code); 6948 phdr->param_control = SLP_LBIN | SLP_LP; 6949 phdr->param_len = 8; 6950 data = (uint8_t *)(phdr + 1); 6951 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6952 data[4] = 0x02; /* per-pool */ 6953 data += phdr->param_len; 6954 } 6955 6956 if (lun->backend->lun_attr != NULL && 6957 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6958 != UINT64_MAX) { 6959 phdr = (struct scsi_log_param_header *)data; 6960 scsi_ulto2b(0x0002, phdr->param_code); 6961 phdr->param_control = SLP_LBIN | SLP_LP; 6962 phdr->param_len = 8; 6963 data = (uint8_t *)(phdr + 1); 6964 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6965 data[4] = 0x01; /* per-LUN */ 6966 data += phdr->param_len; 6967 } 6968 6969 if (lun->backend->lun_attr != NULL && 6970 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6971 != UINT64_MAX) { 6972 phdr = (struct scsi_log_param_header *)data; 6973 scsi_ulto2b(0x00f1, phdr->param_code); 6974 phdr->param_control = SLP_LBIN | SLP_LP; 6975 phdr->param_len = 8; 6976 data = (uint8_t *)(phdr + 1); 6977 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6978 data[4] = 0x02; /* per-pool */ 6979 data += phdr->param_len; 6980 } 6981 6982 if (lun->backend->lun_attr != NULL && 6983 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6984 != UINT64_MAX) { 6985 phdr = (struct scsi_log_param_header *)data; 6986 scsi_ulto2b(0x00f2, phdr->param_code); 6987 phdr->param_control = SLP_LBIN | SLP_LP; 6988 phdr->param_len = 8; 6989 data = (uint8_t *)(phdr + 1); 6990 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6991 data[4] = 0x02; /* per-pool */ 6992 data += phdr->param_len; 6993 } 6994 6995 page_index->page_len = data - page_index->page_data; 6996 return (0); 6997} 6998 6999int 7000ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 7001 struct ctl_page_index *page_index, 7002 int pc) 7003{ 7004 struct ctl_lun *lun; 7005 struct stat_page *data; 7006 uint64_t rn, wn, rb, wb; 7007 struct bintime rt, wt; 7008 int i; 7009 7010 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7011 data = (struct stat_page *)page_index->page_data; 7012 7013 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 7014 data->sap.hdr.param_control = SLP_LBIN; 7015 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 7016 sizeof(struct scsi_log_param_header); 7017 rn = wn = rb = wb = 0; 7018 bintime_clear(&rt); 7019 bintime_clear(&wt); 7020 for (i = 0; i < CTL_MAX_PORTS; i++) { 7021 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 7022 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 7023 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 7024 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 7025 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 7026 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 7027 } 7028 scsi_u64to8b(rn, data->sap.read_num); 7029 scsi_u64to8b(wn, data->sap.write_num); 7030 if (lun->stats.blocksize > 0) { 7031 scsi_u64to8b(wb / lun->stats.blocksize, 7032 data->sap.recvieved_lba); 7033 scsi_u64to8b(rb / lun->stats.blocksize, 7034 data->sap.transmitted_lba); 7035 } 7036 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 7037 data->sap.read_int); 7038 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 7039 data->sap.write_int); 7040 scsi_u64to8b(0, data->sap.weighted_num); 7041 scsi_u64to8b(0, data->sap.weighted_int); 7042 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 7043 data->it.hdr.param_control = SLP_LBIN; 7044 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 7045 sizeof(struct scsi_log_param_header); 7046#ifdef CTL_TIME_IO 7047 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 7048#endif 7049 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 7050 data->it.hdr.param_control = SLP_LBIN; 7051 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 7052 sizeof(struct scsi_log_param_header); 7053 scsi_ulto4b(3, data->ti.exponent); 7054 scsi_ulto4b(1, data->ti.integer); 7055 7056 page_index->page_len = sizeof(*data); 7057 return (0); 7058} 7059 7060int 7061ctl_log_sense(struct ctl_scsiio *ctsio) 7062{ 7063 struct ctl_lun *lun; 7064 int i, pc, page_code, subpage; 7065 int alloc_len, total_len; 7066 struct ctl_page_index *page_index; 7067 struct scsi_log_sense *cdb; 7068 struct scsi_log_header *header; 7069 7070 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7071 7072 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7073 cdb = (struct scsi_log_sense *)ctsio->cdb; 7074 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7075 page_code = cdb->page & SLS_PAGE_CODE; 7076 subpage = cdb->subpage; 7077 alloc_len = scsi_2btoul(cdb->length); 7078 7079 page_index = NULL; 7080 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7081 page_index = &lun->log_pages.index[i]; 7082 7083 /* Look for the right page code */ 7084 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7085 continue; 7086 7087 /* Look for the right subpage or the subpage wildcard*/ 7088 if (page_index->subpage != subpage) 7089 continue; 7090 7091 break; 7092 } 7093 if (i >= CTL_NUM_LOG_PAGES) { 7094 ctl_set_invalid_field(ctsio, 7095 /*sks_valid*/ 1, 7096 /*command*/ 1, 7097 /*field*/ 2, 7098 /*bit_valid*/ 0, 7099 /*bit*/ 0); 7100 ctl_done((union ctl_io *)ctsio); 7101 return (CTL_RETVAL_COMPLETE); 7102 } 7103 7104 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7105 7106 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7107 ctsio->kern_sg_entries = 0; 7108 ctsio->kern_data_resid = 0; 7109 ctsio->kern_rel_offset = 0; 7110 if (total_len < alloc_len) { 7111 ctsio->residual = alloc_len - total_len; 7112 ctsio->kern_data_len = total_len; 7113 ctsio->kern_total_len = total_len; 7114 } else { 7115 ctsio->residual = 0; 7116 ctsio->kern_data_len = alloc_len; 7117 ctsio->kern_total_len = alloc_len; 7118 } 7119 7120 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7121 header->page = page_index->page_code; 7122 if (page_index->subpage) { 7123 header->page |= SL_SPF; 7124 header->subpage = page_index->subpage; 7125 } 7126 scsi_ulto2b(page_index->page_len, header->datalen); 7127 7128 /* 7129 * Call the handler, if it exists, to update the 7130 * page to the latest values. 7131 */ 7132 if (page_index->sense_handler != NULL) 7133 page_index->sense_handler(ctsio, page_index, pc); 7134 7135 memcpy(header + 1, page_index->page_data, page_index->page_len); 7136 7137 ctl_set_success(ctsio); 7138 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7139 ctsio->be_move_done = ctl_config_move_done; 7140 ctl_datamove((union ctl_io *)ctsio); 7141 return (CTL_RETVAL_COMPLETE); 7142} 7143 7144int 7145ctl_read_capacity(struct ctl_scsiio *ctsio) 7146{ 7147 struct scsi_read_capacity *cdb; 7148 struct scsi_read_capacity_data *data; 7149 struct ctl_lun *lun; 7150 uint32_t lba; 7151 7152 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7153 7154 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7155 7156 lba = scsi_4btoul(cdb->addr); 7157 if (((cdb->pmi & SRC_PMI) == 0) 7158 && (lba != 0)) { 7159 ctl_set_invalid_field(/*ctsio*/ ctsio, 7160 /*sks_valid*/ 1, 7161 /*command*/ 1, 7162 /*field*/ 2, 7163 /*bit_valid*/ 0, 7164 /*bit*/ 0); 7165 ctl_done((union ctl_io *)ctsio); 7166 return (CTL_RETVAL_COMPLETE); 7167 } 7168 7169 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7170 7171 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7172 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7173 ctsio->residual = 0; 7174 ctsio->kern_data_len = sizeof(*data); 7175 ctsio->kern_total_len = sizeof(*data); 7176 ctsio->kern_data_resid = 0; 7177 ctsio->kern_rel_offset = 0; 7178 ctsio->kern_sg_entries = 0; 7179 7180 /* 7181 * If the maximum LBA is greater than 0xfffffffe, the user must 7182 * issue a SERVICE ACTION IN (16) command, with the read capacity 7183 * serivce action set. 7184 */ 7185 if (lun->be_lun->maxlba > 0xfffffffe) 7186 scsi_ulto4b(0xffffffff, data->addr); 7187 else 7188 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7189 7190 /* 7191 * XXX KDM this may not be 512 bytes... 7192 */ 7193 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7194 7195 ctl_set_success(ctsio); 7196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7197 ctsio->be_move_done = ctl_config_move_done; 7198 ctl_datamove((union ctl_io *)ctsio); 7199 return (CTL_RETVAL_COMPLETE); 7200} 7201 7202int 7203ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7204{ 7205 struct scsi_read_capacity_16 *cdb; 7206 struct scsi_read_capacity_data_long *data; 7207 struct ctl_lun *lun; 7208 uint64_t lba; 7209 uint32_t alloc_len; 7210 7211 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7212 7213 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7214 7215 alloc_len = scsi_4btoul(cdb->alloc_len); 7216 lba = scsi_8btou64(cdb->addr); 7217 7218 if ((cdb->reladr & SRC16_PMI) 7219 && (lba != 0)) { 7220 ctl_set_invalid_field(/*ctsio*/ ctsio, 7221 /*sks_valid*/ 1, 7222 /*command*/ 1, 7223 /*field*/ 2, 7224 /*bit_valid*/ 0, 7225 /*bit*/ 0); 7226 ctl_done((union ctl_io *)ctsio); 7227 return (CTL_RETVAL_COMPLETE); 7228 } 7229 7230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7231 7232 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7233 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7234 7235 if (sizeof(*data) < alloc_len) { 7236 ctsio->residual = alloc_len - sizeof(*data); 7237 ctsio->kern_data_len = sizeof(*data); 7238 ctsio->kern_total_len = sizeof(*data); 7239 } else { 7240 ctsio->residual = 0; 7241 ctsio->kern_data_len = alloc_len; 7242 ctsio->kern_total_len = alloc_len; 7243 } 7244 ctsio->kern_data_resid = 0; 7245 ctsio->kern_rel_offset = 0; 7246 ctsio->kern_sg_entries = 0; 7247 7248 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7249 /* XXX KDM this may not be 512 bytes... */ 7250 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7251 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7252 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7253 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7254 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7255 7256 ctl_set_success(ctsio); 7257 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7258 ctsio->be_move_done = ctl_config_move_done; 7259 ctl_datamove((union ctl_io *)ctsio); 7260 return (CTL_RETVAL_COMPLETE); 7261} 7262 7263int 7264ctl_get_lba_status(struct ctl_scsiio *ctsio) 7265{ 7266 struct scsi_get_lba_status *cdb; 7267 struct scsi_get_lba_status_data *data; 7268 struct ctl_lun *lun; 7269 struct ctl_lba_len_flags *lbalen; 7270 uint64_t lba; 7271 uint32_t alloc_len, total_len; 7272 int retval; 7273 7274 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7275 7276 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7277 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7278 lba = scsi_8btou64(cdb->addr); 7279 alloc_len = scsi_4btoul(cdb->alloc_len); 7280 7281 if (lba > lun->be_lun->maxlba) { 7282 ctl_set_lba_out_of_range(ctsio); 7283 ctl_done((union ctl_io *)ctsio); 7284 return (CTL_RETVAL_COMPLETE); 7285 } 7286 7287 total_len = sizeof(*data) + sizeof(data->descr[0]); 7288 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7289 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7290 7291 if (total_len < alloc_len) { 7292 ctsio->residual = alloc_len - total_len; 7293 ctsio->kern_data_len = total_len; 7294 ctsio->kern_total_len = total_len; 7295 } else { 7296 ctsio->residual = 0; 7297 ctsio->kern_data_len = alloc_len; 7298 ctsio->kern_total_len = alloc_len; 7299 } 7300 ctsio->kern_data_resid = 0; 7301 ctsio->kern_rel_offset = 0; 7302 ctsio->kern_sg_entries = 0; 7303 7304 /* Fill dummy data in case backend can't tell anything. */ 7305 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7306 scsi_u64to8b(lba, data->descr[0].addr); 7307 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7308 data->descr[0].length); 7309 data->descr[0].status = 0; /* Mapped or unknown. */ 7310 7311 ctl_set_success(ctsio); 7312 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7313 ctsio->be_move_done = ctl_config_move_done; 7314 7315 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7316 lbalen->lba = lba; 7317 lbalen->len = total_len; 7318 lbalen->flags = 0; 7319 retval = lun->backend->config_read((union ctl_io *)ctsio); 7320 return (CTL_RETVAL_COMPLETE); 7321} 7322 7323int 7324ctl_read_defect(struct ctl_scsiio *ctsio) 7325{ 7326 struct scsi_read_defect_data_10 *ccb10; 7327 struct scsi_read_defect_data_12 *ccb12; 7328 struct scsi_read_defect_data_hdr_10 *data10; 7329 struct scsi_read_defect_data_hdr_12 *data12; 7330 uint32_t alloc_len, data_len; 7331 uint8_t format; 7332 7333 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7334 7335 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7336 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7337 format = ccb10->format; 7338 alloc_len = scsi_2btoul(ccb10->alloc_length); 7339 data_len = sizeof(*data10); 7340 } else { 7341 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7342 format = ccb12->format; 7343 alloc_len = scsi_4btoul(ccb12->alloc_length); 7344 data_len = sizeof(*data12); 7345 } 7346 if (alloc_len == 0) { 7347 ctl_set_success(ctsio); 7348 ctl_done((union ctl_io *)ctsio); 7349 return (CTL_RETVAL_COMPLETE); 7350 } 7351 7352 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7353 if (data_len < alloc_len) { 7354 ctsio->residual = alloc_len - data_len; 7355 ctsio->kern_data_len = data_len; 7356 ctsio->kern_total_len = data_len; 7357 } else { 7358 ctsio->residual = 0; 7359 ctsio->kern_data_len = alloc_len; 7360 ctsio->kern_total_len = alloc_len; 7361 } 7362 ctsio->kern_data_resid = 0; 7363 ctsio->kern_rel_offset = 0; 7364 ctsio->kern_sg_entries = 0; 7365 7366 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7367 data10 = (struct scsi_read_defect_data_hdr_10 *) 7368 ctsio->kern_data_ptr; 7369 data10->format = format; 7370 scsi_ulto2b(0, data10->length); 7371 } else { 7372 data12 = (struct scsi_read_defect_data_hdr_12 *) 7373 ctsio->kern_data_ptr; 7374 data12->format = format; 7375 scsi_ulto2b(0, data12->generation); 7376 scsi_ulto4b(0, data12->length); 7377 } 7378 7379 ctl_set_success(ctsio); 7380 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7381 ctsio->be_move_done = ctl_config_move_done; 7382 ctl_datamove((union ctl_io *)ctsio); 7383 return (CTL_RETVAL_COMPLETE); 7384} 7385 7386int 7387ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7388{ 7389 struct scsi_maintenance_in *cdb; 7390 int retval; 7391 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7392 int num_target_port_groups, num_target_ports; 7393 struct ctl_lun *lun; 7394 struct ctl_softc *softc; 7395 struct ctl_port *port; 7396 struct scsi_target_group_data *rtg_ptr; 7397 struct scsi_target_group_data_extended *rtg_ext_ptr; 7398 struct scsi_target_port_group_descriptor *tpg_desc; 7399 7400 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7401 7402 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7403 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7404 softc = lun->ctl_softc; 7405 7406 retval = CTL_RETVAL_COMPLETE; 7407 7408 switch (cdb->byte2 & STG_PDF_MASK) { 7409 case STG_PDF_LENGTH: 7410 ext = 0; 7411 break; 7412 case STG_PDF_EXTENDED: 7413 ext = 1; 7414 break; 7415 default: 7416 ctl_set_invalid_field(/*ctsio*/ ctsio, 7417 /*sks_valid*/ 1, 7418 /*command*/ 1, 7419 /*field*/ 2, 7420 /*bit_valid*/ 1, 7421 /*bit*/ 5); 7422 ctl_done((union ctl_io *)ctsio); 7423 return(retval); 7424 } 7425 7426 if (softc->is_single) 7427 num_target_port_groups = 1; 7428 else 7429 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7430 num_target_ports = 0; 7431 mtx_lock(&softc->ctl_lock); 7432 STAILQ_FOREACH(port, &softc->port_list, links) { 7433 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7434 continue; 7435 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7436 continue; 7437 num_target_ports++; 7438 } 7439 mtx_unlock(&softc->ctl_lock); 7440 7441 if (ext) 7442 total_len = sizeof(struct scsi_target_group_data_extended); 7443 else 7444 total_len = sizeof(struct scsi_target_group_data); 7445 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7446 num_target_port_groups + 7447 sizeof(struct scsi_target_port_descriptor) * 7448 num_target_ports * num_target_port_groups; 7449 7450 alloc_len = scsi_4btoul(cdb->length); 7451 7452 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7453 7454 ctsio->kern_sg_entries = 0; 7455 7456 if (total_len < alloc_len) { 7457 ctsio->residual = alloc_len - total_len; 7458 ctsio->kern_data_len = total_len; 7459 ctsio->kern_total_len = total_len; 7460 } else { 7461 ctsio->residual = 0; 7462 ctsio->kern_data_len = alloc_len; 7463 ctsio->kern_total_len = alloc_len; 7464 } 7465 ctsio->kern_data_resid = 0; 7466 ctsio->kern_rel_offset = 0; 7467 7468 if (ext) { 7469 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7470 ctsio->kern_data_ptr; 7471 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7472 rtg_ext_ptr->format_type = 0x10; 7473 rtg_ext_ptr->implicit_transition_time = 0; 7474 tpg_desc = &rtg_ext_ptr->groups[0]; 7475 } else { 7476 rtg_ptr = (struct scsi_target_group_data *) 7477 ctsio->kern_data_ptr; 7478 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7479 tpg_desc = &rtg_ptr->groups[0]; 7480 } 7481 7482 mtx_lock(&softc->ctl_lock); 7483 pg = softc->port_offset / CTL_MAX_PORTS; 7484 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7485 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7486 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7487 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7488 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7489 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7490 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7491 } else { 7492 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7493 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7494 } 7495 } else { 7496 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7497 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7498 } 7499 for (g = 0; g < num_target_port_groups; g++) { 7500 tpg_desc->pref_state = (g == pg) ? gs : os; 7501 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7502 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7503 tpg_desc->status = TPG_IMPLICIT; 7504 pc = 0; 7505 STAILQ_FOREACH(port, &softc->port_list, links) { 7506 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7507 continue; 7508 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7509 continue; 7510 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7511 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7512 relative_target_port_identifier); 7513 pc++; 7514 } 7515 tpg_desc->target_port_count = pc; 7516 tpg_desc = (struct scsi_target_port_group_descriptor *) 7517 &tpg_desc->descriptors[pc]; 7518 } 7519 mtx_unlock(&softc->ctl_lock); 7520 7521 ctl_set_success(ctsio); 7522 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7523 ctsio->be_move_done = ctl_config_move_done; 7524 ctl_datamove((union ctl_io *)ctsio); 7525 return(retval); 7526} 7527 7528int 7529ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7530{ 7531 struct ctl_lun *lun; 7532 struct scsi_report_supported_opcodes *cdb; 7533 const struct ctl_cmd_entry *entry, *sentry; 7534 struct scsi_report_supported_opcodes_all *all; 7535 struct scsi_report_supported_opcodes_descr *descr; 7536 struct scsi_report_supported_opcodes_one *one; 7537 int retval; 7538 int alloc_len, total_len; 7539 int opcode, service_action, i, j, num; 7540 7541 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7542 7543 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7544 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7545 7546 retval = CTL_RETVAL_COMPLETE; 7547 7548 opcode = cdb->requested_opcode; 7549 service_action = scsi_2btoul(cdb->requested_service_action); 7550 switch (cdb->options & RSO_OPTIONS_MASK) { 7551 case RSO_OPTIONS_ALL: 7552 num = 0; 7553 for (i = 0; i < 256; i++) { 7554 entry = &ctl_cmd_table[i]; 7555 if (entry->flags & CTL_CMD_FLAG_SA5) { 7556 for (j = 0; j < 32; j++) { 7557 sentry = &((const struct ctl_cmd_entry *) 7558 entry->execute)[j]; 7559 if (ctl_cmd_applicable( 7560 lun->be_lun->lun_type, sentry)) 7561 num++; 7562 } 7563 } else { 7564 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7565 entry)) 7566 num++; 7567 } 7568 } 7569 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7570 num * sizeof(struct scsi_report_supported_opcodes_descr); 7571 break; 7572 case RSO_OPTIONS_OC: 7573 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7574 ctl_set_invalid_field(/*ctsio*/ ctsio, 7575 /*sks_valid*/ 1, 7576 /*command*/ 1, 7577 /*field*/ 2, 7578 /*bit_valid*/ 1, 7579 /*bit*/ 2); 7580 ctl_done((union ctl_io *)ctsio); 7581 return (CTL_RETVAL_COMPLETE); 7582 } 7583 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7584 break; 7585 case RSO_OPTIONS_OC_SA: 7586 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7587 service_action >= 32) { 7588 ctl_set_invalid_field(/*ctsio*/ ctsio, 7589 /*sks_valid*/ 1, 7590 /*command*/ 1, 7591 /*field*/ 2, 7592 /*bit_valid*/ 1, 7593 /*bit*/ 2); 7594 ctl_done((union ctl_io *)ctsio); 7595 return (CTL_RETVAL_COMPLETE); 7596 } 7597 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7598 break; 7599 default: 7600 ctl_set_invalid_field(/*ctsio*/ ctsio, 7601 /*sks_valid*/ 1, 7602 /*command*/ 1, 7603 /*field*/ 2, 7604 /*bit_valid*/ 1, 7605 /*bit*/ 2); 7606 ctl_done((union ctl_io *)ctsio); 7607 return (CTL_RETVAL_COMPLETE); 7608 } 7609 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 switch (cdb->options & RSO_OPTIONS_MASK) { 7629 case RSO_OPTIONS_ALL: 7630 all = (struct scsi_report_supported_opcodes_all *) 7631 ctsio->kern_data_ptr; 7632 num = 0; 7633 for (i = 0; i < 256; i++) { 7634 entry = &ctl_cmd_table[i]; 7635 if (entry->flags & CTL_CMD_FLAG_SA5) { 7636 for (j = 0; j < 32; j++) { 7637 sentry = &((const struct ctl_cmd_entry *) 7638 entry->execute)[j]; 7639 if (!ctl_cmd_applicable( 7640 lun->be_lun->lun_type, sentry)) 7641 continue; 7642 descr = &all->descr[num++]; 7643 descr->opcode = i; 7644 scsi_ulto2b(j, descr->service_action); 7645 descr->flags = RSO_SERVACTV; 7646 scsi_ulto2b(sentry->length, 7647 descr->cdb_length); 7648 } 7649 } else { 7650 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7651 entry)) 7652 continue; 7653 descr = &all->descr[num++]; 7654 descr->opcode = i; 7655 scsi_ulto2b(0, descr->service_action); 7656 descr->flags = 0; 7657 scsi_ulto2b(entry->length, descr->cdb_length); 7658 } 7659 } 7660 scsi_ulto4b( 7661 num * sizeof(struct scsi_report_supported_opcodes_descr), 7662 all->length); 7663 break; 7664 case RSO_OPTIONS_OC: 7665 one = (struct scsi_report_supported_opcodes_one *) 7666 ctsio->kern_data_ptr; 7667 entry = &ctl_cmd_table[opcode]; 7668 goto fill_one; 7669 case RSO_OPTIONS_OC_SA: 7670 one = (struct scsi_report_supported_opcodes_one *) 7671 ctsio->kern_data_ptr; 7672 entry = &ctl_cmd_table[opcode]; 7673 entry = &((const struct ctl_cmd_entry *) 7674 entry->execute)[service_action]; 7675fill_one: 7676 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7677 one->support = 3; 7678 scsi_ulto2b(entry->length, one->cdb_length); 7679 one->cdb_usage[0] = opcode; 7680 memcpy(&one->cdb_usage[1], entry->usage, 7681 entry->length - 1); 7682 } else 7683 one->support = 1; 7684 break; 7685 } 7686 7687 ctl_set_success(ctsio); 7688 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7689 ctsio->be_move_done = ctl_config_move_done; 7690 ctl_datamove((union ctl_io *)ctsio); 7691 return(retval); 7692} 7693 7694int 7695ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7696{ 7697 struct scsi_report_supported_tmf *cdb; 7698 struct scsi_report_supported_tmf_data *data; 7699 int retval; 7700 int alloc_len, total_len; 7701 7702 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7703 7704 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7705 7706 retval = CTL_RETVAL_COMPLETE; 7707 7708 total_len = sizeof(struct scsi_report_supported_tmf_data); 7709 alloc_len = scsi_4btoul(cdb->length); 7710 7711 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7712 7713 ctsio->kern_sg_entries = 0; 7714 7715 if (total_len < alloc_len) { 7716 ctsio->residual = alloc_len - total_len; 7717 ctsio->kern_data_len = total_len; 7718 ctsio->kern_total_len = total_len; 7719 } else { 7720 ctsio->residual = 0; 7721 ctsio->kern_data_len = alloc_len; 7722 ctsio->kern_total_len = alloc_len; 7723 } 7724 ctsio->kern_data_resid = 0; 7725 ctsio->kern_rel_offset = 0; 7726 7727 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7728 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7729 data->byte2 |= RST_ITNRS; 7730 7731 ctl_set_success(ctsio); 7732 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7733 ctsio->be_move_done = ctl_config_move_done; 7734 ctl_datamove((union ctl_io *)ctsio); 7735 return (retval); 7736} 7737 7738int 7739ctl_report_timestamp(struct ctl_scsiio *ctsio) 7740{ 7741 struct scsi_report_timestamp *cdb; 7742 struct scsi_report_timestamp_data *data; 7743 struct timeval tv; 7744 int64_t timestamp; 7745 int retval; 7746 int alloc_len, total_len; 7747 7748 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7749 7750 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7751 7752 retval = CTL_RETVAL_COMPLETE; 7753 7754 total_len = sizeof(struct scsi_report_timestamp_data); 7755 alloc_len = scsi_4btoul(cdb->length); 7756 7757 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7758 7759 ctsio->kern_sg_entries = 0; 7760 7761 if (total_len < alloc_len) { 7762 ctsio->residual = alloc_len - total_len; 7763 ctsio->kern_data_len = total_len; 7764 ctsio->kern_total_len = total_len; 7765 } else { 7766 ctsio->residual = 0; 7767 ctsio->kern_data_len = alloc_len; 7768 ctsio->kern_total_len = alloc_len; 7769 } 7770 ctsio->kern_data_resid = 0; 7771 ctsio->kern_rel_offset = 0; 7772 7773 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7774 scsi_ulto2b(sizeof(*data) - 2, data->length); 7775 data->origin = RTS_ORIG_OUTSIDE; 7776 getmicrotime(&tv); 7777 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7778 scsi_ulto4b(timestamp >> 16, data->timestamp); 7779 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7780 7781 ctl_set_success(ctsio); 7782 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7783 ctsio->be_move_done = ctl_config_move_done; 7784 ctl_datamove((union ctl_io *)ctsio); 7785 return (retval); 7786} 7787 7788int 7789ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7790{ 7791 struct scsi_per_res_in *cdb; 7792 int alloc_len, total_len = 0; 7793 /* struct scsi_per_res_in_rsrv in_data; */ 7794 struct ctl_lun *lun; 7795 struct ctl_softc *softc; 7796 uint64_t key; 7797 7798 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7799 7800 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7801 7802 alloc_len = scsi_2btoul(cdb->length); 7803 7804 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7805 softc = lun->ctl_softc; 7806 7807retry: 7808 mtx_lock(&lun->lun_lock); 7809 switch (cdb->action) { 7810 case SPRI_RK: /* read keys */ 7811 total_len = sizeof(struct scsi_per_res_in_keys) + 7812 lun->pr_key_count * 7813 sizeof(struct scsi_per_res_key); 7814 break; 7815 case SPRI_RR: /* read reservation */ 7816 if (lun->flags & CTL_LUN_PR_RESERVED) 7817 total_len = sizeof(struct scsi_per_res_in_rsrv); 7818 else 7819 total_len = sizeof(struct scsi_per_res_in_header); 7820 break; 7821 case SPRI_RC: /* report capabilities */ 7822 total_len = sizeof(struct scsi_per_res_cap); 7823 break; 7824 case SPRI_RS: /* read full status */ 7825 total_len = sizeof(struct scsi_per_res_in_header) + 7826 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7827 lun->pr_key_count; 7828 break; 7829 default: 7830 panic("Invalid PR type %x", cdb->action); 7831 } 7832 mtx_unlock(&lun->lun_lock); 7833 7834 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7835 7836 if (total_len < alloc_len) { 7837 ctsio->residual = alloc_len - total_len; 7838 ctsio->kern_data_len = total_len; 7839 ctsio->kern_total_len = total_len; 7840 } else { 7841 ctsio->residual = 0; 7842 ctsio->kern_data_len = alloc_len; 7843 ctsio->kern_total_len = alloc_len; 7844 } 7845 7846 ctsio->kern_data_resid = 0; 7847 ctsio->kern_rel_offset = 0; 7848 ctsio->kern_sg_entries = 0; 7849 7850 mtx_lock(&lun->lun_lock); 7851 switch (cdb->action) { 7852 case SPRI_RK: { // read keys 7853 struct scsi_per_res_in_keys *res_keys; 7854 int i, key_count; 7855 7856 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7857 7858 /* 7859 * We had to drop the lock to allocate our buffer, which 7860 * leaves time for someone to come in with another 7861 * persistent reservation. (That is unlikely, though, 7862 * since this should be the only persistent reservation 7863 * command active right now.) 7864 */ 7865 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7866 (lun->pr_key_count * 7867 sizeof(struct scsi_per_res_key)))){ 7868 mtx_unlock(&lun->lun_lock); 7869 free(ctsio->kern_data_ptr, M_CTL); 7870 printf("%s: reservation length changed, retrying\n", 7871 __func__); 7872 goto retry; 7873 } 7874 7875 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7876 7877 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7878 lun->pr_key_count, res_keys->header.length); 7879 7880 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7881 if ((key = ctl_get_prkey(lun, i)) == 0) 7882 continue; 7883 7884 /* 7885 * We used lun->pr_key_count to calculate the 7886 * size to allocate. If it turns out the number of 7887 * initiators with the registered flag set is 7888 * larger than that (i.e. they haven't been kept in 7889 * sync), we've got a problem. 7890 */ 7891 if (key_count >= lun->pr_key_count) { 7892#ifdef NEEDTOPORT 7893 csevent_log(CSC_CTL | CSC_SHELF_SW | 7894 CTL_PR_ERROR, 7895 csevent_LogType_Fault, 7896 csevent_AlertLevel_Yellow, 7897 csevent_FRU_ShelfController, 7898 csevent_FRU_Firmware, 7899 csevent_FRU_Unknown, 7900 "registered keys %d >= key " 7901 "count %d", key_count, 7902 lun->pr_key_count); 7903#endif 7904 key_count++; 7905 continue; 7906 } 7907 scsi_u64to8b(key, res_keys->keys[key_count].key); 7908 key_count++; 7909 } 7910 break; 7911 } 7912 case SPRI_RR: { // read reservation 7913 struct scsi_per_res_in_rsrv *res; 7914 int tmp_len, header_only; 7915 7916 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7917 7918 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7919 7920 if (lun->flags & CTL_LUN_PR_RESERVED) 7921 { 7922 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7923 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7924 res->header.length); 7925 header_only = 0; 7926 } else { 7927 tmp_len = sizeof(struct scsi_per_res_in_header); 7928 scsi_ulto4b(0, res->header.length); 7929 header_only = 1; 7930 } 7931 7932 /* 7933 * We had to drop the lock to allocate our buffer, which 7934 * leaves time for someone to come in with another 7935 * persistent reservation. (That is unlikely, though, 7936 * since this should be the only persistent reservation 7937 * command active right now.) 7938 */ 7939 if (tmp_len != total_len) { 7940 mtx_unlock(&lun->lun_lock); 7941 free(ctsio->kern_data_ptr, M_CTL); 7942 printf("%s: reservation status changed, retrying\n", 7943 __func__); 7944 goto retry; 7945 } 7946 7947 /* 7948 * No reservation held, so we're done. 7949 */ 7950 if (header_only != 0) 7951 break; 7952 7953 /* 7954 * If the registration is an All Registrants type, the key 7955 * is 0, since it doesn't really matter. 7956 */ 7957 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7958 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7959 res->data.reservation); 7960 } 7961 res->data.scopetype = lun->res_type; 7962 break; 7963 } 7964 case SPRI_RC: //report capabilities 7965 { 7966 struct scsi_per_res_cap *res_cap; 7967 uint16_t type_mask; 7968 7969 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7970 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7971 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7972 type_mask = SPRI_TM_WR_EX_AR | 7973 SPRI_TM_EX_AC_RO | 7974 SPRI_TM_WR_EX_RO | 7975 SPRI_TM_EX_AC | 7976 SPRI_TM_WR_EX | 7977 SPRI_TM_EX_AC_AR; 7978 scsi_ulto2b(type_mask, res_cap->type_mask); 7979 break; 7980 } 7981 case SPRI_RS: { // read full status 7982 struct scsi_per_res_in_full *res_status; 7983 struct scsi_per_res_in_full_desc *res_desc; 7984 struct ctl_port *port; 7985 int i, len; 7986 7987 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7988 7989 /* 7990 * We had to drop the lock to allocate our buffer, which 7991 * leaves time for someone to come in with another 7992 * persistent reservation. (That is unlikely, though, 7993 * since this should be the only persistent reservation 7994 * command active right now.) 7995 */ 7996 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7997 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7998 lun->pr_key_count)){ 7999 mtx_unlock(&lun->lun_lock); 8000 free(ctsio->kern_data_ptr, M_CTL); 8001 printf("%s: reservation length changed, retrying\n", 8002 __func__); 8003 goto retry; 8004 } 8005 8006 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8007 8008 res_desc = &res_status->desc[0]; 8009 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 8010 if ((key = ctl_get_prkey(lun, i)) == 0) 8011 continue; 8012 8013 scsi_u64to8b(key, res_desc->res_key.key); 8014 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8015 (lun->pr_res_idx == i || 8016 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8017 res_desc->flags = SPRI_FULL_R_HOLDER; 8018 res_desc->scopetype = lun->res_type; 8019 } 8020 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8021 res_desc->rel_trgt_port_id); 8022 len = 0; 8023 port = softc->ctl_ports[ 8024 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 8025 if (port != NULL) 8026 len = ctl_create_iid(port, 8027 i % CTL_MAX_INIT_PER_PORT, 8028 res_desc->transport_id); 8029 scsi_ulto4b(len, res_desc->additional_length); 8030 res_desc = (struct scsi_per_res_in_full_desc *) 8031 &res_desc->transport_id[len]; 8032 } 8033 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8034 res_status->header.length); 8035 break; 8036 } 8037 default: 8038 /* 8039 * This is a bug, because we just checked for this above, 8040 * and should have returned an error. 8041 */ 8042 panic("Invalid PR type %x", cdb->action); 8043 break; /* NOTREACHED */ 8044 } 8045 mtx_unlock(&lun->lun_lock); 8046 8047 ctl_set_success(ctsio); 8048 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8049 ctsio->be_move_done = ctl_config_move_done; 8050 ctl_datamove((union ctl_io *)ctsio); 8051 return (CTL_RETVAL_COMPLETE); 8052} 8053 8054static void 8055ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 8056{ 8057 int off = lun->ctl_softc->persis_offset; 8058 8059 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 8060 ctl_est_ua(lun, residx - off, ua); 8061} 8062 8063/* 8064 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8065 * it should return. 8066 */ 8067static int 8068ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8069 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8070 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8071 struct scsi_per_res_out_parms* param) 8072{ 8073 union ctl_ha_msg persis_io; 8074 int retval, i; 8075 int isc_retval; 8076 8077 retval = 0; 8078 8079 mtx_lock(&lun->lun_lock); 8080 if (sa_res_key == 0) { 8081 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8082 /* validate scope and type */ 8083 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8084 SPR_LU_SCOPE) { 8085 mtx_unlock(&lun->lun_lock); 8086 ctl_set_invalid_field(/*ctsio*/ ctsio, 8087 /*sks_valid*/ 1, 8088 /*command*/ 1, 8089 /*field*/ 2, 8090 /*bit_valid*/ 1, 8091 /*bit*/ 4); 8092 ctl_done((union ctl_io *)ctsio); 8093 return (1); 8094 } 8095 8096 if (type>8 || type==2 || type==4 || type==0) { 8097 mtx_unlock(&lun->lun_lock); 8098 ctl_set_invalid_field(/*ctsio*/ ctsio, 8099 /*sks_valid*/ 1, 8100 /*command*/ 1, 8101 /*field*/ 2, 8102 /*bit_valid*/ 1, 8103 /*bit*/ 0); 8104 ctl_done((union ctl_io *)ctsio); 8105 return (1); 8106 } 8107 8108 /* 8109 * Unregister everybody else and build UA for 8110 * them 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 ctl_clr_prkey(lun, i); 8117 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8118 } 8119 lun->pr_key_count = 1; 8120 lun->res_type = type; 8121 if (lun->res_type != SPR_TYPE_WR_EX_AR 8122 && lun->res_type != SPR_TYPE_EX_AC_AR) 8123 lun->pr_res_idx = residx; 8124 8125 /* send msg to other side */ 8126 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8127 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8128 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8129 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8130 persis_io.pr.pr_info.res_type = type; 8131 memcpy(persis_io.pr.pr_info.sa_res_key, 8132 param->serv_act_res_key, 8133 sizeof(param->serv_act_res_key)); 8134 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8135 &persis_io, sizeof(persis_io), 0)) > 8136 CTL_HA_STATUS_SUCCESS) { 8137 printf("CTL:Persis Out error returned " 8138 "from ctl_ha_msg_send %d\n", 8139 isc_retval); 8140 } 8141 } else { 8142 /* not all registrants */ 8143 mtx_unlock(&lun->lun_lock); 8144 free(ctsio->kern_data_ptr, M_CTL); 8145 ctl_set_invalid_field(ctsio, 8146 /*sks_valid*/ 1, 8147 /*command*/ 0, 8148 /*field*/ 8, 8149 /*bit_valid*/ 0, 8150 /*bit*/ 0); 8151 ctl_done((union ctl_io *)ctsio); 8152 return (1); 8153 } 8154 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8155 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8156 int found = 0; 8157 8158 if (res_key == sa_res_key) { 8159 /* special case */ 8160 /* 8161 * The spec implies this is not good but doesn't 8162 * say what to do. There are two choices either 8163 * generate a res conflict or check condition 8164 * with illegal field in parameter data. Since 8165 * that is what is done when the sa_res_key is 8166 * zero I'll take that approach since this has 8167 * to do with the sa_res_key. 8168 */ 8169 mtx_unlock(&lun->lun_lock); 8170 free(ctsio->kern_data_ptr, M_CTL); 8171 ctl_set_invalid_field(ctsio, 8172 /*sks_valid*/ 1, 8173 /*command*/ 0, 8174 /*field*/ 8, 8175 /*bit_valid*/ 0, 8176 /*bit*/ 0); 8177 ctl_done((union ctl_io *)ctsio); 8178 return (1); 8179 } 8180 8181 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8182 if (ctl_get_prkey(lun, i) != sa_res_key) 8183 continue; 8184 8185 found = 1; 8186 ctl_clr_prkey(lun, i); 8187 lun->pr_key_count--; 8188 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8189 } 8190 if (!found) { 8191 mtx_unlock(&lun->lun_lock); 8192 free(ctsio->kern_data_ptr, M_CTL); 8193 ctl_set_reservation_conflict(ctsio); 8194 ctl_done((union ctl_io *)ctsio); 8195 return (CTL_RETVAL_COMPLETE); 8196 } 8197 /* send msg to other side */ 8198 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8199 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8200 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8201 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8202 persis_io.pr.pr_info.res_type = type; 8203 memcpy(persis_io.pr.pr_info.sa_res_key, 8204 param->serv_act_res_key, 8205 sizeof(param->serv_act_res_key)); 8206 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8207 &persis_io, sizeof(persis_io), 0)) > 8208 CTL_HA_STATUS_SUCCESS) { 8209 printf("CTL:Persis Out error returned from " 8210 "ctl_ha_msg_send %d\n", isc_retval); 8211 } 8212 } else { 8213 /* Reserved but not all registrants */ 8214 /* sa_res_key is res holder */ 8215 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8216 /* validate scope and type */ 8217 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8218 SPR_LU_SCOPE) { 8219 mtx_unlock(&lun->lun_lock); 8220 ctl_set_invalid_field(/*ctsio*/ ctsio, 8221 /*sks_valid*/ 1, 8222 /*command*/ 1, 8223 /*field*/ 2, 8224 /*bit_valid*/ 1, 8225 /*bit*/ 4); 8226 ctl_done((union ctl_io *)ctsio); 8227 return (1); 8228 } 8229 8230 if (type>8 || type==2 || type==4 || type==0) { 8231 mtx_unlock(&lun->lun_lock); 8232 ctl_set_invalid_field(/*ctsio*/ ctsio, 8233 /*sks_valid*/ 1, 8234 /*command*/ 1, 8235 /*field*/ 2, 8236 /*bit_valid*/ 1, 8237 /*bit*/ 0); 8238 ctl_done((union ctl_io *)ctsio); 8239 return (1); 8240 } 8241 8242 /* 8243 * Do the following: 8244 * if sa_res_key != res_key remove all 8245 * registrants w/sa_res_key and generate UA 8246 * for these registrants(Registrations 8247 * Preempted) if it wasn't an exclusive 8248 * reservation generate UA(Reservations 8249 * Preempted) for all other registered nexuses 8250 * if the type has changed. Establish the new 8251 * reservation and holder. If res_key and 8252 * sa_res_key are the same do the above 8253 * except don't unregister the res holder. 8254 */ 8255 8256 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8257 if (i == residx || ctl_get_prkey(lun, i) == 0) 8258 continue; 8259 8260 if (sa_res_key == ctl_get_prkey(lun, i)) { 8261 ctl_clr_prkey(lun, i); 8262 lun->pr_key_count--; 8263 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8264 } else if (type != lun->res_type 8265 && (lun->res_type == SPR_TYPE_WR_EX_RO 8266 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8267 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8268 } 8269 } 8270 lun->res_type = type; 8271 if (lun->res_type != SPR_TYPE_WR_EX_AR 8272 && lun->res_type != SPR_TYPE_EX_AC_AR) 8273 lun->pr_res_idx = residx; 8274 else 8275 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8276 8277 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8278 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8279 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8280 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8281 persis_io.pr.pr_info.res_type = type; 8282 memcpy(persis_io.pr.pr_info.sa_res_key, 8283 param->serv_act_res_key, 8284 sizeof(param->serv_act_res_key)); 8285 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8286 &persis_io, sizeof(persis_io), 0)) > 8287 CTL_HA_STATUS_SUCCESS) { 8288 printf("CTL:Persis Out error returned " 8289 "from ctl_ha_msg_send %d\n", 8290 isc_retval); 8291 } 8292 } else { 8293 /* 8294 * sa_res_key is not the res holder just 8295 * remove registrants 8296 */ 8297 int found=0; 8298 8299 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8300 if (sa_res_key != ctl_get_prkey(lun, i)) 8301 continue; 8302 8303 found = 1; 8304 ctl_clr_prkey(lun, i); 8305 lun->pr_key_count--; 8306 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8307 } 8308 8309 if (!found) { 8310 mtx_unlock(&lun->lun_lock); 8311 free(ctsio->kern_data_ptr, M_CTL); 8312 ctl_set_reservation_conflict(ctsio); 8313 ctl_done((union ctl_io *)ctsio); 8314 return (1); 8315 } 8316 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8317 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8318 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8319 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8320 persis_io.pr.pr_info.res_type = type; 8321 memcpy(persis_io.pr.pr_info.sa_res_key, 8322 param->serv_act_res_key, 8323 sizeof(param->serv_act_res_key)); 8324 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8325 &persis_io, sizeof(persis_io), 0)) > 8326 CTL_HA_STATUS_SUCCESS) { 8327 printf("CTL:Persis Out error returned " 8328 "from ctl_ha_msg_send %d\n", 8329 isc_retval); 8330 } 8331 } 8332 } 8333 8334 lun->PRGeneration++; 8335 mtx_unlock(&lun->lun_lock); 8336 8337 return (retval); 8338} 8339 8340static void 8341ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8342{ 8343 uint64_t sa_res_key; 8344 int i; 8345 8346 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8347 8348 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8349 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8350 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8351 if (sa_res_key == 0) { 8352 /* 8353 * Unregister everybody else and build UA for 8354 * them 8355 */ 8356 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8357 if (i == msg->pr.pr_info.residx || 8358 ctl_get_prkey(lun, i) == 0) 8359 continue; 8360 8361 ctl_clr_prkey(lun, i); 8362 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8363 } 8364 8365 lun->pr_key_count = 1; 8366 lun->res_type = msg->pr.pr_info.res_type; 8367 if (lun->res_type != SPR_TYPE_WR_EX_AR 8368 && lun->res_type != SPR_TYPE_EX_AC_AR) 8369 lun->pr_res_idx = msg->pr.pr_info.residx; 8370 } else { 8371 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8372 if (sa_res_key == ctl_get_prkey(lun, i)) 8373 continue; 8374 8375 ctl_clr_prkey(lun, i); 8376 lun->pr_key_count--; 8377 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8378 } 8379 } 8380 } else { 8381 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8382 if (i == msg->pr.pr_info.residx || 8383 ctl_get_prkey(lun, i) == 0) 8384 continue; 8385 8386 if (sa_res_key == ctl_get_prkey(lun, i)) { 8387 ctl_clr_prkey(lun, i); 8388 lun->pr_key_count--; 8389 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8390 } else if (msg->pr.pr_info.res_type != lun->res_type 8391 && (lun->res_type == SPR_TYPE_WR_EX_RO 8392 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8393 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8394 } 8395 } 8396 lun->res_type = msg->pr.pr_info.res_type; 8397 if (lun->res_type != SPR_TYPE_WR_EX_AR 8398 && lun->res_type != SPR_TYPE_EX_AC_AR) 8399 lun->pr_res_idx = msg->pr.pr_info.residx; 8400 else 8401 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8402 } 8403 lun->PRGeneration++; 8404 8405} 8406 8407 8408int 8409ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8410{ 8411 int retval; 8412 int isc_retval; 8413 u_int32_t param_len; 8414 struct scsi_per_res_out *cdb; 8415 struct ctl_lun *lun; 8416 struct scsi_per_res_out_parms* param; 8417 struct ctl_softc *softc; 8418 uint32_t residx; 8419 uint64_t res_key, sa_res_key, key; 8420 uint8_t type; 8421 union ctl_ha_msg persis_io; 8422 int i; 8423 8424 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8425 8426 retval = CTL_RETVAL_COMPLETE; 8427 8428 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8429 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8430 softc = lun->ctl_softc; 8431 8432 /* 8433 * We only support whole-LUN scope. The scope & type are ignored for 8434 * register, register and ignore existing key and clear. 8435 * We sometimes ignore scope and type on preempts too!! 8436 * Verify reservation type here as well. 8437 */ 8438 type = cdb->scope_type & SPR_TYPE_MASK; 8439 if ((cdb->action == SPRO_RESERVE) 8440 || (cdb->action == SPRO_RELEASE)) { 8441 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8442 ctl_set_invalid_field(/*ctsio*/ ctsio, 8443 /*sks_valid*/ 1, 8444 /*command*/ 1, 8445 /*field*/ 2, 8446 /*bit_valid*/ 1, 8447 /*bit*/ 4); 8448 ctl_done((union ctl_io *)ctsio); 8449 return (CTL_RETVAL_COMPLETE); 8450 } 8451 8452 if (type>8 || type==2 || type==4 || type==0) { 8453 ctl_set_invalid_field(/*ctsio*/ ctsio, 8454 /*sks_valid*/ 1, 8455 /*command*/ 1, 8456 /*field*/ 2, 8457 /*bit_valid*/ 1, 8458 /*bit*/ 0); 8459 ctl_done((union ctl_io *)ctsio); 8460 return (CTL_RETVAL_COMPLETE); 8461 } 8462 } 8463 8464 param_len = scsi_4btoul(cdb->length); 8465 8466 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8467 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8468 ctsio->kern_data_len = param_len; 8469 ctsio->kern_total_len = param_len; 8470 ctsio->kern_data_resid = 0; 8471 ctsio->kern_rel_offset = 0; 8472 ctsio->kern_sg_entries = 0; 8473 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8474 ctsio->be_move_done = ctl_config_move_done; 8475 ctl_datamove((union ctl_io *)ctsio); 8476 8477 return (CTL_RETVAL_COMPLETE); 8478 } 8479 8480 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8481 8482 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8483 res_key = scsi_8btou64(param->res_key.key); 8484 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8485 8486 /* 8487 * Validate the reservation key here except for SPRO_REG_IGNO 8488 * This must be done for all other service actions 8489 */ 8490 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8491 mtx_lock(&lun->lun_lock); 8492 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8493 if (res_key != key) { 8494 /* 8495 * The current key passed in doesn't match 8496 * the one the initiator previously 8497 * registered. 8498 */ 8499 mtx_unlock(&lun->lun_lock); 8500 free(ctsio->kern_data_ptr, M_CTL); 8501 ctl_set_reservation_conflict(ctsio); 8502 ctl_done((union ctl_io *)ctsio); 8503 return (CTL_RETVAL_COMPLETE); 8504 } 8505 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8506 /* 8507 * We are not registered 8508 */ 8509 mtx_unlock(&lun->lun_lock); 8510 free(ctsio->kern_data_ptr, M_CTL); 8511 ctl_set_reservation_conflict(ctsio); 8512 ctl_done((union ctl_io *)ctsio); 8513 return (CTL_RETVAL_COMPLETE); 8514 } else if (res_key != 0) { 8515 /* 8516 * We are not registered and trying to register but 8517 * the register key isn't zero. 8518 */ 8519 mtx_unlock(&lun->lun_lock); 8520 free(ctsio->kern_data_ptr, M_CTL); 8521 ctl_set_reservation_conflict(ctsio); 8522 ctl_done((union ctl_io *)ctsio); 8523 return (CTL_RETVAL_COMPLETE); 8524 } 8525 mtx_unlock(&lun->lun_lock); 8526 } 8527 8528 switch (cdb->action & SPRO_ACTION_MASK) { 8529 case SPRO_REGISTER: 8530 case SPRO_REG_IGNO: { 8531 8532#if 0 8533 printf("Registration received\n"); 8534#endif 8535 8536 /* 8537 * We don't support any of these options, as we report in 8538 * the read capabilities request (see 8539 * ctl_persistent_reserve_in(), above). 8540 */ 8541 if ((param->flags & SPR_SPEC_I_PT) 8542 || (param->flags & SPR_ALL_TG_PT) 8543 || (param->flags & SPR_APTPL)) { 8544 int bit_ptr; 8545 8546 if (param->flags & SPR_APTPL) 8547 bit_ptr = 0; 8548 else if (param->flags & SPR_ALL_TG_PT) 8549 bit_ptr = 2; 8550 else /* SPR_SPEC_I_PT */ 8551 bit_ptr = 3; 8552 8553 free(ctsio->kern_data_ptr, M_CTL); 8554 ctl_set_invalid_field(ctsio, 8555 /*sks_valid*/ 1, 8556 /*command*/ 0, 8557 /*field*/ 20, 8558 /*bit_valid*/ 1, 8559 /*bit*/ bit_ptr); 8560 ctl_done((union ctl_io *)ctsio); 8561 return (CTL_RETVAL_COMPLETE); 8562 } 8563 8564 mtx_lock(&lun->lun_lock); 8565 8566 /* 8567 * The initiator wants to clear the 8568 * key/unregister. 8569 */ 8570 if (sa_res_key == 0) { 8571 if ((res_key == 0 8572 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8573 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8574 && ctl_get_prkey(lun, residx) == 0)) { 8575 mtx_unlock(&lun->lun_lock); 8576 goto done; 8577 } 8578 8579 ctl_clr_prkey(lun, residx); 8580 lun->pr_key_count--; 8581 8582 if (residx == lun->pr_res_idx) { 8583 lun->flags &= ~CTL_LUN_PR_RESERVED; 8584 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8585 8586 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8587 || lun->res_type == SPR_TYPE_EX_AC_RO) 8588 && lun->pr_key_count) { 8589 /* 8590 * If the reservation is a registrants 8591 * only type we need to generate a UA 8592 * for other registered inits. The 8593 * sense code should be RESERVATIONS 8594 * RELEASED 8595 */ 8596 8597 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8598 if (ctl_get_prkey(lun, i + 8599 softc->persis_offset) == 0) 8600 continue; 8601 ctl_est_ua(lun, i, 8602 CTL_UA_RES_RELEASE); 8603 } 8604 } 8605 lun->res_type = 0; 8606 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8607 if (lun->pr_key_count==0) { 8608 lun->flags &= ~CTL_LUN_PR_RESERVED; 8609 lun->res_type = 0; 8610 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8611 } 8612 } 8613 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8614 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8615 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8616 persis_io.pr.pr_info.residx = residx; 8617 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8618 &persis_io, sizeof(persis_io), 0 )) > 8619 CTL_HA_STATUS_SUCCESS) { 8620 printf("CTL:Persis Out error returned from " 8621 "ctl_ha_msg_send %d\n", isc_retval); 8622 } 8623 } else /* sa_res_key != 0 */ { 8624 8625 /* 8626 * If we aren't registered currently then increment 8627 * the key count and set the registered flag. 8628 */ 8629 ctl_alloc_prkey(lun, residx); 8630 if (ctl_get_prkey(lun, residx) == 0) 8631 lun->pr_key_count++; 8632 ctl_set_prkey(lun, residx, sa_res_key); 8633 8634 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8635 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8636 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8637 persis_io.pr.pr_info.residx = residx; 8638 memcpy(persis_io.pr.pr_info.sa_res_key, 8639 param->serv_act_res_key, 8640 sizeof(param->serv_act_res_key)); 8641 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8642 &persis_io, sizeof(persis_io), 0)) > 8643 CTL_HA_STATUS_SUCCESS) { 8644 printf("CTL:Persis Out error returned from " 8645 "ctl_ha_msg_send %d\n", isc_retval); 8646 } 8647 } 8648 lun->PRGeneration++; 8649 mtx_unlock(&lun->lun_lock); 8650 8651 break; 8652 } 8653 case SPRO_RESERVE: 8654#if 0 8655 printf("Reserve executed type %d\n", type); 8656#endif 8657 mtx_lock(&lun->lun_lock); 8658 if (lun->flags & CTL_LUN_PR_RESERVED) { 8659 /* 8660 * if this isn't the reservation holder and it's 8661 * not a "all registrants" type or if the type is 8662 * different then we have a conflict 8663 */ 8664 if ((lun->pr_res_idx != residx 8665 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8666 || lun->res_type != type) { 8667 mtx_unlock(&lun->lun_lock); 8668 free(ctsio->kern_data_ptr, M_CTL); 8669 ctl_set_reservation_conflict(ctsio); 8670 ctl_done((union ctl_io *)ctsio); 8671 return (CTL_RETVAL_COMPLETE); 8672 } 8673 mtx_unlock(&lun->lun_lock); 8674 } else /* create a reservation */ { 8675 /* 8676 * If it's not an "all registrants" type record 8677 * reservation holder 8678 */ 8679 if (type != SPR_TYPE_WR_EX_AR 8680 && type != SPR_TYPE_EX_AC_AR) 8681 lun->pr_res_idx = residx; /* Res holder */ 8682 else 8683 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8684 8685 lun->flags |= CTL_LUN_PR_RESERVED; 8686 lun->res_type = type; 8687 8688 mtx_unlock(&lun->lun_lock); 8689 8690 /* send msg to other side */ 8691 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8692 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8693 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8694 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8695 persis_io.pr.pr_info.res_type = type; 8696 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8697 &persis_io, sizeof(persis_io), 0)) > 8698 CTL_HA_STATUS_SUCCESS) { 8699 printf("CTL:Persis Out error returned from " 8700 "ctl_ha_msg_send %d\n", isc_retval); 8701 } 8702 } 8703 break; 8704 8705 case SPRO_RELEASE: 8706 mtx_lock(&lun->lun_lock); 8707 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8708 /* No reservation exists return good status */ 8709 mtx_unlock(&lun->lun_lock); 8710 goto done; 8711 } 8712 /* 8713 * Is this nexus a reservation holder? 8714 */ 8715 if (lun->pr_res_idx != residx 8716 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8717 /* 8718 * not a res holder return good status but 8719 * do nothing 8720 */ 8721 mtx_unlock(&lun->lun_lock); 8722 goto done; 8723 } 8724 8725 if (lun->res_type != type) { 8726 mtx_unlock(&lun->lun_lock); 8727 free(ctsio->kern_data_ptr, M_CTL); 8728 ctl_set_illegal_pr_release(ctsio); 8729 ctl_done((union ctl_io *)ctsio); 8730 return (CTL_RETVAL_COMPLETE); 8731 } 8732 8733 /* okay to release */ 8734 lun->flags &= ~CTL_LUN_PR_RESERVED; 8735 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8736 lun->res_type = 0; 8737 8738 /* 8739 * if this isn't an exclusive access 8740 * res generate UA for all other 8741 * registrants. 8742 */ 8743 if (type != SPR_TYPE_EX_AC 8744 && type != SPR_TYPE_WR_EX) { 8745 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8746 if (i == residx || 8747 ctl_get_prkey(lun, 8748 i + softc->persis_offset) == 0) 8749 continue; 8750 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8751 } 8752 } 8753 mtx_unlock(&lun->lun_lock); 8754 /* Send msg to other side */ 8755 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8756 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8757 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8758 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8759 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8760 printf("CTL:Persis Out error returned from " 8761 "ctl_ha_msg_send %d\n", isc_retval); 8762 } 8763 break; 8764 8765 case SPRO_CLEAR: 8766 /* send msg to other side */ 8767 8768 mtx_lock(&lun->lun_lock); 8769 lun->flags &= ~CTL_LUN_PR_RESERVED; 8770 lun->res_type = 0; 8771 lun->pr_key_count = 0; 8772 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8773 8774 ctl_clr_prkey(lun, residx); 8775 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8776 if (ctl_get_prkey(lun, i) != 0) { 8777 ctl_clr_prkey(lun, i); 8778 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8779 } 8780 lun->PRGeneration++; 8781 mtx_unlock(&lun->lun_lock); 8782 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8783 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8784 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8785 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8786 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8787 printf("CTL:Persis Out error returned from " 8788 "ctl_ha_msg_send %d\n", isc_retval); 8789 } 8790 break; 8791 8792 case SPRO_PREEMPT: 8793 case SPRO_PRE_ABO: { 8794 int nretval; 8795 8796 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8797 residx, ctsio, cdb, param); 8798 if (nretval != 0) 8799 return (CTL_RETVAL_COMPLETE); 8800 break; 8801 } 8802 default: 8803 panic("Invalid PR type %x", cdb->action); 8804 } 8805 8806done: 8807 free(ctsio->kern_data_ptr, M_CTL); 8808 ctl_set_success(ctsio); 8809 ctl_done((union ctl_io *)ctsio); 8810 8811 return (retval); 8812} 8813 8814/* 8815 * This routine is for handling a message from the other SC pertaining to 8816 * persistent reserve out. All the error checking will have been done 8817 * so only perorming the action need be done here to keep the two 8818 * in sync. 8819 */ 8820static void 8821ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8822{ 8823 struct ctl_lun *lun; 8824 struct ctl_softc *softc; 8825 int i; 8826 uint32_t targ_lun; 8827 8828 softc = control_softc; 8829 8830 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8831 lun = softc->ctl_luns[targ_lun]; 8832 mtx_lock(&lun->lun_lock); 8833 switch(msg->pr.pr_info.action) { 8834 case CTL_PR_REG_KEY: 8835 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8836 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8837 lun->pr_key_count++; 8838 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8839 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8840 lun->PRGeneration++; 8841 break; 8842 8843 case CTL_PR_UNREG_KEY: 8844 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8845 lun->pr_key_count--; 8846 8847 /* XXX Need to see if the reservation has been released */ 8848 /* if so do we need to generate UA? */ 8849 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8850 lun->flags &= ~CTL_LUN_PR_RESERVED; 8851 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8852 8853 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8854 || lun->res_type == SPR_TYPE_EX_AC_RO) 8855 && lun->pr_key_count) { 8856 /* 8857 * If the reservation is a registrants 8858 * only type we need to generate a UA 8859 * for other registered inits. The 8860 * sense code should be RESERVATIONS 8861 * RELEASED 8862 */ 8863 8864 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8865 if (ctl_get_prkey(lun, i + 8866 softc->persis_offset) == 0) 8867 continue; 8868 8869 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8870 } 8871 } 8872 lun->res_type = 0; 8873 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8874 if (lun->pr_key_count==0) { 8875 lun->flags &= ~CTL_LUN_PR_RESERVED; 8876 lun->res_type = 0; 8877 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8878 } 8879 } 8880 lun->PRGeneration++; 8881 break; 8882 8883 case CTL_PR_RESERVE: 8884 lun->flags |= CTL_LUN_PR_RESERVED; 8885 lun->res_type = msg->pr.pr_info.res_type; 8886 lun->pr_res_idx = msg->pr.pr_info.residx; 8887 8888 break; 8889 8890 case CTL_PR_RELEASE: 8891 /* 8892 * if this isn't an exclusive access res generate UA for all 8893 * other registrants. 8894 */ 8895 if (lun->res_type != SPR_TYPE_EX_AC 8896 && lun->res_type != SPR_TYPE_WR_EX) { 8897 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8898 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8899 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8900 } 8901 8902 lun->flags &= ~CTL_LUN_PR_RESERVED; 8903 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8904 lun->res_type = 0; 8905 break; 8906 8907 case CTL_PR_PREEMPT: 8908 ctl_pro_preempt_other(lun, msg); 8909 break; 8910 case CTL_PR_CLEAR: 8911 lun->flags &= ~CTL_LUN_PR_RESERVED; 8912 lun->res_type = 0; 8913 lun->pr_key_count = 0; 8914 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8915 8916 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8917 if (ctl_get_prkey(lun, i) == 0) 8918 continue; 8919 ctl_clr_prkey(lun, i); 8920 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8921 } 8922 lun->PRGeneration++; 8923 break; 8924 } 8925 8926 mtx_unlock(&lun->lun_lock); 8927} 8928 8929int 8930ctl_read_write(struct ctl_scsiio *ctsio) 8931{ 8932 struct ctl_lun *lun; 8933 struct ctl_lba_len_flags *lbalen; 8934 uint64_t lba; 8935 uint32_t num_blocks; 8936 int flags, retval; 8937 int isread; 8938 8939 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8940 8941 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8942 8943 flags = 0; 8944 retval = CTL_RETVAL_COMPLETE; 8945 8946 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8947 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8948 switch (ctsio->cdb[0]) { 8949 case READ_6: 8950 case WRITE_6: { 8951 struct scsi_rw_6 *cdb; 8952 8953 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8954 8955 lba = scsi_3btoul(cdb->addr); 8956 /* only 5 bits are valid in the most significant address byte */ 8957 lba &= 0x1fffff; 8958 num_blocks = cdb->length; 8959 /* 8960 * This is correct according to SBC-2. 8961 */ 8962 if (num_blocks == 0) 8963 num_blocks = 256; 8964 break; 8965 } 8966 case READ_10: 8967 case WRITE_10: { 8968 struct scsi_rw_10 *cdb; 8969 8970 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8971 if (cdb->byte2 & SRW10_FUA) 8972 flags |= CTL_LLF_FUA; 8973 if (cdb->byte2 & SRW10_DPO) 8974 flags |= CTL_LLF_DPO; 8975 lba = scsi_4btoul(cdb->addr); 8976 num_blocks = scsi_2btoul(cdb->length); 8977 break; 8978 } 8979 case WRITE_VERIFY_10: { 8980 struct scsi_write_verify_10 *cdb; 8981 8982 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8983 flags |= CTL_LLF_FUA; 8984 if (cdb->byte2 & SWV_DPO) 8985 flags |= CTL_LLF_DPO; 8986 lba = scsi_4btoul(cdb->addr); 8987 num_blocks = scsi_2btoul(cdb->length); 8988 break; 8989 } 8990 case READ_12: 8991 case WRITE_12: { 8992 struct scsi_rw_12 *cdb; 8993 8994 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8995 if (cdb->byte2 & SRW12_FUA) 8996 flags |= CTL_LLF_FUA; 8997 if (cdb->byte2 & SRW12_DPO) 8998 flags |= CTL_LLF_DPO; 8999 lba = scsi_4btoul(cdb->addr); 9000 num_blocks = scsi_4btoul(cdb->length); 9001 break; 9002 } 9003 case WRITE_VERIFY_12: { 9004 struct scsi_write_verify_12 *cdb; 9005 9006 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9007 flags |= CTL_LLF_FUA; 9008 if (cdb->byte2 & SWV_DPO) 9009 flags |= CTL_LLF_DPO; 9010 lba = scsi_4btoul(cdb->addr); 9011 num_blocks = scsi_4btoul(cdb->length); 9012 break; 9013 } 9014 case READ_16: 9015 case WRITE_16: { 9016 struct scsi_rw_16 *cdb; 9017 9018 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9019 if (cdb->byte2 & SRW12_FUA) 9020 flags |= CTL_LLF_FUA; 9021 if (cdb->byte2 & SRW12_DPO) 9022 flags |= CTL_LLF_DPO; 9023 lba = scsi_8btou64(cdb->addr); 9024 num_blocks = scsi_4btoul(cdb->length); 9025 break; 9026 } 9027 case WRITE_ATOMIC_16: { 9028 struct scsi_rw_16 *cdb; 9029 9030 if (lun->be_lun->atomicblock == 0) { 9031 ctl_set_invalid_opcode(ctsio); 9032 ctl_done((union ctl_io *)ctsio); 9033 return (CTL_RETVAL_COMPLETE); 9034 } 9035 9036 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9037 if (cdb->byte2 & SRW12_FUA) 9038 flags |= CTL_LLF_FUA; 9039 if (cdb->byte2 & SRW12_DPO) 9040 flags |= CTL_LLF_DPO; 9041 lba = scsi_8btou64(cdb->addr); 9042 num_blocks = scsi_4btoul(cdb->length); 9043 if (num_blocks > lun->be_lun->atomicblock) { 9044 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9045 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9046 /*bit*/ 0); 9047 ctl_done((union ctl_io *)ctsio); 9048 return (CTL_RETVAL_COMPLETE); 9049 } 9050 break; 9051 } 9052 case WRITE_VERIFY_16: { 9053 struct scsi_write_verify_16 *cdb; 9054 9055 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9056 flags |= CTL_LLF_FUA; 9057 if (cdb->byte2 & SWV_DPO) 9058 flags |= CTL_LLF_DPO; 9059 lba = scsi_8btou64(cdb->addr); 9060 num_blocks = scsi_4btoul(cdb->length); 9061 break; 9062 } 9063 default: 9064 /* 9065 * We got a command we don't support. This shouldn't 9066 * happen, commands should be filtered out above us. 9067 */ 9068 ctl_set_invalid_opcode(ctsio); 9069 ctl_done((union ctl_io *)ctsio); 9070 9071 return (CTL_RETVAL_COMPLETE); 9072 break; /* NOTREACHED */ 9073 } 9074 9075 /* 9076 * The first check is to make sure we're in bounds, the second 9077 * check is to catch wrap-around problems. If the lba + num blocks 9078 * is less than the lba, then we've wrapped around and the block 9079 * range is invalid anyway. 9080 */ 9081 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9082 || ((lba + num_blocks) < lba)) { 9083 ctl_set_lba_out_of_range(ctsio); 9084 ctl_done((union ctl_io *)ctsio); 9085 return (CTL_RETVAL_COMPLETE); 9086 } 9087 9088 /* 9089 * According to SBC-3, a transfer length of 0 is not an error. 9090 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9091 * translates to 256 blocks for those commands. 9092 */ 9093 if (num_blocks == 0) { 9094 ctl_set_success(ctsio); 9095 ctl_done((union ctl_io *)ctsio); 9096 return (CTL_RETVAL_COMPLETE); 9097 } 9098 9099 /* Set FUA and/or DPO if caches are disabled. */ 9100 if (isread) { 9101 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9102 SCP_RCD) != 0) 9103 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9104 } else { 9105 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9106 SCP_WCE) == 0) 9107 flags |= CTL_LLF_FUA; 9108 } 9109 9110 lbalen = (struct ctl_lba_len_flags *) 9111 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9112 lbalen->lba = lba; 9113 lbalen->len = num_blocks; 9114 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9115 9116 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9117 ctsio->kern_rel_offset = 0; 9118 9119 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9120 9121 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9122 9123 return (retval); 9124} 9125 9126static int 9127ctl_cnw_cont(union ctl_io *io) 9128{ 9129 struct ctl_scsiio *ctsio; 9130 struct ctl_lun *lun; 9131 struct ctl_lba_len_flags *lbalen; 9132 int retval; 9133 9134 ctsio = &io->scsiio; 9135 ctsio->io_hdr.status = CTL_STATUS_NONE; 9136 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9137 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9138 lbalen = (struct ctl_lba_len_flags *) 9139 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9140 lbalen->flags &= ~CTL_LLF_COMPARE; 9141 lbalen->flags |= CTL_LLF_WRITE; 9142 9143 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9144 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9145 return (retval); 9146} 9147 9148int 9149ctl_cnw(struct ctl_scsiio *ctsio) 9150{ 9151 struct ctl_lun *lun; 9152 struct ctl_lba_len_flags *lbalen; 9153 uint64_t lba; 9154 uint32_t num_blocks; 9155 int flags, retval; 9156 9157 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9158 9159 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9160 9161 flags = 0; 9162 retval = CTL_RETVAL_COMPLETE; 9163 9164 switch (ctsio->cdb[0]) { 9165 case COMPARE_AND_WRITE: { 9166 struct scsi_compare_and_write *cdb; 9167 9168 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9169 if (cdb->byte2 & SRW10_FUA) 9170 flags |= CTL_LLF_FUA; 9171 if (cdb->byte2 & SRW10_DPO) 9172 flags |= CTL_LLF_DPO; 9173 lba = scsi_8btou64(cdb->addr); 9174 num_blocks = cdb->length; 9175 break; 9176 } 9177 default: 9178 /* 9179 * We got a command we don't support. This shouldn't 9180 * happen, commands should be filtered out above us. 9181 */ 9182 ctl_set_invalid_opcode(ctsio); 9183 ctl_done((union ctl_io *)ctsio); 9184 9185 return (CTL_RETVAL_COMPLETE); 9186 break; /* NOTREACHED */ 9187 } 9188 9189 /* 9190 * The first check is to make sure we're in bounds, the second 9191 * check is to catch wrap-around problems. If the lba + num blocks 9192 * is less than the lba, then we've wrapped around and the block 9193 * range is invalid anyway. 9194 */ 9195 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9196 || ((lba + num_blocks) < lba)) { 9197 ctl_set_lba_out_of_range(ctsio); 9198 ctl_done((union ctl_io *)ctsio); 9199 return (CTL_RETVAL_COMPLETE); 9200 } 9201 9202 /* 9203 * According to SBC-3, a transfer length of 0 is not an error. 9204 */ 9205 if (num_blocks == 0) { 9206 ctl_set_success(ctsio); 9207 ctl_done((union ctl_io *)ctsio); 9208 return (CTL_RETVAL_COMPLETE); 9209 } 9210 9211 /* Set FUA if write cache is disabled. */ 9212 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9213 SCP_WCE) == 0) 9214 flags |= CTL_LLF_FUA; 9215 9216 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9217 ctsio->kern_rel_offset = 0; 9218 9219 /* 9220 * Set the IO_CONT flag, so that if this I/O gets passed to 9221 * ctl_data_submit_done(), it'll get passed back to 9222 * ctl_ctl_cnw_cont() for further processing. 9223 */ 9224 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9225 ctsio->io_cont = ctl_cnw_cont; 9226 9227 lbalen = (struct ctl_lba_len_flags *) 9228 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9229 lbalen->lba = lba; 9230 lbalen->len = num_blocks; 9231 lbalen->flags = CTL_LLF_COMPARE | flags; 9232 9233 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9234 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9235 return (retval); 9236} 9237 9238int 9239ctl_verify(struct ctl_scsiio *ctsio) 9240{ 9241 struct ctl_lun *lun; 9242 struct ctl_lba_len_flags *lbalen; 9243 uint64_t lba; 9244 uint32_t num_blocks; 9245 int bytchk, flags; 9246 int retval; 9247 9248 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9249 9250 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9251 9252 bytchk = 0; 9253 flags = CTL_LLF_FUA; 9254 retval = CTL_RETVAL_COMPLETE; 9255 9256 switch (ctsio->cdb[0]) { 9257 case VERIFY_10: { 9258 struct scsi_verify_10 *cdb; 9259 9260 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9261 if (cdb->byte2 & SVFY_BYTCHK) 9262 bytchk = 1; 9263 if (cdb->byte2 & SVFY_DPO) 9264 flags |= CTL_LLF_DPO; 9265 lba = scsi_4btoul(cdb->addr); 9266 num_blocks = scsi_2btoul(cdb->length); 9267 break; 9268 } 9269 case VERIFY_12: { 9270 struct scsi_verify_12 *cdb; 9271 9272 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9273 if (cdb->byte2 & SVFY_BYTCHK) 9274 bytchk = 1; 9275 if (cdb->byte2 & SVFY_DPO) 9276 flags |= CTL_LLF_DPO; 9277 lba = scsi_4btoul(cdb->addr); 9278 num_blocks = scsi_4btoul(cdb->length); 9279 break; 9280 } 9281 case VERIFY_16: { 9282 struct scsi_rw_16 *cdb; 9283 9284 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9285 if (cdb->byte2 & SVFY_BYTCHK) 9286 bytchk = 1; 9287 if (cdb->byte2 & SVFY_DPO) 9288 flags |= CTL_LLF_DPO; 9289 lba = scsi_8btou64(cdb->addr); 9290 num_blocks = scsi_4btoul(cdb->length); 9291 break; 9292 } 9293 default: 9294 /* 9295 * We got a command we don't support. This shouldn't 9296 * happen, commands should be filtered out above us. 9297 */ 9298 ctl_set_invalid_opcode(ctsio); 9299 ctl_done((union ctl_io *)ctsio); 9300 return (CTL_RETVAL_COMPLETE); 9301 } 9302 9303 /* 9304 * The first check is to make sure we're in bounds, the second 9305 * check is to catch wrap-around problems. If the lba + num blocks 9306 * is less than the lba, then we've wrapped around and the block 9307 * range is invalid anyway. 9308 */ 9309 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9310 || ((lba + num_blocks) < lba)) { 9311 ctl_set_lba_out_of_range(ctsio); 9312 ctl_done((union ctl_io *)ctsio); 9313 return (CTL_RETVAL_COMPLETE); 9314 } 9315 9316 /* 9317 * According to SBC-3, a transfer length of 0 is not an error. 9318 */ 9319 if (num_blocks == 0) { 9320 ctl_set_success(ctsio); 9321 ctl_done((union ctl_io *)ctsio); 9322 return (CTL_RETVAL_COMPLETE); 9323 } 9324 9325 lbalen = (struct ctl_lba_len_flags *) 9326 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9327 lbalen->lba = lba; 9328 lbalen->len = num_blocks; 9329 if (bytchk) { 9330 lbalen->flags = CTL_LLF_COMPARE | flags; 9331 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9332 } else { 9333 lbalen->flags = CTL_LLF_VERIFY | flags; 9334 ctsio->kern_total_len = 0; 9335 } 9336 ctsio->kern_rel_offset = 0; 9337 9338 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9339 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9340 return (retval); 9341} 9342 9343int 9344ctl_report_luns(struct ctl_scsiio *ctsio) 9345{ 9346 struct ctl_softc *softc = control_softc; 9347 struct scsi_report_luns *cdb; 9348 struct scsi_report_luns_data *lun_data; 9349 struct ctl_lun *lun, *request_lun; 9350 struct ctl_port *port; 9351 int num_luns, retval; 9352 uint32_t alloc_len, lun_datalen; 9353 int num_filled, well_known; 9354 uint32_t initidx, targ_lun_id, lun_id; 9355 9356 retval = CTL_RETVAL_COMPLETE; 9357 well_known = 0; 9358 9359 cdb = (struct scsi_report_luns *)ctsio->cdb; 9360 port = ctl_io_port(&ctsio->io_hdr); 9361 9362 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9363 9364 mtx_lock(&softc->ctl_lock); 9365 num_luns = 0; 9366 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9367 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9368 num_luns++; 9369 } 9370 mtx_unlock(&softc->ctl_lock); 9371 9372 switch (cdb->select_report) { 9373 case RPL_REPORT_DEFAULT: 9374 case RPL_REPORT_ALL: 9375 break; 9376 case RPL_REPORT_WELLKNOWN: 9377 well_known = 1; 9378 num_luns = 0; 9379 break; 9380 default: 9381 ctl_set_invalid_field(ctsio, 9382 /*sks_valid*/ 1, 9383 /*command*/ 1, 9384 /*field*/ 2, 9385 /*bit_valid*/ 0, 9386 /*bit*/ 0); 9387 ctl_done((union ctl_io *)ctsio); 9388 return (retval); 9389 break; /* NOTREACHED */ 9390 } 9391 9392 alloc_len = scsi_4btoul(cdb->length); 9393 /* 9394 * The initiator has to allocate at least 16 bytes for this request, 9395 * so he can at least get the header and the first LUN. Otherwise 9396 * we reject the request (per SPC-3 rev 14, section 6.21). 9397 */ 9398 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9399 sizeof(struct scsi_report_luns_lundata))) { 9400 ctl_set_invalid_field(ctsio, 9401 /*sks_valid*/ 1, 9402 /*command*/ 1, 9403 /*field*/ 6, 9404 /*bit_valid*/ 0, 9405 /*bit*/ 0); 9406 ctl_done((union ctl_io *)ctsio); 9407 return (retval); 9408 } 9409 9410 request_lun = (struct ctl_lun *) 9411 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9412 9413 lun_datalen = sizeof(*lun_data) + 9414 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9415 9416 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9417 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9418 ctsio->kern_sg_entries = 0; 9419 9420 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9421 9422 mtx_lock(&softc->ctl_lock); 9423 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9424 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9425 if (lun_id >= CTL_MAX_LUNS) 9426 continue; 9427 lun = softc->ctl_luns[lun_id]; 9428 if (lun == NULL) 9429 continue; 9430 9431 if (targ_lun_id <= 0xff) { 9432 /* 9433 * Peripheral addressing method, bus number 0. 9434 */ 9435 lun_data->luns[num_filled].lundata[0] = 9436 RPL_LUNDATA_ATYP_PERIPH; 9437 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9438 num_filled++; 9439 } else if (targ_lun_id <= 0x3fff) { 9440 /* 9441 * Flat addressing method. 9442 */ 9443 lun_data->luns[num_filled].lundata[0] = 9444 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9445 lun_data->luns[num_filled].lundata[1] = 9446 (targ_lun_id & 0xff); 9447 num_filled++; 9448 } else if (targ_lun_id <= 0xffffff) { 9449 /* 9450 * Extended flat addressing method. 9451 */ 9452 lun_data->luns[num_filled].lundata[0] = 9453 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9454 scsi_ulto3b(targ_lun_id, 9455 &lun_data->luns[num_filled].lundata[1]); 9456 num_filled++; 9457 } else { 9458 printf("ctl_report_luns: bogus LUN number %jd, " 9459 "skipping\n", (intmax_t)targ_lun_id); 9460 } 9461 /* 9462 * According to SPC-3, rev 14 section 6.21: 9463 * 9464 * "The execution of a REPORT LUNS command to any valid and 9465 * installed logical unit shall clear the REPORTED LUNS DATA 9466 * HAS CHANGED unit attention condition for all logical 9467 * units of that target with respect to the requesting 9468 * initiator. A valid and installed logical unit is one 9469 * having a PERIPHERAL QUALIFIER of 000b in the standard 9470 * INQUIRY data (see 6.4.2)." 9471 * 9472 * If request_lun is NULL, the LUN this report luns command 9473 * was issued to is either disabled or doesn't exist. In that 9474 * case, we shouldn't clear any pending lun change unit 9475 * attention. 9476 */ 9477 if (request_lun != NULL) { 9478 mtx_lock(&lun->lun_lock); 9479 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE); 9480 mtx_unlock(&lun->lun_lock); 9481 } 9482 } 9483 mtx_unlock(&softc->ctl_lock); 9484 9485 /* 9486 * It's quite possible that we've returned fewer LUNs than we allocated 9487 * space for. Trim it. 9488 */ 9489 lun_datalen = sizeof(*lun_data) + 9490 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9491 9492 if (lun_datalen < alloc_len) { 9493 ctsio->residual = alloc_len - lun_datalen; 9494 ctsio->kern_data_len = lun_datalen; 9495 ctsio->kern_total_len = lun_datalen; 9496 } else { 9497 ctsio->residual = 0; 9498 ctsio->kern_data_len = alloc_len; 9499 ctsio->kern_total_len = alloc_len; 9500 } 9501 ctsio->kern_data_resid = 0; 9502 ctsio->kern_rel_offset = 0; 9503 ctsio->kern_sg_entries = 0; 9504 9505 /* 9506 * We set this to the actual data length, regardless of how much 9507 * space we actually have to return results. If the user looks at 9508 * this value, he'll know whether or not he allocated enough space 9509 * and reissue the command if necessary. We don't support well 9510 * known logical units, so if the user asks for that, return none. 9511 */ 9512 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9513 9514 /* 9515 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9516 * this request. 9517 */ 9518 ctl_set_success(ctsio); 9519 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9520 ctsio->be_move_done = ctl_config_move_done; 9521 ctl_datamove((union ctl_io *)ctsio); 9522 return (retval); 9523} 9524 9525int 9526ctl_request_sense(struct ctl_scsiio *ctsio) 9527{ 9528 struct scsi_request_sense *cdb; 9529 struct scsi_sense_data *sense_ptr; 9530 struct ctl_softc *ctl_softc; 9531 struct ctl_lun *lun; 9532 uint32_t initidx; 9533 int have_error; 9534 scsi_sense_data_type sense_format; 9535 ctl_ua_type ua_type; 9536 9537 cdb = (struct scsi_request_sense *)ctsio->cdb; 9538 9539 ctl_softc = control_softc; 9540 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9541 9542 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9543 9544 /* 9545 * Determine which sense format the user wants. 9546 */ 9547 if (cdb->byte2 & SRS_DESC) 9548 sense_format = SSD_TYPE_DESC; 9549 else 9550 sense_format = SSD_TYPE_FIXED; 9551 9552 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9553 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9554 ctsio->kern_sg_entries = 0; 9555 9556 /* 9557 * struct scsi_sense_data, which is currently set to 256 bytes, is 9558 * larger than the largest allowed value for the length field in the 9559 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9560 */ 9561 ctsio->residual = 0; 9562 ctsio->kern_data_len = cdb->length; 9563 ctsio->kern_total_len = cdb->length; 9564 9565 ctsio->kern_data_resid = 0; 9566 ctsio->kern_rel_offset = 0; 9567 ctsio->kern_sg_entries = 0; 9568 9569 /* 9570 * If we don't have a LUN, we don't have any pending sense. 9571 */ 9572 if (lun == NULL) 9573 goto no_sense; 9574 9575 have_error = 0; 9576 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9577 /* 9578 * Check for pending sense, and then for pending unit attentions. 9579 * Pending sense gets returned first, then pending unit attentions. 9580 */ 9581 mtx_lock(&lun->lun_lock); 9582#ifdef CTL_WITH_CA 9583 if (ctl_is_set(lun->have_ca, initidx)) { 9584 scsi_sense_data_type stored_format; 9585 9586 /* 9587 * Check to see which sense format was used for the stored 9588 * sense data. 9589 */ 9590 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9591 9592 /* 9593 * If the user requested a different sense format than the 9594 * one we stored, then we need to convert it to the other 9595 * format. If we're going from descriptor to fixed format 9596 * sense data, we may lose things in translation, depending 9597 * on what options were used. 9598 * 9599 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9600 * for some reason we'll just copy it out as-is. 9601 */ 9602 if ((stored_format == SSD_TYPE_FIXED) 9603 && (sense_format == SSD_TYPE_DESC)) 9604 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9605 &lun->pending_sense[initidx], 9606 (struct scsi_sense_data_desc *)sense_ptr); 9607 else if ((stored_format == SSD_TYPE_DESC) 9608 && (sense_format == SSD_TYPE_FIXED)) 9609 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9610 &lun->pending_sense[initidx], 9611 (struct scsi_sense_data_fixed *)sense_ptr); 9612 else 9613 memcpy(sense_ptr, &lun->pending_sense[initidx], 9614 MIN(sizeof(*sense_ptr), 9615 sizeof(lun->pending_sense[initidx]))); 9616 9617 ctl_clear_mask(lun->have_ca, initidx); 9618 have_error = 1; 9619 } else 9620#endif 9621 { 9622 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9623 if (ua_type != CTL_UA_NONE) 9624 have_error = 1; 9625 if (ua_type == CTL_UA_LUN_CHANGE) { 9626 mtx_unlock(&lun->lun_lock); 9627 mtx_lock(&ctl_softc->ctl_lock); 9628 ctl_clear_ua(ctl_softc, initidx, ua_type); 9629 mtx_unlock(&ctl_softc->ctl_lock); 9630 mtx_lock(&lun->lun_lock); 9631 } 9632 9633 } 9634 mtx_unlock(&lun->lun_lock); 9635 9636 /* 9637 * We already have a pending error, return it. 9638 */ 9639 if (have_error != 0) { 9640 /* 9641 * We report the SCSI status as OK, since the status of the 9642 * request sense command itself is OK. 9643 * We report 0 for the sense length, because we aren't doing 9644 * autosense in this case. We're reporting sense as 9645 * parameter data. 9646 */ 9647 ctl_set_success(ctsio); 9648 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9649 ctsio->be_move_done = ctl_config_move_done; 9650 ctl_datamove((union ctl_io *)ctsio); 9651 return (CTL_RETVAL_COMPLETE); 9652 } 9653 9654no_sense: 9655 9656 /* 9657 * No sense information to report, so we report that everything is 9658 * okay. 9659 */ 9660 ctl_set_sense_data(sense_ptr, 9661 lun, 9662 sense_format, 9663 /*current_error*/ 1, 9664 /*sense_key*/ SSD_KEY_NO_SENSE, 9665 /*asc*/ 0x00, 9666 /*ascq*/ 0x00, 9667 SSD_ELEM_NONE); 9668 9669 /* 9670 * We report 0 for the sense length, because we aren't doing 9671 * autosense in this case. We're reporting sense as parameter data. 9672 */ 9673 ctl_set_success(ctsio); 9674 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9675 ctsio->be_move_done = ctl_config_move_done; 9676 ctl_datamove((union ctl_io *)ctsio); 9677 return (CTL_RETVAL_COMPLETE); 9678} 9679 9680int 9681ctl_tur(struct ctl_scsiio *ctsio) 9682{ 9683 9684 CTL_DEBUG_PRINT(("ctl_tur\n")); 9685 9686 ctl_set_success(ctsio); 9687 ctl_done((union ctl_io *)ctsio); 9688 9689 return (CTL_RETVAL_COMPLETE); 9690} 9691 9692#ifdef notyet 9693static int 9694ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9695{ 9696 9697} 9698#endif 9699 9700/* 9701 * SCSI VPD page 0x00, the Supported VPD Pages page. 9702 */ 9703static int 9704ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9705{ 9706 struct scsi_vpd_supported_pages *pages; 9707 int sup_page_size; 9708 struct ctl_lun *lun; 9709 int p; 9710 9711 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9712 9713 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9714 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9715 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9716 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9717 ctsio->kern_sg_entries = 0; 9718 9719 if (sup_page_size < alloc_len) { 9720 ctsio->residual = alloc_len - sup_page_size; 9721 ctsio->kern_data_len = sup_page_size; 9722 ctsio->kern_total_len = sup_page_size; 9723 } else { 9724 ctsio->residual = 0; 9725 ctsio->kern_data_len = alloc_len; 9726 ctsio->kern_total_len = alloc_len; 9727 } 9728 ctsio->kern_data_resid = 0; 9729 ctsio->kern_rel_offset = 0; 9730 ctsio->kern_sg_entries = 0; 9731 9732 /* 9733 * The control device is always connected. The disk device, on the 9734 * other hand, may not be online all the time. Need to change this 9735 * to figure out whether the disk device is actually online or not. 9736 */ 9737 if (lun != NULL) 9738 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9739 lun->be_lun->lun_type; 9740 else 9741 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9742 9743 p = 0; 9744 /* Supported VPD pages */ 9745 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9746 /* Serial Number */ 9747 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9748 /* Device Identification */ 9749 pages->page_list[p++] = SVPD_DEVICE_ID; 9750 /* Extended INQUIRY Data */ 9751 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9752 /* Mode Page Policy */ 9753 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9754 /* SCSI Ports */ 9755 pages->page_list[p++] = SVPD_SCSI_PORTS; 9756 /* Third-party Copy */ 9757 pages->page_list[p++] = SVPD_SCSI_TPC; 9758 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9759 /* Block limits */ 9760 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9761 /* Block Device Characteristics */ 9762 pages->page_list[p++] = SVPD_BDC; 9763 /* Logical Block Provisioning */ 9764 pages->page_list[p++] = SVPD_LBP; 9765 } 9766 pages->length = p; 9767 9768 ctl_set_success(ctsio); 9769 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9770 ctsio->be_move_done = ctl_config_move_done; 9771 ctl_datamove((union ctl_io *)ctsio); 9772 return (CTL_RETVAL_COMPLETE); 9773} 9774 9775/* 9776 * SCSI VPD page 0x80, the Unit Serial Number page. 9777 */ 9778static int 9779ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9780{ 9781 struct scsi_vpd_unit_serial_number *sn_ptr; 9782 struct ctl_lun *lun; 9783 int data_len; 9784 9785 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9786 9787 data_len = 4 + CTL_SN_LEN; 9788 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9789 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9790 if (data_len < alloc_len) { 9791 ctsio->residual = alloc_len - data_len; 9792 ctsio->kern_data_len = data_len; 9793 ctsio->kern_total_len = data_len; 9794 } else { 9795 ctsio->residual = 0; 9796 ctsio->kern_data_len = alloc_len; 9797 ctsio->kern_total_len = alloc_len; 9798 } 9799 ctsio->kern_data_resid = 0; 9800 ctsio->kern_rel_offset = 0; 9801 ctsio->kern_sg_entries = 0; 9802 9803 /* 9804 * The control device is always connected. The disk device, on the 9805 * other hand, may not be online all the time. Need to change this 9806 * to figure out whether the disk device is actually online or not. 9807 */ 9808 if (lun != NULL) 9809 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9810 lun->be_lun->lun_type; 9811 else 9812 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9813 9814 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9815 sn_ptr->length = CTL_SN_LEN; 9816 /* 9817 * If we don't have a LUN, we just leave the serial number as 9818 * all spaces. 9819 */ 9820 if (lun != NULL) { 9821 strncpy((char *)sn_ptr->serial_num, 9822 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9823 } else 9824 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9825 9826 ctl_set_success(ctsio); 9827 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9828 ctsio->be_move_done = ctl_config_move_done; 9829 ctl_datamove((union ctl_io *)ctsio); 9830 return (CTL_RETVAL_COMPLETE); 9831} 9832 9833 9834/* 9835 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9836 */ 9837static int 9838ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9839{ 9840 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9841 struct ctl_lun *lun; 9842 int data_len; 9843 9844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9845 9846 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9847 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9848 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9849 ctsio->kern_sg_entries = 0; 9850 9851 if (data_len < alloc_len) { 9852 ctsio->residual = alloc_len - data_len; 9853 ctsio->kern_data_len = data_len; 9854 ctsio->kern_total_len = data_len; 9855 } else { 9856 ctsio->residual = 0; 9857 ctsio->kern_data_len = alloc_len; 9858 ctsio->kern_total_len = alloc_len; 9859 } 9860 ctsio->kern_data_resid = 0; 9861 ctsio->kern_rel_offset = 0; 9862 ctsio->kern_sg_entries = 0; 9863 9864 /* 9865 * The control device is always connected. The disk device, on the 9866 * other hand, may not be online all the time. 9867 */ 9868 if (lun != NULL) 9869 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9870 lun->be_lun->lun_type; 9871 else 9872 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9873 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9874 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9875 /* 9876 * We support head of queue, ordered and simple tags. 9877 */ 9878 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9879 /* 9880 * Volatile cache supported. 9881 */ 9882 eid_ptr->flags3 = SVPD_EID_V_SUP; 9883 9884 /* 9885 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9886 * attention for a particular IT nexus on all LUNs once we report 9887 * it to that nexus once. This bit is required as of SPC-4. 9888 */ 9889 eid_ptr->flags4 = SVPD_EID_LUICLT; 9890 9891 /* 9892 * XXX KDM in order to correctly answer this, we would need 9893 * information from the SIM to determine how much sense data it 9894 * can send. So this would really be a path inquiry field, most 9895 * likely. This can be set to a maximum of 252 according to SPC-4, 9896 * but the hardware may or may not be able to support that much. 9897 * 0 just means that the maximum sense data length is not reported. 9898 */ 9899 eid_ptr->max_sense_length = 0; 9900 9901 ctl_set_success(ctsio); 9902 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9903 ctsio->be_move_done = ctl_config_move_done; 9904 ctl_datamove((union ctl_io *)ctsio); 9905 return (CTL_RETVAL_COMPLETE); 9906} 9907 9908static int 9909ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9910{ 9911 struct scsi_vpd_mode_page_policy *mpp_ptr; 9912 struct ctl_lun *lun; 9913 int data_len; 9914 9915 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9916 9917 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9918 sizeof(struct scsi_vpd_mode_page_policy_descr); 9919 9920 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9921 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9922 ctsio->kern_sg_entries = 0; 9923 9924 if (data_len < alloc_len) { 9925 ctsio->residual = alloc_len - data_len; 9926 ctsio->kern_data_len = data_len; 9927 ctsio->kern_total_len = data_len; 9928 } else { 9929 ctsio->residual = 0; 9930 ctsio->kern_data_len = alloc_len; 9931 ctsio->kern_total_len = alloc_len; 9932 } 9933 ctsio->kern_data_resid = 0; 9934 ctsio->kern_rel_offset = 0; 9935 ctsio->kern_sg_entries = 0; 9936 9937 /* 9938 * The control device is always connected. The disk device, on the 9939 * other hand, may not be online all the time. 9940 */ 9941 if (lun != NULL) 9942 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9943 lun->be_lun->lun_type; 9944 else 9945 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9946 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9947 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9948 mpp_ptr->descr[0].page_code = 0x3f; 9949 mpp_ptr->descr[0].subpage_code = 0xff; 9950 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9951 9952 ctl_set_success(ctsio); 9953 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9954 ctsio->be_move_done = ctl_config_move_done; 9955 ctl_datamove((union ctl_io *)ctsio); 9956 return (CTL_RETVAL_COMPLETE); 9957} 9958 9959/* 9960 * SCSI VPD page 0x83, the Device Identification page. 9961 */ 9962static int 9963ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9964{ 9965 struct scsi_vpd_device_id *devid_ptr; 9966 struct scsi_vpd_id_descriptor *desc; 9967 struct ctl_softc *softc; 9968 struct ctl_lun *lun; 9969 struct ctl_port *port; 9970 int data_len; 9971 uint8_t proto; 9972 9973 softc = control_softc; 9974 9975 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9977 9978 data_len = sizeof(struct scsi_vpd_device_id) + 9979 sizeof(struct scsi_vpd_id_descriptor) + 9980 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9981 sizeof(struct scsi_vpd_id_descriptor) + 9982 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9983 if (lun && lun->lun_devid) 9984 data_len += lun->lun_devid->len; 9985 if (port->port_devid) 9986 data_len += port->port_devid->len; 9987 if (port->target_devid) 9988 data_len += port->target_devid->len; 9989 9990 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9991 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9992 ctsio->kern_sg_entries = 0; 9993 9994 if (data_len < alloc_len) { 9995 ctsio->residual = alloc_len - data_len; 9996 ctsio->kern_data_len = data_len; 9997 ctsio->kern_total_len = data_len; 9998 } else { 9999 ctsio->residual = 0; 10000 ctsio->kern_data_len = alloc_len; 10001 ctsio->kern_total_len = alloc_len; 10002 } 10003 ctsio->kern_data_resid = 0; 10004 ctsio->kern_rel_offset = 0; 10005 ctsio->kern_sg_entries = 0; 10006 10007 /* 10008 * The control device is always connected. The disk device, on the 10009 * other hand, may not be online all the time. 10010 */ 10011 if (lun != NULL) 10012 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10013 lun->be_lun->lun_type; 10014 else 10015 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10016 devid_ptr->page_code = SVPD_DEVICE_ID; 10017 scsi_ulto2b(data_len - 4, devid_ptr->length); 10018 10019 if (port->port_type == CTL_PORT_FC) 10020 proto = SCSI_PROTO_FC << 4; 10021 else if (port->port_type == CTL_PORT_ISCSI) 10022 proto = SCSI_PROTO_ISCSI << 4; 10023 else 10024 proto = SCSI_PROTO_SPI << 4; 10025 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10026 10027 /* 10028 * We're using a LUN association here. i.e., this device ID is a 10029 * per-LUN identifier. 10030 */ 10031 if (lun && lun->lun_devid) { 10032 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10033 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10034 lun->lun_devid->len); 10035 } 10036 10037 /* 10038 * This is for the WWPN which is a port association. 10039 */ 10040 if (port->port_devid) { 10041 memcpy(desc, port->port_devid->data, port->port_devid->len); 10042 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10043 port->port_devid->len); 10044 } 10045 10046 /* 10047 * This is for the Relative Target Port(type 4h) identifier 10048 */ 10049 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10050 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10051 SVPD_ID_TYPE_RELTARG; 10052 desc->length = 4; 10053 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10054 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10055 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10056 10057 /* 10058 * This is for the Target Port Group(type 5h) identifier 10059 */ 10060 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10061 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10062 SVPD_ID_TYPE_TPORTGRP; 10063 desc->length = 4; 10064 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10065 &desc->identifier[2]); 10066 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10067 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10068 10069 /* 10070 * This is for the Target identifier 10071 */ 10072 if (port->target_devid) { 10073 memcpy(desc, port->target_devid->data, port->target_devid->len); 10074 } 10075 10076 ctl_set_success(ctsio); 10077 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10078 ctsio->be_move_done = ctl_config_move_done; 10079 ctl_datamove((union ctl_io *)ctsio); 10080 return (CTL_RETVAL_COMPLETE); 10081} 10082 10083static int 10084ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10085{ 10086 struct ctl_softc *softc = control_softc; 10087 struct scsi_vpd_scsi_ports *sp; 10088 struct scsi_vpd_port_designation *pd; 10089 struct scsi_vpd_port_designation_cont *pdc; 10090 struct ctl_lun *lun; 10091 struct ctl_port *port; 10092 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10093 int num_target_port_groups; 10094 10095 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10096 10097 if (softc->is_single) 10098 num_target_port_groups = 1; 10099 else 10100 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10101 num_target_ports = 0; 10102 iid_len = 0; 10103 id_len = 0; 10104 mtx_lock(&softc->ctl_lock); 10105 STAILQ_FOREACH(port, &softc->port_list, links) { 10106 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10107 continue; 10108 if (lun != NULL && 10109 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10110 continue; 10111 num_target_ports++; 10112 if (port->init_devid) 10113 iid_len += port->init_devid->len; 10114 if (port->port_devid) 10115 id_len += port->port_devid->len; 10116 } 10117 mtx_unlock(&softc->ctl_lock); 10118 10119 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10120 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10121 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10122 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10123 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10124 ctsio->kern_sg_entries = 0; 10125 10126 if (data_len < alloc_len) { 10127 ctsio->residual = alloc_len - data_len; 10128 ctsio->kern_data_len = data_len; 10129 ctsio->kern_total_len = data_len; 10130 } else { 10131 ctsio->residual = 0; 10132 ctsio->kern_data_len = alloc_len; 10133 ctsio->kern_total_len = alloc_len; 10134 } 10135 ctsio->kern_data_resid = 0; 10136 ctsio->kern_rel_offset = 0; 10137 ctsio->kern_sg_entries = 0; 10138 10139 /* 10140 * The control device is always connected. The disk device, on the 10141 * other hand, may not be online all the time. Need to change this 10142 * to figure out whether the disk device is actually online or not. 10143 */ 10144 if (lun != NULL) 10145 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10146 lun->be_lun->lun_type; 10147 else 10148 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10149 10150 sp->page_code = SVPD_SCSI_PORTS; 10151 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10152 sp->page_length); 10153 pd = &sp->design[0]; 10154 10155 mtx_lock(&softc->ctl_lock); 10156 pg = softc->port_offset / CTL_MAX_PORTS; 10157 for (g = 0; g < num_target_port_groups; g++) { 10158 STAILQ_FOREACH(port, &softc->port_list, links) { 10159 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10160 continue; 10161 if (lun != NULL && 10162 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10163 continue; 10164 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10165 scsi_ulto2b(p, pd->relative_port_id); 10166 if (port->init_devid && g == pg) { 10167 iid_len = port->init_devid->len; 10168 memcpy(pd->initiator_transportid, 10169 port->init_devid->data, port->init_devid->len); 10170 } else 10171 iid_len = 0; 10172 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10173 pdc = (struct scsi_vpd_port_designation_cont *) 10174 (&pd->initiator_transportid[iid_len]); 10175 if (port->port_devid && g == pg) { 10176 id_len = port->port_devid->len; 10177 memcpy(pdc->target_port_descriptors, 10178 port->port_devid->data, port->port_devid->len); 10179 } else 10180 id_len = 0; 10181 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10182 pd = (struct scsi_vpd_port_designation *) 10183 ((uint8_t *)pdc->target_port_descriptors + id_len); 10184 } 10185 } 10186 mtx_unlock(&softc->ctl_lock); 10187 10188 ctl_set_success(ctsio); 10189 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10190 ctsio->be_move_done = ctl_config_move_done; 10191 ctl_datamove((union ctl_io *)ctsio); 10192 return (CTL_RETVAL_COMPLETE); 10193} 10194 10195static int 10196ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10197{ 10198 struct scsi_vpd_block_limits *bl_ptr; 10199 struct ctl_lun *lun; 10200 int bs; 10201 10202 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10203 10204 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10205 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10206 ctsio->kern_sg_entries = 0; 10207 10208 if (sizeof(*bl_ptr) < alloc_len) { 10209 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10210 ctsio->kern_data_len = sizeof(*bl_ptr); 10211 ctsio->kern_total_len = sizeof(*bl_ptr); 10212 } else { 10213 ctsio->residual = 0; 10214 ctsio->kern_data_len = alloc_len; 10215 ctsio->kern_total_len = alloc_len; 10216 } 10217 ctsio->kern_data_resid = 0; 10218 ctsio->kern_rel_offset = 0; 10219 ctsio->kern_sg_entries = 0; 10220 10221 /* 10222 * The control device is always connected. The disk device, on the 10223 * other hand, may not be online all the time. Need to change this 10224 * to figure out whether the disk device is actually online or not. 10225 */ 10226 if (lun != NULL) 10227 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10228 lun->be_lun->lun_type; 10229 else 10230 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10231 10232 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10233 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10234 bl_ptr->max_cmp_write_len = 0xff; 10235 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10236 if (lun != NULL) { 10237 bs = lun->be_lun->blocksize; 10238 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 10239 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10240 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10241 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10242 if (lun->be_lun->ublockexp != 0) { 10243 scsi_ulto4b((1 << lun->be_lun->ublockexp), 10244 bl_ptr->opt_unmap_grain); 10245 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 10246 bl_ptr->unmap_grain_align); 10247 } 10248 } 10249 scsi_ulto4b(lun->be_lun->atomicblock, 10250 bl_ptr->max_atomic_transfer_length); 10251 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10252 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10253 } 10254 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10255 10256 ctl_set_success(ctsio); 10257 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10258 ctsio->be_move_done = ctl_config_move_done; 10259 ctl_datamove((union ctl_io *)ctsio); 10260 return (CTL_RETVAL_COMPLETE); 10261} 10262 10263static int 10264ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10265{ 10266 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10267 struct ctl_lun *lun; 10268 const char *value; 10269 u_int i; 10270 10271 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10272 10273 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10274 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10275 ctsio->kern_sg_entries = 0; 10276 10277 if (sizeof(*bdc_ptr) < alloc_len) { 10278 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10279 ctsio->kern_data_len = sizeof(*bdc_ptr); 10280 ctsio->kern_total_len = sizeof(*bdc_ptr); 10281 } else { 10282 ctsio->residual = 0; 10283 ctsio->kern_data_len = alloc_len; 10284 ctsio->kern_total_len = alloc_len; 10285 } 10286 ctsio->kern_data_resid = 0; 10287 ctsio->kern_rel_offset = 0; 10288 ctsio->kern_sg_entries = 0; 10289 10290 /* 10291 * The control device is always connected. The disk device, on the 10292 * other hand, may not be online all the time. Need to change this 10293 * to figure out whether the disk device is actually online or not. 10294 */ 10295 if (lun != NULL) 10296 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10297 lun->be_lun->lun_type; 10298 else 10299 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10300 bdc_ptr->page_code = SVPD_BDC; 10301 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10302 if (lun != NULL && 10303 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10304 i = strtol(value, NULL, 0); 10305 else 10306 i = CTL_DEFAULT_ROTATION_RATE; 10307 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10308 if (lun != NULL && 10309 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10310 i = strtol(value, NULL, 0); 10311 else 10312 i = 0; 10313 bdc_ptr->wab_wac_ff = (i & 0x0f); 10314 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10315 10316 ctl_set_success(ctsio); 10317 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10318 ctsio->be_move_done = ctl_config_move_done; 10319 ctl_datamove((union ctl_io *)ctsio); 10320 return (CTL_RETVAL_COMPLETE); 10321} 10322 10323static int 10324ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10325{ 10326 struct scsi_vpd_logical_block_prov *lbp_ptr; 10327 struct ctl_lun *lun; 10328 10329 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10330 10331 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10332 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10333 ctsio->kern_sg_entries = 0; 10334 10335 if (sizeof(*lbp_ptr) < alloc_len) { 10336 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10337 ctsio->kern_data_len = sizeof(*lbp_ptr); 10338 ctsio->kern_total_len = sizeof(*lbp_ptr); 10339 } else { 10340 ctsio->residual = 0; 10341 ctsio->kern_data_len = alloc_len; 10342 ctsio->kern_total_len = alloc_len; 10343 } 10344 ctsio->kern_data_resid = 0; 10345 ctsio->kern_rel_offset = 0; 10346 ctsio->kern_sg_entries = 0; 10347 10348 /* 10349 * The control device is always connected. The disk device, on the 10350 * other hand, may not be online all the time. Need to change this 10351 * to figure out whether the disk device is actually online or not. 10352 */ 10353 if (lun != NULL) 10354 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10355 lun->be_lun->lun_type; 10356 else 10357 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10358 10359 lbp_ptr->page_code = SVPD_LBP; 10360 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10361 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10362 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10363 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10364 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10365 lbp_ptr->prov_type = SVPD_LBP_THIN; 10366 } 10367 10368 ctl_set_success(ctsio); 10369 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10370 ctsio->be_move_done = ctl_config_move_done; 10371 ctl_datamove((union ctl_io *)ctsio); 10372 return (CTL_RETVAL_COMPLETE); 10373} 10374 10375/* 10376 * INQUIRY with the EVPD bit set. 10377 */ 10378static int 10379ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10380{ 10381 struct ctl_lun *lun; 10382 struct scsi_inquiry *cdb; 10383 int alloc_len, retval; 10384 10385 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10386 cdb = (struct scsi_inquiry *)ctsio->cdb; 10387 alloc_len = scsi_2btoul(cdb->length); 10388 10389 switch (cdb->page_code) { 10390 case SVPD_SUPPORTED_PAGES: 10391 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10392 break; 10393 case SVPD_UNIT_SERIAL_NUMBER: 10394 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10395 break; 10396 case SVPD_DEVICE_ID: 10397 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10398 break; 10399 case SVPD_EXTENDED_INQUIRY_DATA: 10400 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10401 break; 10402 case SVPD_MODE_PAGE_POLICY: 10403 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10404 break; 10405 case SVPD_SCSI_PORTS: 10406 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10407 break; 10408 case SVPD_SCSI_TPC: 10409 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10410 break; 10411 case SVPD_BLOCK_LIMITS: 10412 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10413 goto err; 10414 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10415 break; 10416 case SVPD_BDC: 10417 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10418 goto err; 10419 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10420 break; 10421 case SVPD_LBP: 10422 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10423 goto err; 10424 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10425 break; 10426 default: 10427err: 10428 ctl_set_invalid_field(ctsio, 10429 /*sks_valid*/ 1, 10430 /*command*/ 1, 10431 /*field*/ 2, 10432 /*bit_valid*/ 0, 10433 /*bit*/ 0); 10434 ctl_done((union ctl_io *)ctsio); 10435 retval = CTL_RETVAL_COMPLETE; 10436 break; 10437 } 10438 10439 return (retval); 10440} 10441 10442/* 10443 * Standard INQUIRY data. 10444 */ 10445static int 10446ctl_inquiry_std(struct ctl_scsiio *ctsio) 10447{ 10448 struct scsi_inquiry_data *inq_ptr; 10449 struct scsi_inquiry *cdb; 10450 struct ctl_softc *softc; 10451 struct ctl_lun *lun; 10452 char *val; 10453 uint32_t alloc_len, data_len; 10454 ctl_port_type port_type; 10455 10456 softc = control_softc; 10457 10458 /* 10459 * Figure out whether we're talking to a Fibre Channel port or not. 10460 * We treat the ioctl front end, and any SCSI adapters, as packetized 10461 * SCSI front ends. 10462 */ 10463 port_type = softc->ctl_ports[ 10464 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10465 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10466 port_type = CTL_PORT_SCSI; 10467 10468 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10469 cdb = (struct scsi_inquiry *)ctsio->cdb; 10470 alloc_len = scsi_2btoul(cdb->length); 10471 10472 /* 10473 * We malloc the full inquiry data size here and fill it 10474 * in. If the user only asks for less, we'll give him 10475 * that much. 10476 */ 10477 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10478 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10479 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10480 ctsio->kern_sg_entries = 0; 10481 ctsio->kern_data_resid = 0; 10482 ctsio->kern_rel_offset = 0; 10483 10484 if (data_len < alloc_len) { 10485 ctsio->residual = alloc_len - data_len; 10486 ctsio->kern_data_len = data_len; 10487 ctsio->kern_total_len = data_len; 10488 } else { 10489 ctsio->residual = 0; 10490 ctsio->kern_data_len = alloc_len; 10491 ctsio->kern_total_len = alloc_len; 10492 } 10493 10494 /* 10495 * If we have a LUN configured, report it as connected. Otherwise, 10496 * report that it is offline or no device is supported, depending 10497 * on the value of inquiry_pq_no_lun. 10498 * 10499 * According to the spec (SPC-4 r34), the peripheral qualifier 10500 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10501 * 10502 * "A peripheral device having the specified peripheral device type 10503 * is not connected to this logical unit. However, the device 10504 * server is capable of supporting the specified peripheral device 10505 * type on this logical unit." 10506 * 10507 * According to the same spec, the peripheral qualifier 10508 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10509 * 10510 * "The device server is not capable of supporting a peripheral 10511 * device on this logical unit. For this peripheral qualifier the 10512 * peripheral device type shall be set to 1Fh. All other peripheral 10513 * device type values are reserved for this peripheral qualifier." 10514 * 10515 * Given the text, it would seem that we probably want to report that 10516 * the LUN is offline here. There is no LUN connected, but we can 10517 * support a LUN at the given LUN number. 10518 * 10519 * In the real world, though, it sounds like things are a little 10520 * different: 10521 * 10522 * - Linux, when presented with a LUN with the offline peripheral 10523 * qualifier, will create an sg driver instance for it. So when 10524 * you attach it to CTL, you wind up with a ton of sg driver 10525 * instances. (One for every LUN that Linux bothered to probe.) 10526 * Linux does this despite the fact that it issues a REPORT LUNs 10527 * to LUN 0 to get the inventory of supported LUNs. 10528 * 10529 * - There is other anecdotal evidence (from Emulex folks) about 10530 * arrays that use the offline peripheral qualifier for LUNs that 10531 * are on the "passive" path in an active/passive array. 10532 * 10533 * So the solution is provide a hopefully reasonable default 10534 * (return bad/no LUN) and allow the user to change the behavior 10535 * with a tunable/sysctl variable. 10536 */ 10537 if (lun != NULL) 10538 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10539 lun->be_lun->lun_type; 10540 else if (softc->inquiry_pq_no_lun == 0) 10541 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10542 else 10543 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10544 10545 /* RMB in byte 2 is 0 */ 10546 inq_ptr->version = SCSI_REV_SPC4; 10547 10548 /* 10549 * According to SAM-3, even if a device only supports a single 10550 * level of LUN addressing, it should still set the HISUP bit: 10551 * 10552 * 4.9.1 Logical unit numbers overview 10553 * 10554 * All logical unit number formats described in this standard are 10555 * hierarchical in structure even when only a single level in that 10556 * hierarchy is used. The HISUP bit shall be set to one in the 10557 * standard INQUIRY data (see SPC-2) when any logical unit number 10558 * format described in this standard is used. Non-hierarchical 10559 * formats are outside the scope of this standard. 10560 * 10561 * Therefore we set the HiSup bit here. 10562 * 10563 * The reponse format is 2, per SPC-3. 10564 */ 10565 inq_ptr->response_format = SID_HiSup | 2; 10566 10567 inq_ptr->additional_length = data_len - 10568 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10569 CTL_DEBUG_PRINT(("additional_length = %d\n", 10570 inq_ptr->additional_length)); 10571 10572 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10573 /* 16 bit addressing */ 10574 if (port_type == CTL_PORT_SCSI) 10575 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10576 /* XXX set the SID_MultiP bit here if we're actually going to 10577 respond on multiple ports */ 10578 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10579 10580 /* 16 bit data bus, synchronous transfers */ 10581 if (port_type == CTL_PORT_SCSI) 10582 inq_ptr->flags = SID_WBus16 | SID_Sync; 10583 /* 10584 * XXX KDM do we want to support tagged queueing on the control 10585 * device at all? 10586 */ 10587 if ((lun == NULL) 10588 || (lun->be_lun->lun_type != T_PROCESSOR)) 10589 inq_ptr->flags |= SID_CmdQue; 10590 /* 10591 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10592 * We have 8 bytes for the vendor name, and 16 bytes for the device 10593 * name and 4 bytes for the revision. 10594 */ 10595 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10596 "vendor")) == NULL) { 10597 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10598 } else { 10599 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10600 strncpy(inq_ptr->vendor, val, 10601 min(sizeof(inq_ptr->vendor), strlen(val))); 10602 } 10603 if (lun == NULL) { 10604 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10605 sizeof(inq_ptr->product)); 10606 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10607 switch (lun->be_lun->lun_type) { 10608 case T_DIRECT: 10609 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10610 sizeof(inq_ptr->product)); 10611 break; 10612 case T_PROCESSOR: 10613 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10614 sizeof(inq_ptr->product)); 10615 break; 10616 default: 10617 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10618 sizeof(inq_ptr->product)); 10619 break; 10620 } 10621 } else { 10622 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10623 strncpy(inq_ptr->product, val, 10624 min(sizeof(inq_ptr->product), strlen(val))); 10625 } 10626 10627 /* 10628 * XXX make this a macro somewhere so it automatically gets 10629 * incremented when we make changes. 10630 */ 10631 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10632 "revision")) == NULL) { 10633 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10634 } else { 10635 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10636 strncpy(inq_ptr->revision, val, 10637 min(sizeof(inq_ptr->revision), strlen(val))); 10638 } 10639 10640 /* 10641 * For parallel SCSI, we support double transition and single 10642 * transition clocking. We also support QAS (Quick Arbitration 10643 * and Selection) and Information Unit transfers on both the 10644 * control and array devices. 10645 */ 10646 if (port_type == CTL_PORT_SCSI) 10647 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10648 SID_SPI_IUS; 10649 10650 /* SAM-5 (no version claimed) */ 10651 scsi_ulto2b(0x00A0, inq_ptr->version1); 10652 /* SPC-4 (no version claimed) */ 10653 scsi_ulto2b(0x0460, inq_ptr->version2); 10654 if (port_type == CTL_PORT_FC) { 10655 /* FCP-2 ANSI INCITS.350:2003 */ 10656 scsi_ulto2b(0x0917, inq_ptr->version3); 10657 } else if (port_type == CTL_PORT_SCSI) { 10658 /* SPI-4 ANSI INCITS.362:200x */ 10659 scsi_ulto2b(0x0B56, inq_ptr->version3); 10660 } else if (port_type == CTL_PORT_ISCSI) { 10661 /* iSCSI (no version claimed) */ 10662 scsi_ulto2b(0x0960, inq_ptr->version3); 10663 } else if (port_type == CTL_PORT_SAS) { 10664 /* SAS (no version claimed) */ 10665 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10666 } 10667 10668 if (lun == NULL) { 10669 /* SBC-4 (no version claimed) */ 10670 scsi_ulto2b(0x0600, inq_ptr->version4); 10671 } else { 10672 switch (lun->be_lun->lun_type) { 10673 case T_DIRECT: 10674 /* SBC-4 (no version claimed) */ 10675 scsi_ulto2b(0x0600, inq_ptr->version4); 10676 break; 10677 case T_PROCESSOR: 10678 default: 10679 break; 10680 } 10681 } 10682 10683 ctl_set_success(ctsio); 10684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10685 ctsio->be_move_done = ctl_config_move_done; 10686 ctl_datamove((union ctl_io *)ctsio); 10687 return (CTL_RETVAL_COMPLETE); 10688} 10689 10690int 10691ctl_inquiry(struct ctl_scsiio *ctsio) 10692{ 10693 struct scsi_inquiry *cdb; 10694 int retval; 10695 10696 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10697 10698 cdb = (struct scsi_inquiry *)ctsio->cdb; 10699 if (cdb->byte2 & SI_EVPD) 10700 retval = ctl_inquiry_evpd(ctsio); 10701 else if (cdb->page_code == 0) 10702 retval = ctl_inquiry_std(ctsio); 10703 else { 10704 ctl_set_invalid_field(ctsio, 10705 /*sks_valid*/ 1, 10706 /*command*/ 1, 10707 /*field*/ 2, 10708 /*bit_valid*/ 0, 10709 /*bit*/ 0); 10710 ctl_done((union ctl_io *)ctsio); 10711 return (CTL_RETVAL_COMPLETE); 10712 } 10713 10714 return (retval); 10715} 10716 10717/* 10718 * For known CDB types, parse the LBA and length. 10719 */ 10720static int 10721ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10722{ 10723 if (io->io_hdr.io_type != CTL_IO_SCSI) 10724 return (1); 10725 10726 switch (io->scsiio.cdb[0]) { 10727 case COMPARE_AND_WRITE: { 10728 struct scsi_compare_and_write *cdb; 10729 10730 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10731 10732 *lba = scsi_8btou64(cdb->addr); 10733 *len = cdb->length; 10734 break; 10735 } 10736 case READ_6: 10737 case WRITE_6: { 10738 struct scsi_rw_6 *cdb; 10739 10740 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10741 10742 *lba = scsi_3btoul(cdb->addr); 10743 /* only 5 bits are valid in the most significant address byte */ 10744 *lba &= 0x1fffff; 10745 *len = cdb->length; 10746 break; 10747 } 10748 case READ_10: 10749 case WRITE_10: { 10750 struct scsi_rw_10 *cdb; 10751 10752 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10753 10754 *lba = scsi_4btoul(cdb->addr); 10755 *len = scsi_2btoul(cdb->length); 10756 break; 10757 } 10758 case WRITE_VERIFY_10: { 10759 struct scsi_write_verify_10 *cdb; 10760 10761 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10762 10763 *lba = scsi_4btoul(cdb->addr); 10764 *len = scsi_2btoul(cdb->length); 10765 break; 10766 } 10767 case READ_12: 10768 case WRITE_12: { 10769 struct scsi_rw_12 *cdb; 10770 10771 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10772 10773 *lba = scsi_4btoul(cdb->addr); 10774 *len = scsi_4btoul(cdb->length); 10775 break; 10776 } 10777 case WRITE_VERIFY_12: { 10778 struct scsi_write_verify_12 *cdb; 10779 10780 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10781 10782 *lba = scsi_4btoul(cdb->addr); 10783 *len = scsi_4btoul(cdb->length); 10784 break; 10785 } 10786 case READ_16: 10787 case WRITE_16: 10788 case WRITE_ATOMIC_16: { 10789 struct scsi_rw_16 *cdb; 10790 10791 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10792 10793 *lba = scsi_8btou64(cdb->addr); 10794 *len = scsi_4btoul(cdb->length); 10795 break; 10796 } 10797 case WRITE_VERIFY_16: { 10798 struct scsi_write_verify_16 *cdb; 10799 10800 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10801 10802 *lba = scsi_8btou64(cdb->addr); 10803 *len = scsi_4btoul(cdb->length); 10804 break; 10805 } 10806 case WRITE_SAME_10: { 10807 struct scsi_write_same_10 *cdb; 10808 10809 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10810 10811 *lba = scsi_4btoul(cdb->addr); 10812 *len = scsi_2btoul(cdb->length); 10813 break; 10814 } 10815 case WRITE_SAME_16: { 10816 struct scsi_write_same_16 *cdb; 10817 10818 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10819 10820 *lba = scsi_8btou64(cdb->addr); 10821 *len = scsi_4btoul(cdb->length); 10822 break; 10823 } 10824 case VERIFY_10: { 10825 struct scsi_verify_10 *cdb; 10826 10827 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10828 10829 *lba = scsi_4btoul(cdb->addr); 10830 *len = scsi_2btoul(cdb->length); 10831 break; 10832 } 10833 case VERIFY_12: { 10834 struct scsi_verify_12 *cdb; 10835 10836 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10837 10838 *lba = scsi_4btoul(cdb->addr); 10839 *len = scsi_4btoul(cdb->length); 10840 break; 10841 } 10842 case VERIFY_16: { 10843 struct scsi_verify_16 *cdb; 10844 10845 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10846 10847 *lba = scsi_8btou64(cdb->addr); 10848 *len = scsi_4btoul(cdb->length); 10849 break; 10850 } 10851 case UNMAP: { 10852 *lba = 0; 10853 *len = UINT64_MAX; 10854 break; 10855 } 10856 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10857 struct scsi_get_lba_status *cdb; 10858 10859 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10860 *lba = scsi_8btou64(cdb->addr); 10861 *len = UINT32_MAX; 10862 break; 10863 } 10864 default: 10865 return (1); 10866 break; /* NOTREACHED */ 10867 } 10868 10869 return (0); 10870} 10871 10872static ctl_action 10873ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10874 bool seq) 10875{ 10876 uint64_t endlba1, endlba2; 10877 10878 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10879 endlba2 = lba2 + len2 - 1; 10880 10881 if ((endlba1 < lba2) || (endlba2 < lba1)) 10882 return (CTL_ACTION_PASS); 10883 else 10884 return (CTL_ACTION_BLOCK); 10885} 10886 10887static int 10888ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10889{ 10890 struct ctl_ptr_len_flags *ptrlen; 10891 struct scsi_unmap_desc *buf, *end, *range; 10892 uint64_t lba; 10893 uint32_t len; 10894 10895 /* If not UNMAP -- go other way. */ 10896 if (io->io_hdr.io_type != CTL_IO_SCSI || 10897 io->scsiio.cdb[0] != UNMAP) 10898 return (CTL_ACTION_ERROR); 10899 10900 /* If UNMAP without data -- block and wait for data. */ 10901 ptrlen = (struct ctl_ptr_len_flags *) 10902 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10903 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10904 ptrlen->ptr == NULL) 10905 return (CTL_ACTION_BLOCK); 10906 10907 /* UNMAP with data -- check for collision. */ 10908 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10909 end = buf + ptrlen->len / sizeof(*buf); 10910 for (range = buf; range < end; range++) { 10911 lba = scsi_8btou64(range->lba); 10912 len = scsi_4btoul(range->length); 10913 if ((lba < lba2 + len2) && (lba + len > lba2)) 10914 return (CTL_ACTION_BLOCK); 10915 } 10916 return (CTL_ACTION_PASS); 10917} 10918 10919static ctl_action 10920ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10921{ 10922 uint64_t lba1, lba2; 10923 uint64_t len1, len2; 10924 int retval; 10925 10926 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10927 return (CTL_ACTION_ERROR); 10928 10929 retval = ctl_extent_check_unmap(io1, lba2, len2); 10930 if (retval != CTL_ACTION_ERROR) 10931 return (retval); 10932 10933 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10934 return (CTL_ACTION_ERROR); 10935 10936 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10937} 10938 10939static ctl_action 10940ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10941{ 10942 uint64_t lba1, lba2; 10943 uint64_t len1, len2; 10944 10945 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10946 return (CTL_ACTION_ERROR); 10947 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10948 return (CTL_ACTION_ERROR); 10949 10950 if (lba1 + len1 == lba2) 10951 return (CTL_ACTION_BLOCK); 10952 return (CTL_ACTION_PASS); 10953} 10954 10955static ctl_action 10956ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10957 union ctl_io *ooa_io) 10958{ 10959 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10960 ctl_serialize_action *serialize_row; 10961 10962 /* 10963 * The initiator attempted multiple untagged commands at the same 10964 * time. Can't do that. 10965 */ 10966 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10967 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10968 && ((pending_io->io_hdr.nexus.targ_port == 10969 ooa_io->io_hdr.nexus.targ_port) 10970 && (pending_io->io_hdr.nexus.initid.id == 10971 ooa_io->io_hdr.nexus.initid.id)) 10972 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10973 CTL_FLAG_STATUS_SENT)) == 0)) 10974 return (CTL_ACTION_OVERLAP); 10975 10976 /* 10977 * The initiator attempted to send multiple tagged commands with 10978 * the same ID. (It's fine if different initiators have the same 10979 * tag ID.) 10980 * 10981 * Even if all of those conditions are true, we don't kill the I/O 10982 * if the command ahead of us has been aborted. We won't end up 10983 * sending it to the FETD, and it's perfectly legal to resend a 10984 * command with the same tag number as long as the previous 10985 * instance of this tag number has been aborted somehow. 10986 */ 10987 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10988 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10989 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10990 && ((pending_io->io_hdr.nexus.targ_port == 10991 ooa_io->io_hdr.nexus.targ_port) 10992 && (pending_io->io_hdr.nexus.initid.id == 10993 ooa_io->io_hdr.nexus.initid.id)) 10994 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10995 CTL_FLAG_STATUS_SENT)) == 0)) 10996 return (CTL_ACTION_OVERLAP_TAG); 10997 10998 /* 10999 * If we get a head of queue tag, SAM-3 says that we should 11000 * immediately execute it. 11001 * 11002 * What happens if this command would normally block for some other 11003 * reason? e.g. a request sense with a head of queue tag 11004 * immediately after a write. Normally that would block, but this 11005 * will result in its getting executed immediately... 11006 * 11007 * We currently return "pass" instead of "skip", so we'll end up 11008 * going through the rest of the queue to check for overlapped tags. 11009 * 11010 * XXX KDM check for other types of blockage first?? 11011 */ 11012 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11013 return (CTL_ACTION_PASS); 11014 11015 /* 11016 * Ordered tags have to block until all items ahead of them 11017 * have completed. If we get called with an ordered tag, we always 11018 * block, if something else is ahead of us in the queue. 11019 */ 11020 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11021 return (CTL_ACTION_BLOCK); 11022 11023 /* 11024 * Simple tags get blocked until all head of queue and ordered tags 11025 * ahead of them have completed. I'm lumping untagged commands in 11026 * with simple tags here. XXX KDM is that the right thing to do? 11027 */ 11028 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11029 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11030 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11031 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11032 return (CTL_ACTION_BLOCK); 11033 11034 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11035 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11036 11037 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11038 11039 switch (serialize_row[pending_entry->seridx]) { 11040 case CTL_SER_BLOCK: 11041 return (CTL_ACTION_BLOCK); 11042 case CTL_SER_EXTENT: 11043 return (ctl_extent_check(ooa_io, pending_io, 11044 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11045 case CTL_SER_EXTENTOPT: 11046 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11047 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11048 return (ctl_extent_check(ooa_io, pending_io, 11049 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11050 return (CTL_ACTION_PASS); 11051 case CTL_SER_EXTENTSEQ: 11052 if (lun->serseq != CTL_LUN_SERSEQ_OFF) 11053 return (ctl_extent_check_seq(ooa_io, pending_io)); 11054 return (CTL_ACTION_PASS); 11055 case CTL_SER_PASS: 11056 return (CTL_ACTION_PASS); 11057 case CTL_SER_BLOCKOPT: 11058 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11059 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11060 return (CTL_ACTION_BLOCK); 11061 return (CTL_ACTION_PASS); 11062 case CTL_SER_SKIP: 11063 return (CTL_ACTION_SKIP); 11064 default: 11065 panic("invalid serialization value %d", 11066 serialize_row[pending_entry->seridx]); 11067 } 11068 11069 return (CTL_ACTION_ERROR); 11070} 11071 11072/* 11073 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11074 * Assumptions: 11075 * - pending_io is generally either incoming, or on the blocked queue 11076 * - starting I/O is the I/O we want to start the check with. 11077 */ 11078static ctl_action 11079ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11080 union ctl_io *starting_io) 11081{ 11082 union ctl_io *ooa_io; 11083 ctl_action action; 11084 11085 mtx_assert(&lun->lun_lock, MA_OWNED); 11086 11087 /* 11088 * Run back along the OOA queue, starting with the current 11089 * blocked I/O and going through every I/O before it on the 11090 * queue. If starting_io is NULL, we'll just end up returning 11091 * CTL_ACTION_PASS. 11092 */ 11093 for (ooa_io = starting_io; ooa_io != NULL; 11094 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11095 ooa_links)){ 11096 11097 /* 11098 * This routine just checks to see whether 11099 * cur_blocked is blocked by ooa_io, which is ahead 11100 * of it in the queue. It doesn't queue/dequeue 11101 * cur_blocked. 11102 */ 11103 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11104 switch (action) { 11105 case CTL_ACTION_BLOCK: 11106 case CTL_ACTION_OVERLAP: 11107 case CTL_ACTION_OVERLAP_TAG: 11108 case CTL_ACTION_SKIP: 11109 case CTL_ACTION_ERROR: 11110 return (action); 11111 break; /* NOTREACHED */ 11112 case CTL_ACTION_PASS: 11113 break; 11114 default: 11115 panic("invalid action %d", action); 11116 break; /* NOTREACHED */ 11117 } 11118 } 11119 11120 return (CTL_ACTION_PASS); 11121} 11122 11123/* 11124 * Assumptions: 11125 * - An I/O has just completed, and has been removed from the per-LUN OOA 11126 * queue, so some items on the blocked queue may now be unblocked. 11127 */ 11128static int 11129ctl_check_blocked(struct ctl_lun *lun) 11130{ 11131 union ctl_io *cur_blocked, *next_blocked; 11132 11133 mtx_assert(&lun->lun_lock, MA_OWNED); 11134 11135 /* 11136 * Run forward from the head of the blocked queue, checking each 11137 * entry against the I/Os prior to it on the OOA queue to see if 11138 * there is still any blockage. 11139 * 11140 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11141 * with our removing a variable on it while it is traversing the 11142 * list. 11143 */ 11144 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11145 cur_blocked != NULL; cur_blocked = next_blocked) { 11146 union ctl_io *prev_ooa; 11147 ctl_action action; 11148 11149 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11150 blocked_links); 11151 11152 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11153 ctl_ooaq, ooa_links); 11154 11155 /* 11156 * If cur_blocked happens to be the first item in the OOA 11157 * queue now, prev_ooa will be NULL, and the action 11158 * returned will just be CTL_ACTION_PASS. 11159 */ 11160 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11161 11162 switch (action) { 11163 case CTL_ACTION_BLOCK: 11164 /* Nothing to do here, still blocked */ 11165 break; 11166 case CTL_ACTION_OVERLAP: 11167 case CTL_ACTION_OVERLAP_TAG: 11168 /* 11169 * This shouldn't happen! In theory we've already 11170 * checked this command for overlap... 11171 */ 11172 break; 11173 case CTL_ACTION_PASS: 11174 case CTL_ACTION_SKIP: { 11175 const struct ctl_cmd_entry *entry; 11176 int isc_retval; 11177 11178 /* 11179 * The skip case shouldn't happen, this transaction 11180 * should have never made it onto the blocked queue. 11181 */ 11182 /* 11183 * This I/O is no longer blocked, we can remove it 11184 * from the blocked queue. Since this is a TAILQ 11185 * (doubly linked list), we can do O(1) removals 11186 * from any place on the list. 11187 */ 11188 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11189 blocked_links); 11190 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11191 11192 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11193 /* 11194 * Need to send IO back to original side to 11195 * run 11196 */ 11197 union ctl_ha_msg msg_info; 11198 11199 msg_info.hdr.original_sc = 11200 cur_blocked->io_hdr.original_sc; 11201 msg_info.hdr.serializing_sc = cur_blocked; 11202 msg_info.hdr.msg_type = CTL_MSG_R2R; 11203 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11204 &msg_info, sizeof(msg_info), 0)) > 11205 CTL_HA_STATUS_SUCCESS) { 11206 printf("CTL:Check Blocked error from " 11207 "ctl_ha_msg_send %d\n", 11208 isc_retval); 11209 } 11210 break; 11211 } 11212 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11213 11214 /* 11215 * Check this I/O for LUN state changes that may 11216 * have happened while this command was blocked. 11217 * The LUN state may have been changed by a command 11218 * ahead of us in the queue, so we need to re-check 11219 * for any states that can be caused by SCSI 11220 * commands. 11221 */ 11222 if (ctl_scsiio_lun_check(lun, entry, 11223 &cur_blocked->scsiio) == 0) { 11224 cur_blocked->io_hdr.flags |= 11225 CTL_FLAG_IS_WAS_ON_RTR; 11226 ctl_enqueue_rtr(cur_blocked); 11227 } else 11228 ctl_done(cur_blocked); 11229 break; 11230 } 11231 default: 11232 /* 11233 * This probably shouldn't happen -- we shouldn't 11234 * get CTL_ACTION_ERROR, or anything else. 11235 */ 11236 break; 11237 } 11238 } 11239 11240 return (CTL_RETVAL_COMPLETE); 11241} 11242 11243/* 11244 * This routine (with one exception) checks LUN flags that can be set by 11245 * commands ahead of us in the OOA queue. These flags have to be checked 11246 * when a command initially comes in, and when we pull a command off the 11247 * blocked queue and are preparing to execute it. The reason we have to 11248 * check these flags for commands on the blocked queue is that the LUN 11249 * state may have been changed by a command ahead of us while we're on the 11250 * blocked queue. 11251 * 11252 * Ordering is somewhat important with these checks, so please pay 11253 * careful attention to the placement of any new checks. 11254 */ 11255static int 11256ctl_scsiio_lun_check(struct ctl_lun *lun, 11257 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11258{ 11259 struct ctl_softc *softc = lun->ctl_softc; 11260 int retval; 11261 uint32_t residx; 11262 11263 retval = 0; 11264 11265 mtx_assert(&lun->lun_lock, MA_OWNED); 11266 11267 /* 11268 * If this shelf is a secondary shelf controller, we have to reject 11269 * any media access commands. 11270 */ 11271 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11272 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11273 ctl_set_lun_standby(ctsio); 11274 retval = 1; 11275 goto bailout; 11276 } 11277 11278 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11279 if (lun->flags & CTL_LUN_READONLY) { 11280 ctl_set_sense(ctsio, /*current_error*/ 1, 11281 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11282 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11283 retval = 1; 11284 goto bailout; 11285 } 11286 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11287 .eca_and_aen & SCP_SWP) != 0) { 11288 ctl_set_sense(ctsio, /*current_error*/ 1, 11289 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11290 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11291 retval = 1; 11292 goto bailout; 11293 } 11294 } 11295 11296 /* 11297 * Check for a reservation conflict. If this command isn't allowed 11298 * even on reserved LUNs, and if this initiator isn't the one who 11299 * reserved us, reject the command with a reservation conflict. 11300 */ 11301 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11302 if ((lun->flags & CTL_LUN_RESERVED) 11303 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11304 if (lun->res_idx != residx) { 11305 ctl_set_reservation_conflict(ctsio); 11306 retval = 1; 11307 goto bailout; 11308 } 11309 } 11310 11311 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11312 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11313 /* No reservation or command is allowed. */; 11314 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11315 (lun->res_type == SPR_TYPE_WR_EX || 11316 lun->res_type == SPR_TYPE_WR_EX_RO || 11317 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11318 /* The command is allowed for Write Exclusive resv. */; 11319 } else { 11320 /* 11321 * if we aren't registered or it's a res holder type 11322 * reservation and this isn't the res holder then set a 11323 * conflict. 11324 */ 11325 if (ctl_get_prkey(lun, residx) == 0 11326 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11327 ctl_set_reservation_conflict(ctsio); 11328 retval = 1; 11329 goto bailout; 11330 } 11331 11332 } 11333 11334 if ((lun->flags & CTL_LUN_OFFLINE) 11335 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11336 ctl_set_lun_not_ready(ctsio); 11337 retval = 1; 11338 goto bailout; 11339 } 11340 11341 /* 11342 * If the LUN is stopped, see if this particular command is allowed 11343 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11344 */ 11345 if ((lun->flags & CTL_LUN_STOPPED) 11346 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11347 /* "Logical unit not ready, initializing cmd. required" */ 11348 ctl_set_lun_stopped(ctsio); 11349 retval = 1; 11350 goto bailout; 11351 } 11352 11353 if ((lun->flags & CTL_LUN_INOPERABLE) 11354 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11355 /* "Medium format corrupted" */ 11356 ctl_set_medium_format_corrupted(ctsio); 11357 retval = 1; 11358 goto bailout; 11359 } 11360 11361bailout: 11362 return (retval); 11363 11364} 11365 11366static void 11367ctl_failover_io(union ctl_io *io, int have_lock) 11368{ 11369 ctl_set_busy(&io->scsiio); 11370 ctl_done(io); 11371} 11372 11373static void 11374ctl_failover(void) 11375{ 11376 struct ctl_lun *lun; 11377 struct ctl_softc *softc; 11378 union ctl_io *next_io, *pending_io; 11379 union ctl_io *io; 11380 int lun_idx; 11381 11382 softc = control_softc; 11383 11384 mtx_lock(&softc->ctl_lock); 11385 /* 11386 * Remove any cmds from the other SC from the rtr queue. These 11387 * will obviously only be for LUNs for which we're the primary. 11388 * We can't send status or get/send data for these commands. 11389 * Since they haven't been executed yet, we can just remove them. 11390 * We'll either abort them or delete them below, depending on 11391 * which HA mode we're in. 11392 */ 11393#ifdef notyet 11394 mtx_lock(&softc->queue_lock); 11395 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 11396 io != NULL; io = next_io) { 11397 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11398 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11399 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 11400 ctl_io_hdr, links); 11401 } 11402 mtx_unlock(&softc->queue_lock); 11403#endif 11404 11405 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) { 11406 lun = softc->ctl_luns[lun_idx]; 11407 if (lun==NULL) 11408 continue; 11409 11410 /* 11411 * Processor LUNs are primary on both sides. 11412 * XXX will this always be true? 11413 */ 11414 if (lun->be_lun->lun_type == T_PROCESSOR) 11415 continue; 11416 11417 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11418 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11419 printf("FAILOVER: primary lun %d\n", lun_idx); 11420 /* 11421 * Remove all commands from the other SC. First from the 11422 * blocked queue then from the ooa queue. Once we have 11423 * removed them. Call ctl_check_blocked to see if there 11424 * is anything that can run. 11425 */ 11426 for (io = (union ctl_io *)TAILQ_FIRST( 11427 &lun->blocked_queue); io != NULL; io = next_io) { 11428 11429 next_io = (union ctl_io *)TAILQ_NEXT( 11430 &io->io_hdr, blocked_links); 11431 11432 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11433 TAILQ_REMOVE(&lun->blocked_queue, 11434 &io->io_hdr,blocked_links); 11435 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11436 TAILQ_REMOVE(&lun->ooa_queue, 11437 &io->io_hdr, ooa_links); 11438 11439 ctl_free_io(io); 11440 } 11441 } 11442 11443 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11444 io != NULL; io = next_io) { 11445 11446 next_io = (union ctl_io *)TAILQ_NEXT( 11447 &io->io_hdr, ooa_links); 11448 11449 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11450 11451 TAILQ_REMOVE(&lun->ooa_queue, 11452 &io->io_hdr, 11453 ooa_links); 11454 11455 ctl_free_io(io); 11456 } 11457 } 11458 ctl_check_blocked(lun); 11459 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11460 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11461 11462 printf("FAILOVER: primary lun %d\n", lun_idx); 11463 /* 11464 * Abort all commands from the other SC. We can't 11465 * send status back for them now. These should get 11466 * cleaned up when they are completed or come out 11467 * for a datamove operation. 11468 */ 11469 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11470 io != NULL; io = next_io) { 11471 next_io = (union ctl_io *)TAILQ_NEXT( 11472 &io->io_hdr, ooa_links); 11473 11474 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11475 io->io_hdr.flags |= CTL_FLAG_ABORT; 11476 } 11477 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11478 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11479 11480 printf("FAILOVER: secondary lun %d\n", lun_idx); 11481 11482 lun->flags |= CTL_LUN_PRIMARY_SC; 11483 11484 /* 11485 * We send all I/O that was sent to this controller 11486 * and redirected to the other side back with 11487 * busy status, and have the initiator retry it. 11488 * Figuring out how much data has been transferred, 11489 * etc. and picking up where we left off would be 11490 * very tricky. 11491 * 11492 * XXX KDM need to remove I/O from the blocked 11493 * queue as well! 11494 */ 11495 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11496 &lun->ooa_queue); pending_io != NULL; 11497 pending_io = next_io) { 11498 11499 next_io = (union ctl_io *)TAILQ_NEXT( 11500 &pending_io->io_hdr, ooa_links); 11501 11502 pending_io->io_hdr.flags &= 11503 ~CTL_FLAG_SENT_2OTHER_SC; 11504 11505 if (pending_io->io_hdr.flags & 11506 CTL_FLAG_IO_ACTIVE) { 11507 pending_io->io_hdr.flags |= 11508 CTL_FLAG_FAILOVER; 11509 } else { 11510 ctl_set_busy(&pending_io->scsiio); 11511 ctl_done(pending_io); 11512 } 11513 } 11514 11515 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11516 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11517 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11518 printf("FAILOVER: secondary lun %d\n", lun_idx); 11519 /* 11520 * if the first io on the OOA is not on the RtR queue 11521 * add it. 11522 */ 11523 lun->flags |= CTL_LUN_PRIMARY_SC; 11524 11525 pending_io = (union ctl_io *)TAILQ_FIRST( 11526 &lun->ooa_queue); 11527 if (pending_io==NULL) { 11528 printf("Nothing on OOA queue\n"); 11529 continue; 11530 } 11531 11532 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11533 if ((pending_io->io_hdr.flags & 11534 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11535 pending_io->io_hdr.flags |= 11536 CTL_FLAG_IS_WAS_ON_RTR; 11537 ctl_enqueue_rtr(pending_io); 11538 } 11539#if 0 11540 else 11541 { 11542 printf("Tag 0x%04x is running\n", 11543 pending_io->scsiio.tag_num); 11544 } 11545#endif 11546 11547 next_io = (union ctl_io *)TAILQ_NEXT( 11548 &pending_io->io_hdr, ooa_links); 11549 for (pending_io=next_io; pending_io != NULL; 11550 pending_io = next_io) { 11551 pending_io->io_hdr.flags &= 11552 ~CTL_FLAG_SENT_2OTHER_SC; 11553 next_io = (union ctl_io *)TAILQ_NEXT( 11554 &pending_io->io_hdr, ooa_links); 11555 if (pending_io->io_hdr.flags & 11556 CTL_FLAG_IS_WAS_ON_RTR) { 11557#if 0 11558 printf("Tag 0x%04x is running\n", 11559 pending_io->scsiio.tag_num); 11560#endif 11561 continue; 11562 } 11563 11564 switch (ctl_check_ooa(lun, pending_io, 11565 (union ctl_io *)TAILQ_PREV( 11566 &pending_io->io_hdr, ctl_ooaq, 11567 ooa_links))) { 11568 11569 case CTL_ACTION_BLOCK: 11570 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11571 &pending_io->io_hdr, 11572 blocked_links); 11573 pending_io->io_hdr.flags |= 11574 CTL_FLAG_BLOCKED; 11575 break; 11576 case CTL_ACTION_PASS: 11577 case CTL_ACTION_SKIP: 11578 pending_io->io_hdr.flags |= 11579 CTL_FLAG_IS_WAS_ON_RTR; 11580 ctl_enqueue_rtr(pending_io); 11581 break; 11582 case CTL_ACTION_OVERLAP: 11583 ctl_set_overlapped_cmd( 11584 (struct ctl_scsiio *)pending_io); 11585 ctl_done(pending_io); 11586 break; 11587 case CTL_ACTION_OVERLAP_TAG: 11588 ctl_set_overlapped_tag( 11589 (struct ctl_scsiio *)pending_io, 11590 pending_io->scsiio.tag_num & 0xff); 11591 ctl_done(pending_io); 11592 break; 11593 case CTL_ACTION_ERROR: 11594 default: 11595 ctl_set_internal_failure( 11596 (struct ctl_scsiio *)pending_io, 11597 0, // sks_valid 11598 0); //retry count 11599 ctl_done(pending_io); 11600 break; 11601 } 11602 } 11603 11604 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11605 } else { 11606 panic("Unhandled HA mode failover, LUN flags = %#x, " 11607 "ha_mode = #%x", lun->flags, softc->ha_mode); 11608 } 11609 } 11610 ctl_pause_rtr = 0; 11611 mtx_unlock(&softc->ctl_lock); 11612} 11613 11614static void 11615ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 11616 ctl_ua_type ua_type) 11617{ 11618 struct ctl_lun *lun; 11619 ctl_ua_type *pu; 11620 11621 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 11622 11623 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 11624 mtx_lock(&lun->lun_lock); 11625 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 11626 if (pu != NULL) 11627 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type; 11628 mtx_unlock(&lun->lun_lock); 11629 } 11630} 11631 11632static int 11633ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11634{ 11635 struct ctl_lun *lun; 11636 const struct ctl_cmd_entry *entry; 11637 uint32_t initidx, targ_lun; 11638 int retval; 11639 11640 retval = 0; 11641 11642 lun = NULL; 11643 11644 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11645 if ((targ_lun < CTL_MAX_LUNS) 11646 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11647 /* 11648 * If the LUN is invalid, pretend that it doesn't exist. 11649 * It will go away as soon as all pending I/O has been 11650 * completed. 11651 */ 11652 mtx_lock(&lun->lun_lock); 11653 if (lun->flags & CTL_LUN_DISABLED) { 11654 mtx_unlock(&lun->lun_lock); 11655 lun = NULL; 11656 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11657 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11658 } else { 11659 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11660 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11661 lun->be_lun; 11662 if (lun->be_lun->lun_type == T_PROCESSOR) { 11663 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11664 } 11665 11666 /* 11667 * Every I/O goes into the OOA queue for a 11668 * particular LUN, and stays there until completion. 11669 */ 11670#ifdef CTL_TIME_IO 11671 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11672 lun->idle_time += getsbinuptime() - 11673 lun->last_busy; 11674 } 11675#endif 11676 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11677 ooa_links); 11678 } 11679 } else { 11680 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11681 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11682 } 11683 11684 /* Get command entry and return error if it is unsuppotyed. */ 11685 entry = ctl_validate_command(ctsio); 11686 if (entry == NULL) { 11687 if (lun) 11688 mtx_unlock(&lun->lun_lock); 11689 return (retval); 11690 } 11691 11692 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11693 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11694 11695 /* 11696 * Check to see whether we can send this command to LUNs that don't 11697 * exist. This should pretty much only be the case for inquiry 11698 * and request sense. Further checks, below, really require having 11699 * a LUN, so we can't really check the command anymore. Just put 11700 * it on the rtr queue. 11701 */ 11702 if (lun == NULL) { 11703 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11704 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11705 ctl_enqueue_rtr((union ctl_io *)ctsio); 11706 return (retval); 11707 } 11708 11709 ctl_set_unsupported_lun(ctsio); 11710 ctl_done((union ctl_io *)ctsio); 11711 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11712 return (retval); 11713 } else { 11714 /* 11715 * Make sure we support this particular command on this LUN. 11716 * e.g., we don't support writes to the control LUN. 11717 */ 11718 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11719 mtx_unlock(&lun->lun_lock); 11720 ctl_set_invalid_opcode(ctsio); 11721 ctl_done((union ctl_io *)ctsio); 11722 return (retval); 11723 } 11724 } 11725 11726 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11727 11728#ifdef CTL_WITH_CA 11729 /* 11730 * If we've got a request sense, it'll clear the contingent 11731 * allegiance condition. Otherwise, if we have a CA condition for 11732 * this initiator, clear it, because it sent down a command other 11733 * than request sense. 11734 */ 11735 if ((ctsio->cdb[0] != REQUEST_SENSE) 11736 && (ctl_is_set(lun->have_ca, initidx))) 11737 ctl_clear_mask(lun->have_ca, initidx); 11738#endif 11739 11740 /* 11741 * If the command has this flag set, it handles its own unit 11742 * attention reporting, we shouldn't do anything. Otherwise we 11743 * check for any pending unit attentions, and send them back to the 11744 * initiator. We only do this when a command initially comes in, 11745 * not when we pull it off the blocked queue. 11746 * 11747 * According to SAM-3, section 5.3.2, the order that things get 11748 * presented back to the host is basically unit attentions caused 11749 * by some sort of reset event, busy status, reservation conflicts 11750 * or task set full, and finally any other status. 11751 * 11752 * One issue here is that some of the unit attentions we report 11753 * don't fall into the "reset" category (e.g. "reported luns data 11754 * has changed"). So reporting it here, before the reservation 11755 * check, may be technically wrong. I guess the only thing to do 11756 * would be to check for and report the reset events here, and then 11757 * check for the other unit attention types after we check for a 11758 * reservation conflict. 11759 * 11760 * XXX KDM need to fix this 11761 */ 11762 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11763 ctl_ua_type ua_type; 11764 scsi_sense_data_type sense_format; 11765 11766 if (lun->flags & CTL_LUN_SENSE_DESC) 11767 sense_format = SSD_TYPE_DESC; 11768 else 11769 sense_format = SSD_TYPE_FIXED; 11770 11771 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11772 sense_format); 11773 if (ua_type != CTL_UA_NONE) { 11774 mtx_unlock(&lun->lun_lock); 11775 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11776 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11777 ctsio->sense_len = SSD_FULL_SIZE; 11778 ctl_done((union ctl_io *)ctsio); 11779 return (retval); 11780 } 11781 } 11782 11783 11784 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11785 mtx_unlock(&lun->lun_lock); 11786 ctl_done((union ctl_io *)ctsio); 11787 return (retval); 11788 } 11789 11790 /* 11791 * XXX CHD this is where we want to send IO to other side if 11792 * this LUN is secondary on this SC. We will need to make a copy 11793 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11794 * the copy we send as FROM_OTHER. 11795 * We also need to stuff the address of the original IO so we can 11796 * find it easily. Something similar will need be done on the other 11797 * side so when we are done we can find the copy. 11798 */ 11799 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11800 union ctl_ha_msg msg_info; 11801 int isc_retval; 11802 11803 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11804 11805 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11806 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11807#if 0 11808 printf("1. ctsio %p\n", ctsio); 11809#endif 11810 msg_info.hdr.serializing_sc = NULL; 11811 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11812 msg_info.scsi.tag_num = ctsio->tag_num; 11813 msg_info.scsi.tag_type = ctsio->tag_type; 11814 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11815 11816 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11817 11818 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11819 (void *)&msg_info, sizeof(msg_info), 0)) > 11820 CTL_HA_STATUS_SUCCESS) { 11821 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11822 isc_retval); 11823 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11824 } else { 11825#if 0 11826 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11827#endif 11828 } 11829 11830 /* 11831 * XXX KDM this I/O is off the incoming queue, but hasn't 11832 * been inserted on any other queue. We may need to come 11833 * up with a holding queue while we wait for serialization 11834 * so that we have an idea of what we're waiting for from 11835 * the other side. 11836 */ 11837 mtx_unlock(&lun->lun_lock); 11838 return (retval); 11839 } 11840 11841 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11842 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11843 ctl_ooaq, ooa_links))) { 11844 case CTL_ACTION_BLOCK: 11845 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11846 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11847 blocked_links); 11848 mtx_unlock(&lun->lun_lock); 11849 return (retval); 11850 case CTL_ACTION_PASS: 11851 case CTL_ACTION_SKIP: 11852 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11853 mtx_unlock(&lun->lun_lock); 11854 ctl_enqueue_rtr((union ctl_io *)ctsio); 11855 break; 11856 case CTL_ACTION_OVERLAP: 11857 mtx_unlock(&lun->lun_lock); 11858 ctl_set_overlapped_cmd(ctsio); 11859 ctl_done((union ctl_io *)ctsio); 11860 break; 11861 case CTL_ACTION_OVERLAP_TAG: 11862 mtx_unlock(&lun->lun_lock); 11863 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11864 ctl_done((union ctl_io *)ctsio); 11865 break; 11866 case CTL_ACTION_ERROR: 11867 default: 11868 mtx_unlock(&lun->lun_lock); 11869 ctl_set_internal_failure(ctsio, 11870 /*sks_valid*/ 0, 11871 /*retry_count*/ 0); 11872 ctl_done((union ctl_io *)ctsio); 11873 break; 11874 } 11875 return (retval); 11876} 11877 11878const struct ctl_cmd_entry * 11879ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11880{ 11881 const struct ctl_cmd_entry *entry; 11882 int service_action; 11883 11884 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11885 if (sa) 11886 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11887 if (entry->flags & CTL_CMD_FLAG_SA5) { 11888 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11889 entry = &((const struct ctl_cmd_entry *) 11890 entry->execute)[service_action]; 11891 } 11892 return (entry); 11893} 11894 11895const struct ctl_cmd_entry * 11896ctl_validate_command(struct ctl_scsiio *ctsio) 11897{ 11898 const struct ctl_cmd_entry *entry; 11899 int i, sa; 11900 uint8_t diff; 11901 11902 entry = ctl_get_cmd_entry(ctsio, &sa); 11903 if (entry->execute == NULL) { 11904 if (sa) 11905 ctl_set_invalid_field(ctsio, 11906 /*sks_valid*/ 1, 11907 /*command*/ 1, 11908 /*field*/ 1, 11909 /*bit_valid*/ 1, 11910 /*bit*/ 4); 11911 else 11912 ctl_set_invalid_opcode(ctsio); 11913 ctl_done((union ctl_io *)ctsio); 11914 return (NULL); 11915 } 11916 KASSERT(entry->length > 0, 11917 ("Not defined length for command 0x%02x/0x%02x", 11918 ctsio->cdb[0], ctsio->cdb[1])); 11919 for (i = 1; i < entry->length; i++) { 11920 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11921 if (diff == 0) 11922 continue; 11923 ctl_set_invalid_field(ctsio, 11924 /*sks_valid*/ 1, 11925 /*command*/ 1, 11926 /*field*/ i, 11927 /*bit_valid*/ 1, 11928 /*bit*/ fls(diff) - 1); 11929 ctl_done((union ctl_io *)ctsio); 11930 return (NULL); 11931 } 11932 return (entry); 11933} 11934 11935static int 11936ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11937{ 11938 11939 switch (lun_type) { 11940 case T_PROCESSOR: 11941 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11942 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11943 return (0); 11944 break; 11945 case T_DIRECT: 11946 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11947 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11948 return (0); 11949 break; 11950 default: 11951 return (0); 11952 } 11953 return (1); 11954} 11955 11956static int 11957ctl_scsiio(struct ctl_scsiio *ctsio) 11958{ 11959 int retval; 11960 const struct ctl_cmd_entry *entry; 11961 11962 retval = CTL_RETVAL_COMPLETE; 11963 11964 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11965 11966 entry = ctl_get_cmd_entry(ctsio, NULL); 11967 11968 /* 11969 * If this I/O has been aborted, just send it straight to 11970 * ctl_done() without executing it. 11971 */ 11972 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11973 ctl_done((union ctl_io *)ctsio); 11974 goto bailout; 11975 } 11976 11977 /* 11978 * All the checks should have been handled by ctl_scsiio_precheck(). 11979 * We should be clear now to just execute the I/O. 11980 */ 11981 retval = entry->execute(ctsio); 11982 11983bailout: 11984 return (retval); 11985} 11986 11987/* 11988 * Since we only implement one target right now, a bus reset simply resets 11989 * our single target. 11990 */ 11991static int 11992ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11993{ 11994 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11995} 11996 11997static int 11998ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11999 ctl_ua_type ua_type) 12000{ 12001 struct ctl_lun *lun; 12002 int retval; 12003 12004 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12005 union ctl_ha_msg msg_info; 12006 12007 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12008 msg_info.hdr.nexus = io->io_hdr.nexus; 12009 if (ua_type==CTL_UA_TARG_RESET) 12010 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12011 else 12012 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12013 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12014 msg_info.hdr.original_sc = NULL; 12015 msg_info.hdr.serializing_sc = NULL; 12016 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12017 (void *)&msg_info, sizeof(msg_info), 0)) { 12018 } 12019 } 12020 retval = 0; 12021 12022 mtx_lock(&softc->ctl_lock); 12023 STAILQ_FOREACH(lun, &softc->lun_list, links) 12024 retval += ctl_lun_reset(lun, io, ua_type); 12025 mtx_unlock(&softc->ctl_lock); 12026 12027 return (retval); 12028} 12029 12030/* 12031 * The LUN should always be set. The I/O is optional, and is used to 12032 * distinguish between I/Os sent by this initiator, and by other 12033 * initiators. We set unit attention for initiators other than this one. 12034 * SAM-3 is vague on this point. It does say that a unit attention should 12035 * be established for other initiators when a LUN is reset (see section 12036 * 5.7.3), but it doesn't specifically say that the unit attention should 12037 * be established for this particular initiator when a LUN is reset. Here 12038 * is the relevant text, from SAM-3 rev 8: 12039 * 12040 * 5.7.2 When a SCSI initiator port aborts its own tasks 12041 * 12042 * When a SCSI initiator port causes its own task(s) to be aborted, no 12043 * notification that the task(s) have been aborted shall be returned to 12044 * the SCSI initiator port other than the completion response for the 12045 * command or task management function action that caused the task(s) to 12046 * be aborted and notification(s) associated with related effects of the 12047 * action (e.g., a reset unit attention condition). 12048 * 12049 * XXX KDM for now, we're setting unit attention for all initiators. 12050 */ 12051static int 12052ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12053{ 12054 union ctl_io *xio; 12055#if 0 12056 uint32_t initidx; 12057#endif 12058#ifdef CTL_WITH_CA 12059 int i; 12060#endif 12061 12062 mtx_lock(&lun->lun_lock); 12063 /* 12064 * Run through the OOA queue and abort each I/O. 12065 */ 12066 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12067 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12068 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12069 } 12070 12071 /* 12072 * This version sets unit attention for every 12073 */ 12074#if 0 12075 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12076 ctl_est_ua_all(lun, initidx, ua_type); 12077#else 12078 ctl_est_ua_all(lun, -1, ua_type); 12079#endif 12080 12081 /* 12082 * A reset (any kind, really) clears reservations established with 12083 * RESERVE/RELEASE. It does not clear reservations established 12084 * with PERSISTENT RESERVE OUT, but we don't support that at the 12085 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12086 * reservations made with the RESERVE/RELEASE commands, because 12087 * those commands are obsolete in SPC-3. 12088 */ 12089 lun->flags &= ~CTL_LUN_RESERVED; 12090 12091#ifdef CTL_WITH_CA 12092 for (i = 0; i < CTL_MAX_INITIATORS; i++) 12093 ctl_clear_mask(lun->have_ca, i); 12094#endif 12095 mtx_unlock(&lun->lun_lock); 12096 12097 return (0); 12098} 12099 12100static void 12101ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12102 int other_sc) 12103{ 12104 union ctl_io *xio; 12105 12106 mtx_assert(&lun->lun_lock, MA_OWNED); 12107 12108 /* 12109 * Run through the OOA queue and attempt to find the given I/O. 12110 * The target port, initiator ID, tag type and tag number have to 12111 * match the values that we got from the initiator. If we have an 12112 * untagged command to abort, simply abort the first untagged command 12113 * we come to. We only allow one untagged command at a time of course. 12114 */ 12115 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12116 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12117 12118 if ((targ_port == UINT32_MAX || 12119 targ_port == xio->io_hdr.nexus.targ_port) && 12120 (init_id == UINT32_MAX || 12121 init_id == xio->io_hdr.nexus.initid.id)) { 12122 if (targ_port != xio->io_hdr.nexus.targ_port || 12123 init_id != xio->io_hdr.nexus.initid.id) 12124 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12125 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12126 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12127 union ctl_ha_msg msg_info; 12128 12129 msg_info.hdr.nexus = xio->io_hdr.nexus; 12130 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12131 msg_info.task.tag_num = xio->scsiio.tag_num; 12132 msg_info.task.tag_type = xio->scsiio.tag_type; 12133 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12134 msg_info.hdr.original_sc = NULL; 12135 msg_info.hdr.serializing_sc = NULL; 12136 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12137 (void *)&msg_info, sizeof(msg_info), 0); 12138 } 12139 } 12140 } 12141} 12142 12143static int 12144ctl_abort_task_set(union ctl_io *io) 12145{ 12146 struct ctl_softc *softc = control_softc; 12147 struct ctl_lun *lun; 12148 uint32_t targ_lun; 12149 12150 /* 12151 * Look up the LUN. 12152 */ 12153 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12154 mtx_lock(&softc->ctl_lock); 12155 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12156 lun = softc->ctl_luns[targ_lun]; 12157 else { 12158 mtx_unlock(&softc->ctl_lock); 12159 return (1); 12160 } 12161 12162 mtx_lock(&lun->lun_lock); 12163 mtx_unlock(&softc->ctl_lock); 12164 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12165 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12166 io->io_hdr.nexus.initid.id, 12167 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12168 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12169 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12170 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12171 } 12172 mtx_unlock(&lun->lun_lock); 12173 return (0); 12174} 12175 12176static int 12177ctl_i_t_nexus_reset(union ctl_io *io) 12178{ 12179 struct ctl_softc *softc = control_softc; 12180 struct ctl_lun *lun; 12181 uint32_t initidx, residx; 12182 12183 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12184 residx = ctl_get_resindex(&io->io_hdr.nexus); 12185 mtx_lock(&softc->ctl_lock); 12186 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12187 mtx_lock(&lun->lun_lock); 12188 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12189 io->io_hdr.nexus.initid.id, 12190 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12191#ifdef CTL_WITH_CA 12192 ctl_clear_mask(lun->have_ca, initidx); 12193#endif 12194 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12195 lun->flags &= ~CTL_LUN_RESERVED; 12196 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12197 mtx_unlock(&lun->lun_lock); 12198 } 12199 mtx_unlock(&softc->ctl_lock); 12200 return (0); 12201} 12202 12203static int 12204ctl_abort_task(union ctl_io *io) 12205{ 12206 union ctl_io *xio; 12207 struct ctl_lun *lun; 12208 struct ctl_softc *softc; 12209#if 0 12210 struct sbuf sb; 12211 char printbuf[128]; 12212#endif 12213 int found; 12214 uint32_t targ_lun; 12215 12216 softc = control_softc; 12217 found = 0; 12218 12219 /* 12220 * Look up the LUN. 12221 */ 12222 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12223 mtx_lock(&softc->ctl_lock); 12224 if ((targ_lun < CTL_MAX_LUNS) 12225 && (softc->ctl_luns[targ_lun] != NULL)) 12226 lun = softc->ctl_luns[targ_lun]; 12227 else { 12228 mtx_unlock(&softc->ctl_lock); 12229 return (1); 12230 } 12231 12232#if 0 12233 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12234 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12235#endif 12236 12237 mtx_lock(&lun->lun_lock); 12238 mtx_unlock(&softc->ctl_lock); 12239 /* 12240 * Run through the OOA queue and attempt to find the given I/O. 12241 * The target port, initiator ID, tag type and tag number have to 12242 * match the values that we got from the initiator. If we have an 12243 * untagged command to abort, simply abort the first untagged command 12244 * we come to. We only allow one untagged command at a time of course. 12245 */ 12246 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12247 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12248#if 0 12249 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12250 12251 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12252 lun->lun, xio->scsiio.tag_num, 12253 xio->scsiio.tag_type, 12254 (xio->io_hdr.blocked_links.tqe_prev 12255 == NULL) ? "" : " BLOCKED", 12256 (xio->io_hdr.flags & 12257 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12258 (xio->io_hdr.flags & 12259 CTL_FLAG_ABORT) ? " ABORT" : "", 12260 (xio->io_hdr.flags & 12261 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12262 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12263 sbuf_finish(&sb); 12264 printf("%s\n", sbuf_data(&sb)); 12265#endif 12266 12267 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12268 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id) 12269 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12270 continue; 12271 12272 /* 12273 * If the abort says that the task is untagged, the 12274 * task in the queue must be untagged. Otherwise, 12275 * we just check to see whether the tag numbers 12276 * match. This is because the QLogic firmware 12277 * doesn't pass back the tag type in an abort 12278 * request. 12279 */ 12280#if 0 12281 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12282 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12283 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12284#endif 12285 /* 12286 * XXX KDM we've got problems with FC, because it 12287 * doesn't send down a tag type with aborts. So we 12288 * can only really go by the tag number... 12289 * This may cause problems with parallel SCSI. 12290 * Need to figure that out!! 12291 */ 12292 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12293 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12294 found = 1; 12295 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12296 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12297 union ctl_ha_msg msg_info; 12298 12299 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12300 msg_info.hdr.nexus = io->io_hdr.nexus; 12301 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12302 msg_info.task.tag_num = io->taskio.tag_num; 12303 msg_info.task.tag_type = io->taskio.tag_type; 12304 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12305 msg_info.hdr.original_sc = NULL; 12306 msg_info.hdr.serializing_sc = NULL; 12307#if 0 12308 printf("Sent Abort to other side\n"); 12309#endif 12310 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12311 (void *)&msg_info, sizeof(msg_info), 0) != 12312 CTL_HA_STATUS_SUCCESS) { 12313 } 12314 } 12315#if 0 12316 printf("ctl_abort_task: found I/O to abort\n"); 12317#endif 12318 } 12319 } 12320 mtx_unlock(&lun->lun_lock); 12321 12322 if (found == 0) { 12323 /* 12324 * This isn't really an error. It's entirely possible for 12325 * the abort and command completion to cross on the wire. 12326 * This is more of an informative/diagnostic error. 12327 */ 12328#if 0 12329 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12330 "%d:%d:%d:%d tag %d type %d\n", 12331 io->io_hdr.nexus.initid.id, 12332 io->io_hdr.nexus.targ_port, 12333 io->io_hdr.nexus.targ_target.id, 12334 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12335 io->taskio.tag_type); 12336#endif 12337 } 12338 return (0); 12339} 12340 12341static void 12342ctl_run_task(union ctl_io *io) 12343{ 12344 struct ctl_softc *softc = control_softc; 12345 int retval = 1; 12346 const char *task_desc; 12347 12348 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12349 12350 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12351 ("ctl_run_task: Unextected io_type %d\n", 12352 io->io_hdr.io_type)); 12353 12354 task_desc = ctl_scsi_task_string(&io->taskio); 12355 if (task_desc != NULL) { 12356#ifdef NEEDTOPORT 12357 csevent_log(CSC_CTL | CSC_SHELF_SW | 12358 CTL_TASK_REPORT, 12359 csevent_LogType_Trace, 12360 csevent_Severity_Information, 12361 csevent_AlertLevel_Green, 12362 csevent_FRU_Firmware, 12363 csevent_FRU_Unknown, 12364 "CTL: received task: %s",task_desc); 12365#endif 12366 } else { 12367#ifdef NEEDTOPORT 12368 csevent_log(CSC_CTL | CSC_SHELF_SW | 12369 CTL_TASK_REPORT, 12370 csevent_LogType_Trace, 12371 csevent_Severity_Information, 12372 csevent_AlertLevel_Green, 12373 csevent_FRU_Firmware, 12374 csevent_FRU_Unknown, 12375 "CTL: received unknown task " 12376 "type: %d (%#x)", 12377 io->taskio.task_action, 12378 io->taskio.task_action); 12379#endif 12380 } 12381 switch (io->taskio.task_action) { 12382 case CTL_TASK_ABORT_TASK: 12383 retval = ctl_abort_task(io); 12384 break; 12385 case CTL_TASK_ABORT_TASK_SET: 12386 case CTL_TASK_CLEAR_TASK_SET: 12387 retval = ctl_abort_task_set(io); 12388 break; 12389 case CTL_TASK_CLEAR_ACA: 12390 break; 12391 case CTL_TASK_I_T_NEXUS_RESET: 12392 retval = ctl_i_t_nexus_reset(io); 12393 break; 12394 case CTL_TASK_LUN_RESET: { 12395 struct ctl_lun *lun; 12396 uint32_t targ_lun; 12397 12398 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12399 mtx_lock(&softc->ctl_lock); 12400 if ((targ_lun < CTL_MAX_LUNS) 12401 && (softc->ctl_luns[targ_lun] != NULL)) 12402 lun = softc->ctl_luns[targ_lun]; 12403 else { 12404 mtx_unlock(&softc->ctl_lock); 12405 retval = 1; 12406 break; 12407 } 12408 12409 if (!(io->io_hdr.flags & 12410 CTL_FLAG_FROM_OTHER_SC)) { 12411 union ctl_ha_msg msg_info; 12412 12413 io->io_hdr.flags |= 12414 CTL_FLAG_SENT_2OTHER_SC; 12415 msg_info.hdr.msg_type = 12416 CTL_MSG_MANAGE_TASKS; 12417 msg_info.hdr.nexus = io->io_hdr.nexus; 12418 msg_info.task.task_action = 12419 CTL_TASK_LUN_RESET; 12420 msg_info.hdr.original_sc = NULL; 12421 msg_info.hdr.serializing_sc = NULL; 12422 if (CTL_HA_STATUS_SUCCESS != 12423 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12424 (void *)&msg_info, 12425 sizeof(msg_info), 0)) { 12426 } 12427 } 12428 12429 retval = ctl_lun_reset(lun, io, 12430 CTL_UA_LUN_RESET); 12431 mtx_unlock(&softc->ctl_lock); 12432 break; 12433 } 12434 case CTL_TASK_TARGET_RESET: 12435 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12436 break; 12437 case CTL_TASK_BUS_RESET: 12438 retval = ctl_bus_reset(softc, io); 12439 break; 12440 case CTL_TASK_PORT_LOGIN: 12441 break; 12442 case CTL_TASK_PORT_LOGOUT: 12443 break; 12444 default: 12445 printf("ctl_run_task: got unknown task management event %d\n", 12446 io->taskio.task_action); 12447 break; 12448 } 12449 if (retval == 0) 12450 io->io_hdr.status = CTL_SUCCESS; 12451 else 12452 io->io_hdr.status = CTL_ERROR; 12453 ctl_done(io); 12454} 12455 12456/* 12457 * For HA operation. Handle commands that come in from the other 12458 * controller. 12459 */ 12460static void 12461ctl_handle_isc(union ctl_io *io) 12462{ 12463 int free_io; 12464 struct ctl_lun *lun; 12465 struct ctl_softc *softc; 12466 uint32_t targ_lun; 12467 12468 softc = control_softc; 12469 12470 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12471 lun = softc->ctl_luns[targ_lun]; 12472 12473 switch (io->io_hdr.msg_type) { 12474 case CTL_MSG_SERIALIZE: 12475 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12476 break; 12477 case CTL_MSG_R2R: { 12478 const struct ctl_cmd_entry *entry; 12479 12480 /* 12481 * This is only used in SER_ONLY mode. 12482 */ 12483 free_io = 0; 12484 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12485 mtx_lock(&lun->lun_lock); 12486 if (ctl_scsiio_lun_check(lun, 12487 entry, (struct ctl_scsiio *)io) != 0) { 12488 mtx_unlock(&lun->lun_lock); 12489 ctl_done(io); 12490 break; 12491 } 12492 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12493 mtx_unlock(&lun->lun_lock); 12494 ctl_enqueue_rtr(io); 12495 break; 12496 } 12497 case CTL_MSG_FINISH_IO: 12498 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12499 free_io = 0; 12500 ctl_done(io); 12501 } else { 12502 free_io = 1; 12503 mtx_lock(&lun->lun_lock); 12504 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12505 ooa_links); 12506 ctl_check_blocked(lun); 12507 mtx_unlock(&lun->lun_lock); 12508 } 12509 break; 12510 case CTL_MSG_PERS_ACTION: 12511 ctl_hndl_per_res_out_on_other_sc( 12512 (union ctl_ha_msg *)&io->presio.pr_msg); 12513 free_io = 1; 12514 break; 12515 case CTL_MSG_BAD_JUJU: 12516 free_io = 0; 12517 ctl_done(io); 12518 break; 12519 case CTL_MSG_DATAMOVE: 12520 /* Only used in XFER mode */ 12521 free_io = 0; 12522 ctl_datamove_remote(io); 12523 break; 12524 case CTL_MSG_DATAMOVE_DONE: 12525 /* Only used in XFER mode */ 12526 free_io = 0; 12527 io->scsiio.be_move_done(io); 12528 break; 12529 default: 12530 free_io = 1; 12531 printf("%s: Invalid message type %d\n", 12532 __func__, io->io_hdr.msg_type); 12533 break; 12534 } 12535 if (free_io) 12536 ctl_free_io(io); 12537 12538} 12539 12540 12541/* 12542 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12543 * there is no match. 12544 */ 12545static ctl_lun_error_pattern 12546ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12547{ 12548 const struct ctl_cmd_entry *entry; 12549 ctl_lun_error_pattern filtered_pattern, pattern; 12550 12551 pattern = desc->error_pattern; 12552 12553 /* 12554 * XXX KDM we need more data passed into this function to match a 12555 * custom pattern, and we actually need to implement custom pattern 12556 * matching. 12557 */ 12558 if (pattern & CTL_LUN_PAT_CMD) 12559 return (CTL_LUN_PAT_CMD); 12560 12561 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12562 return (CTL_LUN_PAT_ANY); 12563 12564 entry = ctl_get_cmd_entry(ctsio, NULL); 12565 12566 filtered_pattern = entry->pattern & pattern; 12567 12568 /* 12569 * If the user requested specific flags in the pattern (e.g. 12570 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12571 * flags. 12572 * 12573 * If the user did not specify any flags, it doesn't matter whether 12574 * or not the command supports the flags. 12575 */ 12576 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12577 (pattern & ~CTL_LUN_PAT_MASK)) 12578 return (CTL_LUN_PAT_NONE); 12579 12580 /* 12581 * If the user asked for a range check, see if the requested LBA 12582 * range overlaps with this command's LBA range. 12583 */ 12584 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12585 uint64_t lba1; 12586 uint64_t len1; 12587 ctl_action action; 12588 int retval; 12589 12590 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12591 if (retval != 0) 12592 return (CTL_LUN_PAT_NONE); 12593 12594 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12595 desc->lba_range.len, FALSE); 12596 /* 12597 * A "pass" means that the LBA ranges don't overlap, so 12598 * this doesn't match the user's range criteria. 12599 */ 12600 if (action == CTL_ACTION_PASS) 12601 return (CTL_LUN_PAT_NONE); 12602 } 12603 12604 return (filtered_pattern); 12605} 12606 12607static void 12608ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12609{ 12610 struct ctl_error_desc *desc, *desc2; 12611 12612 mtx_assert(&lun->lun_lock, MA_OWNED); 12613 12614 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12615 ctl_lun_error_pattern pattern; 12616 /* 12617 * Check to see whether this particular command matches 12618 * the pattern in the descriptor. 12619 */ 12620 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12621 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12622 continue; 12623 12624 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12625 case CTL_LUN_INJ_ABORTED: 12626 ctl_set_aborted(&io->scsiio); 12627 break; 12628 case CTL_LUN_INJ_MEDIUM_ERR: 12629 ctl_set_medium_error(&io->scsiio); 12630 break; 12631 case CTL_LUN_INJ_UA: 12632 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12633 * OCCURRED */ 12634 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12635 break; 12636 case CTL_LUN_INJ_CUSTOM: 12637 /* 12638 * We're assuming the user knows what he is doing. 12639 * Just copy the sense information without doing 12640 * checks. 12641 */ 12642 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12643 MIN(sizeof(desc->custom_sense), 12644 sizeof(io->scsiio.sense_data))); 12645 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12646 io->scsiio.sense_len = SSD_FULL_SIZE; 12647 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12648 break; 12649 case CTL_LUN_INJ_NONE: 12650 default: 12651 /* 12652 * If this is an error injection type we don't know 12653 * about, clear the continuous flag (if it is set) 12654 * so it will get deleted below. 12655 */ 12656 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12657 break; 12658 } 12659 /* 12660 * By default, each error injection action is a one-shot 12661 */ 12662 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12663 continue; 12664 12665 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12666 12667 free(desc, M_CTL); 12668 } 12669} 12670 12671#ifdef CTL_IO_DELAY 12672static void 12673ctl_datamove_timer_wakeup(void *arg) 12674{ 12675 union ctl_io *io; 12676 12677 io = (union ctl_io *)arg; 12678 12679 ctl_datamove(io); 12680} 12681#endif /* CTL_IO_DELAY */ 12682 12683void 12684ctl_datamove(union ctl_io *io) 12685{ 12686 void (*fe_datamove)(union ctl_io *io); 12687 12688 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12689 12690 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12691 12692#ifdef CTL_TIME_IO 12693 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12694 char str[256]; 12695 char path_str[64]; 12696 struct sbuf sb; 12697 12698 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12699 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12700 12701 sbuf_cat(&sb, path_str); 12702 switch (io->io_hdr.io_type) { 12703 case CTL_IO_SCSI: 12704 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12705 sbuf_printf(&sb, "\n"); 12706 sbuf_cat(&sb, path_str); 12707 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12708 io->scsiio.tag_num, io->scsiio.tag_type); 12709 break; 12710 case CTL_IO_TASK: 12711 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12712 "Tag Type: %d\n", io->taskio.task_action, 12713 io->taskio.tag_num, io->taskio.tag_type); 12714 break; 12715 default: 12716 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12717 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12718 break; 12719 } 12720 sbuf_cat(&sb, path_str); 12721 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12722 (intmax_t)time_uptime - io->io_hdr.start_time); 12723 sbuf_finish(&sb); 12724 printf("%s", sbuf_data(&sb)); 12725 } 12726#endif /* CTL_TIME_IO */ 12727 12728#ifdef CTL_IO_DELAY 12729 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12730 struct ctl_lun *lun; 12731 12732 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12733 12734 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12735 } else { 12736 struct ctl_lun *lun; 12737 12738 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12739 if ((lun != NULL) 12740 && (lun->delay_info.datamove_delay > 0)) { 12741 struct callout *callout; 12742 12743 callout = (struct callout *)&io->io_hdr.timer_bytes; 12744 callout_init(callout, /*mpsafe*/ 1); 12745 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12746 callout_reset(callout, 12747 lun->delay_info.datamove_delay * hz, 12748 ctl_datamove_timer_wakeup, io); 12749 if (lun->delay_info.datamove_type == 12750 CTL_DELAY_TYPE_ONESHOT) 12751 lun->delay_info.datamove_delay = 0; 12752 return; 12753 } 12754 } 12755#endif 12756 12757 /* 12758 * This command has been aborted. Set the port status, so we fail 12759 * the data move. 12760 */ 12761 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12762 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12763 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12764 io->io_hdr.nexus.targ_port, 12765 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12766 io->io_hdr.nexus.targ_lun); 12767 io->io_hdr.port_status = 31337; 12768 /* 12769 * Note that the backend, in this case, will get the 12770 * callback in its context. In other cases it may get 12771 * called in the frontend's interrupt thread context. 12772 */ 12773 io->scsiio.be_move_done(io); 12774 return; 12775 } 12776 12777 /* Don't confuse frontend with zero length data move. */ 12778 if (io->scsiio.kern_data_len == 0) { 12779 io->scsiio.be_move_done(io); 12780 return; 12781 } 12782 12783 /* 12784 * If we're in XFER mode and this I/O is from the other shelf 12785 * controller, we need to send the DMA to the other side to 12786 * actually transfer the data to/from the host. In serialize only 12787 * mode the transfer happens below CTL and ctl_datamove() is only 12788 * called on the machine that originally received the I/O. 12789 */ 12790 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12791 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12792 union ctl_ha_msg msg; 12793 uint32_t sg_entries_sent; 12794 int do_sg_copy; 12795 int i; 12796 12797 memset(&msg, 0, sizeof(msg)); 12798 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12799 msg.hdr.original_sc = io->io_hdr.original_sc; 12800 msg.hdr.serializing_sc = io; 12801 msg.hdr.nexus = io->io_hdr.nexus; 12802 msg.dt.flags = io->io_hdr.flags; 12803 /* 12804 * We convert everything into a S/G list here. We can't 12805 * pass by reference, only by value between controllers. 12806 * So we can't pass a pointer to the S/G list, only as many 12807 * S/G entries as we can fit in here. If it's possible for 12808 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12809 * then we need to break this up into multiple transfers. 12810 */ 12811 if (io->scsiio.kern_sg_entries == 0) { 12812 msg.dt.kern_sg_entries = 1; 12813 /* 12814 * If this is in cached memory, flush the cache 12815 * before we send the DMA request to the other 12816 * controller. We want to do this in either the 12817 * read or the write case. The read case is 12818 * straightforward. In the write case, we want to 12819 * make sure nothing is in the local cache that 12820 * could overwrite the DMAed data. 12821 */ 12822 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12823 /* 12824 * XXX KDM use bus_dmamap_sync() here. 12825 */ 12826 } 12827 12828 /* 12829 * Convert to a physical address if this is a 12830 * virtual address. 12831 */ 12832 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12833 msg.dt.sg_list[0].addr = 12834 io->scsiio.kern_data_ptr; 12835 } else { 12836 /* 12837 * XXX KDM use busdma here! 12838 */ 12839#if 0 12840 msg.dt.sg_list[0].addr = (void *) 12841 vtophys(io->scsiio.kern_data_ptr); 12842#endif 12843 } 12844 12845 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12846 do_sg_copy = 0; 12847 } else { 12848 struct ctl_sg_entry *sgl; 12849 12850 do_sg_copy = 1; 12851 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12852 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12853 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12854 /* 12855 * XXX KDM use bus_dmamap_sync() here. 12856 */ 12857 } 12858 } 12859 12860 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12861 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12862 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12863 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12864 msg.dt.sg_sequence = 0; 12865 12866 /* 12867 * Loop until we've sent all of the S/G entries. On the 12868 * other end, we'll recompose these S/G entries into one 12869 * contiguous list before passing it to the 12870 */ 12871 for (sg_entries_sent = 0; sg_entries_sent < 12872 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12873 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12874 sizeof(msg.dt.sg_list[0])), 12875 msg.dt.kern_sg_entries - sg_entries_sent); 12876 12877 if (do_sg_copy != 0) { 12878 struct ctl_sg_entry *sgl; 12879 int j; 12880 12881 sgl = (struct ctl_sg_entry *) 12882 io->scsiio.kern_data_ptr; 12883 /* 12884 * If this is in cached memory, flush the cache 12885 * before we send the DMA request to the other 12886 * controller. We want to do this in either 12887 * the * read or the write case. The read 12888 * case is straightforward. In the write 12889 * case, we want to make sure nothing is 12890 * in the local cache that could overwrite 12891 * the DMAed data. 12892 */ 12893 12894 for (i = sg_entries_sent, j = 0; 12895 i < msg.dt.cur_sg_entries; i++, j++) { 12896 if ((io->io_hdr.flags & 12897 CTL_FLAG_NO_DATASYNC) == 0) { 12898 /* 12899 * XXX KDM use bus_dmamap_sync() 12900 */ 12901 } 12902 if ((io->io_hdr.flags & 12903 CTL_FLAG_BUS_ADDR) == 0) { 12904 /* 12905 * XXX KDM use busdma. 12906 */ 12907#if 0 12908 msg.dt.sg_list[j].addr =(void *) 12909 vtophys(sgl[i].addr); 12910#endif 12911 } else { 12912 msg.dt.sg_list[j].addr = 12913 sgl[i].addr; 12914 } 12915 msg.dt.sg_list[j].len = sgl[i].len; 12916 } 12917 } 12918 12919 sg_entries_sent += msg.dt.cur_sg_entries; 12920 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12921 msg.dt.sg_last = 1; 12922 else 12923 msg.dt.sg_last = 0; 12924 12925 /* 12926 * XXX KDM drop and reacquire the lock here? 12927 */ 12928 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12929 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12930 /* 12931 * XXX do something here. 12932 */ 12933 } 12934 12935 msg.dt.sent_sg_entries = sg_entries_sent; 12936 } 12937 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12938 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12939 ctl_failover_io(io, /*have_lock*/ 0); 12940 12941 } else { 12942 12943 /* 12944 * Lookup the fe_datamove() function for this particular 12945 * front end. 12946 */ 12947 fe_datamove = 12948 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12949 12950 fe_datamove(io); 12951 } 12952} 12953 12954static void 12955ctl_send_datamove_done(union ctl_io *io, int have_lock) 12956{ 12957 union ctl_ha_msg msg; 12958 int isc_status; 12959 12960 memset(&msg, 0, sizeof(msg)); 12961 12962 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12963 msg.hdr.original_sc = io; 12964 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12965 msg.hdr.nexus = io->io_hdr.nexus; 12966 msg.hdr.status = io->io_hdr.status; 12967 msg.scsi.tag_num = io->scsiio.tag_num; 12968 msg.scsi.tag_type = io->scsiio.tag_type; 12969 msg.scsi.scsi_status = io->scsiio.scsi_status; 12970 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12971 sizeof(io->scsiio.sense_data)); 12972 msg.scsi.sense_len = io->scsiio.sense_len; 12973 msg.scsi.sense_residual = io->scsiio.sense_residual; 12974 msg.scsi.fetd_status = io->io_hdr.port_status; 12975 msg.scsi.residual = io->scsiio.residual; 12976 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12977 12978 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12979 ctl_failover_io(io, /*have_lock*/ have_lock); 12980 return; 12981 } 12982 12983 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12984 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12985 /* XXX do something if this fails */ 12986 } 12987 12988} 12989 12990/* 12991 * The DMA to the remote side is done, now we need to tell the other side 12992 * we're done so it can continue with its data movement. 12993 */ 12994static void 12995ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12996{ 12997 union ctl_io *io; 12998 12999 io = rq->context; 13000 13001 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13002 printf("%s: ISC DMA write failed with error %d", __func__, 13003 rq->ret); 13004 ctl_set_internal_failure(&io->scsiio, 13005 /*sks_valid*/ 1, 13006 /*retry_count*/ rq->ret); 13007 } 13008 13009 ctl_dt_req_free(rq); 13010 13011 /* 13012 * In this case, we had to malloc the memory locally. Free it. 13013 */ 13014 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13015 int i; 13016 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13017 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13018 } 13019 /* 13020 * The data is in local and remote memory, so now we need to send 13021 * status (good or back) back to the other side. 13022 */ 13023 ctl_send_datamove_done(io, /*have_lock*/ 0); 13024} 13025 13026/* 13027 * We've moved the data from the host/controller into local memory. Now we 13028 * need to push it over to the remote controller's memory. 13029 */ 13030static int 13031ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13032{ 13033 int retval; 13034 13035 retval = 0; 13036 13037 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13038 ctl_datamove_remote_write_cb); 13039 13040 return (retval); 13041} 13042 13043static void 13044ctl_datamove_remote_write(union ctl_io *io) 13045{ 13046 int retval; 13047 void (*fe_datamove)(union ctl_io *io); 13048 13049 /* 13050 * - Get the data from the host/HBA into local memory. 13051 * - DMA memory from the local controller to the remote controller. 13052 * - Send status back to the remote controller. 13053 */ 13054 13055 retval = ctl_datamove_remote_sgl_setup(io); 13056 if (retval != 0) 13057 return; 13058 13059 /* Switch the pointer over so the FETD knows what to do */ 13060 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13061 13062 /* 13063 * Use a custom move done callback, since we need to send completion 13064 * back to the other controller, not to the backend on this side. 13065 */ 13066 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13067 13068 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13069 13070 fe_datamove(io); 13071 13072 return; 13073 13074} 13075 13076static int 13077ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13078{ 13079#if 0 13080 char str[256]; 13081 char path_str[64]; 13082 struct sbuf sb; 13083#endif 13084 13085 /* 13086 * In this case, we had to malloc the memory locally. Free it. 13087 */ 13088 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13089 int i; 13090 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13091 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13092 } 13093 13094#if 0 13095 scsi_path_string(io, path_str, sizeof(path_str)); 13096 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13097 sbuf_cat(&sb, path_str); 13098 scsi_command_string(&io->scsiio, NULL, &sb); 13099 sbuf_printf(&sb, "\n"); 13100 sbuf_cat(&sb, path_str); 13101 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13102 io->scsiio.tag_num, io->scsiio.tag_type); 13103 sbuf_cat(&sb, path_str); 13104 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13105 io->io_hdr.flags, io->io_hdr.status); 13106 sbuf_finish(&sb); 13107 printk("%s", sbuf_data(&sb)); 13108#endif 13109 13110 13111 /* 13112 * The read is done, now we need to send status (good or bad) back 13113 * to the other side. 13114 */ 13115 ctl_send_datamove_done(io, /*have_lock*/ 0); 13116 13117 return (0); 13118} 13119 13120static void 13121ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13122{ 13123 union ctl_io *io; 13124 void (*fe_datamove)(union ctl_io *io); 13125 13126 io = rq->context; 13127 13128 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13129 printf("%s: ISC DMA read failed with error %d", __func__, 13130 rq->ret); 13131 ctl_set_internal_failure(&io->scsiio, 13132 /*sks_valid*/ 1, 13133 /*retry_count*/ rq->ret); 13134 } 13135 13136 ctl_dt_req_free(rq); 13137 13138 /* Switch the pointer over so the FETD knows what to do */ 13139 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13140 13141 /* 13142 * Use a custom move done callback, since we need to send completion 13143 * back to the other controller, not to the backend on this side. 13144 */ 13145 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13146 13147 /* XXX KDM add checks like the ones in ctl_datamove? */ 13148 13149 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13150 13151 fe_datamove(io); 13152} 13153 13154static int 13155ctl_datamove_remote_sgl_setup(union ctl_io *io) 13156{ 13157 struct ctl_sg_entry *local_sglist, *remote_sglist; 13158 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13159 struct ctl_softc *softc; 13160 int retval; 13161 int i; 13162 13163 retval = 0; 13164 softc = control_softc; 13165 13166 local_sglist = io->io_hdr.local_sglist; 13167 local_dma_sglist = io->io_hdr.local_dma_sglist; 13168 remote_sglist = io->io_hdr.remote_sglist; 13169 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13170 13171 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13172 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13173 local_sglist[i].len = remote_sglist[i].len; 13174 13175 /* 13176 * XXX Detect the situation where the RS-level I/O 13177 * redirector on the other side has already read the 13178 * data off of the AOR RS on this side, and 13179 * transferred it to remote (mirror) memory on the 13180 * other side. Since we already have the data in 13181 * memory here, we just need to use it. 13182 * 13183 * XXX KDM this can probably be removed once we 13184 * get the cache device code in and take the 13185 * current AOR implementation out. 13186 */ 13187#ifdef NEEDTOPORT 13188 if ((remote_sglist[i].addr >= 13189 (void *)vtophys(softc->mirr->addr)) 13190 && (remote_sglist[i].addr < 13191 ((void *)vtophys(softc->mirr->addr) + 13192 CacheMirrorOffset))) { 13193 local_sglist[i].addr = remote_sglist[i].addr - 13194 CacheMirrorOffset; 13195 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13196 CTL_FLAG_DATA_IN) 13197 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13198 } else { 13199 local_sglist[i].addr = remote_sglist[i].addr + 13200 CacheMirrorOffset; 13201 } 13202#endif 13203#if 0 13204 printf("%s: local %p, remote %p, len %d\n", 13205 __func__, local_sglist[i].addr, 13206 remote_sglist[i].addr, local_sglist[i].len); 13207#endif 13208 } 13209 } else { 13210 uint32_t len_to_go; 13211 13212 /* 13213 * In this case, we don't have automatically allocated 13214 * memory for this I/O on this controller. This typically 13215 * happens with internal CTL I/O -- e.g. inquiry, mode 13216 * sense, etc. Anything coming from RAIDCore will have 13217 * a mirror area available. 13218 */ 13219 len_to_go = io->scsiio.kern_data_len; 13220 13221 /* 13222 * Clear the no datasync flag, we have to use malloced 13223 * buffers. 13224 */ 13225 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13226 13227 /* 13228 * The difficult thing here is that the size of the various 13229 * S/G segments may be different than the size from the 13230 * remote controller. That'll make it harder when DMAing 13231 * the data back to the other side. 13232 */ 13233 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13234 sizeof(io->io_hdr.remote_sglist[0])) && 13235 (len_to_go > 0); i++) { 13236 local_sglist[i].len = MIN(len_to_go, 131072); 13237 CTL_SIZE_8B(local_dma_sglist[i].len, 13238 local_sglist[i].len); 13239 local_sglist[i].addr = 13240 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13241 13242 local_dma_sglist[i].addr = local_sglist[i].addr; 13243 13244 if (local_sglist[i].addr == NULL) { 13245 int j; 13246 13247 printf("malloc failed for %zd bytes!", 13248 local_dma_sglist[i].len); 13249 for (j = 0; j < i; j++) { 13250 free(local_sglist[j].addr, M_CTL); 13251 } 13252 ctl_set_internal_failure(&io->scsiio, 13253 /*sks_valid*/ 1, 13254 /*retry_count*/ 4857); 13255 retval = 1; 13256 goto bailout_error; 13257 13258 } 13259 /* XXX KDM do we need a sync here? */ 13260 13261 len_to_go -= local_sglist[i].len; 13262 } 13263 /* 13264 * Reset the number of S/G entries accordingly. The 13265 * original number of S/G entries is available in 13266 * rem_sg_entries. 13267 */ 13268 io->scsiio.kern_sg_entries = i; 13269 13270#if 0 13271 printf("%s: kern_sg_entries = %d\n", __func__, 13272 io->scsiio.kern_sg_entries); 13273 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13274 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13275 local_sglist[i].addr, local_sglist[i].len, 13276 local_dma_sglist[i].len); 13277#endif 13278 } 13279 13280 13281 return (retval); 13282 13283bailout_error: 13284 13285 ctl_send_datamove_done(io, /*have_lock*/ 0); 13286 13287 return (retval); 13288} 13289 13290static int 13291ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13292 ctl_ha_dt_cb callback) 13293{ 13294 struct ctl_ha_dt_req *rq; 13295 struct ctl_sg_entry *remote_sglist, *local_sglist; 13296 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13297 uint32_t local_used, remote_used, total_used; 13298 int retval; 13299 int i, j; 13300 13301 retval = 0; 13302 13303 rq = ctl_dt_req_alloc(); 13304 13305 /* 13306 * If we failed to allocate the request, and if the DMA didn't fail 13307 * anyway, set busy status. This is just a resource allocation 13308 * failure. 13309 */ 13310 if ((rq == NULL) 13311 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13312 ctl_set_busy(&io->scsiio); 13313 13314 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13315 13316 if (rq != NULL) 13317 ctl_dt_req_free(rq); 13318 13319 /* 13320 * The data move failed. We need to return status back 13321 * to the other controller. No point in trying to DMA 13322 * data to the remote controller. 13323 */ 13324 13325 ctl_send_datamove_done(io, /*have_lock*/ 0); 13326 13327 retval = 1; 13328 13329 goto bailout; 13330 } 13331 13332 local_sglist = io->io_hdr.local_sglist; 13333 local_dma_sglist = io->io_hdr.local_dma_sglist; 13334 remote_sglist = io->io_hdr.remote_sglist; 13335 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13336 local_used = 0; 13337 remote_used = 0; 13338 total_used = 0; 13339 13340 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13341 rq->ret = CTL_HA_STATUS_SUCCESS; 13342 rq->context = io; 13343 callback(rq); 13344 goto bailout; 13345 } 13346 13347 /* 13348 * Pull/push the data over the wire from/to the other controller. 13349 * This takes into account the possibility that the local and 13350 * remote sglists may not be identical in terms of the size of 13351 * the elements and the number of elements. 13352 * 13353 * One fundamental assumption here is that the length allocated for 13354 * both the local and remote sglists is identical. Otherwise, we've 13355 * essentially got a coding error of some sort. 13356 */ 13357 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13358 int isc_ret; 13359 uint32_t cur_len, dma_length; 13360 uint8_t *tmp_ptr; 13361 13362 rq->id = CTL_HA_DATA_CTL; 13363 rq->command = command; 13364 rq->context = io; 13365 13366 /* 13367 * Both pointers should be aligned. But it is possible 13368 * that the allocation length is not. They should both 13369 * also have enough slack left over at the end, though, 13370 * to round up to the next 8 byte boundary. 13371 */ 13372 cur_len = MIN(local_sglist[i].len - local_used, 13373 remote_sglist[j].len - remote_used); 13374 13375 /* 13376 * In this case, we have a size issue and need to decrease 13377 * the size, except in the case where we actually have less 13378 * than 8 bytes left. In that case, we need to increase 13379 * the DMA length to get the last bit. 13380 */ 13381 if ((cur_len & 0x7) != 0) { 13382 if (cur_len > 0x7) { 13383 cur_len = cur_len - (cur_len & 0x7); 13384 dma_length = cur_len; 13385 } else { 13386 CTL_SIZE_8B(dma_length, cur_len); 13387 } 13388 13389 } else 13390 dma_length = cur_len; 13391 13392 /* 13393 * If we had to allocate memory for this I/O, instead of using 13394 * the non-cached mirror memory, we'll need to flush the cache 13395 * before trying to DMA to the other controller. 13396 * 13397 * We could end up doing this multiple times for the same 13398 * segment if we have a larger local segment than remote 13399 * segment. That shouldn't be an issue. 13400 */ 13401 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13402 /* 13403 * XXX KDM use bus_dmamap_sync() here. 13404 */ 13405 } 13406 13407 rq->size = dma_length; 13408 13409 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13410 tmp_ptr += local_used; 13411 13412 /* Use physical addresses when talking to ISC hardware */ 13413 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13414 /* XXX KDM use busdma */ 13415#if 0 13416 rq->local = vtophys(tmp_ptr); 13417#endif 13418 } else 13419 rq->local = tmp_ptr; 13420 13421 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13422 tmp_ptr += remote_used; 13423 rq->remote = tmp_ptr; 13424 13425 rq->callback = NULL; 13426 13427 local_used += cur_len; 13428 if (local_used >= local_sglist[i].len) { 13429 i++; 13430 local_used = 0; 13431 } 13432 13433 remote_used += cur_len; 13434 if (remote_used >= remote_sglist[j].len) { 13435 j++; 13436 remote_used = 0; 13437 } 13438 total_used += cur_len; 13439 13440 if (total_used >= io->scsiio.kern_data_len) 13441 rq->callback = callback; 13442 13443 if ((rq->size & 0x7) != 0) { 13444 printf("%s: warning: size %d is not on 8b boundary\n", 13445 __func__, rq->size); 13446 } 13447 if (((uintptr_t)rq->local & 0x7) != 0) { 13448 printf("%s: warning: local %p not on 8b boundary\n", 13449 __func__, rq->local); 13450 } 13451 if (((uintptr_t)rq->remote & 0x7) != 0) { 13452 printf("%s: warning: remote %p not on 8b boundary\n", 13453 __func__, rq->local); 13454 } 13455#if 0 13456 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13457 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13458 rq->local, rq->remote, rq->size); 13459#endif 13460 13461 isc_ret = ctl_dt_single(rq); 13462 if (isc_ret == CTL_HA_STATUS_WAIT) 13463 continue; 13464 13465 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13466 rq->ret = CTL_HA_STATUS_SUCCESS; 13467 } else { 13468 rq->ret = isc_ret; 13469 } 13470 callback(rq); 13471 goto bailout; 13472 } 13473 13474bailout: 13475 return (retval); 13476 13477} 13478 13479static void 13480ctl_datamove_remote_read(union ctl_io *io) 13481{ 13482 int retval; 13483 int i; 13484 13485 /* 13486 * This will send an error to the other controller in the case of a 13487 * failure. 13488 */ 13489 retval = ctl_datamove_remote_sgl_setup(io); 13490 if (retval != 0) 13491 return; 13492 13493 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13494 ctl_datamove_remote_read_cb); 13495 if ((retval != 0) 13496 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13497 /* 13498 * Make sure we free memory if there was an error.. The 13499 * ctl_datamove_remote_xfer() function will send the 13500 * datamove done message, or call the callback with an 13501 * error if there is a problem. 13502 */ 13503 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13504 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13505 } 13506 13507 return; 13508} 13509 13510/* 13511 * Process a datamove request from the other controller. This is used for 13512 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13513 * first. Once that is complete, the data gets DMAed into the remote 13514 * controller's memory. For reads, we DMA from the remote controller's 13515 * memory into our memory first, and then move it out to the FETD. 13516 */ 13517static void 13518ctl_datamove_remote(union ctl_io *io) 13519{ 13520 struct ctl_softc *softc; 13521 13522 softc = control_softc; 13523 13524 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13525 13526 /* 13527 * Note that we look for an aborted I/O here, but don't do some of 13528 * the other checks that ctl_datamove() normally does. 13529 * We don't need to run the datamove delay code, since that should 13530 * have been done if need be on the other controller. 13531 */ 13532 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13533 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13534 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13535 io->io_hdr.nexus.targ_port, 13536 io->io_hdr.nexus.targ_target.id, 13537 io->io_hdr.nexus.targ_lun); 13538 io->io_hdr.port_status = 31338; 13539 ctl_send_datamove_done(io, /*have_lock*/ 0); 13540 return; 13541 } 13542 13543 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13544 ctl_datamove_remote_write(io); 13545 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13546 ctl_datamove_remote_read(io); 13547 } else { 13548 union ctl_ha_msg msg; 13549 struct scsi_sense_data *sense; 13550 uint8_t sks[3]; 13551 int retry_count; 13552 13553 memset(&msg, 0, sizeof(msg)); 13554 13555 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13556 msg.hdr.status = CTL_SCSI_ERROR; 13557 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13558 13559 retry_count = 4243; 13560 13561 sense = &msg.scsi.sense_data; 13562 sks[0] = SSD_SCS_VALID; 13563 sks[1] = (retry_count >> 8) & 0xff; 13564 sks[2] = retry_count & 0xff; 13565 13566 /* "Internal target failure" */ 13567 scsi_set_sense_data(sense, 13568 /*sense_format*/ SSD_TYPE_NONE, 13569 /*current_error*/ 1, 13570 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13571 /*asc*/ 0x44, 13572 /*ascq*/ 0x00, 13573 /*type*/ SSD_ELEM_SKS, 13574 /*size*/ sizeof(sks), 13575 /*data*/ sks, 13576 SSD_ELEM_NONE); 13577 13578 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13579 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13580 ctl_failover_io(io, /*have_lock*/ 1); 13581 return; 13582 } 13583 13584 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13585 CTL_HA_STATUS_SUCCESS) { 13586 /* XXX KDM what to do if this fails? */ 13587 } 13588 return; 13589 } 13590 13591} 13592 13593static int 13594ctl_process_done(union ctl_io *io) 13595{ 13596 struct ctl_lun *lun; 13597 struct ctl_softc *softc = control_softc; 13598 void (*fe_done)(union ctl_io *io); 13599 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13600 13601 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13602 13603 fe_done = softc->ctl_ports[targ_port]->fe_done; 13604 13605#ifdef CTL_TIME_IO 13606 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13607 char str[256]; 13608 char path_str[64]; 13609 struct sbuf sb; 13610 13611 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13612 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13613 13614 sbuf_cat(&sb, path_str); 13615 switch (io->io_hdr.io_type) { 13616 case CTL_IO_SCSI: 13617 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13618 sbuf_printf(&sb, "\n"); 13619 sbuf_cat(&sb, path_str); 13620 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13621 io->scsiio.tag_num, io->scsiio.tag_type); 13622 break; 13623 case CTL_IO_TASK: 13624 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13625 "Tag Type: %d\n", io->taskio.task_action, 13626 io->taskio.tag_num, io->taskio.tag_type); 13627 break; 13628 default: 13629 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13630 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13631 break; 13632 } 13633 sbuf_cat(&sb, path_str); 13634 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13635 (intmax_t)time_uptime - io->io_hdr.start_time); 13636 sbuf_finish(&sb); 13637 printf("%s", sbuf_data(&sb)); 13638 } 13639#endif /* CTL_TIME_IO */ 13640 13641 switch (io->io_hdr.io_type) { 13642 case CTL_IO_SCSI: 13643 break; 13644 case CTL_IO_TASK: 13645 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13646 ctl_io_error_print(io, NULL); 13647 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13648 ctl_free_io(io); 13649 else 13650 fe_done(io); 13651 return (CTL_RETVAL_COMPLETE); 13652 default: 13653 panic("ctl_process_done: invalid io type %d\n", 13654 io->io_hdr.io_type); 13655 break; /* NOTREACHED */ 13656 } 13657 13658 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13659 if (lun == NULL) { 13660 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13661 io->io_hdr.nexus.targ_mapped_lun)); 13662 goto bailout; 13663 } 13664 13665 mtx_lock(&lun->lun_lock); 13666 13667 /* 13668 * Check to see if we have any errors to inject here. We only 13669 * inject errors for commands that don't already have errors set. 13670 */ 13671 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13672 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13673 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13674 ctl_inject_error(lun, io); 13675 13676 /* 13677 * XXX KDM how do we treat commands that aren't completed 13678 * successfully? 13679 * 13680 * XXX KDM should we also track I/O latency? 13681 */ 13682 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13683 io->io_hdr.io_type == CTL_IO_SCSI) { 13684#ifdef CTL_TIME_IO 13685 struct bintime cur_bt; 13686#endif 13687 int type; 13688 13689 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13690 CTL_FLAG_DATA_IN) 13691 type = CTL_STATS_READ; 13692 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13693 CTL_FLAG_DATA_OUT) 13694 type = CTL_STATS_WRITE; 13695 else 13696 type = CTL_STATS_NO_IO; 13697 13698 lun->stats.ports[targ_port].bytes[type] += 13699 io->scsiio.kern_total_len; 13700 lun->stats.ports[targ_port].operations[type]++; 13701#ifdef CTL_TIME_IO 13702 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13703 &io->io_hdr.dma_bt); 13704 lun->stats.ports[targ_port].num_dmas[type] += 13705 io->io_hdr.num_dmas; 13706 getbintime(&cur_bt); 13707 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13708 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13709#endif 13710 } 13711 13712 /* 13713 * Remove this from the OOA queue. 13714 */ 13715 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13716#ifdef CTL_TIME_IO 13717 if (TAILQ_EMPTY(&lun->ooa_queue)) 13718 lun->last_busy = getsbinuptime(); 13719#endif 13720 13721 /* 13722 * Run through the blocked queue on this LUN and see if anything 13723 * has become unblocked, now that this transaction is done. 13724 */ 13725 ctl_check_blocked(lun); 13726 13727 /* 13728 * If the LUN has been invalidated, free it if there is nothing 13729 * left on its OOA queue. 13730 */ 13731 if ((lun->flags & CTL_LUN_INVALID) 13732 && TAILQ_EMPTY(&lun->ooa_queue)) { 13733 mtx_unlock(&lun->lun_lock); 13734 mtx_lock(&softc->ctl_lock); 13735 ctl_free_lun(lun); 13736 mtx_unlock(&softc->ctl_lock); 13737 } else 13738 mtx_unlock(&lun->lun_lock); 13739 13740bailout: 13741 13742 /* 13743 * If this command has been aborted, make sure we set the status 13744 * properly. The FETD is responsible for freeing the I/O and doing 13745 * whatever it needs to do to clean up its state. 13746 */ 13747 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13748 ctl_set_task_aborted(&io->scsiio); 13749 13750 /* 13751 * If enabled, print command error status. 13752 * We don't print UAs unless debugging was enabled explicitly. 13753 */ 13754 do { 13755 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13756 break; 13757 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13758 break; 13759 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13760 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13761 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13762 int error_code, sense_key, asc, ascq; 13763 13764 scsi_extract_sense_len(&io->scsiio.sense_data, 13765 io->scsiio.sense_len, &error_code, &sense_key, 13766 &asc, &ascq, /*show_errors*/ 0); 13767 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13768 break; 13769 } 13770 13771 ctl_io_error_print(io, NULL); 13772 } while (0); 13773 13774 /* 13775 * Tell the FETD or the other shelf controller we're done with this 13776 * command. Note that only SCSI commands get to this point. Task 13777 * management commands are completed above. 13778 * 13779 * We only send status to the other controller if we're in XFER 13780 * mode. In SER_ONLY mode, the I/O is done on the controller that 13781 * received the I/O (from CTL's perspective), and so the status is 13782 * generated there. 13783 * 13784 * XXX KDM if we hold the lock here, we could cause a deadlock 13785 * if the frontend comes back in in this context to queue 13786 * something. 13787 */ 13788 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13789 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13790 union ctl_ha_msg msg; 13791 13792 memset(&msg, 0, sizeof(msg)); 13793 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13794 msg.hdr.original_sc = io->io_hdr.original_sc; 13795 msg.hdr.nexus = io->io_hdr.nexus; 13796 msg.hdr.status = io->io_hdr.status; 13797 msg.scsi.scsi_status = io->scsiio.scsi_status; 13798 msg.scsi.tag_num = io->scsiio.tag_num; 13799 msg.scsi.tag_type = io->scsiio.tag_type; 13800 msg.scsi.sense_len = io->scsiio.sense_len; 13801 msg.scsi.sense_residual = io->scsiio.sense_residual; 13802 msg.scsi.residual = io->scsiio.residual; 13803 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13804 sizeof(io->scsiio.sense_data)); 13805 /* 13806 * We copy this whether or not this is an I/O-related 13807 * command. Otherwise, we'd have to go and check to see 13808 * whether it's a read/write command, and it really isn't 13809 * worth it. 13810 */ 13811 memcpy(&msg.scsi.lbalen, 13812 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13813 sizeof(msg.scsi.lbalen)); 13814 13815 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13816 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13817 /* XXX do something here */ 13818 } 13819 13820 ctl_free_io(io); 13821 } else 13822 fe_done(io); 13823 13824 return (CTL_RETVAL_COMPLETE); 13825} 13826 13827#ifdef CTL_WITH_CA 13828/* 13829 * Front end should call this if it doesn't do autosense. When the request 13830 * sense comes back in from the initiator, we'll dequeue this and send it. 13831 */ 13832int 13833ctl_queue_sense(union ctl_io *io) 13834{ 13835 struct ctl_lun *lun; 13836 struct ctl_port *port; 13837 struct ctl_softc *softc; 13838 uint32_t initidx, targ_lun; 13839 13840 softc = control_softc; 13841 13842 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13843 13844 /* 13845 * LUN lookup will likely move to the ctl_work_thread() once we 13846 * have our new queueing infrastructure (that doesn't put things on 13847 * a per-LUN queue initially). That is so that we can handle 13848 * things like an INQUIRY to a LUN that we don't have enabled. We 13849 * can't deal with that right now. 13850 */ 13851 mtx_lock(&softc->ctl_lock); 13852 13853 /* 13854 * If we don't have a LUN for this, just toss the sense 13855 * information. 13856 */ 13857 port = ctl_io_port(&ctsio->io_hdr); 13858 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13859 if ((targ_lun < CTL_MAX_LUNS) 13860 && (softc->ctl_luns[targ_lun] != NULL)) 13861 lun = softc->ctl_luns[targ_lun]; 13862 else 13863 goto bailout; 13864 13865 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13866 13867 mtx_lock(&lun->lun_lock); 13868 /* 13869 * Already have CA set for this LUN...toss the sense information. 13870 */ 13871 if (ctl_is_set(lun->have_ca, initidx)) { 13872 mtx_unlock(&lun->lun_lock); 13873 goto bailout; 13874 } 13875 13876 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13877 MIN(sizeof(lun->pending_sense[initidx]), 13878 sizeof(io->scsiio.sense_data))); 13879 ctl_set_mask(lun->have_ca, initidx); 13880 mtx_unlock(&lun->lun_lock); 13881 13882bailout: 13883 mtx_unlock(&softc->ctl_lock); 13884 13885 ctl_free_io(io); 13886 13887 return (CTL_RETVAL_COMPLETE); 13888} 13889#endif 13890 13891/* 13892 * Primary command inlet from frontend ports. All SCSI and task I/O 13893 * requests must go through this function. 13894 */ 13895int 13896ctl_queue(union ctl_io *io) 13897{ 13898 struct ctl_port *port; 13899 13900 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13901 13902#ifdef CTL_TIME_IO 13903 io->io_hdr.start_time = time_uptime; 13904 getbintime(&io->io_hdr.start_bt); 13905#endif /* CTL_TIME_IO */ 13906 13907 /* Map FE-specific LUN ID into global one. */ 13908 port = ctl_io_port(&io->io_hdr); 13909 io->io_hdr.nexus.targ_mapped_lun = 13910 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13911 13912 switch (io->io_hdr.io_type) { 13913 case CTL_IO_SCSI: 13914 case CTL_IO_TASK: 13915 if (ctl_debug & CTL_DEBUG_CDB) 13916 ctl_io_print(io); 13917 ctl_enqueue_incoming(io); 13918 break; 13919 default: 13920 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13921 return (EINVAL); 13922 } 13923 13924 return (CTL_RETVAL_COMPLETE); 13925} 13926 13927#ifdef CTL_IO_DELAY 13928static void 13929ctl_done_timer_wakeup(void *arg) 13930{ 13931 union ctl_io *io; 13932 13933 io = (union ctl_io *)arg; 13934 ctl_done(io); 13935} 13936#endif /* CTL_IO_DELAY */ 13937 13938void 13939ctl_done(union ctl_io *io) 13940{ 13941 13942 /* 13943 * Enable this to catch duplicate completion issues. 13944 */ 13945#if 0 13946 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13947 printf("%s: type %d msg %d cdb %x iptl: " 13948 "%d:%d:%d:%d tag 0x%04x " 13949 "flag %#x status %x\n", 13950 __func__, 13951 io->io_hdr.io_type, 13952 io->io_hdr.msg_type, 13953 io->scsiio.cdb[0], 13954 io->io_hdr.nexus.initid.id, 13955 io->io_hdr.nexus.targ_port, 13956 io->io_hdr.nexus.targ_target.id, 13957 io->io_hdr.nexus.targ_lun, 13958 (io->io_hdr.io_type == 13959 CTL_IO_TASK) ? 13960 io->taskio.tag_num : 13961 io->scsiio.tag_num, 13962 io->io_hdr.flags, 13963 io->io_hdr.status); 13964 } else 13965 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13966#endif 13967 13968 /* 13969 * This is an internal copy of an I/O, and should not go through 13970 * the normal done processing logic. 13971 */ 13972 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13973 return; 13974 13975 /* 13976 * We need to send a msg to the serializing shelf to finish the IO 13977 * as well. We don't send a finish message to the other shelf if 13978 * this is a task management command. Task management commands 13979 * aren't serialized in the OOA queue, but rather just executed on 13980 * both shelf controllers for commands that originated on that 13981 * controller. 13982 */ 13983 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13984 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13985 union ctl_ha_msg msg_io; 13986 13987 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13988 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13989 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13990 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13991 } 13992 /* continue on to finish IO */ 13993 } 13994#ifdef CTL_IO_DELAY 13995 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13996 struct ctl_lun *lun; 13997 13998 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13999 14000 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14001 } else { 14002 struct ctl_lun *lun; 14003 14004 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14005 14006 if ((lun != NULL) 14007 && (lun->delay_info.done_delay > 0)) { 14008 struct callout *callout; 14009 14010 callout = (struct callout *)&io->io_hdr.timer_bytes; 14011 callout_init(callout, /*mpsafe*/ 1); 14012 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14013 callout_reset(callout, 14014 lun->delay_info.done_delay * hz, 14015 ctl_done_timer_wakeup, io); 14016 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14017 lun->delay_info.done_delay = 0; 14018 return; 14019 } 14020 } 14021#endif /* CTL_IO_DELAY */ 14022 14023 ctl_enqueue_done(io); 14024} 14025 14026int 14027ctl_isc(struct ctl_scsiio *ctsio) 14028{ 14029 struct ctl_lun *lun; 14030 int retval; 14031 14032 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14033 14034 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14035 14036 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14037 14038 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14039 14040 return (retval); 14041} 14042 14043 14044static void 14045ctl_work_thread(void *arg) 14046{ 14047 struct ctl_thread *thr = (struct ctl_thread *)arg; 14048 struct ctl_softc *softc = thr->ctl_softc; 14049 union ctl_io *io; 14050 int retval; 14051 14052 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14053 14054 for (;;) { 14055 retval = 0; 14056 14057 /* 14058 * We handle the queues in this order: 14059 * - ISC 14060 * - done queue (to free up resources, unblock other commands) 14061 * - RtR queue 14062 * - incoming queue 14063 * 14064 * If those queues are empty, we break out of the loop and 14065 * go to sleep. 14066 */ 14067 mtx_lock(&thr->queue_lock); 14068 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14069 if (io != NULL) { 14070 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14071 mtx_unlock(&thr->queue_lock); 14072 ctl_handle_isc(io); 14073 continue; 14074 } 14075 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14076 if (io != NULL) { 14077 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14078 /* clear any blocked commands, call fe_done */ 14079 mtx_unlock(&thr->queue_lock); 14080 retval = ctl_process_done(io); 14081 continue; 14082 } 14083 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14084 if (io != NULL) { 14085 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14086 mtx_unlock(&thr->queue_lock); 14087 if (io->io_hdr.io_type == CTL_IO_TASK) 14088 ctl_run_task(io); 14089 else 14090 ctl_scsiio_precheck(softc, &io->scsiio); 14091 continue; 14092 } 14093 if (!ctl_pause_rtr) { 14094 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14095 if (io != NULL) { 14096 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14097 mtx_unlock(&thr->queue_lock); 14098 retval = ctl_scsiio(&io->scsiio); 14099 if (retval != CTL_RETVAL_COMPLETE) 14100 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14101 continue; 14102 } 14103 } 14104 14105 /* Sleep until we have something to do. */ 14106 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14107 } 14108} 14109 14110static void 14111ctl_lun_thread(void *arg) 14112{ 14113 struct ctl_softc *softc = (struct ctl_softc *)arg; 14114 struct ctl_be_lun *be_lun; 14115 int retval; 14116 14117 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14118 14119 for (;;) { 14120 retval = 0; 14121 mtx_lock(&softc->ctl_lock); 14122 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14123 if (be_lun != NULL) { 14124 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14125 mtx_unlock(&softc->ctl_lock); 14126 ctl_create_lun(be_lun); 14127 continue; 14128 } 14129 14130 /* Sleep until we have something to do. */ 14131 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14132 PDROP | PRIBIO, "-", 0); 14133 } 14134} 14135 14136static void 14137ctl_thresh_thread(void *arg) 14138{ 14139 struct ctl_softc *softc = (struct ctl_softc *)arg; 14140 struct ctl_lun *lun; 14141 struct ctl_be_lun *be_lun; 14142 struct scsi_da_rw_recovery_page *rwpage; 14143 struct ctl_logical_block_provisioning_page *page; 14144 const char *attr; 14145 uint64_t thres, val; 14146 int i, e; 14147 14148 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14149 14150 for (;;) { 14151 mtx_lock(&softc->ctl_lock); 14152 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14153 be_lun = lun->be_lun; 14154 if ((lun->flags & CTL_LUN_DISABLED) || 14155 (lun->flags & CTL_LUN_OFFLINE) || 14156 lun->backend->lun_attr == NULL) 14157 continue; 14158 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14159 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14160 continue; 14161 e = 0; 14162 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14163 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14164 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14165 continue; 14166 thres = scsi_4btoul(page->descr[i].count); 14167 thres <<= CTL_LBP_EXPONENT; 14168 switch (page->descr[i].resource) { 14169 case 0x01: 14170 attr = "blocksavail"; 14171 break; 14172 case 0x02: 14173 attr = "blocksused"; 14174 break; 14175 case 0xf1: 14176 attr = "poolblocksavail"; 14177 break; 14178 case 0xf2: 14179 attr = "poolblocksused"; 14180 break; 14181 default: 14182 continue; 14183 } 14184 mtx_unlock(&softc->ctl_lock); // XXX 14185 val = lun->backend->lun_attr( 14186 lun->be_lun->be_lun, attr); 14187 mtx_lock(&softc->ctl_lock); 14188 if (val == UINT64_MAX) 14189 continue; 14190 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14191 == SLBPPD_ARMING_INC) 14192 e |= (val >= thres); 14193 else 14194 e |= (val <= thres); 14195 } 14196 mtx_lock(&lun->lun_lock); 14197 if (e) { 14198 if (lun->lasttpt == 0 || 14199 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14200 lun->lasttpt = time_uptime; 14201 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14202 } 14203 } else { 14204 lun->lasttpt = 0; 14205 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14206 } 14207 mtx_unlock(&lun->lun_lock); 14208 } 14209 mtx_unlock(&softc->ctl_lock); 14210 pause("-", CTL_LBP_PERIOD * hz); 14211 } 14212} 14213 14214static void 14215ctl_enqueue_incoming(union ctl_io *io) 14216{ 14217 struct ctl_softc *softc = control_softc; 14218 struct ctl_thread *thr; 14219 u_int idx; 14220 14221 idx = (io->io_hdr.nexus.targ_port * 127 + 14222 io->io_hdr.nexus.initid.id) % worker_threads; 14223 thr = &softc->threads[idx]; 14224 mtx_lock(&thr->queue_lock); 14225 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14226 mtx_unlock(&thr->queue_lock); 14227 wakeup(thr); 14228} 14229 14230static void 14231ctl_enqueue_rtr(union ctl_io *io) 14232{ 14233 struct ctl_softc *softc = control_softc; 14234 struct ctl_thread *thr; 14235 14236 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14237 mtx_lock(&thr->queue_lock); 14238 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14239 mtx_unlock(&thr->queue_lock); 14240 wakeup(thr); 14241} 14242 14243static void 14244ctl_enqueue_done(union ctl_io *io) 14245{ 14246 struct ctl_softc *softc = control_softc; 14247 struct ctl_thread *thr; 14248 14249 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14250 mtx_lock(&thr->queue_lock); 14251 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14252 mtx_unlock(&thr->queue_lock); 14253 wakeup(thr); 14254} 14255 14256static void 14257ctl_enqueue_isc(union ctl_io *io) 14258{ 14259 struct ctl_softc *softc = control_softc; 14260 struct ctl_thread *thr; 14261 14262 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14263 mtx_lock(&thr->queue_lock); 14264 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14265 mtx_unlock(&thr->queue_lock); 14266 wakeup(thr); 14267} 14268 14269/* Initialization and failover */ 14270 14271void 14272ctl_init_isc_msg(void) 14273{ 14274 printf("CTL: Still calling this thing\n"); 14275} 14276 14277/* 14278 * Init component 14279 * Initializes component into configuration defined by bootMode 14280 * (see hasc-sv.c) 14281 * returns hasc_Status: 14282 * OK 14283 * ERROR - fatal error 14284 */ 14285static ctl_ha_comp_status 14286ctl_isc_init(struct ctl_ha_component *c) 14287{ 14288 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14289 14290 c->status = ret; 14291 return ret; 14292} 14293 14294/* Start component 14295 * Starts component in state requested. If component starts successfully, 14296 * it must set its own state to the requestrd state 14297 * When requested state is HASC_STATE_HA, the component may refine it 14298 * by adding _SLAVE or _MASTER flags. 14299 * Currently allowed state transitions are: 14300 * UNKNOWN->HA - initial startup 14301 * UNKNOWN->SINGLE - initial startup when no parter detected 14302 * HA->SINGLE - failover 14303 * returns ctl_ha_comp_status: 14304 * OK - component successfully started in requested state 14305 * FAILED - could not start the requested state, failover may 14306 * be possible 14307 * ERROR - fatal error detected, no future startup possible 14308 */ 14309static ctl_ha_comp_status 14310ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14311{ 14312 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14313 14314 printf("%s: go\n", __func__); 14315 14316 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14317 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14318 control_softc->is_single = 0; 14319 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14320 != CTL_HA_STATUS_SUCCESS) { 14321 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14322 ret = CTL_HA_COMP_STATUS_ERROR; 14323 } 14324 } else if (CTL_HA_STATE_IS_HA(c->state) 14325 && CTL_HA_STATE_IS_SINGLE(state)){ 14326 // HA->SINGLE transition 14327 ctl_failover(); 14328 control_softc->is_single = 1; 14329 } else { 14330 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14331 c->state, state); 14332 ret = CTL_HA_COMP_STATUS_ERROR; 14333 } 14334 if (CTL_HA_STATE_IS_SINGLE(state)) 14335 control_softc->is_single = 1; 14336 14337 c->state = state; 14338 c->status = ret; 14339 return ret; 14340} 14341 14342/* 14343 * Quiesce component 14344 * The component must clear any error conditions (set status to OK) and 14345 * prepare itself to another Start call 14346 * returns ctl_ha_comp_status: 14347 * OK 14348 * ERROR 14349 */ 14350static ctl_ha_comp_status 14351ctl_isc_quiesce(struct ctl_ha_component *c) 14352{ 14353 int ret = CTL_HA_COMP_STATUS_OK; 14354 14355 ctl_pause_rtr = 1; 14356 c->status = ret; 14357 return ret; 14358} 14359 14360struct ctl_ha_component ctl_ha_component_ctlisc = 14361{ 14362 .name = "CTL ISC", 14363 .state = CTL_HA_STATE_UNKNOWN, 14364 .init = ctl_isc_init, 14365 .start = ctl_isc_start, 14366 .quiesce = ctl_isc_quiesce 14367}; 14368 14369/* 14370 * vim: ts=8 14371 */ 14372